blob: fad8bd222a09a6f967ed51027efafefdd98f4f1a [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 Shmidtb5e8f062012-08-08 10:56:33 -0700176 case P2P_SCAN_SPECIFIC:
177 social_channels[0] = freq;
178 social_channels[1] = 0;
179 params.freqs = social_channels;
180 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700181 case P2P_SCAN_SOCIAL_PLUS_ONE:
182 social_channels[3] = freq;
183 params.freqs = social_channels;
184 break;
185 }
186
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800187 ret = wpa_drv_scan(wpa_s, &params);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700188
189 wpabuf_free(ies);
190
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800191 if (ret) {
Dmitry Shmidt04949592012-07-19 12:16:46 -0700192 if (wpa_s->scanning ||
193 wpa_s->scan_res_handler == wpas_p2p_scan_res_handler) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800194 wpa_s->p2p_cb_on_scan_complete = 1;
195 ret = 1;
196 }
Dmitry Shmidt04949592012-07-19 12:16:46 -0700197 } else
198 wpa_s->scan_res_handler = wpas_p2p_scan_res_handler;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800199
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700200 return ret;
201}
202
203
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700204static enum wpa_driver_if_type wpas_p2p_if_type(int p2p_group_interface)
205{
206 switch (p2p_group_interface) {
207 case P2P_GROUP_INTERFACE_PENDING:
208 return WPA_IF_P2P_GROUP;
209 case P2P_GROUP_INTERFACE_GO:
210 return WPA_IF_P2P_GO;
211 case P2P_GROUP_INTERFACE_CLIENT:
212 return WPA_IF_P2P_CLIENT;
213 }
214
215 return WPA_IF_P2P_GROUP;
216}
217
218
219static struct wpa_supplicant * wpas_get_p2p_group(struct wpa_supplicant *wpa_s,
220 const u8 *ssid,
221 size_t ssid_len, int *go)
222{
223 struct wpa_ssid *s;
224
225 for (wpa_s = wpa_s->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
226 for (s = wpa_s->conf->ssid; s; s = s->next) {
227 if (s->disabled != 0 || !s->p2p_group ||
228 s->ssid_len != ssid_len ||
229 os_memcmp(ssid, s->ssid, ssid_len) != 0)
230 continue;
231 if (s->mode == WPAS_MODE_P2P_GO &&
232 s != wpa_s->current_ssid)
233 continue;
234 if (go)
235 *go = s->mode == WPAS_MODE_P2P_GO;
236 return wpa_s;
237 }
238 }
239
240 return NULL;
241}
242
243
Dmitry Shmidt04949592012-07-19 12:16:46 -0700244static void wpas_p2p_group_delete(struct wpa_supplicant *wpa_s, int silent)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700245{
246 struct wpa_ssid *ssid;
247 char *gtype;
248 const char *reason;
249
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700250 ssid = wpa_s->current_ssid;
251 if (ssid == NULL) {
252 /*
253 * The current SSID was not known, but there may still be a
254 * pending P2P group interface waiting for provisioning.
255 */
256 ssid = wpa_s->conf->ssid;
257 while (ssid) {
258 if (ssid->p2p_group &&
259 (ssid->mode == WPAS_MODE_P2P_GROUP_FORMATION ||
260 (ssid->key_mgmt & WPA_KEY_MGMT_WPS)))
261 break;
262 ssid = ssid->next;
263 }
264 }
265 if (wpa_s->p2p_group_interface == P2P_GROUP_INTERFACE_GO)
266 gtype = "GO";
267 else if (wpa_s->p2p_group_interface == P2P_GROUP_INTERFACE_CLIENT ||
268 (ssid && ssid->mode == WPAS_MODE_INFRA)) {
269 wpa_s->reassociate = 0;
270 wpa_s->disconnected = 1;
271 wpa_supplicant_deauthenticate(wpa_s,
272 WLAN_REASON_DEAUTH_LEAVING);
273 gtype = "client";
274 } else
275 gtype = "GO";
276 if (wpa_s->cross_connect_in_use) {
277 wpa_s->cross_connect_in_use = 0;
278 wpa_msg(wpa_s->parent, MSG_INFO,
279 P2P_EVENT_CROSS_CONNECT_DISABLE "%s %s",
280 wpa_s->ifname, wpa_s->cross_connect_uplink);
281 }
282 switch (wpa_s->removal_reason) {
283 case P2P_GROUP_REMOVAL_REQUESTED:
284 reason = " reason=REQUESTED";
285 break;
286 case P2P_GROUP_REMOVAL_IDLE_TIMEOUT:
287 reason = " reason=IDLE";
288 break;
289 case P2P_GROUP_REMOVAL_UNAVAILABLE:
290 reason = " reason=UNAVAILABLE";
291 break;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700292 case P2P_GROUP_REMOVAL_GO_ENDING_SESSION:
293 reason = " reason=GO_ENDING_SESSION";
294 break;
Dmitry Shmidt687922c2012-03-26 14:02:32 -0700295#ifdef ANDROID_P2P
296 case P2P_GROUP_REMOVAL_FREQ_CONFLICT:
297 reason = " reason=FREQ_CONFLICT";
298 break;
299#endif
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700300 default:
301 reason = "";
302 break;
303 }
Dmitry Shmidt04949592012-07-19 12:16:46 -0700304 if (!silent) {
305 wpa_msg(wpa_s->parent, MSG_INFO,
306 P2P_EVENT_GROUP_REMOVED "%s %s%s",
307 wpa_s->ifname, gtype, reason);
308 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700309
Dmitry Shmidt04949592012-07-19 12:16:46 -0700310 if (eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL) > 0)
311 wpa_printf(MSG_DEBUG, "P2P: Cancelled P2P group idle timeout");
312
313 if (!silent && ssid)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700314 wpas_notify_p2p_group_removed(wpa_s, ssid, gtype);
315
316 if (wpa_s->p2p_group_interface != NOT_P2P_GROUP_INTERFACE) {
317 struct wpa_global *global;
318 char *ifname;
319 enum wpa_driver_if_type type;
320 wpa_printf(MSG_DEBUG, "P2P: Remove group interface %s",
321 wpa_s->ifname);
322 global = wpa_s->global;
323 ifname = os_strdup(wpa_s->ifname);
324 type = wpas_p2p_if_type(wpa_s->p2p_group_interface);
Dmitry Shmidte15c7b52011-08-03 15:04:35 -0700325 wpa_supplicant_remove_iface(wpa_s->global, wpa_s, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700326 wpa_s = global->ifaces;
327 if (wpa_s && ifname)
328 wpa_drv_if_remove(wpa_s, type, ifname);
329 os_free(ifname);
330 return;
331 }
332
333 wpa_printf(MSG_DEBUG, "P2P: Remove temporary group network");
334 if (ssid && (ssid->p2p_group ||
335 ssid->mode == WPAS_MODE_P2P_GROUP_FORMATION ||
336 (ssid->key_mgmt & WPA_KEY_MGMT_WPS))) {
337 int id = ssid->id;
Jouni Malinen75ecf522011-06-27 15:19:46 -0700338 if (ssid == wpa_s->current_ssid) {
339 wpa_sm_set_config(wpa_s->wpa, NULL);
340 eapol_sm_notify_config(wpa_s->eapol, NULL, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700341 wpa_s->current_ssid = NULL;
Jouni Malinen75ecf522011-06-27 15:19:46 -0700342 }
343 /*
344 * Networks objects created during any P2P activities are not
345 * exposed out as they might/will confuse certain non-P2P aware
346 * applications since these network objects won't behave like
347 * regular ones.
348 *
349 * Likewise, we don't send out network removed signals for such
350 * network objects.
351 */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700352 wpa_config_remove_network(wpa_s->conf, id);
353 wpa_supplicant_clear_status(wpa_s);
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -0800354 wpa_supplicant_cancel_sched_scan(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700355 } else {
356 wpa_printf(MSG_DEBUG, "P2P: Temporary group network not "
357 "found");
358 }
Dmitry Shmidt04949592012-07-19 12:16:46 -0700359 if (wpa_s->ap_iface)
360 wpa_supplicant_ap_deinit(wpa_s);
361 else
362 wpa_drv_deinit_p2p_cli(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700363}
364
365
366static int wpas_p2p_persistent_group(struct wpa_supplicant *wpa_s,
367 u8 *go_dev_addr,
368 const u8 *ssid, size_t ssid_len)
369{
370 struct wpa_bss *bss;
371 const u8 *bssid;
372 struct wpabuf *p2p;
373 u8 group_capab;
374 const u8 *addr;
375
376 if (wpa_s->go_params)
377 bssid = wpa_s->go_params->peer_interface_addr;
378 else
379 bssid = wpa_s->bssid;
380
381 bss = wpa_bss_get(wpa_s, bssid, ssid, ssid_len);
382 if (bss == NULL) {
383 u8 iface_addr[ETH_ALEN];
384 if (p2p_get_interface_addr(wpa_s->global->p2p, bssid,
385 iface_addr) == 0)
386 bss = wpa_bss_get(wpa_s, iface_addr, ssid, ssid_len);
387 }
388 if (bss == NULL) {
389 wpa_printf(MSG_DEBUG, "P2P: Could not figure out whether "
390 "group is persistent - BSS " MACSTR " not found",
391 MAC2STR(bssid));
392 return 0;
393 }
394
395 p2p = wpa_bss_get_vendor_ie_multi(bss, P2P_IE_VENDOR_TYPE);
396 if (p2p == NULL) {
397 wpa_printf(MSG_DEBUG, "P2P: Could not figure out whether "
398 "group is persistent - BSS " MACSTR
399 " did not include P2P IE", MAC2STR(bssid));
400 wpa_hexdump(MSG_DEBUG, "P2P: Probe Response IEs",
401 (u8 *) (bss + 1), bss->ie_len);
402 wpa_hexdump(MSG_DEBUG, "P2P: Beacon IEs",
403 ((u8 *) bss + 1) + bss->ie_len,
404 bss->beacon_ie_len);
405 return 0;
406 }
407
408 group_capab = p2p_get_group_capab(p2p);
409 addr = p2p_get_go_dev_addr(p2p);
410 wpa_printf(MSG_DEBUG, "P2P: Checking whether group is persistent: "
411 "group_capab=0x%x", group_capab);
412 if (addr) {
413 os_memcpy(go_dev_addr, addr, ETH_ALEN);
414 wpa_printf(MSG_DEBUG, "P2P: GO Device Address " MACSTR,
415 MAC2STR(addr));
416 } else
417 os_memset(go_dev_addr, 0, ETH_ALEN);
418 wpabuf_free(p2p);
419
420 wpa_printf(MSG_DEBUG, "P2P: BSS " MACSTR " group_capab=0x%x "
421 "go_dev_addr=" MACSTR,
422 MAC2STR(bssid), group_capab, MAC2STR(go_dev_addr));
423
424 return group_capab & P2P_GROUP_CAPAB_PERSISTENT_GROUP;
425}
426
427
Jouni Malinen75ecf522011-06-27 15:19:46 -0700428static int wpas_p2p_store_persistent_group(struct wpa_supplicant *wpa_s,
429 struct wpa_ssid *ssid,
430 const u8 *go_dev_addr)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700431{
432 struct wpa_ssid *s;
433 int changed = 0;
434
435 wpa_printf(MSG_DEBUG, "P2P: Storing credentials for a persistent "
436 "group (GO Dev Addr " MACSTR ")", MAC2STR(go_dev_addr));
437 for (s = wpa_s->conf->ssid; s; s = s->next) {
438 if (s->disabled == 2 &&
439 os_memcmp(go_dev_addr, s->bssid, ETH_ALEN) == 0 &&
440 s->ssid_len == ssid->ssid_len &&
441 os_memcmp(ssid->ssid, s->ssid, ssid->ssid_len) == 0)
442 break;
443 }
444
445 if (s) {
446 wpa_printf(MSG_DEBUG, "P2P: Update existing persistent group "
447 "entry");
448 if (ssid->passphrase && !s->passphrase)
449 changed = 1;
450 else if (ssid->passphrase && s->passphrase &&
451 os_strcmp(ssid->passphrase, s->passphrase) != 0)
452 changed = 1;
453 } else {
454 wpa_printf(MSG_DEBUG, "P2P: Create a new persistent group "
455 "entry");
456 changed = 1;
457 s = wpa_config_add_network(wpa_s->conf);
458 if (s == NULL)
Jouni Malinen75ecf522011-06-27 15:19:46 -0700459 return -1;
460
461 /*
462 * Instead of network_added we emit persistent_group_added
463 * notification. Also to keep the defense checks in
464 * persistent_group obj registration method, we set the
465 * relevant flags in s to designate it as a persistent group.
466 */
467 s->p2p_group = 1;
468 s->p2p_persistent_group = 1;
469 wpas_notify_persistent_group_added(wpa_s, s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700470 wpa_config_set_network_defaults(s);
471 }
472
473 s->p2p_group = 1;
474 s->p2p_persistent_group = 1;
475 s->disabled = 2;
476 s->bssid_set = 1;
477 os_memcpy(s->bssid, go_dev_addr, ETH_ALEN);
478 s->mode = ssid->mode;
479 s->auth_alg = WPA_AUTH_ALG_OPEN;
480 s->key_mgmt = WPA_KEY_MGMT_PSK;
481 s->proto = WPA_PROTO_RSN;
482 s->pairwise_cipher = WPA_CIPHER_CCMP;
483 s->export_keys = 1;
484 if (ssid->passphrase) {
485 os_free(s->passphrase);
486 s->passphrase = os_strdup(ssid->passphrase);
487 }
488 if (ssid->psk_set) {
489 s->psk_set = 1;
490 os_memcpy(s->psk, ssid->psk, 32);
491 }
492 if (s->passphrase && !s->psk_set)
493 wpa_config_update_psk(s);
494 if (s->ssid == NULL || s->ssid_len < ssid->ssid_len) {
495 os_free(s->ssid);
496 s->ssid = os_malloc(ssid->ssid_len);
497 }
498 if (s->ssid) {
499 s->ssid_len = ssid->ssid_len;
500 os_memcpy(s->ssid, ssid->ssid, s->ssid_len);
501 }
502
503#ifndef CONFIG_NO_CONFIG_WRITE
504 if (changed && wpa_s->conf->update_config &&
505 wpa_config_write(wpa_s->confname, wpa_s->conf)) {
506 wpa_printf(MSG_DEBUG, "P2P: Failed to update configuration");
507 }
508#endif /* CONFIG_NO_CONFIG_WRITE */
Jouni Malinen75ecf522011-06-27 15:19:46 -0700509
510 return s->id;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700511}
512
513
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800514static void wpas_p2p_add_persistent_group_client(struct wpa_supplicant *wpa_s,
515 const u8 *addr)
516{
517 struct wpa_ssid *ssid, *s;
518 u8 *n;
519 size_t i;
520
521 ssid = wpa_s->current_ssid;
522 if (ssid == NULL || ssid->mode != WPAS_MODE_P2P_GO ||
523 !ssid->p2p_persistent_group)
524 return;
525
526 for (s = wpa_s->parent->conf->ssid; s; s = s->next) {
527 if (s->disabled != 2 || s->mode != WPAS_MODE_P2P_GO)
528 continue;
529
530 if (s->ssid_len == ssid->ssid_len &&
531 os_memcmp(s->ssid, ssid->ssid, s->ssid_len) == 0)
532 break;
533 }
534
535 if (s == NULL)
536 return;
537
538 for (i = 0; s->p2p_client_list && i < s->num_p2p_clients; i++) {
539 if (os_memcmp(s->p2p_client_list + i * ETH_ALEN, addr,
540 ETH_ALEN) == 0)
541 return; /* already in list */
542 }
543
544 n = os_realloc(s->p2p_client_list,
545 (s->num_p2p_clients + 1) * ETH_ALEN);
546 if (n == NULL)
547 return;
548 os_memcpy(n + s->num_p2p_clients * ETH_ALEN, addr, ETH_ALEN);
549 s->p2p_client_list = n;
550 s->num_p2p_clients++;
551
552#ifndef CONFIG_NO_CONFIG_WRITE
553 if (wpa_s->parent->conf->update_config &&
554 wpa_config_write(wpa_s->parent->confname, wpa_s->parent->conf))
555 wpa_printf(MSG_DEBUG, "P2P: Failed to update configuration");
556#endif /* CONFIG_NO_CONFIG_WRITE */
557}
558
559
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700560static void wpas_group_formation_completed(struct wpa_supplicant *wpa_s,
561 int success)
562{
563 struct wpa_ssid *ssid;
564 const char *ssid_txt;
565 int client;
566 int persistent;
567 u8 go_dev_addr[ETH_ALEN];
Jouni Malinen75ecf522011-06-27 15:19:46 -0700568 int network_id = -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700569
570 /*
571 * This callback is likely called for the main interface. Update wpa_s
572 * to use the group interface if a new interface was created for the
573 * group.
574 */
575 if (wpa_s->global->p2p_group_formation)
576 wpa_s = wpa_s->global->p2p_group_formation;
577 wpa_s->global->p2p_group_formation = NULL;
578 wpa_s->p2p_in_provisioning = 0;
579
580 if (!success) {
581 wpa_msg(wpa_s->parent, MSG_INFO,
582 P2P_EVENT_GROUP_FORMATION_FAILURE);
Dmitry Shmidt04949592012-07-19 12:16:46 -0700583 wpas_p2p_group_delete(wpa_s, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700584 return;
585 }
586
587 wpa_msg(wpa_s->parent, MSG_INFO, P2P_EVENT_GROUP_FORMATION_SUCCESS);
588
589 ssid = wpa_s->current_ssid;
590 if (ssid && ssid->mode == WPAS_MODE_P2P_GROUP_FORMATION) {
591 ssid->mode = WPAS_MODE_P2P_GO;
592 p2p_group_notif_formation_done(wpa_s->p2p_group);
593 wpa_supplicant_ap_mac_addr_filter(wpa_s, NULL);
594 }
595
596 persistent = 0;
597 if (ssid) {
598 ssid_txt = wpa_ssid_txt(ssid->ssid, ssid->ssid_len);
599 client = ssid->mode == WPAS_MODE_INFRA;
600 if (ssid->mode == WPAS_MODE_P2P_GO) {
601 persistent = ssid->p2p_persistent_group;
Dmitry Shmidt497c1d52011-07-21 15:19:46 -0700602 os_memcpy(go_dev_addr, wpa_s->global->p2p_dev_addr,
603 ETH_ALEN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700604 } else
605 persistent = wpas_p2p_persistent_group(wpa_s,
606 go_dev_addr,
607 ssid->ssid,
608 ssid->ssid_len);
609 } else {
610 ssid_txt = "";
611 client = wpa_s->p2p_group_interface ==
612 P2P_GROUP_INTERFACE_CLIENT;
613 os_memset(go_dev_addr, 0, ETH_ALEN);
614 }
615
616 wpa_s->show_group_started = 0;
617 if (client) {
618 /*
619 * Indicate event only after successfully completed 4-way
620 * handshake, i.e., when the interface is ready for data
621 * packets.
622 */
623 wpa_s->show_group_started = 1;
624 } else if (ssid && ssid->passphrase == NULL && ssid->psk_set) {
625 char psk[65];
626 wpa_snprintf_hex(psk, sizeof(psk), ssid->psk, 32);
627 wpa_msg(wpa_s->parent, MSG_INFO, P2P_EVENT_GROUP_STARTED
628 "%s GO ssid=\"%s\" freq=%d psk=%s go_dev_addr=" MACSTR
629 "%s",
630 wpa_s->ifname, ssid_txt, ssid->frequency, psk,
631 MAC2STR(go_dev_addr),
632 persistent ? " [PERSISTENT]" : "");
633 wpas_p2p_cross_connect_setup(wpa_s);
634 wpas_p2p_set_group_idle_timeout(wpa_s);
635 } else {
636 wpa_msg(wpa_s->parent, MSG_INFO, P2P_EVENT_GROUP_STARTED
637 "%s GO ssid=\"%s\" freq=%d passphrase=\"%s\" "
638 "go_dev_addr=" MACSTR "%s",
639 wpa_s->ifname, ssid_txt, ssid ? ssid->frequency : 0,
640 ssid && ssid->passphrase ? ssid->passphrase : "",
641 MAC2STR(go_dev_addr),
642 persistent ? " [PERSISTENT]" : "");
643 wpas_p2p_cross_connect_setup(wpa_s);
644 wpas_p2p_set_group_idle_timeout(wpa_s);
645 }
646
647 if (persistent)
Jouni Malinen75ecf522011-06-27 15:19:46 -0700648 network_id = wpas_p2p_store_persistent_group(wpa_s->parent,
649 ssid, go_dev_addr);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800650 if (network_id < 0 && ssid)
Jouni Malinen75ecf522011-06-27 15:19:46 -0700651 network_id = ssid->id;
652 if (!client)
653 wpas_notify_p2p_group_started(wpa_s, ssid, network_id, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700654}
655
656
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800657static void wpas_p2p_send_action_tx_status(struct wpa_supplicant *wpa_s,
658 unsigned int freq,
659 const u8 *dst, const u8 *src,
660 const u8 *bssid,
661 const u8 *data, size_t data_len,
662 enum offchannel_send_action_result
663 result)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700664{
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800665 enum p2p_send_action_result res = P2P_SEND_ACTION_SUCCESS;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700666
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700667 if (wpa_s->global->p2p == NULL || wpa_s->global->p2p_disabled)
668 return;
669 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
670 return;
671
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800672 switch (result) {
673 case OFFCHANNEL_SEND_ACTION_SUCCESS:
674 res = P2P_SEND_ACTION_SUCCESS;
675 break;
676 case OFFCHANNEL_SEND_ACTION_NO_ACK:
677 res = P2P_SEND_ACTION_NO_ACK;
678 break;
679 case OFFCHANNEL_SEND_ACTION_FAILED:
680 res = P2P_SEND_ACTION_FAILED;
681 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700682 }
683
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800684 p2p_send_action_cb(wpa_s->global->p2p, freq, dst, src, bssid, res);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700685
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -0800686 if (result != OFFCHANNEL_SEND_ACTION_SUCCESS &&
687 wpa_s->pending_pd_before_join &&
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800688 (os_memcmp(dst, wpa_s->pending_join_dev_addr, ETH_ALEN) == 0 ||
689 os_memcmp(dst, wpa_s->pending_join_iface_addr, ETH_ALEN) == 0)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700690 wpa_s->pending_pd_before_join = 0;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700691 if (wpa_s->p2p_fallback_to_go_neg) {
692 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: No ACK for PD Req "
693 "during p2p_connect-auto");
694 wpas_p2p_fallback_to_go_neg(wpa_s, 0);
695 return;
696 }
697
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700698 wpa_printf(MSG_DEBUG, "P2P: Starting pending "
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -0800699 "join-existing-group operation (no ACK for PD "
700 "Req)");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700701 wpas_p2p_join_start(wpa_s);
702 }
703}
704
705
706static int wpas_send_action(void *ctx, unsigned int freq, const u8 *dst,
707 const u8 *src, const u8 *bssid, const u8 *buf,
708 size_t len, unsigned int wait_time)
709{
710 struct wpa_supplicant *wpa_s = ctx;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800711 return offchannel_send_action(wpa_s, freq, dst, src, bssid, buf, len,
712 wait_time,
713 wpas_p2p_send_action_tx_status, 1);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700714}
715
716
717static void wpas_send_action_done(void *ctx)
718{
719 struct wpa_supplicant *wpa_s = ctx;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800720 offchannel_send_action_done(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700721}
722
723
724static int wpas_copy_go_neg_results(struct wpa_supplicant *wpa_s,
725 struct p2p_go_neg_results *params)
726{
727 if (wpa_s->go_params == NULL) {
728 wpa_s->go_params = os_malloc(sizeof(*params));
729 if (wpa_s->go_params == NULL)
730 return -1;
731 }
732 os_memcpy(wpa_s->go_params, params, sizeof(*params));
733 return 0;
734}
735
736
737static void wpas_start_wps_enrollee(struct wpa_supplicant *wpa_s,
738 struct p2p_go_neg_results *res)
739{
740 wpa_printf(MSG_DEBUG, "P2P: Start WPS Enrollee for peer " MACSTR,
741 MAC2STR(res->peer_interface_addr));
742 wpa_hexdump_ascii(MSG_DEBUG, "P2P: Start WPS Enrollee for SSID",
743 res->ssid, res->ssid_len);
744 wpa_supplicant_ap_deinit(wpa_s);
745 wpas_copy_go_neg_results(wpa_s, res);
746 if (res->wps_method == WPS_PBC)
747 wpas_wps_start_pbc(wpa_s, res->peer_interface_addr, 1);
748 else {
749 u16 dev_pw_id = DEV_PW_DEFAULT;
750 if (wpa_s->p2p_wps_method == WPS_PIN_KEYPAD)
751 dev_pw_id = DEV_PW_REGISTRAR_SPECIFIED;
752 wpas_wps_start_pin(wpa_s, res->peer_interface_addr,
753 wpa_s->p2p_pin, 1, dev_pw_id);
754 }
755}
756
757
758static void p2p_go_configured(void *ctx, void *data)
759{
760 struct wpa_supplicant *wpa_s = ctx;
761 struct p2p_go_neg_results *params = data;
762 struct wpa_ssid *ssid;
Jouni Malinen75ecf522011-06-27 15:19:46 -0700763 int network_id = -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700764
765 ssid = wpa_s->current_ssid;
766 if (ssid && ssid->mode == WPAS_MODE_P2P_GO) {
767 wpa_printf(MSG_DEBUG, "P2P: Group setup without provisioning");
768 if (wpa_s->global->p2p_group_formation == wpa_s)
769 wpa_s->global->p2p_group_formation = NULL;
770 wpa_msg(wpa_s->parent, MSG_INFO, P2P_EVENT_GROUP_STARTED
771 "%s GO ssid=\"%s\" freq=%d passphrase=\"%s\" "
772 "go_dev_addr=" MACSTR "%s",
773 wpa_s->ifname,
774 wpa_ssid_txt(ssid->ssid, ssid->ssid_len),
775 ssid->frequency,
776 params->passphrase ? params->passphrase : "",
Dmitry Shmidt497c1d52011-07-21 15:19:46 -0700777 MAC2STR(wpa_s->global->p2p_dev_addr),
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700778 params->persistent_group ? " [PERSISTENT]" : "");
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800779
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700780 if (params->persistent_group)
Jouni Malinen75ecf522011-06-27 15:19:46 -0700781 network_id = wpas_p2p_store_persistent_group(
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700782 wpa_s->parent, ssid,
Dmitry Shmidt497c1d52011-07-21 15:19:46 -0700783 wpa_s->global->p2p_dev_addr);
Jouni Malinen75ecf522011-06-27 15:19:46 -0700784 if (network_id < 0)
785 network_id = ssid->id;
786 wpas_notify_p2p_group_started(wpa_s, ssid, network_id, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700787 wpas_p2p_cross_connect_setup(wpa_s);
788 wpas_p2p_set_group_idle_timeout(wpa_s);
789 return;
790 }
791
792 wpa_printf(MSG_DEBUG, "P2P: Setting up WPS for GO provisioning");
793 if (wpa_supplicant_ap_mac_addr_filter(wpa_s,
794 params->peer_interface_addr)) {
795 wpa_printf(MSG_DEBUG, "P2P: Failed to setup MAC address "
796 "filtering");
797 return;
798 }
799 if (params->wps_method == WPS_PBC)
800 wpa_supplicant_ap_wps_pbc(wpa_s, params->peer_interface_addr,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800801 params->peer_device_addr);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700802 else if (wpa_s->p2p_pin[0])
803 wpa_supplicant_ap_wps_pin(wpa_s, params->peer_interface_addr,
804 wpa_s->p2p_pin, NULL, 0);
805 os_free(wpa_s->go_params);
806 wpa_s->go_params = NULL;
807}
808
809
810static void wpas_start_wps_go(struct wpa_supplicant *wpa_s,
811 struct p2p_go_neg_results *params,
812 int group_formation)
813{
814 struct wpa_ssid *ssid;
815
816 if (wpas_copy_go_neg_results(wpa_s, params) < 0)
817 return;
818
819 ssid = wpa_config_add_network(wpa_s->conf);
820 if (ssid == NULL)
821 return;
822
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800823 wpa_s->show_group_started = 0;
824
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700825 wpa_config_set_network_defaults(ssid);
826 ssid->temporary = 1;
827 ssid->p2p_group = 1;
828 ssid->p2p_persistent_group = params->persistent_group;
829 ssid->mode = group_formation ? WPAS_MODE_P2P_GROUP_FORMATION :
830 WPAS_MODE_P2P_GO;
831 ssid->frequency = params->freq;
832 ssid->ssid = os_zalloc(params->ssid_len + 1);
833 if (ssid->ssid) {
834 os_memcpy(ssid->ssid, params->ssid, params->ssid_len);
835 ssid->ssid_len = params->ssid_len;
836 }
837 ssid->auth_alg = WPA_AUTH_ALG_OPEN;
838 ssid->key_mgmt = WPA_KEY_MGMT_PSK;
839 ssid->proto = WPA_PROTO_RSN;
840 ssid->pairwise_cipher = WPA_CIPHER_CCMP;
841 ssid->passphrase = os_strdup(params->passphrase);
842
843 wpa_s->ap_configured_cb = p2p_go_configured;
844 wpa_s->ap_configured_cb_ctx = wpa_s;
845 wpa_s->ap_configured_cb_data = wpa_s->go_params;
Jouni Malinen75ecf522011-06-27 15:19:46 -0700846 wpa_s->connect_without_scan = ssid;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700847 wpa_s->reassociate = 1;
848 wpa_s->disconnected = 0;
849 wpa_supplicant_req_scan(wpa_s, 0, 0);
850}
851
852
853static void wpas_p2p_clone_config(struct wpa_supplicant *dst,
854 const struct wpa_supplicant *src)
855{
856 struct wpa_config *d;
857 const struct wpa_config *s;
858
859 d = dst->conf;
860 s = src->conf;
861
862#define C(n) if (s->n) d->n = os_strdup(s->n)
863 C(device_name);
864 C(manufacturer);
865 C(model_name);
866 C(model_number);
867 C(serial_number);
868 C(config_methods);
869#undef C
870
871 os_memcpy(d->device_type, s->device_type, WPS_DEV_TYPE_LEN);
872 os_memcpy(d->sec_device_type, s->sec_device_type,
873 sizeof(d->sec_device_type));
874 d->num_sec_device_types = s->num_sec_device_types;
875
876 d->p2p_group_idle = s->p2p_group_idle;
877 d->p2p_intra_bss = s->p2p_intra_bss;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800878 d->persistent_reconnect = s->persistent_reconnect;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700879 d->max_num_sta = s->max_num_sta;
880 d->pbc_in_m1 = s->pbc_in_m1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700881}
882
883
884static int wpas_p2p_add_group_interface(struct wpa_supplicant *wpa_s,
885 enum wpa_driver_if_type type)
886{
887 char ifname[120], force_ifname[120];
888
889 if (wpa_s->pending_interface_name[0]) {
890 wpa_printf(MSG_DEBUG, "P2P: Pending virtual interface exists "
891 "- skip creation of a new one");
892 if (is_zero_ether_addr(wpa_s->pending_interface_addr)) {
893 wpa_printf(MSG_DEBUG, "P2P: Pending virtual address "
894 "unknown?! ifname='%s'",
895 wpa_s->pending_interface_name);
896 return -1;
897 }
898 return 0;
899 }
900
901 os_snprintf(ifname, sizeof(ifname), "p2p-%s-%d", wpa_s->ifname,
902 wpa_s->p2p_group_idx);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800903 if (os_strlen(ifname) >= IFNAMSIZ &&
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700904 os_strlen(wpa_s->ifname) < IFNAMSIZ) {
905 /* Try to avoid going over the IFNAMSIZ length limit */
906 os_snprintf(ifname, sizeof(ifname), "p2p-%d",
907 wpa_s->p2p_group_idx);
908 }
909 force_ifname[0] = '\0';
910
911 wpa_printf(MSG_DEBUG, "P2P: Create a new interface %s for the group",
912 ifname);
913 wpa_s->p2p_group_idx++;
914
915 wpa_s->pending_interface_type = type;
916 if (wpa_drv_if_add(wpa_s, type, ifname, NULL, NULL, force_ifname,
917 wpa_s->pending_interface_addr, NULL) < 0) {
918 wpa_printf(MSG_ERROR, "P2P: Failed to create new group "
919 "interface");
920 return -1;
921 }
922
923 if (force_ifname[0]) {
924 wpa_printf(MSG_DEBUG, "P2P: Driver forced interface name %s",
925 force_ifname);
926 os_strlcpy(wpa_s->pending_interface_name, force_ifname,
927 sizeof(wpa_s->pending_interface_name));
928 } else
929 os_strlcpy(wpa_s->pending_interface_name, ifname,
930 sizeof(wpa_s->pending_interface_name));
931 wpa_printf(MSG_DEBUG, "P2P: Created pending virtual interface %s addr "
932 MACSTR, wpa_s->pending_interface_name,
933 MAC2STR(wpa_s->pending_interface_addr));
934
935 return 0;
936}
937
938
939static void wpas_p2p_remove_pending_group_interface(
940 struct wpa_supplicant *wpa_s)
941{
942 if (!wpa_s->pending_interface_name[0] ||
943 is_zero_ether_addr(wpa_s->pending_interface_addr))
944 return; /* No pending virtual interface */
945
946 wpa_printf(MSG_DEBUG, "P2P: Removing pending group interface %s",
947 wpa_s->pending_interface_name);
948 wpa_drv_if_remove(wpa_s, wpa_s->pending_interface_type,
949 wpa_s->pending_interface_name);
950 os_memset(wpa_s->pending_interface_addr, 0, ETH_ALEN);
951 wpa_s->pending_interface_name[0] = '\0';
952}
953
954
955static struct wpa_supplicant *
956wpas_p2p_init_group_interface(struct wpa_supplicant *wpa_s, int go)
957{
958 struct wpa_interface iface;
959 struct wpa_supplicant *group_wpa_s;
960
961 if (!wpa_s->pending_interface_name[0]) {
962 wpa_printf(MSG_ERROR, "P2P: No pending group interface");
963 if (!wpas_p2p_create_iface(wpa_s))
964 return NULL;
965 /*
966 * Something has forced us to remove the pending interface; try
967 * to create a new one and hope for the best that we will get
968 * the same local address.
969 */
970 if (wpas_p2p_add_group_interface(wpa_s, go ? WPA_IF_P2P_GO :
971 WPA_IF_P2P_CLIENT) < 0)
972 return NULL;
973 }
974
975 os_memset(&iface, 0, sizeof(iface));
976 iface.ifname = wpa_s->pending_interface_name;
977 iface.driver = wpa_s->driver->name;
978 iface.ctrl_interface = wpa_s->conf->ctrl_interface;
979 iface.driver_param = wpa_s->conf->driver_param;
980 group_wpa_s = wpa_supplicant_add_iface(wpa_s->global, &iface);
981 if (group_wpa_s == NULL) {
982 wpa_printf(MSG_ERROR, "P2P: Failed to create new "
983 "wpa_supplicant interface");
984 return NULL;
985 }
986 wpa_s->pending_interface_name[0] = '\0';
987 group_wpa_s->parent = wpa_s;
988 group_wpa_s->p2p_group_interface = go ? P2P_GROUP_INTERFACE_GO :
989 P2P_GROUP_INTERFACE_CLIENT;
990 wpa_s->global->p2p_group_formation = group_wpa_s;
991
992 wpas_p2p_clone_config(group_wpa_s, wpa_s);
993
994 return group_wpa_s;
995}
996
997
998static void wpas_p2p_group_formation_timeout(void *eloop_ctx,
999 void *timeout_ctx)
1000{
1001 struct wpa_supplicant *wpa_s = eloop_ctx;
1002 wpa_printf(MSG_DEBUG, "P2P: Group Formation timed out");
1003 if (wpa_s->global->p2p)
1004 p2p_group_formation_failed(wpa_s->global->p2p);
1005 else if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
1006 wpa_drv_p2p_group_formation_failed(wpa_s);
1007 wpas_group_formation_completed(wpa_s, 0);
1008}
1009
1010
1011void wpas_go_neg_completed(void *ctx, struct p2p_go_neg_results *res)
1012{
1013 struct wpa_supplicant *wpa_s = ctx;
1014
1015 if (wpa_s->off_channel_freq || wpa_s->roc_waiting_drv_freq) {
1016 wpa_drv_cancel_remain_on_channel(wpa_s);
1017 wpa_s->off_channel_freq = 0;
1018 wpa_s->roc_waiting_drv_freq = 0;
1019 }
1020
1021 if (res->status) {
1022 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_GO_NEG_FAILURE "status=%d",
1023 res->status);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001024 wpas_notify_p2p_go_neg_completed(wpa_s, res);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001025 wpas_p2p_remove_pending_group_interface(wpa_s);
1026 return;
1027 }
1028
1029 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_GO_NEG_SUCCESS);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001030 wpas_notify_p2p_go_neg_completed(wpa_s, res);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001031
Dmitry Shmidt04949592012-07-19 12:16:46 -07001032 if (res->role_go && wpa_s->p2p_persistent_id >= 0) {
1033 struct wpa_ssid *ssid;
1034 ssid = wpa_config_get_network(wpa_s->conf,
1035 wpa_s->p2p_persistent_id);
1036 if (ssid && ssid->disabled == 2 &&
1037 ssid->mode == WPAS_MODE_P2P_GO && ssid->passphrase) {
1038 size_t len = os_strlen(ssid->passphrase);
1039 wpa_printf(MSG_DEBUG, "P2P: Override passphrase based "
1040 "on requested persistent group");
1041 os_memcpy(res->passphrase, ssid->passphrase, len);
1042 res->passphrase[len] = '\0';
1043 }
1044 }
1045
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001046 if (wpa_s->create_p2p_iface) {
1047 struct wpa_supplicant *group_wpa_s =
1048 wpas_p2p_init_group_interface(wpa_s, res->role_go);
1049 if (group_wpa_s == NULL) {
1050 wpas_p2p_remove_pending_group_interface(wpa_s);
1051 return;
1052 }
1053 if (group_wpa_s != wpa_s) {
1054 os_memcpy(group_wpa_s->p2p_pin, wpa_s->p2p_pin,
1055 sizeof(group_wpa_s->p2p_pin));
1056 group_wpa_s->p2p_wps_method = wpa_s->p2p_wps_method;
1057 }
1058 os_memset(wpa_s->pending_interface_addr, 0, ETH_ALEN);
1059 wpa_s->pending_interface_name[0] = '\0';
1060 group_wpa_s->p2p_in_provisioning = 1;
1061
1062 if (res->role_go)
1063 wpas_start_wps_go(group_wpa_s, res, 1);
1064 else
1065 wpas_start_wps_enrollee(group_wpa_s, res);
1066 } else {
1067 wpa_s->p2p_in_provisioning = 1;
1068 wpa_s->global->p2p_group_formation = wpa_s;
1069
1070 if (res->role_go)
1071 wpas_start_wps_go(wpa_s, res, 1);
1072 else
1073 wpas_start_wps_enrollee(ctx, res);
1074 }
1075
1076 wpa_s->p2p_long_listen = 0;
1077 eloop_cancel_timeout(wpas_p2p_long_listen_timeout, wpa_s, NULL);
1078
1079 eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s, NULL);
1080 eloop_register_timeout(15 + res->peer_config_timeout / 100,
1081 (res->peer_config_timeout % 100) * 10000,
1082 wpas_p2p_group_formation_timeout, wpa_s, NULL);
1083}
1084
1085
1086void wpas_go_neg_req_rx(void *ctx, const u8 *src, u16 dev_passwd_id)
1087{
1088 struct wpa_supplicant *wpa_s = ctx;
1089 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_GO_NEG_REQUEST MACSTR
1090 " dev_passwd_id=%u", MAC2STR(src), dev_passwd_id);
1091
1092 wpas_notify_p2p_go_neg_req(wpa_s, src, dev_passwd_id);
1093}
1094
1095
1096void wpas_dev_found(void *ctx, const u8 *addr,
1097 const struct p2p_peer_info *info,
1098 int new_device)
1099{
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001100#ifndef CONFIG_NO_STDOUT_DEBUG
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001101 struct wpa_supplicant *wpa_s = ctx;
1102 char devtype[WPS_DEV_TYPE_BUFSIZE];
1103
1104 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_DEVICE_FOUND MACSTR
1105 " p2p_dev_addr=" MACSTR
1106 " pri_dev_type=%s name='%s' config_methods=0x%x "
1107 "dev_capab=0x%x group_capab=0x%x",
1108 MAC2STR(addr), MAC2STR(info->p2p_device_addr),
1109 wps_dev_type_bin2str(info->pri_dev_type, devtype,
1110 sizeof(devtype)),
1111 info->device_name, info->config_methods,
1112 info->dev_capab, info->group_capab);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001113#endif /* CONFIG_NO_STDOUT_DEBUG */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001114
1115 wpas_notify_p2p_device_found(ctx, info->p2p_device_addr, new_device);
1116}
1117
1118
1119static void wpas_dev_lost(void *ctx, const u8 *dev_addr)
1120{
1121 struct wpa_supplicant *wpa_s = ctx;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001122
Dmitry Shmidt497c1d52011-07-21 15:19:46 -07001123 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_DEVICE_LOST
1124 "p2p_dev_addr=" MACSTR, MAC2STR(dev_addr));
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001125
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001126 wpas_notify_p2p_device_lost(wpa_s, dev_addr);
1127}
1128
1129
1130static int wpas_start_listen(void *ctx, unsigned int freq,
1131 unsigned int duration,
1132 const struct wpabuf *probe_resp_ie)
1133{
1134 struct wpa_supplicant *wpa_s = ctx;
1135
1136 wpa_drv_set_ap_wps_ie(wpa_s, NULL, probe_resp_ie, NULL);
1137
1138 if (wpa_drv_probe_req_report(wpa_s, 1) < 0) {
1139 wpa_printf(MSG_DEBUG, "P2P: Failed to request the driver to "
1140 "report received Probe Request frames");
1141 return -1;
1142 }
1143
1144 wpa_s->pending_listen_freq = freq;
1145 wpa_s->pending_listen_duration = duration;
1146
1147 if (wpa_drv_remain_on_channel(wpa_s, freq, duration) < 0) {
1148 wpa_printf(MSG_DEBUG, "P2P: Failed to request the driver "
1149 "to remain on channel (%u MHz) for Listen "
1150 "state", freq);
1151 wpa_s->pending_listen_freq = 0;
1152 return -1;
1153 }
1154 wpa_s->off_channel_freq = 0;
1155 wpa_s->roc_waiting_drv_freq = freq;
1156
1157 return 0;
1158}
1159
1160
1161static void wpas_stop_listen(void *ctx)
1162{
1163 struct wpa_supplicant *wpa_s = ctx;
1164 if (wpa_s->off_channel_freq || wpa_s->roc_waiting_drv_freq) {
1165 wpa_drv_cancel_remain_on_channel(wpa_s);
1166 wpa_s->off_channel_freq = 0;
1167 wpa_s->roc_waiting_drv_freq = 0;
1168 }
1169 wpa_drv_set_ap_wps_ie(wpa_s, NULL, NULL, NULL);
1170 wpa_drv_probe_req_report(wpa_s, 0);
1171}
1172
1173
1174static int wpas_send_probe_resp(void *ctx, const struct wpabuf *buf)
1175{
1176 struct wpa_supplicant *wpa_s = ctx;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001177 return wpa_drv_send_mlme(wpa_s, wpabuf_head(buf), wpabuf_len(buf), 1);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001178}
1179
1180
1181static struct p2p_srv_bonjour *
1182wpas_p2p_service_get_bonjour(struct wpa_supplicant *wpa_s,
1183 const struct wpabuf *query)
1184{
1185 struct p2p_srv_bonjour *bsrv;
1186 size_t len;
1187
1188 len = wpabuf_len(query);
1189 dl_list_for_each(bsrv, &wpa_s->global->p2p_srv_bonjour,
1190 struct p2p_srv_bonjour, list) {
1191 if (len == wpabuf_len(bsrv->query) &&
1192 os_memcmp(wpabuf_head(query), wpabuf_head(bsrv->query),
1193 len) == 0)
1194 return bsrv;
1195 }
1196 return NULL;
1197}
1198
1199
1200static struct p2p_srv_upnp *
1201wpas_p2p_service_get_upnp(struct wpa_supplicant *wpa_s, u8 version,
1202 const char *service)
1203{
1204 struct p2p_srv_upnp *usrv;
1205
1206 dl_list_for_each(usrv, &wpa_s->global->p2p_srv_upnp,
1207 struct p2p_srv_upnp, list) {
1208 if (version == usrv->version &&
1209 os_strcmp(service, usrv->service) == 0)
1210 return usrv;
1211 }
1212 return NULL;
1213}
1214
1215
1216static void wpas_sd_add_proto_not_avail(struct wpabuf *resp, u8 srv_proto,
1217 u8 srv_trans_id)
1218{
1219 u8 *len_pos;
1220
1221 if (wpabuf_tailroom(resp) < 5)
1222 return;
1223
1224 /* Length (to be filled) */
1225 len_pos = wpabuf_put(resp, 2);
1226 wpabuf_put_u8(resp, srv_proto);
1227 wpabuf_put_u8(resp, srv_trans_id);
1228 /* Status Code */
1229 wpabuf_put_u8(resp, P2P_SD_PROTO_NOT_AVAILABLE);
1230 /* Response Data: empty */
1231 WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos - 2);
1232}
1233
1234
1235static void wpas_sd_all_bonjour(struct wpa_supplicant *wpa_s,
1236 struct wpabuf *resp, u8 srv_trans_id)
1237{
1238 struct p2p_srv_bonjour *bsrv;
1239 u8 *len_pos;
1240
1241 wpa_printf(MSG_DEBUG, "P2P: SD Request for all Bonjour services");
1242
1243 if (dl_list_empty(&wpa_s->global->p2p_srv_bonjour)) {
1244 wpa_printf(MSG_DEBUG, "P2P: Bonjour protocol not available");
1245 return;
1246 }
1247
1248 dl_list_for_each(bsrv, &wpa_s->global->p2p_srv_bonjour,
1249 struct p2p_srv_bonjour, list) {
1250 if (wpabuf_tailroom(resp) <
1251 5 + wpabuf_len(bsrv->query) + wpabuf_len(bsrv->resp))
1252 return;
1253 /* Length (to be filled) */
1254 len_pos = wpabuf_put(resp, 2);
1255 wpabuf_put_u8(resp, P2P_SERV_BONJOUR);
1256 wpabuf_put_u8(resp, srv_trans_id);
1257 /* Status Code */
1258 wpabuf_put_u8(resp, P2P_SD_SUCCESS);
1259 wpa_hexdump_ascii(MSG_DEBUG, "P2P: Matching Bonjour service",
1260 wpabuf_head(bsrv->resp),
1261 wpabuf_len(bsrv->resp));
1262 /* Response Data */
1263 wpabuf_put_buf(resp, bsrv->query); /* Key */
1264 wpabuf_put_buf(resp, bsrv->resp); /* Value */
1265 WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos -
1266 2);
1267 }
1268}
1269
1270
1271static void wpas_sd_req_bonjour(struct wpa_supplicant *wpa_s,
1272 struct wpabuf *resp, u8 srv_trans_id,
1273 const u8 *query, size_t query_len)
1274{
1275 struct p2p_srv_bonjour *bsrv;
1276 struct wpabuf buf;
1277 u8 *len_pos;
1278
1279 wpa_hexdump_ascii(MSG_DEBUG, "P2P: SD Request for Bonjour",
1280 query, query_len);
1281 if (dl_list_empty(&wpa_s->global->p2p_srv_bonjour)) {
1282 wpa_printf(MSG_DEBUG, "P2P: Bonjour protocol not available");
1283 wpas_sd_add_proto_not_avail(resp, P2P_SERV_BONJOUR,
1284 srv_trans_id);
1285 return;
1286 }
1287
1288 if (query_len == 0) {
1289 wpas_sd_all_bonjour(wpa_s, resp, srv_trans_id);
1290 return;
1291 }
1292
1293 if (wpabuf_tailroom(resp) < 5)
1294 return;
1295 /* Length (to be filled) */
1296 len_pos = wpabuf_put(resp, 2);
1297 wpabuf_put_u8(resp, P2P_SERV_BONJOUR);
1298 wpabuf_put_u8(resp, srv_trans_id);
1299
1300 wpabuf_set(&buf, query, query_len);
1301 bsrv = wpas_p2p_service_get_bonjour(wpa_s, &buf);
1302 if (bsrv == NULL) {
1303 wpa_printf(MSG_DEBUG, "P2P: Requested Bonjour service not "
1304 "available");
1305
1306 /* Status Code */
1307 wpabuf_put_u8(resp, P2P_SD_REQUESTED_INFO_NOT_AVAILABLE);
1308 /* Response Data: empty */
1309 WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos -
1310 2);
1311 return;
1312 }
1313
1314 /* Status Code */
1315 wpabuf_put_u8(resp, P2P_SD_SUCCESS);
1316 wpa_hexdump_ascii(MSG_DEBUG, "P2P: Matching Bonjour service",
1317 wpabuf_head(bsrv->resp), wpabuf_len(bsrv->resp));
1318
1319 if (wpabuf_tailroom(resp) >=
1320 wpabuf_len(bsrv->query) + wpabuf_len(bsrv->resp)) {
1321 /* Response Data */
1322 wpabuf_put_buf(resp, bsrv->query); /* Key */
1323 wpabuf_put_buf(resp, bsrv->resp); /* Value */
1324 }
1325 WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos - 2);
1326}
1327
1328
1329static void wpas_sd_all_upnp(struct wpa_supplicant *wpa_s,
1330 struct wpabuf *resp, u8 srv_trans_id)
1331{
1332 struct p2p_srv_upnp *usrv;
1333 u8 *len_pos;
1334
1335 wpa_printf(MSG_DEBUG, "P2P: SD Request for all UPnP services");
1336
1337 if (dl_list_empty(&wpa_s->global->p2p_srv_upnp)) {
1338 wpa_printf(MSG_DEBUG, "P2P: UPnP protocol not available");
1339 return;
1340 }
1341
1342 dl_list_for_each(usrv, &wpa_s->global->p2p_srv_upnp,
1343 struct p2p_srv_upnp, list) {
1344 if (wpabuf_tailroom(resp) < 5 + 1 + os_strlen(usrv->service))
1345 return;
1346
1347 /* Length (to be filled) */
1348 len_pos = wpabuf_put(resp, 2);
1349 wpabuf_put_u8(resp, P2P_SERV_UPNP);
1350 wpabuf_put_u8(resp, srv_trans_id);
1351
1352 /* Status Code */
1353 wpabuf_put_u8(resp, P2P_SD_SUCCESS);
1354 /* Response Data */
1355 wpabuf_put_u8(resp, usrv->version);
1356 wpa_printf(MSG_DEBUG, "P2P: Matching UPnP Service: %s",
1357 usrv->service);
1358 wpabuf_put_str(resp, usrv->service);
1359 WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos -
1360 2);
1361 }
1362}
1363
1364
1365static void wpas_sd_req_upnp(struct wpa_supplicant *wpa_s,
1366 struct wpabuf *resp, u8 srv_trans_id,
1367 const u8 *query, size_t query_len)
1368{
1369 struct p2p_srv_upnp *usrv;
1370 u8 *len_pos;
1371 u8 version;
1372 char *str;
1373 int count = 0;
1374
1375 wpa_hexdump_ascii(MSG_DEBUG, "P2P: SD Request for UPnP",
1376 query, query_len);
1377
1378 if (dl_list_empty(&wpa_s->global->p2p_srv_upnp)) {
1379 wpa_printf(MSG_DEBUG, "P2P: UPnP protocol not available");
1380 wpas_sd_add_proto_not_avail(resp, P2P_SERV_UPNP,
1381 srv_trans_id);
1382 return;
1383 }
1384
1385 if (query_len == 0) {
1386 wpas_sd_all_upnp(wpa_s, resp, srv_trans_id);
1387 return;
1388 }
1389
1390 if (wpabuf_tailroom(resp) < 5)
1391 return;
1392
1393 /* Length (to be filled) */
1394 len_pos = wpabuf_put(resp, 2);
1395 wpabuf_put_u8(resp, P2P_SERV_UPNP);
1396 wpabuf_put_u8(resp, srv_trans_id);
1397
1398 version = query[0];
1399 str = os_malloc(query_len);
1400 if (str == NULL)
1401 return;
1402 os_memcpy(str, query + 1, query_len - 1);
1403 str[query_len - 1] = '\0';
1404
1405 dl_list_for_each(usrv, &wpa_s->global->p2p_srv_upnp,
1406 struct p2p_srv_upnp, list) {
1407 if (version != usrv->version)
1408 continue;
1409
1410 if (os_strcmp(str, "ssdp:all") != 0 &&
1411 os_strstr(usrv->service, str) == NULL)
1412 continue;
1413
1414 if (wpabuf_tailroom(resp) < 2)
1415 break;
1416 if (count == 0) {
1417 /* Status Code */
1418 wpabuf_put_u8(resp, P2P_SD_SUCCESS);
1419 /* Response Data */
1420 wpabuf_put_u8(resp, version);
1421 } else
1422 wpabuf_put_u8(resp, ',');
1423
1424 count++;
1425
1426 wpa_printf(MSG_DEBUG, "P2P: Matching UPnP Service: %s",
1427 usrv->service);
1428 if (wpabuf_tailroom(resp) < os_strlen(usrv->service))
1429 break;
1430 wpabuf_put_str(resp, usrv->service);
1431 }
1432 os_free(str);
1433
1434 if (count == 0) {
1435 wpa_printf(MSG_DEBUG, "P2P: Requested UPnP service not "
1436 "available");
1437 /* Status Code */
1438 wpabuf_put_u8(resp, P2P_SD_REQUESTED_INFO_NOT_AVAILABLE);
1439 /* Response Data: empty */
1440 }
1441
1442 WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos - 2);
1443}
1444
1445
1446void wpas_sd_request(void *ctx, int freq, const u8 *sa, u8 dialog_token,
1447 u16 update_indic, const u8 *tlvs, size_t tlvs_len)
1448{
1449 struct wpa_supplicant *wpa_s = ctx;
1450 const u8 *pos = tlvs;
1451 const u8 *end = tlvs + tlvs_len;
1452 const u8 *tlv_end;
1453 u16 slen;
1454 struct wpabuf *resp;
1455 u8 srv_proto, srv_trans_id;
1456 size_t buf_len;
1457 char *buf;
1458
1459 wpa_hexdump(MSG_MSGDUMP, "P2P: Service Discovery Request TLVs",
1460 tlvs, tlvs_len);
1461 buf_len = 2 * tlvs_len + 1;
1462 buf = os_malloc(buf_len);
1463 if (buf) {
1464 wpa_snprintf_hex(buf, buf_len, tlvs, tlvs_len);
1465 wpa_msg_ctrl(wpa_s, MSG_INFO, P2P_EVENT_SERV_DISC_REQ "%d "
1466 MACSTR " %u %u %s",
1467 freq, MAC2STR(sa), dialog_token, update_indic,
1468 buf);
1469 os_free(buf);
1470 }
1471
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001472 if (wpa_s->p2p_sd_over_ctrl_iface) {
1473 wpas_notify_p2p_sd_request(wpa_s, freq, sa, dialog_token,
1474 update_indic, tlvs, tlvs_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001475 return; /* to be processed by an external program */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001476 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001477
1478 resp = wpabuf_alloc(10000);
1479 if (resp == NULL)
1480 return;
1481
1482 while (pos + 1 < end) {
1483 wpa_printf(MSG_DEBUG, "P2P: Service Request TLV");
1484 slen = WPA_GET_LE16(pos);
1485 pos += 2;
1486 if (pos + slen > end || slen < 2) {
1487 wpa_printf(MSG_DEBUG, "P2P: Unexpected Query Data "
1488 "length");
1489 wpabuf_free(resp);
1490 return;
1491 }
1492 tlv_end = pos + slen;
1493
1494 srv_proto = *pos++;
1495 wpa_printf(MSG_DEBUG, "P2P: Service Protocol Type %u",
1496 srv_proto);
1497 srv_trans_id = *pos++;
1498 wpa_printf(MSG_DEBUG, "P2P: Service Transaction ID %u",
1499 srv_trans_id);
1500
1501 wpa_hexdump(MSG_MSGDUMP, "P2P: Query Data",
1502 pos, tlv_end - pos);
1503
1504
1505 if (wpa_s->force_long_sd) {
1506 wpa_printf(MSG_DEBUG, "P2P: SD test - force long "
1507 "response");
1508 wpas_sd_all_bonjour(wpa_s, resp, srv_trans_id);
1509 wpas_sd_all_upnp(wpa_s, resp, srv_trans_id);
1510 goto done;
1511 }
1512
1513 switch (srv_proto) {
1514 case P2P_SERV_ALL_SERVICES:
1515 wpa_printf(MSG_DEBUG, "P2P: Service Discovery Request "
1516 "for all services");
1517 if (dl_list_empty(&wpa_s->global->p2p_srv_upnp) &&
1518 dl_list_empty(&wpa_s->global->p2p_srv_bonjour)) {
1519 wpa_printf(MSG_DEBUG, "P2P: No service "
1520 "discovery protocols available");
1521 wpas_sd_add_proto_not_avail(
1522 resp, P2P_SERV_ALL_SERVICES,
1523 srv_trans_id);
1524 break;
1525 }
1526 wpas_sd_all_bonjour(wpa_s, resp, srv_trans_id);
1527 wpas_sd_all_upnp(wpa_s, resp, srv_trans_id);
1528 break;
1529 case P2P_SERV_BONJOUR:
1530 wpas_sd_req_bonjour(wpa_s, resp, srv_trans_id,
1531 pos, tlv_end - pos);
1532 break;
1533 case P2P_SERV_UPNP:
1534 wpas_sd_req_upnp(wpa_s, resp, srv_trans_id,
1535 pos, tlv_end - pos);
1536 break;
1537 default:
1538 wpa_printf(MSG_DEBUG, "P2P: Unavailable service "
1539 "protocol %u", srv_proto);
1540 wpas_sd_add_proto_not_avail(resp, srv_proto,
1541 srv_trans_id);
1542 break;
1543 }
1544
1545 pos = tlv_end;
1546 }
1547
1548done:
1549 wpas_notify_p2p_sd_request(wpa_s, freq, sa, dialog_token,
1550 update_indic, tlvs, tlvs_len);
1551
1552 wpas_p2p_sd_response(wpa_s, freq, sa, dialog_token, resp);
1553
1554 wpabuf_free(resp);
1555}
1556
1557
1558void wpas_sd_response(void *ctx, const u8 *sa, u16 update_indic,
1559 const u8 *tlvs, size_t tlvs_len)
1560{
1561 struct wpa_supplicant *wpa_s = ctx;
1562 const u8 *pos = tlvs;
1563 const u8 *end = tlvs + tlvs_len;
1564 const u8 *tlv_end;
1565 u16 slen;
1566 size_t buf_len;
1567 char *buf;
1568
1569 wpa_hexdump(MSG_MSGDUMP, "P2P: Service Discovery Response TLVs",
1570 tlvs, tlvs_len);
1571 if (tlvs_len > 1500) {
1572 /* TODO: better way for handling this */
1573 wpa_msg_ctrl(wpa_s, MSG_INFO,
1574 P2P_EVENT_SERV_DISC_RESP MACSTR
1575 " %u <long response: %u bytes>",
1576 MAC2STR(sa), update_indic,
1577 (unsigned int) tlvs_len);
1578 } else {
1579 buf_len = 2 * tlvs_len + 1;
1580 buf = os_malloc(buf_len);
1581 if (buf) {
1582 wpa_snprintf_hex(buf, buf_len, tlvs, tlvs_len);
1583 wpa_msg_ctrl(wpa_s, MSG_INFO,
1584 P2P_EVENT_SERV_DISC_RESP MACSTR " %u %s",
1585 MAC2STR(sa), update_indic, buf);
1586 os_free(buf);
1587 }
1588 }
1589
1590 while (pos < end) {
1591 u8 srv_proto, srv_trans_id, status;
1592
1593 wpa_printf(MSG_DEBUG, "P2P: Service Response TLV");
1594 slen = WPA_GET_LE16(pos);
1595 pos += 2;
1596 if (pos + slen > end || slen < 3) {
1597 wpa_printf(MSG_DEBUG, "P2P: Unexpected Response Data "
1598 "length");
1599 return;
1600 }
1601 tlv_end = pos + slen;
1602
1603 srv_proto = *pos++;
1604 wpa_printf(MSG_DEBUG, "P2P: Service Protocol Type %u",
1605 srv_proto);
1606 srv_trans_id = *pos++;
1607 wpa_printf(MSG_DEBUG, "P2P: Service Transaction ID %u",
1608 srv_trans_id);
1609 status = *pos++;
1610 wpa_printf(MSG_DEBUG, "P2P: Status Code ID %u",
1611 status);
1612
1613 wpa_hexdump(MSG_MSGDUMP, "P2P: Response Data",
1614 pos, tlv_end - pos);
1615
1616 pos = tlv_end;
1617 }
1618
1619 wpas_notify_p2p_sd_response(wpa_s, sa, update_indic, tlvs, tlvs_len);
1620}
1621
1622
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001623u64 wpas_p2p_sd_request(struct wpa_supplicant *wpa_s, const u8 *dst,
1624 const struct wpabuf *tlvs)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001625{
1626 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001627 return wpa_drv_p2p_sd_request(wpa_s, dst, tlvs);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001628 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001629 return 0;
1630 return (uintptr_t) p2p_sd_request(wpa_s->global->p2p, dst, tlvs);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001631}
1632
1633
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001634u64 wpas_p2p_sd_request_upnp(struct wpa_supplicant *wpa_s, const u8 *dst,
1635 u8 version, const char *query)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001636{
1637 struct wpabuf *tlvs;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001638 u64 ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001639
1640 tlvs = wpabuf_alloc(2 + 1 + 1 + 1 + os_strlen(query));
1641 if (tlvs == NULL)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001642 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001643 wpabuf_put_le16(tlvs, 1 + 1 + 1 + os_strlen(query));
1644 wpabuf_put_u8(tlvs, P2P_SERV_UPNP); /* Service Protocol Type */
1645 wpabuf_put_u8(tlvs, 1); /* Service Transaction ID */
1646 wpabuf_put_u8(tlvs, version);
1647 wpabuf_put_str(tlvs, query);
1648 ret = wpas_p2p_sd_request(wpa_s, dst, tlvs);
1649 wpabuf_free(tlvs);
1650 return ret;
1651}
1652
1653
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001654int wpas_p2p_sd_cancel_request(struct wpa_supplicant *wpa_s, u64 req)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001655{
1656 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001657 return wpa_drv_p2p_sd_cancel_request(wpa_s, req);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001658 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
1659 return -1;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001660 return p2p_sd_cancel_request(wpa_s->global->p2p,
1661 (void *) (uintptr_t) req);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001662}
1663
1664
1665void wpas_p2p_sd_response(struct wpa_supplicant *wpa_s, int freq,
1666 const u8 *dst, u8 dialog_token,
1667 const struct wpabuf *resp_tlvs)
1668{
1669 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT) {
1670 wpa_drv_p2p_sd_response(wpa_s, freq, dst, dialog_token,
1671 resp_tlvs);
1672 return;
1673 }
1674 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
1675 return;
1676 p2p_sd_response(wpa_s->global->p2p, freq, dst, dialog_token,
1677 resp_tlvs);
1678}
1679
Dmitry Shmidtb5e8f062012-08-08 10:56:33 -07001680#ifdef ANDROID_P2P
1681void wpas_p2p_sd_service_update(struct wpa_supplicant *wpa_s, int action)
1682#else
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001683void wpas_p2p_sd_service_update(struct wpa_supplicant *wpa_s)
Dmitry Shmidtb5e8f062012-08-08 10:56:33 -07001684#endif
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001685{
1686 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT) {
1687 wpa_drv_p2p_service_update(wpa_s);
1688 return;
1689 }
1690 if (wpa_s->global->p2p)
Dmitry Shmidtb5e8f062012-08-08 10:56:33 -07001691#ifdef ANDROID_P2P
1692 p2p_sd_service_update(wpa_s->global->p2p, action);
1693#else
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001694 p2p_sd_service_update(wpa_s->global->p2p);
Dmitry Shmidtb5e8f062012-08-08 10:56:33 -07001695#endif
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001696}
1697
1698
1699static void wpas_p2p_srv_bonjour_free(struct p2p_srv_bonjour *bsrv)
1700{
1701 dl_list_del(&bsrv->list);
1702 wpabuf_free(bsrv->query);
1703 wpabuf_free(bsrv->resp);
1704 os_free(bsrv);
1705}
1706
1707
1708static void wpas_p2p_srv_upnp_free(struct p2p_srv_upnp *usrv)
1709{
1710 dl_list_del(&usrv->list);
1711 os_free(usrv->service);
1712 os_free(usrv);
1713}
1714
1715
1716void wpas_p2p_service_flush(struct wpa_supplicant *wpa_s)
1717{
1718 struct p2p_srv_bonjour *bsrv, *bn;
1719 struct p2p_srv_upnp *usrv, *un;
1720
1721 dl_list_for_each_safe(bsrv, bn, &wpa_s->global->p2p_srv_bonjour,
1722 struct p2p_srv_bonjour, list)
1723 wpas_p2p_srv_bonjour_free(bsrv);
1724
1725 dl_list_for_each_safe(usrv, un, &wpa_s->global->p2p_srv_upnp,
1726 struct p2p_srv_upnp, list)
1727 wpas_p2p_srv_upnp_free(usrv);
1728
Dmitry Shmidtb5e8f062012-08-08 10:56:33 -07001729#ifdef ANDROID_P2P
1730 wpas_p2p_sd_service_update(wpa_s, SRV_FLUSH);
1731#else
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001732 wpas_p2p_sd_service_update(wpa_s);
Dmitry Shmidtb5e8f062012-08-08 10:56:33 -07001733#endif
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001734}
1735
1736
1737int wpas_p2p_service_add_bonjour(struct wpa_supplicant *wpa_s,
1738 struct wpabuf *query, struct wpabuf *resp)
1739{
1740 struct p2p_srv_bonjour *bsrv;
1741
1742 bsrv = wpas_p2p_service_get_bonjour(wpa_s, query);
1743 if (bsrv) {
1744 wpabuf_free(query);
1745 wpabuf_free(bsrv->resp);
1746 bsrv->resp = resp;
1747 return 0;
1748 }
1749
1750 bsrv = os_zalloc(sizeof(*bsrv));
1751 if (bsrv == NULL)
1752 return -1;
1753 bsrv->query = query;
1754 bsrv->resp = resp;
1755 dl_list_add(&wpa_s->global->p2p_srv_bonjour, &bsrv->list);
1756
Dmitry Shmidtb5e8f062012-08-08 10:56:33 -07001757#ifdef ANDROID_P2P
1758 wpas_p2p_sd_service_update(wpa_s, SRV_ADD);
1759#else
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001760 wpas_p2p_sd_service_update(wpa_s);
Dmitry Shmidtb5e8f062012-08-08 10:56:33 -07001761#endif
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001762 return 0;
1763}
1764
1765
1766int wpas_p2p_service_del_bonjour(struct wpa_supplicant *wpa_s,
1767 const struct wpabuf *query)
1768{
1769 struct p2p_srv_bonjour *bsrv;
1770
1771 bsrv = wpas_p2p_service_get_bonjour(wpa_s, query);
1772 if (bsrv == NULL)
1773 return -1;
1774 wpas_p2p_srv_bonjour_free(bsrv);
Dmitry Shmidtb5e8f062012-08-08 10:56:33 -07001775#ifdef ANDROID_P2P
1776 wpas_p2p_sd_service_update(wpa_s, SRV_DEL);
1777#else
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001778 wpas_p2p_sd_service_update(wpa_s);
Dmitry Shmidtb5e8f062012-08-08 10:56:33 -07001779#endif
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001780 return 0;
1781}
1782
1783
1784int wpas_p2p_service_add_upnp(struct wpa_supplicant *wpa_s, u8 version,
1785 const char *service)
1786{
1787 struct p2p_srv_upnp *usrv;
1788
1789 if (wpas_p2p_service_get_upnp(wpa_s, version, service))
1790 return 0; /* Already listed */
1791 usrv = os_zalloc(sizeof(*usrv));
1792 if (usrv == NULL)
1793 return -1;
1794 usrv->version = version;
1795 usrv->service = os_strdup(service);
1796 if (usrv->service == NULL) {
1797 os_free(usrv);
1798 return -1;
1799 }
1800 dl_list_add(&wpa_s->global->p2p_srv_upnp, &usrv->list);
1801
Dmitry Shmidtb5e8f062012-08-08 10:56:33 -07001802#ifdef ANDROID_P2P
1803 wpas_p2p_sd_service_update(wpa_s, SRV_ADD);
1804#else
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001805 wpas_p2p_sd_service_update(wpa_s);
Dmitry Shmidtb5e8f062012-08-08 10:56:33 -07001806#endif
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001807 return 0;
1808}
1809
1810
1811int wpas_p2p_service_del_upnp(struct wpa_supplicant *wpa_s, u8 version,
1812 const char *service)
1813{
1814 struct p2p_srv_upnp *usrv;
1815
1816 usrv = wpas_p2p_service_get_upnp(wpa_s, version, service);
1817 if (usrv == NULL)
1818 return -1;
1819 wpas_p2p_srv_upnp_free(usrv);
Dmitry Shmidtb5e8f062012-08-08 10:56:33 -07001820#ifdef ANDROID_P2P
1821 wpas_p2p_sd_service_update(wpa_s, SRV_DEL);
1822#else
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001823 wpas_p2p_sd_service_update(wpa_s);
Dmitry Shmidtb5e8f062012-08-08 10:56:33 -07001824#endif
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001825 return 0;
1826}
1827
1828
1829static void wpas_prov_disc_local_display(struct wpa_supplicant *wpa_s,
1830 const u8 *peer, const char *params,
1831 unsigned int generated_pin)
1832{
1833 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_SHOW_PIN MACSTR " %08d%s",
1834 MAC2STR(peer), generated_pin, params);
1835}
1836
1837
1838static void wpas_prov_disc_local_keypad(struct wpa_supplicant *wpa_s,
1839 const u8 *peer, const char *params)
1840{
1841 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_ENTER_PIN MACSTR "%s",
1842 MAC2STR(peer), params);
1843}
1844
1845
1846void wpas_prov_disc_req(void *ctx, const u8 *peer, u16 config_methods,
1847 const u8 *dev_addr, const u8 *pri_dev_type,
1848 const char *dev_name, u16 supp_config_methods,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001849 u8 dev_capab, u8 group_capab, const u8 *group_id,
1850 size_t group_id_len)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001851{
1852 struct wpa_supplicant *wpa_s = ctx;
1853 char devtype[WPS_DEV_TYPE_BUFSIZE];
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001854 char params[300];
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001855 u8 empty_dev_type[8];
1856 unsigned int generated_pin = 0;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001857 struct wpa_supplicant *group = NULL;
1858
1859 if (group_id) {
1860 for (group = wpa_s->global->ifaces; group; group = group->next)
1861 {
1862 struct wpa_ssid *s = group->current_ssid;
1863 if (s != NULL &&
1864 s->mode == WPAS_MODE_P2P_GO &&
1865 group_id_len - ETH_ALEN == s->ssid_len &&
1866 os_memcmp(group_id + ETH_ALEN, s->ssid,
1867 s->ssid_len) == 0)
1868 break;
1869 }
1870 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001871
1872 if (pri_dev_type == NULL) {
1873 os_memset(empty_dev_type, 0, sizeof(empty_dev_type));
1874 pri_dev_type = empty_dev_type;
1875 }
1876 os_snprintf(params, sizeof(params), " p2p_dev_addr=" MACSTR
1877 " pri_dev_type=%s name='%s' config_methods=0x%x "
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001878 "dev_capab=0x%x group_capab=0x%x%s%s",
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001879 MAC2STR(dev_addr),
1880 wps_dev_type_bin2str(pri_dev_type, devtype,
1881 sizeof(devtype)),
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001882 dev_name, supp_config_methods, dev_capab, group_capab,
1883 group ? " group=" : "",
1884 group ? group->ifname : "");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001885 params[sizeof(params) - 1] = '\0';
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08001886
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001887 if (config_methods & WPS_CONFIG_DISPLAY) {
1888 generated_pin = wps_generate_pin();
1889 wpas_prov_disc_local_display(wpa_s, peer, params,
1890 generated_pin);
1891 } else if (config_methods & WPS_CONFIG_KEYPAD)
1892 wpas_prov_disc_local_keypad(wpa_s, peer, params);
1893 else if (config_methods & WPS_CONFIG_PUSHBUTTON)
1894 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_PBC_REQ MACSTR
1895 "%s", MAC2STR(peer), params);
Jouni Malinen75ecf522011-06-27 15:19:46 -07001896
1897 wpas_notify_p2p_provision_discovery(wpa_s, peer, 1 /* request */,
1898 P2P_PROV_DISC_SUCCESS,
1899 config_methods, generated_pin);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001900}
1901
1902
1903void wpas_prov_disc_resp(void *ctx, const u8 *peer, u16 config_methods)
1904{
1905 struct wpa_supplicant *wpa_s = ctx;
1906 unsigned int generated_pin = 0;
Dmitry Shmidt04949592012-07-19 12:16:46 -07001907 char params[20];
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001908
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08001909 if (wpa_s->pending_pd_before_join &&
1910 (os_memcmp(peer, wpa_s->pending_join_dev_addr, ETH_ALEN) == 0 ||
1911 os_memcmp(peer, wpa_s->pending_join_iface_addr, ETH_ALEN) == 0)) {
1912 wpa_s->pending_pd_before_join = 0;
1913 wpa_printf(MSG_DEBUG, "P2P: Starting pending "
1914 "join-existing-group operation");
1915 wpas_p2p_join_start(wpa_s);
1916 return;
1917 }
1918
Dmitry Shmidt04949592012-07-19 12:16:46 -07001919 if (wpa_s->pending_pd_use == AUTO_PD_JOIN ||
1920 wpa_s->pending_pd_use == AUTO_PD_GO_NEG)
1921 os_snprintf(params, sizeof(params), " peer_go=%d",
1922 wpa_s->pending_pd_use == AUTO_PD_JOIN);
1923 else
1924 params[0] = '\0';
1925
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001926 if (config_methods & WPS_CONFIG_DISPLAY)
Dmitry Shmidt04949592012-07-19 12:16:46 -07001927 wpas_prov_disc_local_keypad(wpa_s, peer, params);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001928 else if (config_methods & WPS_CONFIG_KEYPAD) {
1929 generated_pin = wps_generate_pin();
Dmitry Shmidt04949592012-07-19 12:16:46 -07001930 wpas_prov_disc_local_display(wpa_s, peer, params,
1931 generated_pin);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001932 } else if (config_methods & WPS_CONFIG_PUSHBUTTON)
Dmitry Shmidt04949592012-07-19 12:16:46 -07001933 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_PBC_RESP MACSTR
1934 "%s", MAC2STR(peer), params);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001935
Jouni Malinen75ecf522011-06-27 15:19:46 -07001936 wpas_notify_p2p_provision_discovery(wpa_s, peer, 0 /* response */,
1937 P2P_PROV_DISC_SUCCESS,
1938 config_methods, generated_pin);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001939}
1940
1941
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001942static void wpas_prov_disc_fail(void *ctx, const u8 *peer,
1943 enum p2p_prov_disc_status status)
Jouni Malinen75ecf522011-06-27 15:19:46 -07001944{
1945 struct wpa_supplicant *wpa_s = ctx;
1946
Dmitry Shmidt04949592012-07-19 12:16:46 -07001947 if (wpa_s->p2p_fallback_to_go_neg) {
1948 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: PD for p2p_connect-auto "
1949 "failed - fall back to GO Negotiation");
1950 wpas_p2p_fallback_to_go_neg(wpa_s, 0);
1951 return;
1952 }
1953
1954 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_FAILURE
1955 " p2p_dev_addr=" MACSTR " status=%d",
1956 MAC2STR(peer), status);
1957
Jouni Malinen75ecf522011-06-27 15:19:46 -07001958 wpas_notify_p2p_provision_discovery(wpa_s, peer, 0 /* response */,
1959 status, 0, 0);
1960}
1961
1962
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001963static u8 wpas_invitation_process(void *ctx, const u8 *sa, const u8 *bssid,
1964 const u8 *go_dev_addr, const u8 *ssid,
1965 size_t ssid_len, int *go, u8 *group_bssid,
1966 int *force_freq, int persistent_group)
1967{
1968 struct wpa_supplicant *wpa_s = ctx;
1969 struct wpa_ssid *s;
1970 u8 cur_bssid[ETH_ALEN];
1971 int res;
1972 struct wpa_supplicant *grp;
1973
1974 if (!persistent_group) {
1975 wpa_printf(MSG_DEBUG, "P2P: Invitation from " MACSTR
1976 " to join an active group", MAC2STR(sa));
1977 if (!is_zero_ether_addr(wpa_s->p2p_auth_invite) &&
1978 (os_memcmp(go_dev_addr, wpa_s->p2p_auth_invite, ETH_ALEN)
1979 == 0 ||
1980 os_memcmp(sa, wpa_s->p2p_auth_invite, ETH_ALEN) == 0)) {
1981 wpa_printf(MSG_DEBUG, "P2P: Accept previously "
1982 "authorized invitation");
1983 goto accept_inv;
1984 }
1985 /*
1986 * Do not accept the invitation automatically; notify user and
1987 * request approval.
1988 */
1989 return P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE;
1990 }
1991
1992 grp = wpas_get_p2p_group(wpa_s, ssid, ssid_len, go);
1993 if (grp) {
1994 wpa_printf(MSG_DEBUG, "P2P: Accept invitation to already "
1995 "running persistent group");
1996 if (*go)
1997 os_memcpy(group_bssid, grp->own_addr, ETH_ALEN);
1998 goto accept_inv;
1999 }
2000
2001 if (!wpa_s->conf->persistent_reconnect)
2002 return P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE;
2003
2004 for (s = wpa_s->conf->ssid; s; s = s->next) {
2005 if (s->disabled == 2 &&
2006 os_memcmp(s->bssid, go_dev_addr, ETH_ALEN) == 0 &&
2007 s->ssid_len == ssid_len &&
2008 os_memcmp(ssid, s->ssid, ssid_len) == 0)
2009 break;
2010 }
2011
2012 if (!s) {
2013 wpa_printf(MSG_DEBUG, "P2P: Invitation from " MACSTR
2014 " requested reinvocation of an unknown group",
2015 MAC2STR(sa));
2016 return P2P_SC_FAIL_UNKNOWN_GROUP;
2017 }
2018
2019 if (s->mode == WPAS_MODE_P2P_GO && !wpas_p2p_create_iface(wpa_s)) {
2020 *go = 1;
2021 if (wpa_s->wpa_state >= WPA_AUTHENTICATING) {
2022 wpa_printf(MSG_DEBUG, "P2P: The only available "
2023 "interface is already in use - reject "
2024 "invitation");
2025 return P2P_SC_FAIL_UNABLE_TO_ACCOMMODATE;
2026 }
2027 os_memcpy(group_bssid, wpa_s->own_addr, ETH_ALEN);
2028 } else if (s->mode == WPAS_MODE_P2P_GO) {
2029 *go = 1;
2030 if (wpas_p2p_add_group_interface(wpa_s, WPA_IF_P2P_GO) < 0)
2031 {
2032 wpa_printf(MSG_ERROR, "P2P: Failed to allocate a new "
2033 "interface address for the group");
2034 return P2P_SC_FAIL_UNABLE_TO_ACCOMMODATE;
2035 }
2036 os_memcpy(group_bssid, wpa_s->pending_interface_addr,
2037 ETH_ALEN);
2038 }
2039
2040accept_inv:
2041 if (wpa_s->current_ssid && wpa_drv_get_bssid(wpa_s, cur_bssid) == 0 &&
2042 wpa_s->assoc_freq) {
2043 wpa_printf(MSG_DEBUG, "P2P: Trying to force channel to match "
2044 "the channel we are already using");
2045 *force_freq = wpa_s->assoc_freq;
2046 }
2047
2048 res = wpa_drv_shared_freq(wpa_s);
2049 if (res > 0) {
2050 wpa_printf(MSG_DEBUG, "P2P: Trying to force channel to match "
2051 "with the channel we are already using on a "
2052 "shared interface");
2053 *force_freq = res;
2054 }
2055
2056 return P2P_SC_SUCCESS;
2057}
2058
2059
2060static void wpas_invitation_received(void *ctx, const u8 *sa, const u8 *bssid,
2061 const u8 *ssid, size_t ssid_len,
2062 const u8 *go_dev_addr, u8 status,
2063 int op_freq)
2064{
2065 struct wpa_supplicant *wpa_s = ctx;
2066 struct wpa_ssid *s;
2067
2068 for (s = wpa_s->conf->ssid; s; s = s->next) {
2069 if (s->disabled == 2 &&
2070 s->ssid_len == ssid_len &&
2071 os_memcmp(ssid, s->ssid, ssid_len) == 0)
2072 break;
2073 }
2074
2075 if (status == P2P_SC_SUCCESS) {
2076 wpa_printf(MSG_DEBUG, "P2P: Invitation from peer " MACSTR
2077 " was accepted; op_freq=%d MHz",
2078 MAC2STR(sa), op_freq);
2079 if (s) {
2080 wpas_p2p_group_add_persistent(
2081 wpa_s, s, s->mode == WPAS_MODE_P2P_GO, 0);
2082 } else if (bssid) {
2083 wpas_p2p_join(wpa_s, bssid, go_dev_addr,
Dmitry Shmidt04949592012-07-19 12:16:46 -07002084 wpa_s->p2p_wps_method, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002085 }
2086 return;
2087 }
2088
2089 if (status != P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE) {
2090 wpa_printf(MSG_DEBUG, "P2P: Invitation from peer " MACSTR
2091 " was rejected (status %u)", MAC2STR(sa), status);
2092 return;
2093 }
2094
2095 if (!s) {
2096 if (bssid) {
2097 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_INVITATION_RECEIVED
2098 "sa=" MACSTR " go_dev_addr=" MACSTR
2099 " bssid=" MACSTR " unknown-network",
2100 MAC2STR(sa), MAC2STR(go_dev_addr),
2101 MAC2STR(bssid));
2102 } else {
2103 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_INVITATION_RECEIVED
2104 "sa=" MACSTR " go_dev_addr=" MACSTR
2105 " unknown-network",
2106 MAC2STR(sa), MAC2STR(go_dev_addr));
2107 }
2108 return;
2109 }
2110
2111 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_INVITATION_RECEIVED "sa=" MACSTR
2112 " persistent=%d", MAC2STR(sa), s->id);
2113}
2114
2115
2116static void wpas_invitation_result(void *ctx, int status, const u8 *bssid)
2117{
2118 struct wpa_supplicant *wpa_s = ctx;
2119 struct wpa_ssid *ssid;
2120
2121 if (bssid) {
2122 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_INVITATION_RESULT
2123 "status=%d " MACSTR,
2124 status, MAC2STR(bssid));
2125 } else {
2126 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_INVITATION_RESULT
2127 "status=%d ", status);
2128 }
2129 wpas_notify_p2p_invitation_result(wpa_s, status, bssid);
2130
2131 if (wpa_s->pending_invite_ssid_id == -1)
2132 return; /* Invitation to active group */
2133
2134 if (status != P2P_SC_SUCCESS) {
2135 wpas_p2p_remove_pending_group_interface(wpa_s);
2136 return;
2137 }
2138
2139 ssid = wpa_config_get_network(wpa_s->conf,
2140 wpa_s->pending_invite_ssid_id);
2141 if (ssid == NULL) {
2142 wpa_printf(MSG_ERROR, "P2P: Could not find persistent group "
2143 "data matching with invitation");
2144 return;
2145 }
2146
2147 wpas_p2p_group_add_persistent(wpa_s, ssid,
2148 ssid->mode == WPAS_MODE_P2P_GO, 0);
2149}
2150
2151
Dmitry Shmidt04949592012-07-19 12:16:46 -07002152static int wpas_p2p_disallowed_freq(struct wpa_global *global,
2153 unsigned int freq)
2154{
2155 unsigned int i;
2156
2157 if (global->p2p_disallow_freq == NULL)
2158 return 0;
2159
2160 for (i = 0; i < global->num_p2p_disallow_freq; i++) {
2161 if (freq >= global->p2p_disallow_freq[i].min &&
2162 freq <= global->p2p_disallow_freq[i].max)
2163 return 1;
2164 }
2165
2166 return 0;
2167}
2168
2169
2170static void wpas_p2p_add_chan(struct p2p_reg_class *reg, u8 chan)
2171{
2172 reg->channel[reg->channels] = chan;
2173 reg->channels++;
2174}
2175
2176
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002177static int wpas_p2p_default_channels(struct wpa_supplicant *wpa_s,
2178 struct p2p_channels *chan)
2179{
2180 int i, cla = 0;
2181
2182 wpa_printf(MSG_DEBUG, "P2P: Enable operating classes for 2.4 GHz "
2183 "band");
2184
2185 /* Operating class 81 - 2.4 GHz band channels 1..13 */
2186 chan->reg_class[cla].reg_class = 81;
Dmitry Shmidt04949592012-07-19 12:16:46 -07002187 chan->reg_class[cla].channels = 0;
2188 for (i = 0; i < 11; i++) {
2189 if (!wpas_p2p_disallowed_freq(wpa_s->global, 2412 + i * 5))
2190 wpas_p2p_add_chan(&chan->reg_class[cla], i + 1);
2191 }
2192 if (chan->reg_class[cla].channels)
2193 cla++;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002194
2195 wpa_printf(MSG_DEBUG, "P2P: Enable operating classes for lower 5 GHz "
2196 "band");
2197
2198 /* Operating class 115 - 5 GHz, channels 36-48 */
2199 chan->reg_class[cla].reg_class = 115;
Dmitry Shmidt04949592012-07-19 12:16:46 -07002200 chan->reg_class[cla].channels = 0;
2201 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 36 * 5))
2202 wpas_p2p_add_chan(&chan->reg_class[cla], 36);
2203 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 40 * 5))
2204 wpas_p2p_add_chan(&chan->reg_class[cla], 40);
2205 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 44 * 5))
2206 wpas_p2p_add_chan(&chan->reg_class[cla], 44);
2207 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 48 * 5))
2208 wpas_p2p_add_chan(&chan->reg_class[cla], 48);
2209 if (chan->reg_class[cla].channels)
2210 cla++;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002211
2212 wpa_printf(MSG_DEBUG, "P2P: Enable operating classes for higher 5 GHz "
2213 "band");
2214
2215 /* Operating class 124 - 5 GHz, channels 149,153,157,161 */
2216 chan->reg_class[cla].reg_class = 124;
Dmitry Shmidt04949592012-07-19 12:16:46 -07002217 chan->reg_class[cla].channels = 0;
2218 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 149 * 5))
2219 wpas_p2p_add_chan(&chan->reg_class[cla], 149);
2220 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 153 * 5))
2221 wpas_p2p_add_chan(&chan->reg_class[cla], 153);
2222 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 156 * 5))
2223 wpas_p2p_add_chan(&chan->reg_class[cla], 157);
2224 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 161 * 5))
2225 wpas_p2p_add_chan(&chan->reg_class[cla], 161);
2226 if (chan->reg_class[cla].channels)
2227 cla++;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002228
2229 chan->reg_classes = cla;
2230 return 0;
2231}
2232
2233
2234static struct hostapd_hw_modes * get_mode(struct hostapd_hw_modes *modes,
2235 u16 num_modes,
2236 enum hostapd_hw_mode mode)
2237{
2238 u16 i;
2239
2240 for (i = 0; i < num_modes; i++) {
2241 if (modes[i].mode == mode)
2242 return &modes[i];
2243 }
2244
2245 return NULL;
2246}
2247
2248
Dmitry Shmidt04949592012-07-19 12:16:46 -07002249static int has_channel(struct wpa_global *global,
2250 struct hostapd_hw_modes *mode, u8 chan, int *flags)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002251{
2252 int i;
Dmitry Shmidt04949592012-07-19 12:16:46 -07002253 unsigned int freq;
2254
2255 freq = (mode->mode == HOSTAPD_MODE_IEEE80211A ? 5000 : 2407) +
2256 chan * 5;
2257 if (wpas_p2p_disallowed_freq(global, freq))
2258 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002259
2260 for (i = 0; i < mode->num_channels; i++) {
2261 if (mode->channels[i].chan == chan) {
2262 if (flags)
2263 *flags = mode->channels[i].flag;
2264 return !(mode->channels[i].flag &
2265 (HOSTAPD_CHAN_DISABLED |
2266 HOSTAPD_CHAN_PASSIVE_SCAN |
2267 HOSTAPD_CHAN_NO_IBSS |
2268 HOSTAPD_CHAN_RADAR));
2269 }
2270 }
2271
2272 return 0;
2273}
2274
2275
2276struct p2p_oper_class_map {
2277 enum hostapd_hw_mode mode;
2278 u8 op_class;
2279 u8 min_chan;
2280 u8 max_chan;
2281 u8 inc;
2282 enum { BW20, BW40PLUS, BW40MINUS } bw;
2283};
2284
2285static int wpas_p2p_setup_channels(struct wpa_supplicant *wpa_s,
2286 struct p2p_channels *chan)
2287{
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002288 struct hostapd_hw_modes *mode;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002289 int cla, op;
2290 struct p2p_oper_class_map op_class[] = {
2291 { HOSTAPD_MODE_IEEE80211G, 81, 1, 13, 1, BW20 },
2292 { HOSTAPD_MODE_IEEE80211G, 82, 14, 14, 1, BW20 },
2293#if 0 /* Do not enable HT40 on 2 GHz for now */
2294 { HOSTAPD_MODE_IEEE80211G, 83, 1, 9, 1, BW40PLUS },
2295 { HOSTAPD_MODE_IEEE80211G, 84, 5, 13, 1, BW40MINUS },
2296#endif
2297 { HOSTAPD_MODE_IEEE80211A, 115, 36, 48, 4, BW20 },
2298 { HOSTAPD_MODE_IEEE80211A, 124, 149, 161, 4, BW20 },
2299 { HOSTAPD_MODE_IEEE80211A, 116, 36, 44, 8, BW40PLUS },
2300 { HOSTAPD_MODE_IEEE80211A, 117, 40, 48, 8, BW40MINUS },
2301 { HOSTAPD_MODE_IEEE80211A, 126, 149, 157, 8, BW40PLUS },
2302 { HOSTAPD_MODE_IEEE80211A, 127, 153, 161, 8, BW40MINUS },
2303 { -1, 0, 0, 0, 0, BW20 }
2304 };
2305
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002306 if (wpa_s->hw.modes == NULL) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002307 wpa_printf(MSG_DEBUG, "P2P: Driver did not support fetching "
2308 "of all supported channels; assume dualband "
2309 "support");
2310 return wpas_p2p_default_channels(wpa_s, chan);
2311 }
2312
2313 cla = 0;
2314
2315 for (op = 0; op_class[op].op_class; op++) {
2316 struct p2p_oper_class_map *o = &op_class[op];
2317 u8 ch;
2318 struct p2p_reg_class *reg = NULL;
2319
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002320 mode = get_mode(wpa_s->hw.modes, wpa_s->hw.num_modes, o->mode);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002321 if (mode == NULL)
2322 continue;
2323 for (ch = o->min_chan; ch <= o->max_chan; ch += o->inc) {
2324 int flag;
Dmitry Shmidt04949592012-07-19 12:16:46 -07002325 if (!has_channel(wpa_s->global, mode, ch, &flag))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002326 continue;
2327 if (o->bw == BW40MINUS &&
2328 (!(flag & HOSTAPD_CHAN_HT40MINUS) ||
Dmitry Shmidt04949592012-07-19 12:16:46 -07002329 !has_channel(wpa_s->global, mode, ch - 4, NULL)))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002330 continue;
2331 if (o->bw == BW40PLUS &&
2332 (!(flag & HOSTAPD_CHAN_HT40PLUS) ||
Dmitry Shmidt04949592012-07-19 12:16:46 -07002333 !has_channel(wpa_s->global, mode, ch + 4, NULL)))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002334 continue;
2335 if (reg == NULL) {
2336 wpa_printf(MSG_DEBUG, "P2P: Add operating "
2337 "class %u", o->op_class);
2338 reg = &chan->reg_class[cla];
2339 cla++;
2340 reg->reg_class = o->op_class;
2341 }
2342 reg->channel[reg->channels] = ch;
2343 reg->channels++;
2344 }
2345 if (reg) {
2346 wpa_hexdump(MSG_DEBUG, "P2P: Channels",
2347 reg->channel, reg->channels);
2348 }
2349 }
2350
2351 chan->reg_classes = cla;
2352
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002353 return 0;
2354}
2355
2356
2357static int wpas_get_noa(void *ctx, const u8 *interface_addr, u8 *buf,
2358 size_t buf_len)
2359{
2360 struct wpa_supplicant *wpa_s = ctx;
2361
2362 for (wpa_s = wpa_s->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
2363 if (os_memcmp(wpa_s->own_addr, interface_addr, ETH_ALEN) == 0)
2364 break;
2365 }
2366 if (wpa_s == NULL)
2367 return -1;
2368
2369 return wpa_drv_get_noa(wpa_s, buf, buf_len);
2370}
2371
2372
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08002373static int wpas_go_connected(void *ctx, const u8 *dev_addr)
2374{
2375 struct wpa_supplicant *wpa_s = ctx;
2376
2377 for (wpa_s = wpa_s->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
2378 struct wpa_ssid *ssid = wpa_s->current_ssid;
2379 if (ssid == NULL)
2380 continue;
2381 if (ssid->mode != WPAS_MODE_INFRA)
2382 continue;
2383 if (wpa_s->wpa_state != WPA_COMPLETED &&
2384 wpa_s->wpa_state != WPA_GROUP_HANDSHAKE)
2385 continue;
2386 if (os_memcmp(wpa_s->go_dev_addr, dev_addr, ETH_ALEN) == 0)
2387 return 1;
2388 }
2389
2390 return 0;
2391}
2392
2393
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002394/**
2395 * wpas_p2p_init - Initialize P2P module for %wpa_supplicant
2396 * @global: Pointer to global data from wpa_supplicant_init()
2397 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
2398 * Returns: 0 on success, -1 on failure
2399 */
2400int wpas_p2p_init(struct wpa_global *global, struct wpa_supplicant *wpa_s)
2401{
2402 struct p2p_config p2p;
2403 unsigned int r;
2404 int i;
2405
2406 if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_CAPABLE))
2407 return 0;
2408
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002409 if (global->p2p)
2410 return 0;
2411
2412 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT) {
2413 struct p2p_params params;
2414
2415 wpa_printf(MSG_DEBUG, "P2P: Use driver-based P2P management");
2416 os_memset(&params, 0, sizeof(params));
2417 params.dev_name = wpa_s->conf->device_name;
2418 os_memcpy(params.pri_dev_type, wpa_s->conf->device_type,
2419 WPS_DEV_TYPE_LEN);
2420 params.num_sec_dev_types = wpa_s->conf->num_sec_device_types;
2421 os_memcpy(params.sec_dev_type,
2422 wpa_s->conf->sec_device_type,
2423 params.num_sec_dev_types * WPS_DEV_TYPE_LEN);
2424
2425 if (wpa_drv_p2p_set_params(wpa_s, &params) < 0)
2426 return -1;
2427
2428 return 0;
2429 }
2430
2431 os_memset(&p2p, 0, sizeof(p2p));
2432 p2p.msg_ctx = wpa_s;
2433 p2p.cb_ctx = wpa_s;
2434 p2p.p2p_scan = wpas_p2p_scan;
2435 p2p.send_action = wpas_send_action;
2436 p2p.send_action_done = wpas_send_action_done;
2437 p2p.go_neg_completed = wpas_go_neg_completed;
2438 p2p.go_neg_req_rx = wpas_go_neg_req_rx;
2439 p2p.dev_found = wpas_dev_found;
2440 p2p.dev_lost = wpas_dev_lost;
2441 p2p.start_listen = wpas_start_listen;
2442 p2p.stop_listen = wpas_stop_listen;
2443 p2p.send_probe_resp = wpas_send_probe_resp;
2444 p2p.sd_request = wpas_sd_request;
2445 p2p.sd_response = wpas_sd_response;
2446 p2p.prov_disc_req = wpas_prov_disc_req;
2447 p2p.prov_disc_resp = wpas_prov_disc_resp;
Jouni Malinen75ecf522011-06-27 15:19:46 -07002448 p2p.prov_disc_fail = wpas_prov_disc_fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002449 p2p.invitation_process = wpas_invitation_process;
2450 p2p.invitation_received = wpas_invitation_received;
2451 p2p.invitation_result = wpas_invitation_result;
2452 p2p.get_noa = wpas_get_noa;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08002453 p2p.go_connected = wpas_go_connected;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002454
2455 os_memcpy(wpa_s->global->p2p_dev_addr, wpa_s->own_addr, ETH_ALEN);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002456 os_memcpy(p2p.dev_addr, wpa_s->global->p2p_dev_addr, ETH_ALEN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002457 p2p.dev_name = wpa_s->conf->device_name;
2458 p2p.manufacturer = wpa_s->conf->manufacturer;
2459 p2p.model_name = wpa_s->conf->model_name;
2460 p2p.model_number = wpa_s->conf->model_number;
2461 p2p.serial_number = wpa_s->conf->serial_number;
2462 if (wpa_s->wps) {
2463 os_memcpy(p2p.uuid, wpa_s->wps->uuid, 16);
2464 p2p.config_methods = wpa_s->wps->config_methods;
2465 }
2466
2467 if (wpa_s->conf->p2p_listen_reg_class &&
2468 wpa_s->conf->p2p_listen_channel) {
2469 p2p.reg_class = wpa_s->conf->p2p_listen_reg_class;
2470 p2p.channel = wpa_s->conf->p2p_listen_channel;
2471 } else {
2472 p2p.reg_class = 81;
2473 /*
2474 * Pick one of the social channels randomly as the listen
2475 * channel.
2476 */
2477 os_get_random((u8 *) &r, sizeof(r));
2478 p2p.channel = 1 + (r % 3) * 5;
2479 }
2480 wpa_printf(MSG_DEBUG, "P2P: Own listen channel: %d", p2p.channel);
2481
2482 if (wpa_s->conf->p2p_oper_reg_class &&
2483 wpa_s->conf->p2p_oper_channel) {
2484 p2p.op_reg_class = wpa_s->conf->p2p_oper_reg_class;
2485 p2p.op_channel = wpa_s->conf->p2p_oper_channel;
2486 p2p.cfg_op_channel = 1;
2487 wpa_printf(MSG_DEBUG, "P2P: Configured operating channel: "
2488 "%d:%d", p2p.op_reg_class, p2p.op_channel);
2489
2490 } else {
2491 p2p.op_reg_class = 81;
2492 /*
2493 * Use random operation channel from (1, 6, 11) if no other
2494 * preference is indicated.
2495 */
2496 os_get_random((u8 *) &r, sizeof(r));
2497 p2p.op_channel = 1 + (r % 3) * 5;
2498 p2p.cfg_op_channel = 0;
2499 wpa_printf(MSG_DEBUG, "P2P: Random operating channel: "
2500 "%d:%d", p2p.op_reg_class, p2p.op_channel);
2501 }
2502 if (wpa_s->conf->country[0] && wpa_s->conf->country[1]) {
2503 os_memcpy(p2p.country, wpa_s->conf->country, 2);
2504 p2p.country[2] = 0x04;
2505 } else
2506 os_memcpy(p2p.country, "XX\x04", 3);
2507
2508 if (wpas_p2p_setup_channels(wpa_s, &p2p.channels)) {
2509 wpa_printf(MSG_ERROR, "P2P: Failed to configure supported "
2510 "channel list");
2511 return -1;
2512 }
2513
2514 os_memcpy(p2p.pri_dev_type, wpa_s->conf->device_type,
2515 WPS_DEV_TYPE_LEN);
2516
2517 p2p.num_sec_dev_types = wpa_s->conf->num_sec_device_types;
2518 os_memcpy(p2p.sec_dev_type, wpa_s->conf->sec_device_type,
2519 p2p.num_sec_dev_types * WPS_DEV_TYPE_LEN);
2520
2521 p2p.concurrent_operations = !!(wpa_s->drv_flags &
2522 WPA_DRIVER_FLAGS_P2P_CONCURRENT);
2523
2524 p2p.max_peers = 100;
2525
2526 if (wpa_s->conf->p2p_ssid_postfix) {
2527 p2p.ssid_postfix_len =
2528 os_strlen(wpa_s->conf->p2p_ssid_postfix);
2529 if (p2p.ssid_postfix_len > sizeof(p2p.ssid_postfix))
2530 p2p.ssid_postfix_len = sizeof(p2p.ssid_postfix);
2531 os_memcpy(p2p.ssid_postfix, wpa_s->conf->p2p_ssid_postfix,
2532 p2p.ssid_postfix_len);
2533 }
2534
2535 p2p.p2p_intra_bss = wpa_s->conf->p2p_intra_bss;
2536
2537 global->p2p = p2p_init(&p2p);
2538 if (global->p2p == NULL)
2539 return -1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07002540 global->p2p_init_wpa_s = wpa_s;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002541
2542 for (i = 0; i < MAX_WPS_VENDOR_EXT; i++) {
2543 if (wpa_s->conf->wps_vendor_ext[i] == NULL)
2544 continue;
2545 p2p_add_wps_vendor_extension(
2546 global->p2p, wpa_s->conf->wps_vendor_ext[i]);
2547 }
2548
2549 return 0;
2550}
2551
2552
2553/**
2554 * wpas_p2p_deinit - Deinitialize per-interface P2P data
2555 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
2556 *
2557 * This function deinitialize per-interface P2P data.
2558 */
2559void wpas_p2p_deinit(struct wpa_supplicant *wpa_s)
2560{
2561 if (wpa_s->driver && wpa_s->drv_priv)
2562 wpa_drv_probe_req_report(wpa_s, 0);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002563
2564 if (wpa_s->go_params) {
2565 /* Clear any stored provisioning info */
2566 p2p_clear_provisioning_info(
2567 wpa_s->global->p2p,
Dmitry Shmidt04949592012-07-19 12:16:46 -07002568 wpa_s->go_params->peer_device_addr);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002569 }
2570
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002571 os_free(wpa_s->go_params);
2572 wpa_s->go_params = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002573 eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s, NULL);
2574 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08002575 eloop_cancel_timeout(wpas_p2p_pd_before_join_timeout, wpa_s, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002576 wpa_s->p2p_long_listen = 0;
2577 eloop_cancel_timeout(wpas_p2p_long_listen_timeout, wpa_s, NULL);
2578 eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL);
2579 wpas_p2p_remove_pending_group_interface(wpa_s);
2580
2581 /* TODO: remove group interface from the driver if this wpa_s instance
2582 * is on top of a P2P group interface */
2583}
2584
2585
2586/**
2587 * wpas_p2p_deinit_global - Deinitialize global P2P module
2588 * @global: Pointer to global data from wpa_supplicant_init()
2589 *
2590 * This function deinitializes the global (per device) P2P module.
2591 */
2592void wpas_p2p_deinit_global(struct wpa_global *global)
2593{
2594 struct wpa_supplicant *wpa_s, *tmp;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002595
2596 if (global->p2p == NULL)
2597 return;
2598
2599 /* Remove remaining P2P group interfaces */
2600 wpa_s = global->ifaces;
2601 if (wpa_s)
2602 wpas_p2p_service_flush(wpa_s);
2603 while (wpa_s && wpa_s->p2p_group_interface != NOT_P2P_GROUP_INTERFACE)
2604 wpa_s = wpa_s->next;
2605 while (wpa_s) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002606 tmp = global->ifaces;
2607 while (tmp &&
2608 (tmp == wpa_s ||
2609 tmp->p2p_group_interface == NOT_P2P_GROUP_INTERFACE)) {
2610 tmp = tmp->next;
2611 }
2612 if (tmp == NULL)
2613 break;
Dmitry Shmidt04949592012-07-19 12:16:46 -07002614 /* Disconnect from the P2P group and deinit the interface */
2615 wpas_p2p_disconnect(tmp);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002616 }
2617
2618 /*
2619 * Deinit GO data on any possibly remaining interface (if main
2620 * interface is used as GO).
2621 */
2622 for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
2623 if (wpa_s->ap_iface)
2624 wpas_p2p_group_deinit(wpa_s);
2625 }
2626
2627 p2p_deinit(global->p2p);
2628 global->p2p = NULL;
Dmitry Shmidt04949592012-07-19 12:16:46 -07002629 global->p2p_init_wpa_s = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002630}
2631
2632
2633static int wpas_p2p_create_iface(struct wpa_supplicant *wpa_s)
2634{
2635 if (wpa_s->drv_flags &
2636 (WPA_DRIVER_FLAGS_P2P_DEDICATED_INTERFACE |
2637 WPA_DRIVER_FLAGS_P2P_MGMT_AND_NON_P2P))
2638 return 1; /* P2P group requires a new interface in every case
2639 */
2640 if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_CONCURRENT))
2641 return 0; /* driver does not support concurrent operations */
2642 if (wpa_s->global->ifaces->next)
2643 return 1; /* more that one interface already in use */
2644 if (wpa_s->wpa_state >= WPA_AUTHENTICATING)
2645 return 1; /* this interface is already in use */
2646 return 0;
2647}
2648
2649
2650static int wpas_p2p_start_go_neg(struct wpa_supplicant *wpa_s,
2651 const u8 *peer_addr,
2652 enum p2p_wps_method wps_method,
2653 int go_intent, const u8 *own_interface_addr,
Dmitry Shmidt04949592012-07-19 12:16:46 -07002654 unsigned int force_freq, int persistent_group,
2655 struct wpa_ssid *ssid)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002656{
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002657 if (persistent_group && wpa_s->conf->persistent_reconnect)
2658 persistent_group = 2;
2659
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002660 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT) {
2661 return wpa_drv_p2p_connect(wpa_s, peer_addr, wps_method,
2662 go_intent, own_interface_addr,
2663 force_freq, persistent_group);
2664 }
2665
2666 return p2p_connect(wpa_s->global->p2p, peer_addr, wps_method,
2667 go_intent, own_interface_addr, force_freq,
Dmitry Shmidt04949592012-07-19 12:16:46 -07002668 persistent_group, ssid ? ssid->ssid : NULL,
2669 ssid ? ssid->ssid_len : 0,
2670 wpa_s->p2p_pd_before_go_neg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002671}
2672
2673
2674static int wpas_p2p_auth_go_neg(struct wpa_supplicant *wpa_s,
2675 const u8 *peer_addr,
2676 enum p2p_wps_method wps_method,
2677 int go_intent, const u8 *own_interface_addr,
Dmitry Shmidt04949592012-07-19 12:16:46 -07002678 unsigned int force_freq, int persistent_group,
2679 struct wpa_ssid *ssid)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002680{
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002681 if (persistent_group && wpa_s->conf->persistent_reconnect)
2682 persistent_group = 2;
2683
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002684 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
2685 return -1;
2686
2687 return p2p_authorize(wpa_s->global->p2p, peer_addr, wps_method,
2688 go_intent, own_interface_addr, force_freq,
Dmitry Shmidt04949592012-07-19 12:16:46 -07002689 persistent_group, ssid ? ssid->ssid : NULL,
2690 ssid ? ssid->ssid_len : 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002691}
2692
2693
2694static void wpas_p2p_check_join_scan_limit(struct wpa_supplicant *wpa_s)
2695{
2696 wpa_s->p2p_join_scan_count++;
2697 wpa_printf(MSG_DEBUG, "P2P: Join scan attempt %d",
2698 wpa_s->p2p_join_scan_count);
2699 if (wpa_s->p2p_join_scan_count > P2P_MAX_JOIN_SCAN_ATTEMPTS) {
2700 wpa_printf(MSG_DEBUG, "P2P: Failed to find GO " MACSTR
2701 " for join operationg - stop join attempt",
2702 MAC2STR(wpa_s->pending_join_iface_addr));
2703 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
Dmitry Shmidt04949592012-07-19 12:16:46 -07002704 if (wpa_s->p2p_auto_pd) {
2705 wpa_s->p2p_auto_pd = 0;
2706 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_FAILURE
2707 " p2p_dev_addr=" MACSTR " status=N/A",
2708 MAC2STR(wpa_s->pending_join_dev_addr));
2709 return;
2710 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002711 wpa_msg(wpa_s->parent, MSG_INFO,
2712 P2P_EVENT_GROUP_FORMATION_FAILURE);
2713 }
2714}
2715
2716
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08002717static void wpas_p2p_pd_before_join_timeout(void *eloop_ctx, void *timeout_ctx)
2718{
2719 struct wpa_supplicant *wpa_s = eloop_ctx;
2720 if (!wpa_s->pending_pd_before_join)
2721 return;
2722 /*
2723 * Provision Discovery Response may have been lost - try to connect
2724 * anyway since we do not need any information from this PD.
2725 */
2726 wpa_printf(MSG_DEBUG, "P2P: PD timeout for join-existing-group - "
2727 "try to connect anyway");
2728 wpas_p2p_join_start(wpa_s);
2729}
2730
2731
Dmitry Shmidt04949592012-07-19 12:16:46 -07002732static int wpas_check_freq_conflict(struct wpa_supplicant *wpa_s, int freq)
2733{
2734 struct wpa_supplicant *iface;
2735 int shared_freq;
2736 u8 bssid[ETH_ALEN];
2737
2738 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_MULTI_CHANNEL_CONCURRENT)
2739 return 0;
2740
2741 for (iface = wpa_s->global->ifaces; iface; iface = iface->next) {
2742 if (!wpas_p2p_create_iface(wpa_s) && iface == wpa_s)
2743 continue;
2744 if (iface->current_ssid == NULL || iface->assoc_freq == 0)
2745 continue;
2746 if (iface->current_ssid->mode == WPAS_MODE_AP ||
2747 iface->current_ssid->mode == WPAS_MODE_P2P_GO)
2748 shared_freq = iface->current_ssid->frequency;
2749 else if (wpa_drv_get_bssid(iface, bssid) == 0)
2750 shared_freq = iface->assoc_freq;
2751 else
2752 shared_freq = 0;
2753
2754 if (shared_freq && freq != shared_freq) {
2755 wpa_printf(MSG_DEBUG, "P2P: Frequency conflict - %s "
2756 "connected on %d MHz - new connection on "
2757 "%d MHz", iface->ifname, shared_freq, freq);
2758 return 1;
2759 }
2760 }
2761
2762 shared_freq = wpa_drv_shared_freq(wpa_s);
2763 if (shared_freq > 0 && shared_freq != freq) {
2764 wpa_printf(MSG_DEBUG, "P2P: Frequency conflict - shared "
2765 "virtual interface connected on %d MHz - new "
2766 "connection on %d MHz", shared_freq, freq);
2767 return 1;
2768 }
2769
2770 return 0;
2771}
2772
2773
2774static int wpas_p2p_peer_go(struct wpa_supplicant *wpa_s,
2775 const u8 *peer_dev_addr)
2776{
2777 struct wpa_bss *bss;
2778 int updated;
2779
2780 bss = wpa_bss_get_p2p_dev_addr(wpa_s, peer_dev_addr);
2781 if (bss == NULL)
2782 return -1;
2783 if (bss->last_update_idx < wpa_s->bss_update_idx) {
2784 wpa_printf(MSG_DEBUG, "P2P: Peer BSS entry not updated in the "
2785 "last scan");
2786 return 0;
2787 }
2788
2789 updated = os_time_before(&wpa_s->p2p_auto_started, &bss->last_update);
2790 wpa_printf(MSG_DEBUG, "P2P: Current BSS entry for peer updated at "
2791 "%ld.%06ld (%supdated in last scan)",
2792 bss->last_update.sec, bss->last_update.usec,
2793 updated ? "": "not ");
2794
2795 return updated;
2796}
2797
2798
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002799static void wpas_p2p_scan_res_join(struct wpa_supplicant *wpa_s,
2800 struct wpa_scan_results *scan_res)
2801{
2802 struct wpa_bss *bss;
2803 int freq;
2804 u8 iface_addr[ETH_ALEN];
Dmitry Shmidt04949592012-07-19 12:16:46 -07002805
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002806 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
2807
2808 if (wpa_s->global->p2p_disabled)
2809 return;
2810
Dmitry Shmidt04949592012-07-19 12:16:46 -07002811 wpa_printf(MSG_DEBUG, "P2P: Scan results received (%d BSS) for %sjoin",
2812 scan_res ? (int) scan_res->num : -1,
2813 wpa_s->p2p_auto_join ? "auto_" : "");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002814
2815 if (scan_res)
2816 wpas_p2p_scan_res_handler(wpa_s, scan_res);
2817
Dmitry Shmidt04949592012-07-19 12:16:46 -07002818 if (wpa_s->p2p_auto_pd) {
2819 int join = wpas_p2p_peer_go(wpa_s,
2820 wpa_s->pending_join_dev_addr);
2821 if (join == 0 &&
2822 wpa_s->auto_pd_scan_retry < P2P_AUTO_PD_SCAN_ATTEMPTS) {
2823 wpa_s->auto_pd_scan_retry++;
2824 bss = wpa_bss_get_bssid(wpa_s,
2825 wpa_s->pending_join_dev_addr);
2826 if (bss) {
2827 freq = bss->freq;
2828 wpa_printf(MSG_DEBUG, "P2P: Scan retry %d for "
2829 "the peer " MACSTR " at %d MHz",
2830 wpa_s->auto_pd_scan_retry,
2831 MAC2STR(wpa_s->
2832 pending_join_dev_addr),
2833 freq);
2834 wpas_p2p_join_scan_req(wpa_s, freq);
2835 return;
2836 }
2837 }
2838
2839 if (join < 0)
2840 join = 0;
2841
2842 wpa_s->p2p_auto_pd = 0;
2843 wpa_s->pending_pd_use = join ? AUTO_PD_JOIN : AUTO_PD_GO_NEG;
2844 wpa_printf(MSG_DEBUG, "P2P: Auto PD with " MACSTR " join=%d",
2845 MAC2STR(wpa_s->pending_join_dev_addr), join);
2846 if (p2p_prov_disc_req(wpa_s->global->p2p,
2847 wpa_s->pending_join_dev_addr,
2848 wpa_s->pending_pd_config_methods, join,
2849 0) < 0) {
2850 wpa_s->p2p_auto_pd = 0;
2851 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_FAILURE
2852 " p2p_dev_addr=" MACSTR " status=N/A",
2853 MAC2STR(wpa_s->pending_join_dev_addr));
2854 }
2855 return;
2856 }
2857
2858 if (wpa_s->p2p_auto_join) {
2859 int join = wpas_p2p_peer_go(wpa_s,
2860 wpa_s->pending_join_dev_addr);
2861 if (join < 0) {
2862 wpa_printf(MSG_DEBUG, "P2P: Peer was not found to be "
2863 "running a GO -> use GO Negotiation");
2864 wpas_p2p_connect(wpa_s, wpa_s->pending_join_dev_addr,
2865 wpa_s->p2p_pin, wpa_s->p2p_wps_method,
2866 wpa_s->p2p_persistent_group, 0, 0, 0,
2867 wpa_s->p2p_go_intent,
2868 wpa_s->p2p_connect_freq,
2869 wpa_s->p2p_persistent_id,
2870 wpa_s->p2p_pd_before_go_neg);
2871 return;
2872 }
2873
2874 wpa_printf(MSG_DEBUG, "P2P: Peer was found running GO%s -> "
2875 "try to join the group", join ? "" :
2876 " in older scan");
2877 if (!join)
2878 wpa_s->p2p_fallback_to_go_neg = 1;
2879 }
2880
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002881 freq = p2p_get_oper_freq(wpa_s->global->p2p,
2882 wpa_s->pending_join_iface_addr);
2883 if (freq < 0 &&
2884 p2p_get_interface_addr(wpa_s->global->p2p,
2885 wpa_s->pending_join_dev_addr,
2886 iface_addr) == 0 &&
2887 os_memcmp(iface_addr, wpa_s->pending_join_dev_addr, ETH_ALEN) != 0)
2888 {
2889 wpa_printf(MSG_DEBUG, "P2P: Overwrite pending interface "
2890 "address for join from " MACSTR " to " MACSTR
2891 " based on newly discovered P2P peer entry",
2892 MAC2STR(wpa_s->pending_join_iface_addr),
2893 MAC2STR(iface_addr));
2894 os_memcpy(wpa_s->pending_join_iface_addr, iface_addr,
2895 ETH_ALEN);
2896
2897 freq = p2p_get_oper_freq(wpa_s->global->p2p,
2898 wpa_s->pending_join_iface_addr);
2899 }
2900 if (freq >= 0) {
2901 wpa_printf(MSG_DEBUG, "P2P: Target GO operating frequency "
2902 "from P2P peer table: %d MHz", freq);
2903 }
2904 bss = wpa_bss_get_bssid(wpa_s, wpa_s->pending_join_iface_addr);
2905 if (bss) {
2906 freq = bss->freq;
2907 wpa_printf(MSG_DEBUG, "P2P: Target GO operating frequency "
2908 "from BSS table: %d MHz", freq);
2909 }
2910 if (freq > 0) {
2911 u16 method;
2912
Dmitry Shmidt04949592012-07-19 12:16:46 -07002913 if (wpas_check_freq_conflict(wpa_s, freq) > 0) {
2914 wpa_msg(wpa_s->parent, MSG_INFO,
2915 P2P_EVENT_GROUP_FORMATION_FAILURE
2916 "reason=FREQ_CONFLICT");
2917 return;
2918 }
2919
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002920 wpa_printf(MSG_DEBUG, "P2P: Send Provision Discovery Request "
2921 "prior to joining an existing group (GO " MACSTR
2922 " freq=%u MHz)",
2923 MAC2STR(wpa_s->pending_join_dev_addr), freq);
2924 wpa_s->pending_pd_before_join = 1;
2925
2926 switch (wpa_s->pending_join_wps_method) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002927 case WPS_PIN_DISPLAY:
2928 method = WPS_CONFIG_KEYPAD;
2929 break;
2930 case WPS_PIN_KEYPAD:
2931 method = WPS_CONFIG_DISPLAY;
2932 break;
2933 case WPS_PBC:
2934 method = WPS_CONFIG_PUSHBUTTON;
2935 break;
2936 default:
2937 method = 0;
2938 break;
2939 }
2940
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002941 if ((p2p_get_provisioning_info(wpa_s->global->p2p,
2942 wpa_s->pending_join_dev_addr) ==
2943 method)) {
2944 /*
2945 * We have already performed provision discovery for
2946 * joining the group. Proceed directly to join
2947 * operation without duplicated provision discovery. */
2948 wpa_printf(MSG_DEBUG, "P2P: Provision discovery "
2949 "with " MACSTR " already done - proceed to "
2950 "join",
2951 MAC2STR(wpa_s->pending_join_dev_addr));
2952 wpa_s->pending_pd_before_join = 0;
2953 goto start;
2954 }
2955
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002956 if (p2p_prov_disc_req(wpa_s->global->p2p,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002957 wpa_s->pending_join_dev_addr, method, 1,
2958 freq) < 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002959 wpa_printf(MSG_DEBUG, "P2P: Failed to send Provision "
2960 "Discovery Request before joining an "
2961 "existing group");
2962 wpa_s->pending_pd_before_join = 0;
2963 goto start;
2964 }
2965
2966 /*
2967 * Actual join operation will be started from the Action frame
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08002968 * TX status callback (if no ACK is received) or when the
2969 * Provision Discovery Response is received. Use a short
2970 * timeout as a backup mechanism should the Provision Discovery
2971 * Response be lost for any reason.
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002972 */
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08002973 eloop_cancel_timeout(wpas_p2p_pd_before_join_timeout, wpa_s,
2974 NULL);
2975 eloop_register_timeout(2, 0, wpas_p2p_pd_before_join_timeout,
2976 wpa_s, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002977 return;
2978 }
2979
2980 wpa_printf(MSG_DEBUG, "P2P: Failed to find BSS/GO - try again later");
2981 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
2982 eloop_register_timeout(1, 0, wpas_p2p_join_scan, wpa_s, NULL);
2983 wpas_p2p_check_join_scan_limit(wpa_s);
2984 return;
2985
2986start:
2987 /* Start join operation immediately */
2988 wpas_p2p_join_start(wpa_s);
2989}
2990
2991
Dmitry Shmidt04949592012-07-19 12:16:46 -07002992static void wpas_p2p_join_scan_req(struct wpa_supplicant *wpa_s, int freq)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002993{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002994 int ret;
2995 struct wpa_driver_scan_params params;
2996 struct wpabuf *wps_ie, *ies;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002997 size_t ielen;
Dmitry Shmidt04949592012-07-19 12:16:46 -07002998 int freqs[2] = { 0, 0 };
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002999
3000 os_memset(&params, 0, sizeof(params));
3001
3002 /* P2P Wildcard SSID */
3003 params.num_ssids = 1;
3004 params.ssids[0].ssid = (u8 *) P2P_WILDCARD_SSID;
3005 params.ssids[0].ssid_len = P2P_WILDCARD_SSID_LEN;
3006
3007 wpa_s->wps->dev.p2p = 1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003008 wps_ie = wps_build_probe_req_ie(DEV_PW_DEFAULT, &wpa_s->wps->dev,
3009 wpa_s->wps->uuid, WPS_REQ_ENROLLEE, 0,
3010 NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003011 if (wps_ie == NULL) {
3012 wpas_p2p_scan_res_join(wpa_s, NULL);
3013 return;
3014 }
3015
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003016 ielen = p2p_scan_ie_buf_len(wpa_s->global->p2p);
3017 ies = wpabuf_alloc(wpabuf_len(wps_ie) + ielen);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003018 if (ies == NULL) {
3019 wpabuf_free(wps_ie);
3020 wpas_p2p_scan_res_join(wpa_s, NULL);
3021 return;
3022 }
3023 wpabuf_put_buf(ies, wps_ie);
3024 wpabuf_free(wps_ie);
3025
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08003026 p2p_scan_ie(wpa_s->global->p2p, ies, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003027
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003028 params.p2p_probe = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003029 params.extra_ies = wpabuf_head(ies);
3030 params.extra_ies_len = wpabuf_len(ies);
Dmitry Shmidt04949592012-07-19 12:16:46 -07003031 if (freq > 0) {
3032 freqs[0] = freq;
3033 params.freqs = freqs;
3034 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003035
3036 /*
3037 * Run a scan to update BSS table and start Provision Discovery once
3038 * the new scan results become available.
3039 */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003040 ret = wpa_drv_scan(wpa_s, &params);
Dmitry Shmidt04949592012-07-19 12:16:46 -07003041 if (!ret)
3042 wpa_s->scan_res_handler = wpas_p2p_scan_res_join;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003043
3044 wpabuf_free(ies);
3045
3046 if (ret) {
3047 wpa_printf(MSG_DEBUG, "P2P: Failed to start scan for join - "
3048 "try again later");
3049 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
3050 eloop_register_timeout(1, 0, wpas_p2p_join_scan, wpa_s, NULL);
3051 wpas_p2p_check_join_scan_limit(wpa_s);
3052 }
3053}
3054
3055
Dmitry Shmidt04949592012-07-19 12:16:46 -07003056static void wpas_p2p_join_scan(void *eloop_ctx, void *timeout_ctx)
3057{
3058 struct wpa_supplicant *wpa_s = eloop_ctx;
3059 wpas_p2p_join_scan_req(wpa_s, 0);
3060}
3061
3062
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003063static int wpas_p2p_join(struct wpa_supplicant *wpa_s, const u8 *iface_addr,
Dmitry Shmidt04949592012-07-19 12:16:46 -07003064 const u8 *dev_addr, enum p2p_wps_method wps_method,
3065 int auto_join)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003066{
3067 wpa_printf(MSG_DEBUG, "P2P: Request to join existing group (iface "
Dmitry Shmidt04949592012-07-19 12:16:46 -07003068 MACSTR " dev " MACSTR ")%s",
3069 MAC2STR(iface_addr), MAC2STR(dev_addr),
3070 auto_join ? " (auto_join)" : "");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003071
Dmitry Shmidt04949592012-07-19 12:16:46 -07003072 wpa_s->p2p_auto_pd = 0;
3073 wpa_s->p2p_auto_join = !!auto_join;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003074 os_memcpy(wpa_s->pending_join_iface_addr, iface_addr, ETH_ALEN);
3075 os_memcpy(wpa_s->pending_join_dev_addr, dev_addr, ETH_ALEN);
3076 wpa_s->pending_join_wps_method = wps_method;
3077
3078 /* Make sure we are not running find during connection establishment */
3079 wpas_p2p_stop_find(wpa_s);
3080
3081 wpa_s->p2p_join_scan_count = 0;
3082 wpas_p2p_join_scan(wpa_s, NULL);
3083 return 0;
3084}
3085
3086
3087static int wpas_p2p_join_start(struct wpa_supplicant *wpa_s)
3088{
3089 struct wpa_supplicant *group;
3090 struct p2p_go_neg_results res;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003091 struct wpa_bss *bss;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003092
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08003093 eloop_cancel_timeout(wpas_p2p_pd_before_join_timeout, wpa_s, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003094 group = wpas_p2p_get_group_iface(wpa_s, 0, 0);
3095 if (group == NULL)
3096 return -1;
3097 if (group != wpa_s) {
3098 os_memcpy(group->p2p_pin, wpa_s->p2p_pin,
3099 sizeof(group->p2p_pin));
3100 group->p2p_wps_method = wpa_s->p2p_wps_method;
3101 }
3102
3103 group->p2p_in_provisioning = 1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003104 group->p2p_fallback_to_go_neg = wpa_s->p2p_fallback_to_go_neg;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003105
3106 os_memset(&res, 0, sizeof(res));
3107 os_memcpy(res.peer_interface_addr, wpa_s->pending_join_iface_addr,
3108 ETH_ALEN);
3109 res.wps_method = wpa_s->pending_join_wps_method;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003110 bss = wpa_bss_get_bssid(wpa_s, wpa_s->pending_join_iface_addr);
3111 if (bss) {
3112 res.freq = bss->freq;
3113 res.ssid_len = bss->ssid_len;
3114 os_memcpy(res.ssid, bss->ssid, bss->ssid_len);
3115 }
3116
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08003117 if (wpa_s->off_channel_freq || wpa_s->roc_waiting_drv_freq) {
3118 wpa_printf(MSG_DEBUG, "P2P: Cancel remain-on-channel prior to "
3119 "starting client");
3120 wpa_drv_cancel_remain_on_channel(wpa_s);
3121 wpa_s->off_channel_freq = 0;
3122 wpa_s->roc_waiting_drv_freq = 0;
3123 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003124 wpas_start_wps_enrollee(group, &res);
3125
3126 /*
3127 * Allow a longer timeout for join-a-running-group than normal 15
3128 * second group formation timeout since the GO may not have authorized
3129 * our connection yet.
3130 */
3131 eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s, NULL);
3132 eloop_register_timeout(60, 0, wpas_p2p_group_formation_timeout,
3133 wpa_s, NULL);
3134
3135 return 0;
3136}
3137
3138
3139/**
3140 * wpas_p2p_connect - Request P2P Group Formation to be started
3141 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
3142 * @peer_addr: Address of the peer P2P Device
3143 * @pin: PIN to use during provisioning or %NULL to indicate PBC mode
3144 * @persistent_group: Whether to create a persistent group
Dmitry Shmidt04949592012-07-19 12:16:46 -07003145 * @auto_join: Whether to select join vs. GO Negotiation automatically
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003146 * @join: Whether to join an existing group (as a client) instead of starting
3147 * Group Owner negotiation; @peer_addr is BSSID in that case
3148 * @auth: Whether to only authorize the connection instead of doing that and
3149 * initiating Group Owner negotiation
3150 * @go_intent: GO Intent or -1 to use default
3151 * @freq: Frequency for the group or 0 for auto-selection
Dmitry Shmidt04949592012-07-19 12:16:46 -07003152 * @persistent_id: Persistent group credentials to use for forcing GO
3153 * parameters or -1 to generate new values (SSID/passphrase)
3154 * @pd: Whether to send Provision Discovery prior to GO Negotiation as an
3155 * interoperability workaround when initiating group formation
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003156 * Returns: 0 or new PIN (if pin was %NULL) on success, -1 on unspecified
3157 * failure, -2 on failure due to channel not currently available,
3158 * -3 if forced channel is not supported
3159 */
3160int wpas_p2p_connect(struct wpa_supplicant *wpa_s, const u8 *peer_addr,
3161 const char *pin, enum p2p_wps_method wps_method,
Dmitry Shmidt04949592012-07-19 12:16:46 -07003162 int persistent_group, int auto_join, int join, int auth,
3163 int go_intent, int freq, int persistent_id, int pd)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003164{
3165 int force_freq = 0, oper_freq = 0;
3166 u8 bssid[ETH_ALEN];
3167 int ret = 0;
3168 enum wpa_driver_if_type iftype;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003169 const u8 *if_addr;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003170 struct wpa_ssid *ssid = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003171
3172 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
3173 return -1;
3174
Dmitry Shmidt04949592012-07-19 12:16:46 -07003175 if (persistent_id >= 0) {
3176 ssid = wpa_config_get_network(wpa_s->conf, persistent_id);
3177 if (ssid == NULL || ssid->disabled != 2 ||
3178 ssid->mode != WPAS_MODE_P2P_GO)
3179 return -1;
3180 }
3181
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003182 if (go_intent < 0)
3183 go_intent = wpa_s->conf->p2p_go_intent;
3184
3185 if (!auth)
3186 wpa_s->p2p_long_listen = 0;
3187
3188 wpa_s->p2p_wps_method = wps_method;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003189 wpa_s->p2p_persistent_group = !!persistent_group;
3190 wpa_s->p2p_persistent_id = persistent_id;
3191 wpa_s->p2p_go_intent = go_intent;
3192 wpa_s->p2p_connect_freq = freq;
3193 wpa_s->p2p_fallback_to_go_neg = 0;
3194 wpa_s->p2p_pd_before_go_neg = !!pd;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003195
3196 if (pin)
3197 os_strlcpy(wpa_s->p2p_pin, pin, sizeof(wpa_s->p2p_pin));
3198 else if (wps_method == WPS_PIN_DISPLAY) {
3199 ret = wps_generate_pin();
3200 os_snprintf(wpa_s->p2p_pin, sizeof(wpa_s->p2p_pin), "%08d",
3201 ret);
3202 wpa_printf(MSG_DEBUG, "P2P: Randomly generated PIN: %s",
3203 wpa_s->p2p_pin);
3204 } else
3205 wpa_s->p2p_pin[0] = '\0';
3206
Dmitry Shmidt04949592012-07-19 12:16:46 -07003207 if (join || auto_join) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003208 u8 iface_addr[ETH_ALEN], dev_addr[ETH_ALEN];
3209 if (auth) {
3210 wpa_printf(MSG_DEBUG, "P2P: Authorize invitation to "
3211 "connect a running group from " MACSTR,
3212 MAC2STR(peer_addr));
3213 os_memcpy(wpa_s->p2p_auth_invite, peer_addr, ETH_ALEN);
3214 return ret;
3215 }
3216 os_memcpy(dev_addr, peer_addr, ETH_ALEN);
3217 if (p2p_get_interface_addr(wpa_s->global->p2p, peer_addr,
3218 iface_addr) < 0) {
3219 os_memcpy(iface_addr, peer_addr, ETH_ALEN);
3220 p2p_get_dev_addr(wpa_s->global->p2p, peer_addr,
3221 dev_addr);
3222 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07003223 if (auto_join) {
3224 os_get_time(&wpa_s->p2p_auto_started);
3225 wpa_printf(MSG_DEBUG, "P2P: Auto join started at "
3226 "%ld.%06ld",
3227 wpa_s->p2p_auto_started.sec,
3228 wpa_s->p2p_auto_started.usec);
3229 }
3230 if (wpas_p2p_join(wpa_s, iface_addr, dev_addr, wps_method,
3231 auto_join) < 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003232 return -1;
3233 return ret;
3234 }
3235
3236 if (wpa_s->current_ssid && wpa_drv_get_bssid(wpa_s, bssid) == 0 &&
3237 wpa_s->assoc_freq)
3238 oper_freq = wpa_s->assoc_freq;
3239 else {
3240 oper_freq = wpa_drv_shared_freq(wpa_s);
3241 if (oper_freq < 0)
3242 oper_freq = 0;
3243 }
3244
3245 if (freq > 0) {
3246 if (!p2p_supported_freq(wpa_s->global->p2p, freq)) {
3247 wpa_printf(MSG_DEBUG, "P2P: The forced channel "
3248 "(%u MHz) is not supported for P2P uses",
3249 freq);
3250 return -3;
3251 }
3252
3253 if (oper_freq > 0 && freq != oper_freq &&
3254 !(wpa_s->drv_flags &
3255 WPA_DRIVER_FLAGS_MULTI_CHANNEL_CONCURRENT)) {
3256 wpa_printf(MSG_DEBUG, "P2P: Cannot start P2P group "
3257 "on %u MHz while connected on another "
3258 "channel (%u MHz)", freq, oper_freq);
3259 return -2;
3260 }
3261 wpa_printf(MSG_DEBUG, "P2P: Trying to force us to use the "
3262 "requested channel (%u MHz)", freq);
3263 force_freq = freq;
3264 } else if (oper_freq > 0 &&
3265 !p2p_supported_freq(wpa_s->global->p2p, oper_freq)) {
3266 if (!(wpa_s->drv_flags &
3267 WPA_DRIVER_FLAGS_MULTI_CHANNEL_CONCURRENT)) {
3268 wpa_printf(MSG_DEBUG, "P2P: Cannot start P2P group "
3269 "while connected on non-P2P supported "
3270 "channel (%u MHz)", oper_freq);
3271 return -2;
3272 }
3273 wpa_printf(MSG_DEBUG, "P2P: Current operating channel "
3274 "(%u MHz) not available for P2P - try to use "
3275 "another channel", oper_freq);
3276 force_freq = 0;
3277 } else if (oper_freq > 0) {
3278 wpa_printf(MSG_DEBUG, "P2P: Trying to force us to use the "
3279 "channel we are already using (%u MHz) on another "
3280 "interface", oper_freq);
3281 force_freq = oper_freq;
3282 }
3283
3284 wpa_s->create_p2p_iface = wpas_p2p_create_iface(wpa_s);
3285
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003286 if (wpa_s->create_p2p_iface) {
3287 /* Prepare to add a new interface for the group */
3288 iftype = WPA_IF_P2P_GROUP;
3289 if (go_intent == 15)
3290 iftype = WPA_IF_P2P_GO;
3291 if (wpas_p2p_add_group_interface(wpa_s, iftype) < 0) {
3292 wpa_printf(MSG_ERROR, "P2P: Failed to allocate a new "
3293 "interface for the group");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003294 return -1;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003295 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003296
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003297 if_addr = wpa_s->pending_interface_addr;
3298 } else
3299 if_addr = wpa_s->own_addr;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003300
3301 if (auth) {
3302 if (wpas_p2p_auth_go_neg(wpa_s, peer_addr, wps_method,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003303 go_intent, if_addr,
Dmitry Shmidt04949592012-07-19 12:16:46 -07003304 force_freq, persistent_group, ssid) <
3305 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003306 return -1;
3307 return ret;
3308 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003309
3310 if (wpas_p2p_start_go_neg(wpa_s, peer_addr, wps_method,
3311 go_intent, if_addr, force_freq,
Dmitry Shmidt04949592012-07-19 12:16:46 -07003312 persistent_group, ssid) < 0) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003313 if (wpa_s->create_p2p_iface)
3314 wpas_p2p_remove_pending_group_interface(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003315 return -1;
3316 }
3317 return ret;
3318}
3319
3320
3321/**
3322 * wpas_p2p_remain_on_channel_cb - Indication of remain-on-channel start
3323 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
3324 * @freq: Frequency of the channel in MHz
3325 * @duration: Duration of the stay on the channel in milliseconds
3326 *
3327 * This callback is called when the driver indicates that it has started the
3328 * requested remain-on-channel duration.
3329 */
3330void wpas_p2p_remain_on_channel_cb(struct wpa_supplicant *wpa_s,
3331 unsigned int freq, unsigned int duration)
3332{
3333 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
3334 return;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003335 if (wpa_s->off_channel_freq == wpa_s->pending_listen_freq) {
3336 p2p_listen_cb(wpa_s->global->p2p, wpa_s->pending_listen_freq,
3337 wpa_s->pending_listen_duration);
3338 wpa_s->pending_listen_freq = 0;
3339 }
3340}
3341
3342
3343static int wpas_p2p_listen_start(struct wpa_supplicant *wpa_s,
3344 unsigned int timeout)
3345{
3346 /* Limit maximum Listen state time based on driver limitation. */
3347 if (timeout > wpa_s->max_remain_on_chan)
3348 timeout = wpa_s->max_remain_on_chan;
3349
3350 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
3351 return wpa_drv_p2p_listen(wpa_s, timeout);
3352
3353 return p2p_listen(wpa_s->global->p2p, timeout);
3354}
3355
3356
3357/**
3358 * wpas_p2p_cancel_remain_on_channel_cb - Remain-on-channel timeout
3359 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
3360 * @freq: Frequency of the channel in MHz
3361 *
3362 * This callback is called when the driver indicates that a remain-on-channel
3363 * operation has been completed, i.e., the duration on the requested channel
3364 * has timed out.
3365 */
3366void wpas_p2p_cancel_remain_on_channel_cb(struct wpa_supplicant *wpa_s,
3367 unsigned int freq)
3368{
3369 wpa_printf(MSG_DEBUG, "P2P: Cancel remain-on-channel callback "
3370 "(p2p_long_listen=%d ms pending_action_tx=%p)",
3371 wpa_s->p2p_long_listen, wpa_s->pending_action_tx);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003372 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
3373 return;
3374 if (p2p_listen_end(wpa_s->global->p2p, freq) > 0)
3375 return; /* P2P module started a new operation */
3376 if (wpa_s->pending_action_tx)
3377 return;
3378 if (wpa_s->p2p_long_listen > 0)
3379 wpa_s->p2p_long_listen -= wpa_s->max_remain_on_chan;
3380 if (wpa_s->p2p_long_listen > 0) {
3381 wpa_printf(MSG_DEBUG, "P2P: Continuing long Listen state");
3382 wpas_p2p_listen_start(wpa_s, wpa_s->p2p_long_listen);
3383 }
3384}
3385
3386
3387/**
3388 * wpas_p2p_group_remove - Remove a P2P group
3389 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
3390 * @ifname: Network interface name of the group interface or "*" to remove all
3391 * groups
3392 * Returns: 0 on success, -1 on failure
3393 *
3394 * This function is used to remove a P2P group. This can be used to disconnect
3395 * from a group in which the local end is a P2P Client or to end a P2P Group in
3396 * case the local end is the Group Owner. If a virtual network interface was
3397 * created for this group, that interface will be removed. Otherwise, only the
3398 * configured P2P group network will be removed from the interface.
3399 */
3400int wpas_p2p_group_remove(struct wpa_supplicant *wpa_s, const char *ifname)
3401{
3402 struct wpa_global *global = wpa_s->global;
3403
3404 if (os_strcmp(ifname, "*") == 0) {
3405 struct wpa_supplicant *prev;
3406 wpa_s = global->ifaces;
3407 while (wpa_s) {
3408 prev = wpa_s;
3409 wpa_s = wpa_s->next;
3410 wpas_p2p_disconnect(prev);
3411 }
3412 return 0;
3413 }
3414
3415 for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
3416 if (os_strcmp(wpa_s->ifname, ifname) == 0)
3417 break;
3418 }
3419
3420 return wpas_p2p_disconnect(wpa_s);
3421}
3422
3423
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003424static int wpas_p2p_init_go_params(struct wpa_supplicant *wpa_s,
3425 struct p2p_go_neg_results *params,
3426 int freq)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003427{
3428 u8 bssid[ETH_ALEN];
3429 int res;
3430
3431 os_memset(params, 0, sizeof(*params));
3432 params->role_go = 1;
3433 if (freq) {
3434 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on forced "
3435 "frequency %d MHz", freq);
3436 params->freq = freq;
3437 } else if (wpa_s->conf->p2p_oper_reg_class == 81 &&
3438 wpa_s->conf->p2p_oper_channel >= 1 &&
3439 wpa_s->conf->p2p_oper_channel <= 11) {
3440 params->freq = 2407 + 5 * wpa_s->conf->p2p_oper_channel;
3441 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on configured "
3442 "frequency %d MHz", params->freq);
3443 } else if (wpa_s->conf->p2p_oper_reg_class == 115 ||
Jouni Malinen87fd2792011-05-16 18:35:42 +03003444 wpa_s->conf->p2p_oper_reg_class == 124) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003445 params->freq = 5000 + 5 * wpa_s->conf->p2p_oper_channel;
3446 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on configured "
3447 "frequency %d MHz", params->freq);
3448 } else if (wpa_s->conf->p2p_oper_channel == 0 &&
3449 wpa_s->best_overall_freq > 0 &&
3450 p2p_supported_freq(wpa_s->global->p2p,
3451 wpa_s->best_overall_freq)) {
3452 params->freq = wpa_s->best_overall_freq;
3453 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on best overall "
3454 "channel %d MHz", params->freq);
3455 } else if (wpa_s->conf->p2p_oper_channel == 0 &&
3456 wpa_s->best_24_freq > 0 &&
3457 p2p_supported_freq(wpa_s->global->p2p,
3458 wpa_s->best_24_freq)) {
3459 params->freq = wpa_s->best_24_freq;
3460 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on best 2.4 GHz "
3461 "channel %d MHz", params->freq);
3462 } else if (wpa_s->conf->p2p_oper_channel == 0 &&
3463 wpa_s->best_5_freq > 0 &&
3464 p2p_supported_freq(wpa_s->global->p2p,
3465 wpa_s->best_5_freq)) {
3466 params->freq = wpa_s->best_5_freq;
3467 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on best 5 GHz "
3468 "channel %d MHz", params->freq);
3469 } else {
Dmitry Shmidt04949592012-07-19 12:16:46 -07003470 int chan;
3471 for (chan = 0; chan < 11; chan++) {
3472 params->freq = 2412 + chan * 5;
3473 if (!wpas_p2p_disallowed_freq(wpa_s->global,
3474 params->freq))
3475 break;
3476 }
3477 if (chan == 11) {
3478 wpa_printf(MSG_DEBUG, "P2P: No 2.4 GHz channel "
3479 "allowed");
3480 return -1;
3481 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003482 wpa_printf(MSG_DEBUG, "P2P: Set GO freq %d MHz (no preference "
3483 "known)", params->freq);
3484 }
3485
3486 if (wpa_s->current_ssid && wpa_drv_get_bssid(wpa_s, bssid) == 0 &&
3487 wpa_s->assoc_freq && !freq) {
3488 wpa_printf(MSG_DEBUG, "P2P: Force GO on the channel we are "
3489 "already using");
3490 params->freq = wpa_s->assoc_freq;
3491 }
3492
3493 res = wpa_drv_shared_freq(wpa_s);
3494 if (res > 0 && !freq) {
3495 wpa_printf(MSG_DEBUG, "P2P: Force GO on the channel we are "
3496 "already using on a shared interface");
3497 params->freq = res;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003498 } else if (res > 0 && freq != res &&
3499 !(wpa_s->drv_flags &
3500 WPA_DRIVER_FLAGS_MULTI_CHANNEL_CONCURRENT)) {
3501 wpa_printf(MSG_DEBUG, "P2P: Cannot start P2P group on %u MHz "
3502 "while connected on another channel (%u MHz)",
3503 freq, res);
3504 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003505 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003506
3507 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003508}
3509
3510
3511static struct wpa_supplicant *
3512wpas_p2p_get_group_iface(struct wpa_supplicant *wpa_s, int addr_allocated,
3513 int go)
3514{
3515 struct wpa_supplicant *group_wpa_s;
3516
3517 if (!wpas_p2p_create_iface(wpa_s))
3518 return wpa_s;
3519
3520 if (wpas_p2p_add_group_interface(wpa_s, go ? WPA_IF_P2P_GO :
3521 WPA_IF_P2P_CLIENT) < 0)
3522 return NULL;
3523 group_wpa_s = wpas_p2p_init_group_interface(wpa_s, go);
3524 if (group_wpa_s == NULL) {
3525 wpas_p2p_remove_pending_group_interface(wpa_s);
3526 return NULL;
3527 }
3528
3529 return group_wpa_s;
3530}
3531
3532
3533/**
3534 * wpas_p2p_group_add - Add a new P2P group with local end as Group Owner
3535 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
3536 * @persistent_group: Whether to create a persistent group
3537 * @freq: Frequency for the group or 0 to indicate no hardcoding
3538 * Returns: 0 on success, -1 on failure
3539 *
3540 * This function creates a new P2P group with the local end as the Group Owner,
3541 * i.e., without using Group Owner Negotiation.
3542 */
3543int wpas_p2p_group_add(struct wpa_supplicant *wpa_s, int persistent_group,
3544 int freq)
3545{
3546 struct p2p_go_neg_results params;
3547 unsigned int r;
3548
3549 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
3550 return -1;
3551
Dmitry Shmidtdca39792011-09-06 11:17:33 -07003552 /* Make sure we are not running find during connection establishment */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003553 wpa_printf(MSG_DEBUG, "P2P: Stop any on-going P2P FIND");
Dmitry Shmidtdca39792011-09-06 11:17:33 -07003554 wpas_p2p_stop_find(wpa_s);
Dmitry Shmidtdca39792011-09-06 11:17:33 -07003555
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003556 if (freq == 2) {
3557 wpa_printf(MSG_DEBUG, "P2P: Request to start GO on 2.4 GHz "
3558 "band");
3559 if (wpa_s->best_24_freq > 0 &&
3560 p2p_supported_freq(wpa_s->global->p2p,
3561 wpa_s->best_24_freq)) {
3562 freq = wpa_s->best_24_freq;
3563 wpa_printf(MSG_DEBUG, "P2P: Use best 2.4 GHz band "
3564 "channel: %d MHz", freq);
3565 } else {
3566 os_get_random((u8 *) &r, sizeof(r));
3567 freq = 2412 + (r % 3) * 25;
3568 wpa_printf(MSG_DEBUG, "P2P: Use random 2.4 GHz band "
3569 "channel: %d MHz", freq);
3570 }
3571 }
3572
3573 if (freq == 5) {
3574 wpa_printf(MSG_DEBUG, "P2P: Request to start GO on 5 GHz "
3575 "band");
3576 if (wpa_s->best_5_freq > 0 &&
3577 p2p_supported_freq(wpa_s->global->p2p,
3578 wpa_s->best_5_freq)) {
3579 freq = wpa_s->best_5_freq;
3580 wpa_printf(MSG_DEBUG, "P2P: Use best 5 GHz band "
3581 "channel: %d MHz", freq);
3582 } else {
3583 os_get_random((u8 *) &r, sizeof(r));
3584 freq = 5180 + (r % 4) * 20;
3585 if (!p2p_supported_freq(wpa_s->global->p2p, freq)) {
3586 wpa_printf(MSG_DEBUG, "P2P: Could not select "
3587 "5 GHz channel for P2P group");
3588 return -1;
3589 }
3590 wpa_printf(MSG_DEBUG, "P2P: Use random 5 GHz band "
3591 "channel: %d MHz", freq);
3592 }
3593 }
3594
3595 if (freq > 0 && !p2p_supported_freq(wpa_s->global->p2p, freq)) {
3596 wpa_printf(MSG_DEBUG, "P2P: The forced channel for GO "
3597 "(%u MHz) is not supported for P2P uses",
3598 freq);
3599 return -1;
3600 }
3601
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003602 if (wpas_p2p_init_go_params(wpa_s, &params, freq))
3603 return -1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003604 if (params.freq &&
3605 !p2p_supported_freq(wpa_s->global->p2p, params.freq)) {
3606 wpa_printf(MSG_DEBUG, "P2P: The selected channel for GO "
3607 "(%u MHz) is not supported for P2P uses",
3608 params.freq);
3609 return -1;
3610 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003611 p2p_go_params(wpa_s->global->p2p, &params);
3612 params.persistent_group = persistent_group;
3613
3614 wpa_s = wpas_p2p_get_group_iface(wpa_s, 0, 1);
3615 if (wpa_s == NULL)
3616 return -1;
3617 wpas_start_wps_go(wpa_s, &params, 0);
3618
3619 return 0;
3620}
3621
3622
3623static int wpas_start_p2p_client(struct wpa_supplicant *wpa_s,
3624 struct wpa_ssid *params, int addr_allocated)
3625{
3626 struct wpa_ssid *ssid;
3627
3628 wpa_s = wpas_p2p_get_group_iface(wpa_s, addr_allocated, 0);
3629 if (wpa_s == NULL)
3630 return -1;
3631
3632 wpa_supplicant_ap_deinit(wpa_s);
3633
3634 ssid = wpa_config_add_network(wpa_s->conf);
3635 if (ssid == NULL)
3636 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003637 wpa_config_set_network_defaults(ssid);
3638 ssid->temporary = 1;
3639 ssid->proto = WPA_PROTO_RSN;
3640 ssid->pairwise_cipher = WPA_CIPHER_CCMP;
3641 ssid->group_cipher = WPA_CIPHER_CCMP;
3642 ssid->key_mgmt = WPA_KEY_MGMT_PSK;
3643 ssid->ssid = os_malloc(params->ssid_len);
3644 if (ssid->ssid == NULL) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003645 wpa_config_remove_network(wpa_s->conf, ssid->id);
3646 return -1;
3647 }
3648 os_memcpy(ssid->ssid, params->ssid, params->ssid_len);
3649 ssid->ssid_len = params->ssid_len;
3650 ssid->p2p_group = 1;
3651 ssid->export_keys = 1;
3652 if (params->psk_set) {
3653 os_memcpy(ssid->psk, params->psk, 32);
3654 ssid->psk_set = 1;
3655 }
3656 if (params->passphrase)
3657 ssid->passphrase = os_strdup(params->passphrase);
3658
3659 wpa_supplicant_select_network(wpa_s, ssid);
3660
3661 wpa_s->show_group_started = 1;
3662
3663 return 0;
3664}
3665
3666
3667int wpas_p2p_group_add_persistent(struct wpa_supplicant *wpa_s,
3668 struct wpa_ssid *ssid, int addr_allocated,
3669 int freq)
3670{
3671 struct p2p_go_neg_results params;
3672 int go = 0;
3673
3674 if (ssid->disabled != 2 || ssid->ssid == NULL)
3675 return -1;
3676
3677 if (wpas_get_p2p_group(wpa_s, ssid->ssid, ssid->ssid_len, &go) &&
3678 go == (ssid->mode == WPAS_MODE_P2P_GO)) {
3679 wpa_printf(MSG_DEBUG, "P2P: Requested persistent group is "
3680 "already running");
3681 return 0;
3682 }
3683
3684 /* Make sure we are not running find during connection establishment */
3685 wpas_p2p_stop_find(wpa_s);
3686
Dmitry Shmidt04949592012-07-19 12:16:46 -07003687 wpa_s->p2p_fallback_to_go_neg = 0;
3688
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003689 if (ssid->mode == WPAS_MODE_INFRA)
3690 return wpas_start_p2p_client(wpa_s, ssid, addr_allocated);
3691
3692 if (ssid->mode != WPAS_MODE_P2P_GO)
3693 return -1;
3694
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003695 if (wpas_p2p_init_go_params(wpa_s, &params, freq))
3696 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003697
3698 params.role_go = 1;
3699 if (ssid->passphrase == NULL ||
3700 os_strlen(ssid->passphrase) >= sizeof(params.passphrase)) {
3701 wpa_printf(MSG_DEBUG, "P2P: Invalid passphrase in persistent "
3702 "group");
3703 return -1;
3704 }
3705 os_strlcpy(params.passphrase, ssid->passphrase,
3706 sizeof(params.passphrase));
3707 os_memcpy(params.ssid, ssid->ssid, ssid->ssid_len);
3708 params.ssid_len = ssid->ssid_len;
3709 params.persistent_group = 1;
3710
3711 wpa_s = wpas_p2p_get_group_iface(wpa_s, addr_allocated, 1);
3712 if (wpa_s == NULL)
3713 return -1;
3714
3715 wpas_start_wps_go(wpa_s, &params, 0);
3716
3717 return 0;
3718}
3719
3720
3721static void wpas_p2p_ie_update(void *ctx, struct wpabuf *beacon_ies,
3722 struct wpabuf *proberesp_ies)
3723{
3724 struct wpa_supplicant *wpa_s = ctx;
3725 if (wpa_s->ap_iface) {
3726 struct hostapd_data *hapd = wpa_s->ap_iface->bss[0];
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003727 if (!(hapd->conf->p2p & P2P_GROUP_OWNER)) {
3728 wpabuf_free(beacon_ies);
3729 wpabuf_free(proberesp_ies);
3730 return;
3731 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003732 if (beacon_ies) {
3733 wpabuf_free(hapd->p2p_beacon_ie);
3734 hapd->p2p_beacon_ie = beacon_ies;
3735 }
3736 wpabuf_free(hapd->p2p_probe_resp_ie);
3737 hapd->p2p_probe_resp_ie = proberesp_ies;
3738 } else {
3739 wpabuf_free(beacon_ies);
3740 wpabuf_free(proberesp_ies);
3741 }
3742 wpa_supplicant_ap_update_beacon(wpa_s);
3743}
3744
3745
3746static void wpas_p2p_idle_update(void *ctx, int idle)
3747{
3748 struct wpa_supplicant *wpa_s = ctx;
3749 if (!wpa_s->ap_iface)
3750 return;
3751 wpa_printf(MSG_DEBUG, "P2P: GO - group %sidle", idle ? "" : "not ");
Dmitry Shmidt04949592012-07-19 12:16:46 -07003752 if (idle)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003753 wpas_p2p_set_group_idle_timeout(wpa_s);
3754 else
3755 eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL);
3756}
3757
3758
3759struct p2p_group * wpas_p2p_group_init(struct wpa_supplicant *wpa_s,
Dmitry Shmidt04949592012-07-19 12:16:46 -07003760 struct wpa_ssid *ssid)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003761{
3762 struct p2p_group *group;
3763 struct p2p_group_config *cfg;
3764
3765 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
3766 return NULL;
3767 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
3768 return NULL;
3769
3770 cfg = os_zalloc(sizeof(*cfg));
3771 if (cfg == NULL)
3772 return NULL;
3773
Dmitry Shmidt04949592012-07-19 12:16:46 -07003774 if (ssid->p2p_persistent_group && wpa_s->conf->persistent_reconnect)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003775 cfg->persistent_group = 2;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003776 else if (ssid->p2p_persistent_group)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003777 cfg->persistent_group = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003778 os_memcpy(cfg->interface_addr, wpa_s->own_addr, ETH_ALEN);
3779 if (wpa_s->max_stations &&
3780 wpa_s->max_stations < wpa_s->conf->max_num_sta)
3781 cfg->max_clients = wpa_s->max_stations;
3782 else
3783 cfg->max_clients = wpa_s->conf->max_num_sta;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003784 os_memcpy(cfg->ssid, ssid->ssid, ssid->ssid_len);
3785 cfg->ssid_len = ssid->ssid_len;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003786 cfg->cb_ctx = wpa_s;
3787 cfg->ie_update = wpas_p2p_ie_update;
3788 cfg->idle_update = wpas_p2p_idle_update;
3789
3790 group = p2p_group_init(wpa_s->global->p2p, cfg);
3791 if (group == NULL)
3792 os_free(cfg);
Dmitry Shmidt04949592012-07-19 12:16:46 -07003793 if (ssid->mode != WPAS_MODE_P2P_GROUP_FORMATION)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003794 p2p_group_notif_formation_done(group);
3795 wpa_s->p2p_group = group;
3796 return group;
3797}
3798
3799
3800void wpas_p2p_wps_success(struct wpa_supplicant *wpa_s, const u8 *peer_addr,
3801 int registrar)
3802{
Dmitry Shmidt04949592012-07-19 12:16:46 -07003803 struct wpa_ssid *ssid = wpa_s->current_ssid;
3804
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003805 if (!wpa_s->p2p_in_provisioning) {
3806 wpa_printf(MSG_DEBUG, "P2P: Ignore WPS success event - P2P "
3807 "provisioning not in progress");
3808 return;
3809 }
3810
Dmitry Shmidt04949592012-07-19 12:16:46 -07003811 if (ssid && ssid->mode == WPAS_MODE_INFRA) {
3812 u8 go_dev_addr[ETH_ALEN];
3813 os_memcpy(go_dev_addr, wpa_s->bssid, ETH_ALEN);
3814 wpas_p2p_persistent_group(wpa_s, go_dev_addr, ssid->ssid,
3815 ssid->ssid_len);
3816 /* Clear any stored provisioning info */
3817 p2p_clear_provisioning_info(wpa_s->global->p2p, go_dev_addr);
3818 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003819
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003820 eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s->parent,
3821 NULL);
Dmitry Shmidt04949592012-07-19 12:16:46 -07003822 if (ssid && ssid->mode == WPAS_MODE_INFRA) {
3823 /*
3824 * Use a separate timeout for initial data connection to
3825 * complete to allow the group to be removed automatically if
3826 * something goes wrong in this step before the P2P group idle
3827 * timeout mechanism is taken into use.
3828 */
3829 eloop_register_timeout(P2P_MAX_INITIAL_CONN_WAIT, 0,
3830 wpas_p2p_group_formation_timeout,
3831 wpa_s->parent, NULL);
3832 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003833 if (wpa_s->global->p2p)
3834 p2p_wps_success_cb(wpa_s->global->p2p, peer_addr);
3835 else if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
3836 wpa_drv_wps_success_cb(wpa_s, peer_addr);
3837 wpas_group_formation_completed(wpa_s, 1);
3838}
3839
3840
Jouni Malinen75ecf522011-06-27 15:19:46 -07003841void wpas_p2p_wps_failed(struct wpa_supplicant *wpa_s,
3842 struct wps_event_fail *fail)
3843{
3844 if (!wpa_s->p2p_in_provisioning) {
3845 wpa_printf(MSG_DEBUG, "P2P: Ignore WPS fail event - P2P "
3846 "provisioning not in progress");
3847 return;
3848 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003849
3850 if (wpa_s->go_params) {
3851 p2p_clear_provisioning_info(
3852 wpa_s->global->p2p,
Dmitry Shmidt04949592012-07-19 12:16:46 -07003853 wpa_s->go_params->peer_device_addr);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003854 }
3855
Jouni Malinen75ecf522011-06-27 15:19:46 -07003856 wpas_notify_p2p_wps_failed(wpa_s, fail);
3857}
3858
3859
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003860int wpas_p2p_prov_disc(struct wpa_supplicant *wpa_s, const u8 *peer_addr,
Dmitry Shmidt04949592012-07-19 12:16:46 -07003861 const char *config_method,
3862 enum wpas_p2p_prov_disc_use use)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003863{
3864 u16 config_methods;
3865
Dmitry Shmidt04949592012-07-19 12:16:46 -07003866 wpa_s->p2p_fallback_to_go_neg = 0;
3867 wpa_s->pending_pd_use = NORMAL_PD;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003868 if (os_strncmp(config_method, "display", 7) == 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003869 config_methods = WPS_CONFIG_DISPLAY;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003870 else if (os_strncmp(config_method, "keypad", 6) == 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003871 config_methods = WPS_CONFIG_KEYPAD;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003872 else if (os_strncmp(config_method, "pbc", 3) == 0 ||
3873 os_strncmp(config_method, "pushbutton", 10) == 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003874 config_methods = WPS_CONFIG_PUSHBUTTON;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003875 else {
3876 wpa_printf(MSG_DEBUG, "P2P: Unknown config method");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003877 return -1;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003878 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003879
Dmitry Shmidt04949592012-07-19 12:16:46 -07003880 if (use == WPAS_P2P_PD_AUTO) {
3881 os_memcpy(wpa_s->pending_join_dev_addr, peer_addr, ETH_ALEN);
3882 wpa_s->pending_pd_config_methods = config_methods;
3883 wpa_s->p2p_auto_pd = 1;
3884 wpa_s->p2p_auto_join = 0;
3885 wpa_s->pending_pd_before_join = 0;
3886 wpa_s->auto_pd_scan_retry = 0;
3887 wpas_p2p_stop_find(wpa_s);
3888 wpa_s->p2p_join_scan_count = 0;
3889 os_get_time(&wpa_s->p2p_auto_started);
3890 wpa_printf(MSG_DEBUG, "P2P: Auto PD started at %ld.%06ld",
3891 wpa_s->p2p_auto_started.sec,
3892 wpa_s->p2p_auto_started.usec);
3893 wpas_p2p_join_scan(wpa_s, NULL);
3894 return 0;
3895 }
3896
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003897 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT) {
3898 return wpa_drv_p2p_prov_disc_req(wpa_s, peer_addr,
Dmitry Shmidt04949592012-07-19 12:16:46 -07003899 config_methods,
3900 use == WPAS_P2P_PD_FOR_JOIN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003901 }
3902
3903 if (wpa_s->global->p2p == NULL || wpa_s->global->p2p_disabled)
3904 return -1;
3905
3906 return p2p_prov_disc_req(wpa_s->global->p2p, peer_addr,
Dmitry Shmidt04949592012-07-19 12:16:46 -07003907 config_methods, use == WPAS_P2P_PD_FOR_JOIN,
3908 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003909}
3910
3911
3912int wpas_p2p_scan_result_text(const u8 *ies, size_t ies_len, char *buf,
3913 char *end)
3914{
3915 return p2p_scan_result_text(ies, ies_len, buf, end);
3916}
3917
3918
3919static void wpas_p2p_clear_pending_action_tx(struct wpa_supplicant *wpa_s)
3920{
3921 if (!wpa_s->pending_action_tx)
3922 return;
3923
3924 wpa_printf(MSG_DEBUG, "P2P: Drop pending Action TX due to new "
3925 "operation request");
3926 wpabuf_free(wpa_s->pending_action_tx);
3927 wpa_s->pending_action_tx = NULL;
3928}
3929
3930
3931int wpas_p2p_find(struct wpa_supplicant *wpa_s, unsigned int timeout,
3932 enum p2p_discovery_type type,
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08003933 unsigned int num_req_dev_types, const u8 *req_dev_types,
3934 const u8 *dev_id)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003935{
3936 wpas_p2p_clear_pending_action_tx(wpa_s);
3937 wpa_s->p2p_long_listen = 0;
3938
3939 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
3940 return wpa_drv_p2p_find(wpa_s, timeout, type);
3941
Dmitry Shmidt04949592012-07-19 12:16:46 -07003942 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL ||
3943 wpa_s->p2p_in_provisioning)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003944 return -1;
3945
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08003946 wpa_supplicant_cancel_sched_scan(wpa_s);
3947
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003948 return p2p_find(wpa_s->global->p2p, timeout, type,
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08003949 num_req_dev_types, req_dev_types, dev_id);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003950}
3951
3952
3953void wpas_p2p_stop_find(struct wpa_supplicant *wpa_s)
3954{
3955 wpas_p2p_clear_pending_action_tx(wpa_s);
3956 wpa_s->p2p_long_listen = 0;
3957 eloop_cancel_timeout(wpas_p2p_long_listen_timeout, wpa_s, NULL);
3958 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003959 wpa_s->p2p_cb_on_scan_complete = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003960
3961 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT) {
3962 wpa_drv_p2p_stop_find(wpa_s);
3963 return;
3964 }
3965
3966 if (wpa_s->global->p2p)
3967 p2p_stop_find(wpa_s->global->p2p);
3968
3969 wpas_p2p_remove_pending_group_interface(wpa_s);
3970}
3971
3972
3973static void wpas_p2p_long_listen_timeout(void *eloop_ctx, void *timeout_ctx)
3974{
3975 struct wpa_supplicant *wpa_s = eloop_ctx;
3976 wpa_s->p2p_long_listen = 0;
3977}
3978
3979
3980int wpas_p2p_listen(struct wpa_supplicant *wpa_s, unsigned int timeout)
3981{
3982 int res;
3983
3984 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
3985 return -1;
3986
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08003987 wpa_supplicant_cancel_sched_scan(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003988 wpas_p2p_clear_pending_action_tx(wpa_s);
3989
3990 if (timeout == 0) {
3991 /*
3992 * This is a request for unlimited Listen state. However, at
3993 * least for now, this is mapped to a Listen state for one
3994 * hour.
3995 */
3996 timeout = 3600;
3997 }
3998 eloop_cancel_timeout(wpas_p2p_long_listen_timeout, wpa_s, NULL);
3999 wpa_s->p2p_long_listen = 0;
4000
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004001 /*
4002 * Stop previous find/listen operation to avoid trying to request a new
4003 * remain-on-channel operation while the driver is still running the
4004 * previous one.
4005 */
4006 if (wpa_s->global->p2p)
4007 p2p_stop_find(wpa_s->global->p2p);
4008
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004009 res = wpas_p2p_listen_start(wpa_s, timeout * 1000);
4010 if (res == 0 && timeout * 1000 > wpa_s->max_remain_on_chan) {
4011 wpa_s->p2p_long_listen = timeout * 1000;
4012 eloop_register_timeout(timeout, 0,
4013 wpas_p2p_long_listen_timeout,
4014 wpa_s, NULL);
4015 }
4016
4017 return res;
4018}
4019
4020
4021int wpas_p2p_assoc_req_ie(struct wpa_supplicant *wpa_s, struct wpa_bss *bss,
4022 u8 *buf, size_t len, int p2p_group)
4023{
4024 struct wpabuf *p2p_ie;
4025 int ret;
4026
4027 if (wpa_s->global->p2p_disabled)
4028 return -1;
4029 if (wpa_s->global->p2p == NULL)
4030 return -1;
4031 if (bss == NULL)
4032 return -1;
4033
4034 p2p_ie = wpa_bss_get_vendor_ie_multi(bss, P2P_IE_VENDOR_TYPE);
4035 ret = p2p_assoc_req_ie(wpa_s->global->p2p, bss->bssid, buf, len,
4036 p2p_group, p2p_ie);
4037 wpabuf_free(p2p_ie);
4038
4039 return ret;
4040}
4041
4042
4043int wpas_p2p_probe_req_rx(struct wpa_supplicant *wpa_s, const u8 *addr,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004044 const u8 *dst, const u8 *bssid,
Dmitry Shmidt04949592012-07-19 12:16:46 -07004045 const u8 *ie, size_t ie_len, int ssi_signal)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004046{
4047 if (wpa_s->global->p2p_disabled)
4048 return 0;
4049 if (wpa_s->global->p2p == NULL)
4050 return 0;
4051
Dmitry Shmidt04949592012-07-19 12:16:46 -07004052 switch (p2p_probe_req_rx(wpa_s->global->p2p, addr, dst, bssid,
4053 ie, ie_len)) {
4054 case P2P_PREQ_NOT_P2P:
4055 wpas_notify_preq(wpa_s, addr, dst, bssid, ie, ie_len,
4056 ssi_signal);
4057 /* fall through */
4058 case P2P_PREQ_MALFORMED:
4059 case P2P_PREQ_NOT_LISTEN:
4060 case P2P_PREQ_NOT_PROCESSED:
4061 default: /* make gcc happy */
4062 return 0;
4063 case P2P_PREQ_PROCESSED:
4064 return 1;
4065 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004066}
4067
4068
4069void wpas_p2p_rx_action(struct wpa_supplicant *wpa_s, const u8 *da,
4070 const u8 *sa, const u8 *bssid,
4071 u8 category, const u8 *data, size_t len, int freq)
4072{
4073 if (wpa_s->global->p2p_disabled)
4074 return;
4075 if (wpa_s->global->p2p == NULL)
4076 return;
4077
4078 p2p_rx_action(wpa_s->global->p2p, da, sa, bssid, category, data, len,
4079 freq);
4080}
4081
4082
4083void wpas_p2p_scan_ie(struct wpa_supplicant *wpa_s, struct wpabuf *ies)
4084{
4085 if (wpa_s->global->p2p_disabled)
4086 return;
4087 if (wpa_s->global->p2p == NULL)
4088 return;
4089
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08004090 p2p_scan_ie(wpa_s->global->p2p, ies, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004091}
4092
4093
4094void wpas_p2p_group_deinit(struct wpa_supplicant *wpa_s)
4095{
4096 p2p_group_deinit(wpa_s->p2p_group);
4097 wpa_s->p2p_group = NULL;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004098
4099 wpa_s->ap_configured_cb = NULL;
4100 wpa_s->ap_configured_cb_ctx = NULL;
4101 wpa_s->ap_configured_cb_data = NULL;
4102 wpa_s->connect_without_scan = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004103}
4104
4105
4106int wpas_p2p_reject(struct wpa_supplicant *wpa_s, const u8 *addr)
4107{
4108 wpa_s->p2p_long_listen = 0;
4109
4110 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
4111 return wpa_drv_p2p_reject(wpa_s, addr);
4112
4113 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
4114 return -1;
4115
4116 return p2p_reject(wpa_s->global->p2p, addr);
4117}
4118
4119
4120/* Invite to reinvoke a persistent group */
4121int wpas_p2p_invite(struct wpa_supplicant *wpa_s, const u8 *peer_addr,
4122 struct wpa_ssid *ssid, const u8 *go_dev_addr)
4123{
4124 enum p2p_invite_role role;
4125 u8 *bssid = NULL;
4126
4127 if (ssid->mode == WPAS_MODE_P2P_GO) {
4128 role = P2P_INVITE_ROLE_GO;
4129 if (peer_addr == NULL) {
4130 wpa_printf(MSG_DEBUG, "P2P: Missing peer "
4131 "address in invitation command");
4132 return -1;
4133 }
4134 if (wpas_p2p_create_iface(wpa_s)) {
4135 if (wpas_p2p_add_group_interface(wpa_s,
4136 WPA_IF_P2P_GO) < 0) {
4137 wpa_printf(MSG_ERROR, "P2P: Failed to "
4138 "allocate a new interface for the "
4139 "group");
4140 return -1;
4141 }
4142 bssid = wpa_s->pending_interface_addr;
4143 } else
4144 bssid = wpa_s->own_addr;
4145 } else {
4146 role = P2P_INVITE_ROLE_CLIENT;
4147 peer_addr = ssid->bssid;
4148 }
4149 wpa_s->pending_invite_ssid_id = ssid->id;
4150
4151 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
4152 return wpa_drv_p2p_invite(wpa_s, peer_addr, role, bssid,
4153 ssid->ssid, ssid->ssid_len,
4154 go_dev_addr, 1);
4155
4156 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
4157 return -1;
4158
4159 return p2p_invite(wpa_s->global->p2p, peer_addr, role, bssid,
4160 ssid->ssid, ssid->ssid_len, 0, go_dev_addr, 1);
4161}
4162
4163
4164/* Invite to join an active group */
4165int wpas_p2p_invite_group(struct wpa_supplicant *wpa_s, const char *ifname,
4166 const u8 *peer_addr, const u8 *go_dev_addr)
4167{
4168 struct wpa_global *global = wpa_s->global;
4169 enum p2p_invite_role role;
4170 u8 *bssid = NULL;
4171 struct wpa_ssid *ssid;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08004172 int persistent;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004173
4174 for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
4175 if (os_strcmp(wpa_s->ifname, ifname) == 0)
4176 break;
4177 }
4178 if (wpa_s == NULL) {
4179 wpa_printf(MSG_DEBUG, "P2P: Interface '%s' not found", ifname);
4180 return -1;
4181 }
4182
4183 ssid = wpa_s->current_ssid;
4184 if (ssid == NULL) {
4185 wpa_printf(MSG_DEBUG, "P2P: No current SSID to use for "
4186 "invitation");
4187 return -1;
4188 }
4189
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08004190 persistent = ssid->p2p_persistent_group &&
4191 wpas_p2p_get_persistent(wpa_s->parent, peer_addr,
4192 ssid->ssid, ssid->ssid_len);
4193
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004194 if (ssid->mode == WPAS_MODE_P2P_GO) {
4195 role = P2P_INVITE_ROLE_ACTIVE_GO;
4196 bssid = wpa_s->own_addr;
4197 if (go_dev_addr == NULL)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004198 go_dev_addr = wpa_s->global->p2p_dev_addr;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004199 } else {
4200 role = P2P_INVITE_ROLE_CLIENT;
4201 if (wpa_s->wpa_state < WPA_ASSOCIATED) {
4202 wpa_printf(MSG_DEBUG, "P2P: Not associated - cannot "
4203 "invite to current group");
4204 return -1;
4205 }
4206 bssid = wpa_s->bssid;
4207 if (go_dev_addr == NULL &&
4208 !is_zero_ether_addr(wpa_s->go_dev_addr))
4209 go_dev_addr = wpa_s->go_dev_addr;
4210 }
4211 wpa_s->parent->pending_invite_ssid_id = -1;
4212
4213 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
4214 return wpa_drv_p2p_invite(wpa_s, peer_addr, role, bssid,
4215 ssid->ssid, ssid->ssid_len,
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08004216 go_dev_addr, persistent);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004217
4218 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
4219 return -1;
4220
4221 return p2p_invite(wpa_s->global->p2p, peer_addr, role, bssid,
4222 ssid->ssid, ssid->ssid_len, wpa_s->assoc_freq,
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08004223 go_dev_addr, persistent);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004224}
4225
4226
4227void wpas_p2p_completed(struct wpa_supplicant *wpa_s)
4228{
4229 struct wpa_ssid *ssid = wpa_s->current_ssid;
4230 const char *ssid_txt;
4231 u8 go_dev_addr[ETH_ALEN];
Jouni Malinen75ecf522011-06-27 15:19:46 -07004232 int network_id = -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004233 int persistent;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004234 int freq;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004235
Dmitry Shmidt04949592012-07-19 12:16:46 -07004236 if (ssid == NULL || ssid->mode != WPAS_MODE_P2P_GROUP_FORMATION) {
4237 eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
4238 wpa_s->parent, NULL);
4239 }
4240
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004241 if (!wpa_s->show_group_started || !ssid)
4242 return;
4243
4244 wpa_s->show_group_started = 0;
4245
4246 ssid_txt = wpa_ssid_txt(ssid->ssid, ssid->ssid_len);
4247 os_memset(go_dev_addr, 0, ETH_ALEN);
4248 if (ssid->bssid_set)
4249 os_memcpy(go_dev_addr, ssid->bssid, ETH_ALEN);
4250 persistent = wpas_p2p_persistent_group(wpa_s, go_dev_addr, ssid->ssid,
4251 ssid->ssid_len);
4252 os_memcpy(wpa_s->go_dev_addr, go_dev_addr, ETH_ALEN);
4253
4254 if (wpa_s->global->p2p_group_formation == wpa_s)
4255 wpa_s->global->p2p_group_formation = NULL;
4256
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004257 freq = wpa_s->current_bss ? wpa_s->current_bss->freq :
4258 (int) wpa_s->assoc_freq;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004259 if (ssid->passphrase == NULL && ssid->psk_set) {
4260 char psk[65];
4261 wpa_snprintf_hex(psk, sizeof(psk), ssid->psk, 32);
4262 wpa_msg(wpa_s->parent, MSG_INFO, P2P_EVENT_GROUP_STARTED
4263 "%s client ssid=\"%s\" freq=%d psk=%s go_dev_addr="
4264 MACSTR "%s",
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004265 wpa_s->ifname, ssid_txt, freq, psk,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004266 MAC2STR(go_dev_addr),
4267 persistent ? " [PERSISTENT]" : "");
4268 } else {
4269 wpa_msg(wpa_s->parent, MSG_INFO, P2P_EVENT_GROUP_STARTED
4270 "%s client ssid=\"%s\" freq=%d passphrase=\"%s\" "
4271 "go_dev_addr=" MACSTR "%s",
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004272 wpa_s->ifname, ssid_txt, freq,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004273 ssid->passphrase ? ssid->passphrase : "",
4274 MAC2STR(go_dev_addr),
4275 persistent ? " [PERSISTENT]" : "");
4276 }
4277
4278 if (persistent)
Jouni Malinen75ecf522011-06-27 15:19:46 -07004279 network_id = wpas_p2p_store_persistent_group(wpa_s->parent,
4280 ssid, go_dev_addr);
4281 if (network_id < 0)
4282 network_id = ssid->id;
4283 wpas_notify_p2p_group_started(wpa_s, ssid, network_id, 1);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004284}
4285
4286
4287int wpas_p2p_presence_req(struct wpa_supplicant *wpa_s, u32 duration1,
4288 u32 interval1, u32 duration2, u32 interval2)
4289{
4290 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
4291 return -1;
4292 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
4293 return -1;
4294
4295 if (wpa_s->wpa_state < WPA_ASSOCIATED ||
4296 wpa_s->current_ssid == NULL ||
4297 wpa_s->current_ssid->mode != WPAS_MODE_INFRA)
4298 return -1;
4299
4300 return p2p_presence_req(wpa_s->global->p2p, wpa_s->bssid,
4301 wpa_s->own_addr, wpa_s->assoc_freq,
4302 duration1, interval1, duration2, interval2);
4303}
4304
4305
4306int wpas_p2p_ext_listen(struct wpa_supplicant *wpa_s, unsigned int period,
4307 unsigned int interval)
4308{
4309 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
4310 return -1;
4311
4312 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
4313 return -1;
4314
4315 return p2p_ext_listen(wpa_s->global->p2p, period, interval);
4316}
4317
4318
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004319static int wpas_p2p_is_client(struct wpa_supplicant *wpa_s)
4320{
4321 return wpa_s->current_ssid != NULL &&
4322 wpa_s->current_ssid->p2p_group &&
4323 wpa_s->current_ssid->mode == WPAS_MODE_INFRA;
4324}
4325
4326
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004327static void wpas_p2p_group_idle_timeout(void *eloop_ctx, void *timeout_ctx)
4328{
4329 struct wpa_supplicant *wpa_s = eloop_ctx;
4330
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004331 if (wpa_s->conf->p2p_group_idle == 0 && !wpas_p2p_is_client(wpa_s)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004332 wpa_printf(MSG_DEBUG, "P2P: Ignore group idle timeout - "
4333 "disabled");
4334 return;
4335 }
4336
Dmitry Shmidt04949592012-07-19 12:16:46 -07004337 wpa_printf(MSG_DEBUG, "P2P: Group idle timeout reached - terminate "
4338 "group");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004339 wpa_s->removal_reason = P2P_GROUP_REMOVAL_IDLE_TIMEOUT;
Dmitry Shmidt04949592012-07-19 12:16:46 -07004340 wpas_p2p_group_delete(wpa_s, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004341}
4342
4343
4344static void wpas_p2p_set_group_idle_timeout(struct wpa_supplicant *wpa_s)
4345{
Dmitry Shmidt04949592012-07-19 12:16:46 -07004346 int timeout;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004347
Dmitry Shmidt04949592012-07-19 12:16:46 -07004348 if (eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL) > 0)
4349 wpa_printf(MSG_DEBUG, "P2P: Cancelled P2P group idle timeout");
4350
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004351 if (wpa_s->current_ssid == NULL || !wpa_s->current_ssid->p2p_group)
4352 return;
4353
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004354 timeout = wpa_s->conf->p2p_group_idle;
4355 if (wpa_s->current_ssid->mode == WPAS_MODE_INFRA &&
4356 (timeout == 0 || timeout > P2P_MAX_CLIENT_IDLE))
4357 timeout = P2P_MAX_CLIENT_IDLE;
4358
4359 if (timeout == 0)
4360 return;
4361
Dmitry Shmidt04949592012-07-19 12:16:46 -07004362 if (timeout < 0) {
4363 if (wpa_s->current_ssid->mode == WPAS_MODE_INFRA)
4364 timeout = 0; /* special client mode no-timeout */
4365 else
4366 return;
4367 }
4368
4369 if (wpa_s->p2p_in_provisioning) {
4370 /*
4371 * Use the normal group formation timeout during the
4372 * provisioning phase to avoid terminating this process too
4373 * early due to group idle timeout.
4374 */
4375 wpa_printf(MSG_DEBUG, "P2P: Do not use P2P group idle timeout "
4376 "during provisioning");
4377 return;
4378 }
4379
4380 if (wpa_s->show_group_started) {
4381 /*
4382 * Use the normal group formation timeout between the end of
4383 * the provisioning phase and completion of 4-way handshake to
4384 * avoid terminating this process too early due to group idle
4385 * timeout.
4386 */
4387 wpa_printf(MSG_DEBUG, "P2P: Do not use P2P group idle timeout "
4388 "while waiting for initial 4-way handshake to "
4389 "complete");
4390 return;
4391 }
4392
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004393 wpa_printf(MSG_DEBUG, "P2P: Set P2P group idle timeout to %u seconds",
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004394 timeout);
4395 eloop_register_timeout(timeout, 0, wpas_p2p_group_idle_timeout,
4396 wpa_s, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004397}
4398
4399
4400void wpas_p2p_deauth_notif(struct wpa_supplicant *wpa_s, const u8 *bssid,
Dmitry Shmidt04949592012-07-19 12:16:46 -07004401 u16 reason_code, const u8 *ie, size_t ie_len,
4402 int locally_generated)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004403{
4404 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
4405 return;
4406 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
4407 return;
4408
Dmitry Shmidt04949592012-07-19 12:16:46 -07004409 if (!locally_generated)
4410 p2p_deauth_notif(wpa_s->global->p2p, bssid, reason_code, ie,
4411 ie_len);
4412
4413 if (reason_code == WLAN_REASON_DEAUTH_LEAVING && !locally_generated &&
4414 wpa_s->current_ssid &&
4415 wpa_s->current_ssid->p2p_group &&
4416 wpa_s->current_ssid->mode == WPAS_MODE_INFRA) {
4417 wpa_printf(MSG_DEBUG, "P2P: GO indicated that the P2P Group "
4418 "session is ending");
4419 wpa_s->removal_reason = P2P_GROUP_REMOVAL_GO_ENDING_SESSION;
4420 wpas_p2p_group_delete(wpa_s, 0);
4421 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004422}
4423
4424
4425void wpas_p2p_disassoc_notif(struct wpa_supplicant *wpa_s, const u8 *bssid,
Dmitry Shmidt04949592012-07-19 12:16:46 -07004426 u16 reason_code, const u8 *ie, size_t ie_len,
4427 int locally_generated)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004428{
4429 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
4430 return;
4431 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
4432 return;
4433
Dmitry Shmidt04949592012-07-19 12:16:46 -07004434 if (!locally_generated)
4435 p2p_disassoc_notif(wpa_s->global->p2p, bssid, reason_code, ie,
4436 ie_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004437}
4438
4439
4440void wpas_p2p_update_config(struct wpa_supplicant *wpa_s)
4441{
4442 struct p2p_data *p2p = wpa_s->global->p2p;
4443
4444 if (p2p == NULL)
4445 return;
4446
4447 if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_CAPABLE))
4448 return;
4449
4450 if (wpa_s->conf->changed_parameters & CFG_CHANGED_DEVICE_NAME)
4451 p2p_set_dev_name(p2p, wpa_s->conf->device_name);
4452
4453 if (wpa_s->conf->changed_parameters & CFG_CHANGED_DEVICE_TYPE)
4454 p2p_set_pri_dev_type(p2p, wpa_s->conf->device_type);
4455
4456 if (wpa_s->wps &&
4457 (wpa_s->conf->changed_parameters & CFG_CHANGED_CONFIG_METHODS))
4458 p2p_set_config_methods(p2p, wpa_s->wps->config_methods);
4459
4460 if (wpa_s->wps && (wpa_s->conf->changed_parameters & CFG_CHANGED_UUID))
4461 p2p_set_uuid(p2p, wpa_s->wps->uuid);
4462
4463 if (wpa_s->conf->changed_parameters & CFG_CHANGED_WPS_STRING) {
4464 p2p_set_manufacturer(p2p, wpa_s->conf->manufacturer);
4465 p2p_set_model_name(p2p, wpa_s->conf->model_name);
4466 p2p_set_model_number(p2p, wpa_s->conf->model_number);
4467 p2p_set_serial_number(p2p, wpa_s->conf->serial_number);
4468 }
4469
4470 if (wpa_s->conf->changed_parameters & CFG_CHANGED_SEC_DEVICE_TYPE)
4471 p2p_set_sec_dev_types(p2p,
4472 (void *) wpa_s->conf->sec_device_type,
4473 wpa_s->conf->num_sec_device_types);
4474
4475 if (wpa_s->conf->changed_parameters & CFG_CHANGED_VENDOR_EXTENSION) {
4476 int i;
4477 p2p_remove_wps_vendor_extensions(p2p);
4478 for (i = 0; i < MAX_WPS_VENDOR_EXT; i++) {
4479 if (wpa_s->conf->wps_vendor_ext[i] == NULL)
4480 continue;
4481 p2p_add_wps_vendor_extension(
4482 p2p, wpa_s->conf->wps_vendor_ext[i]);
4483 }
4484 }
4485
4486 if ((wpa_s->conf->changed_parameters & CFG_CHANGED_COUNTRY) &&
4487 wpa_s->conf->country[0] && wpa_s->conf->country[1]) {
4488 char country[3];
4489 country[0] = wpa_s->conf->country[0];
4490 country[1] = wpa_s->conf->country[1];
4491 country[2] = 0x04;
4492 p2p_set_country(p2p, country);
4493 }
4494
4495 if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_SSID_POSTFIX) {
4496 p2p_set_ssid_postfix(p2p, (u8 *) wpa_s->conf->p2p_ssid_postfix,
4497 wpa_s->conf->p2p_ssid_postfix ?
4498 os_strlen(wpa_s->conf->p2p_ssid_postfix) :
4499 0);
4500 }
4501
4502 if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_INTRA_BSS)
4503 p2p_set_intra_bss_dist(p2p, wpa_s->conf->p2p_intra_bss);
Jouni Malinen75ecf522011-06-27 15:19:46 -07004504
4505 if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_LISTEN_CHANNEL) {
4506 u8 reg_class, channel;
4507 int ret;
4508 unsigned int r;
4509 if (wpa_s->conf->p2p_listen_reg_class &&
4510 wpa_s->conf->p2p_listen_channel) {
4511 reg_class = wpa_s->conf->p2p_listen_reg_class;
4512 channel = wpa_s->conf->p2p_listen_channel;
4513 } else {
4514 reg_class = 81;
4515 /*
4516 * Pick one of the social channels randomly as the
4517 * listen channel.
4518 */
4519 os_get_random((u8 *) &r, sizeof(r));
4520 channel = 1 + (r % 3) * 5;
4521 }
4522 ret = p2p_set_listen_channel(p2p, reg_class, channel);
4523 if (ret)
4524 wpa_printf(MSG_ERROR, "P2P: Own listen channel update "
4525 "failed: %d", ret);
4526 }
4527 if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_OPER_CHANNEL) {
4528 u8 op_reg_class, op_channel, cfg_op_channel;
4529 int ret = 0;
4530 unsigned int r;
4531 if (wpa_s->conf->p2p_oper_reg_class &&
4532 wpa_s->conf->p2p_oper_channel) {
4533 op_reg_class = wpa_s->conf->p2p_oper_reg_class;
4534 op_channel = wpa_s->conf->p2p_oper_channel;
4535 cfg_op_channel = 1;
4536 } else {
4537 op_reg_class = 81;
4538 /*
4539 * Use random operation channel from (1, 6, 11)
4540 *if no other preference is indicated.
4541 */
4542 os_get_random((u8 *) &r, sizeof(r));
4543 op_channel = 1 + (r % 3) * 5;
4544 cfg_op_channel = 0;
4545 }
4546 ret = p2p_set_oper_channel(p2p, op_reg_class, op_channel,
4547 cfg_op_channel);
4548 if (ret)
4549 wpa_printf(MSG_ERROR, "P2P: Own oper channel update "
4550 "failed: %d", ret);
4551 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07004552
4553 if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_PREF_CHAN) {
4554 if (p2p_set_pref_chan(p2p, wpa_s->conf->num_p2p_pref_chan,
4555 wpa_s->conf->p2p_pref_chan) < 0) {
4556 wpa_printf(MSG_ERROR, "P2P: Preferred channel list "
4557 "update failed");
4558 }
4559 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004560}
4561
4562
4563int wpas_p2p_set_noa(struct wpa_supplicant *wpa_s, u8 count, int start,
4564 int duration)
4565{
4566 if (!wpa_s->ap_iface)
4567 return -1;
4568 return hostapd_p2p_set_noa(wpa_s->ap_iface->bss[0], count, start,
4569 duration);
4570}
4571
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004572
4573int wpas_p2p_set_cross_connect(struct wpa_supplicant *wpa_s, int enabled)
4574{
4575 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
4576 return -1;
4577 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
4578 return -1;
4579
4580 wpa_s->global->cross_connection = enabled;
4581 p2p_set_cross_connect(wpa_s->global->p2p, enabled);
4582
4583 if (!enabled) {
4584 struct wpa_supplicant *iface;
4585
4586 for (iface = wpa_s->global->ifaces; iface; iface = iface->next)
4587 {
4588 if (iface->cross_connect_enabled == 0)
4589 continue;
4590
4591 iface->cross_connect_enabled = 0;
4592 iface->cross_connect_in_use = 0;
4593 wpa_msg(iface->parent, MSG_INFO,
4594 P2P_EVENT_CROSS_CONNECT_DISABLE "%s %s",
4595 iface->ifname, iface->cross_connect_uplink);
4596 }
4597 }
4598
4599 return 0;
4600}
4601
4602
4603static void wpas_p2p_enable_cross_connect(struct wpa_supplicant *uplink)
4604{
4605 struct wpa_supplicant *iface;
4606
4607 if (!uplink->global->cross_connection)
4608 return;
4609
4610 for (iface = uplink->global->ifaces; iface; iface = iface->next) {
4611 if (!iface->cross_connect_enabled)
4612 continue;
4613 if (os_strcmp(uplink->ifname, iface->cross_connect_uplink) !=
4614 0)
4615 continue;
4616 if (iface->ap_iface == NULL)
4617 continue;
4618 if (iface->cross_connect_in_use)
4619 continue;
4620
4621 iface->cross_connect_in_use = 1;
4622 wpa_msg(iface->parent, MSG_INFO,
4623 P2P_EVENT_CROSS_CONNECT_ENABLE "%s %s",
4624 iface->ifname, iface->cross_connect_uplink);
4625 }
4626}
4627
4628
4629static void wpas_p2p_disable_cross_connect(struct wpa_supplicant *uplink)
4630{
4631 struct wpa_supplicant *iface;
4632
4633 for (iface = uplink->global->ifaces; iface; iface = iface->next) {
4634 if (!iface->cross_connect_enabled)
4635 continue;
4636 if (os_strcmp(uplink->ifname, iface->cross_connect_uplink) !=
4637 0)
4638 continue;
4639 if (!iface->cross_connect_in_use)
4640 continue;
4641
4642 wpa_msg(iface->parent, MSG_INFO,
4643 P2P_EVENT_CROSS_CONNECT_DISABLE "%s %s",
4644 iface->ifname, iface->cross_connect_uplink);
4645 iface->cross_connect_in_use = 0;
4646 }
4647}
4648
4649
4650void wpas_p2p_notif_connected(struct wpa_supplicant *wpa_s)
4651{
4652 if (wpa_s->ap_iface || wpa_s->current_ssid == NULL ||
4653 wpa_s->current_ssid->mode != WPAS_MODE_INFRA ||
4654 wpa_s->cross_connect_disallowed)
4655 wpas_p2p_disable_cross_connect(wpa_s);
4656 else
4657 wpas_p2p_enable_cross_connect(wpa_s);
Dmitry Shmidt04949592012-07-19 12:16:46 -07004658 if (!wpa_s->ap_iface &&
4659 eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL) > 0)
4660 wpa_printf(MSG_DEBUG, "P2P: Cancelled P2P group idle timeout");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004661}
4662
4663
4664void wpas_p2p_notif_disconnected(struct wpa_supplicant *wpa_s)
4665{
4666 wpas_p2p_disable_cross_connect(wpa_s);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004667 if (!wpa_s->ap_iface &&
4668 !eloop_is_timeout_registered(wpas_p2p_group_idle_timeout,
4669 wpa_s, NULL))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004670 wpas_p2p_set_group_idle_timeout(wpa_s);
4671}
4672
4673
4674static void wpas_p2p_cross_connect_setup(struct wpa_supplicant *wpa_s)
4675{
4676 struct wpa_supplicant *iface;
4677
4678 if (!wpa_s->global->cross_connection)
4679 return;
4680
4681 for (iface = wpa_s->global->ifaces; iface; iface = iface->next) {
4682 if (iface == wpa_s)
4683 continue;
4684 if (iface->drv_flags &
4685 WPA_DRIVER_FLAGS_P2P_DEDICATED_INTERFACE)
4686 continue;
4687 if (iface->drv_flags & WPA_DRIVER_FLAGS_P2P_CAPABLE)
4688 continue;
4689
4690 wpa_s->cross_connect_enabled = 1;
4691 os_strlcpy(wpa_s->cross_connect_uplink, iface->ifname,
4692 sizeof(wpa_s->cross_connect_uplink));
4693 wpa_printf(MSG_DEBUG, "P2P: Enable cross connection from "
4694 "%s to %s whenever uplink is available",
4695 wpa_s->ifname, wpa_s->cross_connect_uplink);
4696
4697 if (iface->ap_iface || iface->current_ssid == NULL ||
4698 iface->current_ssid->mode != WPAS_MODE_INFRA ||
4699 iface->cross_connect_disallowed ||
4700 iface->wpa_state != WPA_COMPLETED)
4701 break;
4702
4703 wpa_s->cross_connect_in_use = 1;
4704 wpa_msg(wpa_s->parent, MSG_INFO,
4705 P2P_EVENT_CROSS_CONNECT_ENABLE "%s %s",
4706 wpa_s->ifname, wpa_s->cross_connect_uplink);
4707 break;
4708 }
4709}
4710
4711
4712int wpas_p2p_notif_pbc_overlap(struct wpa_supplicant *wpa_s)
4713{
4714 if (wpa_s->p2p_group_interface != P2P_GROUP_INTERFACE_CLIENT &&
4715 !wpa_s->p2p_in_provisioning)
4716 return 0; /* not P2P client operation */
4717
4718 wpa_printf(MSG_DEBUG, "P2P: Terminate connection due to WPS PBC "
4719 "session overlap");
4720 if (wpa_s != wpa_s->parent)
4721 wpa_msg_ctrl(wpa_s->parent, MSG_INFO, WPS_EVENT_OVERLAP);
4722
4723 if (wpa_s->global->p2p)
4724 p2p_group_formation_failed(wpa_s->global->p2p);
4725
4726 eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
4727 wpa_s->parent, NULL);
4728
4729 wpas_group_formation_completed(wpa_s, 0);
4730 return 1;
4731}
4732
4733
4734void wpas_p2p_update_channel_list(struct wpa_supplicant *wpa_s)
4735{
4736 struct p2p_channels chan;
4737
4738 if (wpa_s->global == NULL || wpa_s->global->p2p == NULL)
4739 return;
4740
4741 os_memset(&chan, 0, sizeof(chan));
4742 if (wpas_p2p_setup_channels(wpa_s, &chan)) {
4743 wpa_printf(MSG_ERROR, "P2P: Failed to update supported "
4744 "channel list");
4745 return;
4746 }
4747
4748 p2p_update_channel_list(wpa_s->global->p2p, &chan);
4749}
4750
4751
4752int wpas_p2p_cancel(struct wpa_supplicant *wpa_s)
4753{
4754 struct wpa_global *global = wpa_s->global;
4755 int found = 0;
4756 const u8 *peer;
4757
4758 if (global->p2p == NULL)
4759 return -1;
4760
4761 wpa_printf(MSG_DEBUG, "P2P: Request to cancel group formation");
4762
4763 if (wpa_s->pending_interface_name[0] &&
4764 !is_zero_ether_addr(wpa_s->pending_interface_addr))
4765 found = 1;
4766
4767 peer = p2p_get_go_neg_peer(global->p2p);
4768 if (peer) {
4769 wpa_printf(MSG_DEBUG, "P2P: Unauthorize pending GO Neg peer "
4770 MACSTR, MAC2STR(peer));
4771 p2p_unauthorize(global->p2p, peer);
Dmitry Shmidt04949592012-07-19 12:16:46 -07004772 found = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004773 }
4774
4775 wpas_p2p_stop_find(wpa_s);
4776
4777 for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
4778 if (wpa_s == global->p2p_group_formation &&
4779 (wpa_s->p2p_in_provisioning ||
4780 wpa_s->parent->pending_interface_type ==
4781 WPA_IF_P2P_CLIENT)) {
4782 wpa_printf(MSG_DEBUG, "P2P: Interface %s in group "
4783 "formation found - cancelling",
4784 wpa_s->ifname);
4785 found = 1;
4786 eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
4787 wpa_s->parent, NULL);
Dmitry Shmidt04949592012-07-19 12:16:46 -07004788 wpas_p2p_group_delete(wpa_s, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004789 break;
4790 }
4791 }
4792
4793 if (!found) {
4794 wpa_printf(MSG_DEBUG, "P2P: No ongoing group formation found");
4795 return -1;
4796 }
4797
4798 return 0;
4799}
4800
4801
4802void wpas_p2p_interface_unavailable(struct wpa_supplicant *wpa_s)
4803{
4804 if (wpa_s->current_ssid == NULL || !wpa_s->current_ssid->p2p_group)
4805 return;
4806
4807 wpa_printf(MSG_DEBUG, "P2P: Remove group due to driver resource not "
4808 "being available anymore");
4809 wpa_s->removal_reason = P2P_GROUP_REMOVAL_UNAVAILABLE;
Dmitry Shmidt04949592012-07-19 12:16:46 -07004810 wpas_p2p_group_delete(wpa_s, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004811}
4812
4813
4814void wpas_p2p_update_best_channels(struct wpa_supplicant *wpa_s,
4815 int freq_24, int freq_5, int freq_overall)
4816{
4817 struct p2p_data *p2p = wpa_s->global->p2p;
4818 if (p2p == NULL || (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT))
4819 return;
4820 p2p_set_best_channels(p2p, freq_24, freq_5, freq_overall);
4821}
4822
4823
4824int wpas_p2p_unauthorize(struct wpa_supplicant *wpa_s, const char *addr)
4825{
4826 u8 peer[ETH_ALEN];
4827 struct p2p_data *p2p = wpa_s->global->p2p;
4828
4829 if (p2p == NULL || (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT))
4830 return -1;
4831
4832 if (hwaddr_aton(addr, peer))
4833 return -1;
4834
4835 return p2p_unauthorize(p2p, peer);
4836}
4837
4838
4839/**
4840 * wpas_p2p_disconnect - Disconnect from a P2P Group
4841 * @wpa_s: Pointer to wpa_supplicant data
4842 * Returns: 0 on success, -1 on failure
4843 *
4844 * This can be used to disconnect from a group in which the local end is a P2P
4845 * Client or to end a P2P Group in case the local end is the Group Owner. If a
4846 * virtual network interface was created for this group, that interface will be
4847 * removed. Otherwise, only the configured P2P group network will be removed
4848 * from the interface.
4849 */
4850int wpas_p2p_disconnect(struct wpa_supplicant *wpa_s)
4851{
4852
4853 if (wpa_s == NULL)
4854 return -1;
4855
4856 wpa_s->removal_reason = P2P_GROUP_REMOVAL_REQUESTED;
Dmitry Shmidt04949592012-07-19 12:16:46 -07004857 wpas_p2p_group_delete(wpa_s, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004858
4859 return 0;
4860}
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004861
4862
4863int wpas_p2p_in_progress(struct wpa_supplicant *wpa_s)
4864{
4865 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
4866 return 0;
4867
4868 return p2p_in_progress(wpa_s->global->p2p);
4869}
4870
4871
4872void wpas_p2p_network_removed(struct wpa_supplicant *wpa_s,
4873 struct wpa_ssid *ssid)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004874{
4875 if (wpa_s->p2p_in_provisioning && ssid->p2p_group &&
4876 eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
4877 wpa_s->parent, NULL) > 0) {
Dmitry Shmidt04949592012-07-19 12:16:46 -07004878 /**
4879 * Remove the network by scheduling the group formation
4880 * timeout to happen immediately. The teardown code
4881 * needs to be scheduled to run asynch later so that we
4882 * don't delete data from under ourselves unexpectedly.
4883 * Calling wpas_p2p_group_formation_timeout directly
4884 * causes a series of crashes in WPS failure scenarios.
4885 */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004886 wpa_printf(MSG_DEBUG, "P2P: Canceled group formation due to "
4887 "P2P group network getting removed");
Dmitry Shmidt04949592012-07-19 12:16:46 -07004888 eloop_register_timeout(0, 0, wpas_p2p_group_formation_timeout,
4889 wpa_s->parent, NULL);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004890 }
4891}
4892
4893
4894struct wpa_ssid * wpas_p2p_get_persistent(struct wpa_supplicant *wpa_s,
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08004895 const u8 *addr, const u8 *ssid,
4896 size_t ssid_len)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004897{
4898 struct wpa_ssid *s;
4899 size_t i;
4900
4901 for (s = wpa_s->conf->ssid; s; s = s->next) {
4902 if (s->disabled != 2)
4903 continue;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08004904 if (ssid &&
4905 (ssid_len != s->ssid_len ||
4906 os_memcmp(ssid, s->ssid, ssid_len) != 0))
4907 continue;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004908 if (os_memcmp(s->bssid, addr, ETH_ALEN) == 0)
4909 return s; /* peer is GO in the persistent group */
4910 if (s->mode != WPAS_MODE_P2P_GO || s->p2p_client_list == NULL)
4911 continue;
4912 for (i = 0; i < s->num_p2p_clients; i++) {
4913 if (os_memcmp(s->p2p_client_list + i * ETH_ALEN,
4914 addr, ETH_ALEN) == 0)
4915 return s; /* peer is P2P client in persistent
4916 * group */
4917 }
4918 }
4919
4920 return NULL;
4921}
4922
4923
4924void wpas_p2p_notify_ap_sta_authorized(struct wpa_supplicant *wpa_s,
4925 const u8 *addr)
4926{
4927 if (addr == NULL)
4928 return;
4929 wpas_p2p_add_persistent_group_client(wpa_s, addr);
4930}
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08004931
Dmitry Shmidt04949592012-07-19 12:16:46 -07004932
4933static void wpas_p2p_fallback_to_go_neg(struct wpa_supplicant *wpa_s,
4934 int group_added)
4935{
4936 struct wpa_supplicant *group = wpa_s;
4937 eloop_cancel_timeout(wpas_p2p_pd_before_join_timeout, wpa_s, NULL);
4938 if (wpa_s->global->p2p_group_formation)
4939 group = wpa_s->global->p2p_group_formation;
4940 wpa_s = wpa_s->parent;
4941 offchannel_send_action_done(wpa_s);
4942 if (group_added)
4943 wpas_p2p_group_delete(group, 1);
4944 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Fall back to GO Negotiation");
4945 wpas_p2p_connect(wpa_s, wpa_s->pending_join_dev_addr, wpa_s->p2p_pin,
4946 wpa_s->p2p_wps_method, wpa_s->p2p_persistent_group, 0,
4947 0, 0, wpa_s->p2p_go_intent, wpa_s->p2p_connect_freq,
4948 wpa_s->p2p_persistent_id,
4949 wpa_s->p2p_pd_before_go_neg);
4950}
4951
4952
4953int wpas_p2p_scan_no_go_seen(struct wpa_supplicant *wpa_s)
4954{
4955 if (!wpa_s->p2p_fallback_to_go_neg ||
4956 wpa_s->p2p_in_provisioning <= 5)
4957 return 0;
4958
4959 if (wpas_p2p_peer_go(wpa_s, wpa_s->pending_join_dev_addr) > 0)
4960 return 0; /* peer operating as a GO */
4961
4962 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: GO not found for p2p_connect-auto - "
4963 "fallback to GO Negotiation");
4964 wpas_p2p_fallback_to_go_neg(wpa_s, 1);
4965
4966 return 1;
4967}
4968
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08004969#ifdef ANDROID_P2P
4970int wpas_p2p_handle_frequency_conflicts(struct wpa_supplicant *wpa_s, int freq)
4971{
4972 struct wpa_supplicant *iface = NULL;
4973 struct p2p_data *p2p = wpa_s->global->p2p;
4974
4975 for (iface = wpa_s->global->ifaces; iface; iface = iface->next) {
4976 if((iface->p2p_group_interface) && (iface->current_ssid) &&
4977 (iface->current_ssid->frequency != freq)) {
4978
4979 if (iface->p2p_group_interface == P2P_GROUP_INTERFACE_GO) {
4980 /* Try to see whether we can move the GO. If it
4981 * is not possible, remove the GO interface
4982 */
4983 if(wpa_drv_switch_channel(iface, freq) == 0) {
4984 wpa_printf(MSG_ERROR, "P2P: GO Moved to freq(%d)", freq);
4985 iface->current_ssid->frequency = freq;
4986 continue;
4987 }
4988 }
4989
4990 /* If GO cannot be moved or if the conflicting interface is a
4991 * P2P Client, remove the interface depending up on the connection
4992 * priority */
Dmitry Shmidt687922c2012-03-26 14:02:32 -07004993 if(!wpas_is_p2p_prioritized(wpa_s)) {
4994 /* STA connection has priority over existing
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08004995 * P2P connection. So remove the interface */
Dmitry Shmidt687922c2012-03-26 14:02:32 -07004996 wpa_printf(MSG_DEBUG, "P2P: Removing P2P connection due to Single channel"
4997 "concurrent mode frequency conflict");
4998 iface->removal_reason = P2P_GROUP_REMOVAL_FREQ_CONFLICT;
Dmitry Shmidt04949592012-07-19 12:16:46 -07004999 wpas_p2p_group_delete(iface, 0);
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08005000 } else {
5001 /* Existing connection has the priority. Disable the newly
5002 * selected network and let the application know about it.
5003 */
5004 return -1;
5005 }
5006 }
5007 }
5008 return 0;
5009}
5010#endif