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