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