blob: 2c8754bd4b32f65ecfd84088181cbad4126efa4e [file] [log] [blame]
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001/*
2 * wpa_supplicant - P2P
3 * Copyright (c) 2009-2010, Atheros Communications
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08004 * Copyright (c) 2010-2014, Jouni Malinen <j@w1.fi>
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005 *
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08006 * This software may be distributed under the terms of the BSD license.
7 * See README for more details.
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008 */
9
10#include "includes.h"
11
12#include "common.h"
13#include "eloop.h"
14#include "common/ieee802_11_common.h"
15#include "common/ieee802_11_defs.h"
16#include "common/wpa_ctrl.h"
17#include "wps/wps_i.h"
18#include "p2p/p2p.h"
19#include "ap/hostapd.h"
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080020#include "ap/ap_config.h"
Dmitry Shmidt391c59f2013-09-03 12:16:28 -070021#include "ap/sta_info.h"
22#include "ap/ap_drv_ops.h"
Dmitry Shmidtcf32e602014-01-28 10:57:39 -080023#include "ap/wps_hostapd.h"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070024#include "ap/p2p_hostapd.h"
Dmitry Shmidt203eadb2015-03-05 14:16:04 -080025#include "ap/dfs.h"
Jouni Malinen75ecf522011-06-27 15:19:46 -070026#include "eapol_supp/eapol_supp_sm.h"
27#include "rsn_supp/wpa.h"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070028#include "wpa_supplicant_i.h"
29#include "driver_i.h"
30#include "ap.h"
31#include "config_ssid.h"
32#include "config.h"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070033#include "notify.h"
34#include "scan.h"
35#include "bss.h"
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080036#include "offchannel.h"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070037#include "wps_supplicant.h"
38#include "p2p_supplicant.h"
Dmitry Shmidt04f534e2013-12-09 15:50:16 -080039#include "wifi_display.h"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070040
41
42/*
43 * How many times to try to scan to find the GO before giving up on join
44 * request.
45 */
46#define P2P_MAX_JOIN_SCAN_ATTEMPTS 10
47
Dmitry Shmidt04949592012-07-19 12:16:46 -070048#define P2P_AUTO_PD_SCAN_ATTEMPTS 5
49
Dmitry Shmidtd80a4012015-11-05 16:35:40 -080050/**
51 * Defines time interval in seconds when a GO needs to evacuate a frequency that
52 * it is currently using, but is no longer valid for P2P use cases.
53 */
54#define P2P_GO_FREQ_CHANGE_TIME 5
55
56/**
57 * Defines CSA parameters which are used when GO evacuates the no longer valid
58 * channel (and if the driver supports channel switch).
59 */
60#define P2P_GO_CSA_COUNT 7
61#define P2P_GO_CSA_BLOCK_TX 0
62
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080063#ifndef P2P_MAX_CLIENT_IDLE
64/*
65 * How many seconds to try to reconnect to the GO when connection in P2P client
66 * role has been lost.
67 */
68#define P2P_MAX_CLIENT_IDLE 10
69#endif /* P2P_MAX_CLIENT_IDLE */
70
Dmitry Shmidt04949592012-07-19 12:16:46 -070071#ifndef P2P_MAX_INITIAL_CONN_WAIT
72/*
73 * How many seconds to wait for initial 4-way handshake to get completed after
Dmitry Shmidt15907092014-03-25 10:42:57 -070074 * WPS provisioning step or after the re-invocation of a persistent group on a
75 * P2P Client.
Dmitry Shmidt04949592012-07-19 12:16:46 -070076 */
77#define P2P_MAX_INITIAL_CONN_WAIT 10
78#endif /* P2P_MAX_INITIAL_CONN_WAIT */
79
Dmitry Shmidt92c368d2013-08-29 12:37:21 -070080#ifndef P2P_MAX_INITIAL_CONN_WAIT_GO
81/*
82 * How many seconds to wait for initial 4-way handshake to get completed after
83 * WPS provisioning step on the GO. This controls the extra time the P2P
84 * operation is considered to be in progress (e.g., to delay other scans) after
85 * WPS provisioning has been completed on the GO during group formation.
86 */
87#define P2P_MAX_INITIAL_CONN_WAIT_GO 10
88#endif /* P2P_MAX_INITIAL_CONN_WAIT_GO */
89
Dmitry Shmidt56052862013-10-04 10:23:25 -070090#ifndef P2P_MAX_INITIAL_CONN_WAIT_GO_REINVOKE
91/*
92 * How many seconds to wait for initial 4-way handshake to get completed after
93 * re-invocation of a persistent group on the GO when the client is expected
94 * to connect automatically (no user interaction).
95 */
96#define P2P_MAX_INITIAL_CONN_WAIT_GO_REINVOKE 15
97#endif /* P2P_MAX_INITIAL_CONN_WAIT_GO_REINVOKE */
98
Dmitry Shmidt34af3062013-07-11 10:46:32 -070099#define P2P_MGMT_DEVICE_PREFIX "p2p-dev-"
100
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800101/*
102 * How many seconds to wait to re-attempt to move GOs, in case previous attempt
103 * was not possible.
104 */
105#define P2P_RECONSIDER_GO_MOVE_DELAY 30
106
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700107enum p2p_group_removal_reason {
108 P2P_GROUP_REMOVAL_UNKNOWN,
109 P2P_GROUP_REMOVAL_SILENT,
110 P2P_GROUP_REMOVAL_FORMATION_FAILED,
111 P2P_GROUP_REMOVAL_REQUESTED,
112 P2P_GROUP_REMOVAL_IDLE_TIMEOUT,
113 P2P_GROUP_REMOVAL_UNAVAILABLE,
114 P2P_GROUP_REMOVAL_GO_ENDING_SESSION,
Dmitry Shmidt04f534e2013-12-09 15:50:16 -0800115 P2P_GROUP_REMOVAL_PSK_FAILURE,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800116 P2P_GROUP_REMOVAL_FREQ_CONFLICT,
117 P2P_GROUP_REMOVAL_GO_LEAVE_CHANNEL
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700118};
119
Jouni Malinendc7b7132012-09-14 12:53:47 -0700120
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700121static void wpas_p2p_long_listen_timeout(void *eloop_ctx, void *timeout_ctx);
122static struct wpa_supplicant *
123wpas_p2p_get_group_iface(struct wpa_supplicant *wpa_s, int addr_allocated,
124 int go);
Dmitry Shmidtcf32e602014-01-28 10:57:39 -0800125static int wpas_p2p_join_start(struct wpa_supplicant *wpa_s, int freq,
126 const u8 *ssid, size_t ssid_len);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800127static int wpas_p2p_setup_freqs(struct wpa_supplicant *wpa_s, int freq,
128 int *force_freq, int *pref_freq, int go,
129 unsigned int *pref_freq_list,
130 unsigned int *num_pref_freq);
Dmitry Shmidt344abd32014-01-14 13:17:00 -0800131static void wpas_p2p_join_scan_req(struct wpa_supplicant *wpa_s, int freq,
132 const u8 *ssid, size_t ssid_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700133static void wpas_p2p_join_scan(void *eloop_ctx, void *timeout_ctx);
134static int wpas_p2p_join(struct wpa_supplicant *wpa_s, const u8 *iface_addr,
Dmitry Shmidt04949592012-07-19 12:16:46 -0700135 const u8 *dev_addr, enum p2p_wps_method wps_method,
Dmitry Shmidtcf32e602014-01-28 10:57:39 -0800136 int auto_join, int freq,
137 const u8 *ssid, size_t ssid_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700138static int wpas_p2p_create_iface(struct wpa_supplicant *wpa_s);
139static void wpas_p2p_cross_connect_setup(struct wpa_supplicant *wpa_s);
140static void wpas_p2p_group_idle_timeout(void *eloop_ctx, void *timeout_ctx);
141static void wpas_p2p_set_group_idle_timeout(struct wpa_supplicant *wpa_s);
Dmitry Shmidtf8623282013-02-20 14:34:59 -0800142static void wpas_p2p_group_formation_timeout(void *eloop_ctx,
143 void *timeout_ctx);
Dmitry Shmidt04f534e2013-12-09 15:50:16 -0800144static void wpas_p2p_group_freq_conflict(void *eloop_ctx, void *timeout_ctx);
Dmitry Shmidt7f656022015-02-25 14:36:37 -0800145static int wpas_p2p_fallback_to_go_neg(struct wpa_supplicant *wpa_s,
146 int group_added);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800147static void wpas_p2p_stop_find_oper(struct wpa_supplicant *wpa_s);
Dmitry Shmidtbd14a572014-02-18 10:33:49 -0800148static void wpas_stop_listen(void *ctx);
Dmitry Shmidt684785c2014-05-12 13:34:29 -0700149static void wpas_p2p_psk_failure_removal(void *eloop_ctx, void *timeout_ctx);
Dmitry Shmidt43cb5782014-06-16 16:23:22 -0700150static void wpas_p2p_group_deinit(struct wpa_supplicant *wpa_s);
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800151static int wpas_p2p_add_group_interface(struct wpa_supplicant *wpa_s,
152 enum wpa_driver_if_type type);
Dmitry Shmidta3dc3092015-06-23 11:21:28 -0700153static void wpas_p2p_group_formation_failed(struct wpa_supplicant *wpa_s,
154 int already_deleted);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800155static void wpas_p2p_optimize_listen_channel(struct wpa_supplicant *wpa_s,
156 struct wpa_used_freq_data *freqs,
157 unsigned int num);
158static void wpas_p2p_move_go(void *eloop_ctx, void *timeout_ctx);
159static int wpas_p2p_go_is_peer_freq(struct wpa_supplicant *wpa_s, int freq);
160static void
161wpas_p2p_consider_moving_gos(struct wpa_supplicant *wpa_s,
162 struct wpa_used_freq_data *freqs, unsigned int num,
163 enum wpas_p2p_channel_update_trig trig);
164static void wpas_p2p_reconsider_moving_go(void *eloop_ctx, void *timeout_ctx);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700165
166
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -0700167/*
168 * Get the number of concurrent channels that the HW can operate, but that are
169 * currently not in use by any of the wpa_supplicant interfaces.
170 */
171static int wpas_p2p_num_unused_channels(struct wpa_supplicant *wpa_s)
172{
173 int *freqs;
Dmitry Shmidtb96dad42013-11-05 10:07:29 -0800174 int num, unused;
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -0700175
176 freqs = os_calloc(wpa_s->num_multichan_concurrent, sizeof(int));
177 if (!freqs)
178 return -1;
179
180 num = get_shared_radio_freqs(wpa_s, freqs,
181 wpa_s->num_multichan_concurrent);
182 os_free(freqs);
183
Dmitry Shmidtb96dad42013-11-05 10:07:29 -0800184 unused = wpa_s->num_multichan_concurrent - num;
185 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: num_unused_channels: %d", unused);
186 return unused;
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -0700187}
188
189
190/*
191 * Get the frequencies that are currently in use by one or more of the virtual
192 * interfaces, and that are also valid for P2P operation.
193 */
Dmitry Shmidt43cb5782014-06-16 16:23:22 -0700194static unsigned int
195wpas_p2p_valid_oper_freqs(struct wpa_supplicant *wpa_s,
196 struct wpa_used_freq_data *p2p_freqs,
197 unsigned int len)
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -0700198{
Dmitry Shmidt43cb5782014-06-16 16:23:22 -0700199 struct wpa_used_freq_data *freqs;
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -0700200 unsigned int num, i, j;
201
Dmitry Shmidt43cb5782014-06-16 16:23:22 -0700202 freqs = os_calloc(wpa_s->num_multichan_concurrent,
203 sizeof(struct wpa_used_freq_data));
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -0700204 if (!freqs)
Dmitry Shmidt43cb5782014-06-16 16:23:22 -0700205 return 0;
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -0700206
Dmitry Shmidt43cb5782014-06-16 16:23:22 -0700207 num = get_shared_radio_freqs_data(wpa_s, freqs,
208 wpa_s->num_multichan_concurrent);
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -0700209
Dmitry Shmidt43cb5782014-06-16 16:23:22 -0700210 os_memset(p2p_freqs, 0, sizeof(struct wpa_used_freq_data) * len);
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -0700211
212 for (i = 0, j = 0; i < num && j < len; i++) {
Dmitry Shmidt43cb5782014-06-16 16:23:22 -0700213 if (p2p_supported_freq(wpa_s->global->p2p, freqs[i].freq))
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -0700214 p2p_freqs[j++] = freqs[i];
215 }
216
217 os_free(freqs);
218
Dmitry Shmidt43cb5782014-06-16 16:23:22 -0700219 dump_freq_data(wpa_s, "valid for P2P", p2p_freqs, j);
Dmitry Shmidtb96dad42013-11-05 10:07:29 -0800220
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -0700221 return j;
222}
223
224
Dmitry Shmidt700a1372013-03-15 14:14:44 -0700225static void wpas_p2p_set_own_freq_preference(struct wpa_supplicant *wpa_s,
226 int freq)
227{
228 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
229 return;
Dmitry Shmidt8bd70b72015-05-26 16:02:19 -0700230
231 /* Use the wpa_s used to control the P2P Device operation */
232 wpa_s = wpa_s->global->p2p_init_wpa_s;
233
234 if (wpa_s->conf->p2p_ignore_shared_freq &&
Dmitry Shmidta0d265f2013-11-19 13:13:41 -0800235 freq > 0 && wpa_s->num_multichan_concurrent > 1 &&
236 wpas_p2p_num_unused_channels(wpa_s) > 0) {
237 wpa_printf(MSG_DEBUG, "P2P: Ignore own channel preference %d MHz due to p2p_ignore_shared_freq=1 configuration",
238 freq);
Dmitry Shmidt700a1372013-03-15 14:14:44 -0700239 freq = 0;
Dmitry Shmidta0d265f2013-11-19 13:13:41 -0800240 }
Dmitry Shmidt700a1372013-03-15 14:14:44 -0700241 p2p_set_own_freq_preference(wpa_s->global->p2p, freq);
242}
243
244
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700245static void wpas_p2p_scan_res_handler(struct wpa_supplicant *wpa_s,
246 struct wpa_scan_results *scan_res)
247{
248 size_t i;
249
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800250 if (wpa_s->p2p_scan_work) {
251 struct wpa_radio_work *work = wpa_s->p2p_scan_work;
252 wpa_s->p2p_scan_work = NULL;
253 radio_work_done(work);
254 }
255
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700256 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
257 return;
258
259 wpa_printf(MSG_DEBUG, "P2P: Scan results received (%d BSS)",
260 (int) scan_res->num);
261
262 for (i = 0; i < scan_res->num; i++) {
263 struct wpa_scan_res *bss = scan_res->res[i];
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800264 struct os_reltime time_tmp_age, entry_ts;
Dmitry Shmidt96571392013-10-14 12:54:46 -0700265 const u8 *ies;
266 size_t ies_len;
267
Dmitry Shmidtf8623282013-02-20 14:34:59 -0800268 time_tmp_age.sec = bss->age / 1000;
269 time_tmp_age.usec = (bss->age % 1000) * 1000;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800270 os_reltime_sub(&scan_res->fetch_time, &time_tmp_age, &entry_ts);
Dmitry Shmidt96571392013-10-14 12:54:46 -0700271
272 ies = (const u8 *) (bss + 1);
273 ies_len = bss->ie_len;
274 if (bss->beacon_ie_len > 0 &&
275 !wpa_scan_get_vendor_ie(bss, P2P_IE_VENDOR_TYPE) &&
276 wpa_scan_get_vendor_ie_beacon(bss, P2P_IE_VENDOR_TYPE)) {
277 wpa_printf(MSG_DEBUG, "P2P: Use P2P IE(s) from Beacon frame since no P2P IE(s) in Probe Response frames received for "
278 MACSTR, MAC2STR(bss->bssid));
279 ies = ies + ies_len;
280 ies_len = bss->beacon_ie_len;
281 }
282
283
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700284 if (p2p_scan_res_handler(wpa_s->global->p2p, bss->bssid,
Dmitry Shmidtf8623282013-02-20 14:34:59 -0800285 bss->freq, &entry_ts, bss->level,
Dmitry Shmidt96571392013-10-14 12:54:46 -0700286 ies, ies_len) > 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700287 break;
288 }
289
290 p2p_scan_res_handled(wpa_s->global->p2p);
291}
292
293
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800294static void wpas_p2p_trigger_scan_cb(struct wpa_radio_work *work, int deinit)
295{
296 struct wpa_supplicant *wpa_s = work->wpa_s;
297 struct wpa_driver_scan_params *params = work->ctx;
298 int ret;
299
300 if (deinit) {
Dmitry Shmidtbd14a572014-02-18 10:33:49 -0800301 if (!work->started) {
302 wpa_scan_free_params(params);
303 return;
304 }
305
306 wpa_s->p2p_scan_work = NULL;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800307 return;
308 }
309
Dmitry Shmidtebd93af2017-02-21 13:40:44 -0800310 if (wpa_s->clear_driver_scan_cache) {
311 wpa_printf(MSG_DEBUG,
312 "Request driver to clear scan cache due to local BSS flush");
313 params->only_new_results = 1;
314 }
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800315 ret = wpa_drv_scan(wpa_s, params);
Dmitry Shmidtabb90a32016-12-05 15:34:39 -0800316 if (ret == 0)
317 wpa_s->curr_scan_cookie = params->scan_cookie;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800318 wpa_scan_free_params(params);
319 work->ctx = NULL;
320 if (ret) {
321 radio_work_done(work);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800322 p2p_notify_scan_trigger_status(wpa_s->global->p2p, ret);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800323 return;
324 }
325
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800326 p2p_notify_scan_trigger_status(wpa_s->global->p2p, ret);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800327 os_get_reltime(&wpa_s->scan_trigger_time);
328 wpa_s->scan_res_handler = wpas_p2p_scan_res_handler;
329 wpa_s->own_scan_requested = 1;
Dmitry Shmidtebd93af2017-02-21 13:40:44 -0800330 wpa_s->clear_driver_scan_cache = 0;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800331 wpa_s->p2p_scan_work = work;
332}
333
334
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800335static int wpas_p2p_search_social_channel(struct wpa_supplicant *wpa_s,
336 int freq)
337{
338 if (wpa_s->global->p2p_24ghz_social_channels &&
339 (freq == 2412 || freq == 2437 || freq == 2462)) {
340 /*
341 * Search all social channels regardless of whether these have
342 * been disabled for P2P operating channel use to avoid missing
343 * peers.
344 */
345 return 1;
346 }
347 return p2p_supported_freq(wpa_s->global->p2p, freq);
348}
349
350
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700351static int wpas_p2p_scan(void *ctx, enum p2p_scan_type type, int freq,
352 unsigned int num_req_dev_types,
Dmitry Shmidt04949592012-07-19 12:16:46 -0700353 const u8 *req_dev_types, const u8 *dev_id, u16 pw_id)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700354{
355 struct wpa_supplicant *wpa_s = ctx;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800356 struct wpa_driver_scan_params *params = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700357 struct wpabuf *wps_ie, *ies;
Dmitry Shmidt661b4f72014-09-29 14:58:27 -0700358 unsigned int num_channels = 0;
359 int social_channels_freq[] = { 2412, 2437, 2462, 60480 };
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800360 size_t ielen;
Dmitry Shmidt661b4f72014-09-29 14:58:27 -0700361 u8 *n, i;
Dmitry Shmidt9c175262016-03-03 10:20:07 -0800362 unsigned int bands;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700363
364 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
365 return -1;
366
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800367 if (wpa_s->p2p_scan_work) {
368 wpa_dbg(wpa_s, MSG_INFO, "P2P: Reject scan trigger since one is already pending");
369 return -1;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700370 }
371
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800372 params = os_zalloc(sizeof(*params));
373 if (params == NULL)
374 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700375
376 /* P2P Wildcard SSID */
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800377 params->num_ssids = 1;
378 n = os_malloc(P2P_WILDCARD_SSID_LEN);
379 if (n == NULL)
380 goto fail;
381 os_memcpy(n, P2P_WILDCARD_SSID, P2P_WILDCARD_SSID_LEN);
382 params->ssids[0].ssid = n;
383 params->ssids[0].ssid_len = P2P_WILDCARD_SSID_LEN;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700384
385 wpa_s->wps->dev.p2p = 1;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700386 wps_ie = wps_build_probe_req_ie(pw_id, &wpa_s->wps->dev,
387 wpa_s->wps->uuid, WPS_REQ_ENROLLEE,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700388 num_req_dev_types, req_dev_types);
389 if (wps_ie == NULL)
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800390 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700391
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700392 switch (type) {
393 case P2P_SCAN_SOCIAL:
Dmitry Shmidt661b4f72014-09-29 14:58:27 -0700394 params->freqs = os_calloc(ARRAY_SIZE(social_channels_freq) + 1,
395 sizeof(int));
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800396 if (params->freqs == NULL)
397 goto fail;
Dmitry Shmidt661b4f72014-09-29 14:58:27 -0700398 for (i = 0; i < ARRAY_SIZE(social_channels_freq); i++) {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800399 if (wpas_p2p_search_social_channel(
400 wpa_s, social_channels_freq[i]))
Dmitry Shmidt661b4f72014-09-29 14:58:27 -0700401 params->freqs[num_channels++] =
402 social_channels_freq[i];
403 }
404 params->freqs[num_channels++] = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700405 break;
406 case P2P_SCAN_FULL:
407 break;
Dmitry Shmidt203eadb2015-03-05 14:16:04 -0800408 case P2P_SCAN_SPECIFIC:
409 params->freqs = os_calloc(2, sizeof(int));
410 if (params->freqs == NULL)
411 goto fail;
412 params->freqs[0] = freq;
413 params->freqs[1] = 0;
414 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700415 case P2P_SCAN_SOCIAL_PLUS_ONE:
Dmitry Shmidt661b4f72014-09-29 14:58:27 -0700416 params->freqs = os_calloc(ARRAY_SIZE(social_channels_freq) + 2,
417 sizeof(int));
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800418 if (params->freqs == NULL)
419 goto fail;
Dmitry Shmidt661b4f72014-09-29 14:58:27 -0700420 for (i = 0; i < ARRAY_SIZE(social_channels_freq); i++) {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800421 if (wpas_p2p_search_social_channel(
422 wpa_s, social_channels_freq[i]))
Dmitry Shmidt661b4f72014-09-29 14:58:27 -0700423 params->freqs[num_channels++] =
424 social_channels_freq[i];
425 }
426 if (p2p_supported_freq(wpa_s->global->p2p, freq))
427 params->freqs[num_channels++] = freq;
428 params->freqs[num_channels++] = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700429 break;
430 }
431
Dmitry Shmidt9c175262016-03-03 10:20:07 -0800432 ielen = p2p_scan_ie_buf_len(wpa_s->global->p2p);
433 ies = wpabuf_alloc(wpabuf_len(wps_ie) + ielen);
434 if (ies == NULL) {
435 wpabuf_free(wps_ie);
436 goto fail;
437 }
438 wpabuf_put_buf(ies, wps_ie);
439 wpabuf_free(wps_ie);
440
441 bands = wpas_get_bands(wpa_s, params->freqs);
442 p2p_scan_ie(wpa_s->global->p2p, ies, dev_id, bands);
443
444 params->p2p_probe = 1;
445 n = os_malloc(wpabuf_len(ies));
446 if (n == NULL) {
447 wpabuf_free(ies);
448 goto fail;
449 }
450 os_memcpy(n, wpabuf_head(ies), wpabuf_len(ies));
451 params->extra_ies = n;
452 params->extra_ies_len = wpabuf_len(ies);
453 wpabuf_free(ies);
454
Dmitry Shmidtbd14a572014-02-18 10:33:49 -0800455 radio_remove_works(wpa_s, "p2p-scan", 0);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800456 if (radio_add_work(wpa_s, 0, "p2p-scan", 0, wpas_p2p_trigger_scan_cb,
457 params) < 0)
458 goto fail;
459 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700460
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800461fail:
462 wpa_scan_free_params(params);
463 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700464}
465
466
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700467static enum wpa_driver_if_type wpas_p2p_if_type(int p2p_group_interface)
468{
469 switch (p2p_group_interface) {
470 case P2P_GROUP_INTERFACE_PENDING:
471 return WPA_IF_P2P_GROUP;
472 case P2P_GROUP_INTERFACE_GO:
473 return WPA_IF_P2P_GO;
474 case P2P_GROUP_INTERFACE_CLIENT:
475 return WPA_IF_P2P_CLIENT;
476 }
477
478 return WPA_IF_P2P_GROUP;
479}
480
481
482static struct wpa_supplicant * wpas_get_p2p_group(struct wpa_supplicant *wpa_s,
483 const u8 *ssid,
484 size_t ssid_len, int *go)
485{
486 struct wpa_ssid *s;
487
488 for (wpa_s = wpa_s->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
489 for (s = wpa_s->conf->ssid; s; s = s->next) {
490 if (s->disabled != 0 || !s->p2p_group ||
491 s->ssid_len != ssid_len ||
492 os_memcmp(ssid, s->ssid, ssid_len) != 0)
493 continue;
494 if (s->mode == WPAS_MODE_P2P_GO &&
495 s != wpa_s->current_ssid)
496 continue;
497 if (go)
498 *go = s->mode == WPAS_MODE_P2P_GO;
499 return wpa_s;
500 }
501 }
502
503 return NULL;
504}
505
506
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800507static void run_wpas_p2p_disconnect(void *eloop_ctx, void *timeout_ctx)
508{
509 struct wpa_supplicant *wpa_s = eloop_ctx;
510 wpa_printf(MSG_DEBUG,
511 "P2P: Complete previously requested removal of %s",
512 wpa_s->ifname);
513 wpas_p2p_disconnect(wpa_s);
514}
515
516
517static int wpas_p2p_disconnect_safely(struct wpa_supplicant *wpa_s,
518 struct wpa_supplicant *calling_wpa_s)
519{
520 if (calling_wpa_s == wpa_s && wpa_s &&
521 wpa_s->p2p_group_interface != NOT_P2P_GROUP_INTERFACE) {
522 /*
523 * The calling wpa_s instance is going to be removed. Do that
524 * from an eloop callback to keep the instance available until
525 * the caller has returned. This my be needed, e.g., to provide
526 * control interface responses on the per-interface socket.
527 */
528 if (eloop_register_timeout(0, 0, run_wpas_p2p_disconnect,
529 wpa_s, NULL) < 0)
530 return -1;
531 return 0;
532 }
533
534 return wpas_p2p_disconnect(wpa_s);
535}
536
537
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800538/* Determine total number of clients in active groups where we are the GO */
539static unsigned int p2p_group_go_member_count(struct wpa_supplicant *wpa_s)
540{
541 unsigned int count = 0;
542 struct wpa_ssid *s;
543
544 for (wpa_s = wpa_s->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
545 for (s = wpa_s->conf->ssid; s; s = s->next) {
546 wpa_printf(MSG_DEBUG,
547 "P2P: sup:%p ssid:%p disabled:%d p2p:%d mode:%d",
548 wpa_s, s, s->disabled, s->p2p_group,
549 s->mode);
550 if (!s->disabled && s->p2p_group &&
551 s->mode == WPAS_MODE_P2P_GO) {
552 count += p2p_get_group_num_members(
553 wpa_s->p2p_group);
554 }
555 }
556 }
557
558 return count;
559}
560
561
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800562static unsigned int p2p_is_active_persistent_group(struct wpa_supplicant *wpa_s)
563{
564 return !wpa_s->p2p_mgmt && wpa_s->current_ssid &&
565 !wpa_s->current_ssid->disabled &&
566 wpa_s->current_ssid->p2p_group &&
567 wpa_s->current_ssid->p2p_persistent_group;
568}
569
570
571static unsigned int p2p_is_active_persistent_go(struct wpa_supplicant *wpa_s)
572{
573 return p2p_is_active_persistent_group(wpa_s) &&
574 wpa_s->current_ssid->mode == WPAS_MODE_P2P_GO;
575}
576
577
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800578/* Find an interface for a P2P group where we are the GO */
579static struct wpa_supplicant *
580wpas_p2p_get_go_group(struct wpa_supplicant *wpa_s)
581{
582 struct wpa_supplicant *save = NULL;
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800583
584 if (!wpa_s)
585 return NULL;
586
587 for (wpa_s = wpa_s->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800588 if (!p2p_is_active_persistent_go(wpa_s))
589 continue;
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800590
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800591 /* Prefer a group with connected clients */
592 if (p2p_get_group_num_members(wpa_s->p2p_group))
593 return wpa_s;
594 save = wpa_s;
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800595 }
596
597 /* No group with connected clients, so pick the one without (if any) */
598 return save;
599}
600
601
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800602static unsigned int p2p_is_active_persistent_cli(struct wpa_supplicant *wpa_s)
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800603{
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800604 return p2p_is_active_persistent_group(wpa_s) &&
605 wpa_s->current_ssid->mode == WPAS_MODE_INFRA;
606}
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800607
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800608
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800609/* Find an interface for a P2P group where we are the P2P Client */
610static struct wpa_supplicant *
611wpas_p2p_get_cli_group(struct wpa_supplicant *wpa_s)
612{
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800613 for (wpa_s = wpa_s->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800614 if (p2p_is_active_persistent_cli(wpa_s))
615 return wpa_s;
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800616 }
617
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800618 return NULL;
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800619}
620
621
622/* Find a persistent group where we are the GO */
623static struct wpa_ssid *
624wpas_p2p_get_persistent_go(struct wpa_supplicant *wpa_s)
625{
626 struct wpa_ssid *s;
627
628 for (s = wpa_s->conf->ssid; s; s = s->next) {
629 if (s->disabled == 2 && s->mode == WPAS_MODE_P2P_GO)
630 return s;
631 }
632
633 return NULL;
634}
635
636
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800637static u8 p2ps_group_capability(void *ctx, u8 incoming, u8 role,
638 unsigned int *force_freq,
639 unsigned int *pref_freq)
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800640{
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800641 struct wpa_supplicant *wpa_s = ctx;
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800642 struct wpa_ssid *s;
643 u8 conncap = P2PS_SETUP_NONE;
644 unsigned int owned_members = 0;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800645 struct wpa_supplicant *go_wpa_s, *cli_wpa_s;
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800646 struct wpa_ssid *persistent_go;
647 int p2p_no_group_iface;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800648 unsigned int pref_freq_list[P2P_MAX_PREF_CHANNELS], size;
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800649
650 wpa_printf(MSG_DEBUG, "P2P: Conncap - in:%d role:%d", incoming, role);
651
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800652 if (force_freq)
653 *force_freq = 0;
654 if (pref_freq)
655 *pref_freq = 0;
656
657 size = P2P_MAX_PREF_CHANNELS;
658 if (force_freq && pref_freq &&
659 !wpas_p2p_setup_freqs(wpa_s, 0, (int *) force_freq,
660 (int *) pref_freq, 0, pref_freq_list, &size))
661 wpas_p2p_set_own_freq_preference(wpa_s,
662 *force_freq ? *force_freq :
663 *pref_freq);
664
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800665 /*
666 * For non-concurrent capable devices:
667 * If persistent_go, then no new.
668 * If GO, then no client.
669 * If client, then no GO.
670 */
671 go_wpa_s = wpas_p2p_get_go_group(wpa_s);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800672 if (go_wpa_s)
673 owned_members = p2p_get_group_num_members(go_wpa_s->p2p_group);
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800674 persistent_go = wpas_p2p_get_persistent_go(wpa_s);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800675 p2p_no_group_iface = !wpas_p2p_create_iface(wpa_s);
676 cli_wpa_s = wpas_p2p_get_cli_group(wpa_s);
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800677
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800678 wpa_printf(MSG_DEBUG,
679 "P2P: GO(iface)=%p members=%u CLI(iface)=%p persistent(ssid)=%p",
680 go_wpa_s, owned_members, cli_wpa_s, persistent_go);
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800681
682 /* If not concurrent, restrict our choices */
683 if (p2p_no_group_iface) {
684 wpa_printf(MSG_DEBUG, "P2P: p2p_no_group_iface");
685
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800686 if (cli_wpa_s)
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800687 return P2PS_SETUP_NONE;
688
689 if (go_wpa_s) {
690 if (role == P2PS_SETUP_CLIENT ||
691 incoming == P2PS_SETUP_GROUP_OWNER ||
692 p2p_client_limit_reached(go_wpa_s->p2p_group))
693 return P2PS_SETUP_NONE;
694
695 return P2PS_SETUP_GROUP_OWNER;
696 }
697
698 if (persistent_go) {
699 if (role == P2PS_SETUP_NONE || role == P2PS_SETUP_NEW) {
700 if (!incoming)
701 return P2PS_SETUP_GROUP_OWNER |
702 P2PS_SETUP_CLIENT;
703 if (incoming == P2PS_SETUP_NEW) {
704 u8 r;
705
706 if (os_get_random(&r, sizeof(r)) < 0 ||
707 (r & 1))
708 return P2PS_SETUP_CLIENT;
709 return P2PS_SETUP_GROUP_OWNER;
710 }
711 }
712 }
713 }
714
715 /* If a required role has been specified, handle it here */
716 if (role && role != P2PS_SETUP_NEW) {
717 switch (incoming) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800718 case P2PS_SETUP_GROUP_OWNER | P2PS_SETUP_NEW:
719 case P2PS_SETUP_GROUP_OWNER | P2PS_SETUP_CLIENT:
720 /*
721 * Peer has an active GO, so if the role allows it and
722 * we do not have any active roles, become client.
723 */
724 if ((role & P2PS_SETUP_CLIENT) && !go_wpa_s &&
725 !cli_wpa_s)
726 return P2PS_SETUP_CLIENT;
727
728 /* fall through */
729
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800730 case P2PS_SETUP_NONE:
731 case P2PS_SETUP_NEW:
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800732 conncap = role;
733 goto grp_owner;
734
735 case P2PS_SETUP_GROUP_OWNER:
736 /*
737 * Must be a complimentary role - cannot be a client to
738 * more than one peer.
739 */
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800740 if (incoming == role || cli_wpa_s)
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800741 return P2PS_SETUP_NONE;
742
743 return P2PS_SETUP_CLIENT;
744
745 case P2PS_SETUP_CLIENT:
746 /* Must be a complimentary role */
747 if (incoming != role) {
748 conncap = P2PS_SETUP_GROUP_OWNER;
749 goto grp_owner;
750 }
Roshan Pius3a1667e2018-07-03 15:17:14 -0700751 /* fall through */
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800752
753 default:
754 return P2PS_SETUP_NONE;
755 }
756 }
757
758 /*
759 * For now, we only will support ownership of one group, and being a
760 * client of one group. Therefore, if we have either an existing GO
761 * group, or an existing client group, we will not do a new GO
762 * negotiation, but rather try to re-use the existing groups.
763 */
764 switch (incoming) {
765 case P2PS_SETUP_NONE:
766 case P2PS_SETUP_NEW:
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800767 if (cli_wpa_s)
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800768 conncap = P2PS_SETUP_GROUP_OWNER;
769 else if (!owned_members)
770 conncap = P2PS_SETUP_NEW;
771 else if (incoming == P2PS_SETUP_NONE)
772 conncap = P2PS_SETUP_GROUP_OWNER | P2PS_SETUP_CLIENT;
773 else
774 conncap = P2PS_SETUP_CLIENT;
775 break;
776
777 case P2PS_SETUP_CLIENT:
778 conncap = P2PS_SETUP_GROUP_OWNER;
779 break;
780
781 case P2PS_SETUP_GROUP_OWNER:
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800782 if (!cli_wpa_s)
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800783 conncap = P2PS_SETUP_CLIENT;
784 break;
785
786 case P2PS_SETUP_GROUP_OWNER | P2PS_SETUP_NEW:
787 case P2PS_SETUP_GROUP_OWNER | P2PS_SETUP_CLIENT:
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800788 if (cli_wpa_s)
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800789 conncap = P2PS_SETUP_GROUP_OWNER;
790 else {
791 u8 r;
792
793 if (os_get_random(&r, sizeof(r)) < 0 ||
794 (r & 1))
795 conncap = P2PS_SETUP_CLIENT;
796 else
797 conncap = P2PS_SETUP_GROUP_OWNER;
798 }
799 break;
800
801 default:
802 return P2PS_SETUP_NONE;
803 }
804
805grp_owner:
806 if ((conncap & P2PS_SETUP_GROUP_OWNER) ||
807 (!incoming && (conncap & P2PS_SETUP_NEW))) {
808 if (go_wpa_s && p2p_client_limit_reached(go_wpa_s->p2p_group))
809 conncap &= ~P2PS_SETUP_GROUP_OWNER;
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800810
811 s = wpas_p2p_get_persistent_go(wpa_s);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800812 if (!s && !go_wpa_s && p2p_no_group_iface) {
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800813 p2p_set_intended_addr(wpa_s->global->p2p,
Dmitry Shmidt9c175262016-03-03 10:20:07 -0800814 wpa_s->p2p_mgmt ?
815 wpa_s->parent->own_addr :
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800816 wpa_s->own_addr);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800817 } else if (!s && !go_wpa_s) {
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800818 if (wpas_p2p_add_group_interface(wpa_s,
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -0800819 WPA_IF_P2P_GROUP) < 0) {
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800820 wpa_printf(MSG_ERROR,
821 "P2P: Failed to allocate a new interface for the group");
822 return P2PS_SETUP_NONE;
823 }
824 wpa_s->global->pending_group_iface_for_p2ps = 1;
825 p2p_set_intended_addr(wpa_s->global->p2p,
826 wpa_s->pending_interface_addr);
827 }
828 }
829
830 return conncap;
831}
832
833
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700834static int wpas_p2p_group_delete(struct wpa_supplicant *wpa_s,
835 enum p2p_group_removal_reason removal_reason)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700836{
837 struct wpa_ssid *ssid;
838 char *gtype;
839 const char *reason;
840
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700841 ssid = wpa_s->current_ssid;
842 if (ssid == NULL) {
843 /*
844 * The current SSID was not known, but there may still be a
Dmitry Shmidtaa532512012-09-24 10:35:31 -0700845 * pending P2P group interface waiting for provisioning or a
846 * P2P group that is trying to reconnect.
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700847 */
848 ssid = wpa_s->conf->ssid;
849 while (ssid) {
Jouni Malinen9d712832012-10-05 11:01:57 -0700850 if (ssid->p2p_group && ssid->disabled != 2)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700851 break;
852 ssid = ssid->next;
853 }
Jouni Malinen5c44edb2012-08-31 21:35:32 +0300854 if (ssid == NULL &&
855 wpa_s->p2p_group_interface == NOT_P2P_GROUP_INTERFACE)
856 {
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700857 wpa_printf(MSG_ERROR, "P2P: P2P group interface "
858 "not found");
859 return -1;
860 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700861 }
862 if (wpa_s->p2p_group_interface == P2P_GROUP_INTERFACE_GO)
863 gtype = "GO";
864 else if (wpa_s->p2p_group_interface == P2P_GROUP_INTERFACE_CLIENT ||
865 (ssid && ssid->mode == WPAS_MODE_INFRA)) {
866 wpa_s->reassociate = 0;
867 wpa_s->disconnected = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700868 gtype = "client";
869 } else
870 gtype = "GO";
Dmitry Shmidt661b4f72014-09-29 14:58:27 -0700871
872 if (removal_reason != P2P_GROUP_REMOVAL_SILENT && ssid)
873 wpas_notify_p2p_group_removed(wpa_s, ssid, gtype);
874
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800875 if (os_strcmp(gtype, "client") == 0) {
Dmitry Shmidt661b4f72014-09-29 14:58:27 -0700876 wpa_supplicant_deauthenticate(wpa_s, WLAN_REASON_DEAUTH_LEAVING);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800877 if (eloop_is_timeout_registered(wpas_p2p_psk_failure_removal,
878 wpa_s, NULL)) {
879 wpa_printf(MSG_DEBUG,
880 "P2P: PSK failure removal was scheduled, so use PSK failure as reason for group removal");
881 removal_reason = P2P_GROUP_REMOVAL_PSK_FAILURE;
882 eloop_cancel_timeout(wpas_p2p_psk_failure_removal,
883 wpa_s, NULL);
884 }
885 }
Dmitry Shmidt661b4f72014-09-29 14:58:27 -0700886
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700887 if (wpa_s->cross_connect_in_use) {
888 wpa_s->cross_connect_in_use = 0;
Dmitry Shmidt9c175262016-03-03 10:20:07 -0800889 wpa_msg_global(wpa_s->p2pdev, MSG_INFO,
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -0700890 P2P_EVENT_CROSS_CONNECT_DISABLE "%s %s",
891 wpa_s->ifname, wpa_s->cross_connect_uplink);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700892 }
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700893 switch (removal_reason) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700894 case P2P_GROUP_REMOVAL_REQUESTED:
895 reason = " reason=REQUESTED";
896 break;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700897 case P2P_GROUP_REMOVAL_FORMATION_FAILED:
898 reason = " reason=FORMATION_FAILED";
899 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700900 case P2P_GROUP_REMOVAL_IDLE_TIMEOUT:
901 reason = " reason=IDLE";
902 break;
903 case P2P_GROUP_REMOVAL_UNAVAILABLE:
904 reason = " reason=UNAVAILABLE";
905 break;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700906 case P2P_GROUP_REMOVAL_GO_ENDING_SESSION:
907 reason = " reason=GO_ENDING_SESSION";
908 break;
Dmitry Shmidt391c59f2013-09-03 12:16:28 -0700909 case P2P_GROUP_REMOVAL_PSK_FAILURE:
910 reason = " reason=PSK_FAILURE";
911 break;
Dmitry Shmidt04f534e2013-12-09 15:50:16 -0800912 case P2P_GROUP_REMOVAL_FREQ_CONFLICT:
913 reason = " reason=FREQ_CONFLICT";
914 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700915 default:
916 reason = "";
917 break;
918 }
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700919 if (removal_reason != P2P_GROUP_REMOVAL_SILENT) {
Dmitry Shmidt9c175262016-03-03 10:20:07 -0800920 wpa_msg_global(wpa_s->p2pdev, MSG_INFO,
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -0700921 P2P_EVENT_GROUP_REMOVED "%s %s%s",
922 wpa_s->ifname, gtype, reason);
Dmitry Shmidt04949592012-07-19 12:16:46 -0700923 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700924
Dmitry Shmidt04f534e2013-12-09 15:50:16 -0800925 if (eloop_cancel_timeout(wpas_p2p_group_freq_conflict, wpa_s, NULL) > 0)
926 wpa_printf(MSG_DEBUG, "P2P: Cancelled P2P group freq_conflict timeout");
Dmitry Shmidt04949592012-07-19 12:16:46 -0700927 if (eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL) > 0)
928 wpa_printf(MSG_DEBUG, "P2P: Cancelled P2P group idle timeout");
Dmitry Shmidtf8623282013-02-20 14:34:59 -0800929 if (eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
Dmitry Shmidt9c175262016-03-03 10:20:07 -0800930 wpa_s->p2pdev, NULL) > 0) {
Dmitry Shmidtf8623282013-02-20 14:34:59 -0800931 wpa_printf(MSG_DEBUG, "P2P: Cancelled P2P group formation "
932 "timeout");
Dmitry Shmidt2f023192013-03-12 12:44:17 -0700933 wpa_s->p2p_in_provisioning = 0;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800934 wpas_p2p_group_formation_failed(wpa_s, 1);
Dmitry Shmidt2f023192013-03-12 12:44:17 -0700935 }
Dmitry Shmidt04949592012-07-19 12:16:46 -0700936
Dmitry Shmidt15907092014-03-25 10:42:57 -0700937 wpa_s->p2p_in_invitation = 0;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800938 eloop_cancel_timeout(wpas_p2p_move_go, wpa_s, NULL);
939 eloop_cancel_timeout(wpas_p2p_reconsider_moving_go, wpa_s, NULL);
Dmitry Shmidt15907092014-03-25 10:42:57 -0700940
Dmitry Shmidt96571392013-10-14 12:54:46 -0700941 /*
942 * Make sure wait for the first client does not remain active after the
943 * group has been removed.
944 */
945 wpa_s->global->p2p_go_wait_client.sec = 0;
946
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700947 if (wpa_s->p2p_group_interface != NOT_P2P_GROUP_INTERFACE) {
948 struct wpa_global *global;
949 char *ifname;
950 enum wpa_driver_if_type type;
951 wpa_printf(MSG_DEBUG, "P2P: Remove group interface %s",
952 wpa_s->ifname);
953 global = wpa_s->global;
954 ifname = os_strdup(wpa_s->ifname);
955 type = wpas_p2p_if_type(wpa_s->p2p_group_interface);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800956 eloop_cancel_timeout(run_wpas_p2p_disconnect, wpa_s, NULL);
Dmitry Shmidte15c7b52011-08-03 15:04:35 -0700957 wpa_supplicant_remove_iface(wpa_s->global, wpa_s, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700958 wpa_s = global->ifaces;
959 if (wpa_s && ifname)
960 wpa_drv_if_remove(wpa_s, type, ifname);
961 os_free(ifname);
Jouni Malinen2b89da82012-08-31 22:04:41 +0300962 return 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700963 }
964
Dmitry Shmidt9c175262016-03-03 10:20:07 -0800965 /*
966 * The primary interface was used for P2P group operations, so
967 * need to reset its p2pdev.
968 */
969 wpa_s->p2pdev = wpa_s->parent;
970
Dmitry Shmidt96571392013-10-14 12:54:46 -0700971 if (!wpa_s->p2p_go_group_formation_completed) {
972 wpa_s->global->p2p_group_formation = NULL;
973 wpa_s->p2p_in_provisioning = 0;
974 }
975
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800976 wpa_s->show_group_started = 0;
977 os_free(wpa_s->go_params);
978 wpa_s->go_params = NULL;
979
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800980 os_free(wpa_s->p2p_group_common_freqs);
981 wpa_s->p2p_group_common_freqs = NULL;
982 wpa_s->p2p_group_common_freqs_num = 0;
Hai Shalom021b0b52019-04-10 11:17:58 -0700983 wpa_s->p2p_go_do_acs = 0;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800984
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800985 wpa_s->waiting_presence_resp = 0;
986
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700987 wpa_printf(MSG_DEBUG, "P2P: Remove temporary group network");
988 if (ssid && (ssid->p2p_group ||
989 ssid->mode == WPAS_MODE_P2P_GROUP_FORMATION ||
990 (ssid->key_mgmt & WPA_KEY_MGMT_WPS))) {
991 int id = ssid->id;
Jouni Malinen75ecf522011-06-27 15:19:46 -0700992 if (ssid == wpa_s->current_ssid) {
993 wpa_sm_set_config(wpa_s->wpa, NULL);
994 eapol_sm_notify_config(wpa_s->eapol, NULL, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700995 wpa_s->current_ssid = NULL;
Jouni Malinen75ecf522011-06-27 15:19:46 -0700996 }
997 /*
998 * Networks objects created during any P2P activities are not
999 * exposed out as they might/will confuse certain non-P2P aware
1000 * applications since these network objects won't behave like
1001 * regular ones.
1002 *
1003 * Likewise, we don't send out network removed signals for such
1004 * network objects.
1005 */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001006 wpa_config_remove_network(wpa_s->conf, id);
1007 wpa_supplicant_clear_status(wpa_s);
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08001008 wpa_supplicant_cancel_sched_scan(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001009 } else {
1010 wpa_printf(MSG_DEBUG, "P2P: Temporary group network not "
1011 "found");
1012 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07001013 if (wpa_s->ap_iface)
1014 wpa_supplicant_ap_deinit(wpa_s);
1015 else
1016 wpa_drv_deinit_p2p_cli(wpa_s);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001017
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001018 os_memset(wpa_s->go_dev_addr, 0, ETH_ALEN);
1019
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001020 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001021}
1022
1023
1024static int wpas_p2p_persistent_group(struct wpa_supplicant *wpa_s,
1025 u8 *go_dev_addr,
1026 const u8 *ssid, size_t ssid_len)
1027{
1028 struct wpa_bss *bss;
1029 const u8 *bssid;
1030 struct wpabuf *p2p;
1031 u8 group_capab;
1032 const u8 *addr;
1033
1034 if (wpa_s->go_params)
1035 bssid = wpa_s->go_params->peer_interface_addr;
1036 else
1037 bssid = wpa_s->bssid;
1038
1039 bss = wpa_bss_get(wpa_s, bssid, ssid, ssid_len);
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001040 if (bss == NULL && wpa_s->go_params &&
1041 !is_zero_ether_addr(wpa_s->go_params->peer_device_addr))
1042 bss = wpa_bss_get_p2p_dev_addr(
1043 wpa_s, wpa_s->go_params->peer_device_addr);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001044 if (bss == NULL) {
1045 u8 iface_addr[ETH_ALEN];
1046 if (p2p_get_interface_addr(wpa_s->global->p2p, bssid,
1047 iface_addr) == 0)
1048 bss = wpa_bss_get(wpa_s, iface_addr, ssid, ssid_len);
1049 }
1050 if (bss == NULL) {
1051 wpa_printf(MSG_DEBUG, "P2P: Could not figure out whether "
1052 "group is persistent - BSS " MACSTR " not found",
1053 MAC2STR(bssid));
1054 return 0;
1055 }
1056
1057 p2p = wpa_bss_get_vendor_ie_multi(bss, P2P_IE_VENDOR_TYPE);
Dmitry Shmidt96571392013-10-14 12:54:46 -07001058 if (p2p == NULL)
1059 p2p = wpa_bss_get_vendor_ie_multi_beacon(bss,
1060 P2P_IE_VENDOR_TYPE);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001061 if (p2p == NULL) {
1062 wpa_printf(MSG_DEBUG, "P2P: Could not figure out whether "
1063 "group is persistent - BSS " MACSTR
1064 " did not include P2P IE", MAC2STR(bssid));
1065 wpa_hexdump(MSG_DEBUG, "P2P: Probe Response IEs",
1066 (u8 *) (bss + 1), bss->ie_len);
1067 wpa_hexdump(MSG_DEBUG, "P2P: Beacon IEs",
1068 ((u8 *) bss + 1) + bss->ie_len,
1069 bss->beacon_ie_len);
1070 return 0;
1071 }
1072
1073 group_capab = p2p_get_group_capab(p2p);
1074 addr = p2p_get_go_dev_addr(p2p);
1075 wpa_printf(MSG_DEBUG, "P2P: Checking whether group is persistent: "
1076 "group_capab=0x%x", group_capab);
1077 if (addr) {
1078 os_memcpy(go_dev_addr, addr, ETH_ALEN);
1079 wpa_printf(MSG_DEBUG, "P2P: GO Device Address " MACSTR,
1080 MAC2STR(addr));
1081 } else
1082 os_memset(go_dev_addr, 0, ETH_ALEN);
1083 wpabuf_free(p2p);
1084
1085 wpa_printf(MSG_DEBUG, "P2P: BSS " MACSTR " group_capab=0x%x "
1086 "go_dev_addr=" MACSTR,
1087 MAC2STR(bssid), group_capab, MAC2STR(go_dev_addr));
1088
Dmitry Shmidt7f2c7532016-08-15 09:48:12 -07001089 return !!(group_capab & P2P_GROUP_CAPAB_PERSISTENT_GROUP);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001090}
1091
1092
Jouni Malinen75ecf522011-06-27 15:19:46 -07001093static int wpas_p2p_store_persistent_group(struct wpa_supplicant *wpa_s,
1094 struct wpa_ssid *ssid,
1095 const u8 *go_dev_addr)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001096{
1097 struct wpa_ssid *s;
1098 int changed = 0;
1099
1100 wpa_printf(MSG_DEBUG, "P2P: Storing credentials for a persistent "
1101 "group (GO Dev Addr " MACSTR ")", MAC2STR(go_dev_addr));
1102 for (s = wpa_s->conf->ssid; s; s = s->next) {
1103 if (s->disabled == 2 &&
1104 os_memcmp(go_dev_addr, s->bssid, ETH_ALEN) == 0 &&
1105 s->ssid_len == ssid->ssid_len &&
1106 os_memcmp(ssid->ssid, s->ssid, ssid->ssid_len) == 0)
1107 break;
1108 }
1109
1110 if (s) {
1111 wpa_printf(MSG_DEBUG, "P2P: Update existing persistent group "
1112 "entry");
1113 if (ssid->passphrase && !s->passphrase)
1114 changed = 1;
1115 else if (ssid->passphrase && s->passphrase &&
1116 os_strcmp(ssid->passphrase, s->passphrase) != 0)
1117 changed = 1;
1118 } else {
1119 wpa_printf(MSG_DEBUG, "P2P: Create a new persistent group "
1120 "entry");
1121 changed = 1;
1122 s = wpa_config_add_network(wpa_s->conf);
1123 if (s == NULL)
Jouni Malinen75ecf522011-06-27 15:19:46 -07001124 return -1;
1125
1126 /*
1127 * Instead of network_added we emit persistent_group_added
1128 * notification. Also to keep the defense checks in
1129 * persistent_group obj registration method, we set the
1130 * relevant flags in s to designate it as a persistent group.
1131 */
1132 s->p2p_group = 1;
1133 s->p2p_persistent_group = 1;
1134 wpas_notify_persistent_group_added(wpa_s, s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001135 wpa_config_set_network_defaults(s);
1136 }
1137
1138 s->p2p_group = 1;
1139 s->p2p_persistent_group = 1;
1140 s->disabled = 2;
1141 s->bssid_set = 1;
1142 os_memcpy(s->bssid, go_dev_addr, ETH_ALEN);
1143 s->mode = ssid->mode;
1144 s->auth_alg = WPA_AUTH_ALG_OPEN;
1145 s->key_mgmt = WPA_KEY_MGMT_PSK;
1146 s->proto = WPA_PROTO_RSN;
Dmitry Shmidte4663042016-04-04 10:07:49 -07001147 s->pbss = ssid->pbss;
1148 s->pairwise_cipher = ssid->pbss ? WPA_CIPHER_GCMP : WPA_CIPHER_CCMP;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001149 s->export_keys = 1;
1150 if (ssid->passphrase) {
1151 os_free(s->passphrase);
1152 s->passphrase = os_strdup(ssid->passphrase);
1153 }
1154 if (ssid->psk_set) {
1155 s->psk_set = 1;
1156 os_memcpy(s->psk, ssid->psk, 32);
1157 }
1158 if (s->passphrase && !s->psk_set)
1159 wpa_config_update_psk(s);
1160 if (s->ssid == NULL || s->ssid_len < ssid->ssid_len) {
1161 os_free(s->ssid);
1162 s->ssid = os_malloc(ssid->ssid_len);
1163 }
1164 if (s->ssid) {
1165 s->ssid_len = ssid->ssid_len;
1166 os_memcpy(s->ssid, ssid->ssid, s->ssid_len);
1167 }
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07001168 if (ssid->mode == WPAS_MODE_P2P_GO && wpa_s->global->add_psk) {
1169 dl_list_add(&s->psk_list, &wpa_s->global->add_psk->list);
1170 wpa_s->global->add_psk = NULL;
1171 changed = 1;
1172 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001173
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001174 if (changed && wpa_s->conf->update_config &&
1175 wpa_config_write(wpa_s->confname, wpa_s->conf)) {
1176 wpa_printf(MSG_DEBUG, "P2P: Failed to update configuration");
1177 }
Jouni Malinen75ecf522011-06-27 15:19:46 -07001178
1179 return s->id;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001180}
1181
1182
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001183static void wpas_p2p_add_persistent_group_client(struct wpa_supplicant *wpa_s,
1184 const u8 *addr)
1185{
1186 struct wpa_ssid *ssid, *s;
1187 u8 *n;
1188 size_t i;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001189 int found = 0;
Dmitry Shmidt8bd70b72015-05-26 16:02:19 -07001190 struct wpa_supplicant *p2p_wpa_s = wpa_s->global->p2p_init_wpa_s;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001191
1192 ssid = wpa_s->current_ssid;
1193 if (ssid == NULL || ssid->mode != WPAS_MODE_P2P_GO ||
1194 !ssid->p2p_persistent_group)
1195 return;
1196
Dmitry Shmidt8bd70b72015-05-26 16:02:19 -07001197 for (s = p2p_wpa_s->conf->ssid; s; s = s->next) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001198 if (s->disabled != 2 || s->mode != WPAS_MODE_P2P_GO)
1199 continue;
1200
1201 if (s->ssid_len == ssid->ssid_len &&
1202 os_memcmp(s->ssid, ssid->ssid, s->ssid_len) == 0)
1203 break;
1204 }
1205
1206 if (s == NULL)
1207 return;
1208
1209 for (i = 0; s->p2p_client_list && i < s->num_p2p_clients; i++) {
Dmitry Shmidtff787d52015-01-12 13:01:47 -08001210 if (os_memcmp(s->p2p_client_list + i * 2 * ETH_ALEN, addr,
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001211 ETH_ALEN) != 0)
1212 continue;
1213
1214 if (i == s->num_p2p_clients - 1)
1215 return; /* already the most recent entry */
1216
1217 /* move the entry to mark it most recent */
Dmitry Shmidtff787d52015-01-12 13:01:47 -08001218 os_memmove(s->p2p_client_list + i * 2 * ETH_ALEN,
1219 s->p2p_client_list + (i + 1) * 2 * ETH_ALEN,
1220 (s->num_p2p_clients - i - 1) * 2 * ETH_ALEN);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001221 os_memcpy(s->p2p_client_list +
Dmitry Shmidtff787d52015-01-12 13:01:47 -08001222 (s->num_p2p_clients - 1) * 2 * ETH_ALEN, addr,
1223 ETH_ALEN);
1224 os_memset(s->p2p_client_list +
1225 (s->num_p2p_clients - 1) * 2 * ETH_ALEN + ETH_ALEN,
1226 0xff, ETH_ALEN);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001227 found = 1;
1228 break;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001229 }
1230
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001231 if (!found && s->num_p2p_clients < P2P_MAX_STORED_CLIENTS) {
1232 n = os_realloc_array(s->p2p_client_list,
Dmitry Shmidtff787d52015-01-12 13:01:47 -08001233 s->num_p2p_clients + 1, 2 * ETH_ALEN);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001234 if (n == NULL)
1235 return;
Dmitry Shmidtff787d52015-01-12 13:01:47 -08001236 os_memcpy(n + s->num_p2p_clients * 2 * ETH_ALEN, addr,
1237 ETH_ALEN);
1238 os_memset(n + s->num_p2p_clients * 2 * ETH_ALEN + ETH_ALEN,
1239 0xff, ETH_ALEN);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001240 s->p2p_client_list = n;
1241 s->num_p2p_clients++;
Dmitry Shmidt6aa8ae42014-07-07 09:31:33 -07001242 } else if (!found && s->p2p_client_list) {
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001243 /* Not enough room for an additional entry - drop the oldest
1244 * entry */
1245 os_memmove(s->p2p_client_list,
Dmitry Shmidtff787d52015-01-12 13:01:47 -08001246 s->p2p_client_list + 2 * ETH_ALEN,
1247 (s->num_p2p_clients - 1) * 2 * ETH_ALEN);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001248 os_memcpy(s->p2p_client_list +
Dmitry Shmidtff787d52015-01-12 13:01:47 -08001249 (s->num_p2p_clients - 1) * 2 * ETH_ALEN,
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001250 addr, ETH_ALEN);
Dmitry Shmidtff787d52015-01-12 13:01:47 -08001251 os_memset(s->p2p_client_list +
1252 (s->num_p2p_clients - 1) * 2 * ETH_ALEN + ETH_ALEN,
1253 0xff, ETH_ALEN);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001254 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001255
Dmitry Shmidt8bd70b72015-05-26 16:02:19 -07001256 if (p2p_wpa_s->conf->update_config &&
1257 wpa_config_write(p2p_wpa_s->confname, p2p_wpa_s->conf))
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001258 wpa_printf(MSG_DEBUG, "P2P: Failed to update configuration");
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001259}
1260
1261
Dmitry Shmidt7f0b69e2014-07-28 10:35:20 -07001262static void wpas_p2p_group_started(struct wpa_supplicant *wpa_s,
1263 int go, struct wpa_ssid *ssid, int freq,
1264 const u8 *psk, const char *passphrase,
1265 const u8 *go_dev_addr, int persistent,
1266 const char *extra)
1267{
1268 const char *ssid_txt;
1269 char psk_txt[65];
1270
1271 if (psk)
1272 wpa_snprintf_hex(psk_txt, sizeof(psk_txt), psk, 32);
1273 else
1274 psk_txt[0] = '\0';
1275
1276 if (ssid)
1277 ssid_txt = wpa_ssid_txt(ssid->ssid, ssid->ssid_len);
1278 else
1279 ssid_txt = "";
1280
1281 if (passphrase && passphrase[0] == '\0')
1282 passphrase = NULL;
1283
1284 /*
1285 * Include PSK/passphrase only in the control interface message and
1286 * leave it out from the debug log entry.
1287 */
Dmitry Shmidt9c175262016-03-03 10:20:07 -08001288 wpa_msg_global_ctrl(wpa_s->p2pdev, MSG_INFO,
Dmitry Shmidt7f0b69e2014-07-28 10:35:20 -07001289 P2P_EVENT_GROUP_STARTED
1290 "%s %s ssid=\"%s\" freq=%d%s%s%s%s%s go_dev_addr="
1291 MACSTR "%s%s",
1292 wpa_s->ifname, go ? "GO" : "client", ssid_txt, freq,
1293 psk ? " psk=" : "", psk_txt,
1294 passphrase ? " passphrase=\"" : "",
1295 passphrase ? passphrase : "",
1296 passphrase ? "\"" : "",
1297 MAC2STR(go_dev_addr),
1298 persistent ? " [PERSISTENT]" : "", extra);
1299 wpa_printf(MSG_INFO, P2P_EVENT_GROUP_STARTED
1300 "%s %s ssid=\"%s\" freq=%d go_dev_addr=" MACSTR "%s%s",
1301 wpa_s->ifname, go ? "GO" : "client", ssid_txt, freq,
1302 MAC2STR(go_dev_addr), persistent ? " [PERSISTENT]" : "",
1303 extra);
1304}
1305
1306
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001307static void wpas_group_formation_completed(struct wpa_supplicant *wpa_s,
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07001308 int success, int already_deleted)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001309{
1310 struct wpa_ssid *ssid;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001311 int client;
1312 int persistent;
1313 u8 go_dev_addr[ETH_ALEN];
1314
1315 /*
1316 * This callback is likely called for the main interface. Update wpa_s
1317 * to use the group interface if a new interface was created for the
1318 * group.
1319 */
1320 if (wpa_s->global->p2p_group_formation)
1321 wpa_s = wpa_s->global->p2p_group_formation;
Dmitry Shmidt56052862013-10-04 10:23:25 -07001322 if (wpa_s->p2p_go_group_formation_completed) {
1323 wpa_s->global->p2p_group_formation = NULL;
1324 wpa_s->p2p_in_provisioning = 0;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001325 } else if (wpa_s->p2p_in_provisioning && !success) {
1326 wpa_msg(wpa_s, MSG_DEBUG,
1327 "P2P: Stop provisioning state due to failure");
1328 wpa_s->p2p_in_provisioning = 0;
Dmitry Shmidt56052862013-10-04 10:23:25 -07001329 }
Dmitry Shmidt21de2142014-04-08 10:50:52 -07001330 wpa_s->p2p_in_invitation = 0;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001331 wpa_s->group_formation_reported = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001332
1333 if (!success) {
Dmitry Shmidt9c175262016-03-03 10:20:07 -08001334 wpa_msg_global(wpa_s->p2pdev, MSG_INFO,
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07001335 P2P_EVENT_GROUP_FORMATION_FAILURE);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001336 wpas_notify_p2p_group_formation_failure(wpa_s, "");
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07001337 if (already_deleted)
1338 return;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001339 wpas_p2p_group_delete(wpa_s,
1340 P2P_GROUP_REMOVAL_FORMATION_FAILED);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001341 return;
1342 }
1343
Dmitry Shmidt9c175262016-03-03 10:20:07 -08001344 wpa_msg_global(wpa_s->p2pdev, MSG_INFO,
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07001345 P2P_EVENT_GROUP_FORMATION_SUCCESS);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001346
1347 ssid = wpa_s->current_ssid;
1348 if (ssid && ssid->mode == WPAS_MODE_P2P_GROUP_FORMATION) {
1349 ssid->mode = WPAS_MODE_P2P_GO;
1350 p2p_group_notif_formation_done(wpa_s->p2p_group);
1351 wpa_supplicant_ap_mac_addr_filter(wpa_s, NULL);
1352 }
1353
1354 persistent = 0;
1355 if (ssid) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001356 client = ssid->mode == WPAS_MODE_INFRA;
1357 if (ssid->mode == WPAS_MODE_P2P_GO) {
1358 persistent = ssid->p2p_persistent_group;
Dmitry Shmidt497c1d52011-07-21 15:19:46 -07001359 os_memcpy(go_dev_addr, wpa_s->global->p2p_dev_addr,
1360 ETH_ALEN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001361 } else
1362 persistent = wpas_p2p_persistent_group(wpa_s,
1363 go_dev_addr,
1364 ssid->ssid,
1365 ssid->ssid_len);
1366 } else {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001367 client = wpa_s->p2p_group_interface ==
1368 P2P_GROUP_INTERFACE_CLIENT;
1369 os_memset(go_dev_addr, 0, ETH_ALEN);
1370 }
1371
1372 wpa_s->show_group_started = 0;
1373 if (client) {
1374 /*
1375 * Indicate event only after successfully completed 4-way
1376 * handshake, i.e., when the interface is ready for data
1377 * packets.
1378 */
1379 wpa_s->show_group_started = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001380 } else {
Dmitry Shmidt7f0b69e2014-07-28 10:35:20 -07001381 wpas_p2p_group_started(wpa_s, 1, ssid,
1382 ssid ? ssid->frequency : 0,
1383 ssid && ssid->passphrase == NULL &&
1384 ssid->psk_set ? ssid->psk : NULL,
1385 ssid ? ssid->passphrase : NULL,
1386 go_dev_addr, persistent, "");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001387 wpas_p2p_cross_connect_setup(wpa_s);
1388 wpas_p2p_set_group_idle_timeout(wpa_s);
1389 }
1390
1391 if (persistent)
Dmitry Shmidtd5ab1b52016-06-21 12:38:41 -07001392 wpas_p2p_store_persistent_group(wpa_s->p2pdev,
1393 ssid, go_dev_addr);
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07001394 else {
1395 os_free(wpa_s->global->add_psk);
1396 wpa_s->global->add_psk = NULL;
1397 }
Dmitry Shmidtd5ab1b52016-06-21 12:38:41 -07001398
Dmitry Shmidt92c368d2013-08-29 12:37:21 -07001399 if (!client) {
Dmitry Shmidtabb90a32016-12-05 15:34:39 -08001400 wpas_notify_p2p_group_started(wpa_s, ssid, persistent, 0, NULL);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001401 os_get_reltime(&wpa_s->global->p2p_go_wait_client);
Dmitry Shmidt92c368d2013-08-29 12:37:21 -07001402 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001403}
1404
1405
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001406struct send_action_work {
1407 unsigned int freq;
1408 u8 dst[ETH_ALEN];
1409 u8 src[ETH_ALEN];
1410 u8 bssid[ETH_ALEN];
1411 size_t len;
1412 unsigned int wait_time;
1413 u8 buf[0];
1414};
1415
1416
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001417static void wpas_p2p_free_send_action_work(struct wpa_supplicant *wpa_s)
1418{
1419 struct send_action_work *awork = wpa_s->p2p_send_action_work->ctx;
1420
1421 wpa_printf(MSG_DEBUG,
1422 "P2P: Free Action frame radio work @%p (freq=%u dst="
1423 MACSTR " src=" MACSTR " bssid=" MACSTR " wait_time=%u)",
1424 wpa_s->p2p_send_action_work, awork->freq,
1425 MAC2STR(awork->dst), MAC2STR(awork->src),
1426 MAC2STR(awork->bssid), awork->wait_time);
1427 wpa_hexdump(MSG_DEBUG, "P2P: Freeing pending Action frame",
1428 awork->buf, awork->len);
1429 os_free(awork);
1430 wpa_s->p2p_send_action_work->ctx = NULL;
1431 radio_work_done(wpa_s->p2p_send_action_work);
1432 wpa_s->p2p_send_action_work = NULL;
1433}
1434
1435
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001436static void wpas_p2p_send_action_work_timeout(void *eloop_ctx,
1437 void *timeout_ctx)
1438{
1439 struct wpa_supplicant *wpa_s = eloop_ctx;
1440
1441 if (!wpa_s->p2p_send_action_work)
1442 return;
1443
1444 wpa_printf(MSG_DEBUG, "P2P: Send Action frame radio work timed out");
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001445 wpas_p2p_free_send_action_work(wpa_s);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001446}
1447
1448
Dmitry Shmidt03658832014-08-13 11:03:49 -07001449static void wpas_p2p_action_tx_clear(struct wpa_supplicant *wpa_s)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001450{
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001451 if (wpa_s->p2p_send_action_work) {
1452 struct send_action_work *awork;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001453
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001454 awork = wpa_s->p2p_send_action_work->ctx;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001455 wpa_printf(MSG_DEBUG,
1456 "P2P: Clear Action TX work @%p (wait_time=%u)",
1457 wpa_s->p2p_send_action_work, awork->wait_time);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001458 if (awork->wait_time == 0) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001459 wpas_p2p_free_send_action_work(wpa_s);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001460 } else {
1461 /*
1462 * In theory, this should not be needed, but number of
1463 * places in the P2P code is still using non-zero wait
1464 * time for the last Action frame in the sequence and
1465 * some of these do not call send_action_done().
1466 */
1467 eloop_cancel_timeout(wpas_p2p_send_action_work_timeout,
1468 wpa_s, NULL);
1469 eloop_register_timeout(
1470 0, awork->wait_time * 1000,
1471 wpas_p2p_send_action_work_timeout,
1472 wpa_s, NULL);
1473 }
1474 }
Dmitry Shmidt03658832014-08-13 11:03:49 -07001475}
1476
1477
1478static void wpas_p2p_send_action_tx_status(struct wpa_supplicant *wpa_s,
1479 unsigned int freq,
1480 const u8 *dst, const u8 *src,
1481 const u8 *bssid,
1482 const u8 *data, size_t data_len,
1483 enum offchannel_send_action_result
1484 result)
1485{
1486 enum p2p_send_action_result res = P2P_SEND_ACTION_SUCCESS;
1487
1488 wpas_p2p_action_tx_clear(wpa_s);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001489
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001490 if (wpa_s->global->p2p == NULL || wpa_s->global->p2p_disabled)
1491 return;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001492
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001493 switch (result) {
1494 case OFFCHANNEL_SEND_ACTION_SUCCESS:
1495 res = P2P_SEND_ACTION_SUCCESS;
1496 break;
1497 case OFFCHANNEL_SEND_ACTION_NO_ACK:
1498 res = P2P_SEND_ACTION_NO_ACK;
1499 break;
1500 case OFFCHANNEL_SEND_ACTION_FAILED:
1501 res = P2P_SEND_ACTION_FAILED;
1502 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001503 }
1504
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001505 p2p_send_action_cb(wpa_s->global->p2p, freq, dst, src, bssid, res);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001506
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08001507 if (result != OFFCHANNEL_SEND_ACTION_SUCCESS &&
1508 wpa_s->pending_pd_before_join &&
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001509 (os_memcmp(dst, wpa_s->pending_join_dev_addr, ETH_ALEN) == 0 ||
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001510 os_memcmp(dst, wpa_s->pending_join_iface_addr, ETH_ALEN) == 0) &&
1511 wpa_s->p2p_fallback_to_go_neg) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001512 wpa_s->pending_pd_before_join = 0;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001513 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: No ACK for PD Req "
1514 "during p2p_connect-auto");
Dmitry Shmidt9c175262016-03-03 10:20:07 -08001515 wpa_msg_global(wpa_s->p2pdev, MSG_INFO,
Dmitry Shmidt7f656022015-02-25 14:36:37 -08001516 P2P_EVENT_FALLBACK_TO_GO_NEG
1517 "reason=no-ACK-to-PD-Req");
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001518 wpas_p2p_fallback_to_go_neg(wpa_s, 0);
1519 return;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001520 }
1521}
1522
1523
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001524static void wpas_send_action_cb(struct wpa_radio_work *work, int deinit)
1525{
1526 struct wpa_supplicant *wpa_s = work->wpa_s;
1527 struct send_action_work *awork = work->ctx;
1528
1529 if (deinit) {
Dmitry Shmidtbd14a572014-02-18 10:33:49 -08001530 if (work->started) {
1531 eloop_cancel_timeout(wpas_p2p_send_action_work_timeout,
1532 wpa_s, NULL);
1533 wpa_s->p2p_send_action_work = NULL;
1534 offchannel_send_action_done(wpa_s);
1535 }
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001536 os_free(awork);
1537 return;
1538 }
1539
1540 if (offchannel_send_action(wpa_s, awork->freq, awork->dst, awork->src,
1541 awork->bssid, awork->buf, awork->len,
1542 awork->wait_time,
1543 wpas_p2p_send_action_tx_status, 1) < 0) {
1544 os_free(awork);
1545 radio_work_done(work);
1546 return;
1547 }
1548 wpa_s->p2p_send_action_work = work;
1549}
1550
1551
1552static int wpas_send_action_work(struct wpa_supplicant *wpa_s,
1553 unsigned int freq, const u8 *dst,
1554 const u8 *src, const u8 *bssid, const u8 *buf,
1555 size_t len, unsigned int wait_time)
1556{
1557 struct send_action_work *awork;
1558
Hai Shalom81f62d82019-07-22 12:10:00 -07001559 if (radio_work_pending(wpa_s, "p2p-send-action")) {
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001560 wpa_printf(MSG_DEBUG, "P2P: Cannot schedule new p2p-send-action work since one is already pending");
1561 return -1;
1562 }
1563
1564 awork = os_zalloc(sizeof(*awork) + len);
1565 if (awork == NULL)
1566 return -1;
1567
1568 awork->freq = freq;
1569 os_memcpy(awork->dst, dst, ETH_ALEN);
1570 os_memcpy(awork->src, src, ETH_ALEN);
1571 os_memcpy(awork->bssid, bssid, ETH_ALEN);
1572 awork->len = len;
1573 awork->wait_time = wait_time;
1574 os_memcpy(awork->buf, buf, len);
1575
Hai Shalom81f62d82019-07-22 12:10:00 -07001576 if (radio_add_work(wpa_s, freq, "p2p-send-action", 1,
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001577 wpas_send_action_cb, awork) < 0) {
1578 os_free(awork);
1579 return -1;
1580 }
1581
1582 return 0;
1583}
1584
1585
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001586static int wpas_send_action(void *ctx, unsigned int freq, const u8 *dst,
1587 const u8 *src, const u8 *bssid, const u8 *buf,
Hai Shalom021b0b52019-04-10 11:17:58 -07001588 size_t len, unsigned int wait_time, int *scheduled)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001589{
1590 struct wpa_supplicant *wpa_s = ctx;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001591 int listen_freq = -1, send_freq = -1;
1592
Hai Shalom021b0b52019-04-10 11:17:58 -07001593 if (scheduled)
1594 *scheduled = 0;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001595 if (wpa_s->p2p_listen_work)
1596 listen_freq = wpa_s->p2p_listen_work->freq;
1597 if (wpa_s->p2p_send_action_work)
1598 send_freq = wpa_s->p2p_send_action_work->freq;
1599 if (listen_freq != (int) freq && send_freq != (int) freq) {
Hai Shalom021b0b52019-04-10 11:17:58 -07001600 int res;
1601
1602 wpa_printf(MSG_DEBUG, "P2P: Schedule new radio work for Action frame TX (listen_freq=%d send_freq=%d freq=%u)",
1603 listen_freq, send_freq, freq);
1604 res = wpas_send_action_work(wpa_s, freq, dst, src, bssid, buf,
1605 len, wait_time);
1606 if (res == 0 && scheduled)
1607 *scheduled = 1;
1608 return res;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001609 }
1610
1611 wpa_printf(MSG_DEBUG, "P2P: Use ongoing radio work for Action frame TX");
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001612 return offchannel_send_action(wpa_s, freq, dst, src, bssid, buf, len,
1613 wait_time,
1614 wpas_p2p_send_action_tx_status, 1);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001615}
1616
1617
1618static void wpas_send_action_done(void *ctx)
1619{
1620 struct wpa_supplicant *wpa_s = ctx;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001621
1622 if (wpa_s->p2p_send_action_work) {
1623 eloop_cancel_timeout(wpas_p2p_send_action_work_timeout,
1624 wpa_s, NULL);
1625 os_free(wpa_s->p2p_send_action_work->ctx);
1626 radio_work_done(wpa_s->p2p_send_action_work);
1627 wpa_s->p2p_send_action_work = NULL;
1628 }
1629
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001630 offchannel_send_action_done(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001631}
1632
1633
1634static int wpas_copy_go_neg_results(struct wpa_supplicant *wpa_s,
1635 struct p2p_go_neg_results *params)
1636{
1637 if (wpa_s->go_params == NULL) {
1638 wpa_s->go_params = os_malloc(sizeof(*params));
1639 if (wpa_s->go_params == NULL)
1640 return -1;
1641 }
1642 os_memcpy(wpa_s->go_params, params, sizeof(*params));
1643 return 0;
1644}
1645
1646
1647static void wpas_start_wps_enrollee(struct wpa_supplicant *wpa_s,
1648 struct p2p_go_neg_results *res)
1649{
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001650 wpa_s->group_formation_reported = 0;
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001651 wpa_printf(MSG_DEBUG, "P2P: Start WPS Enrollee for peer " MACSTR
1652 " dev_addr " MACSTR " wps_method %d",
1653 MAC2STR(res->peer_interface_addr),
1654 MAC2STR(res->peer_device_addr), res->wps_method);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001655 wpa_hexdump_ascii(MSG_DEBUG, "P2P: Start WPS Enrollee for SSID",
1656 res->ssid, res->ssid_len);
1657 wpa_supplicant_ap_deinit(wpa_s);
1658 wpas_copy_go_neg_results(wpa_s, res);
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001659 if (res->wps_method == WPS_PBC) {
Hai Shalom021b0b52019-04-10 11:17:58 -07001660 wpas_wps_start_pbc(wpa_s, res->peer_interface_addr, 1, 0);
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001661#ifdef CONFIG_WPS_NFC
1662 } else if (res->wps_method == WPS_NFC) {
1663 wpas_wps_start_nfc(wpa_s, res->peer_device_addr,
1664 res->peer_interface_addr,
Dmitry Shmidt9c175262016-03-03 10:20:07 -08001665 wpa_s->p2pdev->p2p_oob_dev_pw,
1666 wpa_s->p2pdev->p2p_oob_dev_pw_id, 1,
1667 wpa_s->p2pdev->p2p_oob_dev_pw_id ==
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001668 DEV_PW_NFC_CONNECTION_HANDOVER ?
Dmitry Shmidt9c175262016-03-03 10:20:07 -08001669 wpa_s->p2pdev->p2p_peer_oob_pubkey_hash :
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001670 NULL,
1671 NULL, 0, 0);
1672#endif /* CONFIG_WPS_NFC */
1673 } else {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001674 u16 dev_pw_id = DEV_PW_DEFAULT;
Dmitry Shmidt216983b2015-02-06 10:50:36 -08001675 if (wpa_s->p2p_wps_method == WPS_P2PS)
1676 dev_pw_id = DEV_PW_P2PS_DEFAULT;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001677 if (wpa_s->p2p_wps_method == WPS_PIN_KEYPAD)
1678 dev_pw_id = DEV_PW_REGISTRAR_SPECIFIED;
1679 wpas_wps_start_pin(wpa_s, res->peer_interface_addr,
1680 wpa_s->p2p_pin, 1, dev_pw_id);
1681 }
1682}
1683
1684
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07001685static void wpas_p2p_add_psk_list(struct wpa_supplicant *wpa_s,
1686 struct wpa_ssid *ssid)
1687{
1688 struct wpa_ssid *persistent;
1689 struct psk_list_entry *psk;
1690 struct hostapd_data *hapd;
1691
1692 if (!wpa_s->ap_iface)
1693 return;
1694
Dmitry Shmidt9c175262016-03-03 10:20:07 -08001695 persistent = wpas_p2p_get_persistent(wpa_s->p2pdev, NULL, ssid->ssid,
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07001696 ssid->ssid_len);
1697 if (persistent == NULL)
1698 return;
1699
1700 hapd = wpa_s->ap_iface->bss[0];
1701
1702 dl_list_for_each(psk, &persistent->psk_list, struct psk_list_entry,
1703 list) {
1704 struct hostapd_wpa_psk *hpsk;
1705
1706 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Add persistent group PSK entry for "
1707 MACSTR " psk=%d",
1708 MAC2STR(psk->addr), psk->p2p);
1709 hpsk = os_zalloc(sizeof(*hpsk));
1710 if (hpsk == NULL)
1711 break;
1712 os_memcpy(hpsk->psk, psk->psk, PMK_LEN);
1713 if (psk->p2p)
1714 os_memcpy(hpsk->p2p_dev_addr, psk->addr, ETH_ALEN);
1715 else
1716 os_memcpy(hpsk->addr, psk->addr, ETH_ALEN);
1717 hpsk->next = hapd->conf->ssid.wpa_psk;
1718 hapd->conf->ssid.wpa_psk = hpsk;
1719 }
1720}
1721
1722
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001723static void p2p_go_dump_common_freqs(struct wpa_supplicant *wpa_s)
1724{
Roshan Pius3a1667e2018-07-03 15:17:14 -07001725 char buf[20 + P2P_MAX_CHANNELS * 6];
1726 char *pos, *end;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001727 unsigned int i;
Roshan Pius3a1667e2018-07-03 15:17:14 -07001728 int res;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001729
Roshan Pius3a1667e2018-07-03 15:17:14 -07001730 pos = buf;
1731 end = pos + sizeof(buf);
1732 for (i = 0; i < wpa_s->p2p_group_common_freqs_num; i++) {
1733 res = os_snprintf(pos, end - pos, " %d",
1734 wpa_s->p2p_group_common_freqs[i]);
1735 if (os_snprintf_error(end - pos, res))
1736 break;
1737 pos += res;
1738 }
1739 *pos = '\0';
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001740
Roshan Pius3a1667e2018-07-03 15:17:14 -07001741 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Common group frequencies:%s", buf);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001742}
1743
1744
1745static void p2p_go_save_group_common_freqs(struct wpa_supplicant *wpa_s,
1746 struct p2p_go_neg_results *params)
1747{
1748 unsigned int i, len = int_array_len(wpa_s->go_params->freq_list);
1749
1750 wpa_s->p2p_group_common_freqs_num = 0;
1751 os_free(wpa_s->p2p_group_common_freqs);
1752 wpa_s->p2p_group_common_freqs = os_calloc(len, sizeof(int));
1753 if (!wpa_s->p2p_group_common_freqs)
1754 return;
1755
1756 for (i = 0; i < len; i++) {
1757 if (!wpa_s->go_params->freq_list[i])
1758 break;
1759 wpa_s->p2p_group_common_freqs[i] =
1760 wpa_s->go_params->freq_list[i];
1761 }
1762 wpa_s->p2p_group_common_freqs_num = i;
1763}
1764
1765
Dmitry Shmidt216983b2015-02-06 10:50:36 -08001766static void p2p_config_write(struct wpa_supplicant *wpa_s)
1767{
1768#ifndef CONFIG_NO_CONFIG_WRITE
Dmitry Shmidt9c175262016-03-03 10:20:07 -08001769 if (wpa_s->p2pdev->conf->update_config &&
1770 wpa_config_write(wpa_s->p2pdev->confname, wpa_s->p2pdev->conf))
Dmitry Shmidt216983b2015-02-06 10:50:36 -08001771 wpa_printf(MSG_DEBUG, "P2P: Failed to update configuration");
1772#endif /* CONFIG_NO_CONFIG_WRITE */
1773}
1774
1775
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001776static void p2p_go_configured(void *ctx, void *data)
1777{
1778 struct wpa_supplicant *wpa_s = ctx;
1779 struct p2p_go_neg_results *params = data;
1780 struct wpa_ssid *ssid;
1781
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08001782 wpa_s->ap_configured_cb = NULL;
1783 wpa_s->ap_configured_cb_ctx = NULL;
1784 wpa_s->ap_configured_cb_data = NULL;
1785 if (!wpa_s->go_params) {
1786 wpa_printf(MSG_ERROR,
1787 "P2P: p2p_go_configured() called with wpa_s->go_params == NULL");
1788 return;
1789 }
1790
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001791 p2p_go_save_group_common_freqs(wpa_s, params);
1792 p2p_go_dump_common_freqs(wpa_s);
1793
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001794 ssid = wpa_s->current_ssid;
1795 if (ssid && ssid->mode == WPAS_MODE_P2P_GO) {
1796 wpa_printf(MSG_DEBUG, "P2P: Group setup without provisioning");
1797 if (wpa_s->global->p2p_group_formation == wpa_s)
1798 wpa_s->global->p2p_group_formation = NULL;
Dmitry Shmidt7f0b69e2014-07-28 10:35:20 -07001799 wpas_p2p_group_started(wpa_s, 1, ssid, ssid->frequency,
1800 params->passphrase[0] == '\0' ?
1801 params->psk : NULL,
1802 params->passphrase,
1803 wpa_s->global->p2p_dev_addr,
1804 params->persistent_group, "");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001805 wpa_s->group_formation_reported = 1;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001806
Dmitry Shmidt9c175262016-03-03 10:20:07 -08001807 if (wpa_s->p2pdev->p2ps_method_config_any) {
1808 if (is_zero_ether_addr(wpa_s->p2pdev->p2ps_join_addr)) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001809 wpa_dbg(wpa_s, MSG_DEBUG,
1810 "P2PS: Setting default PIN for ANY");
1811 wpa_supplicant_ap_wps_pin(wpa_s, NULL,
1812 "12345670", NULL, 0,
1813 0);
1814 } else {
1815 wpa_dbg(wpa_s, MSG_DEBUG,
1816 "P2PS: Setting default PIN for " MACSTR,
Dmitry Shmidt9c175262016-03-03 10:20:07 -08001817 MAC2STR(wpa_s->p2pdev->p2ps_join_addr));
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001818 wpa_supplicant_ap_wps_pin(
Dmitry Shmidt9c175262016-03-03 10:20:07 -08001819 wpa_s, wpa_s->p2pdev->p2ps_join_addr,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001820 "12345670", NULL, 0, 0);
1821 }
Dmitry Shmidt9c175262016-03-03 10:20:07 -08001822 wpa_s->p2pdev->p2ps_method_config_any = 0;
Dmitry Shmidt216983b2015-02-06 10:50:36 -08001823 }
1824
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001825 os_get_reltime(&wpa_s->global->p2p_go_wait_client);
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07001826 if (params->persistent_group) {
Dmitry Shmidtd5ab1b52016-06-21 12:38:41 -07001827 wpas_p2p_store_persistent_group(
Dmitry Shmidt9c175262016-03-03 10:20:07 -08001828 wpa_s->p2pdev, ssid,
Dmitry Shmidt497c1d52011-07-21 15:19:46 -07001829 wpa_s->global->p2p_dev_addr);
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07001830 wpas_p2p_add_psk_list(wpa_s, ssid);
1831 }
Dmitry Shmidtd5ab1b52016-06-21 12:38:41 -07001832
1833 wpas_notify_p2p_group_started(wpa_s, ssid,
Dmitry Shmidtabb90a32016-12-05 15:34:39 -08001834 params->persistent_group, 0,
1835 NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001836 wpas_p2p_cross_connect_setup(wpa_s);
1837 wpas_p2p_set_group_idle_timeout(wpa_s);
Dmitry Shmidt56052862013-10-04 10:23:25 -07001838
1839 if (wpa_s->p2p_first_connection_timeout) {
1840 wpa_dbg(wpa_s, MSG_DEBUG,
1841 "P2P: Start group formation timeout of %d seconds until first data connection on GO",
1842 wpa_s->p2p_first_connection_timeout);
1843 wpa_s->p2p_go_group_formation_completed = 0;
1844 wpa_s->global->p2p_group_formation = wpa_s;
1845 eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
Dmitry Shmidt9c175262016-03-03 10:20:07 -08001846 wpa_s->p2pdev, NULL);
Dmitry Shmidt56052862013-10-04 10:23:25 -07001847 eloop_register_timeout(
1848 wpa_s->p2p_first_connection_timeout, 0,
1849 wpas_p2p_group_formation_timeout,
Dmitry Shmidt9c175262016-03-03 10:20:07 -08001850 wpa_s->p2pdev, NULL);
Dmitry Shmidt56052862013-10-04 10:23:25 -07001851 }
1852
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001853 return;
1854 }
1855
1856 wpa_printf(MSG_DEBUG, "P2P: Setting up WPS for GO provisioning");
1857 if (wpa_supplicant_ap_mac_addr_filter(wpa_s,
1858 params->peer_interface_addr)) {
1859 wpa_printf(MSG_DEBUG, "P2P: Failed to setup MAC address "
1860 "filtering");
1861 return;
1862 }
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001863 if (params->wps_method == WPS_PBC) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001864 wpa_supplicant_ap_wps_pbc(wpa_s, params->peer_interface_addr,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001865 params->peer_device_addr);
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001866#ifdef CONFIG_WPS_NFC
1867 } else if (params->wps_method == WPS_NFC) {
Dmitry Shmidt9c175262016-03-03 10:20:07 -08001868 if (wpa_s->p2pdev->p2p_oob_dev_pw_id !=
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001869 DEV_PW_NFC_CONNECTION_HANDOVER &&
Dmitry Shmidt9c175262016-03-03 10:20:07 -08001870 !wpa_s->p2pdev->p2p_oob_dev_pw) {
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001871 wpa_printf(MSG_DEBUG, "P2P: No NFC Dev Pw known");
1872 return;
1873 }
1874 wpas_ap_wps_add_nfc_pw(
Dmitry Shmidt9c175262016-03-03 10:20:07 -08001875 wpa_s, wpa_s->p2pdev->p2p_oob_dev_pw_id,
1876 wpa_s->p2pdev->p2p_oob_dev_pw,
1877 wpa_s->p2pdev->p2p_peer_oob_pk_hash_known ?
1878 wpa_s->p2pdev->p2p_peer_oob_pubkey_hash : NULL);
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001879#endif /* CONFIG_WPS_NFC */
1880 } else if (wpa_s->p2p_pin[0])
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001881 wpa_supplicant_ap_wps_pin(wpa_s, params->peer_interface_addr,
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001882 wpa_s->p2p_pin, NULL, 0, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001883 os_free(wpa_s->go_params);
1884 wpa_s->go_params = NULL;
1885}
1886
1887
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08001888/**
1889 * wpas_p2p_freq_to_edmg_channel - Convert frequency into EDMG channel
1890 * @freq: Frequency (MHz) to convert
1891 * @op_class: Buffer for returning operating class
1892 * @op_edmg_channel: Buffer for returning channel number
1893 * Returns: 0 on success, -1 on failure
1894 *
1895 * This can be used to find the highest channel bonding which includes the
1896 * specified frequency.
1897 */
1898static int wpas_p2p_freq_to_edmg_channel(struct wpa_supplicant *wpa_s,
1899 unsigned int freq,
1900 u8 *op_class, u8 *op_edmg_channel)
1901{
1902 struct hostapd_hw_modes *hwmode;
1903 struct ieee80211_edmg_config edmg;
1904 unsigned int i;
1905 enum chan_width chanwidth[] = {
1906 CHAN_WIDTH_8640,
1907 CHAN_WIDTH_6480,
1908 CHAN_WIDTH_4320,
1909 };
1910
1911 if (!wpa_s->hw.modes)
1912 return -1;
1913
1914 hwmode = get_mode(wpa_s->hw.modes, wpa_s->hw.num_modes,
Hai Shalomfdcde762020-04-02 11:19:20 -07001915 HOSTAPD_MODE_IEEE80211AD, 0);
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08001916 if (!hwmode) {
1917 wpa_printf(MSG_ERROR,
1918 "Unsupported AP mode: HOSTAPD_MODE_IEEE80211AD");
1919 return -1;
1920 }
1921
1922 /* Find the highest EDMG channel bandwidth to start the P2P GO */
1923 for (i = 0; i < ARRAY_SIZE(chanwidth); i++) {
1924 if (ieee80211_chaninfo_to_channel(freq, chanwidth[i], 0,
1925 op_class,
1926 op_edmg_channel) < 0)
1927 continue;
1928
1929 hostapd_encode_edmg_chan(1, *op_edmg_channel, 0, &edmg);
1930 if (edmg.channels &&
1931 ieee802_edmg_is_allowed(hwmode->edmg, edmg)) {
1932 wpa_printf(MSG_DEBUG,
1933 "Freq %u to EDMG channel %u at opclass %u",
1934 freq, *op_edmg_channel, *op_class);
1935 return 0;
1936 }
1937 }
1938
1939 return -1;
1940}
1941
1942
1943int wpas_p2p_try_edmg_channel(struct wpa_supplicant *wpa_s,
1944 struct p2p_go_neg_results *params)
1945{
1946 u8 op_channel, op_class;
1947 int freq;
1948
1949 /* Try social channel as primary channel frequency */
1950 freq = (!params->freq) ? 58320 + 1 * 2160 : params->freq;
1951
1952 if (wpas_p2p_freq_to_edmg_channel(wpa_s, freq, &op_class,
1953 &op_channel) == 0) {
1954 wpa_printf(MSG_DEBUG,
1955 "Freq %d will be used to set an EDMG connection (channel=%u opclass=%u)",
1956 freq, op_channel, op_class);
1957 params->freq = freq;
1958 return 0;
1959 }
1960
1961 return -1;
1962}
1963
1964
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001965static void wpas_start_wps_go(struct wpa_supplicant *wpa_s,
1966 struct p2p_go_neg_results *params,
1967 int group_formation)
1968{
1969 struct wpa_ssid *ssid;
1970
Dmitry Shmidtaa532512012-09-24 10:35:31 -07001971 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Starting GO");
1972 if (wpas_copy_go_neg_results(wpa_s, params) < 0) {
1973 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Could not copy GO Negotiation "
1974 "results");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001975 return;
Dmitry Shmidtaa532512012-09-24 10:35:31 -07001976 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001977
1978 ssid = wpa_config_add_network(wpa_s->conf);
Dmitry Shmidtaa532512012-09-24 10:35:31 -07001979 if (ssid == NULL) {
1980 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Could not add network for GO");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001981 return;
Dmitry Shmidtaa532512012-09-24 10:35:31 -07001982 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001983
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001984 wpa_s->show_group_started = 0;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001985 wpa_s->p2p_go_group_formation_completed = 0;
1986 wpa_s->group_formation_reported = 0;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001987 os_memset(wpa_s->go_dev_addr, 0, ETH_ALEN);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001988
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001989 wpa_config_set_network_defaults(ssid);
1990 ssid->temporary = 1;
1991 ssid->p2p_group = 1;
Dmitry Shmidtaca489e2016-09-28 15:44:14 -07001992 ssid->p2p_persistent_group = !!params->persistent_group;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001993 ssid->mode = group_formation ? WPAS_MODE_P2P_GROUP_FORMATION :
1994 WPAS_MODE_P2P_GO;
1995 ssid->frequency = params->freq;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001996 ssid->ht40 = params->ht40;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07001997 ssid->vht = params->vht;
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08001998 ssid->max_oper_chwidth = params->max_oper_chwidth;
1999 ssid->vht_center_freq2 = params->vht_center_freq2;
Hai Shalom74f70d42019-02-11 14:42:39 -08002000 ssid->he = params->he;
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08002001 if (params->edmg) {
2002 u8 op_channel, op_class;
2003
2004 if (!wpas_p2p_freq_to_edmg_channel(wpa_s, params->freq,
2005 &op_class, &op_channel)) {
2006 ssid->edmg_channel = op_channel;
2007 ssid->enable_edmg = params->edmg;
2008 } else {
2009 wpa_dbg(wpa_s, MSG_DEBUG,
2010 "P2P: Could not match EDMG channel, freq %d, for GO",
2011 params->freq);
2012 }
2013 }
2014
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002015 ssid->ssid = os_zalloc(params->ssid_len + 1);
2016 if (ssid->ssid) {
2017 os_memcpy(ssid->ssid, params->ssid, params->ssid_len);
2018 ssid->ssid_len = params->ssid_len;
2019 }
2020 ssid->auth_alg = WPA_AUTH_ALG_OPEN;
2021 ssid->key_mgmt = WPA_KEY_MGMT_PSK;
2022 ssid->proto = WPA_PROTO_RSN;
2023 ssid->pairwise_cipher = WPA_CIPHER_CCMP;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002024 ssid->group_cipher = WPA_CIPHER_CCMP;
2025 if (params->freq > 56160) {
2026 /*
2027 * Enable GCMP instead of CCMP as pairwise_cipher and
2028 * group_cipher in 60 GHz.
2029 */
2030 ssid->pairwise_cipher = WPA_CIPHER_GCMP;
2031 ssid->group_cipher = WPA_CIPHER_GCMP;
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08002032 /* P2P GO in 60 GHz is always a PCP (PBSS) */
2033 ssid->pbss = 1;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002034 }
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08002035 if (os_strlen(params->passphrase) > 0) {
2036 ssid->passphrase = os_strdup(params->passphrase);
2037 if (ssid->passphrase == NULL) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07002038 wpa_msg_global(wpa_s, MSG_ERROR,
2039 "P2P: Failed to copy passphrase for GO");
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08002040 wpa_config_remove_network(wpa_s->conf, ssid->id);
2041 return;
2042 }
2043 } else
2044 ssid->passphrase = NULL;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002045 ssid->psk_set = params->psk_set;
2046 if (ssid->psk_set)
2047 os_memcpy(ssid->psk, params->psk, sizeof(ssid->psk));
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08002048 else if (ssid->passphrase)
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002049 wpa_config_update_psk(ssid);
Dmitry Shmidt9c175262016-03-03 10:20:07 -08002050 ssid->ap_max_inactivity = wpa_s->p2pdev->conf->p2p_go_max_inactivity;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002051
2052 wpa_s->ap_configured_cb = p2p_go_configured;
2053 wpa_s->ap_configured_cb_ctx = wpa_s;
2054 wpa_s->ap_configured_cb_data = wpa_s->go_params;
Dmitry Shmidt6dc03bd2014-05-16 10:40:13 -07002055 wpa_s->scan_req = NORMAL_SCAN_REQ;
Jouni Malinen75ecf522011-06-27 15:19:46 -07002056 wpa_s->connect_without_scan = ssid;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002057 wpa_s->reassociate = 1;
2058 wpa_s->disconnected = 0;
Dmitry Shmidtaa532512012-09-24 10:35:31 -07002059 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Request scan (that will be skipped) to "
2060 "start GO)");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002061 wpa_supplicant_req_scan(wpa_s, 0, 0);
2062}
2063
2064
2065static void wpas_p2p_clone_config(struct wpa_supplicant *dst,
2066 const struct wpa_supplicant *src)
2067{
2068 struct wpa_config *d;
2069 const struct wpa_config *s;
2070
2071 d = dst->conf;
2072 s = src->conf;
2073
Dmitry Shmidt849734c2016-05-27 09:59:01 -07002074#define C(n) \
2075do { \
2076 if (s->n && !d->n) \
2077 d->n = os_strdup(s->n); \
2078} while (0)
2079
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002080 C(device_name);
2081 C(manufacturer);
2082 C(model_name);
2083 C(model_number);
2084 C(serial_number);
2085 C(config_methods);
2086#undef C
2087
2088 os_memcpy(d->device_type, s->device_type, WPS_DEV_TYPE_LEN);
2089 os_memcpy(d->sec_device_type, s->sec_device_type,
2090 sizeof(d->sec_device_type));
2091 d->num_sec_device_types = s->num_sec_device_types;
2092
2093 d->p2p_group_idle = s->p2p_group_idle;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002094 d->p2p_go_freq_change_policy = s->p2p_go_freq_change_policy;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002095 d->p2p_intra_bss = s->p2p_intra_bss;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002096 d->persistent_reconnect = s->persistent_reconnect;
Dmitry Shmidt04949592012-07-19 12:16:46 -07002097 d->max_num_sta = s->max_num_sta;
2098 d->pbc_in_m1 = s->pbc_in_m1;
Dmitry Shmidt444d5672013-04-01 13:08:44 -07002099 d->ignore_old_scan_res = s->ignore_old_scan_res;
Dmitry Shmidt51b6ea82013-05-08 10:42:09 -07002100 d->beacon_int = s->beacon_int;
Dmitry Shmidt18463232014-01-24 12:29:41 -08002101 d->dtim_period = s->dtim_period;
Dmitry Shmidt7f656022015-02-25 14:36:37 -08002102 d->p2p_go_ctwindow = s->p2p_go_ctwindow;
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07002103 d->disassoc_low_ack = s->disassoc_low_ack;
Dmitry Shmidt96571392013-10-14 12:54:46 -07002104 d->disable_scan_offload = s->disable_scan_offload;
Dmitry Shmidt807291d2015-01-27 13:40:23 -08002105 d->passive_scan = s->passive_scan;
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08002106
Dmitry Shmidt849734c2016-05-27 09:59:01 -07002107 if (s->wps_nfc_dh_privkey && s->wps_nfc_dh_pubkey &&
2108 !d->wps_nfc_pw_from_config) {
2109 wpabuf_free(d->wps_nfc_dh_privkey);
2110 wpabuf_free(d->wps_nfc_dh_pubkey);
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08002111 d->wps_nfc_dh_privkey = wpabuf_dup(s->wps_nfc_dh_privkey);
2112 d->wps_nfc_dh_pubkey = wpabuf_dup(s->wps_nfc_dh_pubkey);
2113 }
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07002114 d->p2p_cli_probe = s->p2p_cli_probe;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002115 d->go_interworking = s->go_interworking;
2116 d->go_access_network_type = s->go_access_network_type;
2117 d->go_internet = s->go_internet;
2118 d->go_venue_group = s->go_venue_group;
2119 d->go_venue_type = s->go_venue_type;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002120}
2121
2122
Dmitry Shmidt34af3062013-07-11 10:46:32 -07002123static void wpas_p2p_get_group_ifname(struct wpa_supplicant *wpa_s,
2124 char *ifname, size_t len)
2125{
2126 char *ifname_ptr = wpa_s->ifname;
2127
2128 if (os_strncmp(wpa_s->ifname, P2P_MGMT_DEVICE_PREFIX,
2129 os_strlen(P2P_MGMT_DEVICE_PREFIX)) == 0) {
2130 ifname_ptr = os_strrchr(wpa_s->ifname, '-') + 1;
2131 }
2132
2133 os_snprintf(ifname, len, "p2p-%s-%d", ifname_ptr, wpa_s->p2p_group_idx);
2134 if (os_strlen(ifname) >= IFNAMSIZ &&
2135 os_strlen(wpa_s->ifname) < IFNAMSIZ) {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002136 int res;
2137
Dmitry Shmidt34af3062013-07-11 10:46:32 -07002138 /* Try to avoid going over the IFNAMSIZ length limit */
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002139 res = os_snprintf(ifname, len, "p2p-%d", wpa_s->p2p_group_idx);
2140 if (os_snprintf_error(len, res) && len)
2141 ifname[len - 1] = '\0';
Dmitry Shmidt34af3062013-07-11 10:46:32 -07002142 }
2143}
2144
2145
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002146static int wpas_p2p_add_group_interface(struct wpa_supplicant *wpa_s,
2147 enum wpa_driver_if_type type)
2148{
2149 char ifname[120], force_ifname[120];
2150
2151 if (wpa_s->pending_interface_name[0]) {
2152 wpa_printf(MSG_DEBUG, "P2P: Pending virtual interface exists "
2153 "- skip creation of a new one");
2154 if (is_zero_ether_addr(wpa_s->pending_interface_addr)) {
2155 wpa_printf(MSG_DEBUG, "P2P: Pending virtual address "
2156 "unknown?! ifname='%s'",
2157 wpa_s->pending_interface_name);
2158 return -1;
2159 }
2160 return 0;
2161 }
2162
Dmitry Shmidt34af3062013-07-11 10:46:32 -07002163 wpas_p2p_get_group_ifname(wpa_s, ifname, sizeof(ifname));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002164 force_ifname[0] = '\0';
2165
2166 wpa_printf(MSG_DEBUG, "P2P: Create a new interface %s for the group",
2167 ifname);
2168 wpa_s->p2p_group_idx++;
2169
2170 wpa_s->pending_interface_type = type;
2171 if (wpa_drv_if_add(wpa_s, type, ifname, NULL, NULL, force_ifname,
2172 wpa_s->pending_interface_addr, NULL) < 0) {
2173 wpa_printf(MSG_ERROR, "P2P: Failed to create new group "
2174 "interface");
2175 return -1;
2176 }
2177
Jimmy Chen36c21992018-11-29 16:46:43 +08002178 if (wpa_s->conf->p2p_interface_random_mac_addr) {
Hai Shalom74f70d42019-02-11 14:42:39 -08002179 random_mac_addr(wpa_s->pending_interface_addr);
2180 wpa_printf(MSG_DEBUG, "P2P: Generate random MAC address " MACSTR
2181 " for the group",
2182 MAC2STR(wpa_s->pending_interface_addr));
Jimmy Chen36c21992018-11-29 16:46:43 +08002183 }
2184
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002185 if (force_ifname[0]) {
2186 wpa_printf(MSG_DEBUG, "P2P: Driver forced interface name %s",
2187 force_ifname);
2188 os_strlcpy(wpa_s->pending_interface_name, force_ifname,
2189 sizeof(wpa_s->pending_interface_name));
2190 } else
2191 os_strlcpy(wpa_s->pending_interface_name, ifname,
2192 sizeof(wpa_s->pending_interface_name));
2193 wpa_printf(MSG_DEBUG, "P2P: Created pending virtual interface %s addr "
2194 MACSTR, wpa_s->pending_interface_name,
2195 MAC2STR(wpa_s->pending_interface_addr));
2196
2197 return 0;
2198}
2199
2200
2201static void wpas_p2p_remove_pending_group_interface(
2202 struct wpa_supplicant *wpa_s)
2203{
2204 if (!wpa_s->pending_interface_name[0] ||
2205 is_zero_ether_addr(wpa_s->pending_interface_addr))
2206 return; /* No pending virtual interface */
2207
2208 wpa_printf(MSG_DEBUG, "P2P: Removing pending group interface %s",
2209 wpa_s->pending_interface_name);
2210 wpa_drv_if_remove(wpa_s, wpa_s->pending_interface_type,
2211 wpa_s->pending_interface_name);
2212 os_memset(wpa_s->pending_interface_addr, 0, ETH_ALEN);
2213 wpa_s->pending_interface_name[0] = '\0';
Dmitry Shmidt216983b2015-02-06 10:50:36 -08002214 wpa_s->global->pending_group_iface_for_p2ps = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002215}
2216
2217
2218static struct wpa_supplicant *
2219wpas_p2p_init_group_interface(struct wpa_supplicant *wpa_s, int go)
2220{
2221 struct wpa_interface iface;
2222 struct wpa_supplicant *group_wpa_s;
2223
2224 if (!wpa_s->pending_interface_name[0]) {
2225 wpa_printf(MSG_ERROR, "P2P: No pending group interface");
2226 if (!wpas_p2p_create_iface(wpa_s))
2227 return NULL;
2228 /*
2229 * Something has forced us to remove the pending interface; try
2230 * to create a new one and hope for the best that we will get
2231 * the same local address.
2232 */
2233 if (wpas_p2p_add_group_interface(wpa_s, go ? WPA_IF_P2P_GO :
2234 WPA_IF_P2P_CLIENT) < 0)
2235 return NULL;
2236 }
2237
2238 os_memset(&iface, 0, sizeof(iface));
2239 iface.ifname = wpa_s->pending_interface_name;
2240 iface.driver = wpa_s->driver->name;
Dmitry Shmidt34af3062013-07-11 10:46:32 -07002241 if (wpa_s->conf->ctrl_interface == NULL &&
2242 wpa_s->parent != wpa_s &&
2243 wpa_s->p2p_mgmt &&
2244 (wpa_s->drv_flags & WPA_DRIVER_FLAGS_DEDICATED_P2P_DEVICE))
2245 iface.ctrl_interface = wpa_s->parent->conf->ctrl_interface;
2246 else
2247 iface.ctrl_interface = wpa_s->conf->ctrl_interface;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002248 iface.driver_param = wpa_s->conf->driver_param;
Dmitry Shmidt203eadb2015-03-05 14:16:04 -08002249 group_wpa_s = wpa_supplicant_add_iface(wpa_s->global, &iface, wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002250 if (group_wpa_s == NULL) {
2251 wpa_printf(MSG_ERROR, "P2P: Failed to create new "
2252 "wpa_supplicant interface");
2253 return NULL;
2254 }
2255 wpa_s->pending_interface_name[0] = '\0';
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002256 group_wpa_s->p2p_group_interface = go ? P2P_GROUP_INTERFACE_GO :
2257 P2P_GROUP_INTERFACE_CLIENT;
2258 wpa_s->global->p2p_group_formation = group_wpa_s;
Dmitry Shmidt216983b2015-02-06 10:50:36 -08002259 wpa_s->global->pending_group_iface_for_p2ps = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002260
2261 wpas_p2p_clone_config(group_wpa_s, wpa_s);
2262
Jimmy Chen36c21992018-11-29 16:46:43 +08002263 if (wpa_s->conf->p2p_interface_random_mac_addr) {
Hai Shalom74f70d42019-02-11 14:42:39 -08002264 if (wpa_drv_set_mac_addr(group_wpa_s,
2265 wpa_s->pending_interface_addr) < 0) {
Jimmy Chen36c21992018-11-29 16:46:43 +08002266 wpa_msg(group_wpa_s, MSG_INFO,
2267 "Failed to set random MAC address");
Hai Shalom74f70d42019-02-11 14:42:39 -08002268 wpa_supplicant_remove_iface(wpa_s->global, group_wpa_s,
2269 0);
Jimmy Chen36c21992018-11-29 16:46:43 +08002270 return NULL;
2271 }
2272
2273 if (wpa_supplicant_update_mac_addr(group_wpa_s) < 0) {
2274 wpa_msg(group_wpa_s, MSG_INFO,
2275 "Could not update MAC address information");
Hai Shalom74f70d42019-02-11 14:42:39 -08002276 wpa_supplicant_remove_iface(wpa_s->global, group_wpa_s,
2277 0);
Jimmy Chen36c21992018-11-29 16:46:43 +08002278 return NULL;
2279 }
2280
Hai Shalom74f70d42019-02-11 14:42:39 -08002281 wpa_printf(MSG_DEBUG, "P2P: Using random MAC address " MACSTR
2282 " for the group",
2283 MAC2STR(wpa_s->pending_interface_addr));
Jimmy Chen36c21992018-11-29 16:46:43 +08002284 }
2285
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002286 return group_wpa_s;
2287}
2288
2289
2290static void wpas_p2p_group_formation_timeout(void *eloop_ctx,
2291 void *timeout_ctx)
2292{
2293 struct wpa_supplicant *wpa_s = eloop_ctx;
2294 wpa_printf(MSG_DEBUG, "P2P: Group Formation timed out");
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07002295 wpas_p2p_group_formation_failed(wpa_s, 0);
Dmitry Shmidt34af3062013-07-11 10:46:32 -07002296}
2297
2298
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07002299static void wpas_p2p_group_formation_failed(struct wpa_supplicant *wpa_s,
2300 int already_deleted)
Dmitry Shmidt34af3062013-07-11 10:46:32 -07002301{
2302 eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
Dmitry Shmidt9c175262016-03-03 10:20:07 -08002303 wpa_s->p2pdev, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002304 if (wpa_s->global->p2p)
2305 p2p_group_formation_failed(wpa_s->global->p2p);
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07002306 wpas_group_formation_completed(wpa_s, 0, already_deleted);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002307}
2308
2309
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002310static void wpas_p2p_grpform_fail_after_wps(struct wpa_supplicant *wpa_s)
2311{
2312 wpa_printf(MSG_DEBUG, "P2P: Reject group formation due to WPS provisioning failure");
2313 eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
Dmitry Shmidt9c175262016-03-03 10:20:07 -08002314 wpa_s->p2pdev, NULL);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002315 eloop_register_timeout(0, 0, wpas_p2p_group_formation_timeout,
Dmitry Shmidt9c175262016-03-03 10:20:07 -08002316 wpa_s->p2pdev, NULL);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002317 wpa_s->global->p2p_fail_on_wps_complete = 0;
2318}
2319
2320
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07002321void wpas_p2p_ap_setup_failed(struct wpa_supplicant *wpa_s)
2322{
2323 if (wpa_s->global->p2p_group_formation != wpa_s)
2324 return;
2325 /* Speed up group formation timeout since this cannot succeed */
2326 eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
Dmitry Shmidt9c175262016-03-03 10:20:07 -08002327 wpa_s->p2pdev, NULL);
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07002328 eloop_register_timeout(0, 0, wpas_p2p_group_formation_timeout,
Dmitry Shmidt9c175262016-03-03 10:20:07 -08002329 wpa_s->p2pdev, NULL);
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07002330}
2331
2332
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002333static void wpas_go_neg_completed(void *ctx, struct p2p_go_neg_results *res)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002334{
2335 struct wpa_supplicant *wpa_s = ctx;
Dmitry Shmidt9c175262016-03-03 10:20:07 -08002336 struct wpa_supplicant *group_wpa_s;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002337
2338 if (wpa_s->off_channel_freq || wpa_s->roc_waiting_drv_freq) {
2339 wpa_drv_cancel_remain_on_channel(wpa_s);
2340 wpa_s->off_channel_freq = 0;
2341 wpa_s->roc_waiting_drv_freq = 0;
2342 }
2343
2344 if (res->status) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07002345 wpa_msg_global(wpa_s, MSG_INFO,
2346 P2P_EVENT_GO_NEG_FAILURE "status=%d",
2347 res->status);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002348 wpas_notify_p2p_go_neg_completed(wpa_s, res);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002349 wpas_p2p_remove_pending_group_interface(wpa_s);
2350 return;
2351 }
2352
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002353 if (!res->role_go) {
2354 /* Inform driver of the operating channel of GO. */
2355 wpa_drv_set_prob_oper_freq(wpa_s, res->freq);
2356 }
2357
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002358 if (wpa_s->p2p_go_ht40)
2359 res->ht40 = 1;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07002360 if (wpa_s->p2p_go_vht)
2361 res->vht = 1;
Hai Shalomc3565922019-10-28 11:58:20 -07002362 if (wpa_s->p2p_go_he)
2363 res->he = 1;
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08002364 if (wpa_s->p2p_go_edmg)
2365 res->edmg = 1;
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08002366 res->max_oper_chwidth = wpa_s->p2p_go_max_oper_chwidth;
2367 res->vht_center_freq2 = wpa_s->p2p_go_vht_center_freq2;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002368
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07002369 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_GO_NEG_SUCCESS "role=%s "
2370 "freq=%d ht40=%d peer_dev=" MACSTR " peer_iface=" MACSTR
2371 " wps_method=%s",
2372 res->role_go ? "GO" : "client", res->freq, res->ht40,
2373 MAC2STR(res->peer_device_addr),
2374 MAC2STR(res->peer_interface_addr),
2375 p2p_wps_method_text(res->wps_method));
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002376 wpas_notify_p2p_go_neg_completed(wpa_s, res);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002377
Dmitry Shmidt04949592012-07-19 12:16:46 -07002378 if (res->role_go && wpa_s->p2p_persistent_id >= 0) {
2379 struct wpa_ssid *ssid;
2380 ssid = wpa_config_get_network(wpa_s->conf,
2381 wpa_s->p2p_persistent_id);
2382 if (ssid && ssid->disabled == 2 &&
2383 ssid->mode == WPAS_MODE_P2P_GO && ssid->passphrase) {
2384 size_t len = os_strlen(ssid->passphrase);
2385 wpa_printf(MSG_DEBUG, "P2P: Override passphrase based "
2386 "on requested persistent group");
2387 os_memcpy(res->passphrase, ssid->passphrase, len);
2388 res->passphrase[len] = '\0';
2389 }
2390 }
2391
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002392 if (wpa_s->create_p2p_iface) {
Dmitry Shmidt9c175262016-03-03 10:20:07 -08002393 group_wpa_s =
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002394 wpas_p2p_init_group_interface(wpa_s, res->role_go);
2395 if (group_wpa_s == NULL) {
2396 wpas_p2p_remove_pending_group_interface(wpa_s);
Dmitry Shmidtd11f0192014-03-24 12:09:47 -07002397 eloop_cancel_timeout(wpas_p2p_long_listen_timeout,
2398 wpa_s, NULL);
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07002399 wpas_p2p_group_formation_failed(wpa_s, 1);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002400 return;
2401 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002402 os_memset(wpa_s->pending_interface_addr, 0, ETH_ALEN);
2403 wpa_s->pending_interface_name[0] = '\0';
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002404 } else {
Dmitry Shmidt9c175262016-03-03 10:20:07 -08002405 group_wpa_s = wpa_s->parent;
2406 wpa_s->global->p2p_group_formation = group_wpa_s;
2407 if (group_wpa_s != wpa_s)
Dmitry Shmidt849734c2016-05-27 09:59:01 -07002408 wpas_p2p_clone_config(group_wpa_s, wpa_s);
Dmitry Shmidt9c175262016-03-03 10:20:07 -08002409 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002410
Dmitry Shmidt9c175262016-03-03 10:20:07 -08002411 group_wpa_s->p2p_in_provisioning = 1;
2412 group_wpa_s->p2pdev = wpa_s;
2413 if (group_wpa_s != wpa_s) {
2414 os_memcpy(group_wpa_s->p2p_pin, wpa_s->p2p_pin,
2415 sizeof(group_wpa_s->p2p_pin));
2416 group_wpa_s->p2p_wps_method = wpa_s->p2p_wps_method;
2417 }
2418 if (res->role_go) {
2419 wpas_start_wps_go(group_wpa_s, res, 1);
2420 } else {
2421 os_get_reltime(&group_wpa_s->scan_min_time);
2422 wpas_start_wps_enrollee(group_wpa_s, res);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002423 }
2424
Hai Shalomfdcde762020-04-02 11:19:20 -07002425 wpa_s->global->p2p_long_listen = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002426 eloop_cancel_timeout(wpas_p2p_long_listen_timeout, wpa_s, NULL);
2427
2428 eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s, NULL);
2429 eloop_register_timeout(15 + res->peer_config_timeout / 100,
2430 (res->peer_config_timeout % 100) * 10000,
2431 wpas_p2p_group_formation_timeout, wpa_s, NULL);
2432}
2433
2434
Dmitry Shmidt7a53dbb2015-06-11 13:13:53 -07002435static void wpas_go_neg_req_rx(void *ctx, const u8 *src, u16 dev_passwd_id,
2436 u8 go_intent)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002437{
2438 struct wpa_supplicant *wpa_s = ctx;
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07002439 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_GO_NEG_REQUEST MACSTR
Dmitry Shmidt7a53dbb2015-06-11 13:13:53 -07002440 " dev_passwd_id=%u go_intent=%u", MAC2STR(src),
2441 dev_passwd_id, go_intent);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002442
Dmitry Shmidt7a53dbb2015-06-11 13:13:53 -07002443 wpas_notify_p2p_go_neg_req(wpa_s, src, dev_passwd_id, go_intent);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002444}
2445
2446
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002447static void wpas_dev_found(void *ctx, const u8 *addr,
2448 const struct p2p_peer_info *info,
2449 int new_device)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002450{
Roshan Piusfd2fd662017-01-23 13:41:57 -08002451 u8 *wfd_dev_info = NULL;
2452 u8 wfd_dev_info_len = 0;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002453#ifndef CONFIG_NO_STDOUT_DEBUG
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002454 struct wpa_supplicant *wpa_s = ctx;
2455 char devtype[WPS_DEV_TYPE_BUFSIZE];
Dmitry Shmidt04f534e2013-12-09 15:50:16 -08002456 char *wfd_dev_info_hex = NULL;
2457
Irfan Sheriff8367dc92012-09-09 17:08:19 -07002458#ifdef CONFIG_WIFI_DISPLAY
Dmitry Shmidt04f534e2013-12-09 15:50:16 -08002459 wfd_dev_info_hex = wifi_display_subelem_hex(info->wfd_subelems,
2460 WFD_SUBELEM_DEVICE_INFO);
Roshan Piusfd2fd662017-01-23 13:41:57 -08002461 if (wfd_dev_info_hex) {
2462 wfd_dev_info_len = strlen(wfd_dev_info_hex) / 2;
2463 wfd_dev_info = os_zalloc(wfd_dev_info_len);
2464 // Only used for notification, so not handling error.
2465 hexstr2bin(wfd_dev_info_hex, wfd_dev_info, wfd_dev_info_len);
2466 }
Irfan Sheriff8367dc92012-09-09 17:08:19 -07002467#endif /* CONFIG_WIFI_DISPLAY */
Dmitry Shmidt04f534e2013-12-09 15:50:16 -08002468
Dmitry Shmidt216983b2015-02-06 10:50:36 -08002469 if (info->p2ps_instance) {
2470 char str[256];
2471 const u8 *buf = wpabuf_head(info->p2ps_instance);
2472 size_t len = wpabuf_len(info->p2ps_instance);
2473
2474 while (len) {
2475 u32 id;
2476 u16 methods;
2477 u8 str_len;
2478
2479 if (len < 4 + 2 + 1)
2480 break;
2481 id = WPA_GET_LE32(buf);
2482 buf += sizeof(u32);
2483 methods = WPA_GET_BE16(buf);
2484 buf += sizeof(u16);
2485 str_len = *buf++;
2486 if (str_len > len - 4 - 2 - 1)
2487 break;
2488 os_memcpy(str, buf, str_len);
2489 str[str_len] = '\0';
2490 buf += str_len;
2491 len -= str_len + sizeof(u32) + sizeof(u16) + sizeof(u8);
2492
2493 wpa_msg_global(wpa_s, MSG_INFO,
2494 P2P_EVENT_DEVICE_FOUND MACSTR
2495 " p2p_dev_addr=" MACSTR
2496 " pri_dev_type=%s name='%s'"
2497 " config_methods=0x%x"
2498 " dev_capab=0x%x"
2499 " group_capab=0x%x"
2500 " adv_id=%x asp_svc=%s%s",
2501 MAC2STR(addr),
2502 MAC2STR(info->p2p_device_addr),
2503 wps_dev_type_bin2str(
2504 info->pri_dev_type,
2505 devtype, sizeof(devtype)),
2506 info->device_name, methods,
2507 info->dev_capab, info->group_capab,
2508 id, str,
2509 info->vendor_elems ?
2510 " vendor_elems=1" : "");
2511 }
2512 goto done;
2513 }
2514
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07002515 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_DEVICE_FOUND MACSTR
2516 " p2p_dev_addr=" MACSTR
2517 " pri_dev_type=%s name='%s' config_methods=0x%x "
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002518 "dev_capab=0x%x group_capab=0x%x%s%s%s new=%d",
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07002519 MAC2STR(addr), MAC2STR(info->p2p_device_addr),
2520 wps_dev_type_bin2str(info->pri_dev_type, devtype,
2521 sizeof(devtype)),
2522 info->device_name, info->config_methods,
2523 info->dev_capab, info->group_capab,
Dmitry Shmidt04f534e2013-12-09 15:50:16 -08002524 wfd_dev_info_hex ? " wfd_dev_info=0x" : "",
Dmitry Shmidt2e67f062014-07-16 09:55:28 -07002525 wfd_dev_info_hex ? wfd_dev_info_hex : "",
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002526 info->vendor_elems ? " vendor_elems=1" : "",
2527 new_device);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002528
Dmitry Shmidt216983b2015-02-06 10:50:36 -08002529done:
Dmitry Shmidt04f534e2013-12-09 15:50:16 -08002530 os_free(wfd_dev_info_hex);
Dmitry Shmidt97672262014-02-03 13:02:54 -08002531#endif /* CONFIG_NO_STDOUT_DEBUG */
Dmitry Shmidt04f534e2013-12-09 15:50:16 -08002532
Roshan Piusfd2fd662017-01-23 13:41:57 -08002533 wpas_notify_p2p_device_found(ctx, addr, info, wfd_dev_info,
2534 wfd_dev_info_len, new_device);
2535 os_free(wfd_dev_info);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002536}
2537
2538
2539static void wpas_dev_lost(void *ctx, const u8 *dev_addr)
2540{
2541 struct wpa_supplicant *wpa_s = ctx;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002542
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07002543 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_DEVICE_LOST
2544 "p2p_dev_addr=" MACSTR, MAC2STR(dev_addr));
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002545
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002546 wpas_notify_p2p_device_lost(wpa_s, dev_addr);
2547}
2548
2549
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07002550static void wpas_find_stopped(void *ctx)
2551{
2552 struct wpa_supplicant *wpa_s = ctx;
Dmitry Shmidt9c175262016-03-03 10:20:07 -08002553
2554 if (wpa_s->p2p_scan_work && wpas_abort_ongoing_scan(wpa_s) < 0)
2555 wpa_printf(MSG_DEBUG, "P2P: Abort ongoing scan failed");
2556
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07002557 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_FIND_STOPPED);
Dmitry Shmidt8bd70b72015-05-26 16:02:19 -07002558 wpas_notify_p2p_find_stopped(wpa_s);
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07002559}
2560
2561
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002562struct wpas_p2p_listen_work {
2563 unsigned int freq;
2564 unsigned int duration;
2565 struct wpabuf *probe_resp_ie;
2566};
2567
2568
2569static void wpas_p2p_listen_work_free(struct wpas_p2p_listen_work *lwork)
2570{
2571 if (lwork == NULL)
2572 return;
2573 wpabuf_free(lwork->probe_resp_ie);
2574 os_free(lwork);
2575}
2576
2577
2578static void wpas_p2p_listen_work_done(struct wpa_supplicant *wpa_s)
2579{
2580 struct wpas_p2p_listen_work *lwork;
2581
2582 if (!wpa_s->p2p_listen_work)
2583 return;
2584
2585 lwork = wpa_s->p2p_listen_work->ctx;
2586 wpas_p2p_listen_work_free(lwork);
2587 radio_work_done(wpa_s->p2p_listen_work);
2588 wpa_s->p2p_listen_work = NULL;
2589}
2590
2591
2592static void wpas_start_listen_cb(struct wpa_radio_work *work, int deinit)
2593{
2594 struct wpa_supplicant *wpa_s = work->wpa_s;
2595 struct wpas_p2p_listen_work *lwork = work->ctx;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002596 unsigned int duration;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002597
2598 if (deinit) {
Dmitry Shmidtbd14a572014-02-18 10:33:49 -08002599 if (work->started) {
2600 wpa_s->p2p_listen_work = NULL;
2601 wpas_stop_listen(wpa_s);
2602 }
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002603 wpas_p2p_listen_work_free(lwork);
2604 return;
2605 }
2606
2607 wpa_s->p2p_listen_work = work;
2608
2609 wpa_drv_set_ap_wps_ie(wpa_s, NULL, lwork->probe_resp_ie, NULL);
2610
2611 if (wpa_drv_probe_req_report(wpa_s, 1) < 0) {
2612 wpa_printf(MSG_DEBUG, "P2P: Failed to request the driver to "
2613 "report received Probe Request frames");
2614 wpas_p2p_listen_work_done(wpa_s);
2615 return;
2616 }
2617
2618 wpa_s->pending_listen_freq = lwork->freq;
2619 wpa_s->pending_listen_duration = lwork->duration;
2620
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002621 duration = lwork->duration;
2622#ifdef CONFIG_TESTING_OPTIONS
2623 if (wpa_s->extra_roc_dur) {
2624 wpa_printf(MSG_DEBUG, "TESTING: Increase ROC duration %u -> %u",
2625 duration, duration + wpa_s->extra_roc_dur);
2626 duration += wpa_s->extra_roc_dur;
2627 }
2628#endif /* CONFIG_TESTING_OPTIONS */
2629
2630 if (wpa_drv_remain_on_channel(wpa_s, lwork->freq, duration) < 0) {
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002631 wpa_printf(MSG_DEBUG, "P2P: Failed to request the driver "
2632 "to remain on channel (%u MHz) for Listen "
2633 "state", lwork->freq);
2634 wpas_p2p_listen_work_done(wpa_s);
2635 wpa_s->pending_listen_freq = 0;
2636 return;
2637 }
2638 wpa_s->off_channel_freq = 0;
2639 wpa_s->roc_waiting_drv_freq = lwork->freq;
2640}
2641
2642
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002643static int wpas_start_listen(void *ctx, unsigned int freq,
2644 unsigned int duration,
2645 const struct wpabuf *probe_resp_ie)
2646{
2647 struct wpa_supplicant *wpa_s = ctx;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002648 struct wpas_p2p_listen_work *lwork;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002649
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002650 if (wpa_s->p2p_listen_work) {
2651 wpa_printf(MSG_DEBUG, "P2P: Reject start_listen since p2p_listen_work already exists");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002652 return -1;
2653 }
2654
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002655 lwork = os_zalloc(sizeof(*lwork));
2656 if (lwork == NULL)
2657 return -1;
2658 lwork->freq = freq;
2659 lwork->duration = duration;
2660 if (probe_resp_ie) {
2661 lwork->probe_resp_ie = wpabuf_dup(probe_resp_ie);
2662 if (lwork->probe_resp_ie == NULL) {
2663 wpas_p2p_listen_work_free(lwork);
2664 return -1;
2665 }
2666 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002667
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002668 if (radio_add_work(wpa_s, freq, "p2p-listen", 0, wpas_start_listen_cb,
2669 lwork) < 0) {
2670 wpas_p2p_listen_work_free(lwork);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002671 return -1;
2672 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002673
2674 return 0;
2675}
2676
2677
2678static void wpas_stop_listen(void *ctx)
2679{
2680 struct wpa_supplicant *wpa_s = ctx;
2681 if (wpa_s->off_channel_freq || wpa_s->roc_waiting_drv_freq) {
2682 wpa_drv_cancel_remain_on_channel(wpa_s);
2683 wpa_s->off_channel_freq = 0;
2684 wpa_s->roc_waiting_drv_freq = 0;
2685 }
2686 wpa_drv_set_ap_wps_ie(wpa_s, NULL, NULL, NULL);
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07002687
2688 /*
2689 * Don't cancel Probe Request RX reporting for a connected P2P Client
2690 * handling Probe Request frames.
2691 */
2692 if (!wpa_s->p2p_cli_probe)
2693 wpa_drv_probe_req_report(wpa_s, 0);
2694
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002695 wpas_p2p_listen_work_done(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002696}
2697
2698
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07002699static int wpas_send_probe_resp(void *ctx, const struct wpabuf *buf,
2700 unsigned int freq)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002701{
2702 struct wpa_supplicant *wpa_s = ctx;
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07002703 return wpa_drv_send_mlme(wpa_s, wpabuf_head(buf), wpabuf_len(buf), 1,
Hai Shalomfdcde762020-04-02 11:19:20 -07002704 freq, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002705}
2706
2707
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002708static void wpas_prov_disc_local_display(struct wpa_supplicant *wpa_s,
2709 const u8 *peer, const char *params,
2710 unsigned int generated_pin)
2711{
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07002712 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_SHOW_PIN MACSTR
2713 " %08d%s", MAC2STR(peer), generated_pin, params);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002714}
2715
2716
2717static void wpas_prov_disc_local_keypad(struct wpa_supplicant *wpa_s,
2718 const u8 *peer, const char *params)
2719{
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07002720 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_ENTER_PIN MACSTR
2721 "%s", MAC2STR(peer), params);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002722}
2723
2724
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002725static void wpas_prov_disc_req(void *ctx, const u8 *peer, u16 config_methods,
2726 const u8 *dev_addr, const u8 *pri_dev_type,
2727 const char *dev_name, u16 supp_config_methods,
2728 u8 dev_capab, u8 group_capab, const u8 *group_id,
2729 size_t group_id_len)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002730{
2731 struct wpa_supplicant *wpa_s = ctx;
2732 char devtype[WPS_DEV_TYPE_BUFSIZE];
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002733 char params[300];
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002734 u8 empty_dev_type[8];
2735 unsigned int generated_pin = 0;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002736 struct wpa_supplicant *group = NULL;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002737 int res;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002738
2739 if (group_id) {
2740 for (group = wpa_s->global->ifaces; group; group = group->next)
2741 {
2742 struct wpa_ssid *s = group->current_ssid;
2743 if (s != NULL &&
2744 s->mode == WPAS_MODE_P2P_GO &&
2745 group_id_len - ETH_ALEN == s->ssid_len &&
2746 os_memcmp(group_id + ETH_ALEN, s->ssid,
2747 s->ssid_len) == 0)
2748 break;
2749 }
2750 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002751
2752 if (pri_dev_type == NULL) {
2753 os_memset(empty_dev_type, 0, sizeof(empty_dev_type));
2754 pri_dev_type = empty_dev_type;
2755 }
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002756 res = os_snprintf(params, sizeof(params), " p2p_dev_addr=" MACSTR
2757 " pri_dev_type=%s name='%s' config_methods=0x%x "
2758 "dev_capab=0x%x group_capab=0x%x%s%s",
2759 MAC2STR(dev_addr),
2760 wps_dev_type_bin2str(pri_dev_type, devtype,
2761 sizeof(devtype)),
2762 dev_name, supp_config_methods, dev_capab, group_capab,
2763 group ? " group=" : "",
2764 group ? group->ifname : "");
2765 if (os_snprintf_error(sizeof(params), res))
2766 wpa_printf(MSG_DEBUG, "P2P: PD Request event truncated");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002767 params[sizeof(params) - 1] = '\0';
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08002768
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002769 if (config_methods & WPS_CONFIG_DISPLAY) {
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08002770 if (wps_generate_pin(&generated_pin) < 0) {
2771 wpa_printf(MSG_DEBUG, "P2P: Could not generate PIN");
2772 wpas_notify_p2p_provision_discovery(
2773 wpa_s, peer, 0 /* response */,
2774 P2P_PROV_DISC_INFO_UNAVAILABLE, 0, 0);
2775 return;
2776 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002777 wpas_prov_disc_local_display(wpa_s, peer, params,
2778 generated_pin);
2779 } else if (config_methods & WPS_CONFIG_KEYPAD)
2780 wpas_prov_disc_local_keypad(wpa_s, peer, params);
2781 else if (config_methods & WPS_CONFIG_PUSHBUTTON)
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07002782 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_PBC_REQ
2783 MACSTR "%s", MAC2STR(peer), params);
Jouni Malinen75ecf522011-06-27 15:19:46 -07002784
2785 wpas_notify_p2p_provision_discovery(wpa_s, peer, 1 /* request */,
2786 P2P_PROV_DISC_SUCCESS,
2787 config_methods, generated_pin);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002788}
2789
2790
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002791static void wpas_prov_disc_resp(void *ctx, const u8 *peer, u16 config_methods)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002792{
2793 struct wpa_supplicant *wpa_s = ctx;
2794 unsigned int generated_pin = 0;
Dmitry Shmidt04949592012-07-19 12:16:46 -07002795 char params[20];
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002796
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08002797 if (wpa_s->pending_pd_before_join &&
2798 (os_memcmp(peer, wpa_s->pending_join_dev_addr, ETH_ALEN) == 0 ||
2799 os_memcmp(peer, wpa_s->pending_join_iface_addr, ETH_ALEN) == 0)) {
2800 wpa_s->pending_pd_before_join = 0;
2801 wpa_printf(MSG_DEBUG, "P2P: Starting pending "
2802 "join-existing-group operation");
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08002803 wpas_p2p_join_start(wpa_s, 0, NULL, 0);
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08002804 return;
2805 }
2806
Dmitry Shmidt04949592012-07-19 12:16:46 -07002807 if (wpa_s->pending_pd_use == AUTO_PD_JOIN ||
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002808 wpa_s->pending_pd_use == AUTO_PD_GO_NEG) {
2809 int res;
2810
2811 res = os_snprintf(params, sizeof(params), " peer_go=%d",
2812 wpa_s->pending_pd_use == AUTO_PD_JOIN);
2813 if (os_snprintf_error(sizeof(params), res))
2814 params[sizeof(params) - 1] = '\0';
2815 } else
Dmitry Shmidt04949592012-07-19 12:16:46 -07002816 params[0] = '\0';
2817
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002818 if (config_methods & WPS_CONFIG_DISPLAY)
Dmitry Shmidt04949592012-07-19 12:16:46 -07002819 wpas_prov_disc_local_keypad(wpa_s, peer, params);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002820 else if (config_methods & WPS_CONFIG_KEYPAD) {
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08002821 if (wps_generate_pin(&generated_pin) < 0) {
2822 wpa_printf(MSG_DEBUG, "P2P: Could not generate PIN");
2823 wpas_notify_p2p_provision_discovery(
2824 wpa_s, peer, 0 /* response */,
2825 P2P_PROV_DISC_INFO_UNAVAILABLE, 0, 0);
2826 return;
2827 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07002828 wpas_prov_disc_local_display(wpa_s, peer, params,
2829 generated_pin);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002830 } else if (config_methods & WPS_CONFIG_PUSHBUTTON)
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07002831 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_PBC_RESP
2832 MACSTR "%s", MAC2STR(peer), params);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002833
Jouni Malinen75ecf522011-06-27 15:19:46 -07002834 wpas_notify_p2p_provision_discovery(wpa_s, peer, 0 /* response */,
2835 P2P_PROV_DISC_SUCCESS,
2836 config_methods, generated_pin);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002837}
2838
2839
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002840static void wpas_prov_disc_fail(void *ctx, const u8 *peer,
Dmitry Shmidt216983b2015-02-06 10:50:36 -08002841 enum p2p_prov_disc_status status,
2842 u32 adv_id, const u8 *adv_mac,
2843 const char *deferred_session_resp)
Jouni Malinen75ecf522011-06-27 15:19:46 -07002844{
2845 struct wpa_supplicant *wpa_s = ctx;
2846
Dmitry Shmidt04949592012-07-19 12:16:46 -07002847 if (wpa_s->p2p_fallback_to_go_neg) {
2848 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: PD for p2p_connect-auto "
2849 "failed - fall back to GO Negotiation");
Dmitry Shmidt9c175262016-03-03 10:20:07 -08002850 wpa_msg_global(wpa_s->p2pdev, MSG_INFO,
Dmitry Shmidt7f656022015-02-25 14:36:37 -08002851 P2P_EVENT_FALLBACK_TO_GO_NEG
2852 "reason=PD-failed");
Dmitry Shmidt04949592012-07-19 12:16:46 -07002853 wpas_p2p_fallback_to_go_neg(wpa_s, 0);
2854 return;
2855 }
2856
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002857 if (status == P2P_PROV_DISC_TIMEOUT_JOIN) {
Dmitry Shmidt2b380482012-09-13 10:52:13 -07002858 wpa_s->pending_pd_before_join = 0;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002859 wpa_printf(MSG_DEBUG, "P2P: Starting pending "
2860 "join-existing-group operation (no ACK for PD "
2861 "Req attempts)");
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08002862 wpas_p2p_join_start(wpa_s, 0, NULL, 0);
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002863 return;
Dmitry Shmidt2b380482012-09-13 10:52:13 -07002864 }
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002865
Dmitry Shmidt216983b2015-02-06 10:50:36 -08002866 if (adv_id && adv_mac && deferred_session_resp) {
2867 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_FAILURE
2868 " p2p_dev_addr=" MACSTR " status=%d adv_id=%x"
2869 " deferred_session_resp='%s'",
2870 MAC2STR(peer), status, adv_id,
2871 deferred_session_resp);
2872 } else if (adv_id && adv_mac) {
2873 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_FAILURE
2874 " p2p_dev_addr=" MACSTR " status=%d adv_id=%x",
2875 MAC2STR(peer), status, adv_id);
2876 } else {
2877 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_FAILURE
2878 " p2p_dev_addr=" MACSTR " status=%d",
2879 MAC2STR(peer), status);
2880 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07002881
Jouni Malinen75ecf522011-06-27 15:19:46 -07002882 wpas_notify_p2p_provision_discovery(wpa_s, peer, 0 /* response */,
2883 status, 0, 0);
2884}
2885
2886
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002887static int freq_included(struct wpa_supplicant *wpa_s,
2888 const struct p2p_channels *channels,
2889 unsigned int freq)
Dmitry Shmidtf7e0a992013-05-23 11:03:10 -07002890{
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002891 if ((channels == NULL || p2p_channels_includes_freq(channels, freq)) &&
2892 wpas_p2p_go_is_peer_freq(wpa_s, freq))
2893 return 1;
2894 return 0;
2895}
Dmitry Shmidtf7e0a992013-05-23 11:03:10 -07002896
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002897
2898static void wpas_p2p_go_update_common_freqs(struct wpa_supplicant *wpa_s)
2899{
2900 unsigned int num = P2P_MAX_CHANNELS;
2901 int *common_freqs;
2902 int ret;
2903
2904 p2p_go_dump_common_freqs(wpa_s);
2905 common_freqs = os_calloc(num, sizeof(int));
2906 if (!common_freqs)
2907 return;
2908
2909 ret = p2p_group_get_common_freqs(wpa_s->p2p_group, common_freqs, &num);
2910 if (ret < 0) {
2911 wpa_dbg(wpa_s, MSG_DEBUG,
2912 "P2P: Failed to get group common freqs");
2913 os_free(common_freqs);
2914 return;
2915 }
2916
2917 os_free(wpa_s->p2p_group_common_freqs);
2918 wpa_s->p2p_group_common_freqs = common_freqs;
2919 wpa_s->p2p_group_common_freqs_num = num;
2920 p2p_go_dump_common_freqs(wpa_s);
2921}
2922
2923
2924/*
2925 * Check if the given frequency is one of the possible operating frequencies
2926 * set after the completion of the GO Negotiation.
2927 */
2928static int wpas_p2p_go_is_peer_freq(struct wpa_supplicant *wpa_s, int freq)
2929{
2930 unsigned int i;
2931
2932 p2p_go_dump_common_freqs(wpa_s);
2933
2934 /* assume no restrictions */
2935 if (!wpa_s->p2p_group_common_freqs_num)
2936 return 1;
2937
2938 for (i = 0; i < wpa_s->p2p_group_common_freqs_num; i++) {
2939 if (wpa_s->p2p_group_common_freqs[i] == freq)
2940 return 1;
2941 }
2942 return 0;
2943}
2944
2945
2946static int wpas_sta_check_ecsa(struct hostapd_data *hapd,
2947 struct sta_info *sta, void *ctx)
2948{
2949 int *ecsa_support = ctx;
2950
2951 *ecsa_support &= sta->ecsa_supported;
2952
2953 return 0;
2954}
2955
2956
2957/* Check if all the peers support eCSA */
2958static int wpas_p2p_go_clients_support_ecsa(struct wpa_supplicant *wpa_s)
2959{
2960 int ecsa_support = 1;
2961
2962 ap_for_each_sta(wpa_s->ap_iface->bss[0], wpas_sta_check_ecsa,
2963 &ecsa_support);
2964
2965 return ecsa_support;
Dmitry Shmidtf7e0a992013-05-23 11:03:10 -07002966}
2967
2968
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07002969/**
2970 * Pick the best frequency to use from all the currently used frequencies.
2971 */
2972static int wpas_p2p_pick_best_used_freq(struct wpa_supplicant *wpa_s,
2973 struct wpa_used_freq_data *freqs,
2974 unsigned int num)
2975{
2976 unsigned int i, c;
2977
2978 /* find a candidate freq that is supported by P2P */
2979 for (c = 0; c < num; c++)
2980 if (p2p_supported_freq(wpa_s->global->p2p, freqs[c].freq))
2981 break;
2982
2983 if (c == num)
2984 return 0;
2985
2986 /* once we have a candidate, try to find a 'better' one */
2987 for (i = c + 1; i < num; i++) {
2988 if (!p2p_supported_freq(wpa_s->global->p2p, freqs[i].freq))
2989 continue;
2990
2991 /*
2992 * 1. Infrastructure station interfaces have higher preference.
2993 * 2. P2P Clients have higher preference.
2994 * 3. All others.
2995 */
2996 if (freqs[i].flags & WPA_FREQ_USED_BY_INFRA_STATION) {
2997 c = i;
2998 break;
2999 }
3000
3001 if ((freqs[i].flags & WPA_FREQ_USED_BY_P2P_CLIENT))
3002 c = i;
3003 }
3004 return freqs[c].freq;
3005}
3006
3007
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003008static u8 wpas_invitation_process(void *ctx, const u8 *sa, const u8 *bssid,
3009 const u8 *go_dev_addr, const u8 *ssid,
3010 size_t ssid_len, int *go, u8 *group_bssid,
Dmitry Shmidtf7e0a992013-05-23 11:03:10 -07003011 int *force_freq, int persistent_group,
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08003012 const struct p2p_channels *channels,
3013 int dev_pw_id)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003014{
3015 struct wpa_supplicant *wpa_s = ctx;
3016 struct wpa_ssid *s;
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07003017 struct wpa_used_freq_data *freqs;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003018 struct wpa_supplicant *grp;
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07003019 int best_freq;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003020
3021 if (!persistent_group) {
3022 wpa_printf(MSG_DEBUG, "P2P: Invitation from " MACSTR
Dmitry Shmidt344abd32014-01-14 13:17:00 -08003023 " to join an active group (SSID: %s)",
3024 MAC2STR(sa), wpa_ssid_txt(ssid, ssid_len));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003025 if (!is_zero_ether_addr(wpa_s->p2p_auth_invite) &&
3026 (os_memcmp(go_dev_addr, wpa_s->p2p_auth_invite, ETH_ALEN)
3027 == 0 ||
3028 os_memcmp(sa, wpa_s->p2p_auth_invite, ETH_ALEN) == 0)) {
3029 wpa_printf(MSG_DEBUG, "P2P: Accept previously "
3030 "authorized invitation");
3031 goto accept_inv;
3032 }
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08003033
3034#ifdef CONFIG_WPS_NFC
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08003035 if (dev_pw_id >= 0 && wpa_s->p2p_nfc_tag_enabled &&
3036 dev_pw_id == wpa_s->p2p_oob_dev_pw_id) {
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08003037 wpa_printf(MSG_DEBUG, "P2P: Accept invitation based on local enabled NFC Tag");
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08003038 wpa_s->p2p_wps_method = WPS_NFC;
3039 wpa_s->pending_join_wps_method = WPS_NFC;
3040 os_memcpy(wpa_s->pending_join_dev_addr,
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08003041 go_dev_addr, ETH_ALEN);
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08003042 os_memcpy(wpa_s->pending_join_iface_addr,
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08003043 bssid, ETH_ALEN);
3044 goto accept_inv;
3045 }
3046#endif /* CONFIG_WPS_NFC */
3047
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003048 /*
3049 * Do not accept the invitation automatically; notify user and
3050 * request approval.
3051 */
3052 return P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE;
3053 }
3054
3055 grp = wpas_get_p2p_group(wpa_s, ssid, ssid_len, go);
3056 if (grp) {
3057 wpa_printf(MSG_DEBUG, "P2P: Accept invitation to already "
3058 "running persistent group");
3059 if (*go)
3060 os_memcpy(group_bssid, grp->own_addr, ETH_ALEN);
3061 goto accept_inv;
3062 }
3063
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08003064 if (!is_zero_ether_addr(wpa_s->p2p_auth_invite) &&
3065 os_memcmp(sa, wpa_s->p2p_auth_invite, ETH_ALEN) == 0) {
3066 wpa_printf(MSG_DEBUG, "P2P: Accept previously initiated "
3067 "invitation to re-invoke a persistent group");
Dmitry Shmidt76cd2cc2014-05-27 12:56:04 -07003068 os_memset(wpa_s->p2p_auth_invite, 0, ETH_ALEN);
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08003069 } else if (!wpa_s->conf->persistent_reconnect)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003070 return P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE;
3071
3072 for (s = wpa_s->conf->ssid; s; s = s->next) {
3073 if (s->disabled == 2 &&
3074 os_memcmp(s->bssid, go_dev_addr, ETH_ALEN) == 0 &&
3075 s->ssid_len == ssid_len &&
3076 os_memcmp(ssid, s->ssid, ssid_len) == 0)
3077 break;
3078 }
3079
3080 if (!s) {
3081 wpa_printf(MSG_DEBUG, "P2P: Invitation from " MACSTR
3082 " requested reinvocation of an unknown group",
3083 MAC2STR(sa));
3084 return P2P_SC_FAIL_UNKNOWN_GROUP;
3085 }
3086
3087 if (s->mode == WPAS_MODE_P2P_GO && !wpas_p2p_create_iface(wpa_s)) {
3088 *go = 1;
3089 if (wpa_s->wpa_state >= WPA_AUTHENTICATING) {
3090 wpa_printf(MSG_DEBUG, "P2P: The only available "
3091 "interface is already in use - reject "
3092 "invitation");
3093 return P2P_SC_FAIL_UNABLE_TO_ACCOMMODATE;
3094 }
Dmitry Shmidt9c175262016-03-03 10:20:07 -08003095 if (wpa_s->p2p_mgmt)
3096 os_memcpy(group_bssid, wpa_s->parent->own_addr,
3097 ETH_ALEN);
3098 else
3099 os_memcpy(group_bssid, wpa_s->own_addr, ETH_ALEN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003100 } else if (s->mode == WPAS_MODE_P2P_GO) {
3101 *go = 1;
3102 if (wpas_p2p_add_group_interface(wpa_s, WPA_IF_P2P_GO) < 0)
3103 {
3104 wpa_printf(MSG_ERROR, "P2P: Failed to allocate a new "
3105 "interface address for the group");
3106 return P2P_SC_FAIL_UNABLE_TO_ACCOMMODATE;
3107 }
3108 os_memcpy(group_bssid, wpa_s->pending_interface_addr,
3109 ETH_ALEN);
3110 }
3111
3112accept_inv:
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003113 wpas_p2p_set_own_freq_preference(wpa_s, 0);
3114
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07003115 best_freq = 0;
3116 freqs = os_calloc(wpa_s->num_multichan_concurrent,
3117 sizeof(struct wpa_used_freq_data));
3118 if (freqs) {
3119 int num_channels = wpa_s->num_multichan_concurrent;
3120 int num = wpas_p2p_valid_oper_freqs(wpa_s, freqs, num_channels);
3121 best_freq = wpas_p2p_pick_best_used_freq(wpa_s, freqs, num);
3122 os_free(freqs);
3123 }
3124
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07003125 /* Get one of the frequencies currently in use */
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07003126 if (best_freq > 0) {
Dmitry Shmidta0d265f2013-11-19 13:13:41 -08003127 wpa_printf(MSG_DEBUG, "P2P: Trying to prefer a channel already used by one of the interfaces");
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07003128 wpas_p2p_set_own_freq_preference(wpa_s, best_freq);
Dmitry Shmidta0d265f2013-11-19 13:13:41 -08003129
3130 if (wpa_s->num_multichan_concurrent < 2 ||
3131 wpas_p2p_num_unused_channels(wpa_s) < 1) {
3132 wpa_printf(MSG_DEBUG, "P2P: No extra channels available - trying to force channel to match a channel already used by one of the interfaces");
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07003133 *force_freq = best_freq;
Dmitry Shmidta0d265f2013-11-19 13:13:41 -08003134 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003135 }
3136
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07003137 if (*force_freq > 0 && wpa_s->num_multichan_concurrent > 1 &&
3138 wpas_p2p_num_unused_channels(wpa_s) > 0) {
Dmitry Shmidtf7e0a992013-05-23 11:03:10 -07003139 if (*go == 0) {
3140 /* We are the client */
3141 wpa_printf(MSG_DEBUG, "P2P: Peer was found to be "
3142 "running a GO but we are capable of MCC, "
3143 "figure out the best channel to use");
3144 *force_freq = 0;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003145 } else if (!freq_included(wpa_s, channels, *force_freq)) {
Dmitry Shmidtf7e0a992013-05-23 11:03:10 -07003146 /* We are the GO, and *force_freq is not in the
3147 * intersection */
3148 wpa_printf(MSG_DEBUG, "P2P: Forced GO freq %d MHz not "
3149 "in intersection but we are capable of MCC, "
3150 "figure out the best channel to use",
3151 *force_freq);
3152 *force_freq = 0;
3153 }
3154 }
3155
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003156 return P2P_SC_SUCCESS;
3157}
3158
3159
3160static void wpas_invitation_received(void *ctx, const u8 *sa, const u8 *bssid,
3161 const u8 *ssid, size_t ssid_len,
3162 const u8 *go_dev_addr, u8 status,
3163 int op_freq)
3164{
3165 struct wpa_supplicant *wpa_s = ctx;
3166 struct wpa_ssid *s;
3167
3168 for (s = wpa_s->conf->ssid; s; s = s->next) {
3169 if (s->disabled == 2 &&
3170 s->ssid_len == ssid_len &&
3171 os_memcmp(ssid, s->ssid, ssid_len) == 0)
3172 break;
3173 }
3174
3175 if (status == P2P_SC_SUCCESS) {
3176 wpa_printf(MSG_DEBUG, "P2P: Invitation from peer " MACSTR
Dmitry Shmidt344abd32014-01-14 13:17:00 -08003177 " was accepted; op_freq=%d MHz, SSID=%s",
3178 MAC2STR(sa), op_freq, wpa_ssid_txt(ssid, ssid_len));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003179 if (s) {
Dmitry Shmidt91c40cd2012-09-25 14:23:53 -07003180 int go = s->mode == WPAS_MODE_P2P_GO;
Dmitry Shmidt849734c2016-05-27 09:59:01 -07003181 if (go) {
3182 wpa_msg_global(wpa_s, MSG_INFO,
3183 P2P_EVENT_INVITATION_ACCEPTED
3184 "sa=" MACSTR
3185 " persistent=%d freq=%d",
3186 MAC2STR(sa), s->id, op_freq);
3187 } else {
3188 wpa_msg_global(wpa_s, MSG_INFO,
3189 P2P_EVENT_INVITATION_ACCEPTED
3190 "sa=" MACSTR
3191 " persistent=%d",
3192 MAC2STR(sa), s->id);
3193 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003194 wpas_p2p_group_add_persistent(
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08003195 wpa_s, s, go, 0, op_freq, 0, 0, 0, 0, 0,
3196 0, NULL,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003197 go ? P2P_MAX_INITIAL_CONN_WAIT_GO_REINVOKE : 0,
3198 1);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003199 } else if (bssid) {
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08003200 wpa_s->user_initiated_pd = 0;
Dmitry Shmidt849734c2016-05-27 09:59:01 -07003201 wpa_msg_global(wpa_s, MSG_INFO,
3202 P2P_EVENT_INVITATION_ACCEPTED
3203 "sa=" MACSTR " go_dev_addr=" MACSTR
3204 " bssid=" MACSTR " unknown-network",
3205 MAC2STR(sa), MAC2STR(go_dev_addr),
3206 MAC2STR(bssid));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003207 wpas_p2p_join(wpa_s, bssid, go_dev_addr,
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08003208 wpa_s->p2p_wps_method, 0, op_freq,
Dmitry Shmidt344abd32014-01-14 13:17:00 -08003209 ssid, ssid_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003210 }
3211 return;
3212 }
3213
3214 if (status != P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE) {
3215 wpa_printf(MSG_DEBUG, "P2P: Invitation from peer " MACSTR
3216 " was rejected (status %u)", MAC2STR(sa), status);
3217 return;
3218 }
3219
3220 if (!s) {
3221 if (bssid) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07003222 wpa_msg_global(wpa_s, MSG_INFO,
3223 P2P_EVENT_INVITATION_RECEIVED
3224 "sa=" MACSTR " go_dev_addr=" MACSTR
3225 " bssid=" MACSTR " unknown-network",
3226 MAC2STR(sa), MAC2STR(go_dev_addr),
3227 MAC2STR(bssid));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003228 } else {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07003229 wpa_msg_global(wpa_s, MSG_INFO,
3230 P2P_EVENT_INVITATION_RECEIVED
3231 "sa=" MACSTR " go_dev_addr=" MACSTR
3232 " unknown-network",
3233 MAC2STR(sa), MAC2STR(go_dev_addr));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003234 }
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003235 wpas_notify_p2p_invitation_received(wpa_s, sa, go_dev_addr,
3236 bssid, 0, op_freq);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003237 return;
3238 }
3239
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08003240 if (s->mode == WPAS_MODE_P2P_GO && op_freq) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07003241 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_INVITATION_RECEIVED
3242 "sa=" MACSTR " persistent=%d freq=%d",
3243 MAC2STR(sa), s->id, op_freq);
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08003244 } else {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07003245 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_INVITATION_RECEIVED
3246 "sa=" MACSTR " persistent=%d",
3247 MAC2STR(sa), s->id);
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08003248 }
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003249 wpas_notify_p2p_invitation_received(wpa_s, sa, go_dev_addr, bssid,
3250 s->id, op_freq);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003251}
3252
3253
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003254static void wpas_remove_persistent_peer(struct wpa_supplicant *wpa_s,
3255 struct wpa_ssid *ssid,
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07003256 const u8 *peer, int inv)
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003257{
3258 size_t i;
Dmitry Shmidt8bd70b72015-05-26 16:02:19 -07003259 struct wpa_supplicant *p2p_wpa_s = wpa_s->global->p2p_init_wpa_s;
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003260
3261 if (ssid == NULL)
3262 return;
3263
3264 for (i = 0; ssid->p2p_client_list && i < ssid->num_p2p_clients; i++) {
Dmitry Shmidtff787d52015-01-12 13:01:47 -08003265 if (os_memcmp(ssid->p2p_client_list + i * 2 * ETH_ALEN, peer,
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003266 ETH_ALEN) == 0)
3267 break;
3268 }
Dmitry Shmidt6aa8ae42014-07-07 09:31:33 -07003269 if (i >= ssid->num_p2p_clients || !ssid->p2p_client_list) {
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003270 if (ssid->mode != WPAS_MODE_P2P_GO &&
3271 os_memcmp(ssid->bssid, peer, ETH_ALEN) == 0) {
3272 wpa_printf(MSG_DEBUG, "P2P: Remove persistent group %d "
3273 "due to invitation result", ssid->id);
3274 wpas_notify_network_removed(wpa_s, ssid);
3275 wpa_config_remove_network(wpa_s->conf, ssid->id);
3276 return;
3277 }
3278 return; /* Peer not found in client list */
3279 }
3280
3281 wpa_printf(MSG_DEBUG, "P2P: Remove peer " MACSTR " from persistent "
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07003282 "group %d client list%s",
3283 MAC2STR(peer), ssid->id,
3284 inv ? " due to invitation result" : "");
Dmitry Shmidtff787d52015-01-12 13:01:47 -08003285 os_memmove(ssid->p2p_client_list + i * 2 * ETH_ALEN,
3286 ssid->p2p_client_list + (i + 1) * 2 * ETH_ALEN,
3287 (ssid->num_p2p_clients - i - 1) * 2 * ETH_ALEN);
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003288 ssid->num_p2p_clients--;
Dmitry Shmidt8bd70b72015-05-26 16:02:19 -07003289 if (p2p_wpa_s->conf->update_config &&
3290 wpa_config_write(p2p_wpa_s->confname, p2p_wpa_s->conf))
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003291 wpa_printf(MSG_DEBUG, "P2P: Failed to update configuration");
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003292}
3293
3294
3295static void wpas_remove_persistent_client(struct wpa_supplicant *wpa_s,
3296 const u8 *peer)
3297{
3298 struct wpa_ssid *ssid;
3299
3300 wpa_s = wpa_s->global->p2p_invite_group;
3301 if (wpa_s == NULL)
3302 return; /* No known invitation group */
3303 ssid = wpa_s->current_ssid;
3304 if (ssid == NULL || ssid->mode != WPAS_MODE_P2P_GO ||
3305 !ssid->p2p_persistent_group)
3306 return; /* Not operating as a GO in persistent group */
Dmitry Shmidt9c175262016-03-03 10:20:07 -08003307 ssid = wpas_p2p_get_persistent(wpa_s->p2pdev, peer,
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003308 ssid->ssid, ssid->ssid_len);
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07003309 wpas_remove_persistent_peer(wpa_s, ssid, peer, 1);
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003310}
3311
3312
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08003313static void wpas_invitation_result(void *ctx, int status, const u8 *bssid,
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003314 const struct p2p_channels *channels,
Dmitry Shmidt15907092014-03-25 10:42:57 -07003315 const u8 *peer, int neg_freq,
3316 int peer_oper_freq)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003317{
3318 struct wpa_supplicant *wpa_s = ctx;
3319 struct wpa_ssid *ssid;
Dmitry Shmidt15907092014-03-25 10:42:57 -07003320 int freq;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003321
3322 if (bssid) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07003323 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_INVITATION_RESULT
3324 "status=%d " MACSTR,
3325 status, MAC2STR(bssid));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003326 } else {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07003327 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_INVITATION_RESULT
3328 "status=%d ", status);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003329 }
3330 wpas_notify_p2p_invitation_result(wpa_s, status, bssid);
3331
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003332 wpa_printf(MSG_DEBUG, "P2P: Invitation result - status=%d peer=" MACSTR,
3333 status, MAC2STR(peer));
3334 if (wpa_s->pending_invite_ssid_id == -1) {
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08003335 struct wpa_supplicant *group_if =
3336 wpa_s->global->p2p_invite_group;
3337
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003338 if (status == P2P_SC_FAIL_UNKNOWN_GROUP)
3339 wpas_remove_persistent_client(wpa_s, peer);
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08003340
3341 /*
3342 * Invitation to an active group. If this is successful and we
3343 * are the GO, set the client wait to postpone some concurrent
3344 * operations and to allow provisioning and connection to happen
3345 * more quickly.
3346 */
3347 if (status == P2P_SC_SUCCESS &&
3348 group_if && group_if->current_ssid &&
3349 group_if->current_ssid->mode == WPAS_MODE_P2P_GO) {
3350 os_get_reltime(&wpa_s->global->p2p_go_wait_client);
3351#ifdef CONFIG_TESTING_OPTIONS
3352 if (group_if->p2p_go_csa_on_inv) {
3353 wpa_printf(MSG_DEBUG,
3354 "Testing: force P2P GO CSA after invitation");
3355 eloop_cancel_timeout(
3356 wpas_p2p_reconsider_moving_go,
3357 wpa_s, NULL);
3358 eloop_register_timeout(
3359 0, 50000,
3360 wpas_p2p_reconsider_moving_go,
3361 wpa_s, NULL);
3362 }
3363#endif /* CONFIG_TESTING_OPTIONS */
3364 }
3365 return;
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003366 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003367
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08003368 if (status == P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE) {
3369 wpa_printf(MSG_DEBUG, "P2P: Waiting for peer to start another "
3370 "invitation exchange to indicate readiness for "
3371 "re-invocation");
3372 }
3373
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003374 if (status != P2P_SC_SUCCESS) {
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003375 if (status == P2P_SC_FAIL_UNKNOWN_GROUP) {
3376 ssid = wpa_config_get_network(
3377 wpa_s->conf, wpa_s->pending_invite_ssid_id);
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07003378 wpas_remove_persistent_peer(wpa_s, ssid, peer, 1);
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003379 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003380 wpas_p2p_remove_pending_group_interface(wpa_s);
3381 return;
3382 }
3383
3384 ssid = wpa_config_get_network(wpa_s->conf,
3385 wpa_s->pending_invite_ssid_id);
3386 if (ssid == NULL) {
3387 wpa_printf(MSG_ERROR, "P2P: Could not find persistent group "
3388 "data matching with invitation");
3389 return;
3390 }
3391
Jouni Malinen2c5b17d2012-10-13 21:52:46 -07003392 /*
3393 * The peer could have missed our ctrl::ack frame for Invitation
3394 * Response and continue retransmitting the frame. To reduce the
3395 * likelihood of the peer not getting successful TX status for the
3396 * Invitation Response frame, wait a short time here before starting
3397 * the persistent group so that we will remain on the current channel to
3398 * acknowledge any possible retransmission from the peer.
3399 */
3400 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: 50 ms wait on current channel before "
3401 "starting persistent group");
3402 os_sleep(0, 50000);
3403
Dmitry Shmidt15907092014-03-25 10:42:57 -07003404 if (neg_freq > 0 && ssid->mode == WPAS_MODE_P2P_GO &&
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003405 freq_included(wpa_s, channels, neg_freq))
Dmitry Shmidt15907092014-03-25 10:42:57 -07003406 freq = neg_freq;
3407 else if (peer_oper_freq > 0 && ssid->mode != WPAS_MODE_P2P_GO &&
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003408 freq_included(wpa_s, channels, peer_oper_freq))
Dmitry Shmidt15907092014-03-25 10:42:57 -07003409 freq = peer_oper_freq;
3410 else
3411 freq = 0;
3412
3413 wpa_printf(MSG_DEBUG, "P2P: Persistent group invitation success - op_freq=%d MHz SSID=%s",
3414 freq, wpa_ssid_txt(ssid->ssid, ssid->ssid_len));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003415 wpas_p2p_group_add_persistent(wpa_s, ssid,
Jouni Malinen31be0a42012-08-31 21:20:51 +03003416 ssid->mode == WPAS_MODE_P2P_GO,
Dmitry Shmidt96be6222014-02-13 10:16:51 -08003417 wpa_s->p2p_persistent_go_freq,
Dmitry Shmidt15907092014-03-25 10:42:57 -07003418 freq,
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08003419 wpa_s->p2p_go_vht_center_freq2,
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003420 wpa_s->p2p_go_ht40, wpa_s->p2p_go_vht,
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08003421 wpa_s->p2p_go_max_oper_chwidth,
Hai Shalom74f70d42019-02-11 14:42:39 -08003422 wpa_s->p2p_go_he,
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08003423 wpa_s->p2p_go_edmg,
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003424 channels,
Dmitry Shmidt56052862013-10-04 10:23:25 -07003425 ssid->mode == WPAS_MODE_P2P_GO ?
3426 P2P_MAX_INITIAL_CONN_WAIT_GO_REINVOKE :
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003427 0, 1);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003428}
3429
3430
Dmitry Shmidt04949592012-07-19 12:16:46 -07003431static int wpas_p2p_disallowed_freq(struct wpa_global *global,
3432 unsigned int freq)
3433{
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08003434 if (freq_range_list_includes(&global->p2p_go_avoid_freq, freq))
3435 return 1;
Dmitry Shmidt4ce9c872013-10-24 11:08:13 -07003436 return freq_range_list_includes(&global->p2p_disallow_freq, freq);
Dmitry Shmidt04949592012-07-19 12:16:46 -07003437}
3438
3439
3440static void wpas_p2p_add_chan(struct p2p_reg_class *reg, u8 chan)
3441{
3442 reg->channel[reg->channels] = chan;
3443 reg->channels++;
3444}
3445
3446
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003447static int wpas_p2p_default_channels(struct wpa_supplicant *wpa_s,
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003448 struct p2p_channels *chan,
3449 struct p2p_channels *cli_chan)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003450{
3451 int i, cla = 0;
3452
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003453 wpa_s->global->p2p_24ghz_social_channels = 1;
3454
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003455 os_memset(cli_chan, 0, sizeof(*cli_chan));
3456
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003457 wpa_printf(MSG_DEBUG, "P2P: Enable operating classes for 2.4 GHz "
3458 "band");
3459
3460 /* Operating class 81 - 2.4 GHz band channels 1..13 */
3461 chan->reg_class[cla].reg_class = 81;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003462 chan->reg_class[cla].channels = 0;
3463 for (i = 0; i < 11; i++) {
3464 if (!wpas_p2p_disallowed_freq(wpa_s->global, 2412 + i * 5))
3465 wpas_p2p_add_chan(&chan->reg_class[cla], i + 1);
3466 }
3467 if (chan->reg_class[cla].channels)
3468 cla++;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003469
3470 wpa_printf(MSG_DEBUG, "P2P: Enable operating classes for lower 5 GHz "
3471 "band");
3472
3473 /* Operating class 115 - 5 GHz, channels 36-48 */
3474 chan->reg_class[cla].reg_class = 115;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003475 chan->reg_class[cla].channels = 0;
3476 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 36 * 5))
3477 wpas_p2p_add_chan(&chan->reg_class[cla], 36);
3478 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 40 * 5))
3479 wpas_p2p_add_chan(&chan->reg_class[cla], 40);
3480 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 44 * 5))
3481 wpas_p2p_add_chan(&chan->reg_class[cla], 44);
3482 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 48 * 5))
3483 wpas_p2p_add_chan(&chan->reg_class[cla], 48);
3484 if (chan->reg_class[cla].channels)
3485 cla++;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003486
3487 wpa_printf(MSG_DEBUG, "P2P: Enable operating classes for higher 5 GHz "
3488 "band");
3489
3490 /* Operating class 124 - 5 GHz, channels 149,153,157,161 */
3491 chan->reg_class[cla].reg_class = 124;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003492 chan->reg_class[cla].channels = 0;
3493 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 149 * 5))
3494 wpas_p2p_add_chan(&chan->reg_class[cla], 149);
3495 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 153 * 5))
3496 wpas_p2p_add_chan(&chan->reg_class[cla], 153);
3497 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 156 * 5))
3498 wpas_p2p_add_chan(&chan->reg_class[cla], 157);
3499 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 161 * 5))
3500 wpas_p2p_add_chan(&chan->reg_class[cla], 161);
3501 if (chan->reg_class[cla].channels)
3502 cla++;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003503
3504 chan->reg_classes = cla;
3505 return 0;
3506}
3507
3508
Dmitry Shmidt04949592012-07-19 12:16:46 -07003509static int has_channel(struct wpa_global *global,
3510 struct hostapd_hw_modes *mode, u8 chan, int *flags)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003511{
3512 int i;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003513 unsigned int freq;
3514
3515 freq = (mode->mode == HOSTAPD_MODE_IEEE80211A ? 5000 : 2407) +
3516 chan * 5;
3517 if (wpas_p2p_disallowed_freq(global, freq))
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003518 return NOT_ALLOWED;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003519
3520 for (i = 0; i < mode->num_channels; i++) {
3521 if (mode->channels[i].chan == chan) {
3522 if (flags)
3523 *flags = mode->channels[i].flag;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003524 if (mode->channels[i].flag &
3525 (HOSTAPD_CHAN_DISABLED |
3526 HOSTAPD_CHAN_RADAR))
3527 return NOT_ALLOWED;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003528 if (mode->channels[i].flag & HOSTAPD_CHAN_NO_IR)
3529 return NO_IR;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003530 return ALLOWED;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003531 }
3532 }
3533
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003534 return NOT_ALLOWED;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003535}
3536
3537
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003538static int wpas_p2p_get_center_80mhz(struct wpa_supplicant *wpa_s,
3539 struct hostapd_hw_modes *mode,
3540 u8 channel)
3541{
3542 u8 center_channels[] = { 42, 58, 106, 122, 138, 155 };
Dmitry Shmidtaca489e2016-09-28 15:44:14 -07003543 size_t i;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003544
3545 if (mode->mode != HOSTAPD_MODE_IEEE80211A)
3546 return 0;
3547
3548 for (i = 0; i < ARRAY_SIZE(center_channels); i++)
3549 /*
3550 * In 80 MHz, the bandwidth "spans" 12 channels (e.g., 36-48),
3551 * so the center channel is 6 channels away from the start/end.
3552 */
3553 if (channel >= center_channels[i] - 6 &&
3554 channel <= center_channels[i] + 6)
3555 return center_channels[i];
3556
3557 return 0;
3558}
3559
3560
3561static enum chan_allowed wpas_p2p_verify_80mhz(struct wpa_supplicant *wpa_s,
3562 struct hostapd_hw_modes *mode,
3563 u8 channel, u8 bw)
3564{
3565 u8 center_chan;
3566 int i, flags;
3567 enum chan_allowed res, ret = ALLOWED;
3568
3569 center_chan = wpas_p2p_get_center_80mhz(wpa_s, mode, channel);
3570 if (!center_chan)
3571 return NOT_ALLOWED;
3572 if (center_chan >= 58 && center_chan <= 138)
3573 return NOT_ALLOWED; /* Do not allow DFS channels for P2P */
3574
3575 /* check all the channels are available */
3576 for (i = 0; i < 4; i++) {
3577 int adj_chan = center_chan - 6 + i * 4;
3578
3579 res = has_channel(wpa_s->global, mode, adj_chan, &flags);
3580 if (res == NOT_ALLOWED)
3581 return NOT_ALLOWED;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003582 if (res == NO_IR)
3583 ret = NO_IR;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003584
3585 if (i == 0 && !(flags & HOSTAPD_CHAN_VHT_10_70))
3586 return NOT_ALLOWED;
3587 if (i == 1 && !(flags & HOSTAPD_CHAN_VHT_30_50))
3588 return NOT_ALLOWED;
3589 if (i == 2 && !(flags & HOSTAPD_CHAN_VHT_50_30))
3590 return NOT_ALLOWED;
3591 if (i == 3 && !(flags & HOSTAPD_CHAN_VHT_70_10))
3592 return NOT_ALLOWED;
3593 }
3594
3595 return ret;
3596}
3597
3598
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08003599static int wpas_p2p_get_center_160mhz(struct wpa_supplicant *wpa_s,
3600 struct hostapd_hw_modes *mode,
3601 u8 channel)
3602{
3603 u8 center_channels[] = { 50, 114 };
3604 unsigned int i;
3605
3606 if (mode->mode != HOSTAPD_MODE_IEEE80211A)
3607 return 0;
3608
3609 for (i = 0; i < ARRAY_SIZE(center_channels); i++)
3610 /*
3611 * In 160 MHz, the bandwidth "spans" 28 channels (e.g., 36-64),
3612 * so the center channel is 14 channels away from the start/end.
3613 */
3614 if (channel >= center_channels[i] - 14 &&
3615 channel <= center_channels[i] + 14)
3616 return center_channels[i];
3617
3618 return 0;
3619}
3620
3621
3622static enum chan_allowed wpas_p2p_verify_160mhz(struct wpa_supplicant *wpa_s,
3623 struct hostapd_hw_modes *mode,
3624 u8 channel, u8 bw)
3625{
3626 u8 center_chan;
3627 int i, flags;
3628 enum chan_allowed res, ret = ALLOWED;
3629
3630 center_chan = wpas_p2p_get_center_160mhz(wpa_s, mode, channel);
3631 if (!center_chan)
3632 return NOT_ALLOWED;
3633 /* VHT 160 MHz uses DFS channels in most countries. */
3634
3635 /* Check all the channels are available */
3636 for (i = 0; i < 8; i++) {
3637 int adj_chan = center_chan - 14 + i * 4;
3638
3639 res = has_channel(wpa_s->global, mode, adj_chan, &flags);
3640 if (res == NOT_ALLOWED)
3641 return NOT_ALLOWED;
3642
3643 if (res == NO_IR)
3644 ret = NO_IR;
3645
3646 if (i == 0 && !(flags & HOSTAPD_CHAN_VHT_10_150))
3647 return NOT_ALLOWED;
3648 if (i == 1 && !(flags & HOSTAPD_CHAN_VHT_30_130))
3649 return NOT_ALLOWED;
3650 if (i == 2 && !(flags & HOSTAPD_CHAN_VHT_50_110))
3651 return NOT_ALLOWED;
3652 if (i == 3 && !(flags & HOSTAPD_CHAN_VHT_70_90))
3653 return NOT_ALLOWED;
3654 if (i == 4 && !(flags & HOSTAPD_CHAN_VHT_90_70))
3655 return NOT_ALLOWED;
3656 if (i == 5 && !(flags & HOSTAPD_CHAN_VHT_110_50))
3657 return NOT_ALLOWED;
3658 if (i == 6 && !(flags & HOSTAPD_CHAN_VHT_130_30))
3659 return NOT_ALLOWED;
3660 if (i == 7 && !(flags & HOSTAPD_CHAN_VHT_150_10))
3661 return NOT_ALLOWED;
3662 }
3663
3664 return ret;
3665}
3666
3667
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08003668static enum chan_allowed wpas_p2p_verify_edmg(struct wpa_supplicant *wpa_s,
3669 struct hostapd_hw_modes *mode,
3670 u8 channel)
3671{
3672 struct ieee80211_edmg_config edmg;
3673
3674 hostapd_encode_edmg_chan(1, channel, 0, &edmg);
3675 if (edmg.channels && ieee802_edmg_is_allowed(mode->edmg, edmg))
3676 return ALLOWED;
3677
3678 return NOT_ALLOWED;
3679}
3680
3681
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003682static enum chan_allowed wpas_p2p_verify_channel(struct wpa_supplicant *wpa_s,
3683 struct hostapd_hw_modes *mode,
3684 u8 channel, u8 bw)
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003685{
Dmitry Shmidt96be6222014-02-13 10:16:51 -08003686 int flag = 0;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003687 enum chan_allowed res, res2;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003688
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003689 res2 = res = has_channel(wpa_s->global, mode, channel, &flag);
3690 if (bw == BW40MINUS) {
3691 if (!(flag & HOSTAPD_CHAN_HT40MINUS))
3692 return NOT_ALLOWED;
3693 res2 = has_channel(wpa_s->global, mode, channel - 4, NULL);
3694 } else if (bw == BW40PLUS) {
3695 if (!(flag & HOSTAPD_CHAN_HT40PLUS))
3696 return NOT_ALLOWED;
3697 res2 = has_channel(wpa_s->global, mode, channel + 4, NULL);
3698 } else if (bw == BW80) {
3699 res2 = wpas_p2p_verify_80mhz(wpa_s, mode, channel, bw);
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08003700 } else if (bw == BW160) {
3701 res2 = wpas_p2p_verify_160mhz(wpa_s, mode, channel, bw);
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08003702 } else if (bw == BW4320 || bw == BW6480 || bw == BW8640) {
3703 return wpas_p2p_verify_edmg(wpa_s, mode, channel);
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003704 }
3705
3706 if (res == NOT_ALLOWED || res2 == NOT_ALLOWED)
3707 return NOT_ALLOWED;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003708 if (res == NO_IR || res2 == NO_IR)
3709 return NO_IR;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003710 return res;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003711}
3712
3713
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003714static int wpas_p2p_setup_channels(struct wpa_supplicant *wpa_s,
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003715 struct p2p_channels *chan,
3716 struct p2p_channels *cli_chan)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003717{
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003718 struct hostapd_hw_modes *mode;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003719 int cla, op, cli_cla;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003720
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003721 if (wpa_s->hw.modes == NULL) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003722 wpa_printf(MSG_DEBUG, "P2P: Driver did not support fetching "
3723 "of all supported channels; assume dualband "
3724 "support");
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003725 return wpas_p2p_default_channels(wpa_s, chan, cli_chan);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003726 }
3727
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003728 cla = cli_cla = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003729
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08003730 for (op = 0; global_op_class[op].op_class; op++) {
3731 const struct oper_class_map *o = &global_op_class[op];
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003732 u8 ch;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003733 struct p2p_reg_class *reg = NULL, *cli_reg = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003734
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08003735 if (o->p2p == NO_P2P_SUPP)
3736 continue;
3737
Hai Shalomfdcde762020-04-02 11:19:20 -07003738 mode = get_mode(wpa_s->hw.modes, wpa_s->hw.num_modes, o->mode,
3739 is_6ghz_op_class(o->op_class));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003740 if (mode == NULL)
3741 continue;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003742 if (mode->mode == HOSTAPD_MODE_IEEE80211G)
3743 wpa_s->global->p2p_24ghz_social_channels = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003744 for (ch = o->min_chan; ch <= o->max_chan; ch += o->inc) {
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003745 enum chan_allowed res;
3746 res = wpas_p2p_verify_channel(wpa_s, mode, ch, o->bw);
3747 if (res == ALLOWED) {
3748 if (reg == NULL) {
Hai Shalomfdcde762020-04-02 11:19:20 -07003749 if (cla == P2P_MAX_REG_CLASSES)
3750 continue;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003751 wpa_printf(MSG_DEBUG, "P2P: Add operating class %u",
3752 o->op_class);
3753 reg = &chan->reg_class[cla];
3754 cla++;
3755 reg->reg_class = o->op_class;
3756 }
Hai Shalomfdcde762020-04-02 11:19:20 -07003757 if (reg->channels == P2P_MAX_REG_CLASS_CHANNELS)
3758 continue;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003759 reg->channel[reg->channels] = ch;
3760 reg->channels++;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003761 } else if (res == NO_IR &&
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003762 wpa_s->conf->p2p_add_cli_chan) {
3763 if (cli_reg == NULL) {
Hai Shalomfdcde762020-04-02 11:19:20 -07003764 if (cli_cla == P2P_MAX_REG_CLASSES)
3765 continue;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003766 wpa_printf(MSG_DEBUG, "P2P: Add operating class %u (client only)",
3767 o->op_class);
3768 cli_reg = &cli_chan->reg_class[cli_cla];
3769 cli_cla++;
3770 cli_reg->reg_class = o->op_class;
3771 }
Hai Shalomfdcde762020-04-02 11:19:20 -07003772 if (cli_reg->channels ==
3773 P2P_MAX_REG_CLASS_CHANNELS)
3774 continue;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003775 cli_reg->channel[cli_reg->channels] = ch;
3776 cli_reg->channels++;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003777 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003778 }
3779 if (reg) {
3780 wpa_hexdump(MSG_DEBUG, "P2P: Channels",
3781 reg->channel, reg->channels);
3782 }
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003783 if (cli_reg) {
3784 wpa_hexdump(MSG_DEBUG, "P2P: Channels (client only)",
3785 cli_reg->channel, cli_reg->channels);
3786 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003787 }
3788
3789 chan->reg_classes = cla;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003790 cli_chan->reg_classes = cli_cla;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003791
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003792 return 0;
3793}
3794
3795
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003796int wpas_p2p_get_ht40_mode(struct wpa_supplicant *wpa_s,
3797 struct hostapd_hw_modes *mode, u8 channel)
3798{
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003799 int op;
3800 enum chan_allowed ret;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003801
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08003802 for (op = 0; global_op_class[op].op_class; op++) {
3803 const struct oper_class_map *o = &global_op_class[op];
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003804 u8 ch;
3805
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08003806 if (o->p2p == NO_P2P_SUPP)
3807 continue;
3808
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003809 for (ch = o->min_chan; ch <= o->max_chan; ch += o->inc) {
3810 if (o->mode != HOSTAPD_MODE_IEEE80211A ||
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07003811 (o->bw != BW40PLUS && o->bw != BW40MINUS) ||
3812 ch != channel)
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003813 continue;
3814 ret = wpas_p2p_verify_channel(wpa_s, mode, ch, o->bw);
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003815 if (ret == ALLOWED)
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003816 return (o->bw == BW40MINUS) ? -1 : 1;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003817 }
3818 }
3819 return 0;
3820}
3821
3822
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003823int wpas_p2p_get_vht80_center(struct wpa_supplicant *wpa_s,
3824 struct hostapd_hw_modes *mode, u8 channel)
3825{
3826 if (!wpas_p2p_verify_channel(wpa_s, mode, channel, BW80))
3827 return 0;
3828
3829 return wpas_p2p_get_center_80mhz(wpa_s, mode, channel);
3830}
3831
3832
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08003833int wpas_p2p_get_vht160_center(struct wpa_supplicant *wpa_s,
3834 struct hostapd_hw_modes *mode, u8 channel)
3835{
3836 if (!wpas_p2p_verify_channel(wpa_s, mode, channel, BW160))
3837 return 0;
3838 return wpas_p2p_get_center_160mhz(wpa_s, mode, channel);
3839}
3840
3841
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003842static int wpas_get_noa(void *ctx, const u8 *interface_addr, u8 *buf,
3843 size_t buf_len)
3844{
3845 struct wpa_supplicant *wpa_s = ctx;
3846
3847 for (wpa_s = wpa_s->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
3848 if (os_memcmp(wpa_s->own_addr, interface_addr, ETH_ALEN) == 0)
3849 break;
3850 }
3851 if (wpa_s == NULL)
3852 return -1;
3853
3854 return wpa_drv_get_noa(wpa_s, buf, buf_len);
3855}
3856
3857
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07003858struct wpa_supplicant * wpas_get_p2p_go_iface(struct wpa_supplicant *wpa_s,
3859 const u8 *ssid, size_t ssid_len)
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08003860{
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07003861 for (wpa_s = wpa_s->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
3862 struct wpa_ssid *s = wpa_s->current_ssid;
3863 if (s == NULL)
3864 continue;
3865 if (s->mode != WPAS_MODE_P2P_GO &&
3866 s->mode != WPAS_MODE_AP &&
3867 s->mode != WPAS_MODE_P2P_GROUP_FORMATION)
3868 continue;
3869 if (s->ssid_len != ssid_len ||
Dmitry Shmidt03658832014-08-13 11:03:49 -07003870 os_memcmp(ssid, s->ssid, ssid_len) != 0)
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07003871 continue;
3872 return wpa_s;
3873 }
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08003874
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07003875 return NULL;
3876
3877}
3878
3879
3880struct wpa_supplicant * wpas_get_p2p_client_iface(struct wpa_supplicant *wpa_s,
3881 const u8 *peer_dev_addr)
3882{
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08003883 for (wpa_s = wpa_s->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
3884 struct wpa_ssid *ssid = wpa_s->current_ssid;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003885 if (ssid && (ssid->mode != WPAS_MODE_INFRA || !ssid->p2p_group))
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08003886 continue;
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07003887 if (os_memcmp(wpa_s->go_dev_addr, peer_dev_addr, ETH_ALEN) == 0)
3888 return wpa_s;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08003889 }
3890
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07003891 return NULL;
3892}
3893
3894
3895static int wpas_go_connected(void *ctx, const u8 *dev_addr)
3896{
3897 struct wpa_supplicant *wpa_s = ctx;
3898
3899 return wpas_get_p2p_client_iface(wpa_s, dev_addr) != NULL;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08003900}
3901
3902
Dmitry Shmidt18463232014-01-24 12:29:41 -08003903static int wpas_is_concurrent_session_active(void *ctx)
3904{
3905 struct wpa_supplicant *wpa_s = ctx;
3906 struct wpa_supplicant *ifs;
3907
3908 for (ifs = wpa_s->global->ifaces; ifs; ifs = ifs->next) {
3909 if (ifs == wpa_s)
3910 continue;
3911 if (ifs->wpa_state > WPA_ASSOCIATED)
3912 return 1;
3913 }
3914 return 0;
3915}
3916
3917
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07003918static void wpas_p2p_debug_print(void *ctx, int level, const char *msg)
3919{
3920 struct wpa_supplicant *wpa_s = ctx;
3921 wpa_msg_global(wpa_s, level, "P2P: %s", msg);
3922}
3923
3924
Dmitry Shmidt9ead16e2014-10-07 13:15:23 -07003925int wpas_p2p_add_p2pdev_interface(struct wpa_supplicant *wpa_s,
3926 const char *conf_p2p_dev)
Dmitry Shmidt34af3062013-07-11 10:46:32 -07003927{
3928 struct wpa_interface iface;
3929 struct wpa_supplicant *p2pdev_wpa_s;
3930 char ifname[100];
3931 char force_name[100];
3932 int ret;
3933
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003934 ret = os_snprintf(ifname, sizeof(ifname), P2P_MGMT_DEVICE_PREFIX "%s",
3935 wpa_s->ifname);
3936 if (os_snprintf_error(sizeof(ifname), ret))
3937 return -1;
Dmitry Shmidt34af3062013-07-11 10:46:32 -07003938 force_name[0] = '\0';
3939 wpa_s->pending_interface_type = WPA_IF_P2P_DEVICE;
3940 ret = wpa_drv_if_add(wpa_s, WPA_IF_P2P_DEVICE, ifname, NULL, NULL,
3941 force_name, wpa_s->pending_interface_addr, NULL);
3942 if (ret < 0) {
3943 wpa_printf(MSG_DEBUG, "P2P: Failed to create P2P Device interface");
3944 return ret;
3945 }
3946 os_strlcpy(wpa_s->pending_interface_name, ifname,
3947 sizeof(wpa_s->pending_interface_name));
3948
3949 os_memset(&iface, 0, sizeof(iface));
3950 iface.p2p_mgmt = 1;
3951 iface.ifname = wpa_s->pending_interface_name;
3952 iface.driver = wpa_s->driver->name;
3953 iface.driver_param = wpa_s->conf->driver_param;
Dmitry Shmidt2ac5f602014-03-07 10:08:21 -08003954
3955 /*
3956 * If a P2P Device configuration file was given, use it as the interface
3957 * configuration file (instead of using parent's configuration file.
3958 */
Dmitry Shmidt9ead16e2014-10-07 13:15:23 -07003959 if (conf_p2p_dev) {
3960 iface.confname = conf_p2p_dev;
Dmitry Shmidt2ac5f602014-03-07 10:08:21 -08003961 iface.ctrl_interface = NULL;
3962 } else {
3963 iface.confname = wpa_s->confname;
3964 iface.ctrl_interface = wpa_s->conf->ctrl_interface;
3965 }
Dmitry Shmidt2ac5f602014-03-07 10:08:21 -08003966
Dmitry Shmidt203eadb2015-03-05 14:16:04 -08003967 p2pdev_wpa_s = wpa_supplicant_add_iface(wpa_s->global, &iface, wpa_s);
Dmitry Shmidt34af3062013-07-11 10:46:32 -07003968 if (!p2pdev_wpa_s) {
3969 wpa_printf(MSG_DEBUG, "P2P: Failed to add P2P Device interface");
3970 return -1;
3971 }
Dmitry Shmidt34af3062013-07-11 10:46:32 -07003972
Dmitry Shmidt9c175262016-03-03 10:20:07 -08003973 p2pdev_wpa_s->p2pdev = p2pdev_wpa_s;
Dmitry Shmidt34af3062013-07-11 10:46:32 -07003974 wpa_s->pending_interface_name[0] = '\0';
3975 return 0;
3976}
3977
3978
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08003979static void wpas_presence_resp(void *ctx, const u8 *src, u8 status,
3980 const u8 *noa, size_t noa_len)
3981{
3982 struct wpa_supplicant *wpa_s, *intf = ctx;
3983 char hex[100];
3984
3985 for (wpa_s = intf->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
3986 if (wpa_s->waiting_presence_resp)
3987 break;
3988 }
3989 if (!wpa_s) {
3990 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: No group interface was waiting for presence response");
3991 return;
3992 }
3993 wpa_s->waiting_presence_resp = 0;
3994
3995 wpa_snprintf_hex(hex, sizeof(hex), noa, noa_len);
3996 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_PRESENCE_RESPONSE "src=" MACSTR
3997 " status=%u noa=%s", MAC2STR(src), status, hex);
3998}
3999
4000
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004001static int wpas_get_persistent_group(void *ctx, const u8 *addr, const u8 *ssid,
4002 size_t ssid_len, u8 *go_dev_addr,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004003 u8 *ret_ssid, size_t *ret_ssid_len,
4004 u8 *intended_iface_addr)
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004005{
4006 struct wpa_supplicant *wpa_s = ctx;
4007 struct wpa_ssid *s;
4008
4009 s = wpas_p2p_get_persistent(wpa_s, addr, ssid, ssid_len);
4010 if (s) {
4011 os_memcpy(ret_ssid, s->ssid, s->ssid_len);
4012 *ret_ssid_len = s->ssid_len;
4013 os_memcpy(go_dev_addr, s->bssid, ETH_ALEN);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004014
4015 if (s->mode != WPAS_MODE_P2P_GO) {
4016 os_memset(intended_iface_addr, 0, ETH_ALEN);
4017 } else if (wpas_p2p_create_iface(wpa_s)) {
4018 if (wpas_p2p_add_group_interface(wpa_s, WPA_IF_P2P_GO))
4019 return 0;
4020
4021 os_memcpy(intended_iface_addr,
4022 wpa_s->pending_interface_addr, ETH_ALEN);
4023 } else {
4024 os_memcpy(intended_iface_addr, wpa_s->own_addr,
4025 ETH_ALEN);
4026 }
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004027 return 1;
4028 }
4029
4030 return 0;
4031}
4032
4033
4034static int wpas_get_go_info(void *ctx, u8 *intended_addr,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004035 u8 *ssid, size_t *ssid_len, int *group_iface,
4036 unsigned int *freq)
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004037{
4038 struct wpa_supplicant *wpa_s = ctx;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004039 struct wpa_supplicant *go;
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004040 struct wpa_ssid *s;
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004041
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004042 /*
4043 * group_iface will be set to 1 only if a dedicated interface for P2P
4044 * role is required. First, we try to reuse an active GO. However,
4045 * if it is not present, we will try to reactivate an existing
4046 * persistent group and set group_iface to 1, so the caller will know
4047 * that the pending interface should be used.
4048 */
4049 *group_iface = 0;
4050
4051 if (freq)
4052 *freq = 0;
4053
4054 go = wpas_p2p_get_go_group(wpa_s);
4055 if (!go) {
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004056 s = wpas_p2p_get_persistent_go(wpa_s);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004057 *group_iface = wpas_p2p_create_iface(wpa_s);
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004058 if (s)
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004059 os_memcpy(intended_addr, s->bssid, ETH_ALEN);
4060 else
4061 return 0;
4062 } else {
4063 s = go->current_ssid;
4064 os_memcpy(intended_addr, go->own_addr, ETH_ALEN);
4065 if (freq)
4066 *freq = go->assoc_freq;
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004067 }
4068
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004069 os_memcpy(ssid, s->ssid, s->ssid_len);
4070 *ssid_len = s->ssid_len;
4071
4072 return 1;
4073}
4074
4075
4076static int wpas_remove_stale_groups(void *ctx, const u8 *peer, const u8 *go,
4077 const u8 *ssid, size_t ssid_len)
4078{
4079 struct wpa_supplicant *wpa_s = ctx;
4080 struct wpa_ssid *s;
4081 int save_config = 0;
4082 size_t i;
4083
4084 /* Start with our first choice of Persistent Groups */
4085 while ((s = wpas_p2p_get_persistent(wpa_s, peer, NULL, 0))) {
4086 if (go && ssid && ssid_len &&
4087 s->ssid_len == ssid_len &&
4088 os_memcmp(go, s->bssid, ETH_ALEN) == 0 &&
4089 os_memcmp(ssid, s->ssid, ssid_len) == 0)
4090 break;
4091
4092 /* Remove stale persistent group */
4093 if (s->mode != WPAS_MODE_P2P_GO || s->num_p2p_clients <= 1) {
Hai Shalom74f70d42019-02-11 14:42:39 -08004094 wpa_dbg(wpa_s, MSG_DEBUG,
4095 "P2P: Remove stale persistent group id=%d",
4096 s->id);
4097 wpas_notify_persistent_group_removed(wpa_s, s);
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004098 wpa_config_remove_network(wpa_s->conf, s->id);
4099 save_config = 1;
4100 continue;
4101 }
4102
4103 for (i = 0; i < s->num_p2p_clients; i++) {
4104 if (os_memcmp(s->p2p_client_list + i * 2 * ETH_ALEN,
4105 peer, ETH_ALEN) != 0)
4106 continue;
4107
4108 os_memmove(s->p2p_client_list + i * 2 * ETH_ALEN,
4109 s->p2p_client_list + (i + 1) * 2 * ETH_ALEN,
4110 (s->num_p2p_clients - i - 1) * 2 * ETH_ALEN);
4111 break;
4112 }
4113 s->num_p2p_clients--;
4114 save_config = 1;
4115 }
4116
4117 if (save_config)
4118 p2p_config_write(wpa_s);
4119
4120 /* Return TRUE if valid SSID remains */
4121 return s != NULL;
4122}
4123
4124
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004125static void wpas_p2ps_get_feat_cap_str(char *buf, size_t buf_len,
4126 const u8 *feat_cap, size_t feat_cap_len)
4127{
4128 static const char pref[] = " feature_cap=";
4129 int ret;
4130
4131 buf[0] = '\0';
4132
4133 /*
4134 * We expect a feature capability to contain at least one byte to be
4135 * reported. The string buffer provided by the caller function is
4136 * expected to be big enough to contain all bytes of the attribute for
4137 * known specifications. This function truncates the reported bytes if
4138 * the feature capability data exceeds the string buffer size.
4139 */
4140 if (!feat_cap || !feat_cap_len || buf_len < sizeof(pref) + 2)
4141 return;
4142
4143 os_memcpy(buf, pref, sizeof(pref));
4144 ret = wpa_snprintf_hex(&buf[sizeof(pref) - 1],
4145 buf_len - sizeof(pref) + 1,
4146 feat_cap, feat_cap_len);
4147
4148 if (ret != (2 * (int) feat_cap_len))
4149 wpa_printf(MSG_WARNING, "P2PS feature_cap bytes truncated");
4150}
4151
4152
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004153static void wpas_p2ps_prov_complete(void *ctx, u8 status, const u8 *dev,
4154 const u8 *adv_mac, const u8 *ses_mac,
4155 const u8 *grp_mac, u32 adv_id, u32 ses_id,
4156 u8 conncap, int passwd_id,
4157 const u8 *persist_ssid,
4158 size_t persist_ssid_size, int response_done,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004159 int prov_start, const char *session_info,
4160 const u8 *feat_cap, size_t feat_cap_len,
Dmitry Shmidtde47be72016-01-07 12:52:55 -08004161 unsigned int freq,
4162 const u8 *group_ssid, size_t group_ssid_len)
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004163{
4164 struct wpa_supplicant *wpa_s = ctx;
4165 u8 mac[ETH_ALEN];
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004166 struct wpa_ssid *persistent_go, *stale, *s = NULL;
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004167 int save_config = 0;
4168 struct wpa_supplicant *go_wpa_s;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004169 char feat_cap_str[256];
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004170
4171 if (!dev)
4172 return;
4173
4174 os_memset(mac, 0, ETH_ALEN);
4175 if (!adv_mac)
4176 adv_mac = mac;
4177 if (!ses_mac)
4178 ses_mac = mac;
4179 if (!grp_mac)
4180 grp_mac = mac;
4181
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004182 wpas_p2ps_get_feat_cap_str(feat_cap_str, sizeof(feat_cap_str),
4183 feat_cap, feat_cap_len);
4184
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004185 if (prov_start) {
4186 if (session_info == NULL) {
4187 wpa_msg_global(wpa_s, MSG_INFO,
4188 P2P_EVENT_P2PS_PROVISION_START MACSTR
4189 " adv_id=%x conncap=%x"
4190 " adv_mac=" MACSTR
4191 " session=%x mac=" MACSTR
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004192 " dev_passwd_id=%d%s",
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004193 MAC2STR(dev), adv_id, conncap,
4194 MAC2STR(adv_mac),
4195 ses_id, MAC2STR(ses_mac),
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004196 passwd_id, feat_cap_str);
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004197 } else {
4198 wpa_msg_global(wpa_s, MSG_INFO,
4199 P2P_EVENT_P2PS_PROVISION_START MACSTR
4200 " adv_id=%x conncap=%x"
4201 " adv_mac=" MACSTR
4202 " session=%x mac=" MACSTR
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004203 " dev_passwd_id=%d info='%s'%s",
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004204 MAC2STR(dev), adv_id, conncap,
4205 MAC2STR(adv_mac),
4206 ses_id, MAC2STR(ses_mac),
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004207 passwd_id, session_info, feat_cap_str);
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004208 }
4209 return;
4210 }
4211
4212 go_wpa_s = wpas_p2p_get_go_group(wpa_s);
4213 persistent_go = wpas_p2p_get_persistent_go(wpa_s);
4214
4215 if (status && status != P2P_SC_SUCCESS_DEFERRED) {
4216 if (go_wpa_s && !p2p_group_go_member_count(wpa_s))
4217 wpas_p2p_group_remove(wpa_s, go_wpa_s->ifname);
4218
4219 if (persistent_go && !persistent_go->num_p2p_clients) {
4220 /* remove empty persistent GO */
Hai Shalom74f70d42019-02-11 14:42:39 -08004221 wpa_dbg(wpa_s, MSG_DEBUG,
4222 "P2P: Remove empty persistent group id=%d",
4223 persistent_go->id);
4224 wpas_notify_persistent_group_removed(wpa_s,
4225 persistent_go);
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004226 wpa_config_remove_network(wpa_s->conf,
4227 persistent_go->id);
4228 }
4229
4230 wpa_msg_global(wpa_s, MSG_INFO,
4231 P2P_EVENT_P2PS_PROVISION_DONE MACSTR
4232 " status=%d"
4233 " adv_id=%x adv_mac=" MACSTR
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004234 " session=%x mac=" MACSTR "%s",
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004235 MAC2STR(dev), status,
4236 adv_id, MAC2STR(adv_mac),
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004237 ses_id, MAC2STR(ses_mac), feat_cap_str);
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004238 return;
4239 }
4240
4241 /* Clean up stale persistent groups with this device */
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004242 if (persist_ssid && persist_ssid_size)
4243 s = wpas_p2p_get_persistent(wpa_s, dev, persist_ssid,
4244 persist_ssid_size);
4245
4246 if (persist_ssid && s && s->mode != WPAS_MODE_P2P_GO &&
4247 is_zero_ether_addr(grp_mac)) {
4248 wpa_dbg(wpa_s, MSG_ERROR,
4249 "P2P: Peer device is a GO in a persistent group, but it did not provide the intended MAC address");
4250 return;
4251 }
4252
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004253 for (;;) {
4254 stale = wpas_p2p_get_persistent(wpa_s, dev, NULL, 0);
4255 if (!stale)
4256 break;
4257
4258 if (s && s->ssid_len == stale->ssid_len &&
4259 os_memcmp(stale->bssid, s->bssid, ETH_ALEN) == 0 &&
4260 os_memcmp(stale->ssid, s->ssid, s->ssid_len) == 0)
4261 break;
4262
4263 /* Remove stale persistent group */
4264 if (stale->mode != WPAS_MODE_P2P_GO ||
4265 stale->num_p2p_clients <= 1) {
Hai Shalom74f70d42019-02-11 14:42:39 -08004266 wpa_dbg(wpa_s, MSG_DEBUG,
4267 "P2P: Remove stale persistent group id=%d",
4268 stale->id);
4269 wpas_notify_persistent_group_removed(wpa_s, stale);
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004270 wpa_config_remove_network(wpa_s->conf, stale->id);
4271 } else {
4272 size_t i;
4273
4274 for (i = 0; i < stale->num_p2p_clients; i++) {
4275 if (os_memcmp(stale->p2p_client_list +
4276 i * ETH_ALEN,
4277 dev, ETH_ALEN) == 0) {
4278 os_memmove(stale->p2p_client_list +
4279 i * ETH_ALEN,
4280 stale->p2p_client_list +
4281 (i + 1) * ETH_ALEN,
4282 (stale->num_p2p_clients -
4283 i - 1) * ETH_ALEN);
4284 break;
4285 }
4286 }
4287 stale->num_p2p_clients--;
4288 }
4289 save_config = 1;
4290 }
4291
4292 if (save_config)
4293 p2p_config_write(wpa_s);
4294
4295 if (s) {
4296 if (go_wpa_s && !p2p_group_go_member_count(wpa_s))
4297 wpas_p2p_group_remove(wpa_s, go_wpa_s->ifname);
4298
4299 if (persistent_go && s != persistent_go &&
4300 !persistent_go->num_p2p_clients) {
4301 /* remove empty persistent GO */
Hai Shalom74f70d42019-02-11 14:42:39 -08004302 wpa_dbg(wpa_s, MSG_DEBUG,
4303 "P2P: Remove empty persistent group id=%d",
4304 persistent_go->id);
4305 wpas_notify_persistent_group_removed(wpa_s,
4306 persistent_go);
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004307 wpa_config_remove_network(wpa_s->conf,
4308 persistent_go->id);
4309 /* Save config */
4310 }
4311
4312 wpa_msg_global(wpa_s, MSG_INFO,
4313 P2P_EVENT_P2PS_PROVISION_DONE MACSTR
4314 " status=%d"
4315 " adv_id=%x adv_mac=" MACSTR
4316 " session=%x mac=" MACSTR
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004317 " persist=%d%s",
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004318 MAC2STR(dev), status,
4319 adv_id, MAC2STR(adv_mac),
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004320 ses_id, MAC2STR(ses_mac), s->id, feat_cap_str);
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004321 return;
4322 }
4323
Hai Shalom5f92bc92019-04-18 11:54:11 -07004324 wpa_s->global->pending_p2ps_group = 0;
4325 wpa_s->global->pending_p2ps_group_freq = 0;
4326
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004327 if (conncap == P2PS_SETUP_GROUP_OWNER) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004328 /*
4329 * We need to copy the interface name. Simply saving a
4330 * pointer isn't enough, since if we use pending_interface_name
4331 * it will be overwritten when the group is added.
4332 */
4333 char go_ifname[100];
4334
4335 go_ifname[0] = '\0';
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004336 if (!go_wpa_s) {
Hai Shalom5f92bc92019-04-18 11:54:11 -07004337 if (!response_done) {
4338 wpa_s->global->pending_p2ps_group = 1;
4339 wpa_s->global->pending_p2ps_group_freq = freq;
4340 }
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004341
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004342 if (!wpas_p2p_create_iface(wpa_s))
4343 os_memcpy(go_ifname, wpa_s->ifname,
4344 sizeof(go_ifname));
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004345 else if (wpa_s->pending_interface_name[0])
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004346 os_memcpy(go_ifname,
4347 wpa_s->pending_interface_name,
4348 sizeof(go_ifname));
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004349
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004350 if (!go_ifname[0]) {
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004351 wpas_p2ps_prov_complete(
4352 wpa_s, P2P_SC_FAIL_UNKNOWN_GROUP,
4353 dev, adv_mac, ses_mac,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004354 grp_mac, adv_id, ses_id, 0, 0,
Dmitry Shmidtde47be72016-01-07 12:52:55 -08004355 NULL, 0, 0, 0, NULL, NULL, 0, 0,
4356 NULL, 0);
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004357 return;
4358 }
4359
4360 /* If PD Resp complete, start up the GO */
4361 if (response_done && persistent_go) {
4362 wpas_p2p_group_add_persistent(
4363 wpa_s, persistent_go,
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08004364 0, 0, freq, 0, 0, 0, 0, 0, 0, NULL,
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004365 persistent_go->mode ==
4366 WPAS_MODE_P2P_GO ?
4367 P2P_MAX_INITIAL_CONN_WAIT_GO_REINVOKE :
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004368 0, 0);
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004369 } else if (response_done) {
Hai Shalom74f70d42019-02-11 14:42:39 -08004370 wpas_p2p_group_add(wpa_s, 1, freq,
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08004371 0, 0, 0, 0, 0, 0);
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004372 }
4373
4374 if (passwd_id == DEV_PW_P2PS_DEFAULT) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004375 os_memcpy(wpa_s->p2ps_join_addr, grp_mac,
4376 ETH_ALEN);
4377 wpa_s->p2ps_method_config_any = 1;
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004378 }
4379 } else if (passwd_id == DEV_PW_P2PS_DEFAULT) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004380 os_memcpy(go_ifname, go_wpa_s->ifname,
4381 sizeof(go_ifname));
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004382
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004383 if (is_zero_ether_addr(grp_mac)) {
4384 wpa_dbg(go_wpa_s, MSG_DEBUG,
4385 "P2P: Setting PIN-1 for ANY");
4386 wpa_supplicant_ap_wps_pin(go_wpa_s, NULL,
4387 "12345670", NULL, 0,
4388 0);
4389 } else {
4390 wpa_dbg(go_wpa_s, MSG_DEBUG,
4391 "P2P: Setting PIN-1 for " MACSTR,
4392 MAC2STR(grp_mac));
4393 wpa_supplicant_ap_wps_pin(go_wpa_s, grp_mac,
4394 "12345670", NULL, 0,
4395 0);
4396 }
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004397
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004398 os_memcpy(wpa_s->p2ps_join_addr, grp_mac, ETH_ALEN);
4399 wpa_s->p2ps_method_config_any = 1;
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004400 }
4401
4402 wpa_msg_global(wpa_s, MSG_INFO,
4403 P2P_EVENT_P2PS_PROVISION_DONE MACSTR
4404 " status=%d conncap=%x"
4405 " adv_id=%x adv_mac=" MACSTR
4406 " session=%x mac=" MACSTR
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004407 " dev_passwd_id=%d go=%s%s",
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004408 MAC2STR(dev), status, conncap,
4409 adv_id, MAC2STR(adv_mac),
4410 ses_id, MAC2STR(ses_mac),
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004411 passwd_id, go_ifname, feat_cap_str);
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004412 return;
4413 }
4414
4415 if (go_wpa_s && !p2p_group_go_member_count(wpa_s))
4416 wpas_p2p_group_remove(wpa_s, go_wpa_s->ifname);
4417
4418 if (persistent_go && !persistent_go->num_p2p_clients) {
4419 /* remove empty persistent GO */
Hai Shalom74f70d42019-02-11 14:42:39 -08004420 wpa_dbg(wpa_s, MSG_DEBUG,
4421 "P2P: Remove empty persistent group id=%d",
4422 persistent_go->id);
4423 wpas_notify_persistent_group_removed(wpa_s, persistent_go);
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004424 wpa_config_remove_network(wpa_s->conf, persistent_go->id);
4425 }
4426
4427 if (conncap == P2PS_SETUP_CLIENT) {
Dmitry Shmidtde47be72016-01-07 12:52:55 -08004428 char ssid_hex[32 * 2 + 1];
4429
4430 if (group_ssid)
4431 wpa_snprintf_hex(ssid_hex, sizeof(ssid_hex),
4432 group_ssid, group_ssid_len);
4433 else
4434 ssid_hex[0] = '\0';
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004435 wpa_msg_global(wpa_s, MSG_INFO,
4436 P2P_EVENT_P2PS_PROVISION_DONE MACSTR
4437 " status=%d conncap=%x"
4438 " adv_id=%x adv_mac=" MACSTR
4439 " session=%x mac=" MACSTR
Dmitry Shmidtde47be72016-01-07 12:52:55 -08004440 " dev_passwd_id=%d join=" MACSTR "%s%s%s",
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004441 MAC2STR(dev), status, conncap,
4442 adv_id, MAC2STR(adv_mac),
4443 ses_id, MAC2STR(ses_mac),
Dmitry Shmidtde47be72016-01-07 12:52:55 -08004444 passwd_id, MAC2STR(grp_mac), feat_cap_str,
4445 group_ssid ? " group_ssid=" : "", ssid_hex);
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004446 } else {
4447 wpa_msg_global(wpa_s, MSG_INFO,
4448 P2P_EVENT_P2PS_PROVISION_DONE MACSTR
4449 " status=%d conncap=%x"
4450 " adv_id=%x adv_mac=" MACSTR
4451 " session=%x mac=" MACSTR
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004452 " dev_passwd_id=%d%s",
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004453 MAC2STR(dev), status, conncap,
4454 adv_id, MAC2STR(adv_mac),
4455 ses_id, MAC2STR(ses_mac),
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004456 passwd_id, feat_cap_str);
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004457 }
4458}
4459
4460
Dmitry Shmidt5a1480c2014-05-12 09:46:02 -07004461static int _wpas_p2p_in_progress(void *ctx)
4462{
4463 struct wpa_supplicant *wpa_s = ctx;
4464 return wpas_p2p_in_progress(wpa_s);
4465}
4466
4467
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004468static int wpas_prov_disc_resp_cb(void *ctx)
4469{
4470 struct wpa_supplicant *wpa_s = ctx;
4471 struct wpa_ssid *persistent_go;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004472 unsigned int freq;
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004473
4474 if (!wpa_s->global->pending_p2ps_group)
4475 return 0;
4476
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004477 freq = wpa_s->global->pending_p2ps_group_freq;
4478 wpa_s->global->pending_p2ps_group_freq = 0;
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004479 wpa_s->global->pending_p2ps_group = 0;
4480
4481 if (wpas_p2p_get_go_group(wpa_s))
4482 return 0;
4483 persistent_go = wpas_p2p_get_persistent_go(wpa_s);
4484
4485 if (persistent_go) {
4486 wpas_p2p_group_add_persistent(
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08004487 wpa_s, persistent_go, 0, 0, 0, 0, 0, 0, 0, 0, 0,
4488 NULL,
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004489 persistent_go->mode == WPAS_MODE_P2P_GO ?
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004490 P2P_MAX_INITIAL_CONN_WAIT_GO_REINVOKE : 0, 0);
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004491 } else {
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08004492 wpas_p2p_group_add(wpa_s, 1, freq, 0, 0, 0, 0, 0, 0);
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004493 }
4494
4495 return 1;
4496}
4497
4498
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004499static int wpas_p2p_get_pref_freq_list(void *ctx, int go,
4500 unsigned int *len,
4501 unsigned int *freq_list)
4502{
4503 struct wpa_supplicant *wpa_s = ctx;
4504
4505 return wpa_drv_get_pref_freq_list(wpa_s, go ? WPA_IF_P2P_GO :
4506 WPA_IF_P2P_CLIENT, len, freq_list);
4507}
4508
Jimmy Chenf887c7b2018-11-13 15:19:57 +08004509int wpas_p2p_mac_setup(struct wpa_supplicant *wpa_s)
4510{
4511 u8 addr[ETH_ALEN] = {0};
4512
4513 if (wpa_s->conf->p2p_device_random_mac_addr == 0)
4514 return 0;
4515
4516 if (wpa_s->conf->ssid == NULL) {
4517 if (random_mac_addr(addr) < 0) {
4518 wpa_msg(wpa_s, MSG_INFO,
4519 "Failed to generate random MAC address");
4520 return -EINVAL;
4521 }
4522
Hai Shalom39ba6fc2019-01-22 12:40:38 -08004523 /* Store generated MAC address. */
4524 os_memcpy(wpa_s->conf->p2p_device_persistent_mac_addr, addr,
4525 ETH_ALEN);
Jimmy Chenf887c7b2018-11-13 15:19:57 +08004526 } else {
Hai Shalom39ba6fc2019-01-22 12:40:38 -08004527 /* If there are existing saved groups, restore last MAC address.
4528 * if there is no last used MAC address, the last one is
4529 * factory MAC. */
4530 if (is_zero_ether_addr(
4531 wpa_s->conf->p2p_device_persistent_mac_addr))
Jimmy Chenf887c7b2018-11-13 15:19:57 +08004532 return 0;
Hai Shalom39ba6fc2019-01-22 12:40:38 -08004533 os_memcpy(addr, wpa_s->conf->p2p_device_persistent_mac_addr,
4534 ETH_ALEN);
Jimmy Chenf887c7b2018-11-13 15:19:57 +08004535 wpa_msg(wpa_s, MSG_DEBUG, "Restore last used MAC address.");
4536 }
4537
4538 if (wpa_drv_set_mac_addr(wpa_s, addr) < 0) {
4539 wpa_msg(wpa_s, MSG_INFO,
4540 "Failed to set random MAC address");
4541 return -EINVAL;
4542 }
4543
4544 if (wpa_supplicant_update_mac_addr(wpa_s) < 0) {
4545 wpa_msg(wpa_s, MSG_INFO,
4546 "Could not update MAC address information");
4547 return -EINVAL;
4548 }
4549
4550 wpa_msg(wpa_s, MSG_DEBUG, "Using random MAC address " MACSTR,
4551 MAC2STR(addr));
4552
4553 return 0;
4554}
4555
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004556/**
4557 * wpas_p2p_init - Initialize P2P module for %wpa_supplicant
4558 * @global: Pointer to global data from wpa_supplicant_init()
4559 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
4560 * Returns: 0 on success, -1 on failure
4561 */
4562int wpas_p2p_init(struct wpa_global *global, struct wpa_supplicant *wpa_s)
4563{
4564 struct p2p_config p2p;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004565 int i;
4566
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07004567 if (wpa_s->conf->p2p_disabled)
4568 return 0;
4569
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004570 if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_CAPABLE))
4571 return 0;
4572
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004573 if (global->p2p)
4574 return 0;
4575
Jimmy Chenf887c7b2018-11-13 15:19:57 +08004576 if (wpas_p2p_mac_setup(wpa_s) < 0) {
4577 wpa_msg(wpa_s, MSG_ERROR,
4578 "Failed to initialize P2P random MAC address.");
4579 return -1;
4580 }
4581
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004582 os_memset(&p2p, 0, sizeof(p2p));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004583 p2p.cb_ctx = wpa_s;
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07004584 p2p.debug_print = wpas_p2p_debug_print;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004585 p2p.p2p_scan = wpas_p2p_scan;
4586 p2p.send_action = wpas_send_action;
4587 p2p.send_action_done = wpas_send_action_done;
4588 p2p.go_neg_completed = wpas_go_neg_completed;
4589 p2p.go_neg_req_rx = wpas_go_neg_req_rx;
4590 p2p.dev_found = wpas_dev_found;
4591 p2p.dev_lost = wpas_dev_lost;
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07004592 p2p.find_stopped = wpas_find_stopped;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004593 p2p.start_listen = wpas_start_listen;
4594 p2p.stop_listen = wpas_stop_listen;
4595 p2p.send_probe_resp = wpas_send_probe_resp;
4596 p2p.sd_request = wpas_sd_request;
4597 p2p.sd_response = wpas_sd_response;
4598 p2p.prov_disc_req = wpas_prov_disc_req;
4599 p2p.prov_disc_resp = wpas_prov_disc_resp;
Jouni Malinen75ecf522011-06-27 15:19:46 -07004600 p2p.prov_disc_fail = wpas_prov_disc_fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004601 p2p.invitation_process = wpas_invitation_process;
4602 p2p.invitation_received = wpas_invitation_received;
4603 p2p.invitation_result = wpas_invitation_result;
4604 p2p.get_noa = wpas_get_noa;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08004605 p2p.go_connected = wpas_go_connected;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08004606 p2p.presence_resp = wpas_presence_resp;
Dmitry Shmidt18463232014-01-24 12:29:41 -08004607 p2p.is_concurrent_session_active = wpas_is_concurrent_session_active;
Dmitry Shmidt5a1480c2014-05-12 09:46:02 -07004608 p2p.is_p2p_in_progress = _wpas_p2p_in_progress;
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004609 p2p.get_persistent_group = wpas_get_persistent_group;
4610 p2p.get_go_info = wpas_get_go_info;
4611 p2p.remove_stale_groups = wpas_remove_stale_groups;
4612 p2p.p2ps_prov_complete = wpas_p2ps_prov_complete;
4613 p2p.prov_disc_resp_cb = wpas_prov_disc_resp_cb;
4614 p2p.p2ps_group_capability = p2ps_group_capability;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004615 p2p.get_pref_freq_list = wpas_p2p_get_pref_freq_list;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004616
4617 os_memcpy(wpa_s->global->p2p_dev_addr, wpa_s->own_addr, ETH_ALEN);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004618 os_memcpy(p2p.dev_addr, wpa_s->global->p2p_dev_addr, ETH_ALEN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004619 p2p.dev_name = wpa_s->conf->device_name;
4620 p2p.manufacturer = wpa_s->conf->manufacturer;
4621 p2p.model_name = wpa_s->conf->model_name;
4622 p2p.model_number = wpa_s->conf->model_number;
4623 p2p.serial_number = wpa_s->conf->serial_number;
4624 if (wpa_s->wps) {
4625 os_memcpy(p2p.uuid, wpa_s->wps->uuid, 16);
4626 p2p.config_methods = wpa_s->wps->config_methods;
4627 }
4628
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07004629 if (wpas_p2p_setup_channels(wpa_s, &p2p.channels, &p2p.cli_channels)) {
4630 wpa_printf(MSG_ERROR,
4631 "P2P: Failed to configure supported channel list");
4632 return -1;
4633 }
4634
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004635 if (wpa_s->conf->p2p_listen_reg_class &&
4636 wpa_s->conf->p2p_listen_channel) {
4637 p2p.reg_class = wpa_s->conf->p2p_listen_reg_class;
4638 p2p.channel = wpa_s->conf->p2p_listen_channel;
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07004639 p2p.channel_forced = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004640 } else {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004641 /*
4642 * Pick one of the social channels randomly as the listen
4643 * channel.
4644 */
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07004645 if (p2p_config_get_random_social(&p2p, &p2p.reg_class,
Hai Shalom74f70d42019-02-11 14:42:39 -08004646 &p2p.channel,
4647 &global->p2p_go_avoid_freq,
4648 &global->p2p_disallow_freq) !=
4649 0) {
Dmitry Shmidt1d755d02015-04-28 10:34:29 -07004650 wpa_printf(MSG_INFO,
4651 "P2P: No social channels supported by the driver - do not enable P2P");
4652 return 0;
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07004653 }
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07004654 p2p.channel_forced = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004655 }
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07004656 wpa_printf(MSG_DEBUG, "P2P: Own listen channel: %d:%d",
4657 p2p.reg_class, p2p.channel);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004658
4659 if (wpa_s->conf->p2p_oper_reg_class &&
4660 wpa_s->conf->p2p_oper_channel) {
4661 p2p.op_reg_class = wpa_s->conf->p2p_oper_reg_class;
4662 p2p.op_channel = wpa_s->conf->p2p_oper_channel;
4663 p2p.cfg_op_channel = 1;
4664 wpa_printf(MSG_DEBUG, "P2P: Configured operating channel: "
4665 "%d:%d", p2p.op_reg_class, p2p.op_channel);
4666
4667 } else {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004668 /*
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07004669 * Use random operation channel from 2.4 GHz band social
4670 * channels (1, 6, 11) or band 60 GHz social channel (2) if no
4671 * other preference is indicated.
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004672 */
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07004673 if (p2p_config_get_random_social(&p2p, &p2p.op_reg_class,
Hai Shalom74f70d42019-02-11 14:42:39 -08004674 &p2p.op_channel, NULL,
4675 NULL) != 0) {
4676 wpa_printf(MSG_INFO,
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07004677 "P2P: Failed to select random social channel as operation channel");
Hai Shalom74f70d42019-02-11 14:42:39 -08004678 p2p.op_reg_class = 0;
4679 p2p.op_channel = 0;
4680 /* This will be overridden during group setup in
4681 * p2p_prepare_channel(), so allow setup to continue. */
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07004682 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004683 p2p.cfg_op_channel = 0;
4684 wpa_printf(MSG_DEBUG, "P2P: Random operating channel: "
4685 "%d:%d", p2p.op_reg_class, p2p.op_channel);
4686 }
Dmitry Shmidt44c95782013-05-17 09:51:35 -07004687
4688 if (wpa_s->conf->p2p_pref_chan && wpa_s->conf->num_p2p_pref_chan) {
4689 p2p.pref_chan = wpa_s->conf->p2p_pref_chan;
4690 p2p.num_pref_chan = wpa_s->conf->num_p2p_pref_chan;
4691 }
4692
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004693 if (wpa_s->conf->country[0] && wpa_s->conf->country[1]) {
4694 os_memcpy(p2p.country, wpa_s->conf->country, 2);
4695 p2p.country[2] = 0x04;
4696 } else
4697 os_memcpy(p2p.country, "XX\x04", 3);
4698
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004699 os_memcpy(p2p.pri_dev_type, wpa_s->conf->device_type,
4700 WPS_DEV_TYPE_LEN);
4701
4702 p2p.num_sec_dev_types = wpa_s->conf->num_sec_device_types;
4703 os_memcpy(p2p.sec_dev_type, wpa_s->conf->sec_device_type,
4704 p2p.num_sec_dev_types * WPS_DEV_TYPE_LEN);
4705
4706 p2p.concurrent_operations = !!(wpa_s->drv_flags &
4707 WPA_DRIVER_FLAGS_P2P_CONCURRENT);
4708
4709 p2p.max_peers = 100;
4710
4711 if (wpa_s->conf->p2p_ssid_postfix) {
4712 p2p.ssid_postfix_len =
4713 os_strlen(wpa_s->conf->p2p_ssid_postfix);
4714 if (p2p.ssid_postfix_len > sizeof(p2p.ssid_postfix))
4715 p2p.ssid_postfix_len = sizeof(p2p.ssid_postfix);
4716 os_memcpy(p2p.ssid_postfix, wpa_s->conf->p2p_ssid_postfix,
4717 p2p.ssid_postfix_len);
4718 }
4719
4720 p2p.p2p_intra_bss = wpa_s->conf->p2p_intra_bss;
4721
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08004722 p2p.max_listen = wpa_s->max_remain_on_chan;
4723
Dmitry Shmidt2271d3f2014-06-23 12:16:31 -07004724 if (wpa_s->conf->p2p_passphrase_len >= 8 &&
4725 wpa_s->conf->p2p_passphrase_len <= 63)
4726 p2p.passphrase_len = wpa_s->conf->p2p_passphrase_len;
4727 else
4728 p2p.passphrase_len = 8;
4729
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004730 global->p2p = p2p_init(&p2p);
4731 if (global->p2p == NULL)
4732 return -1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07004733 global->p2p_init_wpa_s = wpa_s;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004734
4735 for (i = 0; i < MAX_WPS_VENDOR_EXT; i++) {
4736 if (wpa_s->conf->wps_vendor_ext[i] == NULL)
4737 continue;
4738 p2p_add_wps_vendor_extension(
4739 global->p2p, wpa_s->conf->wps_vendor_ext[i]);
4740 }
4741
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004742 p2p_set_no_go_freq(global->p2p, &wpa_s->conf->p2p_no_go_freq);
4743
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004744 return 0;
4745}
4746
4747
4748/**
4749 * wpas_p2p_deinit - Deinitialize per-interface P2P data
4750 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
4751 *
4752 * This function deinitialize per-interface P2P data.
4753 */
4754void wpas_p2p_deinit(struct wpa_supplicant *wpa_s)
4755{
4756 if (wpa_s->driver && wpa_s->drv_priv)
4757 wpa_drv_probe_req_report(wpa_s, 0);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004758
4759 if (wpa_s->go_params) {
4760 /* Clear any stored provisioning info */
4761 p2p_clear_provisioning_info(
4762 wpa_s->global->p2p,
Dmitry Shmidt04949592012-07-19 12:16:46 -07004763 wpa_s->go_params->peer_device_addr);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004764 }
4765
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004766 os_free(wpa_s->go_params);
4767 wpa_s->go_params = NULL;
Dmitry Shmidt684785c2014-05-12 13:34:29 -07004768 eloop_cancel_timeout(wpas_p2p_psk_failure_removal, wpa_s, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004769 eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s, NULL);
4770 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
Hai Shalomfdcde762020-04-02 11:19:20 -07004771 wpa_s->global->p2p_long_listen = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004772 eloop_cancel_timeout(wpas_p2p_long_listen_timeout, wpa_s, NULL);
4773 eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL);
4774 wpas_p2p_remove_pending_group_interface(wpa_s);
Dmitry Shmidt04f534e2013-12-09 15:50:16 -08004775 eloop_cancel_timeout(wpas_p2p_group_freq_conflict, wpa_s, NULL);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08004776 wpas_p2p_listen_work_done(wpa_s);
4777 if (wpa_s->p2p_send_action_work) {
4778 os_free(wpa_s->p2p_send_action_work->ctx);
4779 radio_work_done(wpa_s->p2p_send_action_work);
4780 wpa_s->p2p_send_action_work = NULL;
4781 }
4782 eloop_cancel_timeout(wpas_p2p_send_action_work_timeout, wpa_s, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004783
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08004784 wpabuf_free(wpa_s->p2p_oob_dev_pw);
4785 wpa_s->p2p_oob_dev_pw = NULL;
4786
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004787 os_free(wpa_s->p2p_group_common_freqs);
4788 wpa_s->p2p_group_common_freqs = NULL;
4789 wpa_s->p2p_group_common_freqs_num = 0;
4790
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004791 /* TODO: remove group interface from the driver if this wpa_s instance
4792 * is on top of a P2P group interface */
4793}
4794
4795
4796/**
4797 * wpas_p2p_deinit_global - Deinitialize global P2P module
4798 * @global: Pointer to global data from wpa_supplicant_init()
4799 *
4800 * This function deinitializes the global (per device) P2P module.
4801 */
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07004802static void wpas_p2p_deinit_global(struct wpa_global *global)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004803{
4804 struct wpa_supplicant *wpa_s, *tmp;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004805
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004806 wpa_s = global->ifaces;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004807
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07004808 wpas_p2p_service_flush(global->p2p_init_wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004809
4810 /* Remove remaining P2P group interfaces */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004811 while (wpa_s && wpa_s->p2p_group_interface != NOT_P2P_GROUP_INTERFACE)
4812 wpa_s = wpa_s->next;
4813 while (wpa_s) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004814 tmp = global->ifaces;
4815 while (tmp &&
4816 (tmp == wpa_s ||
4817 tmp->p2p_group_interface == NOT_P2P_GROUP_INTERFACE)) {
4818 tmp = tmp->next;
4819 }
4820 if (tmp == NULL)
4821 break;
Dmitry Shmidt04949592012-07-19 12:16:46 -07004822 /* Disconnect from the P2P group and deinit the interface */
4823 wpas_p2p_disconnect(tmp);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004824 }
4825
4826 /*
4827 * Deinit GO data on any possibly remaining interface (if main
4828 * interface is used as GO).
4829 */
4830 for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
4831 if (wpa_s->ap_iface)
4832 wpas_p2p_group_deinit(wpa_s);
4833 }
4834
4835 p2p_deinit(global->p2p);
4836 global->p2p = NULL;
Dmitry Shmidt04949592012-07-19 12:16:46 -07004837 global->p2p_init_wpa_s = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004838}
4839
4840
4841static int wpas_p2p_create_iface(struct wpa_supplicant *wpa_s)
4842{
Dmitry Shmidt9c175262016-03-03 10:20:07 -08004843 if (wpa_s->conf->p2p_no_group_iface)
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08004844 return 0; /* separate interface disabled per configuration */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004845 if (wpa_s->drv_flags &
4846 (WPA_DRIVER_FLAGS_P2P_DEDICATED_INTERFACE |
4847 WPA_DRIVER_FLAGS_P2P_MGMT_AND_NON_P2P))
4848 return 1; /* P2P group requires a new interface in every case
4849 */
4850 if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_CONCURRENT))
4851 return 0; /* driver does not support concurrent operations */
4852 if (wpa_s->global->ifaces->next)
4853 return 1; /* more that one interface already in use */
4854 if (wpa_s->wpa_state >= WPA_AUTHENTICATING)
4855 return 1; /* this interface is already in use */
4856 return 0;
4857}
4858
4859
4860static int wpas_p2p_start_go_neg(struct wpa_supplicant *wpa_s,
4861 const u8 *peer_addr,
4862 enum p2p_wps_method wps_method,
4863 int go_intent, const u8 *own_interface_addr,
Dmitry Shmidt04949592012-07-19 12:16:46 -07004864 unsigned int force_freq, int persistent_group,
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08004865 struct wpa_ssid *ssid, unsigned int pref_freq)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004866{
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004867 if (persistent_group && wpa_s->conf->persistent_reconnect)
4868 persistent_group = 2;
4869
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004870 /*
4871 * Increase GO config timeout if HT40 is used since it takes some time
4872 * to scan channels for coex purposes before the BSS can be started.
4873 */
4874 p2p_set_config_timeout(wpa_s->global->p2p,
4875 wpa_s->p2p_go_ht40 ? 255 : 100, 20);
4876
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004877 return p2p_connect(wpa_s->global->p2p, peer_addr, wps_method,
4878 go_intent, own_interface_addr, force_freq,
Dmitry Shmidt04949592012-07-19 12:16:46 -07004879 persistent_group, ssid ? ssid->ssid : NULL,
4880 ssid ? ssid->ssid_len : 0,
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08004881 wpa_s->p2p_pd_before_go_neg, pref_freq,
4882 wps_method == WPS_NFC ? wpa_s->p2p_oob_dev_pw_id :
4883 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004884}
4885
4886
4887static int wpas_p2p_auth_go_neg(struct wpa_supplicant *wpa_s,
4888 const u8 *peer_addr,
4889 enum p2p_wps_method wps_method,
4890 int go_intent, const u8 *own_interface_addr,
Dmitry Shmidt04949592012-07-19 12:16:46 -07004891 unsigned int force_freq, int persistent_group,
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08004892 struct wpa_ssid *ssid, unsigned int pref_freq)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004893{
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004894 if (persistent_group && wpa_s->conf->persistent_reconnect)
4895 persistent_group = 2;
4896
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004897 return p2p_authorize(wpa_s->global->p2p, peer_addr, wps_method,
4898 go_intent, own_interface_addr, force_freq,
Dmitry Shmidt04949592012-07-19 12:16:46 -07004899 persistent_group, ssid ? ssid->ssid : NULL,
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08004900 ssid ? ssid->ssid_len : 0, pref_freq,
4901 wps_method == WPS_NFC ? wpa_s->p2p_oob_dev_pw_id :
4902 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004903}
4904
4905
4906static void wpas_p2p_check_join_scan_limit(struct wpa_supplicant *wpa_s)
4907{
4908 wpa_s->p2p_join_scan_count++;
4909 wpa_printf(MSG_DEBUG, "P2P: Join scan attempt %d",
4910 wpa_s->p2p_join_scan_count);
4911 if (wpa_s->p2p_join_scan_count > P2P_MAX_JOIN_SCAN_ATTEMPTS) {
4912 wpa_printf(MSG_DEBUG, "P2P: Failed to find GO " MACSTR
4913 " for join operationg - stop join attempt",
4914 MAC2STR(wpa_s->pending_join_iface_addr));
4915 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
Dmitry Shmidt04949592012-07-19 12:16:46 -07004916 if (wpa_s->p2p_auto_pd) {
4917 wpa_s->p2p_auto_pd = 0;
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07004918 wpa_msg_global(wpa_s, MSG_INFO,
4919 P2P_EVENT_PROV_DISC_FAILURE
4920 " p2p_dev_addr=" MACSTR " status=N/A",
4921 MAC2STR(wpa_s->pending_join_dev_addr));
Dmitry Shmidt04949592012-07-19 12:16:46 -07004922 return;
4923 }
Dmitry Shmidt9c175262016-03-03 10:20:07 -08004924 wpa_msg_global(wpa_s->p2pdev, MSG_INFO,
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07004925 P2P_EVENT_GROUP_FORMATION_FAILURE);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004926 wpas_notify_p2p_group_formation_failure(wpa_s, "");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004927 }
4928}
4929
4930
Dmitry Shmidt04949592012-07-19 12:16:46 -07004931static int wpas_check_freq_conflict(struct wpa_supplicant *wpa_s, int freq)
4932{
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07004933 int res;
4934 unsigned int num, i;
4935 struct wpa_used_freq_data *freqs;
Dmitry Shmidt04949592012-07-19 12:16:46 -07004936
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07004937 if (wpas_p2p_num_unused_channels(wpa_s) > 0) {
4938 /* Multiple channels are supported and not all are in use */
Dmitry Shmidt04949592012-07-19 12:16:46 -07004939 return 0;
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07004940 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07004941
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07004942 freqs = os_calloc(wpa_s->num_multichan_concurrent,
4943 sizeof(struct wpa_used_freq_data));
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07004944 if (!freqs)
4945 return 1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07004946
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07004947 num = wpas_p2p_valid_oper_freqs(wpa_s, freqs,
4948 wpa_s->num_multichan_concurrent);
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07004949
4950 for (i = 0; i < num; i++) {
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07004951 if (freqs[i].freq == freq) {
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07004952 wpa_printf(MSG_DEBUG, "P2P: Frequency %d MHz in use by another virtual interface and can be used",
4953 freq);
4954 res = 0;
4955 goto exit_free;
Dmitry Shmidt04949592012-07-19 12:16:46 -07004956 }
4957 }
4958
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07004959 wpa_printf(MSG_DEBUG, "P2P: No valid operating frequencies");
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07004960 res = 1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07004961
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07004962exit_free:
4963 os_free(freqs);
4964 return res;
Dmitry Shmidt04949592012-07-19 12:16:46 -07004965}
4966
4967
4968static int wpas_p2p_peer_go(struct wpa_supplicant *wpa_s,
4969 const u8 *peer_dev_addr)
4970{
4971 struct wpa_bss *bss;
4972 int updated;
4973
4974 bss = wpa_bss_get_p2p_dev_addr(wpa_s, peer_dev_addr);
4975 if (bss == NULL)
4976 return -1;
4977 if (bss->last_update_idx < wpa_s->bss_update_idx) {
4978 wpa_printf(MSG_DEBUG, "P2P: Peer BSS entry not updated in the "
4979 "last scan");
4980 return 0;
4981 }
4982
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08004983 updated = os_reltime_before(&wpa_s->p2p_auto_started,
4984 &bss->last_update);
Dmitry Shmidt04949592012-07-19 12:16:46 -07004985 wpa_printf(MSG_DEBUG, "P2P: Current BSS entry for peer updated at "
4986 "%ld.%06ld (%supdated in last scan)",
4987 bss->last_update.sec, bss->last_update.usec,
4988 updated ? "": "not ");
4989
4990 return updated;
4991}
4992
4993
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004994static void wpas_p2p_scan_res_join(struct wpa_supplicant *wpa_s,
4995 struct wpa_scan_results *scan_res)
4996{
Dmitry Shmidt344abd32014-01-14 13:17:00 -08004997 struct wpa_bss *bss = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004998 int freq;
4999 u8 iface_addr[ETH_ALEN];
Dmitry Shmidt04949592012-07-19 12:16:46 -07005000
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005001 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
5002
5003 if (wpa_s->global->p2p_disabled)
5004 return;
5005
Dmitry Shmidt04949592012-07-19 12:16:46 -07005006 wpa_printf(MSG_DEBUG, "P2P: Scan results received (%d BSS) for %sjoin",
5007 scan_res ? (int) scan_res->num : -1,
5008 wpa_s->p2p_auto_join ? "auto_" : "");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005009
5010 if (scan_res)
5011 wpas_p2p_scan_res_handler(wpa_s, scan_res);
5012
Dmitry Shmidt04949592012-07-19 12:16:46 -07005013 if (wpa_s->p2p_auto_pd) {
5014 int join = wpas_p2p_peer_go(wpa_s,
5015 wpa_s->pending_join_dev_addr);
5016 if (join == 0 &&
5017 wpa_s->auto_pd_scan_retry < P2P_AUTO_PD_SCAN_ATTEMPTS) {
5018 wpa_s->auto_pd_scan_retry++;
Dmitry Shmidt444d5672013-04-01 13:08:44 -07005019 bss = wpa_bss_get_bssid_latest(
5020 wpa_s, wpa_s->pending_join_dev_addr);
Dmitry Shmidt04949592012-07-19 12:16:46 -07005021 if (bss) {
5022 freq = bss->freq;
5023 wpa_printf(MSG_DEBUG, "P2P: Scan retry %d for "
5024 "the peer " MACSTR " at %d MHz",
5025 wpa_s->auto_pd_scan_retry,
5026 MAC2STR(wpa_s->
5027 pending_join_dev_addr),
5028 freq);
Dmitry Shmidt344abd32014-01-14 13:17:00 -08005029 wpas_p2p_join_scan_req(wpa_s, freq, NULL, 0);
Dmitry Shmidt04949592012-07-19 12:16:46 -07005030 return;
5031 }
5032 }
5033
5034 if (join < 0)
5035 join = 0;
5036
5037 wpa_s->p2p_auto_pd = 0;
5038 wpa_s->pending_pd_use = join ? AUTO_PD_JOIN : AUTO_PD_GO_NEG;
5039 wpa_printf(MSG_DEBUG, "P2P: Auto PD with " MACSTR " join=%d",
5040 MAC2STR(wpa_s->pending_join_dev_addr), join);
5041 if (p2p_prov_disc_req(wpa_s->global->p2p,
Dmitry Shmidt216983b2015-02-06 10:50:36 -08005042 wpa_s->pending_join_dev_addr, NULL,
Dmitry Shmidt04949592012-07-19 12:16:46 -07005043 wpa_s->pending_pd_config_methods, join,
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08005044 0, wpa_s->user_initiated_pd) < 0) {
Dmitry Shmidt04949592012-07-19 12:16:46 -07005045 wpa_s->p2p_auto_pd = 0;
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07005046 wpa_msg_global(wpa_s, MSG_INFO,
5047 P2P_EVENT_PROV_DISC_FAILURE
5048 " p2p_dev_addr=" MACSTR " status=N/A",
5049 MAC2STR(wpa_s->pending_join_dev_addr));
Dmitry Shmidt04949592012-07-19 12:16:46 -07005050 }
5051 return;
5052 }
5053
5054 if (wpa_s->p2p_auto_join) {
5055 int join = wpas_p2p_peer_go(wpa_s,
5056 wpa_s->pending_join_dev_addr);
5057 if (join < 0) {
5058 wpa_printf(MSG_DEBUG, "P2P: Peer was not found to be "
5059 "running a GO -> use GO Negotiation");
Dmitry Shmidt9c175262016-03-03 10:20:07 -08005060 wpa_msg_global(wpa_s->p2pdev, MSG_INFO,
Dmitry Shmidt7f656022015-02-25 14:36:37 -08005061 P2P_EVENT_FALLBACK_TO_GO_NEG
5062 "reason=peer-not-running-GO");
Dmitry Shmidt04949592012-07-19 12:16:46 -07005063 wpas_p2p_connect(wpa_s, wpa_s->pending_join_dev_addr,
5064 wpa_s->p2p_pin, wpa_s->p2p_wps_method,
5065 wpa_s->p2p_persistent_group, 0, 0, 0,
5066 wpa_s->p2p_go_intent,
5067 wpa_s->p2p_connect_freq,
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08005068 wpa_s->p2p_go_vht_center_freq2,
Dmitry Shmidt04949592012-07-19 12:16:46 -07005069 wpa_s->p2p_persistent_id,
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005070 wpa_s->p2p_pd_before_go_neg,
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07005071 wpa_s->p2p_go_ht40,
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08005072 wpa_s->p2p_go_vht,
Dmitry Shmidtde47be72016-01-07 12:52:55 -08005073 wpa_s->p2p_go_max_oper_chwidth,
Hai Shalom74f70d42019-02-11 14:42:39 -08005074 wpa_s->p2p_go_he,
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08005075 wpa_s->p2p_go_edmg,
Dmitry Shmidtde47be72016-01-07 12:52:55 -08005076 NULL, 0);
Dmitry Shmidt04949592012-07-19 12:16:46 -07005077 return;
5078 }
5079
5080 wpa_printf(MSG_DEBUG, "P2P: Peer was found running GO%s -> "
5081 "try to join the group", join ? "" :
5082 " in older scan");
Dmitry Shmidt7f656022015-02-25 14:36:37 -08005083 if (!join) {
Dmitry Shmidt9c175262016-03-03 10:20:07 -08005084 wpa_msg_global(wpa_s->p2pdev, MSG_INFO,
Dmitry Shmidt7f656022015-02-25 14:36:37 -08005085 P2P_EVENT_FALLBACK_TO_GO_NEG_ENABLED);
Dmitry Shmidt04949592012-07-19 12:16:46 -07005086 wpa_s->p2p_fallback_to_go_neg = 1;
Dmitry Shmidt7f656022015-02-25 14:36:37 -08005087 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07005088 }
5089
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005090 freq = p2p_get_oper_freq(wpa_s->global->p2p,
5091 wpa_s->pending_join_iface_addr);
5092 if (freq < 0 &&
5093 p2p_get_interface_addr(wpa_s->global->p2p,
5094 wpa_s->pending_join_dev_addr,
5095 iface_addr) == 0 &&
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07005096 os_memcmp(iface_addr, wpa_s->pending_join_dev_addr, ETH_ALEN) != 0
5097 && !wpa_bss_get_bssid(wpa_s, wpa_s->pending_join_iface_addr)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005098 wpa_printf(MSG_DEBUG, "P2P: Overwrite pending interface "
5099 "address for join from " MACSTR " to " MACSTR
5100 " based on newly discovered P2P peer entry",
5101 MAC2STR(wpa_s->pending_join_iface_addr),
5102 MAC2STR(iface_addr));
5103 os_memcpy(wpa_s->pending_join_iface_addr, iface_addr,
5104 ETH_ALEN);
5105
5106 freq = p2p_get_oper_freq(wpa_s->global->p2p,
5107 wpa_s->pending_join_iface_addr);
5108 }
5109 if (freq >= 0) {
5110 wpa_printf(MSG_DEBUG, "P2P: Target GO operating frequency "
5111 "from P2P peer table: %d MHz", freq);
5112 }
Dmitry Shmidt344abd32014-01-14 13:17:00 -08005113 if (wpa_s->p2p_join_ssid_len) {
5114 wpa_printf(MSG_DEBUG, "P2P: Trying to find target GO BSS entry based on BSSID "
5115 MACSTR " and SSID %s",
5116 MAC2STR(wpa_s->pending_join_iface_addr),
5117 wpa_ssid_txt(wpa_s->p2p_join_ssid,
5118 wpa_s->p2p_join_ssid_len));
5119 bss = wpa_bss_get(wpa_s, wpa_s->pending_join_iface_addr,
5120 wpa_s->p2p_join_ssid,
5121 wpa_s->p2p_join_ssid_len);
Dmitry Shmidtde47be72016-01-07 12:52:55 -08005122 } else if (!bss) {
Dmitry Shmidt344abd32014-01-14 13:17:00 -08005123 wpa_printf(MSG_DEBUG, "P2P: Trying to find target GO BSS entry based on BSSID "
5124 MACSTR, MAC2STR(wpa_s->pending_join_iface_addr));
5125 bss = wpa_bss_get_bssid_latest(wpa_s,
5126 wpa_s->pending_join_iface_addr);
5127 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005128 if (bss) {
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07005129 u8 dev_addr[ETH_ALEN];
5130
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005131 freq = bss->freq;
5132 wpa_printf(MSG_DEBUG, "P2P: Target GO operating frequency "
Dmitry Shmidt051af732013-10-22 13:52:46 -07005133 "from BSS table: %d MHz (SSID %s)", freq,
5134 wpa_ssid_txt(bss->ssid, bss->ssid_len));
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07005135 if (p2p_parse_dev_addr((const u8 *) (bss + 1), bss->ie_len,
5136 dev_addr) == 0 &&
5137 os_memcmp(wpa_s->pending_join_dev_addr,
5138 wpa_s->pending_join_iface_addr, ETH_ALEN) == 0 &&
5139 os_memcmp(dev_addr, wpa_s->pending_join_dev_addr,
5140 ETH_ALEN) != 0) {
5141 wpa_printf(MSG_DEBUG,
5142 "P2P: Update target GO device address based on BSS entry: " MACSTR " (was " MACSTR ")",
5143 MAC2STR(dev_addr),
5144 MAC2STR(wpa_s->pending_join_dev_addr));
5145 os_memcpy(wpa_s->pending_join_dev_addr, dev_addr,
5146 ETH_ALEN);
5147 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005148 }
5149 if (freq > 0) {
5150 u16 method;
5151
Dmitry Shmidt04949592012-07-19 12:16:46 -07005152 if (wpas_check_freq_conflict(wpa_s, freq) > 0) {
Dmitry Shmidt9c175262016-03-03 10:20:07 -08005153 wpa_msg_global(wpa_s->p2pdev, MSG_INFO,
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07005154 P2P_EVENT_GROUP_FORMATION_FAILURE
5155 "reason=FREQ_CONFLICT");
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005156 wpas_notify_p2p_group_formation_failure(
5157 wpa_s, "FREQ_CONFLICT");
Dmitry Shmidt04949592012-07-19 12:16:46 -07005158 return;
5159 }
5160
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005161 wpa_printf(MSG_DEBUG, "P2P: Send Provision Discovery Request "
5162 "prior to joining an existing group (GO " MACSTR
5163 " freq=%u MHz)",
5164 MAC2STR(wpa_s->pending_join_dev_addr), freq);
5165 wpa_s->pending_pd_before_join = 1;
5166
5167 switch (wpa_s->pending_join_wps_method) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005168 case WPS_PIN_DISPLAY:
5169 method = WPS_CONFIG_KEYPAD;
5170 break;
5171 case WPS_PIN_KEYPAD:
5172 method = WPS_CONFIG_DISPLAY;
5173 break;
5174 case WPS_PBC:
5175 method = WPS_CONFIG_PUSHBUTTON;
5176 break;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005177 case WPS_P2PS:
5178 method = WPS_CONFIG_P2PS;
5179 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005180 default:
5181 method = 0;
5182 break;
5183 }
5184
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005185 if ((p2p_get_provisioning_info(wpa_s->global->p2p,
5186 wpa_s->pending_join_dev_addr) ==
5187 method)) {
5188 /*
5189 * We have already performed provision discovery for
5190 * joining the group. Proceed directly to join
5191 * operation without duplicated provision discovery. */
5192 wpa_printf(MSG_DEBUG, "P2P: Provision discovery "
5193 "with " MACSTR " already done - proceed to "
5194 "join",
5195 MAC2STR(wpa_s->pending_join_dev_addr));
5196 wpa_s->pending_pd_before_join = 0;
5197 goto start;
5198 }
5199
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005200 if (p2p_prov_disc_req(wpa_s->global->p2p,
Dmitry Shmidt216983b2015-02-06 10:50:36 -08005201 wpa_s->pending_join_dev_addr,
5202 NULL, method, 1,
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08005203 freq, wpa_s->user_initiated_pd) < 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005204 wpa_printf(MSG_DEBUG, "P2P: Failed to send Provision "
5205 "Discovery Request before joining an "
5206 "existing group");
5207 wpa_s->pending_pd_before_join = 0;
5208 goto start;
5209 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005210 return;
5211 }
5212
5213 wpa_printf(MSG_DEBUG, "P2P: Failed to find BSS/GO - try again later");
5214 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
5215 eloop_register_timeout(1, 0, wpas_p2p_join_scan, wpa_s, NULL);
5216 wpas_p2p_check_join_scan_limit(wpa_s);
5217 return;
5218
5219start:
5220 /* Start join operation immediately */
Dmitry Shmidtde47be72016-01-07 12:52:55 -08005221 wpas_p2p_join_start(wpa_s, 0, wpa_s->p2p_join_ssid,
5222 wpa_s->p2p_join_ssid_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005223}
5224
5225
Dmitry Shmidt344abd32014-01-14 13:17:00 -08005226static void wpas_p2p_join_scan_req(struct wpa_supplicant *wpa_s, int freq,
5227 const u8 *ssid, size_t ssid_len)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005228{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005229 int ret;
5230 struct wpa_driver_scan_params params;
5231 struct wpabuf *wps_ie, *ies;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005232 size_t ielen;
Dmitry Shmidt04949592012-07-19 12:16:46 -07005233 int freqs[2] = { 0, 0 };
Dmitry Shmidt9c175262016-03-03 10:20:07 -08005234 unsigned int bands;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005235
5236 os_memset(&params, 0, sizeof(params));
5237
5238 /* P2P Wildcard SSID */
5239 params.num_ssids = 1;
Dmitry Shmidt344abd32014-01-14 13:17:00 -08005240 if (ssid && ssid_len) {
5241 params.ssids[0].ssid = ssid;
5242 params.ssids[0].ssid_len = ssid_len;
5243 os_memcpy(wpa_s->p2p_join_ssid, ssid, ssid_len);
5244 wpa_s->p2p_join_ssid_len = ssid_len;
5245 } else {
5246 params.ssids[0].ssid = (u8 *) P2P_WILDCARD_SSID;
5247 params.ssids[0].ssid_len = P2P_WILDCARD_SSID_LEN;
5248 wpa_s->p2p_join_ssid_len = 0;
5249 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005250
5251 wpa_s->wps->dev.p2p = 1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07005252 wps_ie = wps_build_probe_req_ie(DEV_PW_DEFAULT, &wpa_s->wps->dev,
5253 wpa_s->wps->uuid, WPS_REQ_ENROLLEE, 0,
5254 NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005255 if (wps_ie == NULL) {
5256 wpas_p2p_scan_res_join(wpa_s, NULL);
5257 return;
5258 }
5259
Dmitry Shmidt9e3f8ee2014-01-17 10:52:01 -08005260 if (!freq) {
5261 int oper_freq;
5262 /*
5263 * If freq is not provided, check the operating freq of the GO
5264 * and use a single channel scan on if possible.
5265 */
5266 oper_freq = p2p_get_oper_freq(wpa_s->global->p2p,
5267 wpa_s->pending_join_iface_addr);
5268 if (oper_freq > 0)
5269 freq = oper_freq;
5270 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07005271 if (freq > 0) {
5272 freqs[0] = freq;
5273 params.freqs = freqs;
5274 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005275
Dmitry Shmidt9c175262016-03-03 10:20:07 -08005276 ielen = p2p_scan_ie_buf_len(wpa_s->global->p2p);
5277 ies = wpabuf_alloc(wpabuf_len(wps_ie) + ielen);
5278 if (ies == NULL) {
5279 wpabuf_free(wps_ie);
5280 wpas_p2p_scan_res_join(wpa_s, NULL);
5281 return;
5282 }
5283 wpabuf_put_buf(ies, wps_ie);
5284 wpabuf_free(wps_ie);
5285
5286 bands = wpas_get_bands(wpa_s, freqs);
5287 p2p_scan_ie(wpa_s->global->p2p, ies, NULL, bands);
5288
5289 params.p2p_probe = 1;
5290 params.extra_ies = wpabuf_head(ies);
5291 params.extra_ies_len = wpabuf_len(ies);
5292
Dmitry Shmidtebd93af2017-02-21 13:40:44 -08005293 if (wpa_s->clear_driver_scan_cache) {
5294 wpa_printf(MSG_DEBUG,
5295 "Request driver to clear scan cache due to local BSS flush");
5296 params.only_new_results = 1;
5297 }
5298
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005299 /*
5300 * Run a scan to update BSS table and start Provision Discovery once
5301 * the new scan results become available.
5302 */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005303 ret = wpa_drv_scan(wpa_s, &params);
Dmitry Shmidt444d5672013-04-01 13:08:44 -07005304 if (!ret) {
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005305 os_get_reltime(&wpa_s->scan_trigger_time);
Dmitry Shmidt04949592012-07-19 12:16:46 -07005306 wpa_s->scan_res_handler = wpas_p2p_scan_res_join;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005307 wpa_s->own_scan_requested = 1;
Dmitry Shmidtebd93af2017-02-21 13:40:44 -08005308 wpa_s->clear_driver_scan_cache = 0;
Dmitry Shmidt444d5672013-04-01 13:08:44 -07005309 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005310
5311 wpabuf_free(ies);
5312
5313 if (ret) {
5314 wpa_printf(MSG_DEBUG, "P2P: Failed to start scan for join - "
5315 "try again later");
5316 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
5317 eloop_register_timeout(1, 0, wpas_p2p_join_scan, wpa_s, NULL);
5318 wpas_p2p_check_join_scan_limit(wpa_s);
5319 }
5320}
5321
5322
Dmitry Shmidt04949592012-07-19 12:16:46 -07005323static void wpas_p2p_join_scan(void *eloop_ctx, void *timeout_ctx)
5324{
5325 struct wpa_supplicant *wpa_s = eloop_ctx;
Dmitry Shmidt344abd32014-01-14 13:17:00 -08005326 wpas_p2p_join_scan_req(wpa_s, 0, NULL, 0);
Dmitry Shmidt04949592012-07-19 12:16:46 -07005327}
5328
5329
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005330static int wpas_p2p_join(struct wpa_supplicant *wpa_s, const u8 *iface_addr,
Dmitry Shmidt04949592012-07-19 12:16:46 -07005331 const u8 *dev_addr, enum p2p_wps_method wps_method,
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08005332 int auto_join, int op_freq,
5333 const u8 *ssid, size_t ssid_len)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005334{
5335 wpa_printf(MSG_DEBUG, "P2P: Request to join existing group (iface "
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08005336 MACSTR " dev " MACSTR " op_freq=%d)%s",
5337 MAC2STR(iface_addr), MAC2STR(dev_addr), op_freq,
Dmitry Shmidt04949592012-07-19 12:16:46 -07005338 auto_join ? " (auto_join)" : "");
Dmitry Shmidt344abd32014-01-14 13:17:00 -08005339 if (ssid && ssid_len) {
5340 wpa_printf(MSG_DEBUG, "P2P: Group SSID specified: %s",
5341 wpa_ssid_txt(ssid, ssid_len));
5342 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005343
Dmitry Shmidt04949592012-07-19 12:16:46 -07005344 wpa_s->p2p_auto_pd = 0;
5345 wpa_s->p2p_auto_join = !!auto_join;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005346 os_memcpy(wpa_s->pending_join_iface_addr, iface_addr, ETH_ALEN);
5347 os_memcpy(wpa_s->pending_join_dev_addr, dev_addr, ETH_ALEN);
5348 wpa_s->pending_join_wps_method = wps_method;
5349
5350 /* Make sure we are not running find during connection establishment */
5351 wpas_p2p_stop_find(wpa_s);
5352
5353 wpa_s->p2p_join_scan_count = 0;
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08005354 wpas_p2p_join_scan_req(wpa_s, op_freq, ssid, ssid_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005355 return 0;
5356}
5357
5358
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08005359static int wpas_p2p_join_start(struct wpa_supplicant *wpa_s, int freq,
5360 const u8 *ssid, size_t ssid_len)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005361{
5362 struct wpa_supplicant *group;
5363 struct p2p_go_neg_results res;
Dmitry Shmidt04949592012-07-19 12:16:46 -07005364 struct wpa_bss *bss;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005365
5366 group = wpas_p2p_get_group_iface(wpa_s, 0, 0);
5367 if (group == NULL)
5368 return -1;
5369 if (group != wpa_s) {
5370 os_memcpy(group->p2p_pin, wpa_s->p2p_pin,
5371 sizeof(group->p2p_pin));
5372 group->p2p_wps_method = wpa_s->p2p_wps_method;
5373 }
5374
Hai Shalom74f70d42019-02-11 14:42:39 -08005375 /*
5376 * Need to mark the current interface for p2p_group_formation
5377 * when a separate group interface is not used. This is needed
5378 * to allow p2p_cancel stop a pending p2p_connect-join.
5379 * wpas_p2p_init_group_interface() addresses this for the case
5380 * where a separate group interface is used.
5381 */
5382 if (group == wpa_s->parent)
5383 wpa_s->global->p2p_group_formation = group;
5384
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005385 group->p2p_in_provisioning = 1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07005386 group->p2p_fallback_to_go_neg = wpa_s->p2p_fallback_to_go_neg;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005387
5388 os_memset(&res, 0, sizeof(res));
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08005389 os_memcpy(res.peer_device_addr, wpa_s->pending_join_dev_addr, ETH_ALEN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005390 os_memcpy(res.peer_interface_addr, wpa_s->pending_join_iface_addr,
5391 ETH_ALEN);
5392 res.wps_method = wpa_s->pending_join_wps_method;
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08005393 if (freq && ssid && ssid_len) {
5394 res.freq = freq;
5395 res.ssid_len = ssid_len;
5396 os_memcpy(res.ssid, ssid, ssid_len);
5397 } else {
Dmitry Shmidtde47be72016-01-07 12:52:55 -08005398 if (ssid && ssid_len) {
5399 bss = wpa_bss_get(wpa_s, wpa_s->pending_join_iface_addr,
5400 ssid, ssid_len);
5401 } else {
5402 bss = wpa_bss_get_bssid_latest(
5403 wpa_s, wpa_s->pending_join_iface_addr);
5404 }
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08005405 if (bss) {
5406 res.freq = bss->freq;
5407 res.ssid_len = bss->ssid_len;
5408 os_memcpy(res.ssid, bss->ssid, bss->ssid_len);
5409 wpa_printf(MSG_DEBUG, "P2P: Join target GO operating frequency from BSS table: %d MHz (SSID %s)",
5410 bss->freq,
5411 wpa_ssid_txt(bss->ssid, bss->ssid_len));
Dmitry Shmidtde47be72016-01-07 12:52:55 -08005412 } else if (ssid && ssid_len) {
5413 res.ssid_len = ssid_len;
5414 os_memcpy(res.ssid, ssid, ssid_len);
5415 wpa_printf(MSG_DEBUG, "P2P: Join target GO (SSID %s)",
5416 wpa_ssid_txt(ssid, ssid_len));
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08005417 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07005418 }
5419
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08005420 if (wpa_s->off_channel_freq || wpa_s->roc_waiting_drv_freq) {
5421 wpa_printf(MSG_DEBUG, "P2P: Cancel remain-on-channel prior to "
5422 "starting client");
5423 wpa_drv_cancel_remain_on_channel(wpa_s);
5424 wpa_s->off_channel_freq = 0;
5425 wpa_s->roc_waiting_drv_freq = 0;
5426 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005427 wpas_start_wps_enrollee(group, &res);
5428
5429 /*
5430 * Allow a longer timeout for join-a-running-group than normal 15
5431 * second group formation timeout since the GO may not have authorized
5432 * our connection yet.
5433 */
5434 eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s, NULL);
5435 eloop_register_timeout(60, 0, wpas_p2p_group_formation_timeout,
5436 wpa_s, NULL);
5437
5438 return 0;
5439}
5440
5441
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08005442static int wpas_p2p_setup_freqs(struct wpa_supplicant *wpa_s, int freq,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005443 int *force_freq, int *pref_freq, int go,
5444 unsigned int *pref_freq_list,
5445 unsigned int *num_pref_freq)
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08005446{
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07005447 struct wpa_used_freq_data *freqs;
5448 int res, best_freq, num_unused;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005449 unsigned int freq_in_use = 0, num, i, max_pref_freq;
5450
5451 max_pref_freq = *num_pref_freq;
5452 *num_pref_freq = 0;
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07005453
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07005454 freqs = os_calloc(wpa_s->num_multichan_concurrent,
5455 sizeof(struct wpa_used_freq_data));
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07005456 if (!freqs)
5457 return -1;
5458
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07005459 num = wpas_p2p_valid_oper_freqs(wpa_s, freqs,
5460 wpa_s->num_multichan_concurrent);
5461
5462 /*
5463 * It is possible that the total number of used frequencies is bigger
5464 * than the number of frequencies used for P2P, so get the system wide
5465 * number of unused frequencies.
5466 */
5467 num_unused = wpas_p2p_num_unused_channels(wpa_s);
5468
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07005469 wpa_printf(MSG_DEBUG,
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07005470 "P2P: Setup freqs: freq=%d num_MCC=%d shared_freqs=%u num_unused=%d",
5471 freq, wpa_s->num_multichan_concurrent, num, num_unused);
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07005472
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08005473 if (freq > 0) {
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07005474 int ret;
5475 if (go)
5476 ret = p2p_supported_freq(wpa_s->global->p2p, freq);
5477 else
5478 ret = p2p_supported_freq_cli(wpa_s->global->p2p, freq);
5479 if (!ret) {
Dmitry Shmidt203eadb2015-03-05 14:16:04 -08005480 if ((wpa_s->drv_flags & WPA_DRIVER_FLAGS_DFS_OFFLOAD) &&
Roshan Pius3a1667e2018-07-03 15:17:14 -07005481 ieee80211_is_dfs(freq, wpa_s->hw.modes,
5482 wpa_s->hw.num_modes)) {
Dmitry Shmidt203eadb2015-03-05 14:16:04 -08005483 /*
5484 * If freq is a DFS channel and DFS is offloaded
5485 * to the driver, allow P2P GO to use it.
5486 */
5487 wpa_printf(MSG_DEBUG,
5488 "P2P: The forced channel for GO (%u MHz) is DFS, and DFS is offloaded to the driver",
5489 freq);
5490 } else {
5491 wpa_printf(MSG_DEBUG,
5492 "P2P: The forced channel (%u MHz) is not supported for P2P uses",
5493 freq);
5494 res = -3;
5495 goto exit_free;
5496 }
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08005497 }
5498
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07005499 for (i = 0; i < num; i++) {
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07005500 if (freqs[i].freq == freq)
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07005501 freq_in_use = 1;
5502 }
5503
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07005504 if (num_unused <= 0 && !freq_in_use) {
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07005505 wpa_printf(MSG_DEBUG, "P2P: Cannot start P2P group on %u MHz as there are no available channels",
5506 freq);
5507 res = -2;
5508 goto exit_free;
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08005509 }
5510 wpa_printf(MSG_DEBUG, "P2P: Trying to force us to use the "
5511 "requested channel (%u MHz)", freq);
5512 *force_freq = freq;
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07005513 goto exit_ok;
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08005514 }
5515
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07005516 best_freq = wpas_p2p_pick_best_used_freq(wpa_s, freqs, num);
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07005517
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005518 if (!wpa_s->conf->num_p2p_pref_chan && *pref_freq == 0) {
5519 enum wpa_driver_if_type iface_type;
5520
5521 if (go)
5522 iface_type = WPA_IF_P2P_GO;
5523 else
5524 iface_type = WPA_IF_P2P_CLIENT;
5525
5526 wpa_printf(MSG_DEBUG, "P2P: best_freq=%d, go=%d",
5527 best_freq, go);
5528
5529 res = wpa_drv_get_pref_freq_list(wpa_s, iface_type,
5530 &max_pref_freq,
5531 pref_freq_list);
5532 if (!res && max_pref_freq > 0) {
5533 *num_pref_freq = max_pref_freq;
5534 i = 0;
Dmitry Shmidt29333592017-01-09 12:27:11 -08005535 while (i < *num_pref_freq &&
5536 (!p2p_supported_freq(wpa_s->global->p2p,
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08005537 pref_freq_list[i]) ||
5538 wpas_p2p_disallowed_freq(wpa_s->global,
Dmitry Shmidt29333592017-01-09 12:27:11 -08005539 pref_freq_list[i]))) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005540 wpa_printf(MSG_DEBUG,
5541 "P2P: preferred_freq_list[%d]=%d is disallowed",
5542 i, pref_freq_list[i]);
5543 i++;
5544 }
5545 if (i != *num_pref_freq) {
5546 best_freq = pref_freq_list[i];
5547 wpa_printf(MSG_DEBUG,
5548 "P2P: Using preferred_freq_list[%d]=%d",
5549 i, best_freq);
5550 } else {
5551 wpa_printf(MSG_DEBUG,
5552 "P2P: All driver preferred frequencies are disallowed for P2P use");
5553 *num_pref_freq = 0;
5554 }
5555 } else {
5556 wpa_printf(MSG_DEBUG,
5557 "P2P: No preferred frequency list available");
5558 }
5559 }
5560
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07005561 /* We have a candidate frequency to use */
5562 if (best_freq > 0) {
5563 if (*pref_freq == 0 && num_unused > 0) {
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08005564 wpa_printf(MSG_DEBUG, "P2P: Try to prefer a frequency (%u MHz) we are already using",
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07005565 best_freq);
5566 *pref_freq = best_freq;
Dmitry Shmidt51a47d52013-09-10 10:52:57 -07005567 } else {
5568 wpa_printf(MSG_DEBUG, "P2P: Try to force us to use frequency (%u MHz) which is already in use",
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07005569 best_freq);
5570 *force_freq = best_freq;
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07005571 }
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07005572 } else if (num_unused > 0) {
5573 wpa_printf(MSG_DEBUG,
5574 "P2P: Current operating channels are not available for P2P. Try to use another channel");
5575 *force_freq = 0;
5576 } else {
5577 wpa_printf(MSG_DEBUG,
5578 "P2P: All channels are in use and none of them are P2P enabled. Cannot start P2P group");
5579 res = -2;
5580 goto exit_free;
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07005581 }
5582
5583exit_ok:
5584 res = 0;
5585exit_free:
5586 os_free(freqs);
5587 return res;
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08005588}
5589
5590
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005591/**
5592 * wpas_p2p_connect - Request P2P Group Formation to be started
5593 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
5594 * @peer_addr: Address of the peer P2P Device
5595 * @pin: PIN to use during provisioning or %NULL to indicate PBC mode
5596 * @persistent_group: Whether to create a persistent group
Dmitry Shmidt04949592012-07-19 12:16:46 -07005597 * @auto_join: Whether to select join vs. GO Negotiation automatically
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005598 * @join: Whether to join an existing group (as a client) instead of starting
5599 * Group Owner negotiation; @peer_addr is BSSID in that case
5600 * @auth: Whether to only authorize the connection instead of doing that and
5601 * initiating Group Owner negotiation
5602 * @go_intent: GO Intent or -1 to use default
5603 * @freq: Frequency for the group or 0 for auto-selection
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08005604 * @freq2: Center frequency of segment 1 for the GO operating in VHT 80P80 mode
Dmitry Shmidt04949592012-07-19 12:16:46 -07005605 * @persistent_id: Persistent group credentials to use for forcing GO
5606 * parameters or -1 to generate new values (SSID/passphrase)
5607 * @pd: Whether to send Provision Discovery prior to GO Negotiation as an
5608 * interoperability workaround when initiating group formation
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005609 * @ht40: Start GO with 40 MHz channel width
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07005610 * @vht: Start GO with VHT support
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08005611 * @vht_chwidth: Channel width supported by GO operating with VHT support
Hai Shalom81f62d82019-07-22 12:10:00 -07005612 * (CHANWIDTH_*).
Dmitry Shmidtde47be72016-01-07 12:52:55 -08005613 * @group_ssid: Specific Group SSID for join or %NULL if not set
5614 * @group_ssid_len: Length of @group_ssid in octets
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005615 * Returns: 0 or new PIN (if pin was %NULL) on success, -1 on unspecified
5616 * failure, -2 on failure due to channel not currently available,
5617 * -3 if forced channel is not supported
5618 */
5619int wpas_p2p_connect(struct wpa_supplicant *wpa_s, const u8 *peer_addr,
5620 const char *pin, enum p2p_wps_method wps_method,
Dmitry Shmidt04949592012-07-19 12:16:46 -07005621 int persistent_group, int auto_join, int join, int auth,
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08005622 int go_intent, int freq, unsigned int vht_center_freq2,
5623 int persistent_id, int pd, int ht40, int vht,
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08005624 unsigned int vht_chwidth, int he, int edmg,
5625 const u8 *group_ssid, size_t group_ssid_len)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005626{
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07005627 int force_freq = 0, pref_freq = 0;
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08005628 int ret = 0, res;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005629 enum wpa_driver_if_type iftype;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005630 const u8 *if_addr;
Dmitry Shmidt04949592012-07-19 12:16:46 -07005631 struct wpa_ssid *ssid = NULL;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005632 unsigned int pref_freq_list[P2P_MAX_PREF_CHANNELS], size;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005633
5634 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
5635 return -1;
5636
Dmitry Shmidt04949592012-07-19 12:16:46 -07005637 if (persistent_id >= 0) {
5638 ssid = wpa_config_get_network(wpa_s->conf, persistent_id);
5639 if (ssid == NULL || ssid->disabled != 2 ||
5640 ssid->mode != WPAS_MODE_P2P_GO)
5641 return -1;
5642 }
5643
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07005644 os_free(wpa_s->global->add_psk);
5645 wpa_s->global->add_psk = NULL;
5646
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005647 wpa_s->global->p2p_fail_on_wps_complete = 0;
Dmitry Shmidt216983b2015-02-06 10:50:36 -08005648 wpa_s->global->pending_p2ps_group = 0;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005649 wpa_s->global->pending_p2ps_group_freq = 0;
5650 wpa_s->p2ps_method_config_any = 0;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005651
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005652 if (go_intent < 0)
5653 go_intent = wpa_s->conf->p2p_go_intent;
5654
5655 if (!auth)
Hai Shalomfdcde762020-04-02 11:19:20 -07005656 wpa_s->global->p2p_long_listen = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005657
5658 wpa_s->p2p_wps_method = wps_method;
Dmitry Shmidt04949592012-07-19 12:16:46 -07005659 wpa_s->p2p_persistent_group = !!persistent_group;
5660 wpa_s->p2p_persistent_id = persistent_id;
5661 wpa_s->p2p_go_intent = go_intent;
5662 wpa_s->p2p_connect_freq = freq;
5663 wpa_s->p2p_fallback_to_go_neg = 0;
5664 wpa_s->p2p_pd_before_go_neg = !!pd;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005665 wpa_s->p2p_go_ht40 = !!ht40;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07005666 wpa_s->p2p_go_vht = !!vht;
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08005667 wpa_s->p2p_go_vht_center_freq2 = vht_center_freq2;
5668 wpa_s->p2p_go_max_oper_chwidth = vht_chwidth;
Hai Shalom74f70d42019-02-11 14:42:39 -08005669 wpa_s->p2p_go_he = !!he;
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08005670 wpa_s->p2p_go_edmg = !!edmg;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005671
5672 if (pin)
5673 os_strlcpy(wpa_s->p2p_pin, pin, sizeof(wpa_s->p2p_pin));
5674 else if (wps_method == WPS_PIN_DISPLAY) {
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08005675 if (wps_generate_pin((unsigned int *) &ret) < 0)
5676 return -1;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005677 res = os_snprintf(wpa_s->p2p_pin, sizeof(wpa_s->p2p_pin),
5678 "%08d", ret);
5679 if (os_snprintf_error(sizeof(wpa_s->p2p_pin), res))
5680 wpa_s->p2p_pin[sizeof(wpa_s->p2p_pin) - 1] = '\0';
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005681 wpa_printf(MSG_DEBUG, "P2P: Randomly generated PIN: %s",
5682 wpa_s->p2p_pin);
Dmitry Shmidt849734c2016-05-27 09:59:01 -07005683 } else if (wps_method == WPS_P2PS) {
5684 /* Force the P2Ps default PIN to be used */
5685 os_strlcpy(wpa_s->p2p_pin, "12345670", sizeof(wpa_s->p2p_pin));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005686 } else
5687 wpa_s->p2p_pin[0] = '\0';
5688
Dmitry Shmidt04949592012-07-19 12:16:46 -07005689 if (join || auto_join) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005690 u8 iface_addr[ETH_ALEN], dev_addr[ETH_ALEN];
5691 if (auth) {
5692 wpa_printf(MSG_DEBUG, "P2P: Authorize invitation to "
5693 "connect a running group from " MACSTR,
5694 MAC2STR(peer_addr));
5695 os_memcpy(wpa_s->p2p_auth_invite, peer_addr, ETH_ALEN);
5696 return ret;
5697 }
5698 os_memcpy(dev_addr, peer_addr, ETH_ALEN);
5699 if (p2p_get_interface_addr(wpa_s->global->p2p, peer_addr,
5700 iface_addr) < 0) {
5701 os_memcpy(iface_addr, peer_addr, ETH_ALEN);
5702 p2p_get_dev_addr(wpa_s->global->p2p, peer_addr,
5703 dev_addr);
5704 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07005705 if (auto_join) {
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005706 os_get_reltime(&wpa_s->p2p_auto_started);
Dmitry Shmidt04949592012-07-19 12:16:46 -07005707 wpa_printf(MSG_DEBUG, "P2P: Auto join started at "
5708 "%ld.%06ld",
5709 wpa_s->p2p_auto_started.sec,
5710 wpa_s->p2p_auto_started.usec);
5711 }
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08005712 wpa_s->user_initiated_pd = 1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07005713 if (wpas_p2p_join(wpa_s, iface_addr, dev_addr, wps_method,
Dmitry Shmidtde47be72016-01-07 12:52:55 -08005714 auto_join, freq,
5715 group_ssid, group_ssid_len) < 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005716 return -1;
5717 return ret;
5718 }
5719
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005720 size = P2P_MAX_PREF_CHANNELS;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07005721 res = wpas_p2p_setup_freqs(wpa_s, freq, &force_freq, &pref_freq,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005722 go_intent == 15, pref_freq_list, &size);
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08005723 if (res)
5724 return res;
Dmitry Shmidta0d265f2013-11-19 13:13:41 -08005725 wpas_p2p_set_own_freq_preference(wpa_s,
5726 force_freq ? force_freq : pref_freq);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005727
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005728 p2p_set_own_pref_freq_list(wpa_s->global->p2p, pref_freq_list, size);
5729
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005730 wpa_s->create_p2p_iface = wpas_p2p_create_iface(wpa_s);
5731
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005732 if (wpa_s->create_p2p_iface) {
5733 /* Prepare to add a new interface for the group */
5734 iftype = WPA_IF_P2P_GROUP;
5735 if (go_intent == 15)
5736 iftype = WPA_IF_P2P_GO;
5737 if (wpas_p2p_add_group_interface(wpa_s, iftype) < 0) {
5738 wpa_printf(MSG_ERROR, "P2P: Failed to allocate a new "
5739 "interface for the group");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005740 return -1;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005741 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005742
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005743 if_addr = wpa_s->pending_interface_addr;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005744 } else {
Dmitry Shmidt9c175262016-03-03 10:20:07 -08005745 if (wpa_s->p2p_mgmt)
5746 if_addr = wpa_s->parent->own_addr;
5747 else
5748 if_addr = wpa_s->own_addr;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005749 os_memset(wpa_s->go_dev_addr, 0, ETH_ALEN);
5750 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005751
5752 if (auth) {
5753 if (wpas_p2p_auth_go_neg(wpa_s, peer_addr, wps_method,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005754 go_intent, if_addr,
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08005755 force_freq, persistent_group, ssid,
5756 pref_freq) < 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005757 return -1;
5758 return ret;
5759 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005760
5761 if (wpas_p2p_start_go_neg(wpa_s, peer_addr, wps_method,
5762 go_intent, if_addr, force_freq,
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08005763 persistent_group, ssid, pref_freq) < 0) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005764 if (wpa_s->create_p2p_iface)
5765 wpas_p2p_remove_pending_group_interface(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005766 return -1;
5767 }
5768 return ret;
5769}
5770
5771
5772/**
5773 * wpas_p2p_remain_on_channel_cb - Indication of remain-on-channel start
5774 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
5775 * @freq: Frequency of the channel in MHz
5776 * @duration: Duration of the stay on the channel in milliseconds
5777 *
5778 * This callback is called when the driver indicates that it has started the
5779 * requested remain-on-channel duration.
5780 */
5781void wpas_p2p_remain_on_channel_cb(struct wpa_supplicant *wpa_s,
5782 unsigned int freq, unsigned int duration)
5783{
5784 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
5785 return;
Dmitry Shmidt216983b2015-02-06 10:50:36 -08005786 wpa_printf(MSG_DEBUG, "P2P: remain-on-channel callback (off_channel_freq=%u pending_listen_freq=%d roc_waiting_drv_freq=%d freq=%u duration=%u)",
5787 wpa_s->off_channel_freq, wpa_s->pending_listen_freq,
5788 wpa_s->roc_waiting_drv_freq, freq, duration);
5789 if (wpa_s->off_channel_freq &&
5790 wpa_s->off_channel_freq == wpa_s->pending_listen_freq) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005791 p2p_listen_cb(wpa_s->global->p2p, wpa_s->pending_listen_freq,
5792 wpa_s->pending_listen_duration);
5793 wpa_s->pending_listen_freq = 0;
Dmitry Shmidt7f93d6f2014-02-21 11:22:49 -08005794 } else {
5795 wpa_printf(MSG_DEBUG, "P2P: Ignore remain-on-channel callback (off_channel_freq=%u pending_listen_freq=%d freq=%u duration=%u)",
5796 wpa_s->off_channel_freq, wpa_s->pending_listen_freq,
5797 freq, duration);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005798 }
5799}
5800
5801
Jithu Jance57cea1a2014-08-20 10:22:04 -07005802int wpas_p2p_listen_start(struct wpa_supplicant *wpa_s, unsigned int timeout)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005803{
5804 /* Limit maximum Listen state time based on driver limitation. */
5805 if (timeout > wpa_s->max_remain_on_chan)
5806 timeout = wpa_s->max_remain_on_chan;
5807
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005808 return p2p_listen(wpa_s->global->p2p, timeout);
5809}
5810
5811
5812/**
5813 * wpas_p2p_cancel_remain_on_channel_cb - Remain-on-channel timeout
5814 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
5815 * @freq: Frequency of the channel in MHz
5816 *
5817 * This callback is called when the driver indicates that a remain-on-channel
5818 * operation has been completed, i.e., the duration on the requested channel
5819 * has timed out.
5820 */
5821void wpas_p2p_cancel_remain_on_channel_cb(struct wpa_supplicant *wpa_s,
5822 unsigned int freq)
5823{
5824 wpa_printf(MSG_DEBUG, "P2P: Cancel remain-on-channel callback "
5825 "(p2p_long_listen=%d ms pending_action_tx=%p)",
Hai Shalomfdcde762020-04-02 11:19:20 -07005826 wpa_s->global->p2p_long_listen,
5827 offchannel_pending_action_tx(wpa_s));
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005828 wpas_p2p_listen_work_done(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005829 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
5830 return;
Hai Shalomfdcde762020-04-02 11:19:20 -07005831 if (wpa_s->global->p2p_long_listen > 0)
5832 wpa_s->global->p2p_long_listen -= wpa_s->max_remain_on_chan;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005833 if (p2p_listen_end(wpa_s->global->p2p, freq) > 0)
5834 return; /* P2P module started a new operation */
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08005835 if (offchannel_pending_action_tx(wpa_s))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005836 return;
Hai Shalomfdcde762020-04-02 11:19:20 -07005837 if (wpa_s->global->p2p_long_listen > 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005838 wpa_printf(MSG_DEBUG, "P2P: Continuing long Listen state");
Hai Shalomfdcde762020-04-02 11:19:20 -07005839 wpas_p2p_listen_start(wpa_s, wpa_s->global->p2p_long_listen);
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07005840 } else {
5841 /*
5842 * When listen duration is over, stop listen & update p2p_state
5843 * to IDLE.
5844 */
5845 p2p_stop_listen(wpa_s->global->p2p);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005846 }
5847}
5848
5849
5850/**
5851 * wpas_p2p_group_remove - Remove a P2P group
5852 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
5853 * @ifname: Network interface name of the group interface or "*" to remove all
5854 * groups
5855 * Returns: 0 on success, -1 on failure
5856 *
5857 * This function is used to remove a P2P group. This can be used to disconnect
5858 * from a group in which the local end is a P2P Client or to end a P2P Group in
5859 * case the local end is the Group Owner. If a virtual network interface was
5860 * created for this group, that interface will be removed. Otherwise, only the
5861 * configured P2P group network will be removed from the interface.
5862 */
5863int wpas_p2p_group_remove(struct wpa_supplicant *wpa_s, const char *ifname)
5864{
5865 struct wpa_global *global = wpa_s->global;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005866 struct wpa_supplicant *calling_wpa_s = wpa_s;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005867
5868 if (os_strcmp(ifname, "*") == 0) {
5869 struct wpa_supplicant *prev;
5870 wpa_s = global->ifaces;
5871 while (wpa_s) {
5872 prev = wpa_s;
5873 wpa_s = wpa_s->next;
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07005874 if (prev->p2p_group_interface !=
5875 NOT_P2P_GROUP_INTERFACE ||
5876 (prev->current_ssid &&
5877 prev->current_ssid->p2p_group))
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005878 wpas_p2p_disconnect_safely(prev, calling_wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005879 }
5880 return 0;
5881 }
5882
5883 for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
5884 if (os_strcmp(wpa_s->ifname, ifname) == 0)
5885 break;
5886 }
5887
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005888 return wpas_p2p_disconnect_safely(wpa_s, calling_wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005889}
5890
5891
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07005892static int wpas_p2p_select_go_freq(struct wpa_supplicant *wpa_s, int freq)
5893{
5894 unsigned int r;
5895
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005896 if (!wpa_s->conf->num_p2p_pref_chan && !freq) {
5897 unsigned int i, size = P2P_MAX_PREF_CHANNELS;
5898 unsigned int pref_freq_list[P2P_MAX_PREF_CHANNELS];
5899 int res;
5900
5901 res = wpa_drv_get_pref_freq_list(wpa_s, WPA_IF_P2P_GO,
5902 &size, pref_freq_list);
5903 if (!res && size > 0) {
5904 i = 0;
Dmitry Shmidt29333592017-01-09 12:27:11 -08005905 while (i < size &&
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07005906 (!p2p_supported_freq(wpa_s->global->p2p,
5907 pref_freq_list[i]) ||
5908 wpas_p2p_disallowed_freq(wpa_s->global,
5909 pref_freq_list[i]))) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005910 wpa_printf(MSG_DEBUG,
5911 "P2P: preferred_freq_list[%d]=%d is disallowed",
5912 i, pref_freq_list[i]);
5913 i++;
5914 }
5915 if (i != size) {
5916 freq = pref_freq_list[i];
5917 wpa_printf(MSG_DEBUG,
5918 "P2P: Using preferred_freq_list[%d]=%d",
5919 i, freq);
5920 } else {
5921 wpa_printf(MSG_DEBUG,
5922 "P2P: All driver preferred frequencies are disallowed for P2P use");
5923 }
5924 } else {
5925 wpa_printf(MSG_DEBUG,
5926 "P2P: No preferred frequency list available");
5927 }
5928 }
5929
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07005930 if (freq == 2) {
5931 wpa_printf(MSG_DEBUG, "P2P: Request to start GO on 2.4 GHz "
5932 "band");
5933 if (wpa_s->best_24_freq > 0 &&
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07005934 p2p_supported_freq_go(wpa_s->global->p2p,
5935 wpa_s->best_24_freq)) {
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07005936 freq = wpa_s->best_24_freq;
5937 wpa_printf(MSG_DEBUG, "P2P: Use best 2.4 GHz band "
5938 "channel: %d MHz", freq);
5939 } else {
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07005940 if (os_get_random((u8 *) &r, sizeof(r)) < 0)
5941 return -1;
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07005942 freq = 2412 + (r % 3) * 25;
5943 wpa_printf(MSG_DEBUG, "P2P: Use random 2.4 GHz band "
5944 "channel: %d MHz", freq);
5945 }
5946 }
5947
5948 if (freq == 5) {
5949 wpa_printf(MSG_DEBUG, "P2P: Request to start GO on 5 GHz "
5950 "band");
5951 if (wpa_s->best_5_freq > 0 &&
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07005952 p2p_supported_freq_go(wpa_s->global->p2p,
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07005953 wpa_s->best_5_freq)) {
5954 freq = wpa_s->best_5_freq;
5955 wpa_printf(MSG_DEBUG, "P2P: Use best 5 GHz band "
5956 "channel: %d MHz", freq);
5957 } else {
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07005958 if (os_get_random((u8 *) &r, sizeof(r)) < 0)
5959 return -1;
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07005960 freq = 5180 + (r % 4) * 20;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07005961 if (!p2p_supported_freq_go(wpa_s->global->p2p, freq)) {
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07005962 wpa_printf(MSG_DEBUG, "P2P: Could not select "
5963 "5 GHz channel for P2P group");
5964 return -1;
5965 }
5966 wpa_printf(MSG_DEBUG, "P2P: Use random 5 GHz band "
5967 "channel: %d MHz", freq);
5968 }
5969 }
5970
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07005971 if (freq > 0 && !p2p_supported_freq_go(wpa_s->global->p2p, freq)) {
Dmitry Shmidt203eadb2015-03-05 14:16:04 -08005972 if ((wpa_s->drv_flags & WPA_DRIVER_FLAGS_DFS_OFFLOAD) &&
Roshan Pius3a1667e2018-07-03 15:17:14 -07005973 ieee80211_is_dfs(freq, wpa_s->hw.modes,
5974 wpa_s->hw.num_modes)) {
Dmitry Shmidt203eadb2015-03-05 14:16:04 -08005975 /*
5976 * If freq is a DFS channel and DFS is offloaded to the
5977 * driver, allow P2P GO to use it.
5978 */
5979 wpa_printf(MSG_DEBUG, "P2P: "
5980 "%s: The forced channel for GO (%u MHz) is DFS, and DFS is offloaded",
5981 __func__, freq);
5982 return freq;
5983 }
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07005984 wpa_printf(MSG_DEBUG, "P2P: The forced channel for GO "
5985 "(%u MHz) is not supported for P2P uses",
5986 freq);
5987 return -1;
5988 }
5989
5990 return freq;
5991}
5992
5993
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005994static int wpas_p2p_supported_freq_go(struct wpa_supplicant *wpa_s,
5995 const struct p2p_channels *channels,
5996 int freq)
5997{
5998 if (!wpas_p2p_disallowed_freq(wpa_s->global, freq) &&
5999 p2p_supported_freq_go(wpa_s->global->p2p, freq) &&
6000 freq_included(wpa_s, channels, freq))
6001 return 1;
6002 return 0;
6003}
6004
6005
6006static void wpas_p2p_select_go_freq_no_pref(struct wpa_supplicant *wpa_s,
6007 struct p2p_go_neg_results *params,
6008 const struct p2p_channels *channels)
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07006009{
6010 unsigned int i, r;
6011
Dmitry Shmidt203eadb2015-03-05 14:16:04 -08006012 /* try all channels in operating class 115 */
6013 for (i = 0; i < 4; i++) {
6014 params->freq = 5180 + i * 20;
6015 if (!wpas_p2p_disallowed_freq(wpa_s->global, params->freq) &&
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006016 freq_included(wpa_s, channels, params->freq) &&
Dmitry Shmidt203eadb2015-03-05 14:16:04 -08006017 p2p_supported_freq(wpa_s->global->p2p, params->freq))
6018 goto out;
6019 }
6020
6021 /* try all channels in operating class 124 */
6022 for (i = 0; i < 4; i++) {
6023 params->freq = 5745 + i * 20;
6024 if (!wpas_p2p_disallowed_freq(wpa_s->global, params->freq) &&
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006025 freq_included(wpa_s, channels, params->freq) &&
Dmitry Shmidt203eadb2015-03-05 14:16:04 -08006026 p2p_supported_freq(wpa_s->global->p2p, params->freq))
6027 goto out;
6028 }
6029
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07006030 /* try social channel class 180 channel 2 */
6031 params->freq = 58320 + 1 * 2160;
6032 if (!wpas_p2p_disallowed_freq(wpa_s->global, params->freq) &&
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006033 freq_included(wpa_s, channels, params->freq) &&
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07006034 p2p_supported_freq(wpa_s->global->p2p, params->freq))
6035 goto out;
6036
6037 /* try all channels in reg. class 180 */
6038 for (i = 0; i < 4; i++) {
6039 params->freq = 58320 + i * 2160;
6040 if (!wpas_p2p_disallowed_freq(wpa_s->global, params->freq) &&
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006041 freq_included(wpa_s, channels, params->freq) &&
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07006042 p2p_supported_freq(wpa_s->global->p2p, params->freq))
6043 goto out;
6044 }
6045
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07006046 /* try some random selection of the social channels */
6047 if (os_get_random((u8 *) &r, sizeof(r)) < 0)
6048 return;
6049
6050 for (i = 0; i < 3; i++) {
6051 params->freq = 2412 + ((r + i) % 3) * 25;
6052 if (wpas_p2p_supported_freq_go(wpa_s, channels, params->freq))
6053 goto out;
6054 }
6055
6056 /* try all other channels in operating class 81 */
6057 for (i = 0; i < 11; i++) {
6058 params->freq = 2412 + i * 5;
6059
6060 /* skip social channels; covered in the previous loop */
6061 if (params->freq == 2412 ||
6062 params->freq == 2437 ||
6063 params->freq == 2462)
6064 continue;
6065
6066 if (wpas_p2p_supported_freq_go(wpa_s, channels, params->freq))
6067 goto out;
6068 }
6069
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006070 params->freq = 0;
Dmitry Shmidt203eadb2015-03-05 14:16:04 -08006071 wpa_printf(MSG_DEBUG, "P2P: No 2.4, 5, or 60 GHz channel allowed");
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006072 return;
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07006073out:
6074 wpa_printf(MSG_DEBUG, "P2P: Set GO freq %d MHz (no preference known)",
6075 params->freq);
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07006076}
6077
6078
Roshan Pius3a1667e2018-07-03 15:17:14 -07006079static int wpas_same_band(int freq1, int freq2)
6080{
6081 enum hostapd_hw_mode mode1, mode2;
6082 u8 chan1, chan2;
6083
6084 mode1 = ieee80211_freq_to_chan(freq1, &chan1);
6085 mode2 = ieee80211_freq_to_chan(freq2, &chan2);
6086 if (mode1 == NUM_HOSTAPD_MODES)
6087 return 0;
6088 return mode1 == mode2;
6089}
6090
6091
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006092static int wpas_p2p_init_go_params(struct wpa_supplicant *wpa_s,
6093 struct p2p_go_neg_results *params,
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08006094 int freq, int vht_center_freq2, int ht40,
Hai Shalom74f70d42019-02-11 14:42:39 -08006095 int vht, int max_oper_chwidth, int he,
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08006096 int edmg,
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08006097 const struct p2p_channels *channels)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006098{
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07006099 struct wpa_used_freq_data *freqs;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006100 unsigned int cand;
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07006101 unsigned int num, i;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006102 int ignore_no_freqs = 0;
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08006103 int unused_channels = wpas_p2p_num_unused_channels(wpa_s) > 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006104
6105 os_memset(params, 0, sizeof(*params));
6106 params->role_go = 1;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07006107 params->ht40 = ht40;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07006108 params->vht = vht;
Hai Shalom74f70d42019-02-11 14:42:39 -08006109 params->he = he;
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08006110 params->max_oper_chwidth = max_oper_chwidth;
6111 params->vht_center_freq2 = vht_center_freq2;
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08006112 params->edmg = edmg;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006113
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07006114 freqs = os_calloc(wpa_s->num_multichan_concurrent,
6115 sizeof(struct wpa_used_freq_data));
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07006116 if (!freqs)
6117 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006118
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006119 num = get_shared_radio_freqs_data(wpa_s, freqs,
6120 wpa_s->num_multichan_concurrent);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006121
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006122 if (wpa_s->current_ssid &&
6123 wpa_s->current_ssid->mode == WPAS_MODE_P2P_GO &&
6124 wpa_s->wpa_state == WPA_COMPLETED) {
6125 wpa_printf(MSG_DEBUG, "P2P: %s called for an active GO",
6126 __func__);
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07006127
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006128 /*
6129 * If the frequency selection is done for an active P2P GO that
6130 * is not sharing a frequency, allow to select a new frequency
6131 * even if there are no unused frequencies as we are about to
6132 * move the P2P GO so its frequency can be re-used.
6133 */
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07006134 for (i = 0; i < num; i++) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006135 if (freqs[i].freq == wpa_s->current_ssid->frequency &&
6136 freqs[i].flags == 0) {
6137 ignore_no_freqs = 1;
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07006138 break;
6139 }
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07006140 }
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08006141 }
6142
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08006143 /* Try to use EDMG channel */
6144 if (params->edmg) {
6145 if (wpas_p2p_try_edmg_channel(wpa_s, params) == 0)
6146 goto success;
6147 params->edmg = 0;
6148 }
6149
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006150 /* try using the forced freq */
6151 if (freq) {
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07006152 if (wpas_p2p_disallowed_freq(wpa_s->global, freq) ||
6153 !freq_included(wpa_s, channels, freq)) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006154 wpa_printf(MSG_DEBUG,
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07006155 "P2P: Forced GO freq %d MHz disallowed",
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006156 freq);
6157 goto fail;
6158 }
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07006159 if (!p2p_supported_freq_go(wpa_s->global->p2p, freq)) {
6160 if ((wpa_s->drv_flags & WPA_DRIVER_FLAGS_DFS_OFFLOAD) &&
Roshan Pius3a1667e2018-07-03 15:17:14 -07006161 ieee80211_is_dfs(freq, wpa_s->hw.modes,
6162 wpa_s->hw.num_modes)) {
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07006163 /*
6164 * If freq is a DFS channel and DFS is offloaded
6165 * to the driver, allow P2P GO to use it.
6166 */
6167 wpa_printf(MSG_DEBUG,
6168 "P2P: %s: The forced channel for GO (%u MHz) requires DFS and DFS is offloaded",
6169 __func__, freq);
6170 } else {
6171 wpa_printf(MSG_DEBUG,
6172 "P2P: The forced channel for GO (%u MHz) is not supported for P2P uses",
6173 freq);
6174 goto fail;
6175 }
6176 }
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006177
6178 for (i = 0; i < num; i++) {
6179 if (freqs[i].freq == freq) {
6180 wpa_printf(MSG_DEBUG,
6181 "P2P: forced freq (%d MHz) is also shared",
6182 freq);
6183 params->freq = freq;
6184 goto success;
6185 }
6186 }
6187
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08006188 if (!ignore_no_freqs && !unused_channels) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006189 wpa_printf(MSG_DEBUG,
6190 "P2P: Cannot force GO on freq (%d MHz) as all the channels are in use",
6191 freq);
6192 goto fail;
6193 }
6194
6195 wpa_printf(MSG_DEBUG,
6196 "P2P: force GO freq (%d MHz) on a free channel",
6197 freq);
6198 params->freq = freq;
6199 goto success;
6200 }
6201
6202 /* consider using one of the shared frequencies */
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08006203 if (num &&
6204 (!wpa_s->conf->p2p_ignore_shared_freq || !unused_channels)) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006205 cand = wpas_p2p_pick_best_used_freq(wpa_s, freqs, num);
6206 if (wpas_p2p_supported_freq_go(wpa_s, channels, cand)) {
6207 wpa_printf(MSG_DEBUG,
6208 "P2P: Use shared freq (%d MHz) for GO",
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08006209 cand);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006210 params->freq = cand;
6211 goto success;
6212 }
6213
6214 /* try using one of the shared freqs */
6215 for (i = 0; i < num; i++) {
6216 if (wpas_p2p_supported_freq_go(wpa_s, channels,
6217 freqs[i].freq)) {
6218 wpa_printf(MSG_DEBUG,
6219 "P2P: Use shared freq (%d MHz) for GO",
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08006220 freqs[i].freq);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006221 params->freq = freqs[i].freq;
6222 goto success;
6223 }
6224 }
6225 }
6226
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08006227 if (!ignore_no_freqs && !unused_channels) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006228 wpa_printf(MSG_DEBUG,
6229 "P2P: Cannot force GO on any of the channels we are already using");
6230 goto fail;
6231 }
6232
6233 /* try using the setting from the configuration file */
6234 if (wpa_s->conf->p2p_oper_reg_class == 81 &&
6235 wpa_s->conf->p2p_oper_channel >= 1 &&
6236 wpa_s->conf->p2p_oper_channel <= 11 &&
6237 wpas_p2p_supported_freq_go(
6238 wpa_s, channels,
6239 2407 + 5 * wpa_s->conf->p2p_oper_channel)) {
6240 params->freq = 2407 + 5 * wpa_s->conf->p2p_oper_channel;
6241 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on configured "
6242 "frequency %d MHz", params->freq);
6243 goto success;
6244 }
6245
6246 if ((wpa_s->conf->p2p_oper_reg_class == 115 ||
6247 wpa_s->conf->p2p_oper_reg_class == 116 ||
6248 wpa_s->conf->p2p_oper_reg_class == 117 ||
6249 wpa_s->conf->p2p_oper_reg_class == 124 ||
6250 wpa_s->conf->p2p_oper_reg_class == 125 ||
6251 wpa_s->conf->p2p_oper_reg_class == 126 ||
6252 wpa_s->conf->p2p_oper_reg_class == 127) &&
6253 wpas_p2p_supported_freq_go(wpa_s, channels,
6254 5000 +
6255 5 * wpa_s->conf->p2p_oper_channel)) {
6256 params->freq = 5000 + 5 * wpa_s->conf->p2p_oper_channel;
6257 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on configured "
6258 "frequency %d MHz", params->freq);
6259 goto success;
6260 }
6261
6262 /* Try using best channels */
6263 if (wpa_s->conf->p2p_oper_channel == 0 &&
6264 wpa_s->best_overall_freq > 0 &&
6265 wpas_p2p_supported_freq_go(wpa_s, channels,
6266 wpa_s->best_overall_freq)) {
6267 params->freq = wpa_s->best_overall_freq;
6268 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on best overall "
6269 "channel %d MHz", params->freq);
6270 goto success;
6271 }
6272
6273 if (wpa_s->conf->p2p_oper_channel == 0 &&
6274 wpa_s->best_24_freq > 0 &&
6275 wpas_p2p_supported_freq_go(wpa_s, channels,
6276 wpa_s->best_24_freq)) {
6277 params->freq = wpa_s->best_24_freq;
6278 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on best 2.4 GHz "
6279 "channel %d MHz", params->freq);
6280 goto success;
6281 }
6282
6283 if (wpa_s->conf->p2p_oper_channel == 0 &&
6284 wpa_s->best_5_freq > 0 &&
6285 wpas_p2p_supported_freq_go(wpa_s, channels,
6286 wpa_s->best_5_freq)) {
6287 params->freq = wpa_s->best_5_freq;
6288 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on best 5 GHz "
6289 "channel %d MHz", params->freq);
6290 goto success;
6291 }
6292
6293 /* try using preferred channels */
6294 cand = p2p_get_pref_freq(wpa_s->global->p2p, channels);
6295 if (cand && wpas_p2p_supported_freq_go(wpa_s, channels, cand)) {
6296 params->freq = cand;
6297 wpa_printf(MSG_DEBUG, "P2P: Set GO freq %d MHz from preferred "
6298 "channels", params->freq);
6299 goto success;
6300 }
6301
Roshan Pius3a1667e2018-07-03 15:17:14 -07006302 /* Try using a channel that allows VHT to be used with 80 MHz */
6303 if (wpa_s->hw.modes && wpa_s->p2p_group_common_freqs) {
6304 for (i = 0; i < wpa_s->p2p_group_common_freqs_num; i++) {
6305 enum hostapd_hw_mode mode;
6306 struct hostapd_hw_modes *hwmode;
6307 u8 chan;
6308
6309 cand = wpa_s->p2p_group_common_freqs[i];
6310 mode = ieee80211_freq_to_chan(cand, &chan);
6311 hwmode = get_mode(wpa_s->hw.modes, wpa_s->hw.num_modes,
Hai Shalomfdcde762020-04-02 11:19:20 -07006312 mode, is_6ghz_freq(cand));
Roshan Pius3a1667e2018-07-03 15:17:14 -07006313 if (!hwmode ||
6314 wpas_p2p_verify_channel(wpa_s, hwmode, chan,
6315 BW80) != ALLOWED)
6316 continue;
6317 if (wpas_p2p_supported_freq_go(wpa_s, channels, cand)) {
6318 params->freq = cand;
6319 wpa_printf(MSG_DEBUG,
6320 "P2P: Use freq %d MHz common with the peer and allowing VHT80",
6321 params->freq);
6322 goto success;
6323 }
6324 }
6325 }
6326
6327 /* Try using a channel that allows HT to be used with 40 MHz on the same
6328 * band so that CSA can be used */
6329 if (wpa_s->current_ssid && wpa_s->hw.modes &&
6330 wpa_s->p2p_group_common_freqs) {
6331 for (i = 0; i < wpa_s->p2p_group_common_freqs_num; i++) {
6332 enum hostapd_hw_mode mode;
6333 struct hostapd_hw_modes *hwmode;
6334 u8 chan;
6335
6336 cand = wpa_s->p2p_group_common_freqs[i];
6337 mode = ieee80211_freq_to_chan(cand, &chan);
6338 hwmode = get_mode(wpa_s->hw.modes, wpa_s->hw.num_modes,
Hai Shalomfdcde762020-04-02 11:19:20 -07006339 mode, is_6ghz_freq(cand));
Roshan Pius3a1667e2018-07-03 15:17:14 -07006340 if (!wpas_same_band(wpa_s->current_ssid->frequency,
6341 cand) ||
6342 !hwmode ||
6343 (wpas_p2p_verify_channel(wpa_s, hwmode, chan,
6344 BW40MINUS) != ALLOWED &&
6345 wpas_p2p_verify_channel(wpa_s, hwmode, chan,
6346 BW40PLUS) != ALLOWED))
6347 continue;
6348 if (wpas_p2p_supported_freq_go(wpa_s, channels, cand)) {
6349 params->freq = cand;
6350 wpa_printf(MSG_DEBUG,
6351 "P2P: Use freq %d MHz common with the peer, allowing HT40, and maintaining same band",
6352 params->freq);
6353 goto success;
6354 }
6355 }
6356 }
6357
6358 /* Try using one of the group common freqs on the same band so that CSA
6359 * can be used */
6360 if (wpa_s->current_ssid && wpa_s->p2p_group_common_freqs) {
6361 for (i = 0; i < wpa_s->p2p_group_common_freqs_num; i++) {
6362 cand = wpa_s->p2p_group_common_freqs[i];
6363 if (!wpas_same_band(wpa_s->current_ssid->frequency,
6364 cand))
6365 continue;
6366 if (wpas_p2p_supported_freq_go(wpa_s, channels, cand)) {
6367 params->freq = cand;
6368 wpa_printf(MSG_DEBUG,
6369 "P2P: Use freq %d MHz common with the peer and maintaining same band",
6370 params->freq);
6371 goto success;
6372 }
6373 }
6374 }
6375
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006376 /* Try using one of the group common freqs */
6377 if (wpa_s->p2p_group_common_freqs) {
6378 for (i = 0; i < wpa_s->p2p_group_common_freqs_num; i++) {
6379 cand = wpa_s->p2p_group_common_freqs[i];
6380 if (wpas_p2p_supported_freq_go(wpa_s, channels, cand)) {
6381 params->freq = cand;
6382 wpa_printf(MSG_DEBUG,
6383 "P2P: Use freq %d MHz common with the peer",
6384 params->freq);
6385 goto success;
6386 }
6387 }
6388 }
6389
6390 /* no preference, select some channel */
6391 wpas_p2p_select_go_freq_no_pref(wpa_s, params, channels);
6392
6393 if (params->freq == 0) {
6394 wpa_printf(MSG_DEBUG, "P2P: did not find a freq for GO use");
6395 goto fail;
6396 }
6397
6398success:
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07006399 os_free(freqs);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006400 return 0;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006401fail:
6402 os_free(freqs);
6403 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006404}
6405
6406
6407static struct wpa_supplicant *
6408wpas_p2p_get_group_iface(struct wpa_supplicant *wpa_s, int addr_allocated,
6409 int go)
6410{
6411 struct wpa_supplicant *group_wpa_s;
6412
Dmitry Shmidtaa532512012-09-24 10:35:31 -07006413 if (!wpas_p2p_create_iface(wpa_s)) {
Dmitry Shmidt9c175262016-03-03 10:20:07 -08006414 if (wpa_s->p2p_mgmt) {
6415 /*
6416 * We may be called on the p2p_dev interface which
6417 * cannot be used for group operations, so always use
6418 * the primary interface.
6419 */
6420 wpa_s->parent->p2pdev = wpa_s;
6421 wpa_s = wpa_s->parent;
6422 }
6423 wpa_dbg(wpa_s, MSG_DEBUG,
6424 "P2P: Use primary interface for group operations");
Dmitry Shmidt56052862013-10-04 10:23:25 -07006425 wpa_s->p2p_first_connection_timeout = 0;
Dmitry Shmidt9c175262016-03-03 10:20:07 -08006426 if (wpa_s != wpa_s->p2pdev)
Dmitry Shmidt849734c2016-05-27 09:59:01 -07006427 wpas_p2p_clone_config(wpa_s, wpa_s->p2pdev);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006428 return wpa_s;
Dmitry Shmidtaa532512012-09-24 10:35:31 -07006429 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006430
6431 if (wpas_p2p_add_group_interface(wpa_s, go ? WPA_IF_P2P_GO :
Dmitry Shmidtaa532512012-09-24 10:35:31 -07006432 WPA_IF_P2P_CLIENT) < 0) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07006433 wpa_msg_global(wpa_s, MSG_ERROR,
6434 "P2P: Failed to add group interface");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006435 return NULL;
Dmitry Shmidtaa532512012-09-24 10:35:31 -07006436 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006437 group_wpa_s = wpas_p2p_init_group_interface(wpa_s, go);
6438 if (group_wpa_s == NULL) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07006439 wpa_msg_global(wpa_s, MSG_ERROR,
6440 "P2P: Failed to initialize group interface");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006441 wpas_p2p_remove_pending_group_interface(wpa_s);
6442 return NULL;
6443 }
6444
Roshan Pius3a1667e2018-07-03 15:17:14 -07006445 if (go && wpa_s->p2p_go_do_acs) {
6446 group_wpa_s->p2p_go_do_acs = wpa_s->p2p_go_do_acs;
6447 group_wpa_s->p2p_go_acs_band = wpa_s->p2p_go_acs_band;
6448 wpa_s->p2p_go_do_acs = 0;
6449 }
6450
Dmitry Shmidtaa532512012-09-24 10:35:31 -07006451 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Use separate group interface %s",
6452 group_wpa_s->ifname);
Dmitry Shmidt56052862013-10-04 10:23:25 -07006453 group_wpa_s->p2p_first_connection_timeout = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006454 return group_wpa_s;
6455}
6456
6457
6458/**
6459 * wpas_p2p_group_add - Add a new P2P group with local end as Group Owner
6460 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
6461 * @persistent_group: Whether to create a persistent group
6462 * @freq: Frequency for the group or 0 to indicate no hardcoding
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08006463 * @vht_center_freq2: segment_1 center frequency for GO operating in VHT 80P80
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07006464 * @ht40: Start GO with 40 MHz channel width
6465 * @vht: Start GO with VHT support
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08006466 * @vht_chwidth: channel bandwidth for GO operating with VHT support
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08006467 * @edmg: Start GO with EDMG support
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006468 * Returns: 0 on success, -1 on failure
6469 *
6470 * This function creates a new P2P group with the local end as the Group Owner,
6471 * i.e., without using Group Owner Negotiation.
6472 */
6473int wpas_p2p_group_add(struct wpa_supplicant *wpa_s, int persistent_group,
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08006474 int freq, int vht_center_freq2, int ht40, int vht,
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08006475 int max_oper_chwidth, int he, int edmg)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006476{
6477 struct p2p_go_neg_results params;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006478
6479 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
6480 return -1;
6481
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07006482 os_free(wpa_s->global->add_psk);
6483 wpa_s->global->add_psk = NULL;
6484
Dmitry Shmidtdca39792011-09-06 11:17:33 -07006485 /* Make sure we are not running find during connection establishment */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006486 wpa_printf(MSG_DEBUG, "P2P: Stop any on-going P2P FIND");
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08006487 wpas_p2p_stop_find_oper(wpa_s);
Dmitry Shmidtdca39792011-09-06 11:17:33 -07006488
Roshan Pius3a1667e2018-07-03 15:17:14 -07006489 if (!wpa_s->p2p_go_do_acs) {
6490 freq = wpas_p2p_select_go_freq(wpa_s, freq);
6491 if (freq < 0)
6492 return -1;
6493 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006494
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08006495 if (wpas_p2p_init_go_params(wpa_s, &params, freq, vht_center_freq2,
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08006496 ht40, vht, max_oper_chwidth, he, edmg,
6497 NULL))
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006498 return -1;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07006499
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006500 p2p_go_params(wpa_s->global->p2p, &params);
6501 params.persistent_group = persistent_group;
6502
6503 wpa_s = wpas_p2p_get_group_iface(wpa_s, 0, 1);
6504 if (wpa_s == NULL)
6505 return -1;
6506 wpas_start_wps_go(wpa_s, &params, 0);
6507
6508 return 0;
6509}
6510
6511
6512static int wpas_start_p2p_client(struct wpa_supplicant *wpa_s,
Dmitry Shmidt15907092014-03-25 10:42:57 -07006513 struct wpa_ssid *params, int addr_allocated,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006514 int freq, int force_scan)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006515{
6516 struct wpa_ssid *ssid;
6517
6518 wpa_s = wpas_p2p_get_group_iface(wpa_s, addr_allocated, 0);
6519 if (wpa_s == NULL)
6520 return -1;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006521 if (force_scan)
6522 os_get_reltime(&wpa_s->scan_min_time);
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07006523 wpa_s->p2p_last_4way_hs_fail = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006524
6525 wpa_supplicant_ap_deinit(wpa_s);
6526
6527 ssid = wpa_config_add_network(wpa_s->conf);
6528 if (ssid == NULL)
6529 return -1;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006530 os_memset(wpa_s->go_dev_addr, 0, ETH_ALEN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006531 wpa_config_set_network_defaults(ssid);
6532 ssid->temporary = 1;
6533 ssid->proto = WPA_PROTO_RSN;
Dmitry Shmidte4663042016-04-04 10:07:49 -07006534 ssid->pbss = params->pbss;
6535 ssid->pairwise_cipher = params->pbss ? WPA_CIPHER_GCMP :
6536 WPA_CIPHER_CCMP;
6537 ssid->group_cipher = params->pbss ? WPA_CIPHER_GCMP : WPA_CIPHER_CCMP;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006538 ssid->key_mgmt = WPA_KEY_MGMT_PSK;
6539 ssid->ssid = os_malloc(params->ssid_len);
6540 if (ssid->ssid == NULL) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006541 wpa_config_remove_network(wpa_s->conf, ssid->id);
6542 return -1;
6543 }
6544 os_memcpy(ssid->ssid, params->ssid, params->ssid_len);
6545 ssid->ssid_len = params->ssid_len;
6546 ssid->p2p_group = 1;
6547 ssid->export_keys = 1;
6548 if (params->psk_set) {
6549 os_memcpy(ssid->psk, params->psk, 32);
6550 ssid->psk_set = 1;
6551 }
6552 if (params->passphrase)
6553 ssid->passphrase = os_strdup(params->passphrase);
6554
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006555 wpa_s->show_group_started = 1;
Dmitry Shmidt15907092014-03-25 10:42:57 -07006556 wpa_s->p2p_in_invitation = 1;
6557 wpa_s->p2p_invite_go_freq = freq;
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08006558 wpa_s->p2p_go_group_formation_completed = 0;
6559 wpa_s->global->p2p_group_formation = wpa_s;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006560
Dmitry Shmidt9c175262016-03-03 10:20:07 -08006561 eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s->p2pdev,
Dmitry Shmidt15907092014-03-25 10:42:57 -07006562 NULL);
6563 eloop_register_timeout(P2P_MAX_INITIAL_CONN_WAIT, 0,
6564 wpas_p2p_group_formation_timeout,
Dmitry Shmidt9c175262016-03-03 10:20:07 -08006565 wpa_s->p2pdev, NULL);
Dmitry Shmidtfa3fc4a2013-11-21 13:34:38 -08006566 wpa_supplicant_select_network(wpa_s, ssid);
6567
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006568 return 0;
6569}
6570
6571
6572int wpas_p2p_group_add_persistent(struct wpa_supplicant *wpa_s,
6573 struct wpa_ssid *ssid, int addr_allocated,
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08006574 int force_freq, int neg_freq,
6575 int vht_center_freq2, int ht40,
Hai Shalom74f70d42019-02-11 14:42:39 -08006576 int vht, int max_oper_chwidth, int he,
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08006577 int edmg,
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08006578 const struct p2p_channels *channels,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006579 int connection_timeout, int force_scan)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006580{
6581 struct p2p_go_neg_results params;
Dmitry Shmidt96be6222014-02-13 10:16:51 -08006582 int go = 0, freq;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006583
6584 if (ssid->disabled != 2 || ssid->ssid == NULL)
6585 return -1;
6586
6587 if (wpas_get_p2p_group(wpa_s, ssid->ssid, ssid->ssid_len, &go) &&
6588 go == (ssid->mode == WPAS_MODE_P2P_GO)) {
6589 wpa_printf(MSG_DEBUG, "P2P: Requested persistent group is "
6590 "already running");
Dmitry Shmidtb1e52102015-05-29 12:36:29 -07006591 if (go == 0 &&
6592 eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
Dmitry Shmidt9c175262016-03-03 10:20:07 -08006593 wpa_s->p2pdev, NULL)) {
Dmitry Shmidtb1e52102015-05-29 12:36:29 -07006594 /*
6595 * This can happen if Invitation Response frame was lost
6596 * and the peer (GO of a persistent group) tries to
6597 * invite us again. Reschedule the timeout to avoid
6598 * terminating the wait for the connection too early
6599 * since we now know that the peer is still trying to
6600 * invite us instead of having already started the GO.
6601 */
6602 wpa_printf(MSG_DEBUG,
6603 "P2P: Reschedule group formation timeout since peer is still trying to invite us");
6604 eloop_register_timeout(P2P_MAX_INITIAL_CONN_WAIT, 0,
6605 wpas_p2p_group_formation_timeout,
Dmitry Shmidt9c175262016-03-03 10:20:07 -08006606 wpa_s->p2pdev, NULL);
Dmitry Shmidtb1e52102015-05-29 12:36:29 -07006607 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006608 return 0;
6609 }
6610
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07006611 os_free(wpa_s->global->add_psk);
6612 wpa_s->global->add_psk = NULL;
6613
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006614 /* Make sure we are not running find during connection establishment */
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08006615 wpas_p2p_stop_find_oper(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006616
Dmitry Shmidt04949592012-07-19 12:16:46 -07006617 wpa_s->p2p_fallback_to_go_neg = 0;
6618
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006619 if (ssid->mode == WPAS_MODE_P2P_GO) {
6620 if (force_freq > 0) {
6621 freq = wpas_p2p_select_go_freq(wpa_s, force_freq);
6622 if (freq < 0)
6623 return -1;
6624 } else {
6625 freq = wpas_p2p_select_go_freq(wpa_s, neg_freq);
6626 if (freq < 0 ||
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006627 (freq > 0 && !freq_included(wpa_s, channels, freq)))
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006628 freq = 0;
6629 }
Dmitry Shmidt912c6ec2015-03-30 13:16:51 -07006630 } else if (ssid->mode == WPAS_MODE_INFRA) {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006631 freq = neg_freq;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006632 if (freq <= 0 || !freq_included(wpa_s, channels, freq)) {
Dmitry Shmidt912c6ec2015-03-30 13:16:51 -07006633 struct os_reltime now;
6634 struct wpa_bss *bss =
6635 wpa_bss_get_p2p_dev_addr(wpa_s, ssid->bssid);
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07006636
Dmitry Shmidt912c6ec2015-03-30 13:16:51 -07006637 os_get_reltime(&now);
6638 if (bss &&
6639 !os_reltime_expired(&now, &bss->last_update, 5) &&
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006640 freq_included(wpa_s, channels, bss->freq))
Dmitry Shmidt912c6ec2015-03-30 13:16:51 -07006641 freq = bss->freq;
6642 else
6643 freq = 0;
6644 }
6645
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006646 return wpas_start_p2p_client(wpa_s, ssid, addr_allocated, freq,
6647 force_scan);
Dmitry Shmidt912c6ec2015-03-30 13:16:51 -07006648 } else {
Dmitry Shmidt15907092014-03-25 10:42:57 -07006649 return -1;
Dmitry Shmidt912c6ec2015-03-30 13:16:51 -07006650 }
Dmitry Shmidt15907092014-03-25 10:42:57 -07006651
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08006652 if (wpas_p2p_init_go_params(wpa_s, &params, freq, vht_center_freq2,
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08006653 ht40, vht, max_oper_chwidth, he, edmg,
6654 channels))
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006655 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006656
6657 params.role_go = 1;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08006658 params.psk_set = ssid->psk_set;
6659 if (params.psk_set)
6660 os_memcpy(params.psk, ssid->psk, sizeof(params.psk));
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08006661 if (ssid->passphrase) {
6662 if (os_strlen(ssid->passphrase) >= sizeof(params.passphrase)) {
6663 wpa_printf(MSG_ERROR, "P2P: Invalid passphrase in "
6664 "persistent group");
6665 return -1;
6666 }
6667 os_strlcpy(params.passphrase, ssid->passphrase,
6668 sizeof(params.passphrase));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006669 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006670 os_memcpy(params.ssid, ssid->ssid, ssid->ssid_len);
6671 params.ssid_len = ssid->ssid_len;
6672 params.persistent_group = 1;
6673
6674 wpa_s = wpas_p2p_get_group_iface(wpa_s, addr_allocated, 1);
6675 if (wpa_s == NULL)
6676 return -1;
6677
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006678 p2p_channels_to_freqs(channels, params.freq_list, P2P_MAX_CHANNELS);
6679
Dmitry Shmidt56052862013-10-04 10:23:25 -07006680 wpa_s->p2p_first_connection_timeout = connection_timeout;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006681 wpas_start_wps_go(wpa_s, &params, 0);
6682
6683 return 0;
6684}
6685
6686
6687static void wpas_p2p_ie_update(void *ctx, struct wpabuf *beacon_ies,
6688 struct wpabuf *proberesp_ies)
6689{
6690 struct wpa_supplicant *wpa_s = ctx;
6691 if (wpa_s->ap_iface) {
6692 struct hostapd_data *hapd = wpa_s->ap_iface->bss[0];
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006693 if (!(hapd->conf->p2p & P2P_GROUP_OWNER)) {
6694 wpabuf_free(beacon_ies);
6695 wpabuf_free(proberesp_ies);
6696 return;
6697 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006698 if (beacon_ies) {
6699 wpabuf_free(hapd->p2p_beacon_ie);
6700 hapd->p2p_beacon_ie = beacon_ies;
6701 }
6702 wpabuf_free(hapd->p2p_probe_resp_ie);
6703 hapd->p2p_probe_resp_ie = proberesp_ies;
6704 } else {
6705 wpabuf_free(beacon_ies);
6706 wpabuf_free(proberesp_ies);
6707 }
6708 wpa_supplicant_ap_update_beacon(wpa_s);
6709}
6710
6711
6712static void wpas_p2p_idle_update(void *ctx, int idle)
6713{
6714 struct wpa_supplicant *wpa_s = ctx;
6715 if (!wpa_s->ap_iface)
6716 return;
6717 wpa_printf(MSG_DEBUG, "P2P: GO - group %sidle", idle ? "" : "not ");
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08006718 if (idle) {
6719 if (wpa_s->global->p2p_fail_on_wps_complete &&
6720 wpa_s->p2p_in_provisioning) {
6721 wpas_p2p_grpform_fail_after_wps(wpa_s);
6722 return;
6723 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006724 wpas_p2p_set_group_idle_timeout(wpa_s);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08006725 } else
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006726 eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL);
6727}
6728
6729
6730struct p2p_group * wpas_p2p_group_init(struct wpa_supplicant *wpa_s,
Dmitry Shmidt04949592012-07-19 12:16:46 -07006731 struct wpa_ssid *ssid)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006732{
6733 struct p2p_group *group;
6734 struct p2p_group_config *cfg;
6735
Dmitry Shmidt849734c2016-05-27 09:59:01 -07006736 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL ||
6737 !ssid->p2p_group)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006738 return NULL;
6739
6740 cfg = os_zalloc(sizeof(*cfg));
6741 if (cfg == NULL)
6742 return NULL;
6743
Dmitry Shmidt04949592012-07-19 12:16:46 -07006744 if (ssid->p2p_persistent_group && wpa_s->conf->persistent_reconnect)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006745 cfg->persistent_group = 2;
Dmitry Shmidt04949592012-07-19 12:16:46 -07006746 else if (ssid->p2p_persistent_group)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006747 cfg->persistent_group = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006748 os_memcpy(cfg->interface_addr, wpa_s->own_addr, ETH_ALEN);
6749 if (wpa_s->max_stations &&
6750 wpa_s->max_stations < wpa_s->conf->max_num_sta)
6751 cfg->max_clients = wpa_s->max_stations;
6752 else
6753 cfg->max_clients = wpa_s->conf->max_num_sta;
Dmitry Shmidt04949592012-07-19 12:16:46 -07006754 os_memcpy(cfg->ssid, ssid->ssid, ssid->ssid_len);
6755 cfg->ssid_len = ssid->ssid_len;
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08006756 cfg->freq = ssid->frequency;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006757 cfg->cb_ctx = wpa_s;
6758 cfg->ie_update = wpas_p2p_ie_update;
6759 cfg->idle_update = wpas_p2p_idle_update;
Dmitry Shmidte4663042016-04-04 10:07:49 -07006760 cfg->ip_addr_alloc = WPA_GET_BE32(wpa_s->p2pdev->conf->ip_addr_start)
6761 != 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006762
6763 group = p2p_group_init(wpa_s->global->p2p, cfg);
6764 if (group == NULL)
6765 os_free(cfg);
Dmitry Shmidt04949592012-07-19 12:16:46 -07006766 if (ssid->mode != WPAS_MODE_P2P_GROUP_FORMATION)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006767 p2p_group_notif_formation_done(group);
6768 wpa_s->p2p_group = group;
6769 return group;
6770}
6771
6772
6773void wpas_p2p_wps_success(struct wpa_supplicant *wpa_s, const u8 *peer_addr,
6774 int registrar)
6775{
Dmitry Shmidt04949592012-07-19 12:16:46 -07006776 struct wpa_ssid *ssid = wpa_s->current_ssid;
6777
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006778 if (!wpa_s->p2p_in_provisioning) {
6779 wpa_printf(MSG_DEBUG, "P2P: Ignore WPS success event - P2P "
6780 "provisioning not in progress");
6781 return;
6782 }
6783
Dmitry Shmidt04949592012-07-19 12:16:46 -07006784 if (ssid && ssid->mode == WPAS_MODE_INFRA) {
6785 u8 go_dev_addr[ETH_ALEN];
6786 os_memcpy(go_dev_addr, wpa_s->bssid, ETH_ALEN);
6787 wpas_p2p_persistent_group(wpa_s, go_dev_addr, ssid->ssid,
6788 ssid->ssid_len);
6789 /* Clear any stored provisioning info */
6790 p2p_clear_provisioning_info(wpa_s->global->p2p, go_dev_addr);
6791 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006792
Dmitry Shmidt9c175262016-03-03 10:20:07 -08006793 eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s->p2pdev,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006794 NULL);
Dmitry Shmidt56052862013-10-04 10:23:25 -07006795 wpa_s->p2p_go_group_formation_completed = 1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07006796 if (ssid && ssid->mode == WPAS_MODE_INFRA) {
6797 /*
6798 * Use a separate timeout for initial data connection to
6799 * complete to allow the group to be removed automatically if
6800 * something goes wrong in this step before the P2P group idle
6801 * timeout mechanism is taken into use.
6802 */
Dmitry Shmidt56052862013-10-04 10:23:25 -07006803 wpa_dbg(wpa_s, MSG_DEBUG,
6804 "P2P: Re-start group formation timeout (%d seconds) as client for initial connection",
6805 P2P_MAX_INITIAL_CONN_WAIT);
Dmitry Shmidt04949592012-07-19 12:16:46 -07006806 eloop_register_timeout(P2P_MAX_INITIAL_CONN_WAIT, 0,
6807 wpas_p2p_group_formation_timeout,
Dmitry Shmidt9c175262016-03-03 10:20:07 -08006808 wpa_s->p2pdev, NULL);
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08006809 /* Complete group formation on successful data connection. */
6810 wpa_s->p2p_go_group_formation_completed = 0;
Dmitry Shmidt56052862013-10-04 10:23:25 -07006811 } else if (ssid) {
6812 /*
6813 * Use a separate timeout for initial data connection to
6814 * complete to allow the group to be removed automatically if
6815 * the client does not complete data connection successfully.
6816 */
6817 wpa_dbg(wpa_s, MSG_DEBUG,
6818 "P2P: Re-start group formation timeout (%d seconds) as GO for initial connection",
6819 P2P_MAX_INITIAL_CONN_WAIT_GO);
6820 eloop_register_timeout(P2P_MAX_INITIAL_CONN_WAIT_GO, 0,
6821 wpas_p2p_group_formation_timeout,
Dmitry Shmidt9c175262016-03-03 10:20:07 -08006822 wpa_s->p2pdev, NULL);
Dmitry Shmidt56052862013-10-04 10:23:25 -07006823 /*
6824 * Complete group formation on first successful data connection
6825 */
6826 wpa_s->p2p_go_group_formation_completed = 0;
Dmitry Shmidt04949592012-07-19 12:16:46 -07006827 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006828 if (wpa_s->global->p2p)
6829 p2p_wps_success_cb(wpa_s->global->p2p, peer_addr);
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07006830 wpas_group_formation_completed(wpa_s, 1, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006831}
6832
6833
Jouni Malinen75ecf522011-06-27 15:19:46 -07006834void wpas_p2p_wps_failed(struct wpa_supplicant *wpa_s,
6835 struct wps_event_fail *fail)
6836{
6837 if (!wpa_s->p2p_in_provisioning) {
6838 wpa_printf(MSG_DEBUG, "P2P: Ignore WPS fail event - P2P "
6839 "provisioning not in progress");
6840 return;
6841 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006842
6843 if (wpa_s->go_params) {
6844 p2p_clear_provisioning_info(
6845 wpa_s->global->p2p,
Dmitry Shmidt04949592012-07-19 12:16:46 -07006846 wpa_s->go_params->peer_device_addr);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006847 }
6848
Jouni Malinen75ecf522011-06-27 15:19:46 -07006849 wpas_notify_p2p_wps_failed(wpa_s, fail);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08006850
6851 if (wpa_s == wpa_s->global->p2p_group_formation) {
6852 /*
6853 * Allow some time for the failed WPS negotiation exchange to
6854 * complete, but remove the group since group formation cannot
6855 * succeed after provisioning failure.
6856 */
6857 wpa_printf(MSG_DEBUG, "P2P: WPS step failed during group formation - reject connection from timeout");
6858 wpa_s->global->p2p_fail_on_wps_complete = 1;
6859 eloop_deplete_timeout(0, 50000,
6860 wpas_p2p_group_formation_timeout,
Dmitry Shmidt9c175262016-03-03 10:20:07 -08006861 wpa_s->p2pdev, NULL);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08006862 }
6863}
6864
6865
6866int wpas_p2p_wps_eapol_cb(struct wpa_supplicant *wpa_s)
6867{
6868 if (!wpa_s->global->p2p_fail_on_wps_complete ||
6869 !wpa_s->p2p_in_provisioning)
6870 return 0;
6871
6872 wpas_p2p_grpform_fail_after_wps(wpa_s);
6873
6874 return 1;
Jouni Malinen75ecf522011-06-27 15:19:46 -07006875}
6876
6877
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006878int wpas_p2p_prov_disc(struct wpa_supplicant *wpa_s, const u8 *peer_addr,
Dmitry Shmidt04949592012-07-19 12:16:46 -07006879 const char *config_method,
Dmitry Shmidt216983b2015-02-06 10:50:36 -08006880 enum wpas_p2p_prov_disc_use use,
6881 struct p2ps_provision *p2ps_prov)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006882{
6883 u16 config_methods;
6884
Dmitry Shmidt216983b2015-02-06 10:50:36 -08006885 wpa_s->global->pending_p2ps_group = 0;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006886 wpa_s->global->pending_p2ps_group_freq = 0;
Dmitry Shmidt04949592012-07-19 12:16:46 -07006887 wpa_s->p2p_fallback_to_go_neg = 0;
6888 wpa_s->pending_pd_use = NORMAL_PD;
Dmitry Shmidt216983b2015-02-06 10:50:36 -08006889 if (p2ps_prov && use == WPAS_P2P_PD_FOR_ASP) {
6890 p2ps_prov->conncap = p2ps_group_capability(
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006891 wpa_s, P2PS_SETUP_NONE, p2ps_prov->role,
6892 &p2ps_prov->force_freq, &p2ps_prov->pref_freq);
6893
Dmitry Shmidt216983b2015-02-06 10:50:36 -08006894 wpa_printf(MSG_DEBUG,
6895 "P2P: %s conncap: %d - ASP parsed: %x %x %d %s",
6896 __func__, p2ps_prov->conncap,
6897 p2ps_prov->adv_id, p2ps_prov->conncap,
6898 p2ps_prov->status, p2ps_prov->info);
6899
6900 config_methods = 0;
6901 } else if (os_strncmp(config_method, "display", 7) == 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006902 config_methods = WPS_CONFIG_DISPLAY;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006903 else if (os_strncmp(config_method, "keypad", 6) == 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006904 config_methods = WPS_CONFIG_KEYPAD;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006905 else if (os_strncmp(config_method, "pbc", 3) == 0 ||
6906 os_strncmp(config_method, "pushbutton", 10) == 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006907 config_methods = WPS_CONFIG_PUSHBUTTON;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006908 else {
6909 wpa_printf(MSG_DEBUG, "P2P: Unknown config method");
Dmitry Shmidt216983b2015-02-06 10:50:36 -08006910 os_free(p2ps_prov);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006911 return -1;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006912 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006913
Dmitry Shmidt04949592012-07-19 12:16:46 -07006914 if (use == WPAS_P2P_PD_AUTO) {
6915 os_memcpy(wpa_s->pending_join_dev_addr, peer_addr, ETH_ALEN);
6916 wpa_s->pending_pd_config_methods = config_methods;
6917 wpa_s->p2p_auto_pd = 1;
6918 wpa_s->p2p_auto_join = 0;
6919 wpa_s->pending_pd_before_join = 0;
6920 wpa_s->auto_pd_scan_retry = 0;
6921 wpas_p2p_stop_find(wpa_s);
6922 wpa_s->p2p_join_scan_count = 0;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08006923 os_get_reltime(&wpa_s->p2p_auto_started);
Dmitry Shmidt04949592012-07-19 12:16:46 -07006924 wpa_printf(MSG_DEBUG, "P2P: Auto PD started at %ld.%06ld",
6925 wpa_s->p2p_auto_started.sec,
6926 wpa_s->p2p_auto_started.usec);
6927 wpas_p2p_join_scan(wpa_s, NULL);
6928 return 0;
6929 }
6930
Dmitry Shmidt216983b2015-02-06 10:50:36 -08006931 if (wpa_s->global->p2p == NULL || wpa_s->global->p2p_disabled) {
6932 os_free(p2ps_prov);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006933 return -1;
Dmitry Shmidt216983b2015-02-06 10:50:36 -08006934 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006935
Dmitry Shmidt216983b2015-02-06 10:50:36 -08006936 return p2p_prov_disc_req(wpa_s->global->p2p, peer_addr, p2ps_prov,
Dmitry Shmidt04949592012-07-19 12:16:46 -07006937 config_methods, use == WPAS_P2P_PD_FOR_JOIN,
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08006938 0, 1);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006939}
6940
6941
6942int wpas_p2p_scan_result_text(const u8 *ies, size_t ies_len, char *buf,
6943 char *end)
6944{
6945 return p2p_scan_result_text(ies, ies_len, buf, end);
6946}
6947
6948
6949static void wpas_p2p_clear_pending_action_tx(struct wpa_supplicant *wpa_s)
6950{
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08006951 if (!offchannel_pending_action_tx(wpa_s))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006952 return;
6953
Dmitry Shmidtde47be72016-01-07 12:52:55 -08006954 if (wpa_s->p2p_send_action_work) {
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08006955 wpas_p2p_free_send_action_work(wpa_s);
Dmitry Shmidtde47be72016-01-07 12:52:55 -08006956 eloop_cancel_timeout(wpas_p2p_send_action_work_timeout,
6957 wpa_s, NULL);
6958 offchannel_send_action_done(wpa_s);
6959 }
Dmitry Shmidt03658832014-08-13 11:03:49 -07006960
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006961 wpa_printf(MSG_DEBUG, "P2P: Drop pending Action TX due to new "
6962 "operation request");
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08006963 offchannel_clear_pending_action_tx(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006964}
6965
6966
6967int wpas_p2p_find(struct wpa_supplicant *wpa_s, unsigned int timeout,
6968 enum p2p_discovery_type type,
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08006969 unsigned int num_req_dev_types, const u8 *req_dev_types,
Dmitry Shmidt216983b2015-02-06 10:50:36 -08006970 const u8 *dev_id, unsigned int search_delay,
Dmitry Shmidt203eadb2015-03-05 14:16:04 -08006971 u8 seek_cnt, const char **seek_string, int freq)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006972{
6973 wpas_p2p_clear_pending_action_tx(wpa_s);
Hai Shalomfdcde762020-04-02 11:19:20 -07006974 wpa_s->global->p2p_long_listen = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006975
Dmitry Shmidt04949592012-07-19 12:16:46 -07006976 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL ||
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07006977 wpa_s->p2p_in_provisioning) {
6978 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Reject p2p_find operation%s%s",
6979 (wpa_s->global->p2p_disabled || !wpa_s->global->p2p) ?
6980 " (P2P disabled)" : "",
6981 wpa_s->p2p_in_provisioning ?
6982 " (p2p_in_provisioning)" : "");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006983 return -1;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07006984 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006985
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08006986 wpa_supplicant_cancel_sched_scan(wpa_s);
6987
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006988 return p2p_find(wpa_s->global->p2p, timeout, type,
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07006989 num_req_dev_types, req_dev_types, dev_id,
Dmitry Shmidt203eadb2015-03-05 14:16:04 -08006990 search_delay, seek_cnt, seek_string, freq);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006991}
6992
6993
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006994static void wpas_p2p_scan_res_ignore_search(struct wpa_supplicant *wpa_s,
6995 struct wpa_scan_results *scan_res)
6996{
6997 wpa_printf(MSG_DEBUG, "P2P: Ignore scan results");
6998
6999 if (wpa_s->p2p_scan_work) {
7000 struct wpa_radio_work *work = wpa_s->p2p_scan_work;
7001 wpa_s->p2p_scan_work = NULL;
7002 radio_work_done(work);
7003 }
7004
7005 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
7006 return;
7007
7008 /*
7009 * Indicate that results have been processed so that the P2P module can
7010 * continue pending tasks.
7011 */
7012 p2p_scan_res_handled(wpa_s->global->p2p);
7013}
7014
7015
7016static void wpas_p2p_stop_find_oper(struct wpa_supplicant *wpa_s)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007017{
7018 wpas_p2p_clear_pending_action_tx(wpa_s);
Hai Shalomfdcde762020-04-02 11:19:20 -07007019 wpa_s->global->p2p_long_listen = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007020 eloop_cancel_timeout(wpas_p2p_long_listen_timeout, wpa_s, NULL);
7021 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007022
7023 if (wpa_s->global->p2p)
7024 p2p_stop_find(wpa_s->global->p2p);
7025
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007026 if (wpa_s->scan_res_handler == wpas_p2p_scan_res_handler) {
7027 wpa_printf(MSG_DEBUG,
7028 "P2P: Do not consider the scan results after stop_find");
7029 wpa_s->scan_res_handler = wpas_p2p_scan_res_ignore_search;
7030 }
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08007031}
7032
7033
7034void wpas_p2p_stop_find(struct wpa_supplicant *wpa_s)
7035{
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007036 wpas_p2p_stop_find_oper(wpa_s);
Dmitry Shmidt216983b2015-02-06 10:50:36 -08007037 if (!wpa_s->global->pending_group_iface_for_p2ps)
7038 wpas_p2p_remove_pending_group_interface(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007039}
7040
7041
7042static void wpas_p2p_long_listen_timeout(void *eloop_ctx, void *timeout_ctx)
7043{
7044 struct wpa_supplicant *wpa_s = eloop_ctx;
Hai Shalomfdcde762020-04-02 11:19:20 -07007045 wpa_s->global->p2p_long_listen = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007046}
7047
7048
7049int wpas_p2p_listen(struct wpa_supplicant *wpa_s, unsigned int timeout)
7050{
7051 int res;
7052
7053 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
7054 return -1;
7055
Dmitry Shmidt58d12ad2016-07-28 10:07:03 -07007056 if (wpa_s->p2p_lo_started) {
7057 wpa_printf(MSG_DEBUG,
7058 "P2P: Cannot start P2P listen, it is offloaded");
7059 return -1;
7060 }
7061
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08007062 wpa_supplicant_cancel_sched_scan(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007063 wpas_p2p_clear_pending_action_tx(wpa_s);
7064
7065 if (timeout == 0) {
7066 /*
7067 * This is a request for unlimited Listen state. However, at
7068 * least for now, this is mapped to a Listen state for one
7069 * hour.
7070 */
7071 timeout = 3600;
7072 }
7073 eloop_cancel_timeout(wpas_p2p_long_listen_timeout, wpa_s, NULL);
Hai Shalomfdcde762020-04-02 11:19:20 -07007074 wpa_s->global->p2p_long_listen = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007075
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007076 /*
7077 * Stop previous find/listen operation to avoid trying to request a new
7078 * remain-on-channel operation while the driver is still running the
7079 * previous one.
7080 */
7081 if (wpa_s->global->p2p)
7082 p2p_stop_find(wpa_s->global->p2p);
7083
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007084 res = wpas_p2p_listen_start(wpa_s, timeout * 1000);
7085 if (res == 0 && timeout * 1000 > wpa_s->max_remain_on_chan) {
Hai Shalomfdcde762020-04-02 11:19:20 -07007086 wpa_s->global->p2p_long_listen = timeout * 1000;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007087 eloop_register_timeout(timeout, 0,
7088 wpas_p2p_long_listen_timeout,
7089 wpa_s, NULL);
7090 }
7091
7092 return res;
7093}
7094
7095
7096int wpas_p2p_assoc_req_ie(struct wpa_supplicant *wpa_s, struct wpa_bss *bss,
7097 u8 *buf, size_t len, int p2p_group)
7098{
7099 struct wpabuf *p2p_ie;
7100 int ret;
7101
7102 if (wpa_s->global->p2p_disabled)
7103 return -1;
Dmitry Shmidt8bd70b72015-05-26 16:02:19 -07007104 /*
7105 * Advertize mandatory cross connection capability even on
7106 * p2p_disabled=1 interface when associating with a P2P Manager WLAN AP.
7107 */
7108 if (wpa_s->conf->p2p_disabled && p2p_group)
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07007109 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007110 if (wpa_s->global->p2p == NULL)
7111 return -1;
7112 if (bss == NULL)
7113 return -1;
7114
7115 p2p_ie = wpa_bss_get_vendor_ie_multi(bss, P2P_IE_VENDOR_TYPE);
7116 ret = p2p_assoc_req_ie(wpa_s->global->p2p, bss->bssid, buf, len,
7117 p2p_group, p2p_ie);
7118 wpabuf_free(p2p_ie);
7119
7120 return ret;
7121}
7122
7123
7124int wpas_p2p_probe_req_rx(struct wpa_supplicant *wpa_s, const u8 *addr,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007125 const u8 *dst, const u8 *bssid,
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07007126 const u8 *ie, size_t ie_len,
7127 unsigned int rx_freq, int ssi_signal)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007128{
7129 if (wpa_s->global->p2p_disabled)
7130 return 0;
7131 if (wpa_s->global->p2p == NULL)
7132 return 0;
7133
Dmitry Shmidt04949592012-07-19 12:16:46 -07007134 switch (p2p_probe_req_rx(wpa_s->global->p2p, addr, dst, bssid,
Dmitry Shmidt58d12ad2016-07-28 10:07:03 -07007135 ie, ie_len, rx_freq, wpa_s->p2p_lo_started)) {
Dmitry Shmidt04949592012-07-19 12:16:46 -07007136 case P2P_PREQ_NOT_P2P:
7137 wpas_notify_preq(wpa_s, addr, dst, bssid, ie, ie_len,
7138 ssi_signal);
7139 /* fall through */
7140 case P2P_PREQ_MALFORMED:
7141 case P2P_PREQ_NOT_LISTEN:
7142 case P2P_PREQ_NOT_PROCESSED:
7143 default: /* make gcc happy */
7144 return 0;
7145 case P2P_PREQ_PROCESSED:
7146 return 1;
7147 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007148}
7149
7150
7151void wpas_p2p_rx_action(struct wpa_supplicant *wpa_s, const u8 *da,
7152 const u8 *sa, const u8 *bssid,
7153 u8 category, const u8 *data, size_t len, int freq)
7154{
7155 if (wpa_s->global->p2p_disabled)
7156 return;
7157 if (wpa_s->global->p2p == NULL)
7158 return;
7159
7160 p2p_rx_action(wpa_s->global->p2p, da, sa, bssid, category, data, len,
7161 freq);
7162}
7163
7164
7165void wpas_p2p_scan_ie(struct wpa_supplicant *wpa_s, struct wpabuf *ies)
7166{
Dmitry Shmidt9c175262016-03-03 10:20:07 -08007167 unsigned int bands;
7168
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007169 if (wpa_s->global->p2p_disabled)
7170 return;
7171 if (wpa_s->global->p2p == NULL)
7172 return;
7173
Dmitry Shmidt9c175262016-03-03 10:20:07 -08007174 bands = wpas_get_bands(wpa_s, NULL);
7175 p2p_scan_ie(wpa_s->global->p2p, ies, NULL, bands);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007176}
7177
7178
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07007179static void wpas_p2p_group_deinit(struct wpa_supplicant *wpa_s)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007180{
7181 p2p_group_deinit(wpa_s->p2p_group);
7182 wpa_s->p2p_group = NULL;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007183
7184 wpa_s->ap_configured_cb = NULL;
7185 wpa_s->ap_configured_cb_ctx = NULL;
7186 wpa_s->ap_configured_cb_data = NULL;
7187 wpa_s->connect_without_scan = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007188}
7189
7190
7191int wpas_p2p_reject(struct wpa_supplicant *wpa_s, const u8 *addr)
7192{
Hai Shalomfdcde762020-04-02 11:19:20 -07007193 wpa_s->global->p2p_long_listen = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007194
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007195 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
7196 return -1;
7197
7198 return p2p_reject(wpa_s->global->p2p, addr);
7199}
7200
7201
7202/* Invite to reinvoke a persistent group */
7203int wpas_p2p_invite(struct wpa_supplicant *wpa_s, const u8 *peer_addr,
Jouni Malinen31be0a42012-08-31 21:20:51 +03007204 struct wpa_ssid *ssid, const u8 *go_dev_addr, int freq,
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08007205 int vht_center_freq2, int ht40, int vht, int max_chwidth,
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08007206 int pref_freq, int he, int edmg)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007207{
7208 enum p2p_invite_role role;
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07007209 u8 *bssid = NULL;
7210 int force_freq = 0;
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08007211 int res;
Dmitry Shmidta0d265f2013-11-19 13:13:41 -08007212 int no_pref_freq_given = pref_freq == 0;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08007213 unsigned int pref_freq_list[P2P_MAX_PREF_CHANNELS], size;
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08007214
Dmitry Shmidt700a1372013-03-15 14:14:44 -07007215 wpa_s->global->p2p_invite_group = NULL;
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08007216 if (peer_addr)
7217 os_memcpy(wpa_s->p2p_auth_invite, peer_addr, ETH_ALEN);
7218 else
7219 os_memset(wpa_s->p2p_auth_invite, 0, ETH_ALEN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007220
Jouni Malinen31be0a42012-08-31 21:20:51 +03007221 wpa_s->p2p_persistent_go_freq = freq;
7222 wpa_s->p2p_go_ht40 = !!ht40;
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08007223 wpa_s->p2p_go_vht = !!vht;
Hai Shalom74f70d42019-02-11 14:42:39 -08007224 wpa_s->p2p_go_he = !!he;
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08007225 wpa_s->p2p_go_max_oper_chwidth = max_chwidth;
7226 wpa_s->p2p_go_vht_center_freq2 = vht_center_freq2;
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08007227 wpa_s->p2p_go_edmg = !!edmg;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007228 if (ssid->mode == WPAS_MODE_P2P_GO) {
7229 role = P2P_INVITE_ROLE_GO;
7230 if (peer_addr == NULL) {
7231 wpa_printf(MSG_DEBUG, "P2P: Missing peer "
7232 "address in invitation command");
7233 return -1;
7234 }
7235 if (wpas_p2p_create_iface(wpa_s)) {
7236 if (wpas_p2p_add_group_interface(wpa_s,
7237 WPA_IF_P2P_GO) < 0) {
7238 wpa_printf(MSG_ERROR, "P2P: Failed to "
7239 "allocate a new interface for the "
7240 "group");
7241 return -1;
7242 }
7243 bssid = wpa_s->pending_interface_addr;
Dmitry Shmidt9c175262016-03-03 10:20:07 -08007244 } else if (wpa_s->p2p_mgmt)
7245 bssid = wpa_s->parent->own_addr;
7246 else
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007247 bssid = wpa_s->own_addr;
7248 } else {
7249 role = P2P_INVITE_ROLE_CLIENT;
7250 peer_addr = ssid->bssid;
7251 }
7252 wpa_s->pending_invite_ssid_id = ssid->id;
7253
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08007254 size = P2P_MAX_PREF_CHANNELS;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07007255 res = wpas_p2p_setup_freqs(wpa_s, freq, &force_freq, &pref_freq,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08007256 role == P2P_INVITE_ROLE_GO,
7257 pref_freq_list, &size);
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08007258 if (res)
7259 return res;
Irfan Sheriffaf84a572012-09-22 16:59:30 -07007260
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007261 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
7262 return -1;
7263
Dmitry Shmidt7d56b752015-12-22 10:59:44 -08007264 p2p_set_own_pref_freq_list(wpa_s->global->p2p, pref_freq_list, size);
7265
Dmitry Shmidta0d265f2013-11-19 13:13:41 -08007266 if (wpa_s->parent->conf->p2p_ignore_shared_freq &&
7267 no_pref_freq_given && pref_freq > 0 &&
7268 wpa_s->num_multichan_concurrent > 1 &&
7269 wpas_p2p_num_unused_channels(wpa_s) > 0) {
7270 wpa_printf(MSG_DEBUG, "P2P: Ignore own channel preference %d MHz for invitation due to p2p_ignore_shared_freq=1 configuration",
7271 pref_freq);
7272 pref_freq = 0;
7273 }
7274
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08007275 /*
7276 * Stop any find/listen operations before invitation and possibly
7277 * connection establishment.
7278 */
7279 wpas_p2p_stop_find_oper(wpa_s);
7280
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007281 return p2p_invite(wpa_s->global->p2p, peer_addr, role, bssid,
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08007282 ssid->ssid, ssid->ssid_len, force_freq, go_dev_addr,
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08007283 1, pref_freq, -1);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007284}
7285
7286
7287/* Invite to join an active group */
7288int wpas_p2p_invite_group(struct wpa_supplicant *wpa_s, const char *ifname,
7289 const u8 *peer_addr, const u8 *go_dev_addr)
7290{
7291 struct wpa_global *global = wpa_s->global;
7292 enum p2p_invite_role role;
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07007293 u8 *bssid = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007294 struct wpa_ssid *ssid;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08007295 int persistent;
Dmitry Shmidt051af732013-10-22 13:52:46 -07007296 int freq = 0, force_freq = 0, pref_freq = 0;
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08007297 int res;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08007298 unsigned int pref_freq_list[P2P_MAX_PREF_CHANNELS], size;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007299
Jouni Malinen31be0a42012-08-31 21:20:51 +03007300 wpa_s->p2p_persistent_go_freq = 0;
7301 wpa_s->p2p_go_ht40 = 0;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07007302 wpa_s->p2p_go_vht = 0;
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08007303 wpa_s->p2p_go_vht_center_freq2 = 0;
7304 wpa_s->p2p_go_max_oper_chwidth = 0;
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08007305 wpa_s->p2p_go_edmg = 0;
Jouni Malinen31be0a42012-08-31 21:20:51 +03007306
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007307 for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
7308 if (os_strcmp(wpa_s->ifname, ifname) == 0)
7309 break;
7310 }
7311 if (wpa_s == NULL) {
7312 wpa_printf(MSG_DEBUG, "P2P: Interface '%s' not found", ifname);
7313 return -1;
7314 }
7315
7316 ssid = wpa_s->current_ssid;
7317 if (ssid == NULL) {
7318 wpa_printf(MSG_DEBUG, "P2P: No current SSID to use for "
7319 "invitation");
7320 return -1;
7321 }
7322
Dmitry Shmidt700a1372013-03-15 14:14:44 -07007323 wpa_s->global->p2p_invite_group = wpa_s;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08007324 persistent = ssid->p2p_persistent_group &&
Dmitry Shmidt9c175262016-03-03 10:20:07 -08007325 wpas_p2p_get_persistent(wpa_s->p2pdev, peer_addr,
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08007326 ssid->ssid, ssid->ssid_len);
7327
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007328 if (ssid->mode == WPAS_MODE_P2P_GO) {
7329 role = P2P_INVITE_ROLE_ACTIVE_GO;
7330 bssid = wpa_s->own_addr;
7331 if (go_dev_addr == NULL)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007332 go_dev_addr = wpa_s->global->p2p_dev_addr;
Dmitry Shmidt051af732013-10-22 13:52:46 -07007333 freq = ssid->frequency;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007334 } else {
7335 role = P2P_INVITE_ROLE_CLIENT;
7336 if (wpa_s->wpa_state < WPA_ASSOCIATED) {
7337 wpa_printf(MSG_DEBUG, "P2P: Not associated - cannot "
7338 "invite to current group");
7339 return -1;
7340 }
7341 bssid = wpa_s->bssid;
7342 if (go_dev_addr == NULL &&
7343 !is_zero_ether_addr(wpa_s->go_dev_addr))
7344 go_dev_addr = wpa_s->go_dev_addr;
Dmitry Shmidt051af732013-10-22 13:52:46 -07007345 freq = wpa_s->current_bss ? wpa_s->current_bss->freq :
7346 (int) wpa_s->assoc_freq;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007347 }
Dmitry Shmidt9c175262016-03-03 10:20:07 -08007348 wpa_s->p2pdev->pending_invite_ssid_id = -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007349
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007350 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
7351 return -1;
7352
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08007353 size = P2P_MAX_PREF_CHANNELS;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07007354 res = wpas_p2p_setup_freqs(wpa_s, freq, &force_freq, &pref_freq,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08007355 role == P2P_INVITE_ROLE_ACTIVE_GO,
7356 pref_freq_list, &size);
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08007357 if (res)
7358 return res;
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07007359 wpas_p2p_set_own_freq_preference(wpa_s, force_freq);
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08007360
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007361 return p2p_invite(wpa_s->global->p2p, peer_addr, role, bssid,
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08007362 ssid->ssid, ssid->ssid_len, force_freq,
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08007363 go_dev_addr, persistent, pref_freq, -1);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007364}
7365
7366
7367void wpas_p2p_completed(struct wpa_supplicant *wpa_s)
7368{
7369 struct wpa_ssid *ssid = wpa_s->current_ssid;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007370 u8 go_dev_addr[ETH_ALEN];
7371 int persistent;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007372 int freq;
Hai Shalom5f92bc92019-04-18 11:54:11 -07007373 u8 ip[3 * 4], *ip_ptr = NULL;
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08007374 char ip_addr[100];
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007375
Dmitry Shmidt04949592012-07-19 12:16:46 -07007376 if (ssid == NULL || ssid->mode != WPAS_MODE_P2P_GROUP_FORMATION) {
7377 eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
Dmitry Shmidt9c175262016-03-03 10:20:07 -08007378 wpa_s->p2pdev, NULL);
Dmitry Shmidt04949592012-07-19 12:16:46 -07007379 }
7380
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007381 if (!wpa_s->show_group_started || !ssid)
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08007382 return;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007383
7384 wpa_s->show_group_started = 0;
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08007385 if (!wpa_s->p2p_go_group_formation_completed &&
7386 wpa_s->global->p2p_group_formation == wpa_s) {
7387 wpa_dbg(wpa_s, MSG_DEBUG,
7388 "P2P: Marking group formation completed on client on data connection");
7389 wpa_s->p2p_go_group_formation_completed = 1;
7390 wpa_s->global->p2p_group_formation = NULL;
7391 wpa_s->p2p_in_provisioning = 0;
7392 wpa_s->p2p_in_invitation = 0;
7393 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007394
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007395 os_memset(go_dev_addr, 0, ETH_ALEN);
7396 if (ssid->bssid_set)
7397 os_memcpy(go_dev_addr, ssid->bssid, ETH_ALEN);
7398 persistent = wpas_p2p_persistent_group(wpa_s, go_dev_addr, ssid->ssid,
7399 ssid->ssid_len);
7400 os_memcpy(wpa_s->go_dev_addr, go_dev_addr, ETH_ALEN);
7401
7402 if (wpa_s->global->p2p_group_formation == wpa_s)
7403 wpa_s->global->p2p_group_formation = NULL;
7404
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007405 freq = wpa_s->current_bss ? wpa_s->current_bss->freq :
7406 (int) wpa_s->assoc_freq;
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08007407
7408 ip_addr[0] = '\0';
7409 if (wpa_sm_get_p2p_ip_addr(wpa_s->wpa, ip) == 0) {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007410 int res;
7411
7412 res = os_snprintf(ip_addr, sizeof(ip_addr),
7413 " ip_addr=%u.%u.%u.%u "
7414 "ip_mask=%u.%u.%u.%u go_ip_addr=%u.%u.%u.%u",
7415 ip[0], ip[1], ip[2], ip[3],
7416 ip[4], ip[5], ip[6], ip[7],
7417 ip[8], ip[9], ip[10], ip[11]);
7418 if (os_snprintf_error(sizeof(ip_addr), res))
7419 ip_addr[0] = '\0';
Hai Shalom5f92bc92019-04-18 11:54:11 -07007420 ip_ptr = ip;
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08007421 }
7422
Dmitry Shmidt7f0b69e2014-07-28 10:35:20 -07007423 wpas_p2p_group_started(wpa_s, 0, ssid, freq,
7424 ssid->passphrase == NULL && ssid->psk_set ?
7425 ssid->psk : NULL,
7426 ssid->passphrase, go_dev_addr, persistent,
7427 ip_addr);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007428
7429 if (persistent)
Dmitry Shmidtd5ab1b52016-06-21 12:38:41 -07007430 wpas_p2p_store_persistent_group(wpa_s->p2pdev,
7431 ssid, go_dev_addr);
7432
Hai Shalom5f92bc92019-04-18 11:54:11 -07007433 wpas_notify_p2p_group_started(wpa_s, ssid, persistent, 1, ip_ptr);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007434}
7435
7436
7437int wpas_p2p_presence_req(struct wpa_supplicant *wpa_s, u32 duration1,
7438 u32 interval1, u32 duration2, u32 interval2)
7439{
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08007440 int ret;
7441
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007442 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
7443 return -1;
7444
7445 if (wpa_s->wpa_state < WPA_ASSOCIATED ||
7446 wpa_s->current_ssid == NULL ||
7447 wpa_s->current_ssid->mode != WPAS_MODE_INFRA)
7448 return -1;
7449
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08007450 ret = p2p_presence_req(wpa_s->global->p2p, wpa_s->bssid,
7451 wpa_s->own_addr, wpa_s->assoc_freq,
7452 duration1, interval1, duration2, interval2);
7453 if (ret == 0)
7454 wpa_s->waiting_presence_resp = 1;
7455
7456 return ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007457}
7458
7459
7460int wpas_p2p_ext_listen(struct wpa_supplicant *wpa_s, unsigned int period,
7461 unsigned int interval)
7462{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007463 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
7464 return -1;
7465
7466 return p2p_ext_listen(wpa_s->global->p2p, period, interval);
7467}
7468
7469
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007470static int wpas_p2p_is_client(struct wpa_supplicant *wpa_s)
7471{
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08007472 if (wpa_s->current_ssid == NULL) {
7473 /*
7474 * current_ssid can be cleared when P2P client interface gets
7475 * disconnected, so assume this interface was used as P2P
7476 * client.
7477 */
7478 return 1;
7479 }
7480 return wpa_s->current_ssid->p2p_group &&
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007481 wpa_s->current_ssid->mode == WPAS_MODE_INFRA;
7482}
7483
7484
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007485static void wpas_p2p_group_idle_timeout(void *eloop_ctx, void *timeout_ctx)
7486{
7487 struct wpa_supplicant *wpa_s = eloop_ctx;
7488
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007489 if (wpa_s->conf->p2p_group_idle == 0 && !wpas_p2p_is_client(wpa_s)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007490 wpa_printf(MSG_DEBUG, "P2P: Ignore group idle timeout - "
7491 "disabled");
7492 return;
7493 }
7494
Dmitry Shmidt04949592012-07-19 12:16:46 -07007495 wpa_printf(MSG_DEBUG, "P2P: Group idle timeout reached - terminate "
7496 "group");
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07007497 wpas_p2p_group_delete(wpa_s, P2P_GROUP_REMOVAL_IDLE_TIMEOUT);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007498}
7499
7500
7501static void wpas_p2p_set_group_idle_timeout(struct wpa_supplicant *wpa_s)
7502{
Dmitry Shmidt04949592012-07-19 12:16:46 -07007503 int timeout;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007504
Dmitry Shmidt04949592012-07-19 12:16:46 -07007505 if (eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL) > 0)
7506 wpa_printf(MSG_DEBUG, "P2P: Cancelled P2P group idle timeout");
7507
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007508 if (wpa_s->current_ssid == NULL || !wpa_s->current_ssid->p2p_group)
7509 return;
7510
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007511 timeout = wpa_s->conf->p2p_group_idle;
7512 if (wpa_s->current_ssid->mode == WPAS_MODE_INFRA &&
7513 (timeout == 0 || timeout > P2P_MAX_CLIENT_IDLE))
7514 timeout = P2P_MAX_CLIENT_IDLE;
7515
7516 if (timeout == 0)
7517 return;
7518
Dmitry Shmidt04949592012-07-19 12:16:46 -07007519 if (timeout < 0) {
7520 if (wpa_s->current_ssid->mode == WPAS_MODE_INFRA)
7521 timeout = 0; /* special client mode no-timeout */
7522 else
7523 return;
7524 }
7525
7526 if (wpa_s->p2p_in_provisioning) {
7527 /*
7528 * Use the normal group formation timeout during the
7529 * provisioning phase to avoid terminating this process too
7530 * early due to group idle timeout.
7531 */
7532 wpa_printf(MSG_DEBUG, "P2P: Do not use P2P group idle timeout "
7533 "during provisioning");
7534 return;
7535 }
Deepthi Gowri9e4e8ac2013-04-16 11:57:05 +05307536
Dmitry Shmidt04949592012-07-19 12:16:46 -07007537 if (wpa_s->show_group_started) {
7538 /*
7539 * Use the normal group formation timeout between the end of
7540 * the provisioning phase and completion of 4-way handshake to
7541 * avoid terminating this process too early due to group idle
7542 * timeout.
7543 */
7544 wpa_printf(MSG_DEBUG, "P2P: Do not use P2P group idle timeout "
7545 "while waiting for initial 4-way handshake to "
7546 "complete");
7547 return;
7548 }
7549
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007550 wpa_printf(MSG_DEBUG, "P2P: Set P2P group idle timeout to %u seconds",
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007551 timeout);
7552 eloop_register_timeout(timeout, 0, wpas_p2p_group_idle_timeout,
7553 wpa_s, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007554}
7555
7556
Jouni Malinen2b89da82012-08-31 22:04:41 +03007557/* Returns 1 if the interface was removed */
7558int wpas_p2p_deauth_notif(struct wpa_supplicant *wpa_s, const u8 *bssid,
7559 u16 reason_code, const u8 *ie, size_t ie_len,
7560 int locally_generated)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007561{
7562 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
Jouni Malinen2b89da82012-08-31 22:04:41 +03007563 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007564
Dmitry Shmidt04949592012-07-19 12:16:46 -07007565 if (!locally_generated)
7566 p2p_deauth_notif(wpa_s->global->p2p, bssid, reason_code, ie,
7567 ie_len);
7568
7569 if (reason_code == WLAN_REASON_DEAUTH_LEAVING && !locally_generated &&
7570 wpa_s->current_ssid &&
7571 wpa_s->current_ssid->p2p_group &&
7572 wpa_s->current_ssid->mode == WPAS_MODE_INFRA) {
7573 wpa_printf(MSG_DEBUG, "P2P: GO indicated that the P2P Group "
7574 "session is ending");
Jouni Malinen2b89da82012-08-31 22:04:41 +03007575 if (wpas_p2p_group_delete(wpa_s,
7576 P2P_GROUP_REMOVAL_GO_ENDING_SESSION)
7577 > 0)
7578 return 1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07007579 }
Jouni Malinen2b89da82012-08-31 22:04:41 +03007580
7581 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007582}
7583
7584
7585void wpas_p2p_disassoc_notif(struct wpa_supplicant *wpa_s, const u8 *bssid,
Dmitry Shmidt04949592012-07-19 12:16:46 -07007586 u16 reason_code, const u8 *ie, size_t ie_len,
7587 int locally_generated)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007588{
7589 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
7590 return;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007591
Dmitry Shmidt04949592012-07-19 12:16:46 -07007592 if (!locally_generated)
7593 p2p_disassoc_notif(wpa_s->global->p2p, bssid, reason_code, ie,
7594 ie_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007595}
7596
7597
7598void wpas_p2p_update_config(struct wpa_supplicant *wpa_s)
7599{
7600 struct p2p_data *p2p = wpa_s->global->p2p;
7601
7602 if (p2p == NULL)
7603 return;
7604
7605 if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_CAPABLE))
7606 return;
7607
7608 if (wpa_s->conf->changed_parameters & CFG_CHANGED_DEVICE_NAME)
7609 p2p_set_dev_name(p2p, wpa_s->conf->device_name);
7610
7611 if (wpa_s->conf->changed_parameters & CFG_CHANGED_DEVICE_TYPE)
7612 p2p_set_pri_dev_type(p2p, wpa_s->conf->device_type);
7613
7614 if (wpa_s->wps &&
7615 (wpa_s->conf->changed_parameters & CFG_CHANGED_CONFIG_METHODS))
7616 p2p_set_config_methods(p2p, wpa_s->wps->config_methods);
7617
7618 if (wpa_s->wps && (wpa_s->conf->changed_parameters & CFG_CHANGED_UUID))
7619 p2p_set_uuid(p2p, wpa_s->wps->uuid);
7620
7621 if (wpa_s->conf->changed_parameters & CFG_CHANGED_WPS_STRING) {
7622 p2p_set_manufacturer(p2p, wpa_s->conf->manufacturer);
7623 p2p_set_model_name(p2p, wpa_s->conf->model_name);
7624 p2p_set_model_number(p2p, wpa_s->conf->model_number);
7625 p2p_set_serial_number(p2p, wpa_s->conf->serial_number);
7626 }
7627
7628 if (wpa_s->conf->changed_parameters & CFG_CHANGED_SEC_DEVICE_TYPE)
7629 p2p_set_sec_dev_types(p2p,
7630 (void *) wpa_s->conf->sec_device_type,
7631 wpa_s->conf->num_sec_device_types);
7632
7633 if (wpa_s->conf->changed_parameters & CFG_CHANGED_VENDOR_EXTENSION) {
7634 int i;
7635 p2p_remove_wps_vendor_extensions(p2p);
7636 for (i = 0; i < MAX_WPS_VENDOR_EXT; i++) {
7637 if (wpa_s->conf->wps_vendor_ext[i] == NULL)
7638 continue;
7639 p2p_add_wps_vendor_extension(
7640 p2p, wpa_s->conf->wps_vendor_ext[i]);
7641 }
7642 }
7643
7644 if ((wpa_s->conf->changed_parameters & CFG_CHANGED_COUNTRY) &&
7645 wpa_s->conf->country[0] && wpa_s->conf->country[1]) {
7646 char country[3];
7647 country[0] = wpa_s->conf->country[0];
7648 country[1] = wpa_s->conf->country[1];
7649 country[2] = 0x04;
7650 p2p_set_country(p2p, country);
7651 }
7652
7653 if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_SSID_POSTFIX) {
7654 p2p_set_ssid_postfix(p2p, (u8 *) wpa_s->conf->p2p_ssid_postfix,
7655 wpa_s->conf->p2p_ssid_postfix ?
7656 os_strlen(wpa_s->conf->p2p_ssid_postfix) :
7657 0);
7658 }
7659
7660 if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_INTRA_BSS)
7661 p2p_set_intra_bss_dist(p2p, wpa_s->conf->p2p_intra_bss);
Jouni Malinen75ecf522011-06-27 15:19:46 -07007662
7663 if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_LISTEN_CHANNEL) {
7664 u8 reg_class, channel;
7665 int ret;
7666 unsigned int r;
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07007667 u8 channel_forced;
7668
Jouni Malinen75ecf522011-06-27 15:19:46 -07007669 if (wpa_s->conf->p2p_listen_reg_class &&
7670 wpa_s->conf->p2p_listen_channel) {
7671 reg_class = wpa_s->conf->p2p_listen_reg_class;
7672 channel = wpa_s->conf->p2p_listen_channel;
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07007673 channel_forced = 1;
Jouni Malinen75ecf522011-06-27 15:19:46 -07007674 } else {
7675 reg_class = 81;
7676 /*
7677 * Pick one of the social channels randomly as the
7678 * listen channel.
7679 */
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07007680 if (os_get_random((u8 *) &r, sizeof(r)) < 0)
7681 channel = 1;
7682 else
7683 channel = 1 + (r % 3) * 5;
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07007684 channel_forced = 0;
Jouni Malinen75ecf522011-06-27 15:19:46 -07007685 }
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07007686 ret = p2p_set_listen_channel(p2p, reg_class, channel,
7687 channel_forced);
Jouni Malinen75ecf522011-06-27 15:19:46 -07007688 if (ret)
7689 wpa_printf(MSG_ERROR, "P2P: Own listen channel update "
7690 "failed: %d", ret);
7691 }
7692 if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_OPER_CHANNEL) {
7693 u8 op_reg_class, op_channel, cfg_op_channel;
7694 int ret = 0;
7695 unsigned int r;
7696 if (wpa_s->conf->p2p_oper_reg_class &&
7697 wpa_s->conf->p2p_oper_channel) {
7698 op_reg_class = wpa_s->conf->p2p_oper_reg_class;
7699 op_channel = wpa_s->conf->p2p_oper_channel;
7700 cfg_op_channel = 1;
7701 } else {
7702 op_reg_class = 81;
7703 /*
7704 * Use random operation channel from (1, 6, 11)
7705 *if no other preference is indicated.
7706 */
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07007707 if (os_get_random((u8 *) &r, sizeof(r)) < 0)
7708 op_channel = 1;
7709 else
7710 op_channel = 1 + (r % 3) * 5;
Jouni Malinen75ecf522011-06-27 15:19:46 -07007711 cfg_op_channel = 0;
7712 }
7713 ret = p2p_set_oper_channel(p2p, op_reg_class, op_channel,
7714 cfg_op_channel);
7715 if (ret)
7716 wpa_printf(MSG_ERROR, "P2P: Own oper channel update "
7717 "failed: %d", ret);
7718 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07007719
7720 if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_PREF_CHAN) {
7721 if (p2p_set_pref_chan(p2p, wpa_s->conf->num_p2p_pref_chan,
7722 wpa_s->conf->p2p_pref_chan) < 0) {
7723 wpa_printf(MSG_ERROR, "P2P: Preferred channel list "
7724 "update failed");
7725 }
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07007726
7727 if (p2p_set_no_go_freq(p2p, &wpa_s->conf->p2p_no_go_freq) < 0) {
7728 wpa_printf(MSG_ERROR, "P2P: No GO channel list "
7729 "update failed");
7730 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07007731 }
Dmitry Shmidt2271d3f2014-06-23 12:16:31 -07007732
7733 if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_PASSPHRASE_LEN)
7734 p2p_set_passphrase_len(p2p, wpa_s->conf->p2p_passphrase_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007735}
7736
7737
7738int wpas_p2p_set_noa(struct wpa_supplicant *wpa_s, u8 count, int start,
7739 int duration)
7740{
7741 if (!wpa_s->ap_iface)
7742 return -1;
7743 return hostapd_p2p_set_noa(wpa_s->ap_iface->bss[0], count, start,
7744 duration);
7745}
7746
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007747
7748int wpas_p2p_set_cross_connect(struct wpa_supplicant *wpa_s, int enabled)
7749{
7750 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
7751 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007752
7753 wpa_s->global->cross_connection = enabled;
7754 p2p_set_cross_connect(wpa_s->global->p2p, enabled);
7755
7756 if (!enabled) {
7757 struct wpa_supplicant *iface;
7758
7759 for (iface = wpa_s->global->ifaces; iface; iface = iface->next)
7760 {
7761 if (iface->cross_connect_enabled == 0)
7762 continue;
7763
7764 iface->cross_connect_enabled = 0;
7765 iface->cross_connect_in_use = 0;
Dmitry Shmidt9c175262016-03-03 10:20:07 -08007766 wpa_msg_global(iface->p2pdev, MSG_INFO,
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07007767 P2P_EVENT_CROSS_CONNECT_DISABLE "%s %s",
7768 iface->ifname,
7769 iface->cross_connect_uplink);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007770 }
7771 }
7772
7773 return 0;
7774}
7775
7776
7777static void wpas_p2p_enable_cross_connect(struct wpa_supplicant *uplink)
7778{
7779 struct wpa_supplicant *iface;
7780
7781 if (!uplink->global->cross_connection)
7782 return;
7783
7784 for (iface = uplink->global->ifaces; iface; iface = iface->next) {
7785 if (!iface->cross_connect_enabled)
7786 continue;
7787 if (os_strcmp(uplink->ifname, iface->cross_connect_uplink) !=
7788 0)
7789 continue;
7790 if (iface->ap_iface == NULL)
7791 continue;
7792 if (iface->cross_connect_in_use)
7793 continue;
7794
7795 iface->cross_connect_in_use = 1;
Dmitry Shmidt9c175262016-03-03 10:20:07 -08007796 wpa_msg_global(iface->p2pdev, MSG_INFO,
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07007797 P2P_EVENT_CROSS_CONNECT_ENABLE "%s %s",
7798 iface->ifname, iface->cross_connect_uplink);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007799 }
7800}
7801
7802
7803static void wpas_p2p_disable_cross_connect(struct wpa_supplicant *uplink)
7804{
7805 struct wpa_supplicant *iface;
7806
7807 for (iface = uplink->global->ifaces; iface; iface = iface->next) {
7808 if (!iface->cross_connect_enabled)
7809 continue;
7810 if (os_strcmp(uplink->ifname, iface->cross_connect_uplink) !=
7811 0)
7812 continue;
7813 if (!iface->cross_connect_in_use)
7814 continue;
7815
Dmitry Shmidt9c175262016-03-03 10:20:07 -08007816 wpa_msg_global(iface->p2pdev, MSG_INFO,
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07007817 P2P_EVENT_CROSS_CONNECT_DISABLE "%s %s",
7818 iface->ifname, iface->cross_connect_uplink);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007819 iface->cross_connect_in_use = 0;
7820 }
7821}
7822
7823
7824void wpas_p2p_notif_connected(struct wpa_supplicant *wpa_s)
7825{
7826 if (wpa_s->ap_iface || wpa_s->current_ssid == NULL ||
7827 wpa_s->current_ssid->mode != WPAS_MODE_INFRA ||
7828 wpa_s->cross_connect_disallowed)
7829 wpas_p2p_disable_cross_connect(wpa_s);
7830 else
7831 wpas_p2p_enable_cross_connect(wpa_s);
Dmitry Shmidt04949592012-07-19 12:16:46 -07007832 if (!wpa_s->ap_iface &&
7833 eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL) > 0)
7834 wpa_printf(MSG_DEBUG, "P2P: Cancelled P2P group idle timeout");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007835}
7836
7837
7838void wpas_p2p_notif_disconnected(struct wpa_supplicant *wpa_s)
7839{
7840 wpas_p2p_disable_cross_connect(wpa_s);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007841 if (!wpa_s->ap_iface &&
7842 !eloop_is_timeout_registered(wpas_p2p_group_idle_timeout,
7843 wpa_s, NULL))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007844 wpas_p2p_set_group_idle_timeout(wpa_s);
7845}
7846
7847
7848static void wpas_p2p_cross_connect_setup(struct wpa_supplicant *wpa_s)
7849{
7850 struct wpa_supplicant *iface;
7851
7852 if (!wpa_s->global->cross_connection)
7853 return;
7854
7855 for (iface = wpa_s->global->ifaces; iface; iface = iface->next) {
7856 if (iface == wpa_s)
7857 continue;
7858 if (iface->drv_flags &
7859 WPA_DRIVER_FLAGS_P2P_DEDICATED_INTERFACE)
7860 continue;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007861 if ((iface->drv_flags & WPA_DRIVER_FLAGS_P2P_CAPABLE) &&
7862 iface != wpa_s->parent)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007863 continue;
7864
7865 wpa_s->cross_connect_enabled = 1;
7866 os_strlcpy(wpa_s->cross_connect_uplink, iface->ifname,
7867 sizeof(wpa_s->cross_connect_uplink));
7868 wpa_printf(MSG_DEBUG, "P2P: Enable cross connection from "
7869 "%s to %s whenever uplink is available",
7870 wpa_s->ifname, wpa_s->cross_connect_uplink);
7871
7872 if (iface->ap_iface || iface->current_ssid == NULL ||
7873 iface->current_ssid->mode != WPAS_MODE_INFRA ||
7874 iface->cross_connect_disallowed ||
7875 iface->wpa_state != WPA_COMPLETED)
7876 break;
7877
7878 wpa_s->cross_connect_in_use = 1;
Dmitry Shmidt9c175262016-03-03 10:20:07 -08007879 wpa_msg_global(wpa_s->p2pdev, MSG_INFO,
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07007880 P2P_EVENT_CROSS_CONNECT_ENABLE "%s %s",
7881 wpa_s->ifname, wpa_s->cross_connect_uplink);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007882 break;
7883 }
7884}
7885
7886
7887int wpas_p2p_notif_pbc_overlap(struct wpa_supplicant *wpa_s)
7888{
7889 if (wpa_s->p2p_group_interface != P2P_GROUP_INTERFACE_CLIENT &&
7890 !wpa_s->p2p_in_provisioning)
7891 return 0; /* not P2P client operation */
7892
7893 wpa_printf(MSG_DEBUG, "P2P: Terminate connection due to WPS PBC "
7894 "session overlap");
Dmitry Shmidt9c175262016-03-03 10:20:07 -08007895 if (wpa_s != wpa_s->p2pdev)
7896 wpa_msg_ctrl(wpa_s->p2pdev, MSG_INFO, WPS_EVENT_OVERLAP);
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07007897 wpas_p2p_group_formation_failed(wpa_s, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007898 return 1;
7899}
7900
7901
Dmitry Shmidtdf5a7e42014-04-02 12:59:59 -07007902void wpas_p2p_pbc_overlap_cb(void *eloop_ctx, void *timeout_ctx)
7903{
7904 struct wpa_supplicant *wpa_s = eloop_ctx;
7905 wpas_p2p_notif_pbc_overlap(wpa_s);
7906}
7907
7908
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08007909void wpas_p2p_update_channel_list(struct wpa_supplicant *wpa_s,
7910 enum wpas_p2p_channel_update_trig trig)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007911{
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07007912 struct p2p_channels chan, cli_chan;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08007913 struct wpa_used_freq_data *freqs = NULL;
7914 unsigned int num = wpa_s->num_multichan_concurrent;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007915
7916 if (wpa_s->global == NULL || wpa_s->global->p2p == NULL)
7917 return;
7918
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08007919 freqs = os_calloc(num, sizeof(struct wpa_used_freq_data));
7920 if (!freqs)
7921 return;
7922
7923 num = get_shared_radio_freqs_data(wpa_s, freqs, num);
7924
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007925 os_memset(&chan, 0, sizeof(chan));
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07007926 os_memset(&cli_chan, 0, sizeof(cli_chan));
7927 if (wpas_p2p_setup_channels(wpa_s, &chan, &cli_chan)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007928 wpa_printf(MSG_ERROR, "P2P: Failed to update supported "
7929 "channel list");
7930 return;
7931 }
7932
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07007933 p2p_update_channel_list(wpa_s->global->p2p, &chan, &cli_chan);
Dmitry Shmidt684785c2014-05-12 13:34:29 -07007934
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08007935 wpas_p2p_optimize_listen_channel(wpa_s, freqs, num);
Dmitry Shmidt684785c2014-05-12 13:34:29 -07007936
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08007937 /*
7938 * The used frequencies map changed, so it is possible that a GO is
7939 * using a channel that is no longer valid for P2P use. It is also
7940 * possible that due to policy consideration, it would be preferable to
7941 * move it to a frequency already used by other station interfaces.
7942 */
7943 wpas_p2p_consider_moving_gos(wpa_s, freqs, num, trig);
7944
7945 os_free(freqs);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007946}
7947
7948
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08007949static void wpas_p2p_scan_res_ignore(struct wpa_supplicant *wpa_s,
7950 struct wpa_scan_results *scan_res)
7951{
7952 wpa_printf(MSG_DEBUG, "P2P: Ignore scan results");
7953}
7954
7955
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007956int wpas_p2p_cancel(struct wpa_supplicant *wpa_s)
7957{
7958 struct wpa_global *global = wpa_s->global;
7959 int found = 0;
7960 const u8 *peer;
7961
7962 if (global->p2p == NULL)
7963 return -1;
7964
7965 wpa_printf(MSG_DEBUG, "P2P: Request to cancel group formation");
7966
7967 if (wpa_s->pending_interface_name[0] &&
7968 !is_zero_ether_addr(wpa_s->pending_interface_addr))
7969 found = 1;
7970
7971 peer = p2p_get_go_neg_peer(global->p2p);
7972 if (peer) {
7973 wpa_printf(MSG_DEBUG, "P2P: Unauthorize pending GO Neg peer "
7974 MACSTR, MAC2STR(peer));
7975 p2p_unauthorize(global->p2p, peer);
Dmitry Shmidt04949592012-07-19 12:16:46 -07007976 found = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007977 }
7978
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08007979 if (wpa_s->scan_res_handler == wpas_p2p_scan_res_join) {
7980 wpa_printf(MSG_DEBUG, "P2P: Stop pending scan for join");
7981 wpa_s->scan_res_handler = wpas_p2p_scan_res_ignore;
7982 found = 1;
7983 }
7984
7985 if (wpa_s->pending_pd_before_join) {
7986 wpa_printf(MSG_DEBUG, "P2P: Stop pending PD before join");
7987 wpa_s->pending_pd_before_join = 0;
7988 found = 1;
7989 }
7990
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007991 wpas_p2p_stop_find(wpa_s);
7992
7993 for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
7994 if (wpa_s == global->p2p_group_formation &&
7995 (wpa_s->p2p_in_provisioning ||
7996 wpa_s->parent->pending_interface_type ==
7997 WPA_IF_P2P_CLIENT)) {
7998 wpa_printf(MSG_DEBUG, "P2P: Interface %s in group "
7999 "formation found - cancelling",
8000 wpa_s->ifname);
8001 found = 1;
8002 eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
Dmitry Shmidt9c175262016-03-03 10:20:07 -08008003 wpa_s->p2pdev, NULL);
Jouni Malinenadddfc42012-10-03 14:31:41 -07008004 if (wpa_s->p2p_in_provisioning) {
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07008005 wpas_group_formation_completed(wpa_s, 0, 0);
Jouni Malinenadddfc42012-10-03 14:31:41 -07008006 break;
8007 }
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07008008 wpas_p2p_group_delete(wpa_s,
8009 P2P_GROUP_REMOVAL_REQUESTED);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008010 break;
Dmitry Shmidt21de2142014-04-08 10:50:52 -07008011 } else if (wpa_s->p2p_in_invitation) {
8012 wpa_printf(MSG_DEBUG, "P2P: Interface %s in invitation found - cancelling",
8013 wpa_s->ifname);
8014 found = 1;
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07008015 wpas_p2p_group_formation_failed(wpa_s, 0);
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08008016 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008017 }
8018 }
8019
8020 if (!found) {
8021 wpa_printf(MSG_DEBUG, "P2P: No ongoing group formation found");
8022 return -1;
8023 }
8024
8025 return 0;
8026}
8027
8028
8029void wpas_p2p_interface_unavailable(struct wpa_supplicant *wpa_s)
8030{
8031 if (wpa_s->current_ssid == NULL || !wpa_s->current_ssid->p2p_group)
8032 return;
8033
8034 wpa_printf(MSG_DEBUG, "P2P: Remove group due to driver resource not "
8035 "being available anymore");
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07008036 wpas_p2p_group_delete(wpa_s, P2P_GROUP_REMOVAL_UNAVAILABLE);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008037}
8038
8039
8040void wpas_p2p_update_best_channels(struct wpa_supplicant *wpa_s,
8041 int freq_24, int freq_5, int freq_overall)
8042{
8043 struct p2p_data *p2p = wpa_s->global->p2p;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08008044 if (p2p == NULL)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008045 return;
8046 p2p_set_best_channels(p2p, freq_24, freq_5, freq_overall);
8047}
8048
8049
8050int wpas_p2p_unauthorize(struct wpa_supplicant *wpa_s, const char *addr)
8051{
8052 u8 peer[ETH_ALEN];
8053 struct p2p_data *p2p = wpa_s->global->p2p;
8054
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08008055 if (p2p == NULL)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008056 return -1;
8057
8058 if (hwaddr_aton(addr, peer))
8059 return -1;
8060
8061 return p2p_unauthorize(p2p, peer);
8062}
8063
8064
8065/**
8066 * wpas_p2p_disconnect - Disconnect from a P2P Group
8067 * @wpa_s: Pointer to wpa_supplicant data
8068 * Returns: 0 on success, -1 on failure
8069 *
8070 * This can be used to disconnect from a group in which the local end is a P2P
8071 * Client or to end a P2P Group in case the local end is the Group Owner. If a
8072 * virtual network interface was created for this group, that interface will be
8073 * removed. Otherwise, only the configured P2P group network will be removed
8074 * from the interface.
8075 */
8076int wpas_p2p_disconnect(struct wpa_supplicant *wpa_s)
8077{
8078
8079 if (wpa_s == NULL)
8080 return -1;
8081
Jouni Malinen2b89da82012-08-31 22:04:41 +03008082 return wpas_p2p_group_delete(wpa_s, P2P_GROUP_REMOVAL_REQUESTED) < 0 ?
8083 -1 : 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008084}
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008085
8086
8087int wpas_p2p_in_progress(struct wpa_supplicant *wpa_s)
8088{
Dmitry Shmidtf8623282013-02-20 14:34:59 -08008089 int ret;
8090
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008091 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
8092 return 0;
8093
Dmitry Shmidtf8623282013-02-20 14:34:59 -08008094 ret = p2p_in_progress(wpa_s->global->p2p);
8095 if (ret == 0) {
8096 /*
8097 * Check whether there is an ongoing WPS provisioning step (or
8098 * other parts of group formation) on another interface since
8099 * p2p_in_progress() does not report this to avoid issues for
8100 * scans during such provisioning step.
8101 */
8102 if (wpa_s->global->p2p_group_formation &&
8103 wpa_s->global->p2p_group_formation != wpa_s) {
8104 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Another interface (%s) "
8105 "in group formation",
8106 wpa_s->global->p2p_group_formation->ifname);
8107 ret = 1;
8108 }
8109 }
8110
Dmitry Shmidt92c368d2013-08-29 12:37:21 -07008111 if (!ret && wpa_s->global->p2p_go_wait_client.sec) {
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08008112 struct os_reltime now;
8113 os_get_reltime(&now);
8114 if (os_reltime_expired(&now, &wpa_s->global->p2p_go_wait_client,
8115 P2P_MAX_INITIAL_CONN_WAIT_GO)) {
Dmitry Shmidt92c368d2013-08-29 12:37:21 -07008116 /* Wait for the first client has expired */
8117 wpa_s->global->p2p_go_wait_client.sec = 0;
8118 } else {
8119 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Waiting for initial client connection during group formation");
8120 ret = 1;
8121 }
8122 }
8123
Dmitry Shmidtf8623282013-02-20 14:34:59 -08008124 return ret;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008125}
8126
8127
8128void wpas_p2p_network_removed(struct wpa_supplicant *wpa_s,
8129 struct wpa_ssid *ssid)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008130{
8131 if (wpa_s->p2p_in_provisioning && ssid->p2p_group &&
8132 eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
Dmitry Shmidt9c175262016-03-03 10:20:07 -08008133 wpa_s->p2pdev, NULL) > 0) {
Dmitry Shmidt04949592012-07-19 12:16:46 -07008134 /**
8135 * Remove the network by scheduling the group formation
8136 * timeout to happen immediately. The teardown code
8137 * needs to be scheduled to run asynch later so that we
8138 * don't delete data from under ourselves unexpectedly.
8139 * Calling wpas_p2p_group_formation_timeout directly
8140 * causes a series of crashes in WPS failure scenarios.
8141 */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008142 wpa_printf(MSG_DEBUG, "P2P: Canceled group formation due to "
8143 "P2P group network getting removed");
Dmitry Shmidt04949592012-07-19 12:16:46 -07008144 eloop_register_timeout(0, 0, wpas_p2p_group_formation_timeout,
Dmitry Shmidt9c175262016-03-03 10:20:07 -08008145 wpa_s->p2pdev, NULL);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008146 }
8147}
8148
8149
8150struct wpa_ssid * wpas_p2p_get_persistent(struct wpa_supplicant *wpa_s,
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08008151 const u8 *addr, const u8 *ssid,
8152 size_t ssid_len)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008153{
8154 struct wpa_ssid *s;
8155 size_t i;
8156
8157 for (s = wpa_s->conf->ssid; s; s = s->next) {
8158 if (s->disabled != 2)
8159 continue;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08008160 if (ssid &&
8161 (ssid_len != s->ssid_len ||
8162 os_memcmp(ssid, s->ssid, ssid_len) != 0))
8163 continue;
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07008164 if (addr == NULL) {
8165 if (s->mode == WPAS_MODE_P2P_GO)
8166 return s;
8167 continue;
8168 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008169 if (os_memcmp(s->bssid, addr, ETH_ALEN) == 0)
8170 return s; /* peer is GO in the persistent group */
8171 if (s->mode != WPAS_MODE_P2P_GO || s->p2p_client_list == NULL)
8172 continue;
8173 for (i = 0; i < s->num_p2p_clients; i++) {
Dmitry Shmidtff787d52015-01-12 13:01:47 -08008174 if (os_memcmp(s->p2p_client_list + i * 2 * ETH_ALEN,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008175 addr, ETH_ALEN) == 0)
8176 return s; /* peer is P2P client in persistent
8177 * group */
8178 }
8179 }
8180
8181 return NULL;
8182}
8183
8184
8185void wpas_p2p_notify_ap_sta_authorized(struct wpa_supplicant *wpa_s,
8186 const u8 *addr)
8187{
Dmitry Shmidt56052862013-10-04 10:23:25 -07008188 if (eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
Dmitry Shmidt9c175262016-03-03 10:20:07 -08008189 wpa_s->p2pdev, NULL) > 0) {
Dmitry Shmidt56052862013-10-04 10:23:25 -07008190 /*
8191 * This can happen if WPS provisioning step is not terminated
8192 * cleanly (e.g., P2P Client does not send WSC_Done). Since the
8193 * peer was able to connect, there is no need to time out group
8194 * formation after this, though. In addition, this is used with
8195 * the initial connection wait on the GO as a separate formation
8196 * timeout and as such, expected to be hit after the initial WPS
8197 * provisioning step.
8198 */
8199 wpa_printf(MSG_DEBUG, "P2P: Canceled P2P group formation timeout on data connection");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08008200
8201 if (!wpa_s->p2p_go_group_formation_completed &&
8202 !wpa_s->group_formation_reported) {
8203 /*
8204 * GO has not yet notified group formation success since
8205 * the WPS step was not completed cleanly. Do that
8206 * notification now since the P2P Client was able to
8207 * connect and as such, must have received the
8208 * credential from the WPS step.
8209 */
8210 if (wpa_s->global->p2p)
8211 p2p_wps_success_cb(wpa_s->global->p2p, addr);
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07008212 wpas_group_formation_completed(wpa_s, 1, 0);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08008213 }
Dmitry Shmidt56052862013-10-04 10:23:25 -07008214 }
8215 if (!wpa_s->p2p_go_group_formation_completed) {
8216 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Marking group formation completed on GO on first data connection");
8217 wpa_s->p2p_go_group_formation_completed = 1;
8218 wpa_s->global->p2p_group_formation = NULL;
8219 wpa_s->p2p_in_provisioning = 0;
Dmitry Shmidt21de2142014-04-08 10:50:52 -07008220 wpa_s->p2p_in_invitation = 0;
Dmitry Shmidt56052862013-10-04 10:23:25 -07008221 }
Dmitry Shmidt92c368d2013-08-29 12:37:21 -07008222 wpa_s->global->p2p_go_wait_client.sec = 0;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008223 if (addr == NULL)
8224 return;
8225 wpas_p2p_add_persistent_group_client(wpa_s, addr);
8226}
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08008227
Dmitry Shmidt04949592012-07-19 12:16:46 -07008228
Dmitry Shmidt7f656022015-02-25 14:36:37 -08008229static int wpas_p2p_fallback_to_go_neg(struct wpa_supplicant *wpa_s,
8230 int group_added)
Dmitry Shmidt04949592012-07-19 12:16:46 -07008231{
8232 struct wpa_supplicant *group = wpa_s;
Dmitry Shmidt7f656022015-02-25 14:36:37 -08008233 int ret = 0;
8234
Dmitry Shmidt04949592012-07-19 12:16:46 -07008235 if (wpa_s->global->p2p_group_formation)
8236 group = wpa_s->global->p2p_group_formation;
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07008237 wpa_s = wpa_s->global->p2p_init_wpa_s;
Dmitry Shmidt04949592012-07-19 12:16:46 -07008238 offchannel_send_action_done(wpa_s);
8239 if (group_added)
Dmitry Shmidt7f656022015-02-25 14:36:37 -08008240 ret = wpas_p2p_group_delete(group, P2P_GROUP_REMOVAL_SILENT);
Dmitry Shmidt04949592012-07-19 12:16:46 -07008241 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Fall back to GO Negotiation");
8242 wpas_p2p_connect(wpa_s, wpa_s->pending_join_dev_addr, wpa_s->p2p_pin,
8243 wpa_s->p2p_wps_method, wpa_s->p2p_persistent_group, 0,
8244 0, 0, wpa_s->p2p_go_intent, wpa_s->p2p_connect_freq,
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08008245 wpa_s->p2p_go_vht_center_freq2,
Dmitry Shmidt04949592012-07-19 12:16:46 -07008246 wpa_s->p2p_persistent_id,
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07008247 wpa_s->p2p_pd_before_go_neg,
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07008248 wpa_s->p2p_go_ht40,
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08008249 wpa_s->p2p_go_vht,
Hai Shalom74f70d42019-02-11 14:42:39 -08008250 wpa_s->p2p_go_max_oper_chwidth,
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08008251 wpa_s->p2p_go_he,
8252 wpa_s->p2p_go_edmg,
8253 NULL, 0);
Dmitry Shmidt7f656022015-02-25 14:36:37 -08008254 return ret;
Dmitry Shmidt04949592012-07-19 12:16:46 -07008255}
8256
8257
8258int wpas_p2p_scan_no_go_seen(struct wpa_supplicant *wpa_s)
8259{
Dmitry Shmidt7f656022015-02-25 14:36:37 -08008260 int res;
8261
Dmitry Shmidt04949592012-07-19 12:16:46 -07008262 if (!wpa_s->p2p_fallback_to_go_neg ||
8263 wpa_s->p2p_in_provisioning <= 5)
8264 return 0;
8265
8266 if (wpas_p2p_peer_go(wpa_s, wpa_s->pending_join_dev_addr) > 0)
8267 return 0; /* peer operating as a GO */
8268
8269 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: GO not found for p2p_connect-auto - "
8270 "fallback to GO Negotiation");
Dmitry Shmidt9c175262016-03-03 10:20:07 -08008271 wpa_msg_global(wpa_s->p2pdev, MSG_INFO, P2P_EVENT_FALLBACK_TO_GO_NEG
Dmitry Shmidt7f656022015-02-25 14:36:37 -08008272 "reason=GO-not-found");
8273 res = wpas_p2p_fallback_to_go_neg(wpa_s, 1);
Dmitry Shmidt04949592012-07-19 12:16:46 -07008274
Dmitry Shmidt7f656022015-02-25 14:36:37 -08008275 return res == 1 ? 2 : 1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07008276}
8277
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07008278
8279unsigned int wpas_p2p_search_delay(struct wpa_supplicant *wpa_s)
8280{
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07008281 struct wpa_supplicant *ifs;
8282
8283 if (wpa_s->wpa_state > WPA_SCANNING) {
8284 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Use %u ms search delay due to "
8285 "concurrent operation",
Dmitry Shmidt09f57ba2014-06-10 16:07:13 -07008286 wpa_s->conf->p2p_search_delay);
8287 return wpa_s->conf->p2p_search_delay;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07008288 }
8289
Dmitry Shmidt01904cf2013-12-05 11:08:35 -08008290 dl_list_for_each(ifs, &wpa_s->radio->ifaces, struct wpa_supplicant,
8291 radio_list) {
8292 if (ifs != wpa_s && ifs->wpa_state > WPA_SCANNING) {
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07008293 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Use %u ms search "
8294 "delay due to concurrent operation on "
8295 "interface %s",
Dmitry Shmidt09f57ba2014-06-10 16:07:13 -07008296 wpa_s->conf->p2p_search_delay,
8297 ifs->ifname);
8298 return wpa_s->conf->p2p_search_delay;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07008299 }
8300 }
8301
8302 return 0;
8303}
8304
Dmitry Shmidt37d4d6a2013-03-18 13:09:42 -07008305
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07008306static int wpas_p2p_remove_psk_entry(struct wpa_supplicant *wpa_s,
8307 struct wpa_ssid *s, const u8 *addr,
8308 int iface_addr)
8309{
8310 struct psk_list_entry *psk, *tmp;
8311 int changed = 0;
8312
8313 dl_list_for_each_safe(psk, tmp, &s->psk_list, struct psk_list_entry,
8314 list) {
8315 if ((iface_addr && !psk->p2p &&
8316 os_memcmp(addr, psk->addr, ETH_ALEN) == 0) ||
8317 (!iface_addr && psk->p2p &&
8318 os_memcmp(addr, psk->addr, ETH_ALEN) == 0)) {
8319 wpa_dbg(wpa_s, MSG_DEBUG,
8320 "P2P: Remove persistent group PSK list entry for "
8321 MACSTR " p2p=%u",
8322 MAC2STR(psk->addr), psk->p2p);
8323 dl_list_del(&psk->list);
8324 os_free(psk);
8325 changed++;
8326 }
8327 }
8328
8329 return changed;
8330}
8331
8332
8333void wpas_p2p_new_psk_cb(struct wpa_supplicant *wpa_s, const u8 *mac_addr,
8334 const u8 *p2p_dev_addr,
8335 const u8 *psk, size_t psk_len)
8336{
8337 struct wpa_ssid *ssid = wpa_s->current_ssid;
8338 struct wpa_ssid *persistent;
Dmitry Shmidt7832adb2014-04-29 10:53:02 -07008339 struct psk_list_entry *p, *last;
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07008340
8341 if (psk_len != sizeof(p->psk))
8342 return;
8343
8344 if (p2p_dev_addr) {
8345 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: New PSK for addr=" MACSTR
8346 " p2p_dev_addr=" MACSTR,
8347 MAC2STR(mac_addr), MAC2STR(p2p_dev_addr));
8348 if (is_zero_ether_addr(p2p_dev_addr))
8349 p2p_dev_addr = NULL;
8350 } else {
8351 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: New PSK for addr=" MACSTR,
8352 MAC2STR(mac_addr));
8353 }
8354
8355 if (ssid->mode == WPAS_MODE_P2P_GROUP_FORMATION) {
8356 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: new_psk_cb during group formation");
8357 /* To be added to persistent group once created */
8358 if (wpa_s->global->add_psk == NULL) {
8359 wpa_s->global->add_psk = os_zalloc(sizeof(*p));
8360 if (wpa_s->global->add_psk == NULL)
8361 return;
8362 }
8363 p = wpa_s->global->add_psk;
8364 if (p2p_dev_addr) {
8365 p->p2p = 1;
8366 os_memcpy(p->addr, p2p_dev_addr, ETH_ALEN);
8367 } else {
8368 p->p2p = 0;
8369 os_memcpy(p->addr, mac_addr, ETH_ALEN);
8370 }
8371 os_memcpy(p->psk, psk, psk_len);
8372 return;
8373 }
8374
8375 if (ssid->mode != WPAS_MODE_P2P_GO || !ssid->p2p_persistent_group) {
8376 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Ignore new_psk_cb on not-persistent GO");
8377 return;
8378 }
8379
Dmitry Shmidt9c175262016-03-03 10:20:07 -08008380 persistent = wpas_p2p_get_persistent(wpa_s->p2pdev, NULL, ssid->ssid,
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07008381 ssid->ssid_len);
8382 if (!persistent) {
8383 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Could not find persistent group information to store the new PSK");
8384 return;
8385 }
8386
8387 p = os_zalloc(sizeof(*p));
8388 if (p == NULL)
8389 return;
8390 if (p2p_dev_addr) {
8391 p->p2p = 1;
8392 os_memcpy(p->addr, p2p_dev_addr, ETH_ALEN);
8393 } else {
8394 p->p2p = 0;
8395 os_memcpy(p->addr, mac_addr, ETH_ALEN);
8396 }
8397 os_memcpy(p->psk, psk, psk_len);
8398
Dmitry Shmidt7832adb2014-04-29 10:53:02 -07008399 if (dl_list_len(&persistent->psk_list) > P2P_MAX_STORED_CLIENTS &&
8400 (last = dl_list_last(&persistent->psk_list,
8401 struct psk_list_entry, list))) {
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07008402 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Remove oldest PSK entry for "
8403 MACSTR " (p2p=%u) to make room for a new one",
8404 MAC2STR(last->addr), last->p2p);
8405 dl_list_del(&last->list);
8406 os_free(last);
8407 }
8408
Dmitry Shmidt9c175262016-03-03 10:20:07 -08008409 wpas_p2p_remove_psk_entry(wpa_s->p2pdev, persistent,
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07008410 p2p_dev_addr ? p2p_dev_addr : mac_addr,
8411 p2p_dev_addr == NULL);
8412 if (p2p_dev_addr) {
8413 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Add new PSK for p2p_dev_addr="
8414 MACSTR, MAC2STR(p2p_dev_addr));
8415 } else {
8416 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Add new PSK for addr=" MACSTR,
8417 MAC2STR(mac_addr));
8418 }
8419 dl_list_add(&persistent->psk_list, &p->list);
8420
Dmitry Shmidt9c175262016-03-03 10:20:07 -08008421 if (wpa_s->p2pdev->conf->update_config &&
8422 wpa_config_write(wpa_s->p2pdev->confname, wpa_s->p2pdev->conf))
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07008423 wpa_printf(MSG_DEBUG, "P2P: Failed to update configuration");
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07008424}
8425
8426
8427static void wpas_p2p_remove_psk(struct wpa_supplicant *wpa_s,
8428 struct wpa_ssid *s, const u8 *addr,
8429 int iface_addr)
8430{
8431 int res;
8432
8433 res = wpas_p2p_remove_psk_entry(wpa_s, s, addr, iface_addr);
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08008434 if (res > 0 && wpa_s->conf->update_config &&
8435 wpa_config_write(wpa_s->confname, wpa_s->conf))
8436 wpa_dbg(wpa_s, MSG_DEBUG,
8437 "P2P: Failed to update configuration");
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07008438}
8439
8440
8441static void wpas_p2p_remove_client_go(struct wpa_supplicant *wpa_s,
8442 const u8 *peer, int iface_addr)
8443{
8444 struct hostapd_data *hapd;
8445 struct hostapd_wpa_psk *psk, *prev, *rem;
8446 struct sta_info *sta;
8447
8448 if (wpa_s->ap_iface == NULL || wpa_s->current_ssid == NULL ||
8449 wpa_s->current_ssid->mode != WPAS_MODE_P2P_GO)
8450 return;
8451
8452 /* Remove per-station PSK entry */
8453 hapd = wpa_s->ap_iface->bss[0];
8454 prev = NULL;
8455 psk = hapd->conf->ssid.wpa_psk;
8456 while (psk) {
8457 if ((iface_addr && os_memcmp(peer, psk->addr, ETH_ALEN) == 0) ||
8458 (!iface_addr &&
8459 os_memcmp(peer, psk->p2p_dev_addr, ETH_ALEN) == 0)) {
8460 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Remove operating group PSK entry for "
8461 MACSTR " iface_addr=%d",
8462 MAC2STR(peer), iface_addr);
8463 if (prev)
8464 prev->next = psk->next;
8465 else
8466 hapd->conf->ssid.wpa_psk = psk->next;
8467 rem = psk;
8468 psk = psk->next;
8469 os_free(rem);
8470 } else {
8471 prev = psk;
8472 psk = psk->next;
8473 }
8474 }
8475
8476 /* Disconnect from group */
8477 if (iface_addr)
8478 sta = ap_get_sta(hapd, peer);
8479 else
8480 sta = ap_get_sta_p2p(hapd, peer);
8481 if (sta) {
8482 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Disconnect peer " MACSTR
8483 " (iface_addr=%d) from group",
8484 MAC2STR(peer), iface_addr);
8485 hostapd_drv_sta_deauth(hapd, sta->addr,
8486 WLAN_REASON_DEAUTH_LEAVING);
8487 ap_sta_deauthenticate(hapd, sta, WLAN_REASON_DEAUTH_LEAVING);
8488 }
8489}
8490
8491
8492void wpas_p2p_remove_client(struct wpa_supplicant *wpa_s, const u8 *peer,
8493 int iface_addr)
8494{
8495 struct wpa_ssid *s;
8496 struct wpa_supplicant *w;
Dmitry Shmidt8bd70b72015-05-26 16:02:19 -07008497 struct wpa_supplicant *p2p_wpa_s = wpa_s->global->p2p_init_wpa_s;
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07008498
8499 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Remove client " MACSTR, MAC2STR(peer));
8500
8501 /* Remove from any persistent group */
Dmitry Shmidt8bd70b72015-05-26 16:02:19 -07008502 for (s = p2p_wpa_s->conf->ssid; s; s = s->next) {
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07008503 if (s->disabled != 2 || s->mode != WPAS_MODE_P2P_GO)
8504 continue;
8505 if (!iface_addr)
Dmitry Shmidt8bd70b72015-05-26 16:02:19 -07008506 wpas_remove_persistent_peer(p2p_wpa_s, s, peer, 0);
8507 wpas_p2p_remove_psk(p2p_wpa_s, s, peer, iface_addr);
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07008508 }
8509
8510 /* Remove from any operating group */
8511 for (w = wpa_s->global->ifaces; w; w = w->next)
8512 wpas_p2p_remove_client_go(w, peer, iface_addr);
8513}
8514
8515
8516static void wpas_p2p_psk_failure_removal(void *eloop_ctx, void *timeout_ctx)
8517{
8518 struct wpa_supplicant *wpa_s = eloop_ctx;
8519 wpas_p2p_group_delete(wpa_s, P2P_GROUP_REMOVAL_PSK_FAILURE);
8520}
8521
8522
Dmitry Shmidt04f534e2013-12-09 15:50:16 -08008523static void wpas_p2p_group_freq_conflict(void *eloop_ctx, void *timeout_ctx)
8524{
8525 struct wpa_supplicant *wpa_s = eloop_ctx;
8526
8527 wpa_printf(MSG_DEBUG, "P2P: Frequency conflict - terminate group");
8528 wpas_p2p_group_delete(wpa_s, P2P_GROUP_REMOVAL_FREQ_CONFLICT);
8529}
8530
8531
8532int wpas_p2p_handle_frequency_conflicts(struct wpa_supplicant *wpa_s, int freq,
8533 struct wpa_ssid *ssid)
8534{
8535 struct wpa_supplicant *iface;
8536
8537 for (iface = wpa_s->global->ifaces; iface; iface = iface->next) {
8538 if (!iface->current_ssid ||
8539 iface->current_ssid->frequency == freq ||
8540 (iface->p2p_group_interface == NOT_P2P_GROUP_INTERFACE &&
8541 !iface->current_ssid->p2p_group))
8542 continue;
8543
8544 /* Remove the connection with least priority */
8545 if (!wpas_is_p2p_prioritized(iface)) {
8546 /* STA connection has priority over existing
8547 * P2P connection, so remove the interface. */
8548 wpa_printf(MSG_DEBUG, "P2P: Removing P2P connection due to single channel concurrent mode frequency conflict");
8549 eloop_register_timeout(0, 0,
8550 wpas_p2p_group_freq_conflict,
8551 iface, NULL);
8552 /* If connection in progress is P2P connection, do not
8553 * proceed for the connection. */
8554 if (wpa_s == iface)
8555 return -1;
8556 else
8557 return 0;
8558 } else {
8559 /* P2P connection has priority, disable the STA network
8560 */
8561 wpa_supplicant_disable_network(wpa_s->global->ifaces,
8562 ssid);
8563 wpa_msg(wpa_s->global->ifaces, MSG_INFO,
8564 WPA_EVENT_FREQ_CONFLICT " id=%d", ssid->id);
8565 os_memset(wpa_s->global->ifaces->pending_bssid, 0,
8566 ETH_ALEN);
8567 /* If P2P connection is in progress, continue
8568 * connecting...*/
8569 if (wpa_s == iface)
8570 return 0;
8571 else
8572 return -1;
8573 }
8574 }
8575
8576 return 0;
8577}
8578
8579
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07008580int wpas_p2p_4way_hs_failed(struct wpa_supplicant *wpa_s)
8581{
8582 struct wpa_ssid *ssid = wpa_s->current_ssid;
8583
8584 if (ssid == NULL || !ssid->p2p_group)
8585 return 0;
8586
8587 if (wpa_s->p2p_last_4way_hs_fail &&
8588 wpa_s->p2p_last_4way_hs_fail == ssid) {
8589 u8 go_dev_addr[ETH_ALEN];
8590 struct wpa_ssid *persistent;
8591
8592 if (wpas_p2p_persistent_group(wpa_s, go_dev_addr,
8593 ssid->ssid,
8594 ssid->ssid_len) <= 0) {
8595 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Could not determine whether 4-way handshake failures were for a persistent group");
8596 goto disconnect;
8597 }
8598
8599 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Two 4-way handshake failures for a P2P group - go_dev_addr="
8600 MACSTR, MAC2STR(go_dev_addr));
Dmitry Shmidt9c175262016-03-03 10:20:07 -08008601 persistent = wpas_p2p_get_persistent(wpa_s->p2pdev, go_dev_addr,
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07008602 ssid->ssid,
8603 ssid->ssid_len);
8604 if (persistent == NULL || persistent->mode != WPAS_MODE_INFRA) {
8605 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: No matching persistent group stored");
8606 goto disconnect;
8607 }
Dmitry Shmidt9c175262016-03-03 10:20:07 -08008608 wpa_msg_global(wpa_s->p2pdev, MSG_INFO,
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07008609 P2P_EVENT_PERSISTENT_PSK_FAIL "%d",
8610 persistent->id);
8611 disconnect:
8612 wpa_s->p2p_last_4way_hs_fail = NULL;
8613 /*
8614 * Remove the group from a timeout to avoid issues with caller
8615 * continuing to use the interface if this is on a P2P group
8616 * interface.
8617 */
8618 eloop_register_timeout(0, 0, wpas_p2p_psk_failure_removal,
8619 wpa_s, NULL);
8620 return 1;
8621 }
8622
8623 wpa_s->p2p_last_4way_hs_fail = ssid;
8624 return 0;
8625}
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08008626
8627
8628#ifdef CONFIG_WPS_NFC
8629
8630static struct wpabuf * wpas_p2p_nfc_handover(int ndef, struct wpabuf *wsc,
8631 struct wpabuf *p2p)
8632{
8633 struct wpabuf *ret;
8634 size_t wsc_len;
8635
8636 if (p2p == NULL) {
8637 wpabuf_free(wsc);
8638 wpa_printf(MSG_DEBUG, "P2P: No p2p buffer for handover");
8639 return NULL;
8640 }
8641
8642 wsc_len = wsc ? wpabuf_len(wsc) : 0;
8643 ret = wpabuf_alloc(2 + wsc_len + 2 + wpabuf_len(p2p));
8644 if (ret == NULL) {
8645 wpabuf_free(wsc);
8646 wpabuf_free(p2p);
8647 return NULL;
8648 }
8649
8650 wpabuf_put_be16(ret, wsc_len);
8651 if (wsc)
8652 wpabuf_put_buf(ret, wsc);
8653 wpabuf_put_be16(ret, wpabuf_len(p2p));
8654 wpabuf_put_buf(ret, p2p);
8655
8656 wpabuf_free(wsc);
8657 wpabuf_free(p2p);
8658 wpa_hexdump_buf(MSG_DEBUG,
8659 "P2P: Generated NFC connection handover message", ret);
8660
8661 if (ndef && ret) {
8662 struct wpabuf *tmp;
8663 tmp = ndef_build_p2p(ret);
8664 wpabuf_free(ret);
8665 if (tmp == NULL) {
8666 wpa_printf(MSG_DEBUG, "P2P: Failed to NDEF encapsulate handover request");
8667 return NULL;
8668 }
8669 ret = tmp;
8670 }
8671
8672 return ret;
8673}
8674
8675
8676static int wpas_p2p_cli_freq(struct wpa_supplicant *wpa_s,
8677 struct wpa_ssid **ssid, u8 *go_dev_addr)
8678{
8679 struct wpa_supplicant *iface;
8680
8681 if (go_dev_addr)
8682 os_memset(go_dev_addr, 0, ETH_ALEN);
8683 if (ssid)
8684 *ssid = NULL;
8685 for (iface = wpa_s->global->ifaces; iface; iface = iface->next) {
8686 if (iface->wpa_state < WPA_ASSOCIATING ||
8687 iface->current_ssid == NULL || iface->assoc_freq == 0 ||
8688 !iface->current_ssid->p2p_group ||
8689 iface->current_ssid->mode != WPAS_MODE_INFRA)
8690 continue;
8691 if (ssid)
8692 *ssid = iface->current_ssid;
8693 if (go_dev_addr)
8694 os_memcpy(go_dev_addr, iface->go_dev_addr, ETH_ALEN);
8695 return iface->assoc_freq;
8696 }
8697 return 0;
8698}
8699
8700
8701struct wpabuf * wpas_p2p_nfc_handover_req(struct wpa_supplicant *wpa_s,
8702 int ndef)
8703{
8704 struct wpabuf *wsc, *p2p;
8705 struct wpa_ssid *ssid;
8706 u8 go_dev_addr[ETH_ALEN];
8707 int cli_freq = wpas_p2p_cli_freq(wpa_s, &ssid, go_dev_addr);
8708
8709 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL) {
8710 wpa_printf(MSG_DEBUG, "P2P: P2P disabled - cannot build handover request");
8711 return NULL;
8712 }
8713
8714 if (wpa_s->conf->wps_nfc_dh_pubkey == NULL &&
8715 wps_nfc_gen_dh(&wpa_s->conf->wps_nfc_dh_pubkey,
8716 &wpa_s->conf->wps_nfc_dh_privkey) < 0) {
8717 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: No DH key available for handover request");
8718 return NULL;
8719 }
8720
8721 if (cli_freq == 0) {
8722 wsc = wps_build_nfc_handover_req_p2p(
8723 wpa_s->parent->wps, wpa_s->conf->wps_nfc_dh_pubkey);
8724 } else
8725 wsc = NULL;
8726 p2p = p2p_build_nfc_handover_req(wpa_s->global->p2p, cli_freq,
8727 go_dev_addr, ssid ? ssid->ssid : NULL,
8728 ssid ? ssid->ssid_len : 0);
8729
8730 return wpas_p2p_nfc_handover(ndef, wsc, p2p);
8731}
8732
8733
8734struct wpabuf * wpas_p2p_nfc_handover_sel(struct wpa_supplicant *wpa_s,
8735 int ndef, int tag)
8736{
8737 struct wpabuf *wsc, *p2p;
8738 struct wpa_ssid *ssid;
8739 u8 go_dev_addr[ETH_ALEN];
8740 int cli_freq = wpas_p2p_cli_freq(wpa_s, &ssid, go_dev_addr);
8741
8742 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
8743 return NULL;
8744
8745 if (!tag && wpa_s->conf->wps_nfc_dh_pubkey == NULL &&
8746 wps_nfc_gen_dh(&wpa_s->conf->wps_nfc_dh_pubkey,
8747 &wpa_s->conf->wps_nfc_dh_privkey) < 0)
8748 return NULL;
8749
8750 if (cli_freq == 0) {
8751 wsc = wps_build_nfc_handover_sel_p2p(
8752 wpa_s->parent->wps,
8753 tag ? wpa_s->conf->wps_nfc_dev_pw_id :
8754 DEV_PW_NFC_CONNECTION_HANDOVER,
8755 wpa_s->conf->wps_nfc_dh_pubkey,
8756 tag ? wpa_s->conf->wps_nfc_dev_pw : NULL);
8757 } else
8758 wsc = NULL;
8759 p2p = p2p_build_nfc_handover_sel(wpa_s->global->p2p, cli_freq,
8760 go_dev_addr, ssid ? ssid->ssid : NULL,
8761 ssid ? ssid->ssid_len : 0);
8762
8763 return wpas_p2p_nfc_handover(ndef, wsc, p2p);
8764}
8765
8766
8767static int wpas_p2p_nfc_join_group(struct wpa_supplicant *wpa_s,
8768 struct p2p_nfc_params *params)
8769{
8770 wpa_printf(MSG_DEBUG, "P2P: Initiate join-group based on NFC "
8771 "connection handover (freq=%d)",
8772 params->go_freq);
8773
8774 if (params->go_freq && params->go_ssid_len) {
8775 wpa_s->p2p_wps_method = WPS_NFC;
8776 wpa_s->pending_join_wps_method = WPS_NFC;
8777 os_memset(wpa_s->pending_join_iface_addr, 0, ETH_ALEN);
8778 os_memcpy(wpa_s->pending_join_dev_addr, params->go_dev_addr,
8779 ETH_ALEN);
8780 return wpas_p2p_join_start(wpa_s, params->go_freq,
8781 params->go_ssid,
8782 params->go_ssid_len);
8783 }
8784
8785 return wpas_p2p_connect(wpa_s, params->peer->p2p_device_addr, NULL,
8786 WPS_NFC, 0, 0, 1, 0, wpa_s->conf->p2p_go_intent,
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08008787 params->go_freq, wpa_s->p2p_go_vht_center_freq2,
Dmitry Shmidtde47be72016-01-07 12:52:55 -08008788 -1, 0, 1, 1, wpa_s->p2p_go_max_oper_chwidth,
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08008789 wpa_s->p2p_go_he, wpa_s->p2p_go_edmg,
Dmitry Shmidtde47be72016-01-07 12:52:55 -08008790 params->go_ssid_len ? params->go_ssid : NULL,
8791 params->go_ssid_len);
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08008792}
8793
8794
8795static int wpas_p2p_nfc_auth_join(struct wpa_supplicant *wpa_s,
8796 struct p2p_nfc_params *params, int tag)
8797{
8798 int res, persistent;
8799 struct wpa_ssid *ssid;
8800
8801 wpa_printf(MSG_DEBUG, "P2P: Authorize join-group based on NFC "
8802 "connection handover");
8803 for (wpa_s = wpa_s->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
8804 ssid = wpa_s->current_ssid;
8805 if (ssid == NULL)
8806 continue;
8807 if (ssid->mode != WPAS_MODE_P2P_GO)
8808 continue;
8809 if (wpa_s->ap_iface == NULL)
8810 continue;
8811 break;
8812 }
8813 if (wpa_s == NULL) {
8814 wpa_printf(MSG_DEBUG, "P2P: Could not find GO interface");
8815 return -1;
8816 }
8817
Dmitry Shmidt849734c2016-05-27 09:59:01 -07008818 if (wpa_s->p2pdev->p2p_oob_dev_pw_id !=
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08008819 DEV_PW_NFC_CONNECTION_HANDOVER &&
Dmitry Shmidt849734c2016-05-27 09:59:01 -07008820 !wpa_s->p2pdev->p2p_oob_dev_pw) {
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08008821 wpa_printf(MSG_DEBUG, "P2P: No NFC Dev Pw known");
8822 return -1;
8823 }
8824 res = wpas_ap_wps_add_nfc_pw(
Dmitry Shmidt849734c2016-05-27 09:59:01 -07008825 wpa_s, wpa_s->p2pdev->p2p_oob_dev_pw_id,
8826 wpa_s->p2pdev->p2p_oob_dev_pw,
8827 wpa_s->p2pdev->p2p_peer_oob_pk_hash_known ?
8828 wpa_s->p2pdev->p2p_peer_oob_pubkey_hash : NULL);
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08008829 if (res)
8830 return res;
8831
8832 if (!tag) {
8833 wpa_printf(MSG_DEBUG, "P2P: Negotiated handover - wait for peer to join without invitation");
8834 return 0;
8835 }
8836
8837 if (!params->peer ||
8838 !(params->peer->dev_capab & P2P_DEV_CAPAB_INVITATION_PROCEDURE))
8839 return 0;
8840
8841 wpa_printf(MSG_DEBUG, "P2P: Static handover - invite peer " MACSTR
8842 " to join", MAC2STR(params->peer->p2p_device_addr));
8843
8844 wpa_s->global->p2p_invite_group = wpa_s;
8845 persistent = ssid->p2p_persistent_group &&
Dmitry Shmidt849734c2016-05-27 09:59:01 -07008846 wpas_p2p_get_persistent(wpa_s->p2pdev,
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08008847 params->peer->p2p_device_addr,
8848 ssid->ssid, ssid->ssid_len);
Dmitry Shmidt849734c2016-05-27 09:59:01 -07008849 wpa_s->p2pdev->pending_invite_ssid_id = -1;
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08008850
8851 return p2p_invite(wpa_s->global->p2p, params->peer->p2p_device_addr,
8852 P2P_INVITE_ROLE_ACTIVE_GO, wpa_s->own_addr,
8853 ssid->ssid, ssid->ssid_len, ssid->frequency,
8854 wpa_s->global->p2p_dev_addr, persistent, 0,
Dmitry Shmidt849734c2016-05-27 09:59:01 -07008855 wpa_s->p2pdev->p2p_oob_dev_pw_id);
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08008856}
8857
8858
8859static int wpas_p2p_nfc_init_go_neg(struct wpa_supplicant *wpa_s,
8860 struct p2p_nfc_params *params,
8861 int forced_freq)
8862{
8863 wpa_printf(MSG_DEBUG, "P2P: Initiate GO Negotiation based on NFC "
8864 "connection handover");
8865 return wpas_p2p_connect(wpa_s, params->peer->p2p_device_addr, NULL,
8866 WPS_NFC, 0, 0, 0, 0, wpa_s->conf->p2p_go_intent,
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08008867 forced_freq, wpa_s->p2p_go_vht_center_freq2,
Dmitry Shmidtde47be72016-01-07 12:52:55 -08008868 -1, 0, 1, 1, wpa_s->p2p_go_max_oper_chwidth,
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08008869 wpa_s->p2p_go_he, wpa_s->p2p_go_edmg,
8870 NULL, 0);
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08008871}
8872
8873
8874static int wpas_p2p_nfc_resp_go_neg(struct wpa_supplicant *wpa_s,
8875 struct p2p_nfc_params *params,
8876 int forced_freq)
8877{
8878 int res;
8879
8880 wpa_printf(MSG_DEBUG, "P2P: Authorize GO Negotiation based on NFC "
8881 "connection handover");
8882 res = wpas_p2p_connect(wpa_s, params->peer->p2p_device_addr, NULL,
8883 WPS_NFC, 0, 0, 0, 1, wpa_s->conf->p2p_go_intent,
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08008884 forced_freq, wpa_s->p2p_go_vht_center_freq2,
Dmitry Shmidtde47be72016-01-07 12:52:55 -08008885 -1, 0, 1, 1, wpa_s->p2p_go_max_oper_chwidth,
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08008886 wpa_s->p2p_go_he, wpa_s->p2p_go_edmg,
8887 NULL, 0);
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08008888 if (res)
8889 return res;
8890
8891 res = wpas_p2p_listen(wpa_s, 60);
8892 if (res) {
8893 p2p_unauthorize(wpa_s->global->p2p,
8894 params->peer->p2p_device_addr);
8895 }
8896
8897 return res;
8898}
8899
8900
8901static int wpas_p2p_nfc_connection_handover(struct wpa_supplicant *wpa_s,
8902 const struct wpabuf *data,
8903 int sel, int tag, int forced_freq)
8904{
8905 const u8 *pos, *end;
8906 u16 len, id;
8907 struct p2p_nfc_params params;
8908 int res;
8909
8910 os_memset(&params, 0, sizeof(params));
8911 params.sel = sel;
8912
8913 wpa_hexdump_buf(MSG_DEBUG, "P2P: Received NFC tag payload", data);
8914
8915 pos = wpabuf_head(data);
8916 end = pos + wpabuf_len(data);
8917
8918 if (end - pos < 2) {
8919 wpa_printf(MSG_DEBUG, "P2P: Not enough data for Length of WSC "
8920 "attributes");
8921 return -1;
8922 }
8923 len = WPA_GET_BE16(pos);
8924 pos += 2;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08008925 if (len > end - pos) {
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08008926 wpa_printf(MSG_DEBUG, "P2P: Not enough data for WSC "
8927 "attributes");
8928 return -1;
8929 }
8930 params.wsc_attr = pos;
8931 params.wsc_len = len;
8932 pos += len;
8933
8934 if (end - pos < 2) {
8935 wpa_printf(MSG_DEBUG, "P2P: Not enough data for Length of P2P "
8936 "attributes");
8937 return -1;
8938 }
8939 len = WPA_GET_BE16(pos);
8940 pos += 2;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08008941 if (len > end - pos) {
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08008942 wpa_printf(MSG_DEBUG, "P2P: Not enough data for P2P "
8943 "attributes");
8944 return -1;
8945 }
8946 params.p2p_attr = pos;
8947 params.p2p_len = len;
8948 pos += len;
8949
8950 wpa_hexdump(MSG_DEBUG, "P2P: WSC attributes",
8951 params.wsc_attr, params.wsc_len);
8952 wpa_hexdump(MSG_DEBUG, "P2P: P2P attributes",
8953 params.p2p_attr, params.p2p_len);
8954 if (pos < end) {
8955 wpa_hexdump(MSG_DEBUG,
8956 "P2P: Ignored extra data after P2P attributes",
8957 pos, end - pos);
8958 }
8959
8960 res = p2p_process_nfc_connection_handover(wpa_s->global->p2p, &params);
8961 if (res)
8962 return res;
8963
8964 if (params.next_step == NO_ACTION)
8965 return 0;
8966
8967 if (params.next_step == BOTH_GO) {
8968 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_NFC_BOTH_GO "peer=" MACSTR,
8969 MAC2STR(params.peer->p2p_device_addr));
8970 return 0;
8971 }
8972
8973 if (params.next_step == PEER_CLIENT) {
8974 if (!is_zero_ether_addr(params.go_dev_addr)) {
8975 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_NFC_PEER_CLIENT
8976 "peer=" MACSTR " freq=%d go_dev_addr=" MACSTR
8977 " ssid=\"%s\"",
8978 MAC2STR(params.peer->p2p_device_addr),
8979 params.go_freq,
8980 MAC2STR(params.go_dev_addr),
8981 wpa_ssid_txt(params.go_ssid,
8982 params.go_ssid_len));
8983 } else {
8984 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_NFC_PEER_CLIENT
8985 "peer=" MACSTR " freq=%d",
8986 MAC2STR(params.peer->p2p_device_addr),
8987 params.go_freq);
8988 }
8989 return 0;
8990 }
8991
8992 if (wpas_p2p_cli_freq(wpa_s, NULL, NULL)) {
8993 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_NFC_WHILE_CLIENT "peer="
8994 MACSTR, MAC2STR(params.peer->p2p_device_addr));
8995 return 0;
8996 }
8997
8998 wpabuf_free(wpa_s->p2p_oob_dev_pw);
8999 wpa_s->p2p_oob_dev_pw = NULL;
9000
9001 if (params.oob_dev_pw_len < WPS_OOB_PUBKEY_HASH_LEN + 2) {
9002 wpa_printf(MSG_DEBUG, "P2P: No peer OOB Dev Pw "
9003 "received");
9004 return -1;
9005 }
9006
9007 id = WPA_GET_BE16(params.oob_dev_pw + WPS_OOB_PUBKEY_HASH_LEN);
9008 wpa_printf(MSG_DEBUG, "P2P: Peer OOB Dev Pw %u", id);
9009 wpa_hexdump(MSG_DEBUG, "P2P: Peer OOB Public Key hash",
9010 params.oob_dev_pw, WPS_OOB_PUBKEY_HASH_LEN);
9011 os_memcpy(wpa_s->p2p_peer_oob_pubkey_hash,
9012 params.oob_dev_pw, WPS_OOB_PUBKEY_HASH_LEN);
9013 wpa_s->p2p_peer_oob_pk_hash_known = 1;
9014
9015 if (tag) {
9016 if (id < 0x10) {
9017 wpa_printf(MSG_DEBUG, "P2P: Static handover - invalid "
9018 "peer OOB Device Password Id %u", id);
9019 return -1;
9020 }
9021 wpa_printf(MSG_DEBUG, "P2P: Static handover - use peer OOB "
9022 "Device Password Id %u", id);
9023 wpa_hexdump_key(MSG_DEBUG, "P2P: Peer OOB Device Password",
9024 params.oob_dev_pw + WPS_OOB_PUBKEY_HASH_LEN + 2,
9025 params.oob_dev_pw_len -
9026 WPS_OOB_PUBKEY_HASH_LEN - 2);
9027 wpa_s->p2p_oob_dev_pw_id = id;
9028 wpa_s->p2p_oob_dev_pw = wpabuf_alloc_copy(
9029 params.oob_dev_pw + WPS_OOB_PUBKEY_HASH_LEN + 2,
9030 params.oob_dev_pw_len -
9031 WPS_OOB_PUBKEY_HASH_LEN - 2);
9032 if (wpa_s->p2p_oob_dev_pw == NULL)
9033 return -1;
9034
9035 if (wpa_s->conf->wps_nfc_dh_pubkey == NULL &&
9036 wps_nfc_gen_dh(&wpa_s->conf->wps_nfc_dh_pubkey,
9037 &wpa_s->conf->wps_nfc_dh_privkey) < 0)
9038 return -1;
9039 } else {
9040 wpa_printf(MSG_DEBUG, "P2P: Using abbreviated WPS handshake "
9041 "without Device Password");
9042 wpa_s->p2p_oob_dev_pw_id = DEV_PW_NFC_CONNECTION_HANDOVER;
9043 }
9044
9045 switch (params.next_step) {
9046 case NO_ACTION:
9047 case BOTH_GO:
9048 case PEER_CLIENT:
9049 /* already covered above */
9050 return 0;
9051 case JOIN_GROUP:
9052 return wpas_p2p_nfc_join_group(wpa_s, &params);
9053 case AUTH_JOIN:
9054 return wpas_p2p_nfc_auth_join(wpa_s, &params, tag);
9055 case INIT_GO_NEG:
9056 return wpas_p2p_nfc_init_go_neg(wpa_s, &params, forced_freq);
9057 case RESP_GO_NEG:
9058 /* TODO: use own OOB Dev Pw */
9059 return wpas_p2p_nfc_resp_go_neg(wpa_s, &params, forced_freq);
9060 }
9061
9062 return -1;
9063}
9064
9065
9066int wpas_p2p_nfc_tag_process(struct wpa_supplicant *wpa_s,
9067 const struct wpabuf *data, int forced_freq)
9068{
9069 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
9070 return -1;
9071
9072 return wpas_p2p_nfc_connection_handover(wpa_s, data, 1, 1, forced_freq);
9073}
9074
9075
9076int wpas_p2p_nfc_report_handover(struct wpa_supplicant *wpa_s, int init,
9077 const struct wpabuf *req,
9078 const struct wpabuf *sel, int forced_freq)
9079{
9080 struct wpabuf *tmp;
9081 int ret;
9082
9083 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
9084 return -1;
9085
9086 wpa_printf(MSG_DEBUG, "NFC: P2P connection handover reported");
9087
9088 wpa_hexdump_ascii(MSG_DEBUG, "NFC: Req",
9089 wpabuf_head(req), wpabuf_len(req));
9090 wpa_hexdump_ascii(MSG_DEBUG, "NFC: Sel",
9091 wpabuf_head(sel), wpabuf_len(sel));
9092 if (forced_freq)
9093 wpa_printf(MSG_DEBUG, "NFC: Forced freq %d", forced_freq);
9094 tmp = ndef_parse_p2p(init ? sel : req);
9095 if (tmp == NULL) {
9096 wpa_printf(MSG_DEBUG, "P2P: Could not parse NDEF");
9097 return -1;
9098 }
9099
9100 ret = wpas_p2p_nfc_connection_handover(wpa_s, tmp, init, 0,
9101 forced_freq);
9102 wpabuf_free(tmp);
9103
9104 return ret;
9105}
9106
9107
9108int wpas_p2p_nfc_tag_enabled(struct wpa_supplicant *wpa_s, int enabled)
9109{
9110 const u8 *if_addr;
9111 int go_intent = wpa_s->conf->p2p_go_intent;
9112 struct wpa_supplicant *iface;
9113
9114 if (wpa_s->global->p2p == NULL)
9115 return -1;
9116
9117 if (!enabled) {
9118 wpa_printf(MSG_DEBUG, "P2P: Disable use of own NFC Tag");
9119 for (iface = wpa_s->global->ifaces; iface; iface = iface->next)
9120 {
9121 if (!iface->ap_iface)
9122 continue;
9123 hostapd_wps_nfc_token_disable(iface->ap_iface->bss[0]);
9124 }
9125 p2p_set_authorized_oob_dev_pw_id(wpa_s->global->p2p, 0,
9126 0, NULL);
9127 if (wpa_s->p2p_nfc_tag_enabled)
9128 wpas_p2p_remove_pending_group_interface(wpa_s);
9129 wpa_s->p2p_nfc_tag_enabled = 0;
9130 return 0;
9131 }
9132
9133 if (wpa_s->global->p2p_disabled)
9134 return -1;
9135
9136 if (wpa_s->conf->wps_nfc_dh_pubkey == NULL ||
9137 wpa_s->conf->wps_nfc_dh_privkey == NULL ||
9138 wpa_s->conf->wps_nfc_dev_pw == NULL ||
9139 wpa_s->conf->wps_nfc_dev_pw_id < 0x10) {
9140 wpa_printf(MSG_DEBUG, "P2P: NFC password token not configured "
9141 "to allow static handover cases");
9142 return -1;
9143 }
9144
9145 wpa_printf(MSG_DEBUG, "P2P: Enable use of own NFC Tag");
9146
9147 wpa_s->p2p_oob_dev_pw_id = wpa_s->conf->wps_nfc_dev_pw_id;
9148 wpabuf_free(wpa_s->p2p_oob_dev_pw);
9149 wpa_s->p2p_oob_dev_pw = wpabuf_dup(wpa_s->conf->wps_nfc_dev_pw);
9150 if (wpa_s->p2p_oob_dev_pw == NULL)
9151 return -1;
9152 wpa_s->p2p_peer_oob_pk_hash_known = 0;
9153
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -07009154 if (wpa_s->p2p_group_interface == P2P_GROUP_INTERFACE_GO ||
9155 wpa_s->p2p_group_interface == P2P_GROUP_INTERFACE_CLIENT) {
9156 /*
9157 * P2P Group Interface present and the command came on group
9158 * interface, so enable the token for the current interface.
9159 */
9160 wpa_s->create_p2p_iface = 0;
9161 } else {
9162 wpa_s->create_p2p_iface = wpas_p2p_create_iface(wpa_s);
9163 }
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08009164
9165 if (wpa_s->create_p2p_iface) {
9166 enum wpa_driver_if_type iftype;
9167 /* Prepare to add a new interface for the group */
9168 iftype = WPA_IF_P2P_GROUP;
9169 if (go_intent == 15)
9170 iftype = WPA_IF_P2P_GO;
9171 if (wpas_p2p_add_group_interface(wpa_s, iftype) < 0) {
9172 wpa_printf(MSG_ERROR, "P2P: Failed to allocate a new "
9173 "interface for the group");
9174 return -1;
9175 }
9176
9177 if_addr = wpa_s->pending_interface_addr;
Dmitry Shmidt9c175262016-03-03 10:20:07 -08009178 } else if (wpa_s->p2p_mgmt)
9179 if_addr = wpa_s->parent->own_addr;
9180 else
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08009181 if_addr = wpa_s->own_addr;
9182
9183 wpa_s->p2p_nfc_tag_enabled = enabled;
9184
9185 for (iface = wpa_s->global->ifaces; iface; iface = iface->next) {
9186 struct hostapd_data *hapd;
9187 if (iface->ap_iface == NULL)
9188 continue;
9189 hapd = iface->ap_iface->bss[0];
9190 wpabuf_free(hapd->conf->wps_nfc_dh_pubkey);
9191 hapd->conf->wps_nfc_dh_pubkey =
9192 wpabuf_dup(wpa_s->conf->wps_nfc_dh_pubkey);
9193 wpabuf_free(hapd->conf->wps_nfc_dh_privkey);
9194 hapd->conf->wps_nfc_dh_privkey =
9195 wpabuf_dup(wpa_s->conf->wps_nfc_dh_privkey);
9196 wpabuf_free(hapd->conf->wps_nfc_dev_pw);
9197 hapd->conf->wps_nfc_dev_pw =
9198 wpabuf_dup(wpa_s->conf->wps_nfc_dev_pw);
9199 hapd->conf->wps_nfc_dev_pw_id = wpa_s->conf->wps_nfc_dev_pw_id;
9200
9201 if (hostapd_wps_nfc_token_enable(iface->ap_iface->bss[0]) < 0) {
9202 wpa_dbg(iface, MSG_DEBUG,
9203 "P2P: Failed to enable NFC Tag for GO");
9204 }
9205 }
9206 p2p_set_authorized_oob_dev_pw_id(
9207 wpa_s->global->p2p, wpa_s->conf->wps_nfc_dev_pw_id, go_intent,
9208 if_addr);
9209
9210 return 0;
9211}
9212
9213#endif /* CONFIG_WPS_NFC */
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07009214
9215
9216static void wpas_p2p_optimize_listen_channel(struct wpa_supplicant *wpa_s,
9217 struct wpa_used_freq_data *freqs,
9218 unsigned int num)
9219{
9220 u8 curr_chan, cand, chan;
9221 unsigned int i;
9222
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08009223 /*
9224 * If possible, optimize the Listen channel to be a channel that is
9225 * already used by one of the other interfaces.
9226 */
9227 if (!wpa_s->conf->p2p_optimize_listen_chan)
9228 return;
9229
9230 if (!wpa_s->current_ssid || wpa_s->wpa_state != WPA_COMPLETED)
9231 return;
9232
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07009233 curr_chan = p2p_get_listen_channel(wpa_s->global->p2p);
9234 for (i = 0, cand = 0; i < num; i++) {
9235 ieee80211_freq_to_chan(freqs[i].freq, &chan);
9236 if (curr_chan == chan) {
9237 cand = 0;
9238 break;
9239 }
9240
9241 if (chan == 1 || chan == 6 || chan == 11)
9242 cand = chan;
9243 }
9244
9245 if (cand) {
9246 wpa_dbg(wpa_s, MSG_DEBUG,
Dmitry Shmidt216983b2015-02-06 10:50:36 -08009247 "P2P: Update Listen channel to %u based on operating channel",
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07009248 cand);
9249 p2p_set_listen_channel(wpa_s->global->p2p, 81, cand, 0);
9250 }
9251}
9252
9253
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08009254static int wpas_p2p_move_go_csa(struct wpa_supplicant *wpa_s)
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07009255{
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08009256 struct hostapd_config *conf;
9257 struct p2p_go_neg_results params;
9258 struct csa_settings csa_settings;
9259 struct wpa_ssid *current_ssid = wpa_s->current_ssid;
9260 int old_freq = current_ssid->frequency;
9261 int ret;
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07009262
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08009263 if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_AP_CSA)) {
9264 wpa_dbg(wpa_s, MSG_DEBUG, "CSA is not enabled");
9265 return -1;
9266 }
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07009267
9268 /*
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08009269 * TODO: This function may not always work correctly. For example,
9270 * when we have a running GO and a BSS on a DFS channel.
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07009271 */
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08009272 if (wpas_p2p_init_go_params(wpa_s, &params, 0, 0, 0, 0, 0, 0, 0,
9273 NULL)) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08009274 wpa_dbg(wpa_s, MSG_DEBUG,
9275 "P2P CSA: Failed to select new frequency for GO");
9276 return -1;
9277 }
9278
9279 if (current_ssid->frequency == params.freq) {
9280 wpa_dbg(wpa_s, MSG_DEBUG,
9281 "P2P CSA: Selected same frequency - not moving GO");
9282 return 0;
9283 }
9284
9285 conf = hostapd_config_defaults();
9286 if (!conf) {
9287 wpa_dbg(wpa_s, MSG_DEBUG,
9288 "P2P CSA: Failed to allocate default config");
9289 return -1;
9290 }
9291
9292 current_ssid->frequency = params.freq;
9293 if (wpa_supplicant_conf_ap_ht(wpa_s, current_ssid, conf)) {
9294 wpa_dbg(wpa_s, MSG_DEBUG,
9295 "P2P CSA: Failed to create new GO config");
9296 ret = -1;
9297 goto out;
9298 }
9299
9300 if (conf->hw_mode != wpa_s->ap_iface->current_mode->mode) {
9301 wpa_dbg(wpa_s, MSG_DEBUG,
9302 "P2P CSA: CSA to a different band is not supported");
9303 ret = -1;
9304 goto out;
9305 }
9306
9307 os_memset(&csa_settings, 0, sizeof(csa_settings));
9308 csa_settings.cs_count = P2P_GO_CSA_COUNT;
9309 csa_settings.block_tx = P2P_GO_CSA_BLOCK_TX;
9310 csa_settings.freq_params.freq = params.freq;
9311 csa_settings.freq_params.sec_channel_offset = conf->secondary_channel;
9312 csa_settings.freq_params.ht_enabled = conf->ieee80211n;
9313 csa_settings.freq_params.bandwidth = conf->secondary_channel ? 40 : 20;
9314
9315 if (conf->ieee80211ac) {
9316 int freq1 = 0, freq2 = 0;
9317 u8 chan, opclass;
9318
9319 if (ieee80211_freq_to_channel_ext(params.freq,
9320 conf->secondary_channel,
9321 conf->vht_oper_chwidth,
9322 &opclass, &chan) ==
9323 NUM_HOSTAPD_MODES) {
9324 wpa_printf(MSG_ERROR, "P2P CSA: Bad freq");
9325 ret = -1;
9326 goto out;
9327 }
9328
9329 if (conf->vht_oper_centr_freq_seg0_idx)
9330 freq1 = ieee80211_chan_to_freq(
9331 NULL, opclass,
9332 conf->vht_oper_centr_freq_seg0_idx);
9333
9334 if (conf->vht_oper_centr_freq_seg1_idx)
9335 freq2 = ieee80211_chan_to_freq(
9336 NULL, opclass,
9337 conf->vht_oper_centr_freq_seg1_idx);
9338
9339 if (freq1 < 0 || freq2 < 0) {
9340 wpa_dbg(wpa_s, MSG_DEBUG,
9341 "P2P CSA: Selected invalid VHT center freqs");
9342 ret = -1;
9343 goto out;
9344 }
9345
9346 csa_settings.freq_params.vht_enabled = conf->ieee80211ac;
9347 csa_settings.freq_params.center_freq1 = freq1;
9348 csa_settings.freq_params.center_freq2 = freq2;
9349
9350 switch (conf->vht_oper_chwidth) {
Hai Shalom81f62d82019-07-22 12:10:00 -07009351 case CHANWIDTH_80MHZ:
9352 case CHANWIDTH_80P80MHZ:
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08009353 csa_settings.freq_params.bandwidth = 80;
9354 break;
Hai Shalom81f62d82019-07-22 12:10:00 -07009355 case CHANWIDTH_160MHZ:
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08009356 csa_settings.freq_params.bandwidth = 160;
9357 break;
9358 }
9359 }
9360
9361 ret = ap_switch_channel(wpa_s, &csa_settings);
9362out:
9363 current_ssid->frequency = old_freq;
9364 hostapd_config_free(conf);
9365 return ret;
9366}
9367
9368
9369static void wpas_p2p_move_go_no_csa(struct wpa_supplicant *wpa_s)
9370{
9371 struct p2p_go_neg_results params;
9372 struct wpa_ssid *current_ssid = wpa_s->current_ssid;
9373
9374 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_REMOVE_AND_REFORM_GROUP);
9375
9376 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Move GO from freq=%d MHz",
9377 current_ssid->frequency);
9378
9379 /* Stop the AP functionality */
9380 /* TODO: Should do this in a way that does not indicated to possible
9381 * P2P Clients in the group that the group is terminated. */
9382 wpa_supplicant_ap_deinit(wpa_s);
9383
9384 /* Reselect the GO frequency */
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08009385 if (wpas_p2p_init_go_params(wpa_s, &params, 0, 0, 0, 0, 0, 0, 0,
9386 NULL)) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08009387 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Failed to reselect freq");
9388 wpas_p2p_group_delete(wpa_s,
9389 P2P_GROUP_REMOVAL_GO_LEAVE_CHANNEL);
9390 return;
9391 }
9392 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: New freq selected for the GO (%u MHz)",
9393 params.freq);
9394
9395 if (params.freq &&
9396 !p2p_supported_freq_go(wpa_s->global->p2p, params.freq)) {
9397 wpa_printf(MSG_DEBUG,
9398 "P2P: Selected freq (%u MHz) is not valid for P2P",
9399 params.freq);
9400 wpas_p2p_group_delete(wpa_s,
9401 P2P_GROUP_REMOVAL_GO_LEAVE_CHANNEL);
9402 return;
9403 }
9404
9405 /* Update the frequency */
9406 current_ssid->frequency = params.freq;
9407 wpa_s->connect_without_scan = current_ssid;
9408 wpa_s->reassociate = 1;
9409 wpa_s->disconnected = 0;
9410 wpa_supplicant_req_scan(wpa_s, 0, 0);
9411}
9412
9413
9414static void wpas_p2p_move_go(void *eloop_ctx, void *timeout_ctx)
9415{
9416 struct wpa_supplicant *wpa_s = eloop_ctx;
9417
9418 if (!wpa_s->ap_iface || !wpa_s->current_ssid)
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07009419 return;
9420
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08009421 wpas_p2p_go_update_common_freqs(wpa_s);
9422
9423 /* Do not move GO in the middle of a CSA */
9424 if (hostapd_csa_in_progress(wpa_s->ap_iface)) {
9425 wpa_printf(MSG_DEBUG,
9426 "P2P: CSA is in progress - not moving GO");
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07009427 return;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08009428 }
9429
9430 /*
9431 * First, try a channel switch flow. If it is not supported or fails,
9432 * take down the GO and bring it up again.
9433 */
9434 if (wpas_p2p_move_go_csa(wpa_s) < 0)
9435 wpas_p2p_move_go_no_csa(wpa_s);
9436}
9437
9438
9439static void wpas_p2p_reconsider_moving_go(void *eloop_ctx, void *timeout_ctx)
9440{
9441 struct wpa_supplicant *wpa_s = eloop_ctx;
9442 struct wpa_used_freq_data *freqs = NULL;
9443 unsigned int num = wpa_s->num_multichan_concurrent;
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07009444
9445 freqs = os_calloc(num, sizeof(struct wpa_used_freq_data));
9446 if (!freqs)
9447 return;
9448
9449 num = get_shared_radio_freqs_data(wpa_s, freqs, num);
9450
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08009451 /* Previous attempt to move a GO was not possible -- try again. */
9452 wpas_p2p_consider_moving_gos(wpa_s, freqs, num,
9453 WPAS_P2P_CHANNEL_UPDATE_ANY);
9454
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07009455 os_free(freqs);
9456}
9457
9458
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08009459/*
9460 * Consider moving a GO from its currently used frequency:
9461 * 1. It is possible that due to regulatory consideration the frequency
9462 * can no longer be used and there is a need to evacuate the GO.
9463 * 2. It is possible that due to MCC considerations, it would be preferable
9464 * to move the GO to a channel that is currently used by some other
9465 * station interface.
9466 *
9467 * In case a frequency that became invalid is once again valid, cancel a
9468 * previously initiated GO frequency change.
9469 */
9470static void wpas_p2p_consider_moving_one_go(struct wpa_supplicant *wpa_s,
9471 struct wpa_used_freq_data *freqs,
9472 unsigned int num)
9473{
9474 unsigned int i, invalid_freq = 0, policy_move = 0, flags = 0;
9475 unsigned int timeout;
9476 int freq;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07009477 int dfs_offload;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08009478
9479 wpas_p2p_go_update_common_freqs(wpa_s);
9480
9481 freq = wpa_s->current_ssid->frequency;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07009482 dfs_offload = (wpa_s->drv_flags & WPA_DRIVER_FLAGS_DFS_OFFLOAD) &&
Roshan Pius3a1667e2018-07-03 15:17:14 -07009483 ieee80211_is_dfs(freq, wpa_s->hw.modes, wpa_s->hw.num_modes);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08009484 for (i = 0, invalid_freq = 0; i < num; i++) {
9485 if (freqs[i].freq == freq) {
9486 flags = freqs[i].flags;
9487
9488 /* The channel is invalid, must change it */
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07009489 if (!p2p_supported_freq_go(wpa_s->global->p2p, freq) &&
9490 !dfs_offload) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08009491 wpa_dbg(wpa_s, MSG_DEBUG,
9492 "P2P: Freq=%d MHz no longer valid for GO",
9493 freq);
9494 invalid_freq = 1;
9495 }
9496 } else if (freqs[i].flags == 0) {
9497 /* Freq is not used by any other station interface */
9498 continue;
9499 } else if (!p2p_supported_freq(wpa_s->global->p2p,
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07009500 freqs[i].freq) && !dfs_offload) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08009501 /* Freq is not valid for P2P use cases */
9502 continue;
9503 } else if (wpa_s->conf->p2p_go_freq_change_policy ==
9504 P2P_GO_FREQ_MOVE_SCM) {
9505 policy_move = 1;
9506 } else if (wpa_s->conf->p2p_go_freq_change_policy ==
9507 P2P_GO_FREQ_MOVE_SCM_PEER_SUPPORTS &&
9508 wpas_p2p_go_is_peer_freq(wpa_s, freqs[i].freq)) {
9509 policy_move = 1;
9510 } else if ((wpa_s->conf->p2p_go_freq_change_policy ==
9511 P2P_GO_FREQ_MOVE_SCM_ECSA) &&
9512 wpas_p2p_go_is_peer_freq(wpa_s, freqs[i].freq)) {
9513 if (!p2p_get_group_num_members(wpa_s->p2p_group)) {
9514 policy_move = 1;
9515 } else if ((wpa_s->drv_flags &
9516 WPA_DRIVER_FLAGS_AP_CSA) &&
9517 wpas_p2p_go_clients_support_ecsa(wpa_s)) {
9518 u8 chan;
9519
9520 /*
9521 * We do not support CSA between bands, so move
9522 * GO only within the same band.
9523 */
9524 if (wpa_s->ap_iface->current_mode->mode ==
9525 ieee80211_freq_to_chan(freqs[i].freq,
9526 &chan))
9527 policy_move = 1;
9528 }
9529 }
9530 }
9531
9532 wpa_dbg(wpa_s, MSG_DEBUG,
9533 "P2P: GO move: invalid_freq=%u, policy_move=%u, flags=0x%X",
9534 invalid_freq, policy_move, flags);
9535
9536 /*
9537 * The channel is valid, or we are going to have a policy move, so
9538 * cancel timeout.
9539 */
9540 if (!invalid_freq || policy_move) {
9541 wpa_dbg(wpa_s, MSG_DEBUG,
9542 "P2P: Cancel a GO move from freq=%d MHz", freq);
9543 eloop_cancel_timeout(wpas_p2p_move_go, wpa_s, NULL);
9544
9545 if (wpas_p2p_in_progress(wpa_s)) {
9546 wpa_dbg(wpa_s, MSG_DEBUG,
9547 "P2P: GO move: policy CS is not allowed - setting timeout to re-consider GO move");
9548 eloop_cancel_timeout(wpas_p2p_reconsider_moving_go,
9549 wpa_s, NULL);
9550 eloop_register_timeout(P2P_RECONSIDER_GO_MOVE_DELAY, 0,
9551 wpas_p2p_reconsider_moving_go,
9552 wpa_s, NULL);
9553 return;
9554 }
9555 }
9556
9557 if (!invalid_freq && (!policy_move || flags != 0)) {
9558 wpa_dbg(wpa_s, MSG_DEBUG,
9559 "P2P: Not initiating a GO frequency change");
9560 return;
9561 }
9562
9563 /*
9564 * Do not consider moving GO if it is in the middle of a CSA. When the
9565 * CSA is finished this flow should be retriggered.
9566 */
9567 if (hostapd_csa_in_progress(wpa_s->ap_iface)) {
9568 wpa_dbg(wpa_s, MSG_DEBUG,
9569 "P2P: Not initiating a GO frequency change - CSA is in progress");
9570 return;
9571 }
9572
9573 if (invalid_freq && !wpas_p2p_disallowed_freq(wpa_s->global, freq))
9574 timeout = P2P_GO_FREQ_CHANGE_TIME;
9575 else
9576 timeout = 0;
9577
9578 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Move GO from freq=%d MHz in %d secs",
9579 freq, timeout);
9580 eloop_cancel_timeout(wpas_p2p_move_go, wpa_s, NULL);
9581 eloop_register_timeout(timeout, 0, wpas_p2p_move_go, wpa_s, NULL);
9582}
9583
9584
9585static void wpas_p2p_consider_moving_gos(struct wpa_supplicant *wpa_s,
9586 struct wpa_used_freq_data *freqs,
9587 unsigned int num,
9588 enum wpas_p2p_channel_update_trig trig)
9589{
9590 struct wpa_supplicant *ifs;
9591
9592 eloop_cancel_timeout(wpas_p2p_reconsider_moving_go, ELOOP_ALL_CTX,
9593 NULL);
9594
9595 /*
9596 * Travers all the radio interfaces, and for each GO interface, check
9597 * if there is a need to move the GO from the frequency it is using,
9598 * or in case the frequency is valid again, cancel the evacuation flow.
9599 */
9600 dl_list_for_each(ifs, &wpa_s->radio->ifaces, struct wpa_supplicant,
9601 radio_list) {
9602 if (ifs->current_ssid == NULL ||
9603 ifs->current_ssid->mode != WPAS_MODE_P2P_GO)
9604 continue;
9605
9606 /*
9607 * The GO was just started or completed channel switch, no need
9608 * to move it.
9609 */
9610 if (wpa_s == ifs &&
9611 (trig == WPAS_P2P_CHANNEL_UPDATE_STATE_CHANGE ||
9612 trig == WPAS_P2P_CHANNEL_UPDATE_CS)) {
9613 wpa_dbg(wpa_s, MSG_DEBUG,
9614 "P2P: GO move - schedule re-consideration");
9615 eloop_register_timeout(P2P_RECONSIDER_GO_MOVE_DELAY, 0,
9616 wpas_p2p_reconsider_moving_go,
9617 wpa_s, NULL);
9618 continue;
9619 }
9620
9621 wpas_p2p_consider_moving_one_go(ifs, freqs, num);
9622 }
9623}
9624
9625
9626void wpas_p2p_indicate_state_change(struct wpa_supplicant *wpa_s)
9627{
9628 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
9629 return;
9630
9631 wpas_p2p_update_channel_list(wpa_s,
9632 WPAS_P2P_CHANNEL_UPDATE_STATE_CHANGE);
9633}
9634
9635
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07009636void wpas_p2p_deinit_iface(struct wpa_supplicant *wpa_s)
9637{
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07009638 if (wpa_s == wpa_s->global->p2p_init_wpa_s && wpa_s->global->p2p) {
9639 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Disable P2P since removing "
9640 "the management interface is being removed");
9641 wpas_p2p_deinit_global(wpa_s->global);
9642 }
9643}
9644
9645
9646void wpas_p2p_ap_deinit(struct wpa_supplicant *wpa_s)
9647{
9648 if (wpa_s->ap_iface->bss)
9649 wpa_s->ap_iface->bss[0]->p2p_group = NULL;
9650 wpas_p2p_group_deinit(wpa_s);
9651}
Dmitry Shmidt58d12ad2016-07-28 10:07:03 -07009652
9653
9654int wpas_p2p_lo_start(struct wpa_supplicant *wpa_s, unsigned int freq,
9655 unsigned int period, unsigned int interval,
9656 unsigned int count)
9657{
9658 struct p2p_data *p2p = wpa_s->global->p2p;
9659 u8 *device_types;
9660 size_t dev_types_len;
9661 struct wpabuf *buf;
9662 int ret;
9663
9664 if (wpa_s->p2p_lo_started) {
9665 wpa_dbg(wpa_s, MSG_DEBUG,
9666 "P2P Listen offload is already started");
9667 return 0;
9668 }
9669
9670 if (wpa_s->global->p2p == NULL ||
9671 !(wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_LISTEN_OFFLOAD)) {
9672 wpa_printf(MSG_DEBUG, "P2P: Listen offload not supported");
9673 return -1;
9674 }
9675
9676 if (!p2p_supported_freq(wpa_s->global->p2p, freq)) {
9677 wpa_printf(MSG_ERROR, "P2P: Input channel not supported: %u",
9678 freq);
9679 return -1;
9680 }
9681
9682 /* Get device type */
9683 dev_types_len = (wpa_s->conf->num_sec_device_types + 1) *
9684 WPS_DEV_TYPE_LEN;
9685 device_types = os_malloc(dev_types_len);
9686 if (!device_types)
9687 return -1;
9688 os_memcpy(device_types, wpa_s->conf->device_type, WPS_DEV_TYPE_LEN);
9689 os_memcpy(&device_types[WPS_DEV_TYPE_LEN], wpa_s->conf->sec_device_type,
9690 wpa_s->conf->num_sec_device_types * WPS_DEV_TYPE_LEN);
9691
9692 /* Get Probe Response IE(s) */
9693 buf = p2p_build_probe_resp_template(p2p, freq);
9694 if (!buf) {
9695 os_free(device_types);
9696 return -1;
9697 }
9698
9699 ret = wpa_drv_p2p_lo_start(wpa_s, freq, period, interval, count,
9700 device_types, dev_types_len,
9701 wpabuf_mhead_u8(buf), wpabuf_len(buf));
9702 if (ret < 0)
9703 wpa_dbg(wpa_s, MSG_DEBUG,
9704 "P2P: Failed to start P2P listen offload");
9705
9706 os_free(device_types);
9707 wpabuf_free(buf);
9708
9709 if (ret == 0) {
9710 wpa_s->p2p_lo_started = 1;
9711
9712 /* Stop current P2P listen if any */
9713 wpas_stop_listen(wpa_s);
9714 }
9715
9716 return ret;
9717}
9718
9719
9720int wpas_p2p_lo_stop(struct wpa_supplicant *wpa_s)
9721{
9722 int ret;
9723
9724 if (!wpa_s->p2p_lo_started)
9725 return 0;
9726
9727 ret = wpa_drv_p2p_lo_stop(wpa_s);
9728 if (ret < 0)
9729 wpa_dbg(wpa_s, MSG_DEBUG,
9730 "P2P: Failed to stop P2P listen offload");
9731
9732 wpa_s->p2p_lo_started = 0;
9733 return ret;
9734}