blob: f4df4b0e6ee7a5aa9884b66121dca589f13e9e18 [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
Dmitry Shmidt04abaa92012-08-17 16:22:31 -07002537
2538#ifdef ANDROID_P2P
2539 if(wpa_s->drv_flags & WPA_DRIVER_FLAGS_MULTI_CHANNEL_CONCURRENT)
2540 p2p.p2p_concurrency = P2P_MULTI_CHANNEL_CONCURRENT;
2541 else if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_CONCURRENT)
2542 p2p.p2p_concurrency = P2P_SINGLE_CHANNEL_CONCURRENT;
2543#endif
2544
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002545 global->p2p = p2p_init(&p2p);
2546 if (global->p2p == NULL)
2547 return -1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07002548 global->p2p_init_wpa_s = wpa_s;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002549
2550 for (i = 0; i < MAX_WPS_VENDOR_EXT; i++) {
2551 if (wpa_s->conf->wps_vendor_ext[i] == NULL)
2552 continue;
2553 p2p_add_wps_vendor_extension(
2554 global->p2p, wpa_s->conf->wps_vendor_ext[i]);
2555 }
2556
2557 return 0;
2558}
2559
2560
2561/**
2562 * wpas_p2p_deinit - Deinitialize per-interface P2P data
2563 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
2564 *
2565 * This function deinitialize per-interface P2P data.
2566 */
2567void wpas_p2p_deinit(struct wpa_supplicant *wpa_s)
2568{
2569 if (wpa_s->driver && wpa_s->drv_priv)
2570 wpa_drv_probe_req_report(wpa_s, 0);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002571
2572 if (wpa_s->go_params) {
2573 /* Clear any stored provisioning info */
2574 p2p_clear_provisioning_info(
2575 wpa_s->global->p2p,
Dmitry Shmidt04949592012-07-19 12:16:46 -07002576 wpa_s->go_params->peer_device_addr);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002577 }
2578
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002579 os_free(wpa_s->go_params);
2580 wpa_s->go_params = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002581 eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s, NULL);
2582 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08002583 eloop_cancel_timeout(wpas_p2p_pd_before_join_timeout, wpa_s, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002584 wpa_s->p2p_long_listen = 0;
2585 eloop_cancel_timeout(wpas_p2p_long_listen_timeout, wpa_s, NULL);
2586 eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL);
2587 wpas_p2p_remove_pending_group_interface(wpa_s);
2588
2589 /* TODO: remove group interface from the driver if this wpa_s instance
2590 * is on top of a P2P group interface */
2591}
2592
2593
2594/**
2595 * wpas_p2p_deinit_global - Deinitialize global P2P module
2596 * @global: Pointer to global data from wpa_supplicant_init()
2597 *
2598 * This function deinitializes the global (per device) P2P module.
2599 */
2600void wpas_p2p_deinit_global(struct wpa_global *global)
2601{
2602 struct wpa_supplicant *wpa_s, *tmp;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002603
2604 if (global->p2p == NULL)
2605 return;
2606
2607 /* Remove remaining P2P group interfaces */
2608 wpa_s = global->ifaces;
2609 if (wpa_s)
2610 wpas_p2p_service_flush(wpa_s);
2611 while (wpa_s && wpa_s->p2p_group_interface != NOT_P2P_GROUP_INTERFACE)
2612 wpa_s = wpa_s->next;
2613 while (wpa_s) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002614 tmp = global->ifaces;
2615 while (tmp &&
2616 (tmp == wpa_s ||
2617 tmp->p2p_group_interface == NOT_P2P_GROUP_INTERFACE)) {
2618 tmp = tmp->next;
2619 }
2620 if (tmp == NULL)
2621 break;
Dmitry Shmidt04949592012-07-19 12:16:46 -07002622 /* Disconnect from the P2P group and deinit the interface */
2623 wpas_p2p_disconnect(tmp);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002624 }
2625
2626 /*
2627 * Deinit GO data on any possibly remaining interface (if main
2628 * interface is used as GO).
2629 */
2630 for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
2631 if (wpa_s->ap_iface)
2632 wpas_p2p_group_deinit(wpa_s);
2633 }
2634
2635 p2p_deinit(global->p2p);
2636 global->p2p = NULL;
Dmitry Shmidt04949592012-07-19 12:16:46 -07002637 global->p2p_init_wpa_s = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002638}
2639
2640
2641static int wpas_p2p_create_iface(struct wpa_supplicant *wpa_s)
2642{
2643 if (wpa_s->drv_flags &
2644 (WPA_DRIVER_FLAGS_P2P_DEDICATED_INTERFACE |
2645 WPA_DRIVER_FLAGS_P2P_MGMT_AND_NON_P2P))
2646 return 1; /* P2P group requires a new interface in every case
2647 */
2648 if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_CONCURRENT))
2649 return 0; /* driver does not support concurrent operations */
2650 if (wpa_s->global->ifaces->next)
2651 return 1; /* more that one interface already in use */
2652 if (wpa_s->wpa_state >= WPA_AUTHENTICATING)
2653 return 1; /* this interface is already in use */
2654 return 0;
2655}
2656
2657
2658static int wpas_p2p_start_go_neg(struct wpa_supplicant *wpa_s,
2659 const u8 *peer_addr,
2660 enum p2p_wps_method wps_method,
2661 int go_intent, const u8 *own_interface_addr,
Dmitry Shmidt04949592012-07-19 12:16:46 -07002662 unsigned int force_freq, int persistent_group,
2663 struct wpa_ssid *ssid)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002664{
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002665 if (persistent_group && wpa_s->conf->persistent_reconnect)
2666 persistent_group = 2;
2667
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002668 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT) {
2669 return wpa_drv_p2p_connect(wpa_s, peer_addr, wps_method,
2670 go_intent, own_interface_addr,
2671 force_freq, persistent_group);
2672 }
2673
2674 return p2p_connect(wpa_s->global->p2p, peer_addr, wps_method,
2675 go_intent, own_interface_addr, force_freq,
Dmitry Shmidt04949592012-07-19 12:16:46 -07002676 persistent_group, ssid ? ssid->ssid : NULL,
2677 ssid ? ssid->ssid_len : 0,
2678 wpa_s->p2p_pd_before_go_neg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002679}
2680
2681
2682static int wpas_p2p_auth_go_neg(struct wpa_supplicant *wpa_s,
2683 const u8 *peer_addr,
2684 enum p2p_wps_method wps_method,
2685 int go_intent, const u8 *own_interface_addr,
Dmitry Shmidt04949592012-07-19 12:16:46 -07002686 unsigned int force_freq, int persistent_group,
2687 struct wpa_ssid *ssid)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002688{
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002689 if (persistent_group && wpa_s->conf->persistent_reconnect)
2690 persistent_group = 2;
2691
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002692 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
2693 return -1;
2694
2695 return p2p_authorize(wpa_s->global->p2p, peer_addr, wps_method,
2696 go_intent, own_interface_addr, force_freq,
Dmitry Shmidt04949592012-07-19 12:16:46 -07002697 persistent_group, ssid ? ssid->ssid : NULL,
2698 ssid ? ssid->ssid_len : 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002699}
2700
2701
2702static void wpas_p2p_check_join_scan_limit(struct wpa_supplicant *wpa_s)
2703{
2704 wpa_s->p2p_join_scan_count++;
2705 wpa_printf(MSG_DEBUG, "P2P: Join scan attempt %d",
2706 wpa_s->p2p_join_scan_count);
2707 if (wpa_s->p2p_join_scan_count > P2P_MAX_JOIN_SCAN_ATTEMPTS) {
2708 wpa_printf(MSG_DEBUG, "P2P: Failed to find GO " MACSTR
2709 " for join operationg - stop join attempt",
2710 MAC2STR(wpa_s->pending_join_iface_addr));
2711 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
Dmitry Shmidt04949592012-07-19 12:16:46 -07002712 if (wpa_s->p2p_auto_pd) {
2713 wpa_s->p2p_auto_pd = 0;
2714 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_FAILURE
2715 " p2p_dev_addr=" MACSTR " status=N/A",
2716 MAC2STR(wpa_s->pending_join_dev_addr));
2717 return;
2718 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002719 wpa_msg(wpa_s->parent, MSG_INFO,
2720 P2P_EVENT_GROUP_FORMATION_FAILURE);
2721 }
2722}
2723
2724
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08002725static void wpas_p2p_pd_before_join_timeout(void *eloop_ctx, void *timeout_ctx)
2726{
2727 struct wpa_supplicant *wpa_s = eloop_ctx;
2728 if (!wpa_s->pending_pd_before_join)
2729 return;
2730 /*
2731 * Provision Discovery Response may have been lost - try to connect
2732 * anyway since we do not need any information from this PD.
2733 */
2734 wpa_printf(MSG_DEBUG, "P2P: PD timeout for join-existing-group - "
2735 "try to connect anyway");
2736 wpas_p2p_join_start(wpa_s);
2737}
2738
2739
Dmitry Shmidt04949592012-07-19 12:16:46 -07002740static int wpas_check_freq_conflict(struct wpa_supplicant *wpa_s, int freq)
2741{
2742 struct wpa_supplicant *iface;
2743 int shared_freq;
2744 u8 bssid[ETH_ALEN];
2745
2746 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_MULTI_CHANNEL_CONCURRENT)
2747 return 0;
2748
2749 for (iface = wpa_s->global->ifaces; iface; iface = iface->next) {
2750 if (!wpas_p2p_create_iface(wpa_s) && iface == wpa_s)
2751 continue;
2752 if (iface->current_ssid == NULL || iface->assoc_freq == 0)
2753 continue;
2754 if (iface->current_ssid->mode == WPAS_MODE_AP ||
2755 iface->current_ssid->mode == WPAS_MODE_P2P_GO)
2756 shared_freq = iface->current_ssid->frequency;
2757 else if (wpa_drv_get_bssid(iface, bssid) == 0)
2758 shared_freq = iface->assoc_freq;
2759 else
2760 shared_freq = 0;
2761
2762 if (shared_freq && freq != shared_freq) {
2763 wpa_printf(MSG_DEBUG, "P2P: Frequency conflict - %s "
2764 "connected on %d MHz - new connection on "
2765 "%d MHz", iface->ifname, shared_freq, freq);
2766 return 1;
2767 }
2768 }
2769
2770 shared_freq = wpa_drv_shared_freq(wpa_s);
2771 if (shared_freq > 0 && shared_freq != freq) {
2772 wpa_printf(MSG_DEBUG, "P2P: Frequency conflict - shared "
2773 "virtual interface connected on %d MHz - new "
2774 "connection on %d MHz", shared_freq, freq);
2775 return 1;
2776 }
2777
2778 return 0;
2779}
2780
2781
2782static int wpas_p2p_peer_go(struct wpa_supplicant *wpa_s,
2783 const u8 *peer_dev_addr)
2784{
2785 struct wpa_bss *bss;
2786 int updated;
2787
2788 bss = wpa_bss_get_p2p_dev_addr(wpa_s, peer_dev_addr);
2789 if (bss == NULL)
2790 return -1;
2791 if (bss->last_update_idx < wpa_s->bss_update_idx) {
2792 wpa_printf(MSG_DEBUG, "P2P: Peer BSS entry not updated in the "
2793 "last scan");
2794 return 0;
2795 }
2796
2797 updated = os_time_before(&wpa_s->p2p_auto_started, &bss->last_update);
2798 wpa_printf(MSG_DEBUG, "P2P: Current BSS entry for peer updated at "
2799 "%ld.%06ld (%supdated in last scan)",
2800 bss->last_update.sec, bss->last_update.usec,
2801 updated ? "": "not ");
2802
2803 return updated;
2804}
2805
2806
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002807static void wpas_p2p_scan_res_join(struct wpa_supplicant *wpa_s,
2808 struct wpa_scan_results *scan_res)
2809{
2810 struct wpa_bss *bss;
2811 int freq;
2812 u8 iface_addr[ETH_ALEN];
Dmitry Shmidt04949592012-07-19 12:16:46 -07002813
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002814 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
2815
2816 if (wpa_s->global->p2p_disabled)
2817 return;
2818
Dmitry Shmidt04949592012-07-19 12:16:46 -07002819 wpa_printf(MSG_DEBUG, "P2P: Scan results received (%d BSS) for %sjoin",
2820 scan_res ? (int) scan_res->num : -1,
2821 wpa_s->p2p_auto_join ? "auto_" : "");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002822
2823 if (scan_res)
2824 wpas_p2p_scan_res_handler(wpa_s, scan_res);
2825
Dmitry Shmidt04949592012-07-19 12:16:46 -07002826 if (wpa_s->p2p_auto_pd) {
2827 int join = wpas_p2p_peer_go(wpa_s,
2828 wpa_s->pending_join_dev_addr);
2829 if (join == 0 &&
2830 wpa_s->auto_pd_scan_retry < P2P_AUTO_PD_SCAN_ATTEMPTS) {
2831 wpa_s->auto_pd_scan_retry++;
2832 bss = wpa_bss_get_bssid(wpa_s,
2833 wpa_s->pending_join_dev_addr);
2834 if (bss) {
2835 freq = bss->freq;
2836 wpa_printf(MSG_DEBUG, "P2P: Scan retry %d for "
2837 "the peer " MACSTR " at %d MHz",
2838 wpa_s->auto_pd_scan_retry,
2839 MAC2STR(wpa_s->
2840 pending_join_dev_addr),
2841 freq);
2842 wpas_p2p_join_scan_req(wpa_s, freq);
2843 return;
2844 }
2845 }
2846
2847 if (join < 0)
2848 join = 0;
2849
2850 wpa_s->p2p_auto_pd = 0;
2851 wpa_s->pending_pd_use = join ? AUTO_PD_JOIN : AUTO_PD_GO_NEG;
2852 wpa_printf(MSG_DEBUG, "P2P: Auto PD with " MACSTR " join=%d",
2853 MAC2STR(wpa_s->pending_join_dev_addr), join);
2854 if (p2p_prov_disc_req(wpa_s->global->p2p,
2855 wpa_s->pending_join_dev_addr,
2856 wpa_s->pending_pd_config_methods, join,
2857 0) < 0) {
2858 wpa_s->p2p_auto_pd = 0;
2859 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_FAILURE
2860 " p2p_dev_addr=" MACSTR " status=N/A",
2861 MAC2STR(wpa_s->pending_join_dev_addr));
2862 }
2863 return;
2864 }
2865
2866 if (wpa_s->p2p_auto_join) {
2867 int join = wpas_p2p_peer_go(wpa_s,
2868 wpa_s->pending_join_dev_addr);
2869 if (join < 0) {
2870 wpa_printf(MSG_DEBUG, "P2P: Peer was not found to be "
2871 "running a GO -> use GO Negotiation");
2872 wpas_p2p_connect(wpa_s, wpa_s->pending_join_dev_addr,
2873 wpa_s->p2p_pin, wpa_s->p2p_wps_method,
2874 wpa_s->p2p_persistent_group, 0, 0, 0,
2875 wpa_s->p2p_go_intent,
2876 wpa_s->p2p_connect_freq,
2877 wpa_s->p2p_persistent_id,
2878 wpa_s->p2p_pd_before_go_neg);
2879 return;
2880 }
2881
2882 wpa_printf(MSG_DEBUG, "P2P: Peer was found running GO%s -> "
2883 "try to join the group", join ? "" :
2884 " in older scan");
2885 if (!join)
2886 wpa_s->p2p_fallback_to_go_neg = 1;
2887 }
2888
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002889 freq = p2p_get_oper_freq(wpa_s->global->p2p,
2890 wpa_s->pending_join_iface_addr);
2891 if (freq < 0 &&
2892 p2p_get_interface_addr(wpa_s->global->p2p,
2893 wpa_s->pending_join_dev_addr,
2894 iface_addr) == 0 &&
2895 os_memcmp(iface_addr, wpa_s->pending_join_dev_addr, ETH_ALEN) != 0)
2896 {
2897 wpa_printf(MSG_DEBUG, "P2P: Overwrite pending interface "
2898 "address for join from " MACSTR " to " MACSTR
2899 " based on newly discovered P2P peer entry",
2900 MAC2STR(wpa_s->pending_join_iface_addr),
2901 MAC2STR(iface_addr));
2902 os_memcpy(wpa_s->pending_join_iface_addr, iface_addr,
2903 ETH_ALEN);
2904
2905 freq = p2p_get_oper_freq(wpa_s->global->p2p,
2906 wpa_s->pending_join_iface_addr);
2907 }
2908 if (freq >= 0) {
2909 wpa_printf(MSG_DEBUG, "P2P: Target GO operating frequency "
2910 "from P2P peer table: %d MHz", freq);
2911 }
2912 bss = wpa_bss_get_bssid(wpa_s, wpa_s->pending_join_iface_addr);
2913 if (bss) {
2914 freq = bss->freq;
2915 wpa_printf(MSG_DEBUG, "P2P: Target GO operating frequency "
2916 "from BSS table: %d MHz", freq);
2917 }
2918 if (freq > 0) {
2919 u16 method;
2920
Dmitry Shmidt04949592012-07-19 12:16:46 -07002921 if (wpas_check_freq_conflict(wpa_s, freq) > 0) {
2922 wpa_msg(wpa_s->parent, MSG_INFO,
2923 P2P_EVENT_GROUP_FORMATION_FAILURE
2924 "reason=FREQ_CONFLICT");
2925 return;
2926 }
2927
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002928 wpa_printf(MSG_DEBUG, "P2P: Send Provision Discovery Request "
2929 "prior to joining an existing group (GO " MACSTR
2930 " freq=%u MHz)",
2931 MAC2STR(wpa_s->pending_join_dev_addr), freq);
2932 wpa_s->pending_pd_before_join = 1;
2933
2934 switch (wpa_s->pending_join_wps_method) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002935 case WPS_PIN_DISPLAY:
2936 method = WPS_CONFIG_KEYPAD;
2937 break;
2938 case WPS_PIN_KEYPAD:
2939 method = WPS_CONFIG_DISPLAY;
2940 break;
2941 case WPS_PBC:
2942 method = WPS_CONFIG_PUSHBUTTON;
2943 break;
2944 default:
2945 method = 0;
2946 break;
2947 }
2948
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002949 if ((p2p_get_provisioning_info(wpa_s->global->p2p,
2950 wpa_s->pending_join_dev_addr) ==
2951 method)) {
2952 /*
2953 * We have already performed provision discovery for
2954 * joining the group. Proceed directly to join
2955 * operation without duplicated provision discovery. */
2956 wpa_printf(MSG_DEBUG, "P2P: Provision discovery "
2957 "with " MACSTR " already done - proceed to "
2958 "join",
2959 MAC2STR(wpa_s->pending_join_dev_addr));
2960 wpa_s->pending_pd_before_join = 0;
2961 goto start;
2962 }
2963
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002964 if (p2p_prov_disc_req(wpa_s->global->p2p,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002965 wpa_s->pending_join_dev_addr, method, 1,
2966 freq) < 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002967 wpa_printf(MSG_DEBUG, "P2P: Failed to send Provision "
2968 "Discovery Request before joining an "
2969 "existing group");
2970 wpa_s->pending_pd_before_join = 0;
2971 goto start;
2972 }
2973
2974 /*
2975 * Actual join operation will be started from the Action frame
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08002976 * TX status callback (if no ACK is received) or when the
2977 * Provision Discovery Response is received. Use a short
2978 * timeout as a backup mechanism should the Provision Discovery
2979 * Response be lost for any reason.
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002980 */
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08002981 eloop_cancel_timeout(wpas_p2p_pd_before_join_timeout, wpa_s,
2982 NULL);
2983 eloop_register_timeout(2, 0, wpas_p2p_pd_before_join_timeout,
2984 wpa_s, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002985 return;
2986 }
2987
2988 wpa_printf(MSG_DEBUG, "P2P: Failed to find BSS/GO - try again later");
2989 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
2990 eloop_register_timeout(1, 0, wpas_p2p_join_scan, wpa_s, NULL);
2991 wpas_p2p_check_join_scan_limit(wpa_s);
2992 return;
2993
2994start:
2995 /* Start join operation immediately */
2996 wpas_p2p_join_start(wpa_s);
2997}
2998
2999
Dmitry Shmidt04949592012-07-19 12:16:46 -07003000static void wpas_p2p_join_scan_req(struct wpa_supplicant *wpa_s, int freq)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003001{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003002 int ret;
3003 struct wpa_driver_scan_params params;
3004 struct wpabuf *wps_ie, *ies;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003005 size_t ielen;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003006 int freqs[2] = { 0, 0 };
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003007
3008 os_memset(&params, 0, sizeof(params));
3009
3010 /* P2P Wildcard SSID */
3011 params.num_ssids = 1;
3012 params.ssids[0].ssid = (u8 *) P2P_WILDCARD_SSID;
3013 params.ssids[0].ssid_len = P2P_WILDCARD_SSID_LEN;
3014
3015 wpa_s->wps->dev.p2p = 1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003016 wps_ie = wps_build_probe_req_ie(DEV_PW_DEFAULT, &wpa_s->wps->dev,
3017 wpa_s->wps->uuid, WPS_REQ_ENROLLEE, 0,
3018 NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003019 if (wps_ie == NULL) {
3020 wpas_p2p_scan_res_join(wpa_s, NULL);
3021 return;
3022 }
3023
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003024 ielen = p2p_scan_ie_buf_len(wpa_s->global->p2p);
3025 ies = wpabuf_alloc(wpabuf_len(wps_ie) + ielen);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003026 if (ies == NULL) {
3027 wpabuf_free(wps_ie);
3028 wpas_p2p_scan_res_join(wpa_s, NULL);
3029 return;
3030 }
3031 wpabuf_put_buf(ies, wps_ie);
3032 wpabuf_free(wps_ie);
3033
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08003034 p2p_scan_ie(wpa_s->global->p2p, ies, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003035
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003036 params.p2p_probe = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003037 params.extra_ies = wpabuf_head(ies);
3038 params.extra_ies_len = wpabuf_len(ies);
Dmitry Shmidt04949592012-07-19 12:16:46 -07003039 if (freq > 0) {
3040 freqs[0] = freq;
3041 params.freqs = freqs;
3042 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003043
3044 /*
3045 * Run a scan to update BSS table and start Provision Discovery once
3046 * the new scan results become available.
3047 */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003048 ret = wpa_drv_scan(wpa_s, &params);
Dmitry Shmidt04949592012-07-19 12:16:46 -07003049 if (!ret)
3050 wpa_s->scan_res_handler = wpas_p2p_scan_res_join;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003051
3052 wpabuf_free(ies);
3053
3054 if (ret) {
3055 wpa_printf(MSG_DEBUG, "P2P: Failed to start scan for join - "
3056 "try again later");
3057 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
3058 eloop_register_timeout(1, 0, wpas_p2p_join_scan, wpa_s, NULL);
3059 wpas_p2p_check_join_scan_limit(wpa_s);
3060 }
3061}
3062
3063
Dmitry Shmidt04949592012-07-19 12:16:46 -07003064static void wpas_p2p_join_scan(void *eloop_ctx, void *timeout_ctx)
3065{
3066 struct wpa_supplicant *wpa_s = eloop_ctx;
3067 wpas_p2p_join_scan_req(wpa_s, 0);
3068}
3069
3070
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003071static int wpas_p2p_join(struct wpa_supplicant *wpa_s, const u8 *iface_addr,
Dmitry Shmidt04949592012-07-19 12:16:46 -07003072 const u8 *dev_addr, enum p2p_wps_method wps_method,
3073 int auto_join)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003074{
3075 wpa_printf(MSG_DEBUG, "P2P: Request to join existing group (iface "
Dmitry Shmidt04949592012-07-19 12:16:46 -07003076 MACSTR " dev " MACSTR ")%s",
3077 MAC2STR(iface_addr), MAC2STR(dev_addr),
3078 auto_join ? " (auto_join)" : "");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003079
Dmitry Shmidt04949592012-07-19 12:16:46 -07003080 wpa_s->p2p_auto_pd = 0;
3081 wpa_s->p2p_auto_join = !!auto_join;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003082 os_memcpy(wpa_s->pending_join_iface_addr, iface_addr, ETH_ALEN);
3083 os_memcpy(wpa_s->pending_join_dev_addr, dev_addr, ETH_ALEN);
3084 wpa_s->pending_join_wps_method = wps_method;
3085
3086 /* Make sure we are not running find during connection establishment */
3087 wpas_p2p_stop_find(wpa_s);
3088
3089 wpa_s->p2p_join_scan_count = 0;
3090 wpas_p2p_join_scan(wpa_s, NULL);
3091 return 0;
3092}
3093
3094
3095static int wpas_p2p_join_start(struct wpa_supplicant *wpa_s)
3096{
3097 struct wpa_supplicant *group;
3098 struct p2p_go_neg_results res;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003099 struct wpa_bss *bss;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003100
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08003101 eloop_cancel_timeout(wpas_p2p_pd_before_join_timeout, wpa_s, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003102 group = wpas_p2p_get_group_iface(wpa_s, 0, 0);
3103 if (group == NULL)
3104 return -1;
3105 if (group != wpa_s) {
3106 os_memcpy(group->p2p_pin, wpa_s->p2p_pin,
3107 sizeof(group->p2p_pin));
3108 group->p2p_wps_method = wpa_s->p2p_wps_method;
3109 }
3110
3111 group->p2p_in_provisioning = 1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003112 group->p2p_fallback_to_go_neg = wpa_s->p2p_fallback_to_go_neg;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003113
3114 os_memset(&res, 0, sizeof(res));
3115 os_memcpy(res.peer_interface_addr, wpa_s->pending_join_iface_addr,
3116 ETH_ALEN);
3117 res.wps_method = wpa_s->pending_join_wps_method;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003118 bss = wpa_bss_get_bssid(wpa_s, wpa_s->pending_join_iface_addr);
3119 if (bss) {
3120 res.freq = bss->freq;
3121 res.ssid_len = bss->ssid_len;
3122 os_memcpy(res.ssid, bss->ssid, bss->ssid_len);
3123 }
3124
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08003125 if (wpa_s->off_channel_freq || wpa_s->roc_waiting_drv_freq) {
3126 wpa_printf(MSG_DEBUG, "P2P: Cancel remain-on-channel prior to "
3127 "starting client");
3128 wpa_drv_cancel_remain_on_channel(wpa_s);
3129 wpa_s->off_channel_freq = 0;
3130 wpa_s->roc_waiting_drv_freq = 0;
3131 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003132 wpas_start_wps_enrollee(group, &res);
3133
3134 /*
3135 * Allow a longer timeout for join-a-running-group than normal 15
3136 * second group formation timeout since the GO may not have authorized
3137 * our connection yet.
3138 */
3139 eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s, NULL);
3140 eloop_register_timeout(60, 0, wpas_p2p_group_formation_timeout,
3141 wpa_s, NULL);
3142
3143 return 0;
3144}
3145
3146
3147/**
3148 * wpas_p2p_connect - Request P2P Group Formation to be started
3149 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
3150 * @peer_addr: Address of the peer P2P Device
3151 * @pin: PIN to use during provisioning or %NULL to indicate PBC mode
3152 * @persistent_group: Whether to create a persistent group
Dmitry Shmidt04949592012-07-19 12:16:46 -07003153 * @auto_join: Whether to select join vs. GO Negotiation automatically
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003154 * @join: Whether to join an existing group (as a client) instead of starting
3155 * Group Owner negotiation; @peer_addr is BSSID in that case
3156 * @auth: Whether to only authorize the connection instead of doing that and
3157 * initiating Group Owner negotiation
3158 * @go_intent: GO Intent or -1 to use default
3159 * @freq: Frequency for the group or 0 for auto-selection
Dmitry Shmidt04949592012-07-19 12:16:46 -07003160 * @persistent_id: Persistent group credentials to use for forcing GO
3161 * parameters or -1 to generate new values (SSID/passphrase)
3162 * @pd: Whether to send Provision Discovery prior to GO Negotiation as an
3163 * interoperability workaround when initiating group formation
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003164 * Returns: 0 or new PIN (if pin was %NULL) on success, -1 on unspecified
3165 * failure, -2 on failure due to channel not currently available,
3166 * -3 if forced channel is not supported
3167 */
3168int wpas_p2p_connect(struct wpa_supplicant *wpa_s, const u8 *peer_addr,
3169 const char *pin, enum p2p_wps_method wps_method,
Dmitry Shmidt04949592012-07-19 12:16:46 -07003170 int persistent_group, int auto_join, int join, int auth,
3171 int go_intent, int freq, int persistent_id, int pd)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003172{
3173 int force_freq = 0, oper_freq = 0;
3174 u8 bssid[ETH_ALEN];
3175 int ret = 0;
3176 enum wpa_driver_if_type iftype;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003177 const u8 *if_addr;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003178 struct wpa_ssid *ssid = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003179
3180 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
3181 return -1;
3182
Dmitry Shmidt04949592012-07-19 12:16:46 -07003183 if (persistent_id >= 0) {
3184 ssid = wpa_config_get_network(wpa_s->conf, persistent_id);
3185 if (ssid == NULL || ssid->disabled != 2 ||
3186 ssid->mode != WPAS_MODE_P2P_GO)
3187 return -1;
3188 }
3189
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003190 if (go_intent < 0)
3191 go_intent = wpa_s->conf->p2p_go_intent;
3192
3193 if (!auth)
3194 wpa_s->p2p_long_listen = 0;
3195
3196 wpa_s->p2p_wps_method = wps_method;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003197 wpa_s->p2p_persistent_group = !!persistent_group;
3198 wpa_s->p2p_persistent_id = persistent_id;
3199 wpa_s->p2p_go_intent = go_intent;
3200 wpa_s->p2p_connect_freq = freq;
3201 wpa_s->p2p_fallback_to_go_neg = 0;
3202 wpa_s->p2p_pd_before_go_neg = !!pd;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003203
3204 if (pin)
3205 os_strlcpy(wpa_s->p2p_pin, pin, sizeof(wpa_s->p2p_pin));
3206 else if (wps_method == WPS_PIN_DISPLAY) {
3207 ret = wps_generate_pin();
3208 os_snprintf(wpa_s->p2p_pin, sizeof(wpa_s->p2p_pin), "%08d",
3209 ret);
3210 wpa_printf(MSG_DEBUG, "P2P: Randomly generated PIN: %s",
3211 wpa_s->p2p_pin);
3212 } else
3213 wpa_s->p2p_pin[0] = '\0';
3214
Dmitry Shmidt04949592012-07-19 12:16:46 -07003215 if (join || auto_join) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003216 u8 iface_addr[ETH_ALEN], dev_addr[ETH_ALEN];
3217 if (auth) {
3218 wpa_printf(MSG_DEBUG, "P2P: Authorize invitation to "
3219 "connect a running group from " MACSTR,
3220 MAC2STR(peer_addr));
3221 os_memcpy(wpa_s->p2p_auth_invite, peer_addr, ETH_ALEN);
3222 return ret;
3223 }
3224 os_memcpy(dev_addr, peer_addr, ETH_ALEN);
3225 if (p2p_get_interface_addr(wpa_s->global->p2p, peer_addr,
3226 iface_addr) < 0) {
3227 os_memcpy(iface_addr, peer_addr, ETH_ALEN);
3228 p2p_get_dev_addr(wpa_s->global->p2p, peer_addr,
3229 dev_addr);
3230 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07003231 if (auto_join) {
3232 os_get_time(&wpa_s->p2p_auto_started);
3233 wpa_printf(MSG_DEBUG, "P2P: Auto join started at "
3234 "%ld.%06ld",
3235 wpa_s->p2p_auto_started.sec,
3236 wpa_s->p2p_auto_started.usec);
3237 }
3238 if (wpas_p2p_join(wpa_s, iface_addr, dev_addr, wps_method,
3239 auto_join) < 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003240 return -1;
3241 return ret;
3242 }
3243
3244 if (wpa_s->current_ssid && wpa_drv_get_bssid(wpa_s, bssid) == 0 &&
3245 wpa_s->assoc_freq)
3246 oper_freq = wpa_s->assoc_freq;
3247 else {
3248 oper_freq = wpa_drv_shared_freq(wpa_s);
3249 if (oper_freq < 0)
3250 oper_freq = 0;
3251 }
3252
3253 if (freq > 0) {
3254 if (!p2p_supported_freq(wpa_s->global->p2p, freq)) {
3255 wpa_printf(MSG_DEBUG, "P2P: The forced channel "
3256 "(%u MHz) is not supported for P2P uses",
3257 freq);
3258 return -3;
3259 }
3260
3261 if (oper_freq > 0 && freq != oper_freq &&
3262 !(wpa_s->drv_flags &
3263 WPA_DRIVER_FLAGS_MULTI_CHANNEL_CONCURRENT)) {
3264 wpa_printf(MSG_DEBUG, "P2P: Cannot start P2P group "
3265 "on %u MHz while connected on another "
3266 "channel (%u MHz)", freq, oper_freq);
3267 return -2;
3268 }
3269 wpa_printf(MSG_DEBUG, "P2P: Trying to force us to use the "
3270 "requested channel (%u MHz)", freq);
3271 force_freq = freq;
3272 } else if (oper_freq > 0 &&
3273 !p2p_supported_freq(wpa_s->global->p2p, oper_freq)) {
3274 if (!(wpa_s->drv_flags &
3275 WPA_DRIVER_FLAGS_MULTI_CHANNEL_CONCURRENT)) {
3276 wpa_printf(MSG_DEBUG, "P2P: Cannot start P2P group "
3277 "while connected on non-P2P supported "
3278 "channel (%u MHz)", oper_freq);
3279 return -2;
3280 }
3281 wpa_printf(MSG_DEBUG, "P2P: Current operating channel "
3282 "(%u MHz) not available for P2P - try to use "
3283 "another channel", oper_freq);
3284 force_freq = 0;
3285 } else if (oper_freq > 0) {
3286 wpa_printf(MSG_DEBUG, "P2P: Trying to force us to use the "
3287 "channel we are already using (%u MHz) on another "
3288 "interface", oper_freq);
3289 force_freq = oper_freq;
3290 }
3291
3292 wpa_s->create_p2p_iface = wpas_p2p_create_iface(wpa_s);
3293
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003294 if (wpa_s->create_p2p_iface) {
3295 /* Prepare to add a new interface for the group */
3296 iftype = WPA_IF_P2P_GROUP;
3297 if (go_intent == 15)
3298 iftype = WPA_IF_P2P_GO;
3299 if (wpas_p2p_add_group_interface(wpa_s, iftype) < 0) {
3300 wpa_printf(MSG_ERROR, "P2P: Failed to allocate a new "
3301 "interface for the group");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003302 return -1;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003303 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003304
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003305 if_addr = wpa_s->pending_interface_addr;
3306 } else
3307 if_addr = wpa_s->own_addr;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003308
3309 if (auth) {
3310 if (wpas_p2p_auth_go_neg(wpa_s, peer_addr, wps_method,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003311 go_intent, if_addr,
Dmitry Shmidt04949592012-07-19 12:16:46 -07003312 force_freq, persistent_group, ssid) <
3313 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003314 return -1;
3315 return ret;
3316 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003317
3318 if (wpas_p2p_start_go_neg(wpa_s, peer_addr, wps_method,
3319 go_intent, if_addr, force_freq,
Dmitry Shmidt04949592012-07-19 12:16:46 -07003320 persistent_group, ssid) < 0) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003321 if (wpa_s->create_p2p_iface)
3322 wpas_p2p_remove_pending_group_interface(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003323 return -1;
3324 }
3325 return ret;
3326}
3327
3328
3329/**
3330 * wpas_p2p_remain_on_channel_cb - Indication of remain-on-channel start
3331 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
3332 * @freq: Frequency of the channel in MHz
3333 * @duration: Duration of the stay on the channel in milliseconds
3334 *
3335 * This callback is called when the driver indicates that it has started the
3336 * requested remain-on-channel duration.
3337 */
3338void wpas_p2p_remain_on_channel_cb(struct wpa_supplicant *wpa_s,
3339 unsigned int freq, unsigned int duration)
3340{
3341 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
3342 return;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003343 if (wpa_s->off_channel_freq == wpa_s->pending_listen_freq) {
3344 p2p_listen_cb(wpa_s->global->p2p, wpa_s->pending_listen_freq,
3345 wpa_s->pending_listen_duration);
3346 wpa_s->pending_listen_freq = 0;
3347 }
3348}
3349
3350
3351static int wpas_p2p_listen_start(struct wpa_supplicant *wpa_s,
3352 unsigned int timeout)
3353{
3354 /* Limit maximum Listen state time based on driver limitation. */
3355 if (timeout > wpa_s->max_remain_on_chan)
3356 timeout = wpa_s->max_remain_on_chan;
3357
3358 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
3359 return wpa_drv_p2p_listen(wpa_s, timeout);
3360
3361 return p2p_listen(wpa_s->global->p2p, timeout);
3362}
3363
3364
3365/**
3366 * wpas_p2p_cancel_remain_on_channel_cb - Remain-on-channel timeout
3367 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
3368 * @freq: Frequency of the channel in MHz
3369 *
3370 * This callback is called when the driver indicates that a remain-on-channel
3371 * operation has been completed, i.e., the duration on the requested channel
3372 * has timed out.
3373 */
3374void wpas_p2p_cancel_remain_on_channel_cb(struct wpa_supplicant *wpa_s,
3375 unsigned int freq)
3376{
3377 wpa_printf(MSG_DEBUG, "P2P: Cancel remain-on-channel callback "
3378 "(p2p_long_listen=%d ms pending_action_tx=%p)",
3379 wpa_s->p2p_long_listen, wpa_s->pending_action_tx);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003380 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
3381 return;
3382 if (p2p_listen_end(wpa_s->global->p2p, freq) > 0)
3383 return; /* P2P module started a new operation */
3384 if (wpa_s->pending_action_tx)
3385 return;
3386 if (wpa_s->p2p_long_listen > 0)
3387 wpa_s->p2p_long_listen -= wpa_s->max_remain_on_chan;
3388 if (wpa_s->p2p_long_listen > 0) {
3389 wpa_printf(MSG_DEBUG, "P2P: Continuing long Listen state");
3390 wpas_p2p_listen_start(wpa_s, wpa_s->p2p_long_listen);
3391 }
3392}
3393
3394
3395/**
3396 * wpas_p2p_group_remove - Remove a P2P group
3397 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
3398 * @ifname: Network interface name of the group interface or "*" to remove all
3399 * groups
3400 * Returns: 0 on success, -1 on failure
3401 *
3402 * This function is used to remove a P2P group. This can be used to disconnect
3403 * from a group in which the local end is a P2P Client or to end a P2P Group in
3404 * case the local end is the Group Owner. If a virtual network interface was
3405 * created for this group, that interface will be removed. Otherwise, only the
3406 * configured P2P group network will be removed from the interface.
3407 */
3408int wpas_p2p_group_remove(struct wpa_supplicant *wpa_s, const char *ifname)
3409{
3410 struct wpa_global *global = wpa_s->global;
3411
3412 if (os_strcmp(ifname, "*") == 0) {
3413 struct wpa_supplicant *prev;
3414 wpa_s = global->ifaces;
3415 while (wpa_s) {
3416 prev = wpa_s;
3417 wpa_s = wpa_s->next;
3418 wpas_p2p_disconnect(prev);
3419 }
3420 return 0;
3421 }
3422
3423 for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
3424 if (os_strcmp(wpa_s->ifname, ifname) == 0)
3425 break;
3426 }
3427
3428 return wpas_p2p_disconnect(wpa_s);
3429}
3430
3431
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003432static int wpas_p2p_init_go_params(struct wpa_supplicant *wpa_s,
3433 struct p2p_go_neg_results *params,
3434 int freq)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003435{
3436 u8 bssid[ETH_ALEN];
3437 int res;
3438
3439 os_memset(params, 0, sizeof(*params));
3440 params->role_go = 1;
3441 if (freq) {
3442 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on forced "
3443 "frequency %d MHz", freq);
3444 params->freq = freq;
3445 } else if (wpa_s->conf->p2p_oper_reg_class == 81 &&
3446 wpa_s->conf->p2p_oper_channel >= 1 &&
3447 wpa_s->conf->p2p_oper_channel <= 11) {
3448 params->freq = 2407 + 5 * wpa_s->conf->p2p_oper_channel;
3449 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on configured "
3450 "frequency %d MHz", params->freq);
3451 } else if (wpa_s->conf->p2p_oper_reg_class == 115 ||
Jouni Malinen87fd2792011-05-16 18:35:42 +03003452 wpa_s->conf->p2p_oper_reg_class == 124) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003453 params->freq = 5000 + 5 * wpa_s->conf->p2p_oper_channel;
3454 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on configured "
3455 "frequency %d MHz", params->freq);
3456 } else if (wpa_s->conf->p2p_oper_channel == 0 &&
3457 wpa_s->best_overall_freq > 0 &&
3458 p2p_supported_freq(wpa_s->global->p2p,
3459 wpa_s->best_overall_freq)) {
3460 params->freq = wpa_s->best_overall_freq;
3461 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on best overall "
3462 "channel %d MHz", params->freq);
3463 } else if (wpa_s->conf->p2p_oper_channel == 0 &&
3464 wpa_s->best_24_freq > 0 &&
3465 p2p_supported_freq(wpa_s->global->p2p,
3466 wpa_s->best_24_freq)) {
3467 params->freq = wpa_s->best_24_freq;
3468 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on best 2.4 GHz "
3469 "channel %d MHz", params->freq);
3470 } else if (wpa_s->conf->p2p_oper_channel == 0 &&
3471 wpa_s->best_5_freq > 0 &&
3472 p2p_supported_freq(wpa_s->global->p2p,
3473 wpa_s->best_5_freq)) {
3474 params->freq = wpa_s->best_5_freq;
3475 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on best 5 GHz "
3476 "channel %d MHz", params->freq);
3477 } else {
Dmitry Shmidt04949592012-07-19 12:16:46 -07003478 int chan;
3479 for (chan = 0; chan < 11; chan++) {
3480 params->freq = 2412 + chan * 5;
3481 if (!wpas_p2p_disallowed_freq(wpa_s->global,
3482 params->freq))
3483 break;
3484 }
3485 if (chan == 11) {
3486 wpa_printf(MSG_DEBUG, "P2P: No 2.4 GHz channel "
3487 "allowed");
3488 return -1;
3489 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003490 wpa_printf(MSG_DEBUG, "P2P: Set GO freq %d MHz (no preference "
3491 "known)", params->freq);
3492 }
3493
3494 if (wpa_s->current_ssid && wpa_drv_get_bssid(wpa_s, bssid) == 0 &&
3495 wpa_s->assoc_freq && !freq) {
3496 wpa_printf(MSG_DEBUG, "P2P: Force GO on the channel we are "
3497 "already using");
3498 params->freq = wpa_s->assoc_freq;
3499 }
3500
3501 res = wpa_drv_shared_freq(wpa_s);
3502 if (res > 0 && !freq) {
3503 wpa_printf(MSG_DEBUG, "P2P: Force GO on the channel we are "
3504 "already using on a shared interface");
3505 params->freq = res;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003506 } else if (res > 0 && freq != res &&
3507 !(wpa_s->drv_flags &
3508 WPA_DRIVER_FLAGS_MULTI_CHANNEL_CONCURRENT)) {
3509 wpa_printf(MSG_DEBUG, "P2P: Cannot start P2P group on %u MHz "
3510 "while connected on another channel (%u MHz)",
3511 freq, res);
3512 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003513 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003514
3515 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003516}
3517
3518
3519static struct wpa_supplicant *
3520wpas_p2p_get_group_iface(struct wpa_supplicant *wpa_s, int addr_allocated,
3521 int go)
3522{
3523 struct wpa_supplicant *group_wpa_s;
3524
3525 if (!wpas_p2p_create_iface(wpa_s))
3526 return wpa_s;
3527
3528 if (wpas_p2p_add_group_interface(wpa_s, go ? WPA_IF_P2P_GO :
3529 WPA_IF_P2P_CLIENT) < 0)
3530 return NULL;
3531 group_wpa_s = wpas_p2p_init_group_interface(wpa_s, go);
3532 if (group_wpa_s == NULL) {
3533 wpas_p2p_remove_pending_group_interface(wpa_s);
3534 return NULL;
3535 }
3536
3537 return group_wpa_s;
3538}
3539
3540
3541/**
3542 * wpas_p2p_group_add - Add a new P2P group with local end as Group Owner
3543 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
3544 * @persistent_group: Whether to create a persistent group
3545 * @freq: Frequency for the group or 0 to indicate no hardcoding
3546 * Returns: 0 on success, -1 on failure
3547 *
3548 * This function creates a new P2P group with the local end as the Group Owner,
3549 * i.e., without using Group Owner Negotiation.
3550 */
3551int wpas_p2p_group_add(struct wpa_supplicant *wpa_s, int persistent_group,
3552 int freq)
3553{
3554 struct p2p_go_neg_results params;
3555 unsigned int r;
3556
3557 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
3558 return -1;
3559
Dmitry Shmidtdca39792011-09-06 11:17:33 -07003560 /* Make sure we are not running find during connection establishment */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003561 wpa_printf(MSG_DEBUG, "P2P: Stop any on-going P2P FIND");
Dmitry Shmidtdca39792011-09-06 11:17:33 -07003562 wpas_p2p_stop_find(wpa_s);
Dmitry Shmidtdca39792011-09-06 11:17:33 -07003563
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003564 if (freq == 2) {
3565 wpa_printf(MSG_DEBUG, "P2P: Request to start GO on 2.4 GHz "
3566 "band");
3567 if (wpa_s->best_24_freq > 0 &&
3568 p2p_supported_freq(wpa_s->global->p2p,
3569 wpa_s->best_24_freq)) {
3570 freq = wpa_s->best_24_freq;
3571 wpa_printf(MSG_DEBUG, "P2P: Use best 2.4 GHz band "
3572 "channel: %d MHz", freq);
3573 } else {
3574 os_get_random((u8 *) &r, sizeof(r));
3575 freq = 2412 + (r % 3) * 25;
3576 wpa_printf(MSG_DEBUG, "P2P: Use random 2.4 GHz band "
3577 "channel: %d MHz", freq);
3578 }
3579 }
3580
3581 if (freq == 5) {
3582 wpa_printf(MSG_DEBUG, "P2P: Request to start GO on 5 GHz "
3583 "band");
3584 if (wpa_s->best_5_freq > 0 &&
3585 p2p_supported_freq(wpa_s->global->p2p,
3586 wpa_s->best_5_freq)) {
3587 freq = wpa_s->best_5_freq;
3588 wpa_printf(MSG_DEBUG, "P2P: Use best 5 GHz band "
3589 "channel: %d MHz", freq);
3590 } else {
3591 os_get_random((u8 *) &r, sizeof(r));
3592 freq = 5180 + (r % 4) * 20;
3593 if (!p2p_supported_freq(wpa_s->global->p2p, freq)) {
3594 wpa_printf(MSG_DEBUG, "P2P: Could not select "
3595 "5 GHz channel for P2P group");
3596 return -1;
3597 }
3598 wpa_printf(MSG_DEBUG, "P2P: Use random 5 GHz band "
3599 "channel: %d MHz", freq);
3600 }
3601 }
3602
3603 if (freq > 0 && !p2p_supported_freq(wpa_s->global->p2p, freq)) {
3604 wpa_printf(MSG_DEBUG, "P2P: The forced channel for GO "
3605 "(%u MHz) is not supported for P2P uses",
3606 freq);
3607 return -1;
3608 }
3609
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003610 if (wpas_p2p_init_go_params(wpa_s, &params, freq))
3611 return -1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003612 if (params.freq &&
3613 !p2p_supported_freq(wpa_s->global->p2p, params.freq)) {
3614 wpa_printf(MSG_DEBUG, "P2P: The selected channel for GO "
3615 "(%u MHz) is not supported for P2P uses",
3616 params.freq);
3617 return -1;
3618 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003619 p2p_go_params(wpa_s->global->p2p, &params);
3620 params.persistent_group = persistent_group;
3621
3622 wpa_s = wpas_p2p_get_group_iface(wpa_s, 0, 1);
3623 if (wpa_s == NULL)
3624 return -1;
3625 wpas_start_wps_go(wpa_s, &params, 0);
3626
3627 return 0;
3628}
3629
3630
3631static int wpas_start_p2p_client(struct wpa_supplicant *wpa_s,
3632 struct wpa_ssid *params, int addr_allocated)
3633{
3634 struct wpa_ssid *ssid;
3635
3636 wpa_s = wpas_p2p_get_group_iface(wpa_s, addr_allocated, 0);
3637 if (wpa_s == NULL)
3638 return -1;
3639
3640 wpa_supplicant_ap_deinit(wpa_s);
3641
3642 ssid = wpa_config_add_network(wpa_s->conf);
3643 if (ssid == NULL)
3644 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003645 wpa_config_set_network_defaults(ssid);
3646 ssid->temporary = 1;
3647 ssid->proto = WPA_PROTO_RSN;
3648 ssid->pairwise_cipher = WPA_CIPHER_CCMP;
3649 ssid->group_cipher = WPA_CIPHER_CCMP;
3650 ssid->key_mgmt = WPA_KEY_MGMT_PSK;
3651 ssid->ssid = os_malloc(params->ssid_len);
3652 if (ssid->ssid == NULL) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003653 wpa_config_remove_network(wpa_s->conf, ssid->id);
3654 return -1;
3655 }
3656 os_memcpy(ssid->ssid, params->ssid, params->ssid_len);
3657 ssid->ssid_len = params->ssid_len;
3658 ssid->p2p_group = 1;
3659 ssid->export_keys = 1;
3660 if (params->psk_set) {
3661 os_memcpy(ssid->psk, params->psk, 32);
3662 ssid->psk_set = 1;
3663 }
3664 if (params->passphrase)
3665 ssid->passphrase = os_strdup(params->passphrase);
3666
3667 wpa_supplicant_select_network(wpa_s, ssid);
3668
3669 wpa_s->show_group_started = 1;
3670
3671 return 0;
3672}
3673
3674
3675int wpas_p2p_group_add_persistent(struct wpa_supplicant *wpa_s,
3676 struct wpa_ssid *ssid, int addr_allocated,
3677 int freq)
3678{
3679 struct p2p_go_neg_results params;
3680 int go = 0;
3681
3682 if (ssid->disabled != 2 || ssid->ssid == NULL)
3683 return -1;
3684
3685 if (wpas_get_p2p_group(wpa_s, ssid->ssid, ssid->ssid_len, &go) &&
3686 go == (ssid->mode == WPAS_MODE_P2P_GO)) {
3687 wpa_printf(MSG_DEBUG, "P2P: Requested persistent group is "
3688 "already running");
3689 return 0;
3690 }
3691
3692 /* Make sure we are not running find during connection establishment */
3693 wpas_p2p_stop_find(wpa_s);
3694
Dmitry Shmidt04949592012-07-19 12:16:46 -07003695 wpa_s->p2p_fallback_to_go_neg = 0;
3696
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003697 if (ssid->mode == WPAS_MODE_INFRA)
3698 return wpas_start_p2p_client(wpa_s, ssid, addr_allocated);
3699
3700 if (ssid->mode != WPAS_MODE_P2P_GO)
3701 return -1;
3702
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003703 if (wpas_p2p_init_go_params(wpa_s, &params, freq))
3704 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003705
3706 params.role_go = 1;
3707 if (ssid->passphrase == NULL ||
3708 os_strlen(ssid->passphrase) >= sizeof(params.passphrase)) {
3709 wpa_printf(MSG_DEBUG, "P2P: Invalid passphrase in persistent "
3710 "group");
3711 return -1;
3712 }
3713 os_strlcpy(params.passphrase, ssid->passphrase,
3714 sizeof(params.passphrase));
3715 os_memcpy(params.ssid, ssid->ssid, ssid->ssid_len);
3716 params.ssid_len = ssid->ssid_len;
3717 params.persistent_group = 1;
3718
3719 wpa_s = wpas_p2p_get_group_iface(wpa_s, addr_allocated, 1);
3720 if (wpa_s == NULL)
3721 return -1;
3722
3723 wpas_start_wps_go(wpa_s, &params, 0);
3724
3725 return 0;
3726}
3727
3728
3729static void wpas_p2p_ie_update(void *ctx, struct wpabuf *beacon_ies,
3730 struct wpabuf *proberesp_ies)
3731{
3732 struct wpa_supplicant *wpa_s = ctx;
3733 if (wpa_s->ap_iface) {
3734 struct hostapd_data *hapd = wpa_s->ap_iface->bss[0];
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003735 if (!(hapd->conf->p2p & P2P_GROUP_OWNER)) {
3736 wpabuf_free(beacon_ies);
3737 wpabuf_free(proberesp_ies);
3738 return;
3739 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003740 if (beacon_ies) {
3741 wpabuf_free(hapd->p2p_beacon_ie);
3742 hapd->p2p_beacon_ie = beacon_ies;
3743 }
3744 wpabuf_free(hapd->p2p_probe_resp_ie);
3745 hapd->p2p_probe_resp_ie = proberesp_ies;
3746 } else {
3747 wpabuf_free(beacon_ies);
3748 wpabuf_free(proberesp_ies);
3749 }
3750 wpa_supplicant_ap_update_beacon(wpa_s);
3751}
3752
3753
3754static void wpas_p2p_idle_update(void *ctx, int idle)
3755{
3756 struct wpa_supplicant *wpa_s = ctx;
3757 if (!wpa_s->ap_iface)
3758 return;
3759 wpa_printf(MSG_DEBUG, "P2P: GO - group %sidle", idle ? "" : "not ");
Dmitry Shmidt04949592012-07-19 12:16:46 -07003760 if (idle)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003761 wpas_p2p_set_group_idle_timeout(wpa_s);
3762 else
3763 eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL);
3764}
3765
3766
3767struct p2p_group * wpas_p2p_group_init(struct wpa_supplicant *wpa_s,
Dmitry Shmidt04949592012-07-19 12:16:46 -07003768 struct wpa_ssid *ssid)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003769{
3770 struct p2p_group *group;
3771 struct p2p_group_config *cfg;
3772
3773 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
3774 return NULL;
3775 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
3776 return NULL;
3777
3778 cfg = os_zalloc(sizeof(*cfg));
3779 if (cfg == NULL)
3780 return NULL;
3781
Dmitry Shmidt04949592012-07-19 12:16:46 -07003782 if (ssid->p2p_persistent_group && wpa_s->conf->persistent_reconnect)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003783 cfg->persistent_group = 2;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003784 else if (ssid->p2p_persistent_group)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003785 cfg->persistent_group = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003786 os_memcpy(cfg->interface_addr, wpa_s->own_addr, ETH_ALEN);
3787 if (wpa_s->max_stations &&
3788 wpa_s->max_stations < wpa_s->conf->max_num_sta)
3789 cfg->max_clients = wpa_s->max_stations;
3790 else
3791 cfg->max_clients = wpa_s->conf->max_num_sta;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003792 os_memcpy(cfg->ssid, ssid->ssid, ssid->ssid_len);
3793 cfg->ssid_len = ssid->ssid_len;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003794 cfg->cb_ctx = wpa_s;
3795 cfg->ie_update = wpas_p2p_ie_update;
3796 cfg->idle_update = wpas_p2p_idle_update;
3797
3798 group = p2p_group_init(wpa_s->global->p2p, cfg);
3799 if (group == NULL)
3800 os_free(cfg);
Dmitry Shmidt04949592012-07-19 12:16:46 -07003801 if (ssid->mode != WPAS_MODE_P2P_GROUP_FORMATION)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003802 p2p_group_notif_formation_done(group);
3803 wpa_s->p2p_group = group;
3804 return group;
3805}
3806
3807
3808void wpas_p2p_wps_success(struct wpa_supplicant *wpa_s, const u8 *peer_addr,
3809 int registrar)
3810{
Dmitry Shmidt04949592012-07-19 12:16:46 -07003811 struct wpa_ssid *ssid = wpa_s->current_ssid;
3812
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003813 if (!wpa_s->p2p_in_provisioning) {
3814 wpa_printf(MSG_DEBUG, "P2P: Ignore WPS success event - P2P "
3815 "provisioning not in progress");
3816 return;
3817 }
3818
Dmitry Shmidt04949592012-07-19 12:16:46 -07003819 if (ssid && ssid->mode == WPAS_MODE_INFRA) {
3820 u8 go_dev_addr[ETH_ALEN];
3821 os_memcpy(go_dev_addr, wpa_s->bssid, ETH_ALEN);
3822 wpas_p2p_persistent_group(wpa_s, go_dev_addr, ssid->ssid,
3823 ssid->ssid_len);
3824 /* Clear any stored provisioning info */
3825 p2p_clear_provisioning_info(wpa_s->global->p2p, go_dev_addr);
3826 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003827
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003828 eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s->parent,
3829 NULL);
Dmitry Shmidt04949592012-07-19 12:16:46 -07003830 if (ssid && ssid->mode == WPAS_MODE_INFRA) {
3831 /*
3832 * Use a separate timeout for initial data connection to
3833 * complete to allow the group to be removed automatically if
3834 * something goes wrong in this step before the P2P group idle
3835 * timeout mechanism is taken into use.
3836 */
3837 eloop_register_timeout(P2P_MAX_INITIAL_CONN_WAIT, 0,
3838 wpas_p2p_group_formation_timeout,
3839 wpa_s->parent, NULL);
3840 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003841 if (wpa_s->global->p2p)
3842 p2p_wps_success_cb(wpa_s->global->p2p, peer_addr);
3843 else if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
3844 wpa_drv_wps_success_cb(wpa_s, peer_addr);
3845 wpas_group_formation_completed(wpa_s, 1);
3846}
3847
3848
Jouni Malinen75ecf522011-06-27 15:19:46 -07003849void wpas_p2p_wps_failed(struct wpa_supplicant *wpa_s,
3850 struct wps_event_fail *fail)
3851{
3852 if (!wpa_s->p2p_in_provisioning) {
3853 wpa_printf(MSG_DEBUG, "P2P: Ignore WPS fail event - P2P "
3854 "provisioning not in progress");
3855 return;
3856 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003857
3858 if (wpa_s->go_params) {
3859 p2p_clear_provisioning_info(
3860 wpa_s->global->p2p,
Dmitry Shmidt04949592012-07-19 12:16:46 -07003861 wpa_s->go_params->peer_device_addr);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003862 }
3863
Jouni Malinen75ecf522011-06-27 15:19:46 -07003864 wpas_notify_p2p_wps_failed(wpa_s, fail);
3865}
3866
3867
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003868int wpas_p2p_prov_disc(struct wpa_supplicant *wpa_s, const u8 *peer_addr,
Dmitry Shmidt04949592012-07-19 12:16:46 -07003869 const char *config_method,
3870 enum wpas_p2p_prov_disc_use use)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003871{
3872 u16 config_methods;
3873
Dmitry Shmidt04949592012-07-19 12:16:46 -07003874 wpa_s->p2p_fallback_to_go_neg = 0;
3875 wpa_s->pending_pd_use = NORMAL_PD;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003876 if (os_strncmp(config_method, "display", 7) == 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003877 config_methods = WPS_CONFIG_DISPLAY;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003878 else if (os_strncmp(config_method, "keypad", 6) == 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003879 config_methods = WPS_CONFIG_KEYPAD;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003880 else if (os_strncmp(config_method, "pbc", 3) == 0 ||
3881 os_strncmp(config_method, "pushbutton", 10) == 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003882 config_methods = WPS_CONFIG_PUSHBUTTON;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003883 else {
3884 wpa_printf(MSG_DEBUG, "P2P: Unknown config method");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003885 return -1;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003886 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003887
Dmitry Shmidt04949592012-07-19 12:16:46 -07003888 if (use == WPAS_P2P_PD_AUTO) {
3889 os_memcpy(wpa_s->pending_join_dev_addr, peer_addr, ETH_ALEN);
3890 wpa_s->pending_pd_config_methods = config_methods;
3891 wpa_s->p2p_auto_pd = 1;
3892 wpa_s->p2p_auto_join = 0;
3893 wpa_s->pending_pd_before_join = 0;
3894 wpa_s->auto_pd_scan_retry = 0;
3895 wpas_p2p_stop_find(wpa_s);
3896 wpa_s->p2p_join_scan_count = 0;
3897 os_get_time(&wpa_s->p2p_auto_started);
3898 wpa_printf(MSG_DEBUG, "P2P: Auto PD started at %ld.%06ld",
3899 wpa_s->p2p_auto_started.sec,
3900 wpa_s->p2p_auto_started.usec);
3901 wpas_p2p_join_scan(wpa_s, NULL);
3902 return 0;
3903 }
3904
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003905 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT) {
3906 return wpa_drv_p2p_prov_disc_req(wpa_s, peer_addr,
Dmitry Shmidt04949592012-07-19 12:16:46 -07003907 config_methods,
3908 use == WPAS_P2P_PD_FOR_JOIN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003909 }
3910
3911 if (wpa_s->global->p2p == NULL || wpa_s->global->p2p_disabled)
3912 return -1;
3913
3914 return p2p_prov_disc_req(wpa_s->global->p2p, peer_addr,
Dmitry Shmidt04949592012-07-19 12:16:46 -07003915 config_methods, use == WPAS_P2P_PD_FOR_JOIN,
3916 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003917}
3918
3919
3920int wpas_p2p_scan_result_text(const u8 *ies, size_t ies_len, char *buf,
3921 char *end)
3922{
3923 return p2p_scan_result_text(ies, ies_len, buf, end);
3924}
3925
3926
3927static void wpas_p2p_clear_pending_action_tx(struct wpa_supplicant *wpa_s)
3928{
3929 if (!wpa_s->pending_action_tx)
3930 return;
3931
3932 wpa_printf(MSG_DEBUG, "P2P: Drop pending Action TX due to new "
3933 "operation request");
3934 wpabuf_free(wpa_s->pending_action_tx);
3935 wpa_s->pending_action_tx = NULL;
3936}
3937
3938
3939int wpas_p2p_find(struct wpa_supplicant *wpa_s, unsigned int timeout,
3940 enum p2p_discovery_type type,
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08003941 unsigned int num_req_dev_types, const u8 *req_dev_types,
3942 const u8 *dev_id)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003943{
3944 wpas_p2p_clear_pending_action_tx(wpa_s);
3945 wpa_s->p2p_long_listen = 0;
3946
3947 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
3948 return wpa_drv_p2p_find(wpa_s, timeout, type);
3949
Dmitry Shmidt04949592012-07-19 12:16:46 -07003950 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL ||
3951 wpa_s->p2p_in_provisioning)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003952 return -1;
3953
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08003954 wpa_supplicant_cancel_sched_scan(wpa_s);
3955
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003956 return p2p_find(wpa_s->global->p2p, timeout, type,
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08003957 num_req_dev_types, req_dev_types, dev_id);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003958}
3959
3960
3961void wpas_p2p_stop_find(struct wpa_supplicant *wpa_s)
3962{
3963 wpas_p2p_clear_pending_action_tx(wpa_s);
3964 wpa_s->p2p_long_listen = 0;
3965 eloop_cancel_timeout(wpas_p2p_long_listen_timeout, wpa_s, NULL);
3966 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003967 wpa_s->p2p_cb_on_scan_complete = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003968
3969 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT) {
3970 wpa_drv_p2p_stop_find(wpa_s);
3971 return;
3972 }
3973
3974 if (wpa_s->global->p2p)
3975 p2p_stop_find(wpa_s->global->p2p);
3976
3977 wpas_p2p_remove_pending_group_interface(wpa_s);
3978}
3979
3980
3981static void wpas_p2p_long_listen_timeout(void *eloop_ctx, void *timeout_ctx)
3982{
3983 struct wpa_supplicant *wpa_s = eloop_ctx;
3984 wpa_s->p2p_long_listen = 0;
3985}
3986
3987
3988int wpas_p2p_listen(struct wpa_supplicant *wpa_s, unsigned int timeout)
3989{
3990 int res;
3991
3992 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
3993 return -1;
3994
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08003995 wpa_supplicant_cancel_sched_scan(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003996 wpas_p2p_clear_pending_action_tx(wpa_s);
3997
3998 if (timeout == 0) {
3999 /*
4000 * This is a request for unlimited Listen state. However, at
4001 * least for now, this is mapped to a Listen state for one
4002 * hour.
4003 */
4004 timeout = 3600;
4005 }
4006 eloop_cancel_timeout(wpas_p2p_long_listen_timeout, wpa_s, NULL);
4007 wpa_s->p2p_long_listen = 0;
4008
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004009 /*
4010 * Stop previous find/listen operation to avoid trying to request a new
4011 * remain-on-channel operation while the driver is still running the
4012 * previous one.
4013 */
4014 if (wpa_s->global->p2p)
4015 p2p_stop_find(wpa_s->global->p2p);
4016
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004017 res = wpas_p2p_listen_start(wpa_s, timeout * 1000);
4018 if (res == 0 && timeout * 1000 > wpa_s->max_remain_on_chan) {
4019 wpa_s->p2p_long_listen = timeout * 1000;
4020 eloop_register_timeout(timeout, 0,
4021 wpas_p2p_long_listen_timeout,
4022 wpa_s, NULL);
4023 }
4024
4025 return res;
4026}
4027
4028
4029int wpas_p2p_assoc_req_ie(struct wpa_supplicant *wpa_s, struct wpa_bss *bss,
4030 u8 *buf, size_t len, int p2p_group)
4031{
4032 struct wpabuf *p2p_ie;
4033 int ret;
4034
4035 if (wpa_s->global->p2p_disabled)
4036 return -1;
4037 if (wpa_s->global->p2p == NULL)
4038 return -1;
4039 if (bss == NULL)
4040 return -1;
4041
4042 p2p_ie = wpa_bss_get_vendor_ie_multi(bss, P2P_IE_VENDOR_TYPE);
4043 ret = p2p_assoc_req_ie(wpa_s->global->p2p, bss->bssid, buf, len,
4044 p2p_group, p2p_ie);
4045 wpabuf_free(p2p_ie);
4046
4047 return ret;
4048}
4049
4050
4051int wpas_p2p_probe_req_rx(struct wpa_supplicant *wpa_s, const u8 *addr,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004052 const u8 *dst, const u8 *bssid,
Dmitry Shmidt04949592012-07-19 12:16:46 -07004053 const u8 *ie, size_t ie_len, int ssi_signal)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004054{
4055 if (wpa_s->global->p2p_disabled)
4056 return 0;
4057 if (wpa_s->global->p2p == NULL)
4058 return 0;
4059
Dmitry Shmidt04949592012-07-19 12:16:46 -07004060 switch (p2p_probe_req_rx(wpa_s->global->p2p, addr, dst, bssid,
4061 ie, ie_len)) {
4062 case P2P_PREQ_NOT_P2P:
4063 wpas_notify_preq(wpa_s, addr, dst, bssid, ie, ie_len,
4064 ssi_signal);
4065 /* fall through */
4066 case P2P_PREQ_MALFORMED:
4067 case P2P_PREQ_NOT_LISTEN:
4068 case P2P_PREQ_NOT_PROCESSED:
4069 default: /* make gcc happy */
4070 return 0;
4071 case P2P_PREQ_PROCESSED:
4072 return 1;
4073 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004074}
4075
4076
4077void wpas_p2p_rx_action(struct wpa_supplicant *wpa_s, const u8 *da,
4078 const u8 *sa, const u8 *bssid,
4079 u8 category, const u8 *data, size_t len, int freq)
4080{
4081 if (wpa_s->global->p2p_disabled)
4082 return;
4083 if (wpa_s->global->p2p == NULL)
4084 return;
4085
4086 p2p_rx_action(wpa_s->global->p2p, da, sa, bssid, category, data, len,
4087 freq);
4088}
4089
4090
4091void wpas_p2p_scan_ie(struct wpa_supplicant *wpa_s, struct wpabuf *ies)
4092{
4093 if (wpa_s->global->p2p_disabled)
4094 return;
4095 if (wpa_s->global->p2p == NULL)
4096 return;
4097
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08004098 p2p_scan_ie(wpa_s->global->p2p, ies, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004099}
4100
4101
4102void wpas_p2p_group_deinit(struct wpa_supplicant *wpa_s)
4103{
4104 p2p_group_deinit(wpa_s->p2p_group);
4105 wpa_s->p2p_group = NULL;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004106
4107 wpa_s->ap_configured_cb = NULL;
4108 wpa_s->ap_configured_cb_ctx = NULL;
4109 wpa_s->ap_configured_cb_data = NULL;
4110 wpa_s->connect_without_scan = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004111}
4112
4113
4114int wpas_p2p_reject(struct wpa_supplicant *wpa_s, const u8 *addr)
4115{
4116 wpa_s->p2p_long_listen = 0;
4117
4118 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
4119 return wpa_drv_p2p_reject(wpa_s, addr);
4120
4121 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
4122 return -1;
4123
4124 return p2p_reject(wpa_s->global->p2p, addr);
4125}
4126
4127
4128/* Invite to reinvoke a persistent group */
4129int wpas_p2p_invite(struct wpa_supplicant *wpa_s, const u8 *peer_addr,
4130 struct wpa_ssid *ssid, const u8 *go_dev_addr)
4131{
4132 enum p2p_invite_role role;
4133 u8 *bssid = NULL;
4134
4135 if (ssid->mode == WPAS_MODE_P2P_GO) {
4136 role = P2P_INVITE_ROLE_GO;
4137 if (peer_addr == NULL) {
4138 wpa_printf(MSG_DEBUG, "P2P: Missing peer "
4139 "address in invitation command");
4140 return -1;
4141 }
4142 if (wpas_p2p_create_iface(wpa_s)) {
4143 if (wpas_p2p_add_group_interface(wpa_s,
4144 WPA_IF_P2P_GO) < 0) {
4145 wpa_printf(MSG_ERROR, "P2P: Failed to "
4146 "allocate a new interface for the "
4147 "group");
4148 return -1;
4149 }
4150 bssid = wpa_s->pending_interface_addr;
4151 } else
4152 bssid = wpa_s->own_addr;
4153 } else {
4154 role = P2P_INVITE_ROLE_CLIENT;
4155 peer_addr = ssid->bssid;
4156 }
4157 wpa_s->pending_invite_ssid_id = ssid->id;
4158
4159 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
4160 return wpa_drv_p2p_invite(wpa_s, peer_addr, role, bssid,
4161 ssid->ssid, ssid->ssid_len,
4162 go_dev_addr, 1);
4163
4164 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
4165 return -1;
4166
4167 return p2p_invite(wpa_s->global->p2p, peer_addr, role, bssid,
4168 ssid->ssid, ssid->ssid_len, 0, go_dev_addr, 1);
4169}
4170
4171
4172/* Invite to join an active group */
4173int wpas_p2p_invite_group(struct wpa_supplicant *wpa_s, const char *ifname,
4174 const u8 *peer_addr, const u8 *go_dev_addr)
4175{
4176 struct wpa_global *global = wpa_s->global;
4177 enum p2p_invite_role role;
4178 u8 *bssid = NULL;
4179 struct wpa_ssid *ssid;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08004180 int persistent;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004181
4182 for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
4183 if (os_strcmp(wpa_s->ifname, ifname) == 0)
4184 break;
4185 }
4186 if (wpa_s == NULL) {
4187 wpa_printf(MSG_DEBUG, "P2P: Interface '%s' not found", ifname);
4188 return -1;
4189 }
4190
4191 ssid = wpa_s->current_ssid;
4192 if (ssid == NULL) {
4193 wpa_printf(MSG_DEBUG, "P2P: No current SSID to use for "
4194 "invitation");
4195 return -1;
4196 }
4197
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08004198 persistent = ssid->p2p_persistent_group &&
4199 wpas_p2p_get_persistent(wpa_s->parent, peer_addr,
4200 ssid->ssid, ssid->ssid_len);
4201
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004202 if (ssid->mode == WPAS_MODE_P2P_GO) {
4203 role = P2P_INVITE_ROLE_ACTIVE_GO;
4204 bssid = wpa_s->own_addr;
4205 if (go_dev_addr == NULL)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004206 go_dev_addr = wpa_s->global->p2p_dev_addr;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004207 } else {
4208 role = P2P_INVITE_ROLE_CLIENT;
4209 if (wpa_s->wpa_state < WPA_ASSOCIATED) {
4210 wpa_printf(MSG_DEBUG, "P2P: Not associated - cannot "
4211 "invite to current group");
4212 return -1;
4213 }
4214 bssid = wpa_s->bssid;
4215 if (go_dev_addr == NULL &&
4216 !is_zero_ether_addr(wpa_s->go_dev_addr))
4217 go_dev_addr = wpa_s->go_dev_addr;
4218 }
4219 wpa_s->parent->pending_invite_ssid_id = -1;
4220
4221 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
4222 return wpa_drv_p2p_invite(wpa_s, peer_addr, role, bssid,
4223 ssid->ssid, ssid->ssid_len,
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08004224 go_dev_addr, persistent);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004225
4226 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
4227 return -1;
4228
4229 return p2p_invite(wpa_s->global->p2p, peer_addr, role, bssid,
4230 ssid->ssid, ssid->ssid_len, wpa_s->assoc_freq,
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08004231 go_dev_addr, persistent);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004232}
4233
4234
4235void wpas_p2p_completed(struct wpa_supplicant *wpa_s)
4236{
4237 struct wpa_ssid *ssid = wpa_s->current_ssid;
4238 const char *ssid_txt;
4239 u8 go_dev_addr[ETH_ALEN];
Jouni Malinen75ecf522011-06-27 15:19:46 -07004240 int network_id = -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004241 int persistent;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004242 int freq;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004243
Dmitry Shmidt04949592012-07-19 12:16:46 -07004244 if (ssid == NULL || ssid->mode != WPAS_MODE_P2P_GROUP_FORMATION) {
4245 eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
4246 wpa_s->parent, NULL);
4247 }
4248
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004249 if (!wpa_s->show_group_started || !ssid)
4250 return;
4251
4252 wpa_s->show_group_started = 0;
4253
4254 ssid_txt = wpa_ssid_txt(ssid->ssid, ssid->ssid_len);
4255 os_memset(go_dev_addr, 0, ETH_ALEN);
4256 if (ssid->bssid_set)
4257 os_memcpy(go_dev_addr, ssid->bssid, ETH_ALEN);
4258 persistent = wpas_p2p_persistent_group(wpa_s, go_dev_addr, ssid->ssid,
4259 ssid->ssid_len);
4260 os_memcpy(wpa_s->go_dev_addr, go_dev_addr, ETH_ALEN);
4261
4262 if (wpa_s->global->p2p_group_formation == wpa_s)
4263 wpa_s->global->p2p_group_formation = NULL;
4264
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004265 freq = wpa_s->current_bss ? wpa_s->current_bss->freq :
4266 (int) wpa_s->assoc_freq;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004267 if (ssid->passphrase == NULL && ssid->psk_set) {
4268 char psk[65];
4269 wpa_snprintf_hex(psk, sizeof(psk), ssid->psk, 32);
4270 wpa_msg(wpa_s->parent, MSG_INFO, P2P_EVENT_GROUP_STARTED
4271 "%s client ssid=\"%s\" freq=%d psk=%s go_dev_addr="
4272 MACSTR "%s",
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004273 wpa_s->ifname, ssid_txt, freq, psk,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004274 MAC2STR(go_dev_addr),
4275 persistent ? " [PERSISTENT]" : "");
4276 } else {
4277 wpa_msg(wpa_s->parent, MSG_INFO, P2P_EVENT_GROUP_STARTED
4278 "%s client ssid=\"%s\" freq=%d passphrase=\"%s\" "
4279 "go_dev_addr=" MACSTR "%s",
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004280 wpa_s->ifname, ssid_txt, freq,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004281 ssid->passphrase ? ssid->passphrase : "",
4282 MAC2STR(go_dev_addr),
4283 persistent ? " [PERSISTENT]" : "");
4284 }
4285
4286 if (persistent)
Jouni Malinen75ecf522011-06-27 15:19:46 -07004287 network_id = wpas_p2p_store_persistent_group(wpa_s->parent,
4288 ssid, go_dev_addr);
4289 if (network_id < 0)
4290 network_id = ssid->id;
4291 wpas_notify_p2p_group_started(wpa_s, ssid, network_id, 1);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004292}
4293
4294
4295int wpas_p2p_presence_req(struct wpa_supplicant *wpa_s, u32 duration1,
4296 u32 interval1, u32 duration2, u32 interval2)
4297{
4298 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
4299 return -1;
4300 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
4301 return -1;
4302
4303 if (wpa_s->wpa_state < WPA_ASSOCIATED ||
4304 wpa_s->current_ssid == NULL ||
4305 wpa_s->current_ssid->mode != WPAS_MODE_INFRA)
4306 return -1;
4307
4308 return p2p_presence_req(wpa_s->global->p2p, wpa_s->bssid,
4309 wpa_s->own_addr, wpa_s->assoc_freq,
4310 duration1, interval1, duration2, interval2);
4311}
4312
4313
4314int wpas_p2p_ext_listen(struct wpa_supplicant *wpa_s, unsigned int period,
4315 unsigned int interval)
4316{
4317 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
4318 return -1;
4319
4320 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
4321 return -1;
4322
4323 return p2p_ext_listen(wpa_s->global->p2p, period, interval);
4324}
4325
4326
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004327static int wpas_p2p_is_client(struct wpa_supplicant *wpa_s)
4328{
4329 return wpa_s->current_ssid != NULL &&
4330 wpa_s->current_ssid->p2p_group &&
4331 wpa_s->current_ssid->mode == WPAS_MODE_INFRA;
4332}
4333
4334
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004335static void wpas_p2p_group_idle_timeout(void *eloop_ctx, void *timeout_ctx)
4336{
4337 struct wpa_supplicant *wpa_s = eloop_ctx;
4338
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004339 if (wpa_s->conf->p2p_group_idle == 0 && !wpas_p2p_is_client(wpa_s)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004340 wpa_printf(MSG_DEBUG, "P2P: Ignore group idle timeout - "
4341 "disabled");
4342 return;
4343 }
4344
Dmitry Shmidt04949592012-07-19 12:16:46 -07004345 wpa_printf(MSG_DEBUG, "P2P: Group idle timeout reached - terminate "
4346 "group");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004347 wpa_s->removal_reason = P2P_GROUP_REMOVAL_IDLE_TIMEOUT;
Dmitry Shmidt04949592012-07-19 12:16:46 -07004348 wpas_p2p_group_delete(wpa_s, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004349}
4350
4351
4352static void wpas_p2p_set_group_idle_timeout(struct wpa_supplicant *wpa_s)
4353{
Dmitry Shmidt04949592012-07-19 12:16:46 -07004354 int timeout;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004355
Dmitry Shmidt04949592012-07-19 12:16:46 -07004356 if (eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL) > 0)
4357 wpa_printf(MSG_DEBUG, "P2P: Cancelled P2P group idle timeout");
4358
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004359 if (wpa_s->current_ssid == NULL || !wpa_s->current_ssid->p2p_group)
4360 return;
4361
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004362 timeout = wpa_s->conf->p2p_group_idle;
4363 if (wpa_s->current_ssid->mode == WPAS_MODE_INFRA &&
4364 (timeout == 0 || timeout > P2P_MAX_CLIENT_IDLE))
4365 timeout = P2P_MAX_CLIENT_IDLE;
4366
4367 if (timeout == 0)
4368 return;
4369
Dmitry Shmidt04949592012-07-19 12:16:46 -07004370 if (timeout < 0) {
4371 if (wpa_s->current_ssid->mode == WPAS_MODE_INFRA)
4372 timeout = 0; /* special client mode no-timeout */
4373 else
4374 return;
4375 }
4376
4377 if (wpa_s->p2p_in_provisioning) {
4378 /*
4379 * Use the normal group formation timeout during the
4380 * provisioning phase to avoid terminating this process too
4381 * early due to group idle timeout.
4382 */
4383 wpa_printf(MSG_DEBUG, "P2P: Do not use P2P group idle timeout "
4384 "during provisioning");
4385 return;
4386 }
4387
4388 if (wpa_s->show_group_started) {
4389 /*
4390 * Use the normal group formation timeout between the end of
4391 * the provisioning phase and completion of 4-way handshake to
4392 * avoid terminating this process too early due to group idle
4393 * timeout.
4394 */
4395 wpa_printf(MSG_DEBUG, "P2P: Do not use P2P group idle timeout "
4396 "while waiting for initial 4-way handshake to "
4397 "complete");
4398 return;
4399 }
4400
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004401 wpa_printf(MSG_DEBUG, "P2P: Set P2P group idle timeout to %u seconds",
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004402 timeout);
4403 eloop_register_timeout(timeout, 0, wpas_p2p_group_idle_timeout,
4404 wpa_s, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004405}
4406
4407
4408void wpas_p2p_deauth_notif(struct wpa_supplicant *wpa_s, const u8 *bssid,
Dmitry Shmidt04949592012-07-19 12:16:46 -07004409 u16 reason_code, const u8 *ie, size_t ie_len,
4410 int locally_generated)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004411{
4412 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
4413 return;
4414 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
4415 return;
4416
Dmitry Shmidt04949592012-07-19 12:16:46 -07004417 if (!locally_generated)
4418 p2p_deauth_notif(wpa_s->global->p2p, bssid, reason_code, ie,
4419 ie_len);
4420
4421 if (reason_code == WLAN_REASON_DEAUTH_LEAVING && !locally_generated &&
4422 wpa_s->current_ssid &&
4423 wpa_s->current_ssid->p2p_group &&
4424 wpa_s->current_ssid->mode == WPAS_MODE_INFRA) {
4425 wpa_printf(MSG_DEBUG, "P2P: GO indicated that the P2P Group "
4426 "session is ending");
4427 wpa_s->removal_reason = P2P_GROUP_REMOVAL_GO_ENDING_SESSION;
4428 wpas_p2p_group_delete(wpa_s, 0);
4429 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004430}
4431
4432
4433void wpas_p2p_disassoc_notif(struct wpa_supplicant *wpa_s, const u8 *bssid,
Dmitry Shmidt04949592012-07-19 12:16:46 -07004434 u16 reason_code, const u8 *ie, size_t ie_len,
4435 int locally_generated)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004436{
4437 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
4438 return;
4439 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
4440 return;
4441
Dmitry Shmidt04949592012-07-19 12:16:46 -07004442 if (!locally_generated)
4443 p2p_disassoc_notif(wpa_s->global->p2p, bssid, reason_code, ie,
4444 ie_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004445}
4446
4447
4448void wpas_p2p_update_config(struct wpa_supplicant *wpa_s)
4449{
4450 struct p2p_data *p2p = wpa_s->global->p2p;
4451
4452 if (p2p == NULL)
4453 return;
4454
4455 if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_CAPABLE))
4456 return;
4457
4458 if (wpa_s->conf->changed_parameters & CFG_CHANGED_DEVICE_NAME)
4459 p2p_set_dev_name(p2p, wpa_s->conf->device_name);
4460
4461 if (wpa_s->conf->changed_parameters & CFG_CHANGED_DEVICE_TYPE)
4462 p2p_set_pri_dev_type(p2p, wpa_s->conf->device_type);
4463
4464 if (wpa_s->wps &&
4465 (wpa_s->conf->changed_parameters & CFG_CHANGED_CONFIG_METHODS))
4466 p2p_set_config_methods(p2p, wpa_s->wps->config_methods);
4467
4468 if (wpa_s->wps && (wpa_s->conf->changed_parameters & CFG_CHANGED_UUID))
4469 p2p_set_uuid(p2p, wpa_s->wps->uuid);
4470
4471 if (wpa_s->conf->changed_parameters & CFG_CHANGED_WPS_STRING) {
4472 p2p_set_manufacturer(p2p, wpa_s->conf->manufacturer);
4473 p2p_set_model_name(p2p, wpa_s->conf->model_name);
4474 p2p_set_model_number(p2p, wpa_s->conf->model_number);
4475 p2p_set_serial_number(p2p, wpa_s->conf->serial_number);
4476 }
4477
4478 if (wpa_s->conf->changed_parameters & CFG_CHANGED_SEC_DEVICE_TYPE)
4479 p2p_set_sec_dev_types(p2p,
4480 (void *) wpa_s->conf->sec_device_type,
4481 wpa_s->conf->num_sec_device_types);
4482
4483 if (wpa_s->conf->changed_parameters & CFG_CHANGED_VENDOR_EXTENSION) {
4484 int i;
4485 p2p_remove_wps_vendor_extensions(p2p);
4486 for (i = 0; i < MAX_WPS_VENDOR_EXT; i++) {
4487 if (wpa_s->conf->wps_vendor_ext[i] == NULL)
4488 continue;
4489 p2p_add_wps_vendor_extension(
4490 p2p, wpa_s->conf->wps_vendor_ext[i]);
4491 }
4492 }
4493
4494 if ((wpa_s->conf->changed_parameters & CFG_CHANGED_COUNTRY) &&
4495 wpa_s->conf->country[0] && wpa_s->conf->country[1]) {
4496 char country[3];
4497 country[0] = wpa_s->conf->country[0];
4498 country[1] = wpa_s->conf->country[1];
4499 country[2] = 0x04;
4500 p2p_set_country(p2p, country);
4501 }
4502
4503 if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_SSID_POSTFIX) {
4504 p2p_set_ssid_postfix(p2p, (u8 *) wpa_s->conf->p2p_ssid_postfix,
4505 wpa_s->conf->p2p_ssid_postfix ?
4506 os_strlen(wpa_s->conf->p2p_ssid_postfix) :
4507 0);
4508 }
4509
4510 if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_INTRA_BSS)
4511 p2p_set_intra_bss_dist(p2p, wpa_s->conf->p2p_intra_bss);
Jouni Malinen75ecf522011-06-27 15:19:46 -07004512
4513 if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_LISTEN_CHANNEL) {
4514 u8 reg_class, channel;
4515 int ret;
4516 unsigned int r;
4517 if (wpa_s->conf->p2p_listen_reg_class &&
4518 wpa_s->conf->p2p_listen_channel) {
4519 reg_class = wpa_s->conf->p2p_listen_reg_class;
4520 channel = wpa_s->conf->p2p_listen_channel;
4521 } else {
4522 reg_class = 81;
4523 /*
4524 * Pick one of the social channels randomly as the
4525 * listen channel.
4526 */
4527 os_get_random((u8 *) &r, sizeof(r));
4528 channel = 1 + (r % 3) * 5;
4529 }
4530 ret = p2p_set_listen_channel(p2p, reg_class, channel);
4531 if (ret)
4532 wpa_printf(MSG_ERROR, "P2P: Own listen channel update "
4533 "failed: %d", ret);
4534 }
4535 if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_OPER_CHANNEL) {
4536 u8 op_reg_class, op_channel, cfg_op_channel;
4537 int ret = 0;
4538 unsigned int r;
4539 if (wpa_s->conf->p2p_oper_reg_class &&
4540 wpa_s->conf->p2p_oper_channel) {
4541 op_reg_class = wpa_s->conf->p2p_oper_reg_class;
4542 op_channel = wpa_s->conf->p2p_oper_channel;
4543 cfg_op_channel = 1;
4544 } else {
4545 op_reg_class = 81;
4546 /*
4547 * Use random operation channel from (1, 6, 11)
4548 *if no other preference is indicated.
4549 */
4550 os_get_random((u8 *) &r, sizeof(r));
4551 op_channel = 1 + (r % 3) * 5;
4552 cfg_op_channel = 0;
4553 }
4554 ret = p2p_set_oper_channel(p2p, op_reg_class, op_channel,
4555 cfg_op_channel);
4556 if (ret)
4557 wpa_printf(MSG_ERROR, "P2P: Own oper channel update "
4558 "failed: %d", ret);
4559 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07004560
4561 if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_PREF_CHAN) {
4562 if (p2p_set_pref_chan(p2p, wpa_s->conf->num_p2p_pref_chan,
4563 wpa_s->conf->p2p_pref_chan) < 0) {
4564 wpa_printf(MSG_ERROR, "P2P: Preferred channel list "
4565 "update failed");
4566 }
4567 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004568}
4569
4570
4571int wpas_p2p_set_noa(struct wpa_supplicant *wpa_s, u8 count, int start,
4572 int duration)
4573{
4574 if (!wpa_s->ap_iface)
4575 return -1;
4576 return hostapd_p2p_set_noa(wpa_s->ap_iface->bss[0], count, start,
4577 duration);
4578}
4579
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004580
4581int wpas_p2p_set_cross_connect(struct wpa_supplicant *wpa_s, int enabled)
4582{
4583 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
4584 return -1;
4585 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
4586 return -1;
4587
4588 wpa_s->global->cross_connection = enabled;
4589 p2p_set_cross_connect(wpa_s->global->p2p, enabled);
4590
4591 if (!enabled) {
4592 struct wpa_supplicant *iface;
4593
4594 for (iface = wpa_s->global->ifaces; iface; iface = iface->next)
4595 {
4596 if (iface->cross_connect_enabled == 0)
4597 continue;
4598
4599 iface->cross_connect_enabled = 0;
4600 iface->cross_connect_in_use = 0;
4601 wpa_msg(iface->parent, MSG_INFO,
4602 P2P_EVENT_CROSS_CONNECT_DISABLE "%s %s",
4603 iface->ifname, iface->cross_connect_uplink);
4604 }
4605 }
4606
4607 return 0;
4608}
4609
4610
4611static void wpas_p2p_enable_cross_connect(struct wpa_supplicant *uplink)
4612{
4613 struct wpa_supplicant *iface;
4614
4615 if (!uplink->global->cross_connection)
4616 return;
4617
4618 for (iface = uplink->global->ifaces; iface; iface = iface->next) {
4619 if (!iface->cross_connect_enabled)
4620 continue;
4621 if (os_strcmp(uplink->ifname, iface->cross_connect_uplink) !=
4622 0)
4623 continue;
4624 if (iface->ap_iface == NULL)
4625 continue;
4626 if (iface->cross_connect_in_use)
4627 continue;
4628
4629 iface->cross_connect_in_use = 1;
4630 wpa_msg(iface->parent, MSG_INFO,
4631 P2P_EVENT_CROSS_CONNECT_ENABLE "%s %s",
4632 iface->ifname, iface->cross_connect_uplink);
4633 }
4634}
4635
4636
4637static void wpas_p2p_disable_cross_connect(struct wpa_supplicant *uplink)
4638{
4639 struct wpa_supplicant *iface;
4640
4641 for (iface = uplink->global->ifaces; iface; iface = iface->next) {
4642 if (!iface->cross_connect_enabled)
4643 continue;
4644 if (os_strcmp(uplink->ifname, iface->cross_connect_uplink) !=
4645 0)
4646 continue;
4647 if (!iface->cross_connect_in_use)
4648 continue;
4649
4650 wpa_msg(iface->parent, MSG_INFO,
4651 P2P_EVENT_CROSS_CONNECT_DISABLE "%s %s",
4652 iface->ifname, iface->cross_connect_uplink);
4653 iface->cross_connect_in_use = 0;
4654 }
4655}
4656
4657
4658void wpas_p2p_notif_connected(struct wpa_supplicant *wpa_s)
4659{
4660 if (wpa_s->ap_iface || wpa_s->current_ssid == NULL ||
4661 wpa_s->current_ssid->mode != WPAS_MODE_INFRA ||
4662 wpa_s->cross_connect_disallowed)
4663 wpas_p2p_disable_cross_connect(wpa_s);
4664 else
4665 wpas_p2p_enable_cross_connect(wpa_s);
Dmitry Shmidt04949592012-07-19 12:16:46 -07004666 if (!wpa_s->ap_iface &&
4667 eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL) > 0)
4668 wpa_printf(MSG_DEBUG, "P2P: Cancelled P2P group idle timeout");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004669}
4670
4671
4672void wpas_p2p_notif_disconnected(struct wpa_supplicant *wpa_s)
4673{
4674 wpas_p2p_disable_cross_connect(wpa_s);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004675 if (!wpa_s->ap_iface &&
4676 !eloop_is_timeout_registered(wpas_p2p_group_idle_timeout,
4677 wpa_s, NULL))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004678 wpas_p2p_set_group_idle_timeout(wpa_s);
4679}
4680
4681
4682static void wpas_p2p_cross_connect_setup(struct wpa_supplicant *wpa_s)
4683{
4684 struct wpa_supplicant *iface;
4685
4686 if (!wpa_s->global->cross_connection)
4687 return;
4688
4689 for (iface = wpa_s->global->ifaces; iface; iface = iface->next) {
4690 if (iface == wpa_s)
4691 continue;
4692 if (iface->drv_flags &
4693 WPA_DRIVER_FLAGS_P2P_DEDICATED_INTERFACE)
4694 continue;
4695 if (iface->drv_flags & WPA_DRIVER_FLAGS_P2P_CAPABLE)
4696 continue;
4697
4698 wpa_s->cross_connect_enabled = 1;
4699 os_strlcpy(wpa_s->cross_connect_uplink, iface->ifname,
4700 sizeof(wpa_s->cross_connect_uplink));
4701 wpa_printf(MSG_DEBUG, "P2P: Enable cross connection from "
4702 "%s to %s whenever uplink is available",
4703 wpa_s->ifname, wpa_s->cross_connect_uplink);
4704
4705 if (iface->ap_iface || iface->current_ssid == NULL ||
4706 iface->current_ssid->mode != WPAS_MODE_INFRA ||
4707 iface->cross_connect_disallowed ||
4708 iface->wpa_state != WPA_COMPLETED)
4709 break;
4710
4711 wpa_s->cross_connect_in_use = 1;
4712 wpa_msg(wpa_s->parent, MSG_INFO,
4713 P2P_EVENT_CROSS_CONNECT_ENABLE "%s %s",
4714 wpa_s->ifname, wpa_s->cross_connect_uplink);
4715 break;
4716 }
4717}
4718
4719
4720int wpas_p2p_notif_pbc_overlap(struct wpa_supplicant *wpa_s)
4721{
4722 if (wpa_s->p2p_group_interface != P2P_GROUP_INTERFACE_CLIENT &&
4723 !wpa_s->p2p_in_provisioning)
4724 return 0; /* not P2P client operation */
4725
4726 wpa_printf(MSG_DEBUG, "P2P: Terminate connection due to WPS PBC "
4727 "session overlap");
4728 if (wpa_s != wpa_s->parent)
4729 wpa_msg_ctrl(wpa_s->parent, MSG_INFO, WPS_EVENT_OVERLAP);
4730
4731 if (wpa_s->global->p2p)
4732 p2p_group_formation_failed(wpa_s->global->p2p);
4733
4734 eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
4735 wpa_s->parent, NULL);
4736
4737 wpas_group_formation_completed(wpa_s, 0);
4738 return 1;
4739}
4740
4741
4742void wpas_p2p_update_channel_list(struct wpa_supplicant *wpa_s)
4743{
4744 struct p2p_channels chan;
4745
4746 if (wpa_s->global == NULL || wpa_s->global->p2p == NULL)
4747 return;
4748
4749 os_memset(&chan, 0, sizeof(chan));
4750 if (wpas_p2p_setup_channels(wpa_s, &chan)) {
4751 wpa_printf(MSG_ERROR, "P2P: Failed to update supported "
4752 "channel list");
4753 return;
4754 }
4755
4756 p2p_update_channel_list(wpa_s->global->p2p, &chan);
4757}
4758
4759
4760int wpas_p2p_cancel(struct wpa_supplicant *wpa_s)
4761{
4762 struct wpa_global *global = wpa_s->global;
4763 int found = 0;
4764 const u8 *peer;
4765
4766 if (global->p2p == NULL)
4767 return -1;
4768
4769 wpa_printf(MSG_DEBUG, "P2P: Request to cancel group formation");
4770
4771 if (wpa_s->pending_interface_name[0] &&
4772 !is_zero_ether_addr(wpa_s->pending_interface_addr))
4773 found = 1;
4774
4775 peer = p2p_get_go_neg_peer(global->p2p);
4776 if (peer) {
4777 wpa_printf(MSG_DEBUG, "P2P: Unauthorize pending GO Neg peer "
4778 MACSTR, MAC2STR(peer));
4779 p2p_unauthorize(global->p2p, peer);
Dmitry Shmidt04949592012-07-19 12:16:46 -07004780 found = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004781 }
4782
4783 wpas_p2p_stop_find(wpa_s);
4784
4785 for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
4786 if (wpa_s == global->p2p_group_formation &&
4787 (wpa_s->p2p_in_provisioning ||
4788 wpa_s->parent->pending_interface_type ==
4789 WPA_IF_P2P_CLIENT)) {
4790 wpa_printf(MSG_DEBUG, "P2P: Interface %s in group "
4791 "formation found - cancelling",
4792 wpa_s->ifname);
4793 found = 1;
4794 eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
4795 wpa_s->parent, NULL);
Dmitry Shmidt04949592012-07-19 12:16:46 -07004796 wpas_p2p_group_delete(wpa_s, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004797 break;
4798 }
4799 }
4800
4801 if (!found) {
4802 wpa_printf(MSG_DEBUG, "P2P: No ongoing group formation found");
4803 return -1;
4804 }
4805
4806 return 0;
4807}
4808
4809
4810void wpas_p2p_interface_unavailable(struct wpa_supplicant *wpa_s)
4811{
4812 if (wpa_s->current_ssid == NULL || !wpa_s->current_ssid->p2p_group)
4813 return;
4814
4815 wpa_printf(MSG_DEBUG, "P2P: Remove group due to driver resource not "
4816 "being available anymore");
4817 wpa_s->removal_reason = P2P_GROUP_REMOVAL_UNAVAILABLE;
Dmitry Shmidt04949592012-07-19 12:16:46 -07004818 wpas_p2p_group_delete(wpa_s, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004819}
4820
4821
4822void wpas_p2p_update_best_channels(struct wpa_supplicant *wpa_s,
4823 int freq_24, int freq_5, int freq_overall)
4824{
4825 struct p2p_data *p2p = wpa_s->global->p2p;
4826 if (p2p == NULL || (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT))
4827 return;
4828 p2p_set_best_channels(p2p, freq_24, freq_5, freq_overall);
4829}
4830
4831
4832int wpas_p2p_unauthorize(struct wpa_supplicant *wpa_s, const char *addr)
4833{
4834 u8 peer[ETH_ALEN];
4835 struct p2p_data *p2p = wpa_s->global->p2p;
4836
4837 if (p2p == NULL || (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT))
4838 return -1;
4839
4840 if (hwaddr_aton(addr, peer))
4841 return -1;
4842
4843 return p2p_unauthorize(p2p, peer);
4844}
4845
4846
4847/**
4848 * wpas_p2p_disconnect - Disconnect from a P2P Group
4849 * @wpa_s: Pointer to wpa_supplicant data
4850 * Returns: 0 on success, -1 on failure
4851 *
4852 * This can be used to disconnect from a group in which the local end is a P2P
4853 * Client or to end a P2P Group in case the local end is the Group Owner. If a
4854 * virtual network interface was created for this group, that interface will be
4855 * removed. Otherwise, only the configured P2P group network will be removed
4856 * from the interface.
4857 */
4858int wpas_p2p_disconnect(struct wpa_supplicant *wpa_s)
4859{
4860
4861 if (wpa_s == NULL)
4862 return -1;
4863
4864 wpa_s->removal_reason = P2P_GROUP_REMOVAL_REQUESTED;
Dmitry Shmidt04949592012-07-19 12:16:46 -07004865 wpas_p2p_group_delete(wpa_s, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004866
4867 return 0;
4868}
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004869
4870
4871int wpas_p2p_in_progress(struct wpa_supplicant *wpa_s)
4872{
4873 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
4874 return 0;
4875
4876 return p2p_in_progress(wpa_s->global->p2p);
4877}
4878
4879
4880void wpas_p2p_network_removed(struct wpa_supplicant *wpa_s,
4881 struct wpa_ssid *ssid)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004882{
4883 if (wpa_s->p2p_in_provisioning && ssid->p2p_group &&
4884 eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
4885 wpa_s->parent, NULL) > 0) {
Dmitry Shmidt04949592012-07-19 12:16:46 -07004886 /**
4887 * Remove the network by scheduling the group formation
4888 * timeout to happen immediately. The teardown code
4889 * needs to be scheduled to run asynch later so that we
4890 * don't delete data from under ourselves unexpectedly.
4891 * Calling wpas_p2p_group_formation_timeout directly
4892 * causes a series of crashes in WPS failure scenarios.
4893 */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004894 wpa_printf(MSG_DEBUG, "P2P: Canceled group formation due to "
4895 "P2P group network getting removed");
Dmitry Shmidt04949592012-07-19 12:16:46 -07004896 eloop_register_timeout(0, 0, wpas_p2p_group_formation_timeout,
4897 wpa_s->parent, NULL);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004898 }
4899}
4900
4901
4902struct wpa_ssid * wpas_p2p_get_persistent(struct wpa_supplicant *wpa_s,
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08004903 const u8 *addr, const u8 *ssid,
4904 size_t ssid_len)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004905{
4906 struct wpa_ssid *s;
4907 size_t i;
4908
4909 for (s = wpa_s->conf->ssid; s; s = s->next) {
4910 if (s->disabled != 2)
4911 continue;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08004912 if (ssid &&
4913 (ssid_len != s->ssid_len ||
4914 os_memcmp(ssid, s->ssid, ssid_len) != 0))
4915 continue;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004916 if (os_memcmp(s->bssid, addr, ETH_ALEN) == 0)
4917 return s; /* peer is GO in the persistent group */
4918 if (s->mode != WPAS_MODE_P2P_GO || s->p2p_client_list == NULL)
4919 continue;
4920 for (i = 0; i < s->num_p2p_clients; i++) {
4921 if (os_memcmp(s->p2p_client_list + i * ETH_ALEN,
4922 addr, ETH_ALEN) == 0)
4923 return s; /* peer is P2P client in persistent
4924 * group */
4925 }
4926 }
4927
4928 return NULL;
4929}
4930
4931
4932void wpas_p2p_notify_ap_sta_authorized(struct wpa_supplicant *wpa_s,
4933 const u8 *addr)
4934{
4935 if (addr == NULL)
4936 return;
4937 wpas_p2p_add_persistent_group_client(wpa_s, addr);
4938}
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08004939
Dmitry Shmidt04949592012-07-19 12:16:46 -07004940
4941static void wpas_p2p_fallback_to_go_neg(struct wpa_supplicant *wpa_s,
4942 int group_added)
4943{
4944 struct wpa_supplicant *group = wpa_s;
4945 eloop_cancel_timeout(wpas_p2p_pd_before_join_timeout, wpa_s, NULL);
4946 if (wpa_s->global->p2p_group_formation)
4947 group = wpa_s->global->p2p_group_formation;
4948 wpa_s = wpa_s->parent;
4949 offchannel_send_action_done(wpa_s);
4950 if (group_added)
4951 wpas_p2p_group_delete(group, 1);
4952 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Fall back to GO Negotiation");
4953 wpas_p2p_connect(wpa_s, wpa_s->pending_join_dev_addr, wpa_s->p2p_pin,
4954 wpa_s->p2p_wps_method, wpa_s->p2p_persistent_group, 0,
4955 0, 0, wpa_s->p2p_go_intent, wpa_s->p2p_connect_freq,
4956 wpa_s->p2p_persistent_id,
4957 wpa_s->p2p_pd_before_go_neg);
4958}
4959
4960
4961int wpas_p2p_scan_no_go_seen(struct wpa_supplicant *wpa_s)
4962{
4963 if (!wpa_s->p2p_fallback_to_go_neg ||
4964 wpa_s->p2p_in_provisioning <= 5)
4965 return 0;
4966
4967 if (wpas_p2p_peer_go(wpa_s, wpa_s->pending_join_dev_addr) > 0)
4968 return 0; /* peer operating as a GO */
4969
4970 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: GO not found for p2p_connect-auto - "
4971 "fallback to GO Negotiation");
4972 wpas_p2p_fallback_to_go_neg(wpa_s, 1);
4973
4974 return 1;
4975}
4976
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08004977#ifdef ANDROID_P2P
4978int wpas_p2p_handle_frequency_conflicts(struct wpa_supplicant *wpa_s, int freq)
4979{
4980 struct wpa_supplicant *iface = NULL;
4981 struct p2p_data *p2p = wpa_s->global->p2p;
4982
4983 for (iface = wpa_s->global->ifaces; iface; iface = iface->next) {
4984 if((iface->p2p_group_interface) && (iface->current_ssid) &&
4985 (iface->current_ssid->frequency != freq)) {
4986
4987 if (iface->p2p_group_interface == P2P_GROUP_INTERFACE_GO) {
4988 /* Try to see whether we can move the GO. If it
4989 * is not possible, remove the GO interface
4990 */
4991 if(wpa_drv_switch_channel(iface, freq) == 0) {
4992 wpa_printf(MSG_ERROR, "P2P: GO Moved to freq(%d)", freq);
4993 iface->current_ssid->frequency = freq;
4994 continue;
4995 }
4996 }
4997
4998 /* If GO cannot be moved or if the conflicting interface is a
4999 * P2P Client, remove the interface depending up on the connection
5000 * priority */
Dmitry Shmidt687922c2012-03-26 14:02:32 -07005001 if(!wpas_is_p2p_prioritized(wpa_s)) {
5002 /* STA connection has priority over existing
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08005003 * P2P connection. So remove the interface */
Dmitry Shmidt687922c2012-03-26 14:02:32 -07005004 wpa_printf(MSG_DEBUG, "P2P: Removing P2P connection due to Single channel"
5005 "concurrent mode frequency conflict");
5006 iface->removal_reason = P2P_GROUP_REMOVAL_FREQ_CONFLICT;
Dmitry Shmidt04949592012-07-19 12:16:46 -07005007 wpas_p2p_group_delete(iface, 0);
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08005008 } else {
5009 /* Existing connection has the priority. Disable the newly
5010 * selected network and let the application know about it.
5011 */
5012 return -1;
5013 }
5014 }
5015 }
5016 return 0;
5017}
5018#endif