blob: c43469e291ca5eee1bb3fb6b4006d00e27a44b58 [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 Shmidt61d9df32012-08-29 16:22:06 -070064#ifndef P2P_CONCURRENT_SEARCH_DELAY
65#ifdef ANDROID_P2P
66#define P2P_CONCURRENT_SEARCH_DELAY 0
67#else
68#define P2P_CONCURRENT_SEARCH_DELAY 500
69#endif
70#endif /* P2P_CONCURRENT_SEARCH_DELAY */
71
72enum p2p_group_removal_reason {
73 P2P_GROUP_REMOVAL_UNKNOWN,
74 P2P_GROUP_REMOVAL_SILENT,
75 P2P_GROUP_REMOVAL_FORMATION_FAILED,
76 P2P_GROUP_REMOVAL_REQUESTED,
77 P2P_GROUP_REMOVAL_IDLE_TIMEOUT,
78 P2P_GROUP_REMOVAL_UNAVAILABLE,
79 P2P_GROUP_REMOVAL_GO_ENDING_SESSION,
80#ifdef ANDROID_P2P
81 P2P_GROUP_REMOVAL_FREQ_CONFLICT
82#endif
83};
84
Dmitry Shmidt98f9e762012-05-30 11:18:46 -070085#ifdef ANDROID_P2P
86static int wpas_global_scan_in_progress(struct wpa_supplicant *wpa_s);
87#endif
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070088static void wpas_p2p_long_listen_timeout(void *eloop_ctx, void *timeout_ctx);
89static struct wpa_supplicant *
90wpas_p2p_get_group_iface(struct wpa_supplicant *wpa_s, int addr_allocated,
91 int go);
92static int wpas_p2p_join_start(struct wpa_supplicant *wpa_s);
Dmitry Shmidt04949592012-07-19 12:16:46 -070093static void wpas_p2p_join_scan_req(struct wpa_supplicant *wpa_s, int freq);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070094static void wpas_p2p_join_scan(void *eloop_ctx, void *timeout_ctx);
95static int wpas_p2p_join(struct wpa_supplicant *wpa_s, const u8 *iface_addr,
Dmitry Shmidt04949592012-07-19 12:16:46 -070096 const u8 *dev_addr, enum p2p_wps_method wps_method,
97 int auto_join);
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -080098static void wpas_p2p_pd_before_join_timeout(void *eloop_ctx,
99 void *timeout_ctx);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700100static int wpas_p2p_create_iface(struct wpa_supplicant *wpa_s);
101static void wpas_p2p_cross_connect_setup(struct wpa_supplicant *wpa_s);
102static void wpas_p2p_group_idle_timeout(void *eloop_ctx, void *timeout_ctx);
103static void wpas_p2p_set_group_idle_timeout(struct wpa_supplicant *wpa_s);
Dmitry Shmidt04949592012-07-19 12:16:46 -0700104static void wpas_p2p_fallback_to_go_neg(struct wpa_supplicant *wpa_s,
105 int group_added);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700106
Dmitry Shmidt2fb777c2012-05-02 12:29:53 -0700107#ifdef ANDROID_P2P
108static int wpas_global_scan_in_progress(struct wpa_supplicant *wpa_s)
109{
110 struct wpa_supplicant *iface = NULL;
111
112 for (iface = wpa_s->global->ifaces; iface; iface = iface->next) {
113 if(iface->scanning || iface->wpa_state == WPA_SCANNING) {
114 wpa_printf(MSG_DEBUG, "P2P: Scan in progress on %s,"
115 "defer P2P SEARCH", iface->ifname);
116 return 1;
117 }
118 }
119
120 return 0;
121}
122#endif
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700123
124static void wpas_p2p_scan_res_handler(struct wpa_supplicant *wpa_s,
125 struct wpa_scan_results *scan_res)
126{
127 size_t i;
128
129 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
130 return;
131
132 wpa_printf(MSG_DEBUG, "P2P: Scan results received (%d BSS)",
133 (int) scan_res->num);
134
135 for (i = 0; i < scan_res->num; i++) {
136 struct wpa_scan_res *bss = scan_res->res[i];
137 if (p2p_scan_res_handler(wpa_s->global->p2p, bss->bssid,
138 bss->freq, bss->level,
139 (const u8 *) (bss + 1),
140 bss->ie_len) > 0)
141 break;
142 }
143
144 p2p_scan_res_handled(wpa_s->global->p2p);
145}
146
147
148static int wpas_p2p_scan(void *ctx, enum p2p_scan_type type, int freq,
149 unsigned int num_req_dev_types,
Dmitry Shmidt04949592012-07-19 12:16:46 -0700150 const u8 *req_dev_types, const u8 *dev_id, u16 pw_id)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700151{
152 struct wpa_supplicant *wpa_s = ctx;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700153 struct wpa_supplicant *ifs;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700154 struct wpa_driver_scan_params params;
155 int ret;
156 struct wpabuf *wps_ie, *ies;
157 int social_channels[] = { 2412, 2437, 2462, 0, 0 };
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800158 size_t ielen;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700159
160 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
161 return -1;
162
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700163 for (ifs = wpa_s->global->ifaces; ifs; ifs = ifs->next) {
164 if (ifs->sta_scan_pending &&
165 wpas_p2p_in_progress(wpa_s) == 2) {
166 wpa_printf(MSG_DEBUG, "Delaying P2P scan to allow "
167 "pending station mode scan to be "
168 "completed on interface %s", ifs->ifname);
169 wpa_s->p2p_cb_on_scan_complete = 1;
170 wpa_supplicant_req_scan(ifs, 0, 0);
171 return 1;
172 }
173 }
174
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700175 os_memset(&params, 0, sizeof(params));
176
177 /* P2P Wildcard SSID */
178 params.num_ssids = 1;
179 params.ssids[0].ssid = (u8 *) P2P_WILDCARD_SSID;
180 params.ssids[0].ssid_len = P2P_WILDCARD_SSID_LEN;
181
182 wpa_s->wps->dev.p2p = 1;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700183 wps_ie = wps_build_probe_req_ie(pw_id, &wpa_s->wps->dev,
184 wpa_s->wps->uuid, WPS_REQ_ENROLLEE,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700185 num_req_dev_types, req_dev_types);
186 if (wps_ie == NULL)
187 return -1;
188
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800189 ielen = p2p_scan_ie_buf_len(wpa_s->global->p2p);
190 ies = wpabuf_alloc(wpabuf_len(wps_ie) + ielen);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700191 if (ies == NULL) {
192 wpabuf_free(wps_ie);
193 return -1;
194 }
195 wpabuf_put_buf(ies, wps_ie);
196 wpabuf_free(wps_ie);
197
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -0800198 p2p_scan_ie(wpa_s->global->p2p, ies, dev_id);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700199
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800200 params.p2p_probe = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700201 params.extra_ies = wpabuf_head(ies);
202 params.extra_ies_len = wpabuf_len(ies);
203
204 switch (type) {
205 case P2P_SCAN_SOCIAL:
206 params.freqs = social_channels;
207 break;
208 case P2P_SCAN_FULL:
209 break;
Dmitry Shmidtb5e8f062012-08-08 10:56:33 -0700210 case P2P_SCAN_SPECIFIC:
211 social_channels[0] = freq;
212 social_channels[1] = 0;
213 params.freqs = social_channels;
214 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700215 case P2P_SCAN_SOCIAL_PLUS_ONE:
216 social_channels[3] = freq;
217 params.freqs = social_channels;
218 break;
219 }
220
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800221 ret = wpa_drv_scan(wpa_s, &params);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700222
223 wpabuf_free(ies);
224
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800225 if (ret) {
Dmitry Shmidt04949592012-07-19 12:16:46 -0700226 if (wpa_s->scanning ||
227 wpa_s->scan_res_handler == wpas_p2p_scan_res_handler) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800228 wpa_s->p2p_cb_on_scan_complete = 1;
229 ret = 1;
230 }
Dmitry Shmidt04949592012-07-19 12:16:46 -0700231 } else
232 wpa_s->scan_res_handler = wpas_p2p_scan_res_handler;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800233
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700234 return ret;
235}
236
237
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700238static enum wpa_driver_if_type wpas_p2p_if_type(int p2p_group_interface)
239{
240 switch (p2p_group_interface) {
241 case P2P_GROUP_INTERFACE_PENDING:
242 return WPA_IF_P2P_GROUP;
243 case P2P_GROUP_INTERFACE_GO:
244 return WPA_IF_P2P_GO;
245 case P2P_GROUP_INTERFACE_CLIENT:
246 return WPA_IF_P2P_CLIENT;
247 }
248
249 return WPA_IF_P2P_GROUP;
250}
251
252
253static struct wpa_supplicant * wpas_get_p2p_group(struct wpa_supplicant *wpa_s,
254 const u8 *ssid,
255 size_t ssid_len, int *go)
256{
257 struct wpa_ssid *s;
258
259 for (wpa_s = wpa_s->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
260 for (s = wpa_s->conf->ssid; s; s = s->next) {
261 if (s->disabled != 0 || !s->p2p_group ||
262 s->ssid_len != ssid_len ||
263 os_memcmp(ssid, s->ssid, ssid_len) != 0)
264 continue;
265 if (s->mode == WPAS_MODE_P2P_GO &&
266 s != wpa_s->current_ssid)
267 continue;
268 if (go)
269 *go = s->mode == WPAS_MODE_P2P_GO;
270 return wpa_s;
271 }
272 }
273
274 return NULL;
275}
276
277
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700278static int wpas_p2p_group_delete(struct wpa_supplicant *wpa_s,
279 enum p2p_group_removal_reason removal_reason)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700280{
281 struct wpa_ssid *ssid;
282 char *gtype;
283 const char *reason;
284
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700285 ssid = wpa_s->current_ssid;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700286#ifdef ANDROID_P2P
287 if ((ssid == NULL) && (wpa_s->p2p_group_interface == NOT_P2P_GROUP_INTERFACE)) {
288#else
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700289 if (ssid == NULL) {
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700290#endif
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700291 /*
292 * The current SSID was not known, but there may still be a
293 * pending P2P group interface waiting for provisioning.
294 */
295 ssid = wpa_s->conf->ssid;
296 while (ssid) {
297 if (ssid->p2p_group &&
298 (ssid->mode == WPAS_MODE_P2P_GROUP_FORMATION ||
299 (ssid->key_mgmt & WPA_KEY_MGMT_WPS)))
300 break;
301 ssid = ssid->next;
302 }
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700303 if (ssid == NULL) {
304 wpa_printf(MSG_ERROR, "P2P: P2P group interface "
305 "not found");
306 return -1;
307 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700308 }
309 if (wpa_s->p2p_group_interface == P2P_GROUP_INTERFACE_GO)
310 gtype = "GO";
311 else if (wpa_s->p2p_group_interface == P2P_GROUP_INTERFACE_CLIENT ||
312 (ssid && ssid->mode == WPAS_MODE_INFRA)) {
313 wpa_s->reassociate = 0;
314 wpa_s->disconnected = 1;
315 wpa_supplicant_deauthenticate(wpa_s,
316 WLAN_REASON_DEAUTH_LEAVING);
317 gtype = "client";
318 } else
319 gtype = "GO";
320 if (wpa_s->cross_connect_in_use) {
321 wpa_s->cross_connect_in_use = 0;
322 wpa_msg(wpa_s->parent, MSG_INFO,
323 P2P_EVENT_CROSS_CONNECT_DISABLE "%s %s",
324 wpa_s->ifname, wpa_s->cross_connect_uplink);
325 }
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700326 switch (removal_reason) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700327 case P2P_GROUP_REMOVAL_REQUESTED:
328 reason = " reason=REQUESTED";
329 break;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700330 case P2P_GROUP_REMOVAL_FORMATION_FAILED:
331 reason = " reason=FORMATION_FAILED";
332 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700333 case P2P_GROUP_REMOVAL_IDLE_TIMEOUT:
334 reason = " reason=IDLE";
335 break;
336 case P2P_GROUP_REMOVAL_UNAVAILABLE:
337 reason = " reason=UNAVAILABLE";
338 break;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700339 case P2P_GROUP_REMOVAL_GO_ENDING_SESSION:
340 reason = " reason=GO_ENDING_SESSION";
341 break;
Dmitry Shmidt687922c2012-03-26 14:02:32 -0700342#ifdef ANDROID_P2P
343 case P2P_GROUP_REMOVAL_FREQ_CONFLICT:
344 reason = " reason=FREQ_CONFLICT";
345 break;
346#endif
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700347 default:
348 reason = "";
349 break;
350 }
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700351 if (removal_reason != P2P_GROUP_REMOVAL_SILENT) {
Dmitry Shmidt04949592012-07-19 12:16:46 -0700352 wpa_msg(wpa_s->parent, MSG_INFO,
353 P2P_EVENT_GROUP_REMOVED "%s %s%s",
354 wpa_s->ifname, gtype, reason);
355 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700356
Dmitry Shmidt04949592012-07-19 12:16:46 -0700357 if (eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL) > 0)
358 wpa_printf(MSG_DEBUG, "P2P: Cancelled P2P group idle timeout");
359
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700360 if (removal_reason != P2P_GROUP_REMOVAL_SILENT && ssid)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700361 wpas_notify_p2p_group_removed(wpa_s, ssid, gtype);
362
363 if (wpa_s->p2p_group_interface != NOT_P2P_GROUP_INTERFACE) {
364 struct wpa_global *global;
365 char *ifname;
366 enum wpa_driver_if_type type;
367 wpa_printf(MSG_DEBUG, "P2P: Remove group interface %s",
368 wpa_s->ifname);
369 global = wpa_s->global;
370 ifname = os_strdup(wpa_s->ifname);
371 type = wpas_p2p_if_type(wpa_s->p2p_group_interface);
Dmitry Shmidte15c7b52011-08-03 15:04:35 -0700372 wpa_supplicant_remove_iface(wpa_s->global, wpa_s, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700373 wpa_s = global->ifaces;
374 if (wpa_s && ifname)
375 wpa_drv_if_remove(wpa_s, type, ifname);
376 os_free(ifname);
Jouni Malinen2b89da82012-08-31 22:04:41 +0300377 return 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700378 }
379
380 wpa_printf(MSG_DEBUG, "P2P: Remove temporary group network");
381 if (ssid && (ssid->p2p_group ||
382 ssid->mode == WPAS_MODE_P2P_GROUP_FORMATION ||
383 (ssid->key_mgmt & WPA_KEY_MGMT_WPS))) {
384 int id = ssid->id;
Jouni Malinen75ecf522011-06-27 15:19:46 -0700385 if (ssid == wpa_s->current_ssid) {
386 wpa_sm_set_config(wpa_s->wpa, NULL);
387 eapol_sm_notify_config(wpa_s->eapol, NULL, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700388 wpa_s->current_ssid = NULL;
Jouni Malinen75ecf522011-06-27 15:19:46 -0700389 }
390 /*
391 * Networks objects created during any P2P activities are not
392 * exposed out as they might/will confuse certain non-P2P aware
393 * applications since these network objects won't behave like
394 * regular ones.
395 *
396 * Likewise, we don't send out network removed signals for such
397 * network objects.
398 */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700399 wpa_config_remove_network(wpa_s->conf, id);
400 wpa_supplicant_clear_status(wpa_s);
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -0800401 wpa_supplicant_cancel_sched_scan(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700402 } else {
403 wpa_printf(MSG_DEBUG, "P2P: Temporary group network not "
404 "found");
405 }
Dmitry Shmidt04949592012-07-19 12:16:46 -0700406 if (wpa_s->ap_iface)
407 wpa_supplicant_ap_deinit(wpa_s);
408 else
409 wpa_drv_deinit_p2p_cli(wpa_s);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700410
411 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700412}
413
414
415static int wpas_p2p_persistent_group(struct wpa_supplicant *wpa_s,
416 u8 *go_dev_addr,
417 const u8 *ssid, size_t ssid_len)
418{
419 struct wpa_bss *bss;
420 const u8 *bssid;
421 struct wpabuf *p2p;
422 u8 group_capab;
423 const u8 *addr;
424
425 if (wpa_s->go_params)
426 bssid = wpa_s->go_params->peer_interface_addr;
427 else
428 bssid = wpa_s->bssid;
429
430 bss = wpa_bss_get(wpa_s, bssid, ssid, ssid_len);
431 if (bss == NULL) {
432 u8 iface_addr[ETH_ALEN];
433 if (p2p_get_interface_addr(wpa_s->global->p2p, bssid,
434 iface_addr) == 0)
435 bss = wpa_bss_get(wpa_s, iface_addr, ssid, ssid_len);
436 }
437 if (bss == NULL) {
438 wpa_printf(MSG_DEBUG, "P2P: Could not figure out whether "
439 "group is persistent - BSS " MACSTR " not found",
440 MAC2STR(bssid));
441 return 0;
442 }
443
444 p2p = wpa_bss_get_vendor_ie_multi(bss, P2P_IE_VENDOR_TYPE);
445 if (p2p == NULL) {
446 wpa_printf(MSG_DEBUG, "P2P: Could not figure out whether "
447 "group is persistent - BSS " MACSTR
448 " did not include P2P IE", MAC2STR(bssid));
449 wpa_hexdump(MSG_DEBUG, "P2P: Probe Response IEs",
450 (u8 *) (bss + 1), bss->ie_len);
451 wpa_hexdump(MSG_DEBUG, "P2P: Beacon IEs",
452 ((u8 *) bss + 1) + bss->ie_len,
453 bss->beacon_ie_len);
454 return 0;
455 }
456
457 group_capab = p2p_get_group_capab(p2p);
458 addr = p2p_get_go_dev_addr(p2p);
459 wpa_printf(MSG_DEBUG, "P2P: Checking whether group is persistent: "
460 "group_capab=0x%x", group_capab);
461 if (addr) {
462 os_memcpy(go_dev_addr, addr, ETH_ALEN);
463 wpa_printf(MSG_DEBUG, "P2P: GO Device Address " MACSTR,
464 MAC2STR(addr));
465 } else
466 os_memset(go_dev_addr, 0, ETH_ALEN);
467 wpabuf_free(p2p);
468
469 wpa_printf(MSG_DEBUG, "P2P: BSS " MACSTR " group_capab=0x%x "
470 "go_dev_addr=" MACSTR,
471 MAC2STR(bssid), group_capab, MAC2STR(go_dev_addr));
472
473 return group_capab & P2P_GROUP_CAPAB_PERSISTENT_GROUP;
474}
475
476
Jouni Malinen75ecf522011-06-27 15:19:46 -0700477static int wpas_p2p_store_persistent_group(struct wpa_supplicant *wpa_s,
478 struct wpa_ssid *ssid,
479 const u8 *go_dev_addr)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700480{
481 struct wpa_ssid *s;
482 int changed = 0;
483
484 wpa_printf(MSG_DEBUG, "P2P: Storing credentials for a persistent "
485 "group (GO Dev Addr " MACSTR ")", MAC2STR(go_dev_addr));
486 for (s = wpa_s->conf->ssid; s; s = s->next) {
487 if (s->disabled == 2 &&
488 os_memcmp(go_dev_addr, s->bssid, ETH_ALEN) == 0 &&
489 s->ssid_len == ssid->ssid_len &&
490 os_memcmp(ssid->ssid, s->ssid, ssid->ssid_len) == 0)
491 break;
492 }
493
494 if (s) {
495 wpa_printf(MSG_DEBUG, "P2P: Update existing persistent group "
496 "entry");
497 if (ssid->passphrase && !s->passphrase)
498 changed = 1;
499 else if (ssid->passphrase && s->passphrase &&
500 os_strcmp(ssid->passphrase, s->passphrase) != 0)
501 changed = 1;
502 } else {
503 wpa_printf(MSG_DEBUG, "P2P: Create a new persistent group "
504 "entry");
505 changed = 1;
506 s = wpa_config_add_network(wpa_s->conf);
507 if (s == NULL)
Jouni Malinen75ecf522011-06-27 15:19:46 -0700508 return -1;
509
510 /*
511 * Instead of network_added we emit persistent_group_added
512 * notification. Also to keep the defense checks in
513 * persistent_group obj registration method, we set the
514 * relevant flags in s to designate it as a persistent group.
515 */
516 s->p2p_group = 1;
517 s->p2p_persistent_group = 1;
518 wpas_notify_persistent_group_added(wpa_s, s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700519 wpa_config_set_network_defaults(s);
520 }
521
522 s->p2p_group = 1;
523 s->p2p_persistent_group = 1;
524 s->disabled = 2;
525 s->bssid_set = 1;
526 os_memcpy(s->bssid, go_dev_addr, ETH_ALEN);
527 s->mode = ssid->mode;
528 s->auth_alg = WPA_AUTH_ALG_OPEN;
529 s->key_mgmt = WPA_KEY_MGMT_PSK;
530 s->proto = WPA_PROTO_RSN;
531 s->pairwise_cipher = WPA_CIPHER_CCMP;
532 s->export_keys = 1;
533 if (ssid->passphrase) {
534 os_free(s->passphrase);
535 s->passphrase = os_strdup(ssid->passphrase);
536 }
537 if (ssid->psk_set) {
538 s->psk_set = 1;
539 os_memcpy(s->psk, ssid->psk, 32);
540 }
541 if (s->passphrase && !s->psk_set)
542 wpa_config_update_psk(s);
543 if (s->ssid == NULL || s->ssid_len < ssid->ssid_len) {
544 os_free(s->ssid);
545 s->ssid = os_malloc(ssid->ssid_len);
546 }
547 if (s->ssid) {
548 s->ssid_len = ssid->ssid_len;
549 os_memcpy(s->ssid, ssid->ssid, s->ssid_len);
550 }
551
552#ifndef CONFIG_NO_CONFIG_WRITE
553 if (changed && wpa_s->conf->update_config &&
554 wpa_config_write(wpa_s->confname, wpa_s->conf)) {
555 wpa_printf(MSG_DEBUG, "P2P: Failed to update configuration");
556 }
557#endif /* CONFIG_NO_CONFIG_WRITE */
Jouni Malinen75ecf522011-06-27 15:19:46 -0700558
559 return s->id;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700560}
561
562
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800563static void wpas_p2p_add_persistent_group_client(struct wpa_supplicant *wpa_s,
564 const u8 *addr)
565{
566 struct wpa_ssid *ssid, *s;
567 u8 *n;
568 size_t i;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700569 int found = 0;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800570
571 ssid = wpa_s->current_ssid;
572 if (ssid == NULL || ssid->mode != WPAS_MODE_P2P_GO ||
573 !ssid->p2p_persistent_group)
574 return;
575
576 for (s = wpa_s->parent->conf->ssid; s; s = s->next) {
577 if (s->disabled != 2 || s->mode != WPAS_MODE_P2P_GO)
578 continue;
579
580 if (s->ssid_len == ssid->ssid_len &&
581 os_memcmp(s->ssid, ssid->ssid, s->ssid_len) == 0)
582 break;
583 }
584
585 if (s == NULL)
586 return;
587
588 for (i = 0; s->p2p_client_list && i < s->num_p2p_clients; i++) {
589 if (os_memcmp(s->p2p_client_list + i * ETH_ALEN, addr,
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700590 ETH_ALEN) != 0)
591 continue;
592
593 if (i == s->num_p2p_clients - 1)
594 return; /* already the most recent entry */
595
596 /* move the entry to mark it most recent */
597 os_memmove(s->p2p_client_list + i * ETH_ALEN,
598 s->p2p_client_list + (i + 1) * ETH_ALEN,
599 (s->num_p2p_clients - i - 1) * ETH_ALEN);
600 os_memcpy(s->p2p_client_list +
601 (s->num_p2p_clients - 1) * ETH_ALEN, addr, ETH_ALEN);
602 found = 1;
603 break;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800604 }
605
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700606 if (!found && s->num_p2p_clients < P2P_MAX_STORED_CLIENTS) {
607 n = os_realloc_array(s->p2p_client_list,
608 s->num_p2p_clients + 1, ETH_ALEN);
609 if (n == NULL)
610 return;
611 os_memcpy(n + s->num_p2p_clients * ETH_ALEN, addr, ETH_ALEN);
612 s->p2p_client_list = n;
613 s->num_p2p_clients++;
614 } else if (!found) {
615 /* Not enough room for an additional entry - drop the oldest
616 * entry */
617 os_memmove(s->p2p_client_list,
618 s->p2p_client_list + ETH_ALEN,
619 (s->num_p2p_clients - 1) * ETH_ALEN);
620 os_memcpy(s->p2p_client_list +
621 (s->num_p2p_clients - 1) * ETH_ALEN,
622 addr, ETH_ALEN);
623 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800624
625#ifndef CONFIG_NO_CONFIG_WRITE
626 if (wpa_s->parent->conf->update_config &&
627 wpa_config_write(wpa_s->parent->confname, wpa_s->parent->conf))
628 wpa_printf(MSG_DEBUG, "P2P: Failed to update configuration");
629#endif /* CONFIG_NO_CONFIG_WRITE */
630}
631
632
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700633static void wpas_group_formation_completed(struct wpa_supplicant *wpa_s,
634 int success)
635{
636 struct wpa_ssid *ssid;
637 const char *ssid_txt;
638 int client;
639 int persistent;
640 u8 go_dev_addr[ETH_ALEN];
Jouni Malinen75ecf522011-06-27 15:19:46 -0700641 int network_id = -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700642
643 /*
644 * This callback is likely called for the main interface. Update wpa_s
645 * to use the group interface if a new interface was created for the
646 * group.
647 */
648 if (wpa_s->global->p2p_group_formation)
649 wpa_s = wpa_s->global->p2p_group_formation;
650 wpa_s->global->p2p_group_formation = NULL;
651 wpa_s->p2p_in_provisioning = 0;
652
653 if (!success) {
654 wpa_msg(wpa_s->parent, MSG_INFO,
655 P2P_EVENT_GROUP_FORMATION_FAILURE);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700656 wpas_p2p_group_delete(wpa_s,
657 P2P_GROUP_REMOVAL_FORMATION_FAILED);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700658 return;
659 }
660
661 wpa_msg(wpa_s->parent, MSG_INFO, P2P_EVENT_GROUP_FORMATION_SUCCESS);
662
663 ssid = wpa_s->current_ssid;
664 if (ssid && ssid->mode == WPAS_MODE_P2P_GROUP_FORMATION) {
665 ssid->mode = WPAS_MODE_P2P_GO;
666 p2p_group_notif_formation_done(wpa_s->p2p_group);
667 wpa_supplicant_ap_mac_addr_filter(wpa_s, NULL);
668 }
669
670 persistent = 0;
671 if (ssid) {
672 ssid_txt = wpa_ssid_txt(ssid->ssid, ssid->ssid_len);
673 client = ssid->mode == WPAS_MODE_INFRA;
674 if (ssid->mode == WPAS_MODE_P2P_GO) {
675 persistent = ssid->p2p_persistent_group;
Dmitry Shmidt497c1d52011-07-21 15:19:46 -0700676 os_memcpy(go_dev_addr, wpa_s->global->p2p_dev_addr,
677 ETH_ALEN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700678 } else
679 persistent = wpas_p2p_persistent_group(wpa_s,
680 go_dev_addr,
681 ssid->ssid,
682 ssid->ssid_len);
683 } else {
684 ssid_txt = "";
685 client = wpa_s->p2p_group_interface ==
686 P2P_GROUP_INTERFACE_CLIENT;
687 os_memset(go_dev_addr, 0, ETH_ALEN);
688 }
689
690 wpa_s->show_group_started = 0;
691 if (client) {
692 /*
693 * Indicate event only after successfully completed 4-way
694 * handshake, i.e., when the interface is ready for data
695 * packets.
696 */
697 wpa_s->show_group_started = 1;
Dmitry Shmidt4b86ea52012-09-04 11:06:50 -0700698#ifdef ANDROID_P2P
699 /* For client Second phase of Group formation (4-way handshake) can be still pending
700 * So we need to restore wpa_s->global->p2p_group_formation */
701 wpa_s->global->p2p_group_formation = wpa_s;
702#endif
703
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700704 } else if (ssid && ssid->passphrase == NULL && ssid->psk_set) {
705 char psk[65];
706 wpa_snprintf_hex(psk, sizeof(psk), ssid->psk, 32);
707 wpa_msg(wpa_s->parent, MSG_INFO, P2P_EVENT_GROUP_STARTED
708 "%s GO ssid=\"%s\" freq=%d psk=%s go_dev_addr=" MACSTR
709 "%s",
710 wpa_s->ifname, ssid_txt, ssid->frequency, psk,
711 MAC2STR(go_dev_addr),
712 persistent ? " [PERSISTENT]" : "");
713 wpas_p2p_cross_connect_setup(wpa_s);
714 wpas_p2p_set_group_idle_timeout(wpa_s);
715 } else {
716 wpa_msg(wpa_s->parent, MSG_INFO, P2P_EVENT_GROUP_STARTED
717 "%s GO ssid=\"%s\" freq=%d passphrase=\"%s\" "
718 "go_dev_addr=" MACSTR "%s",
719 wpa_s->ifname, ssid_txt, ssid ? ssid->frequency : 0,
720 ssid && ssid->passphrase ? ssid->passphrase : "",
721 MAC2STR(go_dev_addr),
722 persistent ? " [PERSISTENT]" : "");
723 wpas_p2p_cross_connect_setup(wpa_s);
724 wpas_p2p_set_group_idle_timeout(wpa_s);
725 }
726
727 if (persistent)
Jouni Malinen75ecf522011-06-27 15:19:46 -0700728 network_id = wpas_p2p_store_persistent_group(wpa_s->parent,
729 ssid, go_dev_addr);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800730 if (network_id < 0 && ssid)
Jouni Malinen75ecf522011-06-27 15:19:46 -0700731 network_id = ssid->id;
732 if (!client)
733 wpas_notify_p2p_group_started(wpa_s, ssid, network_id, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700734}
735
736
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800737static void wpas_p2p_send_action_tx_status(struct wpa_supplicant *wpa_s,
738 unsigned int freq,
739 const u8 *dst, const u8 *src,
740 const u8 *bssid,
741 const u8 *data, size_t data_len,
742 enum offchannel_send_action_result
743 result)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700744{
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800745 enum p2p_send_action_result res = P2P_SEND_ACTION_SUCCESS;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700746
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700747 if (wpa_s->global->p2p == NULL || wpa_s->global->p2p_disabled)
748 return;
749 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
750 return;
751
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800752 switch (result) {
753 case OFFCHANNEL_SEND_ACTION_SUCCESS:
754 res = P2P_SEND_ACTION_SUCCESS;
755 break;
756 case OFFCHANNEL_SEND_ACTION_NO_ACK:
757 res = P2P_SEND_ACTION_NO_ACK;
758 break;
759 case OFFCHANNEL_SEND_ACTION_FAILED:
760 res = P2P_SEND_ACTION_FAILED;
761 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700762 }
763
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800764 p2p_send_action_cb(wpa_s->global->p2p, freq, dst, src, bssid, res);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700765
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -0800766 if (result != OFFCHANNEL_SEND_ACTION_SUCCESS &&
767 wpa_s->pending_pd_before_join &&
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800768 (os_memcmp(dst, wpa_s->pending_join_dev_addr, ETH_ALEN) == 0 ||
769 os_memcmp(dst, wpa_s->pending_join_iface_addr, ETH_ALEN) == 0)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700770 wpa_s->pending_pd_before_join = 0;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700771 if (wpa_s->p2p_fallback_to_go_neg) {
772 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: No ACK for PD Req "
773 "during p2p_connect-auto");
774 wpas_p2p_fallback_to_go_neg(wpa_s, 0);
775 return;
776 }
777
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700778 wpa_printf(MSG_DEBUG, "P2P: Starting pending "
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -0800779 "join-existing-group operation (no ACK for PD "
780 "Req)");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700781 wpas_p2p_join_start(wpa_s);
782 }
783}
784
785
786static int wpas_send_action(void *ctx, unsigned int freq, const u8 *dst,
787 const u8 *src, const u8 *bssid, const u8 *buf,
788 size_t len, unsigned int wait_time)
789{
790 struct wpa_supplicant *wpa_s = ctx;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800791 return offchannel_send_action(wpa_s, freq, dst, src, bssid, buf, len,
792 wait_time,
793 wpas_p2p_send_action_tx_status, 1);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700794}
795
796
797static void wpas_send_action_done(void *ctx)
798{
799 struct wpa_supplicant *wpa_s = ctx;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800800 offchannel_send_action_done(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700801}
802
803
804static int wpas_copy_go_neg_results(struct wpa_supplicant *wpa_s,
805 struct p2p_go_neg_results *params)
806{
807 if (wpa_s->go_params == NULL) {
808 wpa_s->go_params = os_malloc(sizeof(*params));
809 if (wpa_s->go_params == NULL)
810 return -1;
811 }
812 os_memcpy(wpa_s->go_params, params, sizeof(*params));
813 return 0;
814}
815
816
817static void wpas_start_wps_enrollee(struct wpa_supplicant *wpa_s,
818 struct p2p_go_neg_results *res)
819{
820 wpa_printf(MSG_DEBUG, "P2P: Start WPS Enrollee for peer " MACSTR,
821 MAC2STR(res->peer_interface_addr));
822 wpa_hexdump_ascii(MSG_DEBUG, "P2P: Start WPS Enrollee for SSID",
823 res->ssid, res->ssid_len);
824 wpa_supplicant_ap_deinit(wpa_s);
825 wpas_copy_go_neg_results(wpa_s, res);
826 if (res->wps_method == WPS_PBC)
827 wpas_wps_start_pbc(wpa_s, res->peer_interface_addr, 1);
828 else {
829 u16 dev_pw_id = DEV_PW_DEFAULT;
830 if (wpa_s->p2p_wps_method == WPS_PIN_KEYPAD)
831 dev_pw_id = DEV_PW_REGISTRAR_SPECIFIED;
832 wpas_wps_start_pin(wpa_s, res->peer_interface_addr,
833 wpa_s->p2p_pin, 1, dev_pw_id);
834 }
835}
836
837
838static void p2p_go_configured(void *ctx, void *data)
839{
840 struct wpa_supplicant *wpa_s = ctx;
841 struct p2p_go_neg_results *params = data;
842 struct wpa_ssid *ssid;
Jouni Malinen75ecf522011-06-27 15:19:46 -0700843 int network_id = -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700844
845 ssid = wpa_s->current_ssid;
846 if (ssid && ssid->mode == WPAS_MODE_P2P_GO) {
847 wpa_printf(MSG_DEBUG, "P2P: Group setup without provisioning");
848 if (wpa_s->global->p2p_group_formation == wpa_s)
849 wpa_s->global->p2p_group_formation = NULL;
850 wpa_msg(wpa_s->parent, MSG_INFO, P2P_EVENT_GROUP_STARTED
851 "%s GO ssid=\"%s\" freq=%d passphrase=\"%s\" "
852 "go_dev_addr=" MACSTR "%s",
853 wpa_s->ifname,
854 wpa_ssid_txt(ssid->ssid, ssid->ssid_len),
855 ssid->frequency,
856 params->passphrase ? params->passphrase : "",
Dmitry Shmidt497c1d52011-07-21 15:19:46 -0700857 MAC2STR(wpa_s->global->p2p_dev_addr),
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700858 params->persistent_group ? " [PERSISTENT]" : "");
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800859
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700860 if (params->persistent_group)
Jouni Malinen75ecf522011-06-27 15:19:46 -0700861 network_id = wpas_p2p_store_persistent_group(
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700862 wpa_s->parent, ssid,
Dmitry Shmidt497c1d52011-07-21 15:19:46 -0700863 wpa_s->global->p2p_dev_addr);
Jouni Malinen75ecf522011-06-27 15:19:46 -0700864 if (network_id < 0)
865 network_id = ssid->id;
866 wpas_notify_p2p_group_started(wpa_s, ssid, network_id, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700867 wpas_p2p_cross_connect_setup(wpa_s);
868 wpas_p2p_set_group_idle_timeout(wpa_s);
869 return;
870 }
871
872 wpa_printf(MSG_DEBUG, "P2P: Setting up WPS for GO provisioning");
873 if (wpa_supplicant_ap_mac_addr_filter(wpa_s,
874 params->peer_interface_addr)) {
875 wpa_printf(MSG_DEBUG, "P2P: Failed to setup MAC address "
876 "filtering");
877 return;
878 }
879 if (params->wps_method == WPS_PBC)
880 wpa_supplicant_ap_wps_pbc(wpa_s, params->peer_interface_addr,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800881 params->peer_device_addr);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700882 else if (wpa_s->p2p_pin[0])
883 wpa_supplicant_ap_wps_pin(wpa_s, params->peer_interface_addr,
884 wpa_s->p2p_pin, NULL, 0);
885 os_free(wpa_s->go_params);
886 wpa_s->go_params = NULL;
887}
888
889
890static void wpas_start_wps_go(struct wpa_supplicant *wpa_s,
891 struct p2p_go_neg_results *params,
892 int group_formation)
893{
894 struct wpa_ssid *ssid;
895
896 if (wpas_copy_go_neg_results(wpa_s, params) < 0)
897 return;
898
899 ssid = wpa_config_add_network(wpa_s->conf);
900 if (ssid == NULL)
901 return;
902
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800903 wpa_s->show_group_started = 0;
904
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700905 wpa_config_set_network_defaults(ssid);
906 ssid->temporary = 1;
907 ssid->p2p_group = 1;
908 ssid->p2p_persistent_group = params->persistent_group;
909 ssid->mode = group_formation ? WPAS_MODE_P2P_GROUP_FORMATION :
910 WPAS_MODE_P2P_GO;
911 ssid->frequency = params->freq;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700912 ssid->ht40 = params->ht40;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700913 ssid->ssid = os_zalloc(params->ssid_len + 1);
914 if (ssid->ssid) {
915 os_memcpy(ssid->ssid, params->ssid, params->ssid_len);
916 ssid->ssid_len = params->ssid_len;
917 }
918 ssid->auth_alg = WPA_AUTH_ALG_OPEN;
919 ssid->key_mgmt = WPA_KEY_MGMT_PSK;
920 ssid->proto = WPA_PROTO_RSN;
921 ssid->pairwise_cipher = WPA_CIPHER_CCMP;
922 ssid->passphrase = os_strdup(params->passphrase);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700923 ssid->ap_max_inactivity = wpa_s->parent->conf->p2p_go_max_inactivity;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700924
925 wpa_s->ap_configured_cb = p2p_go_configured;
926 wpa_s->ap_configured_cb_ctx = wpa_s;
927 wpa_s->ap_configured_cb_data = wpa_s->go_params;
Jouni Malinen75ecf522011-06-27 15:19:46 -0700928 wpa_s->connect_without_scan = ssid;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700929 wpa_s->reassociate = 1;
930 wpa_s->disconnected = 0;
931 wpa_supplicant_req_scan(wpa_s, 0, 0);
932}
933
934
935static void wpas_p2p_clone_config(struct wpa_supplicant *dst,
936 const struct wpa_supplicant *src)
937{
938 struct wpa_config *d;
939 const struct wpa_config *s;
940
941 d = dst->conf;
942 s = src->conf;
943
944#define C(n) if (s->n) d->n = os_strdup(s->n)
945 C(device_name);
946 C(manufacturer);
947 C(model_name);
948 C(model_number);
949 C(serial_number);
950 C(config_methods);
951#undef C
952
953 os_memcpy(d->device_type, s->device_type, WPS_DEV_TYPE_LEN);
954 os_memcpy(d->sec_device_type, s->sec_device_type,
955 sizeof(d->sec_device_type));
956 d->num_sec_device_types = s->num_sec_device_types;
957
958 d->p2p_group_idle = s->p2p_group_idle;
959 d->p2p_intra_bss = s->p2p_intra_bss;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800960 d->persistent_reconnect = s->persistent_reconnect;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700961 d->max_num_sta = s->max_num_sta;
962 d->pbc_in_m1 = s->pbc_in_m1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700963}
964
965
966static int wpas_p2p_add_group_interface(struct wpa_supplicant *wpa_s,
967 enum wpa_driver_if_type type)
968{
969 char ifname[120], force_ifname[120];
970
971 if (wpa_s->pending_interface_name[0]) {
972 wpa_printf(MSG_DEBUG, "P2P: Pending virtual interface exists "
973 "- skip creation of a new one");
974 if (is_zero_ether_addr(wpa_s->pending_interface_addr)) {
975 wpa_printf(MSG_DEBUG, "P2P: Pending virtual address "
976 "unknown?! ifname='%s'",
977 wpa_s->pending_interface_name);
978 return -1;
979 }
980 return 0;
981 }
982
983 os_snprintf(ifname, sizeof(ifname), "p2p-%s-%d", wpa_s->ifname,
984 wpa_s->p2p_group_idx);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800985 if (os_strlen(ifname) >= IFNAMSIZ &&
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700986 os_strlen(wpa_s->ifname) < IFNAMSIZ) {
987 /* Try to avoid going over the IFNAMSIZ length limit */
988 os_snprintf(ifname, sizeof(ifname), "p2p-%d",
989 wpa_s->p2p_group_idx);
990 }
991 force_ifname[0] = '\0';
992
993 wpa_printf(MSG_DEBUG, "P2P: Create a new interface %s for the group",
994 ifname);
995 wpa_s->p2p_group_idx++;
996
997 wpa_s->pending_interface_type = type;
998 if (wpa_drv_if_add(wpa_s, type, ifname, NULL, NULL, force_ifname,
999 wpa_s->pending_interface_addr, NULL) < 0) {
1000 wpa_printf(MSG_ERROR, "P2P: Failed to create new group "
1001 "interface");
1002 return -1;
1003 }
1004
1005 if (force_ifname[0]) {
1006 wpa_printf(MSG_DEBUG, "P2P: Driver forced interface name %s",
1007 force_ifname);
1008 os_strlcpy(wpa_s->pending_interface_name, force_ifname,
1009 sizeof(wpa_s->pending_interface_name));
1010 } else
1011 os_strlcpy(wpa_s->pending_interface_name, ifname,
1012 sizeof(wpa_s->pending_interface_name));
1013 wpa_printf(MSG_DEBUG, "P2P: Created pending virtual interface %s addr "
1014 MACSTR, wpa_s->pending_interface_name,
1015 MAC2STR(wpa_s->pending_interface_addr));
1016
1017 return 0;
1018}
1019
1020
1021static void wpas_p2p_remove_pending_group_interface(
1022 struct wpa_supplicant *wpa_s)
1023{
1024 if (!wpa_s->pending_interface_name[0] ||
1025 is_zero_ether_addr(wpa_s->pending_interface_addr))
1026 return; /* No pending virtual interface */
1027
1028 wpa_printf(MSG_DEBUG, "P2P: Removing pending group interface %s",
1029 wpa_s->pending_interface_name);
1030 wpa_drv_if_remove(wpa_s, wpa_s->pending_interface_type,
1031 wpa_s->pending_interface_name);
1032 os_memset(wpa_s->pending_interface_addr, 0, ETH_ALEN);
1033 wpa_s->pending_interface_name[0] = '\0';
1034}
1035
1036
1037static struct wpa_supplicant *
1038wpas_p2p_init_group_interface(struct wpa_supplicant *wpa_s, int go)
1039{
1040 struct wpa_interface iface;
1041 struct wpa_supplicant *group_wpa_s;
1042
1043 if (!wpa_s->pending_interface_name[0]) {
1044 wpa_printf(MSG_ERROR, "P2P: No pending group interface");
1045 if (!wpas_p2p_create_iface(wpa_s))
1046 return NULL;
1047 /*
1048 * Something has forced us to remove the pending interface; try
1049 * to create a new one and hope for the best that we will get
1050 * the same local address.
1051 */
1052 if (wpas_p2p_add_group_interface(wpa_s, go ? WPA_IF_P2P_GO :
1053 WPA_IF_P2P_CLIENT) < 0)
1054 return NULL;
1055 }
1056
1057 os_memset(&iface, 0, sizeof(iface));
1058 iface.ifname = wpa_s->pending_interface_name;
1059 iface.driver = wpa_s->driver->name;
1060 iface.ctrl_interface = wpa_s->conf->ctrl_interface;
1061 iface.driver_param = wpa_s->conf->driver_param;
1062 group_wpa_s = wpa_supplicant_add_iface(wpa_s->global, &iface);
1063 if (group_wpa_s == NULL) {
1064 wpa_printf(MSG_ERROR, "P2P: Failed to create new "
1065 "wpa_supplicant interface");
1066 return NULL;
1067 }
1068 wpa_s->pending_interface_name[0] = '\0';
1069 group_wpa_s->parent = wpa_s;
1070 group_wpa_s->p2p_group_interface = go ? P2P_GROUP_INTERFACE_GO :
1071 P2P_GROUP_INTERFACE_CLIENT;
1072 wpa_s->global->p2p_group_formation = group_wpa_s;
1073
1074 wpas_p2p_clone_config(group_wpa_s, wpa_s);
1075
1076 return group_wpa_s;
1077}
1078
1079
1080static void wpas_p2p_group_formation_timeout(void *eloop_ctx,
1081 void *timeout_ctx)
1082{
1083 struct wpa_supplicant *wpa_s = eloop_ctx;
1084 wpa_printf(MSG_DEBUG, "P2P: Group Formation timed out");
1085 if (wpa_s->global->p2p)
1086 p2p_group_formation_failed(wpa_s->global->p2p);
1087 else if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
1088 wpa_drv_p2p_group_formation_failed(wpa_s);
1089 wpas_group_formation_completed(wpa_s, 0);
1090}
1091
1092
1093void wpas_go_neg_completed(void *ctx, struct p2p_go_neg_results *res)
1094{
1095 struct wpa_supplicant *wpa_s = ctx;
1096
1097 if (wpa_s->off_channel_freq || wpa_s->roc_waiting_drv_freq) {
1098 wpa_drv_cancel_remain_on_channel(wpa_s);
1099 wpa_s->off_channel_freq = 0;
1100 wpa_s->roc_waiting_drv_freq = 0;
1101 }
1102
1103 if (res->status) {
1104 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_GO_NEG_FAILURE "status=%d",
1105 res->status);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001106 wpas_notify_p2p_go_neg_completed(wpa_s, res);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001107 wpas_p2p_remove_pending_group_interface(wpa_s);
1108 return;
1109 }
1110
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001111 if (wpa_s->p2p_go_ht40)
1112 res->ht40 = 1;
1113
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001114 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_GO_NEG_SUCCESS);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001115 wpas_notify_p2p_go_neg_completed(wpa_s, res);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001116
Dmitry Shmidt04949592012-07-19 12:16:46 -07001117 if (res->role_go && wpa_s->p2p_persistent_id >= 0) {
1118 struct wpa_ssid *ssid;
1119 ssid = wpa_config_get_network(wpa_s->conf,
1120 wpa_s->p2p_persistent_id);
1121 if (ssid && ssid->disabled == 2 &&
1122 ssid->mode == WPAS_MODE_P2P_GO && ssid->passphrase) {
1123 size_t len = os_strlen(ssid->passphrase);
1124 wpa_printf(MSG_DEBUG, "P2P: Override passphrase based "
1125 "on requested persistent group");
1126 os_memcpy(res->passphrase, ssid->passphrase, len);
1127 res->passphrase[len] = '\0';
1128 }
1129 }
1130
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001131 if (wpa_s->create_p2p_iface) {
1132 struct wpa_supplicant *group_wpa_s =
1133 wpas_p2p_init_group_interface(wpa_s, res->role_go);
1134 if (group_wpa_s == NULL) {
1135 wpas_p2p_remove_pending_group_interface(wpa_s);
1136 return;
1137 }
1138 if (group_wpa_s != wpa_s) {
1139 os_memcpy(group_wpa_s->p2p_pin, wpa_s->p2p_pin,
1140 sizeof(group_wpa_s->p2p_pin));
1141 group_wpa_s->p2p_wps_method = wpa_s->p2p_wps_method;
1142 }
1143 os_memset(wpa_s->pending_interface_addr, 0, ETH_ALEN);
1144 wpa_s->pending_interface_name[0] = '\0';
1145 group_wpa_s->p2p_in_provisioning = 1;
1146
1147 if (res->role_go)
1148 wpas_start_wps_go(group_wpa_s, res, 1);
1149 else
1150 wpas_start_wps_enrollee(group_wpa_s, res);
1151 } else {
1152 wpa_s->p2p_in_provisioning = 1;
1153 wpa_s->global->p2p_group_formation = wpa_s;
1154
1155 if (res->role_go)
1156 wpas_start_wps_go(wpa_s, res, 1);
1157 else
1158 wpas_start_wps_enrollee(ctx, res);
1159 }
1160
1161 wpa_s->p2p_long_listen = 0;
1162 eloop_cancel_timeout(wpas_p2p_long_listen_timeout, wpa_s, NULL);
1163
1164 eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s, NULL);
1165 eloop_register_timeout(15 + res->peer_config_timeout / 100,
1166 (res->peer_config_timeout % 100) * 10000,
1167 wpas_p2p_group_formation_timeout, wpa_s, NULL);
1168}
1169
1170
1171void wpas_go_neg_req_rx(void *ctx, const u8 *src, u16 dev_passwd_id)
1172{
1173 struct wpa_supplicant *wpa_s = ctx;
1174 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_GO_NEG_REQUEST MACSTR
1175 " dev_passwd_id=%u", MAC2STR(src), dev_passwd_id);
1176
1177 wpas_notify_p2p_go_neg_req(wpa_s, src, dev_passwd_id);
1178}
1179
1180
1181void wpas_dev_found(void *ctx, const u8 *addr,
1182 const struct p2p_peer_info *info,
1183 int new_device)
1184{
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001185#ifndef CONFIG_NO_STDOUT_DEBUG
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001186 struct wpa_supplicant *wpa_s = ctx;
1187 char devtype[WPS_DEV_TYPE_BUFSIZE];
1188
1189 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_DEVICE_FOUND MACSTR
1190 " p2p_dev_addr=" MACSTR
1191 " pri_dev_type=%s name='%s' config_methods=0x%x "
1192 "dev_capab=0x%x group_capab=0x%x",
1193 MAC2STR(addr), MAC2STR(info->p2p_device_addr),
1194 wps_dev_type_bin2str(info->pri_dev_type, devtype,
1195 sizeof(devtype)),
1196 info->device_name, info->config_methods,
1197 info->dev_capab, info->group_capab);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001198#endif /* CONFIG_NO_STDOUT_DEBUG */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001199
1200 wpas_notify_p2p_device_found(ctx, info->p2p_device_addr, new_device);
1201}
1202
1203
1204static void wpas_dev_lost(void *ctx, const u8 *dev_addr)
1205{
1206 struct wpa_supplicant *wpa_s = ctx;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001207
Dmitry Shmidt497c1d52011-07-21 15:19:46 -07001208 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_DEVICE_LOST
1209 "p2p_dev_addr=" MACSTR, MAC2STR(dev_addr));
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001210
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001211 wpas_notify_p2p_device_lost(wpa_s, dev_addr);
1212}
1213
1214
1215static int wpas_start_listen(void *ctx, unsigned int freq,
1216 unsigned int duration,
1217 const struct wpabuf *probe_resp_ie)
1218{
1219 struct wpa_supplicant *wpa_s = ctx;
1220
1221 wpa_drv_set_ap_wps_ie(wpa_s, NULL, probe_resp_ie, NULL);
1222
1223 if (wpa_drv_probe_req_report(wpa_s, 1) < 0) {
1224 wpa_printf(MSG_DEBUG, "P2P: Failed to request the driver to "
1225 "report received Probe Request frames");
1226 return -1;
1227 }
1228
1229 wpa_s->pending_listen_freq = freq;
1230 wpa_s->pending_listen_duration = duration;
1231
1232 if (wpa_drv_remain_on_channel(wpa_s, freq, duration) < 0) {
1233 wpa_printf(MSG_DEBUG, "P2P: Failed to request the driver "
1234 "to remain on channel (%u MHz) for Listen "
1235 "state", freq);
1236 wpa_s->pending_listen_freq = 0;
1237 return -1;
1238 }
1239 wpa_s->off_channel_freq = 0;
1240 wpa_s->roc_waiting_drv_freq = freq;
1241
1242 return 0;
1243}
1244
1245
1246static void wpas_stop_listen(void *ctx)
1247{
1248 struct wpa_supplicant *wpa_s = ctx;
1249 if (wpa_s->off_channel_freq || wpa_s->roc_waiting_drv_freq) {
1250 wpa_drv_cancel_remain_on_channel(wpa_s);
1251 wpa_s->off_channel_freq = 0;
1252 wpa_s->roc_waiting_drv_freq = 0;
1253 }
1254 wpa_drv_set_ap_wps_ie(wpa_s, NULL, NULL, NULL);
1255 wpa_drv_probe_req_report(wpa_s, 0);
1256}
1257
1258
1259static int wpas_send_probe_resp(void *ctx, const struct wpabuf *buf)
1260{
1261 struct wpa_supplicant *wpa_s = ctx;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001262 return wpa_drv_send_mlme(wpa_s, wpabuf_head(buf), wpabuf_len(buf), 1);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001263}
1264
1265
1266static struct p2p_srv_bonjour *
1267wpas_p2p_service_get_bonjour(struct wpa_supplicant *wpa_s,
1268 const struct wpabuf *query)
1269{
1270 struct p2p_srv_bonjour *bsrv;
1271 size_t len;
1272
1273 len = wpabuf_len(query);
1274 dl_list_for_each(bsrv, &wpa_s->global->p2p_srv_bonjour,
1275 struct p2p_srv_bonjour, list) {
1276 if (len == wpabuf_len(bsrv->query) &&
1277 os_memcmp(wpabuf_head(query), wpabuf_head(bsrv->query),
1278 len) == 0)
1279 return bsrv;
1280 }
1281 return NULL;
1282}
1283
1284
1285static struct p2p_srv_upnp *
1286wpas_p2p_service_get_upnp(struct wpa_supplicant *wpa_s, u8 version,
1287 const char *service)
1288{
1289 struct p2p_srv_upnp *usrv;
1290
1291 dl_list_for_each(usrv, &wpa_s->global->p2p_srv_upnp,
1292 struct p2p_srv_upnp, list) {
1293 if (version == usrv->version &&
1294 os_strcmp(service, usrv->service) == 0)
1295 return usrv;
1296 }
1297 return NULL;
1298}
1299
1300
1301static void wpas_sd_add_proto_not_avail(struct wpabuf *resp, u8 srv_proto,
1302 u8 srv_trans_id)
1303{
1304 u8 *len_pos;
1305
1306 if (wpabuf_tailroom(resp) < 5)
1307 return;
1308
1309 /* Length (to be filled) */
1310 len_pos = wpabuf_put(resp, 2);
1311 wpabuf_put_u8(resp, srv_proto);
1312 wpabuf_put_u8(resp, srv_trans_id);
1313 /* Status Code */
1314 wpabuf_put_u8(resp, P2P_SD_PROTO_NOT_AVAILABLE);
1315 /* Response Data: empty */
1316 WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos - 2);
1317}
1318
1319
1320static void wpas_sd_all_bonjour(struct wpa_supplicant *wpa_s,
1321 struct wpabuf *resp, u8 srv_trans_id)
1322{
1323 struct p2p_srv_bonjour *bsrv;
1324 u8 *len_pos;
1325
1326 wpa_printf(MSG_DEBUG, "P2P: SD Request for all Bonjour services");
1327
1328 if (dl_list_empty(&wpa_s->global->p2p_srv_bonjour)) {
1329 wpa_printf(MSG_DEBUG, "P2P: Bonjour protocol not available");
1330 return;
1331 }
1332
1333 dl_list_for_each(bsrv, &wpa_s->global->p2p_srv_bonjour,
1334 struct p2p_srv_bonjour, list) {
1335 if (wpabuf_tailroom(resp) <
1336 5 + wpabuf_len(bsrv->query) + wpabuf_len(bsrv->resp))
1337 return;
1338 /* Length (to be filled) */
1339 len_pos = wpabuf_put(resp, 2);
1340 wpabuf_put_u8(resp, P2P_SERV_BONJOUR);
1341 wpabuf_put_u8(resp, srv_trans_id);
1342 /* Status Code */
1343 wpabuf_put_u8(resp, P2P_SD_SUCCESS);
1344 wpa_hexdump_ascii(MSG_DEBUG, "P2P: Matching Bonjour service",
1345 wpabuf_head(bsrv->resp),
1346 wpabuf_len(bsrv->resp));
1347 /* Response Data */
1348 wpabuf_put_buf(resp, bsrv->query); /* Key */
1349 wpabuf_put_buf(resp, bsrv->resp); /* Value */
1350 WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos -
1351 2);
1352 }
1353}
1354
1355
1356static void wpas_sd_req_bonjour(struct wpa_supplicant *wpa_s,
1357 struct wpabuf *resp, u8 srv_trans_id,
1358 const u8 *query, size_t query_len)
1359{
1360 struct p2p_srv_bonjour *bsrv;
1361 struct wpabuf buf;
1362 u8 *len_pos;
1363
1364 wpa_hexdump_ascii(MSG_DEBUG, "P2P: SD Request for Bonjour",
1365 query, query_len);
1366 if (dl_list_empty(&wpa_s->global->p2p_srv_bonjour)) {
1367 wpa_printf(MSG_DEBUG, "P2P: Bonjour protocol not available");
1368 wpas_sd_add_proto_not_avail(resp, P2P_SERV_BONJOUR,
1369 srv_trans_id);
1370 return;
1371 }
1372
1373 if (query_len == 0) {
1374 wpas_sd_all_bonjour(wpa_s, resp, srv_trans_id);
1375 return;
1376 }
1377
1378 if (wpabuf_tailroom(resp) < 5)
1379 return;
1380 /* Length (to be filled) */
1381 len_pos = wpabuf_put(resp, 2);
1382 wpabuf_put_u8(resp, P2P_SERV_BONJOUR);
1383 wpabuf_put_u8(resp, srv_trans_id);
1384
1385 wpabuf_set(&buf, query, query_len);
1386 bsrv = wpas_p2p_service_get_bonjour(wpa_s, &buf);
1387 if (bsrv == NULL) {
1388 wpa_printf(MSG_DEBUG, "P2P: Requested Bonjour service not "
1389 "available");
1390
1391 /* Status Code */
1392 wpabuf_put_u8(resp, P2P_SD_REQUESTED_INFO_NOT_AVAILABLE);
1393 /* Response Data: empty */
1394 WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos -
1395 2);
1396 return;
1397 }
1398
1399 /* Status Code */
1400 wpabuf_put_u8(resp, P2P_SD_SUCCESS);
1401 wpa_hexdump_ascii(MSG_DEBUG, "P2P: Matching Bonjour service",
1402 wpabuf_head(bsrv->resp), wpabuf_len(bsrv->resp));
1403
1404 if (wpabuf_tailroom(resp) >=
1405 wpabuf_len(bsrv->query) + wpabuf_len(bsrv->resp)) {
1406 /* Response Data */
1407 wpabuf_put_buf(resp, bsrv->query); /* Key */
1408 wpabuf_put_buf(resp, bsrv->resp); /* Value */
1409 }
1410 WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos - 2);
1411}
1412
1413
1414static void wpas_sd_all_upnp(struct wpa_supplicant *wpa_s,
1415 struct wpabuf *resp, u8 srv_trans_id)
1416{
1417 struct p2p_srv_upnp *usrv;
1418 u8 *len_pos;
1419
1420 wpa_printf(MSG_DEBUG, "P2P: SD Request for all UPnP services");
1421
1422 if (dl_list_empty(&wpa_s->global->p2p_srv_upnp)) {
1423 wpa_printf(MSG_DEBUG, "P2P: UPnP protocol not available");
1424 return;
1425 }
1426
1427 dl_list_for_each(usrv, &wpa_s->global->p2p_srv_upnp,
1428 struct p2p_srv_upnp, list) {
1429 if (wpabuf_tailroom(resp) < 5 + 1 + os_strlen(usrv->service))
1430 return;
1431
1432 /* Length (to be filled) */
1433 len_pos = wpabuf_put(resp, 2);
1434 wpabuf_put_u8(resp, P2P_SERV_UPNP);
1435 wpabuf_put_u8(resp, srv_trans_id);
1436
1437 /* Status Code */
1438 wpabuf_put_u8(resp, P2P_SD_SUCCESS);
1439 /* Response Data */
1440 wpabuf_put_u8(resp, usrv->version);
1441 wpa_printf(MSG_DEBUG, "P2P: Matching UPnP Service: %s",
1442 usrv->service);
1443 wpabuf_put_str(resp, usrv->service);
1444 WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos -
1445 2);
1446 }
1447}
1448
1449
1450static void wpas_sd_req_upnp(struct wpa_supplicant *wpa_s,
1451 struct wpabuf *resp, u8 srv_trans_id,
1452 const u8 *query, size_t query_len)
1453{
1454 struct p2p_srv_upnp *usrv;
1455 u8 *len_pos;
1456 u8 version;
1457 char *str;
1458 int count = 0;
1459
1460 wpa_hexdump_ascii(MSG_DEBUG, "P2P: SD Request for UPnP",
1461 query, query_len);
1462
1463 if (dl_list_empty(&wpa_s->global->p2p_srv_upnp)) {
1464 wpa_printf(MSG_DEBUG, "P2P: UPnP protocol not available");
1465 wpas_sd_add_proto_not_avail(resp, P2P_SERV_UPNP,
1466 srv_trans_id);
1467 return;
1468 }
1469
1470 if (query_len == 0) {
1471 wpas_sd_all_upnp(wpa_s, resp, srv_trans_id);
1472 return;
1473 }
1474
1475 if (wpabuf_tailroom(resp) < 5)
1476 return;
1477
1478 /* Length (to be filled) */
1479 len_pos = wpabuf_put(resp, 2);
1480 wpabuf_put_u8(resp, P2P_SERV_UPNP);
1481 wpabuf_put_u8(resp, srv_trans_id);
1482
1483 version = query[0];
1484 str = os_malloc(query_len);
1485 if (str == NULL)
1486 return;
1487 os_memcpy(str, query + 1, query_len - 1);
1488 str[query_len - 1] = '\0';
1489
1490 dl_list_for_each(usrv, &wpa_s->global->p2p_srv_upnp,
1491 struct p2p_srv_upnp, list) {
1492 if (version != usrv->version)
1493 continue;
1494
1495 if (os_strcmp(str, "ssdp:all") != 0 &&
1496 os_strstr(usrv->service, str) == NULL)
1497 continue;
1498
1499 if (wpabuf_tailroom(resp) < 2)
1500 break;
1501 if (count == 0) {
1502 /* Status Code */
1503 wpabuf_put_u8(resp, P2P_SD_SUCCESS);
1504 /* Response Data */
1505 wpabuf_put_u8(resp, version);
1506 } else
1507 wpabuf_put_u8(resp, ',');
1508
1509 count++;
1510
1511 wpa_printf(MSG_DEBUG, "P2P: Matching UPnP Service: %s",
1512 usrv->service);
1513 if (wpabuf_tailroom(resp) < os_strlen(usrv->service))
1514 break;
1515 wpabuf_put_str(resp, usrv->service);
1516 }
1517 os_free(str);
1518
1519 if (count == 0) {
1520 wpa_printf(MSG_DEBUG, "P2P: Requested UPnP service not "
1521 "available");
1522 /* Status Code */
1523 wpabuf_put_u8(resp, P2P_SD_REQUESTED_INFO_NOT_AVAILABLE);
1524 /* Response Data: empty */
1525 }
1526
1527 WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos - 2);
1528}
1529
1530
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001531#ifdef CONFIG_WIFI_DISPLAY
1532static void wpas_sd_req_wfd(struct wpa_supplicant *wpa_s,
1533 struct wpabuf *resp, u8 srv_trans_id,
1534 const u8 *query, size_t query_len)
1535{
1536 const u8 *pos;
1537 u8 role;
1538 u8 *len_pos;
1539
1540 wpa_hexdump(MSG_DEBUG, "P2P: SD Request for WFD", query, query_len);
1541
1542 if (!wpa_s->global->wifi_display) {
1543 wpa_printf(MSG_DEBUG, "P2P: WFD protocol not available");
1544 wpas_sd_add_proto_not_avail(resp, P2P_SERV_WIFI_DISPLAY,
1545 srv_trans_id);
1546 return;
1547 }
1548
1549 if (query_len < 1) {
1550 wpa_printf(MSG_DEBUG, "P2P: Missing WFD Requested Device "
1551 "Role");
1552 return;
1553 }
1554
1555 if (wpabuf_tailroom(resp) < 5)
1556 return;
1557
1558 pos = query;
1559 role = *pos++;
1560 wpa_printf(MSG_DEBUG, "P2P: WSD for device role 0x%x", role);
1561
1562 /* TODO: role specific handling */
1563
1564 /* Length (to be filled) */
1565 len_pos = wpabuf_put(resp, 2);
1566 wpabuf_put_u8(resp, P2P_SERV_WIFI_DISPLAY);
1567 wpabuf_put_u8(resp, srv_trans_id);
1568 wpabuf_put_u8(resp, P2P_SD_SUCCESS); /* Status Code */
1569
1570 while (pos < query + query_len) {
1571 if (*pos < MAX_WFD_SUBELEMS &&
1572 wpa_s->global->wfd_subelem[*pos] &&
1573 wpabuf_tailroom(resp) >=
1574 wpabuf_len(wpa_s->global->wfd_subelem[*pos])) {
1575 wpa_printf(MSG_DEBUG, "P2P: Add WSD response "
1576 "subelement %u", *pos);
1577 wpabuf_put_buf(resp, wpa_s->global->wfd_subelem[*pos]);
1578 }
1579 pos++;
1580 }
1581
1582 WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos - 2);
1583}
1584#endif /* CONFIG_WIFI_DISPLAY */
1585
1586
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001587void wpas_sd_request(void *ctx, int freq, const u8 *sa, u8 dialog_token,
1588 u16 update_indic, const u8 *tlvs, size_t tlvs_len)
1589{
1590 struct wpa_supplicant *wpa_s = ctx;
1591 const u8 *pos = tlvs;
1592 const u8 *end = tlvs + tlvs_len;
1593 const u8 *tlv_end;
1594 u16 slen;
1595 struct wpabuf *resp;
1596 u8 srv_proto, srv_trans_id;
1597 size_t buf_len;
1598 char *buf;
1599
1600 wpa_hexdump(MSG_MSGDUMP, "P2P: Service Discovery Request TLVs",
1601 tlvs, tlvs_len);
1602 buf_len = 2 * tlvs_len + 1;
1603 buf = os_malloc(buf_len);
1604 if (buf) {
1605 wpa_snprintf_hex(buf, buf_len, tlvs, tlvs_len);
1606 wpa_msg_ctrl(wpa_s, MSG_INFO, P2P_EVENT_SERV_DISC_REQ "%d "
1607 MACSTR " %u %u %s",
1608 freq, MAC2STR(sa), dialog_token, update_indic,
1609 buf);
1610 os_free(buf);
1611 }
1612
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001613 if (wpa_s->p2p_sd_over_ctrl_iface) {
1614 wpas_notify_p2p_sd_request(wpa_s, freq, sa, dialog_token,
1615 update_indic, tlvs, tlvs_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001616 return; /* to be processed by an external program */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001617 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001618
1619 resp = wpabuf_alloc(10000);
1620 if (resp == NULL)
1621 return;
1622
1623 while (pos + 1 < end) {
1624 wpa_printf(MSG_DEBUG, "P2P: Service Request TLV");
1625 slen = WPA_GET_LE16(pos);
1626 pos += 2;
1627 if (pos + slen > end || slen < 2) {
1628 wpa_printf(MSG_DEBUG, "P2P: Unexpected Query Data "
1629 "length");
1630 wpabuf_free(resp);
1631 return;
1632 }
1633 tlv_end = pos + slen;
1634
1635 srv_proto = *pos++;
1636 wpa_printf(MSG_DEBUG, "P2P: Service Protocol Type %u",
1637 srv_proto);
1638 srv_trans_id = *pos++;
1639 wpa_printf(MSG_DEBUG, "P2P: Service Transaction ID %u",
1640 srv_trans_id);
1641
1642 wpa_hexdump(MSG_MSGDUMP, "P2P: Query Data",
1643 pos, tlv_end - pos);
1644
1645
1646 if (wpa_s->force_long_sd) {
1647 wpa_printf(MSG_DEBUG, "P2P: SD test - force long "
1648 "response");
1649 wpas_sd_all_bonjour(wpa_s, resp, srv_trans_id);
1650 wpas_sd_all_upnp(wpa_s, resp, srv_trans_id);
1651 goto done;
1652 }
1653
1654 switch (srv_proto) {
1655 case P2P_SERV_ALL_SERVICES:
1656 wpa_printf(MSG_DEBUG, "P2P: Service Discovery Request "
1657 "for all services");
1658 if (dl_list_empty(&wpa_s->global->p2p_srv_upnp) &&
1659 dl_list_empty(&wpa_s->global->p2p_srv_bonjour)) {
1660 wpa_printf(MSG_DEBUG, "P2P: No service "
1661 "discovery protocols available");
1662 wpas_sd_add_proto_not_avail(
1663 resp, P2P_SERV_ALL_SERVICES,
1664 srv_trans_id);
1665 break;
1666 }
1667 wpas_sd_all_bonjour(wpa_s, resp, srv_trans_id);
1668 wpas_sd_all_upnp(wpa_s, resp, srv_trans_id);
1669 break;
1670 case P2P_SERV_BONJOUR:
1671 wpas_sd_req_bonjour(wpa_s, resp, srv_trans_id,
1672 pos, tlv_end - pos);
1673 break;
1674 case P2P_SERV_UPNP:
1675 wpas_sd_req_upnp(wpa_s, resp, srv_trans_id,
1676 pos, tlv_end - pos);
1677 break;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001678#ifdef CONFIG_WIFI_DISPLAY
1679 case P2P_SERV_WIFI_DISPLAY:
1680 wpas_sd_req_wfd(wpa_s, resp, srv_trans_id,
1681 pos, tlv_end - pos);
1682 break;
1683#endif /* CONFIG_WIFI_DISPLAY */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001684 default:
1685 wpa_printf(MSG_DEBUG, "P2P: Unavailable service "
1686 "protocol %u", srv_proto);
1687 wpas_sd_add_proto_not_avail(resp, srv_proto,
1688 srv_trans_id);
1689 break;
1690 }
1691
1692 pos = tlv_end;
1693 }
1694
1695done:
1696 wpas_notify_p2p_sd_request(wpa_s, freq, sa, dialog_token,
1697 update_indic, tlvs, tlvs_len);
1698
1699 wpas_p2p_sd_response(wpa_s, freq, sa, dialog_token, resp);
1700
1701 wpabuf_free(resp);
1702}
1703
1704
1705void wpas_sd_response(void *ctx, const u8 *sa, u16 update_indic,
1706 const u8 *tlvs, size_t tlvs_len)
1707{
1708 struct wpa_supplicant *wpa_s = ctx;
1709 const u8 *pos = tlvs;
1710 const u8 *end = tlvs + tlvs_len;
1711 const u8 *tlv_end;
1712 u16 slen;
1713 size_t buf_len;
1714 char *buf;
1715
1716 wpa_hexdump(MSG_MSGDUMP, "P2P: Service Discovery Response TLVs",
1717 tlvs, tlvs_len);
1718 if (tlvs_len > 1500) {
1719 /* TODO: better way for handling this */
1720 wpa_msg_ctrl(wpa_s, MSG_INFO,
1721 P2P_EVENT_SERV_DISC_RESP MACSTR
1722 " %u <long response: %u bytes>",
1723 MAC2STR(sa), update_indic,
1724 (unsigned int) tlvs_len);
1725 } else {
1726 buf_len = 2 * tlvs_len + 1;
1727 buf = os_malloc(buf_len);
1728 if (buf) {
1729 wpa_snprintf_hex(buf, buf_len, tlvs, tlvs_len);
1730 wpa_msg_ctrl(wpa_s, MSG_INFO,
1731 P2P_EVENT_SERV_DISC_RESP MACSTR " %u %s",
1732 MAC2STR(sa), update_indic, buf);
1733 os_free(buf);
1734 }
1735 }
1736
1737 while (pos < end) {
1738 u8 srv_proto, srv_trans_id, status;
1739
1740 wpa_printf(MSG_DEBUG, "P2P: Service Response TLV");
1741 slen = WPA_GET_LE16(pos);
1742 pos += 2;
1743 if (pos + slen > end || slen < 3) {
1744 wpa_printf(MSG_DEBUG, "P2P: Unexpected Response Data "
1745 "length");
1746 return;
1747 }
1748 tlv_end = pos + slen;
1749
1750 srv_proto = *pos++;
1751 wpa_printf(MSG_DEBUG, "P2P: Service Protocol Type %u",
1752 srv_proto);
1753 srv_trans_id = *pos++;
1754 wpa_printf(MSG_DEBUG, "P2P: Service Transaction ID %u",
1755 srv_trans_id);
1756 status = *pos++;
1757 wpa_printf(MSG_DEBUG, "P2P: Status Code ID %u",
1758 status);
1759
1760 wpa_hexdump(MSG_MSGDUMP, "P2P: Response Data",
1761 pos, tlv_end - pos);
1762
1763 pos = tlv_end;
1764 }
1765
1766 wpas_notify_p2p_sd_response(wpa_s, sa, update_indic, tlvs, tlvs_len);
1767}
1768
1769
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001770u64 wpas_p2p_sd_request(struct wpa_supplicant *wpa_s, const u8 *dst,
1771 const struct wpabuf *tlvs)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001772{
1773 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001774 return wpa_drv_p2p_sd_request(wpa_s, dst, tlvs);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001775 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001776 return 0;
1777 return (uintptr_t) p2p_sd_request(wpa_s->global->p2p, dst, tlvs);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001778}
1779
1780
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001781u64 wpas_p2p_sd_request_upnp(struct wpa_supplicant *wpa_s, const u8 *dst,
1782 u8 version, const char *query)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001783{
1784 struct wpabuf *tlvs;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001785 u64 ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001786
1787 tlvs = wpabuf_alloc(2 + 1 + 1 + 1 + os_strlen(query));
1788 if (tlvs == NULL)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001789 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001790 wpabuf_put_le16(tlvs, 1 + 1 + 1 + os_strlen(query));
1791 wpabuf_put_u8(tlvs, P2P_SERV_UPNP); /* Service Protocol Type */
1792 wpabuf_put_u8(tlvs, 1); /* Service Transaction ID */
1793 wpabuf_put_u8(tlvs, version);
1794 wpabuf_put_str(tlvs, query);
1795 ret = wpas_p2p_sd_request(wpa_s, dst, tlvs);
1796 wpabuf_free(tlvs);
1797 return ret;
1798}
1799
1800
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001801#ifdef CONFIG_WIFI_DISPLAY
1802
1803static u64 wpas_p2p_sd_request_wfd(struct wpa_supplicant *wpa_s, const u8 *dst,
1804 const struct wpabuf *tlvs)
1805{
1806 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
1807 return 0;
1808 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
1809 return 0;
1810 return (uintptr_t) p2p_sd_request_wfd(wpa_s->global->p2p, dst, tlvs);
1811}
1812
1813
1814#define MAX_WFD_SD_SUBELEMS 20
1815
1816static void wfd_add_sd_req_role(struct wpabuf *tlvs, u8 id, u8 role,
1817 const char *subelems)
1818{
1819 u8 *len;
1820 const char *pos;
1821 int val;
1822 int count = 0;
1823
1824 len = wpabuf_put(tlvs, 2);
1825 wpabuf_put_u8(tlvs, P2P_SERV_WIFI_DISPLAY); /* Service Protocol Type */
1826 wpabuf_put_u8(tlvs, id); /* Service Transaction ID */
1827
1828 wpabuf_put_u8(tlvs, role);
1829
1830 pos = subelems;
1831 while (*pos) {
1832 val = atoi(pos);
1833 if (val >= 0 && val < 256) {
1834 wpabuf_put_u8(tlvs, val);
1835 count++;
1836 if (count == MAX_WFD_SD_SUBELEMS)
1837 break;
1838 }
1839 pos = os_strchr(pos + 1, ',');
1840 if (pos == NULL)
1841 break;
1842 pos++;
1843 }
1844
1845 WPA_PUT_LE16(len, (u8 *) wpabuf_put(tlvs, 0) - len - 2);
1846}
1847
1848
1849u64 wpas_p2p_sd_request_wifi_display(struct wpa_supplicant *wpa_s,
1850 const u8 *dst, const char *role)
1851{
1852 struct wpabuf *tlvs;
1853 u64 ret;
1854 const char *subelems;
1855 u8 id = 1;
1856
1857 subelems = os_strchr(role, ' ');
1858 if (subelems == NULL)
1859 return 0;
1860 subelems++;
1861
1862 tlvs = wpabuf_alloc(4 * (2 + 1 + 1 + 1 + MAX_WFD_SD_SUBELEMS));
1863 if (tlvs == NULL)
1864 return 0;
1865
1866 if (os_strstr(role, "[source]"))
1867 wfd_add_sd_req_role(tlvs, id++, 0x00, subelems);
1868 if (os_strstr(role, "[pri-sink]"))
1869 wfd_add_sd_req_role(tlvs, id++, 0x01, subelems);
1870 if (os_strstr(role, "[sec-sink]"))
1871 wfd_add_sd_req_role(tlvs, id++, 0x02, subelems);
1872 if (os_strstr(role, "[source+sink]"))
1873 wfd_add_sd_req_role(tlvs, id++, 0x03, subelems);
1874
1875 ret = wpas_p2p_sd_request_wfd(wpa_s, dst, tlvs);
1876 wpabuf_free(tlvs);
1877 return ret;
1878}
1879
1880#endif /* CONFIG_WIFI_DISPLAY */
1881
1882
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001883int wpas_p2p_sd_cancel_request(struct wpa_supplicant *wpa_s, u64 req)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001884{
1885 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001886 return wpa_drv_p2p_sd_cancel_request(wpa_s, req);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001887 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
1888 return -1;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001889 return p2p_sd_cancel_request(wpa_s->global->p2p,
1890 (void *) (uintptr_t) req);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001891}
1892
1893
1894void wpas_p2p_sd_response(struct wpa_supplicant *wpa_s, int freq,
1895 const u8 *dst, u8 dialog_token,
1896 const struct wpabuf *resp_tlvs)
1897{
1898 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT) {
1899 wpa_drv_p2p_sd_response(wpa_s, freq, dst, dialog_token,
1900 resp_tlvs);
1901 return;
1902 }
1903 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
1904 return;
1905 p2p_sd_response(wpa_s->global->p2p, freq, dst, dialog_token,
1906 resp_tlvs);
1907}
1908
Dmitry Shmidtb5e8f062012-08-08 10:56:33 -07001909#ifdef ANDROID_P2P
1910void wpas_p2p_sd_service_update(struct wpa_supplicant *wpa_s, int action)
1911#else
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001912void wpas_p2p_sd_service_update(struct wpa_supplicant *wpa_s)
Dmitry Shmidtb5e8f062012-08-08 10:56:33 -07001913#endif
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001914{
1915 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT) {
1916 wpa_drv_p2p_service_update(wpa_s);
1917 return;
1918 }
1919 if (wpa_s->global->p2p)
Dmitry Shmidtb5e8f062012-08-08 10:56:33 -07001920#ifdef ANDROID_P2P
1921 p2p_sd_service_update(wpa_s->global->p2p, action);
1922#else
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001923 p2p_sd_service_update(wpa_s->global->p2p);
Dmitry Shmidtb5e8f062012-08-08 10:56:33 -07001924#endif
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001925}
1926
1927
1928static void wpas_p2p_srv_bonjour_free(struct p2p_srv_bonjour *bsrv)
1929{
1930 dl_list_del(&bsrv->list);
1931 wpabuf_free(bsrv->query);
1932 wpabuf_free(bsrv->resp);
1933 os_free(bsrv);
1934}
1935
1936
1937static void wpas_p2p_srv_upnp_free(struct p2p_srv_upnp *usrv)
1938{
1939 dl_list_del(&usrv->list);
1940 os_free(usrv->service);
1941 os_free(usrv);
1942}
1943
1944
1945void wpas_p2p_service_flush(struct wpa_supplicant *wpa_s)
1946{
1947 struct p2p_srv_bonjour *bsrv, *bn;
1948 struct p2p_srv_upnp *usrv, *un;
1949
1950 dl_list_for_each_safe(bsrv, bn, &wpa_s->global->p2p_srv_bonjour,
1951 struct p2p_srv_bonjour, list)
1952 wpas_p2p_srv_bonjour_free(bsrv);
1953
1954 dl_list_for_each_safe(usrv, un, &wpa_s->global->p2p_srv_upnp,
1955 struct p2p_srv_upnp, list)
1956 wpas_p2p_srv_upnp_free(usrv);
1957
Dmitry Shmidtb5e8f062012-08-08 10:56:33 -07001958#ifdef ANDROID_P2P
1959 wpas_p2p_sd_service_update(wpa_s, SRV_FLUSH);
1960#else
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001961 wpas_p2p_sd_service_update(wpa_s);
Dmitry Shmidtb5e8f062012-08-08 10:56:33 -07001962#endif
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001963}
1964
1965
1966int wpas_p2p_service_add_bonjour(struct wpa_supplicant *wpa_s,
1967 struct wpabuf *query, struct wpabuf *resp)
1968{
1969 struct p2p_srv_bonjour *bsrv;
1970
1971 bsrv = wpas_p2p_service_get_bonjour(wpa_s, query);
1972 if (bsrv) {
1973 wpabuf_free(query);
1974 wpabuf_free(bsrv->resp);
1975 bsrv->resp = resp;
1976 return 0;
1977 }
1978
1979 bsrv = os_zalloc(sizeof(*bsrv));
1980 if (bsrv == NULL)
1981 return -1;
1982 bsrv->query = query;
1983 bsrv->resp = resp;
1984 dl_list_add(&wpa_s->global->p2p_srv_bonjour, &bsrv->list);
1985
Dmitry Shmidtb5e8f062012-08-08 10:56:33 -07001986#ifdef ANDROID_P2P
1987 wpas_p2p_sd_service_update(wpa_s, SRV_ADD);
1988#else
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001989 wpas_p2p_sd_service_update(wpa_s);
Dmitry Shmidtb5e8f062012-08-08 10:56:33 -07001990#endif
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001991 return 0;
1992}
1993
1994
1995int wpas_p2p_service_del_bonjour(struct wpa_supplicant *wpa_s,
1996 const struct wpabuf *query)
1997{
1998 struct p2p_srv_bonjour *bsrv;
1999
2000 bsrv = wpas_p2p_service_get_bonjour(wpa_s, query);
2001 if (bsrv == NULL)
2002 return -1;
2003 wpas_p2p_srv_bonjour_free(bsrv);
Dmitry Shmidtb5e8f062012-08-08 10:56:33 -07002004#ifdef ANDROID_P2P
2005 wpas_p2p_sd_service_update(wpa_s, SRV_DEL);
2006#else
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002007 wpas_p2p_sd_service_update(wpa_s);
Dmitry Shmidtb5e8f062012-08-08 10:56:33 -07002008#endif
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002009 return 0;
2010}
2011
2012
2013int wpas_p2p_service_add_upnp(struct wpa_supplicant *wpa_s, u8 version,
2014 const char *service)
2015{
2016 struct p2p_srv_upnp *usrv;
2017
2018 if (wpas_p2p_service_get_upnp(wpa_s, version, service))
2019 return 0; /* Already listed */
2020 usrv = os_zalloc(sizeof(*usrv));
2021 if (usrv == NULL)
2022 return -1;
2023 usrv->version = version;
2024 usrv->service = os_strdup(service);
2025 if (usrv->service == NULL) {
2026 os_free(usrv);
2027 return -1;
2028 }
2029 dl_list_add(&wpa_s->global->p2p_srv_upnp, &usrv->list);
2030
Dmitry Shmidtb5e8f062012-08-08 10:56:33 -07002031#ifdef ANDROID_P2P
2032 wpas_p2p_sd_service_update(wpa_s, SRV_ADD);
2033#else
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002034 wpas_p2p_sd_service_update(wpa_s);
Dmitry Shmidtb5e8f062012-08-08 10:56:33 -07002035#endif
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002036 return 0;
2037}
2038
2039
2040int wpas_p2p_service_del_upnp(struct wpa_supplicant *wpa_s, u8 version,
2041 const char *service)
2042{
2043 struct p2p_srv_upnp *usrv;
2044
2045 usrv = wpas_p2p_service_get_upnp(wpa_s, version, service);
2046 if (usrv == NULL)
2047 return -1;
2048 wpas_p2p_srv_upnp_free(usrv);
Dmitry Shmidtb5e8f062012-08-08 10:56:33 -07002049#ifdef ANDROID_P2P
2050 wpas_p2p_sd_service_update(wpa_s, SRV_DEL);
2051#else
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002052 wpas_p2p_sd_service_update(wpa_s);
Dmitry Shmidtb5e8f062012-08-08 10:56:33 -07002053#endif
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002054 return 0;
2055}
2056
2057
2058static void wpas_prov_disc_local_display(struct wpa_supplicant *wpa_s,
2059 const u8 *peer, const char *params,
2060 unsigned int generated_pin)
2061{
2062 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_SHOW_PIN MACSTR " %08d%s",
2063 MAC2STR(peer), generated_pin, params);
2064}
2065
2066
2067static void wpas_prov_disc_local_keypad(struct wpa_supplicant *wpa_s,
2068 const u8 *peer, const char *params)
2069{
2070 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_ENTER_PIN MACSTR "%s",
2071 MAC2STR(peer), params);
2072}
2073
2074
2075void wpas_prov_disc_req(void *ctx, const u8 *peer, u16 config_methods,
2076 const u8 *dev_addr, const u8 *pri_dev_type,
2077 const char *dev_name, u16 supp_config_methods,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002078 u8 dev_capab, u8 group_capab, const u8 *group_id,
2079 size_t group_id_len)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002080{
2081 struct wpa_supplicant *wpa_s = ctx;
2082 char devtype[WPS_DEV_TYPE_BUFSIZE];
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002083 char params[300];
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002084 u8 empty_dev_type[8];
2085 unsigned int generated_pin = 0;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002086 struct wpa_supplicant *group = NULL;
2087
2088 if (group_id) {
2089 for (group = wpa_s->global->ifaces; group; group = group->next)
2090 {
2091 struct wpa_ssid *s = group->current_ssid;
2092 if (s != NULL &&
2093 s->mode == WPAS_MODE_P2P_GO &&
2094 group_id_len - ETH_ALEN == s->ssid_len &&
2095 os_memcmp(group_id + ETH_ALEN, s->ssid,
2096 s->ssid_len) == 0)
2097 break;
2098 }
2099 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002100
2101 if (pri_dev_type == NULL) {
2102 os_memset(empty_dev_type, 0, sizeof(empty_dev_type));
2103 pri_dev_type = empty_dev_type;
2104 }
2105 os_snprintf(params, sizeof(params), " p2p_dev_addr=" MACSTR
2106 " pri_dev_type=%s name='%s' config_methods=0x%x "
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002107 "dev_capab=0x%x group_capab=0x%x%s%s",
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002108 MAC2STR(dev_addr),
2109 wps_dev_type_bin2str(pri_dev_type, devtype,
2110 sizeof(devtype)),
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002111 dev_name, supp_config_methods, dev_capab, group_capab,
2112 group ? " group=" : "",
2113 group ? group->ifname : "");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002114 params[sizeof(params) - 1] = '\0';
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08002115
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002116 if (config_methods & WPS_CONFIG_DISPLAY) {
2117 generated_pin = wps_generate_pin();
2118 wpas_prov_disc_local_display(wpa_s, peer, params,
2119 generated_pin);
2120 } else if (config_methods & WPS_CONFIG_KEYPAD)
2121 wpas_prov_disc_local_keypad(wpa_s, peer, params);
2122 else if (config_methods & WPS_CONFIG_PUSHBUTTON)
2123 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_PBC_REQ MACSTR
2124 "%s", MAC2STR(peer), params);
Jouni Malinen75ecf522011-06-27 15:19:46 -07002125
2126 wpas_notify_p2p_provision_discovery(wpa_s, peer, 1 /* request */,
2127 P2P_PROV_DISC_SUCCESS,
2128 config_methods, generated_pin);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002129}
2130
2131
2132void wpas_prov_disc_resp(void *ctx, const u8 *peer, u16 config_methods)
2133{
2134 struct wpa_supplicant *wpa_s = ctx;
2135 unsigned int generated_pin = 0;
Dmitry Shmidt04949592012-07-19 12:16:46 -07002136 char params[20];
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002137
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08002138 if (wpa_s->pending_pd_before_join &&
2139 (os_memcmp(peer, wpa_s->pending_join_dev_addr, ETH_ALEN) == 0 ||
2140 os_memcmp(peer, wpa_s->pending_join_iface_addr, ETH_ALEN) == 0)) {
2141 wpa_s->pending_pd_before_join = 0;
2142 wpa_printf(MSG_DEBUG, "P2P: Starting pending "
2143 "join-existing-group operation");
2144 wpas_p2p_join_start(wpa_s);
2145 return;
2146 }
2147
Dmitry Shmidt04949592012-07-19 12:16:46 -07002148 if (wpa_s->pending_pd_use == AUTO_PD_JOIN ||
2149 wpa_s->pending_pd_use == AUTO_PD_GO_NEG)
2150 os_snprintf(params, sizeof(params), " peer_go=%d",
2151 wpa_s->pending_pd_use == AUTO_PD_JOIN);
2152 else
2153 params[0] = '\0';
2154
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002155 if (config_methods & WPS_CONFIG_DISPLAY)
Dmitry Shmidt04949592012-07-19 12:16:46 -07002156 wpas_prov_disc_local_keypad(wpa_s, peer, params);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002157 else if (config_methods & WPS_CONFIG_KEYPAD) {
2158 generated_pin = wps_generate_pin();
Dmitry Shmidt04949592012-07-19 12:16:46 -07002159 wpas_prov_disc_local_display(wpa_s, peer, params,
2160 generated_pin);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002161 } else if (config_methods & WPS_CONFIG_PUSHBUTTON)
Dmitry Shmidt04949592012-07-19 12:16:46 -07002162 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_PBC_RESP MACSTR
2163 "%s", MAC2STR(peer), params);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002164
Jouni Malinen75ecf522011-06-27 15:19:46 -07002165 wpas_notify_p2p_provision_discovery(wpa_s, peer, 0 /* response */,
2166 P2P_PROV_DISC_SUCCESS,
2167 config_methods, generated_pin);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002168}
2169
2170
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002171static void wpas_prov_disc_fail(void *ctx, const u8 *peer,
2172 enum p2p_prov_disc_status status)
Jouni Malinen75ecf522011-06-27 15:19:46 -07002173{
2174 struct wpa_supplicant *wpa_s = ctx;
2175
Dmitry Shmidt04949592012-07-19 12:16:46 -07002176 if (wpa_s->p2p_fallback_to_go_neg) {
2177 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: PD for p2p_connect-auto "
2178 "failed - fall back to GO Negotiation");
2179 wpas_p2p_fallback_to_go_neg(wpa_s, 0);
2180 return;
2181 }
2182
2183 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_FAILURE
2184 " p2p_dev_addr=" MACSTR " status=%d",
2185 MAC2STR(peer), status);
2186
Jouni Malinen75ecf522011-06-27 15:19:46 -07002187 wpas_notify_p2p_provision_discovery(wpa_s, peer, 0 /* response */,
2188 status, 0, 0);
2189}
2190
2191
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002192static u8 wpas_invitation_process(void *ctx, const u8 *sa, const u8 *bssid,
2193 const u8 *go_dev_addr, const u8 *ssid,
2194 size_t ssid_len, int *go, u8 *group_bssid,
2195 int *force_freq, int persistent_group)
2196{
2197 struct wpa_supplicant *wpa_s = ctx;
2198 struct wpa_ssid *s;
2199 u8 cur_bssid[ETH_ALEN];
2200 int res;
2201 struct wpa_supplicant *grp;
2202
2203 if (!persistent_group) {
2204 wpa_printf(MSG_DEBUG, "P2P: Invitation from " MACSTR
2205 " to join an active group", MAC2STR(sa));
2206 if (!is_zero_ether_addr(wpa_s->p2p_auth_invite) &&
2207 (os_memcmp(go_dev_addr, wpa_s->p2p_auth_invite, ETH_ALEN)
2208 == 0 ||
2209 os_memcmp(sa, wpa_s->p2p_auth_invite, ETH_ALEN) == 0)) {
2210 wpa_printf(MSG_DEBUG, "P2P: Accept previously "
2211 "authorized invitation");
2212 goto accept_inv;
2213 }
2214 /*
2215 * Do not accept the invitation automatically; notify user and
2216 * request approval.
2217 */
2218 return P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE;
2219 }
2220
2221 grp = wpas_get_p2p_group(wpa_s, ssid, ssid_len, go);
2222 if (grp) {
2223 wpa_printf(MSG_DEBUG, "P2P: Accept invitation to already "
2224 "running persistent group");
2225 if (*go)
2226 os_memcpy(group_bssid, grp->own_addr, ETH_ALEN);
2227 goto accept_inv;
2228 }
2229
2230 if (!wpa_s->conf->persistent_reconnect)
2231 return P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE;
2232
2233 for (s = wpa_s->conf->ssid; s; s = s->next) {
2234 if (s->disabled == 2 &&
2235 os_memcmp(s->bssid, go_dev_addr, ETH_ALEN) == 0 &&
2236 s->ssid_len == ssid_len &&
2237 os_memcmp(ssid, s->ssid, ssid_len) == 0)
2238 break;
2239 }
2240
2241 if (!s) {
2242 wpa_printf(MSG_DEBUG, "P2P: Invitation from " MACSTR
2243 " requested reinvocation of an unknown group",
2244 MAC2STR(sa));
2245 return P2P_SC_FAIL_UNKNOWN_GROUP;
2246 }
2247
2248 if (s->mode == WPAS_MODE_P2P_GO && !wpas_p2p_create_iface(wpa_s)) {
2249 *go = 1;
2250 if (wpa_s->wpa_state >= WPA_AUTHENTICATING) {
2251 wpa_printf(MSG_DEBUG, "P2P: The only available "
2252 "interface is already in use - reject "
2253 "invitation");
2254 return P2P_SC_FAIL_UNABLE_TO_ACCOMMODATE;
2255 }
2256 os_memcpy(group_bssid, wpa_s->own_addr, ETH_ALEN);
2257 } else if (s->mode == WPAS_MODE_P2P_GO) {
2258 *go = 1;
2259 if (wpas_p2p_add_group_interface(wpa_s, WPA_IF_P2P_GO) < 0)
2260 {
2261 wpa_printf(MSG_ERROR, "P2P: Failed to allocate a new "
2262 "interface address for the group");
2263 return P2P_SC_FAIL_UNABLE_TO_ACCOMMODATE;
2264 }
2265 os_memcpy(group_bssid, wpa_s->pending_interface_addr,
2266 ETH_ALEN);
2267 }
2268
2269accept_inv:
2270 if (wpa_s->current_ssid && wpa_drv_get_bssid(wpa_s, cur_bssid) == 0 &&
2271 wpa_s->assoc_freq) {
2272 wpa_printf(MSG_DEBUG, "P2P: Trying to force channel to match "
2273 "the channel we are already using");
2274 *force_freq = wpa_s->assoc_freq;
2275 }
2276
2277 res = wpa_drv_shared_freq(wpa_s);
2278 if (res > 0) {
2279 wpa_printf(MSG_DEBUG, "P2P: Trying to force channel to match "
2280 "with the channel we are already using on a "
2281 "shared interface");
2282 *force_freq = res;
2283 }
2284
2285 return P2P_SC_SUCCESS;
2286}
2287
2288
2289static void wpas_invitation_received(void *ctx, const u8 *sa, const u8 *bssid,
2290 const u8 *ssid, size_t ssid_len,
2291 const u8 *go_dev_addr, u8 status,
2292 int op_freq)
2293{
2294 struct wpa_supplicant *wpa_s = ctx;
2295 struct wpa_ssid *s;
2296
2297 for (s = wpa_s->conf->ssid; s; s = s->next) {
2298 if (s->disabled == 2 &&
2299 s->ssid_len == ssid_len &&
2300 os_memcmp(ssid, s->ssid, ssid_len) == 0)
2301 break;
2302 }
2303
2304 if (status == P2P_SC_SUCCESS) {
2305 wpa_printf(MSG_DEBUG, "P2P: Invitation from peer " MACSTR
2306 " was accepted; op_freq=%d MHz",
2307 MAC2STR(sa), op_freq);
2308 if (s) {
2309 wpas_p2p_group_add_persistent(
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002310 wpa_s, s, s->mode == WPAS_MODE_P2P_GO, 0, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002311 } else if (bssid) {
2312 wpas_p2p_join(wpa_s, bssid, go_dev_addr,
Dmitry Shmidt04949592012-07-19 12:16:46 -07002313 wpa_s->p2p_wps_method, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002314 }
2315 return;
2316 }
2317
2318 if (status != P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE) {
2319 wpa_printf(MSG_DEBUG, "P2P: Invitation from peer " MACSTR
2320 " was rejected (status %u)", MAC2STR(sa), status);
2321 return;
2322 }
2323
2324 if (!s) {
2325 if (bssid) {
2326 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_INVITATION_RECEIVED
2327 "sa=" MACSTR " go_dev_addr=" MACSTR
2328 " bssid=" MACSTR " unknown-network",
2329 MAC2STR(sa), MAC2STR(go_dev_addr),
2330 MAC2STR(bssid));
2331 } else {
2332 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_INVITATION_RECEIVED
2333 "sa=" MACSTR " go_dev_addr=" MACSTR
2334 " unknown-network",
2335 MAC2STR(sa), MAC2STR(go_dev_addr));
2336 }
2337 return;
2338 }
2339
2340 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_INVITATION_RECEIVED "sa=" MACSTR
2341 " persistent=%d", MAC2STR(sa), s->id);
2342}
2343
2344
2345static void wpas_invitation_result(void *ctx, int status, const u8 *bssid)
2346{
2347 struct wpa_supplicant *wpa_s = ctx;
2348 struct wpa_ssid *ssid;
2349
2350 if (bssid) {
2351 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_INVITATION_RESULT
2352 "status=%d " MACSTR,
2353 status, MAC2STR(bssid));
2354 } else {
2355 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_INVITATION_RESULT
2356 "status=%d ", status);
2357 }
2358 wpas_notify_p2p_invitation_result(wpa_s, status, bssid);
2359
2360 if (wpa_s->pending_invite_ssid_id == -1)
2361 return; /* Invitation to active group */
2362
2363 if (status != P2P_SC_SUCCESS) {
2364 wpas_p2p_remove_pending_group_interface(wpa_s);
2365 return;
2366 }
2367
2368 ssid = wpa_config_get_network(wpa_s->conf,
2369 wpa_s->pending_invite_ssid_id);
2370 if (ssid == NULL) {
2371 wpa_printf(MSG_ERROR, "P2P: Could not find persistent group "
2372 "data matching with invitation");
2373 return;
2374 }
2375
2376 wpas_p2p_group_add_persistent(wpa_s, ssid,
Jouni Malinen31be0a42012-08-31 21:20:51 +03002377 ssid->mode == WPAS_MODE_P2P_GO,
2378 wpa_s->p2p_persistent_go_freq,
2379 wpa_s->p2p_go_ht40);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002380}
2381
2382
Dmitry Shmidt04949592012-07-19 12:16:46 -07002383static int wpas_p2p_disallowed_freq(struct wpa_global *global,
2384 unsigned int freq)
2385{
2386 unsigned int i;
2387
2388 if (global->p2p_disallow_freq == NULL)
2389 return 0;
2390
2391 for (i = 0; i < global->num_p2p_disallow_freq; i++) {
2392 if (freq >= global->p2p_disallow_freq[i].min &&
2393 freq <= global->p2p_disallow_freq[i].max)
2394 return 1;
2395 }
2396
2397 return 0;
2398}
2399
2400
2401static void wpas_p2p_add_chan(struct p2p_reg_class *reg, u8 chan)
2402{
2403 reg->channel[reg->channels] = chan;
2404 reg->channels++;
2405}
2406
2407
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002408static int wpas_p2p_default_channels(struct wpa_supplicant *wpa_s,
2409 struct p2p_channels *chan)
2410{
2411 int i, cla = 0;
2412
2413 wpa_printf(MSG_DEBUG, "P2P: Enable operating classes for 2.4 GHz "
2414 "band");
2415
2416 /* Operating class 81 - 2.4 GHz band channels 1..13 */
2417 chan->reg_class[cla].reg_class = 81;
Dmitry Shmidt04949592012-07-19 12:16:46 -07002418 chan->reg_class[cla].channels = 0;
2419 for (i = 0; i < 11; i++) {
2420 if (!wpas_p2p_disallowed_freq(wpa_s->global, 2412 + i * 5))
2421 wpas_p2p_add_chan(&chan->reg_class[cla], i + 1);
2422 }
2423 if (chan->reg_class[cla].channels)
2424 cla++;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002425
2426 wpa_printf(MSG_DEBUG, "P2P: Enable operating classes for lower 5 GHz "
2427 "band");
2428
2429 /* Operating class 115 - 5 GHz, channels 36-48 */
2430 chan->reg_class[cla].reg_class = 115;
Dmitry Shmidt04949592012-07-19 12:16:46 -07002431 chan->reg_class[cla].channels = 0;
2432 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 36 * 5))
2433 wpas_p2p_add_chan(&chan->reg_class[cla], 36);
2434 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 40 * 5))
2435 wpas_p2p_add_chan(&chan->reg_class[cla], 40);
2436 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 44 * 5))
2437 wpas_p2p_add_chan(&chan->reg_class[cla], 44);
2438 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 48 * 5))
2439 wpas_p2p_add_chan(&chan->reg_class[cla], 48);
2440 if (chan->reg_class[cla].channels)
2441 cla++;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002442
2443 wpa_printf(MSG_DEBUG, "P2P: Enable operating classes for higher 5 GHz "
2444 "band");
2445
2446 /* Operating class 124 - 5 GHz, channels 149,153,157,161 */
2447 chan->reg_class[cla].reg_class = 124;
Dmitry Shmidt04949592012-07-19 12:16:46 -07002448 chan->reg_class[cla].channels = 0;
2449 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 149 * 5))
2450 wpas_p2p_add_chan(&chan->reg_class[cla], 149);
2451 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 153 * 5))
2452 wpas_p2p_add_chan(&chan->reg_class[cla], 153);
2453 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 156 * 5))
2454 wpas_p2p_add_chan(&chan->reg_class[cla], 157);
2455 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 161 * 5))
2456 wpas_p2p_add_chan(&chan->reg_class[cla], 161);
2457 if (chan->reg_class[cla].channels)
2458 cla++;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002459
2460 chan->reg_classes = cla;
2461 return 0;
2462}
2463
2464
2465static struct hostapd_hw_modes * get_mode(struct hostapd_hw_modes *modes,
2466 u16 num_modes,
2467 enum hostapd_hw_mode mode)
2468{
2469 u16 i;
2470
2471 for (i = 0; i < num_modes; i++) {
2472 if (modes[i].mode == mode)
2473 return &modes[i];
2474 }
2475
2476 return NULL;
2477}
2478
2479
Dmitry Shmidt04949592012-07-19 12:16:46 -07002480static int has_channel(struct wpa_global *global,
2481 struct hostapd_hw_modes *mode, u8 chan, int *flags)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002482{
2483 int i;
Dmitry Shmidt04949592012-07-19 12:16:46 -07002484 unsigned int freq;
2485
2486 freq = (mode->mode == HOSTAPD_MODE_IEEE80211A ? 5000 : 2407) +
2487 chan * 5;
2488 if (wpas_p2p_disallowed_freq(global, freq))
2489 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002490
2491 for (i = 0; i < mode->num_channels; i++) {
2492 if (mode->channels[i].chan == chan) {
2493 if (flags)
2494 *flags = mode->channels[i].flag;
2495 return !(mode->channels[i].flag &
2496 (HOSTAPD_CHAN_DISABLED |
2497 HOSTAPD_CHAN_PASSIVE_SCAN |
2498 HOSTAPD_CHAN_NO_IBSS |
2499 HOSTAPD_CHAN_RADAR));
2500 }
2501 }
2502
2503 return 0;
2504}
2505
2506
2507struct p2p_oper_class_map {
2508 enum hostapd_hw_mode mode;
2509 u8 op_class;
2510 u8 min_chan;
2511 u8 max_chan;
2512 u8 inc;
2513 enum { BW20, BW40PLUS, BW40MINUS } bw;
2514};
2515
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002516static struct p2p_oper_class_map op_class[] = {
2517 { HOSTAPD_MODE_IEEE80211G, 81, 1, 13, 1, BW20 },
2518 { HOSTAPD_MODE_IEEE80211G, 82, 14, 14, 1, BW20 },
2519#if 0 /* Do not enable HT40 on 2 GHz for now */
2520 { HOSTAPD_MODE_IEEE80211G, 83, 1, 9, 1, BW40PLUS },
2521 { HOSTAPD_MODE_IEEE80211G, 84, 5, 13, 1, BW40MINUS },
2522#endif
2523 { HOSTAPD_MODE_IEEE80211A, 115, 36, 48, 4, BW20 },
2524 { HOSTAPD_MODE_IEEE80211A, 124, 149, 161, 4, BW20 },
2525 { HOSTAPD_MODE_IEEE80211A, 116, 36, 44, 8, BW40PLUS },
2526 { HOSTAPD_MODE_IEEE80211A, 117, 40, 48, 8, BW40MINUS },
2527 { HOSTAPD_MODE_IEEE80211A, 126, 149, 157, 8, BW40PLUS },
2528 { HOSTAPD_MODE_IEEE80211A, 127, 153, 161, 8, BW40MINUS },
2529 { -1, 0, 0, 0, 0, BW20 }
2530};
2531
2532
2533static int wpas_p2p_verify_channel(struct wpa_supplicant *wpa_s,
2534 struct hostapd_hw_modes *mode,
2535 u8 channel, u8 bw)
2536{
2537 int flag;
2538
2539 if (!has_channel(wpa_s->global, mode, channel, &flag))
2540 return -1;
2541 if (bw == BW40MINUS &&
2542 (!(flag & HOSTAPD_CHAN_HT40MINUS) ||
2543 !has_channel(wpa_s->global, mode, channel - 4, NULL)))
2544 return 0;
2545 if (bw == BW40PLUS &&
2546 (!(flag & HOSTAPD_CHAN_HT40PLUS) ||
2547 !has_channel(wpa_s->global, mode, channel + 4, NULL)))
2548 return 0;
2549 return 1;
2550}
2551
2552
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002553static int wpas_p2p_setup_channels(struct wpa_supplicant *wpa_s,
2554 struct p2p_channels *chan)
2555{
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002556 struct hostapd_hw_modes *mode;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002557 int cla, op;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002558
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002559 if (wpa_s->hw.modes == NULL) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002560 wpa_printf(MSG_DEBUG, "P2P: Driver did not support fetching "
2561 "of all supported channels; assume dualband "
2562 "support");
2563 return wpas_p2p_default_channels(wpa_s, chan);
2564 }
2565
2566 cla = 0;
2567
2568 for (op = 0; op_class[op].op_class; op++) {
2569 struct p2p_oper_class_map *o = &op_class[op];
2570 u8 ch;
2571 struct p2p_reg_class *reg = NULL;
2572
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002573 mode = get_mode(wpa_s->hw.modes, wpa_s->hw.num_modes, o->mode);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002574 if (mode == NULL)
2575 continue;
2576 for (ch = o->min_chan; ch <= o->max_chan; ch += o->inc) {
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002577 if (wpas_p2p_verify_channel(wpa_s, mode, ch, o->bw) < 1)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002578 continue;
2579 if (reg == NULL) {
2580 wpa_printf(MSG_DEBUG, "P2P: Add operating "
2581 "class %u", o->op_class);
2582 reg = &chan->reg_class[cla];
2583 cla++;
2584 reg->reg_class = o->op_class;
2585 }
2586 reg->channel[reg->channels] = ch;
2587 reg->channels++;
2588 }
2589 if (reg) {
2590 wpa_hexdump(MSG_DEBUG, "P2P: Channels",
2591 reg->channel, reg->channels);
2592 }
2593 }
2594
2595 chan->reg_classes = cla;
2596
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002597 return 0;
2598}
2599
2600
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002601int wpas_p2p_get_ht40_mode(struct wpa_supplicant *wpa_s,
2602 struct hostapd_hw_modes *mode, u8 channel)
2603{
2604 int op, ret;
2605
2606 for (op = 0; op_class[op].op_class; op++) {
2607 struct p2p_oper_class_map *o = &op_class[op];
2608 u8 ch;
2609
2610 for (ch = o->min_chan; ch <= o->max_chan; ch += o->inc) {
2611 if (o->mode != HOSTAPD_MODE_IEEE80211A ||
2612 o->bw == BW20 || ch != channel)
2613 continue;
2614 ret = wpas_p2p_verify_channel(wpa_s, mode, ch, o->bw);
2615 if (ret < 0)
2616 continue;
2617 else if (ret > 0)
2618 return (o->bw == BW40MINUS) ? -1 : 1;
2619 else
2620 return 0;
2621 }
2622 }
2623 return 0;
2624}
2625
2626
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002627static int wpas_get_noa(void *ctx, const u8 *interface_addr, u8 *buf,
2628 size_t buf_len)
2629{
2630 struct wpa_supplicant *wpa_s = ctx;
2631
2632 for (wpa_s = wpa_s->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
2633 if (os_memcmp(wpa_s->own_addr, interface_addr, ETH_ALEN) == 0)
2634 break;
2635 }
2636 if (wpa_s == NULL)
2637 return -1;
2638
2639 return wpa_drv_get_noa(wpa_s, buf, buf_len);
2640}
2641
2642
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08002643static int wpas_go_connected(void *ctx, const u8 *dev_addr)
2644{
2645 struct wpa_supplicant *wpa_s = ctx;
2646
2647 for (wpa_s = wpa_s->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
2648 struct wpa_ssid *ssid = wpa_s->current_ssid;
2649 if (ssid == NULL)
2650 continue;
2651 if (ssid->mode != WPAS_MODE_INFRA)
2652 continue;
2653 if (wpa_s->wpa_state != WPA_COMPLETED &&
2654 wpa_s->wpa_state != WPA_GROUP_HANDSHAKE)
2655 continue;
2656 if (os_memcmp(wpa_s->go_dev_addr, dev_addr, ETH_ALEN) == 0)
2657 return 1;
2658 }
2659
2660 return 0;
2661}
2662
2663
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002664/**
2665 * wpas_p2p_init - Initialize P2P module for %wpa_supplicant
2666 * @global: Pointer to global data from wpa_supplicant_init()
2667 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
2668 * Returns: 0 on success, -1 on failure
2669 */
2670int wpas_p2p_init(struct wpa_global *global, struct wpa_supplicant *wpa_s)
2671{
2672 struct p2p_config p2p;
2673 unsigned int r;
2674 int i;
2675
2676 if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_CAPABLE))
2677 return 0;
2678
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002679 if (global->p2p)
2680 return 0;
2681
2682 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT) {
2683 struct p2p_params params;
2684
2685 wpa_printf(MSG_DEBUG, "P2P: Use driver-based P2P management");
2686 os_memset(&params, 0, sizeof(params));
2687 params.dev_name = wpa_s->conf->device_name;
2688 os_memcpy(params.pri_dev_type, wpa_s->conf->device_type,
2689 WPS_DEV_TYPE_LEN);
2690 params.num_sec_dev_types = wpa_s->conf->num_sec_device_types;
2691 os_memcpy(params.sec_dev_type,
2692 wpa_s->conf->sec_device_type,
2693 params.num_sec_dev_types * WPS_DEV_TYPE_LEN);
2694
2695 if (wpa_drv_p2p_set_params(wpa_s, &params) < 0)
2696 return -1;
2697
2698 return 0;
2699 }
2700
2701 os_memset(&p2p, 0, sizeof(p2p));
2702 p2p.msg_ctx = wpa_s;
2703 p2p.cb_ctx = wpa_s;
2704 p2p.p2p_scan = wpas_p2p_scan;
2705 p2p.send_action = wpas_send_action;
2706 p2p.send_action_done = wpas_send_action_done;
2707 p2p.go_neg_completed = wpas_go_neg_completed;
2708 p2p.go_neg_req_rx = wpas_go_neg_req_rx;
2709 p2p.dev_found = wpas_dev_found;
2710 p2p.dev_lost = wpas_dev_lost;
2711 p2p.start_listen = wpas_start_listen;
2712 p2p.stop_listen = wpas_stop_listen;
2713 p2p.send_probe_resp = wpas_send_probe_resp;
2714 p2p.sd_request = wpas_sd_request;
2715 p2p.sd_response = wpas_sd_response;
2716 p2p.prov_disc_req = wpas_prov_disc_req;
2717 p2p.prov_disc_resp = wpas_prov_disc_resp;
Jouni Malinen75ecf522011-06-27 15:19:46 -07002718 p2p.prov_disc_fail = wpas_prov_disc_fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002719 p2p.invitation_process = wpas_invitation_process;
2720 p2p.invitation_received = wpas_invitation_received;
2721 p2p.invitation_result = wpas_invitation_result;
2722 p2p.get_noa = wpas_get_noa;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08002723 p2p.go_connected = wpas_go_connected;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002724
2725 os_memcpy(wpa_s->global->p2p_dev_addr, wpa_s->own_addr, ETH_ALEN);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002726 os_memcpy(p2p.dev_addr, wpa_s->global->p2p_dev_addr, ETH_ALEN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002727 p2p.dev_name = wpa_s->conf->device_name;
2728 p2p.manufacturer = wpa_s->conf->manufacturer;
2729 p2p.model_name = wpa_s->conf->model_name;
2730 p2p.model_number = wpa_s->conf->model_number;
2731 p2p.serial_number = wpa_s->conf->serial_number;
2732 if (wpa_s->wps) {
2733 os_memcpy(p2p.uuid, wpa_s->wps->uuid, 16);
2734 p2p.config_methods = wpa_s->wps->config_methods;
2735 }
2736
2737 if (wpa_s->conf->p2p_listen_reg_class &&
2738 wpa_s->conf->p2p_listen_channel) {
2739 p2p.reg_class = wpa_s->conf->p2p_listen_reg_class;
2740 p2p.channel = wpa_s->conf->p2p_listen_channel;
2741 } else {
2742 p2p.reg_class = 81;
2743 /*
2744 * Pick one of the social channels randomly as the listen
2745 * channel.
2746 */
2747 os_get_random((u8 *) &r, sizeof(r));
2748 p2p.channel = 1 + (r % 3) * 5;
2749 }
2750 wpa_printf(MSG_DEBUG, "P2P: Own listen channel: %d", p2p.channel);
2751
2752 if (wpa_s->conf->p2p_oper_reg_class &&
2753 wpa_s->conf->p2p_oper_channel) {
2754 p2p.op_reg_class = wpa_s->conf->p2p_oper_reg_class;
2755 p2p.op_channel = wpa_s->conf->p2p_oper_channel;
2756 p2p.cfg_op_channel = 1;
2757 wpa_printf(MSG_DEBUG, "P2P: Configured operating channel: "
2758 "%d:%d", p2p.op_reg_class, p2p.op_channel);
2759
2760 } else {
2761 p2p.op_reg_class = 81;
2762 /*
2763 * Use random operation channel from (1, 6, 11) if no other
2764 * preference is indicated.
2765 */
2766 os_get_random((u8 *) &r, sizeof(r));
2767 p2p.op_channel = 1 + (r % 3) * 5;
2768 p2p.cfg_op_channel = 0;
2769 wpa_printf(MSG_DEBUG, "P2P: Random operating channel: "
2770 "%d:%d", p2p.op_reg_class, p2p.op_channel);
2771 }
2772 if (wpa_s->conf->country[0] && wpa_s->conf->country[1]) {
2773 os_memcpy(p2p.country, wpa_s->conf->country, 2);
2774 p2p.country[2] = 0x04;
2775 } else
2776 os_memcpy(p2p.country, "XX\x04", 3);
2777
2778 if (wpas_p2p_setup_channels(wpa_s, &p2p.channels)) {
2779 wpa_printf(MSG_ERROR, "P2P: Failed to configure supported "
2780 "channel list");
2781 return -1;
2782 }
2783
2784 os_memcpy(p2p.pri_dev_type, wpa_s->conf->device_type,
2785 WPS_DEV_TYPE_LEN);
2786
2787 p2p.num_sec_dev_types = wpa_s->conf->num_sec_device_types;
2788 os_memcpy(p2p.sec_dev_type, wpa_s->conf->sec_device_type,
2789 p2p.num_sec_dev_types * WPS_DEV_TYPE_LEN);
2790
2791 p2p.concurrent_operations = !!(wpa_s->drv_flags &
2792 WPA_DRIVER_FLAGS_P2P_CONCURRENT);
2793
2794 p2p.max_peers = 100;
2795
2796 if (wpa_s->conf->p2p_ssid_postfix) {
2797 p2p.ssid_postfix_len =
2798 os_strlen(wpa_s->conf->p2p_ssid_postfix);
2799 if (p2p.ssid_postfix_len > sizeof(p2p.ssid_postfix))
2800 p2p.ssid_postfix_len = sizeof(p2p.ssid_postfix);
2801 os_memcpy(p2p.ssid_postfix, wpa_s->conf->p2p_ssid_postfix,
2802 p2p.ssid_postfix_len);
2803 }
2804
2805 p2p.p2p_intra_bss = wpa_s->conf->p2p_intra_bss;
2806
Dmitry Shmidt04abaa92012-08-17 16:22:31 -07002807#ifdef ANDROID_P2P
2808 if(wpa_s->drv_flags & WPA_DRIVER_FLAGS_MULTI_CHANNEL_CONCURRENT)
2809 p2p.p2p_concurrency = P2P_MULTI_CHANNEL_CONCURRENT;
2810 else if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_CONCURRENT)
2811 p2p.p2p_concurrency = P2P_SINGLE_CHANNEL_CONCURRENT;
2812#endif
2813
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002814 global->p2p = p2p_init(&p2p);
2815 if (global->p2p == NULL)
2816 return -1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07002817 global->p2p_init_wpa_s = wpa_s;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002818
2819 for (i = 0; i < MAX_WPS_VENDOR_EXT; i++) {
2820 if (wpa_s->conf->wps_vendor_ext[i] == NULL)
2821 continue;
2822 p2p_add_wps_vendor_extension(
2823 global->p2p, wpa_s->conf->wps_vendor_ext[i]);
2824 }
2825
2826 return 0;
2827}
2828
2829
2830/**
2831 * wpas_p2p_deinit - Deinitialize per-interface P2P data
2832 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
2833 *
2834 * This function deinitialize per-interface P2P data.
2835 */
2836void wpas_p2p_deinit(struct wpa_supplicant *wpa_s)
2837{
2838 if (wpa_s->driver && wpa_s->drv_priv)
2839 wpa_drv_probe_req_report(wpa_s, 0);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002840
2841 if (wpa_s->go_params) {
2842 /* Clear any stored provisioning info */
2843 p2p_clear_provisioning_info(
2844 wpa_s->global->p2p,
Dmitry Shmidt04949592012-07-19 12:16:46 -07002845 wpa_s->go_params->peer_device_addr);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002846 }
2847
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002848 os_free(wpa_s->go_params);
2849 wpa_s->go_params = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002850 eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s, NULL);
2851 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08002852 eloop_cancel_timeout(wpas_p2p_pd_before_join_timeout, wpa_s, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002853 wpa_s->p2p_long_listen = 0;
2854 eloop_cancel_timeout(wpas_p2p_long_listen_timeout, wpa_s, NULL);
2855 eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL);
2856 wpas_p2p_remove_pending_group_interface(wpa_s);
2857
2858 /* TODO: remove group interface from the driver if this wpa_s instance
2859 * is on top of a P2P group interface */
2860}
2861
2862
2863/**
2864 * wpas_p2p_deinit_global - Deinitialize global P2P module
2865 * @global: Pointer to global data from wpa_supplicant_init()
2866 *
2867 * This function deinitializes the global (per device) P2P module.
2868 */
2869void wpas_p2p_deinit_global(struct wpa_global *global)
2870{
2871 struct wpa_supplicant *wpa_s, *tmp;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002872
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002873 wpa_s = global->ifaces;
2874 if (wpa_s)
2875 wpas_p2p_service_flush(wpa_s);
2876
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002877 if (global->p2p == NULL)
2878 return;
2879
2880 /* Remove remaining P2P group interfaces */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002881 while (wpa_s && wpa_s->p2p_group_interface != NOT_P2P_GROUP_INTERFACE)
2882 wpa_s = wpa_s->next;
2883 while (wpa_s) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002884 tmp = global->ifaces;
2885 while (tmp &&
2886 (tmp == wpa_s ||
2887 tmp->p2p_group_interface == NOT_P2P_GROUP_INTERFACE)) {
2888 tmp = tmp->next;
2889 }
2890 if (tmp == NULL)
2891 break;
Dmitry Shmidt04949592012-07-19 12:16:46 -07002892 /* Disconnect from the P2P group and deinit the interface */
2893 wpas_p2p_disconnect(tmp);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002894 }
2895
2896 /*
2897 * Deinit GO data on any possibly remaining interface (if main
2898 * interface is used as GO).
2899 */
2900 for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
2901 if (wpa_s->ap_iface)
2902 wpas_p2p_group_deinit(wpa_s);
2903 }
2904
2905 p2p_deinit(global->p2p);
2906 global->p2p = NULL;
Dmitry Shmidt04949592012-07-19 12:16:46 -07002907 global->p2p_init_wpa_s = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002908}
2909
2910
2911static int wpas_p2p_create_iface(struct wpa_supplicant *wpa_s)
2912{
2913 if (wpa_s->drv_flags &
2914 (WPA_DRIVER_FLAGS_P2P_DEDICATED_INTERFACE |
2915 WPA_DRIVER_FLAGS_P2P_MGMT_AND_NON_P2P))
2916 return 1; /* P2P group requires a new interface in every case
2917 */
2918 if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_CONCURRENT))
2919 return 0; /* driver does not support concurrent operations */
2920 if (wpa_s->global->ifaces->next)
2921 return 1; /* more that one interface already in use */
2922 if (wpa_s->wpa_state >= WPA_AUTHENTICATING)
2923 return 1; /* this interface is already in use */
2924 return 0;
2925}
2926
2927
2928static int wpas_p2p_start_go_neg(struct wpa_supplicant *wpa_s,
2929 const u8 *peer_addr,
2930 enum p2p_wps_method wps_method,
2931 int go_intent, const u8 *own_interface_addr,
Dmitry Shmidt04949592012-07-19 12:16:46 -07002932 unsigned int force_freq, int persistent_group,
2933 struct wpa_ssid *ssid)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002934{
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002935 if (persistent_group && wpa_s->conf->persistent_reconnect)
2936 persistent_group = 2;
2937
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002938 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT) {
2939 return wpa_drv_p2p_connect(wpa_s, peer_addr, wps_method,
2940 go_intent, own_interface_addr,
2941 force_freq, persistent_group);
2942 }
2943
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002944 /*
2945 * Increase GO config timeout if HT40 is used since it takes some time
2946 * to scan channels for coex purposes before the BSS can be started.
2947 */
2948 p2p_set_config_timeout(wpa_s->global->p2p,
2949 wpa_s->p2p_go_ht40 ? 255 : 100, 20);
2950
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002951 return p2p_connect(wpa_s->global->p2p, peer_addr, wps_method,
2952 go_intent, own_interface_addr, force_freq,
Dmitry Shmidt04949592012-07-19 12:16:46 -07002953 persistent_group, ssid ? ssid->ssid : NULL,
2954 ssid ? ssid->ssid_len : 0,
2955 wpa_s->p2p_pd_before_go_neg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002956}
2957
2958
2959static int wpas_p2p_auth_go_neg(struct wpa_supplicant *wpa_s,
2960 const u8 *peer_addr,
2961 enum p2p_wps_method wps_method,
2962 int go_intent, const u8 *own_interface_addr,
Dmitry Shmidt04949592012-07-19 12:16:46 -07002963 unsigned int force_freq, int persistent_group,
2964 struct wpa_ssid *ssid)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002965{
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002966 if (persistent_group && wpa_s->conf->persistent_reconnect)
2967 persistent_group = 2;
2968
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002969 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
2970 return -1;
2971
2972 return p2p_authorize(wpa_s->global->p2p, peer_addr, wps_method,
2973 go_intent, own_interface_addr, force_freq,
Dmitry Shmidt04949592012-07-19 12:16:46 -07002974 persistent_group, ssid ? ssid->ssid : NULL,
2975 ssid ? ssid->ssid_len : 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002976}
2977
2978
2979static void wpas_p2p_check_join_scan_limit(struct wpa_supplicant *wpa_s)
2980{
2981 wpa_s->p2p_join_scan_count++;
2982 wpa_printf(MSG_DEBUG, "P2P: Join scan attempt %d",
2983 wpa_s->p2p_join_scan_count);
2984 if (wpa_s->p2p_join_scan_count > P2P_MAX_JOIN_SCAN_ATTEMPTS) {
2985 wpa_printf(MSG_DEBUG, "P2P: Failed to find GO " MACSTR
2986 " for join operationg - stop join attempt",
2987 MAC2STR(wpa_s->pending_join_iface_addr));
2988 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
Dmitry Shmidt04949592012-07-19 12:16:46 -07002989 if (wpa_s->p2p_auto_pd) {
2990 wpa_s->p2p_auto_pd = 0;
2991 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_FAILURE
2992 " p2p_dev_addr=" MACSTR " status=N/A",
2993 MAC2STR(wpa_s->pending_join_dev_addr));
2994 return;
2995 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002996 wpa_msg(wpa_s->parent, MSG_INFO,
2997 P2P_EVENT_GROUP_FORMATION_FAILURE);
2998 }
2999}
3000
3001
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08003002static void wpas_p2p_pd_before_join_timeout(void *eloop_ctx, void *timeout_ctx)
3003{
3004 struct wpa_supplicant *wpa_s = eloop_ctx;
3005 if (!wpa_s->pending_pd_before_join)
3006 return;
3007 /*
3008 * Provision Discovery Response may have been lost - try to connect
3009 * anyway since we do not need any information from this PD.
3010 */
3011 wpa_printf(MSG_DEBUG, "P2P: PD timeout for join-existing-group - "
3012 "try to connect anyway");
3013 wpas_p2p_join_start(wpa_s);
3014}
3015
3016
Dmitry Shmidt04949592012-07-19 12:16:46 -07003017static int wpas_check_freq_conflict(struct wpa_supplicant *wpa_s, int freq)
3018{
3019 struct wpa_supplicant *iface;
3020 int shared_freq;
3021 u8 bssid[ETH_ALEN];
3022
3023 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_MULTI_CHANNEL_CONCURRENT)
3024 return 0;
3025
3026 for (iface = wpa_s->global->ifaces; iface; iface = iface->next) {
3027 if (!wpas_p2p_create_iface(wpa_s) && iface == wpa_s)
3028 continue;
3029 if (iface->current_ssid == NULL || iface->assoc_freq == 0)
3030 continue;
3031 if (iface->current_ssid->mode == WPAS_MODE_AP ||
3032 iface->current_ssid->mode == WPAS_MODE_P2P_GO)
3033 shared_freq = iface->current_ssid->frequency;
3034 else if (wpa_drv_get_bssid(iface, bssid) == 0)
3035 shared_freq = iface->assoc_freq;
3036 else
3037 shared_freq = 0;
3038
3039 if (shared_freq && freq != shared_freq) {
3040 wpa_printf(MSG_DEBUG, "P2P: Frequency conflict - %s "
3041 "connected on %d MHz - new connection on "
3042 "%d MHz", iface->ifname, shared_freq, freq);
3043 return 1;
3044 }
3045 }
3046
3047 shared_freq = wpa_drv_shared_freq(wpa_s);
3048 if (shared_freq > 0 && shared_freq != freq) {
3049 wpa_printf(MSG_DEBUG, "P2P: Frequency conflict - shared "
3050 "virtual interface connected on %d MHz - new "
3051 "connection on %d MHz", shared_freq, freq);
3052 return 1;
3053 }
3054
3055 return 0;
3056}
3057
3058
3059static int wpas_p2p_peer_go(struct wpa_supplicant *wpa_s,
3060 const u8 *peer_dev_addr)
3061{
3062 struct wpa_bss *bss;
3063 int updated;
3064
3065 bss = wpa_bss_get_p2p_dev_addr(wpa_s, peer_dev_addr);
3066 if (bss == NULL)
3067 return -1;
3068 if (bss->last_update_idx < wpa_s->bss_update_idx) {
3069 wpa_printf(MSG_DEBUG, "P2P: Peer BSS entry not updated in the "
3070 "last scan");
3071 return 0;
3072 }
3073
3074 updated = os_time_before(&wpa_s->p2p_auto_started, &bss->last_update);
3075 wpa_printf(MSG_DEBUG, "P2P: Current BSS entry for peer updated at "
3076 "%ld.%06ld (%supdated in last scan)",
3077 bss->last_update.sec, bss->last_update.usec,
3078 updated ? "": "not ");
3079
3080 return updated;
3081}
3082
3083
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003084static void wpas_p2p_scan_res_join(struct wpa_supplicant *wpa_s,
3085 struct wpa_scan_results *scan_res)
3086{
3087 struct wpa_bss *bss;
3088 int freq;
3089 u8 iface_addr[ETH_ALEN];
Dmitry Shmidt04949592012-07-19 12:16:46 -07003090
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003091 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
3092
3093 if (wpa_s->global->p2p_disabled)
3094 return;
3095
Dmitry Shmidt04949592012-07-19 12:16:46 -07003096 wpa_printf(MSG_DEBUG, "P2P: Scan results received (%d BSS) for %sjoin",
3097 scan_res ? (int) scan_res->num : -1,
3098 wpa_s->p2p_auto_join ? "auto_" : "");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003099
3100 if (scan_res)
3101 wpas_p2p_scan_res_handler(wpa_s, scan_res);
3102
Dmitry Shmidt04949592012-07-19 12:16:46 -07003103 if (wpa_s->p2p_auto_pd) {
3104 int join = wpas_p2p_peer_go(wpa_s,
3105 wpa_s->pending_join_dev_addr);
3106 if (join == 0 &&
3107 wpa_s->auto_pd_scan_retry < P2P_AUTO_PD_SCAN_ATTEMPTS) {
3108 wpa_s->auto_pd_scan_retry++;
3109 bss = wpa_bss_get_bssid(wpa_s,
3110 wpa_s->pending_join_dev_addr);
3111 if (bss) {
3112 freq = bss->freq;
3113 wpa_printf(MSG_DEBUG, "P2P: Scan retry %d for "
3114 "the peer " MACSTR " at %d MHz",
3115 wpa_s->auto_pd_scan_retry,
3116 MAC2STR(wpa_s->
3117 pending_join_dev_addr),
3118 freq);
3119 wpas_p2p_join_scan_req(wpa_s, freq);
3120 return;
3121 }
3122 }
3123
3124 if (join < 0)
3125 join = 0;
3126
3127 wpa_s->p2p_auto_pd = 0;
3128 wpa_s->pending_pd_use = join ? AUTO_PD_JOIN : AUTO_PD_GO_NEG;
3129 wpa_printf(MSG_DEBUG, "P2P: Auto PD with " MACSTR " join=%d",
3130 MAC2STR(wpa_s->pending_join_dev_addr), join);
3131 if (p2p_prov_disc_req(wpa_s->global->p2p,
3132 wpa_s->pending_join_dev_addr,
3133 wpa_s->pending_pd_config_methods, join,
3134 0) < 0) {
3135 wpa_s->p2p_auto_pd = 0;
3136 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_FAILURE
3137 " p2p_dev_addr=" MACSTR " status=N/A",
3138 MAC2STR(wpa_s->pending_join_dev_addr));
3139 }
3140 return;
3141 }
3142
3143 if (wpa_s->p2p_auto_join) {
3144 int join = wpas_p2p_peer_go(wpa_s,
3145 wpa_s->pending_join_dev_addr);
3146 if (join < 0) {
3147 wpa_printf(MSG_DEBUG, "P2P: Peer was not found to be "
3148 "running a GO -> use GO Negotiation");
3149 wpas_p2p_connect(wpa_s, wpa_s->pending_join_dev_addr,
3150 wpa_s->p2p_pin, wpa_s->p2p_wps_method,
3151 wpa_s->p2p_persistent_group, 0, 0, 0,
3152 wpa_s->p2p_go_intent,
3153 wpa_s->p2p_connect_freq,
3154 wpa_s->p2p_persistent_id,
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003155 wpa_s->p2p_pd_before_go_neg,
3156 wpa_s->p2p_go_ht40);
Dmitry Shmidt04949592012-07-19 12:16:46 -07003157 return;
3158 }
3159
3160 wpa_printf(MSG_DEBUG, "P2P: Peer was found running GO%s -> "
3161 "try to join the group", join ? "" :
3162 " in older scan");
3163 if (!join)
3164 wpa_s->p2p_fallback_to_go_neg = 1;
3165 }
3166
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003167 freq = p2p_get_oper_freq(wpa_s->global->p2p,
3168 wpa_s->pending_join_iface_addr);
3169 if (freq < 0 &&
3170 p2p_get_interface_addr(wpa_s->global->p2p,
3171 wpa_s->pending_join_dev_addr,
3172 iface_addr) == 0 &&
3173 os_memcmp(iface_addr, wpa_s->pending_join_dev_addr, ETH_ALEN) != 0)
3174 {
3175 wpa_printf(MSG_DEBUG, "P2P: Overwrite pending interface "
3176 "address for join from " MACSTR " to " MACSTR
3177 " based on newly discovered P2P peer entry",
3178 MAC2STR(wpa_s->pending_join_iface_addr),
3179 MAC2STR(iface_addr));
3180 os_memcpy(wpa_s->pending_join_iface_addr, iface_addr,
3181 ETH_ALEN);
3182
3183 freq = p2p_get_oper_freq(wpa_s->global->p2p,
3184 wpa_s->pending_join_iface_addr);
3185 }
3186 if (freq >= 0) {
3187 wpa_printf(MSG_DEBUG, "P2P: Target GO operating frequency "
3188 "from P2P peer table: %d MHz", freq);
3189 }
3190 bss = wpa_bss_get_bssid(wpa_s, wpa_s->pending_join_iface_addr);
3191 if (bss) {
3192 freq = bss->freq;
3193 wpa_printf(MSG_DEBUG, "P2P: Target GO operating frequency "
3194 "from BSS table: %d MHz", freq);
3195 }
3196 if (freq > 0) {
3197 u16 method;
3198
Dmitry Shmidt04949592012-07-19 12:16:46 -07003199 if (wpas_check_freq_conflict(wpa_s, freq) > 0) {
3200 wpa_msg(wpa_s->parent, MSG_INFO,
3201 P2P_EVENT_GROUP_FORMATION_FAILURE
3202 "reason=FREQ_CONFLICT");
3203 return;
3204 }
3205
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003206 wpa_printf(MSG_DEBUG, "P2P: Send Provision Discovery Request "
3207 "prior to joining an existing group (GO " MACSTR
3208 " freq=%u MHz)",
3209 MAC2STR(wpa_s->pending_join_dev_addr), freq);
3210 wpa_s->pending_pd_before_join = 1;
3211
3212 switch (wpa_s->pending_join_wps_method) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003213 case WPS_PIN_DISPLAY:
3214 method = WPS_CONFIG_KEYPAD;
3215 break;
3216 case WPS_PIN_KEYPAD:
3217 method = WPS_CONFIG_DISPLAY;
3218 break;
3219 case WPS_PBC:
3220 method = WPS_CONFIG_PUSHBUTTON;
3221 break;
3222 default:
3223 method = 0;
3224 break;
3225 }
3226
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003227 if ((p2p_get_provisioning_info(wpa_s->global->p2p,
3228 wpa_s->pending_join_dev_addr) ==
3229 method)) {
3230 /*
3231 * We have already performed provision discovery for
3232 * joining the group. Proceed directly to join
3233 * operation without duplicated provision discovery. */
3234 wpa_printf(MSG_DEBUG, "P2P: Provision discovery "
3235 "with " MACSTR " already done - proceed to "
3236 "join",
3237 MAC2STR(wpa_s->pending_join_dev_addr));
3238 wpa_s->pending_pd_before_join = 0;
3239 goto start;
3240 }
3241
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003242 if (p2p_prov_disc_req(wpa_s->global->p2p,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003243 wpa_s->pending_join_dev_addr, method, 1,
3244 freq) < 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003245 wpa_printf(MSG_DEBUG, "P2P: Failed to send Provision "
3246 "Discovery Request before joining an "
3247 "existing group");
3248 wpa_s->pending_pd_before_join = 0;
3249 goto start;
3250 }
3251
3252 /*
3253 * Actual join operation will be started from the Action frame
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08003254 * TX status callback (if no ACK is received) or when the
3255 * Provision Discovery Response is received. Use a short
3256 * timeout as a backup mechanism should the Provision Discovery
3257 * Response be lost for any reason.
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003258 */
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08003259 eloop_cancel_timeout(wpas_p2p_pd_before_join_timeout, wpa_s,
3260 NULL);
3261 eloop_register_timeout(2, 0, wpas_p2p_pd_before_join_timeout,
3262 wpa_s, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003263 return;
3264 }
3265
3266 wpa_printf(MSG_DEBUG, "P2P: Failed to find BSS/GO - try again later");
3267 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
3268 eloop_register_timeout(1, 0, wpas_p2p_join_scan, wpa_s, NULL);
3269 wpas_p2p_check_join_scan_limit(wpa_s);
3270 return;
3271
3272start:
3273 /* Start join operation immediately */
3274 wpas_p2p_join_start(wpa_s);
3275}
3276
3277
Dmitry Shmidt04949592012-07-19 12:16:46 -07003278static void wpas_p2p_join_scan_req(struct wpa_supplicant *wpa_s, int freq)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003279{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003280 int ret;
3281 struct wpa_driver_scan_params params;
3282 struct wpabuf *wps_ie, *ies;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003283 size_t ielen;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003284 int freqs[2] = { 0, 0 };
Dmitry Shmidtad266fb2012-08-24 17:03:35 -07003285#ifdef ANDROID_P2P
3286 int oper_freq;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003287
Dmitry Shmidtad266fb2012-08-24 17:03:35 -07003288 /* If freq is not provided, check the operating freq of the GO and do a
3289 * a directed scan to save time
3290 */
3291 if(!freq) {
3292 freq = (oper_freq = p2p_get_oper_freq(wpa_s->global->p2p,
3293 wpa_s->pending_join_iface_addr) == -1) ? 0 : oper_freq;
3294 }
3295#endif
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003296 os_memset(&params, 0, sizeof(params));
3297
3298 /* P2P Wildcard SSID */
3299 params.num_ssids = 1;
3300 params.ssids[0].ssid = (u8 *) P2P_WILDCARD_SSID;
3301 params.ssids[0].ssid_len = P2P_WILDCARD_SSID_LEN;
3302
3303 wpa_s->wps->dev.p2p = 1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003304 wps_ie = wps_build_probe_req_ie(DEV_PW_DEFAULT, &wpa_s->wps->dev,
3305 wpa_s->wps->uuid, WPS_REQ_ENROLLEE, 0,
3306 NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003307 if (wps_ie == NULL) {
3308 wpas_p2p_scan_res_join(wpa_s, NULL);
3309 return;
3310 }
3311
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003312 ielen = p2p_scan_ie_buf_len(wpa_s->global->p2p);
3313 ies = wpabuf_alloc(wpabuf_len(wps_ie) + ielen);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003314 if (ies == NULL) {
3315 wpabuf_free(wps_ie);
3316 wpas_p2p_scan_res_join(wpa_s, NULL);
3317 return;
3318 }
3319 wpabuf_put_buf(ies, wps_ie);
3320 wpabuf_free(wps_ie);
3321
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08003322 p2p_scan_ie(wpa_s->global->p2p, ies, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003323
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003324 params.p2p_probe = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003325 params.extra_ies = wpabuf_head(ies);
3326 params.extra_ies_len = wpabuf_len(ies);
Dmitry Shmidt04949592012-07-19 12:16:46 -07003327 if (freq > 0) {
3328 freqs[0] = freq;
3329 params.freqs = freqs;
3330 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003331
3332 /*
3333 * Run a scan to update BSS table and start Provision Discovery once
3334 * the new scan results become available.
3335 */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003336 ret = wpa_drv_scan(wpa_s, &params);
Dmitry Shmidt04949592012-07-19 12:16:46 -07003337 if (!ret)
3338 wpa_s->scan_res_handler = wpas_p2p_scan_res_join;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003339
3340 wpabuf_free(ies);
3341
3342 if (ret) {
3343 wpa_printf(MSG_DEBUG, "P2P: Failed to start scan for join - "
3344 "try again later");
3345 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
3346 eloop_register_timeout(1, 0, wpas_p2p_join_scan, wpa_s, NULL);
3347 wpas_p2p_check_join_scan_limit(wpa_s);
3348 }
3349}
3350
3351
Dmitry Shmidt04949592012-07-19 12:16:46 -07003352static void wpas_p2p_join_scan(void *eloop_ctx, void *timeout_ctx)
3353{
3354 struct wpa_supplicant *wpa_s = eloop_ctx;
3355 wpas_p2p_join_scan_req(wpa_s, 0);
3356}
3357
3358
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003359static int wpas_p2p_join(struct wpa_supplicant *wpa_s, const u8 *iface_addr,
Dmitry Shmidt04949592012-07-19 12:16:46 -07003360 const u8 *dev_addr, enum p2p_wps_method wps_method,
3361 int auto_join)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003362{
3363 wpa_printf(MSG_DEBUG, "P2P: Request to join existing group (iface "
Dmitry Shmidt04949592012-07-19 12:16:46 -07003364 MACSTR " dev " MACSTR ")%s",
3365 MAC2STR(iface_addr), MAC2STR(dev_addr),
3366 auto_join ? " (auto_join)" : "");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003367
Dmitry Shmidt04949592012-07-19 12:16:46 -07003368 wpa_s->p2p_auto_pd = 0;
3369 wpa_s->p2p_auto_join = !!auto_join;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003370 os_memcpy(wpa_s->pending_join_iface_addr, iface_addr, ETH_ALEN);
3371 os_memcpy(wpa_s->pending_join_dev_addr, dev_addr, ETH_ALEN);
3372 wpa_s->pending_join_wps_method = wps_method;
3373
3374 /* Make sure we are not running find during connection establishment */
3375 wpas_p2p_stop_find(wpa_s);
3376
3377 wpa_s->p2p_join_scan_count = 0;
3378 wpas_p2p_join_scan(wpa_s, NULL);
3379 return 0;
3380}
3381
3382
3383static int wpas_p2p_join_start(struct wpa_supplicant *wpa_s)
3384{
3385 struct wpa_supplicant *group;
3386 struct p2p_go_neg_results res;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003387 struct wpa_bss *bss;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003388
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08003389 eloop_cancel_timeout(wpas_p2p_pd_before_join_timeout, wpa_s, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003390 group = wpas_p2p_get_group_iface(wpa_s, 0, 0);
3391 if (group == NULL)
3392 return -1;
3393 if (group != wpa_s) {
3394 os_memcpy(group->p2p_pin, wpa_s->p2p_pin,
3395 sizeof(group->p2p_pin));
3396 group->p2p_wps_method = wpa_s->p2p_wps_method;
3397 }
3398
3399 group->p2p_in_provisioning = 1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003400 group->p2p_fallback_to_go_neg = wpa_s->p2p_fallback_to_go_neg;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003401
3402 os_memset(&res, 0, sizeof(res));
3403 os_memcpy(res.peer_interface_addr, wpa_s->pending_join_iface_addr,
3404 ETH_ALEN);
3405 res.wps_method = wpa_s->pending_join_wps_method;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003406 bss = wpa_bss_get_bssid(wpa_s, wpa_s->pending_join_iface_addr);
3407 if (bss) {
3408 res.freq = bss->freq;
3409 res.ssid_len = bss->ssid_len;
3410 os_memcpy(res.ssid, bss->ssid, bss->ssid_len);
3411 }
3412
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08003413 if (wpa_s->off_channel_freq || wpa_s->roc_waiting_drv_freq) {
3414 wpa_printf(MSG_DEBUG, "P2P: Cancel remain-on-channel prior to "
3415 "starting client");
3416 wpa_drv_cancel_remain_on_channel(wpa_s);
3417 wpa_s->off_channel_freq = 0;
3418 wpa_s->roc_waiting_drv_freq = 0;
3419 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003420 wpas_start_wps_enrollee(group, &res);
3421
3422 /*
3423 * Allow a longer timeout for join-a-running-group than normal 15
3424 * second group formation timeout since the GO may not have authorized
3425 * our connection yet.
3426 */
3427 eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s, NULL);
3428 eloop_register_timeout(60, 0, wpas_p2p_group_formation_timeout,
3429 wpa_s, NULL);
3430
3431 return 0;
3432}
3433
3434
3435/**
3436 * wpas_p2p_connect - Request P2P Group Formation to be started
3437 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
3438 * @peer_addr: Address of the peer P2P Device
3439 * @pin: PIN to use during provisioning or %NULL to indicate PBC mode
3440 * @persistent_group: Whether to create a persistent group
Dmitry Shmidt04949592012-07-19 12:16:46 -07003441 * @auto_join: Whether to select join vs. GO Negotiation automatically
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003442 * @join: Whether to join an existing group (as a client) instead of starting
3443 * Group Owner negotiation; @peer_addr is BSSID in that case
3444 * @auth: Whether to only authorize the connection instead of doing that and
3445 * initiating Group Owner negotiation
3446 * @go_intent: GO Intent or -1 to use default
3447 * @freq: Frequency for the group or 0 for auto-selection
Dmitry Shmidt04949592012-07-19 12:16:46 -07003448 * @persistent_id: Persistent group credentials to use for forcing GO
3449 * parameters or -1 to generate new values (SSID/passphrase)
3450 * @pd: Whether to send Provision Discovery prior to GO Negotiation as an
3451 * interoperability workaround when initiating group formation
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003452 * @ht40: Start GO with 40 MHz channel width
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003453 * Returns: 0 or new PIN (if pin was %NULL) on success, -1 on unspecified
3454 * failure, -2 on failure due to channel not currently available,
3455 * -3 if forced channel is not supported
3456 */
3457int wpas_p2p_connect(struct wpa_supplicant *wpa_s, const u8 *peer_addr,
3458 const char *pin, enum p2p_wps_method wps_method,
Dmitry Shmidt04949592012-07-19 12:16:46 -07003459 int persistent_group, int auto_join, int join, int auth,
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003460 int go_intent, int freq, int persistent_id, int pd,
3461 int ht40)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003462{
3463 int force_freq = 0, oper_freq = 0;
3464 u8 bssid[ETH_ALEN];
3465 int ret = 0;
3466 enum wpa_driver_if_type iftype;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003467 const u8 *if_addr;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003468 struct wpa_ssid *ssid = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003469
3470 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
3471 return -1;
3472
Dmitry Shmidt04949592012-07-19 12:16:46 -07003473 if (persistent_id >= 0) {
3474 ssid = wpa_config_get_network(wpa_s->conf, persistent_id);
3475 if (ssid == NULL || ssid->disabled != 2 ||
3476 ssid->mode != WPAS_MODE_P2P_GO)
3477 return -1;
3478 }
3479
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003480 if (go_intent < 0)
3481 go_intent = wpa_s->conf->p2p_go_intent;
3482
3483 if (!auth)
3484 wpa_s->p2p_long_listen = 0;
3485
3486 wpa_s->p2p_wps_method = wps_method;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003487 wpa_s->p2p_persistent_group = !!persistent_group;
3488 wpa_s->p2p_persistent_id = persistent_id;
3489 wpa_s->p2p_go_intent = go_intent;
3490 wpa_s->p2p_connect_freq = freq;
3491 wpa_s->p2p_fallback_to_go_neg = 0;
3492 wpa_s->p2p_pd_before_go_neg = !!pd;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003493 wpa_s->p2p_go_ht40 = !!ht40;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003494
3495 if (pin)
3496 os_strlcpy(wpa_s->p2p_pin, pin, sizeof(wpa_s->p2p_pin));
3497 else if (wps_method == WPS_PIN_DISPLAY) {
3498 ret = wps_generate_pin();
3499 os_snprintf(wpa_s->p2p_pin, sizeof(wpa_s->p2p_pin), "%08d",
3500 ret);
3501 wpa_printf(MSG_DEBUG, "P2P: Randomly generated PIN: %s",
3502 wpa_s->p2p_pin);
3503 } else
3504 wpa_s->p2p_pin[0] = '\0';
3505
Dmitry Shmidt04949592012-07-19 12:16:46 -07003506 if (join || auto_join) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003507 u8 iface_addr[ETH_ALEN], dev_addr[ETH_ALEN];
3508 if (auth) {
3509 wpa_printf(MSG_DEBUG, "P2P: Authorize invitation to "
3510 "connect a running group from " MACSTR,
3511 MAC2STR(peer_addr));
3512 os_memcpy(wpa_s->p2p_auth_invite, peer_addr, ETH_ALEN);
3513 return ret;
3514 }
3515 os_memcpy(dev_addr, peer_addr, ETH_ALEN);
3516 if (p2p_get_interface_addr(wpa_s->global->p2p, peer_addr,
3517 iface_addr) < 0) {
3518 os_memcpy(iface_addr, peer_addr, ETH_ALEN);
3519 p2p_get_dev_addr(wpa_s->global->p2p, peer_addr,
3520 dev_addr);
3521 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07003522 if (auto_join) {
3523 os_get_time(&wpa_s->p2p_auto_started);
3524 wpa_printf(MSG_DEBUG, "P2P: Auto join started at "
3525 "%ld.%06ld",
3526 wpa_s->p2p_auto_started.sec,
3527 wpa_s->p2p_auto_started.usec);
3528 }
3529 if (wpas_p2p_join(wpa_s, iface_addr, dev_addr, wps_method,
3530 auto_join) < 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003531 return -1;
3532 return ret;
3533 }
3534
3535 if (wpa_s->current_ssid && wpa_drv_get_bssid(wpa_s, bssid) == 0 &&
3536 wpa_s->assoc_freq)
3537 oper_freq = wpa_s->assoc_freq;
3538 else {
3539 oper_freq = wpa_drv_shared_freq(wpa_s);
3540 if (oper_freq < 0)
3541 oper_freq = 0;
3542 }
3543
3544 if (freq > 0) {
3545 if (!p2p_supported_freq(wpa_s->global->p2p, freq)) {
3546 wpa_printf(MSG_DEBUG, "P2P: The forced channel "
3547 "(%u MHz) is not supported for P2P uses",
3548 freq);
3549 return -3;
3550 }
3551
3552 if (oper_freq > 0 && freq != oper_freq &&
3553 !(wpa_s->drv_flags &
3554 WPA_DRIVER_FLAGS_MULTI_CHANNEL_CONCURRENT)) {
3555 wpa_printf(MSG_DEBUG, "P2P: Cannot start P2P group "
3556 "on %u MHz while connected on another "
3557 "channel (%u MHz)", freq, oper_freq);
3558 return -2;
3559 }
3560 wpa_printf(MSG_DEBUG, "P2P: Trying to force us to use the "
3561 "requested channel (%u MHz)", freq);
3562 force_freq = freq;
3563 } else if (oper_freq > 0 &&
3564 !p2p_supported_freq(wpa_s->global->p2p, oper_freq)) {
3565 if (!(wpa_s->drv_flags &
3566 WPA_DRIVER_FLAGS_MULTI_CHANNEL_CONCURRENT)) {
3567 wpa_printf(MSG_DEBUG, "P2P: Cannot start P2P group "
3568 "while connected on non-P2P supported "
3569 "channel (%u MHz)", oper_freq);
3570 return -2;
3571 }
3572 wpa_printf(MSG_DEBUG, "P2P: Current operating channel "
3573 "(%u MHz) not available for P2P - try to use "
3574 "another channel", oper_freq);
3575 force_freq = 0;
3576 } else if (oper_freq > 0) {
3577 wpa_printf(MSG_DEBUG, "P2P: Trying to force us to use the "
3578 "channel we are already using (%u MHz) on another "
3579 "interface", oper_freq);
3580 force_freq = oper_freq;
3581 }
3582
3583 wpa_s->create_p2p_iface = wpas_p2p_create_iface(wpa_s);
3584
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003585 if (wpa_s->create_p2p_iface) {
3586 /* Prepare to add a new interface for the group */
3587 iftype = WPA_IF_P2P_GROUP;
3588 if (go_intent == 15)
3589 iftype = WPA_IF_P2P_GO;
3590 if (wpas_p2p_add_group_interface(wpa_s, iftype) < 0) {
3591 wpa_printf(MSG_ERROR, "P2P: Failed to allocate a new "
3592 "interface for the group");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003593 return -1;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003594 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003595
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003596 if_addr = wpa_s->pending_interface_addr;
3597 } else
3598 if_addr = wpa_s->own_addr;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003599
3600 if (auth) {
3601 if (wpas_p2p_auth_go_neg(wpa_s, peer_addr, wps_method,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003602 go_intent, if_addr,
Dmitry Shmidt04949592012-07-19 12:16:46 -07003603 force_freq, persistent_group, ssid) <
3604 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003605 return -1;
3606 return ret;
3607 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003608
3609 if (wpas_p2p_start_go_neg(wpa_s, peer_addr, wps_method,
3610 go_intent, if_addr, force_freq,
Dmitry Shmidt04949592012-07-19 12:16:46 -07003611 persistent_group, ssid) < 0) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003612 if (wpa_s->create_p2p_iface)
3613 wpas_p2p_remove_pending_group_interface(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003614 return -1;
3615 }
3616 return ret;
3617}
3618
3619
3620/**
3621 * wpas_p2p_remain_on_channel_cb - Indication of remain-on-channel start
3622 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
3623 * @freq: Frequency of the channel in MHz
3624 * @duration: Duration of the stay on the channel in milliseconds
3625 *
3626 * This callback is called when the driver indicates that it has started the
3627 * requested remain-on-channel duration.
3628 */
3629void wpas_p2p_remain_on_channel_cb(struct wpa_supplicant *wpa_s,
3630 unsigned int freq, unsigned int duration)
3631{
3632 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
3633 return;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003634 if (wpa_s->off_channel_freq == wpa_s->pending_listen_freq) {
3635 p2p_listen_cb(wpa_s->global->p2p, wpa_s->pending_listen_freq,
3636 wpa_s->pending_listen_duration);
3637 wpa_s->pending_listen_freq = 0;
3638 }
3639}
3640
3641
3642static int wpas_p2p_listen_start(struct wpa_supplicant *wpa_s,
3643 unsigned int timeout)
3644{
3645 /* Limit maximum Listen state time based on driver limitation. */
3646 if (timeout > wpa_s->max_remain_on_chan)
3647 timeout = wpa_s->max_remain_on_chan;
3648
3649 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
3650 return wpa_drv_p2p_listen(wpa_s, timeout);
3651
3652 return p2p_listen(wpa_s->global->p2p, timeout);
3653}
3654
3655
3656/**
3657 * wpas_p2p_cancel_remain_on_channel_cb - Remain-on-channel timeout
3658 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
3659 * @freq: Frequency of the channel in MHz
3660 *
3661 * This callback is called when the driver indicates that a remain-on-channel
3662 * operation has been completed, i.e., the duration on the requested channel
3663 * has timed out.
3664 */
3665void wpas_p2p_cancel_remain_on_channel_cb(struct wpa_supplicant *wpa_s,
3666 unsigned int freq)
3667{
3668 wpa_printf(MSG_DEBUG, "P2P: Cancel remain-on-channel callback "
3669 "(p2p_long_listen=%d ms pending_action_tx=%p)",
3670 wpa_s->p2p_long_listen, wpa_s->pending_action_tx);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003671 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
3672 return;
3673 if (p2p_listen_end(wpa_s->global->p2p, freq) > 0)
3674 return; /* P2P module started a new operation */
3675 if (wpa_s->pending_action_tx)
3676 return;
3677 if (wpa_s->p2p_long_listen > 0)
3678 wpa_s->p2p_long_listen -= wpa_s->max_remain_on_chan;
3679 if (wpa_s->p2p_long_listen > 0) {
3680 wpa_printf(MSG_DEBUG, "P2P: Continuing long Listen state");
3681 wpas_p2p_listen_start(wpa_s, wpa_s->p2p_long_listen);
3682 }
3683}
3684
3685
3686/**
3687 * wpas_p2p_group_remove - Remove a P2P group
3688 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
3689 * @ifname: Network interface name of the group interface or "*" to remove all
3690 * groups
3691 * Returns: 0 on success, -1 on failure
3692 *
3693 * This function is used to remove a P2P group. This can be used to disconnect
3694 * from a group in which the local end is a P2P Client or to end a P2P Group in
3695 * case the local end is the Group Owner. If a virtual network interface was
3696 * created for this group, that interface will be removed. Otherwise, only the
3697 * configured P2P group network will be removed from the interface.
3698 */
3699int wpas_p2p_group_remove(struct wpa_supplicant *wpa_s, const char *ifname)
3700{
3701 struct wpa_global *global = wpa_s->global;
3702
3703 if (os_strcmp(ifname, "*") == 0) {
3704 struct wpa_supplicant *prev;
3705 wpa_s = global->ifaces;
3706 while (wpa_s) {
3707 prev = wpa_s;
3708 wpa_s = wpa_s->next;
3709 wpas_p2p_disconnect(prev);
3710 }
3711 return 0;
3712 }
3713
3714 for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
3715 if (os_strcmp(wpa_s->ifname, ifname) == 0)
3716 break;
3717 }
3718
3719 return wpas_p2p_disconnect(wpa_s);
3720}
3721
3722
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003723static int wpas_p2p_init_go_params(struct wpa_supplicant *wpa_s,
3724 struct p2p_go_neg_results *params,
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003725 int freq, int ht40)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003726{
3727 u8 bssid[ETH_ALEN];
3728 int res;
3729
3730 os_memset(params, 0, sizeof(*params));
3731 params->role_go = 1;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003732 params->ht40 = ht40;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003733 if (freq) {
3734 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on forced "
3735 "frequency %d MHz", freq);
3736 params->freq = freq;
3737 } else if (wpa_s->conf->p2p_oper_reg_class == 81 &&
3738 wpa_s->conf->p2p_oper_channel >= 1 &&
3739 wpa_s->conf->p2p_oper_channel <= 11) {
3740 params->freq = 2407 + 5 * wpa_s->conf->p2p_oper_channel;
3741 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on configured "
3742 "frequency %d MHz", params->freq);
3743 } else if (wpa_s->conf->p2p_oper_reg_class == 115 ||
Jouni Malinen87fd2792011-05-16 18:35:42 +03003744 wpa_s->conf->p2p_oper_reg_class == 124) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003745 params->freq = 5000 + 5 * wpa_s->conf->p2p_oper_channel;
3746 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on configured "
3747 "frequency %d MHz", params->freq);
3748 } else if (wpa_s->conf->p2p_oper_channel == 0 &&
3749 wpa_s->best_overall_freq > 0 &&
3750 p2p_supported_freq(wpa_s->global->p2p,
3751 wpa_s->best_overall_freq)) {
3752 params->freq = wpa_s->best_overall_freq;
3753 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on best overall "
3754 "channel %d MHz", params->freq);
3755 } else if (wpa_s->conf->p2p_oper_channel == 0 &&
3756 wpa_s->best_24_freq > 0 &&
3757 p2p_supported_freq(wpa_s->global->p2p,
3758 wpa_s->best_24_freq)) {
3759 params->freq = wpa_s->best_24_freq;
3760 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on best 2.4 GHz "
3761 "channel %d MHz", params->freq);
3762 } else if (wpa_s->conf->p2p_oper_channel == 0 &&
3763 wpa_s->best_5_freq > 0 &&
3764 p2p_supported_freq(wpa_s->global->p2p,
3765 wpa_s->best_5_freq)) {
3766 params->freq = wpa_s->best_5_freq;
3767 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on best 5 GHz "
3768 "channel %d MHz", params->freq);
3769 } else {
Dmitry Shmidt04949592012-07-19 12:16:46 -07003770 int chan;
3771 for (chan = 0; chan < 11; chan++) {
3772 params->freq = 2412 + chan * 5;
3773 if (!wpas_p2p_disallowed_freq(wpa_s->global,
3774 params->freq))
3775 break;
3776 }
3777 if (chan == 11) {
3778 wpa_printf(MSG_DEBUG, "P2P: No 2.4 GHz channel "
3779 "allowed");
3780 return -1;
3781 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003782 wpa_printf(MSG_DEBUG, "P2P: Set GO freq %d MHz (no preference "
3783 "known)", params->freq);
3784 }
3785
3786 if (wpa_s->current_ssid && wpa_drv_get_bssid(wpa_s, bssid) == 0 &&
3787 wpa_s->assoc_freq && !freq) {
3788 wpa_printf(MSG_DEBUG, "P2P: Force GO on the channel we are "
3789 "already using");
3790 params->freq = wpa_s->assoc_freq;
3791 }
3792
3793 res = wpa_drv_shared_freq(wpa_s);
3794 if (res > 0 && !freq) {
3795 wpa_printf(MSG_DEBUG, "P2P: Force GO on the channel we are "
3796 "already using on a shared interface");
3797 params->freq = res;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003798 } else if (res > 0 && freq != res &&
3799 !(wpa_s->drv_flags &
3800 WPA_DRIVER_FLAGS_MULTI_CHANNEL_CONCURRENT)) {
3801 wpa_printf(MSG_DEBUG, "P2P: Cannot start P2P group on %u MHz "
3802 "while connected on another channel (%u MHz)",
3803 freq, res);
3804 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003805 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003806
3807 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003808}
3809
3810
3811static struct wpa_supplicant *
3812wpas_p2p_get_group_iface(struct wpa_supplicant *wpa_s, int addr_allocated,
3813 int go)
3814{
3815 struct wpa_supplicant *group_wpa_s;
3816
3817 if (!wpas_p2p_create_iface(wpa_s))
3818 return wpa_s;
3819
3820 if (wpas_p2p_add_group_interface(wpa_s, go ? WPA_IF_P2P_GO :
3821 WPA_IF_P2P_CLIENT) < 0)
3822 return NULL;
3823 group_wpa_s = wpas_p2p_init_group_interface(wpa_s, go);
3824 if (group_wpa_s == NULL) {
3825 wpas_p2p_remove_pending_group_interface(wpa_s);
3826 return NULL;
3827 }
3828
3829 return group_wpa_s;
3830}
3831
3832
3833/**
3834 * wpas_p2p_group_add - Add a new P2P group with local end as Group Owner
3835 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
3836 * @persistent_group: Whether to create a persistent group
3837 * @freq: Frequency for the group or 0 to indicate no hardcoding
3838 * Returns: 0 on success, -1 on failure
3839 *
3840 * This function creates a new P2P group with the local end as the Group Owner,
3841 * i.e., without using Group Owner Negotiation.
3842 */
3843int wpas_p2p_group_add(struct wpa_supplicant *wpa_s, int persistent_group,
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003844 int freq, int ht40)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003845{
3846 struct p2p_go_neg_results params;
3847 unsigned int r;
3848
3849 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
3850 return -1;
3851
Dmitry Shmidtdca39792011-09-06 11:17:33 -07003852 /* Make sure we are not running find during connection establishment */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003853 wpa_printf(MSG_DEBUG, "P2P: Stop any on-going P2P FIND");
Dmitry Shmidtdca39792011-09-06 11:17:33 -07003854 wpas_p2p_stop_find(wpa_s);
Dmitry Shmidtdca39792011-09-06 11:17:33 -07003855
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003856 if (freq == 2) {
3857 wpa_printf(MSG_DEBUG, "P2P: Request to start GO on 2.4 GHz "
3858 "band");
3859 if (wpa_s->best_24_freq > 0 &&
3860 p2p_supported_freq(wpa_s->global->p2p,
3861 wpa_s->best_24_freq)) {
3862 freq = wpa_s->best_24_freq;
3863 wpa_printf(MSG_DEBUG, "P2P: Use best 2.4 GHz band "
3864 "channel: %d MHz", freq);
3865 } else {
3866 os_get_random((u8 *) &r, sizeof(r));
3867 freq = 2412 + (r % 3) * 25;
3868 wpa_printf(MSG_DEBUG, "P2P: Use random 2.4 GHz band "
3869 "channel: %d MHz", freq);
3870 }
3871 }
3872
3873 if (freq == 5) {
3874 wpa_printf(MSG_DEBUG, "P2P: Request to start GO on 5 GHz "
3875 "band");
3876 if (wpa_s->best_5_freq > 0 &&
3877 p2p_supported_freq(wpa_s->global->p2p,
3878 wpa_s->best_5_freq)) {
3879 freq = wpa_s->best_5_freq;
3880 wpa_printf(MSG_DEBUG, "P2P: Use best 5 GHz band "
3881 "channel: %d MHz", freq);
3882 } else {
3883 os_get_random((u8 *) &r, sizeof(r));
3884 freq = 5180 + (r % 4) * 20;
3885 if (!p2p_supported_freq(wpa_s->global->p2p, freq)) {
3886 wpa_printf(MSG_DEBUG, "P2P: Could not select "
3887 "5 GHz channel for P2P group");
3888 return -1;
3889 }
3890 wpa_printf(MSG_DEBUG, "P2P: Use random 5 GHz band "
3891 "channel: %d MHz", freq);
3892 }
3893 }
3894
3895 if (freq > 0 && !p2p_supported_freq(wpa_s->global->p2p, freq)) {
3896 wpa_printf(MSG_DEBUG, "P2P: The forced channel for GO "
3897 "(%u MHz) is not supported for P2P uses",
3898 freq);
3899 return -1;
3900 }
3901
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003902 if (wpas_p2p_init_go_params(wpa_s, &params, freq, ht40))
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003903 return -1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003904 if (params.freq &&
3905 !p2p_supported_freq(wpa_s->global->p2p, params.freq)) {
3906 wpa_printf(MSG_DEBUG, "P2P: The selected channel for GO "
3907 "(%u MHz) is not supported for P2P uses",
3908 params.freq);
3909 return -1;
3910 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003911 p2p_go_params(wpa_s->global->p2p, &params);
3912 params.persistent_group = persistent_group;
3913
3914 wpa_s = wpas_p2p_get_group_iface(wpa_s, 0, 1);
3915 if (wpa_s == NULL)
3916 return -1;
3917 wpas_start_wps_go(wpa_s, &params, 0);
3918
3919 return 0;
3920}
3921
3922
3923static int wpas_start_p2p_client(struct wpa_supplicant *wpa_s,
3924 struct wpa_ssid *params, int addr_allocated)
3925{
3926 struct wpa_ssid *ssid;
3927
3928 wpa_s = wpas_p2p_get_group_iface(wpa_s, addr_allocated, 0);
3929 if (wpa_s == NULL)
3930 return -1;
3931
3932 wpa_supplicant_ap_deinit(wpa_s);
3933
3934 ssid = wpa_config_add_network(wpa_s->conf);
3935 if (ssid == NULL)
3936 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003937 wpa_config_set_network_defaults(ssid);
3938 ssid->temporary = 1;
3939 ssid->proto = WPA_PROTO_RSN;
3940 ssid->pairwise_cipher = WPA_CIPHER_CCMP;
3941 ssid->group_cipher = WPA_CIPHER_CCMP;
3942 ssid->key_mgmt = WPA_KEY_MGMT_PSK;
3943 ssid->ssid = os_malloc(params->ssid_len);
3944 if (ssid->ssid == NULL) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003945 wpa_config_remove_network(wpa_s->conf, ssid->id);
3946 return -1;
3947 }
3948 os_memcpy(ssid->ssid, params->ssid, params->ssid_len);
3949 ssid->ssid_len = params->ssid_len;
3950 ssid->p2p_group = 1;
3951 ssid->export_keys = 1;
3952 if (params->psk_set) {
3953 os_memcpy(ssid->psk, params->psk, 32);
3954 ssid->psk_set = 1;
3955 }
3956 if (params->passphrase)
3957 ssid->passphrase = os_strdup(params->passphrase);
3958
3959 wpa_supplicant_select_network(wpa_s, ssid);
3960
3961 wpa_s->show_group_started = 1;
3962
3963 return 0;
3964}
3965
3966
3967int wpas_p2p_group_add_persistent(struct wpa_supplicant *wpa_s,
3968 struct wpa_ssid *ssid, int addr_allocated,
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003969 int freq, int ht40)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003970{
3971 struct p2p_go_neg_results params;
3972 int go = 0;
3973
3974 if (ssid->disabled != 2 || ssid->ssid == NULL)
3975 return -1;
3976
3977 if (wpas_get_p2p_group(wpa_s, ssid->ssid, ssid->ssid_len, &go) &&
3978 go == (ssid->mode == WPAS_MODE_P2P_GO)) {
3979 wpa_printf(MSG_DEBUG, "P2P: Requested persistent group is "
3980 "already running");
3981 return 0;
3982 }
3983
3984 /* Make sure we are not running find during connection establishment */
3985 wpas_p2p_stop_find(wpa_s);
3986
Dmitry Shmidt04949592012-07-19 12:16:46 -07003987 wpa_s->p2p_fallback_to_go_neg = 0;
3988
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003989 if (ssid->mode == WPAS_MODE_INFRA)
3990 return wpas_start_p2p_client(wpa_s, ssid, addr_allocated);
3991
3992 if (ssid->mode != WPAS_MODE_P2P_GO)
3993 return -1;
3994
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003995 if (wpas_p2p_init_go_params(wpa_s, &params, freq, ht40))
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003996 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003997
3998 params.role_go = 1;
3999 if (ssid->passphrase == NULL ||
4000 os_strlen(ssid->passphrase) >= sizeof(params.passphrase)) {
4001 wpa_printf(MSG_DEBUG, "P2P: Invalid passphrase in persistent "
4002 "group");
4003 return -1;
4004 }
4005 os_strlcpy(params.passphrase, ssid->passphrase,
4006 sizeof(params.passphrase));
4007 os_memcpy(params.ssid, ssid->ssid, ssid->ssid_len);
4008 params.ssid_len = ssid->ssid_len;
4009 params.persistent_group = 1;
4010
4011 wpa_s = wpas_p2p_get_group_iface(wpa_s, addr_allocated, 1);
4012 if (wpa_s == NULL)
4013 return -1;
4014
4015 wpas_start_wps_go(wpa_s, &params, 0);
4016
4017 return 0;
4018}
4019
4020
4021static void wpas_p2p_ie_update(void *ctx, struct wpabuf *beacon_ies,
4022 struct wpabuf *proberesp_ies)
4023{
4024 struct wpa_supplicant *wpa_s = ctx;
4025 if (wpa_s->ap_iface) {
4026 struct hostapd_data *hapd = wpa_s->ap_iface->bss[0];
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004027 if (!(hapd->conf->p2p & P2P_GROUP_OWNER)) {
4028 wpabuf_free(beacon_ies);
4029 wpabuf_free(proberesp_ies);
4030 return;
4031 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004032 if (beacon_ies) {
4033 wpabuf_free(hapd->p2p_beacon_ie);
4034 hapd->p2p_beacon_ie = beacon_ies;
4035 }
4036 wpabuf_free(hapd->p2p_probe_resp_ie);
4037 hapd->p2p_probe_resp_ie = proberesp_ies;
4038 } else {
4039 wpabuf_free(beacon_ies);
4040 wpabuf_free(proberesp_ies);
4041 }
4042 wpa_supplicant_ap_update_beacon(wpa_s);
4043}
4044
4045
4046static void wpas_p2p_idle_update(void *ctx, int idle)
4047{
4048 struct wpa_supplicant *wpa_s = ctx;
4049 if (!wpa_s->ap_iface)
4050 return;
4051 wpa_printf(MSG_DEBUG, "P2P: GO - group %sidle", idle ? "" : "not ");
Dmitry Shmidt04949592012-07-19 12:16:46 -07004052 if (idle)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004053 wpas_p2p_set_group_idle_timeout(wpa_s);
4054 else
4055 eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL);
4056}
4057
4058
4059struct p2p_group * wpas_p2p_group_init(struct wpa_supplicant *wpa_s,
Dmitry Shmidt04949592012-07-19 12:16:46 -07004060 struct wpa_ssid *ssid)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004061{
4062 struct p2p_group *group;
4063 struct p2p_group_config *cfg;
4064
4065 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
4066 return NULL;
4067 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
4068 return NULL;
4069
4070 cfg = os_zalloc(sizeof(*cfg));
4071 if (cfg == NULL)
4072 return NULL;
4073
Dmitry Shmidt04949592012-07-19 12:16:46 -07004074 if (ssid->p2p_persistent_group && wpa_s->conf->persistent_reconnect)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004075 cfg->persistent_group = 2;
Dmitry Shmidt04949592012-07-19 12:16:46 -07004076 else if (ssid->p2p_persistent_group)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004077 cfg->persistent_group = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004078 os_memcpy(cfg->interface_addr, wpa_s->own_addr, ETH_ALEN);
4079 if (wpa_s->max_stations &&
4080 wpa_s->max_stations < wpa_s->conf->max_num_sta)
4081 cfg->max_clients = wpa_s->max_stations;
4082 else
4083 cfg->max_clients = wpa_s->conf->max_num_sta;
Dmitry Shmidt04949592012-07-19 12:16:46 -07004084 os_memcpy(cfg->ssid, ssid->ssid, ssid->ssid_len);
4085 cfg->ssid_len = ssid->ssid_len;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004086 cfg->cb_ctx = wpa_s;
4087 cfg->ie_update = wpas_p2p_ie_update;
4088 cfg->idle_update = wpas_p2p_idle_update;
4089
4090 group = p2p_group_init(wpa_s->global->p2p, cfg);
4091 if (group == NULL)
4092 os_free(cfg);
Dmitry Shmidt04949592012-07-19 12:16:46 -07004093 if (ssid->mode != WPAS_MODE_P2P_GROUP_FORMATION)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004094 p2p_group_notif_formation_done(group);
4095 wpa_s->p2p_group = group;
4096 return group;
4097}
4098
4099
4100void wpas_p2p_wps_success(struct wpa_supplicant *wpa_s, const u8 *peer_addr,
4101 int registrar)
4102{
Dmitry Shmidt04949592012-07-19 12:16:46 -07004103 struct wpa_ssid *ssid = wpa_s->current_ssid;
4104
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004105 if (!wpa_s->p2p_in_provisioning) {
4106 wpa_printf(MSG_DEBUG, "P2P: Ignore WPS success event - P2P "
4107 "provisioning not in progress");
4108 return;
4109 }
4110
Dmitry Shmidt04949592012-07-19 12:16:46 -07004111 if (ssid && ssid->mode == WPAS_MODE_INFRA) {
4112 u8 go_dev_addr[ETH_ALEN];
4113 os_memcpy(go_dev_addr, wpa_s->bssid, ETH_ALEN);
4114 wpas_p2p_persistent_group(wpa_s, go_dev_addr, ssid->ssid,
4115 ssid->ssid_len);
4116 /* Clear any stored provisioning info */
4117 p2p_clear_provisioning_info(wpa_s->global->p2p, go_dev_addr);
4118 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004119
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004120 eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s->parent,
4121 NULL);
Dmitry Shmidt04949592012-07-19 12:16:46 -07004122 if (ssid && ssid->mode == WPAS_MODE_INFRA) {
4123 /*
4124 * Use a separate timeout for initial data connection to
4125 * complete to allow the group to be removed automatically if
4126 * something goes wrong in this step before the P2P group idle
4127 * timeout mechanism is taken into use.
4128 */
4129 eloop_register_timeout(P2P_MAX_INITIAL_CONN_WAIT, 0,
4130 wpas_p2p_group_formation_timeout,
4131 wpa_s->parent, NULL);
4132 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004133 if (wpa_s->global->p2p)
4134 p2p_wps_success_cb(wpa_s->global->p2p, peer_addr);
4135 else if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
4136 wpa_drv_wps_success_cb(wpa_s, peer_addr);
4137 wpas_group_formation_completed(wpa_s, 1);
4138}
4139
4140
Jouni Malinen75ecf522011-06-27 15:19:46 -07004141void wpas_p2p_wps_failed(struct wpa_supplicant *wpa_s,
4142 struct wps_event_fail *fail)
4143{
4144 if (!wpa_s->p2p_in_provisioning) {
4145 wpa_printf(MSG_DEBUG, "P2P: Ignore WPS fail event - P2P "
4146 "provisioning not in progress");
4147 return;
4148 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004149
4150 if (wpa_s->go_params) {
4151 p2p_clear_provisioning_info(
4152 wpa_s->global->p2p,
Dmitry Shmidt04949592012-07-19 12:16:46 -07004153 wpa_s->go_params->peer_device_addr);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004154 }
4155
Jouni Malinen75ecf522011-06-27 15:19:46 -07004156 wpas_notify_p2p_wps_failed(wpa_s, fail);
4157}
4158
4159
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004160int wpas_p2p_prov_disc(struct wpa_supplicant *wpa_s, const u8 *peer_addr,
Dmitry Shmidt04949592012-07-19 12:16:46 -07004161 const char *config_method,
4162 enum wpas_p2p_prov_disc_use use)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004163{
4164 u16 config_methods;
4165
Dmitry Shmidt04949592012-07-19 12:16:46 -07004166 wpa_s->p2p_fallback_to_go_neg = 0;
4167 wpa_s->pending_pd_use = NORMAL_PD;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004168 if (os_strncmp(config_method, "display", 7) == 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004169 config_methods = WPS_CONFIG_DISPLAY;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004170 else if (os_strncmp(config_method, "keypad", 6) == 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004171 config_methods = WPS_CONFIG_KEYPAD;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004172 else if (os_strncmp(config_method, "pbc", 3) == 0 ||
4173 os_strncmp(config_method, "pushbutton", 10) == 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004174 config_methods = WPS_CONFIG_PUSHBUTTON;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004175 else {
4176 wpa_printf(MSG_DEBUG, "P2P: Unknown config method");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004177 return -1;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004178 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004179
Dmitry Shmidt04949592012-07-19 12:16:46 -07004180 if (use == WPAS_P2P_PD_AUTO) {
4181 os_memcpy(wpa_s->pending_join_dev_addr, peer_addr, ETH_ALEN);
4182 wpa_s->pending_pd_config_methods = config_methods;
4183 wpa_s->p2p_auto_pd = 1;
4184 wpa_s->p2p_auto_join = 0;
4185 wpa_s->pending_pd_before_join = 0;
4186 wpa_s->auto_pd_scan_retry = 0;
4187 wpas_p2p_stop_find(wpa_s);
4188 wpa_s->p2p_join_scan_count = 0;
4189 os_get_time(&wpa_s->p2p_auto_started);
4190 wpa_printf(MSG_DEBUG, "P2P: Auto PD started at %ld.%06ld",
4191 wpa_s->p2p_auto_started.sec,
4192 wpa_s->p2p_auto_started.usec);
4193 wpas_p2p_join_scan(wpa_s, NULL);
4194 return 0;
4195 }
4196
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004197 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT) {
4198 return wpa_drv_p2p_prov_disc_req(wpa_s, peer_addr,
Dmitry Shmidt04949592012-07-19 12:16:46 -07004199 config_methods,
4200 use == WPAS_P2P_PD_FOR_JOIN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004201 }
4202
4203 if (wpa_s->global->p2p == NULL || wpa_s->global->p2p_disabled)
4204 return -1;
4205
4206 return p2p_prov_disc_req(wpa_s->global->p2p, peer_addr,
Dmitry Shmidt04949592012-07-19 12:16:46 -07004207 config_methods, use == WPAS_P2P_PD_FOR_JOIN,
4208 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004209}
4210
4211
4212int wpas_p2p_scan_result_text(const u8 *ies, size_t ies_len, char *buf,
4213 char *end)
4214{
4215 return p2p_scan_result_text(ies, ies_len, buf, end);
4216}
4217
4218
4219static void wpas_p2p_clear_pending_action_tx(struct wpa_supplicant *wpa_s)
4220{
4221 if (!wpa_s->pending_action_tx)
4222 return;
4223
4224 wpa_printf(MSG_DEBUG, "P2P: Drop pending Action TX due to new "
4225 "operation request");
4226 wpabuf_free(wpa_s->pending_action_tx);
4227 wpa_s->pending_action_tx = NULL;
4228}
4229
4230
4231int wpas_p2p_find(struct wpa_supplicant *wpa_s, unsigned int timeout,
4232 enum p2p_discovery_type type,
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08004233 unsigned int num_req_dev_types, const u8 *req_dev_types,
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004234 const u8 *dev_id, unsigned int search_delay)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004235{
4236 wpas_p2p_clear_pending_action_tx(wpa_s);
4237 wpa_s->p2p_long_listen = 0;
4238
4239 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
4240 return wpa_drv_p2p_find(wpa_s, timeout, type);
4241
Dmitry Shmidt04949592012-07-19 12:16:46 -07004242 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL ||
4243 wpa_s->p2p_in_provisioning)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004244 return -1;
4245
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08004246 wpa_supplicant_cancel_sched_scan(wpa_s);
4247
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004248 return p2p_find(wpa_s->global->p2p, timeout, type,
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004249 num_req_dev_types, req_dev_types, dev_id,
4250 search_delay);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004251}
4252
4253
4254void wpas_p2p_stop_find(struct wpa_supplicant *wpa_s)
4255{
4256 wpas_p2p_clear_pending_action_tx(wpa_s);
4257 wpa_s->p2p_long_listen = 0;
4258 eloop_cancel_timeout(wpas_p2p_long_listen_timeout, wpa_s, NULL);
4259 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004260 wpa_s->p2p_cb_on_scan_complete = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004261
4262 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT) {
4263 wpa_drv_p2p_stop_find(wpa_s);
4264 return;
4265 }
4266
4267 if (wpa_s->global->p2p)
4268 p2p_stop_find(wpa_s->global->p2p);
4269
4270 wpas_p2p_remove_pending_group_interface(wpa_s);
4271}
4272
4273
4274static void wpas_p2p_long_listen_timeout(void *eloop_ctx, void *timeout_ctx)
4275{
4276 struct wpa_supplicant *wpa_s = eloop_ctx;
4277 wpa_s->p2p_long_listen = 0;
4278}
4279
4280
4281int wpas_p2p_listen(struct wpa_supplicant *wpa_s, unsigned int timeout)
4282{
4283 int res;
4284
4285 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
4286 return -1;
4287
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08004288 wpa_supplicant_cancel_sched_scan(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004289 wpas_p2p_clear_pending_action_tx(wpa_s);
4290
4291 if (timeout == 0) {
4292 /*
4293 * This is a request for unlimited Listen state. However, at
4294 * least for now, this is mapped to a Listen state for one
4295 * hour.
4296 */
4297 timeout = 3600;
4298 }
4299 eloop_cancel_timeout(wpas_p2p_long_listen_timeout, wpa_s, NULL);
4300 wpa_s->p2p_long_listen = 0;
4301
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004302 /*
4303 * Stop previous find/listen operation to avoid trying to request a new
4304 * remain-on-channel operation while the driver is still running the
4305 * previous one.
4306 */
4307 if (wpa_s->global->p2p)
4308 p2p_stop_find(wpa_s->global->p2p);
4309
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004310 res = wpas_p2p_listen_start(wpa_s, timeout * 1000);
4311 if (res == 0 && timeout * 1000 > wpa_s->max_remain_on_chan) {
4312 wpa_s->p2p_long_listen = timeout * 1000;
4313 eloop_register_timeout(timeout, 0,
4314 wpas_p2p_long_listen_timeout,
4315 wpa_s, NULL);
4316 }
4317
4318 return res;
4319}
4320
4321
4322int wpas_p2p_assoc_req_ie(struct wpa_supplicant *wpa_s, struct wpa_bss *bss,
4323 u8 *buf, size_t len, int p2p_group)
4324{
4325 struct wpabuf *p2p_ie;
4326 int ret;
4327
4328 if (wpa_s->global->p2p_disabled)
4329 return -1;
4330 if (wpa_s->global->p2p == NULL)
4331 return -1;
4332 if (bss == NULL)
4333 return -1;
4334
4335 p2p_ie = wpa_bss_get_vendor_ie_multi(bss, P2P_IE_VENDOR_TYPE);
4336 ret = p2p_assoc_req_ie(wpa_s->global->p2p, bss->bssid, buf, len,
4337 p2p_group, p2p_ie);
4338 wpabuf_free(p2p_ie);
4339
4340 return ret;
4341}
4342
4343
4344int wpas_p2p_probe_req_rx(struct wpa_supplicant *wpa_s, const u8 *addr,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004345 const u8 *dst, const u8 *bssid,
Dmitry Shmidt04949592012-07-19 12:16:46 -07004346 const u8 *ie, size_t ie_len, int ssi_signal)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004347{
4348 if (wpa_s->global->p2p_disabled)
4349 return 0;
4350 if (wpa_s->global->p2p == NULL)
4351 return 0;
4352
Dmitry Shmidt04949592012-07-19 12:16:46 -07004353 switch (p2p_probe_req_rx(wpa_s->global->p2p, addr, dst, bssid,
4354 ie, ie_len)) {
4355 case P2P_PREQ_NOT_P2P:
4356 wpas_notify_preq(wpa_s, addr, dst, bssid, ie, ie_len,
4357 ssi_signal);
4358 /* fall through */
4359 case P2P_PREQ_MALFORMED:
4360 case P2P_PREQ_NOT_LISTEN:
4361 case P2P_PREQ_NOT_PROCESSED:
4362 default: /* make gcc happy */
4363 return 0;
4364 case P2P_PREQ_PROCESSED:
4365 return 1;
4366 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004367}
4368
4369
4370void wpas_p2p_rx_action(struct wpa_supplicant *wpa_s, const u8 *da,
4371 const u8 *sa, const u8 *bssid,
4372 u8 category, const u8 *data, size_t len, int freq)
4373{
4374 if (wpa_s->global->p2p_disabled)
4375 return;
4376 if (wpa_s->global->p2p == NULL)
4377 return;
4378
4379 p2p_rx_action(wpa_s->global->p2p, da, sa, bssid, category, data, len,
4380 freq);
4381}
4382
4383
4384void wpas_p2p_scan_ie(struct wpa_supplicant *wpa_s, struct wpabuf *ies)
4385{
4386 if (wpa_s->global->p2p_disabled)
4387 return;
4388 if (wpa_s->global->p2p == NULL)
4389 return;
4390
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08004391 p2p_scan_ie(wpa_s->global->p2p, ies, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004392}
4393
4394
4395void wpas_p2p_group_deinit(struct wpa_supplicant *wpa_s)
4396{
4397 p2p_group_deinit(wpa_s->p2p_group);
4398 wpa_s->p2p_group = NULL;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004399
4400 wpa_s->ap_configured_cb = NULL;
4401 wpa_s->ap_configured_cb_ctx = NULL;
4402 wpa_s->ap_configured_cb_data = NULL;
4403 wpa_s->connect_without_scan = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004404}
4405
4406
4407int wpas_p2p_reject(struct wpa_supplicant *wpa_s, const u8 *addr)
4408{
4409 wpa_s->p2p_long_listen = 0;
4410
4411 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
4412 return wpa_drv_p2p_reject(wpa_s, addr);
4413
4414 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
4415 return -1;
4416
4417 return p2p_reject(wpa_s->global->p2p, addr);
4418}
4419
4420
4421/* Invite to reinvoke a persistent group */
4422int wpas_p2p_invite(struct wpa_supplicant *wpa_s, const u8 *peer_addr,
Jouni Malinen31be0a42012-08-31 21:20:51 +03004423 struct wpa_ssid *ssid, const u8 *go_dev_addr, int freq,
4424 int ht40)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004425{
4426 enum p2p_invite_role role;
4427 u8 *bssid = NULL;
4428
Jouni Malinen31be0a42012-08-31 21:20:51 +03004429 wpa_s->p2p_persistent_go_freq = freq;
4430 wpa_s->p2p_go_ht40 = !!ht40;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004431 if (ssid->mode == WPAS_MODE_P2P_GO) {
4432 role = P2P_INVITE_ROLE_GO;
4433 if (peer_addr == NULL) {
4434 wpa_printf(MSG_DEBUG, "P2P: Missing peer "
4435 "address in invitation command");
4436 return -1;
4437 }
4438 if (wpas_p2p_create_iface(wpa_s)) {
4439 if (wpas_p2p_add_group_interface(wpa_s,
4440 WPA_IF_P2P_GO) < 0) {
4441 wpa_printf(MSG_ERROR, "P2P: Failed to "
4442 "allocate a new interface for the "
4443 "group");
4444 return -1;
4445 }
4446 bssid = wpa_s->pending_interface_addr;
4447 } else
4448 bssid = wpa_s->own_addr;
4449 } else {
4450 role = P2P_INVITE_ROLE_CLIENT;
4451 peer_addr = ssid->bssid;
4452 }
4453 wpa_s->pending_invite_ssid_id = ssid->id;
4454
4455 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
4456 return wpa_drv_p2p_invite(wpa_s, peer_addr, role, bssid,
4457 ssid->ssid, ssid->ssid_len,
4458 go_dev_addr, 1);
4459
4460 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
4461 return -1;
4462
4463 return p2p_invite(wpa_s->global->p2p, peer_addr, role, bssid,
Jouni Malinen31be0a42012-08-31 21:20:51 +03004464 ssid->ssid, ssid->ssid_len, freq, go_dev_addr, 1);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004465}
4466
4467
4468/* Invite to join an active group */
4469int wpas_p2p_invite_group(struct wpa_supplicant *wpa_s, const char *ifname,
4470 const u8 *peer_addr, const u8 *go_dev_addr)
4471{
4472 struct wpa_global *global = wpa_s->global;
4473 enum p2p_invite_role role;
4474 u8 *bssid = NULL;
4475 struct wpa_ssid *ssid;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08004476 int persistent;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004477
Jouni Malinen31be0a42012-08-31 21:20:51 +03004478 wpa_s->p2p_persistent_go_freq = 0;
4479 wpa_s->p2p_go_ht40 = 0;
4480
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004481 for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
4482 if (os_strcmp(wpa_s->ifname, ifname) == 0)
4483 break;
4484 }
4485 if (wpa_s == NULL) {
4486 wpa_printf(MSG_DEBUG, "P2P: Interface '%s' not found", ifname);
4487 return -1;
4488 }
4489
4490 ssid = wpa_s->current_ssid;
4491 if (ssid == NULL) {
4492 wpa_printf(MSG_DEBUG, "P2P: No current SSID to use for "
4493 "invitation");
4494 return -1;
4495 }
4496
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08004497 persistent = ssid->p2p_persistent_group &&
4498 wpas_p2p_get_persistent(wpa_s->parent, peer_addr,
4499 ssid->ssid, ssid->ssid_len);
4500
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004501 if (ssid->mode == WPAS_MODE_P2P_GO) {
4502 role = P2P_INVITE_ROLE_ACTIVE_GO;
4503 bssid = wpa_s->own_addr;
4504 if (go_dev_addr == NULL)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004505 go_dev_addr = wpa_s->global->p2p_dev_addr;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004506 } else {
4507 role = P2P_INVITE_ROLE_CLIENT;
4508 if (wpa_s->wpa_state < WPA_ASSOCIATED) {
4509 wpa_printf(MSG_DEBUG, "P2P: Not associated - cannot "
4510 "invite to current group");
4511 return -1;
4512 }
4513 bssid = wpa_s->bssid;
4514 if (go_dev_addr == NULL &&
4515 !is_zero_ether_addr(wpa_s->go_dev_addr))
4516 go_dev_addr = wpa_s->go_dev_addr;
4517 }
4518 wpa_s->parent->pending_invite_ssid_id = -1;
4519
4520 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
4521 return wpa_drv_p2p_invite(wpa_s, peer_addr, role, bssid,
4522 ssid->ssid, ssid->ssid_len,
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08004523 go_dev_addr, persistent);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004524
4525 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
4526 return -1;
4527
4528 return p2p_invite(wpa_s->global->p2p, peer_addr, role, bssid,
4529 ssid->ssid, ssid->ssid_len, wpa_s->assoc_freq,
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08004530 go_dev_addr, persistent);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004531}
4532
4533
4534void wpas_p2p_completed(struct wpa_supplicant *wpa_s)
4535{
4536 struct wpa_ssid *ssid = wpa_s->current_ssid;
4537 const char *ssid_txt;
4538 u8 go_dev_addr[ETH_ALEN];
Jouni Malinen75ecf522011-06-27 15:19:46 -07004539 int network_id = -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004540 int persistent;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004541 int freq;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004542
Dmitry Shmidt04949592012-07-19 12:16:46 -07004543 if (ssid == NULL || ssid->mode != WPAS_MODE_P2P_GROUP_FORMATION) {
4544 eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
4545 wpa_s->parent, NULL);
4546 }
4547
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004548 if (!wpa_s->show_group_started || !ssid)
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004549 goto done;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004550
4551 wpa_s->show_group_started = 0;
4552
4553 ssid_txt = wpa_ssid_txt(ssid->ssid, ssid->ssid_len);
4554 os_memset(go_dev_addr, 0, ETH_ALEN);
4555 if (ssid->bssid_set)
4556 os_memcpy(go_dev_addr, ssid->bssid, ETH_ALEN);
4557 persistent = wpas_p2p_persistent_group(wpa_s, go_dev_addr, ssid->ssid,
4558 ssid->ssid_len);
4559 os_memcpy(wpa_s->go_dev_addr, go_dev_addr, ETH_ALEN);
4560
4561 if (wpa_s->global->p2p_group_formation == wpa_s)
4562 wpa_s->global->p2p_group_formation = NULL;
4563
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004564 freq = wpa_s->current_bss ? wpa_s->current_bss->freq :
4565 (int) wpa_s->assoc_freq;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004566 if (ssid->passphrase == NULL && ssid->psk_set) {
4567 char psk[65];
4568 wpa_snprintf_hex(psk, sizeof(psk), ssid->psk, 32);
4569 wpa_msg(wpa_s->parent, MSG_INFO, P2P_EVENT_GROUP_STARTED
4570 "%s client ssid=\"%s\" freq=%d psk=%s go_dev_addr="
4571 MACSTR "%s",
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004572 wpa_s->ifname, ssid_txt, freq, psk,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004573 MAC2STR(go_dev_addr),
4574 persistent ? " [PERSISTENT]" : "");
4575 } else {
4576 wpa_msg(wpa_s->parent, MSG_INFO, P2P_EVENT_GROUP_STARTED
4577 "%s client ssid=\"%s\" freq=%d passphrase=\"%s\" "
4578 "go_dev_addr=" MACSTR "%s",
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004579 wpa_s->ifname, ssid_txt, freq,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004580 ssid->passphrase ? ssid->passphrase : "",
4581 MAC2STR(go_dev_addr),
4582 persistent ? " [PERSISTENT]" : "");
4583 }
4584
4585 if (persistent)
Jouni Malinen75ecf522011-06-27 15:19:46 -07004586 network_id = wpas_p2p_store_persistent_group(wpa_s->parent,
4587 ssid, go_dev_addr);
4588 if (network_id < 0)
4589 network_id = ssid->id;
4590 wpas_notify_p2p_group_started(wpa_s, ssid, network_id, 1);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004591
4592done:
4593 if (wpa_s->p2p_cb_on_scan_complete && !wpa_s->global->p2p_disabled &&
4594 wpa_s->global->p2p != NULL) {
4595 wpa_s->p2p_cb_on_scan_complete = 0;
4596 if (p2p_other_scan_completed(wpa_s->global->p2p) == 1) {
4597 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Pending P2P operation "
4598 "continued after successful connection");
4599 p2p_increase_search_delay(
4600 wpa_s->global->p2p,
4601 wpas_p2p_search_delay(wpa_s));
4602 }
4603 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004604}
4605
4606
4607int wpas_p2p_presence_req(struct wpa_supplicant *wpa_s, u32 duration1,
4608 u32 interval1, u32 duration2, u32 interval2)
4609{
4610 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
4611 return -1;
4612 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
4613 return -1;
4614
4615 if (wpa_s->wpa_state < WPA_ASSOCIATED ||
4616 wpa_s->current_ssid == NULL ||
4617 wpa_s->current_ssid->mode != WPAS_MODE_INFRA)
4618 return -1;
4619
4620 return p2p_presence_req(wpa_s->global->p2p, wpa_s->bssid,
4621 wpa_s->own_addr, wpa_s->assoc_freq,
4622 duration1, interval1, duration2, interval2);
4623}
4624
4625
4626int wpas_p2p_ext_listen(struct wpa_supplicant *wpa_s, unsigned int period,
4627 unsigned int interval)
4628{
4629 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
4630 return -1;
4631
4632 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
4633 return -1;
4634
4635 return p2p_ext_listen(wpa_s->global->p2p, period, interval);
4636}
4637
4638
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004639static int wpas_p2p_is_client(struct wpa_supplicant *wpa_s)
4640{
4641 return wpa_s->current_ssid != NULL &&
4642 wpa_s->current_ssid->p2p_group &&
4643 wpa_s->current_ssid->mode == WPAS_MODE_INFRA;
4644}
4645
4646
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004647static void wpas_p2p_group_idle_timeout(void *eloop_ctx, void *timeout_ctx)
4648{
4649 struct wpa_supplicant *wpa_s = eloop_ctx;
4650
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004651 if (wpa_s->conf->p2p_group_idle == 0 && !wpas_p2p_is_client(wpa_s)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004652 wpa_printf(MSG_DEBUG, "P2P: Ignore group idle timeout - "
4653 "disabled");
4654 return;
4655 }
4656
Dmitry Shmidt04949592012-07-19 12:16:46 -07004657 wpa_printf(MSG_DEBUG, "P2P: Group idle timeout reached - terminate "
4658 "group");
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004659 wpas_p2p_group_delete(wpa_s, P2P_GROUP_REMOVAL_IDLE_TIMEOUT);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004660}
4661
4662
4663static void wpas_p2p_set_group_idle_timeout(struct wpa_supplicant *wpa_s)
4664{
Dmitry Shmidt04949592012-07-19 12:16:46 -07004665 int timeout;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004666
Dmitry Shmidt04949592012-07-19 12:16:46 -07004667 if (eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL) > 0)
4668 wpa_printf(MSG_DEBUG, "P2P: Cancelled P2P group idle timeout");
4669
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004670 if (wpa_s->current_ssid == NULL || !wpa_s->current_ssid->p2p_group)
4671 return;
4672
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004673 timeout = wpa_s->conf->p2p_group_idle;
4674 if (wpa_s->current_ssid->mode == WPAS_MODE_INFRA &&
4675 (timeout == 0 || timeout > P2P_MAX_CLIENT_IDLE))
4676 timeout = P2P_MAX_CLIENT_IDLE;
4677
4678 if (timeout == 0)
4679 return;
4680
Dmitry Shmidt04949592012-07-19 12:16:46 -07004681 if (timeout < 0) {
4682 if (wpa_s->current_ssid->mode == WPAS_MODE_INFRA)
4683 timeout = 0; /* special client mode no-timeout */
4684 else
4685 return;
4686 }
4687
4688 if (wpa_s->p2p_in_provisioning) {
4689 /*
4690 * Use the normal group formation timeout during the
4691 * provisioning phase to avoid terminating this process too
4692 * early due to group idle timeout.
4693 */
4694 wpa_printf(MSG_DEBUG, "P2P: Do not use P2P group idle timeout "
4695 "during provisioning");
4696 return;
4697 }
Dmitry Shmidtad266fb2012-08-24 17:03:35 -07004698#ifndef ANDROID_P2P
Dmitry Shmidt04949592012-07-19 12:16:46 -07004699 if (wpa_s->show_group_started) {
4700 /*
4701 * Use the normal group formation timeout between the end of
4702 * the provisioning phase and completion of 4-way handshake to
4703 * avoid terminating this process too early due to group idle
4704 * timeout.
4705 */
4706 wpa_printf(MSG_DEBUG, "P2P: Do not use P2P group idle timeout "
4707 "while waiting for initial 4-way handshake to "
4708 "complete");
4709 return;
4710 }
Dmitry Shmidtad266fb2012-08-24 17:03:35 -07004711#endif
Dmitry Shmidt04949592012-07-19 12:16:46 -07004712
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004713 wpa_printf(MSG_DEBUG, "P2P: Set P2P group idle timeout to %u seconds",
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004714 timeout);
4715 eloop_register_timeout(timeout, 0, wpas_p2p_group_idle_timeout,
4716 wpa_s, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004717}
4718
4719
Jouni Malinen2b89da82012-08-31 22:04:41 +03004720/* Returns 1 if the interface was removed */
4721int wpas_p2p_deauth_notif(struct wpa_supplicant *wpa_s, const u8 *bssid,
4722 u16 reason_code, const u8 *ie, size_t ie_len,
4723 int locally_generated)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004724{
4725 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
Jouni Malinen2b89da82012-08-31 22:04:41 +03004726 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004727 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
Jouni Malinen2b89da82012-08-31 22:04:41 +03004728 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004729
Dmitry Shmidt04949592012-07-19 12:16:46 -07004730 if (!locally_generated)
4731 p2p_deauth_notif(wpa_s->global->p2p, bssid, reason_code, ie,
4732 ie_len);
4733
4734 if (reason_code == WLAN_REASON_DEAUTH_LEAVING && !locally_generated &&
4735 wpa_s->current_ssid &&
4736 wpa_s->current_ssid->p2p_group &&
4737 wpa_s->current_ssid->mode == WPAS_MODE_INFRA) {
4738 wpa_printf(MSG_DEBUG, "P2P: GO indicated that the P2P Group "
4739 "session is ending");
Jouni Malinen2b89da82012-08-31 22:04:41 +03004740 if (wpas_p2p_group_delete(wpa_s,
4741 P2P_GROUP_REMOVAL_GO_ENDING_SESSION)
4742 > 0)
4743 return 1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07004744 }
Jouni Malinen2b89da82012-08-31 22:04:41 +03004745
4746 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004747}
4748
4749
4750void wpas_p2p_disassoc_notif(struct wpa_supplicant *wpa_s, const u8 *bssid,
Dmitry Shmidt04949592012-07-19 12:16:46 -07004751 u16 reason_code, const u8 *ie, size_t ie_len,
4752 int locally_generated)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004753{
4754 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
4755 return;
4756 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
4757 return;
4758
Dmitry Shmidt04949592012-07-19 12:16:46 -07004759 if (!locally_generated)
4760 p2p_disassoc_notif(wpa_s->global->p2p, bssid, reason_code, ie,
4761 ie_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004762}
4763
4764
4765void wpas_p2p_update_config(struct wpa_supplicant *wpa_s)
4766{
4767 struct p2p_data *p2p = wpa_s->global->p2p;
4768
4769 if (p2p == NULL)
4770 return;
4771
4772 if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_CAPABLE))
4773 return;
4774
4775 if (wpa_s->conf->changed_parameters & CFG_CHANGED_DEVICE_NAME)
4776 p2p_set_dev_name(p2p, wpa_s->conf->device_name);
4777
4778 if (wpa_s->conf->changed_parameters & CFG_CHANGED_DEVICE_TYPE)
4779 p2p_set_pri_dev_type(p2p, wpa_s->conf->device_type);
4780
4781 if (wpa_s->wps &&
4782 (wpa_s->conf->changed_parameters & CFG_CHANGED_CONFIG_METHODS))
4783 p2p_set_config_methods(p2p, wpa_s->wps->config_methods);
4784
4785 if (wpa_s->wps && (wpa_s->conf->changed_parameters & CFG_CHANGED_UUID))
4786 p2p_set_uuid(p2p, wpa_s->wps->uuid);
4787
4788 if (wpa_s->conf->changed_parameters & CFG_CHANGED_WPS_STRING) {
4789 p2p_set_manufacturer(p2p, wpa_s->conf->manufacturer);
4790 p2p_set_model_name(p2p, wpa_s->conf->model_name);
4791 p2p_set_model_number(p2p, wpa_s->conf->model_number);
4792 p2p_set_serial_number(p2p, wpa_s->conf->serial_number);
4793 }
4794
4795 if (wpa_s->conf->changed_parameters & CFG_CHANGED_SEC_DEVICE_TYPE)
4796 p2p_set_sec_dev_types(p2p,
4797 (void *) wpa_s->conf->sec_device_type,
4798 wpa_s->conf->num_sec_device_types);
4799
4800 if (wpa_s->conf->changed_parameters & CFG_CHANGED_VENDOR_EXTENSION) {
4801 int i;
4802 p2p_remove_wps_vendor_extensions(p2p);
4803 for (i = 0; i < MAX_WPS_VENDOR_EXT; i++) {
4804 if (wpa_s->conf->wps_vendor_ext[i] == NULL)
4805 continue;
4806 p2p_add_wps_vendor_extension(
4807 p2p, wpa_s->conf->wps_vendor_ext[i]);
4808 }
4809 }
4810
4811 if ((wpa_s->conf->changed_parameters & CFG_CHANGED_COUNTRY) &&
4812 wpa_s->conf->country[0] && wpa_s->conf->country[1]) {
4813 char country[3];
4814 country[0] = wpa_s->conf->country[0];
4815 country[1] = wpa_s->conf->country[1];
4816 country[2] = 0x04;
4817 p2p_set_country(p2p, country);
4818 }
4819
4820 if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_SSID_POSTFIX) {
4821 p2p_set_ssid_postfix(p2p, (u8 *) wpa_s->conf->p2p_ssid_postfix,
4822 wpa_s->conf->p2p_ssid_postfix ?
4823 os_strlen(wpa_s->conf->p2p_ssid_postfix) :
4824 0);
4825 }
4826
4827 if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_INTRA_BSS)
4828 p2p_set_intra_bss_dist(p2p, wpa_s->conf->p2p_intra_bss);
Jouni Malinen75ecf522011-06-27 15:19:46 -07004829
4830 if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_LISTEN_CHANNEL) {
4831 u8 reg_class, channel;
4832 int ret;
4833 unsigned int r;
4834 if (wpa_s->conf->p2p_listen_reg_class &&
4835 wpa_s->conf->p2p_listen_channel) {
4836 reg_class = wpa_s->conf->p2p_listen_reg_class;
4837 channel = wpa_s->conf->p2p_listen_channel;
4838 } else {
4839 reg_class = 81;
4840 /*
4841 * Pick one of the social channels randomly as the
4842 * listen channel.
4843 */
4844 os_get_random((u8 *) &r, sizeof(r));
4845 channel = 1 + (r % 3) * 5;
4846 }
4847 ret = p2p_set_listen_channel(p2p, reg_class, channel);
4848 if (ret)
4849 wpa_printf(MSG_ERROR, "P2P: Own listen channel update "
4850 "failed: %d", ret);
4851 }
4852 if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_OPER_CHANNEL) {
4853 u8 op_reg_class, op_channel, cfg_op_channel;
4854 int ret = 0;
4855 unsigned int r;
4856 if (wpa_s->conf->p2p_oper_reg_class &&
4857 wpa_s->conf->p2p_oper_channel) {
4858 op_reg_class = wpa_s->conf->p2p_oper_reg_class;
4859 op_channel = wpa_s->conf->p2p_oper_channel;
4860 cfg_op_channel = 1;
4861 } else {
4862 op_reg_class = 81;
4863 /*
4864 * Use random operation channel from (1, 6, 11)
4865 *if no other preference is indicated.
4866 */
4867 os_get_random((u8 *) &r, sizeof(r));
4868 op_channel = 1 + (r % 3) * 5;
4869 cfg_op_channel = 0;
4870 }
4871 ret = p2p_set_oper_channel(p2p, op_reg_class, op_channel,
4872 cfg_op_channel);
4873 if (ret)
4874 wpa_printf(MSG_ERROR, "P2P: Own oper channel update "
4875 "failed: %d", ret);
4876 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07004877
4878 if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_PREF_CHAN) {
4879 if (p2p_set_pref_chan(p2p, wpa_s->conf->num_p2p_pref_chan,
4880 wpa_s->conf->p2p_pref_chan) < 0) {
4881 wpa_printf(MSG_ERROR, "P2P: Preferred channel list "
4882 "update failed");
4883 }
4884 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004885}
4886
4887
4888int wpas_p2p_set_noa(struct wpa_supplicant *wpa_s, u8 count, int start,
4889 int duration)
4890{
4891 if (!wpa_s->ap_iface)
4892 return -1;
4893 return hostapd_p2p_set_noa(wpa_s->ap_iface->bss[0], count, start,
4894 duration);
4895}
4896
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004897
4898int wpas_p2p_set_cross_connect(struct wpa_supplicant *wpa_s, int enabled)
4899{
4900 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
4901 return -1;
4902 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
4903 return -1;
4904
4905 wpa_s->global->cross_connection = enabled;
4906 p2p_set_cross_connect(wpa_s->global->p2p, enabled);
4907
4908 if (!enabled) {
4909 struct wpa_supplicant *iface;
4910
4911 for (iface = wpa_s->global->ifaces; iface; iface = iface->next)
4912 {
4913 if (iface->cross_connect_enabled == 0)
4914 continue;
4915
4916 iface->cross_connect_enabled = 0;
4917 iface->cross_connect_in_use = 0;
4918 wpa_msg(iface->parent, MSG_INFO,
4919 P2P_EVENT_CROSS_CONNECT_DISABLE "%s %s",
4920 iface->ifname, iface->cross_connect_uplink);
4921 }
4922 }
4923
4924 return 0;
4925}
4926
4927
4928static void wpas_p2p_enable_cross_connect(struct wpa_supplicant *uplink)
4929{
4930 struct wpa_supplicant *iface;
4931
4932 if (!uplink->global->cross_connection)
4933 return;
4934
4935 for (iface = uplink->global->ifaces; iface; iface = iface->next) {
4936 if (!iface->cross_connect_enabled)
4937 continue;
4938 if (os_strcmp(uplink->ifname, iface->cross_connect_uplink) !=
4939 0)
4940 continue;
4941 if (iface->ap_iface == NULL)
4942 continue;
4943 if (iface->cross_connect_in_use)
4944 continue;
4945
4946 iface->cross_connect_in_use = 1;
4947 wpa_msg(iface->parent, MSG_INFO,
4948 P2P_EVENT_CROSS_CONNECT_ENABLE "%s %s",
4949 iface->ifname, iface->cross_connect_uplink);
4950 }
4951}
4952
4953
4954static void wpas_p2p_disable_cross_connect(struct wpa_supplicant *uplink)
4955{
4956 struct wpa_supplicant *iface;
4957
4958 for (iface = uplink->global->ifaces; iface; iface = iface->next) {
4959 if (!iface->cross_connect_enabled)
4960 continue;
4961 if (os_strcmp(uplink->ifname, iface->cross_connect_uplink) !=
4962 0)
4963 continue;
4964 if (!iface->cross_connect_in_use)
4965 continue;
4966
4967 wpa_msg(iface->parent, MSG_INFO,
4968 P2P_EVENT_CROSS_CONNECT_DISABLE "%s %s",
4969 iface->ifname, iface->cross_connect_uplink);
4970 iface->cross_connect_in_use = 0;
4971 }
4972}
4973
4974
4975void wpas_p2p_notif_connected(struct wpa_supplicant *wpa_s)
4976{
4977 if (wpa_s->ap_iface || wpa_s->current_ssid == NULL ||
4978 wpa_s->current_ssid->mode != WPAS_MODE_INFRA ||
4979 wpa_s->cross_connect_disallowed)
4980 wpas_p2p_disable_cross_connect(wpa_s);
4981 else
4982 wpas_p2p_enable_cross_connect(wpa_s);
Dmitry Shmidt04949592012-07-19 12:16:46 -07004983 if (!wpa_s->ap_iface &&
4984 eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL) > 0)
4985 wpa_printf(MSG_DEBUG, "P2P: Cancelled P2P group idle timeout");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004986}
4987
4988
4989void wpas_p2p_notif_disconnected(struct wpa_supplicant *wpa_s)
4990{
4991 wpas_p2p_disable_cross_connect(wpa_s);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004992 if (!wpa_s->ap_iface &&
4993 !eloop_is_timeout_registered(wpas_p2p_group_idle_timeout,
4994 wpa_s, NULL))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004995 wpas_p2p_set_group_idle_timeout(wpa_s);
4996}
4997
4998
4999static void wpas_p2p_cross_connect_setup(struct wpa_supplicant *wpa_s)
5000{
5001 struct wpa_supplicant *iface;
5002
5003 if (!wpa_s->global->cross_connection)
5004 return;
5005
5006 for (iface = wpa_s->global->ifaces; iface; iface = iface->next) {
5007 if (iface == wpa_s)
5008 continue;
5009 if (iface->drv_flags &
5010 WPA_DRIVER_FLAGS_P2P_DEDICATED_INTERFACE)
5011 continue;
5012 if (iface->drv_flags & WPA_DRIVER_FLAGS_P2P_CAPABLE)
5013 continue;
5014
5015 wpa_s->cross_connect_enabled = 1;
5016 os_strlcpy(wpa_s->cross_connect_uplink, iface->ifname,
5017 sizeof(wpa_s->cross_connect_uplink));
5018 wpa_printf(MSG_DEBUG, "P2P: Enable cross connection from "
5019 "%s to %s whenever uplink is available",
5020 wpa_s->ifname, wpa_s->cross_connect_uplink);
5021
5022 if (iface->ap_iface || iface->current_ssid == NULL ||
5023 iface->current_ssid->mode != WPAS_MODE_INFRA ||
5024 iface->cross_connect_disallowed ||
5025 iface->wpa_state != WPA_COMPLETED)
5026 break;
5027
5028 wpa_s->cross_connect_in_use = 1;
5029 wpa_msg(wpa_s->parent, MSG_INFO,
5030 P2P_EVENT_CROSS_CONNECT_ENABLE "%s %s",
5031 wpa_s->ifname, wpa_s->cross_connect_uplink);
5032 break;
5033 }
5034}
5035
5036
5037int wpas_p2p_notif_pbc_overlap(struct wpa_supplicant *wpa_s)
5038{
5039 if (wpa_s->p2p_group_interface != P2P_GROUP_INTERFACE_CLIENT &&
5040 !wpa_s->p2p_in_provisioning)
5041 return 0; /* not P2P client operation */
5042
5043 wpa_printf(MSG_DEBUG, "P2P: Terminate connection due to WPS PBC "
5044 "session overlap");
5045 if (wpa_s != wpa_s->parent)
5046 wpa_msg_ctrl(wpa_s->parent, MSG_INFO, WPS_EVENT_OVERLAP);
5047
5048 if (wpa_s->global->p2p)
5049 p2p_group_formation_failed(wpa_s->global->p2p);
5050
5051 eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
5052 wpa_s->parent, NULL);
5053
5054 wpas_group_formation_completed(wpa_s, 0);
5055 return 1;
5056}
5057
5058
5059void wpas_p2p_update_channel_list(struct wpa_supplicant *wpa_s)
5060{
5061 struct p2p_channels chan;
5062
5063 if (wpa_s->global == NULL || wpa_s->global->p2p == NULL)
5064 return;
5065
5066 os_memset(&chan, 0, sizeof(chan));
5067 if (wpas_p2p_setup_channels(wpa_s, &chan)) {
5068 wpa_printf(MSG_ERROR, "P2P: Failed to update supported "
5069 "channel list");
5070 return;
5071 }
5072
5073 p2p_update_channel_list(wpa_s->global->p2p, &chan);
5074}
5075
5076
5077int wpas_p2p_cancel(struct wpa_supplicant *wpa_s)
5078{
5079 struct wpa_global *global = wpa_s->global;
5080 int found = 0;
5081 const u8 *peer;
5082
5083 if (global->p2p == NULL)
5084 return -1;
5085
5086 wpa_printf(MSG_DEBUG, "P2P: Request to cancel group formation");
5087
5088 if (wpa_s->pending_interface_name[0] &&
5089 !is_zero_ether_addr(wpa_s->pending_interface_addr))
5090 found = 1;
5091
5092 peer = p2p_get_go_neg_peer(global->p2p);
5093 if (peer) {
5094 wpa_printf(MSG_DEBUG, "P2P: Unauthorize pending GO Neg peer "
5095 MACSTR, MAC2STR(peer));
5096 p2p_unauthorize(global->p2p, peer);
Dmitry Shmidt04949592012-07-19 12:16:46 -07005097 found = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005098 }
5099
5100 wpas_p2p_stop_find(wpa_s);
5101
5102 for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
5103 if (wpa_s == global->p2p_group_formation &&
5104 (wpa_s->p2p_in_provisioning ||
5105 wpa_s->parent->pending_interface_type ==
5106 WPA_IF_P2P_CLIENT)) {
5107 wpa_printf(MSG_DEBUG, "P2P: Interface %s in group "
5108 "formation found - cancelling",
5109 wpa_s->ifname);
5110 found = 1;
5111 eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
5112 wpa_s->parent, NULL);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005113 wpas_p2p_group_delete(wpa_s,
5114 P2P_GROUP_REMOVAL_REQUESTED);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005115 break;
5116 }
5117 }
5118
5119 if (!found) {
5120 wpa_printf(MSG_DEBUG, "P2P: No ongoing group formation found");
5121 return -1;
5122 }
5123
5124 return 0;
5125}
5126
5127
5128void wpas_p2p_interface_unavailable(struct wpa_supplicant *wpa_s)
5129{
5130 if (wpa_s->current_ssid == NULL || !wpa_s->current_ssid->p2p_group)
5131 return;
5132
5133 wpa_printf(MSG_DEBUG, "P2P: Remove group due to driver resource not "
5134 "being available anymore");
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005135 wpas_p2p_group_delete(wpa_s, P2P_GROUP_REMOVAL_UNAVAILABLE);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005136}
5137
5138
5139void wpas_p2p_update_best_channels(struct wpa_supplicant *wpa_s,
5140 int freq_24, int freq_5, int freq_overall)
5141{
5142 struct p2p_data *p2p = wpa_s->global->p2p;
5143 if (p2p == NULL || (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT))
5144 return;
5145 p2p_set_best_channels(p2p, freq_24, freq_5, freq_overall);
5146}
5147
5148
5149int wpas_p2p_unauthorize(struct wpa_supplicant *wpa_s, const char *addr)
5150{
5151 u8 peer[ETH_ALEN];
5152 struct p2p_data *p2p = wpa_s->global->p2p;
5153
5154 if (p2p == NULL || (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT))
5155 return -1;
5156
5157 if (hwaddr_aton(addr, peer))
5158 return -1;
5159
5160 return p2p_unauthorize(p2p, peer);
5161}
5162
5163
5164/**
5165 * wpas_p2p_disconnect - Disconnect from a P2P Group
5166 * @wpa_s: Pointer to wpa_supplicant data
5167 * Returns: 0 on success, -1 on failure
5168 *
5169 * This can be used to disconnect from a group in which the local end is a P2P
5170 * Client or to end a P2P Group in case the local end is the Group Owner. If a
5171 * virtual network interface was created for this group, that interface will be
5172 * removed. Otherwise, only the configured P2P group network will be removed
5173 * from the interface.
5174 */
5175int wpas_p2p_disconnect(struct wpa_supplicant *wpa_s)
5176{
5177
5178 if (wpa_s == NULL)
5179 return -1;
5180
Jouni Malinen2b89da82012-08-31 22:04:41 +03005181 return wpas_p2p_group_delete(wpa_s, P2P_GROUP_REMOVAL_REQUESTED) < 0 ?
5182 -1 : 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005183}
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005184
5185
5186int wpas_p2p_in_progress(struct wpa_supplicant *wpa_s)
5187{
Dmitry Shmidt4b86ea52012-09-04 11:06:50 -07005188#ifdef ANDROID_P2P
5189 struct wpa_supplicant *group = wpa_s;
5190#endif
5191
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005192 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
5193 return 0;
5194
Dmitry Shmidt4b86ea52012-09-04 11:06:50 -07005195#ifdef ANDROID_P2P
5196 while (group && (group->p2p_group_interface != NOT_P2P_GROUP_INTERFACE)) {
5197 if(group->wpa_state == WPA_ASSOCIATED) {
5198 /* WPA_ASSOCIATED hasn't moved to WPA_COMPLETED. So it could be in WPS
5199 * or 4Way Hanshake phase. Avoid allowing scan during this time critical
5200 * phase
5201 */
5202 wpa_printf(MSG_ERROR, "P2P: WPS/4way handshake in Progress."
5203 " Defer SCAN ");
5204 return 1;
5205 }
5206 group = group->next;
5207 }
5208#endif
5209
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005210 return p2p_in_progress(wpa_s->global->p2p);
5211}
5212
5213
5214void wpas_p2p_network_removed(struct wpa_supplicant *wpa_s,
5215 struct wpa_ssid *ssid)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005216{
5217 if (wpa_s->p2p_in_provisioning && ssid->p2p_group &&
5218 eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
5219 wpa_s->parent, NULL) > 0) {
Dmitry Shmidt04949592012-07-19 12:16:46 -07005220 /**
5221 * Remove the network by scheduling the group formation
5222 * timeout to happen immediately. The teardown code
5223 * needs to be scheduled to run asynch later so that we
5224 * don't delete data from under ourselves unexpectedly.
5225 * Calling wpas_p2p_group_formation_timeout directly
5226 * causes a series of crashes in WPS failure scenarios.
5227 */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005228 wpa_printf(MSG_DEBUG, "P2P: Canceled group formation due to "
5229 "P2P group network getting removed");
Dmitry Shmidt04949592012-07-19 12:16:46 -07005230 eloop_register_timeout(0, 0, wpas_p2p_group_formation_timeout,
5231 wpa_s->parent, NULL);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005232 }
5233}
5234
5235
5236struct wpa_ssid * wpas_p2p_get_persistent(struct wpa_supplicant *wpa_s,
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08005237 const u8 *addr, const u8 *ssid,
5238 size_t ssid_len)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005239{
5240 struct wpa_ssid *s;
5241 size_t i;
5242
5243 for (s = wpa_s->conf->ssid; s; s = s->next) {
5244 if (s->disabled != 2)
5245 continue;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08005246 if (ssid &&
5247 (ssid_len != s->ssid_len ||
5248 os_memcmp(ssid, s->ssid, ssid_len) != 0))
5249 continue;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005250 if (os_memcmp(s->bssid, addr, ETH_ALEN) == 0)
5251 return s; /* peer is GO in the persistent group */
5252 if (s->mode != WPAS_MODE_P2P_GO || s->p2p_client_list == NULL)
5253 continue;
5254 for (i = 0; i < s->num_p2p_clients; i++) {
5255 if (os_memcmp(s->p2p_client_list + i * ETH_ALEN,
5256 addr, ETH_ALEN) == 0)
5257 return s; /* peer is P2P client in persistent
5258 * group */
5259 }
5260 }
5261
5262 return NULL;
5263}
5264
5265
5266void wpas_p2p_notify_ap_sta_authorized(struct wpa_supplicant *wpa_s,
5267 const u8 *addr)
5268{
5269 if (addr == NULL)
5270 return;
5271 wpas_p2p_add_persistent_group_client(wpa_s, addr);
5272}
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08005273
Dmitry Shmidt04949592012-07-19 12:16:46 -07005274
5275static void wpas_p2p_fallback_to_go_neg(struct wpa_supplicant *wpa_s,
5276 int group_added)
5277{
5278 struct wpa_supplicant *group = wpa_s;
5279 eloop_cancel_timeout(wpas_p2p_pd_before_join_timeout, wpa_s, NULL);
5280 if (wpa_s->global->p2p_group_formation)
5281 group = wpa_s->global->p2p_group_formation;
5282 wpa_s = wpa_s->parent;
5283 offchannel_send_action_done(wpa_s);
5284 if (group_added)
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005285 wpas_p2p_group_delete(group, P2P_GROUP_REMOVAL_SILENT);
Dmitry Shmidt04949592012-07-19 12:16:46 -07005286 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Fall back to GO Negotiation");
5287 wpas_p2p_connect(wpa_s, wpa_s->pending_join_dev_addr, wpa_s->p2p_pin,
5288 wpa_s->p2p_wps_method, wpa_s->p2p_persistent_group, 0,
5289 0, 0, wpa_s->p2p_go_intent, wpa_s->p2p_connect_freq,
5290 wpa_s->p2p_persistent_id,
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005291 wpa_s->p2p_pd_before_go_neg,
5292 wpa_s->p2p_go_ht40);
Dmitry Shmidt04949592012-07-19 12:16:46 -07005293}
5294
5295
5296int wpas_p2p_scan_no_go_seen(struct wpa_supplicant *wpa_s)
5297{
5298 if (!wpa_s->p2p_fallback_to_go_neg ||
5299 wpa_s->p2p_in_provisioning <= 5)
5300 return 0;
5301
5302 if (wpas_p2p_peer_go(wpa_s, wpa_s->pending_join_dev_addr) > 0)
5303 return 0; /* peer operating as a GO */
5304
5305 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: GO not found for p2p_connect-auto - "
5306 "fallback to GO Negotiation");
5307 wpas_p2p_fallback_to_go_neg(wpa_s, 1);
5308
5309 return 1;
5310}
5311
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005312
5313unsigned int wpas_p2p_search_delay(struct wpa_supplicant *wpa_s)
5314{
5315 const char *rn, *rn2;
5316 struct wpa_supplicant *ifs;
5317
5318 if (wpa_s->wpa_state > WPA_SCANNING) {
5319 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Use %u ms search delay due to "
5320 "concurrent operation",
5321 P2P_CONCURRENT_SEARCH_DELAY);
5322 return P2P_CONCURRENT_SEARCH_DELAY;
5323 }
5324
5325 if (!wpa_s->driver->get_radio_name)
5326 return 0;
5327 rn = wpa_s->driver->get_radio_name(wpa_s->drv_priv);
5328 if (rn == NULL || rn[0] == '\0')
5329 return 0;
5330
5331 for (ifs = wpa_s->global->ifaces; ifs; ifs = ifs->next) {
5332 if (ifs == wpa_s || !ifs->driver->get_radio_name)
5333 continue;
5334
5335 rn2 = ifs->driver->get_radio_name(ifs->drv_priv);
5336 if (!rn2 || os_strcmp(rn, rn2) != 0)
5337 continue;
5338 if (ifs->wpa_state > WPA_SCANNING) {
5339 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Use %u ms search "
5340 "delay due to concurrent operation on "
5341 "interface %s",
5342 P2P_CONCURRENT_SEARCH_DELAY, ifs->ifname);
5343 return P2P_CONCURRENT_SEARCH_DELAY;
5344 }
5345 }
5346
5347 return 0;
5348}
5349
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08005350#ifdef ANDROID_P2P
5351int wpas_p2p_handle_frequency_conflicts(struct wpa_supplicant *wpa_s, int freq)
5352{
5353 struct wpa_supplicant *iface = NULL;
5354 struct p2p_data *p2p = wpa_s->global->p2p;
5355
5356 for (iface = wpa_s->global->ifaces; iface; iface = iface->next) {
5357 if((iface->p2p_group_interface) && (iface->current_ssid) &&
5358 (iface->current_ssid->frequency != freq)) {
5359
5360 if (iface->p2p_group_interface == P2P_GROUP_INTERFACE_GO) {
5361 /* Try to see whether we can move the GO. If it
5362 * is not possible, remove the GO interface
5363 */
5364 if(wpa_drv_switch_channel(iface, freq) == 0) {
5365 wpa_printf(MSG_ERROR, "P2P: GO Moved to freq(%d)", freq);
5366 iface->current_ssid->frequency = freq;
5367 continue;
5368 }
5369 }
5370
5371 /* If GO cannot be moved or if the conflicting interface is a
5372 * P2P Client, remove the interface depending up on the connection
5373 * priority */
Dmitry Shmidt687922c2012-03-26 14:02:32 -07005374 if(!wpas_is_p2p_prioritized(wpa_s)) {
5375 /* STA connection has priority over existing
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08005376 * P2P connection. So remove the interface */
Dmitry Shmidt687922c2012-03-26 14:02:32 -07005377 wpa_printf(MSG_DEBUG, "P2P: Removing P2P connection due to Single channel"
5378 "concurrent mode frequency conflict");
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005379 wpas_p2p_group_delete(iface, P2P_GROUP_REMOVAL_FREQ_CONFLICT);
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08005380 } else {
5381 /* Existing connection has the priority. Disable the newly
5382 * selected network and let the application know about it.
5383 */
5384 return -1;
5385 }
5386 }
5387 }
5388 return 0;
5389}
5390#endif