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