blob: e0c358017448db415582977f7e6c043227c7ecfc [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 Shmidt04949592012-07-19 12:16:46 -070042#define P2P_AUTO_PD_SCAN_ATTEMPTS 5
43
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080044#ifndef P2P_MAX_CLIENT_IDLE
45/*
46 * How many seconds to try to reconnect to the GO when connection in P2P client
47 * role has been lost.
48 */
Dmitry Shmidt98f9e762012-05-30 11:18:46 -070049#ifdef ANDROID_P2P
50#define P2P_MAX_CLIENT_IDLE 20
51#else
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080052#define P2P_MAX_CLIENT_IDLE 10
Dmitry Shmidt98f9e762012-05-30 11:18:46 -070053#endif /* ANDROID_P2P */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080054#endif /* P2P_MAX_CLIENT_IDLE */
55
Dmitry Shmidt04949592012-07-19 12:16:46 -070056#ifndef P2P_MAX_INITIAL_CONN_WAIT
57/*
58 * How many seconds to wait for initial 4-way handshake to get completed after
59 * WPS provisioning step.
60 */
61#define P2P_MAX_INITIAL_CONN_WAIT 10
62#endif /* P2P_MAX_INITIAL_CONN_WAIT */
63
Dmitry Shmidt98f9e762012-05-30 11:18:46 -070064#ifdef ANDROID_P2P
65static int wpas_global_scan_in_progress(struct wpa_supplicant *wpa_s);
66#endif
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070067static void wpas_p2p_long_listen_timeout(void *eloop_ctx, void *timeout_ctx);
68static struct wpa_supplicant *
69wpas_p2p_get_group_iface(struct wpa_supplicant *wpa_s, int addr_allocated,
70 int go);
71static int wpas_p2p_join_start(struct wpa_supplicant *wpa_s);
Dmitry Shmidt04949592012-07-19 12:16:46 -070072static void wpas_p2p_join_scan_req(struct wpa_supplicant *wpa_s, int freq);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070073static void wpas_p2p_join_scan(void *eloop_ctx, void *timeout_ctx);
74static int wpas_p2p_join(struct wpa_supplicant *wpa_s, const u8 *iface_addr,
Dmitry Shmidt04949592012-07-19 12:16:46 -070075 const u8 *dev_addr, enum p2p_wps_method wps_method,
76 int auto_join);
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -080077static void wpas_p2p_pd_before_join_timeout(void *eloop_ctx,
78 void *timeout_ctx);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070079static int wpas_p2p_create_iface(struct wpa_supplicant *wpa_s);
80static void wpas_p2p_cross_connect_setup(struct wpa_supplicant *wpa_s);
81static void wpas_p2p_group_idle_timeout(void *eloop_ctx, void *timeout_ctx);
82static void wpas_p2p_set_group_idle_timeout(struct wpa_supplicant *wpa_s);
Dmitry Shmidt04949592012-07-19 12:16:46 -070083static void wpas_p2p_fallback_to_go_neg(struct wpa_supplicant *wpa_s,
84 int group_added);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070085
Dmitry Shmidt2fb777c2012-05-02 12:29:53 -070086#ifdef ANDROID_P2P
87static int wpas_global_scan_in_progress(struct wpa_supplicant *wpa_s)
88{
89 struct wpa_supplicant *iface = NULL;
90
91 for (iface = wpa_s->global->ifaces; iface; iface = iface->next) {
92 if(iface->scanning || iface->wpa_state == WPA_SCANNING) {
93 wpa_printf(MSG_DEBUG, "P2P: Scan in progress on %s,"
94 "defer P2P SEARCH", iface->ifname);
95 return 1;
96 }
97 }
98
99 return 0;
100}
101#endif
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700102
103static void wpas_p2p_scan_res_handler(struct wpa_supplicant *wpa_s,
104 struct wpa_scan_results *scan_res)
105{
106 size_t i;
107
108 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
109 return;
110
111 wpa_printf(MSG_DEBUG, "P2P: Scan results received (%d BSS)",
112 (int) scan_res->num);
113
114 for (i = 0; i < scan_res->num; i++) {
115 struct wpa_scan_res *bss = scan_res->res[i];
116 if (p2p_scan_res_handler(wpa_s->global->p2p, bss->bssid,
117 bss->freq, bss->level,
118 (const u8 *) (bss + 1),
119 bss->ie_len) > 0)
120 break;
121 }
122
123 p2p_scan_res_handled(wpa_s->global->p2p);
124}
125
126
127static int wpas_p2p_scan(void *ctx, enum p2p_scan_type type, int freq,
128 unsigned int num_req_dev_types,
Dmitry Shmidt04949592012-07-19 12:16:46 -0700129 const u8 *req_dev_types, const u8 *dev_id, u16 pw_id)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700130{
131 struct wpa_supplicant *wpa_s = ctx;
132 struct wpa_driver_scan_params params;
133 int ret;
134 struct wpabuf *wps_ie, *ies;
135 int social_channels[] = { 2412, 2437, 2462, 0, 0 };
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800136 size_t ielen;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700137
138 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
139 return -1;
140
141 os_memset(&params, 0, sizeof(params));
142
143 /* P2P Wildcard SSID */
144 params.num_ssids = 1;
145 params.ssids[0].ssid = (u8 *) P2P_WILDCARD_SSID;
146 params.ssids[0].ssid_len = P2P_WILDCARD_SSID_LEN;
147
148 wpa_s->wps->dev.p2p = 1;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700149 wps_ie = wps_build_probe_req_ie(pw_id, &wpa_s->wps->dev,
150 wpa_s->wps->uuid, WPS_REQ_ENROLLEE,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700151 num_req_dev_types, req_dev_types);
152 if (wps_ie == NULL)
153 return -1;
154
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800155 ielen = p2p_scan_ie_buf_len(wpa_s->global->p2p);
156 ies = wpabuf_alloc(wpabuf_len(wps_ie) + ielen);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700157 if (ies == NULL) {
158 wpabuf_free(wps_ie);
159 return -1;
160 }
161 wpabuf_put_buf(ies, wps_ie);
162 wpabuf_free(wps_ie);
163
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -0800164 p2p_scan_ie(wpa_s->global->p2p, ies, dev_id);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700165
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800166 params.p2p_probe = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700167 params.extra_ies = wpabuf_head(ies);
168 params.extra_ies_len = wpabuf_len(ies);
169
170 switch (type) {
171 case P2P_SCAN_SOCIAL:
172 params.freqs = social_channels;
173 break;
174 case P2P_SCAN_FULL:
175 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700176 case P2P_SCAN_SOCIAL_PLUS_ONE:
177 social_channels[3] = freq;
178 params.freqs = social_channels;
179 break;
180 }
181
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800182 ret = wpa_drv_scan(wpa_s, &params);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700183
184 wpabuf_free(ies);
185
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800186 if (ret) {
Dmitry Shmidt04949592012-07-19 12:16:46 -0700187 if (wpa_s->scanning ||
188 wpa_s->scan_res_handler == wpas_p2p_scan_res_handler) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800189 wpa_s->p2p_cb_on_scan_complete = 1;
190 ret = 1;
191 }
Dmitry Shmidt04949592012-07-19 12:16:46 -0700192 } else
193 wpa_s->scan_res_handler = wpas_p2p_scan_res_handler;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800194
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700195 return ret;
196}
197
198
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700199static enum wpa_driver_if_type wpas_p2p_if_type(int p2p_group_interface)
200{
201 switch (p2p_group_interface) {
202 case P2P_GROUP_INTERFACE_PENDING:
203 return WPA_IF_P2P_GROUP;
204 case P2P_GROUP_INTERFACE_GO:
205 return WPA_IF_P2P_GO;
206 case P2P_GROUP_INTERFACE_CLIENT:
207 return WPA_IF_P2P_CLIENT;
208 }
209
210 return WPA_IF_P2P_GROUP;
211}
212
213
214static struct wpa_supplicant * wpas_get_p2p_group(struct wpa_supplicant *wpa_s,
215 const u8 *ssid,
216 size_t ssid_len, int *go)
217{
218 struct wpa_ssid *s;
219
220 for (wpa_s = wpa_s->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
221 for (s = wpa_s->conf->ssid; s; s = s->next) {
222 if (s->disabled != 0 || !s->p2p_group ||
223 s->ssid_len != ssid_len ||
224 os_memcmp(ssid, s->ssid, ssid_len) != 0)
225 continue;
226 if (s->mode == WPAS_MODE_P2P_GO &&
227 s != wpa_s->current_ssid)
228 continue;
229 if (go)
230 *go = s->mode == WPAS_MODE_P2P_GO;
231 return wpa_s;
232 }
233 }
234
235 return NULL;
236}
237
238
Dmitry Shmidt04949592012-07-19 12:16:46 -0700239static void wpas_p2p_group_delete(struct wpa_supplicant *wpa_s, int silent)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700240{
241 struct wpa_ssid *ssid;
242 char *gtype;
243 const char *reason;
244
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700245 ssid = wpa_s->current_ssid;
246 if (ssid == NULL) {
247 /*
248 * The current SSID was not known, but there may still be a
249 * pending P2P group interface waiting for provisioning.
250 */
251 ssid = wpa_s->conf->ssid;
252 while (ssid) {
253 if (ssid->p2p_group &&
254 (ssid->mode == WPAS_MODE_P2P_GROUP_FORMATION ||
255 (ssid->key_mgmt & WPA_KEY_MGMT_WPS)))
256 break;
257 ssid = ssid->next;
258 }
259 }
260 if (wpa_s->p2p_group_interface == P2P_GROUP_INTERFACE_GO)
261 gtype = "GO";
262 else if (wpa_s->p2p_group_interface == P2P_GROUP_INTERFACE_CLIENT ||
263 (ssid && ssid->mode == WPAS_MODE_INFRA)) {
264 wpa_s->reassociate = 0;
265 wpa_s->disconnected = 1;
266 wpa_supplicant_deauthenticate(wpa_s,
267 WLAN_REASON_DEAUTH_LEAVING);
268 gtype = "client";
269 } else
270 gtype = "GO";
271 if (wpa_s->cross_connect_in_use) {
272 wpa_s->cross_connect_in_use = 0;
273 wpa_msg(wpa_s->parent, MSG_INFO,
274 P2P_EVENT_CROSS_CONNECT_DISABLE "%s %s",
275 wpa_s->ifname, wpa_s->cross_connect_uplink);
276 }
277 switch (wpa_s->removal_reason) {
278 case P2P_GROUP_REMOVAL_REQUESTED:
279 reason = " reason=REQUESTED";
280 break;
281 case P2P_GROUP_REMOVAL_IDLE_TIMEOUT:
282 reason = " reason=IDLE";
283 break;
284 case P2P_GROUP_REMOVAL_UNAVAILABLE:
285 reason = " reason=UNAVAILABLE";
286 break;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700287 case P2P_GROUP_REMOVAL_GO_ENDING_SESSION:
288 reason = " reason=GO_ENDING_SESSION";
289 break;
Dmitry Shmidt687922c2012-03-26 14:02:32 -0700290#ifdef ANDROID_P2P
291 case P2P_GROUP_REMOVAL_FREQ_CONFLICT:
292 reason = " reason=FREQ_CONFLICT";
293 break;
294#endif
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700295 default:
296 reason = "";
297 break;
298 }
Dmitry Shmidt04949592012-07-19 12:16:46 -0700299 if (!silent) {
300 wpa_msg(wpa_s->parent, MSG_INFO,
301 P2P_EVENT_GROUP_REMOVED "%s %s%s",
302 wpa_s->ifname, gtype, reason);
303 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700304
Dmitry Shmidt04949592012-07-19 12:16:46 -0700305 if (eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL) > 0)
306 wpa_printf(MSG_DEBUG, "P2P: Cancelled P2P group idle timeout");
307
308 if (!silent && ssid)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700309 wpas_notify_p2p_group_removed(wpa_s, ssid, gtype);
310
311 if (wpa_s->p2p_group_interface != NOT_P2P_GROUP_INTERFACE) {
312 struct wpa_global *global;
313 char *ifname;
314 enum wpa_driver_if_type type;
315 wpa_printf(MSG_DEBUG, "P2P: Remove group interface %s",
316 wpa_s->ifname);
317 global = wpa_s->global;
318 ifname = os_strdup(wpa_s->ifname);
319 type = wpas_p2p_if_type(wpa_s->p2p_group_interface);
Dmitry Shmidte15c7b52011-08-03 15:04:35 -0700320 wpa_supplicant_remove_iface(wpa_s->global, wpa_s, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700321 wpa_s = global->ifaces;
322 if (wpa_s && ifname)
323 wpa_drv_if_remove(wpa_s, type, ifname);
324 os_free(ifname);
325 return;
326 }
327
328 wpa_printf(MSG_DEBUG, "P2P: Remove temporary group network");
329 if (ssid && (ssid->p2p_group ||
330 ssid->mode == WPAS_MODE_P2P_GROUP_FORMATION ||
331 (ssid->key_mgmt & WPA_KEY_MGMT_WPS))) {
332 int id = ssid->id;
Jouni Malinen75ecf522011-06-27 15:19:46 -0700333 if (ssid == wpa_s->current_ssid) {
334 wpa_sm_set_config(wpa_s->wpa, NULL);
335 eapol_sm_notify_config(wpa_s->eapol, NULL, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700336 wpa_s->current_ssid = NULL;
Jouni Malinen75ecf522011-06-27 15:19:46 -0700337 }
338 /*
339 * Networks objects created during any P2P activities are not
340 * exposed out as they might/will confuse certain non-P2P aware
341 * applications since these network objects won't behave like
342 * regular ones.
343 *
344 * Likewise, we don't send out network removed signals for such
345 * network objects.
346 */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700347 wpa_config_remove_network(wpa_s->conf, id);
348 wpa_supplicant_clear_status(wpa_s);
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -0800349 wpa_supplicant_cancel_sched_scan(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700350 } else {
351 wpa_printf(MSG_DEBUG, "P2P: Temporary group network not "
352 "found");
353 }
Dmitry Shmidt04949592012-07-19 12:16:46 -0700354 if (wpa_s->ap_iface)
355 wpa_supplicant_ap_deinit(wpa_s);
356 else
357 wpa_drv_deinit_p2p_cli(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700358}
359
360
361static int wpas_p2p_persistent_group(struct wpa_supplicant *wpa_s,
362 u8 *go_dev_addr,
363 const u8 *ssid, size_t ssid_len)
364{
365 struct wpa_bss *bss;
366 const u8 *bssid;
367 struct wpabuf *p2p;
368 u8 group_capab;
369 const u8 *addr;
370
371 if (wpa_s->go_params)
372 bssid = wpa_s->go_params->peer_interface_addr;
373 else
374 bssid = wpa_s->bssid;
375
376 bss = wpa_bss_get(wpa_s, bssid, ssid, ssid_len);
377 if (bss == NULL) {
378 u8 iface_addr[ETH_ALEN];
379 if (p2p_get_interface_addr(wpa_s->global->p2p, bssid,
380 iface_addr) == 0)
381 bss = wpa_bss_get(wpa_s, iface_addr, ssid, ssid_len);
382 }
383 if (bss == NULL) {
384 wpa_printf(MSG_DEBUG, "P2P: Could not figure out whether "
385 "group is persistent - BSS " MACSTR " not found",
386 MAC2STR(bssid));
387 return 0;
388 }
389
390 p2p = wpa_bss_get_vendor_ie_multi(bss, P2P_IE_VENDOR_TYPE);
391 if (p2p == NULL) {
392 wpa_printf(MSG_DEBUG, "P2P: Could not figure out whether "
393 "group is persistent - BSS " MACSTR
394 " did not include P2P IE", MAC2STR(bssid));
395 wpa_hexdump(MSG_DEBUG, "P2P: Probe Response IEs",
396 (u8 *) (bss + 1), bss->ie_len);
397 wpa_hexdump(MSG_DEBUG, "P2P: Beacon IEs",
398 ((u8 *) bss + 1) + bss->ie_len,
399 bss->beacon_ie_len);
400 return 0;
401 }
402
403 group_capab = p2p_get_group_capab(p2p);
404 addr = p2p_get_go_dev_addr(p2p);
405 wpa_printf(MSG_DEBUG, "P2P: Checking whether group is persistent: "
406 "group_capab=0x%x", group_capab);
407 if (addr) {
408 os_memcpy(go_dev_addr, addr, ETH_ALEN);
409 wpa_printf(MSG_DEBUG, "P2P: GO Device Address " MACSTR,
410 MAC2STR(addr));
411 } else
412 os_memset(go_dev_addr, 0, ETH_ALEN);
413 wpabuf_free(p2p);
414
415 wpa_printf(MSG_DEBUG, "P2P: BSS " MACSTR " group_capab=0x%x "
416 "go_dev_addr=" MACSTR,
417 MAC2STR(bssid), group_capab, MAC2STR(go_dev_addr));
418
419 return group_capab & P2P_GROUP_CAPAB_PERSISTENT_GROUP;
420}
421
422
Jouni Malinen75ecf522011-06-27 15:19:46 -0700423static int wpas_p2p_store_persistent_group(struct wpa_supplicant *wpa_s,
424 struct wpa_ssid *ssid,
425 const u8 *go_dev_addr)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700426{
427 struct wpa_ssid *s;
428 int changed = 0;
429
430 wpa_printf(MSG_DEBUG, "P2P: Storing credentials for a persistent "
431 "group (GO Dev Addr " MACSTR ")", MAC2STR(go_dev_addr));
432 for (s = wpa_s->conf->ssid; s; s = s->next) {
433 if (s->disabled == 2 &&
434 os_memcmp(go_dev_addr, s->bssid, ETH_ALEN) == 0 &&
435 s->ssid_len == ssid->ssid_len &&
436 os_memcmp(ssid->ssid, s->ssid, ssid->ssid_len) == 0)
437 break;
438 }
439
440 if (s) {
441 wpa_printf(MSG_DEBUG, "P2P: Update existing persistent group "
442 "entry");
443 if (ssid->passphrase && !s->passphrase)
444 changed = 1;
445 else if (ssid->passphrase && s->passphrase &&
446 os_strcmp(ssid->passphrase, s->passphrase) != 0)
447 changed = 1;
448 } else {
449 wpa_printf(MSG_DEBUG, "P2P: Create a new persistent group "
450 "entry");
451 changed = 1;
452 s = wpa_config_add_network(wpa_s->conf);
453 if (s == NULL)
Jouni Malinen75ecf522011-06-27 15:19:46 -0700454 return -1;
455
456 /*
457 * Instead of network_added we emit persistent_group_added
458 * notification. Also to keep the defense checks in
459 * persistent_group obj registration method, we set the
460 * relevant flags in s to designate it as a persistent group.
461 */
462 s->p2p_group = 1;
463 s->p2p_persistent_group = 1;
464 wpas_notify_persistent_group_added(wpa_s, s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700465 wpa_config_set_network_defaults(s);
466 }
467
468 s->p2p_group = 1;
469 s->p2p_persistent_group = 1;
470 s->disabled = 2;
471 s->bssid_set = 1;
472 os_memcpy(s->bssid, go_dev_addr, ETH_ALEN);
473 s->mode = ssid->mode;
474 s->auth_alg = WPA_AUTH_ALG_OPEN;
475 s->key_mgmt = WPA_KEY_MGMT_PSK;
476 s->proto = WPA_PROTO_RSN;
477 s->pairwise_cipher = WPA_CIPHER_CCMP;
478 s->export_keys = 1;
479 if (ssid->passphrase) {
480 os_free(s->passphrase);
481 s->passphrase = os_strdup(ssid->passphrase);
482 }
483 if (ssid->psk_set) {
484 s->psk_set = 1;
485 os_memcpy(s->psk, ssid->psk, 32);
486 }
487 if (s->passphrase && !s->psk_set)
488 wpa_config_update_psk(s);
489 if (s->ssid == NULL || s->ssid_len < ssid->ssid_len) {
490 os_free(s->ssid);
491 s->ssid = os_malloc(ssid->ssid_len);
492 }
493 if (s->ssid) {
494 s->ssid_len = ssid->ssid_len;
495 os_memcpy(s->ssid, ssid->ssid, s->ssid_len);
496 }
497
498#ifndef CONFIG_NO_CONFIG_WRITE
499 if (changed && wpa_s->conf->update_config &&
500 wpa_config_write(wpa_s->confname, wpa_s->conf)) {
501 wpa_printf(MSG_DEBUG, "P2P: Failed to update configuration");
502 }
503#endif /* CONFIG_NO_CONFIG_WRITE */
Jouni Malinen75ecf522011-06-27 15:19:46 -0700504
505 return s->id;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700506}
507
508
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800509static void wpas_p2p_add_persistent_group_client(struct wpa_supplicant *wpa_s,
510 const u8 *addr)
511{
512 struct wpa_ssid *ssid, *s;
513 u8 *n;
514 size_t i;
515
516 ssid = wpa_s->current_ssid;
517 if (ssid == NULL || ssid->mode != WPAS_MODE_P2P_GO ||
518 !ssid->p2p_persistent_group)
519 return;
520
521 for (s = wpa_s->parent->conf->ssid; s; s = s->next) {
522 if (s->disabled != 2 || s->mode != WPAS_MODE_P2P_GO)
523 continue;
524
525 if (s->ssid_len == ssid->ssid_len &&
526 os_memcmp(s->ssid, ssid->ssid, s->ssid_len) == 0)
527 break;
528 }
529
530 if (s == NULL)
531 return;
532
533 for (i = 0; s->p2p_client_list && i < s->num_p2p_clients; i++) {
534 if (os_memcmp(s->p2p_client_list + i * ETH_ALEN, addr,
535 ETH_ALEN) == 0)
536 return; /* already in list */
537 }
538
539 n = os_realloc(s->p2p_client_list,
540 (s->num_p2p_clients + 1) * ETH_ALEN);
541 if (n == NULL)
542 return;
543 os_memcpy(n + s->num_p2p_clients * ETH_ALEN, addr, ETH_ALEN);
544 s->p2p_client_list = n;
545 s->num_p2p_clients++;
546
547#ifndef CONFIG_NO_CONFIG_WRITE
548 if (wpa_s->parent->conf->update_config &&
549 wpa_config_write(wpa_s->parent->confname, wpa_s->parent->conf))
550 wpa_printf(MSG_DEBUG, "P2P: Failed to update configuration");
551#endif /* CONFIG_NO_CONFIG_WRITE */
552}
553
554
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700555static void wpas_group_formation_completed(struct wpa_supplicant *wpa_s,
556 int success)
557{
558 struct wpa_ssid *ssid;
559 const char *ssid_txt;
560 int client;
561 int persistent;
562 u8 go_dev_addr[ETH_ALEN];
Jouni Malinen75ecf522011-06-27 15:19:46 -0700563 int network_id = -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700564
565 /*
566 * This callback is likely called for the main interface. Update wpa_s
567 * to use the group interface if a new interface was created for the
568 * group.
569 */
570 if (wpa_s->global->p2p_group_formation)
571 wpa_s = wpa_s->global->p2p_group_formation;
572 wpa_s->global->p2p_group_formation = NULL;
573 wpa_s->p2p_in_provisioning = 0;
574
575 if (!success) {
576 wpa_msg(wpa_s->parent, MSG_INFO,
577 P2P_EVENT_GROUP_FORMATION_FAILURE);
Dmitry Shmidt04949592012-07-19 12:16:46 -0700578 wpas_p2p_group_delete(wpa_s, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700579 return;
580 }
581
582 wpa_msg(wpa_s->parent, MSG_INFO, P2P_EVENT_GROUP_FORMATION_SUCCESS);
583
584 ssid = wpa_s->current_ssid;
585 if (ssid && ssid->mode == WPAS_MODE_P2P_GROUP_FORMATION) {
586 ssid->mode = WPAS_MODE_P2P_GO;
587 p2p_group_notif_formation_done(wpa_s->p2p_group);
588 wpa_supplicant_ap_mac_addr_filter(wpa_s, NULL);
589 }
590
591 persistent = 0;
592 if (ssid) {
593 ssid_txt = wpa_ssid_txt(ssid->ssid, ssid->ssid_len);
594 client = ssid->mode == WPAS_MODE_INFRA;
595 if (ssid->mode == WPAS_MODE_P2P_GO) {
596 persistent = ssid->p2p_persistent_group;
Dmitry Shmidt497c1d52011-07-21 15:19:46 -0700597 os_memcpy(go_dev_addr, wpa_s->global->p2p_dev_addr,
598 ETH_ALEN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700599 } else
600 persistent = wpas_p2p_persistent_group(wpa_s,
601 go_dev_addr,
602 ssid->ssid,
603 ssid->ssid_len);
604 } else {
605 ssid_txt = "";
606 client = wpa_s->p2p_group_interface ==
607 P2P_GROUP_INTERFACE_CLIENT;
608 os_memset(go_dev_addr, 0, ETH_ALEN);
609 }
610
611 wpa_s->show_group_started = 0;
612 if (client) {
613 /*
614 * Indicate event only after successfully completed 4-way
615 * handshake, i.e., when the interface is ready for data
616 * packets.
617 */
618 wpa_s->show_group_started = 1;
619 } else if (ssid && ssid->passphrase == NULL && ssid->psk_set) {
620 char psk[65];
621 wpa_snprintf_hex(psk, sizeof(psk), ssid->psk, 32);
622 wpa_msg(wpa_s->parent, MSG_INFO, P2P_EVENT_GROUP_STARTED
623 "%s GO ssid=\"%s\" freq=%d psk=%s go_dev_addr=" MACSTR
624 "%s",
625 wpa_s->ifname, ssid_txt, ssid->frequency, psk,
626 MAC2STR(go_dev_addr),
627 persistent ? " [PERSISTENT]" : "");
628 wpas_p2p_cross_connect_setup(wpa_s);
629 wpas_p2p_set_group_idle_timeout(wpa_s);
630 } else {
631 wpa_msg(wpa_s->parent, MSG_INFO, P2P_EVENT_GROUP_STARTED
632 "%s GO ssid=\"%s\" freq=%d passphrase=\"%s\" "
633 "go_dev_addr=" MACSTR "%s",
634 wpa_s->ifname, ssid_txt, ssid ? ssid->frequency : 0,
635 ssid && ssid->passphrase ? ssid->passphrase : "",
636 MAC2STR(go_dev_addr),
637 persistent ? " [PERSISTENT]" : "");
638 wpas_p2p_cross_connect_setup(wpa_s);
639 wpas_p2p_set_group_idle_timeout(wpa_s);
640 }
641
642 if (persistent)
Jouni Malinen75ecf522011-06-27 15:19:46 -0700643 network_id = wpas_p2p_store_persistent_group(wpa_s->parent,
644 ssid, go_dev_addr);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800645 if (network_id < 0 && ssid)
Jouni Malinen75ecf522011-06-27 15:19:46 -0700646 network_id = ssid->id;
647 if (!client)
648 wpas_notify_p2p_group_started(wpa_s, ssid, network_id, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700649}
650
651
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800652static void wpas_p2p_send_action_tx_status(struct wpa_supplicant *wpa_s,
653 unsigned int freq,
654 const u8 *dst, const u8 *src,
655 const u8 *bssid,
656 const u8 *data, size_t data_len,
657 enum offchannel_send_action_result
658 result)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700659{
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800660 enum p2p_send_action_result res = P2P_SEND_ACTION_SUCCESS;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700661
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700662 if (wpa_s->global->p2p == NULL || wpa_s->global->p2p_disabled)
663 return;
664 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
665 return;
666
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800667 switch (result) {
668 case OFFCHANNEL_SEND_ACTION_SUCCESS:
669 res = P2P_SEND_ACTION_SUCCESS;
670 break;
671 case OFFCHANNEL_SEND_ACTION_NO_ACK:
672 res = P2P_SEND_ACTION_NO_ACK;
673 break;
674 case OFFCHANNEL_SEND_ACTION_FAILED:
675 res = P2P_SEND_ACTION_FAILED;
676 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700677 }
678
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800679 p2p_send_action_cb(wpa_s->global->p2p, freq, dst, src, bssid, res);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700680
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -0800681 if (result != OFFCHANNEL_SEND_ACTION_SUCCESS &&
682 wpa_s->pending_pd_before_join &&
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800683 (os_memcmp(dst, wpa_s->pending_join_dev_addr, ETH_ALEN) == 0 ||
684 os_memcmp(dst, wpa_s->pending_join_iface_addr, ETH_ALEN) == 0)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700685 wpa_s->pending_pd_before_join = 0;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700686 if (wpa_s->p2p_fallback_to_go_neg) {
687 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: No ACK for PD Req "
688 "during p2p_connect-auto");
689 wpas_p2p_fallback_to_go_neg(wpa_s, 0);
690 return;
691 }
692
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700693 wpa_printf(MSG_DEBUG, "P2P: Starting pending "
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -0800694 "join-existing-group operation (no ACK for PD "
695 "Req)");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700696 wpas_p2p_join_start(wpa_s);
697 }
698}
699
700
701static int wpas_send_action(void *ctx, unsigned int freq, const u8 *dst,
702 const u8 *src, const u8 *bssid, const u8 *buf,
703 size_t len, unsigned int wait_time)
704{
705 struct wpa_supplicant *wpa_s = ctx;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800706 return offchannel_send_action(wpa_s, freq, dst, src, bssid, buf, len,
707 wait_time,
708 wpas_p2p_send_action_tx_status, 1);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700709}
710
711
712static void wpas_send_action_done(void *ctx)
713{
714 struct wpa_supplicant *wpa_s = ctx;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800715 offchannel_send_action_done(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700716}
717
718
719static int wpas_copy_go_neg_results(struct wpa_supplicant *wpa_s,
720 struct p2p_go_neg_results *params)
721{
722 if (wpa_s->go_params == NULL) {
723 wpa_s->go_params = os_malloc(sizeof(*params));
724 if (wpa_s->go_params == NULL)
725 return -1;
726 }
727 os_memcpy(wpa_s->go_params, params, sizeof(*params));
728 return 0;
729}
730
731
732static void wpas_start_wps_enrollee(struct wpa_supplicant *wpa_s,
733 struct p2p_go_neg_results *res)
734{
735 wpa_printf(MSG_DEBUG, "P2P: Start WPS Enrollee for peer " MACSTR,
736 MAC2STR(res->peer_interface_addr));
737 wpa_hexdump_ascii(MSG_DEBUG, "P2P: Start WPS Enrollee for SSID",
738 res->ssid, res->ssid_len);
739 wpa_supplicant_ap_deinit(wpa_s);
740 wpas_copy_go_neg_results(wpa_s, res);
741 if (res->wps_method == WPS_PBC)
742 wpas_wps_start_pbc(wpa_s, res->peer_interface_addr, 1);
743 else {
744 u16 dev_pw_id = DEV_PW_DEFAULT;
745 if (wpa_s->p2p_wps_method == WPS_PIN_KEYPAD)
746 dev_pw_id = DEV_PW_REGISTRAR_SPECIFIED;
747 wpas_wps_start_pin(wpa_s, res->peer_interface_addr,
748 wpa_s->p2p_pin, 1, dev_pw_id);
749 }
750}
751
752
753static void p2p_go_configured(void *ctx, void *data)
754{
755 struct wpa_supplicant *wpa_s = ctx;
756 struct p2p_go_neg_results *params = data;
757 struct wpa_ssid *ssid;
Jouni Malinen75ecf522011-06-27 15:19:46 -0700758 int network_id = -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700759
760 ssid = wpa_s->current_ssid;
761 if (ssid && ssid->mode == WPAS_MODE_P2P_GO) {
762 wpa_printf(MSG_DEBUG, "P2P: Group setup without provisioning");
763 if (wpa_s->global->p2p_group_formation == wpa_s)
764 wpa_s->global->p2p_group_formation = NULL;
765 wpa_msg(wpa_s->parent, MSG_INFO, P2P_EVENT_GROUP_STARTED
766 "%s GO ssid=\"%s\" freq=%d passphrase=\"%s\" "
767 "go_dev_addr=" MACSTR "%s",
768 wpa_s->ifname,
769 wpa_ssid_txt(ssid->ssid, ssid->ssid_len),
770 ssid->frequency,
771 params->passphrase ? params->passphrase : "",
Dmitry Shmidt497c1d52011-07-21 15:19:46 -0700772 MAC2STR(wpa_s->global->p2p_dev_addr),
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700773 params->persistent_group ? " [PERSISTENT]" : "");
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800774
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700775 if (params->persistent_group)
Jouni Malinen75ecf522011-06-27 15:19:46 -0700776 network_id = wpas_p2p_store_persistent_group(
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700777 wpa_s->parent, ssid,
Dmitry Shmidt497c1d52011-07-21 15:19:46 -0700778 wpa_s->global->p2p_dev_addr);
Jouni Malinen75ecf522011-06-27 15:19:46 -0700779 if (network_id < 0)
780 network_id = ssid->id;
781 wpas_notify_p2p_group_started(wpa_s, ssid, network_id, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700782 wpas_p2p_cross_connect_setup(wpa_s);
783 wpas_p2p_set_group_idle_timeout(wpa_s);
784 return;
785 }
786
787 wpa_printf(MSG_DEBUG, "P2P: Setting up WPS for GO provisioning");
788 if (wpa_supplicant_ap_mac_addr_filter(wpa_s,
789 params->peer_interface_addr)) {
790 wpa_printf(MSG_DEBUG, "P2P: Failed to setup MAC address "
791 "filtering");
792 return;
793 }
794 if (params->wps_method == WPS_PBC)
795 wpa_supplicant_ap_wps_pbc(wpa_s, params->peer_interface_addr,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800796 params->peer_device_addr);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700797 else if (wpa_s->p2p_pin[0])
798 wpa_supplicant_ap_wps_pin(wpa_s, params->peer_interface_addr,
799 wpa_s->p2p_pin, NULL, 0);
800 os_free(wpa_s->go_params);
801 wpa_s->go_params = NULL;
802}
803
804
805static void wpas_start_wps_go(struct wpa_supplicant *wpa_s,
806 struct p2p_go_neg_results *params,
807 int group_formation)
808{
809 struct wpa_ssid *ssid;
810
811 if (wpas_copy_go_neg_results(wpa_s, params) < 0)
812 return;
813
814 ssid = wpa_config_add_network(wpa_s->conf);
815 if (ssid == NULL)
816 return;
817
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800818 wpa_s->show_group_started = 0;
819
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700820 wpa_config_set_network_defaults(ssid);
821 ssid->temporary = 1;
822 ssid->p2p_group = 1;
823 ssid->p2p_persistent_group = params->persistent_group;
824 ssid->mode = group_formation ? WPAS_MODE_P2P_GROUP_FORMATION :
825 WPAS_MODE_P2P_GO;
826 ssid->frequency = params->freq;
827 ssid->ssid = os_zalloc(params->ssid_len + 1);
828 if (ssid->ssid) {
829 os_memcpy(ssid->ssid, params->ssid, params->ssid_len);
830 ssid->ssid_len = params->ssid_len;
831 }
832 ssid->auth_alg = WPA_AUTH_ALG_OPEN;
833 ssid->key_mgmt = WPA_KEY_MGMT_PSK;
834 ssid->proto = WPA_PROTO_RSN;
835 ssid->pairwise_cipher = WPA_CIPHER_CCMP;
836 ssid->passphrase = os_strdup(params->passphrase);
837
838 wpa_s->ap_configured_cb = p2p_go_configured;
839 wpa_s->ap_configured_cb_ctx = wpa_s;
840 wpa_s->ap_configured_cb_data = wpa_s->go_params;
Jouni Malinen75ecf522011-06-27 15:19:46 -0700841 wpa_s->connect_without_scan = ssid;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700842 wpa_s->reassociate = 1;
843 wpa_s->disconnected = 0;
844 wpa_supplicant_req_scan(wpa_s, 0, 0);
845}
846
847
848static void wpas_p2p_clone_config(struct wpa_supplicant *dst,
849 const struct wpa_supplicant *src)
850{
851 struct wpa_config *d;
852 const struct wpa_config *s;
853
854 d = dst->conf;
855 s = src->conf;
856
857#define C(n) if (s->n) d->n = os_strdup(s->n)
858 C(device_name);
859 C(manufacturer);
860 C(model_name);
861 C(model_number);
862 C(serial_number);
863 C(config_methods);
864#undef C
865
866 os_memcpy(d->device_type, s->device_type, WPS_DEV_TYPE_LEN);
867 os_memcpy(d->sec_device_type, s->sec_device_type,
868 sizeof(d->sec_device_type));
869 d->num_sec_device_types = s->num_sec_device_types;
870
871 d->p2p_group_idle = s->p2p_group_idle;
872 d->p2p_intra_bss = s->p2p_intra_bss;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800873 d->persistent_reconnect = s->persistent_reconnect;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700874 d->max_num_sta = s->max_num_sta;
875 d->pbc_in_m1 = s->pbc_in_m1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700876}
877
878
879static int wpas_p2p_add_group_interface(struct wpa_supplicant *wpa_s,
880 enum wpa_driver_if_type type)
881{
882 char ifname[120], force_ifname[120];
883
884 if (wpa_s->pending_interface_name[0]) {
885 wpa_printf(MSG_DEBUG, "P2P: Pending virtual interface exists "
886 "- skip creation of a new one");
887 if (is_zero_ether_addr(wpa_s->pending_interface_addr)) {
888 wpa_printf(MSG_DEBUG, "P2P: Pending virtual address "
889 "unknown?! ifname='%s'",
890 wpa_s->pending_interface_name);
891 return -1;
892 }
893 return 0;
894 }
895
896 os_snprintf(ifname, sizeof(ifname), "p2p-%s-%d", wpa_s->ifname,
897 wpa_s->p2p_group_idx);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800898 if (os_strlen(ifname) >= IFNAMSIZ &&
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700899 os_strlen(wpa_s->ifname) < IFNAMSIZ) {
900 /* Try to avoid going over the IFNAMSIZ length limit */
901 os_snprintf(ifname, sizeof(ifname), "p2p-%d",
902 wpa_s->p2p_group_idx);
903 }
904 force_ifname[0] = '\0';
905
906 wpa_printf(MSG_DEBUG, "P2P: Create a new interface %s for the group",
907 ifname);
908 wpa_s->p2p_group_idx++;
909
910 wpa_s->pending_interface_type = type;
911 if (wpa_drv_if_add(wpa_s, type, ifname, NULL, NULL, force_ifname,
912 wpa_s->pending_interface_addr, NULL) < 0) {
913 wpa_printf(MSG_ERROR, "P2P: Failed to create new group "
914 "interface");
915 return -1;
916 }
917
918 if (force_ifname[0]) {
919 wpa_printf(MSG_DEBUG, "P2P: Driver forced interface name %s",
920 force_ifname);
921 os_strlcpy(wpa_s->pending_interface_name, force_ifname,
922 sizeof(wpa_s->pending_interface_name));
923 } else
924 os_strlcpy(wpa_s->pending_interface_name, ifname,
925 sizeof(wpa_s->pending_interface_name));
926 wpa_printf(MSG_DEBUG, "P2P: Created pending virtual interface %s addr "
927 MACSTR, wpa_s->pending_interface_name,
928 MAC2STR(wpa_s->pending_interface_addr));
929
930 return 0;
931}
932
933
934static void wpas_p2p_remove_pending_group_interface(
935 struct wpa_supplicant *wpa_s)
936{
937 if (!wpa_s->pending_interface_name[0] ||
938 is_zero_ether_addr(wpa_s->pending_interface_addr))
939 return; /* No pending virtual interface */
940
941 wpa_printf(MSG_DEBUG, "P2P: Removing pending group interface %s",
942 wpa_s->pending_interface_name);
943 wpa_drv_if_remove(wpa_s, wpa_s->pending_interface_type,
944 wpa_s->pending_interface_name);
945 os_memset(wpa_s->pending_interface_addr, 0, ETH_ALEN);
946 wpa_s->pending_interface_name[0] = '\0';
947}
948
949
950static struct wpa_supplicant *
951wpas_p2p_init_group_interface(struct wpa_supplicant *wpa_s, int go)
952{
953 struct wpa_interface iface;
954 struct wpa_supplicant *group_wpa_s;
955
956 if (!wpa_s->pending_interface_name[0]) {
957 wpa_printf(MSG_ERROR, "P2P: No pending group interface");
958 if (!wpas_p2p_create_iface(wpa_s))
959 return NULL;
960 /*
961 * Something has forced us to remove the pending interface; try
962 * to create a new one and hope for the best that we will get
963 * the same local address.
964 */
965 if (wpas_p2p_add_group_interface(wpa_s, go ? WPA_IF_P2P_GO :
966 WPA_IF_P2P_CLIENT) < 0)
967 return NULL;
968 }
969
970 os_memset(&iface, 0, sizeof(iface));
971 iface.ifname = wpa_s->pending_interface_name;
972 iface.driver = wpa_s->driver->name;
973 iface.ctrl_interface = wpa_s->conf->ctrl_interface;
974 iface.driver_param = wpa_s->conf->driver_param;
975 group_wpa_s = wpa_supplicant_add_iface(wpa_s->global, &iface);
976 if (group_wpa_s == NULL) {
977 wpa_printf(MSG_ERROR, "P2P: Failed to create new "
978 "wpa_supplicant interface");
979 return NULL;
980 }
981 wpa_s->pending_interface_name[0] = '\0';
982 group_wpa_s->parent = wpa_s;
983 group_wpa_s->p2p_group_interface = go ? P2P_GROUP_INTERFACE_GO :
984 P2P_GROUP_INTERFACE_CLIENT;
985 wpa_s->global->p2p_group_formation = group_wpa_s;
986
987 wpas_p2p_clone_config(group_wpa_s, wpa_s);
988
989 return group_wpa_s;
990}
991
992
993static void wpas_p2p_group_formation_timeout(void *eloop_ctx,
994 void *timeout_ctx)
995{
996 struct wpa_supplicant *wpa_s = eloop_ctx;
997 wpa_printf(MSG_DEBUG, "P2P: Group Formation timed out");
998 if (wpa_s->global->p2p)
999 p2p_group_formation_failed(wpa_s->global->p2p);
1000 else if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
1001 wpa_drv_p2p_group_formation_failed(wpa_s);
1002 wpas_group_formation_completed(wpa_s, 0);
1003}
1004
1005
1006void wpas_go_neg_completed(void *ctx, struct p2p_go_neg_results *res)
1007{
1008 struct wpa_supplicant *wpa_s = ctx;
1009
1010 if (wpa_s->off_channel_freq || wpa_s->roc_waiting_drv_freq) {
1011 wpa_drv_cancel_remain_on_channel(wpa_s);
1012 wpa_s->off_channel_freq = 0;
1013 wpa_s->roc_waiting_drv_freq = 0;
1014 }
1015
1016 if (res->status) {
1017 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_GO_NEG_FAILURE "status=%d",
1018 res->status);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001019 wpas_notify_p2p_go_neg_completed(wpa_s, res);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001020 wpas_p2p_remove_pending_group_interface(wpa_s);
1021 return;
1022 }
1023
1024 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_GO_NEG_SUCCESS);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001025 wpas_notify_p2p_go_neg_completed(wpa_s, res);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001026
Dmitry Shmidt04949592012-07-19 12:16:46 -07001027 if (res->role_go && wpa_s->p2p_persistent_id >= 0) {
1028 struct wpa_ssid *ssid;
1029 ssid = wpa_config_get_network(wpa_s->conf,
1030 wpa_s->p2p_persistent_id);
1031 if (ssid && ssid->disabled == 2 &&
1032 ssid->mode == WPAS_MODE_P2P_GO && ssid->passphrase) {
1033 size_t len = os_strlen(ssid->passphrase);
1034 wpa_printf(MSG_DEBUG, "P2P: Override passphrase based "
1035 "on requested persistent group");
1036 os_memcpy(res->passphrase, ssid->passphrase, len);
1037 res->passphrase[len] = '\0';
1038 }
1039 }
1040
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001041 if (wpa_s->create_p2p_iface) {
1042 struct wpa_supplicant *group_wpa_s =
1043 wpas_p2p_init_group_interface(wpa_s, res->role_go);
1044 if (group_wpa_s == NULL) {
1045 wpas_p2p_remove_pending_group_interface(wpa_s);
1046 return;
1047 }
1048 if (group_wpa_s != wpa_s) {
1049 os_memcpy(group_wpa_s->p2p_pin, wpa_s->p2p_pin,
1050 sizeof(group_wpa_s->p2p_pin));
1051 group_wpa_s->p2p_wps_method = wpa_s->p2p_wps_method;
1052 }
1053 os_memset(wpa_s->pending_interface_addr, 0, ETH_ALEN);
1054 wpa_s->pending_interface_name[0] = '\0';
1055 group_wpa_s->p2p_in_provisioning = 1;
1056
1057 if (res->role_go)
1058 wpas_start_wps_go(group_wpa_s, res, 1);
1059 else
1060 wpas_start_wps_enrollee(group_wpa_s, res);
1061 } else {
1062 wpa_s->p2p_in_provisioning = 1;
1063 wpa_s->global->p2p_group_formation = wpa_s;
1064
1065 if (res->role_go)
1066 wpas_start_wps_go(wpa_s, res, 1);
1067 else
1068 wpas_start_wps_enrollee(ctx, res);
1069 }
1070
1071 wpa_s->p2p_long_listen = 0;
1072 eloop_cancel_timeout(wpas_p2p_long_listen_timeout, wpa_s, NULL);
1073
1074 eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s, NULL);
1075 eloop_register_timeout(15 + res->peer_config_timeout / 100,
1076 (res->peer_config_timeout % 100) * 10000,
1077 wpas_p2p_group_formation_timeout, wpa_s, NULL);
1078}
1079
1080
1081void wpas_go_neg_req_rx(void *ctx, const u8 *src, u16 dev_passwd_id)
1082{
1083 struct wpa_supplicant *wpa_s = ctx;
1084 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_GO_NEG_REQUEST MACSTR
1085 " dev_passwd_id=%u", MAC2STR(src), dev_passwd_id);
1086
1087 wpas_notify_p2p_go_neg_req(wpa_s, src, dev_passwd_id);
1088}
1089
1090
1091void wpas_dev_found(void *ctx, const u8 *addr,
1092 const struct p2p_peer_info *info,
1093 int new_device)
1094{
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001095#ifndef CONFIG_NO_STDOUT_DEBUG
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001096 struct wpa_supplicant *wpa_s = ctx;
1097 char devtype[WPS_DEV_TYPE_BUFSIZE];
1098
1099 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_DEVICE_FOUND MACSTR
1100 " p2p_dev_addr=" MACSTR
1101 " pri_dev_type=%s name='%s' config_methods=0x%x "
1102 "dev_capab=0x%x group_capab=0x%x",
1103 MAC2STR(addr), MAC2STR(info->p2p_device_addr),
1104 wps_dev_type_bin2str(info->pri_dev_type, devtype,
1105 sizeof(devtype)),
1106 info->device_name, info->config_methods,
1107 info->dev_capab, info->group_capab);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001108#endif /* CONFIG_NO_STDOUT_DEBUG */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001109
1110 wpas_notify_p2p_device_found(ctx, info->p2p_device_addr, new_device);
1111}
1112
1113
1114static void wpas_dev_lost(void *ctx, const u8 *dev_addr)
1115{
1116 struct wpa_supplicant *wpa_s = ctx;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001117
Dmitry Shmidt497c1d52011-07-21 15:19:46 -07001118 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_DEVICE_LOST
1119 "p2p_dev_addr=" MACSTR, MAC2STR(dev_addr));
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001120
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001121 wpas_notify_p2p_device_lost(wpa_s, dev_addr);
1122}
1123
1124
1125static int wpas_start_listen(void *ctx, unsigned int freq,
1126 unsigned int duration,
1127 const struct wpabuf *probe_resp_ie)
1128{
1129 struct wpa_supplicant *wpa_s = ctx;
1130
1131 wpa_drv_set_ap_wps_ie(wpa_s, NULL, probe_resp_ie, NULL);
1132
1133 if (wpa_drv_probe_req_report(wpa_s, 1) < 0) {
1134 wpa_printf(MSG_DEBUG, "P2P: Failed to request the driver to "
1135 "report received Probe Request frames");
1136 return -1;
1137 }
1138
1139 wpa_s->pending_listen_freq = freq;
1140 wpa_s->pending_listen_duration = duration;
1141
1142 if (wpa_drv_remain_on_channel(wpa_s, freq, duration) < 0) {
1143 wpa_printf(MSG_DEBUG, "P2P: Failed to request the driver "
1144 "to remain on channel (%u MHz) for Listen "
1145 "state", freq);
1146 wpa_s->pending_listen_freq = 0;
1147 return -1;
1148 }
1149 wpa_s->off_channel_freq = 0;
1150 wpa_s->roc_waiting_drv_freq = freq;
1151
1152 return 0;
1153}
1154
1155
1156static void wpas_stop_listen(void *ctx)
1157{
1158 struct wpa_supplicant *wpa_s = ctx;
1159 if (wpa_s->off_channel_freq || wpa_s->roc_waiting_drv_freq) {
1160 wpa_drv_cancel_remain_on_channel(wpa_s);
1161 wpa_s->off_channel_freq = 0;
1162 wpa_s->roc_waiting_drv_freq = 0;
1163 }
1164 wpa_drv_set_ap_wps_ie(wpa_s, NULL, NULL, NULL);
1165 wpa_drv_probe_req_report(wpa_s, 0);
1166}
1167
1168
1169static int wpas_send_probe_resp(void *ctx, const struct wpabuf *buf)
1170{
1171 struct wpa_supplicant *wpa_s = ctx;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001172 return wpa_drv_send_mlme(wpa_s, wpabuf_head(buf), wpabuf_len(buf), 1);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001173}
1174
1175
1176static struct p2p_srv_bonjour *
1177wpas_p2p_service_get_bonjour(struct wpa_supplicant *wpa_s,
1178 const struct wpabuf *query)
1179{
1180 struct p2p_srv_bonjour *bsrv;
1181 size_t len;
1182
1183 len = wpabuf_len(query);
1184 dl_list_for_each(bsrv, &wpa_s->global->p2p_srv_bonjour,
1185 struct p2p_srv_bonjour, list) {
1186 if (len == wpabuf_len(bsrv->query) &&
1187 os_memcmp(wpabuf_head(query), wpabuf_head(bsrv->query),
1188 len) == 0)
1189 return bsrv;
1190 }
1191 return NULL;
1192}
1193
1194
1195static struct p2p_srv_upnp *
1196wpas_p2p_service_get_upnp(struct wpa_supplicant *wpa_s, u8 version,
1197 const char *service)
1198{
1199 struct p2p_srv_upnp *usrv;
1200
1201 dl_list_for_each(usrv, &wpa_s->global->p2p_srv_upnp,
1202 struct p2p_srv_upnp, list) {
1203 if (version == usrv->version &&
1204 os_strcmp(service, usrv->service) == 0)
1205 return usrv;
1206 }
1207 return NULL;
1208}
1209
1210
1211static void wpas_sd_add_proto_not_avail(struct wpabuf *resp, u8 srv_proto,
1212 u8 srv_trans_id)
1213{
1214 u8 *len_pos;
1215
1216 if (wpabuf_tailroom(resp) < 5)
1217 return;
1218
1219 /* Length (to be filled) */
1220 len_pos = wpabuf_put(resp, 2);
1221 wpabuf_put_u8(resp, srv_proto);
1222 wpabuf_put_u8(resp, srv_trans_id);
1223 /* Status Code */
1224 wpabuf_put_u8(resp, P2P_SD_PROTO_NOT_AVAILABLE);
1225 /* Response Data: empty */
1226 WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos - 2);
1227}
1228
1229
1230static void wpas_sd_all_bonjour(struct wpa_supplicant *wpa_s,
1231 struct wpabuf *resp, u8 srv_trans_id)
1232{
1233 struct p2p_srv_bonjour *bsrv;
1234 u8 *len_pos;
1235
1236 wpa_printf(MSG_DEBUG, "P2P: SD Request for all Bonjour services");
1237
1238 if (dl_list_empty(&wpa_s->global->p2p_srv_bonjour)) {
1239 wpa_printf(MSG_DEBUG, "P2P: Bonjour protocol not available");
1240 return;
1241 }
1242
1243 dl_list_for_each(bsrv, &wpa_s->global->p2p_srv_bonjour,
1244 struct p2p_srv_bonjour, list) {
1245 if (wpabuf_tailroom(resp) <
1246 5 + wpabuf_len(bsrv->query) + wpabuf_len(bsrv->resp))
1247 return;
1248 /* Length (to be filled) */
1249 len_pos = wpabuf_put(resp, 2);
1250 wpabuf_put_u8(resp, P2P_SERV_BONJOUR);
1251 wpabuf_put_u8(resp, srv_trans_id);
1252 /* Status Code */
1253 wpabuf_put_u8(resp, P2P_SD_SUCCESS);
1254 wpa_hexdump_ascii(MSG_DEBUG, "P2P: Matching Bonjour service",
1255 wpabuf_head(bsrv->resp),
1256 wpabuf_len(bsrv->resp));
1257 /* Response Data */
1258 wpabuf_put_buf(resp, bsrv->query); /* Key */
1259 wpabuf_put_buf(resp, bsrv->resp); /* Value */
1260 WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos -
1261 2);
1262 }
1263}
1264
1265
1266static void wpas_sd_req_bonjour(struct wpa_supplicant *wpa_s,
1267 struct wpabuf *resp, u8 srv_trans_id,
1268 const u8 *query, size_t query_len)
1269{
1270 struct p2p_srv_bonjour *bsrv;
1271 struct wpabuf buf;
1272 u8 *len_pos;
1273
1274 wpa_hexdump_ascii(MSG_DEBUG, "P2P: SD Request for Bonjour",
1275 query, query_len);
1276 if (dl_list_empty(&wpa_s->global->p2p_srv_bonjour)) {
1277 wpa_printf(MSG_DEBUG, "P2P: Bonjour protocol not available");
1278 wpas_sd_add_proto_not_avail(resp, P2P_SERV_BONJOUR,
1279 srv_trans_id);
1280 return;
1281 }
1282
1283 if (query_len == 0) {
1284 wpas_sd_all_bonjour(wpa_s, resp, srv_trans_id);
1285 return;
1286 }
1287
1288 if (wpabuf_tailroom(resp) < 5)
1289 return;
1290 /* Length (to be filled) */
1291 len_pos = wpabuf_put(resp, 2);
1292 wpabuf_put_u8(resp, P2P_SERV_BONJOUR);
1293 wpabuf_put_u8(resp, srv_trans_id);
1294
1295 wpabuf_set(&buf, query, query_len);
1296 bsrv = wpas_p2p_service_get_bonjour(wpa_s, &buf);
1297 if (bsrv == NULL) {
1298 wpa_printf(MSG_DEBUG, "P2P: Requested Bonjour service not "
1299 "available");
1300
1301 /* Status Code */
1302 wpabuf_put_u8(resp, P2P_SD_REQUESTED_INFO_NOT_AVAILABLE);
1303 /* Response Data: empty */
1304 WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos -
1305 2);
1306 return;
1307 }
1308
1309 /* Status Code */
1310 wpabuf_put_u8(resp, P2P_SD_SUCCESS);
1311 wpa_hexdump_ascii(MSG_DEBUG, "P2P: Matching Bonjour service",
1312 wpabuf_head(bsrv->resp), wpabuf_len(bsrv->resp));
1313
1314 if (wpabuf_tailroom(resp) >=
1315 wpabuf_len(bsrv->query) + wpabuf_len(bsrv->resp)) {
1316 /* Response Data */
1317 wpabuf_put_buf(resp, bsrv->query); /* Key */
1318 wpabuf_put_buf(resp, bsrv->resp); /* Value */
1319 }
1320 WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos - 2);
1321}
1322
1323
1324static void wpas_sd_all_upnp(struct wpa_supplicant *wpa_s,
1325 struct wpabuf *resp, u8 srv_trans_id)
1326{
1327 struct p2p_srv_upnp *usrv;
1328 u8 *len_pos;
1329
1330 wpa_printf(MSG_DEBUG, "P2P: SD Request for all UPnP services");
1331
1332 if (dl_list_empty(&wpa_s->global->p2p_srv_upnp)) {
1333 wpa_printf(MSG_DEBUG, "P2P: UPnP protocol not available");
1334 return;
1335 }
1336
1337 dl_list_for_each(usrv, &wpa_s->global->p2p_srv_upnp,
1338 struct p2p_srv_upnp, list) {
1339 if (wpabuf_tailroom(resp) < 5 + 1 + os_strlen(usrv->service))
1340 return;
1341
1342 /* Length (to be filled) */
1343 len_pos = wpabuf_put(resp, 2);
1344 wpabuf_put_u8(resp, P2P_SERV_UPNP);
1345 wpabuf_put_u8(resp, srv_trans_id);
1346
1347 /* Status Code */
1348 wpabuf_put_u8(resp, P2P_SD_SUCCESS);
1349 /* Response Data */
1350 wpabuf_put_u8(resp, usrv->version);
1351 wpa_printf(MSG_DEBUG, "P2P: Matching UPnP Service: %s",
1352 usrv->service);
1353 wpabuf_put_str(resp, usrv->service);
1354 WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos -
1355 2);
1356 }
1357}
1358
1359
1360static void wpas_sd_req_upnp(struct wpa_supplicant *wpa_s,
1361 struct wpabuf *resp, u8 srv_trans_id,
1362 const u8 *query, size_t query_len)
1363{
1364 struct p2p_srv_upnp *usrv;
1365 u8 *len_pos;
1366 u8 version;
1367 char *str;
1368 int count = 0;
1369
1370 wpa_hexdump_ascii(MSG_DEBUG, "P2P: SD Request for UPnP",
1371 query, query_len);
1372
1373 if (dl_list_empty(&wpa_s->global->p2p_srv_upnp)) {
1374 wpa_printf(MSG_DEBUG, "P2P: UPnP protocol not available");
1375 wpas_sd_add_proto_not_avail(resp, P2P_SERV_UPNP,
1376 srv_trans_id);
1377 return;
1378 }
1379
1380 if (query_len == 0) {
1381 wpas_sd_all_upnp(wpa_s, resp, srv_trans_id);
1382 return;
1383 }
1384
1385 if (wpabuf_tailroom(resp) < 5)
1386 return;
1387
1388 /* Length (to be filled) */
1389 len_pos = wpabuf_put(resp, 2);
1390 wpabuf_put_u8(resp, P2P_SERV_UPNP);
1391 wpabuf_put_u8(resp, srv_trans_id);
1392
1393 version = query[0];
1394 str = os_malloc(query_len);
1395 if (str == NULL)
1396 return;
1397 os_memcpy(str, query + 1, query_len - 1);
1398 str[query_len - 1] = '\0';
1399
1400 dl_list_for_each(usrv, &wpa_s->global->p2p_srv_upnp,
1401 struct p2p_srv_upnp, list) {
1402 if (version != usrv->version)
1403 continue;
1404
1405 if (os_strcmp(str, "ssdp:all") != 0 &&
1406 os_strstr(usrv->service, str) == NULL)
1407 continue;
1408
1409 if (wpabuf_tailroom(resp) < 2)
1410 break;
1411 if (count == 0) {
1412 /* Status Code */
1413 wpabuf_put_u8(resp, P2P_SD_SUCCESS);
1414 /* Response Data */
1415 wpabuf_put_u8(resp, version);
1416 } else
1417 wpabuf_put_u8(resp, ',');
1418
1419 count++;
1420
1421 wpa_printf(MSG_DEBUG, "P2P: Matching UPnP Service: %s",
1422 usrv->service);
1423 if (wpabuf_tailroom(resp) < os_strlen(usrv->service))
1424 break;
1425 wpabuf_put_str(resp, usrv->service);
1426 }
1427 os_free(str);
1428
1429 if (count == 0) {
1430 wpa_printf(MSG_DEBUG, "P2P: Requested UPnP service not "
1431 "available");
1432 /* Status Code */
1433 wpabuf_put_u8(resp, P2P_SD_REQUESTED_INFO_NOT_AVAILABLE);
1434 /* Response Data: empty */
1435 }
1436
1437 WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos - 2);
1438}
1439
1440
1441void wpas_sd_request(void *ctx, int freq, const u8 *sa, u8 dialog_token,
1442 u16 update_indic, const u8 *tlvs, size_t tlvs_len)
1443{
1444 struct wpa_supplicant *wpa_s = ctx;
1445 const u8 *pos = tlvs;
1446 const u8 *end = tlvs + tlvs_len;
1447 const u8 *tlv_end;
1448 u16 slen;
1449 struct wpabuf *resp;
1450 u8 srv_proto, srv_trans_id;
1451 size_t buf_len;
1452 char *buf;
1453
1454 wpa_hexdump(MSG_MSGDUMP, "P2P: Service Discovery Request TLVs",
1455 tlvs, tlvs_len);
1456 buf_len = 2 * tlvs_len + 1;
1457 buf = os_malloc(buf_len);
1458 if (buf) {
1459 wpa_snprintf_hex(buf, buf_len, tlvs, tlvs_len);
1460 wpa_msg_ctrl(wpa_s, MSG_INFO, P2P_EVENT_SERV_DISC_REQ "%d "
1461 MACSTR " %u %u %s",
1462 freq, MAC2STR(sa), dialog_token, update_indic,
1463 buf);
1464 os_free(buf);
1465 }
1466
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001467 if (wpa_s->p2p_sd_over_ctrl_iface) {
1468 wpas_notify_p2p_sd_request(wpa_s, freq, sa, dialog_token,
1469 update_indic, tlvs, tlvs_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001470 return; /* to be processed by an external program */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001471 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001472
1473 resp = wpabuf_alloc(10000);
1474 if (resp == NULL)
1475 return;
1476
1477 while (pos + 1 < end) {
1478 wpa_printf(MSG_DEBUG, "P2P: Service Request TLV");
1479 slen = WPA_GET_LE16(pos);
1480 pos += 2;
1481 if (pos + slen > end || slen < 2) {
1482 wpa_printf(MSG_DEBUG, "P2P: Unexpected Query Data "
1483 "length");
1484 wpabuf_free(resp);
1485 return;
1486 }
1487 tlv_end = pos + slen;
1488
1489 srv_proto = *pos++;
1490 wpa_printf(MSG_DEBUG, "P2P: Service Protocol Type %u",
1491 srv_proto);
1492 srv_trans_id = *pos++;
1493 wpa_printf(MSG_DEBUG, "P2P: Service Transaction ID %u",
1494 srv_trans_id);
1495
1496 wpa_hexdump(MSG_MSGDUMP, "P2P: Query Data",
1497 pos, tlv_end - pos);
1498
1499
1500 if (wpa_s->force_long_sd) {
1501 wpa_printf(MSG_DEBUG, "P2P: SD test - force long "
1502 "response");
1503 wpas_sd_all_bonjour(wpa_s, resp, srv_trans_id);
1504 wpas_sd_all_upnp(wpa_s, resp, srv_trans_id);
1505 goto done;
1506 }
1507
1508 switch (srv_proto) {
1509 case P2P_SERV_ALL_SERVICES:
1510 wpa_printf(MSG_DEBUG, "P2P: Service Discovery Request "
1511 "for all services");
1512 if (dl_list_empty(&wpa_s->global->p2p_srv_upnp) &&
1513 dl_list_empty(&wpa_s->global->p2p_srv_bonjour)) {
1514 wpa_printf(MSG_DEBUG, "P2P: No service "
1515 "discovery protocols available");
1516 wpas_sd_add_proto_not_avail(
1517 resp, P2P_SERV_ALL_SERVICES,
1518 srv_trans_id);
1519 break;
1520 }
1521 wpas_sd_all_bonjour(wpa_s, resp, srv_trans_id);
1522 wpas_sd_all_upnp(wpa_s, resp, srv_trans_id);
1523 break;
1524 case P2P_SERV_BONJOUR:
1525 wpas_sd_req_bonjour(wpa_s, resp, srv_trans_id,
1526 pos, tlv_end - pos);
1527 break;
1528 case P2P_SERV_UPNP:
1529 wpas_sd_req_upnp(wpa_s, resp, srv_trans_id,
1530 pos, tlv_end - pos);
1531 break;
1532 default:
1533 wpa_printf(MSG_DEBUG, "P2P: Unavailable service "
1534 "protocol %u", srv_proto);
1535 wpas_sd_add_proto_not_avail(resp, srv_proto,
1536 srv_trans_id);
1537 break;
1538 }
1539
1540 pos = tlv_end;
1541 }
1542
1543done:
1544 wpas_notify_p2p_sd_request(wpa_s, freq, sa, dialog_token,
1545 update_indic, tlvs, tlvs_len);
1546
1547 wpas_p2p_sd_response(wpa_s, freq, sa, dialog_token, resp);
1548
1549 wpabuf_free(resp);
1550}
1551
1552
1553void wpas_sd_response(void *ctx, const u8 *sa, u16 update_indic,
1554 const u8 *tlvs, size_t tlvs_len)
1555{
1556 struct wpa_supplicant *wpa_s = ctx;
1557 const u8 *pos = tlvs;
1558 const u8 *end = tlvs + tlvs_len;
1559 const u8 *tlv_end;
1560 u16 slen;
1561 size_t buf_len;
1562 char *buf;
1563
1564 wpa_hexdump(MSG_MSGDUMP, "P2P: Service Discovery Response TLVs",
1565 tlvs, tlvs_len);
1566 if (tlvs_len > 1500) {
1567 /* TODO: better way for handling this */
1568 wpa_msg_ctrl(wpa_s, MSG_INFO,
1569 P2P_EVENT_SERV_DISC_RESP MACSTR
1570 " %u <long response: %u bytes>",
1571 MAC2STR(sa), update_indic,
1572 (unsigned int) tlvs_len);
1573 } else {
1574 buf_len = 2 * tlvs_len + 1;
1575 buf = os_malloc(buf_len);
1576 if (buf) {
1577 wpa_snprintf_hex(buf, buf_len, tlvs, tlvs_len);
1578 wpa_msg_ctrl(wpa_s, MSG_INFO,
1579 P2P_EVENT_SERV_DISC_RESP MACSTR " %u %s",
1580 MAC2STR(sa), update_indic, buf);
1581 os_free(buf);
1582 }
1583 }
1584
1585 while (pos < end) {
1586 u8 srv_proto, srv_trans_id, status;
1587
1588 wpa_printf(MSG_DEBUG, "P2P: Service Response TLV");
1589 slen = WPA_GET_LE16(pos);
1590 pos += 2;
1591 if (pos + slen > end || slen < 3) {
1592 wpa_printf(MSG_DEBUG, "P2P: Unexpected Response Data "
1593 "length");
1594 return;
1595 }
1596 tlv_end = pos + slen;
1597
1598 srv_proto = *pos++;
1599 wpa_printf(MSG_DEBUG, "P2P: Service Protocol Type %u",
1600 srv_proto);
1601 srv_trans_id = *pos++;
1602 wpa_printf(MSG_DEBUG, "P2P: Service Transaction ID %u",
1603 srv_trans_id);
1604 status = *pos++;
1605 wpa_printf(MSG_DEBUG, "P2P: Status Code ID %u",
1606 status);
1607
1608 wpa_hexdump(MSG_MSGDUMP, "P2P: Response Data",
1609 pos, tlv_end - pos);
1610
1611 pos = tlv_end;
1612 }
1613
1614 wpas_notify_p2p_sd_response(wpa_s, sa, update_indic, tlvs, tlvs_len);
1615}
1616
1617
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001618u64 wpas_p2p_sd_request(struct wpa_supplicant *wpa_s, const u8 *dst,
1619 const struct wpabuf *tlvs)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001620{
1621 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001622 return wpa_drv_p2p_sd_request(wpa_s, dst, tlvs);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001623 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001624 return 0;
1625 return (uintptr_t) p2p_sd_request(wpa_s->global->p2p, dst, tlvs);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001626}
1627
1628
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001629u64 wpas_p2p_sd_request_upnp(struct wpa_supplicant *wpa_s, const u8 *dst,
1630 u8 version, const char *query)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001631{
1632 struct wpabuf *tlvs;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001633 u64 ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001634
1635 tlvs = wpabuf_alloc(2 + 1 + 1 + 1 + os_strlen(query));
1636 if (tlvs == NULL)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001637 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001638 wpabuf_put_le16(tlvs, 1 + 1 + 1 + os_strlen(query));
1639 wpabuf_put_u8(tlvs, P2P_SERV_UPNP); /* Service Protocol Type */
1640 wpabuf_put_u8(tlvs, 1); /* Service Transaction ID */
1641 wpabuf_put_u8(tlvs, version);
1642 wpabuf_put_str(tlvs, query);
1643 ret = wpas_p2p_sd_request(wpa_s, dst, tlvs);
1644 wpabuf_free(tlvs);
1645 return ret;
1646}
1647
1648
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001649int wpas_p2p_sd_cancel_request(struct wpa_supplicant *wpa_s, u64 req)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001650{
1651 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001652 return wpa_drv_p2p_sd_cancel_request(wpa_s, req);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001653 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
1654 return -1;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001655 return p2p_sd_cancel_request(wpa_s->global->p2p,
1656 (void *) (uintptr_t) req);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001657}
1658
1659
1660void wpas_p2p_sd_response(struct wpa_supplicant *wpa_s, int freq,
1661 const u8 *dst, u8 dialog_token,
1662 const struct wpabuf *resp_tlvs)
1663{
1664 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT) {
1665 wpa_drv_p2p_sd_response(wpa_s, freq, dst, dialog_token,
1666 resp_tlvs);
1667 return;
1668 }
1669 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
1670 return;
1671 p2p_sd_response(wpa_s->global->p2p, freq, dst, dialog_token,
1672 resp_tlvs);
1673}
1674
1675
1676void wpas_p2p_sd_service_update(struct wpa_supplicant *wpa_s)
1677{
1678 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT) {
1679 wpa_drv_p2p_service_update(wpa_s);
1680 return;
1681 }
1682 if (wpa_s->global->p2p)
1683 p2p_sd_service_update(wpa_s->global->p2p);
1684}
1685
1686
1687static void wpas_p2p_srv_bonjour_free(struct p2p_srv_bonjour *bsrv)
1688{
1689 dl_list_del(&bsrv->list);
1690 wpabuf_free(bsrv->query);
1691 wpabuf_free(bsrv->resp);
1692 os_free(bsrv);
1693}
1694
1695
1696static void wpas_p2p_srv_upnp_free(struct p2p_srv_upnp *usrv)
1697{
1698 dl_list_del(&usrv->list);
1699 os_free(usrv->service);
1700 os_free(usrv);
1701}
1702
1703
1704void wpas_p2p_service_flush(struct wpa_supplicant *wpa_s)
1705{
1706 struct p2p_srv_bonjour *bsrv, *bn;
1707 struct p2p_srv_upnp *usrv, *un;
1708
1709 dl_list_for_each_safe(bsrv, bn, &wpa_s->global->p2p_srv_bonjour,
1710 struct p2p_srv_bonjour, list)
1711 wpas_p2p_srv_bonjour_free(bsrv);
1712
1713 dl_list_for_each_safe(usrv, un, &wpa_s->global->p2p_srv_upnp,
1714 struct p2p_srv_upnp, list)
1715 wpas_p2p_srv_upnp_free(usrv);
1716
1717 wpas_p2p_sd_service_update(wpa_s);
1718}
1719
1720
1721int wpas_p2p_service_add_bonjour(struct wpa_supplicant *wpa_s,
1722 struct wpabuf *query, struct wpabuf *resp)
1723{
1724 struct p2p_srv_bonjour *bsrv;
1725
1726 bsrv = wpas_p2p_service_get_bonjour(wpa_s, query);
1727 if (bsrv) {
1728 wpabuf_free(query);
1729 wpabuf_free(bsrv->resp);
1730 bsrv->resp = resp;
1731 return 0;
1732 }
1733
1734 bsrv = os_zalloc(sizeof(*bsrv));
1735 if (bsrv == NULL)
1736 return -1;
1737 bsrv->query = query;
1738 bsrv->resp = resp;
1739 dl_list_add(&wpa_s->global->p2p_srv_bonjour, &bsrv->list);
1740
1741 wpas_p2p_sd_service_update(wpa_s);
1742 return 0;
1743}
1744
1745
1746int wpas_p2p_service_del_bonjour(struct wpa_supplicant *wpa_s,
1747 const struct wpabuf *query)
1748{
1749 struct p2p_srv_bonjour *bsrv;
1750
1751 bsrv = wpas_p2p_service_get_bonjour(wpa_s, query);
1752 if (bsrv == NULL)
1753 return -1;
1754 wpas_p2p_srv_bonjour_free(bsrv);
1755 wpas_p2p_sd_service_update(wpa_s);
1756 return 0;
1757}
1758
1759
1760int wpas_p2p_service_add_upnp(struct wpa_supplicant *wpa_s, u8 version,
1761 const char *service)
1762{
1763 struct p2p_srv_upnp *usrv;
1764
1765 if (wpas_p2p_service_get_upnp(wpa_s, version, service))
1766 return 0; /* Already listed */
1767 usrv = os_zalloc(sizeof(*usrv));
1768 if (usrv == NULL)
1769 return -1;
1770 usrv->version = version;
1771 usrv->service = os_strdup(service);
1772 if (usrv->service == NULL) {
1773 os_free(usrv);
1774 return -1;
1775 }
1776 dl_list_add(&wpa_s->global->p2p_srv_upnp, &usrv->list);
1777
1778 wpas_p2p_sd_service_update(wpa_s);
1779 return 0;
1780}
1781
1782
1783int wpas_p2p_service_del_upnp(struct wpa_supplicant *wpa_s, u8 version,
1784 const char *service)
1785{
1786 struct p2p_srv_upnp *usrv;
1787
1788 usrv = wpas_p2p_service_get_upnp(wpa_s, version, service);
1789 if (usrv == NULL)
1790 return -1;
1791 wpas_p2p_srv_upnp_free(usrv);
1792 wpas_p2p_sd_service_update(wpa_s);
1793 return 0;
1794}
1795
1796
1797static void wpas_prov_disc_local_display(struct wpa_supplicant *wpa_s,
1798 const u8 *peer, const char *params,
1799 unsigned int generated_pin)
1800{
1801 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_SHOW_PIN MACSTR " %08d%s",
1802 MAC2STR(peer), generated_pin, params);
1803}
1804
1805
1806static void wpas_prov_disc_local_keypad(struct wpa_supplicant *wpa_s,
1807 const u8 *peer, const char *params)
1808{
1809 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_ENTER_PIN MACSTR "%s",
1810 MAC2STR(peer), params);
1811}
1812
1813
1814void wpas_prov_disc_req(void *ctx, const u8 *peer, u16 config_methods,
1815 const u8 *dev_addr, const u8 *pri_dev_type,
1816 const char *dev_name, u16 supp_config_methods,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001817 u8 dev_capab, u8 group_capab, const u8 *group_id,
1818 size_t group_id_len)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001819{
1820 struct wpa_supplicant *wpa_s = ctx;
1821 char devtype[WPS_DEV_TYPE_BUFSIZE];
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001822 char params[300];
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001823 u8 empty_dev_type[8];
1824 unsigned int generated_pin = 0;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001825 struct wpa_supplicant *group = NULL;
1826
1827 if (group_id) {
1828 for (group = wpa_s->global->ifaces; group; group = group->next)
1829 {
1830 struct wpa_ssid *s = group->current_ssid;
1831 if (s != NULL &&
1832 s->mode == WPAS_MODE_P2P_GO &&
1833 group_id_len - ETH_ALEN == s->ssid_len &&
1834 os_memcmp(group_id + ETH_ALEN, s->ssid,
1835 s->ssid_len) == 0)
1836 break;
1837 }
1838 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001839
1840 if (pri_dev_type == NULL) {
1841 os_memset(empty_dev_type, 0, sizeof(empty_dev_type));
1842 pri_dev_type = empty_dev_type;
1843 }
1844 os_snprintf(params, sizeof(params), " p2p_dev_addr=" MACSTR
1845 " pri_dev_type=%s name='%s' config_methods=0x%x "
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001846 "dev_capab=0x%x group_capab=0x%x%s%s",
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001847 MAC2STR(dev_addr),
1848 wps_dev_type_bin2str(pri_dev_type, devtype,
1849 sizeof(devtype)),
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001850 dev_name, supp_config_methods, dev_capab, group_capab,
1851 group ? " group=" : "",
1852 group ? group->ifname : "");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001853 params[sizeof(params) - 1] = '\0';
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08001854
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001855 if (config_methods & WPS_CONFIG_DISPLAY) {
1856 generated_pin = wps_generate_pin();
1857 wpas_prov_disc_local_display(wpa_s, peer, params,
1858 generated_pin);
1859 } else if (config_methods & WPS_CONFIG_KEYPAD)
1860 wpas_prov_disc_local_keypad(wpa_s, peer, params);
1861 else if (config_methods & WPS_CONFIG_PUSHBUTTON)
1862 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_PBC_REQ MACSTR
1863 "%s", MAC2STR(peer), params);
Jouni Malinen75ecf522011-06-27 15:19:46 -07001864
1865 wpas_notify_p2p_provision_discovery(wpa_s, peer, 1 /* request */,
1866 P2P_PROV_DISC_SUCCESS,
1867 config_methods, generated_pin);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001868}
1869
1870
1871void wpas_prov_disc_resp(void *ctx, const u8 *peer, u16 config_methods)
1872{
1873 struct wpa_supplicant *wpa_s = ctx;
1874 unsigned int generated_pin = 0;
Dmitry Shmidt04949592012-07-19 12:16:46 -07001875 char params[20];
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001876
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08001877 if (wpa_s->pending_pd_before_join &&
1878 (os_memcmp(peer, wpa_s->pending_join_dev_addr, ETH_ALEN) == 0 ||
1879 os_memcmp(peer, wpa_s->pending_join_iface_addr, ETH_ALEN) == 0)) {
1880 wpa_s->pending_pd_before_join = 0;
1881 wpa_printf(MSG_DEBUG, "P2P: Starting pending "
1882 "join-existing-group operation");
1883 wpas_p2p_join_start(wpa_s);
1884 return;
1885 }
1886
Dmitry Shmidt04949592012-07-19 12:16:46 -07001887 if (wpa_s->pending_pd_use == AUTO_PD_JOIN ||
1888 wpa_s->pending_pd_use == AUTO_PD_GO_NEG)
1889 os_snprintf(params, sizeof(params), " peer_go=%d",
1890 wpa_s->pending_pd_use == AUTO_PD_JOIN);
1891 else
1892 params[0] = '\0';
1893
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001894 if (config_methods & WPS_CONFIG_DISPLAY)
Dmitry Shmidt04949592012-07-19 12:16:46 -07001895 wpas_prov_disc_local_keypad(wpa_s, peer, params);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001896 else if (config_methods & WPS_CONFIG_KEYPAD) {
1897 generated_pin = wps_generate_pin();
Dmitry Shmidt04949592012-07-19 12:16:46 -07001898 wpas_prov_disc_local_display(wpa_s, peer, params,
1899 generated_pin);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001900 } else if (config_methods & WPS_CONFIG_PUSHBUTTON)
Dmitry Shmidt04949592012-07-19 12:16:46 -07001901 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_PBC_RESP MACSTR
1902 "%s", MAC2STR(peer), params);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001903
Jouni Malinen75ecf522011-06-27 15:19:46 -07001904 wpas_notify_p2p_provision_discovery(wpa_s, peer, 0 /* response */,
1905 P2P_PROV_DISC_SUCCESS,
1906 config_methods, generated_pin);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001907}
1908
1909
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001910static void wpas_prov_disc_fail(void *ctx, const u8 *peer,
1911 enum p2p_prov_disc_status status)
Jouni Malinen75ecf522011-06-27 15:19:46 -07001912{
1913 struct wpa_supplicant *wpa_s = ctx;
1914
Dmitry Shmidt04949592012-07-19 12:16:46 -07001915 if (wpa_s->p2p_fallback_to_go_neg) {
1916 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: PD for p2p_connect-auto "
1917 "failed - fall back to GO Negotiation");
1918 wpas_p2p_fallback_to_go_neg(wpa_s, 0);
1919 return;
1920 }
1921
1922 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_FAILURE
1923 " p2p_dev_addr=" MACSTR " status=%d",
1924 MAC2STR(peer), status);
1925
Jouni Malinen75ecf522011-06-27 15:19:46 -07001926 wpas_notify_p2p_provision_discovery(wpa_s, peer, 0 /* response */,
1927 status, 0, 0);
1928}
1929
1930
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001931static u8 wpas_invitation_process(void *ctx, const u8 *sa, const u8 *bssid,
1932 const u8 *go_dev_addr, const u8 *ssid,
1933 size_t ssid_len, int *go, u8 *group_bssid,
1934 int *force_freq, int persistent_group)
1935{
1936 struct wpa_supplicant *wpa_s = ctx;
1937 struct wpa_ssid *s;
1938 u8 cur_bssid[ETH_ALEN];
1939 int res;
1940 struct wpa_supplicant *grp;
1941
1942 if (!persistent_group) {
1943 wpa_printf(MSG_DEBUG, "P2P: Invitation from " MACSTR
1944 " to join an active group", MAC2STR(sa));
1945 if (!is_zero_ether_addr(wpa_s->p2p_auth_invite) &&
1946 (os_memcmp(go_dev_addr, wpa_s->p2p_auth_invite, ETH_ALEN)
1947 == 0 ||
1948 os_memcmp(sa, wpa_s->p2p_auth_invite, ETH_ALEN) == 0)) {
1949 wpa_printf(MSG_DEBUG, "P2P: Accept previously "
1950 "authorized invitation");
1951 goto accept_inv;
1952 }
1953 /*
1954 * Do not accept the invitation automatically; notify user and
1955 * request approval.
1956 */
1957 return P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE;
1958 }
1959
1960 grp = wpas_get_p2p_group(wpa_s, ssid, ssid_len, go);
1961 if (grp) {
1962 wpa_printf(MSG_DEBUG, "P2P: Accept invitation to already "
1963 "running persistent group");
1964 if (*go)
1965 os_memcpy(group_bssid, grp->own_addr, ETH_ALEN);
1966 goto accept_inv;
1967 }
1968
1969 if (!wpa_s->conf->persistent_reconnect)
1970 return P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE;
1971
1972 for (s = wpa_s->conf->ssid; s; s = s->next) {
1973 if (s->disabled == 2 &&
1974 os_memcmp(s->bssid, go_dev_addr, ETH_ALEN) == 0 &&
1975 s->ssid_len == ssid_len &&
1976 os_memcmp(ssid, s->ssid, ssid_len) == 0)
1977 break;
1978 }
1979
1980 if (!s) {
1981 wpa_printf(MSG_DEBUG, "P2P: Invitation from " MACSTR
1982 " requested reinvocation of an unknown group",
1983 MAC2STR(sa));
1984 return P2P_SC_FAIL_UNKNOWN_GROUP;
1985 }
1986
1987 if (s->mode == WPAS_MODE_P2P_GO && !wpas_p2p_create_iface(wpa_s)) {
1988 *go = 1;
1989 if (wpa_s->wpa_state >= WPA_AUTHENTICATING) {
1990 wpa_printf(MSG_DEBUG, "P2P: The only available "
1991 "interface is already in use - reject "
1992 "invitation");
1993 return P2P_SC_FAIL_UNABLE_TO_ACCOMMODATE;
1994 }
1995 os_memcpy(group_bssid, wpa_s->own_addr, ETH_ALEN);
1996 } else if (s->mode == WPAS_MODE_P2P_GO) {
1997 *go = 1;
1998 if (wpas_p2p_add_group_interface(wpa_s, WPA_IF_P2P_GO) < 0)
1999 {
2000 wpa_printf(MSG_ERROR, "P2P: Failed to allocate a new "
2001 "interface address for the group");
2002 return P2P_SC_FAIL_UNABLE_TO_ACCOMMODATE;
2003 }
2004 os_memcpy(group_bssid, wpa_s->pending_interface_addr,
2005 ETH_ALEN);
2006 }
2007
2008accept_inv:
2009 if (wpa_s->current_ssid && wpa_drv_get_bssid(wpa_s, cur_bssid) == 0 &&
2010 wpa_s->assoc_freq) {
2011 wpa_printf(MSG_DEBUG, "P2P: Trying to force channel to match "
2012 "the channel we are already using");
2013 *force_freq = wpa_s->assoc_freq;
2014 }
2015
2016 res = wpa_drv_shared_freq(wpa_s);
2017 if (res > 0) {
2018 wpa_printf(MSG_DEBUG, "P2P: Trying to force channel to match "
2019 "with the channel we are already using on a "
2020 "shared interface");
2021 *force_freq = res;
2022 }
2023
2024 return P2P_SC_SUCCESS;
2025}
2026
2027
2028static void wpas_invitation_received(void *ctx, const u8 *sa, const u8 *bssid,
2029 const u8 *ssid, size_t ssid_len,
2030 const u8 *go_dev_addr, u8 status,
2031 int op_freq)
2032{
2033 struct wpa_supplicant *wpa_s = ctx;
2034 struct wpa_ssid *s;
2035
2036 for (s = wpa_s->conf->ssid; s; s = s->next) {
2037 if (s->disabled == 2 &&
2038 s->ssid_len == ssid_len &&
2039 os_memcmp(ssid, s->ssid, ssid_len) == 0)
2040 break;
2041 }
2042
2043 if (status == P2P_SC_SUCCESS) {
2044 wpa_printf(MSG_DEBUG, "P2P: Invitation from peer " MACSTR
2045 " was accepted; op_freq=%d MHz",
2046 MAC2STR(sa), op_freq);
2047 if (s) {
2048 wpas_p2p_group_add_persistent(
2049 wpa_s, s, s->mode == WPAS_MODE_P2P_GO, 0);
2050 } else if (bssid) {
2051 wpas_p2p_join(wpa_s, bssid, go_dev_addr,
Dmitry Shmidt04949592012-07-19 12:16:46 -07002052 wpa_s->p2p_wps_method, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002053 }
2054 return;
2055 }
2056
2057 if (status != P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE) {
2058 wpa_printf(MSG_DEBUG, "P2P: Invitation from peer " MACSTR
2059 " was rejected (status %u)", MAC2STR(sa), status);
2060 return;
2061 }
2062
2063 if (!s) {
2064 if (bssid) {
2065 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_INVITATION_RECEIVED
2066 "sa=" MACSTR " go_dev_addr=" MACSTR
2067 " bssid=" MACSTR " unknown-network",
2068 MAC2STR(sa), MAC2STR(go_dev_addr),
2069 MAC2STR(bssid));
2070 } else {
2071 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_INVITATION_RECEIVED
2072 "sa=" MACSTR " go_dev_addr=" MACSTR
2073 " unknown-network",
2074 MAC2STR(sa), MAC2STR(go_dev_addr));
2075 }
2076 return;
2077 }
2078
2079 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_INVITATION_RECEIVED "sa=" MACSTR
2080 " persistent=%d", MAC2STR(sa), s->id);
2081}
2082
2083
2084static void wpas_invitation_result(void *ctx, int status, const u8 *bssid)
2085{
2086 struct wpa_supplicant *wpa_s = ctx;
2087 struct wpa_ssid *ssid;
2088
2089 if (bssid) {
2090 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_INVITATION_RESULT
2091 "status=%d " MACSTR,
2092 status, MAC2STR(bssid));
2093 } else {
2094 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_INVITATION_RESULT
2095 "status=%d ", status);
2096 }
2097 wpas_notify_p2p_invitation_result(wpa_s, status, bssid);
2098
2099 if (wpa_s->pending_invite_ssid_id == -1)
2100 return; /* Invitation to active group */
2101
2102 if (status != P2P_SC_SUCCESS) {
2103 wpas_p2p_remove_pending_group_interface(wpa_s);
2104 return;
2105 }
2106
2107 ssid = wpa_config_get_network(wpa_s->conf,
2108 wpa_s->pending_invite_ssid_id);
2109 if (ssid == NULL) {
2110 wpa_printf(MSG_ERROR, "P2P: Could not find persistent group "
2111 "data matching with invitation");
2112 return;
2113 }
2114
2115 wpas_p2p_group_add_persistent(wpa_s, ssid,
2116 ssid->mode == WPAS_MODE_P2P_GO, 0);
2117}
2118
2119
Dmitry Shmidt04949592012-07-19 12:16:46 -07002120static int wpas_p2p_disallowed_freq(struct wpa_global *global,
2121 unsigned int freq)
2122{
2123 unsigned int i;
2124
2125 if (global->p2p_disallow_freq == NULL)
2126 return 0;
2127
2128 for (i = 0; i < global->num_p2p_disallow_freq; i++) {
2129 if (freq >= global->p2p_disallow_freq[i].min &&
2130 freq <= global->p2p_disallow_freq[i].max)
2131 return 1;
2132 }
2133
2134 return 0;
2135}
2136
2137
2138static void wpas_p2p_add_chan(struct p2p_reg_class *reg, u8 chan)
2139{
2140 reg->channel[reg->channels] = chan;
2141 reg->channels++;
2142}
2143
2144
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002145static int wpas_p2p_default_channels(struct wpa_supplicant *wpa_s,
2146 struct p2p_channels *chan)
2147{
2148 int i, cla = 0;
2149
2150 wpa_printf(MSG_DEBUG, "P2P: Enable operating classes for 2.4 GHz "
2151 "band");
2152
2153 /* Operating class 81 - 2.4 GHz band channels 1..13 */
2154 chan->reg_class[cla].reg_class = 81;
Dmitry Shmidt04949592012-07-19 12:16:46 -07002155 chan->reg_class[cla].channels = 0;
2156 for (i = 0; i < 11; i++) {
2157 if (!wpas_p2p_disallowed_freq(wpa_s->global, 2412 + i * 5))
2158 wpas_p2p_add_chan(&chan->reg_class[cla], i + 1);
2159 }
2160 if (chan->reg_class[cla].channels)
2161 cla++;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002162
2163 wpa_printf(MSG_DEBUG, "P2P: Enable operating classes for lower 5 GHz "
2164 "band");
2165
2166 /* Operating class 115 - 5 GHz, channels 36-48 */
2167 chan->reg_class[cla].reg_class = 115;
Dmitry Shmidt04949592012-07-19 12:16:46 -07002168 chan->reg_class[cla].channels = 0;
2169 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 36 * 5))
2170 wpas_p2p_add_chan(&chan->reg_class[cla], 36);
2171 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 40 * 5))
2172 wpas_p2p_add_chan(&chan->reg_class[cla], 40);
2173 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 44 * 5))
2174 wpas_p2p_add_chan(&chan->reg_class[cla], 44);
2175 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 48 * 5))
2176 wpas_p2p_add_chan(&chan->reg_class[cla], 48);
2177 if (chan->reg_class[cla].channels)
2178 cla++;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002179
2180 wpa_printf(MSG_DEBUG, "P2P: Enable operating classes for higher 5 GHz "
2181 "band");
2182
2183 /* Operating class 124 - 5 GHz, channels 149,153,157,161 */
2184 chan->reg_class[cla].reg_class = 124;
Dmitry Shmidt04949592012-07-19 12:16:46 -07002185 chan->reg_class[cla].channels = 0;
2186 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 149 * 5))
2187 wpas_p2p_add_chan(&chan->reg_class[cla], 149);
2188 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 153 * 5))
2189 wpas_p2p_add_chan(&chan->reg_class[cla], 153);
2190 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 156 * 5))
2191 wpas_p2p_add_chan(&chan->reg_class[cla], 157);
2192 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 161 * 5))
2193 wpas_p2p_add_chan(&chan->reg_class[cla], 161);
2194 if (chan->reg_class[cla].channels)
2195 cla++;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002196
2197 chan->reg_classes = cla;
2198 return 0;
2199}
2200
2201
2202static struct hostapd_hw_modes * get_mode(struct hostapd_hw_modes *modes,
2203 u16 num_modes,
2204 enum hostapd_hw_mode mode)
2205{
2206 u16 i;
2207
2208 for (i = 0; i < num_modes; i++) {
2209 if (modes[i].mode == mode)
2210 return &modes[i];
2211 }
2212
2213 return NULL;
2214}
2215
2216
Dmitry Shmidt04949592012-07-19 12:16:46 -07002217static int has_channel(struct wpa_global *global,
2218 struct hostapd_hw_modes *mode, u8 chan, int *flags)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002219{
2220 int i;
Dmitry Shmidt04949592012-07-19 12:16:46 -07002221 unsigned int freq;
2222
2223 freq = (mode->mode == HOSTAPD_MODE_IEEE80211A ? 5000 : 2407) +
2224 chan * 5;
2225 if (wpas_p2p_disallowed_freq(global, freq))
2226 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002227
2228 for (i = 0; i < mode->num_channels; i++) {
2229 if (mode->channels[i].chan == chan) {
2230 if (flags)
2231 *flags = mode->channels[i].flag;
2232 return !(mode->channels[i].flag &
2233 (HOSTAPD_CHAN_DISABLED |
2234 HOSTAPD_CHAN_PASSIVE_SCAN |
2235 HOSTAPD_CHAN_NO_IBSS |
2236 HOSTAPD_CHAN_RADAR));
2237 }
2238 }
2239
2240 return 0;
2241}
2242
2243
2244struct p2p_oper_class_map {
2245 enum hostapd_hw_mode mode;
2246 u8 op_class;
2247 u8 min_chan;
2248 u8 max_chan;
2249 u8 inc;
2250 enum { BW20, BW40PLUS, BW40MINUS } bw;
2251};
2252
2253static int wpas_p2p_setup_channels(struct wpa_supplicant *wpa_s,
2254 struct p2p_channels *chan)
2255{
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002256 struct hostapd_hw_modes *mode;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002257 int cla, op;
2258 struct p2p_oper_class_map op_class[] = {
2259 { HOSTAPD_MODE_IEEE80211G, 81, 1, 13, 1, BW20 },
2260 { HOSTAPD_MODE_IEEE80211G, 82, 14, 14, 1, BW20 },
2261#if 0 /* Do not enable HT40 on 2 GHz for now */
2262 { HOSTAPD_MODE_IEEE80211G, 83, 1, 9, 1, BW40PLUS },
2263 { HOSTAPD_MODE_IEEE80211G, 84, 5, 13, 1, BW40MINUS },
2264#endif
2265 { HOSTAPD_MODE_IEEE80211A, 115, 36, 48, 4, BW20 },
2266 { HOSTAPD_MODE_IEEE80211A, 124, 149, 161, 4, BW20 },
2267 { HOSTAPD_MODE_IEEE80211A, 116, 36, 44, 8, BW40PLUS },
2268 { HOSTAPD_MODE_IEEE80211A, 117, 40, 48, 8, BW40MINUS },
2269 { HOSTAPD_MODE_IEEE80211A, 126, 149, 157, 8, BW40PLUS },
2270 { HOSTAPD_MODE_IEEE80211A, 127, 153, 161, 8, BW40MINUS },
2271 { -1, 0, 0, 0, 0, BW20 }
2272 };
2273
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002274 if (wpa_s->hw.modes == NULL) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002275 wpa_printf(MSG_DEBUG, "P2P: Driver did not support fetching "
2276 "of all supported channels; assume dualband "
2277 "support");
2278 return wpas_p2p_default_channels(wpa_s, chan);
2279 }
2280
2281 cla = 0;
2282
2283 for (op = 0; op_class[op].op_class; op++) {
2284 struct p2p_oper_class_map *o = &op_class[op];
2285 u8 ch;
2286 struct p2p_reg_class *reg = NULL;
2287
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002288 mode = get_mode(wpa_s->hw.modes, wpa_s->hw.num_modes, o->mode);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002289 if (mode == NULL)
2290 continue;
2291 for (ch = o->min_chan; ch <= o->max_chan; ch += o->inc) {
2292 int flag;
Dmitry Shmidt04949592012-07-19 12:16:46 -07002293 if (!has_channel(wpa_s->global, mode, ch, &flag))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002294 continue;
2295 if (o->bw == BW40MINUS &&
2296 (!(flag & HOSTAPD_CHAN_HT40MINUS) ||
Dmitry Shmidt04949592012-07-19 12:16:46 -07002297 !has_channel(wpa_s->global, mode, ch - 4, NULL)))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002298 continue;
2299 if (o->bw == BW40PLUS &&
2300 (!(flag & HOSTAPD_CHAN_HT40PLUS) ||
Dmitry Shmidt04949592012-07-19 12:16:46 -07002301 !has_channel(wpa_s->global, mode, ch + 4, NULL)))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002302 continue;
2303 if (reg == NULL) {
2304 wpa_printf(MSG_DEBUG, "P2P: Add operating "
2305 "class %u", o->op_class);
2306 reg = &chan->reg_class[cla];
2307 cla++;
2308 reg->reg_class = o->op_class;
2309 }
2310 reg->channel[reg->channels] = ch;
2311 reg->channels++;
2312 }
2313 if (reg) {
2314 wpa_hexdump(MSG_DEBUG, "P2P: Channels",
2315 reg->channel, reg->channels);
2316 }
2317 }
2318
2319 chan->reg_classes = cla;
2320
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002321 return 0;
2322}
2323
2324
2325static int wpas_get_noa(void *ctx, const u8 *interface_addr, u8 *buf,
2326 size_t buf_len)
2327{
2328 struct wpa_supplicant *wpa_s = ctx;
2329
2330 for (wpa_s = wpa_s->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
2331 if (os_memcmp(wpa_s->own_addr, interface_addr, ETH_ALEN) == 0)
2332 break;
2333 }
2334 if (wpa_s == NULL)
2335 return -1;
2336
2337 return wpa_drv_get_noa(wpa_s, buf, buf_len);
2338}
2339
2340
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08002341static int wpas_go_connected(void *ctx, const u8 *dev_addr)
2342{
2343 struct wpa_supplicant *wpa_s = ctx;
2344
2345 for (wpa_s = wpa_s->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
2346 struct wpa_ssid *ssid = wpa_s->current_ssid;
2347 if (ssid == NULL)
2348 continue;
2349 if (ssid->mode != WPAS_MODE_INFRA)
2350 continue;
2351 if (wpa_s->wpa_state != WPA_COMPLETED &&
2352 wpa_s->wpa_state != WPA_GROUP_HANDSHAKE)
2353 continue;
2354 if (os_memcmp(wpa_s->go_dev_addr, dev_addr, ETH_ALEN) == 0)
2355 return 1;
2356 }
2357
2358 return 0;
2359}
2360
2361
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002362/**
2363 * wpas_p2p_init - Initialize P2P module for %wpa_supplicant
2364 * @global: Pointer to global data from wpa_supplicant_init()
2365 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
2366 * Returns: 0 on success, -1 on failure
2367 */
2368int wpas_p2p_init(struct wpa_global *global, struct wpa_supplicant *wpa_s)
2369{
2370 struct p2p_config p2p;
2371 unsigned int r;
2372 int i;
2373
2374 if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_CAPABLE))
2375 return 0;
2376
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002377 if (global->p2p)
2378 return 0;
2379
2380 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT) {
2381 struct p2p_params params;
2382
2383 wpa_printf(MSG_DEBUG, "P2P: Use driver-based P2P management");
2384 os_memset(&params, 0, sizeof(params));
2385 params.dev_name = wpa_s->conf->device_name;
2386 os_memcpy(params.pri_dev_type, wpa_s->conf->device_type,
2387 WPS_DEV_TYPE_LEN);
2388 params.num_sec_dev_types = wpa_s->conf->num_sec_device_types;
2389 os_memcpy(params.sec_dev_type,
2390 wpa_s->conf->sec_device_type,
2391 params.num_sec_dev_types * WPS_DEV_TYPE_LEN);
2392
2393 if (wpa_drv_p2p_set_params(wpa_s, &params) < 0)
2394 return -1;
2395
2396 return 0;
2397 }
2398
2399 os_memset(&p2p, 0, sizeof(p2p));
2400 p2p.msg_ctx = wpa_s;
2401 p2p.cb_ctx = wpa_s;
2402 p2p.p2p_scan = wpas_p2p_scan;
2403 p2p.send_action = wpas_send_action;
2404 p2p.send_action_done = wpas_send_action_done;
2405 p2p.go_neg_completed = wpas_go_neg_completed;
2406 p2p.go_neg_req_rx = wpas_go_neg_req_rx;
2407 p2p.dev_found = wpas_dev_found;
2408 p2p.dev_lost = wpas_dev_lost;
2409 p2p.start_listen = wpas_start_listen;
2410 p2p.stop_listen = wpas_stop_listen;
2411 p2p.send_probe_resp = wpas_send_probe_resp;
2412 p2p.sd_request = wpas_sd_request;
2413 p2p.sd_response = wpas_sd_response;
2414 p2p.prov_disc_req = wpas_prov_disc_req;
2415 p2p.prov_disc_resp = wpas_prov_disc_resp;
Jouni Malinen75ecf522011-06-27 15:19:46 -07002416 p2p.prov_disc_fail = wpas_prov_disc_fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002417 p2p.invitation_process = wpas_invitation_process;
2418 p2p.invitation_received = wpas_invitation_received;
2419 p2p.invitation_result = wpas_invitation_result;
2420 p2p.get_noa = wpas_get_noa;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08002421 p2p.go_connected = wpas_go_connected;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002422
2423 os_memcpy(wpa_s->global->p2p_dev_addr, wpa_s->own_addr, ETH_ALEN);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002424 os_memcpy(p2p.dev_addr, wpa_s->global->p2p_dev_addr, ETH_ALEN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002425 p2p.dev_name = wpa_s->conf->device_name;
2426 p2p.manufacturer = wpa_s->conf->manufacturer;
2427 p2p.model_name = wpa_s->conf->model_name;
2428 p2p.model_number = wpa_s->conf->model_number;
2429 p2p.serial_number = wpa_s->conf->serial_number;
2430 if (wpa_s->wps) {
2431 os_memcpy(p2p.uuid, wpa_s->wps->uuid, 16);
2432 p2p.config_methods = wpa_s->wps->config_methods;
2433 }
2434
2435 if (wpa_s->conf->p2p_listen_reg_class &&
2436 wpa_s->conf->p2p_listen_channel) {
2437 p2p.reg_class = wpa_s->conf->p2p_listen_reg_class;
2438 p2p.channel = wpa_s->conf->p2p_listen_channel;
2439 } else {
2440 p2p.reg_class = 81;
2441 /*
2442 * Pick one of the social channels randomly as the listen
2443 * channel.
2444 */
2445 os_get_random((u8 *) &r, sizeof(r));
2446 p2p.channel = 1 + (r % 3) * 5;
2447 }
2448 wpa_printf(MSG_DEBUG, "P2P: Own listen channel: %d", p2p.channel);
2449
2450 if (wpa_s->conf->p2p_oper_reg_class &&
2451 wpa_s->conf->p2p_oper_channel) {
2452 p2p.op_reg_class = wpa_s->conf->p2p_oper_reg_class;
2453 p2p.op_channel = wpa_s->conf->p2p_oper_channel;
2454 p2p.cfg_op_channel = 1;
2455 wpa_printf(MSG_DEBUG, "P2P: Configured operating channel: "
2456 "%d:%d", p2p.op_reg_class, p2p.op_channel);
2457
2458 } else {
2459 p2p.op_reg_class = 81;
2460 /*
2461 * Use random operation channel from (1, 6, 11) if no other
2462 * preference is indicated.
2463 */
2464 os_get_random((u8 *) &r, sizeof(r));
2465 p2p.op_channel = 1 + (r % 3) * 5;
2466 p2p.cfg_op_channel = 0;
2467 wpa_printf(MSG_DEBUG, "P2P: Random operating channel: "
2468 "%d:%d", p2p.op_reg_class, p2p.op_channel);
2469 }
2470 if (wpa_s->conf->country[0] && wpa_s->conf->country[1]) {
2471 os_memcpy(p2p.country, wpa_s->conf->country, 2);
2472 p2p.country[2] = 0x04;
2473 } else
2474 os_memcpy(p2p.country, "XX\x04", 3);
2475
2476 if (wpas_p2p_setup_channels(wpa_s, &p2p.channels)) {
2477 wpa_printf(MSG_ERROR, "P2P: Failed to configure supported "
2478 "channel list");
2479 return -1;
2480 }
2481
2482 os_memcpy(p2p.pri_dev_type, wpa_s->conf->device_type,
2483 WPS_DEV_TYPE_LEN);
2484
2485 p2p.num_sec_dev_types = wpa_s->conf->num_sec_device_types;
2486 os_memcpy(p2p.sec_dev_type, wpa_s->conf->sec_device_type,
2487 p2p.num_sec_dev_types * WPS_DEV_TYPE_LEN);
2488
2489 p2p.concurrent_operations = !!(wpa_s->drv_flags &
2490 WPA_DRIVER_FLAGS_P2P_CONCURRENT);
2491
2492 p2p.max_peers = 100;
2493
2494 if (wpa_s->conf->p2p_ssid_postfix) {
2495 p2p.ssid_postfix_len =
2496 os_strlen(wpa_s->conf->p2p_ssid_postfix);
2497 if (p2p.ssid_postfix_len > sizeof(p2p.ssid_postfix))
2498 p2p.ssid_postfix_len = sizeof(p2p.ssid_postfix);
2499 os_memcpy(p2p.ssid_postfix, wpa_s->conf->p2p_ssid_postfix,
2500 p2p.ssid_postfix_len);
2501 }
2502
2503 p2p.p2p_intra_bss = wpa_s->conf->p2p_intra_bss;
2504
2505 global->p2p = p2p_init(&p2p);
2506 if (global->p2p == NULL)
2507 return -1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07002508 global->p2p_init_wpa_s = wpa_s;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002509
2510 for (i = 0; i < MAX_WPS_VENDOR_EXT; i++) {
2511 if (wpa_s->conf->wps_vendor_ext[i] == NULL)
2512 continue;
2513 p2p_add_wps_vendor_extension(
2514 global->p2p, wpa_s->conf->wps_vendor_ext[i]);
2515 }
2516
2517 return 0;
2518}
2519
2520
2521/**
2522 * wpas_p2p_deinit - Deinitialize per-interface P2P data
2523 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
2524 *
2525 * This function deinitialize per-interface P2P data.
2526 */
2527void wpas_p2p_deinit(struct wpa_supplicant *wpa_s)
2528{
2529 if (wpa_s->driver && wpa_s->drv_priv)
2530 wpa_drv_probe_req_report(wpa_s, 0);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002531
2532 if (wpa_s->go_params) {
2533 /* Clear any stored provisioning info */
2534 p2p_clear_provisioning_info(
2535 wpa_s->global->p2p,
Dmitry Shmidt04949592012-07-19 12:16:46 -07002536 wpa_s->go_params->peer_device_addr);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002537 }
2538
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002539 os_free(wpa_s->go_params);
2540 wpa_s->go_params = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002541 eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s, NULL);
2542 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08002543 eloop_cancel_timeout(wpas_p2p_pd_before_join_timeout, wpa_s, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002544 wpa_s->p2p_long_listen = 0;
2545 eloop_cancel_timeout(wpas_p2p_long_listen_timeout, wpa_s, NULL);
2546 eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL);
2547 wpas_p2p_remove_pending_group_interface(wpa_s);
2548
2549 /* TODO: remove group interface from the driver if this wpa_s instance
2550 * is on top of a P2P group interface */
2551}
2552
2553
2554/**
2555 * wpas_p2p_deinit_global - Deinitialize global P2P module
2556 * @global: Pointer to global data from wpa_supplicant_init()
2557 *
2558 * This function deinitializes the global (per device) P2P module.
2559 */
2560void wpas_p2p_deinit_global(struct wpa_global *global)
2561{
2562 struct wpa_supplicant *wpa_s, *tmp;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002563
2564 if (global->p2p == NULL)
2565 return;
2566
2567 /* Remove remaining P2P group interfaces */
2568 wpa_s = global->ifaces;
2569 if (wpa_s)
2570 wpas_p2p_service_flush(wpa_s);
2571 while (wpa_s && wpa_s->p2p_group_interface != NOT_P2P_GROUP_INTERFACE)
2572 wpa_s = wpa_s->next;
2573 while (wpa_s) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002574 tmp = global->ifaces;
2575 while (tmp &&
2576 (tmp == wpa_s ||
2577 tmp->p2p_group_interface == NOT_P2P_GROUP_INTERFACE)) {
2578 tmp = tmp->next;
2579 }
2580 if (tmp == NULL)
2581 break;
Dmitry Shmidt04949592012-07-19 12:16:46 -07002582 /* Disconnect from the P2P group and deinit the interface */
2583 wpas_p2p_disconnect(tmp);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002584 }
2585
2586 /*
2587 * Deinit GO data on any possibly remaining interface (if main
2588 * interface is used as GO).
2589 */
2590 for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
2591 if (wpa_s->ap_iface)
2592 wpas_p2p_group_deinit(wpa_s);
2593 }
2594
2595 p2p_deinit(global->p2p);
2596 global->p2p = NULL;
Dmitry Shmidt04949592012-07-19 12:16:46 -07002597 global->p2p_init_wpa_s = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002598}
2599
2600
2601static int wpas_p2p_create_iface(struct wpa_supplicant *wpa_s)
2602{
2603 if (wpa_s->drv_flags &
2604 (WPA_DRIVER_FLAGS_P2P_DEDICATED_INTERFACE |
2605 WPA_DRIVER_FLAGS_P2P_MGMT_AND_NON_P2P))
2606 return 1; /* P2P group requires a new interface in every case
2607 */
2608 if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_CONCURRENT))
2609 return 0; /* driver does not support concurrent operations */
2610 if (wpa_s->global->ifaces->next)
2611 return 1; /* more that one interface already in use */
2612 if (wpa_s->wpa_state >= WPA_AUTHENTICATING)
2613 return 1; /* this interface is already in use */
2614 return 0;
2615}
2616
2617
2618static int wpas_p2p_start_go_neg(struct wpa_supplicant *wpa_s,
2619 const u8 *peer_addr,
2620 enum p2p_wps_method wps_method,
2621 int go_intent, const u8 *own_interface_addr,
Dmitry Shmidt04949592012-07-19 12:16:46 -07002622 unsigned int force_freq, int persistent_group,
2623 struct wpa_ssid *ssid)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002624{
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002625 if (persistent_group && wpa_s->conf->persistent_reconnect)
2626 persistent_group = 2;
2627
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002628 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT) {
2629 return wpa_drv_p2p_connect(wpa_s, peer_addr, wps_method,
2630 go_intent, own_interface_addr,
2631 force_freq, persistent_group);
2632 }
2633
2634 return p2p_connect(wpa_s->global->p2p, peer_addr, wps_method,
2635 go_intent, own_interface_addr, force_freq,
Dmitry Shmidt04949592012-07-19 12:16:46 -07002636 persistent_group, ssid ? ssid->ssid : NULL,
2637 ssid ? ssid->ssid_len : 0,
2638 wpa_s->p2p_pd_before_go_neg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002639}
2640
2641
2642static int wpas_p2p_auth_go_neg(struct wpa_supplicant *wpa_s,
2643 const u8 *peer_addr,
2644 enum p2p_wps_method wps_method,
2645 int go_intent, const u8 *own_interface_addr,
Dmitry Shmidt04949592012-07-19 12:16:46 -07002646 unsigned int force_freq, int persistent_group,
2647 struct wpa_ssid *ssid)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002648{
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002649 if (persistent_group && wpa_s->conf->persistent_reconnect)
2650 persistent_group = 2;
2651
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002652 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
2653 return -1;
2654
2655 return p2p_authorize(wpa_s->global->p2p, peer_addr, wps_method,
2656 go_intent, own_interface_addr, force_freq,
Dmitry Shmidt04949592012-07-19 12:16:46 -07002657 persistent_group, ssid ? ssid->ssid : NULL,
2658 ssid ? ssid->ssid_len : 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002659}
2660
2661
2662static void wpas_p2p_check_join_scan_limit(struct wpa_supplicant *wpa_s)
2663{
2664 wpa_s->p2p_join_scan_count++;
2665 wpa_printf(MSG_DEBUG, "P2P: Join scan attempt %d",
2666 wpa_s->p2p_join_scan_count);
2667 if (wpa_s->p2p_join_scan_count > P2P_MAX_JOIN_SCAN_ATTEMPTS) {
2668 wpa_printf(MSG_DEBUG, "P2P: Failed to find GO " MACSTR
2669 " for join operationg - stop join attempt",
2670 MAC2STR(wpa_s->pending_join_iface_addr));
2671 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
Dmitry Shmidt04949592012-07-19 12:16:46 -07002672 if (wpa_s->p2p_auto_pd) {
2673 wpa_s->p2p_auto_pd = 0;
2674 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_FAILURE
2675 " p2p_dev_addr=" MACSTR " status=N/A",
2676 MAC2STR(wpa_s->pending_join_dev_addr));
2677 return;
2678 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002679 wpa_msg(wpa_s->parent, MSG_INFO,
2680 P2P_EVENT_GROUP_FORMATION_FAILURE);
2681 }
2682}
2683
2684
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08002685static void wpas_p2p_pd_before_join_timeout(void *eloop_ctx, void *timeout_ctx)
2686{
2687 struct wpa_supplicant *wpa_s = eloop_ctx;
2688 if (!wpa_s->pending_pd_before_join)
2689 return;
2690 /*
2691 * Provision Discovery Response may have been lost - try to connect
2692 * anyway since we do not need any information from this PD.
2693 */
2694 wpa_printf(MSG_DEBUG, "P2P: PD timeout for join-existing-group - "
2695 "try to connect anyway");
2696 wpas_p2p_join_start(wpa_s);
2697}
2698
2699
Dmitry Shmidt04949592012-07-19 12:16:46 -07002700static int wpas_check_freq_conflict(struct wpa_supplicant *wpa_s, int freq)
2701{
2702 struct wpa_supplicant *iface;
2703 int shared_freq;
2704 u8 bssid[ETH_ALEN];
2705
2706 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_MULTI_CHANNEL_CONCURRENT)
2707 return 0;
2708
2709 for (iface = wpa_s->global->ifaces; iface; iface = iface->next) {
2710 if (!wpas_p2p_create_iface(wpa_s) && iface == wpa_s)
2711 continue;
2712 if (iface->current_ssid == NULL || iface->assoc_freq == 0)
2713 continue;
2714 if (iface->current_ssid->mode == WPAS_MODE_AP ||
2715 iface->current_ssid->mode == WPAS_MODE_P2P_GO)
2716 shared_freq = iface->current_ssid->frequency;
2717 else if (wpa_drv_get_bssid(iface, bssid) == 0)
2718 shared_freq = iface->assoc_freq;
2719 else
2720 shared_freq = 0;
2721
2722 if (shared_freq && freq != shared_freq) {
2723 wpa_printf(MSG_DEBUG, "P2P: Frequency conflict - %s "
2724 "connected on %d MHz - new connection on "
2725 "%d MHz", iface->ifname, shared_freq, freq);
2726 return 1;
2727 }
2728 }
2729
2730 shared_freq = wpa_drv_shared_freq(wpa_s);
2731 if (shared_freq > 0 && shared_freq != freq) {
2732 wpa_printf(MSG_DEBUG, "P2P: Frequency conflict - shared "
2733 "virtual interface connected on %d MHz - new "
2734 "connection on %d MHz", shared_freq, freq);
2735 return 1;
2736 }
2737
2738 return 0;
2739}
2740
2741
2742static int wpas_p2p_peer_go(struct wpa_supplicant *wpa_s,
2743 const u8 *peer_dev_addr)
2744{
2745 struct wpa_bss *bss;
2746 int updated;
2747
2748 bss = wpa_bss_get_p2p_dev_addr(wpa_s, peer_dev_addr);
2749 if (bss == NULL)
2750 return -1;
2751 if (bss->last_update_idx < wpa_s->bss_update_idx) {
2752 wpa_printf(MSG_DEBUG, "P2P: Peer BSS entry not updated in the "
2753 "last scan");
2754 return 0;
2755 }
2756
2757 updated = os_time_before(&wpa_s->p2p_auto_started, &bss->last_update);
2758 wpa_printf(MSG_DEBUG, "P2P: Current BSS entry for peer updated at "
2759 "%ld.%06ld (%supdated in last scan)",
2760 bss->last_update.sec, bss->last_update.usec,
2761 updated ? "": "not ");
2762
2763 return updated;
2764}
2765
2766
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002767static void wpas_p2p_scan_res_join(struct wpa_supplicant *wpa_s,
2768 struct wpa_scan_results *scan_res)
2769{
2770 struct wpa_bss *bss;
2771 int freq;
2772 u8 iface_addr[ETH_ALEN];
Dmitry Shmidt04949592012-07-19 12:16:46 -07002773
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002774 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
2775
2776 if (wpa_s->global->p2p_disabled)
2777 return;
2778
Dmitry Shmidt04949592012-07-19 12:16:46 -07002779 wpa_printf(MSG_DEBUG, "P2P: Scan results received (%d BSS) for %sjoin",
2780 scan_res ? (int) scan_res->num : -1,
2781 wpa_s->p2p_auto_join ? "auto_" : "");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002782
2783 if (scan_res)
2784 wpas_p2p_scan_res_handler(wpa_s, scan_res);
2785
Dmitry Shmidt04949592012-07-19 12:16:46 -07002786 if (wpa_s->p2p_auto_pd) {
2787 int join = wpas_p2p_peer_go(wpa_s,
2788 wpa_s->pending_join_dev_addr);
2789 if (join == 0 &&
2790 wpa_s->auto_pd_scan_retry < P2P_AUTO_PD_SCAN_ATTEMPTS) {
2791 wpa_s->auto_pd_scan_retry++;
2792 bss = wpa_bss_get_bssid(wpa_s,
2793 wpa_s->pending_join_dev_addr);
2794 if (bss) {
2795 freq = bss->freq;
2796 wpa_printf(MSG_DEBUG, "P2P: Scan retry %d for "
2797 "the peer " MACSTR " at %d MHz",
2798 wpa_s->auto_pd_scan_retry,
2799 MAC2STR(wpa_s->
2800 pending_join_dev_addr),
2801 freq);
2802 wpas_p2p_join_scan_req(wpa_s, freq);
2803 return;
2804 }
2805 }
2806
2807 if (join < 0)
2808 join = 0;
2809
2810 wpa_s->p2p_auto_pd = 0;
2811 wpa_s->pending_pd_use = join ? AUTO_PD_JOIN : AUTO_PD_GO_NEG;
2812 wpa_printf(MSG_DEBUG, "P2P: Auto PD with " MACSTR " join=%d",
2813 MAC2STR(wpa_s->pending_join_dev_addr), join);
2814 if (p2p_prov_disc_req(wpa_s->global->p2p,
2815 wpa_s->pending_join_dev_addr,
2816 wpa_s->pending_pd_config_methods, join,
2817 0) < 0) {
2818 wpa_s->p2p_auto_pd = 0;
2819 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_FAILURE
2820 " p2p_dev_addr=" MACSTR " status=N/A",
2821 MAC2STR(wpa_s->pending_join_dev_addr));
2822 }
2823 return;
2824 }
2825
2826 if (wpa_s->p2p_auto_join) {
2827 int join = wpas_p2p_peer_go(wpa_s,
2828 wpa_s->pending_join_dev_addr);
2829 if (join < 0) {
2830 wpa_printf(MSG_DEBUG, "P2P: Peer was not found to be "
2831 "running a GO -> use GO Negotiation");
2832 wpas_p2p_connect(wpa_s, wpa_s->pending_join_dev_addr,
2833 wpa_s->p2p_pin, wpa_s->p2p_wps_method,
2834 wpa_s->p2p_persistent_group, 0, 0, 0,
2835 wpa_s->p2p_go_intent,
2836 wpa_s->p2p_connect_freq,
2837 wpa_s->p2p_persistent_id,
2838 wpa_s->p2p_pd_before_go_neg);
2839 return;
2840 }
2841
2842 wpa_printf(MSG_DEBUG, "P2P: Peer was found running GO%s -> "
2843 "try to join the group", join ? "" :
2844 " in older scan");
2845 if (!join)
2846 wpa_s->p2p_fallback_to_go_neg = 1;
2847 }
2848
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002849 freq = p2p_get_oper_freq(wpa_s->global->p2p,
2850 wpa_s->pending_join_iface_addr);
2851 if (freq < 0 &&
2852 p2p_get_interface_addr(wpa_s->global->p2p,
2853 wpa_s->pending_join_dev_addr,
2854 iface_addr) == 0 &&
2855 os_memcmp(iface_addr, wpa_s->pending_join_dev_addr, ETH_ALEN) != 0)
2856 {
2857 wpa_printf(MSG_DEBUG, "P2P: Overwrite pending interface "
2858 "address for join from " MACSTR " to " MACSTR
2859 " based on newly discovered P2P peer entry",
2860 MAC2STR(wpa_s->pending_join_iface_addr),
2861 MAC2STR(iface_addr));
2862 os_memcpy(wpa_s->pending_join_iface_addr, iface_addr,
2863 ETH_ALEN);
2864
2865 freq = p2p_get_oper_freq(wpa_s->global->p2p,
2866 wpa_s->pending_join_iface_addr);
2867 }
2868 if (freq >= 0) {
2869 wpa_printf(MSG_DEBUG, "P2P: Target GO operating frequency "
2870 "from P2P peer table: %d MHz", freq);
2871 }
2872 bss = wpa_bss_get_bssid(wpa_s, wpa_s->pending_join_iface_addr);
2873 if (bss) {
2874 freq = bss->freq;
2875 wpa_printf(MSG_DEBUG, "P2P: Target GO operating frequency "
2876 "from BSS table: %d MHz", freq);
2877 }
2878 if (freq > 0) {
2879 u16 method;
2880
Dmitry Shmidt04949592012-07-19 12:16:46 -07002881 if (wpas_check_freq_conflict(wpa_s, freq) > 0) {
2882 wpa_msg(wpa_s->parent, MSG_INFO,
2883 P2P_EVENT_GROUP_FORMATION_FAILURE
2884 "reason=FREQ_CONFLICT");
2885 return;
2886 }
2887
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002888 wpa_printf(MSG_DEBUG, "P2P: Send Provision Discovery Request "
2889 "prior to joining an existing group (GO " MACSTR
2890 " freq=%u MHz)",
2891 MAC2STR(wpa_s->pending_join_dev_addr), freq);
2892 wpa_s->pending_pd_before_join = 1;
2893
2894 switch (wpa_s->pending_join_wps_method) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002895 case WPS_PIN_DISPLAY:
2896 method = WPS_CONFIG_KEYPAD;
2897 break;
2898 case WPS_PIN_KEYPAD:
2899 method = WPS_CONFIG_DISPLAY;
2900 break;
2901 case WPS_PBC:
2902 method = WPS_CONFIG_PUSHBUTTON;
2903 break;
2904 default:
2905 method = 0;
2906 break;
2907 }
2908
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002909 if ((p2p_get_provisioning_info(wpa_s->global->p2p,
2910 wpa_s->pending_join_dev_addr) ==
2911 method)) {
2912 /*
2913 * We have already performed provision discovery for
2914 * joining the group. Proceed directly to join
2915 * operation without duplicated provision discovery. */
2916 wpa_printf(MSG_DEBUG, "P2P: Provision discovery "
2917 "with " MACSTR " already done - proceed to "
2918 "join",
2919 MAC2STR(wpa_s->pending_join_dev_addr));
2920 wpa_s->pending_pd_before_join = 0;
2921 goto start;
2922 }
2923
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002924 if (p2p_prov_disc_req(wpa_s->global->p2p,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002925 wpa_s->pending_join_dev_addr, method, 1,
2926 freq) < 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002927 wpa_printf(MSG_DEBUG, "P2P: Failed to send Provision "
2928 "Discovery Request before joining an "
2929 "existing group");
2930 wpa_s->pending_pd_before_join = 0;
2931 goto start;
2932 }
2933
2934 /*
2935 * Actual join operation will be started from the Action frame
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08002936 * TX status callback (if no ACK is received) or when the
2937 * Provision Discovery Response is received. Use a short
2938 * timeout as a backup mechanism should the Provision Discovery
2939 * Response be lost for any reason.
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002940 */
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08002941 eloop_cancel_timeout(wpas_p2p_pd_before_join_timeout, wpa_s,
2942 NULL);
2943 eloop_register_timeout(2, 0, wpas_p2p_pd_before_join_timeout,
2944 wpa_s, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002945 return;
2946 }
2947
2948 wpa_printf(MSG_DEBUG, "P2P: Failed to find BSS/GO - try again later");
2949 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
2950 eloop_register_timeout(1, 0, wpas_p2p_join_scan, wpa_s, NULL);
2951 wpas_p2p_check_join_scan_limit(wpa_s);
2952 return;
2953
2954start:
2955 /* Start join operation immediately */
2956 wpas_p2p_join_start(wpa_s);
2957}
2958
2959
Dmitry Shmidt04949592012-07-19 12:16:46 -07002960static void wpas_p2p_join_scan_req(struct wpa_supplicant *wpa_s, int freq)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002961{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002962 int ret;
2963 struct wpa_driver_scan_params params;
2964 struct wpabuf *wps_ie, *ies;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002965 size_t ielen;
Dmitry Shmidt04949592012-07-19 12:16:46 -07002966 int freqs[2] = { 0, 0 };
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002967
2968 os_memset(&params, 0, sizeof(params));
2969
2970 /* P2P Wildcard SSID */
2971 params.num_ssids = 1;
2972 params.ssids[0].ssid = (u8 *) P2P_WILDCARD_SSID;
2973 params.ssids[0].ssid_len = P2P_WILDCARD_SSID_LEN;
2974
2975 wpa_s->wps->dev.p2p = 1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07002976 wps_ie = wps_build_probe_req_ie(DEV_PW_DEFAULT, &wpa_s->wps->dev,
2977 wpa_s->wps->uuid, WPS_REQ_ENROLLEE, 0,
2978 NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002979 if (wps_ie == NULL) {
2980 wpas_p2p_scan_res_join(wpa_s, NULL);
2981 return;
2982 }
2983
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002984 ielen = p2p_scan_ie_buf_len(wpa_s->global->p2p);
2985 ies = wpabuf_alloc(wpabuf_len(wps_ie) + ielen);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002986 if (ies == NULL) {
2987 wpabuf_free(wps_ie);
2988 wpas_p2p_scan_res_join(wpa_s, NULL);
2989 return;
2990 }
2991 wpabuf_put_buf(ies, wps_ie);
2992 wpabuf_free(wps_ie);
2993
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08002994 p2p_scan_ie(wpa_s->global->p2p, ies, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002995
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002996 params.p2p_probe = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002997 params.extra_ies = wpabuf_head(ies);
2998 params.extra_ies_len = wpabuf_len(ies);
Dmitry Shmidt04949592012-07-19 12:16:46 -07002999 if (freq > 0) {
3000 freqs[0] = freq;
3001 params.freqs = freqs;
3002 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003003
3004 /*
3005 * Run a scan to update BSS table and start Provision Discovery once
3006 * the new scan results become available.
3007 */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003008 ret = wpa_drv_scan(wpa_s, &params);
Dmitry Shmidt04949592012-07-19 12:16:46 -07003009 if (!ret)
3010 wpa_s->scan_res_handler = wpas_p2p_scan_res_join;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003011
3012 wpabuf_free(ies);
3013
3014 if (ret) {
3015 wpa_printf(MSG_DEBUG, "P2P: Failed to start scan for join - "
3016 "try again later");
3017 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
3018 eloop_register_timeout(1, 0, wpas_p2p_join_scan, wpa_s, NULL);
3019 wpas_p2p_check_join_scan_limit(wpa_s);
3020 }
3021}
3022
3023
Dmitry Shmidt04949592012-07-19 12:16:46 -07003024static void wpas_p2p_join_scan(void *eloop_ctx, void *timeout_ctx)
3025{
3026 struct wpa_supplicant *wpa_s = eloop_ctx;
3027 wpas_p2p_join_scan_req(wpa_s, 0);
3028}
3029
3030
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003031static int wpas_p2p_join(struct wpa_supplicant *wpa_s, const u8 *iface_addr,
Dmitry Shmidt04949592012-07-19 12:16:46 -07003032 const u8 *dev_addr, enum p2p_wps_method wps_method,
3033 int auto_join)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003034{
3035 wpa_printf(MSG_DEBUG, "P2P: Request to join existing group (iface "
Dmitry Shmidt04949592012-07-19 12:16:46 -07003036 MACSTR " dev " MACSTR ")%s",
3037 MAC2STR(iface_addr), MAC2STR(dev_addr),
3038 auto_join ? " (auto_join)" : "");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003039
Dmitry Shmidt04949592012-07-19 12:16:46 -07003040 wpa_s->p2p_auto_pd = 0;
3041 wpa_s->p2p_auto_join = !!auto_join;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003042 os_memcpy(wpa_s->pending_join_iface_addr, iface_addr, ETH_ALEN);
3043 os_memcpy(wpa_s->pending_join_dev_addr, dev_addr, ETH_ALEN);
3044 wpa_s->pending_join_wps_method = wps_method;
3045
3046 /* Make sure we are not running find during connection establishment */
3047 wpas_p2p_stop_find(wpa_s);
3048
3049 wpa_s->p2p_join_scan_count = 0;
3050 wpas_p2p_join_scan(wpa_s, NULL);
3051 return 0;
3052}
3053
3054
3055static int wpas_p2p_join_start(struct wpa_supplicant *wpa_s)
3056{
3057 struct wpa_supplicant *group;
3058 struct p2p_go_neg_results res;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003059 struct wpa_bss *bss;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003060
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08003061 eloop_cancel_timeout(wpas_p2p_pd_before_join_timeout, wpa_s, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003062 group = wpas_p2p_get_group_iface(wpa_s, 0, 0);
3063 if (group == NULL)
3064 return -1;
3065 if (group != wpa_s) {
3066 os_memcpy(group->p2p_pin, wpa_s->p2p_pin,
3067 sizeof(group->p2p_pin));
3068 group->p2p_wps_method = wpa_s->p2p_wps_method;
3069 }
3070
3071 group->p2p_in_provisioning = 1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003072 group->p2p_fallback_to_go_neg = wpa_s->p2p_fallback_to_go_neg;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003073
3074 os_memset(&res, 0, sizeof(res));
3075 os_memcpy(res.peer_interface_addr, wpa_s->pending_join_iface_addr,
3076 ETH_ALEN);
3077 res.wps_method = wpa_s->pending_join_wps_method;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003078 bss = wpa_bss_get_bssid(wpa_s, wpa_s->pending_join_iface_addr);
3079 if (bss) {
3080 res.freq = bss->freq;
3081 res.ssid_len = bss->ssid_len;
3082 os_memcpy(res.ssid, bss->ssid, bss->ssid_len);
3083 }
3084
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08003085 if (wpa_s->off_channel_freq || wpa_s->roc_waiting_drv_freq) {
3086 wpa_printf(MSG_DEBUG, "P2P: Cancel remain-on-channel prior to "
3087 "starting client");
3088 wpa_drv_cancel_remain_on_channel(wpa_s);
3089 wpa_s->off_channel_freq = 0;
3090 wpa_s->roc_waiting_drv_freq = 0;
3091 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003092 wpas_start_wps_enrollee(group, &res);
3093
3094 /*
3095 * Allow a longer timeout for join-a-running-group than normal 15
3096 * second group formation timeout since the GO may not have authorized
3097 * our connection yet.
3098 */
3099 eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s, NULL);
3100 eloop_register_timeout(60, 0, wpas_p2p_group_formation_timeout,
3101 wpa_s, NULL);
3102
3103 return 0;
3104}
3105
3106
3107/**
3108 * wpas_p2p_connect - Request P2P Group Formation to be started
3109 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
3110 * @peer_addr: Address of the peer P2P Device
3111 * @pin: PIN to use during provisioning or %NULL to indicate PBC mode
3112 * @persistent_group: Whether to create a persistent group
Dmitry Shmidt04949592012-07-19 12:16:46 -07003113 * @auto_join: Whether to select join vs. GO Negotiation automatically
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003114 * @join: Whether to join an existing group (as a client) instead of starting
3115 * Group Owner negotiation; @peer_addr is BSSID in that case
3116 * @auth: Whether to only authorize the connection instead of doing that and
3117 * initiating Group Owner negotiation
3118 * @go_intent: GO Intent or -1 to use default
3119 * @freq: Frequency for the group or 0 for auto-selection
Dmitry Shmidt04949592012-07-19 12:16:46 -07003120 * @persistent_id: Persistent group credentials to use for forcing GO
3121 * parameters or -1 to generate new values (SSID/passphrase)
3122 * @pd: Whether to send Provision Discovery prior to GO Negotiation as an
3123 * interoperability workaround when initiating group formation
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003124 * Returns: 0 or new PIN (if pin was %NULL) on success, -1 on unspecified
3125 * failure, -2 on failure due to channel not currently available,
3126 * -3 if forced channel is not supported
3127 */
3128int wpas_p2p_connect(struct wpa_supplicant *wpa_s, const u8 *peer_addr,
3129 const char *pin, enum p2p_wps_method wps_method,
Dmitry Shmidt04949592012-07-19 12:16:46 -07003130 int persistent_group, int auto_join, int join, int auth,
3131 int go_intent, int freq, int persistent_id, int pd)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003132{
3133 int force_freq = 0, oper_freq = 0;
3134 u8 bssid[ETH_ALEN];
3135 int ret = 0;
3136 enum wpa_driver_if_type iftype;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003137 const u8 *if_addr;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003138 struct wpa_ssid *ssid = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003139
3140 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
3141 return -1;
3142
Dmitry Shmidt04949592012-07-19 12:16:46 -07003143 if (persistent_id >= 0) {
3144 ssid = wpa_config_get_network(wpa_s->conf, persistent_id);
3145 if (ssid == NULL || ssid->disabled != 2 ||
3146 ssid->mode != WPAS_MODE_P2P_GO)
3147 return -1;
3148 }
3149
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003150 if (go_intent < 0)
3151 go_intent = wpa_s->conf->p2p_go_intent;
3152
3153 if (!auth)
3154 wpa_s->p2p_long_listen = 0;
3155
3156 wpa_s->p2p_wps_method = wps_method;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003157 wpa_s->p2p_persistent_group = !!persistent_group;
3158 wpa_s->p2p_persistent_id = persistent_id;
3159 wpa_s->p2p_go_intent = go_intent;
3160 wpa_s->p2p_connect_freq = freq;
3161 wpa_s->p2p_fallback_to_go_neg = 0;
3162 wpa_s->p2p_pd_before_go_neg = !!pd;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003163
3164 if (pin)
3165 os_strlcpy(wpa_s->p2p_pin, pin, sizeof(wpa_s->p2p_pin));
3166 else if (wps_method == WPS_PIN_DISPLAY) {
3167 ret = wps_generate_pin();
3168 os_snprintf(wpa_s->p2p_pin, sizeof(wpa_s->p2p_pin), "%08d",
3169 ret);
3170 wpa_printf(MSG_DEBUG, "P2P: Randomly generated PIN: %s",
3171 wpa_s->p2p_pin);
3172 } else
3173 wpa_s->p2p_pin[0] = '\0';
3174
Dmitry Shmidt04949592012-07-19 12:16:46 -07003175 if (join || auto_join) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003176 u8 iface_addr[ETH_ALEN], dev_addr[ETH_ALEN];
3177 if (auth) {
3178 wpa_printf(MSG_DEBUG, "P2P: Authorize invitation to "
3179 "connect a running group from " MACSTR,
3180 MAC2STR(peer_addr));
3181 os_memcpy(wpa_s->p2p_auth_invite, peer_addr, ETH_ALEN);
3182 return ret;
3183 }
3184 os_memcpy(dev_addr, peer_addr, ETH_ALEN);
3185 if (p2p_get_interface_addr(wpa_s->global->p2p, peer_addr,
3186 iface_addr) < 0) {
3187 os_memcpy(iface_addr, peer_addr, ETH_ALEN);
3188 p2p_get_dev_addr(wpa_s->global->p2p, peer_addr,
3189 dev_addr);
3190 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07003191 if (auto_join) {
3192 os_get_time(&wpa_s->p2p_auto_started);
3193 wpa_printf(MSG_DEBUG, "P2P: Auto join started at "
3194 "%ld.%06ld",
3195 wpa_s->p2p_auto_started.sec,
3196 wpa_s->p2p_auto_started.usec);
3197 }
3198 if (wpas_p2p_join(wpa_s, iface_addr, dev_addr, wps_method,
3199 auto_join) < 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003200 return -1;
3201 return ret;
3202 }
3203
3204 if (wpa_s->current_ssid && wpa_drv_get_bssid(wpa_s, bssid) == 0 &&
3205 wpa_s->assoc_freq)
3206 oper_freq = wpa_s->assoc_freq;
3207 else {
3208 oper_freq = wpa_drv_shared_freq(wpa_s);
3209 if (oper_freq < 0)
3210 oper_freq = 0;
3211 }
3212
3213 if (freq > 0) {
3214 if (!p2p_supported_freq(wpa_s->global->p2p, freq)) {
3215 wpa_printf(MSG_DEBUG, "P2P: The forced channel "
3216 "(%u MHz) is not supported for P2P uses",
3217 freq);
3218 return -3;
3219 }
3220
3221 if (oper_freq > 0 && freq != oper_freq &&
3222 !(wpa_s->drv_flags &
3223 WPA_DRIVER_FLAGS_MULTI_CHANNEL_CONCURRENT)) {
3224 wpa_printf(MSG_DEBUG, "P2P: Cannot start P2P group "
3225 "on %u MHz while connected on another "
3226 "channel (%u MHz)", freq, oper_freq);
3227 return -2;
3228 }
3229 wpa_printf(MSG_DEBUG, "P2P: Trying to force us to use the "
3230 "requested channel (%u MHz)", freq);
3231 force_freq = freq;
3232 } else if (oper_freq > 0 &&
3233 !p2p_supported_freq(wpa_s->global->p2p, oper_freq)) {
3234 if (!(wpa_s->drv_flags &
3235 WPA_DRIVER_FLAGS_MULTI_CHANNEL_CONCURRENT)) {
3236 wpa_printf(MSG_DEBUG, "P2P: Cannot start P2P group "
3237 "while connected on non-P2P supported "
3238 "channel (%u MHz)", oper_freq);
3239 return -2;
3240 }
3241 wpa_printf(MSG_DEBUG, "P2P: Current operating channel "
3242 "(%u MHz) not available for P2P - try to use "
3243 "another channel", oper_freq);
3244 force_freq = 0;
3245 } else if (oper_freq > 0) {
3246 wpa_printf(MSG_DEBUG, "P2P: Trying to force us to use the "
3247 "channel we are already using (%u MHz) on another "
3248 "interface", oper_freq);
3249 force_freq = oper_freq;
3250 }
3251
3252 wpa_s->create_p2p_iface = wpas_p2p_create_iface(wpa_s);
3253
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003254 if (wpa_s->create_p2p_iface) {
3255 /* Prepare to add a new interface for the group */
3256 iftype = WPA_IF_P2P_GROUP;
3257 if (go_intent == 15)
3258 iftype = WPA_IF_P2P_GO;
3259 if (wpas_p2p_add_group_interface(wpa_s, iftype) < 0) {
3260 wpa_printf(MSG_ERROR, "P2P: Failed to allocate a new "
3261 "interface for the group");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003262 return -1;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003263 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003264
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003265 if_addr = wpa_s->pending_interface_addr;
3266 } else
3267 if_addr = wpa_s->own_addr;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003268
3269 if (auth) {
3270 if (wpas_p2p_auth_go_neg(wpa_s, peer_addr, wps_method,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003271 go_intent, if_addr,
Dmitry Shmidt04949592012-07-19 12:16:46 -07003272 force_freq, persistent_group, ssid) <
3273 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003274 return -1;
3275 return ret;
3276 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003277
3278 if (wpas_p2p_start_go_neg(wpa_s, peer_addr, wps_method,
3279 go_intent, if_addr, force_freq,
Dmitry Shmidt04949592012-07-19 12:16:46 -07003280 persistent_group, ssid) < 0) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003281 if (wpa_s->create_p2p_iface)
3282 wpas_p2p_remove_pending_group_interface(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003283 return -1;
3284 }
3285 return ret;
3286}
3287
3288
3289/**
3290 * wpas_p2p_remain_on_channel_cb - Indication of remain-on-channel start
3291 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
3292 * @freq: Frequency of the channel in MHz
3293 * @duration: Duration of the stay on the channel in milliseconds
3294 *
3295 * This callback is called when the driver indicates that it has started the
3296 * requested remain-on-channel duration.
3297 */
3298void wpas_p2p_remain_on_channel_cb(struct wpa_supplicant *wpa_s,
3299 unsigned int freq, unsigned int duration)
3300{
3301 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
3302 return;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003303 if (wpa_s->off_channel_freq == wpa_s->pending_listen_freq) {
3304 p2p_listen_cb(wpa_s->global->p2p, wpa_s->pending_listen_freq,
3305 wpa_s->pending_listen_duration);
3306 wpa_s->pending_listen_freq = 0;
3307 }
3308}
3309
3310
3311static int wpas_p2p_listen_start(struct wpa_supplicant *wpa_s,
3312 unsigned int timeout)
3313{
3314 /* Limit maximum Listen state time based on driver limitation. */
3315 if (timeout > wpa_s->max_remain_on_chan)
3316 timeout = wpa_s->max_remain_on_chan;
3317
3318 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
3319 return wpa_drv_p2p_listen(wpa_s, timeout);
3320
3321 return p2p_listen(wpa_s->global->p2p, timeout);
3322}
3323
3324
3325/**
3326 * wpas_p2p_cancel_remain_on_channel_cb - Remain-on-channel timeout
3327 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
3328 * @freq: Frequency of the channel in MHz
3329 *
3330 * This callback is called when the driver indicates that a remain-on-channel
3331 * operation has been completed, i.e., the duration on the requested channel
3332 * has timed out.
3333 */
3334void wpas_p2p_cancel_remain_on_channel_cb(struct wpa_supplicant *wpa_s,
3335 unsigned int freq)
3336{
3337 wpa_printf(MSG_DEBUG, "P2P: Cancel remain-on-channel callback "
3338 "(p2p_long_listen=%d ms pending_action_tx=%p)",
3339 wpa_s->p2p_long_listen, wpa_s->pending_action_tx);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003340 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
3341 return;
3342 if (p2p_listen_end(wpa_s->global->p2p, freq) > 0)
3343 return; /* P2P module started a new operation */
3344 if (wpa_s->pending_action_tx)
3345 return;
3346 if (wpa_s->p2p_long_listen > 0)
3347 wpa_s->p2p_long_listen -= wpa_s->max_remain_on_chan;
3348 if (wpa_s->p2p_long_listen > 0) {
3349 wpa_printf(MSG_DEBUG, "P2P: Continuing long Listen state");
3350 wpas_p2p_listen_start(wpa_s, wpa_s->p2p_long_listen);
3351 }
3352}
3353
3354
3355/**
3356 * wpas_p2p_group_remove - Remove a P2P group
3357 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
3358 * @ifname: Network interface name of the group interface or "*" to remove all
3359 * groups
3360 * Returns: 0 on success, -1 on failure
3361 *
3362 * This function is used to remove a P2P group. This can be used to disconnect
3363 * from a group in which the local end is a P2P Client or to end a P2P Group in
3364 * case the local end is the Group Owner. If a virtual network interface was
3365 * created for this group, that interface will be removed. Otherwise, only the
3366 * configured P2P group network will be removed from the interface.
3367 */
3368int wpas_p2p_group_remove(struct wpa_supplicant *wpa_s, const char *ifname)
3369{
3370 struct wpa_global *global = wpa_s->global;
3371
3372 if (os_strcmp(ifname, "*") == 0) {
3373 struct wpa_supplicant *prev;
3374 wpa_s = global->ifaces;
3375 while (wpa_s) {
3376 prev = wpa_s;
3377 wpa_s = wpa_s->next;
3378 wpas_p2p_disconnect(prev);
3379 }
3380 return 0;
3381 }
3382
3383 for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
3384 if (os_strcmp(wpa_s->ifname, ifname) == 0)
3385 break;
3386 }
3387
3388 return wpas_p2p_disconnect(wpa_s);
3389}
3390
3391
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003392static int wpas_p2p_init_go_params(struct wpa_supplicant *wpa_s,
3393 struct p2p_go_neg_results *params,
3394 int freq)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003395{
3396 u8 bssid[ETH_ALEN];
3397 int res;
3398
3399 os_memset(params, 0, sizeof(*params));
3400 params->role_go = 1;
3401 if (freq) {
3402 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on forced "
3403 "frequency %d MHz", freq);
3404 params->freq = freq;
3405 } else if (wpa_s->conf->p2p_oper_reg_class == 81 &&
3406 wpa_s->conf->p2p_oper_channel >= 1 &&
3407 wpa_s->conf->p2p_oper_channel <= 11) {
3408 params->freq = 2407 + 5 * wpa_s->conf->p2p_oper_channel;
3409 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on configured "
3410 "frequency %d MHz", params->freq);
3411 } else if (wpa_s->conf->p2p_oper_reg_class == 115 ||
Jouni Malinen87fd2792011-05-16 18:35:42 +03003412 wpa_s->conf->p2p_oper_reg_class == 124) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003413 params->freq = 5000 + 5 * wpa_s->conf->p2p_oper_channel;
3414 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on configured "
3415 "frequency %d MHz", params->freq);
3416 } else if (wpa_s->conf->p2p_oper_channel == 0 &&
3417 wpa_s->best_overall_freq > 0 &&
3418 p2p_supported_freq(wpa_s->global->p2p,
3419 wpa_s->best_overall_freq)) {
3420 params->freq = wpa_s->best_overall_freq;
3421 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on best overall "
3422 "channel %d MHz", params->freq);
3423 } else if (wpa_s->conf->p2p_oper_channel == 0 &&
3424 wpa_s->best_24_freq > 0 &&
3425 p2p_supported_freq(wpa_s->global->p2p,
3426 wpa_s->best_24_freq)) {
3427 params->freq = wpa_s->best_24_freq;
3428 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on best 2.4 GHz "
3429 "channel %d MHz", params->freq);
3430 } else if (wpa_s->conf->p2p_oper_channel == 0 &&
3431 wpa_s->best_5_freq > 0 &&
3432 p2p_supported_freq(wpa_s->global->p2p,
3433 wpa_s->best_5_freq)) {
3434 params->freq = wpa_s->best_5_freq;
3435 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on best 5 GHz "
3436 "channel %d MHz", params->freq);
3437 } else {
Dmitry Shmidt04949592012-07-19 12:16:46 -07003438 int chan;
3439 for (chan = 0; chan < 11; chan++) {
3440 params->freq = 2412 + chan * 5;
3441 if (!wpas_p2p_disallowed_freq(wpa_s->global,
3442 params->freq))
3443 break;
3444 }
3445 if (chan == 11) {
3446 wpa_printf(MSG_DEBUG, "P2P: No 2.4 GHz channel "
3447 "allowed");
3448 return -1;
3449 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003450 wpa_printf(MSG_DEBUG, "P2P: Set GO freq %d MHz (no preference "
3451 "known)", params->freq);
3452 }
3453
3454 if (wpa_s->current_ssid && wpa_drv_get_bssid(wpa_s, bssid) == 0 &&
3455 wpa_s->assoc_freq && !freq) {
3456 wpa_printf(MSG_DEBUG, "P2P: Force GO on the channel we are "
3457 "already using");
3458 params->freq = wpa_s->assoc_freq;
3459 }
3460
3461 res = wpa_drv_shared_freq(wpa_s);
3462 if (res > 0 && !freq) {
3463 wpa_printf(MSG_DEBUG, "P2P: Force GO on the channel we are "
3464 "already using on a shared interface");
3465 params->freq = res;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003466 } else if (res > 0 && freq != res &&
3467 !(wpa_s->drv_flags &
3468 WPA_DRIVER_FLAGS_MULTI_CHANNEL_CONCURRENT)) {
3469 wpa_printf(MSG_DEBUG, "P2P: Cannot start P2P group on %u MHz "
3470 "while connected on another channel (%u MHz)",
3471 freq, res);
3472 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003473 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003474
3475 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003476}
3477
3478
3479static struct wpa_supplicant *
3480wpas_p2p_get_group_iface(struct wpa_supplicant *wpa_s, int addr_allocated,
3481 int go)
3482{
3483 struct wpa_supplicant *group_wpa_s;
3484
3485 if (!wpas_p2p_create_iface(wpa_s))
3486 return wpa_s;
3487
3488 if (wpas_p2p_add_group_interface(wpa_s, go ? WPA_IF_P2P_GO :
3489 WPA_IF_P2P_CLIENT) < 0)
3490 return NULL;
3491 group_wpa_s = wpas_p2p_init_group_interface(wpa_s, go);
3492 if (group_wpa_s == NULL) {
3493 wpas_p2p_remove_pending_group_interface(wpa_s);
3494 return NULL;
3495 }
3496
3497 return group_wpa_s;
3498}
3499
3500
3501/**
3502 * wpas_p2p_group_add - Add a new P2P group with local end as Group Owner
3503 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
3504 * @persistent_group: Whether to create a persistent group
3505 * @freq: Frequency for the group or 0 to indicate no hardcoding
3506 * Returns: 0 on success, -1 on failure
3507 *
3508 * This function creates a new P2P group with the local end as the Group Owner,
3509 * i.e., without using Group Owner Negotiation.
3510 */
3511int wpas_p2p_group_add(struct wpa_supplicant *wpa_s, int persistent_group,
3512 int freq)
3513{
3514 struct p2p_go_neg_results params;
3515 unsigned int r;
3516
3517 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
3518 return -1;
3519
Dmitry Shmidtdca39792011-09-06 11:17:33 -07003520 /* Make sure we are not running find during connection establishment */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003521 wpa_printf(MSG_DEBUG, "P2P: Stop any on-going P2P FIND");
Dmitry Shmidtdca39792011-09-06 11:17:33 -07003522 wpas_p2p_stop_find(wpa_s);
Dmitry Shmidtdca39792011-09-06 11:17:33 -07003523
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003524 if (freq == 2) {
3525 wpa_printf(MSG_DEBUG, "P2P: Request to start GO on 2.4 GHz "
3526 "band");
3527 if (wpa_s->best_24_freq > 0 &&
3528 p2p_supported_freq(wpa_s->global->p2p,
3529 wpa_s->best_24_freq)) {
3530 freq = wpa_s->best_24_freq;
3531 wpa_printf(MSG_DEBUG, "P2P: Use best 2.4 GHz band "
3532 "channel: %d MHz", freq);
3533 } else {
3534 os_get_random((u8 *) &r, sizeof(r));
3535 freq = 2412 + (r % 3) * 25;
3536 wpa_printf(MSG_DEBUG, "P2P: Use random 2.4 GHz band "
3537 "channel: %d MHz", freq);
3538 }
3539 }
3540
3541 if (freq == 5) {
3542 wpa_printf(MSG_DEBUG, "P2P: Request to start GO on 5 GHz "
3543 "band");
3544 if (wpa_s->best_5_freq > 0 &&
3545 p2p_supported_freq(wpa_s->global->p2p,
3546 wpa_s->best_5_freq)) {
3547 freq = wpa_s->best_5_freq;
3548 wpa_printf(MSG_DEBUG, "P2P: Use best 5 GHz band "
3549 "channel: %d MHz", freq);
3550 } else {
3551 os_get_random((u8 *) &r, sizeof(r));
3552 freq = 5180 + (r % 4) * 20;
3553 if (!p2p_supported_freq(wpa_s->global->p2p, freq)) {
3554 wpa_printf(MSG_DEBUG, "P2P: Could not select "
3555 "5 GHz channel for P2P group");
3556 return -1;
3557 }
3558 wpa_printf(MSG_DEBUG, "P2P: Use random 5 GHz band "
3559 "channel: %d MHz", freq);
3560 }
3561 }
3562
3563 if (freq > 0 && !p2p_supported_freq(wpa_s->global->p2p, freq)) {
3564 wpa_printf(MSG_DEBUG, "P2P: The forced channel for GO "
3565 "(%u MHz) is not supported for P2P uses",
3566 freq);
3567 return -1;
3568 }
3569
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003570 if (wpas_p2p_init_go_params(wpa_s, &params, freq))
3571 return -1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003572 if (params.freq &&
3573 !p2p_supported_freq(wpa_s->global->p2p, params.freq)) {
3574 wpa_printf(MSG_DEBUG, "P2P: The selected channel for GO "
3575 "(%u MHz) is not supported for P2P uses",
3576 params.freq);
3577 return -1;
3578 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003579 p2p_go_params(wpa_s->global->p2p, &params);
3580 params.persistent_group = persistent_group;
3581
3582 wpa_s = wpas_p2p_get_group_iface(wpa_s, 0, 1);
3583 if (wpa_s == NULL)
3584 return -1;
3585 wpas_start_wps_go(wpa_s, &params, 0);
3586
3587 return 0;
3588}
3589
3590
3591static int wpas_start_p2p_client(struct wpa_supplicant *wpa_s,
3592 struct wpa_ssid *params, int addr_allocated)
3593{
3594 struct wpa_ssid *ssid;
3595
3596 wpa_s = wpas_p2p_get_group_iface(wpa_s, addr_allocated, 0);
3597 if (wpa_s == NULL)
3598 return -1;
3599
3600 wpa_supplicant_ap_deinit(wpa_s);
3601
3602 ssid = wpa_config_add_network(wpa_s->conf);
3603 if (ssid == NULL)
3604 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003605 wpa_config_set_network_defaults(ssid);
3606 ssid->temporary = 1;
3607 ssid->proto = WPA_PROTO_RSN;
3608 ssid->pairwise_cipher = WPA_CIPHER_CCMP;
3609 ssid->group_cipher = WPA_CIPHER_CCMP;
3610 ssid->key_mgmt = WPA_KEY_MGMT_PSK;
3611 ssid->ssid = os_malloc(params->ssid_len);
3612 if (ssid->ssid == NULL) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003613 wpa_config_remove_network(wpa_s->conf, ssid->id);
3614 return -1;
3615 }
3616 os_memcpy(ssid->ssid, params->ssid, params->ssid_len);
3617 ssid->ssid_len = params->ssid_len;
3618 ssid->p2p_group = 1;
3619 ssid->export_keys = 1;
3620 if (params->psk_set) {
3621 os_memcpy(ssid->psk, params->psk, 32);
3622 ssid->psk_set = 1;
3623 }
3624 if (params->passphrase)
3625 ssid->passphrase = os_strdup(params->passphrase);
3626
3627 wpa_supplicant_select_network(wpa_s, ssid);
3628
3629 wpa_s->show_group_started = 1;
3630
3631 return 0;
3632}
3633
3634
3635int wpas_p2p_group_add_persistent(struct wpa_supplicant *wpa_s,
3636 struct wpa_ssid *ssid, int addr_allocated,
3637 int freq)
3638{
3639 struct p2p_go_neg_results params;
3640 int go = 0;
3641
3642 if (ssid->disabled != 2 || ssid->ssid == NULL)
3643 return -1;
3644
3645 if (wpas_get_p2p_group(wpa_s, ssid->ssid, ssid->ssid_len, &go) &&
3646 go == (ssid->mode == WPAS_MODE_P2P_GO)) {
3647 wpa_printf(MSG_DEBUG, "P2P: Requested persistent group is "
3648 "already running");
3649 return 0;
3650 }
3651
3652 /* Make sure we are not running find during connection establishment */
3653 wpas_p2p_stop_find(wpa_s);
3654
Dmitry Shmidt04949592012-07-19 12:16:46 -07003655 wpa_s->p2p_fallback_to_go_neg = 0;
3656
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003657 if (ssid->mode == WPAS_MODE_INFRA)
3658 return wpas_start_p2p_client(wpa_s, ssid, addr_allocated);
3659
3660 if (ssid->mode != WPAS_MODE_P2P_GO)
3661 return -1;
3662
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003663 if (wpas_p2p_init_go_params(wpa_s, &params, freq))
3664 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003665
3666 params.role_go = 1;
3667 if (ssid->passphrase == NULL ||
3668 os_strlen(ssid->passphrase) >= sizeof(params.passphrase)) {
3669 wpa_printf(MSG_DEBUG, "P2P: Invalid passphrase in persistent "
3670 "group");
3671 return -1;
3672 }
3673 os_strlcpy(params.passphrase, ssid->passphrase,
3674 sizeof(params.passphrase));
3675 os_memcpy(params.ssid, ssid->ssid, ssid->ssid_len);
3676 params.ssid_len = ssid->ssid_len;
3677 params.persistent_group = 1;
3678
3679 wpa_s = wpas_p2p_get_group_iface(wpa_s, addr_allocated, 1);
3680 if (wpa_s == NULL)
3681 return -1;
3682
3683 wpas_start_wps_go(wpa_s, &params, 0);
3684
3685 return 0;
3686}
3687
3688
3689static void wpas_p2p_ie_update(void *ctx, struct wpabuf *beacon_ies,
3690 struct wpabuf *proberesp_ies)
3691{
3692 struct wpa_supplicant *wpa_s = ctx;
3693 if (wpa_s->ap_iface) {
3694 struct hostapd_data *hapd = wpa_s->ap_iface->bss[0];
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003695 if (!(hapd->conf->p2p & P2P_GROUP_OWNER)) {
3696 wpabuf_free(beacon_ies);
3697 wpabuf_free(proberesp_ies);
3698 return;
3699 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003700 if (beacon_ies) {
3701 wpabuf_free(hapd->p2p_beacon_ie);
3702 hapd->p2p_beacon_ie = beacon_ies;
3703 }
3704 wpabuf_free(hapd->p2p_probe_resp_ie);
3705 hapd->p2p_probe_resp_ie = proberesp_ies;
3706 } else {
3707 wpabuf_free(beacon_ies);
3708 wpabuf_free(proberesp_ies);
3709 }
3710 wpa_supplicant_ap_update_beacon(wpa_s);
3711}
3712
3713
3714static void wpas_p2p_idle_update(void *ctx, int idle)
3715{
3716 struct wpa_supplicant *wpa_s = ctx;
3717 if (!wpa_s->ap_iface)
3718 return;
3719 wpa_printf(MSG_DEBUG, "P2P: GO - group %sidle", idle ? "" : "not ");
Dmitry Shmidt04949592012-07-19 12:16:46 -07003720 if (idle)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003721 wpas_p2p_set_group_idle_timeout(wpa_s);
3722 else
3723 eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL);
3724}
3725
3726
3727struct p2p_group * wpas_p2p_group_init(struct wpa_supplicant *wpa_s,
Dmitry Shmidt04949592012-07-19 12:16:46 -07003728 struct wpa_ssid *ssid)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003729{
3730 struct p2p_group *group;
3731 struct p2p_group_config *cfg;
3732
3733 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
3734 return NULL;
3735 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
3736 return NULL;
3737
3738 cfg = os_zalloc(sizeof(*cfg));
3739 if (cfg == NULL)
3740 return NULL;
3741
Dmitry Shmidt04949592012-07-19 12:16:46 -07003742 if (ssid->p2p_persistent_group && wpa_s->conf->persistent_reconnect)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003743 cfg->persistent_group = 2;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003744 else if (ssid->p2p_persistent_group)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003745 cfg->persistent_group = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003746 os_memcpy(cfg->interface_addr, wpa_s->own_addr, ETH_ALEN);
3747 if (wpa_s->max_stations &&
3748 wpa_s->max_stations < wpa_s->conf->max_num_sta)
3749 cfg->max_clients = wpa_s->max_stations;
3750 else
3751 cfg->max_clients = wpa_s->conf->max_num_sta;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003752 os_memcpy(cfg->ssid, ssid->ssid, ssid->ssid_len);
3753 cfg->ssid_len = ssid->ssid_len;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003754 cfg->cb_ctx = wpa_s;
3755 cfg->ie_update = wpas_p2p_ie_update;
3756 cfg->idle_update = wpas_p2p_idle_update;
3757
3758 group = p2p_group_init(wpa_s->global->p2p, cfg);
3759 if (group == NULL)
3760 os_free(cfg);
Dmitry Shmidt04949592012-07-19 12:16:46 -07003761 if (ssid->mode != WPAS_MODE_P2P_GROUP_FORMATION)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003762 p2p_group_notif_formation_done(group);
3763 wpa_s->p2p_group = group;
3764 return group;
3765}
3766
3767
3768void wpas_p2p_wps_success(struct wpa_supplicant *wpa_s, const u8 *peer_addr,
3769 int registrar)
3770{
Dmitry Shmidt04949592012-07-19 12:16:46 -07003771 struct wpa_ssid *ssid = wpa_s->current_ssid;
3772
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003773 if (!wpa_s->p2p_in_provisioning) {
3774 wpa_printf(MSG_DEBUG, "P2P: Ignore WPS success event - P2P "
3775 "provisioning not in progress");
3776 return;
3777 }
3778
Dmitry Shmidt04949592012-07-19 12:16:46 -07003779 if (ssid && ssid->mode == WPAS_MODE_INFRA) {
3780 u8 go_dev_addr[ETH_ALEN];
3781 os_memcpy(go_dev_addr, wpa_s->bssid, ETH_ALEN);
3782 wpas_p2p_persistent_group(wpa_s, go_dev_addr, ssid->ssid,
3783 ssid->ssid_len);
3784 /* Clear any stored provisioning info */
3785 p2p_clear_provisioning_info(wpa_s->global->p2p, go_dev_addr);
3786 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003787
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003788 eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s->parent,
3789 NULL);
Dmitry Shmidt04949592012-07-19 12:16:46 -07003790 if (ssid && ssid->mode == WPAS_MODE_INFRA) {
3791 /*
3792 * Use a separate timeout for initial data connection to
3793 * complete to allow the group to be removed automatically if
3794 * something goes wrong in this step before the P2P group idle
3795 * timeout mechanism is taken into use.
3796 */
3797 eloop_register_timeout(P2P_MAX_INITIAL_CONN_WAIT, 0,
3798 wpas_p2p_group_formation_timeout,
3799 wpa_s->parent, NULL);
3800 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003801 if (wpa_s->global->p2p)
3802 p2p_wps_success_cb(wpa_s->global->p2p, peer_addr);
3803 else if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
3804 wpa_drv_wps_success_cb(wpa_s, peer_addr);
3805 wpas_group_formation_completed(wpa_s, 1);
3806}
3807
3808
Jouni Malinen75ecf522011-06-27 15:19:46 -07003809void wpas_p2p_wps_failed(struct wpa_supplicant *wpa_s,
3810 struct wps_event_fail *fail)
3811{
3812 if (!wpa_s->p2p_in_provisioning) {
3813 wpa_printf(MSG_DEBUG, "P2P: Ignore WPS fail event - P2P "
3814 "provisioning not in progress");
3815 return;
3816 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003817
3818 if (wpa_s->go_params) {
3819 p2p_clear_provisioning_info(
3820 wpa_s->global->p2p,
Dmitry Shmidt04949592012-07-19 12:16:46 -07003821 wpa_s->go_params->peer_device_addr);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003822 }
3823
Jouni Malinen75ecf522011-06-27 15:19:46 -07003824 wpas_notify_p2p_wps_failed(wpa_s, fail);
3825}
3826
3827
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003828int wpas_p2p_prov_disc(struct wpa_supplicant *wpa_s, const u8 *peer_addr,
Dmitry Shmidt04949592012-07-19 12:16:46 -07003829 const char *config_method,
3830 enum wpas_p2p_prov_disc_use use)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003831{
3832 u16 config_methods;
3833
Dmitry Shmidt04949592012-07-19 12:16:46 -07003834 wpa_s->p2p_fallback_to_go_neg = 0;
3835 wpa_s->pending_pd_use = NORMAL_PD;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003836 if (os_strncmp(config_method, "display", 7) == 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003837 config_methods = WPS_CONFIG_DISPLAY;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003838 else if (os_strncmp(config_method, "keypad", 6) == 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003839 config_methods = WPS_CONFIG_KEYPAD;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003840 else if (os_strncmp(config_method, "pbc", 3) == 0 ||
3841 os_strncmp(config_method, "pushbutton", 10) == 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003842 config_methods = WPS_CONFIG_PUSHBUTTON;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003843 else {
3844 wpa_printf(MSG_DEBUG, "P2P: Unknown config method");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003845 return -1;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003846 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003847
Dmitry Shmidt04949592012-07-19 12:16:46 -07003848 if (use == WPAS_P2P_PD_AUTO) {
3849 os_memcpy(wpa_s->pending_join_dev_addr, peer_addr, ETH_ALEN);
3850 wpa_s->pending_pd_config_methods = config_methods;
3851 wpa_s->p2p_auto_pd = 1;
3852 wpa_s->p2p_auto_join = 0;
3853 wpa_s->pending_pd_before_join = 0;
3854 wpa_s->auto_pd_scan_retry = 0;
3855 wpas_p2p_stop_find(wpa_s);
3856 wpa_s->p2p_join_scan_count = 0;
3857 os_get_time(&wpa_s->p2p_auto_started);
3858 wpa_printf(MSG_DEBUG, "P2P: Auto PD started at %ld.%06ld",
3859 wpa_s->p2p_auto_started.sec,
3860 wpa_s->p2p_auto_started.usec);
3861 wpas_p2p_join_scan(wpa_s, NULL);
3862 return 0;
3863 }
3864
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003865 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT) {
3866 return wpa_drv_p2p_prov_disc_req(wpa_s, peer_addr,
Dmitry Shmidt04949592012-07-19 12:16:46 -07003867 config_methods,
3868 use == WPAS_P2P_PD_FOR_JOIN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003869 }
3870
3871 if (wpa_s->global->p2p == NULL || wpa_s->global->p2p_disabled)
3872 return -1;
3873
3874 return p2p_prov_disc_req(wpa_s->global->p2p, peer_addr,
Dmitry Shmidt04949592012-07-19 12:16:46 -07003875 config_methods, use == WPAS_P2P_PD_FOR_JOIN,
3876 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003877}
3878
3879
3880int wpas_p2p_scan_result_text(const u8 *ies, size_t ies_len, char *buf,
3881 char *end)
3882{
3883 return p2p_scan_result_text(ies, ies_len, buf, end);
3884}
3885
3886
3887static void wpas_p2p_clear_pending_action_tx(struct wpa_supplicant *wpa_s)
3888{
3889 if (!wpa_s->pending_action_tx)
3890 return;
3891
3892 wpa_printf(MSG_DEBUG, "P2P: Drop pending Action TX due to new "
3893 "operation request");
3894 wpabuf_free(wpa_s->pending_action_tx);
3895 wpa_s->pending_action_tx = NULL;
3896}
3897
3898
3899int wpas_p2p_find(struct wpa_supplicant *wpa_s, unsigned int timeout,
3900 enum p2p_discovery_type type,
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08003901 unsigned int num_req_dev_types, const u8 *req_dev_types,
3902 const u8 *dev_id)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003903{
3904 wpas_p2p_clear_pending_action_tx(wpa_s);
3905 wpa_s->p2p_long_listen = 0;
3906
3907 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
3908 return wpa_drv_p2p_find(wpa_s, timeout, type);
3909
Dmitry Shmidt04949592012-07-19 12:16:46 -07003910 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL ||
3911 wpa_s->p2p_in_provisioning)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003912 return -1;
3913
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08003914 wpa_supplicant_cancel_sched_scan(wpa_s);
3915
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003916 return p2p_find(wpa_s->global->p2p, timeout, type,
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08003917 num_req_dev_types, req_dev_types, dev_id);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003918}
3919
3920
3921void wpas_p2p_stop_find(struct wpa_supplicant *wpa_s)
3922{
3923 wpas_p2p_clear_pending_action_tx(wpa_s);
3924 wpa_s->p2p_long_listen = 0;
3925 eloop_cancel_timeout(wpas_p2p_long_listen_timeout, wpa_s, NULL);
3926 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003927 wpa_s->p2p_cb_on_scan_complete = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003928
3929 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT) {
3930 wpa_drv_p2p_stop_find(wpa_s);
3931 return;
3932 }
3933
3934 if (wpa_s->global->p2p)
3935 p2p_stop_find(wpa_s->global->p2p);
3936
3937 wpas_p2p_remove_pending_group_interface(wpa_s);
3938}
3939
3940
3941static void wpas_p2p_long_listen_timeout(void *eloop_ctx, void *timeout_ctx)
3942{
3943 struct wpa_supplicant *wpa_s = eloop_ctx;
3944 wpa_s->p2p_long_listen = 0;
3945}
3946
3947
3948int wpas_p2p_listen(struct wpa_supplicant *wpa_s, unsigned int timeout)
3949{
3950 int res;
3951
3952 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
3953 return -1;
3954
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08003955 wpa_supplicant_cancel_sched_scan(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003956 wpas_p2p_clear_pending_action_tx(wpa_s);
3957
3958 if (timeout == 0) {
3959 /*
3960 * This is a request for unlimited Listen state. However, at
3961 * least for now, this is mapped to a Listen state for one
3962 * hour.
3963 */
3964 timeout = 3600;
3965 }
3966 eloop_cancel_timeout(wpas_p2p_long_listen_timeout, wpa_s, NULL);
3967 wpa_s->p2p_long_listen = 0;
3968
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003969 /*
3970 * Stop previous find/listen operation to avoid trying to request a new
3971 * remain-on-channel operation while the driver is still running the
3972 * previous one.
3973 */
3974 if (wpa_s->global->p2p)
3975 p2p_stop_find(wpa_s->global->p2p);
3976
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003977 res = wpas_p2p_listen_start(wpa_s, timeout * 1000);
3978 if (res == 0 && timeout * 1000 > wpa_s->max_remain_on_chan) {
3979 wpa_s->p2p_long_listen = timeout * 1000;
3980 eloop_register_timeout(timeout, 0,
3981 wpas_p2p_long_listen_timeout,
3982 wpa_s, NULL);
3983 }
3984
3985 return res;
3986}
3987
3988
3989int wpas_p2p_assoc_req_ie(struct wpa_supplicant *wpa_s, struct wpa_bss *bss,
3990 u8 *buf, size_t len, int p2p_group)
3991{
3992 struct wpabuf *p2p_ie;
3993 int ret;
3994
3995 if (wpa_s->global->p2p_disabled)
3996 return -1;
3997 if (wpa_s->global->p2p == NULL)
3998 return -1;
3999 if (bss == NULL)
4000 return -1;
4001
4002 p2p_ie = wpa_bss_get_vendor_ie_multi(bss, P2P_IE_VENDOR_TYPE);
4003 ret = p2p_assoc_req_ie(wpa_s->global->p2p, bss->bssid, buf, len,
4004 p2p_group, p2p_ie);
4005 wpabuf_free(p2p_ie);
4006
4007 return ret;
4008}
4009
4010
4011int wpas_p2p_probe_req_rx(struct wpa_supplicant *wpa_s, const u8 *addr,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004012 const u8 *dst, const u8 *bssid,
Dmitry Shmidt04949592012-07-19 12:16:46 -07004013 const u8 *ie, size_t ie_len, int ssi_signal)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004014{
4015 if (wpa_s->global->p2p_disabled)
4016 return 0;
4017 if (wpa_s->global->p2p == NULL)
4018 return 0;
4019
Dmitry Shmidt04949592012-07-19 12:16:46 -07004020 switch (p2p_probe_req_rx(wpa_s->global->p2p, addr, dst, bssid,
4021 ie, ie_len)) {
4022 case P2P_PREQ_NOT_P2P:
4023 wpas_notify_preq(wpa_s, addr, dst, bssid, ie, ie_len,
4024 ssi_signal);
4025 /* fall through */
4026 case P2P_PREQ_MALFORMED:
4027 case P2P_PREQ_NOT_LISTEN:
4028 case P2P_PREQ_NOT_PROCESSED:
4029 default: /* make gcc happy */
4030 return 0;
4031 case P2P_PREQ_PROCESSED:
4032 return 1;
4033 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004034}
4035
4036
4037void wpas_p2p_rx_action(struct wpa_supplicant *wpa_s, const u8 *da,
4038 const u8 *sa, const u8 *bssid,
4039 u8 category, const u8 *data, size_t len, int freq)
4040{
4041 if (wpa_s->global->p2p_disabled)
4042 return;
4043 if (wpa_s->global->p2p == NULL)
4044 return;
4045
4046 p2p_rx_action(wpa_s->global->p2p, da, sa, bssid, category, data, len,
4047 freq);
4048}
4049
4050
4051void wpas_p2p_scan_ie(struct wpa_supplicant *wpa_s, struct wpabuf *ies)
4052{
4053 if (wpa_s->global->p2p_disabled)
4054 return;
4055 if (wpa_s->global->p2p == NULL)
4056 return;
4057
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08004058 p2p_scan_ie(wpa_s->global->p2p, ies, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004059}
4060
4061
4062void wpas_p2p_group_deinit(struct wpa_supplicant *wpa_s)
4063{
4064 p2p_group_deinit(wpa_s->p2p_group);
4065 wpa_s->p2p_group = NULL;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004066
4067 wpa_s->ap_configured_cb = NULL;
4068 wpa_s->ap_configured_cb_ctx = NULL;
4069 wpa_s->ap_configured_cb_data = NULL;
4070 wpa_s->connect_without_scan = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004071}
4072
4073
4074int wpas_p2p_reject(struct wpa_supplicant *wpa_s, const u8 *addr)
4075{
4076 wpa_s->p2p_long_listen = 0;
4077
4078 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
4079 return wpa_drv_p2p_reject(wpa_s, addr);
4080
4081 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
4082 return -1;
4083
4084 return p2p_reject(wpa_s->global->p2p, addr);
4085}
4086
4087
4088/* Invite to reinvoke a persistent group */
4089int wpas_p2p_invite(struct wpa_supplicant *wpa_s, const u8 *peer_addr,
4090 struct wpa_ssid *ssid, const u8 *go_dev_addr)
4091{
4092 enum p2p_invite_role role;
4093 u8 *bssid = NULL;
4094
4095 if (ssid->mode == WPAS_MODE_P2P_GO) {
4096 role = P2P_INVITE_ROLE_GO;
4097 if (peer_addr == NULL) {
4098 wpa_printf(MSG_DEBUG, "P2P: Missing peer "
4099 "address in invitation command");
4100 return -1;
4101 }
4102 if (wpas_p2p_create_iface(wpa_s)) {
4103 if (wpas_p2p_add_group_interface(wpa_s,
4104 WPA_IF_P2P_GO) < 0) {
4105 wpa_printf(MSG_ERROR, "P2P: Failed to "
4106 "allocate a new interface for the "
4107 "group");
4108 return -1;
4109 }
4110 bssid = wpa_s->pending_interface_addr;
4111 } else
4112 bssid = wpa_s->own_addr;
4113 } else {
4114 role = P2P_INVITE_ROLE_CLIENT;
4115 peer_addr = ssid->bssid;
4116 }
4117 wpa_s->pending_invite_ssid_id = ssid->id;
4118
4119 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
4120 return wpa_drv_p2p_invite(wpa_s, peer_addr, role, bssid,
4121 ssid->ssid, ssid->ssid_len,
4122 go_dev_addr, 1);
4123
4124 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
4125 return -1;
4126
4127 return p2p_invite(wpa_s->global->p2p, peer_addr, role, bssid,
4128 ssid->ssid, ssid->ssid_len, 0, go_dev_addr, 1);
4129}
4130
4131
4132/* Invite to join an active group */
4133int wpas_p2p_invite_group(struct wpa_supplicant *wpa_s, const char *ifname,
4134 const u8 *peer_addr, const u8 *go_dev_addr)
4135{
4136 struct wpa_global *global = wpa_s->global;
4137 enum p2p_invite_role role;
4138 u8 *bssid = NULL;
4139 struct wpa_ssid *ssid;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08004140 int persistent;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004141
4142 for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
4143 if (os_strcmp(wpa_s->ifname, ifname) == 0)
4144 break;
4145 }
4146 if (wpa_s == NULL) {
4147 wpa_printf(MSG_DEBUG, "P2P: Interface '%s' not found", ifname);
4148 return -1;
4149 }
4150
4151 ssid = wpa_s->current_ssid;
4152 if (ssid == NULL) {
4153 wpa_printf(MSG_DEBUG, "P2P: No current SSID to use for "
4154 "invitation");
4155 return -1;
4156 }
4157
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08004158 persistent = ssid->p2p_persistent_group &&
4159 wpas_p2p_get_persistent(wpa_s->parent, peer_addr,
4160 ssid->ssid, ssid->ssid_len);
4161
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004162 if (ssid->mode == WPAS_MODE_P2P_GO) {
4163 role = P2P_INVITE_ROLE_ACTIVE_GO;
4164 bssid = wpa_s->own_addr;
4165 if (go_dev_addr == NULL)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004166 go_dev_addr = wpa_s->global->p2p_dev_addr;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004167 } else {
4168 role = P2P_INVITE_ROLE_CLIENT;
4169 if (wpa_s->wpa_state < WPA_ASSOCIATED) {
4170 wpa_printf(MSG_DEBUG, "P2P: Not associated - cannot "
4171 "invite to current group");
4172 return -1;
4173 }
4174 bssid = wpa_s->bssid;
4175 if (go_dev_addr == NULL &&
4176 !is_zero_ether_addr(wpa_s->go_dev_addr))
4177 go_dev_addr = wpa_s->go_dev_addr;
4178 }
4179 wpa_s->parent->pending_invite_ssid_id = -1;
4180
4181 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
4182 return wpa_drv_p2p_invite(wpa_s, peer_addr, role, bssid,
4183 ssid->ssid, ssid->ssid_len,
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08004184 go_dev_addr, persistent);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004185
4186 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
4187 return -1;
4188
4189 return p2p_invite(wpa_s->global->p2p, peer_addr, role, bssid,
4190 ssid->ssid, ssid->ssid_len, wpa_s->assoc_freq,
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08004191 go_dev_addr, persistent);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004192}
4193
4194
4195void wpas_p2p_completed(struct wpa_supplicant *wpa_s)
4196{
4197 struct wpa_ssid *ssid = wpa_s->current_ssid;
4198 const char *ssid_txt;
4199 u8 go_dev_addr[ETH_ALEN];
Jouni Malinen75ecf522011-06-27 15:19:46 -07004200 int network_id = -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004201 int persistent;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004202 int freq;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004203
Dmitry Shmidt04949592012-07-19 12:16:46 -07004204 if (ssid == NULL || ssid->mode != WPAS_MODE_P2P_GROUP_FORMATION) {
4205 eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
4206 wpa_s->parent, NULL);
4207 }
4208
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004209 if (!wpa_s->show_group_started || !ssid)
4210 return;
4211
4212 wpa_s->show_group_started = 0;
4213
4214 ssid_txt = wpa_ssid_txt(ssid->ssid, ssid->ssid_len);
4215 os_memset(go_dev_addr, 0, ETH_ALEN);
4216 if (ssid->bssid_set)
4217 os_memcpy(go_dev_addr, ssid->bssid, ETH_ALEN);
4218 persistent = wpas_p2p_persistent_group(wpa_s, go_dev_addr, ssid->ssid,
4219 ssid->ssid_len);
4220 os_memcpy(wpa_s->go_dev_addr, go_dev_addr, ETH_ALEN);
4221
4222 if (wpa_s->global->p2p_group_formation == wpa_s)
4223 wpa_s->global->p2p_group_formation = NULL;
4224
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004225 freq = wpa_s->current_bss ? wpa_s->current_bss->freq :
4226 (int) wpa_s->assoc_freq;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004227 if (ssid->passphrase == NULL && ssid->psk_set) {
4228 char psk[65];
4229 wpa_snprintf_hex(psk, sizeof(psk), ssid->psk, 32);
4230 wpa_msg(wpa_s->parent, MSG_INFO, P2P_EVENT_GROUP_STARTED
4231 "%s client ssid=\"%s\" freq=%d psk=%s go_dev_addr="
4232 MACSTR "%s",
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004233 wpa_s->ifname, ssid_txt, freq, psk,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004234 MAC2STR(go_dev_addr),
4235 persistent ? " [PERSISTENT]" : "");
4236 } else {
4237 wpa_msg(wpa_s->parent, MSG_INFO, P2P_EVENT_GROUP_STARTED
4238 "%s client ssid=\"%s\" freq=%d passphrase=\"%s\" "
4239 "go_dev_addr=" MACSTR "%s",
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004240 wpa_s->ifname, ssid_txt, freq,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004241 ssid->passphrase ? ssid->passphrase : "",
4242 MAC2STR(go_dev_addr),
4243 persistent ? " [PERSISTENT]" : "");
4244 }
4245
4246 if (persistent)
Jouni Malinen75ecf522011-06-27 15:19:46 -07004247 network_id = wpas_p2p_store_persistent_group(wpa_s->parent,
4248 ssid, go_dev_addr);
4249 if (network_id < 0)
4250 network_id = ssid->id;
4251 wpas_notify_p2p_group_started(wpa_s, ssid, network_id, 1);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004252}
4253
4254
4255int wpas_p2p_presence_req(struct wpa_supplicant *wpa_s, u32 duration1,
4256 u32 interval1, u32 duration2, u32 interval2)
4257{
4258 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
4259 return -1;
4260 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
4261 return -1;
4262
4263 if (wpa_s->wpa_state < WPA_ASSOCIATED ||
4264 wpa_s->current_ssid == NULL ||
4265 wpa_s->current_ssid->mode != WPAS_MODE_INFRA)
4266 return -1;
4267
4268 return p2p_presence_req(wpa_s->global->p2p, wpa_s->bssid,
4269 wpa_s->own_addr, wpa_s->assoc_freq,
4270 duration1, interval1, duration2, interval2);
4271}
4272
4273
4274int wpas_p2p_ext_listen(struct wpa_supplicant *wpa_s, unsigned int period,
4275 unsigned int interval)
4276{
4277 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
4278 return -1;
4279
4280 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
4281 return -1;
4282
4283 return p2p_ext_listen(wpa_s->global->p2p, period, interval);
4284}
4285
4286
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004287static int wpas_p2p_is_client(struct wpa_supplicant *wpa_s)
4288{
4289 return wpa_s->current_ssid != NULL &&
4290 wpa_s->current_ssid->p2p_group &&
4291 wpa_s->current_ssid->mode == WPAS_MODE_INFRA;
4292}
4293
4294
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004295static void wpas_p2p_group_idle_timeout(void *eloop_ctx, void *timeout_ctx)
4296{
4297 struct wpa_supplicant *wpa_s = eloop_ctx;
4298
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004299 if (wpa_s->conf->p2p_group_idle == 0 && !wpas_p2p_is_client(wpa_s)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004300 wpa_printf(MSG_DEBUG, "P2P: Ignore group idle timeout - "
4301 "disabled");
4302 return;
4303 }
4304
Dmitry Shmidt04949592012-07-19 12:16:46 -07004305 wpa_printf(MSG_DEBUG, "P2P: Group idle timeout reached - terminate "
4306 "group");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004307 wpa_s->removal_reason = P2P_GROUP_REMOVAL_IDLE_TIMEOUT;
Dmitry Shmidt04949592012-07-19 12:16:46 -07004308 wpas_p2p_group_delete(wpa_s, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004309}
4310
4311
4312static void wpas_p2p_set_group_idle_timeout(struct wpa_supplicant *wpa_s)
4313{
Dmitry Shmidt04949592012-07-19 12:16:46 -07004314 int timeout;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004315
Dmitry Shmidt04949592012-07-19 12:16:46 -07004316 if (eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL) > 0)
4317 wpa_printf(MSG_DEBUG, "P2P: Cancelled P2P group idle timeout");
4318
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004319 if (wpa_s->current_ssid == NULL || !wpa_s->current_ssid->p2p_group)
4320 return;
4321
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004322 timeout = wpa_s->conf->p2p_group_idle;
4323 if (wpa_s->current_ssid->mode == WPAS_MODE_INFRA &&
4324 (timeout == 0 || timeout > P2P_MAX_CLIENT_IDLE))
4325 timeout = P2P_MAX_CLIENT_IDLE;
4326
4327 if (timeout == 0)
4328 return;
4329
Dmitry Shmidt04949592012-07-19 12:16:46 -07004330 if (timeout < 0) {
4331 if (wpa_s->current_ssid->mode == WPAS_MODE_INFRA)
4332 timeout = 0; /* special client mode no-timeout */
4333 else
4334 return;
4335 }
4336
4337 if (wpa_s->p2p_in_provisioning) {
4338 /*
4339 * Use the normal group formation timeout during the
4340 * provisioning phase to avoid terminating this process too
4341 * early due to group idle timeout.
4342 */
4343 wpa_printf(MSG_DEBUG, "P2P: Do not use P2P group idle timeout "
4344 "during provisioning");
4345 return;
4346 }
4347
4348 if (wpa_s->show_group_started) {
4349 /*
4350 * Use the normal group formation timeout between the end of
4351 * the provisioning phase and completion of 4-way handshake to
4352 * avoid terminating this process too early due to group idle
4353 * timeout.
4354 */
4355 wpa_printf(MSG_DEBUG, "P2P: Do not use P2P group idle timeout "
4356 "while waiting for initial 4-way handshake to "
4357 "complete");
4358 return;
4359 }
4360
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004361 wpa_printf(MSG_DEBUG, "P2P: Set P2P group idle timeout to %u seconds",
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004362 timeout);
4363 eloop_register_timeout(timeout, 0, wpas_p2p_group_idle_timeout,
4364 wpa_s, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004365}
4366
4367
4368void wpas_p2p_deauth_notif(struct wpa_supplicant *wpa_s, const u8 *bssid,
Dmitry Shmidt04949592012-07-19 12:16:46 -07004369 u16 reason_code, const u8 *ie, size_t ie_len,
4370 int locally_generated)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004371{
4372 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
4373 return;
4374 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
4375 return;
4376
Dmitry Shmidt04949592012-07-19 12:16:46 -07004377 if (!locally_generated)
4378 p2p_deauth_notif(wpa_s->global->p2p, bssid, reason_code, ie,
4379 ie_len);
4380
4381 if (reason_code == WLAN_REASON_DEAUTH_LEAVING && !locally_generated &&
4382 wpa_s->current_ssid &&
4383 wpa_s->current_ssid->p2p_group &&
4384 wpa_s->current_ssid->mode == WPAS_MODE_INFRA) {
4385 wpa_printf(MSG_DEBUG, "P2P: GO indicated that the P2P Group "
4386 "session is ending");
4387 wpa_s->removal_reason = P2P_GROUP_REMOVAL_GO_ENDING_SESSION;
4388 wpas_p2p_group_delete(wpa_s, 0);
4389 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004390}
4391
4392
4393void wpas_p2p_disassoc_notif(struct wpa_supplicant *wpa_s, const u8 *bssid,
Dmitry Shmidt04949592012-07-19 12:16:46 -07004394 u16 reason_code, const u8 *ie, size_t ie_len,
4395 int locally_generated)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004396{
4397 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
4398 return;
4399 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
4400 return;
4401
Dmitry Shmidt04949592012-07-19 12:16:46 -07004402 if (!locally_generated)
4403 p2p_disassoc_notif(wpa_s->global->p2p, bssid, reason_code, ie,
4404 ie_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004405}
4406
4407
4408void wpas_p2p_update_config(struct wpa_supplicant *wpa_s)
4409{
4410 struct p2p_data *p2p = wpa_s->global->p2p;
4411
4412 if (p2p == NULL)
4413 return;
4414
4415 if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_CAPABLE))
4416 return;
4417
4418 if (wpa_s->conf->changed_parameters & CFG_CHANGED_DEVICE_NAME)
4419 p2p_set_dev_name(p2p, wpa_s->conf->device_name);
4420
4421 if (wpa_s->conf->changed_parameters & CFG_CHANGED_DEVICE_TYPE)
4422 p2p_set_pri_dev_type(p2p, wpa_s->conf->device_type);
4423
4424 if (wpa_s->wps &&
4425 (wpa_s->conf->changed_parameters & CFG_CHANGED_CONFIG_METHODS))
4426 p2p_set_config_methods(p2p, wpa_s->wps->config_methods);
4427
4428 if (wpa_s->wps && (wpa_s->conf->changed_parameters & CFG_CHANGED_UUID))
4429 p2p_set_uuid(p2p, wpa_s->wps->uuid);
4430
4431 if (wpa_s->conf->changed_parameters & CFG_CHANGED_WPS_STRING) {
4432 p2p_set_manufacturer(p2p, wpa_s->conf->manufacturer);
4433 p2p_set_model_name(p2p, wpa_s->conf->model_name);
4434 p2p_set_model_number(p2p, wpa_s->conf->model_number);
4435 p2p_set_serial_number(p2p, wpa_s->conf->serial_number);
4436 }
4437
4438 if (wpa_s->conf->changed_parameters & CFG_CHANGED_SEC_DEVICE_TYPE)
4439 p2p_set_sec_dev_types(p2p,
4440 (void *) wpa_s->conf->sec_device_type,
4441 wpa_s->conf->num_sec_device_types);
4442
4443 if (wpa_s->conf->changed_parameters & CFG_CHANGED_VENDOR_EXTENSION) {
4444 int i;
4445 p2p_remove_wps_vendor_extensions(p2p);
4446 for (i = 0; i < MAX_WPS_VENDOR_EXT; i++) {
4447 if (wpa_s->conf->wps_vendor_ext[i] == NULL)
4448 continue;
4449 p2p_add_wps_vendor_extension(
4450 p2p, wpa_s->conf->wps_vendor_ext[i]);
4451 }
4452 }
4453
4454 if ((wpa_s->conf->changed_parameters & CFG_CHANGED_COUNTRY) &&
4455 wpa_s->conf->country[0] && wpa_s->conf->country[1]) {
4456 char country[3];
4457 country[0] = wpa_s->conf->country[0];
4458 country[1] = wpa_s->conf->country[1];
4459 country[2] = 0x04;
4460 p2p_set_country(p2p, country);
4461 }
4462
4463 if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_SSID_POSTFIX) {
4464 p2p_set_ssid_postfix(p2p, (u8 *) wpa_s->conf->p2p_ssid_postfix,
4465 wpa_s->conf->p2p_ssid_postfix ?
4466 os_strlen(wpa_s->conf->p2p_ssid_postfix) :
4467 0);
4468 }
4469
4470 if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_INTRA_BSS)
4471 p2p_set_intra_bss_dist(p2p, wpa_s->conf->p2p_intra_bss);
Jouni Malinen75ecf522011-06-27 15:19:46 -07004472
4473 if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_LISTEN_CHANNEL) {
4474 u8 reg_class, channel;
4475 int ret;
4476 unsigned int r;
4477 if (wpa_s->conf->p2p_listen_reg_class &&
4478 wpa_s->conf->p2p_listen_channel) {
4479 reg_class = wpa_s->conf->p2p_listen_reg_class;
4480 channel = wpa_s->conf->p2p_listen_channel;
4481 } else {
4482 reg_class = 81;
4483 /*
4484 * Pick one of the social channels randomly as the
4485 * listen channel.
4486 */
4487 os_get_random((u8 *) &r, sizeof(r));
4488 channel = 1 + (r % 3) * 5;
4489 }
4490 ret = p2p_set_listen_channel(p2p, reg_class, channel);
4491 if (ret)
4492 wpa_printf(MSG_ERROR, "P2P: Own listen channel update "
4493 "failed: %d", ret);
4494 }
4495 if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_OPER_CHANNEL) {
4496 u8 op_reg_class, op_channel, cfg_op_channel;
4497 int ret = 0;
4498 unsigned int r;
4499 if (wpa_s->conf->p2p_oper_reg_class &&
4500 wpa_s->conf->p2p_oper_channel) {
4501 op_reg_class = wpa_s->conf->p2p_oper_reg_class;
4502 op_channel = wpa_s->conf->p2p_oper_channel;
4503 cfg_op_channel = 1;
4504 } else {
4505 op_reg_class = 81;
4506 /*
4507 * Use random operation channel from (1, 6, 11)
4508 *if no other preference is indicated.
4509 */
4510 os_get_random((u8 *) &r, sizeof(r));
4511 op_channel = 1 + (r % 3) * 5;
4512 cfg_op_channel = 0;
4513 }
4514 ret = p2p_set_oper_channel(p2p, op_reg_class, op_channel,
4515 cfg_op_channel);
4516 if (ret)
4517 wpa_printf(MSG_ERROR, "P2P: Own oper channel update "
4518 "failed: %d", ret);
4519 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07004520
4521 if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_PREF_CHAN) {
4522 if (p2p_set_pref_chan(p2p, wpa_s->conf->num_p2p_pref_chan,
4523 wpa_s->conf->p2p_pref_chan) < 0) {
4524 wpa_printf(MSG_ERROR, "P2P: Preferred channel list "
4525 "update failed");
4526 }
4527 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004528}
4529
4530
4531int wpas_p2p_set_noa(struct wpa_supplicant *wpa_s, u8 count, int start,
4532 int duration)
4533{
4534 if (!wpa_s->ap_iface)
4535 return -1;
4536 return hostapd_p2p_set_noa(wpa_s->ap_iface->bss[0], count, start,
4537 duration);
4538}
4539
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004540
4541int wpas_p2p_set_cross_connect(struct wpa_supplicant *wpa_s, int enabled)
4542{
4543 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
4544 return -1;
4545 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
4546 return -1;
4547
4548 wpa_s->global->cross_connection = enabled;
4549 p2p_set_cross_connect(wpa_s->global->p2p, enabled);
4550
4551 if (!enabled) {
4552 struct wpa_supplicant *iface;
4553
4554 for (iface = wpa_s->global->ifaces; iface; iface = iface->next)
4555 {
4556 if (iface->cross_connect_enabled == 0)
4557 continue;
4558
4559 iface->cross_connect_enabled = 0;
4560 iface->cross_connect_in_use = 0;
4561 wpa_msg(iface->parent, MSG_INFO,
4562 P2P_EVENT_CROSS_CONNECT_DISABLE "%s %s",
4563 iface->ifname, iface->cross_connect_uplink);
4564 }
4565 }
4566
4567 return 0;
4568}
4569
4570
4571static void wpas_p2p_enable_cross_connect(struct wpa_supplicant *uplink)
4572{
4573 struct wpa_supplicant *iface;
4574
4575 if (!uplink->global->cross_connection)
4576 return;
4577
4578 for (iface = uplink->global->ifaces; iface; iface = iface->next) {
4579 if (!iface->cross_connect_enabled)
4580 continue;
4581 if (os_strcmp(uplink->ifname, iface->cross_connect_uplink) !=
4582 0)
4583 continue;
4584 if (iface->ap_iface == NULL)
4585 continue;
4586 if (iface->cross_connect_in_use)
4587 continue;
4588
4589 iface->cross_connect_in_use = 1;
4590 wpa_msg(iface->parent, MSG_INFO,
4591 P2P_EVENT_CROSS_CONNECT_ENABLE "%s %s",
4592 iface->ifname, iface->cross_connect_uplink);
4593 }
4594}
4595
4596
4597static void wpas_p2p_disable_cross_connect(struct wpa_supplicant *uplink)
4598{
4599 struct wpa_supplicant *iface;
4600
4601 for (iface = uplink->global->ifaces; iface; iface = iface->next) {
4602 if (!iface->cross_connect_enabled)
4603 continue;
4604 if (os_strcmp(uplink->ifname, iface->cross_connect_uplink) !=
4605 0)
4606 continue;
4607 if (!iface->cross_connect_in_use)
4608 continue;
4609
4610 wpa_msg(iface->parent, MSG_INFO,
4611 P2P_EVENT_CROSS_CONNECT_DISABLE "%s %s",
4612 iface->ifname, iface->cross_connect_uplink);
4613 iface->cross_connect_in_use = 0;
4614 }
4615}
4616
4617
4618void wpas_p2p_notif_connected(struct wpa_supplicant *wpa_s)
4619{
4620 if (wpa_s->ap_iface || wpa_s->current_ssid == NULL ||
4621 wpa_s->current_ssid->mode != WPAS_MODE_INFRA ||
4622 wpa_s->cross_connect_disallowed)
4623 wpas_p2p_disable_cross_connect(wpa_s);
4624 else
4625 wpas_p2p_enable_cross_connect(wpa_s);
Dmitry Shmidt04949592012-07-19 12:16:46 -07004626 if (!wpa_s->ap_iface &&
4627 eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL) > 0)
4628 wpa_printf(MSG_DEBUG, "P2P: Cancelled P2P group idle timeout");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004629}
4630
4631
4632void wpas_p2p_notif_disconnected(struct wpa_supplicant *wpa_s)
4633{
4634 wpas_p2p_disable_cross_connect(wpa_s);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004635 if (!wpa_s->ap_iface &&
4636 !eloop_is_timeout_registered(wpas_p2p_group_idle_timeout,
4637 wpa_s, NULL))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004638 wpas_p2p_set_group_idle_timeout(wpa_s);
4639}
4640
4641
4642static void wpas_p2p_cross_connect_setup(struct wpa_supplicant *wpa_s)
4643{
4644 struct wpa_supplicant *iface;
4645
4646 if (!wpa_s->global->cross_connection)
4647 return;
4648
4649 for (iface = wpa_s->global->ifaces; iface; iface = iface->next) {
4650 if (iface == wpa_s)
4651 continue;
4652 if (iface->drv_flags &
4653 WPA_DRIVER_FLAGS_P2P_DEDICATED_INTERFACE)
4654 continue;
4655 if (iface->drv_flags & WPA_DRIVER_FLAGS_P2P_CAPABLE)
4656 continue;
4657
4658 wpa_s->cross_connect_enabled = 1;
4659 os_strlcpy(wpa_s->cross_connect_uplink, iface->ifname,
4660 sizeof(wpa_s->cross_connect_uplink));
4661 wpa_printf(MSG_DEBUG, "P2P: Enable cross connection from "
4662 "%s to %s whenever uplink is available",
4663 wpa_s->ifname, wpa_s->cross_connect_uplink);
4664
4665 if (iface->ap_iface || iface->current_ssid == NULL ||
4666 iface->current_ssid->mode != WPAS_MODE_INFRA ||
4667 iface->cross_connect_disallowed ||
4668 iface->wpa_state != WPA_COMPLETED)
4669 break;
4670
4671 wpa_s->cross_connect_in_use = 1;
4672 wpa_msg(wpa_s->parent, MSG_INFO,
4673 P2P_EVENT_CROSS_CONNECT_ENABLE "%s %s",
4674 wpa_s->ifname, wpa_s->cross_connect_uplink);
4675 break;
4676 }
4677}
4678
4679
4680int wpas_p2p_notif_pbc_overlap(struct wpa_supplicant *wpa_s)
4681{
4682 if (wpa_s->p2p_group_interface != P2P_GROUP_INTERFACE_CLIENT &&
4683 !wpa_s->p2p_in_provisioning)
4684 return 0; /* not P2P client operation */
4685
4686 wpa_printf(MSG_DEBUG, "P2P: Terminate connection due to WPS PBC "
4687 "session overlap");
4688 if (wpa_s != wpa_s->parent)
4689 wpa_msg_ctrl(wpa_s->parent, MSG_INFO, WPS_EVENT_OVERLAP);
4690
4691 if (wpa_s->global->p2p)
4692 p2p_group_formation_failed(wpa_s->global->p2p);
4693
4694 eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
4695 wpa_s->parent, NULL);
4696
4697 wpas_group_formation_completed(wpa_s, 0);
4698 return 1;
4699}
4700
4701
4702void wpas_p2p_update_channel_list(struct wpa_supplicant *wpa_s)
4703{
4704 struct p2p_channels chan;
4705
4706 if (wpa_s->global == NULL || wpa_s->global->p2p == NULL)
4707 return;
4708
4709 os_memset(&chan, 0, sizeof(chan));
4710 if (wpas_p2p_setup_channels(wpa_s, &chan)) {
4711 wpa_printf(MSG_ERROR, "P2P: Failed to update supported "
4712 "channel list");
4713 return;
4714 }
4715
4716 p2p_update_channel_list(wpa_s->global->p2p, &chan);
4717}
4718
4719
4720int wpas_p2p_cancel(struct wpa_supplicant *wpa_s)
4721{
4722 struct wpa_global *global = wpa_s->global;
4723 int found = 0;
4724 const u8 *peer;
4725
4726 if (global->p2p == NULL)
4727 return -1;
4728
4729 wpa_printf(MSG_DEBUG, "P2P: Request to cancel group formation");
4730
4731 if (wpa_s->pending_interface_name[0] &&
4732 !is_zero_ether_addr(wpa_s->pending_interface_addr))
4733 found = 1;
4734
4735 peer = p2p_get_go_neg_peer(global->p2p);
4736 if (peer) {
4737 wpa_printf(MSG_DEBUG, "P2P: Unauthorize pending GO Neg peer "
4738 MACSTR, MAC2STR(peer));
4739 p2p_unauthorize(global->p2p, peer);
Dmitry Shmidt04949592012-07-19 12:16:46 -07004740 found = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004741 }
4742
4743 wpas_p2p_stop_find(wpa_s);
4744
4745 for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
4746 if (wpa_s == global->p2p_group_formation &&
4747 (wpa_s->p2p_in_provisioning ||
4748 wpa_s->parent->pending_interface_type ==
4749 WPA_IF_P2P_CLIENT)) {
4750 wpa_printf(MSG_DEBUG, "P2P: Interface %s in group "
4751 "formation found - cancelling",
4752 wpa_s->ifname);
4753 found = 1;
4754 eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
4755 wpa_s->parent, NULL);
Dmitry Shmidt04949592012-07-19 12:16:46 -07004756 wpas_p2p_group_delete(wpa_s, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004757 break;
4758 }
4759 }
4760
4761 if (!found) {
4762 wpa_printf(MSG_DEBUG, "P2P: No ongoing group formation found");
4763 return -1;
4764 }
4765
4766 return 0;
4767}
4768
4769
4770void wpas_p2p_interface_unavailable(struct wpa_supplicant *wpa_s)
4771{
4772 if (wpa_s->current_ssid == NULL || !wpa_s->current_ssid->p2p_group)
4773 return;
4774
4775 wpa_printf(MSG_DEBUG, "P2P: Remove group due to driver resource not "
4776 "being available anymore");
4777 wpa_s->removal_reason = P2P_GROUP_REMOVAL_UNAVAILABLE;
Dmitry Shmidt04949592012-07-19 12:16:46 -07004778 wpas_p2p_group_delete(wpa_s, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004779}
4780
4781
4782void wpas_p2p_update_best_channels(struct wpa_supplicant *wpa_s,
4783 int freq_24, int freq_5, int freq_overall)
4784{
4785 struct p2p_data *p2p = wpa_s->global->p2p;
4786 if (p2p == NULL || (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT))
4787 return;
4788 p2p_set_best_channels(p2p, freq_24, freq_5, freq_overall);
4789}
4790
4791
4792int wpas_p2p_unauthorize(struct wpa_supplicant *wpa_s, const char *addr)
4793{
4794 u8 peer[ETH_ALEN];
4795 struct p2p_data *p2p = wpa_s->global->p2p;
4796
4797 if (p2p == NULL || (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT))
4798 return -1;
4799
4800 if (hwaddr_aton(addr, peer))
4801 return -1;
4802
4803 return p2p_unauthorize(p2p, peer);
4804}
4805
4806
4807/**
4808 * wpas_p2p_disconnect - Disconnect from a P2P Group
4809 * @wpa_s: Pointer to wpa_supplicant data
4810 * Returns: 0 on success, -1 on failure
4811 *
4812 * This can be used to disconnect from a group in which the local end is a P2P
4813 * Client or to end a P2P Group in case the local end is the Group Owner. If a
4814 * virtual network interface was created for this group, that interface will be
4815 * removed. Otherwise, only the configured P2P group network will be removed
4816 * from the interface.
4817 */
4818int wpas_p2p_disconnect(struct wpa_supplicant *wpa_s)
4819{
4820
4821 if (wpa_s == NULL)
4822 return -1;
4823
4824 wpa_s->removal_reason = P2P_GROUP_REMOVAL_REQUESTED;
Dmitry Shmidt04949592012-07-19 12:16:46 -07004825 wpas_p2p_group_delete(wpa_s, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004826
4827 return 0;
4828}
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004829
4830
4831int wpas_p2p_in_progress(struct wpa_supplicant *wpa_s)
4832{
4833 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
4834 return 0;
4835
4836 return p2p_in_progress(wpa_s->global->p2p);
4837}
4838
4839
4840void wpas_p2p_network_removed(struct wpa_supplicant *wpa_s,
4841 struct wpa_ssid *ssid)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004842{
4843 if (wpa_s->p2p_in_provisioning && ssid->p2p_group &&
4844 eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
4845 wpa_s->parent, NULL) > 0) {
Dmitry Shmidt04949592012-07-19 12:16:46 -07004846 /**
4847 * Remove the network by scheduling the group formation
4848 * timeout to happen immediately. The teardown code
4849 * needs to be scheduled to run asynch later so that we
4850 * don't delete data from under ourselves unexpectedly.
4851 * Calling wpas_p2p_group_formation_timeout directly
4852 * causes a series of crashes in WPS failure scenarios.
4853 */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004854 wpa_printf(MSG_DEBUG, "P2P: Canceled group formation due to "
4855 "P2P group network getting removed");
Dmitry Shmidt04949592012-07-19 12:16:46 -07004856 eloop_register_timeout(0, 0, wpas_p2p_group_formation_timeout,
4857 wpa_s->parent, NULL);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004858 }
4859}
4860
4861
4862struct wpa_ssid * wpas_p2p_get_persistent(struct wpa_supplicant *wpa_s,
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08004863 const u8 *addr, const u8 *ssid,
4864 size_t ssid_len)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004865{
4866 struct wpa_ssid *s;
4867 size_t i;
4868
4869 for (s = wpa_s->conf->ssid; s; s = s->next) {
4870 if (s->disabled != 2)
4871 continue;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08004872 if (ssid &&
4873 (ssid_len != s->ssid_len ||
4874 os_memcmp(ssid, s->ssid, ssid_len) != 0))
4875 continue;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004876 if (os_memcmp(s->bssid, addr, ETH_ALEN) == 0)
4877 return s; /* peer is GO in the persistent group */
4878 if (s->mode != WPAS_MODE_P2P_GO || s->p2p_client_list == NULL)
4879 continue;
4880 for (i = 0; i < s->num_p2p_clients; i++) {
4881 if (os_memcmp(s->p2p_client_list + i * ETH_ALEN,
4882 addr, ETH_ALEN) == 0)
4883 return s; /* peer is P2P client in persistent
4884 * group */
4885 }
4886 }
4887
4888 return NULL;
4889}
4890
4891
4892void wpas_p2p_notify_ap_sta_authorized(struct wpa_supplicant *wpa_s,
4893 const u8 *addr)
4894{
4895 if (addr == NULL)
4896 return;
4897 wpas_p2p_add_persistent_group_client(wpa_s, addr);
4898}
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08004899
Dmitry Shmidt04949592012-07-19 12:16:46 -07004900
4901static void wpas_p2p_fallback_to_go_neg(struct wpa_supplicant *wpa_s,
4902 int group_added)
4903{
4904 struct wpa_supplicant *group = wpa_s;
4905 eloop_cancel_timeout(wpas_p2p_pd_before_join_timeout, wpa_s, NULL);
4906 if (wpa_s->global->p2p_group_formation)
4907 group = wpa_s->global->p2p_group_formation;
4908 wpa_s = wpa_s->parent;
4909 offchannel_send_action_done(wpa_s);
4910 if (group_added)
4911 wpas_p2p_group_delete(group, 1);
4912 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Fall back to GO Negotiation");
4913 wpas_p2p_connect(wpa_s, wpa_s->pending_join_dev_addr, wpa_s->p2p_pin,
4914 wpa_s->p2p_wps_method, wpa_s->p2p_persistent_group, 0,
4915 0, 0, wpa_s->p2p_go_intent, wpa_s->p2p_connect_freq,
4916 wpa_s->p2p_persistent_id,
4917 wpa_s->p2p_pd_before_go_neg);
4918}
4919
4920
4921int wpas_p2p_scan_no_go_seen(struct wpa_supplicant *wpa_s)
4922{
4923 if (!wpa_s->p2p_fallback_to_go_neg ||
4924 wpa_s->p2p_in_provisioning <= 5)
4925 return 0;
4926
4927 if (wpas_p2p_peer_go(wpa_s, wpa_s->pending_join_dev_addr) > 0)
4928 return 0; /* peer operating as a GO */
4929
4930 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: GO not found for p2p_connect-auto - "
4931 "fallback to GO Negotiation");
4932 wpas_p2p_fallback_to_go_neg(wpa_s, 1);
4933
4934 return 1;
4935}
4936
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08004937#ifdef ANDROID_P2P
4938int wpas_p2p_handle_frequency_conflicts(struct wpa_supplicant *wpa_s, int freq)
4939{
4940 struct wpa_supplicant *iface = NULL;
4941 struct p2p_data *p2p = wpa_s->global->p2p;
4942
4943 for (iface = wpa_s->global->ifaces; iface; iface = iface->next) {
4944 if((iface->p2p_group_interface) && (iface->current_ssid) &&
4945 (iface->current_ssid->frequency != freq)) {
4946
4947 if (iface->p2p_group_interface == P2P_GROUP_INTERFACE_GO) {
4948 /* Try to see whether we can move the GO. If it
4949 * is not possible, remove the GO interface
4950 */
4951 if(wpa_drv_switch_channel(iface, freq) == 0) {
4952 wpa_printf(MSG_ERROR, "P2P: GO Moved to freq(%d)", freq);
4953 iface->current_ssid->frequency = freq;
4954 continue;
4955 }
4956 }
4957
4958 /* If GO cannot be moved or if the conflicting interface is a
4959 * P2P Client, remove the interface depending up on the connection
4960 * priority */
Dmitry Shmidt687922c2012-03-26 14:02:32 -07004961 if(!wpas_is_p2p_prioritized(wpa_s)) {
4962 /* STA connection has priority over existing
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08004963 * P2P connection. So remove the interface */
Dmitry Shmidt687922c2012-03-26 14:02:32 -07004964 wpa_printf(MSG_DEBUG, "P2P: Removing P2P connection due to Single channel"
4965 "concurrent mode frequency conflict");
4966 iface->removal_reason = P2P_GROUP_REMOVAL_FREQ_CONFLICT;
Dmitry Shmidt04949592012-07-19 12:16:46 -07004967 wpas_p2p_group_delete(iface, 0);
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08004968 } else {
4969 /* Existing connection has the priority. Disable the newly
4970 * selected network and let the application know about it.
4971 */
4972 return -1;
4973 }
4974 }
4975 }
4976 return 0;
4977}
4978#endif