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