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