]> git.ipfire.org Git - thirdparty/hostap.git/blob - wpa_supplicant/p2p_supplicant.c
P2P: Fix a possible buffer overflow in struct p2p_reg_class
[thirdparty/hostap.git] / wpa_supplicant / p2p_supplicant.c
1 /*
2 * wpa_supplicant - P2P
3 * Copyright (c) 2009-2010, Atheros Communications
4 * Copyright (c) 2010-2014, Jouni Malinen <j@w1.fi>
5 *
6 * This software may be distributed under the terms of the BSD license.
7 * See README for more details.
8 */
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"
20 #include "ap/ap_config.h"
21 #include "ap/sta_info.h"
22 #include "ap/ap_drv_ops.h"
23 #include "ap/wps_hostapd.h"
24 #include "ap/p2p_hostapd.h"
25 #include "ap/dfs.h"
26 #include "eapol_supp/eapol_supp_sm.h"
27 #include "rsn_supp/wpa.h"
28 #include "wpa_supplicant_i.h"
29 #include "driver_i.h"
30 #include "ap.h"
31 #include "config_ssid.h"
32 #include "config.h"
33 #include "notify.h"
34 #include "scan.h"
35 #include "bss.h"
36 #include "offchannel.h"
37 #include "wps_supplicant.h"
38 #include "p2p_supplicant.h"
39 #include "wifi_display.h"
40
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
48 #define P2P_AUTO_PD_SCAN_ATTEMPTS 5
49
50 /**
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
63 #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
71 #ifndef P2P_MAX_INITIAL_CONN_WAIT
72 /*
73 * How many seconds to wait for initial 4-way handshake to get completed after
74 * WPS provisioning step or after the re-invocation of a persistent group on a
75 * P2P Client.
76 */
77 #define P2P_MAX_INITIAL_CONN_WAIT 10
78 #endif /* P2P_MAX_INITIAL_CONN_WAIT */
79
80 #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
90 #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
99 #define P2P_MGMT_DEVICE_PREFIX "p2p-dev-"
100
101 /*
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
107 enum 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,
115 P2P_GROUP_REMOVAL_PSK_FAILURE,
116 P2P_GROUP_REMOVAL_FREQ_CONFLICT,
117 P2P_GROUP_REMOVAL_GO_LEAVE_CHANNEL
118 };
119
120
121 static void wpas_p2p_long_listen_timeout(void *eloop_ctx, void *timeout_ctx);
122 static struct wpa_supplicant *
123 wpas_p2p_get_group_iface(struct wpa_supplicant *wpa_s, int addr_allocated,
124 int go);
125 static int wpas_p2p_join_start(struct wpa_supplicant *wpa_s, int freq,
126 const u8 *ssid, size_t ssid_len);
127 static 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);
131 static void wpas_p2p_join_scan_req(struct wpa_supplicant *wpa_s, int freq,
132 const u8 *ssid, size_t ssid_len);
133 static void wpas_p2p_join_scan(void *eloop_ctx, void *timeout_ctx);
134 static int wpas_p2p_join(struct wpa_supplicant *wpa_s, const u8 *iface_addr,
135 const u8 *dev_addr, enum p2p_wps_method wps_method,
136 int auto_join, int freq,
137 const u8 *ssid, size_t ssid_len);
138 static int wpas_p2p_create_iface(struct wpa_supplicant *wpa_s);
139 static void wpas_p2p_cross_connect_setup(struct wpa_supplicant *wpa_s);
140 static void wpas_p2p_group_idle_timeout(void *eloop_ctx, void *timeout_ctx);
141 static void wpas_p2p_set_group_idle_timeout(struct wpa_supplicant *wpa_s);
142 static void wpas_p2p_group_formation_timeout(void *eloop_ctx,
143 void *timeout_ctx);
144 static void wpas_p2p_group_freq_conflict(void *eloop_ctx, void *timeout_ctx);
145 static int wpas_p2p_fallback_to_go_neg(struct wpa_supplicant *wpa_s,
146 int group_added);
147 static void wpas_p2p_stop_find_oper(struct wpa_supplicant *wpa_s);
148 static void wpas_stop_listen(void *ctx);
149 static void wpas_p2p_psk_failure_removal(void *eloop_ctx, void *timeout_ctx);
150 static void wpas_p2p_group_deinit(struct wpa_supplicant *wpa_s);
151 static int wpas_p2p_add_group_interface(struct wpa_supplicant *wpa_s,
152 enum wpa_driver_if_type type);
153 static void wpas_p2p_group_formation_failed(struct wpa_supplicant *wpa_s,
154 int already_deleted);
155 static void wpas_p2p_optimize_listen_channel(struct wpa_supplicant *wpa_s,
156 struct wpa_used_freq_data *freqs,
157 unsigned int num);
158 static void wpas_p2p_move_go(void *eloop_ctx, void *timeout_ctx);
159 static int wpas_p2p_go_is_peer_freq(struct wpa_supplicant *wpa_s, int freq);
160 static void
161 wpas_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);
164 static void wpas_p2p_reconsider_moving_go(void *eloop_ctx, void *timeout_ctx);
165
166
167 /*
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 */
171 static int wpas_p2p_num_unused_channels(struct wpa_supplicant *wpa_s)
172 {
173 int *freqs;
174 int num, unused;
175
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
184 unused = wpa_s->num_multichan_concurrent - num;
185 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: num_unused_channels: %d", unused);
186 return unused;
187 }
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 */
194 static unsigned int
195 wpas_p2p_valid_oper_freqs(struct wpa_supplicant *wpa_s,
196 struct wpa_used_freq_data *p2p_freqs,
197 unsigned int len)
198 {
199 struct wpa_used_freq_data *freqs;
200 unsigned int num, i, j;
201
202 freqs = os_calloc(wpa_s->num_multichan_concurrent,
203 sizeof(struct wpa_used_freq_data));
204 if (!freqs)
205 return 0;
206
207 num = get_shared_radio_freqs_data(wpa_s, freqs,
208 wpa_s->num_multichan_concurrent);
209
210 os_memset(p2p_freqs, 0, sizeof(struct wpa_used_freq_data) * len);
211
212 for (i = 0, j = 0; i < num && j < len; i++) {
213 if (p2p_supported_freq(wpa_s->global->p2p, freqs[i].freq))
214 p2p_freqs[j++] = freqs[i];
215 }
216
217 os_free(freqs);
218
219 dump_freq_data(wpa_s, "valid for P2P", p2p_freqs, j);
220
221 return j;
222 }
223
224
225 static 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;
230
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 &&
235 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);
239 freq = 0;
240 }
241 p2p_set_own_freq_preference(wpa_s->global->p2p, freq);
242 }
243
244
245 static void wpas_p2p_scan_res_handler(struct wpa_supplicant *wpa_s,
246 struct wpa_scan_results *scan_res)
247 {
248 size_t i;
249
250 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
256 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];
264 struct os_reltime time_tmp_age, entry_ts;
265 const u8 *ies;
266 size_t ies_len;
267
268 time_tmp_age.sec = bss->age / 1000;
269 time_tmp_age.usec = (bss->age % 1000) * 1000;
270 os_reltime_sub(&scan_res->fetch_time, &time_tmp_age, &entry_ts);
271
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
284 if (p2p_scan_res_handler(wpa_s->global->p2p, bss->bssid,
285 bss->freq, &entry_ts, bss->level,
286 ies, ies_len) > 0)
287 break;
288 }
289
290 p2p_scan_res_handled(wpa_s->global->p2p);
291 }
292
293
294 static 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) {
301 if (!work->started) {
302 wpa_scan_free_params(params);
303 return;
304 }
305
306 wpa_s->p2p_scan_work = NULL;
307 return;
308 }
309
310 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 }
315 ret = wpa_drv_scan(wpa_s, params);
316 if (ret == 0)
317 wpa_s->curr_scan_cookie = params->scan_cookie;
318 wpa_scan_free_params(params);
319 work->ctx = NULL;
320 if (ret) {
321 radio_work_done(work);
322 p2p_notify_scan_trigger_status(wpa_s->global->p2p, ret);
323 return;
324 }
325
326 p2p_notify_scan_trigger_status(wpa_s->global->p2p, ret);
327 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;
330 wpa_s->clear_driver_scan_cache = 0;
331 wpa_s->p2p_scan_work = work;
332 }
333
334
335 static 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
351 static int wpas_p2p_scan(void *ctx, enum p2p_scan_type type, int freq,
352 unsigned int num_req_dev_types,
353 const u8 *req_dev_types, const u8 *dev_id, u16 pw_id)
354 {
355 struct wpa_supplicant *wpa_s = ctx;
356 struct wpa_driver_scan_params *params = NULL;
357 struct wpabuf *wps_ie, *ies;
358 unsigned int num_channels = 0;
359 int social_channels_freq[] = { 2412, 2437, 2462, 60480 };
360 size_t ielen;
361 u8 *n, i;
362 unsigned int bands;
363
364 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
365 return -1;
366
367 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;
370 }
371
372 params = os_zalloc(sizeof(*params));
373 if (params == NULL)
374 return -1;
375
376 /* P2P Wildcard SSID */
377 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;
384
385 wpa_s->wps->dev.p2p = 1;
386 wps_ie = wps_build_probe_req_ie(pw_id, &wpa_s->wps->dev,
387 wpa_s->wps->uuid, WPS_REQ_ENROLLEE,
388 num_req_dev_types, req_dev_types);
389 if (wps_ie == NULL)
390 goto fail;
391
392 switch (type) {
393 case P2P_SCAN_SOCIAL:
394 params->freqs = os_calloc(ARRAY_SIZE(social_channels_freq) + 1,
395 sizeof(int));
396 if (params->freqs == NULL)
397 goto fail;
398 for (i = 0; i < ARRAY_SIZE(social_channels_freq); i++) {
399 if (wpas_p2p_search_social_channel(
400 wpa_s, social_channels_freq[i]))
401 params->freqs[num_channels++] =
402 social_channels_freq[i];
403 }
404 params->freqs[num_channels++] = 0;
405 break;
406 case P2P_SCAN_FULL:
407 break;
408 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;
415 case P2P_SCAN_SOCIAL_PLUS_ONE:
416 params->freqs = os_calloc(ARRAY_SIZE(social_channels_freq) + 2,
417 sizeof(int));
418 if (params->freqs == NULL)
419 goto fail;
420 for (i = 0; i < ARRAY_SIZE(social_channels_freq); i++) {
421 if (wpas_p2p_search_social_channel(
422 wpa_s, social_channels_freq[i]))
423 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;
429 break;
430 }
431
432 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
455 radio_remove_works(wpa_s, "p2p-scan", 0);
456 if (radio_add_work(wpa_s, 0, "p2p-scan", 0, wpas_p2p_trigger_scan_cb,
457 params) < 0)
458 goto fail;
459 return 0;
460
461 fail:
462 wpa_scan_free_params(params);
463 return -1;
464 }
465
466
467 static 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
482 static 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
507 static 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
517 static 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
538 /* Determine total number of clients in active groups where we are the GO */
539 static 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
562 static 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
571 static 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
578 /* Find an interface for a P2P group where we are the GO */
579 static struct wpa_supplicant *
580 wpas_p2p_get_go_group(struct wpa_supplicant *wpa_s)
581 {
582 struct wpa_supplicant *save = NULL;
583
584 if (!wpa_s)
585 return NULL;
586
587 for (wpa_s = wpa_s->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
588 if (!p2p_is_active_persistent_go(wpa_s))
589 continue;
590
591 /* 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;
595 }
596
597 /* No group with connected clients, so pick the one without (if any) */
598 return save;
599 }
600
601
602 static unsigned int p2p_is_active_persistent_cli(struct wpa_supplicant *wpa_s)
603 {
604 return p2p_is_active_persistent_group(wpa_s) &&
605 wpa_s->current_ssid->mode == WPAS_MODE_INFRA;
606 }
607
608
609 /* Find an interface for a P2P group where we are the P2P Client */
610 static struct wpa_supplicant *
611 wpas_p2p_get_cli_group(struct wpa_supplicant *wpa_s)
612 {
613 for (wpa_s = wpa_s->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
614 if (p2p_is_active_persistent_cli(wpa_s))
615 return wpa_s;
616 }
617
618 return NULL;
619 }
620
621
622 /* Find a persistent group where we are the GO */
623 static struct wpa_ssid *
624 wpas_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
637 static u8 p2ps_group_capability(void *ctx, u8 incoming, u8 role,
638 unsigned int *force_freq,
639 unsigned int *pref_freq)
640 {
641 struct wpa_supplicant *wpa_s = ctx;
642 struct wpa_ssid *s;
643 u8 conncap = P2PS_SETUP_NONE;
644 unsigned int owned_members = 0;
645 struct wpa_supplicant *go_wpa_s, *cli_wpa_s;
646 struct wpa_ssid *persistent_go;
647 int p2p_no_group_iface;
648 unsigned int pref_freq_list[P2P_MAX_PREF_CHANNELS], size;
649
650 wpa_printf(MSG_DEBUG, "P2P: Conncap - in:%d role:%d", incoming, role);
651
652 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
665 /*
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);
672 if (go_wpa_s)
673 owned_members = p2p_get_group_num_members(go_wpa_s->p2p_group);
674 persistent_go = wpas_p2p_get_persistent_go(wpa_s);
675 p2p_no_group_iface = !wpas_p2p_create_iface(wpa_s);
676 cli_wpa_s = wpas_p2p_get_cli_group(wpa_s);
677
678 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);
681
682 /* If not concurrent, restrict our choices */
683 if (p2p_no_group_iface) {
684 wpa_printf(MSG_DEBUG, "P2P: p2p_no_group_iface");
685
686 if (cli_wpa_s)
687 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) {
718 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
730 case P2PS_SETUP_NONE:
731 case P2PS_SETUP_NEW:
732 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 */
740 if (incoming == role || cli_wpa_s)
741 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 }
751 /* fall through */
752
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:
767 if (cli_wpa_s)
768 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:
782 if (!cli_wpa_s)
783 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:
788 if (cli_wpa_s)
789 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
805 grp_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;
810
811 s = wpas_p2p_get_persistent_go(wpa_s);
812 if (!s && !go_wpa_s && p2p_no_group_iface) {
813 p2p_set_intended_addr(wpa_s->global->p2p,
814 wpa_s->p2p_mgmt ?
815 wpa_s->parent->own_addr :
816 wpa_s->own_addr);
817 } else if (!s && !go_wpa_s) {
818 if (wpas_p2p_add_group_interface(wpa_s,
819 WPA_IF_P2P_GROUP) < 0) {
820 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
834 static int wpas_p2p_group_delete(struct wpa_supplicant *wpa_s,
835 enum p2p_group_removal_reason removal_reason)
836 {
837 struct wpa_ssid *ssid;
838 char *gtype;
839 const char *reason;
840
841 ssid = wpa_s->current_ssid;
842 if (ssid == NULL) {
843 /*
844 * The current SSID was not known, but there may still be a
845 * pending P2P group interface waiting for provisioning or a
846 * P2P group that is trying to reconnect.
847 */
848 ssid = wpa_s->conf->ssid;
849 while (ssid) {
850 if (ssid->p2p_group && ssid->disabled != 2)
851 break;
852 ssid = ssid->next;
853 }
854 if (ssid == NULL &&
855 wpa_s->p2p_group_interface == NOT_P2P_GROUP_INTERFACE)
856 {
857 wpa_printf(MSG_ERROR, "P2P: P2P group interface "
858 "not found");
859 return -1;
860 }
861 }
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;
868 gtype = "client";
869 } else
870 gtype = "GO";
871
872 if (removal_reason != P2P_GROUP_REMOVAL_SILENT && ssid)
873 wpas_notify_p2p_group_removed(wpa_s, ssid, gtype);
874
875 if (os_strcmp(gtype, "client") == 0) {
876 wpa_supplicant_deauthenticate(wpa_s, WLAN_REASON_DEAUTH_LEAVING);
877 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 }
886
887 if (wpa_s->cross_connect_in_use) {
888 wpa_s->cross_connect_in_use = 0;
889 wpa_msg_global(wpa_s->p2pdev, MSG_INFO,
890 P2P_EVENT_CROSS_CONNECT_DISABLE "%s %s",
891 wpa_s->ifname, wpa_s->cross_connect_uplink);
892 }
893 switch (removal_reason) {
894 case P2P_GROUP_REMOVAL_REQUESTED:
895 reason = " reason=REQUESTED";
896 break;
897 case P2P_GROUP_REMOVAL_FORMATION_FAILED:
898 reason = " reason=FORMATION_FAILED";
899 break;
900 case P2P_GROUP_REMOVAL_IDLE_TIMEOUT:
901 reason = " reason=IDLE";
902 break;
903 case P2P_GROUP_REMOVAL_UNAVAILABLE:
904 reason = " reason=UNAVAILABLE";
905 break;
906 case P2P_GROUP_REMOVAL_GO_ENDING_SESSION:
907 reason = " reason=GO_ENDING_SESSION";
908 break;
909 case P2P_GROUP_REMOVAL_PSK_FAILURE:
910 reason = " reason=PSK_FAILURE";
911 break;
912 case P2P_GROUP_REMOVAL_FREQ_CONFLICT:
913 reason = " reason=FREQ_CONFLICT";
914 break;
915 default:
916 reason = "";
917 break;
918 }
919 if (removal_reason != P2P_GROUP_REMOVAL_SILENT) {
920 wpa_msg_global(wpa_s->p2pdev, MSG_INFO,
921 P2P_EVENT_GROUP_REMOVED "%s %s%s",
922 wpa_s->ifname, gtype, reason);
923 }
924
925 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");
927 if (eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL) > 0)
928 wpa_printf(MSG_DEBUG, "P2P: Cancelled P2P group idle timeout");
929 if (eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
930 wpa_s->p2pdev, NULL) > 0) {
931 wpa_printf(MSG_DEBUG, "P2P: Cancelled P2P group formation "
932 "timeout");
933 wpa_s->p2p_in_provisioning = 0;
934 wpas_p2p_group_formation_failed(wpa_s, 1);
935 }
936
937 wpa_s->p2p_in_invitation = 0;
938 eloop_cancel_timeout(wpas_p2p_move_go, wpa_s, NULL);
939 eloop_cancel_timeout(wpas_p2p_reconsider_moving_go, wpa_s, NULL);
940
941 /*
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
947 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);
956 eloop_cancel_timeout(run_wpas_p2p_disconnect, wpa_s, NULL);
957 wpa_supplicant_remove_iface(wpa_s->global, wpa_s, 0);
958 wpa_s = global->ifaces;
959 if (wpa_s && ifname)
960 wpa_drv_if_remove(wpa_s, type, ifname);
961 os_free(ifname);
962 return 1;
963 }
964
965 /*
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
971 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
976 wpa_s->show_group_started = 0;
977 os_free(wpa_s->go_params);
978 wpa_s->go_params = NULL;
979
980 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;
983 wpa_s->p2p_go_do_acs = 0;
984
985 wpa_s->waiting_presence_resp = 0;
986
987 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;
992 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);
995 wpa_s->current_ssid = NULL;
996 }
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 */
1006 wpa_config_remove_network(wpa_s->conf, id);
1007 wpa_supplicant_clear_status(wpa_s);
1008 wpa_supplicant_cancel_sched_scan(wpa_s);
1009 } else {
1010 wpa_printf(MSG_DEBUG, "P2P: Temporary group network not "
1011 "found");
1012 }
1013 if (wpa_s->ap_iface)
1014 wpa_supplicant_ap_deinit(wpa_s);
1015 else
1016 wpa_drv_deinit_p2p_cli(wpa_s);
1017
1018 os_memset(wpa_s->go_dev_addr, 0, ETH_ALEN);
1019
1020 return 0;
1021 }
1022
1023
1024 static 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);
1040 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);
1044 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);
1058 if (p2p == NULL)
1059 p2p = wpa_bss_get_vendor_ie_multi_beacon(bss,
1060 P2P_IE_VENDOR_TYPE);
1061 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
1089 return !!(group_capab & P2P_GROUP_CAPAB_PERSISTENT_GROUP);
1090 }
1091
1092
1093 static int wpas_p2p_store_persistent_group(struct wpa_supplicant *wpa_s,
1094 struct wpa_ssid *ssid,
1095 const u8 *go_dev_addr)
1096 {
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)
1124 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);
1135 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;
1147 s->pbss = ssid->pbss;
1148 s->pairwise_cipher = ssid->pbss ? WPA_CIPHER_GCMP : WPA_CIPHER_CCMP;
1149 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 }
1168 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 }
1173
1174 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 }
1178
1179 return s->id;
1180 }
1181
1182
1183 static 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;
1189 int found = 0;
1190 struct wpa_supplicant *p2p_wpa_s = wpa_s->global->p2p_init_wpa_s;
1191
1192 ssid = wpa_s->current_ssid;
1193 if (ssid == NULL || ssid->mode != WPAS_MODE_P2P_GO ||
1194 !ssid->p2p_persistent_group)
1195 return;
1196
1197 for (s = p2p_wpa_s->conf->ssid; s; s = s->next) {
1198 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++) {
1210 if (os_memcmp(s->p2p_client_list + i * 2 * ETH_ALEN, addr,
1211 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 */
1218 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);
1221 os_memcpy(s->p2p_client_list +
1222 (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);
1227 found = 1;
1228 break;
1229 }
1230
1231 if (!found && s->num_p2p_clients < P2P_MAX_STORED_CLIENTS) {
1232 n = os_realloc_array(s->p2p_client_list,
1233 s->num_p2p_clients + 1, 2 * ETH_ALEN);
1234 if (n == NULL)
1235 return;
1236 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);
1240 s->p2p_client_list = n;
1241 s->num_p2p_clients++;
1242 } else if (!found && s->p2p_client_list) {
1243 /* Not enough room for an additional entry - drop the oldest
1244 * entry */
1245 os_memmove(s->p2p_client_list,
1246 s->p2p_client_list + 2 * ETH_ALEN,
1247 (s->num_p2p_clients - 1) * 2 * ETH_ALEN);
1248 os_memcpy(s->p2p_client_list +
1249 (s->num_p2p_clients - 1) * 2 * ETH_ALEN,
1250 addr, ETH_ALEN);
1251 os_memset(s->p2p_client_list +
1252 (s->num_p2p_clients - 1) * 2 * ETH_ALEN + ETH_ALEN,
1253 0xff, ETH_ALEN);
1254 }
1255
1256 if (p2p_wpa_s->conf->update_config &&
1257 wpa_config_write(p2p_wpa_s->confname, p2p_wpa_s->conf))
1258 wpa_printf(MSG_DEBUG, "P2P: Failed to update configuration");
1259 }
1260
1261
1262 static 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 */
1288 wpa_msg_global_ctrl(wpa_s->p2pdev, MSG_INFO,
1289 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
1307 static void wpas_group_formation_completed(struct wpa_supplicant *wpa_s,
1308 int success, int already_deleted)
1309 {
1310 struct wpa_ssid *ssid;
1311 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;
1322 if (wpa_s->p2p_go_group_formation_completed) {
1323 wpa_s->global->p2p_group_formation = NULL;
1324 wpa_s->p2p_in_provisioning = 0;
1325 } 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;
1329 }
1330 wpa_s->p2p_in_invitation = 0;
1331 wpa_s->group_formation_reported = 1;
1332
1333 if (!success) {
1334 wpa_msg_global(wpa_s->p2pdev, MSG_INFO,
1335 P2P_EVENT_GROUP_FORMATION_FAILURE);
1336 wpas_notify_p2p_group_formation_failure(wpa_s, "");
1337 if (already_deleted)
1338 return;
1339 wpas_p2p_group_delete(wpa_s,
1340 P2P_GROUP_REMOVAL_FORMATION_FAILED);
1341 return;
1342 }
1343
1344 wpa_msg_global(wpa_s->p2pdev, MSG_INFO,
1345 P2P_EVENT_GROUP_FORMATION_SUCCESS);
1346
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) {
1356 client = ssid->mode == WPAS_MODE_INFRA;
1357 if (ssid->mode == WPAS_MODE_P2P_GO) {
1358 persistent = ssid->p2p_persistent_group;
1359 os_memcpy(go_dev_addr, wpa_s->global->p2p_dev_addr,
1360 ETH_ALEN);
1361 } else
1362 persistent = wpas_p2p_persistent_group(wpa_s,
1363 go_dev_addr,
1364 ssid->ssid,
1365 ssid->ssid_len);
1366 } else {
1367 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;
1380 } else {
1381 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, "");
1387 wpas_p2p_cross_connect_setup(wpa_s);
1388 wpas_p2p_set_group_idle_timeout(wpa_s);
1389 }
1390
1391 if (persistent)
1392 wpas_p2p_store_persistent_group(wpa_s->p2pdev,
1393 ssid, go_dev_addr);
1394 else {
1395 os_free(wpa_s->global->add_psk);
1396 wpa_s->global->add_psk = NULL;
1397 }
1398
1399 if (!client) {
1400 wpas_notify_p2p_group_started(wpa_s, ssid, persistent, 0, NULL);
1401 os_get_reltime(&wpa_s->global->p2p_go_wait_client);
1402 }
1403 }
1404
1405
1406 struct 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
1417 static 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
1436 static 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");
1445 wpas_p2p_free_send_action_work(wpa_s);
1446 }
1447
1448
1449 static void wpas_p2p_action_tx_clear(struct wpa_supplicant *wpa_s)
1450 {
1451 if (wpa_s->p2p_send_action_work) {
1452 struct send_action_work *awork;
1453
1454 awork = wpa_s->p2p_send_action_work->ctx;
1455 wpa_printf(MSG_DEBUG,
1456 "P2P: Clear Action TX work @%p (wait_time=%u)",
1457 wpa_s->p2p_send_action_work, awork->wait_time);
1458 if (awork->wait_time == 0) {
1459 wpas_p2p_free_send_action_work(wpa_s);
1460 } 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 }
1475 }
1476
1477
1478 static 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);
1489
1490 if (wpa_s->global->p2p == NULL || wpa_s->global->p2p_disabled)
1491 return;
1492
1493 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;
1503 }
1504
1505 p2p_send_action_cb(wpa_s->global->p2p, freq, dst, src, bssid, res);
1506
1507 if (result != OFFCHANNEL_SEND_ACTION_SUCCESS &&
1508 wpa_s->pending_pd_before_join &&
1509 (os_memcmp(dst, wpa_s->pending_join_dev_addr, ETH_ALEN) == 0 ||
1510 os_memcmp(dst, wpa_s->pending_join_iface_addr, ETH_ALEN) == 0) &&
1511 wpa_s->p2p_fallback_to_go_neg) {
1512 wpa_s->pending_pd_before_join = 0;
1513 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: No ACK for PD Req "
1514 "during p2p_connect-auto");
1515 wpa_msg_global(wpa_s->p2pdev, MSG_INFO,
1516 P2P_EVENT_FALLBACK_TO_GO_NEG
1517 "reason=no-ACK-to-PD-Req");
1518 wpas_p2p_fallback_to_go_neg(wpa_s, 0);
1519 return;
1520 }
1521 }
1522
1523
1524 static 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) {
1530 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 }
1536 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
1552 static 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
1559 if (radio_work_pending(wpa_s, "p2p-send-action")) {
1560 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
1576 if (radio_add_work(wpa_s, freq, "p2p-send-action", 1,
1577 wpas_send_action_cb, awork) < 0) {
1578 os_free(awork);
1579 return -1;
1580 }
1581
1582 return 0;
1583 }
1584
1585
1586 static int wpas_send_action(void *ctx, unsigned int freq, const u8 *dst,
1587 const u8 *src, const u8 *bssid, const u8 *buf,
1588 size_t len, unsigned int wait_time, int *scheduled)
1589 {
1590 struct wpa_supplicant *wpa_s = ctx;
1591 int listen_freq = -1, send_freq = -1;
1592
1593 if (scheduled)
1594 *scheduled = 0;
1595 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) {
1600 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;
1609 }
1610
1611 wpa_printf(MSG_DEBUG, "P2P: Use ongoing radio work for Action frame TX");
1612 return offchannel_send_action(wpa_s, freq, dst, src, bssid, buf, len,
1613 wait_time,
1614 wpas_p2p_send_action_tx_status, 1);
1615 }
1616
1617
1618 static void wpas_send_action_done(void *ctx)
1619 {
1620 struct wpa_supplicant *wpa_s = ctx;
1621
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
1630 offchannel_send_action_done(wpa_s);
1631 }
1632
1633
1634 static 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
1647 static void wpas_start_wps_enrollee(struct wpa_supplicant *wpa_s,
1648 struct p2p_go_neg_results *res)
1649 {
1650 wpa_s->group_formation_reported = 0;
1651 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);
1655 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);
1659 if (res->wps_method == WPS_PBC) {
1660 wpas_wps_start_pbc(wpa_s, res->peer_interface_addr, 1, 0);
1661 #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,
1665 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 ==
1668 DEV_PW_NFC_CONNECTION_HANDOVER ?
1669 wpa_s->p2pdev->p2p_peer_oob_pubkey_hash :
1670 NULL,
1671 NULL, 0, 0);
1672 #endif /* CONFIG_WPS_NFC */
1673 } else {
1674 u16 dev_pw_id = DEV_PW_DEFAULT;
1675 if (wpa_s->p2p_wps_method == WPS_P2PS)
1676 dev_pw_id = DEV_PW_P2PS_DEFAULT;
1677 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
1685 static 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
1695 persistent = wpas_p2p_get_persistent(wpa_s->p2pdev, NULL, ssid->ssid,
1696 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
1723 static void p2p_go_dump_common_freqs(struct wpa_supplicant *wpa_s)
1724 {
1725 char buf[20 + P2P_MAX_CHANNELS * 6];
1726 char *pos, *end;
1727 unsigned int i;
1728 int res;
1729
1730 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';
1740
1741 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Common group frequencies:%s", buf);
1742 }
1743
1744
1745 static 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
1766 static void p2p_config_write(struct wpa_supplicant *wpa_s)
1767 {
1768 #ifndef CONFIG_NO_CONFIG_WRITE
1769 if (wpa_s->p2pdev->conf->update_config &&
1770 wpa_config_write(wpa_s->p2pdev->confname, wpa_s->p2pdev->conf))
1771 wpa_printf(MSG_DEBUG, "P2P: Failed to update configuration");
1772 #endif /* CONFIG_NO_CONFIG_WRITE */
1773 }
1774
1775
1776 static 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
1782 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
1791 p2p_go_save_group_common_freqs(wpa_s, params);
1792 p2p_go_dump_common_freqs(wpa_s);
1793
1794 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;
1799 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, "");
1805 wpa_s->group_formation_reported = 1;
1806
1807 if (wpa_s->p2pdev->p2ps_method_config_any) {
1808 if (is_zero_ether_addr(wpa_s->p2pdev->p2ps_join_addr)) {
1809 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,
1817 MAC2STR(wpa_s->p2pdev->p2ps_join_addr));
1818 wpa_supplicant_ap_wps_pin(
1819 wpa_s, wpa_s->p2pdev->p2ps_join_addr,
1820 "12345670", NULL, 0, 0);
1821 }
1822 wpa_s->p2pdev->p2ps_method_config_any = 0;
1823 }
1824
1825 os_get_reltime(&wpa_s->global->p2p_go_wait_client);
1826 if (params->persistent_group) {
1827 wpas_p2p_store_persistent_group(
1828 wpa_s->p2pdev, ssid,
1829 wpa_s->global->p2p_dev_addr);
1830 wpas_p2p_add_psk_list(wpa_s, ssid);
1831 }
1832
1833 wpas_notify_p2p_group_started(wpa_s, ssid,
1834 params->persistent_group, 0,
1835 NULL);
1836 wpas_p2p_cross_connect_setup(wpa_s);
1837 wpas_p2p_set_group_idle_timeout(wpa_s);
1838
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,
1846 wpa_s->p2pdev, NULL);
1847 eloop_register_timeout(
1848 wpa_s->p2p_first_connection_timeout, 0,
1849 wpas_p2p_group_formation_timeout,
1850 wpa_s->p2pdev, NULL);
1851 }
1852
1853 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 }
1863 if (params->wps_method == WPS_PBC) {
1864 wpa_supplicant_ap_wps_pbc(wpa_s, params->peer_interface_addr,
1865 params->peer_device_addr);
1866 #ifdef CONFIG_WPS_NFC
1867 } else if (params->wps_method == WPS_NFC) {
1868 if (wpa_s->p2pdev->p2p_oob_dev_pw_id !=
1869 DEV_PW_NFC_CONNECTION_HANDOVER &&
1870 !wpa_s->p2pdev->p2p_oob_dev_pw) {
1871 wpa_printf(MSG_DEBUG, "P2P: No NFC Dev Pw known");
1872 return;
1873 }
1874 wpas_ap_wps_add_nfc_pw(
1875 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);
1879 #endif /* CONFIG_WPS_NFC */
1880 } else if (wpa_s->p2p_pin[0])
1881 wpa_supplicant_ap_wps_pin(wpa_s, params->peer_interface_addr,
1882 wpa_s->p2p_pin, NULL, 0, 0);
1883 os_free(wpa_s->go_params);
1884 wpa_s->go_params = NULL;
1885 }
1886
1887
1888 /**
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 */
1898 static 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,
1915 HOSTAPD_MODE_IEEE80211AD, 0);
1916 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
1943 int 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
1965 static 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
1971 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");
1975 return;
1976 }
1977
1978 ssid = wpa_config_add_network(wpa_s->conf);
1979 if (ssid == NULL) {
1980 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Could not add network for GO");
1981 return;
1982 }
1983
1984 wpa_s->show_group_started = 0;
1985 wpa_s->p2p_go_group_formation_completed = 0;
1986 wpa_s->group_formation_reported = 0;
1987 os_memset(wpa_s->go_dev_addr, 0, ETH_ALEN);
1988
1989 wpa_config_set_network_defaults(ssid);
1990 ssid->temporary = 1;
1991 ssid->p2p_group = 1;
1992 ssid->p2p_persistent_group = !!params->persistent_group;
1993 ssid->mode = group_formation ? WPAS_MODE_P2P_GROUP_FORMATION :
1994 WPAS_MODE_P2P_GO;
1995 ssid->frequency = params->freq;
1996 ssid->ht40 = params->ht40;
1997 ssid->vht = params->vht;
1998 ssid->max_oper_chwidth = params->max_oper_chwidth;
1999 ssid->vht_center_freq2 = params->vht_center_freq2;
2000 ssid->he = params->he;
2001 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
2015 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;
2024 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;
2032 /* P2P GO in 60 GHz is always a PCP (PBSS) */
2033 ssid->pbss = 1;
2034 }
2035 if (os_strlen(params->passphrase) > 0) {
2036 ssid->passphrase = os_strdup(params->passphrase);
2037 if (ssid->passphrase == NULL) {
2038 wpa_msg_global(wpa_s, MSG_ERROR,
2039 "P2P: Failed to copy passphrase for GO");
2040 wpa_config_remove_network(wpa_s->conf, ssid->id);
2041 return;
2042 }
2043 } else
2044 ssid->passphrase = NULL;
2045 ssid->psk_set = params->psk_set;
2046 if (ssid->psk_set)
2047 os_memcpy(ssid->psk, params->psk, sizeof(ssid->psk));
2048 else if (ssid->passphrase)
2049 wpa_config_update_psk(ssid);
2050 ssid->ap_max_inactivity = wpa_s->p2pdev->conf->p2p_go_max_inactivity;
2051
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;
2055 wpa_s->scan_req = NORMAL_SCAN_REQ;
2056 wpa_s->connect_without_scan = ssid;
2057 wpa_s->reassociate = 1;
2058 wpa_s->disconnected = 0;
2059 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Request scan (that will be skipped) to "
2060 "start GO)");
2061 wpa_supplicant_req_scan(wpa_s, 0, 0);
2062 }
2063
2064
2065 static 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
2074 #define C(n) \
2075 do { \
2076 if (s->n && !d->n) \
2077 d->n = os_strdup(s->n); \
2078 } while (0)
2079
2080 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;
2094 d->p2p_go_freq_change_policy = s->p2p_go_freq_change_policy;
2095 d->p2p_intra_bss = s->p2p_intra_bss;
2096 d->persistent_reconnect = s->persistent_reconnect;
2097 d->max_num_sta = s->max_num_sta;
2098 d->pbc_in_m1 = s->pbc_in_m1;
2099 d->ignore_old_scan_res = s->ignore_old_scan_res;
2100 d->beacon_int = s->beacon_int;
2101 d->dtim_period = s->dtim_period;
2102 d->p2p_go_ctwindow = s->p2p_go_ctwindow;
2103 d->disassoc_low_ack = s->disassoc_low_ack;
2104 d->disable_scan_offload = s->disable_scan_offload;
2105 d->passive_scan = s->passive_scan;
2106
2107 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);
2111 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 }
2114 d->p2p_cli_probe = s->p2p_cli_probe;
2115 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;
2120 }
2121
2122
2123 static 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) {
2136 int res;
2137
2138 /* Try to avoid going over the IFNAMSIZ length limit */
2139 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';
2142 }
2143 }
2144
2145
2146 static 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
2163 wpas_p2p_get_group_ifname(wpa_s, ifname, sizeof(ifname));
2164 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
2178 if (wpa_s->conf->p2p_interface_random_mac_addr) {
2179 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));
2183 }
2184
2185 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
2201 static 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';
2214 wpa_s->global->pending_group_iface_for_p2ps = 0;
2215 }
2216
2217
2218 static struct wpa_supplicant *
2219 wpas_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;
2241 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;
2248 iface.driver_param = wpa_s->conf->driver_param;
2249 group_wpa_s = wpa_supplicant_add_iface(wpa_s->global, &iface, wpa_s);
2250 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';
2256 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;
2259 wpa_s->global->pending_group_iface_for_p2ps = 0;
2260
2261 wpas_p2p_clone_config(group_wpa_s, wpa_s);
2262
2263 if (wpa_s->conf->p2p_interface_random_mac_addr) {
2264 if (wpa_drv_set_mac_addr(group_wpa_s,
2265 wpa_s->pending_interface_addr) < 0) {
2266 wpa_msg(group_wpa_s, MSG_INFO,
2267 "Failed to set random MAC address");
2268 wpa_supplicant_remove_iface(wpa_s->global, group_wpa_s,
2269 0);
2270 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");
2276 wpa_supplicant_remove_iface(wpa_s->global, group_wpa_s,
2277 0);
2278 return NULL;
2279 }
2280
2281 wpa_printf(MSG_DEBUG, "P2P: Using random MAC address " MACSTR
2282 " for the group",
2283 MAC2STR(wpa_s->pending_interface_addr));
2284 }
2285
2286 return group_wpa_s;
2287 }
2288
2289
2290 static 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");
2295 wpas_p2p_group_formation_failed(wpa_s, 0);
2296 }
2297
2298
2299 static void wpas_p2p_group_formation_failed(struct wpa_supplicant *wpa_s,
2300 int already_deleted)
2301 {
2302 eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
2303 wpa_s->p2pdev, NULL);
2304 if (wpa_s->global->p2p)
2305 p2p_group_formation_failed(wpa_s->global->p2p);
2306 wpas_group_formation_completed(wpa_s, 0, already_deleted);
2307 }
2308
2309
2310 static 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,
2314 wpa_s->p2pdev, NULL);
2315 eloop_register_timeout(0, 0, wpas_p2p_group_formation_timeout,
2316 wpa_s->p2pdev, NULL);
2317 wpa_s->global->p2p_fail_on_wps_complete = 0;
2318 }
2319
2320
2321 void 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,
2327 wpa_s->p2pdev, NULL);
2328 eloop_register_timeout(0, 0, wpas_p2p_group_formation_timeout,
2329 wpa_s->p2pdev, NULL);
2330 }
2331
2332
2333 static void wpas_go_neg_completed(void *ctx, struct p2p_go_neg_results *res)
2334 {
2335 struct wpa_supplicant *wpa_s = ctx;
2336 struct wpa_supplicant *group_wpa_s;
2337
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) {
2345 wpa_msg_global(wpa_s, MSG_INFO,
2346 P2P_EVENT_GO_NEG_FAILURE "status=%d",
2347 res->status);
2348 wpas_notify_p2p_go_neg_completed(wpa_s, res);
2349 wpas_p2p_remove_pending_group_interface(wpa_s);
2350 return;
2351 }
2352
2353 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
2358 if (wpa_s->p2p_go_ht40)
2359 res->ht40 = 1;
2360 if (wpa_s->p2p_go_vht)
2361 res->vht = 1;
2362 if (wpa_s->p2p_go_he)
2363 res->he = 1;
2364 if (wpa_s->p2p_go_edmg)
2365 res->edmg = 1;
2366 res->max_oper_chwidth = wpa_s->p2p_go_max_oper_chwidth;
2367 res->vht_center_freq2 = wpa_s->p2p_go_vht_center_freq2;
2368
2369 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));
2376 wpas_notify_p2p_go_neg_completed(wpa_s, res);
2377
2378 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
2392 if (wpa_s->create_p2p_iface) {
2393 group_wpa_s =
2394 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);
2397 eloop_cancel_timeout(wpas_p2p_long_listen_timeout,
2398 wpa_s, NULL);
2399 wpas_p2p_group_formation_failed(wpa_s, 1);
2400 return;
2401 }
2402 os_memset(wpa_s->pending_interface_addr, 0, ETH_ALEN);
2403 wpa_s->pending_interface_name[0] = '\0';
2404 } else {
2405 group_wpa_s = wpa_s->parent;
2406 wpa_s->global->p2p_group_formation = group_wpa_s;
2407 if (group_wpa_s != wpa_s)
2408 wpas_p2p_clone_config(group_wpa_s, wpa_s);
2409 }
2410
2411 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);
2423 }
2424
2425 wpa_s->global->p2p_long_listen = 0;
2426 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
2435 static void wpas_go_neg_req_rx(void *ctx, const u8 *src, u16 dev_passwd_id,
2436 u8 go_intent)
2437 {
2438 struct wpa_supplicant *wpa_s = ctx;
2439 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_GO_NEG_REQUEST MACSTR
2440 " dev_passwd_id=%u go_intent=%u", MAC2STR(src),
2441 dev_passwd_id, go_intent);
2442
2443 wpas_notify_p2p_go_neg_req(wpa_s, src, dev_passwd_id, go_intent);
2444 }
2445
2446
2447 static void wpas_dev_found(void *ctx, const u8 *addr,
2448 const struct p2p_peer_info *info,
2449 int new_device)
2450 {
2451 #ifndef CONFIG_NO_STDOUT_DEBUG
2452 struct wpa_supplicant *wpa_s = ctx;
2453 char devtype[WPS_DEV_TYPE_BUFSIZE];
2454 char *wfd_dev_info_hex = NULL;
2455
2456 #ifdef CONFIG_WIFI_DISPLAY
2457 wfd_dev_info_hex = wifi_display_subelem_hex(info->wfd_subelems,
2458 WFD_SUBELEM_DEVICE_INFO);
2459 #endif /* CONFIG_WIFI_DISPLAY */
2460
2461 if (info->p2ps_instance) {
2462 char str[256];
2463 const u8 *buf = wpabuf_head(info->p2ps_instance);
2464 size_t len = wpabuf_len(info->p2ps_instance);
2465
2466 while (len) {
2467 u32 id;
2468 u16 methods;
2469 u8 str_len;
2470
2471 if (len < 4 + 2 + 1)
2472 break;
2473 id = WPA_GET_LE32(buf);
2474 buf += sizeof(u32);
2475 methods = WPA_GET_BE16(buf);
2476 buf += sizeof(u16);
2477 str_len = *buf++;
2478 if (str_len > len - 4 - 2 - 1)
2479 break;
2480 os_memcpy(str, buf, str_len);
2481 str[str_len] = '\0';
2482 buf += str_len;
2483 len -= str_len + sizeof(u32) + sizeof(u16) + sizeof(u8);
2484
2485 wpa_msg_global(wpa_s, MSG_INFO,
2486 P2P_EVENT_DEVICE_FOUND MACSTR
2487 " p2p_dev_addr=" MACSTR
2488 " pri_dev_type=%s name='%s'"
2489 " config_methods=0x%x"
2490 " dev_capab=0x%x"
2491 " group_capab=0x%x"
2492 " adv_id=%x asp_svc=%s%s",
2493 MAC2STR(addr),
2494 MAC2STR(info->p2p_device_addr),
2495 wps_dev_type_bin2str(
2496 info->pri_dev_type,
2497 devtype, sizeof(devtype)),
2498 info->device_name, methods,
2499 info->dev_capab, info->group_capab,
2500 id, str,
2501 info->vendor_elems ?
2502 " vendor_elems=1" : "");
2503 }
2504 goto done;
2505 }
2506
2507 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_DEVICE_FOUND MACSTR
2508 " p2p_dev_addr=" MACSTR
2509 " pri_dev_type=%s name='%s' config_methods=0x%x "
2510 "dev_capab=0x%x group_capab=0x%x%s%s%s new=%d",
2511 MAC2STR(addr), MAC2STR(info->p2p_device_addr),
2512 wps_dev_type_bin2str(info->pri_dev_type, devtype,
2513 sizeof(devtype)),
2514 info->device_name, info->config_methods,
2515 info->dev_capab, info->group_capab,
2516 wfd_dev_info_hex ? " wfd_dev_info=0x" : "",
2517 wfd_dev_info_hex ? wfd_dev_info_hex : "",
2518 info->vendor_elems ? " vendor_elems=1" : "",
2519 new_device);
2520
2521 done:
2522 os_free(wfd_dev_info_hex);
2523 #endif /* CONFIG_NO_STDOUT_DEBUG */
2524
2525 wpas_notify_p2p_device_found(ctx, info->p2p_device_addr, new_device);
2526 }
2527
2528
2529 static void wpas_dev_lost(void *ctx, const u8 *dev_addr)
2530 {
2531 struct wpa_supplicant *wpa_s = ctx;
2532
2533 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_DEVICE_LOST
2534 "p2p_dev_addr=" MACSTR, MAC2STR(dev_addr));
2535
2536 wpas_notify_p2p_device_lost(wpa_s, dev_addr);
2537 }
2538
2539
2540 static void wpas_find_stopped(void *ctx)
2541 {
2542 struct wpa_supplicant *wpa_s = ctx;
2543
2544 if (wpa_s->p2p_scan_work && wpas_abort_ongoing_scan(wpa_s) < 0)
2545 wpa_printf(MSG_DEBUG, "P2P: Abort ongoing scan failed");
2546
2547 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_FIND_STOPPED);
2548 wpas_notify_p2p_find_stopped(wpa_s);
2549 }
2550
2551
2552 struct wpas_p2p_listen_work {
2553 unsigned int freq;
2554 unsigned int duration;
2555 struct wpabuf *probe_resp_ie;
2556 };
2557
2558
2559 static void wpas_p2p_listen_work_free(struct wpas_p2p_listen_work *lwork)
2560 {
2561 if (lwork == NULL)
2562 return;
2563 wpabuf_free(lwork->probe_resp_ie);
2564 os_free(lwork);
2565 }
2566
2567
2568 static void wpas_p2p_listen_work_done(struct wpa_supplicant *wpa_s)
2569 {
2570 struct wpas_p2p_listen_work *lwork;
2571
2572 if (!wpa_s->p2p_listen_work)
2573 return;
2574
2575 lwork = wpa_s->p2p_listen_work->ctx;
2576 wpas_p2p_listen_work_free(lwork);
2577 radio_work_done(wpa_s->p2p_listen_work);
2578 wpa_s->p2p_listen_work = NULL;
2579 }
2580
2581
2582 static void wpas_start_listen_cb(struct wpa_radio_work *work, int deinit)
2583 {
2584 struct wpa_supplicant *wpa_s = work->wpa_s;
2585 struct wpas_p2p_listen_work *lwork = work->ctx;
2586 unsigned int duration;
2587
2588 if (deinit) {
2589 if (work->started) {
2590 wpa_s->p2p_listen_work = NULL;
2591 wpas_stop_listen(wpa_s);
2592 }
2593 wpas_p2p_listen_work_free(lwork);
2594 return;
2595 }
2596
2597 wpa_s->p2p_listen_work = work;
2598
2599 wpa_drv_set_ap_wps_ie(wpa_s, NULL, lwork->probe_resp_ie, NULL);
2600
2601 if (wpa_drv_probe_req_report(wpa_s, 1) < 0) {
2602 wpa_printf(MSG_DEBUG, "P2P: Failed to request the driver to "
2603 "report received Probe Request frames");
2604 wpas_p2p_listen_work_done(wpa_s);
2605 return;
2606 }
2607
2608 wpa_s->pending_listen_freq = lwork->freq;
2609 wpa_s->pending_listen_duration = lwork->duration;
2610
2611 duration = lwork->duration;
2612 #ifdef CONFIG_TESTING_OPTIONS
2613 if (wpa_s->extra_roc_dur) {
2614 wpa_printf(MSG_DEBUG, "TESTING: Increase ROC duration %u -> %u",
2615 duration, duration + wpa_s->extra_roc_dur);
2616 duration += wpa_s->extra_roc_dur;
2617 }
2618 #endif /* CONFIG_TESTING_OPTIONS */
2619
2620 if (wpa_drv_remain_on_channel(wpa_s, lwork->freq, duration) < 0) {
2621 wpa_printf(MSG_DEBUG, "P2P: Failed to request the driver "
2622 "to remain on channel (%u MHz) for Listen "
2623 "state", lwork->freq);
2624 wpas_p2p_listen_work_done(wpa_s);
2625 wpa_s->pending_listen_freq = 0;
2626 return;
2627 }
2628 wpa_s->off_channel_freq = 0;
2629 wpa_s->roc_waiting_drv_freq = lwork->freq;
2630 }
2631
2632
2633 static int wpas_start_listen(void *ctx, unsigned int freq,
2634 unsigned int duration,
2635 const struct wpabuf *probe_resp_ie)
2636 {
2637 struct wpa_supplicant *wpa_s = ctx;
2638 struct wpas_p2p_listen_work *lwork;
2639
2640 if (wpa_s->p2p_listen_work) {
2641 wpa_printf(MSG_DEBUG, "P2P: Reject start_listen since p2p_listen_work already exists");
2642 return -1;
2643 }
2644
2645 lwork = os_zalloc(sizeof(*lwork));
2646 if (lwork == NULL)
2647 return -1;
2648 lwork->freq = freq;
2649 lwork->duration = duration;
2650 if (probe_resp_ie) {
2651 lwork->probe_resp_ie = wpabuf_dup(probe_resp_ie);
2652 if (lwork->probe_resp_ie == NULL) {
2653 wpas_p2p_listen_work_free(lwork);
2654 return -1;
2655 }
2656 }
2657
2658 if (radio_add_work(wpa_s, freq, "p2p-listen", 0, wpas_start_listen_cb,
2659 lwork) < 0) {
2660 wpas_p2p_listen_work_free(lwork);
2661 return -1;
2662 }
2663
2664 return 0;
2665 }
2666
2667
2668 static void wpas_stop_listen(void *ctx)
2669 {
2670 struct wpa_supplicant *wpa_s = ctx;
2671 if (wpa_s->off_channel_freq || wpa_s->roc_waiting_drv_freq) {
2672 wpa_drv_cancel_remain_on_channel(wpa_s);
2673 wpa_s->off_channel_freq = 0;
2674 wpa_s->roc_waiting_drv_freq = 0;
2675 }
2676 wpa_drv_set_ap_wps_ie(wpa_s, NULL, NULL, NULL);
2677
2678 /*
2679 * Don't cancel Probe Request RX reporting for a connected P2P Client
2680 * handling Probe Request frames.
2681 */
2682 if (!wpa_s->p2p_cli_probe)
2683 wpa_drv_probe_req_report(wpa_s, 0);
2684
2685 wpas_p2p_listen_work_done(wpa_s);
2686 }
2687
2688
2689 static int wpas_send_probe_resp(void *ctx, const struct wpabuf *buf,
2690 unsigned int freq)
2691 {
2692 struct wpa_supplicant *wpa_s = ctx;
2693 return wpa_drv_send_mlme(wpa_s, wpabuf_head(buf), wpabuf_len(buf), 1,
2694 freq);
2695 }
2696
2697
2698 static void wpas_prov_disc_local_display(struct wpa_supplicant *wpa_s,
2699 const u8 *peer, const char *params,
2700 unsigned int generated_pin)
2701 {
2702 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_SHOW_PIN MACSTR
2703 " %08d%s", MAC2STR(peer), generated_pin, params);
2704 }
2705
2706
2707 static void wpas_prov_disc_local_keypad(struct wpa_supplicant *wpa_s,
2708 const u8 *peer, const char *params)
2709 {
2710 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_ENTER_PIN MACSTR
2711 "%s", MAC2STR(peer), params);
2712 }
2713
2714
2715 static void wpas_prov_disc_req(void *ctx, const u8 *peer, u16 config_methods,
2716 const u8 *dev_addr, const u8 *pri_dev_type,
2717 const char *dev_name, u16 supp_config_methods,
2718 u8 dev_capab, u8 group_capab, const u8 *group_id,
2719 size_t group_id_len)
2720 {
2721 struct wpa_supplicant *wpa_s = ctx;
2722 char devtype[WPS_DEV_TYPE_BUFSIZE];
2723 char params[300];
2724 u8 empty_dev_type[8];
2725 unsigned int generated_pin = 0;
2726 struct wpa_supplicant *group = NULL;
2727 int res;
2728
2729 if (group_id) {
2730 for (group = wpa_s->global->ifaces; group; group = group->next)
2731 {
2732 struct wpa_ssid *s = group->current_ssid;
2733 if (s != NULL &&
2734 s->mode == WPAS_MODE_P2P_GO &&
2735 group_id_len - ETH_ALEN == s->ssid_len &&
2736 os_memcmp(group_id + ETH_ALEN, s->ssid,
2737 s->ssid_len) == 0)
2738 break;
2739 }
2740 }
2741
2742 if (pri_dev_type == NULL) {
2743 os_memset(empty_dev_type, 0, sizeof(empty_dev_type));
2744 pri_dev_type = empty_dev_type;
2745 }
2746 res = os_snprintf(params, sizeof(params), " p2p_dev_addr=" MACSTR
2747 " pri_dev_type=%s name='%s' config_methods=0x%x "
2748 "dev_capab=0x%x group_capab=0x%x%s%s",
2749 MAC2STR(dev_addr),
2750 wps_dev_type_bin2str(pri_dev_type, devtype,
2751 sizeof(devtype)),
2752 dev_name, supp_config_methods, dev_capab, group_capab,
2753 group ? " group=" : "",
2754 group ? group->ifname : "");
2755 if (os_snprintf_error(sizeof(params), res))
2756 wpa_printf(MSG_DEBUG, "P2P: PD Request event truncated");
2757 params[sizeof(params) - 1] = '\0';
2758
2759 if (config_methods & WPS_CONFIG_DISPLAY) {
2760 if (wps_generate_pin(&generated_pin) < 0) {
2761 wpa_printf(MSG_DEBUG, "P2P: Could not generate PIN");
2762 wpas_notify_p2p_provision_discovery(
2763 wpa_s, peer, 0 /* response */,
2764 P2P_PROV_DISC_INFO_UNAVAILABLE, 0, 0);
2765 return;
2766 }
2767 wpas_prov_disc_local_display(wpa_s, peer, params,
2768 generated_pin);
2769 } else if (config_methods & WPS_CONFIG_KEYPAD)
2770 wpas_prov_disc_local_keypad(wpa_s, peer, params);
2771 else if (config_methods & WPS_CONFIG_PUSHBUTTON)
2772 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_PBC_REQ
2773 MACSTR "%s", MAC2STR(peer), params);
2774
2775 wpas_notify_p2p_provision_discovery(wpa_s, peer, 1 /* request */,
2776 P2P_PROV_DISC_SUCCESS,
2777 config_methods, generated_pin);
2778 }
2779
2780
2781 static void wpas_prov_disc_resp(void *ctx, const u8 *peer, u16 config_methods)
2782 {
2783 struct wpa_supplicant *wpa_s = ctx;
2784 unsigned int generated_pin = 0;
2785 char params[20];
2786
2787 if (wpa_s->pending_pd_before_join &&
2788 (os_memcmp(peer, wpa_s->pending_join_dev_addr, ETH_ALEN) == 0 ||
2789 os_memcmp(peer, wpa_s->pending_join_iface_addr, ETH_ALEN) == 0)) {
2790 wpa_s->pending_pd_before_join = 0;
2791 wpa_printf(MSG_DEBUG, "P2P: Starting pending "
2792 "join-existing-group operation");
2793 wpas_p2p_join_start(wpa_s, 0, NULL, 0);
2794 return;
2795 }
2796
2797 if (wpa_s->pending_pd_use == AUTO_PD_JOIN ||
2798 wpa_s->pending_pd_use == AUTO_PD_GO_NEG) {
2799 int res;
2800
2801 res = os_snprintf(params, sizeof(params), " peer_go=%d",
2802 wpa_s->pending_pd_use == AUTO_PD_JOIN);
2803 if (os_snprintf_error(sizeof(params), res))
2804 params[sizeof(params) - 1] = '\0';
2805 } else
2806 params[0] = '\0';
2807
2808 if (config_methods & WPS_CONFIG_DISPLAY)
2809 wpas_prov_disc_local_keypad(wpa_s, peer, params);
2810 else if (config_methods & WPS_CONFIG_KEYPAD) {
2811 if (wps_generate_pin(&generated_pin) < 0) {
2812 wpa_printf(MSG_DEBUG, "P2P: Could not generate PIN");
2813 wpas_notify_p2p_provision_discovery(
2814 wpa_s, peer, 0 /* response */,
2815 P2P_PROV_DISC_INFO_UNAVAILABLE, 0, 0);
2816 return;
2817 }
2818 wpas_prov_disc_local_display(wpa_s, peer, params,
2819 generated_pin);
2820 } else if (config_methods & WPS_CONFIG_PUSHBUTTON)
2821 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_PBC_RESP
2822 MACSTR "%s", MAC2STR(peer), params);
2823
2824 wpas_notify_p2p_provision_discovery(wpa_s, peer, 0 /* response */,
2825 P2P_PROV_DISC_SUCCESS,
2826 config_methods, generated_pin);
2827 }
2828
2829
2830 static void wpas_prov_disc_fail(void *ctx, const u8 *peer,
2831 enum p2p_prov_disc_status status,
2832 u32 adv_id, const u8 *adv_mac,
2833 const char *deferred_session_resp)
2834 {
2835 struct wpa_supplicant *wpa_s = ctx;
2836
2837 if (wpa_s->p2p_fallback_to_go_neg) {
2838 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: PD for p2p_connect-auto "
2839 "failed - fall back to GO Negotiation");
2840 wpa_msg_global(wpa_s->p2pdev, MSG_INFO,
2841 P2P_EVENT_FALLBACK_TO_GO_NEG
2842 "reason=PD-failed");
2843 wpas_p2p_fallback_to_go_neg(wpa_s, 0);
2844 return;
2845 }
2846
2847 if (status == P2P_PROV_DISC_TIMEOUT_JOIN) {
2848 wpa_s->pending_pd_before_join = 0;
2849 wpa_printf(MSG_DEBUG, "P2P: Starting pending "
2850 "join-existing-group operation (no ACK for PD "
2851 "Req attempts)");
2852 wpas_p2p_join_start(wpa_s, 0, NULL, 0);
2853 return;
2854 }
2855
2856 if (adv_id && adv_mac && deferred_session_resp) {
2857 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_FAILURE
2858 " p2p_dev_addr=" MACSTR " status=%d adv_id=%x"
2859 " deferred_session_resp='%s'",
2860 MAC2STR(peer), status, adv_id,
2861 deferred_session_resp);
2862 } else if (adv_id && adv_mac) {
2863 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_FAILURE
2864 " p2p_dev_addr=" MACSTR " status=%d adv_id=%x",
2865 MAC2STR(peer), status, adv_id);
2866 } else {
2867 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_FAILURE
2868 " p2p_dev_addr=" MACSTR " status=%d",
2869 MAC2STR(peer), status);
2870 }
2871
2872 wpas_notify_p2p_provision_discovery(wpa_s, peer, 0 /* response */,
2873 status, 0, 0);
2874 }
2875
2876
2877 static int freq_included(struct wpa_supplicant *wpa_s,
2878 const struct p2p_channels *channels,
2879 unsigned int freq)
2880 {
2881 if ((channels == NULL || p2p_channels_includes_freq(channels, freq)) &&
2882 wpas_p2p_go_is_peer_freq(wpa_s, freq))
2883 return 1;
2884 return 0;
2885 }
2886
2887
2888 static void wpas_p2p_go_update_common_freqs(struct wpa_supplicant *wpa_s)
2889 {
2890 unsigned int num = P2P_MAX_CHANNELS;
2891 int *common_freqs;
2892 int ret;
2893
2894 p2p_go_dump_common_freqs(wpa_s);
2895 common_freqs = os_calloc(num, sizeof(int));
2896 if (!common_freqs)
2897 return;
2898
2899 ret = p2p_group_get_common_freqs(wpa_s->p2p_group, common_freqs, &num);
2900 if (ret < 0) {
2901 wpa_dbg(wpa_s, MSG_DEBUG,
2902 "P2P: Failed to get group common freqs");
2903 os_free(common_freqs);
2904 return;
2905 }
2906
2907 os_free(wpa_s->p2p_group_common_freqs);
2908 wpa_s->p2p_group_common_freqs = common_freqs;
2909 wpa_s->p2p_group_common_freqs_num = num;
2910 p2p_go_dump_common_freqs(wpa_s);
2911 }
2912
2913
2914 /*
2915 * Check if the given frequency is one of the possible operating frequencies
2916 * set after the completion of the GO Negotiation.
2917 */
2918 static int wpas_p2p_go_is_peer_freq(struct wpa_supplicant *wpa_s, int freq)
2919 {
2920 unsigned int i;
2921
2922 p2p_go_dump_common_freqs(wpa_s);
2923
2924 /* assume no restrictions */
2925 if (!wpa_s->p2p_group_common_freqs_num)
2926 return 1;
2927
2928 for (i = 0; i < wpa_s->p2p_group_common_freqs_num; i++) {
2929 if (wpa_s->p2p_group_common_freqs[i] == freq)
2930 return 1;
2931 }
2932 return 0;
2933 }
2934
2935
2936 static int wpas_sta_check_ecsa(struct hostapd_data *hapd,
2937 struct sta_info *sta, void *ctx)
2938 {
2939 int *ecsa_support = ctx;
2940
2941 *ecsa_support &= sta->ecsa_supported;
2942
2943 return 0;
2944 }
2945
2946
2947 /* Check if all the peers support eCSA */
2948 static int wpas_p2p_go_clients_support_ecsa(struct wpa_supplicant *wpa_s)
2949 {
2950 int ecsa_support = 1;
2951
2952 ap_for_each_sta(wpa_s->ap_iface->bss[0], wpas_sta_check_ecsa,
2953 &ecsa_support);
2954
2955 return ecsa_support;
2956 }
2957
2958
2959 /**
2960 * Pick the best frequency to use from all the currently used frequencies.
2961 */
2962 static int wpas_p2p_pick_best_used_freq(struct wpa_supplicant *wpa_s,
2963 struct wpa_used_freq_data *freqs,
2964 unsigned int num)
2965 {
2966 unsigned int i, c;
2967
2968 /* find a candidate freq that is supported by P2P */
2969 for (c = 0; c < num; c++)
2970 if (p2p_supported_freq(wpa_s->global->p2p, freqs[c].freq))
2971 break;
2972
2973 if (c == num)
2974 return 0;
2975
2976 /* once we have a candidate, try to find a 'better' one */
2977 for (i = c + 1; i < num; i++) {
2978 if (!p2p_supported_freq(wpa_s->global->p2p, freqs[i].freq))
2979 continue;
2980
2981 /*
2982 * 1. Infrastructure station interfaces have higher preference.
2983 * 2. P2P Clients have higher preference.
2984 * 3. All others.
2985 */
2986 if (freqs[i].flags & WPA_FREQ_USED_BY_INFRA_STATION) {
2987 c = i;
2988 break;
2989 }
2990
2991 if ((freqs[i].flags & WPA_FREQ_USED_BY_P2P_CLIENT))
2992 c = i;
2993 }
2994 return freqs[c].freq;
2995 }
2996
2997
2998 static u8 wpas_invitation_process(void *ctx, const u8 *sa, const u8 *bssid,
2999 const u8 *go_dev_addr, const u8 *ssid,
3000 size_t ssid_len, int *go, u8 *group_bssid,
3001 int *force_freq, int persistent_group,
3002 const struct p2p_channels *channels,
3003 int dev_pw_id)
3004 {
3005 struct wpa_supplicant *wpa_s = ctx;
3006 struct wpa_ssid *s;
3007 struct wpa_used_freq_data *freqs;
3008 struct wpa_supplicant *grp;
3009 int best_freq;
3010
3011 if (!persistent_group) {
3012 wpa_printf(MSG_DEBUG, "P2P: Invitation from " MACSTR
3013 " to join an active group (SSID: %s)",
3014 MAC2STR(sa), wpa_ssid_txt(ssid, ssid_len));
3015 if (!is_zero_ether_addr(wpa_s->p2p_auth_invite) &&
3016 (os_memcmp(go_dev_addr, wpa_s->p2p_auth_invite, ETH_ALEN)
3017 == 0 ||
3018 os_memcmp(sa, wpa_s->p2p_auth_invite, ETH_ALEN) == 0)) {
3019 wpa_printf(MSG_DEBUG, "P2P: Accept previously "
3020 "authorized invitation");
3021 goto accept_inv;
3022 }
3023
3024 #ifdef CONFIG_WPS_NFC
3025 if (dev_pw_id >= 0 && wpa_s->p2p_nfc_tag_enabled &&
3026 dev_pw_id == wpa_s->p2p_oob_dev_pw_id) {
3027 wpa_printf(MSG_DEBUG, "P2P: Accept invitation based on local enabled NFC Tag");
3028 wpa_s->p2p_wps_method = WPS_NFC;
3029 wpa_s->pending_join_wps_method = WPS_NFC;
3030 os_memcpy(wpa_s->pending_join_dev_addr,
3031 go_dev_addr, ETH_ALEN);
3032 os_memcpy(wpa_s->pending_join_iface_addr,
3033 bssid, ETH_ALEN);
3034 goto accept_inv;
3035 }
3036 #endif /* CONFIG_WPS_NFC */
3037
3038 /*
3039 * Do not accept the invitation automatically; notify user and
3040 * request approval.
3041 */
3042 return P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE;
3043 }
3044
3045 grp = wpas_get_p2p_group(wpa_s, ssid, ssid_len, go);
3046 if (grp) {
3047 wpa_printf(MSG_DEBUG, "P2P: Accept invitation to already "
3048 "running persistent group");
3049 if (*go)
3050 os_memcpy(group_bssid, grp->own_addr, ETH_ALEN);
3051 goto accept_inv;
3052 }
3053
3054 if (!is_zero_ether_addr(wpa_s->p2p_auth_invite) &&
3055 os_memcmp(sa, wpa_s->p2p_auth_invite, ETH_ALEN) == 0) {
3056 wpa_printf(MSG_DEBUG, "P2P: Accept previously initiated "
3057 "invitation to re-invoke a persistent group");
3058 os_memset(wpa_s->p2p_auth_invite, 0, ETH_ALEN);
3059 } else if (!wpa_s->conf->persistent_reconnect)
3060 return P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE;
3061
3062 for (s = wpa_s->conf->ssid; s; s = s->next) {
3063 if (s->disabled == 2 &&
3064 os_memcmp(s->bssid, go_dev_addr, ETH_ALEN) == 0 &&
3065 s->ssid_len == ssid_len &&
3066 os_memcmp(ssid, s->ssid, ssid_len) == 0)
3067 break;
3068 }
3069
3070 if (!s) {
3071 wpa_printf(MSG_DEBUG, "P2P: Invitation from " MACSTR
3072 " requested reinvocation of an unknown group",
3073 MAC2STR(sa));
3074 return P2P_SC_FAIL_UNKNOWN_GROUP;
3075 }
3076
3077 if (s->mode == WPAS_MODE_P2P_GO && !wpas_p2p_create_iface(wpa_s)) {
3078 *go = 1;
3079 if (wpa_s->wpa_state >= WPA_AUTHENTICATING) {
3080 wpa_printf(MSG_DEBUG, "P2P: The only available "
3081 "interface is already in use - reject "
3082 "invitation");
3083 return P2P_SC_FAIL_UNABLE_TO_ACCOMMODATE;
3084 }
3085 if (wpa_s->p2p_mgmt)
3086 os_memcpy(group_bssid, wpa_s->parent->own_addr,
3087 ETH_ALEN);
3088 else
3089 os_memcpy(group_bssid, wpa_s->own_addr, ETH_ALEN);
3090 } else if (s->mode == WPAS_MODE_P2P_GO) {
3091 *go = 1;
3092 if (wpas_p2p_add_group_interface(wpa_s, WPA_IF_P2P_GO) < 0)
3093 {
3094 wpa_printf(MSG_ERROR, "P2P: Failed to allocate a new "
3095 "interface address for the group");
3096 return P2P_SC_FAIL_UNABLE_TO_ACCOMMODATE;
3097 }
3098 os_memcpy(group_bssid, wpa_s->pending_interface_addr,
3099 ETH_ALEN);
3100 }
3101
3102 accept_inv:
3103 wpas_p2p_set_own_freq_preference(wpa_s, 0);
3104
3105 best_freq = 0;
3106 freqs = os_calloc(wpa_s->num_multichan_concurrent,
3107 sizeof(struct wpa_used_freq_data));
3108 if (freqs) {
3109 int num_channels = wpa_s->num_multichan_concurrent;
3110 int num = wpas_p2p_valid_oper_freqs(wpa_s, freqs, num_channels);
3111 best_freq = wpas_p2p_pick_best_used_freq(wpa_s, freqs, num);
3112 os_free(freqs);
3113 }
3114
3115 /* Get one of the frequencies currently in use */
3116 if (best_freq > 0) {
3117 wpa_printf(MSG_DEBUG, "P2P: Trying to prefer a channel already used by one of the interfaces");
3118 wpas_p2p_set_own_freq_preference(wpa_s, best_freq);
3119
3120 if (wpa_s->num_multichan_concurrent < 2 ||
3121 wpas_p2p_num_unused_channels(wpa_s) < 1) {
3122 wpa_printf(MSG_DEBUG, "P2P: No extra channels available - trying to force channel to match a channel already used by one of the interfaces");
3123 *force_freq = best_freq;
3124 }
3125 }
3126
3127 if (*force_freq > 0 && wpa_s->num_multichan_concurrent > 1 &&
3128 wpas_p2p_num_unused_channels(wpa_s) > 0) {
3129 if (*go == 0) {
3130 /* We are the client */
3131 wpa_printf(MSG_DEBUG, "P2P: Peer was found to be "
3132 "running a GO but we are capable of MCC, "
3133 "figure out the best channel to use");
3134 *force_freq = 0;
3135 } else if (!freq_included(wpa_s, channels, *force_freq)) {
3136 /* We are the GO, and *force_freq is not in the
3137 * intersection */
3138 wpa_printf(MSG_DEBUG, "P2P: Forced GO freq %d MHz not "
3139 "in intersection but we are capable of MCC, "
3140 "figure out the best channel to use",
3141 *force_freq);
3142 *force_freq = 0;
3143 }
3144 }
3145
3146 return P2P_SC_SUCCESS;
3147 }
3148
3149
3150 static void wpas_invitation_received(void *ctx, const u8 *sa, const u8 *bssid,
3151 const u8 *ssid, size_t ssid_len,
3152 const u8 *go_dev_addr, u8 status,
3153 int op_freq)
3154 {
3155 struct wpa_supplicant *wpa_s = ctx;
3156 struct wpa_ssid *s;
3157
3158 for (s = wpa_s->conf->ssid; s; s = s->next) {
3159 if (s->disabled == 2 &&
3160 s->ssid_len == ssid_len &&
3161 os_memcmp(ssid, s->ssid, ssid_len) == 0)
3162 break;
3163 }
3164
3165 if (status == P2P_SC_SUCCESS) {
3166 wpa_printf(MSG_DEBUG, "P2P: Invitation from peer " MACSTR
3167 " was accepted; op_freq=%d MHz, SSID=%s",
3168 MAC2STR(sa), op_freq, wpa_ssid_txt(ssid, ssid_len));
3169 if (s) {
3170 int go = s->mode == WPAS_MODE_P2P_GO;
3171 if (go) {
3172 wpa_msg_global(wpa_s, MSG_INFO,
3173 P2P_EVENT_INVITATION_ACCEPTED
3174 "sa=" MACSTR
3175 " persistent=%d freq=%d",
3176 MAC2STR(sa), s->id, op_freq);
3177 } else {
3178 wpa_msg_global(wpa_s, MSG_INFO,
3179 P2P_EVENT_INVITATION_ACCEPTED
3180 "sa=" MACSTR
3181 " persistent=%d",
3182 MAC2STR(sa), s->id);
3183 }
3184 wpas_p2p_group_add_persistent(
3185 wpa_s, s, go, 0, op_freq, 0, 0, 0, 0, 0,
3186 0, NULL,
3187 go ? P2P_MAX_INITIAL_CONN_WAIT_GO_REINVOKE : 0,
3188 1);
3189 } else if (bssid) {
3190 wpa_s->user_initiated_pd = 0;
3191 wpa_msg_global(wpa_s, MSG_INFO,
3192 P2P_EVENT_INVITATION_ACCEPTED
3193 "sa=" MACSTR " go_dev_addr=" MACSTR
3194 " bssid=" MACSTR " unknown-network",
3195 MAC2STR(sa), MAC2STR(go_dev_addr),
3196 MAC2STR(bssid));
3197 wpas_p2p_join(wpa_s, bssid, go_dev_addr,
3198 wpa_s->p2p_wps_method, 0, op_freq,
3199 ssid, ssid_len);
3200 }
3201 return;
3202 }
3203
3204 if (status != P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE) {
3205 wpa_printf(MSG_DEBUG, "P2P: Invitation from peer " MACSTR
3206 " was rejected (status %u)", MAC2STR(sa), status);
3207 return;
3208 }
3209
3210 if (!s) {
3211 if (bssid) {
3212 wpa_msg_global(wpa_s, MSG_INFO,
3213 P2P_EVENT_INVITATION_RECEIVED
3214 "sa=" MACSTR " go_dev_addr=" MACSTR
3215 " bssid=" MACSTR " unknown-network",
3216 MAC2STR(sa), MAC2STR(go_dev_addr),
3217 MAC2STR(bssid));
3218 } else {
3219 wpa_msg_global(wpa_s, MSG_INFO,
3220 P2P_EVENT_INVITATION_RECEIVED
3221 "sa=" MACSTR " go_dev_addr=" MACSTR
3222 " unknown-network",
3223 MAC2STR(sa), MAC2STR(go_dev_addr));
3224 }
3225 wpas_notify_p2p_invitation_received(wpa_s, sa, go_dev_addr,
3226 bssid, 0, op_freq);
3227 return;
3228 }
3229
3230 if (s->mode == WPAS_MODE_P2P_GO && op_freq) {
3231 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_INVITATION_RECEIVED
3232 "sa=" MACSTR " persistent=%d freq=%d",
3233 MAC2STR(sa), s->id, op_freq);
3234 } else {
3235 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_INVITATION_RECEIVED
3236 "sa=" MACSTR " persistent=%d",
3237 MAC2STR(sa), s->id);
3238 }
3239 wpas_notify_p2p_invitation_received(wpa_s, sa, go_dev_addr, bssid,
3240 s->id, op_freq);
3241 }
3242
3243
3244 static void wpas_remove_persistent_peer(struct wpa_supplicant *wpa_s,
3245 struct wpa_ssid *ssid,
3246 const u8 *peer, int inv)
3247 {
3248 size_t i;
3249 struct wpa_supplicant *p2p_wpa_s = wpa_s->global->p2p_init_wpa_s;
3250
3251 if (ssid == NULL)
3252 return;
3253
3254 for (i = 0; ssid->p2p_client_list && i < ssid->num_p2p_clients; i++) {
3255 if (os_memcmp(ssid->p2p_client_list + i * 2 * ETH_ALEN, peer,
3256 ETH_ALEN) == 0)
3257 break;
3258 }
3259 if (i >= ssid->num_p2p_clients || !ssid->p2p_client_list) {
3260 if (ssid->mode != WPAS_MODE_P2P_GO &&
3261 os_memcmp(ssid->bssid, peer, ETH_ALEN) == 0) {
3262 wpa_printf(MSG_DEBUG, "P2P: Remove persistent group %d "
3263 "due to invitation result", ssid->id);
3264 wpas_notify_network_removed(wpa_s, ssid);
3265 wpa_config_remove_network(wpa_s->conf, ssid->id);
3266 return;
3267 }
3268 return; /* Peer not found in client list */
3269 }
3270
3271 wpa_printf(MSG_DEBUG, "P2P: Remove peer " MACSTR " from persistent "
3272 "group %d client list%s",
3273 MAC2STR(peer), ssid->id,
3274 inv ? " due to invitation result" : "");
3275 os_memmove(ssid->p2p_client_list + i * 2 * ETH_ALEN,
3276 ssid->p2p_client_list + (i + 1) * 2 * ETH_ALEN,
3277 (ssid->num_p2p_clients - i - 1) * 2 * ETH_ALEN);
3278 ssid->num_p2p_clients--;
3279 if (p2p_wpa_s->conf->update_config &&
3280 wpa_config_write(p2p_wpa_s->confname, p2p_wpa_s->conf))
3281 wpa_printf(MSG_DEBUG, "P2P: Failed to update configuration");
3282 }
3283
3284
3285 static void wpas_remove_persistent_client(struct wpa_supplicant *wpa_s,
3286 const u8 *peer)
3287 {
3288 struct wpa_ssid *ssid;
3289
3290 wpa_s = wpa_s->global->p2p_invite_group;
3291 if (wpa_s == NULL)
3292 return; /* No known invitation group */
3293 ssid = wpa_s->current_ssid;
3294 if (ssid == NULL || ssid->mode != WPAS_MODE_P2P_GO ||
3295 !ssid->p2p_persistent_group)
3296 return; /* Not operating as a GO in persistent group */
3297 ssid = wpas_p2p_get_persistent(wpa_s->p2pdev, peer,
3298 ssid->ssid, ssid->ssid_len);
3299 wpas_remove_persistent_peer(wpa_s, ssid, peer, 1);
3300 }
3301
3302
3303 static void wpas_invitation_result(void *ctx, int status, const u8 *bssid,
3304 const struct p2p_channels *channels,
3305 const u8 *peer, int neg_freq,
3306 int peer_oper_freq)
3307 {
3308 struct wpa_supplicant *wpa_s = ctx;
3309 struct wpa_ssid *ssid;
3310 int freq;
3311
3312 if (bssid) {
3313 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_INVITATION_RESULT
3314 "status=%d " MACSTR,
3315 status, MAC2STR(bssid));
3316 } else {
3317 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_INVITATION_RESULT
3318 "status=%d ", status);
3319 }
3320 wpas_notify_p2p_invitation_result(wpa_s, status, bssid);
3321
3322 wpa_printf(MSG_DEBUG, "P2P: Invitation result - status=%d peer=" MACSTR,
3323 status, MAC2STR(peer));
3324 if (wpa_s->pending_invite_ssid_id == -1) {
3325 struct wpa_supplicant *group_if =
3326 wpa_s->global->p2p_invite_group;
3327
3328 if (status == P2P_SC_FAIL_UNKNOWN_GROUP)
3329 wpas_remove_persistent_client(wpa_s, peer);
3330
3331 /*
3332 * Invitation to an active group. If this is successful and we
3333 * are the GO, set the client wait to postpone some concurrent
3334 * operations and to allow provisioning and connection to happen
3335 * more quickly.
3336 */
3337 if (status == P2P_SC_SUCCESS &&
3338 group_if && group_if->current_ssid &&
3339 group_if->current_ssid->mode == WPAS_MODE_P2P_GO) {
3340 os_get_reltime(&wpa_s->global->p2p_go_wait_client);
3341 #ifdef CONFIG_TESTING_OPTIONS
3342 if (group_if->p2p_go_csa_on_inv) {
3343 wpa_printf(MSG_DEBUG,
3344 "Testing: force P2P GO CSA after invitation");
3345 eloop_cancel_timeout(
3346 wpas_p2p_reconsider_moving_go,
3347 wpa_s, NULL);
3348 eloop_register_timeout(
3349 0, 50000,
3350 wpas_p2p_reconsider_moving_go,
3351 wpa_s, NULL);
3352 }
3353 #endif /* CONFIG_TESTING_OPTIONS */
3354 }
3355 return;
3356 }
3357
3358 if (status == P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE) {
3359 wpa_printf(MSG_DEBUG, "P2P: Waiting for peer to start another "
3360 "invitation exchange to indicate readiness for "
3361 "re-invocation");
3362 }
3363
3364 if (status != P2P_SC_SUCCESS) {
3365 if (status == P2P_SC_FAIL_UNKNOWN_GROUP) {
3366 ssid = wpa_config_get_network(
3367 wpa_s->conf, wpa_s->pending_invite_ssid_id);
3368 wpas_remove_persistent_peer(wpa_s, ssid, peer, 1);
3369 }
3370 wpas_p2p_remove_pending_group_interface(wpa_s);
3371 return;
3372 }
3373
3374 ssid = wpa_config_get_network(wpa_s->conf,
3375 wpa_s->pending_invite_ssid_id);
3376 if (ssid == NULL) {
3377 wpa_printf(MSG_ERROR, "P2P: Could not find persistent group "
3378 "data matching with invitation");
3379 return;
3380 }
3381
3382 /*
3383 * The peer could have missed our ctrl::ack frame for Invitation
3384 * Response and continue retransmitting the frame. To reduce the
3385 * likelihood of the peer not getting successful TX status for the
3386 * Invitation Response frame, wait a short time here before starting
3387 * the persistent group so that we will remain on the current channel to
3388 * acknowledge any possible retransmission from the peer.
3389 */
3390 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: 50 ms wait on current channel before "
3391 "starting persistent group");
3392 os_sleep(0, 50000);
3393
3394 if (neg_freq > 0 && ssid->mode == WPAS_MODE_P2P_GO &&
3395 freq_included(wpa_s, channels, neg_freq))
3396 freq = neg_freq;
3397 else if (peer_oper_freq > 0 && ssid->mode != WPAS_MODE_P2P_GO &&
3398 freq_included(wpa_s, channels, peer_oper_freq))
3399 freq = peer_oper_freq;
3400 else
3401 freq = 0;
3402
3403 wpa_printf(MSG_DEBUG, "P2P: Persistent group invitation success - op_freq=%d MHz SSID=%s",
3404 freq, wpa_ssid_txt(ssid->ssid, ssid->ssid_len));
3405 wpas_p2p_group_add_persistent(wpa_s, ssid,
3406 ssid->mode == WPAS_MODE_P2P_GO,
3407 wpa_s->p2p_persistent_go_freq,
3408 freq,
3409 wpa_s->p2p_go_vht_center_freq2,
3410 wpa_s->p2p_go_ht40, wpa_s->p2p_go_vht,
3411 wpa_s->p2p_go_max_oper_chwidth,
3412 wpa_s->p2p_go_he,
3413 wpa_s->p2p_go_edmg,
3414 channels,
3415 ssid->mode == WPAS_MODE_P2P_GO ?
3416 P2P_MAX_INITIAL_CONN_WAIT_GO_REINVOKE :
3417 0, 1);
3418 }
3419
3420
3421 static int wpas_p2p_disallowed_freq(struct wpa_global *global,
3422 unsigned int freq)
3423 {
3424 if (freq_range_list_includes(&global->p2p_go_avoid_freq, freq))
3425 return 1;
3426 return freq_range_list_includes(&global->p2p_disallow_freq, freq);
3427 }
3428
3429
3430 static void wpas_p2p_add_chan(struct p2p_reg_class *reg, u8 chan)
3431 {
3432 reg->channel[reg->channels] = chan;
3433 reg->channels++;
3434 }
3435
3436
3437 static int wpas_p2p_default_channels(struct wpa_supplicant *wpa_s,
3438 struct p2p_channels *chan,
3439 struct p2p_channels *cli_chan)
3440 {
3441 int i, cla = 0;
3442
3443 wpa_s->global->p2p_24ghz_social_channels = 1;
3444
3445 os_memset(cli_chan, 0, sizeof(*cli_chan));
3446
3447 wpa_printf(MSG_DEBUG, "P2P: Enable operating classes for 2.4 GHz "
3448 "band");
3449
3450 /* Operating class 81 - 2.4 GHz band channels 1..13 */
3451 chan->reg_class[cla].reg_class = 81;
3452 chan->reg_class[cla].channels = 0;
3453 for (i = 0; i < 11; i++) {
3454 if (!wpas_p2p_disallowed_freq(wpa_s->global, 2412 + i * 5))
3455 wpas_p2p_add_chan(&chan->reg_class[cla], i + 1);
3456 }
3457 if (chan->reg_class[cla].channels)
3458 cla++;
3459
3460 wpa_printf(MSG_DEBUG, "P2P: Enable operating classes for lower 5 GHz "
3461 "band");
3462
3463 /* Operating class 115 - 5 GHz, channels 36-48 */
3464 chan->reg_class[cla].reg_class = 115;
3465 chan->reg_class[cla].channels = 0;
3466 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 36 * 5))
3467 wpas_p2p_add_chan(&chan->reg_class[cla], 36);
3468 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 40 * 5))
3469 wpas_p2p_add_chan(&chan->reg_class[cla], 40);
3470 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 44 * 5))
3471 wpas_p2p_add_chan(&chan->reg_class[cla], 44);
3472 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 48 * 5))
3473 wpas_p2p_add_chan(&chan->reg_class[cla], 48);
3474 if (chan->reg_class[cla].channels)
3475 cla++;
3476
3477 wpa_printf(MSG_DEBUG, "P2P: Enable operating classes for higher 5 GHz "
3478 "band");
3479
3480 /* Operating class 124 - 5 GHz, channels 149,153,157,161 */
3481 chan->reg_class[cla].reg_class = 124;
3482 chan->reg_class[cla].channels = 0;
3483 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 149 * 5))
3484 wpas_p2p_add_chan(&chan->reg_class[cla], 149);
3485 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 153 * 5))
3486 wpas_p2p_add_chan(&chan->reg_class[cla], 153);
3487 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 156 * 5))
3488 wpas_p2p_add_chan(&chan->reg_class[cla], 157);
3489 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 161 * 5))
3490 wpas_p2p_add_chan(&chan->reg_class[cla], 161);
3491 if (chan->reg_class[cla].channels)
3492 cla++;
3493
3494 chan->reg_classes = cla;
3495 return 0;
3496 }
3497
3498
3499 static int has_channel(struct wpa_global *global,
3500 struct hostapd_hw_modes *mode, u8 chan, int *flags)
3501 {
3502 int i;
3503 unsigned int freq;
3504
3505 freq = (mode->mode == HOSTAPD_MODE_IEEE80211A ? 5000 : 2407) +
3506 chan * 5;
3507 if (wpas_p2p_disallowed_freq(global, freq))
3508 return NOT_ALLOWED;
3509
3510 for (i = 0; i < mode->num_channels; i++) {
3511 if (mode->channels[i].chan == chan) {
3512 if (flags)
3513 *flags = mode->channels[i].flag;
3514 if (mode->channels[i].flag &
3515 (HOSTAPD_CHAN_DISABLED |
3516 HOSTAPD_CHAN_RADAR))
3517 return NOT_ALLOWED;
3518 if (mode->channels[i].flag & HOSTAPD_CHAN_NO_IR)
3519 return NO_IR;
3520 return ALLOWED;
3521 }
3522 }
3523
3524 return NOT_ALLOWED;
3525 }
3526
3527
3528 static int wpas_p2p_get_center_80mhz(struct wpa_supplicant *wpa_s,
3529 struct hostapd_hw_modes *mode,
3530 u8 channel)
3531 {
3532 u8 center_channels[] = { 42, 58, 106, 122, 138, 155 };
3533 size_t i;
3534
3535 if (mode->mode != HOSTAPD_MODE_IEEE80211A)
3536 return 0;
3537
3538 for (i = 0; i < ARRAY_SIZE(center_channels); i++)
3539 /*
3540 * In 80 MHz, the bandwidth "spans" 12 channels (e.g., 36-48),
3541 * so the center channel is 6 channels away from the start/end.
3542 */
3543 if (channel >= center_channels[i] - 6 &&
3544 channel <= center_channels[i] + 6)
3545 return center_channels[i];
3546
3547 return 0;
3548 }
3549
3550
3551 static enum chan_allowed wpas_p2p_verify_80mhz(struct wpa_supplicant *wpa_s,
3552 struct hostapd_hw_modes *mode,
3553 u8 channel, u8 bw)
3554 {
3555 u8 center_chan;
3556 int i, flags;
3557 enum chan_allowed res, ret = ALLOWED;
3558
3559 center_chan = wpas_p2p_get_center_80mhz(wpa_s, mode, channel);
3560 if (!center_chan)
3561 return NOT_ALLOWED;
3562 if (center_chan >= 58 && center_chan <= 138)
3563 return NOT_ALLOWED; /* Do not allow DFS channels for P2P */
3564
3565 /* check all the channels are available */
3566 for (i = 0; i < 4; i++) {
3567 int adj_chan = center_chan - 6 + i * 4;
3568
3569 res = has_channel(wpa_s->global, mode, adj_chan, &flags);
3570 if (res == NOT_ALLOWED)
3571 return NOT_ALLOWED;
3572 if (res == NO_IR)
3573 ret = NO_IR;
3574
3575 if (i == 0 && !(flags & HOSTAPD_CHAN_VHT_10_70))
3576 return NOT_ALLOWED;
3577 if (i == 1 && !(flags & HOSTAPD_CHAN_VHT_30_50))
3578 return NOT_ALLOWED;
3579 if (i == 2 && !(flags & HOSTAPD_CHAN_VHT_50_30))
3580 return NOT_ALLOWED;
3581 if (i == 3 && !(flags & HOSTAPD_CHAN_VHT_70_10))
3582 return NOT_ALLOWED;
3583 }
3584
3585 return ret;
3586 }
3587
3588
3589 static int wpas_p2p_get_center_160mhz(struct wpa_supplicant *wpa_s,
3590 struct hostapd_hw_modes *mode,
3591 u8 channel)
3592 {
3593 u8 center_channels[] = { 50, 114 };
3594 unsigned int i;
3595
3596 if (mode->mode != HOSTAPD_MODE_IEEE80211A)
3597 return 0;
3598
3599 for (i = 0; i < ARRAY_SIZE(center_channels); i++)
3600 /*
3601 * In 160 MHz, the bandwidth "spans" 28 channels (e.g., 36-64),
3602 * so the center channel is 14 channels away from the start/end.
3603 */
3604 if (channel >= center_channels[i] - 14 &&
3605 channel <= center_channels[i] + 14)
3606 return center_channels[i];
3607
3608 return 0;
3609 }
3610
3611
3612 static enum chan_allowed wpas_p2p_verify_160mhz(struct wpa_supplicant *wpa_s,
3613 struct hostapd_hw_modes *mode,
3614 u8 channel, u8 bw)
3615 {
3616 u8 center_chan;
3617 int i, flags;
3618 enum chan_allowed res, ret = ALLOWED;
3619
3620 center_chan = wpas_p2p_get_center_160mhz(wpa_s, mode, channel);
3621 if (!center_chan)
3622 return NOT_ALLOWED;
3623 /* VHT 160 MHz uses DFS channels in most countries. */
3624
3625 /* Check all the channels are available */
3626 for (i = 0; i < 8; i++) {
3627 int adj_chan = center_chan - 14 + i * 4;
3628
3629 res = has_channel(wpa_s->global, mode, adj_chan, &flags);
3630 if (res == NOT_ALLOWED)
3631 return NOT_ALLOWED;
3632
3633 if (res == NO_IR)
3634 ret = NO_IR;
3635
3636 if (i == 0 && !(flags & HOSTAPD_CHAN_VHT_10_150))
3637 return NOT_ALLOWED;
3638 if (i == 1 && !(flags & HOSTAPD_CHAN_VHT_30_130))
3639 return NOT_ALLOWED;
3640 if (i == 2 && !(flags & HOSTAPD_CHAN_VHT_50_110))
3641 return NOT_ALLOWED;
3642 if (i == 3 && !(flags & HOSTAPD_CHAN_VHT_70_90))
3643 return NOT_ALLOWED;
3644 if (i == 4 && !(flags & HOSTAPD_CHAN_VHT_90_70))
3645 return NOT_ALLOWED;
3646 if (i == 5 && !(flags & HOSTAPD_CHAN_VHT_110_50))
3647 return NOT_ALLOWED;
3648 if (i == 6 && !(flags & HOSTAPD_CHAN_VHT_130_30))
3649 return NOT_ALLOWED;
3650 if (i == 7 && !(flags & HOSTAPD_CHAN_VHT_150_10))
3651 return NOT_ALLOWED;
3652 }
3653
3654 return ret;
3655 }
3656
3657
3658 static enum chan_allowed wpas_p2p_verify_edmg(struct wpa_supplicant *wpa_s,
3659 struct hostapd_hw_modes *mode,
3660 u8 channel)
3661 {
3662 struct ieee80211_edmg_config edmg;
3663
3664 hostapd_encode_edmg_chan(1, channel, 0, &edmg);
3665 if (edmg.channels && ieee802_edmg_is_allowed(mode->edmg, edmg))
3666 return ALLOWED;
3667
3668 return NOT_ALLOWED;
3669 }
3670
3671
3672 static enum chan_allowed wpas_p2p_verify_channel(struct wpa_supplicant *wpa_s,
3673 struct hostapd_hw_modes *mode,
3674 u8 channel, u8 bw)
3675 {
3676 int flag = 0;
3677 enum chan_allowed res, res2;
3678
3679 res2 = res = has_channel(wpa_s->global, mode, channel, &flag);
3680 if (bw == BW40MINUS) {
3681 if (!(flag & HOSTAPD_CHAN_HT40MINUS))
3682 return NOT_ALLOWED;
3683 res2 = has_channel(wpa_s->global, mode, channel - 4, NULL);
3684 } else if (bw == BW40PLUS) {
3685 if (!(flag & HOSTAPD_CHAN_HT40PLUS))
3686 return NOT_ALLOWED;
3687 res2 = has_channel(wpa_s->global, mode, channel + 4, NULL);
3688 } else if (bw == BW80) {
3689 res2 = wpas_p2p_verify_80mhz(wpa_s, mode, channel, bw);
3690 } else if (bw == BW160) {
3691 res2 = wpas_p2p_verify_160mhz(wpa_s, mode, channel, bw);
3692 } else if (bw == BW4320 || bw == BW6480 || bw == BW8640) {
3693 return wpas_p2p_verify_edmg(wpa_s, mode, channel);
3694 }
3695
3696 if (res == NOT_ALLOWED || res2 == NOT_ALLOWED)
3697 return NOT_ALLOWED;
3698 if (res == NO_IR || res2 == NO_IR)
3699 return NO_IR;
3700 return res;
3701 }
3702
3703
3704 static int wpas_p2p_setup_channels(struct wpa_supplicant *wpa_s,
3705 struct p2p_channels *chan,
3706 struct p2p_channels *cli_chan)
3707 {
3708 struct hostapd_hw_modes *mode;
3709 int cla, op, cli_cla;
3710
3711 if (wpa_s->hw.modes == NULL) {
3712 wpa_printf(MSG_DEBUG, "P2P: Driver did not support fetching "
3713 "of all supported channels; assume dualband "
3714 "support");
3715 return wpas_p2p_default_channels(wpa_s, chan, cli_chan);
3716 }
3717
3718 cla = cli_cla = 0;
3719
3720 for (op = 0; global_op_class[op].op_class; op++) {
3721 const struct oper_class_map *o = &global_op_class[op];
3722 u8 ch;
3723 struct p2p_reg_class *reg = NULL, *cli_reg = NULL;
3724
3725 if (o->p2p == NO_P2P_SUPP)
3726 continue;
3727
3728 mode = get_mode(wpa_s->hw.modes, wpa_s->hw.num_modes, o->mode,
3729 is_6ghz_op_class(o->op_class));
3730 if (mode == NULL)
3731 continue;
3732 if (mode->mode == HOSTAPD_MODE_IEEE80211G)
3733 wpa_s->global->p2p_24ghz_social_channels = 1;
3734 for (ch = o->min_chan; ch <= o->max_chan; ch += o->inc) {
3735 enum chan_allowed res;
3736 res = wpas_p2p_verify_channel(wpa_s, mode, ch, o->bw);
3737 if (res == ALLOWED) {
3738 if (reg == NULL) {
3739 if (cla == P2P_MAX_REG_CLASSES)
3740 continue;
3741 wpa_printf(MSG_DEBUG, "P2P: Add operating class %u",
3742 o->op_class);
3743 reg = &chan->reg_class[cla];
3744 cla++;
3745 reg->reg_class = o->op_class;
3746 }
3747 if (reg->channels == P2P_MAX_REG_CLASS_CHANNELS)
3748 continue;
3749 reg->channel[reg->channels] = ch;
3750 reg->channels++;
3751 } else if (res == NO_IR &&
3752 wpa_s->conf->p2p_add_cli_chan) {
3753 if (cli_reg == NULL) {
3754 if (cli_cla == P2P_MAX_REG_CLASSES)
3755 continue;
3756 wpa_printf(MSG_DEBUG, "P2P: Add operating class %u (client only)",
3757 o->op_class);
3758 cli_reg = &cli_chan->reg_class[cli_cla];
3759 cli_cla++;
3760 cli_reg->reg_class = o->op_class;
3761 }
3762 if (cli_reg->channels ==
3763 P2P_MAX_REG_CLASS_CHANNELS)
3764 continue;
3765 cli_reg->channel[cli_reg->channels] = ch;
3766 cli_reg->channels++;
3767 }
3768 }
3769 if (reg) {
3770 wpa_hexdump(MSG_DEBUG, "P2P: Channels",
3771 reg->channel, reg->channels);
3772 }
3773 if (cli_reg) {
3774 wpa_hexdump(MSG_DEBUG, "P2P: Channels (client only)",
3775 cli_reg->channel, cli_reg->channels);
3776 }
3777 }
3778
3779 chan->reg_classes = cla;
3780 cli_chan->reg_classes = cli_cla;
3781
3782 return 0;
3783 }
3784
3785
3786 int wpas_p2p_get_ht40_mode(struct wpa_supplicant *wpa_s,
3787 struct hostapd_hw_modes *mode, u8 channel)
3788 {
3789 int op;
3790 enum chan_allowed ret;
3791
3792 for (op = 0; global_op_class[op].op_class; op++) {
3793 const struct oper_class_map *o = &global_op_class[op];
3794 u8 ch;
3795
3796 if (o->p2p == NO_P2P_SUPP)
3797 continue;
3798
3799 for (ch = o->min_chan; ch <= o->max_chan; ch += o->inc) {
3800 if (o->mode != HOSTAPD_MODE_IEEE80211A ||
3801 (o->bw != BW40PLUS && o->bw != BW40MINUS) ||
3802 ch != channel)
3803 continue;
3804 ret = wpas_p2p_verify_channel(wpa_s, mode, ch, o->bw);
3805 if (ret == ALLOWED)
3806 return (o->bw == BW40MINUS) ? -1 : 1;
3807 }
3808 }
3809 return 0;
3810 }
3811
3812
3813 int wpas_p2p_get_vht80_center(struct wpa_supplicant *wpa_s,
3814 struct hostapd_hw_modes *mode, u8 channel)
3815 {
3816 if (!wpas_p2p_verify_channel(wpa_s, mode, channel, BW80))
3817 return 0;
3818
3819 return wpas_p2p_get_center_80mhz(wpa_s, mode, channel);
3820 }
3821
3822
3823 int wpas_p2p_get_vht160_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, BW160))
3827 return 0;
3828 return wpas_p2p_get_center_160mhz(wpa_s, mode, channel);
3829 }
3830
3831
3832 static int wpas_get_noa(void *ctx, const u8 *interface_addr, u8 *buf,
3833 size_t buf_len)
3834 {
3835 struct wpa_supplicant *wpa_s = ctx;
3836
3837 for (wpa_s = wpa_s->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
3838 if (os_memcmp(wpa_s->own_addr, interface_addr, ETH_ALEN) == 0)
3839 break;
3840 }
3841 if (wpa_s == NULL)
3842 return -1;
3843
3844 return wpa_drv_get_noa(wpa_s, buf, buf_len);
3845 }
3846
3847
3848 struct wpa_supplicant * wpas_get_p2p_go_iface(struct wpa_supplicant *wpa_s,
3849 const u8 *ssid, size_t ssid_len)
3850 {
3851 for (wpa_s = wpa_s->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
3852 struct wpa_ssid *s = wpa_s->current_ssid;
3853 if (s == NULL)
3854 continue;
3855 if (s->mode != WPAS_MODE_P2P_GO &&
3856 s->mode != WPAS_MODE_AP &&
3857 s->mode != WPAS_MODE_P2P_GROUP_FORMATION)
3858 continue;
3859 if (s->ssid_len != ssid_len ||
3860 os_memcmp(ssid, s->ssid, ssid_len) != 0)
3861 continue;
3862 return wpa_s;
3863 }
3864
3865 return NULL;
3866
3867 }
3868
3869
3870 struct wpa_supplicant * wpas_get_p2p_client_iface(struct wpa_supplicant *wpa_s,
3871 const u8 *peer_dev_addr)
3872 {
3873 for (wpa_s = wpa_s->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
3874 struct wpa_ssid *ssid = wpa_s->current_ssid;
3875 if (ssid && (ssid->mode != WPAS_MODE_INFRA || !ssid->p2p_group))
3876 continue;
3877 if (os_memcmp(wpa_s->go_dev_addr, peer_dev_addr, ETH_ALEN) == 0)
3878 return wpa_s;
3879 }
3880
3881 return NULL;
3882 }
3883
3884
3885 static int wpas_go_connected(void *ctx, const u8 *dev_addr)
3886 {
3887 struct wpa_supplicant *wpa_s = ctx;
3888
3889 return wpas_get_p2p_client_iface(wpa_s, dev_addr) != NULL;
3890 }
3891
3892
3893 static int wpas_is_concurrent_session_active(void *ctx)
3894 {
3895 struct wpa_supplicant *wpa_s = ctx;
3896 struct wpa_supplicant *ifs;
3897
3898 for (ifs = wpa_s->global->ifaces; ifs; ifs = ifs->next) {
3899 if (ifs == wpa_s)
3900 continue;
3901 if (ifs->wpa_state > WPA_ASSOCIATED)
3902 return 1;
3903 }
3904 return 0;
3905 }
3906
3907
3908 static void wpas_p2p_debug_print(void *ctx, int level, const char *msg)
3909 {
3910 struct wpa_supplicant *wpa_s = ctx;
3911 wpa_msg_global(wpa_s, level, "P2P: %s", msg);
3912 }
3913
3914
3915 int wpas_p2p_add_p2pdev_interface(struct wpa_supplicant *wpa_s,
3916 const char *conf_p2p_dev)
3917 {
3918 struct wpa_interface iface;
3919 struct wpa_supplicant *p2pdev_wpa_s;
3920 char ifname[100];
3921 char force_name[100];
3922 int ret;
3923
3924 ret = os_snprintf(ifname, sizeof(ifname), P2P_MGMT_DEVICE_PREFIX "%s",
3925 wpa_s->ifname);
3926 if (os_snprintf_error(sizeof(ifname), ret))
3927 return -1;
3928 force_name[0] = '\0';
3929 wpa_s->pending_interface_type = WPA_IF_P2P_DEVICE;
3930 ret = wpa_drv_if_add(wpa_s, WPA_IF_P2P_DEVICE, ifname, NULL, NULL,
3931 force_name, wpa_s->pending_interface_addr, NULL);
3932 if (ret < 0) {
3933 wpa_printf(MSG_DEBUG, "P2P: Failed to create P2P Device interface");
3934 return ret;
3935 }
3936 os_strlcpy(wpa_s->pending_interface_name, ifname,
3937 sizeof(wpa_s->pending_interface_name));
3938
3939 os_memset(&iface, 0, sizeof(iface));
3940 iface.p2p_mgmt = 1;
3941 iface.ifname = wpa_s->pending_interface_name;
3942 iface.driver = wpa_s->driver->name;
3943 iface.driver_param = wpa_s->conf->driver_param;
3944
3945 /*
3946 * If a P2P Device configuration file was given, use it as the interface
3947 * configuration file (instead of using parent's configuration file.
3948 */
3949 if (conf_p2p_dev) {
3950 iface.confname = conf_p2p_dev;
3951 iface.ctrl_interface = NULL;
3952 } else {
3953 iface.confname = wpa_s->confname;
3954 iface.ctrl_interface = wpa_s->conf->ctrl_interface;
3955 }
3956
3957 p2pdev_wpa_s = wpa_supplicant_add_iface(wpa_s->global, &iface, wpa_s);
3958 if (!p2pdev_wpa_s) {
3959 wpa_printf(MSG_DEBUG, "P2P: Failed to add P2P Device interface");
3960 return -1;
3961 }
3962
3963 p2pdev_wpa_s->p2pdev = p2pdev_wpa_s;
3964 wpa_s->pending_interface_name[0] = '\0';
3965 return 0;
3966 }
3967
3968
3969 static void wpas_presence_resp(void *ctx, const u8 *src, u8 status,
3970 const u8 *noa, size_t noa_len)
3971 {
3972 struct wpa_supplicant *wpa_s, *intf = ctx;
3973 char hex[100];
3974
3975 for (wpa_s = intf->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
3976 if (wpa_s->waiting_presence_resp)
3977 break;
3978 }
3979 if (!wpa_s) {
3980 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: No group interface was waiting for presence response");
3981 return;
3982 }
3983 wpa_s->waiting_presence_resp = 0;
3984
3985 wpa_snprintf_hex(hex, sizeof(hex), noa, noa_len);
3986 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_PRESENCE_RESPONSE "src=" MACSTR
3987 " status=%u noa=%s", MAC2STR(src), status, hex);
3988 }
3989
3990
3991 static int wpas_get_persistent_group(void *ctx, const u8 *addr, const u8 *ssid,
3992 size_t ssid_len, u8 *go_dev_addr,
3993 u8 *ret_ssid, size_t *ret_ssid_len,
3994 u8 *intended_iface_addr)
3995 {
3996 struct wpa_supplicant *wpa_s = ctx;
3997 struct wpa_ssid *s;
3998
3999 s = wpas_p2p_get_persistent(wpa_s, addr, ssid, ssid_len);
4000 if (s) {
4001 os_memcpy(ret_ssid, s->ssid, s->ssid_len);
4002 *ret_ssid_len = s->ssid_len;
4003 os_memcpy(go_dev_addr, s->bssid, ETH_ALEN);
4004
4005 if (s->mode != WPAS_MODE_P2P_GO) {
4006 os_memset(intended_iface_addr, 0, ETH_ALEN);
4007 } else if (wpas_p2p_create_iface(wpa_s)) {
4008 if (wpas_p2p_add_group_interface(wpa_s, WPA_IF_P2P_GO))
4009 return 0;
4010
4011 os_memcpy(intended_iface_addr,
4012 wpa_s->pending_interface_addr, ETH_ALEN);
4013 } else {
4014 os_memcpy(intended_iface_addr, wpa_s->own_addr,
4015 ETH_ALEN);
4016 }
4017 return 1;
4018 }
4019
4020 return 0;
4021 }
4022
4023
4024 static int wpas_get_go_info(void *ctx, u8 *intended_addr,
4025 u8 *ssid, size_t *ssid_len, int *group_iface,
4026 unsigned int *freq)
4027 {
4028 struct wpa_supplicant *wpa_s = ctx;
4029 struct wpa_supplicant *go;
4030 struct wpa_ssid *s;
4031
4032 /*
4033 * group_iface will be set to 1 only if a dedicated interface for P2P
4034 * role is required. First, we try to reuse an active GO. However,
4035 * if it is not present, we will try to reactivate an existing
4036 * persistent group and set group_iface to 1, so the caller will know
4037 * that the pending interface should be used.
4038 */
4039 *group_iface = 0;
4040
4041 if (freq)
4042 *freq = 0;
4043
4044 go = wpas_p2p_get_go_group(wpa_s);
4045 if (!go) {
4046 s = wpas_p2p_get_persistent_go(wpa_s);
4047 *group_iface = wpas_p2p_create_iface(wpa_s);
4048 if (s)
4049 os_memcpy(intended_addr, s->bssid, ETH_ALEN);
4050 else
4051 return 0;
4052 } else {
4053 s = go->current_ssid;
4054 os_memcpy(intended_addr, go->own_addr, ETH_ALEN);
4055 if (freq)
4056 *freq = go->assoc_freq;
4057 }
4058
4059 os_memcpy(ssid, s->ssid, s->ssid_len);
4060 *ssid_len = s->ssid_len;
4061
4062 return 1;
4063 }
4064
4065
4066 static int wpas_remove_stale_groups(void *ctx, const u8 *peer, const u8 *go,
4067 const u8 *ssid, size_t ssid_len)
4068 {
4069 struct wpa_supplicant *wpa_s = ctx;
4070 struct wpa_ssid *s;
4071 int save_config = 0;
4072 size_t i;
4073
4074 /* Start with our first choice of Persistent Groups */
4075 while ((s = wpas_p2p_get_persistent(wpa_s, peer, NULL, 0))) {
4076 if (go && ssid && ssid_len &&
4077 s->ssid_len == ssid_len &&
4078 os_memcmp(go, s->bssid, ETH_ALEN) == 0 &&
4079 os_memcmp(ssid, s->ssid, ssid_len) == 0)
4080 break;
4081
4082 /* Remove stale persistent group */
4083 if (s->mode != WPAS_MODE_P2P_GO || s->num_p2p_clients <= 1) {
4084 wpa_dbg(wpa_s, MSG_DEBUG,
4085 "P2P: Remove stale persistent group id=%d",
4086 s->id);
4087 wpas_notify_persistent_group_removed(wpa_s, s);
4088 wpa_config_remove_network(wpa_s->conf, s->id);
4089 save_config = 1;
4090 continue;
4091 }
4092
4093 for (i = 0; i < s->num_p2p_clients; i++) {
4094 if (os_memcmp(s->p2p_client_list + i * 2 * ETH_ALEN,
4095 peer, ETH_ALEN) != 0)
4096 continue;
4097
4098 os_memmove(s->p2p_client_list + i * 2 * ETH_ALEN,
4099 s->p2p_client_list + (i + 1) * 2 * ETH_ALEN,
4100 (s->num_p2p_clients - i - 1) * 2 * ETH_ALEN);
4101 break;
4102 }
4103 s->num_p2p_clients--;
4104 save_config = 1;
4105 }
4106
4107 if (save_config)
4108 p2p_config_write(wpa_s);
4109
4110 /* Return TRUE if valid SSID remains */
4111 return s != NULL;
4112 }
4113
4114
4115 static void wpas_p2ps_get_feat_cap_str(char *buf, size_t buf_len,
4116 const u8 *feat_cap, size_t feat_cap_len)
4117 {
4118 static const char pref[] = " feature_cap=";
4119 int ret;
4120
4121 buf[0] = '\0';
4122
4123 /*
4124 * We expect a feature capability to contain at least one byte to be
4125 * reported. The string buffer provided by the caller function is
4126 * expected to be big enough to contain all bytes of the attribute for
4127 * known specifications. This function truncates the reported bytes if
4128 * the feature capability data exceeds the string buffer size.
4129 */
4130 if (!feat_cap || !feat_cap_len || buf_len < sizeof(pref) + 2)
4131 return;
4132
4133 os_memcpy(buf, pref, sizeof(pref));
4134 ret = wpa_snprintf_hex(&buf[sizeof(pref) - 1],
4135 buf_len - sizeof(pref) + 1,
4136 feat_cap, feat_cap_len);
4137
4138 if (ret != (2 * (int) feat_cap_len))
4139 wpa_printf(MSG_WARNING, "P2PS feature_cap bytes truncated");
4140 }
4141
4142
4143 static void wpas_p2ps_prov_complete(void *ctx, u8 status, const u8 *dev,
4144 const u8 *adv_mac, const u8 *ses_mac,
4145 const u8 *grp_mac, u32 adv_id, u32 ses_id,
4146 u8 conncap, int passwd_id,
4147 const u8 *persist_ssid,
4148 size_t persist_ssid_size, int response_done,
4149 int prov_start, const char *session_info,
4150 const u8 *feat_cap, size_t feat_cap_len,
4151 unsigned int freq,
4152 const u8 *group_ssid, size_t group_ssid_len)
4153 {
4154 struct wpa_supplicant *wpa_s = ctx;
4155 u8 mac[ETH_ALEN];
4156 struct wpa_ssid *persistent_go, *stale, *s = NULL;
4157 int save_config = 0;
4158 struct wpa_supplicant *go_wpa_s;
4159 char feat_cap_str[256];
4160
4161 if (!dev)
4162 return;
4163
4164 os_memset(mac, 0, ETH_ALEN);
4165 if (!adv_mac)
4166 adv_mac = mac;
4167 if (!ses_mac)
4168 ses_mac = mac;
4169 if (!grp_mac)
4170 grp_mac = mac;
4171
4172 wpas_p2ps_get_feat_cap_str(feat_cap_str, sizeof(feat_cap_str),
4173 feat_cap, feat_cap_len);
4174
4175 if (prov_start) {
4176 if (session_info == NULL) {
4177 wpa_msg_global(wpa_s, MSG_INFO,
4178 P2P_EVENT_P2PS_PROVISION_START MACSTR
4179 " adv_id=%x conncap=%x"
4180 " adv_mac=" MACSTR
4181 " session=%x mac=" MACSTR
4182 " dev_passwd_id=%d%s",
4183 MAC2STR(dev), adv_id, conncap,
4184 MAC2STR(adv_mac),
4185 ses_id, MAC2STR(ses_mac),
4186 passwd_id, feat_cap_str);
4187 } else {
4188 wpa_msg_global(wpa_s, MSG_INFO,
4189 P2P_EVENT_P2PS_PROVISION_START MACSTR
4190 " adv_id=%x conncap=%x"
4191 " adv_mac=" MACSTR
4192 " session=%x mac=" MACSTR
4193 " dev_passwd_id=%d info='%s'%s",
4194 MAC2STR(dev), adv_id, conncap,
4195 MAC2STR(adv_mac),
4196 ses_id, MAC2STR(ses_mac),
4197 passwd_id, session_info, feat_cap_str);
4198 }
4199 return;
4200 }
4201
4202 go_wpa_s = wpas_p2p_get_go_group(wpa_s);
4203 persistent_go = wpas_p2p_get_persistent_go(wpa_s);
4204
4205 if (status && status != P2P_SC_SUCCESS_DEFERRED) {
4206 if (go_wpa_s && !p2p_group_go_member_count(wpa_s))
4207 wpas_p2p_group_remove(wpa_s, go_wpa_s->ifname);
4208
4209 if (persistent_go && !persistent_go->num_p2p_clients) {
4210 /* remove empty persistent GO */
4211 wpa_dbg(wpa_s, MSG_DEBUG,
4212 "P2P: Remove empty persistent group id=%d",
4213 persistent_go->id);
4214 wpas_notify_persistent_group_removed(wpa_s,
4215 persistent_go);
4216 wpa_config_remove_network(wpa_s->conf,
4217 persistent_go->id);
4218 }
4219
4220 wpa_msg_global(wpa_s, MSG_INFO,
4221 P2P_EVENT_P2PS_PROVISION_DONE MACSTR
4222 " status=%d"
4223 " adv_id=%x adv_mac=" MACSTR
4224 " session=%x mac=" MACSTR "%s",
4225 MAC2STR(dev), status,
4226 adv_id, MAC2STR(adv_mac),
4227 ses_id, MAC2STR(ses_mac), feat_cap_str);
4228 return;
4229 }
4230
4231 /* Clean up stale persistent groups with this device */
4232 if (persist_ssid && persist_ssid_size)
4233 s = wpas_p2p_get_persistent(wpa_s, dev, persist_ssid,
4234 persist_ssid_size);
4235
4236 if (persist_ssid && s && s->mode != WPAS_MODE_P2P_GO &&
4237 is_zero_ether_addr(grp_mac)) {
4238 wpa_dbg(wpa_s, MSG_ERROR,
4239 "P2P: Peer device is a GO in a persistent group, but it did not provide the intended MAC address");
4240 return;
4241 }
4242
4243 for (;;) {
4244 stale = wpas_p2p_get_persistent(wpa_s, dev, NULL, 0);
4245 if (!stale)
4246 break;
4247
4248 if (s && s->ssid_len == stale->ssid_len &&
4249 os_memcmp(stale->bssid, s->bssid, ETH_ALEN) == 0 &&
4250 os_memcmp(stale->ssid, s->ssid, s->ssid_len) == 0)
4251 break;
4252
4253 /* Remove stale persistent group */
4254 if (stale->mode != WPAS_MODE_P2P_GO ||
4255 stale->num_p2p_clients <= 1) {
4256 wpa_dbg(wpa_s, MSG_DEBUG,
4257 "P2P: Remove stale persistent group id=%d",
4258 stale->id);
4259 wpas_notify_persistent_group_removed(wpa_s, stale);
4260 wpa_config_remove_network(wpa_s->conf, stale->id);
4261 } else {
4262 size_t i;
4263
4264 for (i = 0; i < stale->num_p2p_clients; i++) {
4265 if (os_memcmp(stale->p2p_client_list +
4266 i * ETH_ALEN,
4267 dev, ETH_ALEN) == 0) {
4268 os_memmove(stale->p2p_client_list +
4269 i * ETH_ALEN,
4270 stale->p2p_client_list +
4271 (i + 1) * ETH_ALEN,
4272 (stale->num_p2p_clients -
4273 i - 1) * ETH_ALEN);
4274 break;
4275 }
4276 }
4277 stale->num_p2p_clients--;
4278 }
4279 save_config = 1;
4280 }
4281
4282 if (save_config)
4283 p2p_config_write(wpa_s);
4284
4285 if (s) {
4286 if (go_wpa_s && !p2p_group_go_member_count(wpa_s))
4287 wpas_p2p_group_remove(wpa_s, go_wpa_s->ifname);
4288
4289 if (persistent_go && s != persistent_go &&
4290 !persistent_go->num_p2p_clients) {
4291 /* remove empty persistent GO */
4292 wpa_dbg(wpa_s, MSG_DEBUG,
4293 "P2P: Remove empty persistent group id=%d",
4294 persistent_go->id);
4295 wpas_notify_persistent_group_removed(wpa_s,
4296 persistent_go);
4297 wpa_config_remove_network(wpa_s->conf,
4298 persistent_go->id);
4299 /* Save config */
4300 }
4301
4302 wpa_msg_global(wpa_s, MSG_INFO,
4303 P2P_EVENT_P2PS_PROVISION_DONE MACSTR
4304 " status=%d"
4305 " adv_id=%x adv_mac=" MACSTR
4306 " session=%x mac=" MACSTR
4307 " persist=%d%s",
4308 MAC2STR(dev), status,
4309 adv_id, MAC2STR(adv_mac),
4310 ses_id, MAC2STR(ses_mac), s->id, feat_cap_str);
4311 return;
4312 }
4313
4314 wpa_s->global->pending_p2ps_group = 0;
4315 wpa_s->global->pending_p2ps_group_freq = 0;
4316
4317 if (conncap == P2PS_SETUP_GROUP_OWNER) {
4318 /*
4319 * We need to copy the interface name. Simply saving a
4320 * pointer isn't enough, since if we use pending_interface_name
4321 * it will be overwritten when the group is added.
4322 */
4323 char go_ifname[100];
4324
4325 go_ifname[0] = '\0';
4326 if (!go_wpa_s) {
4327 if (!response_done) {
4328 wpa_s->global->pending_p2ps_group = 1;
4329 wpa_s->global->pending_p2ps_group_freq = freq;
4330 }
4331
4332 if (!wpas_p2p_create_iface(wpa_s))
4333 os_memcpy(go_ifname, wpa_s->ifname,
4334 sizeof(go_ifname));
4335 else if (wpa_s->pending_interface_name[0])
4336 os_memcpy(go_ifname,
4337 wpa_s->pending_interface_name,
4338 sizeof(go_ifname));
4339
4340 if (!go_ifname[0]) {
4341 wpas_p2ps_prov_complete(
4342 wpa_s, P2P_SC_FAIL_UNKNOWN_GROUP,
4343 dev, adv_mac, ses_mac,
4344 grp_mac, adv_id, ses_id, 0, 0,
4345 NULL, 0, 0, 0, NULL, NULL, 0, 0,
4346 NULL, 0);
4347 return;
4348 }
4349
4350 /* If PD Resp complete, start up the GO */
4351 if (response_done && persistent_go) {
4352 wpas_p2p_group_add_persistent(
4353 wpa_s, persistent_go,
4354 0, 0, freq, 0, 0, 0, 0, 0, 0, NULL,
4355 persistent_go->mode ==
4356 WPAS_MODE_P2P_GO ?
4357 P2P_MAX_INITIAL_CONN_WAIT_GO_REINVOKE :
4358 0, 0);
4359 } else if (response_done) {
4360 wpas_p2p_group_add(wpa_s, 1, freq,
4361 0, 0, 0, 0, 0, 0);
4362 }
4363
4364 if (passwd_id == DEV_PW_P2PS_DEFAULT) {
4365 os_memcpy(wpa_s->p2ps_join_addr, grp_mac,
4366 ETH_ALEN);
4367 wpa_s->p2ps_method_config_any = 1;
4368 }
4369 } else if (passwd_id == DEV_PW_P2PS_DEFAULT) {
4370 os_memcpy(go_ifname, go_wpa_s->ifname,
4371 sizeof(go_ifname));
4372
4373 if (is_zero_ether_addr(grp_mac)) {
4374 wpa_dbg(go_wpa_s, MSG_DEBUG,
4375 "P2P: Setting PIN-1 for ANY");
4376 wpa_supplicant_ap_wps_pin(go_wpa_s, NULL,
4377 "12345670", NULL, 0,
4378 0);
4379 } else {
4380 wpa_dbg(go_wpa_s, MSG_DEBUG,
4381 "P2P: Setting PIN-1 for " MACSTR,
4382 MAC2STR(grp_mac));
4383 wpa_supplicant_ap_wps_pin(go_wpa_s, grp_mac,
4384 "12345670", NULL, 0,
4385 0);
4386 }
4387
4388 os_memcpy(wpa_s->p2ps_join_addr, grp_mac, ETH_ALEN);
4389 wpa_s->p2ps_method_config_any = 1;
4390 }
4391
4392 wpa_msg_global(wpa_s, MSG_INFO,
4393 P2P_EVENT_P2PS_PROVISION_DONE MACSTR
4394 " status=%d conncap=%x"
4395 " adv_id=%x adv_mac=" MACSTR
4396 " session=%x mac=" MACSTR
4397 " dev_passwd_id=%d go=%s%s",
4398 MAC2STR(dev), status, conncap,
4399 adv_id, MAC2STR(adv_mac),
4400 ses_id, MAC2STR(ses_mac),
4401 passwd_id, go_ifname, feat_cap_str);
4402 return;
4403 }
4404
4405 if (go_wpa_s && !p2p_group_go_member_count(wpa_s))
4406 wpas_p2p_group_remove(wpa_s, go_wpa_s->ifname);
4407
4408 if (persistent_go && !persistent_go->num_p2p_clients) {
4409 /* remove empty persistent GO */
4410 wpa_dbg(wpa_s, MSG_DEBUG,
4411 "P2P: Remove empty persistent group id=%d",
4412 persistent_go->id);
4413 wpas_notify_persistent_group_removed(wpa_s, persistent_go);
4414 wpa_config_remove_network(wpa_s->conf, persistent_go->id);
4415 }
4416
4417 if (conncap == P2PS_SETUP_CLIENT) {
4418 char ssid_hex[32 * 2 + 1];
4419
4420 if (group_ssid)
4421 wpa_snprintf_hex(ssid_hex, sizeof(ssid_hex),
4422 group_ssid, group_ssid_len);
4423 else
4424 ssid_hex[0] = '\0';
4425 wpa_msg_global(wpa_s, MSG_INFO,
4426 P2P_EVENT_P2PS_PROVISION_DONE MACSTR
4427 " status=%d conncap=%x"
4428 " adv_id=%x adv_mac=" MACSTR
4429 " session=%x mac=" MACSTR
4430 " dev_passwd_id=%d join=" MACSTR "%s%s%s",
4431 MAC2STR(dev), status, conncap,
4432 adv_id, MAC2STR(adv_mac),
4433 ses_id, MAC2STR(ses_mac),
4434 passwd_id, MAC2STR(grp_mac), feat_cap_str,
4435 group_ssid ? " group_ssid=" : "", ssid_hex);
4436 } else {
4437 wpa_msg_global(wpa_s, MSG_INFO,
4438 P2P_EVENT_P2PS_PROVISION_DONE MACSTR
4439 " status=%d conncap=%x"
4440 " adv_id=%x adv_mac=" MACSTR
4441 " session=%x mac=" MACSTR
4442 " dev_passwd_id=%d%s",
4443 MAC2STR(dev), status, conncap,
4444 adv_id, MAC2STR(adv_mac),
4445 ses_id, MAC2STR(ses_mac),
4446 passwd_id, feat_cap_str);
4447 }
4448 }
4449
4450
4451 static int _wpas_p2p_in_progress(void *ctx)
4452 {
4453 struct wpa_supplicant *wpa_s = ctx;
4454 return wpas_p2p_in_progress(wpa_s);
4455 }
4456
4457
4458 static int wpas_prov_disc_resp_cb(void *ctx)
4459 {
4460 struct wpa_supplicant *wpa_s = ctx;
4461 struct wpa_ssid *persistent_go;
4462 unsigned int freq;
4463
4464 if (!wpa_s->global->pending_p2ps_group)
4465 return 0;
4466
4467 freq = wpa_s->global->pending_p2ps_group_freq;
4468 wpa_s->global->pending_p2ps_group_freq = 0;
4469 wpa_s->global->pending_p2ps_group = 0;
4470
4471 if (wpas_p2p_get_go_group(wpa_s))
4472 return 0;
4473 persistent_go = wpas_p2p_get_persistent_go(wpa_s);
4474
4475 if (persistent_go) {
4476 wpas_p2p_group_add_persistent(
4477 wpa_s, persistent_go, 0, 0, 0, 0, 0, 0, 0, 0, 0,
4478 NULL,
4479 persistent_go->mode == WPAS_MODE_P2P_GO ?
4480 P2P_MAX_INITIAL_CONN_WAIT_GO_REINVOKE : 0, 0);
4481 } else {
4482 wpas_p2p_group_add(wpa_s, 1, freq, 0, 0, 0, 0, 0, 0);
4483 }
4484
4485 return 1;
4486 }
4487
4488
4489 static int wpas_p2p_get_pref_freq_list(void *ctx, int go,
4490 unsigned int *len,
4491 unsigned int *freq_list)
4492 {
4493 struct wpa_supplicant *wpa_s = ctx;
4494
4495 return wpa_drv_get_pref_freq_list(wpa_s, go ? WPA_IF_P2P_GO :
4496 WPA_IF_P2P_CLIENT, len, freq_list);
4497 }
4498
4499
4500 int wpas_p2p_mac_setup(struct wpa_supplicant *wpa_s)
4501 {
4502 u8 addr[ETH_ALEN] = {0};
4503
4504 if (wpa_s->conf->p2p_device_random_mac_addr == 0)
4505 return 0;
4506
4507 if (!wpa_s->conf->ssid) {
4508 if (random_mac_addr(addr) < 0) {
4509 wpa_msg(wpa_s, MSG_INFO,
4510 "Failed to generate random MAC address");
4511 return -EINVAL;
4512 }
4513
4514 /* Store generated MAC address. */
4515 os_memcpy(wpa_s->conf->p2p_device_persistent_mac_addr, addr,
4516 ETH_ALEN);
4517 } else {
4518 /* If there are existing saved groups, restore last MAC address.
4519 * if there is no last used MAC address, the last one is
4520 * factory MAC. */
4521 if (is_zero_ether_addr(
4522 wpa_s->conf->p2p_device_persistent_mac_addr))
4523 return 0;
4524 os_memcpy(addr, wpa_s->conf->p2p_device_persistent_mac_addr,
4525 ETH_ALEN);
4526 wpa_msg(wpa_s, MSG_DEBUG, "Restore last used MAC address.");
4527 }
4528
4529 if (wpa_drv_set_mac_addr(wpa_s, addr) < 0) {
4530 wpa_msg(wpa_s, MSG_INFO,
4531 "Failed to set random MAC address");
4532 return -EINVAL;
4533 }
4534
4535 if (wpa_supplicant_update_mac_addr(wpa_s) < 0) {
4536 wpa_msg(wpa_s, MSG_INFO,
4537 "Could not update MAC address information");
4538 return -EINVAL;
4539 }
4540
4541 wpa_msg(wpa_s, MSG_DEBUG, "Using random MAC address " MACSTR,
4542 MAC2STR(addr));
4543
4544 return 0;
4545 }
4546
4547
4548 /**
4549 * wpas_p2p_init - Initialize P2P module for %wpa_supplicant
4550 * @global: Pointer to global data from wpa_supplicant_init()
4551 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
4552 * Returns: 0 on success, -1 on failure
4553 */
4554 int wpas_p2p_init(struct wpa_global *global, struct wpa_supplicant *wpa_s)
4555 {
4556 struct p2p_config p2p;
4557 int i;
4558
4559 if (wpa_s->conf->p2p_disabled)
4560 return 0;
4561
4562 if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_CAPABLE))
4563 return 0;
4564
4565 if (global->p2p)
4566 return 0;
4567
4568 if (wpas_p2p_mac_setup(wpa_s) < 0) {
4569 wpa_msg(wpa_s, MSG_ERROR,
4570 "Failed to initialize P2P random MAC address.");
4571 return -1;
4572 }
4573
4574 os_memset(&p2p, 0, sizeof(p2p));
4575 p2p.cb_ctx = wpa_s;
4576 p2p.debug_print = wpas_p2p_debug_print;
4577 p2p.p2p_scan = wpas_p2p_scan;
4578 p2p.send_action = wpas_send_action;
4579 p2p.send_action_done = wpas_send_action_done;
4580 p2p.go_neg_completed = wpas_go_neg_completed;
4581 p2p.go_neg_req_rx = wpas_go_neg_req_rx;
4582 p2p.dev_found = wpas_dev_found;
4583 p2p.dev_lost = wpas_dev_lost;
4584 p2p.find_stopped = wpas_find_stopped;
4585 p2p.start_listen = wpas_start_listen;
4586 p2p.stop_listen = wpas_stop_listen;
4587 p2p.send_probe_resp = wpas_send_probe_resp;
4588 p2p.sd_request = wpas_sd_request;
4589 p2p.sd_response = wpas_sd_response;
4590 p2p.prov_disc_req = wpas_prov_disc_req;
4591 p2p.prov_disc_resp = wpas_prov_disc_resp;
4592 p2p.prov_disc_fail = wpas_prov_disc_fail;
4593 p2p.invitation_process = wpas_invitation_process;
4594 p2p.invitation_received = wpas_invitation_received;
4595 p2p.invitation_result = wpas_invitation_result;
4596 p2p.get_noa = wpas_get_noa;
4597 p2p.go_connected = wpas_go_connected;
4598 p2p.presence_resp = wpas_presence_resp;
4599 p2p.is_concurrent_session_active = wpas_is_concurrent_session_active;
4600 p2p.is_p2p_in_progress = _wpas_p2p_in_progress;
4601 p2p.get_persistent_group = wpas_get_persistent_group;
4602 p2p.get_go_info = wpas_get_go_info;
4603 p2p.remove_stale_groups = wpas_remove_stale_groups;
4604 p2p.p2ps_prov_complete = wpas_p2ps_prov_complete;
4605 p2p.prov_disc_resp_cb = wpas_prov_disc_resp_cb;
4606 p2p.p2ps_group_capability = p2ps_group_capability;
4607 p2p.get_pref_freq_list = wpas_p2p_get_pref_freq_list;
4608
4609 os_memcpy(wpa_s->global->p2p_dev_addr, wpa_s->own_addr, ETH_ALEN);
4610 os_memcpy(p2p.dev_addr, wpa_s->global->p2p_dev_addr, ETH_ALEN);
4611 p2p.dev_name = wpa_s->conf->device_name;
4612 p2p.manufacturer = wpa_s->conf->manufacturer;
4613 p2p.model_name = wpa_s->conf->model_name;
4614 p2p.model_number = wpa_s->conf->model_number;
4615 p2p.serial_number = wpa_s->conf->serial_number;
4616 if (wpa_s->wps) {
4617 os_memcpy(p2p.uuid, wpa_s->wps->uuid, 16);
4618 p2p.config_methods = wpa_s->wps->config_methods;
4619 }
4620
4621 if (wpas_p2p_setup_channels(wpa_s, &p2p.channels, &p2p.cli_channels)) {
4622 wpa_printf(MSG_ERROR,
4623 "P2P: Failed to configure supported channel list");
4624 return -1;
4625 }
4626
4627 if (wpa_s->conf->p2p_listen_reg_class &&
4628 wpa_s->conf->p2p_listen_channel) {
4629 p2p.reg_class = wpa_s->conf->p2p_listen_reg_class;
4630 p2p.channel = wpa_s->conf->p2p_listen_channel;
4631 p2p.channel_forced = 1;
4632 } else {
4633 /*
4634 * Pick one of the social channels randomly as the listen
4635 * channel.
4636 */
4637 if (p2p_config_get_random_social(&p2p, &p2p.reg_class,
4638 &p2p.channel,
4639 &global->p2p_go_avoid_freq,
4640 &global->p2p_disallow_freq) !=
4641 0) {
4642 wpa_printf(MSG_INFO,
4643 "P2P: No social channels supported by the driver - do not enable P2P");
4644 return 0;
4645 }
4646 p2p.channel_forced = 0;
4647 }
4648 wpa_printf(MSG_DEBUG, "P2P: Own listen channel: %d:%d",
4649 p2p.reg_class, p2p.channel);
4650
4651 if (wpa_s->conf->p2p_oper_reg_class &&
4652 wpa_s->conf->p2p_oper_channel) {
4653 p2p.op_reg_class = wpa_s->conf->p2p_oper_reg_class;
4654 p2p.op_channel = wpa_s->conf->p2p_oper_channel;
4655 p2p.cfg_op_channel = 1;
4656 wpa_printf(MSG_DEBUG, "P2P: Configured operating channel: "
4657 "%d:%d", p2p.op_reg_class, p2p.op_channel);
4658
4659 } else {
4660 /*
4661 * Use random operation channel from 2.4 GHz band social
4662 * channels (1, 6, 11) or band 60 GHz social channel (2) if no
4663 * other preference is indicated.
4664 */
4665 if (p2p_config_get_random_social(&p2p, &p2p.op_reg_class,
4666 &p2p.op_channel, NULL,
4667 NULL) != 0) {
4668 wpa_printf(MSG_INFO,
4669 "P2P: Failed to select random social channel as operation channel");
4670 p2p.op_reg_class = 0;
4671 p2p.op_channel = 0;
4672 /* This will be overridden during group setup in
4673 * p2p_prepare_channel(), so allow setup to continue. */
4674 }
4675 p2p.cfg_op_channel = 0;
4676 wpa_printf(MSG_DEBUG, "P2P: Random operating channel: "
4677 "%d:%d", p2p.op_reg_class, p2p.op_channel);
4678 }
4679
4680 if (wpa_s->conf->p2p_pref_chan && wpa_s->conf->num_p2p_pref_chan) {
4681 p2p.pref_chan = wpa_s->conf->p2p_pref_chan;
4682 p2p.num_pref_chan = wpa_s->conf->num_p2p_pref_chan;
4683 }
4684
4685 if (wpa_s->conf->country[0] && wpa_s->conf->country[1]) {
4686 os_memcpy(p2p.country, wpa_s->conf->country, 2);
4687 p2p.country[2] = 0x04;
4688 } else
4689 os_memcpy(p2p.country, "XX\x04", 3);
4690
4691 os_memcpy(p2p.pri_dev_type, wpa_s->conf->device_type,
4692 WPS_DEV_TYPE_LEN);
4693
4694 p2p.num_sec_dev_types = wpa_s->conf->num_sec_device_types;
4695 os_memcpy(p2p.sec_dev_type, wpa_s->conf->sec_device_type,
4696 p2p.num_sec_dev_types * WPS_DEV_TYPE_LEN);
4697
4698 p2p.concurrent_operations = !!(wpa_s->drv_flags &
4699 WPA_DRIVER_FLAGS_P2P_CONCURRENT);
4700
4701 p2p.max_peers = 100;
4702
4703 if (wpa_s->conf->p2p_ssid_postfix) {
4704 p2p.ssid_postfix_len =
4705 os_strlen(wpa_s->conf->p2p_ssid_postfix);
4706 if (p2p.ssid_postfix_len > sizeof(p2p.ssid_postfix))
4707 p2p.ssid_postfix_len = sizeof(p2p.ssid_postfix);
4708 os_memcpy(p2p.ssid_postfix, wpa_s->conf->p2p_ssid_postfix,
4709 p2p.ssid_postfix_len);
4710 }
4711
4712 p2p.p2p_intra_bss = wpa_s->conf->p2p_intra_bss;
4713
4714 p2p.max_listen = wpa_s->max_remain_on_chan;
4715
4716 if (wpa_s->conf->p2p_passphrase_len >= 8 &&
4717 wpa_s->conf->p2p_passphrase_len <= 63)
4718 p2p.passphrase_len = wpa_s->conf->p2p_passphrase_len;
4719 else
4720 p2p.passphrase_len = 8;
4721
4722 global->p2p = p2p_init(&p2p);
4723 if (global->p2p == NULL)
4724 return -1;
4725 global->p2p_init_wpa_s = wpa_s;
4726
4727 for (i = 0; i < MAX_WPS_VENDOR_EXT; i++) {
4728 if (wpa_s->conf->wps_vendor_ext[i] == NULL)
4729 continue;
4730 p2p_add_wps_vendor_extension(
4731 global->p2p, wpa_s->conf->wps_vendor_ext[i]);
4732 }
4733
4734 p2p_set_no_go_freq(global->p2p, &wpa_s->conf->p2p_no_go_freq);
4735
4736 return 0;
4737 }
4738
4739
4740 /**
4741 * wpas_p2p_deinit - Deinitialize per-interface P2P data
4742 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
4743 *
4744 * This function deinitialize per-interface P2P data.
4745 */
4746 void wpas_p2p_deinit(struct wpa_supplicant *wpa_s)
4747 {
4748 if (wpa_s->driver && wpa_s->drv_priv)
4749 wpa_drv_probe_req_report(wpa_s, 0);
4750
4751 if (wpa_s->go_params) {
4752 /* Clear any stored provisioning info */
4753 p2p_clear_provisioning_info(
4754 wpa_s->global->p2p,
4755 wpa_s->go_params->peer_device_addr);
4756 }
4757
4758 os_free(wpa_s->go_params);
4759 wpa_s->go_params = NULL;
4760 eloop_cancel_timeout(wpas_p2p_psk_failure_removal, wpa_s, NULL);
4761 eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s, NULL);
4762 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
4763 wpa_s->global->p2p_long_listen = 0;
4764 eloop_cancel_timeout(wpas_p2p_long_listen_timeout, wpa_s, NULL);
4765 eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL);
4766 wpas_p2p_remove_pending_group_interface(wpa_s);
4767 eloop_cancel_timeout(wpas_p2p_group_freq_conflict, wpa_s, NULL);
4768 wpas_p2p_listen_work_done(wpa_s);
4769 if (wpa_s->p2p_send_action_work) {
4770 os_free(wpa_s->p2p_send_action_work->ctx);
4771 radio_work_done(wpa_s->p2p_send_action_work);
4772 wpa_s->p2p_send_action_work = NULL;
4773 }
4774 eloop_cancel_timeout(wpas_p2p_send_action_work_timeout, wpa_s, NULL);
4775
4776 wpabuf_free(wpa_s->p2p_oob_dev_pw);
4777 wpa_s->p2p_oob_dev_pw = NULL;
4778
4779 os_free(wpa_s->p2p_group_common_freqs);
4780 wpa_s->p2p_group_common_freqs = NULL;
4781 wpa_s->p2p_group_common_freqs_num = 0;
4782
4783 /* TODO: remove group interface from the driver if this wpa_s instance
4784 * is on top of a P2P group interface */
4785 }
4786
4787
4788 /**
4789 * wpas_p2p_deinit_global - Deinitialize global P2P module
4790 * @global: Pointer to global data from wpa_supplicant_init()
4791 *
4792 * This function deinitializes the global (per device) P2P module.
4793 */
4794 static void wpas_p2p_deinit_global(struct wpa_global *global)
4795 {
4796 struct wpa_supplicant *wpa_s, *tmp;
4797
4798 wpa_s = global->ifaces;
4799
4800 wpas_p2p_service_flush(global->p2p_init_wpa_s);
4801
4802 /* Remove remaining P2P group interfaces */
4803 while (wpa_s && wpa_s->p2p_group_interface != NOT_P2P_GROUP_INTERFACE)
4804 wpa_s = wpa_s->next;
4805 while (wpa_s) {
4806 tmp = global->ifaces;
4807 while (tmp &&
4808 (tmp == wpa_s ||
4809 tmp->p2p_group_interface == NOT_P2P_GROUP_INTERFACE)) {
4810 tmp = tmp->next;
4811 }
4812 if (tmp == NULL)
4813 break;
4814 /* Disconnect from the P2P group and deinit the interface */
4815 wpas_p2p_disconnect(tmp);
4816 }
4817
4818 /*
4819 * Deinit GO data on any possibly remaining interface (if main
4820 * interface is used as GO).
4821 */
4822 for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
4823 if (wpa_s->ap_iface)
4824 wpas_p2p_group_deinit(wpa_s);
4825 }
4826
4827 p2p_deinit(global->p2p);
4828 global->p2p = NULL;
4829 global->p2p_init_wpa_s = NULL;
4830 }
4831
4832
4833 static int wpas_p2p_create_iface(struct wpa_supplicant *wpa_s)
4834 {
4835 if (wpa_s->conf->p2p_no_group_iface)
4836 return 0; /* separate interface disabled per configuration */
4837 if (wpa_s->drv_flags &
4838 (WPA_DRIVER_FLAGS_P2P_DEDICATED_INTERFACE |
4839 WPA_DRIVER_FLAGS_P2P_MGMT_AND_NON_P2P))
4840 return 1; /* P2P group requires a new interface in every case
4841 */
4842 if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_CONCURRENT))
4843 return 0; /* driver does not support concurrent operations */
4844 if (wpa_s->global->ifaces->next)
4845 return 1; /* more that one interface already in use */
4846 if (wpa_s->wpa_state >= WPA_AUTHENTICATING)
4847 return 1; /* this interface is already in use */
4848 return 0;
4849 }
4850
4851
4852 static int wpas_p2p_start_go_neg(struct wpa_supplicant *wpa_s,
4853 const u8 *peer_addr,
4854 enum p2p_wps_method wps_method,
4855 int go_intent, const u8 *own_interface_addr,
4856 unsigned int force_freq, int persistent_group,
4857 struct wpa_ssid *ssid, unsigned int pref_freq)
4858 {
4859 if (persistent_group && wpa_s->conf->persistent_reconnect)
4860 persistent_group = 2;
4861
4862 /*
4863 * Increase GO config timeout if HT40 is used since it takes some time
4864 * to scan channels for coex purposes before the BSS can be started.
4865 */
4866 p2p_set_config_timeout(wpa_s->global->p2p,
4867 wpa_s->p2p_go_ht40 ? 255 : 100, 20);
4868
4869 return p2p_connect(wpa_s->global->p2p, peer_addr, wps_method,
4870 go_intent, own_interface_addr, force_freq,
4871 persistent_group, ssid ? ssid->ssid : NULL,
4872 ssid ? ssid->ssid_len : 0,
4873 wpa_s->p2p_pd_before_go_neg, pref_freq,
4874 wps_method == WPS_NFC ? wpa_s->p2p_oob_dev_pw_id :
4875 0);
4876 }
4877
4878
4879 static int wpas_p2p_auth_go_neg(struct wpa_supplicant *wpa_s,
4880 const u8 *peer_addr,
4881 enum p2p_wps_method wps_method,
4882 int go_intent, const u8 *own_interface_addr,
4883 unsigned int force_freq, int persistent_group,
4884 struct wpa_ssid *ssid, unsigned int pref_freq)
4885 {
4886 if (persistent_group && wpa_s->conf->persistent_reconnect)
4887 persistent_group = 2;
4888
4889 return p2p_authorize(wpa_s->global->p2p, peer_addr, wps_method,
4890 go_intent, own_interface_addr, force_freq,
4891 persistent_group, ssid ? ssid->ssid : NULL,
4892 ssid ? ssid->ssid_len : 0, pref_freq,
4893 wps_method == WPS_NFC ? wpa_s->p2p_oob_dev_pw_id :
4894 0);
4895 }
4896
4897
4898 static void wpas_p2p_check_join_scan_limit(struct wpa_supplicant *wpa_s)
4899 {
4900 wpa_s->p2p_join_scan_count++;
4901 wpa_printf(MSG_DEBUG, "P2P: Join scan attempt %d",
4902 wpa_s->p2p_join_scan_count);
4903 if (wpa_s->p2p_join_scan_count > P2P_MAX_JOIN_SCAN_ATTEMPTS) {
4904 wpa_printf(MSG_DEBUG, "P2P: Failed to find GO " MACSTR
4905 " for join operationg - stop join attempt",
4906 MAC2STR(wpa_s->pending_join_iface_addr));
4907 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
4908 if (wpa_s->p2p_auto_pd) {
4909 wpa_s->p2p_auto_pd = 0;
4910 wpa_msg_global(wpa_s, MSG_INFO,
4911 P2P_EVENT_PROV_DISC_FAILURE
4912 " p2p_dev_addr=" MACSTR " status=N/A",
4913 MAC2STR(wpa_s->pending_join_dev_addr));
4914 return;
4915 }
4916 wpa_msg_global(wpa_s->p2pdev, MSG_INFO,
4917 P2P_EVENT_GROUP_FORMATION_FAILURE);
4918 wpas_notify_p2p_group_formation_failure(wpa_s, "");
4919 }
4920 }
4921
4922
4923 static int wpas_check_freq_conflict(struct wpa_supplicant *wpa_s, int freq)
4924 {
4925 int res;
4926 unsigned int num, i;
4927 struct wpa_used_freq_data *freqs;
4928
4929 if (wpas_p2p_num_unused_channels(wpa_s) > 0) {
4930 /* Multiple channels are supported and not all are in use */
4931 return 0;
4932 }
4933
4934 freqs = os_calloc(wpa_s->num_multichan_concurrent,
4935 sizeof(struct wpa_used_freq_data));
4936 if (!freqs)
4937 return 1;
4938
4939 num = wpas_p2p_valid_oper_freqs(wpa_s, freqs,
4940 wpa_s->num_multichan_concurrent);
4941
4942 for (i = 0; i < num; i++) {
4943 if (freqs[i].freq == freq) {
4944 wpa_printf(MSG_DEBUG, "P2P: Frequency %d MHz in use by another virtual interface and can be used",
4945 freq);
4946 res = 0;
4947 goto exit_free;
4948 }
4949 }
4950
4951 wpa_printf(MSG_DEBUG, "P2P: No valid operating frequencies");
4952 res = 1;
4953
4954 exit_free:
4955 os_free(freqs);
4956 return res;
4957 }
4958
4959
4960 static int wpas_p2p_peer_go(struct wpa_supplicant *wpa_s,
4961 const u8 *peer_dev_addr)
4962 {
4963 struct wpa_bss *bss;
4964 int updated;
4965
4966 bss = wpa_bss_get_p2p_dev_addr(wpa_s, peer_dev_addr);
4967 if (bss == NULL)
4968 return -1;
4969 if (bss->last_update_idx < wpa_s->bss_update_idx) {
4970 wpa_printf(MSG_DEBUG, "P2P: Peer BSS entry not updated in the "
4971 "last scan");
4972 return 0;
4973 }
4974
4975 updated = os_reltime_before(&wpa_s->p2p_auto_started,
4976 &bss->last_update);
4977 wpa_printf(MSG_DEBUG, "P2P: Current BSS entry for peer updated at "
4978 "%ld.%06ld (%supdated in last scan)",
4979 bss->last_update.sec, bss->last_update.usec,
4980 updated ? "": "not ");
4981
4982 return updated;
4983 }
4984
4985
4986 static void wpas_p2p_scan_res_join(struct wpa_supplicant *wpa_s,
4987 struct wpa_scan_results *scan_res)
4988 {
4989 struct wpa_bss *bss = NULL;
4990 int freq;
4991 u8 iface_addr[ETH_ALEN];
4992
4993 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
4994
4995 if (wpa_s->global->p2p_disabled)
4996 return;
4997
4998 wpa_printf(MSG_DEBUG, "P2P: Scan results received (%d BSS) for %sjoin",
4999 scan_res ? (int) scan_res->num : -1,
5000 wpa_s->p2p_auto_join ? "auto_" : "");
5001
5002 if (scan_res)
5003 wpas_p2p_scan_res_handler(wpa_s, scan_res);
5004
5005 if (wpa_s->p2p_auto_pd) {
5006 int join = wpas_p2p_peer_go(wpa_s,
5007 wpa_s->pending_join_dev_addr);
5008 if (join == 0 &&
5009 wpa_s->auto_pd_scan_retry < P2P_AUTO_PD_SCAN_ATTEMPTS) {
5010 wpa_s->auto_pd_scan_retry++;
5011 bss = wpa_bss_get_bssid_latest(
5012 wpa_s, wpa_s->pending_join_dev_addr);
5013 if (bss) {
5014 freq = bss->freq;
5015 wpa_printf(MSG_DEBUG, "P2P: Scan retry %d for "
5016 "the peer " MACSTR " at %d MHz",
5017 wpa_s->auto_pd_scan_retry,
5018 MAC2STR(wpa_s->
5019 pending_join_dev_addr),
5020 freq);
5021 wpas_p2p_join_scan_req(wpa_s, freq, NULL, 0);
5022 return;
5023 }
5024 }
5025
5026 if (join < 0)
5027 join = 0;
5028
5029 wpa_s->p2p_auto_pd = 0;
5030 wpa_s->pending_pd_use = join ? AUTO_PD_JOIN : AUTO_PD_GO_NEG;
5031 wpa_printf(MSG_DEBUG, "P2P: Auto PD with " MACSTR " join=%d",
5032 MAC2STR(wpa_s->pending_join_dev_addr), join);
5033 if (p2p_prov_disc_req(wpa_s->global->p2p,
5034 wpa_s->pending_join_dev_addr, NULL,
5035 wpa_s->pending_pd_config_methods, join,
5036 0, wpa_s->user_initiated_pd) < 0) {
5037 wpa_s->p2p_auto_pd = 0;
5038 wpa_msg_global(wpa_s, MSG_INFO,
5039 P2P_EVENT_PROV_DISC_FAILURE
5040 " p2p_dev_addr=" MACSTR " status=N/A",
5041 MAC2STR(wpa_s->pending_join_dev_addr));
5042 }
5043 return;
5044 }
5045
5046 if (wpa_s->p2p_auto_join) {
5047 int join = wpas_p2p_peer_go(wpa_s,
5048 wpa_s->pending_join_dev_addr);
5049 if (join < 0) {
5050 wpa_printf(MSG_DEBUG, "P2P: Peer was not found to be "
5051 "running a GO -> use GO Negotiation");
5052 wpa_msg_global(wpa_s->p2pdev, MSG_INFO,
5053 P2P_EVENT_FALLBACK_TO_GO_NEG
5054 "reason=peer-not-running-GO");
5055 wpas_p2p_connect(wpa_s, wpa_s->pending_join_dev_addr,
5056 wpa_s->p2p_pin, wpa_s->p2p_wps_method,
5057 wpa_s->p2p_persistent_group, 0, 0, 0,
5058 wpa_s->p2p_go_intent,
5059 wpa_s->p2p_connect_freq,
5060 wpa_s->p2p_go_vht_center_freq2,
5061 wpa_s->p2p_persistent_id,
5062 wpa_s->p2p_pd_before_go_neg,
5063 wpa_s->p2p_go_ht40,
5064 wpa_s->p2p_go_vht,
5065 wpa_s->p2p_go_max_oper_chwidth,
5066 wpa_s->p2p_go_he,
5067 wpa_s->p2p_go_edmg,
5068 NULL, 0);
5069 return;
5070 }
5071
5072 wpa_printf(MSG_DEBUG, "P2P: Peer was found running GO%s -> "
5073 "try to join the group", join ? "" :
5074 " in older scan");
5075 if (!join) {
5076 wpa_msg_global(wpa_s->p2pdev, MSG_INFO,
5077 P2P_EVENT_FALLBACK_TO_GO_NEG_ENABLED);
5078 wpa_s->p2p_fallback_to_go_neg = 1;
5079 }
5080 }
5081
5082 freq = p2p_get_oper_freq(wpa_s->global->p2p,
5083 wpa_s->pending_join_iface_addr);
5084 if (freq < 0 &&
5085 p2p_get_interface_addr(wpa_s->global->p2p,
5086 wpa_s->pending_join_dev_addr,
5087 iface_addr) == 0 &&
5088 os_memcmp(iface_addr, wpa_s->pending_join_dev_addr, ETH_ALEN) != 0
5089 && !wpa_bss_get_bssid(wpa_s, wpa_s->pending_join_iface_addr)) {
5090 wpa_printf(MSG_DEBUG, "P2P: Overwrite pending interface "
5091 "address for join from " MACSTR " to " MACSTR
5092 " based on newly discovered P2P peer entry",
5093 MAC2STR(wpa_s->pending_join_iface_addr),
5094 MAC2STR(iface_addr));
5095 os_memcpy(wpa_s->pending_join_iface_addr, iface_addr,
5096 ETH_ALEN);
5097
5098 freq = p2p_get_oper_freq(wpa_s->global->p2p,
5099 wpa_s->pending_join_iface_addr);
5100 }
5101 if (freq >= 0) {
5102 wpa_printf(MSG_DEBUG, "P2P: Target GO operating frequency "
5103 "from P2P peer table: %d MHz", freq);
5104 }
5105 if (wpa_s->p2p_join_ssid_len) {
5106 wpa_printf(MSG_DEBUG, "P2P: Trying to find target GO BSS entry based on BSSID "
5107 MACSTR " and SSID %s",
5108 MAC2STR(wpa_s->pending_join_iface_addr),
5109 wpa_ssid_txt(wpa_s->p2p_join_ssid,
5110 wpa_s->p2p_join_ssid_len));
5111 bss = wpa_bss_get(wpa_s, wpa_s->pending_join_iface_addr,
5112 wpa_s->p2p_join_ssid,
5113 wpa_s->p2p_join_ssid_len);
5114 } else if (!bss) {
5115 wpa_printf(MSG_DEBUG, "P2P: Trying to find target GO BSS entry based on BSSID "
5116 MACSTR, MAC2STR(wpa_s->pending_join_iface_addr));
5117 bss = wpa_bss_get_bssid_latest(wpa_s,
5118 wpa_s->pending_join_iface_addr);
5119 }
5120 if (bss) {
5121 u8 dev_addr[ETH_ALEN];
5122
5123 freq = bss->freq;
5124 wpa_printf(MSG_DEBUG, "P2P: Target GO operating frequency "
5125 "from BSS table: %d MHz (SSID %s)", freq,
5126 wpa_ssid_txt(bss->ssid, bss->ssid_len));
5127 if (p2p_parse_dev_addr((const u8 *) (bss + 1), bss->ie_len,
5128 dev_addr) == 0 &&
5129 os_memcmp(wpa_s->pending_join_dev_addr,
5130 wpa_s->pending_join_iface_addr, ETH_ALEN) == 0 &&
5131 os_memcmp(dev_addr, wpa_s->pending_join_dev_addr,
5132 ETH_ALEN) != 0) {
5133 wpa_printf(MSG_DEBUG,
5134 "P2P: Update target GO device address based on BSS entry: " MACSTR " (was " MACSTR ")",
5135 MAC2STR(dev_addr),
5136 MAC2STR(wpa_s->pending_join_dev_addr));
5137 os_memcpy(wpa_s->pending_join_dev_addr, dev_addr,
5138 ETH_ALEN);
5139 }
5140 }
5141 if (freq > 0) {
5142 u16 method;
5143
5144 if (wpas_check_freq_conflict(wpa_s, freq) > 0) {
5145 wpa_msg_global(wpa_s->p2pdev, MSG_INFO,
5146 P2P_EVENT_GROUP_FORMATION_FAILURE
5147 "reason=FREQ_CONFLICT");
5148 wpas_notify_p2p_group_formation_failure(
5149 wpa_s, "FREQ_CONFLICT");
5150 return;
5151 }
5152
5153 wpa_printf(MSG_DEBUG, "P2P: Send Provision Discovery Request "
5154 "prior to joining an existing group (GO " MACSTR
5155 " freq=%u MHz)",
5156 MAC2STR(wpa_s->pending_join_dev_addr), freq);
5157 wpa_s->pending_pd_before_join = 1;
5158
5159 switch (wpa_s->pending_join_wps_method) {
5160 case WPS_PIN_DISPLAY:
5161 method = WPS_CONFIG_KEYPAD;
5162 break;
5163 case WPS_PIN_KEYPAD:
5164 method = WPS_CONFIG_DISPLAY;
5165 break;
5166 case WPS_PBC:
5167 method = WPS_CONFIG_PUSHBUTTON;
5168 break;
5169 case WPS_P2PS:
5170 method = WPS_CONFIG_P2PS;
5171 break;
5172 default:
5173 method = 0;
5174 break;
5175 }
5176
5177 if ((p2p_get_provisioning_info(wpa_s->global->p2p,
5178 wpa_s->pending_join_dev_addr) ==
5179 method)) {
5180 /*
5181 * We have already performed provision discovery for
5182 * joining the group. Proceed directly to join
5183 * operation without duplicated provision discovery. */
5184 wpa_printf(MSG_DEBUG, "P2P: Provision discovery "
5185 "with " MACSTR " already done - proceed to "
5186 "join",
5187 MAC2STR(wpa_s->pending_join_dev_addr));
5188 wpa_s->pending_pd_before_join = 0;
5189 goto start;
5190 }
5191
5192 if (p2p_prov_disc_req(wpa_s->global->p2p,
5193 wpa_s->pending_join_dev_addr,
5194 NULL, method, 1,
5195 freq, wpa_s->user_initiated_pd) < 0) {
5196 wpa_printf(MSG_DEBUG, "P2P: Failed to send Provision "
5197 "Discovery Request before joining an "
5198 "existing group");
5199 wpa_s->pending_pd_before_join = 0;
5200 goto start;
5201 }
5202 return;
5203 }
5204
5205 wpa_printf(MSG_DEBUG, "P2P: Failed to find BSS/GO - try again later");
5206 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
5207 eloop_register_timeout(1, 0, wpas_p2p_join_scan, wpa_s, NULL);
5208 wpas_p2p_check_join_scan_limit(wpa_s);
5209 return;
5210
5211 start:
5212 /* Start join operation immediately */
5213 wpas_p2p_join_start(wpa_s, 0, wpa_s->p2p_join_ssid,
5214 wpa_s->p2p_join_ssid_len);
5215 }
5216
5217
5218 static void wpas_p2p_join_scan_req(struct wpa_supplicant *wpa_s, int freq,
5219 const u8 *ssid, size_t ssid_len)
5220 {
5221 int ret;
5222 struct wpa_driver_scan_params params;
5223 struct wpabuf *wps_ie, *ies;
5224 size_t ielen;
5225 int freqs[2] = { 0, 0 };
5226 unsigned int bands;
5227
5228 os_memset(&params, 0, sizeof(params));
5229
5230 /* P2P Wildcard SSID */
5231 params.num_ssids = 1;
5232 if (ssid && ssid_len) {
5233 params.ssids[0].ssid = ssid;
5234 params.ssids[0].ssid_len = ssid_len;
5235 os_memcpy(wpa_s->p2p_join_ssid, ssid, ssid_len);
5236 wpa_s->p2p_join_ssid_len = ssid_len;
5237 } else {
5238 params.ssids[0].ssid = (u8 *) P2P_WILDCARD_SSID;
5239 params.ssids[0].ssid_len = P2P_WILDCARD_SSID_LEN;
5240 wpa_s->p2p_join_ssid_len = 0;
5241 }
5242
5243 wpa_s->wps->dev.p2p = 1;
5244 wps_ie = wps_build_probe_req_ie(DEV_PW_DEFAULT, &wpa_s->wps->dev,
5245 wpa_s->wps->uuid, WPS_REQ_ENROLLEE, 0,
5246 NULL);
5247 if (wps_ie == NULL) {
5248 wpas_p2p_scan_res_join(wpa_s, NULL);
5249 return;
5250 }
5251
5252 if (!freq) {
5253 int oper_freq;
5254 /*
5255 * If freq is not provided, check the operating freq of the GO
5256 * and use a single channel scan on if possible.
5257 */
5258 oper_freq = p2p_get_oper_freq(wpa_s->global->p2p,
5259 wpa_s->pending_join_iface_addr);
5260 if (oper_freq > 0)
5261 freq = oper_freq;
5262 }
5263 if (freq > 0) {
5264 freqs[0] = freq;
5265 params.freqs = freqs;
5266 }
5267
5268 ielen = p2p_scan_ie_buf_len(wpa_s->global->p2p);
5269 ies = wpabuf_alloc(wpabuf_len(wps_ie) + ielen);
5270 if (ies == NULL) {
5271 wpabuf_free(wps_ie);
5272 wpas_p2p_scan_res_join(wpa_s, NULL);
5273 return;
5274 }
5275 wpabuf_put_buf(ies, wps_ie);
5276 wpabuf_free(wps_ie);
5277
5278 bands = wpas_get_bands(wpa_s, freqs);
5279 p2p_scan_ie(wpa_s->global->p2p, ies, NULL, bands);
5280
5281 params.p2p_probe = 1;
5282 params.extra_ies = wpabuf_head(ies);
5283 params.extra_ies_len = wpabuf_len(ies);
5284
5285 if (wpa_s->clear_driver_scan_cache) {
5286 wpa_printf(MSG_DEBUG,
5287 "Request driver to clear scan cache due to local BSS flush");
5288 params.only_new_results = 1;
5289 }
5290
5291 /*
5292 * Run a scan to update BSS table and start Provision Discovery once
5293 * the new scan results become available.
5294 */
5295 ret = wpa_drv_scan(wpa_s, &params);
5296 if (!ret) {
5297 os_get_reltime(&wpa_s->scan_trigger_time);
5298 wpa_s->scan_res_handler = wpas_p2p_scan_res_join;
5299 wpa_s->own_scan_requested = 1;
5300 wpa_s->clear_driver_scan_cache = 0;
5301 }
5302
5303 wpabuf_free(ies);
5304
5305 if (ret) {
5306 wpa_printf(MSG_DEBUG, "P2P: Failed to start scan for join - "
5307 "try again later");
5308 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
5309 eloop_register_timeout(1, 0, wpas_p2p_join_scan, wpa_s, NULL);
5310 wpas_p2p_check_join_scan_limit(wpa_s);
5311 }
5312 }
5313
5314
5315 static void wpas_p2p_join_scan(void *eloop_ctx, void *timeout_ctx)
5316 {
5317 struct wpa_supplicant *wpa_s = eloop_ctx;
5318 wpas_p2p_join_scan_req(wpa_s, 0, NULL, 0);
5319 }
5320
5321
5322 static int wpas_p2p_join(struct wpa_supplicant *wpa_s, const u8 *iface_addr,
5323 const u8 *dev_addr, enum p2p_wps_method wps_method,
5324 int auto_join, int op_freq,
5325 const u8 *ssid, size_t ssid_len)
5326 {
5327 wpa_printf(MSG_DEBUG, "P2P: Request to join existing group (iface "
5328 MACSTR " dev " MACSTR " op_freq=%d)%s",
5329 MAC2STR(iface_addr), MAC2STR(dev_addr), op_freq,
5330 auto_join ? " (auto_join)" : "");
5331 if (ssid && ssid_len) {
5332 wpa_printf(MSG_DEBUG, "P2P: Group SSID specified: %s",
5333 wpa_ssid_txt(ssid, ssid_len));
5334 }
5335
5336 wpa_s->p2p_auto_pd = 0;
5337 wpa_s->p2p_auto_join = !!auto_join;
5338 os_memcpy(wpa_s->pending_join_iface_addr, iface_addr, ETH_ALEN);
5339 os_memcpy(wpa_s->pending_join_dev_addr, dev_addr, ETH_ALEN);
5340 wpa_s->pending_join_wps_method = wps_method;
5341
5342 /* Make sure we are not running find during connection establishment */
5343 wpas_p2p_stop_find(wpa_s);
5344
5345 wpa_s->p2p_join_scan_count = 0;
5346 wpas_p2p_join_scan_req(wpa_s, op_freq, ssid, ssid_len);
5347 return 0;
5348 }
5349
5350
5351 static int wpas_p2p_join_start(struct wpa_supplicant *wpa_s, int freq,
5352 const u8 *ssid, size_t ssid_len)
5353 {
5354 struct wpa_supplicant *group;
5355 struct p2p_go_neg_results res;
5356 struct wpa_bss *bss;
5357
5358 group = wpas_p2p_get_group_iface(wpa_s, 0, 0);
5359 if (group == NULL)
5360 return -1;
5361 if (group != wpa_s) {
5362 os_memcpy(group->p2p_pin, wpa_s->p2p_pin,
5363 sizeof(group->p2p_pin));
5364 group->p2p_wps_method = wpa_s->p2p_wps_method;
5365 }
5366
5367 /*
5368 * Need to mark the current interface for p2p_group_formation
5369 * when a separate group interface is not used. This is needed
5370 * to allow p2p_cancel stop a pending p2p_connect-join.
5371 * wpas_p2p_init_group_interface() addresses this for the case
5372 * where a separate group interface is used.
5373 */
5374 if (group == wpa_s->parent)
5375 wpa_s->global->p2p_group_formation = group;
5376
5377 group->p2p_in_provisioning = 1;
5378 group->p2p_fallback_to_go_neg = wpa_s->p2p_fallback_to_go_neg;
5379
5380 os_memset(&res, 0, sizeof(res));
5381 os_memcpy(res.peer_device_addr, wpa_s->pending_join_dev_addr, ETH_ALEN);
5382 os_memcpy(res.peer_interface_addr, wpa_s->pending_join_iface_addr,
5383 ETH_ALEN);
5384 res.wps_method = wpa_s->pending_join_wps_method;
5385 if (freq && ssid && ssid_len) {
5386 res.freq = freq;
5387 res.ssid_len = ssid_len;
5388 os_memcpy(res.ssid, ssid, ssid_len);
5389 } else {
5390 if (ssid && ssid_len) {
5391 bss = wpa_bss_get(wpa_s, wpa_s->pending_join_iface_addr,
5392 ssid, ssid_len);
5393 } else {
5394 bss = wpa_bss_get_bssid_latest(
5395 wpa_s, wpa_s->pending_join_iface_addr);
5396 }
5397 if (bss) {
5398 res.freq = bss->freq;
5399 res.ssid_len = bss->ssid_len;
5400 os_memcpy(res.ssid, bss->ssid, bss->ssid_len);
5401 wpa_printf(MSG_DEBUG, "P2P: Join target GO operating frequency from BSS table: %d MHz (SSID %s)",
5402 bss->freq,
5403 wpa_ssid_txt(bss->ssid, bss->ssid_len));
5404 } else if (ssid && ssid_len) {
5405 res.ssid_len = ssid_len;
5406 os_memcpy(res.ssid, ssid, ssid_len);
5407 wpa_printf(MSG_DEBUG, "P2P: Join target GO (SSID %s)",
5408 wpa_ssid_txt(ssid, ssid_len));
5409 }
5410 }
5411
5412 if (wpa_s->off_channel_freq || wpa_s->roc_waiting_drv_freq) {
5413 wpa_printf(MSG_DEBUG, "P2P: Cancel remain-on-channel prior to "
5414 "starting client");
5415 wpa_drv_cancel_remain_on_channel(wpa_s);
5416 wpa_s->off_channel_freq = 0;
5417 wpa_s->roc_waiting_drv_freq = 0;
5418 }
5419 wpas_start_wps_enrollee(group, &res);
5420
5421 /*
5422 * Allow a longer timeout for join-a-running-group than normal 15
5423 * second group formation timeout since the GO may not have authorized
5424 * our connection yet.
5425 */
5426 eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s, NULL);
5427 eloop_register_timeout(60, 0, wpas_p2p_group_formation_timeout,
5428 wpa_s, NULL);
5429
5430 return 0;
5431 }
5432
5433
5434 static int wpas_p2p_setup_freqs(struct wpa_supplicant *wpa_s, int freq,
5435 int *force_freq, int *pref_freq, int go,
5436 unsigned int *pref_freq_list,
5437 unsigned int *num_pref_freq)
5438 {
5439 struct wpa_used_freq_data *freqs;
5440 int res, best_freq, num_unused;
5441 unsigned int freq_in_use = 0, num, i, max_pref_freq;
5442
5443 max_pref_freq = *num_pref_freq;
5444 *num_pref_freq = 0;
5445
5446 freqs = os_calloc(wpa_s->num_multichan_concurrent,
5447 sizeof(struct wpa_used_freq_data));
5448 if (!freqs)
5449 return -1;
5450
5451 num = wpas_p2p_valid_oper_freqs(wpa_s, freqs,
5452 wpa_s->num_multichan_concurrent);
5453
5454 /*
5455 * It is possible that the total number of used frequencies is bigger
5456 * than the number of frequencies used for P2P, so get the system wide
5457 * number of unused frequencies.
5458 */
5459 num_unused = wpas_p2p_num_unused_channels(wpa_s);
5460
5461 wpa_printf(MSG_DEBUG,
5462 "P2P: Setup freqs: freq=%d num_MCC=%d shared_freqs=%u num_unused=%d",
5463 freq, wpa_s->num_multichan_concurrent, num, num_unused);
5464
5465 if (freq > 0) {
5466 int ret;
5467 if (go)
5468 ret = p2p_supported_freq(wpa_s->global->p2p, freq);
5469 else
5470 ret = p2p_supported_freq_cli(wpa_s->global->p2p, freq);
5471 if (!ret) {
5472 if ((wpa_s->drv_flags & WPA_DRIVER_FLAGS_DFS_OFFLOAD) &&
5473 ieee80211_is_dfs(freq, wpa_s->hw.modes,
5474 wpa_s->hw.num_modes)) {
5475 /*
5476 * If freq is a DFS channel and DFS is offloaded
5477 * to the driver, allow P2P GO to use it.
5478 */
5479 wpa_printf(MSG_DEBUG,
5480 "P2P: The forced channel for GO (%u MHz) is DFS, and DFS is offloaded to the driver",
5481 freq);
5482 } else {
5483 wpa_printf(MSG_DEBUG,
5484 "P2P: The forced channel (%u MHz) is not supported for P2P uses",
5485 freq);
5486 res = -3;
5487 goto exit_free;
5488 }
5489 }
5490
5491 for (i = 0; i < num; i++) {
5492 if (freqs[i].freq == freq)
5493 freq_in_use = 1;
5494 }
5495
5496 if (num_unused <= 0 && !freq_in_use) {
5497 wpa_printf(MSG_DEBUG, "P2P: Cannot start P2P group on %u MHz as there are no available channels",
5498 freq);
5499 res = -2;
5500 goto exit_free;
5501 }
5502 wpa_printf(MSG_DEBUG, "P2P: Trying to force us to use the "
5503 "requested channel (%u MHz)", freq);
5504 *force_freq = freq;
5505 goto exit_ok;
5506 }
5507
5508 best_freq = wpas_p2p_pick_best_used_freq(wpa_s, freqs, num);
5509
5510 if (!wpa_s->conf->num_p2p_pref_chan && *pref_freq == 0) {
5511 enum wpa_driver_if_type iface_type;
5512
5513 if (go)
5514 iface_type = WPA_IF_P2P_GO;
5515 else
5516 iface_type = WPA_IF_P2P_CLIENT;
5517
5518 wpa_printf(MSG_DEBUG, "P2P: best_freq=%d, go=%d",
5519 best_freq, go);
5520
5521 res = wpa_drv_get_pref_freq_list(wpa_s, iface_type,
5522 &max_pref_freq,
5523 pref_freq_list);
5524 if (!res && max_pref_freq > 0) {
5525 *num_pref_freq = max_pref_freq;
5526 i = 0;
5527 while (i < *num_pref_freq &&
5528 (!p2p_supported_freq(wpa_s->global->p2p,
5529 pref_freq_list[i]) ||
5530 wpas_p2p_disallowed_freq(wpa_s->global,
5531 pref_freq_list[i]))) {
5532 wpa_printf(MSG_DEBUG,
5533 "P2P: preferred_freq_list[%d]=%d is disallowed",
5534 i, pref_freq_list[i]);
5535 i++;
5536 }
5537 if (i != *num_pref_freq) {
5538 best_freq = pref_freq_list[i];
5539 wpa_printf(MSG_DEBUG,
5540 "P2P: Using preferred_freq_list[%d]=%d",
5541 i, best_freq);
5542 } else {
5543 wpa_printf(MSG_DEBUG,
5544 "P2P: All driver preferred frequencies are disallowed for P2P use");
5545 *num_pref_freq = 0;
5546 }
5547 } else {
5548 wpa_printf(MSG_DEBUG,
5549 "P2P: No preferred frequency list available");
5550 }
5551 }
5552
5553 /* We have a candidate frequency to use */
5554 if (best_freq > 0) {
5555 if (*pref_freq == 0 && num_unused > 0) {
5556 wpa_printf(MSG_DEBUG, "P2P: Try to prefer a frequency (%u MHz) we are already using",
5557 best_freq);
5558 *pref_freq = best_freq;
5559 } else {
5560 wpa_printf(MSG_DEBUG, "P2P: Try to force us to use frequency (%u MHz) which is already in use",
5561 best_freq);
5562 *force_freq = best_freq;
5563 }
5564 } else if (num_unused > 0) {
5565 wpa_printf(MSG_DEBUG,
5566 "P2P: Current operating channels are not available for P2P. Try to use another channel");
5567 *force_freq = 0;
5568 } else {
5569 wpa_printf(MSG_DEBUG,
5570 "P2P: All channels are in use and none of them are P2P enabled. Cannot start P2P group");
5571 res = -2;
5572 goto exit_free;
5573 }
5574
5575 exit_ok:
5576 res = 0;
5577 exit_free:
5578 os_free(freqs);
5579 return res;
5580 }
5581
5582
5583 /**
5584 * wpas_p2p_connect - Request P2P Group Formation to be started
5585 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
5586 * @peer_addr: Address of the peer P2P Device
5587 * @pin: PIN to use during provisioning or %NULL to indicate PBC mode
5588 * @persistent_group: Whether to create a persistent group
5589 * @auto_join: Whether to select join vs. GO Negotiation automatically
5590 * @join: Whether to join an existing group (as a client) instead of starting
5591 * Group Owner negotiation; @peer_addr is BSSID in that case
5592 * @auth: Whether to only authorize the connection instead of doing that and
5593 * initiating Group Owner negotiation
5594 * @go_intent: GO Intent or -1 to use default
5595 * @freq: Frequency for the group or 0 for auto-selection
5596 * @freq2: Center frequency of segment 1 for the GO operating in VHT 80P80 mode
5597 * @persistent_id: Persistent group credentials to use for forcing GO
5598 * parameters or -1 to generate new values (SSID/passphrase)
5599 * @pd: Whether to send Provision Discovery prior to GO Negotiation as an
5600 * interoperability workaround when initiating group formation
5601 * @ht40: Start GO with 40 MHz channel width
5602 * @vht: Start GO with VHT support
5603 * @vht_chwidth: Channel width supported by GO operating with VHT support
5604 * (CHANWIDTH_*).
5605 * @group_ssid: Specific Group SSID for join or %NULL if not set
5606 * @group_ssid_len: Length of @group_ssid in octets
5607 * Returns: 0 or new PIN (if pin was %NULL) on success, -1 on unspecified
5608 * failure, -2 on failure due to channel not currently available,
5609 * -3 if forced channel is not supported
5610 */
5611 int wpas_p2p_connect(struct wpa_supplicant *wpa_s, const u8 *peer_addr,
5612 const char *pin, enum p2p_wps_method wps_method,
5613 int persistent_group, int auto_join, int join, int auth,
5614 int go_intent, int freq, unsigned int vht_center_freq2,
5615 int persistent_id, int pd, int ht40, int vht,
5616 unsigned int vht_chwidth, int he, int edmg,
5617 const u8 *group_ssid, size_t group_ssid_len)
5618 {
5619 int force_freq = 0, pref_freq = 0;
5620 int ret = 0, res;
5621 enum wpa_driver_if_type iftype;
5622 const u8 *if_addr;
5623 struct wpa_ssid *ssid = NULL;
5624 unsigned int pref_freq_list[P2P_MAX_PREF_CHANNELS], size;
5625
5626 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
5627 return -1;
5628
5629 if (persistent_id >= 0) {
5630 ssid = wpa_config_get_network(wpa_s->conf, persistent_id);
5631 if (ssid == NULL || ssid->disabled != 2 ||
5632 ssid->mode != WPAS_MODE_P2P_GO)
5633 return -1;
5634 }
5635
5636 os_free(wpa_s->global->add_psk);
5637 wpa_s->global->add_psk = NULL;
5638
5639 wpa_s->global->p2p_fail_on_wps_complete = 0;
5640 wpa_s->global->pending_p2ps_group = 0;
5641 wpa_s->global->pending_p2ps_group_freq = 0;
5642 wpa_s->p2ps_method_config_any = 0;
5643
5644 if (go_intent < 0)
5645 go_intent = wpa_s->conf->p2p_go_intent;
5646
5647 if (!auth)
5648 wpa_s->global->p2p_long_listen = 0;
5649
5650 wpa_s->p2p_wps_method = wps_method;
5651 wpa_s->p2p_persistent_group = !!persistent_group;
5652 wpa_s->p2p_persistent_id = persistent_id;
5653 wpa_s->p2p_go_intent = go_intent;
5654 wpa_s->p2p_connect_freq = freq;
5655 wpa_s->p2p_fallback_to_go_neg = 0;
5656 wpa_s->p2p_pd_before_go_neg = !!pd;
5657 wpa_s->p2p_go_ht40 = !!ht40;
5658 wpa_s->p2p_go_vht = !!vht;
5659 wpa_s->p2p_go_vht_center_freq2 = vht_center_freq2;
5660 wpa_s->p2p_go_max_oper_chwidth = vht_chwidth;
5661 wpa_s->p2p_go_he = !!he;
5662 wpa_s->p2p_go_edmg = !!edmg;
5663
5664 if (pin)
5665 os_strlcpy(wpa_s->p2p_pin, pin, sizeof(wpa_s->p2p_pin));
5666 else if (wps_method == WPS_PIN_DISPLAY) {
5667 if (wps_generate_pin((unsigned int *) &ret) < 0)
5668 return -1;
5669 res = os_snprintf(wpa_s->p2p_pin, sizeof(wpa_s->p2p_pin),
5670 "%08d", ret);
5671 if (os_snprintf_error(sizeof(wpa_s->p2p_pin), res))
5672 wpa_s->p2p_pin[sizeof(wpa_s->p2p_pin) - 1] = '\0';
5673 wpa_printf(MSG_DEBUG, "P2P: Randomly generated PIN: %s",
5674 wpa_s->p2p_pin);
5675 } else if (wps_method == WPS_P2PS) {
5676 /* Force the P2Ps default PIN to be used */
5677 os_strlcpy(wpa_s->p2p_pin, "12345670", sizeof(wpa_s->p2p_pin));
5678 } else
5679 wpa_s->p2p_pin[0] = '\0';
5680
5681 if (join || auto_join) {
5682 u8 iface_addr[ETH_ALEN], dev_addr[ETH_ALEN];
5683 if (auth) {
5684 wpa_printf(MSG_DEBUG, "P2P: Authorize invitation to "
5685 "connect a running group from " MACSTR,
5686 MAC2STR(peer_addr));
5687 os_memcpy(wpa_s->p2p_auth_invite, peer_addr, ETH_ALEN);
5688 return ret;
5689 }
5690 os_memcpy(dev_addr, peer_addr, ETH_ALEN);
5691 if (p2p_get_interface_addr(wpa_s->global->p2p, peer_addr,
5692 iface_addr) < 0) {
5693 os_memcpy(iface_addr, peer_addr, ETH_ALEN);
5694 p2p_get_dev_addr(wpa_s->global->p2p, peer_addr,
5695 dev_addr);
5696 }
5697 if (auto_join) {
5698 os_get_reltime(&wpa_s->p2p_auto_started);
5699 wpa_printf(MSG_DEBUG, "P2P: Auto join started at "
5700 "%ld.%06ld",
5701 wpa_s->p2p_auto_started.sec,
5702 wpa_s->p2p_auto_started.usec);
5703 }
5704 wpa_s->user_initiated_pd = 1;
5705 if (wpas_p2p_join(wpa_s, iface_addr, dev_addr, wps_method,
5706 auto_join, freq,
5707 group_ssid, group_ssid_len) < 0)
5708 return -1;
5709 return ret;
5710 }
5711
5712 size = P2P_MAX_PREF_CHANNELS;
5713 res = wpas_p2p_setup_freqs(wpa_s, freq, &force_freq, &pref_freq,
5714 go_intent == 15, pref_freq_list, &size);
5715 if (res)
5716 return res;
5717 wpas_p2p_set_own_freq_preference(wpa_s,
5718 force_freq ? force_freq : pref_freq);
5719
5720 p2p_set_own_pref_freq_list(wpa_s->global->p2p, pref_freq_list, size);
5721
5722 wpa_s->create_p2p_iface = wpas_p2p_create_iface(wpa_s);
5723
5724 if (wpa_s->create_p2p_iface) {
5725 /* Prepare to add a new interface for the group */
5726 iftype = WPA_IF_P2P_GROUP;
5727 if (go_intent == 15)
5728 iftype = WPA_IF_P2P_GO;
5729 if (wpas_p2p_add_group_interface(wpa_s, iftype) < 0) {
5730 wpa_printf(MSG_ERROR, "P2P: Failed to allocate a new "
5731 "interface for the group");
5732 return -1;
5733 }
5734
5735 if_addr = wpa_s->pending_interface_addr;
5736 } else {
5737 if (wpa_s->p2p_mgmt)
5738 if_addr = wpa_s->parent->own_addr;
5739 else
5740 if_addr = wpa_s->own_addr;
5741 os_memset(wpa_s->go_dev_addr, 0, ETH_ALEN);
5742 }
5743
5744 if (auth) {
5745 if (wpas_p2p_auth_go_neg(wpa_s, peer_addr, wps_method,
5746 go_intent, if_addr,
5747 force_freq, persistent_group, ssid,
5748 pref_freq) < 0)
5749 return -1;
5750 return ret;
5751 }
5752
5753 if (wpas_p2p_start_go_neg(wpa_s, peer_addr, wps_method,
5754 go_intent, if_addr, force_freq,
5755 persistent_group, ssid, pref_freq) < 0) {
5756 if (wpa_s->create_p2p_iface)
5757 wpas_p2p_remove_pending_group_interface(wpa_s);
5758 return -1;
5759 }
5760 return ret;
5761 }
5762
5763
5764 /**
5765 * wpas_p2p_remain_on_channel_cb - Indication of remain-on-channel start
5766 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
5767 * @freq: Frequency of the channel in MHz
5768 * @duration: Duration of the stay on the channel in milliseconds
5769 *
5770 * This callback is called when the driver indicates that it has started the
5771 * requested remain-on-channel duration.
5772 */
5773 void wpas_p2p_remain_on_channel_cb(struct wpa_supplicant *wpa_s,
5774 unsigned int freq, unsigned int duration)
5775 {
5776 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
5777 return;
5778 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)",
5779 wpa_s->off_channel_freq, wpa_s->pending_listen_freq,
5780 wpa_s->roc_waiting_drv_freq, freq, duration);
5781 if (wpa_s->off_channel_freq &&
5782 wpa_s->off_channel_freq == wpa_s->pending_listen_freq) {
5783 p2p_listen_cb(wpa_s->global->p2p, wpa_s->pending_listen_freq,
5784 wpa_s->pending_listen_duration);
5785 wpa_s->pending_listen_freq = 0;
5786 } else {
5787 wpa_printf(MSG_DEBUG, "P2P: Ignore remain-on-channel callback (off_channel_freq=%u pending_listen_freq=%d freq=%u duration=%u)",
5788 wpa_s->off_channel_freq, wpa_s->pending_listen_freq,
5789 freq, duration);
5790 }
5791 }
5792
5793
5794 int wpas_p2p_listen_start(struct wpa_supplicant *wpa_s, unsigned int timeout)
5795 {
5796 /* Limit maximum Listen state time based on driver limitation. */
5797 if (timeout > wpa_s->max_remain_on_chan)
5798 timeout = wpa_s->max_remain_on_chan;
5799
5800 return p2p_listen(wpa_s->global->p2p, timeout);
5801 }
5802
5803
5804 /**
5805 * wpas_p2p_cancel_remain_on_channel_cb - Remain-on-channel timeout
5806 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
5807 * @freq: Frequency of the channel in MHz
5808 *
5809 * This callback is called when the driver indicates that a remain-on-channel
5810 * operation has been completed, i.e., the duration on the requested channel
5811 * has timed out.
5812 */
5813 void wpas_p2p_cancel_remain_on_channel_cb(struct wpa_supplicant *wpa_s,
5814 unsigned int freq)
5815 {
5816 wpa_printf(MSG_DEBUG, "P2P: Cancel remain-on-channel callback "
5817 "(p2p_long_listen=%d ms pending_action_tx=%p)",
5818 wpa_s->global->p2p_long_listen,
5819 offchannel_pending_action_tx(wpa_s));
5820 wpas_p2p_listen_work_done(wpa_s);
5821 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
5822 return;
5823 if (wpa_s->global->p2p_long_listen > 0)
5824 wpa_s->global->p2p_long_listen -= wpa_s->max_remain_on_chan;
5825 if (p2p_listen_end(wpa_s->global->p2p, freq) > 0)
5826 return; /* P2P module started a new operation */
5827 if (offchannel_pending_action_tx(wpa_s))
5828 return;
5829 if (wpa_s->global->p2p_long_listen > 0) {
5830 wpa_printf(MSG_DEBUG, "P2P: Continuing long Listen state");
5831 wpas_p2p_listen_start(wpa_s, wpa_s->global->p2p_long_listen);
5832 } else {
5833 /*
5834 * When listen duration is over, stop listen & update p2p_state
5835 * to IDLE.
5836 */
5837 p2p_stop_listen(wpa_s->global->p2p);
5838 }
5839 }
5840
5841
5842 /**
5843 * wpas_p2p_group_remove - Remove a P2P group
5844 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
5845 * @ifname: Network interface name of the group interface or "*" to remove all
5846 * groups
5847 * Returns: 0 on success, -1 on failure
5848 *
5849 * This function is used to remove a P2P group. This can be used to disconnect
5850 * from a group in which the local end is a P2P Client or to end a P2P Group in
5851 * case the local end is the Group Owner. If a virtual network interface was
5852 * created for this group, that interface will be removed. Otherwise, only the
5853 * configured P2P group network will be removed from the interface.
5854 */
5855 int wpas_p2p_group_remove(struct wpa_supplicant *wpa_s, const char *ifname)
5856 {
5857 struct wpa_global *global = wpa_s->global;
5858 struct wpa_supplicant *calling_wpa_s = wpa_s;
5859
5860 if (os_strcmp(ifname, "*") == 0) {
5861 struct wpa_supplicant *prev;
5862 wpa_s = global->ifaces;
5863 while (wpa_s) {
5864 prev = wpa_s;
5865 wpa_s = wpa_s->next;
5866 if (prev->p2p_group_interface !=
5867 NOT_P2P_GROUP_INTERFACE ||
5868 (prev->current_ssid &&
5869 prev->current_ssid->p2p_group))
5870 wpas_p2p_disconnect_safely(prev, calling_wpa_s);
5871 }
5872 return 0;
5873 }
5874
5875 for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
5876 if (os_strcmp(wpa_s->ifname, ifname) == 0)
5877 break;
5878 }
5879
5880 return wpas_p2p_disconnect_safely(wpa_s, calling_wpa_s);
5881 }
5882
5883
5884 static int wpas_p2p_select_go_freq(struct wpa_supplicant *wpa_s, int freq)
5885 {
5886 unsigned int r;
5887
5888 if (!wpa_s->conf->num_p2p_pref_chan && !freq) {
5889 unsigned int i, size = P2P_MAX_PREF_CHANNELS;
5890 unsigned int pref_freq_list[P2P_MAX_PREF_CHANNELS];
5891 int res;
5892
5893 res = wpa_drv_get_pref_freq_list(wpa_s, WPA_IF_P2P_GO,
5894 &size, pref_freq_list);
5895 if (!res && size > 0) {
5896 i = 0;
5897 while (i < size &&
5898 (!p2p_supported_freq(wpa_s->global->p2p,
5899 pref_freq_list[i]) ||
5900 wpas_p2p_disallowed_freq(wpa_s->global,
5901 pref_freq_list[i]))) {
5902 wpa_printf(MSG_DEBUG,
5903 "P2P: preferred_freq_list[%d]=%d is disallowed",
5904 i, pref_freq_list[i]);
5905 i++;
5906 }
5907 if (i != size) {
5908 freq = pref_freq_list[i];
5909 wpa_printf(MSG_DEBUG,
5910 "P2P: Using preferred_freq_list[%d]=%d",
5911 i, freq);
5912 } else {
5913 wpa_printf(MSG_DEBUG,
5914 "P2P: All driver preferred frequencies are disallowed for P2P use");
5915 }
5916 } else {
5917 wpa_printf(MSG_DEBUG,
5918 "P2P: No preferred frequency list available");
5919 }
5920 }
5921
5922 if (freq == 2) {
5923 wpa_printf(MSG_DEBUG, "P2P: Request to start GO on 2.4 GHz "
5924 "band");
5925 if (wpa_s->best_24_freq > 0 &&
5926 p2p_supported_freq_go(wpa_s->global->p2p,
5927 wpa_s->best_24_freq)) {
5928 freq = wpa_s->best_24_freq;
5929 wpa_printf(MSG_DEBUG, "P2P: Use best 2.4 GHz band "
5930 "channel: %d MHz", freq);
5931 } else {
5932 if (os_get_random((u8 *) &r, sizeof(r)) < 0)
5933 return -1;
5934 freq = 2412 + (r % 3) * 25;
5935 wpa_printf(MSG_DEBUG, "P2P: Use random 2.4 GHz band "
5936 "channel: %d MHz", freq);
5937 }
5938 }
5939
5940 if (freq == 5) {
5941 wpa_printf(MSG_DEBUG, "P2P: Request to start GO on 5 GHz "
5942 "band");
5943 if (wpa_s->best_5_freq > 0 &&
5944 p2p_supported_freq_go(wpa_s->global->p2p,
5945 wpa_s->best_5_freq)) {
5946 freq = wpa_s->best_5_freq;
5947 wpa_printf(MSG_DEBUG, "P2P: Use best 5 GHz band "
5948 "channel: %d MHz", freq);
5949 } else {
5950 if (os_get_random((u8 *) &r, sizeof(r)) < 0)
5951 return -1;
5952 freq = 5180 + (r % 4) * 20;
5953 if (!p2p_supported_freq_go(wpa_s->global->p2p, freq)) {
5954 wpa_printf(MSG_DEBUG, "P2P: Could not select "
5955 "5 GHz channel for P2P group");
5956 return -1;
5957 }
5958 wpa_printf(MSG_DEBUG, "P2P: Use random 5 GHz band "
5959 "channel: %d MHz", freq);
5960 }
5961 }
5962
5963 if (freq > 0 && !p2p_supported_freq_go(wpa_s->global->p2p, freq)) {
5964 if ((wpa_s->drv_flags & WPA_DRIVER_FLAGS_DFS_OFFLOAD) &&
5965 ieee80211_is_dfs(freq, wpa_s->hw.modes,
5966 wpa_s->hw.num_modes)) {
5967 /*
5968 * If freq is a DFS channel and DFS is offloaded to the
5969 * driver, allow P2P GO to use it.
5970 */
5971 wpa_printf(MSG_DEBUG, "P2P: "
5972 "%s: The forced channel for GO (%u MHz) is DFS, and DFS is offloaded",
5973 __func__, freq);
5974 return freq;
5975 }
5976 wpa_printf(MSG_DEBUG, "P2P: The forced channel for GO "
5977 "(%u MHz) is not supported for P2P uses",
5978 freq);
5979 return -1;
5980 }
5981
5982 return freq;
5983 }
5984
5985
5986 static int wpas_p2p_supported_freq_go(struct wpa_supplicant *wpa_s,
5987 const struct p2p_channels *channels,
5988 int freq)
5989 {
5990 if (!wpas_p2p_disallowed_freq(wpa_s->global, freq) &&
5991 p2p_supported_freq_go(wpa_s->global->p2p, freq) &&
5992 freq_included(wpa_s, channels, freq))
5993 return 1;
5994 return 0;
5995 }
5996
5997
5998 static void wpas_p2p_select_go_freq_no_pref(struct wpa_supplicant *wpa_s,
5999 struct p2p_go_neg_results *params,
6000 const struct p2p_channels *channels)
6001 {
6002 unsigned int i, r;
6003
6004 /* try all channels in operating class 115 */
6005 for (i = 0; i < 4; i++) {
6006 params->freq = 5180 + i * 20;
6007 if (!wpas_p2p_disallowed_freq(wpa_s->global, params->freq) &&
6008 freq_included(wpa_s, channels, params->freq) &&
6009 p2p_supported_freq(wpa_s->global->p2p, params->freq))
6010 goto out;
6011 }
6012
6013 /* try all channels in operating class 124 */
6014 for (i = 0; i < 4; i++) {
6015 params->freq = 5745 + i * 20;
6016 if (!wpas_p2p_disallowed_freq(wpa_s->global, params->freq) &&
6017 freq_included(wpa_s, channels, params->freq) &&
6018 p2p_supported_freq(wpa_s->global->p2p, params->freq))
6019 goto out;
6020 }
6021
6022 /* try social channel class 180 channel 2 */
6023 params->freq = 58320 + 1 * 2160;
6024 if (!wpas_p2p_disallowed_freq(wpa_s->global, params->freq) &&
6025 freq_included(wpa_s, channels, params->freq) &&
6026 p2p_supported_freq(wpa_s->global->p2p, params->freq))
6027 goto out;
6028
6029 /* try all channels in reg. class 180 */
6030 for (i = 0; i < 4; i++) {
6031 params->freq = 58320 + i * 2160;
6032 if (!wpas_p2p_disallowed_freq(wpa_s->global, params->freq) &&
6033 freq_included(wpa_s, channels, params->freq) &&
6034 p2p_supported_freq(wpa_s->global->p2p, params->freq))
6035 goto out;
6036 }
6037
6038 /* try some random selection of the social channels */
6039 if (os_get_random((u8 *) &r, sizeof(r)) < 0)
6040 return;
6041
6042 for (i = 0; i < 3; i++) {
6043 params->freq = 2412 + ((r + i) % 3) * 25;
6044 if (wpas_p2p_supported_freq_go(wpa_s, channels, params->freq))
6045 goto out;
6046 }
6047
6048 /* try all other channels in operating class 81 */
6049 for (i = 0; i < 11; i++) {
6050 params->freq = 2412 + i * 5;
6051
6052 /* skip social channels; covered in the previous loop */
6053 if (params->freq == 2412 ||
6054 params->freq == 2437 ||
6055 params->freq == 2462)
6056 continue;
6057
6058 if (wpas_p2p_supported_freq_go(wpa_s, channels, params->freq))
6059 goto out;
6060 }
6061
6062 params->freq = 0;
6063 wpa_printf(MSG_DEBUG, "P2P: No 2.4, 5, or 60 GHz channel allowed");
6064 return;
6065 out:
6066 wpa_printf(MSG_DEBUG, "P2P: Set GO freq %d MHz (no preference known)",
6067 params->freq);
6068 }
6069
6070
6071 static int wpas_same_band(int freq1, int freq2)
6072 {
6073 enum hostapd_hw_mode mode1, mode2;
6074 u8 chan1, chan2;
6075
6076 mode1 = ieee80211_freq_to_chan(freq1, &chan1);
6077 mode2 = ieee80211_freq_to_chan(freq2, &chan2);
6078 if (mode1 == NUM_HOSTAPD_MODES)
6079 return 0;
6080 return mode1 == mode2;
6081 }
6082
6083
6084 static int wpas_p2p_init_go_params(struct wpa_supplicant *wpa_s,
6085 struct p2p_go_neg_results *params,
6086 int freq, int vht_center_freq2, int ht40,
6087 int vht, int max_oper_chwidth, int he,
6088 int edmg,
6089 const struct p2p_channels *channels)
6090 {
6091 struct wpa_used_freq_data *freqs;
6092 unsigned int cand;
6093 unsigned int num, i;
6094 int ignore_no_freqs = 0;
6095 int unused_channels = wpas_p2p_num_unused_channels(wpa_s) > 0;
6096
6097 os_memset(params, 0, sizeof(*params));
6098 params->role_go = 1;
6099 params->ht40 = ht40;
6100 params->vht = vht;
6101 params->he = he;
6102 params->max_oper_chwidth = max_oper_chwidth;
6103 params->vht_center_freq2 = vht_center_freq2;
6104 params->edmg = edmg;
6105
6106 freqs = os_calloc(wpa_s->num_multichan_concurrent,
6107 sizeof(struct wpa_used_freq_data));
6108 if (!freqs)
6109 return -1;
6110
6111 num = get_shared_radio_freqs_data(wpa_s, freqs,
6112 wpa_s->num_multichan_concurrent);
6113
6114 if (wpa_s->current_ssid &&
6115 wpa_s->current_ssid->mode == WPAS_MODE_P2P_GO &&
6116 wpa_s->wpa_state == WPA_COMPLETED) {
6117 wpa_printf(MSG_DEBUG, "P2P: %s called for an active GO",
6118 __func__);
6119
6120 /*
6121 * If the frequency selection is done for an active P2P GO that
6122 * is not sharing a frequency, allow to select a new frequency
6123 * even if there are no unused frequencies as we are about to
6124 * move the P2P GO so its frequency can be re-used.
6125 */
6126 for (i = 0; i < num; i++) {
6127 if (freqs[i].freq == wpa_s->current_ssid->frequency &&
6128 freqs[i].flags == 0) {
6129 ignore_no_freqs = 1;
6130 break;
6131 }
6132 }
6133 }
6134
6135 /* Try to use EDMG channel */
6136 if (params->edmg) {
6137 if (wpas_p2p_try_edmg_channel(wpa_s, params) == 0)
6138 goto success;
6139 params->edmg = 0;
6140 }
6141
6142 /* try using the forced freq */
6143 if (freq) {
6144 if (wpas_p2p_disallowed_freq(wpa_s->global, freq) ||
6145 !freq_included(wpa_s, channels, freq)) {
6146 wpa_printf(MSG_DEBUG,
6147 "P2P: Forced GO freq %d MHz disallowed",
6148 freq);
6149 goto fail;
6150 }
6151 if (!p2p_supported_freq_go(wpa_s->global->p2p, freq)) {
6152 if ((wpa_s->drv_flags & WPA_DRIVER_FLAGS_DFS_OFFLOAD) &&
6153 ieee80211_is_dfs(freq, wpa_s->hw.modes,
6154 wpa_s->hw.num_modes)) {
6155 /*
6156 * If freq is a DFS channel and DFS is offloaded
6157 * to the driver, allow P2P GO to use it.
6158 */
6159 wpa_printf(MSG_DEBUG,
6160 "P2P: %s: The forced channel for GO (%u MHz) requires DFS and DFS is offloaded",
6161 __func__, freq);
6162 } else {
6163 wpa_printf(MSG_DEBUG,
6164 "P2P: The forced channel for GO (%u MHz) is not supported for P2P uses",
6165 freq);
6166 goto fail;
6167 }
6168 }
6169
6170 for (i = 0; i < num; i++) {
6171 if (freqs[i].freq == freq) {
6172 wpa_printf(MSG_DEBUG,
6173 "P2P: forced freq (%d MHz) is also shared",
6174 freq);
6175 params->freq = freq;
6176 goto success;
6177 }
6178 }
6179
6180 if (!ignore_no_freqs && !unused_channels) {
6181 wpa_printf(MSG_DEBUG,
6182 "P2P: Cannot force GO on freq (%d MHz) as all the channels are in use",
6183 freq);
6184 goto fail;
6185 }
6186
6187 wpa_printf(MSG_DEBUG,
6188 "P2P: force GO freq (%d MHz) on a free channel",
6189 freq);
6190 params->freq = freq;
6191 goto success;
6192 }
6193
6194 /* consider using one of the shared frequencies */
6195 if (num &&
6196 (!wpa_s->conf->p2p_ignore_shared_freq || !unused_channels)) {
6197 cand = wpas_p2p_pick_best_used_freq(wpa_s, freqs, num);
6198 if (wpas_p2p_supported_freq_go(wpa_s, channels, cand)) {
6199 wpa_printf(MSG_DEBUG,
6200 "P2P: Use shared freq (%d MHz) for GO",
6201 cand);
6202 params->freq = cand;
6203 goto success;
6204 }
6205
6206 /* try using one of the shared freqs */
6207 for (i = 0; i < num; i++) {
6208 if (wpas_p2p_supported_freq_go(wpa_s, channels,
6209 freqs[i].freq)) {
6210 wpa_printf(MSG_DEBUG,
6211 "P2P: Use shared freq (%d MHz) for GO",
6212 freqs[i].freq);
6213 params->freq = freqs[i].freq;
6214 goto success;
6215 }
6216 }
6217 }
6218
6219 if (!ignore_no_freqs && !unused_channels) {
6220 wpa_printf(MSG_DEBUG,
6221 "P2P: Cannot force GO on any of the channels we are already using");
6222 goto fail;
6223 }
6224
6225 /* try using the setting from the configuration file */
6226 if (wpa_s->conf->p2p_oper_reg_class == 81 &&
6227 wpa_s->conf->p2p_oper_channel >= 1 &&
6228 wpa_s->conf->p2p_oper_channel <= 11 &&
6229 wpas_p2p_supported_freq_go(
6230 wpa_s, channels,
6231 2407 + 5 * wpa_s->conf->p2p_oper_channel)) {
6232 params->freq = 2407 + 5 * wpa_s->conf->p2p_oper_channel;
6233 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on configured "
6234 "frequency %d MHz", params->freq);
6235 goto success;
6236 }
6237
6238 if ((wpa_s->conf->p2p_oper_reg_class == 115 ||
6239 wpa_s->conf->p2p_oper_reg_class == 116 ||
6240 wpa_s->conf->p2p_oper_reg_class == 117 ||
6241 wpa_s->conf->p2p_oper_reg_class == 124 ||
6242 wpa_s->conf->p2p_oper_reg_class == 125 ||
6243 wpa_s->conf->p2p_oper_reg_class == 126 ||
6244 wpa_s->conf->p2p_oper_reg_class == 127) &&
6245 wpas_p2p_supported_freq_go(wpa_s, channels,
6246 5000 +
6247 5 * wpa_s->conf->p2p_oper_channel)) {
6248 params->freq = 5000 + 5 * wpa_s->conf->p2p_oper_channel;
6249 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on configured "
6250 "frequency %d MHz", params->freq);
6251 goto success;
6252 }
6253
6254 /* Try using best channels */
6255 if (wpa_s->conf->p2p_oper_channel == 0 &&
6256 wpa_s->best_overall_freq > 0 &&
6257 wpas_p2p_supported_freq_go(wpa_s, channels,
6258 wpa_s->best_overall_freq)) {
6259 params->freq = wpa_s->best_overall_freq;
6260 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on best overall "
6261 "channel %d MHz", params->freq);
6262 goto success;
6263 }
6264
6265 if (wpa_s->conf->p2p_oper_channel == 0 &&
6266 wpa_s->best_24_freq > 0 &&
6267 wpas_p2p_supported_freq_go(wpa_s, channels,
6268 wpa_s->best_24_freq)) {
6269 params->freq = wpa_s->best_24_freq;
6270 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on best 2.4 GHz "
6271 "channel %d MHz", params->freq);
6272 goto success;
6273 }
6274
6275 if (wpa_s->conf->p2p_oper_channel == 0 &&
6276 wpa_s->best_5_freq > 0 &&
6277 wpas_p2p_supported_freq_go(wpa_s, channels,
6278 wpa_s->best_5_freq)) {
6279 params->freq = wpa_s->best_5_freq;
6280 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on best 5 GHz "
6281 "channel %d MHz", params->freq);
6282 goto success;
6283 }
6284
6285 /* try using preferred channels */
6286 cand = p2p_get_pref_freq(wpa_s->global->p2p, channels);
6287 if (cand && wpas_p2p_supported_freq_go(wpa_s, channels, cand)) {
6288 params->freq = cand;
6289 wpa_printf(MSG_DEBUG, "P2P: Set GO freq %d MHz from preferred "
6290 "channels", params->freq);
6291 goto success;
6292 }
6293
6294 /* Try using a channel that allows VHT to be used with 80 MHz */
6295 if (wpa_s->hw.modes && wpa_s->p2p_group_common_freqs) {
6296 for (i = 0; i < wpa_s->p2p_group_common_freqs_num; i++) {
6297 enum hostapd_hw_mode mode;
6298 struct hostapd_hw_modes *hwmode;
6299 u8 chan;
6300
6301 cand = wpa_s->p2p_group_common_freqs[i];
6302 mode = ieee80211_freq_to_chan(cand, &chan);
6303 hwmode = get_mode(wpa_s->hw.modes, wpa_s->hw.num_modes,
6304 mode, is_6ghz_freq(cand));
6305 if (!hwmode ||
6306 wpas_p2p_verify_channel(wpa_s, hwmode, chan,
6307 BW80) != ALLOWED)
6308 continue;
6309 if (wpas_p2p_supported_freq_go(wpa_s, channels, cand)) {
6310 params->freq = cand;
6311 wpa_printf(MSG_DEBUG,
6312 "P2P: Use freq %d MHz common with the peer and allowing VHT80",
6313 params->freq);
6314 goto success;
6315 }
6316 }
6317 }
6318
6319 /* Try using a channel that allows HT to be used with 40 MHz on the same
6320 * band so that CSA can be used */
6321 if (wpa_s->current_ssid && wpa_s->hw.modes &&
6322 wpa_s->p2p_group_common_freqs) {
6323 for (i = 0; i < wpa_s->p2p_group_common_freqs_num; i++) {
6324 enum hostapd_hw_mode mode;
6325 struct hostapd_hw_modes *hwmode;
6326 u8 chan;
6327
6328 cand = wpa_s->p2p_group_common_freqs[i];
6329 mode = ieee80211_freq_to_chan(cand, &chan);
6330 hwmode = get_mode(wpa_s->hw.modes, wpa_s->hw.num_modes,
6331 mode, is_6ghz_freq(cand));
6332 if (!wpas_same_band(wpa_s->current_ssid->frequency,
6333 cand) ||
6334 !hwmode ||
6335 (wpas_p2p_verify_channel(wpa_s, hwmode, chan,
6336 BW40MINUS) != ALLOWED &&
6337 wpas_p2p_verify_channel(wpa_s, hwmode, chan,
6338 BW40PLUS) != ALLOWED))
6339 continue;
6340 if (wpas_p2p_supported_freq_go(wpa_s, channels, cand)) {
6341 params->freq = cand;
6342 wpa_printf(MSG_DEBUG,
6343 "P2P: Use freq %d MHz common with the peer, allowing HT40, and maintaining same band",
6344 params->freq);
6345 goto success;
6346 }
6347 }
6348 }
6349
6350 /* Try using one of the group common freqs on the same band so that CSA
6351 * can be used */
6352 if (wpa_s->current_ssid && wpa_s->p2p_group_common_freqs) {
6353 for (i = 0; i < wpa_s->p2p_group_common_freqs_num; i++) {
6354 cand = wpa_s->p2p_group_common_freqs[i];
6355 if (!wpas_same_band(wpa_s->current_ssid->frequency,
6356 cand))
6357 continue;
6358 if (wpas_p2p_supported_freq_go(wpa_s, channels, cand)) {
6359 params->freq = cand;
6360 wpa_printf(MSG_DEBUG,
6361 "P2P: Use freq %d MHz common with the peer and maintaining same band",
6362 params->freq);
6363 goto success;
6364 }
6365 }
6366 }
6367
6368 /* Try using one of the group common freqs */
6369 if (wpa_s->p2p_group_common_freqs) {
6370 for (i = 0; i < wpa_s->p2p_group_common_freqs_num; i++) {
6371 cand = wpa_s->p2p_group_common_freqs[i];
6372 if (wpas_p2p_supported_freq_go(wpa_s, channels, cand)) {
6373 params->freq = cand;
6374 wpa_printf(MSG_DEBUG,
6375 "P2P: Use freq %d MHz common with the peer",
6376 params->freq);
6377 goto success;
6378 }
6379 }
6380 }
6381
6382 /* no preference, select some channel */
6383 wpas_p2p_select_go_freq_no_pref(wpa_s, params, channels);
6384
6385 if (params->freq == 0) {
6386 wpa_printf(MSG_DEBUG, "P2P: did not find a freq for GO use");
6387 goto fail;
6388 }
6389
6390 success:
6391 os_free(freqs);
6392 return 0;
6393 fail:
6394 os_free(freqs);
6395 return -1;
6396 }
6397
6398
6399 static struct wpa_supplicant *
6400 wpas_p2p_get_group_iface(struct wpa_supplicant *wpa_s, int addr_allocated,
6401 int go)
6402 {
6403 struct wpa_supplicant *group_wpa_s;
6404
6405 if (!wpas_p2p_create_iface(wpa_s)) {
6406 if (wpa_s->p2p_mgmt) {
6407 /*
6408 * We may be called on the p2p_dev interface which
6409 * cannot be used for group operations, so always use
6410 * the primary interface.
6411 */
6412 wpa_s->parent->p2pdev = wpa_s;
6413 wpa_s = wpa_s->parent;
6414 }
6415 wpa_dbg(wpa_s, MSG_DEBUG,
6416 "P2P: Use primary interface for group operations");
6417 wpa_s->p2p_first_connection_timeout = 0;
6418 if (wpa_s != wpa_s->p2pdev)
6419 wpas_p2p_clone_config(wpa_s, wpa_s->p2pdev);
6420 return wpa_s;
6421 }
6422
6423 if (wpas_p2p_add_group_interface(wpa_s, go ? WPA_IF_P2P_GO :
6424 WPA_IF_P2P_CLIENT) < 0) {
6425 wpa_msg_global(wpa_s, MSG_ERROR,
6426 "P2P: Failed to add group interface");
6427 return NULL;
6428 }
6429 group_wpa_s = wpas_p2p_init_group_interface(wpa_s, go);
6430 if (group_wpa_s == NULL) {
6431 wpa_msg_global(wpa_s, MSG_ERROR,
6432 "P2P: Failed to initialize group interface");
6433 wpas_p2p_remove_pending_group_interface(wpa_s);
6434 return NULL;
6435 }
6436
6437 if (go && wpa_s->p2p_go_do_acs) {
6438 group_wpa_s->p2p_go_do_acs = wpa_s->p2p_go_do_acs;
6439 group_wpa_s->p2p_go_acs_band = wpa_s->p2p_go_acs_band;
6440 wpa_s->p2p_go_do_acs = 0;
6441 }
6442
6443 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Use separate group interface %s",
6444 group_wpa_s->ifname);
6445 group_wpa_s->p2p_first_connection_timeout = 0;
6446 return group_wpa_s;
6447 }
6448
6449
6450 /**
6451 * wpas_p2p_group_add - Add a new P2P group with local end as Group Owner
6452 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
6453 * @persistent_group: Whether to create a persistent group
6454 * @freq: Frequency for the group or 0 to indicate no hardcoding
6455 * @vht_center_freq2: segment_1 center frequency for GO operating in VHT 80P80
6456 * @ht40: Start GO with 40 MHz channel width
6457 * @vht: Start GO with VHT support
6458 * @vht_chwidth: channel bandwidth for GO operating with VHT support
6459 * @edmg: Start GO with EDMG support
6460 * Returns: 0 on success, -1 on failure
6461 *
6462 * This function creates a new P2P group with the local end as the Group Owner,
6463 * i.e., without using Group Owner Negotiation.
6464 */
6465 int wpas_p2p_group_add(struct wpa_supplicant *wpa_s, int persistent_group,
6466 int freq, int vht_center_freq2, int ht40, int vht,
6467 int max_oper_chwidth, int he, int edmg)
6468 {
6469 struct p2p_go_neg_results params;
6470
6471 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
6472 return -1;
6473
6474 os_free(wpa_s->global->add_psk);
6475 wpa_s->global->add_psk = NULL;
6476
6477 /* Make sure we are not running find during connection establishment */
6478 wpa_printf(MSG_DEBUG, "P2P: Stop any on-going P2P FIND");
6479 wpas_p2p_stop_find_oper(wpa_s);
6480
6481 if (!wpa_s->p2p_go_do_acs) {
6482 freq = wpas_p2p_select_go_freq(wpa_s, freq);
6483 if (freq < 0)
6484 return -1;
6485 }
6486
6487 if (wpas_p2p_init_go_params(wpa_s, &params, freq, vht_center_freq2,
6488 ht40, vht, max_oper_chwidth, he, edmg,
6489 NULL))
6490 return -1;
6491
6492 p2p_go_params(wpa_s->global->p2p, &params);
6493 params.persistent_group = persistent_group;
6494
6495 wpa_s = wpas_p2p_get_group_iface(wpa_s, 0, 1);
6496 if (wpa_s == NULL)
6497 return -1;
6498 wpas_start_wps_go(wpa_s, &params, 0);
6499
6500 return 0;
6501 }
6502
6503
6504 static int wpas_start_p2p_client(struct wpa_supplicant *wpa_s,
6505 struct wpa_ssid *params, int addr_allocated,
6506 int freq, int force_scan)
6507 {
6508 struct wpa_ssid *ssid;
6509
6510 wpa_s = wpas_p2p_get_group_iface(wpa_s, addr_allocated, 0);
6511 if (wpa_s == NULL)
6512 return -1;
6513 if (force_scan)
6514 os_get_reltime(&wpa_s->scan_min_time);
6515 wpa_s->p2p_last_4way_hs_fail = NULL;
6516
6517 wpa_supplicant_ap_deinit(wpa_s);
6518
6519 ssid = wpa_config_add_network(wpa_s->conf);
6520 if (ssid == NULL)
6521 return -1;
6522 os_memset(wpa_s->go_dev_addr, 0, ETH_ALEN);
6523 wpa_config_set_network_defaults(ssid);
6524 ssid->temporary = 1;
6525 ssid->proto = WPA_PROTO_RSN;
6526 ssid->pbss = params->pbss;
6527 ssid->pairwise_cipher = params->pbss ? WPA_CIPHER_GCMP :
6528 WPA_CIPHER_CCMP;
6529 ssid->group_cipher = params->pbss ? WPA_CIPHER_GCMP : WPA_CIPHER_CCMP;
6530 ssid->key_mgmt = WPA_KEY_MGMT_PSK;
6531 ssid->ssid = os_malloc(params->ssid_len);
6532 if (ssid->ssid == NULL) {
6533 wpa_config_remove_network(wpa_s->conf, ssid->id);
6534 return -1;
6535 }
6536 os_memcpy(ssid->ssid, params->ssid, params->ssid_len);
6537 ssid->ssid_len = params->ssid_len;
6538 ssid->p2p_group = 1;
6539 ssid->export_keys = 1;
6540 if (params->psk_set) {
6541 os_memcpy(ssid->psk, params->psk, 32);
6542 ssid->psk_set = 1;
6543 }
6544 if (params->passphrase)
6545 ssid->passphrase = os_strdup(params->passphrase);
6546
6547 wpa_s->show_group_started = 1;
6548 wpa_s->p2p_in_invitation = 1;
6549 wpa_s->p2p_invite_go_freq = freq;
6550 wpa_s->p2p_go_group_formation_completed = 0;
6551 wpa_s->global->p2p_group_formation = wpa_s;
6552
6553 eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s->p2pdev,
6554 NULL);
6555 eloop_register_timeout(P2P_MAX_INITIAL_CONN_WAIT, 0,
6556 wpas_p2p_group_formation_timeout,
6557 wpa_s->p2pdev, NULL);
6558 wpa_supplicant_select_network(wpa_s, ssid);
6559
6560 return 0;
6561 }
6562
6563
6564 int wpas_p2p_group_add_persistent(struct wpa_supplicant *wpa_s,
6565 struct wpa_ssid *ssid, int addr_allocated,
6566 int force_freq, int neg_freq,
6567 int vht_center_freq2, int ht40,
6568 int vht, int max_oper_chwidth, int he,
6569 int edmg,
6570 const struct p2p_channels *channels,
6571 int connection_timeout, int force_scan)
6572 {
6573 struct p2p_go_neg_results params;
6574 int go = 0, freq;
6575
6576 if (ssid->disabled != 2 || ssid->ssid == NULL)
6577 return -1;
6578
6579 if (wpas_get_p2p_group(wpa_s, ssid->ssid, ssid->ssid_len, &go) &&
6580 go == (ssid->mode == WPAS_MODE_P2P_GO)) {
6581 wpa_printf(MSG_DEBUG, "P2P: Requested persistent group is "
6582 "already running");
6583 if (go == 0 &&
6584 eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
6585 wpa_s->p2pdev, NULL)) {
6586 /*
6587 * This can happen if Invitation Response frame was lost
6588 * and the peer (GO of a persistent group) tries to
6589 * invite us again. Reschedule the timeout to avoid
6590 * terminating the wait for the connection too early
6591 * since we now know that the peer is still trying to
6592 * invite us instead of having already started the GO.
6593 */
6594 wpa_printf(MSG_DEBUG,
6595 "P2P: Reschedule group formation timeout since peer is still trying to invite us");
6596 eloop_register_timeout(P2P_MAX_INITIAL_CONN_WAIT, 0,
6597 wpas_p2p_group_formation_timeout,
6598 wpa_s->p2pdev, NULL);
6599 }
6600 return 0;
6601 }
6602
6603 os_free(wpa_s->global->add_psk);
6604 wpa_s->global->add_psk = NULL;
6605
6606 /* Make sure we are not running find during connection establishment */
6607 wpas_p2p_stop_find_oper(wpa_s);
6608
6609 wpa_s->p2p_fallback_to_go_neg = 0;
6610
6611 if (ssid->mode == WPAS_MODE_P2P_GO) {
6612 if (force_freq > 0) {
6613 freq = wpas_p2p_select_go_freq(wpa_s, force_freq);
6614 if (freq < 0)
6615 return -1;
6616 } else {
6617 freq = wpas_p2p_select_go_freq(wpa_s, neg_freq);
6618 if (freq < 0 ||
6619 (freq > 0 && !freq_included(wpa_s, channels, freq)))
6620 freq = 0;
6621 }
6622 } else if (ssid->mode == WPAS_MODE_INFRA) {
6623 freq = neg_freq;
6624 if (freq <= 0 || !freq_included(wpa_s, channels, freq)) {
6625 struct os_reltime now;
6626 struct wpa_bss *bss =
6627 wpa_bss_get_p2p_dev_addr(wpa_s, ssid->bssid);
6628
6629 os_get_reltime(&now);
6630 if (bss &&
6631 !os_reltime_expired(&now, &bss->last_update, 5) &&
6632 freq_included(wpa_s, channels, bss->freq))
6633 freq = bss->freq;
6634 else
6635 freq = 0;
6636 }
6637
6638 return wpas_start_p2p_client(wpa_s, ssid, addr_allocated, freq,
6639 force_scan);
6640 } else {
6641 return -1;
6642 }
6643
6644 if (wpas_p2p_init_go_params(wpa_s, &params, freq, vht_center_freq2,
6645 ht40, vht, max_oper_chwidth, he, edmg,
6646 channels))
6647 return -1;
6648
6649 params.role_go = 1;
6650 params.psk_set = ssid->psk_set;
6651 if (params.psk_set)
6652 os_memcpy(params.psk, ssid->psk, sizeof(params.psk));
6653 if (ssid->passphrase) {
6654 if (os_strlen(ssid->passphrase) >= sizeof(params.passphrase)) {
6655 wpa_printf(MSG_ERROR, "P2P: Invalid passphrase in "
6656 "persistent group");
6657 return -1;
6658 }
6659 os_strlcpy(params.passphrase, ssid->passphrase,
6660 sizeof(params.passphrase));
6661 }
6662 os_memcpy(params.ssid, ssid->ssid, ssid->ssid_len);
6663 params.ssid_len = ssid->ssid_len;
6664 params.persistent_group = 1;
6665
6666 wpa_s = wpas_p2p_get_group_iface(wpa_s, addr_allocated, 1);
6667 if (wpa_s == NULL)
6668 return -1;
6669
6670 p2p_channels_to_freqs(channels, params.freq_list, P2P_MAX_CHANNELS);
6671
6672 wpa_s->p2p_first_connection_timeout = connection_timeout;
6673 wpas_start_wps_go(wpa_s, &params, 0);
6674
6675 return 0;
6676 }
6677
6678
6679 static void wpas_p2p_ie_update(void *ctx, struct wpabuf *beacon_ies,
6680 struct wpabuf *proberesp_ies)
6681 {
6682 struct wpa_supplicant *wpa_s = ctx;
6683 if (wpa_s->ap_iface) {
6684 struct hostapd_data *hapd = wpa_s->ap_iface->bss[0];
6685 if (!(hapd->conf->p2p & P2P_GROUP_OWNER)) {
6686 wpabuf_free(beacon_ies);
6687 wpabuf_free(proberesp_ies);
6688 return;
6689 }
6690 if (beacon_ies) {
6691 wpabuf_free(hapd->p2p_beacon_ie);
6692 hapd->p2p_beacon_ie = beacon_ies;
6693 }
6694 wpabuf_free(hapd->p2p_probe_resp_ie);
6695 hapd->p2p_probe_resp_ie = proberesp_ies;
6696 } else {
6697 wpabuf_free(beacon_ies);
6698 wpabuf_free(proberesp_ies);
6699 }
6700 wpa_supplicant_ap_update_beacon(wpa_s);
6701 }
6702
6703
6704 static void wpas_p2p_idle_update(void *ctx, int idle)
6705 {
6706 struct wpa_supplicant *wpa_s = ctx;
6707 if (!wpa_s->ap_iface)
6708 return;
6709 wpa_printf(MSG_DEBUG, "P2P: GO - group %sidle", idle ? "" : "not ");
6710 if (idle) {
6711 if (wpa_s->global->p2p_fail_on_wps_complete &&
6712 wpa_s->p2p_in_provisioning) {
6713 wpas_p2p_grpform_fail_after_wps(wpa_s);
6714 return;
6715 }
6716 wpas_p2p_set_group_idle_timeout(wpa_s);
6717 } else
6718 eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL);
6719 }
6720
6721
6722 struct p2p_group * wpas_p2p_group_init(struct wpa_supplicant *wpa_s,
6723 struct wpa_ssid *ssid)
6724 {
6725 struct p2p_group *group;
6726 struct p2p_group_config *cfg;
6727
6728 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL ||
6729 !ssid->p2p_group)
6730 return NULL;
6731
6732 cfg = os_zalloc(sizeof(*cfg));
6733 if (cfg == NULL)
6734 return NULL;
6735
6736 if (ssid->p2p_persistent_group && wpa_s->conf->persistent_reconnect)
6737 cfg->persistent_group = 2;
6738 else if (ssid->p2p_persistent_group)
6739 cfg->persistent_group = 1;
6740 os_memcpy(cfg->interface_addr, wpa_s->own_addr, ETH_ALEN);
6741 if (wpa_s->max_stations &&
6742 wpa_s->max_stations < wpa_s->conf->max_num_sta)
6743 cfg->max_clients = wpa_s->max_stations;
6744 else
6745 cfg->max_clients = wpa_s->conf->max_num_sta;
6746 os_memcpy(cfg->ssid, ssid->ssid, ssid->ssid_len);
6747 cfg->ssid_len = ssid->ssid_len;
6748 cfg->freq = ssid->frequency;
6749 cfg->cb_ctx = wpa_s;
6750 cfg->ie_update = wpas_p2p_ie_update;
6751 cfg->idle_update = wpas_p2p_idle_update;
6752 cfg->ip_addr_alloc = WPA_GET_BE32(wpa_s->p2pdev->conf->ip_addr_start)
6753 != 0;
6754
6755 group = p2p_group_init(wpa_s->global->p2p, cfg);
6756 if (group == NULL)
6757 os_free(cfg);
6758 if (ssid->mode != WPAS_MODE_P2P_GROUP_FORMATION)
6759 p2p_group_notif_formation_done(group);
6760 wpa_s->p2p_group = group;
6761 return group;
6762 }
6763
6764
6765 void wpas_p2p_wps_success(struct wpa_supplicant *wpa_s, const u8 *peer_addr,
6766 int registrar)
6767 {
6768 struct wpa_ssid *ssid = wpa_s->current_ssid;
6769
6770 if (!wpa_s->p2p_in_provisioning) {
6771 wpa_printf(MSG_DEBUG, "P2P: Ignore WPS success event - P2P "
6772 "provisioning not in progress");
6773 return;
6774 }
6775
6776 if (ssid && ssid->mode == WPAS_MODE_INFRA) {
6777 u8 go_dev_addr[ETH_ALEN];
6778 os_memcpy(go_dev_addr, wpa_s->bssid, ETH_ALEN);
6779 wpas_p2p_persistent_group(wpa_s, go_dev_addr, ssid->ssid,
6780 ssid->ssid_len);
6781 /* Clear any stored provisioning info */
6782 p2p_clear_provisioning_info(wpa_s->global->p2p, go_dev_addr);
6783 }
6784
6785 eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s->p2pdev,
6786 NULL);
6787 wpa_s->p2p_go_group_formation_completed = 1;
6788 if (ssid && ssid->mode == WPAS_MODE_INFRA) {
6789 /*
6790 * Use a separate timeout for initial data connection to
6791 * complete to allow the group to be removed automatically if
6792 * something goes wrong in this step before the P2P group idle
6793 * timeout mechanism is taken into use.
6794 */
6795 wpa_dbg(wpa_s, MSG_DEBUG,
6796 "P2P: Re-start group formation timeout (%d seconds) as client for initial connection",
6797 P2P_MAX_INITIAL_CONN_WAIT);
6798 eloop_register_timeout(P2P_MAX_INITIAL_CONN_WAIT, 0,
6799 wpas_p2p_group_formation_timeout,
6800 wpa_s->p2pdev, NULL);
6801 /* Complete group formation on successful data connection. */
6802 wpa_s->p2p_go_group_formation_completed = 0;
6803 } else if (ssid) {
6804 /*
6805 * Use a separate timeout for initial data connection to
6806 * complete to allow the group to be removed automatically if
6807 * the client does not complete data connection successfully.
6808 */
6809 wpa_dbg(wpa_s, MSG_DEBUG,
6810 "P2P: Re-start group formation timeout (%d seconds) as GO for initial connection",
6811 P2P_MAX_INITIAL_CONN_WAIT_GO);
6812 eloop_register_timeout(P2P_MAX_INITIAL_CONN_WAIT_GO, 0,
6813 wpas_p2p_group_formation_timeout,
6814 wpa_s->p2pdev, NULL);
6815 /*
6816 * Complete group formation on first successful data connection
6817 */
6818 wpa_s->p2p_go_group_formation_completed = 0;
6819 }
6820 if (wpa_s->global->p2p)
6821 p2p_wps_success_cb(wpa_s->global->p2p, peer_addr);
6822 wpas_group_formation_completed(wpa_s, 1, 0);
6823 }
6824
6825
6826 void wpas_p2p_wps_failed(struct wpa_supplicant *wpa_s,
6827 struct wps_event_fail *fail)
6828 {
6829 if (!wpa_s->p2p_in_provisioning) {
6830 wpa_printf(MSG_DEBUG, "P2P: Ignore WPS fail event - P2P "
6831 "provisioning not in progress");
6832 return;
6833 }
6834
6835 if (wpa_s->go_params) {
6836 p2p_clear_provisioning_info(
6837 wpa_s->global->p2p,
6838 wpa_s->go_params->peer_device_addr);
6839 }
6840
6841 wpas_notify_p2p_wps_failed(wpa_s, fail);
6842
6843 if (wpa_s == wpa_s->global->p2p_group_formation) {
6844 /*
6845 * Allow some time for the failed WPS negotiation exchange to
6846 * complete, but remove the group since group formation cannot
6847 * succeed after provisioning failure.
6848 */
6849 wpa_printf(MSG_DEBUG, "P2P: WPS step failed during group formation - reject connection from timeout");
6850 wpa_s->global->p2p_fail_on_wps_complete = 1;
6851 eloop_deplete_timeout(0, 50000,
6852 wpas_p2p_group_formation_timeout,
6853 wpa_s->p2pdev, NULL);
6854 }
6855 }
6856
6857
6858 int wpas_p2p_wps_eapol_cb(struct wpa_supplicant *wpa_s)
6859 {
6860 if (!wpa_s->global->p2p_fail_on_wps_complete ||
6861 !wpa_s->p2p_in_provisioning)
6862 return 0;
6863
6864 wpas_p2p_grpform_fail_after_wps(wpa_s);
6865
6866 return 1;
6867 }
6868
6869
6870 int wpas_p2p_prov_disc(struct wpa_supplicant *wpa_s, const u8 *peer_addr,
6871 const char *config_method,
6872 enum wpas_p2p_prov_disc_use use,
6873 struct p2ps_provision *p2ps_prov)
6874 {
6875 u16 config_methods;
6876
6877 wpa_s->global->pending_p2ps_group = 0;
6878 wpa_s->global->pending_p2ps_group_freq = 0;
6879 wpa_s->p2p_fallback_to_go_neg = 0;
6880 wpa_s->pending_pd_use = NORMAL_PD;
6881 if (p2ps_prov && use == WPAS_P2P_PD_FOR_ASP) {
6882 p2ps_prov->conncap = p2ps_group_capability(
6883 wpa_s, P2PS_SETUP_NONE, p2ps_prov->role,
6884 &p2ps_prov->force_freq, &p2ps_prov->pref_freq);
6885
6886 wpa_printf(MSG_DEBUG,
6887 "P2P: %s conncap: %d - ASP parsed: %x %x %d %s",
6888 __func__, p2ps_prov->conncap,
6889 p2ps_prov->adv_id, p2ps_prov->conncap,
6890 p2ps_prov->status, p2ps_prov->info);
6891
6892 config_methods = 0;
6893 } else if (os_strncmp(config_method, "display", 7) == 0)
6894 config_methods = WPS_CONFIG_DISPLAY;
6895 else if (os_strncmp(config_method, "keypad", 6) == 0)
6896 config_methods = WPS_CONFIG_KEYPAD;
6897 else if (os_strncmp(config_method, "pbc", 3) == 0 ||
6898 os_strncmp(config_method, "pushbutton", 10) == 0)
6899 config_methods = WPS_CONFIG_PUSHBUTTON;
6900 else {
6901 wpa_printf(MSG_DEBUG, "P2P: Unknown config method");
6902 os_free(p2ps_prov);
6903 return -1;
6904 }
6905
6906 if (use == WPAS_P2P_PD_AUTO) {
6907 os_memcpy(wpa_s->pending_join_dev_addr, peer_addr, ETH_ALEN);
6908 wpa_s->pending_pd_config_methods = config_methods;
6909 wpa_s->p2p_auto_pd = 1;
6910 wpa_s->p2p_auto_join = 0;
6911 wpa_s->pending_pd_before_join = 0;
6912 wpa_s->auto_pd_scan_retry = 0;
6913 wpas_p2p_stop_find(wpa_s);
6914 wpa_s->p2p_join_scan_count = 0;
6915 os_get_reltime(&wpa_s->p2p_auto_started);
6916 wpa_printf(MSG_DEBUG, "P2P: Auto PD started at %ld.%06ld",
6917 wpa_s->p2p_auto_started.sec,
6918 wpa_s->p2p_auto_started.usec);
6919 wpas_p2p_join_scan(wpa_s, NULL);
6920 return 0;
6921 }
6922
6923 if (wpa_s->global->p2p == NULL || wpa_s->global->p2p_disabled) {
6924 os_free(p2ps_prov);
6925 return -1;
6926 }
6927
6928 return p2p_prov_disc_req(wpa_s->global->p2p, peer_addr, p2ps_prov,
6929 config_methods, use == WPAS_P2P_PD_FOR_JOIN,
6930 0, 1);
6931 }
6932
6933
6934 int wpas_p2p_scan_result_text(const u8 *ies, size_t ies_len, char *buf,
6935 char *end)
6936 {
6937 return p2p_scan_result_text(ies, ies_len, buf, end);
6938 }
6939
6940
6941 static void wpas_p2p_clear_pending_action_tx(struct wpa_supplicant *wpa_s)
6942 {
6943 if (!offchannel_pending_action_tx(wpa_s))
6944 return;
6945
6946 if (wpa_s->p2p_send_action_work) {
6947 wpas_p2p_free_send_action_work(wpa_s);
6948 eloop_cancel_timeout(wpas_p2p_send_action_work_timeout,
6949 wpa_s, NULL);
6950 offchannel_send_action_done(wpa_s);
6951 }
6952
6953 wpa_printf(MSG_DEBUG, "P2P: Drop pending Action TX due to new "
6954 "operation request");
6955 offchannel_clear_pending_action_tx(wpa_s);
6956 }
6957
6958
6959 int wpas_p2p_find(struct wpa_supplicant *wpa_s, unsigned int timeout,
6960 enum p2p_discovery_type type,
6961 unsigned int num_req_dev_types, const u8 *req_dev_types,
6962 const u8 *dev_id, unsigned int search_delay,
6963 u8 seek_cnt, const char **seek_string, int freq)
6964 {
6965 wpas_p2p_clear_pending_action_tx(wpa_s);
6966 wpa_s->global->p2p_long_listen = 0;
6967
6968 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL ||
6969 wpa_s->p2p_in_provisioning) {
6970 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Reject p2p_find operation%s%s",
6971 (wpa_s->global->p2p_disabled || !wpa_s->global->p2p) ?
6972 " (P2P disabled)" : "",
6973 wpa_s->p2p_in_provisioning ?
6974 " (p2p_in_provisioning)" : "");
6975 return -1;
6976 }
6977
6978 wpa_supplicant_cancel_sched_scan(wpa_s);
6979
6980 return p2p_find(wpa_s->global->p2p, timeout, type,
6981 num_req_dev_types, req_dev_types, dev_id,
6982 search_delay, seek_cnt, seek_string, freq);
6983 }
6984
6985
6986 static void wpas_p2p_scan_res_ignore_search(struct wpa_supplicant *wpa_s,
6987 struct wpa_scan_results *scan_res)
6988 {
6989 wpa_printf(MSG_DEBUG, "P2P: Ignore scan results");
6990
6991 if (wpa_s->p2p_scan_work) {
6992 struct wpa_radio_work *work = wpa_s->p2p_scan_work;
6993 wpa_s->p2p_scan_work = NULL;
6994 radio_work_done(work);
6995 }
6996
6997 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
6998 return;
6999
7000 /*
7001 * Indicate that results have been processed so that the P2P module can
7002 * continue pending tasks.
7003 */
7004 p2p_scan_res_handled(wpa_s->global->p2p);
7005 }
7006
7007
7008 static void wpas_p2p_stop_find_oper(struct wpa_supplicant *wpa_s)
7009 {
7010 wpas_p2p_clear_pending_action_tx(wpa_s);
7011 wpa_s->global->p2p_long_listen = 0;
7012 eloop_cancel_timeout(wpas_p2p_long_listen_timeout, wpa_s, NULL);
7013 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
7014
7015 if (wpa_s->global->p2p)
7016 p2p_stop_find(wpa_s->global->p2p);
7017
7018 if (wpa_s->scan_res_handler == wpas_p2p_scan_res_handler) {
7019 wpa_printf(MSG_DEBUG,
7020 "P2P: Do not consider the scan results after stop_find");
7021 wpa_s->scan_res_handler = wpas_p2p_scan_res_ignore_search;
7022 }
7023 }
7024
7025
7026 void wpas_p2p_stop_find(struct wpa_supplicant *wpa_s)
7027 {
7028 wpas_p2p_stop_find_oper(wpa_s);
7029 if (!wpa_s->global->pending_group_iface_for_p2ps)
7030 wpas_p2p_remove_pending_group_interface(wpa_s);
7031 }
7032
7033
7034 static void wpas_p2p_long_listen_timeout(void *eloop_ctx, void *timeout_ctx)
7035 {
7036 struct wpa_supplicant *wpa_s = eloop_ctx;
7037 wpa_s->global->p2p_long_listen = 0;
7038 }
7039
7040
7041 int wpas_p2p_listen(struct wpa_supplicant *wpa_s, unsigned int timeout)
7042 {
7043 int res;
7044
7045 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
7046 return -1;
7047
7048 if (wpa_s->p2p_lo_started) {
7049 wpa_printf(MSG_DEBUG,
7050 "P2P: Cannot start P2P listen, it is offloaded");
7051 return -1;
7052 }
7053
7054 wpa_supplicant_cancel_sched_scan(wpa_s);
7055 wpas_p2p_clear_pending_action_tx(wpa_s);
7056
7057 if (timeout == 0) {
7058 /*
7059 * This is a request for unlimited Listen state. However, at
7060 * least for now, this is mapped to a Listen state for one
7061 * hour.
7062 */
7063 timeout = 3600;
7064 }
7065 eloop_cancel_timeout(wpas_p2p_long_listen_timeout, wpa_s, NULL);
7066 wpa_s->global->p2p_long_listen = 0;
7067
7068 /*
7069 * Stop previous find/listen operation to avoid trying to request a new
7070 * remain-on-channel operation while the driver is still running the
7071 * previous one.
7072 */
7073 if (wpa_s->global->p2p)
7074 p2p_stop_find(wpa_s->global->p2p);
7075
7076 res = wpas_p2p_listen_start(wpa_s, timeout * 1000);
7077 if (res == 0 && timeout * 1000 > wpa_s->max_remain_on_chan) {
7078 wpa_s->global->p2p_long_listen = timeout * 1000;
7079 eloop_register_timeout(timeout, 0,
7080 wpas_p2p_long_listen_timeout,
7081 wpa_s, NULL);
7082 }
7083
7084 return res;
7085 }
7086
7087
7088 int wpas_p2p_assoc_req_ie(struct wpa_supplicant *wpa_s, struct wpa_bss *bss,
7089 u8 *buf, size_t len, int p2p_group)
7090 {
7091 struct wpabuf *p2p_ie;
7092 int ret;
7093
7094 if (wpa_s->global->p2p_disabled)
7095 return -1;
7096 /*
7097 * Advertize mandatory cross connection capability even on
7098 * p2p_disabled=1 interface when associating with a P2P Manager WLAN AP.
7099 */
7100 if (wpa_s->conf->p2p_disabled && p2p_group)
7101 return -1;
7102 if (wpa_s->global->p2p == NULL)
7103 return -1;
7104 if (bss == NULL)
7105 return -1;
7106
7107 p2p_ie = wpa_bss_get_vendor_ie_multi(bss, P2P_IE_VENDOR_TYPE);
7108 ret = p2p_assoc_req_ie(wpa_s->global->p2p, bss->bssid, buf, len,
7109 p2p_group, p2p_ie);
7110 wpabuf_free(p2p_ie);
7111
7112 return ret;
7113 }
7114
7115
7116 int wpas_p2p_probe_req_rx(struct wpa_supplicant *wpa_s, const u8 *addr,
7117 const u8 *dst, const u8 *bssid,
7118 const u8 *ie, size_t ie_len,
7119 unsigned int rx_freq, int ssi_signal)
7120 {
7121 if (wpa_s->global->p2p_disabled)
7122 return 0;
7123 if (wpa_s->global->p2p == NULL)
7124 return 0;
7125
7126 switch (p2p_probe_req_rx(wpa_s->global->p2p, addr, dst, bssid,
7127 ie, ie_len, rx_freq, wpa_s->p2p_lo_started)) {
7128 case P2P_PREQ_NOT_P2P:
7129 wpas_notify_preq(wpa_s, addr, dst, bssid, ie, ie_len,
7130 ssi_signal);
7131 /* fall through */
7132 case P2P_PREQ_MALFORMED:
7133 case P2P_PREQ_NOT_LISTEN:
7134 case P2P_PREQ_NOT_PROCESSED:
7135 default: /* make gcc happy */
7136 return 0;
7137 case P2P_PREQ_PROCESSED:
7138 return 1;
7139 }
7140 }
7141
7142
7143 void wpas_p2p_rx_action(struct wpa_supplicant *wpa_s, const u8 *da,
7144 const u8 *sa, const u8 *bssid,
7145 u8 category, const u8 *data, size_t len, int freq)
7146 {
7147 if (wpa_s->global->p2p_disabled)
7148 return;
7149 if (wpa_s->global->p2p == NULL)
7150 return;
7151
7152 p2p_rx_action(wpa_s->global->p2p, da, sa, bssid, category, data, len,
7153 freq);
7154 }
7155
7156
7157 void wpas_p2p_scan_ie(struct wpa_supplicant *wpa_s, struct wpabuf *ies)
7158 {
7159 unsigned int bands;
7160
7161 if (wpa_s->global->p2p_disabled)
7162 return;
7163 if (wpa_s->global->p2p == NULL)
7164 return;
7165
7166 bands = wpas_get_bands(wpa_s, NULL);
7167 p2p_scan_ie(wpa_s->global->p2p, ies, NULL, bands);
7168 }
7169
7170
7171 static void wpas_p2p_group_deinit(struct wpa_supplicant *wpa_s)
7172 {
7173 p2p_group_deinit(wpa_s->p2p_group);
7174 wpa_s->p2p_group = NULL;
7175
7176 wpa_s->ap_configured_cb = NULL;
7177 wpa_s->ap_configured_cb_ctx = NULL;
7178 wpa_s->ap_configured_cb_data = NULL;
7179 wpa_s->connect_without_scan = NULL;
7180 }
7181
7182
7183 int wpas_p2p_reject(struct wpa_supplicant *wpa_s, const u8 *addr)
7184 {
7185 wpa_s->global->p2p_long_listen = 0;
7186
7187 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
7188 return -1;
7189
7190 return p2p_reject(wpa_s->global->p2p, addr);
7191 }
7192
7193
7194 /* Invite to reinvoke a persistent group */
7195 int wpas_p2p_invite(struct wpa_supplicant *wpa_s, const u8 *peer_addr,
7196 struct wpa_ssid *ssid, const u8 *go_dev_addr, int freq,
7197 int vht_center_freq2, int ht40, int vht, int max_chwidth,
7198 int pref_freq, int he, int edmg)
7199 {
7200 enum p2p_invite_role role;
7201 u8 *bssid = NULL;
7202 int force_freq = 0;
7203 int res;
7204 int no_pref_freq_given = pref_freq == 0;
7205 unsigned int pref_freq_list[P2P_MAX_PREF_CHANNELS], size;
7206
7207 wpa_s->global->p2p_invite_group = NULL;
7208 if (peer_addr)
7209 os_memcpy(wpa_s->p2p_auth_invite, peer_addr, ETH_ALEN);
7210 else
7211 os_memset(wpa_s->p2p_auth_invite, 0, ETH_ALEN);
7212
7213 wpa_s->p2p_persistent_go_freq = freq;
7214 wpa_s->p2p_go_ht40 = !!ht40;
7215 wpa_s->p2p_go_vht = !!vht;
7216 wpa_s->p2p_go_he = !!he;
7217 wpa_s->p2p_go_max_oper_chwidth = max_chwidth;
7218 wpa_s->p2p_go_vht_center_freq2 = vht_center_freq2;
7219 wpa_s->p2p_go_edmg = !!edmg;
7220 if (ssid->mode == WPAS_MODE_P2P_GO) {
7221 role = P2P_INVITE_ROLE_GO;
7222 if (peer_addr == NULL) {
7223 wpa_printf(MSG_DEBUG, "P2P: Missing peer "
7224 "address in invitation command");
7225 return -1;
7226 }
7227 if (wpas_p2p_create_iface(wpa_s)) {
7228 if (wpas_p2p_add_group_interface(wpa_s,
7229 WPA_IF_P2P_GO) < 0) {
7230 wpa_printf(MSG_ERROR, "P2P: Failed to "
7231 "allocate a new interface for the "
7232 "group");
7233 return -1;
7234 }
7235 bssid = wpa_s->pending_interface_addr;
7236 } else if (wpa_s->p2p_mgmt)
7237 bssid = wpa_s->parent->own_addr;
7238 else
7239 bssid = wpa_s->own_addr;
7240 } else {
7241 role = P2P_INVITE_ROLE_CLIENT;
7242 peer_addr = ssid->bssid;
7243 }
7244 wpa_s->pending_invite_ssid_id = ssid->id;
7245
7246 size = P2P_MAX_PREF_CHANNELS;
7247 res = wpas_p2p_setup_freqs(wpa_s, freq, &force_freq, &pref_freq,
7248 role == P2P_INVITE_ROLE_GO,
7249 pref_freq_list, &size);
7250 if (res)
7251 return res;
7252
7253 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
7254 return -1;
7255
7256 p2p_set_own_pref_freq_list(wpa_s->global->p2p, pref_freq_list, size);
7257
7258 if (wpa_s->parent->conf->p2p_ignore_shared_freq &&
7259 no_pref_freq_given && pref_freq > 0 &&
7260 wpa_s->num_multichan_concurrent > 1 &&
7261 wpas_p2p_num_unused_channels(wpa_s) > 0) {
7262 wpa_printf(MSG_DEBUG, "P2P: Ignore own channel preference %d MHz for invitation due to p2p_ignore_shared_freq=1 configuration",
7263 pref_freq);
7264 pref_freq = 0;
7265 }
7266
7267 /*
7268 * Stop any find/listen operations before invitation and possibly
7269 * connection establishment.
7270 */
7271 wpas_p2p_stop_find_oper(wpa_s);
7272
7273 return p2p_invite(wpa_s->global->p2p, peer_addr, role, bssid,
7274 ssid->ssid, ssid->ssid_len, force_freq, go_dev_addr,
7275 1, pref_freq, -1);
7276 }
7277
7278
7279 /* Invite to join an active group */
7280 int wpas_p2p_invite_group(struct wpa_supplicant *wpa_s, const char *ifname,
7281 const u8 *peer_addr, const u8 *go_dev_addr)
7282 {
7283 struct wpa_global *global = wpa_s->global;
7284 enum p2p_invite_role role;
7285 u8 *bssid = NULL;
7286 struct wpa_ssid *ssid;
7287 int persistent;
7288 int freq = 0, force_freq = 0, pref_freq = 0;
7289 int res;
7290 unsigned int pref_freq_list[P2P_MAX_PREF_CHANNELS], size;
7291
7292 wpa_s->p2p_persistent_go_freq = 0;
7293 wpa_s->p2p_go_ht40 = 0;
7294 wpa_s->p2p_go_vht = 0;
7295 wpa_s->p2p_go_vht_center_freq2 = 0;
7296 wpa_s->p2p_go_max_oper_chwidth = 0;
7297 wpa_s->p2p_go_edmg = 0;
7298
7299 for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
7300 if (os_strcmp(wpa_s->ifname, ifname) == 0)
7301 break;
7302 }
7303 if (wpa_s == NULL) {
7304 wpa_printf(MSG_DEBUG, "P2P: Interface '%s' not found", ifname);
7305 return -1;
7306 }
7307
7308 ssid = wpa_s->current_ssid;
7309 if (ssid == NULL) {
7310 wpa_printf(MSG_DEBUG, "P2P: No current SSID to use for "
7311 "invitation");
7312 return -1;
7313 }
7314
7315 wpa_s->global->p2p_invite_group = wpa_s;
7316 persistent = ssid->p2p_persistent_group &&
7317 wpas_p2p_get_persistent(wpa_s->p2pdev, peer_addr,
7318 ssid->ssid, ssid->ssid_len);
7319
7320 if (ssid->mode == WPAS_MODE_P2P_GO) {
7321 role = P2P_INVITE_ROLE_ACTIVE_GO;
7322 bssid = wpa_s->own_addr;
7323 if (go_dev_addr == NULL)
7324 go_dev_addr = wpa_s->global->p2p_dev_addr;
7325 freq = ssid->frequency;
7326 } else {
7327 role = P2P_INVITE_ROLE_CLIENT;
7328 if (wpa_s->wpa_state < WPA_ASSOCIATED) {
7329 wpa_printf(MSG_DEBUG, "P2P: Not associated - cannot "
7330 "invite to current group");
7331 return -1;
7332 }
7333 bssid = wpa_s->bssid;
7334 if (go_dev_addr == NULL &&
7335 !is_zero_ether_addr(wpa_s->go_dev_addr))
7336 go_dev_addr = wpa_s->go_dev_addr;
7337 freq = wpa_s->current_bss ? wpa_s->current_bss->freq :
7338 (int) wpa_s->assoc_freq;
7339 }
7340 wpa_s->p2pdev->pending_invite_ssid_id = -1;
7341
7342 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
7343 return -1;
7344
7345 size = P2P_MAX_PREF_CHANNELS;
7346 res = wpas_p2p_setup_freqs(wpa_s, freq, &force_freq, &pref_freq,
7347 role == P2P_INVITE_ROLE_ACTIVE_GO,
7348 pref_freq_list, &size);
7349 if (res)
7350 return res;
7351 wpas_p2p_set_own_freq_preference(wpa_s, force_freq);
7352
7353 return p2p_invite(wpa_s->global->p2p, peer_addr, role, bssid,
7354 ssid->ssid, ssid->ssid_len, force_freq,
7355 go_dev_addr, persistent, pref_freq, -1);
7356 }
7357
7358
7359 void wpas_p2p_completed(struct wpa_supplicant *wpa_s)
7360 {
7361 struct wpa_ssid *ssid = wpa_s->current_ssid;
7362 u8 go_dev_addr[ETH_ALEN];
7363 int persistent;
7364 int freq;
7365 u8 ip[3 * 4], *ip_ptr = NULL;
7366 char ip_addr[100];
7367
7368 if (ssid == NULL || ssid->mode != WPAS_MODE_P2P_GROUP_FORMATION) {
7369 eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
7370 wpa_s->p2pdev, NULL);
7371 }
7372
7373 if (!wpa_s->show_group_started || !ssid)
7374 return;
7375
7376 wpa_s->show_group_started = 0;
7377 if (!wpa_s->p2p_go_group_formation_completed &&
7378 wpa_s->global->p2p_group_formation == wpa_s) {
7379 wpa_dbg(wpa_s, MSG_DEBUG,
7380 "P2P: Marking group formation completed on client on data connection");
7381 wpa_s->p2p_go_group_formation_completed = 1;
7382 wpa_s->global->p2p_group_formation = NULL;
7383 wpa_s->p2p_in_provisioning = 0;
7384 wpa_s->p2p_in_invitation = 0;
7385 }
7386
7387 os_memset(go_dev_addr, 0, ETH_ALEN);
7388 if (ssid->bssid_set)
7389 os_memcpy(go_dev_addr, ssid->bssid, ETH_ALEN);
7390 persistent = wpas_p2p_persistent_group(wpa_s, go_dev_addr, ssid->ssid,
7391 ssid->ssid_len);
7392 os_memcpy(wpa_s->go_dev_addr, go_dev_addr, ETH_ALEN);
7393
7394 if (wpa_s->global->p2p_group_formation == wpa_s)
7395 wpa_s->global->p2p_group_formation = NULL;
7396
7397 freq = wpa_s->current_bss ? wpa_s->current_bss->freq :
7398 (int) wpa_s->assoc_freq;
7399
7400 ip_addr[0] = '\0';
7401 if (wpa_sm_get_p2p_ip_addr(wpa_s->wpa, ip) == 0) {
7402 int res;
7403
7404 res = os_snprintf(ip_addr, sizeof(ip_addr),
7405 " ip_addr=%u.%u.%u.%u "
7406 "ip_mask=%u.%u.%u.%u go_ip_addr=%u.%u.%u.%u",
7407 ip[0], ip[1], ip[2], ip[3],
7408 ip[4], ip[5], ip[6], ip[7],
7409 ip[8], ip[9], ip[10], ip[11]);
7410 if (os_snprintf_error(sizeof(ip_addr), res))
7411 ip_addr[0] = '\0';
7412 ip_ptr = ip;
7413 }
7414
7415 wpas_p2p_group_started(wpa_s, 0, ssid, freq,
7416 ssid->passphrase == NULL && ssid->psk_set ?
7417 ssid->psk : NULL,
7418 ssid->passphrase, go_dev_addr, persistent,
7419 ip_addr);
7420
7421 if (persistent)
7422 wpas_p2p_store_persistent_group(wpa_s->p2pdev,
7423 ssid, go_dev_addr);
7424
7425 wpas_notify_p2p_group_started(wpa_s, ssid, persistent, 1, ip_ptr);
7426 }
7427
7428
7429 int wpas_p2p_presence_req(struct wpa_supplicant *wpa_s, u32 duration1,
7430 u32 interval1, u32 duration2, u32 interval2)
7431 {
7432 int ret;
7433
7434 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
7435 return -1;
7436
7437 if (wpa_s->wpa_state < WPA_ASSOCIATED ||
7438 wpa_s->current_ssid == NULL ||
7439 wpa_s->current_ssid->mode != WPAS_MODE_INFRA)
7440 return -1;
7441
7442 ret = p2p_presence_req(wpa_s->global->p2p, wpa_s->bssid,
7443 wpa_s->own_addr, wpa_s->assoc_freq,
7444 duration1, interval1, duration2, interval2);
7445 if (ret == 0)
7446 wpa_s->waiting_presence_resp = 1;
7447
7448 return ret;
7449 }
7450
7451
7452 int wpas_p2p_ext_listen(struct wpa_supplicant *wpa_s, unsigned int period,
7453 unsigned int interval)
7454 {
7455 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
7456 return -1;
7457
7458 return p2p_ext_listen(wpa_s->global->p2p, period, interval);
7459 }
7460
7461
7462 static int wpas_p2p_is_client(struct wpa_supplicant *wpa_s)
7463 {
7464 if (wpa_s->current_ssid == NULL) {
7465 /*
7466 * current_ssid can be cleared when P2P client interface gets
7467 * disconnected, so assume this interface was used as P2P
7468 * client.
7469 */
7470 return 1;
7471 }
7472 return wpa_s->current_ssid->p2p_group &&
7473 wpa_s->current_ssid->mode == WPAS_MODE_INFRA;
7474 }
7475
7476
7477 static void wpas_p2p_group_idle_timeout(void *eloop_ctx, void *timeout_ctx)
7478 {
7479 struct wpa_supplicant *wpa_s = eloop_ctx;
7480
7481 if (wpa_s->conf->p2p_group_idle == 0 && !wpas_p2p_is_client(wpa_s)) {
7482 wpa_printf(MSG_DEBUG, "P2P: Ignore group idle timeout - "
7483 "disabled");
7484 return;
7485 }
7486
7487 wpa_printf(MSG_DEBUG, "P2P: Group idle timeout reached - terminate "
7488 "group");
7489 wpas_p2p_group_delete(wpa_s, P2P_GROUP_REMOVAL_IDLE_TIMEOUT);
7490 }
7491
7492
7493 static void wpas_p2p_set_group_idle_timeout(struct wpa_supplicant *wpa_s)
7494 {
7495 int timeout;
7496
7497 if (eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL) > 0)
7498 wpa_printf(MSG_DEBUG, "P2P: Cancelled P2P group idle timeout");
7499
7500 if (wpa_s->current_ssid == NULL || !wpa_s->current_ssid->p2p_group)
7501 return;
7502
7503 timeout = wpa_s->conf->p2p_group_idle;
7504 if (wpa_s->current_ssid->mode == WPAS_MODE_INFRA &&
7505 (timeout == 0 || timeout > P2P_MAX_CLIENT_IDLE))
7506 timeout = P2P_MAX_CLIENT_IDLE;
7507
7508 if (timeout == 0)
7509 return;
7510
7511 if (timeout < 0) {
7512 if (wpa_s->current_ssid->mode == WPAS_MODE_INFRA)
7513 timeout = 0; /* special client mode no-timeout */
7514 else
7515 return;
7516 }
7517
7518 if (wpa_s->p2p_in_provisioning) {
7519 /*
7520 * Use the normal group formation timeout during the
7521 * provisioning phase to avoid terminating this process too
7522 * early due to group idle timeout.
7523 */
7524 wpa_printf(MSG_DEBUG, "P2P: Do not use P2P group idle timeout "
7525 "during provisioning");
7526 return;
7527 }
7528
7529 if (wpa_s->show_group_started) {
7530 /*
7531 * Use the normal group formation timeout between the end of
7532 * the provisioning phase and completion of 4-way handshake to
7533 * avoid terminating this process too early due to group idle
7534 * timeout.
7535 */
7536 wpa_printf(MSG_DEBUG, "P2P: Do not use P2P group idle timeout "
7537 "while waiting for initial 4-way handshake to "
7538 "complete");
7539 return;
7540 }
7541
7542 wpa_printf(MSG_DEBUG, "P2P: Set P2P group idle timeout to %u seconds",
7543 timeout);
7544 eloop_register_timeout(timeout, 0, wpas_p2p_group_idle_timeout,
7545 wpa_s, NULL);
7546 }
7547
7548
7549 /* Returns 1 if the interface was removed */
7550 int wpas_p2p_deauth_notif(struct wpa_supplicant *wpa_s, const u8 *bssid,
7551 u16 reason_code, const u8 *ie, size_t ie_len,
7552 int locally_generated)
7553 {
7554 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
7555 return 0;
7556
7557 if (!locally_generated)
7558 p2p_deauth_notif(wpa_s->global->p2p, bssid, reason_code, ie,
7559 ie_len);
7560
7561 if (reason_code == WLAN_REASON_DEAUTH_LEAVING && !locally_generated &&
7562 wpa_s->current_ssid &&
7563 wpa_s->current_ssid->p2p_group &&
7564 wpa_s->current_ssid->mode == WPAS_MODE_INFRA) {
7565 wpa_printf(MSG_DEBUG, "P2P: GO indicated that the P2P Group "
7566 "session is ending");
7567 if (wpas_p2p_group_delete(wpa_s,
7568 P2P_GROUP_REMOVAL_GO_ENDING_SESSION)
7569 > 0)
7570 return 1;
7571 }
7572
7573 return 0;
7574 }
7575
7576
7577 void wpas_p2p_disassoc_notif(struct wpa_supplicant *wpa_s, const u8 *bssid,
7578 u16 reason_code, const u8 *ie, size_t ie_len,
7579 int locally_generated)
7580 {
7581 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
7582 return;
7583
7584 if (!locally_generated)
7585 p2p_disassoc_notif(wpa_s->global->p2p, bssid, reason_code, ie,
7586 ie_len);
7587 }
7588
7589
7590 void wpas_p2p_update_config(struct wpa_supplicant *wpa_s)
7591 {
7592 struct p2p_data *p2p = wpa_s->global->p2p;
7593
7594 if (p2p == NULL)
7595 return;
7596
7597 if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_CAPABLE))
7598 return;
7599
7600 if (wpa_s->conf->changed_parameters & CFG_CHANGED_DEVICE_NAME)
7601 p2p_set_dev_name(p2p, wpa_s->conf->device_name);
7602
7603 if (wpa_s->conf->changed_parameters & CFG_CHANGED_DEVICE_TYPE)
7604 p2p_set_pri_dev_type(p2p, wpa_s->conf->device_type);
7605
7606 if (wpa_s->wps &&
7607 (wpa_s->conf->changed_parameters & CFG_CHANGED_CONFIG_METHODS))
7608 p2p_set_config_methods(p2p, wpa_s->wps->config_methods);
7609
7610 if (wpa_s->wps && (wpa_s->conf->changed_parameters & CFG_CHANGED_UUID))
7611 p2p_set_uuid(p2p, wpa_s->wps->uuid);
7612
7613 if (wpa_s->conf->changed_parameters & CFG_CHANGED_WPS_STRING) {
7614 p2p_set_manufacturer(p2p, wpa_s->conf->manufacturer);
7615 p2p_set_model_name(p2p, wpa_s->conf->model_name);
7616 p2p_set_model_number(p2p, wpa_s->conf->model_number);
7617 p2p_set_serial_number(p2p, wpa_s->conf->serial_number);
7618 }
7619
7620 if (wpa_s->conf->changed_parameters & CFG_CHANGED_SEC_DEVICE_TYPE)
7621 p2p_set_sec_dev_types(p2p,
7622 (void *) wpa_s->conf->sec_device_type,
7623 wpa_s->conf->num_sec_device_types);
7624
7625 if (wpa_s->conf->changed_parameters & CFG_CHANGED_VENDOR_EXTENSION) {
7626 int i;
7627 p2p_remove_wps_vendor_extensions(p2p);
7628 for (i = 0; i < MAX_WPS_VENDOR_EXT; i++) {
7629 if (wpa_s->conf->wps_vendor_ext[i] == NULL)
7630 continue;
7631 p2p_add_wps_vendor_extension(
7632 p2p, wpa_s->conf->wps_vendor_ext[i]);
7633 }
7634 }
7635
7636 if ((wpa_s->conf->changed_parameters & CFG_CHANGED_COUNTRY) &&
7637 wpa_s->conf->country[0] && wpa_s->conf->country[1]) {
7638 char country[3];
7639 country[0] = wpa_s->conf->country[0];
7640 country[1] = wpa_s->conf->country[1];
7641 country[2] = 0x04;
7642 p2p_set_country(p2p, country);
7643 }
7644
7645 if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_SSID_POSTFIX) {
7646 p2p_set_ssid_postfix(p2p, (u8 *) wpa_s->conf->p2p_ssid_postfix,
7647 wpa_s->conf->p2p_ssid_postfix ?
7648 os_strlen(wpa_s->conf->p2p_ssid_postfix) :
7649 0);
7650 }
7651
7652 if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_INTRA_BSS)
7653 p2p_set_intra_bss_dist(p2p, wpa_s->conf->p2p_intra_bss);
7654
7655 if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_LISTEN_CHANNEL) {
7656 u8 reg_class, channel;
7657 int ret;
7658 unsigned int r;
7659 u8 channel_forced;
7660
7661 if (wpa_s->conf->p2p_listen_reg_class &&
7662 wpa_s->conf->p2p_listen_channel) {
7663 reg_class = wpa_s->conf->p2p_listen_reg_class;
7664 channel = wpa_s->conf->p2p_listen_channel;
7665 channel_forced = 1;
7666 } else {
7667 reg_class = 81;
7668 /*
7669 * Pick one of the social channels randomly as the
7670 * listen channel.
7671 */
7672 if (os_get_random((u8 *) &r, sizeof(r)) < 0)
7673 channel = 1;
7674 else
7675 channel = 1 + (r % 3) * 5;
7676 channel_forced = 0;
7677 }
7678 ret = p2p_set_listen_channel(p2p, reg_class, channel,
7679 channel_forced);
7680 if (ret)
7681 wpa_printf(MSG_ERROR, "P2P: Own listen channel update "
7682 "failed: %d", ret);
7683 }
7684 if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_OPER_CHANNEL) {
7685 u8 op_reg_class, op_channel, cfg_op_channel;
7686 int ret = 0;
7687 unsigned int r;
7688 if (wpa_s->conf->p2p_oper_reg_class &&
7689 wpa_s->conf->p2p_oper_channel) {
7690 op_reg_class = wpa_s->conf->p2p_oper_reg_class;
7691 op_channel = wpa_s->conf->p2p_oper_channel;
7692 cfg_op_channel = 1;
7693 } else {
7694 op_reg_class = 81;
7695 /*
7696 * Use random operation channel from (1, 6, 11)
7697 *if no other preference is indicated.
7698 */
7699 if (os_get_random((u8 *) &r, sizeof(r)) < 0)
7700 op_channel = 1;
7701 else
7702 op_channel = 1 + (r % 3) * 5;
7703 cfg_op_channel = 0;
7704 }
7705 ret = p2p_set_oper_channel(p2p, op_reg_class, op_channel,
7706 cfg_op_channel);
7707 if (ret)
7708 wpa_printf(MSG_ERROR, "P2P: Own oper channel update "
7709 "failed: %d", ret);
7710 }
7711
7712 if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_PREF_CHAN) {
7713 if (p2p_set_pref_chan(p2p, wpa_s->conf->num_p2p_pref_chan,
7714 wpa_s->conf->p2p_pref_chan) < 0) {
7715 wpa_printf(MSG_ERROR, "P2P: Preferred channel list "
7716 "update failed");
7717 }
7718
7719 if (p2p_set_no_go_freq(p2p, &wpa_s->conf->p2p_no_go_freq) < 0) {
7720 wpa_printf(MSG_ERROR, "P2P: No GO channel list "
7721 "update failed");
7722 }
7723 }
7724
7725 if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_PASSPHRASE_LEN)
7726 p2p_set_passphrase_len(p2p, wpa_s->conf->p2p_passphrase_len);
7727 }
7728
7729
7730 int wpas_p2p_set_noa(struct wpa_supplicant *wpa_s, u8 count, int start,
7731 int duration)
7732 {
7733 if (!wpa_s->ap_iface)
7734 return -1;
7735 return hostapd_p2p_set_noa(wpa_s->ap_iface->bss[0], count, start,
7736 duration);
7737 }
7738
7739
7740 int wpas_p2p_set_cross_connect(struct wpa_supplicant *wpa_s, int enabled)
7741 {
7742 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
7743 return -1;
7744
7745 wpa_s->global->cross_connection = enabled;
7746 p2p_set_cross_connect(wpa_s->global->p2p, enabled);
7747
7748 if (!enabled) {
7749 struct wpa_supplicant *iface;
7750
7751 for (iface = wpa_s->global->ifaces; iface; iface = iface->next)
7752 {
7753 if (iface->cross_connect_enabled == 0)
7754 continue;
7755
7756 iface->cross_connect_enabled = 0;
7757 iface->cross_connect_in_use = 0;
7758 wpa_msg_global(iface->p2pdev, MSG_INFO,
7759 P2P_EVENT_CROSS_CONNECT_DISABLE "%s %s",
7760 iface->ifname,
7761 iface->cross_connect_uplink);
7762 }
7763 }
7764
7765 return 0;
7766 }
7767
7768
7769 static void wpas_p2p_enable_cross_connect(struct wpa_supplicant *uplink)
7770 {
7771 struct wpa_supplicant *iface;
7772
7773 if (!uplink->global->cross_connection)
7774 return;
7775
7776 for (iface = uplink->global->ifaces; iface; iface = iface->next) {
7777 if (!iface->cross_connect_enabled)
7778 continue;
7779 if (os_strcmp(uplink->ifname, iface->cross_connect_uplink) !=
7780 0)
7781 continue;
7782 if (iface->ap_iface == NULL)
7783 continue;
7784 if (iface->cross_connect_in_use)
7785 continue;
7786
7787 iface->cross_connect_in_use = 1;
7788 wpa_msg_global(iface->p2pdev, MSG_INFO,
7789 P2P_EVENT_CROSS_CONNECT_ENABLE "%s %s",
7790 iface->ifname, iface->cross_connect_uplink);
7791 }
7792 }
7793
7794
7795 static void wpas_p2p_disable_cross_connect(struct wpa_supplicant *uplink)
7796 {
7797 struct wpa_supplicant *iface;
7798
7799 for (iface = uplink->global->ifaces; iface; iface = iface->next) {
7800 if (!iface->cross_connect_enabled)
7801 continue;
7802 if (os_strcmp(uplink->ifname, iface->cross_connect_uplink) !=
7803 0)
7804 continue;
7805 if (!iface->cross_connect_in_use)
7806 continue;
7807
7808 wpa_msg_global(iface->p2pdev, MSG_INFO,
7809 P2P_EVENT_CROSS_CONNECT_DISABLE "%s %s",
7810 iface->ifname, iface->cross_connect_uplink);
7811 iface->cross_connect_in_use = 0;
7812 }
7813 }
7814
7815
7816 void wpas_p2p_notif_connected(struct wpa_supplicant *wpa_s)
7817 {
7818 if (wpa_s->ap_iface || wpa_s->current_ssid == NULL ||
7819 wpa_s->current_ssid->mode != WPAS_MODE_INFRA ||
7820 wpa_s->cross_connect_disallowed)
7821 wpas_p2p_disable_cross_connect(wpa_s);
7822 else
7823 wpas_p2p_enable_cross_connect(wpa_s);
7824 if (!wpa_s->ap_iface &&
7825 eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL) > 0)
7826 wpa_printf(MSG_DEBUG, "P2P: Cancelled P2P group idle timeout");
7827 }
7828
7829
7830 void wpas_p2p_notif_disconnected(struct wpa_supplicant *wpa_s)
7831 {
7832 wpas_p2p_disable_cross_connect(wpa_s);
7833 if (!wpa_s->ap_iface &&
7834 !eloop_is_timeout_registered(wpas_p2p_group_idle_timeout,
7835 wpa_s, NULL))
7836 wpas_p2p_set_group_idle_timeout(wpa_s);
7837 }
7838
7839
7840 static void wpas_p2p_cross_connect_setup(struct wpa_supplicant *wpa_s)
7841 {
7842 struct wpa_supplicant *iface;
7843
7844 if (!wpa_s->global->cross_connection)
7845 return;
7846
7847 for (iface = wpa_s->global->ifaces; iface; iface = iface->next) {
7848 if (iface == wpa_s)
7849 continue;
7850 if (iface->drv_flags &
7851 WPA_DRIVER_FLAGS_P2P_DEDICATED_INTERFACE)
7852 continue;
7853 if ((iface->drv_flags & WPA_DRIVER_FLAGS_P2P_CAPABLE) &&
7854 iface != wpa_s->parent)
7855 continue;
7856
7857 wpa_s->cross_connect_enabled = 1;
7858 os_strlcpy(wpa_s->cross_connect_uplink, iface->ifname,
7859 sizeof(wpa_s->cross_connect_uplink));
7860 wpa_printf(MSG_DEBUG, "P2P: Enable cross connection from "
7861 "%s to %s whenever uplink is available",
7862 wpa_s->ifname, wpa_s->cross_connect_uplink);
7863
7864 if (iface->ap_iface || iface->current_ssid == NULL ||
7865 iface->current_ssid->mode != WPAS_MODE_INFRA ||
7866 iface->cross_connect_disallowed ||
7867 iface->wpa_state != WPA_COMPLETED)
7868 break;
7869
7870 wpa_s->cross_connect_in_use = 1;
7871 wpa_msg_global(wpa_s->p2pdev, MSG_INFO,
7872 P2P_EVENT_CROSS_CONNECT_ENABLE "%s %s",
7873 wpa_s->ifname, wpa_s->cross_connect_uplink);
7874 break;
7875 }
7876 }
7877
7878
7879 int wpas_p2p_notif_pbc_overlap(struct wpa_supplicant *wpa_s)
7880 {
7881 if (wpa_s->p2p_group_interface != P2P_GROUP_INTERFACE_CLIENT &&
7882 !wpa_s->p2p_in_provisioning)
7883 return 0; /* not P2P client operation */
7884
7885 wpa_printf(MSG_DEBUG, "P2P: Terminate connection due to WPS PBC "
7886 "session overlap");
7887 if (wpa_s != wpa_s->p2pdev)
7888 wpa_msg_ctrl(wpa_s->p2pdev, MSG_INFO, WPS_EVENT_OVERLAP);
7889 wpas_p2p_group_formation_failed(wpa_s, 0);
7890 return 1;
7891 }
7892
7893
7894 void wpas_p2p_pbc_overlap_cb(void *eloop_ctx, void *timeout_ctx)
7895 {
7896 struct wpa_supplicant *wpa_s = eloop_ctx;
7897 wpas_p2p_notif_pbc_overlap(wpa_s);
7898 }
7899
7900
7901 void wpas_p2p_update_channel_list(struct wpa_supplicant *wpa_s,
7902 enum wpas_p2p_channel_update_trig trig)
7903 {
7904 struct p2p_channels chan, cli_chan;
7905 struct wpa_used_freq_data *freqs = NULL;
7906 unsigned int num = wpa_s->num_multichan_concurrent;
7907
7908 if (wpa_s->global == NULL || wpa_s->global->p2p == NULL)
7909 return;
7910
7911 freqs = os_calloc(num, sizeof(struct wpa_used_freq_data));
7912 if (!freqs)
7913 return;
7914
7915 num = get_shared_radio_freqs_data(wpa_s, freqs, num);
7916
7917 os_memset(&chan, 0, sizeof(chan));
7918 os_memset(&cli_chan, 0, sizeof(cli_chan));
7919 if (wpas_p2p_setup_channels(wpa_s, &chan, &cli_chan)) {
7920 wpa_printf(MSG_ERROR, "P2P: Failed to update supported "
7921 "channel list");
7922 return;
7923 }
7924
7925 p2p_update_channel_list(wpa_s->global->p2p, &chan, &cli_chan);
7926
7927 wpas_p2p_optimize_listen_channel(wpa_s, freqs, num);
7928
7929 /*
7930 * The used frequencies map changed, so it is possible that a GO is
7931 * using a channel that is no longer valid for P2P use. It is also
7932 * possible that due to policy consideration, it would be preferable to
7933 * move it to a frequency already used by other station interfaces.
7934 */
7935 wpas_p2p_consider_moving_gos(wpa_s, freqs, num, trig);
7936
7937 os_free(freqs);
7938 }
7939
7940
7941 static void wpas_p2p_scan_res_ignore(struct wpa_supplicant *wpa_s,
7942 struct wpa_scan_results *scan_res)
7943 {
7944 wpa_printf(MSG_DEBUG, "P2P: Ignore scan results");
7945 }
7946
7947
7948 int wpas_p2p_cancel(struct wpa_supplicant *wpa_s)
7949 {
7950 struct wpa_global *global = wpa_s->global;
7951 int found = 0;
7952 const u8 *peer;
7953
7954 if (global->p2p == NULL)
7955 return -1;
7956
7957 wpa_printf(MSG_DEBUG, "P2P: Request to cancel group formation");
7958
7959 if (wpa_s->pending_interface_name[0] &&
7960 !is_zero_ether_addr(wpa_s->pending_interface_addr))
7961 found = 1;
7962
7963 peer = p2p_get_go_neg_peer(global->p2p);
7964 if (peer) {
7965 wpa_printf(MSG_DEBUG, "P2P: Unauthorize pending GO Neg peer "
7966 MACSTR, MAC2STR(peer));
7967 p2p_unauthorize(global->p2p, peer);
7968 found = 1;
7969 }
7970
7971 if (wpa_s->scan_res_handler == wpas_p2p_scan_res_join) {
7972 wpa_printf(MSG_DEBUG, "P2P: Stop pending scan for join");
7973 wpa_s->scan_res_handler = wpas_p2p_scan_res_ignore;
7974 found = 1;
7975 }
7976
7977 if (wpa_s->pending_pd_before_join) {
7978 wpa_printf(MSG_DEBUG, "P2P: Stop pending PD before join");
7979 wpa_s->pending_pd_before_join = 0;
7980 found = 1;
7981 }
7982
7983 wpas_p2p_stop_find(wpa_s);
7984
7985 for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
7986 if (wpa_s == global->p2p_group_formation &&
7987 (wpa_s->p2p_in_provisioning ||
7988 wpa_s->parent->pending_interface_type ==
7989 WPA_IF_P2P_CLIENT)) {
7990 wpa_printf(MSG_DEBUG, "P2P: Interface %s in group "
7991 "formation found - cancelling",
7992 wpa_s->ifname);
7993 found = 1;
7994 eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
7995 wpa_s->p2pdev, NULL);
7996 if (wpa_s->p2p_in_provisioning) {
7997 wpas_group_formation_completed(wpa_s, 0, 0);
7998 break;
7999 }
8000 wpas_p2p_group_delete(wpa_s,
8001 P2P_GROUP_REMOVAL_REQUESTED);
8002 break;
8003 } else if (wpa_s->p2p_in_invitation) {
8004 wpa_printf(MSG_DEBUG, "P2P: Interface %s in invitation found - cancelling",
8005 wpa_s->ifname);
8006 found = 1;
8007 wpas_p2p_group_formation_failed(wpa_s, 0);
8008 break;
8009 }
8010 }
8011
8012 if (!found) {
8013 wpa_printf(MSG_DEBUG, "P2P: No ongoing group formation found");
8014 return -1;
8015 }
8016
8017 return 0;
8018 }
8019
8020
8021 void wpas_p2p_interface_unavailable(struct wpa_supplicant *wpa_s)
8022 {
8023 if (wpa_s->current_ssid == NULL || !wpa_s->current_ssid->p2p_group)
8024 return;
8025
8026 wpa_printf(MSG_DEBUG, "P2P: Remove group due to driver resource not "
8027 "being available anymore");
8028 wpas_p2p_group_delete(wpa_s, P2P_GROUP_REMOVAL_UNAVAILABLE);
8029 }
8030
8031
8032 void wpas_p2p_update_best_channels(struct wpa_supplicant *wpa_s,
8033 int freq_24, int freq_5, int freq_overall)
8034 {
8035 struct p2p_data *p2p = wpa_s->global->p2p;
8036 if (p2p == NULL)
8037 return;
8038 p2p_set_best_channels(p2p, freq_24, freq_5, freq_overall);
8039 }
8040
8041
8042 int wpas_p2p_unauthorize(struct wpa_supplicant *wpa_s, const char *addr)
8043 {
8044 u8 peer[ETH_ALEN];
8045 struct p2p_data *p2p = wpa_s->global->p2p;
8046
8047 if (p2p == NULL)
8048 return -1;
8049
8050 if (hwaddr_aton(addr, peer))
8051 return -1;
8052
8053 return p2p_unauthorize(p2p, peer);
8054 }
8055
8056
8057 /**
8058 * wpas_p2p_disconnect - Disconnect from a P2P Group
8059 * @wpa_s: Pointer to wpa_supplicant data
8060 * Returns: 0 on success, -1 on failure
8061 *
8062 * This can be used to disconnect from a group in which the local end is a P2P
8063 * Client or to end a P2P Group in case the local end is the Group Owner. If a
8064 * virtual network interface was created for this group, that interface will be
8065 * removed. Otherwise, only the configured P2P group network will be removed
8066 * from the interface.
8067 */
8068 int wpas_p2p_disconnect(struct wpa_supplicant *wpa_s)
8069 {
8070
8071 if (wpa_s == NULL)
8072 return -1;
8073
8074 return wpas_p2p_group_delete(wpa_s, P2P_GROUP_REMOVAL_REQUESTED) < 0 ?
8075 -1 : 0;
8076 }
8077
8078
8079 int wpas_p2p_in_progress(struct wpa_supplicant *wpa_s)
8080 {
8081 int ret;
8082
8083 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
8084 return 0;
8085
8086 ret = p2p_in_progress(wpa_s->global->p2p);
8087 if (ret == 0) {
8088 /*
8089 * Check whether there is an ongoing WPS provisioning step (or
8090 * other parts of group formation) on another interface since
8091 * p2p_in_progress() does not report this to avoid issues for
8092 * scans during such provisioning step.
8093 */
8094 if (wpa_s->global->p2p_group_formation &&
8095 wpa_s->global->p2p_group_formation != wpa_s) {
8096 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Another interface (%s) "
8097 "in group formation",
8098 wpa_s->global->p2p_group_formation->ifname);
8099 ret = 1;
8100 }
8101 }
8102
8103 if (!ret && wpa_s->global->p2p_go_wait_client.sec) {
8104 struct os_reltime now;
8105 os_get_reltime(&now);
8106 if (os_reltime_expired(&now, &wpa_s->global->p2p_go_wait_client,
8107 P2P_MAX_INITIAL_CONN_WAIT_GO)) {
8108 /* Wait for the first client has expired */
8109 wpa_s->global->p2p_go_wait_client.sec = 0;
8110 } else {
8111 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Waiting for initial client connection during group formation");
8112 ret = 1;
8113 }
8114 }
8115
8116 return ret;
8117 }
8118
8119
8120 void wpas_p2p_network_removed(struct wpa_supplicant *wpa_s,
8121 struct wpa_ssid *ssid)
8122 {
8123 if (wpa_s->p2p_in_provisioning && ssid->p2p_group &&
8124 eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
8125 wpa_s->p2pdev, NULL) > 0) {
8126 /**
8127 * Remove the network by scheduling the group formation
8128 * timeout to happen immediately. The teardown code
8129 * needs to be scheduled to run asynch later so that we
8130 * don't delete data from under ourselves unexpectedly.
8131 * Calling wpas_p2p_group_formation_timeout directly
8132 * causes a series of crashes in WPS failure scenarios.
8133 */
8134 wpa_printf(MSG_DEBUG, "P2P: Canceled group formation due to "
8135 "P2P group network getting removed");
8136 eloop_register_timeout(0, 0, wpas_p2p_group_formation_timeout,
8137 wpa_s->p2pdev, NULL);
8138 }
8139 }
8140
8141
8142 struct wpa_ssid * wpas_p2p_get_persistent(struct wpa_supplicant *wpa_s,
8143 const u8 *addr, const u8 *ssid,
8144 size_t ssid_len)
8145 {
8146 struct wpa_ssid *s;
8147 size_t i;
8148
8149 for (s = wpa_s->conf->ssid; s; s = s->next) {
8150 if (s->disabled != 2)
8151 continue;
8152 if (ssid &&
8153 (ssid_len != s->ssid_len ||
8154 os_memcmp(ssid, s->ssid, ssid_len) != 0))
8155 continue;
8156 if (addr == NULL) {
8157 if (s->mode == WPAS_MODE_P2P_GO)
8158 return s;
8159 continue;
8160 }
8161 if (os_memcmp(s->bssid, addr, ETH_ALEN) == 0)
8162 return s; /* peer is GO in the persistent group */
8163 if (s->mode != WPAS_MODE_P2P_GO || s->p2p_client_list == NULL)
8164 continue;
8165 for (i = 0; i < s->num_p2p_clients; i++) {
8166 if (os_memcmp(s->p2p_client_list + i * 2 * ETH_ALEN,
8167 addr, ETH_ALEN) == 0)
8168 return s; /* peer is P2P client in persistent
8169 * group */
8170 }
8171 }
8172
8173 return NULL;
8174 }
8175
8176
8177 void wpas_p2p_notify_ap_sta_authorized(struct wpa_supplicant *wpa_s,
8178 const u8 *addr)
8179 {
8180 if (eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
8181 wpa_s->p2pdev, NULL) > 0) {
8182 /*
8183 * This can happen if WPS provisioning step is not terminated
8184 * cleanly (e.g., P2P Client does not send WSC_Done). Since the
8185 * peer was able to connect, there is no need to time out group
8186 * formation after this, though. In addition, this is used with
8187 * the initial connection wait on the GO as a separate formation
8188 * timeout and as such, expected to be hit after the initial WPS
8189 * provisioning step.
8190 */
8191 wpa_printf(MSG_DEBUG, "P2P: Canceled P2P group formation timeout on data connection");
8192
8193 if (!wpa_s->p2p_go_group_formation_completed &&
8194 !wpa_s->group_formation_reported) {
8195 /*
8196 * GO has not yet notified group formation success since
8197 * the WPS step was not completed cleanly. Do that
8198 * notification now since the P2P Client was able to
8199 * connect and as such, must have received the
8200 * credential from the WPS step.
8201 */
8202 if (wpa_s->global->p2p)
8203 p2p_wps_success_cb(wpa_s->global->p2p, addr);
8204 wpas_group_formation_completed(wpa_s, 1, 0);
8205 }
8206 }
8207 if (!wpa_s->p2p_go_group_formation_completed) {
8208 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Marking group formation completed on GO on first data connection");
8209 wpa_s->p2p_go_group_formation_completed = 1;
8210 wpa_s->global->p2p_group_formation = NULL;
8211 wpa_s->p2p_in_provisioning = 0;
8212 wpa_s->p2p_in_invitation = 0;
8213 }
8214 wpa_s->global->p2p_go_wait_client.sec = 0;
8215 if (addr == NULL)
8216 return;
8217 wpas_p2p_add_persistent_group_client(wpa_s, addr);
8218 }
8219
8220
8221 static int wpas_p2p_fallback_to_go_neg(struct wpa_supplicant *wpa_s,
8222 int group_added)
8223 {
8224 struct wpa_supplicant *group = wpa_s;
8225 int ret = 0;
8226
8227 if (wpa_s->global->p2p_group_formation)
8228 group = wpa_s->global->p2p_group_formation;
8229 wpa_s = wpa_s->global->p2p_init_wpa_s;
8230 offchannel_send_action_done(wpa_s);
8231 if (group_added)
8232 ret = wpas_p2p_group_delete(group, P2P_GROUP_REMOVAL_SILENT);
8233 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Fall back to GO Negotiation");
8234 wpas_p2p_connect(wpa_s, wpa_s->pending_join_dev_addr, wpa_s->p2p_pin,
8235 wpa_s->p2p_wps_method, wpa_s->p2p_persistent_group, 0,
8236 0, 0, wpa_s->p2p_go_intent, wpa_s->p2p_connect_freq,
8237 wpa_s->p2p_go_vht_center_freq2,
8238 wpa_s->p2p_persistent_id,
8239 wpa_s->p2p_pd_before_go_neg,
8240 wpa_s->p2p_go_ht40,
8241 wpa_s->p2p_go_vht,
8242 wpa_s->p2p_go_max_oper_chwidth,
8243 wpa_s->p2p_go_he,
8244 wpa_s->p2p_go_edmg,
8245 NULL, 0);
8246 return ret;
8247 }
8248
8249
8250 int wpas_p2p_scan_no_go_seen(struct wpa_supplicant *wpa_s)
8251 {
8252 int res;
8253
8254 if (!wpa_s->p2p_fallback_to_go_neg ||
8255 wpa_s->p2p_in_provisioning <= 5)
8256 return 0;
8257
8258 if (wpas_p2p_peer_go(wpa_s, wpa_s->pending_join_dev_addr) > 0)
8259 return 0; /* peer operating as a GO */
8260
8261 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: GO not found for p2p_connect-auto - "
8262 "fallback to GO Negotiation");
8263 wpa_msg_global(wpa_s->p2pdev, MSG_INFO, P2P_EVENT_FALLBACK_TO_GO_NEG
8264 "reason=GO-not-found");
8265 res = wpas_p2p_fallback_to_go_neg(wpa_s, 1);
8266
8267 return res == 1 ? 2 : 1;
8268 }
8269
8270
8271 unsigned int wpas_p2p_search_delay(struct wpa_supplicant *wpa_s)
8272 {
8273 struct wpa_supplicant *ifs;
8274
8275 if (wpa_s->wpa_state > WPA_SCANNING) {
8276 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Use %u ms search delay due to "
8277 "concurrent operation",
8278 wpa_s->conf->p2p_search_delay);
8279 return wpa_s->conf->p2p_search_delay;
8280 }
8281
8282 dl_list_for_each(ifs, &wpa_s->radio->ifaces, struct wpa_supplicant,
8283 radio_list) {
8284 if (ifs != wpa_s && ifs->wpa_state > WPA_SCANNING) {
8285 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Use %u ms search "
8286 "delay due to concurrent operation on "
8287 "interface %s",
8288 wpa_s->conf->p2p_search_delay,
8289 ifs->ifname);
8290 return wpa_s->conf->p2p_search_delay;
8291 }
8292 }
8293
8294 return 0;
8295 }
8296
8297
8298 static int wpas_p2p_remove_psk_entry(struct wpa_supplicant *wpa_s,
8299 struct wpa_ssid *s, const u8 *addr,
8300 int iface_addr)
8301 {
8302 struct psk_list_entry *psk, *tmp;
8303 int changed = 0;
8304
8305 dl_list_for_each_safe(psk, tmp, &s->psk_list, struct psk_list_entry,
8306 list) {
8307 if ((iface_addr && !psk->p2p &&
8308 os_memcmp(addr, psk->addr, ETH_ALEN) == 0) ||
8309 (!iface_addr && psk->p2p &&
8310 os_memcmp(addr, psk->addr, ETH_ALEN) == 0)) {
8311 wpa_dbg(wpa_s, MSG_DEBUG,
8312 "P2P: Remove persistent group PSK list entry for "
8313 MACSTR " p2p=%u",
8314 MAC2STR(psk->addr), psk->p2p);
8315 dl_list_del(&psk->list);
8316 os_free(psk);
8317 changed++;
8318 }
8319 }
8320
8321 return changed;
8322 }
8323
8324
8325 void wpas_p2p_new_psk_cb(struct wpa_supplicant *wpa_s, const u8 *mac_addr,
8326 const u8 *p2p_dev_addr,
8327 const u8 *psk, size_t psk_len)
8328 {
8329 struct wpa_ssid *ssid = wpa_s->current_ssid;
8330 struct wpa_ssid *persistent;
8331 struct psk_list_entry *p, *last;
8332
8333 if (psk_len != sizeof(p->psk))
8334 return;
8335
8336 if (p2p_dev_addr) {
8337 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: New PSK for addr=" MACSTR
8338 " p2p_dev_addr=" MACSTR,
8339 MAC2STR(mac_addr), MAC2STR(p2p_dev_addr));
8340 if (is_zero_ether_addr(p2p_dev_addr))
8341 p2p_dev_addr = NULL;
8342 } else {
8343 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: New PSK for addr=" MACSTR,
8344 MAC2STR(mac_addr));
8345 }
8346
8347 if (ssid->mode == WPAS_MODE_P2P_GROUP_FORMATION) {
8348 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: new_psk_cb during group formation");
8349 /* To be added to persistent group once created */
8350 if (wpa_s->global->add_psk == NULL) {
8351 wpa_s->global->add_psk = os_zalloc(sizeof(*p));
8352 if (wpa_s->global->add_psk == NULL)
8353 return;
8354 }
8355 p = wpa_s->global->add_psk;
8356 if (p2p_dev_addr) {
8357 p->p2p = 1;
8358 os_memcpy(p->addr, p2p_dev_addr, ETH_ALEN);
8359 } else {
8360 p->p2p = 0;
8361 os_memcpy(p->addr, mac_addr, ETH_ALEN);
8362 }
8363 os_memcpy(p->psk, psk, psk_len);
8364 return;
8365 }
8366
8367 if (ssid->mode != WPAS_MODE_P2P_GO || !ssid->p2p_persistent_group) {
8368 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Ignore new_psk_cb on not-persistent GO");
8369 return;
8370 }
8371
8372 persistent = wpas_p2p_get_persistent(wpa_s->p2pdev, NULL, ssid->ssid,
8373 ssid->ssid_len);
8374 if (!persistent) {
8375 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Could not find persistent group information to store the new PSK");
8376 return;
8377 }
8378
8379 p = os_zalloc(sizeof(*p));
8380 if (p == NULL)
8381 return;
8382 if (p2p_dev_addr) {
8383 p->p2p = 1;
8384 os_memcpy(p->addr, p2p_dev_addr, ETH_ALEN);
8385 } else {
8386 p->p2p = 0;
8387 os_memcpy(p->addr, mac_addr, ETH_ALEN);
8388 }
8389 os_memcpy(p->psk, psk, psk_len);
8390
8391 if (dl_list_len(&persistent->psk_list) > P2P_MAX_STORED_CLIENTS &&
8392 (last = dl_list_last(&persistent->psk_list,
8393 struct psk_list_entry, list))) {
8394 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Remove oldest PSK entry for "
8395 MACSTR " (p2p=%u) to make room for a new one",
8396 MAC2STR(last->addr), last->p2p);
8397 dl_list_del(&last->list);
8398 os_free(last);
8399 }
8400
8401 wpas_p2p_remove_psk_entry(wpa_s->p2pdev, persistent,
8402 p2p_dev_addr ? p2p_dev_addr : mac_addr,
8403 p2p_dev_addr == NULL);
8404 if (p2p_dev_addr) {
8405 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Add new PSK for p2p_dev_addr="
8406 MACSTR, MAC2STR(p2p_dev_addr));
8407 } else {
8408 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Add new PSK for addr=" MACSTR,
8409 MAC2STR(mac_addr));
8410 }
8411 dl_list_add(&persistent->psk_list, &p->list);
8412
8413 if (wpa_s->p2pdev->conf->update_config &&
8414 wpa_config_write(wpa_s->p2pdev->confname, wpa_s->p2pdev->conf))
8415 wpa_printf(MSG_DEBUG, "P2P: Failed to update configuration");
8416 }
8417
8418
8419 static void wpas_p2p_remove_psk(struct wpa_supplicant *wpa_s,
8420 struct wpa_ssid *s, const u8 *addr,
8421 int iface_addr)
8422 {
8423 int res;
8424
8425 res = wpas_p2p_remove_psk_entry(wpa_s, s, addr, iface_addr);
8426 if (res > 0 && wpa_s->conf->update_config &&
8427 wpa_config_write(wpa_s->confname, wpa_s->conf))
8428 wpa_dbg(wpa_s, MSG_DEBUG,
8429 "P2P: Failed to update configuration");
8430 }
8431
8432
8433 static void wpas_p2p_remove_client_go(struct wpa_supplicant *wpa_s,
8434 const u8 *peer, int iface_addr)
8435 {
8436 struct hostapd_data *hapd;
8437 struct hostapd_wpa_psk *psk, *prev, *rem;
8438 struct sta_info *sta;
8439
8440 if (wpa_s->ap_iface == NULL || wpa_s->current_ssid == NULL ||
8441 wpa_s->current_ssid->mode != WPAS_MODE_P2P_GO)
8442 return;
8443
8444 /* Remove per-station PSK entry */
8445 hapd = wpa_s->ap_iface->bss[0];
8446 prev = NULL;
8447 psk = hapd->conf->ssid.wpa_psk;
8448 while (psk) {
8449 if ((iface_addr && os_memcmp(peer, psk->addr, ETH_ALEN) == 0) ||
8450 (!iface_addr &&
8451 os_memcmp(peer, psk->p2p_dev_addr, ETH_ALEN) == 0)) {
8452 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Remove operating group PSK entry for "
8453 MACSTR " iface_addr=%d",
8454 MAC2STR(peer), iface_addr);
8455 if (prev)
8456 prev->next = psk->next;
8457 else
8458 hapd->conf->ssid.wpa_psk = psk->next;
8459 rem = psk;
8460 psk = psk->next;
8461 os_free(rem);
8462 } else {
8463 prev = psk;
8464 psk = psk->next;
8465 }
8466 }
8467
8468 /* Disconnect from group */
8469 if (iface_addr)
8470 sta = ap_get_sta(hapd, peer);
8471 else
8472 sta = ap_get_sta_p2p(hapd, peer);
8473 if (sta) {
8474 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Disconnect peer " MACSTR
8475 " (iface_addr=%d) from group",
8476 MAC2STR(peer), iface_addr);
8477 hostapd_drv_sta_deauth(hapd, sta->addr,
8478 WLAN_REASON_DEAUTH_LEAVING);
8479 ap_sta_deauthenticate(hapd, sta, WLAN_REASON_DEAUTH_LEAVING);
8480 }
8481 }
8482
8483
8484 void wpas_p2p_remove_client(struct wpa_supplicant *wpa_s, const u8 *peer,
8485 int iface_addr)
8486 {
8487 struct wpa_ssid *s;
8488 struct wpa_supplicant *w;
8489 struct wpa_supplicant *p2p_wpa_s = wpa_s->global->p2p_init_wpa_s;
8490
8491 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Remove client " MACSTR, MAC2STR(peer));
8492
8493 /* Remove from any persistent group */
8494 for (s = p2p_wpa_s->conf->ssid; s; s = s->next) {
8495 if (s->disabled != 2 || s->mode != WPAS_MODE_P2P_GO)
8496 continue;
8497 if (!iface_addr)
8498 wpas_remove_persistent_peer(p2p_wpa_s, s, peer, 0);
8499 wpas_p2p_remove_psk(p2p_wpa_s, s, peer, iface_addr);
8500 }
8501
8502 /* Remove from any operating group */
8503 for (w = wpa_s->global->ifaces; w; w = w->next)
8504 wpas_p2p_remove_client_go(w, peer, iface_addr);
8505 }
8506
8507
8508 static void wpas_p2p_psk_failure_removal(void *eloop_ctx, void *timeout_ctx)
8509 {
8510 struct wpa_supplicant *wpa_s = eloop_ctx;
8511 wpas_p2p_group_delete(wpa_s, P2P_GROUP_REMOVAL_PSK_FAILURE);
8512 }
8513
8514
8515 static void wpas_p2p_group_freq_conflict(void *eloop_ctx, void *timeout_ctx)
8516 {
8517 struct wpa_supplicant *wpa_s = eloop_ctx;
8518
8519 wpa_printf(MSG_DEBUG, "P2P: Frequency conflict - terminate group");
8520 wpas_p2p_group_delete(wpa_s, P2P_GROUP_REMOVAL_FREQ_CONFLICT);
8521 }
8522
8523
8524 int wpas_p2p_handle_frequency_conflicts(struct wpa_supplicant *wpa_s, int freq,
8525 struct wpa_ssid *ssid)
8526 {
8527 struct wpa_supplicant *iface;
8528
8529 for (iface = wpa_s->global->ifaces; iface; iface = iface->next) {
8530 if (!iface->current_ssid ||
8531 iface->current_ssid->frequency == freq ||
8532 (iface->p2p_group_interface == NOT_P2P_GROUP_INTERFACE &&
8533 !iface->current_ssid->p2p_group))
8534 continue;
8535
8536 /* Remove the connection with least priority */
8537 if (!wpas_is_p2p_prioritized(iface)) {
8538 /* STA connection has priority over existing
8539 * P2P connection, so remove the interface. */
8540 wpa_printf(MSG_DEBUG, "P2P: Removing P2P connection due to single channel concurrent mode frequency conflict");
8541 eloop_register_timeout(0, 0,
8542 wpas_p2p_group_freq_conflict,
8543 iface, NULL);
8544 /* If connection in progress is P2P connection, do not
8545 * proceed for the connection. */
8546 if (wpa_s == iface)
8547 return -1;
8548 else
8549 return 0;
8550 } else {
8551 /* P2P connection has priority, disable the STA network
8552 */
8553 wpa_supplicant_disable_network(wpa_s->global->ifaces,
8554 ssid);
8555 wpa_msg(wpa_s->global->ifaces, MSG_INFO,
8556 WPA_EVENT_FREQ_CONFLICT " id=%d", ssid->id);
8557 os_memset(wpa_s->global->ifaces->pending_bssid, 0,
8558 ETH_ALEN);
8559 /* If P2P connection is in progress, continue
8560 * connecting...*/
8561 if (wpa_s == iface)
8562 return 0;
8563 else
8564 return -1;
8565 }
8566 }
8567
8568 return 0;
8569 }
8570
8571
8572 int wpas_p2p_4way_hs_failed(struct wpa_supplicant *wpa_s)
8573 {
8574 struct wpa_ssid *ssid = wpa_s->current_ssid;
8575
8576 if (ssid == NULL || !ssid->p2p_group)
8577 return 0;
8578
8579 if (wpa_s->p2p_last_4way_hs_fail &&
8580 wpa_s->p2p_last_4way_hs_fail == ssid) {
8581 u8 go_dev_addr[ETH_ALEN];
8582 struct wpa_ssid *persistent;
8583
8584 if (wpas_p2p_persistent_group(wpa_s, go_dev_addr,
8585 ssid->ssid,
8586 ssid->ssid_len) <= 0) {
8587 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Could not determine whether 4-way handshake failures were for a persistent group");
8588 goto disconnect;
8589 }
8590
8591 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Two 4-way handshake failures for a P2P group - go_dev_addr="
8592 MACSTR, MAC2STR(go_dev_addr));
8593 persistent = wpas_p2p_get_persistent(wpa_s->p2pdev, go_dev_addr,
8594 ssid->ssid,
8595 ssid->ssid_len);
8596 if (persistent == NULL || persistent->mode != WPAS_MODE_INFRA) {
8597 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: No matching persistent group stored");
8598 goto disconnect;
8599 }
8600 wpa_msg_global(wpa_s->p2pdev, MSG_INFO,
8601 P2P_EVENT_PERSISTENT_PSK_FAIL "%d",
8602 persistent->id);
8603 disconnect:
8604 wpa_s->p2p_last_4way_hs_fail = NULL;
8605 /*
8606 * Remove the group from a timeout to avoid issues with caller
8607 * continuing to use the interface if this is on a P2P group
8608 * interface.
8609 */
8610 eloop_register_timeout(0, 0, wpas_p2p_psk_failure_removal,
8611 wpa_s, NULL);
8612 return 1;
8613 }
8614
8615 wpa_s->p2p_last_4way_hs_fail = ssid;
8616 return 0;
8617 }
8618
8619
8620 #ifdef CONFIG_WPS_NFC
8621
8622 static struct wpabuf * wpas_p2p_nfc_handover(int ndef, struct wpabuf *wsc,
8623 struct wpabuf *p2p)
8624 {
8625 struct wpabuf *ret;
8626 size_t wsc_len;
8627
8628 if (p2p == NULL) {
8629 wpabuf_free(wsc);
8630 wpa_printf(MSG_DEBUG, "P2P: No p2p buffer for handover");
8631 return NULL;
8632 }
8633
8634 wsc_len = wsc ? wpabuf_len(wsc) : 0;
8635 ret = wpabuf_alloc(2 + wsc_len + 2 + wpabuf_len(p2p));
8636 if (ret == NULL) {
8637 wpabuf_free(wsc);
8638 wpabuf_free(p2p);
8639 return NULL;
8640 }
8641
8642 wpabuf_put_be16(ret, wsc_len);
8643 if (wsc)
8644 wpabuf_put_buf(ret, wsc);
8645 wpabuf_put_be16(ret, wpabuf_len(p2p));
8646 wpabuf_put_buf(ret, p2p);
8647
8648 wpabuf_free(wsc);
8649 wpabuf_free(p2p);
8650 wpa_hexdump_buf(MSG_DEBUG,
8651 "P2P: Generated NFC connection handover message", ret);
8652
8653 if (ndef && ret) {
8654 struct wpabuf *tmp;
8655 tmp = ndef_build_p2p(ret);
8656 wpabuf_free(ret);
8657 if (tmp == NULL) {
8658 wpa_printf(MSG_DEBUG, "P2P: Failed to NDEF encapsulate handover request");
8659 return NULL;
8660 }
8661 ret = tmp;
8662 }
8663
8664 return ret;
8665 }
8666
8667
8668 static int wpas_p2p_cli_freq(struct wpa_supplicant *wpa_s,
8669 struct wpa_ssid **ssid, u8 *go_dev_addr)
8670 {
8671 struct wpa_supplicant *iface;
8672
8673 if (go_dev_addr)
8674 os_memset(go_dev_addr, 0, ETH_ALEN);
8675 if (ssid)
8676 *ssid = NULL;
8677 for (iface = wpa_s->global->ifaces; iface; iface = iface->next) {
8678 if (iface->wpa_state < WPA_ASSOCIATING ||
8679 iface->current_ssid == NULL || iface->assoc_freq == 0 ||
8680 !iface->current_ssid->p2p_group ||
8681 iface->current_ssid->mode != WPAS_MODE_INFRA)
8682 continue;
8683 if (ssid)
8684 *ssid = iface->current_ssid;
8685 if (go_dev_addr)
8686 os_memcpy(go_dev_addr, iface->go_dev_addr, ETH_ALEN);
8687 return iface->assoc_freq;
8688 }
8689 return 0;
8690 }
8691
8692
8693 struct wpabuf * wpas_p2p_nfc_handover_req(struct wpa_supplicant *wpa_s,
8694 int ndef)
8695 {
8696 struct wpabuf *wsc, *p2p;
8697 struct wpa_ssid *ssid;
8698 u8 go_dev_addr[ETH_ALEN];
8699 int cli_freq = wpas_p2p_cli_freq(wpa_s, &ssid, go_dev_addr);
8700
8701 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL) {
8702 wpa_printf(MSG_DEBUG, "P2P: P2P disabled - cannot build handover request");
8703 return NULL;
8704 }
8705
8706 if (wpa_s->conf->wps_nfc_dh_pubkey == NULL &&
8707 wps_nfc_gen_dh(&wpa_s->conf->wps_nfc_dh_pubkey,
8708 &wpa_s->conf->wps_nfc_dh_privkey) < 0) {
8709 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: No DH key available for handover request");
8710 return NULL;
8711 }
8712
8713 if (cli_freq == 0) {
8714 wsc = wps_build_nfc_handover_req_p2p(
8715 wpa_s->parent->wps, wpa_s->conf->wps_nfc_dh_pubkey);
8716 } else
8717 wsc = NULL;
8718 p2p = p2p_build_nfc_handover_req(wpa_s->global->p2p, cli_freq,
8719 go_dev_addr, ssid ? ssid->ssid : NULL,
8720 ssid ? ssid->ssid_len : 0);
8721
8722 return wpas_p2p_nfc_handover(ndef, wsc, p2p);
8723 }
8724
8725
8726 struct wpabuf * wpas_p2p_nfc_handover_sel(struct wpa_supplicant *wpa_s,
8727 int ndef, int tag)
8728 {
8729 struct wpabuf *wsc, *p2p;
8730 struct wpa_ssid *ssid;
8731 u8 go_dev_addr[ETH_ALEN];
8732 int cli_freq = wpas_p2p_cli_freq(wpa_s, &ssid, go_dev_addr);
8733
8734 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
8735 return NULL;
8736
8737 if (!tag && wpa_s->conf->wps_nfc_dh_pubkey == NULL &&
8738 wps_nfc_gen_dh(&wpa_s->conf->wps_nfc_dh_pubkey,
8739 &wpa_s->conf->wps_nfc_dh_privkey) < 0)
8740 return NULL;
8741
8742 if (cli_freq == 0) {
8743 wsc = wps_build_nfc_handover_sel_p2p(
8744 wpa_s->parent->wps,
8745 tag ? wpa_s->conf->wps_nfc_dev_pw_id :
8746 DEV_PW_NFC_CONNECTION_HANDOVER,
8747 wpa_s->conf->wps_nfc_dh_pubkey,
8748 tag ? wpa_s->conf->wps_nfc_dev_pw : NULL);
8749 } else
8750 wsc = NULL;
8751 p2p = p2p_build_nfc_handover_sel(wpa_s->global->p2p, cli_freq,
8752 go_dev_addr, ssid ? ssid->ssid : NULL,
8753 ssid ? ssid->ssid_len : 0);
8754
8755 return wpas_p2p_nfc_handover(ndef, wsc, p2p);
8756 }
8757
8758
8759 static int wpas_p2p_nfc_join_group(struct wpa_supplicant *wpa_s,
8760 struct p2p_nfc_params *params)
8761 {
8762 wpa_printf(MSG_DEBUG, "P2P: Initiate join-group based on NFC "
8763 "connection handover (freq=%d)",
8764 params->go_freq);
8765
8766 if (params->go_freq && params->go_ssid_len) {
8767 wpa_s->p2p_wps_method = WPS_NFC;
8768 wpa_s->pending_join_wps_method = WPS_NFC;
8769 os_memset(wpa_s->pending_join_iface_addr, 0, ETH_ALEN);
8770 os_memcpy(wpa_s->pending_join_dev_addr, params->go_dev_addr,
8771 ETH_ALEN);
8772 return wpas_p2p_join_start(wpa_s, params->go_freq,
8773 params->go_ssid,
8774 params->go_ssid_len);
8775 }
8776
8777 return wpas_p2p_connect(wpa_s, params->peer->p2p_device_addr, NULL,
8778 WPS_NFC, 0, 0, 1, 0, wpa_s->conf->p2p_go_intent,
8779 params->go_freq, wpa_s->p2p_go_vht_center_freq2,
8780 -1, 0, 1, 1, wpa_s->p2p_go_max_oper_chwidth,
8781 wpa_s->p2p_go_he, wpa_s->p2p_go_edmg,
8782 params->go_ssid_len ? params->go_ssid : NULL,
8783 params->go_ssid_len);
8784 }
8785
8786
8787 static int wpas_p2p_nfc_auth_join(struct wpa_supplicant *wpa_s,
8788 struct p2p_nfc_params *params, int tag)
8789 {
8790 int res, persistent;
8791 struct wpa_ssid *ssid;
8792
8793 wpa_printf(MSG_DEBUG, "P2P: Authorize join-group based on NFC "
8794 "connection handover");
8795 for (wpa_s = wpa_s->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
8796 ssid = wpa_s->current_ssid;
8797 if (ssid == NULL)
8798 continue;
8799 if (ssid->mode != WPAS_MODE_P2P_GO)
8800 continue;
8801 if (wpa_s->ap_iface == NULL)
8802 continue;
8803 break;
8804 }
8805 if (wpa_s == NULL) {
8806 wpa_printf(MSG_DEBUG, "P2P: Could not find GO interface");
8807 return -1;
8808 }
8809
8810 if (wpa_s->p2pdev->p2p_oob_dev_pw_id !=
8811 DEV_PW_NFC_CONNECTION_HANDOVER &&
8812 !wpa_s->p2pdev->p2p_oob_dev_pw) {
8813 wpa_printf(MSG_DEBUG, "P2P: No NFC Dev Pw known");
8814 return -1;
8815 }
8816 res = wpas_ap_wps_add_nfc_pw(
8817 wpa_s, wpa_s->p2pdev->p2p_oob_dev_pw_id,
8818 wpa_s->p2pdev->p2p_oob_dev_pw,
8819 wpa_s->p2pdev->p2p_peer_oob_pk_hash_known ?
8820 wpa_s->p2pdev->p2p_peer_oob_pubkey_hash : NULL);
8821 if (res)
8822 return res;
8823
8824 if (!tag) {
8825 wpa_printf(MSG_DEBUG, "P2P: Negotiated handover - wait for peer to join without invitation");
8826 return 0;
8827 }
8828
8829 if (!params->peer ||
8830 !(params->peer->dev_capab & P2P_DEV_CAPAB_INVITATION_PROCEDURE))
8831 return 0;
8832
8833 wpa_printf(MSG_DEBUG, "P2P: Static handover - invite peer " MACSTR
8834 " to join", MAC2STR(params->peer->p2p_device_addr));
8835
8836 wpa_s->global->p2p_invite_group = wpa_s;
8837 persistent = ssid->p2p_persistent_group &&
8838 wpas_p2p_get_persistent(wpa_s->p2pdev,
8839 params->peer->p2p_device_addr,
8840 ssid->ssid, ssid->ssid_len);
8841 wpa_s->p2pdev->pending_invite_ssid_id = -1;
8842
8843 return p2p_invite(wpa_s->global->p2p, params->peer->p2p_device_addr,
8844 P2P_INVITE_ROLE_ACTIVE_GO, wpa_s->own_addr,
8845 ssid->ssid, ssid->ssid_len, ssid->frequency,
8846 wpa_s->global->p2p_dev_addr, persistent, 0,
8847 wpa_s->p2pdev->p2p_oob_dev_pw_id);
8848 }
8849
8850
8851 static int wpas_p2p_nfc_init_go_neg(struct wpa_supplicant *wpa_s,
8852 struct p2p_nfc_params *params,
8853 int forced_freq)
8854 {
8855 wpa_printf(MSG_DEBUG, "P2P: Initiate GO Negotiation based on NFC "
8856 "connection handover");
8857 return wpas_p2p_connect(wpa_s, params->peer->p2p_device_addr, NULL,
8858 WPS_NFC, 0, 0, 0, 0, wpa_s->conf->p2p_go_intent,
8859 forced_freq, wpa_s->p2p_go_vht_center_freq2,
8860 -1, 0, 1, 1, wpa_s->p2p_go_max_oper_chwidth,
8861 wpa_s->p2p_go_he, wpa_s->p2p_go_edmg,
8862 NULL, 0);
8863 }
8864
8865
8866 static int wpas_p2p_nfc_resp_go_neg(struct wpa_supplicant *wpa_s,
8867 struct p2p_nfc_params *params,
8868 int forced_freq)
8869 {
8870 int res;
8871
8872 wpa_printf(MSG_DEBUG, "P2P: Authorize GO Negotiation based on NFC "
8873 "connection handover");
8874 res = wpas_p2p_connect(wpa_s, params->peer->p2p_device_addr, NULL,
8875 WPS_NFC, 0, 0, 0, 1, wpa_s->conf->p2p_go_intent,
8876 forced_freq, wpa_s->p2p_go_vht_center_freq2,
8877 -1, 0, 1, 1, wpa_s->p2p_go_max_oper_chwidth,
8878 wpa_s->p2p_go_he, wpa_s->p2p_go_edmg,
8879 NULL, 0);
8880 if (res)
8881 return res;
8882
8883 res = wpas_p2p_listen(wpa_s, 60);
8884 if (res) {
8885 p2p_unauthorize(wpa_s->global->p2p,
8886 params->peer->p2p_device_addr);
8887 }
8888
8889 return res;
8890 }
8891
8892
8893 static int wpas_p2p_nfc_connection_handover(struct wpa_supplicant *wpa_s,
8894 const struct wpabuf *data,
8895 int sel, int tag, int forced_freq)
8896 {
8897 const u8 *pos, *end;
8898 u16 len, id;
8899 struct p2p_nfc_params params;
8900 int res;
8901
8902 os_memset(&params, 0, sizeof(params));
8903 params.sel = sel;
8904
8905 wpa_hexdump_buf(MSG_DEBUG, "P2P: Received NFC tag payload", data);
8906
8907 pos = wpabuf_head(data);
8908 end = pos + wpabuf_len(data);
8909
8910 if (end - pos < 2) {
8911 wpa_printf(MSG_DEBUG, "P2P: Not enough data for Length of WSC "
8912 "attributes");
8913 return -1;
8914 }
8915 len = WPA_GET_BE16(pos);
8916 pos += 2;
8917 if (len > end - pos) {
8918 wpa_printf(MSG_DEBUG, "P2P: Not enough data for WSC "
8919 "attributes");
8920 return -1;
8921 }
8922 params.wsc_attr = pos;
8923 params.wsc_len = len;
8924 pos += len;
8925
8926 if (end - pos < 2) {
8927 wpa_printf(MSG_DEBUG, "P2P: Not enough data for Length of P2P "
8928 "attributes");
8929 return -1;
8930 }
8931 len = WPA_GET_BE16(pos);
8932 pos += 2;
8933 if (len > end - pos) {
8934 wpa_printf(MSG_DEBUG, "P2P: Not enough data for P2P "
8935 "attributes");
8936 return -1;
8937 }
8938 params.p2p_attr = pos;
8939 params.p2p_len = len;
8940 pos += len;
8941
8942 wpa_hexdump(MSG_DEBUG, "P2P: WSC attributes",
8943 params.wsc_attr, params.wsc_len);
8944 wpa_hexdump(MSG_DEBUG, "P2P: P2P attributes",
8945 params.p2p_attr, params.p2p_len);
8946 if (pos < end) {
8947 wpa_hexdump(MSG_DEBUG,
8948 "P2P: Ignored extra data after P2P attributes",
8949 pos, end - pos);
8950 }
8951
8952 res = p2p_process_nfc_connection_handover(wpa_s->global->p2p, &params);
8953 if (res)
8954 return res;
8955
8956 if (params.next_step == NO_ACTION)
8957 return 0;
8958
8959 if (params.next_step == BOTH_GO) {
8960 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_NFC_BOTH_GO "peer=" MACSTR,
8961 MAC2STR(params.peer->p2p_device_addr));
8962 return 0;
8963 }
8964
8965 if (params.next_step == PEER_CLIENT) {
8966 if (!is_zero_ether_addr(params.go_dev_addr)) {
8967 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_NFC_PEER_CLIENT
8968 "peer=" MACSTR " freq=%d go_dev_addr=" MACSTR
8969 " ssid=\"%s\"",
8970 MAC2STR(params.peer->p2p_device_addr),
8971 params.go_freq,
8972 MAC2STR(params.go_dev_addr),
8973 wpa_ssid_txt(params.go_ssid,
8974 params.go_ssid_len));
8975 } else {
8976 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_NFC_PEER_CLIENT
8977 "peer=" MACSTR " freq=%d",
8978 MAC2STR(params.peer->p2p_device_addr),
8979 params.go_freq);
8980 }
8981 return 0;
8982 }
8983
8984 if (wpas_p2p_cli_freq(wpa_s, NULL, NULL)) {
8985 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_NFC_WHILE_CLIENT "peer="
8986 MACSTR, MAC2STR(params.peer->p2p_device_addr));
8987 return 0;
8988 }
8989
8990 wpabuf_free(wpa_s->p2p_oob_dev_pw);
8991 wpa_s->p2p_oob_dev_pw = NULL;
8992
8993 if (params.oob_dev_pw_len < WPS_OOB_PUBKEY_HASH_LEN + 2) {
8994 wpa_printf(MSG_DEBUG, "P2P: No peer OOB Dev Pw "
8995 "received");
8996 return -1;
8997 }
8998
8999 id = WPA_GET_BE16(params.oob_dev_pw + WPS_OOB_PUBKEY_HASH_LEN);
9000 wpa_printf(MSG_DEBUG, "P2P: Peer OOB Dev Pw %u", id);
9001 wpa_hexdump(MSG_DEBUG, "P2P: Peer OOB Public Key hash",
9002 params.oob_dev_pw, WPS_OOB_PUBKEY_HASH_LEN);
9003 os_memcpy(wpa_s->p2p_peer_oob_pubkey_hash,
9004 params.oob_dev_pw, WPS_OOB_PUBKEY_HASH_LEN);
9005 wpa_s->p2p_peer_oob_pk_hash_known = 1;
9006
9007 if (tag) {
9008 if (id < 0x10) {
9009 wpa_printf(MSG_DEBUG, "P2P: Static handover - invalid "
9010 "peer OOB Device Password Id %u", id);
9011 return -1;
9012 }
9013 wpa_printf(MSG_DEBUG, "P2P: Static handover - use peer OOB "
9014 "Device Password Id %u", id);
9015 wpa_hexdump_key(MSG_DEBUG, "P2P: Peer OOB Device Password",
9016 params.oob_dev_pw + WPS_OOB_PUBKEY_HASH_LEN + 2,
9017 params.oob_dev_pw_len -
9018 WPS_OOB_PUBKEY_HASH_LEN - 2);
9019 wpa_s->p2p_oob_dev_pw_id = id;
9020 wpa_s->p2p_oob_dev_pw = wpabuf_alloc_copy(
9021 params.oob_dev_pw + WPS_OOB_PUBKEY_HASH_LEN + 2,
9022 params.oob_dev_pw_len -
9023 WPS_OOB_PUBKEY_HASH_LEN - 2);
9024 if (wpa_s->p2p_oob_dev_pw == NULL)
9025 return -1;
9026
9027 if (wpa_s->conf->wps_nfc_dh_pubkey == NULL &&
9028 wps_nfc_gen_dh(&wpa_s->conf->wps_nfc_dh_pubkey,
9029 &wpa_s->conf->wps_nfc_dh_privkey) < 0)
9030 return -1;
9031 } else {
9032 wpa_printf(MSG_DEBUG, "P2P: Using abbreviated WPS handshake "
9033 "without Device Password");
9034 wpa_s->p2p_oob_dev_pw_id = DEV_PW_NFC_CONNECTION_HANDOVER;
9035 }
9036
9037 switch (params.next_step) {
9038 case NO_ACTION:
9039 case BOTH_GO:
9040 case PEER_CLIENT:
9041 /* already covered above */
9042 return 0;
9043 case JOIN_GROUP:
9044 return wpas_p2p_nfc_join_group(wpa_s, &params);
9045 case AUTH_JOIN:
9046 return wpas_p2p_nfc_auth_join(wpa_s, &params, tag);
9047 case INIT_GO_NEG:
9048 return wpas_p2p_nfc_init_go_neg(wpa_s, &params, forced_freq);
9049 case RESP_GO_NEG:
9050 /* TODO: use own OOB Dev Pw */
9051 return wpas_p2p_nfc_resp_go_neg(wpa_s, &params, forced_freq);
9052 }
9053
9054 return -1;
9055 }
9056
9057
9058 int wpas_p2p_nfc_tag_process(struct wpa_supplicant *wpa_s,
9059 const struct wpabuf *data, int forced_freq)
9060 {
9061 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
9062 return -1;
9063
9064 return wpas_p2p_nfc_connection_handover(wpa_s, data, 1, 1, forced_freq);
9065 }
9066
9067
9068 int wpas_p2p_nfc_report_handover(struct wpa_supplicant *wpa_s, int init,
9069 const struct wpabuf *req,
9070 const struct wpabuf *sel, int forced_freq)
9071 {
9072 struct wpabuf *tmp;
9073 int ret;
9074
9075 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
9076 return -1;
9077
9078 wpa_printf(MSG_DEBUG, "NFC: P2P connection handover reported");
9079
9080 wpa_hexdump_ascii(MSG_DEBUG, "NFC: Req",
9081 wpabuf_head(req), wpabuf_len(req));
9082 wpa_hexdump_ascii(MSG_DEBUG, "NFC: Sel",
9083 wpabuf_head(sel), wpabuf_len(sel));
9084 if (forced_freq)
9085 wpa_printf(MSG_DEBUG, "NFC: Forced freq %d", forced_freq);
9086 tmp = ndef_parse_p2p(init ? sel : req);
9087 if (tmp == NULL) {
9088 wpa_printf(MSG_DEBUG, "P2P: Could not parse NDEF");
9089 return -1;
9090 }
9091
9092 ret = wpas_p2p_nfc_connection_handover(wpa_s, tmp, init, 0,
9093 forced_freq);
9094 wpabuf_free(tmp);
9095
9096 return ret;
9097 }
9098
9099
9100 int wpas_p2p_nfc_tag_enabled(struct wpa_supplicant *wpa_s, int enabled)
9101 {
9102 const u8 *if_addr;
9103 int go_intent = wpa_s->conf->p2p_go_intent;
9104 struct wpa_supplicant *iface;
9105
9106 if (wpa_s->global->p2p == NULL)
9107 return -1;
9108
9109 if (!enabled) {
9110 wpa_printf(MSG_DEBUG, "P2P: Disable use of own NFC Tag");
9111 for (iface = wpa_s->global->ifaces; iface; iface = iface->next)
9112 {
9113 if (!iface->ap_iface)
9114 continue;
9115 hostapd_wps_nfc_token_disable(iface->ap_iface->bss[0]);
9116 }
9117 p2p_set_authorized_oob_dev_pw_id(wpa_s->global->p2p, 0,
9118 0, NULL);
9119 if (wpa_s->p2p_nfc_tag_enabled)
9120 wpas_p2p_remove_pending_group_interface(wpa_s);
9121 wpa_s->p2p_nfc_tag_enabled = 0;
9122 return 0;
9123 }
9124
9125 if (wpa_s->global->p2p_disabled)
9126 return -1;
9127
9128 if (wpa_s->conf->wps_nfc_dh_pubkey == NULL ||
9129 wpa_s->conf->wps_nfc_dh_privkey == NULL ||
9130 wpa_s->conf->wps_nfc_dev_pw == NULL ||
9131 wpa_s->conf->wps_nfc_dev_pw_id < 0x10) {
9132 wpa_printf(MSG_DEBUG, "P2P: NFC password token not configured "
9133 "to allow static handover cases");
9134 return -1;
9135 }
9136
9137 wpa_printf(MSG_DEBUG, "P2P: Enable use of own NFC Tag");
9138
9139 wpa_s->p2p_oob_dev_pw_id = wpa_s->conf->wps_nfc_dev_pw_id;
9140 wpabuf_free(wpa_s->p2p_oob_dev_pw);
9141 wpa_s->p2p_oob_dev_pw = wpabuf_dup(wpa_s->conf->wps_nfc_dev_pw);
9142 if (wpa_s->p2p_oob_dev_pw == NULL)
9143 return -1;
9144 wpa_s->p2p_peer_oob_pk_hash_known = 0;
9145
9146 if (wpa_s->p2p_group_interface == P2P_GROUP_INTERFACE_GO ||
9147 wpa_s->p2p_group_interface == P2P_GROUP_INTERFACE_CLIENT) {
9148 /*
9149 * P2P Group Interface present and the command came on group
9150 * interface, so enable the token for the current interface.
9151 */
9152 wpa_s->create_p2p_iface = 0;
9153 } else {
9154 wpa_s->create_p2p_iface = wpas_p2p_create_iface(wpa_s);
9155 }
9156
9157 if (wpa_s->create_p2p_iface) {
9158 enum wpa_driver_if_type iftype;
9159 /* Prepare to add a new interface for the group */
9160 iftype = WPA_IF_P2P_GROUP;
9161 if (go_intent == 15)
9162 iftype = WPA_IF_P2P_GO;
9163 if (wpas_p2p_add_group_interface(wpa_s, iftype) < 0) {
9164 wpa_printf(MSG_ERROR, "P2P: Failed to allocate a new "
9165 "interface for the group");
9166 return -1;
9167 }
9168
9169 if_addr = wpa_s->pending_interface_addr;
9170 } else if (wpa_s->p2p_mgmt)
9171 if_addr = wpa_s->parent->own_addr;
9172 else
9173 if_addr = wpa_s->own_addr;
9174
9175 wpa_s->p2p_nfc_tag_enabled = enabled;
9176
9177 for (iface = wpa_s->global->ifaces; iface; iface = iface->next) {
9178 struct hostapd_data *hapd;
9179 if (iface->ap_iface == NULL)
9180 continue;
9181 hapd = iface->ap_iface->bss[0];
9182 wpabuf_free(hapd->conf->wps_nfc_dh_pubkey);
9183 hapd->conf->wps_nfc_dh_pubkey =
9184 wpabuf_dup(wpa_s->conf->wps_nfc_dh_pubkey);
9185 wpabuf_free(hapd->conf->wps_nfc_dh_privkey);
9186 hapd->conf->wps_nfc_dh_privkey =
9187 wpabuf_dup(wpa_s->conf->wps_nfc_dh_privkey);
9188 wpabuf_free(hapd->conf->wps_nfc_dev_pw);
9189 hapd->conf->wps_nfc_dev_pw =
9190 wpabuf_dup(wpa_s->conf->wps_nfc_dev_pw);
9191 hapd->conf->wps_nfc_dev_pw_id = wpa_s->conf->wps_nfc_dev_pw_id;
9192
9193 if (hostapd_wps_nfc_token_enable(iface->ap_iface->bss[0]) < 0) {
9194 wpa_dbg(iface, MSG_DEBUG,
9195 "P2P: Failed to enable NFC Tag for GO");
9196 }
9197 }
9198 p2p_set_authorized_oob_dev_pw_id(
9199 wpa_s->global->p2p, wpa_s->conf->wps_nfc_dev_pw_id, go_intent,
9200 if_addr);
9201
9202 return 0;
9203 }
9204
9205 #endif /* CONFIG_WPS_NFC */
9206
9207
9208 static void wpas_p2p_optimize_listen_channel(struct wpa_supplicant *wpa_s,
9209 struct wpa_used_freq_data *freqs,
9210 unsigned int num)
9211 {
9212 u8 curr_chan, cand, chan;
9213 unsigned int i;
9214
9215 /*
9216 * If possible, optimize the Listen channel to be a channel that is
9217 * already used by one of the other interfaces.
9218 */
9219 if (!wpa_s->conf->p2p_optimize_listen_chan)
9220 return;
9221
9222 if (!wpa_s->current_ssid || wpa_s->wpa_state != WPA_COMPLETED)
9223 return;
9224
9225 curr_chan = p2p_get_listen_channel(wpa_s->global->p2p);
9226 for (i = 0, cand = 0; i < num; i++) {
9227 ieee80211_freq_to_chan(freqs[i].freq, &chan);
9228 if (curr_chan == chan) {
9229 cand = 0;
9230 break;
9231 }
9232
9233 if (chan == 1 || chan == 6 || chan == 11)
9234 cand = chan;
9235 }
9236
9237 if (cand) {
9238 wpa_dbg(wpa_s, MSG_DEBUG,
9239 "P2P: Update Listen channel to %u based on operating channel",
9240 cand);
9241 p2p_set_listen_channel(wpa_s->global->p2p, 81, cand, 0);
9242 }
9243 }
9244
9245
9246 static int wpas_p2p_move_go_csa(struct wpa_supplicant *wpa_s)
9247 {
9248 struct hostapd_config *conf;
9249 struct p2p_go_neg_results params;
9250 struct csa_settings csa_settings;
9251 struct wpa_ssid *current_ssid = wpa_s->current_ssid;
9252 int old_freq = current_ssid->frequency;
9253 int ret;
9254
9255 if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_AP_CSA)) {
9256 wpa_dbg(wpa_s, MSG_DEBUG, "CSA is not enabled");
9257 return -1;
9258 }
9259
9260 /*
9261 * TODO: This function may not always work correctly. For example,
9262 * when we have a running GO and a BSS on a DFS channel.
9263 */
9264 if (wpas_p2p_init_go_params(wpa_s, &params, 0, 0, 0, 0, 0, 0, 0,
9265 NULL)) {
9266 wpa_dbg(wpa_s, MSG_DEBUG,
9267 "P2P CSA: Failed to select new frequency for GO");
9268 return -1;
9269 }
9270
9271 if (current_ssid->frequency == params.freq) {
9272 wpa_dbg(wpa_s, MSG_DEBUG,
9273 "P2P CSA: Selected same frequency - not moving GO");
9274 return 0;
9275 }
9276
9277 conf = hostapd_config_defaults();
9278 if (!conf) {
9279 wpa_dbg(wpa_s, MSG_DEBUG,
9280 "P2P CSA: Failed to allocate default config");
9281 return -1;
9282 }
9283
9284 current_ssid->frequency = params.freq;
9285 if (wpa_supplicant_conf_ap_ht(wpa_s, current_ssid, conf)) {
9286 wpa_dbg(wpa_s, MSG_DEBUG,
9287 "P2P CSA: Failed to create new GO config");
9288 ret = -1;
9289 goto out;
9290 }
9291
9292 if (conf->hw_mode != wpa_s->ap_iface->current_mode->mode) {
9293 wpa_dbg(wpa_s, MSG_DEBUG,
9294 "P2P CSA: CSA to a different band is not supported");
9295 ret = -1;
9296 goto out;
9297 }
9298
9299 os_memset(&csa_settings, 0, sizeof(csa_settings));
9300 csa_settings.cs_count = P2P_GO_CSA_COUNT;
9301 csa_settings.block_tx = P2P_GO_CSA_BLOCK_TX;
9302 csa_settings.freq_params.freq = params.freq;
9303 csa_settings.freq_params.sec_channel_offset = conf->secondary_channel;
9304 csa_settings.freq_params.ht_enabled = conf->ieee80211n;
9305 csa_settings.freq_params.bandwidth = conf->secondary_channel ? 40 : 20;
9306
9307 if (conf->ieee80211ac) {
9308 int freq1 = 0, freq2 = 0;
9309 u8 chan, opclass;
9310
9311 if (ieee80211_freq_to_channel_ext(params.freq,
9312 conf->secondary_channel,
9313 conf->vht_oper_chwidth,
9314 &opclass, &chan) ==
9315 NUM_HOSTAPD_MODES) {
9316 wpa_printf(MSG_ERROR, "P2P CSA: Bad freq");
9317 ret = -1;
9318 goto out;
9319 }
9320
9321 if (conf->vht_oper_centr_freq_seg0_idx)
9322 freq1 = ieee80211_chan_to_freq(
9323 NULL, opclass,
9324 conf->vht_oper_centr_freq_seg0_idx);
9325
9326 if (conf->vht_oper_centr_freq_seg1_idx)
9327 freq2 = ieee80211_chan_to_freq(
9328 NULL, opclass,
9329 conf->vht_oper_centr_freq_seg1_idx);
9330
9331 if (freq1 < 0 || freq2 < 0) {
9332 wpa_dbg(wpa_s, MSG_DEBUG,
9333 "P2P CSA: Selected invalid VHT center freqs");
9334 ret = -1;
9335 goto out;
9336 }
9337
9338 csa_settings.freq_params.vht_enabled = conf->ieee80211ac;
9339 csa_settings.freq_params.center_freq1 = freq1;
9340 csa_settings.freq_params.center_freq2 = freq2;
9341
9342 switch (conf->vht_oper_chwidth) {
9343 case CHANWIDTH_80MHZ:
9344 case CHANWIDTH_80P80MHZ:
9345 csa_settings.freq_params.bandwidth = 80;
9346 break;
9347 case CHANWIDTH_160MHZ:
9348 csa_settings.freq_params.bandwidth = 160;
9349 break;
9350 }
9351 }
9352
9353 ret = ap_switch_channel(wpa_s, &csa_settings);
9354 out:
9355 current_ssid->frequency = old_freq;
9356 hostapd_config_free(conf);
9357 return ret;
9358 }
9359
9360
9361 static void wpas_p2p_move_go_no_csa(struct wpa_supplicant *wpa_s)
9362 {
9363 struct p2p_go_neg_results params;
9364 struct wpa_ssid *current_ssid = wpa_s->current_ssid;
9365
9366 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_REMOVE_AND_REFORM_GROUP);
9367
9368 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Move GO from freq=%d MHz",
9369 current_ssid->frequency);
9370
9371 /* Stop the AP functionality */
9372 /* TODO: Should do this in a way that does not indicated to possible
9373 * P2P Clients in the group that the group is terminated. */
9374 wpa_supplicant_ap_deinit(wpa_s);
9375
9376 /* Reselect the GO frequency */
9377 if (wpas_p2p_init_go_params(wpa_s, &params, 0, 0, 0, 0, 0, 0, 0,
9378 NULL)) {
9379 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Failed to reselect freq");
9380 wpas_p2p_group_delete(wpa_s,
9381 P2P_GROUP_REMOVAL_GO_LEAVE_CHANNEL);
9382 return;
9383 }
9384 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: New freq selected for the GO (%u MHz)",
9385 params.freq);
9386
9387 if (params.freq &&
9388 !p2p_supported_freq_go(wpa_s->global->p2p, params.freq)) {
9389 wpa_printf(MSG_DEBUG,
9390 "P2P: Selected freq (%u MHz) is not valid for P2P",
9391 params.freq);
9392 wpas_p2p_group_delete(wpa_s,
9393 P2P_GROUP_REMOVAL_GO_LEAVE_CHANNEL);
9394 return;
9395 }
9396
9397 /* Update the frequency */
9398 current_ssid->frequency = params.freq;
9399 wpa_s->connect_without_scan = current_ssid;
9400 wpa_s->reassociate = 1;
9401 wpa_s->disconnected = 0;
9402 wpa_supplicant_req_scan(wpa_s, 0, 0);
9403 }
9404
9405
9406 static void wpas_p2p_move_go(void *eloop_ctx, void *timeout_ctx)
9407 {
9408 struct wpa_supplicant *wpa_s = eloop_ctx;
9409
9410 if (!wpa_s->ap_iface || !wpa_s->current_ssid)
9411 return;
9412
9413 wpas_p2p_go_update_common_freqs(wpa_s);
9414
9415 /* Do not move GO in the middle of a CSA */
9416 if (hostapd_csa_in_progress(wpa_s->ap_iface)) {
9417 wpa_printf(MSG_DEBUG,
9418 "P2P: CSA is in progress - not moving GO");
9419 return;
9420 }
9421
9422 /*
9423 * First, try a channel switch flow. If it is not supported or fails,
9424 * take down the GO and bring it up again.
9425 */
9426 if (wpas_p2p_move_go_csa(wpa_s) < 0)
9427 wpas_p2p_move_go_no_csa(wpa_s);
9428 }
9429
9430
9431 static void wpas_p2p_reconsider_moving_go(void *eloop_ctx, void *timeout_ctx)
9432 {
9433 struct wpa_supplicant *wpa_s = eloop_ctx;
9434 struct wpa_used_freq_data *freqs = NULL;
9435 unsigned int num = wpa_s->num_multichan_concurrent;
9436
9437 freqs = os_calloc(num, sizeof(struct wpa_used_freq_data));
9438 if (!freqs)
9439 return;
9440
9441 num = get_shared_radio_freqs_data(wpa_s, freqs, num);
9442
9443 /* Previous attempt to move a GO was not possible -- try again. */
9444 wpas_p2p_consider_moving_gos(wpa_s, freqs, num,
9445 WPAS_P2P_CHANNEL_UPDATE_ANY);
9446
9447 os_free(freqs);
9448 }
9449
9450
9451 /*
9452 * Consider moving a GO from its currently used frequency:
9453 * 1. It is possible that due to regulatory consideration the frequency
9454 * can no longer be used and there is a need to evacuate the GO.
9455 * 2. It is possible that due to MCC considerations, it would be preferable
9456 * to move the GO to a channel that is currently used by some other
9457 * station interface.
9458 *
9459 * In case a frequency that became invalid is once again valid, cancel a
9460 * previously initiated GO frequency change.
9461 */
9462 static void wpas_p2p_consider_moving_one_go(struct wpa_supplicant *wpa_s,
9463 struct wpa_used_freq_data *freqs,
9464 unsigned int num)
9465 {
9466 unsigned int i, invalid_freq = 0, policy_move = 0, flags = 0;
9467 unsigned int timeout;
9468 int freq;
9469 int dfs_offload;
9470
9471 wpas_p2p_go_update_common_freqs(wpa_s);
9472
9473 freq = wpa_s->current_ssid->frequency;
9474 dfs_offload = (wpa_s->drv_flags & WPA_DRIVER_FLAGS_DFS_OFFLOAD) &&
9475 ieee80211_is_dfs(freq, wpa_s->hw.modes, wpa_s->hw.num_modes);
9476 for (i = 0, invalid_freq = 0; i < num; i++) {
9477 if (freqs[i].freq == freq) {
9478 flags = freqs[i].flags;
9479
9480 /* The channel is invalid, must change it */
9481 if (!p2p_supported_freq_go(wpa_s->global->p2p, freq) &&
9482 !dfs_offload) {
9483 wpa_dbg(wpa_s, MSG_DEBUG,
9484 "P2P: Freq=%d MHz no longer valid for GO",
9485 freq);
9486 invalid_freq = 1;
9487 }
9488 } else if (freqs[i].flags == 0) {
9489 /* Freq is not used by any other station interface */
9490 continue;
9491 } else if (!p2p_supported_freq(wpa_s->global->p2p,
9492 freqs[i].freq) && !dfs_offload) {
9493 /* Freq is not valid for P2P use cases */
9494 continue;
9495 } else if (wpa_s->conf->p2p_go_freq_change_policy ==
9496 P2P_GO_FREQ_MOVE_SCM) {
9497 policy_move = 1;
9498 } else if (wpa_s->conf->p2p_go_freq_change_policy ==
9499 P2P_GO_FREQ_MOVE_SCM_PEER_SUPPORTS &&
9500 wpas_p2p_go_is_peer_freq(wpa_s, freqs[i].freq)) {
9501 policy_move = 1;
9502 } else if ((wpa_s->conf->p2p_go_freq_change_policy ==
9503 P2P_GO_FREQ_MOVE_SCM_ECSA) &&
9504 wpas_p2p_go_is_peer_freq(wpa_s, freqs[i].freq)) {
9505 if (!p2p_get_group_num_members(wpa_s->p2p_group)) {
9506 policy_move = 1;
9507 } else if ((wpa_s->drv_flags &
9508 WPA_DRIVER_FLAGS_AP_CSA) &&
9509 wpas_p2p_go_clients_support_ecsa(wpa_s)) {
9510 u8 chan;
9511
9512 /*
9513 * We do not support CSA between bands, so move
9514 * GO only within the same band.
9515 */
9516 if (wpa_s->ap_iface->current_mode->mode ==
9517 ieee80211_freq_to_chan(freqs[i].freq,
9518 &chan))
9519 policy_move = 1;
9520 }
9521 }
9522 }
9523
9524 wpa_dbg(wpa_s, MSG_DEBUG,
9525 "P2P: GO move: invalid_freq=%u, policy_move=%u, flags=0x%X",
9526 invalid_freq, policy_move, flags);
9527
9528 /*
9529 * The channel is valid, or we are going to have a policy move, so
9530 * cancel timeout.
9531 */
9532 if (!invalid_freq || policy_move) {
9533 wpa_dbg(wpa_s, MSG_DEBUG,
9534 "P2P: Cancel a GO move from freq=%d MHz", freq);
9535 eloop_cancel_timeout(wpas_p2p_move_go, wpa_s, NULL);
9536
9537 if (wpas_p2p_in_progress(wpa_s)) {
9538 wpa_dbg(wpa_s, MSG_DEBUG,
9539 "P2P: GO move: policy CS is not allowed - setting timeout to re-consider GO move");
9540 eloop_cancel_timeout(wpas_p2p_reconsider_moving_go,
9541 wpa_s, NULL);
9542 eloop_register_timeout(P2P_RECONSIDER_GO_MOVE_DELAY, 0,
9543 wpas_p2p_reconsider_moving_go,
9544 wpa_s, NULL);
9545 return;
9546 }
9547 }
9548
9549 if (!invalid_freq && (!policy_move || flags != 0)) {
9550 wpa_dbg(wpa_s, MSG_DEBUG,
9551 "P2P: Not initiating a GO frequency change");
9552 return;
9553 }
9554
9555 /*
9556 * Do not consider moving GO if it is in the middle of a CSA. When the
9557 * CSA is finished this flow should be retriggered.
9558 */
9559 if (hostapd_csa_in_progress(wpa_s->ap_iface)) {
9560 wpa_dbg(wpa_s, MSG_DEBUG,
9561 "P2P: Not initiating a GO frequency change - CSA is in progress");
9562 return;
9563 }
9564
9565 if (invalid_freq && !wpas_p2p_disallowed_freq(wpa_s->global, freq))
9566 timeout = P2P_GO_FREQ_CHANGE_TIME;
9567 else
9568 timeout = 0;
9569
9570 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Move GO from freq=%d MHz in %d secs",
9571 freq, timeout);
9572 eloop_cancel_timeout(wpas_p2p_move_go, wpa_s, NULL);
9573 eloop_register_timeout(timeout, 0, wpas_p2p_move_go, wpa_s, NULL);
9574 }
9575
9576
9577 static void wpas_p2p_consider_moving_gos(struct wpa_supplicant *wpa_s,
9578 struct wpa_used_freq_data *freqs,
9579 unsigned int num,
9580 enum wpas_p2p_channel_update_trig trig)
9581 {
9582 struct wpa_supplicant *ifs;
9583
9584 eloop_cancel_timeout(wpas_p2p_reconsider_moving_go, ELOOP_ALL_CTX,
9585 NULL);
9586
9587 /*
9588 * Travers all the radio interfaces, and for each GO interface, check
9589 * if there is a need to move the GO from the frequency it is using,
9590 * or in case the frequency is valid again, cancel the evacuation flow.
9591 */
9592 dl_list_for_each(ifs, &wpa_s->radio->ifaces, struct wpa_supplicant,
9593 radio_list) {
9594 if (ifs->current_ssid == NULL ||
9595 ifs->current_ssid->mode != WPAS_MODE_P2P_GO)
9596 continue;
9597
9598 /*
9599 * The GO was just started or completed channel switch, no need
9600 * to move it.
9601 */
9602 if (wpa_s == ifs &&
9603 (trig == WPAS_P2P_CHANNEL_UPDATE_STATE_CHANGE ||
9604 trig == WPAS_P2P_CHANNEL_UPDATE_CS)) {
9605 wpa_dbg(wpa_s, MSG_DEBUG,
9606 "P2P: GO move - schedule re-consideration");
9607 eloop_register_timeout(P2P_RECONSIDER_GO_MOVE_DELAY, 0,
9608 wpas_p2p_reconsider_moving_go,
9609 wpa_s, NULL);
9610 continue;
9611 }
9612
9613 wpas_p2p_consider_moving_one_go(ifs, freqs, num);
9614 }
9615 }
9616
9617
9618 void wpas_p2p_indicate_state_change(struct wpa_supplicant *wpa_s)
9619 {
9620 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
9621 return;
9622
9623 wpas_p2p_update_channel_list(wpa_s,
9624 WPAS_P2P_CHANNEL_UPDATE_STATE_CHANGE);
9625 }
9626
9627
9628 void wpas_p2p_deinit_iface(struct wpa_supplicant *wpa_s)
9629 {
9630 if (wpa_s == wpa_s->global->p2p_init_wpa_s && wpa_s->global->p2p) {
9631 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Disable P2P since removing "
9632 "the management interface is being removed");
9633 wpas_p2p_deinit_global(wpa_s->global);
9634 }
9635 }
9636
9637
9638 void wpas_p2p_ap_deinit(struct wpa_supplicant *wpa_s)
9639 {
9640 if (wpa_s->ap_iface->bss)
9641 wpa_s->ap_iface->bss[0]->p2p_group = NULL;
9642 wpas_p2p_group_deinit(wpa_s);
9643 }
9644
9645
9646 int wpas_p2p_lo_start(struct wpa_supplicant *wpa_s, unsigned int freq,
9647 unsigned int period, unsigned int interval,
9648 unsigned int count)
9649 {
9650 struct p2p_data *p2p = wpa_s->global->p2p;
9651 u8 *device_types;
9652 size_t dev_types_len;
9653 struct wpabuf *buf;
9654 int ret;
9655
9656 if (wpa_s->p2p_lo_started) {
9657 wpa_dbg(wpa_s, MSG_DEBUG,
9658 "P2P Listen offload is already started");
9659 return 0;
9660 }
9661
9662 if (wpa_s->global->p2p == NULL ||
9663 !(wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_LISTEN_OFFLOAD)) {
9664 wpa_printf(MSG_DEBUG, "P2P: Listen offload not supported");
9665 return -1;
9666 }
9667
9668 if (!p2p_supported_freq(wpa_s->global->p2p, freq)) {
9669 wpa_printf(MSG_ERROR, "P2P: Input channel not supported: %u",
9670 freq);
9671 return -1;
9672 }
9673
9674 /* Get device type */
9675 dev_types_len = (wpa_s->conf->num_sec_device_types + 1) *
9676 WPS_DEV_TYPE_LEN;
9677 device_types = os_malloc(dev_types_len);
9678 if (!device_types)
9679 return -1;
9680 os_memcpy(device_types, wpa_s->conf->device_type, WPS_DEV_TYPE_LEN);
9681 os_memcpy(&device_types[WPS_DEV_TYPE_LEN], wpa_s->conf->sec_device_type,
9682 wpa_s->conf->num_sec_device_types * WPS_DEV_TYPE_LEN);
9683
9684 /* Get Probe Response IE(s) */
9685 buf = p2p_build_probe_resp_template(p2p, freq);
9686 if (!buf) {
9687 os_free(device_types);
9688 return -1;
9689 }
9690
9691 ret = wpa_drv_p2p_lo_start(wpa_s, freq, period, interval, count,
9692 device_types, dev_types_len,
9693 wpabuf_mhead_u8(buf), wpabuf_len(buf));
9694 if (ret < 0)
9695 wpa_dbg(wpa_s, MSG_DEBUG,
9696 "P2P: Failed to start P2P listen offload");
9697
9698 os_free(device_types);
9699 wpabuf_free(buf);
9700
9701 if (ret == 0) {
9702 wpa_s->p2p_lo_started = 1;
9703
9704 /* Stop current P2P listen if any */
9705 wpas_stop_listen(wpa_s);
9706 }
9707
9708 return ret;
9709 }
9710
9711
9712 int wpas_p2p_lo_stop(struct wpa_supplicant *wpa_s)
9713 {
9714 int ret;
9715
9716 if (!wpa_s->p2p_lo_started)
9717 return 0;
9718
9719 ret = wpa_drv_p2p_lo_stop(wpa_s);
9720 if (ret < 0)
9721 wpa_dbg(wpa_s, MSG_DEBUG,
9722 "P2P: Failed to stop P2P listen offload");
9723
9724 wpa_s->p2p_lo_started = 0;
9725 return ret;
9726 }