]> git.ipfire.org Git - thirdparty/hostap.git/blob - wpa_supplicant/p2p_supplicant.c
P2P: Use 500 ms search delay by default during concurrent operations
[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/p2p_hostapd.h"
21 #include "eapol_supp/eapol_supp_sm.h"
22 #include "rsn_supp/wpa.h"
23 #include "wpa_supplicant_i.h"
24 #include "driver_i.h"
25 #include "ap.h"
26 #include "config_ssid.h"
27 #include "config.h"
28 #include "notify.h"
29 #include "scan.h"
30 #include "bss.h"
31 #include "offchannel.h"
32 #include "wps_supplicant.h"
33 #include "p2p_supplicant.h"
34
35
36 /*
37 * How many times to try to scan to find the GO before giving up on join
38 * request.
39 */
40 #define P2P_MAX_JOIN_SCAN_ATTEMPTS 10
41
42 #define P2P_AUTO_PD_SCAN_ATTEMPTS 5
43
44 #ifndef P2P_MAX_CLIENT_IDLE
45 /*
46 * How many seconds to try to reconnect to the GO when connection in P2P client
47 * role has been lost.
48 */
49 #define P2P_MAX_CLIENT_IDLE 10
50 #endif /* P2P_MAX_CLIENT_IDLE */
51
52 #ifndef P2P_MAX_INITIAL_CONN_WAIT
53 /*
54 * How many seconds to wait for initial 4-way handshake to get completed after
55 * WPS provisioning step.
56 */
57 #define P2P_MAX_INITIAL_CONN_WAIT 10
58 #endif /* P2P_MAX_INITIAL_CONN_WAIT */
59
60 #ifndef P2P_CONCURRENT_SEARCH_DELAY
61 #define P2P_CONCURRENT_SEARCH_DELAY 500
62 #endif /* P2P_CONCURRENT_SEARCH_DELAY */
63
64 enum p2p_group_removal_reason {
65 P2P_GROUP_REMOVAL_UNKNOWN,
66 P2P_GROUP_REMOVAL_SILENT,
67 P2P_GROUP_REMOVAL_FORMATION_FAILED,
68 P2P_GROUP_REMOVAL_REQUESTED,
69 P2P_GROUP_REMOVAL_IDLE_TIMEOUT,
70 P2P_GROUP_REMOVAL_UNAVAILABLE,
71 P2P_GROUP_REMOVAL_GO_ENDING_SESSION
72 };
73
74
75 static void wpas_p2p_long_listen_timeout(void *eloop_ctx, void *timeout_ctx);
76 static struct wpa_supplicant *
77 wpas_p2p_get_group_iface(struct wpa_supplicant *wpa_s, int addr_allocated,
78 int go);
79 static int wpas_p2p_join_start(struct wpa_supplicant *wpa_s);
80 static void wpas_p2p_join_scan_req(struct wpa_supplicant *wpa_s, int freq);
81 static void wpas_p2p_join_scan(void *eloop_ctx, void *timeout_ctx);
82 static int wpas_p2p_join(struct wpa_supplicant *wpa_s, const u8 *iface_addr,
83 const u8 *dev_addr, enum p2p_wps_method wps_method,
84 int auto_join);
85 static void wpas_p2p_pd_before_join_timeout(void *eloop_ctx,
86 void *timeout_ctx);
87 static int wpas_p2p_create_iface(struct wpa_supplicant *wpa_s);
88 static void wpas_p2p_cross_connect_setup(struct wpa_supplicant *wpa_s);
89 static void wpas_p2p_group_idle_timeout(void *eloop_ctx, void *timeout_ctx);
90 static void wpas_p2p_set_group_idle_timeout(struct wpa_supplicant *wpa_s);
91 static void wpas_p2p_fallback_to_go_neg(struct wpa_supplicant *wpa_s,
92 int group_added);
93
94
95 static void wpas_p2p_scan_res_handler(struct wpa_supplicant *wpa_s,
96 struct wpa_scan_results *scan_res)
97 {
98 size_t i;
99
100 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
101 return;
102
103 wpa_printf(MSG_DEBUG, "P2P: Scan results received (%d BSS)",
104 (int) scan_res->num);
105
106 for (i = 0; i < scan_res->num; i++) {
107 struct wpa_scan_res *bss = scan_res->res[i];
108 if (p2p_scan_res_handler(wpa_s->global->p2p, bss->bssid,
109 bss->freq, bss->level,
110 (const u8 *) (bss + 1),
111 bss->ie_len) > 0)
112 break;
113 }
114
115 p2p_scan_res_handled(wpa_s->global->p2p);
116 }
117
118
119 static int wpas_p2p_scan(void *ctx, enum p2p_scan_type type, int freq,
120 unsigned int num_req_dev_types,
121 const u8 *req_dev_types, const u8 *dev_id, u16 pw_id)
122 {
123 struct wpa_supplicant *wpa_s = ctx;
124 struct wpa_driver_scan_params params;
125 int ret;
126 struct wpabuf *wps_ie, *ies;
127 int social_channels[] = { 2412, 2437, 2462, 0, 0 };
128 size_t ielen;
129
130 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
131 return -1;
132
133 os_memset(&params, 0, sizeof(params));
134
135 /* P2P Wildcard SSID */
136 params.num_ssids = 1;
137 params.ssids[0].ssid = (u8 *) P2P_WILDCARD_SSID;
138 params.ssids[0].ssid_len = P2P_WILDCARD_SSID_LEN;
139
140 wpa_s->wps->dev.p2p = 1;
141 wps_ie = wps_build_probe_req_ie(pw_id, &wpa_s->wps->dev,
142 wpa_s->wps->uuid, WPS_REQ_ENROLLEE,
143 num_req_dev_types, req_dev_types);
144 if (wps_ie == NULL)
145 return -1;
146
147 ielen = p2p_scan_ie_buf_len(wpa_s->global->p2p);
148 ies = wpabuf_alloc(wpabuf_len(wps_ie) + ielen);
149 if (ies == NULL) {
150 wpabuf_free(wps_ie);
151 return -1;
152 }
153 wpabuf_put_buf(ies, wps_ie);
154 wpabuf_free(wps_ie);
155
156 p2p_scan_ie(wpa_s->global->p2p, ies, dev_id);
157
158 params.p2p_probe = 1;
159 params.extra_ies = wpabuf_head(ies);
160 params.extra_ies_len = wpabuf_len(ies);
161
162 switch (type) {
163 case P2P_SCAN_SOCIAL:
164 params.freqs = social_channels;
165 break;
166 case P2P_SCAN_FULL:
167 break;
168 case P2P_SCAN_SOCIAL_PLUS_ONE:
169 social_channels[3] = freq;
170 params.freqs = social_channels;
171 break;
172 }
173
174 ret = wpa_drv_scan(wpa_s, &params);
175
176 wpabuf_free(ies);
177
178 if (ret) {
179 if (wpa_s->scanning ||
180 wpa_s->scan_res_handler == wpas_p2p_scan_res_handler) {
181 wpa_s->p2p_cb_on_scan_complete = 1;
182 ret = 1;
183 }
184 } else
185 wpa_s->scan_res_handler = wpas_p2p_scan_res_handler;
186
187 return ret;
188 }
189
190
191 static enum wpa_driver_if_type wpas_p2p_if_type(int p2p_group_interface)
192 {
193 switch (p2p_group_interface) {
194 case P2P_GROUP_INTERFACE_PENDING:
195 return WPA_IF_P2P_GROUP;
196 case P2P_GROUP_INTERFACE_GO:
197 return WPA_IF_P2P_GO;
198 case P2P_GROUP_INTERFACE_CLIENT:
199 return WPA_IF_P2P_CLIENT;
200 }
201
202 return WPA_IF_P2P_GROUP;
203 }
204
205
206 static struct wpa_supplicant * wpas_get_p2p_group(struct wpa_supplicant *wpa_s,
207 const u8 *ssid,
208 size_t ssid_len, int *go)
209 {
210 struct wpa_ssid *s;
211
212 for (wpa_s = wpa_s->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
213 for (s = wpa_s->conf->ssid; s; s = s->next) {
214 if (s->disabled != 0 || !s->p2p_group ||
215 s->ssid_len != ssid_len ||
216 os_memcmp(ssid, s->ssid, ssid_len) != 0)
217 continue;
218 if (s->mode == WPAS_MODE_P2P_GO &&
219 s != wpa_s->current_ssid)
220 continue;
221 if (go)
222 *go = s->mode == WPAS_MODE_P2P_GO;
223 return wpa_s;
224 }
225 }
226
227 return NULL;
228 }
229
230
231 static int wpas_p2p_group_delete(struct wpa_supplicant *wpa_s,
232 enum p2p_group_removal_reason removal_reason)
233 {
234 struct wpa_ssid *ssid;
235 char *gtype;
236 const char *reason;
237
238 ssid = wpa_s->current_ssid;
239 if (ssid == NULL) {
240 /*
241 * The current SSID was not known, but there may still be a
242 * pending P2P group interface waiting for provisioning.
243 */
244 ssid = wpa_s->conf->ssid;
245 while (ssid) {
246 if (ssid->p2p_group &&
247 (ssid->mode == WPAS_MODE_P2P_GROUP_FORMATION ||
248 (ssid->key_mgmt & WPA_KEY_MGMT_WPS)))
249 break;
250 ssid = ssid->next;
251 }
252 if (ssid == NULL) {
253 wpa_printf(MSG_ERROR, "P2P: P2P group interface "
254 "not found");
255 return -1;
256 }
257 }
258 if (wpa_s->p2p_group_interface == P2P_GROUP_INTERFACE_GO)
259 gtype = "GO";
260 else if (wpa_s->p2p_group_interface == P2P_GROUP_INTERFACE_CLIENT ||
261 (ssid && ssid->mode == WPAS_MODE_INFRA)) {
262 wpa_s->reassociate = 0;
263 wpa_s->disconnected = 1;
264 wpa_supplicant_deauthenticate(wpa_s,
265 WLAN_REASON_DEAUTH_LEAVING);
266 gtype = "client";
267 } else
268 gtype = "GO";
269 if (wpa_s->cross_connect_in_use) {
270 wpa_s->cross_connect_in_use = 0;
271 wpa_msg(wpa_s->parent, MSG_INFO,
272 P2P_EVENT_CROSS_CONNECT_DISABLE "%s %s",
273 wpa_s->ifname, wpa_s->cross_connect_uplink);
274 }
275 switch (removal_reason) {
276 case P2P_GROUP_REMOVAL_REQUESTED:
277 reason = " reason=REQUESTED";
278 break;
279 case P2P_GROUP_REMOVAL_FORMATION_FAILED:
280 reason = " reason=FORMATION_FAILED";
281 break;
282 case P2P_GROUP_REMOVAL_IDLE_TIMEOUT:
283 reason = " reason=IDLE";
284 break;
285 case P2P_GROUP_REMOVAL_UNAVAILABLE:
286 reason = " reason=UNAVAILABLE";
287 break;
288 case P2P_GROUP_REMOVAL_GO_ENDING_SESSION:
289 reason = " reason=GO_ENDING_SESSION";
290 break;
291 default:
292 reason = "";
293 break;
294 }
295 if (removal_reason != P2P_GROUP_REMOVAL_SILENT) {
296 wpa_msg(wpa_s->parent, MSG_INFO,
297 P2P_EVENT_GROUP_REMOVED "%s %s%s",
298 wpa_s->ifname, gtype, reason);
299 }
300
301 if (eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL) > 0)
302 wpa_printf(MSG_DEBUG, "P2P: Cancelled P2P group idle timeout");
303
304 if (removal_reason != P2P_GROUP_REMOVAL_SILENT && ssid)
305 wpas_notify_p2p_group_removed(wpa_s, ssid, gtype);
306
307 if (wpa_s->p2p_group_interface != NOT_P2P_GROUP_INTERFACE) {
308 struct wpa_global *global;
309 char *ifname;
310 enum wpa_driver_if_type type;
311 wpa_printf(MSG_DEBUG, "P2P: Remove group interface %s",
312 wpa_s->ifname);
313 global = wpa_s->global;
314 ifname = os_strdup(wpa_s->ifname);
315 type = wpas_p2p_if_type(wpa_s->p2p_group_interface);
316 wpa_supplicant_remove_iface(wpa_s->global, wpa_s, 0);
317 wpa_s = global->ifaces;
318 if (wpa_s && ifname)
319 wpa_drv_if_remove(wpa_s, type, ifname);
320 os_free(ifname);
321 return 0;
322 }
323
324 wpa_printf(MSG_DEBUG, "P2P: Remove temporary group network");
325 if (ssid && (ssid->p2p_group ||
326 ssid->mode == WPAS_MODE_P2P_GROUP_FORMATION ||
327 (ssid->key_mgmt & WPA_KEY_MGMT_WPS))) {
328 int id = ssid->id;
329 if (ssid == wpa_s->current_ssid) {
330 wpa_sm_set_config(wpa_s->wpa, NULL);
331 eapol_sm_notify_config(wpa_s->eapol, NULL, NULL);
332 wpa_s->current_ssid = NULL;
333 }
334 /*
335 * Networks objects created during any P2P activities are not
336 * exposed out as they might/will confuse certain non-P2P aware
337 * applications since these network objects won't behave like
338 * regular ones.
339 *
340 * Likewise, we don't send out network removed signals for such
341 * network objects.
342 */
343 wpa_config_remove_network(wpa_s->conf, id);
344 wpa_supplicant_clear_status(wpa_s);
345 wpa_supplicant_cancel_sched_scan(wpa_s);
346 } else {
347 wpa_printf(MSG_DEBUG, "P2P: Temporary group network not "
348 "found");
349 }
350 if (wpa_s->ap_iface)
351 wpa_supplicant_ap_deinit(wpa_s);
352 else
353 wpa_drv_deinit_p2p_cli(wpa_s);
354
355 return 0;
356 }
357
358
359 static int wpas_p2p_persistent_group(struct wpa_supplicant *wpa_s,
360 u8 *go_dev_addr,
361 const u8 *ssid, size_t ssid_len)
362 {
363 struct wpa_bss *bss;
364 const u8 *bssid;
365 struct wpabuf *p2p;
366 u8 group_capab;
367 const u8 *addr;
368
369 if (wpa_s->go_params)
370 bssid = wpa_s->go_params->peer_interface_addr;
371 else
372 bssid = wpa_s->bssid;
373
374 bss = wpa_bss_get(wpa_s, bssid, ssid, ssid_len);
375 if (bss == NULL) {
376 u8 iface_addr[ETH_ALEN];
377 if (p2p_get_interface_addr(wpa_s->global->p2p, bssid,
378 iface_addr) == 0)
379 bss = wpa_bss_get(wpa_s, iface_addr, ssid, ssid_len);
380 }
381 if (bss == NULL) {
382 wpa_printf(MSG_DEBUG, "P2P: Could not figure out whether "
383 "group is persistent - BSS " MACSTR " not found",
384 MAC2STR(bssid));
385 return 0;
386 }
387
388 p2p = wpa_bss_get_vendor_ie_multi(bss, P2P_IE_VENDOR_TYPE);
389 if (p2p == NULL) {
390 wpa_printf(MSG_DEBUG, "P2P: Could not figure out whether "
391 "group is persistent - BSS " MACSTR
392 " did not include P2P IE", MAC2STR(bssid));
393 wpa_hexdump(MSG_DEBUG, "P2P: Probe Response IEs",
394 (u8 *) (bss + 1), bss->ie_len);
395 wpa_hexdump(MSG_DEBUG, "P2P: Beacon IEs",
396 ((u8 *) bss + 1) + bss->ie_len,
397 bss->beacon_ie_len);
398 return 0;
399 }
400
401 group_capab = p2p_get_group_capab(p2p);
402 addr = p2p_get_go_dev_addr(p2p);
403 wpa_printf(MSG_DEBUG, "P2P: Checking whether group is persistent: "
404 "group_capab=0x%x", group_capab);
405 if (addr) {
406 os_memcpy(go_dev_addr, addr, ETH_ALEN);
407 wpa_printf(MSG_DEBUG, "P2P: GO Device Address " MACSTR,
408 MAC2STR(addr));
409 } else
410 os_memset(go_dev_addr, 0, ETH_ALEN);
411 wpabuf_free(p2p);
412
413 wpa_printf(MSG_DEBUG, "P2P: BSS " MACSTR " group_capab=0x%x "
414 "go_dev_addr=" MACSTR,
415 MAC2STR(bssid), group_capab, MAC2STR(go_dev_addr));
416
417 return group_capab & P2P_GROUP_CAPAB_PERSISTENT_GROUP;
418 }
419
420
421 static int wpas_p2p_store_persistent_group(struct wpa_supplicant *wpa_s,
422 struct wpa_ssid *ssid,
423 const u8 *go_dev_addr)
424 {
425 struct wpa_ssid *s;
426 int changed = 0;
427
428 wpa_printf(MSG_DEBUG, "P2P: Storing credentials for a persistent "
429 "group (GO Dev Addr " MACSTR ")", MAC2STR(go_dev_addr));
430 for (s = wpa_s->conf->ssid; s; s = s->next) {
431 if (s->disabled == 2 &&
432 os_memcmp(go_dev_addr, s->bssid, ETH_ALEN) == 0 &&
433 s->ssid_len == ssid->ssid_len &&
434 os_memcmp(ssid->ssid, s->ssid, ssid->ssid_len) == 0)
435 break;
436 }
437
438 if (s) {
439 wpa_printf(MSG_DEBUG, "P2P: Update existing persistent group "
440 "entry");
441 if (ssid->passphrase && !s->passphrase)
442 changed = 1;
443 else if (ssid->passphrase && s->passphrase &&
444 os_strcmp(ssid->passphrase, s->passphrase) != 0)
445 changed = 1;
446 } else {
447 wpa_printf(MSG_DEBUG, "P2P: Create a new persistent group "
448 "entry");
449 changed = 1;
450 s = wpa_config_add_network(wpa_s->conf);
451 if (s == NULL)
452 return -1;
453
454 /*
455 * Instead of network_added we emit persistent_group_added
456 * notification. Also to keep the defense checks in
457 * persistent_group obj registration method, we set the
458 * relevant flags in s to designate it as a persistent group.
459 */
460 s->p2p_group = 1;
461 s->p2p_persistent_group = 1;
462 wpas_notify_persistent_group_added(wpa_s, s);
463 wpa_config_set_network_defaults(s);
464 }
465
466 s->p2p_group = 1;
467 s->p2p_persistent_group = 1;
468 s->disabled = 2;
469 s->bssid_set = 1;
470 os_memcpy(s->bssid, go_dev_addr, ETH_ALEN);
471 s->mode = ssid->mode;
472 s->auth_alg = WPA_AUTH_ALG_OPEN;
473 s->key_mgmt = WPA_KEY_MGMT_PSK;
474 s->proto = WPA_PROTO_RSN;
475 s->pairwise_cipher = WPA_CIPHER_CCMP;
476 s->export_keys = 1;
477 if (ssid->passphrase) {
478 os_free(s->passphrase);
479 s->passphrase = os_strdup(ssid->passphrase);
480 }
481 if (ssid->psk_set) {
482 s->psk_set = 1;
483 os_memcpy(s->psk, ssid->psk, 32);
484 }
485 if (s->passphrase && !s->psk_set)
486 wpa_config_update_psk(s);
487 if (s->ssid == NULL || s->ssid_len < ssid->ssid_len) {
488 os_free(s->ssid);
489 s->ssid = os_malloc(ssid->ssid_len);
490 }
491 if (s->ssid) {
492 s->ssid_len = ssid->ssid_len;
493 os_memcpy(s->ssid, ssid->ssid, s->ssid_len);
494 }
495
496 #ifndef CONFIG_NO_CONFIG_WRITE
497 if (changed && wpa_s->conf->update_config &&
498 wpa_config_write(wpa_s->confname, wpa_s->conf)) {
499 wpa_printf(MSG_DEBUG, "P2P: Failed to update configuration");
500 }
501 #endif /* CONFIG_NO_CONFIG_WRITE */
502
503 return s->id;
504 }
505
506
507 static void wpas_p2p_add_persistent_group_client(struct wpa_supplicant *wpa_s,
508 const u8 *addr)
509 {
510 struct wpa_ssid *ssid, *s;
511 u8 *n;
512 size_t i;
513
514 ssid = wpa_s->current_ssid;
515 if (ssid == NULL || ssid->mode != WPAS_MODE_P2P_GO ||
516 !ssid->p2p_persistent_group)
517 return;
518
519 for (s = wpa_s->parent->conf->ssid; s; s = s->next) {
520 if (s->disabled != 2 || s->mode != WPAS_MODE_P2P_GO)
521 continue;
522
523 if (s->ssid_len == ssid->ssid_len &&
524 os_memcmp(s->ssid, ssid->ssid, s->ssid_len) == 0)
525 break;
526 }
527
528 if (s == NULL)
529 return;
530
531 for (i = 0; s->p2p_client_list && i < s->num_p2p_clients; i++) {
532 if (os_memcmp(s->p2p_client_list + i * ETH_ALEN, addr,
533 ETH_ALEN) == 0)
534 return; /* already in list */
535 }
536
537 n = os_realloc_array(s->p2p_client_list, s->num_p2p_clients + 1,
538 ETH_ALEN);
539 if (n == NULL)
540 return;
541 os_memcpy(n + s->num_p2p_clients * ETH_ALEN, addr, ETH_ALEN);
542 s->p2p_client_list = n;
543 s->num_p2p_clients++;
544
545 #ifndef CONFIG_NO_CONFIG_WRITE
546 if (wpa_s->parent->conf->update_config &&
547 wpa_config_write(wpa_s->parent->confname, wpa_s->parent->conf))
548 wpa_printf(MSG_DEBUG, "P2P: Failed to update configuration");
549 #endif /* CONFIG_NO_CONFIG_WRITE */
550 }
551
552
553 static void wpas_group_formation_completed(struct wpa_supplicant *wpa_s,
554 int success)
555 {
556 struct wpa_ssid *ssid;
557 const char *ssid_txt;
558 int client;
559 int persistent;
560 u8 go_dev_addr[ETH_ALEN];
561 int network_id = -1;
562
563 /*
564 * This callback is likely called for the main interface. Update wpa_s
565 * to use the group interface if a new interface was created for the
566 * group.
567 */
568 if (wpa_s->global->p2p_group_formation)
569 wpa_s = wpa_s->global->p2p_group_formation;
570 wpa_s->global->p2p_group_formation = NULL;
571 wpa_s->p2p_in_provisioning = 0;
572
573 if (!success) {
574 wpa_msg(wpa_s->parent, MSG_INFO,
575 P2P_EVENT_GROUP_FORMATION_FAILURE);
576 wpas_p2p_group_delete(wpa_s,
577 P2P_GROUP_REMOVAL_FORMATION_FAILED);
578 return;
579 }
580
581 wpa_msg(wpa_s->parent, MSG_INFO, P2P_EVENT_GROUP_FORMATION_SUCCESS);
582
583 ssid = wpa_s->current_ssid;
584 if (ssid && ssid->mode == WPAS_MODE_P2P_GROUP_FORMATION) {
585 ssid->mode = WPAS_MODE_P2P_GO;
586 p2p_group_notif_formation_done(wpa_s->p2p_group);
587 wpa_supplicant_ap_mac_addr_filter(wpa_s, NULL);
588 }
589
590 persistent = 0;
591 if (ssid) {
592 ssid_txt = wpa_ssid_txt(ssid->ssid, ssid->ssid_len);
593 client = ssid->mode == WPAS_MODE_INFRA;
594 if (ssid->mode == WPAS_MODE_P2P_GO) {
595 persistent = ssid->p2p_persistent_group;
596 os_memcpy(go_dev_addr, wpa_s->global->p2p_dev_addr,
597 ETH_ALEN);
598 } else
599 persistent = wpas_p2p_persistent_group(wpa_s,
600 go_dev_addr,
601 ssid->ssid,
602 ssid->ssid_len);
603 } else {
604 ssid_txt = "";
605 client = wpa_s->p2p_group_interface ==
606 P2P_GROUP_INTERFACE_CLIENT;
607 os_memset(go_dev_addr, 0, ETH_ALEN);
608 }
609
610 wpa_s->show_group_started = 0;
611 if (client) {
612 /*
613 * Indicate event only after successfully completed 4-way
614 * handshake, i.e., when the interface is ready for data
615 * packets.
616 */
617 wpa_s->show_group_started = 1;
618 } else if (ssid && ssid->passphrase == NULL && ssid->psk_set) {
619 char psk[65];
620 wpa_snprintf_hex(psk, sizeof(psk), ssid->psk, 32);
621 wpa_msg(wpa_s->parent, MSG_INFO, P2P_EVENT_GROUP_STARTED
622 "%s GO ssid=\"%s\" freq=%d psk=%s go_dev_addr=" MACSTR
623 "%s",
624 wpa_s->ifname, ssid_txt, ssid->frequency, psk,
625 MAC2STR(go_dev_addr),
626 persistent ? " [PERSISTENT]" : "");
627 wpas_p2p_cross_connect_setup(wpa_s);
628 wpas_p2p_set_group_idle_timeout(wpa_s);
629 } else {
630 wpa_msg(wpa_s->parent, MSG_INFO, P2P_EVENT_GROUP_STARTED
631 "%s GO ssid=\"%s\" freq=%d passphrase=\"%s\" "
632 "go_dev_addr=" MACSTR "%s",
633 wpa_s->ifname, ssid_txt, ssid ? ssid->frequency : 0,
634 ssid && ssid->passphrase ? ssid->passphrase : "",
635 MAC2STR(go_dev_addr),
636 persistent ? " [PERSISTENT]" : "");
637 wpas_p2p_cross_connect_setup(wpa_s);
638 wpas_p2p_set_group_idle_timeout(wpa_s);
639 }
640
641 if (persistent)
642 network_id = wpas_p2p_store_persistent_group(wpa_s->parent,
643 ssid, go_dev_addr);
644 if (network_id < 0 && ssid)
645 network_id = ssid->id;
646 if (!client)
647 wpas_notify_p2p_group_started(wpa_s, ssid, network_id, 0);
648 }
649
650
651 static void wpas_p2p_send_action_tx_status(struct wpa_supplicant *wpa_s,
652 unsigned int freq,
653 const u8 *dst, const u8 *src,
654 const u8 *bssid,
655 const u8 *data, size_t data_len,
656 enum offchannel_send_action_result
657 result)
658 {
659 enum p2p_send_action_result res = P2P_SEND_ACTION_SUCCESS;
660
661 if (wpa_s->global->p2p == NULL || wpa_s->global->p2p_disabled)
662 return;
663 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
664 return;
665
666 switch (result) {
667 case OFFCHANNEL_SEND_ACTION_SUCCESS:
668 res = P2P_SEND_ACTION_SUCCESS;
669 break;
670 case OFFCHANNEL_SEND_ACTION_NO_ACK:
671 res = P2P_SEND_ACTION_NO_ACK;
672 break;
673 case OFFCHANNEL_SEND_ACTION_FAILED:
674 res = P2P_SEND_ACTION_FAILED;
675 break;
676 }
677
678 p2p_send_action_cb(wpa_s->global->p2p, freq, dst, src, bssid, res);
679
680 if (result != OFFCHANNEL_SEND_ACTION_SUCCESS &&
681 wpa_s->pending_pd_before_join &&
682 (os_memcmp(dst, wpa_s->pending_join_dev_addr, ETH_ALEN) == 0 ||
683 os_memcmp(dst, wpa_s->pending_join_iface_addr, ETH_ALEN) == 0)) {
684 wpa_s->pending_pd_before_join = 0;
685 if (wpa_s->p2p_fallback_to_go_neg) {
686 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: No ACK for PD Req "
687 "during p2p_connect-auto");
688 wpas_p2p_fallback_to_go_neg(wpa_s, 0);
689 return;
690 }
691
692 wpa_printf(MSG_DEBUG, "P2P: Starting pending "
693 "join-existing-group operation (no ACK for PD "
694 "Req)");
695 wpas_p2p_join_start(wpa_s);
696 }
697 }
698
699
700 static int wpas_send_action(void *ctx, unsigned int freq, const u8 *dst,
701 const u8 *src, const u8 *bssid, const u8 *buf,
702 size_t len, unsigned int wait_time)
703 {
704 struct wpa_supplicant *wpa_s = ctx;
705 return offchannel_send_action(wpa_s, freq, dst, src, bssid, buf, len,
706 wait_time,
707 wpas_p2p_send_action_tx_status, 1);
708 }
709
710
711 static void wpas_send_action_done(void *ctx)
712 {
713 struct wpa_supplicant *wpa_s = ctx;
714 offchannel_send_action_done(wpa_s);
715 }
716
717
718 static int wpas_copy_go_neg_results(struct wpa_supplicant *wpa_s,
719 struct p2p_go_neg_results *params)
720 {
721 if (wpa_s->go_params == NULL) {
722 wpa_s->go_params = os_malloc(sizeof(*params));
723 if (wpa_s->go_params == NULL)
724 return -1;
725 }
726 os_memcpy(wpa_s->go_params, params, sizeof(*params));
727 return 0;
728 }
729
730
731 static void wpas_start_wps_enrollee(struct wpa_supplicant *wpa_s,
732 struct p2p_go_neg_results *res)
733 {
734 wpa_printf(MSG_DEBUG, "P2P: Start WPS Enrollee for peer " MACSTR,
735 MAC2STR(res->peer_interface_addr));
736 wpa_hexdump_ascii(MSG_DEBUG, "P2P: Start WPS Enrollee for SSID",
737 res->ssid, res->ssid_len);
738 wpa_supplicant_ap_deinit(wpa_s);
739 wpas_copy_go_neg_results(wpa_s, res);
740 if (res->wps_method == WPS_PBC)
741 wpas_wps_start_pbc(wpa_s, res->peer_interface_addr, 1);
742 else {
743 u16 dev_pw_id = DEV_PW_DEFAULT;
744 if (wpa_s->p2p_wps_method == WPS_PIN_KEYPAD)
745 dev_pw_id = DEV_PW_REGISTRAR_SPECIFIED;
746 wpas_wps_start_pin(wpa_s, res->peer_interface_addr,
747 wpa_s->p2p_pin, 1, dev_pw_id);
748 }
749 }
750
751
752 static void p2p_go_configured(void *ctx, void *data)
753 {
754 struct wpa_supplicant *wpa_s = ctx;
755 struct p2p_go_neg_results *params = data;
756 struct wpa_ssid *ssid;
757 int network_id = -1;
758
759 ssid = wpa_s->current_ssid;
760 if (ssid && ssid->mode == WPAS_MODE_P2P_GO) {
761 wpa_printf(MSG_DEBUG, "P2P: Group setup without provisioning");
762 if (wpa_s->global->p2p_group_formation == wpa_s)
763 wpa_s->global->p2p_group_formation = NULL;
764 wpa_msg(wpa_s->parent, MSG_INFO, P2P_EVENT_GROUP_STARTED
765 "%s GO ssid=\"%s\" freq=%d passphrase=\"%s\" "
766 "go_dev_addr=" MACSTR "%s",
767 wpa_s->ifname,
768 wpa_ssid_txt(ssid->ssid, ssid->ssid_len),
769 ssid->frequency,
770 params->passphrase ? params->passphrase : "",
771 MAC2STR(wpa_s->global->p2p_dev_addr),
772 params->persistent_group ? " [PERSISTENT]" : "");
773
774 if (params->persistent_group)
775 network_id = wpas_p2p_store_persistent_group(
776 wpa_s->parent, ssid,
777 wpa_s->global->p2p_dev_addr);
778 if (network_id < 0)
779 network_id = ssid->id;
780 wpas_notify_p2p_group_started(wpa_s, ssid, network_id, 0);
781 wpas_p2p_cross_connect_setup(wpa_s);
782 wpas_p2p_set_group_idle_timeout(wpa_s);
783 return;
784 }
785
786 wpa_printf(MSG_DEBUG, "P2P: Setting up WPS for GO provisioning");
787 if (wpa_supplicant_ap_mac_addr_filter(wpa_s,
788 params->peer_interface_addr)) {
789 wpa_printf(MSG_DEBUG, "P2P: Failed to setup MAC address "
790 "filtering");
791 return;
792 }
793 if (params->wps_method == WPS_PBC)
794 wpa_supplicant_ap_wps_pbc(wpa_s, params->peer_interface_addr,
795 params->peer_device_addr);
796 else if (wpa_s->p2p_pin[0])
797 wpa_supplicant_ap_wps_pin(wpa_s, params->peer_interface_addr,
798 wpa_s->p2p_pin, NULL, 0);
799 os_free(wpa_s->go_params);
800 wpa_s->go_params = NULL;
801 }
802
803
804 static void wpas_start_wps_go(struct wpa_supplicant *wpa_s,
805 struct p2p_go_neg_results *params,
806 int group_formation)
807 {
808 struct wpa_ssid *ssid;
809
810 if (wpas_copy_go_neg_results(wpa_s, params) < 0)
811 return;
812
813 ssid = wpa_config_add_network(wpa_s->conf);
814 if (ssid == NULL)
815 return;
816
817 wpa_s->show_group_started = 0;
818
819 wpa_config_set_network_defaults(ssid);
820 ssid->temporary = 1;
821 ssid->p2p_group = 1;
822 ssid->p2p_persistent_group = params->persistent_group;
823 ssid->mode = group_formation ? WPAS_MODE_P2P_GROUP_FORMATION :
824 WPAS_MODE_P2P_GO;
825 ssid->frequency = params->freq;
826 ssid->ht40 = params->ht40;
827 ssid->ssid = os_zalloc(params->ssid_len + 1);
828 if (ssid->ssid) {
829 os_memcpy(ssid->ssid, params->ssid, params->ssid_len);
830 ssid->ssid_len = params->ssid_len;
831 }
832 ssid->auth_alg = WPA_AUTH_ALG_OPEN;
833 ssid->key_mgmt = WPA_KEY_MGMT_PSK;
834 ssid->proto = WPA_PROTO_RSN;
835 ssid->pairwise_cipher = WPA_CIPHER_CCMP;
836 ssid->passphrase = os_strdup(params->passphrase);
837 ssid->ap_max_inactivity = wpa_s->parent->conf->p2p_go_max_inactivity;
838
839 wpa_s->ap_configured_cb = p2p_go_configured;
840 wpa_s->ap_configured_cb_ctx = wpa_s;
841 wpa_s->ap_configured_cb_data = wpa_s->go_params;
842 wpa_s->connect_without_scan = ssid;
843 wpa_s->reassociate = 1;
844 wpa_s->disconnected = 0;
845 wpa_supplicant_req_scan(wpa_s, 0, 0);
846 }
847
848
849 static void wpas_p2p_clone_config(struct wpa_supplicant *dst,
850 const struct wpa_supplicant *src)
851 {
852 struct wpa_config *d;
853 const struct wpa_config *s;
854
855 d = dst->conf;
856 s = src->conf;
857
858 #define C(n) if (s->n) d->n = os_strdup(s->n)
859 C(device_name);
860 C(manufacturer);
861 C(model_name);
862 C(model_number);
863 C(serial_number);
864 C(config_methods);
865 #undef C
866
867 os_memcpy(d->device_type, s->device_type, WPS_DEV_TYPE_LEN);
868 os_memcpy(d->sec_device_type, s->sec_device_type,
869 sizeof(d->sec_device_type));
870 d->num_sec_device_types = s->num_sec_device_types;
871
872 d->p2p_group_idle = s->p2p_group_idle;
873 d->p2p_intra_bss = s->p2p_intra_bss;
874 d->persistent_reconnect = s->persistent_reconnect;
875 d->max_num_sta = s->max_num_sta;
876 d->pbc_in_m1 = s->pbc_in_m1;
877 }
878
879
880 static int wpas_p2p_add_group_interface(struct wpa_supplicant *wpa_s,
881 enum wpa_driver_if_type type)
882 {
883 char ifname[120], force_ifname[120];
884
885 if (wpa_s->pending_interface_name[0]) {
886 wpa_printf(MSG_DEBUG, "P2P: Pending virtual interface exists "
887 "- skip creation of a new one");
888 if (is_zero_ether_addr(wpa_s->pending_interface_addr)) {
889 wpa_printf(MSG_DEBUG, "P2P: Pending virtual address "
890 "unknown?! ifname='%s'",
891 wpa_s->pending_interface_name);
892 return -1;
893 }
894 return 0;
895 }
896
897 os_snprintf(ifname, sizeof(ifname), "p2p-%s-%d", wpa_s->ifname,
898 wpa_s->p2p_group_idx);
899 if (os_strlen(ifname) >= IFNAMSIZ &&
900 os_strlen(wpa_s->ifname) < IFNAMSIZ) {
901 /* Try to avoid going over the IFNAMSIZ length limit */
902 os_snprintf(ifname, sizeof(ifname), "p2p-%d",
903 wpa_s->p2p_group_idx);
904 }
905 force_ifname[0] = '\0';
906
907 wpa_printf(MSG_DEBUG, "P2P: Create a new interface %s for the group",
908 ifname);
909 wpa_s->p2p_group_idx++;
910
911 wpa_s->pending_interface_type = type;
912 if (wpa_drv_if_add(wpa_s, type, ifname, NULL, NULL, force_ifname,
913 wpa_s->pending_interface_addr, NULL) < 0) {
914 wpa_printf(MSG_ERROR, "P2P: Failed to create new group "
915 "interface");
916 return -1;
917 }
918
919 if (force_ifname[0]) {
920 wpa_printf(MSG_DEBUG, "P2P: Driver forced interface name %s",
921 force_ifname);
922 os_strlcpy(wpa_s->pending_interface_name, force_ifname,
923 sizeof(wpa_s->pending_interface_name));
924 } else
925 os_strlcpy(wpa_s->pending_interface_name, ifname,
926 sizeof(wpa_s->pending_interface_name));
927 wpa_printf(MSG_DEBUG, "P2P: Created pending virtual interface %s addr "
928 MACSTR, wpa_s->pending_interface_name,
929 MAC2STR(wpa_s->pending_interface_addr));
930
931 return 0;
932 }
933
934
935 static void wpas_p2p_remove_pending_group_interface(
936 struct wpa_supplicant *wpa_s)
937 {
938 if (!wpa_s->pending_interface_name[0] ||
939 is_zero_ether_addr(wpa_s->pending_interface_addr))
940 return; /* No pending virtual interface */
941
942 wpa_printf(MSG_DEBUG, "P2P: Removing pending group interface %s",
943 wpa_s->pending_interface_name);
944 wpa_drv_if_remove(wpa_s, wpa_s->pending_interface_type,
945 wpa_s->pending_interface_name);
946 os_memset(wpa_s->pending_interface_addr, 0, ETH_ALEN);
947 wpa_s->pending_interface_name[0] = '\0';
948 }
949
950
951 static struct wpa_supplicant *
952 wpas_p2p_init_group_interface(struct wpa_supplicant *wpa_s, int go)
953 {
954 struct wpa_interface iface;
955 struct wpa_supplicant *group_wpa_s;
956
957 if (!wpa_s->pending_interface_name[0]) {
958 wpa_printf(MSG_ERROR, "P2P: No pending group interface");
959 if (!wpas_p2p_create_iface(wpa_s))
960 return NULL;
961 /*
962 * Something has forced us to remove the pending interface; try
963 * to create a new one and hope for the best that we will get
964 * the same local address.
965 */
966 if (wpas_p2p_add_group_interface(wpa_s, go ? WPA_IF_P2P_GO :
967 WPA_IF_P2P_CLIENT) < 0)
968 return NULL;
969 }
970
971 os_memset(&iface, 0, sizeof(iface));
972 iface.ifname = wpa_s->pending_interface_name;
973 iface.driver = wpa_s->driver->name;
974 iface.ctrl_interface = wpa_s->conf->ctrl_interface;
975 iface.driver_param = wpa_s->conf->driver_param;
976 group_wpa_s = wpa_supplicant_add_iface(wpa_s->global, &iface);
977 if (group_wpa_s == NULL) {
978 wpa_printf(MSG_ERROR, "P2P: Failed to create new "
979 "wpa_supplicant interface");
980 return NULL;
981 }
982 wpa_s->pending_interface_name[0] = '\0';
983 group_wpa_s->parent = wpa_s;
984 group_wpa_s->p2p_group_interface = go ? P2P_GROUP_INTERFACE_GO :
985 P2P_GROUP_INTERFACE_CLIENT;
986 wpa_s->global->p2p_group_formation = group_wpa_s;
987
988 wpas_p2p_clone_config(group_wpa_s, wpa_s);
989
990 return group_wpa_s;
991 }
992
993
994 static void wpas_p2p_group_formation_timeout(void *eloop_ctx,
995 void *timeout_ctx)
996 {
997 struct wpa_supplicant *wpa_s = eloop_ctx;
998 wpa_printf(MSG_DEBUG, "P2P: Group Formation timed out");
999 if (wpa_s->global->p2p)
1000 p2p_group_formation_failed(wpa_s->global->p2p);
1001 else if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
1002 wpa_drv_p2p_group_formation_failed(wpa_s);
1003 wpas_group_formation_completed(wpa_s, 0);
1004 }
1005
1006
1007 void wpas_go_neg_completed(void *ctx, struct p2p_go_neg_results *res)
1008 {
1009 struct wpa_supplicant *wpa_s = ctx;
1010
1011 if (wpa_s->off_channel_freq || wpa_s->roc_waiting_drv_freq) {
1012 wpa_drv_cancel_remain_on_channel(wpa_s);
1013 wpa_s->off_channel_freq = 0;
1014 wpa_s->roc_waiting_drv_freq = 0;
1015 }
1016
1017 if (res->status) {
1018 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_GO_NEG_FAILURE "status=%d",
1019 res->status);
1020 wpas_notify_p2p_go_neg_completed(wpa_s, res);
1021 wpas_p2p_remove_pending_group_interface(wpa_s);
1022 return;
1023 }
1024
1025 if (wpa_s->p2p_go_ht40)
1026 res->ht40 = 1;
1027
1028 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_GO_NEG_SUCCESS);
1029 wpas_notify_p2p_go_neg_completed(wpa_s, res);
1030
1031 if (res->role_go && wpa_s->p2p_persistent_id >= 0) {
1032 struct wpa_ssid *ssid;
1033 ssid = wpa_config_get_network(wpa_s->conf,
1034 wpa_s->p2p_persistent_id);
1035 if (ssid && ssid->disabled == 2 &&
1036 ssid->mode == WPAS_MODE_P2P_GO && ssid->passphrase) {
1037 size_t len = os_strlen(ssid->passphrase);
1038 wpa_printf(MSG_DEBUG, "P2P: Override passphrase based "
1039 "on requested persistent group");
1040 os_memcpy(res->passphrase, ssid->passphrase, len);
1041 res->passphrase[len] = '\0';
1042 }
1043 }
1044
1045 if (wpa_s->create_p2p_iface) {
1046 struct wpa_supplicant *group_wpa_s =
1047 wpas_p2p_init_group_interface(wpa_s, res->role_go);
1048 if (group_wpa_s == NULL) {
1049 wpas_p2p_remove_pending_group_interface(wpa_s);
1050 return;
1051 }
1052 if (group_wpa_s != wpa_s) {
1053 os_memcpy(group_wpa_s->p2p_pin, wpa_s->p2p_pin,
1054 sizeof(group_wpa_s->p2p_pin));
1055 group_wpa_s->p2p_wps_method = wpa_s->p2p_wps_method;
1056 }
1057 os_memset(wpa_s->pending_interface_addr, 0, ETH_ALEN);
1058 wpa_s->pending_interface_name[0] = '\0';
1059 group_wpa_s->p2p_in_provisioning = 1;
1060
1061 if (res->role_go)
1062 wpas_start_wps_go(group_wpa_s, res, 1);
1063 else
1064 wpas_start_wps_enrollee(group_wpa_s, res);
1065 } else {
1066 wpa_s->p2p_in_provisioning = 1;
1067 wpa_s->global->p2p_group_formation = wpa_s;
1068
1069 if (res->role_go)
1070 wpas_start_wps_go(wpa_s, res, 1);
1071 else
1072 wpas_start_wps_enrollee(ctx, res);
1073 }
1074
1075 wpa_s->p2p_long_listen = 0;
1076 eloop_cancel_timeout(wpas_p2p_long_listen_timeout, wpa_s, NULL);
1077
1078 eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s, NULL);
1079 eloop_register_timeout(15 + res->peer_config_timeout / 100,
1080 (res->peer_config_timeout % 100) * 10000,
1081 wpas_p2p_group_formation_timeout, wpa_s, NULL);
1082 }
1083
1084
1085 void wpas_go_neg_req_rx(void *ctx, const u8 *src, u16 dev_passwd_id)
1086 {
1087 struct wpa_supplicant *wpa_s = ctx;
1088 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_GO_NEG_REQUEST MACSTR
1089 " dev_passwd_id=%u", MAC2STR(src), dev_passwd_id);
1090
1091 wpas_notify_p2p_go_neg_req(wpa_s, src, dev_passwd_id);
1092 }
1093
1094
1095 void wpas_dev_found(void *ctx, const u8 *addr,
1096 const struct p2p_peer_info *info,
1097 int new_device)
1098 {
1099 #ifndef CONFIG_NO_STDOUT_DEBUG
1100 struct wpa_supplicant *wpa_s = ctx;
1101 char devtype[WPS_DEV_TYPE_BUFSIZE];
1102
1103 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_DEVICE_FOUND MACSTR
1104 " p2p_dev_addr=" MACSTR
1105 " pri_dev_type=%s name='%s' config_methods=0x%x "
1106 "dev_capab=0x%x group_capab=0x%x",
1107 MAC2STR(addr), MAC2STR(info->p2p_device_addr),
1108 wps_dev_type_bin2str(info->pri_dev_type, devtype,
1109 sizeof(devtype)),
1110 info->device_name, info->config_methods,
1111 info->dev_capab, info->group_capab);
1112 #endif /* CONFIG_NO_STDOUT_DEBUG */
1113
1114 wpas_notify_p2p_device_found(ctx, info->p2p_device_addr, new_device);
1115 }
1116
1117
1118 static void wpas_dev_lost(void *ctx, const u8 *dev_addr)
1119 {
1120 struct wpa_supplicant *wpa_s = ctx;
1121
1122 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_DEVICE_LOST
1123 "p2p_dev_addr=" MACSTR, MAC2STR(dev_addr));
1124
1125 wpas_notify_p2p_device_lost(wpa_s, dev_addr);
1126 }
1127
1128
1129 static int wpas_start_listen(void *ctx, unsigned int freq,
1130 unsigned int duration,
1131 const struct wpabuf *probe_resp_ie)
1132 {
1133 struct wpa_supplicant *wpa_s = ctx;
1134
1135 wpa_drv_set_ap_wps_ie(wpa_s, NULL, probe_resp_ie, NULL);
1136
1137 if (wpa_drv_probe_req_report(wpa_s, 1) < 0) {
1138 wpa_printf(MSG_DEBUG, "P2P: Failed to request the driver to "
1139 "report received Probe Request frames");
1140 return -1;
1141 }
1142
1143 wpa_s->pending_listen_freq = freq;
1144 wpa_s->pending_listen_duration = duration;
1145
1146 if (wpa_drv_remain_on_channel(wpa_s, freq, duration) < 0) {
1147 wpa_printf(MSG_DEBUG, "P2P: Failed to request the driver "
1148 "to remain on channel (%u MHz) for Listen "
1149 "state", freq);
1150 wpa_s->pending_listen_freq = 0;
1151 return -1;
1152 }
1153 wpa_s->off_channel_freq = 0;
1154 wpa_s->roc_waiting_drv_freq = freq;
1155
1156 return 0;
1157 }
1158
1159
1160 static void wpas_stop_listen(void *ctx)
1161 {
1162 struct wpa_supplicant *wpa_s = ctx;
1163 if (wpa_s->off_channel_freq || wpa_s->roc_waiting_drv_freq) {
1164 wpa_drv_cancel_remain_on_channel(wpa_s);
1165 wpa_s->off_channel_freq = 0;
1166 wpa_s->roc_waiting_drv_freq = 0;
1167 }
1168 wpa_drv_set_ap_wps_ie(wpa_s, NULL, NULL, NULL);
1169 wpa_drv_probe_req_report(wpa_s, 0);
1170 }
1171
1172
1173 static int wpas_send_probe_resp(void *ctx, const struct wpabuf *buf)
1174 {
1175 struct wpa_supplicant *wpa_s = ctx;
1176 return wpa_drv_send_mlme(wpa_s, wpabuf_head(buf), wpabuf_len(buf), 1);
1177 }
1178
1179
1180 static struct p2p_srv_bonjour *
1181 wpas_p2p_service_get_bonjour(struct wpa_supplicant *wpa_s,
1182 const struct wpabuf *query)
1183 {
1184 struct p2p_srv_bonjour *bsrv;
1185 size_t len;
1186
1187 len = wpabuf_len(query);
1188 dl_list_for_each(bsrv, &wpa_s->global->p2p_srv_bonjour,
1189 struct p2p_srv_bonjour, list) {
1190 if (len == wpabuf_len(bsrv->query) &&
1191 os_memcmp(wpabuf_head(query), wpabuf_head(bsrv->query),
1192 len) == 0)
1193 return bsrv;
1194 }
1195 return NULL;
1196 }
1197
1198
1199 static struct p2p_srv_upnp *
1200 wpas_p2p_service_get_upnp(struct wpa_supplicant *wpa_s, u8 version,
1201 const char *service)
1202 {
1203 struct p2p_srv_upnp *usrv;
1204
1205 dl_list_for_each(usrv, &wpa_s->global->p2p_srv_upnp,
1206 struct p2p_srv_upnp, list) {
1207 if (version == usrv->version &&
1208 os_strcmp(service, usrv->service) == 0)
1209 return usrv;
1210 }
1211 return NULL;
1212 }
1213
1214
1215 static void wpas_sd_add_proto_not_avail(struct wpabuf *resp, u8 srv_proto,
1216 u8 srv_trans_id)
1217 {
1218 u8 *len_pos;
1219
1220 if (wpabuf_tailroom(resp) < 5)
1221 return;
1222
1223 /* Length (to be filled) */
1224 len_pos = wpabuf_put(resp, 2);
1225 wpabuf_put_u8(resp, srv_proto);
1226 wpabuf_put_u8(resp, srv_trans_id);
1227 /* Status Code */
1228 wpabuf_put_u8(resp, P2P_SD_PROTO_NOT_AVAILABLE);
1229 /* Response Data: empty */
1230 WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos - 2);
1231 }
1232
1233
1234 static void wpas_sd_all_bonjour(struct wpa_supplicant *wpa_s,
1235 struct wpabuf *resp, u8 srv_trans_id)
1236 {
1237 struct p2p_srv_bonjour *bsrv;
1238 u8 *len_pos;
1239
1240 wpa_printf(MSG_DEBUG, "P2P: SD Request for all Bonjour services");
1241
1242 if (dl_list_empty(&wpa_s->global->p2p_srv_bonjour)) {
1243 wpa_printf(MSG_DEBUG, "P2P: Bonjour protocol not available");
1244 return;
1245 }
1246
1247 dl_list_for_each(bsrv, &wpa_s->global->p2p_srv_bonjour,
1248 struct p2p_srv_bonjour, list) {
1249 if (wpabuf_tailroom(resp) <
1250 5 + wpabuf_len(bsrv->query) + wpabuf_len(bsrv->resp))
1251 return;
1252 /* Length (to be filled) */
1253 len_pos = wpabuf_put(resp, 2);
1254 wpabuf_put_u8(resp, P2P_SERV_BONJOUR);
1255 wpabuf_put_u8(resp, srv_trans_id);
1256 /* Status Code */
1257 wpabuf_put_u8(resp, P2P_SD_SUCCESS);
1258 wpa_hexdump_ascii(MSG_DEBUG, "P2P: Matching Bonjour service",
1259 wpabuf_head(bsrv->resp),
1260 wpabuf_len(bsrv->resp));
1261 /* Response Data */
1262 wpabuf_put_buf(resp, bsrv->query); /* Key */
1263 wpabuf_put_buf(resp, bsrv->resp); /* Value */
1264 WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos -
1265 2);
1266 }
1267 }
1268
1269
1270 static void wpas_sd_req_bonjour(struct wpa_supplicant *wpa_s,
1271 struct wpabuf *resp, u8 srv_trans_id,
1272 const u8 *query, size_t query_len)
1273 {
1274 struct p2p_srv_bonjour *bsrv;
1275 struct wpabuf buf;
1276 u8 *len_pos;
1277
1278 wpa_hexdump_ascii(MSG_DEBUG, "P2P: SD Request for Bonjour",
1279 query, query_len);
1280 if (dl_list_empty(&wpa_s->global->p2p_srv_bonjour)) {
1281 wpa_printf(MSG_DEBUG, "P2P: Bonjour protocol not available");
1282 wpas_sd_add_proto_not_avail(resp, P2P_SERV_BONJOUR,
1283 srv_trans_id);
1284 return;
1285 }
1286
1287 if (query_len == 0) {
1288 wpas_sd_all_bonjour(wpa_s, resp, srv_trans_id);
1289 return;
1290 }
1291
1292 if (wpabuf_tailroom(resp) < 5)
1293 return;
1294 /* Length (to be filled) */
1295 len_pos = wpabuf_put(resp, 2);
1296 wpabuf_put_u8(resp, P2P_SERV_BONJOUR);
1297 wpabuf_put_u8(resp, srv_trans_id);
1298
1299 wpabuf_set(&buf, query, query_len);
1300 bsrv = wpas_p2p_service_get_bonjour(wpa_s, &buf);
1301 if (bsrv == NULL) {
1302 wpa_printf(MSG_DEBUG, "P2P: Requested Bonjour service not "
1303 "available");
1304
1305 /* Status Code */
1306 wpabuf_put_u8(resp, P2P_SD_REQUESTED_INFO_NOT_AVAILABLE);
1307 /* Response Data: empty */
1308 WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos -
1309 2);
1310 return;
1311 }
1312
1313 /* Status Code */
1314 wpabuf_put_u8(resp, P2P_SD_SUCCESS);
1315 wpa_hexdump_ascii(MSG_DEBUG, "P2P: Matching Bonjour service",
1316 wpabuf_head(bsrv->resp), wpabuf_len(bsrv->resp));
1317
1318 if (wpabuf_tailroom(resp) >=
1319 wpabuf_len(bsrv->query) + wpabuf_len(bsrv->resp)) {
1320 /* Response Data */
1321 wpabuf_put_buf(resp, bsrv->query); /* Key */
1322 wpabuf_put_buf(resp, bsrv->resp); /* Value */
1323 }
1324 WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos - 2);
1325 }
1326
1327
1328 static void wpas_sd_all_upnp(struct wpa_supplicant *wpa_s,
1329 struct wpabuf *resp, u8 srv_trans_id)
1330 {
1331 struct p2p_srv_upnp *usrv;
1332 u8 *len_pos;
1333
1334 wpa_printf(MSG_DEBUG, "P2P: SD Request for all UPnP services");
1335
1336 if (dl_list_empty(&wpa_s->global->p2p_srv_upnp)) {
1337 wpa_printf(MSG_DEBUG, "P2P: UPnP protocol not available");
1338 return;
1339 }
1340
1341 dl_list_for_each(usrv, &wpa_s->global->p2p_srv_upnp,
1342 struct p2p_srv_upnp, list) {
1343 if (wpabuf_tailroom(resp) < 5 + 1 + os_strlen(usrv->service))
1344 return;
1345
1346 /* Length (to be filled) */
1347 len_pos = wpabuf_put(resp, 2);
1348 wpabuf_put_u8(resp, P2P_SERV_UPNP);
1349 wpabuf_put_u8(resp, srv_trans_id);
1350
1351 /* Status Code */
1352 wpabuf_put_u8(resp, P2P_SD_SUCCESS);
1353 /* Response Data */
1354 wpabuf_put_u8(resp, usrv->version);
1355 wpa_printf(MSG_DEBUG, "P2P: Matching UPnP Service: %s",
1356 usrv->service);
1357 wpabuf_put_str(resp, usrv->service);
1358 WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos -
1359 2);
1360 }
1361 }
1362
1363
1364 static void wpas_sd_req_upnp(struct wpa_supplicant *wpa_s,
1365 struct wpabuf *resp, u8 srv_trans_id,
1366 const u8 *query, size_t query_len)
1367 {
1368 struct p2p_srv_upnp *usrv;
1369 u8 *len_pos;
1370 u8 version;
1371 char *str;
1372 int count = 0;
1373
1374 wpa_hexdump_ascii(MSG_DEBUG, "P2P: SD Request for UPnP",
1375 query, query_len);
1376
1377 if (dl_list_empty(&wpa_s->global->p2p_srv_upnp)) {
1378 wpa_printf(MSG_DEBUG, "P2P: UPnP protocol not available");
1379 wpas_sd_add_proto_not_avail(resp, P2P_SERV_UPNP,
1380 srv_trans_id);
1381 return;
1382 }
1383
1384 if (query_len == 0) {
1385 wpas_sd_all_upnp(wpa_s, resp, srv_trans_id);
1386 return;
1387 }
1388
1389 if (wpabuf_tailroom(resp) < 5)
1390 return;
1391
1392 /* Length (to be filled) */
1393 len_pos = wpabuf_put(resp, 2);
1394 wpabuf_put_u8(resp, P2P_SERV_UPNP);
1395 wpabuf_put_u8(resp, srv_trans_id);
1396
1397 version = query[0];
1398 str = os_malloc(query_len);
1399 if (str == NULL)
1400 return;
1401 os_memcpy(str, query + 1, query_len - 1);
1402 str[query_len - 1] = '\0';
1403
1404 dl_list_for_each(usrv, &wpa_s->global->p2p_srv_upnp,
1405 struct p2p_srv_upnp, list) {
1406 if (version != usrv->version)
1407 continue;
1408
1409 if (os_strcmp(str, "ssdp:all") != 0 &&
1410 os_strstr(usrv->service, str) == NULL)
1411 continue;
1412
1413 if (wpabuf_tailroom(resp) < 2)
1414 break;
1415 if (count == 0) {
1416 /* Status Code */
1417 wpabuf_put_u8(resp, P2P_SD_SUCCESS);
1418 /* Response Data */
1419 wpabuf_put_u8(resp, version);
1420 } else
1421 wpabuf_put_u8(resp, ',');
1422
1423 count++;
1424
1425 wpa_printf(MSG_DEBUG, "P2P: Matching UPnP Service: %s",
1426 usrv->service);
1427 if (wpabuf_tailroom(resp) < os_strlen(usrv->service))
1428 break;
1429 wpabuf_put_str(resp, usrv->service);
1430 }
1431 os_free(str);
1432
1433 if (count == 0) {
1434 wpa_printf(MSG_DEBUG, "P2P: Requested UPnP service not "
1435 "available");
1436 /* Status Code */
1437 wpabuf_put_u8(resp, P2P_SD_REQUESTED_INFO_NOT_AVAILABLE);
1438 /* Response Data: empty */
1439 }
1440
1441 WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos - 2);
1442 }
1443
1444
1445 void wpas_sd_request(void *ctx, int freq, const u8 *sa, u8 dialog_token,
1446 u16 update_indic, const u8 *tlvs, size_t tlvs_len)
1447 {
1448 struct wpa_supplicant *wpa_s = ctx;
1449 const u8 *pos = tlvs;
1450 const u8 *end = tlvs + tlvs_len;
1451 const u8 *tlv_end;
1452 u16 slen;
1453 struct wpabuf *resp;
1454 u8 srv_proto, srv_trans_id;
1455 size_t buf_len;
1456 char *buf;
1457
1458 wpa_hexdump(MSG_MSGDUMP, "P2P: Service Discovery Request TLVs",
1459 tlvs, tlvs_len);
1460 buf_len = 2 * tlvs_len + 1;
1461 buf = os_malloc(buf_len);
1462 if (buf) {
1463 wpa_snprintf_hex(buf, buf_len, tlvs, tlvs_len);
1464 wpa_msg_ctrl(wpa_s, MSG_INFO, P2P_EVENT_SERV_DISC_REQ "%d "
1465 MACSTR " %u %u %s",
1466 freq, MAC2STR(sa), dialog_token, update_indic,
1467 buf);
1468 os_free(buf);
1469 }
1470
1471 if (wpa_s->p2p_sd_over_ctrl_iface) {
1472 wpas_notify_p2p_sd_request(wpa_s, freq, sa, dialog_token,
1473 update_indic, tlvs, tlvs_len);
1474 return; /* to be processed by an external program */
1475 }
1476
1477 resp = wpabuf_alloc(10000);
1478 if (resp == NULL)
1479 return;
1480
1481 while (pos + 1 < end) {
1482 wpa_printf(MSG_DEBUG, "P2P: Service Request TLV");
1483 slen = WPA_GET_LE16(pos);
1484 pos += 2;
1485 if (pos + slen > end || slen < 2) {
1486 wpa_printf(MSG_DEBUG, "P2P: Unexpected Query Data "
1487 "length");
1488 wpabuf_free(resp);
1489 return;
1490 }
1491 tlv_end = pos + slen;
1492
1493 srv_proto = *pos++;
1494 wpa_printf(MSG_DEBUG, "P2P: Service Protocol Type %u",
1495 srv_proto);
1496 srv_trans_id = *pos++;
1497 wpa_printf(MSG_DEBUG, "P2P: Service Transaction ID %u",
1498 srv_trans_id);
1499
1500 wpa_hexdump(MSG_MSGDUMP, "P2P: Query Data",
1501 pos, tlv_end - pos);
1502
1503
1504 if (wpa_s->force_long_sd) {
1505 wpa_printf(MSG_DEBUG, "P2P: SD test - force long "
1506 "response");
1507 wpas_sd_all_bonjour(wpa_s, resp, srv_trans_id);
1508 wpas_sd_all_upnp(wpa_s, resp, srv_trans_id);
1509 goto done;
1510 }
1511
1512 switch (srv_proto) {
1513 case P2P_SERV_ALL_SERVICES:
1514 wpa_printf(MSG_DEBUG, "P2P: Service Discovery Request "
1515 "for all services");
1516 if (dl_list_empty(&wpa_s->global->p2p_srv_upnp) &&
1517 dl_list_empty(&wpa_s->global->p2p_srv_bonjour)) {
1518 wpa_printf(MSG_DEBUG, "P2P: No service "
1519 "discovery protocols available");
1520 wpas_sd_add_proto_not_avail(
1521 resp, P2P_SERV_ALL_SERVICES,
1522 srv_trans_id);
1523 break;
1524 }
1525 wpas_sd_all_bonjour(wpa_s, resp, srv_trans_id);
1526 wpas_sd_all_upnp(wpa_s, resp, srv_trans_id);
1527 break;
1528 case P2P_SERV_BONJOUR:
1529 wpas_sd_req_bonjour(wpa_s, resp, srv_trans_id,
1530 pos, tlv_end - pos);
1531 break;
1532 case P2P_SERV_UPNP:
1533 wpas_sd_req_upnp(wpa_s, resp, srv_trans_id,
1534 pos, tlv_end - pos);
1535 break;
1536 default:
1537 wpa_printf(MSG_DEBUG, "P2P: Unavailable service "
1538 "protocol %u", srv_proto);
1539 wpas_sd_add_proto_not_avail(resp, srv_proto,
1540 srv_trans_id);
1541 break;
1542 }
1543
1544 pos = tlv_end;
1545 }
1546
1547 done:
1548 wpas_notify_p2p_sd_request(wpa_s, freq, sa, dialog_token,
1549 update_indic, tlvs, tlvs_len);
1550
1551 wpas_p2p_sd_response(wpa_s, freq, sa, dialog_token, resp);
1552
1553 wpabuf_free(resp);
1554 }
1555
1556
1557 void wpas_sd_response(void *ctx, const u8 *sa, u16 update_indic,
1558 const u8 *tlvs, size_t tlvs_len)
1559 {
1560 struct wpa_supplicant *wpa_s = ctx;
1561 const u8 *pos = tlvs;
1562 const u8 *end = tlvs + tlvs_len;
1563 const u8 *tlv_end;
1564 u16 slen;
1565 size_t buf_len;
1566 char *buf;
1567
1568 wpa_hexdump(MSG_MSGDUMP, "P2P: Service Discovery Response TLVs",
1569 tlvs, tlvs_len);
1570 if (tlvs_len > 1500) {
1571 /* TODO: better way for handling this */
1572 wpa_msg_ctrl(wpa_s, MSG_INFO,
1573 P2P_EVENT_SERV_DISC_RESP MACSTR
1574 " %u <long response: %u bytes>",
1575 MAC2STR(sa), update_indic,
1576 (unsigned int) tlvs_len);
1577 } else {
1578 buf_len = 2 * tlvs_len + 1;
1579 buf = os_malloc(buf_len);
1580 if (buf) {
1581 wpa_snprintf_hex(buf, buf_len, tlvs, tlvs_len);
1582 wpa_msg_ctrl(wpa_s, MSG_INFO,
1583 P2P_EVENT_SERV_DISC_RESP MACSTR " %u %s",
1584 MAC2STR(sa), update_indic, buf);
1585 os_free(buf);
1586 }
1587 }
1588
1589 while (pos < end) {
1590 u8 srv_proto, srv_trans_id, status;
1591
1592 wpa_printf(MSG_DEBUG, "P2P: Service Response TLV");
1593 slen = WPA_GET_LE16(pos);
1594 pos += 2;
1595 if (pos + slen > end || slen < 3) {
1596 wpa_printf(MSG_DEBUG, "P2P: Unexpected Response Data "
1597 "length");
1598 return;
1599 }
1600 tlv_end = pos + slen;
1601
1602 srv_proto = *pos++;
1603 wpa_printf(MSG_DEBUG, "P2P: Service Protocol Type %u",
1604 srv_proto);
1605 srv_trans_id = *pos++;
1606 wpa_printf(MSG_DEBUG, "P2P: Service Transaction ID %u",
1607 srv_trans_id);
1608 status = *pos++;
1609 wpa_printf(MSG_DEBUG, "P2P: Status Code ID %u",
1610 status);
1611
1612 wpa_hexdump(MSG_MSGDUMP, "P2P: Response Data",
1613 pos, tlv_end - pos);
1614
1615 pos = tlv_end;
1616 }
1617
1618 wpas_notify_p2p_sd_response(wpa_s, sa, update_indic, tlvs, tlvs_len);
1619 }
1620
1621
1622 u64 wpas_p2p_sd_request(struct wpa_supplicant *wpa_s, const u8 *dst,
1623 const struct wpabuf *tlvs)
1624 {
1625 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
1626 return wpa_drv_p2p_sd_request(wpa_s, dst, tlvs);
1627 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
1628 return 0;
1629 return (uintptr_t) p2p_sd_request(wpa_s->global->p2p, dst, tlvs);
1630 }
1631
1632
1633 u64 wpas_p2p_sd_request_upnp(struct wpa_supplicant *wpa_s, const u8 *dst,
1634 u8 version, const char *query)
1635 {
1636 struct wpabuf *tlvs;
1637 u64 ret;
1638
1639 tlvs = wpabuf_alloc(2 + 1 + 1 + 1 + os_strlen(query));
1640 if (tlvs == NULL)
1641 return 0;
1642 wpabuf_put_le16(tlvs, 1 + 1 + 1 + os_strlen(query));
1643 wpabuf_put_u8(tlvs, P2P_SERV_UPNP); /* Service Protocol Type */
1644 wpabuf_put_u8(tlvs, 1); /* Service Transaction ID */
1645 wpabuf_put_u8(tlvs, version);
1646 wpabuf_put_str(tlvs, query);
1647 ret = wpas_p2p_sd_request(wpa_s, dst, tlvs);
1648 wpabuf_free(tlvs);
1649 return ret;
1650 }
1651
1652
1653 int wpas_p2p_sd_cancel_request(struct wpa_supplicant *wpa_s, u64 req)
1654 {
1655 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
1656 return wpa_drv_p2p_sd_cancel_request(wpa_s, req);
1657 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
1658 return -1;
1659 return p2p_sd_cancel_request(wpa_s->global->p2p,
1660 (void *) (uintptr_t) req);
1661 }
1662
1663
1664 void wpas_p2p_sd_response(struct wpa_supplicant *wpa_s, int freq,
1665 const u8 *dst, u8 dialog_token,
1666 const struct wpabuf *resp_tlvs)
1667 {
1668 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT) {
1669 wpa_drv_p2p_sd_response(wpa_s, freq, dst, dialog_token,
1670 resp_tlvs);
1671 return;
1672 }
1673 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
1674 return;
1675 p2p_sd_response(wpa_s->global->p2p, freq, dst, dialog_token,
1676 resp_tlvs);
1677 }
1678
1679
1680 void wpas_p2p_sd_service_update(struct wpa_supplicant *wpa_s)
1681 {
1682 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT) {
1683 wpa_drv_p2p_service_update(wpa_s);
1684 return;
1685 }
1686 if (wpa_s->global->p2p)
1687 p2p_sd_service_update(wpa_s->global->p2p);
1688 }
1689
1690
1691 static void wpas_p2p_srv_bonjour_free(struct p2p_srv_bonjour *bsrv)
1692 {
1693 dl_list_del(&bsrv->list);
1694 wpabuf_free(bsrv->query);
1695 wpabuf_free(bsrv->resp);
1696 os_free(bsrv);
1697 }
1698
1699
1700 static void wpas_p2p_srv_upnp_free(struct p2p_srv_upnp *usrv)
1701 {
1702 dl_list_del(&usrv->list);
1703 os_free(usrv->service);
1704 os_free(usrv);
1705 }
1706
1707
1708 void wpas_p2p_service_flush(struct wpa_supplicant *wpa_s)
1709 {
1710 struct p2p_srv_bonjour *bsrv, *bn;
1711 struct p2p_srv_upnp *usrv, *un;
1712
1713 dl_list_for_each_safe(bsrv, bn, &wpa_s->global->p2p_srv_bonjour,
1714 struct p2p_srv_bonjour, list)
1715 wpas_p2p_srv_bonjour_free(bsrv);
1716
1717 dl_list_for_each_safe(usrv, un, &wpa_s->global->p2p_srv_upnp,
1718 struct p2p_srv_upnp, list)
1719 wpas_p2p_srv_upnp_free(usrv);
1720
1721 wpas_p2p_sd_service_update(wpa_s);
1722 }
1723
1724
1725 int wpas_p2p_service_add_bonjour(struct wpa_supplicant *wpa_s,
1726 struct wpabuf *query, struct wpabuf *resp)
1727 {
1728 struct p2p_srv_bonjour *bsrv;
1729
1730 bsrv = wpas_p2p_service_get_bonjour(wpa_s, query);
1731 if (bsrv) {
1732 wpabuf_free(query);
1733 wpabuf_free(bsrv->resp);
1734 bsrv->resp = resp;
1735 return 0;
1736 }
1737
1738 bsrv = os_zalloc(sizeof(*bsrv));
1739 if (bsrv == NULL)
1740 return -1;
1741 bsrv->query = query;
1742 bsrv->resp = resp;
1743 dl_list_add(&wpa_s->global->p2p_srv_bonjour, &bsrv->list);
1744
1745 wpas_p2p_sd_service_update(wpa_s);
1746 return 0;
1747 }
1748
1749
1750 int wpas_p2p_service_del_bonjour(struct wpa_supplicant *wpa_s,
1751 const struct wpabuf *query)
1752 {
1753 struct p2p_srv_bonjour *bsrv;
1754
1755 bsrv = wpas_p2p_service_get_bonjour(wpa_s, query);
1756 if (bsrv == NULL)
1757 return -1;
1758 wpas_p2p_srv_bonjour_free(bsrv);
1759 wpas_p2p_sd_service_update(wpa_s);
1760 return 0;
1761 }
1762
1763
1764 int wpas_p2p_service_add_upnp(struct wpa_supplicant *wpa_s, u8 version,
1765 const char *service)
1766 {
1767 struct p2p_srv_upnp *usrv;
1768
1769 if (wpas_p2p_service_get_upnp(wpa_s, version, service))
1770 return 0; /* Already listed */
1771 usrv = os_zalloc(sizeof(*usrv));
1772 if (usrv == NULL)
1773 return -1;
1774 usrv->version = version;
1775 usrv->service = os_strdup(service);
1776 if (usrv->service == NULL) {
1777 os_free(usrv);
1778 return -1;
1779 }
1780 dl_list_add(&wpa_s->global->p2p_srv_upnp, &usrv->list);
1781
1782 wpas_p2p_sd_service_update(wpa_s);
1783 return 0;
1784 }
1785
1786
1787 int wpas_p2p_service_del_upnp(struct wpa_supplicant *wpa_s, u8 version,
1788 const char *service)
1789 {
1790 struct p2p_srv_upnp *usrv;
1791
1792 usrv = wpas_p2p_service_get_upnp(wpa_s, version, service);
1793 if (usrv == NULL)
1794 return -1;
1795 wpas_p2p_srv_upnp_free(usrv);
1796 wpas_p2p_sd_service_update(wpa_s);
1797 return 0;
1798 }
1799
1800
1801 static void wpas_prov_disc_local_display(struct wpa_supplicant *wpa_s,
1802 const u8 *peer, const char *params,
1803 unsigned int generated_pin)
1804 {
1805 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_SHOW_PIN MACSTR " %08d%s",
1806 MAC2STR(peer), generated_pin, params);
1807 }
1808
1809
1810 static void wpas_prov_disc_local_keypad(struct wpa_supplicant *wpa_s,
1811 const u8 *peer, const char *params)
1812 {
1813 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_ENTER_PIN MACSTR "%s",
1814 MAC2STR(peer), params);
1815 }
1816
1817
1818 void wpas_prov_disc_req(void *ctx, const u8 *peer, u16 config_methods,
1819 const u8 *dev_addr, const u8 *pri_dev_type,
1820 const char *dev_name, u16 supp_config_methods,
1821 u8 dev_capab, u8 group_capab, const u8 *group_id,
1822 size_t group_id_len)
1823 {
1824 struct wpa_supplicant *wpa_s = ctx;
1825 char devtype[WPS_DEV_TYPE_BUFSIZE];
1826 char params[300];
1827 u8 empty_dev_type[8];
1828 unsigned int generated_pin = 0;
1829 struct wpa_supplicant *group = NULL;
1830
1831 if (group_id) {
1832 for (group = wpa_s->global->ifaces; group; group = group->next)
1833 {
1834 struct wpa_ssid *s = group->current_ssid;
1835 if (s != NULL &&
1836 s->mode == WPAS_MODE_P2P_GO &&
1837 group_id_len - ETH_ALEN == s->ssid_len &&
1838 os_memcmp(group_id + ETH_ALEN, s->ssid,
1839 s->ssid_len) == 0)
1840 break;
1841 }
1842 }
1843
1844 if (pri_dev_type == NULL) {
1845 os_memset(empty_dev_type, 0, sizeof(empty_dev_type));
1846 pri_dev_type = empty_dev_type;
1847 }
1848 os_snprintf(params, sizeof(params), " p2p_dev_addr=" MACSTR
1849 " pri_dev_type=%s name='%s' config_methods=0x%x "
1850 "dev_capab=0x%x group_capab=0x%x%s%s",
1851 MAC2STR(dev_addr),
1852 wps_dev_type_bin2str(pri_dev_type, devtype,
1853 sizeof(devtype)),
1854 dev_name, supp_config_methods, dev_capab, group_capab,
1855 group ? " group=" : "",
1856 group ? group->ifname : "");
1857 params[sizeof(params) - 1] = '\0';
1858
1859 if (config_methods & WPS_CONFIG_DISPLAY) {
1860 generated_pin = wps_generate_pin();
1861 wpas_prov_disc_local_display(wpa_s, peer, params,
1862 generated_pin);
1863 } else if (config_methods & WPS_CONFIG_KEYPAD)
1864 wpas_prov_disc_local_keypad(wpa_s, peer, params);
1865 else if (config_methods & WPS_CONFIG_PUSHBUTTON)
1866 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_PBC_REQ MACSTR
1867 "%s", MAC2STR(peer), params);
1868
1869 wpas_notify_p2p_provision_discovery(wpa_s, peer, 1 /* request */,
1870 P2P_PROV_DISC_SUCCESS,
1871 config_methods, generated_pin);
1872 }
1873
1874
1875 void wpas_prov_disc_resp(void *ctx, const u8 *peer, u16 config_methods)
1876 {
1877 struct wpa_supplicant *wpa_s = ctx;
1878 unsigned int generated_pin = 0;
1879 char params[20];
1880
1881 if (wpa_s->pending_pd_before_join &&
1882 (os_memcmp(peer, wpa_s->pending_join_dev_addr, ETH_ALEN) == 0 ||
1883 os_memcmp(peer, wpa_s->pending_join_iface_addr, ETH_ALEN) == 0)) {
1884 wpa_s->pending_pd_before_join = 0;
1885 wpa_printf(MSG_DEBUG, "P2P: Starting pending "
1886 "join-existing-group operation");
1887 wpas_p2p_join_start(wpa_s);
1888 return;
1889 }
1890
1891 if (wpa_s->pending_pd_use == AUTO_PD_JOIN ||
1892 wpa_s->pending_pd_use == AUTO_PD_GO_NEG)
1893 os_snprintf(params, sizeof(params), " peer_go=%d",
1894 wpa_s->pending_pd_use == AUTO_PD_JOIN);
1895 else
1896 params[0] = '\0';
1897
1898 if (config_methods & WPS_CONFIG_DISPLAY)
1899 wpas_prov_disc_local_keypad(wpa_s, peer, params);
1900 else if (config_methods & WPS_CONFIG_KEYPAD) {
1901 generated_pin = wps_generate_pin();
1902 wpas_prov_disc_local_display(wpa_s, peer, params,
1903 generated_pin);
1904 } else if (config_methods & WPS_CONFIG_PUSHBUTTON)
1905 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_PBC_RESP MACSTR
1906 "%s", MAC2STR(peer), params);
1907
1908 wpas_notify_p2p_provision_discovery(wpa_s, peer, 0 /* response */,
1909 P2P_PROV_DISC_SUCCESS,
1910 config_methods, generated_pin);
1911 }
1912
1913
1914 static void wpas_prov_disc_fail(void *ctx, const u8 *peer,
1915 enum p2p_prov_disc_status status)
1916 {
1917 struct wpa_supplicant *wpa_s = ctx;
1918
1919 if (wpa_s->p2p_fallback_to_go_neg) {
1920 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: PD for p2p_connect-auto "
1921 "failed - fall back to GO Negotiation");
1922 wpas_p2p_fallback_to_go_neg(wpa_s, 0);
1923 return;
1924 }
1925
1926 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_FAILURE
1927 " p2p_dev_addr=" MACSTR " status=%d",
1928 MAC2STR(peer), status);
1929
1930 wpas_notify_p2p_provision_discovery(wpa_s, peer, 0 /* response */,
1931 status, 0, 0);
1932 }
1933
1934
1935 static u8 wpas_invitation_process(void *ctx, const u8 *sa, const u8 *bssid,
1936 const u8 *go_dev_addr, const u8 *ssid,
1937 size_t ssid_len, int *go, u8 *group_bssid,
1938 int *force_freq, int persistent_group)
1939 {
1940 struct wpa_supplicant *wpa_s = ctx;
1941 struct wpa_ssid *s;
1942 u8 cur_bssid[ETH_ALEN];
1943 int res;
1944 struct wpa_supplicant *grp;
1945
1946 if (!persistent_group) {
1947 wpa_printf(MSG_DEBUG, "P2P: Invitation from " MACSTR
1948 " to join an active group", MAC2STR(sa));
1949 if (!is_zero_ether_addr(wpa_s->p2p_auth_invite) &&
1950 (os_memcmp(go_dev_addr, wpa_s->p2p_auth_invite, ETH_ALEN)
1951 == 0 ||
1952 os_memcmp(sa, wpa_s->p2p_auth_invite, ETH_ALEN) == 0)) {
1953 wpa_printf(MSG_DEBUG, "P2P: Accept previously "
1954 "authorized invitation");
1955 goto accept_inv;
1956 }
1957 /*
1958 * Do not accept the invitation automatically; notify user and
1959 * request approval.
1960 */
1961 return P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE;
1962 }
1963
1964 grp = wpas_get_p2p_group(wpa_s, ssid, ssid_len, go);
1965 if (grp) {
1966 wpa_printf(MSG_DEBUG, "P2P: Accept invitation to already "
1967 "running persistent group");
1968 if (*go)
1969 os_memcpy(group_bssid, grp->own_addr, ETH_ALEN);
1970 goto accept_inv;
1971 }
1972
1973 if (!wpa_s->conf->persistent_reconnect)
1974 return P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE;
1975
1976 for (s = wpa_s->conf->ssid; s; s = s->next) {
1977 if (s->disabled == 2 &&
1978 os_memcmp(s->bssid, go_dev_addr, ETH_ALEN) == 0 &&
1979 s->ssid_len == ssid_len &&
1980 os_memcmp(ssid, s->ssid, ssid_len) == 0)
1981 break;
1982 }
1983
1984 if (!s) {
1985 wpa_printf(MSG_DEBUG, "P2P: Invitation from " MACSTR
1986 " requested reinvocation of an unknown group",
1987 MAC2STR(sa));
1988 return P2P_SC_FAIL_UNKNOWN_GROUP;
1989 }
1990
1991 if (s->mode == WPAS_MODE_P2P_GO && !wpas_p2p_create_iface(wpa_s)) {
1992 *go = 1;
1993 if (wpa_s->wpa_state >= WPA_AUTHENTICATING) {
1994 wpa_printf(MSG_DEBUG, "P2P: The only available "
1995 "interface is already in use - reject "
1996 "invitation");
1997 return P2P_SC_FAIL_UNABLE_TO_ACCOMMODATE;
1998 }
1999 os_memcpy(group_bssid, wpa_s->own_addr, ETH_ALEN);
2000 } else if (s->mode == WPAS_MODE_P2P_GO) {
2001 *go = 1;
2002 if (wpas_p2p_add_group_interface(wpa_s, WPA_IF_P2P_GO) < 0)
2003 {
2004 wpa_printf(MSG_ERROR, "P2P: Failed to allocate a new "
2005 "interface address for the group");
2006 return P2P_SC_FAIL_UNABLE_TO_ACCOMMODATE;
2007 }
2008 os_memcpy(group_bssid, wpa_s->pending_interface_addr,
2009 ETH_ALEN);
2010 }
2011
2012 accept_inv:
2013 if (wpa_s->current_ssid && wpa_drv_get_bssid(wpa_s, cur_bssid) == 0 &&
2014 wpa_s->assoc_freq) {
2015 wpa_printf(MSG_DEBUG, "P2P: Trying to force channel to match "
2016 "the channel we are already using");
2017 *force_freq = wpa_s->assoc_freq;
2018 }
2019
2020 res = wpa_drv_shared_freq(wpa_s);
2021 if (res > 0) {
2022 wpa_printf(MSG_DEBUG, "P2P: Trying to force channel to match "
2023 "with the channel we are already using on a "
2024 "shared interface");
2025 *force_freq = res;
2026 }
2027
2028 return P2P_SC_SUCCESS;
2029 }
2030
2031
2032 static void wpas_invitation_received(void *ctx, const u8 *sa, const u8 *bssid,
2033 const u8 *ssid, size_t ssid_len,
2034 const u8 *go_dev_addr, u8 status,
2035 int op_freq)
2036 {
2037 struct wpa_supplicant *wpa_s = ctx;
2038 struct wpa_ssid *s;
2039
2040 for (s = wpa_s->conf->ssid; s; s = s->next) {
2041 if (s->disabled == 2 &&
2042 s->ssid_len == ssid_len &&
2043 os_memcmp(ssid, s->ssid, ssid_len) == 0)
2044 break;
2045 }
2046
2047 if (status == P2P_SC_SUCCESS) {
2048 wpa_printf(MSG_DEBUG, "P2P: Invitation from peer " MACSTR
2049 " was accepted; op_freq=%d MHz",
2050 MAC2STR(sa), op_freq);
2051 if (s) {
2052 wpas_p2p_group_add_persistent(
2053 wpa_s, s, s->mode == WPAS_MODE_P2P_GO, 0, 0);
2054 } else if (bssid) {
2055 wpas_p2p_join(wpa_s, bssid, go_dev_addr,
2056 wpa_s->p2p_wps_method, 0);
2057 }
2058 return;
2059 }
2060
2061 if (status != P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE) {
2062 wpa_printf(MSG_DEBUG, "P2P: Invitation from peer " MACSTR
2063 " was rejected (status %u)", MAC2STR(sa), status);
2064 return;
2065 }
2066
2067 if (!s) {
2068 if (bssid) {
2069 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_INVITATION_RECEIVED
2070 "sa=" MACSTR " go_dev_addr=" MACSTR
2071 " bssid=" MACSTR " unknown-network",
2072 MAC2STR(sa), MAC2STR(go_dev_addr),
2073 MAC2STR(bssid));
2074 } else {
2075 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_INVITATION_RECEIVED
2076 "sa=" MACSTR " go_dev_addr=" MACSTR
2077 " unknown-network",
2078 MAC2STR(sa), MAC2STR(go_dev_addr));
2079 }
2080 return;
2081 }
2082
2083 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_INVITATION_RECEIVED "sa=" MACSTR
2084 " persistent=%d", MAC2STR(sa), s->id);
2085 }
2086
2087
2088 static void wpas_invitation_result(void *ctx, int status, const u8 *bssid)
2089 {
2090 struct wpa_supplicant *wpa_s = ctx;
2091 struct wpa_ssid *ssid;
2092
2093 if (bssid) {
2094 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_INVITATION_RESULT
2095 "status=%d " MACSTR,
2096 status, MAC2STR(bssid));
2097 } else {
2098 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_INVITATION_RESULT
2099 "status=%d ", status);
2100 }
2101 wpas_notify_p2p_invitation_result(wpa_s, status, bssid);
2102
2103 if (wpa_s->pending_invite_ssid_id == -1)
2104 return; /* Invitation to active group */
2105
2106 if (status != P2P_SC_SUCCESS) {
2107 wpas_p2p_remove_pending_group_interface(wpa_s);
2108 return;
2109 }
2110
2111 ssid = wpa_config_get_network(wpa_s->conf,
2112 wpa_s->pending_invite_ssid_id);
2113 if (ssid == NULL) {
2114 wpa_printf(MSG_ERROR, "P2P: Could not find persistent group "
2115 "data matching with invitation");
2116 return;
2117 }
2118
2119 wpas_p2p_group_add_persistent(wpa_s, ssid,
2120 ssid->mode == WPAS_MODE_P2P_GO, 0, 0);
2121 }
2122
2123
2124 static int wpas_p2p_disallowed_freq(struct wpa_global *global,
2125 unsigned int freq)
2126 {
2127 unsigned int i;
2128
2129 if (global->p2p_disallow_freq == NULL)
2130 return 0;
2131
2132 for (i = 0; i < global->num_p2p_disallow_freq; i++) {
2133 if (freq >= global->p2p_disallow_freq[i].min &&
2134 freq <= global->p2p_disallow_freq[i].max)
2135 return 1;
2136 }
2137
2138 return 0;
2139 }
2140
2141
2142 static void wpas_p2p_add_chan(struct p2p_reg_class *reg, u8 chan)
2143 {
2144 reg->channel[reg->channels] = chan;
2145 reg->channels++;
2146 }
2147
2148
2149 static int wpas_p2p_default_channels(struct wpa_supplicant *wpa_s,
2150 struct p2p_channels *chan)
2151 {
2152 int i, cla = 0;
2153
2154 wpa_printf(MSG_DEBUG, "P2P: Enable operating classes for 2.4 GHz "
2155 "band");
2156
2157 /* Operating class 81 - 2.4 GHz band channels 1..13 */
2158 chan->reg_class[cla].reg_class = 81;
2159 chan->reg_class[cla].channels = 0;
2160 for (i = 0; i < 11; i++) {
2161 if (!wpas_p2p_disallowed_freq(wpa_s->global, 2412 + i * 5))
2162 wpas_p2p_add_chan(&chan->reg_class[cla], i + 1);
2163 }
2164 if (chan->reg_class[cla].channels)
2165 cla++;
2166
2167 wpa_printf(MSG_DEBUG, "P2P: Enable operating classes for lower 5 GHz "
2168 "band");
2169
2170 /* Operating class 115 - 5 GHz, channels 36-48 */
2171 chan->reg_class[cla].reg_class = 115;
2172 chan->reg_class[cla].channels = 0;
2173 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 36 * 5))
2174 wpas_p2p_add_chan(&chan->reg_class[cla], 36);
2175 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 40 * 5))
2176 wpas_p2p_add_chan(&chan->reg_class[cla], 40);
2177 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 44 * 5))
2178 wpas_p2p_add_chan(&chan->reg_class[cla], 44);
2179 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 48 * 5))
2180 wpas_p2p_add_chan(&chan->reg_class[cla], 48);
2181 if (chan->reg_class[cla].channels)
2182 cla++;
2183
2184 wpa_printf(MSG_DEBUG, "P2P: Enable operating classes for higher 5 GHz "
2185 "band");
2186
2187 /* Operating class 124 - 5 GHz, channels 149,153,157,161 */
2188 chan->reg_class[cla].reg_class = 124;
2189 chan->reg_class[cla].channels = 0;
2190 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 149 * 5))
2191 wpas_p2p_add_chan(&chan->reg_class[cla], 149);
2192 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 153 * 5))
2193 wpas_p2p_add_chan(&chan->reg_class[cla], 153);
2194 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 156 * 5))
2195 wpas_p2p_add_chan(&chan->reg_class[cla], 157);
2196 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 161 * 5))
2197 wpas_p2p_add_chan(&chan->reg_class[cla], 161);
2198 if (chan->reg_class[cla].channels)
2199 cla++;
2200
2201 chan->reg_classes = cla;
2202 return 0;
2203 }
2204
2205
2206 static struct hostapd_hw_modes * get_mode(struct hostapd_hw_modes *modes,
2207 u16 num_modes,
2208 enum hostapd_hw_mode mode)
2209 {
2210 u16 i;
2211
2212 for (i = 0; i < num_modes; i++) {
2213 if (modes[i].mode == mode)
2214 return &modes[i];
2215 }
2216
2217 return NULL;
2218 }
2219
2220
2221 static int has_channel(struct wpa_global *global,
2222 struct hostapd_hw_modes *mode, u8 chan, int *flags)
2223 {
2224 int i;
2225 unsigned int freq;
2226
2227 freq = (mode->mode == HOSTAPD_MODE_IEEE80211A ? 5000 : 2407) +
2228 chan * 5;
2229 if (wpas_p2p_disallowed_freq(global, freq))
2230 return 0;
2231
2232 for (i = 0; i < mode->num_channels; i++) {
2233 if (mode->channels[i].chan == chan) {
2234 if (flags)
2235 *flags = mode->channels[i].flag;
2236 return !(mode->channels[i].flag &
2237 (HOSTAPD_CHAN_DISABLED |
2238 HOSTAPD_CHAN_PASSIVE_SCAN |
2239 HOSTAPD_CHAN_NO_IBSS |
2240 HOSTAPD_CHAN_RADAR));
2241 }
2242 }
2243
2244 return 0;
2245 }
2246
2247
2248 struct p2p_oper_class_map {
2249 enum hostapd_hw_mode mode;
2250 u8 op_class;
2251 u8 min_chan;
2252 u8 max_chan;
2253 u8 inc;
2254 enum { BW20, BW40PLUS, BW40MINUS } bw;
2255 };
2256
2257 static struct p2p_oper_class_map op_class[] = {
2258 { HOSTAPD_MODE_IEEE80211G, 81, 1, 13, 1, BW20 },
2259 { HOSTAPD_MODE_IEEE80211G, 82, 14, 14, 1, BW20 },
2260 #if 0 /* Do not enable HT40 on 2 GHz for now */
2261 { HOSTAPD_MODE_IEEE80211G, 83, 1, 9, 1, BW40PLUS },
2262 { HOSTAPD_MODE_IEEE80211G, 84, 5, 13, 1, BW40MINUS },
2263 #endif
2264 { HOSTAPD_MODE_IEEE80211A, 115, 36, 48, 4, BW20 },
2265 { HOSTAPD_MODE_IEEE80211A, 124, 149, 161, 4, BW20 },
2266 { HOSTAPD_MODE_IEEE80211A, 116, 36, 44, 8, BW40PLUS },
2267 { HOSTAPD_MODE_IEEE80211A, 117, 40, 48, 8, BW40MINUS },
2268 { HOSTAPD_MODE_IEEE80211A, 126, 149, 157, 8, BW40PLUS },
2269 { HOSTAPD_MODE_IEEE80211A, 127, 153, 161, 8, BW40MINUS },
2270 { -1, 0, 0, 0, 0, BW20 }
2271 };
2272
2273
2274 static int wpas_p2p_verify_channel(struct wpa_supplicant *wpa_s,
2275 struct hostapd_hw_modes *mode,
2276 u8 channel, u8 bw)
2277 {
2278 int flag;
2279
2280 if (!has_channel(wpa_s->global, mode, channel, &flag))
2281 return -1;
2282 if (bw == BW40MINUS &&
2283 (!(flag & HOSTAPD_CHAN_HT40MINUS) ||
2284 !has_channel(wpa_s->global, mode, channel - 4, NULL)))
2285 return 0;
2286 if (bw == BW40PLUS &&
2287 (!(flag & HOSTAPD_CHAN_HT40PLUS) ||
2288 !has_channel(wpa_s->global, mode, channel + 4, NULL)))
2289 return 0;
2290 return 1;
2291 }
2292
2293
2294 static int wpas_p2p_setup_channels(struct wpa_supplicant *wpa_s,
2295 struct p2p_channels *chan)
2296 {
2297 struct hostapd_hw_modes *mode;
2298 int cla, op;
2299
2300 if (wpa_s->hw.modes == NULL) {
2301 wpa_printf(MSG_DEBUG, "P2P: Driver did not support fetching "
2302 "of all supported channels; assume dualband "
2303 "support");
2304 return wpas_p2p_default_channels(wpa_s, chan);
2305 }
2306
2307 cla = 0;
2308
2309 for (op = 0; op_class[op].op_class; op++) {
2310 struct p2p_oper_class_map *o = &op_class[op];
2311 u8 ch;
2312 struct p2p_reg_class *reg = NULL;
2313
2314 mode = get_mode(wpa_s->hw.modes, wpa_s->hw.num_modes, o->mode);
2315 if (mode == NULL)
2316 continue;
2317 for (ch = o->min_chan; ch <= o->max_chan; ch += o->inc) {
2318 if (wpas_p2p_verify_channel(wpa_s, mode, ch, o->bw) < 1)
2319 continue;
2320 if (reg == NULL) {
2321 wpa_printf(MSG_DEBUG, "P2P: Add operating "
2322 "class %u", o->op_class);
2323 reg = &chan->reg_class[cla];
2324 cla++;
2325 reg->reg_class = o->op_class;
2326 }
2327 reg->channel[reg->channels] = ch;
2328 reg->channels++;
2329 }
2330 if (reg) {
2331 wpa_hexdump(MSG_DEBUG, "P2P: Channels",
2332 reg->channel, reg->channels);
2333 }
2334 }
2335
2336 chan->reg_classes = cla;
2337
2338 return 0;
2339 }
2340
2341
2342 int wpas_p2p_get_ht40_mode(struct wpa_supplicant *wpa_s,
2343 struct hostapd_hw_modes *mode, u8 channel)
2344 {
2345 int op, ret;
2346
2347 for (op = 0; op_class[op].op_class; op++) {
2348 struct p2p_oper_class_map *o = &op_class[op];
2349 u8 ch;
2350
2351 for (ch = o->min_chan; ch <= o->max_chan; ch += o->inc) {
2352 if (o->mode != HOSTAPD_MODE_IEEE80211A ||
2353 o->bw == BW20 || ch != channel)
2354 continue;
2355 ret = wpas_p2p_verify_channel(wpa_s, mode, ch, o->bw);
2356 if (ret < 0)
2357 continue;
2358 else if (ret > 0)
2359 return (o->bw == BW40MINUS) ? -1 : 1;
2360 else
2361 return 0;
2362 }
2363 }
2364 return 0;
2365 }
2366
2367
2368 static int wpas_get_noa(void *ctx, const u8 *interface_addr, u8 *buf,
2369 size_t buf_len)
2370 {
2371 struct wpa_supplicant *wpa_s = ctx;
2372
2373 for (wpa_s = wpa_s->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
2374 if (os_memcmp(wpa_s->own_addr, interface_addr, ETH_ALEN) == 0)
2375 break;
2376 }
2377 if (wpa_s == NULL)
2378 return -1;
2379
2380 return wpa_drv_get_noa(wpa_s, buf, buf_len);
2381 }
2382
2383
2384 static int wpas_go_connected(void *ctx, const u8 *dev_addr)
2385 {
2386 struct wpa_supplicant *wpa_s = ctx;
2387
2388 for (wpa_s = wpa_s->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
2389 struct wpa_ssid *ssid = wpa_s->current_ssid;
2390 if (ssid == NULL)
2391 continue;
2392 if (ssid->mode != WPAS_MODE_INFRA)
2393 continue;
2394 if (wpa_s->wpa_state != WPA_COMPLETED &&
2395 wpa_s->wpa_state != WPA_GROUP_HANDSHAKE)
2396 continue;
2397 if (os_memcmp(wpa_s->go_dev_addr, dev_addr, ETH_ALEN) == 0)
2398 return 1;
2399 }
2400
2401 return 0;
2402 }
2403
2404
2405 /**
2406 * wpas_p2p_init - Initialize P2P module for %wpa_supplicant
2407 * @global: Pointer to global data from wpa_supplicant_init()
2408 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
2409 * Returns: 0 on success, -1 on failure
2410 */
2411 int wpas_p2p_init(struct wpa_global *global, struct wpa_supplicant *wpa_s)
2412 {
2413 struct p2p_config p2p;
2414 unsigned int r;
2415 int i;
2416
2417 if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_CAPABLE))
2418 return 0;
2419
2420 if (global->p2p)
2421 return 0;
2422
2423 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT) {
2424 struct p2p_params params;
2425
2426 wpa_printf(MSG_DEBUG, "P2P: Use driver-based P2P management");
2427 os_memset(&params, 0, sizeof(params));
2428 params.dev_name = wpa_s->conf->device_name;
2429 os_memcpy(params.pri_dev_type, wpa_s->conf->device_type,
2430 WPS_DEV_TYPE_LEN);
2431 params.num_sec_dev_types = wpa_s->conf->num_sec_device_types;
2432 os_memcpy(params.sec_dev_type,
2433 wpa_s->conf->sec_device_type,
2434 params.num_sec_dev_types * WPS_DEV_TYPE_LEN);
2435
2436 if (wpa_drv_p2p_set_params(wpa_s, &params) < 0)
2437 return -1;
2438
2439 return 0;
2440 }
2441
2442 os_memset(&p2p, 0, sizeof(p2p));
2443 p2p.msg_ctx = wpa_s;
2444 p2p.cb_ctx = wpa_s;
2445 p2p.p2p_scan = wpas_p2p_scan;
2446 p2p.send_action = wpas_send_action;
2447 p2p.send_action_done = wpas_send_action_done;
2448 p2p.go_neg_completed = wpas_go_neg_completed;
2449 p2p.go_neg_req_rx = wpas_go_neg_req_rx;
2450 p2p.dev_found = wpas_dev_found;
2451 p2p.dev_lost = wpas_dev_lost;
2452 p2p.start_listen = wpas_start_listen;
2453 p2p.stop_listen = wpas_stop_listen;
2454 p2p.send_probe_resp = wpas_send_probe_resp;
2455 p2p.sd_request = wpas_sd_request;
2456 p2p.sd_response = wpas_sd_response;
2457 p2p.prov_disc_req = wpas_prov_disc_req;
2458 p2p.prov_disc_resp = wpas_prov_disc_resp;
2459 p2p.prov_disc_fail = wpas_prov_disc_fail;
2460 p2p.invitation_process = wpas_invitation_process;
2461 p2p.invitation_received = wpas_invitation_received;
2462 p2p.invitation_result = wpas_invitation_result;
2463 p2p.get_noa = wpas_get_noa;
2464 p2p.go_connected = wpas_go_connected;
2465
2466 os_memcpy(wpa_s->global->p2p_dev_addr, wpa_s->own_addr, ETH_ALEN);
2467 os_memcpy(p2p.dev_addr, wpa_s->global->p2p_dev_addr, ETH_ALEN);
2468 p2p.dev_name = wpa_s->conf->device_name;
2469 p2p.manufacturer = wpa_s->conf->manufacturer;
2470 p2p.model_name = wpa_s->conf->model_name;
2471 p2p.model_number = wpa_s->conf->model_number;
2472 p2p.serial_number = wpa_s->conf->serial_number;
2473 if (wpa_s->wps) {
2474 os_memcpy(p2p.uuid, wpa_s->wps->uuid, 16);
2475 p2p.config_methods = wpa_s->wps->config_methods;
2476 }
2477
2478 if (wpa_s->conf->p2p_listen_reg_class &&
2479 wpa_s->conf->p2p_listen_channel) {
2480 p2p.reg_class = wpa_s->conf->p2p_listen_reg_class;
2481 p2p.channel = wpa_s->conf->p2p_listen_channel;
2482 } else {
2483 p2p.reg_class = 81;
2484 /*
2485 * Pick one of the social channels randomly as the listen
2486 * channel.
2487 */
2488 os_get_random((u8 *) &r, sizeof(r));
2489 p2p.channel = 1 + (r % 3) * 5;
2490 }
2491 wpa_printf(MSG_DEBUG, "P2P: Own listen channel: %d", p2p.channel);
2492
2493 if (wpa_s->conf->p2p_oper_reg_class &&
2494 wpa_s->conf->p2p_oper_channel) {
2495 p2p.op_reg_class = wpa_s->conf->p2p_oper_reg_class;
2496 p2p.op_channel = wpa_s->conf->p2p_oper_channel;
2497 p2p.cfg_op_channel = 1;
2498 wpa_printf(MSG_DEBUG, "P2P: Configured operating channel: "
2499 "%d:%d", p2p.op_reg_class, p2p.op_channel);
2500
2501 } else {
2502 p2p.op_reg_class = 81;
2503 /*
2504 * Use random operation channel from (1, 6, 11) if no other
2505 * preference is indicated.
2506 */
2507 os_get_random((u8 *) &r, sizeof(r));
2508 p2p.op_channel = 1 + (r % 3) * 5;
2509 p2p.cfg_op_channel = 0;
2510 wpa_printf(MSG_DEBUG, "P2P: Random operating channel: "
2511 "%d:%d", p2p.op_reg_class, p2p.op_channel);
2512 }
2513 if (wpa_s->conf->country[0] && wpa_s->conf->country[1]) {
2514 os_memcpy(p2p.country, wpa_s->conf->country, 2);
2515 p2p.country[2] = 0x04;
2516 } else
2517 os_memcpy(p2p.country, "XX\x04", 3);
2518
2519 if (wpas_p2p_setup_channels(wpa_s, &p2p.channels)) {
2520 wpa_printf(MSG_ERROR, "P2P: Failed to configure supported "
2521 "channel list");
2522 return -1;
2523 }
2524
2525 os_memcpy(p2p.pri_dev_type, wpa_s->conf->device_type,
2526 WPS_DEV_TYPE_LEN);
2527
2528 p2p.num_sec_dev_types = wpa_s->conf->num_sec_device_types;
2529 os_memcpy(p2p.sec_dev_type, wpa_s->conf->sec_device_type,
2530 p2p.num_sec_dev_types * WPS_DEV_TYPE_LEN);
2531
2532 p2p.concurrent_operations = !!(wpa_s->drv_flags &
2533 WPA_DRIVER_FLAGS_P2P_CONCURRENT);
2534
2535 p2p.max_peers = 100;
2536
2537 if (wpa_s->conf->p2p_ssid_postfix) {
2538 p2p.ssid_postfix_len =
2539 os_strlen(wpa_s->conf->p2p_ssid_postfix);
2540 if (p2p.ssid_postfix_len > sizeof(p2p.ssid_postfix))
2541 p2p.ssid_postfix_len = sizeof(p2p.ssid_postfix);
2542 os_memcpy(p2p.ssid_postfix, wpa_s->conf->p2p_ssid_postfix,
2543 p2p.ssid_postfix_len);
2544 }
2545
2546 p2p.p2p_intra_bss = wpa_s->conf->p2p_intra_bss;
2547
2548 global->p2p = p2p_init(&p2p);
2549 if (global->p2p == NULL)
2550 return -1;
2551 global->p2p_init_wpa_s = wpa_s;
2552
2553 for (i = 0; i < MAX_WPS_VENDOR_EXT; i++) {
2554 if (wpa_s->conf->wps_vendor_ext[i] == NULL)
2555 continue;
2556 p2p_add_wps_vendor_extension(
2557 global->p2p, wpa_s->conf->wps_vendor_ext[i]);
2558 }
2559
2560 return 0;
2561 }
2562
2563
2564 /**
2565 * wpas_p2p_deinit - Deinitialize per-interface P2P data
2566 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
2567 *
2568 * This function deinitialize per-interface P2P data.
2569 */
2570 void wpas_p2p_deinit(struct wpa_supplicant *wpa_s)
2571 {
2572 if (wpa_s->driver && wpa_s->drv_priv)
2573 wpa_drv_probe_req_report(wpa_s, 0);
2574
2575 if (wpa_s->go_params) {
2576 /* Clear any stored provisioning info */
2577 p2p_clear_provisioning_info(
2578 wpa_s->global->p2p,
2579 wpa_s->go_params->peer_device_addr);
2580 }
2581
2582 os_free(wpa_s->go_params);
2583 wpa_s->go_params = NULL;
2584 eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s, NULL);
2585 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
2586 eloop_cancel_timeout(wpas_p2p_pd_before_join_timeout, wpa_s, NULL);
2587 wpa_s->p2p_long_listen = 0;
2588 eloop_cancel_timeout(wpas_p2p_long_listen_timeout, wpa_s, NULL);
2589 eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL);
2590 wpas_p2p_remove_pending_group_interface(wpa_s);
2591
2592 /* TODO: remove group interface from the driver if this wpa_s instance
2593 * is on top of a P2P group interface */
2594 }
2595
2596
2597 /**
2598 * wpas_p2p_deinit_global - Deinitialize global P2P module
2599 * @global: Pointer to global data from wpa_supplicant_init()
2600 *
2601 * This function deinitializes the global (per device) P2P module.
2602 */
2603 void wpas_p2p_deinit_global(struct wpa_global *global)
2604 {
2605 struct wpa_supplicant *wpa_s, *tmp;
2606
2607 if (global->p2p == NULL)
2608 return;
2609
2610 /* Remove remaining P2P group interfaces */
2611 wpa_s = global->ifaces;
2612 if (wpa_s)
2613 wpas_p2p_service_flush(wpa_s);
2614 while (wpa_s && wpa_s->p2p_group_interface != NOT_P2P_GROUP_INTERFACE)
2615 wpa_s = wpa_s->next;
2616 while (wpa_s) {
2617 tmp = global->ifaces;
2618 while (tmp &&
2619 (tmp == wpa_s ||
2620 tmp->p2p_group_interface == NOT_P2P_GROUP_INTERFACE)) {
2621 tmp = tmp->next;
2622 }
2623 if (tmp == NULL)
2624 break;
2625 /* Disconnect from the P2P group and deinit the interface */
2626 wpas_p2p_disconnect(tmp);
2627 }
2628
2629 /*
2630 * Deinit GO data on any possibly remaining interface (if main
2631 * interface is used as GO).
2632 */
2633 for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
2634 if (wpa_s->ap_iface)
2635 wpas_p2p_group_deinit(wpa_s);
2636 }
2637
2638 p2p_deinit(global->p2p);
2639 global->p2p = NULL;
2640 global->p2p_init_wpa_s = NULL;
2641 }
2642
2643
2644 static int wpas_p2p_create_iface(struct wpa_supplicant *wpa_s)
2645 {
2646 if (wpa_s->drv_flags &
2647 (WPA_DRIVER_FLAGS_P2P_DEDICATED_INTERFACE |
2648 WPA_DRIVER_FLAGS_P2P_MGMT_AND_NON_P2P))
2649 return 1; /* P2P group requires a new interface in every case
2650 */
2651 if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_CONCURRENT))
2652 return 0; /* driver does not support concurrent operations */
2653 if (wpa_s->global->ifaces->next)
2654 return 1; /* more that one interface already in use */
2655 if (wpa_s->wpa_state >= WPA_AUTHENTICATING)
2656 return 1; /* this interface is already in use */
2657 return 0;
2658 }
2659
2660
2661 static int wpas_p2p_start_go_neg(struct wpa_supplicant *wpa_s,
2662 const u8 *peer_addr,
2663 enum p2p_wps_method wps_method,
2664 int go_intent, const u8 *own_interface_addr,
2665 unsigned int force_freq, int persistent_group,
2666 struct wpa_ssid *ssid)
2667 {
2668 if (persistent_group && wpa_s->conf->persistent_reconnect)
2669 persistent_group = 2;
2670
2671 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT) {
2672 return wpa_drv_p2p_connect(wpa_s, peer_addr, wps_method,
2673 go_intent, own_interface_addr,
2674 force_freq, persistent_group);
2675 }
2676
2677 /*
2678 * Increase GO config timeout if HT40 is used since it takes some time
2679 * to scan channels for coex purposes before the BSS can be started.
2680 */
2681 p2p_set_config_timeout(wpa_s->global->p2p,
2682 wpa_s->p2p_go_ht40 ? 255 : 100, 20);
2683
2684 return p2p_connect(wpa_s->global->p2p, peer_addr, wps_method,
2685 go_intent, own_interface_addr, force_freq,
2686 persistent_group, ssid ? ssid->ssid : NULL,
2687 ssid ? ssid->ssid_len : 0,
2688 wpa_s->p2p_pd_before_go_neg);
2689 }
2690
2691
2692 static int wpas_p2p_auth_go_neg(struct wpa_supplicant *wpa_s,
2693 const u8 *peer_addr,
2694 enum p2p_wps_method wps_method,
2695 int go_intent, const u8 *own_interface_addr,
2696 unsigned int force_freq, int persistent_group,
2697 struct wpa_ssid *ssid)
2698 {
2699 if (persistent_group && wpa_s->conf->persistent_reconnect)
2700 persistent_group = 2;
2701
2702 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
2703 return -1;
2704
2705 return p2p_authorize(wpa_s->global->p2p, peer_addr, wps_method,
2706 go_intent, own_interface_addr, force_freq,
2707 persistent_group, ssid ? ssid->ssid : NULL,
2708 ssid ? ssid->ssid_len : 0);
2709 }
2710
2711
2712 static void wpas_p2p_check_join_scan_limit(struct wpa_supplicant *wpa_s)
2713 {
2714 wpa_s->p2p_join_scan_count++;
2715 wpa_printf(MSG_DEBUG, "P2P: Join scan attempt %d",
2716 wpa_s->p2p_join_scan_count);
2717 if (wpa_s->p2p_join_scan_count > P2P_MAX_JOIN_SCAN_ATTEMPTS) {
2718 wpa_printf(MSG_DEBUG, "P2P: Failed to find GO " MACSTR
2719 " for join operationg - stop join attempt",
2720 MAC2STR(wpa_s->pending_join_iface_addr));
2721 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
2722 if (wpa_s->p2p_auto_pd) {
2723 wpa_s->p2p_auto_pd = 0;
2724 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_FAILURE
2725 " p2p_dev_addr=" MACSTR " status=N/A",
2726 MAC2STR(wpa_s->pending_join_dev_addr));
2727 return;
2728 }
2729 wpa_msg(wpa_s->parent, MSG_INFO,
2730 P2P_EVENT_GROUP_FORMATION_FAILURE);
2731 }
2732 }
2733
2734
2735 static void wpas_p2p_pd_before_join_timeout(void *eloop_ctx, void *timeout_ctx)
2736 {
2737 struct wpa_supplicant *wpa_s = eloop_ctx;
2738 if (!wpa_s->pending_pd_before_join)
2739 return;
2740 /*
2741 * Provision Discovery Response may have been lost - try to connect
2742 * anyway since we do not need any information from this PD.
2743 */
2744 wpa_printf(MSG_DEBUG, "P2P: PD timeout for join-existing-group - "
2745 "try to connect anyway");
2746 wpas_p2p_join_start(wpa_s);
2747 }
2748
2749
2750 static int wpas_check_freq_conflict(struct wpa_supplicant *wpa_s, int freq)
2751 {
2752 struct wpa_supplicant *iface;
2753 int shared_freq;
2754 u8 bssid[ETH_ALEN];
2755
2756 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_MULTI_CHANNEL_CONCURRENT)
2757 return 0;
2758
2759 for (iface = wpa_s->global->ifaces; iface; iface = iface->next) {
2760 if (!wpas_p2p_create_iface(wpa_s) && iface == wpa_s)
2761 continue;
2762 if (iface->current_ssid == NULL || iface->assoc_freq == 0)
2763 continue;
2764 if (iface->current_ssid->mode == WPAS_MODE_AP ||
2765 iface->current_ssid->mode == WPAS_MODE_P2P_GO)
2766 shared_freq = iface->current_ssid->frequency;
2767 else if (wpa_drv_get_bssid(iface, bssid) == 0)
2768 shared_freq = iface->assoc_freq;
2769 else
2770 shared_freq = 0;
2771
2772 if (shared_freq && freq != shared_freq) {
2773 wpa_printf(MSG_DEBUG, "P2P: Frequency conflict - %s "
2774 "connected on %d MHz - new connection on "
2775 "%d MHz", iface->ifname, shared_freq, freq);
2776 return 1;
2777 }
2778 }
2779
2780 shared_freq = wpa_drv_shared_freq(wpa_s);
2781 if (shared_freq > 0 && shared_freq != freq) {
2782 wpa_printf(MSG_DEBUG, "P2P: Frequency conflict - shared "
2783 "virtual interface connected on %d MHz - new "
2784 "connection on %d MHz", shared_freq, freq);
2785 return 1;
2786 }
2787
2788 return 0;
2789 }
2790
2791
2792 static int wpas_p2p_peer_go(struct wpa_supplicant *wpa_s,
2793 const u8 *peer_dev_addr)
2794 {
2795 struct wpa_bss *bss;
2796 int updated;
2797
2798 bss = wpa_bss_get_p2p_dev_addr(wpa_s, peer_dev_addr);
2799 if (bss == NULL)
2800 return -1;
2801 if (bss->last_update_idx < wpa_s->bss_update_idx) {
2802 wpa_printf(MSG_DEBUG, "P2P: Peer BSS entry not updated in the "
2803 "last scan");
2804 return 0;
2805 }
2806
2807 updated = os_time_before(&wpa_s->p2p_auto_started, &bss->last_update);
2808 wpa_printf(MSG_DEBUG, "P2P: Current BSS entry for peer updated at "
2809 "%ld.%06ld (%supdated in last scan)",
2810 bss->last_update.sec, bss->last_update.usec,
2811 updated ? "": "not ");
2812
2813 return updated;
2814 }
2815
2816
2817 static void wpas_p2p_scan_res_join(struct wpa_supplicant *wpa_s,
2818 struct wpa_scan_results *scan_res)
2819 {
2820 struct wpa_bss *bss;
2821 int freq;
2822 u8 iface_addr[ETH_ALEN];
2823
2824 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
2825
2826 if (wpa_s->global->p2p_disabled)
2827 return;
2828
2829 wpa_printf(MSG_DEBUG, "P2P: Scan results received (%d BSS) for %sjoin",
2830 scan_res ? (int) scan_res->num : -1,
2831 wpa_s->p2p_auto_join ? "auto_" : "");
2832
2833 if (scan_res)
2834 wpas_p2p_scan_res_handler(wpa_s, scan_res);
2835
2836 if (wpa_s->p2p_auto_pd) {
2837 int join = wpas_p2p_peer_go(wpa_s,
2838 wpa_s->pending_join_dev_addr);
2839 if (join == 0 &&
2840 wpa_s->auto_pd_scan_retry < P2P_AUTO_PD_SCAN_ATTEMPTS) {
2841 wpa_s->auto_pd_scan_retry++;
2842 bss = wpa_bss_get_bssid(wpa_s,
2843 wpa_s->pending_join_dev_addr);
2844 if (bss) {
2845 freq = bss->freq;
2846 wpa_printf(MSG_DEBUG, "P2P: Scan retry %d for "
2847 "the peer " MACSTR " at %d MHz",
2848 wpa_s->auto_pd_scan_retry,
2849 MAC2STR(wpa_s->
2850 pending_join_dev_addr),
2851 freq);
2852 wpas_p2p_join_scan_req(wpa_s, freq);
2853 return;
2854 }
2855 }
2856
2857 if (join < 0)
2858 join = 0;
2859
2860 wpa_s->p2p_auto_pd = 0;
2861 wpa_s->pending_pd_use = join ? AUTO_PD_JOIN : AUTO_PD_GO_NEG;
2862 wpa_printf(MSG_DEBUG, "P2P: Auto PD with " MACSTR " join=%d",
2863 MAC2STR(wpa_s->pending_join_dev_addr), join);
2864 if (p2p_prov_disc_req(wpa_s->global->p2p,
2865 wpa_s->pending_join_dev_addr,
2866 wpa_s->pending_pd_config_methods, join,
2867 0) < 0) {
2868 wpa_s->p2p_auto_pd = 0;
2869 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_FAILURE
2870 " p2p_dev_addr=" MACSTR " status=N/A",
2871 MAC2STR(wpa_s->pending_join_dev_addr));
2872 }
2873 return;
2874 }
2875
2876 if (wpa_s->p2p_auto_join) {
2877 int join = wpas_p2p_peer_go(wpa_s,
2878 wpa_s->pending_join_dev_addr);
2879 if (join < 0) {
2880 wpa_printf(MSG_DEBUG, "P2P: Peer was not found to be "
2881 "running a GO -> use GO Negotiation");
2882 wpas_p2p_connect(wpa_s, wpa_s->pending_join_dev_addr,
2883 wpa_s->p2p_pin, wpa_s->p2p_wps_method,
2884 wpa_s->p2p_persistent_group, 0, 0, 0,
2885 wpa_s->p2p_go_intent,
2886 wpa_s->p2p_connect_freq,
2887 wpa_s->p2p_persistent_id,
2888 wpa_s->p2p_pd_before_go_neg,
2889 wpa_s->p2p_go_ht40);
2890 return;
2891 }
2892
2893 wpa_printf(MSG_DEBUG, "P2P: Peer was found running GO%s -> "
2894 "try to join the group", join ? "" :
2895 " in older scan");
2896 if (!join)
2897 wpa_s->p2p_fallback_to_go_neg = 1;
2898 }
2899
2900 freq = p2p_get_oper_freq(wpa_s->global->p2p,
2901 wpa_s->pending_join_iface_addr);
2902 if (freq < 0 &&
2903 p2p_get_interface_addr(wpa_s->global->p2p,
2904 wpa_s->pending_join_dev_addr,
2905 iface_addr) == 0 &&
2906 os_memcmp(iface_addr, wpa_s->pending_join_dev_addr, ETH_ALEN) != 0)
2907 {
2908 wpa_printf(MSG_DEBUG, "P2P: Overwrite pending interface "
2909 "address for join from " MACSTR " to " MACSTR
2910 " based on newly discovered P2P peer entry",
2911 MAC2STR(wpa_s->pending_join_iface_addr),
2912 MAC2STR(iface_addr));
2913 os_memcpy(wpa_s->pending_join_iface_addr, iface_addr,
2914 ETH_ALEN);
2915
2916 freq = p2p_get_oper_freq(wpa_s->global->p2p,
2917 wpa_s->pending_join_iface_addr);
2918 }
2919 if (freq >= 0) {
2920 wpa_printf(MSG_DEBUG, "P2P: Target GO operating frequency "
2921 "from P2P peer table: %d MHz", freq);
2922 }
2923 bss = wpa_bss_get_bssid(wpa_s, wpa_s->pending_join_iface_addr);
2924 if (bss) {
2925 freq = bss->freq;
2926 wpa_printf(MSG_DEBUG, "P2P: Target GO operating frequency "
2927 "from BSS table: %d MHz", freq);
2928 }
2929 if (freq > 0) {
2930 u16 method;
2931
2932 if (wpas_check_freq_conflict(wpa_s, freq) > 0) {
2933 wpa_msg(wpa_s->parent, MSG_INFO,
2934 P2P_EVENT_GROUP_FORMATION_FAILURE
2935 "reason=FREQ_CONFLICT");
2936 return;
2937 }
2938
2939 wpa_printf(MSG_DEBUG, "P2P: Send Provision Discovery Request "
2940 "prior to joining an existing group (GO " MACSTR
2941 " freq=%u MHz)",
2942 MAC2STR(wpa_s->pending_join_dev_addr), freq);
2943 wpa_s->pending_pd_before_join = 1;
2944
2945 switch (wpa_s->pending_join_wps_method) {
2946 case WPS_PIN_DISPLAY:
2947 method = WPS_CONFIG_KEYPAD;
2948 break;
2949 case WPS_PIN_KEYPAD:
2950 method = WPS_CONFIG_DISPLAY;
2951 break;
2952 case WPS_PBC:
2953 method = WPS_CONFIG_PUSHBUTTON;
2954 break;
2955 default:
2956 method = 0;
2957 break;
2958 }
2959
2960 if ((p2p_get_provisioning_info(wpa_s->global->p2p,
2961 wpa_s->pending_join_dev_addr) ==
2962 method)) {
2963 /*
2964 * We have already performed provision discovery for
2965 * joining the group. Proceed directly to join
2966 * operation without duplicated provision discovery. */
2967 wpa_printf(MSG_DEBUG, "P2P: Provision discovery "
2968 "with " MACSTR " already done - proceed to "
2969 "join",
2970 MAC2STR(wpa_s->pending_join_dev_addr));
2971 wpa_s->pending_pd_before_join = 0;
2972 goto start;
2973 }
2974
2975 if (p2p_prov_disc_req(wpa_s->global->p2p,
2976 wpa_s->pending_join_dev_addr, method, 1,
2977 freq) < 0) {
2978 wpa_printf(MSG_DEBUG, "P2P: Failed to send Provision "
2979 "Discovery Request before joining an "
2980 "existing group");
2981 wpa_s->pending_pd_before_join = 0;
2982 goto start;
2983 }
2984
2985 /*
2986 * Actual join operation will be started from the Action frame
2987 * TX status callback (if no ACK is received) or when the
2988 * Provision Discovery Response is received. Use a short
2989 * timeout as a backup mechanism should the Provision Discovery
2990 * Response be lost for any reason.
2991 */
2992 eloop_cancel_timeout(wpas_p2p_pd_before_join_timeout, wpa_s,
2993 NULL);
2994 eloop_register_timeout(2, 0, wpas_p2p_pd_before_join_timeout,
2995 wpa_s, NULL);
2996 return;
2997 }
2998
2999 wpa_printf(MSG_DEBUG, "P2P: Failed to find BSS/GO - try again later");
3000 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
3001 eloop_register_timeout(1, 0, wpas_p2p_join_scan, wpa_s, NULL);
3002 wpas_p2p_check_join_scan_limit(wpa_s);
3003 return;
3004
3005 start:
3006 /* Start join operation immediately */
3007 wpas_p2p_join_start(wpa_s);
3008 }
3009
3010
3011 static void wpas_p2p_join_scan_req(struct wpa_supplicant *wpa_s, int freq)
3012 {
3013 int ret;
3014 struct wpa_driver_scan_params params;
3015 struct wpabuf *wps_ie, *ies;
3016 size_t ielen;
3017 int freqs[2] = { 0, 0 };
3018
3019 os_memset(&params, 0, sizeof(params));
3020
3021 /* P2P Wildcard SSID */
3022 params.num_ssids = 1;
3023 params.ssids[0].ssid = (u8 *) P2P_WILDCARD_SSID;
3024 params.ssids[0].ssid_len = P2P_WILDCARD_SSID_LEN;
3025
3026 wpa_s->wps->dev.p2p = 1;
3027 wps_ie = wps_build_probe_req_ie(DEV_PW_DEFAULT, &wpa_s->wps->dev,
3028 wpa_s->wps->uuid, WPS_REQ_ENROLLEE, 0,
3029 NULL);
3030 if (wps_ie == NULL) {
3031 wpas_p2p_scan_res_join(wpa_s, NULL);
3032 return;
3033 }
3034
3035 ielen = p2p_scan_ie_buf_len(wpa_s->global->p2p);
3036 ies = wpabuf_alloc(wpabuf_len(wps_ie) + ielen);
3037 if (ies == NULL) {
3038 wpabuf_free(wps_ie);
3039 wpas_p2p_scan_res_join(wpa_s, NULL);
3040 return;
3041 }
3042 wpabuf_put_buf(ies, wps_ie);
3043 wpabuf_free(wps_ie);
3044
3045 p2p_scan_ie(wpa_s->global->p2p, ies, NULL);
3046
3047 params.p2p_probe = 1;
3048 params.extra_ies = wpabuf_head(ies);
3049 params.extra_ies_len = wpabuf_len(ies);
3050 if (freq > 0) {
3051 freqs[0] = freq;
3052 params.freqs = freqs;
3053 }
3054
3055 /*
3056 * Run a scan to update BSS table and start Provision Discovery once
3057 * the new scan results become available.
3058 */
3059 ret = wpa_drv_scan(wpa_s, &params);
3060 if (!ret)
3061 wpa_s->scan_res_handler = wpas_p2p_scan_res_join;
3062
3063 wpabuf_free(ies);
3064
3065 if (ret) {
3066 wpa_printf(MSG_DEBUG, "P2P: Failed to start scan for join - "
3067 "try again later");
3068 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
3069 eloop_register_timeout(1, 0, wpas_p2p_join_scan, wpa_s, NULL);
3070 wpas_p2p_check_join_scan_limit(wpa_s);
3071 }
3072 }
3073
3074
3075 static void wpas_p2p_join_scan(void *eloop_ctx, void *timeout_ctx)
3076 {
3077 struct wpa_supplicant *wpa_s = eloop_ctx;
3078 wpas_p2p_join_scan_req(wpa_s, 0);
3079 }
3080
3081
3082 static int wpas_p2p_join(struct wpa_supplicant *wpa_s, const u8 *iface_addr,
3083 const u8 *dev_addr, enum p2p_wps_method wps_method,
3084 int auto_join)
3085 {
3086 wpa_printf(MSG_DEBUG, "P2P: Request to join existing group (iface "
3087 MACSTR " dev " MACSTR ")%s",
3088 MAC2STR(iface_addr), MAC2STR(dev_addr),
3089 auto_join ? " (auto_join)" : "");
3090
3091 wpa_s->p2p_auto_pd = 0;
3092 wpa_s->p2p_auto_join = !!auto_join;
3093 os_memcpy(wpa_s->pending_join_iface_addr, iface_addr, ETH_ALEN);
3094 os_memcpy(wpa_s->pending_join_dev_addr, dev_addr, ETH_ALEN);
3095 wpa_s->pending_join_wps_method = wps_method;
3096
3097 /* Make sure we are not running find during connection establishment */
3098 wpas_p2p_stop_find(wpa_s);
3099
3100 wpa_s->p2p_join_scan_count = 0;
3101 wpas_p2p_join_scan(wpa_s, NULL);
3102 return 0;
3103 }
3104
3105
3106 static int wpas_p2p_join_start(struct wpa_supplicant *wpa_s)
3107 {
3108 struct wpa_supplicant *group;
3109 struct p2p_go_neg_results res;
3110 struct wpa_bss *bss;
3111
3112 eloop_cancel_timeout(wpas_p2p_pd_before_join_timeout, wpa_s, NULL);
3113 group = wpas_p2p_get_group_iface(wpa_s, 0, 0);
3114 if (group == NULL)
3115 return -1;
3116 if (group != wpa_s) {
3117 os_memcpy(group->p2p_pin, wpa_s->p2p_pin,
3118 sizeof(group->p2p_pin));
3119 group->p2p_wps_method = wpa_s->p2p_wps_method;
3120 }
3121
3122 group->p2p_in_provisioning = 1;
3123 group->p2p_fallback_to_go_neg = wpa_s->p2p_fallback_to_go_neg;
3124
3125 os_memset(&res, 0, sizeof(res));
3126 os_memcpy(res.peer_interface_addr, wpa_s->pending_join_iface_addr,
3127 ETH_ALEN);
3128 res.wps_method = wpa_s->pending_join_wps_method;
3129 bss = wpa_bss_get_bssid(wpa_s, wpa_s->pending_join_iface_addr);
3130 if (bss) {
3131 res.freq = bss->freq;
3132 res.ssid_len = bss->ssid_len;
3133 os_memcpy(res.ssid, bss->ssid, bss->ssid_len);
3134 }
3135
3136 if (wpa_s->off_channel_freq || wpa_s->roc_waiting_drv_freq) {
3137 wpa_printf(MSG_DEBUG, "P2P: Cancel remain-on-channel prior to "
3138 "starting client");
3139 wpa_drv_cancel_remain_on_channel(wpa_s);
3140 wpa_s->off_channel_freq = 0;
3141 wpa_s->roc_waiting_drv_freq = 0;
3142 }
3143 wpas_start_wps_enrollee(group, &res);
3144
3145 /*
3146 * Allow a longer timeout for join-a-running-group than normal 15
3147 * second group formation timeout since the GO may not have authorized
3148 * our connection yet.
3149 */
3150 eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s, NULL);
3151 eloop_register_timeout(60, 0, wpas_p2p_group_formation_timeout,
3152 wpa_s, NULL);
3153
3154 return 0;
3155 }
3156
3157
3158 /**
3159 * wpas_p2p_connect - Request P2P Group Formation to be started
3160 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
3161 * @peer_addr: Address of the peer P2P Device
3162 * @pin: PIN to use during provisioning or %NULL to indicate PBC mode
3163 * @persistent_group: Whether to create a persistent group
3164 * @auto_join: Whether to select join vs. GO Negotiation automatically
3165 * @join: Whether to join an existing group (as a client) instead of starting
3166 * Group Owner negotiation; @peer_addr is BSSID in that case
3167 * @auth: Whether to only authorize the connection instead of doing that and
3168 * initiating Group Owner negotiation
3169 * @go_intent: GO Intent or -1 to use default
3170 * @freq: Frequency for the group or 0 for auto-selection
3171 * @persistent_id: Persistent group credentials to use for forcing GO
3172 * parameters or -1 to generate new values (SSID/passphrase)
3173 * @pd: Whether to send Provision Discovery prior to GO Negotiation as an
3174 * interoperability workaround when initiating group formation
3175 * @ht40: Start GO with 40 MHz channel width
3176 * Returns: 0 or new PIN (if pin was %NULL) on success, -1 on unspecified
3177 * failure, -2 on failure due to channel not currently available,
3178 * -3 if forced channel is not supported
3179 */
3180 int wpas_p2p_connect(struct wpa_supplicant *wpa_s, const u8 *peer_addr,
3181 const char *pin, enum p2p_wps_method wps_method,
3182 int persistent_group, int auto_join, int join, int auth,
3183 int go_intent, int freq, int persistent_id, int pd,
3184 int ht40)
3185 {
3186 int force_freq = 0, oper_freq = 0;
3187 u8 bssid[ETH_ALEN];
3188 int ret = 0;
3189 enum wpa_driver_if_type iftype;
3190 const u8 *if_addr;
3191 struct wpa_ssid *ssid = NULL;
3192
3193 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
3194 return -1;
3195
3196 if (persistent_id >= 0) {
3197 ssid = wpa_config_get_network(wpa_s->conf, persistent_id);
3198 if (ssid == NULL || ssid->disabled != 2 ||
3199 ssid->mode != WPAS_MODE_P2P_GO)
3200 return -1;
3201 }
3202
3203 if (go_intent < 0)
3204 go_intent = wpa_s->conf->p2p_go_intent;
3205
3206 if (!auth)
3207 wpa_s->p2p_long_listen = 0;
3208
3209 wpa_s->p2p_wps_method = wps_method;
3210 wpa_s->p2p_persistent_group = !!persistent_group;
3211 wpa_s->p2p_persistent_id = persistent_id;
3212 wpa_s->p2p_go_intent = go_intent;
3213 wpa_s->p2p_connect_freq = freq;
3214 wpa_s->p2p_fallback_to_go_neg = 0;
3215 wpa_s->p2p_pd_before_go_neg = !!pd;
3216 wpa_s->p2p_go_ht40 = !!ht40;
3217
3218 if (pin)
3219 os_strlcpy(wpa_s->p2p_pin, pin, sizeof(wpa_s->p2p_pin));
3220 else if (wps_method == WPS_PIN_DISPLAY) {
3221 ret = wps_generate_pin();
3222 os_snprintf(wpa_s->p2p_pin, sizeof(wpa_s->p2p_pin), "%08d",
3223 ret);
3224 wpa_printf(MSG_DEBUG, "P2P: Randomly generated PIN: %s",
3225 wpa_s->p2p_pin);
3226 } else
3227 wpa_s->p2p_pin[0] = '\0';
3228
3229 if (join || auto_join) {
3230 u8 iface_addr[ETH_ALEN], dev_addr[ETH_ALEN];
3231 if (auth) {
3232 wpa_printf(MSG_DEBUG, "P2P: Authorize invitation to "
3233 "connect a running group from " MACSTR,
3234 MAC2STR(peer_addr));
3235 os_memcpy(wpa_s->p2p_auth_invite, peer_addr, ETH_ALEN);
3236 return ret;
3237 }
3238 os_memcpy(dev_addr, peer_addr, ETH_ALEN);
3239 if (p2p_get_interface_addr(wpa_s->global->p2p, peer_addr,
3240 iface_addr) < 0) {
3241 os_memcpy(iface_addr, peer_addr, ETH_ALEN);
3242 p2p_get_dev_addr(wpa_s->global->p2p, peer_addr,
3243 dev_addr);
3244 }
3245 if (auto_join) {
3246 os_get_time(&wpa_s->p2p_auto_started);
3247 wpa_printf(MSG_DEBUG, "P2P: Auto join started at "
3248 "%ld.%06ld",
3249 wpa_s->p2p_auto_started.sec,
3250 wpa_s->p2p_auto_started.usec);
3251 }
3252 if (wpas_p2p_join(wpa_s, iface_addr, dev_addr, wps_method,
3253 auto_join) < 0)
3254 return -1;
3255 return ret;
3256 }
3257
3258 if (wpa_s->current_ssid && wpa_drv_get_bssid(wpa_s, bssid) == 0 &&
3259 wpa_s->assoc_freq)
3260 oper_freq = wpa_s->assoc_freq;
3261 else {
3262 oper_freq = wpa_drv_shared_freq(wpa_s);
3263 if (oper_freq < 0)
3264 oper_freq = 0;
3265 }
3266
3267 if (freq > 0) {
3268 if (!p2p_supported_freq(wpa_s->global->p2p, freq)) {
3269 wpa_printf(MSG_DEBUG, "P2P: The forced channel "
3270 "(%u MHz) is not supported for P2P uses",
3271 freq);
3272 return -3;
3273 }
3274
3275 if (oper_freq > 0 && freq != oper_freq &&
3276 !(wpa_s->drv_flags &
3277 WPA_DRIVER_FLAGS_MULTI_CHANNEL_CONCURRENT)) {
3278 wpa_printf(MSG_DEBUG, "P2P: Cannot start P2P group "
3279 "on %u MHz while connected on another "
3280 "channel (%u MHz)", freq, oper_freq);
3281 return -2;
3282 }
3283 wpa_printf(MSG_DEBUG, "P2P: Trying to force us to use the "
3284 "requested channel (%u MHz)", freq);
3285 force_freq = freq;
3286 } else if (oper_freq > 0 &&
3287 !p2p_supported_freq(wpa_s->global->p2p, oper_freq)) {
3288 if (!(wpa_s->drv_flags &
3289 WPA_DRIVER_FLAGS_MULTI_CHANNEL_CONCURRENT)) {
3290 wpa_printf(MSG_DEBUG, "P2P: Cannot start P2P group "
3291 "while connected on non-P2P supported "
3292 "channel (%u MHz)", oper_freq);
3293 return -2;
3294 }
3295 wpa_printf(MSG_DEBUG, "P2P: Current operating channel "
3296 "(%u MHz) not available for P2P - try to use "
3297 "another channel", oper_freq);
3298 force_freq = 0;
3299 } else if (oper_freq > 0) {
3300 wpa_printf(MSG_DEBUG, "P2P: Trying to force us to use the "
3301 "channel we are already using (%u MHz) on another "
3302 "interface", oper_freq);
3303 force_freq = oper_freq;
3304 }
3305
3306 wpa_s->create_p2p_iface = wpas_p2p_create_iface(wpa_s);
3307
3308 if (wpa_s->create_p2p_iface) {
3309 /* Prepare to add a new interface for the group */
3310 iftype = WPA_IF_P2P_GROUP;
3311 if (go_intent == 15)
3312 iftype = WPA_IF_P2P_GO;
3313 if (wpas_p2p_add_group_interface(wpa_s, iftype) < 0) {
3314 wpa_printf(MSG_ERROR, "P2P: Failed to allocate a new "
3315 "interface for the group");
3316 return -1;
3317 }
3318
3319 if_addr = wpa_s->pending_interface_addr;
3320 } else
3321 if_addr = wpa_s->own_addr;
3322
3323 if (auth) {
3324 if (wpas_p2p_auth_go_neg(wpa_s, peer_addr, wps_method,
3325 go_intent, if_addr,
3326 force_freq, persistent_group, ssid) <
3327 0)
3328 return -1;
3329 return ret;
3330 }
3331
3332 if (wpas_p2p_start_go_neg(wpa_s, peer_addr, wps_method,
3333 go_intent, if_addr, force_freq,
3334 persistent_group, ssid) < 0) {
3335 if (wpa_s->create_p2p_iface)
3336 wpas_p2p_remove_pending_group_interface(wpa_s);
3337 return -1;
3338 }
3339 return ret;
3340 }
3341
3342
3343 /**
3344 * wpas_p2p_remain_on_channel_cb - Indication of remain-on-channel start
3345 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
3346 * @freq: Frequency of the channel in MHz
3347 * @duration: Duration of the stay on the channel in milliseconds
3348 *
3349 * This callback is called when the driver indicates that it has started the
3350 * requested remain-on-channel duration.
3351 */
3352 void wpas_p2p_remain_on_channel_cb(struct wpa_supplicant *wpa_s,
3353 unsigned int freq, unsigned int duration)
3354 {
3355 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
3356 return;
3357 if (wpa_s->off_channel_freq == wpa_s->pending_listen_freq) {
3358 p2p_listen_cb(wpa_s->global->p2p, wpa_s->pending_listen_freq,
3359 wpa_s->pending_listen_duration);
3360 wpa_s->pending_listen_freq = 0;
3361 }
3362 }
3363
3364
3365 static int wpas_p2p_listen_start(struct wpa_supplicant *wpa_s,
3366 unsigned int timeout)
3367 {
3368 /* Limit maximum Listen state time based on driver limitation. */
3369 if (timeout > wpa_s->max_remain_on_chan)
3370 timeout = wpa_s->max_remain_on_chan;
3371
3372 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
3373 return wpa_drv_p2p_listen(wpa_s, timeout);
3374
3375 return p2p_listen(wpa_s->global->p2p, timeout);
3376 }
3377
3378
3379 /**
3380 * wpas_p2p_cancel_remain_on_channel_cb - Remain-on-channel timeout
3381 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
3382 * @freq: Frequency of the channel in MHz
3383 *
3384 * This callback is called when the driver indicates that a remain-on-channel
3385 * operation has been completed, i.e., the duration on the requested channel
3386 * has timed out.
3387 */
3388 void wpas_p2p_cancel_remain_on_channel_cb(struct wpa_supplicant *wpa_s,
3389 unsigned int freq)
3390 {
3391 wpa_printf(MSG_DEBUG, "P2P: Cancel remain-on-channel callback "
3392 "(p2p_long_listen=%d ms pending_action_tx=%p)",
3393 wpa_s->p2p_long_listen, wpa_s->pending_action_tx);
3394 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
3395 return;
3396 if (p2p_listen_end(wpa_s->global->p2p, freq) > 0)
3397 return; /* P2P module started a new operation */
3398 if (wpa_s->pending_action_tx)
3399 return;
3400 if (wpa_s->p2p_long_listen > 0)
3401 wpa_s->p2p_long_listen -= wpa_s->max_remain_on_chan;
3402 if (wpa_s->p2p_long_listen > 0) {
3403 wpa_printf(MSG_DEBUG, "P2P: Continuing long Listen state");
3404 wpas_p2p_listen_start(wpa_s, wpa_s->p2p_long_listen);
3405 }
3406 }
3407
3408
3409 /**
3410 * wpas_p2p_group_remove - Remove a P2P group
3411 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
3412 * @ifname: Network interface name of the group interface or "*" to remove all
3413 * groups
3414 * Returns: 0 on success, -1 on failure
3415 *
3416 * This function is used to remove a P2P group. This can be used to disconnect
3417 * from a group in which the local end is a P2P Client or to end a P2P Group in
3418 * case the local end is the Group Owner. If a virtual network interface was
3419 * created for this group, that interface will be removed. Otherwise, only the
3420 * configured P2P group network will be removed from the interface.
3421 */
3422 int wpas_p2p_group_remove(struct wpa_supplicant *wpa_s, const char *ifname)
3423 {
3424 struct wpa_global *global = wpa_s->global;
3425
3426 if (os_strcmp(ifname, "*") == 0) {
3427 struct wpa_supplicant *prev;
3428 wpa_s = global->ifaces;
3429 while (wpa_s) {
3430 prev = wpa_s;
3431 wpa_s = wpa_s->next;
3432 wpas_p2p_disconnect(prev);
3433 }
3434 return 0;
3435 }
3436
3437 for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
3438 if (os_strcmp(wpa_s->ifname, ifname) == 0)
3439 break;
3440 }
3441
3442 return wpas_p2p_disconnect(wpa_s);
3443 }
3444
3445
3446 static int wpas_p2p_init_go_params(struct wpa_supplicant *wpa_s,
3447 struct p2p_go_neg_results *params,
3448 int freq, int ht40)
3449 {
3450 u8 bssid[ETH_ALEN];
3451 int res;
3452
3453 os_memset(params, 0, sizeof(*params));
3454 params->role_go = 1;
3455 params->ht40 = ht40;
3456 if (freq) {
3457 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on forced "
3458 "frequency %d MHz", freq);
3459 params->freq = freq;
3460 } else if (wpa_s->conf->p2p_oper_reg_class == 81 &&
3461 wpa_s->conf->p2p_oper_channel >= 1 &&
3462 wpa_s->conf->p2p_oper_channel <= 11) {
3463 params->freq = 2407 + 5 * wpa_s->conf->p2p_oper_channel;
3464 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on configured "
3465 "frequency %d MHz", params->freq);
3466 } else if (wpa_s->conf->p2p_oper_reg_class == 115 ||
3467 wpa_s->conf->p2p_oper_reg_class == 124) {
3468 params->freq = 5000 + 5 * wpa_s->conf->p2p_oper_channel;
3469 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on configured "
3470 "frequency %d MHz", params->freq);
3471 } else if (wpa_s->conf->p2p_oper_channel == 0 &&
3472 wpa_s->best_overall_freq > 0 &&
3473 p2p_supported_freq(wpa_s->global->p2p,
3474 wpa_s->best_overall_freq)) {
3475 params->freq = wpa_s->best_overall_freq;
3476 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on best overall "
3477 "channel %d MHz", params->freq);
3478 } else if (wpa_s->conf->p2p_oper_channel == 0 &&
3479 wpa_s->best_24_freq > 0 &&
3480 p2p_supported_freq(wpa_s->global->p2p,
3481 wpa_s->best_24_freq)) {
3482 params->freq = wpa_s->best_24_freq;
3483 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on best 2.4 GHz "
3484 "channel %d MHz", params->freq);
3485 } else if (wpa_s->conf->p2p_oper_channel == 0 &&
3486 wpa_s->best_5_freq > 0 &&
3487 p2p_supported_freq(wpa_s->global->p2p,
3488 wpa_s->best_5_freq)) {
3489 params->freq = wpa_s->best_5_freq;
3490 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on best 5 GHz "
3491 "channel %d MHz", params->freq);
3492 } else {
3493 int chan;
3494 for (chan = 0; chan < 11; chan++) {
3495 params->freq = 2412 + chan * 5;
3496 if (!wpas_p2p_disallowed_freq(wpa_s->global,
3497 params->freq))
3498 break;
3499 }
3500 if (chan == 11) {
3501 wpa_printf(MSG_DEBUG, "P2P: No 2.4 GHz channel "
3502 "allowed");
3503 return -1;
3504 }
3505 wpa_printf(MSG_DEBUG, "P2P: Set GO freq %d MHz (no preference "
3506 "known)", params->freq);
3507 }
3508
3509 if (wpa_s->current_ssid && wpa_drv_get_bssid(wpa_s, bssid) == 0 &&
3510 wpa_s->assoc_freq && !freq) {
3511 wpa_printf(MSG_DEBUG, "P2P: Force GO on the channel we are "
3512 "already using");
3513 params->freq = wpa_s->assoc_freq;
3514 }
3515
3516 res = wpa_drv_shared_freq(wpa_s);
3517 if (res > 0 && !freq) {
3518 wpa_printf(MSG_DEBUG, "P2P: Force GO on the channel we are "
3519 "already using on a shared interface");
3520 params->freq = res;
3521 } else if (res > 0 && freq != res &&
3522 !(wpa_s->drv_flags &
3523 WPA_DRIVER_FLAGS_MULTI_CHANNEL_CONCURRENT)) {
3524 wpa_printf(MSG_DEBUG, "P2P: Cannot start P2P group on %u MHz "
3525 "while connected on another channel (%u MHz)",
3526 freq, res);
3527 return -1;
3528 }
3529
3530 return 0;
3531 }
3532
3533
3534 static struct wpa_supplicant *
3535 wpas_p2p_get_group_iface(struct wpa_supplicant *wpa_s, int addr_allocated,
3536 int go)
3537 {
3538 struct wpa_supplicant *group_wpa_s;
3539
3540 if (!wpas_p2p_create_iface(wpa_s))
3541 return wpa_s;
3542
3543 if (wpas_p2p_add_group_interface(wpa_s, go ? WPA_IF_P2P_GO :
3544 WPA_IF_P2P_CLIENT) < 0)
3545 return NULL;
3546 group_wpa_s = wpas_p2p_init_group_interface(wpa_s, go);
3547 if (group_wpa_s == NULL) {
3548 wpas_p2p_remove_pending_group_interface(wpa_s);
3549 return NULL;
3550 }
3551
3552 return group_wpa_s;
3553 }
3554
3555
3556 /**
3557 * wpas_p2p_group_add - Add a new P2P group with local end as Group Owner
3558 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
3559 * @persistent_group: Whether to create a persistent group
3560 * @freq: Frequency for the group or 0 to indicate no hardcoding
3561 * Returns: 0 on success, -1 on failure
3562 *
3563 * This function creates a new P2P group with the local end as the Group Owner,
3564 * i.e., without using Group Owner Negotiation.
3565 */
3566 int wpas_p2p_group_add(struct wpa_supplicant *wpa_s, int persistent_group,
3567 int freq, int ht40)
3568 {
3569 struct p2p_go_neg_results params;
3570 unsigned int r;
3571
3572 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
3573 return -1;
3574
3575 /* Make sure we are not running find during connection establishment */
3576 wpa_printf(MSG_DEBUG, "P2P: Stop any on-going P2P FIND");
3577 wpas_p2p_stop_find(wpa_s);
3578
3579 if (freq == 2) {
3580 wpa_printf(MSG_DEBUG, "P2P: Request to start GO on 2.4 GHz "
3581 "band");
3582 if (wpa_s->best_24_freq > 0 &&
3583 p2p_supported_freq(wpa_s->global->p2p,
3584 wpa_s->best_24_freq)) {
3585 freq = wpa_s->best_24_freq;
3586 wpa_printf(MSG_DEBUG, "P2P: Use best 2.4 GHz band "
3587 "channel: %d MHz", freq);
3588 } else {
3589 os_get_random((u8 *) &r, sizeof(r));
3590 freq = 2412 + (r % 3) * 25;
3591 wpa_printf(MSG_DEBUG, "P2P: Use random 2.4 GHz band "
3592 "channel: %d MHz", freq);
3593 }
3594 }
3595
3596 if (freq == 5) {
3597 wpa_printf(MSG_DEBUG, "P2P: Request to start GO on 5 GHz "
3598 "band");
3599 if (wpa_s->best_5_freq > 0 &&
3600 p2p_supported_freq(wpa_s->global->p2p,
3601 wpa_s->best_5_freq)) {
3602 freq = wpa_s->best_5_freq;
3603 wpa_printf(MSG_DEBUG, "P2P: Use best 5 GHz band "
3604 "channel: %d MHz", freq);
3605 } else {
3606 os_get_random((u8 *) &r, sizeof(r));
3607 freq = 5180 + (r % 4) * 20;
3608 if (!p2p_supported_freq(wpa_s->global->p2p, freq)) {
3609 wpa_printf(MSG_DEBUG, "P2P: Could not select "
3610 "5 GHz channel for P2P group");
3611 return -1;
3612 }
3613 wpa_printf(MSG_DEBUG, "P2P: Use random 5 GHz band "
3614 "channel: %d MHz", freq);
3615 }
3616 }
3617
3618 if (freq > 0 && !p2p_supported_freq(wpa_s->global->p2p, freq)) {
3619 wpa_printf(MSG_DEBUG, "P2P: The forced channel for GO "
3620 "(%u MHz) is not supported for P2P uses",
3621 freq);
3622 return -1;
3623 }
3624
3625 if (wpas_p2p_init_go_params(wpa_s, &params, freq, ht40))
3626 return -1;
3627 if (params.freq &&
3628 !p2p_supported_freq(wpa_s->global->p2p, params.freq)) {
3629 wpa_printf(MSG_DEBUG, "P2P: The selected channel for GO "
3630 "(%u MHz) is not supported for P2P uses",
3631 params.freq);
3632 return -1;
3633 }
3634 p2p_go_params(wpa_s->global->p2p, &params);
3635 params.persistent_group = persistent_group;
3636
3637 wpa_s = wpas_p2p_get_group_iface(wpa_s, 0, 1);
3638 if (wpa_s == NULL)
3639 return -1;
3640 wpas_start_wps_go(wpa_s, &params, 0);
3641
3642 return 0;
3643 }
3644
3645
3646 static int wpas_start_p2p_client(struct wpa_supplicant *wpa_s,
3647 struct wpa_ssid *params, int addr_allocated)
3648 {
3649 struct wpa_ssid *ssid;
3650
3651 wpa_s = wpas_p2p_get_group_iface(wpa_s, addr_allocated, 0);
3652 if (wpa_s == NULL)
3653 return -1;
3654
3655 wpa_supplicant_ap_deinit(wpa_s);
3656
3657 ssid = wpa_config_add_network(wpa_s->conf);
3658 if (ssid == NULL)
3659 return -1;
3660 wpa_config_set_network_defaults(ssid);
3661 ssid->temporary = 1;
3662 ssid->proto = WPA_PROTO_RSN;
3663 ssid->pairwise_cipher = WPA_CIPHER_CCMP;
3664 ssid->group_cipher = WPA_CIPHER_CCMP;
3665 ssid->key_mgmt = WPA_KEY_MGMT_PSK;
3666 ssid->ssid = os_malloc(params->ssid_len);
3667 if (ssid->ssid == NULL) {
3668 wpa_config_remove_network(wpa_s->conf, ssid->id);
3669 return -1;
3670 }
3671 os_memcpy(ssid->ssid, params->ssid, params->ssid_len);
3672 ssid->ssid_len = params->ssid_len;
3673 ssid->p2p_group = 1;
3674 ssid->export_keys = 1;
3675 if (params->psk_set) {
3676 os_memcpy(ssid->psk, params->psk, 32);
3677 ssid->psk_set = 1;
3678 }
3679 if (params->passphrase)
3680 ssid->passphrase = os_strdup(params->passphrase);
3681
3682 wpa_supplicant_select_network(wpa_s, ssid);
3683
3684 wpa_s->show_group_started = 1;
3685
3686 return 0;
3687 }
3688
3689
3690 int wpas_p2p_group_add_persistent(struct wpa_supplicant *wpa_s,
3691 struct wpa_ssid *ssid, int addr_allocated,
3692 int freq, int ht40)
3693 {
3694 struct p2p_go_neg_results params;
3695 int go = 0;
3696
3697 if (ssid->disabled != 2 || ssid->ssid == NULL)
3698 return -1;
3699
3700 if (wpas_get_p2p_group(wpa_s, ssid->ssid, ssid->ssid_len, &go) &&
3701 go == (ssid->mode == WPAS_MODE_P2P_GO)) {
3702 wpa_printf(MSG_DEBUG, "P2P: Requested persistent group is "
3703 "already running");
3704 return 0;
3705 }
3706
3707 /* Make sure we are not running find during connection establishment */
3708 wpas_p2p_stop_find(wpa_s);
3709
3710 wpa_s->p2p_fallback_to_go_neg = 0;
3711
3712 if (ssid->mode == WPAS_MODE_INFRA)
3713 return wpas_start_p2p_client(wpa_s, ssid, addr_allocated);
3714
3715 if (ssid->mode != WPAS_MODE_P2P_GO)
3716 return -1;
3717
3718 if (wpas_p2p_init_go_params(wpa_s, &params, freq, ht40))
3719 return -1;
3720
3721 params.role_go = 1;
3722 if (ssid->passphrase == NULL ||
3723 os_strlen(ssid->passphrase) >= sizeof(params.passphrase)) {
3724 wpa_printf(MSG_DEBUG, "P2P: Invalid passphrase in persistent "
3725 "group");
3726 return -1;
3727 }
3728 os_strlcpy(params.passphrase, ssid->passphrase,
3729 sizeof(params.passphrase));
3730 os_memcpy(params.ssid, ssid->ssid, ssid->ssid_len);
3731 params.ssid_len = ssid->ssid_len;
3732 params.persistent_group = 1;
3733
3734 wpa_s = wpas_p2p_get_group_iface(wpa_s, addr_allocated, 1);
3735 if (wpa_s == NULL)
3736 return -1;
3737
3738 wpas_start_wps_go(wpa_s, &params, 0);
3739
3740 return 0;
3741 }
3742
3743
3744 static void wpas_p2p_ie_update(void *ctx, struct wpabuf *beacon_ies,
3745 struct wpabuf *proberesp_ies)
3746 {
3747 struct wpa_supplicant *wpa_s = ctx;
3748 if (wpa_s->ap_iface) {
3749 struct hostapd_data *hapd = wpa_s->ap_iface->bss[0];
3750 if (!(hapd->conf->p2p & P2P_GROUP_OWNER)) {
3751 wpabuf_free(beacon_ies);
3752 wpabuf_free(proberesp_ies);
3753 return;
3754 }
3755 if (beacon_ies) {
3756 wpabuf_free(hapd->p2p_beacon_ie);
3757 hapd->p2p_beacon_ie = beacon_ies;
3758 }
3759 wpabuf_free(hapd->p2p_probe_resp_ie);
3760 hapd->p2p_probe_resp_ie = proberesp_ies;
3761 } else {
3762 wpabuf_free(beacon_ies);
3763 wpabuf_free(proberesp_ies);
3764 }
3765 wpa_supplicant_ap_update_beacon(wpa_s);
3766 }
3767
3768
3769 static void wpas_p2p_idle_update(void *ctx, int idle)
3770 {
3771 struct wpa_supplicant *wpa_s = ctx;
3772 if (!wpa_s->ap_iface)
3773 return;
3774 wpa_printf(MSG_DEBUG, "P2P: GO - group %sidle", idle ? "" : "not ");
3775 if (idle)
3776 wpas_p2p_set_group_idle_timeout(wpa_s);
3777 else
3778 eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL);
3779 }
3780
3781
3782 struct p2p_group * wpas_p2p_group_init(struct wpa_supplicant *wpa_s,
3783 struct wpa_ssid *ssid)
3784 {
3785 struct p2p_group *group;
3786 struct p2p_group_config *cfg;
3787
3788 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
3789 return NULL;
3790 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
3791 return NULL;
3792
3793 cfg = os_zalloc(sizeof(*cfg));
3794 if (cfg == NULL)
3795 return NULL;
3796
3797 if (ssid->p2p_persistent_group && wpa_s->conf->persistent_reconnect)
3798 cfg->persistent_group = 2;
3799 else if (ssid->p2p_persistent_group)
3800 cfg->persistent_group = 1;
3801 os_memcpy(cfg->interface_addr, wpa_s->own_addr, ETH_ALEN);
3802 if (wpa_s->max_stations &&
3803 wpa_s->max_stations < wpa_s->conf->max_num_sta)
3804 cfg->max_clients = wpa_s->max_stations;
3805 else
3806 cfg->max_clients = wpa_s->conf->max_num_sta;
3807 os_memcpy(cfg->ssid, ssid->ssid, ssid->ssid_len);
3808 cfg->ssid_len = ssid->ssid_len;
3809 cfg->cb_ctx = wpa_s;
3810 cfg->ie_update = wpas_p2p_ie_update;
3811 cfg->idle_update = wpas_p2p_idle_update;
3812
3813 group = p2p_group_init(wpa_s->global->p2p, cfg);
3814 if (group == NULL)
3815 os_free(cfg);
3816 if (ssid->mode != WPAS_MODE_P2P_GROUP_FORMATION)
3817 p2p_group_notif_formation_done(group);
3818 wpa_s->p2p_group = group;
3819 return group;
3820 }
3821
3822
3823 void wpas_p2p_wps_success(struct wpa_supplicant *wpa_s, const u8 *peer_addr,
3824 int registrar)
3825 {
3826 struct wpa_ssid *ssid = wpa_s->current_ssid;
3827
3828 if (!wpa_s->p2p_in_provisioning) {
3829 wpa_printf(MSG_DEBUG, "P2P: Ignore WPS success event - P2P "
3830 "provisioning not in progress");
3831 return;
3832 }
3833
3834 if (ssid && ssid->mode == WPAS_MODE_INFRA) {
3835 u8 go_dev_addr[ETH_ALEN];
3836 os_memcpy(go_dev_addr, wpa_s->bssid, ETH_ALEN);
3837 wpas_p2p_persistent_group(wpa_s, go_dev_addr, ssid->ssid,
3838 ssid->ssid_len);
3839 /* Clear any stored provisioning info */
3840 p2p_clear_provisioning_info(wpa_s->global->p2p, go_dev_addr);
3841 }
3842
3843 eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s->parent,
3844 NULL);
3845 if (ssid && ssid->mode == WPAS_MODE_INFRA) {
3846 /*
3847 * Use a separate timeout for initial data connection to
3848 * complete to allow the group to be removed automatically if
3849 * something goes wrong in this step before the P2P group idle
3850 * timeout mechanism is taken into use.
3851 */
3852 eloop_register_timeout(P2P_MAX_INITIAL_CONN_WAIT, 0,
3853 wpas_p2p_group_formation_timeout,
3854 wpa_s->parent, NULL);
3855 }
3856 if (wpa_s->global->p2p)
3857 p2p_wps_success_cb(wpa_s->global->p2p, peer_addr);
3858 else if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
3859 wpa_drv_wps_success_cb(wpa_s, peer_addr);
3860 wpas_group_formation_completed(wpa_s, 1);
3861 }
3862
3863
3864 void wpas_p2p_wps_failed(struct wpa_supplicant *wpa_s,
3865 struct wps_event_fail *fail)
3866 {
3867 if (!wpa_s->p2p_in_provisioning) {
3868 wpa_printf(MSG_DEBUG, "P2P: Ignore WPS fail event - P2P "
3869 "provisioning not in progress");
3870 return;
3871 }
3872
3873 if (wpa_s->go_params) {
3874 p2p_clear_provisioning_info(
3875 wpa_s->global->p2p,
3876 wpa_s->go_params->peer_device_addr);
3877 }
3878
3879 wpas_notify_p2p_wps_failed(wpa_s, fail);
3880 }
3881
3882
3883 int wpas_p2p_prov_disc(struct wpa_supplicant *wpa_s, const u8 *peer_addr,
3884 const char *config_method,
3885 enum wpas_p2p_prov_disc_use use)
3886 {
3887 u16 config_methods;
3888
3889 wpa_s->p2p_fallback_to_go_neg = 0;
3890 wpa_s->pending_pd_use = NORMAL_PD;
3891 if (os_strncmp(config_method, "display", 7) == 0)
3892 config_methods = WPS_CONFIG_DISPLAY;
3893 else if (os_strncmp(config_method, "keypad", 6) == 0)
3894 config_methods = WPS_CONFIG_KEYPAD;
3895 else if (os_strncmp(config_method, "pbc", 3) == 0 ||
3896 os_strncmp(config_method, "pushbutton", 10) == 0)
3897 config_methods = WPS_CONFIG_PUSHBUTTON;
3898 else {
3899 wpa_printf(MSG_DEBUG, "P2P: Unknown config method");
3900 return -1;
3901 }
3902
3903 if (use == WPAS_P2P_PD_AUTO) {
3904 os_memcpy(wpa_s->pending_join_dev_addr, peer_addr, ETH_ALEN);
3905 wpa_s->pending_pd_config_methods = config_methods;
3906 wpa_s->p2p_auto_pd = 1;
3907 wpa_s->p2p_auto_join = 0;
3908 wpa_s->pending_pd_before_join = 0;
3909 wpa_s->auto_pd_scan_retry = 0;
3910 wpas_p2p_stop_find(wpa_s);
3911 wpa_s->p2p_join_scan_count = 0;
3912 os_get_time(&wpa_s->p2p_auto_started);
3913 wpa_printf(MSG_DEBUG, "P2P: Auto PD started at %ld.%06ld",
3914 wpa_s->p2p_auto_started.sec,
3915 wpa_s->p2p_auto_started.usec);
3916 wpas_p2p_join_scan(wpa_s, NULL);
3917 return 0;
3918 }
3919
3920 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT) {
3921 return wpa_drv_p2p_prov_disc_req(wpa_s, peer_addr,
3922 config_methods,
3923 use == WPAS_P2P_PD_FOR_JOIN);
3924 }
3925
3926 if (wpa_s->global->p2p == NULL || wpa_s->global->p2p_disabled)
3927 return -1;
3928
3929 return p2p_prov_disc_req(wpa_s->global->p2p, peer_addr,
3930 config_methods, use == WPAS_P2P_PD_FOR_JOIN,
3931 0);
3932 }
3933
3934
3935 int wpas_p2p_scan_result_text(const u8 *ies, size_t ies_len, char *buf,
3936 char *end)
3937 {
3938 return p2p_scan_result_text(ies, ies_len, buf, end);
3939 }
3940
3941
3942 static void wpas_p2p_clear_pending_action_tx(struct wpa_supplicant *wpa_s)
3943 {
3944 if (!wpa_s->pending_action_tx)
3945 return;
3946
3947 wpa_printf(MSG_DEBUG, "P2P: Drop pending Action TX due to new "
3948 "operation request");
3949 wpabuf_free(wpa_s->pending_action_tx);
3950 wpa_s->pending_action_tx = NULL;
3951 }
3952
3953
3954 int wpas_p2p_find(struct wpa_supplicant *wpa_s, unsigned int timeout,
3955 enum p2p_discovery_type type,
3956 unsigned int num_req_dev_types, const u8 *req_dev_types,
3957 const u8 *dev_id, unsigned int search_delay)
3958 {
3959 wpas_p2p_clear_pending_action_tx(wpa_s);
3960 wpa_s->p2p_long_listen = 0;
3961
3962 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
3963 return wpa_drv_p2p_find(wpa_s, timeout, type);
3964
3965 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL ||
3966 wpa_s->p2p_in_provisioning)
3967 return -1;
3968
3969 wpa_supplicant_cancel_sched_scan(wpa_s);
3970
3971 return p2p_find(wpa_s->global->p2p, timeout, type,
3972 num_req_dev_types, req_dev_types, dev_id,
3973 search_delay);
3974 }
3975
3976
3977 void wpas_p2p_stop_find(struct wpa_supplicant *wpa_s)
3978 {
3979 wpas_p2p_clear_pending_action_tx(wpa_s);
3980 wpa_s->p2p_long_listen = 0;
3981 eloop_cancel_timeout(wpas_p2p_long_listen_timeout, wpa_s, NULL);
3982 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
3983 wpa_s->p2p_cb_on_scan_complete = 0;
3984
3985 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT) {
3986 wpa_drv_p2p_stop_find(wpa_s);
3987 return;
3988 }
3989
3990 if (wpa_s->global->p2p)
3991 p2p_stop_find(wpa_s->global->p2p);
3992
3993 wpas_p2p_remove_pending_group_interface(wpa_s);
3994 }
3995
3996
3997 static void wpas_p2p_long_listen_timeout(void *eloop_ctx, void *timeout_ctx)
3998 {
3999 struct wpa_supplicant *wpa_s = eloop_ctx;
4000 wpa_s->p2p_long_listen = 0;
4001 }
4002
4003
4004 int wpas_p2p_listen(struct wpa_supplicant *wpa_s, unsigned int timeout)
4005 {
4006 int res;
4007
4008 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
4009 return -1;
4010
4011 wpa_supplicant_cancel_sched_scan(wpa_s);
4012 wpas_p2p_clear_pending_action_tx(wpa_s);
4013
4014 if (timeout == 0) {
4015 /*
4016 * This is a request for unlimited Listen state. However, at
4017 * least for now, this is mapped to a Listen state for one
4018 * hour.
4019 */
4020 timeout = 3600;
4021 }
4022 eloop_cancel_timeout(wpas_p2p_long_listen_timeout, wpa_s, NULL);
4023 wpa_s->p2p_long_listen = 0;
4024
4025 /*
4026 * Stop previous find/listen operation to avoid trying to request a new
4027 * remain-on-channel operation while the driver is still running the
4028 * previous one.
4029 */
4030 if (wpa_s->global->p2p)
4031 p2p_stop_find(wpa_s->global->p2p);
4032
4033 res = wpas_p2p_listen_start(wpa_s, timeout * 1000);
4034 if (res == 0 && timeout * 1000 > wpa_s->max_remain_on_chan) {
4035 wpa_s->p2p_long_listen = timeout * 1000;
4036 eloop_register_timeout(timeout, 0,
4037 wpas_p2p_long_listen_timeout,
4038 wpa_s, NULL);
4039 }
4040
4041 return res;
4042 }
4043
4044
4045 int wpas_p2p_assoc_req_ie(struct wpa_supplicant *wpa_s, struct wpa_bss *bss,
4046 u8 *buf, size_t len, int p2p_group)
4047 {
4048 struct wpabuf *p2p_ie;
4049 int ret;
4050
4051 if (wpa_s->global->p2p_disabled)
4052 return -1;
4053 if (wpa_s->global->p2p == NULL)
4054 return -1;
4055 if (bss == NULL)
4056 return -1;
4057
4058 p2p_ie = wpa_bss_get_vendor_ie_multi(bss, P2P_IE_VENDOR_TYPE);
4059 ret = p2p_assoc_req_ie(wpa_s->global->p2p, bss->bssid, buf, len,
4060 p2p_group, p2p_ie);
4061 wpabuf_free(p2p_ie);
4062
4063 return ret;
4064 }
4065
4066
4067 int wpas_p2p_probe_req_rx(struct wpa_supplicant *wpa_s, const u8 *addr,
4068 const u8 *dst, const u8 *bssid,
4069 const u8 *ie, size_t ie_len, int ssi_signal)
4070 {
4071 if (wpa_s->global->p2p_disabled)
4072 return 0;
4073 if (wpa_s->global->p2p == NULL)
4074 return 0;
4075
4076 switch (p2p_probe_req_rx(wpa_s->global->p2p, addr, dst, bssid,
4077 ie, ie_len)) {
4078 case P2P_PREQ_NOT_P2P:
4079 wpas_notify_preq(wpa_s, addr, dst, bssid, ie, ie_len,
4080 ssi_signal);
4081 /* fall through */
4082 case P2P_PREQ_MALFORMED:
4083 case P2P_PREQ_NOT_LISTEN:
4084 case P2P_PREQ_NOT_PROCESSED:
4085 default: /* make gcc happy */
4086 return 0;
4087 case P2P_PREQ_PROCESSED:
4088 return 1;
4089 }
4090 }
4091
4092
4093 void wpas_p2p_rx_action(struct wpa_supplicant *wpa_s, const u8 *da,
4094 const u8 *sa, const u8 *bssid,
4095 u8 category, const u8 *data, size_t len, int freq)
4096 {
4097 if (wpa_s->global->p2p_disabled)
4098 return;
4099 if (wpa_s->global->p2p == NULL)
4100 return;
4101
4102 p2p_rx_action(wpa_s->global->p2p, da, sa, bssid, category, data, len,
4103 freq);
4104 }
4105
4106
4107 void wpas_p2p_scan_ie(struct wpa_supplicant *wpa_s, struct wpabuf *ies)
4108 {
4109 if (wpa_s->global->p2p_disabled)
4110 return;
4111 if (wpa_s->global->p2p == NULL)
4112 return;
4113
4114 p2p_scan_ie(wpa_s->global->p2p, ies, NULL);
4115 }
4116
4117
4118 void wpas_p2p_group_deinit(struct wpa_supplicant *wpa_s)
4119 {
4120 p2p_group_deinit(wpa_s->p2p_group);
4121 wpa_s->p2p_group = NULL;
4122
4123 wpa_s->ap_configured_cb = NULL;
4124 wpa_s->ap_configured_cb_ctx = NULL;
4125 wpa_s->ap_configured_cb_data = NULL;
4126 wpa_s->connect_without_scan = NULL;
4127 }
4128
4129
4130 int wpas_p2p_reject(struct wpa_supplicant *wpa_s, const u8 *addr)
4131 {
4132 wpa_s->p2p_long_listen = 0;
4133
4134 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
4135 return wpa_drv_p2p_reject(wpa_s, addr);
4136
4137 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
4138 return -1;
4139
4140 return p2p_reject(wpa_s->global->p2p, addr);
4141 }
4142
4143
4144 /* Invite to reinvoke a persistent group */
4145 int wpas_p2p_invite(struct wpa_supplicant *wpa_s, const u8 *peer_addr,
4146 struct wpa_ssid *ssid, const u8 *go_dev_addr)
4147 {
4148 enum p2p_invite_role role;
4149 u8 *bssid = NULL;
4150
4151 if (ssid->mode == WPAS_MODE_P2P_GO) {
4152 role = P2P_INVITE_ROLE_GO;
4153 if (peer_addr == NULL) {
4154 wpa_printf(MSG_DEBUG, "P2P: Missing peer "
4155 "address in invitation command");
4156 return -1;
4157 }
4158 if (wpas_p2p_create_iface(wpa_s)) {
4159 if (wpas_p2p_add_group_interface(wpa_s,
4160 WPA_IF_P2P_GO) < 0) {
4161 wpa_printf(MSG_ERROR, "P2P: Failed to "
4162 "allocate a new interface for the "
4163 "group");
4164 return -1;
4165 }
4166 bssid = wpa_s->pending_interface_addr;
4167 } else
4168 bssid = wpa_s->own_addr;
4169 } else {
4170 role = P2P_INVITE_ROLE_CLIENT;
4171 peer_addr = ssid->bssid;
4172 }
4173 wpa_s->pending_invite_ssid_id = ssid->id;
4174
4175 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
4176 return wpa_drv_p2p_invite(wpa_s, peer_addr, role, bssid,
4177 ssid->ssid, ssid->ssid_len,
4178 go_dev_addr, 1);
4179
4180 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
4181 return -1;
4182
4183 return p2p_invite(wpa_s->global->p2p, peer_addr, role, bssid,
4184 ssid->ssid, ssid->ssid_len, 0, go_dev_addr, 1);
4185 }
4186
4187
4188 /* Invite to join an active group */
4189 int wpas_p2p_invite_group(struct wpa_supplicant *wpa_s, const char *ifname,
4190 const u8 *peer_addr, const u8 *go_dev_addr)
4191 {
4192 struct wpa_global *global = wpa_s->global;
4193 enum p2p_invite_role role;
4194 u8 *bssid = NULL;
4195 struct wpa_ssid *ssid;
4196 int persistent;
4197
4198 for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
4199 if (os_strcmp(wpa_s->ifname, ifname) == 0)
4200 break;
4201 }
4202 if (wpa_s == NULL) {
4203 wpa_printf(MSG_DEBUG, "P2P: Interface '%s' not found", ifname);
4204 return -1;
4205 }
4206
4207 ssid = wpa_s->current_ssid;
4208 if (ssid == NULL) {
4209 wpa_printf(MSG_DEBUG, "P2P: No current SSID to use for "
4210 "invitation");
4211 return -1;
4212 }
4213
4214 persistent = ssid->p2p_persistent_group &&
4215 wpas_p2p_get_persistent(wpa_s->parent, peer_addr,
4216 ssid->ssid, ssid->ssid_len);
4217
4218 if (ssid->mode == WPAS_MODE_P2P_GO) {
4219 role = P2P_INVITE_ROLE_ACTIVE_GO;
4220 bssid = wpa_s->own_addr;
4221 if (go_dev_addr == NULL)
4222 go_dev_addr = wpa_s->global->p2p_dev_addr;
4223 } else {
4224 role = P2P_INVITE_ROLE_CLIENT;
4225 if (wpa_s->wpa_state < WPA_ASSOCIATED) {
4226 wpa_printf(MSG_DEBUG, "P2P: Not associated - cannot "
4227 "invite to current group");
4228 return -1;
4229 }
4230 bssid = wpa_s->bssid;
4231 if (go_dev_addr == NULL &&
4232 !is_zero_ether_addr(wpa_s->go_dev_addr))
4233 go_dev_addr = wpa_s->go_dev_addr;
4234 }
4235 wpa_s->parent->pending_invite_ssid_id = -1;
4236
4237 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
4238 return wpa_drv_p2p_invite(wpa_s, peer_addr, role, bssid,
4239 ssid->ssid, ssid->ssid_len,
4240 go_dev_addr, persistent);
4241
4242 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
4243 return -1;
4244
4245 return p2p_invite(wpa_s->global->p2p, peer_addr, role, bssid,
4246 ssid->ssid, ssid->ssid_len, wpa_s->assoc_freq,
4247 go_dev_addr, persistent);
4248 }
4249
4250
4251 void wpas_p2p_completed(struct wpa_supplicant *wpa_s)
4252 {
4253 struct wpa_ssid *ssid = wpa_s->current_ssid;
4254 const char *ssid_txt;
4255 u8 go_dev_addr[ETH_ALEN];
4256 int network_id = -1;
4257 int persistent;
4258 int freq;
4259
4260 if (ssid == NULL || ssid->mode != WPAS_MODE_P2P_GROUP_FORMATION) {
4261 eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
4262 wpa_s->parent, NULL);
4263 }
4264
4265 if (!wpa_s->show_group_started || !ssid)
4266 return;
4267
4268 wpa_s->show_group_started = 0;
4269
4270 ssid_txt = wpa_ssid_txt(ssid->ssid, ssid->ssid_len);
4271 os_memset(go_dev_addr, 0, ETH_ALEN);
4272 if (ssid->bssid_set)
4273 os_memcpy(go_dev_addr, ssid->bssid, ETH_ALEN);
4274 persistent = wpas_p2p_persistent_group(wpa_s, go_dev_addr, ssid->ssid,
4275 ssid->ssid_len);
4276 os_memcpy(wpa_s->go_dev_addr, go_dev_addr, ETH_ALEN);
4277
4278 if (wpa_s->global->p2p_group_formation == wpa_s)
4279 wpa_s->global->p2p_group_formation = NULL;
4280
4281 freq = wpa_s->current_bss ? wpa_s->current_bss->freq :
4282 (int) wpa_s->assoc_freq;
4283 if (ssid->passphrase == NULL && ssid->psk_set) {
4284 char psk[65];
4285 wpa_snprintf_hex(psk, sizeof(psk), ssid->psk, 32);
4286 wpa_msg(wpa_s->parent, MSG_INFO, P2P_EVENT_GROUP_STARTED
4287 "%s client ssid=\"%s\" freq=%d psk=%s go_dev_addr="
4288 MACSTR "%s",
4289 wpa_s->ifname, ssid_txt, freq, psk,
4290 MAC2STR(go_dev_addr),
4291 persistent ? " [PERSISTENT]" : "");
4292 } else {
4293 wpa_msg(wpa_s->parent, MSG_INFO, P2P_EVENT_GROUP_STARTED
4294 "%s client ssid=\"%s\" freq=%d passphrase=\"%s\" "
4295 "go_dev_addr=" MACSTR "%s",
4296 wpa_s->ifname, ssid_txt, freq,
4297 ssid->passphrase ? ssid->passphrase : "",
4298 MAC2STR(go_dev_addr),
4299 persistent ? " [PERSISTENT]" : "");
4300 }
4301
4302 if (persistent)
4303 network_id = wpas_p2p_store_persistent_group(wpa_s->parent,
4304 ssid, go_dev_addr);
4305 if (network_id < 0)
4306 network_id = ssid->id;
4307 wpas_notify_p2p_group_started(wpa_s, ssid, network_id, 1);
4308 }
4309
4310
4311 int wpas_p2p_presence_req(struct wpa_supplicant *wpa_s, u32 duration1,
4312 u32 interval1, u32 duration2, u32 interval2)
4313 {
4314 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
4315 return -1;
4316 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
4317 return -1;
4318
4319 if (wpa_s->wpa_state < WPA_ASSOCIATED ||
4320 wpa_s->current_ssid == NULL ||
4321 wpa_s->current_ssid->mode != WPAS_MODE_INFRA)
4322 return -1;
4323
4324 return p2p_presence_req(wpa_s->global->p2p, wpa_s->bssid,
4325 wpa_s->own_addr, wpa_s->assoc_freq,
4326 duration1, interval1, duration2, interval2);
4327 }
4328
4329
4330 int wpas_p2p_ext_listen(struct wpa_supplicant *wpa_s, unsigned int period,
4331 unsigned int interval)
4332 {
4333 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
4334 return -1;
4335
4336 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
4337 return -1;
4338
4339 return p2p_ext_listen(wpa_s->global->p2p, period, interval);
4340 }
4341
4342
4343 static int wpas_p2p_is_client(struct wpa_supplicant *wpa_s)
4344 {
4345 return wpa_s->current_ssid != NULL &&
4346 wpa_s->current_ssid->p2p_group &&
4347 wpa_s->current_ssid->mode == WPAS_MODE_INFRA;
4348 }
4349
4350
4351 static void wpas_p2p_group_idle_timeout(void *eloop_ctx, void *timeout_ctx)
4352 {
4353 struct wpa_supplicant *wpa_s = eloop_ctx;
4354
4355 if (wpa_s->conf->p2p_group_idle == 0 && !wpas_p2p_is_client(wpa_s)) {
4356 wpa_printf(MSG_DEBUG, "P2P: Ignore group idle timeout - "
4357 "disabled");
4358 return;
4359 }
4360
4361 wpa_printf(MSG_DEBUG, "P2P: Group idle timeout reached - terminate "
4362 "group");
4363 wpas_p2p_group_delete(wpa_s, P2P_GROUP_REMOVAL_IDLE_TIMEOUT);
4364 }
4365
4366
4367 static void wpas_p2p_set_group_idle_timeout(struct wpa_supplicant *wpa_s)
4368 {
4369 int timeout;
4370
4371 if (eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL) > 0)
4372 wpa_printf(MSG_DEBUG, "P2P: Cancelled P2P group idle timeout");
4373
4374 if (wpa_s->current_ssid == NULL || !wpa_s->current_ssid->p2p_group)
4375 return;
4376
4377 timeout = wpa_s->conf->p2p_group_idle;
4378 if (wpa_s->current_ssid->mode == WPAS_MODE_INFRA &&
4379 (timeout == 0 || timeout > P2P_MAX_CLIENT_IDLE))
4380 timeout = P2P_MAX_CLIENT_IDLE;
4381
4382 if (timeout == 0)
4383 return;
4384
4385 if (timeout < 0) {
4386 if (wpa_s->current_ssid->mode == WPAS_MODE_INFRA)
4387 timeout = 0; /* special client mode no-timeout */
4388 else
4389 return;
4390 }
4391
4392 if (wpa_s->p2p_in_provisioning) {
4393 /*
4394 * Use the normal group formation timeout during the
4395 * provisioning phase to avoid terminating this process too
4396 * early due to group idle timeout.
4397 */
4398 wpa_printf(MSG_DEBUG, "P2P: Do not use P2P group idle timeout "
4399 "during provisioning");
4400 return;
4401 }
4402
4403 if (wpa_s->show_group_started) {
4404 /*
4405 * Use the normal group formation timeout between the end of
4406 * the provisioning phase and completion of 4-way handshake to
4407 * avoid terminating this process too early due to group idle
4408 * timeout.
4409 */
4410 wpa_printf(MSG_DEBUG, "P2P: Do not use P2P group idle timeout "
4411 "while waiting for initial 4-way handshake to "
4412 "complete");
4413 return;
4414 }
4415
4416 wpa_printf(MSG_DEBUG, "P2P: Set P2P group idle timeout to %u seconds",
4417 timeout);
4418 eloop_register_timeout(timeout, 0, wpas_p2p_group_idle_timeout,
4419 wpa_s, NULL);
4420 }
4421
4422
4423 void wpas_p2p_deauth_notif(struct wpa_supplicant *wpa_s, const u8 *bssid,
4424 u16 reason_code, const u8 *ie, size_t ie_len,
4425 int locally_generated)
4426 {
4427 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
4428 return;
4429 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
4430 return;
4431
4432 if (!locally_generated)
4433 p2p_deauth_notif(wpa_s->global->p2p, bssid, reason_code, ie,
4434 ie_len);
4435
4436 if (reason_code == WLAN_REASON_DEAUTH_LEAVING && !locally_generated &&
4437 wpa_s->current_ssid &&
4438 wpa_s->current_ssid->p2p_group &&
4439 wpa_s->current_ssid->mode == WPAS_MODE_INFRA) {
4440 wpa_printf(MSG_DEBUG, "P2P: GO indicated that the P2P Group "
4441 "session is ending");
4442 wpas_p2p_group_delete(wpa_s,
4443 P2P_GROUP_REMOVAL_GO_ENDING_SESSION);
4444 }
4445 }
4446
4447
4448 void wpas_p2p_disassoc_notif(struct wpa_supplicant *wpa_s, const u8 *bssid,
4449 u16 reason_code, const u8 *ie, size_t ie_len,
4450 int locally_generated)
4451 {
4452 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
4453 return;
4454 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
4455 return;
4456
4457 if (!locally_generated)
4458 p2p_disassoc_notif(wpa_s->global->p2p, bssid, reason_code, ie,
4459 ie_len);
4460 }
4461
4462
4463 void wpas_p2p_update_config(struct wpa_supplicant *wpa_s)
4464 {
4465 struct p2p_data *p2p = wpa_s->global->p2p;
4466
4467 if (p2p == NULL)
4468 return;
4469
4470 if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_CAPABLE))
4471 return;
4472
4473 if (wpa_s->conf->changed_parameters & CFG_CHANGED_DEVICE_NAME)
4474 p2p_set_dev_name(p2p, wpa_s->conf->device_name);
4475
4476 if (wpa_s->conf->changed_parameters & CFG_CHANGED_DEVICE_TYPE)
4477 p2p_set_pri_dev_type(p2p, wpa_s->conf->device_type);
4478
4479 if (wpa_s->wps &&
4480 (wpa_s->conf->changed_parameters & CFG_CHANGED_CONFIG_METHODS))
4481 p2p_set_config_methods(p2p, wpa_s->wps->config_methods);
4482
4483 if (wpa_s->wps && (wpa_s->conf->changed_parameters & CFG_CHANGED_UUID))
4484 p2p_set_uuid(p2p, wpa_s->wps->uuid);
4485
4486 if (wpa_s->conf->changed_parameters & CFG_CHANGED_WPS_STRING) {
4487 p2p_set_manufacturer(p2p, wpa_s->conf->manufacturer);
4488 p2p_set_model_name(p2p, wpa_s->conf->model_name);
4489 p2p_set_model_number(p2p, wpa_s->conf->model_number);
4490 p2p_set_serial_number(p2p, wpa_s->conf->serial_number);
4491 }
4492
4493 if (wpa_s->conf->changed_parameters & CFG_CHANGED_SEC_DEVICE_TYPE)
4494 p2p_set_sec_dev_types(p2p,
4495 (void *) wpa_s->conf->sec_device_type,
4496 wpa_s->conf->num_sec_device_types);
4497
4498 if (wpa_s->conf->changed_parameters & CFG_CHANGED_VENDOR_EXTENSION) {
4499 int i;
4500 p2p_remove_wps_vendor_extensions(p2p);
4501 for (i = 0; i < MAX_WPS_VENDOR_EXT; i++) {
4502 if (wpa_s->conf->wps_vendor_ext[i] == NULL)
4503 continue;
4504 p2p_add_wps_vendor_extension(
4505 p2p, wpa_s->conf->wps_vendor_ext[i]);
4506 }
4507 }
4508
4509 if ((wpa_s->conf->changed_parameters & CFG_CHANGED_COUNTRY) &&
4510 wpa_s->conf->country[0] && wpa_s->conf->country[1]) {
4511 char country[3];
4512 country[0] = wpa_s->conf->country[0];
4513 country[1] = wpa_s->conf->country[1];
4514 country[2] = 0x04;
4515 p2p_set_country(p2p, country);
4516 }
4517
4518 if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_SSID_POSTFIX) {
4519 p2p_set_ssid_postfix(p2p, (u8 *) wpa_s->conf->p2p_ssid_postfix,
4520 wpa_s->conf->p2p_ssid_postfix ?
4521 os_strlen(wpa_s->conf->p2p_ssid_postfix) :
4522 0);
4523 }
4524
4525 if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_INTRA_BSS)
4526 p2p_set_intra_bss_dist(p2p, wpa_s->conf->p2p_intra_bss);
4527
4528 if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_LISTEN_CHANNEL) {
4529 u8 reg_class, channel;
4530 int ret;
4531 unsigned int r;
4532 if (wpa_s->conf->p2p_listen_reg_class &&
4533 wpa_s->conf->p2p_listen_channel) {
4534 reg_class = wpa_s->conf->p2p_listen_reg_class;
4535 channel = wpa_s->conf->p2p_listen_channel;
4536 } else {
4537 reg_class = 81;
4538 /*
4539 * Pick one of the social channels randomly as the
4540 * listen channel.
4541 */
4542 os_get_random((u8 *) &r, sizeof(r));
4543 channel = 1 + (r % 3) * 5;
4544 }
4545 ret = p2p_set_listen_channel(p2p, reg_class, channel);
4546 if (ret)
4547 wpa_printf(MSG_ERROR, "P2P: Own listen channel update "
4548 "failed: %d", ret);
4549 }
4550 if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_OPER_CHANNEL) {
4551 u8 op_reg_class, op_channel, cfg_op_channel;
4552 int ret = 0;
4553 unsigned int r;
4554 if (wpa_s->conf->p2p_oper_reg_class &&
4555 wpa_s->conf->p2p_oper_channel) {
4556 op_reg_class = wpa_s->conf->p2p_oper_reg_class;
4557 op_channel = wpa_s->conf->p2p_oper_channel;
4558 cfg_op_channel = 1;
4559 } else {
4560 op_reg_class = 81;
4561 /*
4562 * Use random operation channel from (1, 6, 11)
4563 *if no other preference is indicated.
4564 */
4565 os_get_random((u8 *) &r, sizeof(r));
4566 op_channel = 1 + (r % 3) * 5;
4567 cfg_op_channel = 0;
4568 }
4569 ret = p2p_set_oper_channel(p2p, op_reg_class, op_channel,
4570 cfg_op_channel);
4571 if (ret)
4572 wpa_printf(MSG_ERROR, "P2P: Own oper channel update "
4573 "failed: %d", ret);
4574 }
4575
4576 if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_PREF_CHAN) {
4577 if (p2p_set_pref_chan(p2p, wpa_s->conf->num_p2p_pref_chan,
4578 wpa_s->conf->p2p_pref_chan) < 0) {
4579 wpa_printf(MSG_ERROR, "P2P: Preferred channel list "
4580 "update failed");
4581 }
4582 }
4583 }
4584
4585
4586 int wpas_p2p_set_noa(struct wpa_supplicant *wpa_s, u8 count, int start,
4587 int duration)
4588 {
4589 if (!wpa_s->ap_iface)
4590 return -1;
4591 return hostapd_p2p_set_noa(wpa_s->ap_iface->bss[0], count, start,
4592 duration);
4593 }
4594
4595
4596 int wpas_p2p_set_cross_connect(struct wpa_supplicant *wpa_s, int enabled)
4597 {
4598 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
4599 return -1;
4600 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
4601 return -1;
4602
4603 wpa_s->global->cross_connection = enabled;
4604 p2p_set_cross_connect(wpa_s->global->p2p, enabled);
4605
4606 if (!enabled) {
4607 struct wpa_supplicant *iface;
4608
4609 for (iface = wpa_s->global->ifaces; iface; iface = iface->next)
4610 {
4611 if (iface->cross_connect_enabled == 0)
4612 continue;
4613
4614 iface->cross_connect_enabled = 0;
4615 iface->cross_connect_in_use = 0;
4616 wpa_msg(iface->parent, MSG_INFO,
4617 P2P_EVENT_CROSS_CONNECT_DISABLE "%s %s",
4618 iface->ifname, iface->cross_connect_uplink);
4619 }
4620 }
4621
4622 return 0;
4623 }
4624
4625
4626 static void wpas_p2p_enable_cross_connect(struct wpa_supplicant *uplink)
4627 {
4628 struct wpa_supplicant *iface;
4629
4630 if (!uplink->global->cross_connection)
4631 return;
4632
4633 for (iface = uplink->global->ifaces; iface; iface = iface->next) {
4634 if (!iface->cross_connect_enabled)
4635 continue;
4636 if (os_strcmp(uplink->ifname, iface->cross_connect_uplink) !=
4637 0)
4638 continue;
4639 if (iface->ap_iface == NULL)
4640 continue;
4641 if (iface->cross_connect_in_use)
4642 continue;
4643
4644 iface->cross_connect_in_use = 1;
4645 wpa_msg(iface->parent, MSG_INFO,
4646 P2P_EVENT_CROSS_CONNECT_ENABLE "%s %s",
4647 iface->ifname, iface->cross_connect_uplink);
4648 }
4649 }
4650
4651
4652 static void wpas_p2p_disable_cross_connect(struct wpa_supplicant *uplink)
4653 {
4654 struct wpa_supplicant *iface;
4655
4656 for (iface = uplink->global->ifaces; iface; iface = iface->next) {
4657 if (!iface->cross_connect_enabled)
4658 continue;
4659 if (os_strcmp(uplink->ifname, iface->cross_connect_uplink) !=
4660 0)
4661 continue;
4662 if (!iface->cross_connect_in_use)
4663 continue;
4664
4665 wpa_msg(iface->parent, MSG_INFO,
4666 P2P_EVENT_CROSS_CONNECT_DISABLE "%s %s",
4667 iface->ifname, iface->cross_connect_uplink);
4668 iface->cross_connect_in_use = 0;
4669 }
4670 }
4671
4672
4673 void wpas_p2p_notif_connected(struct wpa_supplicant *wpa_s)
4674 {
4675 if (wpa_s->ap_iface || wpa_s->current_ssid == NULL ||
4676 wpa_s->current_ssid->mode != WPAS_MODE_INFRA ||
4677 wpa_s->cross_connect_disallowed)
4678 wpas_p2p_disable_cross_connect(wpa_s);
4679 else
4680 wpas_p2p_enable_cross_connect(wpa_s);
4681 if (!wpa_s->ap_iface &&
4682 eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL) > 0)
4683 wpa_printf(MSG_DEBUG, "P2P: Cancelled P2P group idle timeout");
4684 }
4685
4686
4687 void wpas_p2p_notif_disconnected(struct wpa_supplicant *wpa_s)
4688 {
4689 wpas_p2p_disable_cross_connect(wpa_s);
4690 if (!wpa_s->ap_iface &&
4691 !eloop_is_timeout_registered(wpas_p2p_group_idle_timeout,
4692 wpa_s, NULL))
4693 wpas_p2p_set_group_idle_timeout(wpa_s);
4694 }
4695
4696
4697 static void wpas_p2p_cross_connect_setup(struct wpa_supplicant *wpa_s)
4698 {
4699 struct wpa_supplicant *iface;
4700
4701 if (!wpa_s->global->cross_connection)
4702 return;
4703
4704 for (iface = wpa_s->global->ifaces; iface; iface = iface->next) {
4705 if (iface == wpa_s)
4706 continue;
4707 if (iface->drv_flags &
4708 WPA_DRIVER_FLAGS_P2P_DEDICATED_INTERFACE)
4709 continue;
4710 if (iface->drv_flags & WPA_DRIVER_FLAGS_P2P_CAPABLE)
4711 continue;
4712
4713 wpa_s->cross_connect_enabled = 1;
4714 os_strlcpy(wpa_s->cross_connect_uplink, iface->ifname,
4715 sizeof(wpa_s->cross_connect_uplink));
4716 wpa_printf(MSG_DEBUG, "P2P: Enable cross connection from "
4717 "%s to %s whenever uplink is available",
4718 wpa_s->ifname, wpa_s->cross_connect_uplink);
4719
4720 if (iface->ap_iface || iface->current_ssid == NULL ||
4721 iface->current_ssid->mode != WPAS_MODE_INFRA ||
4722 iface->cross_connect_disallowed ||
4723 iface->wpa_state != WPA_COMPLETED)
4724 break;
4725
4726 wpa_s->cross_connect_in_use = 1;
4727 wpa_msg(wpa_s->parent, MSG_INFO,
4728 P2P_EVENT_CROSS_CONNECT_ENABLE "%s %s",
4729 wpa_s->ifname, wpa_s->cross_connect_uplink);
4730 break;
4731 }
4732 }
4733
4734
4735 int wpas_p2p_notif_pbc_overlap(struct wpa_supplicant *wpa_s)
4736 {
4737 if (wpa_s->p2p_group_interface != P2P_GROUP_INTERFACE_CLIENT &&
4738 !wpa_s->p2p_in_provisioning)
4739 return 0; /* not P2P client operation */
4740
4741 wpa_printf(MSG_DEBUG, "P2P: Terminate connection due to WPS PBC "
4742 "session overlap");
4743 if (wpa_s != wpa_s->parent)
4744 wpa_msg_ctrl(wpa_s->parent, MSG_INFO, WPS_EVENT_OVERLAP);
4745
4746 if (wpa_s->global->p2p)
4747 p2p_group_formation_failed(wpa_s->global->p2p);
4748
4749 eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
4750 wpa_s->parent, NULL);
4751
4752 wpas_group_formation_completed(wpa_s, 0);
4753 return 1;
4754 }
4755
4756
4757 void wpas_p2p_update_channel_list(struct wpa_supplicant *wpa_s)
4758 {
4759 struct p2p_channels chan;
4760
4761 if (wpa_s->global == NULL || wpa_s->global->p2p == NULL)
4762 return;
4763
4764 os_memset(&chan, 0, sizeof(chan));
4765 if (wpas_p2p_setup_channels(wpa_s, &chan)) {
4766 wpa_printf(MSG_ERROR, "P2P: Failed to update supported "
4767 "channel list");
4768 return;
4769 }
4770
4771 p2p_update_channel_list(wpa_s->global->p2p, &chan);
4772 }
4773
4774
4775 int wpas_p2p_cancel(struct wpa_supplicant *wpa_s)
4776 {
4777 struct wpa_global *global = wpa_s->global;
4778 int found = 0;
4779 const u8 *peer;
4780
4781 if (global->p2p == NULL)
4782 return -1;
4783
4784 wpa_printf(MSG_DEBUG, "P2P: Request to cancel group formation");
4785
4786 if (wpa_s->pending_interface_name[0] &&
4787 !is_zero_ether_addr(wpa_s->pending_interface_addr))
4788 found = 1;
4789
4790 peer = p2p_get_go_neg_peer(global->p2p);
4791 if (peer) {
4792 wpa_printf(MSG_DEBUG, "P2P: Unauthorize pending GO Neg peer "
4793 MACSTR, MAC2STR(peer));
4794 p2p_unauthorize(global->p2p, peer);
4795 found = 1;
4796 }
4797
4798 wpas_p2p_stop_find(wpa_s);
4799
4800 for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
4801 if (wpa_s == global->p2p_group_formation &&
4802 (wpa_s->p2p_in_provisioning ||
4803 wpa_s->parent->pending_interface_type ==
4804 WPA_IF_P2P_CLIENT)) {
4805 wpa_printf(MSG_DEBUG, "P2P: Interface %s in group "
4806 "formation found - cancelling",
4807 wpa_s->ifname);
4808 found = 1;
4809 eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
4810 wpa_s->parent, NULL);
4811 wpas_p2p_group_delete(wpa_s,
4812 P2P_GROUP_REMOVAL_REQUESTED);
4813 break;
4814 }
4815 }
4816
4817 if (!found) {
4818 wpa_printf(MSG_DEBUG, "P2P: No ongoing group formation found");
4819 return -1;
4820 }
4821
4822 return 0;
4823 }
4824
4825
4826 void wpas_p2p_interface_unavailable(struct wpa_supplicant *wpa_s)
4827 {
4828 if (wpa_s->current_ssid == NULL || !wpa_s->current_ssid->p2p_group)
4829 return;
4830
4831 wpa_printf(MSG_DEBUG, "P2P: Remove group due to driver resource not "
4832 "being available anymore");
4833 wpas_p2p_group_delete(wpa_s, P2P_GROUP_REMOVAL_UNAVAILABLE);
4834 }
4835
4836
4837 void wpas_p2p_update_best_channels(struct wpa_supplicant *wpa_s,
4838 int freq_24, int freq_5, int freq_overall)
4839 {
4840 struct p2p_data *p2p = wpa_s->global->p2p;
4841 if (p2p == NULL || (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT))
4842 return;
4843 p2p_set_best_channels(p2p, freq_24, freq_5, freq_overall);
4844 }
4845
4846
4847 int wpas_p2p_unauthorize(struct wpa_supplicant *wpa_s, const char *addr)
4848 {
4849 u8 peer[ETH_ALEN];
4850 struct p2p_data *p2p = wpa_s->global->p2p;
4851
4852 if (p2p == NULL || (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT))
4853 return -1;
4854
4855 if (hwaddr_aton(addr, peer))
4856 return -1;
4857
4858 return p2p_unauthorize(p2p, peer);
4859 }
4860
4861
4862 /**
4863 * wpas_p2p_disconnect - Disconnect from a P2P Group
4864 * @wpa_s: Pointer to wpa_supplicant data
4865 * Returns: 0 on success, -1 on failure
4866 *
4867 * This can be used to disconnect from a group in which the local end is a P2P
4868 * Client or to end a P2P Group in case the local end is the Group Owner. If a
4869 * virtual network interface was created for this group, that interface will be
4870 * removed. Otherwise, only the configured P2P group network will be removed
4871 * from the interface.
4872 */
4873 int wpas_p2p_disconnect(struct wpa_supplicant *wpa_s)
4874 {
4875
4876 if (wpa_s == NULL)
4877 return -1;
4878
4879 return wpas_p2p_group_delete(wpa_s, P2P_GROUP_REMOVAL_REQUESTED);
4880 }
4881
4882
4883 int wpas_p2p_in_progress(struct wpa_supplicant *wpa_s)
4884 {
4885 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
4886 return 0;
4887
4888 return p2p_in_progress(wpa_s->global->p2p);
4889 }
4890
4891
4892 void wpas_p2p_network_removed(struct wpa_supplicant *wpa_s,
4893 struct wpa_ssid *ssid)
4894 {
4895 if (wpa_s->p2p_in_provisioning && ssid->p2p_group &&
4896 eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
4897 wpa_s->parent, NULL) > 0) {
4898 /**
4899 * Remove the network by scheduling the group formation
4900 * timeout to happen immediately. The teardown code
4901 * needs to be scheduled to run asynch later so that we
4902 * don't delete data from under ourselves unexpectedly.
4903 * Calling wpas_p2p_group_formation_timeout directly
4904 * causes a series of crashes in WPS failure scenarios.
4905 */
4906 wpa_printf(MSG_DEBUG, "P2P: Canceled group formation due to "
4907 "P2P group network getting removed");
4908 eloop_register_timeout(0, 0, wpas_p2p_group_formation_timeout,
4909 wpa_s->parent, NULL);
4910 }
4911 }
4912
4913
4914 struct wpa_ssid * wpas_p2p_get_persistent(struct wpa_supplicant *wpa_s,
4915 const u8 *addr, const u8 *ssid,
4916 size_t ssid_len)
4917 {
4918 struct wpa_ssid *s;
4919 size_t i;
4920
4921 for (s = wpa_s->conf->ssid; s; s = s->next) {
4922 if (s->disabled != 2)
4923 continue;
4924 if (ssid &&
4925 (ssid_len != s->ssid_len ||
4926 os_memcmp(ssid, s->ssid, ssid_len) != 0))
4927 continue;
4928 if (os_memcmp(s->bssid, addr, ETH_ALEN) == 0)
4929 return s; /* peer is GO in the persistent group */
4930 if (s->mode != WPAS_MODE_P2P_GO || s->p2p_client_list == NULL)
4931 continue;
4932 for (i = 0; i < s->num_p2p_clients; i++) {
4933 if (os_memcmp(s->p2p_client_list + i * ETH_ALEN,
4934 addr, ETH_ALEN) == 0)
4935 return s; /* peer is P2P client in persistent
4936 * group */
4937 }
4938 }
4939
4940 return NULL;
4941 }
4942
4943
4944 void wpas_p2p_notify_ap_sta_authorized(struct wpa_supplicant *wpa_s,
4945 const u8 *addr)
4946 {
4947 if (addr == NULL)
4948 return;
4949 wpas_p2p_add_persistent_group_client(wpa_s, addr);
4950 }
4951
4952
4953 static void wpas_p2p_fallback_to_go_neg(struct wpa_supplicant *wpa_s,
4954 int group_added)
4955 {
4956 struct wpa_supplicant *group = wpa_s;
4957 eloop_cancel_timeout(wpas_p2p_pd_before_join_timeout, wpa_s, NULL);
4958 if (wpa_s->global->p2p_group_formation)
4959 group = wpa_s->global->p2p_group_formation;
4960 wpa_s = wpa_s->parent;
4961 offchannel_send_action_done(wpa_s);
4962 if (group_added)
4963 wpas_p2p_group_delete(group, P2P_GROUP_REMOVAL_SILENT);
4964 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Fall back to GO Negotiation");
4965 wpas_p2p_connect(wpa_s, wpa_s->pending_join_dev_addr, wpa_s->p2p_pin,
4966 wpa_s->p2p_wps_method, wpa_s->p2p_persistent_group, 0,
4967 0, 0, wpa_s->p2p_go_intent, wpa_s->p2p_connect_freq,
4968 wpa_s->p2p_persistent_id,
4969 wpa_s->p2p_pd_before_go_neg,
4970 wpa_s->p2p_go_ht40);
4971 }
4972
4973
4974 int wpas_p2p_scan_no_go_seen(struct wpa_supplicant *wpa_s)
4975 {
4976 if (!wpa_s->p2p_fallback_to_go_neg ||
4977 wpa_s->p2p_in_provisioning <= 5)
4978 return 0;
4979
4980 if (wpas_p2p_peer_go(wpa_s, wpa_s->pending_join_dev_addr) > 0)
4981 return 0; /* peer operating as a GO */
4982
4983 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: GO not found for p2p_connect-auto - "
4984 "fallback to GO Negotiation");
4985 wpas_p2p_fallback_to_go_neg(wpa_s, 1);
4986
4987 return 1;
4988 }
4989
4990
4991 unsigned int wpas_p2p_search_delay(struct wpa_supplicant *wpa_s)
4992 {
4993 const char *rn, *rn2;
4994 struct wpa_supplicant *ifs;
4995
4996 if (wpa_s->wpa_state > WPA_SCANNING) {
4997 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Use %u ms search delay due to "
4998 "concurrent operation",
4999 P2P_CONCURRENT_SEARCH_DELAY);
5000 return P2P_CONCURRENT_SEARCH_DELAY;
5001 }
5002
5003 if (!wpa_s->driver->get_radio_name)
5004 return 0;
5005 rn = wpa_s->driver->get_radio_name(wpa_s->drv_priv);
5006 if (rn == NULL || rn[0] == '\0')
5007 return 0;
5008
5009 for (ifs = wpa_s->global->ifaces; ifs; ifs = ifs->next) {
5010 if (ifs == wpa_s || !ifs->driver->get_radio_name)
5011 continue;
5012
5013 rn2 = ifs->driver->get_radio_name(ifs->drv_priv);
5014 if (!rn2 || os_strcmp(rn, rn2) != 0)
5015 continue;
5016 if (ifs->wpa_state > WPA_SCANNING) {
5017 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Use %u ms search "
5018 "delay due to concurrent operation on "
5019 "interface %s",
5020 P2P_CONCURRENT_SEARCH_DELAY, ifs->ifname);
5021 return P2P_CONCURRENT_SEARCH_DELAY;
5022 }
5023 }
5024
5025 return 0;
5026 }