blob: 3e020999f9df85b0469f8c2894c5c7cc4be2c01e [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,
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800115 bss->freq, bss->age, bss->level,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700116 (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;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800823 if (os_strlen(params->passphrase) > 0) {
824 wpa_msg(wpa_s->parent, MSG_INFO, P2P_EVENT_GROUP_STARTED
825 "%s GO ssid=\"%s\" freq=%d passphrase=\"%s\" "
826 "go_dev_addr=" MACSTR "%s", wpa_s->ifname,
827 wpa_ssid_txt(ssid->ssid, ssid->ssid_len),
828 ssid->frequency, params->passphrase,
829 MAC2STR(wpa_s->global->p2p_dev_addr),
830 params->persistent_group ? " [PERSISTENT]" :
831 "");
832 } else {
833 char psk[65];
834 wpa_snprintf_hex(psk, sizeof(psk), params->psk,
835 sizeof(params->psk));
836 wpa_msg(wpa_s->parent, MSG_INFO, P2P_EVENT_GROUP_STARTED
837 "%s GO ssid=\"%s\" freq=%d psk=%s "
838 "go_dev_addr=" MACSTR "%s", wpa_s->ifname,
839 wpa_ssid_txt(ssid->ssid, ssid->ssid_len),
840 ssid->frequency, psk,
841 MAC2STR(wpa_s->global->p2p_dev_addr),
842 params->persistent_group ? " [PERSISTENT]" :
843 "");
844 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800845
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700846 if (params->persistent_group)
Jouni Malinen75ecf522011-06-27 15:19:46 -0700847 network_id = wpas_p2p_store_persistent_group(
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700848 wpa_s->parent, ssid,
Dmitry Shmidt497c1d52011-07-21 15:19:46 -0700849 wpa_s->global->p2p_dev_addr);
Jouni Malinen75ecf522011-06-27 15:19:46 -0700850 if (network_id < 0)
851 network_id = ssid->id;
852 wpas_notify_p2p_group_started(wpa_s, ssid, network_id, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700853 wpas_p2p_cross_connect_setup(wpa_s);
854 wpas_p2p_set_group_idle_timeout(wpa_s);
855 return;
856 }
857
858 wpa_printf(MSG_DEBUG, "P2P: Setting up WPS for GO provisioning");
859 if (wpa_supplicant_ap_mac_addr_filter(wpa_s,
860 params->peer_interface_addr)) {
861 wpa_printf(MSG_DEBUG, "P2P: Failed to setup MAC address "
862 "filtering");
863 return;
864 }
865 if (params->wps_method == WPS_PBC)
866 wpa_supplicant_ap_wps_pbc(wpa_s, params->peer_interface_addr,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800867 params->peer_device_addr);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700868 else if (wpa_s->p2p_pin[0])
869 wpa_supplicant_ap_wps_pin(wpa_s, params->peer_interface_addr,
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800870 wpa_s->p2p_pin, NULL, 0, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700871 os_free(wpa_s->go_params);
872 wpa_s->go_params = NULL;
873}
874
875
876static void wpas_start_wps_go(struct wpa_supplicant *wpa_s,
877 struct p2p_go_neg_results *params,
878 int group_formation)
879{
880 struct wpa_ssid *ssid;
881
Dmitry Shmidtaa532512012-09-24 10:35:31 -0700882 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Starting GO");
883 if (wpas_copy_go_neg_results(wpa_s, params) < 0) {
884 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Could not copy GO Negotiation "
885 "results");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700886 return;
Dmitry Shmidtaa532512012-09-24 10:35:31 -0700887 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700888
889 ssid = wpa_config_add_network(wpa_s->conf);
Dmitry Shmidtaa532512012-09-24 10:35:31 -0700890 if (ssid == NULL) {
891 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Could not add network for GO");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700892 return;
Dmitry Shmidtaa532512012-09-24 10:35:31 -0700893 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700894
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800895 wpa_s->show_group_started = 0;
896
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700897 wpa_config_set_network_defaults(ssid);
898 ssid->temporary = 1;
899 ssid->p2p_group = 1;
900 ssid->p2p_persistent_group = params->persistent_group;
901 ssid->mode = group_formation ? WPAS_MODE_P2P_GROUP_FORMATION :
902 WPAS_MODE_P2P_GO;
903 ssid->frequency = params->freq;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700904 ssid->ht40 = params->ht40;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700905 ssid->ssid = os_zalloc(params->ssid_len + 1);
906 if (ssid->ssid) {
907 os_memcpy(ssid->ssid, params->ssid, params->ssid_len);
908 ssid->ssid_len = params->ssid_len;
909 }
910 ssid->auth_alg = WPA_AUTH_ALG_OPEN;
911 ssid->key_mgmt = WPA_KEY_MGMT_PSK;
912 ssid->proto = WPA_PROTO_RSN;
913 ssid->pairwise_cipher = WPA_CIPHER_CCMP;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800914 if (os_strlen(params->passphrase) > 0) {
915 ssid->passphrase = os_strdup(params->passphrase);
916 if (ssid->passphrase == NULL) {
917 wpa_msg(wpa_s, MSG_ERROR, "P2P: Failed to copy "
918 "passphrase for GO");
919 wpa_config_remove_network(wpa_s->conf, ssid->id);
920 return;
921 }
922 } else
923 ssid->passphrase = NULL;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800924 ssid->psk_set = params->psk_set;
925 if (ssid->psk_set)
926 os_memcpy(ssid->psk, params->psk, sizeof(ssid->psk));
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800927 else if (ssid->passphrase)
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800928 wpa_config_update_psk(ssid);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700929 ssid->ap_max_inactivity = wpa_s->parent->conf->p2p_go_max_inactivity;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700930
931 wpa_s->ap_configured_cb = p2p_go_configured;
932 wpa_s->ap_configured_cb_ctx = wpa_s;
933 wpa_s->ap_configured_cb_data = wpa_s->go_params;
Jouni Malinen75ecf522011-06-27 15:19:46 -0700934 wpa_s->connect_without_scan = ssid;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700935 wpa_s->reassociate = 1;
936 wpa_s->disconnected = 0;
Dmitry Shmidtaa532512012-09-24 10:35:31 -0700937 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Request scan (that will be skipped) to "
938 "start GO)");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700939 wpa_supplicant_req_scan(wpa_s, 0, 0);
940}
941
942
943static void wpas_p2p_clone_config(struct wpa_supplicant *dst,
944 const struct wpa_supplicant *src)
945{
946 struct wpa_config *d;
947 const struct wpa_config *s;
948
949 d = dst->conf;
950 s = src->conf;
951
952#define C(n) if (s->n) d->n = os_strdup(s->n)
953 C(device_name);
954 C(manufacturer);
955 C(model_name);
956 C(model_number);
957 C(serial_number);
958 C(config_methods);
959#undef C
960
961 os_memcpy(d->device_type, s->device_type, WPS_DEV_TYPE_LEN);
962 os_memcpy(d->sec_device_type, s->sec_device_type,
963 sizeof(d->sec_device_type));
964 d->num_sec_device_types = s->num_sec_device_types;
965
966 d->p2p_group_idle = s->p2p_group_idle;
967 d->p2p_intra_bss = s->p2p_intra_bss;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800968 d->persistent_reconnect = s->persistent_reconnect;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700969 d->max_num_sta = s->max_num_sta;
970 d->pbc_in_m1 = s->pbc_in_m1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700971}
972
973
974static int wpas_p2p_add_group_interface(struct wpa_supplicant *wpa_s,
975 enum wpa_driver_if_type type)
976{
977 char ifname[120], force_ifname[120];
978
979 if (wpa_s->pending_interface_name[0]) {
980 wpa_printf(MSG_DEBUG, "P2P: Pending virtual interface exists "
981 "- skip creation of a new one");
982 if (is_zero_ether_addr(wpa_s->pending_interface_addr)) {
983 wpa_printf(MSG_DEBUG, "P2P: Pending virtual address "
984 "unknown?! ifname='%s'",
985 wpa_s->pending_interface_name);
986 return -1;
987 }
988 return 0;
989 }
990
991 os_snprintf(ifname, sizeof(ifname), "p2p-%s-%d", wpa_s->ifname,
992 wpa_s->p2p_group_idx);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800993 if (os_strlen(ifname) >= IFNAMSIZ &&
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700994 os_strlen(wpa_s->ifname) < IFNAMSIZ) {
995 /* Try to avoid going over the IFNAMSIZ length limit */
996 os_snprintf(ifname, sizeof(ifname), "p2p-%d",
997 wpa_s->p2p_group_idx);
998 }
999 force_ifname[0] = '\0';
1000
1001 wpa_printf(MSG_DEBUG, "P2P: Create a new interface %s for the group",
1002 ifname);
1003 wpa_s->p2p_group_idx++;
1004
1005 wpa_s->pending_interface_type = type;
1006 if (wpa_drv_if_add(wpa_s, type, ifname, NULL, NULL, force_ifname,
1007 wpa_s->pending_interface_addr, NULL) < 0) {
1008 wpa_printf(MSG_ERROR, "P2P: Failed to create new group "
1009 "interface");
1010 return -1;
1011 }
1012
1013 if (force_ifname[0]) {
1014 wpa_printf(MSG_DEBUG, "P2P: Driver forced interface name %s",
1015 force_ifname);
1016 os_strlcpy(wpa_s->pending_interface_name, force_ifname,
1017 sizeof(wpa_s->pending_interface_name));
1018 } else
1019 os_strlcpy(wpa_s->pending_interface_name, ifname,
1020 sizeof(wpa_s->pending_interface_name));
1021 wpa_printf(MSG_DEBUG, "P2P: Created pending virtual interface %s addr "
1022 MACSTR, wpa_s->pending_interface_name,
1023 MAC2STR(wpa_s->pending_interface_addr));
1024
1025 return 0;
1026}
1027
1028
1029static void wpas_p2p_remove_pending_group_interface(
1030 struct wpa_supplicant *wpa_s)
1031{
1032 if (!wpa_s->pending_interface_name[0] ||
1033 is_zero_ether_addr(wpa_s->pending_interface_addr))
1034 return; /* No pending virtual interface */
1035
1036 wpa_printf(MSG_DEBUG, "P2P: Removing pending group interface %s",
1037 wpa_s->pending_interface_name);
1038 wpa_drv_if_remove(wpa_s, wpa_s->pending_interface_type,
1039 wpa_s->pending_interface_name);
1040 os_memset(wpa_s->pending_interface_addr, 0, ETH_ALEN);
1041 wpa_s->pending_interface_name[0] = '\0';
1042}
1043
1044
1045static struct wpa_supplicant *
1046wpas_p2p_init_group_interface(struct wpa_supplicant *wpa_s, int go)
1047{
1048 struct wpa_interface iface;
1049 struct wpa_supplicant *group_wpa_s;
1050
1051 if (!wpa_s->pending_interface_name[0]) {
1052 wpa_printf(MSG_ERROR, "P2P: No pending group interface");
1053 if (!wpas_p2p_create_iface(wpa_s))
1054 return NULL;
1055 /*
1056 * Something has forced us to remove the pending interface; try
1057 * to create a new one and hope for the best that we will get
1058 * the same local address.
1059 */
1060 if (wpas_p2p_add_group_interface(wpa_s, go ? WPA_IF_P2P_GO :
1061 WPA_IF_P2P_CLIENT) < 0)
1062 return NULL;
1063 }
1064
1065 os_memset(&iface, 0, sizeof(iface));
1066 iface.ifname = wpa_s->pending_interface_name;
1067 iface.driver = wpa_s->driver->name;
1068 iface.ctrl_interface = wpa_s->conf->ctrl_interface;
1069 iface.driver_param = wpa_s->conf->driver_param;
1070 group_wpa_s = wpa_supplicant_add_iface(wpa_s->global, &iface);
1071 if (group_wpa_s == NULL) {
1072 wpa_printf(MSG_ERROR, "P2P: Failed to create new "
1073 "wpa_supplicant interface");
1074 return NULL;
1075 }
1076 wpa_s->pending_interface_name[0] = '\0';
1077 group_wpa_s->parent = wpa_s;
1078 group_wpa_s->p2p_group_interface = go ? P2P_GROUP_INTERFACE_GO :
1079 P2P_GROUP_INTERFACE_CLIENT;
1080 wpa_s->global->p2p_group_formation = group_wpa_s;
1081
1082 wpas_p2p_clone_config(group_wpa_s, wpa_s);
1083
1084 return group_wpa_s;
1085}
1086
1087
1088static void wpas_p2p_group_formation_timeout(void *eloop_ctx,
1089 void *timeout_ctx)
1090{
1091 struct wpa_supplicant *wpa_s = eloop_ctx;
1092 wpa_printf(MSG_DEBUG, "P2P: Group Formation timed out");
1093 if (wpa_s->global->p2p)
1094 p2p_group_formation_failed(wpa_s->global->p2p);
1095 else if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
1096 wpa_drv_p2p_group_formation_failed(wpa_s);
1097 wpas_group_formation_completed(wpa_s, 0);
1098}
1099
1100
1101void wpas_go_neg_completed(void *ctx, struct p2p_go_neg_results *res)
1102{
1103 struct wpa_supplicant *wpa_s = ctx;
1104
1105 if (wpa_s->off_channel_freq || wpa_s->roc_waiting_drv_freq) {
1106 wpa_drv_cancel_remain_on_channel(wpa_s);
1107 wpa_s->off_channel_freq = 0;
1108 wpa_s->roc_waiting_drv_freq = 0;
1109 }
1110
1111 if (res->status) {
1112 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_GO_NEG_FAILURE "status=%d",
1113 res->status);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001114 wpas_notify_p2p_go_neg_completed(wpa_s, res);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001115 wpas_p2p_remove_pending_group_interface(wpa_s);
1116 return;
1117 }
1118
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001119 if (wpa_s->p2p_go_ht40)
1120 res->ht40 = 1;
1121
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001122 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_GO_NEG_SUCCESS);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001123 wpas_notify_p2p_go_neg_completed(wpa_s, res);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001124
Dmitry Shmidt04949592012-07-19 12:16:46 -07001125 if (res->role_go && wpa_s->p2p_persistent_id >= 0) {
1126 struct wpa_ssid *ssid;
1127 ssid = wpa_config_get_network(wpa_s->conf,
1128 wpa_s->p2p_persistent_id);
1129 if (ssid && ssid->disabled == 2 &&
1130 ssid->mode == WPAS_MODE_P2P_GO && ssid->passphrase) {
1131 size_t len = os_strlen(ssid->passphrase);
1132 wpa_printf(MSG_DEBUG, "P2P: Override passphrase based "
1133 "on requested persistent group");
1134 os_memcpy(res->passphrase, ssid->passphrase, len);
1135 res->passphrase[len] = '\0';
1136 }
1137 }
1138
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001139 if (wpa_s->create_p2p_iface) {
1140 struct wpa_supplicant *group_wpa_s =
1141 wpas_p2p_init_group_interface(wpa_s, res->role_go);
1142 if (group_wpa_s == NULL) {
1143 wpas_p2p_remove_pending_group_interface(wpa_s);
1144 return;
1145 }
1146 if (group_wpa_s != wpa_s) {
1147 os_memcpy(group_wpa_s->p2p_pin, wpa_s->p2p_pin,
1148 sizeof(group_wpa_s->p2p_pin));
1149 group_wpa_s->p2p_wps_method = wpa_s->p2p_wps_method;
1150 }
1151 os_memset(wpa_s->pending_interface_addr, 0, ETH_ALEN);
1152 wpa_s->pending_interface_name[0] = '\0';
1153 group_wpa_s->p2p_in_provisioning = 1;
1154
1155 if (res->role_go)
1156 wpas_start_wps_go(group_wpa_s, res, 1);
1157 else
1158 wpas_start_wps_enrollee(group_wpa_s, res);
1159 } else {
1160 wpa_s->p2p_in_provisioning = 1;
1161 wpa_s->global->p2p_group_formation = wpa_s;
1162
1163 if (res->role_go)
1164 wpas_start_wps_go(wpa_s, res, 1);
1165 else
1166 wpas_start_wps_enrollee(ctx, res);
1167 }
1168
1169 wpa_s->p2p_long_listen = 0;
1170 eloop_cancel_timeout(wpas_p2p_long_listen_timeout, wpa_s, NULL);
1171
1172 eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s, NULL);
1173 eloop_register_timeout(15 + res->peer_config_timeout / 100,
1174 (res->peer_config_timeout % 100) * 10000,
1175 wpas_p2p_group_formation_timeout, wpa_s, NULL);
1176}
1177
1178
1179void wpas_go_neg_req_rx(void *ctx, const u8 *src, u16 dev_passwd_id)
1180{
1181 struct wpa_supplicant *wpa_s = ctx;
1182 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_GO_NEG_REQUEST MACSTR
1183 " dev_passwd_id=%u", MAC2STR(src), dev_passwd_id);
1184
1185 wpas_notify_p2p_go_neg_req(wpa_s, src, dev_passwd_id);
1186}
1187
1188
1189void wpas_dev_found(void *ctx, const u8 *addr,
1190 const struct p2p_peer_info *info,
1191 int new_device)
1192{
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001193#ifndef CONFIG_NO_STDOUT_DEBUG
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001194 struct wpa_supplicant *wpa_s = ctx;
1195 char devtype[WPS_DEV_TYPE_BUFSIZE];
Irfan Sheriff8367dc92012-09-09 17:08:19 -07001196#define WFD_DEV_INFO_SIZE 9
1197 char wfd_dev_info_hex[2 * WFD_DEV_INFO_SIZE + 1];
Irfan Sheriff8d965182012-09-11 08:58:24 -07001198 os_memset(wfd_dev_info_hex, 0, sizeof(wfd_dev_info_hex));
Irfan Sheriff8367dc92012-09-09 17:08:19 -07001199#ifdef CONFIG_WIFI_DISPLAY
1200 if (info->wfd_subelems) {
1201 wpa_snprintf_hex(wfd_dev_info_hex, sizeof(wfd_dev_info_hex),
1202 wpabuf_head(info->wfd_subelems),
1203 WFD_DEV_INFO_SIZE);
1204 }
1205#endif /* CONFIG_WIFI_DISPLAY */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001206 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_DEVICE_FOUND MACSTR
1207 " p2p_dev_addr=" MACSTR
1208 " pri_dev_type=%s name='%s' config_methods=0x%x "
Irfan Sheriff8367dc92012-09-09 17:08:19 -07001209 "dev_capab=0x%x group_capab=0x%x%s%s",
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001210 MAC2STR(addr), MAC2STR(info->p2p_device_addr),
1211 wps_dev_type_bin2str(info->pri_dev_type, devtype,
1212 sizeof(devtype)),
1213 info->device_name, info->config_methods,
Irfan Sheriff8367dc92012-09-09 17:08:19 -07001214 info->dev_capab, info->group_capab,
1215 wfd_dev_info_hex[0] ? " wfd_dev_info=0x" : "", wfd_dev_info_hex);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001216#endif /* CONFIG_NO_STDOUT_DEBUG */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001217
1218 wpas_notify_p2p_device_found(ctx, info->p2p_device_addr, new_device);
1219}
1220
1221
1222static void wpas_dev_lost(void *ctx, const u8 *dev_addr)
1223{
1224 struct wpa_supplicant *wpa_s = ctx;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001225
Dmitry Shmidt497c1d52011-07-21 15:19:46 -07001226 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_DEVICE_LOST
1227 "p2p_dev_addr=" MACSTR, MAC2STR(dev_addr));
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001228
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001229 wpas_notify_p2p_device_lost(wpa_s, dev_addr);
1230}
1231
1232
1233static int wpas_start_listen(void *ctx, unsigned int freq,
1234 unsigned int duration,
1235 const struct wpabuf *probe_resp_ie)
1236{
1237 struct wpa_supplicant *wpa_s = ctx;
1238
1239 wpa_drv_set_ap_wps_ie(wpa_s, NULL, probe_resp_ie, NULL);
1240
1241 if (wpa_drv_probe_req_report(wpa_s, 1) < 0) {
1242 wpa_printf(MSG_DEBUG, "P2P: Failed to request the driver to "
1243 "report received Probe Request frames");
1244 return -1;
1245 }
1246
1247 wpa_s->pending_listen_freq = freq;
1248 wpa_s->pending_listen_duration = duration;
1249
1250 if (wpa_drv_remain_on_channel(wpa_s, freq, duration) < 0) {
1251 wpa_printf(MSG_DEBUG, "P2P: Failed to request the driver "
1252 "to remain on channel (%u MHz) for Listen "
1253 "state", freq);
1254 wpa_s->pending_listen_freq = 0;
1255 return -1;
1256 }
1257 wpa_s->off_channel_freq = 0;
1258 wpa_s->roc_waiting_drv_freq = freq;
1259
1260 return 0;
1261}
1262
1263
1264static void wpas_stop_listen(void *ctx)
1265{
1266 struct wpa_supplicant *wpa_s = ctx;
1267 if (wpa_s->off_channel_freq || wpa_s->roc_waiting_drv_freq) {
1268 wpa_drv_cancel_remain_on_channel(wpa_s);
1269 wpa_s->off_channel_freq = 0;
1270 wpa_s->roc_waiting_drv_freq = 0;
1271 }
1272 wpa_drv_set_ap_wps_ie(wpa_s, NULL, NULL, NULL);
1273 wpa_drv_probe_req_report(wpa_s, 0);
1274}
1275
1276
1277static int wpas_send_probe_resp(void *ctx, const struct wpabuf *buf)
1278{
1279 struct wpa_supplicant *wpa_s = ctx;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001280 return wpa_drv_send_mlme(wpa_s, wpabuf_head(buf), wpabuf_len(buf), 1);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001281}
1282
1283
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001284/*
1285 * DNS Header section is used only to calculate compression pointers, so the
1286 * contents of this data does not matter, but the length needs to be reserved
1287 * in the virtual packet.
1288 */
1289#define DNS_HEADER_LEN 12
1290
1291/*
1292 * 27-octet in-memory packet from P2P specification containing two implied
1293 * queries for _tcp.lcoal. PTR IN and _udp.local. PTR IN
1294 */
1295#define P2P_SD_IN_MEMORY_LEN 27
1296
1297static int p2p_sd_dns_uncompress_label(char **upos, char *uend, u8 *start,
1298 u8 **spos, const u8 *end)
1299{
1300 while (*spos < end) {
1301 u8 val = ((*spos)[0] & 0xc0) >> 6;
1302 int len;
1303
1304 if (val == 1 || val == 2) {
1305 /* These are reserved values in RFC 1035 */
1306 wpa_printf(MSG_DEBUG, "P2P: Invalid domain name "
1307 "sequence starting with 0x%x", val);
1308 return -1;
1309 }
1310
1311 if (val == 3) {
1312 u16 offset;
1313 u8 *spos_tmp;
1314
1315 /* Offset */
1316 if (*spos + 2 > end) {
1317 wpa_printf(MSG_DEBUG, "P2P: No room for full "
1318 "DNS offset field");
1319 return -1;
1320 }
1321
1322 offset = (((*spos)[0] & 0x3f) << 8) | (*spos)[1];
1323 if (offset >= *spos - start) {
1324 wpa_printf(MSG_DEBUG, "P2P: Invalid DNS "
1325 "pointer offset %u", offset);
1326 return -1;
1327 }
1328
1329 (*spos) += 2;
1330 spos_tmp = start + offset;
1331 return p2p_sd_dns_uncompress_label(upos, uend, start,
1332 &spos_tmp,
1333 *spos - 2);
1334 }
1335
1336 /* Label */
1337 len = (*spos)[0] & 0x3f;
1338 if (len == 0)
1339 return 0;
1340
1341 (*spos)++;
1342 if (*spos + len > end) {
1343 wpa_printf(MSG_DEBUG, "P2P: Invalid domain name "
1344 "sequence - no room for label with length "
1345 "%u", len);
1346 return -1;
1347 }
1348
1349 if (*upos + len + 2 > uend)
1350 return -2;
1351
1352 os_memcpy(*upos, *spos, len);
1353 *spos += len;
1354 *upos += len;
1355 (*upos)[0] = '.';
1356 (*upos)++;
1357 (*upos)[0] = '\0';
1358 }
1359
1360 return 0;
1361}
1362
1363
1364/* Uncompress domain names per RFC 1035 using the P2P SD in-memory packet.
1365 * Returns -1 on parsing error (invalid input sequence), -2 if output buffer is
1366 * not large enough */
1367static int p2p_sd_dns_uncompress(char *buf, size_t buf_len, const u8 *msg,
1368 size_t msg_len, size_t offset)
1369{
1370 /* 27-octet in-memory packet from P2P specification */
1371 const char *prefix = "\x04_tcp\x05local\x00\x00\x0C\x00\x01"
1372 "\x04_udp\xC0\x11\x00\x0C\x00\x01";
1373 u8 *tmp, *end, *spos;
1374 char *upos, *uend;
1375 int ret = 0;
1376
1377 if (buf_len < 2)
1378 return -1;
1379 if (offset > msg_len)
1380 return -1;
1381
1382 tmp = os_malloc(DNS_HEADER_LEN + P2P_SD_IN_MEMORY_LEN + msg_len);
1383 if (tmp == NULL)
1384 return -1;
1385 spos = tmp + DNS_HEADER_LEN + P2P_SD_IN_MEMORY_LEN;
1386 end = spos + msg_len;
1387 spos += offset;
1388
1389 os_memset(tmp, 0, DNS_HEADER_LEN);
1390 os_memcpy(tmp + DNS_HEADER_LEN, prefix, P2P_SD_IN_MEMORY_LEN);
1391 os_memcpy(tmp + DNS_HEADER_LEN + P2P_SD_IN_MEMORY_LEN, msg, msg_len);
1392
1393 upos = buf;
1394 uend = buf + buf_len;
1395
1396 ret = p2p_sd_dns_uncompress_label(&upos, uend, tmp, &spos, end);
1397 if (ret) {
1398 os_free(tmp);
1399 return ret;
1400 }
1401
1402 if (upos == buf) {
1403 upos[0] = '.';
1404 upos[1] = '\0';
1405 } else if (upos[-1] == '.')
1406 upos[-1] = '\0';
1407
1408 os_free(tmp);
1409 return 0;
1410}
1411
1412
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001413static struct p2p_srv_bonjour *
1414wpas_p2p_service_get_bonjour(struct wpa_supplicant *wpa_s,
1415 const struct wpabuf *query)
1416{
1417 struct p2p_srv_bonjour *bsrv;
1418 size_t len;
1419
1420 len = wpabuf_len(query);
1421 dl_list_for_each(bsrv, &wpa_s->global->p2p_srv_bonjour,
1422 struct p2p_srv_bonjour, list) {
1423 if (len == wpabuf_len(bsrv->query) &&
1424 os_memcmp(wpabuf_head(query), wpabuf_head(bsrv->query),
1425 len) == 0)
1426 return bsrv;
1427 }
1428 return NULL;
1429}
1430
1431
1432static struct p2p_srv_upnp *
1433wpas_p2p_service_get_upnp(struct wpa_supplicant *wpa_s, u8 version,
1434 const char *service)
1435{
1436 struct p2p_srv_upnp *usrv;
1437
1438 dl_list_for_each(usrv, &wpa_s->global->p2p_srv_upnp,
1439 struct p2p_srv_upnp, list) {
1440 if (version == usrv->version &&
1441 os_strcmp(service, usrv->service) == 0)
1442 return usrv;
1443 }
1444 return NULL;
1445}
1446
1447
1448static void wpas_sd_add_proto_not_avail(struct wpabuf *resp, u8 srv_proto,
1449 u8 srv_trans_id)
1450{
1451 u8 *len_pos;
1452
1453 if (wpabuf_tailroom(resp) < 5)
1454 return;
1455
1456 /* Length (to be filled) */
1457 len_pos = wpabuf_put(resp, 2);
1458 wpabuf_put_u8(resp, srv_proto);
1459 wpabuf_put_u8(resp, srv_trans_id);
1460 /* Status Code */
1461 wpabuf_put_u8(resp, P2P_SD_PROTO_NOT_AVAILABLE);
1462 /* Response Data: empty */
1463 WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos - 2);
1464}
1465
1466
1467static void wpas_sd_all_bonjour(struct wpa_supplicant *wpa_s,
1468 struct wpabuf *resp, u8 srv_trans_id)
1469{
1470 struct p2p_srv_bonjour *bsrv;
1471 u8 *len_pos;
1472
1473 wpa_printf(MSG_DEBUG, "P2P: SD Request for all Bonjour services");
1474
1475 if (dl_list_empty(&wpa_s->global->p2p_srv_bonjour)) {
1476 wpa_printf(MSG_DEBUG, "P2P: Bonjour protocol not available");
1477 return;
1478 }
1479
1480 dl_list_for_each(bsrv, &wpa_s->global->p2p_srv_bonjour,
1481 struct p2p_srv_bonjour, list) {
1482 if (wpabuf_tailroom(resp) <
1483 5 + wpabuf_len(bsrv->query) + wpabuf_len(bsrv->resp))
1484 return;
1485 /* Length (to be filled) */
1486 len_pos = wpabuf_put(resp, 2);
1487 wpabuf_put_u8(resp, P2P_SERV_BONJOUR);
1488 wpabuf_put_u8(resp, srv_trans_id);
1489 /* Status Code */
1490 wpabuf_put_u8(resp, P2P_SD_SUCCESS);
1491 wpa_hexdump_ascii(MSG_DEBUG, "P2P: Matching Bonjour service",
1492 wpabuf_head(bsrv->resp),
1493 wpabuf_len(bsrv->resp));
1494 /* Response Data */
1495 wpabuf_put_buf(resp, bsrv->query); /* Key */
1496 wpabuf_put_buf(resp, bsrv->resp); /* Value */
1497 WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos -
1498 2);
1499 }
1500}
1501
1502
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001503static int match_bonjour_query(struct p2p_srv_bonjour *bsrv, const u8 *query,
1504 size_t query_len)
1505{
1506 char str_rx[256], str_srv[256];
1507
1508 if (query_len < 3 || wpabuf_len(bsrv->query) < 3)
1509 return 0; /* Too short to include DNS Type and Version */
1510 if (os_memcmp(query + query_len - 3,
1511 wpabuf_head_u8(bsrv->query) + wpabuf_len(bsrv->query) - 3,
1512 3) != 0)
1513 return 0; /* Mismatch in DNS Type or Version */
1514 if (query_len == wpabuf_len(bsrv->query) &&
1515 os_memcmp(query, wpabuf_head(bsrv->query), query_len - 3) == 0)
1516 return 1; /* Binary match */
1517
1518 if (p2p_sd_dns_uncompress(str_rx, sizeof(str_rx), query, query_len - 3,
1519 0))
1520 return 0; /* Failed to uncompress query */
1521 if (p2p_sd_dns_uncompress(str_srv, sizeof(str_srv),
1522 wpabuf_head(bsrv->query),
1523 wpabuf_len(bsrv->query) - 3, 0))
1524 return 0; /* Failed to uncompress service */
1525
1526 return os_strcmp(str_rx, str_srv) == 0;
1527}
1528
1529
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001530static void wpas_sd_req_bonjour(struct wpa_supplicant *wpa_s,
1531 struct wpabuf *resp, u8 srv_trans_id,
1532 const u8 *query, size_t query_len)
1533{
1534 struct p2p_srv_bonjour *bsrv;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001535 u8 *len_pos;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001536 int matches = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001537
1538 wpa_hexdump_ascii(MSG_DEBUG, "P2P: SD Request for Bonjour",
1539 query, query_len);
1540 if (dl_list_empty(&wpa_s->global->p2p_srv_bonjour)) {
1541 wpa_printf(MSG_DEBUG, "P2P: Bonjour protocol not available");
1542 wpas_sd_add_proto_not_avail(resp, P2P_SERV_BONJOUR,
1543 srv_trans_id);
1544 return;
1545 }
1546
1547 if (query_len == 0) {
1548 wpas_sd_all_bonjour(wpa_s, resp, srv_trans_id);
1549 return;
1550 }
1551
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001552 dl_list_for_each(bsrv, &wpa_s->global->p2p_srv_bonjour,
1553 struct p2p_srv_bonjour, list) {
1554 if (!match_bonjour_query(bsrv, query, query_len))
1555 continue;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001556
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001557 if (wpabuf_tailroom(resp) <
1558 5 + query_len + wpabuf_len(bsrv->resp))
1559 return;
1560
1561 matches++;
1562
1563 /* Length (to be filled) */
1564 len_pos = wpabuf_put(resp, 2);
1565 wpabuf_put_u8(resp, P2P_SERV_BONJOUR);
1566 wpabuf_put_u8(resp, srv_trans_id);
1567
1568 /* Status Code */
1569 wpabuf_put_u8(resp, P2P_SD_SUCCESS);
1570 wpa_hexdump_ascii(MSG_DEBUG, "P2P: Matching Bonjour service",
1571 wpabuf_head(bsrv->resp),
1572 wpabuf_len(bsrv->resp));
1573
1574 /* Response Data */
1575 wpabuf_put_data(resp, query, query_len); /* Key */
1576 wpabuf_put_buf(resp, bsrv->resp); /* Value */
1577
1578 WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos - 2);
1579 }
1580
1581 if (matches == 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001582 wpa_printf(MSG_DEBUG, "P2P: Requested Bonjour service not "
1583 "available");
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001584 if (wpabuf_tailroom(resp) < 5)
1585 return;
1586
1587 /* Length (to be filled) */
1588 len_pos = wpabuf_put(resp, 2);
1589 wpabuf_put_u8(resp, P2P_SERV_BONJOUR);
1590 wpabuf_put_u8(resp, srv_trans_id);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001591
1592 /* Status Code */
1593 wpabuf_put_u8(resp, P2P_SD_REQUESTED_INFO_NOT_AVAILABLE);
1594 /* Response Data: empty */
1595 WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos -
1596 2);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001597 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001598}
1599
1600
1601static void wpas_sd_all_upnp(struct wpa_supplicant *wpa_s,
1602 struct wpabuf *resp, u8 srv_trans_id)
1603{
1604 struct p2p_srv_upnp *usrv;
1605 u8 *len_pos;
1606
1607 wpa_printf(MSG_DEBUG, "P2P: SD Request for all UPnP services");
1608
1609 if (dl_list_empty(&wpa_s->global->p2p_srv_upnp)) {
1610 wpa_printf(MSG_DEBUG, "P2P: UPnP protocol not available");
1611 return;
1612 }
1613
1614 dl_list_for_each(usrv, &wpa_s->global->p2p_srv_upnp,
1615 struct p2p_srv_upnp, list) {
1616 if (wpabuf_tailroom(resp) < 5 + 1 + os_strlen(usrv->service))
1617 return;
1618
1619 /* Length (to be filled) */
1620 len_pos = wpabuf_put(resp, 2);
1621 wpabuf_put_u8(resp, P2P_SERV_UPNP);
1622 wpabuf_put_u8(resp, srv_trans_id);
1623
1624 /* Status Code */
1625 wpabuf_put_u8(resp, P2P_SD_SUCCESS);
1626 /* Response Data */
1627 wpabuf_put_u8(resp, usrv->version);
1628 wpa_printf(MSG_DEBUG, "P2P: Matching UPnP Service: %s",
1629 usrv->service);
1630 wpabuf_put_str(resp, usrv->service);
1631 WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos -
1632 2);
1633 }
1634}
1635
1636
1637static void wpas_sd_req_upnp(struct wpa_supplicant *wpa_s,
1638 struct wpabuf *resp, u8 srv_trans_id,
1639 const u8 *query, size_t query_len)
1640{
1641 struct p2p_srv_upnp *usrv;
1642 u8 *len_pos;
1643 u8 version;
1644 char *str;
1645 int count = 0;
1646
1647 wpa_hexdump_ascii(MSG_DEBUG, "P2P: SD Request for UPnP",
1648 query, query_len);
1649
1650 if (dl_list_empty(&wpa_s->global->p2p_srv_upnp)) {
1651 wpa_printf(MSG_DEBUG, "P2P: UPnP protocol not available");
1652 wpas_sd_add_proto_not_avail(resp, P2P_SERV_UPNP,
1653 srv_trans_id);
1654 return;
1655 }
1656
1657 if (query_len == 0) {
1658 wpas_sd_all_upnp(wpa_s, resp, srv_trans_id);
1659 return;
1660 }
1661
1662 if (wpabuf_tailroom(resp) < 5)
1663 return;
1664
1665 /* Length (to be filled) */
1666 len_pos = wpabuf_put(resp, 2);
1667 wpabuf_put_u8(resp, P2P_SERV_UPNP);
1668 wpabuf_put_u8(resp, srv_trans_id);
1669
1670 version = query[0];
1671 str = os_malloc(query_len);
1672 if (str == NULL)
1673 return;
1674 os_memcpy(str, query + 1, query_len - 1);
1675 str[query_len - 1] = '\0';
1676
1677 dl_list_for_each(usrv, &wpa_s->global->p2p_srv_upnp,
1678 struct p2p_srv_upnp, list) {
1679 if (version != usrv->version)
1680 continue;
1681
1682 if (os_strcmp(str, "ssdp:all") != 0 &&
1683 os_strstr(usrv->service, str) == NULL)
1684 continue;
1685
1686 if (wpabuf_tailroom(resp) < 2)
1687 break;
1688 if (count == 0) {
1689 /* Status Code */
1690 wpabuf_put_u8(resp, P2P_SD_SUCCESS);
1691 /* Response Data */
1692 wpabuf_put_u8(resp, version);
1693 } else
1694 wpabuf_put_u8(resp, ',');
1695
1696 count++;
1697
1698 wpa_printf(MSG_DEBUG, "P2P: Matching UPnP Service: %s",
1699 usrv->service);
1700 if (wpabuf_tailroom(resp) < os_strlen(usrv->service))
1701 break;
1702 wpabuf_put_str(resp, usrv->service);
1703 }
1704 os_free(str);
1705
1706 if (count == 0) {
1707 wpa_printf(MSG_DEBUG, "P2P: Requested UPnP service not "
1708 "available");
1709 /* Status Code */
1710 wpabuf_put_u8(resp, P2P_SD_REQUESTED_INFO_NOT_AVAILABLE);
1711 /* Response Data: empty */
1712 }
1713
1714 WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos - 2);
1715}
1716
1717
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001718#ifdef CONFIG_WIFI_DISPLAY
1719static void wpas_sd_req_wfd(struct wpa_supplicant *wpa_s,
1720 struct wpabuf *resp, u8 srv_trans_id,
1721 const u8 *query, size_t query_len)
1722{
1723 const u8 *pos;
1724 u8 role;
1725 u8 *len_pos;
1726
1727 wpa_hexdump(MSG_DEBUG, "P2P: SD Request for WFD", query, query_len);
1728
1729 if (!wpa_s->global->wifi_display) {
1730 wpa_printf(MSG_DEBUG, "P2P: WFD protocol not available");
1731 wpas_sd_add_proto_not_avail(resp, P2P_SERV_WIFI_DISPLAY,
1732 srv_trans_id);
1733 return;
1734 }
1735
1736 if (query_len < 1) {
1737 wpa_printf(MSG_DEBUG, "P2P: Missing WFD Requested Device "
1738 "Role");
1739 return;
1740 }
1741
1742 if (wpabuf_tailroom(resp) < 5)
1743 return;
1744
1745 pos = query;
1746 role = *pos++;
1747 wpa_printf(MSG_DEBUG, "P2P: WSD for device role 0x%x", role);
1748
1749 /* TODO: role specific handling */
1750
1751 /* Length (to be filled) */
1752 len_pos = wpabuf_put(resp, 2);
1753 wpabuf_put_u8(resp, P2P_SERV_WIFI_DISPLAY);
1754 wpabuf_put_u8(resp, srv_trans_id);
1755 wpabuf_put_u8(resp, P2P_SD_SUCCESS); /* Status Code */
1756
1757 while (pos < query + query_len) {
1758 if (*pos < MAX_WFD_SUBELEMS &&
1759 wpa_s->global->wfd_subelem[*pos] &&
1760 wpabuf_tailroom(resp) >=
1761 wpabuf_len(wpa_s->global->wfd_subelem[*pos])) {
1762 wpa_printf(MSG_DEBUG, "P2P: Add WSD response "
1763 "subelement %u", *pos);
1764 wpabuf_put_buf(resp, wpa_s->global->wfd_subelem[*pos]);
1765 }
1766 pos++;
1767 }
1768
1769 WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos - 2);
1770}
1771#endif /* CONFIG_WIFI_DISPLAY */
1772
1773
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001774void wpas_sd_request(void *ctx, int freq, const u8 *sa, u8 dialog_token,
1775 u16 update_indic, const u8 *tlvs, size_t tlvs_len)
1776{
1777 struct wpa_supplicant *wpa_s = ctx;
1778 const u8 *pos = tlvs;
1779 const u8 *end = tlvs + tlvs_len;
1780 const u8 *tlv_end;
1781 u16 slen;
1782 struct wpabuf *resp;
1783 u8 srv_proto, srv_trans_id;
1784 size_t buf_len;
1785 char *buf;
1786
1787 wpa_hexdump(MSG_MSGDUMP, "P2P: Service Discovery Request TLVs",
1788 tlvs, tlvs_len);
1789 buf_len = 2 * tlvs_len + 1;
1790 buf = os_malloc(buf_len);
1791 if (buf) {
1792 wpa_snprintf_hex(buf, buf_len, tlvs, tlvs_len);
1793 wpa_msg_ctrl(wpa_s, MSG_INFO, P2P_EVENT_SERV_DISC_REQ "%d "
1794 MACSTR " %u %u %s",
1795 freq, MAC2STR(sa), dialog_token, update_indic,
1796 buf);
1797 os_free(buf);
1798 }
1799
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001800 if (wpa_s->p2p_sd_over_ctrl_iface) {
1801 wpas_notify_p2p_sd_request(wpa_s, freq, sa, dialog_token,
1802 update_indic, tlvs, tlvs_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001803 return; /* to be processed by an external program */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001804 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001805
1806 resp = wpabuf_alloc(10000);
1807 if (resp == NULL)
1808 return;
1809
1810 while (pos + 1 < end) {
1811 wpa_printf(MSG_DEBUG, "P2P: Service Request TLV");
1812 slen = WPA_GET_LE16(pos);
1813 pos += 2;
1814 if (pos + slen > end || slen < 2) {
1815 wpa_printf(MSG_DEBUG, "P2P: Unexpected Query Data "
1816 "length");
1817 wpabuf_free(resp);
1818 return;
1819 }
1820 tlv_end = pos + slen;
1821
1822 srv_proto = *pos++;
1823 wpa_printf(MSG_DEBUG, "P2P: Service Protocol Type %u",
1824 srv_proto);
1825 srv_trans_id = *pos++;
1826 wpa_printf(MSG_DEBUG, "P2P: Service Transaction ID %u",
1827 srv_trans_id);
1828
1829 wpa_hexdump(MSG_MSGDUMP, "P2P: Query Data",
1830 pos, tlv_end - pos);
1831
1832
1833 if (wpa_s->force_long_sd) {
1834 wpa_printf(MSG_DEBUG, "P2P: SD test - force long "
1835 "response");
1836 wpas_sd_all_bonjour(wpa_s, resp, srv_trans_id);
1837 wpas_sd_all_upnp(wpa_s, resp, srv_trans_id);
1838 goto done;
1839 }
1840
1841 switch (srv_proto) {
1842 case P2P_SERV_ALL_SERVICES:
1843 wpa_printf(MSG_DEBUG, "P2P: Service Discovery Request "
1844 "for all services");
1845 if (dl_list_empty(&wpa_s->global->p2p_srv_upnp) &&
1846 dl_list_empty(&wpa_s->global->p2p_srv_bonjour)) {
1847 wpa_printf(MSG_DEBUG, "P2P: No service "
1848 "discovery protocols available");
1849 wpas_sd_add_proto_not_avail(
1850 resp, P2P_SERV_ALL_SERVICES,
1851 srv_trans_id);
1852 break;
1853 }
1854 wpas_sd_all_bonjour(wpa_s, resp, srv_trans_id);
1855 wpas_sd_all_upnp(wpa_s, resp, srv_trans_id);
1856 break;
1857 case P2P_SERV_BONJOUR:
1858 wpas_sd_req_bonjour(wpa_s, resp, srv_trans_id,
1859 pos, tlv_end - pos);
1860 break;
1861 case P2P_SERV_UPNP:
1862 wpas_sd_req_upnp(wpa_s, resp, srv_trans_id,
1863 pos, tlv_end - pos);
1864 break;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001865#ifdef CONFIG_WIFI_DISPLAY
1866 case P2P_SERV_WIFI_DISPLAY:
1867 wpas_sd_req_wfd(wpa_s, resp, srv_trans_id,
1868 pos, tlv_end - pos);
1869 break;
1870#endif /* CONFIG_WIFI_DISPLAY */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001871 default:
1872 wpa_printf(MSG_DEBUG, "P2P: Unavailable service "
1873 "protocol %u", srv_proto);
1874 wpas_sd_add_proto_not_avail(resp, srv_proto,
1875 srv_trans_id);
1876 break;
1877 }
1878
1879 pos = tlv_end;
1880 }
1881
1882done:
1883 wpas_notify_p2p_sd_request(wpa_s, freq, sa, dialog_token,
1884 update_indic, tlvs, tlvs_len);
1885
1886 wpas_p2p_sd_response(wpa_s, freq, sa, dialog_token, resp);
1887
1888 wpabuf_free(resp);
1889}
1890
1891
1892void wpas_sd_response(void *ctx, const u8 *sa, u16 update_indic,
1893 const u8 *tlvs, size_t tlvs_len)
1894{
1895 struct wpa_supplicant *wpa_s = ctx;
1896 const u8 *pos = tlvs;
1897 const u8 *end = tlvs + tlvs_len;
1898 const u8 *tlv_end;
1899 u16 slen;
1900 size_t buf_len;
1901 char *buf;
1902
1903 wpa_hexdump(MSG_MSGDUMP, "P2P: Service Discovery Response TLVs",
1904 tlvs, tlvs_len);
1905 if (tlvs_len > 1500) {
1906 /* TODO: better way for handling this */
1907 wpa_msg_ctrl(wpa_s, MSG_INFO,
1908 P2P_EVENT_SERV_DISC_RESP MACSTR
1909 " %u <long response: %u bytes>",
1910 MAC2STR(sa), update_indic,
1911 (unsigned int) tlvs_len);
1912 } else {
1913 buf_len = 2 * tlvs_len + 1;
1914 buf = os_malloc(buf_len);
1915 if (buf) {
1916 wpa_snprintf_hex(buf, buf_len, tlvs, tlvs_len);
1917 wpa_msg_ctrl(wpa_s, MSG_INFO,
1918 P2P_EVENT_SERV_DISC_RESP MACSTR " %u %s",
1919 MAC2STR(sa), update_indic, buf);
1920 os_free(buf);
1921 }
1922 }
1923
1924 while (pos < end) {
1925 u8 srv_proto, srv_trans_id, status;
1926
1927 wpa_printf(MSG_DEBUG, "P2P: Service Response TLV");
1928 slen = WPA_GET_LE16(pos);
1929 pos += 2;
1930 if (pos + slen > end || slen < 3) {
1931 wpa_printf(MSG_DEBUG, "P2P: Unexpected Response Data "
1932 "length");
1933 return;
1934 }
1935 tlv_end = pos + slen;
1936
1937 srv_proto = *pos++;
1938 wpa_printf(MSG_DEBUG, "P2P: Service Protocol Type %u",
1939 srv_proto);
1940 srv_trans_id = *pos++;
1941 wpa_printf(MSG_DEBUG, "P2P: Service Transaction ID %u",
1942 srv_trans_id);
1943 status = *pos++;
1944 wpa_printf(MSG_DEBUG, "P2P: Status Code ID %u",
1945 status);
1946
1947 wpa_hexdump(MSG_MSGDUMP, "P2P: Response Data",
1948 pos, tlv_end - pos);
1949
1950 pos = tlv_end;
1951 }
1952
1953 wpas_notify_p2p_sd_response(wpa_s, sa, update_indic, tlvs, tlvs_len);
1954}
1955
1956
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001957u64 wpas_p2p_sd_request(struct wpa_supplicant *wpa_s, const u8 *dst,
1958 const struct wpabuf *tlvs)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001959{
1960 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001961 return wpa_drv_p2p_sd_request(wpa_s, dst, tlvs);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001962 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001963 return 0;
1964 return (uintptr_t) p2p_sd_request(wpa_s->global->p2p, dst, tlvs);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001965}
1966
1967
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001968u64 wpas_p2p_sd_request_upnp(struct wpa_supplicant *wpa_s, const u8 *dst,
1969 u8 version, const char *query)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001970{
1971 struct wpabuf *tlvs;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001972 u64 ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001973
1974 tlvs = wpabuf_alloc(2 + 1 + 1 + 1 + os_strlen(query));
1975 if (tlvs == NULL)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001976 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001977 wpabuf_put_le16(tlvs, 1 + 1 + 1 + os_strlen(query));
1978 wpabuf_put_u8(tlvs, P2P_SERV_UPNP); /* Service Protocol Type */
1979 wpabuf_put_u8(tlvs, 1); /* Service Transaction ID */
1980 wpabuf_put_u8(tlvs, version);
1981 wpabuf_put_str(tlvs, query);
1982 ret = wpas_p2p_sd_request(wpa_s, dst, tlvs);
1983 wpabuf_free(tlvs);
1984 return ret;
1985}
1986
1987
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001988#ifdef CONFIG_WIFI_DISPLAY
1989
1990static u64 wpas_p2p_sd_request_wfd(struct wpa_supplicant *wpa_s, const u8 *dst,
1991 const struct wpabuf *tlvs)
1992{
1993 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
1994 return 0;
1995 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
1996 return 0;
1997 return (uintptr_t) p2p_sd_request_wfd(wpa_s->global->p2p, dst, tlvs);
1998}
1999
2000
2001#define MAX_WFD_SD_SUBELEMS 20
2002
2003static void wfd_add_sd_req_role(struct wpabuf *tlvs, u8 id, u8 role,
2004 const char *subelems)
2005{
2006 u8 *len;
2007 const char *pos;
2008 int val;
2009 int count = 0;
2010
2011 len = wpabuf_put(tlvs, 2);
2012 wpabuf_put_u8(tlvs, P2P_SERV_WIFI_DISPLAY); /* Service Protocol Type */
2013 wpabuf_put_u8(tlvs, id); /* Service Transaction ID */
2014
2015 wpabuf_put_u8(tlvs, role);
2016
2017 pos = subelems;
2018 while (*pos) {
2019 val = atoi(pos);
2020 if (val >= 0 && val < 256) {
2021 wpabuf_put_u8(tlvs, val);
2022 count++;
2023 if (count == MAX_WFD_SD_SUBELEMS)
2024 break;
2025 }
2026 pos = os_strchr(pos + 1, ',');
2027 if (pos == NULL)
2028 break;
2029 pos++;
2030 }
2031
2032 WPA_PUT_LE16(len, (u8 *) wpabuf_put(tlvs, 0) - len - 2);
2033}
2034
2035
2036u64 wpas_p2p_sd_request_wifi_display(struct wpa_supplicant *wpa_s,
2037 const u8 *dst, const char *role)
2038{
2039 struct wpabuf *tlvs;
2040 u64 ret;
2041 const char *subelems;
2042 u8 id = 1;
2043
2044 subelems = os_strchr(role, ' ');
2045 if (subelems == NULL)
2046 return 0;
2047 subelems++;
2048
2049 tlvs = wpabuf_alloc(4 * (2 + 1 + 1 + 1 + MAX_WFD_SD_SUBELEMS));
2050 if (tlvs == NULL)
2051 return 0;
2052
2053 if (os_strstr(role, "[source]"))
2054 wfd_add_sd_req_role(tlvs, id++, 0x00, subelems);
2055 if (os_strstr(role, "[pri-sink]"))
2056 wfd_add_sd_req_role(tlvs, id++, 0x01, subelems);
2057 if (os_strstr(role, "[sec-sink]"))
2058 wfd_add_sd_req_role(tlvs, id++, 0x02, subelems);
2059 if (os_strstr(role, "[source+sink]"))
2060 wfd_add_sd_req_role(tlvs, id++, 0x03, subelems);
2061
2062 ret = wpas_p2p_sd_request_wfd(wpa_s, dst, tlvs);
2063 wpabuf_free(tlvs);
2064 return ret;
2065}
2066
2067#endif /* CONFIG_WIFI_DISPLAY */
2068
2069
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002070int wpas_p2p_sd_cancel_request(struct wpa_supplicant *wpa_s, u64 req)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002071{
2072 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002073 return wpa_drv_p2p_sd_cancel_request(wpa_s, req);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002074 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
2075 return -1;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002076 return p2p_sd_cancel_request(wpa_s->global->p2p,
2077 (void *) (uintptr_t) req);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002078}
2079
2080
2081void wpas_p2p_sd_response(struct wpa_supplicant *wpa_s, int freq,
2082 const u8 *dst, u8 dialog_token,
2083 const struct wpabuf *resp_tlvs)
2084{
2085 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT) {
2086 wpa_drv_p2p_sd_response(wpa_s, freq, dst, dialog_token,
2087 resp_tlvs);
2088 return;
2089 }
2090 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
2091 return;
2092 p2p_sd_response(wpa_s->global->p2p, freq, dst, dialog_token,
2093 resp_tlvs);
2094}
2095
Dmitry Shmidtb5e8f062012-08-08 10:56:33 -07002096#ifdef ANDROID_P2P
2097void wpas_p2p_sd_service_update(struct wpa_supplicant *wpa_s, int action)
2098#else
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002099void wpas_p2p_sd_service_update(struct wpa_supplicant *wpa_s)
Dmitry Shmidtb5e8f062012-08-08 10:56:33 -07002100#endif
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002101{
2102 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT) {
2103 wpa_drv_p2p_service_update(wpa_s);
2104 return;
2105 }
2106 if (wpa_s->global->p2p)
Dmitry Shmidtb5e8f062012-08-08 10:56:33 -07002107#ifdef ANDROID_P2P
2108 p2p_sd_service_update(wpa_s->global->p2p, action);
2109#else
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002110 p2p_sd_service_update(wpa_s->global->p2p);
Dmitry Shmidtb5e8f062012-08-08 10:56:33 -07002111#endif
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002112}
2113
2114
2115static void wpas_p2p_srv_bonjour_free(struct p2p_srv_bonjour *bsrv)
2116{
2117 dl_list_del(&bsrv->list);
2118 wpabuf_free(bsrv->query);
2119 wpabuf_free(bsrv->resp);
2120 os_free(bsrv);
2121}
2122
2123
2124static void wpas_p2p_srv_upnp_free(struct p2p_srv_upnp *usrv)
2125{
2126 dl_list_del(&usrv->list);
2127 os_free(usrv->service);
2128 os_free(usrv);
2129}
2130
2131
2132void wpas_p2p_service_flush(struct wpa_supplicant *wpa_s)
2133{
2134 struct p2p_srv_bonjour *bsrv, *bn;
2135 struct p2p_srv_upnp *usrv, *un;
2136
2137 dl_list_for_each_safe(bsrv, bn, &wpa_s->global->p2p_srv_bonjour,
2138 struct p2p_srv_bonjour, list)
2139 wpas_p2p_srv_bonjour_free(bsrv);
2140
2141 dl_list_for_each_safe(usrv, un, &wpa_s->global->p2p_srv_upnp,
2142 struct p2p_srv_upnp, list)
2143 wpas_p2p_srv_upnp_free(usrv);
2144
Dmitry Shmidtb5e8f062012-08-08 10:56:33 -07002145#ifdef ANDROID_P2P
2146 wpas_p2p_sd_service_update(wpa_s, SRV_FLUSH);
2147#else
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002148 wpas_p2p_sd_service_update(wpa_s);
Dmitry Shmidtb5e8f062012-08-08 10:56:33 -07002149#endif
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002150}
2151
2152
2153int wpas_p2p_service_add_bonjour(struct wpa_supplicant *wpa_s,
2154 struct wpabuf *query, struct wpabuf *resp)
2155{
2156 struct p2p_srv_bonjour *bsrv;
2157
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002158 bsrv = os_zalloc(sizeof(*bsrv));
2159 if (bsrv == NULL)
2160 return -1;
2161 bsrv->query = query;
2162 bsrv->resp = resp;
2163 dl_list_add(&wpa_s->global->p2p_srv_bonjour, &bsrv->list);
2164
Dmitry Shmidtb5e8f062012-08-08 10:56:33 -07002165#ifdef ANDROID_P2P
2166 wpas_p2p_sd_service_update(wpa_s, SRV_ADD);
2167#else
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002168 wpas_p2p_sd_service_update(wpa_s);
Dmitry Shmidtb5e8f062012-08-08 10:56:33 -07002169#endif
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002170 return 0;
2171}
2172
2173
2174int wpas_p2p_service_del_bonjour(struct wpa_supplicant *wpa_s,
2175 const struct wpabuf *query)
2176{
2177 struct p2p_srv_bonjour *bsrv;
2178
2179 bsrv = wpas_p2p_service_get_bonjour(wpa_s, query);
2180 if (bsrv == NULL)
2181 return -1;
2182 wpas_p2p_srv_bonjour_free(bsrv);
Dmitry Shmidtb5e8f062012-08-08 10:56:33 -07002183#ifdef ANDROID_P2P
2184 wpas_p2p_sd_service_update(wpa_s, SRV_DEL);
2185#else
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002186 wpas_p2p_sd_service_update(wpa_s);
Dmitry Shmidtb5e8f062012-08-08 10:56:33 -07002187#endif
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002188 return 0;
2189}
2190
2191
2192int wpas_p2p_service_add_upnp(struct wpa_supplicant *wpa_s, u8 version,
2193 const char *service)
2194{
2195 struct p2p_srv_upnp *usrv;
2196
2197 if (wpas_p2p_service_get_upnp(wpa_s, version, service))
2198 return 0; /* Already listed */
2199 usrv = os_zalloc(sizeof(*usrv));
2200 if (usrv == NULL)
2201 return -1;
2202 usrv->version = version;
2203 usrv->service = os_strdup(service);
2204 if (usrv->service == NULL) {
2205 os_free(usrv);
2206 return -1;
2207 }
2208 dl_list_add(&wpa_s->global->p2p_srv_upnp, &usrv->list);
2209
Dmitry Shmidtb5e8f062012-08-08 10:56:33 -07002210#ifdef ANDROID_P2P
2211 wpas_p2p_sd_service_update(wpa_s, SRV_ADD);
2212#else
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002213 wpas_p2p_sd_service_update(wpa_s);
Dmitry Shmidtb5e8f062012-08-08 10:56:33 -07002214#endif
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002215 return 0;
2216}
2217
2218
2219int wpas_p2p_service_del_upnp(struct wpa_supplicant *wpa_s, u8 version,
2220 const char *service)
2221{
2222 struct p2p_srv_upnp *usrv;
2223
2224 usrv = wpas_p2p_service_get_upnp(wpa_s, version, service);
2225 if (usrv == NULL)
2226 return -1;
2227 wpas_p2p_srv_upnp_free(usrv);
Dmitry Shmidtb5e8f062012-08-08 10:56:33 -07002228#ifdef ANDROID_P2P
2229 wpas_p2p_sd_service_update(wpa_s, SRV_DEL);
2230#else
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002231 wpas_p2p_sd_service_update(wpa_s);
Dmitry Shmidtb5e8f062012-08-08 10:56:33 -07002232#endif
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002233 return 0;
2234}
2235
2236
2237static void wpas_prov_disc_local_display(struct wpa_supplicant *wpa_s,
2238 const u8 *peer, const char *params,
2239 unsigned int generated_pin)
2240{
2241 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_SHOW_PIN MACSTR " %08d%s",
2242 MAC2STR(peer), generated_pin, params);
2243}
2244
2245
2246static void wpas_prov_disc_local_keypad(struct wpa_supplicant *wpa_s,
2247 const u8 *peer, const char *params)
2248{
2249 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_ENTER_PIN MACSTR "%s",
2250 MAC2STR(peer), params);
2251}
2252
2253
2254void wpas_prov_disc_req(void *ctx, const u8 *peer, u16 config_methods,
2255 const u8 *dev_addr, const u8 *pri_dev_type,
2256 const char *dev_name, u16 supp_config_methods,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002257 u8 dev_capab, u8 group_capab, const u8 *group_id,
2258 size_t group_id_len)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002259{
2260 struct wpa_supplicant *wpa_s = ctx;
2261 char devtype[WPS_DEV_TYPE_BUFSIZE];
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002262 char params[300];
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002263 u8 empty_dev_type[8];
2264 unsigned int generated_pin = 0;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002265 struct wpa_supplicant *group = NULL;
2266
2267 if (group_id) {
2268 for (group = wpa_s->global->ifaces; group; group = group->next)
2269 {
2270 struct wpa_ssid *s = group->current_ssid;
2271 if (s != NULL &&
2272 s->mode == WPAS_MODE_P2P_GO &&
2273 group_id_len - ETH_ALEN == s->ssid_len &&
2274 os_memcmp(group_id + ETH_ALEN, s->ssid,
2275 s->ssid_len) == 0)
2276 break;
2277 }
2278 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002279
2280 if (pri_dev_type == NULL) {
2281 os_memset(empty_dev_type, 0, sizeof(empty_dev_type));
2282 pri_dev_type = empty_dev_type;
2283 }
2284 os_snprintf(params, sizeof(params), " p2p_dev_addr=" MACSTR
2285 " pri_dev_type=%s name='%s' config_methods=0x%x "
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002286 "dev_capab=0x%x group_capab=0x%x%s%s",
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002287 MAC2STR(dev_addr),
2288 wps_dev_type_bin2str(pri_dev_type, devtype,
2289 sizeof(devtype)),
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002290 dev_name, supp_config_methods, dev_capab, group_capab,
2291 group ? " group=" : "",
2292 group ? group->ifname : "");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002293 params[sizeof(params) - 1] = '\0';
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08002294
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002295 if (config_methods & WPS_CONFIG_DISPLAY) {
2296 generated_pin = wps_generate_pin();
2297 wpas_prov_disc_local_display(wpa_s, peer, params,
2298 generated_pin);
2299 } else if (config_methods & WPS_CONFIG_KEYPAD)
2300 wpas_prov_disc_local_keypad(wpa_s, peer, params);
2301 else if (config_methods & WPS_CONFIG_PUSHBUTTON)
2302 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_PBC_REQ MACSTR
2303 "%s", MAC2STR(peer), params);
Jouni Malinen75ecf522011-06-27 15:19:46 -07002304
2305 wpas_notify_p2p_provision_discovery(wpa_s, peer, 1 /* request */,
2306 P2P_PROV_DISC_SUCCESS,
2307 config_methods, generated_pin);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002308}
2309
2310
2311void wpas_prov_disc_resp(void *ctx, const u8 *peer, u16 config_methods)
2312{
2313 struct wpa_supplicant *wpa_s = ctx;
2314 unsigned int generated_pin = 0;
Dmitry Shmidt04949592012-07-19 12:16:46 -07002315 char params[20];
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002316
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08002317 if (wpa_s->pending_pd_before_join &&
2318 (os_memcmp(peer, wpa_s->pending_join_dev_addr, ETH_ALEN) == 0 ||
2319 os_memcmp(peer, wpa_s->pending_join_iface_addr, ETH_ALEN) == 0)) {
2320 wpa_s->pending_pd_before_join = 0;
2321 wpa_printf(MSG_DEBUG, "P2P: Starting pending "
2322 "join-existing-group operation");
2323 wpas_p2p_join_start(wpa_s);
2324 return;
2325 }
2326
Dmitry Shmidt04949592012-07-19 12:16:46 -07002327 if (wpa_s->pending_pd_use == AUTO_PD_JOIN ||
2328 wpa_s->pending_pd_use == AUTO_PD_GO_NEG)
2329 os_snprintf(params, sizeof(params), " peer_go=%d",
2330 wpa_s->pending_pd_use == AUTO_PD_JOIN);
2331 else
2332 params[0] = '\0';
2333
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002334 if (config_methods & WPS_CONFIG_DISPLAY)
Dmitry Shmidt04949592012-07-19 12:16:46 -07002335 wpas_prov_disc_local_keypad(wpa_s, peer, params);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002336 else if (config_methods & WPS_CONFIG_KEYPAD) {
2337 generated_pin = wps_generate_pin();
Dmitry Shmidt04949592012-07-19 12:16:46 -07002338 wpas_prov_disc_local_display(wpa_s, peer, params,
2339 generated_pin);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002340 } else if (config_methods & WPS_CONFIG_PUSHBUTTON)
Dmitry Shmidt04949592012-07-19 12:16:46 -07002341 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_PBC_RESP MACSTR
2342 "%s", MAC2STR(peer), params);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002343
Jouni Malinen75ecf522011-06-27 15:19:46 -07002344 wpas_notify_p2p_provision_discovery(wpa_s, peer, 0 /* response */,
2345 P2P_PROV_DISC_SUCCESS,
2346 config_methods, generated_pin);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002347}
2348
2349
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002350static void wpas_prov_disc_fail(void *ctx, const u8 *peer,
2351 enum p2p_prov_disc_status status)
Jouni Malinen75ecf522011-06-27 15:19:46 -07002352{
2353 struct wpa_supplicant *wpa_s = ctx;
2354
Dmitry Shmidt04949592012-07-19 12:16:46 -07002355 if (wpa_s->p2p_fallback_to_go_neg) {
2356 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: PD for p2p_connect-auto "
2357 "failed - fall back to GO Negotiation");
2358 wpas_p2p_fallback_to_go_neg(wpa_s, 0);
2359 return;
2360 }
2361
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002362 if (status == P2P_PROV_DISC_TIMEOUT_JOIN) {
Dmitry Shmidt2b380482012-09-13 10:52:13 -07002363 wpa_s->pending_pd_before_join = 0;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002364 wpa_printf(MSG_DEBUG, "P2P: Starting pending "
2365 "join-existing-group operation (no ACK for PD "
2366 "Req attempts)");
2367 wpas_p2p_join_start(wpa_s);
2368 return;
Dmitry Shmidt2b380482012-09-13 10:52:13 -07002369 }
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002370
Dmitry Shmidt04949592012-07-19 12:16:46 -07002371 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_FAILURE
2372 " p2p_dev_addr=" MACSTR " status=%d",
2373 MAC2STR(peer), status);
2374
Jouni Malinen75ecf522011-06-27 15:19:46 -07002375 wpas_notify_p2p_provision_discovery(wpa_s, peer, 0 /* response */,
2376 status, 0, 0);
2377}
2378
2379
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002380static u8 wpas_invitation_process(void *ctx, const u8 *sa, const u8 *bssid,
2381 const u8 *go_dev_addr, const u8 *ssid,
2382 size_t ssid_len, int *go, u8 *group_bssid,
2383 int *force_freq, int persistent_group)
2384{
2385 struct wpa_supplicant *wpa_s = ctx;
2386 struct wpa_ssid *s;
2387 u8 cur_bssid[ETH_ALEN];
2388 int res;
2389 struct wpa_supplicant *grp;
2390
2391 if (!persistent_group) {
2392 wpa_printf(MSG_DEBUG, "P2P: Invitation from " MACSTR
2393 " to join an active group", MAC2STR(sa));
2394 if (!is_zero_ether_addr(wpa_s->p2p_auth_invite) &&
2395 (os_memcmp(go_dev_addr, wpa_s->p2p_auth_invite, ETH_ALEN)
2396 == 0 ||
2397 os_memcmp(sa, wpa_s->p2p_auth_invite, ETH_ALEN) == 0)) {
2398 wpa_printf(MSG_DEBUG, "P2P: Accept previously "
2399 "authorized invitation");
2400 goto accept_inv;
2401 }
2402 /*
2403 * Do not accept the invitation automatically; notify user and
2404 * request approval.
2405 */
2406 return P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE;
2407 }
2408
2409 grp = wpas_get_p2p_group(wpa_s, ssid, ssid_len, go);
2410 if (grp) {
2411 wpa_printf(MSG_DEBUG, "P2P: Accept invitation to already "
2412 "running persistent group");
2413 if (*go)
2414 os_memcpy(group_bssid, grp->own_addr, ETH_ALEN);
2415 goto accept_inv;
2416 }
2417
2418 if (!wpa_s->conf->persistent_reconnect)
2419 return P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE;
2420
2421 for (s = wpa_s->conf->ssid; s; s = s->next) {
2422 if (s->disabled == 2 &&
2423 os_memcmp(s->bssid, go_dev_addr, ETH_ALEN) == 0 &&
2424 s->ssid_len == ssid_len &&
2425 os_memcmp(ssid, s->ssid, ssid_len) == 0)
2426 break;
2427 }
2428
2429 if (!s) {
2430 wpa_printf(MSG_DEBUG, "P2P: Invitation from " MACSTR
2431 " requested reinvocation of an unknown group",
2432 MAC2STR(sa));
2433 return P2P_SC_FAIL_UNKNOWN_GROUP;
2434 }
2435
2436 if (s->mode == WPAS_MODE_P2P_GO && !wpas_p2p_create_iface(wpa_s)) {
2437 *go = 1;
2438 if (wpa_s->wpa_state >= WPA_AUTHENTICATING) {
2439 wpa_printf(MSG_DEBUG, "P2P: The only available "
2440 "interface is already in use - reject "
2441 "invitation");
2442 return P2P_SC_FAIL_UNABLE_TO_ACCOMMODATE;
2443 }
2444 os_memcpy(group_bssid, wpa_s->own_addr, ETH_ALEN);
2445 } else if (s->mode == WPAS_MODE_P2P_GO) {
2446 *go = 1;
2447 if (wpas_p2p_add_group_interface(wpa_s, WPA_IF_P2P_GO) < 0)
2448 {
2449 wpa_printf(MSG_ERROR, "P2P: Failed to allocate a new "
2450 "interface address for the group");
2451 return P2P_SC_FAIL_UNABLE_TO_ACCOMMODATE;
2452 }
2453 os_memcpy(group_bssid, wpa_s->pending_interface_addr,
2454 ETH_ALEN);
2455 }
2456
2457accept_inv:
2458 if (wpa_s->current_ssid && wpa_drv_get_bssid(wpa_s, cur_bssid) == 0 &&
2459 wpa_s->assoc_freq) {
2460 wpa_printf(MSG_DEBUG, "P2P: Trying to force channel to match "
2461 "the channel we are already using");
2462 *force_freq = wpa_s->assoc_freq;
2463 }
2464
2465 res = wpa_drv_shared_freq(wpa_s);
2466 if (res > 0) {
2467 wpa_printf(MSG_DEBUG, "P2P: Trying to force channel to match "
2468 "with the channel we are already using on a "
2469 "shared interface");
2470 *force_freq = res;
2471 }
2472
2473 return P2P_SC_SUCCESS;
2474}
2475
2476
2477static void wpas_invitation_received(void *ctx, const u8 *sa, const u8 *bssid,
2478 const u8 *ssid, size_t ssid_len,
2479 const u8 *go_dev_addr, u8 status,
2480 int op_freq)
2481{
2482 struct wpa_supplicant *wpa_s = ctx;
2483 struct wpa_ssid *s;
2484
2485 for (s = wpa_s->conf->ssid; s; s = s->next) {
2486 if (s->disabled == 2 &&
2487 s->ssid_len == ssid_len &&
2488 os_memcmp(ssid, s->ssid, ssid_len) == 0)
2489 break;
2490 }
2491
2492 if (status == P2P_SC_SUCCESS) {
2493 wpa_printf(MSG_DEBUG, "P2P: Invitation from peer " MACSTR
2494 " was accepted; op_freq=%d MHz",
2495 MAC2STR(sa), op_freq);
2496 if (s) {
Dmitry Shmidt91c40cd2012-09-25 14:23:53 -07002497 int go = s->mode == WPAS_MODE_P2P_GO;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002498 wpas_p2p_group_add_persistent(
Dmitry Shmidt91c40cd2012-09-25 14:23:53 -07002499 wpa_s, s, go, go ? op_freq : 0, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002500 } else if (bssid) {
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002501 wpa_s->user_initiated_pd = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002502 wpas_p2p_join(wpa_s, bssid, go_dev_addr,
Dmitry Shmidt04949592012-07-19 12:16:46 -07002503 wpa_s->p2p_wps_method, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002504 }
2505 return;
2506 }
2507
2508 if (status != P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE) {
2509 wpa_printf(MSG_DEBUG, "P2P: Invitation from peer " MACSTR
2510 " was rejected (status %u)", MAC2STR(sa), status);
2511 return;
2512 }
2513
2514 if (!s) {
2515 if (bssid) {
2516 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_INVITATION_RECEIVED
2517 "sa=" MACSTR " go_dev_addr=" MACSTR
2518 " bssid=" MACSTR " unknown-network",
2519 MAC2STR(sa), MAC2STR(go_dev_addr),
2520 MAC2STR(bssid));
2521 } else {
2522 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_INVITATION_RECEIVED
2523 "sa=" MACSTR " go_dev_addr=" MACSTR
2524 " unknown-network",
2525 MAC2STR(sa), MAC2STR(go_dev_addr));
2526 }
2527 return;
2528 }
2529
2530 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_INVITATION_RECEIVED "sa=" MACSTR
2531 " persistent=%d", MAC2STR(sa), s->id);
2532}
2533
2534
2535static void wpas_invitation_result(void *ctx, int status, const u8 *bssid)
2536{
2537 struct wpa_supplicant *wpa_s = ctx;
2538 struct wpa_ssid *ssid;
2539
2540 if (bssid) {
2541 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_INVITATION_RESULT
2542 "status=%d " MACSTR,
2543 status, MAC2STR(bssid));
2544 } else {
2545 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_INVITATION_RESULT
2546 "status=%d ", status);
2547 }
2548 wpas_notify_p2p_invitation_result(wpa_s, status, bssid);
2549
2550 if (wpa_s->pending_invite_ssid_id == -1)
2551 return; /* Invitation to active group */
2552
2553 if (status != P2P_SC_SUCCESS) {
2554 wpas_p2p_remove_pending_group_interface(wpa_s);
2555 return;
2556 }
2557
2558 ssid = wpa_config_get_network(wpa_s->conf,
2559 wpa_s->pending_invite_ssid_id);
2560 if (ssid == NULL) {
2561 wpa_printf(MSG_ERROR, "P2P: Could not find persistent group "
2562 "data matching with invitation");
2563 return;
2564 }
2565
Jouni Malinen2c5b17d2012-10-13 21:52:46 -07002566 /*
2567 * The peer could have missed our ctrl::ack frame for Invitation
2568 * Response and continue retransmitting the frame. To reduce the
2569 * likelihood of the peer not getting successful TX status for the
2570 * Invitation Response frame, wait a short time here before starting
2571 * the persistent group so that we will remain on the current channel to
2572 * acknowledge any possible retransmission from the peer.
2573 */
Irfan Sheriff81931b82012-10-16 21:40:46 -07002574#ifndef ANDROID_P2P
Jouni Malinen2c5b17d2012-10-13 21:52:46 -07002575 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: 50 ms wait on current channel before "
2576 "starting persistent group");
2577 os_sleep(0, 50000);
Irfan Sheriff81931b82012-10-16 21:40:46 -07002578#else
2579 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: 100 ms wait on current channel before "
2580 "starting persistent group");
2581 os_sleep(0, 100000);
2582#endif
Jouni Malinen2c5b17d2012-10-13 21:52:46 -07002583
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002584 wpas_p2p_group_add_persistent(wpa_s, ssid,
Jouni Malinen31be0a42012-08-31 21:20:51 +03002585 ssid->mode == WPAS_MODE_P2P_GO,
2586 wpa_s->p2p_persistent_go_freq,
2587 wpa_s->p2p_go_ht40);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002588}
2589
2590
Dmitry Shmidt04949592012-07-19 12:16:46 -07002591static int wpas_p2p_disallowed_freq(struct wpa_global *global,
2592 unsigned int freq)
2593{
2594 unsigned int i;
2595
2596 if (global->p2p_disallow_freq == NULL)
2597 return 0;
2598
2599 for (i = 0; i < global->num_p2p_disallow_freq; i++) {
2600 if (freq >= global->p2p_disallow_freq[i].min &&
2601 freq <= global->p2p_disallow_freq[i].max)
2602 return 1;
2603 }
2604
2605 return 0;
2606}
2607
2608
2609static void wpas_p2p_add_chan(struct p2p_reg_class *reg, u8 chan)
2610{
2611 reg->channel[reg->channels] = chan;
2612 reg->channels++;
2613}
2614
2615
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002616static int wpas_p2p_default_channels(struct wpa_supplicant *wpa_s,
2617 struct p2p_channels *chan)
2618{
2619 int i, cla = 0;
2620
2621 wpa_printf(MSG_DEBUG, "P2P: Enable operating classes for 2.4 GHz "
2622 "band");
2623
2624 /* Operating class 81 - 2.4 GHz band channels 1..13 */
2625 chan->reg_class[cla].reg_class = 81;
Dmitry Shmidt04949592012-07-19 12:16:46 -07002626 chan->reg_class[cla].channels = 0;
2627 for (i = 0; i < 11; i++) {
2628 if (!wpas_p2p_disallowed_freq(wpa_s->global, 2412 + i * 5))
2629 wpas_p2p_add_chan(&chan->reg_class[cla], i + 1);
2630 }
2631 if (chan->reg_class[cla].channels)
2632 cla++;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002633
2634 wpa_printf(MSG_DEBUG, "P2P: Enable operating classes for lower 5 GHz "
2635 "band");
2636
2637 /* Operating class 115 - 5 GHz, channels 36-48 */
2638 chan->reg_class[cla].reg_class = 115;
Dmitry Shmidt04949592012-07-19 12:16:46 -07002639 chan->reg_class[cla].channels = 0;
2640 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 36 * 5))
2641 wpas_p2p_add_chan(&chan->reg_class[cla], 36);
2642 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 40 * 5))
2643 wpas_p2p_add_chan(&chan->reg_class[cla], 40);
2644 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 44 * 5))
2645 wpas_p2p_add_chan(&chan->reg_class[cla], 44);
2646 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 48 * 5))
2647 wpas_p2p_add_chan(&chan->reg_class[cla], 48);
2648 if (chan->reg_class[cla].channels)
2649 cla++;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002650
2651 wpa_printf(MSG_DEBUG, "P2P: Enable operating classes for higher 5 GHz "
2652 "band");
2653
2654 /* Operating class 124 - 5 GHz, channels 149,153,157,161 */
2655 chan->reg_class[cla].reg_class = 124;
Dmitry Shmidt04949592012-07-19 12:16:46 -07002656 chan->reg_class[cla].channels = 0;
2657 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 149 * 5))
2658 wpas_p2p_add_chan(&chan->reg_class[cla], 149);
2659 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 153 * 5))
2660 wpas_p2p_add_chan(&chan->reg_class[cla], 153);
2661 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 156 * 5))
2662 wpas_p2p_add_chan(&chan->reg_class[cla], 157);
2663 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 161 * 5))
2664 wpas_p2p_add_chan(&chan->reg_class[cla], 161);
2665 if (chan->reg_class[cla].channels)
2666 cla++;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002667
2668 chan->reg_classes = cla;
2669 return 0;
2670}
2671
2672
2673static struct hostapd_hw_modes * get_mode(struct hostapd_hw_modes *modes,
2674 u16 num_modes,
2675 enum hostapd_hw_mode mode)
2676{
2677 u16 i;
2678
2679 for (i = 0; i < num_modes; i++) {
2680 if (modes[i].mode == mode)
2681 return &modes[i];
2682 }
2683
2684 return NULL;
2685}
2686
2687
Dmitry Shmidt04949592012-07-19 12:16:46 -07002688static int has_channel(struct wpa_global *global,
2689 struct hostapd_hw_modes *mode, u8 chan, int *flags)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002690{
2691 int i;
Dmitry Shmidt04949592012-07-19 12:16:46 -07002692 unsigned int freq;
2693
2694 freq = (mode->mode == HOSTAPD_MODE_IEEE80211A ? 5000 : 2407) +
2695 chan * 5;
2696 if (wpas_p2p_disallowed_freq(global, freq))
2697 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002698
2699 for (i = 0; i < mode->num_channels; i++) {
2700 if (mode->channels[i].chan == chan) {
2701 if (flags)
2702 *flags = mode->channels[i].flag;
2703 return !(mode->channels[i].flag &
2704 (HOSTAPD_CHAN_DISABLED |
2705 HOSTAPD_CHAN_PASSIVE_SCAN |
2706 HOSTAPD_CHAN_NO_IBSS |
2707 HOSTAPD_CHAN_RADAR));
2708 }
2709 }
2710
2711 return 0;
2712}
2713
2714
2715struct p2p_oper_class_map {
2716 enum hostapd_hw_mode mode;
2717 u8 op_class;
2718 u8 min_chan;
2719 u8 max_chan;
2720 u8 inc;
2721 enum { BW20, BW40PLUS, BW40MINUS } bw;
2722};
2723
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002724static struct p2p_oper_class_map op_class[] = {
2725 { HOSTAPD_MODE_IEEE80211G, 81, 1, 13, 1, BW20 },
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002726#if 0 /* Do not enable HT40 on 2 GHz for now */
2727 { HOSTAPD_MODE_IEEE80211G, 83, 1, 9, 1, BW40PLUS },
2728 { HOSTAPD_MODE_IEEE80211G, 84, 5, 13, 1, BW40MINUS },
2729#endif
2730 { HOSTAPD_MODE_IEEE80211A, 115, 36, 48, 4, BW20 },
2731 { HOSTAPD_MODE_IEEE80211A, 124, 149, 161, 4, BW20 },
2732 { HOSTAPD_MODE_IEEE80211A, 116, 36, 44, 8, BW40PLUS },
2733 { HOSTAPD_MODE_IEEE80211A, 117, 40, 48, 8, BW40MINUS },
2734 { HOSTAPD_MODE_IEEE80211A, 126, 149, 157, 8, BW40PLUS },
2735 { HOSTAPD_MODE_IEEE80211A, 127, 153, 161, 8, BW40MINUS },
2736 { -1, 0, 0, 0, 0, BW20 }
2737};
2738
2739
2740static int wpas_p2p_verify_channel(struct wpa_supplicant *wpa_s,
2741 struct hostapd_hw_modes *mode,
2742 u8 channel, u8 bw)
2743{
2744 int flag;
2745
2746 if (!has_channel(wpa_s->global, mode, channel, &flag))
2747 return -1;
2748 if (bw == BW40MINUS &&
2749 (!(flag & HOSTAPD_CHAN_HT40MINUS) ||
2750 !has_channel(wpa_s->global, mode, channel - 4, NULL)))
2751 return 0;
2752 if (bw == BW40PLUS &&
2753 (!(flag & HOSTAPD_CHAN_HT40PLUS) ||
2754 !has_channel(wpa_s->global, mode, channel + 4, NULL)))
2755 return 0;
2756 return 1;
2757}
2758
2759
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002760static int wpas_p2p_setup_channels(struct wpa_supplicant *wpa_s,
2761 struct p2p_channels *chan)
2762{
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002763 struct hostapd_hw_modes *mode;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002764 int cla, op;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002765
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002766 if (wpa_s->hw.modes == NULL) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002767 wpa_printf(MSG_DEBUG, "P2P: Driver did not support fetching "
2768 "of all supported channels; assume dualband "
2769 "support");
2770 return wpas_p2p_default_channels(wpa_s, chan);
2771 }
2772
2773 cla = 0;
2774
2775 for (op = 0; op_class[op].op_class; op++) {
2776 struct p2p_oper_class_map *o = &op_class[op];
2777 u8 ch;
2778 struct p2p_reg_class *reg = NULL;
2779
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002780 mode = get_mode(wpa_s->hw.modes, wpa_s->hw.num_modes, o->mode);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002781 if (mode == NULL)
2782 continue;
2783 for (ch = o->min_chan; ch <= o->max_chan; ch += o->inc) {
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002784 if (wpas_p2p_verify_channel(wpa_s, mode, ch, o->bw) < 1)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002785 continue;
2786 if (reg == NULL) {
2787 wpa_printf(MSG_DEBUG, "P2P: Add operating "
2788 "class %u", o->op_class);
2789 reg = &chan->reg_class[cla];
2790 cla++;
2791 reg->reg_class = o->op_class;
2792 }
2793 reg->channel[reg->channels] = ch;
2794 reg->channels++;
2795 }
2796 if (reg) {
2797 wpa_hexdump(MSG_DEBUG, "P2P: Channels",
2798 reg->channel, reg->channels);
2799 }
2800 }
2801
2802 chan->reg_classes = cla;
2803
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002804 return 0;
2805}
2806
2807
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002808int wpas_p2p_get_ht40_mode(struct wpa_supplicant *wpa_s,
2809 struct hostapd_hw_modes *mode, u8 channel)
2810{
2811 int op, ret;
2812
2813 for (op = 0; op_class[op].op_class; op++) {
2814 struct p2p_oper_class_map *o = &op_class[op];
2815 u8 ch;
2816
2817 for (ch = o->min_chan; ch <= o->max_chan; ch += o->inc) {
2818 if (o->mode != HOSTAPD_MODE_IEEE80211A ||
2819 o->bw == BW20 || ch != channel)
2820 continue;
2821 ret = wpas_p2p_verify_channel(wpa_s, mode, ch, o->bw);
2822 if (ret < 0)
2823 continue;
2824 else if (ret > 0)
2825 return (o->bw == BW40MINUS) ? -1 : 1;
2826 else
2827 return 0;
2828 }
2829 }
2830 return 0;
2831}
2832
2833
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002834static int wpas_get_noa(void *ctx, const u8 *interface_addr, u8 *buf,
2835 size_t buf_len)
2836{
2837 struct wpa_supplicant *wpa_s = ctx;
2838
2839 for (wpa_s = wpa_s->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
2840 if (os_memcmp(wpa_s->own_addr, interface_addr, ETH_ALEN) == 0)
2841 break;
2842 }
2843 if (wpa_s == NULL)
2844 return -1;
2845
2846 return wpa_drv_get_noa(wpa_s, buf, buf_len);
2847}
2848
2849
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08002850static int wpas_go_connected(void *ctx, const u8 *dev_addr)
2851{
2852 struct wpa_supplicant *wpa_s = ctx;
2853
2854 for (wpa_s = wpa_s->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
2855 struct wpa_ssid *ssid = wpa_s->current_ssid;
2856 if (ssid == NULL)
2857 continue;
2858 if (ssid->mode != WPAS_MODE_INFRA)
2859 continue;
2860 if (wpa_s->wpa_state != WPA_COMPLETED &&
2861 wpa_s->wpa_state != WPA_GROUP_HANDSHAKE)
2862 continue;
2863 if (os_memcmp(wpa_s->go_dev_addr, dev_addr, ETH_ALEN) == 0)
2864 return 1;
2865 }
2866
2867 return 0;
2868}
2869
2870
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002871/**
2872 * wpas_p2p_init - Initialize P2P module for %wpa_supplicant
2873 * @global: Pointer to global data from wpa_supplicant_init()
2874 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
2875 * Returns: 0 on success, -1 on failure
2876 */
2877int wpas_p2p_init(struct wpa_global *global, struct wpa_supplicant *wpa_s)
2878{
2879 struct p2p_config p2p;
2880 unsigned int r;
2881 int i;
2882
2883 if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_CAPABLE))
2884 return 0;
2885
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002886 if (global->p2p)
2887 return 0;
2888
2889 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT) {
2890 struct p2p_params params;
2891
2892 wpa_printf(MSG_DEBUG, "P2P: Use driver-based P2P management");
2893 os_memset(&params, 0, sizeof(params));
2894 params.dev_name = wpa_s->conf->device_name;
2895 os_memcpy(params.pri_dev_type, wpa_s->conf->device_type,
2896 WPS_DEV_TYPE_LEN);
2897 params.num_sec_dev_types = wpa_s->conf->num_sec_device_types;
2898 os_memcpy(params.sec_dev_type,
2899 wpa_s->conf->sec_device_type,
2900 params.num_sec_dev_types * WPS_DEV_TYPE_LEN);
2901
2902 if (wpa_drv_p2p_set_params(wpa_s, &params) < 0)
2903 return -1;
2904
2905 return 0;
2906 }
2907
2908 os_memset(&p2p, 0, sizeof(p2p));
2909 p2p.msg_ctx = wpa_s;
2910 p2p.cb_ctx = wpa_s;
2911 p2p.p2p_scan = wpas_p2p_scan;
2912 p2p.send_action = wpas_send_action;
2913 p2p.send_action_done = wpas_send_action_done;
2914 p2p.go_neg_completed = wpas_go_neg_completed;
2915 p2p.go_neg_req_rx = wpas_go_neg_req_rx;
2916 p2p.dev_found = wpas_dev_found;
2917 p2p.dev_lost = wpas_dev_lost;
2918 p2p.start_listen = wpas_start_listen;
2919 p2p.stop_listen = wpas_stop_listen;
2920 p2p.send_probe_resp = wpas_send_probe_resp;
2921 p2p.sd_request = wpas_sd_request;
2922 p2p.sd_response = wpas_sd_response;
2923 p2p.prov_disc_req = wpas_prov_disc_req;
2924 p2p.prov_disc_resp = wpas_prov_disc_resp;
Jouni Malinen75ecf522011-06-27 15:19:46 -07002925 p2p.prov_disc_fail = wpas_prov_disc_fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002926 p2p.invitation_process = wpas_invitation_process;
2927 p2p.invitation_received = wpas_invitation_received;
2928 p2p.invitation_result = wpas_invitation_result;
2929 p2p.get_noa = wpas_get_noa;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08002930 p2p.go_connected = wpas_go_connected;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002931
2932 os_memcpy(wpa_s->global->p2p_dev_addr, wpa_s->own_addr, ETH_ALEN);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002933 os_memcpy(p2p.dev_addr, wpa_s->global->p2p_dev_addr, ETH_ALEN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002934 p2p.dev_name = wpa_s->conf->device_name;
2935 p2p.manufacturer = wpa_s->conf->manufacturer;
2936 p2p.model_name = wpa_s->conf->model_name;
2937 p2p.model_number = wpa_s->conf->model_number;
2938 p2p.serial_number = wpa_s->conf->serial_number;
2939 if (wpa_s->wps) {
2940 os_memcpy(p2p.uuid, wpa_s->wps->uuid, 16);
2941 p2p.config_methods = wpa_s->wps->config_methods;
2942 }
2943
2944 if (wpa_s->conf->p2p_listen_reg_class &&
2945 wpa_s->conf->p2p_listen_channel) {
2946 p2p.reg_class = wpa_s->conf->p2p_listen_reg_class;
2947 p2p.channel = wpa_s->conf->p2p_listen_channel;
2948 } else {
2949 p2p.reg_class = 81;
2950 /*
2951 * Pick one of the social channels randomly as the listen
2952 * channel.
2953 */
2954 os_get_random((u8 *) &r, sizeof(r));
2955 p2p.channel = 1 + (r % 3) * 5;
2956 }
2957 wpa_printf(MSG_DEBUG, "P2P: Own listen channel: %d", p2p.channel);
2958
2959 if (wpa_s->conf->p2p_oper_reg_class &&
2960 wpa_s->conf->p2p_oper_channel) {
2961 p2p.op_reg_class = wpa_s->conf->p2p_oper_reg_class;
2962 p2p.op_channel = wpa_s->conf->p2p_oper_channel;
2963 p2p.cfg_op_channel = 1;
2964 wpa_printf(MSG_DEBUG, "P2P: Configured operating channel: "
2965 "%d:%d", p2p.op_reg_class, p2p.op_channel);
2966
2967 } else {
2968 p2p.op_reg_class = 81;
2969 /*
2970 * Use random operation channel from (1, 6, 11) if no other
2971 * preference is indicated.
2972 */
2973 os_get_random((u8 *) &r, sizeof(r));
2974 p2p.op_channel = 1 + (r % 3) * 5;
2975 p2p.cfg_op_channel = 0;
2976 wpa_printf(MSG_DEBUG, "P2P: Random operating channel: "
2977 "%d:%d", p2p.op_reg_class, p2p.op_channel);
2978 }
2979 if (wpa_s->conf->country[0] && wpa_s->conf->country[1]) {
2980 os_memcpy(p2p.country, wpa_s->conf->country, 2);
2981 p2p.country[2] = 0x04;
2982 } else
2983 os_memcpy(p2p.country, "XX\x04", 3);
2984
2985 if (wpas_p2p_setup_channels(wpa_s, &p2p.channels)) {
2986 wpa_printf(MSG_ERROR, "P2P: Failed to configure supported "
2987 "channel list");
2988 return -1;
2989 }
2990
2991 os_memcpy(p2p.pri_dev_type, wpa_s->conf->device_type,
2992 WPS_DEV_TYPE_LEN);
2993
2994 p2p.num_sec_dev_types = wpa_s->conf->num_sec_device_types;
2995 os_memcpy(p2p.sec_dev_type, wpa_s->conf->sec_device_type,
2996 p2p.num_sec_dev_types * WPS_DEV_TYPE_LEN);
2997
2998 p2p.concurrent_operations = !!(wpa_s->drv_flags &
2999 WPA_DRIVER_FLAGS_P2P_CONCURRENT);
3000
3001 p2p.max_peers = 100;
3002
3003 if (wpa_s->conf->p2p_ssid_postfix) {
3004 p2p.ssid_postfix_len =
3005 os_strlen(wpa_s->conf->p2p_ssid_postfix);
3006 if (p2p.ssid_postfix_len > sizeof(p2p.ssid_postfix))
3007 p2p.ssid_postfix_len = sizeof(p2p.ssid_postfix);
3008 os_memcpy(p2p.ssid_postfix, wpa_s->conf->p2p_ssid_postfix,
3009 p2p.ssid_postfix_len);
3010 }
3011
3012 p2p.p2p_intra_bss = wpa_s->conf->p2p_intra_bss;
3013
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08003014 p2p.max_listen = wpa_s->max_remain_on_chan;
3015
Dmitry Shmidt04abaa92012-08-17 16:22:31 -07003016#ifdef ANDROID_P2P
Irfan Sheriffaf84a572012-09-22 16:59:30 -07003017 if(wpa_s->drv_flags & WPA_DRIVER_FLAGS_MULTI_CHANNEL_CONCURRENT) {
Dmitry Shmidt04abaa92012-08-17 16:22:31 -07003018 p2p.p2p_concurrency = P2P_MULTI_CHANNEL_CONCURRENT;
Irfan Sheriffaf84a572012-09-22 16:59:30 -07003019 wpa_printf(MSG_DEBUG, "P2P: Multi channel concurrency support");
3020 } else {
3021 // Add support for WPA_DRIVER_FLAGS_P2P_CONCURRENT
Dmitry Shmidt04abaa92012-08-17 16:22:31 -07003022 p2p.p2p_concurrency = P2P_SINGLE_CHANNEL_CONCURRENT;
Irfan Sheriffaf84a572012-09-22 16:59:30 -07003023 wpa_printf(MSG_DEBUG, "P2P: Single channel concurrency support");
3024 }
Dmitry Shmidt04abaa92012-08-17 16:22:31 -07003025#endif
3026
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003027 global->p2p = p2p_init(&p2p);
3028 if (global->p2p == NULL)
3029 return -1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003030 global->p2p_init_wpa_s = wpa_s;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003031
3032 for (i = 0; i < MAX_WPS_VENDOR_EXT; i++) {
3033 if (wpa_s->conf->wps_vendor_ext[i] == NULL)
3034 continue;
3035 p2p_add_wps_vendor_extension(
3036 global->p2p, wpa_s->conf->wps_vendor_ext[i]);
3037 }
3038
3039 return 0;
3040}
3041
3042
3043/**
3044 * wpas_p2p_deinit - Deinitialize per-interface P2P data
3045 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
3046 *
3047 * This function deinitialize per-interface P2P data.
3048 */
3049void wpas_p2p_deinit(struct wpa_supplicant *wpa_s)
3050{
3051 if (wpa_s->driver && wpa_s->drv_priv)
3052 wpa_drv_probe_req_report(wpa_s, 0);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003053
3054 if (wpa_s->go_params) {
3055 /* Clear any stored provisioning info */
3056 p2p_clear_provisioning_info(
3057 wpa_s->global->p2p,
Dmitry Shmidt04949592012-07-19 12:16:46 -07003058 wpa_s->go_params->peer_device_addr);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003059 }
3060
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003061 os_free(wpa_s->go_params);
3062 wpa_s->go_params = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003063 eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s, NULL);
3064 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
3065 wpa_s->p2p_long_listen = 0;
3066 eloop_cancel_timeout(wpas_p2p_long_listen_timeout, wpa_s, NULL);
3067 eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL);
3068 wpas_p2p_remove_pending_group_interface(wpa_s);
3069
3070 /* TODO: remove group interface from the driver if this wpa_s instance
3071 * is on top of a P2P group interface */
3072}
3073
3074
3075/**
3076 * wpas_p2p_deinit_global - Deinitialize global P2P module
3077 * @global: Pointer to global data from wpa_supplicant_init()
3078 *
3079 * This function deinitializes the global (per device) P2P module.
3080 */
3081void wpas_p2p_deinit_global(struct wpa_global *global)
3082{
3083 struct wpa_supplicant *wpa_s, *tmp;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003084
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003085 wpa_s = global->ifaces;
3086 if (wpa_s)
3087 wpas_p2p_service_flush(wpa_s);
3088
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003089 if (global->p2p == NULL)
3090 return;
3091
3092 /* Remove remaining P2P group interfaces */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003093 while (wpa_s && wpa_s->p2p_group_interface != NOT_P2P_GROUP_INTERFACE)
3094 wpa_s = wpa_s->next;
3095 while (wpa_s) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003096 tmp = global->ifaces;
3097 while (tmp &&
3098 (tmp == wpa_s ||
3099 tmp->p2p_group_interface == NOT_P2P_GROUP_INTERFACE)) {
3100 tmp = tmp->next;
3101 }
3102 if (tmp == NULL)
3103 break;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003104 /* Disconnect from the P2P group and deinit the interface */
3105 wpas_p2p_disconnect(tmp);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003106 }
3107
3108 /*
3109 * Deinit GO data on any possibly remaining interface (if main
3110 * interface is used as GO).
3111 */
3112 for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
3113 if (wpa_s->ap_iface)
3114 wpas_p2p_group_deinit(wpa_s);
3115 }
3116
3117 p2p_deinit(global->p2p);
3118 global->p2p = NULL;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003119 global->p2p_init_wpa_s = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003120}
3121
3122
3123static int wpas_p2p_create_iface(struct wpa_supplicant *wpa_s)
3124{
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08003125 if (wpa_s->conf->p2p_no_group_iface)
3126 return 0; /* separate interface disabled per configuration */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003127 if (wpa_s->drv_flags &
3128 (WPA_DRIVER_FLAGS_P2P_DEDICATED_INTERFACE |
3129 WPA_DRIVER_FLAGS_P2P_MGMT_AND_NON_P2P))
3130 return 1; /* P2P group requires a new interface in every case
3131 */
3132 if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_CONCURRENT))
3133 return 0; /* driver does not support concurrent operations */
3134 if (wpa_s->global->ifaces->next)
3135 return 1; /* more that one interface already in use */
3136 if (wpa_s->wpa_state >= WPA_AUTHENTICATING)
3137 return 1; /* this interface is already in use */
3138 return 0;
3139}
3140
3141
3142static int wpas_p2p_start_go_neg(struct wpa_supplicant *wpa_s,
3143 const u8 *peer_addr,
3144 enum p2p_wps_method wps_method,
3145 int go_intent, const u8 *own_interface_addr,
Dmitry Shmidt04949592012-07-19 12:16:46 -07003146 unsigned int force_freq, int persistent_group,
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08003147 struct wpa_ssid *ssid, unsigned int pref_freq)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003148{
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003149 if (persistent_group && wpa_s->conf->persistent_reconnect)
3150 persistent_group = 2;
3151
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003152 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT) {
3153 return wpa_drv_p2p_connect(wpa_s, peer_addr, wps_method,
3154 go_intent, own_interface_addr,
3155 force_freq, persistent_group);
3156 }
3157
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003158 /*
3159 * Increase GO config timeout if HT40 is used since it takes some time
3160 * to scan channels for coex purposes before the BSS can be started.
3161 */
3162 p2p_set_config_timeout(wpa_s->global->p2p,
3163 wpa_s->p2p_go_ht40 ? 255 : 100, 20);
3164
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003165 return p2p_connect(wpa_s->global->p2p, peer_addr, wps_method,
3166 go_intent, own_interface_addr, force_freq,
Dmitry Shmidt04949592012-07-19 12:16:46 -07003167 persistent_group, ssid ? ssid->ssid : NULL,
3168 ssid ? ssid->ssid_len : 0,
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08003169 wpa_s->p2p_pd_before_go_neg, pref_freq);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003170}
3171
3172
3173static int wpas_p2p_auth_go_neg(struct wpa_supplicant *wpa_s,
3174 const u8 *peer_addr,
3175 enum p2p_wps_method wps_method,
3176 int go_intent, const u8 *own_interface_addr,
Dmitry Shmidt04949592012-07-19 12:16:46 -07003177 unsigned int force_freq, int persistent_group,
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08003178 struct wpa_ssid *ssid, unsigned int pref_freq)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003179{
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003180 if (persistent_group && wpa_s->conf->persistent_reconnect)
3181 persistent_group = 2;
3182
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003183 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
3184 return -1;
3185
3186 return p2p_authorize(wpa_s->global->p2p, peer_addr, wps_method,
3187 go_intent, own_interface_addr, force_freq,
Dmitry Shmidt04949592012-07-19 12:16:46 -07003188 persistent_group, ssid ? ssid->ssid : NULL,
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08003189 ssid ? ssid->ssid_len : 0, pref_freq);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003190}
3191
3192
3193static void wpas_p2p_check_join_scan_limit(struct wpa_supplicant *wpa_s)
3194{
3195 wpa_s->p2p_join_scan_count++;
3196 wpa_printf(MSG_DEBUG, "P2P: Join scan attempt %d",
3197 wpa_s->p2p_join_scan_count);
3198 if (wpa_s->p2p_join_scan_count > P2P_MAX_JOIN_SCAN_ATTEMPTS) {
3199 wpa_printf(MSG_DEBUG, "P2P: Failed to find GO " MACSTR
3200 " for join operationg - stop join attempt",
3201 MAC2STR(wpa_s->pending_join_iface_addr));
3202 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
Dmitry Shmidt04949592012-07-19 12:16:46 -07003203 if (wpa_s->p2p_auto_pd) {
3204 wpa_s->p2p_auto_pd = 0;
3205 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_FAILURE
3206 " p2p_dev_addr=" MACSTR " status=N/A",
3207 MAC2STR(wpa_s->pending_join_dev_addr));
3208 return;
3209 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003210 wpa_msg(wpa_s->parent, MSG_INFO,
3211 P2P_EVENT_GROUP_FORMATION_FAILURE);
3212 }
3213}
3214
3215
Dmitry Shmidt04949592012-07-19 12:16:46 -07003216static int wpas_check_freq_conflict(struct wpa_supplicant *wpa_s, int freq)
3217{
3218 struct wpa_supplicant *iface;
3219 int shared_freq;
3220 u8 bssid[ETH_ALEN];
3221
3222 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_MULTI_CHANNEL_CONCURRENT)
3223 return 0;
3224
3225 for (iface = wpa_s->global->ifaces; iface; iface = iface->next) {
3226 if (!wpas_p2p_create_iface(wpa_s) && iface == wpa_s)
3227 continue;
3228 if (iface->current_ssid == NULL || iface->assoc_freq == 0)
3229 continue;
3230 if (iface->current_ssid->mode == WPAS_MODE_AP ||
3231 iface->current_ssid->mode == WPAS_MODE_P2P_GO)
3232 shared_freq = iface->current_ssid->frequency;
3233 else if (wpa_drv_get_bssid(iface, bssid) == 0)
3234 shared_freq = iface->assoc_freq;
3235 else
3236 shared_freq = 0;
3237
3238 if (shared_freq && freq != shared_freq) {
3239 wpa_printf(MSG_DEBUG, "P2P: Frequency conflict - %s "
3240 "connected on %d MHz - new connection on "
3241 "%d MHz", iface->ifname, shared_freq, freq);
3242 return 1;
3243 }
3244 }
3245
3246 shared_freq = wpa_drv_shared_freq(wpa_s);
3247 if (shared_freq > 0 && shared_freq != freq) {
3248 wpa_printf(MSG_DEBUG, "P2P: Frequency conflict - shared "
3249 "virtual interface connected on %d MHz - new "
3250 "connection on %d MHz", shared_freq, freq);
3251 return 1;
3252 }
3253
3254 return 0;
3255}
3256
3257
3258static int wpas_p2p_peer_go(struct wpa_supplicant *wpa_s,
3259 const u8 *peer_dev_addr)
3260{
3261 struct wpa_bss *bss;
3262 int updated;
3263
3264 bss = wpa_bss_get_p2p_dev_addr(wpa_s, peer_dev_addr);
3265 if (bss == NULL)
3266 return -1;
3267 if (bss->last_update_idx < wpa_s->bss_update_idx) {
3268 wpa_printf(MSG_DEBUG, "P2P: Peer BSS entry not updated in the "
3269 "last scan");
3270 return 0;
3271 }
3272
3273 updated = os_time_before(&wpa_s->p2p_auto_started, &bss->last_update);
3274 wpa_printf(MSG_DEBUG, "P2P: Current BSS entry for peer updated at "
3275 "%ld.%06ld (%supdated in last scan)",
3276 bss->last_update.sec, bss->last_update.usec,
3277 updated ? "": "not ");
3278
3279 return updated;
3280}
3281
3282
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003283static void wpas_p2p_scan_res_join(struct wpa_supplicant *wpa_s,
3284 struct wpa_scan_results *scan_res)
3285{
3286 struct wpa_bss *bss;
3287 int freq;
3288 u8 iface_addr[ETH_ALEN];
Dmitry Shmidt04949592012-07-19 12:16:46 -07003289
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003290 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
3291
3292 if (wpa_s->global->p2p_disabled)
3293 return;
3294
Dmitry Shmidt04949592012-07-19 12:16:46 -07003295 wpa_printf(MSG_DEBUG, "P2P: Scan results received (%d BSS) for %sjoin",
3296 scan_res ? (int) scan_res->num : -1,
3297 wpa_s->p2p_auto_join ? "auto_" : "");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003298
3299 if (scan_res)
3300 wpas_p2p_scan_res_handler(wpa_s, scan_res);
3301
Dmitry Shmidt04949592012-07-19 12:16:46 -07003302 if (wpa_s->p2p_auto_pd) {
3303 int join = wpas_p2p_peer_go(wpa_s,
3304 wpa_s->pending_join_dev_addr);
3305 if (join == 0 &&
3306 wpa_s->auto_pd_scan_retry < P2P_AUTO_PD_SCAN_ATTEMPTS) {
3307 wpa_s->auto_pd_scan_retry++;
3308 bss = wpa_bss_get_bssid(wpa_s,
3309 wpa_s->pending_join_dev_addr);
3310 if (bss) {
3311 freq = bss->freq;
3312 wpa_printf(MSG_DEBUG, "P2P: Scan retry %d for "
3313 "the peer " MACSTR " at %d MHz",
3314 wpa_s->auto_pd_scan_retry,
3315 MAC2STR(wpa_s->
3316 pending_join_dev_addr),
3317 freq);
3318 wpas_p2p_join_scan_req(wpa_s, freq);
3319 return;
3320 }
3321 }
3322
3323 if (join < 0)
3324 join = 0;
3325
3326 wpa_s->p2p_auto_pd = 0;
3327 wpa_s->pending_pd_use = join ? AUTO_PD_JOIN : AUTO_PD_GO_NEG;
3328 wpa_printf(MSG_DEBUG, "P2P: Auto PD with " MACSTR " join=%d",
3329 MAC2STR(wpa_s->pending_join_dev_addr), join);
3330 if (p2p_prov_disc_req(wpa_s->global->p2p,
3331 wpa_s->pending_join_dev_addr,
3332 wpa_s->pending_pd_config_methods, join,
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08003333 0, wpa_s->user_initiated_pd) < 0) {
Dmitry Shmidt04949592012-07-19 12:16:46 -07003334 wpa_s->p2p_auto_pd = 0;
3335 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_FAILURE
3336 " p2p_dev_addr=" MACSTR " status=N/A",
3337 MAC2STR(wpa_s->pending_join_dev_addr));
3338 }
3339 return;
3340 }
3341
3342 if (wpa_s->p2p_auto_join) {
3343 int join = wpas_p2p_peer_go(wpa_s,
3344 wpa_s->pending_join_dev_addr);
3345 if (join < 0) {
3346 wpa_printf(MSG_DEBUG, "P2P: Peer was not found to be "
3347 "running a GO -> use GO Negotiation");
3348 wpas_p2p_connect(wpa_s, wpa_s->pending_join_dev_addr,
3349 wpa_s->p2p_pin, wpa_s->p2p_wps_method,
3350 wpa_s->p2p_persistent_group, 0, 0, 0,
3351 wpa_s->p2p_go_intent,
3352 wpa_s->p2p_connect_freq,
3353 wpa_s->p2p_persistent_id,
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003354 wpa_s->p2p_pd_before_go_neg,
3355 wpa_s->p2p_go_ht40);
Dmitry Shmidt04949592012-07-19 12:16:46 -07003356 return;
3357 }
3358
3359 wpa_printf(MSG_DEBUG, "P2P: Peer was found running GO%s -> "
3360 "try to join the group", join ? "" :
3361 " in older scan");
3362 if (!join)
3363 wpa_s->p2p_fallback_to_go_neg = 1;
3364 }
3365
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003366 freq = p2p_get_oper_freq(wpa_s->global->p2p,
3367 wpa_s->pending_join_iface_addr);
3368 if (freq < 0 &&
3369 p2p_get_interface_addr(wpa_s->global->p2p,
3370 wpa_s->pending_join_dev_addr,
3371 iface_addr) == 0 &&
3372 os_memcmp(iface_addr, wpa_s->pending_join_dev_addr, ETH_ALEN) != 0)
3373 {
3374 wpa_printf(MSG_DEBUG, "P2P: Overwrite pending interface "
3375 "address for join from " MACSTR " to " MACSTR
3376 " based on newly discovered P2P peer entry",
3377 MAC2STR(wpa_s->pending_join_iface_addr),
3378 MAC2STR(iface_addr));
3379 os_memcpy(wpa_s->pending_join_iface_addr, iface_addr,
3380 ETH_ALEN);
3381
3382 freq = p2p_get_oper_freq(wpa_s->global->p2p,
3383 wpa_s->pending_join_iface_addr);
3384 }
3385 if (freq >= 0) {
3386 wpa_printf(MSG_DEBUG, "P2P: Target GO operating frequency "
3387 "from P2P peer table: %d MHz", freq);
3388 }
3389 bss = wpa_bss_get_bssid(wpa_s, wpa_s->pending_join_iface_addr);
3390 if (bss) {
3391 freq = bss->freq;
3392 wpa_printf(MSG_DEBUG, "P2P: Target GO operating frequency "
3393 "from BSS table: %d MHz", freq);
3394 }
3395 if (freq > 0) {
3396 u16 method;
3397
Dmitry Shmidt04949592012-07-19 12:16:46 -07003398 if (wpas_check_freq_conflict(wpa_s, freq) > 0) {
3399 wpa_msg(wpa_s->parent, MSG_INFO,
3400 P2P_EVENT_GROUP_FORMATION_FAILURE
3401 "reason=FREQ_CONFLICT");
3402 return;
3403 }
3404
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003405 wpa_printf(MSG_DEBUG, "P2P: Send Provision Discovery Request "
3406 "prior to joining an existing group (GO " MACSTR
3407 " freq=%u MHz)",
3408 MAC2STR(wpa_s->pending_join_dev_addr), freq);
3409 wpa_s->pending_pd_before_join = 1;
3410
3411 switch (wpa_s->pending_join_wps_method) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003412 case WPS_PIN_DISPLAY:
3413 method = WPS_CONFIG_KEYPAD;
3414 break;
3415 case WPS_PIN_KEYPAD:
3416 method = WPS_CONFIG_DISPLAY;
3417 break;
3418 case WPS_PBC:
3419 method = WPS_CONFIG_PUSHBUTTON;
3420 break;
3421 default:
3422 method = 0;
3423 break;
3424 }
3425
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003426 if ((p2p_get_provisioning_info(wpa_s->global->p2p,
3427 wpa_s->pending_join_dev_addr) ==
3428 method)) {
3429 /*
3430 * We have already performed provision discovery for
3431 * joining the group. Proceed directly to join
3432 * operation without duplicated provision discovery. */
3433 wpa_printf(MSG_DEBUG, "P2P: Provision discovery "
3434 "with " MACSTR " already done - proceed to "
3435 "join",
3436 MAC2STR(wpa_s->pending_join_dev_addr));
3437 wpa_s->pending_pd_before_join = 0;
3438 goto start;
3439 }
3440
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003441 if (p2p_prov_disc_req(wpa_s->global->p2p,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003442 wpa_s->pending_join_dev_addr, method, 1,
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08003443 freq, wpa_s->user_initiated_pd) < 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003444 wpa_printf(MSG_DEBUG, "P2P: Failed to send Provision "
3445 "Discovery Request before joining an "
3446 "existing group");
3447 wpa_s->pending_pd_before_join = 0;
3448 goto start;
3449 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003450 return;
3451 }
3452
3453 wpa_printf(MSG_DEBUG, "P2P: Failed to find BSS/GO - try again later");
3454 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
3455 eloop_register_timeout(1, 0, wpas_p2p_join_scan, wpa_s, NULL);
3456 wpas_p2p_check_join_scan_limit(wpa_s);
3457 return;
3458
3459start:
3460 /* Start join operation immediately */
3461 wpas_p2p_join_start(wpa_s);
3462}
3463
3464
Dmitry Shmidt04949592012-07-19 12:16:46 -07003465static void wpas_p2p_join_scan_req(struct wpa_supplicant *wpa_s, int freq)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003466{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003467 int ret;
3468 struct wpa_driver_scan_params params;
3469 struct wpabuf *wps_ie, *ies;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003470 size_t ielen;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003471 int freqs[2] = { 0, 0 };
Dmitry Shmidtad266fb2012-08-24 17:03:35 -07003472#ifdef ANDROID_P2P
3473 int oper_freq;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003474
Dmitry Shmidtad266fb2012-08-24 17:03:35 -07003475 /* If freq is not provided, check the operating freq of the GO and do a
3476 * a directed scan to save time
3477 */
3478 if(!freq) {
3479 freq = (oper_freq = p2p_get_oper_freq(wpa_s->global->p2p,
3480 wpa_s->pending_join_iface_addr) == -1) ? 0 : oper_freq;
3481 }
3482#endif
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003483 os_memset(&params, 0, sizeof(params));
3484
3485 /* P2P Wildcard SSID */
3486 params.num_ssids = 1;
3487 params.ssids[0].ssid = (u8 *) P2P_WILDCARD_SSID;
3488 params.ssids[0].ssid_len = P2P_WILDCARD_SSID_LEN;
3489
3490 wpa_s->wps->dev.p2p = 1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003491 wps_ie = wps_build_probe_req_ie(DEV_PW_DEFAULT, &wpa_s->wps->dev,
3492 wpa_s->wps->uuid, WPS_REQ_ENROLLEE, 0,
3493 NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003494 if (wps_ie == NULL) {
3495 wpas_p2p_scan_res_join(wpa_s, NULL);
3496 return;
3497 }
3498
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003499 ielen = p2p_scan_ie_buf_len(wpa_s->global->p2p);
3500 ies = wpabuf_alloc(wpabuf_len(wps_ie) + ielen);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003501 if (ies == NULL) {
3502 wpabuf_free(wps_ie);
3503 wpas_p2p_scan_res_join(wpa_s, NULL);
3504 return;
3505 }
3506 wpabuf_put_buf(ies, wps_ie);
3507 wpabuf_free(wps_ie);
3508
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08003509 p2p_scan_ie(wpa_s->global->p2p, ies, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003510
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003511 params.p2p_probe = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003512 params.extra_ies = wpabuf_head(ies);
3513 params.extra_ies_len = wpabuf_len(ies);
Dmitry Shmidt04949592012-07-19 12:16:46 -07003514 if (freq > 0) {
3515 freqs[0] = freq;
3516 params.freqs = freqs;
3517 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003518
3519 /*
3520 * Run a scan to update BSS table and start Provision Discovery once
3521 * the new scan results become available.
3522 */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003523 ret = wpa_drv_scan(wpa_s, &params);
Dmitry Shmidt04949592012-07-19 12:16:46 -07003524 if (!ret)
3525 wpa_s->scan_res_handler = wpas_p2p_scan_res_join;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003526
3527 wpabuf_free(ies);
3528
3529 if (ret) {
3530 wpa_printf(MSG_DEBUG, "P2P: Failed to start scan for join - "
3531 "try again later");
3532 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
3533 eloop_register_timeout(1, 0, wpas_p2p_join_scan, wpa_s, NULL);
3534 wpas_p2p_check_join_scan_limit(wpa_s);
3535 }
3536}
3537
3538
Dmitry Shmidt04949592012-07-19 12:16:46 -07003539static void wpas_p2p_join_scan(void *eloop_ctx, void *timeout_ctx)
3540{
3541 struct wpa_supplicant *wpa_s = eloop_ctx;
3542 wpas_p2p_join_scan_req(wpa_s, 0);
3543}
3544
3545
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003546static int wpas_p2p_join(struct wpa_supplicant *wpa_s, const u8 *iface_addr,
Dmitry Shmidt04949592012-07-19 12:16:46 -07003547 const u8 *dev_addr, enum p2p_wps_method wps_method,
3548 int auto_join)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003549{
3550 wpa_printf(MSG_DEBUG, "P2P: Request to join existing group (iface "
Dmitry Shmidt04949592012-07-19 12:16:46 -07003551 MACSTR " dev " MACSTR ")%s",
3552 MAC2STR(iface_addr), MAC2STR(dev_addr),
3553 auto_join ? " (auto_join)" : "");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003554
Dmitry Shmidt04949592012-07-19 12:16:46 -07003555 wpa_s->p2p_auto_pd = 0;
3556 wpa_s->p2p_auto_join = !!auto_join;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003557 os_memcpy(wpa_s->pending_join_iface_addr, iface_addr, ETH_ALEN);
3558 os_memcpy(wpa_s->pending_join_dev_addr, dev_addr, ETH_ALEN);
3559 wpa_s->pending_join_wps_method = wps_method;
3560
3561 /* Make sure we are not running find during connection establishment */
3562 wpas_p2p_stop_find(wpa_s);
3563
3564 wpa_s->p2p_join_scan_count = 0;
3565 wpas_p2p_join_scan(wpa_s, NULL);
3566 return 0;
3567}
3568
3569
3570static int wpas_p2p_join_start(struct wpa_supplicant *wpa_s)
3571{
3572 struct wpa_supplicant *group;
3573 struct p2p_go_neg_results res;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003574 struct wpa_bss *bss;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003575
3576 group = wpas_p2p_get_group_iface(wpa_s, 0, 0);
3577 if (group == NULL)
3578 return -1;
3579 if (group != wpa_s) {
3580 os_memcpy(group->p2p_pin, wpa_s->p2p_pin,
3581 sizeof(group->p2p_pin));
3582 group->p2p_wps_method = wpa_s->p2p_wps_method;
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08003583 } else {
3584 /*
3585 * Need to mark the current interface for p2p_group_formation
3586 * when a separate group interface is not used. This is needed
3587 * to allow p2p_cancel stop a pending p2p_connect-join.
3588 * wpas_p2p_init_group_interface() addresses this for the case
3589 * where a separate group interface is used.
3590 */
3591 wpa_s->global->p2p_group_formation = wpa_s;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003592 }
3593
3594 group->p2p_in_provisioning = 1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003595 group->p2p_fallback_to_go_neg = wpa_s->p2p_fallback_to_go_neg;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003596
3597 os_memset(&res, 0, sizeof(res));
3598 os_memcpy(res.peer_interface_addr, wpa_s->pending_join_iface_addr,
3599 ETH_ALEN);
3600 res.wps_method = wpa_s->pending_join_wps_method;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003601 bss = wpa_bss_get_bssid(wpa_s, wpa_s->pending_join_iface_addr);
3602 if (bss) {
3603 res.freq = bss->freq;
3604 res.ssid_len = bss->ssid_len;
3605 os_memcpy(res.ssid, bss->ssid, bss->ssid_len);
3606 }
3607
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08003608 if (wpa_s->off_channel_freq || wpa_s->roc_waiting_drv_freq) {
3609 wpa_printf(MSG_DEBUG, "P2P: Cancel remain-on-channel prior to "
3610 "starting client");
3611 wpa_drv_cancel_remain_on_channel(wpa_s);
3612 wpa_s->off_channel_freq = 0;
3613 wpa_s->roc_waiting_drv_freq = 0;
3614 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003615 wpas_start_wps_enrollee(group, &res);
3616
3617 /*
3618 * Allow a longer timeout for join-a-running-group than normal 15
3619 * second group formation timeout since the GO may not have authorized
3620 * our connection yet.
3621 */
3622 eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s, NULL);
3623 eloop_register_timeout(60, 0, wpas_p2p_group_formation_timeout,
3624 wpa_s, NULL);
3625
3626 return 0;
3627}
3628
3629
3630/**
3631 * wpas_p2p_connect - Request P2P Group Formation to be started
3632 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
3633 * @peer_addr: Address of the peer P2P Device
3634 * @pin: PIN to use during provisioning or %NULL to indicate PBC mode
3635 * @persistent_group: Whether to create a persistent group
Dmitry Shmidt04949592012-07-19 12:16:46 -07003636 * @auto_join: Whether to select join vs. GO Negotiation automatically
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003637 * @join: Whether to join an existing group (as a client) instead of starting
3638 * Group Owner negotiation; @peer_addr is BSSID in that case
3639 * @auth: Whether to only authorize the connection instead of doing that and
3640 * initiating Group Owner negotiation
3641 * @go_intent: GO Intent or -1 to use default
3642 * @freq: Frequency for the group or 0 for auto-selection
Dmitry Shmidt04949592012-07-19 12:16:46 -07003643 * @persistent_id: Persistent group credentials to use for forcing GO
3644 * parameters or -1 to generate new values (SSID/passphrase)
3645 * @pd: Whether to send Provision Discovery prior to GO Negotiation as an
3646 * interoperability workaround when initiating group formation
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003647 * @ht40: Start GO with 40 MHz channel width
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003648 * Returns: 0 or new PIN (if pin was %NULL) on success, -1 on unspecified
3649 * failure, -2 on failure due to channel not currently available,
3650 * -3 if forced channel is not supported
3651 */
3652int wpas_p2p_connect(struct wpa_supplicant *wpa_s, const u8 *peer_addr,
3653 const char *pin, enum p2p_wps_method wps_method,
Dmitry Shmidt04949592012-07-19 12:16:46 -07003654 int persistent_group, int auto_join, int join, int auth,
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003655 int go_intent, int freq, int persistent_id, int pd,
3656 int ht40)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003657{
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08003658 int force_freq = 0, pref_freq = 0, oper_freq = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003659 u8 bssid[ETH_ALEN];
3660 int ret = 0;
3661 enum wpa_driver_if_type iftype;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003662 const u8 *if_addr;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003663 struct wpa_ssid *ssid = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003664
3665 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
3666 return -1;
3667
Dmitry Shmidt04949592012-07-19 12:16:46 -07003668 if (persistent_id >= 0) {
3669 ssid = wpa_config_get_network(wpa_s->conf, persistent_id);
3670 if (ssid == NULL || ssid->disabled != 2 ||
3671 ssid->mode != WPAS_MODE_P2P_GO)
3672 return -1;
3673 }
3674
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003675 if (go_intent < 0)
3676 go_intent = wpa_s->conf->p2p_go_intent;
3677
3678 if (!auth)
3679 wpa_s->p2p_long_listen = 0;
3680
3681 wpa_s->p2p_wps_method = wps_method;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003682 wpa_s->p2p_persistent_group = !!persistent_group;
3683 wpa_s->p2p_persistent_id = persistent_id;
3684 wpa_s->p2p_go_intent = go_intent;
3685 wpa_s->p2p_connect_freq = freq;
3686 wpa_s->p2p_fallback_to_go_neg = 0;
3687 wpa_s->p2p_pd_before_go_neg = !!pd;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003688 wpa_s->p2p_go_ht40 = !!ht40;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003689
3690 if (pin)
3691 os_strlcpy(wpa_s->p2p_pin, pin, sizeof(wpa_s->p2p_pin));
3692 else if (wps_method == WPS_PIN_DISPLAY) {
3693 ret = wps_generate_pin();
3694 os_snprintf(wpa_s->p2p_pin, sizeof(wpa_s->p2p_pin), "%08d",
3695 ret);
3696 wpa_printf(MSG_DEBUG, "P2P: Randomly generated PIN: %s",
3697 wpa_s->p2p_pin);
3698 } else
3699 wpa_s->p2p_pin[0] = '\0';
3700
Dmitry Shmidt04949592012-07-19 12:16:46 -07003701 if (join || auto_join) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003702 u8 iface_addr[ETH_ALEN], dev_addr[ETH_ALEN];
3703 if (auth) {
3704 wpa_printf(MSG_DEBUG, "P2P: Authorize invitation to "
3705 "connect a running group from " MACSTR,
3706 MAC2STR(peer_addr));
3707 os_memcpy(wpa_s->p2p_auth_invite, peer_addr, ETH_ALEN);
3708 return ret;
3709 }
3710 os_memcpy(dev_addr, peer_addr, ETH_ALEN);
3711 if (p2p_get_interface_addr(wpa_s->global->p2p, peer_addr,
3712 iface_addr) < 0) {
3713 os_memcpy(iface_addr, peer_addr, ETH_ALEN);
3714 p2p_get_dev_addr(wpa_s->global->p2p, peer_addr,
3715 dev_addr);
3716 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07003717 if (auto_join) {
3718 os_get_time(&wpa_s->p2p_auto_started);
3719 wpa_printf(MSG_DEBUG, "P2P: Auto join started at "
3720 "%ld.%06ld",
3721 wpa_s->p2p_auto_started.sec,
3722 wpa_s->p2p_auto_started.usec);
3723 }
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08003724 wpa_s->user_initiated_pd = 1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003725 if (wpas_p2p_join(wpa_s, iface_addr, dev_addr, wps_method,
3726 auto_join) < 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003727 return -1;
3728 return ret;
3729 }
3730
3731 if (wpa_s->current_ssid && wpa_drv_get_bssid(wpa_s, bssid) == 0 &&
3732 wpa_s->assoc_freq)
3733 oper_freq = wpa_s->assoc_freq;
3734 else {
3735 oper_freq = wpa_drv_shared_freq(wpa_s);
3736 if (oper_freq < 0)
3737 oper_freq = 0;
3738 }
3739
3740 if (freq > 0) {
3741 if (!p2p_supported_freq(wpa_s->global->p2p, freq)) {
3742 wpa_printf(MSG_DEBUG, "P2P: The forced channel "
3743 "(%u MHz) is not supported for P2P uses",
3744 freq);
3745 return -3;
3746 }
3747
3748 if (oper_freq > 0 && freq != oper_freq &&
3749 !(wpa_s->drv_flags &
3750 WPA_DRIVER_FLAGS_MULTI_CHANNEL_CONCURRENT)) {
3751 wpa_printf(MSG_DEBUG, "P2P: Cannot start P2P group "
3752 "on %u MHz while connected on another "
3753 "channel (%u MHz)", freq, oper_freq);
3754 return -2;
3755 }
3756 wpa_printf(MSG_DEBUG, "P2P: Trying to force us to use the "
3757 "requested channel (%u MHz)", freq);
3758 force_freq = freq;
3759 } else if (oper_freq > 0 &&
3760 !p2p_supported_freq(wpa_s->global->p2p, oper_freq)) {
3761 if (!(wpa_s->drv_flags &
3762 WPA_DRIVER_FLAGS_MULTI_CHANNEL_CONCURRENT)) {
3763 wpa_printf(MSG_DEBUG, "P2P: Cannot start P2P group "
3764 "while connected on non-P2P supported "
3765 "channel (%u MHz)", oper_freq);
3766 return -2;
3767 }
3768 wpa_printf(MSG_DEBUG, "P2P: Current operating channel "
3769 "(%u MHz) not available for P2P - try to use "
3770 "another channel", oper_freq);
3771 force_freq = 0;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08003772 } else if (oper_freq > 0 &&
3773 (wpa_s->drv_flags &
3774 WPA_DRIVER_FLAGS_MULTI_CHANNEL_CONCURRENT)) {
3775 wpa_printf(MSG_DEBUG, "P2P: Trying to prefer the channel we "
3776 "are already using (%u MHz) on another interface",
3777 oper_freq);
3778 pref_freq = oper_freq;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003779 } else if (oper_freq > 0) {
3780 wpa_printf(MSG_DEBUG, "P2P: Trying to force us to use the "
3781 "channel we are already using (%u MHz) on another "
3782 "interface", oper_freq);
3783 force_freq = oper_freq;
3784 }
3785
3786 wpa_s->create_p2p_iface = wpas_p2p_create_iface(wpa_s);
3787
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003788 if (wpa_s->create_p2p_iface) {
3789 /* Prepare to add a new interface for the group */
3790 iftype = WPA_IF_P2P_GROUP;
3791 if (go_intent == 15)
3792 iftype = WPA_IF_P2P_GO;
3793 if (wpas_p2p_add_group_interface(wpa_s, iftype) < 0) {
3794 wpa_printf(MSG_ERROR, "P2P: Failed to allocate a new "
3795 "interface for the group");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003796 return -1;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003797 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003798
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003799 if_addr = wpa_s->pending_interface_addr;
3800 } else
3801 if_addr = wpa_s->own_addr;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003802
3803 if (auth) {
3804 if (wpas_p2p_auth_go_neg(wpa_s, peer_addr, wps_method,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003805 go_intent, if_addr,
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08003806 force_freq, persistent_group, ssid,
3807 pref_freq) < 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003808 return -1;
3809 return ret;
3810 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003811
3812 if (wpas_p2p_start_go_neg(wpa_s, peer_addr, wps_method,
3813 go_intent, if_addr, force_freq,
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08003814 persistent_group, ssid, pref_freq) < 0) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003815 if (wpa_s->create_p2p_iface)
3816 wpas_p2p_remove_pending_group_interface(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003817 return -1;
3818 }
3819 return ret;
3820}
3821
3822
3823/**
3824 * wpas_p2p_remain_on_channel_cb - Indication of remain-on-channel start
3825 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
3826 * @freq: Frequency of the channel in MHz
3827 * @duration: Duration of the stay on the channel in milliseconds
3828 *
3829 * This callback is called when the driver indicates that it has started the
3830 * requested remain-on-channel duration.
3831 */
3832void wpas_p2p_remain_on_channel_cb(struct wpa_supplicant *wpa_s,
3833 unsigned int freq, unsigned int duration)
3834{
3835 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
3836 return;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003837 if (wpa_s->off_channel_freq == wpa_s->pending_listen_freq) {
3838 p2p_listen_cb(wpa_s->global->p2p, wpa_s->pending_listen_freq,
3839 wpa_s->pending_listen_duration);
3840 wpa_s->pending_listen_freq = 0;
3841 }
3842}
3843
3844
3845static int wpas_p2p_listen_start(struct wpa_supplicant *wpa_s,
3846 unsigned int timeout)
3847{
3848 /* Limit maximum Listen state time based on driver limitation. */
3849 if (timeout > wpa_s->max_remain_on_chan)
3850 timeout = wpa_s->max_remain_on_chan;
3851
3852 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
3853 return wpa_drv_p2p_listen(wpa_s, timeout);
3854
3855 return p2p_listen(wpa_s->global->p2p, timeout);
3856}
3857
3858
3859/**
3860 * wpas_p2p_cancel_remain_on_channel_cb - Remain-on-channel timeout
3861 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
3862 * @freq: Frequency of the channel in MHz
3863 *
3864 * This callback is called when the driver indicates that a remain-on-channel
3865 * operation has been completed, i.e., the duration on the requested channel
3866 * has timed out.
3867 */
3868void wpas_p2p_cancel_remain_on_channel_cb(struct wpa_supplicant *wpa_s,
3869 unsigned int freq)
3870{
3871 wpa_printf(MSG_DEBUG, "P2P: Cancel remain-on-channel callback "
3872 "(p2p_long_listen=%d ms pending_action_tx=%p)",
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08003873 wpa_s->p2p_long_listen, offchannel_pending_action_tx(wpa_s));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003874 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
3875 return;
3876 if (p2p_listen_end(wpa_s->global->p2p, freq) > 0)
3877 return; /* P2P module started a new operation */
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08003878 if (offchannel_pending_action_tx(wpa_s))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003879 return;
3880 if (wpa_s->p2p_long_listen > 0)
3881 wpa_s->p2p_long_listen -= wpa_s->max_remain_on_chan;
3882 if (wpa_s->p2p_long_listen > 0) {
3883 wpa_printf(MSG_DEBUG, "P2P: Continuing long Listen state");
3884 wpas_p2p_listen_start(wpa_s, wpa_s->p2p_long_listen);
3885 }
3886}
3887
3888
3889/**
3890 * wpas_p2p_group_remove - Remove a P2P group
3891 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
3892 * @ifname: Network interface name of the group interface or "*" to remove all
3893 * groups
3894 * Returns: 0 on success, -1 on failure
3895 *
3896 * This function is used to remove a P2P group. This can be used to disconnect
3897 * from a group in which the local end is a P2P Client or to end a P2P Group in
3898 * case the local end is the Group Owner. If a virtual network interface was
3899 * created for this group, that interface will be removed. Otherwise, only the
3900 * configured P2P group network will be removed from the interface.
3901 */
3902int wpas_p2p_group_remove(struct wpa_supplicant *wpa_s, const char *ifname)
3903{
3904 struct wpa_global *global = wpa_s->global;
3905
3906 if (os_strcmp(ifname, "*") == 0) {
3907 struct wpa_supplicant *prev;
3908 wpa_s = global->ifaces;
3909 while (wpa_s) {
3910 prev = wpa_s;
3911 wpa_s = wpa_s->next;
3912 wpas_p2p_disconnect(prev);
3913 }
3914 return 0;
3915 }
3916
3917 for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
3918 if (os_strcmp(wpa_s->ifname, ifname) == 0)
3919 break;
3920 }
3921
3922 return wpas_p2p_disconnect(wpa_s);
3923}
3924
3925
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003926static int wpas_p2p_init_go_params(struct wpa_supplicant *wpa_s,
3927 struct p2p_go_neg_results *params,
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003928 int freq, int ht40)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003929{
3930 u8 bssid[ETH_ALEN];
3931 int res;
3932
3933 os_memset(params, 0, sizeof(*params));
3934 params->role_go = 1;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003935 params->ht40 = ht40;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003936 if (freq) {
3937 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on forced "
3938 "frequency %d MHz", freq);
3939 params->freq = freq;
3940 } else if (wpa_s->conf->p2p_oper_reg_class == 81 &&
3941 wpa_s->conf->p2p_oper_channel >= 1 &&
3942 wpa_s->conf->p2p_oper_channel <= 11) {
3943 params->freq = 2407 + 5 * wpa_s->conf->p2p_oper_channel;
3944 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on configured "
3945 "frequency %d MHz", params->freq);
3946 } else if (wpa_s->conf->p2p_oper_reg_class == 115 ||
Jouni Malinen87fd2792011-05-16 18:35:42 +03003947 wpa_s->conf->p2p_oper_reg_class == 124) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003948 params->freq = 5000 + 5 * wpa_s->conf->p2p_oper_channel;
3949 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on configured "
3950 "frequency %d MHz", params->freq);
3951 } else if (wpa_s->conf->p2p_oper_channel == 0 &&
3952 wpa_s->best_overall_freq > 0 &&
3953 p2p_supported_freq(wpa_s->global->p2p,
3954 wpa_s->best_overall_freq)) {
3955 params->freq = wpa_s->best_overall_freq;
3956 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on best overall "
3957 "channel %d MHz", params->freq);
3958 } else if (wpa_s->conf->p2p_oper_channel == 0 &&
3959 wpa_s->best_24_freq > 0 &&
3960 p2p_supported_freq(wpa_s->global->p2p,
3961 wpa_s->best_24_freq)) {
3962 params->freq = wpa_s->best_24_freq;
3963 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on best 2.4 GHz "
3964 "channel %d MHz", params->freq);
3965 } else if (wpa_s->conf->p2p_oper_channel == 0 &&
3966 wpa_s->best_5_freq > 0 &&
3967 p2p_supported_freq(wpa_s->global->p2p,
3968 wpa_s->best_5_freq)) {
3969 params->freq = wpa_s->best_5_freq;
3970 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on best 5 GHz "
3971 "channel %d MHz", params->freq);
3972 } else {
Dmitry Shmidt04949592012-07-19 12:16:46 -07003973 int chan;
3974 for (chan = 0; chan < 11; chan++) {
3975 params->freq = 2412 + chan * 5;
3976 if (!wpas_p2p_disallowed_freq(wpa_s->global,
3977 params->freq))
3978 break;
3979 }
3980 if (chan == 11) {
3981 wpa_printf(MSG_DEBUG, "P2P: No 2.4 GHz channel "
3982 "allowed");
3983 return -1;
3984 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003985 wpa_printf(MSG_DEBUG, "P2P: Set GO freq %d MHz (no preference "
3986 "known)", params->freq);
3987 }
3988
3989 if (wpa_s->current_ssid && wpa_drv_get_bssid(wpa_s, bssid) == 0 &&
3990 wpa_s->assoc_freq && !freq) {
3991 wpa_printf(MSG_DEBUG, "P2P: Force GO on the channel we are "
3992 "already using");
3993 params->freq = wpa_s->assoc_freq;
3994 }
3995
3996 res = wpa_drv_shared_freq(wpa_s);
3997 if (res > 0 && !freq) {
3998 wpa_printf(MSG_DEBUG, "P2P: Force GO on the channel we are "
3999 "already using on a shared interface");
4000 params->freq = res;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004001 } else if (res > 0 && freq != res &&
4002 !(wpa_s->drv_flags &
4003 WPA_DRIVER_FLAGS_MULTI_CHANNEL_CONCURRENT)) {
4004 wpa_printf(MSG_DEBUG, "P2P: Cannot start P2P group on %u MHz "
4005 "while connected on another channel (%u MHz)",
4006 freq, res);
4007 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004008 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004009
4010 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004011}
4012
4013
4014static struct wpa_supplicant *
4015wpas_p2p_get_group_iface(struct wpa_supplicant *wpa_s, int addr_allocated,
4016 int go)
4017{
4018 struct wpa_supplicant *group_wpa_s;
4019
Dmitry Shmidtaa532512012-09-24 10:35:31 -07004020 if (!wpas_p2p_create_iface(wpa_s)) {
4021 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Use same interface for group "
4022 "operations");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004023 return wpa_s;
Dmitry Shmidtaa532512012-09-24 10:35:31 -07004024 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004025
4026 if (wpas_p2p_add_group_interface(wpa_s, go ? WPA_IF_P2P_GO :
Dmitry Shmidtaa532512012-09-24 10:35:31 -07004027 WPA_IF_P2P_CLIENT) < 0) {
4028 wpa_msg(wpa_s, MSG_ERROR, "P2P: Failed to add group interface");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004029 return NULL;
Dmitry Shmidtaa532512012-09-24 10:35:31 -07004030 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004031 group_wpa_s = wpas_p2p_init_group_interface(wpa_s, go);
4032 if (group_wpa_s == NULL) {
Dmitry Shmidtaa532512012-09-24 10:35:31 -07004033 wpa_msg(wpa_s, MSG_ERROR, "P2P: Failed to initialize group "
4034 "interface");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004035 wpas_p2p_remove_pending_group_interface(wpa_s);
4036 return NULL;
4037 }
4038
Dmitry Shmidtaa532512012-09-24 10:35:31 -07004039 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Use separate group interface %s",
4040 group_wpa_s->ifname);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004041 return group_wpa_s;
4042}
4043
4044
4045/**
4046 * wpas_p2p_group_add - Add a new P2P group with local end as Group Owner
4047 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
4048 * @persistent_group: Whether to create a persistent group
4049 * @freq: Frequency for the group or 0 to indicate no hardcoding
4050 * Returns: 0 on success, -1 on failure
4051 *
4052 * This function creates a new P2P group with the local end as the Group Owner,
4053 * i.e., without using Group Owner Negotiation.
4054 */
4055int wpas_p2p_group_add(struct wpa_supplicant *wpa_s, int persistent_group,
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004056 int freq, int ht40)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004057{
4058 struct p2p_go_neg_results params;
4059 unsigned int r;
4060
4061 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
4062 return -1;
4063
Dmitry Shmidtdca39792011-09-06 11:17:33 -07004064 /* Make sure we are not running find during connection establishment */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004065 wpa_printf(MSG_DEBUG, "P2P: Stop any on-going P2P FIND");
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08004066 wpas_p2p_stop_find_oper(wpa_s);
Dmitry Shmidtdca39792011-09-06 11:17:33 -07004067
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004068 if (freq == 2) {
4069 wpa_printf(MSG_DEBUG, "P2P: Request to start GO on 2.4 GHz "
4070 "band");
4071 if (wpa_s->best_24_freq > 0 &&
4072 p2p_supported_freq(wpa_s->global->p2p,
4073 wpa_s->best_24_freq)) {
4074 freq = wpa_s->best_24_freq;
4075 wpa_printf(MSG_DEBUG, "P2P: Use best 2.4 GHz band "
4076 "channel: %d MHz", freq);
4077 } else {
4078 os_get_random((u8 *) &r, sizeof(r));
4079 freq = 2412 + (r % 3) * 25;
4080 wpa_printf(MSG_DEBUG, "P2P: Use random 2.4 GHz band "
4081 "channel: %d MHz", freq);
4082 }
4083 }
4084
4085 if (freq == 5) {
4086 wpa_printf(MSG_DEBUG, "P2P: Request to start GO on 5 GHz "
4087 "band");
4088 if (wpa_s->best_5_freq > 0 &&
4089 p2p_supported_freq(wpa_s->global->p2p,
4090 wpa_s->best_5_freq)) {
4091 freq = wpa_s->best_5_freq;
4092 wpa_printf(MSG_DEBUG, "P2P: Use best 5 GHz band "
4093 "channel: %d MHz", freq);
4094 } else {
4095 os_get_random((u8 *) &r, sizeof(r));
4096 freq = 5180 + (r % 4) * 20;
4097 if (!p2p_supported_freq(wpa_s->global->p2p, freq)) {
4098 wpa_printf(MSG_DEBUG, "P2P: Could not select "
4099 "5 GHz channel for P2P group");
4100 return -1;
4101 }
4102 wpa_printf(MSG_DEBUG, "P2P: Use random 5 GHz band "
4103 "channel: %d MHz", freq);
4104 }
4105 }
4106
4107 if (freq > 0 && !p2p_supported_freq(wpa_s->global->p2p, freq)) {
4108 wpa_printf(MSG_DEBUG, "P2P: The forced channel for GO "
4109 "(%u MHz) is not supported for P2P uses",
4110 freq);
4111 return -1;
4112 }
4113
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004114 if (wpas_p2p_init_go_params(wpa_s, &params, freq, ht40))
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004115 return -1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07004116 if (params.freq &&
4117 !p2p_supported_freq(wpa_s->global->p2p, params.freq)) {
4118 wpa_printf(MSG_DEBUG, "P2P: The selected channel for GO "
4119 "(%u MHz) is not supported for P2P uses",
4120 params.freq);
4121 return -1;
4122 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004123 p2p_go_params(wpa_s->global->p2p, &params);
4124 params.persistent_group = persistent_group;
4125
4126 wpa_s = wpas_p2p_get_group_iface(wpa_s, 0, 1);
4127 if (wpa_s == NULL)
4128 return -1;
4129 wpas_start_wps_go(wpa_s, &params, 0);
4130
4131 return 0;
4132}
4133
4134
4135static int wpas_start_p2p_client(struct wpa_supplicant *wpa_s,
4136 struct wpa_ssid *params, int addr_allocated)
4137{
4138 struct wpa_ssid *ssid;
4139
4140 wpa_s = wpas_p2p_get_group_iface(wpa_s, addr_allocated, 0);
4141 if (wpa_s == NULL)
4142 return -1;
4143
4144 wpa_supplicant_ap_deinit(wpa_s);
4145
4146 ssid = wpa_config_add_network(wpa_s->conf);
4147 if (ssid == NULL)
4148 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004149 wpa_config_set_network_defaults(ssid);
4150 ssid->temporary = 1;
4151 ssid->proto = WPA_PROTO_RSN;
4152 ssid->pairwise_cipher = WPA_CIPHER_CCMP;
4153 ssid->group_cipher = WPA_CIPHER_CCMP;
4154 ssid->key_mgmt = WPA_KEY_MGMT_PSK;
4155 ssid->ssid = os_malloc(params->ssid_len);
4156 if (ssid->ssid == NULL) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004157 wpa_config_remove_network(wpa_s->conf, ssid->id);
4158 return -1;
4159 }
4160 os_memcpy(ssid->ssid, params->ssid, params->ssid_len);
4161 ssid->ssid_len = params->ssid_len;
4162 ssid->p2p_group = 1;
4163 ssid->export_keys = 1;
4164 if (params->psk_set) {
4165 os_memcpy(ssid->psk, params->psk, 32);
4166 ssid->psk_set = 1;
4167 }
4168 if (params->passphrase)
4169 ssid->passphrase = os_strdup(params->passphrase);
4170
4171 wpa_supplicant_select_network(wpa_s, ssid);
4172
4173 wpa_s->show_group_started = 1;
4174
4175 return 0;
4176}
4177
4178
4179int wpas_p2p_group_add_persistent(struct wpa_supplicant *wpa_s,
4180 struct wpa_ssid *ssid, int addr_allocated,
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004181 int freq, int ht40)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004182{
4183 struct p2p_go_neg_results params;
4184 int go = 0;
4185
4186 if (ssid->disabled != 2 || ssid->ssid == NULL)
4187 return -1;
4188
4189 if (wpas_get_p2p_group(wpa_s, ssid->ssid, ssid->ssid_len, &go) &&
4190 go == (ssid->mode == WPAS_MODE_P2P_GO)) {
4191 wpa_printf(MSG_DEBUG, "P2P: Requested persistent group is "
4192 "already running");
4193 return 0;
4194 }
4195
4196 /* Make sure we are not running find during connection establishment */
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08004197 wpas_p2p_stop_find_oper(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004198
Dmitry Shmidt04949592012-07-19 12:16:46 -07004199 wpa_s->p2p_fallback_to_go_neg = 0;
4200
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004201 if (ssid->mode == WPAS_MODE_INFRA)
4202 return wpas_start_p2p_client(wpa_s, ssid, addr_allocated);
4203
4204 if (ssid->mode != WPAS_MODE_P2P_GO)
4205 return -1;
4206
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004207 if (wpas_p2p_init_go_params(wpa_s, &params, freq, ht40))
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004208 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004209
4210 params.role_go = 1;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08004211 params.psk_set = ssid->psk_set;
4212 if (params.psk_set)
4213 os_memcpy(params.psk, ssid->psk, sizeof(params.psk));
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08004214 if (ssid->passphrase) {
4215 if (os_strlen(ssid->passphrase) >= sizeof(params.passphrase)) {
4216 wpa_printf(MSG_ERROR, "P2P: Invalid passphrase in "
4217 "persistent group");
4218 return -1;
4219 }
4220 os_strlcpy(params.passphrase, ssid->passphrase,
4221 sizeof(params.passphrase));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004222 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004223 os_memcpy(params.ssid, ssid->ssid, ssid->ssid_len);
4224 params.ssid_len = ssid->ssid_len;
4225 params.persistent_group = 1;
4226
4227 wpa_s = wpas_p2p_get_group_iface(wpa_s, addr_allocated, 1);
4228 if (wpa_s == NULL)
4229 return -1;
4230
4231 wpas_start_wps_go(wpa_s, &params, 0);
4232
4233 return 0;
4234}
4235
4236
4237static void wpas_p2p_ie_update(void *ctx, struct wpabuf *beacon_ies,
4238 struct wpabuf *proberesp_ies)
4239{
4240 struct wpa_supplicant *wpa_s = ctx;
4241 if (wpa_s->ap_iface) {
4242 struct hostapd_data *hapd = wpa_s->ap_iface->bss[0];
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004243 if (!(hapd->conf->p2p & P2P_GROUP_OWNER)) {
4244 wpabuf_free(beacon_ies);
4245 wpabuf_free(proberesp_ies);
4246 return;
4247 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004248 if (beacon_ies) {
4249 wpabuf_free(hapd->p2p_beacon_ie);
4250 hapd->p2p_beacon_ie = beacon_ies;
4251 }
4252 wpabuf_free(hapd->p2p_probe_resp_ie);
4253 hapd->p2p_probe_resp_ie = proberesp_ies;
4254 } else {
4255 wpabuf_free(beacon_ies);
4256 wpabuf_free(proberesp_ies);
4257 }
4258 wpa_supplicant_ap_update_beacon(wpa_s);
4259}
4260
4261
4262static void wpas_p2p_idle_update(void *ctx, int idle)
4263{
4264 struct wpa_supplicant *wpa_s = ctx;
4265 if (!wpa_s->ap_iface)
4266 return;
4267 wpa_printf(MSG_DEBUG, "P2P: GO - group %sidle", idle ? "" : "not ");
Dmitry Shmidt04949592012-07-19 12:16:46 -07004268 if (idle)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004269 wpas_p2p_set_group_idle_timeout(wpa_s);
4270 else
4271 eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL);
4272}
4273
4274
4275struct p2p_group * wpas_p2p_group_init(struct wpa_supplicant *wpa_s,
Dmitry Shmidt04949592012-07-19 12:16:46 -07004276 struct wpa_ssid *ssid)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004277{
4278 struct p2p_group *group;
4279 struct p2p_group_config *cfg;
4280
4281 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
4282 return NULL;
4283 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
4284 return NULL;
4285
4286 cfg = os_zalloc(sizeof(*cfg));
4287 if (cfg == NULL)
4288 return NULL;
4289
Dmitry Shmidt04949592012-07-19 12:16:46 -07004290 if (ssid->p2p_persistent_group && wpa_s->conf->persistent_reconnect)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004291 cfg->persistent_group = 2;
Dmitry Shmidt04949592012-07-19 12:16:46 -07004292 else if (ssid->p2p_persistent_group)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004293 cfg->persistent_group = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004294 os_memcpy(cfg->interface_addr, wpa_s->own_addr, ETH_ALEN);
4295 if (wpa_s->max_stations &&
4296 wpa_s->max_stations < wpa_s->conf->max_num_sta)
4297 cfg->max_clients = wpa_s->max_stations;
4298 else
4299 cfg->max_clients = wpa_s->conf->max_num_sta;
Dmitry Shmidt04949592012-07-19 12:16:46 -07004300 os_memcpy(cfg->ssid, ssid->ssid, ssid->ssid_len);
4301 cfg->ssid_len = ssid->ssid_len;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004302 cfg->cb_ctx = wpa_s;
4303 cfg->ie_update = wpas_p2p_ie_update;
4304 cfg->idle_update = wpas_p2p_idle_update;
4305
4306 group = p2p_group_init(wpa_s->global->p2p, cfg);
4307 if (group == NULL)
4308 os_free(cfg);
Dmitry Shmidt04949592012-07-19 12:16:46 -07004309 if (ssid->mode != WPAS_MODE_P2P_GROUP_FORMATION)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004310 p2p_group_notif_formation_done(group);
4311 wpa_s->p2p_group = group;
4312 return group;
4313}
4314
4315
4316void wpas_p2p_wps_success(struct wpa_supplicant *wpa_s, const u8 *peer_addr,
4317 int registrar)
4318{
Dmitry Shmidt04949592012-07-19 12:16:46 -07004319 struct wpa_ssid *ssid = wpa_s->current_ssid;
4320
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004321 if (!wpa_s->p2p_in_provisioning) {
4322 wpa_printf(MSG_DEBUG, "P2P: Ignore WPS success event - P2P "
4323 "provisioning not in progress");
4324 return;
4325 }
4326
Dmitry Shmidt04949592012-07-19 12:16:46 -07004327 if (ssid && ssid->mode == WPAS_MODE_INFRA) {
4328 u8 go_dev_addr[ETH_ALEN];
4329 os_memcpy(go_dev_addr, wpa_s->bssid, ETH_ALEN);
4330 wpas_p2p_persistent_group(wpa_s, go_dev_addr, ssid->ssid,
4331 ssid->ssid_len);
4332 /* Clear any stored provisioning info */
4333 p2p_clear_provisioning_info(wpa_s->global->p2p, go_dev_addr);
4334 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004335
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004336 eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s->parent,
4337 NULL);
Dmitry Shmidt04949592012-07-19 12:16:46 -07004338 if (ssid && ssid->mode == WPAS_MODE_INFRA) {
4339 /*
4340 * Use a separate timeout for initial data connection to
4341 * complete to allow the group to be removed automatically if
4342 * something goes wrong in this step before the P2P group idle
4343 * timeout mechanism is taken into use.
4344 */
4345 eloop_register_timeout(P2P_MAX_INITIAL_CONN_WAIT, 0,
4346 wpas_p2p_group_formation_timeout,
4347 wpa_s->parent, NULL);
4348 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004349 if (wpa_s->global->p2p)
4350 p2p_wps_success_cb(wpa_s->global->p2p, peer_addr);
4351 else if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
4352 wpa_drv_wps_success_cb(wpa_s, peer_addr);
4353 wpas_group_formation_completed(wpa_s, 1);
4354}
4355
4356
Jouni Malinen75ecf522011-06-27 15:19:46 -07004357void wpas_p2p_wps_failed(struct wpa_supplicant *wpa_s,
4358 struct wps_event_fail *fail)
4359{
4360 if (!wpa_s->p2p_in_provisioning) {
4361 wpa_printf(MSG_DEBUG, "P2P: Ignore WPS fail event - P2P "
4362 "provisioning not in progress");
4363 return;
4364 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004365
4366 if (wpa_s->go_params) {
4367 p2p_clear_provisioning_info(
4368 wpa_s->global->p2p,
Dmitry Shmidt04949592012-07-19 12:16:46 -07004369 wpa_s->go_params->peer_device_addr);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004370 }
4371
Jouni Malinen75ecf522011-06-27 15:19:46 -07004372 wpas_notify_p2p_wps_failed(wpa_s, fail);
4373}
4374
4375
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004376int wpas_p2p_prov_disc(struct wpa_supplicant *wpa_s, const u8 *peer_addr,
Dmitry Shmidt04949592012-07-19 12:16:46 -07004377 const char *config_method,
4378 enum wpas_p2p_prov_disc_use use)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004379{
4380 u16 config_methods;
4381
Dmitry Shmidt04949592012-07-19 12:16:46 -07004382 wpa_s->p2p_fallback_to_go_neg = 0;
4383 wpa_s->pending_pd_use = NORMAL_PD;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004384 if (os_strncmp(config_method, "display", 7) == 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004385 config_methods = WPS_CONFIG_DISPLAY;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004386 else if (os_strncmp(config_method, "keypad", 6) == 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004387 config_methods = WPS_CONFIG_KEYPAD;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004388 else if (os_strncmp(config_method, "pbc", 3) == 0 ||
4389 os_strncmp(config_method, "pushbutton", 10) == 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004390 config_methods = WPS_CONFIG_PUSHBUTTON;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004391 else {
4392 wpa_printf(MSG_DEBUG, "P2P: Unknown config method");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004393 return -1;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004394 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004395
Dmitry Shmidt04949592012-07-19 12:16:46 -07004396 if (use == WPAS_P2P_PD_AUTO) {
4397 os_memcpy(wpa_s->pending_join_dev_addr, peer_addr, ETH_ALEN);
4398 wpa_s->pending_pd_config_methods = config_methods;
4399 wpa_s->p2p_auto_pd = 1;
4400 wpa_s->p2p_auto_join = 0;
4401 wpa_s->pending_pd_before_join = 0;
4402 wpa_s->auto_pd_scan_retry = 0;
4403 wpas_p2p_stop_find(wpa_s);
4404 wpa_s->p2p_join_scan_count = 0;
4405 os_get_time(&wpa_s->p2p_auto_started);
4406 wpa_printf(MSG_DEBUG, "P2P: Auto PD started at %ld.%06ld",
4407 wpa_s->p2p_auto_started.sec,
4408 wpa_s->p2p_auto_started.usec);
4409 wpas_p2p_join_scan(wpa_s, NULL);
4410 return 0;
4411 }
4412
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004413 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT) {
4414 return wpa_drv_p2p_prov_disc_req(wpa_s, peer_addr,
Dmitry Shmidt04949592012-07-19 12:16:46 -07004415 config_methods,
4416 use == WPAS_P2P_PD_FOR_JOIN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004417 }
4418
4419 if (wpa_s->global->p2p == NULL || wpa_s->global->p2p_disabled)
4420 return -1;
4421
4422 return p2p_prov_disc_req(wpa_s->global->p2p, peer_addr,
Dmitry Shmidt04949592012-07-19 12:16:46 -07004423 config_methods, use == WPAS_P2P_PD_FOR_JOIN,
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08004424 0, 1);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004425}
4426
4427
4428int wpas_p2p_scan_result_text(const u8 *ies, size_t ies_len, char *buf,
4429 char *end)
4430{
4431 return p2p_scan_result_text(ies, ies_len, buf, end);
4432}
4433
4434
4435static void wpas_p2p_clear_pending_action_tx(struct wpa_supplicant *wpa_s)
4436{
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08004437 if (!offchannel_pending_action_tx(wpa_s))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004438 return;
4439
4440 wpa_printf(MSG_DEBUG, "P2P: Drop pending Action TX due to new "
4441 "operation request");
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08004442 offchannel_clear_pending_action_tx(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004443}
4444
4445
4446int wpas_p2p_find(struct wpa_supplicant *wpa_s, unsigned int timeout,
4447 enum p2p_discovery_type type,
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08004448 unsigned int num_req_dev_types, const u8 *req_dev_types,
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004449 const u8 *dev_id, unsigned int search_delay)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004450{
4451 wpas_p2p_clear_pending_action_tx(wpa_s);
4452 wpa_s->p2p_long_listen = 0;
4453
4454 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
4455 return wpa_drv_p2p_find(wpa_s, timeout, type);
4456
Dmitry Shmidt04949592012-07-19 12:16:46 -07004457 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL ||
4458 wpa_s->p2p_in_provisioning)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004459 return -1;
4460
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08004461 wpa_supplicant_cancel_sched_scan(wpa_s);
4462
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004463 return p2p_find(wpa_s->global->p2p, timeout, type,
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004464 num_req_dev_types, req_dev_types, dev_id,
4465 search_delay);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004466}
4467
4468
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08004469static int wpas_p2p_stop_find_oper(struct wpa_supplicant *wpa_s)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004470{
4471 wpas_p2p_clear_pending_action_tx(wpa_s);
4472 wpa_s->p2p_long_listen = 0;
4473 eloop_cancel_timeout(wpas_p2p_long_listen_timeout, wpa_s, NULL);
4474 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
Jouni Malinendc7b7132012-09-14 12:53:47 -07004475 wpa_s->global->p2p_cb_on_scan_complete = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004476
4477 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT) {
4478 wpa_drv_p2p_stop_find(wpa_s);
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08004479 return 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004480 }
4481
4482 if (wpa_s->global->p2p)
4483 p2p_stop_find(wpa_s->global->p2p);
4484
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08004485 return 0;
4486}
4487
4488
4489void wpas_p2p_stop_find(struct wpa_supplicant *wpa_s)
4490{
4491 if (wpas_p2p_stop_find_oper(wpa_s) > 0)
4492 return;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004493 wpas_p2p_remove_pending_group_interface(wpa_s);
4494}
4495
4496
4497static void wpas_p2p_long_listen_timeout(void *eloop_ctx, void *timeout_ctx)
4498{
4499 struct wpa_supplicant *wpa_s = eloop_ctx;
4500 wpa_s->p2p_long_listen = 0;
4501}
4502
4503
4504int wpas_p2p_listen(struct wpa_supplicant *wpa_s, unsigned int timeout)
4505{
4506 int res;
4507
4508 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
4509 return -1;
4510
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08004511 wpa_supplicant_cancel_sched_scan(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004512 wpas_p2p_clear_pending_action_tx(wpa_s);
4513
4514 if (timeout == 0) {
4515 /*
4516 * This is a request for unlimited Listen state. However, at
4517 * least for now, this is mapped to a Listen state for one
4518 * hour.
4519 */
4520 timeout = 3600;
4521 }
4522 eloop_cancel_timeout(wpas_p2p_long_listen_timeout, wpa_s, NULL);
4523 wpa_s->p2p_long_listen = 0;
4524
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004525 /*
4526 * Stop previous find/listen operation to avoid trying to request a new
4527 * remain-on-channel operation while the driver is still running the
4528 * previous one.
4529 */
4530 if (wpa_s->global->p2p)
4531 p2p_stop_find(wpa_s->global->p2p);
4532
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004533 res = wpas_p2p_listen_start(wpa_s, timeout * 1000);
4534 if (res == 0 && timeout * 1000 > wpa_s->max_remain_on_chan) {
4535 wpa_s->p2p_long_listen = timeout * 1000;
4536 eloop_register_timeout(timeout, 0,
4537 wpas_p2p_long_listen_timeout,
4538 wpa_s, NULL);
4539 }
4540
4541 return res;
4542}
4543
4544
4545int wpas_p2p_assoc_req_ie(struct wpa_supplicant *wpa_s, struct wpa_bss *bss,
4546 u8 *buf, size_t len, int p2p_group)
4547{
4548 struct wpabuf *p2p_ie;
4549 int ret;
4550
4551 if (wpa_s->global->p2p_disabled)
4552 return -1;
4553 if (wpa_s->global->p2p == NULL)
4554 return -1;
4555 if (bss == NULL)
4556 return -1;
4557
4558 p2p_ie = wpa_bss_get_vendor_ie_multi(bss, P2P_IE_VENDOR_TYPE);
4559 ret = p2p_assoc_req_ie(wpa_s->global->p2p, bss->bssid, buf, len,
4560 p2p_group, p2p_ie);
4561 wpabuf_free(p2p_ie);
4562
4563 return ret;
4564}
4565
4566
4567int wpas_p2p_probe_req_rx(struct wpa_supplicant *wpa_s, const u8 *addr,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004568 const u8 *dst, const u8 *bssid,
Dmitry Shmidt04949592012-07-19 12:16:46 -07004569 const u8 *ie, size_t ie_len, int ssi_signal)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004570{
4571 if (wpa_s->global->p2p_disabled)
4572 return 0;
4573 if (wpa_s->global->p2p == NULL)
4574 return 0;
4575
Dmitry Shmidt04949592012-07-19 12:16:46 -07004576 switch (p2p_probe_req_rx(wpa_s->global->p2p, addr, dst, bssid,
4577 ie, ie_len)) {
4578 case P2P_PREQ_NOT_P2P:
4579 wpas_notify_preq(wpa_s, addr, dst, bssid, ie, ie_len,
4580 ssi_signal);
4581 /* fall through */
4582 case P2P_PREQ_MALFORMED:
4583 case P2P_PREQ_NOT_LISTEN:
4584 case P2P_PREQ_NOT_PROCESSED:
4585 default: /* make gcc happy */
4586 return 0;
4587 case P2P_PREQ_PROCESSED:
4588 return 1;
4589 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004590}
4591
4592
4593void wpas_p2p_rx_action(struct wpa_supplicant *wpa_s, const u8 *da,
4594 const u8 *sa, const u8 *bssid,
4595 u8 category, const u8 *data, size_t len, int freq)
4596{
4597 if (wpa_s->global->p2p_disabled)
4598 return;
4599 if (wpa_s->global->p2p == NULL)
4600 return;
4601
4602 p2p_rx_action(wpa_s->global->p2p, da, sa, bssid, category, data, len,
4603 freq);
4604}
4605
4606
4607void wpas_p2p_scan_ie(struct wpa_supplicant *wpa_s, struct wpabuf *ies)
4608{
4609 if (wpa_s->global->p2p_disabled)
4610 return;
4611 if (wpa_s->global->p2p == NULL)
4612 return;
4613
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08004614 p2p_scan_ie(wpa_s->global->p2p, ies, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004615}
4616
4617
4618void wpas_p2p_group_deinit(struct wpa_supplicant *wpa_s)
4619{
4620 p2p_group_deinit(wpa_s->p2p_group);
4621 wpa_s->p2p_group = NULL;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004622
4623 wpa_s->ap_configured_cb = NULL;
4624 wpa_s->ap_configured_cb_ctx = NULL;
4625 wpa_s->ap_configured_cb_data = NULL;
4626 wpa_s->connect_without_scan = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004627}
4628
4629
4630int wpas_p2p_reject(struct wpa_supplicant *wpa_s, const u8 *addr)
4631{
4632 wpa_s->p2p_long_listen = 0;
4633
4634 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
4635 return wpa_drv_p2p_reject(wpa_s, addr);
4636
4637 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
4638 return -1;
4639
4640 return p2p_reject(wpa_s->global->p2p, addr);
4641}
4642
4643
4644/* Invite to reinvoke a persistent group */
4645int wpas_p2p_invite(struct wpa_supplicant *wpa_s, const u8 *peer_addr,
Jouni Malinen31be0a42012-08-31 21:20:51 +03004646 struct wpa_ssid *ssid, const u8 *go_dev_addr, int freq,
4647 int ht40)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004648{
4649 enum p2p_invite_role role;
4650 u8 *bssid = NULL;
Irfan Sherifff0619662012-09-23 19:11:37 -07004651#ifdef ANDROID_P2P
Irfan Sheriffaf84a572012-09-22 16:59:30 -07004652 int force_freq = 0, oper_freq = 0;
Irfan Sherifff0619662012-09-23 19:11:37 -07004653#endif
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004654
Jouni Malinen31be0a42012-08-31 21:20:51 +03004655 wpa_s->p2p_persistent_go_freq = freq;
4656 wpa_s->p2p_go_ht40 = !!ht40;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004657 if (ssid->mode == WPAS_MODE_P2P_GO) {
4658 role = P2P_INVITE_ROLE_GO;
4659 if (peer_addr == NULL) {
4660 wpa_printf(MSG_DEBUG, "P2P: Missing peer "
4661 "address in invitation command");
4662 return -1;
4663 }
4664 if (wpas_p2p_create_iface(wpa_s)) {
4665 if (wpas_p2p_add_group_interface(wpa_s,
4666 WPA_IF_P2P_GO) < 0) {
4667 wpa_printf(MSG_ERROR, "P2P: Failed to "
4668 "allocate a new interface for the "
4669 "group");
4670 return -1;
4671 }
4672 bssid = wpa_s->pending_interface_addr;
4673 } else
4674 bssid = wpa_s->own_addr;
4675 } else {
4676 role = P2P_INVITE_ROLE_CLIENT;
4677 peer_addr = ssid->bssid;
4678 }
4679 wpa_s->pending_invite_ssid_id = ssid->id;
4680
Irfan Sherifff0619662012-09-23 19:11:37 -07004681#ifdef ANDROID_P2P
Irfan Sheriffaf84a572012-09-22 16:59:30 -07004682 if (wpa_s->current_ssid && wpa_drv_get_bssid(wpa_s, bssid) == 0 &&
4683 wpa_s->assoc_freq)
4684 oper_freq = wpa_s->assoc_freq;
4685 else {
4686 oper_freq = wpa_drv_shared_freq(wpa_s);
4687 if (oper_freq < 0)
4688 oper_freq = 0;
4689 }
4690
4691 if (freq > 0) {
4692 if (!p2p_supported_freq(wpa_s->global->p2p, freq)) {
4693 wpa_printf(MSG_DEBUG, "P2P: The forced channel "
4694 "(%u MHz) is not supported for P2P uses",
4695 freq);
4696 return -3;
4697 }
4698
4699 if (oper_freq > 0 && freq != oper_freq &&
4700 !(wpa_s->drv_flags &
4701 WPA_DRIVER_FLAGS_MULTI_CHANNEL_CONCURRENT)) {
4702 wpa_printf(MSG_DEBUG, "P2P: Cannot start P2P group "
4703 "on %u MHz while connected on another "
4704 "channel (%u MHz)", freq, oper_freq);
4705 return -2;
4706 }
4707 wpa_printf(MSG_DEBUG, "P2P: Trying to force us to use the "
4708 "requested channel (%u MHz)", freq);
4709 force_freq = freq;
4710 } else if (oper_freq > 0 &&
4711 !p2p_supported_freq(wpa_s->global->p2p, oper_freq)) {
4712 if (!(wpa_s->drv_flags &
4713 WPA_DRIVER_FLAGS_MULTI_CHANNEL_CONCURRENT)) {
4714 wpa_printf(MSG_DEBUG, "P2P: Cannot start P2P group "
4715 "while connected on non-P2P supported "
4716 "channel (%u MHz)", oper_freq);
4717 return -2;
4718 }
4719 wpa_printf(MSG_DEBUG, "P2P: Current operating channel "
4720 "(%u MHz) not available for P2P - try to use "
4721 "another channel", oper_freq);
4722 force_freq = 0;
4723 } else if (oper_freq > 0) {
4724 wpa_printf(MSG_DEBUG, "P2P: Trying to force us to use the "
4725 "channel we are already using (%u MHz) on another "
4726 "interface", oper_freq);
4727 force_freq = oper_freq;
4728 }
Irfan Sherifff0619662012-09-23 19:11:37 -07004729#endif
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004730 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
4731 return wpa_drv_p2p_invite(wpa_s, peer_addr, role, bssid,
4732 ssid->ssid, ssid->ssid_len,
4733 go_dev_addr, 1);
4734
4735 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
4736 return -1;
4737
Irfan Sherifff0619662012-09-23 19:11:37 -07004738#ifdef ANDROID_P2P
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004739 return p2p_invite(wpa_s->global->p2p, peer_addr, role, bssid,
Irfan Sheriffaf84a572012-09-22 16:59:30 -07004740 ssid->ssid, ssid->ssid_len, force_freq, go_dev_addr, 1);
Irfan Sherifff0619662012-09-23 19:11:37 -07004741#else
4742 return p2p_invite(wpa_s->global->p2p, peer_addr, role, bssid,
4743 ssid->ssid, ssid->ssid_len, freq, go_dev_addr, 1);
4744#endif
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004745}
4746
4747
4748/* Invite to join an active group */
4749int wpas_p2p_invite_group(struct wpa_supplicant *wpa_s, const char *ifname,
4750 const u8 *peer_addr, const u8 *go_dev_addr)
4751{
4752 struct wpa_global *global = wpa_s->global;
4753 enum p2p_invite_role role;
4754 u8 *bssid = NULL;
4755 struct wpa_ssid *ssid;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08004756 int persistent;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004757
Jouni Malinen31be0a42012-08-31 21:20:51 +03004758 wpa_s->p2p_persistent_go_freq = 0;
4759 wpa_s->p2p_go_ht40 = 0;
4760
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004761 for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
4762 if (os_strcmp(wpa_s->ifname, ifname) == 0)
4763 break;
4764 }
4765 if (wpa_s == NULL) {
4766 wpa_printf(MSG_DEBUG, "P2P: Interface '%s' not found", ifname);
4767 return -1;
4768 }
4769
4770 ssid = wpa_s->current_ssid;
4771 if (ssid == NULL) {
4772 wpa_printf(MSG_DEBUG, "P2P: No current SSID to use for "
4773 "invitation");
4774 return -1;
4775 }
4776
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08004777 persistent = ssid->p2p_persistent_group &&
4778 wpas_p2p_get_persistent(wpa_s->parent, peer_addr,
4779 ssid->ssid, ssid->ssid_len);
4780
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004781 if (ssid->mode == WPAS_MODE_P2P_GO) {
4782 role = P2P_INVITE_ROLE_ACTIVE_GO;
4783 bssid = wpa_s->own_addr;
4784 if (go_dev_addr == NULL)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004785 go_dev_addr = wpa_s->global->p2p_dev_addr;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004786 } else {
4787 role = P2P_INVITE_ROLE_CLIENT;
4788 if (wpa_s->wpa_state < WPA_ASSOCIATED) {
4789 wpa_printf(MSG_DEBUG, "P2P: Not associated - cannot "
4790 "invite to current group");
4791 return -1;
4792 }
4793 bssid = wpa_s->bssid;
4794 if (go_dev_addr == NULL &&
4795 !is_zero_ether_addr(wpa_s->go_dev_addr))
4796 go_dev_addr = wpa_s->go_dev_addr;
4797 }
4798 wpa_s->parent->pending_invite_ssid_id = -1;
4799
4800 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
4801 return wpa_drv_p2p_invite(wpa_s, peer_addr, role, bssid,
4802 ssid->ssid, ssid->ssid_len,
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08004803 go_dev_addr, persistent);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004804
4805 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
4806 return -1;
4807
4808 return p2p_invite(wpa_s->global->p2p, peer_addr, role, bssid,
4809 ssid->ssid, ssid->ssid_len, wpa_s->assoc_freq,
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08004810 go_dev_addr, persistent);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004811}
4812
4813
4814void wpas_p2p_completed(struct wpa_supplicant *wpa_s)
4815{
4816 struct wpa_ssid *ssid = wpa_s->current_ssid;
4817 const char *ssid_txt;
4818 u8 go_dev_addr[ETH_ALEN];
Jouni Malinen75ecf522011-06-27 15:19:46 -07004819 int network_id = -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004820 int persistent;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004821 int freq;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004822
Dmitry Shmidt04949592012-07-19 12:16:46 -07004823 if (ssid == NULL || ssid->mode != WPAS_MODE_P2P_GROUP_FORMATION) {
4824 eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
4825 wpa_s->parent, NULL);
4826 }
4827
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004828 if (!wpa_s->show_group_started || !ssid)
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004829 goto done;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004830
4831 wpa_s->show_group_started = 0;
4832
4833 ssid_txt = wpa_ssid_txt(ssid->ssid, ssid->ssid_len);
4834 os_memset(go_dev_addr, 0, ETH_ALEN);
4835 if (ssid->bssid_set)
4836 os_memcpy(go_dev_addr, ssid->bssid, ETH_ALEN);
4837 persistent = wpas_p2p_persistent_group(wpa_s, go_dev_addr, ssid->ssid,
4838 ssid->ssid_len);
4839 os_memcpy(wpa_s->go_dev_addr, go_dev_addr, ETH_ALEN);
4840
4841 if (wpa_s->global->p2p_group_formation == wpa_s)
4842 wpa_s->global->p2p_group_formation = NULL;
4843
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004844 freq = wpa_s->current_bss ? wpa_s->current_bss->freq :
4845 (int) wpa_s->assoc_freq;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004846 if (ssid->passphrase == NULL && ssid->psk_set) {
4847 char psk[65];
4848 wpa_snprintf_hex(psk, sizeof(psk), ssid->psk, 32);
4849 wpa_msg(wpa_s->parent, MSG_INFO, P2P_EVENT_GROUP_STARTED
4850 "%s client ssid=\"%s\" freq=%d psk=%s go_dev_addr="
4851 MACSTR "%s",
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004852 wpa_s->ifname, ssid_txt, freq, psk,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004853 MAC2STR(go_dev_addr),
4854 persistent ? " [PERSISTENT]" : "");
4855 } else {
4856 wpa_msg(wpa_s->parent, MSG_INFO, P2P_EVENT_GROUP_STARTED
4857 "%s client ssid=\"%s\" freq=%d passphrase=\"%s\" "
4858 "go_dev_addr=" MACSTR "%s",
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004859 wpa_s->ifname, ssid_txt, freq,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004860 ssid->passphrase ? ssid->passphrase : "",
4861 MAC2STR(go_dev_addr),
4862 persistent ? " [PERSISTENT]" : "");
4863 }
4864
4865 if (persistent)
Jouni Malinen75ecf522011-06-27 15:19:46 -07004866 network_id = wpas_p2p_store_persistent_group(wpa_s->parent,
4867 ssid, go_dev_addr);
4868 if (network_id < 0)
4869 network_id = ssid->id;
4870 wpas_notify_p2p_group_started(wpa_s, ssid, network_id, 1);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004871
4872done:
Jouni Malinendc7b7132012-09-14 12:53:47 -07004873 if (wpa_s->global->p2p_cb_on_scan_complete && !wpa_s->global->p2p_disabled &&
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004874 wpa_s->global->p2p != NULL) {
Jouni Malinendc7b7132012-09-14 12:53:47 -07004875 wpa_s->global->p2p_cb_on_scan_complete = 0;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004876 if (p2p_other_scan_completed(wpa_s->global->p2p) == 1) {
4877 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Pending P2P operation "
4878 "continued after successful connection");
4879 p2p_increase_search_delay(
4880 wpa_s->global->p2p,
4881 wpas_p2p_search_delay(wpa_s));
4882 }
4883 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004884}
4885
4886
4887int wpas_p2p_presence_req(struct wpa_supplicant *wpa_s, u32 duration1,
4888 u32 interval1, u32 duration2, u32 interval2)
4889{
4890 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
4891 return -1;
4892 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
4893 return -1;
4894
4895 if (wpa_s->wpa_state < WPA_ASSOCIATED ||
4896 wpa_s->current_ssid == NULL ||
4897 wpa_s->current_ssid->mode != WPAS_MODE_INFRA)
4898 return -1;
4899
4900 return p2p_presence_req(wpa_s->global->p2p, wpa_s->bssid,
4901 wpa_s->own_addr, wpa_s->assoc_freq,
4902 duration1, interval1, duration2, interval2);
4903}
4904
4905
4906int wpas_p2p_ext_listen(struct wpa_supplicant *wpa_s, unsigned int period,
4907 unsigned int interval)
4908{
4909 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
4910 return -1;
4911
4912 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
4913 return -1;
4914
4915 return p2p_ext_listen(wpa_s->global->p2p, period, interval);
4916}
4917
4918
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004919static int wpas_p2p_is_client(struct wpa_supplicant *wpa_s)
4920{
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08004921 if (wpa_s->current_ssid == NULL) {
4922 /*
4923 * current_ssid can be cleared when P2P client interface gets
4924 * disconnected, so assume this interface was used as P2P
4925 * client.
4926 */
4927 return 1;
4928 }
4929 return wpa_s->current_ssid->p2p_group &&
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004930 wpa_s->current_ssid->mode == WPAS_MODE_INFRA;
4931}
4932
4933
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004934static void wpas_p2p_group_idle_timeout(void *eloop_ctx, void *timeout_ctx)
4935{
4936 struct wpa_supplicant *wpa_s = eloop_ctx;
4937
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004938 if (wpa_s->conf->p2p_group_idle == 0 && !wpas_p2p_is_client(wpa_s)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004939 wpa_printf(MSG_DEBUG, "P2P: Ignore group idle timeout - "
4940 "disabled");
4941 return;
4942 }
4943
Dmitry Shmidt04949592012-07-19 12:16:46 -07004944 wpa_printf(MSG_DEBUG, "P2P: Group idle timeout reached - terminate "
4945 "group");
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004946 wpas_p2p_group_delete(wpa_s, P2P_GROUP_REMOVAL_IDLE_TIMEOUT);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004947}
4948
4949
4950static void wpas_p2p_set_group_idle_timeout(struct wpa_supplicant *wpa_s)
4951{
Dmitry Shmidt04949592012-07-19 12:16:46 -07004952 int timeout;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004953
Dmitry Shmidt04949592012-07-19 12:16:46 -07004954 if (eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL) > 0)
4955 wpa_printf(MSG_DEBUG, "P2P: Cancelled P2P group idle timeout");
4956
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004957 if (wpa_s->current_ssid == NULL || !wpa_s->current_ssid->p2p_group)
4958 return;
4959
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004960 timeout = wpa_s->conf->p2p_group_idle;
4961 if (wpa_s->current_ssid->mode == WPAS_MODE_INFRA &&
4962 (timeout == 0 || timeout > P2P_MAX_CLIENT_IDLE))
4963 timeout = P2P_MAX_CLIENT_IDLE;
4964
4965 if (timeout == 0)
4966 return;
4967
Dmitry Shmidt04949592012-07-19 12:16:46 -07004968 if (timeout < 0) {
4969 if (wpa_s->current_ssid->mode == WPAS_MODE_INFRA)
4970 timeout = 0; /* special client mode no-timeout */
4971 else
4972 return;
4973 }
4974
4975 if (wpa_s->p2p_in_provisioning) {
4976 /*
4977 * Use the normal group formation timeout during the
4978 * provisioning phase to avoid terminating this process too
4979 * early due to group idle timeout.
4980 */
4981 wpa_printf(MSG_DEBUG, "P2P: Do not use P2P group idle timeout "
4982 "during provisioning");
4983 return;
4984 }
Dmitry Shmidtad266fb2012-08-24 17:03:35 -07004985#ifndef ANDROID_P2P
Dmitry Shmidt04949592012-07-19 12:16:46 -07004986 if (wpa_s->show_group_started) {
4987 /*
4988 * Use the normal group formation timeout between the end of
4989 * the provisioning phase and completion of 4-way handshake to
4990 * avoid terminating this process too early due to group idle
4991 * timeout.
4992 */
4993 wpa_printf(MSG_DEBUG, "P2P: Do not use P2P group idle timeout "
4994 "while waiting for initial 4-way handshake to "
4995 "complete");
4996 return;
4997 }
Dmitry Shmidtad266fb2012-08-24 17:03:35 -07004998#endif
Dmitry Shmidt04949592012-07-19 12:16:46 -07004999
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005000 wpa_printf(MSG_DEBUG, "P2P: Set P2P group idle timeout to %u seconds",
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005001 timeout);
5002 eloop_register_timeout(timeout, 0, wpas_p2p_group_idle_timeout,
5003 wpa_s, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005004}
5005
5006
Jouni Malinen2b89da82012-08-31 22:04:41 +03005007/* Returns 1 if the interface was removed */
5008int wpas_p2p_deauth_notif(struct wpa_supplicant *wpa_s, const u8 *bssid,
5009 u16 reason_code, const u8 *ie, size_t ie_len,
5010 int locally_generated)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005011{
5012 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
Jouni Malinen2b89da82012-08-31 22:04:41 +03005013 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005014 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
Jouni Malinen2b89da82012-08-31 22:04:41 +03005015 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005016
Dmitry Shmidt04949592012-07-19 12:16:46 -07005017 if (!locally_generated)
5018 p2p_deauth_notif(wpa_s->global->p2p, bssid, reason_code, ie,
5019 ie_len);
5020
5021 if (reason_code == WLAN_REASON_DEAUTH_LEAVING && !locally_generated &&
5022 wpa_s->current_ssid &&
5023 wpa_s->current_ssid->p2p_group &&
5024 wpa_s->current_ssid->mode == WPAS_MODE_INFRA) {
5025 wpa_printf(MSG_DEBUG, "P2P: GO indicated that the P2P Group "
5026 "session is ending");
Jouni Malinen2b89da82012-08-31 22:04:41 +03005027 if (wpas_p2p_group_delete(wpa_s,
5028 P2P_GROUP_REMOVAL_GO_ENDING_SESSION)
5029 > 0)
5030 return 1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07005031 }
Jouni Malinen2b89da82012-08-31 22:04:41 +03005032
5033 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005034}
5035
5036
5037void wpas_p2p_disassoc_notif(struct wpa_supplicant *wpa_s, const u8 *bssid,
Dmitry Shmidt04949592012-07-19 12:16:46 -07005038 u16 reason_code, const u8 *ie, size_t ie_len,
5039 int locally_generated)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005040{
5041 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
5042 return;
5043 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
5044 return;
5045
Dmitry Shmidt04949592012-07-19 12:16:46 -07005046 if (!locally_generated)
5047 p2p_disassoc_notif(wpa_s->global->p2p, bssid, reason_code, ie,
5048 ie_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005049}
5050
5051
5052void wpas_p2p_update_config(struct wpa_supplicant *wpa_s)
5053{
5054 struct p2p_data *p2p = wpa_s->global->p2p;
5055
5056 if (p2p == NULL)
5057 return;
5058
5059 if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_CAPABLE))
5060 return;
5061
5062 if (wpa_s->conf->changed_parameters & CFG_CHANGED_DEVICE_NAME)
5063 p2p_set_dev_name(p2p, wpa_s->conf->device_name);
5064
5065 if (wpa_s->conf->changed_parameters & CFG_CHANGED_DEVICE_TYPE)
5066 p2p_set_pri_dev_type(p2p, wpa_s->conf->device_type);
5067
5068 if (wpa_s->wps &&
5069 (wpa_s->conf->changed_parameters & CFG_CHANGED_CONFIG_METHODS))
5070 p2p_set_config_methods(p2p, wpa_s->wps->config_methods);
5071
5072 if (wpa_s->wps && (wpa_s->conf->changed_parameters & CFG_CHANGED_UUID))
5073 p2p_set_uuid(p2p, wpa_s->wps->uuid);
5074
5075 if (wpa_s->conf->changed_parameters & CFG_CHANGED_WPS_STRING) {
5076 p2p_set_manufacturer(p2p, wpa_s->conf->manufacturer);
5077 p2p_set_model_name(p2p, wpa_s->conf->model_name);
5078 p2p_set_model_number(p2p, wpa_s->conf->model_number);
5079 p2p_set_serial_number(p2p, wpa_s->conf->serial_number);
5080 }
5081
5082 if (wpa_s->conf->changed_parameters & CFG_CHANGED_SEC_DEVICE_TYPE)
5083 p2p_set_sec_dev_types(p2p,
5084 (void *) wpa_s->conf->sec_device_type,
5085 wpa_s->conf->num_sec_device_types);
5086
5087 if (wpa_s->conf->changed_parameters & CFG_CHANGED_VENDOR_EXTENSION) {
5088 int i;
5089 p2p_remove_wps_vendor_extensions(p2p);
5090 for (i = 0; i < MAX_WPS_VENDOR_EXT; i++) {
5091 if (wpa_s->conf->wps_vendor_ext[i] == NULL)
5092 continue;
5093 p2p_add_wps_vendor_extension(
5094 p2p, wpa_s->conf->wps_vendor_ext[i]);
5095 }
5096 }
5097
5098 if ((wpa_s->conf->changed_parameters & CFG_CHANGED_COUNTRY) &&
5099 wpa_s->conf->country[0] && wpa_s->conf->country[1]) {
5100 char country[3];
5101 country[0] = wpa_s->conf->country[0];
5102 country[1] = wpa_s->conf->country[1];
5103 country[2] = 0x04;
5104 p2p_set_country(p2p, country);
5105 }
5106
5107 if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_SSID_POSTFIX) {
5108 p2p_set_ssid_postfix(p2p, (u8 *) wpa_s->conf->p2p_ssid_postfix,
5109 wpa_s->conf->p2p_ssid_postfix ?
5110 os_strlen(wpa_s->conf->p2p_ssid_postfix) :
5111 0);
5112 }
5113
5114 if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_INTRA_BSS)
5115 p2p_set_intra_bss_dist(p2p, wpa_s->conf->p2p_intra_bss);
Jouni Malinen75ecf522011-06-27 15:19:46 -07005116
5117 if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_LISTEN_CHANNEL) {
5118 u8 reg_class, channel;
5119 int ret;
5120 unsigned int r;
5121 if (wpa_s->conf->p2p_listen_reg_class &&
5122 wpa_s->conf->p2p_listen_channel) {
5123 reg_class = wpa_s->conf->p2p_listen_reg_class;
5124 channel = wpa_s->conf->p2p_listen_channel;
5125 } else {
5126 reg_class = 81;
5127 /*
5128 * Pick one of the social channels randomly as the
5129 * listen channel.
5130 */
5131 os_get_random((u8 *) &r, sizeof(r));
5132 channel = 1 + (r % 3) * 5;
5133 }
5134 ret = p2p_set_listen_channel(p2p, reg_class, channel);
5135 if (ret)
5136 wpa_printf(MSG_ERROR, "P2P: Own listen channel update "
5137 "failed: %d", ret);
5138 }
5139 if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_OPER_CHANNEL) {
5140 u8 op_reg_class, op_channel, cfg_op_channel;
5141 int ret = 0;
5142 unsigned int r;
5143 if (wpa_s->conf->p2p_oper_reg_class &&
5144 wpa_s->conf->p2p_oper_channel) {
5145 op_reg_class = wpa_s->conf->p2p_oper_reg_class;
5146 op_channel = wpa_s->conf->p2p_oper_channel;
5147 cfg_op_channel = 1;
5148 } else {
5149 op_reg_class = 81;
5150 /*
5151 * Use random operation channel from (1, 6, 11)
5152 *if no other preference is indicated.
5153 */
5154 os_get_random((u8 *) &r, sizeof(r));
5155 op_channel = 1 + (r % 3) * 5;
5156 cfg_op_channel = 0;
5157 }
5158 ret = p2p_set_oper_channel(p2p, op_reg_class, op_channel,
5159 cfg_op_channel);
5160 if (ret)
5161 wpa_printf(MSG_ERROR, "P2P: Own oper channel update "
5162 "failed: %d", ret);
5163 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07005164
5165 if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_PREF_CHAN) {
5166 if (p2p_set_pref_chan(p2p, wpa_s->conf->num_p2p_pref_chan,
5167 wpa_s->conf->p2p_pref_chan) < 0) {
5168 wpa_printf(MSG_ERROR, "P2P: Preferred channel list "
5169 "update failed");
5170 }
5171 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005172}
5173
5174
5175int wpas_p2p_set_noa(struct wpa_supplicant *wpa_s, u8 count, int start,
5176 int duration)
5177{
5178 if (!wpa_s->ap_iface)
5179 return -1;
5180 return hostapd_p2p_set_noa(wpa_s->ap_iface->bss[0], count, start,
5181 duration);
5182}
5183
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005184
5185int wpas_p2p_set_cross_connect(struct wpa_supplicant *wpa_s, int enabled)
5186{
5187 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
5188 return -1;
5189 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
5190 return -1;
5191
5192 wpa_s->global->cross_connection = enabled;
5193 p2p_set_cross_connect(wpa_s->global->p2p, enabled);
5194
5195 if (!enabled) {
5196 struct wpa_supplicant *iface;
5197
5198 for (iface = wpa_s->global->ifaces; iface; iface = iface->next)
5199 {
5200 if (iface->cross_connect_enabled == 0)
5201 continue;
5202
5203 iface->cross_connect_enabled = 0;
5204 iface->cross_connect_in_use = 0;
5205 wpa_msg(iface->parent, MSG_INFO,
5206 P2P_EVENT_CROSS_CONNECT_DISABLE "%s %s",
5207 iface->ifname, iface->cross_connect_uplink);
5208 }
5209 }
5210
5211 return 0;
5212}
5213
5214
5215static void wpas_p2p_enable_cross_connect(struct wpa_supplicant *uplink)
5216{
5217 struct wpa_supplicant *iface;
5218
5219 if (!uplink->global->cross_connection)
5220 return;
5221
5222 for (iface = uplink->global->ifaces; iface; iface = iface->next) {
5223 if (!iface->cross_connect_enabled)
5224 continue;
5225 if (os_strcmp(uplink->ifname, iface->cross_connect_uplink) !=
5226 0)
5227 continue;
5228 if (iface->ap_iface == NULL)
5229 continue;
5230 if (iface->cross_connect_in_use)
5231 continue;
5232
5233 iface->cross_connect_in_use = 1;
5234 wpa_msg(iface->parent, MSG_INFO,
5235 P2P_EVENT_CROSS_CONNECT_ENABLE "%s %s",
5236 iface->ifname, iface->cross_connect_uplink);
5237 }
5238}
5239
5240
5241static void wpas_p2p_disable_cross_connect(struct wpa_supplicant *uplink)
5242{
5243 struct wpa_supplicant *iface;
5244
5245 for (iface = uplink->global->ifaces; iface; iface = iface->next) {
5246 if (!iface->cross_connect_enabled)
5247 continue;
5248 if (os_strcmp(uplink->ifname, iface->cross_connect_uplink) !=
5249 0)
5250 continue;
5251 if (!iface->cross_connect_in_use)
5252 continue;
5253
5254 wpa_msg(iface->parent, MSG_INFO,
5255 P2P_EVENT_CROSS_CONNECT_DISABLE "%s %s",
5256 iface->ifname, iface->cross_connect_uplink);
5257 iface->cross_connect_in_use = 0;
5258 }
5259}
5260
5261
5262void wpas_p2p_notif_connected(struct wpa_supplicant *wpa_s)
5263{
5264 if (wpa_s->ap_iface || wpa_s->current_ssid == NULL ||
5265 wpa_s->current_ssid->mode != WPAS_MODE_INFRA ||
5266 wpa_s->cross_connect_disallowed)
5267 wpas_p2p_disable_cross_connect(wpa_s);
5268 else
5269 wpas_p2p_enable_cross_connect(wpa_s);
Dmitry Shmidt04949592012-07-19 12:16:46 -07005270 if (!wpa_s->ap_iface &&
5271 eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL) > 0)
5272 wpa_printf(MSG_DEBUG, "P2P: Cancelled P2P group idle timeout");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005273}
5274
5275
5276void wpas_p2p_notif_disconnected(struct wpa_supplicant *wpa_s)
5277{
5278 wpas_p2p_disable_cross_connect(wpa_s);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005279 if (!wpa_s->ap_iface &&
5280 !eloop_is_timeout_registered(wpas_p2p_group_idle_timeout,
5281 wpa_s, NULL))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005282 wpas_p2p_set_group_idle_timeout(wpa_s);
5283}
5284
5285
5286static void wpas_p2p_cross_connect_setup(struct wpa_supplicant *wpa_s)
5287{
5288 struct wpa_supplicant *iface;
5289
5290 if (!wpa_s->global->cross_connection)
5291 return;
5292
5293 for (iface = wpa_s->global->ifaces; iface; iface = iface->next) {
5294 if (iface == wpa_s)
5295 continue;
5296 if (iface->drv_flags &
5297 WPA_DRIVER_FLAGS_P2P_DEDICATED_INTERFACE)
5298 continue;
5299 if (iface->drv_flags & WPA_DRIVER_FLAGS_P2P_CAPABLE)
5300 continue;
5301
5302 wpa_s->cross_connect_enabled = 1;
5303 os_strlcpy(wpa_s->cross_connect_uplink, iface->ifname,
5304 sizeof(wpa_s->cross_connect_uplink));
5305 wpa_printf(MSG_DEBUG, "P2P: Enable cross connection from "
5306 "%s to %s whenever uplink is available",
5307 wpa_s->ifname, wpa_s->cross_connect_uplink);
5308
5309 if (iface->ap_iface || iface->current_ssid == NULL ||
5310 iface->current_ssid->mode != WPAS_MODE_INFRA ||
5311 iface->cross_connect_disallowed ||
5312 iface->wpa_state != WPA_COMPLETED)
5313 break;
5314
5315 wpa_s->cross_connect_in_use = 1;
5316 wpa_msg(wpa_s->parent, MSG_INFO,
5317 P2P_EVENT_CROSS_CONNECT_ENABLE "%s %s",
5318 wpa_s->ifname, wpa_s->cross_connect_uplink);
5319 break;
5320 }
5321}
5322
5323
5324int wpas_p2p_notif_pbc_overlap(struct wpa_supplicant *wpa_s)
5325{
5326 if (wpa_s->p2p_group_interface != P2P_GROUP_INTERFACE_CLIENT &&
5327 !wpa_s->p2p_in_provisioning)
5328 return 0; /* not P2P client operation */
5329
5330 wpa_printf(MSG_DEBUG, "P2P: Terminate connection due to WPS PBC "
5331 "session overlap");
5332 if (wpa_s != wpa_s->parent)
5333 wpa_msg_ctrl(wpa_s->parent, MSG_INFO, WPS_EVENT_OVERLAP);
5334
5335 if (wpa_s->global->p2p)
5336 p2p_group_formation_failed(wpa_s->global->p2p);
5337
5338 eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
5339 wpa_s->parent, NULL);
5340
5341 wpas_group_formation_completed(wpa_s, 0);
5342 return 1;
5343}
5344
5345
5346void wpas_p2p_update_channel_list(struct wpa_supplicant *wpa_s)
5347{
5348 struct p2p_channels chan;
5349
5350 if (wpa_s->global == NULL || wpa_s->global->p2p == NULL)
5351 return;
5352
5353 os_memset(&chan, 0, sizeof(chan));
5354 if (wpas_p2p_setup_channels(wpa_s, &chan)) {
5355 wpa_printf(MSG_ERROR, "P2P: Failed to update supported "
5356 "channel list");
5357 return;
5358 }
5359
5360 p2p_update_channel_list(wpa_s->global->p2p, &chan);
5361}
5362
5363
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08005364static void wpas_p2p_scan_res_ignore(struct wpa_supplicant *wpa_s,
5365 struct wpa_scan_results *scan_res)
5366{
5367 wpa_printf(MSG_DEBUG, "P2P: Ignore scan results");
5368}
5369
5370
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005371int wpas_p2p_cancel(struct wpa_supplicant *wpa_s)
5372{
5373 struct wpa_global *global = wpa_s->global;
5374 int found = 0;
5375 const u8 *peer;
5376
5377 if (global->p2p == NULL)
5378 return -1;
5379
5380 wpa_printf(MSG_DEBUG, "P2P: Request to cancel group formation");
5381
5382 if (wpa_s->pending_interface_name[0] &&
5383 !is_zero_ether_addr(wpa_s->pending_interface_addr))
5384 found = 1;
5385
5386 peer = p2p_get_go_neg_peer(global->p2p);
5387 if (peer) {
5388 wpa_printf(MSG_DEBUG, "P2P: Unauthorize pending GO Neg peer "
5389 MACSTR, MAC2STR(peer));
5390 p2p_unauthorize(global->p2p, peer);
Dmitry Shmidt04949592012-07-19 12:16:46 -07005391 found = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005392 }
5393
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08005394 if (wpa_s->scan_res_handler == wpas_p2p_scan_res_join) {
5395 wpa_printf(MSG_DEBUG, "P2P: Stop pending scan for join");
5396 wpa_s->scan_res_handler = wpas_p2p_scan_res_ignore;
5397 found = 1;
5398 }
5399
5400 if (wpa_s->pending_pd_before_join) {
5401 wpa_printf(MSG_DEBUG, "P2P: Stop pending PD before join");
5402 wpa_s->pending_pd_before_join = 0;
5403 found = 1;
5404 }
5405
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005406 wpas_p2p_stop_find(wpa_s);
5407
5408 for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
5409 if (wpa_s == global->p2p_group_formation &&
5410 (wpa_s->p2p_in_provisioning ||
5411 wpa_s->parent->pending_interface_type ==
5412 WPA_IF_P2P_CLIENT)) {
5413 wpa_printf(MSG_DEBUG, "P2P: Interface %s in group "
5414 "formation found - cancelling",
5415 wpa_s->ifname);
5416 found = 1;
5417 eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
5418 wpa_s->parent, NULL);
Jouni Malinenadddfc42012-10-03 14:31:41 -07005419 if (wpa_s->p2p_in_provisioning) {
5420 wpas_group_formation_completed(wpa_s, 0);
5421 break;
5422 }
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005423 wpas_p2p_group_delete(wpa_s,
5424 P2P_GROUP_REMOVAL_REQUESTED);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005425 break;
5426 }
5427 }
5428
5429 if (!found) {
5430 wpa_printf(MSG_DEBUG, "P2P: No ongoing group formation found");
5431 return -1;
5432 }
5433
5434 return 0;
5435}
5436
5437
5438void wpas_p2p_interface_unavailable(struct wpa_supplicant *wpa_s)
5439{
5440 if (wpa_s->current_ssid == NULL || !wpa_s->current_ssid->p2p_group)
5441 return;
5442
5443 wpa_printf(MSG_DEBUG, "P2P: Remove group due to driver resource not "
5444 "being available anymore");
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005445 wpas_p2p_group_delete(wpa_s, P2P_GROUP_REMOVAL_UNAVAILABLE);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005446}
5447
5448
5449void wpas_p2p_update_best_channels(struct wpa_supplicant *wpa_s,
5450 int freq_24, int freq_5, int freq_overall)
5451{
5452 struct p2p_data *p2p = wpa_s->global->p2p;
5453 if (p2p == NULL || (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT))
5454 return;
5455 p2p_set_best_channels(p2p, freq_24, freq_5, freq_overall);
5456}
5457
5458
5459int wpas_p2p_unauthorize(struct wpa_supplicant *wpa_s, const char *addr)
5460{
5461 u8 peer[ETH_ALEN];
5462 struct p2p_data *p2p = wpa_s->global->p2p;
5463
5464 if (p2p == NULL || (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT))
5465 return -1;
5466
5467 if (hwaddr_aton(addr, peer))
5468 return -1;
5469
5470 return p2p_unauthorize(p2p, peer);
5471}
5472
5473
5474/**
5475 * wpas_p2p_disconnect - Disconnect from a P2P Group
5476 * @wpa_s: Pointer to wpa_supplicant data
5477 * Returns: 0 on success, -1 on failure
5478 *
5479 * This can be used to disconnect from a group in which the local end is a P2P
5480 * Client or to end a P2P Group in case the local end is the Group Owner. If a
5481 * virtual network interface was created for this group, that interface will be
5482 * removed. Otherwise, only the configured P2P group network will be removed
5483 * from the interface.
5484 */
5485int wpas_p2p_disconnect(struct wpa_supplicant *wpa_s)
5486{
5487
5488 if (wpa_s == NULL)
5489 return -1;
5490
Jouni Malinen2b89da82012-08-31 22:04:41 +03005491 return wpas_p2p_group_delete(wpa_s, P2P_GROUP_REMOVAL_REQUESTED) < 0 ?
5492 -1 : 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005493}
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005494
5495
5496int wpas_p2p_in_progress(struct wpa_supplicant *wpa_s)
5497{
5498 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
5499 return 0;
5500
5501 return p2p_in_progress(wpa_s->global->p2p);
5502}
5503
5504
5505void wpas_p2p_network_removed(struct wpa_supplicant *wpa_s,
5506 struct wpa_ssid *ssid)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005507{
5508 if (wpa_s->p2p_in_provisioning && ssid->p2p_group &&
5509 eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
5510 wpa_s->parent, NULL) > 0) {
Dmitry Shmidt04949592012-07-19 12:16:46 -07005511 /**
5512 * Remove the network by scheduling the group formation
5513 * timeout to happen immediately. The teardown code
5514 * needs to be scheduled to run asynch later so that we
5515 * don't delete data from under ourselves unexpectedly.
5516 * Calling wpas_p2p_group_formation_timeout directly
5517 * causes a series of crashes in WPS failure scenarios.
5518 */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005519 wpa_printf(MSG_DEBUG, "P2P: Canceled group formation due to "
5520 "P2P group network getting removed");
Dmitry Shmidt04949592012-07-19 12:16:46 -07005521 eloop_register_timeout(0, 0, wpas_p2p_group_formation_timeout,
5522 wpa_s->parent, NULL);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005523 }
5524}
5525
5526
5527struct wpa_ssid * wpas_p2p_get_persistent(struct wpa_supplicant *wpa_s,
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08005528 const u8 *addr, const u8 *ssid,
5529 size_t ssid_len)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005530{
5531 struct wpa_ssid *s;
5532 size_t i;
5533
5534 for (s = wpa_s->conf->ssid; s; s = s->next) {
5535 if (s->disabled != 2)
5536 continue;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08005537 if (ssid &&
5538 (ssid_len != s->ssid_len ||
5539 os_memcmp(ssid, s->ssid, ssid_len) != 0))
5540 continue;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005541 if (os_memcmp(s->bssid, addr, ETH_ALEN) == 0)
5542 return s; /* peer is GO in the persistent group */
5543 if (s->mode != WPAS_MODE_P2P_GO || s->p2p_client_list == NULL)
5544 continue;
5545 for (i = 0; i < s->num_p2p_clients; i++) {
5546 if (os_memcmp(s->p2p_client_list + i * ETH_ALEN,
5547 addr, ETH_ALEN) == 0)
5548 return s; /* peer is P2P client in persistent
5549 * group */
5550 }
5551 }
5552
5553 return NULL;
5554}
5555
5556
5557void wpas_p2p_notify_ap_sta_authorized(struct wpa_supplicant *wpa_s,
5558 const u8 *addr)
5559{
5560 if (addr == NULL)
5561 return;
5562 wpas_p2p_add_persistent_group_client(wpa_s, addr);
5563}
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08005564
Dmitry Shmidt04949592012-07-19 12:16:46 -07005565
5566static void wpas_p2p_fallback_to_go_neg(struct wpa_supplicant *wpa_s,
5567 int group_added)
5568{
5569 struct wpa_supplicant *group = wpa_s;
Dmitry Shmidt04949592012-07-19 12:16:46 -07005570 if (wpa_s->global->p2p_group_formation)
5571 group = wpa_s->global->p2p_group_formation;
5572 wpa_s = wpa_s->parent;
5573 offchannel_send_action_done(wpa_s);
5574 if (group_added)
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005575 wpas_p2p_group_delete(group, P2P_GROUP_REMOVAL_SILENT);
Dmitry Shmidt04949592012-07-19 12:16:46 -07005576 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Fall back to GO Negotiation");
5577 wpas_p2p_connect(wpa_s, wpa_s->pending_join_dev_addr, wpa_s->p2p_pin,
5578 wpa_s->p2p_wps_method, wpa_s->p2p_persistent_group, 0,
5579 0, 0, wpa_s->p2p_go_intent, wpa_s->p2p_connect_freq,
5580 wpa_s->p2p_persistent_id,
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005581 wpa_s->p2p_pd_before_go_neg,
5582 wpa_s->p2p_go_ht40);
Dmitry Shmidt04949592012-07-19 12:16:46 -07005583}
5584
5585
5586int wpas_p2p_scan_no_go_seen(struct wpa_supplicant *wpa_s)
5587{
5588 if (!wpa_s->p2p_fallback_to_go_neg ||
5589 wpa_s->p2p_in_provisioning <= 5)
5590 return 0;
5591
5592 if (wpas_p2p_peer_go(wpa_s, wpa_s->pending_join_dev_addr) > 0)
5593 return 0; /* peer operating as a GO */
5594
5595 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: GO not found for p2p_connect-auto - "
5596 "fallback to GO Negotiation");
5597 wpas_p2p_fallback_to_go_neg(wpa_s, 1);
5598
5599 return 1;
5600}
5601
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005602
5603unsigned int wpas_p2p_search_delay(struct wpa_supplicant *wpa_s)
5604{
5605 const char *rn, *rn2;
5606 struct wpa_supplicant *ifs;
5607
5608 if (wpa_s->wpa_state > WPA_SCANNING) {
5609 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Use %u ms search delay due to "
5610 "concurrent operation",
5611 P2P_CONCURRENT_SEARCH_DELAY);
5612 return P2P_CONCURRENT_SEARCH_DELAY;
5613 }
5614
5615 if (!wpa_s->driver->get_radio_name)
5616 return 0;
5617 rn = wpa_s->driver->get_radio_name(wpa_s->drv_priv);
5618 if (rn == NULL || rn[0] == '\0')
5619 return 0;
5620
5621 for (ifs = wpa_s->global->ifaces; ifs; ifs = ifs->next) {
5622 if (ifs == wpa_s || !ifs->driver->get_radio_name)
5623 continue;
5624
5625 rn2 = ifs->driver->get_radio_name(ifs->drv_priv);
5626 if (!rn2 || os_strcmp(rn, rn2) != 0)
5627 continue;
5628 if (ifs->wpa_state > WPA_SCANNING) {
5629 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Use %u ms search "
5630 "delay due to concurrent operation on "
5631 "interface %s",
5632 P2P_CONCURRENT_SEARCH_DELAY, ifs->ifname);
5633 return P2P_CONCURRENT_SEARCH_DELAY;
5634 }
5635 }
5636
5637 return 0;
5638}
5639
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08005640#ifdef ANDROID_P2P
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08005641int wpas_p2p_handle_frequency_conflicts(struct wpa_supplicant *wpa_s, int freq,
5642 struct wpa_ssid *ssid)
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08005643{
5644 struct wpa_supplicant *iface = NULL;
5645 struct p2p_data *p2p = wpa_s->global->p2p;
5646
5647 for (iface = wpa_s->global->ifaces; iface; iface = iface->next) {
Jeff Johnson12b1cd92012-10-07 19:34:24 -07005648 if ((iface->current_ssid) &&
5649 (iface->current_ssid->frequency != freq) &&
5650 ((iface->p2p_group_interface) ||
5651 (iface->current_ssid->p2p_group))) {
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08005652
Jeff Johnson12b1cd92012-10-07 19:34:24 -07005653 if ((iface->p2p_group_interface == P2P_GROUP_INTERFACE_GO) ||
5654 (iface->current_ssid->mode == WPAS_MODE_P2P_GO)) {
5655 /* Try to see whether we can move the GO. If it
5656 * is not possible, remove the GO interface
5657 */
5658 if (wpa_drv_switch_channel(iface, freq) == 0) {
5659 wpa_printf(MSG_ERROR, "P2P: GO Moved to freq(%d)", freq);
5660 iface->current_ssid->frequency = freq;
5661 continue;
5662 }
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08005663 }
5664
5665 /* If GO cannot be moved or if the conflicting interface is a
5666 * P2P Client, remove the interface depending up on the connection
5667 * priority */
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08005668 if(!wpas_is_p2p_prioritized(iface)) {
Dmitry Shmidtf4f5db32012-09-11 14:36:56 -07005669 /* STA connection has priority over existing
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08005670 * P2P connection. So remove the interface */
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08005671 wpa_printf(MSG_DEBUG, "P2P: Removing P2P connection due to Single channel"
Dmitry Shmidt687922c2012-03-26 14:02:32 -07005672 "concurrent mode frequency conflict");
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005673 wpas_p2p_group_delete(iface, P2P_GROUP_REMOVAL_FREQ_CONFLICT);
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08005674 /* If connection in progress is p2p connection, do not proceed for the connection */
5675 if (wpa_s == iface)
5676 return -1;
5677 else
5678 /* If connection in progress is STA connection, proceed for the connection */
5679 return 0;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08005680 } else {
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08005681 /* P2p connection has priority, disable the STA network*/
5682 wpa_supplicant_disable_network(wpa_s->global->ifaces, ssid);
5683 wpa_msg(wpa_s->global->ifaces, MSG_INFO, WPA_EVENT_FREQ_CONFLICT
5684 " id=%d", ssid->id);
5685 os_memset(wpa_s->global->ifaces->pending_bssid, 0, ETH_ALEN);
5686 if (wpa_s == iface) {
5687 /* p2p connection is in progress, continue connecting...*/
5688 return 0;
5689 }
5690 else {
5691 /* STA connection is in progress, do not allow to continue */
5692 return -1;
5693 }
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08005694 }
5695 }
5696 }
5697 return 0;
5698}
5699#endif