blob: cf8f9355aa1a3964d38c679d4b2413a6bf05c8fc [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 Shmidtc5ec7f52012-03-06 16:33:24 -080092static void wpas_p2p_pd_before_join_timeout(void *eloop_ctx,
93 void *timeout_ctx);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070094static int wpas_p2p_create_iface(struct wpa_supplicant *wpa_s);
95static void wpas_p2p_cross_connect_setup(struct wpa_supplicant *wpa_s);
96static void wpas_p2p_group_idle_timeout(void *eloop_ctx, void *timeout_ctx);
97static void wpas_p2p_set_group_idle_timeout(struct wpa_supplicant *wpa_s);
Dmitry Shmidt04949592012-07-19 12:16:46 -070098static void wpas_p2p_fallback_to_go_neg(struct wpa_supplicant *wpa_s,
99 int group_added);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700100
101
102static void wpas_p2p_scan_res_handler(struct wpa_supplicant *wpa_s,
103 struct wpa_scan_results *scan_res)
104{
105 size_t i;
106
107 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
108 return;
109
110 wpa_printf(MSG_DEBUG, "P2P: Scan results received (%d BSS)",
111 (int) scan_res->num);
112
113 for (i = 0; i < scan_res->num; i++) {
114 struct wpa_scan_res *bss = scan_res->res[i];
115 if (p2p_scan_res_handler(wpa_s->global->p2p, bss->bssid,
116 bss->freq, bss->level,
117 (const u8 *) (bss + 1),
118 bss->ie_len) > 0)
119 break;
120 }
121
122 p2p_scan_res_handled(wpa_s->global->p2p);
123}
124
125
126static int wpas_p2p_scan(void *ctx, enum p2p_scan_type type, int freq,
127 unsigned int num_req_dev_types,
Dmitry Shmidt04949592012-07-19 12:16:46 -0700128 const u8 *req_dev_types, const u8 *dev_id, u16 pw_id)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700129{
130 struct wpa_supplicant *wpa_s = ctx;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700131 struct wpa_supplicant *ifs;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700132 struct wpa_driver_scan_params params;
133 int ret;
134 struct wpabuf *wps_ie, *ies;
135 int social_channels[] = { 2412, 2437, 2462, 0, 0 };
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800136 size_t ielen;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700137
138 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
139 return -1;
140
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700141 for (ifs = wpa_s->global->ifaces; ifs; ifs = ifs->next) {
142 if (ifs->sta_scan_pending &&
143 wpas_p2p_in_progress(wpa_s) == 2) {
144 wpa_printf(MSG_DEBUG, "Delaying P2P scan to allow "
145 "pending station mode scan to be "
146 "completed on interface %s", ifs->ifname);
Jouni Malinendc7b7132012-09-14 12:53:47 -0700147 wpa_s->global->p2p_cb_on_scan_complete = 1;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700148 wpa_supplicant_req_scan(ifs, 0, 0);
149 return 1;
150 }
151 }
152
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700153 os_memset(&params, 0, sizeof(params));
154
155 /* P2P Wildcard SSID */
156 params.num_ssids = 1;
157 params.ssids[0].ssid = (u8 *) P2P_WILDCARD_SSID;
158 params.ssids[0].ssid_len = P2P_WILDCARD_SSID_LEN;
159
160 wpa_s->wps->dev.p2p = 1;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700161 wps_ie = wps_build_probe_req_ie(pw_id, &wpa_s->wps->dev,
162 wpa_s->wps->uuid, WPS_REQ_ENROLLEE,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700163 num_req_dev_types, req_dev_types);
164 if (wps_ie == NULL)
165 return -1;
166
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800167 ielen = p2p_scan_ie_buf_len(wpa_s->global->p2p);
168 ies = wpabuf_alloc(wpabuf_len(wps_ie) + ielen);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700169 if (ies == NULL) {
170 wpabuf_free(wps_ie);
171 return -1;
172 }
173 wpabuf_put_buf(ies, wps_ie);
174 wpabuf_free(wps_ie);
175
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -0800176 p2p_scan_ie(wpa_s->global->p2p, ies, dev_id);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700177
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800178 params.p2p_probe = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700179 params.extra_ies = wpabuf_head(ies);
180 params.extra_ies_len = wpabuf_len(ies);
181
182 switch (type) {
183 case P2P_SCAN_SOCIAL:
184 params.freqs = social_channels;
185 break;
186 case P2P_SCAN_FULL:
187 break;
Dmitry Shmidtb5e8f062012-08-08 10:56:33 -0700188 case P2P_SCAN_SPECIFIC:
189 social_channels[0] = freq;
190 social_channels[1] = 0;
191 params.freqs = social_channels;
192 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700193 case P2P_SCAN_SOCIAL_PLUS_ONE:
194 social_channels[3] = freq;
195 params.freqs = social_channels;
196 break;
197 }
198
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800199 ret = wpa_drv_scan(wpa_s, &params);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700200
201 wpabuf_free(ies);
202
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800203 if (ret) {
Jouni Malinen043a5a92012-09-13 18:03:14 -0700204 for (ifs = wpa_s->global->ifaces; ifs; ifs = ifs->next) {
205 if (ifs->scanning ||
206 ifs->scan_res_handler == wpas_p2p_scan_res_handler) {
207 wpa_s->global->p2p_cb_on_scan_complete = 1;
208 ret = 1;
209 break;
210 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800211 }
Dmitry Shmidt04949592012-07-19 12:16:46 -0700212 } else
213 wpa_s->scan_res_handler = wpas_p2p_scan_res_handler;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800214
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700215 return ret;
216}
217
218
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700219static enum wpa_driver_if_type wpas_p2p_if_type(int p2p_group_interface)
220{
221 switch (p2p_group_interface) {
222 case P2P_GROUP_INTERFACE_PENDING:
223 return WPA_IF_P2P_GROUP;
224 case P2P_GROUP_INTERFACE_GO:
225 return WPA_IF_P2P_GO;
226 case P2P_GROUP_INTERFACE_CLIENT:
227 return WPA_IF_P2P_CLIENT;
228 }
229
230 return WPA_IF_P2P_GROUP;
231}
232
233
234static struct wpa_supplicant * wpas_get_p2p_group(struct wpa_supplicant *wpa_s,
235 const u8 *ssid,
236 size_t ssid_len, int *go)
237{
238 struct wpa_ssid *s;
239
240 for (wpa_s = wpa_s->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
241 for (s = wpa_s->conf->ssid; s; s = s->next) {
242 if (s->disabled != 0 || !s->p2p_group ||
243 s->ssid_len != ssid_len ||
244 os_memcmp(ssid, s->ssid, ssid_len) != 0)
245 continue;
246 if (s->mode == WPAS_MODE_P2P_GO &&
247 s != wpa_s->current_ssid)
248 continue;
249 if (go)
250 *go = s->mode == WPAS_MODE_P2P_GO;
251 return wpa_s;
252 }
253 }
254
255 return NULL;
256}
257
258
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700259static int wpas_p2p_group_delete(struct wpa_supplicant *wpa_s,
260 enum p2p_group_removal_reason removal_reason)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700261{
262 struct wpa_ssid *ssid;
263 char *gtype;
264 const char *reason;
265
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700266 ssid = wpa_s->current_ssid;
267 if (ssid == NULL) {
268 /*
269 * The current SSID was not known, but there may still be a
Dmitry Shmidtaa532512012-09-24 10:35:31 -0700270 * pending P2P group interface waiting for provisioning or a
271 * P2P group that is trying to reconnect.
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700272 */
273 ssid = wpa_s->conf->ssid;
274 while (ssid) {
Dmitry Shmidtaa532512012-09-24 10:35:31 -0700275 if (ssid->p2p_group)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700276 break;
277 ssid = ssid->next;
278 }
Jouni Malinen5c44edb2012-08-31 21:35:32 +0300279 if (ssid == NULL &&
280 wpa_s->p2p_group_interface == NOT_P2P_GROUP_INTERFACE)
281 {
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700282 wpa_printf(MSG_ERROR, "P2P: P2P group interface "
283 "not found");
284 return -1;
285 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700286 }
287 if (wpa_s->p2p_group_interface == P2P_GROUP_INTERFACE_GO)
288 gtype = "GO";
289 else if (wpa_s->p2p_group_interface == P2P_GROUP_INTERFACE_CLIENT ||
290 (ssid && ssid->mode == WPAS_MODE_INFRA)) {
291 wpa_s->reassociate = 0;
292 wpa_s->disconnected = 1;
293 wpa_supplicant_deauthenticate(wpa_s,
294 WLAN_REASON_DEAUTH_LEAVING);
295 gtype = "client";
296 } else
297 gtype = "GO";
298 if (wpa_s->cross_connect_in_use) {
299 wpa_s->cross_connect_in_use = 0;
300 wpa_msg(wpa_s->parent, MSG_INFO,
301 P2P_EVENT_CROSS_CONNECT_DISABLE "%s %s",
302 wpa_s->ifname, wpa_s->cross_connect_uplink);
303 }
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700304 switch (removal_reason) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700305 case P2P_GROUP_REMOVAL_REQUESTED:
306 reason = " reason=REQUESTED";
307 break;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700308 case P2P_GROUP_REMOVAL_FORMATION_FAILED:
309 reason = " reason=FORMATION_FAILED";
310 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700311 case P2P_GROUP_REMOVAL_IDLE_TIMEOUT:
312 reason = " reason=IDLE";
313 break;
314 case P2P_GROUP_REMOVAL_UNAVAILABLE:
315 reason = " reason=UNAVAILABLE";
316 break;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700317 case P2P_GROUP_REMOVAL_GO_ENDING_SESSION:
318 reason = " reason=GO_ENDING_SESSION";
319 break;
Dmitry Shmidt687922c2012-03-26 14:02:32 -0700320#ifdef ANDROID_P2P
321 case P2P_GROUP_REMOVAL_FREQ_CONFLICT:
322 reason = " reason=FREQ_CONFLICT";
323 break;
324#endif
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700325 default:
326 reason = "";
327 break;
328 }
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700329 if (removal_reason != P2P_GROUP_REMOVAL_SILENT) {
Dmitry Shmidt04949592012-07-19 12:16:46 -0700330 wpa_msg(wpa_s->parent, MSG_INFO,
331 P2P_EVENT_GROUP_REMOVED "%s %s%s",
332 wpa_s->ifname, gtype, reason);
333 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700334
Dmitry Shmidt04949592012-07-19 12:16:46 -0700335 if (eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL) > 0)
336 wpa_printf(MSG_DEBUG, "P2P: Cancelled P2P group idle timeout");
337
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700338 if (removal_reason != P2P_GROUP_REMOVAL_SILENT && ssid)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700339 wpas_notify_p2p_group_removed(wpa_s, ssid, gtype);
340
341 if (wpa_s->p2p_group_interface != NOT_P2P_GROUP_INTERFACE) {
342 struct wpa_global *global;
343 char *ifname;
344 enum wpa_driver_if_type type;
345 wpa_printf(MSG_DEBUG, "P2P: Remove group interface %s",
346 wpa_s->ifname);
347 global = wpa_s->global;
348 ifname = os_strdup(wpa_s->ifname);
349 type = wpas_p2p_if_type(wpa_s->p2p_group_interface);
Dmitry Shmidte15c7b52011-08-03 15:04:35 -0700350 wpa_supplicant_remove_iface(wpa_s->global, wpa_s, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700351 wpa_s = global->ifaces;
352 if (wpa_s && ifname)
353 wpa_drv_if_remove(wpa_s, type, ifname);
354 os_free(ifname);
Jouni Malinen2b89da82012-08-31 22:04:41 +0300355 return 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700356 }
357
358 wpa_printf(MSG_DEBUG, "P2P: Remove temporary group network");
359 if (ssid && (ssid->p2p_group ||
360 ssid->mode == WPAS_MODE_P2P_GROUP_FORMATION ||
361 (ssid->key_mgmt & WPA_KEY_MGMT_WPS))) {
362 int id = ssid->id;
Jouni Malinen75ecf522011-06-27 15:19:46 -0700363 if (ssid == wpa_s->current_ssid) {
364 wpa_sm_set_config(wpa_s->wpa, NULL);
365 eapol_sm_notify_config(wpa_s->eapol, NULL, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700366 wpa_s->current_ssid = NULL;
Jouni Malinen75ecf522011-06-27 15:19:46 -0700367 }
368 /*
369 * Networks objects created during any P2P activities are not
370 * exposed out as they might/will confuse certain non-P2P aware
371 * applications since these network objects won't behave like
372 * regular ones.
373 *
374 * Likewise, we don't send out network removed signals for such
375 * network objects.
376 */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700377 wpa_config_remove_network(wpa_s->conf, id);
378 wpa_supplicant_clear_status(wpa_s);
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -0800379 wpa_supplicant_cancel_sched_scan(wpa_s);
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 ||
748 os_memcmp(dst, wpa_s->pending_join_iface_addr, ETH_ALEN) == 0)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700749 wpa_s->pending_pd_before_join = 0;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700750 if (wpa_s->p2p_fallback_to_go_neg) {
751 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;
755 }
756
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700757 wpa_printf(MSG_DEBUG, "P2P: Starting pending "
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -0800758 "join-existing-group operation (no ACK for PD "
759 "Req)");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700760 wpas_p2p_join_start(wpa_s);
761 }
762}
763
764
765static int wpas_send_action(void *ctx, unsigned int freq, const u8 *dst,
766 const u8 *src, const u8 *bssid, const u8 *buf,
767 size_t len, unsigned int wait_time)
768{
769 struct wpa_supplicant *wpa_s = ctx;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800770 return offchannel_send_action(wpa_s, freq, dst, src, bssid, buf, len,
771 wait_time,
772 wpas_p2p_send_action_tx_status, 1);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700773}
774
775
776static void wpas_send_action_done(void *ctx)
777{
778 struct wpa_supplicant *wpa_s = ctx;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800779 offchannel_send_action_done(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700780}
781
782
783static int wpas_copy_go_neg_results(struct wpa_supplicant *wpa_s,
784 struct p2p_go_neg_results *params)
785{
786 if (wpa_s->go_params == NULL) {
787 wpa_s->go_params = os_malloc(sizeof(*params));
788 if (wpa_s->go_params == NULL)
789 return -1;
790 }
791 os_memcpy(wpa_s->go_params, params, sizeof(*params));
792 return 0;
793}
794
795
796static void wpas_start_wps_enrollee(struct wpa_supplicant *wpa_s,
797 struct p2p_go_neg_results *res)
798{
799 wpa_printf(MSG_DEBUG, "P2P: Start WPS Enrollee for peer " MACSTR,
800 MAC2STR(res->peer_interface_addr));
801 wpa_hexdump_ascii(MSG_DEBUG, "P2P: Start WPS Enrollee for SSID",
802 res->ssid, res->ssid_len);
803 wpa_supplicant_ap_deinit(wpa_s);
804 wpas_copy_go_neg_results(wpa_s, res);
805 if (res->wps_method == WPS_PBC)
806 wpas_wps_start_pbc(wpa_s, res->peer_interface_addr, 1);
807 else {
808 u16 dev_pw_id = DEV_PW_DEFAULT;
809 if (wpa_s->p2p_wps_method == WPS_PIN_KEYPAD)
810 dev_pw_id = DEV_PW_REGISTRAR_SPECIFIED;
811 wpas_wps_start_pin(wpa_s, res->peer_interface_addr,
812 wpa_s->p2p_pin, 1, dev_pw_id);
813 }
814}
815
816
817static void p2p_go_configured(void *ctx, void *data)
818{
819 struct wpa_supplicant *wpa_s = ctx;
820 struct p2p_go_neg_results *params = data;
821 struct wpa_ssid *ssid;
Jouni Malinen75ecf522011-06-27 15:19:46 -0700822 int network_id = -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700823
824 ssid = wpa_s->current_ssid;
825 if (ssid && ssid->mode == WPAS_MODE_P2P_GO) {
826 wpa_printf(MSG_DEBUG, "P2P: Group setup without provisioning");
827 if (wpa_s->global->p2p_group_formation == wpa_s)
828 wpa_s->global->p2p_group_formation = NULL;
829 wpa_msg(wpa_s->parent, MSG_INFO, P2P_EVENT_GROUP_STARTED
830 "%s GO ssid=\"%s\" freq=%d passphrase=\"%s\" "
831 "go_dev_addr=" MACSTR "%s",
832 wpa_s->ifname,
833 wpa_ssid_txt(ssid->ssid, ssid->ssid_len),
834 ssid->frequency,
835 params->passphrase ? params->passphrase : "",
Dmitry Shmidt497c1d52011-07-21 15:19:46 -0700836 MAC2STR(wpa_s->global->p2p_dev_addr),
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700837 params->persistent_group ? " [PERSISTENT]" : "");
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800838
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700839 if (params->persistent_group)
Jouni Malinen75ecf522011-06-27 15:19:46 -0700840 network_id = wpas_p2p_store_persistent_group(
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700841 wpa_s->parent, ssid,
Dmitry Shmidt497c1d52011-07-21 15:19:46 -0700842 wpa_s->global->p2p_dev_addr);
Jouni Malinen75ecf522011-06-27 15:19:46 -0700843 if (network_id < 0)
844 network_id = ssid->id;
845 wpas_notify_p2p_group_started(wpa_s, ssid, network_id, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700846 wpas_p2p_cross_connect_setup(wpa_s);
847 wpas_p2p_set_group_idle_timeout(wpa_s);
848 return;
849 }
850
851 wpa_printf(MSG_DEBUG, "P2P: Setting up WPS for GO provisioning");
852 if (wpa_supplicant_ap_mac_addr_filter(wpa_s,
853 params->peer_interface_addr)) {
854 wpa_printf(MSG_DEBUG, "P2P: Failed to setup MAC address "
855 "filtering");
856 return;
857 }
858 if (params->wps_method == WPS_PBC)
859 wpa_supplicant_ap_wps_pbc(wpa_s, params->peer_interface_addr,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800860 params->peer_device_addr);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700861 else if (wpa_s->p2p_pin[0])
862 wpa_supplicant_ap_wps_pin(wpa_s, params->peer_interface_addr,
863 wpa_s->p2p_pin, NULL, 0);
864 os_free(wpa_s->go_params);
865 wpa_s->go_params = NULL;
866}
867
868
869static void wpas_start_wps_go(struct wpa_supplicant *wpa_s,
870 struct p2p_go_neg_results *params,
871 int group_formation)
872{
873 struct wpa_ssid *ssid;
874
Dmitry Shmidtaa532512012-09-24 10:35:31 -0700875 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Starting GO");
876 if (wpas_copy_go_neg_results(wpa_s, params) < 0) {
877 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Could not copy GO Negotiation "
878 "results");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700879 return;
Dmitry Shmidtaa532512012-09-24 10:35:31 -0700880 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700881
882 ssid = wpa_config_add_network(wpa_s->conf);
Dmitry Shmidtaa532512012-09-24 10:35:31 -0700883 if (ssid == NULL) {
884 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Could not add network for GO");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700885 return;
Dmitry Shmidtaa532512012-09-24 10:35:31 -0700886 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700887
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800888 wpa_s->show_group_started = 0;
889
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700890 wpa_config_set_network_defaults(ssid);
891 ssid->temporary = 1;
892 ssid->p2p_group = 1;
893 ssid->p2p_persistent_group = params->persistent_group;
894 ssid->mode = group_formation ? WPAS_MODE_P2P_GROUP_FORMATION :
895 WPAS_MODE_P2P_GO;
896 ssid->frequency = params->freq;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700897 ssid->ht40 = params->ht40;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700898 ssid->ssid = os_zalloc(params->ssid_len + 1);
899 if (ssid->ssid) {
900 os_memcpy(ssid->ssid, params->ssid, params->ssid_len);
901 ssid->ssid_len = params->ssid_len;
902 }
903 ssid->auth_alg = WPA_AUTH_ALG_OPEN;
904 ssid->key_mgmt = WPA_KEY_MGMT_PSK;
905 ssid->proto = WPA_PROTO_RSN;
906 ssid->pairwise_cipher = WPA_CIPHER_CCMP;
907 ssid->passphrase = os_strdup(params->passphrase);
Dmitry Shmidtaa532512012-09-24 10:35:31 -0700908 if (ssid->passphrase == NULL) {
909 wpa_msg(wpa_s, MSG_ERROR, "P2P: Failed to copy passphrase for "
910 "GO");
911 wpa_config_remove_network(wpa_s->conf, ssid->id);
912 return;
913 }
914 wpa_config_update_psk(ssid);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700915 ssid->ap_max_inactivity = wpa_s->parent->conf->p2p_go_max_inactivity;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700916
917 wpa_s->ap_configured_cb = p2p_go_configured;
918 wpa_s->ap_configured_cb_ctx = wpa_s;
919 wpa_s->ap_configured_cb_data = wpa_s->go_params;
Jouni Malinen75ecf522011-06-27 15:19:46 -0700920 wpa_s->connect_without_scan = ssid;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700921 wpa_s->reassociate = 1;
922 wpa_s->disconnected = 0;
Dmitry Shmidtaa532512012-09-24 10:35:31 -0700923 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Request scan (that will be skipped) to "
924 "start GO)");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700925 wpa_supplicant_req_scan(wpa_s, 0, 0);
926}
927
928
929static void wpas_p2p_clone_config(struct wpa_supplicant *dst,
930 const struct wpa_supplicant *src)
931{
932 struct wpa_config *d;
933 const struct wpa_config *s;
934
935 d = dst->conf;
936 s = src->conf;
937
938#define C(n) if (s->n) d->n = os_strdup(s->n)
939 C(device_name);
940 C(manufacturer);
941 C(model_name);
942 C(model_number);
943 C(serial_number);
944 C(config_methods);
945#undef C
946
947 os_memcpy(d->device_type, s->device_type, WPS_DEV_TYPE_LEN);
948 os_memcpy(d->sec_device_type, s->sec_device_type,
949 sizeof(d->sec_device_type));
950 d->num_sec_device_types = s->num_sec_device_types;
951
952 d->p2p_group_idle = s->p2p_group_idle;
953 d->p2p_intra_bss = s->p2p_intra_bss;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800954 d->persistent_reconnect = s->persistent_reconnect;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700955 d->max_num_sta = s->max_num_sta;
956 d->pbc_in_m1 = s->pbc_in_m1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700957}
958
959
960static int wpas_p2p_add_group_interface(struct wpa_supplicant *wpa_s,
961 enum wpa_driver_if_type type)
962{
963 char ifname[120], force_ifname[120];
964
965 if (wpa_s->pending_interface_name[0]) {
966 wpa_printf(MSG_DEBUG, "P2P: Pending virtual interface exists "
967 "- skip creation of a new one");
968 if (is_zero_ether_addr(wpa_s->pending_interface_addr)) {
969 wpa_printf(MSG_DEBUG, "P2P: Pending virtual address "
970 "unknown?! ifname='%s'",
971 wpa_s->pending_interface_name);
972 return -1;
973 }
974 return 0;
975 }
976
977 os_snprintf(ifname, sizeof(ifname), "p2p-%s-%d", wpa_s->ifname,
978 wpa_s->p2p_group_idx);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800979 if (os_strlen(ifname) >= IFNAMSIZ &&
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700980 os_strlen(wpa_s->ifname) < IFNAMSIZ) {
981 /* Try to avoid going over the IFNAMSIZ length limit */
982 os_snprintf(ifname, sizeof(ifname), "p2p-%d",
983 wpa_s->p2p_group_idx);
984 }
985 force_ifname[0] = '\0';
986
987 wpa_printf(MSG_DEBUG, "P2P: Create a new interface %s for the group",
988 ifname);
989 wpa_s->p2p_group_idx++;
990
991 wpa_s->pending_interface_type = type;
992 if (wpa_drv_if_add(wpa_s, type, ifname, NULL, NULL, force_ifname,
993 wpa_s->pending_interface_addr, NULL) < 0) {
994 wpa_printf(MSG_ERROR, "P2P: Failed to create new group "
995 "interface");
996 return -1;
997 }
998
999 if (force_ifname[0]) {
1000 wpa_printf(MSG_DEBUG, "P2P: Driver forced interface name %s",
1001 force_ifname);
1002 os_strlcpy(wpa_s->pending_interface_name, force_ifname,
1003 sizeof(wpa_s->pending_interface_name));
1004 } else
1005 os_strlcpy(wpa_s->pending_interface_name, ifname,
1006 sizeof(wpa_s->pending_interface_name));
1007 wpa_printf(MSG_DEBUG, "P2P: Created pending virtual interface %s addr "
1008 MACSTR, wpa_s->pending_interface_name,
1009 MAC2STR(wpa_s->pending_interface_addr));
1010
1011 return 0;
1012}
1013
1014
1015static void wpas_p2p_remove_pending_group_interface(
1016 struct wpa_supplicant *wpa_s)
1017{
1018 if (!wpa_s->pending_interface_name[0] ||
1019 is_zero_ether_addr(wpa_s->pending_interface_addr))
1020 return; /* No pending virtual interface */
1021
1022 wpa_printf(MSG_DEBUG, "P2P: Removing pending group interface %s",
1023 wpa_s->pending_interface_name);
1024 wpa_drv_if_remove(wpa_s, wpa_s->pending_interface_type,
1025 wpa_s->pending_interface_name);
1026 os_memset(wpa_s->pending_interface_addr, 0, ETH_ALEN);
1027 wpa_s->pending_interface_name[0] = '\0';
1028}
1029
1030
1031static struct wpa_supplicant *
1032wpas_p2p_init_group_interface(struct wpa_supplicant *wpa_s, int go)
1033{
1034 struct wpa_interface iface;
1035 struct wpa_supplicant *group_wpa_s;
1036
1037 if (!wpa_s->pending_interface_name[0]) {
1038 wpa_printf(MSG_ERROR, "P2P: No pending group interface");
1039 if (!wpas_p2p_create_iface(wpa_s))
1040 return NULL;
1041 /*
1042 * Something has forced us to remove the pending interface; try
1043 * to create a new one and hope for the best that we will get
1044 * the same local address.
1045 */
1046 if (wpas_p2p_add_group_interface(wpa_s, go ? WPA_IF_P2P_GO :
1047 WPA_IF_P2P_CLIENT) < 0)
1048 return NULL;
1049 }
1050
1051 os_memset(&iface, 0, sizeof(iface));
1052 iface.ifname = wpa_s->pending_interface_name;
1053 iface.driver = wpa_s->driver->name;
1054 iface.ctrl_interface = wpa_s->conf->ctrl_interface;
1055 iface.driver_param = wpa_s->conf->driver_param;
1056 group_wpa_s = wpa_supplicant_add_iface(wpa_s->global, &iface);
1057 if (group_wpa_s == NULL) {
1058 wpa_printf(MSG_ERROR, "P2P: Failed to create new "
1059 "wpa_supplicant interface");
1060 return NULL;
1061 }
1062 wpa_s->pending_interface_name[0] = '\0';
1063 group_wpa_s->parent = wpa_s;
1064 group_wpa_s->p2p_group_interface = go ? P2P_GROUP_INTERFACE_GO :
1065 P2P_GROUP_INTERFACE_CLIENT;
1066 wpa_s->global->p2p_group_formation = group_wpa_s;
1067
1068 wpas_p2p_clone_config(group_wpa_s, wpa_s);
1069
1070 return group_wpa_s;
1071}
1072
1073
1074static void wpas_p2p_group_formation_timeout(void *eloop_ctx,
1075 void *timeout_ctx)
1076{
1077 struct wpa_supplicant *wpa_s = eloop_ctx;
1078 wpa_printf(MSG_DEBUG, "P2P: Group Formation timed out");
1079 if (wpa_s->global->p2p)
1080 p2p_group_formation_failed(wpa_s->global->p2p);
1081 else if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
1082 wpa_drv_p2p_group_formation_failed(wpa_s);
1083 wpas_group_formation_completed(wpa_s, 0);
1084}
1085
1086
1087void wpas_go_neg_completed(void *ctx, struct p2p_go_neg_results *res)
1088{
1089 struct wpa_supplicant *wpa_s = ctx;
1090
1091 if (wpa_s->off_channel_freq || wpa_s->roc_waiting_drv_freq) {
1092 wpa_drv_cancel_remain_on_channel(wpa_s);
1093 wpa_s->off_channel_freq = 0;
1094 wpa_s->roc_waiting_drv_freq = 0;
1095 }
1096
1097 if (res->status) {
1098 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_GO_NEG_FAILURE "status=%d",
1099 res->status);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001100 wpas_notify_p2p_go_neg_completed(wpa_s, res);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001101 wpas_p2p_remove_pending_group_interface(wpa_s);
1102 return;
1103 }
1104
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001105 if (wpa_s->p2p_go_ht40)
1106 res->ht40 = 1;
1107
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001108 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_GO_NEG_SUCCESS);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001109 wpas_notify_p2p_go_neg_completed(wpa_s, res);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001110
Dmitry Shmidt04949592012-07-19 12:16:46 -07001111 if (res->role_go && wpa_s->p2p_persistent_id >= 0) {
1112 struct wpa_ssid *ssid;
1113 ssid = wpa_config_get_network(wpa_s->conf,
1114 wpa_s->p2p_persistent_id);
1115 if (ssid && ssid->disabled == 2 &&
1116 ssid->mode == WPAS_MODE_P2P_GO && ssid->passphrase) {
1117 size_t len = os_strlen(ssid->passphrase);
1118 wpa_printf(MSG_DEBUG, "P2P: Override passphrase based "
1119 "on requested persistent group");
1120 os_memcpy(res->passphrase, ssid->passphrase, len);
1121 res->passphrase[len] = '\0';
1122 }
1123 }
1124
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001125 if (wpa_s->create_p2p_iface) {
1126 struct wpa_supplicant *group_wpa_s =
1127 wpas_p2p_init_group_interface(wpa_s, res->role_go);
1128 if (group_wpa_s == NULL) {
1129 wpas_p2p_remove_pending_group_interface(wpa_s);
1130 return;
1131 }
1132 if (group_wpa_s != wpa_s) {
1133 os_memcpy(group_wpa_s->p2p_pin, wpa_s->p2p_pin,
1134 sizeof(group_wpa_s->p2p_pin));
1135 group_wpa_s->p2p_wps_method = wpa_s->p2p_wps_method;
1136 }
1137 os_memset(wpa_s->pending_interface_addr, 0, ETH_ALEN);
1138 wpa_s->pending_interface_name[0] = '\0';
1139 group_wpa_s->p2p_in_provisioning = 1;
1140
1141 if (res->role_go)
1142 wpas_start_wps_go(group_wpa_s, res, 1);
1143 else
1144 wpas_start_wps_enrollee(group_wpa_s, res);
1145 } else {
1146 wpa_s->p2p_in_provisioning = 1;
1147 wpa_s->global->p2p_group_formation = wpa_s;
1148
1149 if (res->role_go)
1150 wpas_start_wps_go(wpa_s, res, 1);
1151 else
1152 wpas_start_wps_enrollee(ctx, res);
1153 }
1154
1155 wpa_s->p2p_long_listen = 0;
1156 eloop_cancel_timeout(wpas_p2p_long_listen_timeout, wpa_s, NULL);
1157
1158 eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s, NULL);
1159 eloop_register_timeout(15 + res->peer_config_timeout / 100,
1160 (res->peer_config_timeout % 100) * 10000,
1161 wpas_p2p_group_formation_timeout, wpa_s, NULL);
1162}
1163
1164
1165void wpas_go_neg_req_rx(void *ctx, const u8 *src, u16 dev_passwd_id)
1166{
1167 struct wpa_supplicant *wpa_s = ctx;
1168 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_GO_NEG_REQUEST MACSTR
1169 " dev_passwd_id=%u", MAC2STR(src), dev_passwd_id);
1170
1171 wpas_notify_p2p_go_neg_req(wpa_s, src, dev_passwd_id);
1172}
1173
1174
1175void wpas_dev_found(void *ctx, const u8 *addr,
1176 const struct p2p_peer_info *info,
1177 int new_device)
1178{
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001179#ifndef CONFIG_NO_STDOUT_DEBUG
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001180 struct wpa_supplicant *wpa_s = ctx;
1181 char devtype[WPS_DEV_TYPE_BUFSIZE];
Irfan Sheriff8367dc92012-09-09 17:08:19 -07001182#define WFD_DEV_INFO_SIZE 9
1183 char wfd_dev_info_hex[2 * WFD_DEV_INFO_SIZE + 1];
Irfan Sheriff8d965182012-09-11 08:58:24 -07001184 os_memset(wfd_dev_info_hex, 0, sizeof(wfd_dev_info_hex));
Irfan Sheriff8367dc92012-09-09 17:08:19 -07001185#ifdef CONFIG_WIFI_DISPLAY
1186 if (info->wfd_subelems) {
1187 wpa_snprintf_hex(wfd_dev_info_hex, sizeof(wfd_dev_info_hex),
1188 wpabuf_head(info->wfd_subelems),
1189 WFD_DEV_INFO_SIZE);
1190 }
1191#endif /* CONFIG_WIFI_DISPLAY */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001192 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_DEVICE_FOUND MACSTR
1193 " p2p_dev_addr=" MACSTR
1194 " pri_dev_type=%s name='%s' config_methods=0x%x "
Irfan Sheriff8367dc92012-09-09 17:08:19 -07001195 "dev_capab=0x%x group_capab=0x%x%s%s",
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001196 MAC2STR(addr), MAC2STR(info->p2p_device_addr),
1197 wps_dev_type_bin2str(info->pri_dev_type, devtype,
1198 sizeof(devtype)),
1199 info->device_name, info->config_methods,
Irfan Sheriff8367dc92012-09-09 17:08:19 -07001200 info->dev_capab, info->group_capab,
1201 wfd_dev_info_hex[0] ? " wfd_dev_info=0x" : "", wfd_dev_info_hex);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001202#endif /* CONFIG_NO_STDOUT_DEBUG */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001203
1204 wpas_notify_p2p_device_found(ctx, info->p2p_device_addr, new_device);
1205}
1206
1207
1208static void wpas_dev_lost(void *ctx, const u8 *dev_addr)
1209{
1210 struct wpa_supplicant *wpa_s = ctx;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001211
Dmitry Shmidt497c1d52011-07-21 15:19:46 -07001212 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_DEVICE_LOST
1213 "p2p_dev_addr=" MACSTR, MAC2STR(dev_addr));
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001214
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001215 wpas_notify_p2p_device_lost(wpa_s, dev_addr);
1216}
1217
1218
1219static int wpas_start_listen(void *ctx, unsigned int freq,
1220 unsigned int duration,
1221 const struct wpabuf *probe_resp_ie)
1222{
1223 struct wpa_supplicant *wpa_s = ctx;
1224
1225 wpa_drv_set_ap_wps_ie(wpa_s, NULL, probe_resp_ie, NULL);
1226
1227 if (wpa_drv_probe_req_report(wpa_s, 1) < 0) {
1228 wpa_printf(MSG_DEBUG, "P2P: Failed to request the driver to "
1229 "report received Probe Request frames");
1230 return -1;
1231 }
1232
1233 wpa_s->pending_listen_freq = freq;
1234 wpa_s->pending_listen_duration = duration;
1235
1236 if (wpa_drv_remain_on_channel(wpa_s, freq, duration) < 0) {
1237 wpa_printf(MSG_DEBUG, "P2P: Failed to request the driver "
1238 "to remain on channel (%u MHz) for Listen "
1239 "state", freq);
1240 wpa_s->pending_listen_freq = 0;
1241 return -1;
1242 }
1243 wpa_s->off_channel_freq = 0;
1244 wpa_s->roc_waiting_drv_freq = freq;
1245
1246 return 0;
1247}
1248
1249
1250static void wpas_stop_listen(void *ctx)
1251{
1252 struct wpa_supplicant *wpa_s = ctx;
1253 if (wpa_s->off_channel_freq || wpa_s->roc_waiting_drv_freq) {
1254 wpa_drv_cancel_remain_on_channel(wpa_s);
1255 wpa_s->off_channel_freq = 0;
1256 wpa_s->roc_waiting_drv_freq = 0;
1257 }
1258 wpa_drv_set_ap_wps_ie(wpa_s, NULL, NULL, NULL);
1259 wpa_drv_probe_req_report(wpa_s, 0);
1260}
1261
1262
1263static int wpas_send_probe_resp(void *ctx, const struct wpabuf *buf)
1264{
1265 struct wpa_supplicant *wpa_s = ctx;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001266 return wpa_drv_send_mlme(wpa_s, wpabuf_head(buf), wpabuf_len(buf), 1);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001267}
1268
1269
1270static struct p2p_srv_bonjour *
1271wpas_p2p_service_get_bonjour(struct wpa_supplicant *wpa_s,
1272 const struct wpabuf *query)
1273{
1274 struct p2p_srv_bonjour *bsrv;
1275 size_t len;
1276
1277 len = wpabuf_len(query);
1278 dl_list_for_each(bsrv, &wpa_s->global->p2p_srv_bonjour,
1279 struct p2p_srv_bonjour, list) {
1280 if (len == wpabuf_len(bsrv->query) &&
1281 os_memcmp(wpabuf_head(query), wpabuf_head(bsrv->query),
1282 len) == 0)
1283 return bsrv;
1284 }
1285 return NULL;
1286}
1287
1288
1289static struct p2p_srv_upnp *
1290wpas_p2p_service_get_upnp(struct wpa_supplicant *wpa_s, u8 version,
1291 const char *service)
1292{
1293 struct p2p_srv_upnp *usrv;
1294
1295 dl_list_for_each(usrv, &wpa_s->global->p2p_srv_upnp,
1296 struct p2p_srv_upnp, list) {
1297 if (version == usrv->version &&
1298 os_strcmp(service, usrv->service) == 0)
1299 return usrv;
1300 }
1301 return NULL;
1302}
1303
1304
1305static void wpas_sd_add_proto_not_avail(struct wpabuf *resp, u8 srv_proto,
1306 u8 srv_trans_id)
1307{
1308 u8 *len_pos;
1309
1310 if (wpabuf_tailroom(resp) < 5)
1311 return;
1312
1313 /* Length (to be filled) */
1314 len_pos = wpabuf_put(resp, 2);
1315 wpabuf_put_u8(resp, srv_proto);
1316 wpabuf_put_u8(resp, srv_trans_id);
1317 /* Status Code */
1318 wpabuf_put_u8(resp, P2P_SD_PROTO_NOT_AVAILABLE);
1319 /* Response Data: empty */
1320 WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos - 2);
1321}
1322
1323
1324static void wpas_sd_all_bonjour(struct wpa_supplicant *wpa_s,
1325 struct wpabuf *resp, u8 srv_trans_id)
1326{
1327 struct p2p_srv_bonjour *bsrv;
1328 u8 *len_pos;
1329
1330 wpa_printf(MSG_DEBUG, "P2P: SD Request for all Bonjour services");
1331
1332 if (dl_list_empty(&wpa_s->global->p2p_srv_bonjour)) {
1333 wpa_printf(MSG_DEBUG, "P2P: Bonjour protocol not available");
1334 return;
1335 }
1336
1337 dl_list_for_each(bsrv, &wpa_s->global->p2p_srv_bonjour,
1338 struct p2p_srv_bonjour, list) {
1339 if (wpabuf_tailroom(resp) <
1340 5 + wpabuf_len(bsrv->query) + wpabuf_len(bsrv->resp))
1341 return;
1342 /* Length (to be filled) */
1343 len_pos = wpabuf_put(resp, 2);
1344 wpabuf_put_u8(resp, P2P_SERV_BONJOUR);
1345 wpabuf_put_u8(resp, srv_trans_id);
1346 /* Status Code */
1347 wpabuf_put_u8(resp, P2P_SD_SUCCESS);
1348 wpa_hexdump_ascii(MSG_DEBUG, "P2P: Matching Bonjour service",
1349 wpabuf_head(bsrv->resp),
1350 wpabuf_len(bsrv->resp));
1351 /* Response Data */
1352 wpabuf_put_buf(resp, bsrv->query); /* Key */
1353 wpabuf_put_buf(resp, bsrv->resp); /* Value */
1354 WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos -
1355 2);
1356 }
1357}
1358
1359
1360static void wpas_sd_req_bonjour(struct wpa_supplicant *wpa_s,
1361 struct wpabuf *resp, u8 srv_trans_id,
1362 const u8 *query, size_t query_len)
1363{
1364 struct p2p_srv_bonjour *bsrv;
1365 struct wpabuf buf;
1366 u8 *len_pos;
1367
1368 wpa_hexdump_ascii(MSG_DEBUG, "P2P: SD Request for Bonjour",
1369 query, query_len);
1370 if (dl_list_empty(&wpa_s->global->p2p_srv_bonjour)) {
1371 wpa_printf(MSG_DEBUG, "P2P: Bonjour protocol not available");
1372 wpas_sd_add_proto_not_avail(resp, P2P_SERV_BONJOUR,
1373 srv_trans_id);
1374 return;
1375 }
1376
1377 if (query_len == 0) {
1378 wpas_sd_all_bonjour(wpa_s, resp, srv_trans_id);
1379 return;
1380 }
1381
1382 if (wpabuf_tailroom(resp) < 5)
1383 return;
1384 /* Length (to be filled) */
1385 len_pos = wpabuf_put(resp, 2);
1386 wpabuf_put_u8(resp, P2P_SERV_BONJOUR);
1387 wpabuf_put_u8(resp, srv_trans_id);
1388
1389 wpabuf_set(&buf, query, query_len);
1390 bsrv = wpas_p2p_service_get_bonjour(wpa_s, &buf);
1391 if (bsrv == NULL) {
1392 wpa_printf(MSG_DEBUG, "P2P: Requested Bonjour service not "
1393 "available");
1394
1395 /* Status Code */
1396 wpabuf_put_u8(resp, P2P_SD_REQUESTED_INFO_NOT_AVAILABLE);
1397 /* Response Data: empty */
1398 WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos -
1399 2);
1400 return;
1401 }
1402
1403 /* Status Code */
1404 wpabuf_put_u8(resp, P2P_SD_SUCCESS);
1405 wpa_hexdump_ascii(MSG_DEBUG, "P2P: Matching Bonjour service",
1406 wpabuf_head(bsrv->resp), wpabuf_len(bsrv->resp));
1407
1408 if (wpabuf_tailroom(resp) >=
1409 wpabuf_len(bsrv->query) + wpabuf_len(bsrv->resp)) {
1410 /* Response Data */
1411 wpabuf_put_buf(resp, bsrv->query); /* Key */
1412 wpabuf_put_buf(resp, bsrv->resp); /* Value */
1413 }
1414 WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos - 2);
1415}
1416
1417
1418static void wpas_sd_all_upnp(struct wpa_supplicant *wpa_s,
1419 struct wpabuf *resp, u8 srv_trans_id)
1420{
1421 struct p2p_srv_upnp *usrv;
1422 u8 *len_pos;
1423
1424 wpa_printf(MSG_DEBUG, "P2P: SD Request for all UPnP services");
1425
1426 if (dl_list_empty(&wpa_s->global->p2p_srv_upnp)) {
1427 wpa_printf(MSG_DEBUG, "P2P: UPnP protocol not available");
1428 return;
1429 }
1430
1431 dl_list_for_each(usrv, &wpa_s->global->p2p_srv_upnp,
1432 struct p2p_srv_upnp, list) {
1433 if (wpabuf_tailroom(resp) < 5 + 1 + os_strlen(usrv->service))
1434 return;
1435
1436 /* Length (to be filled) */
1437 len_pos = wpabuf_put(resp, 2);
1438 wpabuf_put_u8(resp, P2P_SERV_UPNP);
1439 wpabuf_put_u8(resp, srv_trans_id);
1440
1441 /* Status Code */
1442 wpabuf_put_u8(resp, P2P_SD_SUCCESS);
1443 /* Response Data */
1444 wpabuf_put_u8(resp, usrv->version);
1445 wpa_printf(MSG_DEBUG, "P2P: Matching UPnP Service: %s",
1446 usrv->service);
1447 wpabuf_put_str(resp, usrv->service);
1448 WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos -
1449 2);
1450 }
1451}
1452
1453
1454static void wpas_sd_req_upnp(struct wpa_supplicant *wpa_s,
1455 struct wpabuf *resp, u8 srv_trans_id,
1456 const u8 *query, size_t query_len)
1457{
1458 struct p2p_srv_upnp *usrv;
1459 u8 *len_pos;
1460 u8 version;
1461 char *str;
1462 int count = 0;
1463
1464 wpa_hexdump_ascii(MSG_DEBUG, "P2P: SD Request for UPnP",
1465 query, query_len);
1466
1467 if (dl_list_empty(&wpa_s->global->p2p_srv_upnp)) {
1468 wpa_printf(MSG_DEBUG, "P2P: UPnP protocol not available");
1469 wpas_sd_add_proto_not_avail(resp, P2P_SERV_UPNP,
1470 srv_trans_id);
1471 return;
1472 }
1473
1474 if (query_len == 0) {
1475 wpas_sd_all_upnp(wpa_s, resp, srv_trans_id);
1476 return;
1477 }
1478
1479 if (wpabuf_tailroom(resp) < 5)
1480 return;
1481
1482 /* Length (to be filled) */
1483 len_pos = wpabuf_put(resp, 2);
1484 wpabuf_put_u8(resp, P2P_SERV_UPNP);
1485 wpabuf_put_u8(resp, srv_trans_id);
1486
1487 version = query[0];
1488 str = os_malloc(query_len);
1489 if (str == NULL)
1490 return;
1491 os_memcpy(str, query + 1, query_len - 1);
1492 str[query_len - 1] = '\0';
1493
1494 dl_list_for_each(usrv, &wpa_s->global->p2p_srv_upnp,
1495 struct p2p_srv_upnp, list) {
1496 if (version != usrv->version)
1497 continue;
1498
1499 if (os_strcmp(str, "ssdp:all") != 0 &&
1500 os_strstr(usrv->service, str) == NULL)
1501 continue;
1502
1503 if (wpabuf_tailroom(resp) < 2)
1504 break;
1505 if (count == 0) {
1506 /* Status Code */
1507 wpabuf_put_u8(resp, P2P_SD_SUCCESS);
1508 /* Response Data */
1509 wpabuf_put_u8(resp, version);
1510 } else
1511 wpabuf_put_u8(resp, ',');
1512
1513 count++;
1514
1515 wpa_printf(MSG_DEBUG, "P2P: Matching UPnP Service: %s",
1516 usrv->service);
1517 if (wpabuf_tailroom(resp) < os_strlen(usrv->service))
1518 break;
1519 wpabuf_put_str(resp, usrv->service);
1520 }
1521 os_free(str);
1522
1523 if (count == 0) {
1524 wpa_printf(MSG_DEBUG, "P2P: Requested UPnP service not "
1525 "available");
1526 /* Status Code */
1527 wpabuf_put_u8(resp, P2P_SD_REQUESTED_INFO_NOT_AVAILABLE);
1528 /* Response Data: empty */
1529 }
1530
1531 WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos - 2);
1532}
1533
1534
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001535#ifdef CONFIG_WIFI_DISPLAY
1536static void wpas_sd_req_wfd(struct wpa_supplicant *wpa_s,
1537 struct wpabuf *resp, u8 srv_trans_id,
1538 const u8 *query, size_t query_len)
1539{
1540 const u8 *pos;
1541 u8 role;
1542 u8 *len_pos;
1543
1544 wpa_hexdump(MSG_DEBUG, "P2P: SD Request for WFD", query, query_len);
1545
1546 if (!wpa_s->global->wifi_display) {
1547 wpa_printf(MSG_DEBUG, "P2P: WFD protocol not available");
1548 wpas_sd_add_proto_not_avail(resp, P2P_SERV_WIFI_DISPLAY,
1549 srv_trans_id);
1550 return;
1551 }
1552
1553 if (query_len < 1) {
1554 wpa_printf(MSG_DEBUG, "P2P: Missing WFD Requested Device "
1555 "Role");
1556 return;
1557 }
1558
1559 if (wpabuf_tailroom(resp) < 5)
1560 return;
1561
1562 pos = query;
1563 role = *pos++;
1564 wpa_printf(MSG_DEBUG, "P2P: WSD for device role 0x%x", role);
1565
1566 /* TODO: role specific handling */
1567
1568 /* Length (to be filled) */
1569 len_pos = wpabuf_put(resp, 2);
1570 wpabuf_put_u8(resp, P2P_SERV_WIFI_DISPLAY);
1571 wpabuf_put_u8(resp, srv_trans_id);
1572 wpabuf_put_u8(resp, P2P_SD_SUCCESS); /* Status Code */
1573
1574 while (pos < query + query_len) {
1575 if (*pos < MAX_WFD_SUBELEMS &&
1576 wpa_s->global->wfd_subelem[*pos] &&
1577 wpabuf_tailroom(resp) >=
1578 wpabuf_len(wpa_s->global->wfd_subelem[*pos])) {
1579 wpa_printf(MSG_DEBUG, "P2P: Add WSD response "
1580 "subelement %u", *pos);
1581 wpabuf_put_buf(resp, wpa_s->global->wfd_subelem[*pos]);
1582 }
1583 pos++;
1584 }
1585
1586 WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos - 2);
1587}
1588#endif /* CONFIG_WIFI_DISPLAY */
1589
1590
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001591void wpas_sd_request(void *ctx, int freq, const u8 *sa, u8 dialog_token,
1592 u16 update_indic, const u8 *tlvs, size_t tlvs_len)
1593{
1594 struct wpa_supplicant *wpa_s = ctx;
1595 const u8 *pos = tlvs;
1596 const u8 *end = tlvs + tlvs_len;
1597 const u8 *tlv_end;
1598 u16 slen;
1599 struct wpabuf *resp;
1600 u8 srv_proto, srv_trans_id;
1601 size_t buf_len;
1602 char *buf;
1603
1604 wpa_hexdump(MSG_MSGDUMP, "P2P: Service Discovery Request TLVs",
1605 tlvs, tlvs_len);
1606 buf_len = 2 * tlvs_len + 1;
1607 buf = os_malloc(buf_len);
1608 if (buf) {
1609 wpa_snprintf_hex(buf, buf_len, tlvs, tlvs_len);
1610 wpa_msg_ctrl(wpa_s, MSG_INFO, P2P_EVENT_SERV_DISC_REQ "%d "
1611 MACSTR " %u %u %s",
1612 freq, MAC2STR(sa), dialog_token, update_indic,
1613 buf);
1614 os_free(buf);
1615 }
1616
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001617 if (wpa_s->p2p_sd_over_ctrl_iface) {
1618 wpas_notify_p2p_sd_request(wpa_s, freq, sa, dialog_token,
1619 update_indic, tlvs, tlvs_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001620 return; /* to be processed by an external program */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001621 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001622
1623 resp = wpabuf_alloc(10000);
1624 if (resp == NULL)
1625 return;
1626
1627 while (pos + 1 < end) {
1628 wpa_printf(MSG_DEBUG, "P2P: Service Request TLV");
1629 slen = WPA_GET_LE16(pos);
1630 pos += 2;
1631 if (pos + slen > end || slen < 2) {
1632 wpa_printf(MSG_DEBUG, "P2P: Unexpected Query Data "
1633 "length");
1634 wpabuf_free(resp);
1635 return;
1636 }
1637 tlv_end = pos + slen;
1638
1639 srv_proto = *pos++;
1640 wpa_printf(MSG_DEBUG, "P2P: Service Protocol Type %u",
1641 srv_proto);
1642 srv_trans_id = *pos++;
1643 wpa_printf(MSG_DEBUG, "P2P: Service Transaction ID %u",
1644 srv_trans_id);
1645
1646 wpa_hexdump(MSG_MSGDUMP, "P2P: Query Data",
1647 pos, tlv_end - pos);
1648
1649
1650 if (wpa_s->force_long_sd) {
1651 wpa_printf(MSG_DEBUG, "P2P: SD test - force long "
1652 "response");
1653 wpas_sd_all_bonjour(wpa_s, resp, srv_trans_id);
1654 wpas_sd_all_upnp(wpa_s, resp, srv_trans_id);
1655 goto done;
1656 }
1657
1658 switch (srv_proto) {
1659 case P2P_SERV_ALL_SERVICES:
1660 wpa_printf(MSG_DEBUG, "P2P: Service Discovery Request "
1661 "for all services");
1662 if (dl_list_empty(&wpa_s->global->p2p_srv_upnp) &&
1663 dl_list_empty(&wpa_s->global->p2p_srv_bonjour)) {
1664 wpa_printf(MSG_DEBUG, "P2P: No service "
1665 "discovery protocols available");
1666 wpas_sd_add_proto_not_avail(
1667 resp, P2P_SERV_ALL_SERVICES,
1668 srv_trans_id);
1669 break;
1670 }
1671 wpas_sd_all_bonjour(wpa_s, resp, srv_trans_id);
1672 wpas_sd_all_upnp(wpa_s, resp, srv_trans_id);
1673 break;
1674 case P2P_SERV_BONJOUR:
1675 wpas_sd_req_bonjour(wpa_s, resp, srv_trans_id,
1676 pos, tlv_end - pos);
1677 break;
1678 case P2P_SERV_UPNP:
1679 wpas_sd_req_upnp(wpa_s, resp, srv_trans_id,
1680 pos, tlv_end - pos);
1681 break;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001682#ifdef CONFIG_WIFI_DISPLAY
1683 case P2P_SERV_WIFI_DISPLAY:
1684 wpas_sd_req_wfd(wpa_s, resp, srv_trans_id,
1685 pos, tlv_end - pos);
1686 break;
1687#endif /* CONFIG_WIFI_DISPLAY */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001688 default:
1689 wpa_printf(MSG_DEBUG, "P2P: Unavailable service "
1690 "protocol %u", srv_proto);
1691 wpas_sd_add_proto_not_avail(resp, srv_proto,
1692 srv_trans_id);
1693 break;
1694 }
1695
1696 pos = tlv_end;
1697 }
1698
1699done:
1700 wpas_notify_p2p_sd_request(wpa_s, freq, sa, dialog_token,
1701 update_indic, tlvs, tlvs_len);
1702
1703 wpas_p2p_sd_response(wpa_s, freq, sa, dialog_token, resp);
1704
1705 wpabuf_free(resp);
1706}
1707
1708
1709void wpas_sd_response(void *ctx, const u8 *sa, u16 update_indic,
1710 const u8 *tlvs, size_t tlvs_len)
1711{
1712 struct wpa_supplicant *wpa_s = ctx;
1713 const u8 *pos = tlvs;
1714 const u8 *end = tlvs + tlvs_len;
1715 const u8 *tlv_end;
1716 u16 slen;
1717 size_t buf_len;
1718 char *buf;
1719
1720 wpa_hexdump(MSG_MSGDUMP, "P2P: Service Discovery Response TLVs",
1721 tlvs, tlvs_len);
1722 if (tlvs_len > 1500) {
1723 /* TODO: better way for handling this */
1724 wpa_msg_ctrl(wpa_s, MSG_INFO,
1725 P2P_EVENT_SERV_DISC_RESP MACSTR
1726 " %u <long response: %u bytes>",
1727 MAC2STR(sa), update_indic,
1728 (unsigned int) tlvs_len);
1729 } else {
1730 buf_len = 2 * tlvs_len + 1;
1731 buf = os_malloc(buf_len);
1732 if (buf) {
1733 wpa_snprintf_hex(buf, buf_len, tlvs, tlvs_len);
1734 wpa_msg_ctrl(wpa_s, MSG_INFO,
1735 P2P_EVENT_SERV_DISC_RESP MACSTR " %u %s",
1736 MAC2STR(sa), update_indic, buf);
1737 os_free(buf);
1738 }
1739 }
1740
1741 while (pos < end) {
1742 u8 srv_proto, srv_trans_id, status;
1743
1744 wpa_printf(MSG_DEBUG, "P2P: Service Response TLV");
1745 slen = WPA_GET_LE16(pos);
1746 pos += 2;
1747 if (pos + slen > end || slen < 3) {
1748 wpa_printf(MSG_DEBUG, "P2P: Unexpected Response Data "
1749 "length");
1750 return;
1751 }
1752 tlv_end = pos + slen;
1753
1754 srv_proto = *pos++;
1755 wpa_printf(MSG_DEBUG, "P2P: Service Protocol Type %u",
1756 srv_proto);
1757 srv_trans_id = *pos++;
1758 wpa_printf(MSG_DEBUG, "P2P: Service Transaction ID %u",
1759 srv_trans_id);
1760 status = *pos++;
1761 wpa_printf(MSG_DEBUG, "P2P: Status Code ID %u",
1762 status);
1763
1764 wpa_hexdump(MSG_MSGDUMP, "P2P: Response Data",
1765 pos, tlv_end - pos);
1766
1767 pos = tlv_end;
1768 }
1769
1770 wpas_notify_p2p_sd_response(wpa_s, sa, update_indic, tlvs, tlvs_len);
1771}
1772
1773
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001774u64 wpas_p2p_sd_request(struct wpa_supplicant *wpa_s, const u8 *dst,
1775 const struct wpabuf *tlvs)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001776{
1777 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001778 return wpa_drv_p2p_sd_request(wpa_s, dst, tlvs);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001779 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001780 return 0;
1781 return (uintptr_t) p2p_sd_request(wpa_s->global->p2p, dst, tlvs);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001782}
1783
1784
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001785u64 wpas_p2p_sd_request_upnp(struct wpa_supplicant *wpa_s, const u8 *dst,
1786 u8 version, const char *query)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001787{
1788 struct wpabuf *tlvs;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001789 u64 ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001790
1791 tlvs = wpabuf_alloc(2 + 1 + 1 + 1 + os_strlen(query));
1792 if (tlvs == NULL)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001793 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001794 wpabuf_put_le16(tlvs, 1 + 1 + 1 + os_strlen(query));
1795 wpabuf_put_u8(tlvs, P2P_SERV_UPNP); /* Service Protocol Type */
1796 wpabuf_put_u8(tlvs, 1); /* Service Transaction ID */
1797 wpabuf_put_u8(tlvs, version);
1798 wpabuf_put_str(tlvs, query);
1799 ret = wpas_p2p_sd_request(wpa_s, dst, tlvs);
1800 wpabuf_free(tlvs);
1801 return ret;
1802}
1803
1804
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001805#ifdef CONFIG_WIFI_DISPLAY
1806
1807static u64 wpas_p2p_sd_request_wfd(struct wpa_supplicant *wpa_s, const u8 *dst,
1808 const struct wpabuf *tlvs)
1809{
1810 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
1811 return 0;
1812 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
1813 return 0;
1814 return (uintptr_t) p2p_sd_request_wfd(wpa_s->global->p2p, dst, tlvs);
1815}
1816
1817
1818#define MAX_WFD_SD_SUBELEMS 20
1819
1820static void wfd_add_sd_req_role(struct wpabuf *tlvs, u8 id, u8 role,
1821 const char *subelems)
1822{
1823 u8 *len;
1824 const char *pos;
1825 int val;
1826 int count = 0;
1827
1828 len = wpabuf_put(tlvs, 2);
1829 wpabuf_put_u8(tlvs, P2P_SERV_WIFI_DISPLAY); /* Service Protocol Type */
1830 wpabuf_put_u8(tlvs, id); /* Service Transaction ID */
1831
1832 wpabuf_put_u8(tlvs, role);
1833
1834 pos = subelems;
1835 while (*pos) {
1836 val = atoi(pos);
1837 if (val >= 0 && val < 256) {
1838 wpabuf_put_u8(tlvs, val);
1839 count++;
1840 if (count == MAX_WFD_SD_SUBELEMS)
1841 break;
1842 }
1843 pos = os_strchr(pos + 1, ',');
1844 if (pos == NULL)
1845 break;
1846 pos++;
1847 }
1848
1849 WPA_PUT_LE16(len, (u8 *) wpabuf_put(tlvs, 0) - len - 2);
1850}
1851
1852
1853u64 wpas_p2p_sd_request_wifi_display(struct wpa_supplicant *wpa_s,
1854 const u8 *dst, const char *role)
1855{
1856 struct wpabuf *tlvs;
1857 u64 ret;
1858 const char *subelems;
1859 u8 id = 1;
1860
1861 subelems = os_strchr(role, ' ');
1862 if (subelems == NULL)
1863 return 0;
1864 subelems++;
1865
1866 tlvs = wpabuf_alloc(4 * (2 + 1 + 1 + 1 + MAX_WFD_SD_SUBELEMS));
1867 if (tlvs == NULL)
1868 return 0;
1869
1870 if (os_strstr(role, "[source]"))
1871 wfd_add_sd_req_role(tlvs, id++, 0x00, subelems);
1872 if (os_strstr(role, "[pri-sink]"))
1873 wfd_add_sd_req_role(tlvs, id++, 0x01, subelems);
1874 if (os_strstr(role, "[sec-sink]"))
1875 wfd_add_sd_req_role(tlvs, id++, 0x02, subelems);
1876 if (os_strstr(role, "[source+sink]"))
1877 wfd_add_sd_req_role(tlvs, id++, 0x03, subelems);
1878
1879 ret = wpas_p2p_sd_request_wfd(wpa_s, dst, tlvs);
1880 wpabuf_free(tlvs);
1881 return ret;
1882}
1883
1884#endif /* CONFIG_WIFI_DISPLAY */
1885
1886
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001887int wpas_p2p_sd_cancel_request(struct wpa_supplicant *wpa_s, u64 req)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001888{
1889 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001890 return wpa_drv_p2p_sd_cancel_request(wpa_s, req);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001891 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
1892 return -1;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001893 return p2p_sd_cancel_request(wpa_s->global->p2p,
1894 (void *) (uintptr_t) req);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001895}
1896
1897
1898void wpas_p2p_sd_response(struct wpa_supplicant *wpa_s, int freq,
1899 const u8 *dst, u8 dialog_token,
1900 const struct wpabuf *resp_tlvs)
1901{
1902 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT) {
1903 wpa_drv_p2p_sd_response(wpa_s, freq, dst, dialog_token,
1904 resp_tlvs);
1905 return;
1906 }
1907 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
1908 return;
1909 p2p_sd_response(wpa_s->global->p2p, freq, dst, dialog_token,
1910 resp_tlvs);
1911}
1912
Dmitry Shmidtb5e8f062012-08-08 10:56:33 -07001913#ifdef ANDROID_P2P
1914void wpas_p2p_sd_service_update(struct wpa_supplicant *wpa_s, int action)
1915#else
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001916void wpas_p2p_sd_service_update(struct wpa_supplicant *wpa_s)
Dmitry Shmidtb5e8f062012-08-08 10:56:33 -07001917#endif
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001918{
1919 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT) {
1920 wpa_drv_p2p_service_update(wpa_s);
1921 return;
1922 }
1923 if (wpa_s->global->p2p)
Dmitry Shmidtb5e8f062012-08-08 10:56:33 -07001924#ifdef ANDROID_P2P
1925 p2p_sd_service_update(wpa_s->global->p2p, action);
1926#else
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001927 p2p_sd_service_update(wpa_s->global->p2p);
Dmitry Shmidtb5e8f062012-08-08 10:56:33 -07001928#endif
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001929}
1930
1931
1932static void wpas_p2p_srv_bonjour_free(struct p2p_srv_bonjour *bsrv)
1933{
1934 dl_list_del(&bsrv->list);
1935 wpabuf_free(bsrv->query);
1936 wpabuf_free(bsrv->resp);
1937 os_free(bsrv);
1938}
1939
1940
1941static void wpas_p2p_srv_upnp_free(struct p2p_srv_upnp *usrv)
1942{
1943 dl_list_del(&usrv->list);
1944 os_free(usrv->service);
1945 os_free(usrv);
1946}
1947
1948
1949void wpas_p2p_service_flush(struct wpa_supplicant *wpa_s)
1950{
1951 struct p2p_srv_bonjour *bsrv, *bn;
1952 struct p2p_srv_upnp *usrv, *un;
1953
1954 dl_list_for_each_safe(bsrv, bn, &wpa_s->global->p2p_srv_bonjour,
1955 struct p2p_srv_bonjour, list)
1956 wpas_p2p_srv_bonjour_free(bsrv);
1957
1958 dl_list_for_each_safe(usrv, un, &wpa_s->global->p2p_srv_upnp,
1959 struct p2p_srv_upnp, list)
1960 wpas_p2p_srv_upnp_free(usrv);
1961
Dmitry Shmidtb5e8f062012-08-08 10:56:33 -07001962#ifdef ANDROID_P2P
1963 wpas_p2p_sd_service_update(wpa_s, SRV_FLUSH);
1964#else
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001965 wpas_p2p_sd_service_update(wpa_s);
Dmitry Shmidtb5e8f062012-08-08 10:56:33 -07001966#endif
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001967}
1968
1969
1970int wpas_p2p_service_add_bonjour(struct wpa_supplicant *wpa_s,
1971 struct wpabuf *query, struct wpabuf *resp)
1972{
1973 struct p2p_srv_bonjour *bsrv;
1974
1975 bsrv = wpas_p2p_service_get_bonjour(wpa_s, query);
1976 if (bsrv) {
1977 wpabuf_free(query);
1978 wpabuf_free(bsrv->resp);
1979 bsrv->resp = resp;
1980 return 0;
1981 }
1982
1983 bsrv = os_zalloc(sizeof(*bsrv));
1984 if (bsrv == NULL)
1985 return -1;
1986 bsrv->query = query;
1987 bsrv->resp = resp;
1988 dl_list_add(&wpa_s->global->p2p_srv_bonjour, &bsrv->list);
1989
Dmitry Shmidtb5e8f062012-08-08 10:56:33 -07001990#ifdef ANDROID_P2P
1991 wpas_p2p_sd_service_update(wpa_s, SRV_ADD);
1992#else
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001993 wpas_p2p_sd_service_update(wpa_s);
Dmitry Shmidtb5e8f062012-08-08 10:56:33 -07001994#endif
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001995 return 0;
1996}
1997
1998
1999int wpas_p2p_service_del_bonjour(struct wpa_supplicant *wpa_s,
2000 const struct wpabuf *query)
2001{
2002 struct p2p_srv_bonjour *bsrv;
2003
2004 bsrv = wpas_p2p_service_get_bonjour(wpa_s, query);
2005 if (bsrv == NULL)
2006 return -1;
2007 wpas_p2p_srv_bonjour_free(bsrv);
Dmitry Shmidtb5e8f062012-08-08 10:56:33 -07002008#ifdef ANDROID_P2P
2009 wpas_p2p_sd_service_update(wpa_s, SRV_DEL);
2010#else
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002011 wpas_p2p_sd_service_update(wpa_s);
Dmitry Shmidtb5e8f062012-08-08 10:56:33 -07002012#endif
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002013 return 0;
2014}
2015
2016
2017int wpas_p2p_service_add_upnp(struct wpa_supplicant *wpa_s, u8 version,
2018 const char *service)
2019{
2020 struct p2p_srv_upnp *usrv;
2021
2022 if (wpas_p2p_service_get_upnp(wpa_s, version, service))
2023 return 0; /* Already listed */
2024 usrv = os_zalloc(sizeof(*usrv));
2025 if (usrv == NULL)
2026 return -1;
2027 usrv->version = version;
2028 usrv->service = os_strdup(service);
2029 if (usrv->service == NULL) {
2030 os_free(usrv);
2031 return -1;
2032 }
2033 dl_list_add(&wpa_s->global->p2p_srv_upnp, &usrv->list);
2034
Dmitry Shmidtb5e8f062012-08-08 10:56:33 -07002035#ifdef ANDROID_P2P
2036 wpas_p2p_sd_service_update(wpa_s, SRV_ADD);
2037#else
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002038 wpas_p2p_sd_service_update(wpa_s);
Dmitry Shmidtb5e8f062012-08-08 10:56:33 -07002039#endif
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002040 return 0;
2041}
2042
2043
2044int wpas_p2p_service_del_upnp(struct wpa_supplicant *wpa_s, u8 version,
2045 const char *service)
2046{
2047 struct p2p_srv_upnp *usrv;
2048
2049 usrv = wpas_p2p_service_get_upnp(wpa_s, version, service);
2050 if (usrv == NULL)
2051 return -1;
2052 wpas_p2p_srv_upnp_free(usrv);
Dmitry Shmidtb5e8f062012-08-08 10:56:33 -07002053#ifdef ANDROID_P2P
2054 wpas_p2p_sd_service_update(wpa_s, SRV_DEL);
2055#else
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002056 wpas_p2p_sd_service_update(wpa_s);
Dmitry Shmidtb5e8f062012-08-08 10:56:33 -07002057#endif
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002058 return 0;
2059}
2060
2061
2062static void wpas_prov_disc_local_display(struct wpa_supplicant *wpa_s,
2063 const u8 *peer, const char *params,
2064 unsigned int generated_pin)
2065{
2066 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_SHOW_PIN MACSTR " %08d%s",
2067 MAC2STR(peer), generated_pin, params);
2068}
2069
2070
2071static void wpas_prov_disc_local_keypad(struct wpa_supplicant *wpa_s,
2072 const u8 *peer, const char *params)
2073{
2074 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_ENTER_PIN MACSTR "%s",
2075 MAC2STR(peer), params);
2076}
2077
2078
2079void wpas_prov_disc_req(void *ctx, const u8 *peer, u16 config_methods,
2080 const u8 *dev_addr, const u8 *pri_dev_type,
2081 const char *dev_name, u16 supp_config_methods,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002082 u8 dev_capab, u8 group_capab, const u8 *group_id,
2083 size_t group_id_len)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002084{
2085 struct wpa_supplicant *wpa_s = ctx;
2086 char devtype[WPS_DEV_TYPE_BUFSIZE];
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002087 char params[300];
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002088 u8 empty_dev_type[8];
2089 unsigned int generated_pin = 0;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002090 struct wpa_supplicant *group = NULL;
2091
2092 if (group_id) {
2093 for (group = wpa_s->global->ifaces; group; group = group->next)
2094 {
2095 struct wpa_ssid *s = group->current_ssid;
2096 if (s != NULL &&
2097 s->mode == WPAS_MODE_P2P_GO &&
2098 group_id_len - ETH_ALEN == s->ssid_len &&
2099 os_memcmp(group_id + ETH_ALEN, s->ssid,
2100 s->ssid_len) == 0)
2101 break;
2102 }
2103 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002104
2105 if (pri_dev_type == NULL) {
2106 os_memset(empty_dev_type, 0, sizeof(empty_dev_type));
2107 pri_dev_type = empty_dev_type;
2108 }
2109 os_snprintf(params, sizeof(params), " p2p_dev_addr=" MACSTR
2110 " pri_dev_type=%s name='%s' config_methods=0x%x "
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002111 "dev_capab=0x%x group_capab=0x%x%s%s",
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002112 MAC2STR(dev_addr),
2113 wps_dev_type_bin2str(pri_dev_type, devtype,
2114 sizeof(devtype)),
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002115 dev_name, supp_config_methods, dev_capab, group_capab,
2116 group ? " group=" : "",
2117 group ? group->ifname : "");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002118 params[sizeof(params) - 1] = '\0';
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08002119
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002120 if (config_methods & WPS_CONFIG_DISPLAY) {
2121 generated_pin = wps_generate_pin();
2122 wpas_prov_disc_local_display(wpa_s, peer, params,
2123 generated_pin);
2124 } else if (config_methods & WPS_CONFIG_KEYPAD)
2125 wpas_prov_disc_local_keypad(wpa_s, peer, params);
2126 else if (config_methods & WPS_CONFIG_PUSHBUTTON)
2127 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_PBC_REQ MACSTR
2128 "%s", MAC2STR(peer), params);
Jouni Malinen75ecf522011-06-27 15:19:46 -07002129
2130 wpas_notify_p2p_provision_discovery(wpa_s, peer, 1 /* request */,
2131 P2P_PROV_DISC_SUCCESS,
2132 config_methods, generated_pin);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002133}
2134
2135
2136void wpas_prov_disc_resp(void *ctx, const u8 *peer, u16 config_methods)
2137{
2138 struct wpa_supplicant *wpa_s = ctx;
2139 unsigned int generated_pin = 0;
Dmitry Shmidt04949592012-07-19 12:16:46 -07002140 char params[20];
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002141
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08002142 if (wpa_s->pending_pd_before_join &&
2143 (os_memcmp(peer, wpa_s->pending_join_dev_addr, ETH_ALEN) == 0 ||
2144 os_memcmp(peer, wpa_s->pending_join_iface_addr, ETH_ALEN) == 0)) {
2145 wpa_s->pending_pd_before_join = 0;
2146 wpa_printf(MSG_DEBUG, "P2P: Starting pending "
2147 "join-existing-group operation");
2148 wpas_p2p_join_start(wpa_s);
2149 return;
2150 }
2151
Dmitry Shmidt04949592012-07-19 12:16:46 -07002152 if (wpa_s->pending_pd_use == AUTO_PD_JOIN ||
2153 wpa_s->pending_pd_use == AUTO_PD_GO_NEG)
2154 os_snprintf(params, sizeof(params), " peer_go=%d",
2155 wpa_s->pending_pd_use == AUTO_PD_JOIN);
2156 else
2157 params[0] = '\0';
2158
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002159 if (config_methods & WPS_CONFIG_DISPLAY)
Dmitry Shmidt04949592012-07-19 12:16:46 -07002160 wpas_prov_disc_local_keypad(wpa_s, peer, params);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002161 else if (config_methods & WPS_CONFIG_KEYPAD) {
2162 generated_pin = wps_generate_pin();
Dmitry Shmidt04949592012-07-19 12:16:46 -07002163 wpas_prov_disc_local_display(wpa_s, peer, params,
2164 generated_pin);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002165 } else if (config_methods & WPS_CONFIG_PUSHBUTTON)
Dmitry Shmidt04949592012-07-19 12:16:46 -07002166 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_PBC_RESP MACSTR
2167 "%s", MAC2STR(peer), params);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002168
Jouni Malinen75ecf522011-06-27 15:19:46 -07002169 wpas_notify_p2p_provision_discovery(wpa_s, peer, 0 /* response */,
2170 P2P_PROV_DISC_SUCCESS,
2171 config_methods, generated_pin);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002172}
2173
2174
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002175static void wpas_prov_disc_fail(void *ctx, const u8 *peer,
2176 enum p2p_prov_disc_status status)
Jouni Malinen75ecf522011-06-27 15:19:46 -07002177{
2178 struct wpa_supplicant *wpa_s = ctx;
2179
Dmitry Shmidt04949592012-07-19 12:16:46 -07002180 if (wpa_s->p2p_fallback_to_go_neg) {
2181 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: PD for p2p_connect-auto "
2182 "failed - fall back to GO Negotiation");
2183 wpas_p2p_fallback_to_go_neg(wpa_s, 0);
2184 return;
2185 }
2186
Dmitry Shmidt2b380482012-09-13 10:52:13 -07002187#ifdef ANDROID_P2P
2188 /* If provision discovery failed it is safe to cancel the timer here and
2189 * also do not start the join */
2190 if (wpa_s->pending_pd_before_join &&
2191 (os_memcmp(peer, wpa_s->pending_join_dev_addr, ETH_ALEN) == 0 ||
2192 os_memcmp(peer, wpa_s->pending_join_iface_addr, ETH_ALEN) == 0)) {
2193 wpa_s->pending_pd_before_join = 0;
2194 wpa_printf(MSG_DEBUG, "P2P: Do not Start pending "
2195 "join-existing-group operation");
2196 eloop_cancel_timeout(wpas_p2p_pd_before_join_timeout, wpa_s, NULL);
2197 }
2198#endif /* ANDROID_P2P */
Dmitry Shmidt04949592012-07-19 12:16:46 -07002199 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_FAILURE
2200 " p2p_dev_addr=" MACSTR " status=%d",
2201 MAC2STR(peer), status);
2202
Jouni Malinen75ecf522011-06-27 15:19:46 -07002203 wpas_notify_p2p_provision_discovery(wpa_s, peer, 0 /* response */,
2204 status, 0, 0);
2205}
2206
2207
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002208static u8 wpas_invitation_process(void *ctx, const u8 *sa, const u8 *bssid,
2209 const u8 *go_dev_addr, const u8 *ssid,
2210 size_t ssid_len, int *go, u8 *group_bssid,
2211 int *force_freq, int persistent_group)
2212{
2213 struct wpa_supplicant *wpa_s = ctx;
2214 struct wpa_ssid *s;
2215 u8 cur_bssid[ETH_ALEN];
2216 int res;
2217 struct wpa_supplicant *grp;
2218
2219 if (!persistent_group) {
2220 wpa_printf(MSG_DEBUG, "P2P: Invitation from " MACSTR
2221 " to join an active group", MAC2STR(sa));
2222 if (!is_zero_ether_addr(wpa_s->p2p_auth_invite) &&
2223 (os_memcmp(go_dev_addr, wpa_s->p2p_auth_invite, ETH_ALEN)
2224 == 0 ||
2225 os_memcmp(sa, wpa_s->p2p_auth_invite, ETH_ALEN) == 0)) {
2226 wpa_printf(MSG_DEBUG, "P2P: Accept previously "
2227 "authorized invitation");
2228 goto accept_inv;
2229 }
2230 /*
2231 * Do not accept the invitation automatically; notify user and
2232 * request approval.
2233 */
2234 return P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE;
2235 }
2236
2237 grp = wpas_get_p2p_group(wpa_s, ssid, ssid_len, go);
2238 if (grp) {
2239 wpa_printf(MSG_DEBUG, "P2P: Accept invitation to already "
2240 "running persistent group");
2241 if (*go)
2242 os_memcpy(group_bssid, grp->own_addr, ETH_ALEN);
2243 goto accept_inv;
2244 }
2245
2246 if (!wpa_s->conf->persistent_reconnect)
2247 return P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE;
2248
2249 for (s = wpa_s->conf->ssid; s; s = s->next) {
2250 if (s->disabled == 2 &&
2251 os_memcmp(s->bssid, go_dev_addr, ETH_ALEN) == 0 &&
2252 s->ssid_len == ssid_len &&
2253 os_memcmp(ssid, s->ssid, ssid_len) == 0)
2254 break;
2255 }
2256
2257 if (!s) {
2258 wpa_printf(MSG_DEBUG, "P2P: Invitation from " MACSTR
2259 " requested reinvocation of an unknown group",
2260 MAC2STR(sa));
2261 return P2P_SC_FAIL_UNKNOWN_GROUP;
2262 }
2263
2264 if (s->mode == WPAS_MODE_P2P_GO && !wpas_p2p_create_iface(wpa_s)) {
2265 *go = 1;
2266 if (wpa_s->wpa_state >= WPA_AUTHENTICATING) {
2267 wpa_printf(MSG_DEBUG, "P2P: The only available "
2268 "interface is already in use - reject "
2269 "invitation");
2270 return P2P_SC_FAIL_UNABLE_TO_ACCOMMODATE;
2271 }
2272 os_memcpy(group_bssid, wpa_s->own_addr, ETH_ALEN);
2273 } else if (s->mode == WPAS_MODE_P2P_GO) {
2274 *go = 1;
2275 if (wpas_p2p_add_group_interface(wpa_s, WPA_IF_P2P_GO) < 0)
2276 {
2277 wpa_printf(MSG_ERROR, "P2P: Failed to allocate a new "
2278 "interface address for the group");
2279 return P2P_SC_FAIL_UNABLE_TO_ACCOMMODATE;
2280 }
2281 os_memcpy(group_bssid, wpa_s->pending_interface_addr,
2282 ETH_ALEN);
2283 }
2284
2285accept_inv:
2286 if (wpa_s->current_ssid && wpa_drv_get_bssid(wpa_s, cur_bssid) == 0 &&
2287 wpa_s->assoc_freq) {
2288 wpa_printf(MSG_DEBUG, "P2P: Trying to force channel to match "
2289 "the channel we are already using");
2290 *force_freq = wpa_s->assoc_freq;
2291 }
2292
2293 res = wpa_drv_shared_freq(wpa_s);
2294 if (res > 0) {
2295 wpa_printf(MSG_DEBUG, "P2P: Trying to force channel to match "
2296 "with the channel we are already using on a "
2297 "shared interface");
2298 *force_freq = res;
2299 }
2300
2301 return P2P_SC_SUCCESS;
2302}
2303
2304
2305static void wpas_invitation_received(void *ctx, const u8 *sa, const u8 *bssid,
2306 const u8 *ssid, size_t ssid_len,
2307 const u8 *go_dev_addr, u8 status,
2308 int op_freq)
2309{
2310 struct wpa_supplicant *wpa_s = ctx;
2311 struct wpa_ssid *s;
2312
2313 for (s = wpa_s->conf->ssid; s; s = s->next) {
2314 if (s->disabled == 2 &&
2315 s->ssid_len == ssid_len &&
2316 os_memcmp(ssid, s->ssid, ssid_len) == 0)
2317 break;
2318 }
2319
2320 if (status == P2P_SC_SUCCESS) {
2321 wpa_printf(MSG_DEBUG, "P2P: Invitation from peer " MACSTR
2322 " was accepted; op_freq=%d MHz",
2323 MAC2STR(sa), op_freq);
2324 if (s) {
2325 wpas_p2p_group_add_persistent(
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002326 wpa_s, s, s->mode == WPAS_MODE_P2P_GO, 0, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002327 } else if (bssid) {
2328 wpas_p2p_join(wpa_s, bssid, go_dev_addr,
Dmitry Shmidt04949592012-07-19 12:16:46 -07002329 wpa_s->p2p_wps_method, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002330 }
2331 return;
2332 }
2333
2334 if (status != P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE) {
2335 wpa_printf(MSG_DEBUG, "P2P: Invitation from peer " MACSTR
2336 " was rejected (status %u)", MAC2STR(sa), status);
2337 return;
2338 }
2339
2340 if (!s) {
2341 if (bssid) {
2342 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_INVITATION_RECEIVED
2343 "sa=" MACSTR " go_dev_addr=" MACSTR
2344 " bssid=" MACSTR " unknown-network",
2345 MAC2STR(sa), MAC2STR(go_dev_addr),
2346 MAC2STR(bssid));
2347 } else {
2348 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_INVITATION_RECEIVED
2349 "sa=" MACSTR " go_dev_addr=" MACSTR
2350 " unknown-network",
2351 MAC2STR(sa), MAC2STR(go_dev_addr));
2352 }
2353 return;
2354 }
2355
2356 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_INVITATION_RECEIVED "sa=" MACSTR
2357 " persistent=%d", MAC2STR(sa), s->id);
2358}
2359
2360
2361static void wpas_invitation_result(void *ctx, int status, const u8 *bssid)
2362{
2363 struct wpa_supplicant *wpa_s = ctx;
2364 struct wpa_ssid *ssid;
2365
2366 if (bssid) {
2367 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_INVITATION_RESULT
2368 "status=%d " MACSTR,
2369 status, MAC2STR(bssid));
2370 } else {
2371 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_INVITATION_RESULT
2372 "status=%d ", status);
2373 }
2374 wpas_notify_p2p_invitation_result(wpa_s, status, bssid);
2375
2376 if (wpa_s->pending_invite_ssid_id == -1)
2377 return; /* Invitation to active group */
2378
2379 if (status != P2P_SC_SUCCESS) {
2380 wpas_p2p_remove_pending_group_interface(wpa_s);
2381 return;
2382 }
2383
2384 ssid = wpa_config_get_network(wpa_s->conf,
2385 wpa_s->pending_invite_ssid_id);
2386 if (ssid == NULL) {
2387 wpa_printf(MSG_ERROR, "P2P: Could not find persistent group "
2388 "data matching with invitation");
2389 return;
2390 }
2391
2392 wpas_p2p_group_add_persistent(wpa_s, ssid,
Jouni Malinen31be0a42012-08-31 21:20:51 +03002393 ssid->mode == WPAS_MODE_P2P_GO,
2394 wpa_s->p2p_persistent_go_freq,
2395 wpa_s->p2p_go_ht40);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002396}
2397
2398
Dmitry Shmidt04949592012-07-19 12:16:46 -07002399static int wpas_p2p_disallowed_freq(struct wpa_global *global,
2400 unsigned int freq)
2401{
2402 unsigned int i;
2403
2404 if (global->p2p_disallow_freq == NULL)
2405 return 0;
2406
2407 for (i = 0; i < global->num_p2p_disallow_freq; i++) {
2408 if (freq >= global->p2p_disallow_freq[i].min &&
2409 freq <= global->p2p_disallow_freq[i].max)
2410 return 1;
2411 }
2412
2413 return 0;
2414}
2415
2416
2417static void wpas_p2p_add_chan(struct p2p_reg_class *reg, u8 chan)
2418{
2419 reg->channel[reg->channels] = chan;
2420 reg->channels++;
2421}
2422
2423
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002424static int wpas_p2p_default_channels(struct wpa_supplicant *wpa_s,
2425 struct p2p_channels *chan)
2426{
2427 int i, cla = 0;
2428
2429 wpa_printf(MSG_DEBUG, "P2P: Enable operating classes for 2.4 GHz "
2430 "band");
2431
2432 /* Operating class 81 - 2.4 GHz band channels 1..13 */
2433 chan->reg_class[cla].reg_class = 81;
Dmitry Shmidt04949592012-07-19 12:16:46 -07002434 chan->reg_class[cla].channels = 0;
2435 for (i = 0; i < 11; i++) {
2436 if (!wpas_p2p_disallowed_freq(wpa_s->global, 2412 + i * 5))
2437 wpas_p2p_add_chan(&chan->reg_class[cla], i + 1);
2438 }
2439 if (chan->reg_class[cla].channels)
2440 cla++;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002441
2442 wpa_printf(MSG_DEBUG, "P2P: Enable operating classes for lower 5 GHz "
2443 "band");
2444
2445 /* Operating class 115 - 5 GHz, channels 36-48 */
2446 chan->reg_class[cla].reg_class = 115;
Dmitry Shmidt04949592012-07-19 12:16:46 -07002447 chan->reg_class[cla].channels = 0;
2448 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 36 * 5))
2449 wpas_p2p_add_chan(&chan->reg_class[cla], 36);
2450 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 40 * 5))
2451 wpas_p2p_add_chan(&chan->reg_class[cla], 40);
2452 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 44 * 5))
2453 wpas_p2p_add_chan(&chan->reg_class[cla], 44);
2454 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 48 * 5))
2455 wpas_p2p_add_chan(&chan->reg_class[cla], 48);
2456 if (chan->reg_class[cla].channels)
2457 cla++;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002458
2459 wpa_printf(MSG_DEBUG, "P2P: Enable operating classes for higher 5 GHz "
2460 "band");
2461
2462 /* Operating class 124 - 5 GHz, channels 149,153,157,161 */
2463 chan->reg_class[cla].reg_class = 124;
Dmitry Shmidt04949592012-07-19 12:16:46 -07002464 chan->reg_class[cla].channels = 0;
2465 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 149 * 5))
2466 wpas_p2p_add_chan(&chan->reg_class[cla], 149);
2467 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 153 * 5))
2468 wpas_p2p_add_chan(&chan->reg_class[cla], 153);
2469 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 156 * 5))
2470 wpas_p2p_add_chan(&chan->reg_class[cla], 157);
2471 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 161 * 5))
2472 wpas_p2p_add_chan(&chan->reg_class[cla], 161);
2473 if (chan->reg_class[cla].channels)
2474 cla++;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002475
2476 chan->reg_classes = cla;
2477 return 0;
2478}
2479
2480
2481static struct hostapd_hw_modes * get_mode(struct hostapd_hw_modes *modes,
2482 u16 num_modes,
2483 enum hostapd_hw_mode mode)
2484{
2485 u16 i;
2486
2487 for (i = 0; i < num_modes; i++) {
2488 if (modes[i].mode == mode)
2489 return &modes[i];
2490 }
2491
2492 return NULL;
2493}
2494
2495
Dmitry Shmidt04949592012-07-19 12:16:46 -07002496static int has_channel(struct wpa_global *global,
2497 struct hostapd_hw_modes *mode, u8 chan, int *flags)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002498{
2499 int i;
Dmitry Shmidt04949592012-07-19 12:16:46 -07002500 unsigned int freq;
2501
2502 freq = (mode->mode == HOSTAPD_MODE_IEEE80211A ? 5000 : 2407) +
2503 chan * 5;
2504 if (wpas_p2p_disallowed_freq(global, freq))
2505 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002506
2507 for (i = 0; i < mode->num_channels; i++) {
2508 if (mode->channels[i].chan == chan) {
2509 if (flags)
2510 *flags = mode->channels[i].flag;
2511 return !(mode->channels[i].flag &
2512 (HOSTAPD_CHAN_DISABLED |
2513 HOSTAPD_CHAN_PASSIVE_SCAN |
2514 HOSTAPD_CHAN_NO_IBSS |
2515 HOSTAPD_CHAN_RADAR));
2516 }
2517 }
2518
2519 return 0;
2520}
2521
2522
2523struct p2p_oper_class_map {
2524 enum hostapd_hw_mode mode;
2525 u8 op_class;
2526 u8 min_chan;
2527 u8 max_chan;
2528 u8 inc;
2529 enum { BW20, BW40PLUS, BW40MINUS } bw;
2530};
2531
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002532static struct p2p_oper_class_map op_class[] = {
2533 { HOSTAPD_MODE_IEEE80211G, 81, 1, 13, 1, BW20 },
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002534#if 0 /* Do not enable HT40 on 2 GHz for now */
2535 { HOSTAPD_MODE_IEEE80211G, 83, 1, 9, 1, BW40PLUS },
2536 { HOSTAPD_MODE_IEEE80211G, 84, 5, 13, 1, BW40MINUS },
2537#endif
2538 { HOSTAPD_MODE_IEEE80211A, 115, 36, 48, 4, BW20 },
2539 { HOSTAPD_MODE_IEEE80211A, 124, 149, 161, 4, BW20 },
2540 { HOSTAPD_MODE_IEEE80211A, 116, 36, 44, 8, BW40PLUS },
2541 { HOSTAPD_MODE_IEEE80211A, 117, 40, 48, 8, BW40MINUS },
2542 { HOSTAPD_MODE_IEEE80211A, 126, 149, 157, 8, BW40PLUS },
2543 { HOSTAPD_MODE_IEEE80211A, 127, 153, 161, 8, BW40MINUS },
2544 { -1, 0, 0, 0, 0, BW20 }
2545};
2546
2547
2548static int wpas_p2p_verify_channel(struct wpa_supplicant *wpa_s,
2549 struct hostapd_hw_modes *mode,
2550 u8 channel, u8 bw)
2551{
2552 int flag;
2553
2554 if (!has_channel(wpa_s->global, mode, channel, &flag))
2555 return -1;
2556 if (bw == BW40MINUS &&
2557 (!(flag & HOSTAPD_CHAN_HT40MINUS) ||
2558 !has_channel(wpa_s->global, mode, channel - 4, NULL)))
2559 return 0;
2560 if (bw == BW40PLUS &&
2561 (!(flag & HOSTAPD_CHAN_HT40PLUS) ||
2562 !has_channel(wpa_s->global, mode, channel + 4, NULL)))
2563 return 0;
2564 return 1;
2565}
2566
2567
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002568static int wpas_p2p_setup_channels(struct wpa_supplicant *wpa_s,
2569 struct p2p_channels *chan)
2570{
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002571 struct hostapd_hw_modes *mode;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002572 int cla, op;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002573
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002574 if (wpa_s->hw.modes == NULL) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002575 wpa_printf(MSG_DEBUG, "P2P: Driver did not support fetching "
2576 "of all supported channels; assume dualband "
2577 "support");
2578 return wpas_p2p_default_channels(wpa_s, chan);
2579 }
2580
2581 cla = 0;
2582
2583 for (op = 0; op_class[op].op_class; op++) {
2584 struct p2p_oper_class_map *o = &op_class[op];
2585 u8 ch;
2586 struct p2p_reg_class *reg = NULL;
2587
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002588 mode = get_mode(wpa_s->hw.modes, wpa_s->hw.num_modes, o->mode);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002589 if (mode == NULL)
2590 continue;
2591 for (ch = o->min_chan; ch <= o->max_chan; ch += o->inc) {
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002592 if (wpas_p2p_verify_channel(wpa_s, mode, ch, o->bw) < 1)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002593 continue;
2594 if (reg == NULL) {
2595 wpa_printf(MSG_DEBUG, "P2P: Add operating "
2596 "class %u", o->op_class);
2597 reg = &chan->reg_class[cla];
2598 cla++;
2599 reg->reg_class = o->op_class;
2600 }
2601 reg->channel[reg->channels] = ch;
2602 reg->channels++;
2603 }
2604 if (reg) {
2605 wpa_hexdump(MSG_DEBUG, "P2P: Channels",
2606 reg->channel, reg->channels);
2607 }
2608 }
2609
2610 chan->reg_classes = cla;
2611
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002612 return 0;
2613}
2614
2615
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002616int wpas_p2p_get_ht40_mode(struct wpa_supplicant *wpa_s,
2617 struct hostapd_hw_modes *mode, u8 channel)
2618{
2619 int op, ret;
2620
2621 for (op = 0; op_class[op].op_class; op++) {
2622 struct p2p_oper_class_map *o = &op_class[op];
2623 u8 ch;
2624
2625 for (ch = o->min_chan; ch <= o->max_chan; ch += o->inc) {
2626 if (o->mode != HOSTAPD_MODE_IEEE80211A ||
2627 o->bw == BW20 || ch != channel)
2628 continue;
2629 ret = wpas_p2p_verify_channel(wpa_s, mode, ch, o->bw);
2630 if (ret < 0)
2631 continue;
2632 else if (ret > 0)
2633 return (o->bw == BW40MINUS) ? -1 : 1;
2634 else
2635 return 0;
2636 }
2637 }
2638 return 0;
2639}
2640
2641
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002642static int wpas_get_noa(void *ctx, const u8 *interface_addr, u8 *buf,
2643 size_t buf_len)
2644{
2645 struct wpa_supplicant *wpa_s = ctx;
2646
2647 for (wpa_s = wpa_s->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
2648 if (os_memcmp(wpa_s->own_addr, interface_addr, ETH_ALEN) == 0)
2649 break;
2650 }
2651 if (wpa_s == NULL)
2652 return -1;
2653
2654 return wpa_drv_get_noa(wpa_s, buf, buf_len);
2655}
2656
2657
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08002658static int wpas_go_connected(void *ctx, const u8 *dev_addr)
2659{
2660 struct wpa_supplicant *wpa_s = ctx;
2661
2662 for (wpa_s = wpa_s->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
2663 struct wpa_ssid *ssid = wpa_s->current_ssid;
2664 if (ssid == NULL)
2665 continue;
2666 if (ssid->mode != WPAS_MODE_INFRA)
2667 continue;
2668 if (wpa_s->wpa_state != WPA_COMPLETED &&
2669 wpa_s->wpa_state != WPA_GROUP_HANDSHAKE)
2670 continue;
2671 if (os_memcmp(wpa_s->go_dev_addr, dev_addr, ETH_ALEN) == 0)
2672 return 1;
2673 }
2674
2675 return 0;
2676}
2677
2678
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002679/**
2680 * wpas_p2p_init - Initialize P2P module for %wpa_supplicant
2681 * @global: Pointer to global data from wpa_supplicant_init()
2682 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
2683 * Returns: 0 on success, -1 on failure
2684 */
2685int wpas_p2p_init(struct wpa_global *global, struct wpa_supplicant *wpa_s)
2686{
2687 struct p2p_config p2p;
2688 unsigned int r;
2689 int i;
2690
2691 if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_CAPABLE))
2692 return 0;
2693
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002694 if (global->p2p)
2695 return 0;
2696
2697 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT) {
2698 struct p2p_params params;
2699
2700 wpa_printf(MSG_DEBUG, "P2P: Use driver-based P2P management");
2701 os_memset(&params, 0, sizeof(params));
2702 params.dev_name = wpa_s->conf->device_name;
2703 os_memcpy(params.pri_dev_type, wpa_s->conf->device_type,
2704 WPS_DEV_TYPE_LEN);
2705 params.num_sec_dev_types = wpa_s->conf->num_sec_device_types;
2706 os_memcpy(params.sec_dev_type,
2707 wpa_s->conf->sec_device_type,
2708 params.num_sec_dev_types * WPS_DEV_TYPE_LEN);
2709
2710 if (wpa_drv_p2p_set_params(wpa_s, &params) < 0)
2711 return -1;
2712
2713 return 0;
2714 }
2715
2716 os_memset(&p2p, 0, sizeof(p2p));
2717 p2p.msg_ctx = wpa_s;
2718 p2p.cb_ctx = wpa_s;
2719 p2p.p2p_scan = wpas_p2p_scan;
2720 p2p.send_action = wpas_send_action;
2721 p2p.send_action_done = wpas_send_action_done;
2722 p2p.go_neg_completed = wpas_go_neg_completed;
2723 p2p.go_neg_req_rx = wpas_go_neg_req_rx;
2724 p2p.dev_found = wpas_dev_found;
2725 p2p.dev_lost = wpas_dev_lost;
2726 p2p.start_listen = wpas_start_listen;
2727 p2p.stop_listen = wpas_stop_listen;
2728 p2p.send_probe_resp = wpas_send_probe_resp;
2729 p2p.sd_request = wpas_sd_request;
2730 p2p.sd_response = wpas_sd_response;
2731 p2p.prov_disc_req = wpas_prov_disc_req;
2732 p2p.prov_disc_resp = wpas_prov_disc_resp;
Jouni Malinen75ecf522011-06-27 15:19:46 -07002733 p2p.prov_disc_fail = wpas_prov_disc_fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002734 p2p.invitation_process = wpas_invitation_process;
2735 p2p.invitation_received = wpas_invitation_received;
2736 p2p.invitation_result = wpas_invitation_result;
2737 p2p.get_noa = wpas_get_noa;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08002738 p2p.go_connected = wpas_go_connected;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002739
2740 os_memcpy(wpa_s->global->p2p_dev_addr, wpa_s->own_addr, ETH_ALEN);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002741 os_memcpy(p2p.dev_addr, wpa_s->global->p2p_dev_addr, ETH_ALEN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002742 p2p.dev_name = wpa_s->conf->device_name;
2743 p2p.manufacturer = wpa_s->conf->manufacturer;
2744 p2p.model_name = wpa_s->conf->model_name;
2745 p2p.model_number = wpa_s->conf->model_number;
2746 p2p.serial_number = wpa_s->conf->serial_number;
2747 if (wpa_s->wps) {
2748 os_memcpy(p2p.uuid, wpa_s->wps->uuid, 16);
2749 p2p.config_methods = wpa_s->wps->config_methods;
2750 }
2751
2752 if (wpa_s->conf->p2p_listen_reg_class &&
2753 wpa_s->conf->p2p_listen_channel) {
2754 p2p.reg_class = wpa_s->conf->p2p_listen_reg_class;
2755 p2p.channel = wpa_s->conf->p2p_listen_channel;
2756 } else {
2757 p2p.reg_class = 81;
2758 /*
2759 * Pick one of the social channels randomly as the listen
2760 * channel.
2761 */
2762 os_get_random((u8 *) &r, sizeof(r));
2763 p2p.channel = 1 + (r % 3) * 5;
2764 }
2765 wpa_printf(MSG_DEBUG, "P2P: Own listen channel: %d", p2p.channel);
2766
2767 if (wpa_s->conf->p2p_oper_reg_class &&
2768 wpa_s->conf->p2p_oper_channel) {
2769 p2p.op_reg_class = wpa_s->conf->p2p_oper_reg_class;
2770 p2p.op_channel = wpa_s->conf->p2p_oper_channel;
2771 p2p.cfg_op_channel = 1;
2772 wpa_printf(MSG_DEBUG, "P2P: Configured operating channel: "
2773 "%d:%d", p2p.op_reg_class, p2p.op_channel);
2774
2775 } else {
2776 p2p.op_reg_class = 81;
2777 /*
2778 * Use random operation channel from (1, 6, 11) if no other
2779 * preference is indicated.
2780 */
2781 os_get_random((u8 *) &r, sizeof(r));
2782 p2p.op_channel = 1 + (r % 3) * 5;
2783 p2p.cfg_op_channel = 0;
2784 wpa_printf(MSG_DEBUG, "P2P: Random operating channel: "
2785 "%d:%d", p2p.op_reg_class, p2p.op_channel);
2786 }
2787 if (wpa_s->conf->country[0] && wpa_s->conf->country[1]) {
2788 os_memcpy(p2p.country, wpa_s->conf->country, 2);
2789 p2p.country[2] = 0x04;
2790 } else
2791 os_memcpy(p2p.country, "XX\x04", 3);
2792
2793 if (wpas_p2p_setup_channels(wpa_s, &p2p.channels)) {
2794 wpa_printf(MSG_ERROR, "P2P: Failed to configure supported "
2795 "channel list");
2796 return -1;
2797 }
2798
2799 os_memcpy(p2p.pri_dev_type, wpa_s->conf->device_type,
2800 WPS_DEV_TYPE_LEN);
2801
2802 p2p.num_sec_dev_types = wpa_s->conf->num_sec_device_types;
2803 os_memcpy(p2p.sec_dev_type, wpa_s->conf->sec_device_type,
2804 p2p.num_sec_dev_types * WPS_DEV_TYPE_LEN);
2805
2806 p2p.concurrent_operations = !!(wpa_s->drv_flags &
2807 WPA_DRIVER_FLAGS_P2P_CONCURRENT);
2808
2809 p2p.max_peers = 100;
2810
2811 if (wpa_s->conf->p2p_ssid_postfix) {
2812 p2p.ssid_postfix_len =
2813 os_strlen(wpa_s->conf->p2p_ssid_postfix);
2814 if (p2p.ssid_postfix_len > sizeof(p2p.ssid_postfix))
2815 p2p.ssid_postfix_len = sizeof(p2p.ssid_postfix);
2816 os_memcpy(p2p.ssid_postfix, wpa_s->conf->p2p_ssid_postfix,
2817 p2p.ssid_postfix_len);
2818 }
2819
2820 p2p.p2p_intra_bss = wpa_s->conf->p2p_intra_bss;
2821
Dmitry Shmidt04abaa92012-08-17 16:22:31 -07002822#ifdef ANDROID_P2P
Irfan Sheriffaf84a572012-09-22 16:59:30 -07002823 if(wpa_s->drv_flags & WPA_DRIVER_FLAGS_MULTI_CHANNEL_CONCURRENT) {
Dmitry Shmidt04abaa92012-08-17 16:22:31 -07002824 p2p.p2p_concurrency = P2P_MULTI_CHANNEL_CONCURRENT;
Irfan Sheriffaf84a572012-09-22 16:59:30 -07002825 wpa_printf(MSG_DEBUG, "P2P: Multi channel concurrency support");
2826 } else {
2827 // Add support for WPA_DRIVER_FLAGS_P2P_CONCURRENT
Dmitry Shmidt04abaa92012-08-17 16:22:31 -07002828 p2p.p2p_concurrency = P2P_SINGLE_CHANNEL_CONCURRENT;
Irfan Sheriffaf84a572012-09-22 16:59:30 -07002829 wpa_printf(MSG_DEBUG, "P2P: Single channel concurrency support");
2830 }
Dmitry Shmidt04abaa92012-08-17 16:22:31 -07002831#endif
2832
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002833 global->p2p = p2p_init(&p2p);
2834 if (global->p2p == NULL)
2835 return -1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07002836 global->p2p_init_wpa_s = wpa_s;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002837
2838 for (i = 0; i < MAX_WPS_VENDOR_EXT; i++) {
2839 if (wpa_s->conf->wps_vendor_ext[i] == NULL)
2840 continue;
2841 p2p_add_wps_vendor_extension(
2842 global->p2p, wpa_s->conf->wps_vendor_ext[i]);
2843 }
2844
2845 return 0;
2846}
2847
2848
2849/**
2850 * wpas_p2p_deinit - Deinitialize per-interface P2P data
2851 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
2852 *
2853 * This function deinitialize per-interface P2P data.
2854 */
2855void wpas_p2p_deinit(struct wpa_supplicant *wpa_s)
2856{
2857 if (wpa_s->driver && wpa_s->drv_priv)
2858 wpa_drv_probe_req_report(wpa_s, 0);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002859
2860 if (wpa_s->go_params) {
2861 /* Clear any stored provisioning info */
2862 p2p_clear_provisioning_info(
2863 wpa_s->global->p2p,
Dmitry Shmidt04949592012-07-19 12:16:46 -07002864 wpa_s->go_params->peer_device_addr);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002865 }
2866
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002867 os_free(wpa_s->go_params);
2868 wpa_s->go_params = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002869 eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s, NULL);
2870 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08002871 eloop_cancel_timeout(wpas_p2p_pd_before_join_timeout, wpa_s, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002872 wpa_s->p2p_long_listen = 0;
2873 eloop_cancel_timeout(wpas_p2p_long_listen_timeout, wpa_s, NULL);
2874 eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL);
2875 wpas_p2p_remove_pending_group_interface(wpa_s);
2876
2877 /* TODO: remove group interface from the driver if this wpa_s instance
2878 * is on top of a P2P group interface */
2879}
2880
2881
2882/**
2883 * wpas_p2p_deinit_global - Deinitialize global P2P module
2884 * @global: Pointer to global data from wpa_supplicant_init()
2885 *
2886 * This function deinitializes the global (per device) P2P module.
2887 */
2888void wpas_p2p_deinit_global(struct wpa_global *global)
2889{
2890 struct wpa_supplicant *wpa_s, *tmp;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002891
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002892 wpa_s = global->ifaces;
2893 if (wpa_s)
2894 wpas_p2p_service_flush(wpa_s);
2895
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002896 if (global->p2p == NULL)
2897 return;
2898
2899 /* Remove remaining P2P group interfaces */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002900 while (wpa_s && wpa_s->p2p_group_interface != NOT_P2P_GROUP_INTERFACE)
2901 wpa_s = wpa_s->next;
2902 while (wpa_s) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002903 tmp = global->ifaces;
2904 while (tmp &&
2905 (tmp == wpa_s ||
2906 tmp->p2p_group_interface == NOT_P2P_GROUP_INTERFACE)) {
2907 tmp = tmp->next;
2908 }
2909 if (tmp == NULL)
2910 break;
Dmitry Shmidt04949592012-07-19 12:16:46 -07002911 /* Disconnect from the P2P group and deinit the interface */
2912 wpas_p2p_disconnect(tmp);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002913 }
2914
2915 /*
2916 * Deinit GO data on any possibly remaining interface (if main
2917 * interface is used as GO).
2918 */
2919 for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
2920 if (wpa_s->ap_iface)
2921 wpas_p2p_group_deinit(wpa_s);
2922 }
2923
2924 p2p_deinit(global->p2p);
2925 global->p2p = NULL;
Dmitry Shmidt04949592012-07-19 12:16:46 -07002926 global->p2p_init_wpa_s = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002927}
2928
2929
2930static int wpas_p2p_create_iface(struct wpa_supplicant *wpa_s)
2931{
2932 if (wpa_s->drv_flags &
2933 (WPA_DRIVER_FLAGS_P2P_DEDICATED_INTERFACE |
2934 WPA_DRIVER_FLAGS_P2P_MGMT_AND_NON_P2P))
2935 return 1; /* P2P group requires a new interface in every case
2936 */
2937 if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_CONCURRENT))
2938 return 0; /* driver does not support concurrent operations */
2939 if (wpa_s->global->ifaces->next)
2940 return 1; /* more that one interface already in use */
2941 if (wpa_s->wpa_state >= WPA_AUTHENTICATING)
2942 return 1; /* this interface is already in use */
2943 return 0;
2944}
2945
2946
2947static int wpas_p2p_start_go_neg(struct wpa_supplicant *wpa_s,
2948 const u8 *peer_addr,
2949 enum p2p_wps_method wps_method,
2950 int go_intent, const u8 *own_interface_addr,
Dmitry Shmidt04949592012-07-19 12:16:46 -07002951 unsigned int force_freq, int persistent_group,
2952 struct wpa_ssid *ssid)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002953{
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002954 if (persistent_group && wpa_s->conf->persistent_reconnect)
2955 persistent_group = 2;
2956
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002957 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT) {
2958 return wpa_drv_p2p_connect(wpa_s, peer_addr, wps_method,
2959 go_intent, own_interface_addr,
2960 force_freq, persistent_group);
2961 }
2962
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002963 /*
2964 * Increase GO config timeout if HT40 is used since it takes some time
2965 * to scan channels for coex purposes before the BSS can be started.
2966 */
2967 p2p_set_config_timeout(wpa_s->global->p2p,
2968 wpa_s->p2p_go_ht40 ? 255 : 100, 20);
2969
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002970 return p2p_connect(wpa_s->global->p2p, peer_addr, wps_method,
2971 go_intent, own_interface_addr, force_freq,
Dmitry Shmidt04949592012-07-19 12:16:46 -07002972 persistent_group, ssid ? ssid->ssid : NULL,
2973 ssid ? ssid->ssid_len : 0,
2974 wpa_s->p2p_pd_before_go_neg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002975}
2976
2977
2978static int wpas_p2p_auth_go_neg(struct wpa_supplicant *wpa_s,
2979 const u8 *peer_addr,
2980 enum p2p_wps_method wps_method,
2981 int go_intent, const u8 *own_interface_addr,
Dmitry Shmidt04949592012-07-19 12:16:46 -07002982 unsigned int force_freq, int persistent_group,
2983 struct wpa_ssid *ssid)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002984{
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002985 if (persistent_group && wpa_s->conf->persistent_reconnect)
2986 persistent_group = 2;
2987
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002988 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
2989 return -1;
2990
2991 return p2p_authorize(wpa_s->global->p2p, peer_addr, wps_method,
2992 go_intent, own_interface_addr, force_freq,
Dmitry Shmidt04949592012-07-19 12:16:46 -07002993 persistent_group, ssid ? ssid->ssid : NULL,
2994 ssid ? ssid->ssid_len : 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002995}
2996
2997
2998static void wpas_p2p_check_join_scan_limit(struct wpa_supplicant *wpa_s)
2999{
3000 wpa_s->p2p_join_scan_count++;
3001 wpa_printf(MSG_DEBUG, "P2P: Join scan attempt %d",
3002 wpa_s->p2p_join_scan_count);
3003 if (wpa_s->p2p_join_scan_count > P2P_MAX_JOIN_SCAN_ATTEMPTS) {
3004 wpa_printf(MSG_DEBUG, "P2P: Failed to find GO " MACSTR
3005 " for join operationg - stop join attempt",
3006 MAC2STR(wpa_s->pending_join_iface_addr));
3007 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
Dmitry Shmidt04949592012-07-19 12:16:46 -07003008 if (wpa_s->p2p_auto_pd) {
3009 wpa_s->p2p_auto_pd = 0;
3010 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_FAILURE
3011 " p2p_dev_addr=" MACSTR " status=N/A",
3012 MAC2STR(wpa_s->pending_join_dev_addr));
3013 return;
3014 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003015 wpa_msg(wpa_s->parent, MSG_INFO,
3016 P2P_EVENT_GROUP_FORMATION_FAILURE);
3017 }
3018}
3019
3020
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08003021static void wpas_p2p_pd_before_join_timeout(void *eloop_ctx, void *timeout_ctx)
3022{
3023 struct wpa_supplicant *wpa_s = eloop_ctx;
3024 if (!wpa_s->pending_pd_before_join)
3025 return;
3026 /*
3027 * Provision Discovery Response may have been lost - try to connect
3028 * anyway since we do not need any information from this PD.
3029 */
3030 wpa_printf(MSG_DEBUG, "P2P: PD timeout for join-existing-group - "
3031 "try to connect anyway");
3032 wpas_p2p_join_start(wpa_s);
3033}
3034
3035
Dmitry Shmidt04949592012-07-19 12:16:46 -07003036static int wpas_check_freq_conflict(struct wpa_supplicant *wpa_s, int freq)
3037{
3038 struct wpa_supplicant *iface;
3039 int shared_freq;
3040 u8 bssid[ETH_ALEN];
3041
3042 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_MULTI_CHANNEL_CONCURRENT)
3043 return 0;
3044
3045 for (iface = wpa_s->global->ifaces; iface; iface = iface->next) {
3046 if (!wpas_p2p_create_iface(wpa_s) && iface == wpa_s)
3047 continue;
3048 if (iface->current_ssid == NULL || iface->assoc_freq == 0)
3049 continue;
3050 if (iface->current_ssid->mode == WPAS_MODE_AP ||
3051 iface->current_ssid->mode == WPAS_MODE_P2P_GO)
3052 shared_freq = iface->current_ssid->frequency;
3053 else if (wpa_drv_get_bssid(iface, bssid) == 0)
3054 shared_freq = iface->assoc_freq;
3055 else
3056 shared_freq = 0;
3057
3058 if (shared_freq && freq != shared_freq) {
3059 wpa_printf(MSG_DEBUG, "P2P: Frequency conflict - %s "
3060 "connected on %d MHz - new connection on "
3061 "%d MHz", iface->ifname, shared_freq, freq);
3062 return 1;
3063 }
3064 }
3065
3066 shared_freq = wpa_drv_shared_freq(wpa_s);
3067 if (shared_freq > 0 && shared_freq != freq) {
3068 wpa_printf(MSG_DEBUG, "P2P: Frequency conflict - shared "
3069 "virtual interface connected on %d MHz - new "
3070 "connection on %d MHz", shared_freq, freq);
3071 return 1;
3072 }
3073
3074 return 0;
3075}
3076
3077
3078static int wpas_p2p_peer_go(struct wpa_supplicant *wpa_s,
3079 const u8 *peer_dev_addr)
3080{
3081 struct wpa_bss *bss;
3082 int updated;
3083
3084 bss = wpa_bss_get_p2p_dev_addr(wpa_s, peer_dev_addr);
3085 if (bss == NULL)
3086 return -1;
3087 if (bss->last_update_idx < wpa_s->bss_update_idx) {
3088 wpa_printf(MSG_DEBUG, "P2P: Peer BSS entry not updated in the "
3089 "last scan");
3090 return 0;
3091 }
3092
3093 updated = os_time_before(&wpa_s->p2p_auto_started, &bss->last_update);
3094 wpa_printf(MSG_DEBUG, "P2P: Current BSS entry for peer updated at "
3095 "%ld.%06ld (%supdated in last scan)",
3096 bss->last_update.sec, bss->last_update.usec,
3097 updated ? "": "not ");
3098
3099 return updated;
3100}
3101
3102
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003103static void wpas_p2p_scan_res_join(struct wpa_supplicant *wpa_s,
3104 struct wpa_scan_results *scan_res)
3105{
3106 struct wpa_bss *bss;
3107 int freq;
3108 u8 iface_addr[ETH_ALEN];
Dmitry Shmidt04949592012-07-19 12:16:46 -07003109
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003110 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
3111
3112 if (wpa_s->global->p2p_disabled)
3113 return;
3114
Dmitry Shmidt04949592012-07-19 12:16:46 -07003115 wpa_printf(MSG_DEBUG, "P2P: Scan results received (%d BSS) for %sjoin",
3116 scan_res ? (int) scan_res->num : -1,
3117 wpa_s->p2p_auto_join ? "auto_" : "");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003118
3119 if (scan_res)
3120 wpas_p2p_scan_res_handler(wpa_s, scan_res);
3121
Dmitry Shmidt04949592012-07-19 12:16:46 -07003122 if (wpa_s->p2p_auto_pd) {
3123 int join = wpas_p2p_peer_go(wpa_s,
3124 wpa_s->pending_join_dev_addr);
3125 if (join == 0 &&
3126 wpa_s->auto_pd_scan_retry < P2P_AUTO_PD_SCAN_ATTEMPTS) {
3127 wpa_s->auto_pd_scan_retry++;
3128 bss = wpa_bss_get_bssid(wpa_s,
3129 wpa_s->pending_join_dev_addr);
3130 if (bss) {
3131 freq = bss->freq;
3132 wpa_printf(MSG_DEBUG, "P2P: Scan retry %d for "
3133 "the peer " MACSTR " at %d MHz",
3134 wpa_s->auto_pd_scan_retry,
3135 MAC2STR(wpa_s->
3136 pending_join_dev_addr),
3137 freq);
3138 wpas_p2p_join_scan_req(wpa_s, freq);
3139 return;
3140 }
3141 }
3142
3143 if (join < 0)
3144 join = 0;
3145
3146 wpa_s->p2p_auto_pd = 0;
3147 wpa_s->pending_pd_use = join ? AUTO_PD_JOIN : AUTO_PD_GO_NEG;
3148 wpa_printf(MSG_DEBUG, "P2P: Auto PD with " MACSTR " join=%d",
3149 MAC2STR(wpa_s->pending_join_dev_addr), join);
3150 if (p2p_prov_disc_req(wpa_s->global->p2p,
3151 wpa_s->pending_join_dev_addr,
3152 wpa_s->pending_pd_config_methods, join,
3153 0) < 0) {
3154 wpa_s->p2p_auto_pd = 0;
3155 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_FAILURE
3156 " p2p_dev_addr=" MACSTR " status=N/A",
3157 MAC2STR(wpa_s->pending_join_dev_addr));
3158 }
3159 return;
3160 }
3161
3162 if (wpa_s->p2p_auto_join) {
3163 int join = wpas_p2p_peer_go(wpa_s,
3164 wpa_s->pending_join_dev_addr);
3165 if (join < 0) {
3166 wpa_printf(MSG_DEBUG, "P2P: Peer was not found to be "
3167 "running a GO -> use GO Negotiation");
3168 wpas_p2p_connect(wpa_s, wpa_s->pending_join_dev_addr,
3169 wpa_s->p2p_pin, wpa_s->p2p_wps_method,
3170 wpa_s->p2p_persistent_group, 0, 0, 0,
3171 wpa_s->p2p_go_intent,
3172 wpa_s->p2p_connect_freq,
3173 wpa_s->p2p_persistent_id,
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003174 wpa_s->p2p_pd_before_go_neg,
3175 wpa_s->p2p_go_ht40);
Dmitry Shmidt04949592012-07-19 12:16:46 -07003176 return;
3177 }
3178
3179 wpa_printf(MSG_DEBUG, "P2P: Peer was found running GO%s -> "
3180 "try to join the group", join ? "" :
3181 " in older scan");
3182 if (!join)
3183 wpa_s->p2p_fallback_to_go_neg = 1;
3184 }
3185
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003186 freq = p2p_get_oper_freq(wpa_s->global->p2p,
3187 wpa_s->pending_join_iface_addr);
3188 if (freq < 0 &&
3189 p2p_get_interface_addr(wpa_s->global->p2p,
3190 wpa_s->pending_join_dev_addr,
3191 iface_addr) == 0 &&
3192 os_memcmp(iface_addr, wpa_s->pending_join_dev_addr, ETH_ALEN) != 0)
3193 {
3194 wpa_printf(MSG_DEBUG, "P2P: Overwrite pending interface "
3195 "address for join from " MACSTR " to " MACSTR
3196 " based on newly discovered P2P peer entry",
3197 MAC2STR(wpa_s->pending_join_iface_addr),
3198 MAC2STR(iface_addr));
3199 os_memcpy(wpa_s->pending_join_iface_addr, iface_addr,
3200 ETH_ALEN);
3201
3202 freq = p2p_get_oper_freq(wpa_s->global->p2p,
3203 wpa_s->pending_join_iface_addr);
3204 }
3205 if (freq >= 0) {
3206 wpa_printf(MSG_DEBUG, "P2P: Target GO operating frequency "
3207 "from P2P peer table: %d MHz", freq);
3208 }
3209 bss = wpa_bss_get_bssid(wpa_s, wpa_s->pending_join_iface_addr);
3210 if (bss) {
3211 freq = bss->freq;
3212 wpa_printf(MSG_DEBUG, "P2P: Target GO operating frequency "
3213 "from BSS table: %d MHz", freq);
3214 }
3215 if (freq > 0) {
3216 u16 method;
3217
Dmitry Shmidt04949592012-07-19 12:16:46 -07003218 if (wpas_check_freq_conflict(wpa_s, freq) > 0) {
3219 wpa_msg(wpa_s->parent, MSG_INFO,
3220 P2P_EVENT_GROUP_FORMATION_FAILURE
3221 "reason=FREQ_CONFLICT");
3222 return;
3223 }
3224
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003225 wpa_printf(MSG_DEBUG, "P2P: Send Provision Discovery Request "
3226 "prior to joining an existing group (GO " MACSTR
3227 " freq=%u MHz)",
3228 MAC2STR(wpa_s->pending_join_dev_addr), freq);
3229 wpa_s->pending_pd_before_join = 1;
3230
3231 switch (wpa_s->pending_join_wps_method) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003232 case WPS_PIN_DISPLAY:
3233 method = WPS_CONFIG_KEYPAD;
3234 break;
3235 case WPS_PIN_KEYPAD:
3236 method = WPS_CONFIG_DISPLAY;
3237 break;
3238 case WPS_PBC:
3239 method = WPS_CONFIG_PUSHBUTTON;
3240 break;
3241 default:
3242 method = 0;
3243 break;
3244 }
3245
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003246 if ((p2p_get_provisioning_info(wpa_s->global->p2p,
3247 wpa_s->pending_join_dev_addr) ==
3248 method)) {
3249 /*
3250 * We have already performed provision discovery for
3251 * joining the group. Proceed directly to join
3252 * operation without duplicated provision discovery. */
3253 wpa_printf(MSG_DEBUG, "P2P: Provision discovery "
3254 "with " MACSTR " already done - proceed to "
3255 "join",
3256 MAC2STR(wpa_s->pending_join_dev_addr));
3257 wpa_s->pending_pd_before_join = 0;
3258 goto start;
3259 }
3260
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003261 if (p2p_prov_disc_req(wpa_s->global->p2p,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003262 wpa_s->pending_join_dev_addr, method, 1,
3263 freq) < 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003264 wpa_printf(MSG_DEBUG, "P2P: Failed to send Provision "
3265 "Discovery Request before joining an "
3266 "existing group");
3267 wpa_s->pending_pd_before_join = 0;
3268 goto start;
3269 }
3270
3271 /*
3272 * Actual join operation will be started from the Action frame
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08003273 * TX status callback (if no ACK is received) or when the
3274 * Provision Discovery Response is received. Use a short
3275 * timeout as a backup mechanism should the Provision Discovery
3276 * Response be lost for any reason.
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003277 */
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08003278 eloop_cancel_timeout(wpas_p2p_pd_before_join_timeout, wpa_s,
3279 NULL);
3280 eloop_register_timeout(2, 0, wpas_p2p_pd_before_join_timeout,
3281 wpa_s, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003282 return;
3283 }
3284
3285 wpa_printf(MSG_DEBUG, "P2P: Failed to find BSS/GO - try again later");
3286 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
3287 eloop_register_timeout(1, 0, wpas_p2p_join_scan, wpa_s, NULL);
3288 wpas_p2p_check_join_scan_limit(wpa_s);
3289 return;
3290
3291start:
3292 /* Start join operation immediately */
3293 wpas_p2p_join_start(wpa_s);
3294}
3295
3296
Dmitry Shmidt04949592012-07-19 12:16:46 -07003297static void wpas_p2p_join_scan_req(struct wpa_supplicant *wpa_s, int freq)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003298{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003299 int ret;
3300 struct wpa_driver_scan_params params;
3301 struct wpabuf *wps_ie, *ies;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003302 size_t ielen;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003303 int freqs[2] = { 0, 0 };
Dmitry Shmidtad266fb2012-08-24 17:03:35 -07003304#ifdef ANDROID_P2P
3305 int oper_freq;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003306
Dmitry Shmidtad266fb2012-08-24 17:03:35 -07003307 /* If freq is not provided, check the operating freq of the GO and do a
3308 * a directed scan to save time
3309 */
3310 if(!freq) {
3311 freq = (oper_freq = p2p_get_oper_freq(wpa_s->global->p2p,
3312 wpa_s->pending_join_iface_addr) == -1) ? 0 : oper_freq;
3313 }
3314#endif
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003315 os_memset(&params, 0, sizeof(params));
3316
3317 /* P2P Wildcard SSID */
3318 params.num_ssids = 1;
3319 params.ssids[0].ssid = (u8 *) P2P_WILDCARD_SSID;
3320 params.ssids[0].ssid_len = P2P_WILDCARD_SSID_LEN;
3321
3322 wpa_s->wps->dev.p2p = 1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003323 wps_ie = wps_build_probe_req_ie(DEV_PW_DEFAULT, &wpa_s->wps->dev,
3324 wpa_s->wps->uuid, WPS_REQ_ENROLLEE, 0,
3325 NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003326 if (wps_ie == NULL) {
3327 wpas_p2p_scan_res_join(wpa_s, NULL);
3328 return;
3329 }
3330
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003331 ielen = p2p_scan_ie_buf_len(wpa_s->global->p2p);
3332 ies = wpabuf_alloc(wpabuf_len(wps_ie) + ielen);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003333 if (ies == NULL) {
3334 wpabuf_free(wps_ie);
3335 wpas_p2p_scan_res_join(wpa_s, NULL);
3336 return;
3337 }
3338 wpabuf_put_buf(ies, wps_ie);
3339 wpabuf_free(wps_ie);
3340
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08003341 p2p_scan_ie(wpa_s->global->p2p, ies, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003342
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003343 params.p2p_probe = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003344 params.extra_ies = wpabuf_head(ies);
3345 params.extra_ies_len = wpabuf_len(ies);
Dmitry Shmidt04949592012-07-19 12:16:46 -07003346 if (freq > 0) {
3347 freqs[0] = freq;
3348 params.freqs = freqs;
3349 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003350
3351 /*
3352 * Run a scan to update BSS table and start Provision Discovery once
3353 * the new scan results become available.
3354 */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003355 ret = wpa_drv_scan(wpa_s, &params);
Dmitry Shmidt04949592012-07-19 12:16:46 -07003356 if (!ret)
3357 wpa_s->scan_res_handler = wpas_p2p_scan_res_join;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003358
3359 wpabuf_free(ies);
3360
3361 if (ret) {
3362 wpa_printf(MSG_DEBUG, "P2P: Failed to start scan for join - "
3363 "try again later");
3364 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
3365 eloop_register_timeout(1, 0, wpas_p2p_join_scan, wpa_s, NULL);
3366 wpas_p2p_check_join_scan_limit(wpa_s);
3367 }
3368}
3369
3370
Dmitry Shmidt04949592012-07-19 12:16:46 -07003371static void wpas_p2p_join_scan(void *eloop_ctx, void *timeout_ctx)
3372{
3373 struct wpa_supplicant *wpa_s = eloop_ctx;
3374 wpas_p2p_join_scan_req(wpa_s, 0);
3375}
3376
3377
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003378static int wpas_p2p_join(struct wpa_supplicant *wpa_s, const u8 *iface_addr,
Dmitry Shmidt04949592012-07-19 12:16:46 -07003379 const u8 *dev_addr, enum p2p_wps_method wps_method,
3380 int auto_join)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003381{
3382 wpa_printf(MSG_DEBUG, "P2P: Request to join existing group (iface "
Dmitry Shmidt04949592012-07-19 12:16:46 -07003383 MACSTR " dev " MACSTR ")%s",
3384 MAC2STR(iface_addr), MAC2STR(dev_addr),
3385 auto_join ? " (auto_join)" : "");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003386
Dmitry Shmidt04949592012-07-19 12:16:46 -07003387 wpa_s->p2p_auto_pd = 0;
3388 wpa_s->p2p_auto_join = !!auto_join;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003389 os_memcpy(wpa_s->pending_join_iface_addr, iface_addr, ETH_ALEN);
3390 os_memcpy(wpa_s->pending_join_dev_addr, dev_addr, ETH_ALEN);
3391 wpa_s->pending_join_wps_method = wps_method;
3392
3393 /* Make sure we are not running find during connection establishment */
3394 wpas_p2p_stop_find(wpa_s);
3395
3396 wpa_s->p2p_join_scan_count = 0;
3397 wpas_p2p_join_scan(wpa_s, NULL);
3398 return 0;
3399}
3400
3401
3402static int wpas_p2p_join_start(struct wpa_supplicant *wpa_s)
3403{
3404 struct wpa_supplicant *group;
3405 struct p2p_go_neg_results res;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003406 struct wpa_bss *bss;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003407
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08003408 eloop_cancel_timeout(wpas_p2p_pd_before_join_timeout, wpa_s, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003409 group = wpas_p2p_get_group_iface(wpa_s, 0, 0);
3410 if (group == NULL)
3411 return -1;
3412 if (group != wpa_s) {
3413 os_memcpy(group->p2p_pin, wpa_s->p2p_pin,
3414 sizeof(group->p2p_pin));
3415 group->p2p_wps_method = wpa_s->p2p_wps_method;
3416 }
3417
3418 group->p2p_in_provisioning = 1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003419 group->p2p_fallback_to_go_neg = wpa_s->p2p_fallback_to_go_neg;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003420
3421 os_memset(&res, 0, sizeof(res));
3422 os_memcpy(res.peer_interface_addr, wpa_s->pending_join_iface_addr,
3423 ETH_ALEN);
3424 res.wps_method = wpa_s->pending_join_wps_method;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003425 bss = wpa_bss_get_bssid(wpa_s, wpa_s->pending_join_iface_addr);
3426 if (bss) {
3427 res.freq = bss->freq;
3428 res.ssid_len = bss->ssid_len;
3429 os_memcpy(res.ssid, bss->ssid, bss->ssid_len);
3430 }
3431
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08003432 if (wpa_s->off_channel_freq || wpa_s->roc_waiting_drv_freq) {
3433 wpa_printf(MSG_DEBUG, "P2P: Cancel remain-on-channel prior to "
3434 "starting client");
3435 wpa_drv_cancel_remain_on_channel(wpa_s);
3436 wpa_s->off_channel_freq = 0;
3437 wpa_s->roc_waiting_drv_freq = 0;
3438 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003439 wpas_start_wps_enrollee(group, &res);
3440
3441 /*
3442 * Allow a longer timeout for join-a-running-group than normal 15
3443 * second group formation timeout since the GO may not have authorized
3444 * our connection yet.
3445 */
3446 eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s, NULL);
3447 eloop_register_timeout(60, 0, wpas_p2p_group_formation_timeout,
3448 wpa_s, NULL);
3449
3450 return 0;
3451}
3452
3453
3454/**
3455 * wpas_p2p_connect - Request P2P Group Formation to be started
3456 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
3457 * @peer_addr: Address of the peer P2P Device
3458 * @pin: PIN to use during provisioning or %NULL to indicate PBC mode
3459 * @persistent_group: Whether to create a persistent group
Dmitry Shmidt04949592012-07-19 12:16:46 -07003460 * @auto_join: Whether to select join vs. GO Negotiation automatically
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003461 * @join: Whether to join an existing group (as a client) instead of starting
3462 * Group Owner negotiation; @peer_addr is BSSID in that case
3463 * @auth: Whether to only authorize the connection instead of doing that and
3464 * initiating Group Owner negotiation
3465 * @go_intent: GO Intent or -1 to use default
3466 * @freq: Frequency for the group or 0 for auto-selection
Dmitry Shmidt04949592012-07-19 12:16:46 -07003467 * @persistent_id: Persistent group credentials to use for forcing GO
3468 * parameters or -1 to generate new values (SSID/passphrase)
3469 * @pd: Whether to send Provision Discovery prior to GO Negotiation as an
3470 * interoperability workaround when initiating group formation
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003471 * @ht40: Start GO with 40 MHz channel width
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003472 * Returns: 0 or new PIN (if pin was %NULL) on success, -1 on unspecified
3473 * failure, -2 on failure due to channel not currently available,
3474 * -3 if forced channel is not supported
3475 */
3476int wpas_p2p_connect(struct wpa_supplicant *wpa_s, const u8 *peer_addr,
3477 const char *pin, enum p2p_wps_method wps_method,
Dmitry Shmidt04949592012-07-19 12:16:46 -07003478 int persistent_group, int auto_join, int join, int auth,
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003479 int go_intent, int freq, int persistent_id, int pd,
3480 int ht40)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003481{
3482 int force_freq = 0, oper_freq = 0;
3483 u8 bssid[ETH_ALEN];
3484 int ret = 0;
3485 enum wpa_driver_if_type iftype;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003486 const u8 *if_addr;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003487 struct wpa_ssid *ssid = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003488
3489 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
3490 return -1;
3491
Dmitry Shmidt04949592012-07-19 12:16:46 -07003492 if (persistent_id >= 0) {
3493 ssid = wpa_config_get_network(wpa_s->conf, persistent_id);
3494 if (ssid == NULL || ssid->disabled != 2 ||
3495 ssid->mode != WPAS_MODE_P2P_GO)
3496 return -1;
3497 }
3498
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003499 if (go_intent < 0)
3500 go_intent = wpa_s->conf->p2p_go_intent;
3501
3502 if (!auth)
3503 wpa_s->p2p_long_listen = 0;
3504
3505 wpa_s->p2p_wps_method = wps_method;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003506 wpa_s->p2p_persistent_group = !!persistent_group;
3507 wpa_s->p2p_persistent_id = persistent_id;
3508 wpa_s->p2p_go_intent = go_intent;
3509 wpa_s->p2p_connect_freq = freq;
3510 wpa_s->p2p_fallback_to_go_neg = 0;
3511 wpa_s->p2p_pd_before_go_neg = !!pd;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003512 wpa_s->p2p_go_ht40 = !!ht40;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003513
3514 if (pin)
3515 os_strlcpy(wpa_s->p2p_pin, pin, sizeof(wpa_s->p2p_pin));
3516 else if (wps_method == WPS_PIN_DISPLAY) {
3517 ret = wps_generate_pin();
3518 os_snprintf(wpa_s->p2p_pin, sizeof(wpa_s->p2p_pin), "%08d",
3519 ret);
3520 wpa_printf(MSG_DEBUG, "P2P: Randomly generated PIN: %s",
3521 wpa_s->p2p_pin);
3522 } else
3523 wpa_s->p2p_pin[0] = '\0';
3524
Dmitry Shmidt04949592012-07-19 12:16:46 -07003525 if (join || auto_join) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003526 u8 iface_addr[ETH_ALEN], dev_addr[ETH_ALEN];
3527 if (auth) {
3528 wpa_printf(MSG_DEBUG, "P2P: Authorize invitation to "
3529 "connect a running group from " MACSTR,
3530 MAC2STR(peer_addr));
3531 os_memcpy(wpa_s->p2p_auth_invite, peer_addr, ETH_ALEN);
3532 return ret;
3533 }
3534 os_memcpy(dev_addr, peer_addr, ETH_ALEN);
3535 if (p2p_get_interface_addr(wpa_s->global->p2p, peer_addr,
3536 iface_addr) < 0) {
3537 os_memcpy(iface_addr, peer_addr, ETH_ALEN);
3538 p2p_get_dev_addr(wpa_s->global->p2p, peer_addr,
3539 dev_addr);
3540 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07003541 if (auto_join) {
3542 os_get_time(&wpa_s->p2p_auto_started);
3543 wpa_printf(MSG_DEBUG, "P2P: Auto join started at "
3544 "%ld.%06ld",
3545 wpa_s->p2p_auto_started.sec,
3546 wpa_s->p2p_auto_started.usec);
3547 }
3548 if (wpas_p2p_join(wpa_s, iface_addr, dev_addr, wps_method,
3549 auto_join) < 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003550 return -1;
3551 return ret;
3552 }
3553
3554 if (wpa_s->current_ssid && wpa_drv_get_bssid(wpa_s, bssid) == 0 &&
3555 wpa_s->assoc_freq)
3556 oper_freq = wpa_s->assoc_freq;
3557 else {
3558 oper_freq = wpa_drv_shared_freq(wpa_s);
3559 if (oper_freq < 0)
3560 oper_freq = 0;
3561 }
3562
3563 if (freq > 0) {
3564 if (!p2p_supported_freq(wpa_s->global->p2p, freq)) {
3565 wpa_printf(MSG_DEBUG, "P2P: The forced channel "
3566 "(%u MHz) is not supported for P2P uses",
3567 freq);
3568 return -3;
3569 }
3570
3571 if (oper_freq > 0 && freq != oper_freq &&
3572 !(wpa_s->drv_flags &
3573 WPA_DRIVER_FLAGS_MULTI_CHANNEL_CONCURRENT)) {
3574 wpa_printf(MSG_DEBUG, "P2P: Cannot start P2P group "
3575 "on %u MHz while connected on another "
3576 "channel (%u MHz)", freq, oper_freq);
3577 return -2;
3578 }
3579 wpa_printf(MSG_DEBUG, "P2P: Trying to force us to use the "
3580 "requested channel (%u MHz)", freq);
3581 force_freq = freq;
3582 } else if (oper_freq > 0 &&
3583 !p2p_supported_freq(wpa_s->global->p2p, oper_freq)) {
3584 if (!(wpa_s->drv_flags &
3585 WPA_DRIVER_FLAGS_MULTI_CHANNEL_CONCURRENT)) {
3586 wpa_printf(MSG_DEBUG, "P2P: Cannot start P2P group "
3587 "while connected on non-P2P supported "
3588 "channel (%u MHz)", oper_freq);
3589 return -2;
3590 }
3591 wpa_printf(MSG_DEBUG, "P2P: Current operating channel "
3592 "(%u MHz) not available for P2P - try to use "
3593 "another channel", oper_freq);
3594 force_freq = 0;
3595 } else if (oper_freq > 0) {
3596 wpa_printf(MSG_DEBUG, "P2P: Trying to force us to use the "
3597 "channel we are already using (%u MHz) on another "
3598 "interface", oper_freq);
3599 force_freq = oper_freq;
3600 }
3601
3602 wpa_s->create_p2p_iface = wpas_p2p_create_iface(wpa_s);
3603
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003604 if (wpa_s->create_p2p_iface) {
3605 /* Prepare to add a new interface for the group */
3606 iftype = WPA_IF_P2P_GROUP;
3607 if (go_intent == 15)
3608 iftype = WPA_IF_P2P_GO;
3609 if (wpas_p2p_add_group_interface(wpa_s, iftype) < 0) {
3610 wpa_printf(MSG_ERROR, "P2P: Failed to allocate a new "
3611 "interface for the group");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003612 return -1;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003613 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003614
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003615 if_addr = wpa_s->pending_interface_addr;
3616 } else
3617 if_addr = wpa_s->own_addr;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003618
3619 if (auth) {
3620 if (wpas_p2p_auth_go_neg(wpa_s, peer_addr, wps_method,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003621 go_intent, if_addr,
Dmitry Shmidt04949592012-07-19 12:16:46 -07003622 force_freq, persistent_group, ssid) <
3623 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003624 return -1;
3625 return ret;
3626 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003627
3628 if (wpas_p2p_start_go_neg(wpa_s, peer_addr, wps_method,
3629 go_intent, if_addr, force_freq,
Dmitry Shmidt04949592012-07-19 12:16:46 -07003630 persistent_group, ssid) < 0) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003631 if (wpa_s->create_p2p_iface)
3632 wpas_p2p_remove_pending_group_interface(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003633 return -1;
3634 }
3635 return ret;
3636}
3637
3638
3639/**
3640 * wpas_p2p_remain_on_channel_cb - Indication of remain-on-channel start
3641 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
3642 * @freq: Frequency of the channel in MHz
3643 * @duration: Duration of the stay on the channel in milliseconds
3644 *
3645 * This callback is called when the driver indicates that it has started the
3646 * requested remain-on-channel duration.
3647 */
3648void wpas_p2p_remain_on_channel_cb(struct wpa_supplicant *wpa_s,
3649 unsigned int freq, unsigned int duration)
3650{
3651 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
3652 return;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003653 if (wpa_s->off_channel_freq == wpa_s->pending_listen_freq) {
3654 p2p_listen_cb(wpa_s->global->p2p, wpa_s->pending_listen_freq,
3655 wpa_s->pending_listen_duration);
3656 wpa_s->pending_listen_freq = 0;
3657 }
3658}
3659
3660
3661static int wpas_p2p_listen_start(struct wpa_supplicant *wpa_s,
3662 unsigned int timeout)
3663{
3664 /* Limit maximum Listen state time based on driver limitation. */
3665 if (timeout > wpa_s->max_remain_on_chan)
3666 timeout = wpa_s->max_remain_on_chan;
3667
3668 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
3669 return wpa_drv_p2p_listen(wpa_s, timeout);
3670
3671 return p2p_listen(wpa_s->global->p2p, timeout);
3672}
3673
3674
3675/**
3676 * wpas_p2p_cancel_remain_on_channel_cb - Remain-on-channel timeout
3677 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
3678 * @freq: Frequency of the channel in MHz
3679 *
3680 * This callback is called when the driver indicates that a remain-on-channel
3681 * operation has been completed, i.e., the duration on the requested channel
3682 * has timed out.
3683 */
3684void wpas_p2p_cancel_remain_on_channel_cb(struct wpa_supplicant *wpa_s,
3685 unsigned int freq)
3686{
3687 wpa_printf(MSG_DEBUG, "P2P: Cancel remain-on-channel callback "
3688 "(p2p_long_listen=%d ms pending_action_tx=%p)",
3689 wpa_s->p2p_long_listen, wpa_s->pending_action_tx);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003690 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
3691 return;
3692 if (p2p_listen_end(wpa_s->global->p2p, freq) > 0)
3693 return; /* P2P module started a new operation */
3694 if (wpa_s->pending_action_tx)
3695 return;
3696 if (wpa_s->p2p_long_listen > 0)
3697 wpa_s->p2p_long_listen -= wpa_s->max_remain_on_chan;
3698 if (wpa_s->p2p_long_listen > 0) {
3699 wpa_printf(MSG_DEBUG, "P2P: Continuing long Listen state");
3700 wpas_p2p_listen_start(wpa_s, wpa_s->p2p_long_listen);
3701 }
3702}
3703
3704
3705/**
3706 * wpas_p2p_group_remove - Remove a P2P group
3707 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
3708 * @ifname: Network interface name of the group interface or "*" to remove all
3709 * groups
3710 * Returns: 0 on success, -1 on failure
3711 *
3712 * This function is used to remove a P2P group. This can be used to disconnect
3713 * from a group in which the local end is a P2P Client or to end a P2P Group in
3714 * case the local end is the Group Owner. If a virtual network interface was
3715 * created for this group, that interface will be removed. Otherwise, only the
3716 * configured P2P group network will be removed from the interface.
3717 */
3718int wpas_p2p_group_remove(struct wpa_supplicant *wpa_s, const char *ifname)
3719{
3720 struct wpa_global *global = wpa_s->global;
3721
3722 if (os_strcmp(ifname, "*") == 0) {
3723 struct wpa_supplicant *prev;
3724 wpa_s = global->ifaces;
3725 while (wpa_s) {
3726 prev = wpa_s;
3727 wpa_s = wpa_s->next;
3728 wpas_p2p_disconnect(prev);
3729 }
3730 return 0;
3731 }
3732
3733 for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
3734 if (os_strcmp(wpa_s->ifname, ifname) == 0)
3735 break;
3736 }
3737
3738 return wpas_p2p_disconnect(wpa_s);
3739}
3740
3741
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003742static int wpas_p2p_init_go_params(struct wpa_supplicant *wpa_s,
3743 struct p2p_go_neg_results *params,
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003744 int freq, int ht40)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003745{
3746 u8 bssid[ETH_ALEN];
3747 int res;
3748
3749 os_memset(params, 0, sizeof(*params));
3750 params->role_go = 1;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003751 params->ht40 = ht40;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003752 if (freq) {
3753 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on forced "
3754 "frequency %d MHz", freq);
3755 params->freq = freq;
3756 } else if (wpa_s->conf->p2p_oper_reg_class == 81 &&
3757 wpa_s->conf->p2p_oper_channel >= 1 &&
3758 wpa_s->conf->p2p_oper_channel <= 11) {
3759 params->freq = 2407 + 5 * wpa_s->conf->p2p_oper_channel;
3760 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on configured "
3761 "frequency %d MHz", params->freq);
3762 } else if (wpa_s->conf->p2p_oper_reg_class == 115 ||
Jouni Malinen87fd2792011-05-16 18:35:42 +03003763 wpa_s->conf->p2p_oper_reg_class == 124) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003764 params->freq = 5000 + 5 * wpa_s->conf->p2p_oper_channel;
3765 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on configured "
3766 "frequency %d MHz", params->freq);
3767 } else if (wpa_s->conf->p2p_oper_channel == 0 &&
3768 wpa_s->best_overall_freq > 0 &&
3769 p2p_supported_freq(wpa_s->global->p2p,
3770 wpa_s->best_overall_freq)) {
3771 params->freq = wpa_s->best_overall_freq;
3772 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on best overall "
3773 "channel %d MHz", params->freq);
3774 } else if (wpa_s->conf->p2p_oper_channel == 0 &&
3775 wpa_s->best_24_freq > 0 &&
3776 p2p_supported_freq(wpa_s->global->p2p,
3777 wpa_s->best_24_freq)) {
3778 params->freq = wpa_s->best_24_freq;
3779 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on best 2.4 GHz "
3780 "channel %d MHz", params->freq);
3781 } else if (wpa_s->conf->p2p_oper_channel == 0 &&
3782 wpa_s->best_5_freq > 0 &&
3783 p2p_supported_freq(wpa_s->global->p2p,
3784 wpa_s->best_5_freq)) {
3785 params->freq = wpa_s->best_5_freq;
3786 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on best 5 GHz "
3787 "channel %d MHz", params->freq);
3788 } else {
Dmitry Shmidt04949592012-07-19 12:16:46 -07003789 int chan;
3790 for (chan = 0; chan < 11; chan++) {
3791 params->freq = 2412 + chan * 5;
3792 if (!wpas_p2p_disallowed_freq(wpa_s->global,
3793 params->freq))
3794 break;
3795 }
3796 if (chan == 11) {
3797 wpa_printf(MSG_DEBUG, "P2P: No 2.4 GHz channel "
3798 "allowed");
3799 return -1;
3800 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003801 wpa_printf(MSG_DEBUG, "P2P: Set GO freq %d MHz (no preference "
3802 "known)", params->freq);
3803 }
3804
3805 if (wpa_s->current_ssid && wpa_drv_get_bssid(wpa_s, bssid) == 0 &&
3806 wpa_s->assoc_freq && !freq) {
3807 wpa_printf(MSG_DEBUG, "P2P: Force GO on the channel we are "
3808 "already using");
3809 params->freq = wpa_s->assoc_freq;
3810 }
3811
3812 res = wpa_drv_shared_freq(wpa_s);
3813 if (res > 0 && !freq) {
3814 wpa_printf(MSG_DEBUG, "P2P: Force GO on the channel we are "
3815 "already using on a shared interface");
3816 params->freq = res;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003817 } else if (res > 0 && freq != res &&
3818 !(wpa_s->drv_flags &
3819 WPA_DRIVER_FLAGS_MULTI_CHANNEL_CONCURRENT)) {
3820 wpa_printf(MSG_DEBUG, "P2P: Cannot start P2P group on %u MHz "
3821 "while connected on another channel (%u MHz)",
3822 freq, res);
3823 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003824 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003825
3826 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003827}
3828
3829
3830static struct wpa_supplicant *
3831wpas_p2p_get_group_iface(struct wpa_supplicant *wpa_s, int addr_allocated,
3832 int go)
3833{
3834 struct wpa_supplicant *group_wpa_s;
3835
Dmitry Shmidtaa532512012-09-24 10:35:31 -07003836 if (!wpas_p2p_create_iface(wpa_s)) {
3837 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Use same interface for group "
3838 "operations");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003839 return wpa_s;
Dmitry Shmidtaa532512012-09-24 10:35:31 -07003840 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003841
3842 if (wpas_p2p_add_group_interface(wpa_s, go ? WPA_IF_P2P_GO :
Dmitry Shmidtaa532512012-09-24 10:35:31 -07003843 WPA_IF_P2P_CLIENT) < 0) {
3844 wpa_msg(wpa_s, MSG_ERROR, "P2P: Failed to add group interface");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003845 return NULL;
Dmitry Shmidtaa532512012-09-24 10:35:31 -07003846 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003847 group_wpa_s = wpas_p2p_init_group_interface(wpa_s, go);
3848 if (group_wpa_s == NULL) {
Dmitry Shmidtaa532512012-09-24 10:35:31 -07003849 wpa_msg(wpa_s, MSG_ERROR, "P2P: Failed to initialize group "
3850 "interface");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003851 wpas_p2p_remove_pending_group_interface(wpa_s);
3852 return NULL;
3853 }
3854
Dmitry Shmidtaa532512012-09-24 10:35:31 -07003855 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Use separate group interface %s",
3856 group_wpa_s->ifname);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003857 return group_wpa_s;
3858}
3859
3860
3861/**
3862 * wpas_p2p_group_add - Add a new P2P group with local end as Group Owner
3863 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
3864 * @persistent_group: Whether to create a persistent group
3865 * @freq: Frequency for the group or 0 to indicate no hardcoding
3866 * Returns: 0 on success, -1 on failure
3867 *
3868 * This function creates a new P2P group with the local end as the Group Owner,
3869 * i.e., without using Group Owner Negotiation.
3870 */
3871int wpas_p2p_group_add(struct wpa_supplicant *wpa_s, int persistent_group,
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003872 int freq, int ht40)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003873{
3874 struct p2p_go_neg_results params;
3875 unsigned int r;
3876
3877 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
3878 return -1;
3879
Dmitry Shmidtdca39792011-09-06 11:17:33 -07003880 /* Make sure we are not running find during connection establishment */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003881 wpa_printf(MSG_DEBUG, "P2P: Stop any on-going P2P FIND");
Dmitry Shmidtdca39792011-09-06 11:17:33 -07003882 wpas_p2p_stop_find(wpa_s);
Dmitry Shmidtdca39792011-09-06 11:17:33 -07003883
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003884 if (freq == 2) {
3885 wpa_printf(MSG_DEBUG, "P2P: Request to start GO on 2.4 GHz "
3886 "band");
3887 if (wpa_s->best_24_freq > 0 &&
3888 p2p_supported_freq(wpa_s->global->p2p,
3889 wpa_s->best_24_freq)) {
3890 freq = wpa_s->best_24_freq;
3891 wpa_printf(MSG_DEBUG, "P2P: Use best 2.4 GHz band "
3892 "channel: %d MHz", freq);
3893 } else {
3894 os_get_random((u8 *) &r, sizeof(r));
3895 freq = 2412 + (r % 3) * 25;
3896 wpa_printf(MSG_DEBUG, "P2P: Use random 2.4 GHz band "
3897 "channel: %d MHz", freq);
3898 }
3899 }
3900
3901 if (freq == 5) {
3902 wpa_printf(MSG_DEBUG, "P2P: Request to start GO on 5 GHz "
3903 "band");
3904 if (wpa_s->best_5_freq > 0 &&
3905 p2p_supported_freq(wpa_s->global->p2p,
3906 wpa_s->best_5_freq)) {
3907 freq = wpa_s->best_5_freq;
3908 wpa_printf(MSG_DEBUG, "P2P: Use best 5 GHz band "
3909 "channel: %d MHz", freq);
3910 } else {
3911 os_get_random((u8 *) &r, sizeof(r));
3912 freq = 5180 + (r % 4) * 20;
3913 if (!p2p_supported_freq(wpa_s->global->p2p, freq)) {
3914 wpa_printf(MSG_DEBUG, "P2P: Could not select "
3915 "5 GHz channel for P2P group");
3916 return -1;
3917 }
3918 wpa_printf(MSG_DEBUG, "P2P: Use random 5 GHz band "
3919 "channel: %d MHz", freq);
3920 }
3921 }
3922
3923 if (freq > 0 && !p2p_supported_freq(wpa_s->global->p2p, freq)) {
3924 wpa_printf(MSG_DEBUG, "P2P: The forced channel for GO "
3925 "(%u MHz) is not supported for P2P uses",
3926 freq);
3927 return -1;
3928 }
3929
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003930 if (wpas_p2p_init_go_params(wpa_s, &params, freq, ht40))
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003931 return -1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003932 if (params.freq &&
3933 !p2p_supported_freq(wpa_s->global->p2p, params.freq)) {
3934 wpa_printf(MSG_DEBUG, "P2P: The selected channel for GO "
3935 "(%u MHz) is not supported for P2P uses",
3936 params.freq);
3937 return -1;
3938 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003939 p2p_go_params(wpa_s->global->p2p, &params);
3940 params.persistent_group = persistent_group;
3941
3942 wpa_s = wpas_p2p_get_group_iface(wpa_s, 0, 1);
3943 if (wpa_s == NULL)
3944 return -1;
3945 wpas_start_wps_go(wpa_s, &params, 0);
3946
3947 return 0;
3948}
3949
3950
3951static int wpas_start_p2p_client(struct wpa_supplicant *wpa_s,
3952 struct wpa_ssid *params, int addr_allocated)
3953{
3954 struct wpa_ssid *ssid;
3955
3956 wpa_s = wpas_p2p_get_group_iface(wpa_s, addr_allocated, 0);
3957 if (wpa_s == NULL)
3958 return -1;
3959
3960 wpa_supplicant_ap_deinit(wpa_s);
3961
3962 ssid = wpa_config_add_network(wpa_s->conf);
3963 if (ssid == NULL)
3964 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003965 wpa_config_set_network_defaults(ssid);
3966 ssid->temporary = 1;
3967 ssid->proto = WPA_PROTO_RSN;
3968 ssid->pairwise_cipher = WPA_CIPHER_CCMP;
3969 ssid->group_cipher = WPA_CIPHER_CCMP;
3970 ssid->key_mgmt = WPA_KEY_MGMT_PSK;
3971 ssid->ssid = os_malloc(params->ssid_len);
3972 if (ssid->ssid == NULL) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003973 wpa_config_remove_network(wpa_s->conf, ssid->id);
3974 return -1;
3975 }
3976 os_memcpy(ssid->ssid, params->ssid, params->ssid_len);
3977 ssid->ssid_len = params->ssid_len;
3978 ssid->p2p_group = 1;
3979 ssid->export_keys = 1;
3980 if (params->psk_set) {
3981 os_memcpy(ssid->psk, params->psk, 32);
3982 ssid->psk_set = 1;
3983 }
3984 if (params->passphrase)
3985 ssid->passphrase = os_strdup(params->passphrase);
3986
3987 wpa_supplicant_select_network(wpa_s, ssid);
3988
3989 wpa_s->show_group_started = 1;
3990
3991 return 0;
3992}
3993
3994
3995int wpas_p2p_group_add_persistent(struct wpa_supplicant *wpa_s,
3996 struct wpa_ssid *ssid, int addr_allocated,
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003997 int freq, int ht40)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003998{
3999 struct p2p_go_neg_results params;
4000 int go = 0;
4001
4002 if (ssid->disabled != 2 || ssid->ssid == NULL)
4003 return -1;
4004
4005 if (wpas_get_p2p_group(wpa_s, ssid->ssid, ssid->ssid_len, &go) &&
4006 go == (ssid->mode == WPAS_MODE_P2P_GO)) {
4007 wpa_printf(MSG_DEBUG, "P2P: Requested persistent group is "
4008 "already running");
4009 return 0;
4010 }
4011
4012 /* Make sure we are not running find during connection establishment */
4013 wpas_p2p_stop_find(wpa_s);
4014
Dmitry Shmidt04949592012-07-19 12:16:46 -07004015 wpa_s->p2p_fallback_to_go_neg = 0;
4016
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004017 if (ssid->mode == WPAS_MODE_INFRA)
4018 return wpas_start_p2p_client(wpa_s, ssid, addr_allocated);
4019
4020 if (ssid->mode != WPAS_MODE_P2P_GO)
4021 return -1;
4022
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004023 if (wpas_p2p_init_go_params(wpa_s, &params, freq, ht40))
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004024 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004025
4026 params.role_go = 1;
4027 if (ssid->passphrase == NULL ||
4028 os_strlen(ssid->passphrase) >= sizeof(params.passphrase)) {
4029 wpa_printf(MSG_DEBUG, "P2P: Invalid passphrase in persistent "
4030 "group");
4031 return -1;
4032 }
4033 os_strlcpy(params.passphrase, ssid->passphrase,
4034 sizeof(params.passphrase));
4035 os_memcpy(params.ssid, ssid->ssid, ssid->ssid_len);
4036 params.ssid_len = ssid->ssid_len;
4037 params.persistent_group = 1;
4038
4039 wpa_s = wpas_p2p_get_group_iface(wpa_s, addr_allocated, 1);
4040 if (wpa_s == NULL)
4041 return -1;
4042
4043 wpas_start_wps_go(wpa_s, &params, 0);
4044
4045 return 0;
4046}
4047
4048
4049static void wpas_p2p_ie_update(void *ctx, struct wpabuf *beacon_ies,
4050 struct wpabuf *proberesp_ies)
4051{
4052 struct wpa_supplicant *wpa_s = ctx;
4053 if (wpa_s->ap_iface) {
4054 struct hostapd_data *hapd = wpa_s->ap_iface->bss[0];
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004055 if (!(hapd->conf->p2p & P2P_GROUP_OWNER)) {
4056 wpabuf_free(beacon_ies);
4057 wpabuf_free(proberesp_ies);
4058 return;
4059 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004060 if (beacon_ies) {
4061 wpabuf_free(hapd->p2p_beacon_ie);
4062 hapd->p2p_beacon_ie = beacon_ies;
4063 }
4064 wpabuf_free(hapd->p2p_probe_resp_ie);
4065 hapd->p2p_probe_resp_ie = proberesp_ies;
4066 } else {
4067 wpabuf_free(beacon_ies);
4068 wpabuf_free(proberesp_ies);
4069 }
4070 wpa_supplicant_ap_update_beacon(wpa_s);
4071}
4072
4073
4074static void wpas_p2p_idle_update(void *ctx, int idle)
4075{
4076 struct wpa_supplicant *wpa_s = ctx;
4077 if (!wpa_s->ap_iface)
4078 return;
4079 wpa_printf(MSG_DEBUG, "P2P: GO - group %sidle", idle ? "" : "not ");
Dmitry Shmidt04949592012-07-19 12:16:46 -07004080 if (idle)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004081 wpas_p2p_set_group_idle_timeout(wpa_s);
4082 else
4083 eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL);
4084}
4085
4086
4087struct p2p_group * wpas_p2p_group_init(struct wpa_supplicant *wpa_s,
Dmitry Shmidt04949592012-07-19 12:16:46 -07004088 struct wpa_ssid *ssid)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004089{
4090 struct p2p_group *group;
4091 struct p2p_group_config *cfg;
4092
4093 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
4094 return NULL;
4095 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
4096 return NULL;
4097
4098 cfg = os_zalloc(sizeof(*cfg));
4099 if (cfg == NULL)
4100 return NULL;
4101
Dmitry Shmidt04949592012-07-19 12:16:46 -07004102 if (ssid->p2p_persistent_group && wpa_s->conf->persistent_reconnect)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004103 cfg->persistent_group = 2;
Dmitry Shmidt04949592012-07-19 12:16:46 -07004104 else if (ssid->p2p_persistent_group)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004105 cfg->persistent_group = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004106 os_memcpy(cfg->interface_addr, wpa_s->own_addr, ETH_ALEN);
4107 if (wpa_s->max_stations &&
4108 wpa_s->max_stations < wpa_s->conf->max_num_sta)
4109 cfg->max_clients = wpa_s->max_stations;
4110 else
4111 cfg->max_clients = wpa_s->conf->max_num_sta;
Dmitry Shmidt04949592012-07-19 12:16:46 -07004112 os_memcpy(cfg->ssid, ssid->ssid, ssid->ssid_len);
4113 cfg->ssid_len = ssid->ssid_len;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004114 cfg->cb_ctx = wpa_s;
4115 cfg->ie_update = wpas_p2p_ie_update;
4116 cfg->idle_update = wpas_p2p_idle_update;
4117
4118 group = p2p_group_init(wpa_s->global->p2p, cfg);
4119 if (group == NULL)
4120 os_free(cfg);
Dmitry Shmidt04949592012-07-19 12:16:46 -07004121 if (ssid->mode != WPAS_MODE_P2P_GROUP_FORMATION)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004122 p2p_group_notif_formation_done(group);
4123 wpa_s->p2p_group = group;
4124 return group;
4125}
4126
4127
4128void wpas_p2p_wps_success(struct wpa_supplicant *wpa_s, const u8 *peer_addr,
4129 int registrar)
4130{
Dmitry Shmidt04949592012-07-19 12:16:46 -07004131 struct wpa_ssid *ssid = wpa_s->current_ssid;
4132
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004133 if (!wpa_s->p2p_in_provisioning) {
4134 wpa_printf(MSG_DEBUG, "P2P: Ignore WPS success event - P2P "
4135 "provisioning not in progress");
4136 return;
4137 }
4138
Dmitry Shmidt04949592012-07-19 12:16:46 -07004139 if (ssid && ssid->mode == WPAS_MODE_INFRA) {
4140 u8 go_dev_addr[ETH_ALEN];
4141 os_memcpy(go_dev_addr, wpa_s->bssid, ETH_ALEN);
4142 wpas_p2p_persistent_group(wpa_s, go_dev_addr, ssid->ssid,
4143 ssid->ssid_len);
4144 /* Clear any stored provisioning info */
4145 p2p_clear_provisioning_info(wpa_s->global->p2p, go_dev_addr);
4146 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004147
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004148 eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s->parent,
4149 NULL);
Dmitry Shmidt04949592012-07-19 12:16:46 -07004150 if (ssid && ssid->mode == WPAS_MODE_INFRA) {
4151 /*
4152 * Use a separate timeout for initial data connection to
4153 * complete to allow the group to be removed automatically if
4154 * something goes wrong in this step before the P2P group idle
4155 * timeout mechanism is taken into use.
4156 */
4157 eloop_register_timeout(P2P_MAX_INITIAL_CONN_WAIT, 0,
4158 wpas_p2p_group_formation_timeout,
4159 wpa_s->parent, NULL);
4160 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004161 if (wpa_s->global->p2p)
4162 p2p_wps_success_cb(wpa_s->global->p2p, peer_addr);
4163 else if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
4164 wpa_drv_wps_success_cb(wpa_s, peer_addr);
4165 wpas_group_formation_completed(wpa_s, 1);
4166}
4167
4168
Jouni Malinen75ecf522011-06-27 15:19:46 -07004169void wpas_p2p_wps_failed(struct wpa_supplicant *wpa_s,
4170 struct wps_event_fail *fail)
4171{
4172 if (!wpa_s->p2p_in_provisioning) {
4173 wpa_printf(MSG_DEBUG, "P2P: Ignore WPS fail event - P2P "
4174 "provisioning not in progress");
4175 return;
4176 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004177
4178 if (wpa_s->go_params) {
4179 p2p_clear_provisioning_info(
4180 wpa_s->global->p2p,
Dmitry Shmidt04949592012-07-19 12:16:46 -07004181 wpa_s->go_params->peer_device_addr);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004182 }
4183
Jouni Malinen75ecf522011-06-27 15:19:46 -07004184 wpas_notify_p2p_wps_failed(wpa_s, fail);
4185}
4186
4187
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004188int wpas_p2p_prov_disc(struct wpa_supplicant *wpa_s, const u8 *peer_addr,
Dmitry Shmidt04949592012-07-19 12:16:46 -07004189 const char *config_method,
4190 enum wpas_p2p_prov_disc_use use)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004191{
4192 u16 config_methods;
4193
Dmitry Shmidt04949592012-07-19 12:16:46 -07004194 wpa_s->p2p_fallback_to_go_neg = 0;
4195 wpa_s->pending_pd_use = NORMAL_PD;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004196 if (os_strncmp(config_method, "display", 7) == 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004197 config_methods = WPS_CONFIG_DISPLAY;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004198 else if (os_strncmp(config_method, "keypad", 6) == 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004199 config_methods = WPS_CONFIG_KEYPAD;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004200 else if (os_strncmp(config_method, "pbc", 3) == 0 ||
4201 os_strncmp(config_method, "pushbutton", 10) == 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004202 config_methods = WPS_CONFIG_PUSHBUTTON;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004203 else {
4204 wpa_printf(MSG_DEBUG, "P2P: Unknown config method");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004205 return -1;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004206 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004207
Dmitry Shmidt04949592012-07-19 12:16:46 -07004208 if (use == WPAS_P2P_PD_AUTO) {
4209 os_memcpy(wpa_s->pending_join_dev_addr, peer_addr, ETH_ALEN);
4210 wpa_s->pending_pd_config_methods = config_methods;
4211 wpa_s->p2p_auto_pd = 1;
4212 wpa_s->p2p_auto_join = 0;
4213 wpa_s->pending_pd_before_join = 0;
4214 wpa_s->auto_pd_scan_retry = 0;
4215 wpas_p2p_stop_find(wpa_s);
4216 wpa_s->p2p_join_scan_count = 0;
4217 os_get_time(&wpa_s->p2p_auto_started);
4218 wpa_printf(MSG_DEBUG, "P2P: Auto PD started at %ld.%06ld",
4219 wpa_s->p2p_auto_started.sec,
4220 wpa_s->p2p_auto_started.usec);
4221 wpas_p2p_join_scan(wpa_s, NULL);
4222 return 0;
4223 }
4224
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004225 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT) {
4226 return wpa_drv_p2p_prov_disc_req(wpa_s, peer_addr,
Dmitry Shmidt04949592012-07-19 12:16:46 -07004227 config_methods,
4228 use == WPAS_P2P_PD_FOR_JOIN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004229 }
4230
4231 if (wpa_s->global->p2p == NULL || wpa_s->global->p2p_disabled)
4232 return -1;
4233
4234 return p2p_prov_disc_req(wpa_s->global->p2p, peer_addr,
Dmitry Shmidt04949592012-07-19 12:16:46 -07004235 config_methods, use == WPAS_P2P_PD_FOR_JOIN,
4236 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004237}
4238
4239
4240int wpas_p2p_scan_result_text(const u8 *ies, size_t ies_len, char *buf,
4241 char *end)
4242{
4243 return p2p_scan_result_text(ies, ies_len, buf, end);
4244}
4245
4246
4247static void wpas_p2p_clear_pending_action_tx(struct wpa_supplicant *wpa_s)
4248{
4249 if (!wpa_s->pending_action_tx)
4250 return;
4251
4252 wpa_printf(MSG_DEBUG, "P2P: Drop pending Action TX due to new "
4253 "operation request");
4254 wpabuf_free(wpa_s->pending_action_tx);
4255 wpa_s->pending_action_tx = NULL;
4256}
4257
4258
4259int wpas_p2p_find(struct wpa_supplicant *wpa_s, unsigned int timeout,
4260 enum p2p_discovery_type type,
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08004261 unsigned int num_req_dev_types, const u8 *req_dev_types,
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004262 const u8 *dev_id, unsigned int search_delay)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004263{
4264 wpas_p2p_clear_pending_action_tx(wpa_s);
4265 wpa_s->p2p_long_listen = 0;
4266
4267 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
4268 return wpa_drv_p2p_find(wpa_s, timeout, type);
4269
Dmitry Shmidt04949592012-07-19 12:16:46 -07004270 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL ||
4271 wpa_s->p2p_in_provisioning)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004272 return -1;
4273
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08004274 wpa_supplicant_cancel_sched_scan(wpa_s);
4275
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004276 return p2p_find(wpa_s->global->p2p, timeout, type,
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004277 num_req_dev_types, req_dev_types, dev_id,
4278 search_delay);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004279}
4280
4281
4282void wpas_p2p_stop_find(struct wpa_supplicant *wpa_s)
4283{
4284 wpas_p2p_clear_pending_action_tx(wpa_s);
4285 wpa_s->p2p_long_listen = 0;
4286 eloop_cancel_timeout(wpas_p2p_long_listen_timeout, wpa_s, NULL);
4287 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
Jouni Malinendc7b7132012-09-14 12:53:47 -07004288 wpa_s->global->p2p_cb_on_scan_complete = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004289
4290 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT) {
4291 wpa_drv_p2p_stop_find(wpa_s);
4292 return;
4293 }
4294
4295 if (wpa_s->global->p2p)
4296 p2p_stop_find(wpa_s->global->p2p);
4297
4298 wpas_p2p_remove_pending_group_interface(wpa_s);
4299}
4300
4301
4302static void wpas_p2p_long_listen_timeout(void *eloop_ctx, void *timeout_ctx)
4303{
4304 struct wpa_supplicant *wpa_s = eloop_ctx;
4305 wpa_s->p2p_long_listen = 0;
4306}
4307
4308
4309int wpas_p2p_listen(struct wpa_supplicant *wpa_s, unsigned int timeout)
4310{
4311 int res;
4312
4313 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
4314 return -1;
4315
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08004316 wpa_supplicant_cancel_sched_scan(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004317 wpas_p2p_clear_pending_action_tx(wpa_s);
4318
4319 if (timeout == 0) {
4320 /*
4321 * This is a request for unlimited Listen state. However, at
4322 * least for now, this is mapped to a Listen state for one
4323 * hour.
4324 */
4325 timeout = 3600;
4326 }
4327 eloop_cancel_timeout(wpas_p2p_long_listen_timeout, wpa_s, NULL);
4328 wpa_s->p2p_long_listen = 0;
4329
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004330 /*
4331 * Stop previous find/listen operation to avoid trying to request a new
4332 * remain-on-channel operation while the driver is still running the
4333 * previous one.
4334 */
4335 if (wpa_s->global->p2p)
4336 p2p_stop_find(wpa_s->global->p2p);
4337
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004338 res = wpas_p2p_listen_start(wpa_s, timeout * 1000);
4339 if (res == 0 && timeout * 1000 > wpa_s->max_remain_on_chan) {
4340 wpa_s->p2p_long_listen = timeout * 1000;
4341 eloop_register_timeout(timeout, 0,
4342 wpas_p2p_long_listen_timeout,
4343 wpa_s, NULL);
4344 }
4345
4346 return res;
4347}
4348
4349
4350int wpas_p2p_assoc_req_ie(struct wpa_supplicant *wpa_s, struct wpa_bss *bss,
4351 u8 *buf, size_t len, int p2p_group)
4352{
4353 struct wpabuf *p2p_ie;
4354 int ret;
4355
4356 if (wpa_s->global->p2p_disabled)
4357 return -1;
4358 if (wpa_s->global->p2p == NULL)
4359 return -1;
4360 if (bss == NULL)
4361 return -1;
4362
4363 p2p_ie = wpa_bss_get_vendor_ie_multi(bss, P2P_IE_VENDOR_TYPE);
4364 ret = p2p_assoc_req_ie(wpa_s->global->p2p, bss->bssid, buf, len,
4365 p2p_group, p2p_ie);
4366 wpabuf_free(p2p_ie);
4367
4368 return ret;
4369}
4370
4371
4372int wpas_p2p_probe_req_rx(struct wpa_supplicant *wpa_s, const u8 *addr,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004373 const u8 *dst, const u8 *bssid,
Dmitry Shmidt04949592012-07-19 12:16:46 -07004374 const u8 *ie, size_t ie_len, int ssi_signal)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004375{
4376 if (wpa_s->global->p2p_disabled)
4377 return 0;
4378 if (wpa_s->global->p2p == NULL)
4379 return 0;
4380
Dmitry Shmidt04949592012-07-19 12:16:46 -07004381 switch (p2p_probe_req_rx(wpa_s->global->p2p, addr, dst, bssid,
4382 ie, ie_len)) {
4383 case P2P_PREQ_NOT_P2P:
4384 wpas_notify_preq(wpa_s, addr, dst, bssid, ie, ie_len,
4385 ssi_signal);
4386 /* fall through */
4387 case P2P_PREQ_MALFORMED:
4388 case P2P_PREQ_NOT_LISTEN:
4389 case P2P_PREQ_NOT_PROCESSED:
4390 default: /* make gcc happy */
4391 return 0;
4392 case P2P_PREQ_PROCESSED:
4393 return 1;
4394 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004395}
4396
4397
4398void wpas_p2p_rx_action(struct wpa_supplicant *wpa_s, const u8 *da,
4399 const u8 *sa, const u8 *bssid,
4400 u8 category, const u8 *data, size_t len, int freq)
4401{
4402 if (wpa_s->global->p2p_disabled)
4403 return;
4404 if (wpa_s->global->p2p == NULL)
4405 return;
4406
4407 p2p_rx_action(wpa_s->global->p2p, da, sa, bssid, category, data, len,
4408 freq);
4409}
4410
4411
4412void wpas_p2p_scan_ie(struct wpa_supplicant *wpa_s, struct wpabuf *ies)
4413{
4414 if (wpa_s->global->p2p_disabled)
4415 return;
4416 if (wpa_s->global->p2p == NULL)
4417 return;
4418
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08004419 p2p_scan_ie(wpa_s->global->p2p, ies, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004420}
4421
4422
4423void wpas_p2p_group_deinit(struct wpa_supplicant *wpa_s)
4424{
4425 p2p_group_deinit(wpa_s->p2p_group);
4426 wpa_s->p2p_group = NULL;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004427
4428 wpa_s->ap_configured_cb = NULL;
4429 wpa_s->ap_configured_cb_ctx = NULL;
4430 wpa_s->ap_configured_cb_data = NULL;
4431 wpa_s->connect_without_scan = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004432}
4433
4434
4435int wpas_p2p_reject(struct wpa_supplicant *wpa_s, const u8 *addr)
4436{
4437 wpa_s->p2p_long_listen = 0;
4438
4439 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
4440 return wpa_drv_p2p_reject(wpa_s, addr);
4441
4442 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
4443 return -1;
4444
4445 return p2p_reject(wpa_s->global->p2p, addr);
4446}
4447
4448
4449/* Invite to reinvoke a persistent group */
4450int wpas_p2p_invite(struct wpa_supplicant *wpa_s, const u8 *peer_addr,
Jouni Malinen31be0a42012-08-31 21:20:51 +03004451 struct wpa_ssid *ssid, const u8 *go_dev_addr, int freq,
4452 int ht40)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004453{
4454 enum p2p_invite_role role;
4455 u8 *bssid = NULL;
Irfan Sherifff0619662012-09-23 19:11:37 -07004456#ifdef ANDROID_P2P
Irfan Sheriffaf84a572012-09-22 16:59:30 -07004457 int force_freq = 0, oper_freq = 0;
Irfan Sherifff0619662012-09-23 19:11:37 -07004458#endif
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004459
Jouni Malinen31be0a42012-08-31 21:20:51 +03004460 wpa_s->p2p_persistent_go_freq = freq;
4461 wpa_s->p2p_go_ht40 = !!ht40;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004462 if (ssid->mode == WPAS_MODE_P2P_GO) {
4463 role = P2P_INVITE_ROLE_GO;
4464 if (peer_addr == NULL) {
4465 wpa_printf(MSG_DEBUG, "P2P: Missing peer "
4466 "address in invitation command");
4467 return -1;
4468 }
4469 if (wpas_p2p_create_iface(wpa_s)) {
4470 if (wpas_p2p_add_group_interface(wpa_s,
4471 WPA_IF_P2P_GO) < 0) {
4472 wpa_printf(MSG_ERROR, "P2P: Failed to "
4473 "allocate a new interface for the "
4474 "group");
4475 return -1;
4476 }
4477 bssid = wpa_s->pending_interface_addr;
4478 } else
4479 bssid = wpa_s->own_addr;
4480 } else {
4481 role = P2P_INVITE_ROLE_CLIENT;
4482 peer_addr = ssid->bssid;
4483 }
4484 wpa_s->pending_invite_ssid_id = ssid->id;
4485
Irfan Sherifff0619662012-09-23 19:11:37 -07004486#ifdef ANDROID_P2P
Irfan Sheriffaf84a572012-09-22 16:59:30 -07004487 if (wpa_s->current_ssid && wpa_drv_get_bssid(wpa_s, bssid) == 0 &&
4488 wpa_s->assoc_freq)
4489 oper_freq = wpa_s->assoc_freq;
4490 else {
4491 oper_freq = wpa_drv_shared_freq(wpa_s);
4492 if (oper_freq < 0)
4493 oper_freq = 0;
4494 }
4495
4496 if (freq > 0) {
4497 if (!p2p_supported_freq(wpa_s->global->p2p, freq)) {
4498 wpa_printf(MSG_DEBUG, "P2P: The forced channel "
4499 "(%u MHz) is not supported for P2P uses",
4500 freq);
4501 return -3;
4502 }
4503
4504 if (oper_freq > 0 && freq != oper_freq &&
4505 !(wpa_s->drv_flags &
4506 WPA_DRIVER_FLAGS_MULTI_CHANNEL_CONCURRENT)) {
4507 wpa_printf(MSG_DEBUG, "P2P: Cannot start P2P group "
4508 "on %u MHz while connected on another "
4509 "channel (%u MHz)", freq, oper_freq);
4510 return -2;
4511 }
4512 wpa_printf(MSG_DEBUG, "P2P: Trying to force us to use the "
4513 "requested channel (%u MHz)", freq);
4514 force_freq = freq;
4515 } else if (oper_freq > 0 &&
4516 !p2p_supported_freq(wpa_s->global->p2p, oper_freq)) {
4517 if (!(wpa_s->drv_flags &
4518 WPA_DRIVER_FLAGS_MULTI_CHANNEL_CONCURRENT)) {
4519 wpa_printf(MSG_DEBUG, "P2P: Cannot start P2P group "
4520 "while connected on non-P2P supported "
4521 "channel (%u MHz)", oper_freq);
4522 return -2;
4523 }
4524 wpa_printf(MSG_DEBUG, "P2P: Current operating channel "
4525 "(%u MHz) not available for P2P - try to use "
4526 "another channel", oper_freq);
4527 force_freq = 0;
4528 } else if (oper_freq > 0) {
4529 wpa_printf(MSG_DEBUG, "P2P: Trying to force us to use the "
4530 "channel we are already using (%u MHz) on another "
4531 "interface", oper_freq);
4532 force_freq = oper_freq;
4533 }
Irfan Sherifff0619662012-09-23 19:11:37 -07004534#endif
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004535 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
4536 return wpa_drv_p2p_invite(wpa_s, peer_addr, role, bssid,
4537 ssid->ssid, ssid->ssid_len,
4538 go_dev_addr, 1);
4539
4540 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
4541 return -1;
4542
Irfan Sherifff0619662012-09-23 19:11:37 -07004543#ifdef ANDROID_P2P
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004544 return p2p_invite(wpa_s->global->p2p, peer_addr, role, bssid,
Irfan Sheriffaf84a572012-09-22 16:59:30 -07004545 ssid->ssid, ssid->ssid_len, force_freq, go_dev_addr, 1);
Irfan Sherifff0619662012-09-23 19:11:37 -07004546#else
4547 return p2p_invite(wpa_s->global->p2p, peer_addr, role, bssid,
4548 ssid->ssid, ssid->ssid_len, freq, go_dev_addr, 1);
4549#endif
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004550}
4551
4552
4553/* Invite to join an active group */
4554int wpas_p2p_invite_group(struct wpa_supplicant *wpa_s, const char *ifname,
4555 const u8 *peer_addr, const u8 *go_dev_addr)
4556{
4557 struct wpa_global *global = wpa_s->global;
4558 enum p2p_invite_role role;
4559 u8 *bssid = NULL;
4560 struct wpa_ssid *ssid;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08004561 int persistent;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004562
Jouni Malinen31be0a42012-08-31 21:20:51 +03004563 wpa_s->p2p_persistent_go_freq = 0;
4564 wpa_s->p2p_go_ht40 = 0;
4565
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004566 for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
4567 if (os_strcmp(wpa_s->ifname, ifname) == 0)
4568 break;
4569 }
4570 if (wpa_s == NULL) {
4571 wpa_printf(MSG_DEBUG, "P2P: Interface '%s' not found", ifname);
4572 return -1;
4573 }
4574
4575 ssid = wpa_s->current_ssid;
4576 if (ssid == NULL) {
4577 wpa_printf(MSG_DEBUG, "P2P: No current SSID to use for "
4578 "invitation");
4579 return -1;
4580 }
4581
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08004582 persistent = ssid->p2p_persistent_group &&
4583 wpas_p2p_get_persistent(wpa_s->parent, peer_addr,
4584 ssid->ssid, ssid->ssid_len);
4585
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004586 if (ssid->mode == WPAS_MODE_P2P_GO) {
4587 role = P2P_INVITE_ROLE_ACTIVE_GO;
4588 bssid = wpa_s->own_addr;
4589 if (go_dev_addr == NULL)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004590 go_dev_addr = wpa_s->global->p2p_dev_addr;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004591 } else {
4592 role = P2P_INVITE_ROLE_CLIENT;
4593 if (wpa_s->wpa_state < WPA_ASSOCIATED) {
4594 wpa_printf(MSG_DEBUG, "P2P: Not associated - cannot "
4595 "invite to current group");
4596 return -1;
4597 }
4598 bssid = wpa_s->bssid;
4599 if (go_dev_addr == NULL &&
4600 !is_zero_ether_addr(wpa_s->go_dev_addr))
4601 go_dev_addr = wpa_s->go_dev_addr;
4602 }
4603 wpa_s->parent->pending_invite_ssid_id = -1;
4604
4605 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
4606 return wpa_drv_p2p_invite(wpa_s, peer_addr, role, bssid,
4607 ssid->ssid, ssid->ssid_len,
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08004608 go_dev_addr, persistent);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004609
4610 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
4611 return -1;
4612
4613 return p2p_invite(wpa_s->global->p2p, peer_addr, role, bssid,
4614 ssid->ssid, ssid->ssid_len, wpa_s->assoc_freq,
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08004615 go_dev_addr, persistent);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004616}
4617
4618
4619void wpas_p2p_completed(struct wpa_supplicant *wpa_s)
4620{
4621 struct wpa_ssid *ssid = wpa_s->current_ssid;
4622 const char *ssid_txt;
4623 u8 go_dev_addr[ETH_ALEN];
Jouni Malinen75ecf522011-06-27 15:19:46 -07004624 int network_id = -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004625 int persistent;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004626 int freq;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004627
Dmitry Shmidt04949592012-07-19 12:16:46 -07004628 if (ssid == NULL || ssid->mode != WPAS_MODE_P2P_GROUP_FORMATION) {
4629 eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
4630 wpa_s->parent, NULL);
4631 }
4632
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004633 if (!wpa_s->show_group_started || !ssid)
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004634 goto done;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004635
4636 wpa_s->show_group_started = 0;
4637
4638 ssid_txt = wpa_ssid_txt(ssid->ssid, ssid->ssid_len);
4639 os_memset(go_dev_addr, 0, ETH_ALEN);
4640 if (ssid->bssid_set)
4641 os_memcpy(go_dev_addr, ssid->bssid, ETH_ALEN);
4642 persistent = wpas_p2p_persistent_group(wpa_s, go_dev_addr, ssid->ssid,
4643 ssid->ssid_len);
4644 os_memcpy(wpa_s->go_dev_addr, go_dev_addr, ETH_ALEN);
4645
4646 if (wpa_s->global->p2p_group_formation == wpa_s)
4647 wpa_s->global->p2p_group_formation = NULL;
4648
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004649 freq = wpa_s->current_bss ? wpa_s->current_bss->freq :
4650 (int) wpa_s->assoc_freq;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004651 if (ssid->passphrase == NULL && ssid->psk_set) {
4652 char psk[65];
4653 wpa_snprintf_hex(psk, sizeof(psk), ssid->psk, 32);
4654 wpa_msg(wpa_s->parent, MSG_INFO, P2P_EVENT_GROUP_STARTED
4655 "%s client ssid=\"%s\" freq=%d psk=%s go_dev_addr="
4656 MACSTR "%s",
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004657 wpa_s->ifname, ssid_txt, freq, psk,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004658 MAC2STR(go_dev_addr),
4659 persistent ? " [PERSISTENT]" : "");
4660 } else {
4661 wpa_msg(wpa_s->parent, MSG_INFO, P2P_EVENT_GROUP_STARTED
4662 "%s client ssid=\"%s\" freq=%d passphrase=\"%s\" "
4663 "go_dev_addr=" MACSTR "%s",
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004664 wpa_s->ifname, ssid_txt, freq,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004665 ssid->passphrase ? ssid->passphrase : "",
4666 MAC2STR(go_dev_addr),
4667 persistent ? " [PERSISTENT]" : "");
4668 }
4669
4670 if (persistent)
Jouni Malinen75ecf522011-06-27 15:19:46 -07004671 network_id = wpas_p2p_store_persistent_group(wpa_s->parent,
4672 ssid, go_dev_addr);
4673 if (network_id < 0)
4674 network_id = ssid->id;
4675 wpas_notify_p2p_group_started(wpa_s, ssid, network_id, 1);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004676
4677done:
Jouni Malinendc7b7132012-09-14 12:53:47 -07004678 if (wpa_s->global->p2p_cb_on_scan_complete && !wpa_s->global->p2p_disabled &&
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004679 wpa_s->global->p2p != NULL) {
Jouni Malinendc7b7132012-09-14 12:53:47 -07004680 wpa_s->global->p2p_cb_on_scan_complete = 0;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004681 if (p2p_other_scan_completed(wpa_s->global->p2p) == 1) {
4682 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Pending P2P operation "
4683 "continued after successful connection");
4684 p2p_increase_search_delay(
4685 wpa_s->global->p2p,
4686 wpas_p2p_search_delay(wpa_s));
4687 }
4688 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004689}
4690
4691
4692int wpas_p2p_presence_req(struct wpa_supplicant *wpa_s, u32 duration1,
4693 u32 interval1, u32 duration2, u32 interval2)
4694{
4695 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
4696 return -1;
4697 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
4698 return -1;
4699
4700 if (wpa_s->wpa_state < WPA_ASSOCIATED ||
4701 wpa_s->current_ssid == NULL ||
4702 wpa_s->current_ssid->mode != WPAS_MODE_INFRA)
4703 return -1;
4704
4705 return p2p_presence_req(wpa_s->global->p2p, wpa_s->bssid,
4706 wpa_s->own_addr, wpa_s->assoc_freq,
4707 duration1, interval1, duration2, interval2);
4708}
4709
4710
4711int wpas_p2p_ext_listen(struct wpa_supplicant *wpa_s, unsigned int period,
4712 unsigned int interval)
4713{
4714 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
4715 return -1;
4716
4717 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
4718 return -1;
4719
4720 return p2p_ext_listen(wpa_s->global->p2p, period, interval);
4721}
4722
4723
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004724static int wpas_p2p_is_client(struct wpa_supplicant *wpa_s)
4725{
4726 return wpa_s->current_ssid != NULL &&
4727 wpa_s->current_ssid->p2p_group &&
4728 wpa_s->current_ssid->mode == WPAS_MODE_INFRA;
4729}
4730
4731
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004732static void wpas_p2p_group_idle_timeout(void *eloop_ctx, void *timeout_ctx)
4733{
4734 struct wpa_supplicant *wpa_s = eloop_ctx;
4735
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004736 if (wpa_s->conf->p2p_group_idle == 0 && !wpas_p2p_is_client(wpa_s)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004737 wpa_printf(MSG_DEBUG, "P2P: Ignore group idle timeout - "
4738 "disabled");
4739 return;
4740 }
4741
Dmitry Shmidt04949592012-07-19 12:16:46 -07004742 wpa_printf(MSG_DEBUG, "P2P: Group idle timeout reached - terminate "
4743 "group");
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004744 wpas_p2p_group_delete(wpa_s, P2P_GROUP_REMOVAL_IDLE_TIMEOUT);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004745}
4746
4747
4748static void wpas_p2p_set_group_idle_timeout(struct wpa_supplicant *wpa_s)
4749{
Dmitry Shmidt04949592012-07-19 12:16:46 -07004750 int timeout;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004751
Dmitry Shmidt04949592012-07-19 12:16:46 -07004752 if (eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL) > 0)
4753 wpa_printf(MSG_DEBUG, "P2P: Cancelled P2P group idle timeout");
4754
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004755 if (wpa_s->current_ssid == NULL || !wpa_s->current_ssid->p2p_group)
4756 return;
4757
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004758 timeout = wpa_s->conf->p2p_group_idle;
4759 if (wpa_s->current_ssid->mode == WPAS_MODE_INFRA &&
4760 (timeout == 0 || timeout > P2P_MAX_CLIENT_IDLE))
4761 timeout = P2P_MAX_CLIENT_IDLE;
4762
4763 if (timeout == 0)
4764 return;
4765
Dmitry Shmidt04949592012-07-19 12:16:46 -07004766 if (timeout < 0) {
4767 if (wpa_s->current_ssid->mode == WPAS_MODE_INFRA)
4768 timeout = 0; /* special client mode no-timeout */
4769 else
4770 return;
4771 }
4772
4773 if (wpa_s->p2p_in_provisioning) {
4774 /*
4775 * Use the normal group formation timeout during the
4776 * provisioning phase to avoid terminating this process too
4777 * early due to group idle timeout.
4778 */
4779 wpa_printf(MSG_DEBUG, "P2P: Do not use P2P group idle timeout "
4780 "during provisioning");
4781 return;
4782 }
Dmitry Shmidtad266fb2012-08-24 17:03:35 -07004783#ifndef ANDROID_P2P
Dmitry Shmidt04949592012-07-19 12:16:46 -07004784 if (wpa_s->show_group_started) {
4785 /*
4786 * Use the normal group formation timeout between the end of
4787 * the provisioning phase and completion of 4-way handshake to
4788 * avoid terminating this process too early due to group idle
4789 * timeout.
4790 */
4791 wpa_printf(MSG_DEBUG, "P2P: Do not use P2P group idle timeout "
4792 "while waiting for initial 4-way handshake to "
4793 "complete");
4794 return;
4795 }
Dmitry Shmidtad266fb2012-08-24 17:03:35 -07004796#endif
Dmitry Shmidt04949592012-07-19 12:16:46 -07004797
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004798 wpa_printf(MSG_DEBUG, "P2P: Set P2P group idle timeout to %u seconds",
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004799 timeout);
4800 eloop_register_timeout(timeout, 0, wpas_p2p_group_idle_timeout,
4801 wpa_s, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004802}
4803
4804
Jouni Malinen2b89da82012-08-31 22:04:41 +03004805/* Returns 1 if the interface was removed */
4806int wpas_p2p_deauth_notif(struct wpa_supplicant *wpa_s, const u8 *bssid,
4807 u16 reason_code, const u8 *ie, size_t ie_len,
4808 int locally_generated)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004809{
4810 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
Jouni Malinen2b89da82012-08-31 22:04:41 +03004811 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004812 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
Jouni Malinen2b89da82012-08-31 22:04:41 +03004813 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004814
Dmitry Shmidt04949592012-07-19 12:16:46 -07004815 if (!locally_generated)
4816 p2p_deauth_notif(wpa_s->global->p2p, bssid, reason_code, ie,
4817 ie_len);
4818
4819 if (reason_code == WLAN_REASON_DEAUTH_LEAVING && !locally_generated &&
4820 wpa_s->current_ssid &&
4821 wpa_s->current_ssid->p2p_group &&
4822 wpa_s->current_ssid->mode == WPAS_MODE_INFRA) {
4823 wpa_printf(MSG_DEBUG, "P2P: GO indicated that the P2P Group "
4824 "session is ending");
Jouni Malinen2b89da82012-08-31 22:04:41 +03004825 if (wpas_p2p_group_delete(wpa_s,
4826 P2P_GROUP_REMOVAL_GO_ENDING_SESSION)
4827 > 0)
4828 return 1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07004829 }
Jouni Malinen2b89da82012-08-31 22:04:41 +03004830
4831 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004832}
4833
4834
4835void wpas_p2p_disassoc_notif(struct wpa_supplicant *wpa_s, const u8 *bssid,
Dmitry Shmidt04949592012-07-19 12:16:46 -07004836 u16 reason_code, const u8 *ie, size_t ie_len,
4837 int locally_generated)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004838{
4839 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
4840 return;
4841 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
4842 return;
4843
Dmitry Shmidt04949592012-07-19 12:16:46 -07004844 if (!locally_generated)
4845 p2p_disassoc_notif(wpa_s->global->p2p, bssid, reason_code, ie,
4846 ie_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004847}
4848
4849
4850void wpas_p2p_update_config(struct wpa_supplicant *wpa_s)
4851{
4852 struct p2p_data *p2p = wpa_s->global->p2p;
4853
4854 if (p2p == NULL)
4855 return;
4856
4857 if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_CAPABLE))
4858 return;
4859
4860 if (wpa_s->conf->changed_parameters & CFG_CHANGED_DEVICE_NAME)
4861 p2p_set_dev_name(p2p, wpa_s->conf->device_name);
4862
4863 if (wpa_s->conf->changed_parameters & CFG_CHANGED_DEVICE_TYPE)
4864 p2p_set_pri_dev_type(p2p, wpa_s->conf->device_type);
4865
4866 if (wpa_s->wps &&
4867 (wpa_s->conf->changed_parameters & CFG_CHANGED_CONFIG_METHODS))
4868 p2p_set_config_methods(p2p, wpa_s->wps->config_methods);
4869
4870 if (wpa_s->wps && (wpa_s->conf->changed_parameters & CFG_CHANGED_UUID))
4871 p2p_set_uuid(p2p, wpa_s->wps->uuid);
4872
4873 if (wpa_s->conf->changed_parameters & CFG_CHANGED_WPS_STRING) {
4874 p2p_set_manufacturer(p2p, wpa_s->conf->manufacturer);
4875 p2p_set_model_name(p2p, wpa_s->conf->model_name);
4876 p2p_set_model_number(p2p, wpa_s->conf->model_number);
4877 p2p_set_serial_number(p2p, wpa_s->conf->serial_number);
4878 }
4879
4880 if (wpa_s->conf->changed_parameters & CFG_CHANGED_SEC_DEVICE_TYPE)
4881 p2p_set_sec_dev_types(p2p,
4882 (void *) wpa_s->conf->sec_device_type,
4883 wpa_s->conf->num_sec_device_types);
4884
4885 if (wpa_s->conf->changed_parameters & CFG_CHANGED_VENDOR_EXTENSION) {
4886 int i;
4887 p2p_remove_wps_vendor_extensions(p2p);
4888 for (i = 0; i < MAX_WPS_VENDOR_EXT; i++) {
4889 if (wpa_s->conf->wps_vendor_ext[i] == NULL)
4890 continue;
4891 p2p_add_wps_vendor_extension(
4892 p2p, wpa_s->conf->wps_vendor_ext[i]);
4893 }
4894 }
4895
4896 if ((wpa_s->conf->changed_parameters & CFG_CHANGED_COUNTRY) &&
4897 wpa_s->conf->country[0] && wpa_s->conf->country[1]) {
4898 char country[3];
4899 country[0] = wpa_s->conf->country[0];
4900 country[1] = wpa_s->conf->country[1];
4901 country[2] = 0x04;
4902 p2p_set_country(p2p, country);
4903 }
4904
4905 if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_SSID_POSTFIX) {
4906 p2p_set_ssid_postfix(p2p, (u8 *) wpa_s->conf->p2p_ssid_postfix,
4907 wpa_s->conf->p2p_ssid_postfix ?
4908 os_strlen(wpa_s->conf->p2p_ssid_postfix) :
4909 0);
4910 }
4911
4912 if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_INTRA_BSS)
4913 p2p_set_intra_bss_dist(p2p, wpa_s->conf->p2p_intra_bss);
Jouni Malinen75ecf522011-06-27 15:19:46 -07004914
4915 if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_LISTEN_CHANNEL) {
4916 u8 reg_class, channel;
4917 int ret;
4918 unsigned int r;
4919 if (wpa_s->conf->p2p_listen_reg_class &&
4920 wpa_s->conf->p2p_listen_channel) {
4921 reg_class = wpa_s->conf->p2p_listen_reg_class;
4922 channel = wpa_s->conf->p2p_listen_channel;
4923 } else {
4924 reg_class = 81;
4925 /*
4926 * Pick one of the social channels randomly as the
4927 * listen channel.
4928 */
4929 os_get_random((u8 *) &r, sizeof(r));
4930 channel = 1 + (r % 3) * 5;
4931 }
4932 ret = p2p_set_listen_channel(p2p, reg_class, channel);
4933 if (ret)
4934 wpa_printf(MSG_ERROR, "P2P: Own listen channel update "
4935 "failed: %d", ret);
4936 }
4937 if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_OPER_CHANNEL) {
4938 u8 op_reg_class, op_channel, cfg_op_channel;
4939 int ret = 0;
4940 unsigned int r;
4941 if (wpa_s->conf->p2p_oper_reg_class &&
4942 wpa_s->conf->p2p_oper_channel) {
4943 op_reg_class = wpa_s->conf->p2p_oper_reg_class;
4944 op_channel = wpa_s->conf->p2p_oper_channel;
4945 cfg_op_channel = 1;
4946 } else {
4947 op_reg_class = 81;
4948 /*
4949 * Use random operation channel from (1, 6, 11)
4950 *if no other preference is indicated.
4951 */
4952 os_get_random((u8 *) &r, sizeof(r));
4953 op_channel = 1 + (r % 3) * 5;
4954 cfg_op_channel = 0;
4955 }
4956 ret = p2p_set_oper_channel(p2p, op_reg_class, op_channel,
4957 cfg_op_channel);
4958 if (ret)
4959 wpa_printf(MSG_ERROR, "P2P: Own oper channel update "
4960 "failed: %d", ret);
4961 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07004962
4963 if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_PREF_CHAN) {
4964 if (p2p_set_pref_chan(p2p, wpa_s->conf->num_p2p_pref_chan,
4965 wpa_s->conf->p2p_pref_chan) < 0) {
4966 wpa_printf(MSG_ERROR, "P2P: Preferred channel list "
4967 "update failed");
4968 }
4969 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004970}
4971
4972
4973int wpas_p2p_set_noa(struct wpa_supplicant *wpa_s, u8 count, int start,
4974 int duration)
4975{
4976 if (!wpa_s->ap_iface)
4977 return -1;
4978 return hostapd_p2p_set_noa(wpa_s->ap_iface->bss[0], count, start,
4979 duration);
4980}
4981
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004982
4983int wpas_p2p_set_cross_connect(struct wpa_supplicant *wpa_s, int enabled)
4984{
4985 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
4986 return -1;
4987 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
4988 return -1;
4989
4990 wpa_s->global->cross_connection = enabled;
4991 p2p_set_cross_connect(wpa_s->global->p2p, enabled);
4992
4993 if (!enabled) {
4994 struct wpa_supplicant *iface;
4995
4996 for (iface = wpa_s->global->ifaces; iface; iface = iface->next)
4997 {
4998 if (iface->cross_connect_enabled == 0)
4999 continue;
5000
5001 iface->cross_connect_enabled = 0;
5002 iface->cross_connect_in_use = 0;
5003 wpa_msg(iface->parent, MSG_INFO,
5004 P2P_EVENT_CROSS_CONNECT_DISABLE "%s %s",
5005 iface->ifname, iface->cross_connect_uplink);
5006 }
5007 }
5008
5009 return 0;
5010}
5011
5012
5013static void wpas_p2p_enable_cross_connect(struct wpa_supplicant *uplink)
5014{
5015 struct wpa_supplicant *iface;
5016
5017 if (!uplink->global->cross_connection)
5018 return;
5019
5020 for (iface = uplink->global->ifaces; iface; iface = iface->next) {
5021 if (!iface->cross_connect_enabled)
5022 continue;
5023 if (os_strcmp(uplink->ifname, iface->cross_connect_uplink) !=
5024 0)
5025 continue;
5026 if (iface->ap_iface == NULL)
5027 continue;
5028 if (iface->cross_connect_in_use)
5029 continue;
5030
5031 iface->cross_connect_in_use = 1;
5032 wpa_msg(iface->parent, MSG_INFO,
5033 P2P_EVENT_CROSS_CONNECT_ENABLE "%s %s",
5034 iface->ifname, iface->cross_connect_uplink);
5035 }
5036}
5037
5038
5039static void wpas_p2p_disable_cross_connect(struct wpa_supplicant *uplink)
5040{
5041 struct wpa_supplicant *iface;
5042
5043 for (iface = uplink->global->ifaces; iface; iface = iface->next) {
5044 if (!iface->cross_connect_enabled)
5045 continue;
5046 if (os_strcmp(uplink->ifname, iface->cross_connect_uplink) !=
5047 0)
5048 continue;
5049 if (!iface->cross_connect_in_use)
5050 continue;
5051
5052 wpa_msg(iface->parent, MSG_INFO,
5053 P2P_EVENT_CROSS_CONNECT_DISABLE "%s %s",
5054 iface->ifname, iface->cross_connect_uplink);
5055 iface->cross_connect_in_use = 0;
5056 }
5057}
5058
5059
5060void wpas_p2p_notif_connected(struct wpa_supplicant *wpa_s)
5061{
5062 if (wpa_s->ap_iface || wpa_s->current_ssid == NULL ||
5063 wpa_s->current_ssid->mode != WPAS_MODE_INFRA ||
5064 wpa_s->cross_connect_disallowed)
5065 wpas_p2p_disable_cross_connect(wpa_s);
5066 else
5067 wpas_p2p_enable_cross_connect(wpa_s);
Dmitry Shmidt04949592012-07-19 12:16:46 -07005068 if (!wpa_s->ap_iface &&
5069 eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL) > 0)
5070 wpa_printf(MSG_DEBUG, "P2P: Cancelled P2P group idle timeout");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005071}
5072
5073
5074void wpas_p2p_notif_disconnected(struct wpa_supplicant *wpa_s)
5075{
5076 wpas_p2p_disable_cross_connect(wpa_s);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005077 if (!wpa_s->ap_iface &&
5078 !eloop_is_timeout_registered(wpas_p2p_group_idle_timeout,
5079 wpa_s, NULL))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005080 wpas_p2p_set_group_idle_timeout(wpa_s);
5081}
5082
5083
5084static void wpas_p2p_cross_connect_setup(struct wpa_supplicant *wpa_s)
5085{
5086 struct wpa_supplicant *iface;
5087
5088 if (!wpa_s->global->cross_connection)
5089 return;
5090
5091 for (iface = wpa_s->global->ifaces; iface; iface = iface->next) {
5092 if (iface == wpa_s)
5093 continue;
5094 if (iface->drv_flags &
5095 WPA_DRIVER_FLAGS_P2P_DEDICATED_INTERFACE)
5096 continue;
5097 if (iface->drv_flags & WPA_DRIVER_FLAGS_P2P_CAPABLE)
5098 continue;
5099
5100 wpa_s->cross_connect_enabled = 1;
5101 os_strlcpy(wpa_s->cross_connect_uplink, iface->ifname,
5102 sizeof(wpa_s->cross_connect_uplink));
5103 wpa_printf(MSG_DEBUG, "P2P: Enable cross connection from "
5104 "%s to %s whenever uplink is available",
5105 wpa_s->ifname, wpa_s->cross_connect_uplink);
5106
5107 if (iface->ap_iface || iface->current_ssid == NULL ||
5108 iface->current_ssid->mode != WPAS_MODE_INFRA ||
5109 iface->cross_connect_disallowed ||
5110 iface->wpa_state != WPA_COMPLETED)
5111 break;
5112
5113 wpa_s->cross_connect_in_use = 1;
5114 wpa_msg(wpa_s->parent, MSG_INFO,
5115 P2P_EVENT_CROSS_CONNECT_ENABLE "%s %s",
5116 wpa_s->ifname, wpa_s->cross_connect_uplink);
5117 break;
5118 }
5119}
5120
5121
5122int wpas_p2p_notif_pbc_overlap(struct wpa_supplicant *wpa_s)
5123{
5124 if (wpa_s->p2p_group_interface != P2P_GROUP_INTERFACE_CLIENT &&
5125 !wpa_s->p2p_in_provisioning)
5126 return 0; /* not P2P client operation */
5127
5128 wpa_printf(MSG_DEBUG, "P2P: Terminate connection due to WPS PBC "
5129 "session overlap");
5130 if (wpa_s != wpa_s->parent)
5131 wpa_msg_ctrl(wpa_s->parent, MSG_INFO, WPS_EVENT_OVERLAP);
5132
5133 if (wpa_s->global->p2p)
5134 p2p_group_formation_failed(wpa_s->global->p2p);
5135
5136 eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
5137 wpa_s->parent, NULL);
5138
5139 wpas_group_formation_completed(wpa_s, 0);
5140 return 1;
5141}
5142
5143
5144void wpas_p2p_update_channel_list(struct wpa_supplicant *wpa_s)
5145{
5146 struct p2p_channels chan;
5147
5148 if (wpa_s->global == NULL || wpa_s->global->p2p == NULL)
5149 return;
5150
5151 os_memset(&chan, 0, sizeof(chan));
5152 if (wpas_p2p_setup_channels(wpa_s, &chan)) {
5153 wpa_printf(MSG_ERROR, "P2P: Failed to update supported "
5154 "channel list");
5155 return;
5156 }
5157
5158 p2p_update_channel_list(wpa_s->global->p2p, &chan);
5159}
5160
5161
5162int wpas_p2p_cancel(struct wpa_supplicant *wpa_s)
5163{
5164 struct wpa_global *global = wpa_s->global;
5165 int found = 0;
5166 const u8 *peer;
5167
5168 if (global->p2p == NULL)
5169 return -1;
5170
5171 wpa_printf(MSG_DEBUG, "P2P: Request to cancel group formation");
5172
5173 if (wpa_s->pending_interface_name[0] &&
5174 !is_zero_ether_addr(wpa_s->pending_interface_addr))
5175 found = 1;
5176
5177 peer = p2p_get_go_neg_peer(global->p2p);
5178 if (peer) {
5179 wpa_printf(MSG_DEBUG, "P2P: Unauthorize pending GO Neg peer "
5180 MACSTR, MAC2STR(peer));
5181 p2p_unauthorize(global->p2p, peer);
Dmitry Shmidt04949592012-07-19 12:16:46 -07005182 found = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005183 }
5184
5185 wpas_p2p_stop_find(wpa_s);
5186
5187 for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
5188 if (wpa_s == global->p2p_group_formation &&
5189 (wpa_s->p2p_in_provisioning ||
5190 wpa_s->parent->pending_interface_type ==
5191 WPA_IF_P2P_CLIENT)) {
5192 wpa_printf(MSG_DEBUG, "P2P: Interface %s in group "
5193 "formation found - cancelling",
5194 wpa_s->ifname);
5195 found = 1;
5196 eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
5197 wpa_s->parent, NULL);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005198 wpas_p2p_group_delete(wpa_s,
5199 P2P_GROUP_REMOVAL_REQUESTED);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005200 break;
5201 }
5202 }
5203
5204 if (!found) {
5205 wpa_printf(MSG_DEBUG, "P2P: No ongoing group formation found");
5206 return -1;
5207 }
5208
5209 return 0;
5210}
5211
5212
5213void wpas_p2p_interface_unavailable(struct wpa_supplicant *wpa_s)
5214{
5215 if (wpa_s->current_ssid == NULL || !wpa_s->current_ssid->p2p_group)
5216 return;
5217
5218 wpa_printf(MSG_DEBUG, "P2P: Remove group due to driver resource not "
5219 "being available anymore");
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005220 wpas_p2p_group_delete(wpa_s, P2P_GROUP_REMOVAL_UNAVAILABLE);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005221}
5222
5223
5224void wpas_p2p_update_best_channels(struct wpa_supplicant *wpa_s,
5225 int freq_24, int freq_5, int freq_overall)
5226{
5227 struct p2p_data *p2p = wpa_s->global->p2p;
5228 if (p2p == NULL || (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT))
5229 return;
5230 p2p_set_best_channels(p2p, freq_24, freq_5, freq_overall);
5231}
5232
5233
5234int wpas_p2p_unauthorize(struct wpa_supplicant *wpa_s, const char *addr)
5235{
5236 u8 peer[ETH_ALEN];
5237 struct p2p_data *p2p = wpa_s->global->p2p;
5238
5239 if (p2p == NULL || (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT))
5240 return -1;
5241
5242 if (hwaddr_aton(addr, peer))
5243 return -1;
5244
5245 return p2p_unauthorize(p2p, peer);
5246}
5247
5248
5249/**
5250 * wpas_p2p_disconnect - Disconnect from a P2P Group
5251 * @wpa_s: Pointer to wpa_supplicant data
5252 * Returns: 0 on success, -1 on failure
5253 *
5254 * This can be used to disconnect from a group in which the local end is a P2P
5255 * Client or to end a P2P Group in case the local end is the Group Owner. If a
5256 * virtual network interface was created for this group, that interface will be
5257 * removed. Otherwise, only the configured P2P group network will be removed
5258 * from the interface.
5259 */
5260int wpas_p2p_disconnect(struct wpa_supplicant *wpa_s)
5261{
5262
5263 if (wpa_s == NULL)
5264 return -1;
5265
Jouni Malinen2b89da82012-08-31 22:04:41 +03005266 return wpas_p2p_group_delete(wpa_s, P2P_GROUP_REMOVAL_REQUESTED) < 0 ?
5267 -1 : 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005268}
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005269
5270
5271int wpas_p2p_in_progress(struct wpa_supplicant *wpa_s)
5272{
5273 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
5274 return 0;
5275
5276 return p2p_in_progress(wpa_s->global->p2p);
5277}
5278
5279
5280void wpas_p2p_network_removed(struct wpa_supplicant *wpa_s,
5281 struct wpa_ssid *ssid)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005282{
5283 if (wpa_s->p2p_in_provisioning && ssid->p2p_group &&
5284 eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
5285 wpa_s->parent, NULL) > 0) {
Dmitry Shmidt04949592012-07-19 12:16:46 -07005286 /**
5287 * Remove the network by scheduling the group formation
5288 * timeout to happen immediately. The teardown code
5289 * needs to be scheduled to run asynch later so that we
5290 * don't delete data from under ourselves unexpectedly.
5291 * Calling wpas_p2p_group_formation_timeout directly
5292 * causes a series of crashes in WPS failure scenarios.
5293 */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005294 wpa_printf(MSG_DEBUG, "P2P: Canceled group formation due to "
5295 "P2P group network getting removed");
Dmitry Shmidt04949592012-07-19 12:16:46 -07005296 eloop_register_timeout(0, 0, wpas_p2p_group_formation_timeout,
5297 wpa_s->parent, NULL);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005298 }
5299}
5300
5301
5302struct wpa_ssid * wpas_p2p_get_persistent(struct wpa_supplicant *wpa_s,
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08005303 const u8 *addr, const u8 *ssid,
5304 size_t ssid_len)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005305{
5306 struct wpa_ssid *s;
5307 size_t i;
5308
5309 for (s = wpa_s->conf->ssid; s; s = s->next) {
5310 if (s->disabled != 2)
5311 continue;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08005312 if (ssid &&
5313 (ssid_len != s->ssid_len ||
5314 os_memcmp(ssid, s->ssid, ssid_len) != 0))
5315 continue;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005316 if (os_memcmp(s->bssid, addr, ETH_ALEN) == 0)
5317 return s; /* peer is GO in the persistent group */
5318 if (s->mode != WPAS_MODE_P2P_GO || s->p2p_client_list == NULL)
5319 continue;
5320 for (i = 0; i < s->num_p2p_clients; i++) {
5321 if (os_memcmp(s->p2p_client_list + i * ETH_ALEN,
5322 addr, ETH_ALEN) == 0)
5323 return s; /* peer is P2P client in persistent
5324 * group */
5325 }
5326 }
5327
5328 return NULL;
5329}
5330
5331
5332void wpas_p2p_notify_ap_sta_authorized(struct wpa_supplicant *wpa_s,
5333 const u8 *addr)
5334{
5335 if (addr == NULL)
5336 return;
5337 wpas_p2p_add_persistent_group_client(wpa_s, addr);
5338}
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08005339
Dmitry Shmidt04949592012-07-19 12:16:46 -07005340
5341static void wpas_p2p_fallback_to_go_neg(struct wpa_supplicant *wpa_s,
5342 int group_added)
5343{
5344 struct wpa_supplicant *group = wpa_s;
5345 eloop_cancel_timeout(wpas_p2p_pd_before_join_timeout, wpa_s, NULL);
5346 if (wpa_s->global->p2p_group_formation)
5347 group = wpa_s->global->p2p_group_formation;
5348 wpa_s = wpa_s->parent;
5349 offchannel_send_action_done(wpa_s);
5350 if (group_added)
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005351 wpas_p2p_group_delete(group, P2P_GROUP_REMOVAL_SILENT);
Dmitry Shmidt04949592012-07-19 12:16:46 -07005352 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Fall back to GO Negotiation");
5353 wpas_p2p_connect(wpa_s, wpa_s->pending_join_dev_addr, wpa_s->p2p_pin,
5354 wpa_s->p2p_wps_method, wpa_s->p2p_persistent_group, 0,
5355 0, 0, wpa_s->p2p_go_intent, wpa_s->p2p_connect_freq,
5356 wpa_s->p2p_persistent_id,
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005357 wpa_s->p2p_pd_before_go_neg,
5358 wpa_s->p2p_go_ht40);
Dmitry Shmidt04949592012-07-19 12:16:46 -07005359}
5360
5361
5362int wpas_p2p_scan_no_go_seen(struct wpa_supplicant *wpa_s)
5363{
5364 if (!wpa_s->p2p_fallback_to_go_neg ||
5365 wpa_s->p2p_in_provisioning <= 5)
5366 return 0;
5367
5368 if (wpas_p2p_peer_go(wpa_s, wpa_s->pending_join_dev_addr) > 0)
5369 return 0; /* peer operating as a GO */
5370
5371 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: GO not found for p2p_connect-auto - "
5372 "fallback to GO Negotiation");
5373 wpas_p2p_fallback_to_go_neg(wpa_s, 1);
5374
5375 return 1;
5376}
5377
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005378
5379unsigned int wpas_p2p_search_delay(struct wpa_supplicant *wpa_s)
5380{
5381 const char *rn, *rn2;
5382 struct wpa_supplicant *ifs;
5383
5384 if (wpa_s->wpa_state > WPA_SCANNING) {
5385 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Use %u ms search delay due to "
5386 "concurrent operation",
5387 P2P_CONCURRENT_SEARCH_DELAY);
5388 return P2P_CONCURRENT_SEARCH_DELAY;
5389 }
5390
5391 if (!wpa_s->driver->get_radio_name)
5392 return 0;
5393 rn = wpa_s->driver->get_radio_name(wpa_s->drv_priv);
5394 if (rn == NULL || rn[0] == '\0')
5395 return 0;
5396
5397 for (ifs = wpa_s->global->ifaces; ifs; ifs = ifs->next) {
5398 if (ifs == wpa_s || !ifs->driver->get_radio_name)
5399 continue;
5400
5401 rn2 = ifs->driver->get_radio_name(ifs->drv_priv);
5402 if (!rn2 || os_strcmp(rn, rn2) != 0)
5403 continue;
5404 if (ifs->wpa_state > WPA_SCANNING) {
5405 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Use %u ms search "
5406 "delay due to concurrent operation on "
5407 "interface %s",
5408 P2P_CONCURRENT_SEARCH_DELAY, ifs->ifname);
5409 return P2P_CONCURRENT_SEARCH_DELAY;
5410 }
5411 }
5412
5413 return 0;
5414}
5415
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08005416#ifdef ANDROID_P2P
5417int wpas_p2p_handle_frequency_conflicts(struct wpa_supplicant *wpa_s, int freq)
5418{
5419 struct wpa_supplicant *iface = NULL;
5420 struct p2p_data *p2p = wpa_s->global->p2p;
5421
5422 for (iface = wpa_s->global->ifaces; iface; iface = iface->next) {
5423 if((iface->p2p_group_interface) && (iface->current_ssid) &&
5424 (iface->current_ssid->frequency != freq)) {
5425
5426 if (iface->p2p_group_interface == P2P_GROUP_INTERFACE_GO) {
5427 /* Try to see whether we can move the GO. If it
5428 * is not possible, remove the GO interface
5429 */
5430 if(wpa_drv_switch_channel(iface, freq) == 0) {
5431 wpa_printf(MSG_ERROR, "P2P: GO Moved to freq(%d)", freq);
5432 iface->current_ssid->frequency = freq;
5433 continue;
5434 }
5435 }
5436
5437 /* If GO cannot be moved or if the conflicting interface is a
5438 * P2P Client, remove the interface depending up on the connection
5439 * priority */
Dmitry Shmidt687922c2012-03-26 14:02:32 -07005440 if(!wpas_is_p2p_prioritized(wpa_s)) {
Dmitry Shmidtf4f5db32012-09-11 14:36:56 -07005441 /* STA connection has priority over existing
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08005442 * P2P connection. So remove the interface */
Dmitry Shmidt687922c2012-03-26 14:02:32 -07005443 wpa_printf(MSG_DEBUG, "P2P: Removing P2P connection due to Single channel"
5444 "concurrent mode frequency conflict");
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005445 wpas_p2p_group_delete(iface, P2P_GROUP_REMOVAL_FREQ_CONFLICT);
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08005446 } else {
5447 /* Existing connection has the priority. Disable the newly
5448 * selected network and let the application know about it.
5449 */
5450 return -1;
5451 }
5452 }
5453 }
5454 return 0;
5455}
5456#endif