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