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