blob: f8db14edf2282227c6e9b5eb2b48a8c9e73d1db7 [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
270 * pending P2P group interface waiting for provisioning.
271 */
272 ssid = wpa_s->conf->ssid;
273 while (ssid) {
274 if (ssid->p2p_group &&
275 (ssid->mode == WPAS_MODE_P2P_GROUP_FORMATION ||
276 (ssid->key_mgmt & WPA_KEY_MGMT_WPS)))
277 break;
278 ssid = ssid->next;
279 }
Jouni Malinen5c44edb2012-08-31 21:35:32 +0300280 if (ssid == NULL &&
281 wpa_s->p2p_group_interface == NOT_P2P_GROUP_INTERFACE)
282 {
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700283 wpa_printf(MSG_ERROR, "P2P: P2P group interface "
284 "not found");
285 return -1;
286 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700287 }
288 if (wpa_s->p2p_group_interface == P2P_GROUP_INTERFACE_GO)
289 gtype = "GO";
290 else if (wpa_s->p2p_group_interface == P2P_GROUP_INTERFACE_CLIENT ||
291 (ssid && ssid->mode == WPAS_MODE_INFRA)) {
292 wpa_s->reassociate = 0;
293 wpa_s->disconnected = 1;
294 wpa_supplicant_deauthenticate(wpa_s,
295 WLAN_REASON_DEAUTH_LEAVING);
296 gtype = "client";
297 } else
298 gtype = "GO";
299 if (wpa_s->cross_connect_in_use) {
300 wpa_s->cross_connect_in_use = 0;
301 wpa_msg(wpa_s->parent, MSG_INFO,
302 P2P_EVENT_CROSS_CONNECT_DISABLE "%s %s",
303 wpa_s->ifname, wpa_s->cross_connect_uplink);
304 }
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700305 switch (removal_reason) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700306 case P2P_GROUP_REMOVAL_REQUESTED:
307 reason = " reason=REQUESTED";
308 break;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700309 case P2P_GROUP_REMOVAL_FORMATION_FAILED:
310 reason = " reason=FORMATION_FAILED";
311 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700312 case P2P_GROUP_REMOVAL_IDLE_TIMEOUT:
313 reason = " reason=IDLE";
314 break;
315 case P2P_GROUP_REMOVAL_UNAVAILABLE:
316 reason = " reason=UNAVAILABLE";
317 break;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700318 case P2P_GROUP_REMOVAL_GO_ENDING_SESSION:
319 reason = " reason=GO_ENDING_SESSION";
320 break;
Dmitry Shmidt687922c2012-03-26 14:02:32 -0700321#ifdef ANDROID_P2P
322 case P2P_GROUP_REMOVAL_FREQ_CONFLICT:
323 reason = " reason=FREQ_CONFLICT";
324 break;
325#endif
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700326 default:
327 reason = "";
328 break;
329 }
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700330 if (removal_reason != P2P_GROUP_REMOVAL_SILENT) {
Dmitry Shmidt04949592012-07-19 12:16:46 -0700331 wpa_msg(wpa_s->parent, MSG_INFO,
332 P2P_EVENT_GROUP_REMOVED "%s %s%s",
333 wpa_s->ifname, gtype, reason);
334 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700335
Dmitry Shmidt04949592012-07-19 12:16:46 -0700336 if (eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL) > 0)
337 wpa_printf(MSG_DEBUG, "P2P: Cancelled P2P group idle timeout");
338
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700339 if (removal_reason != P2P_GROUP_REMOVAL_SILENT && ssid)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700340 wpas_notify_p2p_group_removed(wpa_s, ssid, gtype);
341
342 if (wpa_s->p2p_group_interface != NOT_P2P_GROUP_INTERFACE) {
343 struct wpa_global *global;
344 char *ifname;
345 enum wpa_driver_if_type type;
346 wpa_printf(MSG_DEBUG, "P2P: Remove group interface %s",
347 wpa_s->ifname);
348 global = wpa_s->global;
349 ifname = os_strdup(wpa_s->ifname);
350 type = wpas_p2p_if_type(wpa_s->p2p_group_interface);
Dmitry Shmidte15c7b52011-08-03 15:04:35 -0700351 wpa_supplicant_remove_iface(wpa_s->global, wpa_s, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700352 wpa_s = global->ifaces;
353 if (wpa_s && ifname)
354 wpa_drv_if_remove(wpa_s, type, ifname);
355 os_free(ifname);
Jouni Malinen2b89da82012-08-31 22:04:41 +0300356 return 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700357 }
358
359 wpa_printf(MSG_DEBUG, "P2P: Remove temporary group network");
360 if (ssid && (ssid->p2p_group ||
361 ssid->mode == WPAS_MODE_P2P_GROUP_FORMATION ||
362 (ssid->key_mgmt & WPA_KEY_MGMT_WPS))) {
363 int id = ssid->id;
Jouni Malinen75ecf522011-06-27 15:19:46 -0700364 if (ssid == wpa_s->current_ssid) {
365 wpa_sm_set_config(wpa_s->wpa, NULL);
366 eapol_sm_notify_config(wpa_s->eapol, NULL, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700367 wpa_s->current_ssid = NULL;
Jouni Malinen75ecf522011-06-27 15:19:46 -0700368 }
369 /*
370 * Networks objects created during any P2P activities are not
371 * exposed out as they might/will confuse certain non-P2P aware
372 * applications since these network objects won't behave like
373 * regular ones.
374 *
375 * Likewise, we don't send out network removed signals for such
376 * network objects.
377 */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700378 wpa_config_remove_network(wpa_s->conf, id);
379 wpa_supplicant_clear_status(wpa_s);
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -0800380 wpa_supplicant_cancel_sched_scan(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700381 } else {
382 wpa_printf(MSG_DEBUG, "P2P: Temporary group network not "
383 "found");
384 }
Dmitry Shmidt04949592012-07-19 12:16:46 -0700385 if (wpa_s->ap_iface)
386 wpa_supplicant_ap_deinit(wpa_s);
387 else
388 wpa_drv_deinit_p2p_cli(wpa_s);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700389
390 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700391}
392
393
394static int wpas_p2p_persistent_group(struct wpa_supplicant *wpa_s,
395 u8 *go_dev_addr,
396 const u8 *ssid, size_t ssid_len)
397{
398 struct wpa_bss *bss;
399 const u8 *bssid;
400 struct wpabuf *p2p;
401 u8 group_capab;
402 const u8 *addr;
403
404 if (wpa_s->go_params)
405 bssid = wpa_s->go_params->peer_interface_addr;
406 else
407 bssid = wpa_s->bssid;
408
409 bss = wpa_bss_get(wpa_s, bssid, ssid, ssid_len);
410 if (bss == NULL) {
411 u8 iface_addr[ETH_ALEN];
412 if (p2p_get_interface_addr(wpa_s->global->p2p, bssid,
413 iface_addr) == 0)
414 bss = wpa_bss_get(wpa_s, iface_addr, ssid, ssid_len);
415 }
416 if (bss == NULL) {
417 wpa_printf(MSG_DEBUG, "P2P: Could not figure out whether "
418 "group is persistent - BSS " MACSTR " not found",
419 MAC2STR(bssid));
420 return 0;
421 }
422
423 p2p = wpa_bss_get_vendor_ie_multi(bss, P2P_IE_VENDOR_TYPE);
424 if (p2p == NULL) {
425 wpa_printf(MSG_DEBUG, "P2P: Could not figure out whether "
426 "group is persistent - BSS " MACSTR
427 " did not include P2P IE", MAC2STR(bssid));
428 wpa_hexdump(MSG_DEBUG, "P2P: Probe Response IEs",
429 (u8 *) (bss + 1), bss->ie_len);
430 wpa_hexdump(MSG_DEBUG, "P2P: Beacon IEs",
431 ((u8 *) bss + 1) + bss->ie_len,
432 bss->beacon_ie_len);
433 return 0;
434 }
435
436 group_capab = p2p_get_group_capab(p2p);
437 addr = p2p_get_go_dev_addr(p2p);
438 wpa_printf(MSG_DEBUG, "P2P: Checking whether group is persistent: "
439 "group_capab=0x%x", group_capab);
440 if (addr) {
441 os_memcpy(go_dev_addr, addr, ETH_ALEN);
442 wpa_printf(MSG_DEBUG, "P2P: GO Device Address " MACSTR,
443 MAC2STR(addr));
444 } else
445 os_memset(go_dev_addr, 0, ETH_ALEN);
446 wpabuf_free(p2p);
447
448 wpa_printf(MSG_DEBUG, "P2P: BSS " MACSTR " group_capab=0x%x "
449 "go_dev_addr=" MACSTR,
450 MAC2STR(bssid), group_capab, MAC2STR(go_dev_addr));
451
452 return group_capab & P2P_GROUP_CAPAB_PERSISTENT_GROUP;
453}
454
455
Jouni Malinen75ecf522011-06-27 15:19:46 -0700456static int wpas_p2p_store_persistent_group(struct wpa_supplicant *wpa_s,
457 struct wpa_ssid *ssid,
458 const u8 *go_dev_addr)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700459{
460 struct wpa_ssid *s;
461 int changed = 0;
462
463 wpa_printf(MSG_DEBUG, "P2P: Storing credentials for a persistent "
464 "group (GO Dev Addr " MACSTR ")", MAC2STR(go_dev_addr));
465 for (s = wpa_s->conf->ssid; s; s = s->next) {
466 if (s->disabled == 2 &&
467 os_memcmp(go_dev_addr, s->bssid, ETH_ALEN) == 0 &&
468 s->ssid_len == ssid->ssid_len &&
469 os_memcmp(ssid->ssid, s->ssid, ssid->ssid_len) == 0)
470 break;
471 }
472
473 if (s) {
474 wpa_printf(MSG_DEBUG, "P2P: Update existing persistent group "
475 "entry");
476 if (ssid->passphrase && !s->passphrase)
477 changed = 1;
478 else if (ssid->passphrase && s->passphrase &&
479 os_strcmp(ssid->passphrase, s->passphrase) != 0)
480 changed = 1;
481 } else {
482 wpa_printf(MSG_DEBUG, "P2P: Create a new persistent group "
483 "entry");
484 changed = 1;
485 s = wpa_config_add_network(wpa_s->conf);
486 if (s == NULL)
Jouni Malinen75ecf522011-06-27 15:19:46 -0700487 return -1;
488
489 /*
490 * Instead of network_added we emit persistent_group_added
491 * notification. Also to keep the defense checks in
492 * persistent_group obj registration method, we set the
493 * relevant flags in s to designate it as a persistent group.
494 */
495 s->p2p_group = 1;
496 s->p2p_persistent_group = 1;
497 wpas_notify_persistent_group_added(wpa_s, s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700498 wpa_config_set_network_defaults(s);
499 }
500
501 s->p2p_group = 1;
502 s->p2p_persistent_group = 1;
503 s->disabled = 2;
504 s->bssid_set = 1;
505 os_memcpy(s->bssid, go_dev_addr, ETH_ALEN);
506 s->mode = ssid->mode;
507 s->auth_alg = WPA_AUTH_ALG_OPEN;
508 s->key_mgmt = WPA_KEY_MGMT_PSK;
509 s->proto = WPA_PROTO_RSN;
510 s->pairwise_cipher = WPA_CIPHER_CCMP;
511 s->export_keys = 1;
512 if (ssid->passphrase) {
513 os_free(s->passphrase);
514 s->passphrase = os_strdup(ssid->passphrase);
515 }
516 if (ssid->psk_set) {
517 s->psk_set = 1;
518 os_memcpy(s->psk, ssid->psk, 32);
519 }
520 if (s->passphrase && !s->psk_set)
521 wpa_config_update_psk(s);
522 if (s->ssid == NULL || s->ssid_len < ssid->ssid_len) {
523 os_free(s->ssid);
524 s->ssid = os_malloc(ssid->ssid_len);
525 }
526 if (s->ssid) {
527 s->ssid_len = ssid->ssid_len;
528 os_memcpy(s->ssid, ssid->ssid, s->ssid_len);
529 }
530
531#ifndef CONFIG_NO_CONFIG_WRITE
532 if (changed && wpa_s->conf->update_config &&
533 wpa_config_write(wpa_s->confname, wpa_s->conf)) {
534 wpa_printf(MSG_DEBUG, "P2P: Failed to update configuration");
535 }
536#endif /* CONFIG_NO_CONFIG_WRITE */
Jouni Malinen75ecf522011-06-27 15:19:46 -0700537
538 return s->id;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700539}
540
541
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800542static void wpas_p2p_add_persistent_group_client(struct wpa_supplicant *wpa_s,
543 const u8 *addr)
544{
545 struct wpa_ssid *ssid, *s;
546 u8 *n;
547 size_t i;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700548 int found = 0;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800549
550 ssid = wpa_s->current_ssid;
551 if (ssid == NULL || ssid->mode != WPAS_MODE_P2P_GO ||
552 !ssid->p2p_persistent_group)
553 return;
554
555 for (s = wpa_s->parent->conf->ssid; s; s = s->next) {
556 if (s->disabled != 2 || s->mode != WPAS_MODE_P2P_GO)
557 continue;
558
559 if (s->ssid_len == ssid->ssid_len &&
560 os_memcmp(s->ssid, ssid->ssid, s->ssid_len) == 0)
561 break;
562 }
563
564 if (s == NULL)
565 return;
566
567 for (i = 0; s->p2p_client_list && i < s->num_p2p_clients; i++) {
568 if (os_memcmp(s->p2p_client_list + i * ETH_ALEN, addr,
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700569 ETH_ALEN) != 0)
570 continue;
571
572 if (i == s->num_p2p_clients - 1)
573 return; /* already the most recent entry */
574
575 /* move the entry to mark it most recent */
576 os_memmove(s->p2p_client_list + i * ETH_ALEN,
577 s->p2p_client_list + (i + 1) * ETH_ALEN,
578 (s->num_p2p_clients - i - 1) * ETH_ALEN);
579 os_memcpy(s->p2p_client_list +
580 (s->num_p2p_clients - 1) * ETH_ALEN, addr, ETH_ALEN);
581 found = 1;
582 break;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800583 }
584
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700585 if (!found && s->num_p2p_clients < P2P_MAX_STORED_CLIENTS) {
586 n = os_realloc_array(s->p2p_client_list,
587 s->num_p2p_clients + 1, ETH_ALEN);
588 if (n == NULL)
589 return;
590 os_memcpy(n + s->num_p2p_clients * ETH_ALEN, addr, ETH_ALEN);
591 s->p2p_client_list = n;
592 s->num_p2p_clients++;
593 } else if (!found) {
594 /* Not enough room for an additional entry - drop the oldest
595 * entry */
596 os_memmove(s->p2p_client_list,
597 s->p2p_client_list + ETH_ALEN,
598 (s->num_p2p_clients - 1) * ETH_ALEN);
599 os_memcpy(s->p2p_client_list +
600 (s->num_p2p_clients - 1) * ETH_ALEN,
601 addr, ETH_ALEN);
602 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800603
604#ifndef CONFIG_NO_CONFIG_WRITE
605 if (wpa_s->parent->conf->update_config &&
606 wpa_config_write(wpa_s->parent->confname, wpa_s->parent->conf))
607 wpa_printf(MSG_DEBUG, "P2P: Failed to update configuration");
608#endif /* CONFIG_NO_CONFIG_WRITE */
609}
610
611
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700612static void wpas_group_formation_completed(struct wpa_supplicant *wpa_s,
613 int success)
614{
615 struct wpa_ssid *ssid;
616 const char *ssid_txt;
617 int client;
618 int persistent;
619 u8 go_dev_addr[ETH_ALEN];
Jouni Malinen75ecf522011-06-27 15:19:46 -0700620 int network_id = -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700621
622 /*
623 * This callback is likely called for the main interface. Update wpa_s
624 * to use the group interface if a new interface was created for the
625 * group.
626 */
627 if (wpa_s->global->p2p_group_formation)
628 wpa_s = wpa_s->global->p2p_group_formation;
629 wpa_s->global->p2p_group_formation = NULL;
630 wpa_s->p2p_in_provisioning = 0;
631
632 if (!success) {
633 wpa_msg(wpa_s->parent, MSG_INFO,
634 P2P_EVENT_GROUP_FORMATION_FAILURE);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700635 wpas_p2p_group_delete(wpa_s,
636 P2P_GROUP_REMOVAL_FORMATION_FAILED);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700637 return;
638 }
639
640 wpa_msg(wpa_s->parent, MSG_INFO, P2P_EVENT_GROUP_FORMATION_SUCCESS);
641
642 ssid = wpa_s->current_ssid;
643 if (ssid && ssid->mode == WPAS_MODE_P2P_GROUP_FORMATION) {
644 ssid->mode = WPAS_MODE_P2P_GO;
645 p2p_group_notif_formation_done(wpa_s->p2p_group);
646 wpa_supplicant_ap_mac_addr_filter(wpa_s, NULL);
647 }
648
649 persistent = 0;
650 if (ssid) {
651 ssid_txt = wpa_ssid_txt(ssid->ssid, ssid->ssid_len);
652 client = ssid->mode == WPAS_MODE_INFRA;
653 if (ssid->mode == WPAS_MODE_P2P_GO) {
654 persistent = ssid->p2p_persistent_group;
Dmitry Shmidt497c1d52011-07-21 15:19:46 -0700655 os_memcpy(go_dev_addr, wpa_s->global->p2p_dev_addr,
656 ETH_ALEN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700657 } else
658 persistent = wpas_p2p_persistent_group(wpa_s,
659 go_dev_addr,
660 ssid->ssid,
661 ssid->ssid_len);
662 } else {
663 ssid_txt = "";
664 client = wpa_s->p2p_group_interface ==
665 P2P_GROUP_INTERFACE_CLIENT;
666 os_memset(go_dev_addr, 0, ETH_ALEN);
667 }
668
669 wpa_s->show_group_started = 0;
670 if (client) {
671 /*
672 * Indicate event only after successfully completed 4-way
673 * handshake, i.e., when the interface is ready for data
674 * packets.
675 */
676 wpa_s->show_group_started = 1;
Dmitry Shmidt4b86ea52012-09-04 11:06:50 -0700677#ifdef ANDROID_P2P
678 /* For client Second phase of Group formation (4-way handshake) can be still pending
679 * So we need to restore wpa_s->global->p2p_group_formation */
Dmitry Shmidta2854ab2012-09-10 16:15:47 -0700680 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 -0700681 wpa_s->global->p2p_group_formation = wpa_s;
682#endif
683
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700684 } else if (ssid && ssid->passphrase == NULL && ssid->psk_set) {
685 char psk[65];
686 wpa_snprintf_hex(psk, sizeof(psk), ssid->psk, 32);
687 wpa_msg(wpa_s->parent, MSG_INFO, P2P_EVENT_GROUP_STARTED
688 "%s GO ssid=\"%s\" freq=%d psk=%s go_dev_addr=" MACSTR
689 "%s",
690 wpa_s->ifname, ssid_txt, ssid->frequency, psk,
691 MAC2STR(go_dev_addr),
692 persistent ? " [PERSISTENT]" : "");
693 wpas_p2p_cross_connect_setup(wpa_s);
694 wpas_p2p_set_group_idle_timeout(wpa_s);
695 } else {
696 wpa_msg(wpa_s->parent, MSG_INFO, P2P_EVENT_GROUP_STARTED
697 "%s GO ssid=\"%s\" freq=%d passphrase=\"%s\" "
698 "go_dev_addr=" MACSTR "%s",
699 wpa_s->ifname, ssid_txt, ssid ? ssid->frequency : 0,
700 ssid && ssid->passphrase ? ssid->passphrase : "",
701 MAC2STR(go_dev_addr),
702 persistent ? " [PERSISTENT]" : "");
703 wpas_p2p_cross_connect_setup(wpa_s);
704 wpas_p2p_set_group_idle_timeout(wpa_s);
705 }
706
707 if (persistent)
Jouni Malinen75ecf522011-06-27 15:19:46 -0700708 network_id = wpas_p2p_store_persistent_group(wpa_s->parent,
709 ssid, go_dev_addr);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800710 if (network_id < 0 && ssid)
Jouni Malinen75ecf522011-06-27 15:19:46 -0700711 network_id = ssid->id;
712 if (!client)
713 wpas_notify_p2p_group_started(wpa_s, ssid, network_id, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700714}
715
716
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800717static void wpas_p2p_send_action_tx_status(struct wpa_supplicant *wpa_s,
718 unsigned int freq,
719 const u8 *dst, const u8 *src,
720 const u8 *bssid,
721 const u8 *data, size_t data_len,
722 enum offchannel_send_action_result
723 result)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700724{
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800725 enum p2p_send_action_result res = P2P_SEND_ACTION_SUCCESS;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700726
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700727 if (wpa_s->global->p2p == NULL || wpa_s->global->p2p_disabled)
728 return;
729 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
730 return;
731
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800732 switch (result) {
733 case OFFCHANNEL_SEND_ACTION_SUCCESS:
734 res = P2P_SEND_ACTION_SUCCESS;
735 break;
736 case OFFCHANNEL_SEND_ACTION_NO_ACK:
737 res = P2P_SEND_ACTION_NO_ACK;
738 break;
739 case OFFCHANNEL_SEND_ACTION_FAILED:
740 res = P2P_SEND_ACTION_FAILED;
741 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700742 }
743
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800744 p2p_send_action_cb(wpa_s->global->p2p, freq, dst, src, bssid, res);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700745
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -0800746 if (result != OFFCHANNEL_SEND_ACTION_SUCCESS &&
747 wpa_s->pending_pd_before_join &&
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800748 (os_memcmp(dst, wpa_s->pending_join_dev_addr, ETH_ALEN) == 0 ||
749 os_memcmp(dst, wpa_s->pending_join_iface_addr, ETH_ALEN) == 0)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700750 wpa_s->pending_pd_before_join = 0;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700751 if (wpa_s->p2p_fallback_to_go_neg) {
752 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: No ACK for PD Req "
753 "during p2p_connect-auto");
754 wpas_p2p_fallback_to_go_neg(wpa_s, 0);
755 return;
756 }
757
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700758 wpa_printf(MSG_DEBUG, "P2P: Starting pending "
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -0800759 "join-existing-group operation (no ACK for PD "
760 "Req)");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700761 wpas_p2p_join_start(wpa_s);
762 }
763}
764
765
766static int wpas_send_action(void *ctx, unsigned int freq, const u8 *dst,
767 const u8 *src, const u8 *bssid, const u8 *buf,
768 size_t len, unsigned int wait_time)
769{
770 struct wpa_supplicant *wpa_s = ctx;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800771 return offchannel_send_action(wpa_s, freq, dst, src, bssid, buf, len,
772 wait_time,
773 wpas_p2p_send_action_tx_status, 1);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700774}
775
776
777static void wpas_send_action_done(void *ctx)
778{
779 struct wpa_supplicant *wpa_s = ctx;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800780 offchannel_send_action_done(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700781}
782
783
784static int wpas_copy_go_neg_results(struct wpa_supplicant *wpa_s,
785 struct p2p_go_neg_results *params)
786{
787 if (wpa_s->go_params == NULL) {
788 wpa_s->go_params = os_malloc(sizeof(*params));
789 if (wpa_s->go_params == NULL)
790 return -1;
791 }
792 os_memcpy(wpa_s->go_params, params, sizeof(*params));
793 return 0;
794}
795
796
797static void wpas_start_wps_enrollee(struct wpa_supplicant *wpa_s,
798 struct p2p_go_neg_results *res)
799{
800 wpa_printf(MSG_DEBUG, "P2P: Start WPS Enrollee for peer " MACSTR,
801 MAC2STR(res->peer_interface_addr));
802 wpa_hexdump_ascii(MSG_DEBUG, "P2P: Start WPS Enrollee for SSID",
803 res->ssid, res->ssid_len);
804 wpa_supplicant_ap_deinit(wpa_s);
805 wpas_copy_go_neg_results(wpa_s, res);
806 if (res->wps_method == WPS_PBC)
807 wpas_wps_start_pbc(wpa_s, res->peer_interface_addr, 1);
808 else {
809 u16 dev_pw_id = DEV_PW_DEFAULT;
810 if (wpa_s->p2p_wps_method == WPS_PIN_KEYPAD)
811 dev_pw_id = DEV_PW_REGISTRAR_SPECIFIED;
812 wpas_wps_start_pin(wpa_s, res->peer_interface_addr,
813 wpa_s->p2p_pin, 1, dev_pw_id);
814 }
815}
816
817
818static void p2p_go_configured(void *ctx, void *data)
819{
820 struct wpa_supplicant *wpa_s = ctx;
821 struct p2p_go_neg_results *params = data;
822 struct wpa_ssid *ssid;
Jouni Malinen75ecf522011-06-27 15:19:46 -0700823 int network_id = -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700824
825 ssid = wpa_s->current_ssid;
826 if (ssid && ssid->mode == WPAS_MODE_P2P_GO) {
827 wpa_printf(MSG_DEBUG, "P2P: Group setup without provisioning");
828 if (wpa_s->global->p2p_group_formation == wpa_s)
829 wpa_s->global->p2p_group_formation = NULL;
830 wpa_msg(wpa_s->parent, MSG_INFO, P2P_EVENT_GROUP_STARTED
831 "%s GO ssid=\"%s\" freq=%d passphrase=\"%s\" "
832 "go_dev_addr=" MACSTR "%s",
833 wpa_s->ifname,
834 wpa_ssid_txt(ssid->ssid, ssid->ssid_len),
835 ssid->frequency,
836 params->passphrase ? params->passphrase : "",
Dmitry Shmidt497c1d52011-07-21 15:19:46 -0700837 MAC2STR(wpa_s->global->p2p_dev_addr),
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700838 params->persistent_group ? " [PERSISTENT]" : "");
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800839
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700840 if (params->persistent_group)
Jouni Malinen75ecf522011-06-27 15:19:46 -0700841 network_id = wpas_p2p_store_persistent_group(
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700842 wpa_s->parent, ssid,
Dmitry Shmidt497c1d52011-07-21 15:19:46 -0700843 wpa_s->global->p2p_dev_addr);
Jouni Malinen75ecf522011-06-27 15:19:46 -0700844 if (network_id < 0)
845 network_id = ssid->id;
846 wpas_notify_p2p_group_started(wpa_s, ssid, network_id, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700847 wpas_p2p_cross_connect_setup(wpa_s);
848 wpas_p2p_set_group_idle_timeout(wpa_s);
849 return;
850 }
851
852 wpa_printf(MSG_DEBUG, "P2P: Setting up WPS for GO provisioning");
853 if (wpa_supplicant_ap_mac_addr_filter(wpa_s,
854 params->peer_interface_addr)) {
855 wpa_printf(MSG_DEBUG, "P2P: Failed to setup MAC address "
856 "filtering");
857 return;
858 }
859 if (params->wps_method == WPS_PBC)
860 wpa_supplicant_ap_wps_pbc(wpa_s, params->peer_interface_addr,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800861 params->peer_device_addr);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700862 else if (wpa_s->p2p_pin[0])
863 wpa_supplicant_ap_wps_pin(wpa_s, params->peer_interface_addr,
864 wpa_s->p2p_pin, NULL, 0);
865 os_free(wpa_s->go_params);
866 wpa_s->go_params = NULL;
867}
868
869
870static void wpas_start_wps_go(struct wpa_supplicant *wpa_s,
871 struct p2p_go_neg_results *params,
872 int group_formation)
873{
874 struct wpa_ssid *ssid;
875
876 if (wpas_copy_go_neg_results(wpa_s, params) < 0)
877 return;
878
879 ssid = wpa_config_add_network(wpa_s->conf);
880 if (ssid == NULL)
881 return;
882
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800883 wpa_s->show_group_started = 0;
884
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700885 wpa_config_set_network_defaults(ssid);
886 ssid->temporary = 1;
887 ssid->p2p_group = 1;
888 ssid->p2p_persistent_group = params->persistent_group;
889 ssid->mode = group_formation ? WPAS_MODE_P2P_GROUP_FORMATION :
890 WPAS_MODE_P2P_GO;
891 ssid->frequency = params->freq;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700892 ssid->ht40 = params->ht40;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700893 ssid->ssid = os_zalloc(params->ssid_len + 1);
894 if (ssid->ssid) {
895 os_memcpy(ssid->ssid, params->ssid, params->ssid_len);
896 ssid->ssid_len = params->ssid_len;
897 }
898 ssid->auth_alg = WPA_AUTH_ALG_OPEN;
899 ssid->key_mgmt = WPA_KEY_MGMT_PSK;
900 ssid->proto = WPA_PROTO_RSN;
901 ssid->pairwise_cipher = WPA_CIPHER_CCMP;
902 ssid->passphrase = os_strdup(params->passphrase);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700903 ssid->ap_max_inactivity = wpa_s->parent->conf->p2p_go_max_inactivity;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700904
905 wpa_s->ap_configured_cb = p2p_go_configured;
906 wpa_s->ap_configured_cb_ctx = wpa_s;
907 wpa_s->ap_configured_cb_data = wpa_s->go_params;
Jouni Malinen75ecf522011-06-27 15:19:46 -0700908 wpa_s->connect_without_scan = ssid;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700909 wpa_s->reassociate = 1;
910 wpa_s->disconnected = 0;
911 wpa_supplicant_req_scan(wpa_s, 0, 0);
912}
913
914
915static void wpas_p2p_clone_config(struct wpa_supplicant *dst,
916 const struct wpa_supplicant *src)
917{
918 struct wpa_config *d;
919 const struct wpa_config *s;
920
921 d = dst->conf;
922 s = src->conf;
923
924#define C(n) if (s->n) d->n = os_strdup(s->n)
925 C(device_name);
926 C(manufacturer);
927 C(model_name);
928 C(model_number);
929 C(serial_number);
930 C(config_methods);
931#undef C
932
933 os_memcpy(d->device_type, s->device_type, WPS_DEV_TYPE_LEN);
934 os_memcpy(d->sec_device_type, s->sec_device_type,
935 sizeof(d->sec_device_type));
936 d->num_sec_device_types = s->num_sec_device_types;
937
938 d->p2p_group_idle = s->p2p_group_idle;
939 d->p2p_intra_bss = s->p2p_intra_bss;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800940 d->persistent_reconnect = s->persistent_reconnect;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700941 d->max_num_sta = s->max_num_sta;
942 d->pbc_in_m1 = s->pbc_in_m1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700943}
944
945
946static int wpas_p2p_add_group_interface(struct wpa_supplicant *wpa_s,
947 enum wpa_driver_if_type type)
948{
949 char ifname[120], force_ifname[120];
950
951 if (wpa_s->pending_interface_name[0]) {
952 wpa_printf(MSG_DEBUG, "P2P: Pending virtual interface exists "
953 "- skip creation of a new one");
954 if (is_zero_ether_addr(wpa_s->pending_interface_addr)) {
955 wpa_printf(MSG_DEBUG, "P2P: Pending virtual address "
956 "unknown?! ifname='%s'",
957 wpa_s->pending_interface_name);
958 return -1;
959 }
960 return 0;
961 }
962
963 os_snprintf(ifname, sizeof(ifname), "p2p-%s-%d", wpa_s->ifname,
964 wpa_s->p2p_group_idx);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800965 if (os_strlen(ifname) >= IFNAMSIZ &&
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700966 os_strlen(wpa_s->ifname) < IFNAMSIZ) {
967 /* Try to avoid going over the IFNAMSIZ length limit */
968 os_snprintf(ifname, sizeof(ifname), "p2p-%d",
969 wpa_s->p2p_group_idx);
970 }
971 force_ifname[0] = '\0';
972
973 wpa_printf(MSG_DEBUG, "P2P: Create a new interface %s for the group",
974 ifname);
975 wpa_s->p2p_group_idx++;
976
977 wpa_s->pending_interface_type = type;
978 if (wpa_drv_if_add(wpa_s, type, ifname, NULL, NULL, force_ifname,
979 wpa_s->pending_interface_addr, NULL) < 0) {
980 wpa_printf(MSG_ERROR, "P2P: Failed to create new group "
981 "interface");
982 return -1;
983 }
984
985 if (force_ifname[0]) {
986 wpa_printf(MSG_DEBUG, "P2P: Driver forced interface name %s",
987 force_ifname);
988 os_strlcpy(wpa_s->pending_interface_name, force_ifname,
989 sizeof(wpa_s->pending_interface_name));
990 } else
991 os_strlcpy(wpa_s->pending_interface_name, ifname,
992 sizeof(wpa_s->pending_interface_name));
993 wpa_printf(MSG_DEBUG, "P2P: Created pending virtual interface %s addr "
994 MACSTR, wpa_s->pending_interface_name,
995 MAC2STR(wpa_s->pending_interface_addr));
996
997 return 0;
998}
999
1000
1001static void wpas_p2p_remove_pending_group_interface(
1002 struct wpa_supplicant *wpa_s)
1003{
1004 if (!wpa_s->pending_interface_name[0] ||
1005 is_zero_ether_addr(wpa_s->pending_interface_addr))
1006 return; /* No pending virtual interface */
1007
1008 wpa_printf(MSG_DEBUG, "P2P: Removing pending group interface %s",
1009 wpa_s->pending_interface_name);
1010 wpa_drv_if_remove(wpa_s, wpa_s->pending_interface_type,
1011 wpa_s->pending_interface_name);
1012 os_memset(wpa_s->pending_interface_addr, 0, ETH_ALEN);
1013 wpa_s->pending_interface_name[0] = '\0';
1014}
1015
1016
1017static struct wpa_supplicant *
1018wpas_p2p_init_group_interface(struct wpa_supplicant *wpa_s, int go)
1019{
1020 struct wpa_interface iface;
1021 struct wpa_supplicant *group_wpa_s;
1022
1023 if (!wpa_s->pending_interface_name[0]) {
1024 wpa_printf(MSG_ERROR, "P2P: No pending group interface");
1025 if (!wpas_p2p_create_iface(wpa_s))
1026 return NULL;
1027 /*
1028 * Something has forced us to remove the pending interface; try
1029 * to create a new one and hope for the best that we will get
1030 * the same local address.
1031 */
1032 if (wpas_p2p_add_group_interface(wpa_s, go ? WPA_IF_P2P_GO :
1033 WPA_IF_P2P_CLIENT) < 0)
1034 return NULL;
1035 }
1036
1037 os_memset(&iface, 0, sizeof(iface));
1038 iface.ifname = wpa_s->pending_interface_name;
1039 iface.driver = wpa_s->driver->name;
1040 iface.ctrl_interface = wpa_s->conf->ctrl_interface;
1041 iface.driver_param = wpa_s->conf->driver_param;
1042 group_wpa_s = wpa_supplicant_add_iface(wpa_s->global, &iface);
1043 if (group_wpa_s == NULL) {
1044 wpa_printf(MSG_ERROR, "P2P: Failed to create new "
1045 "wpa_supplicant interface");
1046 return NULL;
1047 }
1048 wpa_s->pending_interface_name[0] = '\0';
1049 group_wpa_s->parent = wpa_s;
1050 group_wpa_s->p2p_group_interface = go ? P2P_GROUP_INTERFACE_GO :
1051 P2P_GROUP_INTERFACE_CLIENT;
1052 wpa_s->global->p2p_group_formation = group_wpa_s;
1053
1054 wpas_p2p_clone_config(group_wpa_s, wpa_s);
1055
1056 return group_wpa_s;
1057}
1058
1059
1060static void wpas_p2p_group_formation_timeout(void *eloop_ctx,
1061 void *timeout_ctx)
1062{
1063 struct wpa_supplicant *wpa_s = eloop_ctx;
1064 wpa_printf(MSG_DEBUG, "P2P: Group Formation timed out");
1065 if (wpa_s->global->p2p)
1066 p2p_group_formation_failed(wpa_s->global->p2p);
1067 else if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
1068 wpa_drv_p2p_group_formation_failed(wpa_s);
1069 wpas_group_formation_completed(wpa_s, 0);
1070}
1071
1072
1073void wpas_go_neg_completed(void *ctx, struct p2p_go_neg_results *res)
1074{
1075 struct wpa_supplicant *wpa_s = ctx;
1076
1077 if (wpa_s->off_channel_freq || wpa_s->roc_waiting_drv_freq) {
1078 wpa_drv_cancel_remain_on_channel(wpa_s);
1079 wpa_s->off_channel_freq = 0;
1080 wpa_s->roc_waiting_drv_freq = 0;
1081 }
1082
1083 if (res->status) {
1084 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_GO_NEG_FAILURE "status=%d",
1085 res->status);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001086 wpas_notify_p2p_go_neg_completed(wpa_s, res);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001087 wpas_p2p_remove_pending_group_interface(wpa_s);
1088 return;
1089 }
1090
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001091 if (wpa_s->p2p_go_ht40)
1092 res->ht40 = 1;
1093
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001094 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_GO_NEG_SUCCESS);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001095 wpas_notify_p2p_go_neg_completed(wpa_s, res);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001096
Dmitry Shmidt04949592012-07-19 12:16:46 -07001097 if (res->role_go && wpa_s->p2p_persistent_id >= 0) {
1098 struct wpa_ssid *ssid;
1099 ssid = wpa_config_get_network(wpa_s->conf,
1100 wpa_s->p2p_persistent_id);
1101 if (ssid && ssid->disabled == 2 &&
1102 ssid->mode == WPAS_MODE_P2P_GO && ssid->passphrase) {
1103 size_t len = os_strlen(ssid->passphrase);
1104 wpa_printf(MSG_DEBUG, "P2P: Override passphrase based "
1105 "on requested persistent group");
1106 os_memcpy(res->passphrase, ssid->passphrase, len);
1107 res->passphrase[len] = '\0';
1108 }
1109 }
1110
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001111 if (wpa_s->create_p2p_iface) {
1112 struct wpa_supplicant *group_wpa_s =
1113 wpas_p2p_init_group_interface(wpa_s, res->role_go);
1114 if (group_wpa_s == NULL) {
1115 wpas_p2p_remove_pending_group_interface(wpa_s);
1116 return;
1117 }
1118 if (group_wpa_s != wpa_s) {
1119 os_memcpy(group_wpa_s->p2p_pin, wpa_s->p2p_pin,
1120 sizeof(group_wpa_s->p2p_pin));
1121 group_wpa_s->p2p_wps_method = wpa_s->p2p_wps_method;
1122 }
1123 os_memset(wpa_s->pending_interface_addr, 0, ETH_ALEN);
1124 wpa_s->pending_interface_name[0] = '\0';
1125 group_wpa_s->p2p_in_provisioning = 1;
1126
1127 if (res->role_go)
1128 wpas_start_wps_go(group_wpa_s, res, 1);
1129 else
1130 wpas_start_wps_enrollee(group_wpa_s, res);
1131 } else {
1132 wpa_s->p2p_in_provisioning = 1;
1133 wpa_s->global->p2p_group_formation = wpa_s;
1134
1135 if (res->role_go)
1136 wpas_start_wps_go(wpa_s, res, 1);
1137 else
1138 wpas_start_wps_enrollee(ctx, res);
1139 }
1140
1141 wpa_s->p2p_long_listen = 0;
1142 eloop_cancel_timeout(wpas_p2p_long_listen_timeout, wpa_s, NULL);
1143
1144 eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s, NULL);
1145 eloop_register_timeout(15 + res->peer_config_timeout / 100,
1146 (res->peer_config_timeout % 100) * 10000,
1147 wpas_p2p_group_formation_timeout, wpa_s, NULL);
1148}
1149
1150
1151void wpas_go_neg_req_rx(void *ctx, const u8 *src, u16 dev_passwd_id)
1152{
1153 struct wpa_supplicant *wpa_s = ctx;
1154 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_GO_NEG_REQUEST MACSTR
1155 " dev_passwd_id=%u", MAC2STR(src), dev_passwd_id);
1156
1157 wpas_notify_p2p_go_neg_req(wpa_s, src, dev_passwd_id);
1158}
1159
1160
1161void wpas_dev_found(void *ctx, const u8 *addr,
1162 const struct p2p_peer_info *info,
1163 int new_device)
1164{
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001165#ifndef CONFIG_NO_STDOUT_DEBUG
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001166 struct wpa_supplicant *wpa_s = ctx;
1167 char devtype[WPS_DEV_TYPE_BUFSIZE];
Irfan Sheriff8367dc92012-09-09 17:08:19 -07001168#define WFD_DEV_INFO_SIZE 9
1169 char wfd_dev_info_hex[2 * WFD_DEV_INFO_SIZE + 1];
Irfan Sheriff8d965182012-09-11 08:58:24 -07001170 os_memset(wfd_dev_info_hex, 0, sizeof(wfd_dev_info_hex));
Irfan Sheriff8367dc92012-09-09 17:08:19 -07001171#ifdef CONFIG_WIFI_DISPLAY
1172 if (info->wfd_subelems) {
1173 wpa_snprintf_hex(wfd_dev_info_hex, sizeof(wfd_dev_info_hex),
1174 wpabuf_head(info->wfd_subelems),
1175 WFD_DEV_INFO_SIZE);
1176 }
1177#endif /* CONFIG_WIFI_DISPLAY */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001178 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_DEVICE_FOUND MACSTR
1179 " p2p_dev_addr=" MACSTR
1180 " pri_dev_type=%s name='%s' config_methods=0x%x "
Irfan Sheriff8367dc92012-09-09 17:08:19 -07001181 "dev_capab=0x%x group_capab=0x%x%s%s",
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001182 MAC2STR(addr), MAC2STR(info->p2p_device_addr),
1183 wps_dev_type_bin2str(info->pri_dev_type, devtype,
1184 sizeof(devtype)),
1185 info->device_name, info->config_methods,
Irfan Sheriff8367dc92012-09-09 17:08:19 -07001186 info->dev_capab, info->group_capab,
1187 wfd_dev_info_hex[0] ? " wfd_dev_info=0x" : "", wfd_dev_info_hex);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001188#endif /* CONFIG_NO_STDOUT_DEBUG */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001189
1190 wpas_notify_p2p_device_found(ctx, info->p2p_device_addr, new_device);
1191}
1192
1193
1194static void wpas_dev_lost(void *ctx, const u8 *dev_addr)
1195{
1196 struct wpa_supplicant *wpa_s = ctx;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001197
Dmitry Shmidt497c1d52011-07-21 15:19:46 -07001198 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_DEVICE_LOST
1199 "p2p_dev_addr=" MACSTR, MAC2STR(dev_addr));
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001200
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001201 wpas_notify_p2p_device_lost(wpa_s, dev_addr);
1202}
1203
1204
1205static int wpas_start_listen(void *ctx, unsigned int freq,
1206 unsigned int duration,
1207 const struct wpabuf *probe_resp_ie)
1208{
1209 struct wpa_supplicant *wpa_s = ctx;
1210
1211 wpa_drv_set_ap_wps_ie(wpa_s, NULL, probe_resp_ie, NULL);
1212
1213 if (wpa_drv_probe_req_report(wpa_s, 1) < 0) {
1214 wpa_printf(MSG_DEBUG, "P2P: Failed to request the driver to "
1215 "report received Probe Request frames");
1216 return -1;
1217 }
1218
1219 wpa_s->pending_listen_freq = freq;
1220 wpa_s->pending_listen_duration = duration;
1221
1222 if (wpa_drv_remain_on_channel(wpa_s, freq, duration) < 0) {
1223 wpa_printf(MSG_DEBUG, "P2P: Failed to request the driver "
1224 "to remain on channel (%u MHz) for Listen "
1225 "state", freq);
1226 wpa_s->pending_listen_freq = 0;
1227 return -1;
1228 }
1229 wpa_s->off_channel_freq = 0;
1230 wpa_s->roc_waiting_drv_freq = freq;
1231
1232 return 0;
1233}
1234
1235
1236static void wpas_stop_listen(void *ctx)
1237{
1238 struct wpa_supplicant *wpa_s = ctx;
1239 if (wpa_s->off_channel_freq || wpa_s->roc_waiting_drv_freq) {
1240 wpa_drv_cancel_remain_on_channel(wpa_s);
1241 wpa_s->off_channel_freq = 0;
1242 wpa_s->roc_waiting_drv_freq = 0;
1243 }
1244 wpa_drv_set_ap_wps_ie(wpa_s, NULL, NULL, NULL);
1245 wpa_drv_probe_req_report(wpa_s, 0);
1246}
1247
1248
1249static int wpas_send_probe_resp(void *ctx, const struct wpabuf *buf)
1250{
1251 struct wpa_supplicant *wpa_s = ctx;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001252 return wpa_drv_send_mlme(wpa_s, wpabuf_head(buf), wpabuf_len(buf), 1);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001253}
1254
1255
1256static struct p2p_srv_bonjour *
1257wpas_p2p_service_get_bonjour(struct wpa_supplicant *wpa_s,
1258 const struct wpabuf *query)
1259{
1260 struct p2p_srv_bonjour *bsrv;
1261 size_t len;
1262
1263 len = wpabuf_len(query);
1264 dl_list_for_each(bsrv, &wpa_s->global->p2p_srv_bonjour,
1265 struct p2p_srv_bonjour, list) {
1266 if (len == wpabuf_len(bsrv->query) &&
1267 os_memcmp(wpabuf_head(query), wpabuf_head(bsrv->query),
1268 len) == 0)
1269 return bsrv;
1270 }
1271 return NULL;
1272}
1273
1274
1275static struct p2p_srv_upnp *
1276wpas_p2p_service_get_upnp(struct wpa_supplicant *wpa_s, u8 version,
1277 const char *service)
1278{
1279 struct p2p_srv_upnp *usrv;
1280
1281 dl_list_for_each(usrv, &wpa_s->global->p2p_srv_upnp,
1282 struct p2p_srv_upnp, list) {
1283 if (version == usrv->version &&
1284 os_strcmp(service, usrv->service) == 0)
1285 return usrv;
1286 }
1287 return NULL;
1288}
1289
1290
1291static void wpas_sd_add_proto_not_avail(struct wpabuf *resp, u8 srv_proto,
1292 u8 srv_trans_id)
1293{
1294 u8 *len_pos;
1295
1296 if (wpabuf_tailroom(resp) < 5)
1297 return;
1298
1299 /* Length (to be filled) */
1300 len_pos = wpabuf_put(resp, 2);
1301 wpabuf_put_u8(resp, srv_proto);
1302 wpabuf_put_u8(resp, srv_trans_id);
1303 /* Status Code */
1304 wpabuf_put_u8(resp, P2P_SD_PROTO_NOT_AVAILABLE);
1305 /* Response Data: empty */
1306 WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos - 2);
1307}
1308
1309
1310static void wpas_sd_all_bonjour(struct wpa_supplicant *wpa_s,
1311 struct wpabuf *resp, u8 srv_trans_id)
1312{
1313 struct p2p_srv_bonjour *bsrv;
1314 u8 *len_pos;
1315
1316 wpa_printf(MSG_DEBUG, "P2P: SD Request for all Bonjour services");
1317
1318 if (dl_list_empty(&wpa_s->global->p2p_srv_bonjour)) {
1319 wpa_printf(MSG_DEBUG, "P2P: Bonjour protocol not available");
1320 return;
1321 }
1322
1323 dl_list_for_each(bsrv, &wpa_s->global->p2p_srv_bonjour,
1324 struct p2p_srv_bonjour, list) {
1325 if (wpabuf_tailroom(resp) <
1326 5 + wpabuf_len(bsrv->query) + wpabuf_len(bsrv->resp))
1327 return;
1328 /* Length (to be filled) */
1329 len_pos = wpabuf_put(resp, 2);
1330 wpabuf_put_u8(resp, P2P_SERV_BONJOUR);
1331 wpabuf_put_u8(resp, srv_trans_id);
1332 /* Status Code */
1333 wpabuf_put_u8(resp, P2P_SD_SUCCESS);
1334 wpa_hexdump_ascii(MSG_DEBUG, "P2P: Matching Bonjour service",
1335 wpabuf_head(bsrv->resp),
1336 wpabuf_len(bsrv->resp));
1337 /* Response Data */
1338 wpabuf_put_buf(resp, bsrv->query); /* Key */
1339 wpabuf_put_buf(resp, bsrv->resp); /* Value */
1340 WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos -
1341 2);
1342 }
1343}
1344
1345
1346static void wpas_sd_req_bonjour(struct wpa_supplicant *wpa_s,
1347 struct wpabuf *resp, u8 srv_trans_id,
1348 const u8 *query, size_t query_len)
1349{
1350 struct p2p_srv_bonjour *bsrv;
1351 struct wpabuf buf;
1352 u8 *len_pos;
1353
1354 wpa_hexdump_ascii(MSG_DEBUG, "P2P: SD Request for Bonjour",
1355 query, query_len);
1356 if (dl_list_empty(&wpa_s->global->p2p_srv_bonjour)) {
1357 wpa_printf(MSG_DEBUG, "P2P: Bonjour protocol not available");
1358 wpas_sd_add_proto_not_avail(resp, P2P_SERV_BONJOUR,
1359 srv_trans_id);
1360 return;
1361 }
1362
1363 if (query_len == 0) {
1364 wpas_sd_all_bonjour(wpa_s, resp, srv_trans_id);
1365 return;
1366 }
1367
1368 if (wpabuf_tailroom(resp) < 5)
1369 return;
1370 /* Length (to be filled) */
1371 len_pos = wpabuf_put(resp, 2);
1372 wpabuf_put_u8(resp, P2P_SERV_BONJOUR);
1373 wpabuf_put_u8(resp, srv_trans_id);
1374
1375 wpabuf_set(&buf, query, query_len);
1376 bsrv = wpas_p2p_service_get_bonjour(wpa_s, &buf);
1377 if (bsrv == NULL) {
1378 wpa_printf(MSG_DEBUG, "P2P: Requested Bonjour service not "
1379 "available");
1380
1381 /* Status Code */
1382 wpabuf_put_u8(resp, P2P_SD_REQUESTED_INFO_NOT_AVAILABLE);
1383 /* Response Data: empty */
1384 WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos -
1385 2);
1386 return;
1387 }
1388
1389 /* Status Code */
1390 wpabuf_put_u8(resp, P2P_SD_SUCCESS);
1391 wpa_hexdump_ascii(MSG_DEBUG, "P2P: Matching Bonjour service",
1392 wpabuf_head(bsrv->resp), wpabuf_len(bsrv->resp));
1393
1394 if (wpabuf_tailroom(resp) >=
1395 wpabuf_len(bsrv->query) + wpabuf_len(bsrv->resp)) {
1396 /* Response Data */
1397 wpabuf_put_buf(resp, bsrv->query); /* Key */
1398 wpabuf_put_buf(resp, bsrv->resp); /* Value */
1399 }
1400 WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos - 2);
1401}
1402
1403
1404static void wpas_sd_all_upnp(struct wpa_supplicant *wpa_s,
1405 struct wpabuf *resp, u8 srv_trans_id)
1406{
1407 struct p2p_srv_upnp *usrv;
1408 u8 *len_pos;
1409
1410 wpa_printf(MSG_DEBUG, "P2P: SD Request for all UPnP services");
1411
1412 if (dl_list_empty(&wpa_s->global->p2p_srv_upnp)) {
1413 wpa_printf(MSG_DEBUG, "P2P: UPnP protocol not available");
1414 return;
1415 }
1416
1417 dl_list_for_each(usrv, &wpa_s->global->p2p_srv_upnp,
1418 struct p2p_srv_upnp, list) {
1419 if (wpabuf_tailroom(resp) < 5 + 1 + os_strlen(usrv->service))
1420 return;
1421
1422 /* Length (to be filled) */
1423 len_pos = wpabuf_put(resp, 2);
1424 wpabuf_put_u8(resp, P2P_SERV_UPNP);
1425 wpabuf_put_u8(resp, srv_trans_id);
1426
1427 /* Status Code */
1428 wpabuf_put_u8(resp, P2P_SD_SUCCESS);
1429 /* Response Data */
1430 wpabuf_put_u8(resp, usrv->version);
1431 wpa_printf(MSG_DEBUG, "P2P: Matching UPnP Service: %s",
1432 usrv->service);
1433 wpabuf_put_str(resp, usrv->service);
1434 WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos -
1435 2);
1436 }
1437}
1438
1439
1440static void wpas_sd_req_upnp(struct wpa_supplicant *wpa_s,
1441 struct wpabuf *resp, u8 srv_trans_id,
1442 const u8 *query, size_t query_len)
1443{
1444 struct p2p_srv_upnp *usrv;
1445 u8 *len_pos;
1446 u8 version;
1447 char *str;
1448 int count = 0;
1449
1450 wpa_hexdump_ascii(MSG_DEBUG, "P2P: SD Request for UPnP",
1451 query, query_len);
1452
1453 if (dl_list_empty(&wpa_s->global->p2p_srv_upnp)) {
1454 wpa_printf(MSG_DEBUG, "P2P: UPnP protocol not available");
1455 wpas_sd_add_proto_not_avail(resp, P2P_SERV_UPNP,
1456 srv_trans_id);
1457 return;
1458 }
1459
1460 if (query_len == 0) {
1461 wpas_sd_all_upnp(wpa_s, resp, srv_trans_id);
1462 return;
1463 }
1464
1465 if (wpabuf_tailroom(resp) < 5)
1466 return;
1467
1468 /* Length (to be filled) */
1469 len_pos = wpabuf_put(resp, 2);
1470 wpabuf_put_u8(resp, P2P_SERV_UPNP);
1471 wpabuf_put_u8(resp, srv_trans_id);
1472
1473 version = query[0];
1474 str = os_malloc(query_len);
1475 if (str == NULL)
1476 return;
1477 os_memcpy(str, query + 1, query_len - 1);
1478 str[query_len - 1] = '\0';
1479
1480 dl_list_for_each(usrv, &wpa_s->global->p2p_srv_upnp,
1481 struct p2p_srv_upnp, list) {
1482 if (version != usrv->version)
1483 continue;
1484
1485 if (os_strcmp(str, "ssdp:all") != 0 &&
1486 os_strstr(usrv->service, str) == NULL)
1487 continue;
1488
1489 if (wpabuf_tailroom(resp) < 2)
1490 break;
1491 if (count == 0) {
1492 /* Status Code */
1493 wpabuf_put_u8(resp, P2P_SD_SUCCESS);
1494 /* Response Data */
1495 wpabuf_put_u8(resp, version);
1496 } else
1497 wpabuf_put_u8(resp, ',');
1498
1499 count++;
1500
1501 wpa_printf(MSG_DEBUG, "P2P: Matching UPnP Service: %s",
1502 usrv->service);
1503 if (wpabuf_tailroom(resp) < os_strlen(usrv->service))
1504 break;
1505 wpabuf_put_str(resp, usrv->service);
1506 }
1507 os_free(str);
1508
1509 if (count == 0) {
1510 wpa_printf(MSG_DEBUG, "P2P: Requested UPnP service not "
1511 "available");
1512 /* Status Code */
1513 wpabuf_put_u8(resp, P2P_SD_REQUESTED_INFO_NOT_AVAILABLE);
1514 /* Response Data: empty */
1515 }
1516
1517 WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos - 2);
1518}
1519
1520
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001521#ifdef CONFIG_WIFI_DISPLAY
1522static void wpas_sd_req_wfd(struct wpa_supplicant *wpa_s,
1523 struct wpabuf *resp, u8 srv_trans_id,
1524 const u8 *query, size_t query_len)
1525{
1526 const u8 *pos;
1527 u8 role;
1528 u8 *len_pos;
1529
1530 wpa_hexdump(MSG_DEBUG, "P2P: SD Request for WFD", query, query_len);
1531
1532 if (!wpa_s->global->wifi_display) {
1533 wpa_printf(MSG_DEBUG, "P2P: WFD protocol not available");
1534 wpas_sd_add_proto_not_avail(resp, P2P_SERV_WIFI_DISPLAY,
1535 srv_trans_id);
1536 return;
1537 }
1538
1539 if (query_len < 1) {
1540 wpa_printf(MSG_DEBUG, "P2P: Missing WFD Requested Device "
1541 "Role");
1542 return;
1543 }
1544
1545 if (wpabuf_tailroom(resp) < 5)
1546 return;
1547
1548 pos = query;
1549 role = *pos++;
1550 wpa_printf(MSG_DEBUG, "P2P: WSD for device role 0x%x", role);
1551
1552 /* TODO: role specific handling */
1553
1554 /* Length (to be filled) */
1555 len_pos = wpabuf_put(resp, 2);
1556 wpabuf_put_u8(resp, P2P_SERV_WIFI_DISPLAY);
1557 wpabuf_put_u8(resp, srv_trans_id);
1558 wpabuf_put_u8(resp, P2P_SD_SUCCESS); /* Status Code */
1559
1560 while (pos < query + query_len) {
1561 if (*pos < MAX_WFD_SUBELEMS &&
1562 wpa_s->global->wfd_subelem[*pos] &&
1563 wpabuf_tailroom(resp) >=
1564 wpabuf_len(wpa_s->global->wfd_subelem[*pos])) {
1565 wpa_printf(MSG_DEBUG, "P2P: Add WSD response "
1566 "subelement %u", *pos);
1567 wpabuf_put_buf(resp, wpa_s->global->wfd_subelem[*pos]);
1568 }
1569 pos++;
1570 }
1571
1572 WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos - 2);
1573}
1574#endif /* CONFIG_WIFI_DISPLAY */
1575
1576
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001577void wpas_sd_request(void *ctx, int freq, const u8 *sa, u8 dialog_token,
1578 u16 update_indic, const u8 *tlvs, size_t tlvs_len)
1579{
1580 struct wpa_supplicant *wpa_s = ctx;
1581 const u8 *pos = tlvs;
1582 const u8 *end = tlvs + tlvs_len;
1583 const u8 *tlv_end;
1584 u16 slen;
1585 struct wpabuf *resp;
1586 u8 srv_proto, srv_trans_id;
1587 size_t buf_len;
1588 char *buf;
1589
1590 wpa_hexdump(MSG_MSGDUMP, "P2P: Service Discovery Request TLVs",
1591 tlvs, tlvs_len);
1592 buf_len = 2 * tlvs_len + 1;
1593 buf = os_malloc(buf_len);
1594 if (buf) {
1595 wpa_snprintf_hex(buf, buf_len, tlvs, tlvs_len);
1596 wpa_msg_ctrl(wpa_s, MSG_INFO, P2P_EVENT_SERV_DISC_REQ "%d "
1597 MACSTR " %u %u %s",
1598 freq, MAC2STR(sa), dialog_token, update_indic,
1599 buf);
1600 os_free(buf);
1601 }
1602
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001603 if (wpa_s->p2p_sd_over_ctrl_iface) {
1604 wpas_notify_p2p_sd_request(wpa_s, freq, sa, dialog_token,
1605 update_indic, tlvs, tlvs_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001606 return; /* to be processed by an external program */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001607 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001608
1609 resp = wpabuf_alloc(10000);
1610 if (resp == NULL)
1611 return;
1612
1613 while (pos + 1 < end) {
1614 wpa_printf(MSG_DEBUG, "P2P: Service Request TLV");
1615 slen = WPA_GET_LE16(pos);
1616 pos += 2;
1617 if (pos + slen > end || slen < 2) {
1618 wpa_printf(MSG_DEBUG, "P2P: Unexpected Query Data "
1619 "length");
1620 wpabuf_free(resp);
1621 return;
1622 }
1623 tlv_end = pos + slen;
1624
1625 srv_proto = *pos++;
1626 wpa_printf(MSG_DEBUG, "P2P: Service Protocol Type %u",
1627 srv_proto);
1628 srv_trans_id = *pos++;
1629 wpa_printf(MSG_DEBUG, "P2P: Service Transaction ID %u",
1630 srv_trans_id);
1631
1632 wpa_hexdump(MSG_MSGDUMP, "P2P: Query Data",
1633 pos, tlv_end - pos);
1634
1635
1636 if (wpa_s->force_long_sd) {
1637 wpa_printf(MSG_DEBUG, "P2P: SD test - force long "
1638 "response");
1639 wpas_sd_all_bonjour(wpa_s, resp, srv_trans_id);
1640 wpas_sd_all_upnp(wpa_s, resp, srv_trans_id);
1641 goto done;
1642 }
1643
1644 switch (srv_proto) {
1645 case P2P_SERV_ALL_SERVICES:
1646 wpa_printf(MSG_DEBUG, "P2P: Service Discovery Request "
1647 "for all services");
1648 if (dl_list_empty(&wpa_s->global->p2p_srv_upnp) &&
1649 dl_list_empty(&wpa_s->global->p2p_srv_bonjour)) {
1650 wpa_printf(MSG_DEBUG, "P2P: No service "
1651 "discovery protocols available");
1652 wpas_sd_add_proto_not_avail(
1653 resp, P2P_SERV_ALL_SERVICES,
1654 srv_trans_id);
1655 break;
1656 }
1657 wpas_sd_all_bonjour(wpa_s, resp, srv_trans_id);
1658 wpas_sd_all_upnp(wpa_s, resp, srv_trans_id);
1659 break;
1660 case P2P_SERV_BONJOUR:
1661 wpas_sd_req_bonjour(wpa_s, resp, srv_trans_id,
1662 pos, tlv_end - pos);
1663 break;
1664 case P2P_SERV_UPNP:
1665 wpas_sd_req_upnp(wpa_s, resp, srv_trans_id,
1666 pos, tlv_end - pos);
1667 break;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001668#ifdef CONFIG_WIFI_DISPLAY
1669 case P2P_SERV_WIFI_DISPLAY:
1670 wpas_sd_req_wfd(wpa_s, resp, srv_trans_id,
1671 pos, tlv_end - pos);
1672 break;
1673#endif /* CONFIG_WIFI_DISPLAY */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001674 default:
1675 wpa_printf(MSG_DEBUG, "P2P: Unavailable service "
1676 "protocol %u", srv_proto);
1677 wpas_sd_add_proto_not_avail(resp, srv_proto,
1678 srv_trans_id);
1679 break;
1680 }
1681
1682 pos = tlv_end;
1683 }
1684
1685done:
1686 wpas_notify_p2p_sd_request(wpa_s, freq, sa, dialog_token,
1687 update_indic, tlvs, tlvs_len);
1688
1689 wpas_p2p_sd_response(wpa_s, freq, sa, dialog_token, resp);
1690
1691 wpabuf_free(resp);
1692}
1693
1694
1695void wpas_sd_response(void *ctx, const u8 *sa, u16 update_indic,
1696 const u8 *tlvs, size_t tlvs_len)
1697{
1698 struct wpa_supplicant *wpa_s = ctx;
1699 const u8 *pos = tlvs;
1700 const u8 *end = tlvs + tlvs_len;
1701 const u8 *tlv_end;
1702 u16 slen;
1703 size_t buf_len;
1704 char *buf;
1705
1706 wpa_hexdump(MSG_MSGDUMP, "P2P: Service Discovery Response TLVs",
1707 tlvs, tlvs_len);
1708 if (tlvs_len > 1500) {
1709 /* TODO: better way for handling this */
1710 wpa_msg_ctrl(wpa_s, MSG_INFO,
1711 P2P_EVENT_SERV_DISC_RESP MACSTR
1712 " %u <long response: %u bytes>",
1713 MAC2STR(sa), update_indic,
1714 (unsigned int) tlvs_len);
1715 } else {
1716 buf_len = 2 * tlvs_len + 1;
1717 buf = os_malloc(buf_len);
1718 if (buf) {
1719 wpa_snprintf_hex(buf, buf_len, tlvs, tlvs_len);
1720 wpa_msg_ctrl(wpa_s, MSG_INFO,
1721 P2P_EVENT_SERV_DISC_RESP MACSTR " %u %s",
1722 MAC2STR(sa), update_indic, buf);
1723 os_free(buf);
1724 }
1725 }
1726
1727 while (pos < end) {
1728 u8 srv_proto, srv_trans_id, status;
1729
1730 wpa_printf(MSG_DEBUG, "P2P: Service Response TLV");
1731 slen = WPA_GET_LE16(pos);
1732 pos += 2;
1733 if (pos + slen > end || slen < 3) {
1734 wpa_printf(MSG_DEBUG, "P2P: Unexpected Response Data "
1735 "length");
1736 return;
1737 }
1738 tlv_end = pos + slen;
1739
1740 srv_proto = *pos++;
1741 wpa_printf(MSG_DEBUG, "P2P: Service Protocol Type %u",
1742 srv_proto);
1743 srv_trans_id = *pos++;
1744 wpa_printf(MSG_DEBUG, "P2P: Service Transaction ID %u",
1745 srv_trans_id);
1746 status = *pos++;
1747 wpa_printf(MSG_DEBUG, "P2P: Status Code ID %u",
1748 status);
1749
1750 wpa_hexdump(MSG_MSGDUMP, "P2P: Response Data",
1751 pos, tlv_end - pos);
1752
1753 pos = tlv_end;
1754 }
1755
1756 wpas_notify_p2p_sd_response(wpa_s, sa, update_indic, tlvs, tlvs_len);
1757}
1758
1759
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001760u64 wpas_p2p_sd_request(struct wpa_supplicant *wpa_s, const u8 *dst,
1761 const struct wpabuf *tlvs)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001762{
1763 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001764 return wpa_drv_p2p_sd_request(wpa_s, dst, tlvs);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001765 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001766 return 0;
1767 return (uintptr_t) p2p_sd_request(wpa_s->global->p2p, dst, tlvs);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001768}
1769
1770
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001771u64 wpas_p2p_sd_request_upnp(struct wpa_supplicant *wpa_s, const u8 *dst,
1772 u8 version, const char *query)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001773{
1774 struct wpabuf *tlvs;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001775 u64 ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001776
1777 tlvs = wpabuf_alloc(2 + 1 + 1 + 1 + os_strlen(query));
1778 if (tlvs == NULL)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001779 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001780 wpabuf_put_le16(tlvs, 1 + 1 + 1 + os_strlen(query));
1781 wpabuf_put_u8(tlvs, P2P_SERV_UPNP); /* Service Protocol Type */
1782 wpabuf_put_u8(tlvs, 1); /* Service Transaction ID */
1783 wpabuf_put_u8(tlvs, version);
1784 wpabuf_put_str(tlvs, query);
1785 ret = wpas_p2p_sd_request(wpa_s, dst, tlvs);
1786 wpabuf_free(tlvs);
1787 return ret;
1788}
1789
1790
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001791#ifdef CONFIG_WIFI_DISPLAY
1792
1793static u64 wpas_p2p_sd_request_wfd(struct wpa_supplicant *wpa_s, const u8 *dst,
1794 const struct wpabuf *tlvs)
1795{
1796 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
1797 return 0;
1798 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
1799 return 0;
1800 return (uintptr_t) p2p_sd_request_wfd(wpa_s->global->p2p, dst, tlvs);
1801}
1802
1803
1804#define MAX_WFD_SD_SUBELEMS 20
1805
1806static void wfd_add_sd_req_role(struct wpabuf *tlvs, u8 id, u8 role,
1807 const char *subelems)
1808{
1809 u8 *len;
1810 const char *pos;
1811 int val;
1812 int count = 0;
1813
1814 len = wpabuf_put(tlvs, 2);
1815 wpabuf_put_u8(tlvs, P2P_SERV_WIFI_DISPLAY); /* Service Protocol Type */
1816 wpabuf_put_u8(tlvs, id); /* Service Transaction ID */
1817
1818 wpabuf_put_u8(tlvs, role);
1819
1820 pos = subelems;
1821 while (*pos) {
1822 val = atoi(pos);
1823 if (val >= 0 && val < 256) {
1824 wpabuf_put_u8(tlvs, val);
1825 count++;
1826 if (count == MAX_WFD_SD_SUBELEMS)
1827 break;
1828 }
1829 pos = os_strchr(pos + 1, ',');
1830 if (pos == NULL)
1831 break;
1832 pos++;
1833 }
1834
1835 WPA_PUT_LE16(len, (u8 *) wpabuf_put(tlvs, 0) - len - 2);
1836}
1837
1838
1839u64 wpas_p2p_sd_request_wifi_display(struct wpa_supplicant *wpa_s,
1840 const u8 *dst, const char *role)
1841{
1842 struct wpabuf *tlvs;
1843 u64 ret;
1844 const char *subelems;
1845 u8 id = 1;
1846
1847 subelems = os_strchr(role, ' ');
1848 if (subelems == NULL)
1849 return 0;
1850 subelems++;
1851
1852 tlvs = wpabuf_alloc(4 * (2 + 1 + 1 + 1 + MAX_WFD_SD_SUBELEMS));
1853 if (tlvs == NULL)
1854 return 0;
1855
1856 if (os_strstr(role, "[source]"))
1857 wfd_add_sd_req_role(tlvs, id++, 0x00, subelems);
1858 if (os_strstr(role, "[pri-sink]"))
1859 wfd_add_sd_req_role(tlvs, id++, 0x01, subelems);
1860 if (os_strstr(role, "[sec-sink]"))
1861 wfd_add_sd_req_role(tlvs, id++, 0x02, subelems);
1862 if (os_strstr(role, "[source+sink]"))
1863 wfd_add_sd_req_role(tlvs, id++, 0x03, subelems);
1864
1865 ret = wpas_p2p_sd_request_wfd(wpa_s, dst, tlvs);
1866 wpabuf_free(tlvs);
1867 return ret;
1868}
1869
1870#endif /* CONFIG_WIFI_DISPLAY */
1871
1872
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001873int wpas_p2p_sd_cancel_request(struct wpa_supplicant *wpa_s, u64 req)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001874{
1875 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001876 return wpa_drv_p2p_sd_cancel_request(wpa_s, req);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001877 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
1878 return -1;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001879 return p2p_sd_cancel_request(wpa_s->global->p2p,
1880 (void *) (uintptr_t) req);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001881}
1882
1883
1884void wpas_p2p_sd_response(struct wpa_supplicant *wpa_s, int freq,
1885 const u8 *dst, u8 dialog_token,
1886 const struct wpabuf *resp_tlvs)
1887{
1888 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT) {
1889 wpa_drv_p2p_sd_response(wpa_s, freq, dst, dialog_token,
1890 resp_tlvs);
1891 return;
1892 }
1893 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
1894 return;
1895 p2p_sd_response(wpa_s->global->p2p, freq, dst, dialog_token,
1896 resp_tlvs);
1897}
1898
Dmitry Shmidtb5e8f062012-08-08 10:56:33 -07001899#ifdef ANDROID_P2P
1900void wpas_p2p_sd_service_update(struct wpa_supplicant *wpa_s, int action)
1901#else
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001902void wpas_p2p_sd_service_update(struct wpa_supplicant *wpa_s)
Dmitry Shmidtb5e8f062012-08-08 10:56:33 -07001903#endif
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001904{
1905 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT) {
1906 wpa_drv_p2p_service_update(wpa_s);
1907 return;
1908 }
1909 if (wpa_s->global->p2p)
Dmitry Shmidtb5e8f062012-08-08 10:56:33 -07001910#ifdef ANDROID_P2P
1911 p2p_sd_service_update(wpa_s->global->p2p, action);
1912#else
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001913 p2p_sd_service_update(wpa_s->global->p2p);
Dmitry Shmidtb5e8f062012-08-08 10:56:33 -07001914#endif
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001915}
1916
1917
1918static void wpas_p2p_srv_bonjour_free(struct p2p_srv_bonjour *bsrv)
1919{
1920 dl_list_del(&bsrv->list);
1921 wpabuf_free(bsrv->query);
1922 wpabuf_free(bsrv->resp);
1923 os_free(bsrv);
1924}
1925
1926
1927static void wpas_p2p_srv_upnp_free(struct p2p_srv_upnp *usrv)
1928{
1929 dl_list_del(&usrv->list);
1930 os_free(usrv->service);
1931 os_free(usrv);
1932}
1933
1934
1935void wpas_p2p_service_flush(struct wpa_supplicant *wpa_s)
1936{
1937 struct p2p_srv_bonjour *bsrv, *bn;
1938 struct p2p_srv_upnp *usrv, *un;
1939
1940 dl_list_for_each_safe(bsrv, bn, &wpa_s->global->p2p_srv_bonjour,
1941 struct p2p_srv_bonjour, list)
1942 wpas_p2p_srv_bonjour_free(bsrv);
1943
1944 dl_list_for_each_safe(usrv, un, &wpa_s->global->p2p_srv_upnp,
1945 struct p2p_srv_upnp, list)
1946 wpas_p2p_srv_upnp_free(usrv);
1947
Dmitry Shmidtb5e8f062012-08-08 10:56:33 -07001948#ifdef ANDROID_P2P
1949 wpas_p2p_sd_service_update(wpa_s, SRV_FLUSH);
1950#else
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001951 wpas_p2p_sd_service_update(wpa_s);
Dmitry Shmidtb5e8f062012-08-08 10:56:33 -07001952#endif
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001953}
1954
1955
1956int wpas_p2p_service_add_bonjour(struct wpa_supplicant *wpa_s,
1957 struct wpabuf *query, struct wpabuf *resp)
1958{
1959 struct p2p_srv_bonjour *bsrv;
1960
1961 bsrv = wpas_p2p_service_get_bonjour(wpa_s, query);
1962 if (bsrv) {
1963 wpabuf_free(query);
1964 wpabuf_free(bsrv->resp);
1965 bsrv->resp = resp;
1966 return 0;
1967 }
1968
1969 bsrv = os_zalloc(sizeof(*bsrv));
1970 if (bsrv == NULL)
1971 return -1;
1972 bsrv->query = query;
1973 bsrv->resp = resp;
1974 dl_list_add(&wpa_s->global->p2p_srv_bonjour, &bsrv->list);
1975
Dmitry Shmidtb5e8f062012-08-08 10:56:33 -07001976#ifdef ANDROID_P2P
1977 wpas_p2p_sd_service_update(wpa_s, SRV_ADD);
1978#else
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001979 wpas_p2p_sd_service_update(wpa_s);
Dmitry Shmidtb5e8f062012-08-08 10:56:33 -07001980#endif
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001981 return 0;
1982}
1983
1984
1985int wpas_p2p_service_del_bonjour(struct wpa_supplicant *wpa_s,
1986 const struct wpabuf *query)
1987{
1988 struct p2p_srv_bonjour *bsrv;
1989
1990 bsrv = wpas_p2p_service_get_bonjour(wpa_s, query);
1991 if (bsrv == NULL)
1992 return -1;
1993 wpas_p2p_srv_bonjour_free(bsrv);
Dmitry Shmidtb5e8f062012-08-08 10:56:33 -07001994#ifdef ANDROID_P2P
1995 wpas_p2p_sd_service_update(wpa_s, SRV_DEL);
1996#else
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001997 wpas_p2p_sd_service_update(wpa_s);
Dmitry Shmidtb5e8f062012-08-08 10:56:33 -07001998#endif
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001999 return 0;
2000}
2001
2002
2003int wpas_p2p_service_add_upnp(struct wpa_supplicant *wpa_s, u8 version,
2004 const char *service)
2005{
2006 struct p2p_srv_upnp *usrv;
2007
2008 if (wpas_p2p_service_get_upnp(wpa_s, version, service))
2009 return 0; /* Already listed */
2010 usrv = os_zalloc(sizeof(*usrv));
2011 if (usrv == NULL)
2012 return -1;
2013 usrv->version = version;
2014 usrv->service = os_strdup(service);
2015 if (usrv->service == NULL) {
2016 os_free(usrv);
2017 return -1;
2018 }
2019 dl_list_add(&wpa_s->global->p2p_srv_upnp, &usrv->list);
2020
Dmitry Shmidtb5e8f062012-08-08 10:56:33 -07002021#ifdef ANDROID_P2P
2022 wpas_p2p_sd_service_update(wpa_s, SRV_ADD);
2023#else
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002024 wpas_p2p_sd_service_update(wpa_s);
Dmitry Shmidtb5e8f062012-08-08 10:56:33 -07002025#endif
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002026 return 0;
2027}
2028
2029
2030int wpas_p2p_service_del_upnp(struct wpa_supplicant *wpa_s, u8 version,
2031 const char *service)
2032{
2033 struct p2p_srv_upnp *usrv;
2034
2035 usrv = wpas_p2p_service_get_upnp(wpa_s, version, service);
2036 if (usrv == NULL)
2037 return -1;
2038 wpas_p2p_srv_upnp_free(usrv);
Dmitry Shmidtb5e8f062012-08-08 10:56:33 -07002039#ifdef ANDROID_P2P
2040 wpas_p2p_sd_service_update(wpa_s, SRV_DEL);
2041#else
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002042 wpas_p2p_sd_service_update(wpa_s);
Dmitry Shmidtb5e8f062012-08-08 10:56:33 -07002043#endif
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002044 return 0;
2045}
2046
2047
2048static void wpas_prov_disc_local_display(struct wpa_supplicant *wpa_s,
2049 const u8 *peer, const char *params,
2050 unsigned int generated_pin)
2051{
2052 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_SHOW_PIN MACSTR " %08d%s",
2053 MAC2STR(peer), generated_pin, params);
2054}
2055
2056
2057static void wpas_prov_disc_local_keypad(struct wpa_supplicant *wpa_s,
2058 const u8 *peer, const char *params)
2059{
2060 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_ENTER_PIN MACSTR "%s",
2061 MAC2STR(peer), params);
2062}
2063
2064
2065void wpas_prov_disc_req(void *ctx, const u8 *peer, u16 config_methods,
2066 const u8 *dev_addr, const u8 *pri_dev_type,
2067 const char *dev_name, u16 supp_config_methods,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002068 u8 dev_capab, u8 group_capab, const u8 *group_id,
2069 size_t group_id_len)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002070{
2071 struct wpa_supplicant *wpa_s = ctx;
2072 char devtype[WPS_DEV_TYPE_BUFSIZE];
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002073 char params[300];
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002074 u8 empty_dev_type[8];
2075 unsigned int generated_pin = 0;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002076 struct wpa_supplicant *group = NULL;
2077
2078 if (group_id) {
2079 for (group = wpa_s->global->ifaces; group; group = group->next)
2080 {
2081 struct wpa_ssid *s = group->current_ssid;
2082 if (s != NULL &&
2083 s->mode == WPAS_MODE_P2P_GO &&
2084 group_id_len - ETH_ALEN == s->ssid_len &&
2085 os_memcmp(group_id + ETH_ALEN, s->ssid,
2086 s->ssid_len) == 0)
2087 break;
2088 }
2089 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002090
2091 if (pri_dev_type == NULL) {
2092 os_memset(empty_dev_type, 0, sizeof(empty_dev_type));
2093 pri_dev_type = empty_dev_type;
2094 }
2095 os_snprintf(params, sizeof(params), " p2p_dev_addr=" MACSTR
2096 " pri_dev_type=%s name='%s' config_methods=0x%x "
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002097 "dev_capab=0x%x group_capab=0x%x%s%s",
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002098 MAC2STR(dev_addr),
2099 wps_dev_type_bin2str(pri_dev_type, devtype,
2100 sizeof(devtype)),
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002101 dev_name, supp_config_methods, dev_capab, group_capab,
2102 group ? " group=" : "",
2103 group ? group->ifname : "");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002104 params[sizeof(params) - 1] = '\0';
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08002105
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002106 if (config_methods & WPS_CONFIG_DISPLAY) {
2107 generated_pin = wps_generate_pin();
2108 wpas_prov_disc_local_display(wpa_s, peer, params,
2109 generated_pin);
2110 } else if (config_methods & WPS_CONFIG_KEYPAD)
2111 wpas_prov_disc_local_keypad(wpa_s, peer, params);
2112 else if (config_methods & WPS_CONFIG_PUSHBUTTON)
2113 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_PBC_REQ MACSTR
2114 "%s", MAC2STR(peer), params);
Jouni Malinen75ecf522011-06-27 15:19:46 -07002115
2116 wpas_notify_p2p_provision_discovery(wpa_s, peer, 1 /* request */,
2117 P2P_PROV_DISC_SUCCESS,
2118 config_methods, generated_pin);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002119}
2120
2121
2122void wpas_prov_disc_resp(void *ctx, const u8 *peer, u16 config_methods)
2123{
2124 struct wpa_supplicant *wpa_s = ctx;
2125 unsigned int generated_pin = 0;
Dmitry Shmidt04949592012-07-19 12:16:46 -07002126 char params[20];
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002127
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08002128 if (wpa_s->pending_pd_before_join &&
2129 (os_memcmp(peer, wpa_s->pending_join_dev_addr, ETH_ALEN) == 0 ||
2130 os_memcmp(peer, wpa_s->pending_join_iface_addr, ETH_ALEN) == 0)) {
2131 wpa_s->pending_pd_before_join = 0;
2132 wpa_printf(MSG_DEBUG, "P2P: Starting pending "
2133 "join-existing-group operation");
2134 wpas_p2p_join_start(wpa_s);
2135 return;
2136 }
2137
Dmitry Shmidt04949592012-07-19 12:16:46 -07002138 if (wpa_s->pending_pd_use == AUTO_PD_JOIN ||
2139 wpa_s->pending_pd_use == AUTO_PD_GO_NEG)
2140 os_snprintf(params, sizeof(params), " peer_go=%d",
2141 wpa_s->pending_pd_use == AUTO_PD_JOIN);
2142 else
2143 params[0] = '\0';
2144
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002145 if (config_methods & WPS_CONFIG_DISPLAY)
Dmitry Shmidt04949592012-07-19 12:16:46 -07002146 wpas_prov_disc_local_keypad(wpa_s, peer, params);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002147 else if (config_methods & WPS_CONFIG_KEYPAD) {
2148 generated_pin = wps_generate_pin();
Dmitry Shmidt04949592012-07-19 12:16:46 -07002149 wpas_prov_disc_local_display(wpa_s, peer, params,
2150 generated_pin);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002151 } else if (config_methods & WPS_CONFIG_PUSHBUTTON)
Dmitry Shmidt04949592012-07-19 12:16:46 -07002152 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_PBC_RESP MACSTR
2153 "%s", MAC2STR(peer), params);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002154
Jouni Malinen75ecf522011-06-27 15:19:46 -07002155 wpas_notify_p2p_provision_discovery(wpa_s, peer, 0 /* response */,
2156 P2P_PROV_DISC_SUCCESS,
2157 config_methods, generated_pin);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002158}
2159
2160
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002161static void wpas_prov_disc_fail(void *ctx, const u8 *peer,
2162 enum p2p_prov_disc_status status)
Jouni Malinen75ecf522011-06-27 15:19:46 -07002163{
2164 struct wpa_supplicant *wpa_s = ctx;
2165
Dmitry Shmidt04949592012-07-19 12:16:46 -07002166 if (wpa_s->p2p_fallback_to_go_neg) {
2167 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: PD for p2p_connect-auto "
2168 "failed - fall back to GO Negotiation");
2169 wpas_p2p_fallback_to_go_neg(wpa_s, 0);
2170 return;
2171 }
2172
Dmitry Shmidt2b380482012-09-13 10:52:13 -07002173#ifdef ANDROID_P2P
2174 /* If provision discovery failed it is safe to cancel the timer here and
2175 * also do not start the join */
2176 if (wpa_s->pending_pd_before_join &&
2177 (os_memcmp(peer, wpa_s->pending_join_dev_addr, ETH_ALEN) == 0 ||
2178 os_memcmp(peer, wpa_s->pending_join_iface_addr, ETH_ALEN) == 0)) {
2179 wpa_s->pending_pd_before_join = 0;
2180 wpa_printf(MSG_DEBUG, "P2P: Do not Start pending "
2181 "join-existing-group operation");
2182 eloop_cancel_timeout(wpas_p2p_pd_before_join_timeout, wpa_s, NULL);
2183 }
2184#endif /* ANDROID_P2P */
Dmitry Shmidt04949592012-07-19 12:16:46 -07002185 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_FAILURE
2186 " p2p_dev_addr=" MACSTR " status=%d",
2187 MAC2STR(peer), status);
2188
Jouni Malinen75ecf522011-06-27 15:19:46 -07002189 wpas_notify_p2p_provision_discovery(wpa_s, peer, 0 /* response */,
2190 status, 0, 0);
2191}
2192
2193
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002194static u8 wpas_invitation_process(void *ctx, const u8 *sa, const u8 *bssid,
2195 const u8 *go_dev_addr, const u8 *ssid,
2196 size_t ssid_len, int *go, u8 *group_bssid,
2197 int *force_freq, int persistent_group)
2198{
2199 struct wpa_supplicant *wpa_s = ctx;
2200 struct wpa_ssid *s;
2201 u8 cur_bssid[ETH_ALEN];
2202 int res;
2203 struct wpa_supplicant *grp;
2204
2205 if (!persistent_group) {
2206 wpa_printf(MSG_DEBUG, "P2P: Invitation from " MACSTR
2207 " to join an active group", MAC2STR(sa));
2208 if (!is_zero_ether_addr(wpa_s->p2p_auth_invite) &&
2209 (os_memcmp(go_dev_addr, wpa_s->p2p_auth_invite, ETH_ALEN)
2210 == 0 ||
2211 os_memcmp(sa, wpa_s->p2p_auth_invite, ETH_ALEN) == 0)) {
2212 wpa_printf(MSG_DEBUG, "P2P: Accept previously "
2213 "authorized invitation");
2214 goto accept_inv;
2215 }
2216 /*
2217 * Do not accept the invitation automatically; notify user and
2218 * request approval.
2219 */
2220 return P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE;
2221 }
2222
2223 grp = wpas_get_p2p_group(wpa_s, ssid, ssid_len, go);
2224 if (grp) {
2225 wpa_printf(MSG_DEBUG, "P2P: Accept invitation to already "
2226 "running persistent group");
2227 if (*go)
2228 os_memcpy(group_bssid, grp->own_addr, ETH_ALEN);
2229 goto accept_inv;
2230 }
2231
2232 if (!wpa_s->conf->persistent_reconnect)
2233 return P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE;
2234
2235 for (s = wpa_s->conf->ssid; s; s = s->next) {
2236 if (s->disabled == 2 &&
2237 os_memcmp(s->bssid, go_dev_addr, ETH_ALEN) == 0 &&
2238 s->ssid_len == ssid_len &&
2239 os_memcmp(ssid, s->ssid, ssid_len) == 0)
2240 break;
2241 }
2242
2243 if (!s) {
2244 wpa_printf(MSG_DEBUG, "P2P: Invitation from " MACSTR
2245 " requested reinvocation of an unknown group",
2246 MAC2STR(sa));
2247 return P2P_SC_FAIL_UNKNOWN_GROUP;
2248 }
2249
2250 if (s->mode == WPAS_MODE_P2P_GO && !wpas_p2p_create_iface(wpa_s)) {
2251 *go = 1;
2252 if (wpa_s->wpa_state >= WPA_AUTHENTICATING) {
2253 wpa_printf(MSG_DEBUG, "P2P: The only available "
2254 "interface is already in use - reject "
2255 "invitation");
2256 return P2P_SC_FAIL_UNABLE_TO_ACCOMMODATE;
2257 }
2258 os_memcpy(group_bssid, wpa_s->own_addr, ETH_ALEN);
2259 } else if (s->mode == WPAS_MODE_P2P_GO) {
2260 *go = 1;
2261 if (wpas_p2p_add_group_interface(wpa_s, WPA_IF_P2P_GO) < 0)
2262 {
2263 wpa_printf(MSG_ERROR, "P2P: Failed to allocate a new "
2264 "interface address for the group");
2265 return P2P_SC_FAIL_UNABLE_TO_ACCOMMODATE;
2266 }
2267 os_memcpy(group_bssid, wpa_s->pending_interface_addr,
2268 ETH_ALEN);
2269 }
2270
2271accept_inv:
2272 if (wpa_s->current_ssid && wpa_drv_get_bssid(wpa_s, cur_bssid) == 0 &&
2273 wpa_s->assoc_freq) {
2274 wpa_printf(MSG_DEBUG, "P2P: Trying to force channel to match "
2275 "the channel we are already using");
2276 *force_freq = wpa_s->assoc_freq;
2277 }
2278
2279 res = wpa_drv_shared_freq(wpa_s);
2280 if (res > 0) {
2281 wpa_printf(MSG_DEBUG, "P2P: Trying to force channel to match "
2282 "with the channel we are already using on a "
2283 "shared interface");
2284 *force_freq = res;
2285 }
2286
2287 return P2P_SC_SUCCESS;
2288}
2289
2290
2291static void wpas_invitation_received(void *ctx, const u8 *sa, const u8 *bssid,
2292 const u8 *ssid, size_t ssid_len,
2293 const u8 *go_dev_addr, u8 status,
2294 int op_freq)
2295{
2296 struct wpa_supplicant *wpa_s = ctx;
2297 struct wpa_ssid *s;
2298
2299 for (s = wpa_s->conf->ssid; s; s = s->next) {
2300 if (s->disabled == 2 &&
2301 s->ssid_len == ssid_len &&
2302 os_memcmp(ssid, s->ssid, ssid_len) == 0)
2303 break;
2304 }
2305
2306 if (status == P2P_SC_SUCCESS) {
2307 wpa_printf(MSG_DEBUG, "P2P: Invitation from peer " MACSTR
2308 " was accepted; op_freq=%d MHz",
2309 MAC2STR(sa), op_freq);
2310 if (s) {
2311 wpas_p2p_group_add_persistent(
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002312 wpa_s, s, s->mode == WPAS_MODE_P2P_GO, 0, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002313 } else if (bssid) {
2314 wpas_p2p_join(wpa_s, bssid, go_dev_addr,
Dmitry Shmidt04949592012-07-19 12:16:46 -07002315 wpa_s->p2p_wps_method, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002316 }
2317 return;
2318 }
2319
2320 if (status != P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE) {
2321 wpa_printf(MSG_DEBUG, "P2P: Invitation from peer " MACSTR
2322 " was rejected (status %u)", MAC2STR(sa), status);
2323 return;
2324 }
2325
2326 if (!s) {
2327 if (bssid) {
2328 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_INVITATION_RECEIVED
2329 "sa=" MACSTR " go_dev_addr=" MACSTR
2330 " bssid=" MACSTR " unknown-network",
2331 MAC2STR(sa), MAC2STR(go_dev_addr),
2332 MAC2STR(bssid));
2333 } else {
2334 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_INVITATION_RECEIVED
2335 "sa=" MACSTR " go_dev_addr=" MACSTR
2336 " unknown-network",
2337 MAC2STR(sa), MAC2STR(go_dev_addr));
2338 }
2339 return;
2340 }
2341
2342 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_INVITATION_RECEIVED "sa=" MACSTR
2343 " persistent=%d", MAC2STR(sa), s->id);
2344}
2345
2346
2347static void wpas_invitation_result(void *ctx, int status, const u8 *bssid)
2348{
2349 struct wpa_supplicant *wpa_s = ctx;
2350 struct wpa_ssid *ssid;
2351
2352 if (bssid) {
2353 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_INVITATION_RESULT
2354 "status=%d " MACSTR,
2355 status, MAC2STR(bssid));
2356 } else {
2357 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_INVITATION_RESULT
2358 "status=%d ", status);
2359 }
2360 wpas_notify_p2p_invitation_result(wpa_s, status, bssid);
2361
2362 if (wpa_s->pending_invite_ssid_id == -1)
2363 return; /* Invitation to active group */
2364
2365 if (status != P2P_SC_SUCCESS) {
2366 wpas_p2p_remove_pending_group_interface(wpa_s);
2367 return;
2368 }
2369
2370 ssid = wpa_config_get_network(wpa_s->conf,
2371 wpa_s->pending_invite_ssid_id);
2372 if (ssid == NULL) {
2373 wpa_printf(MSG_ERROR, "P2P: Could not find persistent group "
2374 "data matching with invitation");
2375 return;
2376 }
2377
2378 wpas_p2p_group_add_persistent(wpa_s, ssid,
Jouni Malinen31be0a42012-08-31 21:20:51 +03002379 ssid->mode == WPAS_MODE_P2P_GO,
2380 wpa_s->p2p_persistent_go_freq,
2381 wpa_s->p2p_go_ht40);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002382}
2383
2384
Dmitry Shmidt04949592012-07-19 12:16:46 -07002385static int wpas_p2p_disallowed_freq(struct wpa_global *global,
2386 unsigned int freq)
2387{
2388 unsigned int i;
2389
2390 if (global->p2p_disallow_freq == NULL)
2391 return 0;
2392
2393 for (i = 0; i < global->num_p2p_disallow_freq; i++) {
2394 if (freq >= global->p2p_disallow_freq[i].min &&
2395 freq <= global->p2p_disallow_freq[i].max)
2396 return 1;
2397 }
2398
2399 return 0;
2400}
2401
2402
2403static void wpas_p2p_add_chan(struct p2p_reg_class *reg, u8 chan)
2404{
2405 reg->channel[reg->channels] = chan;
2406 reg->channels++;
2407}
2408
2409
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002410static int wpas_p2p_default_channels(struct wpa_supplicant *wpa_s,
2411 struct p2p_channels *chan)
2412{
2413 int i, cla = 0;
2414
2415 wpa_printf(MSG_DEBUG, "P2P: Enable operating classes for 2.4 GHz "
2416 "band");
2417
2418 /* Operating class 81 - 2.4 GHz band channels 1..13 */
2419 chan->reg_class[cla].reg_class = 81;
Dmitry Shmidt04949592012-07-19 12:16:46 -07002420 chan->reg_class[cla].channels = 0;
2421 for (i = 0; i < 11; i++) {
2422 if (!wpas_p2p_disallowed_freq(wpa_s->global, 2412 + i * 5))
2423 wpas_p2p_add_chan(&chan->reg_class[cla], i + 1);
2424 }
2425 if (chan->reg_class[cla].channels)
2426 cla++;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002427
2428 wpa_printf(MSG_DEBUG, "P2P: Enable operating classes for lower 5 GHz "
2429 "band");
2430
2431 /* Operating class 115 - 5 GHz, channels 36-48 */
2432 chan->reg_class[cla].reg_class = 115;
Dmitry Shmidt04949592012-07-19 12:16:46 -07002433 chan->reg_class[cla].channels = 0;
2434 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 36 * 5))
2435 wpas_p2p_add_chan(&chan->reg_class[cla], 36);
2436 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 40 * 5))
2437 wpas_p2p_add_chan(&chan->reg_class[cla], 40);
2438 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 44 * 5))
2439 wpas_p2p_add_chan(&chan->reg_class[cla], 44);
2440 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 48 * 5))
2441 wpas_p2p_add_chan(&chan->reg_class[cla], 48);
2442 if (chan->reg_class[cla].channels)
2443 cla++;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002444
2445 wpa_printf(MSG_DEBUG, "P2P: Enable operating classes for higher 5 GHz "
2446 "band");
2447
2448 /* Operating class 124 - 5 GHz, channels 149,153,157,161 */
2449 chan->reg_class[cla].reg_class = 124;
Dmitry Shmidt04949592012-07-19 12:16:46 -07002450 chan->reg_class[cla].channels = 0;
2451 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 149 * 5))
2452 wpas_p2p_add_chan(&chan->reg_class[cla], 149);
2453 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 153 * 5))
2454 wpas_p2p_add_chan(&chan->reg_class[cla], 153);
2455 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 156 * 5))
2456 wpas_p2p_add_chan(&chan->reg_class[cla], 157);
2457 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 161 * 5))
2458 wpas_p2p_add_chan(&chan->reg_class[cla], 161);
2459 if (chan->reg_class[cla].channels)
2460 cla++;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002461
2462 chan->reg_classes = cla;
2463 return 0;
2464}
2465
2466
2467static struct hostapd_hw_modes * get_mode(struct hostapd_hw_modes *modes,
2468 u16 num_modes,
2469 enum hostapd_hw_mode mode)
2470{
2471 u16 i;
2472
2473 for (i = 0; i < num_modes; i++) {
2474 if (modes[i].mode == mode)
2475 return &modes[i];
2476 }
2477
2478 return NULL;
2479}
2480
2481
Dmitry Shmidt04949592012-07-19 12:16:46 -07002482static int has_channel(struct wpa_global *global,
2483 struct hostapd_hw_modes *mode, u8 chan, int *flags)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002484{
2485 int i;
Dmitry Shmidt04949592012-07-19 12:16:46 -07002486 unsigned int freq;
2487
2488 freq = (mode->mode == HOSTAPD_MODE_IEEE80211A ? 5000 : 2407) +
2489 chan * 5;
2490 if (wpas_p2p_disallowed_freq(global, freq))
2491 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002492
2493 for (i = 0; i < mode->num_channels; i++) {
2494 if (mode->channels[i].chan == chan) {
2495 if (flags)
2496 *flags = mode->channels[i].flag;
2497 return !(mode->channels[i].flag &
2498 (HOSTAPD_CHAN_DISABLED |
2499 HOSTAPD_CHAN_PASSIVE_SCAN |
2500 HOSTAPD_CHAN_NO_IBSS |
2501 HOSTAPD_CHAN_RADAR));
2502 }
2503 }
2504
2505 return 0;
2506}
2507
2508
2509struct p2p_oper_class_map {
2510 enum hostapd_hw_mode mode;
2511 u8 op_class;
2512 u8 min_chan;
2513 u8 max_chan;
2514 u8 inc;
2515 enum { BW20, BW40PLUS, BW40MINUS } bw;
2516};
2517
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002518static struct p2p_oper_class_map op_class[] = {
2519 { HOSTAPD_MODE_IEEE80211G, 81, 1, 13, 1, BW20 },
2520 { HOSTAPD_MODE_IEEE80211G, 82, 14, 14, 1, BW20 },
2521#if 0 /* Do not enable HT40 on 2 GHz for now */
2522 { HOSTAPD_MODE_IEEE80211G, 83, 1, 9, 1, BW40PLUS },
2523 { HOSTAPD_MODE_IEEE80211G, 84, 5, 13, 1, BW40MINUS },
2524#endif
2525 { HOSTAPD_MODE_IEEE80211A, 115, 36, 48, 4, BW20 },
2526 { HOSTAPD_MODE_IEEE80211A, 124, 149, 161, 4, BW20 },
2527 { HOSTAPD_MODE_IEEE80211A, 116, 36, 44, 8, BW40PLUS },
2528 { HOSTAPD_MODE_IEEE80211A, 117, 40, 48, 8, BW40MINUS },
2529 { HOSTAPD_MODE_IEEE80211A, 126, 149, 157, 8, BW40PLUS },
2530 { HOSTAPD_MODE_IEEE80211A, 127, 153, 161, 8, BW40MINUS },
2531 { -1, 0, 0, 0, 0, BW20 }
2532};
2533
2534
2535static int wpas_p2p_verify_channel(struct wpa_supplicant *wpa_s,
2536 struct hostapd_hw_modes *mode,
2537 u8 channel, u8 bw)
2538{
2539 int flag;
2540
2541 if (!has_channel(wpa_s->global, mode, channel, &flag))
2542 return -1;
2543 if (bw == BW40MINUS &&
2544 (!(flag & HOSTAPD_CHAN_HT40MINUS) ||
2545 !has_channel(wpa_s->global, mode, channel - 4, NULL)))
2546 return 0;
2547 if (bw == BW40PLUS &&
2548 (!(flag & HOSTAPD_CHAN_HT40PLUS) ||
2549 !has_channel(wpa_s->global, mode, channel + 4, NULL)))
2550 return 0;
2551 return 1;
2552}
2553
2554
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002555static int wpas_p2p_setup_channels(struct wpa_supplicant *wpa_s,
2556 struct p2p_channels *chan)
2557{
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002558 struct hostapd_hw_modes *mode;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002559 int cla, op;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002560
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002561 if (wpa_s->hw.modes == NULL) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002562 wpa_printf(MSG_DEBUG, "P2P: Driver did not support fetching "
2563 "of all supported channels; assume dualband "
2564 "support");
2565 return wpas_p2p_default_channels(wpa_s, chan);
2566 }
2567
2568 cla = 0;
2569
2570 for (op = 0; op_class[op].op_class; op++) {
2571 struct p2p_oper_class_map *o = &op_class[op];
2572 u8 ch;
2573 struct p2p_reg_class *reg = NULL;
2574
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002575 mode = get_mode(wpa_s->hw.modes, wpa_s->hw.num_modes, o->mode);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002576 if (mode == NULL)
2577 continue;
2578 for (ch = o->min_chan; ch <= o->max_chan; ch += o->inc) {
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002579 if (wpas_p2p_verify_channel(wpa_s, mode, ch, o->bw) < 1)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002580 continue;
2581 if (reg == NULL) {
2582 wpa_printf(MSG_DEBUG, "P2P: Add operating "
2583 "class %u", o->op_class);
2584 reg = &chan->reg_class[cla];
2585 cla++;
2586 reg->reg_class = o->op_class;
2587 }
2588 reg->channel[reg->channels] = ch;
2589 reg->channels++;
2590 }
2591 if (reg) {
2592 wpa_hexdump(MSG_DEBUG, "P2P: Channels",
2593 reg->channel, reg->channels);
2594 }
2595 }
2596
2597 chan->reg_classes = cla;
2598
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002599 return 0;
2600}
2601
2602
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002603int wpas_p2p_get_ht40_mode(struct wpa_supplicant *wpa_s,
2604 struct hostapd_hw_modes *mode, u8 channel)
2605{
2606 int op, ret;
2607
2608 for (op = 0; op_class[op].op_class; op++) {
2609 struct p2p_oper_class_map *o = &op_class[op];
2610 u8 ch;
2611
2612 for (ch = o->min_chan; ch <= o->max_chan; ch += o->inc) {
2613 if (o->mode != HOSTAPD_MODE_IEEE80211A ||
2614 o->bw == BW20 || ch != channel)
2615 continue;
2616 ret = wpas_p2p_verify_channel(wpa_s, mode, ch, o->bw);
2617 if (ret < 0)
2618 continue;
2619 else if (ret > 0)
2620 return (o->bw == BW40MINUS) ? -1 : 1;
2621 else
2622 return 0;
2623 }
2624 }
2625 return 0;
2626}
2627
2628
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002629static int wpas_get_noa(void *ctx, const u8 *interface_addr, u8 *buf,
2630 size_t buf_len)
2631{
2632 struct wpa_supplicant *wpa_s = ctx;
2633
2634 for (wpa_s = wpa_s->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
2635 if (os_memcmp(wpa_s->own_addr, interface_addr, ETH_ALEN) == 0)
2636 break;
2637 }
2638 if (wpa_s == NULL)
2639 return -1;
2640
2641 return wpa_drv_get_noa(wpa_s, buf, buf_len);
2642}
2643
2644
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08002645static int wpas_go_connected(void *ctx, const u8 *dev_addr)
2646{
2647 struct wpa_supplicant *wpa_s = ctx;
2648
2649 for (wpa_s = wpa_s->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
2650 struct wpa_ssid *ssid = wpa_s->current_ssid;
2651 if (ssid == NULL)
2652 continue;
2653 if (ssid->mode != WPAS_MODE_INFRA)
2654 continue;
2655 if (wpa_s->wpa_state != WPA_COMPLETED &&
2656 wpa_s->wpa_state != WPA_GROUP_HANDSHAKE)
2657 continue;
2658 if (os_memcmp(wpa_s->go_dev_addr, dev_addr, ETH_ALEN) == 0)
2659 return 1;
2660 }
2661
2662 return 0;
2663}
2664
2665
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002666/**
2667 * wpas_p2p_init - Initialize P2P module for %wpa_supplicant
2668 * @global: Pointer to global data from wpa_supplicant_init()
2669 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
2670 * Returns: 0 on success, -1 on failure
2671 */
2672int wpas_p2p_init(struct wpa_global *global, struct wpa_supplicant *wpa_s)
2673{
2674 struct p2p_config p2p;
2675 unsigned int r;
2676 int i;
2677
2678 if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_CAPABLE))
2679 return 0;
2680
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002681 if (global->p2p)
2682 return 0;
2683
2684 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT) {
2685 struct p2p_params params;
2686
2687 wpa_printf(MSG_DEBUG, "P2P: Use driver-based P2P management");
2688 os_memset(&params, 0, sizeof(params));
2689 params.dev_name = wpa_s->conf->device_name;
2690 os_memcpy(params.pri_dev_type, wpa_s->conf->device_type,
2691 WPS_DEV_TYPE_LEN);
2692 params.num_sec_dev_types = wpa_s->conf->num_sec_device_types;
2693 os_memcpy(params.sec_dev_type,
2694 wpa_s->conf->sec_device_type,
2695 params.num_sec_dev_types * WPS_DEV_TYPE_LEN);
2696
2697 if (wpa_drv_p2p_set_params(wpa_s, &params) < 0)
2698 return -1;
2699
2700 return 0;
2701 }
2702
2703 os_memset(&p2p, 0, sizeof(p2p));
2704 p2p.msg_ctx = wpa_s;
2705 p2p.cb_ctx = wpa_s;
2706 p2p.p2p_scan = wpas_p2p_scan;
2707 p2p.send_action = wpas_send_action;
2708 p2p.send_action_done = wpas_send_action_done;
2709 p2p.go_neg_completed = wpas_go_neg_completed;
2710 p2p.go_neg_req_rx = wpas_go_neg_req_rx;
2711 p2p.dev_found = wpas_dev_found;
2712 p2p.dev_lost = wpas_dev_lost;
2713 p2p.start_listen = wpas_start_listen;
2714 p2p.stop_listen = wpas_stop_listen;
2715 p2p.send_probe_resp = wpas_send_probe_resp;
2716 p2p.sd_request = wpas_sd_request;
2717 p2p.sd_response = wpas_sd_response;
2718 p2p.prov_disc_req = wpas_prov_disc_req;
2719 p2p.prov_disc_resp = wpas_prov_disc_resp;
Jouni Malinen75ecf522011-06-27 15:19:46 -07002720 p2p.prov_disc_fail = wpas_prov_disc_fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002721 p2p.invitation_process = wpas_invitation_process;
2722 p2p.invitation_received = wpas_invitation_received;
2723 p2p.invitation_result = wpas_invitation_result;
2724 p2p.get_noa = wpas_get_noa;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08002725 p2p.go_connected = wpas_go_connected;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002726
2727 os_memcpy(wpa_s->global->p2p_dev_addr, wpa_s->own_addr, ETH_ALEN);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002728 os_memcpy(p2p.dev_addr, wpa_s->global->p2p_dev_addr, ETH_ALEN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002729 p2p.dev_name = wpa_s->conf->device_name;
2730 p2p.manufacturer = wpa_s->conf->manufacturer;
2731 p2p.model_name = wpa_s->conf->model_name;
2732 p2p.model_number = wpa_s->conf->model_number;
2733 p2p.serial_number = wpa_s->conf->serial_number;
2734 if (wpa_s->wps) {
2735 os_memcpy(p2p.uuid, wpa_s->wps->uuid, 16);
2736 p2p.config_methods = wpa_s->wps->config_methods;
2737 }
2738
2739 if (wpa_s->conf->p2p_listen_reg_class &&
2740 wpa_s->conf->p2p_listen_channel) {
2741 p2p.reg_class = wpa_s->conf->p2p_listen_reg_class;
2742 p2p.channel = wpa_s->conf->p2p_listen_channel;
2743 } else {
2744 p2p.reg_class = 81;
2745 /*
2746 * Pick one of the social channels randomly as the listen
2747 * channel.
2748 */
2749 os_get_random((u8 *) &r, sizeof(r));
2750 p2p.channel = 1 + (r % 3) * 5;
2751 }
2752 wpa_printf(MSG_DEBUG, "P2P: Own listen channel: %d", p2p.channel);
2753
2754 if (wpa_s->conf->p2p_oper_reg_class &&
2755 wpa_s->conf->p2p_oper_channel) {
2756 p2p.op_reg_class = wpa_s->conf->p2p_oper_reg_class;
2757 p2p.op_channel = wpa_s->conf->p2p_oper_channel;
2758 p2p.cfg_op_channel = 1;
2759 wpa_printf(MSG_DEBUG, "P2P: Configured operating channel: "
2760 "%d:%d", p2p.op_reg_class, p2p.op_channel);
2761
2762 } else {
2763 p2p.op_reg_class = 81;
2764 /*
2765 * Use random operation channel from (1, 6, 11) if no other
2766 * preference is indicated.
2767 */
2768 os_get_random((u8 *) &r, sizeof(r));
2769 p2p.op_channel = 1 + (r % 3) * 5;
2770 p2p.cfg_op_channel = 0;
2771 wpa_printf(MSG_DEBUG, "P2P: Random operating channel: "
2772 "%d:%d", p2p.op_reg_class, p2p.op_channel);
2773 }
2774 if (wpa_s->conf->country[0] && wpa_s->conf->country[1]) {
2775 os_memcpy(p2p.country, wpa_s->conf->country, 2);
2776 p2p.country[2] = 0x04;
2777 } else
2778 os_memcpy(p2p.country, "XX\x04", 3);
2779
2780 if (wpas_p2p_setup_channels(wpa_s, &p2p.channels)) {
2781 wpa_printf(MSG_ERROR, "P2P: Failed to configure supported "
2782 "channel list");
2783 return -1;
2784 }
2785
2786 os_memcpy(p2p.pri_dev_type, wpa_s->conf->device_type,
2787 WPS_DEV_TYPE_LEN);
2788
2789 p2p.num_sec_dev_types = wpa_s->conf->num_sec_device_types;
2790 os_memcpy(p2p.sec_dev_type, wpa_s->conf->sec_device_type,
2791 p2p.num_sec_dev_types * WPS_DEV_TYPE_LEN);
2792
2793 p2p.concurrent_operations = !!(wpa_s->drv_flags &
2794 WPA_DRIVER_FLAGS_P2P_CONCURRENT);
2795
2796 p2p.max_peers = 100;
2797
2798 if (wpa_s->conf->p2p_ssid_postfix) {
2799 p2p.ssid_postfix_len =
2800 os_strlen(wpa_s->conf->p2p_ssid_postfix);
2801 if (p2p.ssid_postfix_len > sizeof(p2p.ssid_postfix))
2802 p2p.ssid_postfix_len = sizeof(p2p.ssid_postfix);
2803 os_memcpy(p2p.ssid_postfix, wpa_s->conf->p2p_ssid_postfix,
2804 p2p.ssid_postfix_len);
2805 }
2806
2807 p2p.p2p_intra_bss = wpa_s->conf->p2p_intra_bss;
2808
Dmitry Shmidt04abaa92012-08-17 16:22:31 -07002809#ifdef ANDROID_P2P
2810 if(wpa_s->drv_flags & WPA_DRIVER_FLAGS_MULTI_CHANNEL_CONCURRENT)
2811 p2p.p2p_concurrency = P2P_MULTI_CHANNEL_CONCURRENT;
2812 else if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_CONCURRENT)
2813 p2p.p2p_concurrency = P2P_SINGLE_CHANNEL_CONCURRENT;
2814#endif
2815
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002816 global->p2p = p2p_init(&p2p);
2817 if (global->p2p == NULL)
2818 return -1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07002819 global->p2p_init_wpa_s = wpa_s;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002820
2821 for (i = 0; i < MAX_WPS_VENDOR_EXT; i++) {
2822 if (wpa_s->conf->wps_vendor_ext[i] == NULL)
2823 continue;
2824 p2p_add_wps_vendor_extension(
2825 global->p2p, wpa_s->conf->wps_vendor_ext[i]);
2826 }
2827
2828 return 0;
2829}
2830
2831
2832/**
2833 * wpas_p2p_deinit - Deinitialize per-interface P2P data
2834 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
2835 *
2836 * This function deinitialize per-interface P2P data.
2837 */
2838void wpas_p2p_deinit(struct wpa_supplicant *wpa_s)
2839{
2840 if (wpa_s->driver && wpa_s->drv_priv)
2841 wpa_drv_probe_req_report(wpa_s, 0);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002842
2843 if (wpa_s->go_params) {
2844 /* Clear any stored provisioning info */
2845 p2p_clear_provisioning_info(
2846 wpa_s->global->p2p,
Dmitry Shmidt04949592012-07-19 12:16:46 -07002847 wpa_s->go_params->peer_device_addr);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002848 }
2849
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002850 os_free(wpa_s->go_params);
2851 wpa_s->go_params = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002852 eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s, NULL);
2853 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08002854 eloop_cancel_timeout(wpas_p2p_pd_before_join_timeout, wpa_s, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002855 wpa_s->p2p_long_listen = 0;
2856 eloop_cancel_timeout(wpas_p2p_long_listen_timeout, wpa_s, NULL);
2857 eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL);
2858 wpas_p2p_remove_pending_group_interface(wpa_s);
2859
2860 /* TODO: remove group interface from the driver if this wpa_s instance
2861 * is on top of a P2P group interface */
2862}
2863
2864
2865/**
2866 * wpas_p2p_deinit_global - Deinitialize global P2P module
2867 * @global: Pointer to global data from wpa_supplicant_init()
2868 *
2869 * This function deinitializes the global (per device) P2P module.
2870 */
2871void wpas_p2p_deinit_global(struct wpa_global *global)
2872{
2873 struct wpa_supplicant *wpa_s, *tmp;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002874
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002875 wpa_s = global->ifaces;
2876 if (wpa_s)
2877 wpas_p2p_service_flush(wpa_s);
2878
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002879 if (global->p2p == NULL)
2880 return;
2881
2882 /* Remove remaining P2P group interfaces */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002883 while (wpa_s && wpa_s->p2p_group_interface != NOT_P2P_GROUP_INTERFACE)
2884 wpa_s = wpa_s->next;
2885 while (wpa_s) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002886 tmp = global->ifaces;
2887 while (tmp &&
2888 (tmp == wpa_s ||
2889 tmp->p2p_group_interface == NOT_P2P_GROUP_INTERFACE)) {
2890 tmp = tmp->next;
2891 }
2892 if (tmp == NULL)
2893 break;
Dmitry Shmidt04949592012-07-19 12:16:46 -07002894 /* Disconnect from the P2P group and deinit the interface */
2895 wpas_p2p_disconnect(tmp);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002896 }
2897
2898 /*
2899 * Deinit GO data on any possibly remaining interface (if main
2900 * interface is used as GO).
2901 */
2902 for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
2903 if (wpa_s->ap_iface)
2904 wpas_p2p_group_deinit(wpa_s);
2905 }
2906
2907 p2p_deinit(global->p2p);
2908 global->p2p = NULL;
Dmitry Shmidt04949592012-07-19 12:16:46 -07002909 global->p2p_init_wpa_s = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002910}
2911
2912
2913static int wpas_p2p_create_iface(struct wpa_supplicant *wpa_s)
2914{
2915 if (wpa_s->drv_flags &
2916 (WPA_DRIVER_FLAGS_P2P_DEDICATED_INTERFACE |
2917 WPA_DRIVER_FLAGS_P2P_MGMT_AND_NON_P2P))
2918 return 1; /* P2P group requires a new interface in every case
2919 */
2920 if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_CONCURRENT))
2921 return 0; /* driver does not support concurrent operations */
2922 if (wpa_s->global->ifaces->next)
2923 return 1; /* more that one interface already in use */
2924 if (wpa_s->wpa_state >= WPA_AUTHENTICATING)
2925 return 1; /* this interface is already in use */
2926 return 0;
2927}
2928
2929
2930static int wpas_p2p_start_go_neg(struct wpa_supplicant *wpa_s,
2931 const u8 *peer_addr,
2932 enum p2p_wps_method wps_method,
2933 int go_intent, const u8 *own_interface_addr,
Dmitry Shmidt04949592012-07-19 12:16:46 -07002934 unsigned int force_freq, int persistent_group,
2935 struct wpa_ssid *ssid)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002936{
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002937 if (persistent_group && wpa_s->conf->persistent_reconnect)
2938 persistent_group = 2;
2939
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002940 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT) {
2941 return wpa_drv_p2p_connect(wpa_s, peer_addr, wps_method,
2942 go_intent, own_interface_addr,
2943 force_freq, persistent_group);
2944 }
2945
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002946 /*
2947 * Increase GO config timeout if HT40 is used since it takes some time
2948 * to scan channels for coex purposes before the BSS can be started.
2949 */
2950 p2p_set_config_timeout(wpa_s->global->p2p,
2951 wpa_s->p2p_go_ht40 ? 255 : 100, 20);
2952
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002953 return p2p_connect(wpa_s->global->p2p, peer_addr, wps_method,
2954 go_intent, own_interface_addr, force_freq,
Dmitry Shmidt04949592012-07-19 12:16:46 -07002955 persistent_group, ssid ? ssid->ssid : NULL,
2956 ssid ? ssid->ssid_len : 0,
2957 wpa_s->p2p_pd_before_go_neg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002958}
2959
2960
2961static int wpas_p2p_auth_go_neg(struct wpa_supplicant *wpa_s,
2962 const u8 *peer_addr,
2963 enum p2p_wps_method wps_method,
2964 int go_intent, const u8 *own_interface_addr,
Dmitry Shmidt04949592012-07-19 12:16:46 -07002965 unsigned int force_freq, int persistent_group,
2966 struct wpa_ssid *ssid)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002967{
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002968 if (persistent_group && wpa_s->conf->persistent_reconnect)
2969 persistent_group = 2;
2970
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002971 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
2972 return -1;
2973
2974 return p2p_authorize(wpa_s->global->p2p, peer_addr, wps_method,
2975 go_intent, own_interface_addr, force_freq,
Dmitry Shmidt04949592012-07-19 12:16:46 -07002976 persistent_group, ssid ? ssid->ssid : NULL,
2977 ssid ? ssid->ssid_len : 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002978}
2979
2980
2981static void wpas_p2p_check_join_scan_limit(struct wpa_supplicant *wpa_s)
2982{
2983 wpa_s->p2p_join_scan_count++;
2984 wpa_printf(MSG_DEBUG, "P2P: Join scan attempt %d",
2985 wpa_s->p2p_join_scan_count);
2986 if (wpa_s->p2p_join_scan_count > P2P_MAX_JOIN_SCAN_ATTEMPTS) {
2987 wpa_printf(MSG_DEBUG, "P2P: Failed to find GO " MACSTR
2988 " for join operationg - stop join attempt",
2989 MAC2STR(wpa_s->pending_join_iface_addr));
2990 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
Dmitry Shmidt04949592012-07-19 12:16:46 -07002991 if (wpa_s->p2p_auto_pd) {
2992 wpa_s->p2p_auto_pd = 0;
2993 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_FAILURE
2994 " p2p_dev_addr=" MACSTR " status=N/A",
2995 MAC2STR(wpa_s->pending_join_dev_addr));
2996 return;
2997 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002998 wpa_msg(wpa_s->parent, MSG_INFO,
2999 P2P_EVENT_GROUP_FORMATION_FAILURE);
3000 }
3001}
3002
3003
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08003004static void wpas_p2p_pd_before_join_timeout(void *eloop_ctx, void *timeout_ctx)
3005{
3006 struct wpa_supplicant *wpa_s = eloop_ctx;
3007 if (!wpa_s->pending_pd_before_join)
3008 return;
3009 /*
3010 * Provision Discovery Response may have been lost - try to connect
3011 * anyway since we do not need any information from this PD.
3012 */
3013 wpa_printf(MSG_DEBUG, "P2P: PD timeout for join-existing-group - "
3014 "try to connect anyway");
3015 wpas_p2p_join_start(wpa_s);
3016}
3017
3018
Dmitry Shmidt04949592012-07-19 12:16:46 -07003019static int wpas_check_freq_conflict(struct wpa_supplicant *wpa_s, int freq)
3020{
3021 struct wpa_supplicant *iface;
3022 int shared_freq;
3023 u8 bssid[ETH_ALEN];
3024
3025 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_MULTI_CHANNEL_CONCURRENT)
3026 return 0;
3027
3028 for (iface = wpa_s->global->ifaces; iface; iface = iface->next) {
3029 if (!wpas_p2p_create_iface(wpa_s) && iface == wpa_s)
3030 continue;
3031 if (iface->current_ssid == NULL || iface->assoc_freq == 0)
3032 continue;
3033 if (iface->current_ssid->mode == WPAS_MODE_AP ||
3034 iface->current_ssid->mode == WPAS_MODE_P2P_GO)
3035 shared_freq = iface->current_ssid->frequency;
3036 else if (wpa_drv_get_bssid(iface, bssid) == 0)
3037 shared_freq = iface->assoc_freq;
3038 else
3039 shared_freq = 0;
3040
3041 if (shared_freq && freq != shared_freq) {
3042 wpa_printf(MSG_DEBUG, "P2P: Frequency conflict - %s "
3043 "connected on %d MHz - new connection on "
3044 "%d MHz", iface->ifname, shared_freq, freq);
3045 return 1;
3046 }
3047 }
3048
3049 shared_freq = wpa_drv_shared_freq(wpa_s);
3050 if (shared_freq > 0 && shared_freq != freq) {
3051 wpa_printf(MSG_DEBUG, "P2P: Frequency conflict - shared "
3052 "virtual interface connected on %d MHz - new "
3053 "connection on %d MHz", shared_freq, freq);
3054 return 1;
3055 }
3056
3057 return 0;
3058}
3059
3060
3061static int wpas_p2p_peer_go(struct wpa_supplicant *wpa_s,
3062 const u8 *peer_dev_addr)
3063{
3064 struct wpa_bss *bss;
3065 int updated;
3066
3067 bss = wpa_bss_get_p2p_dev_addr(wpa_s, peer_dev_addr);
3068 if (bss == NULL)
3069 return -1;
3070 if (bss->last_update_idx < wpa_s->bss_update_idx) {
3071 wpa_printf(MSG_DEBUG, "P2P: Peer BSS entry not updated in the "
3072 "last scan");
3073 return 0;
3074 }
3075
3076 updated = os_time_before(&wpa_s->p2p_auto_started, &bss->last_update);
3077 wpa_printf(MSG_DEBUG, "P2P: Current BSS entry for peer updated at "
3078 "%ld.%06ld (%supdated in last scan)",
3079 bss->last_update.sec, bss->last_update.usec,
3080 updated ? "": "not ");
3081
3082 return updated;
3083}
3084
3085
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003086static void wpas_p2p_scan_res_join(struct wpa_supplicant *wpa_s,
3087 struct wpa_scan_results *scan_res)
3088{
3089 struct wpa_bss *bss;
3090 int freq;
3091 u8 iface_addr[ETH_ALEN];
Dmitry Shmidt04949592012-07-19 12:16:46 -07003092
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003093 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
3094
3095 if (wpa_s->global->p2p_disabled)
3096 return;
3097
Dmitry Shmidt04949592012-07-19 12:16:46 -07003098 wpa_printf(MSG_DEBUG, "P2P: Scan results received (%d BSS) for %sjoin",
3099 scan_res ? (int) scan_res->num : -1,
3100 wpa_s->p2p_auto_join ? "auto_" : "");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003101
3102 if (scan_res)
3103 wpas_p2p_scan_res_handler(wpa_s, scan_res);
3104
Dmitry Shmidt04949592012-07-19 12:16:46 -07003105 if (wpa_s->p2p_auto_pd) {
3106 int join = wpas_p2p_peer_go(wpa_s,
3107 wpa_s->pending_join_dev_addr);
3108 if (join == 0 &&
3109 wpa_s->auto_pd_scan_retry < P2P_AUTO_PD_SCAN_ATTEMPTS) {
3110 wpa_s->auto_pd_scan_retry++;
3111 bss = wpa_bss_get_bssid(wpa_s,
3112 wpa_s->pending_join_dev_addr);
3113 if (bss) {
3114 freq = bss->freq;
3115 wpa_printf(MSG_DEBUG, "P2P: Scan retry %d for "
3116 "the peer " MACSTR " at %d MHz",
3117 wpa_s->auto_pd_scan_retry,
3118 MAC2STR(wpa_s->
3119 pending_join_dev_addr),
3120 freq);
3121 wpas_p2p_join_scan_req(wpa_s, freq);
3122 return;
3123 }
3124 }
3125
3126 if (join < 0)
3127 join = 0;
3128
3129 wpa_s->p2p_auto_pd = 0;
3130 wpa_s->pending_pd_use = join ? AUTO_PD_JOIN : AUTO_PD_GO_NEG;
3131 wpa_printf(MSG_DEBUG, "P2P: Auto PD with " MACSTR " join=%d",
3132 MAC2STR(wpa_s->pending_join_dev_addr), join);
3133 if (p2p_prov_disc_req(wpa_s->global->p2p,
3134 wpa_s->pending_join_dev_addr,
3135 wpa_s->pending_pd_config_methods, join,
3136 0) < 0) {
3137 wpa_s->p2p_auto_pd = 0;
3138 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_FAILURE
3139 " p2p_dev_addr=" MACSTR " status=N/A",
3140 MAC2STR(wpa_s->pending_join_dev_addr));
3141 }
3142 return;
3143 }
3144
3145 if (wpa_s->p2p_auto_join) {
3146 int join = wpas_p2p_peer_go(wpa_s,
3147 wpa_s->pending_join_dev_addr);
3148 if (join < 0) {
3149 wpa_printf(MSG_DEBUG, "P2P: Peer was not found to be "
3150 "running a GO -> use GO Negotiation");
3151 wpas_p2p_connect(wpa_s, wpa_s->pending_join_dev_addr,
3152 wpa_s->p2p_pin, wpa_s->p2p_wps_method,
3153 wpa_s->p2p_persistent_group, 0, 0, 0,
3154 wpa_s->p2p_go_intent,
3155 wpa_s->p2p_connect_freq,
3156 wpa_s->p2p_persistent_id,
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003157 wpa_s->p2p_pd_before_go_neg,
3158 wpa_s->p2p_go_ht40);
Dmitry Shmidt04949592012-07-19 12:16:46 -07003159 return;
3160 }
3161
3162 wpa_printf(MSG_DEBUG, "P2P: Peer was found running GO%s -> "
3163 "try to join the group", join ? "" :
3164 " in older scan");
3165 if (!join)
3166 wpa_s->p2p_fallback_to_go_neg = 1;
3167 }
3168
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003169 freq = p2p_get_oper_freq(wpa_s->global->p2p,
3170 wpa_s->pending_join_iface_addr);
3171 if (freq < 0 &&
3172 p2p_get_interface_addr(wpa_s->global->p2p,
3173 wpa_s->pending_join_dev_addr,
3174 iface_addr) == 0 &&
3175 os_memcmp(iface_addr, wpa_s->pending_join_dev_addr, ETH_ALEN) != 0)
3176 {
3177 wpa_printf(MSG_DEBUG, "P2P: Overwrite pending interface "
3178 "address for join from " MACSTR " to " MACSTR
3179 " based on newly discovered P2P peer entry",
3180 MAC2STR(wpa_s->pending_join_iface_addr),
3181 MAC2STR(iface_addr));
3182 os_memcpy(wpa_s->pending_join_iface_addr, iface_addr,
3183 ETH_ALEN);
3184
3185 freq = p2p_get_oper_freq(wpa_s->global->p2p,
3186 wpa_s->pending_join_iface_addr);
3187 }
3188 if (freq >= 0) {
3189 wpa_printf(MSG_DEBUG, "P2P: Target GO operating frequency "
3190 "from P2P peer table: %d MHz", freq);
3191 }
3192 bss = wpa_bss_get_bssid(wpa_s, wpa_s->pending_join_iface_addr);
3193 if (bss) {
3194 freq = bss->freq;
3195 wpa_printf(MSG_DEBUG, "P2P: Target GO operating frequency "
3196 "from BSS table: %d MHz", freq);
3197 }
3198 if (freq > 0) {
3199 u16 method;
3200
Dmitry Shmidt04949592012-07-19 12:16:46 -07003201 if (wpas_check_freq_conflict(wpa_s, freq) > 0) {
3202 wpa_msg(wpa_s->parent, MSG_INFO,
3203 P2P_EVENT_GROUP_FORMATION_FAILURE
3204 "reason=FREQ_CONFLICT");
3205 return;
3206 }
3207
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003208 wpa_printf(MSG_DEBUG, "P2P: Send Provision Discovery Request "
3209 "prior to joining an existing group (GO " MACSTR
3210 " freq=%u MHz)",
3211 MAC2STR(wpa_s->pending_join_dev_addr), freq);
3212 wpa_s->pending_pd_before_join = 1;
3213
3214 switch (wpa_s->pending_join_wps_method) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003215 case WPS_PIN_DISPLAY:
3216 method = WPS_CONFIG_KEYPAD;
3217 break;
3218 case WPS_PIN_KEYPAD:
3219 method = WPS_CONFIG_DISPLAY;
3220 break;
3221 case WPS_PBC:
3222 method = WPS_CONFIG_PUSHBUTTON;
3223 break;
3224 default:
3225 method = 0;
3226 break;
3227 }
3228
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003229 if ((p2p_get_provisioning_info(wpa_s->global->p2p,
3230 wpa_s->pending_join_dev_addr) ==
3231 method)) {
3232 /*
3233 * We have already performed provision discovery for
3234 * joining the group. Proceed directly to join
3235 * operation without duplicated provision discovery. */
3236 wpa_printf(MSG_DEBUG, "P2P: Provision discovery "
3237 "with " MACSTR " already done - proceed to "
3238 "join",
3239 MAC2STR(wpa_s->pending_join_dev_addr));
3240 wpa_s->pending_pd_before_join = 0;
3241 goto start;
3242 }
3243
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003244 if (p2p_prov_disc_req(wpa_s->global->p2p,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003245 wpa_s->pending_join_dev_addr, method, 1,
3246 freq) < 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003247 wpa_printf(MSG_DEBUG, "P2P: Failed to send Provision "
3248 "Discovery Request before joining an "
3249 "existing group");
3250 wpa_s->pending_pd_before_join = 0;
3251 goto start;
3252 }
3253
3254 /*
3255 * Actual join operation will be started from the Action frame
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08003256 * TX status callback (if no ACK is received) or when the
3257 * Provision Discovery Response is received. Use a short
3258 * timeout as a backup mechanism should the Provision Discovery
3259 * Response be lost for any reason.
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003260 */
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08003261 eloop_cancel_timeout(wpas_p2p_pd_before_join_timeout, wpa_s,
3262 NULL);
3263 eloop_register_timeout(2, 0, wpas_p2p_pd_before_join_timeout,
3264 wpa_s, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003265 return;
3266 }
3267
3268 wpa_printf(MSG_DEBUG, "P2P: Failed to find BSS/GO - try again later");
3269 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
3270 eloop_register_timeout(1, 0, wpas_p2p_join_scan, wpa_s, NULL);
3271 wpas_p2p_check_join_scan_limit(wpa_s);
3272 return;
3273
3274start:
3275 /* Start join operation immediately */
3276 wpas_p2p_join_start(wpa_s);
3277}
3278
3279
Dmitry Shmidt04949592012-07-19 12:16:46 -07003280static void wpas_p2p_join_scan_req(struct wpa_supplicant *wpa_s, int freq)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003281{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003282 int ret;
3283 struct wpa_driver_scan_params params;
3284 struct wpabuf *wps_ie, *ies;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003285 size_t ielen;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003286 int freqs[2] = { 0, 0 };
Dmitry Shmidtad266fb2012-08-24 17:03:35 -07003287#ifdef ANDROID_P2P
3288 int oper_freq;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003289
Dmitry Shmidtad266fb2012-08-24 17:03:35 -07003290 /* If freq is not provided, check the operating freq of the GO and do a
3291 * a directed scan to save time
3292 */
3293 if(!freq) {
3294 freq = (oper_freq = p2p_get_oper_freq(wpa_s->global->p2p,
3295 wpa_s->pending_join_iface_addr) == -1) ? 0 : oper_freq;
3296 }
3297#endif
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003298 os_memset(&params, 0, sizeof(params));
3299
3300 /* P2P Wildcard SSID */
3301 params.num_ssids = 1;
3302 params.ssids[0].ssid = (u8 *) P2P_WILDCARD_SSID;
3303 params.ssids[0].ssid_len = P2P_WILDCARD_SSID_LEN;
3304
3305 wpa_s->wps->dev.p2p = 1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003306 wps_ie = wps_build_probe_req_ie(DEV_PW_DEFAULT, &wpa_s->wps->dev,
3307 wpa_s->wps->uuid, WPS_REQ_ENROLLEE, 0,
3308 NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003309 if (wps_ie == NULL) {
3310 wpas_p2p_scan_res_join(wpa_s, NULL);
3311 return;
3312 }
3313
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003314 ielen = p2p_scan_ie_buf_len(wpa_s->global->p2p);
3315 ies = wpabuf_alloc(wpabuf_len(wps_ie) + ielen);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003316 if (ies == NULL) {
3317 wpabuf_free(wps_ie);
3318 wpas_p2p_scan_res_join(wpa_s, NULL);
3319 return;
3320 }
3321 wpabuf_put_buf(ies, wps_ie);
3322 wpabuf_free(wps_ie);
3323
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08003324 p2p_scan_ie(wpa_s->global->p2p, ies, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003325
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003326 params.p2p_probe = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003327 params.extra_ies = wpabuf_head(ies);
3328 params.extra_ies_len = wpabuf_len(ies);
Dmitry Shmidt04949592012-07-19 12:16:46 -07003329 if (freq > 0) {
3330 freqs[0] = freq;
3331 params.freqs = freqs;
3332 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003333
3334 /*
3335 * Run a scan to update BSS table and start Provision Discovery once
3336 * the new scan results become available.
3337 */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003338 ret = wpa_drv_scan(wpa_s, &params);
Dmitry Shmidt04949592012-07-19 12:16:46 -07003339 if (!ret)
3340 wpa_s->scan_res_handler = wpas_p2p_scan_res_join;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003341
3342 wpabuf_free(ies);
3343
3344 if (ret) {
3345 wpa_printf(MSG_DEBUG, "P2P: Failed to start scan for join - "
3346 "try again later");
3347 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
3348 eloop_register_timeout(1, 0, wpas_p2p_join_scan, wpa_s, NULL);
3349 wpas_p2p_check_join_scan_limit(wpa_s);
3350 }
3351}
3352
3353
Dmitry Shmidt04949592012-07-19 12:16:46 -07003354static void wpas_p2p_join_scan(void *eloop_ctx, void *timeout_ctx)
3355{
3356 struct wpa_supplicant *wpa_s = eloop_ctx;
3357 wpas_p2p_join_scan_req(wpa_s, 0);
3358}
3359
3360
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003361static int wpas_p2p_join(struct wpa_supplicant *wpa_s, const u8 *iface_addr,
Dmitry Shmidt04949592012-07-19 12:16:46 -07003362 const u8 *dev_addr, enum p2p_wps_method wps_method,
3363 int auto_join)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003364{
3365 wpa_printf(MSG_DEBUG, "P2P: Request to join existing group (iface "
Dmitry Shmidt04949592012-07-19 12:16:46 -07003366 MACSTR " dev " MACSTR ")%s",
3367 MAC2STR(iface_addr), MAC2STR(dev_addr),
3368 auto_join ? " (auto_join)" : "");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003369
Dmitry Shmidt04949592012-07-19 12:16:46 -07003370 wpa_s->p2p_auto_pd = 0;
3371 wpa_s->p2p_auto_join = !!auto_join;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003372 os_memcpy(wpa_s->pending_join_iface_addr, iface_addr, ETH_ALEN);
3373 os_memcpy(wpa_s->pending_join_dev_addr, dev_addr, ETH_ALEN);
3374 wpa_s->pending_join_wps_method = wps_method;
3375
3376 /* Make sure we are not running find during connection establishment */
3377 wpas_p2p_stop_find(wpa_s);
3378
3379 wpa_s->p2p_join_scan_count = 0;
3380 wpas_p2p_join_scan(wpa_s, NULL);
3381 return 0;
3382}
3383
3384
3385static int wpas_p2p_join_start(struct wpa_supplicant *wpa_s)
3386{
3387 struct wpa_supplicant *group;
3388 struct p2p_go_neg_results res;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003389 struct wpa_bss *bss;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003390
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08003391 eloop_cancel_timeout(wpas_p2p_pd_before_join_timeout, wpa_s, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003392 group = wpas_p2p_get_group_iface(wpa_s, 0, 0);
3393 if (group == NULL)
3394 return -1;
3395 if (group != wpa_s) {
3396 os_memcpy(group->p2p_pin, wpa_s->p2p_pin,
3397 sizeof(group->p2p_pin));
3398 group->p2p_wps_method = wpa_s->p2p_wps_method;
3399 }
3400
3401 group->p2p_in_provisioning = 1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003402 group->p2p_fallback_to_go_neg = wpa_s->p2p_fallback_to_go_neg;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003403
3404 os_memset(&res, 0, sizeof(res));
3405 os_memcpy(res.peer_interface_addr, wpa_s->pending_join_iface_addr,
3406 ETH_ALEN);
3407 res.wps_method = wpa_s->pending_join_wps_method;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003408 bss = wpa_bss_get_bssid(wpa_s, wpa_s->pending_join_iface_addr);
3409 if (bss) {
3410 res.freq = bss->freq;
3411 res.ssid_len = bss->ssid_len;
3412 os_memcpy(res.ssid, bss->ssid, bss->ssid_len);
3413 }
3414
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08003415 if (wpa_s->off_channel_freq || wpa_s->roc_waiting_drv_freq) {
3416 wpa_printf(MSG_DEBUG, "P2P: Cancel remain-on-channel prior to "
3417 "starting client");
3418 wpa_drv_cancel_remain_on_channel(wpa_s);
3419 wpa_s->off_channel_freq = 0;
3420 wpa_s->roc_waiting_drv_freq = 0;
3421 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003422 wpas_start_wps_enrollee(group, &res);
3423
3424 /*
3425 * Allow a longer timeout for join-a-running-group than normal 15
3426 * second group formation timeout since the GO may not have authorized
3427 * our connection yet.
3428 */
3429 eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s, NULL);
3430 eloop_register_timeout(60, 0, wpas_p2p_group_formation_timeout,
3431 wpa_s, NULL);
3432
3433 return 0;
3434}
3435
3436
3437/**
3438 * wpas_p2p_connect - Request P2P Group Formation to be started
3439 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
3440 * @peer_addr: Address of the peer P2P Device
3441 * @pin: PIN to use during provisioning or %NULL to indicate PBC mode
3442 * @persistent_group: Whether to create a persistent group
Dmitry Shmidt04949592012-07-19 12:16:46 -07003443 * @auto_join: Whether to select join vs. GO Negotiation automatically
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003444 * @join: Whether to join an existing group (as a client) instead of starting
3445 * Group Owner negotiation; @peer_addr is BSSID in that case
3446 * @auth: Whether to only authorize the connection instead of doing that and
3447 * initiating Group Owner negotiation
3448 * @go_intent: GO Intent or -1 to use default
3449 * @freq: Frequency for the group or 0 for auto-selection
Dmitry Shmidt04949592012-07-19 12:16:46 -07003450 * @persistent_id: Persistent group credentials to use for forcing GO
3451 * parameters or -1 to generate new values (SSID/passphrase)
3452 * @pd: Whether to send Provision Discovery prior to GO Negotiation as an
3453 * interoperability workaround when initiating group formation
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003454 * @ht40: Start GO with 40 MHz channel width
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003455 * Returns: 0 or new PIN (if pin was %NULL) on success, -1 on unspecified
3456 * failure, -2 on failure due to channel not currently available,
3457 * -3 if forced channel is not supported
3458 */
3459int wpas_p2p_connect(struct wpa_supplicant *wpa_s, const u8 *peer_addr,
3460 const char *pin, enum p2p_wps_method wps_method,
Dmitry Shmidt04949592012-07-19 12:16:46 -07003461 int persistent_group, int auto_join, int join, int auth,
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003462 int go_intent, int freq, int persistent_id, int pd,
3463 int ht40)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003464{
3465 int force_freq = 0, oper_freq = 0;
3466 u8 bssid[ETH_ALEN];
3467 int ret = 0;
3468 enum wpa_driver_if_type iftype;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003469 const u8 *if_addr;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003470 struct wpa_ssid *ssid = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003471
3472 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
3473 return -1;
3474
Dmitry Shmidt04949592012-07-19 12:16:46 -07003475 if (persistent_id >= 0) {
3476 ssid = wpa_config_get_network(wpa_s->conf, persistent_id);
3477 if (ssid == NULL || ssid->disabled != 2 ||
3478 ssid->mode != WPAS_MODE_P2P_GO)
3479 return -1;
3480 }
3481
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003482 if (go_intent < 0)
3483 go_intent = wpa_s->conf->p2p_go_intent;
3484
3485 if (!auth)
3486 wpa_s->p2p_long_listen = 0;
3487
3488 wpa_s->p2p_wps_method = wps_method;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003489 wpa_s->p2p_persistent_group = !!persistent_group;
3490 wpa_s->p2p_persistent_id = persistent_id;
3491 wpa_s->p2p_go_intent = go_intent;
3492 wpa_s->p2p_connect_freq = freq;
3493 wpa_s->p2p_fallback_to_go_neg = 0;
3494 wpa_s->p2p_pd_before_go_neg = !!pd;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003495 wpa_s->p2p_go_ht40 = !!ht40;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003496
3497 if (pin)
3498 os_strlcpy(wpa_s->p2p_pin, pin, sizeof(wpa_s->p2p_pin));
3499 else if (wps_method == WPS_PIN_DISPLAY) {
3500 ret = wps_generate_pin();
3501 os_snprintf(wpa_s->p2p_pin, sizeof(wpa_s->p2p_pin), "%08d",
3502 ret);
3503 wpa_printf(MSG_DEBUG, "P2P: Randomly generated PIN: %s",
3504 wpa_s->p2p_pin);
3505 } else
3506 wpa_s->p2p_pin[0] = '\0';
3507
Dmitry Shmidt04949592012-07-19 12:16:46 -07003508 if (join || auto_join) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003509 u8 iface_addr[ETH_ALEN], dev_addr[ETH_ALEN];
3510 if (auth) {
3511 wpa_printf(MSG_DEBUG, "P2P: Authorize invitation to "
3512 "connect a running group from " MACSTR,
3513 MAC2STR(peer_addr));
3514 os_memcpy(wpa_s->p2p_auth_invite, peer_addr, ETH_ALEN);
3515 return ret;
3516 }
3517 os_memcpy(dev_addr, peer_addr, ETH_ALEN);
3518 if (p2p_get_interface_addr(wpa_s->global->p2p, peer_addr,
3519 iface_addr) < 0) {
3520 os_memcpy(iface_addr, peer_addr, ETH_ALEN);
3521 p2p_get_dev_addr(wpa_s->global->p2p, peer_addr,
3522 dev_addr);
3523 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07003524 if (auto_join) {
3525 os_get_time(&wpa_s->p2p_auto_started);
3526 wpa_printf(MSG_DEBUG, "P2P: Auto join started at "
3527 "%ld.%06ld",
3528 wpa_s->p2p_auto_started.sec,
3529 wpa_s->p2p_auto_started.usec);
3530 }
3531 if (wpas_p2p_join(wpa_s, iface_addr, dev_addr, wps_method,
3532 auto_join) < 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003533 return -1;
3534 return ret;
3535 }
3536
3537 if (wpa_s->current_ssid && wpa_drv_get_bssid(wpa_s, bssid) == 0 &&
3538 wpa_s->assoc_freq)
3539 oper_freq = wpa_s->assoc_freq;
3540 else {
3541 oper_freq = wpa_drv_shared_freq(wpa_s);
3542 if (oper_freq < 0)
3543 oper_freq = 0;
3544 }
3545
3546 if (freq > 0) {
3547 if (!p2p_supported_freq(wpa_s->global->p2p, freq)) {
3548 wpa_printf(MSG_DEBUG, "P2P: The forced channel "
3549 "(%u MHz) is not supported for P2P uses",
3550 freq);
3551 return -3;
3552 }
3553
3554 if (oper_freq > 0 && freq != oper_freq &&
3555 !(wpa_s->drv_flags &
3556 WPA_DRIVER_FLAGS_MULTI_CHANNEL_CONCURRENT)) {
3557 wpa_printf(MSG_DEBUG, "P2P: Cannot start P2P group "
3558 "on %u MHz while connected on another "
3559 "channel (%u MHz)", freq, oper_freq);
3560 return -2;
3561 }
3562 wpa_printf(MSG_DEBUG, "P2P: Trying to force us to use the "
3563 "requested channel (%u MHz)", freq);
3564 force_freq = freq;
3565 } else if (oper_freq > 0 &&
3566 !p2p_supported_freq(wpa_s->global->p2p, oper_freq)) {
3567 if (!(wpa_s->drv_flags &
3568 WPA_DRIVER_FLAGS_MULTI_CHANNEL_CONCURRENT)) {
3569 wpa_printf(MSG_DEBUG, "P2P: Cannot start P2P group "
3570 "while connected on non-P2P supported "
3571 "channel (%u MHz)", oper_freq);
3572 return -2;
3573 }
3574 wpa_printf(MSG_DEBUG, "P2P: Current operating channel "
3575 "(%u MHz) not available for P2P - try to use "
3576 "another channel", oper_freq);
3577 force_freq = 0;
3578 } else if (oper_freq > 0) {
3579 wpa_printf(MSG_DEBUG, "P2P: Trying to force us to use the "
3580 "channel we are already using (%u MHz) on another "
3581 "interface", oper_freq);
3582 force_freq = oper_freq;
3583 }
3584
3585 wpa_s->create_p2p_iface = wpas_p2p_create_iface(wpa_s);
3586
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003587 if (wpa_s->create_p2p_iface) {
3588 /* Prepare to add a new interface for the group */
3589 iftype = WPA_IF_P2P_GROUP;
3590 if (go_intent == 15)
3591 iftype = WPA_IF_P2P_GO;
3592 if (wpas_p2p_add_group_interface(wpa_s, iftype) < 0) {
3593 wpa_printf(MSG_ERROR, "P2P: Failed to allocate a new "
3594 "interface for the group");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003595 return -1;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003596 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003597
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003598 if_addr = wpa_s->pending_interface_addr;
3599 } else
3600 if_addr = wpa_s->own_addr;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003601
3602 if (auth) {
3603 if (wpas_p2p_auth_go_neg(wpa_s, peer_addr, wps_method,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003604 go_intent, if_addr,
Dmitry Shmidt04949592012-07-19 12:16:46 -07003605 force_freq, persistent_group, ssid) <
3606 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003607 return -1;
3608 return ret;
3609 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003610
3611 if (wpas_p2p_start_go_neg(wpa_s, peer_addr, wps_method,
3612 go_intent, if_addr, force_freq,
Dmitry Shmidt04949592012-07-19 12:16:46 -07003613 persistent_group, ssid) < 0) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003614 if (wpa_s->create_p2p_iface)
3615 wpas_p2p_remove_pending_group_interface(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003616 return -1;
3617 }
3618 return ret;
3619}
3620
3621
3622/**
3623 * wpas_p2p_remain_on_channel_cb - Indication of remain-on-channel start
3624 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
3625 * @freq: Frequency of the channel in MHz
3626 * @duration: Duration of the stay on the channel in milliseconds
3627 *
3628 * This callback is called when the driver indicates that it has started the
3629 * requested remain-on-channel duration.
3630 */
3631void wpas_p2p_remain_on_channel_cb(struct wpa_supplicant *wpa_s,
3632 unsigned int freq, unsigned int duration)
3633{
3634 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
3635 return;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003636 if (wpa_s->off_channel_freq == wpa_s->pending_listen_freq) {
3637 p2p_listen_cb(wpa_s->global->p2p, wpa_s->pending_listen_freq,
3638 wpa_s->pending_listen_duration);
3639 wpa_s->pending_listen_freq = 0;
3640 }
3641}
3642
3643
3644static int wpas_p2p_listen_start(struct wpa_supplicant *wpa_s,
3645 unsigned int timeout)
3646{
3647 /* Limit maximum Listen state time based on driver limitation. */
3648 if (timeout > wpa_s->max_remain_on_chan)
3649 timeout = wpa_s->max_remain_on_chan;
3650
3651 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
3652 return wpa_drv_p2p_listen(wpa_s, timeout);
3653
3654 return p2p_listen(wpa_s->global->p2p, timeout);
3655}
3656
3657
3658/**
3659 * wpas_p2p_cancel_remain_on_channel_cb - Remain-on-channel timeout
3660 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
3661 * @freq: Frequency of the channel in MHz
3662 *
3663 * This callback is called when the driver indicates that a remain-on-channel
3664 * operation has been completed, i.e., the duration on the requested channel
3665 * has timed out.
3666 */
3667void wpas_p2p_cancel_remain_on_channel_cb(struct wpa_supplicant *wpa_s,
3668 unsigned int freq)
3669{
3670 wpa_printf(MSG_DEBUG, "P2P: Cancel remain-on-channel callback "
3671 "(p2p_long_listen=%d ms pending_action_tx=%p)",
3672 wpa_s->p2p_long_listen, wpa_s->pending_action_tx);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003673 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
3674 return;
3675 if (p2p_listen_end(wpa_s->global->p2p, freq) > 0)
3676 return; /* P2P module started a new operation */
3677 if (wpa_s->pending_action_tx)
3678 return;
3679 if (wpa_s->p2p_long_listen > 0)
3680 wpa_s->p2p_long_listen -= wpa_s->max_remain_on_chan;
3681 if (wpa_s->p2p_long_listen > 0) {
3682 wpa_printf(MSG_DEBUG, "P2P: Continuing long Listen state");
3683 wpas_p2p_listen_start(wpa_s, wpa_s->p2p_long_listen);
3684 }
3685}
3686
3687
3688/**
3689 * wpas_p2p_group_remove - Remove a P2P group
3690 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
3691 * @ifname: Network interface name of the group interface or "*" to remove all
3692 * groups
3693 * Returns: 0 on success, -1 on failure
3694 *
3695 * This function is used to remove a P2P group. This can be used to disconnect
3696 * from a group in which the local end is a P2P Client or to end a P2P Group in
3697 * case the local end is the Group Owner. If a virtual network interface was
3698 * created for this group, that interface will be removed. Otherwise, only the
3699 * configured P2P group network will be removed from the interface.
3700 */
3701int wpas_p2p_group_remove(struct wpa_supplicant *wpa_s, const char *ifname)
3702{
3703 struct wpa_global *global = wpa_s->global;
3704
3705 if (os_strcmp(ifname, "*") == 0) {
3706 struct wpa_supplicant *prev;
3707 wpa_s = global->ifaces;
3708 while (wpa_s) {
3709 prev = wpa_s;
3710 wpa_s = wpa_s->next;
3711 wpas_p2p_disconnect(prev);
3712 }
3713 return 0;
3714 }
3715
3716 for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
3717 if (os_strcmp(wpa_s->ifname, ifname) == 0)
3718 break;
3719 }
3720
3721 return wpas_p2p_disconnect(wpa_s);
3722}
3723
3724
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003725static int wpas_p2p_init_go_params(struct wpa_supplicant *wpa_s,
3726 struct p2p_go_neg_results *params,
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003727 int freq, int ht40)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003728{
3729 u8 bssid[ETH_ALEN];
3730 int res;
3731
3732 os_memset(params, 0, sizeof(*params));
3733 params->role_go = 1;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003734 params->ht40 = ht40;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003735 if (freq) {
3736 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on forced "
3737 "frequency %d MHz", freq);
3738 params->freq = freq;
3739 } else if (wpa_s->conf->p2p_oper_reg_class == 81 &&
3740 wpa_s->conf->p2p_oper_channel >= 1 &&
3741 wpa_s->conf->p2p_oper_channel <= 11) {
3742 params->freq = 2407 + 5 * wpa_s->conf->p2p_oper_channel;
3743 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on configured "
3744 "frequency %d MHz", params->freq);
3745 } else if (wpa_s->conf->p2p_oper_reg_class == 115 ||
Jouni Malinen87fd2792011-05-16 18:35:42 +03003746 wpa_s->conf->p2p_oper_reg_class == 124) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003747 params->freq = 5000 + 5 * wpa_s->conf->p2p_oper_channel;
3748 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on configured "
3749 "frequency %d MHz", params->freq);
3750 } else if (wpa_s->conf->p2p_oper_channel == 0 &&
3751 wpa_s->best_overall_freq > 0 &&
3752 p2p_supported_freq(wpa_s->global->p2p,
3753 wpa_s->best_overall_freq)) {
3754 params->freq = wpa_s->best_overall_freq;
3755 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on best overall "
3756 "channel %d MHz", params->freq);
3757 } else if (wpa_s->conf->p2p_oper_channel == 0 &&
3758 wpa_s->best_24_freq > 0 &&
3759 p2p_supported_freq(wpa_s->global->p2p,
3760 wpa_s->best_24_freq)) {
3761 params->freq = wpa_s->best_24_freq;
3762 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on best 2.4 GHz "
3763 "channel %d MHz", params->freq);
3764 } else if (wpa_s->conf->p2p_oper_channel == 0 &&
3765 wpa_s->best_5_freq > 0 &&
3766 p2p_supported_freq(wpa_s->global->p2p,
3767 wpa_s->best_5_freq)) {
3768 params->freq = wpa_s->best_5_freq;
3769 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on best 5 GHz "
3770 "channel %d MHz", params->freq);
3771 } else {
Dmitry Shmidt04949592012-07-19 12:16:46 -07003772 int chan;
3773 for (chan = 0; chan < 11; chan++) {
3774 params->freq = 2412 + chan * 5;
3775 if (!wpas_p2p_disallowed_freq(wpa_s->global,
3776 params->freq))
3777 break;
3778 }
3779 if (chan == 11) {
3780 wpa_printf(MSG_DEBUG, "P2P: No 2.4 GHz channel "
3781 "allowed");
3782 return -1;
3783 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003784 wpa_printf(MSG_DEBUG, "P2P: Set GO freq %d MHz (no preference "
3785 "known)", params->freq);
3786 }
3787
3788 if (wpa_s->current_ssid && wpa_drv_get_bssid(wpa_s, bssid) == 0 &&
3789 wpa_s->assoc_freq && !freq) {
3790 wpa_printf(MSG_DEBUG, "P2P: Force GO on the channel we are "
3791 "already using");
3792 params->freq = wpa_s->assoc_freq;
3793 }
3794
3795 res = wpa_drv_shared_freq(wpa_s);
3796 if (res > 0 && !freq) {
3797 wpa_printf(MSG_DEBUG, "P2P: Force GO on the channel we are "
3798 "already using on a shared interface");
3799 params->freq = res;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003800 } else if (res > 0 && freq != res &&
3801 !(wpa_s->drv_flags &
3802 WPA_DRIVER_FLAGS_MULTI_CHANNEL_CONCURRENT)) {
3803 wpa_printf(MSG_DEBUG, "P2P: Cannot start P2P group on %u MHz "
3804 "while connected on another channel (%u MHz)",
3805 freq, res);
3806 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003807 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003808
3809 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003810}
3811
3812
3813static struct wpa_supplicant *
3814wpas_p2p_get_group_iface(struct wpa_supplicant *wpa_s, int addr_allocated,
3815 int go)
3816{
3817 struct wpa_supplicant *group_wpa_s;
3818
3819 if (!wpas_p2p_create_iface(wpa_s))
3820 return wpa_s;
3821
3822 if (wpas_p2p_add_group_interface(wpa_s, go ? WPA_IF_P2P_GO :
3823 WPA_IF_P2P_CLIENT) < 0)
3824 return NULL;
3825 group_wpa_s = wpas_p2p_init_group_interface(wpa_s, go);
3826 if (group_wpa_s == NULL) {
3827 wpas_p2p_remove_pending_group_interface(wpa_s);
3828 return NULL;
3829 }
3830
3831 return group_wpa_s;
3832}
3833
3834
3835/**
3836 * wpas_p2p_group_add - Add a new P2P group with local end as Group Owner
3837 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
3838 * @persistent_group: Whether to create a persistent group
3839 * @freq: Frequency for the group or 0 to indicate no hardcoding
3840 * Returns: 0 on success, -1 on failure
3841 *
3842 * This function creates a new P2P group with the local end as the Group Owner,
3843 * i.e., without using Group Owner Negotiation.
3844 */
3845int wpas_p2p_group_add(struct wpa_supplicant *wpa_s, int persistent_group,
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003846 int freq, int ht40)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003847{
3848 struct p2p_go_neg_results params;
3849 unsigned int r;
3850
3851 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
3852 return -1;
3853
Dmitry Shmidtdca39792011-09-06 11:17:33 -07003854 /* Make sure we are not running find during connection establishment */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003855 wpa_printf(MSG_DEBUG, "P2P: Stop any on-going P2P FIND");
Dmitry Shmidtdca39792011-09-06 11:17:33 -07003856 wpas_p2p_stop_find(wpa_s);
Dmitry Shmidtdca39792011-09-06 11:17:33 -07003857
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003858 if (freq == 2) {
3859 wpa_printf(MSG_DEBUG, "P2P: Request to start GO on 2.4 GHz "
3860 "band");
3861 if (wpa_s->best_24_freq > 0 &&
3862 p2p_supported_freq(wpa_s->global->p2p,
3863 wpa_s->best_24_freq)) {
3864 freq = wpa_s->best_24_freq;
3865 wpa_printf(MSG_DEBUG, "P2P: Use best 2.4 GHz band "
3866 "channel: %d MHz", freq);
3867 } else {
3868 os_get_random((u8 *) &r, sizeof(r));
3869 freq = 2412 + (r % 3) * 25;
3870 wpa_printf(MSG_DEBUG, "P2P: Use random 2.4 GHz band "
3871 "channel: %d MHz", freq);
3872 }
3873 }
3874
3875 if (freq == 5) {
3876 wpa_printf(MSG_DEBUG, "P2P: Request to start GO on 5 GHz "
3877 "band");
3878 if (wpa_s->best_5_freq > 0 &&
3879 p2p_supported_freq(wpa_s->global->p2p,
3880 wpa_s->best_5_freq)) {
3881 freq = wpa_s->best_5_freq;
3882 wpa_printf(MSG_DEBUG, "P2P: Use best 5 GHz band "
3883 "channel: %d MHz", freq);
3884 } else {
3885 os_get_random((u8 *) &r, sizeof(r));
3886 freq = 5180 + (r % 4) * 20;
3887 if (!p2p_supported_freq(wpa_s->global->p2p, freq)) {
3888 wpa_printf(MSG_DEBUG, "P2P: Could not select "
3889 "5 GHz channel for P2P group");
3890 return -1;
3891 }
3892 wpa_printf(MSG_DEBUG, "P2P: Use random 5 GHz band "
3893 "channel: %d MHz", freq);
3894 }
3895 }
3896
3897 if (freq > 0 && !p2p_supported_freq(wpa_s->global->p2p, freq)) {
3898 wpa_printf(MSG_DEBUG, "P2P: The forced channel for GO "
3899 "(%u MHz) is not supported for P2P uses",
3900 freq);
3901 return -1;
3902 }
3903
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003904 if (wpas_p2p_init_go_params(wpa_s, &params, freq, ht40))
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003905 return -1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003906 if (params.freq &&
3907 !p2p_supported_freq(wpa_s->global->p2p, params.freq)) {
3908 wpa_printf(MSG_DEBUG, "P2P: The selected channel for GO "
3909 "(%u MHz) is not supported for P2P uses",
3910 params.freq);
3911 return -1;
3912 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003913 p2p_go_params(wpa_s->global->p2p, &params);
3914 params.persistent_group = persistent_group;
3915
3916 wpa_s = wpas_p2p_get_group_iface(wpa_s, 0, 1);
3917 if (wpa_s == NULL)
3918 return -1;
3919 wpas_start_wps_go(wpa_s, &params, 0);
3920
3921 return 0;
3922}
3923
3924
3925static int wpas_start_p2p_client(struct wpa_supplicant *wpa_s,
3926 struct wpa_ssid *params, int addr_allocated)
3927{
3928 struct wpa_ssid *ssid;
3929
3930 wpa_s = wpas_p2p_get_group_iface(wpa_s, addr_allocated, 0);
3931 if (wpa_s == NULL)
3932 return -1;
3933
3934 wpa_supplicant_ap_deinit(wpa_s);
3935
3936 ssid = wpa_config_add_network(wpa_s->conf);
3937 if (ssid == NULL)
3938 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003939 wpa_config_set_network_defaults(ssid);
3940 ssid->temporary = 1;
3941 ssid->proto = WPA_PROTO_RSN;
3942 ssid->pairwise_cipher = WPA_CIPHER_CCMP;
3943 ssid->group_cipher = WPA_CIPHER_CCMP;
3944 ssid->key_mgmt = WPA_KEY_MGMT_PSK;
3945 ssid->ssid = os_malloc(params->ssid_len);
3946 if (ssid->ssid == NULL) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003947 wpa_config_remove_network(wpa_s->conf, ssid->id);
3948 return -1;
3949 }
3950 os_memcpy(ssid->ssid, params->ssid, params->ssid_len);
3951 ssid->ssid_len = params->ssid_len;
3952 ssid->p2p_group = 1;
3953 ssid->export_keys = 1;
3954 if (params->psk_set) {
3955 os_memcpy(ssid->psk, params->psk, 32);
3956 ssid->psk_set = 1;
3957 }
3958 if (params->passphrase)
3959 ssid->passphrase = os_strdup(params->passphrase);
3960
3961 wpa_supplicant_select_network(wpa_s, ssid);
3962
3963 wpa_s->show_group_started = 1;
3964
3965 return 0;
3966}
3967
3968
3969int wpas_p2p_group_add_persistent(struct wpa_supplicant *wpa_s,
3970 struct wpa_ssid *ssid, int addr_allocated,
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003971 int freq, int ht40)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003972{
3973 struct p2p_go_neg_results params;
3974 int go = 0;
3975
3976 if (ssid->disabled != 2 || ssid->ssid == NULL)
3977 return -1;
3978
3979 if (wpas_get_p2p_group(wpa_s, ssid->ssid, ssid->ssid_len, &go) &&
3980 go == (ssid->mode == WPAS_MODE_P2P_GO)) {
3981 wpa_printf(MSG_DEBUG, "P2P: Requested persistent group is "
3982 "already running");
3983 return 0;
3984 }
3985
3986 /* Make sure we are not running find during connection establishment */
3987 wpas_p2p_stop_find(wpa_s);
3988
Dmitry Shmidt04949592012-07-19 12:16:46 -07003989 wpa_s->p2p_fallback_to_go_neg = 0;
3990
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003991 if (ssid->mode == WPAS_MODE_INFRA)
3992 return wpas_start_p2p_client(wpa_s, ssid, addr_allocated);
3993
3994 if (ssid->mode != WPAS_MODE_P2P_GO)
3995 return -1;
3996
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003997 if (wpas_p2p_init_go_params(wpa_s, &params, freq, ht40))
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003998 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003999
4000 params.role_go = 1;
4001 if (ssid->passphrase == NULL ||
4002 os_strlen(ssid->passphrase) >= sizeof(params.passphrase)) {
4003 wpa_printf(MSG_DEBUG, "P2P: Invalid passphrase in persistent "
4004 "group");
4005 return -1;
4006 }
4007 os_strlcpy(params.passphrase, ssid->passphrase,
4008 sizeof(params.passphrase));
4009 os_memcpy(params.ssid, ssid->ssid, ssid->ssid_len);
4010 params.ssid_len = ssid->ssid_len;
4011 params.persistent_group = 1;
4012
4013 wpa_s = wpas_p2p_get_group_iface(wpa_s, addr_allocated, 1);
4014 if (wpa_s == NULL)
4015 return -1;
4016
4017 wpas_start_wps_go(wpa_s, &params, 0);
4018
4019 return 0;
4020}
4021
4022
4023static void wpas_p2p_ie_update(void *ctx, struct wpabuf *beacon_ies,
4024 struct wpabuf *proberesp_ies)
4025{
4026 struct wpa_supplicant *wpa_s = ctx;
4027 if (wpa_s->ap_iface) {
4028 struct hostapd_data *hapd = wpa_s->ap_iface->bss[0];
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004029 if (!(hapd->conf->p2p & P2P_GROUP_OWNER)) {
4030 wpabuf_free(beacon_ies);
4031 wpabuf_free(proberesp_ies);
4032 return;
4033 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004034 if (beacon_ies) {
4035 wpabuf_free(hapd->p2p_beacon_ie);
4036 hapd->p2p_beacon_ie = beacon_ies;
4037 }
4038 wpabuf_free(hapd->p2p_probe_resp_ie);
4039 hapd->p2p_probe_resp_ie = proberesp_ies;
4040 } else {
4041 wpabuf_free(beacon_ies);
4042 wpabuf_free(proberesp_ies);
4043 }
4044 wpa_supplicant_ap_update_beacon(wpa_s);
4045}
4046
4047
4048static void wpas_p2p_idle_update(void *ctx, int idle)
4049{
4050 struct wpa_supplicant *wpa_s = ctx;
4051 if (!wpa_s->ap_iface)
4052 return;
4053 wpa_printf(MSG_DEBUG, "P2P: GO - group %sidle", idle ? "" : "not ");
Dmitry Shmidt04949592012-07-19 12:16:46 -07004054 if (idle)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004055 wpas_p2p_set_group_idle_timeout(wpa_s);
4056 else
4057 eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL);
4058}
4059
4060
4061struct p2p_group * wpas_p2p_group_init(struct wpa_supplicant *wpa_s,
Dmitry Shmidt04949592012-07-19 12:16:46 -07004062 struct wpa_ssid *ssid)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004063{
4064 struct p2p_group *group;
4065 struct p2p_group_config *cfg;
4066
4067 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
4068 return NULL;
4069 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
4070 return NULL;
4071
4072 cfg = os_zalloc(sizeof(*cfg));
4073 if (cfg == NULL)
4074 return NULL;
4075
Dmitry Shmidt04949592012-07-19 12:16:46 -07004076 if (ssid->p2p_persistent_group && wpa_s->conf->persistent_reconnect)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004077 cfg->persistent_group = 2;
Dmitry Shmidt04949592012-07-19 12:16:46 -07004078 else if (ssid->p2p_persistent_group)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004079 cfg->persistent_group = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004080 os_memcpy(cfg->interface_addr, wpa_s->own_addr, ETH_ALEN);
4081 if (wpa_s->max_stations &&
4082 wpa_s->max_stations < wpa_s->conf->max_num_sta)
4083 cfg->max_clients = wpa_s->max_stations;
4084 else
4085 cfg->max_clients = wpa_s->conf->max_num_sta;
Dmitry Shmidt04949592012-07-19 12:16:46 -07004086 os_memcpy(cfg->ssid, ssid->ssid, ssid->ssid_len);
4087 cfg->ssid_len = ssid->ssid_len;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004088 cfg->cb_ctx = wpa_s;
4089 cfg->ie_update = wpas_p2p_ie_update;
4090 cfg->idle_update = wpas_p2p_idle_update;
4091
4092 group = p2p_group_init(wpa_s->global->p2p, cfg);
4093 if (group == NULL)
4094 os_free(cfg);
Dmitry Shmidt04949592012-07-19 12:16:46 -07004095 if (ssid->mode != WPAS_MODE_P2P_GROUP_FORMATION)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004096 p2p_group_notif_formation_done(group);
4097 wpa_s->p2p_group = group;
4098 return group;
4099}
4100
4101
4102void wpas_p2p_wps_success(struct wpa_supplicant *wpa_s, const u8 *peer_addr,
4103 int registrar)
4104{
Dmitry Shmidt04949592012-07-19 12:16:46 -07004105 struct wpa_ssid *ssid = wpa_s->current_ssid;
4106
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004107 if (!wpa_s->p2p_in_provisioning) {
4108 wpa_printf(MSG_DEBUG, "P2P: Ignore WPS success event - P2P "
4109 "provisioning not in progress");
4110 return;
4111 }
4112
Dmitry Shmidt04949592012-07-19 12:16:46 -07004113 if (ssid && ssid->mode == WPAS_MODE_INFRA) {
4114 u8 go_dev_addr[ETH_ALEN];
4115 os_memcpy(go_dev_addr, wpa_s->bssid, ETH_ALEN);
4116 wpas_p2p_persistent_group(wpa_s, go_dev_addr, ssid->ssid,
4117 ssid->ssid_len);
4118 /* Clear any stored provisioning info */
4119 p2p_clear_provisioning_info(wpa_s->global->p2p, go_dev_addr);
4120 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004121
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004122 eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s->parent,
4123 NULL);
Dmitry Shmidt04949592012-07-19 12:16:46 -07004124 if (ssid && ssid->mode == WPAS_MODE_INFRA) {
4125 /*
4126 * Use a separate timeout for initial data connection to
4127 * complete to allow the group to be removed automatically if
4128 * something goes wrong in this step before the P2P group idle
4129 * timeout mechanism is taken into use.
4130 */
4131 eloop_register_timeout(P2P_MAX_INITIAL_CONN_WAIT, 0,
4132 wpas_p2p_group_formation_timeout,
4133 wpa_s->parent, NULL);
4134 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004135 if (wpa_s->global->p2p)
4136 p2p_wps_success_cb(wpa_s->global->p2p, peer_addr);
4137 else if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
4138 wpa_drv_wps_success_cb(wpa_s, peer_addr);
4139 wpas_group_formation_completed(wpa_s, 1);
4140}
4141
4142
Jouni Malinen75ecf522011-06-27 15:19:46 -07004143void wpas_p2p_wps_failed(struct wpa_supplicant *wpa_s,
4144 struct wps_event_fail *fail)
4145{
4146 if (!wpa_s->p2p_in_provisioning) {
4147 wpa_printf(MSG_DEBUG, "P2P: Ignore WPS fail event - P2P "
4148 "provisioning not in progress");
4149 return;
4150 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004151
4152 if (wpa_s->go_params) {
4153 p2p_clear_provisioning_info(
4154 wpa_s->global->p2p,
Dmitry Shmidt04949592012-07-19 12:16:46 -07004155 wpa_s->go_params->peer_device_addr);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004156 }
4157
Jouni Malinen75ecf522011-06-27 15:19:46 -07004158 wpas_notify_p2p_wps_failed(wpa_s, fail);
4159}
4160
4161
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004162int wpas_p2p_prov_disc(struct wpa_supplicant *wpa_s, const u8 *peer_addr,
Dmitry Shmidt04949592012-07-19 12:16:46 -07004163 const char *config_method,
4164 enum wpas_p2p_prov_disc_use use)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004165{
4166 u16 config_methods;
4167
Dmitry Shmidt04949592012-07-19 12:16:46 -07004168 wpa_s->p2p_fallback_to_go_neg = 0;
4169 wpa_s->pending_pd_use = NORMAL_PD;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004170 if (os_strncmp(config_method, "display", 7) == 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004171 config_methods = WPS_CONFIG_DISPLAY;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004172 else if (os_strncmp(config_method, "keypad", 6) == 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004173 config_methods = WPS_CONFIG_KEYPAD;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004174 else if (os_strncmp(config_method, "pbc", 3) == 0 ||
4175 os_strncmp(config_method, "pushbutton", 10) == 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004176 config_methods = WPS_CONFIG_PUSHBUTTON;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004177 else {
4178 wpa_printf(MSG_DEBUG, "P2P: Unknown config method");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004179 return -1;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004180 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004181
Dmitry Shmidt04949592012-07-19 12:16:46 -07004182 if (use == WPAS_P2P_PD_AUTO) {
4183 os_memcpy(wpa_s->pending_join_dev_addr, peer_addr, ETH_ALEN);
4184 wpa_s->pending_pd_config_methods = config_methods;
4185 wpa_s->p2p_auto_pd = 1;
4186 wpa_s->p2p_auto_join = 0;
4187 wpa_s->pending_pd_before_join = 0;
4188 wpa_s->auto_pd_scan_retry = 0;
4189 wpas_p2p_stop_find(wpa_s);
4190 wpa_s->p2p_join_scan_count = 0;
4191 os_get_time(&wpa_s->p2p_auto_started);
4192 wpa_printf(MSG_DEBUG, "P2P: Auto PD started at %ld.%06ld",
4193 wpa_s->p2p_auto_started.sec,
4194 wpa_s->p2p_auto_started.usec);
4195 wpas_p2p_join_scan(wpa_s, NULL);
4196 return 0;
4197 }
4198
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004199 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT) {
4200 return wpa_drv_p2p_prov_disc_req(wpa_s, peer_addr,
Dmitry Shmidt04949592012-07-19 12:16:46 -07004201 config_methods,
4202 use == WPAS_P2P_PD_FOR_JOIN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004203 }
4204
4205 if (wpa_s->global->p2p == NULL || wpa_s->global->p2p_disabled)
4206 return -1;
4207
4208 return p2p_prov_disc_req(wpa_s->global->p2p, peer_addr,
Dmitry Shmidt04949592012-07-19 12:16:46 -07004209 config_methods, use == WPAS_P2P_PD_FOR_JOIN,
4210 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004211}
4212
4213
4214int wpas_p2p_scan_result_text(const u8 *ies, size_t ies_len, char *buf,
4215 char *end)
4216{
4217 return p2p_scan_result_text(ies, ies_len, buf, end);
4218}
4219
4220
4221static void wpas_p2p_clear_pending_action_tx(struct wpa_supplicant *wpa_s)
4222{
4223 if (!wpa_s->pending_action_tx)
4224 return;
4225
4226 wpa_printf(MSG_DEBUG, "P2P: Drop pending Action TX due to new "
4227 "operation request");
4228 wpabuf_free(wpa_s->pending_action_tx);
4229 wpa_s->pending_action_tx = NULL;
4230}
4231
4232
4233int wpas_p2p_find(struct wpa_supplicant *wpa_s, unsigned int timeout,
4234 enum p2p_discovery_type type,
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08004235 unsigned int num_req_dev_types, const u8 *req_dev_types,
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004236 const u8 *dev_id, unsigned int search_delay)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004237{
4238 wpas_p2p_clear_pending_action_tx(wpa_s);
4239 wpa_s->p2p_long_listen = 0;
4240
4241 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
4242 return wpa_drv_p2p_find(wpa_s, timeout, type);
4243
Dmitry Shmidt04949592012-07-19 12:16:46 -07004244 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL ||
4245 wpa_s->p2p_in_provisioning)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004246 return -1;
4247
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08004248 wpa_supplicant_cancel_sched_scan(wpa_s);
4249
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004250 return p2p_find(wpa_s->global->p2p, timeout, type,
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004251 num_req_dev_types, req_dev_types, dev_id,
4252 search_delay);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004253}
4254
4255
4256void wpas_p2p_stop_find(struct wpa_supplicant *wpa_s)
4257{
4258 wpas_p2p_clear_pending_action_tx(wpa_s);
4259 wpa_s->p2p_long_listen = 0;
4260 eloop_cancel_timeout(wpas_p2p_long_listen_timeout, wpa_s, NULL);
4261 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
Jouni Malinendc7b7132012-09-14 12:53:47 -07004262 wpa_s->global->p2p_cb_on_scan_complete = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004263
4264 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT) {
4265 wpa_drv_p2p_stop_find(wpa_s);
4266 return;
4267 }
4268
4269 if (wpa_s->global->p2p)
4270 p2p_stop_find(wpa_s->global->p2p);
4271
4272 wpas_p2p_remove_pending_group_interface(wpa_s);
4273}
4274
4275
4276static void wpas_p2p_long_listen_timeout(void *eloop_ctx, void *timeout_ctx)
4277{
4278 struct wpa_supplicant *wpa_s = eloop_ctx;
4279 wpa_s->p2p_long_listen = 0;
4280}
4281
4282
4283int wpas_p2p_listen(struct wpa_supplicant *wpa_s, unsigned int timeout)
4284{
4285 int res;
4286
4287 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
4288 return -1;
4289
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08004290 wpa_supplicant_cancel_sched_scan(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004291 wpas_p2p_clear_pending_action_tx(wpa_s);
4292
4293 if (timeout == 0) {
4294 /*
4295 * This is a request for unlimited Listen state. However, at
4296 * least for now, this is mapped to a Listen state for one
4297 * hour.
4298 */
4299 timeout = 3600;
4300 }
4301 eloop_cancel_timeout(wpas_p2p_long_listen_timeout, wpa_s, NULL);
4302 wpa_s->p2p_long_listen = 0;
4303
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004304 /*
4305 * Stop previous find/listen operation to avoid trying to request a new
4306 * remain-on-channel operation while the driver is still running the
4307 * previous one.
4308 */
4309 if (wpa_s->global->p2p)
4310 p2p_stop_find(wpa_s->global->p2p);
4311
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004312 res = wpas_p2p_listen_start(wpa_s, timeout * 1000);
4313 if (res == 0 && timeout * 1000 > wpa_s->max_remain_on_chan) {
4314 wpa_s->p2p_long_listen = timeout * 1000;
4315 eloop_register_timeout(timeout, 0,
4316 wpas_p2p_long_listen_timeout,
4317 wpa_s, NULL);
4318 }
4319
4320 return res;
4321}
4322
4323
4324int wpas_p2p_assoc_req_ie(struct wpa_supplicant *wpa_s, struct wpa_bss *bss,
4325 u8 *buf, size_t len, int p2p_group)
4326{
4327 struct wpabuf *p2p_ie;
4328 int ret;
4329
4330 if (wpa_s->global->p2p_disabled)
4331 return -1;
4332 if (wpa_s->global->p2p == NULL)
4333 return -1;
4334 if (bss == NULL)
4335 return -1;
4336
4337 p2p_ie = wpa_bss_get_vendor_ie_multi(bss, P2P_IE_VENDOR_TYPE);
4338 ret = p2p_assoc_req_ie(wpa_s->global->p2p, bss->bssid, buf, len,
4339 p2p_group, p2p_ie);
4340 wpabuf_free(p2p_ie);
4341
4342 return ret;
4343}
4344
4345
4346int wpas_p2p_probe_req_rx(struct wpa_supplicant *wpa_s, const u8 *addr,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004347 const u8 *dst, const u8 *bssid,
Dmitry Shmidt04949592012-07-19 12:16:46 -07004348 const u8 *ie, size_t ie_len, int ssi_signal)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004349{
4350 if (wpa_s->global->p2p_disabled)
4351 return 0;
4352 if (wpa_s->global->p2p == NULL)
4353 return 0;
4354
Dmitry Shmidt04949592012-07-19 12:16:46 -07004355 switch (p2p_probe_req_rx(wpa_s->global->p2p, addr, dst, bssid,
4356 ie, ie_len)) {
4357 case P2P_PREQ_NOT_P2P:
4358 wpas_notify_preq(wpa_s, addr, dst, bssid, ie, ie_len,
4359 ssi_signal);
4360 /* fall through */
4361 case P2P_PREQ_MALFORMED:
4362 case P2P_PREQ_NOT_LISTEN:
4363 case P2P_PREQ_NOT_PROCESSED:
4364 default: /* make gcc happy */
4365 return 0;
4366 case P2P_PREQ_PROCESSED:
4367 return 1;
4368 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004369}
4370
4371
4372void wpas_p2p_rx_action(struct wpa_supplicant *wpa_s, const u8 *da,
4373 const u8 *sa, const u8 *bssid,
4374 u8 category, const u8 *data, size_t len, int freq)
4375{
4376 if (wpa_s->global->p2p_disabled)
4377 return;
4378 if (wpa_s->global->p2p == NULL)
4379 return;
4380
4381 p2p_rx_action(wpa_s->global->p2p, da, sa, bssid, category, data, len,
4382 freq);
4383}
4384
4385
4386void wpas_p2p_scan_ie(struct wpa_supplicant *wpa_s, struct wpabuf *ies)
4387{
4388 if (wpa_s->global->p2p_disabled)
4389 return;
4390 if (wpa_s->global->p2p == NULL)
4391 return;
4392
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08004393 p2p_scan_ie(wpa_s->global->p2p, ies, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004394}
4395
4396
4397void wpas_p2p_group_deinit(struct wpa_supplicant *wpa_s)
4398{
4399 p2p_group_deinit(wpa_s->p2p_group);
4400 wpa_s->p2p_group = NULL;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004401
4402 wpa_s->ap_configured_cb = NULL;
4403 wpa_s->ap_configured_cb_ctx = NULL;
4404 wpa_s->ap_configured_cb_data = NULL;
4405 wpa_s->connect_without_scan = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004406}
4407
4408
4409int wpas_p2p_reject(struct wpa_supplicant *wpa_s, const u8 *addr)
4410{
4411 wpa_s->p2p_long_listen = 0;
4412
4413 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
4414 return wpa_drv_p2p_reject(wpa_s, addr);
4415
4416 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
4417 return -1;
4418
4419 return p2p_reject(wpa_s->global->p2p, addr);
4420}
4421
4422
4423/* Invite to reinvoke a persistent group */
4424int wpas_p2p_invite(struct wpa_supplicant *wpa_s, const u8 *peer_addr,
Jouni Malinen31be0a42012-08-31 21:20:51 +03004425 struct wpa_ssid *ssid, const u8 *go_dev_addr, int freq,
4426 int ht40)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004427{
4428 enum p2p_invite_role role;
4429 u8 *bssid = NULL;
4430
Jouni Malinen31be0a42012-08-31 21:20:51 +03004431 wpa_s->p2p_persistent_go_freq = freq;
4432 wpa_s->p2p_go_ht40 = !!ht40;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004433 if (ssid->mode == WPAS_MODE_P2P_GO) {
4434 role = P2P_INVITE_ROLE_GO;
4435 if (peer_addr == NULL) {
4436 wpa_printf(MSG_DEBUG, "P2P: Missing peer "
4437 "address in invitation command");
4438 return -1;
4439 }
4440 if (wpas_p2p_create_iface(wpa_s)) {
4441 if (wpas_p2p_add_group_interface(wpa_s,
4442 WPA_IF_P2P_GO) < 0) {
4443 wpa_printf(MSG_ERROR, "P2P: Failed to "
4444 "allocate a new interface for the "
4445 "group");
4446 return -1;
4447 }
4448 bssid = wpa_s->pending_interface_addr;
4449 } else
4450 bssid = wpa_s->own_addr;
4451 } else {
4452 role = P2P_INVITE_ROLE_CLIENT;
4453 peer_addr = ssid->bssid;
4454 }
4455 wpa_s->pending_invite_ssid_id = ssid->id;
4456
4457 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
4458 return wpa_drv_p2p_invite(wpa_s, peer_addr, role, bssid,
4459 ssid->ssid, ssid->ssid_len,
4460 go_dev_addr, 1);
4461
4462 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
4463 return -1;
4464
4465 return p2p_invite(wpa_s->global->p2p, peer_addr, role, bssid,
Jouni Malinen31be0a42012-08-31 21:20:51 +03004466 ssid->ssid, ssid->ssid_len, freq, go_dev_addr, 1);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004467}
4468
4469
4470/* Invite to join an active group */
4471int wpas_p2p_invite_group(struct wpa_supplicant *wpa_s, const char *ifname,
4472 const u8 *peer_addr, const u8 *go_dev_addr)
4473{
4474 struct wpa_global *global = wpa_s->global;
4475 enum p2p_invite_role role;
4476 u8 *bssid = NULL;
4477 struct wpa_ssid *ssid;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08004478 int persistent;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004479
Jouni Malinen31be0a42012-08-31 21:20:51 +03004480 wpa_s->p2p_persistent_go_freq = 0;
4481 wpa_s->p2p_go_ht40 = 0;
4482
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004483 for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
4484 if (os_strcmp(wpa_s->ifname, ifname) == 0)
4485 break;
4486 }
4487 if (wpa_s == NULL) {
4488 wpa_printf(MSG_DEBUG, "P2P: Interface '%s' not found", ifname);
4489 return -1;
4490 }
4491
4492 ssid = wpa_s->current_ssid;
4493 if (ssid == NULL) {
4494 wpa_printf(MSG_DEBUG, "P2P: No current SSID to use for "
4495 "invitation");
4496 return -1;
4497 }
4498
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08004499 persistent = ssid->p2p_persistent_group &&
4500 wpas_p2p_get_persistent(wpa_s->parent, peer_addr,
4501 ssid->ssid, ssid->ssid_len);
4502
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004503 if (ssid->mode == WPAS_MODE_P2P_GO) {
4504 role = P2P_INVITE_ROLE_ACTIVE_GO;
4505 bssid = wpa_s->own_addr;
4506 if (go_dev_addr == NULL)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004507 go_dev_addr = wpa_s->global->p2p_dev_addr;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004508 } else {
4509 role = P2P_INVITE_ROLE_CLIENT;
4510 if (wpa_s->wpa_state < WPA_ASSOCIATED) {
4511 wpa_printf(MSG_DEBUG, "P2P: Not associated - cannot "
4512 "invite to current group");
4513 return -1;
4514 }
4515 bssid = wpa_s->bssid;
4516 if (go_dev_addr == NULL &&
4517 !is_zero_ether_addr(wpa_s->go_dev_addr))
4518 go_dev_addr = wpa_s->go_dev_addr;
4519 }
4520 wpa_s->parent->pending_invite_ssid_id = -1;
4521
4522 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
4523 return wpa_drv_p2p_invite(wpa_s, peer_addr, role, bssid,
4524 ssid->ssid, ssid->ssid_len,
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08004525 go_dev_addr, persistent);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004526
4527 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
4528 return -1;
4529
4530 return p2p_invite(wpa_s->global->p2p, peer_addr, role, bssid,
4531 ssid->ssid, ssid->ssid_len, wpa_s->assoc_freq,
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08004532 go_dev_addr, persistent);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004533}
4534
4535
4536void wpas_p2p_completed(struct wpa_supplicant *wpa_s)
4537{
4538 struct wpa_ssid *ssid = wpa_s->current_ssid;
4539 const char *ssid_txt;
4540 u8 go_dev_addr[ETH_ALEN];
Jouni Malinen75ecf522011-06-27 15:19:46 -07004541 int network_id = -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004542 int persistent;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004543 int freq;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004544
Dmitry Shmidt04949592012-07-19 12:16:46 -07004545 if (ssid == NULL || ssid->mode != WPAS_MODE_P2P_GROUP_FORMATION) {
4546 eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
4547 wpa_s->parent, NULL);
4548 }
4549
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004550 if (!wpa_s->show_group_started || !ssid)
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004551 goto done;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004552
4553 wpa_s->show_group_started = 0;
4554
4555 ssid_txt = wpa_ssid_txt(ssid->ssid, ssid->ssid_len);
4556 os_memset(go_dev_addr, 0, ETH_ALEN);
4557 if (ssid->bssid_set)
4558 os_memcpy(go_dev_addr, ssid->bssid, ETH_ALEN);
4559 persistent = wpas_p2p_persistent_group(wpa_s, go_dev_addr, ssid->ssid,
4560 ssid->ssid_len);
4561 os_memcpy(wpa_s->go_dev_addr, go_dev_addr, ETH_ALEN);
4562
4563 if (wpa_s->global->p2p_group_formation == wpa_s)
4564 wpa_s->global->p2p_group_formation = NULL;
4565
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004566 freq = wpa_s->current_bss ? wpa_s->current_bss->freq :
4567 (int) wpa_s->assoc_freq;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004568 if (ssid->passphrase == NULL && ssid->psk_set) {
4569 char psk[65];
4570 wpa_snprintf_hex(psk, sizeof(psk), ssid->psk, 32);
4571 wpa_msg(wpa_s->parent, MSG_INFO, P2P_EVENT_GROUP_STARTED
4572 "%s client ssid=\"%s\" freq=%d psk=%s go_dev_addr="
4573 MACSTR "%s",
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004574 wpa_s->ifname, ssid_txt, freq, psk,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004575 MAC2STR(go_dev_addr),
4576 persistent ? " [PERSISTENT]" : "");
4577 } else {
4578 wpa_msg(wpa_s->parent, MSG_INFO, P2P_EVENT_GROUP_STARTED
4579 "%s client ssid=\"%s\" freq=%d passphrase=\"%s\" "
4580 "go_dev_addr=" MACSTR "%s",
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004581 wpa_s->ifname, ssid_txt, freq,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004582 ssid->passphrase ? ssid->passphrase : "",
4583 MAC2STR(go_dev_addr),
4584 persistent ? " [PERSISTENT]" : "");
4585 }
4586
4587 if (persistent)
Jouni Malinen75ecf522011-06-27 15:19:46 -07004588 network_id = wpas_p2p_store_persistent_group(wpa_s->parent,
4589 ssid, go_dev_addr);
4590 if (network_id < 0)
4591 network_id = ssid->id;
4592 wpas_notify_p2p_group_started(wpa_s, ssid, network_id, 1);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004593
4594done:
Jouni Malinendc7b7132012-09-14 12:53:47 -07004595 if (wpa_s->global->p2p_cb_on_scan_complete && !wpa_s->global->p2p_disabled &&
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004596 wpa_s->global->p2p != NULL) {
Jouni Malinendc7b7132012-09-14 12:53:47 -07004597 wpa_s->global->p2p_cb_on_scan_complete = 0;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004598 if (p2p_other_scan_completed(wpa_s->global->p2p) == 1) {
4599 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Pending P2P operation "
4600 "continued after successful connection");
4601 p2p_increase_search_delay(
4602 wpa_s->global->p2p,
4603 wpas_p2p_search_delay(wpa_s));
4604 }
4605 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004606}
4607
4608
4609int wpas_p2p_presence_req(struct wpa_supplicant *wpa_s, u32 duration1,
4610 u32 interval1, u32 duration2, u32 interval2)
4611{
4612 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
4613 return -1;
4614 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
4615 return -1;
4616
4617 if (wpa_s->wpa_state < WPA_ASSOCIATED ||
4618 wpa_s->current_ssid == NULL ||
4619 wpa_s->current_ssid->mode != WPAS_MODE_INFRA)
4620 return -1;
4621
4622 return p2p_presence_req(wpa_s->global->p2p, wpa_s->bssid,
4623 wpa_s->own_addr, wpa_s->assoc_freq,
4624 duration1, interval1, duration2, interval2);
4625}
4626
4627
4628int wpas_p2p_ext_listen(struct wpa_supplicant *wpa_s, unsigned int period,
4629 unsigned int interval)
4630{
4631 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
4632 return -1;
4633
4634 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
4635 return -1;
4636
4637 return p2p_ext_listen(wpa_s->global->p2p, period, interval);
4638}
4639
4640
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004641static int wpas_p2p_is_client(struct wpa_supplicant *wpa_s)
4642{
4643 return wpa_s->current_ssid != NULL &&
4644 wpa_s->current_ssid->p2p_group &&
4645 wpa_s->current_ssid->mode == WPAS_MODE_INFRA;
4646}
4647
4648
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004649static void wpas_p2p_group_idle_timeout(void *eloop_ctx, void *timeout_ctx)
4650{
4651 struct wpa_supplicant *wpa_s = eloop_ctx;
4652
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004653 if (wpa_s->conf->p2p_group_idle == 0 && !wpas_p2p_is_client(wpa_s)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004654 wpa_printf(MSG_DEBUG, "P2P: Ignore group idle timeout - "
4655 "disabled");
4656 return;
4657 }
4658
Dmitry Shmidt04949592012-07-19 12:16:46 -07004659 wpa_printf(MSG_DEBUG, "P2P: Group idle timeout reached - terminate "
4660 "group");
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004661 wpas_p2p_group_delete(wpa_s, P2P_GROUP_REMOVAL_IDLE_TIMEOUT);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004662}
4663
4664
4665static void wpas_p2p_set_group_idle_timeout(struct wpa_supplicant *wpa_s)
4666{
Dmitry Shmidt04949592012-07-19 12:16:46 -07004667 int timeout;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004668
Dmitry Shmidt04949592012-07-19 12:16:46 -07004669 if (eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL) > 0)
4670 wpa_printf(MSG_DEBUG, "P2P: Cancelled P2P group idle timeout");
4671
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004672 if (wpa_s->current_ssid == NULL || !wpa_s->current_ssid->p2p_group)
4673 return;
4674
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004675 timeout = wpa_s->conf->p2p_group_idle;
4676 if (wpa_s->current_ssid->mode == WPAS_MODE_INFRA &&
4677 (timeout == 0 || timeout > P2P_MAX_CLIENT_IDLE))
4678 timeout = P2P_MAX_CLIENT_IDLE;
4679
4680 if (timeout == 0)
4681 return;
4682
Dmitry Shmidt04949592012-07-19 12:16:46 -07004683 if (timeout < 0) {
4684 if (wpa_s->current_ssid->mode == WPAS_MODE_INFRA)
4685 timeout = 0; /* special client mode no-timeout */
4686 else
4687 return;
4688 }
4689
4690 if (wpa_s->p2p_in_provisioning) {
4691 /*
4692 * Use the normal group formation timeout during the
4693 * provisioning phase to avoid terminating this process too
4694 * early due to group idle timeout.
4695 */
4696 wpa_printf(MSG_DEBUG, "P2P: Do not use P2P group idle timeout "
4697 "during provisioning");
4698 return;
4699 }
Dmitry Shmidtad266fb2012-08-24 17:03:35 -07004700#ifndef ANDROID_P2P
Dmitry Shmidt04949592012-07-19 12:16:46 -07004701 if (wpa_s->show_group_started) {
4702 /*
4703 * Use the normal group formation timeout between the end of
4704 * the provisioning phase and completion of 4-way handshake to
4705 * avoid terminating this process too early due to group idle
4706 * timeout.
4707 */
4708 wpa_printf(MSG_DEBUG, "P2P: Do not use P2P group idle timeout "
4709 "while waiting for initial 4-way handshake to "
4710 "complete");
4711 return;
4712 }
Dmitry Shmidtad266fb2012-08-24 17:03:35 -07004713#endif
Dmitry Shmidt04949592012-07-19 12:16:46 -07004714
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004715 wpa_printf(MSG_DEBUG, "P2P: Set P2P group idle timeout to %u seconds",
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004716 timeout);
4717 eloop_register_timeout(timeout, 0, wpas_p2p_group_idle_timeout,
4718 wpa_s, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004719}
4720
4721
Jouni Malinen2b89da82012-08-31 22:04:41 +03004722/* Returns 1 if the interface was removed */
4723int wpas_p2p_deauth_notif(struct wpa_supplicant *wpa_s, const u8 *bssid,
4724 u16 reason_code, const u8 *ie, size_t ie_len,
4725 int locally_generated)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004726{
4727 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
Jouni Malinen2b89da82012-08-31 22:04:41 +03004728 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004729 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
Jouni Malinen2b89da82012-08-31 22:04:41 +03004730 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004731
Dmitry Shmidt04949592012-07-19 12:16:46 -07004732 if (!locally_generated)
4733 p2p_deauth_notif(wpa_s->global->p2p, bssid, reason_code, ie,
4734 ie_len);
4735
4736 if (reason_code == WLAN_REASON_DEAUTH_LEAVING && !locally_generated &&
4737 wpa_s->current_ssid &&
4738 wpa_s->current_ssid->p2p_group &&
4739 wpa_s->current_ssid->mode == WPAS_MODE_INFRA) {
4740 wpa_printf(MSG_DEBUG, "P2P: GO indicated that the P2P Group "
4741 "session is ending");
Jouni Malinen2b89da82012-08-31 22:04:41 +03004742 if (wpas_p2p_group_delete(wpa_s,
4743 P2P_GROUP_REMOVAL_GO_ENDING_SESSION)
4744 > 0)
4745 return 1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07004746 }
Jouni Malinen2b89da82012-08-31 22:04:41 +03004747
4748 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004749}
4750
4751
4752void wpas_p2p_disassoc_notif(struct wpa_supplicant *wpa_s, const u8 *bssid,
Dmitry Shmidt04949592012-07-19 12:16:46 -07004753 u16 reason_code, const u8 *ie, size_t ie_len,
4754 int locally_generated)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004755{
4756 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
4757 return;
4758 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
4759 return;
4760
Dmitry Shmidt04949592012-07-19 12:16:46 -07004761 if (!locally_generated)
4762 p2p_disassoc_notif(wpa_s->global->p2p, bssid, reason_code, ie,
4763 ie_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004764}
4765
4766
4767void wpas_p2p_update_config(struct wpa_supplicant *wpa_s)
4768{
4769 struct p2p_data *p2p = wpa_s->global->p2p;
4770
4771 if (p2p == NULL)
4772 return;
4773
4774 if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_CAPABLE))
4775 return;
4776
4777 if (wpa_s->conf->changed_parameters & CFG_CHANGED_DEVICE_NAME)
4778 p2p_set_dev_name(p2p, wpa_s->conf->device_name);
4779
4780 if (wpa_s->conf->changed_parameters & CFG_CHANGED_DEVICE_TYPE)
4781 p2p_set_pri_dev_type(p2p, wpa_s->conf->device_type);
4782
4783 if (wpa_s->wps &&
4784 (wpa_s->conf->changed_parameters & CFG_CHANGED_CONFIG_METHODS))
4785 p2p_set_config_methods(p2p, wpa_s->wps->config_methods);
4786
4787 if (wpa_s->wps && (wpa_s->conf->changed_parameters & CFG_CHANGED_UUID))
4788 p2p_set_uuid(p2p, wpa_s->wps->uuid);
4789
4790 if (wpa_s->conf->changed_parameters & CFG_CHANGED_WPS_STRING) {
4791 p2p_set_manufacturer(p2p, wpa_s->conf->manufacturer);
4792 p2p_set_model_name(p2p, wpa_s->conf->model_name);
4793 p2p_set_model_number(p2p, wpa_s->conf->model_number);
4794 p2p_set_serial_number(p2p, wpa_s->conf->serial_number);
4795 }
4796
4797 if (wpa_s->conf->changed_parameters & CFG_CHANGED_SEC_DEVICE_TYPE)
4798 p2p_set_sec_dev_types(p2p,
4799 (void *) wpa_s->conf->sec_device_type,
4800 wpa_s->conf->num_sec_device_types);
4801
4802 if (wpa_s->conf->changed_parameters & CFG_CHANGED_VENDOR_EXTENSION) {
4803 int i;
4804 p2p_remove_wps_vendor_extensions(p2p);
4805 for (i = 0; i < MAX_WPS_VENDOR_EXT; i++) {
4806 if (wpa_s->conf->wps_vendor_ext[i] == NULL)
4807 continue;
4808 p2p_add_wps_vendor_extension(
4809 p2p, wpa_s->conf->wps_vendor_ext[i]);
4810 }
4811 }
4812
4813 if ((wpa_s->conf->changed_parameters & CFG_CHANGED_COUNTRY) &&
4814 wpa_s->conf->country[0] && wpa_s->conf->country[1]) {
4815 char country[3];
4816 country[0] = wpa_s->conf->country[0];
4817 country[1] = wpa_s->conf->country[1];
4818 country[2] = 0x04;
4819 p2p_set_country(p2p, country);
4820 }
4821
4822 if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_SSID_POSTFIX) {
4823 p2p_set_ssid_postfix(p2p, (u8 *) wpa_s->conf->p2p_ssid_postfix,
4824 wpa_s->conf->p2p_ssid_postfix ?
4825 os_strlen(wpa_s->conf->p2p_ssid_postfix) :
4826 0);
4827 }
4828
4829 if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_INTRA_BSS)
4830 p2p_set_intra_bss_dist(p2p, wpa_s->conf->p2p_intra_bss);
Jouni Malinen75ecf522011-06-27 15:19:46 -07004831
4832 if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_LISTEN_CHANNEL) {
4833 u8 reg_class, channel;
4834 int ret;
4835 unsigned int r;
4836 if (wpa_s->conf->p2p_listen_reg_class &&
4837 wpa_s->conf->p2p_listen_channel) {
4838 reg_class = wpa_s->conf->p2p_listen_reg_class;
4839 channel = wpa_s->conf->p2p_listen_channel;
4840 } else {
4841 reg_class = 81;
4842 /*
4843 * Pick one of the social channels randomly as the
4844 * listen channel.
4845 */
4846 os_get_random((u8 *) &r, sizeof(r));
4847 channel = 1 + (r % 3) * 5;
4848 }
4849 ret = p2p_set_listen_channel(p2p, reg_class, channel);
4850 if (ret)
4851 wpa_printf(MSG_ERROR, "P2P: Own listen channel update "
4852 "failed: %d", ret);
4853 }
4854 if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_OPER_CHANNEL) {
4855 u8 op_reg_class, op_channel, cfg_op_channel;
4856 int ret = 0;
4857 unsigned int r;
4858 if (wpa_s->conf->p2p_oper_reg_class &&
4859 wpa_s->conf->p2p_oper_channel) {
4860 op_reg_class = wpa_s->conf->p2p_oper_reg_class;
4861 op_channel = wpa_s->conf->p2p_oper_channel;
4862 cfg_op_channel = 1;
4863 } else {
4864 op_reg_class = 81;
4865 /*
4866 * Use random operation channel from (1, 6, 11)
4867 *if no other preference is indicated.
4868 */
4869 os_get_random((u8 *) &r, sizeof(r));
4870 op_channel = 1 + (r % 3) * 5;
4871 cfg_op_channel = 0;
4872 }
4873 ret = p2p_set_oper_channel(p2p, op_reg_class, op_channel,
4874 cfg_op_channel);
4875 if (ret)
4876 wpa_printf(MSG_ERROR, "P2P: Own oper channel update "
4877 "failed: %d", ret);
4878 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07004879
4880 if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_PREF_CHAN) {
4881 if (p2p_set_pref_chan(p2p, wpa_s->conf->num_p2p_pref_chan,
4882 wpa_s->conf->p2p_pref_chan) < 0) {
4883 wpa_printf(MSG_ERROR, "P2P: Preferred channel list "
4884 "update failed");
4885 }
4886 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004887}
4888
4889
4890int wpas_p2p_set_noa(struct wpa_supplicant *wpa_s, u8 count, int start,
4891 int duration)
4892{
4893 if (!wpa_s->ap_iface)
4894 return -1;
4895 return hostapd_p2p_set_noa(wpa_s->ap_iface->bss[0], count, start,
4896 duration);
4897}
4898
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004899
4900int wpas_p2p_set_cross_connect(struct wpa_supplicant *wpa_s, int enabled)
4901{
4902 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
4903 return -1;
4904 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
4905 return -1;
4906
4907 wpa_s->global->cross_connection = enabled;
4908 p2p_set_cross_connect(wpa_s->global->p2p, enabled);
4909
4910 if (!enabled) {
4911 struct wpa_supplicant *iface;
4912
4913 for (iface = wpa_s->global->ifaces; iface; iface = iface->next)
4914 {
4915 if (iface->cross_connect_enabled == 0)
4916 continue;
4917
4918 iface->cross_connect_enabled = 0;
4919 iface->cross_connect_in_use = 0;
4920 wpa_msg(iface->parent, MSG_INFO,
4921 P2P_EVENT_CROSS_CONNECT_DISABLE "%s %s",
4922 iface->ifname, iface->cross_connect_uplink);
4923 }
4924 }
4925
4926 return 0;
4927}
4928
4929
4930static void wpas_p2p_enable_cross_connect(struct wpa_supplicant *uplink)
4931{
4932 struct wpa_supplicant *iface;
4933
4934 if (!uplink->global->cross_connection)
4935 return;
4936
4937 for (iface = uplink->global->ifaces; iface; iface = iface->next) {
4938 if (!iface->cross_connect_enabled)
4939 continue;
4940 if (os_strcmp(uplink->ifname, iface->cross_connect_uplink) !=
4941 0)
4942 continue;
4943 if (iface->ap_iface == NULL)
4944 continue;
4945 if (iface->cross_connect_in_use)
4946 continue;
4947
4948 iface->cross_connect_in_use = 1;
4949 wpa_msg(iface->parent, MSG_INFO,
4950 P2P_EVENT_CROSS_CONNECT_ENABLE "%s %s",
4951 iface->ifname, iface->cross_connect_uplink);
4952 }
4953}
4954
4955
4956static void wpas_p2p_disable_cross_connect(struct wpa_supplicant *uplink)
4957{
4958 struct wpa_supplicant *iface;
4959
4960 for (iface = uplink->global->ifaces; iface; iface = iface->next) {
4961 if (!iface->cross_connect_enabled)
4962 continue;
4963 if (os_strcmp(uplink->ifname, iface->cross_connect_uplink) !=
4964 0)
4965 continue;
4966 if (!iface->cross_connect_in_use)
4967 continue;
4968
4969 wpa_msg(iface->parent, MSG_INFO,
4970 P2P_EVENT_CROSS_CONNECT_DISABLE "%s %s",
4971 iface->ifname, iface->cross_connect_uplink);
4972 iface->cross_connect_in_use = 0;
4973 }
4974}
4975
4976
4977void wpas_p2p_notif_connected(struct wpa_supplicant *wpa_s)
4978{
4979 if (wpa_s->ap_iface || wpa_s->current_ssid == NULL ||
4980 wpa_s->current_ssid->mode != WPAS_MODE_INFRA ||
4981 wpa_s->cross_connect_disallowed)
4982 wpas_p2p_disable_cross_connect(wpa_s);
4983 else
4984 wpas_p2p_enable_cross_connect(wpa_s);
Dmitry Shmidt04949592012-07-19 12:16:46 -07004985 if (!wpa_s->ap_iface &&
4986 eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL) > 0)
4987 wpa_printf(MSG_DEBUG, "P2P: Cancelled P2P group idle timeout");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004988}
4989
4990
4991void wpas_p2p_notif_disconnected(struct wpa_supplicant *wpa_s)
4992{
4993 wpas_p2p_disable_cross_connect(wpa_s);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004994 if (!wpa_s->ap_iface &&
4995 !eloop_is_timeout_registered(wpas_p2p_group_idle_timeout,
4996 wpa_s, NULL))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004997 wpas_p2p_set_group_idle_timeout(wpa_s);
4998}
4999
5000
5001static void wpas_p2p_cross_connect_setup(struct wpa_supplicant *wpa_s)
5002{
5003 struct wpa_supplicant *iface;
5004
5005 if (!wpa_s->global->cross_connection)
5006 return;
5007
5008 for (iface = wpa_s->global->ifaces; iface; iface = iface->next) {
5009 if (iface == wpa_s)
5010 continue;
5011 if (iface->drv_flags &
5012 WPA_DRIVER_FLAGS_P2P_DEDICATED_INTERFACE)
5013 continue;
5014 if (iface->drv_flags & WPA_DRIVER_FLAGS_P2P_CAPABLE)
5015 continue;
5016
5017 wpa_s->cross_connect_enabled = 1;
5018 os_strlcpy(wpa_s->cross_connect_uplink, iface->ifname,
5019 sizeof(wpa_s->cross_connect_uplink));
5020 wpa_printf(MSG_DEBUG, "P2P: Enable cross connection from "
5021 "%s to %s whenever uplink is available",
5022 wpa_s->ifname, wpa_s->cross_connect_uplink);
5023
5024 if (iface->ap_iface || iface->current_ssid == NULL ||
5025 iface->current_ssid->mode != WPAS_MODE_INFRA ||
5026 iface->cross_connect_disallowed ||
5027 iface->wpa_state != WPA_COMPLETED)
5028 break;
5029
5030 wpa_s->cross_connect_in_use = 1;
5031 wpa_msg(wpa_s->parent, MSG_INFO,
5032 P2P_EVENT_CROSS_CONNECT_ENABLE "%s %s",
5033 wpa_s->ifname, wpa_s->cross_connect_uplink);
5034 break;
5035 }
5036}
5037
5038
5039int wpas_p2p_notif_pbc_overlap(struct wpa_supplicant *wpa_s)
5040{
5041 if (wpa_s->p2p_group_interface != P2P_GROUP_INTERFACE_CLIENT &&
5042 !wpa_s->p2p_in_provisioning)
5043 return 0; /* not P2P client operation */
5044
5045 wpa_printf(MSG_DEBUG, "P2P: Terminate connection due to WPS PBC "
5046 "session overlap");
5047 if (wpa_s != wpa_s->parent)
5048 wpa_msg_ctrl(wpa_s->parent, MSG_INFO, WPS_EVENT_OVERLAP);
5049
5050 if (wpa_s->global->p2p)
5051 p2p_group_formation_failed(wpa_s->global->p2p);
5052
5053 eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
5054 wpa_s->parent, NULL);
5055
5056 wpas_group_formation_completed(wpa_s, 0);
5057 return 1;
5058}
5059
5060
5061void wpas_p2p_update_channel_list(struct wpa_supplicant *wpa_s)
5062{
5063 struct p2p_channels chan;
5064
5065 if (wpa_s->global == NULL || wpa_s->global->p2p == NULL)
5066 return;
5067
5068 os_memset(&chan, 0, sizeof(chan));
5069 if (wpas_p2p_setup_channels(wpa_s, &chan)) {
5070 wpa_printf(MSG_ERROR, "P2P: Failed to update supported "
5071 "channel list");
5072 return;
5073 }
5074
5075 p2p_update_channel_list(wpa_s->global->p2p, &chan);
5076}
5077
5078
5079int wpas_p2p_cancel(struct wpa_supplicant *wpa_s)
5080{
5081 struct wpa_global *global = wpa_s->global;
5082 int found = 0;
5083 const u8 *peer;
5084
5085 if (global->p2p == NULL)
5086 return -1;
5087
5088 wpa_printf(MSG_DEBUG, "P2P: Request to cancel group formation");
5089
5090 if (wpa_s->pending_interface_name[0] &&
5091 !is_zero_ether_addr(wpa_s->pending_interface_addr))
5092 found = 1;
5093
5094 peer = p2p_get_go_neg_peer(global->p2p);
5095 if (peer) {
5096 wpa_printf(MSG_DEBUG, "P2P: Unauthorize pending GO Neg peer "
5097 MACSTR, MAC2STR(peer));
5098 p2p_unauthorize(global->p2p, peer);
Dmitry Shmidt04949592012-07-19 12:16:46 -07005099 found = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005100 }
5101
5102 wpas_p2p_stop_find(wpa_s);
5103
5104 for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
5105 if (wpa_s == global->p2p_group_formation &&
5106 (wpa_s->p2p_in_provisioning ||
5107 wpa_s->parent->pending_interface_type ==
5108 WPA_IF_P2P_CLIENT)) {
5109 wpa_printf(MSG_DEBUG, "P2P: Interface %s in group "
5110 "formation found - cancelling",
5111 wpa_s->ifname);
5112 found = 1;
5113 eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
5114 wpa_s->parent, NULL);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005115 wpas_p2p_group_delete(wpa_s,
5116 P2P_GROUP_REMOVAL_REQUESTED);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005117 break;
5118 }
5119 }
5120
5121 if (!found) {
5122 wpa_printf(MSG_DEBUG, "P2P: No ongoing group formation found");
5123 return -1;
5124 }
5125
5126 return 0;
5127}
5128
5129
5130void wpas_p2p_interface_unavailable(struct wpa_supplicant *wpa_s)
5131{
5132 if (wpa_s->current_ssid == NULL || !wpa_s->current_ssid->p2p_group)
5133 return;
5134
5135 wpa_printf(MSG_DEBUG, "P2P: Remove group due to driver resource not "
5136 "being available anymore");
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005137 wpas_p2p_group_delete(wpa_s, P2P_GROUP_REMOVAL_UNAVAILABLE);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005138}
5139
5140
5141void wpas_p2p_update_best_channels(struct wpa_supplicant *wpa_s,
5142 int freq_24, int freq_5, int freq_overall)
5143{
5144 struct p2p_data *p2p = wpa_s->global->p2p;
5145 if (p2p == NULL || (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT))
5146 return;
5147 p2p_set_best_channels(p2p, freq_24, freq_5, freq_overall);
5148}
5149
5150
5151int wpas_p2p_unauthorize(struct wpa_supplicant *wpa_s, const char *addr)
5152{
5153 u8 peer[ETH_ALEN];
5154 struct p2p_data *p2p = wpa_s->global->p2p;
5155
5156 if (p2p == NULL || (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT))
5157 return -1;
5158
5159 if (hwaddr_aton(addr, peer))
5160 return -1;
5161
5162 return p2p_unauthorize(p2p, peer);
5163}
5164
5165
5166/**
5167 * wpas_p2p_disconnect - Disconnect from a P2P Group
5168 * @wpa_s: Pointer to wpa_supplicant data
5169 * Returns: 0 on success, -1 on failure
5170 *
5171 * This can be used to disconnect from a group in which the local end is a P2P
5172 * Client or to end a P2P Group in case the local end is the Group Owner. If a
5173 * virtual network interface was created for this group, that interface will be
5174 * removed. Otherwise, only the configured P2P group network will be removed
5175 * from the interface.
5176 */
5177int wpas_p2p_disconnect(struct wpa_supplicant *wpa_s)
5178{
5179
5180 if (wpa_s == NULL)
5181 return -1;
5182
Jouni Malinen2b89da82012-08-31 22:04:41 +03005183 return wpas_p2p_group_delete(wpa_s, P2P_GROUP_REMOVAL_REQUESTED) < 0 ?
5184 -1 : 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005185}
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005186
5187
5188int wpas_p2p_in_progress(struct wpa_supplicant *wpa_s)
5189{
5190 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
5191 return 0;
5192
5193 return p2p_in_progress(wpa_s->global->p2p);
5194}
5195
5196
5197void wpas_p2p_network_removed(struct wpa_supplicant *wpa_s,
5198 struct wpa_ssid *ssid)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005199{
5200 if (wpa_s->p2p_in_provisioning && ssid->p2p_group &&
5201 eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
5202 wpa_s->parent, NULL) > 0) {
Dmitry Shmidt04949592012-07-19 12:16:46 -07005203 /**
5204 * Remove the network by scheduling the group formation
5205 * timeout to happen immediately. The teardown code
5206 * needs to be scheduled to run asynch later so that we
5207 * don't delete data from under ourselves unexpectedly.
5208 * Calling wpas_p2p_group_formation_timeout directly
5209 * causes a series of crashes in WPS failure scenarios.
5210 */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005211 wpa_printf(MSG_DEBUG, "P2P: Canceled group formation due to "
5212 "P2P group network getting removed");
Dmitry Shmidt04949592012-07-19 12:16:46 -07005213 eloop_register_timeout(0, 0, wpas_p2p_group_formation_timeout,
5214 wpa_s->parent, NULL);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005215 }
5216}
5217
5218
5219struct wpa_ssid * wpas_p2p_get_persistent(struct wpa_supplicant *wpa_s,
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08005220 const u8 *addr, const u8 *ssid,
5221 size_t ssid_len)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005222{
5223 struct wpa_ssid *s;
5224 size_t i;
5225
5226 for (s = wpa_s->conf->ssid; s; s = s->next) {
5227 if (s->disabled != 2)
5228 continue;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08005229 if (ssid &&
5230 (ssid_len != s->ssid_len ||
5231 os_memcmp(ssid, s->ssid, ssid_len) != 0))
5232 continue;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005233 if (os_memcmp(s->bssid, addr, ETH_ALEN) == 0)
5234 return s; /* peer is GO in the persistent group */
5235 if (s->mode != WPAS_MODE_P2P_GO || s->p2p_client_list == NULL)
5236 continue;
5237 for (i = 0; i < s->num_p2p_clients; i++) {
5238 if (os_memcmp(s->p2p_client_list + i * ETH_ALEN,
5239 addr, ETH_ALEN) == 0)
5240 return s; /* peer is P2P client in persistent
5241 * group */
5242 }
5243 }
5244
5245 return NULL;
5246}
5247
5248
5249void wpas_p2p_notify_ap_sta_authorized(struct wpa_supplicant *wpa_s,
5250 const u8 *addr)
5251{
5252 if (addr == NULL)
5253 return;
5254 wpas_p2p_add_persistent_group_client(wpa_s, addr);
5255}
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08005256
Dmitry Shmidt04949592012-07-19 12:16:46 -07005257
5258static void wpas_p2p_fallback_to_go_neg(struct wpa_supplicant *wpa_s,
5259 int group_added)
5260{
5261 struct wpa_supplicant *group = wpa_s;
5262 eloop_cancel_timeout(wpas_p2p_pd_before_join_timeout, wpa_s, NULL);
5263 if (wpa_s->global->p2p_group_formation)
5264 group = wpa_s->global->p2p_group_formation;
5265 wpa_s = wpa_s->parent;
5266 offchannel_send_action_done(wpa_s);
5267 if (group_added)
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005268 wpas_p2p_group_delete(group, P2P_GROUP_REMOVAL_SILENT);
Dmitry Shmidt04949592012-07-19 12:16:46 -07005269 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Fall back to GO Negotiation");
5270 wpas_p2p_connect(wpa_s, wpa_s->pending_join_dev_addr, wpa_s->p2p_pin,
5271 wpa_s->p2p_wps_method, wpa_s->p2p_persistent_group, 0,
5272 0, 0, wpa_s->p2p_go_intent, wpa_s->p2p_connect_freq,
5273 wpa_s->p2p_persistent_id,
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005274 wpa_s->p2p_pd_before_go_neg,
5275 wpa_s->p2p_go_ht40);
Dmitry Shmidt04949592012-07-19 12:16:46 -07005276}
5277
5278
5279int wpas_p2p_scan_no_go_seen(struct wpa_supplicant *wpa_s)
5280{
5281 if (!wpa_s->p2p_fallback_to_go_neg ||
5282 wpa_s->p2p_in_provisioning <= 5)
5283 return 0;
5284
5285 if (wpas_p2p_peer_go(wpa_s, wpa_s->pending_join_dev_addr) > 0)
5286 return 0; /* peer operating as a GO */
5287
5288 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: GO not found for p2p_connect-auto - "
5289 "fallback to GO Negotiation");
5290 wpas_p2p_fallback_to_go_neg(wpa_s, 1);
5291
5292 return 1;
5293}
5294
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005295
5296unsigned int wpas_p2p_search_delay(struct wpa_supplicant *wpa_s)
5297{
5298 const char *rn, *rn2;
5299 struct wpa_supplicant *ifs;
5300
5301 if (wpa_s->wpa_state > WPA_SCANNING) {
5302 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Use %u ms search delay due to "
5303 "concurrent operation",
5304 P2P_CONCURRENT_SEARCH_DELAY);
5305 return P2P_CONCURRENT_SEARCH_DELAY;
5306 }
5307
5308 if (!wpa_s->driver->get_radio_name)
5309 return 0;
5310 rn = wpa_s->driver->get_radio_name(wpa_s->drv_priv);
5311 if (rn == NULL || rn[0] == '\0')
5312 return 0;
5313
5314 for (ifs = wpa_s->global->ifaces; ifs; ifs = ifs->next) {
5315 if (ifs == wpa_s || !ifs->driver->get_radio_name)
5316 continue;
5317
5318 rn2 = ifs->driver->get_radio_name(ifs->drv_priv);
5319 if (!rn2 || os_strcmp(rn, rn2) != 0)
5320 continue;
5321 if (ifs->wpa_state > WPA_SCANNING) {
5322 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Use %u ms search "
5323 "delay due to concurrent operation on "
5324 "interface %s",
5325 P2P_CONCURRENT_SEARCH_DELAY, ifs->ifname);
5326 return P2P_CONCURRENT_SEARCH_DELAY;
5327 }
5328 }
5329
5330 return 0;
5331}
5332
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08005333#ifdef ANDROID_P2P
5334int wpas_p2p_handle_frequency_conflicts(struct wpa_supplicant *wpa_s, int freq)
5335{
5336 struct wpa_supplicant *iface = NULL;
5337 struct p2p_data *p2p = wpa_s->global->p2p;
5338
5339 for (iface = wpa_s->global->ifaces; iface; iface = iface->next) {
5340 if((iface->p2p_group_interface) && (iface->current_ssid) &&
5341 (iface->current_ssid->frequency != freq)) {
5342
5343 if (iface->p2p_group_interface == P2P_GROUP_INTERFACE_GO) {
5344 /* Try to see whether we can move the GO. If it
5345 * is not possible, remove the GO interface
5346 */
5347 if(wpa_drv_switch_channel(iface, freq) == 0) {
5348 wpa_printf(MSG_ERROR, "P2P: GO Moved to freq(%d)", freq);
5349 iface->current_ssid->frequency = freq;
5350 continue;
5351 }
5352 }
5353
5354 /* If GO cannot be moved or if the conflicting interface is a
5355 * P2P Client, remove the interface depending up on the connection
5356 * priority */
Dmitry Shmidt687922c2012-03-26 14:02:32 -07005357 if(!wpas_is_p2p_prioritized(wpa_s)) {
Dmitry Shmidtf4f5db32012-09-11 14:36:56 -07005358 /* STA connection has priority over existing
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08005359 * P2P connection. So remove the interface */
Dmitry Shmidt687922c2012-03-26 14:02:32 -07005360 wpa_printf(MSG_DEBUG, "P2P: Removing P2P connection due to Single channel"
5361 "concurrent mode frequency conflict");
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005362 wpas_p2p_group_delete(iface, P2P_GROUP_REMOVAL_FREQ_CONFLICT);
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08005363 } else {
5364 /* Existing connection has the priority. Disable the newly
5365 * selected network and let the application know about it.
5366 */
5367 return -1;
5368 }
5369 }
5370 }
5371 return 0;
5372}
5373#endif