blob: 48cd42909444a283dd81ea22b0d61c6ffa082b1c [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 Shmidt391c59f2013-09-03 12:16:28 -070020#include "ap/sta_info.h"
21#include "ap/ap_drv_ops.h"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070022#include "ap/p2p_hostapd.h"
Jouni Malinen75ecf522011-06-27 15:19:46 -070023#include "eapol_supp/eapol_supp_sm.h"
24#include "rsn_supp/wpa.h"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070025#include "wpa_supplicant_i.h"
26#include "driver_i.h"
27#include "ap.h"
28#include "config_ssid.h"
29#include "config.h"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070030#include "notify.h"
31#include "scan.h"
32#include "bss.h"
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080033#include "offchannel.h"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070034#include "wps_supplicant.h"
35#include "p2p_supplicant.h"
36
37
38/*
39 * How many times to try to scan to find the GO before giving up on join
40 * request.
41 */
42#define P2P_MAX_JOIN_SCAN_ATTEMPTS 10
43
Dmitry Shmidt04949592012-07-19 12:16:46 -070044#define P2P_AUTO_PD_SCAN_ATTEMPTS 5
45
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080046#ifndef P2P_MAX_CLIENT_IDLE
47/*
48 * How many seconds to try to reconnect to the GO when connection in P2P client
49 * role has been lost.
50 */
Dmitry Shmidt98f9e762012-05-30 11:18:46 -070051#ifdef ANDROID_P2P
52#define P2P_MAX_CLIENT_IDLE 20
53#else
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080054#define P2P_MAX_CLIENT_IDLE 10
Dmitry Shmidt98f9e762012-05-30 11:18:46 -070055#endif /* ANDROID_P2P */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080056#endif /* P2P_MAX_CLIENT_IDLE */
57
Dmitry Shmidt04949592012-07-19 12:16:46 -070058#ifndef P2P_MAX_INITIAL_CONN_WAIT
59/*
60 * How many seconds to wait for initial 4-way handshake to get completed after
61 * WPS provisioning step.
62 */
63#define P2P_MAX_INITIAL_CONN_WAIT 10
64#endif /* P2P_MAX_INITIAL_CONN_WAIT */
65
Dmitry Shmidt92c368d2013-08-29 12:37:21 -070066#ifndef P2P_MAX_INITIAL_CONN_WAIT_GO
67/*
68 * How many seconds to wait for initial 4-way handshake to get completed after
69 * WPS provisioning step on the GO. This controls the extra time the P2P
70 * operation is considered to be in progress (e.g., to delay other scans) after
71 * WPS provisioning has been completed on the GO during group formation.
72 */
73#define P2P_MAX_INITIAL_CONN_WAIT_GO 10
74#endif /* P2P_MAX_INITIAL_CONN_WAIT_GO */
75
Dmitry Shmidt56052862013-10-04 10:23:25 -070076#ifndef P2P_MAX_INITIAL_CONN_WAIT_GO_REINVOKE
77/*
78 * How many seconds to wait for initial 4-way handshake to get completed after
79 * re-invocation of a persistent group on the GO when the client is expected
80 * to connect automatically (no user interaction).
81 */
82#define P2P_MAX_INITIAL_CONN_WAIT_GO_REINVOKE 15
83#endif /* P2P_MAX_INITIAL_CONN_WAIT_GO_REINVOKE */
84
Dmitry Shmidt61d9df32012-08-29 16:22:06 -070085#ifndef P2P_CONCURRENT_SEARCH_DELAY
Dmitry Shmidt61d9df32012-08-29 16:22:06 -070086#define P2P_CONCURRENT_SEARCH_DELAY 500
Dmitry Shmidt61d9df32012-08-29 16:22:06 -070087#endif /* P2P_CONCURRENT_SEARCH_DELAY */
88
Dmitry Shmidt34af3062013-07-11 10:46:32 -070089#define P2P_MGMT_DEVICE_PREFIX "p2p-dev-"
90
Dmitry Shmidt61d9df32012-08-29 16:22:06 -070091enum p2p_group_removal_reason {
92 P2P_GROUP_REMOVAL_UNKNOWN,
93 P2P_GROUP_REMOVAL_SILENT,
94 P2P_GROUP_REMOVAL_FORMATION_FAILED,
95 P2P_GROUP_REMOVAL_REQUESTED,
96 P2P_GROUP_REMOVAL_IDLE_TIMEOUT,
97 P2P_GROUP_REMOVAL_UNAVAILABLE,
98 P2P_GROUP_REMOVAL_GO_ENDING_SESSION,
Dmitry Shmidt391c59f2013-09-03 12:16:28 -070099 P2P_GROUP_REMOVAL_PSK_FAILURE,
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700100#ifdef ANDROID_P2P
101 P2P_GROUP_REMOVAL_FREQ_CONFLICT
102#endif
103};
104
Jouni Malinendc7b7132012-09-14 12:53:47 -0700105
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700106static void wpas_p2p_long_listen_timeout(void *eloop_ctx, void *timeout_ctx);
107static struct wpa_supplicant *
108wpas_p2p_get_group_iface(struct wpa_supplicant *wpa_s, int addr_allocated,
109 int go);
110static int wpas_p2p_join_start(struct wpa_supplicant *wpa_s);
Dmitry Shmidt04949592012-07-19 12:16:46 -0700111static void wpas_p2p_join_scan_req(struct wpa_supplicant *wpa_s, int freq);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700112static void wpas_p2p_join_scan(void *eloop_ctx, void *timeout_ctx);
113static int wpas_p2p_join(struct wpa_supplicant *wpa_s, const u8 *iface_addr,
Dmitry Shmidt04949592012-07-19 12:16:46 -0700114 const u8 *dev_addr, enum p2p_wps_method wps_method,
115 int auto_join);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700116static int wpas_p2p_create_iface(struct wpa_supplicant *wpa_s);
117static void wpas_p2p_cross_connect_setup(struct wpa_supplicant *wpa_s);
118static void wpas_p2p_group_idle_timeout(void *eloop_ctx, void *timeout_ctx);
119static void wpas_p2p_set_group_idle_timeout(struct wpa_supplicant *wpa_s);
Dmitry Shmidtf8623282013-02-20 14:34:59 -0800120static void wpas_p2p_group_formation_timeout(void *eloop_ctx,
121 void *timeout_ctx);
Dmitry Shmidt1cf45732013-04-29 17:36:45 -0700122#ifdef ANDROID_P2P
123static void wpas_p2p_group_freq_conflict(void *eloop_ctx, void *timeout_ctx);
124#endif
Dmitry Shmidt04949592012-07-19 12:16:46 -0700125static void wpas_p2p_fallback_to_go_neg(struct wpa_supplicant *wpa_s,
126 int group_added);
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800127static int wpas_p2p_stop_find_oper(struct wpa_supplicant *wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700128
129
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -0700130/*
131 * Get the number of concurrent channels that the HW can operate, but that are
132 * currently not in use by any of the wpa_supplicant interfaces.
133 */
134static int wpas_p2p_num_unused_channels(struct wpa_supplicant *wpa_s)
135{
136 int *freqs;
137 int num;
138
139 freqs = os_calloc(wpa_s->num_multichan_concurrent, sizeof(int));
140 if (!freqs)
141 return -1;
142
143 num = get_shared_radio_freqs(wpa_s, freqs,
144 wpa_s->num_multichan_concurrent);
145 os_free(freqs);
146
147 return wpa_s->num_multichan_concurrent - num;
148}
149
150
151/*
152 * Get the frequencies that are currently in use by one or more of the virtual
153 * interfaces, and that are also valid for P2P operation.
154 */
155static int wpas_p2p_valid_oper_freqs(struct wpa_supplicant *wpa_s,
156 int *p2p_freqs, unsigned int len)
157{
158 int *freqs;
159 unsigned int num, i, j;
160
161 freqs = os_calloc(wpa_s->num_multichan_concurrent, sizeof(int));
162 if (!freqs)
163 return -1;
164
165 num = get_shared_radio_freqs(wpa_s, freqs,
166 wpa_s->num_multichan_concurrent);
167
168 os_memset(p2p_freqs, 0, sizeof(int) * len);
169
170 for (i = 0, j = 0; i < num && j < len; i++) {
171 if (p2p_supported_freq(wpa_s->global->p2p, freqs[i]))
172 p2p_freqs[j++] = freqs[i];
173 }
174
175 os_free(freqs);
176
177 return j;
178}
179
180
Dmitry Shmidt700a1372013-03-15 14:14:44 -0700181static void wpas_p2p_set_own_freq_preference(struct wpa_supplicant *wpa_s,
182 int freq)
183{
184 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
185 return;
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -0700186 if (freq > 0 && wpa_s->num_multichan_concurrent > 1 &&
187 wpas_p2p_num_unused_channels(wpa_s) > 0 &&
Dmitry Shmidt700a1372013-03-15 14:14:44 -0700188 wpa_s->parent->conf->p2p_ignore_shared_freq)
189 freq = 0;
190 p2p_set_own_freq_preference(wpa_s->global->p2p, freq);
191}
192
193
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700194static void wpas_p2p_scan_res_handler(struct wpa_supplicant *wpa_s,
195 struct wpa_scan_results *scan_res)
196{
197 size_t i;
198
199 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
200 return;
201
202 wpa_printf(MSG_DEBUG, "P2P: Scan results received (%d BSS)",
203 (int) scan_res->num);
204
205 for (i = 0; i < scan_res->num; i++) {
206 struct wpa_scan_res *bss = scan_res->res[i];
Dmitry Shmidtf8623282013-02-20 14:34:59 -0800207 struct os_time time_tmp_age, entry_ts;
Dmitry Shmidt96571392013-10-14 12:54:46 -0700208 const u8 *ies;
209 size_t ies_len;
210
Dmitry Shmidtf8623282013-02-20 14:34:59 -0800211 time_tmp_age.sec = bss->age / 1000;
212 time_tmp_age.usec = (bss->age % 1000) * 1000;
213 os_time_sub(&scan_res->fetch_time, &time_tmp_age, &entry_ts);
Dmitry Shmidt96571392013-10-14 12:54:46 -0700214
215 ies = (const u8 *) (bss + 1);
216 ies_len = bss->ie_len;
217 if (bss->beacon_ie_len > 0 &&
218 !wpa_scan_get_vendor_ie(bss, P2P_IE_VENDOR_TYPE) &&
219 wpa_scan_get_vendor_ie_beacon(bss, P2P_IE_VENDOR_TYPE)) {
220 wpa_printf(MSG_DEBUG, "P2P: Use P2P IE(s) from Beacon frame since no P2P IE(s) in Probe Response frames received for "
221 MACSTR, MAC2STR(bss->bssid));
222 ies = ies + ies_len;
223 ies_len = bss->beacon_ie_len;
224 }
225
226
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700227 if (p2p_scan_res_handler(wpa_s->global->p2p, bss->bssid,
Dmitry Shmidtf8623282013-02-20 14:34:59 -0800228 bss->freq, &entry_ts, bss->level,
Dmitry Shmidt96571392013-10-14 12:54:46 -0700229 ies, ies_len) > 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700230 break;
231 }
232
233 p2p_scan_res_handled(wpa_s->global->p2p);
234}
235
236
237static int wpas_p2p_scan(void *ctx, enum p2p_scan_type type, int freq,
238 unsigned int num_req_dev_types,
Dmitry Shmidt04949592012-07-19 12:16:46 -0700239 const u8 *req_dev_types, const u8 *dev_id, u16 pw_id)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700240{
241 struct wpa_supplicant *wpa_s = ctx;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700242 struct wpa_supplicant *ifs;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700243 struct wpa_driver_scan_params params;
244 int ret;
245 struct wpabuf *wps_ie, *ies;
246 int social_channels[] = { 2412, 2437, 2462, 0, 0 };
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800247 size_t ielen;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700248
249 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
250 return -1;
251
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700252 for (ifs = wpa_s->global->ifaces; ifs; ifs = ifs->next) {
253 if (ifs->sta_scan_pending &&
Dmitry Shmidt37d4d6a2013-03-18 13:09:42 -0700254 (wpas_scan_scheduled(ifs) || ifs->scanning) &&
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700255 wpas_p2p_in_progress(wpa_s) == 2) {
256 wpa_printf(MSG_DEBUG, "Delaying P2P scan to allow "
257 "pending station mode scan to be "
258 "completed on interface %s", ifs->ifname);
Jouni Malinendc7b7132012-09-14 12:53:47 -0700259 wpa_s->global->p2p_cb_on_scan_complete = 1;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700260 wpa_supplicant_req_scan(ifs, 0, 0);
261 return 1;
262 }
263 }
264
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700265 os_memset(&params, 0, sizeof(params));
266
267 /* P2P Wildcard SSID */
268 params.num_ssids = 1;
269 params.ssids[0].ssid = (u8 *) P2P_WILDCARD_SSID;
270 params.ssids[0].ssid_len = P2P_WILDCARD_SSID_LEN;
271
272 wpa_s->wps->dev.p2p = 1;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700273 wps_ie = wps_build_probe_req_ie(pw_id, &wpa_s->wps->dev,
274 wpa_s->wps->uuid, WPS_REQ_ENROLLEE,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700275 num_req_dev_types, req_dev_types);
276 if (wps_ie == NULL)
277 return -1;
278
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800279 ielen = p2p_scan_ie_buf_len(wpa_s->global->p2p);
280 ies = wpabuf_alloc(wpabuf_len(wps_ie) + ielen);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700281 if (ies == NULL) {
282 wpabuf_free(wps_ie);
283 return -1;
284 }
285 wpabuf_put_buf(ies, wps_ie);
286 wpabuf_free(wps_ie);
287
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -0800288 p2p_scan_ie(wpa_s->global->p2p, ies, dev_id);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700289
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800290 params.p2p_probe = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700291 params.extra_ies = wpabuf_head(ies);
292 params.extra_ies_len = wpabuf_len(ies);
293
294 switch (type) {
295 case P2P_SCAN_SOCIAL:
296 params.freqs = social_channels;
297 break;
298 case P2P_SCAN_FULL:
299 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700300 case P2P_SCAN_SOCIAL_PLUS_ONE:
301 social_channels[3] = freq;
302 params.freqs = social_channels;
303 break;
304 }
305
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800306 ret = wpa_drv_scan(wpa_s, &params);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700307
308 wpabuf_free(ies);
309
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800310 if (ret) {
Jouni Malinen043a5a92012-09-13 18:03:14 -0700311 for (ifs = wpa_s->global->ifaces; ifs; ifs = ifs->next) {
312 if (ifs->scanning ||
313 ifs->scan_res_handler == wpas_p2p_scan_res_handler) {
314 wpa_s->global->p2p_cb_on_scan_complete = 1;
315 ret = 1;
316 break;
317 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800318 }
Dmitry Shmidt444d5672013-04-01 13:08:44 -0700319 } else {
320 os_get_time(&wpa_s->scan_trigger_time);
Dmitry Shmidt04949592012-07-19 12:16:46 -0700321 wpa_s->scan_res_handler = wpas_p2p_scan_res_handler;
Dmitry Shmidt444d5672013-04-01 13:08:44 -0700322 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800323
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700324 return ret;
325}
326
327
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700328static enum wpa_driver_if_type wpas_p2p_if_type(int p2p_group_interface)
329{
330 switch (p2p_group_interface) {
331 case P2P_GROUP_INTERFACE_PENDING:
332 return WPA_IF_P2P_GROUP;
333 case P2P_GROUP_INTERFACE_GO:
334 return WPA_IF_P2P_GO;
335 case P2P_GROUP_INTERFACE_CLIENT:
336 return WPA_IF_P2P_CLIENT;
337 }
338
339 return WPA_IF_P2P_GROUP;
340}
341
342
343static struct wpa_supplicant * wpas_get_p2p_group(struct wpa_supplicant *wpa_s,
344 const u8 *ssid,
345 size_t ssid_len, int *go)
346{
347 struct wpa_ssid *s;
348
349 for (wpa_s = wpa_s->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
350 for (s = wpa_s->conf->ssid; s; s = s->next) {
351 if (s->disabled != 0 || !s->p2p_group ||
352 s->ssid_len != ssid_len ||
353 os_memcmp(ssid, s->ssid, ssid_len) != 0)
354 continue;
355 if (s->mode == WPAS_MODE_P2P_GO &&
356 s != wpa_s->current_ssid)
357 continue;
358 if (go)
359 *go = s->mode == WPAS_MODE_P2P_GO;
360 return wpa_s;
361 }
362 }
363
364 return NULL;
365}
366
367
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700368static int wpas_p2p_group_delete(struct wpa_supplicant *wpa_s,
369 enum p2p_group_removal_reason removal_reason)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700370{
371 struct wpa_ssid *ssid;
372 char *gtype;
373 const char *reason;
374
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700375 ssid = wpa_s->current_ssid;
376 if (ssid == NULL) {
377 /*
378 * The current SSID was not known, but there may still be a
Dmitry Shmidtaa532512012-09-24 10:35:31 -0700379 * pending P2P group interface waiting for provisioning or a
380 * P2P group that is trying to reconnect.
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700381 */
382 ssid = wpa_s->conf->ssid;
383 while (ssid) {
Jouni Malinen9d712832012-10-05 11:01:57 -0700384 if (ssid->p2p_group && ssid->disabled != 2)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700385 break;
386 ssid = ssid->next;
387 }
Jouni Malinen5c44edb2012-08-31 21:35:32 +0300388 if (ssid == NULL &&
389 wpa_s->p2p_group_interface == NOT_P2P_GROUP_INTERFACE)
390 {
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700391 wpa_printf(MSG_ERROR, "P2P: P2P group interface "
392 "not found");
393 return -1;
394 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700395 }
396 if (wpa_s->p2p_group_interface == P2P_GROUP_INTERFACE_GO)
397 gtype = "GO";
398 else if (wpa_s->p2p_group_interface == P2P_GROUP_INTERFACE_CLIENT ||
399 (ssid && ssid->mode == WPAS_MODE_INFRA)) {
400 wpa_s->reassociate = 0;
401 wpa_s->disconnected = 1;
402 wpa_supplicant_deauthenticate(wpa_s,
403 WLAN_REASON_DEAUTH_LEAVING);
404 gtype = "client";
405 } else
406 gtype = "GO";
407 if (wpa_s->cross_connect_in_use) {
408 wpa_s->cross_connect_in_use = 0;
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -0700409 wpa_msg_global(wpa_s->parent, MSG_INFO,
410 P2P_EVENT_CROSS_CONNECT_DISABLE "%s %s",
411 wpa_s->ifname, wpa_s->cross_connect_uplink);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700412 }
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700413 switch (removal_reason) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700414 case P2P_GROUP_REMOVAL_REQUESTED:
415 reason = " reason=REQUESTED";
416 break;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700417 case P2P_GROUP_REMOVAL_FORMATION_FAILED:
418 reason = " reason=FORMATION_FAILED";
419 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700420 case P2P_GROUP_REMOVAL_IDLE_TIMEOUT:
421 reason = " reason=IDLE";
422 break;
423 case P2P_GROUP_REMOVAL_UNAVAILABLE:
424 reason = " reason=UNAVAILABLE";
425 break;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700426 case P2P_GROUP_REMOVAL_GO_ENDING_SESSION:
427 reason = " reason=GO_ENDING_SESSION";
428 break;
Dmitry Shmidt391c59f2013-09-03 12:16:28 -0700429 case P2P_GROUP_REMOVAL_PSK_FAILURE:
430 reason = " reason=PSK_FAILURE";
431 break;
Dmitry Shmidt687922c2012-03-26 14:02:32 -0700432#ifdef ANDROID_P2P
433 case P2P_GROUP_REMOVAL_FREQ_CONFLICT:
434 reason = " reason=FREQ_CONFLICT";
435 break;
436#endif
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700437 default:
438 reason = "";
439 break;
440 }
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700441 if (removal_reason != P2P_GROUP_REMOVAL_SILENT) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -0700442 wpa_msg_global(wpa_s->parent, MSG_INFO,
443 P2P_EVENT_GROUP_REMOVED "%s %s%s",
444 wpa_s->ifname, gtype, reason);
Dmitry Shmidt04949592012-07-19 12:16:46 -0700445 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700446
Dmitry Shmidt1cf45732013-04-29 17:36:45 -0700447#ifdef ANDROID_P2P
448 eloop_cancel_timeout(wpas_p2p_group_freq_conflict, wpa_s, NULL);
449#endif
Dmitry Shmidt04949592012-07-19 12:16:46 -0700450 if (eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL) > 0)
451 wpa_printf(MSG_DEBUG, "P2P: Cancelled P2P group idle timeout");
Dmitry Shmidtf8623282013-02-20 14:34:59 -0800452 if (eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
Dmitry Shmidt2f023192013-03-12 12:44:17 -0700453 wpa_s->parent, NULL) > 0) {
Dmitry Shmidtf8623282013-02-20 14:34:59 -0800454 wpa_printf(MSG_DEBUG, "P2P: Cancelled P2P group formation "
455 "timeout");
Dmitry Shmidt2f023192013-03-12 12:44:17 -0700456 wpa_s->p2p_in_provisioning = 0;
457 }
Dmitry Shmidt04949592012-07-19 12:16:46 -0700458
Dmitry Shmidt96571392013-10-14 12:54:46 -0700459 /*
460 * Make sure wait for the first client does not remain active after the
461 * group has been removed.
462 */
463 wpa_s->global->p2p_go_wait_client.sec = 0;
464
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700465 if (removal_reason != P2P_GROUP_REMOVAL_SILENT && ssid)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700466 wpas_notify_p2p_group_removed(wpa_s, ssid, gtype);
467
468 if (wpa_s->p2p_group_interface != NOT_P2P_GROUP_INTERFACE) {
469 struct wpa_global *global;
470 char *ifname;
471 enum wpa_driver_if_type type;
472 wpa_printf(MSG_DEBUG, "P2P: Remove group interface %s",
473 wpa_s->ifname);
474 global = wpa_s->global;
475 ifname = os_strdup(wpa_s->ifname);
476 type = wpas_p2p_if_type(wpa_s->p2p_group_interface);
Dmitry Shmidte15c7b52011-08-03 15:04:35 -0700477 wpa_supplicant_remove_iface(wpa_s->global, wpa_s, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700478 wpa_s = global->ifaces;
479 if (wpa_s && ifname)
480 wpa_drv_if_remove(wpa_s, type, ifname);
481 os_free(ifname);
Jouni Malinen2b89da82012-08-31 22:04:41 +0300482 return 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700483 }
484
Dmitry Shmidt96571392013-10-14 12:54:46 -0700485 if (!wpa_s->p2p_go_group_formation_completed) {
486 wpa_s->global->p2p_group_formation = NULL;
487 wpa_s->p2p_in_provisioning = 0;
488 }
489
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700490 wpa_printf(MSG_DEBUG, "P2P: Remove temporary group network");
491 if (ssid && (ssid->p2p_group ||
492 ssid->mode == WPAS_MODE_P2P_GROUP_FORMATION ||
493 (ssid->key_mgmt & WPA_KEY_MGMT_WPS))) {
494 int id = ssid->id;
Jouni Malinen75ecf522011-06-27 15:19:46 -0700495 if (ssid == wpa_s->current_ssid) {
496 wpa_sm_set_config(wpa_s->wpa, NULL);
497 eapol_sm_notify_config(wpa_s->eapol, NULL, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700498 wpa_s->current_ssid = NULL;
Jouni Malinen75ecf522011-06-27 15:19:46 -0700499 }
500 /*
501 * Networks objects created during any P2P activities are not
502 * exposed out as they might/will confuse certain non-P2P aware
503 * applications since these network objects won't behave like
504 * regular ones.
505 *
506 * Likewise, we don't send out network removed signals for such
507 * network objects.
508 */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700509 wpa_config_remove_network(wpa_s->conf, id);
510 wpa_supplicant_clear_status(wpa_s);
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -0800511 wpa_supplicant_cancel_sched_scan(wpa_s);
Dmitry Shmidt91c40cd2012-09-25 14:23:53 -0700512 wpa_s->sta_scan_pending = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700513 } else {
514 wpa_printf(MSG_DEBUG, "P2P: Temporary group network not "
515 "found");
516 }
Dmitry Shmidt04949592012-07-19 12:16:46 -0700517 if (wpa_s->ap_iface)
518 wpa_supplicant_ap_deinit(wpa_s);
519 else
520 wpa_drv_deinit_p2p_cli(wpa_s);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700521
522 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700523}
524
525
526static int wpas_p2p_persistent_group(struct wpa_supplicant *wpa_s,
527 u8 *go_dev_addr,
528 const u8 *ssid, size_t ssid_len)
529{
530 struct wpa_bss *bss;
531 const u8 *bssid;
532 struct wpabuf *p2p;
533 u8 group_capab;
534 const u8 *addr;
535
536 if (wpa_s->go_params)
537 bssid = wpa_s->go_params->peer_interface_addr;
538 else
539 bssid = wpa_s->bssid;
540
541 bss = wpa_bss_get(wpa_s, bssid, ssid, ssid_len);
542 if (bss == NULL) {
543 u8 iface_addr[ETH_ALEN];
544 if (p2p_get_interface_addr(wpa_s->global->p2p, bssid,
545 iface_addr) == 0)
546 bss = wpa_bss_get(wpa_s, iface_addr, ssid, ssid_len);
547 }
548 if (bss == NULL) {
549 wpa_printf(MSG_DEBUG, "P2P: Could not figure out whether "
550 "group is persistent - BSS " MACSTR " not found",
551 MAC2STR(bssid));
552 return 0;
553 }
554
555 p2p = wpa_bss_get_vendor_ie_multi(bss, P2P_IE_VENDOR_TYPE);
Dmitry Shmidt96571392013-10-14 12:54:46 -0700556 if (p2p == NULL)
557 p2p = wpa_bss_get_vendor_ie_multi_beacon(bss,
558 P2P_IE_VENDOR_TYPE);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700559 if (p2p == NULL) {
560 wpa_printf(MSG_DEBUG, "P2P: Could not figure out whether "
561 "group is persistent - BSS " MACSTR
562 " did not include P2P IE", MAC2STR(bssid));
563 wpa_hexdump(MSG_DEBUG, "P2P: Probe Response IEs",
564 (u8 *) (bss + 1), bss->ie_len);
565 wpa_hexdump(MSG_DEBUG, "P2P: Beacon IEs",
566 ((u8 *) bss + 1) + bss->ie_len,
567 bss->beacon_ie_len);
568 return 0;
569 }
570
571 group_capab = p2p_get_group_capab(p2p);
572 addr = p2p_get_go_dev_addr(p2p);
573 wpa_printf(MSG_DEBUG, "P2P: Checking whether group is persistent: "
574 "group_capab=0x%x", group_capab);
575 if (addr) {
576 os_memcpy(go_dev_addr, addr, ETH_ALEN);
577 wpa_printf(MSG_DEBUG, "P2P: GO Device Address " MACSTR,
578 MAC2STR(addr));
579 } else
580 os_memset(go_dev_addr, 0, ETH_ALEN);
581 wpabuf_free(p2p);
582
583 wpa_printf(MSG_DEBUG, "P2P: BSS " MACSTR " group_capab=0x%x "
584 "go_dev_addr=" MACSTR,
585 MAC2STR(bssid), group_capab, MAC2STR(go_dev_addr));
586
587 return group_capab & P2P_GROUP_CAPAB_PERSISTENT_GROUP;
588}
589
590
Jouni Malinen75ecf522011-06-27 15:19:46 -0700591static int wpas_p2p_store_persistent_group(struct wpa_supplicant *wpa_s,
592 struct wpa_ssid *ssid,
593 const u8 *go_dev_addr)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700594{
595 struct wpa_ssid *s;
596 int changed = 0;
597
598 wpa_printf(MSG_DEBUG, "P2P: Storing credentials for a persistent "
599 "group (GO Dev Addr " MACSTR ")", MAC2STR(go_dev_addr));
600 for (s = wpa_s->conf->ssid; s; s = s->next) {
601 if (s->disabled == 2 &&
602 os_memcmp(go_dev_addr, s->bssid, ETH_ALEN) == 0 &&
603 s->ssid_len == ssid->ssid_len &&
604 os_memcmp(ssid->ssid, s->ssid, ssid->ssid_len) == 0)
605 break;
606 }
607
608 if (s) {
609 wpa_printf(MSG_DEBUG, "P2P: Update existing persistent group "
610 "entry");
611 if (ssid->passphrase && !s->passphrase)
612 changed = 1;
613 else if (ssid->passphrase && s->passphrase &&
614 os_strcmp(ssid->passphrase, s->passphrase) != 0)
615 changed = 1;
616 } else {
617 wpa_printf(MSG_DEBUG, "P2P: Create a new persistent group "
618 "entry");
619 changed = 1;
620 s = wpa_config_add_network(wpa_s->conf);
621 if (s == NULL)
Jouni Malinen75ecf522011-06-27 15:19:46 -0700622 return -1;
623
624 /*
625 * Instead of network_added we emit persistent_group_added
626 * notification. Also to keep the defense checks in
627 * persistent_group obj registration method, we set the
628 * relevant flags in s to designate it as a persistent group.
629 */
630 s->p2p_group = 1;
631 s->p2p_persistent_group = 1;
632 wpas_notify_persistent_group_added(wpa_s, s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700633 wpa_config_set_network_defaults(s);
634 }
635
636 s->p2p_group = 1;
637 s->p2p_persistent_group = 1;
638 s->disabled = 2;
639 s->bssid_set = 1;
640 os_memcpy(s->bssid, go_dev_addr, ETH_ALEN);
641 s->mode = ssid->mode;
642 s->auth_alg = WPA_AUTH_ALG_OPEN;
643 s->key_mgmt = WPA_KEY_MGMT_PSK;
644 s->proto = WPA_PROTO_RSN;
645 s->pairwise_cipher = WPA_CIPHER_CCMP;
646 s->export_keys = 1;
647 if (ssid->passphrase) {
648 os_free(s->passphrase);
649 s->passphrase = os_strdup(ssid->passphrase);
650 }
651 if (ssid->psk_set) {
652 s->psk_set = 1;
653 os_memcpy(s->psk, ssid->psk, 32);
654 }
655 if (s->passphrase && !s->psk_set)
656 wpa_config_update_psk(s);
657 if (s->ssid == NULL || s->ssid_len < ssid->ssid_len) {
658 os_free(s->ssid);
659 s->ssid = os_malloc(ssid->ssid_len);
660 }
661 if (s->ssid) {
662 s->ssid_len = ssid->ssid_len;
663 os_memcpy(s->ssid, ssid->ssid, s->ssid_len);
664 }
Dmitry Shmidt391c59f2013-09-03 12:16:28 -0700665 if (ssid->mode == WPAS_MODE_P2P_GO && wpa_s->global->add_psk) {
666 dl_list_add(&s->psk_list, &wpa_s->global->add_psk->list);
667 wpa_s->global->add_psk = NULL;
668 changed = 1;
669 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700670
671#ifndef CONFIG_NO_CONFIG_WRITE
672 if (changed && wpa_s->conf->update_config &&
673 wpa_config_write(wpa_s->confname, wpa_s->conf)) {
674 wpa_printf(MSG_DEBUG, "P2P: Failed to update configuration");
675 }
676#endif /* CONFIG_NO_CONFIG_WRITE */
Jouni Malinen75ecf522011-06-27 15:19:46 -0700677
678 return s->id;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700679}
680
681
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800682static void wpas_p2p_add_persistent_group_client(struct wpa_supplicant *wpa_s,
683 const u8 *addr)
684{
685 struct wpa_ssid *ssid, *s;
686 u8 *n;
687 size_t i;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700688 int found = 0;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800689
690 ssid = wpa_s->current_ssid;
691 if (ssid == NULL || ssid->mode != WPAS_MODE_P2P_GO ||
692 !ssid->p2p_persistent_group)
693 return;
694
695 for (s = wpa_s->parent->conf->ssid; s; s = s->next) {
696 if (s->disabled != 2 || s->mode != WPAS_MODE_P2P_GO)
697 continue;
698
699 if (s->ssid_len == ssid->ssid_len &&
700 os_memcmp(s->ssid, ssid->ssid, s->ssid_len) == 0)
701 break;
702 }
703
704 if (s == NULL)
705 return;
706
707 for (i = 0; s->p2p_client_list && i < s->num_p2p_clients; i++) {
708 if (os_memcmp(s->p2p_client_list + i * ETH_ALEN, addr,
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700709 ETH_ALEN) != 0)
710 continue;
711
712 if (i == s->num_p2p_clients - 1)
713 return; /* already the most recent entry */
714
715 /* move the entry to mark it most recent */
716 os_memmove(s->p2p_client_list + i * ETH_ALEN,
717 s->p2p_client_list + (i + 1) * ETH_ALEN,
718 (s->num_p2p_clients - i - 1) * ETH_ALEN);
719 os_memcpy(s->p2p_client_list +
720 (s->num_p2p_clients - 1) * ETH_ALEN, addr, ETH_ALEN);
721 found = 1;
722 break;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800723 }
724
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700725 if (!found && s->num_p2p_clients < P2P_MAX_STORED_CLIENTS) {
726 n = os_realloc_array(s->p2p_client_list,
727 s->num_p2p_clients + 1, ETH_ALEN);
728 if (n == NULL)
729 return;
730 os_memcpy(n + s->num_p2p_clients * ETH_ALEN, addr, ETH_ALEN);
731 s->p2p_client_list = n;
732 s->num_p2p_clients++;
733 } else if (!found) {
734 /* Not enough room for an additional entry - drop the oldest
735 * entry */
736 os_memmove(s->p2p_client_list,
737 s->p2p_client_list + ETH_ALEN,
738 (s->num_p2p_clients - 1) * ETH_ALEN);
739 os_memcpy(s->p2p_client_list +
740 (s->num_p2p_clients - 1) * ETH_ALEN,
741 addr, ETH_ALEN);
742 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800743
744#ifndef CONFIG_NO_CONFIG_WRITE
745 if (wpa_s->parent->conf->update_config &&
746 wpa_config_write(wpa_s->parent->confname, wpa_s->parent->conf))
747 wpa_printf(MSG_DEBUG, "P2P: Failed to update configuration");
748#endif /* CONFIG_NO_CONFIG_WRITE */
749}
750
751
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700752static void wpas_group_formation_completed(struct wpa_supplicant *wpa_s,
753 int success)
754{
755 struct wpa_ssid *ssid;
756 const char *ssid_txt;
757 int client;
758 int persistent;
759 u8 go_dev_addr[ETH_ALEN];
Jouni Malinen75ecf522011-06-27 15:19:46 -0700760 int network_id = -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700761
762 /*
763 * This callback is likely called for the main interface. Update wpa_s
764 * to use the group interface if a new interface was created for the
765 * group.
766 */
767 if (wpa_s->global->p2p_group_formation)
768 wpa_s = wpa_s->global->p2p_group_formation;
Dmitry Shmidt56052862013-10-04 10:23:25 -0700769 if (wpa_s->p2p_go_group_formation_completed) {
770 wpa_s->global->p2p_group_formation = NULL;
771 wpa_s->p2p_in_provisioning = 0;
772 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700773
774 if (!success) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -0700775 wpa_msg_global(wpa_s->parent, MSG_INFO,
776 P2P_EVENT_GROUP_FORMATION_FAILURE);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700777 wpas_p2p_group_delete(wpa_s,
778 P2P_GROUP_REMOVAL_FORMATION_FAILED);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700779 return;
780 }
781
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -0700782 wpa_msg_global(wpa_s->parent, MSG_INFO,
783 P2P_EVENT_GROUP_FORMATION_SUCCESS);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700784
785 ssid = wpa_s->current_ssid;
786 if (ssid && ssid->mode == WPAS_MODE_P2P_GROUP_FORMATION) {
787 ssid->mode = WPAS_MODE_P2P_GO;
788 p2p_group_notif_formation_done(wpa_s->p2p_group);
789 wpa_supplicant_ap_mac_addr_filter(wpa_s, NULL);
790 }
791
792 persistent = 0;
793 if (ssid) {
794 ssid_txt = wpa_ssid_txt(ssid->ssid, ssid->ssid_len);
795 client = ssid->mode == WPAS_MODE_INFRA;
796 if (ssid->mode == WPAS_MODE_P2P_GO) {
797 persistent = ssid->p2p_persistent_group;
Dmitry Shmidt497c1d52011-07-21 15:19:46 -0700798 os_memcpy(go_dev_addr, wpa_s->global->p2p_dev_addr,
799 ETH_ALEN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700800 } else
801 persistent = wpas_p2p_persistent_group(wpa_s,
802 go_dev_addr,
803 ssid->ssid,
804 ssid->ssid_len);
805 } else {
806 ssid_txt = "";
807 client = wpa_s->p2p_group_interface ==
808 P2P_GROUP_INTERFACE_CLIENT;
809 os_memset(go_dev_addr, 0, ETH_ALEN);
810 }
811
812 wpa_s->show_group_started = 0;
813 if (client) {
814 /*
815 * Indicate event only after successfully completed 4-way
816 * handshake, i.e., when the interface is ready for data
817 * packets.
818 */
819 wpa_s->show_group_started = 1;
Dmitry Shmidt4b86ea52012-09-04 11:06:50 -0700820#ifdef ANDROID_P2P
821 /* For client Second phase of Group formation (4-way handshake) can be still pending
822 * So we need to restore wpa_s->global->p2p_group_formation */
Dmitry Shmidta2854ab2012-09-10 16:15:47 -0700823 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 -0700824 wpa_s->global->p2p_group_formation = wpa_s;
825#endif
826
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700827 } else if (ssid && ssid->passphrase == NULL && ssid->psk_set) {
828 char psk[65];
829 wpa_snprintf_hex(psk, sizeof(psk), ssid->psk, 32);
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -0700830 wpa_msg_global(wpa_s->parent, MSG_INFO, P2P_EVENT_GROUP_STARTED
831 "%s GO ssid=\"%s\" freq=%d psk=%s go_dev_addr="
832 MACSTR "%s",
833 wpa_s->ifname, ssid_txt, ssid->frequency, psk,
834 MAC2STR(go_dev_addr),
835 persistent ? " [PERSISTENT]" : "");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700836 wpas_p2p_cross_connect_setup(wpa_s);
837 wpas_p2p_set_group_idle_timeout(wpa_s);
838 } else {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -0700839 wpa_msg_global(wpa_s->parent, MSG_INFO, P2P_EVENT_GROUP_STARTED
840 "%s GO ssid=\"%s\" freq=%d passphrase=\"%s\" "
841 "go_dev_addr=" MACSTR "%s",
842 wpa_s->ifname, ssid_txt,
843 ssid ? ssid->frequency : 0,
844 ssid && ssid->passphrase ? ssid->passphrase : "",
845 MAC2STR(go_dev_addr),
846 persistent ? " [PERSISTENT]" : "");
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 }
850
851 if (persistent)
Jouni Malinen75ecf522011-06-27 15:19:46 -0700852 network_id = wpas_p2p_store_persistent_group(wpa_s->parent,
853 ssid, go_dev_addr);
Dmitry Shmidt391c59f2013-09-03 12:16:28 -0700854 else {
855 os_free(wpa_s->global->add_psk);
856 wpa_s->global->add_psk = NULL;
857 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800858 if (network_id < 0 && ssid)
Jouni Malinen75ecf522011-06-27 15:19:46 -0700859 network_id = ssid->id;
Dmitry Shmidt92c368d2013-08-29 12:37:21 -0700860 if (!client) {
Jouni Malinen75ecf522011-06-27 15:19:46 -0700861 wpas_notify_p2p_group_started(wpa_s, ssid, network_id, 0);
Dmitry Shmidt92c368d2013-08-29 12:37:21 -0700862 os_get_time(&wpa_s->global->p2p_go_wait_client);
863 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700864}
865
866
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800867static void wpas_p2p_send_action_tx_status(struct wpa_supplicant *wpa_s,
868 unsigned int freq,
869 const u8 *dst, const u8 *src,
870 const u8 *bssid,
871 const u8 *data, size_t data_len,
872 enum offchannel_send_action_result
873 result)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700874{
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800875 enum p2p_send_action_result res = P2P_SEND_ACTION_SUCCESS;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700876
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700877 if (wpa_s->global->p2p == NULL || wpa_s->global->p2p_disabled)
878 return;
879 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
880 return;
881
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800882 switch (result) {
883 case OFFCHANNEL_SEND_ACTION_SUCCESS:
884 res = P2P_SEND_ACTION_SUCCESS;
885 break;
886 case OFFCHANNEL_SEND_ACTION_NO_ACK:
887 res = P2P_SEND_ACTION_NO_ACK;
888 break;
889 case OFFCHANNEL_SEND_ACTION_FAILED:
890 res = P2P_SEND_ACTION_FAILED;
891 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700892 }
893
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800894 p2p_send_action_cb(wpa_s->global->p2p, freq, dst, src, bssid, res);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700895
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -0800896 if (result != OFFCHANNEL_SEND_ACTION_SUCCESS &&
897 wpa_s->pending_pd_before_join &&
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800898 (os_memcmp(dst, wpa_s->pending_join_dev_addr, ETH_ALEN) == 0 ||
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800899 os_memcmp(dst, wpa_s->pending_join_iface_addr, ETH_ALEN) == 0) &&
900 wpa_s->p2p_fallback_to_go_neg) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700901 wpa_s->pending_pd_before_join = 0;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800902 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: No ACK for PD Req "
903 "during p2p_connect-auto");
904 wpas_p2p_fallback_to_go_neg(wpa_s, 0);
905 return;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700906 }
907}
908
909
910static int wpas_send_action(void *ctx, unsigned int freq, const u8 *dst,
911 const u8 *src, const u8 *bssid, const u8 *buf,
912 size_t len, unsigned int wait_time)
913{
914 struct wpa_supplicant *wpa_s = ctx;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800915 return offchannel_send_action(wpa_s, freq, dst, src, bssid, buf, len,
916 wait_time,
917 wpas_p2p_send_action_tx_status, 1);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700918}
919
920
921static void wpas_send_action_done(void *ctx)
922{
923 struct wpa_supplicant *wpa_s = ctx;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800924 offchannel_send_action_done(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700925}
926
927
928static int wpas_copy_go_neg_results(struct wpa_supplicant *wpa_s,
929 struct p2p_go_neg_results *params)
930{
931 if (wpa_s->go_params == NULL) {
932 wpa_s->go_params = os_malloc(sizeof(*params));
933 if (wpa_s->go_params == NULL)
934 return -1;
935 }
936 os_memcpy(wpa_s->go_params, params, sizeof(*params));
937 return 0;
938}
939
940
941static void wpas_start_wps_enrollee(struct wpa_supplicant *wpa_s,
942 struct p2p_go_neg_results *res)
943{
944 wpa_printf(MSG_DEBUG, "P2P: Start WPS Enrollee for peer " MACSTR,
945 MAC2STR(res->peer_interface_addr));
946 wpa_hexdump_ascii(MSG_DEBUG, "P2P: Start WPS Enrollee for SSID",
947 res->ssid, res->ssid_len);
948 wpa_supplicant_ap_deinit(wpa_s);
949 wpas_copy_go_neg_results(wpa_s, res);
950 if (res->wps_method == WPS_PBC)
951 wpas_wps_start_pbc(wpa_s, res->peer_interface_addr, 1);
952 else {
953 u16 dev_pw_id = DEV_PW_DEFAULT;
954 if (wpa_s->p2p_wps_method == WPS_PIN_KEYPAD)
955 dev_pw_id = DEV_PW_REGISTRAR_SPECIFIED;
956 wpas_wps_start_pin(wpa_s, res->peer_interface_addr,
957 wpa_s->p2p_pin, 1, dev_pw_id);
958 }
959}
960
961
Dmitry Shmidt391c59f2013-09-03 12:16:28 -0700962static void wpas_p2p_add_psk_list(struct wpa_supplicant *wpa_s,
963 struct wpa_ssid *ssid)
964{
965 struct wpa_ssid *persistent;
966 struct psk_list_entry *psk;
967 struct hostapd_data *hapd;
968
969 if (!wpa_s->ap_iface)
970 return;
971
972 persistent = wpas_p2p_get_persistent(wpa_s->parent, NULL, ssid->ssid,
973 ssid->ssid_len);
974 if (persistent == NULL)
975 return;
976
977 hapd = wpa_s->ap_iface->bss[0];
978
979 dl_list_for_each(psk, &persistent->psk_list, struct psk_list_entry,
980 list) {
981 struct hostapd_wpa_psk *hpsk;
982
983 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Add persistent group PSK entry for "
984 MACSTR " psk=%d",
985 MAC2STR(psk->addr), psk->p2p);
986 hpsk = os_zalloc(sizeof(*hpsk));
987 if (hpsk == NULL)
988 break;
989 os_memcpy(hpsk->psk, psk->psk, PMK_LEN);
990 if (psk->p2p)
991 os_memcpy(hpsk->p2p_dev_addr, psk->addr, ETH_ALEN);
992 else
993 os_memcpy(hpsk->addr, psk->addr, ETH_ALEN);
994 hpsk->next = hapd->conf->ssid.wpa_psk;
995 hapd->conf->ssid.wpa_psk = hpsk;
996 }
997}
998
999
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001000static void p2p_go_configured(void *ctx, void *data)
1001{
1002 struct wpa_supplicant *wpa_s = ctx;
1003 struct p2p_go_neg_results *params = data;
1004 struct wpa_ssid *ssid;
Jouni Malinen75ecf522011-06-27 15:19:46 -07001005 int network_id = -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001006
1007 ssid = wpa_s->current_ssid;
1008 if (ssid && ssid->mode == WPAS_MODE_P2P_GO) {
1009 wpa_printf(MSG_DEBUG, "P2P: Group setup without provisioning");
1010 if (wpa_s->global->p2p_group_formation == wpa_s)
1011 wpa_s->global->p2p_group_formation = NULL;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001012 if (os_strlen(params->passphrase) > 0) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07001013 wpa_msg_global(wpa_s->parent, MSG_INFO,
1014 P2P_EVENT_GROUP_STARTED
1015 "%s GO ssid=\"%s\" freq=%d "
1016 "passphrase=\"%s\" go_dev_addr=" MACSTR
1017 "%s", wpa_s->ifname,
1018 wpa_ssid_txt(ssid->ssid, ssid->ssid_len),
1019 ssid->frequency, params->passphrase,
1020 MAC2STR(wpa_s->global->p2p_dev_addr),
1021 params->persistent_group ?
1022 " [PERSISTENT]" : "");
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001023 } else {
1024 char psk[65];
1025 wpa_snprintf_hex(psk, sizeof(psk), params->psk,
1026 sizeof(params->psk));
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07001027 wpa_msg_global(wpa_s->parent, MSG_INFO,
1028 P2P_EVENT_GROUP_STARTED
1029 "%s GO ssid=\"%s\" freq=%d psk=%s "
1030 "go_dev_addr=" MACSTR "%s",
1031 wpa_s->ifname,
1032 wpa_ssid_txt(ssid->ssid, ssid->ssid_len),
1033 ssid->frequency, psk,
1034 MAC2STR(wpa_s->global->p2p_dev_addr),
1035 params->persistent_group ?
1036 " [PERSISTENT]" : "");
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001037 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001038
Dmitry Shmidt56052862013-10-04 10:23:25 -07001039 os_get_time(&wpa_s->global->p2p_go_wait_client);
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07001040 if (params->persistent_group) {
Jouni Malinen75ecf522011-06-27 15:19:46 -07001041 network_id = wpas_p2p_store_persistent_group(
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001042 wpa_s->parent, ssid,
Dmitry Shmidt497c1d52011-07-21 15:19:46 -07001043 wpa_s->global->p2p_dev_addr);
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07001044 wpas_p2p_add_psk_list(wpa_s, ssid);
1045 }
Jouni Malinen75ecf522011-06-27 15:19:46 -07001046 if (network_id < 0)
1047 network_id = ssid->id;
1048 wpas_notify_p2p_group_started(wpa_s, ssid, network_id, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001049 wpas_p2p_cross_connect_setup(wpa_s);
1050 wpas_p2p_set_group_idle_timeout(wpa_s);
Dmitry Shmidt56052862013-10-04 10:23:25 -07001051
1052 if (wpa_s->p2p_first_connection_timeout) {
1053 wpa_dbg(wpa_s, MSG_DEBUG,
1054 "P2P: Start group formation timeout of %d seconds until first data connection on GO",
1055 wpa_s->p2p_first_connection_timeout);
1056 wpa_s->p2p_go_group_formation_completed = 0;
1057 wpa_s->global->p2p_group_formation = wpa_s;
1058 eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
1059 wpa_s->parent, NULL);
1060 eloop_register_timeout(
1061 wpa_s->p2p_first_connection_timeout, 0,
1062 wpas_p2p_group_formation_timeout,
1063 wpa_s->parent, NULL);
1064 }
1065
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001066 return;
1067 }
1068
1069 wpa_printf(MSG_DEBUG, "P2P: Setting up WPS for GO provisioning");
1070 if (wpa_supplicant_ap_mac_addr_filter(wpa_s,
1071 params->peer_interface_addr)) {
1072 wpa_printf(MSG_DEBUG, "P2P: Failed to setup MAC address "
1073 "filtering");
1074 return;
1075 }
1076 if (params->wps_method == WPS_PBC)
1077 wpa_supplicant_ap_wps_pbc(wpa_s, params->peer_interface_addr,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001078 params->peer_device_addr);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001079 else if (wpa_s->p2p_pin[0])
1080 wpa_supplicant_ap_wps_pin(wpa_s, params->peer_interface_addr,
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001081 wpa_s->p2p_pin, NULL, 0, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001082 os_free(wpa_s->go_params);
1083 wpa_s->go_params = NULL;
1084}
1085
1086
1087static void wpas_start_wps_go(struct wpa_supplicant *wpa_s,
1088 struct p2p_go_neg_results *params,
1089 int group_formation)
1090{
1091 struct wpa_ssid *ssid;
1092
Dmitry Shmidtaa532512012-09-24 10:35:31 -07001093 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Starting GO");
1094 if (wpas_copy_go_neg_results(wpa_s, params) < 0) {
1095 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Could not copy GO Negotiation "
1096 "results");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001097 return;
Dmitry Shmidtaa532512012-09-24 10:35:31 -07001098 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001099
1100 ssid = wpa_config_add_network(wpa_s->conf);
Dmitry Shmidtaa532512012-09-24 10:35:31 -07001101 if (ssid == NULL) {
1102 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Could not add network for GO");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001103 return;
Dmitry Shmidtaa532512012-09-24 10:35:31 -07001104 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001105
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001106 wpa_s->show_group_started = 0;
1107
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001108 wpa_config_set_network_defaults(ssid);
1109 ssid->temporary = 1;
1110 ssid->p2p_group = 1;
1111 ssid->p2p_persistent_group = params->persistent_group;
1112 ssid->mode = group_formation ? WPAS_MODE_P2P_GROUP_FORMATION :
1113 WPAS_MODE_P2P_GO;
1114 ssid->frequency = params->freq;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001115 ssid->ht40 = params->ht40;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001116 ssid->ssid = os_zalloc(params->ssid_len + 1);
1117 if (ssid->ssid) {
1118 os_memcpy(ssid->ssid, params->ssid, params->ssid_len);
1119 ssid->ssid_len = params->ssid_len;
1120 }
1121 ssid->auth_alg = WPA_AUTH_ALG_OPEN;
1122 ssid->key_mgmt = WPA_KEY_MGMT_PSK;
1123 ssid->proto = WPA_PROTO_RSN;
1124 ssid->pairwise_cipher = WPA_CIPHER_CCMP;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001125 if (os_strlen(params->passphrase) > 0) {
1126 ssid->passphrase = os_strdup(params->passphrase);
1127 if (ssid->passphrase == NULL) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07001128 wpa_msg_global(wpa_s, MSG_ERROR,
1129 "P2P: Failed to copy passphrase for GO");
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001130 wpa_config_remove_network(wpa_s->conf, ssid->id);
1131 return;
1132 }
1133 } else
1134 ssid->passphrase = NULL;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001135 ssid->psk_set = params->psk_set;
1136 if (ssid->psk_set)
1137 os_memcpy(ssid->psk, params->psk, sizeof(ssid->psk));
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001138 else if (ssid->passphrase)
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001139 wpa_config_update_psk(ssid);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001140 ssid->ap_max_inactivity = wpa_s->parent->conf->p2p_go_max_inactivity;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001141
1142 wpa_s->ap_configured_cb = p2p_go_configured;
1143 wpa_s->ap_configured_cb_ctx = wpa_s;
1144 wpa_s->ap_configured_cb_data = wpa_s->go_params;
Jouni Malinen75ecf522011-06-27 15:19:46 -07001145 wpa_s->connect_without_scan = ssid;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001146 wpa_s->reassociate = 1;
1147 wpa_s->disconnected = 0;
Dmitry Shmidtaa532512012-09-24 10:35:31 -07001148 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Request scan (that will be skipped) to "
1149 "start GO)");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001150 wpa_supplicant_req_scan(wpa_s, 0, 0);
1151}
1152
1153
1154static void wpas_p2p_clone_config(struct wpa_supplicant *dst,
1155 const struct wpa_supplicant *src)
1156{
1157 struct wpa_config *d;
1158 const struct wpa_config *s;
1159
1160 d = dst->conf;
1161 s = src->conf;
1162
1163#define C(n) if (s->n) d->n = os_strdup(s->n)
1164 C(device_name);
1165 C(manufacturer);
1166 C(model_name);
1167 C(model_number);
1168 C(serial_number);
1169 C(config_methods);
1170#undef C
1171
1172 os_memcpy(d->device_type, s->device_type, WPS_DEV_TYPE_LEN);
1173 os_memcpy(d->sec_device_type, s->sec_device_type,
1174 sizeof(d->sec_device_type));
1175 d->num_sec_device_types = s->num_sec_device_types;
1176
1177 d->p2p_group_idle = s->p2p_group_idle;
1178 d->p2p_intra_bss = s->p2p_intra_bss;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001179 d->persistent_reconnect = s->persistent_reconnect;
Dmitry Shmidt04949592012-07-19 12:16:46 -07001180 d->max_num_sta = s->max_num_sta;
1181 d->pbc_in_m1 = s->pbc_in_m1;
Dmitry Shmidt444d5672013-04-01 13:08:44 -07001182 d->ignore_old_scan_res = s->ignore_old_scan_res;
Dmitry Shmidt51b6ea82013-05-08 10:42:09 -07001183 d->beacon_int = s->beacon_int;
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07001184 d->disassoc_low_ack = s->disassoc_low_ack;
Dmitry Shmidt96571392013-10-14 12:54:46 -07001185 d->disable_scan_offload = s->disable_scan_offload;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001186}
1187
1188
Dmitry Shmidt34af3062013-07-11 10:46:32 -07001189static void wpas_p2p_get_group_ifname(struct wpa_supplicant *wpa_s,
1190 char *ifname, size_t len)
1191{
1192 char *ifname_ptr = wpa_s->ifname;
1193
1194 if (os_strncmp(wpa_s->ifname, P2P_MGMT_DEVICE_PREFIX,
1195 os_strlen(P2P_MGMT_DEVICE_PREFIX)) == 0) {
1196 ifname_ptr = os_strrchr(wpa_s->ifname, '-') + 1;
1197 }
1198
1199 os_snprintf(ifname, len, "p2p-%s-%d", ifname_ptr, wpa_s->p2p_group_idx);
1200 if (os_strlen(ifname) >= IFNAMSIZ &&
1201 os_strlen(wpa_s->ifname) < IFNAMSIZ) {
1202 /* Try to avoid going over the IFNAMSIZ length limit */
1203 os_snprintf(ifname, sizeof(ifname), "p2p-%d",
1204 wpa_s->p2p_group_idx);
1205 }
1206}
1207
1208
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001209static int wpas_p2p_add_group_interface(struct wpa_supplicant *wpa_s,
1210 enum wpa_driver_if_type type)
1211{
1212 char ifname[120], force_ifname[120];
1213
1214 if (wpa_s->pending_interface_name[0]) {
1215 wpa_printf(MSG_DEBUG, "P2P: Pending virtual interface exists "
1216 "- skip creation of a new one");
1217 if (is_zero_ether_addr(wpa_s->pending_interface_addr)) {
1218 wpa_printf(MSG_DEBUG, "P2P: Pending virtual address "
1219 "unknown?! ifname='%s'",
1220 wpa_s->pending_interface_name);
1221 return -1;
1222 }
1223 return 0;
1224 }
1225
Dmitry Shmidt34af3062013-07-11 10:46:32 -07001226 wpas_p2p_get_group_ifname(wpa_s, ifname, sizeof(ifname));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001227 force_ifname[0] = '\0';
1228
1229 wpa_printf(MSG_DEBUG, "P2P: Create a new interface %s for the group",
1230 ifname);
1231 wpa_s->p2p_group_idx++;
1232
1233 wpa_s->pending_interface_type = type;
1234 if (wpa_drv_if_add(wpa_s, type, ifname, NULL, NULL, force_ifname,
1235 wpa_s->pending_interface_addr, NULL) < 0) {
1236 wpa_printf(MSG_ERROR, "P2P: Failed to create new group "
1237 "interface");
1238 return -1;
1239 }
1240
1241 if (force_ifname[0]) {
1242 wpa_printf(MSG_DEBUG, "P2P: Driver forced interface name %s",
1243 force_ifname);
1244 os_strlcpy(wpa_s->pending_interface_name, force_ifname,
1245 sizeof(wpa_s->pending_interface_name));
1246 } else
1247 os_strlcpy(wpa_s->pending_interface_name, ifname,
1248 sizeof(wpa_s->pending_interface_name));
1249 wpa_printf(MSG_DEBUG, "P2P: Created pending virtual interface %s addr "
1250 MACSTR, wpa_s->pending_interface_name,
1251 MAC2STR(wpa_s->pending_interface_addr));
1252
1253 return 0;
1254}
1255
1256
1257static void wpas_p2p_remove_pending_group_interface(
1258 struct wpa_supplicant *wpa_s)
1259{
1260 if (!wpa_s->pending_interface_name[0] ||
1261 is_zero_ether_addr(wpa_s->pending_interface_addr))
1262 return; /* No pending virtual interface */
1263
1264 wpa_printf(MSG_DEBUG, "P2P: Removing pending group interface %s",
1265 wpa_s->pending_interface_name);
1266 wpa_drv_if_remove(wpa_s, wpa_s->pending_interface_type,
1267 wpa_s->pending_interface_name);
1268 os_memset(wpa_s->pending_interface_addr, 0, ETH_ALEN);
1269 wpa_s->pending_interface_name[0] = '\0';
1270}
1271
1272
1273static struct wpa_supplicant *
1274wpas_p2p_init_group_interface(struct wpa_supplicant *wpa_s, int go)
1275{
1276 struct wpa_interface iface;
1277 struct wpa_supplicant *group_wpa_s;
1278
1279 if (!wpa_s->pending_interface_name[0]) {
1280 wpa_printf(MSG_ERROR, "P2P: No pending group interface");
1281 if (!wpas_p2p_create_iface(wpa_s))
1282 return NULL;
1283 /*
1284 * Something has forced us to remove the pending interface; try
1285 * to create a new one and hope for the best that we will get
1286 * the same local address.
1287 */
1288 if (wpas_p2p_add_group_interface(wpa_s, go ? WPA_IF_P2P_GO :
1289 WPA_IF_P2P_CLIENT) < 0)
1290 return NULL;
1291 }
1292
1293 os_memset(&iface, 0, sizeof(iface));
1294 iface.ifname = wpa_s->pending_interface_name;
1295 iface.driver = wpa_s->driver->name;
Dmitry Shmidt34af3062013-07-11 10:46:32 -07001296 if (wpa_s->conf->ctrl_interface == NULL &&
1297 wpa_s->parent != wpa_s &&
1298 wpa_s->p2p_mgmt &&
1299 (wpa_s->drv_flags & WPA_DRIVER_FLAGS_DEDICATED_P2P_DEVICE))
1300 iface.ctrl_interface = wpa_s->parent->conf->ctrl_interface;
1301 else
1302 iface.ctrl_interface = wpa_s->conf->ctrl_interface;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001303 iface.driver_param = wpa_s->conf->driver_param;
1304 group_wpa_s = wpa_supplicant_add_iface(wpa_s->global, &iface);
1305 if (group_wpa_s == NULL) {
1306 wpa_printf(MSG_ERROR, "P2P: Failed to create new "
1307 "wpa_supplicant interface");
1308 return NULL;
1309 }
1310 wpa_s->pending_interface_name[0] = '\0';
1311 group_wpa_s->parent = wpa_s;
1312 group_wpa_s->p2p_group_interface = go ? P2P_GROUP_INTERFACE_GO :
1313 P2P_GROUP_INTERFACE_CLIENT;
1314 wpa_s->global->p2p_group_formation = group_wpa_s;
1315
1316 wpas_p2p_clone_config(group_wpa_s, wpa_s);
1317
1318 return group_wpa_s;
1319}
1320
1321
1322static void wpas_p2p_group_formation_timeout(void *eloop_ctx,
1323 void *timeout_ctx)
1324{
1325 struct wpa_supplicant *wpa_s = eloop_ctx;
1326 wpa_printf(MSG_DEBUG, "P2P: Group Formation timed out");
Dmitry Shmidt34af3062013-07-11 10:46:32 -07001327 wpas_p2p_group_formation_failed(wpa_s);
1328}
1329
1330
1331void wpas_p2p_group_formation_failed(struct wpa_supplicant *wpa_s)
1332{
1333 eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
1334 wpa_s->parent, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001335 if (wpa_s->global->p2p)
1336 p2p_group_formation_failed(wpa_s->global->p2p);
1337 else if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
1338 wpa_drv_p2p_group_formation_failed(wpa_s);
1339 wpas_group_formation_completed(wpa_s, 0);
1340}
1341
1342
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07001343void wpas_p2p_ap_setup_failed(struct wpa_supplicant *wpa_s)
1344{
1345 if (wpa_s->global->p2p_group_formation != wpa_s)
1346 return;
1347 /* Speed up group formation timeout since this cannot succeed */
1348 eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
1349 wpa_s->parent, NULL);
1350 eloop_register_timeout(0, 0, wpas_p2p_group_formation_timeout,
1351 wpa_s->parent, NULL);
1352}
1353
1354
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001355void wpas_go_neg_completed(void *ctx, struct p2p_go_neg_results *res)
1356{
1357 struct wpa_supplicant *wpa_s = ctx;
1358
1359 if (wpa_s->off_channel_freq || wpa_s->roc_waiting_drv_freq) {
1360 wpa_drv_cancel_remain_on_channel(wpa_s);
1361 wpa_s->off_channel_freq = 0;
1362 wpa_s->roc_waiting_drv_freq = 0;
1363 }
1364
1365 if (res->status) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07001366 wpa_msg_global(wpa_s, MSG_INFO,
1367 P2P_EVENT_GO_NEG_FAILURE "status=%d",
1368 res->status);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001369 wpas_notify_p2p_go_neg_completed(wpa_s, res);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001370 wpas_p2p_remove_pending_group_interface(wpa_s);
1371 return;
1372 }
1373
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001374 if (wpa_s->p2p_go_ht40)
1375 res->ht40 = 1;
1376
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07001377 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_GO_NEG_SUCCESS "role=%s "
1378 "freq=%d ht40=%d peer_dev=" MACSTR " peer_iface=" MACSTR
1379 " wps_method=%s",
1380 res->role_go ? "GO" : "client", res->freq, res->ht40,
1381 MAC2STR(res->peer_device_addr),
1382 MAC2STR(res->peer_interface_addr),
1383 p2p_wps_method_text(res->wps_method));
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001384 wpas_notify_p2p_go_neg_completed(wpa_s, res);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001385
Dmitry Shmidt04949592012-07-19 12:16:46 -07001386 if (res->role_go && wpa_s->p2p_persistent_id >= 0) {
1387 struct wpa_ssid *ssid;
1388 ssid = wpa_config_get_network(wpa_s->conf,
1389 wpa_s->p2p_persistent_id);
1390 if (ssid && ssid->disabled == 2 &&
1391 ssid->mode == WPAS_MODE_P2P_GO && ssid->passphrase) {
1392 size_t len = os_strlen(ssid->passphrase);
1393 wpa_printf(MSG_DEBUG, "P2P: Override passphrase based "
1394 "on requested persistent group");
1395 os_memcpy(res->passphrase, ssid->passphrase, len);
1396 res->passphrase[len] = '\0';
1397 }
1398 }
1399
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001400 if (wpa_s->create_p2p_iface) {
1401 struct wpa_supplicant *group_wpa_s =
1402 wpas_p2p_init_group_interface(wpa_s, res->role_go);
1403 if (group_wpa_s == NULL) {
1404 wpas_p2p_remove_pending_group_interface(wpa_s);
1405 return;
1406 }
1407 if (group_wpa_s != wpa_s) {
1408 os_memcpy(group_wpa_s->p2p_pin, wpa_s->p2p_pin,
1409 sizeof(group_wpa_s->p2p_pin));
1410 group_wpa_s->p2p_wps_method = wpa_s->p2p_wps_method;
1411 }
1412 os_memset(wpa_s->pending_interface_addr, 0, ETH_ALEN);
1413 wpa_s->pending_interface_name[0] = '\0';
1414 group_wpa_s->p2p_in_provisioning = 1;
1415
1416 if (res->role_go)
1417 wpas_start_wps_go(group_wpa_s, res, 1);
1418 else
1419 wpas_start_wps_enrollee(group_wpa_s, res);
1420 } else {
1421 wpa_s->p2p_in_provisioning = 1;
1422 wpa_s->global->p2p_group_formation = wpa_s;
1423
1424 if (res->role_go)
1425 wpas_start_wps_go(wpa_s, res, 1);
1426 else
1427 wpas_start_wps_enrollee(ctx, res);
1428 }
1429
1430 wpa_s->p2p_long_listen = 0;
1431 eloop_cancel_timeout(wpas_p2p_long_listen_timeout, wpa_s, NULL);
1432
1433 eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s, NULL);
1434 eloop_register_timeout(15 + res->peer_config_timeout / 100,
1435 (res->peer_config_timeout % 100) * 10000,
1436 wpas_p2p_group_formation_timeout, wpa_s, NULL);
1437}
1438
1439
1440void wpas_go_neg_req_rx(void *ctx, const u8 *src, u16 dev_passwd_id)
1441{
1442 struct wpa_supplicant *wpa_s = ctx;
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07001443 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_GO_NEG_REQUEST MACSTR
1444 " dev_passwd_id=%u", MAC2STR(src), dev_passwd_id);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001445
1446 wpas_notify_p2p_go_neg_req(wpa_s, src, dev_passwd_id);
1447}
1448
1449
1450void wpas_dev_found(void *ctx, const u8 *addr,
1451 const struct p2p_peer_info *info,
1452 int new_device)
1453{
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001454#ifndef CONFIG_NO_STDOUT_DEBUG
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001455 struct wpa_supplicant *wpa_s = ctx;
1456 char devtype[WPS_DEV_TYPE_BUFSIZE];
Irfan Sheriff8367dc92012-09-09 17:08:19 -07001457#define WFD_DEV_INFO_SIZE 9
1458 char wfd_dev_info_hex[2 * WFD_DEV_INFO_SIZE + 1];
Irfan Sheriff8d965182012-09-11 08:58:24 -07001459 os_memset(wfd_dev_info_hex, 0, sizeof(wfd_dev_info_hex));
Irfan Sheriff8367dc92012-09-09 17:08:19 -07001460#ifdef CONFIG_WIFI_DISPLAY
1461 if (info->wfd_subelems) {
1462 wpa_snprintf_hex(wfd_dev_info_hex, sizeof(wfd_dev_info_hex),
1463 wpabuf_head(info->wfd_subelems),
1464 WFD_DEV_INFO_SIZE);
1465 }
1466#endif /* CONFIG_WIFI_DISPLAY */
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07001467 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_DEVICE_FOUND MACSTR
1468 " p2p_dev_addr=" MACSTR
1469 " pri_dev_type=%s name='%s' config_methods=0x%x "
1470 "dev_capab=0x%x group_capab=0x%x%s%s",
1471 MAC2STR(addr), MAC2STR(info->p2p_device_addr),
1472 wps_dev_type_bin2str(info->pri_dev_type, devtype,
1473 sizeof(devtype)),
1474 info->device_name, info->config_methods,
1475 info->dev_capab, info->group_capab,
1476 wfd_dev_info_hex[0] ? " wfd_dev_info=0x" : "", wfd_dev_info_hex);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001477#endif /* CONFIG_NO_STDOUT_DEBUG */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001478
1479 wpas_notify_p2p_device_found(ctx, info->p2p_device_addr, new_device);
1480}
1481
1482
1483static void wpas_dev_lost(void *ctx, const u8 *dev_addr)
1484{
1485 struct wpa_supplicant *wpa_s = ctx;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001486
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07001487 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_DEVICE_LOST
1488 "p2p_dev_addr=" MACSTR, MAC2STR(dev_addr));
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001489
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001490 wpas_notify_p2p_device_lost(wpa_s, dev_addr);
1491}
1492
1493
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07001494static void wpas_find_stopped(void *ctx)
1495{
1496 struct wpa_supplicant *wpa_s = ctx;
1497 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_FIND_STOPPED);
1498}
1499
1500
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001501static int wpas_start_listen(void *ctx, unsigned int freq,
1502 unsigned int duration,
1503 const struct wpabuf *probe_resp_ie)
1504{
1505 struct wpa_supplicant *wpa_s = ctx;
1506
1507 wpa_drv_set_ap_wps_ie(wpa_s, NULL, probe_resp_ie, NULL);
1508
1509 if (wpa_drv_probe_req_report(wpa_s, 1) < 0) {
1510 wpa_printf(MSG_DEBUG, "P2P: Failed to request the driver to "
1511 "report received Probe Request frames");
1512 return -1;
1513 }
1514
1515 wpa_s->pending_listen_freq = freq;
1516 wpa_s->pending_listen_duration = duration;
1517
1518 if (wpa_drv_remain_on_channel(wpa_s, freq, duration) < 0) {
1519 wpa_printf(MSG_DEBUG, "P2P: Failed to request the driver "
1520 "to remain on channel (%u MHz) for Listen "
1521 "state", freq);
1522 wpa_s->pending_listen_freq = 0;
1523 return -1;
1524 }
1525 wpa_s->off_channel_freq = 0;
1526 wpa_s->roc_waiting_drv_freq = freq;
1527
1528 return 0;
1529}
1530
1531
1532static void wpas_stop_listen(void *ctx)
1533{
1534 struct wpa_supplicant *wpa_s = ctx;
1535 if (wpa_s->off_channel_freq || wpa_s->roc_waiting_drv_freq) {
1536 wpa_drv_cancel_remain_on_channel(wpa_s);
1537 wpa_s->off_channel_freq = 0;
1538 wpa_s->roc_waiting_drv_freq = 0;
1539 }
1540 wpa_drv_set_ap_wps_ie(wpa_s, NULL, NULL, NULL);
1541 wpa_drv_probe_req_report(wpa_s, 0);
1542}
1543
1544
1545static int wpas_send_probe_resp(void *ctx, const struct wpabuf *buf)
1546{
1547 struct wpa_supplicant *wpa_s = ctx;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001548 return wpa_drv_send_mlme(wpa_s, wpabuf_head(buf), wpabuf_len(buf), 1);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001549}
1550
1551
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001552/*
1553 * DNS Header section is used only to calculate compression pointers, so the
1554 * contents of this data does not matter, but the length needs to be reserved
1555 * in the virtual packet.
1556 */
1557#define DNS_HEADER_LEN 12
1558
1559/*
1560 * 27-octet in-memory packet from P2P specification containing two implied
1561 * queries for _tcp.lcoal. PTR IN and _udp.local. PTR IN
1562 */
1563#define P2P_SD_IN_MEMORY_LEN 27
1564
1565static int p2p_sd_dns_uncompress_label(char **upos, char *uend, u8 *start,
1566 u8 **spos, const u8 *end)
1567{
1568 while (*spos < end) {
1569 u8 val = ((*spos)[0] & 0xc0) >> 6;
1570 int len;
1571
1572 if (val == 1 || val == 2) {
1573 /* These are reserved values in RFC 1035 */
1574 wpa_printf(MSG_DEBUG, "P2P: Invalid domain name "
1575 "sequence starting with 0x%x", val);
1576 return -1;
1577 }
1578
1579 if (val == 3) {
1580 u16 offset;
1581 u8 *spos_tmp;
1582
1583 /* Offset */
1584 if (*spos + 2 > end) {
1585 wpa_printf(MSG_DEBUG, "P2P: No room for full "
1586 "DNS offset field");
1587 return -1;
1588 }
1589
1590 offset = (((*spos)[0] & 0x3f) << 8) | (*spos)[1];
1591 if (offset >= *spos - start) {
1592 wpa_printf(MSG_DEBUG, "P2P: Invalid DNS "
1593 "pointer offset %u", offset);
1594 return -1;
1595 }
1596
1597 (*spos) += 2;
1598 spos_tmp = start + offset;
1599 return p2p_sd_dns_uncompress_label(upos, uend, start,
1600 &spos_tmp,
1601 *spos - 2);
1602 }
1603
1604 /* Label */
1605 len = (*spos)[0] & 0x3f;
1606 if (len == 0)
1607 return 0;
1608
1609 (*spos)++;
1610 if (*spos + len > end) {
1611 wpa_printf(MSG_DEBUG, "P2P: Invalid domain name "
1612 "sequence - no room for label with length "
1613 "%u", len);
1614 return -1;
1615 }
1616
1617 if (*upos + len + 2 > uend)
1618 return -2;
1619
1620 os_memcpy(*upos, *spos, len);
1621 *spos += len;
1622 *upos += len;
1623 (*upos)[0] = '.';
1624 (*upos)++;
1625 (*upos)[0] = '\0';
1626 }
1627
1628 return 0;
1629}
1630
1631
1632/* Uncompress domain names per RFC 1035 using the P2P SD in-memory packet.
1633 * Returns -1 on parsing error (invalid input sequence), -2 if output buffer is
1634 * not large enough */
1635static int p2p_sd_dns_uncompress(char *buf, size_t buf_len, const u8 *msg,
1636 size_t msg_len, size_t offset)
1637{
1638 /* 27-octet in-memory packet from P2P specification */
1639 const char *prefix = "\x04_tcp\x05local\x00\x00\x0C\x00\x01"
1640 "\x04_udp\xC0\x11\x00\x0C\x00\x01";
1641 u8 *tmp, *end, *spos;
1642 char *upos, *uend;
1643 int ret = 0;
1644
1645 if (buf_len < 2)
1646 return -1;
1647 if (offset > msg_len)
1648 return -1;
1649
1650 tmp = os_malloc(DNS_HEADER_LEN + P2P_SD_IN_MEMORY_LEN + msg_len);
1651 if (tmp == NULL)
1652 return -1;
1653 spos = tmp + DNS_HEADER_LEN + P2P_SD_IN_MEMORY_LEN;
1654 end = spos + msg_len;
1655 spos += offset;
1656
1657 os_memset(tmp, 0, DNS_HEADER_LEN);
1658 os_memcpy(tmp + DNS_HEADER_LEN, prefix, P2P_SD_IN_MEMORY_LEN);
1659 os_memcpy(tmp + DNS_HEADER_LEN + P2P_SD_IN_MEMORY_LEN, msg, msg_len);
1660
1661 upos = buf;
1662 uend = buf + buf_len;
1663
1664 ret = p2p_sd_dns_uncompress_label(&upos, uend, tmp, &spos, end);
1665 if (ret) {
1666 os_free(tmp);
1667 return ret;
1668 }
1669
1670 if (upos == buf) {
1671 upos[0] = '.';
1672 upos[1] = '\0';
1673 } else if (upos[-1] == '.')
1674 upos[-1] = '\0';
1675
1676 os_free(tmp);
1677 return 0;
1678}
1679
1680
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001681static struct p2p_srv_bonjour *
1682wpas_p2p_service_get_bonjour(struct wpa_supplicant *wpa_s,
1683 const struct wpabuf *query)
1684{
1685 struct p2p_srv_bonjour *bsrv;
1686 size_t len;
1687
1688 len = wpabuf_len(query);
1689 dl_list_for_each(bsrv, &wpa_s->global->p2p_srv_bonjour,
1690 struct p2p_srv_bonjour, list) {
1691 if (len == wpabuf_len(bsrv->query) &&
1692 os_memcmp(wpabuf_head(query), wpabuf_head(bsrv->query),
1693 len) == 0)
1694 return bsrv;
1695 }
1696 return NULL;
1697}
1698
1699
1700static struct p2p_srv_upnp *
1701wpas_p2p_service_get_upnp(struct wpa_supplicant *wpa_s, u8 version,
1702 const char *service)
1703{
1704 struct p2p_srv_upnp *usrv;
1705
1706 dl_list_for_each(usrv, &wpa_s->global->p2p_srv_upnp,
1707 struct p2p_srv_upnp, list) {
1708 if (version == usrv->version &&
1709 os_strcmp(service, usrv->service) == 0)
1710 return usrv;
1711 }
1712 return NULL;
1713}
1714
1715
1716static void wpas_sd_add_proto_not_avail(struct wpabuf *resp, u8 srv_proto,
1717 u8 srv_trans_id)
1718{
1719 u8 *len_pos;
1720
1721 if (wpabuf_tailroom(resp) < 5)
1722 return;
1723
1724 /* Length (to be filled) */
1725 len_pos = wpabuf_put(resp, 2);
1726 wpabuf_put_u8(resp, srv_proto);
1727 wpabuf_put_u8(resp, srv_trans_id);
1728 /* Status Code */
1729 wpabuf_put_u8(resp, P2P_SD_PROTO_NOT_AVAILABLE);
1730 /* Response Data: empty */
1731 WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos - 2);
1732}
1733
1734
1735static void wpas_sd_all_bonjour(struct wpa_supplicant *wpa_s,
1736 struct wpabuf *resp, u8 srv_trans_id)
1737{
1738 struct p2p_srv_bonjour *bsrv;
1739 u8 *len_pos;
1740
1741 wpa_printf(MSG_DEBUG, "P2P: SD Request for all Bonjour services");
1742
1743 if (dl_list_empty(&wpa_s->global->p2p_srv_bonjour)) {
1744 wpa_printf(MSG_DEBUG, "P2P: Bonjour protocol not available");
1745 return;
1746 }
1747
1748 dl_list_for_each(bsrv, &wpa_s->global->p2p_srv_bonjour,
1749 struct p2p_srv_bonjour, list) {
1750 if (wpabuf_tailroom(resp) <
1751 5 + wpabuf_len(bsrv->query) + wpabuf_len(bsrv->resp))
1752 return;
1753 /* Length (to be filled) */
1754 len_pos = wpabuf_put(resp, 2);
1755 wpabuf_put_u8(resp, P2P_SERV_BONJOUR);
1756 wpabuf_put_u8(resp, srv_trans_id);
1757 /* Status Code */
1758 wpabuf_put_u8(resp, P2P_SD_SUCCESS);
1759 wpa_hexdump_ascii(MSG_DEBUG, "P2P: Matching Bonjour service",
1760 wpabuf_head(bsrv->resp),
1761 wpabuf_len(bsrv->resp));
1762 /* Response Data */
1763 wpabuf_put_buf(resp, bsrv->query); /* Key */
1764 wpabuf_put_buf(resp, bsrv->resp); /* Value */
1765 WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos -
1766 2);
1767 }
1768}
1769
1770
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001771static int match_bonjour_query(struct p2p_srv_bonjour *bsrv, const u8 *query,
1772 size_t query_len)
1773{
1774 char str_rx[256], str_srv[256];
1775
1776 if (query_len < 3 || wpabuf_len(bsrv->query) < 3)
1777 return 0; /* Too short to include DNS Type and Version */
1778 if (os_memcmp(query + query_len - 3,
1779 wpabuf_head_u8(bsrv->query) + wpabuf_len(bsrv->query) - 3,
1780 3) != 0)
1781 return 0; /* Mismatch in DNS Type or Version */
1782 if (query_len == wpabuf_len(bsrv->query) &&
1783 os_memcmp(query, wpabuf_head(bsrv->query), query_len - 3) == 0)
1784 return 1; /* Binary match */
1785
1786 if (p2p_sd_dns_uncompress(str_rx, sizeof(str_rx), query, query_len - 3,
1787 0))
1788 return 0; /* Failed to uncompress query */
1789 if (p2p_sd_dns_uncompress(str_srv, sizeof(str_srv),
1790 wpabuf_head(bsrv->query),
1791 wpabuf_len(bsrv->query) - 3, 0))
1792 return 0; /* Failed to uncompress service */
1793
1794 return os_strcmp(str_rx, str_srv) == 0;
1795}
1796
1797
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001798static void wpas_sd_req_bonjour(struct wpa_supplicant *wpa_s,
1799 struct wpabuf *resp, u8 srv_trans_id,
1800 const u8 *query, size_t query_len)
1801{
1802 struct p2p_srv_bonjour *bsrv;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001803 u8 *len_pos;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001804 int matches = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001805
1806 wpa_hexdump_ascii(MSG_DEBUG, "P2P: SD Request for Bonjour",
1807 query, query_len);
1808 if (dl_list_empty(&wpa_s->global->p2p_srv_bonjour)) {
1809 wpa_printf(MSG_DEBUG, "P2P: Bonjour protocol not available");
1810 wpas_sd_add_proto_not_avail(resp, P2P_SERV_BONJOUR,
1811 srv_trans_id);
1812 return;
1813 }
1814
1815 if (query_len == 0) {
1816 wpas_sd_all_bonjour(wpa_s, resp, srv_trans_id);
1817 return;
1818 }
1819
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001820 dl_list_for_each(bsrv, &wpa_s->global->p2p_srv_bonjour,
1821 struct p2p_srv_bonjour, list) {
1822 if (!match_bonjour_query(bsrv, query, query_len))
1823 continue;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001824
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001825 if (wpabuf_tailroom(resp) <
1826 5 + query_len + wpabuf_len(bsrv->resp))
1827 return;
1828
1829 matches++;
1830
1831 /* Length (to be filled) */
1832 len_pos = wpabuf_put(resp, 2);
1833 wpabuf_put_u8(resp, P2P_SERV_BONJOUR);
1834 wpabuf_put_u8(resp, srv_trans_id);
1835
1836 /* Status Code */
1837 wpabuf_put_u8(resp, P2P_SD_SUCCESS);
1838 wpa_hexdump_ascii(MSG_DEBUG, "P2P: Matching Bonjour service",
1839 wpabuf_head(bsrv->resp),
1840 wpabuf_len(bsrv->resp));
1841
1842 /* Response Data */
1843 wpabuf_put_data(resp, query, query_len); /* Key */
1844 wpabuf_put_buf(resp, bsrv->resp); /* Value */
1845
1846 WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos - 2);
1847 }
1848
1849 if (matches == 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001850 wpa_printf(MSG_DEBUG, "P2P: Requested Bonjour service not "
1851 "available");
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001852 if (wpabuf_tailroom(resp) < 5)
1853 return;
1854
1855 /* Length (to be filled) */
1856 len_pos = wpabuf_put(resp, 2);
1857 wpabuf_put_u8(resp, P2P_SERV_BONJOUR);
1858 wpabuf_put_u8(resp, srv_trans_id);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001859
1860 /* Status Code */
1861 wpabuf_put_u8(resp, P2P_SD_REQUESTED_INFO_NOT_AVAILABLE);
1862 /* Response Data: empty */
1863 WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos -
1864 2);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001865 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001866}
1867
1868
1869static void wpas_sd_all_upnp(struct wpa_supplicant *wpa_s,
1870 struct wpabuf *resp, u8 srv_trans_id)
1871{
1872 struct p2p_srv_upnp *usrv;
1873 u8 *len_pos;
1874
1875 wpa_printf(MSG_DEBUG, "P2P: SD Request for all UPnP services");
1876
1877 if (dl_list_empty(&wpa_s->global->p2p_srv_upnp)) {
1878 wpa_printf(MSG_DEBUG, "P2P: UPnP protocol not available");
1879 return;
1880 }
1881
1882 dl_list_for_each(usrv, &wpa_s->global->p2p_srv_upnp,
1883 struct p2p_srv_upnp, list) {
1884 if (wpabuf_tailroom(resp) < 5 + 1 + os_strlen(usrv->service))
1885 return;
1886
1887 /* Length (to be filled) */
1888 len_pos = wpabuf_put(resp, 2);
1889 wpabuf_put_u8(resp, P2P_SERV_UPNP);
1890 wpabuf_put_u8(resp, srv_trans_id);
1891
1892 /* Status Code */
1893 wpabuf_put_u8(resp, P2P_SD_SUCCESS);
1894 /* Response Data */
1895 wpabuf_put_u8(resp, usrv->version);
1896 wpa_printf(MSG_DEBUG, "P2P: Matching UPnP Service: %s",
1897 usrv->service);
1898 wpabuf_put_str(resp, usrv->service);
1899 WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos -
1900 2);
1901 }
1902}
1903
1904
1905static void wpas_sd_req_upnp(struct wpa_supplicant *wpa_s,
1906 struct wpabuf *resp, u8 srv_trans_id,
1907 const u8 *query, size_t query_len)
1908{
1909 struct p2p_srv_upnp *usrv;
1910 u8 *len_pos;
1911 u8 version;
1912 char *str;
1913 int count = 0;
1914
1915 wpa_hexdump_ascii(MSG_DEBUG, "P2P: SD Request for UPnP",
1916 query, query_len);
1917
1918 if (dl_list_empty(&wpa_s->global->p2p_srv_upnp)) {
1919 wpa_printf(MSG_DEBUG, "P2P: UPnP protocol not available");
1920 wpas_sd_add_proto_not_avail(resp, P2P_SERV_UPNP,
1921 srv_trans_id);
1922 return;
1923 }
1924
1925 if (query_len == 0) {
1926 wpas_sd_all_upnp(wpa_s, resp, srv_trans_id);
1927 return;
1928 }
1929
1930 if (wpabuf_tailroom(resp) < 5)
1931 return;
1932
1933 /* Length (to be filled) */
1934 len_pos = wpabuf_put(resp, 2);
1935 wpabuf_put_u8(resp, P2P_SERV_UPNP);
1936 wpabuf_put_u8(resp, srv_trans_id);
1937
1938 version = query[0];
1939 str = os_malloc(query_len);
1940 if (str == NULL)
1941 return;
1942 os_memcpy(str, query + 1, query_len - 1);
1943 str[query_len - 1] = '\0';
1944
1945 dl_list_for_each(usrv, &wpa_s->global->p2p_srv_upnp,
1946 struct p2p_srv_upnp, list) {
1947 if (version != usrv->version)
1948 continue;
1949
1950 if (os_strcmp(str, "ssdp:all") != 0 &&
1951 os_strstr(usrv->service, str) == NULL)
1952 continue;
1953
1954 if (wpabuf_tailroom(resp) < 2)
1955 break;
1956 if (count == 0) {
1957 /* Status Code */
1958 wpabuf_put_u8(resp, P2P_SD_SUCCESS);
1959 /* Response Data */
1960 wpabuf_put_u8(resp, version);
1961 } else
1962 wpabuf_put_u8(resp, ',');
1963
1964 count++;
1965
1966 wpa_printf(MSG_DEBUG, "P2P: Matching UPnP Service: %s",
1967 usrv->service);
1968 if (wpabuf_tailroom(resp) < os_strlen(usrv->service))
1969 break;
1970 wpabuf_put_str(resp, usrv->service);
1971 }
1972 os_free(str);
1973
1974 if (count == 0) {
1975 wpa_printf(MSG_DEBUG, "P2P: Requested UPnP service not "
1976 "available");
1977 /* Status Code */
1978 wpabuf_put_u8(resp, P2P_SD_REQUESTED_INFO_NOT_AVAILABLE);
1979 /* Response Data: empty */
1980 }
1981
1982 WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos - 2);
1983}
1984
1985
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001986#ifdef CONFIG_WIFI_DISPLAY
1987static void wpas_sd_req_wfd(struct wpa_supplicant *wpa_s,
1988 struct wpabuf *resp, u8 srv_trans_id,
1989 const u8 *query, size_t query_len)
1990{
1991 const u8 *pos;
1992 u8 role;
1993 u8 *len_pos;
1994
1995 wpa_hexdump(MSG_DEBUG, "P2P: SD Request for WFD", query, query_len);
1996
1997 if (!wpa_s->global->wifi_display) {
1998 wpa_printf(MSG_DEBUG, "P2P: WFD protocol not available");
1999 wpas_sd_add_proto_not_avail(resp, P2P_SERV_WIFI_DISPLAY,
2000 srv_trans_id);
2001 return;
2002 }
2003
2004 if (query_len < 1) {
2005 wpa_printf(MSG_DEBUG, "P2P: Missing WFD Requested Device "
2006 "Role");
2007 return;
2008 }
2009
2010 if (wpabuf_tailroom(resp) < 5)
2011 return;
2012
2013 pos = query;
2014 role = *pos++;
2015 wpa_printf(MSG_DEBUG, "P2P: WSD for device role 0x%x", role);
2016
2017 /* TODO: role specific handling */
2018
2019 /* Length (to be filled) */
2020 len_pos = wpabuf_put(resp, 2);
2021 wpabuf_put_u8(resp, P2P_SERV_WIFI_DISPLAY);
2022 wpabuf_put_u8(resp, srv_trans_id);
2023 wpabuf_put_u8(resp, P2P_SD_SUCCESS); /* Status Code */
2024
2025 while (pos < query + query_len) {
2026 if (*pos < MAX_WFD_SUBELEMS &&
2027 wpa_s->global->wfd_subelem[*pos] &&
2028 wpabuf_tailroom(resp) >=
2029 wpabuf_len(wpa_s->global->wfd_subelem[*pos])) {
2030 wpa_printf(MSG_DEBUG, "P2P: Add WSD response "
2031 "subelement %u", *pos);
2032 wpabuf_put_buf(resp, wpa_s->global->wfd_subelem[*pos]);
2033 }
2034 pos++;
2035 }
2036
2037 WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos - 2);
2038}
2039#endif /* CONFIG_WIFI_DISPLAY */
2040
2041
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002042void wpas_sd_request(void *ctx, int freq, const u8 *sa, u8 dialog_token,
2043 u16 update_indic, const u8 *tlvs, size_t tlvs_len)
2044{
2045 struct wpa_supplicant *wpa_s = ctx;
2046 const u8 *pos = tlvs;
2047 const u8 *end = tlvs + tlvs_len;
2048 const u8 *tlv_end;
2049 u16 slen;
2050 struct wpabuf *resp;
2051 u8 srv_proto, srv_trans_id;
2052 size_t buf_len;
2053 char *buf;
2054
2055 wpa_hexdump(MSG_MSGDUMP, "P2P: Service Discovery Request TLVs",
2056 tlvs, tlvs_len);
2057 buf_len = 2 * tlvs_len + 1;
2058 buf = os_malloc(buf_len);
2059 if (buf) {
2060 wpa_snprintf_hex(buf, buf_len, tlvs, tlvs_len);
2061 wpa_msg_ctrl(wpa_s, MSG_INFO, P2P_EVENT_SERV_DISC_REQ "%d "
2062 MACSTR " %u %u %s",
2063 freq, MAC2STR(sa), dialog_token, update_indic,
2064 buf);
2065 os_free(buf);
2066 }
2067
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002068 if (wpa_s->p2p_sd_over_ctrl_iface) {
2069 wpas_notify_p2p_sd_request(wpa_s, freq, sa, dialog_token,
2070 update_indic, tlvs, tlvs_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002071 return; /* to be processed by an external program */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002072 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002073
2074 resp = wpabuf_alloc(10000);
2075 if (resp == NULL)
2076 return;
2077
2078 while (pos + 1 < end) {
2079 wpa_printf(MSG_DEBUG, "P2P: Service Request TLV");
2080 slen = WPA_GET_LE16(pos);
2081 pos += 2;
2082 if (pos + slen > end || slen < 2) {
2083 wpa_printf(MSG_DEBUG, "P2P: Unexpected Query Data "
2084 "length");
2085 wpabuf_free(resp);
2086 return;
2087 }
2088 tlv_end = pos + slen;
2089
2090 srv_proto = *pos++;
2091 wpa_printf(MSG_DEBUG, "P2P: Service Protocol Type %u",
2092 srv_proto);
2093 srv_trans_id = *pos++;
2094 wpa_printf(MSG_DEBUG, "P2P: Service Transaction ID %u",
2095 srv_trans_id);
2096
2097 wpa_hexdump(MSG_MSGDUMP, "P2P: Query Data",
2098 pos, tlv_end - pos);
2099
2100
2101 if (wpa_s->force_long_sd) {
2102 wpa_printf(MSG_DEBUG, "P2P: SD test - force long "
2103 "response");
2104 wpas_sd_all_bonjour(wpa_s, resp, srv_trans_id);
2105 wpas_sd_all_upnp(wpa_s, resp, srv_trans_id);
2106 goto done;
2107 }
2108
2109 switch (srv_proto) {
2110 case P2P_SERV_ALL_SERVICES:
2111 wpa_printf(MSG_DEBUG, "P2P: Service Discovery Request "
2112 "for all services");
2113 if (dl_list_empty(&wpa_s->global->p2p_srv_upnp) &&
2114 dl_list_empty(&wpa_s->global->p2p_srv_bonjour)) {
2115 wpa_printf(MSG_DEBUG, "P2P: No service "
2116 "discovery protocols available");
2117 wpas_sd_add_proto_not_avail(
2118 resp, P2P_SERV_ALL_SERVICES,
2119 srv_trans_id);
2120 break;
2121 }
2122 wpas_sd_all_bonjour(wpa_s, resp, srv_trans_id);
2123 wpas_sd_all_upnp(wpa_s, resp, srv_trans_id);
2124 break;
2125 case P2P_SERV_BONJOUR:
2126 wpas_sd_req_bonjour(wpa_s, resp, srv_trans_id,
2127 pos, tlv_end - pos);
2128 break;
2129 case P2P_SERV_UPNP:
2130 wpas_sd_req_upnp(wpa_s, resp, srv_trans_id,
2131 pos, tlv_end - pos);
2132 break;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002133#ifdef CONFIG_WIFI_DISPLAY
2134 case P2P_SERV_WIFI_DISPLAY:
2135 wpas_sd_req_wfd(wpa_s, resp, srv_trans_id,
2136 pos, tlv_end - pos);
2137 break;
2138#endif /* CONFIG_WIFI_DISPLAY */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002139 default:
2140 wpa_printf(MSG_DEBUG, "P2P: Unavailable service "
2141 "protocol %u", srv_proto);
2142 wpas_sd_add_proto_not_avail(resp, srv_proto,
2143 srv_trans_id);
2144 break;
2145 }
2146
2147 pos = tlv_end;
2148 }
2149
2150done:
2151 wpas_notify_p2p_sd_request(wpa_s, freq, sa, dialog_token,
2152 update_indic, tlvs, tlvs_len);
2153
2154 wpas_p2p_sd_response(wpa_s, freq, sa, dialog_token, resp);
2155
2156 wpabuf_free(resp);
2157}
2158
2159
2160void wpas_sd_response(void *ctx, const u8 *sa, u16 update_indic,
2161 const u8 *tlvs, size_t tlvs_len)
2162{
2163 struct wpa_supplicant *wpa_s = ctx;
2164 const u8 *pos = tlvs;
2165 const u8 *end = tlvs + tlvs_len;
2166 const u8 *tlv_end;
2167 u16 slen;
2168 size_t buf_len;
2169 char *buf;
2170
2171 wpa_hexdump(MSG_MSGDUMP, "P2P: Service Discovery Response TLVs",
2172 tlvs, tlvs_len);
2173 if (tlvs_len > 1500) {
2174 /* TODO: better way for handling this */
2175 wpa_msg_ctrl(wpa_s, MSG_INFO,
2176 P2P_EVENT_SERV_DISC_RESP MACSTR
2177 " %u <long response: %u bytes>",
2178 MAC2STR(sa), update_indic,
2179 (unsigned int) tlvs_len);
2180 } else {
2181 buf_len = 2 * tlvs_len + 1;
2182 buf = os_malloc(buf_len);
2183 if (buf) {
2184 wpa_snprintf_hex(buf, buf_len, tlvs, tlvs_len);
2185 wpa_msg_ctrl(wpa_s, MSG_INFO,
2186 P2P_EVENT_SERV_DISC_RESP MACSTR " %u %s",
2187 MAC2STR(sa), update_indic, buf);
2188 os_free(buf);
2189 }
2190 }
2191
2192 while (pos < end) {
2193 u8 srv_proto, srv_trans_id, status;
2194
2195 wpa_printf(MSG_DEBUG, "P2P: Service Response TLV");
2196 slen = WPA_GET_LE16(pos);
2197 pos += 2;
2198 if (pos + slen > end || slen < 3) {
2199 wpa_printf(MSG_DEBUG, "P2P: Unexpected Response Data "
2200 "length");
2201 return;
2202 }
2203 tlv_end = pos + slen;
2204
2205 srv_proto = *pos++;
2206 wpa_printf(MSG_DEBUG, "P2P: Service Protocol Type %u",
2207 srv_proto);
2208 srv_trans_id = *pos++;
2209 wpa_printf(MSG_DEBUG, "P2P: Service Transaction ID %u",
2210 srv_trans_id);
2211 status = *pos++;
2212 wpa_printf(MSG_DEBUG, "P2P: Status Code ID %u",
2213 status);
2214
2215 wpa_hexdump(MSG_MSGDUMP, "P2P: Response Data",
2216 pos, tlv_end - pos);
2217
2218 pos = tlv_end;
2219 }
2220
2221 wpas_notify_p2p_sd_response(wpa_s, sa, update_indic, tlvs, tlvs_len);
2222}
2223
2224
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002225u64 wpas_p2p_sd_request(struct wpa_supplicant *wpa_s, const u8 *dst,
2226 const struct wpabuf *tlvs)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002227{
2228 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002229 return wpa_drv_p2p_sd_request(wpa_s, dst, tlvs);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002230 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002231 return 0;
2232 return (uintptr_t) p2p_sd_request(wpa_s->global->p2p, dst, tlvs);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002233}
2234
2235
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002236u64 wpas_p2p_sd_request_upnp(struct wpa_supplicant *wpa_s, const u8 *dst,
2237 u8 version, const char *query)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002238{
2239 struct wpabuf *tlvs;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002240 u64 ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002241
2242 tlvs = wpabuf_alloc(2 + 1 + 1 + 1 + os_strlen(query));
2243 if (tlvs == NULL)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002244 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002245 wpabuf_put_le16(tlvs, 1 + 1 + 1 + os_strlen(query));
2246 wpabuf_put_u8(tlvs, P2P_SERV_UPNP); /* Service Protocol Type */
2247 wpabuf_put_u8(tlvs, 1); /* Service Transaction ID */
2248 wpabuf_put_u8(tlvs, version);
2249 wpabuf_put_str(tlvs, query);
2250 ret = wpas_p2p_sd_request(wpa_s, dst, tlvs);
2251 wpabuf_free(tlvs);
2252 return ret;
2253}
2254
2255
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002256#ifdef CONFIG_WIFI_DISPLAY
2257
2258static u64 wpas_p2p_sd_request_wfd(struct wpa_supplicant *wpa_s, const u8 *dst,
2259 const struct wpabuf *tlvs)
2260{
2261 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
2262 return 0;
2263 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
2264 return 0;
2265 return (uintptr_t) p2p_sd_request_wfd(wpa_s->global->p2p, dst, tlvs);
2266}
2267
2268
2269#define MAX_WFD_SD_SUBELEMS 20
2270
2271static void wfd_add_sd_req_role(struct wpabuf *tlvs, u8 id, u8 role,
2272 const char *subelems)
2273{
2274 u8 *len;
2275 const char *pos;
2276 int val;
2277 int count = 0;
2278
2279 len = wpabuf_put(tlvs, 2);
2280 wpabuf_put_u8(tlvs, P2P_SERV_WIFI_DISPLAY); /* Service Protocol Type */
2281 wpabuf_put_u8(tlvs, id); /* Service Transaction ID */
2282
2283 wpabuf_put_u8(tlvs, role);
2284
2285 pos = subelems;
2286 while (*pos) {
2287 val = atoi(pos);
2288 if (val >= 0 && val < 256) {
2289 wpabuf_put_u8(tlvs, val);
2290 count++;
2291 if (count == MAX_WFD_SD_SUBELEMS)
2292 break;
2293 }
2294 pos = os_strchr(pos + 1, ',');
2295 if (pos == NULL)
2296 break;
2297 pos++;
2298 }
2299
2300 WPA_PUT_LE16(len, (u8 *) wpabuf_put(tlvs, 0) - len - 2);
2301}
2302
2303
2304u64 wpas_p2p_sd_request_wifi_display(struct wpa_supplicant *wpa_s,
2305 const u8 *dst, const char *role)
2306{
2307 struct wpabuf *tlvs;
2308 u64 ret;
2309 const char *subelems;
2310 u8 id = 1;
2311
2312 subelems = os_strchr(role, ' ');
2313 if (subelems == NULL)
2314 return 0;
2315 subelems++;
2316
2317 tlvs = wpabuf_alloc(4 * (2 + 1 + 1 + 1 + MAX_WFD_SD_SUBELEMS));
2318 if (tlvs == NULL)
2319 return 0;
2320
2321 if (os_strstr(role, "[source]"))
2322 wfd_add_sd_req_role(tlvs, id++, 0x00, subelems);
2323 if (os_strstr(role, "[pri-sink]"))
2324 wfd_add_sd_req_role(tlvs, id++, 0x01, subelems);
2325 if (os_strstr(role, "[sec-sink]"))
2326 wfd_add_sd_req_role(tlvs, id++, 0x02, subelems);
2327 if (os_strstr(role, "[source+sink]"))
2328 wfd_add_sd_req_role(tlvs, id++, 0x03, subelems);
2329
2330 ret = wpas_p2p_sd_request_wfd(wpa_s, dst, tlvs);
2331 wpabuf_free(tlvs);
2332 return ret;
2333}
2334
2335#endif /* CONFIG_WIFI_DISPLAY */
2336
2337
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002338int wpas_p2p_sd_cancel_request(struct wpa_supplicant *wpa_s, u64 req)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002339{
2340 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002341 return wpa_drv_p2p_sd_cancel_request(wpa_s, req);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002342 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
2343 return -1;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002344 return p2p_sd_cancel_request(wpa_s->global->p2p,
2345 (void *) (uintptr_t) req);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002346}
2347
2348
2349void wpas_p2p_sd_response(struct wpa_supplicant *wpa_s, int freq,
2350 const u8 *dst, u8 dialog_token,
2351 const struct wpabuf *resp_tlvs)
2352{
2353 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT) {
2354 wpa_drv_p2p_sd_response(wpa_s, freq, dst, dialog_token,
2355 resp_tlvs);
2356 return;
2357 }
2358 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
2359 return;
2360 p2p_sd_response(wpa_s->global->p2p, freq, dst, dialog_token,
2361 resp_tlvs);
2362}
2363
Dmitry Shmidteaf261d2013-08-14 15:30:08 -07002364
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002365void wpas_p2p_sd_service_update(struct wpa_supplicant *wpa_s)
2366{
2367 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT) {
2368 wpa_drv_p2p_service_update(wpa_s);
2369 return;
2370 }
2371 if (wpa_s->global->p2p)
2372 p2p_sd_service_update(wpa_s->global->p2p);
2373}
2374
2375
2376static void wpas_p2p_srv_bonjour_free(struct p2p_srv_bonjour *bsrv)
2377{
2378 dl_list_del(&bsrv->list);
2379 wpabuf_free(bsrv->query);
2380 wpabuf_free(bsrv->resp);
2381 os_free(bsrv);
2382}
2383
2384
2385static void wpas_p2p_srv_upnp_free(struct p2p_srv_upnp *usrv)
2386{
2387 dl_list_del(&usrv->list);
2388 os_free(usrv->service);
2389 os_free(usrv);
2390}
2391
2392
2393void wpas_p2p_service_flush(struct wpa_supplicant *wpa_s)
2394{
2395 struct p2p_srv_bonjour *bsrv, *bn;
2396 struct p2p_srv_upnp *usrv, *un;
2397
2398 dl_list_for_each_safe(bsrv, bn, &wpa_s->global->p2p_srv_bonjour,
2399 struct p2p_srv_bonjour, list)
2400 wpas_p2p_srv_bonjour_free(bsrv);
2401
2402 dl_list_for_each_safe(usrv, un, &wpa_s->global->p2p_srv_upnp,
2403 struct p2p_srv_upnp, list)
2404 wpas_p2p_srv_upnp_free(usrv);
2405
2406 wpas_p2p_sd_service_update(wpa_s);
2407}
2408
2409
2410int wpas_p2p_service_add_bonjour(struct wpa_supplicant *wpa_s,
2411 struct wpabuf *query, struct wpabuf *resp)
2412{
2413 struct p2p_srv_bonjour *bsrv;
2414
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002415 bsrv = os_zalloc(sizeof(*bsrv));
2416 if (bsrv == NULL)
2417 return -1;
2418 bsrv->query = query;
2419 bsrv->resp = resp;
2420 dl_list_add(&wpa_s->global->p2p_srv_bonjour, &bsrv->list);
2421
2422 wpas_p2p_sd_service_update(wpa_s);
2423 return 0;
2424}
2425
2426
2427int wpas_p2p_service_del_bonjour(struct wpa_supplicant *wpa_s,
2428 const struct wpabuf *query)
2429{
2430 struct p2p_srv_bonjour *bsrv;
2431
2432 bsrv = wpas_p2p_service_get_bonjour(wpa_s, query);
2433 if (bsrv == NULL)
2434 return -1;
2435 wpas_p2p_srv_bonjour_free(bsrv);
2436 wpas_p2p_sd_service_update(wpa_s);
2437 return 0;
2438}
2439
2440
2441int wpas_p2p_service_add_upnp(struct wpa_supplicant *wpa_s, u8 version,
2442 const char *service)
2443{
2444 struct p2p_srv_upnp *usrv;
2445
2446 if (wpas_p2p_service_get_upnp(wpa_s, version, service))
2447 return 0; /* Already listed */
2448 usrv = os_zalloc(sizeof(*usrv));
2449 if (usrv == NULL)
2450 return -1;
2451 usrv->version = version;
2452 usrv->service = os_strdup(service);
2453 if (usrv->service == NULL) {
2454 os_free(usrv);
2455 return -1;
2456 }
2457 dl_list_add(&wpa_s->global->p2p_srv_upnp, &usrv->list);
2458
2459 wpas_p2p_sd_service_update(wpa_s);
2460 return 0;
2461}
2462
2463
2464int wpas_p2p_service_del_upnp(struct wpa_supplicant *wpa_s, u8 version,
2465 const char *service)
2466{
2467 struct p2p_srv_upnp *usrv;
2468
2469 usrv = wpas_p2p_service_get_upnp(wpa_s, version, service);
2470 if (usrv == NULL)
2471 return -1;
2472 wpas_p2p_srv_upnp_free(usrv);
2473 wpas_p2p_sd_service_update(wpa_s);
2474 return 0;
2475}
2476
2477
2478static void wpas_prov_disc_local_display(struct wpa_supplicant *wpa_s,
2479 const u8 *peer, const char *params,
2480 unsigned int generated_pin)
2481{
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07002482 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_SHOW_PIN MACSTR
2483 " %08d%s", MAC2STR(peer), generated_pin, params);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002484}
2485
2486
2487static void wpas_prov_disc_local_keypad(struct wpa_supplicant *wpa_s,
2488 const u8 *peer, const char *params)
2489{
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07002490 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_ENTER_PIN MACSTR
2491 "%s", MAC2STR(peer), params);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002492}
2493
2494
2495void wpas_prov_disc_req(void *ctx, const u8 *peer, u16 config_methods,
2496 const u8 *dev_addr, const u8 *pri_dev_type,
2497 const char *dev_name, u16 supp_config_methods,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002498 u8 dev_capab, u8 group_capab, const u8 *group_id,
2499 size_t group_id_len)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002500{
2501 struct wpa_supplicant *wpa_s = ctx;
2502 char devtype[WPS_DEV_TYPE_BUFSIZE];
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002503 char params[300];
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002504 u8 empty_dev_type[8];
2505 unsigned int generated_pin = 0;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002506 struct wpa_supplicant *group = NULL;
2507
2508 if (group_id) {
2509 for (group = wpa_s->global->ifaces; group; group = group->next)
2510 {
2511 struct wpa_ssid *s = group->current_ssid;
2512 if (s != NULL &&
2513 s->mode == WPAS_MODE_P2P_GO &&
2514 group_id_len - ETH_ALEN == s->ssid_len &&
2515 os_memcmp(group_id + ETH_ALEN, s->ssid,
2516 s->ssid_len) == 0)
2517 break;
2518 }
2519 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002520
2521 if (pri_dev_type == NULL) {
2522 os_memset(empty_dev_type, 0, sizeof(empty_dev_type));
2523 pri_dev_type = empty_dev_type;
2524 }
2525 os_snprintf(params, sizeof(params), " p2p_dev_addr=" MACSTR
2526 " pri_dev_type=%s name='%s' config_methods=0x%x "
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002527 "dev_capab=0x%x group_capab=0x%x%s%s",
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002528 MAC2STR(dev_addr),
2529 wps_dev_type_bin2str(pri_dev_type, devtype,
2530 sizeof(devtype)),
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002531 dev_name, supp_config_methods, dev_capab, group_capab,
2532 group ? " group=" : "",
2533 group ? group->ifname : "");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002534 params[sizeof(params) - 1] = '\0';
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08002535
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002536 if (config_methods & WPS_CONFIG_DISPLAY) {
2537 generated_pin = wps_generate_pin();
2538 wpas_prov_disc_local_display(wpa_s, peer, params,
2539 generated_pin);
2540 } else if (config_methods & WPS_CONFIG_KEYPAD)
2541 wpas_prov_disc_local_keypad(wpa_s, peer, params);
2542 else if (config_methods & WPS_CONFIG_PUSHBUTTON)
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07002543 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_PBC_REQ
2544 MACSTR "%s", MAC2STR(peer), params);
Jouni Malinen75ecf522011-06-27 15:19:46 -07002545
2546 wpas_notify_p2p_provision_discovery(wpa_s, peer, 1 /* request */,
2547 P2P_PROV_DISC_SUCCESS,
2548 config_methods, generated_pin);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002549}
2550
2551
2552void wpas_prov_disc_resp(void *ctx, const u8 *peer, u16 config_methods)
2553{
2554 struct wpa_supplicant *wpa_s = ctx;
2555 unsigned int generated_pin = 0;
Dmitry Shmidt04949592012-07-19 12:16:46 -07002556 char params[20];
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002557
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08002558 if (wpa_s->pending_pd_before_join &&
2559 (os_memcmp(peer, wpa_s->pending_join_dev_addr, ETH_ALEN) == 0 ||
2560 os_memcmp(peer, wpa_s->pending_join_iface_addr, ETH_ALEN) == 0)) {
2561 wpa_s->pending_pd_before_join = 0;
2562 wpa_printf(MSG_DEBUG, "P2P: Starting pending "
2563 "join-existing-group operation");
2564 wpas_p2p_join_start(wpa_s);
2565 return;
2566 }
2567
Dmitry Shmidt04949592012-07-19 12:16:46 -07002568 if (wpa_s->pending_pd_use == AUTO_PD_JOIN ||
2569 wpa_s->pending_pd_use == AUTO_PD_GO_NEG)
2570 os_snprintf(params, sizeof(params), " peer_go=%d",
2571 wpa_s->pending_pd_use == AUTO_PD_JOIN);
2572 else
2573 params[0] = '\0';
2574
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002575 if (config_methods & WPS_CONFIG_DISPLAY)
Dmitry Shmidt04949592012-07-19 12:16:46 -07002576 wpas_prov_disc_local_keypad(wpa_s, peer, params);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002577 else if (config_methods & WPS_CONFIG_KEYPAD) {
2578 generated_pin = wps_generate_pin();
Dmitry Shmidt04949592012-07-19 12:16:46 -07002579 wpas_prov_disc_local_display(wpa_s, peer, params,
2580 generated_pin);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002581 } else if (config_methods & WPS_CONFIG_PUSHBUTTON)
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07002582 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_PBC_RESP
2583 MACSTR "%s", MAC2STR(peer), params);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002584
Jouni Malinen75ecf522011-06-27 15:19:46 -07002585 wpas_notify_p2p_provision_discovery(wpa_s, peer, 0 /* response */,
2586 P2P_PROV_DISC_SUCCESS,
2587 config_methods, generated_pin);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002588}
2589
2590
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002591static void wpas_prov_disc_fail(void *ctx, const u8 *peer,
2592 enum p2p_prov_disc_status status)
Jouni Malinen75ecf522011-06-27 15:19:46 -07002593{
2594 struct wpa_supplicant *wpa_s = ctx;
2595
Dmitry Shmidt04949592012-07-19 12:16:46 -07002596 if (wpa_s->p2p_fallback_to_go_neg) {
2597 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: PD for p2p_connect-auto "
2598 "failed - fall back to GO Negotiation");
2599 wpas_p2p_fallback_to_go_neg(wpa_s, 0);
2600 return;
2601 }
2602
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002603 if (status == P2P_PROV_DISC_TIMEOUT_JOIN) {
Dmitry Shmidt2b380482012-09-13 10:52:13 -07002604 wpa_s->pending_pd_before_join = 0;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002605 wpa_printf(MSG_DEBUG, "P2P: Starting pending "
2606 "join-existing-group operation (no ACK for PD "
2607 "Req attempts)");
2608 wpas_p2p_join_start(wpa_s);
2609 return;
Dmitry Shmidt2b380482012-09-13 10:52:13 -07002610 }
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002611
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07002612 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_FAILURE
2613 " p2p_dev_addr=" MACSTR " status=%d",
2614 MAC2STR(peer), status);
Dmitry Shmidt04949592012-07-19 12:16:46 -07002615
Jouni Malinen75ecf522011-06-27 15:19:46 -07002616 wpas_notify_p2p_provision_discovery(wpa_s, peer, 0 /* response */,
2617 status, 0, 0);
2618}
2619
2620
Dmitry Shmidtf7e0a992013-05-23 11:03:10 -07002621static int freq_included(const struct p2p_channels *channels, unsigned int freq)
2622{
2623 if (channels == NULL)
2624 return 1; /* Assume no restrictions */
2625 return p2p_channels_includes_freq(channels, freq);
2626
2627}
2628
2629
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002630static u8 wpas_invitation_process(void *ctx, const u8 *sa, const u8 *bssid,
2631 const u8 *go_dev_addr, const u8 *ssid,
2632 size_t ssid_len, int *go, u8 *group_bssid,
Dmitry Shmidtf7e0a992013-05-23 11:03:10 -07002633 int *force_freq, int persistent_group,
2634 const struct p2p_channels *channels)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002635{
2636 struct wpa_supplicant *wpa_s = ctx;
2637 struct wpa_ssid *s;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002638 int res;
2639 struct wpa_supplicant *grp;
2640
2641 if (!persistent_group) {
2642 wpa_printf(MSG_DEBUG, "P2P: Invitation from " MACSTR
2643 " to join an active group", MAC2STR(sa));
2644 if (!is_zero_ether_addr(wpa_s->p2p_auth_invite) &&
2645 (os_memcmp(go_dev_addr, wpa_s->p2p_auth_invite, ETH_ALEN)
2646 == 0 ||
2647 os_memcmp(sa, wpa_s->p2p_auth_invite, ETH_ALEN) == 0)) {
2648 wpa_printf(MSG_DEBUG, "P2P: Accept previously "
2649 "authorized invitation");
2650 goto accept_inv;
2651 }
2652 /*
2653 * Do not accept the invitation automatically; notify user and
2654 * request approval.
2655 */
2656 return P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE;
2657 }
2658
2659 grp = wpas_get_p2p_group(wpa_s, ssid, ssid_len, go);
2660 if (grp) {
2661 wpa_printf(MSG_DEBUG, "P2P: Accept invitation to already "
2662 "running persistent group");
2663 if (*go)
2664 os_memcpy(group_bssid, grp->own_addr, ETH_ALEN);
2665 goto accept_inv;
2666 }
2667
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08002668 if (!is_zero_ether_addr(wpa_s->p2p_auth_invite) &&
2669 os_memcmp(sa, wpa_s->p2p_auth_invite, ETH_ALEN) == 0) {
2670 wpa_printf(MSG_DEBUG, "P2P: Accept previously initiated "
2671 "invitation to re-invoke a persistent group");
2672 } else if (!wpa_s->conf->persistent_reconnect)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002673 return P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE;
2674
2675 for (s = wpa_s->conf->ssid; s; s = s->next) {
2676 if (s->disabled == 2 &&
2677 os_memcmp(s->bssid, go_dev_addr, ETH_ALEN) == 0 &&
2678 s->ssid_len == ssid_len &&
2679 os_memcmp(ssid, s->ssid, ssid_len) == 0)
2680 break;
2681 }
2682
2683 if (!s) {
2684 wpa_printf(MSG_DEBUG, "P2P: Invitation from " MACSTR
2685 " requested reinvocation of an unknown group",
2686 MAC2STR(sa));
2687 return P2P_SC_FAIL_UNKNOWN_GROUP;
2688 }
2689
2690 if (s->mode == WPAS_MODE_P2P_GO && !wpas_p2p_create_iface(wpa_s)) {
2691 *go = 1;
2692 if (wpa_s->wpa_state >= WPA_AUTHENTICATING) {
2693 wpa_printf(MSG_DEBUG, "P2P: The only available "
2694 "interface is already in use - reject "
2695 "invitation");
2696 return P2P_SC_FAIL_UNABLE_TO_ACCOMMODATE;
2697 }
2698 os_memcpy(group_bssid, wpa_s->own_addr, ETH_ALEN);
2699 } else if (s->mode == WPAS_MODE_P2P_GO) {
2700 *go = 1;
2701 if (wpas_p2p_add_group_interface(wpa_s, WPA_IF_P2P_GO) < 0)
2702 {
2703 wpa_printf(MSG_ERROR, "P2P: Failed to allocate a new "
2704 "interface address for the group");
2705 return P2P_SC_FAIL_UNABLE_TO_ACCOMMODATE;
2706 }
2707 os_memcpy(group_bssid, wpa_s->pending_interface_addr,
2708 ETH_ALEN);
2709 }
2710
2711accept_inv:
Dmitry Shmidt700a1372013-03-15 14:14:44 -07002712 wpas_p2p_set_own_freq_preference(wpa_s, 0);
2713
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07002714 /* Get one of the frequencies currently in use */
2715 if (wpas_p2p_valid_oper_freqs(wpa_s, &res, 1) > 0) {
2716 wpa_printf(MSG_DEBUG, "P2P: Trying to force channel to match a channel already used by one of the interfaces");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002717 *force_freq = res;
Dmitry Shmidt700a1372013-03-15 14:14:44 -07002718 wpas_p2p_set_own_freq_preference(wpa_s, res);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002719 }
2720
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07002721 if (*force_freq > 0 && wpa_s->num_multichan_concurrent > 1 &&
2722 wpas_p2p_num_unused_channels(wpa_s) > 0) {
Dmitry Shmidtf7e0a992013-05-23 11:03:10 -07002723 if (*go == 0) {
2724 /* We are the client */
2725 wpa_printf(MSG_DEBUG, "P2P: Peer was found to be "
2726 "running a GO but we are capable of MCC, "
2727 "figure out the best channel to use");
2728 *force_freq = 0;
2729 } else if (!freq_included(channels, *force_freq)) {
2730 /* We are the GO, and *force_freq is not in the
2731 * intersection */
2732 wpa_printf(MSG_DEBUG, "P2P: Forced GO freq %d MHz not "
2733 "in intersection but we are capable of MCC, "
2734 "figure out the best channel to use",
2735 *force_freq);
2736 *force_freq = 0;
2737 }
2738 }
2739
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002740 return P2P_SC_SUCCESS;
2741}
2742
2743
2744static void wpas_invitation_received(void *ctx, const u8 *sa, const u8 *bssid,
2745 const u8 *ssid, size_t ssid_len,
2746 const u8 *go_dev_addr, u8 status,
2747 int op_freq)
2748{
2749 struct wpa_supplicant *wpa_s = ctx;
2750 struct wpa_ssid *s;
2751
2752 for (s = wpa_s->conf->ssid; s; s = s->next) {
2753 if (s->disabled == 2 &&
2754 s->ssid_len == ssid_len &&
2755 os_memcmp(ssid, s->ssid, ssid_len) == 0)
2756 break;
2757 }
2758
2759 if (status == P2P_SC_SUCCESS) {
2760 wpa_printf(MSG_DEBUG, "P2P: Invitation from peer " MACSTR
2761 " was accepted; op_freq=%d MHz",
2762 MAC2STR(sa), op_freq);
2763 if (s) {
Dmitry Shmidt91c40cd2012-09-25 14:23:53 -07002764 int go = s->mode == WPAS_MODE_P2P_GO;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002765 wpas_p2p_group_add_persistent(
Dmitry Shmidt56052862013-10-04 10:23:25 -07002766 wpa_s, s, go, go ? op_freq : 0, 0, NULL,
2767 go ? P2P_MAX_INITIAL_CONN_WAIT_GO_REINVOKE : 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002768 } else if (bssid) {
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002769 wpa_s->user_initiated_pd = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002770 wpas_p2p_join(wpa_s, bssid, go_dev_addr,
Dmitry Shmidt04949592012-07-19 12:16:46 -07002771 wpa_s->p2p_wps_method, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002772 }
2773 return;
2774 }
2775
2776 if (status != P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE) {
2777 wpa_printf(MSG_DEBUG, "P2P: Invitation from peer " MACSTR
2778 " was rejected (status %u)", MAC2STR(sa), status);
2779 return;
2780 }
2781
2782 if (!s) {
2783 if (bssid) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07002784 wpa_msg_global(wpa_s, MSG_INFO,
2785 P2P_EVENT_INVITATION_RECEIVED
2786 "sa=" MACSTR " go_dev_addr=" MACSTR
2787 " bssid=" MACSTR " unknown-network",
2788 MAC2STR(sa), MAC2STR(go_dev_addr),
2789 MAC2STR(bssid));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002790 } else {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07002791 wpa_msg_global(wpa_s, MSG_INFO,
2792 P2P_EVENT_INVITATION_RECEIVED
2793 "sa=" MACSTR " go_dev_addr=" MACSTR
2794 " unknown-network",
2795 MAC2STR(sa), MAC2STR(go_dev_addr));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002796 }
2797 return;
2798 }
2799
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08002800 if (s->mode == WPAS_MODE_P2P_GO && op_freq) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07002801 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_INVITATION_RECEIVED
2802 "sa=" MACSTR " persistent=%d freq=%d",
2803 MAC2STR(sa), s->id, op_freq);
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08002804 } else {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07002805 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_INVITATION_RECEIVED
2806 "sa=" MACSTR " persistent=%d",
2807 MAC2STR(sa), s->id);
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08002808 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002809}
2810
2811
Dmitry Shmidt700a1372013-03-15 14:14:44 -07002812static void wpas_remove_persistent_peer(struct wpa_supplicant *wpa_s,
2813 struct wpa_ssid *ssid,
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07002814 const u8 *peer, int inv)
Dmitry Shmidt700a1372013-03-15 14:14:44 -07002815{
2816 size_t i;
2817
2818 if (ssid == NULL)
2819 return;
2820
2821 for (i = 0; ssid->p2p_client_list && i < ssid->num_p2p_clients; i++) {
2822 if (os_memcmp(ssid->p2p_client_list + i * ETH_ALEN, peer,
2823 ETH_ALEN) == 0)
2824 break;
2825 }
2826 if (i >= ssid->num_p2p_clients) {
2827 if (ssid->mode != WPAS_MODE_P2P_GO &&
2828 os_memcmp(ssid->bssid, peer, ETH_ALEN) == 0) {
2829 wpa_printf(MSG_DEBUG, "P2P: Remove persistent group %d "
2830 "due to invitation result", ssid->id);
2831 wpas_notify_network_removed(wpa_s, ssid);
2832 wpa_config_remove_network(wpa_s->conf, ssid->id);
2833 return;
2834 }
2835 return; /* Peer not found in client list */
2836 }
2837
2838 wpa_printf(MSG_DEBUG, "P2P: Remove peer " MACSTR " from persistent "
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07002839 "group %d client list%s",
2840 MAC2STR(peer), ssid->id,
2841 inv ? " due to invitation result" : "");
Dmitry Shmidt700a1372013-03-15 14:14:44 -07002842 os_memmove(ssid->p2p_client_list + i * ETH_ALEN,
2843 ssid->p2p_client_list + (i + 1) * ETH_ALEN,
2844 (ssid->num_p2p_clients - i - 1) * ETH_ALEN);
2845 ssid->num_p2p_clients--;
2846#ifndef CONFIG_NO_CONFIG_WRITE
2847 if (wpa_s->parent->conf->update_config &&
2848 wpa_config_write(wpa_s->parent->confname, wpa_s->parent->conf))
2849 wpa_printf(MSG_DEBUG, "P2P: Failed to update configuration");
2850#endif /* CONFIG_NO_CONFIG_WRITE */
2851}
2852
2853
2854static void wpas_remove_persistent_client(struct wpa_supplicant *wpa_s,
2855 const u8 *peer)
2856{
2857 struct wpa_ssid *ssid;
2858
2859 wpa_s = wpa_s->global->p2p_invite_group;
2860 if (wpa_s == NULL)
2861 return; /* No known invitation group */
2862 ssid = wpa_s->current_ssid;
2863 if (ssid == NULL || ssid->mode != WPAS_MODE_P2P_GO ||
2864 !ssid->p2p_persistent_group)
2865 return; /* Not operating as a GO in persistent group */
2866 ssid = wpas_p2p_get_persistent(wpa_s->parent, peer,
2867 ssid->ssid, ssid->ssid_len);
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07002868 wpas_remove_persistent_peer(wpa_s, ssid, peer, 1);
Dmitry Shmidt700a1372013-03-15 14:14:44 -07002869}
2870
2871
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08002872static void wpas_invitation_result(void *ctx, int status, const u8 *bssid,
Dmitry Shmidt700a1372013-03-15 14:14:44 -07002873 const struct p2p_channels *channels,
2874 const u8 *peer)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002875{
2876 struct wpa_supplicant *wpa_s = ctx;
2877 struct wpa_ssid *ssid;
2878
2879 if (bssid) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07002880 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_INVITATION_RESULT
2881 "status=%d " MACSTR,
2882 status, MAC2STR(bssid));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002883 } else {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07002884 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_INVITATION_RESULT
2885 "status=%d ", status);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002886 }
2887 wpas_notify_p2p_invitation_result(wpa_s, status, bssid);
2888
Dmitry Shmidt700a1372013-03-15 14:14:44 -07002889 wpa_printf(MSG_DEBUG, "P2P: Invitation result - status=%d peer=" MACSTR,
2890 status, MAC2STR(peer));
2891 if (wpa_s->pending_invite_ssid_id == -1) {
2892 if (status == P2P_SC_FAIL_UNKNOWN_GROUP)
2893 wpas_remove_persistent_client(wpa_s, peer);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002894 return; /* Invitation to active group */
Dmitry Shmidt700a1372013-03-15 14:14:44 -07002895 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002896
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08002897 if (status == P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE) {
2898 wpa_printf(MSG_DEBUG, "P2P: Waiting for peer to start another "
2899 "invitation exchange to indicate readiness for "
2900 "re-invocation");
2901 }
2902
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002903 if (status != P2P_SC_SUCCESS) {
Dmitry Shmidt700a1372013-03-15 14:14:44 -07002904 if (status == P2P_SC_FAIL_UNKNOWN_GROUP) {
2905 ssid = wpa_config_get_network(
2906 wpa_s->conf, wpa_s->pending_invite_ssid_id);
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07002907 wpas_remove_persistent_peer(wpa_s, ssid, peer, 1);
Dmitry Shmidt700a1372013-03-15 14:14:44 -07002908 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002909 wpas_p2p_remove_pending_group_interface(wpa_s);
2910 return;
2911 }
2912
2913 ssid = wpa_config_get_network(wpa_s->conf,
2914 wpa_s->pending_invite_ssid_id);
2915 if (ssid == NULL) {
2916 wpa_printf(MSG_ERROR, "P2P: Could not find persistent group "
2917 "data matching with invitation");
2918 return;
2919 }
2920
Jouni Malinen2c5b17d2012-10-13 21:52:46 -07002921 /*
2922 * The peer could have missed our ctrl::ack frame for Invitation
2923 * Response and continue retransmitting the frame. To reduce the
2924 * likelihood of the peer not getting successful TX status for the
2925 * Invitation Response frame, wait a short time here before starting
2926 * the persistent group so that we will remain on the current channel to
2927 * acknowledge any possible retransmission from the peer.
2928 */
Irfan Sheriff81931b82012-10-16 21:40:46 -07002929#ifndef ANDROID_P2P
Jouni Malinen2c5b17d2012-10-13 21:52:46 -07002930 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: 50 ms wait on current channel before "
2931 "starting persistent group");
2932 os_sleep(0, 50000);
Irfan Sheriff81931b82012-10-16 21:40:46 -07002933#else
2934 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: 100 ms wait on current channel before "
2935 "starting persistent group");
2936 os_sleep(0, 100000);
2937#endif
Jouni Malinen2c5b17d2012-10-13 21:52:46 -07002938
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002939 wpas_p2p_group_add_persistent(wpa_s, ssid,
Jouni Malinen31be0a42012-08-31 21:20:51 +03002940 ssid->mode == WPAS_MODE_P2P_GO,
2941 wpa_s->p2p_persistent_go_freq,
Dmitry Shmidt56052862013-10-04 10:23:25 -07002942 wpa_s->p2p_go_ht40, channels,
2943 ssid->mode == WPAS_MODE_P2P_GO ?
2944 P2P_MAX_INITIAL_CONN_WAIT_GO_REINVOKE :
2945 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002946}
2947
2948
Dmitry Shmidt04949592012-07-19 12:16:46 -07002949static int wpas_p2p_disallowed_freq(struct wpa_global *global,
2950 unsigned int freq)
2951{
2952 unsigned int i;
2953
2954 if (global->p2p_disallow_freq == NULL)
2955 return 0;
2956
2957 for (i = 0; i < global->num_p2p_disallow_freq; i++) {
2958 if (freq >= global->p2p_disallow_freq[i].min &&
2959 freq <= global->p2p_disallow_freq[i].max)
2960 return 1;
2961 }
2962
2963 return 0;
2964}
2965
2966
2967static void wpas_p2p_add_chan(struct p2p_reg_class *reg, u8 chan)
2968{
2969 reg->channel[reg->channels] = chan;
2970 reg->channels++;
2971}
2972
2973
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002974static int wpas_p2p_default_channels(struct wpa_supplicant *wpa_s,
2975 struct p2p_channels *chan)
2976{
2977 int i, cla = 0;
2978
2979 wpa_printf(MSG_DEBUG, "P2P: Enable operating classes for 2.4 GHz "
2980 "band");
2981
2982 /* Operating class 81 - 2.4 GHz band channels 1..13 */
2983 chan->reg_class[cla].reg_class = 81;
Dmitry Shmidt04949592012-07-19 12:16:46 -07002984 chan->reg_class[cla].channels = 0;
2985 for (i = 0; i < 11; i++) {
2986 if (!wpas_p2p_disallowed_freq(wpa_s->global, 2412 + i * 5))
2987 wpas_p2p_add_chan(&chan->reg_class[cla], i + 1);
2988 }
2989 if (chan->reg_class[cla].channels)
2990 cla++;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002991
2992 wpa_printf(MSG_DEBUG, "P2P: Enable operating classes for lower 5 GHz "
2993 "band");
2994
2995 /* Operating class 115 - 5 GHz, channels 36-48 */
2996 chan->reg_class[cla].reg_class = 115;
Dmitry Shmidt04949592012-07-19 12:16:46 -07002997 chan->reg_class[cla].channels = 0;
2998 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 36 * 5))
2999 wpas_p2p_add_chan(&chan->reg_class[cla], 36);
3000 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 40 * 5))
3001 wpas_p2p_add_chan(&chan->reg_class[cla], 40);
3002 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 44 * 5))
3003 wpas_p2p_add_chan(&chan->reg_class[cla], 44);
3004 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 48 * 5))
3005 wpas_p2p_add_chan(&chan->reg_class[cla], 48);
3006 if (chan->reg_class[cla].channels)
3007 cla++;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003008
3009 wpa_printf(MSG_DEBUG, "P2P: Enable operating classes for higher 5 GHz "
3010 "band");
3011
3012 /* Operating class 124 - 5 GHz, channels 149,153,157,161 */
3013 chan->reg_class[cla].reg_class = 124;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003014 chan->reg_class[cla].channels = 0;
3015 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 149 * 5))
3016 wpas_p2p_add_chan(&chan->reg_class[cla], 149);
3017 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 153 * 5))
3018 wpas_p2p_add_chan(&chan->reg_class[cla], 153);
3019 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 156 * 5))
3020 wpas_p2p_add_chan(&chan->reg_class[cla], 157);
3021 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 161 * 5))
3022 wpas_p2p_add_chan(&chan->reg_class[cla], 161);
3023 if (chan->reg_class[cla].channels)
3024 cla++;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003025
3026 chan->reg_classes = cla;
3027 return 0;
3028}
3029
3030
3031static struct hostapd_hw_modes * get_mode(struct hostapd_hw_modes *modes,
3032 u16 num_modes,
3033 enum hostapd_hw_mode mode)
3034{
3035 u16 i;
3036
3037 for (i = 0; i < num_modes; i++) {
3038 if (modes[i].mode == mode)
3039 return &modes[i];
3040 }
3041
3042 return NULL;
3043}
3044
3045
Dmitry Shmidt04949592012-07-19 12:16:46 -07003046static int has_channel(struct wpa_global *global,
3047 struct hostapd_hw_modes *mode, u8 chan, int *flags)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003048{
3049 int i;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003050 unsigned int freq;
3051
3052 freq = (mode->mode == HOSTAPD_MODE_IEEE80211A ? 5000 : 2407) +
3053 chan * 5;
3054 if (wpas_p2p_disallowed_freq(global, freq))
3055 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003056
3057 for (i = 0; i < mode->num_channels; i++) {
3058 if (mode->channels[i].chan == chan) {
3059 if (flags)
3060 *flags = mode->channels[i].flag;
3061 return !(mode->channels[i].flag &
3062 (HOSTAPD_CHAN_DISABLED |
3063 HOSTAPD_CHAN_PASSIVE_SCAN |
3064 HOSTAPD_CHAN_NO_IBSS |
3065 HOSTAPD_CHAN_RADAR));
3066 }
3067 }
3068
3069 return 0;
3070}
3071
3072
3073struct p2p_oper_class_map {
3074 enum hostapd_hw_mode mode;
3075 u8 op_class;
3076 u8 min_chan;
3077 u8 max_chan;
3078 u8 inc;
3079 enum { BW20, BW40PLUS, BW40MINUS } bw;
3080};
3081
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003082static struct p2p_oper_class_map op_class[] = {
3083 { HOSTAPD_MODE_IEEE80211G, 81, 1, 13, 1, BW20 },
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003084#if 0 /* Do not enable HT40 on 2 GHz for now */
3085 { HOSTAPD_MODE_IEEE80211G, 83, 1, 9, 1, BW40PLUS },
3086 { HOSTAPD_MODE_IEEE80211G, 84, 5, 13, 1, BW40MINUS },
3087#endif
3088 { HOSTAPD_MODE_IEEE80211A, 115, 36, 48, 4, BW20 },
3089 { HOSTAPD_MODE_IEEE80211A, 124, 149, 161, 4, BW20 },
3090 { HOSTAPD_MODE_IEEE80211A, 116, 36, 44, 8, BW40PLUS },
3091 { HOSTAPD_MODE_IEEE80211A, 117, 40, 48, 8, BW40MINUS },
3092 { HOSTAPD_MODE_IEEE80211A, 126, 149, 157, 8, BW40PLUS },
3093 { HOSTAPD_MODE_IEEE80211A, 127, 153, 161, 8, BW40MINUS },
3094 { -1, 0, 0, 0, 0, BW20 }
3095};
3096
3097
3098static int wpas_p2p_verify_channel(struct wpa_supplicant *wpa_s,
3099 struct hostapd_hw_modes *mode,
3100 u8 channel, u8 bw)
3101{
3102 int flag;
3103
3104 if (!has_channel(wpa_s->global, mode, channel, &flag))
3105 return -1;
3106 if (bw == BW40MINUS &&
3107 (!(flag & HOSTAPD_CHAN_HT40MINUS) ||
3108 !has_channel(wpa_s->global, mode, channel - 4, NULL)))
3109 return 0;
3110 if (bw == BW40PLUS &&
3111 (!(flag & HOSTAPD_CHAN_HT40PLUS) ||
3112 !has_channel(wpa_s->global, mode, channel + 4, NULL)))
3113 return 0;
3114 return 1;
3115}
3116
3117
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003118static int wpas_p2p_setup_channels(struct wpa_supplicant *wpa_s,
3119 struct p2p_channels *chan)
3120{
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003121 struct hostapd_hw_modes *mode;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003122 int cla, op;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003123
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003124 if (wpa_s->hw.modes == NULL) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003125 wpa_printf(MSG_DEBUG, "P2P: Driver did not support fetching "
3126 "of all supported channels; assume dualband "
3127 "support");
3128 return wpas_p2p_default_channels(wpa_s, chan);
3129 }
3130
3131 cla = 0;
3132
3133 for (op = 0; op_class[op].op_class; op++) {
3134 struct p2p_oper_class_map *o = &op_class[op];
3135 u8 ch;
3136 struct p2p_reg_class *reg = NULL;
3137
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003138 mode = get_mode(wpa_s->hw.modes, wpa_s->hw.num_modes, o->mode);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003139 if (mode == NULL)
3140 continue;
3141 for (ch = o->min_chan; ch <= o->max_chan; ch += o->inc) {
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003142 if (wpas_p2p_verify_channel(wpa_s, mode, ch, o->bw) < 1)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003143 continue;
3144 if (reg == NULL) {
3145 wpa_printf(MSG_DEBUG, "P2P: Add operating "
3146 "class %u", o->op_class);
3147 reg = &chan->reg_class[cla];
3148 cla++;
3149 reg->reg_class = o->op_class;
3150 }
3151 reg->channel[reg->channels] = ch;
3152 reg->channels++;
3153 }
3154 if (reg) {
3155 wpa_hexdump(MSG_DEBUG, "P2P: Channels",
3156 reg->channel, reg->channels);
3157 }
3158 }
3159
3160 chan->reg_classes = cla;
3161
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003162 return 0;
3163}
3164
3165
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003166int wpas_p2p_get_ht40_mode(struct wpa_supplicant *wpa_s,
3167 struct hostapd_hw_modes *mode, u8 channel)
3168{
3169 int op, ret;
3170
3171 for (op = 0; op_class[op].op_class; op++) {
3172 struct p2p_oper_class_map *o = &op_class[op];
3173 u8 ch;
3174
3175 for (ch = o->min_chan; ch <= o->max_chan; ch += o->inc) {
3176 if (o->mode != HOSTAPD_MODE_IEEE80211A ||
3177 o->bw == BW20 || ch != channel)
3178 continue;
3179 ret = wpas_p2p_verify_channel(wpa_s, mode, ch, o->bw);
3180 if (ret < 0)
3181 continue;
3182 else if (ret > 0)
3183 return (o->bw == BW40MINUS) ? -1 : 1;
3184 else
3185 return 0;
3186 }
3187 }
3188 return 0;
3189}
3190
3191
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003192static int wpas_get_noa(void *ctx, const u8 *interface_addr, u8 *buf,
3193 size_t buf_len)
3194{
3195 struct wpa_supplicant *wpa_s = ctx;
3196
3197 for (wpa_s = wpa_s->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
3198 if (os_memcmp(wpa_s->own_addr, interface_addr, ETH_ALEN) == 0)
3199 break;
3200 }
3201 if (wpa_s == NULL)
3202 return -1;
3203
3204 return wpa_drv_get_noa(wpa_s, buf, buf_len);
3205}
3206
3207
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08003208static int wpas_go_connected(void *ctx, const u8 *dev_addr)
3209{
3210 struct wpa_supplicant *wpa_s = ctx;
3211
3212 for (wpa_s = wpa_s->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
3213 struct wpa_ssid *ssid = wpa_s->current_ssid;
3214 if (ssid == NULL)
3215 continue;
3216 if (ssid->mode != WPAS_MODE_INFRA)
3217 continue;
3218 if (wpa_s->wpa_state != WPA_COMPLETED &&
3219 wpa_s->wpa_state != WPA_GROUP_HANDSHAKE)
3220 continue;
3221 if (os_memcmp(wpa_s->go_dev_addr, dev_addr, ETH_ALEN) == 0)
3222 return 1;
3223 }
3224
3225 return 0;
3226}
3227
3228
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07003229static void wpas_p2p_debug_print(void *ctx, int level, const char *msg)
3230{
3231 struct wpa_supplicant *wpa_s = ctx;
3232 wpa_msg_global(wpa_s, level, "P2P: %s", msg);
3233}
3234
3235
Dmitry Shmidt34af3062013-07-11 10:46:32 -07003236int wpas_p2p_add_p2pdev_interface(struct wpa_supplicant *wpa_s)
3237{
3238 struct wpa_interface iface;
3239 struct wpa_supplicant *p2pdev_wpa_s;
3240 char ifname[100];
3241 char force_name[100];
3242 int ret;
3243
3244 os_snprintf(ifname, sizeof(ifname), P2P_MGMT_DEVICE_PREFIX "%s",
3245 wpa_s->ifname);
3246 force_name[0] = '\0';
3247 wpa_s->pending_interface_type = WPA_IF_P2P_DEVICE;
3248 ret = wpa_drv_if_add(wpa_s, WPA_IF_P2P_DEVICE, ifname, NULL, NULL,
3249 force_name, wpa_s->pending_interface_addr, NULL);
3250 if (ret < 0) {
3251 wpa_printf(MSG_DEBUG, "P2P: Failed to create P2P Device interface");
3252 return ret;
3253 }
3254 os_strlcpy(wpa_s->pending_interface_name, ifname,
3255 sizeof(wpa_s->pending_interface_name));
3256
3257 os_memset(&iface, 0, sizeof(iface));
3258 iface.p2p_mgmt = 1;
3259 iface.ifname = wpa_s->pending_interface_name;
3260 iface.driver = wpa_s->driver->name;
3261 iface.driver_param = wpa_s->conf->driver_param;
3262 iface.confname = wpa_s->confname;
3263 p2pdev_wpa_s = wpa_supplicant_add_iface(wpa_s->global, &iface);
3264 if (!p2pdev_wpa_s) {
3265 wpa_printf(MSG_DEBUG, "P2P: Failed to add P2P Device interface");
3266 return -1;
3267 }
3268 p2pdev_wpa_s->parent = wpa_s;
3269
3270 wpa_s->pending_interface_name[0] = '\0';
3271 return 0;
3272}
3273
3274
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003275/**
3276 * wpas_p2p_init - Initialize P2P module for %wpa_supplicant
3277 * @global: Pointer to global data from wpa_supplicant_init()
3278 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
3279 * Returns: 0 on success, -1 on failure
3280 */
3281int wpas_p2p_init(struct wpa_global *global, struct wpa_supplicant *wpa_s)
3282{
3283 struct p2p_config p2p;
3284 unsigned int r;
3285 int i;
3286
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07003287 if (wpa_s->conf->p2p_disabled)
3288 return 0;
3289
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003290 if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_CAPABLE))
3291 return 0;
3292
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003293 if (global->p2p)
3294 return 0;
3295
3296 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT) {
3297 struct p2p_params params;
3298
3299 wpa_printf(MSG_DEBUG, "P2P: Use driver-based P2P management");
3300 os_memset(&params, 0, sizeof(params));
3301 params.dev_name = wpa_s->conf->device_name;
3302 os_memcpy(params.pri_dev_type, wpa_s->conf->device_type,
3303 WPS_DEV_TYPE_LEN);
3304 params.num_sec_dev_types = wpa_s->conf->num_sec_device_types;
3305 os_memcpy(params.sec_dev_type,
3306 wpa_s->conf->sec_device_type,
3307 params.num_sec_dev_types * WPS_DEV_TYPE_LEN);
3308
3309 if (wpa_drv_p2p_set_params(wpa_s, &params) < 0)
3310 return -1;
3311
3312 return 0;
3313 }
3314
3315 os_memset(&p2p, 0, sizeof(p2p));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003316 p2p.cb_ctx = wpa_s;
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07003317 p2p.debug_print = wpas_p2p_debug_print;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003318 p2p.p2p_scan = wpas_p2p_scan;
3319 p2p.send_action = wpas_send_action;
3320 p2p.send_action_done = wpas_send_action_done;
3321 p2p.go_neg_completed = wpas_go_neg_completed;
3322 p2p.go_neg_req_rx = wpas_go_neg_req_rx;
3323 p2p.dev_found = wpas_dev_found;
3324 p2p.dev_lost = wpas_dev_lost;
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07003325 p2p.find_stopped = wpas_find_stopped;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003326 p2p.start_listen = wpas_start_listen;
3327 p2p.stop_listen = wpas_stop_listen;
3328 p2p.send_probe_resp = wpas_send_probe_resp;
3329 p2p.sd_request = wpas_sd_request;
3330 p2p.sd_response = wpas_sd_response;
3331 p2p.prov_disc_req = wpas_prov_disc_req;
3332 p2p.prov_disc_resp = wpas_prov_disc_resp;
Jouni Malinen75ecf522011-06-27 15:19:46 -07003333 p2p.prov_disc_fail = wpas_prov_disc_fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003334 p2p.invitation_process = wpas_invitation_process;
3335 p2p.invitation_received = wpas_invitation_received;
3336 p2p.invitation_result = wpas_invitation_result;
3337 p2p.get_noa = wpas_get_noa;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08003338 p2p.go_connected = wpas_go_connected;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003339
3340 os_memcpy(wpa_s->global->p2p_dev_addr, wpa_s->own_addr, ETH_ALEN);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003341 os_memcpy(p2p.dev_addr, wpa_s->global->p2p_dev_addr, ETH_ALEN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003342 p2p.dev_name = wpa_s->conf->device_name;
3343 p2p.manufacturer = wpa_s->conf->manufacturer;
3344 p2p.model_name = wpa_s->conf->model_name;
3345 p2p.model_number = wpa_s->conf->model_number;
3346 p2p.serial_number = wpa_s->conf->serial_number;
3347 if (wpa_s->wps) {
3348 os_memcpy(p2p.uuid, wpa_s->wps->uuid, 16);
3349 p2p.config_methods = wpa_s->wps->config_methods;
3350 }
3351
3352 if (wpa_s->conf->p2p_listen_reg_class &&
3353 wpa_s->conf->p2p_listen_channel) {
3354 p2p.reg_class = wpa_s->conf->p2p_listen_reg_class;
3355 p2p.channel = wpa_s->conf->p2p_listen_channel;
3356 } else {
3357 p2p.reg_class = 81;
3358 /*
3359 * Pick one of the social channels randomly as the listen
3360 * channel.
3361 */
3362 os_get_random((u8 *) &r, sizeof(r));
3363 p2p.channel = 1 + (r % 3) * 5;
3364 }
3365 wpa_printf(MSG_DEBUG, "P2P: Own listen channel: %d", p2p.channel);
3366
3367 if (wpa_s->conf->p2p_oper_reg_class &&
3368 wpa_s->conf->p2p_oper_channel) {
3369 p2p.op_reg_class = wpa_s->conf->p2p_oper_reg_class;
3370 p2p.op_channel = wpa_s->conf->p2p_oper_channel;
3371 p2p.cfg_op_channel = 1;
3372 wpa_printf(MSG_DEBUG, "P2P: Configured operating channel: "
3373 "%d:%d", p2p.op_reg_class, p2p.op_channel);
3374
3375 } else {
3376 p2p.op_reg_class = 81;
3377 /*
3378 * Use random operation channel from (1, 6, 11) if no other
3379 * preference is indicated.
3380 */
3381 os_get_random((u8 *) &r, sizeof(r));
3382 p2p.op_channel = 1 + (r % 3) * 5;
3383 p2p.cfg_op_channel = 0;
3384 wpa_printf(MSG_DEBUG, "P2P: Random operating channel: "
3385 "%d:%d", p2p.op_reg_class, p2p.op_channel);
3386 }
Dmitry Shmidt44c95782013-05-17 09:51:35 -07003387
3388 if (wpa_s->conf->p2p_pref_chan && wpa_s->conf->num_p2p_pref_chan) {
3389 p2p.pref_chan = wpa_s->conf->p2p_pref_chan;
3390 p2p.num_pref_chan = wpa_s->conf->num_p2p_pref_chan;
3391 }
3392
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003393 if (wpa_s->conf->country[0] && wpa_s->conf->country[1]) {
3394 os_memcpy(p2p.country, wpa_s->conf->country, 2);
3395 p2p.country[2] = 0x04;
3396 } else
3397 os_memcpy(p2p.country, "XX\x04", 3);
3398
3399 if (wpas_p2p_setup_channels(wpa_s, &p2p.channels)) {
3400 wpa_printf(MSG_ERROR, "P2P: Failed to configure supported "
3401 "channel list");
3402 return -1;
3403 }
3404
3405 os_memcpy(p2p.pri_dev_type, wpa_s->conf->device_type,
3406 WPS_DEV_TYPE_LEN);
3407
3408 p2p.num_sec_dev_types = wpa_s->conf->num_sec_device_types;
3409 os_memcpy(p2p.sec_dev_type, wpa_s->conf->sec_device_type,
3410 p2p.num_sec_dev_types * WPS_DEV_TYPE_LEN);
3411
3412 p2p.concurrent_operations = !!(wpa_s->drv_flags &
3413 WPA_DRIVER_FLAGS_P2P_CONCURRENT);
3414
3415 p2p.max_peers = 100;
3416
3417 if (wpa_s->conf->p2p_ssid_postfix) {
3418 p2p.ssid_postfix_len =
3419 os_strlen(wpa_s->conf->p2p_ssid_postfix);
3420 if (p2p.ssid_postfix_len > sizeof(p2p.ssid_postfix))
3421 p2p.ssid_postfix_len = sizeof(p2p.ssid_postfix);
3422 os_memcpy(p2p.ssid_postfix, wpa_s->conf->p2p_ssid_postfix,
3423 p2p.ssid_postfix_len);
3424 }
3425
3426 p2p.p2p_intra_bss = wpa_s->conf->p2p_intra_bss;
3427
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08003428 p2p.max_listen = wpa_s->max_remain_on_chan;
3429
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003430 global->p2p = p2p_init(&p2p);
3431 if (global->p2p == NULL)
3432 return -1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003433 global->p2p_init_wpa_s = wpa_s;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003434
3435 for (i = 0; i < MAX_WPS_VENDOR_EXT; i++) {
3436 if (wpa_s->conf->wps_vendor_ext[i] == NULL)
3437 continue;
3438 p2p_add_wps_vendor_extension(
3439 global->p2p, wpa_s->conf->wps_vendor_ext[i]);
3440 }
3441
3442 return 0;
3443}
3444
3445
3446/**
3447 * wpas_p2p_deinit - Deinitialize per-interface P2P data
3448 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
3449 *
3450 * This function deinitialize per-interface P2P data.
3451 */
3452void wpas_p2p_deinit(struct wpa_supplicant *wpa_s)
3453{
3454 if (wpa_s->driver && wpa_s->drv_priv)
3455 wpa_drv_probe_req_report(wpa_s, 0);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003456
3457 if (wpa_s->go_params) {
3458 /* Clear any stored provisioning info */
3459 p2p_clear_provisioning_info(
3460 wpa_s->global->p2p,
Dmitry Shmidt04949592012-07-19 12:16:46 -07003461 wpa_s->go_params->peer_device_addr);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003462 }
3463
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003464 os_free(wpa_s->go_params);
3465 wpa_s->go_params = NULL;
Dmitry Shmidt1cf45732013-04-29 17:36:45 -07003466#ifdef ANDROID_P2P
3467 eloop_cancel_timeout(wpas_p2p_group_freq_conflict, wpa_s, NULL);
3468#endif
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003469 eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s, NULL);
3470 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
3471 wpa_s->p2p_long_listen = 0;
3472 eloop_cancel_timeout(wpas_p2p_long_listen_timeout, wpa_s, NULL);
3473 eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL);
3474 wpas_p2p_remove_pending_group_interface(wpa_s);
3475
3476 /* TODO: remove group interface from the driver if this wpa_s instance
3477 * is on top of a P2P group interface */
3478}
3479
3480
3481/**
3482 * wpas_p2p_deinit_global - Deinitialize global P2P module
3483 * @global: Pointer to global data from wpa_supplicant_init()
3484 *
3485 * This function deinitializes the global (per device) P2P module.
3486 */
3487void wpas_p2p_deinit_global(struct wpa_global *global)
3488{
3489 struct wpa_supplicant *wpa_s, *tmp;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003490
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003491 wpa_s = global->ifaces;
3492 if (wpa_s)
3493 wpas_p2p_service_flush(wpa_s);
3494
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003495 if (global->p2p == NULL)
3496 return;
3497
3498 /* Remove remaining P2P group interfaces */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003499 while (wpa_s && wpa_s->p2p_group_interface != NOT_P2P_GROUP_INTERFACE)
3500 wpa_s = wpa_s->next;
3501 while (wpa_s) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003502 tmp = global->ifaces;
3503 while (tmp &&
3504 (tmp == wpa_s ||
3505 tmp->p2p_group_interface == NOT_P2P_GROUP_INTERFACE)) {
3506 tmp = tmp->next;
3507 }
3508 if (tmp == NULL)
3509 break;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003510 /* Disconnect from the P2P group and deinit the interface */
3511 wpas_p2p_disconnect(tmp);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003512 }
3513
3514 /*
3515 * Deinit GO data on any possibly remaining interface (if main
3516 * interface is used as GO).
3517 */
3518 for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
3519 if (wpa_s->ap_iface)
3520 wpas_p2p_group_deinit(wpa_s);
3521 }
3522
3523 p2p_deinit(global->p2p);
3524 global->p2p = NULL;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003525 global->p2p_init_wpa_s = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003526}
3527
3528
3529static int wpas_p2p_create_iface(struct wpa_supplicant *wpa_s)
3530{
Dmitry Shmidt34af3062013-07-11 10:46:32 -07003531 if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_DEDICATED_P2P_DEVICE) &&
3532 wpa_s->conf->p2p_no_group_iface)
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08003533 return 0; /* separate interface disabled per configuration */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003534 if (wpa_s->drv_flags &
3535 (WPA_DRIVER_FLAGS_P2P_DEDICATED_INTERFACE |
3536 WPA_DRIVER_FLAGS_P2P_MGMT_AND_NON_P2P))
3537 return 1; /* P2P group requires a new interface in every case
3538 */
3539 if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_CONCURRENT))
3540 return 0; /* driver does not support concurrent operations */
3541 if (wpa_s->global->ifaces->next)
3542 return 1; /* more that one interface already in use */
3543 if (wpa_s->wpa_state >= WPA_AUTHENTICATING)
3544 return 1; /* this interface is already in use */
3545 return 0;
3546}
3547
3548
3549static int wpas_p2p_start_go_neg(struct wpa_supplicant *wpa_s,
3550 const u8 *peer_addr,
3551 enum p2p_wps_method wps_method,
3552 int go_intent, const u8 *own_interface_addr,
Dmitry Shmidt04949592012-07-19 12:16:46 -07003553 unsigned int force_freq, int persistent_group,
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08003554 struct wpa_ssid *ssid, unsigned int pref_freq)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003555{
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003556 if (persistent_group && wpa_s->conf->persistent_reconnect)
3557 persistent_group = 2;
3558
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003559 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT) {
3560 return wpa_drv_p2p_connect(wpa_s, peer_addr, wps_method,
3561 go_intent, own_interface_addr,
3562 force_freq, persistent_group);
3563 }
3564
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003565 /*
3566 * Increase GO config timeout if HT40 is used since it takes some time
3567 * to scan channels for coex purposes before the BSS can be started.
3568 */
3569 p2p_set_config_timeout(wpa_s->global->p2p,
3570 wpa_s->p2p_go_ht40 ? 255 : 100, 20);
3571
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003572 return p2p_connect(wpa_s->global->p2p, peer_addr, wps_method,
3573 go_intent, own_interface_addr, force_freq,
Dmitry Shmidt04949592012-07-19 12:16:46 -07003574 persistent_group, ssid ? ssid->ssid : NULL,
3575 ssid ? ssid->ssid_len : 0,
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08003576 wpa_s->p2p_pd_before_go_neg, pref_freq);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003577}
3578
3579
3580static int wpas_p2p_auth_go_neg(struct wpa_supplicant *wpa_s,
3581 const u8 *peer_addr,
3582 enum p2p_wps_method wps_method,
3583 int go_intent, const u8 *own_interface_addr,
Dmitry Shmidt04949592012-07-19 12:16:46 -07003584 unsigned int force_freq, int persistent_group,
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08003585 struct wpa_ssid *ssid, unsigned int pref_freq)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003586{
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003587 if (persistent_group && wpa_s->conf->persistent_reconnect)
3588 persistent_group = 2;
3589
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003590 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
3591 return -1;
3592
3593 return p2p_authorize(wpa_s->global->p2p, peer_addr, wps_method,
3594 go_intent, own_interface_addr, force_freq,
Dmitry Shmidt04949592012-07-19 12:16:46 -07003595 persistent_group, ssid ? ssid->ssid : NULL,
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08003596 ssid ? ssid->ssid_len : 0, pref_freq);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003597}
3598
3599
3600static void wpas_p2p_check_join_scan_limit(struct wpa_supplicant *wpa_s)
3601{
3602 wpa_s->p2p_join_scan_count++;
3603 wpa_printf(MSG_DEBUG, "P2P: Join scan attempt %d",
3604 wpa_s->p2p_join_scan_count);
3605 if (wpa_s->p2p_join_scan_count > P2P_MAX_JOIN_SCAN_ATTEMPTS) {
3606 wpa_printf(MSG_DEBUG, "P2P: Failed to find GO " MACSTR
3607 " for join operationg - stop join attempt",
3608 MAC2STR(wpa_s->pending_join_iface_addr));
3609 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
Dmitry Shmidt04949592012-07-19 12:16:46 -07003610 if (wpa_s->p2p_auto_pd) {
3611 wpa_s->p2p_auto_pd = 0;
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07003612 wpa_msg_global(wpa_s, MSG_INFO,
3613 P2P_EVENT_PROV_DISC_FAILURE
3614 " p2p_dev_addr=" MACSTR " status=N/A",
3615 MAC2STR(wpa_s->pending_join_dev_addr));
Dmitry Shmidt04949592012-07-19 12:16:46 -07003616 return;
3617 }
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07003618 wpa_msg_global(wpa_s->parent, MSG_INFO,
3619 P2P_EVENT_GROUP_FORMATION_FAILURE);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003620 }
3621}
3622
3623
Dmitry Shmidt04949592012-07-19 12:16:46 -07003624static int wpas_check_freq_conflict(struct wpa_supplicant *wpa_s, int freq)
3625{
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07003626 int *freqs, res, num, i;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003627
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07003628 if (wpas_p2p_num_unused_channels(wpa_s) > 0) {
3629 /* Multiple channels are supported and not all are in use */
Dmitry Shmidt04949592012-07-19 12:16:46 -07003630 return 0;
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07003631 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07003632
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07003633 freqs = os_calloc(wpa_s->num_multichan_concurrent, sizeof(int));
3634 if (!freqs)
3635 return 1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003636
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07003637 num = wpas_p2p_valid_oper_freqs(wpa_s, freqs,
3638 wpa_s->num_multichan_concurrent);
3639 if (num < 0) {
3640 res = 1;
3641 goto exit_free;
3642 }
3643
3644 for (i = 0; i < num; i++) {
3645 if (freqs[i] == freq) {
3646 wpa_printf(MSG_DEBUG, "P2P: Frequency %d MHz in use by another virtual interface and can be used",
3647 freq);
3648 res = 0;
3649 goto exit_free;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003650 }
3651 }
3652
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07003653 res = 1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003654
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07003655exit_free:
3656 os_free(freqs);
3657 return res;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003658}
3659
3660
3661static int wpas_p2p_peer_go(struct wpa_supplicant *wpa_s,
3662 const u8 *peer_dev_addr)
3663{
3664 struct wpa_bss *bss;
3665 int updated;
3666
3667 bss = wpa_bss_get_p2p_dev_addr(wpa_s, peer_dev_addr);
3668 if (bss == NULL)
3669 return -1;
3670 if (bss->last_update_idx < wpa_s->bss_update_idx) {
3671 wpa_printf(MSG_DEBUG, "P2P: Peer BSS entry not updated in the "
3672 "last scan");
3673 return 0;
3674 }
3675
3676 updated = os_time_before(&wpa_s->p2p_auto_started, &bss->last_update);
3677 wpa_printf(MSG_DEBUG, "P2P: Current BSS entry for peer updated at "
3678 "%ld.%06ld (%supdated in last scan)",
3679 bss->last_update.sec, bss->last_update.usec,
3680 updated ? "": "not ");
3681
3682 return updated;
3683}
3684
3685
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003686static void wpas_p2p_scan_res_join(struct wpa_supplicant *wpa_s,
3687 struct wpa_scan_results *scan_res)
3688{
3689 struct wpa_bss *bss;
3690 int freq;
3691 u8 iface_addr[ETH_ALEN];
Dmitry Shmidt04949592012-07-19 12:16:46 -07003692
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003693 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
3694
3695 if (wpa_s->global->p2p_disabled)
3696 return;
3697
Dmitry Shmidt04949592012-07-19 12:16:46 -07003698 wpa_printf(MSG_DEBUG, "P2P: Scan results received (%d BSS) for %sjoin",
3699 scan_res ? (int) scan_res->num : -1,
3700 wpa_s->p2p_auto_join ? "auto_" : "");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003701
3702 if (scan_res)
3703 wpas_p2p_scan_res_handler(wpa_s, scan_res);
3704
Dmitry Shmidt04949592012-07-19 12:16:46 -07003705 if (wpa_s->p2p_auto_pd) {
3706 int join = wpas_p2p_peer_go(wpa_s,
3707 wpa_s->pending_join_dev_addr);
3708 if (join == 0 &&
3709 wpa_s->auto_pd_scan_retry < P2P_AUTO_PD_SCAN_ATTEMPTS) {
3710 wpa_s->auto_pd_scan_retry++;
Dmitry Shmidt444d5672013-04-01 13:08:44 -07003711 bss = wpa_bss_get_bssid_latest(
3712 wpa_s, wpa_s->pending_join_dev_addr);
Dmitry Shmidt04949592012-07-19 12:16:46 -07003713 if (bss) {
3714 freq = bss->freq;
3715 wpa_printf(MSG_DEBUG, "P2P: Scan retry %d for "
3716 "the peer " MACSTR " at %d MHz",
3717 wpa_s->auto_pd_scan_retry,
3718 MAC2STR(wpa_s->
3719 pending_join_dev_addr),
3720 freq);
3721 wpas_p2p_join_scan_req(wpa_s, freq);
3722 return;
3723 }
3724 }
3725
3726 if (join < 0)
3727 join = 0;
3728
3729 wpa_s->p2p_auto_pd = 0;
3730 wpa_s->pending_pd_use = join ? AUTO_PD_JOIN : AUTO_PD_GO_NEG;
3731 wpa_printf(MSG_DEBUG, "P2P: Auto PD with " MACSTR " join=%d",
3732 MAC2STR(wpa_s->pending_join_dev_addr), join);
3733 if (p2p_prov_disc_req(wpa_s->global->p2p,
3734 wpa_s->pending_join_dev_addr,
3735 wpa_s->pending_pd_config_methods, join,
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08003736 0, wpa_s->user_initiated_pd) < 0) {
Dmitry Shmidt04949592012-07-19 12:16:46 -07003737 wpa_s->p2p_auto_pd = 0;
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07003738 wpa_msg_global(wpa_s, MSG_INFO,
3739 P2P_EVENT_PROV_DISC_FAILURE
3740 " p2p_dev_addr=" MACSTR " status=N/A",
3741 MAC2STR(wpa_s->pending_join_dev_addr));
Dmitry Shmidt04949592012-07-19 12:16:46 -07003742 }
3743 return;
3744 }
3745
3746 if (wpa_s->p2p_auto_join) {
3747 int join = wpas_p2p_peer_go(wpa_s,
3748 wpa_s->pending_join_dev_addr);
3749 if (join < 0) {
3750 wpa_printf(MSG_DEBUG, "P2P: Peer was not found to be "
3751 "running a GO -> use GO Negotiation");
3752 wpas_p2p_connect(wpa_s, wpa_s->pending_join_dev_addr,
3753 wpa_s->p2p_pin, wpa_s->p2p_wps_method,
3754 wpa_s->p2p_persistent_group, 0, 0, 0,
3755 wpa_s->p2p_go_intent,
3756 wpa_s->p2p_connect_freq,
3757 wpa_s->p2p_persistent_id,
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003758 wpa_s->p2p_pd_before_go_neg,
3759 wpa_s->p2p_go_ht40);
Dmitry Shmidt04949592012-07-19 12:16:46 -07003760 return;
3761 }
3762
3763 wpa_printf(MSG_DEBUG, "P2P: Peer was found running GO%s -> "
3764 "try to join the group", join ? "" :
3765 " in older scan");
3766 if (!join)
3767 wpa_s->p2p_fallback_to_go_neg = 1;
3768 }
3769
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003770 freq = p2p_get_oper_freq(wpa_s->global->p2p,
3771 wpa_s->pending_join_iface_addr);
3772 if (freq < 0 &&
3773 p2p_get_interface_addr(wpa_s->global->p2p,
3774 wpa_s->pending_join_dev_addr,
3775 iface_addr) == 0 &&
3776 os_memcmp(iface_addr, wpa_s->pending_join_dev_addr, ETH_ALEN) != 0)
3777 {
3778 wpa_printf(MSG_DEBUG, "P2P: Overwrite pending interface "
3779 "address for join from " MACSTR " to " MACSTR
3780 " based on newly discovered P2P peer entry",
3781 MAC2STR(wpa_s->pending_join_iface_addr),
3782 MAC2STR(iface_addr));
3783 os_memcpy(wpa_s->pending_join_iface_addr, iface_addr,
3784 ETH_ALEN);
3785
3786 freq = p2p_get_oper_freq(wpa_s->global->p2p,
3787 wpa_s->pending_join_iface_addr);
3788 }
3789 if (freq >= 0) {
3790 wpa_printf(MSG_DEBUG, "P2P: Target GO operating frequency "
3791 "from P2P peer table: %d MHz", freq);
3792 }
Dmitry Shmidt444d5672013-04-01 13:08:44 -07003793 bss = wpa_bss_get_bssid_latest(wpa_s, wpa_s->pending_join_iface_addr);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003794 if (bss) {
3795 freq = bss->freq;
3796 wpa_printf(MSG_DEBUG, "P2P: Target GO operating frequency "
Dmitry Shmidt051af732013-10-22 13:52:46 -07003797 "from BSS table: %d MHz (SSID %s)", freq,
3798 wpa_ssid_txt(bss->ssid, bss->ssid_len));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003799 }
3800 if (freq > 0) {
3801 u16 method;
3802
Dmitry Shmidt04949592012-07-19 12:16:46 -07003803 if (wpas_check_freq_conflict(wpa_s, freq) > 0) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07003804 wpa_msg_global(wpa_s->parent, MSG_INFO,
3805 P2P_EVENT_GROUP_FORMATION_FAILURE
3806 "reason=FREQ_CONFLICT");
Dmitry Shmidt04949592012-07-19 12:16:46 -07003807 return;
3808 }
3809
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003810 wpa_printf(MSG_DEBUG, "P2P: Send Provision Discovery Request "
3811 "prior to joining an existing group (GO " MACSTR
3812 " freq=%u MHz)",
3813 MAC2STR(wpa_s->pending_join_dev_addr), freq);
3814 wpa_s->pending_pd_before_join = 1;
3815
3816 switch (wpa_s->pending_join_wps_method) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003817 case WPS_PIN_DISPLAY:
3818 method = WPS_CONFIG_KEYPAD;
3819 break;
3820 case WPS_PIN_KEYPAD:
3821 method = WPS_CONFIG_DISPLAY;
3822 break;
3823 case WPS_PBC:
3824 method = WPS_CONFIG_PUSHBUTTON;
3825 break;
3826 default:
3827 method = 0;
3828 break;
3829 }
3830
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003831 if ((p2p_get_provisioning_info(wpa_s->global->p2p,
3832 wpa_s->pending_join_dev_addr) ==
3833 method)) {
3834 /*
3835 * We have already performed provision discovery for
3836 * joining the group. Proceed directly to join
3837 * operation without duplicated provision discovery. */
3838 wpa_printf(MSG_DEBUG, "P2P: Provision discovery "
3839 "with " MACSTR " already done - proceed to "
3840 "join",
3841 MAC2STR(wpa_s->pending_join_dev_addr));
3842 wpa_s->pending_pd_before_join = 0;
3843 goto start;
3844 }
3845
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003846 if (p2p_prov_disc_req(wpa_s->global->p2p,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003847 wpa_s->pending_join_dev_addr, method, 1,
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08003848 freq, wpa_s->user_initiated_pd) < 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003849 wpa_printf(MSG_DEBUG, "P2P: Failed to send Provision "
3850 "Discovery Request before joining an "
3851 "existing group");
3852 wpa_s->pending_pd_before_join = 0;
3853 goto start;
3854 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003855 return;
3856 }
3857
3858 wpa_printf(MSG_DEBUG, "P2P: Failed to find BSS/GO - try again later");
3859 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
3860 eloop_register_timeout(1, 0, wpas_p2p_join_scan, wpa_s, NULL);
3861 wpas_p2p_check_join_scan_limit(wpa_s);
3862 return;
3863
3864start:
3865 /* Start join operation immediately */
3866 wpas_p2p_join_start(wpa_s);
3867}
3868
3869
Dmitry Shmidt04949592012-07-19 12:16:46 -07003870static void wpas_p2p_join_scan_req(struct wpa_supplicant *wpa_s, int freq)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003871{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003872 int ret;
3873 struct wpa_driver_scan_params params;
3874 struct wpabuf *wps_ie, *ies;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003875 size_t ielen;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003876 int freqs[2] = { 0, 0 };
Dmitry Shmidtad266fb2012-08-24 17:03:35 -07003877#ifdef ANDROID_P2P
3878 int oper_freq;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003879
Dmitry Shmidtad266fb2012-08-24 17:03:35 -07003880 /* If freq is not provided, check the operating freq of the GO and do a
3881 * a directed scan to save time
3882 */
3883 if(!freq) {
3884 freq = (oper_freq = p2p_get_oper_freq(wpa_s->global->p2p,
3885 wpa_s->pending_join_iface_addr) == -1) ? 0 : oper_freq;
3886 }
3887#endif
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003888 os_memset(&params, 0, sizeof(params));
3889
3890 /* P2P Wildcard SSID */
3891 params.num_ssids = 1;
3892 params.ssids[0].ssid = (u8 *) P2P_WILDCARD_SSID;
3893 params.ssids[0].ssid_len = P2P_WILDCARD_SSID_LEN;
3894
3895 wpa_s->wps->dev.p2p = 1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003896 wps_ie = wps_build_probe_req_ie(DEV_PW_DEFAULT, &wpa_s->wps->dev,
3897 wpa_s->wps->uuid, WPS_REQ_ENROLLEE, 0,
3898 NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003899 if (wps_ie == NULL) {
3900 wpas_p2p_scan_res_join(wpa_s, NULL);
3901 return;
3902 }
3903
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003904 ielen = p2p_scan_ie_buf_len(wpa_s->global->p2p);
3905 ies = wpabuf_alloc(wpabuf_len(wps_ie) + ielen);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003906 if (ies == NULL) {
3907 wpabuf_free(wps_ie);
3908 wpas_p2p_scan_res_join(wpa_s, NULL);
3909 return;
3910 }
3911 wpabuf_put_buf(ies, wps_ie);
3912 wpabuf_free(wps_ie);
3913
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08003914 p2p_scan_ie(wpa_s->global->p2p, ies, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003915
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003916 params.p2p_probe = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003917 params.extra_ies = wpabuf_head(ies);
3918 params.extra_ies_len = wpabuf_len(ies);
Dmitry Shmidt04949592012-07-19 12:16:46 -07003919 if (freq > 0) {
3920 freqs[0] = freq;
3921 params.freqs = freqs;
3922 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003923
3924 /*
3925 * Run a scan to update BSS table and start Provision Discovery once
3926 * the new scan results become available.
3927 */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003928 ret = wpa_drv_scan(wpa_s, &params);
Dmitry Shmidt444d5672013-04-01 13:08:44 -07003929 if (!ret) {
3930 os_get_time(&wpa_s->scan_trigger_time);
Dmitry Shmidt04949592012-07-19 12:16:46 -07003931 wpa_s->scan_res_handler = wpas_p2p_scan_res_join;
Dmitry Shmidt444d5672013-04-01 13:08:44 -07003932 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003933
3934 wpabuf_free(ies);
3935
3936 if (ret) {
3937 wpa_printf(MSG_DEBUG, "P2P: Failed to start scan for join - "
3938 "try again later");
3939 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
3940 eloop_register_timeout(1, 0, wpas_p2p_join_scan, wpa_s, NULL);
3941 wpas_p2p_check_join_scan_limit(wpa_s);
3942 }
3943}
3944
3945
Dmitry Shmidt04949592012-07-19 12:16:46 -07003946static void wpas_p2p_join_scan(void *eloop_ctx, void *timeout_ctx)
3947{
3948 struct wpa_supplicant *wpa_s = eloop_ctx;
3949 wpas_p2p_join_scan_req(wpa_s, 0);
3950}
3951
3952
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003953static int wpas_p2p_join(struct wpa_supplicant *wpa_s, const u8 *iface_addr,
Dmitry Shmidt04949592012-07-19 12:16:46 -07003954 const u8 *dev_addr, enum p2p_wps_method wps_method,
3955 int auto_join)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003956{
3957 wpa_printf(MSG_DEBUG, "P2P: Request to join existing group (iface "
Dmitry Shmidt04949592012-07-19 12:16:46 -07003958 MACSTR " dev " MACSTR ")%s",
3959 MAC2STR(iface_addr), MAC2STR(dev_addr),
3960 auto_join ? " (auto_join)" : "");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003961
Dmitry Shmidt04949592012-07-19 12:16:46 -07003962 wpa_s->p2p_auto_pd = 0;
3963 wpa_s->p2p_auto_join = !!auto_join;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003964 os_memcpy(wpa_s->pending_join_iface_addr, iface_addr, ETH_ALEN);
3965 os_memcpy(wpa_s->pending_join_dev_addr, dev_addr, ETH_ALEN);
3966 wpa_s->pending_join_wps_method = wps_method;
3967
3968 /* Make sure we are not running find during connection establishment */
3969 wpas_p2p_stop_find(wpa_s);
3970
3971 wpa_s->p2p_join_scan_count = 0;
3972 wpas_p2p_join_scan(wpa_s, NULL);
3973 return 0;
3974}
3975
3976
3977static int wpas_p2p_join_start(struct wpa_supplicant *wpa_s)
3978{
3979 struct wpa_supplicant *group;
3980 struct p2p_go_neg_results res;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003981 struct wpa_bss *bss;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003982
3983 group = wpas_p2p_get_group_iface(wpa_s, 0, 0);
3984 if (group == NULL)
3985 return -1;
3986 if (group != wpa_s) {
3987 os_memcpy(group->p2p_pin, wpa_s->p2p_pin,
3988 sizeof(group->p2p_pin));
3989 group->p2p_wps_method = wpa_s->p2p_wps_method;
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08003990 } else {
3991 /*
3992 * Need to mark the current interface for p2p_group_formation
3993 * when a separate group interface is not used. This is needed
3994 * to allow p2p_cancel stop a pending p2p_connect-join.
3995 * wpas_p2p_init_group_interface() addresses this for the case
3996 * where a separate group interface is used.
3997 */
3998 wpa_s->global->p2p_group_formation = wpa_s;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003999 }
4000
4001 group->p2p_in_provisioning = 1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07004002 group->p2p_fallback_to_go_neg = wpa_s->p2p_fallback_to_go_neg;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004003
4004 os_memset(&res, 0, sizeof(res));
4005 os_memcpy(res.peer_interface_addr, wpa_s->pending_join_iface_addr,
4006 ETH_ALEN);
4007 res.wps_method = wpa_s->pending_join_wps_method;
Dmitry Shmidt444d5672013-04-01 13:08:44 -07004008 bss = wpa_bss_get_bssid_latest(wpa_s, wpa_s->pending_join_iface_addr);
Dmitry Shmidt04949592012-07-19 12:16:46 -07004009 if (bss) {
4010 res.freq = bss->freq;
4011 res.ssid_len = bss->ssid_len;
4012 os_memcpy(res.ssid, bss->ssid, bss->ssid_len);
Dmitry Shmidt051af732013-10-22 13:52:46 -07004013 wpa_printf(MSG_DEBUG, "P2P: Join target GO operating frequency "
4014 "from BSS table: %d MHz (SSID %s)", bss->freq,
4015 wpa_ssid_txt(bss->ssid, bss->ssid_len));
Dmitry Shmidt04949592012-07-19 12:16:46 -07004016 }
4017
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08004018 if (wpa_s->off_channel_freq || wpa_s->roc_waiting_drv_freq) {
4019 wpa_printf(MSG_DEBUG, "P2P: Cancel remain-on-channel prior to "
4020 "starting client");
4021 wpa_drv_cancel_remain_on_channel(wpa_s);
4022 wpa_s->off_channel_freq = 0;
4023 wpa_s->roc_waiting_drv_freq = 0;
4024 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004025 wpas_start_wps_enrollee(group, &res);
4026
4027 /*
4028 * Allow a longer timeout for join-a-running-group than normal 15
4029 * second group formation timeout since the GO may not have authorized
4030 * our connection yet.
4031 */
4032 eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s, NULL);
4033 eloop_register_timeout(60, 0, wpas_p2p_group_formation_timeout,
4034 wpa_s, NULL);
4035
4036 return 0;
4037}
4038
4039
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08004040static int wpas_p2p_setup_freqs(struct wpa_supplicant *wpa_s, int freq,
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07004041 int *force_freq, int *pref_freq)
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08004042{
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07004043 int *freqs, res;
4044 unsigned int freq_in_use = 0, num, i;
4045
4046 freqs = os_calloc(wpa_s->num_multichan_concurrent, sizeof(int));
4047 if (!freqs)
4048 return -1;
4049
4050 num = get_shared_radio_freqs(wpa_s, freqs,
4051 wpa_s->num_multichan_concurrent);
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07004052 wpa_printf(MSG_DEBUG,
4053 "P2P: Setup freqs: freq=%d num_MCC=%d shared_freqs=%u",
4054 freq, wpa_s->num_multichan_concurrent, num);
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07004055
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08004056 if (freq > 0) {
4057 if (!p2p_supported_freq(wpa_s->global->p2p, freq)) {
4058 wpa_printf(MSG_DEBUG, "P2P: The forced channel "
4059 "(%u MHz) is not supported for P2P uses",
4060 freq);
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07004061 res = -3;
4062 goto exit_free;
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08004063 }
4064
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07004065 for (i = 0; i < num; i++) {
4066 if (freqs[i] == freq)
4067 freq_in_use = 1;
4068 }
4069
4070 if (num == wpa_s->num_multichan_concurrent && !freq_in_use) {
4071 wpa_printf(MSG_DEBUG, "P2P: Cannot start P2P group on %u MHz as there are no available channels",
4072 freq);
4073 res = -2;
4074 goto exit_free;
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08004075 }
4076 wpa_printf(MSG_DEBUG, "P2P: Trying to force us to use the "
4077 "requested channel (%u MHz)", freq);
4078 *force_freq = freq;
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07004079 goto exit_ok;
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08004080 }
4081
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07004082 for (i = 0; i < num; i++) {
4083 if (!p2p_supported_freq(wpa_s->global->p2p, freqs[i]))
4084 continue;
4085
Dmitry Shmidt51a47d52013-09-10 10:52:57 -07004086#ifndef ANDROID_P2P
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07004087 wpa_printf(MSG_DEBUG, "P2P: Try to force us to use frequency (%u MHz) which is already in use",
4088 *force_freq);
4089 *force_freq = freqs[i];
Dmitry Shmidt51a47d52013-09-10 10:52:57 -07004090#endif
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07004091
4092 if (*pref_freq == 0 && num < wpa_s->num_multichan_concurrent) {
4093 wpa_printf(MSG_DEBUG, "P2P: Try to prefer a frequency we are already using");
Dmitry Shmidt51a47d52013-09-10 10:52:57 -07004094 *pref_freq = freqs[i];
4095#ifdef ANDROID_P2P
4096 } else {
4097 wpa_printf(MSG_DEBUG, "P2P: Try to force us to use frequency (%u MHz) which is already in use",
4098 *force_freq);
4099 *force_freq = freqs[i];
4100#endif
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07004101 }
4102 break;
4103 }
4104
4105 if (i == num) {
4106 if (num < wpa_s->num_multichan_concurrent) {
4107 wpa_printf(MSG_DEBUG, "P2P: Current operating channels are not available for P2P. Try to use another channel");
4108 *force_freq = 0;
4109 } else {
4110 wpa_printf(MSG_DEBUG, "P2P: All channels are in use and none of them are P2P enabled. Cannot start P2P group");
4111 res = -2;
4112 goto exit_free;
4113 }
4114 }
4115
4116exit_ok:
4117 res = 0;
4118exit_free:
4119 os_free(freqs);
4120 return res;
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08004121}
4122
4123
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004124/**
4125 * wpas_p2p_connect - Request P2P Group Formation to be started
4126 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
4127 * @peer_addr: Address of the peer P2P Device
4128 * @pin: PIN to use during provisioning or %NULL to indicate PBC mode
4129 * @persistent_group: Whether to create a persistent group
Dmitry Shmidt04949592012-07-19 12:16:46 -07004130 * @auto_join: Whether to select join vs. GO Negotiation automatically
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004131 * @join: Whether to join an existing group (as a client) instead of starting
4132 * Group Owner negotiation; @peer_addr is BSSID in that case
4133 * @auth: Whether to only authorize the connection instead of doing that and
4134 * initiating Group Owner negotiation
4135 * @go_intent: GO Intent or -1 to use default
4136 * @freq: Frequency for the group or 0 for auto-selection
Dmitry Shmidt04949592012-07-19 12:16:46 -07004137 * @persistent_id: Persistent group credentials to use for forcing GO
4138 * parameters or -1 to generate new values (SSID/passphrase)
4139 * @pd: Whether to send Provision Discovery prior to GO Negotiation as an
4140 * interoperability workaround when initiating group formation
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004141 * @ht40: Start GO with 40 MHz channel width
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004142 * Returns: 0 or new PIN (if pin was %NULL) on success, -1 on unspecified
4143 * failure, -2 on failure due to channel not currently available,
4144 * -3 if forced channel is not supported
4145 */
4146int wpas_p2p_connect(struct wpa_supplicant *wpa_s, const u8 *peer_addr,
4147 const char *pin, enum p2p_wps_method wps_method,
Dmitry Shmidt04949592012-07-19 12:16:46 -07004148 int persistent_group, int auto_join, int join, int auth,
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004149 int go_intent, int freq, int persistent_id, int pd,
4150 int ht40)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004151{
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07004152 int force_freq = 0, pref_freq = 0;
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08004153 int ret = 0, res;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004154 enum wpa_driver_if_type iftype;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004155 const u8 *if_addr;
Dmitry Shmidt04949592012-07-19 12:16:46 -07004156 struct wpa_ssid *ssid = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004157
4158 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
4159 return -1;
4160
Dmitry Shmidt04949592012-07-19 12:16:46 -07004161 if (persistent_id >= 0) {
4162 ssid = wpa_config_get_network(wpa_s->conf, persistent_id);
4163 if (ssid == NULL || ssid->disabled != 2 ||
4164 ssid->mode != WPAS_MODE_P2P_GO)
4165 return -1;
4166 }
4167
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07004168 os_free(wpa_s->global->add_psk);
4169 wpa_s->global->add_psk = NULL;
4170
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004171 if (go_intent < 0)
4172 go_intent = wpa_s->conf->p2p_go_intent;
4173
4174 if (!auth)
4175 wpa_s->p2p_long_listen = 0;
4176
4177 wpa_s->p2p_wps_method = wps_method;
Dmitry Shmidt04949592012-07-19 12:16:46 -07004178 wpa_s->p2p_persistent_group = !!persistent_group;
4179 wpa_s->p2p_persistent_id = persistent_id;
4180 wpa_s->p2p_go_intent = go_intent;
4181 wpa_s->p2p_connect_freq = freq;
4182 wpa_s->p2p_fallback_to_go_neg = 0;
4183 wpa_s->p2p_pd_before_go_neg = !!pd;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004184 wpa_s->p2p_go_ht40 = !!ht40;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004185
4186 if (pin)
4187 os_strlcpy(wpa_s->p2p_pin, pin, sizeof(wpa_s->p2p_pin));
4188 else if (wps_method == WPS_PIN_DISPLAY) {
4189 ret = wps_generate_pin();
4190 os_snprintf(wpa_s->p2p_pin, sizeof(wpa_s->p2p_pin), "%08d",
4191 ret);
4192 wpa_printf(MSG_DEBUG, "P2P: Randomly generated PIN: %s",
4193 wpa_s->p2p_pin);
4194 } else
4195 wpa_s->p2p_pin[0] = '\0';
4196
Dmitry Shmidt04949592012-07-19 12:16:46 -07004197 if (join || auto_join) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004198 u8 iface_addr[ETH_ALEN], dev_addr[ETH_ALEN];
4199 if (auth) {
4200 wpa_printf(MSG_DEBUG, "P2P: Authorize invitation to "
4201 "connect a running group from " MACSTR,
4202 MAC2STR(peer_addr));
4203 os_memcpy(wpa_s->p2p_auth_invite, peer_addr, ETH_ALEN);
4204 return ret;
4205 }
4206 os_memcpy(dev_addr, peer_addr, ETH_ALEN);
4207 if (p2p_get_interface_addr(wpa_s->global->p2p, peer_addr,
4208 iface_addr) < 0) {
4209 os_memcpy(iface_addr, peer_addr, ETH_ALEN);
4210 p2p_get_dev_addr(wpa_s->global->p2p, peer_addr,
4211 dev_addr);
4212 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07004213 if (auto_join) {
4214 os_get_time(&wpa_s->p2p_auto_started);
4215 wpa_printf(MSG_DEBUG, "P2P: Auto join started at "
4216 "%ld.%06ld",
4217 wpa_s->p2p_auto_started.sec,
4218 wpa_s->p2p_auto_started.usec);
4219 }
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08004220 wpa_s->user_initiated_pd = 1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07004221 if (wpas_p2p_join(wpa_s, iface_addr, dev_addr, wps_method,
4222 auto_join) < 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004223 return -1;
4224 return ret;
4225 }
4226
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07004227 res = wpas_p2p_setup_freqs(wpa_s, freq, &force_freq, &pref_freq);
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08004228 if (res)
4229 return res;
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07004230 wpas_p2p_set_own_freq_preference(wpa_s, force_freq);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004231
4232 wpa_s->create_p2p_iface = wpas_p2p_create_iface(wpa_s);
4233
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004234 if (wpa_s->create_p2p_iface) {
4235 /* Prepare to add a new interface for the group */
4236 iftype = WPA_IF_P2P_GROUP;
4237 if (go_intent == 15)
4238 iftype = WPA_IF_P2P_GO;
4239 if (wpas_p2p_add_group_interface(wpa_s, iftype) < 0) {
4240 wpa_printf(MSG_ERROR, "P2P: Failed to allocate a new "
4241 "interface for the group");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004242 return -1;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004243 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004244
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004245 if_addr = wpa_s->pending_interface_addr;
4246 } else
4247 if_addr = wpa_s->own_addr;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004248
4249 if (auth) {
4250 if (wpas_p2p_auth_go_neg(wpa_s, peer_addr, wps_method,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004251 go_intent, if_addr,
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08004252 force_freq, persistent_group, ssid,
4253 pref_freq) < 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004254 return -1;
4255 return ret;
4256 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004257
4258 if (wpas_p2p_start_go_neg(wpa_s, peer_addr, wps_method,
4259 go_intent, if_addr, force_freq,
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08004260 persistent_group, ssid, pref_freq) < 0) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004261 if (wpa_s->create_p2p_iface)
4262 wpas_p2p_remove_pending_group_interface(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004263 return -1;
4264 }
4265 return ret;
4266}
4267
4268
4269/**
4270 * wpas_p2p_remain_on_channel_cb - Indication of remain-on-channel start
4271 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
4272 * @freq: Frequency of the channel in MHz
4273 * @duration: Duration of the stay on the channel in milliseconds
4274 *
4275 * This callback is called when the driver indicates that it has started the
4276 * requested remain-on-channel duration.
4277 */
4278void wpas_p2p_remain_on_channel_cb(struct wpa_supplicant *wpa_s,
4279 unsigned int freq, unsigned int duration)
4280{
4281 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
4282 return;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004283 if (wpa_s->off_channel_freq == wpa_s->pending_listen_freq) {
4284 p2p_listen_cb(wpa_s->global->p2p, wpa_s->pending_listen_freq,
4285 wpa_s->pending_listen_duration);
4286 wpa_s->pending_listen_freq = 0;
4287 }
4288}
4289
4290
4291static int wpas_p2p_listen_start(struct wpa_supplicant *wpa_s,
4292 unsigned int timeout)
4293{
4294 /* Limit maximum Listen state time based on driver limitation. */
4295 if (timeout > wpa_s->max_remain_on_chan)
4296 timeout = wpa_s->max_remain_on_chan;
4297
4298 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
4299 return wpa_drv_p2p_listen(wpa_s, timeout);
4300
4301 return p2p_listen(wpa_s->global->p2p, timeout);
4302}
4303
4304
4305/**
4306 * wpas_p2p_cancel_remain_on_channel_cb - Remain-on-channel timeout
4307 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
4308 * @freq: Frequency of the channel in MHz
4309 *
4310 * This callback is called when the driver indicates that a remain-on-channel
4311 * operation has been completed, i.e., the duration on the requested channel
4312 * has timed out.
4313 */
4314void wpas_p2p_cancel_remain_on_channel_cb(struct wpa_supplicant *wpa_s,
4315 unsigned int freq)
4316{
4317 wpa_printf(MSG_DEBUG, "P2P: Cancel remain-on-channel callback "
4318 "(p2p_long_listen=%d ms pending_action_tx=%p)",
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08004319 wpa_s->p2p_long_listen, offchannel_pending_action_tx(wpa_s));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004320 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
4321 return;
4322 if (p2p_listen_end(wpa_s->global->p2p, freq) > 0)
4323 return; /* P2P module started a new operation */
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08004324 if (offchannel_pending_action_tx(wpa_s))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004325 return;
4326 if (wpa_s->p2p_long_listen > 0)
4327 wpa_s->p2p_long_listen -= wpa_s->max_remain_on_chan;
4328 if (wpa_s->p2p_long_listen > 0) {
4329 wpa_printf(MSG_DEBUG, "P2P: Continuing long Listen state");
4330 wpas_p2p_listen_start(wpa_s, wpa_s->p2p_long_listen);
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07004331 } else {
4332 /*
4333 * When listen duration is over, stop listen & update p2p_state
4334 * to IDLE.
4335 */
4336 p2p_stop_listen(wpa_s->global->p2p);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004337 }
4338}
4339
4340
4341/**
4342 * wpas_p2p_group_remove - Remove a P2P group
4343 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
4344 * @ifname: Network interface name of the group interface or "*" to remove all
4345 * groups
4346 * Returns: 0 on success, -1 on failure
4347 *
4348 * This function is used to remove a P2P group. This can be used to disconnect
4349 * from a group in which the local end is a P2P Client or to end a P2P Group in
4350 * case the local end is the Group Owner. If a virtual network interface was
4351 * created for this group, that interface will be removed. Otherwise, only the
4352 * configured P2P group network will be removed from the interface.
4353 */
4354int wpas_p2p_group_remove(struct wpa_supplicant *wpa_s, const char *ifname)
4355{
4356 struct wpa_global *global = wpa_s->global;
4357
4358 if (os_strcmp(ifname, "*") == 0) {
4359 struct wpa_supplicant *prev;
4360 wpa_s = global->ifaces;
4361 while (wpa_s) {
4362 prev = wpa_s;
4363 wpa_s = wpa_s->next;
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07004364 if (prev->p2p_group_interface !=
4365 NOT_P2P_GROUP_INTERFACE ||
4366 (prev->current_ssid &&
4367 prev->current_ssid->p2p_group))
4368 wpas_p2p_disconnect(prev);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004369 }
4370 return 0;
4371 }
4372
4373 for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
4374 if (os_strcmp(wpa_s->ifname, ifname) == 0)
4375 break;
4376 }
4377
4378 return wpas_p2p_disconnect(wpa_s);
4379}
4380
4381
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07004382static int wpas_p2p_select_go_freq(struct wpa_supplicant *wpa_s, int freq)
4383{
4384 unsigned int r;
4385
4386 if (freq == 2) {
4387 wpa_printf(MSG_DEBUG, "P2P: Request to start GO on 2.4 GHz "
4388 "band");
4389 if (wpa_s->best_24_freq > 0 &&
4390 p2p_supported_freq(wpa_s->global->p2p,
4391 wpa_s->best_24_freq)) {
4392 freq = wpa_s->best_24_freq;
4393 wpa_printf(MSG_DEBUG, "P2P: Use best 2.4 GHz band "
4394 "channel: %d MHz", freq);
4395 } else {
4396 os_get_random((u8 *) &r, sizeof(r));
4397 freq = 2412 + (r % 3) * 25;
4398 wpa_printf(MSG_DEBUG, "P2P: Use random 2.4 GHz band "
4399 "channel: %d MHz", freq);
4400 }
4401 }
4402
4403 if (freq == 5) {
4404 wpa_printf(MSG_DEBUG, "P2P: Request to start GO on 5 GHz "
4405 "band");
4406 if (wpa_s->best_5_freq > 0 &&
4407 p2p_supported_freq(wpa_s->global->p2p,
4408 wpa_s->best_5_freq)) {
4409 freq = wpa_s->best_5_freq;
4410 wpa_printf(MSG_DEBUG, "P2P: Use best 5 GHz band "
4411 "channel: %d MHz", freq);
4412 } else {
4413 os_get_random((u8 *) &r, sizeof(r));
4414 freq = 5180 + (r % 4) * 20;
4415 if (!p2p_supported_freq(wpa_s->global->p2p, freq)) {
4416 wpa_printf(MSG_DEBUG, "P2P: Could not select "
4417 "5 GHz channel for P2P group");
4418 return -1;
4419 }
4420 wpa_printf(MSG_DEBUG, "P2P: Use random 5 GHz band "
4421 "channel: %d MHz", freq);
4422 }
4423 }
4424
4425 if (freq > 0 && !p2p_supported_freq(wpa_s->global->p2p, freq)) {
4426 wpa_printf(MSG_DEBUG, "P2P: The forced channel for GO "
4427 "(%u MHz) is not supported for P2P uses",
4428 freq);
4429 return -1;
4430 }
4431
4432 return freq;
4433}
4434
4435
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004436static int wpas_p2p_init_go_params(struct wpa_supplicant *wpa_s,
4437 struct p2p_go_neg_results *params,
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08004438 int freq, int ht40,
4439 const struct p2p_channels *channels)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004440{
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07004441 int res, *freqs;
Dmitry Shmidt44c95782013-05-17 09:51:35 -07004442 unsigned int pref_freq;
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07004443 unsigned int num, i;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004444
4445 os_memset(params, 0, sizeof(*params));
4446 params->role_go = 1;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004447 params->ht40 = ht40;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004448 if (freq) {
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08004449 if (!freq_included(channels, freq)) {
4450 wpa_printf(MSG_DEBUG, "P2P: Forced GO freq %d MHz not "
4451 "accepted", freq);
4452 return -1;
4453 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004454 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on forced "
4455 "frequency %d MHz", freq);
4456 params->freq = freq;
4457 } else if (wpa_s->conf->p2p_oper_reg_class == 81 &&
4458 wpa_s->conf->p2p_oper_channel >= 1 &&
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08004459 wpa_s->conf->p2p_oper_channel <= 11 &&
4460 freq_included(channels,
4461 2407 + 5 * wpa_s->conf->p2p_oper_channel)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004462 params->freq = 2407 + 5 * wpa_s->conf->p2p_oper_channel;
4463 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on configured "
4464 "frequency %d MHz", params->freq);
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08004465 } else if ((wpa_s->conf->p2p_oper_reg_class == 115 ||
4466 wpa_s->conf->p2p_oper_reg_class == 116 ||
4467 wpa_s->conf->p2p_oper_reg_class == 117 ||
4468 wpa_s->conf->p2p_oper_reg_class == 124 ||
4469 wpa_s->conf->p2p_oper_reg_class == 126 ||
4470 wpa_s->conf->p2p_oper_reg_class == 127) &&
4471 freq_included(channels,
4472 5000 + 5 * wpa_s->conf->p2p_oper_channel)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004473 params->freq = 5000 + 5 * wpa_s->conf->p2p_oper_channel;
4474 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on configured "
4475 "frequency %d MHz", params->freq);
4476 } else if (wpa_s->conf->p2p_oper_channel == 0 &&
4477 wpa_s->best_overall_freq > 0 &&
4478 p2p_supported_freq(wpa_s->global->p2p,
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08004479 wpa_s->best_overall_freq) &&
4480 freq_included(channels, wpa_s->best_overall_freq)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004481 params->freq = wpa_s->best_overall_freq;
4482 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on best overall "
4483 "channel %d MHz", params->freq);
4484 } else if (wpa_s->conf->p2p_oper_channel == 0 &&
4485 wpa_s->best_24_freq > 0 &&
4486 p2p_supported_freq(wpa_s->global->p2p,
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08004487 wpa_s->best_24_freq) &&
4488 freq_included(channels, wpa_s->best_24_freq)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004489 params->freq = wpa_s->best_24_freq;
4490 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on best 2.4 GHz "
4491 "channel %d MHz", params->freq);
4492 } else if (wpa_s->conf->p2p_oper_channel == 0 &&
4493 wpa_s->best_5_freq > 0 &&
4494 p2p_supported_freq(wpa_s->global->p2p,
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08004495 wpa_s->best_5_freq) &&
4496 freq_included(channels, wpa_s->best_5_freq)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004497 params->freq = wpa_s->best_5_freq;
4498 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on best 5 GHz "
4499 "channel %d MHz", params->freq);
Dmitry Shmidt44c95782013-05-17 09:51:35 -07004500 } else if ((pref_freq = p2p_get_pref_freq(wpa_s->global->p2p,
4501 channels))) {
4502 params->freq = pref_freq;
4503 wpa_printf(MSG_DEBUG, "P2P: Set GO freq %d MHz from preferred "
4504 "channels", params->freq);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004505 } else {
Dmitry Shmidt04949592012-07-19 12:16:46 -07004506 int chan;
4507 for (chan = 0; chan < 11; chan++) {
4508 params->freq = 2412 + chan * 5;
4509 if (!wpas_p2p_disallowed_freq(wpa_s->global,
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08004510 params->freq) &&
4511 freq_included(channels, params->freq))
Dmitry Shmidt04949592012-07-19 12:16:46 -07004512 break;
4513 }
4514 if (chan == 11) {
4515 wpa_printf(MSG_DEBUG, "P2P: No 2.4 GHz channel "
4516 "allowed");
4517 return -1;
4518 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004519 wpa_printf(MSG_DEBUG, "P2P: Set GO freq %d MHz (no preference "
4520 "known)", params->freq);
4521 }
4522
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07004523 freqs = os_calloc(wpa_s->num_multichan_concurrent, sizeof(int));
4524 if (!freqs)
4525 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004526
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07004527 res = wpas_p2p_valid_oper_freqs(wpa_s, freqs,
4528 wpa_s->num_multichan_concurrent);
4529 if (res < 0) {
4530 os_free(freqs);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004531 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004532 }
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07004533 num = res;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004534
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07004535 if (!freq) {
4536 for (i = 0; i < num; i++) {
4537 if (freq_included(channels, freqs[i])) {
4538 wpa_printf(MSG_DEBUG, "P2P: Force GO on a channel we are already using (%u MHz)",
4539 freqs[i]);
4540 params->freq = freqs[i];
4541 break;
4542 }
4543 }
4544
4545 if (i == num) {
4546 if (num == wpa_s->num_multichan_concurrent) {
4547 wpa_printf(MSG_DEBUG, "P2P: Cannot force GO on any of the channels we are already using");
4548 os_free(freqs);
4549 return -1;
4550 } else {
4551 wpa_printf(MSG_DEBUG, "P2P: Cannot force GO on any of the channels we are already using. Use one of the free channels");
4552 }
4553 }
4554 } else {
4555 for (i = 0; i < num; i++) {
4556 if (freqs[i] == freq)
4557 break;
4558 }
4559
4560 if (i == num) {
4561 if (num == wpa_s->num_multichan_concurrent) {
4562 wpa_printf(MSG_DEBUG, "P2P: Cannot force GO on freq (%u MHz) as all the channels are in use", freq);
4563 os_free(freqs);
4564 return -1;
4565 } else {
4566 wpa_printf(MSG_DEBUG, "P2P: Cannot force GO on any of the channels we are already using. Use one of the free channels");
4567 }
4568 }
4569 }
4570 os_free(freqs);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004571 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004572}
4573
4574
4575static struct wpa_supplicant *
4576wpas_p2p_get_group_iface(struct wpa_supplicant *wpa_s, int addr_allocated,
4577 int go)
4578{
4579 struct wpa_supplicant *group_wpa_s;
4580
Dmitry Shmidtaa532512012-09-24 10:35:31 -07004581 if (!wpas_p2p_create_iface(wpa_s)) {
4582 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Use same interface for group "
4583 "operations");
Dmitry Shmidt56052862013-10-04 10:23:25 -07004584 wpa_s->p2p_first_connection_timeout = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004585 return wpa_s;
Dmitry Shmidtaa532512012-09-24 10:35:31 -07004586 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004587
4588 if (wpas_p2p_add_group_interface(wpa_s, go ? WPA_IF_P2P_GO :
Dmitry Shmidtaa532512012-09-24 10:35:31 -07004589 WPA_IF_P2P_CLIENT) < 0) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07004590 wpa_msg_global(wpa_s, MSG_ERROR,
4591 "P2P: Failed to add group interface");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004592 return NULL;
Dmitry Shmidtaa532512012-09-24 10:35:31 -07004593 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004594 group_wpa_s = wpas_p2p_init_group_interface(wpa_s, go);
4595 if (group_wpa_s == NULL) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07004596 wpa_msg_global(wpa_s, MSG_ERROR,
4597 "P2P: Failed to initialize group interface");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004598 wpas_p2p_remove_pending_group_interface(wpa_s);
4599 return NULL;
4600 }
4601
Dmitry Shmidtaa532512012-09-24 10:35:31 -07004602 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Use separate group interface %s",
4603 group_wpa_s->ifname);
Dmitry Shmidt56052862013-10-04 10:23:25 -07004604 group_wpa_s->p2p_first_connection_timeout = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004605 return group_wpa_s;
4606}
4607
4608
4609/**
4610 * wpas_p2p_group_add - Add a new P2P group with local end as Group Owner
4611 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
4612 * @persistent_group: Whether to create a persistent group
4613 * @freq: Frequency for the group or 0 to indicate no hardcoding
4614 * Returns: 0 on success, -1 on failure
4615 *
4616 * This function creates a new P2P group with the local end as the Group Owner,
4617 * i.e., without using Group Owner Negotiation.
4618 */
4619int wpas_p2p_group_add(struct wpa_supplicant *wpa_s, int persistent_group,
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004620 int freq, int ht40)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004621{
4622 struct p2p_go_neg_results params;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004623
4624 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
4625 return -1;
4626
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07004627 os_free(wpa_s->global->add_psk);
4628 wpa_s->global->add_psk = NULL;
4629
Dmitry Shmidtdca39792011-09-06 11:17:33 -07004630 /* Make sure we are not running find during connection establishment */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004631 wpa_printf(MSG_DEBUG, "P2P: Stop any on-going P2P FIND");
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08004632 wpas_p2p_stop_find_oper(wpa_s);
Dmitry Shmidtdca39792011-09-06 11:17:33 -07004633
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07004634 freq = wpas_p2p_select_go_freq(wpa_s, freq);
4635 if (freq < 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004636 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004637
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08004638 if (wpas_p2p_init_go_params(wpa_s, &params, freq, ht40, NULL))
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004639 return -1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07004640 if (params.freq &&
4641 !p2p_supported_freq(wpa_s->global->p2p, params.freq)) {
4642 wpa_printf(MSG_DEBUG, "P2P: The selected channel for GO "
4643 "(%u MHz) is not supported for P2P uses",
4644 params.freq);
4645 return -1;
4646 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004647 p2p_go_params(wpa_s->global->p2p, &params);
4648 params.persistent_group = persistent_group;
4649
4650 wpa_s = wpas_p2p_get_group_iface(wpa_s, 0, 1);
4651 if (wpa_s == NULL)
4652 return -1;
4653 wpas_start_wps_go(wpa_s, &params, 0);
4654
4655 return 0;
4656}
4657
4658
4659static int wpas_start_p2p_client(struct wpa_supplicant *wpa_s,
4660 struct wpa_ssid *params, int addr_allocated)
4661{
4662 struct wpa_ssid *ssid;
4663
4664 wpa_s = wpas_p2p_get_group_iface(wpa_s, addr_allocated, 0);
4665 if (wpa_s == NULL)
4666 return -1;
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07004667 wpa_s->p2p_last_4way_hs_fail = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004668
4669 wpa_supplicant_ap_deinit(wpa_s);
4670
4671 ssid = wpa_config_add_network(wpa_s->conf);
4672 if (ssid == NULL)
4673 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004674 wpa_config_set_network_defaults(ssid);
4675 ssid->temporary = 1;
4676 ssid->proto = WPA_PROTO_RSN;
4677 ssid->pairwise_cipher = WPA_CIPHER_CCMP;
4678 ssid->group_cipher = WPA_CIPHER_CCMP;
4679 ssid->key_mgmt = WPA_KEY_MGMT_PSK;
4680 ssid->ssid = os_malloc(params->ssid_len);
4681 if (ssid->ssid == NULL) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004682 wpa_config_remove_network(wpa_s->conf, ssid->id);
4683 return -1;
4684 }
4685 os_memcpy(ssid->ssid, params->ssid, params->ssid_len);
4686 ssid->ssid_len = params->ssid_len;
4687 ssid->p2p_group = 1;
4688 ssid->export_keys = 1;
4689 if (params->psk_set) {
4690 os_memcpy(ssid->psk, params->psk, 32);
4691 ssid->psk_set = 1;
4692 }
4693 if (params->passphrase)
4694 ssid->passphrase = os_strdup(params->passphrase);
4695
4696 wpa_supplicant_select_network(wpa_s, ssid);
4697
4698 wpa_s->show_group_started = 1;
4699
4700 return 0;
4701}
4702
4703
4704int wpas_p2p_group_add_persistent(struct wpa_supplicant *wpa_s,
4705 struct wpa_ssid *ssid, int addr_allocated,
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08004706 int freq, int ht40,
Dmitry Shmidt56052862013-10-04 10:23:25 -07004707 const struct p2p_channels *channels,
4708 int connection_timeout)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004709{
4710 struct p2p_go_neg_results params;
4711 int go = 0;
4712
4713 if (ssid->disabled != 2 || ssid->ssid == NULL)
4714 return -1;
4715
4716 if (wpas_get_p2p_group(wpa_s, ssid->ssid, ssid->ssid_len, &go) &&
4717 go == (ssid->mode == WPAS_MODE_P2P_GO)) {
4718 wpa_printf(MSG_DEBUG, "P2P: Requested persistent group is "
4719 "already running");
4720 return 0;
4721 }
4722
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07004723 os_free(wpa_s->global->add_psk);
4724 wpa_s->global->add_psk = NULL;
4725
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004726 /* Make sure we are not running find during connection establishment */
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08004727 wpas_p2p_stop_find_oper(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004728
Dmitry Shmidt04949592012-07-19 12:16:46 -07004729 wpa_s->p2p_fallback_to_go_neg = 0;
4730
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004731 if (ssid->mode == WPAS_MODE_INFRA)
4732 return wpas_start_p2p_client(wpa_s, ssid, addr_allocated);
4733
4734 if (ssid->mode != WPAS_MODE_P2P_GO)
4735 return -1;
4736
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07004737 freq = wpas_p2p_select_go_freq(wpa_s, freq);
4738 if (freq < 0)
4739 return -1;
4740
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08004741 if (wpas_p2p_init_go_params(wpa_s, &params, freq, ht40, channels))
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004742 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004743
4744 params.role_go = 1;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08004745 params.psk_set = ssid->psk_set;
4746 if (params.psk_set)
4747 os_memcpy(params.psk, ssid->psk, sizeof(params.psk));
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08004748 if (ssid->passphrase) {
4749 if (os_strlen(ssid->passphrase) >= sizeof(params.passphrase)) {
4750 wpa_printf(MSG_ERROR, "P2P: Invalid passphrase in "
4751 "persistent group");
4752 return -1;
4753 }
4754 os_strlcpy(params.passphrase, ssid->passphrase,
4755 sizeof(params.passphrase));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004756 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004757 os_memcpy(params.ssid, ssid->ssid, ssid->ssid_len);
4758 params.ssid_len = ssid->ssid_len;
4759 params.persistent_group = 1;
4760
4761 wpa_s = wpas_p2p_get_group_iface(wpa_s, addr_allocated, 1);
4762 if (wpa_s == NULL)
4763 return -1;
4764
Dmitry Shmidt56052862013-10-04 10:23:25 -07004765 wpa_s->p2p_first_connection_timeout = connection_timeout;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004766 wpas_start_wps_go(wpa_s, &params, 0);
4767
4768 return 0;
4769}
4770
4771
4772static void wpas_p2p_ie_update(void *ctx, struct wpabuf *beacon_ies,
4773 struct wpabuf *proberesp_ies)
4774{
4775 struct wpa_supplicant *wpa_s = ctx;
4776 if (wpa_s->ap_iface) {
4777 struct hostapd_data *hapd = wpa_s->ap_iface->bss[0];
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004778 if (!(hapd->conf->p2p & P2P_GROUP_OWNER)) {
4779 wpabuf_free(beacon_ies);
4780 wpabuf_free(proberesp_ies);
4781 return;
4782 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004783 if (beacon_ies) {
4784 wpabuf_free(hapd->p2p_beacon_ie);
4785 hapd->p2p_beacon_ie = beacon_ies;
4786 }
4787 wpabuf_free(hapd->p2p_probe_resp_ie);
4788 hapd->p2p_probe_resp_ie = proberesp_ies;
4789 } else {
4790 wpabuf_free(beacon_ies);
4791 wpabuf_free(proberesp_ies);
4792 }
4793 wpa_supplicant_ap_update_beacon(wpa_s);
4794}
4795
4796
4797static void wpas_p2p_idle_update(void *ctx, int idle)
4798{
4799 struct wpa_supplicant *wpa_s = ctx;
4800 if (!wpa_s->ap_iface)
4801 return;
4802 wpa_printf(MSG_DEBUG, "P2P: GO - group %sidle", idle ? "" : "not ");
Dmitry Shmidt04949592012-07-19 12:16:46 -07004803 if (idle)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004804 wpas_p2p_set_group_idle_timeout(wpa_s);
4805 else
4806 eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL);
4807}
4808
4809
4810struct p2p_group * wpas_p2p_group_init(struct wpa_supplicant *wpa_s,
Dmitry Shmidt04949592012-07-19 12:16:46 -07004811 struct wpa_ssid *ssid)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004812{
4813 struct p2p_group *group;
4814 struct p2p_group_config *cfg;
4815
4816 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
4817 return NULL;
4818 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
4819 return NULL;
4820
4821 cfg = os_zalloc(sizeof(*cfg));
4822 if (cfg == NULL)
4823 return NULL;
4824
Dmitry Shmidt04949592012-07-19 12:16:46 -07004825 if (ssid->p2p_persistent_group && wpa_s->conf->persistent_reconnect)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004826 cfg->persistent_group = 2;
Dmitry Shmidt04949592012-07-19 12:16:46 -07004827 else if (ssid->p2p_persistent_group)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004828 cfg->persistent_group = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004829 os_memcpy(cfg->interface_addr, wpa_s->own_addr, ETH_ALEN);
4830 if (wpa_s->max_stations &&
4831 wpa_s->max_stations < wpa_s->conf->max_num_sta)
4832 cfg->max_clients = wpa_s->max_stations;
4833 else
4834 cfg->max_clients = wpa_s->conf->max_num_sta;
Dmitry Shmidt04949592012-07-19 12:16:46 -07004835 os_memcpy(cfg->ssid, ssid->ssid, ssid->ssid_len);
4836 cfg->ssid_len = ssid->ssid_len;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004837 cfg->cb_ctx = wpa_s;
4838 cfg->ie_update = wpas_p2p_ie_update;
4839 cfg->idle_update = wpas_p2p_idle_update;
4840
4841 group = p2p_group_init(wpa_s->global->p2p, cfg);
4842 if (group == NULL)
4843 os_free(cfg);
Dmitry Shmidt04949592012-07-19 12:16:46 -07004844 if (ssid->mode != WPAS_MODE_P2P_GROUP_FORMATION)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004845 p2p_group_notif_formation_done(group);
4846 wpa_s->p2p_group = group;
4847 return group;
4848}
4849
4850
4851void wpas_p2p_wps_success(struct wpa_supplicant *wpa_s, const u8 *peer_addr,
4852 int registrar)
4853{
Dmitry Shmidt04949592012-07-19 12:16:46 -07004854 struct wpa_ssid *ssid = wpa_s->current_ssid;
4855
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004856 if (!wpa_s->p2p_in_provisioning) {
4857 wpa_printf(MSG_DEBUG, "P2P: Ignore WPS success event - P2P "
4858 "provisioning not in progress");
4859 return;
4860 }
4861
Dmitry Shmidt04949592012-07-19 12:16:46 -07004862 if (ssid && ssid->mode == WPAS_MODE_INFRA) {
4863 u8 go_dev_addr[ETH_ALEN];
4864 os_memcpy(go_dev_addr, wpa_s->bssid, ETH_ALEN);
4865 wpas_p2p_persistent_group(wpa_s, go_dev_addr, ssid->ssid,
4866 ssid->ssid_len);
4867 /* Clear any stored provisioning info */
4868 p2p_clear_provisioning_info(wpa_s->global->p2p, go_dev_addr);
4869 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004870
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004871 eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s->parent,
4872 NULL);
Dmitry Shmidt56052862013-10-04 10:23:25 -07004873 wpa_s->p2p_go_group_formation_completed = 1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07004874 if (ssid && ssid->mode == WPAS_MODE_INFRA) {
4875 /*
4876 * Use a separate timeout for initial data connection to
4877 * complete to allow the group to be removed automatically if
4878 * something goes wrong in this step before the P2P group idle
4879 * timeout mechanism is taken into use.
4880 */
Dmitry Shmidt56052862013-10-04 10:23:25 -07004881 wpa_dbg(wpa_s, MSG_DEBUG,
4882 "P2P: Re-start group formation timeout (%d seconds) as client for initial connection",
4883 P2P_MAX_INITIAL_CONN_WAIT);
Dmitry Shmidt04949592012-07-19 12:16:46 -07004884 eloop_register_timeout(P2P_MAX_INITIAL_CONN_WAIT, 0,
4885 wpas_p2p_group_formation_timeout,
4886 wpa_s->parent, NULL);
Dmitry Shmidt56052862013-10-04 10:23:25 -07004887 } else if (ssid) {
4888 /*
4889 * Use a separate timeout for initial data connection to
4890 * complete to allow the group to be removed automatically if
4891 * the client does not complete data connection successfully.
4892 */
4893 wpa_dbg(wpa_s, MSG_DEBUG,
4894 "P2P: Re-start group formation timeout (%d seconds) as GO for initial connection",
4895 P2P_MAX_INITIAL_CONN_WAIT_GO);
4896 eloop_register_timeout(P2P_MAX_INITIAL_CONN_WAIT_GO, 0,
4897 wpas_p2p_group_formation_timeout,
4898 wpa_s->parent, NULL);
4899 /*
4900 * Complete group formation on first successful data connection
4901 */
4902 wpa_s->p2p_go_group_formation_completed = 0;
Dmitry Shmidt04949592012-07-19 12:16:46 -07004903 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004904 if (wpa_s->global->p2p)
4905 p2p_wps_success_cb(wpa_s->global->p2p, peer_addr);
4906 else if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
4907 wpa_drv_wps_success_cb(wpa_s, peer_addr);
4908 wpas_group_formation_completed(wpa_s, 1);
4909}
4910
4911
Jouni Malinen75ecf522011-06-27 15:19:46 -07004912void wpas_p2p_wps_failed(struct wpa_supplicant *wpa_s,
4913 struct wps_event_fail *fail)
4914{
4915 if (!wpa_s->p2p_in_provisioning) {
4916 wpa_printf(MSG_DEBUG, "P2P: Ignore WPS fail event - P2P "
4917 "provisioning not in progress");
4918 return;
4919 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004920
4921 if (wpa_s->go_params) {
4922 p2p_clear_provisioning_info(
4923 wpa_s->global->p2p,
Dmitry Shmidt04949592012-07-19 12:16:46 -07004924 wpa_s->go_params->peer_device_addr);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004925 }
4926
Jouni Malinen75ecf522011-06-27 15:19:46 -07004927 wpas_notify_p2p_wps_failed(wpa_s, fail);
4928}
4929
4930
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004931int wpas_p2p_prov_disc(struct wpa_supplicant *wpa_s, const u8 *peer_addr,
Dmitry Shmidt04949592012-07-19 12:16:46 -07004932 const char *config_method,
4933 enum wpas_p2p_prov_disc_use use)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004934{
4935 u16 config_methods;
4936
Dmitry Shmidt04949592012-07-19 12:16:46 -07004937 wpa_s->p2p_fallback_to_go_neg = 0;
4938 wpa_s->pending_pd_use = NORMAL_PD;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004939 if (os_strncmp(config_method, "display", 7) == 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004940 config_methods = WPS_CONFIG_DISPLAY;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004941 else if (os_strncmp(config_method, "keypad", 6) == 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004942 config_methods = WPS_CONFIG_KEYPAD;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004943 else if (os_strncmp(config_method, "pbc", 3) == 0 ||
4944 os_strncmp(config_method, "pushbutton", 10) == 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004945 config_methods = WPS_CONFIG_PUSHBUTTON;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004946 else {
4947 wpa_printf(MSG_DEBUG, "P2P: Unknown config method");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004948 return -1;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004949 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004950
Dmitry Shmidt04949592012-07-19 12:16:46 -07004951 if (use == WPAS_P2P_PD_AUTO) {
4952 os_memcpy(wpa_s->pending_join_dev_addr, peer_addr, ETH_ALEN);
4953 wpa_s->pending_pd_config_methods = config_methods;
4954 wpa_s->p2p_auto_pd = 1;
4955 wpa_s->p2p_auto_join = 0;
4956 wpa_s->pending_pd_before_join = 0;
4957 wpa_s->auto_pd_scan_retry = 0;
4958 wpas_p2p_stop_find(wpa_s);
4959 wpa_s->p2p_join_scan_count = 0;
4960 os_get_time(&wpa_s->p2p_auto_started);
4961 wpa_printf(MSG_DEBUG, "P2P: Auto PD started at %ld.%06ld",
4962 wpa_s->p2p_auto_started.sec,
4963 wpa_s->p2p_auto_started.usec);
4964 wpas_p2p_join_scan(wpa_s, NULL);
4965 return 0;
4966 }
4967
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004968 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT) {
4969 return wpa_drv_p2p_prov_disc_req(wpa_s, peer_addr,
Dmitry Shmidt04949592012-07-19 12:16:46 -07004970 config_methods,
4971 use == WPAS_P2P_PD_FOR_JOIN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004972 }
4973
4974 if (wpa_s->global->p2p == NULL || wpa_s->global->p2p_disabled)
4975 return -1;
4976
4977 return p2p_prov_disc_req(wpa_s->global->p2p, peer_addr,
Dmitry Shmidt04949592012-07-19 12:16:46 -07004978 config_methods, use == WPAS_P2P_PD_FOR_JOIN,
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08004979 0, 1);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004980}
4981
4982
4983int wpas_p2p_scan_result_text(const u8 *ies, size_t ies_len, char *buf,
4984 char *end)
4985{
4986 return p2p_scan_result_text(ies, ies_len, buf, end);
4987}
4988
4989
4990static void wpas_p2p_clear_pending_action_tx(struct wpa_supplicant *wpa_s)
4991{
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08004992 if (!offchannel_pending_action_tx(wpa_s))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004993 return;
4994
4995 wpa_printf(MSG_DEBUG, "P2P: Drop pending Action TX due to new "
4996 "operation request");
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08004997 offchannel_clear_pending_action_tx(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004998}
4999
5000
5001int wpas_p2p_find(struct wpa_supplicant *wpa_s, unsigned int timeout,
5002 enum p2p_discovery_type type,
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08005003 unsigned int num_req_dev_types, const u8 *req_dev_types,
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005004 const u8 *dev_id, unsigned int search_delay)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005005{
5006 wpas_p2p_clear_pending_action_tx(wpa_s);
5007 wpa_s->p2p_long_listen = 0;
5008
5009 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
5010 return wpa_drv_p2p_find(wpa_s, timeout, type);
5011
Dmitry Shmidt04949592012-07-19 12:16:46 -07005012 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL ||
5013 wpa_s->p2p_in_provisioning)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005014 return -1;
5015
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08005016 wpa_supplicant_cancel_sched_scan(wpa_s);
5017
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005018 return p2p_find(wpa_s->global->p2p, timeout, type,
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005019 num_req_dev_types, req_dev_types, dev_id,
5020 search_delay);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005021}
5022
5023
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08005024static int wpas_p2p_stop_find_oper(struct wpa_supplicant *wpa_s)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005025{
5026 wpas_p2p_clear_pending_action_tx(wpa_s);
5027 wpa_s->p2p_long_listen = 0;
5028 eloop_cancel_timeout(wpas_p2p_long_listen_timeout, wpa_s, NULL);
5029 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
Jouni Malinendc7b7132012-09-14 12:53:47 -07005030 wpa_s->global->p2p_cb_on_scan_complete = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005031
5032 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT) {
5033 wpa_drv_p2p_stop_find(wpa_s);
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08005034 return 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005035 }
5036
5037 if (wpa_s->global->p2p)
5038 p2p_stop_find(wpa_s->global->p2p);
5039
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08005040 return 0;
5041}
5042
5043
5044void wpas_p2p_stop_find(struct wpa_supplicant *wpa_s)
5045{
5046 if (wpas_p2p_stop_find_oper(wpa_s) > 0)
5047 return;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005048 wpas_p2p_remove_pending_group_interface(wpa_s);
5049}
5050
5051
5052static void wpas_p2p_long_listen_timeout(void *eloop_ctx, void *timeout_ctx)
5053{
5054 struct wpa_supplicant *wpa_s = eloop_ctx;
5055 wpa_s->p2p_long_listen = 0;
5056}
5057
5058
5059int wpas_p2p_listen(struct wpa_supplicant *wpa_s, unsigned int timeout)
5060{
5061 int res;
5062
5063 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
5064 return -1;
5065
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08005066 wpa_supplicant_cancel_sched_scan(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005067 wpas_p2p_clear_pending_action_tx(wpa_s);
5068
5069 if (timeout == 0) {
5070 /*
5071 * This is a request for unlimited Listen state. However, at
5072 * least for now, this is mapped to a Listen state for one
5073 * hour.
5074 */
5075 timeout = 3600;
5076 }
5077 eloop_cancel_timeout(wpas_p2p_long_listen_timeout, wpa_s, NULL);
5078 wpa_s->p2p_long_listen = 0;
5079
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005080 /*
5081 * Stop previous find/listen operation to avoid trying to request a new
5082 * remain-on-channel operation while the driver is still running the
5083 * previous one.
5084 */
5085 if (wpa_s->global->p2p)
5086 p2p_stop_find(wpa_s->global->p2p);
5087
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005088 res = wpas_p2p_listen_start(wpa_s, timeout * 1000);
5089 if (res == 0 && timeout * 1000 > wpa_s->max_remain_on_chan) {
5090 wpa_s->p2p_long_listen = timeout * 1000;
5091 eloop_register_timeout(timeout, 0,
5092 wpas_p2p_long_listen_timeout,
5093 wpa_s, NULL);
5094 }
5095
5096 return res;
5097}
5098
5099
5100int wpas_p2p_assoc_req_ie(struct wpa_supplicant *wpa_s, struct wpa_bss *bss,
5101 u8 *buf, size_t len, int p2p_group)
5102{
5103 struct wpabuf *p2p_ie;
5104 int ret;
5105
5106 if (wpa_s->global->p2p_disabled)
5107 return -1;
5108 if (wpa_s->global->p2p == NULL)
5109 return -1;
5110 if (bss == NULL)
5111 return -1;
5112
5113 p2p_ie = wpa_bss_get_vendor_ie_multi(bss, P2P_IE_VENDOR_TYPE);
5114 ret = p2p_assoc_req_ie(wpa_s->global->p2p, bss->bssid, buf, len,
5115 p2p_group, p2p_ie);
5116 wpabuf_free(p2p_ie);
5117
5118 return ret;
5119}
5120
5121
5122int wpas_p2p_probe_req_rx(struct wpa_supplicant *wpa_s, const u8 *addr,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005123 const u8 *dst, const u8 *bssid,
Dmitry Shmidt04949592012-07-19 12:16:46 -07005124 const u8 *ie, size_t ie_len, int ssi_signal)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005125{
5126 if (wpa_s->global->p2p_disabled)
5127 return 0;
5128 if (wpa_s->global->p2p == NULL)
5129 return 0;
5130
Dmitry Shmidt04949592012-07-19 12:16:46 -07005131 switch (p2p_probe_req_rx(wpa_s->global->p2p, addr, dst, bssid,
5132 ie, ie_len)) {
5133 case P2P_PREQ_NOT_P2P:
5134 wpas_notify_preq(wpa_s, addr, dst, bssid, ie, ie_len,
5135 ssi_signal);
5136 /* fall through */
5137 case P2P_PREQ_MALFORMED:
5138 case P2P_PREQ_NOT_LISTEN:
5139 case P2P_PREQ_NOT_PROCESSED:
5140 default: /* make gcc happy */
5141 return 0;
5142 case P2P_PREQ_PROCESSED:
5143 return 1;
5144 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005145}
5146
5147
5148void wpas_p2p_rx_action(struct wpa_supplicant *wpa_s, const u8 *da,
5149 const u8 *sa, const u8 *bssid,
5150 u8 category, const u8 *data, size_t len, int freq)
5151{
5152 if (wpa_s->global->p2p_disabled)
5153 return;
5154 if (wpa_s->global->p2p == NULL)
5155 return;
5156
5157 p2p_rx_action(wpa_s->global->p2p, da, sa, bssid, category, data, len,
5158 freq);
5159}
5160
5161
5162void wpas_p2p_scan_ie(struct wpa_supplicant *wpa_s, struct wpabuf *ies)
5163{
5164 if (wpa_s->global->p2p_disabled)
5165 return;
5166 if (wpa_s->global->p2p == NULL)
5167 return;
5168
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08005169 p2p_scan_ie(wpa_s->global->p2p, ies, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005170}
5171
5172
5173void wpas_p2p_group_deinit(struct wpa_supplicant *wpa_s)
5174{
5175 p2p_group_deinit(wpa_s->p2p_group);
5176 wpa_s->p2p_group = NULL;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005177
5178 wpa_s->ap_configured_cb = NULL;
5179 wpa_s->ap_configured_cb_ctx = NULL;
5180 wpa_s->ap_configured_cb_data = NULL;
5181 wpa_s->connect_without_scan = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005182}
5183
5184
5185int wpas_p2p_reject(struct wpa_supplicant *wpa_s, const u8 *addr)
5186{
5187 wpa_s->p2p_long_listen = 0;
5188
5189 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
5190 return wpa_drv_p2p_reject(wpa_s, addr);
5191
5192 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
5193 return -1;
5194
5195 return p2p_reject(wpa_s->global->p2p, addr);
5196}
5197
5198
5199/* Invite to reinvoke a persistent group */
5200int wpas_p2p_invite(struct wpa_supplicant *wpa_s, const u8 *peer_addr,
Jouni Malinen31be0a42012-08-31 21:20:51 +03005201 struct wpa_ssid *ssid, const u8 *go_dev_addr, int freq,
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08005202 int ht40, int pref_freq)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005203{
5204 enum p2p_invite_role role;
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07005205 u8 *bssid = NULL;
5206 int force_freq = 0;
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08005207 int res;
5208
Dmitry Shmidt700a1372013-03-15 14:14:44 -07005209 wpa_s->global->p2p_invite_group = NULL;
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08005210 if (peer_addr)
5211 os_memcpy(wpa_s->p2p_auth_invite, peer_addr, ETH_ALEN);
5212 else
5213 os_memset(wpa_s->p2p_auth_invite, 0, ETH_ALEN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005214
Jouni Malinen31be0a42012-08-31 21:20:51 +03005215 wpa_s->p2p_persistent_go_freq = freq;
5216 wpa_s->p2p_go_ht40 = !!ht40;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005217 if (ssid->mode == WPAS_MODE_P2P_GO) {
5218 role = P2P_INVITE_ROLE_GO;
5219 if (peer_addr == NULL) {
5220 wpa_printf(MSG_DEBUG, "P2P: Missing peer "
5221 "address in invitation command");
5222 return -1;
5223 }
5224 if (wpas_p2p_create_iface(wpa_s)) {
5225 if (wpas_p2p_add_group_interface(wpa_s,
5226 WPA_IF_P2P_GO) < 0) {
5227 wpa_printf(MSG_ERROR, "P2P: Failed to "
5228 "allocate a new interface for the "
5229 "group");
5230 return -1;
5231 }
5232 bssid = wpa_s->pending_interface_addr;
5233 } else
5234 bssid = wpa_s->own_addr;
5235 } else {
5236 role = P2P_INVITE_ROLE_CLIENT;
5237 peer_addr = ssid->bssid;
5238 }
5239 wpa_s->pending_invite_ssid_id = ssid->id;
5240
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07005241 res = wpas_p2p_setup_freqs(wpa_s, freq, &force_freq, &pref_freq);
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08005242 if (res)
5243 return res;
Irfan Sheriffaf84a572012-09-22 16:59:30 -07005244
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005245 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
5246 return wpa_drv_p2p_invite(wpa_s, peer_addr, role, bssid,
5247 ssid->ssid, ssid->ssid_len,
5248 go_dev_addr, 1);
5249
5250 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
5251 return -1;
5252
5253 return p2p_invite(wpa_s->global->p2p, peer_addr, role, bssid,
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08005254 ssid->ssid, ssid->ssid_len, force_freq, go_dev_addr,
5255 1, pref_freq);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005256}
5257
5258
5259/* Invite to join an active group */
5260int wpas_p2p_invite_group(struct wpa_supplicant *wpa_s, const char *ifname,
5261 const u8 *peer_addr, const u8 *go_dev_addr)
5262{
5263 struct wpa_global *global = wpa_s->global;
5264 enum p2p_invite_role role;
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07005265 u8 *bssid = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005266 struct wpa_ssid *ssid;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08005267 int persistent;
Dmitry Shmidt051af732013-10-22 13:52:46 -07005268 int freq = 0, force_freq = 0, pref_freq = 0;
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08005269 int res;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005270
Jouni Malinen31be0a42012-08-31 21:20:51 +03005271 wpa_s->p2p_persistent_go_freq = 0;
5272 wpa_s->p2p_go_ht40 = 0;
5273
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005274 for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
5275 if (os_strcmp(wpa_s->ifname, ifname) == 0)
5276 break;
5277 }
5278 if (wpa_s == NULL) {
5279 wpa_printf(MSG_DEBUG, "P2P: Interface '%s' not found", ifname);
5280 return -1;
5281 }
5282
5283 ssid = wpa_s->current_ssid;
5284 if (ssid == NULL) {
5285 wpa_printf(MSG_DEBUG, "P2P: No current SSID to use for "
5286 "invitation");
5287 return -1;
5288 }
5289
Dmitry Shmidt700a1372013-03-15 14:14:44 -07005290 wpa_s->global->p2p_invite_group = wpa_s;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08005291 persistent = ssid->p2p_persistent_group &&
5292 wpas_p2p_get_persistent(wpa_s->parent, peer_addr,
5293 ssid->ssid, ssid->ssid_len);
5294
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005295 if (ssid->mode == WPAS_MODE_P2P_GO) {
5296 role = P2P_INVITE_ROLE_ACTIVE_GO;
5297 bssid = wpa_s->own_addr;
5298 if (go_dev_addr == NULL)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005299 go_dev_addr = wpa_s->global->p2p_dev_addr;
Dmitry Shmidt051af732013-10-22 13:52:46 -07005300 freq = ssid->frequency;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005301 } else {
5302 role = P2P_INVITE_ROLE_CLIENT;
5303 if (wpa_s->wpa_state < WPA_ASSOCIATED) {
5304 wpa_printf(MSG_DEBUG, "P2P: Not associated - cannot "
5305 "invite to current group");
5306 return -1;
5307 }
5308 bssid = wpa_s->bssid;
5309 if (go_dev_addr == NULL &&
5310 !is_zero_ether_addr(wpa_s->go_dev_addr))
5311 go_dev_addr = wpa_s->go_dev_addr;
Dmitry Shmidt051af732013-10-22 13:52:46 -07005312 freq = wpa_s->current_bss ? wpa_s->current_bss->freq :
5313 (int) wpa_s->assoc_freq;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005314 }
5315 wpa_s->parent->pending_invite_ssid_id = -1;
5316
5317 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
5318 return wpa_drv_p2p_invite(wpa_s, peer_addr, role, bssid,
5319 ssid->ssid, ssid->ssid_len,
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08005320 go_dev_addr, persistent);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005321
5322 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
5323 return -1;
5324
Dmitry Shmidt051af732013-10-22 13:52:46 -07005325 res = wpas_p2p_setup_freqs(wpa_s, freq, &force_freq, &pref_freq);
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08005326 if (res)
5327 return res;
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07005328 wpas_p2p_set_own_freq_preference(wpa_s, force_freq);
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08005329
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005330 return p2p_invite(wpa_s->global->p2p, peer_addr, role, bssid,
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08005331 ssid->ssid, ssid->ssid_len, force_freq,
5332 go_dev_addr, persistent, pref_freq);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005333}
5334
5335
5336void wpas_p2p_completed(struct wpa_supplicant *wpa_s)
5337{
5338 struct wpa_ssid *ssid = wpa_s->current_ssid;
5339 const char *ssid_txt;
5340 u8 go_dev_addr[ETH_ALEN];
Jouni Malinen75ecf522011-06-27 15:19:46 -07005341 int network_id = -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005342 int persistent;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005343 int freq;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005344
Dmitry Shmidt04949592012-07-19 12:16:46 -07005345 if (ssid == NULL || ssid->mode != WPAS_MODE_P2P_GROUP_FORMATION) {
5346 eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
5347 wpa_s->parent, NULL);
5348 }
5349
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005350 if (!wpa_s->show_group_started || !ssid)
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005351 goto done;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005352
5353 wpa_s->show_group_started = 0;
5354
5355 ssid_txt = wpa_ssid_txt(ssid->ssid, ssid->ssid_len);
5356 os_memset(go_dev_addr, 0, ETH_ALEN);
5357 if (ssid->bssid_set)
5358 os_memcpy(go_dev_addr, ssid->bssid, ETH_ALEN);
5359 persistent = wpas_p2p_persistent_group(wpa_s, go_dev_addr, ssid->ssid,
5360 ssid->ssid_len);
5361 os_memcpy(wpa_s->go_dev_addr, go_dev_addr, ETH_ALEN);
5362
5363 if (wpa_s->global->p2p_group_formation == wpa_s)
5364 wpa_s->global->p2p_group_formation = NULL;
5365
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005366 freq = wpa_s->current_bss ? wpa_s->current_bss->freq :
5367 (int) wpa_s->assoc_freq;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005368 if (ssid->passphrase == NULL && ssid->psk_set) {
5369 char psk[65];
5370 wpa_snprintf_hex(psk, sizeof(psk), ssid->psk, 32);
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07005371 wpa_msg_global(wpa_s->parent, MSG_INFO, P2P_EVENT_GROUP_STARTED
5372 "%s client ssid=\"%s\" freq=%d psk=%s "
5373 "go_dev_addr=" MACSTR "%s",
5374 wpa_s->ifname, ssid_txt, freq, psk,
5375 MAC2STR(go_dev_addr),
5376 persistent ? " [PERSISTENT]" : "");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005377 } else {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07005378 wpa_msg_global(wpa_s->parent, MSG_INFO, P2P_EVENT_GROUP_STARTED
5379 "%s client ssid=\"%s\" freq=%d "
5380 "passphrase=\"%s\" go_dev_addr=" MACSTR "%s",
5381 wpa_s->ifname, ssid_txt, freq,
5382 ssid->passphrase ? ssid->passphrase : "",
5383 MAC2STR(go_dev_addr),
5384 persistent ? " [PERSISTENT]" : "");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005385 }
5386
5387 if (persistent)
Jouni Malinen75ecf522011-06-27 15:19:46 -07005388 network_id = wpas_p2p_store_persistent_group(wpa_s->parent,
5389 ssid, go_dev_addr);
5390 if (network_id < 0)
5391 network_id = ssid->id;
5392 wpas_notify_p2p_group_started(wpa_s, ssid, network_id, 1);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005393
5394done:
Dmitry Shmidt37d4d6a2013-03-18 13:09:42 -07005395 wpas_p2p_continue_after_scan(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005396}
5397
5398
5399int wpas_p2p_presence_req(struct wpa_supplicant *wpa_s, u32 duration1,
5400 u32 interval1, u32 duration2, u32 interval2)
5401{
5402 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
5403 return -1;
5404 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
5405 return -1;
5406
5407 if (wpa_s->wpa_state < WPA_ASSOCIATED ||
5408 wpa_s->current_ssid == NULL ||
5409 wpa_s->current_ssid->mode != WPAS_MODE_INFRA)
5410 return -1;
5411
5412 return p2p_presence_req(wpa_s->global->p2p, wpa_s->bssid,
5413 wpa_s->own_addr, wpa_s->assoc_freq,
5414 duration1, interval1, duration2, interval2);
5415}
5416
5417
5418int wpas_p2p_ext_listen(struct wpa_supplicant *wpa_s, unsigned int period,
5419 unsigned int interval)
5420{
5421 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
5422 return -1;
5423
5424 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
5425 return -1;
5426
5427 return p2p_ext_listen(wpa_s->global->p2p, period, interval);
5428}
5429
5430
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005431static int wpas_p2p_is_client(struct wpa_supplicant *wpa_s)
5432{
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08005433 if (wpa_s->current_ssid == NULL) {
5434 /*
5435 * current_ssid can be cleared when P2P client interface gets
5436 * disconnected, so assume this interface was used as P2P
5437 * client.
5438 */
5439 return 1;
5440 }
5441 return wpa_s->current_ssid->p2p_group &&
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005442 wpa_s->current_ssid->mode == WPAS_MODE_INFRA;
5443}
5444
5445
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005446static void wpas_p2p_group_idle_timeout(void *eloop_ctx, void *timeout_ctx)
5447{
5448 struct wpa_supplicant *wpa_s = eloop_ctx;
5449
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005450 if (wpa_s->conf->p2p_group_idle == 0 && !wpas_p2p_is_client(wpa_s)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005451 wpa_printf(MSG_DEBUG, "P2P: Ignore group idle timeout - "
5452 "disabled");
5453 return;
5454 }
5455
Dmitry Shmidt04949592012-07-19 12:16:46 -07005456 wpa_printf(MSG_DEBUG, "P2P: Group idle timeout reached - terminate "
5457 "group");
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005458 wpas_p2p_group_delete(wpa_s, P2P_GROUP_REMOVAL_IDLE_TIMEOUT);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005459}
5460
5461
5462static void wpas_p2p_set_group_idle_timeout(struct wpa_supplicant *wpa_s)
5463{
Dmitry Shmidt04949592012-07-19 12:16:46 -07005464 int timeout;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005465
Dmitry Shmidt04949592012-07-19 12:16:46 -07005466 if (eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL) > 0)
5467 wpa_printf(MSG_DEBUG, "P2P: Cancelled P2P group idle timeout");
5468
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005469 if (wpa_s->current_ssid == NULL || !wpa_s->current_ssid->p2p_group)
5470 return;
5471
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005472 timeout = wpa_s->conf->p2p_group_idle;
5473 if (wpa_s->current_ssid->mode == WPAS_MODE_INFRA &&
5474 (timeout == 0 || timeout > P2P_MAX_CLIENT_IDLE))
5475 timeout = P2P_MAX_CLIENT_IDLE;
5476
5477 if (timeout == 0)
5478 return;
5479
Dmitry Shmidt04949592012-07-19 12:16:46 -07005480 if (timeout < 0) {
5481 if (wpa_s->current_ssid->mode == WPAS_MODE_INFRA)
5482 timeout = 0; /* special client mode no-timeout */
5483 else
5484 return;
5485 }
5486
5487 if (wpa_s->p2p_in_provisioning) {
5488 /*
5489 * Use the normal group formation timeout during the
5490 * provisioning phase to avoid terminating this process too
5491 * early due to group idle timeout.
5492 */
5493 wpa_printf(MSG_DEBUG, "P2P: Do not use P2P group idle timeout "
5494 "during provisioning");
5495 return;
5496 }
Deepthi Gowri9e4e8ac2013-04-16 11:57:05 +05305497
Dmitry Shmidt04949592012-07-19 12:16:46 -07005498 if (wpa_s->show_group_started) {
5499 /*
5500 * Use the normal group formation timeout between the end of
5501 * the provisioning phase and completion of 4-way handshake to
5502 * avoid terminating this process too early due to group idle
5503 * timeout.
5504 */
5505 wpa_printf(MSG_DEBUG, "P2P: Do not use P2P group idle timeout "
5506 "while waiting for initial 4-way handshake to "
5507 "complete");
5508 return;
5509 }
5510
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005511 wpa_printf(MSG_DEBUG, "P2P: Set P2P group idle timeout to %u seconds",
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005512 timeout);
5513 eloop_register_timeout(timeout, 0, wpas_p2p_group_idle_timeout,
5514 wpa_s, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005515}
5516
5517
Jouni Malinen2b89da82012-08-31 22:04:41 +03005518/* Returns 1 if the interface was removed */
5519int wpas_p2p_deauth_notif(struct wpa_supplicant *wpa_s, const u8 *bssid,
5520 u16 reason_code, const u8 *ie, size_t ie_len,
5521 int locally_generated)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005522{
5523 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
Jouni Malinen2b89da82012-08-31 22:04:41 +03005524 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005525 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
Jouni Malinen2b89da82012-08-31 22:04:41 +03005526 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005527
Dmitry Shmidt04949592012-07-19 12:16:46 -07005528 if (!locally_generated)
5529 p2p_deauth_notif(wpa_s->global->p2p, bssid, reason_code, ie,
5530 ie_len);
5531
5532 if (reason_code == WLAN_REASON_DEAUTH_LEAVING && !locally_generated &&
5533 wpa_s->current_ssid &&
5534 wpa_s->current_ssid->p2p_group &&
5535 wpa_s->current_ssid->mode == WPAS_MODE_INFRA) {
5536 wpa_printf(MSG_DEBUG, "P2P: GO indicated that the P2P Group "
5537 "session is ending");
Jouni Malinen2b89da82012-08-31 22:04:41 +03005538 if (wpas_p2p_group_delete(wpa_s,
5539 P2P_GROUP_REMOVAL_GO_ENDING_SESSION)
5540 > 0)
5541 return 1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07005542 }
Jouni Malinen2b89da82012-08-31 22:04:41 +03005543
5544 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005545}
5546
5547
5548void wpas_p2p_disassoc_notif(struct wpa_supplicant *wpa_s, const u8 *bssid,
Dmitry Shmidt04949592012-07-19 12:16:46 -07005549 u16 reason_code, const u8 *ie, size_t ie_len,
5550 int locally_generated)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005551{
5552 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
5553 return;
5554 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
5555 return;
5556
Dmitry Shmidt04949592012-07-19 12:16:46 -07005557 if (!locally_generated)
5558 p2p_disassoc_notif(wpa_s->global->p2p, bssid, reason_code, ie,
5559 ie_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005560}
5561
5562
5563void wpas_p2p_update_config(struct wpa_supplicant *wpa_s)
5564{
5565 struct p2p_data *p2p = wpa_s->global->p2p;
5566
5567 if (p2p == NULL)
5568 return;
5569
5570 if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_CAPABLE))
5571 return;
5572
5573 if (wpa_s->conf->changed_parameters & CFG_CHANGED_DEVICE_NAME)
5574 p2p_set_dev_name(p2p, wpa_s->conf->device_name);
5575
5576 if (wpa_s->conf->changed_parameters & CFG_CHANGED_DEVICE_TYPE)
5577 p2p_set_pri_dev_type(p2p, wpa_s->conf->device_type);
5578
5579 if (wpa_s->wps &&
5580 (wpa_s->conf->changed_parameters & CFG_CHANGED_CONFIG_METHODS))
5581 p2p_set_config_methods(p2p, wpa_s->wps->config_methods);
5582
5583 if (wpa_s->wps && (wpa_s->conf->changed_parameters & CFG_CHANGED_UUID))
5584 p2p_set_uuid(p2p, wpa_s->wps->uuid);
5585
5586 if (wpa_s->conf->changed_parameters & CFG_CHANGED_WPS_STRING) {
5587 p2p_set_manufacturer(p2p, wpa_s->conf->manufacturer);
5588 p2p_set_model_name(p2p, wpa_s->conf->model_name);
5589 p2p_set_model_number(p2p, wpa_s->conf->model_number);
5590 p2p_set_serial_number(p2p, wpa_s->conf->serial_number);
5591 }
5592
5593 if (wpa_s->conf->changed_parameters & CFG_CHANGED_SEC_DEVICE_TYPE)
5594 p2p_set_sec_dev_types(p2p,
5595 (void *) wpa_s->conf->sec_device_type,
5596 wpa_s->conf->num_sec_device_types);
5597
5598 if (wpa_s->conf->changed_parameters & CFG_CHANGED_VENDOR_EXTENSION) {
5599 int i;
5600 p2p_remove_wps_vendor_extensions(p2p);
5601 for (i = 0; i < MAX_WPS_VENDOR_EXT; i++) {
5602 if (wpa_s->conf->wps_vendor_ext[i] == NULL)
5603 continue;
5604 p2p_add_wps_vendor_extension(
5605 p2p, wpa_s->conf->wps_vendor_ext[i]);
5606 }
5607 }
5608
5609 if ((wpa_s->conf->changed_parameters & CFG_CHANGED_COUNTRY) &&
5610 wpa_s->conf->country[0] && wpa_s->conf->country[1]) {
5611 char country[3];
5612 country[0] = wpa_s->conf->country[0];
5613 country[1] = wpa_s->conf->country[1];
5614 country[2] = 0x04;
5615 p2p_set_country(p2p, country);
5616 }
5617
5618 if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_SSID_POSTFIX) {
5619 p2p_set_ssid_postfix(p2p, (u8 *) wpa_s->conf->p2p_ssid_postfix,
5620 wpa_s->conf->p2p_ssid_postfix ?
5621 os_strlen(wpa_s->conf->p2p_ssid_postfix) :
5622 0);
5623 }
5624
5625 if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_INTRA_BSS)
5626 p2p_set_intra_bss_dist(p2p, wpa_s->conf->p2p_intra_bss);
Jouni Malinen75ecf522011-06-27 15:19:46 -07005627
5628 if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_LISTEN_CHANNEL) {
5629 u8 reg_class, channel;
5630 int ret;
5631 unsigned int r;
5632 if (wpa_s->conf->p2p_listen_reg_class &&
5633 wpa_s->conf->p2p_listen_channel) {
5634 reg_class = wpa_s->conf->p2p_listen_reg_class;
5635 channel = wpa_s->conf->p2p_listen_channel;
5636 } else {
5637 reg_class = 81;
5638 /*
5639 * Pick one of the social channels randomly as the
5640 * listen channel.
5641 */
5642 os_get_random((u8 *) &r, sizeof(r));
5643 channel = 1 + (r % 3) * 5;
5644 }
5645 ret = p2p_set_listen_channel(p2p, reg_class, channel);
5646 if (ret)
5647 wpa_printf(MSG_ERROR, "P2P: Own listen channel update "
5648 "failed: %d", ret);
5649 }
5650 if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_OPER_CHANNEL) {
5651 u8 op_reg_class, op_channel, cfg_op_channel;
5652 int ret = 0;
5653 unsigned int r;
5654 if (wpa_s->conf->p2p_oper_reg_class &&
5655 wpa_s->conf->p2p_oper_channel) {
5656 op_reg_class = wpa_s->conf->p2p_oper_reg_class;
5657 op_channel = wpa_s->conf->p2p_oper_channel;
5658 cfg_op_channel = 1;
5659 } else {
5660 op_reg_class = 81;
5661 /*
5662 * Use random operation channel from (1, 6, 11)
5663 *if no other preference is indicated.
5664 */
5665 os_get_random((u8 *) &r, sizeof(r));
5666 op_channel = 1 + (r % 3) * 5;
5667 cfg_op_channel = 0;
5668 }
5669 ret = p2p_set_oper_channel(p2p, op_reg_class, op_channel,
5670 cfg_op_channel);
5671 if (ret)
5672 wpa_printf(MSG_ERROR, "P2P: Own oper channel update "
5673 "failed: %d", ret);
5674 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07005675
5676 if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_PREF_CHAN) {
5677 if (p2p_set_pref_chan(p2p, wpa_s->conf->num_p2p_pref_chan,
5678 wpa_s->conf->p2p_pref_chan) < 0) {
5679 wpa_printf(MSG_ERROR, "P2P: Preferred channel list "
5680 "update failed");
5681 }
5682 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005683}
5684
5685
5686int wpas_p2p_set_noa(struct wpa_supplicant *wpa_s, u8 count, int start,
5687 int duration)
5688{
5689 if (!wpa_s->ap_iface)
5690 return -1;
5691 return hostapd_p2p_set_noa(wpa_s->ap_iface->bss[0], count, start,
5692 duration);
5693}
5694
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005695
5696int wpas_p2p_set_cross_connect(struct wpa_supplicant *wpa_s, int enabled)
5697{
5698 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
5699 return -1;
5700 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
5701 return -1;
5702
5703 wpa_s->global->cross_connection = enabled;
5704 p2p_set_cross_connect(wpa_s->global->p2p, enabled);
5705
5706 if (!enabled) {
5707 struct wpa_supplicant *iface;
5708
5709 for (iface = wpa_s->global->ifaces; iface; iface = iface->next)
5710 {
5711 if (iface->cross_connect_enabled == 0)
5712 continue;
5713
5714 iface->cross_connect_enabled = 0;
5715 iface->cross_connect_in_use = 0;
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07005716 wpa_msg_global(iface->parent, MSG_INFO,
5717 P2P_EVENT_CROSS_CONNECT_DISABLE "%s %s",
5718 iface->ifname,
5719 iface->cross_connect_uplink);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005720 }
5721 }
5722
5723 return 0;
5724}
5725
5726
5727static void wpas_p2p_enable_cross_connect(struct wpa_supplicant *uplink)
5728{
5729 struct wpa_supplicant *iface;
5730
5731 if (!uplink->global->cross_connection)
5732 return;
5733
5734 for (iface = uplink->global->ifaces; iface; iface = iface->next) {
5735 if (!iface->cross_connect_enabled)
5736 continue;
5737 if (os_strcmp(uplink->ifname, iface->cross_connect_uplink) !=
5738 0)
5739 continue;
5740 if (iface->ap_iface == NULL)
5741 continue;
5742 if (iface->cross_connect_in_use)
5743 continue;
5744
5745 iface->cross_connect_in_use = 1;
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07005746 wpa_msg_global(iface->parent, MSG_INFO,
5747 P2P_EVENT_CROSS_CONNECT_ENABLE "%s %s",
5748 iface->ifname, iface->cross_connect_uplink);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005749 }
5750}
5751
5752
5753static void wpas_p2p_disable_cross_connect(struct wpa_supplicant *uplink)
5754{
5755 struct wpa_supplicant *iface;
5756
5757 for (iface = uplink->global->ifaces; iface; iface = iface->next) {
5758 if (!iface->cross_connect_enabled)
5759 continue;
5760 if (os_strcmp(uplink->ifname, iface->cross_connect_uplink) !=
5761 0)
5762 continue;
5763 if (!iface->cross_connect_in_use)
5764 continue;
5765
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07005766 wpa_msg_global(iface->parent, MSG_INFO,
5767 P2P_EVENT_CROSS_CONNECT_DISABLE "%s %s",
5768 iface->ifname, iface->cross_connect_uplink);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005769 iface->cross_connect_in_use = 0;
5770 }
5771}
5772
5773
5774void wpas_p2p_notif_connected(struct wpa_supplicant *wpa_s)
5775{
5776 if (wpa_s->ap_iface || wpa_s->current_ssid == NULL ||
5777 wpa_s->current_ssid->mode != WPAS_MODE_INFRA ||
5778 wpa_s->cross_connect_disallowed)
5779 wpas_p2p_disable_cross_connect(wpa_s);
5780 else
5781 wpas_p2p_enable_cross_connect(wpa_s);
Dmitry Shmidt04949592012-07-19 12:16:46 -07005782 if (!wpa_s->ap_iface &&
5783 eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL) > 0)
5784 wpa_printf(MSG_DEBUG, "P2P: Cancelled P2P group idle timeout");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005785}
5786
5787
5788void wpas_p2p_notif_disconnected(struct wpa_supplicant *wpa_s)
5789{
5790 wpas_p2p_disable_cross_connect(wpa_s);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005791 if (!wpa_s->ap_iface &&
5792 !eloop_is_timeout_registered(wpas_p2p_group_idle_timeout,
5793 wpa_s, NULL))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005794 wpas_p2p_set_group_idle_timeout(wpa_s);
5795}
5796
5797
5798static void wpas_p2p_cross_connect_setup(struct wpa_supplicant *wpa_s)
5799{
5800 struct wpa_supplicant *iface;
5801
5802 if (!wpa_s->global->cross_connection)
5803 return;
5804
5805 for (iface = wpa_s->global->ifaces; iface; iface = iface->next) {
5806 if (iface == wpa_s)
5807 continue;
5808 if (iface->drv_flags &
5809 WPA_DRIVER_FLAGS_P2P_DEDICATED_INTERFACE)
5810 continue;
5811 if (iface->drv_flags & WPA_DRIVER_FLAGS_P2P_CAPABLE)
5812 continue;
5813
5814 wpa_s->cross_connect_enabled = 1;
5815 os_strlcpy(wpa_s->cross_connect_uplink, iface->ifname,
5816 sizeof(wpa_s->cross_connect_uplink));
5817 wpa_printf(MSG_DEBUG, "P2P: Enable cross connection from "
5818 "%s to %s whenever uplink is available",
5819 wpa_s->ifname, wpa_s->cross_connect_uplink);
5820
5821 if (iface->ap_iface || iface->current_ssid == NULL ||
5822 iface->current_ssid->mode != WPAS_MODE_INFRA ||
5823 iface->cross_connect_disallowed ||
5824 iface->wpa_state != WPA_COMPLETED)
5825 break;
5826
5827 wpa_s->cross_connect_in_use = 1;
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07005828 wpa_msg_global(wpa_s->parent, MSG_INFO,
5829 P2P_EVENT_CROSS_CONNECT_ENABLE "%s %s",
5830 wpa_s->ifname, wpa_s->cross_connect_uplink);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005831 break;
5832 }
5833}
5834
5835
5836int wpas_p2p_notif_pbc_overlap(struct wpa_supplicant *wpa_s)
5837{
5838 if (wpa_s->p2p_group_interface != P2P_GROUP_INTERFACE_CLIENT &&
5839 !wpa_s->p2p_in_provisioning)
5840 return 0; /* not P2P client operation */
5841
5842 wpa_printf(MSG_DEBUG, "P2P: Terminate connection due to WPS PBC "
5843 "session overlap");
5844 if (wpa_s != wpa_s->parent)
5845 wpa_msg_ctrl(wpa_s->parent, MSG_INFO, WPS_EVENT_OVERLAP);
Dmitry Shmidt34af3062013-07-11 10:46:32 -07005846 wpas_p2p_group_formation_failed(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005847 return 1;
5848}
5849
5850
5851void wpas_p2p_update_channel_list(struct wpa_supplicant *wpa_s)
5852{
5853 struct p2p_channels chan;
5854
5855 if (wpa_s->global == NULL || wpa_s->global->p2p == NULL)
5856 return;
5857
5858 os_memset(&chan, 0, sizeof(chan));
5859 if (wpas_p2p_setup_channels(wpa_s, &chan)) {
5860 wpa_printf(MSG_ERROR, "P2P: Failed to update supported "
5861 "channel list");
5862 return;
5863 }
5864
5865 p2p_update_channel_list(wpa_s->global->p2p, &chan);
5866}
5867
5868
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08005869static void wpas_p2p_scan_res_ignore(struct wpa_supplicant *wpa_s,
5870 struct wpa_scan_results *scan_res)
5871{
5872 wpa_printf(MSG_DEBUG, "P2P: Ignore scan results");
5873}
5874
5875
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005876int wpas_p2p_cancel(struct wpa_supplicant *wpa_s)
5877{
5878 struct wpa_global *global = wpa_s->global;
5879 int found = 0;
5880 const u8 *peer;
5881
5882 if (global->p2p == NULL)
5883 return -1;
5884
5885 wpa_printf(MSG_DEBUG, "P2P: Request to cancel group formation");
5886
5887 if (wpa_s->pending_interface_name[0] &&
5888 !is_zero_ether_addr(wpa_s->pending_interface_addr))
5889 found = 1;
5890
5891 peer = p2p_get_go_neg_peer(global->p2p);
5892 if (peer) {
5893 wpa_printf(MSG_DEBUG, "P2P: Unauthorize pending GO Neg peer "
5894 MACSTR, MAC2STR(peer));
5895 p2p_unauthorize(global->p2p, peer);
Dmitry Shmidt04949592012-07-19 12:16:46 -07005896 found = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005897 }
5898
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08005899 if (wpa_s->scan_res_handler == wpas_p2p_scan_res_join) {
5900 wpa_printf(MSG_DEBUG, "P2P: Stop pending scan for join");
5901 wpa_s->scan_res_handler = wpas_p2p_scan_res_ignore;
5902 found = 1;
5903 }
5904
5905 if (wpa_s->pending_pd_before_join) {
5906 wpa_printf(MSG_DEBUG, "P2P: Stop pending PD before join");
5907 wpa_s->pending_pd_before_join = 0;
5908 found = 1;
5909 }
5910
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005911 wpas_p2p_stop_find(wpa_s);
5912
5913 for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
5914 if (wpa_s == global->p2p_group_formation &&
5915 (wpa_s->p2p_in_provisioning ||
5916 wpa_s->parent->pending_interface_type ==
5917 WPA_IF_P2P_CLIENT)) {
5918 wpa_printf(MSG_DEBUG, "P2P: Interface %s in group "
5919 "formation found - cancelling",
5920 wpa_s->ifname);
5921 found = 1;
5922 eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
5923 wpa_s->parent, NULL);
Jouni Malinenadddfc42012-10-03 14:31:41 -07005924 if (wpa_s->p2p_in_provisioning) {
5925 wpas_group_formation_completed(wpa_s, 0);
5926 break;
5927 }
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005928 wpas_p2p_group_delete(wpa_s,
5929 P2P_GROUP_REMOVAL_REQUESTED);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005930 break;
5931 }
5932 }
5933
5934 if (!found) {
5935 wpa_printf(MSG_DEBUG, "P2P: No ongoing group formation found");
5936 return -1;
5937 }
5938
5939 return 0;
5940}
5941
5942
5943void wpas_p2p_interface_unavailable(struct wpa_supplicant *wpa_s)
5944{
5945 if (wpa_s->current_ssid == NULL || !wpa_s->current_ssid->p2p_group)
5946 return;
5947
5948 wpa_printf(MSG_DEBUG, "P2P: Remove group due to driver resource not "
5949 "being available anymore");
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005950 wpas_p2p_group_delete(wpa_s, P2P_GROUP_REMOVAL_UNAVAILABLE);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005951}
5952
5953
5954void wpas_p2p_update_best_channels(struct wpa_supplicant *wpa_s,
5955 int freq_24, int freq_5, int freq_overall)
5956{
5957 struct p2p_data *p2p = wpa_s->global->p2p;
5958 if (p2p == NULL || (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT))
5959 return;
5960 p2p_set_best_channels(p2p, freq_24, freq_5, freq_overall);
5961}
5962
5963
5964int wpas_p2p_unauthorize(struct wpa_supplicant *wpa_s, const char *addr)
5965{
5966 u8 peer[ETH_ALEN];
5967 struct p2p_data *p2p = wpa_s->global->p2p;
5968
5969 if (p2p == NULL || (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT))
5970 return -1;
5971
5972 if (hwaddr_aton(addr, peer))
5973 return -1;
5974
5975 return p2p_unauthorize(p2p, peer);
5976}
5977
5978
5979/**
5980 * wpas_p2p_disconnect - Disconnect from a P2P Group
5981 * @wpa_s: Pointer to wpa_supplicant data
5982 * Returns: 0 on success, -1 on failure
5983 *
5984 * This can be used to disconnect from a group in which the local end is a P2P
5985 * Client or to end a P2P Group in case the local end is the Group Owner. If a
5986 * virtual network interface was created for this group, that interface will be
5987 * removed. Otherwise, only the configured P2P group network will be removed
5988 * from the interface.
5989 */
5990int wpas_p2p_disconnect(struct wpa_supplicant *wpa_s)
5991{
5992
5993 if (wpa_s == NULL)
5994 return -1;
5995
Jouni Malinen2b89da82012-08-31 22:04:41 +03005996 return wpas_p2p_group_delete(wpa_s, P2P_GROUP_REMOVAL_REQUESTED) < 0 ?
5997 -1 : 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005998}
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005999
6000
6001int wpas_p2p_in_progress(struct wpa_supplicant *wpa_s)
6002{
Dmitry Shmidtf8623282013-02-20 14:34:59 -08006003 int ret;
6004
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006005 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
6006 return 0;
6007
Dmitry Shmidtf8623282013-02-20 14:34:59 -08006008 ret = p2p_in_progress(wpa_s->global->p2p);
6009 if (ret == 0) {
6010 /*
6011 * Check whether there is an ongoing WPS provisioning step (or
6012 * other parts of group formation) on another interface since
6013 * p2p_in_progress() does not report this to avoid issues for
6014 * scans during such provisioning step.
6015 */
6016 if (wpa_s->global->p2p_group_formation &&
6017 wpa_s->global->p2p_group_formation != wpa_s) {
6018 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Another interface (%s) "
6019 "in group formation",
6020 wpa_s->global->p2p_group_formation->ifname);
6021 ret = 1;
6022 }
6023 }
6024
Dmitry Shmidt92c368d2013-08-29 12:37:21 -07006025 if (!ret && wpa_s->global->p2p_go_wait_client.sec) {
6026 struct os_time now;
6027 os_get_time(&now);
6028 if (now.sec > wpa_s->global->p2p_go_wait_client.sec +
6029 P2P_MAX_INITIAL_CONN_WAIT_GO) {
6030 /* Wait for the first client has expired */
6031 wpa_s->global->p2p_go_wait_client.sec = 0;
6032 } else {
6033 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Waiting for initial client connection during group formation");
6034 ret = 1;
6035 }
6036 }
6037
Dmitry Shmidtf8623282013-02-20 14:34:59 -08006038 return ret;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006039}
6040
6041
6042void wpas_p2p_network_removed(struct wpa_supplicant *wpa_s,
6043 struct wpa_ssid *ssid)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006044{
6045 if (wpa_s->p2p_in_provisioning && ssid->p2p_group &&
6046 eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
6047 wpa_s->parent, NULL) > 0) {
Dmitry Shmidt04949592012-07-19 12:16:46 -07006048 /**
6049 * Remove the network by scheduling the group formation
6050 * timeout to happen immediately. The teardown code
6051 * needs to be scheduled to run asynch later so that we
6052 * don't delete data from under ourselves unexpectedly.
6053 * Calling wpas_p2p_group_formation_timeout directly
6054 * causes a series of crashes in WPS failure scenarios.
6055 */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006056 wpa_printf(MSG_DEBUG, "P2P: Canceled group formation due to "
6057 "P2P group network getting removed");
Dmitry Shmidt04949592012-07-19 12:16:46 -07006058 eloop_register_timeout(0, 0, wpas_p2p_group_formation_timeout,
6059 wpa_s->parent, NULL);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006060 }
6061}
6062
6063
6064struct wpa_ssid * wpas_p2p_get_persistent(struct wpa_supplicant *wpa_s,
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08006065 const u8 *addr, const u8 *ssid,
6066 size_t ssid_len)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006067{
6068 struct wpa_ssid *s;
6069 size_t i;
6070
6071 for (s = wpa_s->conf->ssid; s; s = s->next) {
6072 if (s->disabled != 2)
6073 continue;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08006074 if (ssid &&
6075 (ssid_len != s->ssid_len ||
6076 os_memcmp(ssid, s->ssid, ssid_len) != 0))
6077 continue;
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07006078 if (addr == NULL) {
6079 if (s->mode == WPAS_MODE_P2P_GO)
6080 return s;
6081 continue;
6082 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006083 if (os_memcmp(s->bssid, addr, ETH_ALEN) == 0)
6084 return s; /* peer is GO in the persistent group */
6085 if (s->mode != WPAS_MODE_P2P_GO || s->p2p_client_list == NULL)
6086 continue;
6087 for (i = 0; i < s->num_p2p_clients; i++) {
6088 if (os_memcmp(s->p2p_client_list + i * ETH_ALEN,
6089 addr, ETH_ALEN) == 0)
6090 return s; /* peer is P2P client in persistent
6091 * group */
6092 }
6093 }
6094
6095 return NULL;
6096}
6097
6098
6099void wpas_p2p_notify_ap_sta_authorized(struct wpa_supplicant *wpa_s,
6100 const u8 *addr)
6101{
Dmitry Shmidt56052862013-10-04 10:23:25 -07006102 if (eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
6103 wpa_s->parent, NULL) > 0) {
6104 /*
6105 * This can happen if WPS provisioning step is not terminated
6106 * cleanly (e.g., P2P Client does not send WSC_Done). Since the
6107 * peer was able to connect, there is no need to time out group
6108 * formation after this, though. In addition, this is used with
6109 * the initial connection wait on the GO as a separate formation
6110 * timeout and as such, expected to be hit after the initial WPS
6111 * provisioning step.
6112 */
6113 wpa_printf(MSG_DEBUG, "P2P: Canceled P2P group formation timeout on data connection");
6114 }
6115 if (!wpa_s->p2p_go_group_formation_completed) {
6116 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Marking group formation completed on GO on first data connection");
6117 wpa_s->p2p_go_group_formation_completed = 1;
6118 wpa_s->global->p2p_group_formation = NULL;
6119 wpa_s->p2p_in_provisioning = 0;
6120 }
Dmitry Shmidt92c368d2013-08-29 12:37:21 -07006121 wpa_s->global->p2p_go_wait_client.sec = 0;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006122 if (addr == NULL)
6123 return;
6124 wpas_p2p_add_persistent_group_client(wpa_s, addr);
6125}
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08006126
Dmitry Shmidt04949592012-07-19 12:16:46 -07006127
6128static void wpas_p2p_fallback_to_go_neg(struct wpa_supplicant *wpa_s,
6129 int group_added)
6130{
6131 struct wpa_supplicant *group = wpa_s;
Dmitry Shmidt04949592012-07-19 12:16:46 -07006132 if (wpa_s->global->p2p_group_formation)
6133 group = wpa_s->global->p2p_group_formation;
6134 wpa_s = wpa_s->parent;
6135 offchannel_send_action_done(wpa_s);
6136 if (group_added)
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07006137 wpas_p2p_group_delete(group, P2P_GROUP_REMOVAL_SILENT);
Dmitry Shmidt04949592012-07-19 12:16:46 -07006138 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Fall back to GO Negotiation");
6139 wpas_p2p_connect(wpa_s, wpa_s->pending_join_dev_addr, wpa_s->p2p_pin,
6140 wpa_s->p2p_wps_method, wpa_s->p2p_persistent_group, 0,
6141 0, 0, wpa_s->p2p_go_intent, wpa_s->p2p_connect_freq,
6142 wpa_s->p2p_persistent_id,
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07006143 wpa_s->p2p_pd_before_go_neg,
6144 wpa_s->p2p_go_ht40);
Dmitry Shmidt04949592012-07-19 12:16:46 -07006145}
6146
6147
6148int wpas_p2p_scan_no_go_seen(struct wpa_supplicant *wpa_s)
6149{
6150 if (!wpa_s->p2p_fallback_to_go_neg ||
6151 wpa_s->p2p_in_provisioning <= 5)
6152 return 0;
6153
6154 if (wpas_p2p_peer_go(wpa_s, wpa_s->pending_join_dev_addr) > 0)
6155 return 0; /* peer operating as a GO */
6156
6157 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: GO not found for p2p_connect-auto - "
6158 "fallback to GO Negotiation");
6159 wpas_p2p_fallback_to_go_neg(wpa_s, 1);
6160
6161 return 1;
6162}
6163
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07006164
6165unsigned int wpas_p2p_search_delay(struct wpa_supplicant *wpa_s)
6166{
6167 const char *rn, *rn2;
6168 struct wpa_supplicant *ifs;
6169
6170 if (wpa_s->wpa_state > WPA_SCANNING) {
6171 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Use %u ms search delay due to "
6172 "concurrent operation",
6173 P2P_CONCURRENT_SEARCH_DELAY);
6174 return P2P_CONCURRENT_SEARCH_DELAY;
6175 }
6176
6177 if (!wpa_s->driver->get_radio_name)
6178 return 0;
6179 rn = wpa_s->driver->get_radio_name(wpa_s->drv_priv);
6180 if (rn == NULL || rn[0] == '\0')
6181 return 0;
6182
6183 for (ifs = wpa_s->global->ifaces; ifs; ifs = ifs->next) {
6184 if (ifs == wpa_s || !ifs->driver->get_radio_name)
6185 continue;
6186
6187 rn2 = ifs->driver->get_radio_name(ifs->drv_priv);
6188 if (!rn2 || os_strcmp(rn, rn2) != 0)
6189 continue;
6190 if (ifs->wpa_state > WPA_SCANNING) {
6191 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Use %u ms search "
6192 "delay due to concurrent operation on "
6193 "interface %s",
6194 P2P_CONCURRENT_SEARCH_DELAY, ifs->ifname);
6195 return P2P_CONCURRENT_SEARCH_DELAY;
6196 }
6197 }
6198
6199 return 0;
6200}
6201
Dmitry Shmidt37d4d6a2013-03-18 13:09:42 -07006202
6203void wpas_p2p_continue_after_scan(struct wpa_supplicant *wpa_s)
6204{
6205 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Station mode scan operation not "
6206 "pending anymore (sta_scan_pending=%d "
6207 "p2p_cb_on_scan_complete=%d)", wpa_s->sta_scan_pending,
6208 wpa_s->global->p2p_cb_on_scan_complete);
6209 wpa_s->sta_scan_pending = 0;
6210
6211 if (!wpa_s->global->p2p_cb_on_scan_complete)
6212 return;
6213 wpa_s->global->p2p_cb_on_scan_complete = 0;
6214
6215 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
6216 return;
6217
6218 if (p2p_other_scan_completed(wpa_s->global->p2p) == 1) {
6219 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Pending P2P operation "
6220 "continued after successful connection");
6221 p2p_increase_search_delay(wpa_s->global->p2p,
6222 wpas_p2p_search_delay(wpa_s));
6223 }
6224}
6225
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07006226
6227static int wpas_p2p_remove_psk_entry(struct wpa_supplicant *wpa_s,
6228 struct wpa_ssid *s, const u8 *addr,
6229 int iface_addr)
6230{
6231 struct psk_list_entry *psk, *tmp;
6232 int changed = 0;
6233
6234 dl_list_for_each_safe(psk, tmp, &s->psk_list, struct psk_list_entry,
6235 list) {
6236 if ((iface_addr && !psk->p2p &&
6237 os_memcmp(addr, psk->addr, ETH_ALEN) == 0) ||
6238 (!iface_addr && psk->p2p &&
6239 os_memcmp(addr, psk->addr, ETH_ALEN) == 0)) {
6240 wpa_dbg(wpa_s, MSG_DEBUG,
6241 "P2P: Remove persistent group PSK list entry for "
6242 MACSTR " p2p=%u",
6243 MAC2STR(psk->addr), psk->p2p);
6244 dl_list_del(&psk->list);
6245 os_free(psk);
6246 changed++;
6247 }
6248 }
6249
6250 return changed;
6251}
6252
6253
6254void wpas_p2p_new_psk_cb(struct wpa_supplicant *wpa_s, const u8 *mac_addr,
6255 const u8 *p2p_dev_addr,
6256 const u8 *psk, size_t psk_len)
6257{
6258 struct wpa_ssid *ssid = wpa_s->current_ssid;
6259 struct wpa_ssid *persistent;
6260 struct psk_list_entry *p;
6261
6262 if (psk_len != sizeof(p->psk))
6263 return;
6264
6265 if (p2p_dev_addr) {
6266 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: New PSK for addr=" MACSTR
6267 " p2p_dev_addr=" MACSTR,
6268 MAC2STR(mac_addr), MAC2STR(p2p_dev_addr));
6269 if (is_zero_ether_addr(p2p_dev_addr))
6270 p2p_dev_addr = NULL;
6271 } else {
6272 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: New PSK for addr=" MACSTR,
6273 MAC2STR(mac_addr));
6274 }
6275
6276 if (ssid->mode == WPAS_MODE_P2P_GROUP_FORMATION) {
6277 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: new_psk_cb during group formation");
6278 /* To be added to persistent group once created */
6279 if (wpa_s->global->add_psk == NULL) {
6280 wpa_s->global->add_psk = os_zalloc(sizeof(*p));
6281 if (wpa_s->global->add_psk == NULL)
6282 return;
6283 }
6284 p = wpa_s->global->add_psk;
6285 if (p2p_dev_addr) {
6286 p->p2p = 1;
6287 os_memcpy(p->addr, p2p_dev_addr, ETH_ALEN);
6288 } else {
6289 p->p2p = 0;
6290 os_memcpy(p->addr, mac_addr, ETH_ALEN);
6291 }
6292 os_memcpy(p->psk, psk, psk_len);
6293 return;
6294 }
6295
6296 if (ssid->mode != WPAS_MODE_P2P_GO || !ssid->p2p_persistent_group) {
6297 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Ignore new_psk_cb on not-persistent GO");
6298 return;
6299 }
6300
6301 persistent = wpas_p2p_get_persistent(wpa_s->parent, NULL, ssid->ssid,
6302 ssid->ssid_len);
6303 if (!persistent) {
6304 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Could not find persistent group information to store the new PSK");
6305 return;
6306 }
6307
6308 p = os_zalloc(sizeof(*p));
6309 if (p == NULL)
6310 return;
6311 if (p2p_dev_addr) {
6312 p->p2p = 1;
6313 os_memcpy(p->addr, p2p_dev_addr, ETH_ALEN);
6314 } else {
6315 p->p2p = 0;
6316 os_memcpy(p->addr, mac_addr, ETH_ALEN);
6317 }
6318 os_memcpy(p->psk, psk, psk_len);
6319
6320 if (dl_list_len(&persistent->psk_list) > P2P_MAX_STORED_CLIENTS) {
6321 struct psk_list_entry *last;
6322 last = dl_list_last(&persistent->psk_list,
6323 struct psk_list_entry, list);
6324 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Remove oldest PSK entry for "
6325 MACSTR " (p2p=%u) to make room for a new one",
6326 MAC2STR(last->addr), last->p2p);
6327 dl_list_del(&last->list);
6328 os_free(last);
6329 }
6330
6331 wpas_p2p_remove_psk_entry(wpa_s->parent, persistent,
6332 p2p_dev_addr ? p2p_dev_addr : mac_addr,
6333 p2p_dev_addr == NULL);
6334 if (p2p_dev_addr) {
6335 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Add new PSK for p2p_dev_addr="
6336 MACSTR, MAC2STR(p2p_dev_addr));
6337 } else {
6338 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Add new PSK for addr=" MACSTR,
6339 MAC2STR(mac_addr));
6340 }
6341 dl_list_add(&persistent->psk_list, &p->list);
6342
6343#ifndef CONFIG_NO_CONFIG_WRITE
6344 if (wpa_s->parent->conf->update_config &&
6345 wpa_config_write(wpa_s->parent->confname, wpa_s->parent->conf))
6346 wpa_printf(MSG_DEBUG, "P2P: Failed to update configuration");
6347#endif /* CONFIG_NO_CONFIG_WRITE */
6348}
6349
6350
6351static void wpas_p2p_remove_psk(struct wpa_supplicant *wpa_s,
6352 struct wpa_ssid *s, const u8 *addr,
6353 int iface_addr)
6354{
6355 int res;
6356
6357 res = wpas_p2p_remove_psk_entry(wpa_s, s, addr, iface_addr);
6358 if (res > 0) {
6359#ifndef CONFIG_NO_CONFIG_WRITE
6360 if (wpa_s->conf->update_config &&
6361 wpa_config_write(wpa_s->confname, wpa_s->conf))
6362 wpa_dbg(wpa_s, MSG_DEBUG,
6363 "P2P: Failed to update configuration");
6364#endif /* CONFIG_NO_CONFIG_WRITE */
6365 }
6366}
6367
6368
6369static void wpas_p2p_remove_client_go(struct wpa_supplicant *wpa_s,
6370 const u8 *peer, int iface_addr)
6371{
6372 struct hostapd_data *hapd;
6373 struct hostapd_wpa_psk *psk, *prev, *rem;
6374 struct sta_info *sta;
6375
6376 if (wpa_s->ap_iface == NULL || wpa_s->current_ssid == NULL ||
6377 wpa_s->current_ssid->mode != WPAS_MODE_P2P_GO)
6378 return;
6379
6380 /* Remove per-station PSK entry */
6381 hapd = wpa_s->ap_iface->bss[0];
6382 prev = NULL;
6383 psk = hapd->conf->ssid.wpa_psk;
6384 while (psk) {
6385 if ((iface_addr && os_memcmp(peer, psk->addr, ETH_ALEN) == 0) ||
6386 (!iface_addr &&
6387 os_memcmp(peer, psk->p2p_dev_addr, ETH_ALEN) == 0)) {
6388 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Remove operating group PSK entry for "
6389 MACSTR " iface_addr=%d",
6390 MAC2STR(peer), iface_addr);
6391 if (prev)
6392 prev->next = psk->next;
6393 else
6394 hapd->conf->ssid.wpa_psk = psk->next;
6395 rem = psk;
6396 psk = psk->next;
6397 os_free(rem);
6398 } else {
6399 prev = psk;
6400 psk = psk->next;
6401 }
6402 }
6403
6404 /* Disconnect from group */
6405 if (iface_addr)
6406 sta = ap_get_sta(hapd, peer);
6407 else
6408 sta = ap_get_sta_p2p(hapd, peer);
6409 if (sta) {
6410 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Disconnect peer " MACSTR
6411 " (iface_addr=%d) from group",
6412 MAC2STR(peer), iface_addr);
6413 hostapd_drv_sta_deauth(hapd, sta->addr,
6414 WLAN_REASON_DEAUTH_LEAVING);
6415 ap_sta_deauthenticate(hapd, sta, WLAN_REASON_DEAUTH_LEAVING);
6416 }
6417}
6418
6419
6420void wpas_p2p_remove_client(struct wpa_supplicant *wpa_s, const u8 *peer,
6421 int iface_addr)
6422{
6423 struct wpa_ssid *s;
6424 struct wpa_supplicant *w;
6425
6426 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Remove client " MACSTR, MAC2STR(peer));
6427
6428 /* Remove from any persistent group */
6429 for (s = wpa_s->parent->conf->ssid; s; s = s->next) {
6430 if (s->disabled != 2 || s->mode != WPAS_MODE_P2P_GO)
6431 continue;
6432 if (!iface_addr)
6433 wpas_remove_persistent_peer(wpa_s, s, peer, 0);
6434 wpas_p2p_remove_psk(wpa_s->parent, s, peer, iface_addr);
6435 }
6436
6437 /* Remove from any operating group */
6438 for (w = wpa_s->global->ifaces; w; w = w->next)
6439 wpas_p2p_remove_client_go(w, peer, iface_addr);
6440}
6441
6442
6443static void wpas_p2p_psk_failure_removal(void *eloop_ctx, void *timeout_ctx)
6444{
6445 struct wpa_supplicant *wpa_s = eloop_ctx;
6446 wpas_p2p_group_delete(wpa_s, P2P_GROUP_REMOVAL_PSK_FAILURE);
6447}
6448
6449
6450int wpas_p2p_4way_hs_failed(struct wpa_supplicant *wpa_s)
6451{
6452 struct wpa_ssid *ssid = wpa_s->current_ssid;
6453
6454 if (ssid == NULL || !ssid->p2p_group)
6455 return 0;
6456
6457 if (wpa_s->p2p_last_4way_hs_fail &&
6458 wpa_s->p2p_last_4way_hs_fail == ssid) {
6459 u8 go_dev_addr[ETH_ALEN];
6460 struct wpa_ssid *persistent;
6461
6462 if (wpas_p2p_persistent_group(wpa_s, go_dev_addr,
6463 ssid->ssid,
6464 ssid->ssid_len) <= 0) {
6465 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Could not determine whether 4-way handshake failures were for a persistent group");
6466 goto disconnect;
6467 }
6468
6469 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Two 4-way handshake failures for a P2P group - go_dev_addr="
6470 MACSTR, MAC2STR(go_dev_addr));
6471 persistent = wpas_p2p_get_persistent(wpa_s->parent, go_dev_addr,
6472 ssid->ssid,
6473 ssid->ssid_len);
6474 if (persistent == NULL || persistent->mode != WPAS_MODE_INFRA) {
6475 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: No matching persistent group stored");
6476 goto disconnect;
6477 }
6478 wpa_msg_global(wpa_s->parent, MSG_INFO,
6479 P2P_EVENT_PERSISTENT_PSK_FAIL "%d",
6480 persistent->id);
6481 disconnect:
6482 wpa_s->p2p_last_4way_hs_fail = NULL;
6483 /*
6484 * Remove the group from a timeout to avoid issues with caller
6485 * continuing to use the interface if this is on a P2P group
6486 * interface.
6487 */
6488 eloop_register_timeout(0, 0, wpas_p2p_psk_failure_removal,
6489 wpa_s, NULL);
6490 return 1;
6491 }
6492
6493 wpa_s->p2p_last_4way_hs_fail = ssid;
6494 return 0;
6495}
6496
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08006497#ifdef ANDROID_P2P
Dmitry Shmidt1cf45732013-04-29 17:36:45 -07006498static void wpas_p2p_group_freq_conflict(void *eloop_ctx, void *timeout_ctx)
6499{
6500 struct wpa_supplicant *wpa_s = eloop_ctx;
6501
6502 wpa_printf(MSG_DEBUG, "P2P: Frequency conflict - terminate group");
6503 wpas_p2p_group_delete(wpa_s, P2P_GROUP_REMOVAL_FREQ_CONFLICT);
6504}
6505
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08006506int wpas_p2p_handle_frequency_conflicts(struct wpa_supplicant *wpa_s, int freq,
6507 struct wpa_ssid *ssid)
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08006508{
6509 struct wpa_supplicant *iface = NULL;
6510 struct p2p_data *p2p = wpa_s->global->p2p;
6511
6512 for (iface = wpa_s->global->ifaces; iface; iface = iface->next) {
Jeff Johnson12b1cd92012-10-07 19:34:24 -07006513 if ((iface->current_ssid) &&
6514 (iface->current_ssid->frequency != freq) &&
6515 ((iface->p2p_group_interface) ||
6516 (iface->current_ssid->p2p_group))) {
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08006517
Jeff Johnson12b1cd92012-10-07 19:34:24 -07006518 if ((iface->p2p_group_interface == P2P_GROUP_INTERFACE_GO) ||
6519 (iface->current_ssid->mode == WPAS_MODE_P2P_GO)) {
6520 /* Try to see whether we can move the GO. If it
6521 * is not possible, remove the GO interface
6522 */
6523 if (wpa_drv_switch_channel(iface, freq) == 0) {
6524 wpa_printf(MSG_ERROR, "P2P: GO Moved to freq(%d)", freq);
6525 iface->current_ssid->frequency = freq;
6526 continue;
6527 }
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08006528 }
6529
6530 /* If GO cannot be moved or if the conflicting interface is a
6531 * P2P Client, remove the interface depending up on the connection
6532 * priority */
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08006533 if(!wpas_is_p2p_prioritized(iface)) {
Dmitry Shmidtf4f5db32012-09-11 14:36:56 -07006534 /* STA connection has priority over existing
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08006535 * P2P connection. So remove the interface */
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08006536 wpa_printf(MSG_DEBUG, "P2P: Removing P2P connection due to Single channel"
Dmitry Shmidt687922c2012-03-26 14:02:32 -07006537 "concurrent mode frequency conflict");
Dmitry Shmidt1cf45732013-04-29 17:36:45 -07006538 eloop_register_timeout(0, 0, wpas_p2p_group_freq_conflict,
6539 iface, NULL);
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08006540 /* If connection in progress is p2p connection, do not proceed for the connection */
6541 if (wpa_s == iface)
6542 return -1;
6543 else
6544 /* If connection in progress is STA connection, proceed for the connection */
6545 return 0;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08006546 } else {
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08006547 /* P2p connection has priority, disable the STA network*/
6548 wpa_supplicant_disable_network(wpa_s->global->ifaces, ssid);
6549 wpa_msg(wpa_s->global->ifaces, MSG_INFO, WPA_EVENT_FREQ_CONFLICT
6550 " id=%d", ssid->id);
6551 os_memset(wpa_s->global->ifaces->pending_bssid, 0, ETH_ALEN);
6552 if (wpa_s == iface) {
6553 /* p2p connection is in progress, continue connecting...*/
6554 return 0;
6555 }
6556 else {
6557 /* STA connection is in progress, do not allow to continue */
6558 return -1;
6559 }
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08006560 }
6561 }
6562 }
6563 return 0;
6564}
6565#endif