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