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