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