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