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