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