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