blob: c2d702e205c9376fa0094ff7784a01488bdca7af [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
Dmitry Shmidt61d9df32012-08-29 16:22:06 -070065#define P2P_CONCURRENT_SEARCH_DELAY 500
Dmitry Shmidt61d9df32012-08-29 16:22:06 -070066#endif /* P2P_CONCURRENT_SEARCH_DELAY */
67
68enum p2p_group_removal_reason {
69 P2P_GROUP_REMOVAL_UNKNOWN,
70 P2P_GROUP_REMOVAL_SILENT,
71 P2P_GROUP_REMOVAL_FORMATION_FAILED,
72 P2P_GROUP_REMOVAL_REQUESTED,
73 P2P_GROUP_REMOVAL_IDLE_TIMEOUT,
74 P2P_GROUP_REMOVAL_UNAVAILABLE,
75 P2P_GROUP_REMOVAL_GO_ENDING_SESSION,
76#ifdef ANDROID_P2P
77 P2P_GROUP_REMOVAL_FREQ_CONFLICT
78#endif
79};
80
Jouni Malinendc7b7132012-09-14 12:53:47 -070081
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070082static void wpas_p2p_long_listen_timeout(void *eloop_ctx, void *timeout_ctx);
83static struct wpa_supplicant *
84wpas_p2p_get_group_iface(struct wpa_supplicant *wpa_s, int addr_allocated,
85 int go);
86static int wpas_p2p_join_start(struct wpa_supplicant *wpa_s);
Dmitry Shmidt04949592012-07-19 12:16:46 -070087static void wpas_p2p_join_scan_req(struct wpa_supplicant *wpa_s, int freq);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070088static void wpas_p2p_join_scan(void *eloop_ctx, void *timeout_ctx);
89static int wpas_p2p_join(struct wpa_supplicant *wpa_s, const u8 *iface_addr,
Dmitry Shmidt04949592012-07-19 12:16:46 -070090 const u8 *dev_addr, enum p2p_wps_method wps_method,
91 int auto_join);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070092static int wpas_p2p_create_iface(struct wpa_supplicant *wpa_s);
93static void wpas_p2p_cross_connect_setup(struct wpa_supplicant *wpa_s);
94static void wpas_p2p_group_idle_timeout(void *eloop_ctx, void *timeout_ctx);
95static void wpas_p2p_set_group_idle_timeout(struct wpa_supplicant *wpa_s);
Dmitry Shmidt04949592012-07-19 12:16:46 -070096static void wpas_p2p_fallback_to_go_neg(struct wpa_supplicant *wpa_s,
97 int group_added);
Dmitry Shmidtd5e49232012-12-03 15:08:10 -080098static int wpas_p2p_stop_find_oper(struct wpa_supplicant *wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070099
100
101static void wpas_p2p_scan_res_handler(struct wpa_supplicant *wpa_s,
102 struct wpa_scan_results *scan_res)
103{
104 size_t i;
105
106 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
107 return;
108
109 wpa_printf(MSG_DEBUG, "P2P: Scan results received (%d BSS)",
110 (int) scan_res->num);
111
112 for (i = 0; i < scan_res->num; i++) {
113 struct wpa_scan_res *bss = scan_res->res[i];
114 if (p2p_scan_res_handler(wpa_s->global->p2p, bss->bssid,
115 bss->freq, bss->level,
116 (const u8 *) (bss + 1),
117 bss->ie_len) > 0)
118 break;
119 }
120
121 p2p_scan_res_handled(wpa_s->global->p2p);
122}
123
124
125static int wpas_p2p_scan(void *ctx, enum p2p_scan_type type, int freq,
126 unsigned int num_req_dev_types,
Dmitry Shmidt04949592012-07-19 12:16:46 -0700127 const u8 *req_dev_types, const u8 *dev_id, u16 pw_id)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700128{
129 struct wpa_supplicant *wpa_s = ctx;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700130 struct wpa_supplicant *ifs;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700131 struct wpa_driver_scan_params params;
132 int ret;
133 struct wpabuf *wps_ie, *ies;
134 int social_channels[] = { 2412, 2437, 2462, 0, 0 };
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800135 size_t ielen;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700136
137 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
138 return -1;
139
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700140 for (ifs = wpa_s->global->ifaces; ifs; ifs = ifs->next) {
141 if (ifs->sta_scan_pending &&
142 wpas_p2p_in_progress(wpa_s) == 2) {
143 wpa_printf(MSG_DEBUG, "Delaying P2P scan to allow "
144 "pending station mode scan to be "
145 "completed on interface %s", ifs->ifname);
Jouni Malinendc7b7132012-09-14 12:53:47 -0700146 wpa_s->global->p2p_cb_on_scan_complete = 1;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700147 wpa_supplicant_req_scan(ifs, 0, 0);
148 return 1;
149 }
150 }
151
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700152 os_memset(&params, 0, sizeof(params));
153
154 /* P2P Wildcard SSID */
155 params.num_ssids = 1;
156 params.ssids[0].ssid = (u8 *) P2P_WILDCARD_SSID;
157 params.ssids[0].ssid_len = P2P_WILDCARD_SSID_LEN;
158
159 wpa_s->wps->dev.p2p = 1;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700160 wps_ie = wps_build_probe_req_ie(pw_id, &wpa_s->wps->dev,
161 wpa_s->wps->uuid, WPS_REQ_ENROLLEE,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700162 num_req_dev_types, req_dev_types);
163 if (wps_ie == NULL)
164 return -1;
165
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800166 ielen = p2p_scan_ie_buf_len(wpa_s->global->p2p);
167 ies = wpabuf_alloc(wpabuf_len(wps_ie) + ielen);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700168 if (ies == NULL) {
169 wpabuf_free(wps_ie);
170 return -1;
171 }
172 wpabuf_put_buf(ies, wps_ie);
173 wpabuf_free(wps_ie);
174
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -0800175 p2p_scan_ie(wpa_s->global->p2p, ies, dev_id);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700176
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800177 params.p2p_probe = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700178 params.extra_ies = wpabuf_head(ies);
179 params.extra_ies_len = wpabuf_len(ies);
180
181 switch (type) {
182 case P2P_SCAN_SOCIAL:
183 params.freqs = social_channels;
184 break;
185 case P2P_SCAN_FULL:
186 break;
Dmitry Shmidtb5e8f062012-08-08 10:56:33 -0700187 case P2P_SCAN_SPECIFIC:
188 social_channels[0] = freq;
189 social_channels[1] = 0;
190 params.freqs = social_channels;
191 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700192 case P2P_SCAN_SOCIAL_PLUS_ONE:
193 social_channels[3] = freq;
194 params.freqs = social_channels;
195 break;
196 }
197
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800198 ret = wpa_drv_scan(wpa_s, &params);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700199
200 wpabuf_free(ies);
201
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800202 if (ret) {
Jouni Malinen043a5a92012-09-13 18:03:14 -0700203 for (ifs = wpa_s->global->ifaces; ifs; ifs = ifs->next) {
204 if (ifs->scanning ||
205 ifs->scan_res_handler == wpas_p2p_scan_res_handler) {
206 wpa_s->global->p2p_cb_on_scan_complete = 1;
207 ret = 1;
208 break;
209 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800210 }
Dmitry Shmidt04949592012-07-19 12:16:46 -0700211 } else
212 wpa_s->scan_res_handler = wpas_p2p_scan_res_handler;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800213
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700214 return ret;
215}
216
217
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700218static enum wpa_driver_if_type wpas_p2p_if_type(int p2p_group_interface)
219{
220 switch (p2p_group_interface) {
221 case P2P_GROUP_INTERFACE_PENDING:
222 return WPA_IF_P2P_GROUP;
223 case P2P_GROUP_INTERFACE_GO:
224 return WPA_IF_P2P_GO;
225 case P2P_GROUP_INTERFACE_CLIENT:
226 return WPA_IF_P2P_CLIENT;
227 }
228
229 return WPA_IF_P2P_GROUP;
230}
231
232
233static struct wpa_supplicant * wpas_get_p2p_group(struct wpa_supplicant *wpa_s,
234 const u8 *ssid,
235 size_t ssid_len, int *go)
236{
237 struct wpa_ssid *s;
238
239 for (wpa_s = wpa_s->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
240 for (s = wpa_s->conf->ssid; s; s = s->next) {
241 if (s->disabled != 0 || !s->p2p_group ||
242 s->ssid_len != ssid_len ||
243 os_memcmp(ssid, s->ssid, ssid_len) != 0)
244 continue;
245 if (s->mode == WPAS_MODE_P2P_GO &&
246 s != wpa_s->current_ssid)
247 continue;
248 if (go)
249 *go = s->mode == WPAS_MODE_P2P_GO;
250 return wpa_s;
251 }
252 }
253
254 return NULL;
255}
256
257
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700258static int wpas_p2p_group_delete(struct wpa_supplicant *wpa_s,
259 enum p2p_group_removal_reason removal_reason)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700260{
261 struct wpa_ssid *ssid;
262 char *gtype;
263 const char *reason;
264
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700265 ssid = wpa_s->current_ssid;
266 if (ssid == NULL) {
267 /*
268 * The current SSID was not known, but there may still be a
Dmitry Shmidtaa532512012-09-24 10:35:31 -0700269 * pending P2P group interface waiting for provisioning or a
270 * P2P group that is trying to reconnect.
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700271 */
272 ssid = wpa_s->conf->ssid;
273 while (ssid) {
Jouni Malinen9d712832012-10-05 11:01:57 -0700274 if (ssid->p2p_group && ssid->disabled != 2)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700275 break;
276 ssid = ssid->next;
277 }
Jouni Malinen5c44edb2012-08-31 21:35:32 +0300278 if (ssid == NULL &&
279 wpa_s->p2p_group_interface == NOT_P2P_GROUP_INTERFACE)
280 {
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700281 wpa_printf(MSG_ERROR, "P2P: P2P group interface "
282 "not found");
283 return -1;
284 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700285 }
286 if (wpa_s->p2p_group_interface == P2P_GROUP_INTERFACE_GO)
287 gtype = "GO";
288 else if (wpa_s->p2p_group_interface == P2P_GROUP_INTERFACE_CLIENT ||
289 (ssid && ssid->mode == WPAS_MODE_INFRA)) {
290 wpa_s->reassociate = 0;
291 wpa_s->disconnected = 1;
292 wpa_supplicant_deauthenticate(wpa_s,
293 WLAN_REASON_DEAUTH_LEAVING);
294 gtype = "client";
295 } else
296 gtype = "GO";
297 if (wpa_s->cross_connect_in_use) {
298 wpa_s->cross_connect_in_use = 0;
299 wpa_msg(wpa_s->parent, MSG_INFO,
300 P2P_EVENT_CROSS_CONNECT_DISABLE "%s %s",
301 wpa_s->ifname, wpa_s->cross_connect_uplink);
302 }
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700303 switch (removal_reason) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700304 case P2P_GROUP_REMOVAL_REQUESTED:
305 reason = " reason=REQUESTED";
306 break;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700307 case P2P_GROUP_REMOVAL_FORMATION_FAILED:
308 reason = " reason=FORMATION_FAILED";
309 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700310 case P2P_GROUP_REMOVAL_IDLE_TIMEOUT:
311 reason = " reason=IDLE";
312 break;
313 case P2P_GROUP_REMOVAL_UNAVAILABLE:
314 reason = " reason=UNAVAILABLE";
315 break;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700316 case P2P_GROUP_REMOVAL_GO_ENDING_SESSION:
317 reason = " reason=GO_ENDING_SESSION";
318 break;
Dmitry Shmidt687922c2012-03-26 14:02:32 -0700319#ifdef ANDROID_P2P
320 case P2P_GROUP_REMOVAL_FREQ_CONFLICT:
321 reason = " reason=FREQ_CONFLICT";
322 break;
323#endif
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700324 default:
325 reason = "";
326 break;
327 }
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700328 if (removal_reason != P2P_GROUP_REMOVAL_SILENT) {
Dmitry Shmidt04949592012-07-19 12:16:46 -0700329 wpa_msg(wpa_s->parent, MSG_INFO,
330 P2P_EVENT_GROUP_REMOVED "%s %s%s",
331 wpa_s->ifname, gtype, reason);
332 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700333
Dmitry Shmidt04949592012-07-19 12:16:46 -0700334 if (eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL) > 0)
335 wpa_printf(MSG_DEBUG, "P2P: Cancelled P2P group idle timeout");
336
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700337 if (removal_reason != P2P_GROUP_REMOVAL_SILENT && ssid)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700338 wpas_notify_p2p_group_removed(wpa_s, ssid, gtype);
339
340 if (wpa_s->p2p_group_interface != NOT_P2P_GROUP_INTERFACE) {
341 struct wpa_global *global;
342 char *ifname;
343 enum wpa_driver_if_type type;
344 wpa_printf(MSG_DEBUG, "P2P: Remove group interface %s",
345 wpa_s->ifname);
346 global = wpa_s->global;
347 ifname = os_strdup(wpa_s->ifname);
348 type = wpas_p2p_if_type(wpa_s->p2p_group_interface);
Dmitry Shmidte15c7b52011-08-03 15:04:35 -0700349 wpa_supplicant_remove_iface(wpa_s->global, wpa_s, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700350 wpa_s = global->ifaces;
351 if (wpa_s && ifname)
352 wpa_drv_if_remove(wpa_s, type, ifname);
353 os_free(ifname);
Jouni Malinen2b89da82012-08-31 22:04:41 +0300354 return 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700355 }
356
357 wpa_printf(MSG_DEBUG, "P2P: Remove temporary group network");
358 if (ssid && (ssid->p2p_group ||
359 ssid->mode == WPAS_MODE_P2P_GROUP_FORMATION ||
360 (ssid->key_mgmt & WPA_KEY_MGMT_WPS))) {
361 int id = ssid->id;
Jouni Malinen75ecf522011-06-27 15:19:46 -0700362 if (ssid == wpa_s->current_ssid) {
363 wpa_sm_set_config(wpa_s->wpa, NULL);
364 eapol_sm_notify_config(wpa_s->eapol, NULL, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700365 wpa_s->current_ssid = NULL;
Jouni Malinen75ecf522011-06-27 15:19:46 -0700366 }
367 /*
368 * Networks objects created during any P2P activities are not
369 * exposed out as they might/will confuse certain non-P2P aware
370 * applications since these network objects won't behave like
371 * regular ones.
372 *
373 * Likewise, we don't send out network removed signals for such
374 * network objects.
375 */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700376 wpa_config_remove_network(wpa_s->conf, id);
377 wpa_supplicant_clear_status(wpa_s);
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -0800378 wpa_supplicant_cancel_sched_scan(wpa_s);
Dmitry Shmidt91c40cd2012-09-25 14:23:53 -0700379 wpa_s->sta_scan_pending = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700380 } else {
381 wpa_printf(MSG_DEBUG, "P2P: Temporary group network not "
382 "found");
383 }
Dmitry Shmidt04949592012-07-19 12:16:46 -0700384 if (wpa_s->ap_iface)
385 wpa_supplicant_ap_deinit(wpa_s);
386 else
387 wpa_drv_deinit_p2p_cli(wpa_s);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700388
389 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700390}
391
392
393static int wpas_p2p_persistent_group(struct wpa_supplicant *wpa_s,
394 u8 *go_dev_addr,
395 const u8 *ssid, size_t ssid_len)
396{
397 struct wpa_bss *bss;
398 const u8 *bssid;
399 struct wpabuf *p2p;
400 u8 group_capab;
401 const u8 *addr;
402
403 if (wpa_s->go_params)
404 bssid = wpa_s->go_params->peer_interface_addr;
405 else
406 bssid = wpa_s->bssid;
407
408 bss = wpa_bss_get(wpa_s, bssid, ssid, ssid_len);
409 if (bss == NULL) {
410 u8 iface_addr[ETH_ALEN];
411 if (p2p_get_interface_addr(wpa_s->global->p2p, bssid,
412 iface_addr) == 0)
413 bss = wpa_bss_get(wpa_s, iface_addr, ssid, ssid_len);
414 }
415 if (bss == NULL) {
416 wpa_printf(MSG_DEBUG, "P2P: Could not figure out whether "
417 "group is persistent - BSS " MACSTR " not found",
418 MAC2STR(bssid));
419 return 0;
420 }
421
422 p2p = wpa_bss_get_vendor_ie_multi(bss, P2P_IE_VENDOR_TYPE);
423 if (p2p == NULL) {
424 wpa_printf(MSG_DEBUG, "P2P: Could not figure out whether "
425 "group is persistent - BSS " MACSTR
426 " did not include P2P IE", MAC2STR(bssid));
427 wpa_hexdump(MSG_DEBUG, "P2P: Probe Response IEs",
428 (u8 *) (bss + 1), bss->ie_len);
429 wpa_hexdump(MSG_DEBUG, "P2P: Beacon IEs",
430 ((u8 *) bss + 1) + bss->ie_len,
431 bss->beacon_ie_len);
432 return 0;
433 }
434
435 group_capab = p2p_get_group_capab(p2p);
436 addr = p2p_get_go_dev_addr(p2p);
437 wpa_printf(MSG_DEBUG, "P2P: Checking whether group is persistent: "
438 "group_capab=0x%x", group_capab);
439 if (addr) {
440 os_memcpy(go_dev_addr, addr, ETH_ALEN);
441 wpa_printf(MSG_DEBUG, "P2P: GO Device Address " MACSTR,
442 MAC2STR(addr));
443 } else
444 os_memset(go_dev_addr, 0, ETH_ALEN);
445 wpabuf_free(p2p);
446
447 wpa_printf(MSG_DEBUG, "P2P: BSS " MACSTR " group_capab=0x%x "
448 "go_dev_addr=" MACSTR,
449 MAC2STR(bssid), group_capab, MAC2STR(go_dev_addr));
450
451 return group_capab & P2P_GROUP_CAPAB_PERSISTENT_GROUP;
452}
453
454
Jouni Malinen75ecf522011-06-27 15:19:46 -0700455static int wpas_p2p_store_persistent_group(struct wpa_supplicant *wpa_s,
456 struct wpa_ssid *ssid,
457 const u8 *go_dev_addr)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700458{
459 struct wpa_ssid *s;
460 int changed = 0;
461
462 wpa_printf(MSG_DEBUG, "P2P: Storing credentials for a persistent "
463 "group (GO Dev Addr " MACSTR ")", MAC2STR(go_dev_addr));
464 for (s = wpa_s->conf->ssid; s; s = s->next) {
465 if (s->disabled == 2 &&
466 os_memcmp(go_dev_addr, s->bssid, ETH_ALEN) == 0 &&
467 s->ssid_len == ssid->ssid_len &&
468 os_memcmp(ssid->ssid, s->ssid, ssid->ssid_len) == 0)
469 break;
470 }
471
472 if (s) {
473 wpa_printf(MSG_DEBUG, "P2P: Update existing persistent group "
474 "entry");
475 if (ssid->passphrase && !s->passphrase)
476 changed = 1;
477 else if (ssid->passphrase && s->passphrase &&
478 os_strcmp(ssid->passphrase, s->passphrase) != 0)
479 changed = 1;
480 } else {
481 wpa_printf(MSG_DEBUG, "P2P: Create a new persistent group "
482 "entry");
483 changed = 1;
484 s = wpa_config_add_network(wpa_s->conf);
485 if (s == NULL)
Jouni Malinen75ecf522011-06-27 15:19:46 -0700486 return -1;
487
488 /*
489 * Instead of network_added we emit persistent_group_added
490 * notification. Also to keep the defense checks in
491 * persistent_group obj registration method, we set the
492 * relevant flags in s to designate it as a persistent group.
493 */
494 s->p2p_group = 1;
495 s->p2p_persistent_group = 1;
496 wpas_notify_persistent_group_added(wpa_s, s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700497 wpa_config_set_network_defaults(s);
498 }
499
500 s->p2p_group = 1;
501 s->p2p_persistent_group = 1;
502 s->disabled = 2;
503 s->bssid_set = 1;
504 os_memcpy(s->bssid, go_dev_addr, ETH_ALEN);
505 s->mode = ssid->mode;
506 s->auth_alg = WPA_AUTH_ALG_OPEN;
507 s->key_mgmt = WPA_KEY_MGMT_PSK;
508 s->proto = WPA_PROTO_RSN;
509 s->pairwise_cipher = WPA_CIPHER_CCMP;
510 s->export_keys = 1;
511 if (ssid->passphrase) {
512 os_free(s->passphrase);
513 s->passphrase = os_strdup(ssid->passphrase);
514 }
515 if (ssid->psk_set) {
516 s->psk_set = 1;
517 os_memcpy(s->psk, ssid->psk, 32);
518 }
519 if (s->passphrase && !s->psk_set)
520 wpa_config_update_psk(s);
521 if (s->ssid == NULL || s->ssid_len < ssid->ssid_len) {
522 os_free(s->ssid);
523 s->ssid = os_malloc(ssid->ssid_len);
524 }
525 if (s->ssid) {
526 s->ssid_len = ssid->ssid_len;
527 os_memcpy(s->ssid, ssid->ssid, s->ssid_len);
528 }
529
530#ifndef CONFIG_NO_CONFIG_WRITE
531 if (changed && wpa_s->conf->update_config &&
532 wpa_config_write(wpa_s->confname, wpa_s->conf)) {
533 wpa_printf(MSG_DEBUG, "P2P: Failed to update configuration");
534 }
535#endif /* CONFIG_NO_CONFIG_WRITE */
Jouni Malinen75ecf522011-06-27 15:19:46 -0700536
537 return s->id;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700538}
539
540
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800541static void wpas_p2p_add_persistent_group_client(struct wpa_supplicant *wpa_s,
542 const u8 *addr)
543{
544 struct wpa_ssid *ssid, *s;
545 u8 *n;
546 size_t i;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700547 int found = 0;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800548
549 ssid = wpa_s->current_ssid;
550 if (ssid == NULL || ssid->mode != WPAS_MODE_P2P_GO ||
551 !ssid->p2p_persistent_group)
552 return;
553
554 for (s = wpa_s->parent->conf->ssid; s; s = s->next) {
555 if (s->disabled != 2 || s->mode != WPAS_MODE_P2P_GO)
556 continue;
557
558 if (s->ssid_len == ssid->ssid_len &&
559 os_memcmp(s->ssid, ssid->ssid, s->ssid_len) == 0)
560 break;
561 }
562
563 if (s == NULL)
564 return;
565
566 for (i = 0; s->p2p_client_list && i < s->num_p2p_clients; i++) {
567 if (os_memcmp(s->p2p_client_list + i * ETH_ALEN, addr,
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700568 ETH_ALEN) != 0)
569 continue;
570
571 if (i == s->num_p2p_clients - 1)
572 return; /* already the most recent entry */
573
574 /* move the entry to mark it most recent */
575 os_memmove(s->p2p_client_list + i * ETH_ALEN,
576 s->p2p_client_list + (i + 1) * ETH_ALEN,
577 (s->num_p2p_clients - i - 1) * ETH_ALEN);
578 os_memcpy(s->p2p_client_list +
579 (s->num_p2p_clients - 1) * ETH_ALEN, addr, ETH_ALEN);
580 found = 1;
581 break;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800582 }
583
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700584 if (!found && s->num_p2p_clients < P2P_MAX_STORED_CLIENTS) {
585 n = os_realloc_array(s->p2p_client_list,
586 s->num_p2p_clients + 1, ETH_ALEN);
587 if (n == NULL)
588 return;
589 os_memcpy(n + s->num_p2p_clients * ETH_ALEN, addr, ETH_ALEN);
590 s->p2p_client_list = n;
591 s->num_p2p_clients++;
592 } else if (!found) {
593 /* Not enough room for an additional entry - drop the oldest
594 * entry */
595 os_memmove(s->p2p_client_list,
596 s->p2p_client_list + ETH_ALEN,
597 (s->num_p2p_clients - 1) * ETH_ALEN);
598 os_memcpy(s->p2p_client_list +
599 (s->num_p2p_clients - 1) * ETH_ALEN,
600 addr, ETH_ALEN);
601 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800602
603#ifndef CONFIG_NO_CONFIG_WRITE
604 if (wpa_s->parent->conf->update_config &&
605 wpa_config_write(wpa_s->parent->confname, wpa_s->parent->conf))
606 wpa_printf(MSG_DEBUG, "P2P: Failed to update configuration");
607#endif /* CONFIG_NO_CONFIG_WRITE */
608}
609
610
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700611static void wpas_group_formation_completed(struct wpa_supplicant *wpa_s,
612 int success)
613{
614 struct wpa_ssid *ssid;
615 const char *ssid_txt;
616 int client;
617 int persistent;
618 u8 go_dev_addr[ETH_ALEN];
Jouni Malinen75ecf522011-06-27 15:19:46 -0700619 int network_id = -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700620
621 /*
622 * This callback is likely called for the main interface. Update wpa_s
623 * to use the group interface if a new interface was created for the
624 * group.
625 */
626 if (wpa_s->global->p2p_group_formation)
627 wpa_s = wpa_s->global->p2p_group_formation;
628 wpa_s->global->p2p_group_formation = NULL;
629 wpa_s->p2p_in_provisioning = 0;
630
631 if (!success) {
632 wpa_msg(wpa_s->parent, MSG_INFO,
633 P2P_EVENT_GROUP_FORMATION_FAILURE);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700634 wpas_p2p_group_delete(wpa_s,
635 P2P_GROUP_REMOVAL_FORMATION_FAILED);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700636 return;
637 }
638
639 wpa_msg(wpa_s->parent, MSG_INFO, P2P_EVENT_GROUP_FORMATION_SUCCESS);
640
641 ssid = wpa_s->current_ssid;
642 if (ssid && ssid->mode == WPAS_MODE_P2P_GROUP_FORMATION) {
643 ssid->mode = WPAS_MODE_P2P_GO;
644 p2p_group_notif_formation_done(wpa_s->p2p_group);
645 wpa_supplicant_ap_mac_addr_filter(wpa_s, NULL);
646 }
647
648 persistent = 0;
649 if (ssid) {
650 ssid_txt = wpa_ssid_txt(ssid->ssid, ssid->ssid_len);
651 client = ssid->mode == WPAS_MODE_INFRA;
652 if (ssid->mode == WPAS_MODE_P2P_GO) {
653 persistent = ssid->p2p_persistent_group;
Dmitry Shmidt497c1d52011-07-21 15:19:46 -0700654 os_memcpy(go_dev_addr, wpa_s->global->p2p_dev_addr,
655 ETH_ALEN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700656 } else
657 persistent = wpas_p2p_persistent_group(wpa_s,
658 go_dev_addr,
659 ssid->ssid,
660 ssid->ssid_len);
661 } else {
662 ssid_txt = "";
663 client = wpa_s->p2p_group_interface ==
664 P2P_GROUP_INTERFACE_CLIENT;
665 os_memset(go_dev_addr, 0, ETH_ALEN);
666 }
667
668 wpa_s->show_group_started = 0;
669 if (client) {
670 /*
671 * Indicate event only after successfully completed 4-way
672 * handshake, i.e., when the interface is ready for data
673 * packets.
674 */
675 wpa_s->show_group_started = 1;
Dmitry Shmidt4b86ea52012-09-04 11:06:50 -0700676#ifdef ANDROID_P2P
677 /* For client Second phase of Group formation (4-way handshake) can be still pending
678 * So we need to restore wpa_s->global->p2p_group_formation */
Dmitry Shmidta2854ab2012-09-10 16:15:47 -0700679 wpa_printf(MSG_INFO, "Restoring back wpa_s->global->p2p_group_formation to wpa_s %p\n", wpa_s);
Dmitry Shmidt4b86ea52012-09-04 11:06:50 -0700680 wpa_s->global->p2p_group_formation = wpa_s;
681#endif
682
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700683 } else if (ssid && ssid->passphrase == NULL && ssid->psk_set) {
684 char psk[65];
685 wpa_snprintf_hex(psk, sizeof(psk), ssid->psk, 32);
686 wpa_msg(wpa_s->parent, MSG_INFO, P2P_EVENT_GROUP_STARTED
687 "%s GO ssid=\"%s\" freq=%d psk=%s go_dev_addr=" MACSTR
688 "%s",
689 wpa_s->ifname, ssid_txt, ssid->frequency, psk,
690 MAC2STR(go_dev_addr),
691 persistent ? " [PERSISTENT]" : "");
692 wpas_p2p_cross_connect_setup(wpa_s);
693 wpas_p2p_set_group_idle_timeout(wpa_s);
694 } else {
695 wpa_msg(wpa_s->parent, MSG_INFO, P2P_EVENT_GROUP_STARTED
696 "%s GO ssid=\"%s\" freq=%d passphrase=\"%s\" "
697 "go_dev_addr=" MACSTR "%s",
698 wpa_s->ifname, ssid_txt, ssid ? ssid->frequency : 0,
699 ssid && ssid->passphrase ? ssid->passphrase : "",
700 MAC2STR(go_dev_addr),
701 persistent ? " [PERSISTENT]" : "");
702 wpas_p2p_cross_connect_setup(wpa_s);
703 wpas_p2p_set_group_idle_timeout(wpa_s);
704 }
705
706 if (persistent)
Jouni Malinen75ecf522011-06-27 15:19:46 -0700707 network_id = wpas_p2p_store_persistent_group(wpa_s->parent,
708 ssid, go_dev_addr);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800709 if (network_id < 0 && ssid)
Jouni Malinen75ecf522011-06-27 15:19:46 -0700710 network_id = ssid->id;
711 if (!client)
712 wpas_notify_p2p_group_started(wpa_s, ssid, network_id, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700713}
714
715
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800716static void wpas_p2p_send_action_tx_status(struct wpa_supplicant *wpa_s,
717 unsigned int freq,
718 const u8 *dst, const u8 *src,
719 const u8 *bssid,
720 const u8 *data, size_t data_len,
721 enum offchannel_send_action_result
722 result)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700723{
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800724 enum p2p_send_action_result res = P2P_SEND_ACTION_SUCCESS;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700725
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700726 if (wpa_s->global->p2p == NULL || wpa_s->global->p2p_disabled)
727 return;
728 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
729 return;
730
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800731 switch (result) {
732 case OFFCHANNEL_SEND_ACTION_SUCCESS:
733 res = P2P_SEND_ACTION_SUCCESS;
734 break;
735 case OFFCHANNEL_SEND_ACTION_NO_ACK:
736 res = P2P_SEND_ACTION_NO_ACK;
737 break;
738 case OFFCHANNEL_SEND_ACTION_FAILED:
739 res = P2P_SEND_ACTION_FAILED;
740 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700741 }
742
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800743 p2p_send_action_cb(wpa_s->global->p2p, freq, dst, src, bssid, res);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700744
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -0800745 if (result != OFFCHANNEL_SEND_ACTION_SUCCESS &&
746 wpa_s->pending_pd_before_join &&
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800747 (os_memcmp(dst, wpa_s->pending_join_dev_addr, ETH_ALEN) == 0 ||
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800748 os_memcmp(dst, wpa_s->pending_join_iface_addr, ETH_ALEN) == 0) &&
749 wpa_s->p2p_fallback_to_go_neg) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700750 wpa_s->pending_pd_before_join = 0;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800751 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: No ACK for PD Req "
752 "during p2p_connect-auto");
753 wpas_p2p_fallback_to_go_neg(wpa_s, 0);
754 return;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700755 }
756}
757
758
759static int wpas_send_action(void *ctx, unsigned int freq, const u8 *dst,
760 const u8 *src, const u8 *bssid, const u8 *buf,
761 size_t len, unsigned int wait_time)
762{
763 struct wpa_supplicant *wpa_s = ctx;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800764 return offchannel_send_action(wpa_s, freq, dst, src, bssid, buf, len,
765 wait_time,
766 wpas_p2p_send_action_tx_status, 1);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700767}
768
769
770static void wpas_send_action_done(void *ctx)
771{
772 struct wpa_supplicant *wpa_s = ctx;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800773 offchannel_send_action_done(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700774}
775
776
777static int wpas_copy_go_neg_results(struct wpa_supplicant *wpa_s,
778 struct p2p_go_neg_results *params)
779{
780 if (wpa_s->go_params == NULL) {
781 wpa_s->go_params = os_malloc(sizeof(*params));
782 if (wpa_s->go_params == NULL)
783 return -1;
784 }
785 os_memcpy(wpa_s->go_params, params, sizeof(*params));
786 return 0;
787}
788
789
790static void wpas_start_wps_enrollee(struct wpa_supplicant *wpa_s,
791 struct p2p_go_neg_results *res)
792{
793 wpa_printf(MSG_DEBUG, "P2P: Start WPS Enrollee for peer " MACSTR,
794 MAC2STR(res->peer_interface_addr));
795 wpa_hexdump_ascii(MSG_DEBUG, "P2P: Start WPS Enrollee for SSID",
796 res->ssid, res->ssid_len);
797 wpa_supplicant_ap_deinit(wpa_s);
798 wpas_copy_go_neg_results(wpa_s, res);
799 if (res->wps_method == WPS_PBC)
800 wpas_wps_start_pbc(wpa_s, res->peer_interface_addr, 1);
801 else {
802 u16 dev_pw_id = DEV_PW_DEFAULT;
803 if (wpa_s->p2p_wps_method == WPS_PIN_KEYPAD)
804 dev_pw_id = DEV_PW_REGISTRAR_SPECIFIED;
805 wpas_wps_start_pin(wpa_s, res->peer_interface_addr,
806 wpa_s->p2p_pin, 1, dev_pw_id);
807 }
808}
809
810
811static void p2p_go_configured(void *ctx, void *data)
812{
813 struct wpa_supplicant *wpa_s = ctx;
814 struct p2p_go_neg_results *params = data;
815 struct wpa_ssid *ssid;
Jouni Malinen75ecf522011-06-27 15:19:46 -0700816 int network_id = -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700817
818 ssid = wpa_s->current_ssid;
819 if (ssid && ssid->mode == WPAS_MODE_P2P_GO) {
820 wpa_printf(MSG_DEBUG, "P2P: Group setup without provisioning");
821 if (wpa_s->global->p2p_group_formation == wpa_s)
822 wpa_s->global->p2p_group_formation = NULL;
823 wpa_msg(wpa_s->parent, MSG_INFO, P2P_EVENT_GROUP_STARTED
824 "%s GO ssid=\"%s\" freq=%d passphrase=\"%s\" "
825 "go_dev_addr=" MACSTR "%s",
826 wpa_s->ifname,
827 wpa_ssid_txt(ssid->ssid, ssid->ssid_len),
828 ssid->frequency,
829 params->passphrase ? params->passphrase : "",
Dmitry Shmidt497c1d52011-07-21 15:19:46 -0700830 MAC2STR(wpa_s->global->p2p_dev_addr),
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700831 params->persistent_group ? " [PERSISTENT]" : "");
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800832
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700833 if (params->persistent_group)
Jouni Malinen75ecf522011-06-27 15:19:46 -0700834 network_id = wpas_p2p_store_persistent_group(
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700835 wpa_s->parent, ssid,
Dmitry Shmidt497c1d52011-07-21 15:19:46 -0700836 wpa_s->global->p2p_dev_addr);
Jouni Malinen75ecf522011-06-27 15:19:46 -0700837 if (network_id < 0)
838 network_id = ssid->id;
839 wpas_notify_p2p_group_started(wpa_s, ssid, network_id, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700840 wpas_p2p_cross_connect_setup(wpa_s);
841 wpas_p2p_set_group_idle_timeout(wpa_s);
842 return;
843 }
844
845 wpa_printf(MSG_DEBUG, "P2P: Setting up WPS for GO provisioning");
846 if (wpa_supplicant_ap_mac_addr_filter(wpa_s,
847 params->peer_interface_addr)) {
848 wpa_printf(MSG_DEBUG, "P2P: Failed to setup MAC address "
849 "filtering");
850 return;
851 }
852 if (params->wps_method == WPS_PBC)
853 wpa_supplicant_ap_wps_pbc(wpa_s, params->peer_interface_addr,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800854 params->peer_device_addr);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700855 else if (wpa_s->p2p_pin[0])
856 wpa_supplicant_ap_wps_pin(wpa_s, params->peer_interface_addr,
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800857 wpa_s->p2p_pin, NULL, 0, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700858 os_free(wpa_s->go_params);
859 wpa_s->go_params = NULL;
860}
861
862
863static void wpas_start_wps_go(struct wpa_supplicant *wpa_s,
864 struct p2p_go_neg_results *params,
865 int group_formation)
866{
867 struct wpa_ssid *ssid;
868
Dmitry Shmidtaa532512012-09-24 10:35:31 -0700869 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Starting GO");
870 if (wpas_copy_go_neg_results(wpa_s, params) < 0) {
871 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Could not copy GO Negotiation "
872 "results");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700873 return;
Dmitry Shmidtaa532512012-09-24 10:35:31 -0700874 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700875
876 ssid = wpa_config_add_network(wpa_s->conf);
Dmitry Shmidtaa532512012-09-24 10:35:31 -0700877 if (ssid == NULL) {
878 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Could not add network for GO");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700879 return;
Dmitry Shmidtaa532512012-09-24 10:35:31 -0700880 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700881
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800882 wpa_s->show_group_started = 0;
883
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700884 wpa_config_set_network_defaults(ssid);
885 ssid->temporary = 1;
886 ssid->p2p_group = 1;
887 ssid->p2p_persistent_group = params->persistent_group;
888 ssid->mode = group_formation ? WPAS_MODE_P2P_GROUP_FORMATION :
889 WPAS_MODE_P2P_GO;
890 ssid->frequency = params->freq;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700891 ssid->ht40 = params->ht40;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700892 ssid->ssid = os_zalloc(params->ssid_len + 1);
893 if (ssid->ssid) {
894 os_memcpy(ssid->ssid, params->ssid, params->ssid_len);
895 ssid->ssid_len = params->ssid_len;
896 }
897 ssid->auth_alg = WPA_AUTH_ALG_OPEN;
898 ssid->key_mgmt = WPA_KEY_MGMT_PSK;
899 ssid->proto = WPA_PROTO_RSN;
900 ssid->pairwise_cipher = WPA_CIPHER_CCMP;
901 ssid->passphrase = os_strdup(params->passphrase);
Dmitry Shmidtaa532512012-09-24 10:35:31 -0700902 if (ssid->passphrase == NULL) {
903 wpa_msg(wpa_s, MSG_ERROR, "P2P: Failed to copy passphrase for "
904 "GO");
905 wpa_config_remove_network(wpa_s->conf, ssid->id);
906 return;
907 }
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800908 ssid->psk_set = params->psk_set;
909 if (ssid->psk_set)
910 os_memcpy(ssid->psk, params->psk, sizeof(ssid->psk));
911 else
912 wpa_config_update_psk(ssid);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700913 ssid->ap_max_inactivity = wpa_s->parent->conf->p2p_go_max_inactivity;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700914
915 wpa_s->ap_configured_cb = p2p_go_configured;
916 wpa_s->ap_configured_cb_ctx = wpa_s;
917 wpa_s->ap_configured_cb_data = wpa_s->go_params;
Jouni Malinen75ecf522011-06-27 15:19:46 -0700918 wpa_s->connect_without_scan = ssid;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700919 wpa_s->reassociate = 1;
920 wpa_s->disconnected = 0;
Dmitry Shmidtaa532512012-09-24 10:35:31 -0700921 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Request scan (that will be skipped) to "
922 "start GO)");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700923 wpa_supplicant_req_scan(wpa_s, 0, 0);
924}
925
926
927static void wpas_p2p_clone_config(struct wpa_supplicant *dst,
928 const struct wpa_supplicant *src)
929{
930 struct wpa_config *d;
931 const struct wpa_config *s;
932
933 d = dst->conf;
934 s = src->conf;
935
936#define C(n) if (s->n) d->n = os_strdup(s->n)
937 C(device_name);
938 C(manufacturer);
939 C(model_name);
940 C(model_number);
941 C(serial_number);
942 C(config_methods);
943#undef C
944
945 os_memcpy(d->device_type, s->device_type, WPS_DEV_TYPE_LEN);
946 os_memcpy(d->sec_device_type, s->sec_device_type,
947 sizeof(d->sec_device_type));
948 d->num_sec_device_types = s->num_sec_device_types;
949
950 d->p2p_group_idle = s->p2p_group_idle;
951 d->p2p_intra_bss = s->p2p_intra_bss;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800952 d->persistent_reconnect = s->persistent_reconnect;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700953 d->max_num_sta = s->max_num_sta;
954 d->pbc_in_m1 = s->pbc_in_m1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700955}
956
957
958static int wpas_p2p_add_group_interface(struct wpa_supplicant *wpa_s,
959 enum wpa_driver_if_type type)
960{
961 char ifname[120], force_ifname[120];
962
963 if (wpa_s->pending_interface_name[0]) {
964 wpa_printf(MSG_DEBUG, "P2P: Pending virtual interface exists "
965 "- skip creation of a new one");
966 if (is_zero_ether_addr(wpa_s->pending_interface_addr)) {
967 wpa_printf(MSG_DEBUG, "P2P: Pending virtual address "
968 "unknown?! ifname='%s'",
969 wpa_s->pending_interface_name);
970 return -1;
971 }
972 return 0;
973 }
974
975 os_snprintf(ifname, sizeof(ifname), "p2p-%s-%d", wpa_s->ifname,
976 wpa_s->p2p_group_idx);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800977 if (os_strlen(ifname) >= IFNAMSIZ &&
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700978 os_strlen(wpa_s->ifname) < IFNAMSIZ) {
979 /* Try to avoid going over the IFNAMSIZ length limit */
980 os_snprintf(ifname, sizeof(ifname), "p2p-%d",
981 wpa_s->p2p_group_idx);
982 }
983 force_ifname[0] = '\0';
984
985 wpa_printf(MSG_DEBUG, "P2P: Create a new interface %s for the group",
986 ifname);
987 wpa_s->p2p_group_idx++;
988
989 wpa_s->pending_interface_type = type;
990 if (wpa_drv_if_add(wpa_s, type, ifname, NULL, NULL, force_ifname,
991 wpa_s->pending_interface_addr, NULL) < 0) {
992 wpa_printf(MSG_ERROR, "P2P: Failed to create new group "
993 "interface");
994 return -1;
995 }
996
997 if (force_ifname[0]) {
998 wpa_printf(MSG_DEBUG, "P2P: Driver forced interface name %s",
999 force_ifname);
1000 os_strlcpy(wpa_s->pending_interface_name, force_ifname,
1001 sizeof(wpa_s->pending_interface_name));
1002 } else
1003 os_strlcpy(wpa_s->pending_interface_name, ifname,
1004 sizeof(wpa_s->pending_interface_name));
1005 wpa_printf(MSG_DEBUG, "P2P: Created pending virtual interface %s addr "
1006 MACSTR, wpa_s->pending_interface_name,
1007 MAC2STR(wpa_s->pending_interface_addr));
1008
1009 return 0;
1010}
1011
1012
1013static void wpas_p2p_remove_pending_group_interface(
1014 struct wpa_supplicant *wpa_s)
1015{
1016 if (!wpa_s->pending_interface_name[0] ||
1017 is_zero_ether_addr(wpa_s->pending_interface_addr))
1018 return; /* No pending virtual interface */
1019
1020 wpa_printf(MSG_DEBUG, "P2P: Removing pending group interface %s",
1021 wpa_s->pending_interface_name);
1022 wpa_drv_if_remove(wpa_s, wpa_s->pending_interface_type,
1023 wpa_s->pending_interface_name);
1024 os_memset(wpa_s->pending_interface_addr, 0, ETH_ALEN);
1025 wpa_s->pending_interface_name[0] = '\0';
1026}
1027
1028
1029static struct wpa_supplicant *
1030wpas_p2p_init_group_interface(struct wpa_supplicant *wpa_s, int go)
1031{
1032 struct wpa_interface iface;
1033 struct wpa_supplicant *group_wpa_s;
1034
1035 if (!wpa_s->pending_interface_name[0]) {
1036 wpa_printf(MSG_ERROR, "P2P: No pending group interface");
1037 if (!wpas_p2p_create_iface(wpa_s))
1038 return NULL;
1039 /*
1040 * Something has forced us to remove the pending interface; try
1041 * to create a new one and hope for the best that we will get
1042 * the same local address.
1043 */
1044 if (wpas_p2p_add_group_interface(wpa_s, go ? WPA_IF_P2P_GO :
1045 WPA_IF_P2P_CLIENT) < 0)
1046 return NULL;
1047 }
1048
1049 os_memset(&iface, 0, sizeof(iface));
1050 iface.ifname = wpa_s->pending_interface_name;
1051 iface.driver = wpa_s->driver->name;
1052 iface.ctrl_interface = wpa_s->conf->ctrl_interface;
1053 iface.driver_param = wpa_s->conf->driver_param;
1054 group_wpa_s = wpa_supplicant_add_iface(wpa_s->global, &iface);
1055 if (group_wpa_s == NULL) {
1056 wpa_printf(MSG_ERROR, "P2P: Failed to create new "
1057 "wpa_supplicant interface");
1058 return NULL;
1059 }
1060 wpa_s->pending_interface_name[0] = '\0';
1061 group_wpa_s->parent = wpa_s;
1062 group_wpa_s->p2p_group_interface = go ? P2P_GROUP_INTERFACE_GO :
1063 P2P_GROUP_INTERFACE_CLIENT;
1064 wpa_s->global->p2p_group_formation = group_wpa_s;
1065
1066 wpas_p2p_clone_config(group_wpa_s, wpa_s);
1067
1068 return group_wpa_s;
1069}
1070
1071
1072static void wpas_p2p_group_formation_timeout(void *eloop_ctx,
1073 void *timeout_ctx)
1074{
1075 struct wpa_supplicant *wpa_s = eloop_ctx;
1076 wpa_printf(MSG_DEBUG, "P2P: Group Formation timed out");
1077 if (wpa_s->global->p2p)
1078 p2p_group_formation_failed(wpa_s->global->p2p);
1079 else if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
1080 wpa_drv_p2p_group_formation_failed(wpa_s);
1081 wpas_group_formation_completed(wpa_s, 0);
1082}
1083
1084
1085void wpas_go_neg_completed(void *ctx, struct p2p_go_neg_results *res)
1086{
1087 struct wpa_supplicant *wpa_s = ctx;
1088
1089 if (wpa_s->off_channel_freq || wpa_s->roc_waiting_drv_freq) {
1090 wpa_drv_cancel_remain_on_channel(wpa_s);
1091 wpa_s->off_channel_freq = 0;
1092 wpa_s->roc_waiting_drv_freq = 0;
1093 }
1094
1095 if (res->status) {
1096 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_GO_NEG_FAILURE "status=%d",
1097 res->status);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001098 wpas_notify_p2p_go_neg_completed(wpa_s, res);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001099 wpas_p2p_remove_pending_group_interface(wpa_s);
1100 return;
1101 }
1102
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001103 if (wpa_s->p2p_go_ht40)
1104 res->ht40 = 1;
1105
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001106 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_GO_NEG_SUCCESS);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001107 wpas_notify_p2p_go_neg_completed(wpa_s, res);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001108
Dmitry Shmidt04949592012-07-19 12:16:46 -07001109 if (res->role_go && wpa_s->p2p_persistent_id >= 0) {
1110 struct wpa_ssid *ssid;
1111 ssid = wpa_config_get_network(wpa_s->conf,
1112 wpa_s->p2p_persistent_id);
1113 if (ssid && ssid->disabled == 2 &&
1114 ssid->mode == WPAS_MODE_P2P_GO && ssid->passphrase) {
1115 size_t len = os_strlen(ssid->passphrase);
1116 wpa_printf(MSG_DEBUG, "P2P: Override passphrase based "
1117 "on requested persistent group");
1118 os_memcpy(res->passphrase, ssid->passphrase, len);
1119 res->passphrase[len] = '\0';
1120 }
1121 }
1122
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001123 if (wpa_s->create_p2p_iface) {
1124 struct wpa_supplicant *group_wpa_s =
1125 wpas_p2p_init_group_interface(wpa_s, res->role_go);
1126 if (group_wpa_s == NULL) {
1127 wpas_p2p_remove_pending_group_interface(wpa_s);
1128 return;
1129 }
1130 if (group_wpa_s != wpa_s) {
1131 os_memcpy(group_wpa_s->p2p_pin, wpa_s->p2p_pin,
1132 sizeof(group_wpa_s->p2p_pin));
1133 group_wpa_s->p2p_wps_method = wpa_s->p2p_wps_method;
1134 }
1135 os_memset(wpa_s->pending_interface_addr, 0, ETH_ALEN);
1136 wpa_s->pending_interface_name[0] = '\0';
1137 group_wpa_s->p2p_in_provisioning = 1;
1138
1139 if (res->role_go)
1140 wpas_start_wps_go(group_wpa_s, res, 1);
1141 else
1142 wpas_start_wps_enrollee(group_wpa_s, res);
1143 } else {
1144 wpa_s->p2p_in_provisioning = 1;
1145 wpa_s->global->p2p_group_formation = wpa_s;
1146
1147 if (res->role_go)
1148 wpas_start_wps_go(wpa_s, res, 1);
1149 else
1150 wpas_start_wps_enrollee(ctx, res);
1151 }
1152
1153 wpa_s->p2p_long_listen = 0;
1154 eloop_cancel_timeout(wpas_p2p_long_listen_timeout, wpa_s, NULL);
1155
1156 eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s, NULL);
1157 eloop_register_timeout(15 + res->peer_config_timeout / 100,
1158 (res->peer_config_timeout % 100) * 10000,
1159 wpas_p2p_group_formation_timeout, wpa_s, NULL);
1160}
1161
1162
1163void wpas_go_neg_req_rx(void *ctx, const u8 *src, u16 dev_passwd_id)
1164{
1165 struct wpa_supplicant *wpa_s = ctx;
1166 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_GO_NEG_REQUEST MACSTR
1167 " dev_passwd_id=%u", MAC2STR(src), dev_passwd_id);
1168
1169 wpas_notify_p2p_go_neg_req(wpa_s, src, dev_passwd_id);
1170}
1171
1172
1173void wpas_dev_found(void *ctx, const u8 *addr,
1174 const struct p2p_peer_info *info,
1175 int new_device)
1176{
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001177#ifndef CONFIG_NO_STDOUT_DEBUG
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001178 struct wpa_supplicant *wpa_s = ctx;
1179 char devtype[WPS_DEV_TYPE_BUFSIZE];
Irfan Sheriff8367dc92012-09-09 17:08:19 -07001180#define WFD_DEV_INFO_SIZE 9
1181 char wfd_dev_info_hex[2 * WFD_DEV_INFO_SIZE + 1];
Irfan Sheriff8d965182012-09-11 08:58:24 -07001182 os_memset(wfd_dev_info_hex, 0, sizeof(wfd_dev_info_hex));
Irfan Sheriff8367dc92012-09-09 17:08:19 -07001183#ifdef CONFIG_WIFI_DISPLAY
1184 if (info->wfd_subelems) {
1185 wpa_snprintf_hex(wfd_dev_info_hex, sizeof(wfd_dev_info_hex),
1186 wpabuf_head(info->wfd_subelems),
1187 WFD_DEV_INFO_SIZE);
1188 }
1189#endif /* CONFIG_WIFI_DISPLAY */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001190 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_DEVICE_FOUND MACSTR
1191 " p2p_dev_addr=" MACSTR
1192 " pri_dev_type=%s name='%s' config_methods=0x%x "
Irfan Sheriff8367dc92012-09-09 17:08:19 -07001193 "dev_capab=0x%x group_capab=0x%x%s%s",
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001194 MAC2STR(addr), MAC2STR(info->p2p_device_addr),
1195 wps_dev_type_bin2str(info->pri_dev_type, devtype,
1196 sizeof(devtype)),
1197 info->device_name, info->config_methods,
Irfan Sheriff8367dc92012-09-09 17:08:19 -07001198 info->dev_capab, info->group_capab,
1199 wfd_dev_info_hex[0] ? " wfd_dev_info=0x" : "", wfd_dev_info_hex);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001200#endif /* CONFIG_NO_STDOUT_DEBUG */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001201
1202 wpas_notify_p2p_device_found(ctx, info->p2p_device_addr, new_device);
1203}
1204
1205
1206static void wpas_dev_lost(void *ctx, const u8 *dev_addr)
1207{
1208 struct wpa_supplicant *wpa_s = ctx;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001209
Dmitry Shmidt497c1d52011-07-21 15:19:46 -07001210 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_DEVICE_LOST
1211 "p2p_dev_addr=" MACSTR, MAC2STR(dev_addr));
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001212
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001213 wpas_notify_p2p_device_lost(wpa_s, dev_addr);
1214}
1215
1216
1217static int wpas_start_listen(void *ctx, unsigned int freq,
1218 unsigned int duration,
1219 const struct wpabuf *probe_resp_ie)
1220{
1221 struct wpa_supplicant *wpa_s = ctx;
1222
1223 wpa_drv_set_ap_wps_ie(wpa_s, NULL, probe_resp_ie, NULL);
1224
1225 if (wpa_drv_probe_req_report(wpa_s, 1) < 0) {
1226 wpa_printf(MSG_DEBUG, "P2P: Failed to request the driver to "
1227 "report received Probe Request frames");
1228 return -1;
1229 }
1230
1231 wpa_s->pending_listen_freq = freq;
1232 wpa_s->pending_listen_duration = duration;
1233
1234 if (wpa_drv_remain_on_channel(wpa_s, freq, duration) < 0) {
1235 wpa_printf(MSG_DEBUG, "P2P: Failed to request the driver "
1236 "to remain on channel (%u MHz) for Listen "
1237 "state", freq);
1238 wpa_s->pending_listen_freq = 0;
1239 return -1;
1240 }
1241 wpa_s->off_channel_freq = 0;
1242 wpa_s->roc_waiting_drv_freq = freq;
1243
1244 return 0;
1245}
1246
1247
1248static void wpas_stop_listen(void *ctx)
1249{
1250 struct wpa_supplicant *wpa_s = ctx;
1251 if (wpa_s->off_channel_freq || wpa_s->roc_waiting_drv_freq) {
1252 wpa_drv_cancel_remain_on_channel(wpa_s);
1253 wpa_s->off_channel_freq = 0;
1254 wpa_s->roc_waiting_drv_freq = 0;
1255 }
1256 wpa_drv_set_ap_wps_ie(wpa_s, NULL, NULL, NULL);
1257 wpa_drv_probe_req_report(wpa_s, 0);
1258}
1259
1260
1261static int wpas_send_probe_resp(void *ctx, const struct wpabuf *buf)
1262{
1263 struct wpa_supplicant *wpa_s = ctx;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001264 return wpa_drv_send_mlme(wpa_s, wpabuf_head(buf), wpabuf_len(buf), 1);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001265}
1266
1267
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001268/*
1269 * DNS Header section is used only to calculate compression pointers, so the
1270 * contents of this data does not matter, but the length needs to be reserved
1271 * in the virtual packet.
1272 */
1273#define DNS_HEADER_LEN 12
1274
1275/*
1276 * 27-octet in-memory packet from P2P specification containing two implied
1277 * queries for _tcp.lcoal. PTR IN and _udp.local. PTR IN
1278 */
1279#define P2P_SD_IN_MEMORY_LEN 27
1280
1281static int p2p_sd_dns_uncompress_label(char **upos, char *uend, u8 *start,
1282 u8 **spos, const u8 *end)
1283{
1284 while (*spos < end) {
1285 u8 val = ((*spos)[0] & 0xc0) >> 6;
1286 int len;
1287
1288 if (val == 1 || val == 2) {
1289 /* These are reserved values in RFC 1035 */
1290 wpa_printf(MSG_DEBUG, "P2P: Invalid domain name "
1291 "sequence starting with 0x%x", val);
1292 return -1;
1293 }
1294
1295 if (val == 3) {
1296 u16 offset;
1297 u8 *spos_tmp;
1298
1299 /* Offset */
1300 if (*spos + 2 > end) {
1301 wpa_printf(MSG_DEBUG, "P2P: No room for full "
1302 "DNS offset field");
1303 return -1;
1304 }
1305
1306 offset = (((*spos)[0] & 0x3f) << 8) | (*spos)[1];
1307 if (offset >= *spos - start) {
1308 wpa_printf(MSG_DEBUG, "P2P: Invalid DNS "
1309 "pointer offset %u", offset);
1310 return -1;
1311 }
1312
1313 (*spos) += 2;
1314 spos_tmp = start + offset;
1315 return p2p_sd_dns_uncompress_label(upos, uend, start,
1316 &spos_tmp,
1317 *spos - 2);
1318 }
1319
1320 /* Label */
1321 len = (*spos)[0] & 0x3f;
1322 if (len == 0)
1323 return 0;
1324
1325 (*spos)++;
1326 if (*spos + len > end) {
1327 wpa_printf(MSG_DEBUG, "P2P: Invalid domain name "
1328 "sequence - no room for label with length "
1329 "%u", len);
1330 return -1;
1331 }
1332
1333 if (*upos + len + 2 > uend)
1334 return -2;
1335
1336 os_memcpy(*upos, *spos, len);
1337 *spos += len;
1338 *upos += len;
1339 (*upos)[0] = '.';
1340 (*upos)++;
1341 (*upos)[0] = '\0';
1342 }
1343
1344 return 0;
1345}
1346
1347
1348/* Uncompress domain names per RFC 1035 using the P2P SD in-memory packet.
1349 * Returns -1 on parsing error (invalid input sequence), -2 if output buffer is
1350 * not large enough */
1351static int p2p_sd_dns_uncompress(char *buf, size_t buf_len, const u8 *msg,
1352 size_t msg_len, size_t offset)
1353{
1354 /* 27-octet in-memory packet from P2P specification */
1355 const char *prefix = "\x04_tcp\x05local\x00\x00\x0C\x00\x01"
1356 "\x04_udp\xC0\x11\x00\x0C\x00\x01";
1357 u8 *tmp, *end, *spos;
1358 char *upos, *uend;
1359 int ret = 0;
1360
1361 if (buf_len < 2)
1362 return -1;
1363 if (offset > msg_len)
1364 return -1;
1365
1366 tmp = os_malloc(DNS_HEADER_LEN + P2P_SD_IN_MEMORY_LEN + msg_len);
1367 if (tmp == NULL)
1368 return -1;
1369 spos = tmp + DNS_HEADER_LEN + P2P_SD_IN_MEMORY_LEN;
1370 end = spos + msg_len;
1371 spos += offset;
1372
1373 os_memset(tmp, 0, DNS_HEADER_LEN);
1374 os_memcpy(tmp + DNS_HEADER_LEN, prefix, P2P_SD_IN_MEMORY_LEN);
1375 os_memcpy(tmp + DNS_HEADER_LEN + P2P_SD_IN_MEMORY_LEN, msg, msg_len);
1376
1377 upos = buf;
1378 uend = buf + buf_len;
1379
1380 ret = p2p_sd_dns_uncompress_label(&upos, uend, tmp, &spos, end);
1381 if (ret) {
1382 os_free(tmp);
1383 return ret;
1384 }
1385
1386 if (upos == buf) {
1387 upos[0] = '.';
1388 upos[1] = '\0';
1389 } else if (upos[-1] == '.')
1390 upos[-1] = '\0';
1391
1392 os_free(tmp);
1393 return 0;
1394}
1395
1396
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001397static struct p2p_srv_bonjour *
1398wpas_p2p_service_get_bonjour(struct wpa_supplicant *wpa_s,
1399 const struct wpabuf *query)
1400{
1401 struct p2p_srv_bonjour *bsrv;
1402 size_t len;
1403
1404 len = wpabuf_len(query);
1405 dl_list_for_each(bsrv, &wpa_s->global->p2p_srv_bonjour,
1406 struct p2p_srv_bonjour, list) {
1407 if (len == wpabuf_len(bsrv->query) &&
1408 os_memcmp(wpabuf_head(query), wpabuf_head(bsrv->query),
1409 len) == 0)
1410 return bsrv;
1411 }
1412 return NULL;
1413}
1414
1415
1416static struct p2p_srv_upnp *
1417wpas_p2p_service_get_upnp(struct wpa_supplicant *wpa_s, u8 version,
1418 const char *service)
1419{
1420 struct p2p_srv_upnp *usrv;
1421
1422 dl_list_for_each(usrv, &wpa_s->global->p2p_srv_upnp,
1423 struct p2p_srv_upnp, list) {
1424 if (version == usrv->version &&
1425 os_strcmp(service, usrv->service) == 0)
1426 return usrv;
1427 }
1428 return NULL;
1429}
1430
1431
1432static void wpas_sd_add_proto_not_avail(struct wpabuf *resp, u8 srv_proto,
1433 u8 srv_trans_id)
1434{
1435 u8 *len_pos;
1436
1437 if (wpabuf_tailroom(resp) < 5)
1438 return;
1439
1440 /* Length (to be filled) */
1441 len_pos = wpabuf_put(resp, 2);
1442 wpabuf_put_u8(resp, srv_proto);
1443 wpabuf_put_u8(resp, srv_trans_id);
1444 /* Status Code */
1445 wpabuf_put_u8(resp, P2P_SD_PROTO_NOT_AVAILABLE);
1446 /* Response Data: empty */
1447 WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos - 2);
1448}
1449
1450
1451static void wpas_sd_all_bonjour(struct wpa_supplicant *wpa_s,
1452 struct wpabuf *resp, u8 srv_trans_id)
1453{
1454 struct p2p_srv_bonjour *bsrv;
1455 u8 *len_pos;
1456
1457 wpa_printf(MSG_DEBUG, "P2P: SD Request for all Bonjour services");
1458
1459 if (dl_list_empty(&wpa_s->global->p2p_srv_bonjour)) {
1460 wpa_printf(MSG_DEBUG, "P2P: Bonjour protocol not available");
1461 return;
1462 }
1463
1464 dl_list_for_each(bsrv, &wpa_s->global->p2p_srv_bonjour,
1465 struct p2p_srv_bonjour, list) {
1466 if (wpabuf_tailroom(resp) <
1467 5 + wpabuf_len(bsrv->query) + wpabuf_len(bsrv->resp))
1468 return;
1469 /* Length (to be filled) */
1470 len_pos = wpabuf_put(resp, 2);
1471 wpabuf_put_u8(resp, P2P_SERV_BONJOUR);
1472 wpabuf_put_u8(resp, srv_trans_id);
1473 /* Status Code */
1474 wpabuf_put_u8(resp, P2P_SD_SUCCESS);
1475 wpa_hexdump_ascii(MSG_DEBUG, "P2P: Matching Bonjour service",
1476 wpabuf_head(bsrv->resp),
1477 wpabuf_len(bsrv->resp));
1478 /* Response Data */
1479 wpabuf_put_buf(resp, bsrv->query); /* Key */
1480 wpabuf_put_buf(resp, bsrv->resp); /* Value */
1481 WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos -
1482 2);
1483 }
1484}
1485
1486
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001487static int match_bonjour_query(struct p2p_srv_bonjour *bsrv, const u8 *query,
1488 size_t query_len)
1489{
1490 char str_rx[256], str_srv[256];
1491
1492 if (query_len < 3 || wpabuf_len(bsrv->query) < 3)
1493 return 0; /* Too short to include DNS Type and Version */
1494 if (os_memcmp(query + query_len - 3,
1495 wpabuf_head_u8(bsrv->query) + wpabuf_len(bsrv->query) - 3,
1496 3) != 0)
1497 return 0; /* Mismatch in DNS Type or Version */
1498 if (query_len == wpabuf_len(bsrv->query) &&
1499 os_memcmp(query, wpabuf_head(bsrv->query), query_len - 3) == 0)
1500 return 1; /* Binary match */
1501
1502 if (p2p_sd_dns_uncompress(str_rx, sizeof(str_rx), query, query_len - 3,
1503 0))
1504 return 0; /* Failed to uncompress query */
1505 if (p2p_sd_dns_uncompress(str_srv, sizeof(str_srv),
1506 wpabuf_head(bsrv->query),
1507 wpabuf_len(bsrv->query) - 3, 0))
1508 return 0; /* Failed to uncompress service */
1509
1510 return os_strcmp(str_rx, str_srv) == 0;
1511}
1512
1513
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001514static void wpas_sd_req_bonjour(struct wpa_supplicant *wpa_s,
1515 struct wpabuf *resp, u8 srv_trans_id,
1516 const u8 *query, size_t query_len)
1517{
1518 struct p2p_srv_bonjour *bsrv;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001519 u8 *len_pos;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001520 int matches = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001521
1522 wpa_hexdump_ascii(MSG_DEBUG, "P2P: SD Request for Bonjour",
1523 query, query_len);
1524 if (dl_list_empty(&wpa_s->global->p2p_srv_bonjour)) {
1525 wpa_printf(MSG_DEBUG, "P2P: Bonjour protocol not available");
1526 wpas_sd_add_proto_not_avail(resp, P2P_SERV_BONJOUR,
1527 srv_trans_id);
1528 return;
1529 }
1530
1531 if (query_len == 0) {
1532 wpas_sd_all_bonjour(wpa_s, resp, srv_trans_id);
1533 return;
1534 }
1535
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001536 dl_list_for_each(bsrv, &wpa_s->global->p2p_srv_bonjour,
1537 struct p2p_srv_bonjour, list) {
1538 if (!match_bonjour_query(bsrv, query, query_len))
1539 continue;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001540
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001541 if (wpabuf_tailroom(resp) <
1542 5 + query_len + wpabuf_len(bsrv->resp))
1543 return;
1544
1545 matches++;
1546
1547 /* Length (to be filled) */
1548 len_pos = wpabuf_put(resp, 2);
1549 wpabuf_put_u8(resp, P2P_SERV_BONJOUR);
1550 wpabuf_put_u8(resp, srv_trans_id);
1551
1552 /* Status Code */
1553 wpabuf_put_u8(resp, P2P_SD_SUCCESS);
1554 wpa_hexdump_ascii(MSG_DEBUG, "P2P: Matching Bonjour service",
1555 wpabuf_head(bsrv->resp),
1556 wpabuf_len(bsrv->resp));
1557
1558 /* Response Data */
1559 wpabuf_put_data(resp, query, query_len); /* Key */
1560 wpabuf_put_buf(resp, bsrv->resp); /* Value */
1561
1562 WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos - 2);
1563 }
1564
1565 if (matches == 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001566 wpa_printf(MSG_DEBUG, "P2P: Requested Bonjour service not "
1567 "available");
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001568 if (wpabuf_tailroom(resp) < 5)
1569 return;
1570
1571 /* Length (to be filled) */
1572 len_pos = wpabuf_put(resp, 2);
1573 wpabuf_put_u8(resp, P2P_SERV_BONJOUR);
1574 wpabuf_put_u8(resp, srv_trans_id);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001575
1576 /* Status Code */
1577 wpabuf_put_u8(resp, P2P_SD_REQUESTED_INFO_NOT_AVAILABLE);
1578 /* Response Data: empty */
1579 WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos -
1580 2);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001581 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001582}
1583
1584
1585static void wpas_sd_all_upnp(struct wpa_supplicant *wpa_s,
1586 struct wpabuf *resp, u8 srv_trans_id)
1587{
1588 struct p2p_srv_upnp *usrv;
1589 u8 *len_pos;
1590
1591 wpa_printf(MSG_DEBUG, "P2P: SD Request for all UPnP services");
1592
1593 if (dl_list_empty(&wpa_s->global->p2p_srv_upnp)) {
1594 wpa_printf(MSG_DEBUG, "P2P: UPnP protocol not available");
1595 return;
1596 }
1597
1598 dl_list_for_each(usrv, &wpa_s->global->p2p_srv_upnp,
1599 struct p2p_srv_upnp, list) {
1600 if (wpabuf_tailroom(resp) < 5 + 1 + os_strlen(usrv->service))
1601 return;
1602
1603 /* Length (to be filled) */
1604 len_pos = wpabuf_put(resp, 2);
1605 wpabuf_put_u8(resp, P2P_SERV_UPNP);
1606 wpabuf_put_u8(resp, srv_trans_id);
1607
1608 /* Status Code */
1609 wpabuf_put_u8(resp, P2P_SD_SUCCESS);
1610 /* Response Data */
1611 wpabuf_put_u8(resp, usrv->version);
1612 wpa_printf(MSG_DEBUG, "P2P: Matching UPnP Service: %s",
1613 usrv->service);
1614 wpabuf_put_str(resp, usrv->service);
1615 WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos -
1616 2);
1617 }
1618}
1619
1620
1621static void wpas_sd_req_upnp(struct wpa_supplicant *wpa_s,
1622 struct wpabuf *resp, u8 srv_trans_id,
1623 const u8 *query, size_t query_len)
1624{
1625 struct p2p_srv_upnp *usrv;
1626 u8 *len_pos;
1627 u8 version;
1628 char *str;
1629 int count = 0;
1630
1631 wpa_hexdump_ascii(MSG_DEBUG, "P2P: SD Request for UPnP",
1632 query, query_len);
1633
1634 if (dl_list_empty(&wpa_s->global->p2p_srv_upnp)) {
1635 wpa_printf(MSG_DEBUG, "P2P: UPnP protocol not available");
1636 wpas_sd_add_proto_not_avail(resp, P2P_SERV_UPNP,
1637 srv_trans_id);
1638 return;
1639 }
1640
1641 if (query_len == 0) {
1642 wpas_sd_all_upnp(wpa_s, resp, srv_trans_id);
1643 return;
1644 }
1645
1646 if (wpabuf_tailroom(resp) < 5)
1647 return;
1648
1649 /* Length (to be filled) */
1650 len_pos = wpabuf_put(resp, 2);
1651 wpabuf_put_u8(resp, P2P_SERV_UPNP);
1652 wpabuf_put_u8(resp, srv_trans_id);
1653
1654 version = query[0];
1655 str = os_malloc(query_len);
1656 if (str == NULL)
1657 return;
1658 os_memcpy(str, query + 1, query_len - 1);
1659 str[query_len - 1] = '\0';
1660
1661 dl_list_for_each(usrv, &wpa_s->global->p2p_srv_upnp,
1662 struct p2p_srv_upnp, list) {
1663 if (version != usrv->version)
1664 continue;
1665
1666 if (os_strcmp(str, "ssdp:all") != 0 &&
1667 os_strstr(usrv->service, str) == NULL)
1668 continue;
1669
1670 if (wpabuf_tailroom(resp) < 2)
1671 break;
1672 if (count == 0) {
1673 /* Status Code */
1674 wpabuf_put_u8(resp, P2P_SD_SUCCESS);
1675 /* Response Data */
1676 wpabuf_put_u8(resp, version);
1677 } else
1678 wpabuf_put_u8(resp, ',');
1679
1680 count++;
1681
1682 wpa_printf(MSG_DEBUG, "P2P: Matching UPnP Service: %s",
1683 usrv->service);
1684 if (wpabuf_tailroom(resp) < os_strlen(usrv->service))
1685 break;
1686 wpabuf_put_str(resp, usrv->service);
1687 }
1688 os_free(str);
1689
1690 if (count == 0) {
1691 wpa_printf(MSG_DEBUG, "P2P: Requested UPnP service not "
1692 "available");
1693 /* Status Code */
1694 wpabuf_put_u8(resp, P2P_SD_REQUESTED_INFO_NOT_AVAILABLE);
1695 /* Response Data: empty */
1696 }
1697
1698 WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos - 2);
1699}
1700
1701
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001702#ifdef CONFIG_WIFI_DISPLAY
1703static void wpas_sd_req_wfd(struct wpa_supplicant *wpa_s,
1704 struct wpabuf *resp, u8 srv_trans_id,
1705 const u8 *query, size_t query_len)
1706{
1707 const u8 *pos;
1708 u8 role;
1709 u8 *len_pos;
1710
1711 wpa_hexdump(MSG_DEBUG, "P2P: SD Request for WFD", query, query_len);
1712
1713 if (!wpa_s->global->wifi_display) {
1714 wpa_printf(MSG_DEBUG, "P2P: WFD protocol not available");
1715 wpas_sd_add_proto_not_avail(resp, P2P_SERV_WIFI_DISPLAY,
1716 srv_trans_id);
1717 return;
1718 }
1719
1720 if (query_len < 1) {
1721 wpa_printf(MSG_DEBUG, "P2P: Missing WFD Requested Device "
1722 "Role");
1723 return;
1724 }
1725
1726 if (wpabuf_tailroom(resp) < 5)
1727 return;
1728
1729 pos = query;
1730 role = *pos++;
1731 wpa_printf(MSG_DEBUG, "P2P: WSD for device role 0x%x", role);
1732
1733 /* TODO: role specific handling */
1734
1735 /* Length (to be filled) */
1736 len_pos = wpabuf_put(resp, 2);
1737 wpabuf_put_u8(resp, P2P_SERV_WIFI_DISPLAY);
1738 wpabuf_put_u8(resp, srv_trans_id);
1739 wpabuf_put_u8(resp, P2P_SD_SUCCESS); /* Status Code */
1740
1741 while (pos < query + query_len) {
1742 if (*pos < MAX_WFD_SUBELEMS &&
1743 wpa_s->global->wfd_subelem[*pos] &&
1744 wpabuf_tailroom(resp) >=
1745 wpabuf_len(wpa_s->global->wfd_subelem[*pos])) {
1746 wpa_printf(MSG_DEBUG, "P2P: Add WSD response "
1747 "subelement %u", *pos);
1748 wpabuf_put_buf(resp, wpa_s->global->wfd_subelem[*pos]);
1749 }
1750 pos++;
1751 }
1752
1753 WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos - 2);
1754}
1755#endif /* CONFIG_WIFI_DISPLAY */
1756
1757
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001758void wpas_sd_request(void *ctx, int freq, const u8 *sa, u8 dialog_token,
1759 u16 update_indic, const u8 *tlvs, size_t tlvs_len)
1760{
1761 struct wpa_supplicant *wpa_s = ctx;
1762 const u8 *pos = tlvs;
1763 const u8 *end = tlvs + tlvs_len;
1764 const u8 *tlv_end;
1765 u16 slen;
1766 struct wpabuf *resp;
1767 u8 srv_proto, srv_trans_id;
1768 size_t buf_len;
1769 char *buf;
1770
1771 wpa_hexdump(MSG_MSGDUMP, "P2P: Service Discovery Request TLVs",
1772 tlvs, tlvs_len);
1773 buf_len = 2 * tlvs_len + 1;
1774 buf = os_malloc(buf_len);
1775 if (buf) {
1776 wpa_snprintf_hex(buf, buf_len, tlvs, tlvs_len);
1777 wpa_msg_ctrl(wpa_s, MSG_INFO, P2P_EVENT_SERV_DISC_REQ "%d "
1778 MACSTR " %u %u %s",
1779 freq, MAC2STR(sa), dialog_token, update_indic,
1780 buf);
1781 os_free(buf);
1782 }
1783
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001784 if (wpa_s->p2p_sd_over_ctrl_iface) {
1785 wpas_notify_p2p_sd_request(wpa_s, freq, sa, dialog_token,
1786 update_indic, tlvs, tlvs_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001787 return; /* to be processed by an external program */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001788 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001789
1790 resp = wpabuf_alloc(10000);
1791 if (resp == NULL)
1792 return;
1793
1794 while (pos + 1 < end) {
1795 wpa_printf(MSG_DEBUG, "P2P: Service Request TLV");
1796 slen = WPA_GET_LE16(pos);
1797 pos += 2;
1798 if (pos + slen > end || slen < 2) {
1799 wpa_printf(MSG_DEBUG, "P2P: Unexpected Query Data "
1800 "length");
1801 wpabuf_free(resp);
1802 return;
1803 }
1804 tlv_end = pos + slen;
1805
1806 srv_proto = *pos++;
1807 wpa_printf(MSG_DEBUG, "P2P: Service Protocol Type %u",
1808 srv_proto);
1809 srv_trans_id = *pos++;
1810 wpa_printf(MSG_DEBUG, "P2P: Service Transaction ID %u",
1811 srv_trans_id);
1812
1813 wpa_hexdump(MSG_MSGDUMP, "P2P: Query Data",
1814 pos, tlv_end - pos);
1815
1816
1817 if (wpa_s->force_long_sd) {
1818 wpa_printf(MSG_DEBUG, "P2P: SD test - force long "
1819 "response");
1820 wpas_sd_all_bonjour(wpa_s, resp, srv_trans_id);
1821 wpas_sd_all_upnp(wpa_s, resp, srv_trans_id);
1822 goto done;
1823 }
1824
1825 switch (srv_proto) {
1826 case P2P_SERV_ALL_SERVICES:
1827 wpa_printf(MSG_DEBUG, "P2P: Service Discovery Request "
1828 "for all services");
1829 if (dl_list_empty(&wpa_s->global->p2p_srv_upnp) &&
1830 dl_list_empty(&wpa_s->global->p2p_srv_bonjour)) {
1831 wpa_printf(MSG_DEBUG, "P2P: No service "
1832 "discovery protocols available");
1833 wpas_sd_add_proto_not_avail(
1834 resp, P2P_SERV_ALL_SERVICES,
1835 srv_trans_id);
1836 break;
1837 }
1838 wpas_sd_all_bonjour(wpa_s, resp, srv_trans_id);
1839 wpas_sd_all_upnp(wpa_s, resp, srv_trans_id);
1840 break;
1841 case P2P_SERV_BONJOUR:
1842 wpas_sd_req_bonjour(wpa_s, resp, srv_trans_id,
1843 pos, tlv_end - pos);
1844 break;
1845 case P2P_SERV_UPNP:
1846 wpas_sd_req_upnp(wpa_s, resp, srv_trans_id,
1847 pos, tlv_end - pos);
1848 break;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001849#ifdef CONFIG_WIFI_DISPLAY
1850 case P2P_SERV_WIFI_DISPLAY:
1851 wpas_sd_req_wfd(wpa_s, resp, srv_trans_id,
1852 pos, tlv_end - pos);
1853 break;
1854#endif /* CONFIG_WIFI_DISPLAY */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001855 default:
1856 wpa_printf(MSG_DEBUG, "P2P: Unavailable service "
1857 "protocol %u", srv_proto);
1858 wpas_sd_add_proto_not_avail(resp, srv_proto,
1859 srv_trans_id);
1860 break;
1861 }
1862
1863 pos = tlv_end;
1864 }
1865
1866done:
1867 wpas_notify_p2p_sd_request(wpa_s, freq, sa, dialog_token,
1868 update_indic, tlvs, tlvs_len);
1869
1870 wpas_p2p_sd_response(wpa_s, freq, sa, dialog_token, resp);
1871
1872 wpabuf_free(resp);
1873}
1874
1875
1876void wpas_sd_response(void *ctx, const u8 *sa, u16 update_indic,
1877 const u8 *tlvs, size_t tlvs_len)
1878{
1879 struct wpa_supplicant *wpa_s = ctx;
1880 const u8 *pos = tlvs;
1881 const u8 *end = tlvs + tlvs_len;
1882 const u8 *tlv_end;
1883 u16 slen;
1884 size_t buf_len;
1885 char *buf;
1886
1887 wpa_hexdump(MSG_MSGDUMP, "P2P: Service Discovery Response TLVs",
1888 tlvs, tlvs_len);
1889 if (tlvs_len > 1500) {
1890 /* TODO: better way for handling this */
1891 wpa_msg_ctrl(wpa_s, MSG_INFO,
1892 P2P_EVENT_SERV_DISC_RESP MACSTR
1893 " %u <long response: %u bytes>",
1894 MAC2STR(sa), update_indic,
1895 (unsigned int) tlvs_len);
1896 } else {
1897 buf_len = 2 * tlvs_len + 1;
1898 buf = os_malloc(buf_len);
1899 if (buf) {
1900 wpa_snprintf_hex(buf, buf_len, tlvs, tlvs_len);
1901 wpa_msg_ctrl(wpa_s, MSG_INFO,
1902 P2P_EVENT_SERV_DISC_RESP MACSTR " %u %s",
1903 MAC2STR(sa), update_indic, buf);
1904 os_free(buf);
1905 }
1906 }
1907
1908 while (pos < end) {
1909 u8 srv_proto, srv_trans_id, status;
1910
1911 wpa_printf(MSG_DEBUG, "P2P: Service Response TLV");
1912 slen = WPA_GET_LE16(pos);
1913 pos += 2;
1914 if (pos + slen > end || slen < 3) {
1915 wpa_printf(MSG_DEBUG, "P2P: Unexpected Response Data "
1916 "length");
1917 return;
1918 }
1919 tlv_end = pos + slen;
1920
1921 srv_proto = *pos++;
1922 wpa_printf(MSG_DEBUG, "P2P: Service Protocol Type %u",
1923 srv_proto);
1924 srv_trans_id = *pos++;
1925 wpa_printf(MSG_DEBUG, "P2P: Service Transaction ID %u",
1926 srv_trans_id);
1927 status = *pos++;
1928 wpa_printf(MSG_DEBUG, "P2P: Status Code ID %u",
1929 status);
1930
1931 wpa_hexdump(MSG_MSGDUMP, "P2P: Response Data",
1932 pos, tlv_end - pos);
1933
1934 pos = tlv_end;
1935 }
1936
1937 wpas_notify_p2p_sd_response(wpa_s, sa, update_indic, tlvs, tlvs_len);
1938}
1939
1940
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001941u64 wpas_p2p_sd_request(struct wpa_supplicant *wpa_s, const u8 *dst,
1942 const struct wpabuf *tlvs)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001943{
1944 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001945 return wpa_drv_p2p_sd_request(wpa_s, dst, tlvs);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001946 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001947 return 0;
1948 return (uintptr_t) p2p_sd_request(wpa_s->global->p2p, dst, tlvs);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001949}
1950
1951
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001952u64 wpas_p2p_sd_request_upnp(struct wpa_supplicant *wpa_s, const u8 *dst,
1953 u8 version, const char *query)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001954{
1955 struct wpabuf *tlvs;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001956 u64 ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001957
1958 tlvs = wpabuf_alloc(2 + 1 + 1 + 1 + os_strlen(query));
1959 if (tlvs == NULL)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001960 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001961 wpabuf_put_le16(tlvs, 1 + 1 + 1 + os_strlen(query));
1962 wpabuf_put_u8(tlvs, P2P_SERV_UPNP); /* Service Protocol Type */
1963 wpabuf_put_u8(tlvs, 1); /* Service Transaction ID */
1964 wpabuf_put_u8(tlvs, version);
1965 wpabuf_put_str(tlvs, query);
1966 ret = wpas_p2p_sd_request(wpa_s, dst, tlvs);
1967 wpabuf_free(tlvs);
1968 return ret;
1969}
1970
1971
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001972#ifdef CONFIG_WIFI_DISPLAY
1973
1974static u64 wpas_p2p_sd_request_wfd(struct wpa_supplicant *wpa_s, const u8 *dst,
1975 const struct wpabuf *tlvs)
1976{
1977 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
1978 return 0;
1979 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
1980 return 0;
1981 return (uintptr_t) p2p_sd_request_wfd(wpa_s->global->p2p, dst, tlvs);
1982}
1983
1984
1985#define MAX_WFD_SD_SUBELEMS 20
1986
1987static void wfd_add_sd_req_role(struct wpabuf *tlvs, u8 id, u8 role,
1988 const char *subelems)
1989{
1990 u8 *len;
1991 const char *pos;
1992 int val;
1993 int count = 0;
1994
1995 len = wpabuf_put(tlvs, 2);
1996 wpabuf_put_u8(tlvs, P2P_SERV_WIFI_DISPLAY); /* Service Protocol Type */
1997 wpabuf_put_u8(tlvs, id); /* Service Transaction ID */
1998
1999 wpabuf_put_u8(tlvs, role);
2000
2001 pos = subelems;
2002 while (*pos) {
2003 val = atoi(pos);
2004 if (val >= 0 && val < 256) {
2005 wpabuf_put_u8(tlvs, val);
2006 count++;
2007 if (count == MAX_WFD_SD_SUBELEMS)
2008 break;
2009 }
2010 pos = os_strchr(pos + 1, ',');
2011 if (pos == NULL)
2012 break;
2013 pos++;
2014 }
2015
2016 WPA_PUT_LE16(len, (u8 *) wpabuf_put(tlvs, 0) - len - 2);
2017}
2018
2019
2020u64 wpas_p2p_sd_request_wifi_display(struct wpa_supplicant *wpa_s,
2021 const u8 *dst, const char *role)
2022{
2023 struct wpabuf *tlvs;
2024 u64 ret;
2025 const char *subelems;
2026 u8 id = 1;
2027
2028 subelems = os_strchr(role, ' ');
2029 if (subelems == NULL)
2030 return 0;
2031 subelems++;
2032
2033 tlvs = wpabuf_alloc(4 * (2 + 1 + 1 + 1 + MAX_WFD_SD_SUBELEMS));
2034 if (tlvs == NULL)
2035 return 0;
2036
2037 if (os_strstr(role, "[source]"))
2038 wfd_add_sd_req_role(tlvs, id++, 0x00, subelems);
2039 if (os_strstr(role, "[pri-sink]"))
2040 wfd_add_sd_req_role(tlvs, id++, 0x01, subelems);
2041 if (os_strstr(role, "[sec-sink]"))
2042 wfd_add_sd_req_role(tlvs, id++, 0x02, subelems);
2043 if (os_strstr(role, "[source+sink]"))
2044 wfd_add_sd_req_role(tlvs, id++, 0x03, subelems);
2045
2046 ret = wpas_p2p_sd_request_wfd(wpa_s, dst, tlvs);
2047 wpabuf_free(tlvs);
2048 return ret;
2049}
2050
2051#endif /* CONFIG_WIFI_DISPLAY */
2052
2053
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002054int wpas_p2p_sd_cancel_request(struct wpa_supplicant *wpa_s, u64 req)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002055{
2056 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002057 return wpa_drv_p2p_sd_cancel_request(wpa_s, req);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002058 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
2059 return -1;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002060 return p2p_sd_cancel_request(wpa_s->global->p2p,
2061 (void *) (uintptr_t) req);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002062}
2063
2064
2065void wpas_p2p_sd_response(struct wpa_supplicant *wpa_s, int freq,
2066 const u8 *dst, u8 dialog_token,
2067 const struct wpabuf *resp_tlvs)
2068{
2069 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT) {
2070 wpa_drv_p2p_sd_response(wpa_s, freq, dst, dialog_token,
2071 resp_tlvs);
2072 return;
2073 }
2074 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
2075 return;
2076 p2p_sd_response(wpa_s->global->p2p, freq, dst, dialog_token,
2077 resp_tlvs);
2078}
2079
Dmitry Shmidtb5e8f062012-08-08 10:56:33 -07002080#ifdef ANDROID_P2P
2081void wpas_p2p_sd_service_update(struct wpa_supplicant *wpa_s, int action)
2082#else
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002083void wpas_p2p_sd_service_update(struct wpa_supplicant *wpa_s)
Dmitry Shmidtb5e8f062012-08-08 10:56:33 -07002084#endif
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002085{
2086 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT) {
2087 wpa_drv_p2p_service_update(wpa_s);
2088 return;
2089 }
2090 if (wpa_s->global->p2p)
Dmitry Shmidtb5e8f062012-08-08 10:56:33 -07002091#ifdef ANDROID_P2P
2092 p2p_sd_service_update(wpa_s->global->p2p, action);
2093#else
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002094 p2p_sd_service_update(wpa_s->global->p2p);
Dmitry Shmidtb5e8f062012-08-08 10:56:33 -07002095#endif
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002096}
2097
2098
2099static void wpas_p2p_srv_bonjour_free(struct p2p_srv_bonjour *bsrv)
2100{
2101 dl_list_del(&bsrv->list);
2102 wpabuf_free(bsrv->query);
2103 wpabuf_free(bsrv->resp);
2104 os_free(bsrv);
2105}
2106
2107
2108static void wpas_p2p_srv_upnp_free(struct p2p_srv_upnp *usrv)
2109{
2110 dl_list_del(&usrv->list);
2111 os_free(usrv->service);
2112 os_free(usrv);
2113}
2114
2115
2116void wpas_p2p_service_flush(struct wpa_supplicant *wpa_s)
2117{
2118 struct p2p_srv_bonjour *bsrv, *bn;
2119 struct p2p_srv_upnp *usrv, *un;
2120
2121 dl_list_for_each_safe(bsrv, bn, &wpa_s->global->p2p_srv_bonjour,
2122 struct p2p_srv_bonjour, list)
2123 wpas_p2p_srv_bonjour_free(bsrv);
2124
2125 dl_list_for_each_safe(usrv, un, &wpa_s->global->p2p_srv_upnp,
2126 struct p2p_srv_upnp, list)
2127 wpas_p2p_srv_upnp_free(usrv);
2128
Dmitry Shmidtb5e8f062012-08-08 10:56:33 -07002129#ifdef ANDROID_P2P
2130 wpas_p2p_sd_service_update(wpa_s, SRV_FLUSH);
2131#else
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002132 wpas_p2p_sd_service_update(wpa_s);
Dmitry Shmidtb5e8f062012-08-08 10:56:33 -07002133#endif
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002134}
2135
2136
2137int wpas_p2p_service_add_bonjour(struct wpa_supplicant *wpa_s,
2138 struct wpabuf *query, struct wpabuf *resp)
2139{
2140 struct p2p_srv_bonjour *bsrv;
2141
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002142 bsrv = os_zalloc(sizeof(*bsrv));
2143 if (bsrv == NULL)
2144 return -1;
2145 bsrv->query = query;
2146 bsrv->resp = resp;
2147 dl_list_add(&wpa_s->global->p2p_srv_bonjour, &bsrv->list);
2148
Dmitry Shmidtb5e8f062012-08-08 10:56:33 -07002149#ifdef ANDROID_P2P
2150 wpas_p2p_sd_service_update(wpa_s, SRV_ADD);
2151#else
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002152 wpas_p2p_sd_service_update(wpa_s);
Dmitry Shmidtb5e8f062012-08-08 10:56:33 -07002153#endif
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002154 return 0;
2155}
2156
2157
2158int wpas_p2p_service_del_bonjour(struct wpa_supplicant *wpa_s,
2159 const struct wpabuf *query)
2160{
2161 struct p2p_srv_bonjour *bsrv;
2162
2163 bsrv = wpas_p2p_service_get_bonjour(wpa_s, query);
2164 if (bsrv == NULL)
2165 return -1;
2166 wpas_p2p_srv_bonjour_free(bsrv);
Dmitry Shmidtb5e8f062012-08-08 10:56:33 -07002167#ifdef ANDROID_P2P
2168 wpas_p2p_sd_service_update(wpa_s, SRV_DEL);
2169#else
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002170 wpas_p2p_sd_service_update(wpa_s);
Dmitry Shmidtb5e8f062012-08-08 10:56:33 -07002171#endif
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002172 return 0;
2173}
2174
2175
2176int wpas_p2p_service_add_upnp(struct wpa_supplicant *wpa_s, u8 version,
2177 const char *service)
2178{
2179 struct p2p_srv_upnp *usrv;
2180
2181 if (wpas_p2p_service_get_upnp(wpa_s, version, service))
2182 return 0; /* Already listed */
2183 usrv = os_zalloc(sizeof(*usrv));
2184 if (usrv == NULL)
2185 return -1;
2186 usrv->version = version;
2187 usrv->service = os_strdup(service);
2188 if (usrv->service == NULL) {
2189 os_free(usrv);
2190 return -1;
2191 }
2192 dl_list_add(&wpa_s->global->p2p_srv_upnp, &usrv->list);
2193
Dmitry Shmidtb5e8f062012-08-08 10:56:33 -07002194#ifdef ANDROID_P2P
2195 wpas_p2p_sd_service_update(wpa_s, SRV_ADD);
2196#else
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002197 wpas_p2p_sd_service_update(wpa_s);
Dmitry Shmidtb5e8f062012-08-08 10:56:33 -07002198#endif
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002199 return 0;
2200}
2201
2202
2203int wpas_p2p_service_del_upnp(struct wpa_supplicant *wpa_s, u8 version,
2204 const char *service)
2205{
2206 struct p2p_srv_upnp *usrv;
2207
2208 usrv = wpas_p2p_service_get_upnp(wpa_s, version, service);
2209 if (usrv == NULL)
2210 return -1;
2211 wpas_p2p_srv_upnp_free(usrv);
Dmitry Shmidtb5e8f062012-08-08 10:56:33 -07002212#ifdef ANDROID_P2P
2213 wpas_p2p_sd_service_update(wpa_s, SRV_DEL);
2214#else
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002215 wpas_p2p_sd_service_update(wpa_s);
Dmitry Shmidtb5e8f062012-08-08 10:56:33 -07002216#endif
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002217 return 0;
2218}
2219
2220
2221static void wpas_prov_disc_local_display(struct wpa_supplicant *wpa_s,
2222 const u8 *peer, const char *params,
2223 unsigned int generated_pin)
2224{
2225 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_SHOW_PIN MACSTR " %08d%s",
2226 MAC2STR(peer), generated_pin, params);
2227}
2228
2229
2230static void wpas_prov_disc_local_keypad(struct wpa_supplicant *wpa_s,
2231 const u8 *peer, const char *params)
2232{
2233 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_ENTER_PIN MACSTR "%s",
2234 MAC2STR(peer), params);
2235}
2236
2237
2238void wpas_prov_disc_req(void *ctx, const u8 *peer, u16 config_methods,
2239 const u8 *dev_addr, const u8 *pri_dev_type,
2240 const char *dev_name, u16 supp_config_methods,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002241 u8 dev_capab, u8 group_capab, const u8 *group_id,
2242 size_t group_id_len)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002243{
2244 struct wpa_supplicant *wpa_s = ctx;
2245 char devtype[WPS_DEV_TYPE_BUFSIZE];
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002246 char params[300];
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002247 u8 empty_dev_type[8];
2248 unsigned int generated_pin = 0;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002249 struct wpa_supplicant *group = NULL;
2250
2251 if (group_id) {
2252 for (group = wpa_s->global->ifaces; group; group = group->next)
2253 {
2254 struct wpa_ssid *s = group->current_ssid;
2255 if (s != NULL &&
2256 s->mode == WPAS_MODE_P2P_GO &&
2257 group_id_len - ETH_ALEN == s->ssid_len &&
2258 os_memcmp(group_id + ETH_ALEN, s->ssid,
2259 s->ssid_len) == 0)
2260 break;
2261 }
2262 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002263
2264 if (pri_dev_type == NULL) {
2265 os_memset(empty_dev_type, 0, sizeof(empty_dev_type));
2266 pri_dev_type = empty_dev_type;
2267 }
2268 os_snprintf(params, sizeof(params), " p2p_dev_addr=" MACSTR
2269 " pri_dev_type=%s name='%s' config_methods=0x%x "
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002270 "dev_capab=0x%x group_capab=0x%x%s%s",
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002271 MAC2STR(dev_addr),
2272 wps_dev_type_bin2str(pri_dev_type, devtype,
2273 sizeof(devtype)),
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002274 dev_name, supp_config_methods, dev_capab, group_capab,
2275 group ? " group=" : "",
2276 group ? group->ifname : "");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002277 params[sizeof(params) - 1] = '\0';
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08002278
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002279 if (config_methods & WPS_CONFIG_DISPLAY) {
2280 generated_pin = wps_generate_pin();
2281 wpas_prov_disc_local_display(wpa_s, peer, params,
2282 generated_pin);
2283 } else if (config_methods & WPS_CONFIG_KEYPAD)
2284 wpas_prov_disc_local_keypad(wpa_s, peer, params);
2285 else if (config_methods & WPS_CONFIG_PUSHBUTTON)
2286 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_PBC_REQ MACSTR
2287 "%s", MAC2STR(peer), params);
Jouni Malinen75ecf522011-06-27 15:19:46 -07002288
2289 wpas_notify_p2p_provision_discovery(wpa_s, peer, 1 /* request */,
2290 P2P_PROV_DISC_SUCCESS,
2291 config_methods, generated_pin);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002292}
2293
2294
2295void wpas_prov_disc_resp(void *ctx, const u8 *peer, u16 config_methods)
2296{
2297 struct wpa_supplicant *wpa_s = ctx;
2298 unsigned int generated_pin = 0;
Dmitry Shmidt04949592012-07-19 12:16:46 -07002299 char params[20];
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002300
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08002301 if (wpa_s->pending_pd_before_join &&
2302 (os_memcmp(peer, wpa_s->pending_join_dev_addr, ETH_ALEN) == 0 ||
2303 os_memcmp(peer, wpa_s->pending_join_iface_addr, ETH_ALEN) == 0)) {
2304 wpa_s->pending_pd_before_join = 0;
2305 wpa_printf(MSG_DEBUG, "P2P: Starting pending "
2306 "join-existing-group operation");
2307 wpas_p2p_join_start(wpa_s);
2308 return;
2309 }
2310
Dmitry Shmidt04949592012-07-19 12:16:46 -07002311 if (wpa_s->pending_pd_use == AUTO_PD_JOIN ||
2312 wpa_s->pending_pd_use == AUTO_PD_GO_NEG)
2313 os_snprintf(params, sizeof(params), " peer_go=%d",
2314 wpa_s->pending_pd_use == AUTO_PD_JOIN);
2315 else
2316 params[0] = '\0';
2317
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002318 if (config_methods & WPS_CONFIG_DISPLAY)
Dmitry Shmidt04949592012-07-19 12:16:46 -07002319 wpas_prov_disc_local_keypad(wpa_s, peer, params);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002320 else if (config_methods & WPS_CONFIG_KEYPAD) {
2321 generated_pin = wps_generate_pin();
Dmitry Shmidt04949592012-07-19 12:16:46 -07002322 wpas_prov_disc_local_display(wpa_s, peer, params,
2323 generated_pin);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002324 } else if (config_methods & WPS_CONFIG_PUSHBUTTON)
Dmitry Shmidt04949592012-07-19 12:16:46 -07002325 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_PBC_RESP MACSTR
2326 "%s", MAC2STR(peer), params);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002327
Jouni Malinen75ecf522011-06-27 15:19:46 -07002328 wpas_notify_p2p_provision_discovery(wpa_s, peer, 0 /* response */,
2329 P2P_PROV_DISC_SUCCESS,
2330 config_methods, generated_pin);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002331}
2332
2333
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002334static void wpas_prov_disc_fail(void *ctx, const u8 *peer,
2335 enum p2p_prov_disc_status status)
Jouni Malinen75ecf522011-06-27 15:19:46 -07002336{
2337 struct wpa_supplicant *wpa_s = ctx;
2338
Dmitry Shmidt04949592012-07-19 12:16:46 -07002339 if (wpa_s->p2p_fallback_to_go_neg) {
2340 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: PD for p2p_connect-auto "
2341 "failed - fall back to GO Negotiation");
2342 wpas_p2p_fallback_to_go_neg(wpa_s, 0);
2343 return;
2344 }
2345
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002346 if (status == P2P_PROV_DISC_TIMEOUT_JOIN) {
Dmitry Shmidt2b380482012-09-13 10:52:13 -07002347 wpa_s->pending_pd_before_join = 0;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002348 wpa_printf(MSG_DEBUG, "P2P: Starting pending "
2349 "join-existing-group operation (no ACK for PD "
2350 "Req attempts)");
2351 wpas_p2p_join_start(wpa_s);
2352 return;
Dmitry Shmidt2b380482012-09-13 10:52:13 -07002353 }
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002354
Dmitry Shmidt04949592012-07-19 12:16:46 -07002355 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_FAILURE
2356 " p2p_dev_addr=" MACSTR " status=%d",
2357 MAC2STR(peer), status);
2358
Jouni Malinen75ecf522011-06-27 15:19:46 -07002359 wpas_notify_p2p_provision_discovery(wpa_s, peer, 0 /* response */,
2360 status, 0, 0);
2361}
2362
2363
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002364static u8 wpas_invitation_process(void *ctx, const u8 *sa, const u8 *bssid,
2365 const u8 *go_dev_addr, const u8 *ssid,
2366 size_t ssid_len, int *go, u8 *group_bssid,
2367 int *force_freq, int persistent_group)
2368{
2369 struct wpa_supplicant *wpa_s = ctx;
2370 struct wpa_ssid *s;
2371 u8 cur_bssid[ETH_ALEN];
2372 int res;
2373 struct wpa_supplicant *grp;
2374
2375 if (!persistent_group) {
2376 wpa_printf(MSG_DEBUG, "P2P: Invitation from " MACSTR
2377 " to join an active group", MAC2STR(sa));
2378 if (!is_zero_ether_addr(wpa_s->p2p_auth_invite) &&
2379 (os_memcmp(go_dev_addr, wpa_s->p2p_auth_invite, ETH_ALEN)
2380 == 0 ||
2381 os_memcmp(sa, wpa_s->p2p_auth_invite, ETH_ALEN) == 0)) {
2382 wpa_printf(MSG_DEBUG, "P2P: Accept previously "
2383 "authorized invitation");
2384 goto accept_inv;
2385 }
2386 /*
2387 * Do not accept the invitation automatically; notify user and
2388 * request approval.
2389 */
2390 return P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE;
2391 }
2392
2393 grp = wpas_get_p2p_group(wpa_s, ssid, ssid_len, go);
2394 if (grp) {
2395 wpa_printf(MSG_DEBUG, "P2P: Accept invitation to already "
2396 "running persistent group");
2397 if (*go)
2398 os_memcpy(group_bssid, grp->own_addr, ETH_ALEN);
2399 goto accept_inv;
2400 }
2401
2402 if (!wpa_s->conf->persistent_reconnect)
2403 return P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE;
2404
2405 for (s = wpa_s->conf->ssid; s; s = s->next) {
2406 if (s->disabled == 2 &&
2407 os_memcmp(s->bssid, go_dev_addr, ETH_ALEN) == 0 &&
2408 s->ssid_len == ssid_len &&
2409 os_memcmp(ssid, s->ssid, ssid_len) == 0)
2410 break;
2411 }
2412
2413 if (!s) {
2414 wpa_printf(MSG_DEBUG, "P2P: Invitation from " MACSTR
2415 " requested reinvocation of an unknown group",
2416 MAC2STR(sa));
2417 return P2P_SC_FAIL_UNKNOWN_GROUP;
2418 }
2419
2420 if (s->mode == WPAS_MODE_P2P_GO && !wpas_p2p_create_iface(wpa_s)) {
2421 *go = 1;
2422 if (wpa_s->wpa_state >= WPA_AUTHENTICATING) {
2423 wpa_printf(MSG_DEBUG, "P2P: The only available "
2424 "interface is already in use - reject "
2425 "invitation");
2426 return P2P_SC_FAIL_UNABLE_TO_ACCOMMODATE;
2427 }
2428 os_memcpy(group_bssid, wpa_s->own_addr, ETH_ALEN);
2429 } else if (s->mode == WPAS_MODE_P2P_GO) {
2430 *go = 1;
2431 if (wpas_p2p_add_group_interface(wpa_s, WPA_IF_P2P_GO) < 0)
2432 {
2433 wpa_printf(MSG_ERROR, "P2P: Failed to allocate a new "
2434 "interface address for the group");
2435 return P2P_SC_FAIL_UNABLE_TO_ACCOMMODATE;
2436 }
2437 os_memcpy(group_bssid, wpa_s->pending_interface_addr,
2438 ETH_ALEN);
2439 }
2440
2441accept_inv:
2442 if (wpa_s->current_ssid && wpa_drv_get_bssid(wpa_s, cur_bssid) == 0 &&
2443 wpa_s->assoc_freq) {
2444 wpa_printf(MSG_DEBUG, "P2P: Trying to force channel to match "
2445 "the channel we are already using");
2446 *force_freq = wpa_s->assoc_freq;
2447 }
2448
2449 res = wpa_drv_shared_freq(wpa_s);
2450 if (res > 0) {
2451 wpa_printf(MSG_DEBUG, "P2P: Trying to force channel to match "
2452 "with the channel we are already using on a "
2453 "shared interface");
2454 *force_freq = res;
2455 }
2456
2457 return P2P_SC_SUCCESS;
2458}
2459
2460
2461static void wpas_invitation_received(void *ctx, const u8 *sa, const u8 *bssid,
2462 const u8 *ssid, size_t ssid_len,
2463 const u8 *go_dev_addr, u8 status,
2464 int op_freq)
2465{
2466 struct wpa_supplicant *wpa_s = ctx;
2467 struct wpa_ssid *s;
2468
2469 for (s = wpa_s->conf->ssid; s; s = s->next) {
2470 if (s->disabled == 2 &&
2471 s->ssid_len == ssid_len &&
2472 os_memcmp(ssid, s->ssid, ssid_len) == 0)
2473 break;
2474 }
2475
2476 if (status == P2P_SC_SUCCESS) {
2477 wpa_printf(MSG_DEBUG, "P2P: Invitation from peer " MACSTR
2478 " was accepted; op_freq=%d MHz",
2479 MAC2STR(sa), op_freq);
2480 if (s) {
Dmitry Shmidt91c40cd2012-09-25 14:23:53 -07002481 int go = s->mode == WPAS_MODE_P2P_GO;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002482 wpas_p2p_group_add_persistent(
Dmitry Shmidt91c40cd2012-09-25 14:23:53 -07002483 wpa_s, s, go, go ? op_freq : 0, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002484 } else if (bssid) {
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002485 wpa_s->user_initiated_pd = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002486 wpas_p2p_join(wpa_s, bssid, go_dev_addr,
Dmitry Shmidt04949592012-07-19 12:16:46 -07002487 wpa_s->p2p_wps_method, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002488 }
2489 return;
2490 }
2491
2492 if (status != P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE) {
2493 wpa_printf(MSG_DEBUG, "P2P: Invitation from peer " MACSTR
2494 " was rejected (status %u)", MAC2STR(sa), status);
2495 return;
2496 }
2497
2498 if (!s) {
2499 if (bssid) {
2500 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_INVITATION_RECEIVED
2501 "sa=" MACSTR " go_dev_addr=" MACSTR
2502 " bssid=" MACSTR " unknown-network",
2503 MAC2STR(sa), MAC2STR(go_dev_addr),
2504 MAC2STR(bssid));
2505 } else {
2506 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_INVITATION_RECEIVED
2507 "sa=" MACSTR " go_dev_addr=" MACSTR
2508 " unknown-network",
2509 MAC2STR(sa), MAC2STR(go_dev_addr));
2510 }
2511 return;
2512 }
2513
2514 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_INVITATION_RECEIVED "sa=" MACSTR
2515 " persistent=%d", MAC2STR(sa), s->id);
2516}
2517
2518
2519static void wpas_invitation_result(void *ctx, int status, const u8 *bssid)
2520{
2521 struct wpa_supplicant *wpa_s = ctx;
2522 struct wpa_ssid *ssid;
2523
2524 if (bssid) {
2525 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_INVITATION_RESULT
2526 "status=%d " MACSTR,
2527 status, MAC2STR(bssid));
2528 } else {
2529 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_INVITATION_RESULT
2530 "status=%d ", status);
2531 }
2532 wpas_notify_p2p_invitation_result(wpa_s, status, bssid);
2533
2534 if (wpa_s->pending_invite_ssid_id == -1)
2535 return; /* Invitation to active group */
2536
2537 if (status != P2P_SC_SUCCESS) {
2538 wpas_p2p_remove_pending_group_interface(wpa_s);
2539 return;
2540 }
2541
2542 ssid = wpa_config_get_network(wpa_s->conf,
2543 wpa_s->pending_invite_ssid_id);
2544 if (ssid == NULL) {
2545 wpa_printf(MSG_ERROR, "P2P: Could not find persistent group "
2546 "data matching with invitation");
2547 return;
2548 }
2549
Jouni Malinen2c5b17d2012-10-13 21:52:46 -07002550 /*
2551 * The peer could have missed our ctrl::ack frame for Invitation
2552 * Response and continue retransmitting the frame. To reduce the
2553 * likelihood of the peer not getting successful TX status for the
2554 * Invitation Response frame, wait a short time here before starting
2555 * the persistent group so that we will remain on the current channel to
2556 * acknowledge any possible retransmission from the peer.
2557 */
Irfan Sheriff81931b82012-10-16 21:40:46 -07002558#ifndef ANDROID_P2P
Jouni Malinen2c5b17d2012-10-13 21:52:46 -07002559 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: 50 ms wait on current channel before "
2560 "starting persistent group");
2561 os_sleep(0, 50000);
Irfan Sheriff81931b82012-10-16 21:40:46 -07002562#else
2563 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: 100 ms wait on current channel before "
2564 "starting persistent group");
2565 os_sleep(0, 100000);
2566#endif
Jouni Malinen2c5b17d2012-10-13 21:52:46 -07002567
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002568 wpas_p2p_group_add_persistent(wpa_s, ssid,
Jouni Malinen31be0a42012-08-31 21:20:51 +03002569 ssid->mode == WPAS_MODE_P2P_GO,
2570 wpa_s->p2p_persistent_go_freq,
2571 wpa_s->p2p_go_ht40);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002572}
2573
2574
Dmitry Shmidt04949592012-07-19 12:16:46 -07002575static int wpas_p2p_disallowed_freq(struct wpa_global *global,
2576 unsigned int freq)
2577{
2578 unsigned int i;
2579
2580 if (global->p2p_disallow_freq == NULL)
2581 return 0;
2582
2583 for (i = 0; i < global->num_p2p_disallow_freq; i++) {
2584 if (freq >= global->p2p_disallow_freq[i].min &&
2585 freq <= global->p2p_disallow_freq[i].max)
2586 return 1;
2587 }
2588
2589 return 0;
2590}
2591
2592
2593static void wpas_p2p_add_chan(struct p2p_reg_class *reg, u8 chan)
2594{
2595 reg->channel[reg->channels] = chan;
2596 reg->channels++;
2597}
2598
2599
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002600static int wpas_p2p_default_channels(struct wpa_supplicant *wpa_s,
2601 struct p2p_channels *chan)
2602{
2603 int i, cla = 0;
2604
2605 wpa_printf(MSG_DEBUG, "P2P: Enable operating classes for 2.4 GHz "
2606 "band");
2607
2608 /* Operating class 81 - 2.4 GHz band channels 1..13 */
2609 chan->reg_class[cla].reg_class = 81;
Dmitry Shmidt04949592012-07-19 12:16:46 -07002610 chan->reg_class[cla].channels = 0;
2611 for (i = 0; i < 11; i++) {
2612 if (!wpas_p2p_disallowed_freq(wpa_s->global, 2412 + i * 5))
2613 wpas_p2p_add_chan(&chan->reg_class[cla], i + 1);
2614 }
2615 if (chan->reg_class[cla].channels)
2616 cla++;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002617
2618 wpa_printf(MSG_DEBUG, "P2P: Enable operating classes for lower 5 GHz "
2619 "band");
2620
2621 /* Operating class 115 - 5 GHz, channels 36-48 */
2622 chan->reg_class[cla].reg_class = 115;
Dmitry Shmidt04949592012-07-19 12:16:46 -07002623 chan->reg_class[cla].channels = 0;
2624 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 36 * 5))
2625 wpas_p2p_add_chan(&chan->reg_class[cla], 36);
2626 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 40 * 5))
2627 wpas_p2p_add_chan(&chan->reg_class[cla], 40);
2628 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 44 * 5))
2629 wpas_p2p_add_chan(&chan->reg_class[cla], 44);
2630 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 48 * 5))
2631 wpas_p2p_add_chan(&chan->reg_class[cla], 48);
2632 if (chan->reg_class[cla].channels)
2633 cla++;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002634
2635 wpa_printf(MSG_DEBUG, "P2P: Enable operating classes for higher 5 GHz "
2636 "band");
2637
2638 /* Operating class 124 - 5 GHz, channels 149,153,157,161 */
2639 chan->reg_class[cla].reg_class = 124;
Dmitry Shmidt04949592012-07-19 12:16:46 -07002640 chan->reg_class[cla].channels = 0;
2641 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 149 * 5))
2642 wpas_p2p_add_chan(&chan->reg_class[cla], 149);
2643 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 153 * 5))
2644 wpas_p2p_add_chan(&chan->reg_class[cla], 153);
2645 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 156 * 5))
2646 wpas_p2p_add_chan(&chan->reg_class[cla], 157);
2647 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 161 * 5))
2648 wpas_p2p_add_chan(&chan->reg_class[cla], 161);
2649 if (chan->reg_class[cla].channels)
2650 cla++;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002651
2652 chan->reg_classes = cla;
2653 return 0;
2654}
2655
2656
2657static struct hostapd_hw_modes * get_mode(struct hostapd_hw_modes *modes,
2658 u16 num_modes,
2659 enum hostapd_hw_mode mode)
2660{
2661 u16 i;
2662
2663 for (i = 0; i < num_modes; i++) {
2664 if (modes[i].mode == mode)
2665 return &modes[i];
2666 }
2667
2668 return NULL;
2669}
2670
2671
Dmitry Shmidt04949592012-07-19 12:16:46 -07002672static int has_channel(struct wpa_global *global,
2673 struct hostapd_hw_modes *mode, u8 chan, int *flags)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002674{
2675 int i;
Dmitry Shmidt04949592012-07-19 12:16:46 -07002676 unsigned int freq;
2677
2678 freq = (mode->mode == HOSTAPD_MODE_IEEE80211A ? 5000 : 2407) +
2679 chan * 5;
2680 if (wpas_p2p_disallowed_freq(global, freq))
2681 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002682
2683 for (i = 0; i < mode->num_channels; i++) {
2684 if (mode->channels[i].chan == chan) {
2685 if (flags)
2686 *flags = mode->channels[i].flag;
2687 return !(mode->channels[i].flag &
2688 (HOSTAPD_CHAN_DISABLED |
2689 HOSTAPD_CHAN_PASSIVE_SCAN |
2690 HOSTAPD_CHAN_NO_IBSS |
2691 HOSTAPD_CHAN_RADAR));
2692 }
2693 }
2694
2695 return 0;
2696}
2697
2698
2699struct p2p_oper_class_map {
2700 enum hostapd_hw_mode mode;
2701 u8 op_class;
2702 u8 min_chan;
2703 u8 max_chan;
2704 u8 inc;
2705 enum { BW20, BW40PLUS, BW40MINUS } bw;
2706};
2707
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002708static struct p2p_oper_class_map op_class[] = {
2709 { HOSTAPD_MODE_IEEE80211G, 81, 1, 13, 1, BW20 },
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002710#if 0 /* Do not enable HT40 on 2 GHz for now */
2711 { HOSTAPD_MODE_IEEE80211G, 83, 1, 9, 1, BW40PLUS },
2712 { HOSTAPD_MODE_IEEE80211G, 84, 5, 13, 1, BW40MINUS },
2713#endif
2714 { HOSTAPD_MODE_IEEE80211A, 115, 36, 48, 4, BW20 },
2715 { HOSTAPD_MODE_IEEE80211A, 124, 149, 161, 4, BW20 },
2716 { HOSTAPD_MODE_IEEE80211A, 116, 36, 44, 8, BW40PLUS },
2717 { HOSTAPD_MODE_IEEE80211A, 117, 40, 48, 8, BW40MINUS },
2718 { HOSTAPD_MODE_IEEE80211A, 126, 149, 157, 8, BW40PLUS },
2719 { HOSTAPD_MODE_IEEE80211A, 127, 153, 161, 8, BW40MINUS },
2720 { -1, 0, 0, 0, 0, BW20 }
2721};
2722
2723
2724static int wpas_p2p_verify_channel(struct wpa_supplicant *wpa_s,
2725 struct hostapd_hw_modes *mode,
2726 u8 channel, u8 bw)
2727{
2728 int flag;
2729
2730 if (!has_channel(wpa_s->global, mode, channel, &flag))
2731 return -1;
2732 if (bw == BW40MINUS &&
2733 (!(flag & HOSTAPD_CHAN_HT40MINUS) ||
2734 !has_channel(wpa_s->global, mode, channel - 4, NULL)))
2735 return 0;
2736 if (bw == BW40PLUS &&
2737 (!(flag & HOSTAPD_CHAN_HT40PLUS) ||
2738 !has_channel(wpa_s->global, mode, channel + 4, NULL)))
2739 return 0;
2740 return 1;
2741}
2742
2743
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002744static int wpas_p2p_setup_channels(struct wpa_supplicant *wpa_s,
2745 struct p2p_channels *chan)
2746{
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002747 struct hostapd_hw_modes *mode;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002748 int cla, op;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002749
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002750 if (wpa_s->hw.modes == NULL) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002751 wpa_printf(MSG_DEBUG, "P2P: Driver did not support fetching "
2752 "of all supported channels; assume dualband "
2753 "support");
2754 return wpas_p2p_default_channels(wpa_s, chan);
2755 }
2756
2757 cla = 0;
2758
2759 for (op = 0; op_class[op].op_class; op++) {
2760 struct p2p_oper_class_map *o = &op_class[op];
2761 u8 ch;
2762 struct p2p_reg_class *reg = NULL;
2763
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002764 mode = get_mode(wpa_s->hw.modes, wpa_s->hw.num_modes, o->mode);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002765 if (mode == NULL)
2766 continue;
2767 for (ch = o->min_chan; ch <= o->max_chan; ch += o->inc) {
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002768 if (wpas_p2p_verify_channel(wpa_s, mode, ch, o->bw) < 1)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002769 continue;
2770 if (reg == NULL) {
2771 wpa_printf(MSG_DEBUG, "P2P: Add operating "
2772 "class %u", o->op_class);
2773 reg = &chan->reg_class[cla];
2774 cla++;
2775 reg->reg_class = o->op_class;
2776 }
2777 reg->channel[reg->channels] = ch;
2778 reg->channels++;
2779 }
2780 if (reg) {
2781 wpa_hexdump(MSG_DEBUG, "P2P: Channels",
2782 reg->channel, reg->channels);
2783 }
2784 }
2785
2786 chan->reg_classes = cla;
2787
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002788 return 0;
2789}
2790
2791
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002792int wpas_p2p_get_ht40_mode(struct wpa_supplicant *wpa_s,
2793 struct hostapd_hw_modes *mode, u8 channel)
2794{
2795 int op, ret;
2796
2797 for (op = 0; op_class[op].op_class; op++) {
2798 struct p2p_oper_class_map *o = &op_class[op];
2799 u8 ch;
2800
2801 for (ch = o->min_chan; ch <= o->max_chan; ch += o->inc) {
2802 if (o->mode != HOSTAPD_MODE_IEEE80211A ||
2803 o->bw == BW20 || ch != channel)
2804 continue;
2805 ret = wpas_p2p_verify_channel(wpa_s, mode, ch, o->bw);
2806 if (ret < 0)
2807 continue;
2808 else if (ret > 0)
2809 return (o->bw == BW40MINUS) ? -1 : 1;
2810 else
2811 return 0;
2812 }
2813 }
2814 return 0;
2815}
2816
2817
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002818static int wpas_get_noa(void *ctx, const u8 *interface_addr, u8 *buf,
2819 size_t buf_len)
2820{
2821 struct wpa_supplicant *wpa_s = ctx;
2822
2823 for (wpa_s = wpa_s->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
2824 if (os_memcmp(wpa_s->own_addr, interface_addr, ETH_ALEN) == 0)
2825 break;
2826 }
2827 if (wpa_s == NULL)
2828 return -1;
2829
2830 return wpa_drv_get_noa(wpa_s, buf, buf_len);
2831}
2832
2833
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08002834static int wpas_go_connected(void *ctx, const u8 *dev_addr)
2835{
2836 struct wpa_supplicant *wpa_s = ctx;
2837
2838 for (wpa_s = wpa_s->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
2839 struct wpa_ssid *ssid = wpa_s->current_ssid;
2840 if (ssid == NULL)
2841 continue;
2842 if (ssid->mode != WPAS_MODE_INFRA)
2843 continue;
2844 if (wpa_s->wpa_state != WPA_COMPLETED &&
2845 wpa_s->wpa_state != WPA_GROUP_HANDSHAKE)
2846 continue;
2847 if (os_memcmp(wpa_s->go_dev_addr, dev_addr, ETH_ALEN) == 0)
2848 return 1;
2849 }
2850
2851 return 0;
2852}
2853
2854
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002855/**
2856 * wpas_p2p_init - Initialize P2P module for %wpa_supplicant
2857 * @global: Pointer to global data from wpa_supplicant_init()
2858 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
2859 * Returns: 0 on success, -1 on failure
2860 */
2861int wpas_p2p_init(struct wpa_global *global, struct wpa_supplicant *wpa_s)
2862{
2863 struct p2p_config p2p;
2864 unsigned int r;
2865 int i;
2866
2867 if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_CAPABLE))
2868 return 0;
2869
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002870 if (global->p2p)
2871 return 0;
2872
2873 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT) {
2874 struct p2p_params params;
2875
2876 wpa_printf(MSG_DEBUG, "P2P: Use driver-based P2P management");
2877 os_memset(&params, 0, sizeof(params));
2878 params.dev_name = wpa_s->conf->device_name;
2879 os_memcpy(params.pri_dev_type, wpa_s->conf->device_type,
2880 WPS_DEV_TYPE_LEN);
2881 params.num_sec_dev_types = wpa_s->conf->num_sec_device_types;
2882 os_memcpy(params.sec_dev_type,
2883 wpa_s->conf->sec_device_type,
2884 params.num_sec_dev_types * WPS_DEV_TYPE_LEN);
2885
2886 if (wpa_drv_p2p_set_params(wpa_s, &params) < 0)
2887 return -1;
2888
2889 return 0;
2890 }
2891
2892 os_memset(&p2p, 0, sizeof(p2p));
2893 p2p.msg_ctx = wpa_s;
2894 p2p.cb_ctx = wpa_s;
2895 p2p.p2p_scan = wpas_p2p_scan;
2896 p2p.send_action = wpas_send_action;
2897 p2p.send_action_done = wpas_send_action_done;
2898 p2p.go_neg_completed = wpas_go_neg_completed;
2899 p2p.go_neg_req_rx = wpas_go_neg_req_rx;
2900 p2p.dev_found = wpas_dev_found;
2901 p2p.dev_lost = wpas_dev_lost;
2902 p2p.start_listen = wpas_start_listen;
2903 p2p.stop_listen = wpas_stop_listen;
2904 p2p.send_probe_resp = wpas_send_probe_resp;
2905 p2p.sd_request = wpas_sd_request;
2906 p2p.sd_response = wpas_sd_response;
2907 p2p.prov_disc_req = wpas_prov_disc_req;
2908 p2p.prov_disc_resp = wpas_prov_disc_resp;
Jouni Malinen75ecf522011-06-27 15:19:46 -07002909 p2p.prov_disc_fail = wpas_prov_disc_fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002910 p2p.invitation_process = wpas_invitation_process;
2911 p2p.invitation_received = wpas_invitation_received;
2912 p2p.invitation_result = wpas_invitation_result;
2913 p2p.get_noa = wpas_get_noa;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08002914 p2p.go_connected = wpas_go_connected;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002915
2916 os_memcpy(wpa_s->global->p2p_dev_addr, wpa_s->own_addr, ETH_ALEN);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002917 os_memcpy(p2p.dev_addr, wpa_s->global->p2p_dev_addr, ETH_ALEN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002918 p2p.dev_name = wpa_s->conf->device_name;
2919 p2p.manufacturer = wpa_s->conf->manufacturer;
2920 p2p.model_name = wpa_s->conf->model_name;
2921 p2p.model_number = wpa_s->conf->model_number;
2922 p2p.serial_number = wpa_s->conf->serial_number;
2923 if (wpa_s->wps) {
2924 os_memcpy(p2p.uuid, wpa_s->wps->uuid, 16);
2925 p2p.config_methods = wpa_s->wps->config_methods;
2926 }
2927
2928 if (wpa_s->conf->p2p_listen_reg_class &&
2929 wpa_s->conf->p2p_listen_channel) {
2930 p2p.reg_class = wpa_s->conf->p2p_listen_reg_class;
2931 p2p.channel = wpa_s->conf->p2p_listen_channel;
2932 } else {
2933 p2p.reg_class = 81;
2934 /*
2935 * Pick one of the social channels randomly as the listen
2936 * channel.
2937 */
2938 os_get_random((u8 *) &r, sizeof(r));
2939 p2p.channel = 1 + (r % 3) * 5;
2940 }
2941 wpa_printf(MSG_DEBUG, "P2P: Own listen channel: %d", p2p.channel);
2942
2943 if (wpa_s->conf->p2p_oper_reg_class &&
2944 wpa_s->conf->p2p_oper_channel) {
2945 p2p.op_reg_class = wpa_s->conf->p2p_oper_reg_class;
2946 p2p.op_channel = wpa_s->conf->p2p_oper_channel;
2947 p2p.cfg_op_channel = 1;
2948 wpa_printf(MSG_DEBUG, "P2P: Configured operating channel: "
2949 "%d:%d", p2p.op_reg_class, p2p.op_channel);
2950
2951 } else {
2952 p2p.op_reg_class = 81;
2953 /*
2954 * Use random operation channel from (1, 6, 11) if no other
2955 * preference is indicated.
2956 */
2957 os_get_random((u8 *) &r, sizeof(r));
2958 p2p.op_channel = 1 + (r % 3) * 5;
2959 p2p.cfg_op_channel = 0;
2960 wpa_printf(MSG_DEBUG, "P2P: Random operating channel: "
2961 "%d:%d", p2p.op_reg_class, p2p.op_channel);
2962 }
2963 if (wpa_s->conf->country[0] && wpa_s->conf->country[1]) {
2964 os_memcpy(p2p.country, wpa_s->conf->country, 2);
2965 p2p.country[2] = 0x04;
2966 } else
2967 os_memcpy(p2p.country, "XX\x04", 3);
2968
2969 if (wpas_p2p_setup_channels(wpa_s, &p2p.channels)) {
2970 wpa_printf(MSG_ERROR, "P2P: Failed to configure supported "
2971 "channel list");
2972 return -1;
2973 }
2974
2975 os_memcpy(p2p.pri_dev_type, wpa_s->conf->device_type,
2976 WPS_DEV_TYPE_LEN);
2977
2978 p2p.num_sec_dev_types = wpa_s->conf->num_sec_device_types;
2979 os_memcpy(p2p.sec_dev_type, wpa_s->conf->sec_device_type,
2980 p2p.num_sec_dev_types * WPS_DEV_TYPE_LEN);
2981
2982 p2p.concurrent_operations = !!(wpa_s->drv_flags &
2983 WPA_DRIVER_FLAGS_P2P_CONCURRENT);
2984
2985 p2p.max_peers = 100;
2986
2987 if (wpa_s->conf->p2p_ssid_postfix) {
2988 p2p.ssid_postfix_len =
2989 os_strlen(wpa_s->conf->p2p_ssid_postfix);
2990 if (p2p.ssid_postfix_len > sizeof(p2p.ssid_postfix))
2991 p2p.ssid_postfix_len = sizeof(p2p.ssid_postfix);
2992 os_memcpy(p2p.ssid_postfix, wpa_s->conf->p2p_ssid_postfix,
2993 p2p.ssid_postfix_len);
2994 }
2995
2996 p2p.p2p_intra_bss = wpa_s->conf->p2p_intra_bss;
2997
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002998 p2p.max_listen = wpa_s->max_remain_on_chan;
2999
Dmitry Shmidt04abaa92012-08-17 16:22:31 -07003000#ifdef ANDROID_P2P
Irfan Sheriffaf84a572012-09-22 16:59:30 -07003001 if(wpa_s->drv_flags & WPA_DRIVER_FLAGS_MULTI_CHANNEL_CONCURRENT) {
Dmitry Shmidt04abaa92012-08-17 16:22:31 -07003002 p2p.p2p_concurrency = P2P_MULTI_CHANNEL_CONCURRENT;
Irfan Sheriffaf84a572012-09-22 16:59:30 -07003003 wpa_printf(MSG_DEBUG, "P2P: Multi channel concurrency support");
3004 } else {
3005 // Add support for WPA_DRIVER_FLAGS_P2P_CONCURRENT
Dmitry Shmidt04abaa92012-08-17 16:22:31 -07003006 p2p.p2p_concurrency = P2P_SINGLE_CHANNEL_CONCURRENT;
Irfan Sheriffaf84a572012-09-22 16:59:30 -07003007 wpa_printf(MSG_DEBUG, "P2P: Single channel concurrency support");
3008 }
Dmitry Shmidt04abaa92012-08-17 16:22:31 -07003009#endif
3010
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003011 global->p2p = p2p_init(&p2p);
3012 if (global->p2p == NULL)
3013 return -1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003014 global->p2p_init_wpa_s = wpa_s;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003015
3016 for (i = 0; i < MAX_WPS_VENDOR_EXT; i++) {
3017 if (wpa_s->conf->wps_vendor_ext[i] == NULL)
3018 continue;
3019 p2p_add_wps_vendor_extension(
3020 global->p2p, wpa_s->conf->wps_vendor_ext[i]);
3021 }
3022
3023 return 0;
3024}
3025
3026
3027/**
3028 * wpas_p2p_deinit - Deinitialize per-interface P2P data
3029 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
3030 *
3031 * This function deinitialize per-interface P2P data.
3032 */
3033void wpas_p2p_deinit(struct wpa_supplicant *wpa_s)
3034{
3035 if (wpa_s->driver && wpa_s->drv_priv)
3036 wpa_drv_probe_req_report(wpa_s, 0);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003037
3038 if (wpa_s->go_params) {
3039 /* Clear any stored provisioning info */
3040 p2p_clear_provisioning_info(
3041 wpa_s->global->p2p,
Dmitry Shmidt04949592012-07-19 12:16:46 -07003042 wpa_s->go_params->peer_device_addr);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003043 }
3044
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003045 os_free(wpa_s->go_params);
3046 wpa_s->go_params = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003047 eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s, NULL);
3048 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
3049 wpa_s->p2p_long_listen = 0;
3050 eloop_cancel_timeout(wpas_p2p_long_listen_timeout, wpa_s, NULL);
3051 eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL);
3052 wpas_p2p_remove_pending_group_interface(wpa_s);
3053
3054 /* TODO: remove group interface from the driver if this wpa_s instance
3055 * is on top of a P2P group interface */
3056}
3057
3058
3059/**
3060 * wpas_p2p_deinit_global - Deinitialize global P2P module
3061 * @global: Pointer to global data from wpa_supplicant_init()
3062 *
3063 * This function deinitializes the global (per device) P2P module.
3064 */
3065void wpas_p2p_deinit_global(struct wpa_global *global)
3066{
3067 struct wpa_supplicant *wpa_s, *tmp;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003068
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003069 wpa_s = global->ifaces;
3070 if (wpa_s)
3071 wpas_p2p_service_flush(wpa_s);
3072
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003073 if (global->p2p == NULL)
3074 return;
3075
3076 /* Remove remaining P2P group interfaces */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003077 while (wpa_s && wpa_s->p2p_group_interface != NOT_P2P_GROUP_INTERFACE)
3078 wpa_s = wpa_s->next;
3079 while (wpa_s) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003080 tmp = global->ifaces;
3081 while (tmp &&
3082 (tmp == wpa_s ||
3083 tmp->p2p_group_interface == NOT_P2P_GROUP_INTERFACE)) {
3084 tmp = tmp->next;
3085 }
3086 if (tmp == NULL)
3087 break;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003088 /* Disconnect from the P2P group and deinit the interface */
3089 wpas_p2p_disconnect(tmp);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003090 }
3091
3092 /*
3093 * Deinit GO data on any possibly remaining interface (if main
3094 * interface is used as GO).
3095 */
3096 for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
3097 if (wpa_s->ap_iface)
3098 wpas_p2p_group_deinit(wpa_s);
3099 }
3100
3101 p2p_deinit(global->p2p);
3102 global->p2p = NULL;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003103 global->p2p_init_wpa_s = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003104}
3105
3106
3107static int wpas_p2p_create_iface(struct wpa_supplicant *wpa_s)
3108{
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08003109 if (wpa_s->conf->p2p_no_group_iface)
3110 return 0; /* separate interface disabled per configuration */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003111 if (wpa_s->drv_flags &
3112 (WPA_DRIVER_FLAGS_P2P_DEDICATED_INTERFACE |
3113 WPA_DRIVER_FLAGS_P2P_MGMT_AND_NON_P2P))
3114 return 1; /* P2P group requires a new interface in every case
3115 */
3116 if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_CONCURRENT))
3117 return 0; /* driver does not support concurrent operations */
3118 if (wpa_s->global->ifaces->next)
3119 return 1; /* more that one interface already in use */
3120 if (wpa_s->wpa_state >= WPA_AUTHENTICATING)
3121 return 1; /* this interface is already in use */
3122 return 0;
3123}
3124
3125
3126static int wpas_p2p_start_go_neg(struct wpa_supplicant *wpa_s,
3127 const u8 *peer_addr,
3128 enum p2p_wps_method wps_method,
3129 int go_intent, const u8 *own_interface_addr,
Dmitry Shmidt04949592012-07-19 12:16:46 -07003130 unsigned int force_freq, int persistent_group,
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08003131 struct wpa_ssid *ssid, unsigned int pref_freq)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003132{
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003133 if (persistent_group && wpa_s->conf->persistent_reconnect)
3134 persistent_group = 2;
3135
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003136 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT) {
3137 return wpa_drv_p2p_connect(wpa_s, peer_addr, wps_method,
3138 go_intent, own_interface_addr,
3139 force_freq, persistent_group);
3140 }
3141
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003142 /*
3143 * Increase GO config timeout if HT40 is used since it takes some time
3144 * to scan channels for coex purposes before the BSS can be started.
3145 */
3146 p2p_set_config_timeout(wpa_s->global->p2p,
3147 wpa_s->p2p_go_ht40 ? 255 : 100, 20);
3148
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003149 return p2p_connect(wpa_s->global->p2p, peer_addr, wps_method,
3150 go_intent, own_interface_addr, force_freq,
Dmitry Shmidt04949592012-07-19 12:16:46 -07003151 persistent_group, ssid ? ssid->ssid : NULL,
3152 ssid ? ssid->ssid_len : 0,
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08003153 wpa_s->p2p_pd_before_go_neg, pref_freq);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003154}
3155
3156
3157static int wpas_p2p_auth_go_neg(struct wpa_supplicant *wpa_s,
3158 const u8 *peer_addr,
3159 enum p2p_wps_method wps_method,
3160 int go_intent, const u8 *own_interface_addr,
Dmitry Shmidt04949592012-07-19 12:16:46 -07003161 unsigned int force_freq, int persistent_group,
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08003162 struct wpa_ssid *ssid, unsigned int pref_freq)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003163{
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003164 if (persistent_group && wpa_s->conf->persistent_reconnect)
3165 persistent_group = 2;
3166
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003167 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
3168 return -1;
3169
3170 return p2p_authorize(wpa_s->global->p2p, peer_addr, wps_method,
3171 go_intent, own_interface_addr, force_freq,
Dmitry Shmidt04949592012-07-19 12:16:46 -07003172 persistent_group, ssid ? ssid->ssid : NULL,
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08003173 ssid ? ssid->ssid_len : 0, pref_freq);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003174}
3175
3176
3177static void wpas_p2p_check_join_scan_limit(struct wpa_supplicant *wpa_s)
3178{
3179 wpa_s->p2p_join_scan_count++;
3180 wpa_printf(MSG_DEBUG, "P2P: Join scan attempt %d",
3181 wpa_s->p2p_join_scan_count);
3182 if (wpa_s->p2p_join_scan_count > P2P_MAX_JOIN_SCAN_ATTEMPTS) {
3183 wpa_printf(MSG_DEBUG, "P2P: Failed to find GO " MACSTR
3184 " for join operationg - stop join attempt",
3185 MAC2STR(wpa_s->pending_join_iface_addr));
3186 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
Dmitry Shmidt04949592012-07-19 12:16:46 -07003187 if (wpa_s->p2p_auto_pd) {
3188 wpa_s->p2p_auto_pd = 0;
3189 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_FAILURE
3190 " p2p_dev_addr=" MACSTR " status=N/A",
3191 MAC2STR(wpa_s->pending_join_dev_addr));
3192 return;
3193 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003194 wpa_msg(wpa_s->parent, MSG_INFO,
3195 P2P_EVENT_GROUP_FORMATION_FAILURE);
3196 }
3197}
3198
3199
Dmitry Shmidt04949592012-07-19 12:16:46 -07003200static int wpas_check_freq_conflict(struct wpa_supplicant *wpa_s, int freq)
3201{
3202 struct wpa_supplicant *iface;
3203 int shared_freq;
3204 u8 bssid[ETH_ALEN];
3205
3206 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_MULTI_CHANNEL_CONCURRENT)
3207 return 0;
3208
3209 for (iface = wpa_s->global->ifaces; iface; iface = iface->next) {
3210 if (!wpas_p2p_create_iface(wpa_s) && iface == wpa_s)
3211 continue;
3212 if (iface->current_ssid == NULL || iface->assoc_freq == 0)
3213 continue;
3214 if (iface->current_ssid->mode == WPAS_MODE_AP ||
3215 iface->current_ssid->mode == WPAS_MODE_P2P_GO)
3216 shared_freq = iface->current_ssid->frequency;
3217 else if (wpa_drv_get_bssid(iface, bssid) == 0)
3218 shared_freq = iface->assoc_freq;
3219 else
3220 shared_freq = 0;
3221
3222 if (shared_freq && freq != shared_freq) {
3223 wpa_printf(MSG_DEBUG, "P2P: Frequency conflict - %s "
3224 "connected on %d MHz - new connection on "
3225 "%d MHz", iface->ifname, shared_freq, freq);
3226 return 1;
3227 }
3228 }
3229
3230 shared_freq = wpa_drv_shared_freq(wpa_s);
3231 if (shared_freq > 0 && shared_freq != freq) {
3232 wpa_printf(MSG_DEBUG, "P2P: Frequency conflict - shared "
3233 "virtual interface connected on %d MHz - new "
3234 "connection on %d MHz", shared_freq, freq);
3235 return 1;
3236 }
3237
3238 return 0;
3239}
3240
3241
3242static int wpas_p2p_peer_go(struct wpa_supplicant *wpa_s,
3243 const u8 *peer_dev_addr)
3244{
3245 struct wpa_bss *bss;
3246 int updated;
3247
3248 bss = wpa_bss_get_p2p_dev_addr(wpa_s, peer_dev_addr);
3249 if (bss == NULL)
3250 return -1;
3251 if (bss->last_update_idx < wpa_s->bss_update_idx) {
3252 wpa_printf(MSG_DEBUG, "P2P: Peer BSS entry not updated in the "
3253 "last scan");
3254 return 0;
3255 }
3256
3257 updated = os_time_before(&wpa_s->p2p_auto_started, &bss->last_update);
3258 wpa_printf(MSG_DEBUG, "P2P: Current BSS entry for peer updated at "
3259 "%ld.%06ld (%supdated in last scan)",
3260 bss->last_update.sec, bss->last_update.usec,
3261 updated ? "": "not ");
3262
3263 return updated;
3264}
3265
3266
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003267static void wpas_p2p_scan_res_join(struct wpa_supplicant *wpa_s,
3268 struct wpa_scan_results *scan_res)
3269{
3270 struct wpa_bss *bss;
3271 int freq;
3272 u8 iface_addr[ETH_ALEN];
Dmitry Shmidt04949592012-07-19 12:16:46 -07003273
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003274 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
3275
3276 if (wpa_s->global->p2p_disabled)
3277 return;
3278
Dmitry Shmidt04949592012-07-19 12:16:46 -07003279 wpa_printf(MSG_DEBUG, "P2P: Scan results received (%d BSS) for %sjoin",
3280 scan_res ? (int) scan_res->num : -1,
3281 wpa_s->p2p_auto_join ? "auto_" : "");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003282
3283 if (scan_res)
3284 wpas_p2p_scan_res_handler(wpa_s, scan_res);
3285
Dmitry Shmidt04949592012-07-19 12:16:46 -07003286 if (wpa_s->p2p_auto_pd) {
3287 int join = wpas_p2p_peer_go(wpa_s,
3288 wpa_s->pending_join_dev_addr);
3289 if (join == 0 &&
3290 wpa_s->auto_pd_scan_retry < P2P_AUTO_PD_SCAN_ATTEMPTS) {
3291 wpa_s->auto_pd_scan_retry++;
3292 bss = wpa_bss_get_bssid(wpa_s,
3293 wpa_s->pending_join_dev_addr);
3294 if (bss) {
3295 freq = bss->freq;
3296 wpa_printf(MSG_DEBUG, "P2P: Scan retry %d for "
3297 "the peer " MACSTR " at %d MHz",
3298 wpa_s->auto_pd_scan_retry,
3299 MAC2STR(wpa_s->
3300 pending_join_dev_addr),
3301 freq);
3302 wpas_p2p_join_scan_req(wpa_s, freq);
3303 return;
3304 }
3305 }
3306
3307 if (join < 0)
3308 join = 0;
3309
3310 wpa_s->p2p_auto_pd = 0;
3311 wpa_s->pending_pd_use = join ? AUTO_PD_JOIN : AUTO_PD_GO_NEG;
3312 wpa_printf(MSG_DEBUG, "P2P: Auto PD with " MACSTR " join=%d",
3313 MAC2STR(wpa_s->pending_join_dev_addr), join);
3314 if (p2p_prov_disc_req(wpa_s->global->p2p,
3315 wpa_s->pending_join_dev_addr,
3316 wpa_s->pending_pd_config_methods, join,
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08003317 0, wpa_s->user_initiated_pd) < 0) {
Dmitry Shmidt04949592012-07-19 12:16:46 -07003318 wpa_s->p2p_auto_pd = 0;
3319 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_FAILURE
3320 " p2p_dev_addr=" MACSTR " status=N/A",
3321 MAC2STR(wpa_s->pending_join_dev_addr));
3322 }
3323 return;
3324 }
3325
3326 if (wpa_s->p2p_auto_join) {
3327 int join = wpas_p2p_peer_go(wpa_s,
3328 wpa_s->pending_join_dev_addr);
3329 if (join < 0) {
3330 wpa_printf(MSG_DEBUG, "P2P: Peer was not found to be "
3331 "running a GO -> use GO Negotiation");
3332 wpas_p2p_connect(wpa_s, wpa_s->pending_join_dev_addr,
3333 wpa_s->p2p_pin, wpa_s->p2p_wps_method,
3334 wpa_s->p2p_persistent_group, 0, 0, 0,
3335 wpa_s->p2p_go_intent,
3336 wpa_s->p2p_connect_freq,
3337 wpa_s->p2p_persistent_id,
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003338 wpa_s->p2p_pd_before_go_neg,
3339 wpa_s->p2p_go_ht40);
Dmitry Shmidt04949592012-07-19 12:16:46 -07003340 return;
3341 }
3342
3343 wpa_printf(MSG_DEBUG, "P2P: Peer was found running GO%s -> "
3344 "try to join the group", join ? "" :
3345 " in older scan");
3346 if (!join)
3347 wpa_s->p2p_fallback_to_go_neg = 1;
3348 }
3349
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003350 freq = p2p_get_oper_freq(wpa_s->global->p2p,
3351 wpa_s->pending_join_iface_addr);
3352 if (freq < 0 &&
3353 p2p_get_interface_addr(wpa_s->global->p2p,
3354 wpa_s->pending_join_dev_addr,
3355 iface_addr) == 0 &&
3356 os_memcmp(iface_addr, wpa_s->pending_join_dev_addr, ETH_ALEN) != 0)
3357 {
3358 wpa_printf(MSG_DEBUG, "P2P: Overwrite pending interface "
3359 "address for join from " MACSTR " to " MACSTR
3360 " based on newly discovered P2P peer entry",
3361 MAC2STR(wpa_s->pending_join_iface_addr),
3362 MAC2STR(iface_addr));
3363 os_memcpy(wpa_s->pending_join_iface_addr, iface_addr,
3364 ETH_ALEN);
3365
3366 freq = p2p_get_oper_freq(wpa_s->global->p2p,
3367 wpa_s->pending_join_iface_addr);
3368 }
3369 if (freq >= 0) {
3370 wpa_printf(MSG_DEBUG, "P2P: Target GO operating frequency "
3371 "from P2P peer table: %d MHz", freq);
3372 }
3373 bss = wpa_bss_get_bssid(wpa_s, wpa_s->pending_join_iface_addr);
3374 if (bss) {
3375 freq = bss->freq;
3376 wpa_printf(MSG_DEBUG, "P2P: Target GO operating frequency "
3377 "from BSS table: %d MHz", freq);
3378 }
3379 if (freq > 0) {
3380 u16 method;
3381
Dmitry Shmidt04949592012-07-19 12:16:46 -07003382 if (wpas_check_freq_conflict(wpa_s, freq) > 0) {
3383 wpa_msg(wpa_s->parent, MSG_INFO,
3384 P2P_EVENT_GROUP_FORMATION_FAILURE
3385 "reason=FREQ_CONFLICT");
3386 return;
3387 }
3388
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003389 wpa_printf(MSG_DEBUG, "P2P: Send Provision Discovery Request "
3390 "prior to joining an existing group (GO " MACSTR
3391 " freq=%u MHz)",
3392 MAC2STR(wpa_s->pending_join_dev_addr), freq);
3393 wpa_s->pending_pd_before_join = 1;
3394
3395 switch (wpa_s->pending_join_wps_method) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003396 case WPS_PIN_DISPLAY:
3397 method = WPS_CONFIG_KEYPAD;
3398 break;
3399 case WPS_PIN_KEYPAD:
3400 method = WPS_CONFIG_DISPLAY;
3401 break;
3402 case WPS_PBC:
3403 method = WPS_CONFIG_PUSHBUTTON;
3404 break;
3405 default:
3406 method = 0;
3407 break;
3408 }
3409
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003410 if ((p2p_get_provisioning_info(wpa_s->global->p2p,
3411 wpa_s->pending_join_dev_addr) ==
3412 method)) {
3413 /*
3414 * We have already performed provision discovery for
3415 * joining the group. Proceed directly to join
3416 * operation without duplicated provision discovery. */
3417 wpa_printf(MSG_DEBUG, "P2P: Provision discovery "
3418 "with " MACSTR " already done - proceed to "
3419 "join",
3420 MAC2STR(wpa_s->pending_join_dev_addr));
3421 wpa_s->pending_pd_before_join = 0;
3422 goto start;
3423 }
3424
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003425 if (p2p_prov_disc_req(wpa_s->global->p2p,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003426 wpa_s->pending_join_dev_addr, method, 1,
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08003427 freq, wpa_s->user_initiated_pd) < 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003428 wpa_printf(MSG_DEBUG, "P2P: Failed to send Provision "
3429 "Discovery Request before joining an "
3430 "existing group");
3431 wpa_s->pending_pd_before_join = 0;
3432 goto start;
3433 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003434 return;
3435 }
3436
3437 wpa_printf(MSG_DEBUG, "P2P: Failed to find BSS/GO - try again later");
3438 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
3439 eloop_register_timeout(1, 0, wpas_p2p_join_scan, wpa_s, NULL);
3440 wpas_p2p_check_join_scan_limit(wpa_s);
3441 return;
3442
3443start:
3444 /* Start join operation immediately */
3445 wpas_p2p_join_start(wpa_s);
3446}
3447
3448
Dmitry Shmidt04949592012-07-19 12:16:46 -07003449static void wpas_p2p_join_scan_req(struct wpa_supplicant *wpa_s, int freq)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003450{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003451 int ret;
3452 struct wpa_driver_scan_params params;
3453 struct wpabuf *wps_ie, *ies;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003454 size_t ielen;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003455 int freqs[2] = { 0, 0 };
Dmitry Shmidtad266fb2012-08-24 17:03:35 -07003456#ifdef ANDROID_P2P
3457 int oper_freq;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003458
Dmitry Shmidtad266fb2012-08-24 17:03:35 -07003459 /* If freq is not provided, check the operating freq of the GO and do a
3460 * a directed scan to save time
3461 */
3462 if(!freq) {
3463 freq = (oper_freq = p2p_get_oper_freq(wpa_s->global->p2p,
3464 wpa_s->pending_join_iface_addr) == -1) ? 0 : oper_freq;
3465 }
3466#endif
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003467 os_memset(&params, 0, sizeof(params));
3468
3469 /* P2P Wildcard SSID */
3470 params.num_ssids = 1;
3471 params.ssids[0].ssid = (u8 *) P2P_WILDCARD_SSID;
3472 params.ssids[0].ssid_len = P2P_WILDCARD_SSID_LEN;
3473
3474 wpa_s->wps->dev.p2p = 1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003475 wps_ie = wps_build_probe_req_ie(DEV_PW_DEFAULT, &wpa_s->wps->dev,
3476 wpa_s->wps->uuid, WPS_REQ_ENROLLEE, 0,
3477 NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003478 if (wps_ie == NULL) {
3479 wpas_p2p_scan_res_join(wpa_s, NULL);
3480 return;
3481 }
3482
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003483 ielen = p2p_scan_ie_buf_len(wpa_s->global->p2p);
3484 ies = wpabuf_alloc(wpabuf_len(wps_ie) + ielen);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003485 if (ies == NULL) {
3486 wpabuf_free(wps_ie);
3487 wpas_p2p_scan_res_join(wpa_s, NULL);
3488 return;
3489 }
3490 wpabuf_put_buf(ies, wps_ie);
3491 wpabuf_free(wps_ie);
3492
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08003493 p2p_scan_ie(wpa_s->global->p2p, ies, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003494
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003495 params.p2p_probe = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003496 params.extra_ies = wpabuf_head(ies);
3497 params.extra_ies_len = wpabuf_len(ies);
Dmitry Shmidt04949592012-07-19 12:16:46 -07003498 if (freq > 0) {
3499 freqs[0] = freq;
3500 params.freqs = freqs;
3501 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003502
3503 /*
3504 * Run a scan to update BSS table and start Provision Discovery once
3505 * the new scan results become available.
3506 */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003507 ret = wpa_drv_scan(wpa_s, &params);
Dmitry Shmidt04949592012-07-19 12:16:46 -07003508 if (!ret)
3509 wpa_s->scan_res_handler = wpas_p2p_scan_res_join;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003510
3511 wpabuf_free(ies);
3512
3513 if (ret) {
3514 wpa_printf(MSG_DEBUG, "P2P: Failed to start scan for join - "
3515 "try again later");
3516 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
3517 eloop_register_timeout(1, 0, wpas_p2p_join_scan, wpa_s, NULL);
3518 wpas_p2p_check_join_scan_limit(wpa_s);
3519 }
3520}
3521
3522
Dmitry Shmidt04949592012-07-19 12:16:46 -07003523static void wpas_p2p_join_scan(void *eloop_ctx, void *timeout_ctx)
3524{
3525 struct wpa_supplicant *wpa_s = eloop_ctx;
3526 wpas_p2p_join_scan_req(wpa_s, 0);
3527}
3528
3529
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003530static int wpas_p2p_join(struct wpa_supplicant *wpa_s, const u8 *iface_addr,
Dmitry Shmidt04949592012-07-19 12:16:46 -07003531 const u8 *dev_addr, enum p2p_wps_method wps_method,
3532 int auto_join)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003533{
3534 wpa_printf(MSG_DEBUG, "P2P: Request to join existing group (iface "
Dmitry Shmidt04949592012-07-19 12:16:46 -07003535 MACSTR " dev " MACSTR ")%s",
3536 MAC2STR(iface_addr), MAC2STR(dev_addr),
3537 auto_join ? " (auto_join)" : "");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003538
Dmitry Shmidt04949592012-07-19 12:16:46 -07003539 wpa_s->p2p_auto_pd = 0;
3540 wpa_s->p2p_auto_join = !!auto_join;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003541 os_memcpy(wpa_s->pending_join_iface_addr, iface_addr, ETH_ALEN);
3542 os_memcpy(wpa_s->pending_join_dev_addr, dev_addr, ETH_ALEN);
3543 wpa_s->pending_join_wps_method = wps_method;
3544
3545 /* Make sure we are not running find during connection establishment */
3546 wpas_p2p_stop_find(wpa_s);
3547
3548 wpa_s->p2p_join_scan_count = 0;
3549 wpas_p2p_join_scan(wpa_s, NULL);
3550 return 0;
3551}
3552
3553
3554static int wpas_p2p_join_start(struct wpa_supplicant *wpa_s)
3555{
3556 struct wpa_supplicant *group;
3557 struct p2p_go_neg_results res;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003558 struct wpa_bss *bss;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003559
3560 group = wpas_p2p_get_group_iface(wpa_s, 0, 0);
3561 if (group == NULL)
3562 return -1;
3563 if (group != wpa_s) {
3564 os_memcpy(group->p2p_pin, wpa_s->p2p_pin,
3565 sizeof(group->p2p_pin));
3566 group->p2p_wps_method = wpa_s->p2p_wps_method;
3567 }
3568
3569 group->p2p_in_provisioning = 1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003570 group->p2p_fallback_to_go_neg = wpa_s->p2p_fallback_to_go_neg;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003571
3572 os_memset(&res, 0, sizeof(res));
3573 os_memcpy(res.peer_interface_addr, wpa_s->pending_join_iface_addr,
3574 ETH_ALEN);
3575 res.wps_method = wpa_s->pending_join_wps_method;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003576 bss = wpa_bss_get_bssid(wpa_s, wpa_s->pending_join_iface_addr);
3577 if (bss) {
3578 res.freq = bss->freq;
3579 res.ssid_len = bss->ssid_len;
3580 os_memcpy(res.ssid, bss->ssid, bss->ssid_len);
3581 }
3582
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08003583 if (wpa_s->off_channel_freq || wpa_s->roc_waiting_drv_freq) {
3584 wpa_printf(MSG_DEBUG, "P2P: Cancel remain-on-channel prior to "
3585 "starting client");
3586 wpa_drv_cancel_remain_on_channel(wpa_s);
3587 wpa_s->off_channel_freq = 0;
3588 wpa_s->roc_waiting_drv_freq = 0;
3589 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003590 wpas_start_wps_enrollee(group, &res);
3591
3592 /*
3593 * Allow a longer timeout for join-a-running-group than normal 15
3594 * second group formation timeout since the GO may not have authorized
3595 * our connection yet.
3596 */
3597 eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s, NULL);
3598 eloop_register_timeout(60, 0, wpas_p2p_group_formation_timeout,
3599 wpa_s, NULL);
3600
3601 return 0;
3602}
3603
3604
3605/**
3606 * wpas_p2p_connect - Request P2P Group Formation to be started
3607 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
3608 * @peer_addr: Address of the peer P2P Device
3609 * @pin: PIN to use during provisioning or %NULL to indicate PBC mode
3610 * @persistent_group: Whether to create a persistent group
Dmitry Shmidt04949592012-07-19 12:16:46 -07003611 * @auto_join: Whether to select join vs. GO Negotiation automatically
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003612 * @join: Whether to join an existing group (as a client) instead of starting
3613 * Group Owner negotiation; @peer_addr is BSSID in that case
3614 * @auth: Whether to only authorize the connection instead of doing that and
3615 * initiating Group Owner negotiation
3616 * @go_intent: GO Intent or -1 to use default
3617 * @freq: Frequency for the group or 0 for auto-selection
Dmitry Shmidt04949592012-07-19 12:16:46 -07003618 * @persistent_id: Persistent group credentials to use for forcing GO
3619 * parameters or -1 to generate new values (SSID/passphrase)
3620 * @pd: Whether to send Provision Discovery prior to GO Negotiation as an
3621 * interoperability workaround when initiating group formation
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003622 * @ht40: Start GO with 40 MHz channel width
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003623 * Returns: 0 or new PIN (if pin was %NULL) on success, -1 on unspecified
3624 * failure, -2 on failure due to channel not currently available,
3625 * -3 if forced channel is not supported
3626 */
3627int wpas_p2p_connect(struct wpa_supplicant *wpa_s, const u8 *peer_addr,
3628 const char *pin, enum p2p_wps_method wps_method,
Dmitry Shmidt04949592012-07-19 12:16:46 -07003629 int persistent_group, int auto_join, int join, int auth,
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003630 int go_intent, int freq, int persistent_id, int pd,
3631 int ht40)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003632{
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08003633 int force_freq = 0, pref_freq = 0, oper_freq = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003634 u8 bssid[ETH_ALEN];
3635 int ret = 0;
3636 enum wpa_driver_if_type iftype;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003637 const u8 *if_addr;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003638 struct wpa_ssid *ssid = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003639
3640 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
3641 return -1;
3642
Dmitry Shmidt04949592012-07-19 12:16:46 -07003643 if (persistent_id >= 0) {
3644 ssid = wpa_config_get_network(wpa_s->conf, persistent_id);
3645 if (ssid == NULL || ssid->disabled != 2 ||
3646 ssid->mode != WPAS_MODE_P2P_GO)
3647 return -1;
3648 }
3649
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003650 if (go_intent < 0)
3651 go_intent = wpa_s->conf->p2p_go_intent;
3652
3653 if (!auth)
3654 wpa_s->p2p_long_listen = 0;
3655
3656 wpa_s->p2p_wps_method = wps_method;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003657 wpa_s->p2p_persistent_group = !!persistent_group;
3658 wpa_s->p2p_persistent_id = persistent_id;
3659 wpa_s->p2p_go_intent = go_intent;
3660 wpa_s->p2p_connect_freq = freq;
3661 wpa_s->p2p_fallback_to_go_neg = 0;
3662 wpa_s->p2p_pd_before_go_neg = !!pd;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003663 wpa_s->p2p_go_ht40 = !!ht40;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003664
3665 if (pin)
3666 os_strlcpy(wpa_s->p2p_pin, pin, sizeof(wpa_s->p2p_pin));
3667 else if (wps_method == WPS_PIN_DISPLAY) {
3668 ret = wps_generate_pin();
3669 os_snprintf(wpa_s->p2p_pin, sizeof(wpa_s->p2p_pin), "%08d",
3670 ret);
3671 wpa_printf(MSG_DEBUG, "P2P: Randomly generated PIN: %s",
3672 wpa_s->p2p_pin);
3673 } else
3674 wpa_s->p2p_pin[0] = '\0';
3675
Dmitry Shmidt04949592012-07-19 12:16:46 -07003676 if (join || auto_join) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003677 u8 iface_addr[ETH_ALEN], dev_addr[ETH_ALEN];
3678 if (auth) {
3679 wpa_printf(MSG_DEBUG, "P2P: Authorize invitation to "
3680 "connect a running group from " MACSTR,
3681 MAC2STR(peer_addr));
3682 os_memcpy(wpa_s->p2p_auth_invite, peer_addr, ETH_ALEN);
3683 return ret;
3684 }
3685 os_memcpy(dev_addr, peer_addr, ETH_ALEN);
3686 if (p2p_get_interface_addr(wpa_s->global->p2p, peer_addr,
3687 iface_addr) < 0) {
3688 os_memcpy(iface_addr, peer_addr, ETH_ALEN);
3689 p2p_get_dev_addr(wpa_s->global->p2p, peer_addr,
3690 dev_addr);
3691 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07003692 if (auto_join) {
3693 os_get_time(&wpa_s->p2p_auto_started);
3694 wpa_printf(MSG_DEBUG, "P2P: Auto join started at "
3695 "%ld.%06ld",
3696 wpa_s->p2p_auto_started.sec,
3697 wpa_s->p2p_auto_started.usec);
3698 }
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08003699 wpa_s->user_initiated_pd = 1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003700 if (wpas_p2p_join(wpa_s, iface_addr, dev_addr, wps_method,
3701 auto_join) < 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003702 return -1;
3703 return ret;
3704 }
3705
3706 if (wpa_s->current_ssid && wpa_drv_get_bssid(wpa_s, bssid) == 0 &&
3707 wpa_s->assoc_freq)
3708 oper_freq = wpa_s->assoc_freq;
3709 else {
3710 oper_freq = wpa_drv_shared_freq(wpa_s);
3711 if (oper_freq < 0)
3712 oper_freq = 0;
3713 }
3714
3715 if (freq > 0) {
3716 if (!p2p_supported_freq(wpa_s->global->p2p, freq)) {
3717 wpa_printf(MSG_DEBUG, "P2P: The forced channel "
3718 "(%u MHz) is not supported for P2P uses",
3719 freq);
3720 return -3;
3721 }
3722
3723 if (oper_freq > 0 && freq != oper_freq &&
3724 !(wpa_s->drv_flags &
3725 WPA_DRIVER_FLAGS_MULTI_CHANNEL_CONCURRENT)) {
3726 wpa_printf(MSG_DEBUG, "P2P: Cannot start P2P group "
3727 "on %u MHz while connected on another "
3728 "channel (%u MHz)", freq, oper_freq);
3729 return -2;
3730 }
3731 wpa_printf(MSG_DEBUG, "P2P: Trying to force us to use the "
3732 "requested channel (%u MHz)", freq);
3733 force_freq = freq;
3734 } else if (oper_freq > 0 &&
3735 !p2p_supported_freq(wpa_s->global->p2p, oper_freq)) {
3736 if (!(wpa_s->drv_flags &
3737 WPA_DRIVER_FLAGS_MULTI_CHANNEL_CONCURRENT)) {
3738 wpa_printf(MSG_DEBUG, "P2P: Cannot start P2P group "
3739 "while connected on non-P2P supported "
3740 "channel (%u MHz)", oper_freq);
3741 return -2;
3742 }
3743 wpa_printf(MSG_DEBUG, "P2P: Current operating channel "
3744 "(%u MHz) not available for P2P - try to use "
3745 "another channel", oper_freq);
3746 force_freq = 0;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08003747 } else if (oper_freq > 0 &&
3748 (wpa_s->drv_flags &
3749 WPA_DRIVER_FLAGS_MULTI_CHANNEL_CONCURRENT)) {
3750 wpa_printf(MSG_DEBUG, "P2P: Trying to prefer the channel we "
3751 "are already using (%u MHz) on another interface",
3752 oper_freq);
3753 pref_freq = oper_freq;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003754 } else if (oper_freq > 0) {
3755 wpa_printf(MSG_DEBUG, "P2P: Trying to force us to use the "
3756 "channel we are already using (%u MHz) on another "
3757 "interface", oper_freq);
3758 force_freq = oper_freq;
3759 }
3760
3761 wpa_s->create_p2p_iface = wpas_p2p_create_iface(wpa_s);
3762
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003763 if (wpa_s->create_p2p_iface) {
3764 /* Prepare to add a new interface for the group */
3765 iftype = WPA_IF_P2P_GROUP;
3766 if (go_intent == 15)
3767 iftype = WPA_IF_P2P_GO;
3768 if (wpas_p2p_add_group_interface(wpa_s, iftype) < 0) {
3769 wpa_printf(MSG_ERROR, "P2P: Failed to allocate a new "
3770 "interface for the group");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003771 return -1;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003772 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003773
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003774 if_addr = wpa_s->pending_interface_addr;
3775 } else
3776 if_addr = wpa_s->own_addr;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003777
3778 if (auth) {
3779 if (wpas_p2p_auth_go_neg(wpa_s, peer_addr, wps_method,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003780 go_intent, if_addr,
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08003781 force_freq, persistent_group, ssid,
3782 pref_freq) < 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003783 return -1;
3784 return ret;
3785 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003786
3787 if (wpas_p2p_start_go_neg(wpa_s, peer_addr, wps_method,
3788 go_intent, if_addr, force_freq,
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08003789 persistent_group, ssid, pref_freq) < 0) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003790 if (wpa_s->create_p2p_iface)
3791 wpas_p2p_remove_pending_group_interface(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003792 return -1;
3793 }
3794 return ret;
3795}
3796
3797
3798/**
3799 * wpas_p2p_remain_on_channel_cb - Indication of remain-on-channel start
3800 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
3801 * @freq: Frequency of the channel in MHz
3802 * @duration: Duration of the stay on the channel in milliseconds
3803 *
3804 * This callback is called when the driver indicates that it has started the
3805 * requested remain-on-channel duration.
3806 */
3807void wpas_p2p_remain_on_channel_cb(struct wpa_supplicant *wpa_s,
3808 unsigned int freq, unsigned int duration)
3809{
3810 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
3811 return;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003812 if (wpa_s->off_channel_freq == wpa_s->pending_listen_freq) {
3813 p2p_listen_cb(wpa_s->global->p2p, wpa_s->pending_listen_freq,
3814 wpa_s->pending_listen_duration);
3815 wpa_s->pending_listen_freq = 0;
3816 }
3817}
3818
3819
3820static int wpas_p2p_listen_start(struct wpa_supplicant *wpa_s,
3821 unsigned int timeout)
3822{
3823 /* Limit maximum Listen state time based on driver limitation. */
3824 if (timeout > wpa_s->max_remain_on_chan)
3825 timeout = wpa_s->max_remain_on_chan;
3826
3827 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
3828 return wpa_drv_p2p_listen(wpa_s, timeout);
3829
3830 return p2p_listen(wpa_s->global->p2p, timeout);
3831}
3832
3833
3834/**
3835 * wpas_p2p_cancel_remain_on_channel_cb - Remain-on-channel timeout
3836 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
3837 * @freq: Frequency of the channel in MHz
3838 *
3839 * This callback is called when the driver indicates that a remain-on-channel
3840 * operation has been completed, i.e., the duration on the requested channel
3841 * has timed out.
3842 */
3843void wpas_p2p_cancel_remain_on_channel_cb(struct wpa_supplicant *wpa_s,
3844 unsigned int freq)
3845{
3846 wpa_printf(MSG_DEBUG, "P2P: Cancel remain-on-channel callback "
3847 "(p2p_long_listen=%d ms pending_action_tx=%p)",
3848 wpa_s->p2p_long_listen, wpa_s->pending_action_tx);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003849 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
3850 return;
3851 if (p2p_listen_end(wpa_s->global->p2p, freq) > 0)
3852 return; /* P2P module started a new operation */
3853 if (wpa_s->pending_action_tx)
3854 return;
3855 if (wpa_s->p2p_long_listen > 0)
3856 wpa_s->p2p_long_listen -= wpa_s->max_remain_on_chan;
3857 if (wpa_s->p2p_long_listen > 0) {
3858 wpa_printf(MSG_DEBUG, "P2P: Continuing long Listen state");
3859 wpas_p2p_listen_start(wpa_s, wpa_s->p2p_long_listen);
3860 }
3861}
3862
3863
3864/**
3865 * wpas_p2p_group_remove - Remove a P2P group
3866 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
3867 * @ifname: Network interface name of the group interface or "*" to remove all
3868 * groups
3869 * Returns: 0 on success, -1 on failure
3870 *
3871 * This function is used to remove a P2P group. This can be used to disconnect
3872 * from a group in which the local end is a P2P Client or to end a P2P Group in
3873 * case the local end is the Group Owner. If a virtual network interface was
3874 * created for this group, that interface will be removed. Otherwise, only the
3875 * configured P2P group network will be removed from the interface.
3876 */
3877int wpas_p2p_group_remove(struct wpa_supplicant *wpa_s, const char *ifname)
3878{
3879 struct wpa_global *global = wpa_s->global;
3880
3881 if (os_strcmp(ifname, "*") == 0) {
3882 struct wpa_supplicant *prev;
3883 wpa_s = global->ifaces;
3884 while (wpa_s) {
3885 prev = wpa_s;
3886 wpa_s = wpa_s->next;
3887 wpas_p2p_disconnect(prev);
3888 }
3889 return 0;
3890 }
3891
3892 for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
3893 if (os_strcmp(wpa_s->ifname, ifname) == 0)
3894 break;
3895 }
3896
3897 return wpas_p2p_disconnect(wpa_s);
3898}
3899
3900
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003901static int wpas_p2p_init_go_params(struct wpa_supplicant *wpa_s,
3902 struct p2p_go_neg_results *params,
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003903 int freq, int ht40)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003904{
3905 u8 bssid[ETH_ALEN];
3906 int res;
3907
3908 os_memset(params, 0, sizeof(*params));
3909 params->role_go = 1;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003910 params->ht40 = ht40;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003911 if (freq) {
3912 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on forced "
3913 "frequency %d MHz", freq);
3914 params->freq = freq;
3915 } else if (wpa_s->conf->p2p_oper_reg_class == 81 &&
3916 wpa_s->conf->p2p_oper_channel >= 1 &&
3917 wpa_s->conf->p2p_oper_channel <= 11) {
3918 params->freq = 2407 + 5 * wpa_s->conf->p2p_oper_channel;
3919 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on configured "
3920 "frequency %d MHz", params->freq);
3921 } else if (wpa_s->conf->p2p_oper_reg_class == 115 ||
Jouni Malinen87fd2792011-05-16 18:35:42 +03003922 wpa_s->conf->p2p_oper_reg_class == 124) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003923 params->freq = 5000 + 5 * wpa_s->conf->p2p_oper_channel;
3924 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on configured "
3925 "frequency %d MHz", params->freq);
3926 } else if (wpa_s->conf->p2p_oper_channel == 0 &&
3927 wpa_s->best_overall_freq > 0 &&
3928 p2p_supported_freq(wpa_s->global->p2p,
3929 wpa_s->best_overall_freq)) {
3930 params->freq = wpa_s->best_overall_freq;
3931 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on best overall "
3932 "channel %d MHz", params->freq);
3933 } else if (wpa_s->conf->p2p_oper_channel == 0 &&
3934 wpa_s->best_24_freq > 0 &&
3935 p2p_supported_freq(wpa_s->global->p2p,
3936 wpa_s->best_24_freq)) {
3937 params->freq = wpa_s->best_24_freq;
3938 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on best 2.4 GHz "
3939 "channel %d MHz", params->freq);
3940 } else if (wpa_s->conf->p2p_oper_channel == 0 &&
3941 wpa_s->best_5_freq > 0 &&
3942 p2p_supported_freq(wpa_s->global->p2p,
3943 wpa_s->best_5_freq)) {
3944 params->freq = wpa_s->best_5_freq;
3945 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on best 5 GHz "
3946 "channel %d MHz", params->freq);
3947 } else {
Dmitry Shmidt04949592012-07-19 12:16:46 -07003948 int chan;
3949 for (chan = 0; chan < 11; chan++) {
3950 params->freq = 2412 + chan * 5;
3951 if (!wpas_p2p_disallowed_freq(wpa_s->global,
3952 params->freq))
3953 break;
3954 }
3955 if (chan == 11) {
3956 wpa_printf(MSG_DEBUG, "P2P: No 2.4 GHz channel "
3957 "allowed");
3958 return -1;
3959 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003960 wpa_printf(MSG_DEBUG, "P2P: Set GO freq %d MHz (no preference "
3961 "known)", params->freq);
3962 }
3963
3964 if (wpa_s->current_ssid && wpa_drv_get_bssid(wpa_s, bssid) == 0 &&
3965 wpa_s->assoc_freq && !freq) {
3966 wpa_printf(MSG_DEBUG, "P2P: Force GO on the channel we are "
3967 "already using");
3968 params->freq = wpa_s->assoc_freq;
3969 }
3970
3971 res = wpa_drv_shared_freq(wpa_s);
3972 if (res > 0 && !freq) {
3973 wpa_printf(MSG_DEBUG, "P2P: Force GO on the channel we are "
3974 "already using on a shared interface");
3975 params->freq = res;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003976 } else if (res > 0 && freq != res &&
3977 !(wpa_s->drv_flags &
3978 WPA_DRIVER_FLAGS_MULTI_CHANNEL_CONCURRENT)) {
3979 wpa_printf(MSG_DEBUG, "P2P: Cannot start P2P group on %u MHz "
3980 "while connected on another channel (%u MHz)",
3981 freq, res);
3982 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003983 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003984
3985 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003986}
3987
3988
3989static struct wpa_supplicant *
3990wpas_p2p_get_group_iface(struct wpa_supplicant *wpa_s, int addr_allocated,
3991 int go)
3992{
3993 struct wpa_supplicant *group_wpa_s;
3994
Dmitry Shmidtaa532512012-09-24 10:35:31 -07003995 if (!wpas_p2p_create_iface(wpa_s)) {
3996 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Use same interface for group "
3997 "operations");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003998 return wpa_s;
Dmitry Shmidtaa532512012-09-24 10:35:31 -07003999 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004000
4001 if (wpas_p2p_add_group_interface(wpa_s, go ? WPA_IF_P2P_GO :
Dmitry Shmidtaa532512012-09-24 10:35:31 -07004002 WPA_IF_P2P_CLIENT) < 0) {
4003 wpa_msg(wpa_s, MSG_ERROR, "P2P: Failed to add group interface");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004004 return NULL;
Dmitry Shmidtaa532512012-09-24 10:35:31 -07004005 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004006 group_wpa_s = wpas_p2p_init_group_interface(wpa_s, go);
4007 if (group_wpa_s == NULL) {
Dmitry Shmidtaa532512012-09-24 10:35:31 -07004008 wpa_msg(wpa_s, MSG_ERROR, "P2P: Failed to initialize group "
4009 "interface");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004010 wpas_p2p_remove_pending_group_interface(wpa_s);
4011 return NULL;
4012 }
4013
Dmitry Shmidtaa532512012-09-24 10:35:31 -07004014 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Use separate group interface %s",
4015 group_wpa_s->ifname);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004016 return group_wpa_s;
4017}
4018
4019
4020/**
4021 * wpas_p2p_group_add - Add a new P2P group with local end as Group Owner
4022 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
4023 * @persistent_group: Whether to create a persistent group
4024 * @freq: Frequency for the group or 0 to indicate no hardcoding
4025 * Returns: 0 on success, -1 on failure
4026 *
4027 * This function creates a new P2P group with the local end as the Group Owner,
4028 * i.e., without using Group Owner Negotiation.
4029 */
4030int wpas_p2p_group_add(struct wpa_supplicant *wpa_s, int persistent_group,
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004031 int freq, int ht40)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004032{
4033 struct p2p_go_neg_results params;
4034 unsigned int r;
4035
4036 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
4037 return -1;
4038
Dmitry Shmidtdca39792011-09-06 11:17:33 -07004039 /* Make sure we are not running find during connection establishment */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004040 wpa_printf(MSG_DEBUG, "P2P: Stop any on-going P2P FIND");
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08004041 wpas_p2p_stop_find_oper(wpa_s);
Dmitry Shmidtdca39792011-09-06 11:17:33 -07004042
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004043 if (freq == 2) {
4044 wpa_printf(MSG_DEBUG, "P2P: Request to start GO on 2.4 GHz "
4045 "band");
4046 if (wpa_s->best_24_freq > 0 &&
4047 p2p_supported_freq(wpa_s->global->p2p,
4048 wpa_s->best_24_freq)) {
4049 freq = wpa_s->best_24_freq;
4050 wpa_printf(MSG_DEBUG, "P2P: Use best 2.4 GHz band "
4051 "channel: %d MHz", freq);
4052 } else {
4053 os_get_random((u8 *) &r, sizeof(r));
4054 freq = 2412 + (r % 3) * 25;
4055 wpa_printf(MSG_DEBUG, "P2P: Use random 2.4 GHz band "
4056 "channel: %d MHz", freq);
4057 }
4058 }
4059
4060 if (freq == 5) {
4061 wpa_printf(MSG_DEBUG, "P2P: Request to start GO on 5 GHz "
4062 "band");
4063 if (wpa_s->best_5_freq > 0 &&
4064 p2p_supported_freq(wpa_s->global->p2p,
4065 wpa_s->best_5_freq)) {
4066 freq = wpa_s->best_5_freq;
4067 wpa_printf(MSG_DEBUG, "P2P: Use best 5 GHz band "
4068 "channel: %d MHz", freq);
4069 } else {
4070 os_get_random((u8 *) &r, sizeof(r));
4071 freq = 5180 + (r % 4) * 20;
4072 if (!p2p_supported_freq(wpa_s->global->p2p, freq)) {
4073 wpa_printf(MSG_DEBUG, "P2P: Could not select "
4074 "5 GHz channel for P2P group");
4075 return -1;
4076 }
4077 wpa_printf(MSG_DEBUG, "P2P: Use random 5 GHz band "
4078 "channel: %d MHz", freq);
4079 }
4080 }
4081
4082 if (freq > 0 && !p2p_supported_freq(wpa_s->global->p2p, freq)) {
4083 wpa_printf(MSG_DEBUG, "P2P: The forced channel for GO "
4084 "(%u MHz) is not supported for P2P uses",
4085 freq);
4086 return -1;
4087 }
4088
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004089 if (wpas_p2p_init_go_params(wpa_s, &params, freq, ht40))
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004090 return -1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07004091 if (params.freq &&
4092 !p2p_supported_freq(wpa_s->global->p2p, params.freq)) {
4093 wpa_printf(MSG_DEBUG, "P2P: The selected channel for GO "
4094 "(%u MHz) is not supported for P2P uses",
4095 params.freq);
4096 return -1;
4097 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004098 p2p_go_params(wpa_s->global->p2p, &params);
4099 params.persistent_group = persistent_group;
4100
4101 wpa_s = wpas_p2p_get_group_iface(wpa_s, 0, 1);
4102 if (wpa_s == NULL)
4103 return -1;
4104 wpas_start_wps_go(wpa_s, &params, 0);
4105
4106 return 0;
4107}
4108
4109
4110static int wpas_start_p2p_client(struct wpa_supplicant *wpa_s,
4111 struct wpa_ssid *params, int addr_allocated)
4112{
4113 struct wpa_ssid *ssid;
4114
4115 wpa_s = wpas_p2p_get_group_iface(wpa_s, addr_allocated, 0);
4116 if (wpa_s == NULL)
4117 return -1;
4118
4119 wpa_supplicant_ap_deinit(wpa_s);
4120
4121 ssid = wpa_config_add_network(wpa_s->conf);
4122 if (ssid == NULL)
4123 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004124 wpa_config_set_network_defaults(ssid);
4125 ssid->temporary = 1;
4126 ssid->proto = WPA_PROTO_RSN;
4127 ssid->pairwise_cipher = WPA_CIPHER_CCMP;
4128 ssid->group_cipher = WPA_CIPHER_CCMP;
4129 ssid->key_mgmt = WPA_KEY_MGMT_PSK;
4130 ssid->ssid = os_malloc(params->ssid_len);
4131 if (ssid->ssid == NULL) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004132 wpa_config_remove_network(wpa_s->conf, ssid->id);
4133 return -1;
4134 }
4135 os_memcpy(ssid->ssid, params->ssid, params->ssid_len);
4136 ssid->ssid_len = params->ssid_len;
4137 ssid->p2p_group = 1;
4138 ssid->export_keys = 1;
4139 if (params->psk_set) {
4140 os_memcpy(ssid->psk, params->psk, 32);
4141 ssid->psk_set = 1;
4142 }
4143 if (params->passphrase)
4144 ssid->passphrase = os_strdup(params->passphrase);
4145
4146 wpa_supplicant_select_network(wpa_s, ssid);
4147
4148 wpa_s->show_group_started = 1;
4149
4150 return 0;
4151}
4152
4153
4154int wpas_p2p_group_add_persistent(struct wpa_supplicant *wpa_s,
4155 struct wpa_ssid *ssid, int addr_allocated,
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004156 int freq, int ht40)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004157{
4158 struct p2p_go_neg_results params;
4159 int go = 0;
4160
4161 if (ssid->disabled != 2 || ssid->ssid == NULL)
4162 return -1;
4163
4164 if (wpas_get_p2p_group(wpa_s, ssid->ssid, ssid->ssid_len, &go) &&
4165 go == (ssid->mode == WPAS_MODE_P2P_GO)) {
4166 wpa_printf(MSG_DEBUG, "P2P: Requested persistent group is "
4167 "already running");
4168 return 0;
4169 }
4170
4171 /* Make sure we are not running find during connection establishment */
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08004172 wpas_p2p_stop_find_oper(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004173
Dmitry Shmidt04949592012-07-19 12:16:46 -07004174 wpa_s->p2p_fallback_to_go_neg = 0;
4175
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004176 if (ssid->mode == WPAS_MODE_INFRA)
4177 return wpas_start_p2p_client(wpa_s, ssid, addr_allocated);
4178
4179 if (ssid->mode != WPAS_MODE_P2P_GO)
4180 return -1;
4181
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004182 if (wpas_p2p_init_go_params(wpa_s, &params, freq, ht40))
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004183 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004184
4185 params.role_go = 1;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08004186 params.psk_set = ssid->psk_set;
4187 if (params.psk_set)
4188 os_memcpy(params.psk, ssid->psk, sizeof(params.psk));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004189 if (ssid->passphrase == NULL ||
4190 os_strlen(ssid->passphrase) >= sizeof(params.passphrase)) {
4191 wpa_printf(MSG_DEBUG, "P2P: Invalid passphrase in persistent "
4192 "group");
4193 return -1;
4194 }
4195 os_strlcpy(params.passphrase, ssid->passphrase,
4196 sizeof(params.passphrase));
4197 os_memcpy(params.ssid, ssid->ssid, ssid->ssid_len);
4198 params.ssid_len = ssid->ssid_len;
4199 params.persistent_group = 1;
4200
4201 wpa_s = wpas_p2p_get_group_iface(wpa_s, addr_allocated, 1);
4202 if (wpa_s == NULL)
4203 return -1;
4204
4205 wpas_start_wps_go(wpa_s, &params, 0);
4206
4207 return 0;
4208}
4209
4210
4211static void wpas_p2p_ie_update(void *ctx, struct wpabuf *beacon_ies,
4212 struct wpabuf *proberesp_ies)
4213{
4214 struct wpa_supplicant *wpa_s = ctx;
4215 if (wpa_s->ap_iface) {
4216 struct hostapd_data *hapd = wpa_s->ap_iface->bss[0];
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004217 if (!(hapd->conf->p2p & P2P_GROUP_OWNER)) {
4218 wpabuf_free(beacon_ies);
4219 wpabuf_free(proberesp_ies);
4220 return;
4221 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004222 if (beacon_ies) {
4223 wpabuf_free(hapd->p2p_beacon_ie);
4224 hapd->p2p_beacon_ie = beacon_ies;
4225 }
4226 wpabuf_free(hapd->p2p_probe_resp_ie);
4227 hapd->p2p_probe_resp_ie = proberesp_ies;
4228 } else {
4229 wpabuf_free(beacon_ies);
4230 wpabuf_free(proberesp_ies);
4231 }
4232 wpa_supplicant_ap_update_beacon(wpa_s);
4233}
4234
4235
4236static void wpas_p2p_idle_update(void *ctx, int idle)
4237{
4238 struct wpa_supplicant *wpa_s = ctx;
4239 if (!wpa_s->ap_iface)
4240 return;
4241 wpa_printf(MSG_DEBUG, "P2P: GO - group %sidle", idle ? "" : "not ");
Dmitry Shmidt04949592012-07-19 12:16:46 -07004242 if (idle)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004243 wpas_p2p_set_group_idle_timeout(wpa_s);
4244 else
4245 eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL);
4246}
4247
4248
4249struct p2p_group * wpas_p2p_group_init(struct wpa_supplicant *wpa_s,
Dmitry Shmidt04949592012-07-19 12:16:46 -07004250 struct wpa_ssid *ssid)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004251{
4252 struct p2p_group *group;
4253 struct p2p_group_config *cfg;
4254
4255 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
4256 return NULL;
4257 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
4258 return NULL;
4259
4260 cfg = os_zalloc(sizeof(*cfg));
4261 if (cfg == NULL)
4262 return NULL;
4263
Dmitry Shmidt04949592012-07-19 12:16:46 -07004264 if (ssid->p2p_persistent_group && wpa_s->conf->persistent_reconnect)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004265 cfg->persistent_group = 2;
Dmitry Shmidt04949592012-07-19 12:16:46 -07004266 else if (ssid->p2p_persistent_group)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004267 cfg->persistent_group = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004268 os_memcpy(cfg->interface_addr, wpa_s->own_addr, ETH_ALEN);
4269 if (wpa_s->max_stations &&
4270 wpa_s->max_stations < wpa_s->conf->max_num_sta)
4271 cfg->max_clients = wpa_s->max_stations;
4272 else
4273 cfg->max_clients = wpa_s->conf->max_num_sta;
Dmitry Shmidt04949592012-07-19 12:16:46 -07004274 os_memcpy(cfg->ssid, ssid->ssid, ssid->ssid_len);
4275 cfg->ssid_len = ssid->ssid_len;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004276 cfg->cb_ctx = wpa_s;
4277 cfg->ie_update = wpas_p2p_ie_update;
4278 cfg->idle_update = wpas_p2p_idle_update;
4279
4280 group = p2p_group_init(wpa_s->global->p2p, cfg);
4281 if (group == NULL)
4282 os_free(cfg);
Dmitry Shmidt04949592012-07-19 12:16:46 -07004283 if (ssid->mode != WPAS_MODE_P2P_GROUP_FORMATION)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004284 p2p_group_notif_formation_done(group);
4285 wpa_s->p2p_group = group;
4286 return group;
4287}
4288
4289
4290void wpas_p2p_wps_success(struct wpa_supplicant *wpa_s, const u8 *peer_addr,
4291 int registrar)
4292{
Dmitry Shmidt04949592012-07-19 12:16:46 -07004293 struct wpa_ssid *ssid = wpa_s->current_ssid;
4294
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004295 if (!wpa_s->p2p_in_provisioning) {
4296 wpa_printf(MSG_DEBUG, "P2P: Ignore WPS success event - P2P "
4297 "provisioning not in progress");
4298 return;
4299 }
4300
Dmitry Shmidt04949592012-07-19 12:16:46 -07004301 if (ssid && ssid->mode == WPAS_MODE_INFRA) {
4302 u8 go_dev_addr[ETH_ALEN];
4303 os_memcpy(go_dev_addr, wpa_s->bssid, ETH_ALEN);
4304 wpas_p2p_persistent_group(wpa_s, go_dev_addr, ssid->ssid,
4305 ssid->ssid_len);
4306 /* Clear any stored provisioning info */
4307 p2p_clear_provisioning_info(wpa_s->global->p2p, go_dev_addr);
4308 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004309
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004310 eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s->parent,
4311 NULL);
Dmitry Shmidt04949592012-07-19 12:16:46 -07004312 if (ssid && ssid->mode == WPAS_MODE_INFRA) {
4313 /*
4314 * Use a separate timeout for initial data connection to
4315 * complete to allow the group to be removed automatically if
4316 * something goes wrong in this step before the P2P group idle
4317 * timeout mechanism is taken into use.
4318 */
4319 eloop_register_timeout(P2P_MAX_INITIAL_CONN_WAIT, 0,
4320 wpas_p2p_group_formation_timeout,
4321 wpa_s->parent, NULL);
4322 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004323 if (wpa_s->global->p2p)
4324 p2p_wps_success_cb(wpa_s->global->p2p, peer_addr);
4325 else if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
4326 wpa_drv_wps_success_cb(wpa_s, peer_addr);
4327 wpas_group_formation_completed(wpa_s, 1);
4328}
4329
4330
Jouni Malinen75ecf522011-06-27 15:19:46 -07004331void wpas_p2p_wps_failed(struct wpa_supplicant *wpa_s,
4332 struct wps_event_fail *fail)
4333{
4334 if (!wpa_s->p2p_in_provisioning) {
4335 wpa_printf(MSG_DEBUG, "P2P: Ignore WPS fail event - P2P "
4336 "provisioning not in progress");
4337 return;
4338 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004339
4340 if (wpa_s->go_params) {
4341 p2p_clear_provisioning_info(
4342 wpa_s->global->p2p,
Dmitry Shmidt04949592012-07-19 12:16:46 -07004343 wpa_s->go_params->peer_device_addr);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004344 }
4345
Jouni Malinen75ecf522011-06-27 15:19:46 -07004346 wpas_notify_p2p_wps_failed(wpa_s, fail);
4347}
4348
4349
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004350int wpas_p2p_prov_disc(struct wpa_supplicant *wpa_s, const u8 *peer_addr,
Dmitry Shmidt04949592012-07-19 12:16:46 -07004351 const char *config_method,
4352 enum wpas_p2p_prov_disc_use use)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004353{
4354 u16 config_methods;
4355
Dmitry Shmidt04949592012-07-19 12:16:46 -07004356 wpa_s->p2p_fallback_to_go_neg = 0;
4357 wpa_s->pending_pd_use = NORMAL_PD;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004358 if (os_strncmp(config_method, "display", 7) == 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004359 config_methods = WPS_CONFIG_DISPLAY;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004360 else if (os_strncmp(config_method, "keypad", 6) == 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004361 config_methods = WPS_CONFIG_KEYPAD;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004362 else if (os_strncmp(config_method, "pbc", 3) == 0 ||
4363 os_strncmp(config_method, "pushbutton", 10) == 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004364 config_methods = WPS_CONFIG_PUSHBUTTON;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004365 else {
4366 wpa_printf(MSG_DEBUG, "P2P: Unknown config method");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004367 return -1;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004368 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004369
Dmitry Shmidt04949592012-07-19 12:16:46 -07004370 if (use == WPAS_P2P_PD_AUTO) {
4371 os_memcpy(wpa_s->pending_join_dev_addr, peer_addr, ETH_ALEN);
4372 wpa_s->pending_pd_config_methods = config_methods;
4373 wpa_s->p2p_auto_pd = 1;
4374 wpa_s->p2p_auto_join = 0;
4375 wpa_s->pending_pd_before_join = 0;
4376 wpa_s->auto_pd_scan_retry = 0;
4377 wpas_p2p_stop_find(wpa_s);
4378 wpa_s->p2p_join_scan_count = 0;
4379 os_get_time(&wpa_s->p2p_auto_started);
4380 wpa_printf(MSG_DEBUG, "P2P: Auto PD started at %ld.%06ld",
4381 wpa_s->p2p_auto_started.sec,
4382 wpa_s->p2p_auto_started.usec);
4383 wpas_p2p_join_scan(wpa_s, NULL);
4384 return 0;
4385 }
4386
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004387 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT) {
4388 return wpa_drv_p2p_prov_disc_req(wpa_s, peer_addr,
Dmitry Shmidt04949592012-07-19 12:16:46 -07004389 config_methods,
4390 use == WPAS_P2P_PD_FOR_JOIN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004391 }
4392
4393 if (wpa_s->global->p2p == NULL || wpa_s->global->p2p_disabled)
4394 return -1;
4395
4396 return p2p_prov_disc_req(wpa_s->global->p2p, peer_addr,
Dmitry Shmidt04949592012-07-19 12:16:46 -07004397 config_methods, use == WPAS_P2P_PD_FOR_JOIN,
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08004398 0, 1);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004399}
4400
4401
4402int wpas_p2p_scan_result_text(const u8 *ies, size_t ies_len, char *buf,
4403 char *end)
4404{
4405 return p2p_scan_result_text(ies, ies_len, buf, end);
4406}
4407
4408
4409static void wpas_p2p_clear_pending_action_tx(struct wpa_supplicant *wpa_s)
4410{
4411 if (!wpa_s->pending_action_tx)
4412 return;
4413
4414 wpa_printf(MSG_DEBUG, "P2P: Drop pending Action TX due to new "
4415 "operation request");
4416 wpabuf_free(wpa_s->pending_action_tx);
4417 wpa_s->pending_action_tx = NULL;
4418}
4419
4420
4421int wpas_p2p_find(struct wpa_supplicant *wpa_s, unsigned int timeout,
4422 enum p2p_discovery_type type,
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08004423 unsigned int num_req_dev_types, const u8 *req_dev_types,
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004424 const u8 *dev_id, unsigned int search_delay)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004425{
4426 wpas_p2p_clear_pending_action_tx(wpa_s);
4427 wpa_s->p2p_long_listen = 0;
4428
4429 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
4430 return wpa_drv_p2p_find(wpa_s, timeout, type);
4431
Dmitry Shmidt04949592012-07-19 12:16:46 -07004432 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL ||
4433 wpa_s->p2p_in_provisioning)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004434 return -1;
4435
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08004436 wpa_supplicant_cancel_sched_scan(wpa_s);
4437
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004438 return p2p_find(wpa_s->global->p2p, timeout, type,
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004439 num_req_dev_types, req_dev_types, dev_id,
4440 search_delay);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004441}
4442
4443
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08004444static int wpas_p2p_stop_find_oper(struct wpa_supplicant *wpa_s)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004445{
4446 wpas_p2p_clear_pending_action_tx(wpa_s);
4447 wpa_s->p2p_long_listen = 0;
4448 eloop_cancel_timeout(wpas_p2p_long_listen_timeout, wpa_s, NULL);
4449 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
Jouni Malinendc7b7132012-09-14 12:53:47 -07004450 wpa_s->global->p2p_cb_on_scan_complete = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004451
4452 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT) {
4453 wpa_drv_p2p_stop_find(wpa_s);
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08004454 return 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004455 }
4456
4457 if (wpa_s->global->p2p)
4458 p2p_stop_find(wpa_s->global->p2p);
4459
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08004460 return 0;
4461}
4462
4463
4464void wpas_p2p_stop_find(struct wpa_supplicant *wpa_s)
4465{
4466 if (wpas_p2p_stop_find_oper(wpa_s) > 0)
4467 return;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004468 wpas_p2p_remove_pending_group_interface(wpa_s);
4469}
4470
4471
4472static void wpas_p2p_long_listen_timeout(void *eloop_ctx, void *timeout_ctx)
4473{
4474 struct wpa_supplicant *wpa_s = eloop_ctx;
4475 wpa_s->p2p_long_listen = 0;
4476}
4477
4478
4479int wpas_p2p_listen(struct wpa_supplicant *wpa_s, unsigned int timeout)
4480{
4481 int res;
4482
4483 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
4484 return -1;
4485
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08004486 wpa_supplicant_cancel_sched_scan(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004487 wpas_p2p_clear_pending_action_tx(wpa_s);
4488
4489 if (timeout == 0) {
4490 /*
4491 * This is a request for unlimited Listen state. However, at
4492 * least for now, this is mapped to a Listen state for one
4493 * hour.
4494 */
4495 timeout = 3600;
4496 }
4497 eloop_cancel_timeout(wpas_p2p_long_listen_timeout, wpa_s, NULL);
4498 wpa_s->p2p_long_listen = 0;
4499
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004500 /*
4501 * Stop previous find/listen operation to avoid trying to request a new
4502 * remain-on-channel operation while the driver is still running the
4503 * previous one.
4504 */
4505 if (wpa_s->global->p2p)
4506 p2p_stop_find(wpa_s->global->p2p);
4507
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004508 res = wpas_p2p_listen_start(wpa_s, timeout * 1000);
4509 if (res == 0 && timeout * 1000 > wpa_s->max_remain_on_chan) {
4510 wpa_s->p2p_long_listen = timeout * 1000;
4511 eloop_register_timeout(timeout, 0,
4512 wpas_p2p_long_listen_timeout,
4513 wpa_s, NULL);
4514 }
4515
4516 return res;
4517}
4518
4519
4520int wpas_p2p_assoc_req_ie(struct wpa_supplicant *wpa_s, struct wpa_bss *bss,
4521 u8 *buf, size_t len, int p2p_group)
4522{
4523 struct wpabuf *p2p_ie;
4524 int ret;
4525
4526 if (wpa_s->global->p2p_disabled)
4527 return -1;
4528 if (wpa_s->global->p2p == NULL)
4529 return -1;
4530 if (bss == NULL)
4531 return -1;
4532
4533 p2p_ie = wpa_bss_get_vendor_ie_multi(bss, P2P_IE_VENDOR_TYPE);
4534 ret = p2p_assoc_req_ie(wpa_s->global->p2p, bss->bssid, buf, len,
4535 p2p_group, p2p_ie);
4536 wpabuf_free(p2p_ie);
4537
4538 return ret;
4539}
4540
4541
4542int wpas_p2p_probe_req_rx(struct wpa_supplicant *wpa_s, const u8 *addr,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004543 const u8 *dst, const u8 *bssid,
Dmitry Shmidt04949592012-07-19 12:16:46 -07004544 const u8 *ie, size_t ie_len, int ssi_signal)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004545{
4546 if (wpa_s->global->p2p_disabled)
4547 return 0;
4548 if (wpa_s->global->p2p == NULL)
4549 return 0;
4550
Dmitry Shmidt04949592012-07-19 12:16:46 -07004551 switch (p2p_probe_req_rx(wpa_s->global->p2p, addr, dst, bssid,
4552 ie, ie_len)) {
4553 case P2P_PREQ_NOT_P2P:
4554 wpas_notify_preq(wpa_s, addr, dst, bssid, ie, ie_len,
4555 ssi_signal);
4556 /* fall through */
4557 case P2P_PREQ_MALFORMED:
4558 case P2P_PREQ_NOT_LISTEN:
4559 case P2P_PREQ_NOT_PROCESSED:
4560 default: /* make gcc happy */
4561 return 0;
4562 case P2P_PREQ_PROCESSED:
4563 return 1;
4564 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004565}
4566
4567
4568void wpas_p2p_rx_action(struct wpa_supplicant *wpa_s, const u8 *da,
4569 const u8 *sa, const u8 *bssid,
4570 u8 category, const u8 *data, size_t len, int freq)
4571{
4572 if (wpa_s->global->p2p_disabled)
4573 return;
4574 if (wpa_s->global->p2p == NULL)
4575 return;
4576
4577 p2p_rx_action(wpa_s->global->p2p, da, sa, bssid, category, data, len,
4578 freq);
4579}
4580
4581
4582void wpas_p2p_scan_ie(struct wpa_supplicant *wpa_s, struct wpabuf *ies)
4583{
4584 if (wpa_s->global->p2p_disabled)
4585 return;
4586 if (wpa_s->global->p2p == NULL)
4587 return;
4588
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08004589 p2p_scan_ie(wpa_s->global->p2p, ies, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004590}
4591
4592
4593void wpas_p2p_group_deinit(struct wpa_supplicant *wpa_s)
4594{
4595 p2p_group_deinit(wpa_s->p2p_group);
4596 wpa_s->p2p_group = NULL;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004597
4598 wpa_s->ap_configured_cb = NULL;
4599 wpa_s->ap_configured_cb_ctx = NULL;
4600 wpa_s->ap_configured_cb_data = NULL;
4601 wpa_s->connect_without_scan = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004602}
4603
4604
4605int wpas_p2p_reject(struct wpa_supplicant *wpa_s, const u8 *addr)
4606{
4607 wpa_s->p2p_long_listen = 0;
4608
4609 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
4610 return wpa_drv_p2p_reject(wpa_s, addr);
4611
4612 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
4613 return -1;
4614
4615 return p2p_reject(wpa_s->global->p2p, addr);
4616}
4617
4618
4619/* Invite to reinvoke a persistent group */
4620int wpas_p2p_invite(struct wpa_supplicant *wpa_s, const u8 *peer_addr,
Jouni Malinen31be0a42012-08-31 21:20:51 +03004621 struct wpa_ssid *ssid, const u8 *go_dev_addr, int freq,
4622 int ht40)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004623{
4624 enum p2p_invite_role role;
4625 u8 *bssid = NULL;
Irfan Sherifff0619662012-09-23 19:11:37 -07004626#ifdef ANDROID_P2P
Irfan Sheriffaf84a572012-09-22 16:59:30 -07004627 int force_freq = 0, oper_freq = 0;
Irfan Sherifff0619662012-09-23 19:11:37 -07004628#endif
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004629
Jouni Malinen31be0a42012-08-31 21:20:51 +03004630 wpa_s->p2p_persistent_go_freq = freq;
4631 wpa_s->p2p_go_ht40 = !!ht40;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004632 if (ssid->mode == WPAS_MODE_P2P_GO) {
4633 role = P2P_INVITE_ROLE_GO;
4634 if (peer_addr == NULL) {
4635 wpa_printf(MSG_DEBUG, "P2P: Missing peer "
4636 "address in invitation command");
4637 return -1;
4638 }
4639 if (wpas_p2p_create_iface(wpa_s)) {
4640 if (wpas_p2p_add_group_interface(wpa_s,
4641 WPA_IF_P2P_GO) < 0) {
4642 wpa_printf(MSG_ERROR, "P2P: Failed to "
4643 "allocate a new interface for the "
4644 "group");
4645 return -1;
4646 }
4647 bssid = wpa_s->pending_interface_addr;
4648 } else
4649 bssid = wpa_s->own_addr;
4650 } else {
4651 role = P2P_INVITE_ROLE_CLIENT;
4652 peer_addr = ssid->bssid;
4653 }
4654 wpa_s->pending_invite_ssid_id = ssid->id;
4655
Irfan Sherifff0619662012-09-23 19:11:37 -07004656#ifdef ANDROID_P2P
Irfan Sheriffaf84a572012-09-22 16:59:30 -07004657 if (wpa_s->current_ssid && wpa_drv_get_bssid(wpa_s, bssid) == 0 &&
4658 wpa_s->assoc_freq)
4659 oper_freq = wpa_s->assoc_freq;
4660 else {
4661 oper_freq = wpa_drv_shared_freq(wpa_s);
4662 if (oper_freq < 0)
4663 oper_freq = 0;
4664 }
4665
4666 if (freq > 0) {
4667 if (!p2p_supported_freq(wpa_s->global->p2p, freq)) {
4668 wpa_printf(MSG_DEBUG, "P2P: The forced channel "
4669 "(%u MHz) is not supported for P2P uses",
4670 freq);
4671 return -3;
4672 }
4673
4674 if (oper_freq > 0 && freq != oper_freq &&
4675 !(wpa_s->drv_flags &
4676 WPA_DRIVER_FLAGS_MULTI_CHANNEL_CONCURRENT)) {
4677 wpa_printf(MSG_DEBUG, "P2P: Cannot start P2P group "
4678 "on %u MHz while connected on another "
4679 "channel (%u MHz)", freq, oper_freq);
4680 return -2;
4681 }
4682 wpa_printf(MSG_DEBUG, "P2P: Trying to force us to use the "
4683 "requested channel (%u MHz)", freq);
4684 force_freq = freq;
4685 } else if (oper_freq > 0 &&
4686 !p2p_supported_freq(wpa_s->global->p2p, oper_freq)) {
4687 if (!(wpa_s->drv_flags &
4688 WPA_DRIVER_FLAGS_MULTI_CHANNEL_CONCURRENT)) {
4689 wpa_printf(MSG_DEBUG, "P2P: Cannot start P2P group "
4690 "while connected on non-P2P supported "
4691 "channel (%u MHz)", oper_freq);
4692 return -2;
4693 }
4694 wpa_printf(MSG_DEBUG, "P2P: Current operating channel "
4695 "(%u MHz) not available for P2P - try to use "
4696 "another channel", oper_freq);
4697 force_freq = 0;
4698 } else if (oper_freq > 0) {
4699 wpa_printf(MSG_DEBUG, "P2P: Trying to force us to use the "
4700 "channel we are already using (%u MHz) on another "
4701 "interface", oper_freq);
4702 force_freq = oper_freq;
4703 }
Irfan Sherifff0619662012-09-23 19:11:37 -07004704#endif
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004705 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
4706 return wpa_drv_p2p_invite(wpa_s, peer_addr, role, bssid,
4707 ssid->ssid, ssid->ssid_len,
4708 go_dev_addr, 1);
4709
4710 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
4711 return -1;
4712
Irfan Sherifff0619662012-09-23 19:11:37 -07004713#ifdef ANDROID_P2P
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004714 return p2p_invite(wpa_s->global->p2p, peer_addr, role, bssid,
Irfan Sheriffaf84a572012-09-22 16:59:30 -07004715 ssid->ssid, ssid->ssid_len, force_freq, go_dev_addr, 1);
Irfan Sherifff0619662012-09-23 19:11:37 -07004716#else
4717 return p2p_invite(wpa_s->global->p2p, peer_addr, role, bssid,
4718 ssid->ssid, ssid->ssid_len, freq, go_dev_addr, 1);
4719#endif
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004720}
4721
4722
4723/* Invite to join an active group */
4724int wpas_p2p_invite_group(struct wpa_supplicant *wpa_s, const char *ifname,
4725 const u8 *peer_addr, const u8 *go_dev_addr)
4726{
4727 struct wpa_global *global = wpa_s->global;
4728 enum p2p_invite_role role;
4729 u8 *bssid = NULL;
4730 struct wpa_ssid *ssid;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08004731 int persistent;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004732
Jouni Malinen31be0a42012-08-31 21:20:51 +03004733 wpa_s->p2p_persistent_go_freq = 0;
4734 wpa_s->p2p_go_ht40 = 0;
4735
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004736 for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
4737 if (os_strcmp(wpa_s->ifname, ifname) == 0)
4738 break;
4739 }
4740 if (wpa_s == NULL) {
4741 wpa_printf(MSG_DEBUG, "P2P: Interface '%s' not found", ifname);
4742 return -1;
4743 }
4744
4745 ssid = wpa_s->current_ssid;
4746 if (ssid == NULL) {
4747 wpa_printf(MSG_DEBUG, "P2P: No current SSID to use for "
4748 "invitation");
4749 return -1;
4750 }
4751
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08004752 persistent = ssid->p2p_persistent_group &&
4753 wpas_p2p_get_persistent(wpa_s->parent, peer_addr,
4754 ssid->ssid, ssid->ssid_len);
4755
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004756 if (ssid->mode == WPAS_MODE_P2P_GO) {
4757 role = P2P_INVITE_ROLE_ACTIVE_GO;
4758 bssid = wpa_s->own_addr;
4759 if (go_dev_addr == NULL)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004760 go_dev_addr = wpa_s->global->p2p_dev_addr;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004761 } else {
4762 role = P2P_INVITE_ROLE_CLIENT;
4763 if (wpa_s->wpa_state < WPA_ASSOCIATED) {
4764 wpa_printf(MSG_DEBUG, "P2P: Not associated - cannot "
4765 "invite to current group");
4766 return -1;
4767 }
4768 bssid = wpa_s->bssid;
4769 if (go_dev_addr == NULL &&
4770 !is_zero_ether_addr(wpa_s->go_dev_addr))
4771 go_dev_addr = wpa_s->go_dev_addr;
4772 }
4773 wpa_s->parent->pending_invite_ssid_id = -1;
4774
4775 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
4776 return wpa_drv_p2p_invite(wpa_s, peer_addr, role, bssid,
4777 ssid->ssid, ssid->ssid_len,
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08004778 go_dev_addr, persistent);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004779
4780 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
4781 return -1;
4782
4783 return p2p_invite(wpa_s->global->p2p, peer_addr, role, bssid,
4784 ssid->ssid, ssid->ssid_len, wpa_s->assoc_freq,
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08004785 go_dev_addr, persistent);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004786}
4787
4788
4789void wpas_p2p_completed(struct wpa_supplicant *wpa_s)
4790{
4791 struct wpa_ssid *ssid = wpa_s->current_ssid;
4792 const char *ssid_txt;
4793 u8 go_dev_addr[ETH_ALEN];
Jouni Malinen75ecf522011-06-27 15:19:46 -07004794 int network_id = -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004795 int persistent;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004796 int freq;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004797
Dmitry Shmidt04949592012-07-19 12:16:46 -07004798 if (ssid == NULL || ssid->mode != WPAS_MODE_P2P_GROUP_FORMATION) {
4799 eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
4800 wpa_s->parent, NULL);
4801 }
4802
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004803 if (!wpa_s->show_group_started || !ssid)
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004804 goto done;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004805
4806 wpa_s->show_group_started = 0;
4807
4808 ssid_txt = wpa_ssid_txt(ssid->ssid, ssid->ssid_len);
4809 os_memset(go_dev_addr, 0, ETH_ALEN);
4810 if (ssid->bssid_set)
4811 os_memcpy(go_dev_addr, ssid->bssid, ETH_ALEN);
4812 persistent = wpas_p2p_persistent_group(wpa_s, go_dev_addr, ssid->ssid,
4813 ssid->ssid_len);
4814 os_memcpy(wpa_s->go_dev_addr, go_dev_addr, ETH_ALEN);
4815
4816 if (wpa_s->global->p2p_group_formation == wpa_s)
4817 wpa_s->global->p2p_group_formation = NULL;
4818
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004819 freq = wpa_s->current_bss ? wpa_s->current_bss->freq :
4820 (int) wpa_s->assoc_freq;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004821 if (ssid->passphrase == NULL && ssid->psk_set) {
4822 char psk[65];
4823 wpa_snprintf_hex(psk, sizeof(psk), ssid->psk, 32);
4824 wpa_msg(wpa_s->parent, MSG_INFO, P2P_EVENT_GROUP_STARTED
4825 "%s client ssid=\"%s\" freq=%d psk=%s go_dev_addr="
4826 MACSTR "%s",
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004827 wpa_s->ifname, ssid_txt, freq, psk,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004828 MAC2STR(go_dev_addr),
4829 persistent ? " [PERSISTENT]" : "");
4830 } else {
4831 wpa_msg(wpa_s->parent, MSG_INFO, P2P_EVENT_GROUP_STARTED
4832 "%s client ssid=\"%s\" freq=%d passphrase=\"%s\" "
4833 "go_dev_addr=" MACSTR "%s",
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004834 wpa_s->ifname, ssid_txt, freq,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004835 ssid->passphrase ? ssid->passphrase : "",
4836 MAC2STR(go_dev_addr),
4837 persistent ? " [PERSISTENT]" : "");
4838 }
4839
4840 if (persistent)
Jouni Malinen75ecf522011-06-27 15:19:46 -07004841 network_id = wpas_p2p_store_persistent_group(wpa_s->parent,
4842 ssid, go_dev_addr);
4843 if (network_id < 0)
4844 network_id = ssid->id;
4845 wpas_notify_p2p_group_started(wpa_s, ssid, network_id, 1);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004846
4847done:
Jouni Malinendc7b7132012-09-14 12:53:47 -07004848 if (wpa_s->global->p2p_cb_on_scan_complete && !wpa_s->global->p2p_disabled &&
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004849 wpa_s->global->p2p != NULL) {
Jouni Malinendc7b7132012-09-14 12:53:47 -07004850 wpa_s->global->p2p_cb_on_scan_complete = 0;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004851 if (p2p_other_scan_completed(wpa_s->global->p2p) == 1) {
4852 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Pending P2P operation "
4853 "continued after successful connection");
4854 p2p_increase_search_delay(
4855 wpa_s->global->p2p,
4856 wpas_p2p_search_delay(wpa_s));
4857 }
4858 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004859}
4860
4861
4862int wpas_p2p_presence_req(struct wpa_supplicant *wpa_s, u32 duration1,
4863 u32 interval1, u32 duration2, u32 interval2)
4864{
4865 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
4866 return -1;
4867 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
4868 return -1;
4869
4870 if (wpa_s->wpa_state < WPA_ASSOCIATED ||
4871 wpa_s->current_ssid == NULL ||
4872 wpa_s->current_ssid->mode != WPAS_MODE_INFRA)
4873 return -1;
4874
4875 return p2p_presence_req(wpa_s->global->p2p, wpa_s->bssid,
4876 wpa_s->own_addr, wpa_s->assoc_freq,
4877 duration1, interval1, duration2, interval2);
4878}
4879
4880
4881int wpas_p2p_ext_listen(struct wpa_supplicant *wpa_s, unsigned int period,
4882 unsigned int interval)
4883{
4884 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
4885 return -1;
4886
4887 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
4888 return -1;
4889
4890 return p2p_ext_listen(wpa_s->global->p2p, period, interval);
4891}
4892
4893
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004894static int wpas_p2p_is_client(struct wpa_supplicant *wpa_s)
4895{
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08004896 if (wpa_s->current_ssid == NULL) {
4897 /*
4898 * current_ssid can be cleared when P2P client interface gets
4899 * disconnected, so assume this interface was used as P2P
4900 * client.
4901 */
4902 return 1;
4903 }
4904 return wpa_s->current_ssid->p2p_group &&
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004905 wpa_s->current_ssid->mode == WPAS_MODE_INFRA;
4906}
4907
4908
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004909static void wpas_p2p_group_idle_timeout(void *eloop_ctx, void *timeout_ctx)
4910{
4911 struct wpa_supplicant *wpa_s = eloop_ctx;
4912
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004913 if (wpa_s->conf->p2p_group_idle == 0 && !wpas_p2p_is_client(wpa_s)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004914 wpa_printf(MSG_DEBUG, "P2P: Ignore group idle timeout - "
4915 "disabled");
4916 return;
4917 }
4918
Dmitry Shmidt04949592012-07-19 12:16:46 -07004919 wpa_printf(MSG_DEBUG, "P2P: Group idle timeout reached - terminate "
4920 "group");
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004921 wpas_p2p_group_delete(wpa_s, P2P_GROUP_REMOVAL_IDLE_TIMEOUT);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004922}
4923
4924
4925static void wpas_p2p_set_group_idle_timeout(struct wpa_supplicant *wpa_s)
4926{
Dmitry Shmidt04949592012-07-19 12:16:46 -07004927 int timeout;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004928
Dmitry Shmidt04949592012-07-19 12:16:46 -07004929 if (eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL) > 0)
4930 wpa_printf(MSG_DEBUG, "P2P: Cancelled P2P group idle timeout");
4931
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004932 if (wpa_s->current_ssid == NULL || !wpa_s->current_ssid->p2p_group)
4933 return;
4934
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004935 timeout = wpa_s->conf->p2p_group_idle;
4936 if (wpa_s->current_ssid->mode == WPAS_MODE_INFRA &&
4937 (timeout == 0 || timeout > P2P_MAX_CLIENT_IDLE))
4938 timeout = P2P_MAX_CLIENT_IDLE;
4939
4940 if (timeout == 0)
4941 return;
4942
Dmitry Shmidt04949592012-07-19 12:16:46 -07004943 if (timeout < 0) {
4944 if (wpa_s->current_ssid->mode == WPAS_MODE_INFRA)
4945 timeout = 0; /* special client mode no-timeout */
4946 else
4947 return;
4948 }
4949
4950 if (wpa_s->p2p_in_provisioning) {
4951 /*
4952 * Use the normal group formation timeout during the
4953 * provisioning phase to avoid terminating this process too
4954 * early due to group idle timeout.
4955 */
4956 wpa_printf(MSG_DEBUG, "P2P: Do not use P2P group idle timeout "
4957 "during provisioning");
4958 return;
4959 }
Dmitry Shmidtad266fb2012-08-24 17:03:35 -07004960#ifndef ANDROID_P2P
Dmitry Shmidt04949592012-07-19 12:16:46 -07004961 if (wpa_s->show_group_started) {
4962 /*
4963 * Use the normal group formation timeout between the end of
4964 * the provisioning phase and completion of 4-way handshake to
4965 * avoid terminating this process too early due to group idle
4966 * timeout.
4967 */
4968 wpa_printf(MSG_DEBUG, "P2P: Do not use P2P group idle timeout "
4969 "while waiting for initial 4-way handshake to "
4970 "complete");
4971 return;
4972 }
Dmitry Shmidtad266fb2012-08-24 17:03:35 -07004973#endif
Dmitry Shmidt04949592012-07-19 12:16:46 -07004974
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004975 wpa_printf(MSG_DEBUG, "P2P: Set P2P group idle timeout to %u seconds",
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004976 timeout);
4977 eloop_register_timeout(timeout, 0, wpas_p2p_group_idle_timeout,
4978 wpa_s, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004979}
4980
4981
Jouni Malinen2b89da82012-08-31 22:04:41 +03004982/* Returns 1 if the interface was removed */
4983int wpas_p2p_deauth_notif(struct wpa_supplicant *wpa_s, const u8 *bssid,
4984 u16 reason_code, const u8 *ie, size_t ie_len,
4985 int locally_generated)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004986{
4987 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
Jouni Malinen2b89da82012-08-31 22:04:41 +03004988 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004989 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
Jouni Malinen2b89da82012-08-31 22:04:41 +03004990 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004991
Dmitry Shmidt04949592012-07-19 12:16:46 -07004992 if (!locally_generated)
4993 p2p_deauth_notif(wpa_s->global->p2p, bssid, reason_code, ie,
4994 ie_len);
4995
4996 if (reason_code == WLAN_REASON_DEAUTH_LEAVING && !locally_generated &&
4997 wpa_s->current_ssid &&
4998 wpa_s->current_ssid->p2p_group &&
4999 wpa_s->current_ssid->mode == WPAS_MODE_INFRA) {
5000 wpa_printf(MSG_DEBUG, "P2P: GO indicated that the P2P Group "
5001 "session is ending");
Jouni Malinen2b89da82012-08-31 22:04:41 +03005002 if (wpas_p2p_group_delete(wpa_s,
5003 P2P_GROUP_REMOVAL_GO_ENDING_SESSION)
5004 > 0)
5005 return 1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07005006 }
Jouni Malinen2b89da82012-08-31 22:04:41 +03005007
5008 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005009}
5010
5011
5012void wpas_p2p_disassoc_notif(struct wpa_supplicant *wpa_s, const u8 *bssid,
Dmitry Shmidt04949592012-07-19 12:16:46 -07005013 u16 reason_code, const u8 *ie, size_t ie_len,
5014 int locally_generated)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005015{
5016 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
5017 return;
5018 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
5019 return;
5020
Dmitry Shmidt04949592012-07-19 12:16:46 -07005021 if (!locally_generated)
5022 p2p_disassoc_notif(wpa_s->global->p2p, bssid, reason_code, ie,
5023 ie_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005024}
5025
5026
5027void wpas_p2p_update_config(struct wpa_supplicant *wpa_s)
5028{
5029 struct p2p_data *p2p = wpa_s->global->p2p;
5030
5031 if (p2p == NULL)
5032 return;
5033
5034 if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_CAPABLE))
5035 return;
5036
5037 if (wpa_s->conf->changed_parameters & CFG_CHANGED_DEVICE_NAME)
5038 p2p_set_dev_name(p2p, wpa_s->conf->device_name);
5039
5040 if (wpa_s->conf->changed_parameters & CFG_CHANGED_DEVICE_TYPE)
5041 p2p_set_pri_dev_type(p2p, wpa_s->conf->device_type);
5042
5043 if (wpa_s->wps &&
5044 (wpa_s->conf->changed_parameters & CFG_CHANGED_CONFIG_METHODS))
5045 p2p_set_config_methods(p2p, wpa_s->wps->config_methods);
5046
5047 if (wpa_s->wps && (wpa_s->conf->changed_parameters & CFG_CHANGED_UUID))
5048 p2p_set_uuid(p2p, wpa_s->wps->uuid);
5049
5050 if (wpa_s->conf->changed_parameters & CFG_CHANGED_WPS_STRING) {
5051 p2p_set_manufacturer(p2p, wpa_s->conf->manufacturer);
5052 p2p_set_model_name(p2p, wpa_s->conf->model_name);
5053 p2p_set_model_number(p2p, wpa_s->conf->model_number);
5054 p2p_set_serial_number(p2p, wpa_s->conf->serial_number);
5055 }
5056
5057 if (wpa_s->conf->changed_parameters & CFG_CHANGED_SEC_DEVICE_TYPE)
5058 p2p_set_sec_dev_types(p2p,
5059 (void *) wpa_s->conf->sec_device_type,
5060 wpa_s->conf->num_sec_device_types);
5061
5062 if (wpa_s->conf->changed_parameters & CFG_CHANGED_VENDOR_EXTENSION) {
5063 int i;
5064 p2p_remove_wps_vendor_extensions(p2p);
5065 for (i = 0; i < MAX_WPS_VENDOR_EXT; i++) {
5066 if (wpa_s->conf->wps_vendor_ext[i] == NULL)
5067 continue;
5068 p2p_add_wps_vendor_extension(
5069 p2p, wpa_s->conf->wps_vendor_ext[i]);
5070 }
5071 }
5072
5073 if ((wpa_s->conf->changed_parameters & CFG_CHANGED_COUNTRY) &&
5074 wpa_s->conf->country[0] && wpa_s->conf->country[1]) {
5075 char country[3];
5076 country[0] = wpa_s->conf->country[0];
5077 country[1] = wpa_s->conf->country[1];
5078 country[2] = 0x04;
5079 p2p_set_country(p2p, country);
5080 }
5081
5082 if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_SSID_POSTFIX) {
5083 p2p_set_ssid_postfix(p2p, (u8 *) wpa_s->conf->p2p_ssid_postfix,
5084 wpa_s->conf->p2p_ssid_postfix ?
5085 os_strlen(wpa_s->conf->p2p_ssid_postfix) :
5086 0);
5087 }
5088
5089 if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_INTRA_BSS)
5090 p2p_set_intra_bss_dist(p2p, wpa_s->conf->p2p_intra_bss);
Jouni Malinen75ecf522011-06-27 15:19:46 -07005091
5092 if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_LISTEN_CHANNEL) {
5093 u8 reg_class, channel;
5094 int ret;
5095 unsigned int r;
5096 if (wpa_s->conf->p2p_listen_reg_class &&
5097 wpa_s->conf->p2p_listen_channel) {
5098 reg_class = wpa_s->conf->p2p_listen_reg_class;
5099 channel = wpa_s->conf->p2p_listen_channel;
5100 } else {
5101 reg_class = 81;
5102 /*
5103 * Pick one of the social channels randomly as the
5104 * listen channel.
5105 */
5106 os_get_random((u8 *) &r, sizeof(r));
5107 channel = 1 + (r % 3) * 5;
5108 }
5109 ret = p2p_set_listen_channel(p2p, reg_class, channel);
5110 if (ret)
5111 wpa_printf(MSG_ERROR, "P2P: Own listen channel update "
5112 "failed: %d", ret);
5113 }
5114 if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_OPER_CHANNEL) {
5115 u8 op_reg_class, op_channel, cfg_op_channel;
5116 int ret = 0;
5117 unsigned int r;
5118 if (wpa_s->conf->p2p_oper_reg_class &&
5119 wpa_s->conf->p2p_oper_channel) {
5120 op_reg_class = wpa_s->conf->p2p_oper_reg_class;
5121 op_channel = wpa_s->conf->p2p_oper_channel;
5122 cfg_op_channel = 1;
5123 } else {
5124 op_reg_class = 81;
5125 /*
5126 * Use random operation channel from (1, 6, 11)
5127 *if no other preference is indicated.
5128 */
5129 os_get_random((u8 *) &r, sizeof(r));
5130 op_channel = 1 + (r % 3) * 5;
5131 cfg_op_channel = 0;
5132 }
5133 ret = p2p_set_oper_channel(p2p, op_reg_class, op_channel,
5134 cfg_op_channel);
5135 if (ret)
5136 wpa_printf(MSG_ERROR, "P2P: Own oper channel update "
5137 "failed: %d", ret);
5138 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07005139
5140 if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_PREF_CHAN) {
5141 if (p2p_set_pref_chan(p2p, wpa_s->conf->num_p2p_pref_chan,
5142 wpa_s->conf->p2p_pref_chan) < 0) {
5143 wpa_printf(MSG_ERROR, "P2P: Preferred channel list "
5144 "update failed");
5145 }
5146 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005147}
5148
5149
5150int wpas_p2p_set_noa(struct wpa_supplicant *wpa_s, u8 count, int start,
5151 int duration)
5152{
5153 if (!wpa_s->ap_iface)
5154 return -1;
5155 return hostapd_p2p_set_noa(wpa_s->ap_iface->bss[0], count, start,
5156 duration);
5157}
5158
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005159
5160int wpas_p2p_set_cross_connect(struct wpa_supplicant *wpa_s, int enabled)
5161{
5162 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
5163 return -1;
5164 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
5165 return -1;
5166
5167 wpa_s->global->cross_connection = enabled;
5168 p2p_set_cross_connect(wpa_s->global->p2p, enabled);
5169
5170 if (!enabled) {
5171 struct wpa_supplicant *iface;
5172
5173 for (iface = wpa_s->global->ifaces; iface; iface = iface->next)
5174 {
5175 if (iface->cross_connect_enabled == 0)
5176 continue;
5177
5178 iface->cross_connect_enabled = 0;
5179 iface->cross_connect_in_use = 0;
5180 wpa_msg(iface->parent, MSG_INFO,
5181 P2P_EVENT_CROSS_CONNECT_DISABLE "%s %s",
5182 iface->ifname, iface->cross_connect_uplink);
5183 }
5184 }
5185
5186 return 0;
5187}
5188
5189
5190static void wpas_p2p_enable_cross_connect(struct wpa_supplicant *uplink)
5191{
5192 struct wpa_supplicant *iface;
5193
5194 if (!uplink->global->cross_connection)
5195 return;
5196
5197 for (iface = uplink->global->ifaces; iface; iface = iface->next) {
5198 if (!iface->cross_connect_enabled)
5199 continue;
5200 if (os_strcmp(uplink->ifname, iface->cross_connect_uplink) !=
5201 0)
5202 continue;
5203 if (iface->ap_iface == NULL)
5204 continue;
5205 if (iface->cross_connect_in_use)
5206 continue;
5207
5208 iface->cross_connect_in_use = 1;
5209 wpa_msg(iface->parent, MSG_INFO,
5210 P2P_EVENT_CROSS_CONNECT_ENABLE "%s %s",
5211 iface->ifname, iface->cross_connect_uplink);
5212 }
5213}
5214
5215
5216static void wpas_p2p_disable_cross_connect(struct wpa_supplicant *uplink)
5217{
5218 struct wpa_supplicant *iface;
5219
5220 for (iface = uplink->global->ifaces; iface; iface = iface->next) {
5221 if (!iface->cross_connect_enabled)
5222 continue;
5223 if (os_strcmp(uplink->ifname, iface->cross_connect_uplink) !=
5224 0)
5225 continue;
5226 if (!iface->cross_connect_in_use)
5227 continue;
5228
5229 wpa_msg(iface->parent, MSG_INFO,
5230 P2P_EVENT_CROSS_CONNECT_DISABLE "%s %s",
5231 iface->ifname, iface->cross_connect_uplink);
5232 iface->cross_connect_in_use = 0;
5233 }
5234}
5235
5236
5237void wpas_p2p_notif_connected(struct wpa_supplicant *wpa_s)
5238{
5239 if (wpa_s->ap_iface || wpa_s->current_ssid == NULL ||
5240 wpa_s->current_ssid->mode != WPAS_MODE_INFRA ||
5241 wpa_s->cross_connect_disallowed)
5242 wpas_p2p_disable_cross_connect(wpa_s);
5243 else
5244 wpas_p2p_enable_cross_connect(wpa_s);
Dmitry Shmidt04949592012-07-19 12:16:46 -07005245 if (!wpa_s->ap_iface &&
5246 eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL) > 0)
5247 wpa_printf(MSG_DEBUG, "P2P: Cancelled P2P group idle timeout");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005248}
5249
5250
5251void wpas_p2p_notif_disconnected(struct wpa_supplicant *wpa_s)
5252{
5253 wpas_p2p_disable_cross_connect(wpa_s);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005254 if (!wpa_s->ap_iface &&
5255 !eloop_is_timeout_registered(wpas_p2p_group_idle_timeout,
5256 wpa_s, NULL))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005257 wpas_p2p_set_group_idle_timeout(wpa_s);
5258}
5259
5260
5261static void wpas_p2p_cross_connect_setup(struct wpa_supplicant *wpa_s)
5262{
5263 struct wpa_supplicant *iface;
5264
5265 if (!wpa_s->global->cross_connection)
5266 return;
5267
5268 for (iface = wpa_s->global->ifaces; iface; iface = iface->next) {
5269 if (iface == wpa_s)
5270 continue;
5271 if (iface->drv_flags &
5272 WPA_DRIVER_FLAGS_P2P_DEDICATED_INTERFACE)
5273 continue;
5274 if (iface->drv_flags & WPA_DRIVER_FLAGS_P2P_CAPABLE)
5275 continue;
5276
5277 wpa_s->cross_connect_enabled = 1;
5278 os_strlcpy(wpa_s->cross_connect_uplink, iface->ifname,
5279 sizeof(wpa_s->cross_connect_uplink));
5280 wpa_printf(MSG_DEBUG, "P2P: Enable cross connection from "
5281 "%s to %s whenever uplink is available",
5282 wpa_s->ifname, wpa_s->cross_connect_uplink);
5283
5284 if (iface->ap_iface || iface->current_ssid == NULL ||
5285 iface->current_ssid->mode != WPAS_MODE_INFRA ||
5286 iface->cross_connect_disallowed ||
5287 iface->wpa_state != WPA_COMPLETED)
5288 break;
5289
5290 wpa_s->cross_connect_in_use = 1;
5291 wpa_msg(wpa_s->parent, MSG_INFO,
5292 P2P_EVENT_CROSS_CONNECT_ENABLE "%s %s",
5293 wpa_s->ifname, wpa_s->cross_connect_uplink);
5294 break;
5295 }
5296}
5297
5298
5299int wpas_p2p_notif_pbc_overlap(struct wpa_supplicant *wpa_s)
5300{
5301 if (wpa_s->p2p_group_interface != P2P_GROUP_INTERFACE_CLIENT &&
5302 !wpa_s->p2p_in_provisioning)
5303 return 0; /* not P2P client operation */
5304
5305 wpa_printf(MSG_DEBUG, "P2P: Terminate connection due to WPS PBC "
5306 "session overlap");
5307 if (wpa_s != wpa_s->parent)
5308 wpa_msg_ctrl(wpa_s->parent, MSG_INFO, WPS_EVENT_OVERLAP);
5309
5310 if (wpa_s->global->p2p)
5311 p2p_group_formation_failed(wpa_s->global->p2p);
5312
5313 eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
5314 wpa_s->parent, NULL);
5315
5316 wpas_group_formation_completed(wpa_s, 0);
5317 return 1;
5318}
5319
5320
5321void wpas_p2p_update_channel_list(struct wpa_supplicant *wpa_s)
5322{
5323 struct p2p_channels chan;
5324
5325 if (wpa_s->global == NULL || wpa_s->global->p2p == NULL)
5326 return;
5327
5328 os_memset(&chan, 0, sizeof(chan));
5329 if (wpas_p2p_setup_channels(wpa_s, &chan)) {
5330 wpa_printf(MSG_ERROR, "P2P: Failed to update supported "
5331 "channel list");
5332 return;
5333 }
5334
5335 p2p_update_channel_list(wpa_s->global->p2p, &chan);
5336}
5337
5338
5339int wpas_p2p_cancel(struct wpa_supplicant *wpa_s)
5340{
5341 struct wpa_global *global = wpa_s->global;
5342 int found = 0;
5343 const u8 *peer;
5344
5345 if (global->p2p == NULL)
5346 return -1;
5347
5348 wpa_printf(MSG_DEBUG, "P2P: Request to cancel group formation");
5349
5350 if (wpa_s->pending_interface_name[0] &&
5351 !is_zero_ether_addr(wpa_s->pending_interface_addr))
5352 found = 1;
5353
5354 peer = p2p_get_go_neg_peer(global->p2p);
5355 if (peer) {
5356 wpa_printf(MSG_DEBUG, "P2P: Unauthorize pending GO Neg peer "
5357 MACSTR, MAC2STR(peer));
5358 p2p_unauthorize(global->p2p, peer);
Dmitry Shmidt04949592012-07-19 12:16:46 -07005359 found = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005360 }
5361
5362 wpas_p2p_stop_find(wpa_s);
5363
5364 for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
5365 if (wpa_s == global->p2p_group_formation &&
5366 (wpa_s->p2p_in_provisioning ||
5367 wpa_s->parent->pending_interface_type ==
5368 WPA_IF_P2P_CLIENT)) {
5369 wpa_printf(MSG_DEBUG, "P2P: Interface %s in group "
5370 "formation found - cancelling",
5371 wpa_s->ifname);
5372 found = 1;
5373 eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
5374 wpa_s->parent, NULL);
Jouni Malinenadddfc42012-10-03 14:31:41 -07005375 if (wpa_s->p2p_in_provisioning) {
5376 wpas_group_formation_completed(wpa_s, 0);
5377 break;
5378 }
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005379 wpas_p2p_group_delete(wpa_s,
5380 P2P_GROUP_REMOVAL_REQUESTED);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005381 break;
5382 }
5383 }
5384
5385 if (!found) {
5386 wpa_printf(MSG_DEBUG, "P2P: No ongoing group formation found");
5387 return -1;
5388 }
5389
5390 return 0;
5391}
5392
5393
5394void wpas_p2p_interface_unavailable(struct wpa_supplicant *wpa_s)
5395{
5396 if (wpa_s->current_ssid == NULL || !wpa_s->current_ssid->p2p_group)
5397 return;
5398
5399 wpa_printf(MSG_DEBUG, "P2P: Remove group due to driver resource not "
5400 "being available anymore");
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005401 wpas_p2p_group_delete(wpa_s, P2P_GROUP_REMOVAL_UNAVAILABLE);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005402}
5403
5404
5405void wpas_p2p_update_best_channels(struct wpa_supplicant *wpa_s,
5406 int freq_24, int freq_5, int freq_overall)
5407{
5408 struct p2p_data *p2p = wpa_s->global->p2p;
5409 if (p2p == NULL || (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT))
5410 return;
5411 p2p_set_best_channels(p2p, freq_24, freq_5, freq_overall);
5412}
5413
5414
5415int wpas_p2p_unauthorize(struct wpa_supplicant *wpa_s, const char *addr)
5416{
5417 u8 peer[ETH_ALEN];
5418 struct p2p_data *p2p = wpa_s->global->p2p;
5419
5420 if (p2p == NULL || (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT))
5421 return -1;
5422
5423 if (hwaddr_aton(addr, peer))
5424 return -1;
5425
5426 return p2p_unauthorize(p2p, peer);
5427}
5428
5429
5430/**
5431 * wpas_p2p_disconnect - Disconnect from a P2P Group
5432 * @wpa_s: Pointer to wpa_supplicant data
5433 * Returns: 0 on success, -1 on failure
5434 *
5435 * This can be used to disconnect from a group in which the local end is a P2P
5436 * Client or to end a P2P Group in case the local end is the Group Owner. If a
5437 * virtual network interface was created for this group, that interface will be
5438 * removed. Otherwise, only the configured P2P group network will be removed
5439 * from the interface.
5440 */
5441int wpas_p2p_disconnect(struct wpa_supplicant *wpa_s)
5442{
5443
5444 if (wpa_s == NULL)
5445 return -1;
5446
Jouni Malinen2b89da82012-08-31 22:04:41 +03005447 return wpas_p2p_group_delete(wpa_s, P2P_GROUP_REMOVAL_REQUESTED) < 0 ?
5448 -1 : 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005449}
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005450
5451
5452int wpas_p2p_in_progress(struct wpa_supplicant *wpa_s)
5453{
5454 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
5455 return 0;
5456
5457 return p2p_in_progress(wpa_s->global->p2p);
5458}
5459
5460
5461void wpas_p2p_network_removed(struct wpa_supplicant *wpa_s,
5462 struct wpa_ssid *ssid)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005463{
5464 if (wpa_s->p2p_in_provisioning && ssid->p2p_group &&
5465 eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
5466 wpa_s->parent, NULL) > 0) {
Dmitry Shmidt04949592012-07-19 12:16:46 -07005467 /**
5468 * Remove the network by scheduling the group formation
5469 * timeout to happen immediately. The teardown code
5470 * needs to be scheduled to run asynch later so that we
5471 * don't delete data from under ourselves unexpectedly.
5472 * Calling wpas_p2p_group_formation_timeout directly
5473 * causes a series of crashes in WPS failure scenarios.
5474 */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005475 wpa_printf(MSG_DEBUG, "P2P: Canceled group formation due to "
5476 "P2P group network getting removed");
Dmitry Shmidt04949592012-07-19 12:16:46 -07005477 eloop_register_timeout(0, 0, wpas_p2p_group_formation_timeout,
5478 wpa_s->parent, NULL);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005479 }
5480}
5481
5482
5483struct wpa_ssid * wpas_p2p_get_persistent(struct wpa_supplicant *wpa_s,
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08005484 const u8 *addr, const u8 *ssid,
5485 size_t ssid_len)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005486{
5487 struct wpa_ssid *s;
5488 size_t i;
5489
5490 for (s = wpa_s->conf->ssid; s; s = s->next) {
5491 if (s->disabled != 2)
5492 continue;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08005493 if (ssid &&
5494 (ssid_len != s->ssid_len ||
5495 os_memcmp(ssid, s->ssid, ssid_len) != 0))
5496 continue;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005497 if (os_memcmp(s->bssid, addr, ETH_ALEN) == 0)
5498 return s; /* peer is GO in the persistent group */
5499 if (s->mode != WPAS_MODE_P2P_GO || s->p2p_client_list == NULL)
5500 continue;
5501 for (i = 0; i < s->num_p2p_clients; i++) {
5502 if (os_memcmp(s->p2p_client_list + i * ETH_ALEN,
5503 addr, ETH_ALEN) == 0)
5504 return s; /* peer is P2P client in persistent
5505 * group */
5506 }
5507 }
5508
5509 return NULL;
5510}
5511
5512
5513void wpas_p2p_notify_ap_sta_authorized(struct wpa_supplicant *wpa_s,
5514 const u8 *addr)
5515{
5516 if (addr == NULL)
5517 return;
5518 wpas_p2p_add_persistent_group_client(wpa_s, addr);
5519}
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08005520
Dmitry Shmidt04949592012-07-19 12:16:46 -07005521
5522static void wpas_p2p_fallback_to_go_neg(struct wpa_supplicant *wpa_s,
5523 int group_added)
5524{
5525 struct wpa_supplicant *group = wpa_s;
Dmitry Shmidt04949592012-07-19 12:16:46 -07005526 if (wpa_s->global->p2p_group_formation)
5527 group = wpa_s->global->p2p_group_formation;
5528 wpa_s = wpa_s->parent;
5529 offchannel_send_action_done(wpa_s);
5530 if (group_added)
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005531 wpas_p2p_group_delete(group, P2P_GROUP_REMOVAL_SILENT);
Dmitry Shmidt04949592012-07-19 12:16:46 -07005532 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Fall back to GO Negotiation");
5533 wpas_p2p_connect(wpa_s, wpa_s->pending_join_dev_addr, wpa_s->p2p_pin,
5534 wpa_s->p2p_wps_method, wpa_s->p2p_persistent_group, 0,
5535 0, 0, wpa_s->p2p_go_intent, wpa_s->p2p_connect_freq,
5536 wpa_s->p2p_persistent_id,
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005537 wpa_s->p2p_pd_before_go_neg,
5538 wpa_s->p2p_go_ht40);
Dmitry Shmidt04949592012-07-19 12:16:46 -07005539}
5540
5541
5542int wpas_p2p_scan_no_go_seen(struct wpa_supplicant *wpa_s)
5543{
5544 if (!wpa_s->p2p_fallback_to_go_neg ||
5545 wpa_s->p2p_in_provisioning <= 5)
5546 return 0;
5547
5548 if (wpas_p2p_peer_go(wpa_s, wpa_s->pending_join_dev_addr) > 0)
5549 return 0; /* peer operating as a GO */
5550
5551 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: GO not found for p2p_connect-auto - "
5552 "fallback to GO Negotiation");
5553 wpas_p2p_fallback_to_go_neg(wpa_s, 1);
5554
5555 return 1;
5556}
5557
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005558
5559unsigned int wpas_p2p_search_delay(struct wpa_supplicant *wpa_s)
5560{
5561 const char *rn, *rn2;
5562 struct wpa_supplicant *ifs;
5563
5564 if (wpa_s->wpa_state > WPA_SCANNING) {
5565 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Use %u ms search delay due to "
5566 "concurrent operation",
5567 P2P_CONCURRENT_SEARCH_DELAY);
5568 return P2P_CONCURRENT_SEARCH_DELAY;
5569 }
5570
5571 if (!wpa_s->driver->get_radio_name)
5572 return 0;
5573 rn = wpa_s->driver->get_radio_name(wpa_s->drv_priv);
5574 if (rn == NULL || rn[0] == '\0')
5575 return 0;
5576
5577 for (ifs = wpa_s->global->ifaces; ifs; ifs = ifs->next) {
5578 if (ifs == wpa_s || !ifs->driver->get_radio_name)
5579 continue;
5580
5581 rn2 = ifs->driver->get_radio_name(ifs->drv_priv);
5582 if (!rn2 || os_strcmp(rn, rn2) != 0)
5583 continue;
5584 if (ifs->wpa_state > WPA_SCANNING) {
5585 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Use %u ms search "
5586 "delay due to concurrent operation on "
5587 "interface %s",
5588 P2P_CONCURRENT_SEARCH_DELAY, ifs->ifname);
5589 return P2P_CONCURRENT_SEARCH_DELAY;
5590 }
5591 }
5592
5593 return 0;
5594}
5595
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08005596#ifdef ANDROID_P2P
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08005597int wpas_p2p_handle_frequency_conflicts(struct wpa_supplicant *wpa_s, int freq,
5598 struct wpa_ssid *ssid)
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08005599{
5600 struct wpa_supplicant *iface = NULL;
5601 struct p2p_data *p2p = wpa_s->global->p2p;
5602
5603 for (iface = wpa_s->global->ifaces; iface; iface = iface->next) {
Jeff Johnson12b1cd92012-10-07 19:34:24 -07005604 if ((iface->current_ssid) &&
5605 (iface->current_ssid->frequency != freq) &&
5606 ((iface->p2p_group_interface) ||
5607 (iface->current_ssid->p2p_group))) {
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08005608
Jeff Johnson12b1cd92012-10-07 19:34:24 -07005609 if ((iface->p2p_group_interface == P2P_GROUP_INTERFACE_GO) ||
5610 (iface->current_ssid->mode == WPAS_MODE_P2P_GO)) {
5611 /* Try to see whether we can move the GO. If it
5612 * is not possible, remove the GO interface
5613 */
5614 if (wpa_drv_switch_channel(iface, freq) == 0) {
5615 wpa_printf(MSG_ERROR, "P2P: GO Moved to freq(%d)", freq);
5616 iface->current_ssid->frequency = freq;
5617 continue;
5618 }
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08005619 }
5620
5621 /* If GO cannot be moved or if the conflicting interface is a
5622 * P2P Client, remove the interface depending up on the connection
5623 * priority */
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08005624 if(!wpas_is_p2p_prioritized(iface)) {
Dmitry Shmidtf4f5db32012-09-11 14:36:56 -07005625 /* STA connection has priority over existing
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08005626 * P2P connection. So remove the interface */
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08005627 wpa_printf(MSG_DEBUG, "P2P: Removing P2P connection due to Single channel"
Dmitry Shmidt687922c2012-03-26 14:02:32 -07005628 "concurrent mode frequency conflict");
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005629 wpas_p2p_group_delete(iface, P2P_GROUP_REMOVAL_FREQ_CONFLICT);
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08005630 /* If connection in progress is p2p connection, do not proceed for the connection */
5631 if (wpa_s == iface)
5632 return -1;
5633 else
5634 /* If connection in progress is STA connection, proceed for the connection */
5635 return 0;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08005636 } else {
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08005637 /* P2p connection has priority, disable the STA network*/
5638 wpa_supplicant_disable_network(wpa_s->global->ifaces, ssid);
5639 wpa_msg(wpa_s->global->ifaces, MSG_INFO, WPA_EVENT_FREQ_CONFLICT
5640 " id=%d", ssid->id);
5641 os_memset(wpa_s->global->ifaces->pending_bssid, 0, ETH_ALEN);
5642 if (wpa_s == iface) {
5643 /* p2p connection is in progress, continue connecting...*/
5644 return 0;
5645 }
5646 else {
5647 /* STA connection is in progress, do not allow to continue */
5648 return -1;
5649 }
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08005650 }
5651 }
5652 }
5653 return 0;
5654}
5655#endif