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