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