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