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