]> git.ipfire.org Git - thirdparty/hostap.git/blob - wpa_supplicant/p2p_supplicant.c
Use radio work for P2P scan requests
[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/p2p_hostapd.h"
24 #include "eapol_supp/eapol_supp_sm.h"
25 #include "rsn_supp/wpa.h"
26 #include "wpa_supplicant_i.h"
27 #include "driver_i.h"
28 #include "ap.h"
29 #include "config_ssid.h"
30 #include "config.h"
31 #include "notify.h"
32 #include "scan.h"
33 #include "bss.h"
34 #include "offchannel.h"
35 #include "wps_supplicant.h"
36 #include "p2p_supplicant.h"
37 #include "wifi_display.h"
38
39
40 /*
41 * How many times to try to scan to find the GO before giving up on join
42 * request.
43 */
44 #define P2P_MAX_JOIN_SCAN_ATTEMPTS 10
45
46 #define P2P_AUTO_PD_SCAN_ATTEMPTS 5
47
48 #ifndef P2P_MAX_CLIENT_IDLE
49 /*
50 * How many seconds to try to reconnect to the GO when connection in P2P client
51 * role has been lost.
52 */
53 #define P2P_MAX_CLIENT_IDLE 10
54 #endif /* P2P_MAX_CLIENT_IDLE */
55
56 #ifndef P2P_MAX_INITIAL_CONN_WAIT
57 /*
58 * How many seconds to wait for initial 4-way handshake to get completed after
59 * WPS provisioning step.
60 */
61 #define P2P_MAX_INITIAL_CONN_WAIT 10
62 #endif /* P2P_MAX_INITIAL_CONN_WAIT */
63
64 #ifndef P2P_MAX_INITIAL_CONN_WAIT_GO
65 /*
66 * How many seconds to wait for initial 4-way handshake to get completed after
67 * WPS provisioning step on the GO. This controls the extra time the P2P
68 * operation is considered to be in progress (e.g., to delay other scans) after
69 * WPS provisioning has been completed on the GO during group formation.
70 */
71 #define P2P_MAX_INITIAL_CONN_WAIT_GO 10
72 #endif /* P2P_MAX_INITIAL_CONN_WAIT_GO */
73
74 #ifndef P2P_MAX_INITIAL_CONN_WAIT_GO_REINVOKE
75 /*
76 * How many seconds to wait for initial 4-way handshake to get completed after
77 * re-invocation of a persistent group on the GO when the client is expected
78 * to connect automatically (no user interaction).
79 */
80 #define P2P_MAX_INITIAL_CONN_WAIT_GO_REINVOKE 15
81 #endif /* P2P_MAX_INITIAL_CONN_WAIT_GO_REINVOKE */
82
83 #ifndef P2P_CONCURRENT_SEARCH_DELAY
84 #define P2P_CONCURRENT_SEARCH_DELAY 500
85 #endif /* P2P_CONCURRENT_SEARCH_DELAY */
86
87 #define P2P_MGMT_DEVICE_PREFIX "p2p-dev-"
88
89 enum p2p_group_removal_reason {
90 P2P_GROUP_REMOVAL_UNKNOWN,
91 P2P_GROUP_REMOVAL_SILENT,
92 P2P_GROUP_REMOVAL_FORMATION_FAILED,
93 P2P_GROUP_REMOVAL_REQUESTED,
94 P2P_GROUP_REMOVAL_IDLE_TIMEOUT,
95 P2P_GROUP_REMOVAL_UNAVAILABLE,
96 P2P_GROUP_REMOVAL_GO_ENDING_SESSION,
97 P2P_GROUP_REMOVAL_PSK_FAILURE,
98 P2P_GROUP_REMOVAL_FREQ_CONFLICT
99 };
100
101
102 static void wpas_p2p_long_listen_timeout(void *eloop_ctx, void *timeout_ctx);
103 static struct wpa_supplicant *
104 wpas_p2p_get_group_iface(struct wpa_supplicant *wpa_s, int addr_allocated,
105 int go);
106 static int wpas_p2p_join_start(struct wpa_supplicant *wpa_s);
107 static void wpas_p2p_join_scan_req(struct wpa_supplicant *wpa_s, int freq);
108 static void wpas_p2p_join_scan(void *eloop_ctx, void *timeout_ctx);
109 static int wpas_p2p_join(struct wpa_supplicant *wpa_s, const u8 *iface_addr,
110 const u8 *dev_addr, enum p2p_wps_method wps_method,
111 int auto_join);
112 static int wpas_p2p_create_iface(struct wpa_supplicant *wpa_s);
113 static void wpas_p2p_cross_connect_setup(struct wpa_supplicant *wpa_s);
114 static void wpas_p2p_group_idle_timeout(void *eloop_ctx, void *timeout_ctx);
115 static void wpas_p2p_set_group_idle_timeout(struct wpa_supplicant *wpa_s);
116 static void wpas_p2p_group_formation_timeout(void *eloop_ctx,
117 void *timeout_ctx);
118 static void wpas_p2p_group_freq_conflict(void *eloop_ctx, void *timeout_ctx);
119 static void wpas_p2p_fallback_to_go_neg(struct wpa_supplicant *wpa_s,
120 int group_added);
121 static int wpas_p2p_stop_find_oper(struct wpa_supplicant *wpa_s);
122
123
124 /*
125 * Get the number of concurrent channels that the HW can operate, but that are
126 * currently not in use by any of the wpa_supplicant interfaces.
127 */
128 static int wpas_p2p_num_unused_channels(struct wpa_supplicant *wpa_s)
129 {
130 int *freqs;
131 int num, unused;
132
133 freqs = os_calloc(wpa_s->num_multichan_concurrent, sizeof(int));
134 if (!freqs)
135 return -1;
136
137 num = get_shared_radio_freqs(wpa_s, freqs,
138 wpa_s->num_multichan_concurrent);
139 os_free(freqs);
140
141 unused = wpa_s->num_multichan_concurrent - num;
142 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: num_unused_channels: %d", unused);
143 return unused;
144 }
145
146
147 /*
148 * Get the frequencies that are currently in use by one or more of the virtual
149 * interfaces, and that are also valid for P2P operation.
150 */
151 static int wpas_p2p_valid_oper_freqs(struct wpa_supplicant *wpa_s,
152 int *p2p_freqs, unsigned int len)
153 {
154 int *freqs;
155 unsigned int num, i, j;
156
157 freqs = os_calloc(wpa_s->num_multichan_concurrent, sizeof(int));
158 if (!freqs)
159 return -1;
160
161 num = get_shared_radio_freqs(wpa_s, freqs,
162 wpa_s->num_multichan_concurrent);
163
164 os_memset(p2p_freqs, 0, sizeof(int) * len);
165
166 for (i = 0, j = 0; i < num && j < len; i++) {
167 if (p2p_supported_freq(wpa_s->global->p2p, freqs[i]))
168 p2p_freqs[j++] = freqs[i];
169 }
170
171 os_free(freqs);
172
173 dump_freq_array(wpa_s, "valid for P2P", p2p_freqs, j);
174
175 return j;
176 }
177
178
179 static void wpas_p2p_set_own_freq_preference(struct wpa_supplicant *wpa_s,
180 int freq)
181 {
182 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
183 return;
184 if (wpa_s->parent->conf->p2p_ignore_shared_freq &&
185 freq > 0 && wpa_s->num_multichan_concurrent > 1 &&
186 wpas_p2p_num_unused_channels(wpa_s) > 0) {
187 wpa_printf(MSG_DEBUG, "P2P: Ignore own channel preference %d MHz due to p2p_ignore_shared_freq=1 configuration",
188 freq);
189 freq = 0;
190 }
191 p2p_set_own_freq_preference(wpa_s->global->p2p, freq);
192 }
193
194
195 static void wpas_p2p_scan_res_handler(struct wpa_supplicant *wpa_s,
196 struct wpa_scan_results *scan_res)
197 {
198 size_t i;
199
200 if (wpa_s->p2p_scan_work) {
201 struct wpa_radio_work *work = wpa_s->p2p_scan_work;
202 wpa_s->p2p_scan_work = NULL;
203 radio_work_done(work);
204 }
205
206 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
207 return;
208
209 wpa_printf(MSG_DEBUG, "P2P: Scan results received (%d BSS)",
210 (int) scan_res->num);
211
212 for (i = 0; i < scan_res->num; i++) {
213 struct wpa_scan_res *bss = scan_res->res[i];
214 struct os_reltime time_tmp_age, entry_ts;
215 const u8 *ies;
216 size_t ies_len;
217
218 time_tmp_age.sec = bss->age / 1000;
219 time_tmp_age.usec = (bss->age % 1000) * 1000;
220 os_reltime_sub(&scan_res->fetch_time, &time_tmp_age, &entry_ts);
221
222 ies = (const u8 *) (bss + 1);
223 ies_len = bss->ie_len;
224 if (bss->beacon_ie_len > 0 &&
225 !wpa_scan_get_vendor_ie(bss, P2P_IE_VENDOR_TYPE) &&
226 wpa_scan_get_vendor_ie_beacon(bss, P2P_IE_VENDOR_TYPE)) {
227 wpa_printf(MSG_DEBUG, "P2P: Use P2P IE(s) from Beacon frame since no P2P IE(s) in Probe Response frames received for "
228 MACSTR, MAC2STR(bss->bssid));
229 ies = ies + ies_len;
230 ies_len = bss->beacon_ie_len;
231 }
232
233
234 if (p2p_scan_res_handler(wpa_s->global->p2p, bss->bssid,
235 bss->freq, &entry_ts, bss->level,
236 ies, ies_len) > 0)
237 break;
238 }
239
240 p2p_scan_res_handled(wpa_s->global->p2p);
241 }
242
243
244 static void wpas_p2p_trigger_scan_cb(struct wpa_radio_work *work, int deinit)
245 {
246 struct wpa_supplicant *wpa_s = work->wpa_s;
247 struct wpa_driver_scan_params *params = work->ctx;
248 int ret;
249
250 if (deinit) {
251 wpa_scan_free_params(params);
252 return;
253 }
254
255 ret = wpa_drv_scan(wpa_s, params);
256 wpa_scan_free_params(params);
257 work->ctx = NULL;
258 if (ret) {
259 radio_work_done(work);
260 return;
261 }
262
263 os_get_reltime(&wpa_s->scan_trigger_time);
264 wpa_s->scan_res_handler = wpas_p2p_scan_res_handler;
265 wpa_s->own_scan_requested = 1;
266 wpa_s->p2p_scan_work = work;
267 }
268
269
270 static int wpas_p2p_scan(void *ctx, enum p2p_scan_type type, int freq,
271 unsigned int num_req_dev_types,
272 const u8 *req_dev_types, const u8 *dev_id, u16 pw_id)
273 {
274 struct wpa_supplicant *wpa_s = ctx;
275 struct wpa_driver_scan_params *params = NULL;
276 struct wpabuf *wps_ie, *ies;
277 size_t ielen;
278 u8 *n;
279
280 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
281 return -1;
282
283 if (wpa_s->p2p_scan_work) {
284 wpa_dbg(wpa_s, MSG_INFO, "P2P: Reject scan trigger since one is already pending");
285 return -1;
286 }
287
288 params = os_zalloc(sizeof(*params));
289 if (params == NULL)
290 return -1;
291
292 /* P2P Wildcard SSID */
293 params->num_ssids = 1;
294 n = os_malloc(P2P_WILDCARD_SSID_LEN);
295 if (n == NULL)
296 goto fail;
297 os_memcpy(n, P2P_WILDCARD_SSID, P2P_WILDCARD_SSID_LEN);
298 params->ssids[0].ssid = n;
299 params->ssids[0].ssid_len = P2P_WILDCARD_SSID_LEN;
300
301 wpa_s->wps->dev.p2p = 1;
302 wps_ie = wps_build_probe_req_ie(pw_id, &wpa_s->wps->dev,
303 wpa_s->wps->uuid, WPS_REQ_ENROLLEE,
304 num_req_dev_types, req_dev_types);
305 if (wps_ie == NULL)
306 goto fail;
307
308 ielen = p2p_scan_ie_buf_len(wpa_s->global->p2p);
309 ies = wpabuf_alloc(wpabuf_len(wps_ie) + ielen);
310 if (ies == NULL) {
311 wpabuf_free(wps_ie);
312 goto fail;
313 }
314 wpabuf_put_buf(ies, wps_ie);
315 wpabuf_free(wps_ie);
316
317 p2p_scan_ie(wpa_s->global->p2p, ies, dev_id);
318
319 params->p2p_probe = 1;
320 n = os_malloc(wpabuf_len(ies));
321 if (n == NULL) {
322 wpabuf_free(ies);
323 goto fail;
324 }
325 os_memcpy(n, wpabuf_head(ies), wpabuf_len(ies));
326 params->extra_ies = n;
327 params->extra_ies_len = wpabuf_len(ies);
328 wpabuf_free(ies);
329
330 switch (type) {
331 case P2P_SCAN_SOCIAL:
332 params->freqs = os_malloc(4 * sizeof(int));
333 if (params->freqs == NULL)
334 goto fail;
335 params->freqs[0] = 2412;
336 params->freqs[1] = 2437;
337 params->freqs[2] = 2462;
338 params->freqs[3] = 0;
339 break;
340 case P2P_SCAN_FULL:
341 break;
342 case P2P_SCAN_SOCIAL_PLUS_ONE:
343 params->freqs = os_malloc(5 * sizeof(int));
344 if (params->freqs == NULL)
345 goto fail;
346 params->freqs[0] = 2412;
347 params->freqs[1] = 2437;
348 params->freqs[2] = 2462;
349 params->freqs[3] = freq;
350 params->freqs[4] = 0;
351 break;
352 }
353
354 radio_remove_unstarted_work(wpa_s, "p2p-scan");
355 if (radio_add_work(wpa_s, 0, "p2p-scan", 0, wpas_p2p_trigger_scan_cb,
356 params) < 0)
357 goto fail;
358 return 0;
359
360 fail:
361 wpa_scan_free_params(params);
362 return -1;
363 }
364
365
366 static enum wpa_driver_if_type wpas_p2p_if_type(int p2p_group_interface)
367 {
368 switch (p2p_group_interface) {
369 case P2P_GROUP_INTERFACE_PENDING:
370 return WPA_IF_P2P_GROUP;
371 case P2P_GROUP_INTERFACE_GO:
372 return WPA_IF_P2P_GO;
373 case P2P_GROUP_INTERFACE_CLIENT:
374 return WPA_IF_P2P_CLIENT;
375 }
376
377 return WPA_IF_P2P_GROUP;
378 }
379
380
381 static struct wpa_supplicant * wpas_get_p2p_group(struct wpa_supplicant *wpa_s,
382 const u8 *ssid,
383 size_t ssid_len, int *go)
384 {
385 struct wpa_ssid *s;
386
387 for (wpa_s = wpa_s->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
388 for (s = wpa_s->conf->ssid; s; s = s->next) {
389 if (s->disabled != 0 || !s->p2p_group ||
390 s->ssid_len != ssid_len ||
391 os_memcmp(ssid, s->ssid, ssid_len) != 0)
392 continue;
393 if (s->mode == WPAS_MODE_P2P_GO &&
394 s != wpa_s->current_ssid)
395 continue;
396 if (go)
397 *go = s->mode == WPAS_MODE_P2P_GO;
398 return wpa_s;
399 }
400 }
401
402 return NULL;
403 }
404
405
406 static int wpas_p2p_group_delete(struct wpa_supplicant *wpa_s,
407 enum p2p_group_removal_reason removal_reason)
408 {
409 struct wpa_ssid *ssid;
410 char *gtype;
411 const char *reason;
412
413 ssid = wpa_s->current_ssid;
414 if (ssid == NULL) {
415 /*
416 * The current SSID was not known, but there may still be a
417 * pending P2P group interface waiting for provisioning or a
418 * P2P group that is trying to reconnect.
419 */
420 ssid = wpa_s->conf->ssid;
421 while (ssid) {
422 if (ssid->p2p_group && ssid->disabled != 2)
423 break;
424 ssid = ssid->next;
425 }
426 if (ssid == NULL &&
427 wpa_s->p2p_group_interface == NOT_P2P_GROUP_INTERFACE)
428 {
429 wpa_printf(MSG_ERROR, "P2P: P2P group interface "
430 "not found");
431 return -1;
432 }
433 }
434 if (wpa_s->p2p_group_interface == P2P_GROUP_INTERFACE_GO)
435 gtype = "GO";
436 else if (wpa_s->p2p_group_interface == P2P_GROUP_INTERFACE_CLIENT ||
437 (ssid && ssid->mode == WPAS_MODE_INFRA)) {
438 wpa_s->reassociate = 0;
439 wpa_s->disconnected = 1;
440 wpa_supplicant_deauthenticate(wpa_s,
441 WLAN_REASON_DEAUTH_LEAVING);
442 gtype = "client";
443 } else
444 gtype = "GO";
445 if (wpa_s->cross_connect_in_use) {
446 wpa_s->cross_connect_in_use = 0;
447 wpa_msg_global(wpa_s->parent, MSG_INFO,
448 P2P_EVENT_CROSS_CONNECT_DISABLE "%s %s",
449 wpa_s->ifname, wpa_s->cross_connect_uplink);
450 }
451 switch (removal_reason) {
452 case P2P_GROUP_REMOVAL_REQUESTED:
453 reason = " reason=REQUESTED";
454 break;
455 case P2P_GROUP_REMOVAL_FORMATION_FAILED:
456 reason = " reason=FORMATION_FAILED";
457 break;
458 case P2P_GROUP_REMOVAL_IDLE_TIMEOUT:
459 reason = " reason=IDLE";
460 break;
461 case P2P_GROUP_REMOVAL_UNAVAILABLE:
462 reason = " reason=UNAVAILABLE";
463 break;
464 case P2P_GROUP_REMOVAL_GO_ENDING_SESSION:
465 reason = " reason=GO_ENDING_SESSION";
466 break;
467 case P2P_GROUP_REMOVAL_PSK_FAILURE:
468 reason = " reason=PSK_FAILURE";
469 break;
470 case P2P_GROUP_REMOVAL_FREQ_CONFLICT:
471 reason = " reason=FREQ_CONFLICT";
472 break;
473 default:
474 reason = "";
475 break;
476 }
477 if (removal_reason != P2P_GROUP_REMOVAL_SILENT) {
478 wpa_msg_global(wpa_s->parent, MSG_INFO,
479 P2P_EVENT_GROUP_REMOVED "%s %s%s",
480 wpa_s->ifname, gtype, reason);
481 }
482
483 if (eloop_cancel_timeout(wpas_p2p_group_freq_conflict, wpa_s, NULL) > 0)
484 wpa_printf(MSG_DEBUG, "P2P: Cancelled P2P group freq_conflict timeout");
485 if (eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL) > 0)
486 wpa_printf(MSG_DEBUG, "P2P: Cancelled P2P group idle timeout");
487 if (eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
488 wpa_s->parent, NULL) > 0) {
489 wpa_printf(MSG_DEBUG, "P2P: Cancelled P2P group formation "
490 "timeout");
491 wpa_s->p2p_in_provisioning = 0;
492 }
493
494 /*
495 * Make sure wait for the first client does not remain active after the
496 * group has been removed.
497 */
498 wpa_s->global->p2p_go_wait_client.sec = 0;
499
500 if (removal_reason != P2P_GROUP_REMOVAL_SILENT && ssid)
501 wpas_notify_p2p_group_removed(wpa_s, ssid, gtype);
502
503 if (wpa_s->p2p_group_interface != NOT_P2P_GROUP_INTERFACE) {
504 struct wpa_global *global;
505 char *ifname;
506 enum wpa_driver_if_type type;
507 wpa_printf(MSG_DEBUG, "P2P: Remove group interface %s",
508 wpa_s->ifname);
509 global = wpa_s->global;
510 ifname = os_strdup(wpa_s->ifname);
511 type = wpas_p2p_if_type(wpa_s->p2p_group_interface);
512 wpa_supplicant_remove_iface(wpa_s->global, wpa_s, 0);
513 wpa_s = global->ifaces;
514 if (wpa_s && ifname)
515 wpa_drv_if_remove(wpa_s, type, ifname);
516 os_free(ifname);
517 return 1;
518 }
519
520 if (!wpa_s->p2p_go_group_formation_completed) {
521 wpa_s->global->p2p_group_formation = NULL;
522 wpa_s->p2p_in_provisioning = 0;
523 }
524
525 wpa_s->show_group_started = 0;
526 os_free(wpa_s->go_params);
527 wpa_s->go_params = NULL;
528
529 wpa_s->waiting_presence_resp = 0;
530
531 wpa_printf(MSG_DEBUG, "P2P: Remove temporary group network");
532 if (ssid && (ssid->p2p_group ||
533 ssid->mode == WPAS_MODE_P2P_GROUP_FORMATION ||
534 (ssid->key_mgmt & WPA_KEY_MGMT_WPS))) {
535 int id = ssid->id;
536 if (ssid == wpa_s->current_ssid) {
537 wpa_sm_set_config(wpa_s->wpa, NULL);
538 eapol_sm_notify_config(wpa_s->eapol, NULL, NULL);
539 wpa_s->current_ssid = NULL;
540 }
541 /*
542 * Networks objects created during any P2P activities are not
543 * exposed out as they might/will confuse certain non-P2P aware
544 * applications since these network objects won't behave like
545 * regular ones.
546 *
547 * Likewise, we don't send out network removed signals for such
548 * network objects.
549 */
550 wpa_config_remove_network(wpa_s->conf, id);
551 wpa_supplicant_clear_status(wpa_s);
552 wpa_supplicant_cancel_sched_scan(wpa_s);
553 } else {
554 wpa_printf(MSG_DEBUG, "P2P: Temporary group network not "
555 "found");
556 }
557 if (wpa_s->ap_iface)
558 wpa_supplicant_ap_deinit(wpa_s);
559 else
560 wpa_drv_deinit_p2p_cli(wpa_s);
561
562 return 0;
563 }
564
565
566 static int wpas_p2p_persistent_group(struct wpa_supplicant *wpa_s,
567 u8 *go_dev_addr,
568 const u8 *ssid, size_t ssid_len)
569 {
570 struct wpa_bss *bss;
571 const u8 *bssid;
572 struct wpabuf *p2p;
573 u8 group_capab;
574 const u8 *addr;
575
576 if (wpa_s->go_params)
577 bssid = wpa_s->go_params->peer_interface_addr;
578 else
579 bssid = wpa_s->bssid;
580
581 bss = wpa_bss_get(wpa_s, bssid, ssid, ssid_len);
582 if (bss == NULL) {
583 u8 iface_addr[ETH_ALEN];
584 if (p2p_get_interface_addr(wpa_s->global->p2p, bssid,
585 iface_addr) == 0)
586 bss = wpa_bss_get(wpa_s, iface_addr, ssid, ssid_len);
587 }
588 if (bss == NULL) {
589 wpa_printf(MSG_DEBUG, "P2P: Could not figure out whether "
590 "group is persistent - BSS " MACSTR " not found",
591 MAC2STR(bssid));
592 return 0;
593 }
594
595 p2p = wpa_bss_get_vendor_ie_multi(bss, P2P_IE_VENDOR_TYPE);
596 if (p2p == NULL)
597 p2p = wpa_bss_get_vendor_ie_multi_beacon(bss,
598 P2P_IE_VENDOR_TYPE);
599 if (p2p == NULL) {
600 wpa_printf(MSG_DEBUG, "P2P: Could not figure out whether "
601 "group is persistent - BSS " MACSTR
602 " did not include P2P IE", MAC2STR(bssid));
603 wpa_hexdump(MSG_DEBUG, "P2P: Probe Response IEs",
604 (u8 *) (bss + 1), bss->ie_len);
605 wpa_hexdump(MSG_DEBUG, "P2P: Beacon IEs",
606 ((u8 *) bss + 1) + bss->ie_len,
607 bss->beacon_ie_len);
608 return 0;
609 }
610
611 group_capab = p2p_get_group_capab(p2p);
612 addr = p2p_get_go_dev_addr(p2p);
613 wpa_printf(MSG_DEBUG, "P2P: Checking whether group is persistent: "
614 "group_capab=0x%x", group_capab);
615 if (addr) {
616 os_memcpy(go_dev_addr, addr, ETH_ALEN);
617 wpa_printf(MSG_DEBUG, "P2P: GO Device Address " MACSTR,
618 MAC2STR(addr));
619 } else
620 os_memset(go_dev_addr, 0, ETH_ALEN);
621 wpabuf_free(p2p);
622
623 wpa_printf(MSG_DEBUG, "P2P: BSS " MACSTR " group_capab=0x%x "
624 "go_dev_addr=" MACSTR,
625 MAC2STR(bssid), group_capab, MAC2STR(go_dev_addr));
626
627 return group_capab & P2P_GROUP_CAPAB_PERSISTENT_GROUP;
628 }
629
630
631 static int wpas_p2p_store_persistent_group(struct wpa_supplicant *wpa_s,
632 struct wpa_ssid *ssid,
633 const u8 *go_dev_addr)
634 {
635 struct wpa_ssid *s;
636 int changed = 0;
637
638 wpa_printf(MSG_DEBUG, "P2P: Storing credentials for a persistent "
639 "group (GO Dev Addr " MACSTR ")", MAC2STR(go_dev_addr));
640 for (s = wpa_s->conf->ssid; s; s = s->next) {
641 if (s->disabled == 2 &&
642 os_memcmp(go_dev_addr, s->bssid, ETH_ALEN) == 0 &&
643 s->ssid_len == ssid->ssid_len &&
644 os_memcmp(ssid->ssid, s->ssid, ssid->ssid_len) == 0)
645 break;
646 }
647
648 if (s) {
649 wpa_printf(MSG_DEBUG, "P2P: Update existing persistent group "
650 "entry");
651 if (ssid->passphrase && !s->passphrase)
652 changed = 1;
653 else if (ssid->passphrase && s->passphrase &&
654 os_strcmp(ssid->passphrase, s->passphrase) != 0)
655 changed = 1;
656 } else {
657 wpa_printf(MSG_DEBUG, "P2P: Create a new persistent group "
658 "entry");
659 changed = 1;
660 s = wpa_config_add_network(wpa_s->conf);
661 if (s == NULL)
662 return -1;
663
664 /*
665 * Instead of network_added we emit persistent_group_added
666 * notification. Also to keep the defense checks in
667 * persistent_group obj registration method, we set the
668 * relevant flags in s to designate it as a persistent group.
669 */
670 s->p2p_group = 1;
671 s->p2p_persistent_group = 1;
672 wpas_notify_persistent_group_added(wpa_s, s);
673 wpa_config_set_network_defaults(s);
674 }
675
676 s->p2p_group = 1;
677 s->p2p_persistent_group = 1;
678 s->disabled = 2;
679 s->bssid_set = 1;
680 os_memcpy(s->bssid, go_dev_addr, ETH_ALEN);
681 s->mode = ssid->mode;
682 s->auth_alg = WPA_AUTH_ALG_OPEN;
683 s->key_mgmt = WPA_KEY_MGMT_PSK;
684 s->proto = WPA_PROTO_RSN;
685 s->pairwise_cipher = WPA_CIPHER_CCMP;
686 s->export_keys = 1;
687 if (ssid->passphrase) {
688 os_free(s->passphrase);
689 s->passphrase = os_strdup(ssid->passphrase);
690 }
691 if (ssid->psk_set) {
692 s->psk_set = 1;
693 os_memcpy(s->psk, ssid->psk, 32);
694 }
695 if (s->passphrase && !s->psk_set)
696 wpa_config_update_psk(s);
697 if (s->ssid == NULL || s->ssid_len < ssid->ssid_len) {
698 os_free(s->ssid);
699 s->ssid = os_malloc(ssid->ssid_len);
700 }
701 if (s->ssid) {
702 s->ssid_len = ssid->ssid_len;
703 os_memcpy(s->ssid, ssid->ssid, s->ssid_len);
704 }
705 if (ssid->mode == WPAS_MODE_P2P_GO && wpa_s->global->add_psk) {
706 dl_list_add(&s->psk_list, &wpa_s->global->add_psk->list);
707 wpa_s->global->add_psk = NULL;
708 changed = 1;
709 }
710
711 #ifndef CONFIG_NO_CONFIG_WRITE
712 if (changed && wpa_s->conf->update_config &&
713 wpa_config_write(wpa_s->confname, wpa_s->conf)) {
714 wpa_printf(MSG_DEBUG, "P2P: Failed to update configuration");
715 }
716 #endif /* CONFIG_NO_CONFIG_WRITE */
717
718 return s->id;
719 }
720
721
722 static void wpas_p2p_add_persistent_group_client(struct wpa_supplicant *wpa_s,
723 const u8 *addr)
724 {
725 struct wpa_ssid *ssid, *s;
726 u8 *n;
727 size_t i;
728 int found = 0;
729
730 ssid = wpa_s->current_ssid;
731 if (ssid == NULL || ssid->mode != WPAS_MODE_P2P_GO ||
732 !ssid->p2p_persistent_group)
733 return;
734
735 for (s = wpa_s->parent->conf->ssid; s; s = s->next) {
736 if (s->disabled != 2 || s->mode != WPAS_MODE_P2P_GO)
737 continue;
738
739 if (s->ssid_len == ssid->ssid_len &&
740 os_memcmp(s->ssid, ssid->ssid, s->ssid_len) == 0)
741 break;
742 }
743
744 if (s == NULL)
745 return;
746
747 for (i = 0; s->p2p_client_list && i < s->num_p2p_clients; i++) {
748 if (os_memcmp(s->p2p_client_list + i * ETH_ALEN, addr,
749 ETH_ALEN) != 0)
750 continue;
751
752 if (i == s->num_p2p_clients - 1)
753 return; /* already the most recent entry */
754
755 /* move the entry to mark it most recent */
756 os_memmove(s->p2p_client_list + i * ETH_ALEN,
757 s->p2p_client_list + (i + 1) * ETH_ALEN,
758 (s->num_p2p_clients - i - 1) * ETH_ALEN);
759 os_memcpy(s->p2p_client_list +
760 (s->num_p2p_clients - 1) * ETH_ALEN, addr, ETH_ALEN);
761 found = 1;
762 break;
763 }
764
765 if (!found && s->num_p2p_clients < P2P_MAX_STORED_CLIENTS) {
766 n = os_realloc_array(s->p2p_client_list,
767 s->num_p2p_clients + 1, ETH_ALEN);
768 if (n == NULL)
769 return;
770 os_memcpy(n + s->num_p2p_clients * ETH_ALEN, addr, ETH_ALEN);
771 s->p2p_client_list = n;
772 s->num_p2p_clients++;
773 } else if (!found) {
774 /* Not enough room for an additional entry - drop the oldest
775 * entry */
776 os_memmove(s->p2p_client_list,
777 s->p2p_client_list + ETH_ALEN,
778 (s->num_p2p_clients - 1) * ETH_ALEN);
779 os_memcpy(s->p2p_client_list +
780 (s->num_p2p_clients - 1) * ETH_ALEN,
781 addr, ETH_ALEN);
782 }
783
784 #ifndef CONFIG_NO_CONFIG_WRITE
785 if (wpa_s->parent->conf->update_config &&
786 wpa_config_write(wpa_s->parent->confname, wpa_s->parent->conf))
787 wpa_printf(MSG_DEBUG, "P2P: Failed to update configuration");
788 #endif /* CONFIG_NO_CONFIG_WRITE */
789 }
790
791
792 static void wpas_group_formation_completed(struct wpa_supplicant *wpa_s,
793 int success)
794 {
795 struct wpa_ssid *ssid;
796 const char *ssid_txt;
797 int client;
798 int persistent;
799 u8 go_dev_addr[ETH_ALEN];
800 int network_id = -1;
801
802 /*
803 * This callback is likely called for the main interface. Update wpa_s
804 * to use the group interface if a new interface was created for the
805 * group.
806 */
807 if (wpa_s->global->p2p_group_formation)
808 wpa_s = wpa_s->global->p2p_group_formation;
809 if (wpa_s->p2p_go_group_formation_completed) {
810 wpa_s->global->p2p_group_formation = NULL;
811 wpa_s->p2p_in_provisioning = 0;
812 }
813
814 if (!success) {
815 wpa_msg_global(wpa_s->parent, MSG_INFO,
816 P2P_EVENT_GROUP_FORMATION_FAILURE);
817 wpas_p2p_group_delete(wpa_s,
818 P2P_GROUP_REMOVAL_FORMATION_FAILED);
819 return;
820 }
821
822 wpa_msg_global(wpa_s->parent, MSG_INFO,
823 P2P_EVENT_GROUP_FORMATION_SUCCESS);
824
825 ssid = wpa_s->current_ssid;
826 if (ssid && ssid->mode == WPAS_MODE_P2P_GROUP_FORMATION) {
827 ssid->mode = WPAS_MODE_P2P_GO;
828 p2p_group_notif_formation_done(wpa_s->p2p_group);
829 wpa_supplicant_ap_mac_addr_filter(wpa_s, NULL);
830 }
831
832 persistent = 0;
833 if (ssid) {
834 ssid_txt = wpa_ssid_txt(ssid->ssid, ssid->ssid_len);
835 client = ssid->mode == WPAS_MODE_INFRA;
836 if (ssid->mode == WPAS_MODE_P2P_GO) {
837 persistent = ssid->p2p_persistent_group;
838 os_memcpy(go_dev_addr, wpa_s->global->p2p_dev_addr,
839 ETH_ALEN);
840 } else
841 persistent = wpas_p2p_persistent_group(wpa_s,
842 go_dev_addr,
843 ssid->ssid,
844 ssid->ssid_len);
845 } else {
846 ssid_txt = "";
847 client = wpa_s->p2p_group_interface ==
848 P2P_GROUP_INTERFACE_CLIENT;
849 os_memset(go_dev_addr, 0, ETH_ALEN);
850 }
851
852 wpa_s->show_group_started = 0;
853 if (client) {
854 /*
855 * Indicate event only after successfully completed 4-way
856 * handshake, i.e., when the interface is ready for data
857 * packets.
858 */
859 wpa_s->show_group_started = 1;
860 } else if (ssid && ssid->passphrase == NULL && ssid->psk_set) {
861 char psk[65];
862 wpa_snprintf_hex(psk, sizeof(psk), ssid->psk, 32);
863 wpa_msg_global(wpa_s->parent, MSG_INFO, P2P_EVENT_GROUP_STARTED
864 "%s GO ssid=\"%s\" freq=%d psk=%s go_dev_addr="
865 MACSTR "%s",
866 wpa_s->ifname, ssid_txt, ssid->frequency, psk,
867 MAC2STR(go_dev_addr),
868 persistent ? " [PERSISTENT]" : "");
869 wpas_p2p_cross_connect_setup(wpa_s);
870 wpas_p2p_set_group_idle_timeout(wpa_s);
871 } else {
872 wpa_msg_global(wpa_s->parent, MSG_INFO, P2P_EVENT_GROUP_STARTED
873 "%s GO ssid=\"%s\" freq=%d passphrase=\"%s\" "
874 "go_dev_addr=" MACSTR "%s",
875 wpa_s->ifname, ssid_txt,
876 ssid ? ssid->frequency : 0,
877 ssid && ssid->passphrase ? ssid->passphrase : "",
878 MAC2STR(go_dev_addr),
879 persistent ? " [PERSISTENT]" : "");
880 wpas_p2p_cross_connect_setup(wpa_s);
881 wpas_p2p_set_group_idle_timeout(wpa_s);
882 }
883
884 if (persistent)
885 network_id = wpas_p2p_store_persistent_group(wpa_s->parent,
886 ssid, go_dev_addr);
887 else {
888 os_free(wpa_s->global->add_psk);
889 wpa_s->global->add_psk = NULL;
890 }
891 if (network_id < 0 && ssid)
892 network_id = ssid->id;
893 if (!client) {
894 wpas_notify_p2p_group_started(wpa_s, ssid, network_id, 0);
895 os_get_reltime(&wpa_s->global->p2p_go_wait_client);
896 }
897 }
898
899
900 static void wpas_p2p_send_action_tx_status(struct wpa_supplicant *wpa_s,
901 unsigned int freq,
902 const u8 *dst, const u8 *src,
903 const u8 *bssid,
904 const u8 *data, size_t data_len,
905 enum offchannel_send_action_result
906 result)
907 {
908 enum p2p_send_action_result res = P2P_SEND_ACTION_SUCCESS;
909
910 if (wpa_s->global->p2p == NULL || wpa_s->global->p2p_disabled)
911 return;
912
913 switch (result) {
914 case OFFCHANNEL_SEND_ACTION_SUCCESS:
915 res = P2P_SEND_ACTION_SUCCESS;
916 break;
917 case OFFCHANNEL_SEND_ACTION_NO_ACK:
918 res = P2P_SEND_ACTION_NO_ACK;
919 break;
920 case OFFCHANNEL_SEND_ACTION_FAILED:
921 res = P2P_SEND_ACTION_FAILED;
922 break;
923 }
924
925 p2p_send_action_cb(wpa_s->global->p2p, freq, dst, src, bssid, res);
926
927 if (result != OFFCHANNEL_SEND_ACTION_SUCCESS &&
928 wpa_s->pending_pd_before_join &&
929 (os_memcmp(dst, wpa_s->pending_join_dev_addr, ETH_ALEN) == 0 ||
930 os_memcmp(dst, wpa_s->pending_join_iface_addr, ETH_ALEN) == 0) &&
931 wpa_s->p2p_fallback_to_go_neg) {
932 wpa_s->pending_pd_before_join = 0;
933 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: No ACK for PD Req "
934 "during p2p_connect-auto");
935 wpas_p2p_fallback_to_go_neg(wpa_s, 0);
936 return;
937 }
938 }
939
940
941 static int wpas_send_action(void *ctx, unsigned int freq, const u8 *dst,
942 const u8 *src, const u8 *bssid, const u8 *buf,
943 size_t len, unsigned int wait_time)
944 {
945 struct wpa_supplicant *wpa_s = ctx;
946 return offchannel_send_action(wpa_s, freq, dst, src, bssid, buf, len,
947 wait_time,
948 wpas_p2p_send_action_tx_status, 1);
949 }
950
951
952 static void wpas_send_action_done(void *ctx)
953 {
954 struct wpa_supplicant *wpa_s = ctx;
955 offchannel_send_action_done(wpa_s);
956 }
957
958
959 static int wpas_copy_go_neg_results(struct wpa_supplicant *wpa_s,
960 struct p2p_go_neg_results *params)
961 {
962 if (wpa_s->go_params == NULL) {
963 wpa_s->go_params = os_malloc(sizeof(*params));
964 if (wpa_s->go_params == NULL)
965 return -1;
966 }
967 os_memcpy(wpa_s->go_params, params, sizeof(*params));
968 return 0;
969 }
970
971
972 static void wpas_start_wps_enrollee(struct wpa_supplicant *wpa_s,
973 struct p2p_go_neg_results *res)
974 {
975 wpa_printf(MSG_DEBUG, "P2P: Start WPS Enrollee for peer " MACSTR,
976 MAC2STR(res->peer_interface_addr));
977 wpa_hexdump_ascii(MSG_DEBUG, "P2P: Start WPS Enrollee for SSID",
978 res->ssid, res->ssid_len);
979 wpa_supplicant_ap_deinit(wpa_s);
980 wpas_copy_go_neg_results(wpa_s, res);
981 if (res->wps_method == WPS_PBC)
982 wpas_wps_start_pbc(wpa_s, res->peer_interface_addr, 1);
983 else {
984 u16 dev_pw_id = DEV_PW_DEFAULT;
985 if (wpa_s->p2p_wps_method == WPS_PIN_KEYPAD)
986 dev_pw_id = DEV_PW_REGISTRAR_SPECIFIED;
987 wpas_wps_start_pin(wpa_s, res->peer_interface_addr,
988 wpa_s->p2p_pin, 1, dev_pw_id);
989 }
990 }
991
992
993 static void wpas_p2p_add_psk_list(struct wpa_supplicant *wpa_s,
994 struct wpa_ssid *ssid)
995 {
996 struct wpa_ssid *persistent;
997 struct psk_list_entry *psk;
998 struct hostapd_data *hapd;
999
1000 if (!wpa_s->ap_iface)
1001 return;
1002
1003 persistent = wpas_p2p_get_persistent(wpa_s->parent, NULL, ssid->ssid,
1004 ssid->ssid_len);
1005 if (persistent == NULL)
1006 return;
1007
1008 hapd = wpa_s->ap_iface->bss[0];
1009
1010 dl_list_for_each(psk, &persistent->psk_list, struct psk_list_entry,
1011 list) {
1012 struct hostapd_wpa_psk *hpsk;
1013
1014 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Add persistent group PSK entry for "
1015 MACSTR " psk=%d",
1016 MAC2STR(psk->addr), psk->p2p);
1017 hpsk = os_zalloc(sizeof(*hpsk));
1018 if (hpsk == NULL)
1019 break;
1020 os_memcpy(hpsk->psk, psk->psk, PMK_LEN);
1021 if (psk->p2p)
1022 os_memcpy(hpsk->p2p_dev_addr, psk->addr, ETH_ALEN);
1023 else
1024 os_memcpy(hpsk->addr, psk->addr, ETH_ALEN);
1025 hpsk->next = hapd->conf->ssid.wpa_psk;
1026 hapd->conf->ssid.wpa_psk = hpsk;
1027 }
1028 }
1029
1030
1031 static void p2p_go_configured(void *ctx, void *data)
1032 {
1033 struct wpa_supplicant *wpa_s = ctx;
1034 struct p2p_go_neg_results *params = data;
1035 struct wpa_ssid *ssid;
1036 int network_id = -1;
1037
1038 ssid = wpa_s->current_ssid;
1039 if (ssid && ssid->mode == WPAS_MODE_P2P_GO) {
1040 wpa_printf(MSG_DEBUG, "P2P: Group setup without provisioning");
1041 if (wpa_s->global->p2p_group_formation == wpa_s)
1042 wpa_s->global->p2p_group_formation = NULL;
1043 if (os_strlen(params->passphrase) > 0) {
1044 wpa_msg_global(wpa_s->parent, MSG_INFO,
1045 P2P_EVENT_GROUP_STARTED
1046 "%s GO ssid=\"%s\" freq=%d "
1047 "passphrase=\"%s\" go_dev_addr=" MACSTR
1048 "%s", wpa_s->ifname,
1049 wpa_ssid_txt(ssid->ssid, ssid->ssid_len),
1050 ssid->frequency, params->passphrase,
1051 MAC2STR(wpa_s->global->p2p_dev_addr),
1052 params->persistent_group ?
1053 " [PERSISTENT]" : "");
1054 } else {
1055 char psk[65];
1056 wpa_snprintf_hex(psk, sizeof(psk), params->psk,
1057 sizeof(params->psk));
1058 wpa_msg_global(wpa_s->parent, MSG_INFO,
1059 P2P_EVENT_GROUP_STARTED
1060 "%s GO ssid=\"%s\" freq=%d psk=%s "
1061 "go_dev_addr=" MACSTR "%s",
1062 wpa_s->ifname,
1063 wpa_ssid_txt(ssid->ssid, ssid->ssid_len),
1064 ssid->frequency, psk,
1065 MAC2STR(wpa_s->global->p2p_dev_addr),
1066 params->persistent_group ?
1067 " [PERSISTENT]" : "");
1068 }
1069
1070 os_get_reltime(&wpa_s->global->p2p_go_wait_client);
1071 if (params->persistent_group) {
1072 network_id = wpas_p2p_store_persistent_group(
1073 wpa_s->parent, ssid,
1074 wpa_s->global->p2p_dev_addr);
1075 wpas_p2p_add_psk_list(wpa_s, ssid);
1076 }
1077 if (network_id < 0)
1078 network_id = ssid->id;
1079 wpas_notify_p2p_group_started(wpa_s, ssid, network_id, 0);
1080 wpas_p2p_cross_connect_setup(wpa_s);
1081 wpas_p2p_set_group_idle_timeout(wpa_s);
1082
1083 if (wpa_s->p2p_first_connection_timeout) {
1084 wpa_dbg(wpa_s, MSG_DEBUG,
1085 "P2P: Start group formation timeout of %d seconds until first data connection on GO",
1086 wpa_s->p2p_first_connection_timeout);
1087 wpa_s->p2p_go_group_formation_completed = 0;
1088 wpa_s->global->p2p_group_formation = wpa_s;
1089 eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
1090 wpa_s->parent, NULL);
1091 eloop_register_timeout(
1092 wpa_s->p2p_first_connection_timeout, 0,
1093 wpas_p2p_group_formation_timeout,
1094 wpa_s->parent, NULL);
1095 }
1096
1097 return;
1098 }
1099
1100 wpa_printf(MSG_DEBUG, "P2P: Setting up WPS for GO provisioning");
1101 if (wpa_supplicant_ap_mac_addr_filter(wpa_s,
1102 params->peer_interface_addr)) {
1103 wpa_printf(MSG_DEBUG, "P2P: Failed to setup MAC address "
1104 "filtering");
1105 return;
1106 }
1107 if (params->wps_method == WPS_PBC)
1108 wpa_supplicant_ap_wps_pbc(wpa_s, params->peer_interface_addr,
1109 params->peer_device_addr);
1110 else if (wpa_s->p2p_pin[0])
1111 wpa_supplicant_ap_wps_pin(wpa_s, params->peer_interface_addr,
1112 wpa_s->p2p_pin, NULL, 0, 0);
1113 os_free(wpa_s->go_params);
1114 wpa_s->go_params = NULL;
1115 }
1116
1117
1118 static void wpas_start_wps_go(struct wpa_supplicant *wpa_s,
1119 struct p2p_go_neg_results *params,
1120 int group_formation)
1121 {
1122 struct wpa_ssid *ssid;
1123
1124 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Starting GO");
1125 if (wpas_copy_go_neg_results(wpa_s, params) < 0) {
1126 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Could not copy GO Negotiation "
1127 "results");
1128 return;
1129 }
1130
1131 ssid = wpa_config_add_network(wpa_s->conf);
1132 if (ssid == NULL) {
1133 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Could not add network for GO");
1134 return;
1135 }
1136
1137 wpa_s->show_group_started = 0;
1138
1139 wpa_config_set_network_defaults(ssid);
1140 ssid->temporary = 1;
1141 ssid->p2p_group = 1;
1142 ssid->p2p_persistent_group = params->persistent_group;
1143 ssid->mode = group_formation ? WPAS_MODE_P2P_GROUP_FORMATION :
1144 WPAS_MODE_P2P_GO;
1145 ssid->frequency = params->freq;
1146 ssid->ht40 = params->ht40;
1147 ssid->vht = params->vht;
1148 ssid->ssid = os_zalloc(params->ssid_len + 1);
1149 if (ssid->ssid) {
1150 os_memcpy(ssid->ssid, params->ssid, params->ssid_len);
1151 ssid->ssid_len = params->ssid_len;
1152 }
1153 ssid->auth_alg = WPA_AUTH_ALG_OPEN;
1154 ssid->key_mgmt = WPA_KEY_MGMT_PSK;
1155 ssid->proto = WPA_PROTO_RSN;
1156 ssid->pairwise_cipher = WPA_CIPHER_CCMP;
1157 if (os_strlen(params->passphrase) > 0) {
1158 ssid->passphrase = os_strdup(params->passphrase);
1159 if (ssid->passphrase == NULL) {
1160 wpa_msg_global(wpa_s, MSG_ERROR,
1161 "P2P: Failed to copy passphrase for GO");
1162 wpa_config_remove_network(wpa_s->conf, ssid->id);
1163 return;
1164 }
1165 } else
1166 ssid->passphrase = NULL;
1167 ssid->psk_set = params->psk_set;
1168 if (ssid->psk_set)
1169 os_memcpy(ssid->psk, params->psk, sizeof(ssid->psk));
1170 else if (ssid->passphrase)
1171 wpa_config_update_psk(ssid);
1172 ssid->ap_max_inactivity = wpa_s->parent->conf->p2p_go_max_inactivity;
1173
1174 wpa_s->ap_configured_cb = p2p_go_configured;
1175 wpa_s->ap_configured_cb_ctx = wpa_s;
1176 wpa_s->ap_configured_cb_data = wpa_s->go_params;
1177 wpa_s->connect_without_scan = ssid;
1178 wpa_s->reassociate = 1;
1179 wpa_s->disconnected = 0;
1180 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Request scan (that will be skipped) to "
1181 "start GO)");
1182 wpa_supplicant_req_scan(wpa_s, 0, 0);
1183 }
1184
1185
1186 static void wpas_p2p_clone_config(struct wpa_supplicant *dst,
1187 const struct wpa_supplicant *src)
1188 {
1189 struct wpa_config *d;
1190 const struct wpa_config *s;
1191
1192 d = dst->conf;
1193 s = src->conf;
1194
1195 #define C(n) if (s->n) d->n = os_strdup(s->n)
1196 C(device_name);
1197 C(manufacturer);
1198 C(model_name);
1199 C(model_number);
1200 C(serial_number);
1201 C(config_methods);
1202 #undef C
1203
1204 os_memcpy(d->device_type, s->device_type, WPS_DEV_TYPE_LEN);
1205 os_memcpy(d->sec_device_type, s->sec_device_type,
1206 sizeof(d->sec_device_type));
1207 d->num_sec_device_types = s->num_sec_device_types;
1208
1209 d->p2p_group_idle = s->p2p_group_idle;
1210 d->p2p_intra_bss = s->p2p_intra_bss;
1211 d->persistent_reconnect = s->persistent_reconnect;
1212 d->max_num_sta = s->max_num_sta;
1213 d->pbc_in_m1 = s->pbc_in_m1;
1214 d->ignore_old_scan_res = s->ignore_old_scan_res;
1215 d->beacon_int = s->beacon_int;
1216 d->disassoc_low_ack = s->disassoc_low_ack;
1217 d->disable_scan_offload = s->disable_scan_offload;
1218 }
1219
1220
1221 static void wpas_p2p_get_group_ifname(struct wpa_supplicant *wpa_s,
1222 char *ifname, size_t len)
1223 {
1224 char *ifname_ptr = wpa_s->ifname;
1225
1226 if (os_strncmp(wpa_s->ifname, P2P_MGMT_DEVICE_PREFIX,
1227 os_strlen(P2P_MGMT_DEVICE_PREFIX)) == 0) {
1228 ifname_ptr = os_strrchr(wpa_s->ifname, '-') + 1;
1229 }
1230
1231 os_snprintf(ifname, len, "p2p-%s-%d", ifname_ptr, wpa_s->p2p_group_idx);
1232 if (os_strlen(ifname) >= IFNAMSIZ &&
1233 os_strlen(wpa_s->ifname) < IFNAMSIZ) {
1234 /* Try to avoid going over the IFNAMSIZ length limit */
1235 os_snprintf(ifname, len, "p2p-%d", wpa_s->p2p_group_idx);
1236 }
1237 }
1238
1239
1240 static int wpas_p2p_add_group_interface(struct wpa_supplicant *wpa_s,
1241 enum wpa_driver_if_type type)
1242 {
1243 char ifname[120], force_ifname[120];
1244
1245 if (wpa_s->pending_interface_name[0]) {
1246 wpa_printf(MSG_DEBUG, "P2P: Pending virtual interface exists "
1247 "- skip creation of a new one");
1248 if (is_zero_ether_addr(wpa_s->pending_interface_addr)) {
1249 wpa_printf(MSG_DEBUG, "P2P: Pending virtual address "
1250 "unknown?! ifname='%s'",
1251 wpa_s->pending_interface_name);
1252 return -1;
1253 }
1254 return 0;
1255 }
1256
1257 wpas_p2p_get_group_ifname(wpa_s, ifname, sizeof(ifname));
1258 force_ifname[0] = '\0';
1259
1260 wpa_printf(MSG_DEBUG, "P2P: Create a new interface %s for the group",
1261 ifname);
1262 wpa_s->p2p_group_idx++;
1263
1264 wpa_s->pending_interface_type = type;
1265 if (wpa_drv_if_add(wpa_s, type, ifname, NULL, NULL, force_ifname,
1266 wpa_s->pending_interface_addr, NULL) < 0) {
1267 wpa_printf(MSG_ERROR, "P2P: Failed to create new group "
1268 "interface");
1269 return -1;
1270 }
1271
1272 if (force_ifname[0]) {
1273 wpa_printf(MSG_DEBUG, "P2P: Driver forced interface name %s",
1274 force_ifname);
1275 os_strlcpy(wpa_s->pending_interface_name, force_ifname,
1276 sizeof(wpa_s->pending_interface_name));
1277 } else
1278 os_strlcpy(wpa_s->pending_interface_name, ifname,
1279 sizeof(wpa_s->pending_interface_name));
1280 wpa_printf(MSG_DEBUG, "P2P: Created pending virtual interface %s addr "
1281 MACSTR, wpa_s->pending_interface_name,
1282 MAC2STR(wpa_s->pending_interface_addr));
1283
1284 return 0;
1285 }
1286
1287
1288 static void wpas_p2p_remove_pending_group_interface(
1289 struct wpa_supplicant *wpa_s)
1290 {
1291 if (!wpa_s->pending_interface_name[0] ||
1292 is_zero_ether_addr(wpa_s->pending_interface_addr))
1293 return; /* No pending virtual interface */
1294
1295 wpa_printf(MSG_DEBUG, "P2P: Removing pending group interface %s",
1296 wpa_s->pending_interface_name);
1297 wpa_drv_if_remove(wpa_s, wpa_s->pending_interface_type,
1298 wpa_s->pending_interface_name);
1299 os_memset(wpa_s->pending_interface_addr, 0, ETH_ALEN);
1300 wpa_s->pending_interface_name[0] = '\0';
1301 }
1302
1303
1304 static struct wpa_supplicant *
1305 wpas_p2p_init_group_interface(struct wpa_supplicant *wpa_s, int go)
1306 {
1307 struct wpa_interface iface;
1308 struct wpa_supplicant *group_wpa_s;
1309
1310 if (!wpa_s->pending_interface_name[0]) {
1311 wpa_printf(MSG_ERROR, "P2P: No pending group interface");
1312 if (!wpas_p2p_create_iface(wpa_s))
1313 return NULL;
1314 /*
1315 * Something has forced us to remove the pending interface; try
1316 * to create a new one and hope for the best that we will get
1317 * the same local address.
1318 */
1319 if (wpas_p2p_add_group_interface(wpa_s, go ? WPA_IF_P2P_GO :
1320 WPA_IF_P2P_CLIENT) < 0)
1321 return NULL;
1322 }
1323
1324 os_memset(&iface, 0, sizeof(iface));
1325 iface.ifname = wpa_s->pending_interface_name;
1326 iface.driver = wpa_s->driver->name;
1327 if (wpa_s->conf->ctrl_interface == NULL &&
1328 wpa_s->parent != wpa_s &&
1329 wpa_s->p2p_mgmt &&
1330 (wpa_s->drv_flags & WPA_DRIVER_FLAGS_DEDICATED_P2P_DEVICE))
1331 iface.ctrl_interface = wpa_s->parent->conf->ctrl_interface;
1332 else
1333 iface.ctrl_interface = wpa_s->conf->ctrl_interface;
1334 iface.driver_param = wpa_s->conf->driver_param;
1335 group_wpa_s = wpa_supplicant_add_iface(wpa_s->global, &iface);
1336 if (group_wpa_s == NULL) {
1337 wpa_printf(MSG_ERROR, "P2P: Failed to create new "
1338 "wpa_supplicant interface");
1339 return NULL;
1340 }
1341 wpa_s->pending_interface_name[0] = '\0';
1342 group_wpa_s->parent = wpa_s;
1343 group_wpa_s->p2p_group_interface = go ? P2P_GROUP_INTERFACE_GO :
1344 P2P_GROUP_INTERFACE_CLIENT;
1345 wpa_s->global->p2p_group_formation = group_wpa_s;
1346
1347 wpas_p2p_clone_config(group_wpa_s, wpa_s);
1348
1349 return group_wpa_s;
1350 }
1351
1352
1353 static void wpas_p2p_group_formation_timeout(void *eloop_ctx,
1354 void *timeout_ctx)
1355 {
1356 struct wpa_supplicant *wpa_s = eloop_ctx;
1357 wpa_printf(MSG_DEBUG, "P2P: Group Formation timed out");
1358 wpas_p2p_group_formation_failed(wpa_s);
1359 }
1360
1361
1362 void wpas_p2p_group_formation_failed(struct wpa_supplicant *wpa_s)
1363 {
1364 eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
1365 wpa_s->parent, NULL);
1366 if (wpa_s->global->p2p)
1367 p2p_group_formation_failed(wpa_s->global->p2p);
1368 wpas_group_formation_completed(wpa_s, 0);
1369 }
1370
1371
1372 void wpas_p2p_ap_setup_failed(struct wpa_supplicant *wpa_s)
1373 {
1374 if (wpa_s->global->p2p_group_formation != wpa_s)
1375 return;
1376 /* Speed up group formation timeout since this cannot succeed */
1377 eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
1378 wpa_s->parent, NULL);
1379 eloop_register_timeout(0, 0, wpas_p2p_group_formation_timeout,
1380 wpa_s->parent, NULL);
1381 }
1382
1383
1384 static void wpas_go_neg_completed(void *ctx, struct p2p_go_neg_results *res)
1385 {
1386 struct wpa_supplicant *wpa_s = ctx;
1387
1388 if (wpa_s->off_channel_freq || wpa_s->roc_waiting_drv_freq) {
1389 wpa_drv_cancel_remain_on_channel(wpa_s);
1390 wpa_s->off_channel_freq = 0;
1391 wpa_s->roc_waiting_drv_freq = 0;
1392 }
1393
1394 if (res->status) {
1395 wpa_msg_global(wpa_s, MSG_INFO,
1396 P2P_EVENT_GO_NEG_FAILURE "status=%d",
1397 res->status);
1398 wpas_notify_p2p_go_neg_completed(wpa_s, res);
1399 wpas_p2p_remove_pending_group_interface(wpa_s);
1400 return;
1401 }
1402
1403 if (wpa_s->p2p_go_ht40)
1404 res->ht40 = 1;
1405 if (wpa_s->p2p_go_vht)
1406 res->vht = 1;
1407
1408 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_GO_NEG_SUCCESS "role=%s "
1409 "freq=%d ht40=%d peer_dev=" MACSTR " peer_iface=" MACSTR
1410 " wps_method=%s",
1411 res->role_go ? "GO" : "client", res->freq, res->ht40,
1412 MAC2STR(res->peer_device_addr),
1413 MAC2STR(res->peer_interface_addr),
1414 p2p_wps_method_text(res->wps_method));
1415 wpas_notify_p2p_go_neg_completed(wpa_s, res);
1416
1417 if (res->role_go && wpa_s->p2p_persistent_id >= 0) {
1418 struct wpa_ssid *ssid;
1419 ssid = wpa_config_get_network(wpa_s->conf,
1420 wpa_s->p2p_persistent_id);
1421 if (ssid && ssid->disabled == 2 &&
1422 ssid->mode == WPAS_MODE_P2P_GO && ssid->passphrase) {
1423 size_t len = os_strlen(ssid->passphrase);
1424 wpa_printf(MSG_DEBUG, "P2P: Override passphrase based "
1425 "on requested persistent group");
1426 os_memcpy(res->passphrase, ssid->passphrase, len);
1427 res->passphrase[len] = '\0';
1428 }
1429 }
1430
1431 if (wpa_s->create_p2p_iface) {
1432 struct wpa_supplicant *group_wpa_s =
1433 wpas_p2p_init_group_interface(wpa_s, res->role_go);
1434 if (group_wpa_s == NULL) {
1435 wpas_p2p_remove_pending_group_interface(wpa_s);
1436 return;
1437 }
1438 if (group_wpa_s != wpa_s) {
1439 os_memcpy(group_wpa_s->p2p_pin, wpa_s->p2p_pin,
1440 sizeof(group_wpa_s->p2p_pin));
1441 group_wpa_s->p2p_wps_method = wpa_s->p2p_wps_method;
1442 }
1443 os_memset(wpa_s->pending_interface_addr, 0, ETH_ALEN);
1444 wpa_s->pending_interface_name[0] = '\0';
1445 group_wpa_s->p2p_in_provisioning = 1;
1446
1447 if (res->role_go)
1448 wpas_start_wps_go(group_wpa_s, res, 1);
1449 else
1450 wpas_start_wps_enrollee(group_wpa_s, res);
1451 } else {
1452 wpa_s->p2p_in_provisioning = 1;
1453 wpa_s->global->p2p_group_formation = wpa_s;
1454
1455 if (res->role_go)
1456 wpas_start_wps_go(wpa_s, res, 1);
1457 else
1458 wpas_start_wps_enrollee(ctx, res);
1459 }
1460
1461 wpa_s->p2p_long_listen = 0;
1462 eloop_cancel_timeout(wpas_p2p_long_listen_timeout, wpa_s, NULL);
1463
1464 eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s, NULL);
1465 eloop_register_timeout(15 + res->peer_config_timeout / 100,
1466 (res->peer_config_timeout % 100) * 10000,
1467 wpas_p2p_group_formation_timeout, wpa_s, NULL);
1468 }
1469
1470
1471 static void wpas_go_neg_req_rx(void *ctx, const u8 *src, u16 dev_passwd_id)
1472 {
1473 struct wpa_supplicant *wpa_s = ctx;
1474 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_GO_NEG_REQUEST MACSTR
1475 " dev_passwd_id=%u", MAC2STR(src), dev_passwd_id);
1476
1477 wpas_notify_p2p_go_neg_req(wpa_s, src, dev_passwd_id);
1478 }
1479
1480
1481 static void wpas_dev_found(void *ctx, const u8 *addr,
1482 const struct p2p_peer_info *info,
1483 int new_device)
1484 {
1485 #ifndef CONFIG_NO_STDOUT_DEBUG
1486 struct wpa_supplicant *wpa_s = ctx;
1487 char devtype[WPS_DEV_TYPE_BUFSIZE];
1488 char *wfd_dev_info_hex = NULL;
1489
1490 #ifdef CONFIG_WIFI_DISPLAY
1491 wfd_dev_info_hex = wifi_display_subelem_hex(info->wfd_subelems,
1492 WFD_SUBELEM_DEVICE_INFO);
1493 #endif /* CONFIG_WIFI_DISPLAY */
1494
1495 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_DEVICE_FOUND MACSTR
1496 " p2p_dev_addr=" MACSTR
1497 " pri_dev_type=%s name='%s' config_methods=0x%x "
1498 "dev_capab=0x%x group_capab=0x%x%s%s",
1499 MAC2STR(addr), MAC2STR(info->p2p_device_addr),
1500 wps_dev_type_bin2str(info->pri_dev_type, devtype,
1501 sizeof(devtype)),
1502 info->device_name, info->config_methods,
1503 info->dev_capab, info->group_capab,
1504 wfd_dev_info_hex ? " wfd_dev_info=0x" : "",
1505 wfd_dev_info_hex ? wfd_dev_info_hex : "");
1506 #endif /* CONFIG_NO_STDOUT_DEBUG */
1507
1508 os_free(wfd_dev_info_hex);
1509
1510 wpas_notify_p2p_device_found(ctx, info->p2p_device_addr, new_device);
1511 }
1512
1513
1514 static void wpas_dev_lost(void *ctx, const u8 *dev_addr)
1515 {
1516 struct wpa_supplicant *wpa_s = ctx;
1517
1518 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_DEVICE_LOST
1519 "p2p_dev_addr=" MACSTR, MAC2STR(dev_addr));
1520
1521 wpas_notify_p2p_device_lost(wpa_s, dev_addr);
1522 }
1523
1524
1525 static void wpas_find_stopped(void *ctx)
1526 {
1527 struct wpa_supplicant *wpa_s = ctx;
1528 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_FIND_STOPPED);
1529 }
1530
1531
1532 static int wpas_start_listen(void *ctx, unsigned int freq,
1533 unsigned int duration,
1534 const struct wpabuf *probe_resp_ie)
1535 {
1536 struct wpa_supplicant *wpa_s = ctx;
1537
1538 wpa_drv_set_ap_wps_ie(wpa_s, NULL, probe_resp_ie, NULL);
1539
1540 if (wpa_drv_probe_req_report(wpa_s, 1) < 0) {
1541 wpa_printf(MSG_DEBUG, "P2P: Failed to request the driver to "
1542 "report received Probe Request frames");
1543 return -1;
1544 }
1545
1546 wpa_s->pending_listen_freq = freq;
1547 wpa_s->pending_listen_duration = duration;
1548
1549 if (wpa_drv_remain_on_channel(wpa_s, freq, duration) < 0) {
1550 wpa_printf(MSG_DEBUG, "P2P: Failed to request the driver "
1551 "to remain on channel (%u MHz) for Listen "
1552 "state", freq);
1553 wpa_s->pending_listen_freq = 0;
1554 return -1;
1555 }
1556 wpa_s->off_channel_freq = 0;
1557 wpa_s->roc_waiting_drv_freq = freq;
1558
1559 return 0;
1560 }
1561
1562
1563 static void wpas_stop_listen(void *ctx)
1564 {
1565 struct wpa_supplicant *wpa_s = ctx;
1566 if (wpa_s->off_channel_freq || wpa_s->roc_waiting_drv_freq) {
1567 wpa_drv_cancel_remain_on_channel(wpa_s);
1568 wpa_s->off_channel_freq = 0;
1569 wpa_s->roc_waiting_drv_freq = 0;
1570 }
1571 wpa_drv_set_ap_wps_ie(wpa_s, NULL, NULL, NULL);
1572 wpa_drv_probe_req_report(wpa_s, 0);
1573 }
1574
1575
1576 static int wpas_send_probe_resp(void *ctx, const struct wpabuf *buf)
1577 {
1578 struct wpa_supplicant *wpa_s = ctx;
1579 return wpa_drv_send_mlme(wpa_s, wpabuf_head(buf), wpabuf_len(buf), 1);
1580 }
1581
1582
1583 /*
1584 * DNS Header section is used only to calculate compression pointers, so the
1585 * contents of this data does not matter, but the length needs to be reserved
1586 * in the virtual packet.
1587 */
1588 #define DNS_HEADER_LEN 12
1589
1590 /*
1591 * 27-octet in-memory packet from P2P specification containing two implied
1592 * queries for _tcp.lcoal. PTR IN and _udp.local. PTR IN
1593 */
1594 #define P2P_SD_IN_MEMORY_LEN 27
1595
1596 static int p2p_sd_dns_uncompress_label(char **upos, char *uend, u8 *start,
1597 u8 **spos, const u8 *end)
1598 {
1599 while (*spos < end) {
1600 u8 val = ((*spos)[0] & 0xc0) >> 6;
1601 int len;
1602
1603 if (val == 1 || val == 2) {
1604 /* These are reserved values in RFC 1035 */
1605 wpa_printf(MSG_DEBUG, "P2P: Invalid domain name "
1606 "sequence starting with 0x%x", val);
1607 return -1;
1608 }
1609
1610 if (val == 3) {
1611 u16 offset;
1612 u8 *spos_tmp;
1613
1614 /* Offset */
1615 if (*spos + 2 > end) {
1616 wpa_printf(MSG_DEBUG, "P2P: No room for full "
1617 "DNS offset field");
1618 return -1;
1619 }
1620
1621 offset = (((*spos)[0] & 0x3f) << 8) | (*spos)[1];
1622 if (offset >= *spos - start) {
1623 wpa_printf(MSG_DEBUG, "P2P: Invalid DNS "
1624 "pointer offset %u", offset);
1625 return -1;
1626 }
1627
1628 (*spos) += 2;
1629 spos_tmp = start + offset;
1630 return p2p_sd_dns_uncompress_label(upos, uend, start,
1631 &spos_tmp,
1632 *spos - 2);
1633 }
1634
1635 /* Label */
1636 len = (*spos)[0] & 0x3f;
1637 if (len == 0)
1638 return 0;
1639
1640 (*spos)++;
1641 if (*spos + len > end) {
1642 wpa_printf(MSG_DEBUG, "P2P: Invalid domain name "
1643 "sequence - no room for label with length "
1644 "%u", len);
1645 return -1;
1646 }
1647
1648 if (*upos + len + 2 > uend)
1649 return -2;
1650
1651 os_memcpy(*upos, *spos, len);
1652 *spos += len;
1653 *upos += len;
1654 (*upos)[0] = '.';
1655 (*upos)++;
1656 (*upos)[0] = '\0';
1657 }
1658
1659 return 0;
1660 }
1661
1662
1663 /* Uncompress domain names per RFC 1035 using the P2P SD in-memory packet.
1664 * Returns -1 on parsing error (invalid input sequence), -2 if output buffer is
1665 * not large enough */
1666 static int p2p_sd_dns_uncompress(char *buf, size_t buf_len, const u8 *msg,
1667 size_t msg_len, size_t offset)
1668 {
1669 /* 27-octet in-memory packet from P2P specification */
1670 const char *prefix = "\x04_tcp\x05local\x00\x00\x0C\x00\x01"
1671 "\x04_udp\xC0\x11\x00\x0C\x00\x01";
1672 u8 *tmp, *end, *spos;
1673 char *upos, *uend;
1674 int ret = 0;
1675
1676 if (buf_len < 2)
1677 return -1;
1678 if (offset > msg_len)
1679 return -1;
1680
1681 tmp = os_malloc(DNS_HEADER_LEN + P2P_SD_IN_MEMORY_LEN + msg_len);
1682 if (tmp == NULL)
1683 return -1;
1684 spos = tmp + DNS_HEADER_LEN + P2P_SD_IN_MEMORY_LEN;
1685 end = spos + msg_len;
1686 spos += offset;
1687
1688 os_memset(tmp, 0, DNS_HEADER_LEN);
1689 os_memcpy(tmp + DNS_HEADER_LEN, prefix, P2P_SD_IN_MEMORY_LEN);
1690 os_memcpy(tmp + DNS_HEADER_LEN + P2P_SD_IN_MEMORY_LEN, msg, msg_len);
1691
1692 upos = buf;
1693 uend = buf + buf_len;
1694
1695 ret = p2p_sd_dns_uncompress_label(&upos, uend, tmp, &spos, end);
1696 if (ret) {
1697 os_free(tmp);
1698 return ret;
1699 }
1700
1701 if (upos == buf) {
1702 upos[0] = '.';
1703 upos[1] = '\0';
1704 } else if (upos[-1] == '.')
1705 upos[-1] = '\0';
1706
1707 os_free(tmp);
1708 return 0;
1709 }
1710
1711
1712 static struct p2p_srv_bonjour *
1713 wpas_p2p_service_get_bonjour(struct wpa_supplicant *wpa_s,
1714 const struct wpabuf *query)
1715 {
1716 struct p2p_srv_bonjour *bsrv;
1717 size_t len;
1718
1719 len = wpabuf_len(query);
1720 dl_list_for_each(bsrv, &wpa_s->global->p2p_srv_bonjour,
1721 struct p2p_srv_bonjour, list) {
1722 if (len == wpabuf_len(bsrv->query) &&
1723 os_memcmp(wpabuf_head(query), wpabuf_head(bsrv->query),
1724 len) == 0)
1725 return bsrv;
1726 }
1727 return NULL;
1728 }
1729
1730
1731 static struct p2p_srv_upnp *
1732 wpas_p2p_service_get_upnp(struct wpa_supplicant *wpa_s, u8 version,
1733 const char *service)
1734 {
1735 struct p2p_srv_upnp *usrv;
1736
1737 dl_list_for_each(usrv, &wpa_s->global->p2p_srv_upnp,
1738 struct p2p_srv_upnp, list) {
1739 if (version == usrv->version &&
1740 os_strcmp(service, usrv->service) == 0)
1741 return usrv;
1742 }
1743 return NULL;
1744 }
1745
1746
1747 static void wpas_sd_add_proto_not_avail(struct wpabuf *resp, u8 srv_proto,
1748 u8 srv_trans_id)
1749 {
1750 u8 *len_pos;
1751
1752 if (wpabuf_tailroom(resp) < 5)
1753 return;
1754
1755 /* Length (to be filled) */
1756 len_pos = wpabuf_put(resp, 2);
1757 wpabuf_put_u8(resp, srv_proto);
1758 wpabuf_put_u8(resp, srv_trans_id);
1759 /* Status Code */
1760 wpabuf_put_u8(resp, P2P_SD_PROTO_NOT_AVAILABLE);
1761 /* Response Data: empty */
1762 WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos - 2);
1763 }
1764
1765
1766 static void wpas_sd_all_bonjour(struct wpa_supplicant *wpa_s,
1767 struct wpabuf *resp, u8 srv_trans_id)
1768 {
1769 struct p2p_srv_bonjour *bsrv;
1770 u8 *len_pos;
1771
1772 wpa_printf(MSG_DEBUG, "P2P: SD Request for all Bonjour services");
1773
1774 if (dl_list_empty(&wpa_s->global->p2p_srv_bonjour)) {
1775 wpa_printf(MSG_DEBUG, "P2P: Bonjour protocol not available");
1776 return;
1777 }
1778
1779 dl_list_for_each(bsrv, &wpa_s->global->p2p_srv_bonjour,
1780 struct p2p_srv_bonjour, list) {
1781 if (wpabuf_tailroom(resp) <
1782 5 + wpabuf_len(bsrv->query) + wpabuf_len(bsrv->resp))
1783 return;
1784 /* Length (to be filled) */
1785 len_pos = wpabuf_put(resp, 2);
1786 wpabuf_put_u8(resp, P2P_SERV_BONJOUR);
1787 wpabuf_put_u8(resp, srv_trans_id);
1788 /* Status Code */
1789 wpabuf_put_u8(resp, P2P_SD_SUCCESS);
1790 wpa_hexdump_ascii(MSG_DEBUG, "P2P: Matching Bonjour service",
1791 wpabuf_head(bsrv->resp),
1792 wpabuf_len(bsrv->resp));
1793 /* Response Data */
1794 wpabuf_put_buf(resp, bsrv->query); /* Key */
1795 wpabuf_put_buf(resp, bsrv->resp); /* Value */
1796 WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos -
1797 2);
1798 }
1799 }
1800
1801
1802 static int match_bonjour_query(struct p2p_srv_bonjour *bsrv, const u8 *query,
1803 size_t query_len)
1804 {
1805 char str_rx[256], str_srv[256];
1806
1807 if (query_len < 3 || wpabuf_len(bsrv->query) < 3)
1808 return 0; /* Too short to include DNS Type and Version */
1809 if (os_memcmp(query + query_len - 3,
1810 wpabuf_head_u8(bsrv->query) + wpabuf_len(bsrv->query) - 3,
1811 3) != 0)
1812 return 0; /* Mismatch in DNS Type or Version */
1813 if (query_len == wpabuf_len(bsrv->query) &&
1814 os_memcmp(query, wpabuf_head(bsrv->query), query_len - 3) == 0)
1815 return 1; /* Binary match */
1816
1817 if (p2p_sd_dns_uncompress(str_rx, sizeof(str_rx), query, query_len - 3,
1818 0))
1819 return 0; /* Failed to uncompress query */
1820 if (p2p_sd_dns_uncompress(str_srv, sizeof(str_srv),
1821 wpabuf_head(bsrv->query),
1822 wpabuf_len(bsrv->query) - 3, 0))
1823 return 0; /* Failed to uncompress service */
1824
1825 return os_strcmp(str_rx, str_srv) == 0;
1826 }
1827
1828
1829 static void wpas_sd_req_bonjour(struct wpa_supplicant *wpa_s,
1830 struct wpabuf *resp, u8 srv_trans_id,
1831 const u8 *query, size_t query_len)
1832 {
1833 struct p2p_srv_bonjour *bsrv;
1834 u8 *len_pos;
1835 int matches = 0;
1836
1837 wpa_hexdump_ascii(MSG_DEBUG, "P2P: SD Request for Bonjour",
1838 query, query_len);
1839 if (dl_list_empty(&wpa_s->global->p2p_srv_bonjour)) {
1840 wpa_printf(MSG_DEBUG, "P2P: Bonjour protocol not available");
1841 wpas_sd_add_proto_not_avail(resp, P2P_SERV_BONJOUR,
1842 srv_trans_id);
1843 return;
1844 }
1845
1846 if (query_len == 0) {
1847 wpas_sd_all_bonjour(wpa_s, resp, srv_trans_id);
1848 return;
1849 }
1850
1851 dl_list_for_each(bsrv, &wpa_s->global->p2p_srv_bonjour,
1852 struct p2p_srv_bonjour, list) {
1853 if (!match_bonjour_query(bsrv, query, query_len))
1854 continue;
1855
1856 if (wpabuf_tailroom(resp) <
1857 5 + query_len + wpabuf_len(bsrv->resp))
1858 return;
1859
1860 matches++;
1861
1862 /* Length (to be filled) */
1863 len_pos = wpabuf_put(resp, 2);
1864 wpabuf_put_u8(resp, P2P_SERV_BONJOUR);
1865 wpabuf_put_u8(resp, srv_trans_id);
1866
1867 /* Status Code */
1868 wpabuf_put_u8(resp, P2P_SD_SUCCESS);
1869 wpa_hexdump_ascii(MSG_DEBUG, "P2P: Matching Bonjour service",
1870 wpabuf_head(bsrv->resp),
1871 wpabuf_len(bsrv->resp));
1872
1873 /* Response Data */
1874 wpabuf_put_data(resp, query, query_len); /* Key */
1875 wpabuf_put_buf(resp, bsrv->resp); /* Value */
1876
1877 WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos - 2);
1878 }
1879
1880 if (matches == 0) {
1881 wpa_printf(MSG_DEBUG, "P2P: Requested Bonjour service not "
1882 "available");
1883 if (wpabuf_tailroom(resp) < 5)
1884 return;
1885
1886 /* Length (to be filled) */
1887 len_pos = wpabuf_put(resp, 2);
1888 wpabuf_put_u8(resp, P2P_SERV_BONJOUR);
1889 wpabuf_put_u8(resp, srv_trans_id);
1890
1891 /* Status Code */
1892 wpabuf_put_u8(resp, P2P_SD_REQUESTED_INFO_NOT_AVAILABLE);
1893 /* Response Data: empty */
1894 WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos -
1895 2);
1896 }
1897 }
1898
1899
1900 static void wpas_sd_all_upnp(struct wpa_supplicant *wpa_s,
1901 struct wpabuf *resp, u8 srv_trans_id)
1902 {
1903 struct p2p_srv_upnp *usrv;
1904 u8 *len_pos;
1905
1906 wpa_printf(MSG_DEBUG, "P2P: SD Request for all UPnP services");
1907
1908 if (dl_list_empty(&wpa_s->global->p2p_srv_upnp)) {
1909 wpa_printf(MSG_DEBUG, "P2P: UPnP protocol not available");
1910 return;
1911 }
1912
1913 dl_list_for_each(usrv, &wpa_s->global->p2p_srv_upnp,
1914 struct p2p_srv_upnp, list) {
1915 if (wpabuf_tailroom(resp) < 5 + 1 + os_strlen(usrv->service))
1916 return;
1917
1918 /* Length (to be filled) */
1919 len_pos = wpabuf_put(resp, 2);
1920 wpabuf_put_u8(resp, P2P_SERV_UPNP);
1921 wpabuf_put_u8(resp, srv_trans_id);
1922
1923 /* Status Code */
1924 wpabuf_put_u8(resp, P2P_SD_SUCCESS);
1925 /* Response Data */
1926 wpabuf_put_u8(resp, usrv->version);
1927 wpa_printf(MSG_DEBUG, "P2P: Matching UPnP Service: %s",
1928 usrv->service);
1929 wpabuf_put_str(resp, usrv->service);
1930 WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos -
1931 2);
1932 }
1933 }
1934
1935
1936 static void wpas_sd_req_upnp(struct wpa_supplicant *wpa_s,
1937 struct wpabuf *resp, u8 srv_trans_id,
1938 const u8 *query, size_t query_len)
1939 {
1940 struct p2p_srv_upnp *usrv;
1941 u8 *len_pos;
1942 u8 version;
1943 char *str;
1944 int count = 0;
1945
1946 wpa_hexdump_ascii(MSG_DEBUG, "P2P: SD Request for UPnP",
1947 query, query_len);
1948
1949 if (dl_list_empty(&wpa_s->global->p2p_srv_upnp)) {
1950 wpa_printf(MSG_DEBUG, "P2P: UPnP protocol not available");
1951 wpas_sd_add_proto_not_avail(resp, P2P_SERV_UPNP,
1952 srv_trans_id);
1953 return;
1954 }
1955
1956 if (query_len == 0) {
1957 wpas_sd_all_upnp(wpa_s, resp, srv_trans_id);
1958 return;
1959 }
1960
1961 if (wpabuf_tailroom(resp) < 5)
1962 return;
1963
1964 /* Length (to be filled) */
1965 len_pos = wpabuf_put(resp, 2);
1966 wpabuf_put_u8(resp, P2P_SERV_UPNP);
1967 wpabuf_put_u8(resp, srv_trans_id);
1968
1969 version = query[0];
1970 str = os_malloc(query_len);
1971 if (str == NULL)
1972 return;
1973 os_memcpy(str, query + 1, query_len - 1);
1974 str[query_len - 1] = '\0';
1975
1976 dl_list_for_each(usrv, &wpa_s->global->p2p_srv_upnp,
1977 struct p2p_srv_upnp, list) {
1978 if (version != usrv->version)
1979 continue;
1980
1981 if (os_strcmp(str, "ssdp:all") != 0 &&
1982 os_strstr(usrv->service, str) == NULL)
1983 continue;
1984
1985 if (wpabuf_tailroom(resp) < 2)
1986 break;
1987 if (count == 0) {
1988 /* Status Code */
1989 wpabuf_put_u8(resp, P2P_SD_SUCCESS);
1990 /* Response Data */
1991 wpabuf_put_u8(resp, version);
1992 } else
1993 wpabuf_put_u8(resp, ',');
1994
1995 count++;
1996
1997 wpa_printf(MSG_DEBUG, "P2P: Matching UPnP Service: %s",
1998 usrv->service);
1999 if (wpabuf_tailroom(resp) < os_strlen(usrv->service))
2000 break;
2001 wpabuf_put_str(resp, usrv->service);
2002 }
2003 os_free(str);
2004
2005 if (count == 0) {
2006 wpa_printf(MSG_DEBUG, "P2P: Requested UPnP service not "
2007 "available");
2008 /* Status Code */
2009 wpabuf_put_u8(resp, P2P_SD_REQUESTED_INFO_NOT_AVAILABLE);
2010 /* Response Data: empty */
2011 }
2012
2013 WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos - 2);
2014 }
2015
2016
2017 #ifdef CONFIG_WIFI_DISPLAY
2018 static void wpas_sd_req_wfd(struct wpa_supplicant *wpa_s,
2019 struct wpabuf *resp, u8 srv_trans_id,
2020 const u8 *query, size_t query_len)
2021 {
2022 const u8 *pos;
2023 u8 role;
2024 u8 *len_pos;
2025
2026 wpa_hexdump(MSG_DEBUG, "P2P: SD Request for WFD", query, query_len);
2027
2028 if (!wpa_s->global->wifi_display) {
2029 wpa_printf(MSG_DEBUG, "P2P: WFD protocol not available");
2030 wpas_sd_add_proto_not_avail(resp, P2P_SERV_WIFI_DISPLAY,
2031 srv_trans_id);
2032 return;
2033 }
2034
2035 if (query_len < 1) {
2036 wpa_printf(MSG_DEBUG, "P2P: Missing WFD Requested Device "
2037 "Role");
2038 return;
2039 }
2040
2041 if (wpabuf_tailroom(resp) < 5)
2042 return;
2043
2044 pos = query;
2045 role = *pos++;
2046 wpa_printf(MSG_DEBUG, "P2P: WSD for device role 0x%x", role);
2047
2048 /* TODO: role specific handling */
2049
2050 /* Length (to be filled) */
2051 len_pos = wpabuf_put(resp, 2);
2052 wpabuf_put_u8(resp, P2P_SERV_WIFI_DISPLAY);
2053 wpabuf_put_u8(resp, srv_trans_id);
2054 wpabuf_put_u8(resp, P2P_SD_SUCCESS); /* Status Code */
2055
2056 while (pos < query + query_len) {
2057 if (*pos < MAX_WFD_SUBELEMS &&
2058 wpa_s->global->wfd_subelem[*pos] &&
2059 wpabuf_tailroom(resp) >=
2060 wpabuf_len(wpa_s->global->wfd_subelem[*pos])) {
2061 wpa_printf(MSG_DEBUG, "P2P: Add WSD response "
2062 "subelement %u", *pos);
2063 wpabuf_put_buf(resp, wpa_s->global->wfd_subelem[*pos]);
2064 }
2065 pos++;
2066 }
2067
2068 WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos - 2);
2069 }
2070 #endif /* CONFIG_WIFI_DISPLAY */
2071
2072
2073 static void wpas_sd_request(void *ctx, int freq, const u8 *sa, u8 dialog_token,
2074 u16 update_indic, const u8 *tlvs, size_t tlvs_len)
2075 {
2076 struct wpa_supplicant *wpa_s = ctx;
2077 const u8 *pos = tlvs;
2078 const u8 *end = tlvs + tlvs_len;
2079 const u8 *tlv_end;
2080 u16 slen;
2081 struct wpabuf *resp;
2082 u8 srv_proto, srv_trans_id;
2083 size_t buf_len;
2084 char *buf;
2085
2086 wpa_hexdump(MSG_MSGDUMP, "P2P: Service Discovery Request TLVs",
2087 tlvs, tlvs_len);
2088 buf_len = 2 * tlvs_len + 1;
2089 buf = os_malloc(buf_len);
2090 if (buf) {
2091 wpa_snprintf_hex(buf, buf_len, tlvs, tlvs_len);
2092 wpa_msg_ctrl(wpa_s, MSG_INFO, P2P_EVENT_SERV_DISC_REQ "%d "
2093 MACSTR " %u %u %s",
2094 freq, MAC2STR(sa), dialog_token, update_indic,
2095 buf);
2096 os_free(buf);
2097 }
2098
2099 if (wpa_s->p2p_sd_over_ctrl_iface) {
2100 wpas_notify_p2p_sd_request(wpa_s, freq, sa, dialog_token,
2101 update_indic, tlvs, tlvs_len);
2102 return; /* to be processed by an external program */
2103 }
2104
2105 resp = wpabuf_alloc(10000);
2106 if (resp == NULL)
2107 return;
2108
2109 while (pos + 1 < end) {
2110 wpa_printf(MSG_DEBUG, "P2P: Service Request TLV");
2111 slen = WPA_GET_LE16(pos);
2112 pos += 2;
2113 if (pos + slen > end || slen < 2) {
2114 wpa_printf(MSG_DEBUG, "P2P: Unexpected Query Data "
2115 "length");
2116 wpabuf_free(resp);
2117 return;
2118 }
2119 tlv_end = pos + slen;
2120
2121 srv_proto = *pos++;
2122 wpa_printf(MSG_DEBUG, "P2P: Service Protocol Type %u",
2123 srv_proto);
2124 srv_trans_id = *pos++;
2125 wpa_printf(MSG_DEBUG, "P2P: Service Transaction ID %u",
2126 srv_trans_id);
2127
2128 wpa_hexdump(MSG_MSGDUMP, "P2P: Query Data",
2129 pos, tlv_end - pos);
2130
2131
2132 if (wpa_s->force_long_sd) {
2133 wpa_printf(MSG_DEBUG, "P2P: SD test - force long "
2134 "response");
2135 wpas_sd_all_bonjour(wpa_s, resp, srv_trans_id);
2136 wpas_sd_all_upnp(wpa_s, resp, srv_trans_id);
2137 goto done;
2138 }
2139
2140 switch (srv_proto) {
2141 case P2P_SERV_ALL_SERVICES:
2142 wpa_printf(MSG_DEBUG, "P2P: Service Discovery Request "
2143 "for all services");
2144 if (dl_list_empty(&wpa_s->global->p2p_srv_upnp) &&
2145 dl_list_empty(&wpa_s->global->p2p_srv_bonjour)) {
2146 wpa_printf(MSG_DEBUG, "P2P: No service "
2147 "discovery protocols available");
2148 wpas_sd_add_proto_not_avail(
2149 resp, P2P_SERV_ALL_SERVICES,
2150 srv_trans_id);
2151 break;
2152 }
2153 wpas_sd_all_bonjour(wpa_s, resp, srv_trans_id);
2154 wpas_sd_all_upnp(wpa_s, resp, srv_trans_id);
2155 break;
2156 case P2P_SERV_BONJOUR:
2157 wpas_sd_req_bonjour(wpa_s, resp, srv_trans_id,
2158 pos, tlv_end - pos);
2159 break;
2160 case P2P_SERV_UPNP:
2161 wpas_sd_req_upnp(wpa_s, resp, srv_trans_id,
2162 pos, tlv_end - pos);
2163 break;
2164 #ifdef CONFIG_WIFI_DISPLAY
2165 case P2P_SERV_WIFI_DISPLAY:
2166 wpas_sd_req_wfd(wpa_s, resp, srv_trans_id,
2167 pos, tlv_end - pos);
2168 break;
2169 #endif /* CONFIG_WIFI_DISPLAY */
2170 default:
2171 wpa_printf(MSG_DEBUG, "P2P: Unavailable service "
2172 "protocol %u", srv_proto);
2173 wpas_sd_add_proto_not_avail(resp, srv_proto,
2174 srv_trans_id);
2175 break;
2176 }
2177
2178 pos = tlv_end;
2179 }
2180
2181 done:
2182 wpas_notify_p2p_sd_request(wpa_s, freq, sa, dialog_token,
2183 update_indic, tlvs, tlvs_len);
2184
2185 wpas_p2p_sd_response(wpa_s, freq, sa, dialog_token, resp);
2186
2187 wpabuf_free(resp);
2188 }
2189
2190
2191 static void wpas_sd_response(void *ctx, const u8 *sa, u16 update_indic,
2192 const u8 *tlvs, size_t tlvs_len)
2193 {
2194 struct wpa_supplicant *wpa_s = ctx;
2195 const u8 *pos = tlvs;
2196 const u8 *end = tlvs + tlvs_len;
2197 const u8 *tlv_end;
2198 u16 slen;
2199 size_t buf_len;
2200 char *buf;
2201
2202 wpa_hexdump(MSG_MSGDUMP, "P2P: Service Discovery Response TLVs",
2203 tlvs, tlvs_len);
2204 if (tlvs_len > 1500) {
2205 /* TODO: better way for handling this */
2206 wpa_msg_ctrl(wpa_s, MSG_INFO,
2207 P2P_EVENT_SERV_DISC_RESP MACSTR
2208 " %u <long response: %u bytes>",
2209 MAC2STR(sa), update_indic,
2210 (unsigned int) tlvs_len);
2211 } else {
2212 buf_len = 2 * tlvs_len + 1;
2213 buf = os_malloc(buf_len);
2214 if (buf) {
2215 wpa_snprintf_hex(buf, buf_len, tlvs, tlvs_len);
2216 wpa_msg_ctrl(wpa_s, MSG_INFO,
2217 P2P_EVENT_SERV_DISC_RESP MACSTR " %u %s",
2218 MAC2STR(sa), update_indic, buf);
2219 os_free(buf);
2220 }
2221 }
2222
2223 while (pos < end) {
2224 u8 srv_proto, srv_trans_id, status;
2225
2226 wpa_printf(MSG_DEBUG, "P2P: Service Response TLV");
2227 slen = WPA_GET_LE16(pos);
2228 pos += 2;
2229 if (pos + slen > end || slen < 3) {
2230 wpa_printf(MSG_DEBUG, "P2P: Unexpected Response Data "
2231 "length");
2232 return;
2233 }
2234 tlv_end = pos + slen;
2235
2236 srv_proto = *pos++;
2237 wpa_printf(MSG_DEBUG, "P2P: Service Protocol Type %u",
2238 srv_proto);
2239 srv_trans_id = *pos++;
2240 wpa_printf(MSG_DEBUG, "P2P: Service Transaction ID %u",
2241 srv_trans_id);
2242 status = *pos++;
2243 wpa_printf(MSG_DEBUG, "P2P: Status Code ID %u",
2244 status);
2245
2246 wpa_hexdump(MSG_MSGDUMP, "P2P: Response Data",
2247 pos, tlv_end - pos);
2248
2249 pos = tlv_end;
2250 }
2251
2252 wpas_notify_p2p_sd_response(wpa_s, sa, update_indic, tlvs, tlvs_len);
2253 }
2254
2255
2256 u64 wpas_p2p_sd_request(struct wpa_supplicant *wpa_s, const u8 *dst,
2257 const struct wpabuf *tlvs)
2258 {
2259 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
2260 return 0;
2261 return (uintptr_t) p2p_sd_request(wpa_s->global->p2p, dst, tlvs);
2262 }
2263
2264
2265 u64 wpas_p2p_sd_request_upnp(struct wpa_supplicant *wpa_s, const u8 *dst,
2266 u8 version, const char *query)
2267 {
2268 struct wpabuf *tlvs;
2269 u64 ret;
2270
2271 tlvs = wpabuf_alloc(2 + 1 + 1 + 1 + os_strlen(query));
2272 if (tlvs == NULL)
2273 return 0;
2274 wpabuf_put_le16(tlvs, 1 + 1 + 1 + os_strlen(query));
2275 wpabuf_put_u8(tlvs, P2P_SERV_UPNP); /* Service Protocol Type */
2276 wpabuf_put_u8(tlvs, 1); /* Service Transaction ID */
2277 wpabuf_put_u8(tlvs, version);
2278 wpabuf_put_str(tlvs, query);
2279 ret = wpas_p2p_sd_request(wpa_s, dst, tlvs);
2280 wpabuf_free(tlvs);
2281 return ret;
2282 }
2283
2284
2285 #ifdef CONFIG_WIFI_DISPLAY
2286
2287 static u64 wpas_p2p_sd_request_wfd(struct wpa_supplicant *wpa_s, const u8 *dst,
2288 const struct wpabuf *tlvs)
2289 {
2290 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
2291 return 0;
2292 return (uintptr_t) p2p_sd_request_wfd(wpa_s->global->p2p, dst, tlvs);
2293 }
2294
2295
2296 #define MAX_WFD_SD_SUBELEMS 20
2297
2298 static void wfd_add_sd_req_role(struct wpabuf *tlvs, u8 id, u8 role,
2299 const char *subelems)
2300 {
2301 u8 *len;
2302 const char *pos;
2303 int val;
2304 int count = 0;
2305
2306 len = wpabuf_put(tlvs, 2);
2307 wpabuf_put_u8(tlvs, P2P_SERV_WIFI_DISPLAY); /* Service Protocol Type */
2308 wpabuf_put_u8(tlvs, id); /* Service Transaction ID */
2309
2310 wpabuf_put_u8(tlvs, role);
2311
2312 pos = subelems;
2313 while (*pos) {
2314 val = atoi(pos);
2315 if (val >= 0 && val < 256) {
2316 wpabuf_put_u8(tlvs, val);
2317 count++;
2318 if (count == MAX_WFD_SD_SUBELEMS)
2319 break;
2320 }
2321 pos = os_strchr(pos + 1, ',');
2322 if (pos == NULL)
2323 break;
2324 pos++;
2325 }
2326
2327 WPA_PUT_LE16(len, (u8 *) wpabuf_put(tlvs, 0) - len - 2);
2328 }
2329
2330
2331 u64 wpas_p2p_sd_request_wifi_display(struct wpa_supplicant *wpa_s,
2332 const u8 *dst, const char *role)
2333 {
2334 struct wpabuf *tlvs;
2335 u64 ret;
2336 const char *subelems;
2337 u8 id = 1;
2338
2339 subelems = os_strchr(role, ' ');
2340 if (subelems == NULL)
2341 return 0;
2342 subelems++;
2343
2344 tlvs = wpabuf_alloc(4 * (2 + 1 + 1 + 1 + MAX_WFD_SD_SUBELEMS));
2345 if (tlvs == NULL)
2346 return 0;
2347
2348 if (os_strstr(role, "[source]"))
2349 wfd_add_sd_req_role(tlvs, id++, 0x00, subelems);
2350 if (os_strstr(role, "[pri-sink]"))
2351 wfd_add_sd_req_role(tlvs, id++, 0x01, subelems);
2352 if (os_strstr(role, "[sec-sink]"))
2353 wfd_add_sd_req_role(tlvs, id++, 0x02, subelems);
2354 if (os_strstr(role, "[source+sink]"))
2355 wfd_add_sd_req_role(tlvs, id++, 0x03, subelems);
2356
2357 ret = wpas_p2p_sd_request_wfd(wpa_s, dst, tlvs);
2358 wpabuf_free(tlvs);
2359 return ret;
2360 }
2361
2362 #endif /* CONFIG_WIFI_DISPLAY */
2363
2364
2365 int wpas_p2p_sd_cancel_request(struct wpa_supplicant *wpa_s, u64 req)
2366 {
2367 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
2368 return -1;
2369 return p2p_sd_cancel_request(wpa_s->global->p2p,
2370 (void *) (uintptr_t) req);
2371 }
2372
2373
2374 void wpas_p2p_sd_response(struct wpa_supplicant *wpa_s, int freq,
2375 const u8 *dst, u8 dialog_token,
2376 const struct wpabuf *resp_tlvs)
2377 {
2378 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
2379 return;
2380 p2p_sd_response(wpa_s->global->p2p, freq, dst, dialog_token,
2381 resp_tlvs);
2382 }
2383
2384
2385 void wpas_p2p_sd_service_update(struct wpa_supplicant *wpa_s)
2386 {
2387 if (wpa_s->global->p2p)
2388 p2p_sd_service_update(wpa_s->global->p2p);
2389 }
2390
2391
2392 static void wpas_p2p_srv_bonjour_free(struct p2p_srv_bonjour *bsrv)
2393 {
2394 dl_list_del(&bsrv->list);
2395 wpabuf_free(bsrv->query);
2396 wpabuf_free(bsrv->resp);
2397 os_free(bsrv);
2398 }
2399
2400
2401 static void wpas_p2p_srv_upnp_free(struct p2p_srv_upnp *usrv)
2402 {
2403 dl_list_del(&usrv->list);
2404 os_free(usrv->service);
2405 os_free(usrv);
2406 }
2407
2408
2409 void wpas_p2p_service_flush(struct wpa_supplicant *wpa_s)
2410 {
2411 struct p2p_srv_bonjour *bsrv, *bn;
2412 struct p2p_srv_upnp *usrv, *un;
2413
2414 dl_list_for_each_safe(bsrv, bn, &wpa_s->global->p2p_srv_bonjour,
2415 struct p2p_srv_bonjour, list)
2416 wpas_p2p_srv_bonjour_free(bsrv);
2417
2418 dl_list_for_each_safe(usrv, un, &wpa_s->global->p2p_srv_upnp,
2419 struct p2p_srv_upnp, list)
2420 wpas_p2p_srv_upnp_free(usrv);
2421
2422 wpas_p2p_sd_service_update(wpa_s);
2423 }
2424
2425
2426 int wpas_p2p_service_add_bonjour(struct wpa_supplicant *wpa_s,
2427 struct wpabuf *query, struct wpabuf *resp)
2428 {
2429 struct p2p_srv_bonjour *bsrv;
2430
2431 bsrv = os_zalloc(sizeof(*bsrv));
2432 if (bsrv == NULL)
2433 return -1;
2434 bsrv->query = query;
2435 bsrv->resp = resp;
2436 dl_list_add(&wpa_s->global->p2p_srv_bonjour, &bsrv->list);
2437
2438 wpas_p2p_sd_service_update(wpa_s);
2439 return 0;
2440 }
2441
2442
2443 int wpas_p2p_service_del_bonjour(struct wpa_supplicant *wpa_s,
2444 const struct wpabuf *query)
2445 {
2446 struct p2p_srv_bonjour *bsrv;
2447
2448 bsrv = wpas_p2p_service_get_bonjour(wpa_s, query);
2449 if (bsrv == NULL)
2450 return -1;
2451 wpas_p2p_srv_bonjour_free(bsrv);
2452 wpas_p2p_sd_service_update(wpa_s);
2453 return 0;
2454 }
2455
2456
2457 int wpas_p2p_service_add_upnp(struct wpa_supplicant *wpa_s, u8 version,
2458 const char *service)
2459 {
2460 struct p2p_srv_upnp *usrv;
2461
2462 if (wpas_p2p_service_get_upnp(wpa_s, version, service))
2463 return 0; /* Already listed */
2464 usrv = os_zalloc(sizeof(*usrv));
2465 if (usrv == NULL)
2466 return -1;
2467 usrv->version = version;
2468 usrv->service = os_strdup(service);
2469 if (usrv->service == NULL) {
2470 os_free(usrv);
2471 return -1;
2472 }
2473 dl_list_add(&wpa_s->global->p2p_srv_upnp, &usrv->list);
2474
2475 wpas_p2p_sd_service_update(wpa_s);
2476 return 0;
2477 }
2478
2479
2480 int wpas_p2p_service_del_upnp(struct wpa_supplicant *wpa_s, u8 version,
2481 const char *service)
2482 {
2483 struct p2p_srv_upnp *usrv;
2484
2485 usrv = wpas_p2p_service_get_upnp(wpa_s, version, service);
2486 if (usrv == NULL)
2487 return -1;
2488 wpas_p2p_srv_upnp_free(usrv);
2489 wpas_p2p_sd_service_update(wpa_s);
2490 return 0;
2491 }
2492
2493
2494 static void wpas_prov_disc_local_display(struct wpa_supplicant *wpa_s,
2495 const u8 *peer, const char *params,
2496 unsigned int generated_pin)
2497 {
2498 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_SHOW_PIN MACSTR
2499 " %08d%s", MAC2STR(peer), generated_pin, params);
2500 }
2501
2502
2503 static void wpas_prov_disc_local_keypad(struct wpa_supplicant *wpa_s,
2504 const u8 *peer, const char *params)
2505 {
2506 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_ENTER_PIN MACSTR
2507 "%s", MAC2STR(peer), params);
2508 }
2509
2510
2511 static void wpas_prov_disc_req(void *ctx, const u8 *peer, u16 config_methods,
2512 const u8 *dev_addr, const u8 *pri_dev_type,
2513 const char *dev_name, u16 supp_config_methods,
2514 u8 dev_capab, u8 group_capab, const u8 *group_id,
2515 size_t group_id_len)
2516 {
2517 struct wpa_supplicant *wpa_s = ctx;
2518 char devtype[WPS_DEV_TYPE_BUFSIZE];
2519 char params[300];
2520 u8 empty_dev_type[8];
2521 unsigned int generated_pin = 0;
2522 struct wpa_supplicant *group = NULL;
2523
2524 if (group_id) {
2525 for (group = wpa_s->global->ifaces; group; group = group->next)
2526 {
2527 struct wpa_ssid *s = group->current_ssid;
2528 if (s != NULL &&
2529 s->mode == WPAS_MODE_P2P_GO &&
2530 group_id_len - ETH_ALEN == s->ssid_len &&
2531 os_memcmp(group_id + ETH_ALEN, s->ssid,
2532 s->ssid_len) == 0)
2533 break;
2534 }
2535 }
2536
2537 if (pri_dev_type == NULL) {
2538 os_memset(empty_dev_type, 0, sizeof(empty_dev_type));
2539 pri_dev_type = empty_dev_type;
2540 }
2541 os_snprintf(params, sizeof(params), " p2p_dev_addr=" MACSTR
2542 " pri_dev_type=%s name='%s' config_methods=0x%x "
2543 "dev_capab=0x%x group_capab=0x%x%s%s",
2544 MAC2STR(dev_addr),
2545 wps_dev_type_bin2str(pri_dev_type, devtype,
2546 sizeof(devtype)),
2547 dev_name, supp_config_methods, dev_capab, group_capab,
2548 group ? " group=" : "",
2549 group ? group->ifname : "");
2550 params[sizeof(params) - 1] = '\0';
2551
2552 if (config_methods & WPS_CONFIG_DISPLAY) {
2553 generated_pin = wps_generate_pin();
2554 wpas_prov_disc_local_display(wpa_s, peer, params,
2555 generated_pin);
2556 } else if (config_methods & WPS_CONFIG_KEYPAD)
2557 wpas_prov_disc_local_keypad(wpa_s, peer, params);
2558 else if (config_methods & WPS_CONFIG_PUSHBUTTON)
2559 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_PBC_REQ
2560 MACSTR "%s", MAC2STR(peer), params);
2561
2562 wpas_notify_p2p_provision_discovery(wpa_s, peer, 1 /* request */,
2563 P2P_PROV_DISC_SUCCESS,
2564 config_methods, generated_pin);
2565 }
2566
2567
2568 static void wpas_prov_disc_resp(void *ctx, const u8 *peer, u16 config_methods)
2569 {
2570 struct wpa_supplicant *wpa_s = ctx;
2571 unsigned int generated_pin = 0;
2572 char params[20];
2573
2574 if (wpa_s->pending_pd_before_join &&
2575 (os_memcmp(peer, wpa_s->pending_join_dev_addr, ETH_ALEN) == 0 ||
2576 os_memcmp(peer, wpa_s->pending_join_iface_addr, ETH_ALEN) == 0)) {
2577 wpa_s->pending_pd_before_join = 0;
2578 wpa_printf(MSG_DEBUG, "P2P: Starting pending "
2579 "join-existing-group operation");
2580 wpas_p2p_join_start(wpa_s);
2581 return;
2582 }
2583
2584 if (wpa_s->pending_pd_use == AUTO_PD_JOIN ||
2585 wpa_s->pending_pd_use == AUTO_PD_GO_NEG)
2586 os_snprintf(params, sizeof(params), " peer_go=%d",
2587 wpa_s->pending_pd_use == AUTO_PD_JOIN);
2588 else
2589 params[0] = '\0';
2590
2591 if (config_methods & WPS_CONFIG_DISPLAY)
2592 wpas_prov_disc_local_keypad(wpa_s, peer, params);
2593 else if (config_methods & WPS_CONFIG_KEYPAD) {
2594 generated_pin = wps_generate_pin();
2595 wpas_prov_disc_local_display(wpa_s, peer, params,
2596 generated_pin);
2597 } else if (config_methods & WPS_CONFIG_PUSHBUTTON)
2598 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_PBC_RESP
2599 MACSTR "%s", MAC2STR(peer), params);
2600
2601 wpas_notify_p2p_provision_discovery(wpa_s, peer, 0 /* response */,
2602 P2P_PROV_DISC_SUCCESS,
2603 config_methods, generated_pin);
2604 }
2605
2606
2607 static void wpas_prov_disc_fail(void *ctx, const u8 *peer,
2608 enum p2p_prov_disc_status status)
2609 {
2610 struct wpa_supplicant *wpa_s = ctx;
2611
2612 if (wpa_s->p2p_fallback_to_go_neg) {
2613 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: PD for p2p_connect-auto "
2614 "failed - fall back to GO Negotiation");
2615 wpas_p2p_fallback_to_go_neg(wpa_s, 0);
2616 return;
2617 }
2618
2619 if (status == P2P_PROV_DISC_TIMEOUT_JOIN) {
2620 wpa_s->pending_pd_before_join = 0;
2621 wpa_printf(MSG_DEBUG, "P2P: Starting pending "
2622 "join-existing-group operation (no ACK for PD "
2623 "Req attempts)");
2624 wpas_p2p_join_start(wpa_s);
2625 return;
2626 }
2627
2628 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_FAILURE
2629 " p2p_dev_addr=" MACSTR " status=%d",
2630 MAC2STR(peer), status);
2631
2632 wpas_notify_p2p_provision_discovery(wpa_s, peer, 0 /* response */,
2633 status, 0, 0);
2634 }
2635
2636
2637 static int freq_included(const struct p2p_channels *channels, unsigned int freq)
2638 {
2639 if (channels == NULL)
2640 return 1; /* Assume no restrictions */
2641 return p2p_channels_includes_freq(channels, freq);
2642
2643 }
2644
2645
2646 static u8 wpas_invitation_process(void *ctx, const u8 *sa, const u8 *bssid,
2647 const u8 *go_dev_addr, const u8 *ssid,
2648 size_t ssid_len, int *go, u8 *group_bssid,
2649 int *force_freq, int persistent_group,
2650 const struct p2p_channels *channels)
2651 {
2652 struct wpa_supplicant *wpa_s = ctx;
2653 struct wpa_ssid *s;
2654 int res;
2655 struct wpa_supplicant *grp;
2656
2657 if (!persistent_group) {
2658 wpa_printf(MSG_DEBUG, "P2P: Invitation from " MACSTR
2659 " to join an active group", MAC2STR(sa));
2660 if (!is_zero_ether_addr(wpa_s->p2p_auth_invite) &&
2661 (os_memcmp(go_dev_addr, wpa_s->p2p_auth_invite, ETH_ALEN)
2662 == 0 ||
2663 os_memcmp(sa, wpa_s->p2p_auth_invite, ETH_ALEN) == 0)) {
2664 wpa_printf(MSG_DEBUG, "P2P: Accept previously "
2665 "authorized invitation");
2666 goto accept_inv;
2667 }
2668 /*
2669 * Do not accept the invitation automatically; notify user and
2670 * request approval.
2671 */
2672 return P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE;
2673 }
2674
2675 grp = wpas_get_p2p_group(wpa_s, ssid, ssid_len, go);
2676 if (grp) {
2677 wpa_printf(MSG_DEBUG, "P2P: Accept invitation to already "
2678 "running persistent group");
2679 if (*go)
2680 os_memcpy(group_bssid, grp->own_addr, ETH_ALEN);
2681 goto accept_inv;
2682 }
2683
2684 if (!is_zero_ether_addr(wpa_s->p2p_auth_invite) &&
2685 os_memcmp(sa, wpa_s->p2p_auth_invite, ETH_ALEN) == 0) {
2686 wpa_printf(MSG_DEBUG, "P2P: Accept previously initiated "
2687 "invitation to re-invoke a persistent group");
2688 } else if (!wpa_s->conf->persistent_reconnect)
2689 return P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE;
2690
2691 for (s = wpa_s->conf->ssid; s; s = s->next) {
2692 if (s->disabled == 2 &&
2693 os_memcmp(s->bssid, go_dev_addr, ETH_ALEN) == 0 &&
2694 s->ssid_len == ssid_len &&
2695 os_memcmp(ssid, s->ssid, ssid_len) == 0)
2696 break;
2697 }
2698
2699 if (!s) {
2700 wpa_printf(MSG_DEBUG, "P2P: Invitation from " MACSTR
2701 " requested reinvocation of an unknown group",
2702 MAC2STR(sa));
2703 return P2P_SC_FAIL_UNKNOWN_GROUP;
2704 }
2705
2706 if (s->mode == WPAS_MODE_P2P_GO && !wpas_p2p_create_iface(wpa_s)) {
2707 *go = 1;
2708 if (wpa_s->wpa_state >= WPA_AUTHENTICATING) {
2709 wpa_printf(MSG_DEBUG, "P2P: The only available "
2710 "interface is already in use - reject "
2711 "invitation");
2712 return P2P_SC_FAIL_UNABLE_TO_ACCOMMODATE;
2713 }
2714 os_memcpy(group_bssid, wpa_s->own_addr, ETH_ALEN);
2715 } else if (s->mode == WPAS_MODE_P2P_GO) {
2716 *go = 1;
2717 if (wpas_p2p_add_group_interface(wpa_s, WPA_IF_P2P_GO) < 0)
2718 {
2719 wpa_printf(MSG_ERROR, "P2P: Failed to allocate a new "
2720 "interface address for the group");
2721 return P2P_SC_FAIL_UNABLE_TO_ACCOMMODATE;
2722 }
2723 os_memcpy(group_bssid, wpa_s->pending_interface_addr,
2724 ETH_ALEN);
2725 }
2726
2727 accept_inv:
2728 wpas_p2p_set_own_freq_preference(wpa_s, 0);
2729
2730 /* Get one of the frequencies currently in use */
2731 if (wpas_p2p_valid_oper_freqs(wpa_s, &res, 1) > 0) {
2732 wpa_printf(MSG_DEBUG, "P2P: Trying to prefer a channel already used by one of the interfaces");
2733 wpas_p2p_set_own_freq_preference(wpa_s, res);
2734
2735 if (wpa_s->num_multichan_concurrent < 2 ||
2736 wpas_p2p_num_unused_channels(wpa_s) < 1) {
2737 wpa_printf(MSG_DEBUG, "P2P: No extra channels available - trying to force channel to match a channel already used by one of the interfaces");
2738 *force_freq = res;
2739 }
2740 }
2741
2742 if (*force_freq > 0 && wpa_s->num_multichan_concurrent > 1 &&
2743 wpas_p2p_num_unused_channels(wpa_s) > 0) {
2744 if (*go == 0) {
2745 /* We are the client */
2746 wpa_printf(MSG_DEBUG, "P2P: Peer was found to be "
2747 "running a GO but we are capable of MCC, "
2748 "figure out the best channel to use");
2749 *force_freq = 0;
2750 } else if (!freq_included(channels, *force_freq)) {
2751 /* We are the GO, and *force_freq is not in the
2752 * intersection */
2753 wpa_printf(MSG_DEBUG, "P2P: Forced GO freq %d MHz not "
2754 "in intersection but we are capable of MCC, "
2755 "figure out the best channel to use",
2756 *force_freq);
2757 *force_freq = 0;
2758 }
2759 }
2760
2761 return P2P_SC_SUCCESS;
2762 }
2763
2764
2765 static void wpas_invitation_received(void *ctx, const u8 *sa, const u8 *bssid,
2766 const u8 *ssid, size_t ssid_len,
2767 const u8 *go_dev_addr, u8 status,
2768 int op_freq)
2769 {
2770 struct wpa_supplicant *wpa_s = ctx;
2771 struct wpa_ssid *s;
2772
2773 for (s = wpa_s->conf->ssid; s; s = s->next) {
2774 if (s->disabled == 2 &&
2775 s->ssid_len == ssid_len &&
2776 os_memcmp(ssid, s->ssid, ssid_len) == 0)
2777 break;
2778 }
2779
2780 if (status == P2P_SC_SUCCESS) {
2781 wpa_printf(MSG_DEBUG, "P2P: Invitation from peer " MACSTR
2782 " was accepted; op_freq=%d MHz",
2783 MAC2STR(sa), op_freq);
2784 if (s) {
2785 int go = s->mode == WPAS_MODE_P2P_GO;
2786 wpas_p2p_group_add_persistent(
2787 wpa_s, s, go, go ? op_freq : 0, 0, 0, NULL,
2788 go ? P2P_MAX_INITIAL_CONN_WAIT_GO_REINVOKE : 0);
2789 } else if (bssid) {
2790 wpa_s->user_initiated_pd = 0;
2791 wpas_p2p_join(wpa_s, bssid, go_dev_addr,
2792 wpa_s->p2p_wps_method, 0);
2793 }
2794 return;
2795 }
2796
2797 if (status != P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE) {
2798 wpa_printf(MSG_DEBUG, "P2P: Invitation from peer " MACSTR
2799 " was rejected (status %u)", MAC2STR(sa), status);
2800 return;
2801 }
2802
2803 if (!s) {
2804 if (bssid) {
2805 wpa_msg_global(wpa_s, MSG_INFO,
2806 P2P_EVENT_INVITATION_RECEIVED
2807 "sa=" MACSTR " go_dev_addr=" MACSTR
2808 " bssid=" MACSTR " unknown-network",
2809 MAC2STR(sa), MAC2STR(go_dev_addr),
2810 MAC2STR(bssid));
2811 } else {
2812 wpa_msg_global(wpa_s, MSG_INFO,
2813 P2P_EVENT_INVITATION_RECEIVED
2814 "sa=" MACSTR " go_dev_addr=" MACSTR
2815 " unknown-network",
2816 MAC2STR(sa), MAC2STR(go_dev_addr));
2817 }
2818 return;
2819 }
2820
2821 if (s->mode == WPAS_MODE_P2P_GO && op_freq) {
2822 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_INVITATION_RECEIVED
2823 "sa=" MACSTR " persistent=%d freq=%d",
2824 MAC2STR(sa), s->id, op_freq);
2825 } else {
2826 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_INVITATION_RECEIVED
2827 "sa=" MACSTR " persistent=%d",
2828 MAC2STR(sa), s->id);
2829 }
2830 }
2831
2832
2833 static void wpas_remove_persistent_peer(struct wpa_supplicant *wpa_s,
2834 struct wpa_ssid *ssid,
2835 const u8 *peer, int inv)
2836 {
2837 size_t i;
2838
2839 if (ssid == NULL)
2840 return;
2841
2842 for (i = 0; ssid->p2p_client_list && i < ssid->num_p2p_clients; i++) {
2843 if (os_memcmp(ssid->p2p_client_list + i * ETH_ALEN, peer,
2844 ETH_ALEN) == 0)
2845 break;
2846 }
2847 if (i >= ssid->num_p2p_clients) {
2848 if (ssid->mode != WPAS_MODE_P2P_GO &&
2849 os_memcmp(ssid->bssid, peer, ETH_ALEN) == 0) {
2850 wpa_printf(MSG_DEBUG, "P2P: Remove persistent group %d "
2851 "due to invitation result", ssid->id);
2852 wpas_notify_network_removed(wpa_s, ssid);
2853 wpa_config_remove_network(wpa_s->conf, ssid->id);
2854 return;
2855 }
2856 return; /* Peer not found in client list */
2857 }
2858
2859 wpa_printf(MSG_DEBUG, "P2P: Remove peer " MACSTR " from persistent "
2860 "group %d client list%s",
2861 MAC2STR(peer), ssid->id,
2862 inv ? " due to invitation result" : "");
2863 os_memmove(ssid->p2p_client_list + i * ETH_ALEN,
2864 ssid->p2p_client_list + (i + 1) * ETH_ALEN,
2865 (ssid->num_p2p_clients - i - 1) * ETH_ALEN);
2866 ssid->num_p2p_clients--;
2867 #ifndef CONFIG_NO_CONFIG_WRITE
2868 if (wpa_s->parent->conf->update_config &&
2869 wpa_config_write(wpa_s->parent->confname, wpa_s->parent->conf))
2870 wpa_printf(MSG_DEBUG, "P2P: Failed to update configuration");
2871 #endif /* CONFIG_NO_CONFIG_WRITE */
2872 }
2873
2874
2875 static void wpas_remove_persistent_client(struct wpa_supplicant *wpa_s,
2876 const u8 *peer)
2877 {
2878 struct wpa_ssid *ssid;
2879
2880 wpa_s = wpa_s->global->p2p_invite_group;
2881 if (wpa_s == NULL)
2882 return; /* No known invitation group */
2883 ssid = wpa_s->current_ssid;
2884 if (ssid == NULL || ssid->mode != WPAS_MODE_P2P_GO ||
2885 !ssid->p2p_persistent_group)
2886 return; /* Not operating as a GO in persistent group */
2887 ssid = wpas_p2p_get_persistent(wpa_s->parent, peer,
2888 ssid->ssid, ssid->ssid_len);
2889 wpas_remove_persistent_peer(wpa_s, ssid, peer, 1);
2890 }
2891
2892
2893 static void wpas_invitation_result(void *ctx, int status, const u8 *bssid,
2894 const struct p2p_channels *channels,
2895 const u8 *peer, int neg_freq)
2896 {
2897 struct wpa_supplicant *wpa_s = ctx;
2898 struct wpa_ssid *ssid;
2899 int freq;
2900
2901 if (bssid) {
2902 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_INVITATION_RESULT
2903 "status=%d " MACSTR,
2904 status, MAC2STR(bssid));
2905 } else {
2906 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_INVITATION_RESULT
2907 "status=%d ", status);
2908 }
2909 wpas_notify_p2p_invitation_result(wpa_s, status, bssid);
2910
2911 wpa_printf(MSG_DEBUG, "P2P: Invitation result - status=%d peer=" MACSTR,
2912 status, MAC2STR(peer));
2913 if (wpa_s->pending_invite_ssid_id == -1) {
2914 if (status == P2P_SC_FAIL_UNKNOWN_GROUP)
2915 wpas_remove_persistent_client(wpa_s, peer);
2916 return; /* Invitation to active group */
2917 }
2918
2919 if (status == P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE) {
2920 wpa_printf(MSG_DEBUG, "P2P: Waiting for peer to start another "
2921 "invitation exchange to indicate readiness for "
2922 "re-invocation");
2923 }
2924
2925 if (status != P2P_SC_SUCCESS) {
2926 if (status == P2P_SC_FAIL_UNKNOWN_GROUP) {
2927 ssid = wpa_config_get_network(
2928 wpa_s->conf, wpa_s->pending_invite_ssid_id);
2929 wpas_remove_persistent_peer(wpa_s, ssid, peer, 1);
2930 }
2931 wpas_p2p_remove_pending_group_interface(wpa_s);
2932 return;
2933 }
2934
2935 ssid = wpa_config_get_network(wpa_s->conf,
2936 wpa_s->pending_invite_ssid_id);
2937 if (ssid == NULL) {
2938 wpa_printf(MSG_ERROR, "P2P: Could not find persistent group "
2939 "data matching with invitation");
2940 return;
2941 }
2942
2943 /*
2944 * The peer could have missed our ctrl::ack frame for Invitation
2945 * Response and continue retransmitting the frame. To reduce the
2946 * likelihood of the peer not getting successful TX status for the
2947 * Invitation Response frame, wait a short time here before starting
2948 * the persistent group so that we will remain on the current channel to
2949 * acknowledge any possible retransmission from the peer.
2950 */
2951 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: 50 ms wait on current channel before "
2952 "starting persistent group");
2953 os_sleep(0, 50000);
2954
2955 freq = wpa_s->p2p_persistent_go_freq;
2956 if (neg_freq > 0 && ssid->mode == WPAS_MODE_P2P_GO &&
2957 freq_included(channels, neg_freq)) {
2958 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Use frequence %d MHz from invitation for GO mode",
2959 neg_freq);
2960 freq = neg_freq;
2961 }
2962
2963 wpas_p2p_group_add_persistent(wpa_s, ssid,
2964 ssid->mode == WPAS_MODE_P2P_GO,
2965 freq,
2966 wpa_s->p2p_go_ht40, wpa_s->p2p_go_vht,
2967 channels,
2968 ssid->mode == WPAS_MODE_P2P_GO ?
2969 P2P_MAX_INITIAL_CONN_WAIT_GO_REINVOKE :
2970 0);
2971 }
2972
2973
2974 static int wpas_p2p_disallowed_freq(struct wpa_global *global,
2975 unsigned int freq)
2976 {
2977 return freq_range_list_includes(&global->p2p_disallow_freq, freq);
2978 }
2979
2980
2981 static void wpas_p2p_add_chan(struct p2p_reg_class *reg, u8 chan)
2982 {
2983 reg->channel[reg->channels] = chan;
2984 reg->channels++;
2985 }
2986
2987
2988 static int wpas_p2p_default_channels(struct wpa_supplicant *wpa_s,
2989 struct p2p_channels *chan,
2990 struct p2p_channels *cli_chan)
2991 {
2992 int i, cla = 0;
2993
2994 os_memset(cli_chan, 0, sizeof(*cli_chan));
2995
2996 wpa_printf(MSG_DEBUG, "P2P: Enable operating classes for 2.4 GHz "
2997 "band");
2998
2999 /* Operating class 81 - 2.4 GHz band channels 1..13 */
3000 chan->reg_class[cla].reg_class = 81;
3001 chan->reg_class[cla].channels = 0;
3002 for (i = 0; i < 11; i++) {
3003 if (!wpas_p2p_disallowed_freq(wpa_s->global, 2412 + i * 5))
3004 wpas_p2p_add_chan(&chan->reg_class[cla], i + 1);
3005 }
3006 if (chan->reg_class[cla].channels)
3007 cla++;
3008
3009 wpa_printf(MSG_DEBUG, "P2P: Enable operating classes for lower 5 GHz "
3010 "band");
3011
3012 /* Operating class 115 - 5 GHz, channels 36-48 */
3013 chan->reg_class[cla].reg_class = 115;
3014 chan->reg_class[cla].channels = 0;
3015 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 36 * 5))
3016 wpas_p2p_add_chan(&chan->reg_class[cla], 36);
3017 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 40 * 5))
3018 wpas_p2p_add_chan(&chan->reg_class[cla], 40);
3019 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 44 * 5))
3020 wpas_p2p_add_chan(&chan->reg_class[cla], 44);
3021 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 48 * 5))
3022 wpas_p2p_add_chan(&chan->reg_class[cla], 48);
3023 if (chan->reg_class[cla].channels)
3024 cla++;
3025
3026 wpa_printf(MSG_DEBUG, "P2P: Enable operating classes for higher 5 GHz "
3027 "band");
3028
3029 /* Operating class 124 - 5 GHz, channels 149,153,157,161 */
3030 chan->reg_class[cla].reg_class = 124;
3031 chan->reg_class[cla].channels = 0;
3032 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 149 * 5))
3033 wpas_p2p_add_chan(&chan->reg_class[cla], 149);
3034 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 153 * 5))
3035 wpas_p2p_add_chan(&chan->reg_class[cla], 153);
3036 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 156 * 5))
3037 wpas_p2p_add_chan(&chan->reg_class[cla], 157);
3038 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 161 * 5))
3039 wpas_p2p_add_chan(&chan->reg_class[cla], 161);
3040 if (chan->reg_class[cla].channels)
3041 cla++;
3042
3043 chan->reg_classes = cla;
3044 return 0;
3045 }
3046
3047
3048 static struct hostapd_hw_modes * get_mode(struct hostapd_hw_modes *modes,
3049 u16 num_modes,
3050 enum hostapd_hw_mode mode)
3051 {
3052 u16 i;
3053
3054 for (i = 0; i < num_modes; i++) {
3055 if (modes[i].mode == mode)
3056 return &modes[i];
3057 }
3058
3059 return NULL;
3060 }
3061
3062
3063 enum chan_allowed {
3064 NOT_ALLOWED, PASSIVE_ONLY, ALLOWED
3065 };
3066
3067 static int has_channel(struct wpa_global *global,
3068 struct hostapd_hw_modes *mode, u8 chan, int *flags)
3069 {
3070 int i;
3071 unsigned int freq;
3072
3073 freq = (mode->mode == HOSTAPD_MODE_IEEE80211A ? 5000 : 2407) +
3074 chan * 5;
3075 if (wpas_p2p_disallowed_freq(global, freq))
3076 return NOT_ALLOWED;
3077
3078 for (i = 0; i < mode->num_channels; i++) {
3079 if (mode->channels[i].chan == chan) {
3080 if (flags)
3081 *flags = mode->channels[i].flag;
3082 if (mode->channels[i].flag &
3083 (HOSTAPD_CHAN_DISABLED |
3084 HOSTAPD_CHAN_RADAR))
3085 return NOT_ALLOWED;
3086 if (mode->channels[i].flag &
3087 (HOSTAPD_CHAN_PASSIVE_SCAN |
3088 HOSTAPD_CHAN_NO_IBSS))
3089 return PASSIVE_ONLY;
3090 return ALLOWED;
3091 }
3092 }
3093
3094 return NOT_ALLOWED;
3095 }
3096
3097
3098 struct p2p_oper_class_map {
3099 enum hostapd_hw_mode mode;
3100 u8 op_class;
3101 u8 min_chan;
3102 u8 max_chan;
3103 u8 inc;
3104 enum { BW20, BW40PLUS, BW40MINUS, BW80 } bw;
3105 };
3106
3107 static struct p2p_oper_class_map op_class[] = {
3108 { HOSTAPD_MODE_IEEE80211G, 81, 1, 13, 1, BW20 },
3109 #if 0 /* Do not enable HT40 on 2 GHz for now */
3110 { HOSTAPD_MODE_IEEE80211G, 83, 1, 9, 1, BW40PLUS },
3111 { HOSTAPD_MODE_IEEE80211G, 84, 5, 13, 1, BW40MINUS },
3112 #endif
3113 { HOSTAPD_MODE_IEEE80211A, 115, 36, 48, 4, BW20 },
3114 { HOSTAPD_MODE_IEEE80211A, 124, 149, 161, 4, BW20 },
3115 { HOSTAPD_MODE_IEEE80211A, 116, 36, 44, 8, BW40PLUS },
3116 { HOSTAPD_MODE_IEEE80211A, 117, 40, 48, 8, BW40MINUS },
3117 { HOSTAPD_MODE_IEEE80211A, 126, 149, 157, 8, BW40PLUS },
3118 { HOSTAPD_MODE_IEEE80211A, 127, 153, 161, 8, BW40MINUS },
3119
3120 /*
3121 * IEEE P802.11ac/D7.0 Table E-4 actually talks about channel center
3122 * frequency index 42, 58, 106, 122, 138, 155 with channel spacing of
3123 * 80 MHz, but currently use the following definition for simplicity
3124 * (these center frequencies are not actual channels, which makes
3125 * has_channel() fail). wpas_p2p_verify_80mhz() should take care of
3126 * removing invalid channels.
3127 */
3128 { HOSTAPD_MODE_IEEE80211A, 128, 36, 161, 4, BW80 },
3129 { -1, 0, 0, 0, 0, BW20 }
3130 };
3131
3132
3133 static int wpas_p2p_get_center_80mhz(struct wpa_supplicant *wpa_s,
3134 struct hostapd_hw_modes *mode,
3135 u8 channel)
3136 {
3137 u8 center_channels[] = { 42, 58, 106, 122, 138, 155 };
3138 unsigned int i;
3139
3140 if (mode->mode != HOSTAPD_MODE_IEEE80211A)
3141 return 0;
3142
3143 for (i = 0; i < ARRAY_SIZE(center_channels); i++)
3144 /*
3145 * In 80 MHz, the bandwidth "spans" 12 channels (e.g., 36-48),
3146 * so the center channel is 6 channels away from the start/end.
3147 */
3148 if (channel >= center_channels[i] - 6 &&
3149 channel <= center_channels[i] + 6)
3150 return center_channels[i];
3151
3152 return 0;
3153 }
3154
3155
3156 static enum chan_allowed wpas_p2p_verify_80mhz(struct wpa_supplicant *wpa_s,
3157 struct hostapd_hw_modes *mode,
3158 u8 channel, u8 bw)
3159 {
3160 u8 center_chan;
3161 int i, flags;
3162 enum chan_allowed res, ret = ALLOWED;
3163
3164 center_chan = wpas_p2p_get_center_80mhz(wpa_s, mode, channel);
3165 if (!center_chan)
3166 return NOT_ALLOWED;
3167 if (center_chan >= 58 && center_chan <= 138)
3168 return NOT_ALLOWED; /* Do not allow DFS channels for P2P */
3169
3170 /* check all the channels are available */
3171 for (i = 0; i < 4; i++) {
3172 int adj_chan = center_chan - 6 + i * 4;
3173
3174 res = has_channel(wpa_s->global, mode, adj_chan, &flags);
3175 if (res == NOT_ALLOWED)
3176 return NOT_ALLOWED;
3177 if (res == PASSIVE_ONLY)
3178 ret = PASSIVE_ONLY;
3179
3180 if (i == 0 && !(flags & HOSTAPD_CHAN_VHT_10_70))
3181 return NOT_ALLOWED;
3182 if (i == 1 && !(flags & HOSTAPD_CHAN_VHT_30_50))
3183 return NOT_ALLOWED;
3184 if (i == 2 && !(flags & HOSTAPD_CHAN_VHT_50_30))
3185 return NOT_ALLOWED;
3186 if (i == 3 && !(flags & HOSTAPD_CHAN_VHT_70_10))
3187 return NOT_ALLOWED;
3188 }
3189
3190 return ret;
3191 }
3192
3193
3194 static enum chan_allowed wpas_p2p_verify_channel(struct wpa_supplicant *wpa_s,
3195 struct hostapd_hw_modes *mode,
3196 u8 channel, u8 bw)
3197 {
3198 int flag;
3199 enum chan_allowed res, res2;
3200
3201 res2 = res = has_channel(wpa_s->global, mode, channel, &flag);
3202 if (bw == BW40MINUS) {
3203 if (!(flag & HOSTAPD_CHAN_HT40MINUS))
3204 return NOT_ALLOWED;
3205 res2 = has_channel(wpa_s->global, mode, channel - 4, NULL);
3206 } else if (bw == BW40PLUS) {
3207 if (!(flag & HOSTAPD_CHAN_HT40PLUS))
3208 return NOT_ALLOWED;
3209 res2 = has_channel(wpa_s->global, mode, channel + 4, NULL);
3210 } else if (bw == BW80) {
3211 res2 = wpas_p2p_verify_80mhz(wpa_s, mode, channel, bw);
3212 }
3213
3214 if (res == NOT_ALLOWED || res2 == NOT_ALLOWED)
3215 return NOT_ALLOWED;
3216 if (res == PASSIVE_ONLY || res2 == PASSIVE_ONLY)
3217 return PASSIVE_ONLY;
3218 return res;
3219 }
3220
3221
3222 static int wpas_p2p_setup_channels(struct wpa_supplicant *wpa_s,
3223 struct p2p_channels *chan,
3224 struct p2p_channels *cli_chan)
3225 {
3226 struct hostapd_hw_modes *mode;
3227 int cla, op, cli_cla;
3228
3229 if (wpa_s->hw.modes == NULL) {
3230 wpa_printf(MSG_DEBUG, "P2P: Driver did not support fetching "
3231 "of all supported channels; assume dualband "
3232 "support");
3233 return wpas_p2p_default_channels(wpa_s, chan, cli_chan);
3234 }
3235
3236 cla = cli_cla = 0;
3237
3238 for (op = 0; op_class[op].op_class; op++) {
3239 struct p2p_oper_class_map *o = &op_class[op];
3240 u8 ch;
3241 struct p2p_reg_class *reg = NULL, *cli_reg = NULL;
3242
3243 mode = get_mode(wpa_s->hw.modes, wpa_s->hw.num_modes, o->mode);
3244 if (mode == NULL)
3245 continue;
3246 for (ch = o->min_chan; ch <= o->max_chan; ch += o->inc) {
3247 enum chan_allowed res;
3248 res = wpas_p2p_verify_channel(wpa_s, mode, ch, o->bw);
3249 if (res == ALLOWED) {
3250 if (reg == NULL) {
3251 wpa_printf(MSG_DEBUG, "P2P: Add operating class %u",
3252 o->op_class);
3253 reg = &chan->reg_class[cla];
3254 cla++;
3255 reg->reg_class = o->op_class;
3256 }
3257 reg->channel[reg->channels] = ch;
3258 reg->channels++;
3259 } else if (res == PASSIVE_ONLY &&
3260 wpa_s->conf->p2p_add_cli_chan) {
3261 if (cli_reg == NULL) {
3262 wpa_printf(MSG_DEBUG, "P2P: Add operating class %u (client only)",
3263 o->op_class);
3264 cli_reg = &cli_chan->reg_class[cli_cla];
3265 cli_cla++;
3266 cli_reg->reg_class = o->op_class;
3267 }
3268 cli_reg->channel[cli_reg->channels] = ch;
3269 cli_reg->channels++;
3270 }
3271 }
3272 if (reg) {
3273 wpa_hexdump(MSG_DEBUG, "P2P: Channels",
3274 reg->channel, reg->channels);
3275 }
3276 if (cli_reg) {
3277 wpa_hexdump(MSG_DEBUG, "P2P: Channels (client only)",
3278 cli_reg->channel, cli_reg->channels);
3279 }
3280 }
3281
3282 chan->reg_classes = cla;
3283 cli_chan->reg_classes = cli_cla;
3284
3285 return 0;
3286 }
3287
3288
3289 int wpas_p2p_get_ht40_mode(struct wpa_supplicant *wpa_s,
3290 struct hostapd_hw_modes *mode, u8 channel)
3291 {
3292 int op;
3293 enum chan_allowed ret;
3294
3295 for (op = 0; op_class[op].op_class; op++) {
3296 struct p2p_oper_class_map *o = &op_class[op];
3297 u8 ch;
3298
3299 for (ch = o->min_chan; ch <= o->max_chan; ch += o->inc) {
3300 if (o->mode != HOSTAPD_MODE_IEEE80211A ||
3301 o->bw == BW20 || ch != channel)
3302 continue;
3303 ret = wpas_p2p_verify_channel(wpa_s, mode, ch, o->bw);
3304 if (ret == ALLOWED)
3305 return (o->bw == BW40MINUS) ? -1 : 1;
3306 }
3307 }
3308 return 0;
3309 }
3310
3311
3312 int wpas_p2p_get_vht80_center(struct wpa_supplicant *wpa_s,
3313 struct hostapd_hw_modes *mode, u8 channel)
3314 {
3315 if (!wpas_p2p_verify_channel(wpa_s, mode, channel, BW80))
3316 return 0;
3317
3318 return wpas_p2p_get_center_80mhz(wpa_s, mode, channel);
3319 }
3320
3321
3322 static int wpas_get_noa(void *ctx, const u8 *interface_addr, u8 *buf,
3323 size_t buf_len)
3324 {
3325 struct wpa_supplicant *wpa_s = ctx;
3326
3327 for (wpa_s = wpa_s->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
3328 if (os_memcmp(wpa_s->own_addr, interface_addr, ETH_ALEN) == 0)
3329 break;
3330 }
3331 if (wpa_s == NULL)
3332 return -1;
3333
3334 return wpa_drv_get_noa(wpa_s, buf, buf_len);
3335 }
3336
3337
3338 static int wpas_go_connected(void *ctx, const u8 *dev_addr)
3339 {
3340 struct wpa_supplicant *wpa_s = ctx;
3341
3342 for (wpa_s = wpa_s->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
3343 struct wpa_ssid *ssid = wpa_s->current_ssid;
3344 if (ssid == NULL)
3345 continue;
3346 if (ssid->mode != WPAS_MODE_INFRA)
3347 continue;
3348 if (wpa_s->wpa_state != WPA_COMPLETED &&
3349 wpa_s->wpa_state != WPA_GROUP_HANDSHAKE)
3350 continue;
3351 if (os_memcmp(wpa_s->go_dev_addr, dev_addr, ETH_ALEN) == 0)
3352 return 1;
3353 }
3354
3355 return 0;
3356 }
3357
3358
3359 static void wpas_p2p_debug_print(void *ctx, int level, const char *msg)
3360 {
3361 struct wpa_supplicant *wpa_s = ctx;
3362 wpa_msg_global(wpa_s, level, "P2P: %s", msg);
3363 }
3364
3365
3366 int wpas_p2p_add_p2pdev_interface(struct wpa_supplicant *wpa_s)
3367 {
3368 struct wpa_interface iface;
3369 struct wpa_supplicant *p2pdev_wpa_s;
3370 char ifname[100];
3371 char force_name[100];
3372 int ret;
3373
3374 os_snprintf(ifname, sizeof(ifname), P2P_MGMT_DEVICE_PREFIX "%s",
3375 wpa_s->ifname);
3376 force_name[0] = '\0';
3377 wpa_s->pending_interface_type = WPA_IF_P2P_DEVICE;
3378 ret = wpa_drv_if_add(wpa_s, WPA_IF_P2P_DEVICE, ifname, NULL, NULL,
3379 force_name, wpa_s->pending_interface_addr, NULL);
3380 if (ret < 0) {
3381 wpa_printf(MSG_DEBUG, "P2P: Failed to create P2P Device interface");
3382 return ret;
3383 }
3384 os_strlcpy(wpa_s->pending_interface_name, ifname,
3385 sizeof(wpa_s->pending_interface_name));
3386
3387 os_memset(&iface, 0, sizeof(iface));
3388 iface.p2p_mgmt = 1;
3389 iface.ifname = wpa_s->pending_interface_name;
3390 iface.driver = wpa_s->driver->name;
3391 iface.driver_param = wpa_s->conf->driver_param;
3392 iface.confname = wpa_s->confname;
3393 p2pdev_wpa_s = wpa_supplicant_add_iface(wpa_s->global, &iface);
3394 if (!p2pdev_wpa_s) {
3395 wpa_printf(MSG_DEBUG, "P2P: Failed to add P2P Device interface");
3396 return -1;
3397 }
3398 p2pdev_wpa_s->parent = wpa_s;
3399
3400 wpa_s->pending_interface_name[0] = '\0';
3401 return 0;
3402 }
3403
3404
3405 static void wpas_presence_resp(void *ctx, const u8 *src, u8 status,
3406 const u8 *noa, size_t noa_len)
3407 {
3408 struct wpa_supplicant *wpa_s, *intf = ctx;
3409 char hex[100];
3410
3411 for (wpa_s = intf->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
3412 if (wpa_s->waiting_presence_resp)
3413 break;
3414 }
3415 if (!wpa_s) {
3416 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: No group interface was waiting for presence response");
3417 return;
3418 }
3419 wpa_s->waiting_presence_resp = 0;
3420
3421 wpa_snprintf_hex(hex, sizeof(hex), noa, noa_len);
3422 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_PRESENCE_RESPONSE "src=" MACSTR
3423 " status=%u noa=%s", MAC2STR(src), status, hex);
3424 }
3425
3426
3427 /**
3428 * wpas_p2p_init - Initialize P2P module for %wpa_supplicant
3429 * @global: Pointer to global data from wpa_supplicant_init()
3430 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
3431 * Returns: 0 on success, -1 on failure
3432 */
3433 int wpas_p2p_init(struct wpa_global *global, struct wpa_supplicant *wpa_s)
3434 {
3435 struct p2p_config p2p;
3436 unsigned int r;
3437 int i;
3438
3439 if (wpa_s->conf->p2p_disabled)
3440 return 0;
3441
3442 if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_CAPABLE))
3443 return 0;
3444
3445 if (global->p2p)
3446 return 0;
3447
3448 os_memset(&p2p, 0, sizeof(p2p));
3449 p2p.cb_ctx = wpa_s;
3450 p2p.debug_print = wpas_p2p_debug_print;
3451 p2p.p2p_scan = wpas_p2p_scan;
3452 p2p.send_action = wpas_send_action;
3453 p2p.send_action_done = wpas_send_action_done;
3454 p2p.go_neg_completed = wpas_go_neg_completed;
3455 p2p.go_neg_req_rx = wpas_go_neg_req_rx;
3456 p2p.dev_found = wpas_dev_found;
3457 p2p.dev_lost = wpas_dev_lost;
3458 p2p.find_stopped = wpas_find_stopped;
3459 p2p.start_listen = wpas_start_listen;
3460 p2p.stop_listen = wpas_stop_listen;
3461 p2p.send_probe_resp = wpas_send_probe_resp;
3462 p2p.sd_request = wpas_sd_request;
3463 p2p.sd_response = wpas_sd_response;
3464 p2p.prov_disc_req = wpas_prov_disc_req;
3465 p2p.prov_disc_resp = wpas_prov_disc_resp;
3466 p2p.prov_disc_fail = wpas_prov_disc_fail;
3467 p2p.invitation_process = wpas_invitation_process;
3468 p2p.invitation_received = wpas_invitation_received;
3469 p2p.invitation_result = wpas_invitation_result;
3470 p2p.get_noa = wpas_get_noa;
3471 p2p.go_connected = wpas_go_connected;
3472 p2p.presence_resp = wpas_presence_resp;
3473
3474 os_memcpy(wpa_s->global->p2p_dev_addr, wpa_s->own_addr, ETH_ALEN);
3475 os_memcpy(p2p.dev_addr, wpa_s->global->p2p_dev_addr, ETH_ALEN);
3476 p2p.dev_name = wpa_s->conf->device_name;
3477 p2p.manufacturer = wpa_s->conf->manufacturer;
3478 p2p.model_name = wpa_s->conf->model_name;
3479 p2p.model_number = wpa_s->conf->model_number;
3480 p2p.serial_number = wpa_s->conf->serial_number;
3481 if (wpa_s->wps) {
3482 os_memcpy(p2p.uuid, wpa_s->wps->uuid, 16);
3483 p2p.config_methods = wpa_s->wps->config_methods;
3484 }
3485
3486 if (wpa_s->conf->p2p_listen_reg_class &&
3487 wpa_s->conf->p2p_listen_channel) {
3488 p2p.reg_class = wpa_s->conf->p2p_listen_reg_class;
3489 p2p.channel = wpa_s->conf->p2p_listen_channel;
3490 } else {
3491 p2p.reg_class = 81;
3492 /*
3493 * Pick one of the social channels randomly as the listen
3494 * channel.
3495 */
3496 os_get_random((u8 *) &r, sizeof(r));
3497 p2p.channel = 1 + (r % 3) * 5;
3498 }
3499 wpa_printf(MSG_DEBUG, "P2P: Own listen channel: %d", p2p.channel);
3500
3501 if (wpa_s->conf->p2p_oper_reg_class &&
3502 wpa_s->conf->p2p_oper_channel) {
3503 p2p.op_reg_class = wpa_s->conf->p2p_oper_reg_class;
3504 p2p.op_channel = wpa_s->conf->p2p_oper_channel;
3505 p2p.cfg_op_channel = 1;
3506 wpa_printf(MSG_DEBUG, "P2P: Configured operating channel: "
3507 "%d:%d", p2p.op_reg_class, p2p.op_channel);
3508
3509 } else {
3510 p2p.op_reg_class = 81;
3511 /*
3512 * Use random operation channel from (1, 6, 11) if no other
3513 * preference is indicated.
3514 */
3515 os_get_random((u8 *) &r, sizeof(r));
3516 p2p.op_channel = 1 + (r % 3) * 5;
3517 p2p.cfg_op_channel = 0;
3518 wpa_printf(MSG_DEBUG, "P2P: Random operating channel: "
3519 "%d:%d", p2p.op_reg_class, p2p.op_channel);
3520 }
3521
3522 if (wpa_s->conf->p2p_pref_chan && wpa_s->conf->num_p2p_pref_chan) {
3523 p2p.pref_chan = wpa_s->conf->p2p_pref_chan;
3524 p2p.num_pref_chan = wpa_s->conf->num_p2p_pref_chan;
3525 }
3526
3527 if (wpa_s->conf->country[0] && wpa_s->conf->country[1]) {
3528 os_memcpy(p2p.country, wpa_s->conf->country, 2);
3529 p2p.country[2] = 0x04;
3530 } else
3531 os_memcpy(p2p.country, "XX\x04", 3);
3532
3533 if (wpas_p2p_setup_channels(wpa_s, &p2p.channels, &p2p.cli_channels)) {
3534 wpa_printf(MSG_ERROR, "P2P: Failed to configure supported "
3535 "channel list");
3536 return -1;
3537 }
3538
3539 os_memcpy(p2p.pri_dev_type, wpa_s->conf->device_type,
3540 WPS_DEV_TYPE_LEN);
3541
3542 p2p.num_sec_dev_types = wpa_s->conf->num_sec_device_types;
3543 os_memcpy(p2p.sec_dev_type, wpa_s->conf->sec_device_type,
3544 p2p.num_sec_dev_types * WPS_DEV_TYPE_LEN);
3545
3546 p2p.concurrent_operations = !!(wpa_s->drv_flags &
3547 WPA_DRIVER_FLAGS_P2P_CONCURRENT);
3548
3549 p2p.max_peers = 100;
3550
3551 if (wpa_s->conf->p2p_ssid_postfix) {
3552 p2p.ssid_postfix_len =
3553 os_strlen(wpa_s->conf->p2p_ssid_postfix);
3554 if (p2p.ssid_postfix_len > sizeof(p2p.ssid_postfix))
3555 p2p.ssid_postfix_len = sizeof(p2p.ssid_postfix);
3556 os_memcpy(p2p.ssid_postfix, wpa_s->conf->p2p_ssid_postfix,
3557 p2p.ssid_postfix_len);
3558 }
3559
3560 p2p.p2p_intra_bss = wpa_s->conf->p2p_intra_bss;
3561
3562 p2p.max_listen = wpa_s->max_remain_on_chan;
3563
3564 global->p2p = p2p_init(&p2p);
3565 if (global->p2p == NULL)
3566 return -1;
3567 global->p2p_init_wpa_s = wpa_s;
3568
3569 for (i = 0; i < MAX_WPS_VENDOR_EXT; i++) {
3570 if (wpa_s->conf->wps_vendor_ext[i] == NULL)
3571 continue;
3572 p2p_add_wps_vendor_extension(
3573 global->p2p, wpa_s->conf->wps_vendor_ext[i]);
3574 }
3575
3576 p2p_set_no_go_freq(global->p2p, &wpa_s->conf->p2p_no_go_freq);
3577
3578 return 0;
3579 }
3580
3581
3582 /**
3583 * wpas_p2p_deinit - Deinitialize per-interface P2P data
3584 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
3585 *
3586 * This function deinitialize per-interface P2P data.
3587 */
3588 void wpas_p2p_deinit(struct wpa_supplicant *wpa_s)
3589 {
3590 if (wpa_s->driver && wpa_s->drv_priv)
3591 wpa_drv_probe_req_report(wpa_s, 0);
3592
3593 if (wpa_s->go_params) {
3594 /* Clear any stored provisioning info */
3595 p2p_clear_provisioning_info(
3596 wpa_s->global->p2p,
3597 wpa_s->go_params->peer_device_addr);
3598 }
3599
3600 os_free(wpa_s->go_params);
3601 wpa_s->go_params = NULL;
3602 eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s, NULL);
3603 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
3604 wpa_s->p2p_long_listen = 0;
3605 eloop_cancel_timeout(wpas_p2p_long_listen_timeout, wpa_s, NULL);
3606 eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL);
3607 wpas_p2p_remove_pending_group_interface(wpa_s);
3608 eloop_cancel_timeout(wpas_p2p_group_freq_conflict, wpa_s, NULL);
3609
3610 /* TODO: remove group interface from the driver if this wpa_s instance
3611 * is on top of a P2P group interface */
3612 }
3613
3614
3615 /**
3616 * wpas_p2p_deinit_global - Deinitialize global P2P module
3617 * @global: Pointer to global data from wpa_supplicant_init()
3618 *
3619 * This function deinitializes the global (per device) P2P module.
3620 */
3621 void wpas_p2p_deinit_global(struct wpa_global *global)
3622 {
3623 struct wpa_supplicant *wpa_s, *tmp;
3624
3625 wpa_s = global->ifaces;
3626 if (wpa_s)
3627 wpas_p2p_service_flush(wpa_s);
3628
3629 if (global->p2p == NULL)
3630 return;
3631
3632 /* Remove remaining P2P group interfaces */
3633 while (wpa_s && wpa_s->p2p_group_interface != NOT_P2P_GROUP_INTERFACE)
3634 wpa_s = wpa_s->next;
3635 while (wpa_s) {
3636 tmp = global->ifaces;
3637 while (tmp &&
3638 (tmp == wpa_s ||
3639 tmp->p2p_group_interface == NOT_P2P_GROUP_INTERFACE)) {
3640 tmp = tmp->next;
3641 }
3642 if (tmp == NULL)
3643 break;
3644 /* Disconnect from the P2P group and deinit the interface */
3645 wpas_p2p_disconnect(tmp);
3646 }
3647
3648 /*
3649 * Deinit GO data on any possibly remaining interface (if main
3650 * interface is used as GO).
3651 */
3652 for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
3653 if (wpa_s->ap_iface)
3654 wpas_p2p_group_deinit(wpa_s);
3655 }
3656
3657 p2p_deinit(global->p2p);
3658 global->p2p = NULL;
3659 global->p2p_init_wpa_s = NULL;
3660 }
3661
3662
3663 static int wpas_p2p_create_iface(struct wpa_supplicant *wpa_s)
3664 {
3665 if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_DEDICATED_P2P_DEVICE) &&
3666 wpa_s->conf->p2p_no_group_iface)
3667 return 0; /* separate interface disabled per configuration */
3668 if (wpa_s->drv_flags &
3669 (WPA_DRIVER_FLAGS_P2P_DEDICATED_INTERFACE |
3670 WPA_DRIVER_FLAGS_P2P_MGMT_AND_NON_P2P))
3671 return 1; /* P2P group requires a new interface in every case
3672 */
3673 if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_CONCURRENT))
3674 return 0; /* driver does not support concurrent operations */
3675 if (wpa_s->global->ifaces->next)
3676 return 1; /* more that one interface already in use */
3677 if (wpa_s->wpa_state >= WPA_AUTHENTICATING)
3678 return 1; /* this interface is already in use */
3679 return 0;
3680 }
3681
3682
3683 static int wpas_p2p_start_go_neg(struct wpa_supplicant *wpa_s,
3684 const u8 *peer_addr,
3685 enum p2p_wps_method wps_method,
3686 int go_intent, const u8 *own_interface_addr,
3687 unsigned int force_freq, int persistent_group,
3688 struct wpa_ssid *ssid, unsigned int pref_freq)
3689 {
3690 if (persistent_group && wpa_s->conf->persistent_reconnect)
3691 persistent_group = 2;
3692
3693 /*
3694 * Increase GO config timeout if HT40 is used since it takes some time
3695 * to scan channels for coex purposes before the BSS can be started.
3696 */
3697 p2p_set_config_timeout(wpa_s->global->p2p,
3698 wpa_s->p2p_go_ht40 ? 255 : 100, 20);
3699
3700 return p2p_connect(wpa_s->global->p2p, peer_addr, wps_method,
3701 go_intent, own_interface_addr, force_freq,
3702 persistent_group, ssid ? ssid->ssid : NULL,
3703 ssid ? ssid->ssid_len : 0,
3704 wpa_s->p2p_pd_before_go_neg, pref_freq);
3705 }
3706
3707
3708 static int wpas_p2p_auth_go_neg(struct wpa_supplicant *wpa_s,
3709 const u8 *peer_addr,
3710 enum p2p_wps_method wps_method,
3711 int go_intent, const u8 *own_interface_addr,
3712 unsigned int force_freq, int persistent_group,
3713 struct wpa_ssid *ssid, unsigned int pref_freq)
3714 {
3715 if (persistent_group && wpa_s->conf->persistent_reconnect)
3716 persistent_group = 2;
3717
3718 return p2p_authorize(wpa_s->global->p2p, peer_addr, wps_method,
3719 go_intent, own_interface_addr, force_freq,
3720 persistent_group, ssid ? ssid->ssid : NULL,
3721 ssid ? ssid->ssid_len : 0, pref_freq);
3722 }
3723
3724
3725 static void wpas_p2p_check_join_scan_limit(struct wpa_supplicant *wpa_s)
3726 {
3727 wpa_s->p2p_join_scan_count++;
3728 wpa_printf(MSG_DEBUG, "P2P: Join scan attempt %d",
3729 wpa_s->p2p_join_scan_count);
3730 if (wpa_s->p2p_join_scan_count > P2P_MAX_JOIN_SCAN_ATTEMPTS) {
3731 wpa_printf(MSG_DEBUG, "P2P: Failed to find GO " MACSTR
3732 " for join operationg - stop join attempt",
3733 MAC2STR(wpa_s->pending_join_iface_addr));
3734 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
3735 if (wpa_s->p2p_auto_pd) {
3736 wpa_s->p2p_auto_pd = 0;
3737 wpa_msg_global(wpa_s, MSG_INFO,
3738 P2P_EVENT_PROV_DISC_FAILURE
3739 " p2p_dev_addr=" MACSTR " status=N/A",
3740 MAC2STR(wpa_s->pending_join_dev_addr));
3741 return;
3742 }
3743 wpa_msg_global(wpa_s->parent, MSG_INFO,
3744 P2P_EVENT_GROUP_FORMATION_FAILURE);
3745 }
3746 }
3747
3748
3749 static int wpas_check_freq_conflict(struct wpa_supplicant *wpa_s, int freq)
3750 {
3751 int *freqs, res, num, i;
3752
3753 if (wpas_p2p_num_unused_channels(wpa_s) > 0) {
3754 /* Multiple channels are supported and not all are in use */
3755 return 0;
3756 }
3757
3758 freqs = os_calloc(wpa_s->num_multichan_concurrent, sizeof(int));
3759 if (!freqs)
3760 return 1;
3761
3762 num = wpas_p2p_valid_oper_freqs(wpa_s, freqs,
3763 wpa_s->num_multichan_concurrent);
3764 if (num < 0) {
3765 res = 1;
3766 goto exit_free;
3767 }
3768
3769 for (i = 0; i < num; i++) {
3770 if (freqs[i] == freq) {
3771 wpa_printf(MSG_DEBUG, "P2P: Frequency %d MHz in use by another virtual interface and can be used",
3772 freq);
3773 res = 0;
3774 goto exit_free;
3775 }
3776 }
3777
3778 res = 1;
3779
3780 exit_free:
3781 os_free(freqs);
3782 return res;
3783 }
3784
3785
3786 static int wpas_p2p_peer_go(struct wpa_supplicant *wpa_s,
3787 const u8 *peer_dev_addr)
3788 {
3789 struct wpa_bss *bss;
3790 int updated;
3791
3792 bss = wpa_bss_get_p2p_dev_addr(wpa_s, peer_dev_addr);
3793 if (bss == NULL)
3794 return -1;
3795 if (bss->last_update_idx < wpa_s->bss_update_idx) {
3796 wpa_printf(MSG_DEBUG, "P2P: Peer BSS entry not updated in the "
3797 "last scan");
3798 return 0;
3799 }
3800
3801 updated = os_reltime_before(&wpa_s->p2p_auto_started,
3802 &bss->last_update);
3803 wpa_printf(MSG_DEBUG, "P2P: Current BSS entry for peer updated at "
3804 "%ld.%06ld (%supdated in last scan)",
3805 bss->last_update.sec, bss->last_update.usec,
3806 updated ? "": "not ");
3807
3808 return updated;
3809 }
3810
3811
3812 static void wpas_p2p_scan_res_join(struct wpa_supplicant *wpa_s,
3813 struct wpa_scan_results *scan_res)
3814 {
3815 struct wpa_bss *bss;
3816 int freq;
3817 u8 iface_addr[ETH_ALEN];
3818
3819 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
3820
3821 if (wpa_s->global->p2p_disabled)
3822 return;
3823
3824 wpa_printf(MSG_DEBUG, "P2P: Scan results received (%d BSS) for %sjoin",
3825 scan_res ? (int) scan_res->num : -1,
3826 wpa_s->p2p_auto_join ? "auto_" : "");
3827
3828 if (scan_res)
3829 wpas_p2p_scan_res_handler(wpa_s, scan_res);
3830
3831 if (wpa_s->p2p_auto_pd) {
3832 int join = wpas_p2p_peer_go(wpa_s,
3833 wpa_s->pending_join_dev_addr);
3834 if (join == 0 &&
3835 wpa_s->auto_pd_scan_retry < P2P_AUTO_PD_SCAN_ATTEMPTS) {
3836 wpa_s->auto_pd_scan_retry++;
3837 bss = wpa_bss_get_bssid_latest(
3838 wpa_s, wpa_s->pending_join_dev_addr);
3839 if (bss) {
3840 freq = bss->freq;
3841 wpa_printf(MSG_DEBUG, "P2P: Scan retry %d for "
3842 "the peer " MACSTR " at %d MHz",
3843 wpa_s->auto_pd_scan_retry,
3844 MAC2STR(wpa_s->
3845 pending_join_dev_addr),
3846 freq);
3847 wpas_p2p_join_scan_req(wpa_s, freq);
3848 return;
3849 }
3850 }
3851
3852 if (join < 0)
3853 join = 0;
3854
3855 wpa_s->p2p_auto_pd = 0;
3856 wpa_s->pending_pd_use = join ? AUTO_PD_JOIN : AUTO_PD_GO_NEG;
3857 wpa_printf(MSG_DEBUG, "P2P: Auto PD with " MACSTR " join=%d",
3858 MAC2STR(wpa_s->pending_join_dev_addr), join);
3859 if (p2p_prov_disc_req(wpa_s->global->p2p,
3860 wpa_s->pending_join_dev_addr,
3861 wpa_s->pending_pd_config_methods, join,
3862 0, wpa_s->user_initiated_pd) < 0) {
3863 wpa_s->p2p_auto_pd = 0;
3864 wpa_msg_global(wpa_s, MSG_INFO,
3865 P2P_EVENT_PROV_DISC_FAILURE
3866 " p2p_dev_addr=" MACSTR " status=N/A",
3867 MAC2STR(wpa_s->pending_join_dev_addr));
3868 }
3869 return;
3870 }
3871
3872 if (wpa_s->p2p_auto_join) {
3873 int join = wpas_p2p_peer_go(wpa_s,
3874 wpa_s->pending_join_dev_addr);
3875 if (join < 0) {
3876 wpa_printf(MSG_DEBUG, "P2P: Peer was not found to be "
3877 "running a GO -> use GO Negotiation");
3878 wpas_p2p_connect(wpa_s, wpa_s->pending_join_dev_addr,
3879 wpa_s->p2p_pin, wpa_s->p2p_wps_method,
3880 wpa_s->p2p_persistent_group, 0, 0, 0,
3881 wpa_s->p2p_go_intent,
3882 wpa_s->p2p_connect_freq,
3883 wpa_s->p2p_persistent_id,
3884 wpa_s->p2p_pd_before_go_neg,
3885 wpa_s->p2p_go_ht40,
3886 wpa_s->p2p_go_vht);
3887 return;
3888 }
3889
3890 wpa_printf(MSG_DEBUG, "P2P: Peer was found running GO%s -> "
3891 "try to join the group", join ? "" :
3892 " in older scan");
3893 if (!join)
3894 wpa_s->p2p_fallback_to_go_neg = 1;
3895 }
3896
3897 freq = p2p_get_oper_freq(wpa_s->global->p2p,
3898 wpa_s->pending_join_iface_addr);
3899 if (freq < 0 &&
3900 p2p_get_interface_addr(wpa_s->global->p2p,
3901 wpa_s->pending_join_dev_addr,
3902 iface_addr) == 0 &&
3903 os_memcmp(iface_addr, wpa_s->pending_join_dev_addr, ETH_ALEN) != 0)
3904 {
3905 wpa_printf(MSG_DEBUG, "P2P: Overwrite pending interface "
3906 "address for join from " MACSTR " to " MACSTR
3907 " based on newly discovered P2P peer entry",
3908 MAC2STR(wpa_s->pending_join_iface_addr),
3909 MAC2STR(iface_addr));
3910 os_memcpy(wpa_s->pending_join_iface_addr, iface_addr,
3911 ETH_ALEN);
3912
3913 freq = p2p_get_oper_freq(wpa_s->global->p2p,
3914 wpa_s->pending_join_iface_addr);
3915 }
3916 if (freq >= 0) {
3917 wpa_printf(MSG_DEBUG, "P2P: Target GO operating frequency "
3918 "from P2P peer table: %d MHz", freq);
3919 }
3920 bss = wpa_bss_get_bssid_latest(wpa_s, wpa_s->pending_join_iface_addr);
3921 if (bss) {
3922 freq = bss->freq;
3923 wpa_printf(MSG_DEBUG, "P2P: Target GO operating frequency "
3924 "from BSS table: %d MHz (SSID %s)", freq,
3925 wpa_ssid_txt(bss->ssid, bss->ssid_len));
3926 }
3927 if (freq > 0) {
3928 u16 method;
3929
3930 if (wpas_check_freq_conflict(wpa_s, freq) > 0) {
3931 wpa_msg_global(wpa_s->parent, MSG_INFO,
3932 P2P_EVENT_GROUP_FORMATION_FAILURE
3933 "reason=FREQ_CONFLICT");
3934 return;
3935 }
3936
3937 wpa_printf(MSG_DEBUG, "P2P: Send Provision Discovery Request "
3938 "prior to joining an existing group (GO " MACSTR
3939 " freq=%u MHz)",
3940 MAC2STR(wpa_s->pending_join_dev_addr), freq);
3941 wpa_s->pending_pd_before_join = 1;
3942
3943 switch (wpa_s->pending_join_wps_method) {
3944 case WPS_PIN_DISPLAY:
3945 method = WPS_CONFIG_KEYPAD;
3946 break;
3947 case WPS_PIN_KEYPAD:
3948 method = WPS_CONFIG_DISPLAY;
3949 break;
3950 case WPS_PBC:
3951 method = WPS_CONFIG_PUSHBUTTON;
3952 break;
3953 default:
3954 method = 0;
3955 break;
3956 }
3957
3958 if ((p2p_get_provisioning_info(wpa_s->global->p2p,
3959 wpa_s->pending_join_dev_addr) ==
3960 method)) {
3961 /*
3962 * We have already performed provision discovery for
3963 * joining the group. Proceed directly to join
3964 * operation without duplicated provision discovery. */
3965 wpa_printf(MSG_DEBUG, "P2P: Provision discovery "
3966 "with " MACSTR " already done - proceed to "
3967 "join",
3968 MAC2STR(wpa_s->pending_join_dev_addr));
3969 wpa_s->pending_pd_before_join = 0;
3970 goto start;
3971 }
3972
3973 if (p2p_prov_disc_req(wpa_s->global->p2p,
3974 wpa_s->pending_join_dev_addr, method, 1,
3975 freq, wpa_s->user_initiated_pd) < 0) {
3976 wpa_printf(MSG_DEBUG, "P2P: Failed to send Provision "
3977 "Discovery Request before joining an "
3978 "existing group");
3979 wpa_s->pending_pd_before_join = 0;
3980 goto start;
3981 }
3982 return;
3983 }
3984
3985 wpa_printf(MSG_DEBUG, "P2P: Failed to find BSS/GO - try again later");
3986 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
3987 eloop_register_timeout(1, 0, wpas_p2p_join_scan, wpa_s, NULL);
3988 wpas_p2p_check_join_scan_limit(wpa_s);
3989 return;
3990
3991 start:
3992 /* Start join operation immediately */
3993 wpas_p2p_join_start(wpa_s);
3994 }
3995
3996
3997 static void wpas_p2p_join_scan_req(struct wpa_supplicant *wpa_s, int freq)
3998 {
3999 int ret;
4000 struct wpa_driver_scan_params params;
4001 struct wpabuf *wps_ie, *ies;
4002 size_t ielen;
4003 int freqs[2] = { 0, 0 };
4004
4005 os_memset(&params, 0, sizeof(params));
4006
4007 /* P2P Wildcard SSID */
4008 params.num_ssids = 1;
4009 params.ssids[0].ssid = (u8 *) P2P_WILDCARD_SSID;
4010 params.ssids[0].ssid_len = P2P_WILDCARD_SSID_LEN;
4011
4012 wpa_s->wps->dev.p2p = 1;
4013 wps_ie = wps_build_probe_req_ie(DEV_PW_DEFAULT, &wpa_s->wps->dev,
4014 wpa_s->wps->uuid, WPS_REQ_ENROLLEE, 0,
4015 NULL);
4016 if (wps_ie == NULL) {
4017 wpas_p2p_scan_res_join(wpa_s, NULL);
4018 return;
4019 }
4020
4021 ielen = p2p_scan_ie_buf_len(wpa_s->global->p2p);
4022 ies = wpabuf_alloc(wpabuf_len(wps_ie) + ielen);
4023 if (ies == NULL) {
4024 wpabuf_free(wps_ie);
4025 wpas_p2p_scan_res_join(wpa_s, NULL);
4026 return;
4027 }
4028 wpabuf_put_buf(ies, wps_ie);
4029 wpabuf_free(wps_ie);
4030
4031 p2p_scan_ie(wpa_s->global->p2p, ies, NULL);
4032
4033 params.p2p_probe = 1;
4034 params.extra_ies = wpabuf_head(ies);
4035 params.extra_ies_len = wpabuf_len(ies);
4036 if (freq > 0) {
4037 freqs[0] = freq;
4038 params.freqs = freqs;
4039 }
4040
4041 /*
4042 * Run a scan to update BSS table and start Provision Discovery once
4043 * the new scan results become available.
4044 */
4045 ret = wpa_drv_scan(wpa_s, &params);
4046 if (!ret) {
4047 os_get_reltime(&wpa_s->scan_trigger_time);
4048 wpa_s->scan_res_handler = wpas_p2p_scan_res_join;
4049 wpa_s->own_scan_requested = 1;
4050 }
4051
4052 wpabuf_free(ies);
4053
4054 if (ret) {
4055 wpa_printf(MSG_DEBUG, "P2P: Failed to start scan for join - "
4056 "try again later");
4057 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
4058 eloop_register_timeout(1, 0, wpas_p2p_join_scan, wpa_s, NULL);
4059 wpas_p2p_check_join_scan_limit(wpa_s);
4060 }
4061 }
4062
4063
4064 static void wpas_p2p_join_scan(void *eloop_ctx, void *timeout_ctx)
4065 {
4066 struct wpa_supplicant *wpa_s = eloop_ctx;
4067 wpas_p2p_join_scan_req(wpa_s, 0);
4068 }
4069
4070
4071 static int wpas_p2p_join(struct wpa_supplicant *wpa_s, const u8 *iface_addr,
4072 const u8 *dev_addr, enum p2p_wps_method wps_method,
4073 int auto_join)
4074 {
4075 wpa_printf(MSG_DEBUG, "P2P: Request to join existing group (iface "
4076 MACSTR " dev " MACSTR ")%s",
4077 MAC2STR(iface_addr), MAC2STR(dev_addr),
4078 auto_join ? " (auto_join)" : "");
4079
4080 wpa_s->p2p_auto_pd = 0;
4081 wpa_s->p2p_auto_join = !!auto_join;
4082 os_memcpy(wpa_s->pending_join_iface_addr, iface_addr, ETH_ALEN);
4083 os_memcpy(wpa_s->pending_join_dev_addr, dev_addr, ETH_ALEN);
4084 wpa_s->pending_join_wps_method = wps_method;
4085
4086 /* Make sure we are not running find during connection establishment */
4087 wpas_p2p_stop_find(wpa_s);
4088
4089 wpa_s->p2p_join_scan_count = 0;
4090 wpas_p2p_join_scan(wpa_s, NULL);
4091 return 0;
4092 }
4093
4094
4095 static int wpas_p2p_join_start(struct wpa_supplicant *wpa_s)
4096 {
4097 struct wpa_supplicant *group;
4098 struct p2p_go_neg_results res;
4099 struct wpa_bss *bss;
4100
4101 group = wpas_p2p_get_group_iface(wpa_s, 0, 0);
4102 if (group == NULL)
4103 return -1;
4104 if (group != wpa_s) {
4105 os_memcpy(group->p2p_pin, wpa_s->p2p_pin,
4106 sizeof(group->p2p_pin));
4107 group->p2p_wps_method = wpa_s->p2p_wps_method;
4108 } else {
4109 /*
4110 * Need to mark the current interface for p2p_group_formation
4111 * when a separate group interface is not used. This is needed
4112 * to allow p2p_cancel stop a pending p2p_connect-join.
4113 * wpas_p2p_init_group_interface() addresses this for the case
4114 * where a separate group interface is used.
4115 */
4116 wpa_s->global->p2p_group_formation = wpa_s;
4117 }
4118
4119 group->p2p_in_provisioning = 1;
4120 group->p2p_fallback_to_go_neg = wpa_s->p2p_fallback_to_go_neg;
4121
4122 os_memset(&res, 0, sizeof(res));
4123 os_memcpy(res.peer_interface_addr, wpa_s->pending_join_iface_addr,
4124 ETH_ALEN);
4125 res.wps_method = wpa_s->pending_join_wps_method;
4126 bss = wpa_bss_get_bssid_latest(wpa_s, wpa_s->pending_join_iface_addr);
4127 if (bss) {
4128 res.freq = bss->freq;
4129 res.ssid_len = bss->ssid_len;
4130 os_memcpy(res.ssid, bss->ssid, bss->ssid_len);
4131 wpa_printf(MSG_DEBUG, "P2P: Join target GO operating frequency "
4132 "from BSS table: %d MHz (SSID %s)", bss->freq,
4133 wpa_ssid_txt(bss->ssid, bss->ssid_len));
4134 }
4135
4136 if (wpa_s->off_channel_freq || wpa_s->roc_waiting_drv_freq) {
4137 wpa_printf(MSG_DEBUG, "P2P: Cancel remain-on-channel prior to "
4138 "starting client");
4139 wpa_drv_cancel_remain_on_channel(wpa_s);
4140 wpa_s->off_channel_freq = 0;
4141 wpa_s->roc_waiting_drv_freq = 0;
4142 }
4143 wpas_start_wps_enrollee(group, &res);
4144
4145 /*
4146 * Allow a longer timeout for join-a-running-group than normal 15
4147 * second group formation timeout since the GO may not have authorized
4148 * our connection yet.
4149 */
4150 eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s, NULL);
4151 eloop_register_timeout(60, 0, wpas_p2p_group_formation_timeout,
4152 wpa_s, NULL);
4153
4154 return 0;
4155 }
4156
4157
4158 static int wpas_p2p_setup_freqs(struct wpa_supplicant *wpa_s, int freq,
4159 int *force_freq, int *pref_freq, int go)
4160 {
4161 int *freqs, res;
4162 unsigned int freq_in_use = 0, num, i;
4163
4164 freqs = os_calloc(wpa_s->num_multichan_concurrent, sizeof(int));
4165 if (!freqs)
4166 return -1;
4167
4168 num = get_shared_radio_freqs(wpa_s, freqs,
4169 wpa_s->num_multichan_concurrent);
4170 wpa_printf(MSG_DEBUG,
4171 "P2P: Setup freqs: freq=%d num_MCC=%d shared_freqs=%u",
4172 freq, wpa_s->num_multichan_concurrent, num);
4173
4174 if (freq > 0) {
4175 int ret;
4176 if (go)
4177 ret = p2p_supported_freq(wpa_s->global->p2p, freq);
4178 else
4179 ret = p2p_supported_freq_cli(wpa_s->global->p2p, freq);
4180 if (!ret) {
4181 wpa_printf(MSG_DEBUG, "P2P: The forced channel "
4182 "(%u MHz) is not supported for P2P uses",
4183 freq);
4184 res = -3;
4185 goto exit_free;
4186 }
4187
4188 for (i = 0; i < num; i++) {
4189 if (freqs[i] == freq)
4190 freq_in_use = 1;
4191 }
4192
4193 if (num == wpa_s->num_multichan_concurrent && !freq_in_use) {
4194 wpa_printf(MSG_DEBUG, "P2P: Cannot start P2P group on %u MHz as there are no available channels",
4195 freq);
4196 res = -2;
4197 goto exit_free;
4198 }
4199 wpa_printf(MSG_DEBUG, "P2P: Trying to force us to use the "
4200 "requested channel (%u MHz)", freq);
4201 *force_freq = freq;
4202 goto exit_ok;
4203 }
4204
4205 for (i = 0; i < num; i++) {
4206 if (!p2p_supported_freq(wpa_s->global->p2p, freqs[i]))
4207 continue;
4208
4209 if (*pref_freq == 0 && num < wpa_s->num_multichan_concurrent) {
4210 wpa_printf(MSG_DEBUG, "P2P: Try to prefer a frequency (%u MHz) we are already using",
4211 freqs[i]);
4212 *pref_freq = freqs[i];
4213 } else {
4214 wpa_printf(MSG_DEBUG, "P2P: Try to force us to use frequency (%u MHz) which is already in use",
4215 freqs[i]);
4216 *force_freq = freqs[i];
4217 }
4218 break;
4219 }
4220
4221 if (i == num) {
4222 if (num < wpa_s->num_multichan_concurrent && num > 0) {
4223 wpa_printf(MSG_DEBUG, "P2P: Current operating channels are not available for P2P. Try to use another channel");
4224 *force_freq = 0;
4225 } else if (num < wpa_s->num_multichan_concurrent) {
4226 wpa_printf(MSG_DEBUG, "P2P: No current operating channels - try to use a new channel");
4227 *force_freq = 0;
4228 } else {
4229 wpa_printf(MSG_DEBUG, "P2P: All channels are in use and none of them are P2P enabled. Cannot start P2P group");
4230 res = -2;
4231 goto exit_free;
4232 }
4233 }
4234
4235 exit_ok:
4236 res = 0;
4237 exit_free:
4238 os_free(freqs);
4239 return res;
4240 }
4241
4242
4243 /**
4244 * wpas_p2p_connect - Request P2P Group Formation to be started
4245 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
4246 * @peer_addr: Address of the peer P2P Device
4247 * @pin: PIN to use during provisioning or %NULL to indicate PBC mode
4248 * @persistent_group: Whether to create a persistent group
4249 * @auto_join: Whether to select join vs. GO Negotiation automatically
4250 * @join: Whether to join an existing group (as a client) instead of starting
4251 * Group Owner negotiation; @peer_addr is BSSID in that case
4252 * @auth: Whether to only authorize the connection instead of doing that and
4253 * initiating Group Owner negotiation
4254 * @go_intent: GO Intent or -1 to use default
4255 * @freq: Frequency for the group or 0 for auto-selection
4256 * @persistent_id: Persistent group credentials to use for forcing GO
4257 * parameters or -1 to generate new values (SSID/passphrase)
4258 * @pd: Whether to send Provision Discovery prior to GO Negotiation as an
4259 * interoperability workaround when initiating group formation
4260 * @ht40: Start GO with 40 MHz channel width
4261 * @vht: Start GO with VHT support
4262 * Returns: 0 or new PIN (if pin was %NULL) on success, -1 on unspecified
4263 * failure, -2 on failure due to channel not currently available,
4264 * -3 if forced channel is not supported
4265 */
4266 int wpas_p2p_connect(struct wpa_supplicant *wpa_s, const u8 *peer_addr,
4267 const char *pin, enum p2p_wps_method wps_method,
4268 int persistent_group, int auto_join, int join, int auth,
4269 int go_intent, int freq, int persistent_id, int pd,
4270 int ht40, int vht)
4271 {
4272 int force_freq = 0, pref_freq = 0;
4273 int ret = 0, res;
4274 enum wpa_driver_if_type iftype;
4275 const u8 *if_addr;
4276 struct wpa_ssid *ssid = NULL;
4277
4278 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
4279 return -1;
4280
4281 if (persistent_id >= 0) {
4282 ssid = wpa_config_get_network(wpa_s->conf, persistent_id);
4283 if (ssid == NULL || ssid->disabled != 2 ||
4284 ssid->mode != WPAS_MODE_P2P_GO)
4285 return -1;
4286 }
4287
4288 os_free(wpa_s->global->add_psk);
4289 wpa_s->global->add_psk = NULL;
4290
4291 if (go_intent < 0)
4292 go_intent = wpa_s->conf->p2p_go_intent;
4293
4294 if (!auth)
4295 wpa_s->p2p_long_listen = 0;
4296
4297 wpa_s->p2p_wps_method = wps_method;
4298 wpa_s->p2p_persistent_group = !!persistent_group;
4299 wpa_s->p2p_persistent_id = persistent_id;
4300 wpa_s->p2p_go_intent = go_intent;
4301 wpa_s->p2p_connect_freq = freq;
4302 wpa_s->p2p_fallback_to_go_neg = 0;
4303 wpa_s->p2p_pd_before_go_neg = !!pd;
4304 wpa_s->p2p_go_ht40 = !!ht40;
4305 wpa_s->p2p_go_vht = !!vht;
4306
4307 if (pin)
4308 os_strlcpy(wpa_s->p2p_pin, pin, sizeof(wpa_s->p2p_pin));
4309 else if (wps_method == WPS_PIN_DISPLAY) {
4310 ret = wps_generate_pin();
4311 os_snprintf(wpa_s->p2p_pin, sizeof(wpa_s->p2p_pin), "%08d",
4312 ret);
4313 wpa_printf(MSG_DEBUG, "P2P: Randomly generated PIN: %s",
4314 wpa_s->p2p_pin);
4315 } else
4316 wpa_s->p2p_pin[0] = '\0';
4317
4318 if (join || auto_join) {
4319 u8 iface_addr[ETH_ALEN], dev_addr[ETH_ALEN];
4320 if (auth) {
4321 wpa_printf(MSG_DEBUG, "P2P: Authorize invitation to "
4322 "connect a running group from " MACSTR,
4323 MAC2STR(peer_addr));
4324 os_memcpy(wpa_s->p2p_auth_invite, peer_addr, ETH_ALEN);
4325 return ret;
4326 }
4327 os_memcpy(dev_addr, peer_addr, ETH_ALEN);
4328 if (p2p_get_interface_addr(wpa_s->global->p2p, peer_addr,
4329 iface_addr) < 0) {
4330 os_memcpy(iface_addr, peer_addr, ETH_ALEN);
4331 p2p_get_dev_addr(wpa_s->global->p2p, peer_addr,
4332 dev_addr);
4333 }
4334 if (auto_join) {
4335 os_get_reltime(&wpa_s->p2p_auto_started);
4336 wpa_printf(MSG_DEBUG, "P2P: Auto join started at "
4337 "%ld.%06ld",
4338 wpa_s->p2p_auto_started.sec,
4339 wpa_s->p2p_auto_started.usec);
4340 }
4341 wpa_s->user_initiated_pd = 1;
4342 if (wpas_p2p_join(wpa_s, iface_addr, dev_addr, wps_method,
4343 auto_join) < 0)
4344 return -1;
4345 return ret;
4346 }
4347
4348 res = wpas_p2p_setup_freqs(wpa_s, freq, &force_freq, &pref_freq,
4349 go_intent == 15);
4350 if (res)
4351 return res;
4352 wpas_p2p_set_own_freq_preference(wpa_s,
4353 force_freq ? force_freq : pref_freq);
4354
4355 wpa_s->create_p2p_iface = wpas_p2p_create_iface(wpa_s);
4356
4357 if (wpa_s->create_p2p_iface) {
4358 /* Prepare to add a new interface for the group */
4359 iftype = WPA_IF_P2P_GROUP;
4360 if (go_intent == 15)
4361 iftype = WPA_IF_P2P_GO;
4362 if (wpas_p2p_add_group_interface(wpa_s, iftype) < 0) {
4363 wpa_printf(MSG_ERROR, "P2P: Failed to allocate a new "
4364 "interface for the group");
4365 return -1;
4366 }
4367
4368 if_addr = wpa_s->pending_interface_addr;
4369 } else
4370 if_addr = wpa_s->own_addr;
4371
4372 if (auth) {
4373 if (wpas_p2p_auth_go_neg(wpa_s, peer_addr, wps_method,
4374 go_intent, if_addr,
4375 force_freq, persistent_group, ssid,
4376 pref_freq) < 0)
4377 return -1;
4378 return ret;
4379 }
4380
4381 if (wpas_p2p_start_go_neg(wpa_s, peer_addr, wps_method,
4382 go_intent, if_addr, force_freq,
4383 persistent_group, ssid, pref_freq) < 0) {
4384 if (wpa_s->create_p2p_iface)
4385 wpas_p2p_remove_pending_group_interface(wpa_s);
4386 return -1;
4387 }
4388 return ret;
4389 }
4390
4391
4392 /**
4393 * wpas_p2p_remain_on_channel_cb - Indication of remain-on-channel start
4394 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
4395 * @freq: Frequency of the channel in MHz
4396 * @duration: Duration of the stay on the channel in milliseconds
4397 *
4398 * This callback is called when the driver indicates that it has started the
4399 * requested remain-on-channel duration.
4400 */
4401 void wpas_p2p_remain_on_channel_cb(struct wpa_supplicant *wpa_s,
4402 unsigned int freq, unsigned int duration)
4403 {
4404 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
4405 return;
4406 if (wpa_s->off_channel_freq == wpa_s->pending_listen_freq) {
4407 p2p_listen_cb(wpa_s->global->p2p, wpa_s->pending_listen_freq,
4408 wpa_s->pending_listen_duration);
4409 wpa_s->pending_listen_freq = 0;
4410 }
4411 }
4412
4413
4414 static int wpas_p2p_listen_start(struct wpa_supplicant *wpa_s,
4415 unsigned int timeout)
4416 {
4417 /* Limit maximum Listen state time based on driver limitation. */
4418 if (timeout > wpa_s->max_remain_on_chan)
4419 timeout = wpa_s->max_remain_on_chan;
4420
4421 return p2p_listen(wpa_s->global->p2p, timeout);
4422 }
4423
4424
4425 /**
4426 * wpas_p2p_cancel_remain_on_channel_cb - Remain-on-channel timeout
4427 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
4428 * @freq: Frequency of the channel in MHz
4429 *
4430 * This callback is called when the driver indicates that a remain-on-channel
4431 * operation has been completed, i.e., the duration on the requested channel
4432 * has timed out.
4433 */
4434 void wpas_p2p_cancel_remain_on_channel_cb(struct wpa_supplicant *wpa_s,
4435 unsigned int freq)
4436 {
4437 wpa_printf(MSG_DEBUG, "P2P: Cancel remain-on-channel callback "
4438 "(p2p_long_listen=%d ms pending_action_tx=%p)",
4439 wpa_s->p2p_long_listen, offchannel_pending_action_tx(wpa_s));
4440 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
4441 return;
4442 if (p2p_listen_end(wpa_s->global->p2p, freq) > 0)
4443 return; /* P2P module started a new operation */
4444 if (offchannel_pending_action_tx(wpa_s))
4445 return;
4446 if (wpa_s->p2p_long_listen > 0)
4447 wpa_s->p2p_long_listen -= wpa_s->max_remain_on_chan;
4448 if (wpa_s->p2p_long_listen > 0) {
4449 wpa_printf(MSG_DEBUG, "P2P: Continuing long Listen state");
4450 wpas_p2p_listen_start(wpa_s, wpa_s->p2p_long_listen);
4451 } else {
4452 /*
4453 * When listen duration is over, stop listen & update p2p_state
4454 * to IDLE.
4455 */
4456 p2p_stop_listen(wpa_s->global->p2p);
4457 }
4458 }
4459
4460
4461 /**
4462 * wpas_p2p_group_remove - Remove a P2P group
4463 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
4464 * @ifname: Network interface name of the group interface or "*" to remove all
4465 * groups
4466 * Returns: 0 on success, -1 on failure
4467 *
4468 * This function is used to remove a P2P group. This can be used to disconnect
4469 * from a group in which the local end is a P2P Client or to end a P2P Group in
4470 * case the local end is the Group Owner. If a virtual network interface was
4471 * created for this group, that interface will be removed. Otherwise, only the
4472 * configured P2P group network will be removed from the interface.
4473 */
4474 int wpas_p2p_group_remove(struct wpa_supplicant *wpa_s, const char *ifname)
4475 {
4476 struct wpa_global *global = wpa_s->global;
4477
4478 if (os_strcmp(ifname, "*") == 0) {
4479 struct wpa_supplicant *prev;
4480 wpa_s = global->ifaces;
4481 while (wpa_s) {
4482 prev = wpa_s;
4483 wpa_s = wpa_s->next;
4484 if (prev->p2p_group_interface !=
4485 NOT_P2P_GROUP_INTERFACE ||
4486 (prev->current_ssid &&
4487 prev->current_ssid->p2p_group))
4488 wpas_p2p_disconnect(prev);
4489 }
4490 return 0;
4491 }
4492
4493 for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
4494 if (os_strcmp(wpa_s->ifname, ifname) == 0)
4495 break;
4496 }
4497
4498 return wpas_p2p_disconnect(wpa_s);
4499 }
4500
4501
4502 static int wpas_p2p_select_go_freq(struct wpa_supplicant *wpa_s, int freq)
4503 {
4504 unsigned int r;
4505
4506 if (freq == 2) {
4507 wpa_printf(MSG_DEBUG, "P2P: Request to start GO on 2.4 GHz "
4508 "band");
4509 if (wpa_s->best_24_freq > 0 &&
4510 p2p_supported_freq_go(wpa_s->global->p2p,
4511 wpa_s->best_24_freq)) {
4512 freq = wpa_s->best_24_freq;
4513 wpa_printf(MSG_DEBUG, "P2P: Use best 2.4 GHz band "
4514 "channel: %d MHz", freq);
4515 } else {
4516 os_get_random((u8 *) &r, sizeof(r));
4517 freq = 2412 + (r % 3) * 25;
4518 wpa_printf(MSG_DEBUG, "P2P: Use random 2.4 GHz band "
4519 "channel: %d MHz", freq);
4520 }
4521 }
4522
4523 if (freq == 5) {
4524 wpa_printf(MSG_DEBUG, "P2P: Request to start GO on 5 GHz "
4525 "band");
4526 if (wpa_s->best_5_freq > 0 &&
4527 p2p_supported_freq_go(wpa_s->global->p2p,
4528 wpa_s->best_5_freq)) {
4529 freq = wpa_s->best_5_freq;
4530 wpa_printf(MSG_DEBUG, "P2P: Use best 5 GHz band "
4531 "channel: %d MHz", freq);
4532 } else {
4533 os_get_random((u8 *) &r, sizeof(r));
4534 freq = 5180 + (r % 4) * 20;
4535 if (!p2p_supported_freq_go(wpa_s->global->p2p, freq)) {
4536 wpa_printf(MSG_DEBUG, "P2P: Could not select "
4537 "5 GHz channel for P2P group");
4538 return -1;
4539 }
4540 wpa_printf(MSG_DEBUG, "P2P: Use random 5 GHz band "
4541 "channel: %d MHz", freq);
4542 }
4543 }
4544
4545 if (freq > 0 && !p2p_supported_freq_go(wpa_s->global->p2p, freq)) {
4546 wpa_printf(MSG_DEBUG, "P2P: The forced channel for GO "
4547 "(%u MHz) is not supported for P2P uses",
4548 freq);
4549 return -1;
4550 }
4551
4552 return freq;
4553 }
4554
4555
4556 static int wpas_p2p_init_go_params(struct wpa_supplicant *wpa_s,
4557 struct p2p_go_neg_results *params,
4558 int freq, int ht40, int vht,
4559 const struct p2p_channels *channels)
4560 {
4561 int res, *freqs;
4562 unsigned int pref_freq;
4563 unsigned int num, i;
4564
4565 os_memset(params, 0, sizeof(*params));
4566 params->role_go = 1;
4567 params->ht40 = ht40;
4568 params->vht = vht;
4569 if (freq) {
4570 if (!freq_included(channels, freq)) {
4571 wpa_printf(MSG_DEBUG, "P2P: Forced GO freq %d MHz not "
4572 "accepted", freq);
4573 return -1;
4574 }
4575 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on forced "
4576 "frequency %d MHz", freq);
4577 params->freq = freq;
4578 } else if (wpa_s->conf->p2p_oper_reg_class == 81 &&
4579 wpa_s->conf->p2p_oper_channel >= 1 &&
4580 wpa_s->conf->p2p_oper_channel <= 11 &&
4581 freq_included(channels,
4582 2407 + 5 * wpa_s->conf->p2p_oper_channel)) {
4583 params->freq = 2407 + 5 * wpa_s->conf->p2p_oper_channel;
4584 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on configured "
4585 "frequency %d MHz", params->freq);
4586 } else if ((wpa_s->conf->p2p_oper_reg_class == 115 ||
4587 wpa_s->conf->p2p_oper_reg_class == 116 ||
4588 wpa_s->conf->p2p_oper_reg_class == 117 ||
4589 wpa_s->conf->p2p_oper_reg_class == 124 ||
4590 wpa_s->conf->p2p_oper_reg_class == 126 ||
4591 wpa_s->conf->p2p_oper_reg_class == 127) &&
4592 freq_included(channels,
4593 5000 + 5 * wpa_s->conf->p2p_oper_channel)) {
4594 params->freq = 5000 + 5 * wpa_s->conf->p2p_oper_channel;
4595 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on configured "
4596 "frequency %d MHz", params->freq);
4597 } else if (wpa_s->conf->p2p_oper_channel == 0 &&
4598 wpa_s->best_overall_freq > 0 &&
4599 p2p_supported_freq_go(wpa_s->global->p2p,
4600 wpa_s->best_overall_freq) &&
4601 freq_included(channels, wpa_s->best_overall_freq)) {
4602 params->freq = wpa_s->best_overall_freq;
4603 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on best overall "
4604 "channel %d MHz", params->freq);
4605 } else if (wpa_s->conf->p2p_oper_channel == 0 &&
4606 wpa_s->best_24_freq > 0 &&
4607 p2p_supported_freq_go(wpa_s->global->p2p,
4608 wpa_s->best_24_freq) &&
4609 freq_included(channels, wpa_s->best_24_freq)) {
4610 params->freq = wpa_s->best_24_freq;
4611 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on best 2.4 GHz "
4612 "channel %d MHz", params->freq);
4613 } else if (wpa_s->conf->p2p_oper_channel == 0 &&
4614 wpa_s->best_5_freq > 0 &&
4615 p2p_supported_freq_go(wpa_s->global->p2p,
4616 wpa_s->best_5_freq) &&
4617 freq_included(channels, wpa_s->best_5_freq)) {
4618 params->freq = wpa_s->best_5_freq;
4619 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on best 5 GHz "
4620 "channel %d MHz", params->freq);
4621 } else if ((pref_freq = p2p_get_pref_freq(wpa_s->global->p2p,
4622 channels))) {
4623 params->freq = pref_freq;
4624 wpa_printf(MSG_DEBUG, "P2P: Set GO freq %d MHz from preferred "
4625 "channels", params->freq);
4626 } else {
4627 int chan;
4628 for (chan = 0; chan < 11; chan++) {
4629 params->freq = 2412 + chan * 5;
4630 if (!wpas_p2p_disallowed_freq(wpa_s->global,
4631 params->freq) &&
4632 freq_included(channels, params->freq))
4633 break;
4634 }
4635 if (chan == 11) {
4636 wpa_printf(MSG_DEBUG, "P2P: No 2.4 GHz channel "
4637 "allowed");
4638 return -1;
4639 }
4640 wpa_printf(MSG_DEBUG, "P2P: Set GO freq %d MHz (no preference "
4641 "known)", params->freq);
4642 }
4643
4644 freqs = os_calloc(wpa_s->num_multichan_concurrent, sizeof(int));
4645 if (!freqs)
4646 return -1;
4647
4648 res = wpas_p2p_valid_oper_freqs(wpa_s, freqs,
4649 wpa_s->num_multichan_concurrent);
4650 if (res < 0) {
4651 os_free(freqs);
4652 return -1;
4653 }
4654 num = res;
4655
4656 for (i = 0; i < num; i++) {
4657 if (freq && freqs[i] == freq)
4658 break;
4659 if (!freq && freq_included(channels, freqs[i])) {
4660 wpa_printf(MSG_DEBUG, "P2P: Force GO on a channel we are already using (%u MHz)",
4661 freqs[i]);
4662 params->freq = freqs[i];
4663 break;
4664 }
4665 }
4666
4667 if (i == num) {
4668 if (wpas_p2p_num_unused_channels(wpa_s) <= 0) {
4669 if (freq)
4670 wpa_printf(MSG_DEBUG, "P2P: Cannot force GO on freq (%u MHz) as all the channels are in use", freq);
4671 else
4672 wpa_printf(MSG_DEBUG, "P2P: Cannot force GO on any of the channels we are already using");
4673 os_free(freqs);
4674 return -1;
4675 } else if (num == 0) {
4676 wpa_printf(MSG_DEBUG, "P2P: Use one of the free channels");
4677 } else {
4678 wpa_printf(MSG_DEBUG, "P2P: Cannot force GO on any of the channels we are already using. Use one of the free channels");
4679 }
4680 }
4681
4682 os_free(freqs);
4683 return 0;
4684 }
4685
4686
4687 static struct wpa_supplicant *
4688 wpas_p2p_get_group_iface(struct wpa_supplicant *wpa_s, int addr_allocated,
4689 int go)
4690 {
4691 struct wpa_supplicant *group_wpa_s;
4692
4693 if (!wpas_p2p_create_iface(wpa_s)) {
4694 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Use same interface for group "
4695 "operations");
4696 wpa_s->p2p_first_connection_timeout = 0;
4697 return wpa_s;
4698 }
4699
4700 if (wpas_p2p_add_group_interface(wpa_s, go ? WPA_IF_P2P_GO :
4701 WPA_IF_P2P_CLIENT) < 0) {
4702 wpa_msg_global(wpa_s, MSG_ERROR,
4703 "P2P: Failed to add group interface");
4704 return NULL;
4705 }
4706 group_wpa_s = wpas_p2p_init_group_interface(wpa_s, go);
4707 if (group_wpa_s == NULL) {
4708 wpa_msg_global(wpa_s, MSG_ERROR,
4709 "P2P: Failed to initialize group interface");
4710 wpas_p2p_remove_pending_group_interface(wpa_s);
4711 return NULL;
4712 }
4713
4714 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Use separate group interface %s",
4715 group_wpa_s->ifname);
4716 group_wpa_s->p2p_first_connection_timeout = 0;
4717 return group_wpa_s;
4718 }
4719
4720
4721 /**
4722 * wpas_p2p_group_add - Add a new P2P group with local end as Group Owner
4723 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
4724 * @persistent_group: Whether to create a persistent group
4725 * @freq: Frequency for the group or 0 to indicate no hardcoding
4726 * @ht40: Start GO with 40 MHz channel width
4727 * @vht: Start GO with VHT support
4728 * Returns: 0 on success, -1 on failure
4729 *
4730 * This function creates a new P2P group with the local end as the Group Owner,
4731 * i.e., without using Group Owner Negotiation.
4732 */
4733 int wpas_p2p_group_add(struct wpa_supplicant *wpa_s, int persistent_group,
4734 int freq, int ht40, int vht)
4735 {
4736 struct p2p_go_neg_results params;
4737
4738 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
4739 return -1;
4740
4741 os_free(wpa_s->global->add_psk);
4742 wpa_s->global->add_psk = NULL;
4743
4744 /* Make sure we are not running find during connection establishment */
4745 wpa_printf(MSG_DEBUG, "P2P: Stop any on-going P2P FIND");
4746 wpas_p2p_stop_find_oper(wpa_s);
4747
4748 freq = wpas_p2p_select_go_freq(wpa_s, freq);
4749 if (freq < 0)
4750 return -1;
4751
4752 if (wpas_p2p_init_go_params(wpa_s, &params, freq, ht40, vht, NULL))
4753 return -1;
4754 if (params.freq &&
4755 !p2p_supported_freq_go(wpa_s->global->p2p, params.freq)) {
4756 wpa_printf(MSG_DEBUG, "P2P: The selected channel for GO "
4757 "(%u MHz) is not supported for P2P uses",
4758 params.freq);
4759 return -1;
4760 }
4761 p2p_go_params(wpa_s->global->p2p, &params);
4762 params.persistent_group = persistent_group;
4763
4764 wpa_s = wpas_p2p_get_group_iface(wpa_s, 0, 1);
4765 if (wpa_s == NULL)
4766 return -1;
4767 wpas_start_wps_go(wpa_s, &params, 0);
4768
4769 return 0;
4770 }
4771
4772
4773 static int wpas_start_p2p_client(struct wpa_supplicant *wpa_s,
4774 struct wpa_ssid *params, int addr_allocated)
4775 {
4776 struct wpa_ssid *ssid;
4777
4778 wpa_s = wpas_p2p_get_group_iface(wpa_s, addr_allocated, 0);
4779 if (wpa_s == NULL)
4780 return -1;
4781 wpa_s->p2p_last_4way_hs_fail = NULL;
4782
4783 wpa_supplicant_ap_deinit(wpa_s);
4784
4785 ssid = wpa_config_add_network(wpa_s->conf);
4786 if (ssid == NULL)
4787 return -1;
4788 wpa_config_set_network_defaults(ssid);
4789 ssid->temporary = 1;
4790 ssid->proto = WPA_PROTO_RSN;
4791 ssid->pairwise_cipher = WPA_CIPHER_CCMP;
4792 ssid->group_cipher = WPA_CIPHER_CCMP;
4793 ssid->key_mgmt = WPA_KEY_MGMT_PSK;
4794 ssid->ssid = os_malloc(params->ssid_len);
4795 if (ssid->ssid == NULL) {
4796 wpa_config_remove_network(wpa_s->conf, ssid->id);
4797 return -1;
4798 }
4799 os_memcpy(ssid->ssid, params->ssid, params->ssid_len);
4800 ssid->ssid_len = params->ssid_len;
4801 ssid->p2p_group = 1;
4802 ssid->export_keys = 1;
4803 if (params->psk_set) {
4804 os_memcpy(ssid->psk, params->psk, 32);
4805 ssid->psk_set = 1;
4806 }
4807 if (params->passphrase)
4808 ssid->passphrase = os_strdup(params->passphrase);
4809
4810 wpa_s->show_group_started = 1;
4811
4812 wpa_supplicant_select_network(wpa_s, ssid);
4813
4814 return 0;
4815 }
4816
4817
4818 int wpas_p2p_group_add_persistent(struct wpa_supplicant *wpa_s,
4819 struct wpa_ssid *ssid, int addr_allocated,
4820 int freq, int ht40, int vht,
4821 const struct p2p_channels *channels,
4822 int connection_timeout)
4823 {
4824 struct p2p_go_neg_results params;
4825 int go = 0;
4826
4827 if (ssid->disabled != 2 || ssid->ssid == NULL)
4828 return -1;
4829
4830 if (wpas_get_p2p_group(wpa_s, ssid->ssid, ssid->ssid_len, &go) &&
4831 go == (ssid->mode == WPAS_MODE_P2P_GO)) {
4832 wpa_printf(MSG_DEBUG, "P2P: Requested persistent group is "
4833 "already running");
4834 return 0;
4835 }
4836
4837 os_free(wpa_s->global->add_psk);
4838 wpa_s->global->add_psk = NULL;
4839
4840 /* Make sure we are not running find during connection establishment */
4841 wpas_p2p_stop_find_oper(wpa_s);
4842
4843 wpa_s->p2p_fallback_to_go_neg = 0;
4844
4845 if (ssid->mode == WPAS_MODE_INFRA)
4846 return wpas_start_p2p_client(wpa_s, ssid, addr_allocated);
4847
4848 if (ssid->mode != WPAS_MODE_P2P_GO)
4849 return -1;
4850
4851 freq = wpas_p2p_select_go_freq(wpa_s, freq);
4852 if (freq < 0)
4853 return -1;
4854
4855 if (wpas_p2p_init_go_params(wpa_s, &params, freq, ht40, vht, channels))
4856 return -1;
4857
4858 params.role_go = 1;
4859 params.psk_set = ssid->psk_set;
4860 if (params.psk_set)
4861 os_memcpy(params.psk, ssid->psk, sizeof(params.psk));
4862 if (ssid->passphrase) {
4863 if (os_strlen(ssid->passphrase) >= sizeof(params.passphrase)) {
4864 wpa_printf(MSG_ERROR, "P2P: Invalid passphrase in "
4865 "persistent group");
4866 return -1;
4867 }
4868 os_strlcpy(params.passphrase, ssid->passphrase,
4869 sizeof(params.passphrase));
4870 }
4871 os_memcpy(params.ssid, ssid->ssid, ssid->ssid_len);
4872 params.ssid_len = ssid->ssid_len;
4873 params.persistent_group = 1;
4874
4875 wpa_s = wpas_p2p_get_group_iface(wpa_s, addr_allocated, 1);
4876 if (wpa_s == NULL)
4877 return -1;
4878
4879 wpa_s->p2p_first_connection_timeout = connection_timeout;
4880 wpas_start_wps_go(wpa_s, &params, 0);
4881
4882 return 0;
4883 }
4884
4885
4886 static void wpas_p2p_ie_update(void *ctx, struct wpabuf *beacon_ies,
4887 struct wpabuf *proberesp_ies)
4888 {
4889 struct wpa_supplicant *wpa_s = ctx;
4890 if (wpa_s->ap_iface) {
4891 struct hostapd_data *hapd = wpa_s->ap_iface->bss[0];
4892 if (!(hapd->conf->p2p & P2P_GROUP_OWNER)) {
4893 wpabuf_free(beacon_ies);
4894 wpabuf_free(proberesp_ies);
4895 return;
4896 }
4897 if (beacon_ies) {
4898 wpabuf_free(hapd->p2p_beacon_ie);
4899 hapd->p2p_beacon_ie = beacon_ies;
4900 }
4901 wpabuf_free(hapd->p2p_probe_resp_ie);
4902 hapd->p2p_probe_resp_ie = proberesp_ies;
4903 } else {
4904 wpabuf_free(beacon_ies);
4905 wpabuf_free(proberesp_ies);
4906 }
4907 wpa_supplicant_ap_update_beacon(wpa_s);
4908 }
4909
4910
4911 static void wpas_p2p_idle_update(void *ctx, int idle)
4912 {
4913 struct wpa_supplicant *wpa_s = ctx;
4914 if (!wpa_s->ap_iface)
4915 return;
4916 wpa_printf(MSG_DEBUG, "P2P: GO - group %sidle", idle ? "" : "not ");
4917 if (idle)
4918 wpas_p2p_set_group_idle_timeout(wpa_s);
4919 else
4920 eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL);
4921 }
4922
4923
4924 struct p2p_group * wpas_p2p_group_init(struct wpa_supplicant *wpa_s,
4925 struct wpa_ssid *ssid)
4926 {
4927 struct p2p_group *group;
4928 struct p2p_group_config *cfg;
4929
4930 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
4931 return NULL;
4932
4933 cfg = os_zalloc(sizeof(*cfg));
4934 if (cfg == NULL)
4935 return NULL;
4936
4937 if (ssid->p2p_persistent_group && wpa_s->conf->persistent_reconnect)
4938 cfg->persistent_group = 2;
4939 else if (ssid->p2p_persistent_group)
4940 cfg->persistent_group = 1;
4941 os_memcpy(cfg->interface_addr, wpa_s->own_addr, ETH_ALEN);
4942 if (wpa_s->max_stations &&
4943 wpa_s->max_stations < wpa_s->conf->max_num_sta)
4944 cfg->max_clients = wpa_s->max_stations;
4945 else
4946 cfg->max_clients = wpa_s->conf->max_num_sta;
4947 os_memcpy(cfg->ssid, ssid->ssid, ssid->ssid_len);
4948 cfg->ssid_len = ssid->ssid_len;
4949 cfg->cb_ctx = wpa_s;
4950 cfg->ie_update = wpas_p2p_ie_update;
4951 cfg->idle_update = wpas_p2p_idle_update;
4952
4953 group = p2p_group_init(wpa_s->global->p2p, cfg);
4954 if (group == NULL)
4955 os_free(cfg);
4956 if (ssid->mode != WPAS_MODE_P2P_GROUP_FORMATION)
4957 p2p_group_notif_formation_done(group);
4958 wpa_s->p2p_group = group;
4959 return group;
4960 }
4961
4962
4963 void wpas_p2p_wps_success(struct wpa_supplicant *wpa_s, const u8 *peer_addr,
4964 int registrar)
4965 {
4966 struct wpa_ssid *ssid = wpa_s->current_ssid;
4967
4968 if (!wpa_s->p2p_in_provisioning) {
4969 wpa_printf(MSG_DEBUG, "P2P: Ignore WPS success event - P2P "
4970 "provisioning not in progress");
4971 return;
4972 }
4973
4974 if (ssid && ssid->mode == WPAS_MODE_INFRA) {
4975 u8 go_dev_addr[ETH_ALEN];
4976 os_memcpy(go_dev_addr, wpa_s->bssid, ETH_ALEN);
4977 wpas_p2p_persistent_group(wpa_s, go_dev_addr, ssid->ssid,
4978 ssid->ssid_len);
4979 /* Clear any stored provisioning info */
4980 p2p_clear_provisioning_info(wpa_s->global->p2p, go_dev_addr);
4981 }
4982
4983 eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s->parent,
4984 NULL);
4985 wpa_s->p2p_go_group_formation_completed = 1;
4986 if (ssid && ssid->mode == WPAS_MODE_INFRA) {
4987 /*
4988 * Use a separate timeout for initial data connection to
4989 * complete to allow the group to be removed automatically if
4990 * something goes wrong in this step before the P2P group idle
4991 * timeout mechanism is taken into use.
4992 */
4993 wpa_dbg(wpa_s, MSG_DEBUG,
4994 "P2P: Re-start group formation timeout (%d seconds) as client for initial connection",
4995 P2P_MAX_INITIAL_CONN_WAIT);
4996 eloop_register_timeout(P2P_MAX_INITIAL_CONN_WAIT, 0,
4997 wpas_p2p_group_formation_timeout,
4998 wpa_s->parent, NULL);
4999 } else if (ssid) {
5000 /*
5001 * Use a separate timeout for initial data connection to
5002 * complete to allow the group to be removed automatically if
5003 * the client does not complete data connection successfully.
5004 */
5005 wpa_dbg(wpa_s, MSG_DEBUG,
5006 "P2P: Re-start group formation timeout (%d seconds) as GO for initial connection",
5007 P2P_MAX_INITIAL_CONN_WAIT_GO);
5008 eloop_register_timeout(P2P_MAX_INITIAL_CONN_WAIT_GO, 0,
5009 wpas_p2p_group_formation_timeout,
5010 wpa_s->parent, NULL);
5011 /*
5012 * Complete group formation on first successful data connection
5013 */
5014 wpa_s->p2p_go_group_formation_completed = 0;
5015 }
5016 if (wpa_s->global->p2p)
5017 p2p_wps_success_cb(wpa_s->global->p2p, peer_addr);
5018 wpas_group_formation_completed(wpa_s, 1);
5019 }
5020
5021
5022 void wpas_p2p_wps_failed(struct wpa_supplicant *wpa_s,
5023 struct wps_event_fail *fail)
5024 {
5025 if (!wpa_s->p2p_in_provisioning) {
5026 wpa_printf(MSG_DEBUG, "P2P: Ignore WPS fail event - P2P "
5027 "provisioning not in progress");
5028 return;
5029 }
5030
5031 if (wpa_s->go_params) {
5032 p2p_clear_provisioning_info(
5033 wpa_s->global->p2p,
5034 wpa_s->go_params->peer_device_addr);
5035 }
5036
5037 wpas_notify_p2p_wps_failed(wpa_s, fail);
5038 }
5039
5040
5041 int wpas_p2p_prov_disc(struct wpa_supplicant *wpa_s, const u8 *peer_addr,
5042 const char *config_method,
5043 enum wpas_p2p_prov_disc_use use)
5044 {
5045 u16 config_methods;
5046
5047 wpa_s->p2p_fallback_to_go_neg = 0;
5048 wpa_s->pending_pd_use = NORMAL_PD;
5049 if (os_strncmp(config_method, "display", 7) == 0)
5050 config_methods = WPS_CONFIG_DISPLAY;
5051 else if (os_strncmp(config_method, "keypad", 6) == 0)
5052 config_methods = WPS_CONFIG_KEYPAD;
5053 else if (os_strncmp(config_method, "pbc", 3) == 0 ||
5054 os_strncmp(config_method, "pushbutton", 10) == 0)
5055 config_methods = WPS_CONFIG_PUSHBUTTON;
5056 else {
5057 wpa_printf(MSG_DEBUG, "P2P: Unknown config method");
5058 return -1;
5059 }
5060
5061 if (use == WPAS_P2P_PD_AUTO) {
5062 os_memcpy(wpa_s->pending_join_dev_addr, peer_addr, ETH_ALEN);
5063 wpa_s->pending_pd_config_methods = config_methods;
5064 wpa_s->p2p_auto_pd = 1;
5065 wpa_s->p2p_auto_join = 0;
5066 wpa_s->pending_pd_before_join = 0;
5067 wpa_s->auto_pd_scan_retry = 0;
5068 wpas_p2p_stop_find(wpa_s);
5069 wpa_s->p2p_join_scan_count = 0;
5070 os_get_reltime(&wpa_s->p2p_auto_started);
5071 wpa_printf(MSG_DEBUG, "P2P: Auto PD started at %ld.%06ld",
5072 wpa_s->p2p_auto_started.sec,
5073 wpa_s->p2p_auto_started.usec);
5074 wpas_p2p_join_scan(wpa_s, NULL);
5075 return 0;
5076 }
5077
5078 if (wpa_s->global->p2p == NULL || wpa_s->global->p2p_disabled)
5079 return -1;
5080
5081 return p2p_prov_disc_req(wpa_s->global->p2p, peer_addr,
5082 config_methods, use == WPAS_P2P_PD_FOR_JOIN,
5083 0, 1);
5084 }
5085
5086
5087 int wpas_p2p_scan_result_text(const u8 *ies, size_t ies_len, char *buf,
5088 char *end)
5089 {
5090 return p2p_scan_result_text(ies, ies_len, buf, end);
5091 }
5092
5093
5094 static void wpas_p2p_clear_pending_action_tx(struct wpa_supplicant *wpa_s)
5095 {
5096 if (!offchannel_pending_action_tx(wpa_s))
5097 return;
5098
5099 wpa_printf(MSG_DEBUG, "P2P: Drop pending Action TX due to new "
5100 "operation request");
5101 offchannel_clear_pending_action_tx(wpa_s);
5102 }
5103
5104
5105 int wpas_p2p_find(struct wpa_supplicant *wpa_s, unsigned int timeout,
5106 enum p2p_discovery_type type,
5107 unsigned int num_req_dev_types, const u8 *req_dev_types,
5108 const u8 *dev_id, unsigned int search_delay)
5109 {
5110 wpas_p2p_clear_pending_action_tx(wpa_s);
5111 wpa_s->p2p_long_listen = 0;
5112
5113 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL ||
5114 wpa_s->p2p_in_provisioning)
5115 return -1;
5116
5117 wpa_supplicant_cancel_sched_scan(wpa_s);
5118
5119 return p2p_find(wpa_s->global->p2p, timeout, type,
5120 num_req_dev_types, req_dev_types, dev_id,
5121 search_delay);
5122 }
5123
5124
5125 static int wpas_p2p_stop_find_oper(struct wpa_supplicant *wpa_s)
5126 {
5127 wpas_p2p_clear_pending_action_tx(wpa_s);
5128 wpa_s->p2p_long_listen = 0;
5129 eloop_cancel_timeout(wpas_p2p_long_listen_timeout, wpa_s, NULL);
5130 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
5131
5132 if (wpa_s->global->p2p)
5133 p2p_stop_find(wpa_s->global->p2p);
5134
5135 return 0;
5136 }
5137
5138
5139 void wpas_p2p_stop_find(struct wpa_supplicant *wpa_s)
5140 {
5141 if (wpas_p2p_stop_find_oper(wpa_s) > 0)
5142 return;
5143 wpas_p2p_remove_pending_group_interface(wpa_s);
5144 }
5145
5146
5147 static void wpas_p2p_long_listen_timeout(void *eloop_ctx, void *timeout_ctx)
5148 {
5149 struct wpa_supplicant *wpa_s = eloop_ctx;
5150 wpa_s->p2p_long_listen = 0;
5151 }
5152
5153
5154 int wpas_p2p_listen(struct wpa_supplicant *wpa_s, unsigned int timeout)
5155 {
5156 int res;
5157
5158 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
5159 return -1;
5160
5161 wpa_supplicant_cancel_sched_scan(wpa_s);
5162 wpas_p2p_clear_pending_action_tx(wpa_s);
5163
5164 if (timeout == 0) {
5165 /*
5166 * This is a request for unlimited Listen state. However, at
5167 * least for now, this is mapped to a Listen state for one
5168 * hour.
5169 */
5170 timeout = 3600;
5171 }
5172 eloop_cancel_timeout(wpas_p2p_long_listen_timeout, wpa_s, NULL);
5173 wpa_s->p2p_long_listen = 0;
5174
5175 /*
5176 * Stop previous find/listen operation to avoid trying to request a new
5177 * remain-on-channel operation while the driver is still running the
5178 * previous one.
5179 */
5180 if (wpa_s->global->p2p)
5181 p2p_stop_find(wpa_s->global->p2p);
5182
5183 res = wpas_p2p_listen_start(wpa_s, timeout * 1000);
5184 if (res == 0 && timeout * 1000 > wpa_s->max_remain_on_chan) {
5185 wpa_s->p2p_long_listen = timeout * 1000;
5186 eloop_register_timeout(timeout, 0,
5187 wpas_p2p_long_listen_timeout,
5188 wpa_s, NULL);
5189 }
5190
5191 return res;
5192 }
5193
5194
5195 int wpas_p2p_assoc_req_ie(struct wpa_supplicant *wpa_s, struct wpa_bss *bss,
5196 u8 *buf, size_t len, int p2p_group)
5197 {
5198 struct wpabuf *p2p_ie;
5199 int ret;
5200
5201 if (wpa_s->global->p2p_disabled)
5202 return -1;
5203 if (wpa_s->global->p2p == NULL)
5204 return -1;
5205 if (bss == NULL)
5206 return -1;
5207
5208 p2p_ie = wpa_bss_get_vendor_ie_multi(bss, P2P_IE_VENDOR_TYPE);
5209 ret = p2p_assoc_req_ie(wpa_s->global->p2p, bss->bssid, buf, len,
5210 p2p_group, p2p_ie);
5211 wpabuf_free(p2p_ie);
5212
5213 return ret;
5214 }
5215
5216
5217 int wpas_p2p_probe_req_rx(struct wpa_supplicant *wpa_s, const u8 *addr,
5218 const u8 *dst, const u8 *bssid,
5219 const u8 *ie, size_t ie_len, int ssi_signal)
5220 {
5221 if (wpa_s->global->p2p_disabled)
5222 return 0;
5223 if (wpa_s->global->p2p == NULL)
5224 return 0;
5225
5226 switch (p2p_probe_req_rx(wpa_s->global->p2p, addr, dst, bssid,
5227 ie, ie_len)) {
5228 case P2P_PREQ_NOT_P2P:
5229 wpas_notify_preq(wpa_s, addr, dst, bssid, ie, ie_len,
5230 ssi_signal);
5231 /* fall through */
5232 case P2P_PREQ_MALFORMED:
5233 case P2P_PREQ_NOT_LISTEN:
5234 case P2P_PREQ_NOT_PROCESSED:
5235 default: /* make gcc happy */
5236 return 0;
5237 case P2P_PREQ_PROCESSED:
5238 return 1;
5239 }
5240 }
5241
5242
5243 void wpas_p2p_rx_action(struct wpa_supplicant *wpa_s, const u8 *da,
5244 const u8 *sa, const u8 *bssid,
5245 u8 category, const u8 *data, size_t len, int freq)
5246 {
5247 if (wpa_s->global->p2p_disabled)
5248 return;
5249 if (wpa_s->global->p2p == NULL)
5250 return;
5251
5252 p2p_rx_action(wpa_s->global->p2p, da, sa, bssid, category, data, len,
5253 freq);
5254 }
5255
5256
5257 void wpas_p2p_scan_ie(struct wpa_supplicant *wpa_s, struct wpabuf *ies)
5258 {
5259 if (wpa_s->global->p2p_disabled)
5260 return;
5261 if (wpa_s->global->p2p == NULL)
5262 return;
5263
5264 p2p_scan_ie(wpa_s->global->p2p, ies, NULL);
5265 }
5266
5267
5268 void wpas_p2p_group_deinit(struct wpa_supplicant *wpa_s)
5269 {
5270 p2p_group_deinit(wpa_s->p2p_group);
5271 wpa_s->p2p_group = NULL;
5272
5273 wpa_s->ap_configured_cb = NULL;
5274 wpa_s->ap_configured_cb_ctx = NULL;
5275 wpa_s->ap_configured_cb_data = NULL;
5276 wpa_s->connect_without_scan = NULL;
5277 }
5278
5279
5280 int wpas_p2p_reject(struct wpa_supplicant *wpa_s, const u8 *addr)
5281 {
5282 wpa_s->p2p_long_listen = 0;
5283
5284 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
5285 return -1;
5286
5287 return p2p_reject(wpa_s->global->p2p, addr);
5288 }
5289
5290
5291 /* Invite to reinvoke a persistent group */
5292 int wpas_p2p_invite(struct wpa_supplicant *wpa_s, const u8 *peer_addr,
5293 struct wpa_ssid *ssid, const u8 *go_dev_addr, int freq,
5294 int ht40, int vht, int pref_freq)
5295 {
5296 enum p2p_invite_role role;
5297 u8 *bssid = NULL;
5298 int force_freq = 0;
5299 int res;
5300 int no_pref_freq_given = pref_freq == 0;
5301
5302 wpa_s->global->p2p_invite_group = NULL;
5303 if (peer_addr)
5304 os_memcpy(wpa_s->p2p_auth_invite, peer_addr, ETH_ALEN);
5305 else
5306 os_memset(wpa_s->p2p_auth_invite, 0, ETH_ALEN);
5307
5308 wpa_s->p2p_persistent_go_freq = freq;
5309 wpa_s->p2p_go_ht40 = !!ht40;
5310 if (ssid->mode == WPAS_MODE_P2P_GO) {
5311 role = P2P_INVITE_ROLE_GO;
5312 if (peer_addr == NULL) {
5313 wpa_printf(MSG_DEBUG, "P2P: Missing peer "
5314 "address in invitation command");
5315 return -1;
5316 }
5317 if (wpas_p2p_create_iface(wpa_s)) {
5318 if (wpas_p2p_add_group_interface(wpa_s,
5319 WPA_IF_P2P_GO) < 0) {
5320 wpa_printf(MSG_ERROR, "P2P: Failed to "
5321 "allocate a new interface for the "
5322 "group");
5323 return -1;
5324 }
5325 bssid = wpa_s->pending_interface_addr;
5326 } else
5327 bssid = wpa_s->own_addr;
5328 } else {
5329 role = P2P_INVITE_ROLE_CLIENT;
5330 peer_addr = ssid->bssid;
5331 }
5332 wpa_s->pending_invite_ssid_id = ssid->id;
5333
5334 res = wpas_p2p_setup_freqs(wpa_s, freq, &force_freq, &pref_freq,
5335 role == P2P_INVITE_ROLE_GO);
5336 if (res)
5337 return res;
5338
5339 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
5340 return -1;
5341
5342 if (wpa_s->parent->conf->p2p_ignore_shared_freq &&
5343 no_pref_freq_given && pref_freq > 0 &&
5344 wpa_s->num_multichan_concurrent > 1 &&
5345 wpas_p2p_num_unused_channels(wpa_s) > 0) {
5346 wpa_printf(MSG_DEBUG, "P2P: Ignore own channel preference %d MHz for invitation due to p2p_ignore_shared_freq=1 configuration",
5347 pref_freq);
5348 pref_freq = 0;
5349 }
5350
5351 return p2p_invite(wpa_s->global->p2p, peer_addr, role, bssid,
5352 ssid->ssid, ssid->ssid_len, force_freq, go_dev_addr,
5353 1, pref_freq);
5354 }
5355
5356
5357 /* Invite to join an active group */
5358 int wpas_p2p_invite_group(struct wpa_supplicant *wpa_s, const char *ifname,
5359 const u8 *peer_addr, const u8 *go_dev_addr)
5360 {
5361 struct wpa_global *global = wpa_s->global;
5362 enum p2p_invite_role role;
5363 u8 *bssid = NULL;
5364 struct wpa_ssid *ssid;
5365 int persistent;
5366 int freq = 0, force_freq = 0, pref_freq = 0;
5367 int res;
5368
5369 wpa_s->p2p_persistent_go_freq = 0;
5370 wpa_s->p2p_go_ht40 = 0;
5371 wpa_s->p2p_go_vht = 0;
5372
5373 for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
5374 if (os_strcmp(wpa_s->ifname, ifname) == 0)
5375 break;
5376 }
5377 if (wpa_s == NULL) {
5378 wpa_printf(MSG_DEBUG, "P2P: Interface '%s' not found", ifname);
5379 return -1;
5380 }
5381
5382 ssid = wpa_s->current_ssid;
5383 if (ssid == NULL) {
5384 wpa_printf(MSG_DEBUG, "P2P: No current SSID to use for "
5385 "invitation");
5386 return -1;
5387 }
5388
5389 wpa_s->global->p2p_invite_group = wpa_s;
5390 persistent = ssid->p2p_persistent_group &&
5391 wpas_p2p_get_persistent(wpa_s->parent, peer_addr,
5392 ssid->ssid, ssid->ssid_len);
5393
5394 if (ssid->mode == WPAS_MODE_P2P_GO) {
5395 role = P2P_INVITE_ROLE_ACTIVE_GO;
5396 bssid = wpa_s->own_addr;
5397 if (go_dev_addr == NULL)
5398 go_dev_addr = wpa_s->global->p2p_dev_addr;
5399 freq = ssid->frequency;
5400 } else {
5401 role = P2P_INVITE_ROLE_CLIENT;
5402 if (wpa_s->wpa_state < WPA_ASSOCIATED) {
5403 wpa_printf(MSG_DEBUG, "P2P: Not associated - cannot "
5404 "invite to current group");
5405 return -1;
5406 }
5407 bssid = wpa_s->bssid;
5408 if (go_dev_addr == NULL &&
5409 !is_zero_ether_addr(wpa_s->go_dev_addr))
5410 go_dev_addr = wpa_s->go_dev_addr;
5411 freq = wpa_s->current_bss ? wpa_s->current_bss->freq :
5412 (int) wpa_s->assoc_freq;
5413 }
5414 wpa_s->parent->pending_invite_ssid_id = -1;
5415
5416 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
5417 return -1;
5418
5419 res = wpas_p2p_setup_freqs(wpa_s, freq, &force_freq, &pref_freq,
5420 role == P2P_INVITE_ROLE_ACTIVE_GO);
5421 if (res)
5422 return res;
5423 wpas_p2p_set_own_freq_preference(wpa_s, force_freq);
5424
5425 return p2p_invite(wpa_s->global->p2p, peer_addr, role, bssid,
5426 ssid->ssid, ssid->ssid_len, force_freq,
5427 go_dev_addr, persistent, pref_freq);
5428 }
5429
5430
5431 void wpas_p2p_completed(struct wpa_supplicant *wpa_s)
5432 {
5433 struct wpa_ssid *ssid = wpa_s->current_ssid;
5434 const char *ssid_txt;
5435 u8 go_dev_addr[ETH_ALEN];
5436 int network_id = -1;
5437 int persistent;
5438 int freq;
5439
5440 if (ssid == NULL || ssid->mode != WPAS_MODE_P2P_GROUP_FORMATION) {
5441 eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
5442 wpa_s->parent, NULL);
5443 }
5444
5445 if (!wpa_s->show_group_started || !ssid)
5446 return;
5447
5448 wpa_s->show_group_started = 0;
5449
5450 ssid_txt = wpa_ssid_txt(ssid->ssid, ssid->ssid_len);
5451 os_memset(go_dev_addr, 0, ETH_ALEN);
5452 if (ssid->bssid_set)
5453 os_memcpy(go_dev_addr, ssid->bssid, ETH_ALEN);
5454 persistent = wpas_p2p_persistent_group(wpa_s, go_dev_addr, ssid->ssid,
5455 ssid->ssid_len);
5456 os_memcpy(wpa_s->go_dev_addr, go_dev_addr, ETH_ALEN);
5457
5458 if (wpa_s->global->p2p_group_formation == wpa_s)
5459 wpa_s->global->p2p_group_formation = NULL;
5460
5461 freq = wpa_s->current_bss ? wpa_s->current_bss->freq :
5462 (int) wpa_s->assoc_freq;
5463 if (ssid->passphrase == NULL && ssid->psk_set) {
5464 char psk[65];
5465 wpa_snprintf_hex(psk, sizeof(psk), ssid->psk, 32);
5466 wpa_msg_global(wpa_s->parent, MSG_INFO, P2P_EVENT_GROUP_STARTED
5467 "%s client ssid=\"%s\" freq=%d psk=%s "
5468 "go_dev_addr=" MACSTR "%s",
5469 wpa_s->ifname, ssid_txt, freq, psk,
5470 MAC2STR(go_dev_addr),
5471 persistent ? " [PERSISTENT]" : "");
5472 } else {
5473 wpa_msg_global(wpa_s->parent, MSG_INFO, P2P_EVENT_GROUP_STARTED
5474 "%s client ssid=\"%s\" freq=%d "
5475 "passphrase=\"%s\" go_dev_addr=" MACSTR "%s",
5476 wpa_s->ifname, ssid_txt, freq,
5477 ssid->passphrase ? ssid->passphrase : "",
5478 MAC2STR(go_dev_addr),
5479 persistent ? " [PERSISTENT]" : "");
5480 }
5481
5482 if (persistent)
5483 network_id = wpas_p2p_store_persistent_group(wpa_s->parent,
5484 ssid, go_dev_addr);
5485 if (network_id < 0)
5486 network_id = ssid->id;
5487 wpas_notify_p2p_group_started(wpa_s, ssid, network_id, 1);
5488 }
5489
5490
5491 int wpas_p2p_presence_req(struct wpa_supplicant *wpa_s, u32 duration1,
5492 u32 interval1, u32 duration2, u32 interval2)
5493 {
5494 int ret;
5495
5496 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
5497 return -1;
5498
5499 if (wpa_s->wpa_state < WPA_ASSOCIATED ||
5500 wpa_s->current_ssid == NULL ||
5501 wpa_s->current_ssid->mode != WPAS_MODE_INFRA)
5502 return -1;
5503
5504 ret = p2p_presence_req(wpa_s->global->p2p, wpa_s->bssid,
5505 wpa_s->own_addr, wpa_s->assoc_freq,
5506 duration1, interval1, duration2, interval2);
5507 if (ret == 0)
5508 wpa_s->waiting_presence_resp = 1;
5509
5510 return ret;
5511 }
5512
5513
5514 int wpas_p2p_ext_listen(struct wpa_supplicant *wpa_s, unsigned int period,
5515 unsigned int interval)
5516 {
5517 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
5518 return -1;
5519
5520 return p2p_ext_listen(wpa_s->global->p2p, period, interval);
5521 }
5522
5523
5524 static int wpas_p2p_is_client(struct wpa_supplicant *wpa_s)
5525 {
5526 if (wpa_s->current_ssid == NULL) {
5527 /*
5528 * current_ssid can be cleared when P2P client interface gets
5529 * disconnected, so assume this interface was used as P2P
5530 * client.
5531 */
5532 return 1;
5533 }
5534 return wpa_s->current_ssid->p2p_group &&
5535 wpa_s->current_ssid->mode == WPAS_MODE_INFRA;
5536 }
5537
5538
5539 static void wpas_p2p_group_idle_timeout(void *eloop_ctx, void *timeout_ctx)
5540 {
5541 struct wpa_supplicant *wpa_s = eloop_ctx;
5542
5543 if (wpa_s->conf->p2p_group_idle == 0 && !wpas_p2p_is_client(wpa_s)) {
5544 wpa_printf(MSG_DEBUG, "P2P: Ignore group idle timeout - "
5545 "disabled");
5546 return;
5547 }
5548
5549 wpa_printf(MSG_DEBUG, "P2P: Group idle timeout reached - terminate "
5550 "group");
5551 wpas_p2p_group_delete(wpa_s, P2P_GROUP_REMOVAL_IDLE_TIMEOUT);
5552 }
5553
5554
5555 static void wpas_p2p_set_group_idle_timeout(struct wpa_supplicant *wpa_s)
5556 {
5557 int timeout;
5558
5559 if (eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL) > 0)
5560 wpa_printf(MSG_DEBUG, "P2P: Cancelled P2P group idle timeout");
5561
5562 if (wpa_s->current_ssid == NULL || !wpa_s->current_ssid->p2p_group)
5563 return;
5564
5565 timeout = wpa_s->conf->p2p_group_idle;
5566 if (wpa_s->current_ssid->mode == WPAS_MODE_INFRA &&
5567 (timeout == 0 || timeout > P2P_MAX_CLIENT_IDLE))
5568 timeout = P2P_MAX_CLIENT_IDLE;
5569
5570 if (timeout == 0)
5571 return;
5572
5573 if (timeout < 0) {
5574 if (wpa_s->current_ssid->mode == WPAS_MODE_INFRA)
5575 timeout = 0; /* special client mode no-timeout */
5576 else
5577 return;
5578 }
5579
5580 if (wpa_s->p2p_in_provisioning) {
5581 /*
5582 * Use the normal group formation timeout during the
5583 * provisioning phase to avoid terminating this process too
5584 * early due to group idle timeout.
5585 */
5586 wpa_printf(MSG_DEBUG, "P2P: Do not use P2P group idle timeout "
5587 "during provisioning");
5588 return;
5589 }
5590
5591 if (wpa_s->show_group_started) {
5592 /*
5593 * Use the normal group formation timeout between the end of
5594 * the provisioning phase and completion of 4-way handshake to
5595 * avoid terminating this process too early due to group idle
5596 * timeout.
5597 */
5598 wpa_printf(MSG_DEBUG, "P2P: Do not use P2P group idle timeout "
5599 "while waiting for initial 4-way handshake to "
5600 "complete");
5601 return;
5602 }
5603
5604 wpa_printf(MSG_DEBUG, "P2P: Set P2P group idle timeout to %u seconds",
5605 timeout);
5606 eloop_register_timeout(timeout, 0, wpas_p2p_group_idle_timeout,
5607 wpa_s, NULL);
5608 }
5609
5610
5611 /* Returns 1 if the interface was removed */
5612 int wpas_p2p_deauth_notif(struct wpa_supplicant *wpa_s, const u8 *bssid,
5613 u16 reason_code, const u8 *ie, size_t ie_len,
5614 int locally_generated)
5615 {
5616 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
5617 return 0;
5618
5619 if (!locally_generated)
5620 p2p_deauth_notif(wpa_s->global->p2p, bssid, reason_code, ie,
5621 ie_len);
5622
5623 if (reason_code == WLAN_REASON_DEAUTH_LEAVING && !locally_generated &&
5624 wpa_s->current_ssid &&
5625 wpa_s->current_ssid->p2p_group &&
5626 wpa_s->current_ssid->mode == WPAS_MODE_INFRA) {
5627 wpa_printf(MSG_DEBUG, "P2P: GO indicated that the P2P Group "
5628 "session is ending");
5629 if (wpas_p2p_group_delete(wpa_s,
5630 P2P_GROUP_REMOVAL_GO_ENDING_SESSION)
5631 > 0)
5632 return 1;
5633 }
5634
5635 return 0;
5636 }
5637
5638
5639 void wpas_p2p_disassoc_notif(struct wpa_supplicant *wpa_s, const u8 *bssid,
5640 u16 reason_code, const u8 *ie, size_t ie_len,
5641 int locally_generated)
5642 {
5643 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
5644 return;
5645
5646 if (!locally_generated)
5647 p2p_disassoc_notif(wpa_s->global->p2p, bssid, reason_code, ie,
5648 ie_len);
5649 }
5650
5651
5652 void wpas_p2p_update_config(struct wpa_supplicant *wpa_s)
5653 {
5654 struct p2p_data *p2p = wpa_s->global->p2p;
5655
5656 if (p2p == NULL)
5657 return;
5658
5659 if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_CAPABLE))
5660 return;
5661
5662 if (wpa_s->conf->changed_parameters & CFG_CHANGED_DEVICE_NAME)
5663 p2p_set_dev_name(p2p, wpa_s->conf->device_name);
5664
5665 if (wpa_s->conf->changed_parameters & CFG_CHANGED_DEVICE_TYPE)
5666 p2p_set_pri_dev_type(p2p, wpa_s->conf->device_type);
5667
5668 if (wpa_s->wps &&
5669 (wpa_s->conf->changed_parameters & CFG_CHANGED_CONFIG_METHODS))
5670 p2p_set_config_methods(p2p, wpa_s->wps->config_methods);
5671
5672 if (wpa_s->wps && (wpa_s->conf->changed_parameters & CFG_CHANGED_UUID))
5673 p2p_set_uuid(p2p, wpa_s->wps->uuid);
5674
5675 if (wpa_s->conf->changed_parameters & CFG_CHANGED_WPS_STRING) {
5676 p2p_set_manufacturer(p2p, wpa_s->conf->manufacturer);
5677 p2p_set_model_name(p2p, wpa_s->conf->model_name);
5678 p2p_set_model_number(p2p, wpa_s->conf->model_number);
5679 p2p_set_serial_number(p2p, wpa_s->conf->serial_number);
5680 }
5681
5682 if (wpa_s->conf->changed_parameters & CFG_CHANGED_SEC_DEVICE_TYPE)
5683 p2p_set_sec_dev_types(p2p,
5684 (void *) wpa_s->conf->sec_device_type,
5685 wpa_s->conf->num_sec_device_types);
5686
5687 if (wpa_s->conf->changed_parameters & CFG_CHANGED_VENDOR_EXTENSION) {
5688 int i;
5689 p2p_remove_wps_vendor_extensions(p2p);
5690 for (i = 0; i < MAX_WPS_VENDOR_EXT; i++) {
5691 if (wpa_s->conf->wps_vendor_ext[i] == NULL)
5692 continue;
5693 p2p_add_wps_vendor_extension(
5694 p2p, wpa_s->conf->wps_vendor_ext[i]);
5695 }
5696 }
5697
5698 if ((wpa_s->conf->changed_parameters & CFG_CHANGED_COUNTRY) &&
5699 wpa_s->conf->country[0] && wpa_s->conf->country[1]) {
5700 char country[3];
5701 country[0] = wpa_s->conf->country[0];
5702 country[1] = wpa_s->conf->country[1];
5703 country[2] = 0x04;
5704 p2p_set_country(p2p, country);
5705 }
5706
5707 if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_SSID_POSTFIX) {
5708 p2p_set_ssid_postfix(p2p, (u8 *) wpa_s->conf->p2p_ssid_postfix,
5709 wpa_s->conf->p2p_ssid_postfix ?
5710 os_strlen(wpa_s->conf->p2p_ssid_postfix) :
5711 0);
5712 }
5713
5714 if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_INTRA_BSS)
5715 p2p_set_intra_bss_dist(p2p, wpa_s->conf->p2p_intra_bss);
5716
5717 if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_LISTEN_CHANNEL) {
5718 u8 reg_class, channel;
5719 int ret;
5720 unsigned int r;
5721 if (wpa_s->conf->p2p_listen_reg_class &&
5722 wpa_s->conf->p2p_listen_channel) {
5723 reg_class = wpa_s->conf->p2p_listen_reg_class;
5724 channel = wpa_s->conf->p2p_listen_channel;
5725 } else {
5726 reg_class = 81;
5727 /*
5728 * Pick one of the social channels randomly as the
5729 * listen channel.
5730 */
5731 os_get_random((u8 *) &r, sizeof(r));
5732 channel = 1 + (r % 3) * 5;
5733 }
5734 ret = p2p_set_listen_channel(p2p, reg_class, channel);
5735 if (ret)
5736 wpa_printf(MSG_ERROR, "P2P: Own listen channel update "
5737 "failed: %d", ret);
5738 }
5739 if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_OPER_CHANNEL) {
5740 u8 op_reg_class, op_channel, cfg_op_channel;
5741 int ret = 0;
5742 unsigned int r;
5743 if (wpa_s->conf->p2p_oper_reg_class &&
5744 wpa_s->conf->p2p_oper_channel) {
5745 op_reg_class = wpa_s->conf->p2p_oper_reg_class;
5746 op_channel = wpa_s->conf->p2p_oper_channel;
5747 cfg_op_channel = 1;
5748 } else {
5749 op_reg_class = 81;
5750 /*
5751 * Use random operation channel from (1, 6, 11)
5752 *if no other preference is indicated.
5753 */
5754 os_get_random((u8 *) &r, sizeof(r));
5755 op_channel = 1 + (r % 3) * 5;
5756 cfg_op_channel = 0;
5757 }
5758 ret = p2p_set_oper_channel(p2p, op_reg_class, op_channel,
5759 cfg_op_channel);
5760 if (ret)
5761 wpa_printf(MSG_ERROR, "P2P: Own oper channel update "
5762 "failed: %d", ret);
5763 }
5764
5765 if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_PREF_CHAN) {
5766 if (p2p_set_pref_chan(p2p, wpa_s->conf->num_p2p_pref_chan,
5767 wpa_s->conf->p2p_pref_chan) < 0) {
5768 wpa_printf(MSG_ERROR, "P2P: Preferred channel list "
5769 "update failed");
5770 }
5771
5772 if (p2p_set_no_go_freq(p2p, &wpa_s->conf->p2p_no_go_freq) < 0) {
5773 wpa_printf(MSG_ERROR, "P2P: No GO channel list "
5774 "update failed");
5775 }
5776 }
5777 }
5778
5779
5780 int wpas_p2p_set_noa(struct wpa_supplicant *wpa_s, u8 count, int start,
5781 int duration)
5782 {
5783 if (!wpa_s->ap_iface)
5784 return -1;
5785 return hostapd_p2p_set_noa(wpa_s->ap_iface->bss[0], count, start,
5786 duration);
5787 }
5788
5789
5790 int wpas_p2p_set_cross_connect(struct wpa_supplicant *wpa_s, int enabled)
5791 {
5792 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
5793 return -1;
5794
5795 wpa_s->global->cross_connection = enabled;
5796 p2p_set_cross_connect(wpa_s->global->p2p, enabled);
5797
5798 if (!enabled) {
5799 struct wpa_supplicant *iface;
5800
5801 for (iface = wpa_s->global->ifaces; iface; iface = iface->next)
5802 {
5803 if (iface->cross_connect_enabled == 0)
5804 continue;
5805
5806 iface->cross_connect_enabled = 0;
5807 iface->cross_connect_in_use = 0;
5808 wpa_msg_global(iface->parent, MSG_INFO,
5809 P2P_EVENT_CROSS_CONNECT_DISABLE "%s %s",
5810 iface->ifname,
5811 iface->cross_connect_uplink);
5812 }
5813 }
5814
5815 return 0;
5816 }
5817
5818
5819 static void wpas_p2p_enable_cross_connect(struct wpa_supplicant *uplink)
5820 {
5821 struct wpa_supplicant *iface;
5822
5823 if (!uplink->global->cross_connection)
5824 return;
5825
5826 for (iface = uplink->global->ifaces; iface; iface = iface->next) {
5827 if (!iface->cross_connect_enabled)
5828 continue;
5829 if (os_strcmp(uplink->ifname, iface->cross_connect_uplink) !=
5830 0)
5831 continue;
5832 if (iface->ap_iface == NULL)
5833 continue;
5834 if (iface->cross_connect_in_use)
5835 continue;
5836
5837 iface->cross_connect_in_use = 1;
5838 wpa_msg_global(iface->parent, MSG_INFO,
5839 P2P_EVENT_CROSS_CONNECT_ENABLE "%s %s",
5840 iface->ifname, iface->cross_connect_uplink);
5841 }
5842 }
5843
5844
5845 static void wpas_p2p_disable_cross_connect(struct wpa_supplicant *uplink)
5846 {
5847 struct wpa_supplicant *iface;
5848
5849 for (iface = uplink->global->ifaces; iface; iface = iface->next) {
5850 if (!iface->cross_connect_enabled)
5851 continue;
5852 if (os_strcmp(uplink->ifname, iface->cross_connect_uplink) !=
5853 0)
5854 continue;
5855 if (!iface->cross_connect_in_use)
5856 continue;
5857
5858 wpa_msg_global(iface->parent, MSG_INFO,
5859 P2P_EVENT_CROSS_CONNECT_DISABLE "%s %s",
5860 iface->ifname, iface->cross_connect_uplink);
5861 iface->cross_connect_in_use = 0;
5862 }
5863 }
5864
5865
5866 void wpas_p2p_notif_connected(struct wpa_supplicant *wpa_s)
5867 {
5868 if (wpa_s->ap_iface || wpa_s->current_ssid == NULL ||
5869 wpa_s->current_ssid->mode != WPAS_MODE_INFRA ||
5870 wpa_s->cross_connect_disallowed)
5871 wpas_p2p_disable_cross_connect(wpa_s);
5872 else
5873 wpas_p2p_enable_cross_connect(wpa_s);
5874 if (!wpa_s->ap_iface &&
5875 eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL) > 0)
5876 wpa_printf(MSG_DEBUG, "P2P: Cancelled P2P group idle timeout");
5877 }
5878
5879
5880 void wpas_p2p_notif_disconnected(struct wpa_supplicant *wpa_s)
5881 {
5882 wpas_p2p_disable_cross_connect(wpa_s);
5883 if (!wpa_s->ap_iface &&
5884 !eloop_is_timeout_registered(wpas_p2p_group_idle_timeout,
5885 wpa_s, NULL))
5886 wpas_p2p_set_group_idle_timeout(wpa_s);
5887 }
5888
5889
5890 static void wpas_p2p_cross_connect_setup(struct wpa_supplicant *wpa_s)
5891 {
5892 struct wpa_supplicant *iface;
5893
5894 if (!wpa_s->global->cross_connection)
5895 return;
5896
5897 for (iface = wpa_s->global->ifaces; iface; iface = iface->next) {
5898 if (iface == wpa_s)
5899 continue;
5900 if (iface->drv_flags &
5901 WPA_DRIVER_FLAGS_P2P_DEDICATED_INTERFACE)
5902 continue;
5903 if (iface->drv_flags & WPA_DRIVER_FLAGS_P2P_CAPABLE)
5904 continue;
5905
5906 wpa_s->cross_connect_enabled = 1;
5907 os_strlcpy(wpa_s->cross_connect_uplink, iface->ifname,
5908 sizeof(wpa_s->cross_connect_uplink));
5909 wpa_printf(MSG_DEBUG, "P2P: Enable cross connection from "
5910 "%s to %s whenever uplink is available",
5911 wpa_s->ifname, wpa_s->cross_connect_uplink);
5912
5913 if (iface->ap_iface || iface->current_ssid == NULL ||
5914 iface->current_ssid->mode != WPAS_MODE_INFRA ||
5915 iface->cross_connect_disallowed ||
5916 iface->wpa_state != WPA_COMPLETED)
5917 break;
5918
5919 wpa_s->cross_connect_in_use = 1;
5920 wpa_msg_global(wpa_s->parent, MSG_INFO,
5921 P2P_EVENT_CROSS_CONNECT_ENABLE "%s %s",
5922 wpa_s->ifname, wpa_s->cross_connect_uplink);
5923 break;
5924 }
5925 }
5926
5927
5928 int wpas_p2p_notif_pbc_overlap(struct wpa_supplicant *wpa_s)
5929 {
5930 if (wpa_s->p2p_group_interface != P2P_GROUP_INTERFACE_CLIENT &&
5931 !wpa_s->p2p_in_provisioning)
5932 return 0; /* not P2P client operation */
5933
5934 wpa_printf(MSG_DEBUG, "P2P: Terminate connection due to WPS PBC "
5935 "session overlap");
5936 if (wpa_s != wpa_s->parent)
5937 wpa_msg_ctrl(wpa_s->parent, MSG_INFO, WPS_EVENT_OVERLAP);
5938 wpas_p2p_group_formation_failed(wpa_s);
5939 return 1;
5940 }
5941
5942
5943 void wpas_p2p_update_channel_list(struct wpa_supplicant *wpa_s)
5944 {
5945 struct p2p_channels chan, cli_chan;
5946
5947 if (wpa_s->global == NULL || wpa_s->global->p2p == NULL)
5948 return;
5949
5950 os_memset(&chan, 0, sizeof(chan));
5951 os_memset(&cli_chan, 0, sizeof(cli_chan));
5952 if (wpas_p2p_setup_channels(wpa_s, &chan, &cli_chan)) {
5953 wpa_printf(MSG_ERROR, "P2P: Failed to update supported "
5954 "channel list");
5955 return;
5956 }
5957
5958 p2p_update_channel_list(wpa_s->global->p2p, &chan, &cli_chan);
5959 }
5960
5961
5962 static void wpas_p2p_scan_res_ignore(struct wpa_supplicant *wpa_s,
5963 struct wpa_scan_results *scan_res)
5964 {
5965 wpa_printf(MSG_DEBUG, "P2P: Ignore scan results");
5966 }
5967
5968
5969 int wpas_p2p_cancel(struct wpa_supplicant *wpa_s)
5970 {
5971 struct wpa_global *global = wpa_s->global;
5972 int found = 0;
5973 const u8 *peer;
5974
5975 if (global->p2p == NULL)
5976 return -1;
5977
5978 wpa_printf(MSG_DEBUG, "P2P: Request to cancel group formation");
5979
5980 if (wpa_s->pending_interface_name[0] &&
5981 !is_zero_ether_addr(wpa_s->pending_interface_addr))
5982 found = 1;
5983
5984 peer = p2p_get_go_neg_peer(global->p2p);
5985 if (peer) {
5986 wpa_printf(MSG_DEBUG, "P2P: Unauthorize pending GO Neg peer "
5987 MACSTR, MAC2STR(peer));
5988 p2p_unauthorize(global->p2p, peer);
5989 found = 1;
5990 }
5991
5992 if (wpa_s->scan_res_handler == wpas_p2p_scan_res_join) {
5993 wpa_printf(MSG_DEBUG, "P2P: Stop pending scan for join");
5994 wpa_s->scan_res_handler = wpas_p2p_scan_res_ignore;
5995 found = 1;
5996 }
5997
5998 if (wpa_s->pending_pd_before_join) {
5999 wpa_printf(MSG_DEBUG, "P2P: Stop pending PD before join");
6000 wpa_s->pending_pd_before_join = 0;
6001 found = 1;
6002 }
6003
6004 wpas_p2p_stop_find(wpa_s);
6005
6006 for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
6007 if (wpa_s == global->p2p_group_formation &&
6008 (wpa_s->p2p_in_provisioning ||
6009 wpa_s->parent->pending_interface_type ==
6010 WPA_IF_P2P_CLIENT)) {
6011 wpa_printf(MSG_DEBUG, "P2P: Interface %s in group "
6012 "formation found - cancelling",
6013 wpa_s->ifname);
6014 found = 1;
6015 eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
6016 wpa_s->parent, NULL);
6017 if (wpa_s->p2p_in_provisioning) {
6018 wpas_group_formation_completed(wpa_s, 0);
6019 break;
6020 }
6021 wpas_p2p_group_delete(wpa_s,
6022 P2P_GROUP_REMOVAL_REQUESTED);
6023 break;
6024 }
6025 }
6026
6027 if (!found) {
6028 wpa_printf(MSG_DEBUG, "P2P: No ongoing group formation found");
6029 return -1;
6030 }
6031
6032 return 0;
6033 }
6034
6035
6036 void wpas_p2p_interface_unavailable(struct wpa_supplicant *wpa_s)
6037 {
6038 if (wpa_s->current_ssid == NULL || !wpa_s->current_ssid->p2p_group)
6039 return;
6040
6041 wpa_printf(MSG_DEBUG, "P2P: Remove group due to driver resource not "
6042 "being available anymore");
6043 wpas_p2p_group_delete(wpa_s, P2P_GROUP_REMOVAL_UNAVAILABLE);
6044 }
6045
6046
6047 void wpas_p2p_update_best_channels(struct wpa_supplicant *wpa_s,
6048 int freq_24, int freq_5, int freq_overall)
6049 {
6050 struct p2p_data *p2p = wpa_s->global->p2p;
6051 if (p2p == NULL)
6052 return;
6053 p2p_set_best_channels(p2p, freq_24, freq_5, freq_overall);
6054 }
6055
6056
6057 int wpas_p2p_unauthorize(struct wpa_supplicant *wpa_s, const char *addr)
6058 {
6059 u8 peer[ETH_ALEN];
6060 struct p2p_data *p2p = wpa_s->global->p2p;
6061
6062 if (p2p == NULL)
6063 return -1;
6064
6065 if (hwaddr_aton(addr, peer))
6066 return -1;
6067
6068 return p2p_unauthorize(p2p, peer);
6069 }
6070
6071
6072 /**
6073 * wpas_p2p_disconnect - Disconnect from a P2P Group
6074 * @wpa_s: Pointer to wpa_supplicant data
6075 * Returns: 0 on success, -1 on failure
6076 *
6077 * This can be used to disconnect from a group in which the local end is a P2P
6078 * Client or to end a P2P Group in case the local end is the Group Owner. If a
6079 * virtual network interface was created for this group, that interface will be
6080 * removed. Otherwise, only the configured P2P group network will be removed
6081 * from the interface.
6082 */
6083 int wpas_p2p_disconnect(struct wpa_supplicant *wpa_s)
6084 {
6085
6086 if (wpa_s == NULL)
6087 return -1;
6088
6089 return wpas_p2p_group_delete(wpa_s, P2P_GROUP_REMOVAL_REQUESTED) < 0 ?
6090 -1 : 0;
6091 }
6092
6093
6094 int wpas_p2p_in_progress(struct wpa_supplicant *wpa_s)
6095 {
6096 int ret;
6097
6098 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
6099 return 0;
6100
6101 ret = p2p_in_progress(wpa_s->global->p2p);
6102 if (ret == 0) {
6103 /*
6104 * Check whether there is an ongoing WPS provisioning step (or
6105 * other parts of group formation) on another interface since
6106 * p2p_in_progress() does not report this to avoid issues for
6107 * scans during such provisioning step.
6108 */
6109 if (wpa_s->global->p2p_group_formation &&
6110 wpa_s->global->p2p_group_formation != wpa_s) {
6111 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Another interface (%s) "
6112 "in group formation",
6113 wpa_s->global->p2p_group_formation->ifname);
6114 ret = 1;
6115 }
6116 }
6117
6118 if (!ret && wpa_s->global->p2p_go_wait_client.sec) {
6119 struct os_reltime now;
6120 os_get_reltime(&now);
6121 if (os_reltime_expired(&now, &wpa_s->global->p2p_go_wait_client,
6122 P2P_MAX_INITIAL_CONN_WAIT_GO)) {
6123 /* Wait for the first client has expired */
6124 wpa_s->global->p2p_go_wait_client.sec = 0;
6125 } else {
6126 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Waiting for initial client connection during group formation");
6127 ret = 1;
6128 }
6129 }
6130
6131 return ret;
6132 }
6133
6134
6135 void wpas_p2p_network_removed(struct wpa_supplicant *wpa_s,
6136 struct wpa_ssid *ssid)
6137 {
6138 if (wpa_s->p2p_in_provisioning && ssid->p2p_group &&
6139 eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
6140 wpa_s->parent, NULL) > 0) {
6141 /**
6142 * Remove the network by scheduling the group formation
6143 * timeout to happen immediately. The teardown code
6144 * needs to be scheduled to run asynch later so that we
6145 * don't delete data from under ourselves unexpectedly.
6146 * Calling wpas_p2p_group_formation_timeout directly
6147 * causes a series of crashes in WPS failure scenarios.
6148 */
6149 wpa_printf(MSG_DEBUG, "P2P: Canceled group formation due to "
6150 "P2P group network getting removed");
6151 eloop_register_timeout(0, 0, wpas_p2p_group_formation_timeout,
6152 wpa_s->parent, NULL);
6153 }
6154 }
6155
6156
6157 struct wpa_ssid * wpas_p2p_get_persistent(struct wpa_supplicant *wpa_s,
6158 const u8 *addr, const u8 *ssid,
6159 size_t ssid_len)
6160 {
6161 struct wpa_ssid *s;
6162 size_t i;
6163
6164 for (s = wpa_s->conf->ssid; s; s = s->next) {
6165 if (s->disabled != 2)
6166 continue;
6167 if (ssid &&
6168 (ssid_len != s->ssid_len ||
6169 os_memcmp(ssid, s->ssid, ssid_len) != 0))
6170 continue;
6171 if (addr == NULL) {
6172 if (s->mode == WPAS_MODE_P2P_GO)
6173 return s;
6174 continue;
6175 }
6176 if (os_memcmp(s->bssid, addr, ETH_ALEN) == 0)
6177 return s; /* peer is GO in the persistent group */
6178 if (s->mode != WPAS_MODE_P2P_GO || s->p2p_client_list == NULL)
6179 continue;
6180 for (i = 0; i < s->num_p2p_clients; i++) {
6181 if (os_memcmp(s->p2p_client_list + i * ETH_ALEN,
6182 addr, ETH_ALEN) == 0)
6183 return s; /* peer is P2P client in persistent
6184 * group */
6185 }
6186 }
6187
6188 return NULL;
6189 }
6190
6191
6192 void wpas_p2p_notify_ap_sta_authorized(struct wpa_supplicant *wpa_s,
6193 const u8 *addr)
6194 {
6195 if (eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
6196 wpa_s->parent, NULL) > 0) {
6197 /*
6198 * This can happen if WPS provisioning step is not terminated
6199 * cleanly (e.g., P2P Client does not send WSC_Done). Since the
6200 * peer was able to connect, there is no need to time out group
6201 * formation after this, though. In addition, this is used with
6202 * the initial connection wait on the GO as a separate formation
6203 * timeout and as such, expected to be hit after the initial WPS
6204 * provisioning step.
6205 */
6206 wpa_printf(MSG_DEBUG, "P2P: Canceled P2P group formation timeout on data connection");
6207 }
6208 if (!wpa_s->p2p_go_group_formation_completed) {
6209 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Marking group formation completed on GO on first data connection");
6210 wpa_s->p2p_go_group_formation_completed = 1;
6211 wpa_s->global->p2p_group_formation = NULL;
6212 wpa_s->p2p_in_provisioning = 0;
6213 }
6214 wpa_s->global->p2p_go_wait_client.sec = 0;
6215 if (addr == NULL)
6216 return;
6217 wpas_p2p_add_persistent_group_client(wpa_s, addr);
6218 }
6219
6220
6221 static void wpas_p2p_fallback_to_go_neg(struct wpa_supplicant *wpa_s,
6222 int group_added)
6223 {
6224 struct wpa_supplicant *group = wpa_s;
6225 if (wpa_s->global->p2p_group_formation)
6226 group = wpa_s->global->p2p_group_formation;
6227 wpa_s = wpa_s->parent;
6228 offchannel_send_action_done(wpa_s);
6229 if (group_added)
6230 wpas_p2p_group_delete(group, P2P_GROUP_REMOVAL_SILENT);
6231 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Fall back to GO Negotiation");
6232 wpas_p2p_connect(wpa_s, wpa_s->pending_join_dev_addr, wpa_s->p2p_pin,
6233 wpa_s->p2p_wps_method, wpa_s->p2p_persistent_group, 0,
6234 0, 0, wpa_s->p2p_go_intent, wpa_s->p2p_connect_freq,
6235 wpa_s->p2p_persistent_id,
6236 wpa_s->p2p_pd_before_go_neg,
6237 wpa_s->p2p_go_ht40,
6238 wpa_s->p2p_go_vht);
6239 }
6240
6241
6242 int wpas_p2p_scan_no_go_seen(struct wpa_supplicant *wpa_s)
6243 {
6244 if (!wpa_s->p2p_fallback_to_go_neg ||
6245 wpa_s->p2p_in_provisioning <= 5)
6246 return 0;
6247
6248 if (wpas_p2p_peer_go(wpa_s, wpa_s->pending_join_dev_addr) > 0)
6249 return 0; /* peer operating as a GO */
6250
6251 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: GO not found for p2p_connect-auto - "
6252 "fallback to GO Negotiation");
6253 wpas_p2p_fallback_to_go_neg(wpa_s, 1);
6254
6255 return 1;
6256 }
6257
6258
6259 unsigned int wpas_p2p_search_delay(struct wpa_supplicant *wpa_s)
6260 {
6261 struct wpa_supplicant *ifs;
6262
6263 if (wpa_s->wpa_state > WPA_SCANNING) {
6264 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Use %u ms search delay due to "
6265 "concurrent operation",
6266 P2P_CONCURRENT_SEARCH_DELAY);
6267 return P2P_CONCURRENT_SEARCH_DELAY;
6268 }
6269
6270 dl_list_for_each(ifs, &wpa_s->radio->ifaces, struct wpa_supplicant,
6271 radio_list) {
6272 if (ifs != wpa_s && ifs->wpa_state > WPA_SCANNING) {
6273 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Use %u ms search "
6274 "delay due to concurrent operation on "
6275 "interface %s",
6276 P2P_CONCURRENT_SEARCH_DELAY, ifs->ifname);
6277 return P2P_CONCURRENT_SEARCH_DELAY;
6278 }
6279 }
6280
6281 return 0;
6282 }
6283
6284
6285 static int wpas_p2p_remove_psk_entry(struct wpa_supplicant *wpa_s,
6286 struct wpa_ssid *s, const u8 *addr,
6287 int iface_addr)
6288 {
6289 struct psk_list_entry *psk, *tmp;
6290 int changed = 0;
6291
6292 dl_list_for_each_safe(psk, tmp, &s->psk_list, struct psk_list_entry,
6293 list) {
6294 if ((iface_addr && !psk->p2p &&
6295 os_memcmp(addr, psk->addr, ETH_ALEN) == 0) ||
6296 (!iface_addr && psk->p2p &&
6297 os_memcmp(addr, psk->addr, ETH_ALEN) == 0)) {
6298 wpa_dbg(wpa_s, MSG_DEBUG,
6299 "P2P: Remove persistent group PSK list entry for "
6300 MACSTR " p2p=%u",
6301 MAC2STR(psk->addr), psk->p2p);
6302 dl_list_del(&psk->list);
6303 os_free(psk);
6304 changed++;
6305 }
6306 }
6307
6308 return changed;
6309 }
6310
6311
6312 void wpas_p2p_new_psk_cb(struct wpa_supplicant *wpa_s, const u8 *mac_addr,
6313 const u8 *p2p_dev_addr,
6314 const u8 *psk, size_t psk_len)
6315 {
6316 struct wpa_ssid *ssid = wpa_s->current_ssid;
6317 struct wpa_ssid *persistent;
6318 struct psk_list_entry *p;
6319
6320 if (psk_len != sizeof(p->psk))
6321 return;
6322
6323 if (p2p_dev_addr) {
6324 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: New PSK for addr=" MACSTR
6325 " p2p_dev_addr=" MACSTR,
6326 MAC2STR(mac_addr), MAC2STR(p2p_dev_addr));
6327 if (is_zero_ether_addr(p2p_dev_addr))
6328 p2p_dev_addr = NULL;
6329 } else {
6330 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: New PSK for addr=" MACSTR,
6331 MAC2STR(mac_addr));
6332 }
6333
6334 if (ssid->mode == WPAS_MODE_P2P_GROUP_FORMATION) {
6335 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: new_psk_cb during group formation");
6336 /* To be added to persistent group once created */
6337 if (wpa_s->global->add_psk == NULL) {
6338 wpa_s->global->add_psk = os_zalloc(sizeof(*p));
6339 if (wpa_s->global->add_psk == NULL)
6340 return;
6341 }
6342 p = wpa_s->global->add_psk;
6343 if (p2p_dev_addr) {
6344 p->p2p = 1;
6345 os_memcpy(p->addr, p2p_dev_addr, ETH_ALEN);
6346 } else {
6347 p->p2p = 0;
6348 os_memcpy(p->addr, mac_addr, ETH_ALEN);
6349 }
6350 os_memcpy(p->psk, psk, psk_len);
6351 return;
6352 }
6353
6354 if (ssid->mode != WPAS_MODE_P2P_GO || !ssid->p2p_persistent_group) {
6355 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Ignore new_psk_cb on not-persistent GO");
6356 return;
6357 }
6358
6359 persistent = wpas_p2p_get_persistent(wpa_s->parent, NULL, ssid->ssid,
6360 ssid->ssid_len);
6361 if (!persistent) {
6362 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Could not find persistent group information to store the new PSK");
6363 return;
6364 }
6365
6366 p = os_zalloc(sizeof(*p));
6367 if (p == NULL)
6368 return;
6369 if (p2p_dev_addr) {
6370 p->p2p = 1;
6371 os_memcpy(p->addr, p2p_dev_addr, ETH_ALEN);
6372 } else {
6373 p->p2p = 0;
6374 os_memcpy(p->addr, mac_addr, ETH_ALEN);
6375 }
6376 os_memcpy(p->psk, psk, psk_len);
6377
6378 if (dl_list_len(&persistent->psk_list) > P2P_MAX_STORED_CLIENTS) {
6379 struct psk_list_entry *last;
6380 last = dl_list_last(&persistent->psk_list,
6381 struct psk_list_entry, list);
6382 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Remove oldest PSK entry for "
6383 MACSTR " (p2p=%u) to make room for a new one",
6384 MAC2STR(last->addr), last->p2p);
6385 dl_list_del(&last->list);
6386 os_free(last);
6387 }
6388
6389 wpas_p2p_remove_psk_entry(wpa_s->parent, persistent,
6390 p2p_dev_addr ? p2p_dev_addr : mac_addr,
6391 p2p_dev_addr == NULL);
6392 if (p2p_dev_addr) {
6393 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Add new PSK for p2p_dev_addr="
6394 MACSTR, MAC2STR(p2p_dev_addr));
6395 } else {
6396 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Add new PSK for addr=" MACSTR,
6397 MAC2STR(mac_addr));
6398 }
6399 dl_list_add(&persistent->psk_list, &p->list);
6400
6401 #ifndef CONFIG_NO_CONFIG_WRITE
6402 if (wpa_s->parent->conf->update_config &&
6403 wpa_config_write(wpa_s->parent->confname, wpa_s->parent->conf))
6404 wpa_printf(MSG_DEBUG, "P2P: Failed to update configuration");
6405 #endif /* CONFIG_NO_CONFIG_WRITE */
6406 }
6407
6408
6409 static void wpas_p2p_remove_psk(struct wpa_supplicant *wpa_s,
6410 struct wpa_ssid *s, const u8 *addr,
6411 int iface_addr)
6412 {
6413 int res;
6414
6415 res = wpas_p2p_remove_psk_entry(wpa_s, s, addr, iface_addr);
6416 if (res > 0) {
6417 #ifndef CONFIG_NO_CONFIG_WRITE
6418 if (wpa_s->conf->update_config &&
6419 wpa_config_write(wpa_s->confname, wpa_s->conf))
6420 wpa_dbg(wpa_s, MSG_DEBUG,
6421 "P2P: Failed to update configuration");
6422 #endif /* CONFIG_NO_CONFIG_WRITE */
6423 }
6424 }
6425
6426
6427 static void wpas_p2p_remove_client_go(struct wpa_supplicant *wpa_s,
6428 const u8 *peer, int iface_addr)
6429 {
6430 struct hostapd_data *hapd;
6431 struct hostapd_wpa_psk *psk, *prev, *rem;
6432 struct sta_info *sta;
6433
6434 if (wpa_s->ap_iface == NULL || wpa_s->current_ssid == NULL ||
6435 wpa_s->current_ssid->mode != WPAS_MODE_P2P_GO)
6436 return;
6437
6438 /* Remove per-station PSK entry */
6439 hapd = wpa_s->ap_iface->bss[0];
6440 prev = NULL;
6441 psk = hapd->conf->ssid.wpa_psk;
6442 while (psk) {
6443 if ((iface_addr && os_memcmp(peer, psk->addr, ETH_ALEN) == 0) ||
6444 (!iface_addr &&
6445 os_memcmp(peer, psk->p2p_dev_addr, ETH_ALEN) == 0)) {
6446 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Remove operating group PSK entry for "
6447 MACSTR " iface_addr=%d",
6448 MAC2STR(peer), iface_addr);
6449 if (prev)
6450 prev->next = psk->next;
6451 else
6452 hapd->conf->ssid.wpa_psk = psk->next;
6453 rem = psk;
6454 psk = psk->next;
6455 os_free(rem);
6456 } else {
6457 prev = psk;
6458 psk = psk->next;
6459 }
6460 }
6461
6462 /* Disconnect from group */
6463 if (iface_addr)
6464 sta = ap_get_sta(hapd, peer);
6465 else
6466 sta = ap_get_sta_p2p(hapd, peer);
6467 if (sta) {
6468 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Disconnect peer " MACSTR
6469 " (iface_addr=%d) from group",
6470 MAC2STR(peer), iface_addr);
6471 hostapd_drv_sta_deauth(hapd, sta->addr,
6472 WLAN_REASON_DEAUTH_LEAVING);
6473 ap_sta_deauthenticate(hapd, sta, WLAN_REASON_DEAUTH_LEAVING);
6474 }
6475 }
6476
6477
6478 void wpas_p2p_remove_client(struct wpa_supplicant *wpa_s, const u8 *peer,
6479 int iface_addr)
6480 {
6481 struct wpa_ssid *s;
6482 struct wpa_supplicant *w;
6483
6484 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Remove client " MACSTR, MAC2STR(peer));
6485
6486 /* Remove from any persistent group */
6487 for (s = wpa_s->parent->conf->ssid; s; s = s->next) {
6488 if (s->disabled != 2 || s->mode != WPAS_MODE_P2P_GO)
6489 continue;
6490 if (!iface_addr)
6491 wpas_remove_persistent_peer(wpa_s, s, peer, 0);
6492 wpas_p2p_remove_psk(wpa_s->parent, s, peer, iface_addr);
6493 }
6494
6495 /* Remove from any operating group */
6496 for (w = wpa_s->global->ifaces; w; w = w->next)
6497 wpas_p2p_remove_client_go(w, peer, iface_addr);
6498 }
6499
6500
6501 static void wpas_p2p_psk_failure_removal(void *eloop_ctx, void *timeout_ctx)
6502 {
6503 struct wpa_supplicant *wpa_s = eloop_ctx;
6504 wpas_p2p_group_delete(wpa_s, P2P_GROUP_REMOVAL_PSK_FAILURE);
6505 }
6506
6507
6508 static void wpas_p2p_group_freq_conflict(void *eloop_ctx, void *timeout_ctx)
6509 {
6510 struct wpa_supplicant *wpa_s = eloop_ctx;
6511
6512 wpa_printf(MSG_DEBUG, "P2P: Frequency conflict - terminate group");
6513 wpas_p2p_group_delete(wpa_s, P2P_GROUP_REMOVAL_FREQ_CONFLICT);
6514 }
6515
6516
6517 int wpas_p2p_handle_frequency_conflicts(struct wpa_supplicant *wpa_s, int freq,
6518 struct wpa_ssid *ssid)
6519 {
6520 struct wpa_supplicant *iface;
6521
6522 for (iface = wpa_s->global->ifaces; iface; iface = iface->next) {
6523 if (!iface->current_ssid ||
6524 iface->current_ssid->frequency == freq ||
6525 (iface->p2p_group_interface == NOT_P2P_GROUP_INTERFACE &&
6526 !iface->current_ssid->p2p_group))
6527 continue;
6528
6529 /* Remove the connection with least priority */
6530 if (!wpas_is_p2p_prioritized(iface)) {
6531 /* STA connection has priority over existing
6532 * P2P connection, so remove the interface. */
6533 wpa_printf(MSG_DEBUG, "P2P: Removing P2P connection due to single channel concurrent mode frequency conflict");
6534 eloop_register_timeout(0, 0,
6535 wpas_p2p_group_freq_conflict,
6536 iface, NULL);
6537 /* If connection in progress is P2P connection, do not
6538 * proceed for the connection. */
6539 if (wpa_s == iface)
6540 return -1;
6541 else
6542 return 0;
6543 } else {
6544 /* P2P connection has priority, disable the STA network
6545 */
6546 wpa_supplicant_disable_network(wpa_s->global->ifaces,
6547 ssid);
6548 wpa_msg(wpa_s->global->ifaces, MSG_INFO,
6549 WPA_EVENT_FREQ_CONFLICT " id=%d", ssid->id);
6550 os_memset(wpa_s->global->ifaces->pending_bssid, 0,
6551 ETH_ALEN);
6552 /* If P2P connection is in progress, continue
6553 * connecting...*/
6554 if (wpa_s == iface)
6555 return 0;
6556 else
6557 return -1;
6558 }
6559 }
6560
6561 return 0;
6562 }
6563
6564
6565 int wpas_p2p_4way_hs_failed(struct wpa_supplicant *wpa_s)
6566 {
6567 struct wpa_ssid *ssid = wpa_s->current_ssid;
6568
6569 if (ssid == NULL || !ssid->p2p_group)
6570 return 0;
6571
6572 if (wpa_s->p2p_last_4way_hs_fail &&
6573 wpa_s->p2p_last_4way_hs_fail == ssid) {
6574 u8 go_dev_addr[ETH_ALEN];
6575 struct wpa_ssid *persistent;
6576
6577 if (wpas_p2p_persistent_group(wpa_s, go_dev_addr,
6578 ssid->ssid,
6579 ssid->ssid_len) <= 0) {
6580 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Could not determine whether 4-way handshake failures were for a persistent group");
6581 goto disconnect;
6582 }
6583
6584 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Two 4-way handshake failures for a P2P group - go_dev_addr="
6585 MACSTR, MAC2STR(go_dev_addr));
6586 persistent = wpas_p2p_get_persistent(wpa_s->parent, go_dev_addr,
6587 ssid->ssid,
6588 ssid->ssid_len);
6589 if (persistent == NULL || persistent->mode != WPAS_MODE_INFRA) {
6590 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: No matching persistent group stored");
6591 goto disconnect;
6592 }
6593 wpa_msg_global(wpa_s->parent, MSG_INFO,
6594 P2P_EVENT_PERSISTENT_PSK_FAIL "%d",
6595 persistent->id);
6596 disconnect:
6597 wpa_s->p2p_last_4way_hs_fail = NULL;
6598 /*
6599 * Remove the group from a timeout to avoid issues with caller
6600 * continuing to use the interface if this is on a P2P group
6601 * interface.
6602 */
6603 eloop_register_timeout(0, 0, wpas_p2p_psk_failure_removal,
6604 wpa_s, NULL);
6605 return 1;
6606 }
6607
6608 wpa_s->p2p_last_4way_hs_fail = ssid;
6609 return 0;
6610 }