blob: 523178ff3647a22d3d6118fa232b39d69e17c7dc [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;
3583 }
3584
3585 group->p2p_in_provisioning = 1;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08003586 wpa_s->global->p2p_group_formation = wpa_s;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003587 group->p2p_fallback_to_go_neg = wpa_s->p2p_fallback_to_go_neg;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003588
3589 os_memset(&res, 0, sizeof(res));
3590 os_memcpy(res.peer_interface_addr, wpa_s->pending_join_iface_addr,
3591 ETH_ALEN);
3592 res.wps_method = wpa_s->pending_join_wps_method;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003593 bss = wpa_bss_get_bssid(wpa_s, wpa_s->pending_join_iface_addr);
3594 if (bss) {
3595 res.freq = bss->freq;
3596 res.ssid_len = bss->ssid_len;
3597 os_memcpy(res.ssid, bss->ssid, bss->ssid_len);
3598 }
3599
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08003600 if (wpa_s->off_channel_freq || wpa_s->roc_waiting_drv_freq) {
3601 wpa_printf(MSG_DEBUG, "P2P: Cancel remain-on-channel prior to "
3602 "starting client");
3603 wpa_drv_cancel_remain_on_channel(wpa_s);
3604 wpa_s->off_channel_freq = 0;
3605 wpa_s->roc_waiting_drv_freq = 0;
3606 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003607 wpas_start_wps_enrollee(group, &res);
3608
3609 /*
3610 * Allow a longer timeout for join-a-running-group than normal 15
3611 * second group formation timeout since the GO may not have authorized
3612 * our connection yet.
3613 */
3614 eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s, NULL);
3615 eloop_register_timeout(60, 0, wpas_p2p_group_formation_timeout,
3616 wpa_s, NULL);
3617
3618 return 0;
3619}
3620
3621
3622/**
3623 * wpas_p2p_connect - Request P2P Group Formation to be started
3624 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
3625 * @peer_addr: Address of the peer P2P Device
3626 * @pin: PIN to use during provisioning or %NULL to indicate PBC mode
3627 * @persistent_group: Whether to create a persistent group
Dmitry Shmidt04949592012-07-19 12:16:46 -07003628 * @auto_join: Whether to select join vs. GO Negotiation automatically
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003629 * @join: Whether to join an existing group (as a client) instead of starting
3630 * Group Owner negotiation; @peer_addr is BSSID in that case
3631 * @auth: Whether to only authorize the connection instead of doing that and
3632 * initiating Group Owner negotiation
3633 * @go_intent: GO Intent or -1 to use default
3634 * @freq: Frequency for the group or 0 for auto-selection
Dmitry Shmidt04949592012-07-19 12:16:46 -07003635 * @persistent_id: Persistent group credentials to use for forcing GO
3636 * parameters or -1 to generate new values (SSID/passphrase)
3637 * @pd: Whether to send Provision Discovery prior to GO Negotiation as an
3638 * interoperability workaround when initiating group formation
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003639 * @ht40: Start GO with 40 MHz channel width
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003640 * Returns: 0 or new PIN (if pin was %NULL) on success, -1 on unspecified
3641 * failure, -2 on failure due to channel not currently available,
3642 * -3 if forced channel is not supported
3643 */
3644int wpas_p2p_connect(struct wpa_supplicant *wpa_s, const u8 *peer_addr,
3645 const char *pin, enum p2p_wps_method wps_method,
Dmitry Shmidt04949592012-07-19 12:16:46 -07003646 int persistent_group, int auto_join, int join, int auth,
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003647 int go_intent, int freq, int persistent_id, int pd,
3648 int ht40)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003649{
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08003650 int force_freq = 0, pref_freq = 0, oper_freq = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003651 u8 bssid[ETH_ALEN];
3652 int ret = 0;
3653 enum wpa_driver_if_type iftype;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003654 const u8 *if_addr;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003655 struct wpa_ssid *ssid = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003656
3657 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
3658 return -1;
3659
Dmitry Shmidt04949592012-07-19 12:16:46 -07003660 if (persistent_id >= 0) {
3661 ssid = wpa_config_get_network(wpa_s->conf, persistent_id);
3662 if (ssid == NULL || ssid->disabled != 2 ||
3663 ssid->mode != WPAS_MODE_P2P_GO)
3664 return -1;
3665 }
3666
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003667 if (go_intent < 0)
3668 go_intent = wpa_s->conf->p2p_go_intent;
3669
3670 if (!auth)
3671 wpa_s->p2p_long_listen = 0;
3672
3673 wpa_s->p2p_wps_method = wps_method;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003674 wpa_s->p2p_persistent_group = !!persistent_group;
3675 wpa_s->p2p_persistent_id = persistent_id;
3676 wpa_s->p2p_go_intent = go_intent;
3677 wpa_s->p2p_connect_freq = freq;
3678 wpa_s->p2p_fallback_to_go_neg = 0;
3679 wpa_s->p2p_pd_before_go_neg = !!pd;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003680 wpa_s->p2p_go_ht40 = !!ht40;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003681
3682 if (pin)
3683 os_strlcpy(wpa_s->p2p_pin, pin, sizeof(wpa_s->p2p_pin));
3684 else if (wps_method == WPS_PIN_DISPLAY) {
3685 ret = wps_generate_pin();
3686 os_snprintf(wpa_s->p2p_pin, sizeof(wpa_s->p2p_pin), "%08d",
3687 ret);
3688 wpa_printf(MSG_DEBUG, "P2P: Randomly generated PIN: %s",
3689 wpa_s->p2p_pin);
3690 } else
3691 wpa_s->p2p_pin[0] = '\0';
3692
Dmitry Shmidt04949592012-07-19 12:16:46 -07003693 if (join || auto_join) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003694 u8 iface_addr[ETH_ALEN], dev_addr[ETH_ALEN];
3695 if (auth) {
3696 wpa_printf(MSG_DEBUG, "P2P: Authorize invitation to "
3697 "connect a running group from " MACSTR,
3698 MAC2STR(peer_addr));
3699 os_memcpy(wpa_s->p2p_auth_invite, peer_addr, ETH_ALEN);
3700 return ret;
3701 }
3702 os_memcpy(dev_addr, peer_addr, ETH_ALEN);
3703 if (p2p_get_interface_addr(wpa_s->global->p2p, peer_addr,
3704 iface_addr) < 0) {
3705 os_memcpy(iface_addr, peer_addr, ETH_ALEN);
3706 p2p_get_dev_addr(wpa_s->global->p2p, peer_addr,
3707 dev_addr);
3708 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07003709 if (auto_join) {
3710 os_get_time(&wpa_s->p2p_auto_started);
3711 wpa_printf(MSG_DEBUG, "P2P: Auto join started at "
3712 "%ld.%06ld",
3713 wpa_s->p2p_auto_started.sec,
3714 wpa_s->p2p_auto_started.usec);
3715 }
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08003716 wpa_s->user_initiated_pd = 1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003717 if (wpas_p2p_join(wpa_s, iface_addr, dev_addr, wps_method,
3718 auto_join) < 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003719 return -1;
3720 return ret;
3721 }
3722
3723 if (wpa_s->current_ssid && wpa_drv_get_bssid(wpa_s, bssid) == 0 &&
3724 wpa_s->assoc_freq)
3725 oper_freq = wpa_s->assoc_freq;
3726 else {
3727 oper_freq = wpa_drv_shared_freq(wpa_s);
3728 if (oper_freq < 0)
3729 oper_freq = 0;
3730 }
3731
3732 if (freq > 0) {
3733 if (!p2p_supported_freq(wpa_s->global->p2p, freq)) {
3734 wpa_printf(MSG_DEBUG, "P2P: The forced channel "
3735 "(%u MHz) is not supported for P2P uses",
3736 freq);
3737 return -3;
3738 }
3739
3740 if (oper_freq > 0 && freq != oper_freq &&
3741 !(wpa_s->drv_flags &
3742 WPA_DRIVER_FLAGS_MULTI_CHANNEL_CONCURRENT)) {
3743 wpa_printf(MSG_DEBUG, "P2P: Cannot start P2P group "
3744 "on %u MHz while connected on another "
3745 "channel (%u MHz)", freq, oper_freq);
3746 return -2;
3747 }
3748 wpa_printf(MSG_DEBUG, "P2P: Trying to force us to use the "
3749 "requested channel (%u MHz)", freq);
3750 force_freq = freq;
3751 } else if (oper_freq > 0 &&
3752 !p2p_supported_freq(wpa_s->global->p2p, oper_freq)) {
3753 if (!(wpa_s->drv_flags &
3754 WPA_DRIVER_FLAGS_MULTI_CHANNEL_CONCURRENT)) {
3755 wpa_printf(MSG_DEBUG, "P2P: Cannot start P2P group "
3756 "while connected on non-P2P supported "
3757 "channel (%u MHz)", oper_freq);
3758 return -2;
3759 }
3760 wpa_printf(MSG_DEBUG, "P2P: Current operating channel "
3761 "(%u MHz) not available for P2P - try to use "
3762 "another channel", oper_freq);
3763 force_freq = 0;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08003764 } else if (oper_freq > 0 &&
3765 (wpa_s->drv_flags &
3766 WPA_DRIVER_FLAGS_MULTI_CHANNEL_CONCURRENT)) {
3767 wpa_printf(MSG_DEBUG, "P2P: Trying to prefer the channel we "
3768 "are already using (%u MHz) on another interface",
3769 oper_freq);
3770 pref_freq = oper_freq;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003771 } else if (oper_freq > 0) {
3772 wpa_printf(MSG_DEBUG, "P2P: Trying to force us to use the "
3773 "channel we are already using (%u MHz) on another "
3774 "interface", oper_freq);
3775 force_freq = oper_freq;
3776 }
3777
3778 wpa_s->create_p2p_iface = wpas_p2p_create_iface(wpa_s);
3779
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003780 if (wpa_s->create_p2p_iface) {
3781 /* Prepare to add a new interface for the group */
3782 iftype = WPA_IF_P2P_GROUP;
3783 if (go_intent == 15)
3784 iftype = WPA_IF_P2P_GO;
3785 if (wpas_p2p_add_group_interface(wpa_s, iftype) < 0) {
3786 wpa_printf(MSG_ERROR, "P2P: Failed to allocate a new "
3787 "interface for the group");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003788 return -1;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003789 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003790
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003791 if_addr = wpa_s->pending_interface_addr;
3792 } else
3793 if_addr = wpa_s->own_addr;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003794
3795 if (auth) {
3796 if (wpas_p2p_auth_go_neg(wpa_s, peer_addr, wps_method,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003797 go_intent, if_addr,
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08003798 force_freq, persistent_group, ssid,
3799 pref_freq) < 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003800 return -1;
3801 return ret;
3802 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003803
3804 if (wpas_p2p_start_go_neg(wpa_s, peer_addr, wps_method,
3805 go_intent, if_addr, force_freq,
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08003806 persistent_group, ssid, pref_freq) < 0) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003807 if (wpa_s->create_p2p_iface)
3808 wpas_p2p_remove_pending_group_interface(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003809 return -1;
3810 }
3811 return ret;
3812}
3813
3814
3815/**
3816 * wpas_p2p_remain_on_channel_cb - Indication of remain-on-channel start
3817 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
3818 * @freq: Frequency of the channel in MHz
3819 * @duration: Duration of the stay on the channel in milliseconds
3820 *
3821 * This callback is called when the driver indicates that it has started the
3822 * requested remain-on-channel duration.
3823 */
3824void wpas_p2p_remain_on_channel_cb(struct wpa_supplicant *wpa_s,
3825 unsigned int freq, unsigned int duration)
3826{
3827 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
3828 return;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003829 if (wpa_s->off_channel_freq == wpa_s->pending_listen_freq) {
3830 p2p_listen_cb(wpa_s->global->p2p, wpa_s->pending_listen_freq,
3831 wpa_s->pending_listen_duration);
3832 wpa_s->pending_listen_freq = 0;
3833 }
3834}
3835
3836
3837static int wpas_p2p_listen_start(struct wpa_supplicant *wpa_s,
3838 unsigned int timeout)
3839{
3840 /* Limit maximum Listen state time based on driver limitation. */
3841 if (timeout > wpa_s->max_remain_on_chan)
3842 timeout = wpa_s->max_remain_on_chan;
3843
3844 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
3845 return wpa_drv_p2p_listen(wpa_s, timeout);
3846
3847 return p2p_listen(wpa_s->global->p2p, timeout);
3848}
3849
3850
3851/**
3852 * wpas_p2p_cancel_remain_on_channel_cb - Remain-on-channel timeout
3853 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
3854 * @freq: Frequency of the channel in MHz
3855 *
3856 * This callback is called when the driver indicates that a remain-on-channel
3857 * operation has been completed, i.e., the duration on the requested channel
3858 * has timed out.
3859 */
3860void wpas_p2p_cancel_remain_on_channel_cb(struct wpa_supplicant *wpa_s,
3861 unsigned int freq)
3862{
3863 wpa_printf(MSG_DEBUG, "P2P: Cancel remain-on-channel callback "
3864 "(p2p_long_listen=%d ms pending_action_tx=%p)",
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08003865 wpa_s->p2p_long_listen, offchannel_pending_action_tx(wpa_s));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003866 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
3867 return;
3868 if (p2p_listen_end(wpa_s->global->p2p, freq) > 0)
3869 return; /* P2P module started a new operation */
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08003870 if (offchannel_pending_action_tx(wpa_s))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003871 return;
3872 if (wpa_s->p2p_long_listen > 0)
3873 wpa_s->p2p_long_listen -= wpa_s->max_remain_on_chan;
3874 if (wpa_s->p2p_long_listen > 0) {
3875 wpa_printf(MSG_DEBUG, "P2P: Continuing long Listen state");
3876 wpas_p2p_listen_start(wpa_s, wpa_s->p2p_long_listen);
3877 }
3878}
3879
3880
3881/**
3882 * wpas_p2p_group_remove - Remove a P2P group
3883 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
3884 * @ifname: Network interface name of the group interface or "*" to remove all
3885 * groups
3886 * Returns: 0 on success, -1 on failure
3887 *
3888 * This function is used to remove a P2P group. This can be used to disconnect
3889 * from a group in which the local end is a P2P Client or to end a P2P Group in
3890 * case the local end is the Group Owner. If a virtual network interface was
3891 * created for this group, that interface will be removed. Otherwise, only the
3892 * configured P2P group network will be removed from the interface.
3893 */
3894int wpas_p2p_group_remove(struct wpa_supplicant *wpa_s, const char *ifname)
3895{
3896 struct wpa_global *global = wpa_s->global;
3897
3898 if (os_strcmp(ifname, "*") == 0) {
3899 struct wpa_supplicant *prev;
3900 wpa_s = global->ifaces;
3901 while (wpa_s) {
3902 prev = wpa_s;
3903 wpa_s = wpa_s->next;
3904 wpas_p2p_disconnect(prev);
3905 }
3906 return 0;
3907 }
3908
3909 for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
3910 if (os_strcmp(wpa_s->ifname, ifname) == 0)
3911 break;
3912 }
3913
3914 return wpas_p2p_disconnect(wpa_s);
3915}
3916
3917
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003918static int wpas_p2p_init_go_params(struct wpa_supplicant *wpa_s,
3919 struct p2p_go_neg_results *params,
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003920 int freq, int ht40)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003921{
3922 u8 bssid[ETH_ALEN];
3923 int res;
3924
3925 os_memset(params, 0, sizeof(*params));
3926 params->role_go = 1;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003927 params->ht40 = ht40;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003928 if (freq) {
3929 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on forced "
3930 "frequency %d MHz", freq);
3931 params->freq = freq;
3932 } else if (wpa_s->conf->p2p_oper_reg_class == 81 &&
3933 wpa_s->conf->p2p_oper_channel >= 1 &&
3934 wpa_s->conf->p2p_oper_channel <= 11) {
3935 params->freq = 2407 + 5 * wpa_s->conf->p2p_oper_channel;
3936 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on configured "
3937 "frequency %d MHz", params->freq);
3938 } else if (wpa_s->conf->p2p_oper_reg_class == 115 ||
Jouni Malinen87fd2792011-05-16 18:35:42 +03003939 wpa_s->conf->p2p_oper_reg_class == 124) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003940 params->freq = 5000 + 5 * wpa_s->conf->p2p_oper_channel;
3941 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on configured "
3942 "frequency %d MHz", params->freq);
3943 } else if (wpa_s->conf->p2p_oper_channel == 0 &&
3944 wpa_s->best_overall_freq > 0 &&
3945 p2p_supported_freq(wpa_s->global->p2p,
3946 wpa_s->best_overall_freq)) {
3947 params->freq = wpa_s->best_overall_freq;
3948 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on best overall "
3949 "channel %d MHz", params->freq);
3950 } else if (wpa_s->conf->p2p_oper_channel == 0 &&
3951 wpa_s->best_24_freq > 0 &&
3952 p2p_supported_freq(wpa_s->global->p2p,
3953 wpa_s->best_24_freq)) {
3954 params->freq = wpa_s->best_24_freq;
3955 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on best 2.4 GHz "
3956 "channel %d MHz", params->freq);
3957 } else if (wpa_s->conf->p2p_oper_channel == 0 &&
3958 wpa_s->best_5_freq > 0 &&
3959 p2p_supported_freq(wpa_s->global->p2p,
3960 wpa_s->best_5_freq)) {
3961 params->freq = wpa_s->best_5_freq;
3962 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on best 5 GHz "
3963 "channel %d MHz", params->freq);
3964 } else {
Dmitry Shmidt04949592012-07-19 12:16:46 -07003965 int chan;
3966 for (chan = 0; chan < 11; chan++) {
3967 params->freq = 2412 + chan * 5;
3968 if (!wpas_p2p_disallowed_freq(wpa_s->global,
3969 params->freq))
3970 break;
3971 }
3972 if (chan == 11) {
3973 wpa_printf(MSG_DEBUG, "P2P: No 2.4 GHz channel "
3974 "allowed");
3975 return -1;
3976 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003977 wpa_printf(MSG_DEBUG, "P2P: Set GO freq %d MHz (no preference "
3978 "known)", params->freq);
3979 }
3980
3981 if (wpa_s->current_ssid && wpa_drv_get_bssid(wpa_s, bssid) == 0 &&
3982 wpa_s->assoc_freq && !freq) {
3983 wpa_printf(MSG_DEBUG, "P2P: Force GO on the channel we are "
3984 "already using");
3985 params->freq = wpa_s->assoc_freq;
3986 }
3987
3988 res = wpa_drv_shared_freq(wpa_s);
3989 if (res > 0 && !freq) {
3990 wpa_printf(MSG_DEBUG, "P2P: Force GO on the channel we are "
3991 "already using on a shared interface");
3992 params->freq = res;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003993 } else if (res > 0 && freq != res &&
3994 !(wpa_s->drv_flags &
3995 WPA_DRIVER_FLAGS_MULTI_CHANNEL_CONCURRENT)) {
3996 wpa_printf(MSG_DEBUG, "P2P: Cannot start P2P group on %u MHz "
3997 "while connected on another channel (%u MHz)",
3998 freq, res);
3999 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004000 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004001
4002 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004003}
4004
4005
4006static struct wpa_supplicant *
4007wpas_p2p_get_group_iface(struct wpa_supplicant *wpa_s, int addr_allocated,
4008 int go)
4009{
4010 struct wpa_supplicant *group_wpa_s;
4011
Dmitry Shmidtaa532512012-09-24 10:35:31 -07004012 if (!wpas_p2p_create_iface(wpa_s)) {
4013 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Use same interface for group "
4014 "operations");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004015 return wpa_s;
Dmitry Shmidtaa532512012-09-24 10:35:31 -07004016 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004017
4018 if (wpas_p2p_add_group_interface(wpa_s, go ? WPA_IF_P2P_GO :
Dmitry Shmidtaa532512012-09-24 10:35:31 -07004019 WPA_IF_P2P_CLIENT) < 0) {
4020 wpa_msg(wpa_s, MSG_ERROR, "P2P: Failed to add group interface");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004021 return NULL;
Dmitry Shmidtaa532512012-09-24 10:35:31 -07004022 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004023 group_wpa_s = wpas_p2p_init_group_interface(wpa_s, go);
4024 if (group_wpa_s == NULL) {
Dmitry Shmidtaa532512012-09-24 10:35:31 -07004025 wpa_msg(wpa_s, MSG_ERROR, "P2P: Failed to initialize group "
4026 "interface");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004027 wpas_p2p_remove_pending_group_interface(wpa_s);
4028 return NULL;
4029 }
4030
Dmitry Shmidtaa532512012-09-24 10:35:31 -07004031 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Use separate group interface %s",
4032 group_wpa_s->ifname);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004033 return group_wpa_s;
4034}
4035
4036
4037/**
4038 * wpas_p2p_group_add - Add a new P2P group with local end as Group Owner
4039 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
4040 * @persistent_group: Whether to create a persistent group
4041 * @freq: Frequency for the group or 0 to indicate no hardcoding
4042 * Returns: 0 on success, -1 on failure
4043 *
4044 * This function creates a new P2P group with the local end as the Group Owner,
4045 * i.e., without using Group Owner Negotiation.
4046 */
4047int wpas_p2p_group_add(struct wpa_supplicant *wpa_s, int persistent_group,
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004048 int freq, int ht40)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004049{
4050 struct p2p_go_neg_results params;
4051 unsigned int r;
4052
4053 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
4054 return -1;
4055
Dmitry Shmidtdca39792011-09-06 11:17:33 -07004056 /* Make sure we are not running find during connection establishment */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004057 wpa_printf(MSG_DEBUG, "P2P: Stop any on-going P2P FIND");
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08004058 wpas_p2p_stop_find_oper(wpa_s);
Dmitry Shmidtdca39792011-09-06 11:17:33 -07004059
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004060 if (freq == 2) {
4061 wpa_printf(MSG_DEBUG, "P2P: Request to start GO on 2.4 GHz "
4062 "band");
4063 if (wpa_s->best_24_freq > 0 &&
4064 p2p_supported_freq(wpa_s->global->p2p,
4065 wpa_s->best_24_freq)) {
4066 freq = wpa_s->best_24_freq;
4067 wpa_printf(MSG_DEBUG, "P2P: Use best 2.4 GHz band "
4068 "channel: %d MHz", freq);
4069 } else {
4070 os_get_random((u8 *) &r, sizeof(r));
4071 freq = 2412 + (r % 3) * 25;
4072 wpa_printf(MSG_DEBUG, "P2P: Use random 2.4 GHz band "
4073 "channel: %d MHz", freq);
4074 }
4075 }
4076
4077 if (freq == 5) {
4078 wpa_printf(MSG_DEBUG, "P2P: Request to start GO on 5 GHz "
4079 "band");
4080 if (wpa_s->best_5_freq > 0 &&
4081 p2p_supported_freq(wpa_s->global->p2p,
4082 wpa_s->best_5_freq)) {
4083 freq = wpa_s->best_5_freq;
4084 wpa_printf(MSG_DEBUG, "P2P: Use best 5 GHz band "
4085 "channel: %d MHz", freq);
4086 } else {
4087 os_get_random((u8 *) &r, sizeof(r));
4088 freq = 5180 + (r % 4) * 20;
4089 if (!p2p_supported_freq(wpa_s->global->p2p, freq)) {
4090 wpa_printf(MSG_DEBUG, "P2P: Could not select "
4091 "5 GHz channel for P2P group");
4092 return -1;
4093 }
4094 wpa_printf(MSG_DEBUG, "P2P: Use random 5 GHz band "
4095 "channel: %d MHz", freq);
4096 }
4097 }
4098
4099 if (freq > 0 && !p2p_supported_freq(wpa_s->global->p2p, freq)) {
4100 wpa_printf(MSG_DEBUG, "P2P: The forced channel for GO "
4101 "(%u MHz) is not supported for P2P uses",
4102 freq);
4103 return -1;
4104 }
4105
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004106 if (wpas_p2p_init_go_params(wpa_s, &params, freq, ht40))
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004107 return -1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07004108 if (params.freq &&
4109 !p2p_supported_freq(wpa_s->global->p2p, params.freq)) {
4110 wpa_printf(MSG_DEBUG, "P2P: The selected channel for GO "
4111 "(%u MHz) is not supported for P2P uses",
4112 params.freq);
4113 return -1;
4114 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004115 p2p_go_params(wpa_s->global->p2p, &params);
4116 params.persistent_group = persistent_group;
4117
4118 wpa_s = wpas_p2p_get_group_iface(wpa_s, 0, 1);
4119 if (wpa_s == NULL)
4120 return -1;
4121 wpas_start_wps_go(wpa_s, &params, 0);
4122
4123 return 0;
4124}
4125
4126
4127static int wpas_start_p2p_client(struct wpa_supplicant *wpa_s,
4128 struct wpa_ssid *params, int addr_allocated)
4129{
4130 struct wpa_ssid *ssid;
4131
4132 wpa_s = wpas_p2p_get_group_iface(wpa_s, addr_allocated, 0);
4133 if (wpa_s == NULL)
4134 return -1;
4135
4136 wpa_supplicant_ap_deinit(wpa_s);
4137
4138 ssid = wpa_config_add_network(wpa_s->conf);
4139 if (ssid == NULL)
4140 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004141 wpa_config_set_network_defaults(ssid);
4142 ssid->temporary = 1;
4143 ssid->proto = WPA_PROTO_RSN;
4144 ssid->pairwise_cipher = WPA_CIPHER_CCMP;
4145 ssid->group_cipher = WPA_CIPHER_CCMP;
4146 ssid->key_mgmt = WPA_KEY_MGMT_PSK;
4147 ssid->ssid = os_malloc(params->ssid_len);
4148 if (ssid->ssid == NULL) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004149 wpa_config_remove_network(wpa_s->conf, ssid->id);
4150 return -1;
4151 }
4152 os_memcpy(ssid->ssid, params->ssid, params->ssid_len);
4153 ssid->ssid_len = params->ssid_len;
4154 ssid->p2p_group = 1;
4155 ssid->export_keys = 1;
4156 if (params->psk_set) {
4157 os_memcpy(ssid->psk, params->psk, 32);
4158 ssid->psk_set = 1;
4159 }
4160 if (params->passphrase)
4161 ssid->passphrase = os_strdup(params->passphrase);
4162
4163 wpa_supplicant_select_network(wpa_s, ssid);
4164
4165 wpa_s->show_group_started = 1;
4166
4167 return 0;
4168}
4169
4170
4171int wpas_p2p_group_add_persistent(struct wpa_supplicant *wpa_s,
4172 struct wpa_ssid *ssid, int addr_allocated,
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004173 int freq, int ht40)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004174{
4175 struct p2p_go_neg_results params;
4176 int go = 0;
4177
4178 if (ssid->disabled != 2 || ssid->ssid == NULL)
4179 return -1;
4180
4181 if (wpas_get_p2p_group(wpa_s, ssid->ssid, ssid->ssid_len, &go) &&
4182 go == (ssid->mode == WPAS_MODE_P2P_GO)) {
4183 wpa_printf(MSG_DEBUG, "P2P: Requested persistent group is "
4184 "already running");
4185 return 0;
4186 }
4187
4188 /* Make sure we are not running find during connection establishment */
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08004189 wpas_p2p_stop_find_oper(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004190
Dmitry Shmidt04949592012-07-19 12:16:46 -07004191 wpa_s->p2p_fallback_to_go_neg = 0;
4192
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004193 if (ssid->mode == WPAS_MODE_INFRA)
4194 return wpas_start_p2p_client(wpa_s, ssid, addr_allocated);
4195
4196 if (ssid->mode != WPAS_MODE_P2P_GO)
4197 return -1;
4198
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004199 if (wpas_p2p_init_go_params(wpa_s, &params, freq, ht40))
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004200 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004201
4202 params.role_go = 1;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08004203 params.psk_set = ssid->psk_set;
4204 if (params.psk_set)
4205 os_memcpy(params.psk, ssid->psk, sizeof(params.psk));
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08004206 if (ssid->passphrase) {
4207 if (os_strlen(ssid->passphrase) >= sizeof(params.passphrase)) {
4208 wpa_printf(MSG_ERROR, "P2P: Invalid passphrase in "
4209 "persistent group");
4210 return -1;
4211 }
4212 os_strlcpy(params.passphrase, ssid->passphrase,
4213 sizeof(params.passphrase));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004214 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004215 os_memcpy(params.ssid, ssid->ssid, ssid->ssid_len);
4216 params.ssid_len = ssid->ssid_len;
4217 params.persistent_group = 1;
4218
4219 wpa_s = wpas_p2p_get_group_iface(wpa_s, addr_allocated, 1);
4220 if (wpa_s == NULL)
4221 return -1;
4222
4223 wpas_start_wps_go(wpa_s, &params, 0);
4224
4225 return 0;
4226}
4227
4228
4229static void wpas_p2p_ie_update(void *ctx, struct wpabuf *beacon_ies,
4230 struct wpabuf *proberesp_ies)
4231{
4232 struct wpa_supplicant *wpa_s = ctx;
4233 if (wpa_s->ap_iface) {
4234 struct hostapd_data *hapd = wpa_s->ap_iface->bss[0];
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004235 if (!(hapd->conf->p2p & P2P_GROUP_OWNER)) {
4236 wpabuf_free(beacon_ies);
4237 wpabuf_free(proberesp_ies);
4238 return;
4239 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004240 if (beacon_ies) {
4241 wpabuf_free(hapd->p2p_beacon_ie);
4242 hapd->p2p_beacon_ie = beacon_ies;
4243 }
4244 wpabuf_free(hapd->p2p_probe_resp_ie);
4245 hapd->p2p_probe_resp_ie = proberesp_ies;
4246 } else {
4247 wpabuf_free(beacon_ies);
4248 wpabuf_free(proberesp_ies);
4249 }
4250 wpa_supplicant_ap_update_beacon(wpa_s);
4251}
4252
4253
4254static void wpas_p2p_idle_update(void *ctx, int idle)
4255{
4256 struct wpa_supplicant *wpa_s = ctx;
4257 if (!wpa_s->ap_iface)
4258 return;
4259 wpa_printf(MSG_DEBUG, "P2P: GO - group %sidle", idle ? "" : "not ");
Dmitry Shmidt04949592012-07-19 12:16:46 -07004260 if (idle)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004261 wpas_p2p_set_group_idle_timeout(wpa_s);
4262 else
4263 eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL);
4264}
4265
4266
4267struct p2p_group * wpas_p2p_group_init(struct wpa_supplicant *wpa_s,
Dmitry Shmidt04949592012-07-19 12:16:46 -07004268 struct wpa_ssid *ssid)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004269{
4270 struct p2p_group *group;
4271 struct p2p_group_config *cfg;
4272
4273 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
4274 return NULL;
4275 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
4276 return NULL;
4277
4278 cfg = os_zalloc(sizeof(*cfg));
4279 if (cfg == NULL)
4280 return NULL;
4281
Dmitry Shmidt04949592012-07-19 12:16:46 -07004282 if (ssid->p2p_persistent_group && wpa_s->conf->persistent_reconnect)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004283 cfg->persistent_group = 2;
Dmitry Shmidt04949592012-07-19 12:16:46 -07004284 else if (ssid->p2p_persistent_group)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004285 cfg->persistent_group = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004286 os_memcpy(cfg->interface_addr, wpa_s->own_addr, ETH_ALEN);
4287 if (wpa_s->max_stations &&
4288 wpa_s->max_stations < wpa_s->conf->max_num_sta)
4289 cfg->max_clients = wpa_s->max_stations;
4290 else
4291 cfg->max_clients = wpa_s->conf->max_num_sta;
Dmitry Shmidt04949592012-07-19 12:16:46 -07004292 os_memcpy(cfg->ssid, ssid->ssid, ssid->ssid_len);
4293 cfg->ssid_len = ssid->ssid_len;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004294 cfg->cb_ctx = wpa_s;
4295 cfg->ie_update = wpas_p2p_ie_update;
4296 cfg->idle_update = wpas_p2p_idle_update;
4297
4298 group = p2p_group_init(wpa_s->global->p2p, cfg);
4299 if (group == NULL)
4300 os_free(cfg);
Dmitry Shmidt04949592012-07-19 12:16:46 -07004301 if (ssid->mode != WPAS_MODE_P2P_GROUP_FORMATION)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004302 p2p_group_notif_formation_done(group);
4303 wpa_s->p2p_group = group;
4304 return group;
4305}
4306
4307
4308void wpas_p2p_wps_success(struct wpa_supplicant *wpa_s, const u8 *peer_addr,
4309 int registrar)
4310{
Dmitry Shmidt04949592012-07-19 12:16:46 -07004311 struct wpa_ssid *ssid = wpa_s->current_ssid;
4312
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004313 if (!wpa_s->p2p_in_provisioning) {
4314 wpa_printf(MSG_DEBUG, "P2P: Ignore WPS success event - P2P "
4315 "provisioning not in progress");
4316 return;
4317 }
4318
Dmitry Shmidt04949592012-07-19 12:16:46 -07004319 if (ssid && ssid->mode == WPAS_MODE_INFRA) {
4320 u8 go_dev_addr[ETH_ALEN];
4321 os_memcpy(go_dev_addr, wpa_s->bssid, ETH_ALEN);
4322 wpas_p2p_persistent_group(wpa_s, go_dev_addr, ssid->ssid,
4323 ssid->ssid_len);
4324 /* Clear any stored provisioning info */
4325 p2p_clear_provisioning_info(wpa_s->global->p2p, go_dev_addr);
4326 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004327
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004328 eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s->parent,
4329 NULL);
Dmitry Shmidt04949592012-07-19 12:16:46 -07004330 if (ssid && ssid->mode == WPAS_MODE_INFRA) {
4331 /*
4332 * Use a separate timeout for initial data connection to
4333 * complete to allow the group to be removed automatically if
4334 * something goes wrong in this step before the P2P group idle
4335 * timeout mechanism is taken into use.
4336 */
4337 eloop_register_timeout(P2P_MAX_INITIAL_CONN_WAIT, 0,
4338 wpas_p2p_group_formation_timeout,
4339 wpa_s->parent, NULL);
4340 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004341 if (wpa_s->global->p2p)
4342 p2p_wps_success_cb(wpa_s->global->p2p, peer_addr);
4343 else if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
4344 wpa_drv_wps_success_cb(wpa_s, peer_addr);
4345 wpas_group_formation_completed(wpa_s, 1);
4346}
4347
4348
Jouni Malinen75ecf522011-06-27 15:19:46 -07004349void wpas_p2p_wps_failed(struct wpa_supplicant *wpa_s,
4350 struct wps_event_fail *fail)
4351{
4352 if (!wpa_s->p2p_in_provisioning) {
4353 wpa_printf(MSG_DEBUG, "P2P: Ignore WPS fail event - P2P "
4354 "provisioning not in progress");
4355 return;
4356 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004357
4358 if (wpa_s->go_params) {
4359 p2p_clear_provisioning_info(
4360 wpa_s->global->p2p,
Dmitry Shmidt04949592012-07-19 12:16:46 -07004361 wpa_s->go_params->peer_device_addr);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004362 }
4363
Jouni Malinen75ecf522011-06-27 15:19:46 -07004364 wpas_notify_p2p_wps_failed(wpa_s, fail);
4365}
4366
4367
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004368int wpas_p2p_prov_disc(struct wpa_supplicant *wpa_s, const u8 *peer_addr,
Dmitry Shmidt04949592012-07-19 12:16:46 -07004369 const char *config_method,
4370 enum wpas_p2p_prov_disc_use use)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004371{
4372 u16 config_methods;
4373
Dmitry Shmidt04949592012-07-19 12:16:46 -07004374 wpa_s->p2p_fallback_to_go_neg = 0;
4375 wpa_s->pending_pd_use = NORMAL_PD;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004376 if (os_strncmp(config_method, "display", 7) == 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004377 config_methods = WPS_CONFIG_DISPLAY;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004378 else if (os_strncmp(config_method, "keypad", 6) == 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004379 config_methods = WPS_CONFIG_KEYPAD;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004380 else if (os_strncmp(config_method, "pbc", 3) == 0 ||
4381 os_strncmp(config_method, "pushbutton", 10) == 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004382 config_methods = WPS_CONFIG_PUSHBUTTON;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004383 else {
4384 wpa_printf(MSG_DEBUG, "P2P: Unknown config method");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004385 return -1;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004386 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004387
Dmitry Shmidt04949592012-07-19 12:16:46 -07004388 if (use == WPAS_P2P_PD_AUTO) {
4389 os_memcpy(wpa_s->pending_join_dev_addr, peer_addr, ETH_ALEN);
4390 wpa_s->pending_pd_config_methods = config_methods;
4391 wpa_s->p2p_auto_pd = 1;
4392 wpa_s->p2p_auto_join = 0;
4393 wpa_s->pending_pd_before_join = 0;
4394 wpa_s->auto_pd_scan_retry = 0;
4395 wpas_p2p_stop_find(wpa_s);
4396 wpa_s->p2p_join_scan_count = 0;
4397 os_get_time(&wpa_s->p2p_auto_started);
4398 wpa_printf(MSG_DEBUG, "P2P: Auto PD started at %ld.%06ld",
4399 wpa_s->p2p_auto_started.sec,
4400 wpa_s->p2p_auto_started.usec);
4401 wpas_p2p_join_scan(wpa_s, NULL);
4402 return 0;
4403 }
4404
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004405 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT) {
4406 return wpa_drv_p2p_prov_disc_req(wpa_s, peer_addr,
Dmitry Shmidt04949592012-07-19 12:16:46 -07004407 config_methods,
4408 use == WPAS_P2P_PD_FOR_JOIN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004409 }
4410
4411 if (wpa_s->global->p2p == NULL || wpa_s->global->p2p_disabled)
4412 return -1;
4413
4414 return p2p_prov_disc_req(wpa_s->global->p2p, peer_addr,
Dmitry Shmidt04949592012-07-19 12:16:46 -07004415 config_methods, use == WPAS_P2P_PD_FOR_JOIN,
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08004416 0, 1);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004417}
4418
4419
4420int wpas_p2p_scan_result_text(const u8 *ies, size_t ies_len, char *buf,
4421 char *end)
4422{
4423 return p2p_scan_result_text(ies, ies_len, buf, end);
4424}
4425
4426
4427static void wpas_p2p_clear_pending_action_tx(struct wpa_supplicant *wpa_s)
4428{
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08004429 if (!offchannel_pending_action_tx(wpa_s))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004430 return;
4431
4432 wpa_printf(MSG_DEBUG, "P2P: Drop pending Action TX due to new "
4433 "operation request");
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08004434 offchannel_clear_pending_action_tx(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004435}
4436
4437
4438int wpas_p2p_find(struct wpa_supplicant *wpa_s, unsigned int timeout,
4439 enum p2p_discovery_type type,
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08004440 unsigned int num_req_dev_types, const u8 *req_dev_types,
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004441 const u8 *dev_id, unsigned int search_delay)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004442{
4443 wpas_p2p_clear_pending_action_tx(wpa_s);
4444 wpa_s->p2p_long_listen = 0;
4445
4446 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
4447 return wpa_drv_p2p_find(wpa_s, timeout, type);
4448
Dmitry Shmidt04949592012-07-19 12:16:46 -07004449 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL ||
4450 wpa_s->p2p_in_provisioning)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004451 return -1;
4452
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08004453 wpa_supplicant_cancel_sched_scan(wpa_s);
4454
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004455 return p2p_find(wpa_s->global->p2p, timeout, type,
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004456 num_req_dev_types, req_dev_types, dev_id,
4457 search_delay);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004458}
4459
4460
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08004461static int wpas_p2p_stop_find_oper(struct wpa_supplicant *wpa_s)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004462{
4463 wpas_p2p_clear_pending_action_tx(wpa_s);
4464 wpa_s->p2p_long_listen = 0;
4465 eloop_cancel_timeout(wpas_p2p_long_listen_timeout, wpa_s, NULL);
4466 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
Jouni Malinendc7b7132012-09-14 12:53:47 -07004467 wpa_s->global->p2p_cb_on_scan_complete = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004468
4469 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT) {
4470 wpa_drv_p2p_stop_find(wpa_s);
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08004471 return 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004472 }
4473
4474 if (wpa_s->global->p2p)
4475 p2p_stop_find(wpa_s->global->p2p);
4476
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08004477 return 0;
4478}
4479
4480
4481void wpas_p2p_stop_find(struct wpa_supplicant *wpa_s)
4482{
4483 if (wpas_p2p_stop_find_oper(wpa_s) > 0)
4484 return;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004485 wpas_p2p_remove_pending_group_interface(wpa_s);
4486}
4487
4488
4489static void wpas_p2p_long_listen_timeout(void *eloop_ctx, void *timeout_ctx)
4490{
4491 struct wpa_supplicant *wpa_s = eloop_ctx;
4492 wpa_s->p2p_long_listen = 0;
4493}
4494
4495
4496int wpas_p2p_listen(struct wpa_supplicant *wpa_s, unsigned int timeout)
4497{
4498 int res;
4499
4500 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
4501 return -1;
4502
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08004503 wpa_supplicant_cancel_sched_scan(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004504 wpas_p2p_clear_pending_action_tx(wpa_s);
4505
4506 if (timeout == 0) {
4507 /*
4508 * This is a request for unlimited Listen state. However, at
4509 * least for now, this is mapped to a Listen state for one
4510 * hour.
4511 */
4512 timeout = 3600;
4513 }
4514 eloop_cancel_timeout(wpas_p2p_long_listen_timeout, wpa_s, NULL);
4515 wpa_s->p2p_long_listen = 0;
4516
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004517 /*
4518 * Stop previous find/listen operation to avoid trying to request a new
4519 * remain-on-channel operation while the driver is still running the
4520 * previous one.
4521 */
4522 if (wpa_s->global->p2p)
4523 p2p_stop_find(wpa_s->global->p2p);
4524
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004525 res = wpas_p2p_listen_start(wpa_s, timeout * 1000);
4526 if (res == 0 && timeout * 1000 > wpa_s->max_remain_on_chan) {
4527 wpa_s->p2p_long_listen = timeout * 1000;
4528 eloop_register_timeout(timeout, 0,
4529 wpas_p2p_long_listen_timeout,
4530 wpa_s, NULL);
4531 }
4532
4533 return res;
4534}
4535
4536
4537int wpas_p2p_assoc_req_ie(struct wpa_supplicant *wpa_s, struct wpa_bss *bss,
4538 u8 *buf, size_t len, int p2p_group)
4539{
4540 struct wpabuf *p2p_ie;
4541 int ret;
4542
4543 if (wpa_s->global->p2p_disabled)
4544 return -1;
4545 if (wpa_s->global->p2p == NULL)
4546 return -1;
4547 if (bss == NULL)
4548 return -1;
4549
4550 p2p_ie = wpa_bss_get_vendor_ie_multi(bss, P2P_IE_VENDOR_TYPE);
4551 ret = p2p_assoc_req_ie(wpa_s->global->p2p, bss->bssid, buf, len,
4552 p2p_group, p2p_ie);
4553 wpabuf_free(p2p_ie);
4554
4555 return ret;
4556}
4557
4558
4559int wpas_p2p_probe_req_rx(struct wpa_supplicant *wpa_s, const u8 *addr,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004560 const u8 *dst, const u8 *bssid,
Dmitry Shmidt04949592012-07-19 12:16:46 -07004561 const u8 *ie, size_t ie_len, int ssi_signal)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004562{
4563 if (wpa_s->global->p2p_disabled)
4564 return 0;
4565 if (wpa_s->global->p2p == NULL)
4566 return 0;
4567
Dmitry Shmidt04949592012-07-19 12:16:46 -07004568 switch (p2p_probe_req_rx(wpa_s->global->p2p, addr, dst, bssid,
4569 ie, ie_len)) {
4570 case P2P_PREQ_NOT_P2P:
4571 wpas_notify_preq(wpa_s, addr, dst, bssid, ie, ie_len,
4572 ssi_signal);
4573 /* fall through */
4574 case P2P_PREQ_MALFORMED:
4575 case P2P_PREQ_NOT_LISTEN:
4576 case P2P_PREQ_NOT_PROCESSED:
4577 default: /* make gcc happy */
4578 return 0;
4579 case P2P_PREQ_PROCESSED:
4580 return 1;
4581 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004582}
4583
4584
4585void wpas_p2p_rx_action(struct wpa_supplicant *wpa_s, const u8 *da,
4586 const u8 *sa, const u8 *bssid,
4587 u8 category, const u8 *data, size_t len, int freq)
4588{
4589 if (wpa_s->global->p2p_disabled)
4590 return;
4591 if (wpa_s->global->p2p == NULL)
4592 return;
4593
4594 p2p_rx_action(wpa_s->global->p2p, da, sa, bssid, category, data, len,
4595 freq);
4596}
4597
4598
4599void wpas_p2p_scan_ie(struct wpa_supplicant *wpa_s, struct wpabuf *ies)
4600{
4601 if (wpa_s->global->p2p_disabled)
4602 return;
4603 if (wpa_s->global->p2p == NULL)
4604 return;
4605
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08004606 p2p_scan_ie(wpa_s->global->p2p, ies, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004607}
4608
4609
4610void wpas_p2p_group_deinit(struct wpa_supplicant *wpa_s)
4611{
4612 p2p_group_deinit(wpa_s->p2p_group);
4613 wpa_s->p2p_group = NULL;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004614
4615 wpa_s->ap_configured_cb = NULL;
4616 wpa_s->ap_configured_cb_ctx = NULL;
4617 wpa_s->ap_configured_cb_data = NULL;
4618 wpa_s->connect_without_scan = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004619}
4620
4621
4622int wpas_p2p_reject(struct wpa_supplicant *wpa_s, const u8 *addr)
4623{
4624 wpa_s->p2p_long_listen = 0;
4625
4626 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
4627 return wpa_drv_p2p_reject(wpa_s, addr);
4628
4629 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
4630 return -1;
4631
4632 return p2p_reject(wpa_s->global->p2p, addr);
4633}
4634
4635
4636/* Invite to reinvoke a persistent group */
4637int wpas_p2p_invite(struct wpa_supplicant *wpa_s, const u8 *peer_addr,
Jouni Malinen31be0a42012-08-31 21:20:51 +03004638 struct wpa_ssid *ssid, const u8 *go_dev_addr, int freq,
4639 int ht40)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004640{
4641 enum p2p_invite_role role;
4642 u8 *bssid = NULL;
Irfan Sherifff0619662012-09-23 19:11:37 -07004643#ifdef ANDROID_P2P
Irfan Sheriffaf84a572012-09-22 16:59:30 -07004644 int force_freq = 0, oper_freq = 0;
Irfan Sherifff0619662012-09-23 19:11:37 -07004645#endif
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004646
Jouni Malinen31be0a42012-08-31 21:20:51 +03004647 wpa_s->p2p_persistent_go_freq = freq;
4648 wpa_s->p2p_go_ht40 = !!ht40;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004649 if (ssid->mode == WPAS_MODE_P2P_GO) {
4650 role = P2P_INVITE_ROLE_GO;
4651 if (peer_addr == NULL) {
4652 wpa_printf(MSG_DEBUG, "P2P: Missing peer "
4653 "address in invitation command");
4654 return -1;
4655 }
4656 if (wpas_p2p_create_iface(wpa_s)) {
4657 if (wpas_p2p_add_group_interface(wpa_s,
4658 WPA_IF_P2P_GO) < 0) {
4659 wpa_printf(MSG_ERROR, "P2P: Failed to "
4660 "allocate a new interface for the "
4661 "group");
4662 return -1;
4663 }
4664 bssid = wpa_s->pending_interface_addr;
4665 } else
4666 bssid = wpa_s->own_addr;
4667 } else {
4668 role = P2P_INVITE_ROLE_CLIENT;
4669 peer_addr = ssid->bssid;
4670 }
4671 wpa_s->pending_invite_ssid_id = ssid->id;
4672
Irfan Sherifff0619662012-09-23 19:11:37 -07004673#ifdef ANDROID_P2P
Irfan Sheriffaf84a572012-09-22 16:59:30 -07004674 if (wpa_s->current_ssid && wpa_drv_get_bssid(wpa_s, bssid) == 0 &&
4675 wpa_s->assoc_freq)
4676 oper_freq = wpa_s->assoc_freq;
4677 else {
4678 oper_freq = wpa_drv_shared_freq(wpa_s);
4679 if (oper_freq < 0)
4680 oper_freq = 0;
4681 }
4682
4683 if (freq > 0) {
4684 if (!p2p_supported_freq(wpa_s->global->p2p, freq)) {
4685 wpa_printf(MSG_DEBUG, "P2P: The forced channel "
4686 "(%u MHz) is not supported for P2P uses",
4687 freq);
4688 return -3;
4689 }
4690
4691 if (oper_freq > 0 && freq != oper_freq &&
4692 !(wpa_s->drv_flags &
4693 WPA_DRIVER_FLAGS_MULTI_CHANNEL_CONCURRENT)) {
4694 wpa_printf(MSG_DEBUG, "P2P: Cannot start P2P group "
4695 "on %u MHz while connected on another "
4696 "channel (%u MHz)", freq, oper_freq);
4697 return -2;
4698 }
4699 wpa_printf(MSG_DEBUG, "P2P: Trying to force us to use the "
4700 "requested channel (%u MHz)", freq);
4701 force_freq = freq;
4702 } else if (oper_freq > 0 &&
4703 !p2p_supported_freq(wpa_s->global->p2p, oper_freq)) {
4704 if (!(wpa_s->drv_flags &
4705 WPA_DRIVER_FLAGS_MULTI_CHANNEL_CONCURRENT)) {
4706 wpa_printf(MSG_DEBUG, "P2P: Cannot start P2P group "
4707 "while connected on non-P2P supported "
4708 "channel (%u MHz)", oper_freq);
4709 return -2;
4710 }
4711 wpa_printf(MSG_DEBUG, "P2P: Current operating channel "
4712 "(%u MHz) not available for P2P - try to use "
4713 "another channel", oper_freq);
4714 force_freq = 0;
4715 } else if (oper_freq > 0) {
4716 wpa_printf(MSG_DEBUG, "P2P: Trying to force us to use the "
4717 "channel we are already using (%u MHz) on another "
4718 "interface", oper_freq);
4719 force_freq = oper_freq;
4720 }
Irfan Sherifff0619662012-09-23 19:11:37 -07004721#endif
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004722 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
4723 return wpa_drv_p2p_invite(wpa_s, peer_addr, role, bssid,
4724 ssid->ssid, ssid->ssid_len,
4725 go_dev_addr, 1);
4726
4727 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
4728 return -1;
4729
Irfan Sherifff0619662012-09-23 19:11:37 -07004730#ifdef ANDROID_P2P
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004731 return p2p_invite(wpa_s->global->p2p, peer_addr, role, bssid,
Irfan Sheriffaf84a572012-09-22 16:59:30 -07004732 ssid->ssid, ssid->ssid_len, force_freq, go_dev_addr, 1);
Irfan Sherifff0619662012-09-23 19:11:37 -07004733#else
4734 return p2p_invite(wpa_s->global->p2p, peer_addr, role, bssid,
4735 ssid->ssid, ssid->ssid_len, freq, go_dev_addr, 1);
4736#endif
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004737}
4738
4739
4740/* Invite to join an active group */
4741int wpas_p2p_invite_group(struct wpa_supplicant *wpa_s, const char *ifname,
4742 const u8 *peer_addr, const u8 *go_dev_addr)
4743{
4744 struct wpa_global *global = wpa_s->global;
4745 enum p2p_invite_role role;
4746 u8 *bssid = NULL;
4747 struct wpa_ssid *ssid;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08004748 int persistent;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004749
Jouni Malinen31be0a42012-08-31 21:20:51 +03004750 wpa_s->p2p_persistent_go_freq = 0;
4751 wpa_s->p2p_go_ht40 = 0;
4752
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004753 for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
4754 if (os_strcmp(wpa_s->ifname, ifname) == 0)
4755 break;
4756 }
4757 if (wpa_s == NULL) {
4758 wpa_printf(MSG_DEBUG, "P2P: Interface '%s' not found", ifname);
4759 return -1;
4760 }
4761
4762 ssid = wpa_s->current_ssid;
4763 if (ssid == NULL) {
4764 wpa_printf(MSG_DEBUG, "P2P: No current SSID to use for "
4765 "invitation");
4766 return -1;
4767 }
4768
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08004769 persistent = ssid->p2p_persistent_group &&
4770 wpas_p2p_get_persistent(wpa_s->parent, peer_addr,
4771 ssid->ssid, ssid->ssid_len);
4772
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004773 if (ssid->mode == WPAS_MODE_P2P_GO) {
4774 role = P2P_INVITE_ROLE_ACTIVE_GO;
4775 bssid = wpa_s->own_addr;
4776 if (go_dev_addr == NULL)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004777 go_dev_addr = wpa_s->global->p2p_dev_addr;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004778 } else {
4779 role = P2P_INVITE_ROLE_CLIENT;
4780 if (wpa_s->wpa_state < WPA_ASSOCIATED) {
4781 wpa_printf(MSG_DEBUG, "P2P: Not associated - cannot "
4782 "invite to current group");
4783 return -1;
4784 }
4785 bssid = wpa_s->bssid;
4786 if (go_dev_addr == NULL &&
4787 !is_zero_ether_addr(wpa_s->go_dev_addr))
4788 go_dev_addr = wpa_s->go_dev_addr;
4789 }
4790 wpa_s->parent->pending_invite_ssid_id = -1;
4791
4792 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
4793 return wpa_drv_p2p_invite(wpa_s, peer_addr, role, bssid,
4794 ssid->ssid, ssid->ssid_len,
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08004795 go_dev_addr, persistent);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004796
4797 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
4798 return -1;
4799
4800 return p2p_invite(wpa_s->global->p2p, peer_addr, role, bssid,
4801 ssid->ssid, ssid->ssid_len, wpa_s->assoc_freq,
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08004802 go_dev_addr, persistent);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004803}
4804
4805
4806void wpas_p2p_completed(struct wpa_supplicant *wpa_s)
4807{
4808 struct wpa_ssid *ssid = wpa_s->current_ssid;
4809 const char *ssid_txt;
4810 u8 go_dev_addr[ETH_ALEN];
Jouni Malinen75ecf522011-06-27 15:19:46 -07004811 int network_id = -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004812 int persistent;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004813 int freq;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004814
Dmitry Shmidt04949592012-07-19 12:16:46 -07004815 if (ssid == NULL || ssid->mode != WPAS_MODE_P2P_GROUP_FORMATION) {
4816 eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
4817 wpa_s->parent, NULL);
4818 }
4819
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004820 if (!wpa_s->show_group_started || !ssid)
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004821 goto done;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004822
4823 wpa_s->show_group_started = 0;
4824
4825 ssid_txt = wpa_ssid_txt(ssid->ssid, ssid->ssid_len);
4826 os_memset(go_dev_addr, 0, ETH_ALEN);
4827 if (ssid->bssid_set)
4828 os_memcpy(go_dev_addr, ssid->bssid, ETH_ALEN);
4829 persistent = wpas_p2p_persistent_group(wpa_s, go_dev_addr, ssid->ssid,
4830 ssid->ssid_len);
4831 os_memcpy(wpa_s->go_dev_addr, go_dev_addr, ETH_ALEN);
4832
4833 if (wpa_s->global->p2p_group_formation == wpa_s)
4834 wpa_s->global->p2p_group_formation = NULL;
4835
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004836 freq = wpa_s->current_bss ? wpa_s->current_bss->freq :
4837 (int) wpa_s->assoc_freq;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004838 if (ssid->passphrase == NULL && ssid->psk_set) {
4839 char psk[65];
4840 wpa_snprintf_hex(psk, sizeof(psk), ssid->psk, 32);
4841 wpa_msg(wpa_s->parent, MSG_INFO, P2P_EVENT_GROUP_STARTED
4842 "%s client ssid=\"%s\" freq=%d psk=%s go_dev_addr="
4843 MACSTR "%s",
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004844 wpa_s->ifname, ssid_txt, freq, psk,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004845 MAC2STR(go_dev_addr),
4846 persistent ? " [PERSISTENT]" : "");
4847 } else {
4848 wpa_msg(wpa_s->parent, MSG_INFO, P2P_EVENT_GROUP_STARTED
4849 "%s client ssid=\"%s\" freq=%d passphrase=\"%s\" "
4850 "go_dev_addr=" MACSTR "%s",
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004851 wpa_s->ifname, ssid_txt, freq,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004852 ssid->passphrase ? ssid->passphrase : "",
4853 MAC2STR(go_dev_addr),
4854 persistent ? " [PERSISTENT]" : "");
4855 }
4856
4857 if (persistent)
Jouni Malinen75ecf522011-06-27 15:19:46 -07004858 network_id = wpas_p2p_store_persistent_group(wpa_s->parent,
4859 ssid, go_dev_addr);
4860 if (network_id < 0)
4861 network_id = ssid->id;
4862 wpas_notify_p2p_group_started(wpa_s, ssid, network_id, 1);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004863
4864done:
Jouni Malinendc7b7132012-09-14 12:53:47 -07004865 if (wpa_s->global->p2p_cb_on_scan_complete && !wpa_s->global->p2p_disabled &&
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004866 wpa_s->global->p2p != NULL) {
Jouni Malinendc7b7132012-09-14 12:53:47 -07004867 wpa_s->global->p2p_cb_on_scan_complete = 0;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004868 if (p2p_other_scan_completed(wpa_s->global->p2p) == 1) {
4869 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Pending P2P operation "
4870 "continued after successful connection");
4871 p2p_increase_search_delay(
4872 wpa_s->global->p2p,
4873 wpas_p2p_search_delay(wpa_s));
4874 }
4875 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004876}
4877
4878
4879int wpas_p2p_presence_req(struct wpa_supplicant *wpa_s, u32 duration1,
4880 u32 interval1, u32 duration2, u32 interval2)
4881{
4882 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
4883 return -1;
4884 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
4885 return -1;
4886
4887 if (wpa_s->wpa_state < WPA_ASSOCIATED ||
4888 wpa_s->current_ssid == NULL ||
4889 wpa_s->current_ssid->mode != WPAS_MODE_INFRA)
4890 return -1;
4891
4892 return p2p_presence_req(wpa_s->global->p2p, wpa_s->bssid,
4893 wpa_s->own_addr, wpa_s->assoc_freq,
4894 duration1, interval1, duration2, interval2);
4895}
4896
4897
4898int wpas_p2p_ext_listen(struct wpa_supplicant *wpa_s, unsigned int period,
4899 unsigned int interval)
4900{
4901 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
4902 return -1;
4903
4904 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
4905 return -1;
4906
4907 return p2p_ext_listen(wpa_s->global->p2p, period, interval);
4908}
4909
4910
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004911static int wpas_p2p_is_client(struct wpa_supplicant *wpa_s)
4912{
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08004913 if (wpa_s->current_ssid == NULL) {
4914 /*
4915 * current_ssid can be cleared when P2P client interface gets
4916 * disconnected, so assume this interface was used as P2P
4917 * client.
4918 */
4919 return 1;
4920 }
4921 return wpa_s->current_ssid->p2p_group &&
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004922 wpa_s->current_ssid->mode == WPAS_MODE_INFRA;
4923}
4924
4925
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004926static void wpas_p2p_group_idle_timeout(void *eloop_ctx, void *timeout_ctx)
4927{
4928 struct wpa_supplicant *wpa_s = eloop_ctx;
4929
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004930 if (wpa_s->conf->p2p_group_idle == 0 && !wpas_p2p_is_client(wpa_s)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004931 wpa_printf(MSG_DEBUG, "P2P: Ignore group idle timeout - "
4932 "disabled");
4933 return;
4934 }
4935
Dmitry Shmidt04949592012-07-19 12:16:46 -07004936 wpa_printf(MSG_DEBUG, "P2P: Group idle timeout reached - terminate "
4937 "group");
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004938 wpas_p2p_group_delete(wpa_s, P2P_GROUP_REMOVAL_IDLE_TIMEOUT);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004939}
4940
4941
4942static void wpas_p2p_set_group_idle_timeout(struct wpa_supplicant *wpa_s)
4943{
Dmitry Shmidt04949592012-07-19 12:16:46 -07004944 int timeout;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004945
Dmitry Shmidt04949592012-07-19 12:16:46 -07004946 if (eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL) > 0)
4947 wpa_printf(MSG_DEBUG, "P2P: Cancelled P2P group idle timeout");
4948
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004949 if (wpa_s->current_ssid == NULL || !wpa_s->current_ssid->p2p_group)
4950 return;
4951
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004952 timeout = wpa_s->conf->p2p_group_idle;
4953 if (wpa_s->current_ssid->mode == WPAS_MODE_INFRA &&
4954 (timeout == 0 || timeout > P2P_MAX_CLIENT_IDLE))
4955 timeout = P2P_MAX_CLIENT_IDLE;
4956
4957 if (timeout == 0)
4958 return;
4959
Dmitry Shmidt04949592012-07-19 12:16:46 -07004960 if (timeout < 0) {
4961 if (wpa_s->current_ssid->mode == WPAS_MODE_INFRA)
4962 timeout = 0; /* special client mode no-timeout */
4963 else
4964 return;
4965 }
4966
4967 if (wpa_s->p2p_in_provisioning) {
4968 /*
4969 * Use the normal group formation timeout during the
4970 * provisioning phase to avoid terminating this process too
4971 * early due to group idle timeout.
4972 */
4973 wpa_printf(MSG_DEBUG, "P2P: Do not use P2P group idle timeout "
4974 "during provisioning");
4975 return;
4976 }
Dmitry Shmidtad266fb2012-08-24 17:03:35 -07004977#ifndef ANDROID_P2P
Dmitry Shmidt04949592012-07-19 12:16:46 -07004978 if (wpa_s->show_group_started) {
4979 /*
4980 * Use the normal group formation timeout between the end of
4981 * the provisioning phase and completion of 4-way handshake to
4982 * avoid terminating this process too early due to group idle
4983 * timeout.
4984 */
4985 wpa_printf(MSG_DEBUG, "P2P: Do not use P2P group idle timeout "
4986 "while waiting for initial 4-way handshake to "
4987 "complete");
4988 return;
4989 }
Dmitry Shmidtad266fb2012-08-24 17:03:35 -07004990#endif
Dmitry Shmidt04949592012-07-19 12:16:46 -07004991
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004992 wpa_printf(MSG_DEBUG, "P2P: Set P2P group idle timeout to %u seconds",
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004993 timeout);
4994 eloop_register_timeout(timeout, 0, wpas_p2p_group_idle_timeout,
4995 wpa_s, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004996}
4997
4998
Jouni Malinen2b89da82012-08-31 22:04:41 +03004999/* Returns 1 if the interface was removed */
5000int wpas_p2p_deauth_notif(struct wpa_supplicant *wpa_s, const u8 *bssid,
5001 u16 reason_code, const u8 *ie, size_t ie_len,
5002 int locally_generated)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005003{
5004 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
Jouni Malinen2b89da82012-08-31 22:04:41 +03005005 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005006 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
Jouni Malinen2b89da82012-08-31 22:04:41 +03005007 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005008
Dmitry Shmidt04949592012-07-19 12:16:46 -07005009 if (!locally_generated)
5010 p2p_deauth_notif(wpa_s->global->p2p, bssid, reason_code, ie,
5011 ie_len);
5012
5013 if (reason_code == WLAN_REASON_DEAUTH_LEAVING && !locally_generated &&
5014 wpa_s->current_ssid &&
5015 wpa_s->current_ssid->p2p_group &&
5016 wpa_s->current_ssid->mode == WPAS_MODE_INFRA) {
5017 wpa_printf(MSG_DEBUG, "P2P: GO indicated that the P2P Group "
5018 "session is ending");
Jouni Malinen2b89da82012-08-31 22:04:41 +03005019 if (wpas_p2p_group_delete(wpa_s,
5020 P2P_GROUP_REMOVAL_GO_ENDING_SESSION)
5021 > 0)
5022 return 1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07005023 }
Jouni Malinen2b89da82012-08-31 22:04:41 +03005024
5025 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005026}
5027
5028
5029void wpas_p2p_disassoc_notif(struct wpa_supplicant *wpa_s, const u8 *bssid,
Dmitry Shmidt04949592012-07-19 12:16:46 -07005030 u16 reason_code, const u8 *ie, size_t ie_len,
5031 int locally_generated)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005032{
5033 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
5034 return;
5035 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
5036 return;
5037
Dmitry Shmidt04949592012-07-19 12:16:46 -07005038 if (!locally_generated)
5039 p2p_disassoc_notif(wpa_s->global->p2p, bssid, reason_code, ie,
5040 ie_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005041}
5042
5043
5044void wpas_p2p_update_config(struct wpa_supplicant *wpa_s)
5045{
5046 struct p2p_data *p2p = wpa_s->global->p2p;
5047
5048 if (p2p == NULL)
5049 return;
5050
5051 if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_CAPABLE))
5052 return;
5053
5054 if (wpa_s->conf->changed_parameters & CFG_CHANGED_DEVICE_NAME)
5055 p2p_set_dev_name(p2p, wpa_s->conf->device_name);
5056
5057 if (wpa_s->conf->changed_parameters & CFG_CHANGED_DEVICE_TYPE)
5058 p2p_set_pri_dev_type(p2p, wpa_s->conf->device_type);
5059
5060 if (wpa_s->wps &&
5061 (wpa_s->conf->changed_parameters & CFG_CHANGED_CONFIG_METHODS))
5062 p2p_set_config_methods(p2p, wpa_s->wps->config_methods);
5063
5064 if (wpa_s->wps && (wpa_s->conf->changed_parameters & CFG_CHANGED_UUID))
5065 p2p_set_uuid(p2p, wpa_s->wps->uuid);
5066
5067 if (wpa_s->conf->changed_parameters & CFG_CHANGED_WPS_STRING) {
5068 p2p_set_manufacturer(p2p, wpa_s->conf->manufacturer);
5069 p2p_set_model_name(p2p, wpa_s->conf->model_name);
5070 p2p_set_model_number(p2p, wpa_s->conf->model_number);
5071 p2p_set_serial_number(p2p, wpa_s->conf->serial_number);
5072 }
5073
5074 if (wpa_s->conf->changed_parameters & CFG_CHANGED_SEC_DEVICE_TYPE)
5075 p2p_set_sec_dev_types(p2p,
5076 (void *) wpa_s->conf->sec_device_type,
5077 wpa_s->conf->num_sec_device_types);
5078
5079 if (wpa_s->conf->changed_parameters & CFG_CHANGED_VENDOR_EXTENSION) {
5080 int i;
5081 p2p_remove_wps_vendor_extensions(p2p);
5082 for (i = 0; i < MAX_WPS_VENDOR_EXT; i++) {
5083 if (wpa_s->conf->wps_vendor_ext[i] == NULL)
5084 continue;
5085 p2p_add_wps_vendor_extension(
5086 p2p, wpa_s->conf->wps_vendor_ext[i]);
5087 }
5088 }
5089
5090 if ((wpa_s->conf->changed_parameters & CFG_CHANGED_COUNTRY) &&
5091 wpa_s->conf->country[0] && wpa_s->conf->country[1]) {
5092 char country[3];
5093 country[0] = wpa_s->conf->country[0];
5094 country[1] = wpa_s->conf->country[1];
5095 country[2] = 0x04;
5096 p2p_set_country(p2p, country);
5097 }
5098
5099 if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_SSID_POSTFIX) {
5100 p2p_set_ssid_postfix(p2p, (u8 *) wpa_s->conf->p2p_ssid_postfix,
5101 wpa_s->conf->p2p_ssid_postfix ?
5102 os_strlen(wpa_s->conf->p2p_ssid_postfix) :
5103 0);
5104 }
5105
5106 if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_INTRA_BSS)
5107 p2p_set_intra_bss_dist(p2p, wpa_s->conf->p2p_intra_bss);
Jouni Malinen75ecf522011-06-27 15:19:46 -07005108
5109 if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_LISTEN_CHANNEL) {
5110 u8 reg_class, channel;
5111 int ret;
5112 unsigned int r;
5113 if (wpa_s->conf->p2p_listen_reg_class &&
5114 wpa_s->conf->p2p_listen_channel) {
5115 reg_class = wpa_s->conf->p2p_listen_reg_class;
5116 channel = wpa_s->conf->p2p_listen_channel;
5117 } else {
5118 reg_class = 81;
5119 /*
5120 * Pick one of the social channels randomly as the
5121 * listen channel.
5122 */
5123 os_get_random((u8 *) &r, sizeof(r));
5124 channel = 1 + (r % 3) * 5;
5125 }
5126 ret = p2p_set_listen_channel(p2p, reg_class, channel);
5127 if (ret)
5128 wpa_printf(MSG_ERROR, "P2P: Own listen channel update "
5129 "failed: %d", ret);
5130 }
5131 if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_OPER_CHANNEL) {
5132 u8 op_reg_class, op_channel, cfg_op_channel;
5133 int ret = 0;
5134 unsigned int r;
5135 if (wpa_s->conf->p2p_oper_reg_class &&
5136 wpa_s->conf->p2p_oper_channel) {
5137 op_reg_class = wpa_s->conf->p2p_oper_reg_class;
5138 op_channel = wpa_s->conf->p2p_oper_channel;
5139 cfg_op_channel = 1;
5140 } else {
5141 op_reg_class = 81;
5142 /*
5143 * Use random operation channel from (1, 6, 11)
5144 *if no other preference is indicated.
5145 */
5146 os_get_random((u8 *) &r, sizeof(r));
5147 op_channel = 1 + (r % 3) * 5;
5148 cfg_op_channel = 0;
5149 }
5150 ret = p2p_set_oper_channel(p2p, op_reg_class, op_channel,
5151 cfg_op_channel);
5152 if (ret)
5153 wpa_printf(MSG_ERROR, "P2P: Own oper channel update "
5154 "failed: %d", ret);
5155 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07005156
5157 if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_PREF_CHAN) {
5158 if (p2p_set_pref_chan(p2p, wpa_s->conf->num_p2p_pref_chan,
5159 wpa_s->conf->p2p_pref_chan) < 0) {
5160 wpa_printf(MSG_ERROR, "P2P: Preferred channel list "
5161 "update failed");
5162 }
5163 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005164}
5165
5166
5167int wpas_p2p_set_noa(struct wpa_supplicant *wpa_s, u8 count, int start,
5168 int duration)
5169{
5170 if (!wpa_s->ap_iface)
5171 return -1;
5172 return hostapd_p2p_set_noa(wpa_s->ap_iface->bss[0], count, start,
5173 duration);
5174}
5175
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005176
5177int wpas_p2p_set_cross_connect(struct wpa_supplicant *wpa_s, int enabled)
5178{
5179 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
5180 return -1;
5181 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
5182 return -1;
5183
5184 wpa_s->global->cross_connection = enabled;
5185 p2p_set_cross_connect(wpa_s->global->p2p, enabled);
5186
5187 if (!enabled) {
5188 struct wpa_supplicant *iface;
5189
5190 for (iface = wpa_s->global->ifaces; iface; iface = iface->next)
5191 {
5192 if (iface->cross_connect_enabled == 0)
5193 continue;
5194
5195 iface->cross_connect_enabled = 0;
5196 iface->cross_connect_in_use = 0;
5197 wpa_msg(iface->parent, MSG_INFO,
5198 P2P_EVENT_CROSS_CONNECT_DISABLE "%s %s",
5199 iface->ifname, iface->cross_connect_uplink);
5200 }
5201 }
5202
5203 return 0;
5204}
5205
5206
5207static void wpas_p2p_enable_cross_connect(struct wpa_supplicant *uplink)
5208{
5209 struct wpa_supplicant *iface;
5210
5211 if (!uplink->global->cross_connection)
5212 return;
5213
5214 for (iface = uplink->global->ifaces; iface; iface = iface->next) {
5215 if (!iface->cross_connect_enabled)
5216 continue;
5217 if (os_strcmp(uplink->ifname, iface->cross_connect_uplink) !=
5218 0)
5219 continue;
5220 if (iface->ap_iface == NULL)
5221 continue;
5222 if (iface->cross_connect_in_use)
5223 continue;
5224
5225 iface->cross_connect_in_use = 1;
5226 wpa_msg(iface->parent, MSG_INFO,
5227 P2P_EVENT_CROSS_CONNECT_ENABLE "%s %s",
5228 iface->ifname, iface->cross_connect_uplink);
5229 }
5230}
5231
5232
5233static void wpas_p2p_disable_cross_connect(struct wpa_supplicant *uplink)
5234{
5235 struct wpa_supplicant *iface;
5236
5237 for (iface = uplink->global->ifaces; iface; iface = iface->next) {
5238 if (!iface->cross_connect_enabled)
5239 continue;
5240 if (os_strcmp(uplink->ifname, iface->cross_connect_uplink) !=
5241 0)
5242 continue;
5243 if (!iface->cross_connect_in_use)
5244 continue;
5245
5246 wpa_msg(iface->parent, MSG_INFO,
5247 P2P_EVENT_CROSS_CONNECT_DISABLE "%s %s",
5248 iface->ifname, iface->cross_connect_uplink);
5249 iface->cross_connect_in_use = 0;
5250 }
5251}
5252
5253
5254void wpas_p2p_notif_connected(struct wpa_supplicant *wpa_s)
5255{
5256 if (wpa_s->ap_iface || wpa_s->current_ssid == NULL ||
5257 wpa_s->current_ssid->mode != WPAS_MODE_INFRA ||
5258 wpa_s->cross_connect_disallowed)
5259 wpas_p2p_disable_cross_connect(wpa_s);
5260 else
5261 wpas_p2p_enable_cross_connect(wpa_s);
Dmitry Shmidt04949592012-07-19 12:16:46 -07005262 if (!wpa_s->ap_iface &&
5263 eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL) > 0)
5264 wpa_printf(MSG_DEBUG, "P2P: Cancelled P2P group idle timeout");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005265}
5266
5267
5268void wpas_p2p_notif_disconnected(struct wpa_supplicant *wpa_s)
5269{
5270 wpas_p2p_disable_cross_connect(wpa_s);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005271 if (!wpa_s->ap_iface &&
5272 !eloop_is_timeout_registered(wpas_p2p_group_idle_timeout,
5273 wpa_s, NULL))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005274 wpas_p2p_set_group_idle_timeout(wpa_s);
5275}
5276
5277
5278static void wpas_p2p_cross_connect_setup(struct wpa_supplicant *wpa_s)
5279{
5280 struct wpa_supplicant *iface;
5281
5282 if (!wpa_s->global->cross_connection)
5283 return;
5284
5285 for (iface = wpa_s->global->ifaces; iface; iface = iface->next) {
5286 if (iface == wpa_s)
5287 continue;
5288 if (iface->drv_flags &
5289 WPA_DRIVER_FLAGS_P2P_DEDICATED_INTERFACE)
5290 continue;
5291 if (iface->drv_flags & WPA_DRIVER_FLAGS_P2P_CAPABLE)
5292 continue;
5293
5294 wpa_s->cross_connect_enabled = 1;
5295 os_strlcpy(wpa_s->cross_connect_uplink, iface->ifname,
5296 sizeof(wpa_s->cross_connect_uplink));
5297 wpa_printf(MSG_DEBUG, "P2P: Enable cross connection from "
5298 "%s to %s whenever uplink is available",
5299 wpa_s->ifname, wpa_s->cross_connect_uplink);
5300
5301 if (iface->ap_iface || iface->current_ssid == NULL ||
5302 iface->current_ssid->mode != WPAS_MODE_INFRA ||
5303 iface->cross_connect_disallowed ||
5304 iface->wpa_state != WPA_COMPLETED)
5305 break;
5306
5307 wpa_s->cross_connect_in_use = 1;
5308 wpa_msg(wpa_s->parent, MSG_INFO,
5309 P2P_EVENT_CROSS_CONNECT_ENABLE "%s %s",
5310 wpa_s->ifname, wpa_s->cross_connect_uplink);
5311 break;
5312 }
5313}
5314
5315
5316int wpas_p2p_notif_pbc_overlap(struct wpa_supplicant *wpa_s)
5317{
5318 if (wpa_s->p2p_group_interface != P2P_GROUP_INTERFACE_CLIENT &&
5319 !wpa_s->p2p_in_provisioning)
5320 return 0; /* not P2P client operation */
5321
5322 wpa_printf(MSG_DEBUG, "P2P: Terminate connection due to WPS PBC "
5323 "session overlap");
5324 if (wpa_s != wpa_s->parent)
5325 wpa_msg_ctrl(wpa_s->parent, MSG_INFO, WPS_EVENT_OVERLAP);
5326
5327 if (wpa_s->global->p2p)
5328 p2p_group_formation_failed(wpa_s->global->p2p);
5329
5330 eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
5331 wpa_s->parent, NULL);
5332
5333 wpas_group_formation_completed(wpa_s, 0);
5334 return 1;
5335}
5336
5337
5338void wpas_p2p_update_channel_list(struct wpa_supplicant *wpa_s)
5339{
5340 struct p2p_channels chan;
5341
5342 if (wpa_s->global == NULL || wpa_s->global->p2p == NULL)
5343 return;
5344
5345 os_memset(&chan, 0, sizeof(chan));
5346 if (wpas_p2p_setup_channels(wpa_s, &chan)) {
5347 wpa_printf(MSG_ERROR, "P2P: Failed to update supported "
5348 "channel list");
5349 return;
5350 }
5351
5352 p2p_update_channel_list(wpa_s->global->p2p, &chan);
5353}
5354
5355
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08005356static void wpas_p2p_scan_res_ignore(struct wpa_supplicant *wpa_s,
5357 struct wpa_scan_results *scan_res)
5358{
5359 wpa_printf(MSG_DEBUG, "P2P: Ignore scan results");
5360}
5361
5362
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005363int wpas_p2p_cancel(struct wpa_supplicant *wpa_s)
5364{
5365 struct wpa_global *global = wpa_s->global;
5366 int found = 0;
5367 const u8 *peer;
5368
5369 if (global->p2p == NULL)
5370 return -1;
5371
5372 wpa_printf(MSG_DEBUG, "P2P: Request to cancel group formation");
5373
5374 if (wpa_s->pending_interface_name[0] &&
5375 !is_zero_ether_addr(wpa_s->pending_interface_addr))
5376 found = 1;
5377
5378 peer = p2p_get_go_neg_peer(global->p2p);
5379 if (peer) {
5380 wpa_printf(MSG_DEBUG, "P2P: Unauthorize pending GO Neg peer "
5381 MACSTR, MAC2STR(peer));
5382 p2p_unauthorize(global->p2p, peer);
Dmitry Shmidt04949592012-07-19 12:16:46 -07005383 found = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005384 }
5385
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08005386 if (wpa_s->scan_res_handler == wpas_p2p_scan_res_join) {
5387 wpa_printf(MSG_DEBUG, "P2P: Stop pending scan for join");
5388 wpa_s->scan_res_handler = wpas_p2p_scan_res_ignore;
5389 found = 1;
5390 }
5391
5392 if (wpa_s->pending_pd_before_join) {
5393 wpa_printf(MSG_DEBUG, "P2P: Stop pending PD before join");
5394 wpa_s->pending_pd_before_join = 0;
5395 found = 1;
5396 }
5397
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005398 wpas_p2p_stop_find(wpa_s);
5399
5400 for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
5401 if (wpa_s == global->p2p_group_formation &&
5402 (wpa_s->p2p_in_provisioning ||
5403 wpa_s->parent->pending_interface_type ==
5404 WPA_IF_P2P_CLIENT)) {
5405 wpa_printf(MSG_DEBUG, "P2P: Interface %s in group "
5406 "formation found - cancelling",
5407 wpa_s->ifname);
5408 found = 1;
5409 eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
5410 wpa_s->parent, NULL);
Jouni Malinenadddfc42012-10-03 14:31:41 -07005411 if (wpa_s->p2p_in_provisioning) {
5412 wpas_group_formation_completed(wpa_s, 0);
5413 break;
5414 }
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005415 wpas_p2p_group_delete(wpa_s,
5416 P2P_GROUP_REMOVAL_REQUESTED);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005417 break;
5418 }
5419 }
5420
5421 if (!found) {
5422 wpa_printf(MSG_DEBUG, "P2P: No ongoing group formation found");
5423 return -1;
5424 }
5425
5426 return 0;
5427}
5428
5429
5430void wpas_p2p_interface_unavailable(struct wpa_supplicant *wpa_s)
5431{
5432 if (wpa_s->current_ssid == NULL || !wpa_s->current_ssid->p2p_group)
5433 return;
5434
5435 wpa_printf(MSG_DEBUG, "P2P: Remove group due to driver resource not "
5436 "being available anymore");
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005437 wpas_p2p_group_delete(wpa_s, P2P_GROUP_REMOVAL_UNAVAILABLE);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005438}
5439
5440
5441void wpas_p2p_update_best_channels(struct wpa_supplicant *wpa_s,
5442 int freq_24, int freq_5, int freq_overall)
5443{
5444 struct p2p_data *p2p = wpa_s->global->p2p;
5445 if (p2p == NULL || (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT))
5446 return;
5447 p2p_set_best_channels(p2p, freq_24, freq_5, freq_overall);
5448}
5449
5450
5451int wpas_p2p_unauthorize(struct wpa_supplicant *wpa_s, const char *addr)
5452{
5453 u8 peer[ETH_ALEN];
5454 struct p2p_data *p2p = wpa_s->global->p2p;
5455
5456 if (p2p == NULL || (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT))
5457 return -1;
5458
5459 if (hwaddr_aton(addr, peer))
5460 return -1;
5461
5462 return p2p_unauthorize(p2p, peer);
5463}
5464
5465
5466/**
5467 * wpas_p2p_disconnect - Disconnect from a P2P Group
5468 * @wpa_s: Pointer to wpa_supplicant data
5469 * Returns: 0 on success, -1 on failure
5470 *
5471 * This can be used to disconnect from a group in which the local end is a P2P
5472 * Client or to end a P2P Group in case the local end is the Group Owner. If a
5473 * virtual network interface was created for this group, that interface will be
5474 * removed. Otherwise, only the configured P2P group network will be removed
5475 * from the interface.
5476 */
5477int wpas_p2p_disconnect(struct wpa_supplicant *wpa_s)
5478{
5479
5480 if (wpa_s == NULL)
5481 return -1;
5482
Jouni Malinen2b89da82012-08-31 22:04:41 +03005483 return wpas_p2p_group_delete(wpa_s, P2P_GROUP_REMOVAL_REQUESTED) < 0 ?
5484 -1 : 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005485}
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005486
5487
5488int wpas_p2p_in_progress(struct wpa_supplicant *wpa_s)
5489{
5490 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
5491 return 0;
5492
5493 return p2p_in_progress(wpa_s->global->p2p);
5494}
5495
5496
5497void wpas_p2p_network_removed(struct wpa_supplicant *wpa_s,
5498 struct wpa_ssid *ssid)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005499{
5500 if (wpa_s->p2p_in_provisioning && ssid->p2p_group &&
5501 eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
5502 wpa_s->parent, NULL) > 0) {
Dmitry Shmidt04949592012-07-19 12:16:46 -07005503 /**
5504 * Remove the network by scheduling the group formation
5505 * timeout to happen immediately. The teardown code
5506 * needs to be scheduled to run asynch later so that we
5507 * don't delete data from under ourselves unexpectedly.
5508 * Calling wpas_p2p_group_formation_timeout directly
5509 * causes a series of crashes in WPS failure scenarios.
5510 */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005511 wpa_printf(MSG_DEBUG, "P2P: Canceled group formation due to "
5512 "P2P group network getting removed");
Dmitry Shmidt04949592012-07-19 12:16:46 -07005513 eloop_register_timeout(0, 0, wpas_p2p_group_formation_timeout,
5514 wpa_s->parent, NULL);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005515 }
5516}
5517
5518
5519struct wpa_ssid * wpas_p2p_get_persistent(struct wpa_supplicant *wpa_s,
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08005520 const u8 *addr, const u8 *ssid,
5521 size_t ssid_len)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005522{
5523 struct wpa_ssid *s;
5524 size_t i;
5525
5526 for (s = wpa_s->conf->ssid; s; s = s->next) {
5527 if (s->disabled != 2)
5528 continue;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08005529 if (ssid &&
5530 (ssid_len != s->ssid_len ||
5531 os_memcmp(ssid, s->ssid, ssid_len) != 0))
5532 continue;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005533 if (os_memcmp(s->bssid, addr, ETH_ALEN) == 0)
5534 return s; /* peer is GO in the persistent group */
5535 if (s->mode != WPAS_MODE_P2P_GO || s->p2p_client_list == NULL)
5536 continue;
5537 for (i = 0; i < s->num_p2p_clients; i++) {
5538 if (os_memcmp(s->p2p_client_list + i * ETH_ALEN,
5539 addr, ETH_ALEN) == 0)
5540 return s; /* peer is P2P client in persistent
5541 * group */
5542 }
5543 }
5544
5545 return NULL;
5546}
5547
5548
5549void wpas_p2p_notify_ap_sta_authorized(struct wpa_supplicant *wpa_s,
5550 const u8 *addr)
5551{
5552 if (addr == NULL)
5553 return;
5554 wpas_p2p_add_persistent_group_client(wpa_s, addr);
5555}
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08005556
Dmitry Shmidt04949592012-07-19 12:16:46 -07005557
5558static void wpas_p2p_fallback_to_go_neg(struct wpa_supplicant *wpa_s,
5559 int group_added)
5560{
5561 struct wpa_supplicant *group = wpa_s;
Dmitry Shmidt04949592012-07-19 12:16:46 -07005562 if (wpa_s->global->p2p_group_formation)
5563 group = wpa_s->global->p2p_group_formation;
5564 wpa_s = wpa_s->parent;
5565 offchannel_send_action_done(wpa_s);
5566 if (group_added)
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005567 wpas_p2p_group_delete(group, P2P_GROUP_REMOVAL_SILENT);
Dmitry Shmidt04949592012-07-19 12:16:46 -07005568 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Fall back to GO Negotiation");
5569 wpas_p2p_connect(wpa_s, wpa_s->pending_join_dev_addr, wpa_s->p2p_pin,
5570 wpa_s->p2p_wps_method, wpa_s->p2p_persistent_group, 0,
5571 0, 0, wpa_s->p2p_go_intent, wpa_s->p2p_connect_freq,
5572 wpa_s->p2p_persistent_id,
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005573 wpa_s->p2p_pd_before_go_neg,
5574 wpa_s->p2p_go_ht40);
Dmitry Shmidt04949592012-07-19 12:16:46 -07005575}
5576
5577
5578int wpas_p2p_scan_no_go_seen(struct wpa_supplicant *wpa_s)
5579{
5580 if (!wpa_s->p2p_fallback_to_go_neg ||
5581 wpa_s->p2p_in_provisioning <= 5)
5582 return 0;
5583
5584 if (wpas_p2p_peer_go(wpa_s, wpa_s->pending_join_dev_addr) > 0)
5585 return 0; /* peer operating as a GO */
5586
5587 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: GO not found for p2p_connect-auto - "
5588 "fallback to GO Negotiation");
5589 wpas_p2p_fallback_to_go_neg(wpa_s, 1);
5590
5591 return 1;
5592}
5593
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005594
5595unsigned int wpas_p2p_search_delay(struct wpa_supplicant *wpa_s)
5596{
5597 const char *rn, *rn2;
5598 struct wpa_supplicant *ifs;
5599
5600 if (wpa_s->wpa_state > WPA_SCANNING) {
5601 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Use %u ms search delay due to "
5602 "concurrent operation",
5603 P2P_CONCURRENT_SEARCH_DELAY);
5604 return P2P_CONCURRENT_SEARCH_DELAY;
5605 }
5606
5607 if (!wpa_s->driver->get_radio_name)
5608 return 0;
5609 rn = wpa_s->driver->get_radio_name(wpa_s->drv_priv);
5610 if (rn == NULL || rn[0] == '\0')
5611 return 0;
5612
5613 for (ifs = wpa_s->global->ifaces; ifs; ifs = ifs->next) {
5614 if (ifs == wpa_s || !ifs->driver->get_radio_name)
5615 continue;
5616
5617 rn2 = ifs->driver->get_radio_name(ifs->drv_priv);
5618 if (!rn2 || os_strcmp(rn, rn2) != 0)
5619 continue;
5620 if (ifs->wpa_state > WPA_SCANNING) {
5621 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Use %u ms search "
5622 "delay due to concurrent operation on "
5623 "interface %s",
5624 P2P_CONCURRENT_SEARCH_DELAY, ifs->ifname);
5625 return P2P_CONCURRENT_SEARCH_DELAY;
5626 }
5627 }
5628
5629 return 0;
5630}
5631
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08005632#ifdef ANDROID_P2P
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08005633int wpas_p2p_handle_frequency_conflicts(struct wpa_supplicant *wpa_s, int freq,
5634 struct wpa_ssid *ssid)
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08005635{
5636 struct wpa_supplicant *iface = NULL;
5637 struct p2p_data *p2p = wpa_s->global->p2p;
5638
5639 for (iface = wpa_s->global->ifaces; iface; iface = iface->next) {
Jeff Johnson12b1cd92012-10-07 19:34:24 -07005640 if ((iface->current_ssid) &&
5641 (iface->current_ssid->frequency != freq) &&
5642 ((iface->p2p_group_interface) ||
5643 (iface->current_ssid->p2p_group))) {
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08005644
Jeff Johnson12b1cd92012-10-07 19:34:24 -07005645 if ((iface->p2p_group_interface == P2P_GROUP_INTERFACE_GO) ||
5646 (iface->current_ssid->mode == WPAS_MODE_P2P_GO)) {
5647 /* Try to see whether we can move the GO. If it
5648 * is not possible, remove the GO interface
5649 */
5650 if (wpa_drv_switch_channel(iface, freq) == 0) {
5651 wpa_printf(MSG_ERROR, "P2P: GO Moved to freq(%d)", freq);
5652 iface->current_ssid->frequency = freq;
5653 continue;
5654 }
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08005655 }
5656
5657 /* If GO cannot be moved or if the conflicting interface is a
5658 * P2P Client, remove the interface depending up on the connection
5659 * priority */
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08005660 if(!wpas_is_p2p_prioritized(iface)) {
Dmitry Shmidtf4f5db32012-09-11 14:36:56 -07005661 /* STA connection has priority over existing
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08005662 * P2P connection. So remove the interface */
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08005663 wpa_printf(MSG_DEBUG, "P2P: Removing P2P connection due to Single channel"
Dmitry Shmidt687922c2012-03-26 14:02:32 -07005664 "concurrent mode frequency conflict");
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005665 wpas_p2p_group_delete(iface, P2P_GROUP_REMOVAL_FREQ_CONFLICT);
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08005666 /* If connection in progress is p2p connection, do not proceed for the connection */
5667 if (wpa_s == iface)
5668 return -1;
5669 else
5670 /* If connection in progress is STA connection, proceed for the connection */
5671 return 0;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08005672 } else {
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08005673 /* P2p connection has priority, disable the STA network*/
5674 wpa_supplicant_disable_network(wpa_s->global->ifaces, ssid);
5675 wpa_msg(wpa_s->global->ifaces, MSG_INFO, WPA_EVENT_FREQ_CONFLICT
5676 " id=%d", ssid->id);
5677 os_memset(wpa_s->global->ifaces->pending_bssid, 0, ETH_ALEN);
5678 if (wpa_s == iface) {
5679 /* p2p connection is in progress, continue connecting...*/
5680 return 0;
5681 }
5682 else {
5683 /* STA connection is in progress, do not allow to continue */
5684 return -1;
5685 }
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08005686 }
5687 }
5688 }
5689 return 0;
5690}
5691#endif