blob: cd1ff12456c37e88577956f8412fc95b82e7dc11 [file] [log] [blame]
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001/*
2 * wpa_supplicant - P2P
3 * Copyright (c) 2009-2010, Atheros Communications
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08004 * Copyright (c) 2010-2014, Jouni Malinen <j@w1.fi>
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005 *
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08006 * This software may be distributed under the terms of the BSD license.
7 * See README for more details.
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008 */
9
10#include "includes.h"
11
12#include "common.h"
13#include "eloop.h"
14#include "common/ieee802_11_common.h"
15#include "common/ieee802_11_defs.h"
16#include "common/wpa_ctrl.h"
17#include "wps/wps_i.h"
18#include "p2p/p2p.h"
19#include "ap/hostapd.h"
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080020#include "ap/ap_config.h"
Dmitry Shmidt391c59f2013-09-03 12:16:28 -070021#include "ap/sta_info.h"
22#include "ap/ap_drv_ops.h"
Dmitry Shmidtcf32e602014-01-28 10:57:39 -080023#include "ap/wps_hostapd.h"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070024#include "ap/p2p_hostapd.h"
Jouni Malinen75ecf522011-06-27 15:19:46 -070025#include "eapol_supp/eapol_supp_sm.h"
26#include "rsn_supp/wpa.h"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070027#include "wpa_supplicant_i.h"
28#include "driver_i.h"
29#include "ap.h"
30#include "config_ssid.h"
31#include "config.h"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070032#include "notify.h"
33#include "scan.h"
34#include "bss.h"
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080035#include "offchannel.h"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070036#include "wps_supplicant.h"
37#include "p2p_supplicant.h"
Dmitry Shmidt04f534e2013-12-09 15:50:16 -080038#include "wifi_display.h"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070039
40
41/*
42 * How many times to try to scan to find the GO before giving up on join
43 * request.
44 */
45#define P2P_MAX_JOIN_SCAN_ATTEMPTS 10
46
Dmitry Shmidt04949592012-07-19 12:16:46 -070047#define P2P_AUTO_PD_SCAN_ATTEMPTS 5
48
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080049#ifndef P2P_MAX_CLIENT_IDLE
50/*
51 * How many seconds to try to reconnect to the GO when connection in P2P client
52 * role has been lost.
53 */
54#define P2P_MAX_CLIENT_IDLE 10
55#endif /* P2P_MAX_CLIENT_IDLE */
56
Dmitry Shmidt04949592012-07-19 12:16:46 -070057#ifndef P2P_MAX_INITIAL_CONN_WAIT
58/*
59 * How many seconds to wait for initial 4-way handshake to get completed after
Dmitry Shmidt15907092014-03-25 10:42:57 -070060 * WPS provisioning step or after the re-invocation of a persistent group on a
61 * P2P Client.
Dmitry Shmidt04949592012-07-19 12:16:46 -070062 */
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 Shmidt04f534e2013-12-09 15:50:16 -080099 P2P_GROUP_REMOVAL_PSK_FAILURE,
100 P2P_GROUP_REMOVAL_FREQ_CONFLICT
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700101};
102
Jouni Malinendc7b7132012-09-14 12:53:47 -0700103
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700104static void wpas_p2p_long_listen_timeout(void *eloop_ctx, void *timeout_ctx);
105static struct wpa_supplicant *
106wpas_p2p_get_group_iface(struct wpa_supplicant *wpa_s, int addr_allocated,
107 int go);
Dmitry Shmidtcf32e602014-01-28 10:57:39 -0800108static int wpas_p2p_join_start(struct wpa_supplicant *wpa_s, int freq,
109 const u8 *ssid, size_t ssid_len);
Dmitry Shmidt344abd32014-01-14 13:17:00 -0800110static void wpas_p2p_join_scan_req(struct wpa_supplicant *wpa_s, int freq,
111 const u8 *ssid, size_t ssid_len);
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,
Dmitry Shmidtcf32e602014-01-28 10:57:39 -0800115 int auto_join, int freq,
116 const u8 *ssid, size_t ssid_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700117static int wpas_p2p_create_iface(struct wpa_supplicant *wpa_s);
118static void wpas_p2p_cross_connect_setup(struct wpa_supplicant *wpa_s);
119static void wpas_p2p_group_idle_timeout(void *eloop_ctx, void *timeout_ctx);
120static void wpas_p2p_set_group_idle_timeout(struct wpa_supplicant *wpa_s);
Dmitry Shmidtf8623282013-02-20 14:34:59 -0800121static void wpas_p2p_group_formation_timeout(void *eloop_ctx,
122 void *timeout_ctx);
Dmitry Shmidt04f534e2013-12-09 15:50:16 -0800123static void wpas_p2p_group_freq_conflict(void *eloop_ctx, void *timeout_ctx);
Dmitry Shmidt04949592012-07-19 12:16:46 -0700124static void wpas_p2p_fallback_to_go_neg(struct wpa_supplicant *wpa_s,
125 int group_added);
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800126static int wpas_p2p_stop_find_oper(struct wpa_supplicant *wpa_s);
Dmitry Shmidtbd14a572014-02-18 10:33:49 -0800127static void wpas_stop_listen(void *ctx);
Dmitry Shmidt684785c2014-05-12 13:34:29 -0700128static void wpas_p2p_psk_failure_removal(void *eloop_ctx, void *timeout_ctx);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700129
130
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -0700131/*
132 * Get the number of concurrent channels that the HW can operate, but that are
133 * currently not in use by any of the wpa_supplicant interfaces.
134 */
135static int wpas_p2p_num_unused_channels(struct wpa_supplicant *wpa_s)
136{
137 int *freqs;
Dmitry Shmidtb96dad42013-11-05 10:07:29 -0800138 int num, unused;
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -0700139
140 freqs = os_calloc(wpa_s->num_multichan_concurrent, sizeof(int));
141 if (!freqs)
142 return -1;
143
144 num = get_shared_radio_freqs(wpa_s, freqs,
145 wpa_s->num_multichan_concurrent);
146 os_free(freqs);
147
Dmitry Shmidtb96dad42013-11-05 10:07:29 -0800148 unused = wpa_s->num_multichan_concurrent - num;
149 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: num_unused_channels: %d", unused);
150 return unused;
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -0700151}
152
153
154/*
155 * Get the frequencies that are currently in use by one or more of the virtual
156 * interfaces, and that are also valid for P2P operation.
157 */
158static int wpas_p2p_valid_oper_freqs(struct wpa_supplicant *wpa_s,
159 int *p2p_freqs, unsigned int len)
160{
161 int *freqs;
162 unsigned int num, i, j;
163
164 freqs = os_calloc(wpa_s->num_multichan_concurrent, sizeof(int));
165 if (!freqs)
166 return -1;
167
168 num = get_shared_radio_freqs(wpa_s, freqs,
169 wpa_s->num_multichan_concurrent);
170
171 os_memset(p2p_freqs, 0, sizeof(int) * len);
172
173 for (i = 0, j = 0; i < num && j < len; i++) {
174 if (p2p_supported_freq(wpa_s->global->p2p, freqs[i]))
175 p2p_freqs[j++] = freqs[i];
176 }
177
178 os_free(freqs);
179
Dmitry Shmidtb96dad42013-11-05 10:07:29 -0800180 dump_freq_array(wpa_s, "valid for P2P", p2p_freqs, j);
181
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -0700182 return j;
183}
184
185
Dmitry Shmidt700a1372013-03-15 14:14:44 -0700186static void wpas_p2p_set_own_freq_preference(struct wpa_supplicant *wpa_s,
187 int freq)
188{
189 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
190 return;
Dmitry Shmidta0d265f2013-11-19 13:13:41 -0800191 if (wpa_s->parent->conf->p2p_ignore_shared_freq &&
192 freq > 0 && wpa_s->num_multichan_concurrent > 1 &&
193 wpas_p2p_num_unused_channels(wpa_s) > 0) {
194 wpa_printf(MSG_DEBUG, "P2P: Ignore own channel preference %d MHz due to p2p_ignore_shared_freq=1 configuration",
195 freq);
Dmitry Shmidt700a1372013-03-15 14:14:44 -0700196 freq = 0;
Dmitry Shmidta0d265f2013-11-19 13:13:41 -0800197 }
Dmitry Shmidt700a1372013-03-15 14:14:44 -0700198 p2p_set_own_freq_preference(wpa_s->global->p2p, freq);
199}
200
201
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700202static void wpas_p2p_scan_res_handler(struct wpa_supplicant *wpa_s,
203 struct wpa_scan_results *scan_res)
204{
205 size_t i;
206
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800207 if (wpa_s->p2p_scan_work) {
208 struct wpa_radio_work *work = wpa_s->p2p_scan_work;
209 wpa_s->p2p_scan_work = NULL;
210 radio_work_done(work);
211 }
212
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700213 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
214 return;
215
216 wpa_printf(MSG_DEBUG, "P2P: Scan results received (%d BSS)",
217 (int) scan_res->num);
218
219 for (i = 0; i < scan_res->num; i++) {
220 struct wpa_scan_res *bss = scan_res->res[i];
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800221 struct os_reltime time_tmp_age, entry_ts;
Dmitry Shmidt96571392013-10-14 12:54:46 -0700222 const u8 *ies;
223 size_t ies_len;
224
Dmitry Shmidtf8623282013-02-20 14:34:59 -0800225 time_tmp_age.sec = bss->age / 1000;
226 time_tmp_age.usec = (bss->age % 1000) * 1000;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800227 os_reltime_sub(&scan_res->fetch_time, &time_tmp_age, &entry_ts);
Dmitry Shmidt96571392013-10-14 12:54:46 -0700228
229 ies = (const u8 *) (bss + 1);
230 ies_len = bss->ie_len;
231 if (bss->beacon_ie_len > 0 &&
232 !wpa_scan_get_vendor_ie(bss, P2P_IE_VENDOR_TYPE) &&
233 wpa_scan_get_vendor_ie_beacon(bss, P2P_IE_VENDOR_TYPE)) {
234 wpa_printf(MSG_DEBUG, "P2P: Use P2P IE(s) from Beacon frame since no P2P IE(s) in Probe Response frames received for "
235 MACSTR, MAC2STR(bss->bssid));
236 ies = ies + ies_len;
237 ies_len = bss->beacon_ie_len;
238 }
239
240
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700241 if (p2p_scan_res_handler(wpa_s->global->p2p, bss->bssid,
Dmitry Shmidtf8623282013-02-20 14:34:59 -0800242 bss->freq, &entry_ts, bss->level,
Dmitry Shmidt96571392013-10-14 12:54:46 -0700243 ies, ies_len) > 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700244 break;
245 }
246
247 p2p_scan_res_handled(wpa_s->global->p2p);
248}
249
250
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800251static void wpas_p2p_trigger_scan_cb(struct wpa_radio_work *work, int deinit)
252{
253 struct wpa_supplicant *wpa_s = work->wpa_s;
254 struct wpa_driver_scan_params *params = work->ctx;
255 int ret;
256
257 if (deinit) {
Dmitry Shmidtbd14a572014-02-18 10:33:49 -0800258 if (!work->started) {
259 wpa_scan_free_params(params);
260 return;
261 }
262
263 wpa_s->p2p_scan_work = NULL;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800264 return;
265 }
266
267 ret = wpa_drv_scan(wpa_s, params);
268 wpa_scan_free_params(params);
269 work->ctx = NULL;
270 if (ret) {
271 radio_work_done(work);
272 return;
273 }
274
275 os_get_reltime(&wpa_s->scan_trigger_time);
276 wpa_s->scan_res_handler = wpas_p2p_scan_res_handler;
277 wpa_s->own_scan_requested = 1;
278 wpa_s->p2p_scan_work = work;
279}
280
281
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700282static int wpas_p2p_scan(void *ctx, enum p2p_scan_type type, int freq,
283 unsigned int num_req_dev_types,
Dmitry Shmidt04949592012-07-19 12:16:46 -0700284 const u8 *req_dev_types, const u8 *dev_id, u16 pw_id)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700285{
286 struct wpa_supplicant *wpa_s = ctx;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800287 struct wpa_driver_scan_params *params = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700288 struct wpabuf *wps_ie, *ies;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800289 size_t ielen;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800290 u8 *n;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700291
292 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
293 return -1;
294
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800295 if (wpa_s->p2p_scan_work) {
296 wpa_dbg(wpa_s, MSG_INFO, "P2P: Reject scan trigger since one is already pending");
297 return -1;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700298 }
299
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800300 params = os_zalloc(sizeof(*params));
301 if (params == NULL)
302 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700303
304 /* P2P Wildcard SSID */
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800305 params->num_ssids = 1;
306 n = os_malloc(P2P_WILDCARD_SSID_LEN);
307 if (n == NULL)
308 goto fail;
309 os_memcpy(n, P2P_WILDCARD_SSID, P2P_WILDCARD_SSID_LEN);
310 params->ssids[0].ssid = n;
311 params->ssids[0].ssid_len = P2P_WILDCARD_SSID_LEN;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700312
313 wpa_s->wps->dev.p2p = 1;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700314 wps_ie = wps_build_probe_req_ie(pw_id, &wpa_s->wps->dev,
315 wpa_s->wps->uuid, WPS_REQ_ENROLLEE,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700316 num_req_dev_types, req_dev_types);
317 if (wps_ie == NULL)
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800318 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700319
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800320 ielen = p2p_scan_ie_buf_len(wpa_s->global->p2p);
321 ies = wpabuf_alloc(wpabuf_len(wps_ie) + ielen);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700322 if (ies == NULL) {
323 wpabuf_free(wps_ie);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800324 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700325 }
326 wpabuf_put_buf(ies, wps_ie);
327 wpabuf_free(wps_ie);
328
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -0800329 p2p_scan_ie(wpa_s->global->p2p, ies, dev_id);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700330
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800331 params->p2p_probe = 1;
332 n = os_malloc(wpabuf_len(ies));
333 if (n == NULL) {
334 wpabuf_free(ies);
335 goto fail;
336 }
337 os_memcpy(n, wpabuf_head(ies), wpabuf_len(ies));
338 params->extra_ies = n;
339 params->extra_ies_len = wpabuf_len(ies);
340 wpabuf_free(ies);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700341
342 switch (type) {
343 case P2P_SCAN_SOCIAL:
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800344 params->freqs = os_malloc(4 * sizeof(int));
345 if (params->freqs == NULL)
346 goto fail;
347 params->freqs[0] = 2412;
348 params->freqs[1] = 2437;
349 params->freqs[2] = 2462;
350 params->freqs[3] = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700351 break;
352 case P2P_SCAN_FULL:
353 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700354 case P2P_SCAN_SOCIAL_PLUS_ONE:
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800355 params->freqs = os_malloc(5 * sizeof(int));
356 if (params->freqs == NULL)
357 goto fail;
358 params->freqs[0] = 2412;
359 params->freqs[1] = 2437;
360 params->freqs[2] = 2462;
361 params->freqs[3] = freq;
362 params->freqs[4] = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700363 break;
364 }
365
Dmitry Shmidtbd14a572014-02-18 10:33:49 -0800366 radio_remove_works(wpa_s, "p2p-scan", 0);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800367 if (radio_add_work(wpa_s, 0, "p2p-scan", 0, wpas_p2p_trigger_scan_cb,
368 params) < 0)
369 goto fail;
370 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700371
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800372fail:
373 wpa_scan_free_params(params);
374 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700375}
376
377
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700378static enum wpa_driver_if_type wpas_p2p_if_type(int p2p_group_interface)
379{
380 switch (p2p_group_interface) {
381 case P2P_GROUP_INTERFACE_PENDING:
382 return WPA_IF_P2P_GROUP;
383 case P2P_GROUP_INTERFACE_GO:
384 return WPA_IF_P2P_GO;
385 case P2P_GROUP_INTERFACE_CLIENT:
386 return WPA_IF_P2P_CLIENT;
387 }
388
389 return WPA_IF_P2P_GROUP;
390}
391
392
393static struct wpa_supplicant * wpas_get_p2p_group(struct wpa_supplicant *wpa_s,
394 const u8 *ssid,
395 size_t ssid_len, int *go)
396{
397 struct wpa_ssid *s;
398
399 for (wpa_s = wpa_s->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
400 for (s = wpa_s->conf->ssid; s; s = s->next) {
401 if (s->disabled != 0 || !s->p2p_group ||
402 s->ssid_len != ssid_len ||
403 os_memcmp(ssid, s->ssid, ssid_len) != 0)
404 continue;
405 if (s->mode == WPAS_MODE_P2P_GO &&
406 s != wpa_s->current_ssid)
407 continue;
408 if (go)
409 *go = s->mode == WPAS_MODE_P2P_GO;
410 return wpa_s;
411 }
412 }
413
414 return NULL;
415}
416
417
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700418static int wpas_p2p_group_delete(struct wpa_supplicant *wpa_s,
419 enum p2p_group_removal_reason removal_reason)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700420{
421 struct wpa_ssid *ssid;
422 char *gtype;
423 const char *reason;
424
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700425 ssid = wpa_s->current_ssid;
426 if (ssid == NULL) {
427 /*
428 * The current SSID was not known, but there may still be a
Dmitry Shmidtaa532512012-09-24 10:35:31 -0700429 * pending P2P group interface waiting for provisioning or a
430 * P2P group that is trying to reconnect.
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700431 */
432 ssid = wpa_s->conf->ssid;
433 while (ssid) {
Jouni Malinen9d712832012-10-05 11:01:57 -0700434 if (ssid->p2p_group && ssid->disabled != 2)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700435 break;
436 ssid = ssid->next;
437 }
Jouni Malinen5c44edb2012-08-31 21:35:32 +0300438 if (ssid == NULL &&
439 wpa_s->p2p_group_interface == NOT_P2P_GROUP_INTERFACE)
440 {
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700441 wpa_printf(MSG_ERROR, "P2P: P2P group interface "
442 "not found");
443 return -1;
444 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700445 }
446 if (wpa_s->p2p_group_interface == P2P_GROUP_INTERFACE_GO)
447 gtype = "GO";
448 else if (wpa_s->p2p_group_interface == P2P_GROUP_INTERFACE_CLIENT ||
449 (ssid && ssid->mode == WPAS_MODE_INFRA)) {
450 wpa_s->reassociate = 0;
451 wpa_s->disconnected = 1;
452 wpa_supplicant_deauthenticate(wpa_s,
453 WLAN_REASON_DEAUTH_LEAVING);
454 gtype = "client";
455 } else
456 gtype = "GO";
457 if (wpa_s->cross_connect_in_use) {
458 wpa_s->cross_connect_in_use = 0;
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -0700459 wpa_msg_global(wpa_s->parent, MSG_INFO,
460 P2P_EVENT_CROSS_CONNECT_DISABLE "%s %s",
461 wpa_s->ifname, wpa_s->cross_connect_uplink);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700462 }
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700463 switch (removal_reason) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700464 case P2P_GROUP_REMOVAL_REQUESTED:
465 reason = " reason=REQUESTED";
466 break;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700467 case P2P_GROUP_REMOVAL_FORMATION_FAILED:
468 reason = " reason=FORMATION_FAILED";
469 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700470 case P2P_GROUP_REMOVAL_IDLE_TIMEOUT:
471 reason = " reason=IDLE";
472 break;
473 case P2P_GROUP_REMOVAL_UNAVAILABLE:
474 reason = " reason=UNAVAILABLE";
475 break;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700476 case P2P_GROUP_REMOVAL_GO_ENDING_SESSION:
477 reason = " reason=GO_ENDING_SESSION";
478 break;
Dmitry Shmidt391c59f2013-09-03 12:16:28 -0700479 case P2P_GROUP_REMOVAL_PSK_FAILURE:
480 reason = " reason=PSK_FAILURE";
481 break;
Dmitry Shmidt04f534e2013-12-09 15:50:16 -0800482 case P2P_GROUP_REMOVAL_FREQ_CONFLICT:
483 reason = " reason=FREQ_CONFLICT";
484 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700485 default:
486 reason = "";
487 break;
488 }
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700489 if (removal_reason != P2P_GROUP_REMOVAL_SILENT) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -0700490 wpa_msg_global(wpa_s->parent, MSG_INFO,
491 P2P_EVENT_GROUP_REMOVED "%s %s%s",
492 wpa_s->ifname, gtype, reason);
Dmitry Shmidt04949592012-07-19 12:16:46 -0700493 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700494
Dmitry Shmidt04f534e2013-12-09 15:50:16 -0800495 if (eloop_cancel_timeout(wpas_p2p_group_freq_conflict, wpa_s, NULL) > 0)
496 wpa_printf(MSG_DEBUG, "P2P: Cancelled P2P group freq_conflict timeout");
Dmitry Shmidt04949592012-07-19 12:16:46 -0700497 if (eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL) > 0)
498 wpa_printf(MSG_DEBUG, "P2P: Cancelled P2P group idle timeout");
Dmitry Shmidtf8623282013-02-20 14:34:59 -0800499 if (eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
Dmitry Shmidt2f023192013-03-12 12:44:17 -0700500 wpa_s->parent, NULL) > 0) {
Dmitry Shmidtf8623282013-02-20 14:34:59 -0800501 wpa_printf(MSG_DEBUG, "P2P: Cancelled P2P group formation "
502 "timeout");
Dmitry Shmidt2f023192013-03-12 12:44:17 -0700503 wpa_s->p2p_in_provisioning = 0;
504 }
Dmitry Shmidt04949592012-07-19 12:16:46 -0700505
Dmitry Shmidt15907092014-03-25 10:42:57 -0700506 wpa_s->p2p_in_invitation = 0;
507
Dmitry Shmidt96571392013-10-14 12:54:46 -0700508 /*
509 * Make sure wait for the first client does not remain active after the
510 * group has been removed.
511 */
512 wpa_s->global->p2p_go_wait_client.sec = 0;
513
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700514 if (removal_reason != P2P_GROUP_REMOVAL_SILENT && ssid)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700515 wpas_notify_p2p_group_removed(wpa_s, ssid, gtype);
516
517 if (wpa_s->p2p_group_interface != NOT_P2P_GROUP_INTERFACE) {
518 struct wpa_global *global;
519 char *ifname;
520 enum wpa_driver_if_type type;
521 wpa_printf(MSG_DEBUG, "P2P: Remove group interface %s",
522 wpa_s->ifname);
523 global = wpa_s->global;
524 ifname = os_strdup(wpa_s->ifname);
525 type = wpas_p2p_if_type(wpa_s->p2p_group_interface);
Dmitry Shmidte15c7b52011-08-03 15:04:35 -0700526 wpa_supplicant_remove_iface(wpa_s->global, wpa_s, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700527 wpa_s = global->ifaces;
528 if (wpa_s && ifname)
529 wpa_drv_if_remove(wpa_s, type, ifname);
530 os_free(ifname);
Jouni Malinen2b89da82012-08-31 22:04:41 +0300531 return 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700532 }
533
Dmitry Shmidt96571392013-10-14 12:54:46 -0700534 if (!wpa_s->p2p_go_group_formation_completed) {
535 wpa_s->global->p2p_group_formation = NULL;
536 wpa_s->p2p_in_provisioning = 0;
537 }
538
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800539 wpa_s->show_group_started = 0;
540 os_free(wpa_s->go_params);
541 wpa_s->go_params = NULL;
542
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800543 wpa_s->waiting_presence_resp = 0;
544
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700545 wpa_printf(MSG_DEBUG, "P2P: Remove temporary group network");
546 if (ssid && (ssid->p2p_group ||
547 ssid->mode == WPAS_MODE_P2P_GROUP_FORMATION ||
548 (ssid->key_mgmt & WPA_KEY_MGMT_WPS))) {
549 int id = ssid->id;
Jouni Malinen75ecf522011-06-27 15:19:46 -0700550 if (ssid == wpa_s->current_ssid) {
551 wpa_sm_set_config(wpa_s->wpa, NULL);
552 eapol_sm_notify_config(wpa_s->eapol, NULL, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700553 wpa_s->current_ssid = NULL;
Jouni Malinen75ecf522011-06-27 15:19:46 -0700554 }
555 /*
556 * Networks objects created during any P2P activities are not
557 * exposed out as they might/will confuse certain non-P2P aware
558 * applications since these network objects won't behave like
559 * regular ones.
560 *
561 * Likewise, we don't send out network removed signals for such
562 * network objects.
563 */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700564 wpa_config_remove_network(wpa_s->conf, id);
565 wpa_supplicant_clear_status(wpa_s);
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -0800566 wpa_supplicant_cancel_sched_scan(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700567 } else {
568 wpa_printf(MSG_DEBUG, "P2P: Temporary group network not "
569 "found");
570 }
Dmitry Shmidt04949592012-07-19 12:16:46 -0700571 if (wpa_s->ap_iface)
572 wpa_supplicant_ap_deinit(wpa_s);
573 else
574 wpa_drv_deinit_p2p_cli(wpa_s);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700575
576 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700577}
578
579
580static int wpas_p2p_persistent_group(struct wpa_supplicant *wpa_s,
581 u8 *go_dev_addr,
582 const u8 *ssid, size_t ssid_len)
583{
584 struct wpa_bss *bss;
585 const u8 *bssid;
586 struct wpabuf *p2p;
587 u8 group_capab;
588 const u8 *addr;
589
590 if (wpa_s->go_params)
591 bssid = wpa_s->go_params->peer_interface_addr;
592 else
593 bssid = wpa_s->bssid;
594
595 bss = wpa_bss_get(wpa_s, bssid, ssid, ssid_len);
Dmitry Shmidtcf32e602014-01-28 10:57:39 -0800596 if (bss == NULL && wpa_s->go_params &&
597 !is_zero_ether_addr(wpa_s->go_params->peer_device_addr))
598 bss = wpa_bss_get_p2p_dev_addr(
599 wpa_s, wpa_s->go_params->peer_device_addr);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700600 if (bss == NULL) {
601 u8 iface_addr[ETH_ALEN];
602 if (p2p_get_interface_addr(wpa_s->global->p2p, bssid,
603 iface_addr) == 0)
604 bss = wpa_bss_get(wpa_s, iface_addr, ssid, ssid_len);
605 }
606 if (bss == NULL) {
607 wpa_printf(MSG_DEBUG, "P2P: Could not figure out whether "
608 "group is persistent - BSS " MACSTR " not found",
609 MAC2STR(bssid));
610 return 0;
611 }
612
613 p2p = wpa_bss_get_vendor_ie_multi(bss, P2P_IE_VENDOR_TYPE);
Dmitry Shmidt96571392013-10-14 12:54:46 -0700614 if (p2p == NULL)
615 p2p = wpa_bss_get_vendor_ie_multi_beacon(bss,
616 P2P_IE_VENDOR_TYPE);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700617 if (p2p == NULL) {
618 wpa_printf(MSG_DEBUG, "P2P: Could not figure out whether "
619 "group is persistent - BSS " MACSTR
620 " did not include P2P IE", MAC2STR(bssid));
621 wpa_hexdump(MSG_DEBUG, "P2P: Probe Response IEs",
622 (u8 *) (bss + 1), bss->ie_len);
623 wpa_hexdump(MSG_DEBUG, "P2P: Beacon IEs",
624 ((u8 *) bss + 1) + bss->ie_len,
625 bss->beacon_ie_len);
626 return 0;
627 }
628
629 group_capab = p2p_get_group_capab(p2p);
630 addr = p2p_get_go_dev_addr(p2p);
631 wpa_printf(MSG_DEBUG, "P2P: Checking whether group is persistent: "
632 "group_capab=0x%x", group_capab);
633 if (addr) {
634 os_memcpy(go_dev_addr, addr, ETH_ALEN);
635 wpa_printf(MSG_DEBUG, "P2P: GO Device Address " MACSTR,
636 MAC2STR(addr));
637 } else
638 os_memset(go_dev_addr, 0, ETH_ALEN);
639 wpabuf_free(p2p);
640
641 wpa_printf(MSG_DEBUG, "P2P: BSS " MACSTR " group_capab=0x%x "
642 "go_dev_addr=" MACSTR,
643 MAC2STR(bssid), group_capab, MAC2STR(go_dev_addr));
644
645 return group_capab & P2P_GROUP_CAPAB_PERSISTENT_GROUP;
646}
647
648
Jouni Malinen75ecf522011-06-27 15:19:46 -0700649static int wpas_p2p_store_persistent_group(struct wpa_supplicant *wpa_s,
650 struct wpa_ssid *ssid,
651 const u8 *go_dev_addr)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700652{
653 struct wpa_ssid *s;
654 int changed = 0;
655
656 wpa_printf(MSG_DEBUG, "P2P: Storing credentials for a persistent "
657 "group (GO Dev Addr " MACSTR ")", MAC2STR(go_dev_addr));
658 for (s = wpa_s->conf->ssid; s; s = s->next) {
659 if (s->disabled == 2 &&
660 os_memcmp(go_dev_addr, s->bssid, ETH_ALEN) == 0 &&
661 s->ssid_len == ssid->ssid_len &&
662 os_memcmp(ssid->ssid, s->ssid, ssid->ssid_len) == 0)
663 break;
664 }
665
666 if (s) {
667 wpa_printf(MSG_DEBUG, "P2P: Update existing persistent group "
668 "entry");
669 if (ssid->passphrase && !s->passphrase)
670 changed = 1;
671 else if (ssid->passphrase && s->passphrase &&
672 os_strcmp(ssid->passphrase, s->passphrase) != 0)
673 changed = 1;
674 } else {
675 wpa_printf(MSG_DEBUG, "P2P: Create a new persistent group "
676 "entry");
677 changed = 1;
678 s = wpa_config_add_network(wpa_s->conf);
679 if (s == NULL)
Jouni Malinen75ecf522011-06-27 15:19:46 -0700680 return -1;
681
682 /*
683 * Instead of network_added we emit persistent_group_added
684 * notification. Also to keep the defense checks in
685 * persistent_group obj registration method, we set the
686 * relevant flags in s to designate it as a persistent group.
687 */
688 s->p2p_group = 1;
689 s->p2p_persistent_group = 1;
690 wpas_notify_persistent_group_added(wpa_s, s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700691 wpa_config_set_network_defaults(s);
692 }
693
694 s->p2p_group = 1;
695 s->p2p_persistent_group = 1;
696 s->disabled = 2;
697 s->bssid_set = 1;
698 os_memcpy(s->bssid, go_dev_addr, ETH_ALEN);
699 s->mode = ssid->mode;
700 s->auth_alg = WPA_AUTH_ALG_OPEN;
701 s->key_mgmt = WPA_KEY_MGMT_PSK;
702 s->proto = WPA_PROTO_RSN;
703 s->pairwise_cipher = WPA_CIPHER_CCMP;
704 s->export_keys = 1;
705 if (ssid->passphrase) {
706 os_free(s->passphrase);
707 s->passphrase = os_strdup(ssid->passphrase);
708 }
709 if (ssid->psk_set) {
710 s->psk_set = 1;
711 os_memcpy(s->psk, ssid->psk, 32);
712 }
713 if (s->passphrase && !s->psk_set)
714 wpa_config_update_psk(s);
715 if (s->ssid == NULL || s->ssid_len < ssid->ssid_len) {
716 os_free(s->ssid);
717 s->ssid = os_malloc(ssid->ssid_len);
718 }
719 if (s->ssid) {
720 s->ssid_len = ssid->ssid_len;
721 os_memcpy(s->ssid, ssid->ssid, s->ssid_len);
722 }
Dmitry Shmidt391c59f2013-09-03 12:16:28 -0700723 if (ssid->mode == WPAS_MODE_P2P_GO && wpa_s->global->add_psk) {
724 dl_list_add(&s->psk_list, &wpa_s->global->add_psk->list);
725 wpa_s->global->add_psk = NULL;
726 changed = 1;
727 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700728
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700729 if (changed && wpa_s->conf->update_config &&
730 wpa_config_write(wpa_s->confname, wpa_s->conf)) {
731 wpa_printf(MSG_DEBUG, "P2P: Failed to update configuration");
732 }
Jouni Malinen75ecf522011-06-27 15:19:46 -0700733
734 return s->id;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700735}
736
737
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800738static void wpas_p2p_add_persistent_group_client(struct wpa_supplicant *wpa_s,
739 const u8 *addr)
740{
741 struct wpa_ssid *ssid, *s;
742 u8 *n;
743 size_t i;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700744 int found = 0;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800745
746 ssid = wpa_s->current_ssid;
747 if (ssid == NULL || ssid->mode != WPAS_MODE_P2P_GO ||
748 !ssid->p2p_persistent_group)
749 return;
750
751 for (s = wpa_s->parent->conf->ssid; s; s = s->next) {
752 if (s->disabled != 2 || s->mode != WPAS_MODE_P2P_GO)
753 continue;
754
755 if (s->ssid_len == ssid->ssid_len &&
756 os_memcmp(s->ssid, ssid->ssid, s->ssid_len) == 0)
757 break;
758 }
759
760 if (s == NULL)
761 return;
762
763 for (i = 0; s->p2p_client_list && i < s->num_p2p_clients; i++) {
764 if (os_memcmp(s->p2p_client_list + i * ETH_ALEN, addr,
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700765 ETH_ALEN) != 0)
766 continue;
767
768 if (i == s->num_p2p_clients - 1)
769 return; /* already the most recent entry */
770
771 /* move the entry to mark it most recent */
772 os_memmove(s->p2p_client_list + i * ETH_ALEN,
773 s->p2p_client_list + (i + 1) * ETH_ALEN,
774 (s->num_p2p_clients - i - 1) * ETH_ALEN);
775 os_memcpy(s->p2p_client_list +
776 (s->num_p2p_clients - 1) * ETH_ALEN, addr, ETH_ALEN);
777 found = 1;
778 break;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800779 }
780
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700781 if (!found && s->num_p2p_clients < P2P_MAX_STORED_CLIENTS) {
782 n = os_realloc_array(s->p2p_client_list,
783 s->num_p2p_clients + 1, ETH_ALEN);
784 if (n == NULL)
785 return;
786 os_memcpy(n + s->num_p2p_clients * ETH_ALEN, addr, ETH_ALEN);
787 s->p2p_client_list = n;
788 s->num_p2p_clients++;
789 } else if (!found) {
790 /* Not enough room for an additional entry - drop the oldest
791 * entry */
792 os_memmove(s->p2p_client_list,
793 s->p2p_client_list + ETH_ALEN,
794 (s->num_p2p_clients - 1) * ETH_ALEN);
795 os_memcpy(s->p2p_client_list +
796 (s->num_p2p_clients - 1) * ETH_ALEN,
797 addr, ETH_ALEN);
798 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800799
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800800 if (wpa_s->parent->conf->update_config &&
801 wpa_config_write(wpa_s->parent->confname, wpa_s->parent->conf))
802 wpa_printf(MSG_DEBUG, "P2P: Failed to update configuration");
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800803}
804
805
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700806static void wpas_group_formation_completed(struct wpa_supplicant *wpa_s,
807 int success)
808{
809 struct wpa_ssid *ssid;
810 const char *ssid_txt;
811 int client;
812 int persistent;
813 u8 go_dev_addr[ETH_ALEN];
Jouni Malinen75ecf522011-06-27 15:19:46 -0700814 int network_id = -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700815
816 /*
817 * This callback is likely called for the main interface. Update wpa_s
818 * to use the group interface if a new interface was created for the
819 * group.
820 */
821 if (wpa_s->global->p2p_group_formation)
822 wpa_s = wpa_s->global->p2p_group_formation;
Dmitry Shmidt56052862013-10-04 10:23:25 -0700823 if (wpa_s->p2p_go_group_formation_completed) {
824 wpa_s->global->p2p_group_formation = NULL;
825 wpa_s->p2p_in_provisioning = 0;
826 }
Dmitry Shmidt21de2142014-04-08 10:50:52 -0700827 wpa_s->p2p_in_invitation = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700828
829 if (!success) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -0700830 wpa_msg_global(wpa_s->parent, MSG_INFO,
831 P2P_EVENT_GROUP_FORMATION_FAILURE);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700832 wpas_p2p_group_delete(wpa_s,
833 P2P_GROUP_REMOVAL_FORMATION_FAILED);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700834 return;
835 }
836
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -0700837 wpa_msg_global(wpa_s->parent, MSG_INFO,
838 P2P_EVENT_GROUP_FORMATION_SUCCESS);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700839
840 ssid = wpa_s->current_ssid;
841 if (ssid && ssid->mode == WPAS_MODE_P2P_GROUP_FORMATION) {
842 ssid->mode = WPAS_MODE_P2P_GO;
843 p2p_group_notif_formation_done(wpa_s->p2p_group);
844 wpa_supplicant_ap_mac_addr_filter(wpa_s, NULL);
845 }
846
847 persistent = 0;
848 if (ssid) {
849 ssid_txt = wpa_ssid_txt(ssid->ssid, ssid->ssid_len);
850 client = ssid->mode == WPAS_MODE_INFRA;
851 if (ssid->mode == WPAS_MODE_P2P_GO) {
852 persistent = ssid->p2p_persistent_group;
Dmitry Shmidt497c1d52011-07-21 15:19:46 -0700853 os_memcpy(go_dev_addr, wpa_s->global->p2p_dev_addr,
854 ETH_ALEN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700855 } else
856 persistent = wpas_p2p_persistent_group(wpa_s,
857 go_dev_addr,
858 ssid->ssid,
859 ssid->ssid_len);
860 } else {
861 ssid_txt = "";
862 client = wpa_s->p2p_group_interface ==
863 P2P_GROUP_INTERFACE_CLIENT;
864 os_memset(go_dev_addr, 0, ETH_ALEN);
865 }
866
867 wpa_s->show_group_started = 0;
868 if (client) {
869 /*
870 * Indicate event only after successfully completed 4-way
871 * handshake, i.e., when the interface is ready for data
872 * packets.
873 */
874 wpa_s->show_group_started = 1;
875 } else if (ssid && ssid->passphrase == NULL && ssid->psk_set) {
876 char psk[65];
877 wpa_snprintf_hex(psk, sizeof(psk), ssid->psk, 32);
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -0700878 wpa_msg_global(wpa_s->parent, MSG_INFO, P2P_EVENT_GROUP_STARTED
879 "%s GO ssid=\"%s\" freq=%d psk=%s go_dev_addr="
880 MACSTR "%s",
881 wpa_s->ifname, ssid_txt, ssid->frequency, psk,
882 MAC2STR(go_dev_addr),
883 persistent ? " [PERSISTENT]" : "");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700884 wpas_p2p_cross_connect_setup(wpa_s);
885 wpas_p2p_set_group_idle_timeout(wpa_s);
886 } else {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -0700887 wpa_msg_global(wpa_s->parent, MSG_INFO, P2P_EVENT_GROUP_STARTED
888 "%s GO ssid=\"%s\" freq=%d passphrase=\"%s\" "
889 "go_dev_addr=" MACSTR "%s",
890 wpa_s->ifname, ssid_txt,
891 ssid ? ssid->frequency : 0,
892 ssid && ssid->passphrase ? ssid->passphrase : "",
893 MAC2STR(go_dev_addr),
894 persistent ? " [PERSISTENT]" : "");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700895 wpas_p2p_cross_connect_setup(wpa_s);
896 wpas_p2p_set_group_idle_timeout(wpa_s);
897 }
898
899 if (persistent)
Jouni Malinen75ecf522011-06-27 15:19:46 -0700900 network_id = wpas_p2p_store_persistent_group(wpa_s->parent,
901 ssid, go_dev_addr);
Dmitry Shmidt391c59f2013-09-03 12:16:28 -0700902 else {
903 os_free(wpa_s->global->add_psk);
904 wpa_s->global->add_psk = NULL;
905 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800906 if (network_id < 0 && ssid)
Jouni Malinen75ecf522011-06-27 15:19:46 -0700907 network_id = ssid->id;
Dmitry Shmidt92c368d2013-08-29 12:37:21 -0700908 if (!client) {
Jouni Malinen75ecf522011-06-27 15:19:46 -0700909 wpas_notify_p2p_group_started(wpa_s, ssid, network_id, 0);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800910 os_get_reltime(&wpa_s->global->p2p_go_wait_client);
Dmitry Shmidt92c368d2013-08-29 12:37:21 -0700911 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700912}
913
914
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800915struct send_action_work {
916 unsigned int freq;
917 u8 dst[ETH_ALEN];
918 u8 src[ETH_ALEN];
919 u8 bssid[ETH_ALEN];
920 size_t len;
921 unsigned int wait_time;
922 u8 buf[0];
923};
924
925
926static void wpas_p2p_send_action_work_timeout(void *eloop_ctx,
927 void *timeout_ctx)
928{
929 struct wpa_supplicant *wpa_s = eloop_ctx;
930
931 if (!wpa_s->p2p_send_action_work)
932 return;
933
934 wpa_printf(MSG_DEBUG, "P2P: Send Action frame radio work timed out");
935 os_free(wpa_s->p2p_send_action_work->ctx);
936 radio_work_done(wpa_s->p2p_send_action_work);
937 wpa_s->p2p_send_action_work = NULL;
938}
939
940
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800941static void wpas_p2p_send_action_tx_status(struct wpa_supplicant *wpa_s,
942 unsigned int freq,
943 const u8 *dst, const u8 *src,
944 const u8 *bssid,
945 const u8 *data, size_t data_len,
946 enum offchannel_send_action_result
947 result)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700948{
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800949 enum p2p_send_action_result res = P2P_SEND_ACTION_SUCCESS;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700950
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800951 if (wpa_s->p2p_send_action_work) {
952 struct send_action_work *awork;
953 awork = wpa_s->p2p_send_action_work->ctx;
954 if (awork->wait_time == 0) {
955 os_free(awork);
956 radio_work_done(wpa_s->p2p_send_action_work);
957 wpa_s->p2p_send_action_work = NULL;
958 } else {
959 /*
960 * In theory, this should not be needed, but number of
961 * places in the P2P code is still using non-zero wait
962 * time for the last Action frame in the sequence and
963 * some of these do not call send_action_done().
964 */
965 eloop_cancel_timeout(wpas_p2p_send_action_work_timeout,
966 wpa_s, NULL);
967 eloop_register_timeout(
968 0, awork->wait_time * 1000,
969 wpas_p2p_send_action_work_timeout,
970 wpa_s, NULL);
971 }
972 }
973
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700974 if (wpa_s->global->p2p == NULL || wpa_s->global->p2p_disabled)
975 return;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700976
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800977 switch (result) {
978 case OFFCHANNEL_SEND_ACTION_SUCCESS:
979 res = P2P_SEND_ACTION_SUCCESS;
980 break;
981 case OFFCHANNEL_SEND_ACTION_NO_ACK:
982 res = P2P_SEND_ACTION_NO_ACK;
983 break;
984 case OFFCHANNEL_SEND_ACTION_FAILED:
985 res = P2P_SEND_ACTION_FAILED;
986 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700987 }
988
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800989 p2p_send_action_cb(wpa_s->global->p2p, freq, dst, src, bssid, res);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700990
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -0800991 if (result != OFFCHANNEL_SEND_ACTION_SUCCESS &&
992 wpa_s->pending_pd_before_join &&
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800993 (os_memcmp(dst, wpa_s->pending_join_dev_addr, ETH_ALEN) == 0 ||
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800994 os_memcmp(dst, wpa_s->pending_join_iface_addr, ETH_ALEN) == 0) &&
995 wpa_s->p2p_fallback_to_go_neg) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700996 wpa_s->pending_pd_before_join = 0;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800997 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: No ACK for PD Req "
998 "during p2p_connect-auto");
999 wpas_p2p_fallback_to_go_neg(wpa_s, 0);
1000 return;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001001 }
1002}
1003
1004
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001005static void wpas_send_action_cb(struct wpa_radio_work *work, int deinit)
1006{
1007 struct wpa_supplicant *wpa_s = work->wpa_s;
1008 struct send_action_work *awork = work->ctx;
1009
1010 if (deinit) {
Dmitry Shmidtbd14a572014-02-18 10:33:49 -08001011 if (work->started) {
1012 eloop_cancel_timeout(wpas_p2p_send_action_work_timeout,
1013 wpa_s, NULL);
1014 wpa_s->p2p_send_action_work = NULL;
1015 offchannel_send_action_done(wpa_s);
1016 }
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001017 os_free(awork);
1018 return;
1019 }
1020
1021 if (offchannel_send_action(wpa_s, awork->freq, awork->dst, awork->src,
1022 awork->bssid, awork->buf, awork->len,
1023 awork->wait_time,
1024 wpas_p2p_send_action_tx_status, 1) < 0) {
1025 os_free(awork);
1026 radio_work_done(work);
1027 return;
1028 }
1029 wpa_s->p2p_send_action_work = work;
1030}
1031
1032
1033static int wpas_send_action_work(struct wpa_supplicant *wpa_s,
1034 unsigned int freq, const u8 *dst,
1035 const u8 *src, const u8 *bssid, const u8 *buf,
1036 size_t len, unsigned int wait_time)
1037{
1038 struct send_action_work *awork;
1039
1040 if (wpa_s->p2p_send_action_work) {
1041 wpa_printf(MSG_DEBUG, "P2P: Cannot schedule new p2p-send-action work since one is already pending");
1042 return -1;
1043 }
1044
1045 awork = os_zalloc(sizeof(*awork) + len);
1046 if (awork == NULL)
1047 return -1;
1048
1049 awork->freq = freq;
1050 os_memcpy(awork->dst, dst, ETH_ALEN);
1051 os_memcpy(awork->src, src, ETH_ALEN);
1052 os_memcpy(awork->bssid, bssid, ETH_ALEN);
1053 awork->len = len;
1054 awork->wait_time = wait_time;
1055 os_memcpy(awork->buf, buf, len);
1056
1057 if (radio_add_work(wpa_s, freq, "p2p-send-action", 0,
1058 wpas_send_action_cb, awork) < 0) {
1059 os_free(awork);
1060 return -1;
1061 }
1062
1063 return 0;
1064}
1065
1066
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001067static int wpas_send_action(void *ctx, unsigned int freq, const u8 *dst,
1068 const u8 *src, const u8 *bssid, const u8 *buf,
1069 size_t len, unsigned int wait_time)
1070{
1071 struct wpa_supplicant *wpa_s = ctx;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001072 int listen_freq = -1, send_freq = -1;
1073
1074 if (wpa_s->p2p_listen_work)
1075 listen_freq = wpa_s->p2p_listen_work->freq;
1076 if (wpa_s->p2p_send_action_work)
1077 send_freq = wpa_s->p2p_send_action_work->freq;
1078 if (listen_freq != (int) freq && send_freq != (int) freq) {
1079 wpa_printf(MSG_DEBUG, "P2P: Schedule new radio work for Action frame TX (listen_freq=%d send_freq=%d)",
1080 listen_freq, send_freq);
1081 return wpas_send_action_work(wpa_s, freq, dst, src, bssid, buf,
1082 len, wait_time);
1083 }
1084
1085 wpa_printf(MSG_DEBUG, "P2P: Use ongoing radio work for Action frame TX");
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001086 return offchannel_send_action(wpa_s, freq, dst, src, bssid, buf, len,
1087 wait_time,
1088 wpas_p2p_send_action_tx_status, 1);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001089}
1090
1091
1092static void wpas_send_action_done(void *ctx)
1093{
1094 struct wpa_supplicant *wpa_s = ctx;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001095
1096 if (wpa_s->p2p_send_action_work) {
1097 eloop_cancel_timeout(wpas_p2p_send_action_work_timeout,
1098 wpa_s, NULL);
1099 os_free(wpa_s->p2p_send_action_work->ctx);
1100 radio_work_done(wpa_s->p2p_send_action_work);
1101 wpa_s->p2p_send_action_work = NULL;
1102 }
1103
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001104 offchannel_send_action_done(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001105}
1106
1107
1108static int wpas_copy_go_neg_results(struct wpa_supplicant *wpa_s,
1109 struct p2p_go_neg_results *params)
1110{
1111 if (wpa_s->go_params == NULL) {
1112 wpa_s->go_params = os_malloc(sizeof(*params));
1113 if (wpa_s->go_params == NULL)
1114 return -1;
1115 }
1116 os_memcpy(wpa_s->go_params, params, sizeof(*params));
1117 return 0;
1118}
1119
1120
1121static void wpas_start_wps_enrollee(struct wpa_supplicant *wpa_s,
1122 struct p2p_go_neg_results *res)
1123{
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001124 wpa_printf(MSG_DEBUG, "P2P: Start WPS Enrollee for peer " MACSTR
1125 " dev_addr " MACSTR " wps_method %d",
1126 MAC2STR(res->peer_interface_addr),
1127 MAC2STR(res->peer_device_addr), res->wps_method);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001128 wpa_hexdump_ascii(MSG_DEBUG, "P2P: Start WPS Enrollee for SSID",
1129 res->ssid, res->ssid_len);
1130 wpa_supplicant_ap_deinit(wpa_s);
1131 wpas_copy_go_neg_results(wpa_s, res);
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001132 if (res->wps_method == WPS_PBC) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001133 wpas_wps_start_pbc(wpa_s, res->peer_interface_addr, 1);
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001134#ifdef CONFIG_WPS_NFC
1135 } else if (res->wps_method == WPS_NFC) {
1136 wpas_wps_start_nfc(wpa_s, res->peer_device_addr,
1137 res->peer_interface_addr,
1138 wpa_s->parent->p2p_oob_dev_pw,
1139 wpa_s->parent->p2p_oob_dev_pw_id, 1,
1140 wpa_s->parent->p2p_oob_dev_pw_id ==
1141 DEV_PW_NFC_CONNECTION_HANDOVER ?
1142 wpa_s->parent->p2p_peer_oob_pubkey_hash :
1143 NULL,
1144 NULL, 0, 0);
1145#endif /* CONFIG_WPS_NFC */
1146 } else {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001147 u16 dev_pw_id = DEV_PW_DEFAULT;
1148 if (wpa_s->p2p_wps_method == WPS_PIN_KEYPAD)
1149 dev_pw_id = DEV_PW_REGISTRAR_SPECIFIED;
1150 wpas_wps_start_pin(wpa_s, res->peer_interface_addr,
1151 wpa_s->p2p_pin, 1, dev_pw_id);
1152 }
1153}
1154
1155
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07001156static void wpas_p2p_add_psk_list(struct wpa_supplicant *wpa_s,
1157 struct wpa_ssid *ssid)
1158{
1159 struct wpa_ssid *persistent;
1160 struct psk_list_entry *psk;
1161 struct hostapd_data *hapd;
1162
1163 if (!wpa_s->ap_iface)
1164 return;
1165
1166 persistent = wpas_p2p_get_persistent(wpa_s->parent, NULL, ssid->ssid,
1167 ssid->ssid_len);
1168 if (persistent == NULL)
1169 return;
1170
1171 hapd = wpa_s->ap_iface->bss[0];
1172
1173 dl_list_for_each(psk, &persistent->psk_list, struct psk_list_entry,
1174 list) {
1175 struct hostapd_wpa_psk *hpsk;
1176
1177 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Add persistent group PSK entry for "
1178 MACSTR " psk=%d",
1179 MAC2STR(psk->addr), psk->p2p);
1180 hpsk = os_zalloc(sizeof(*hpsk));
1181 if (hpsk == NULL)
1182 break;
1183 os_memcpy(hpsk->psk, psk->psk, PMK_LEN);
1184 if (psk->p2p)
1185 os_memcpy(hpsk->p2p_dev_addr, psk->addr, ETH_ALEN);
1186 else
1187 os_memcpy(hpsk->addr, psk->addr, ETH_ALEN);
1188 hpsk->next = hapd->conf->ssid.wpa_psk;
1189 hapd->conf->ssid.wpa_psk = hpsk;
1190 }
1191}
1192
1193
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001194static void p2p_go_configured(void *ctx, void *data)
1195{
1196 struct wpa_supplicant *wpa_s = ctx;
1197 struct p2p_go_neg_results *params = data;
1198 struct wpa_ssid *ssid;
Jouni Malinen75ecf522011-06-27 15:19:46 -07001199 int network_id = -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001200
1201 ssid = wpa_s->current_ssid;
1202 if (ssid && ssid->mode == WPAS_MODE_P2P_GO) {
1203 wpa_printf(MSG_DEBUG, "P2P: Group setup without provisioning");
1204 if (wpa_s->global->p2p_group_formation == wpa_s)
1205 wpa_s->global->p2p_group_formation = NULL;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001206 if (os_strlen(params->passphrase) > 0) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07001207 wpa_msg_global(wpa_s->parent, MSG_INFO,
1208 P2P_EVENT_GROUP_STARTED
1209 "%s GO ssid=\"%s\" freq=%d "
1210 "passphrase=\"%s\" go_dev_addr=" MACSTR
1211 "%s", wpa_s->ifname,
1212 wpa_ssid_txt(ssid->ssid, ssid->ssid_len),
1213 ssid->frequency, params->passphrase,
1214 MAC2STR(wpa_s->global->p2p_dev_addr),
1215 params->persistent_group ?
1216 " [PERSISTENT]" : "");
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001217 } else {
1218 char psk[65];
1219 wpa_snprintf_hex(psk, sizeof(psk), params->psk,
1220 sizeof(params->psk));
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07001221 wpa_msg_global(wpa_s->parent, MSG_INFO,
1222 P2P_EVENT_GROUP_STARTED
1223 "%s GO ssid=\"%s\" freq=%d psk=%s "
1224 "go_dev_addr=" MACSTR "%s",
1225 wpa_s->ifname,
1226 wpa_ssid_txt(ssid->ssid, ssid->ssid_len),
1227 ssid->frequency, psk,
1228 MAC2STR(wpa_s->global->p2p_dev_addr),
1229 params->persistent_group ?
1230 " [PERSISTENT]" : "");
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001231 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001232
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001233 os_get_reltime(&wpa_s->global->p2p_go_wait_client);
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07001234 if (params->persistent_group) {
Jouni Malinen75ecf522011-06-27 15:19:46 -07001235 network_id = wpas_p2p_store_persistent_group(
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001236 wpa_s->parent, ssid,
Dmitry Shmidt497c1d52011-07-21 15:19:46 -07001237 wpa_s->global->p2p_dev_addr);
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07001238 wpas_p2p_add_psk_list(wpa_s, ssid);
1239 }
Jouni Malinen75ecf522011-06-27 15:19:46 -07001240 if (network_id < 0)
1241 network_id = ssid->id;
1242 wpas_notify_p2p_group_started(wpa_s, ssid, network_id, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001243 wpas_p2p_cross_connect_setup(wpa_s);
1244 wpas_p2p_set_group_idle_timeout(wpa_s);
Dmitry Shmidt56052862013-10-04 10:23:25 -07001245
1246 if (wpa_s->p2p_first_connection_timeout) {
1247 wpa_dbg(wpa_s, MSG_DEBUG,
1248 "P2P: Start group formation timeout of %d seconds until first data connection on GO",
1249 wpa_s->p2p_first_connection_timeout);
1250 wpa_s->p2p_go_group_formation_completed = 0;
1251 wpa_s->global->p2p_group_formation = wpa_s;
1252 eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
1253 wpa_s->parent, NULL);
1254 eloop_register_timeout(
1255 wpa_s->p2p_first_connection_timeout, 0,
1256 wpas_p2p_group_formation_timeout,
1257 wpa_s->parent, NULL);
1258 }
1259
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001260 return;
1261 }
1262
1263 wpa_printf(MSG_DEBUG, "P2P: Setting up WPS for GO provisioning");
1264 if (wpa_supplicant_ap_mac_addr_filter(wpa_s,
1265 params->peer_interface_addr)) {
1266 wpa_printf(MSG_DEBUG, "P2P: Failed to setup MAC address "
1267 "filtering");
1268 return;
1269 }
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001270 if (params->wps_method == WPS_PBC) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001271 wpa_supplicant_ap_wps_pbc(wpa_s, params->peer_interface_addr,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001272 params->peer_device_addr);
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001273#ifdef CONFIG_WPS_NFC
1274 } else if (params->wps_method == WPS_NFC) {
1275 if (wpa_s->parent->p2p_oob_dev_pw_id !=
1276 DEV_PW_NFC_CONNECTION_HANDOVER &&
1277 !wpa_s->parent->p2p_oob_dev_pw) {
1278 wpa_printf(MSG_DEBUG, "P2P: No NFC Dev Pw known");
1279 return;
1280 }
1281 wpas_ap_wps_add_nfc_pw(
1282 wpa_s, wpa_s->parent->p2p_oob_dev_pw_id,
1283 wpa_s->parent->p2p_oob_dev_pw,
1284 wpa_s->parent->p2p_peer_oob_pk_hash_known ?
1285 wpa_s->parent->p2p_peer_oob_pubkey_hash : NULL);
1286#endif /* CONFIG_WPS_NFC */
1287 } else if (wpa_s->p2p_pin[0])
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001288 wpa_supplicant_ap_wps_pin(wpa_s, params->peer_interface_addr,
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001289 wpa_s->p2p_pin, NULL, 0, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001290 os_free(wpa_s->go_params);
1291 wpa_s->go_params = NULL;
1292}
1293
1294
1295static void wpas_start_wps_go(struct wpa_supplicant *wpa_s,
1296 struct p2p_go_neg_results *params,
1297 int group_formation)
1298{
1299 struct wpa_ssid *ssid;
1300
Dmitry Shmidtaa532512012-09-24 10:35:31 -07001301 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Starting GO");
1302 if (wpas_copy_go_neg_results(wpa_s, params) < 0) {
1303 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Could not copy GO Negotiation "
1304 "results");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001305 return;
Dmitry Shmidtaa532512012-09-24 10:35:31 -07001306 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001307
1308 ssid = wpa_config_add_network(wpa_s->conf);
Dmitry Shmidtaa532512012-09-24 10:35:31 -07001309 if (ssid == NULL) {
1310 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Could not add network for GO");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001311 return;
Dmitry Shmidtaa532512012-09-24 10:35:31 -07001312 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001313
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001314 wpa_s->show_group_started = 0;
1315
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001316 wpa_config_set_network_defaults(ssid);
1317 ssid->temporary = 1;
1318 ssid->p2p_group = 1;
1319 ssid->p2p_persistent_group = params->persistent_group;
1320 ssid->mode = group_formation ? WPAS_MODE_P2P_GROUP_FORMATION :
1321 WPAS_MODE_P2P_GO;
1322 ssid->frequency = params->freq;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001323 ssid->ht40 = params->ht40;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07001324 ssid->vht = params->vht;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001325 ssid->ssid = os_zalloc(params->ssid_len + 1);
1326 if (ssid->ssid) {
1327 os_memcpy(ssid->ssid, params->ssid, params->ssid_len);
1328 ssid->ssid_len = params->ssid_len;
1329 }
1330 ssid->auth_alg = WPA_AUTH_ALG_OPEN;
1331 ssid->key_mgmt = WPA_KEY_MGMT_PSK;
1332 ssid->proto = WPA_PROTO_RSN;
1333 ssid->pairwise_cipher = WPA_CIPHER_CCMP;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001334 if (os_strlen(params->passphrase) > 0) {
1335 ssid->passphrase = os_strdup(params->passphrase);
1336 if (ssid->passphrase == NULL) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07001337 wpa_msg_global(wpa_s, MSG_ERROR,
1338 "P2P: Failed to copy passphrase for GO");
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001339 wpa_config_remove_network(wpa_s->conf, ssid->id);
1340 return;
1341 }
1342 } else
1343 ssid->passphrase = NULL;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001344 ssid->psk_set = params->psk_set;
1345 if (ssid->psk_set)
1346 os_memcpy(ssid->psk, params->psk, sizeof(ssid->psk));
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001347 else if (ssid->passphrase)
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001348 wpa_config_update_psk(ssid);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001349 ssid->ap_max_inactivity = wpa_s->parent->conf->p2p_go_max_inactivity;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001350
1351 wpa_s->ap_configured_cb = p2p_go_configured;
1352 wpa_s->ap_configured_cb_ctx = wpa_s;
1353 wpa_s->ap_configured_cb_data = wpa_s->go_params;
Jouni Malinen75ecf522011-06-27 15:19:46 -07001354 wpa_s->connect_without_scan = ssid;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001355 wpa_s->reassociate = 1;
1356 wpa_s->disconnected = 0;
Dmitry Shmidtaa532512012-09-24 10:35:31 -07001357 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Request scan (that will be skipped) to "
1358 "start GO)");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001359 wpa_supplicant_req_scan(wpa_s, 0, 0);
1360}
1361
1362
1363static void wpas_p2p_clone_config(struct wpa_supplicant *dst,
1364 const struct wpa_supplicant *src)
1365{
1366 struct wpa_config *d;
1367 const struct wpa_config *s;
1368
1369 d = dst->conf;
1370 s = src->conf;
1371
1372#define C(n) if (s->n) d->n = os_strdup(s->n)
1373 C(device_name);
1374 C(manufacturer);
1375 C(model_name);
1376 C(model_number);
1377 C(serial_number);
1378 C(config_methods);
1379#undef C
1380
1381 os_memcpy(d->device_type, s->device_type, WPS_DEV_TYPE_LEN);
1382 os_memcpy(d->sec_device_type, s->sec_device_type,
1383 sizeof(d->sec_device_type));
1384 d->num_sec_device_types = s->num_sec_device_types;
1385
1386 d->p2p_group_idle = s->p2p_group_idle;
1387 d->p2p_intra_bss = s->p2p_intra_bss;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001388 d->persistent_reconnect = s->persistent_reconnect;
Dmitry Shmidt04949592012-07-19 12:16:46 -07001389 d->max_num_sta = s->max_num_sta;
1390 d->pbc_in_m1 = s->pbc_in_m1;
Dmitry Shmidt444d5672013-04-01 13:08:44 -07001391 d->ignore_old_scan_res = s->ignore_old_scan_res;
Dmitry Shmidt51b6ea82013-05-08 10:42:09 -07001392 d->beacon_int = s->beacon_int;
Dmitry Shmidt18463232014-01-24 12:29:41 -08001393 d->dtim_period = s->dtim_period;
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07001394 d->disassoc_low_ack = s->disassoc_low_ack;
Dmitry Shmidt96571392013-10-14 12:54:46 -07001395 d->disable_scan_offload = s->disable_scan_offload;
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001396
1397 if (s->wps_nfc_dh_privkey && s->wps_nfc_dh_pubkey) {
1398 d->wps_nfc_dh_privkey = wpabuf_dup(s->wps_nfc_dh_privkey);
1399 d->wps_nfc_dh_pubkey = wpabuf_dup(s->wps_nfc_dh_pubkey);
1400 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001401}
1402
1403
Dmitry Shmidt34af3062013-07-11 10:46:32 -07001404static void wpas_p2p_get_group_ifname(struct wpa_supplicant *wpa_s,
1405 char *ifname, size_t len)
1406{
1407 char *ifname_ptr = wpa_s->ifname;
1408
1409 if (os_strncmp(wpa_s->ifname, P2P_MGMT_DEVICE_PREFIX,
1410 os_strlen(P2P_MGMT_DEVICE_PREFIX)) == 0) {
1411 ifname_ptr = os_strrchr(wpa_s->ifname, '-') + 1;
1412 }
1413
1414 os_snprintf(ifname, len, "p2p-%s-%d", ifname_ptr, wpa_s->p2p_group_idx);
1415 if (os_strlen(ifname) >= IFNAMSIZ &&
1416 os_strlen(wpa_s->ifname) < IFNAMSIZ) {
1417 /* Try to avoid going over the IFNAMSIZ length limit */
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07001418 os_snprintf(ifname, len, "p2p-%d", wpa_s->p2p_group_idx);
Dmitry Shmidt34af3062013-07-11 10:46:32 -07001419 }
1420}
1421
1422
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001423static int wpas_p2p_add_group_interface(struct wpa_supplicant *wpa_s,
1424 enum wpa_driver_if_type type)
1425{
1426 char ifname[120], force_ifname[120];
1427
1428 if (wpa_s->pending_interface_name[0]) {
1429 wpa_printf(MSG_DEBUG, "P2P: Pending virtual interface exists "
1430 "- skip creation of a new one");
1431 if (is_zero_ether_addr(wpa_s->pending_interface_addr)) {
1432 wpa_printf(MSG_DEBUG, "P2P: Pending virtual address "
1433 "unknown?! ifname='%s'",
1434 wpa_s->pending_interface_name);
1435 return -1;
1436 }
1437 return 0;
1438 }
1439
Dmitry Shmidt34af3062013-07-11 10:46:32 -07001440 wpas_p2p_get_group_ifname(wpa_s, ifname, sizeof(ifname));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001441 force_ifname[0] = '\0';
1442
1443 wpa_printf(MSG_DEBUG, "P2P: Create a new interface %s for the group",
1444 ifname);
1445 wpa_s->p2p_group_idx++;
1446
1447 wpa_s->pending_interface_type = type;
1448 if (wpa_drv_if_add(wpa_s, type, ifname, NULL, NULL, force_ifname,
1449 wpa_s->pending_interface_addr, NULL) < 0) {
1450 wpa_printf(MSG_ERROR, "P2P: Failed to create new group "
1451 "interface");
1452 return -1;
1453 }
1454
1455 if (force_ifname[0]) {
1456 wpa_printf(MSG_DEBUG, "P2P: Driver forced interface name %s",
1457 force_ifname);
1458 os_strlcpy(wpa_s->pending_interface_name, force_ifname,
1459 sizeof(wpa_s->pending_interface_name));
1460 } else
1461 os_strlcpy(wpa_s->pending_interface_name, ifname,
1462 sizeof(wpa_s->pending_interface_name));
1463 wpa_printf(MSG_DEBUG, "P2P: Created pending virtual interface %s addr "
1464 MACSTR, wpa_s->pending_interface_name,
1465 MAC2STR(wpa_s->pending_interface_addr));
1466
1467 return 0;
1468}
1469
1470
1471static void wpas_p2p_remove_pending_group_interface(
1472 struct wpa_supplicant *wpa_s)
1473{
1474 if (!wpa_s->pending_interface_name[0] ||
1475 is_zero_ether_addr(wpa_s->pending_interface_addr))
1476 return; /* No pending virtual interface */
1477
1478 wpa_printf(MSG_DEBUG, "P2P: Removing pending group interface %s",
1479 wpa_s->pending_interface_name);
1480 wpa_drv_if_remove(wpa_s, wpa_s->pending_interface_type,
1481 wpa_s->pending_interface_name);
1482 os_memset(wpa_s->pending_interface_addr, 0, ETH_ALEN);
1483 wpa_s->pending_interface_name[0] = '\0';
1484}
1485
1486
1487static struct wpa_supplicant *
1488wpas_p2p_init_group_interface(struct wpa_supplicant *wpa_s, int go)
1489{
1490 struct wpa_interface iface;
1491 struct wpa_supplicant *group_wpa_s;
1492
1493 if (!wpa_s->pending_interface_name[0]) {
1494 wpa_printf(MSG_ERROR, "P2P: No pending group interface");
1495 if (!wpas_p2p_create_iface(wpa_s))
1496 return NULL;
1497 /*
1498 * Something has forced us to remove the pending interface; try
1499 * to create a new one and hope for the best that we will get
1500 * the same local address.
1501 */
1502 if (wpas_p2p_add_group_interface(wpa_s, go ? WPA_IF_P2P_GO :
1503 WPA_IF_P2P_CLIENT) < 0)
1504 return NULL;
1505 }
1506
1507 os_memset(&iface, 0, sizeof(iface));
1508 iface.ifname = wpa_s->pending_interface_name;
1509 iface.driver = wpa_s->driver->name;
Dmitry Shmidt34af3062013-07-11 10:46:32 -07001510 if (wpa_s->conf->ctrl_interface == NULL &&
1511 wpa_s->parent != wpa_s &&
1512 wpa_s->p2p_mgmt &&
1513 (wpa_s->drv_flags & WPA_DRIVER_FLAGS_DEDICATED_P2P_DEVICE))
1514 iface.ctrl_interface = wpa_s->parent->conf->ctrl_interface;
1515 else
1516 iface.ctrl_interface = wpa_s->conf->ctrl_interface;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001517 iface.driver_param = wpa_s->conf->driver_param;
1518 group_wpa_s = wpa_supplicant_add_iface(wpa_s->global, &iface);
1519 if (group_wpa_s == NULL) {
1520 wpa_printf(MSG_ERROR, "P2P: Failed to create new "
1521 "wpa_supplicant interface");
1522 return NULL;
1523 }
1524 wpa_s->pending_interface_name[0] = '\0';
1525 group_wpa_s->parent = wpa_s;
1526 group_wpa_s->p2p_group_interface = go ? P2P_GROUP_INTERFACE_GO :
1527 P2P_GROUP_INTERFACE_CLIENT;
1528 wpa_s->global->p2p_group_formation = group_wpa_s;
1529
1530 wpas_p2p_clone_config(group_wpa_s, wpa_s);
1531
1532 return group_wpa_s;
1533}
1534
1535
1536static void wpas_p2p_group_formation_timeout(void *eloop_ctx,
1537 void *timeout_ctx)
1538{
1539 struct wpa_supplicant *wpa_s = eloop_ctx;
1540 wpa_printf(MSG_DEBUG, "P2P: Group Formation timed out");
Dmitry Shmidt34af3062013-07-11 10:46:32 -07001541 wpas_p2p_group_formation_failed(wpa_s);
1542}
1543
1544
1545void wpas_p2p_group_formation_failed(struct wpa_supplicant *wpa_s)
1546{
1547 eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
1548 wpa_s->parent, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001549 if (wpa_s->global->p2p)
1550 p2p_group_formation_failed(wpa_s->global->p2p);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001551 wpas_group_formation_completed(wpa_s, 0);
1552}
1553
1554
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001555static void wpas_p2p_grpform_fail_after_wps(struct wpa_supplicant *wpa_s)
1556{
1557 wpa_printf(MSG_DEBUG, "P2P: Reject group formation due to WPS provisioning failure");
1558 eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
1559 wpa_s->parent, NULL);
1560 eloop_register_timeout(0, 0, wpas_p2p_group_formation_timeout,
1561 wpa_s->parent, NULL);
1562 wpa_s->global->p2p_fail_on_wps_complete = 0;
1563}
1564
1565
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07001566void wpas_p2p_ap_setup_failed(struct wpa_supplicant *wpa_s)
1567{
1568 if (wpa_s->global->p2p_group_formation != wpa_s)
1569 return;
1570 /* Speed up group formation timeout since this cannot succeed */
1571 eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
1572 wpa_s->parent, NULL);
1573 eloop_register_timeout(0, 0, wpas_p2p_group_formation_timeout,
1574 wpa_s->parent, NULL);
1575}
1576
1577
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001578static void wpas_go_neg_completed(void *ctx, struct p2p_go_neg_results *res)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001579{
1580 struct wpa_supplicant *wpa_s = ctx;
1581
1582 if (wpa_s->off_channel_freq || wpa_s->roc_waiting_drv_freq) {
1583 wpa_drv_cancel_remain_on_channel(wpa_s);
1584 wpa_s->off_channel_freq = 0;
1585 wpa_s->roc_waiting_drv_freq = 0;
1586 }
1587
1588 if (res->status) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07001589 wpa_msg_global(wpa_s, MSG_INFO,
1590 P2P_EVENT_GO_NEG_FAILURE "status=%d",
1591 res->status);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001592 wpas_notify_p2p_go_neg_completed(wpa_s, res);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001593 wpas_p2p_remove_pending_group_interface(wpa_s);
1594 return;
1595 }
1596
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001597 if (wpa_s->p2p_go_ht40)
1598 res->ht40 = 1;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07001599 if (wpa_s->p2p_go_vht)
1600 res->vht = 1;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001601
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07001602 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_GO_NEG_SUCCESS "role=%s "
1603 "freq=%d ht40=%d peer_dev=" MACSTR " peer_iface=" MACSTR
1604 " wps_method=%s",
1605 res->role_go ? "GO" : "client", res->freq, res->ht40,
1606 MAC2STR(res->peer_device_addr),
1607 MAC2STR(res->peer_interface_addr),
1608 p2p_wps_method_text(res->wps_method));
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001609 wpas_notify_p2p_go_neg_completed(wpa_s, res);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001610
Dmitry Shmidt04949592012-07-19 12:16:46 -07001611 if (res->role_go && wpa_s->p2p_persistent_id >= 0) {
1612 struct wpa_ssid *ssid;
1613 ssid = wpa_config_get_network(wpa_s->conf,
1614 wpa_s->p2p_persistent_id);
1615 if (ssid && ssid->disabled == 2 &&
1616 ssid->mode == WPAS_MODE_P2P_GO && ssid->passphrase) {
1617 size_t len = os_strlen(ssid->passphrase);
1618 wpa_printf(MSG_DEBUG, "P2P: Override passphrase based "
1619 "on requested persistent group");
1620 os_memcpy(res->passphrase, ssid->passphrase, len);
1621 res->passphrase[len] = '\0';
1622 }
1623 }
1624
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001625 if (wpa_s->create_p2p_iface) {
1626 struct wpa_supplicant *group_wpa_s =
1627 wpas_p2p_init_group_interface(wpa_s, res->role_go);
1628 if (group_wpa_s == NULL) {
1629 wpas_p2p_remove_pending_group_interface(wpa_s);
Dmitry Shmidtd11f0192014-03-24 12:09:47 -07001630 eloop_cancel_timeout(wpas_p2p_long_listen_timeout,
1631 wpa_s, NULL);
1632 wpas_p2p_group_formation_failed(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001633 return;
1634 }
1635 if (group_wpa_s != wpa_s) {
1636 os_memcpy(group_wpa_s->p2p_pin, wpa_s->p2p_pin,
1637 sizeof(group_wpa_s->p2p_pin));
1638 group_wpa_s->p2p_wps_method = wpa_s->p2p_wps_method;
1639 }
1640 os_memset(wpa_s->pending_interface_addr, 0, ETH_ALEN);
1641 wpa_s->pending_interface_name[0] = '\0';
1642 group_wpa_s->p2p_in_provisioning = 1;
1643
1644 if (res->role_go)
1645 wpas_start_wps_go(group_wpa_s, res, 1);
1646 else
1647 wpas_start_wps_enrollee(group_wpa_s, res);
1648 } else {
1649 wpa_s->p2p_in_provisioning = 1;
1650 wpa_s->global->p2p_group_formation = wpa_s;
1651
1652 if (res->role_go)
1653 wpas_start_wps_go(wpa_s, res, 1);
1654 else
1655 wpas_start_wps_enrollee(ctx, res);
1656 }
1657
1658 wpa_s->p2p_long_listen = 0;
1659 eloop_cancel_timeout(wpas_p2p_long_listen_timeout, wpa_s, NULL);
1660
1661 eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s, NULL);
1662 eloop_register_timeout(15 + res->peer_config_timeout / 100,
1663 (res->peer_config_timeout % 100) * 10000,
1664 wpas_p2p_group_formation_timeout, wpa_s, NULL);
1665}
1666
1667
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001668static void wpas_go_neg_req_rx(void *ctx, const u8 *src, u16 dev_passwd_id)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001669{
1670 struct wpa_supplicant *wpa_s = ctx;
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07001671 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_GO_NEG_REQUEST MACSTR
1672 " dev_passwd_id=%u", MAC2STR(src), dev_passwd_id);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001673
1674 wpas_notify_p2p_go_neg_req(wpa_s, src, dev_passwd_id);
1675}
1676
1677
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001678static void wpas_dev_found(void *ctx, const u8 *addr,
1679 const struct p2p_peer_info *info,
1680 int new_device)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001681{
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001682#ifndef CONFIG_NO_STDOUT_DEBUG
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001683 struct wpa_supplicant *wpa_s = ctx;
1684 char devtype[WPS_DEV_TYPE_BUFSIZE];
Dmitry Shmidt04f534e2013-12-09 15:50:16 -08001685 char *wfd_dev_info_hex = NULL;
1686
Irfan Sheriff8367dc92012-09-09 17:08:19 -07001687#ifdef CONFIG_WIFI_DISPLAY
Dmitry Shmidt04f534e2013-12-09 15:50:16 -08001688 wfd_dev_info_hex = wifi_display_subelem_hex(info->wfd_subelems,
1689 WFD_SUBELEM_DEVICE_INFO);
Irfan Sheriff8367dc92012-09-09 17:08:19 -07001690#endif /* CONFIG_WIFI_DISPLAY */
Dmitry Shmidt04f534e2013-12-09 15:50:16 -08001691
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07001692 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_DEVICE_FOUND MACSTR
1693 " p2p_dev_addr=" MACSTR
1694 " pri_dev_type=%s name='%s' config_methods=0x%x "
1695 "dev_capab=0x%x group_capab=0x%x%s%s",
1696 MAC2STR(addr), MAC2STR(info->p2p_device_addr),
1697 wps_dev_type_bin2str(info->pri_dev_type, devtype,
1698 sizeof(devtype)),
1699 info->device_name, info->config_methods,
1700 info->dev_capab, info->group_capab,
Dmitry Shmidt04f534e2013-12-09 15:50:16 -08001701 wfd_dev_info_hex ? " wfd_dev_info=0x" : "",
1702 wfd_dev_info_hex ? wfd_dev_info_hex : "");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001703
Dmitry Shmidt04f534e2013-12-09 15:50:16 -08001704 os_free(wfd_dev_info_hex);
Dmitry Shmidt97672262014-02-03 13:02:54 -08001705#endif /* CONFIG_NO_STDOUT_DEBUG */
Dmitry Shmidt04f534e2013-12-09 15:50:16 -08001706
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001707 wpas_notify_p2p_device_found(ctx, info->p2p_device_addr, new_device);
1708}
1709
1710
1711static void wpas_dev_lost(void *ctx, const u8 *dev_addr)
1712{
1713 struct wpa_supplicant *wpa_s = ctx;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001714
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07001715 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_DEVICE_LOST
1716 "p2p_dev_addr=" MACSTR, MAC2STR(dev_addr));
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001717
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001718 wpas_notify_p2p_device_lost(wpa_s, dev_addr);
1719}
1720
1721
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07001722static void wpas_find_stopped(void *ctx)
1723{
1724 struct wpa_supplicant *wpa_s = ctx;
1725 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_FIND_STOPPED);
1726}
1727
1728
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001729struct wpas_p2p_listen_work {
1730 unsigned int freq;
1731 unsigned int duration;
1732 struct wpabuf *probe_resp_ie;
1733};
1734
1735
1736static void wpas_p2p_listen_work_free(struct wpas_p2p_listen_work *lwork)
1737{
1738 if (lwork == NULL)
1739 return;
1740 wpabuf_free(lwork->probe_resp_ie);
1741 os_free(lwork);
1742}
1743
1744
1745static void wpas_p2p_listen_work_done(struct wpa_supplicant *wpa_s)
1746{
1747 struct wpas_p2p_listen_work *lwork;
1748
1749 if (!wpa_s->p2p_listen_work)
1750 return;
1751
1752 lwork = wpa_s->p2p_listen_work->ctx;
1753 wpas_p2p_listen_work_free(lwork);
1754 radio_work_done(wpa_s->p2p_listen_work);
1755 wpa_s->p2p_listen_work = NULL;
1756}
1757
1758
1759static void wpas_start_listen_cb(struct wpa_radio_work *work, int deinit)
1760{
1761 struct wpa_supplicant *wpa_s = work->wpa_s;
1762 struct wpas_p2p_listen_work *lwork = work->ctx;
1763
1764 if (deinit) {
Dmitry Shmidtbd14a572014-02-18 10:33:49 -08001765 if (work->started) {
1766 wpa_s->p2p_listen_work = NULL;
1767 wpas_stop_listen(wpa_s);
1768 }
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001769 wpas_p2p_listen_work_free(lwork);
1770 return;
1771 }
1772
1773 wpa_s->p2p_listen_work = work;
1774
1775 wpa_drv_set_ap_wps_ie(wpa_s, NULL, lwork->probe_resp_ie, NULL);
1776
1777 if (wpa_drv_probe_req_report(wpa_s, 1) < 0) {
1778 wpa_printf(MSG_DEBUG, "P2P: Failed to request the driver to "
1779 "report received Probe Request frames");
1780 wpas_p2p_listen_work_done(wpa_s);
1781 return;
1782 }
1783
1784 wpa_s->pending_listen_freq = lwork->freq;
1785 wpa_s->pending_listen_duration = lwork->duration;
1786
1787 if (wpa_drv_remain_on_channel(wpa_s, lwork->freq, lwork->duration) < 0)
1788 {
1789 wpa_printf(MSG_DEBUG, "P2P: Failed to request the driver "
1790 "to remain on channel (%u MHz) for Listen "
1791 "state", lwork->freq);
1792 wpas_p2p_listen_work_done(wpa_s);
1793 wpa_s->pending_listen_freq = 0;
1794 return;
1795 }
1796 wpa_s->off_channel_freq = 0;
1797 wpa_s->roc_waiting_drv_freq = lwork->freq;
1798}
1799
1800
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001801static int wpas_start_listen(void *ctx, unsigned int freq,
1802 unsigned int duration,
1803 const struct wpabuf *probe_resp_ie)
1804{
1805 struct wpa_supplicant *wpa_s = ctx;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001806 struct wpas_p2p_listen_work *lwork;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001807
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001808 if (wpa_s->p2p_listen_work) {
1809 wpa_printf(MSG_DEBUG, "P2P: Reject start_listen since p2p_listen_work already exists");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001810 return -1;
1811 }
1812
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001813 lwork = os_zalloc(sizeof(*lwork));
1814 if (lwork == NULL)
1815 return -1;
1816 lwork->freq = freq;
1817 lwork->duration = duration;
1818 if (probe_resp_ie) {
1819 lwork->probe_resp_ie = wpabuf_dup(probe_resp_ie);
1820 if (lwork->probe_resp_ie == NULL) {
1821 wpas_p2p_listen_work_free(lwork);
1822 return -1;
1823 }
1824 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001825
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001826 if (radio_add_work(wpa_s, freq, "p2p-listen", 0, wpas_start_listen_cb,
1827 lwork) < 0) {
1828 wpas_p2p_listen_work_free(lwork);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001829 return -1;
1830 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001831
1832 return 0;
1833}
1834
1835
1836static void wpas_stop_listen(void *ctx)
1837{
1838 struct wpa_supplicant *wpa_s = ctx;
1839 if (wpa_s->off_channel_freq || wpa_s->roc_waiting_drv_freq) {
1840 wpa_drv_cancel_remain_on_channel(wpa_s);
1841 wpa_s->off_channel_freq = 0;
1842 wpa_s->roc_waiting_drv_freq = 0;
1843 }
1844 wpa_drv_set_ap_wps_ie(wpa_s, NULL, NULL, NULL);
1845 wpa_drv_probe_req_report(wpa_s, 0);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001846 wpas_p2p_listen_work_done(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001847}
1848
1849
1850static int wpas_send_probe_resp(void *ctx, const struct wpabuf *buf)
1851{
1852 struct wpa_supplicant *wpa_s = ctx;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001853 return wpa_drv_send_mlme(wpa_s, wpabuf_head(buf), wpabuf_len(buf), 1);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001854}
1855
1856
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001857/*
1858 * DNS Header section is used only to calculate compression pointers, so the
1859 * contents of this data does not matter, but the length needs to be reserved
1860 * in the virtual packet.
1861 */
1862#define DNS_HEADER_LEN 12
1863
1864/*
1865 * 27-octet in-memory packet from P2P specification containing two implied
1866 * queries for _tcp.lcoal. PTR IN and _udp.local. PTR IN
1867 */
1868#define P2P_SD_IN_MEMORY_LEN 27
1869
1870static int p2p_sd_dns_uncompress_label(char **upos, char *uend, u8 *start,
1871 u8 **spos, const u8 *end)
1872{
1873 while (*spos < end) {
1874 u8 val = ((*spos)[0] & 0xc0) >> 6;
1875 int len;
1876
1877 if (val == 1 || val == 2) {
1878 /* These are reserved values in RFC 1035 */
1879 wpa_printf(MSG_DEBUG, "P2P: Invalid domain name "
1880 "sequence starting with 0x%x", val);
1881 return -1;
1882 }
1883
1884 if (val == 3) {
1885 u16 offset;
1886 u8 *spos_tmp;
1887
1888 /* Offset */
1889 if (*spos + 2 > end) {
1890 wpa_printf(MSG_DEBUG, "P2P: No room for full "
1891 "DNS offset field");
1892 return -1;
1893 }
1894
1895 offset = (((*spos)[0] & 0x3f) << 8) | (*spos)[1];
1896 if (offset >= *spos - start) {
1897 wpa_printf(MSG_DEBUG, "P2P: Invalid DNS "
1898 "pointer offset %u", offset);
1899 return -1;
1900 }
1901
1902 (*spos) += 2;
1903 spos_tmp = start + offset;
1904 return p2p_sd_dns_uncompress_label(upos, uend, start,
1905 &spos_tmp,
1906 *spos - 2);
1907 }
1908
1909 /* Label */
1910 len = (*spos)[0] & 0x3f;
1911 if (len == 0)
1912 return 0;
1913
1914 (*spos)++;
1915 if (*spos + len > end) {
1916 wpa_printf(MSG_DEBUG, "P2P: Invalid domain name "
1917 "sequence - no room for label with length "
1918 "%u", len);
1919 return -1;
1920 }
1921
1922 if (*upos + len + 2 > uend)
1923 return -2;
1924
1925 os_memcpy(*upos, *spos, len);
1926 *spos += len;
1927 *upos += len;
1928 (*upos)[0] = '.';
1929 (*upos)++;
1930 (*upos)[0] = '\0';
1931 }
1932
1933 return 0;
1934}
1935
1936
1937/* Uncompress domain names per RFC 1035 using the P2P SD in-memory packet.
1938 * Returns -1 on parsing error (invalid input sequence), -2 if output buffer is
1939 * not large enough */
1940static int p2p_sd_dns_uncompress(char *buf, size_t buf_len, const u8 *msg,
1941 size_t msg_len, size_t offset)
1942{
1943 /* 27-octet in-memory packet from P2P specification */
1944 const char *prefix = "\x04_tcp\x05local\x00\x00\x0C\x00\x01"
1945 "\x04_udp\xC0\x11\x00\x0C\x00\x01";
1946 u8 *tmp, *end, *spos;
1947 char *upos, *uend;
1948 int ret = 0;
1949
1950 if (buf_len < 2)
1951 return -1;
1952 if (offset > msg_len)
1953 return -1;
1954
1955 tmp = os_malloc(DNS_HEADER_LEN + P2P_SD_IN_MEMORY_LEN + msg_len);
1956 if (tmp == NULL)
1957 return -1;
1958 spos = tmp + DNS_HEADER_LEN + P2P_SD_IN_MEMORY_LEN;
1959 end = spos + msg_len;
1960 spos += offset;
1961
1962 os_memset(tmp, 0, DNS_HEADER_LEN);
1963 os_memcpy(tmp + DNS_HEADER_LEN, prefix, P2P_SD_IN_MEMORY_LEN);
1964 os_memcpy(tmp + DNS_HEADER_LEN + P2P_SD_IN_MEMORY_LEN, msg, msg_len);
1965
1966 upos = buf;
1967 uend = buf + buf_len;
1968
1969 ret = p2p_sd_dns_uncompress_label(&upos, uend, tmp, &spos, end);
1970 if (ret) {
1971 os_free(tmp);
1972 return ret;
1973 }
1974
1975 if (upos == buf) {
1976 upos[0] = '.';
1977 upos[1] = '\0';
1978 } else if (upos[-1] == '.')
1979 upos[-1] = '\0';
1980
1981 os_free(tmp);
1982 return 0;
1983}
1984
1985
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001986static struct p2p_srv_bonjour *
1987wpas_p2p_service_get_bonjour(struct wpa_supplicant *wpa_s,
1988 const struct wpabuf *query)
1989{
1990 struct p2p_srv_bonjour *bsrv;
1991 size_t len;
1992
1993 len = wpabuf_len(query);
1994 dl_list_for_each(bsrv, &wpa_s->global->p2p_srv_bonjour,
1995 struct p2p_srv_bonjour, list) {
1996 if (len == wpabuf_len(bsrv->query) &&
1997 os_memcmp(wpabuf_head(query), wpabuf_head(bsrv->query),
1998 len) == 0)
1999 return bsrv;
2000 }
2001 return NULL;
2002}
2003
2004
2005static struct p2p_srv_upnp *
2006wpas_p2p_service_get_upnp(struct wpa_supplicant *wpa_s, u8 version,
2007 const char *service)
2008{
2009 struct p2p_srv_upnp *usrv;
2010
2011 dl_list_for_each(usrv, &wpa_s->global->p2p_srv_upnp,
2012 struct p2p_srv_upnp, list) {
2013 if (version == usrv->version &&
2014 os_strcmp(service, usrv->service) == 0)
2015 return usrv;
2016 }
2017 return NULL;
2018}
2019
2020
2021static void wpas_sd_add_proto_not_avail(struct wpabuf *resp, u8 srv_proto,
2022 u8 srv_trans_id)
2023{
2024 u8 *len_pos;
2025
2026 if (wpabuf_tailroom(resp) < 5)
2027 return;
2028
2029 /* Length (to be filled) */
2030 len_pos = wpabuf_put(resp, 2);
2031 wpabuf_put_u8(resp, srv_proto);
2032 wpabuf_put_u8(resp, srv_trans_id);
2033 /* Status Code */
2034 wpabuf_put_u8(resp, P2P_SD_PROTO_NOT_AVAILABLE);
2035 /* Response Data: empty */
2036 WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos - 2);
2037}
2038
2039
2040static void wpas_sd_all_bonjour(struct wpa_supplicant *wpa_s,
2041 struct wpabuf *resp, u8 srv_trans_id)
2042{
2043 struct p2p_srv_bonjour *bsrv;
2044 u8 *len_pos;
2045
2046 wpa_printf(MSG_DEBUG, "P2P: SD Request for all Bonjour services");
2047
2048 if (dl_list_empty(&wpa_s->global->p2p_srv_bonjour)) {
2049 wpa_printf(MSG_DEBUG, "P2P: Bonjour protocol not available");
2050 return;
2051 }
2052
2053 dl_list_for_each(bsrv, &wpa_s->global->p2p_srv_bonjour,
2054 struct p2p_srv_bonjour, list) {
2055 if (wpabuf_tailroom(resp) <
2056 5 + wpabuf_len(bsrv->query) + wpabuf_len(bsrv->resp))
2057 return;
2058 /* Length (to be filled) */
2059 len_pos = wpabuf_put(resp, 2);
2060 wpabuf_put_u8(resp, P2P_SERV_BONJOUR);
2061 wpabuf_put_u8(resp, srv_trans_id);
2062 /* Status Code */
2063 wpabuf_put_u8(resp, P2P_SD_SUCCESS);
2064 wpa_hexdump_ascii(MSG_DEBUG, "P2P: Matching Bonjour service",
2065 wpabuf_head(bsrv->resp),
2066 wpabuf_len(bsrv->resp));
2067 /* Response Data */
2068 wpabuf_put_buf(resp, bsrv->query); /* Key */
2069 wpabuf_put_buf(resp, bsrv->resp); /* Value */
2070 WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos -
2071 2);
2072 }
2073}
2074
2075
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002076static int match_bonjour_query(struct p2p_srv_bonjour *bsrv, const u8 *query,
2077 size_t query_len)
2078{
2079 char str_rx[256], str_srv[256];
2080
2081 if (query_len < 3 || wpabuf_len(bsrv->query) < 3)
2082 return 0; /* Too short to include DNS Type and Version */
2083 if (os_memcmp(query + query_len - 3,
2084 wpabuf_head_u8(bsrv->query) + wpabuf_len(bsrv->query) - 3,
2085 3) != 0)
2086 return 0; /* Mismatch in DNS Type or Version */
2087 if (query_len == wpabuf_len(bsrv->query) &&
2088 os_memcmp(query, wpabuf_head(bsrv->query), query_len - 3) == 0)
2089 return 1; /* Binary match */
2090
2091 if (p2p_sd_dns_uncompress(str_rx, sizeof(str_rx), query, query_len - 3,
2092 0))
2093 return 0; /* Failed to uncompress query */
2094 if (p2p_sd_dns_uncompress(str_srv, sizeof(str_srv),
2095 wpabuf_head(bsrv->query),
2096 wpabuf_len(bsrv->query) - 3, 0))
2097 return 0; /* Failed to uncompress service */
2098
2099 return os_strcmp(str_rx, str_srv) == 0;
2100}
2101
2102
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002103static void wpas_sd_req_bonjour(struct wpa_supplicant *wpa_s,
2104 struct wpabuf *resp, u8 srv_trans_id,
2105 const u8 *query, size_t query_len)
2106{
2107 struct p2p_srv_bonjour *bsrv;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002108 u8 *len_pos;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002109 int matches = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002110
2111 wpa_hexdump_ascii(MSG_DEBUG, "P2P: SD Request for Bonjour",
2112 query, query_len);
2113 if (dl_list_empty(&wpa_s->global->p2p_srv_bonjour)) {
2114 wpa_printf(MSG_DEBUG, "P2P: Bonjour protocol not available");
2115 wpas_sd_add_proto_not_avail(resp, P2P_SERV_BONJOUR,
2116 srv_trans_id);
2117 return;
2118 }
2119
2120 if (query_len == 0) {
2121 wpas_sd_all_bonjour(wpa_s, resp, srv_trans_id);
2122 return;
2123 }
2124
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002125 dl_list_for_each(bsrv, &wpa_s->global->p2p_srv_bonjour,
2126 struct p2p_srv_bonjour, list) {
2127 if (!match_bonjour_query(bsrv, query, query_len))
2128 continue;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002129
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002130 if (wpabuf_tailroom(resp) <
2131 5 + query_len + wpabuf_len(bsrv->resp))
2132 return;
2133
2134 matches++;
2135
2136 /* Length (to be filled) */
2137 len_pos = wpabuf_put(resp, 2);
2138 wpabuf_put_u8(resp, P2P_SERV_BONJOUR);
2139 wpabuf_put_u8(resp, srv_trans_id);
2140
2141 /* Status Code */
2142 wpabuf_put_u8(resp, P2P_SD_SUCCESS);
2143 wpa_hexdump_ascii(MSG_DEBUG, "P2P: Matching Bonjour service",
2144 wpabuf_head(bsrv->resp),
2145 wpabuf_len(bsrv->resp));
2146
2147 /* Response Data */
2148 wpabuf_put_data(resp, query, query_len); /* Key */
2149 wpabuf_put_buf(resp, bsrv->resp); /* Value */
2150
2151 WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos - 2);
2152 }
2153
2154 if (matches == 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002155 wpa_printf(MSG_DEBUG, "P2P: Requested Bonjour service not "
2156 "available");
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002157 if (wpabuf_tailroom(resp) < 5)
2158 return;
2159
2160 /* Length (to be filled) */
2161 len_pos = wpabuf_put(resp, 2);
2162 wpabuf_put_u8(resp, P2P_SERV_BONJOUR);
2163 wpabuf_put_u8(resp, srv_trans_id);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002164
2165 /* Status Code */
2166 wpabuf_put_u8(resp, P2P_SD_REQUESTED_INFO_NOT_AVAILABLE);
2167 /* Response Data: empty */
2168 WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos -
2169 2);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002170 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002171}
2172
2173
2174static void wpas_sd_all_upnp(struct wpa_supplicant *wpa_s,
2175 struct wpabuf *resp, u8 srv_trans_id)
2176{
2177 struct p2p_srv_upnp *usrv;
2178 u8 *len_pos;
2179
2180 wpa_printf(MSG_DEBUG, "P2P: SD Request for all UPnP services");
2181
2182 if (dl_list_empty(&wpa_s->global->p2p_srv_upnp)) {
2183 wpa_printf(MSG_DEBUG, "P2P: UPnP protocol not available");
2184 return;
2185 }
2186
2187 dl_list_for_each(usrv, &wpa_s->global->p2p_srv_upnp,
2188 struct p2p_srv_upnp, list) {
2189 if (wpabuf_tailroom(resp) < 5 + 1 + os_strlen(usrv->service))
2190 return;
2191
2192 /* Length (to be filled) */
2193 len_pos = wpabuf_put(resp, 2);
2194 wpabuf_put_u8(resp, P2P_SERV_UPNP);
2195 wpabuf_put_u8(resp, srv_trans_id);
2196
2197 /* Status Code */
2198 wpabuf_put_u8(resp, P2P_SD_SUCCESS);
2199 /* Response Data */
2200 wpabuf_put_u8(resp, usrv->version);
2201 wpa_printf(MSG_DEBUG, "P2P: Matching UPnP Service: %s",
2202 usrv->service);
2203 wpabuf_put_str(resp, usrv->service);
2204 WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos -
2205 2);
2206 }
2207}
2208
2209
2210static void wpas_sd_req_upnp(struct wpa_supplicant *wpa_s,
2211 struct wpabuf *resp, u8 srv_trans_id,
2212 const u8 *query, size_t query_len)
2213{
2214 struct p2p_srv_upnp *usrv;
2215 u8 *len_pos;
2216 u8 version;
2217 char *str;
2218 int count = 0;
2219
2220 wpa_hexdump_ascii(MSG_DEBUG, "P2P: SD Request for UPnP",
2221 query, query_len);
2222
2223 if (dl_list_empty(&wpa_s->global->p2p_srv_upnp)) {
2224 wpa_printf(MSG_DEBUG, "P2P: UPnP protocol not available");
2225 wpas_sd_add_proto_not_avail(resp, P2P_SERV_UPNP,
2226 srv_trans_id);
2227 return;
2228 }
2229
2230 if (query_len == 0) {
2231 wpas_sd_all_upnp(wpa_s, resp, srv_trans_id);
2232 return;
2233 }
2234
2235 if (wpabuf_tailroom(resp) < 5)
2236 return;
2237
2238 /* Length (to be filled) */
2239 len_pos = wpabuf_put(resp, 2);
2240 wpabuf_put_u8(resp, P2P_SERV_UPNP);
2241 wpabuf_put_u8(resp, srv_trans_id);
2242
2243 version = query[0];
2244 str = os_malloc(query_len);
2245 if (str == NULL)
2246 return;
2247 os_memcpy(str, query + 1, query_len - 1);
2248 str[query_len - 1] = '\0';
2249
2250 dl_list_for_each(usrv, &wpa_s->global->p2p_srv_upnp,
2251 struct p2p_srv_upnp, list) {
2252 if (version != usrv->version)
2253 continue;
2254
2255 if (os_strcmp(str, "ssdp:all") != 0 &&
2256 os_strstr(usrv->service, str) == NULL)
2257 continue;
2258
2259 if (wpabuf_tailroom(resp) < 2)
2260 break;
2261 if (count == 0) {
2262 /* Status Code */
2263 wpabuf_put_u8(resp, P2P_SD_SUCCESS);
2264 /* Response Data */
2265 wpabuf_put_u8(resp, version);
2266 } else
2267 wpabuf_put_u8(resp, ',');
2268
2269 count++;
2270
2271 wpa_printf(MSG_DEBUG, "P2P: Matching UPnP Service: %s",
2272 usrv->service);
2273 if (wpabuf_tailroom(resp) < os_strlen(usrv->service))
2274 break;
2275 wpabuf_put_str(resp, usrv->service);
2276 }
2277 os_free(str);
2278
2279 if (count == 0) {
2280 wpa_printf(MSG_DEBUG, "P2P: Requested UPnP service not "
2281 "available");
2282 /* Status Code */
2283 wpabuf_put_u8(resp, P2P_SD_REQUESTED_INFO_NOT_AVAILABLE);
2284 /* Response Data: empty */
2285 }
2286
2287 WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos - 2);
2288}
2289
2290
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002291#ifdef CONFIG_WIFI_DISPLAY
2292static void wpas_sd_req_wfd(struct wpa_supplicant *wpa_s,
2293 struct wpabuf *resp, u8 srv_trans_id,
2294 const u8 *query, size_t query_len)
2295{
2296 const u8 *pos;
2297 u8 role;
2298 u8 *len_pos;
2299
2300 wpa_hexdump(MSG_DEBUG, "P2P: SD Request for WFD", query, query_len);
2301
2302 if (!wpa_s->global->wifi_display) {
2303 wpa_printf(MSG_DEBUG, "P2P: WFD protocol not available");
2304 wpas_sd_add_proto_not_avail(resp, P2P_SERV_WIFI_DISPLAY,
2305 srv_trans_id);
2306 return;
2307 }
2308
2309 if (query_len < 1) {
2310 wpa_printf(MSG_DEBUG, "P2P: Missing WFD Requested Device "
2311 "Role");
2312 return;
2313 }
2314
2315 if (wpabuf_tailroom(resp) < 5)
2316 return;
2317
2318 pos = query;
2319 role = *pos++;
2320 wpa_printf(MSG_DEBUG, "P2P: WSD for device role 0x%x", role);
2321
2322 /* TODO: role specific handling */
2323
2324 /* Length (to be filled) */
2325 len_pos = wpabuf_put(resp, 2);
2326 wpabuf_put_u8(resp, P2P_SERV_WIFI_DISPLAY);
2327 wpabuf_put_u8(resp, srv_trans_id);
2328 wpabuf_put_u8(resp, P2P_SD_SUCCESS); /* Status Code */
2329
2330 while (pos < query + query_len) {
2331 if (*pos < MAX_WFD_SUBELEMS &&
2332 wpa_s->global->wfd_subelem[*pos] &&
2333 wpabuf_tailroom(resp) >=
2334 wpabuf_len(wpa_s->global->wfd_subelem[*pos])) {
2335 wpa_printf(MSG_DEBUG, "P2P: Add WSD response "
2336 "subelement %u", *pos);
2337 wpabuf_put_buf(resp, wpa_s->global->wfd_subelem[*pos]);
2338 }
2339 pos++;
2340 }
2341
2342 WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos - 2);
2343}
2344#endif /* CONFIG_WIFI_DISPLAY */
2345
2346
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002347static void wpas_sd_request(void *ctx, int freq, const u8 *sa, u8 dialog_token,
2348 u16 update_indic, const u8 *tlvs, size_t tlvs_len)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002349{
2350 struct wpa_supplicant *wpa_s = ctx;
2351 const u8 *pos = tlvs;
2352 const u8 *end = tlvs + tlvs_len;
2353 const u8 *tlv_end;
2354 u16 slen;
2355 struct wpabuf *resp;
2356 u8 srv_proto, srv_trans_id;
2357 size_t buf_len;
2358 char *buf;
2359
2360 wpa_hexdump(MSG_MSGDUMP, "P2P: Service Discovery Request TLVs",
2361 tlvs, tlvs_len);
2362 buf_len = 2 * tlvs_len + 1;
2363 buf = os_malloc(buf_len);
2364 if (buf) {
2365 wpa_snprintf_hex(buf, buf_len, tlvs, tlvs_len);
2366 wpa_msg_ctrl(wpa_s, MSG_INFO, P2P_EVENT_SERV_DISC_REQ "%d "
2367 MACSTR " %u %u %s",
2368 freq, MAC2STR(sa), dialog_token, update_indic,
2369 buf);
2370 os_free(buf);
2371 }
2372
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002373 if (wpa_s->p2p_sd_over_ctrl_iface) {
2374 wpas_notify_p2p_sd_request(wpa_s, freq, sa, dialog_token,
2375 update_indic, tlvs, tlvs_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002376 return; /* to be processed by an external program */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002377 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002378
2379 resp = wpabuf_alloc(10000);
2380 if (resp == NULL)
2381 return;
2382
2383 while (pos + 1 < end) {
2384 wpa_printf(MSG_DEBUG, "P2P: Service Request TLV");
2385 slen = WPA_GET_LE16(pos);
2386 pos += 2;
2387 if (pos + slen > end || slen < 2) {
2388 wpa_printf(MSG_DEBUG, "P2P: Unexpected Query Data "
2389 "length");
2390 wpabuf_free(resp);
2391 return;
2392 }
2393 tlv_end = pos + slen;
2394
2395 srv_proto = *pos++;
2396 wpa_printf(MSG_DEBUG, "P2P: Service Protocol Type %u",
2397 srv_proto);
2398 srv_trans_id = *pos++;
2399 wpa_printf(MSG_DEBUG, "P2P: Service Transaction ID %u",
2400 srv_trans_id);
2401
2402 wpa_hexdump(MSG_MSGDUMP, "P2P: Query Data",
2403 pos, tlv_end - pos);
2404
2405
2406 if (wpa_s->force_long_sd) {
2407 wpa_printf(MSG_DEBUG, "P2P: SD test - force long "
2408 "response");
2409 wpas_sd_all_bonjour(wpa_s, resp, srv_trans_id);
2410 wpas_sd_all_upnp(wpa_s, resp, srv_trans_id);
2411 goto done;
2412 }
2413
2414 switch (srv_proto) {
2415 case P2P_SERV_ALL_SERVICES:
2416 wpa_printf(MSG_DEBUG, "P2P: Service Discovery Request "
2417 "for all services");
2418 if (dl_list_empty(&wpa_s->global->p2p_srv_upnp) &&
2419 dl_list_empty(&wpa_s->global->p2p_srv_bonjour)) {
2420 wpa_printf(MSG_DEBUG, "P2P: No service "
2421 "discovery protocols available");
2422 wpas_sd_add_proto_not_avail(
2423 resp, P2P_SERV_ALL_SERVICES,
2424 srv_trans_id);
2425 break;
2426 }
2427 wpas_sd_all_bonjour(wpa_s, resp, srv_trans_id);
2428 wpas_sd_all_upnp(wpa_s, resp, srv_trans_id);
2429 break;
2430 case P2P_SERV_BONJOUR:
2431 wpas_sd_req_bonjour(wpa_s, resp, srv_trans_id,
2432 pos, tlv_end - pos);
2433 break;
2434 case P2P_SERV_UPNP:
2435 wpas_sd_req_upnp(wpa_s, resp, srv_trans_id,
2436 pos, tlv_end - pos);
2437 break;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002438#ifdef CONFIG_WIFI_DISPLAY
2439 case P2P_SERV_WIFI_DISPLAY:
2440 wpas_sd_req_wfd(wpa_s, resp, srv_trans_id,
2441 pos, tlv_end - pos);
2442 break;
2443#endif /* CONFIG_WIFI_DISPLAY */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002444 default:
2445 wpa_printf(MSG_DEBUG, "P2P: Unavailable service "
2446 "protocol %u", srv_proto);
2447 wpas_sd_add_proto_not_avail(resp, srv_proto,
2448 srv_trans_id);
2449 break;
2450 }
2451
2452 pos = tlv_end;
2453 }
2454
2455done:
2456 wpas_notify_p2p_sd_request(wpa_s, freq, sa, dialog_token,
2457 update_indic, tlvs, tlvs_len);
2458
2459 wpas_p2p_sd_response(wpa_s, freq, sa, dialog_token, resp);
2460
2461 wpabuf_free(resp);
2462}
2463
2464
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002465static void wpas_sd_response(void *ctx, const u8 *sa, u16 update_indic,
2466 const u8 *tlvs, size_t tlvs_len)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002467{
2468 struct wpa_supplicant *wpa_s = ctx;
2469 const u8 *pos = tlvs;
2470 const u8 *end = tlvs + tlvs_len;
2471 const u8 *tlv_end;
2472 u16 slen;
2473 size_t buf_len;
2474 char *buf;
2475
2476 wpa_hexdump(MSG_MSGDUMP, "P2P: Service Discovery Response TLVs",
2477 tlvs, tlvs_len);
2478 if (tlvs_len > 1500) {
2479 /* TODO: better way for handling this */
2480 wpa_msg_ctrl(wpa_s, MSG_INFO,
2481 P2P_EVENT_SERV_DISC_RESP MACSTR
2482 " %u <long response: %u bytes>",
2483 MAC2STR(sa), update_indic,
2484 (unsigned int) tlvs_len);
2485 } else {
2486 buf_len = 2 * tlvs_len + 1;
2487 buf = os_malloc(buf_len);
2488 if (buf) {
2489 wpa_snprintf_hex(buf, buf_len, tlvs, tlvs_len);
2490 wpa_msg_ctrl(wpa_s, MSG_INFO,
2491 P2P_EVENT_SERV_DISC_RESP MACSTR " %u %s",
2492 MAC2STR(sa), update_indic, buf);
2493 os_free(buf);
2494 }
2495 }
2496
2497 while (pos < end) {
2498 u8 srv_proto, srv_trans_id, status;
2499
2500 wpa_printf(MSG_DEBUG, "P2P: Service Response TLV");
2501 slen = WPA_GET_LE16(pos);
2502 pos += 2;
2503 if (pos + slen > end || slen < 3) {
2504 wpa_printf(MSG_DEBUG, "P2P: Unexpected Response Data "
2505 "length");
2506 return;
2507 }
2508 tlv_end = pos + slen;
2509
2510 srv_proto = *pos++;
2511 wpa_printf(MSG_DEBUG, "P2P: Service Protocol Type %u",
2512 srv_proto);
2513 srv_trans_id = *pos++;
2514 wpa_printf(MSG_DEBUG, "P2P: Service Transaction ID %u",
2515 srv_trans_id);
2516 status = *pos++;
2517 wpa_printf(MSG_DEBUG, "P2P: Status Code ID %u",
2518 status);
2519
2520 wpa_hexdump(MSG_MSGDUMP, "P2P: Response Data",
2521 pos, tlv_end - pos);
2522
2523 pos = tlv_end;
2524 }
2525
2526 wpas_notify_p2p_sd_response(wpa_s, sa, update_indic, tlvs, tlvs_len);
2527}
2528
2529
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002530u64 wpas_p2p_sd_request(struct wpa_supplicant *wpa_s, const u8 *dst,
2531 const struct wpabuf *tlvs)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002532{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002533 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002534 return 0;
2535 return (uintptr_t) p2p_sd_request(wpa_s->global->p2p, dst, tlvs);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002536}
2537
2538
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002539u64 wpas_p2p_sd_request_upnp(struct wpa_supplicant *wpa_s, const u8 *dst,
2540 u8 version, const char *query)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002541{
2542 struct wpabuf *tlvs;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002543 u64 ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002544
2545 tlvs = wpabuf_alloc(2 + 1 + 1 + 1 + os_strlen(query));
2546 if (tlvs == NULL)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002547 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002548 wpabuf_put_le16(tlvs, 1 + 1 + 1 + os_strlen(query));
2549 wpabuf_put_u8(tlvs, P2P_SERV_UPNP); /* Service Protocol Type */
2550 wpabuf_put_u8(tlvs, 1); /* Service Transaction ID */
2551 wpabuf_put_u8(tlvs, version);
2552 wpabuf_put_str(tlvs, query);
2553 ret = wpas_p2p_sd_request(wpa_s, dst, tlvs);
2554 wpabuf_free(tlvs);
2555 return ret;
2556}
2557
2558
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002559#ifdef CONFIG_WIFI_DISPLAY
2560
2561static u64 wpas_p2p_sd_request_wfd(struct wpa_supplicant *wpa_s, const u8 *dst,
2562 const struct wpabuf *tlvs)
2563{
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002564 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
2565 return 0;
2566 return (uintptr_t) p2p_sd_request_wfd(wpa_s->global->p2p, dst, tlvs);
2567}
2568
2569
2570#define MAX_WFD_SD_SUBELEMS 20
2571
2572static void wfd_add_sd_req_role(struct wpabuf *tlvs, u8 id, u8 role,
2573 const char *subelems)
2574{
2575 u8 *len;
2576 const char *pos;
2577 int val;
2578 int count = 0;
2579
2580 len = wpabuf_put(tlvs, 2);
2581 wpabuf_put_u8(tlvs, P2P_SERV_WIFI_DISPLAY); /* Service Protocol Type */
2582 wpabuf_put_u8(tlvs, id); /* Service Transaction ID */
2583
2584 wpabuf_put_u8(tlvs, role);
2585
2586 pos = subelems;
2587 while (*pos) {
2588 val = atoi(pos);
2589 if (val >= 0 && val < 256) {
2590 wpabuf_put_u8(tlvs, val);
2591 count++;
2592 if (count == MAX_WFD_SD_SUBELEMS)
2593 break;
2594 }
2595 pos = os_strchr(pos + 1, ',');
2596 if (pos == NULL)
2597 break;
2598 pos++;
2599 }
2600
2601 WPA_PUT_LE16(len, (u8 *) wpabuf_put(tlvs, 0) - len - 2);
2602}
2603
2604
2605u64 wpas_p2p_sd_request_wifi_display(struct wpa_supplicant *wpa_s,
2606 const u8 *dst, const char *role)
2607{
2608 struct wpabuf *tlvs;
2609 u64 ret;
2610 const char *subelems;
2611 u8 id = 1;
2612
2613 subelems = os_strchr(role, ' ');
2614 if (subelems == NULL)
2615 return 0;
2616 subelems++;
2617
2618 tlvs = wpabuf_alloc(4 * (2 + 1 + 1 + 1 + MAX_WFD_SD_SUBELEMS));
2619 if (tlvs == NULL)
2620 return 0;
2621
2622 if (os_strstr(role, "[source]"))
2623 wfd_add_sd_req_role(tlvs, id++, 0x00, subelems);
2624 if (os_strstr(role, "[pri-sink]"))
2625 wfd_add_sd_req_role(tlvs, id++, 0x01, subelems);
2626 if (os_strstr(role, "[sec-sink]"))
2627 wfd_add_sd_req_role(tlvs, id++, 0x02, subelems);
2628 if (os_strstr(role, "[source+sink]"))
2629 wfd_add_sd_req_role(tlvs, id++, 0x03, subelems);
2630
2631 ret = wpas_p2p_sd_request_wfd(wpa_s, dst, tlvs);
2632 wpabuf_free(tlvs);
2633 return ret;
2634}
2635
2636#endif /* CONFIG_WIFI_DISPLAY */
2637
2638
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002639int wpas_p2p_sd_cancel_request(struct wpa_supplicant *wpa_s, u64 req)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002640{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002641 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
2642 return -1;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002643 return p2p_sd_cancel_request(wpa_s->global->p2p,
2644 (void *) (uintptr_t) req);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002645}
2646
2647
2648void wpas_p2p_sd_response(struct wpa_supplicant *wpa_s, int freq,
2649 const u8 *dst, u8 dialog_token,
2650 const struct wpabuf *resp_tlvs)
2651{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002652 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
2653 return;
2654 p2p_sd_response(wpa_s->global->p2p, freq, dst, dialog_token,
2655 resp_tlvs);
2656}
2657
Dmitry Shmidteaf261d2013-08-14 15:30:08 -07002658
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002659void wpas_p2p_sd_service_update(struct wpa_supplicant *wpa_s)
2660{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002661 if (wpa_s->global->p2p)
2662 p2p_sd_service_update(wpa_s->global->p2p);
2663}
2664
2665
2666static void wpas_p2p_srv_bonjour_free(struct p2p_srv_bonjour *bsrv)
2667{
2668 dl_list_del(&bsrv->list);
2669 wpabuf_free(bsrv->query);
2670 wpabuf_free(bsrv->resp);
2671 os_free(bsrv);
2672}
2673
2674
2675static void wpas_p2p_srv_upnp_free(struct p2p_srv_upnp *usrv)
2676{
2677 dl_list_del(&usrv->list);
2678 os_free(usrv->service);
2679 os_free(usrv);
2680}
2681
2682
2683void wpas_p2p_service_flush(struct wpa_supplicant *wpa_s)
2684{
2685 struct p2p_srv_bonjour *bsrv, *bn;
2686 struct p2p_srv_upnp *usrv, *un;
2687
2688 dl_list_for_each_safe(bsrv, bn, &wpa_s->global->p2p_srv_bonjour,
2689 struct p2p_srv_bonjour, list)
2690 wpas_p2p_srv_bonjour_free(bsrv);
2691
2692 dl_list_for_each_safe(usrv, un, &wpa_s->global->p2p_srv_upnp,
2693 struct p2p_srv_upnp, list)
2694 wpas_p2p_srv_upnp_free(usrv);
2695
2696 wpas_p2p_sd_service_update(wpa_s);
2697}
2698
2699
2700int wpas_p2p_service_add_bonjour(struct wpa_supplicant *wpa_s,
2701 struct wpabuf *query, struct wpabuf *resp)
2702{
2703 struct p2p_srv_bonjour *bsrv;
2704
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002705 bsrv = os_zalloc(sizeof(*bsrv));
2706 if (bsrv == NULL)
2707 return -1;
2708 bsrv->query = query;
2709 bsrv->resp = resp;
2710 dl_list_add(&wpa_s->global->p2p_srv_bonjour, &bsrv->list);
2711
2712 wpas_p2p_sd_service_update(wpa_s);
2713 return 0;
2714}
2715
2716
2717int wpas_p2p_service_del_bonjour(struct wpa_supplicant *wpa_s,
2718 const struct wpabuf *query)
2719{
2720 struct p2p_srv_bonjour *bsrv;
2721
2722 bsrv = wpas_p2p_service_get_bonjour(wpa_s, query);
2723 if (bsrv == NULL)
2724 return -1;
2725 wpas_p2p_srv_bonjour_free(bsrv);
2726 wpas_p2p_sd_service_update(wpa_s);
2727 return 0;
2728}
2729
2730
2731int wpas_p2p_service_add_upnp(struct wpa_supplicant *wpa_s, u8 version,
2732 const char *service)
2733{
2734 struct p2p_srv_upnp *usrv;
2735
2736 if (wpas_p2p_service_get_upnp(wpa_s, version, service))
2737 return 0; /* Already listed */
2738 usrv = os_zalloc(sizeof(*usrv));
2739 if (usrv == NULL)
2740 return -1;
2741 usrv->version = version;
2742 usrv->service = os_strdup(service);
2743 if (usrv->service == NULL) {
2744 os_free(usrv);
2745 return -1;
2746 }
2747 dl_list_add(&wpa_s->global->p2p_srv_upnp, &usrv->list);
2748
2749 wpas_p2p_sd_service_update(wpa_s);
2750 return 0;
2751}
2752
2753
2754int wpas_p2p_service_del_upnp(struct wpa_supplicant *wpa_s, u8 version,
2755 const char *service)
2756{
2757 struct p2p_srv_upnp *usrv;
2758
2759 usrv = wpas_p2p_service_get_upnp(wpa_s, version, service);
2760 if (usrv == NULL)
2761 return -1;
2762 wpas_p2p_srv_upnp_free(usrv);
2763 wpas_p2p_sd_service_update(wpa_s);
2764 return 0;
2765}
2766
2767
2768static void wpas_prov_disc_local_display(struct wpa_supplicant *wpa_s,
2769 const u8 *peer, const char *params,
2770 unsigned int generated_pin)
2771{
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07002772 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_SHOW_PIN MACSTR
2773 " %08d%s", MAC2STR(peer), generated_pin, params);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002774}
2775
2776
2777static void wpas_prov_disc_local_keypad(struct wpa_supplicant *wpa_s,
2778 const u8 *peer, const char *params)
2779{
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07002780 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_ENTER_PIN MACSTR
2781 "%s", MAC2STR(peer), params);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002782}
2783
2784
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002785static void wpas_prov_disc_req(void *ctx, const u8 *peer, u16 config_methods,
2786 const u8 *dev_addr, const u8 *pri_dev_type,
2787 const char *dev_name, u16 supp_config_methods,
2788 u8 dev_capab, u8 group_capab, const u8 *group_id,
2789 size_t group_id_len)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002790{
2791 struct wpa_supplicant *wpa_s = ctx;
2792 char devtype[WPS_DEV_TYPE_BUFSIZE];
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002793 char params[300];
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002794 u8 empty_dev_type[8];
2795 unsigned int generated_pin = 0;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002796 struct wpa_supplicant *group = NULL;
2797
2798 if (group_id) {
2799 for (group = wpa_s->global->ifaces; group; group = group->next)
2800 {
2801 struct wpa_ssid *s = group->current_ssid;
2802 if (s != NULL &&
2803 s->mode == WPAS_MODE_P2P_GO &&
2804 group_id_len - ETH_ALEN == s->ssid_len &&
2805 os_memcmp(group_id + ETH_ALEN, s->ssid,
2806 s->ssid_len) == 0)
2807 break;
2808 }
2809 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002810
2811 if (pri_dev_type == NULL) {
2812 os_memset(empty_dev_type, 0, sizeof(empty_dev_type));
2813 pri_dev_type = empty_dev_type;
2814 }
2815 os_snprintf(params, sizeof(params), " p2p_dev_addr=" MACSTR
2816 " pri_dev_type=%s name='%s' config_methods=0x%x "
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002817 "dev_capab=0x%x group_capab=0x%x%s%s",
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002818 MAC2STR(dev_addr),
2819 wps_dev_type_bin2str(pri_dev_type, devtype,
2820 sizeof(devtype)),
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002821 dev_name, supp_config_methods, dev_capab, group_capab,
2822 group ? " group=" : "",
2823 group ? group->ifname : "");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002824 params[sizeof(params) - 1] = '\0';
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08002825
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002826 if (config_methods & WPS_CONFIG_DISPLAY) {
2827 generated_pin = wps_generate_pin();
2828 wpas_prov_disc_local_display(wpa_s, peer, params,
2829 generated_pin);
2830 } else if (config_methods & WPS_CONFIG_KEYPAD)
2831 wpas_prov_disc_local_keypad(wpa_s, peer, params);
2832 else if (config_methods & WPS_CONFIG_PUSHBUTTON)
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07002833 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_PBC_REQ
2834 MACSTR "%s", MAC2STR(peer), params);
Jouni Malinen75ecf522011-06-27 15:19:46 -07002835
2836 wpas_notify_p2p_provision_discovery(wpa_s, peer, 1 /* request */,
2837 P2P_PROV_DISC_SUCCESS,
2838 config_methods, generated_pin);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002839}
2840
2841
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002842static void wpas_prov_disc_resp(void *ctx, const u8 *peer, u16 config_methods)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002843{
2844 struct wpa_supplicant *wpa_s = ctx;
2845 unsigned int generated_pin = 0;
Dmitry Shmidt04949592012-07-19 12:16:46 -07002846 char params[20];
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002847
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08002848 if (wpa_s->pending_pd_before_join &&
2849 (os_memcmp(peer, wpa_s->pending_join_dev_addr, ETH_ALEN) == 0 ||
2850 os_memcmp(peer, wpa_s->pending_join_iface_addr, ETH_ALEN) == 0)) {
2851 wpa_s->pending_pd_before_join = 0;
2852 wpa_printf(MSG_DEBUG, "P2P: Starting pending "
2853 "join-existing-group operation");
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08002854 wpas_p2p_join_start(wpa_s, 0, NULL, 0);
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08002855 return;
2856 }
2857
Dmitry Shmidt04949592012-07-19 12:16:46 -07002858 if (wpa_s->pending_pd_use == AUTO_PD_JOIN ||
2859 wpa_s->pending_pd_use == AUTO_PD_GO_NEG)
2860 os_snprintf(params, sizeof(params), " peer_go=%d",
2861 wpa_s->pending_pd_use == AUTO_PD_JOIN);
2862 else
2863 params[0] = '\0';
2864
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002865 if (config_methods & WPS_CONFIG_DISPLAY)
Dmitry Shmidt04949592012-07-19 12:16:46 -07002866 wpas_prov_disc_local_keypad(wpa_s, peer, params);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002867 else if (config_methods & WPS_CONFIG_KEYPAD) {
2868 generated_pin = wps_generate_pin();
Dmitry Shmidt04949592012-07-19 12:16:46 -07002869 wpas_prov_disc_local_display(wpa_s, peer, params,
2870 generated_pin);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002871 } else if (config_methods & WPS_CONFIG_PUSHBUTTON)
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07002872 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_PBC_RESP
2873 MACSTR "%s", MAC2STR(peer), params);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002874
Jouni Malinen75ecf522011-06-27 15:19:46 -07002875 wpas_notify_p2p_provision_discovery(wpa_s, peer, 0 /* response */,
2876 P2P_PROV_DISC_SUCCESS,
2877 config_methods, generated_pin);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002878}
2879
2880
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002881static void wpas_prov_disc_fail(void *ctx, const u8 *peer,
2882 enum p2p_prov_disc_status status)
Jouni Malinen75ecf522011-06-27 15:19:46 -07002883{
2884 struct wpa_supplicant *wpa_s = ctx;
2885
Dmitry Shmidt04949592012-07-19 12:16:46 -07002886 if (wpa_s->p2p_fallback_to_go_neg) {
2887 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: PD for p2p_connect-auto "
2888 "failed - fall back to GO Negotiation");
2889 wpas_p2p_fallback_to_go_neg(wpa_s, 0);
2890 return;
2891 }
2892
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002893 if (status == P2P_PROV_DISC_TIMEOUT_JOIN) {
Dmitry Shmidt2b380482012-09-13 10:52:13 -07002894 wpa_s->pending_pd_before_join = 0;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002895 wpa_printf(MSG_DEBUG, "P2P: Starting pending "
2896 "join-existing-group operation (no ACK for PD "
2897 "Req attempts)");
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08002898 wpas_p2p_join_start(wpa_s, 0, NULL, 0);
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002899 return;
Dmitry Shmidt2b380482012-09-13 10:52:13 -07002900 }
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002901
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07002902 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_FAILURE
2903 " p2p_dev_addr=" MACSTR " status=%d",
2904 MAC2STR(peer), status);
Dmitry Shmidt04949592012-07-19 12:16:46 -07002905
Jouni Malinen75ecf522011-06-27 15:19:46 -07002906 wpas_notify_p2p_provision_discovery(wpa_s, peer, 0 /* response */,
2907 status, 0, 0);
2908}
2909
2910
Dmitry Shmidtf7e0a992013-05-23 11:03:10 -07002911static int freq_included(const struct p2p_channels *channels, unsigned int freq)
2912{
2913 if (channels == NULL)
2914 return 1; /* Assume no restrictions */
2915 return p2p_channels_includes_freq(channels, freq);
2916
2917}
2918
2919
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002920static u8 wpas_invitation_process(void *ctx, const u8 *sa, const u8 *bssid,
2921 const u8 *go_dev_addr, const u8 *ssid,
2922 size_t ssid_len, int *go, u8 *group_bssid,
Dmitry Shmidtf7e0a992013-05-23 11:03:10 -07002923 int *force_freq, int persistent_group,
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08002924 const struct p2p_channels *channels,
2925 int dev_pw_id)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002926{
2927 struct wpa_supplicant *wpa_s = ctx;
2928 struct wpa_ssid *s;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002929 int res;
2930 struct wpa_supplicant *grp;
2931
2932 if (!persistent_group) {
2933 wpa_printf(MSG_DEBUG, "P2P: Invitation from " MACSTR
Dmitry Shmidt344abd32014-01-14 13:17:00 -08002934 " to join an active group (SSID: %s)",
2935 MAC2STR(sa), wpa_ssid_txt(ssid, ssid_len));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002936 if (!is_zero_ether_addr(wpa_s->p2p_auth_invite) &&
2937 (os_memcmp(go_dev_addr, wpa_s->p2p_auth_invite, ETH_ALEN)
2938 == 0 ||
2939 os_memcmp(sa, wpa_s->p2p_auth_invite, ETH_ALEN) == 0)) {
2940 wpa_printf(MSG_DEBUG, "P2P: Accept previously "
2941 "authorized invitation");
2942 goto accept_inv;
2943 }
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08002944
2945#ifdef CONFIG_WPS_NFC
2946 if (dev_pw_id >= 0 && wpa_s->parent->p2p_nfc_tag_enabled &&
2947 dev_pw_id == wpa_s->parent->p2p_oob_dev_pw_id) {
2948 wpa_printf(MSG_DEBUG, "P2P: Accept invitation based on local enabled NFC Tag");
2949 wpa_s->parent->p2p_wps_method = WPS_NFC;
2950 wpa_s->parent->pending_join_wps_method = WPS_NFC;
2951 os_memcpy(wpa_s->parent->pending_join_dev_addr,
2952 go_dev_addr, ETH_ALEN);
2953 os_memcpy(wpa_s->parent->pending_join_iface_addr,
2954 bssid, ETH_ALEN);
2955 goto accept_inv;
2956 }
2957#endif /* CONFIG_WPS_NFC */
2958
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002959 /*
2960 * Do not accept the invitation automatically; notify user and
2961 * request approval.
2962 */
2963 return P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE;
2964 }
2965
2966 grp = wpas_get_p2p_group(wpa_s, ssid, ssid_len, go);
2967 if (grp) {
2968 wpa_printf(MSG_DEBUG, "P2P: Accept invitation to already "
2969 "running persistent group");
2970 if (*go)
2971 os_memcpy(group_bssid, grp->own_addr, ETH_ALEN);
2972 goto accept_inv;
2973 }
2974
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08002975 if (!is_zero_ether_addr(wpa_s->p2p_auth_invite) &&
2976 os_memcmp(sa, wpa_s->p2p_auth_invite, ETH_ALEN) == 0) {
2977 wpa_printf(MSG_DEBUG, "P2P: Accept previously initiated "
2978 "invitation to re-invoke a persistent group");
2979 } else if (!wpa_s->conf->persistent_reconnect)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002980 return P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE;
2981
2982 for (s = wpa_s->conf->ssid; s; s = s->next) {
2983 if (s->disabled == 2 &&
2984 os_memcmp(s->bssid, go_dev_addr, ETH_ALEN) == 0 &&
2985 s->ssid_len == ssid_len &&
2986 os_memcmp(ssid, s->ssid, ssid_len) == 0)
2987 break;
2988 }
2989
2990 if (!s) {
2991 wpa_printf(MSG_DEBUG, "P2P: Invitation from " MACSTR
2992 " requested reinvocation of an unknown group",
2993 MAC2STR(sa));
2994 return P2P_SC_FAIL_UNKNOWN_GROUP;
2995 }
2996
2997 if (s->mode == WPAS_MODE_P2P_GO && !wpas_p2p_create_iface(wpa_s)) {
2998 *go = 1;
2999 if (wpa_s->wpa_state >= WPA_AUTHENTICATING) {
3000 wpa_printf(MSG_DEBUG, "P2P: The only available "
3001 "interface is already in use - reject "
3002 "invitation");
3003 return P2P_SC_FAIL_UNABLE_TO_ACCOMMODATE;
3004 }
3005 os_memcpy(group_bssid, wpa_s->own_addr, ETH_ALEN);
3006 } else if (s->mode == WPAS_MODE_P2P_GO) {
3007 *go = 1;
3008 if (wpas_p2p_add_group_interface(wpa_s, WPA_IF_P2P_GO) < 0)
3009 {
3010 wpa_printf(MSG_ERROR, "P2P: Failed to allocate a new "
3011 "interface address for the group");
3012 return P2P_SC_FAIL_UNABLE_TO_ACCOMMODATE;
3013 }
3014 os_memcpy(group_bssid, wpa_s->pending_interface_addr,
3015 ETH_ALEN);
3016 }
3017
3018accept_inv:
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003019 wpas_p2p_set_own_freq_preference(wpa_s, 0);
3020
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07003021 /* Get one of the frequencies currently in use */
3022 if (wpas_p2p_valid_oper_freqs(wpa_s, &res, 1) > 0) {
Dmitry Shmidta0d265f2013-11-19 13:13:41 -08003023 wpa_printf(MSG_DEBUG, "P2P: Trying to prefer a channel already used by one of the interfaces");
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003024 wpas_p2p_set_own_freq_preference(wpa_s, res);
Dmitry Shmidta0d265f2013-11-19 13:13:41 -08003025
3026 if (wpa_s->num_multichan_concurrent < 2 ||
3027 wpas_p2p_num_unused_channels(wpa_s) < 1) {
3028 wpa_printf(MSG_DEBUG, "P2P: No extra channels available - trying to force channel to match a channel already used by one of the interfaces");
3029 *force_freq = res;
3030 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003031 }
3032
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07003033 if (*force_freq > 0 && wpa_s->num_multichan_concurrent > 1 &&
3034 wpas_p2p_num_unused_channels(wpa_s) > 0) {
Dmitry Shmidtf7e0a992013-05-23 11:03:10 -07003035 if (*go == 0) {
3036 /* We are the client */
3037 wpa_printf(MSG_DEBUG, "P2P: Peer was found to be "
3038 "running a GO but we are capable of MCC, "
3039 "figure out the best channel to use");
3040 *force_freq = 0;
3041 } else if (!freq_included(channels, *force_freq)) {
3042 /* We are the GO, and *force_freq is not in the
3043 * intersection */
3044 wpa_printf(MSG_DEBUG, "P2P: Forced GO freq %d MHz not "
3045 "in intersection but we are capable of MCC, "
3046 "figure out the best channel to use",
3047 *force_freq);
3048 *force_freq = 0;
3049 }
3050 }
3051
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003052 return P2P_SC_SUCCESS;
3053}
3054
3055
3056static void wpas_invitation_received(void *ctx, const u8 *sa, const u8 *bssid,
3057 const u8 *ssid, size_t ssid_len,
3058 const u8 *go_dev_addr, u8 status,
3059 int op_freq)
3060{
3061 struct wpa_supplicant *wpa_s = ctx;
3062 struct wpa_ssid *s;
3063
3064 for (s = wpa_s->conf->ssid; s; s = s->next) {
3065 if (s->disabled == 2 &&
3066 s->ssid_len == ssid_len &&
3067 os_memcmp(ssid, s->ssid, ssid_len) == 0)
3068 break;
3069 }
3070
3071 if (status == P2P_SC_SUCCESS) {
3072 wpa_printf(MSG_DEBUG, "P2P: Invitation from peer " MACSTR
Dmitry Shmidt344abd32014-01-14 13:17:00 -08003073 " was accepted; op_freq=%d MHz, SSID=%s",
3074 MAC2STR(sa), op_freq, wpa_ssid_txt(ssid, ssid_len));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003075 if (s) {
Dmitry Shmidt91c40cd2012-09-25 14:23:53 -07003076 int go = s->mode == WPAS_MODE_P2P_GO;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003077 wpas_p2p_group_add_persistent(
Dmitry Shmidt15907092014-03-25 10:42:57 -07003078 wpa_s, s, go, 0, op_freq, 0, 0, NULL,
Dmitry Shmidt56052862013-10-04 10:23:25 -07003079 go ? P2P_MAX_INITIAL_CONN_WAIT_GO_REINVOKE : 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003080 } else if (bssid) {
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08003081 wpa_s->user_initiated_pd = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003082 wpas_p2p_join(wpa_s, bssid, go_dev_addr,
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08003083 wpa_s->p2p_wps_method, 0, op_freq,
Dmitry Shmidt344abd32014-01-14 13:17:00 -08003084 ssid, ssid_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003085 }
3086 return;
3087 }
3088
3089 if (status != P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE) {
3090 wpa_printf(MSG_DEBUG, "P2P: Invitation from peer " MACSTR
3091 " was rejected (status %u)", MAC2STR(sa), status);
3092 return;
3093 }
3094
3095 if (!s) {
3096 if (bssid) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07003097 wpa_msg_global(wpa_s, MSG_INFO,
3098 P2P_EVENT_INVITATION_RECEIVED
3099 "sa=" MACSTR " go_dev_addr=" MACSTR
3100 " bssid=" MACSTR " unknown-network",
3101 MAC2STR(sa), MAC2STR(go_dev_addr),
3102 MAC2STR(bssid));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003103 } else {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07003104 wpa_msg_global(wpa_s, MSG_INFO,
3105 P2P_EVENT_INVITATION_RECEIVED
3106 "sa=" MACSTR " go_dev_addr=" MACSTR
3107 " unknown-network",
3108 MAC2STR(sa), MAC2STR(go_dev_addr));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003109 }
3110 return;
3111 }
3112
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08003113 if (s->mode == WPAS_MODE_P2P_GO && op_freq) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07003114 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_INVITATION_RECEIVED
3115 "sa=" MACSTR " persistent=%d freq=%d",
3116 MAC2STR(sa), s->id, op_freq);
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08003117 } else {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07003118 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_INVITATION_RECEIVED
3119 "sa=" MACSTR " persistent=%d",
3120 MAC2STR(sa), s->id);
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08003121 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003122}
3123
3124
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003125static void wpas_remove_persistent_peer(struct wpa_supplicant *wpa_s,
3126 struct wpa_ssid *ssid,
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07003127 const u8 *peer, int inv)
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003128{
3129 size_t i;
3130
3131 if (ssid == NULL)
3132 return;
3133
3134 for (i = 0; ssid->p2p_client_list && i < ssid->num_p2p_clients; i++) {
3135 if (os_memcmp(ssid->p2p_client_list + i * ETH_ALEN, peer,
3136 ETH_ALEN) == 0)
3137 break;
3138 }
3139 if (i >= ssid->num_p2p_clients) {
3140 if (ssid->mode != WPAS_MODE_P2P_GO &&
3141 os_memcmp(ssid->bssid, peer, ETH_ALEN) == 0) {
3142 wpa_printf(MSG_DEBUG, "P2P: Remove persistent group %d "
3143 "due to invitation result", ssid->id);
3144 wpas_notify_network_removed(wpa_s, ssid);
3145 wpa_config_remove_network(wpa_s->conf, ssid->id);
3146 return;
3147 }
3148 return; /* Peer not found in client list */
3149 }
3150
3151 wpa_printf(MSG_DEBUG, "P2P: Remove peer " MACSTR " from persistent "
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07003152 "group %d client list%s",
3153 MAC2STR(peer), ssid->id,
3154 inv ? " due to invitation result" : "");
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003155 os_memmove(ssid->p2p_client_list + i * ETH_ALEN,
3156 ssid->p2p_client_list + (i + 1) * ETH_ALEN,
3157 (ssid->num_p2p_clients - i - 1) * ETH_ALEN);
3158 ssid->num_p2p_clients--;
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003159 if (wpa_s->parent->conf->update_config &&
3160 wpa_config_write(wpa_s->parent->confname, wpa_s->parent->conf))
3161 wpa_printf(MSG_DEBUG, "P2P: Failed to update configuration");
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003162}
3163
3164
3165static void wpas_remove_persistent_client(struct wpa_supplicant *wpa_s,
3166 const u8 *peer)
3167{
3168 struct wpa_ssid *ssid;
3169
3170 wpa_s = wpa_s->global->p2p_invite_group;
3171 if (wpa_s == NULL)
3172 return; /* No known invitation group */
3173 ssid = wpa_s->current_ssid;
3174 if (ssid == NULL || ssid->mode != WPAS_MODE_P2P_GO ||
3175 !ssid->p2p_persistent_group)
3176 return; /* Not operating as a GO in persistent group */
3177 ssid = wpas_p2p_get_persistent(wpa_s->parent, peer,
3178 ssid->ssid, ssid->ssid_len);
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07003179 wpas_remove_persistent_peer(wpa_s, ssid, peer, 1);
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003180}
3181
3182
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08003183static void wpas_invitation_result(void *ctx, int status, const u8 *bssid,
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003184 const struct p2p_channels *channels,
Dmitry Shmidt15907092014-03-25 10:42:57 -07003185 const u8 *peer, int neg_freq,
3186 int peer_oper_freq)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003187{
3188 struct wpa_supplicant *wpa_s = ctx;
3189 struct wpa_ssid *ssid;
Dmitry Shmidt15907092014-03-25 10:42:57 -07003190 int freq;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003191
3192 if (bssid) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07003193 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_INVITATION_RESULT
3194 "status=%d " MACSTR,
3195 status, MAC2STR(bssid));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003196 } else {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07003197 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_INVITATION_RESULT
3198 "status=%d ", status);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003199 }
3200 wpas_notify_p2p_invitation_result(wpa_s, status, bssid);
3201
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003202 wpa_printf(MSG_DEBUG, "P2P: Invitation result - status=%d peer=" MACSTR,
3203 status, MAC2STR(peer));
3204 if (wpa_s->pending_invite_ssid_id == -1) {
3205 if (status == P2P_SC_FAIL_UNKNOWN_GROUP)
3206 wpas_remove_persistent_client(wpa_s, peer);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003207 return; /* Invitation to active group */
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003208 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003209
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08003210 if (status == P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE) {
3211 wpa_printf(MSG_DEBUG, "P2P: Waiting for peer to start another "
3212 "invitation exchange to indicate readiness for "
3213 "re-invocation");
3214 }
3215
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003216 if (status != P2P_SC_SUCCESS) {
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003217 if (status == P2P_SC_FAIL_UNKNOWN_GROUP) {
3218 ssid = wpa_config_get_network(
3219 wpa_s->conf, wpa_s->pending_invite_ssid_id);
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07003220 wpas_remove_persistent_peer(wpa_s, ssid, peer, 1);
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003221 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003222 wpas_p2p_remove_pending_group_interface(wpa_s);
3223 return;
3224 }
3225
3226 ssid = wpa_config_get_network(wpa_s->conf,
3227 wpa_s->pending_invite_ssid_id);
3228 if (ssid == NULL) {
3229 wpa_printf(MSG_ERROR, "P2P: Could not find persistent group "
3230 "data matching with invitation");
3231 return;
3232 }
3233
Jouni Malinen2c5b17d2012-10-13 21:52:46 -07003234 /*
3235 * The peer could have missed our ctrl::ack frame for Invitation
3236 * Response and continue retransmitting the frame. To reduce the
3237 * likelihood of the peer not getting successful TX status for the
3238 * Invitation Response frame, wait a short time here before starting
3239 * the persistent group so that we will remain on the current channel to
3240 * acknowledge any possible retransmission from the peer.
3241 */
3242 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: 50 ms wait on current channel before "
3243 "starting persistent group");
3244 os_sleep(0, 50000);
3245
Dmitry Shmidt15907092014-03-25 10:42:57 -07003246 if (neg_freq > 0 && ssid->mode == WPAS_MODE_P2P_GO &&
3247 freq_included(channels, neg_freq))
3248 freq = neg_freq;
3249 else if (peer_oper_freq > 0 && ssid->mode != WPAS_MODE_P2P_GO &&
3250 freq_included(channels, peer_oper_freq))
3251 freq = peer_oper_freq;
3252 else
3253 freq = 0;
3254
3255 wpa_printf(MSG_DEBUG, "P2P: Persistent group invitation success - op_freq=%d MHz SSID=%s",
3256 freq, wpa_ssid_txt(ssid->ssid, ssid->ssid_len));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003257 wpas_p2p_group_add_persistent(wpa_s, ssid,
Jouni Malinen31be0a42012-08-31 21:20:51 +03003258 ssid->mode == WPAS_MODE_P2P_GO,
Dmitry Shmidt96be6222014-02-13 10:16:51 -08003259 wpa_s->p2p_persistent_go_freq,
Dmitry Shmidt15907092014-03-25 10:42:57 -07003260 freq,
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003261 wpa_s->p2p_go_ht40, wpa_s->p2p_go_vht,
3262 channels,
Dmitry Shmidt56052862013-10-04 10:23:25 -07003263 ssid->mode == WPAS_MODE_P2P_GO ?
3264 P2P_MAX_INITIAL_CONN_WAIT_GO_REINVOKE :
3265 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003266}
3267
3268
Dmitry Shmidt04949592012-07-19 12:16:46 -07003269static int wpas_p2p_disallowed_freq(struct wpa_global *global,
3270 unsigned int freq)
3271{
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08003272 if (freq_range_list_includes(&global->p2p_go_avoid_freq, freq))
3273 return 1;
Dmitry Shmidt4ce9c872013-10-24 11:08:13 -07003274 return freq_range_list_includes(&global->p2p_disallow_freq, freq);
Dmitry Shmidt04949592012-07-19 12:16:46 -07003275}
3276
3277
3278static void wpas_p2p_add_chan(struct p2p_reg_class *reg, u8 chan)
3279{
3280 reg->channel[reg->channels] = chan;
3281 reg->channels++;
3282}
3283
3284
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003285static int wpas_p2p_default_channels(struct wpa_supplicant *wpa_s,
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003286 struct p2p_channels *chan,
3287 struct p2p_channels *cli_chan)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003288{
3289 int i, cla = 0;
3290
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003291 os_memset(cli_chan, 0, sizeof(*cli_chan));
3292
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003293 wpa_printf(MSG_DEBUG, "P2P: Enable operating classes for 2.4 GHz "
3294 "band");
3295
3296 /* Operating class 81 - 2.4 GHz band channels 1..13 */
3297 chan->reg_class[cla].reg_class = 81;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003298 chan->reg_class[cla].channels = 0;
3299 for (i = 0; i < 11; i++) {
3300 if (!wpas_p2p_disallowed_freq(wpa_s->global, 2412 + i * 5))
3301 wpas_p2p_add_chan(&chan->reg_class[cla], i + 1);
3302 }
3303 if (chan->reg_class[cla].channels)
3304 cla++;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003305
3306 wpa_printf(MSG_DEBUG, "P2P: Enable operating classes for lower 5 GHz "
3307 "band");
3308
3309 /* Operating class 115 - 5 GHz, channels 36-48 */
3310 chan->reg_class[cla].reg_class = 115;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003311 chan->reg_class[cla].channels = 0;
3312 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 36 * 5))
3313 wpas_p2p_add_chan(&chan->reg_class[cla], 36);
3314 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 40 * 5))
3315 wpas_p2p_add_chan(&chan->reg_class[cla], 40);
3316 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 44 * 5))
3317 wpas_p2p_add_chan(&chan->reg_class[cla], 44);
3318 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 48 * 5))
3319 wpas_p2p_add_chan(&chan->reg_class[cla], 48);
3320 if (chan->reg_class[cla].channels)
3321 cla++;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003322
3323 wpa_printf(MSG_DEBUG, "P2P: Enable operating classes for higher 5 GHz "
3324 "band");
3325
3326 /* Operating class 124 - 5 GHz, channels 149,153,157,161 */
3327 chan->reg_class[cla].reg_class = 124;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003328 chan->reg_class[cla].channels = 0;
3329 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 149 * 5))
3330 wpas_p2p_add_chan(&chan->reg_class[cla], 149);
3331 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 153 * 5))
3332 wpas_p2p_add_chan(&chan->reg_class[cla], 153);
3333 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 156 * 5))
3334 wpas_p2p_add_chan(&chan->reg_class[cla], 157);
3335 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 161 * 5))
3336 wpas_p2p_add_chan(&chan->reg_class[cla], 161);
3337 if (chan->reg_class[cla].channels)
3338 cla++;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003339
3340 chan->reg_classes = cla;
3341 return 0;
3342}
3343
3344
3345static struct hostapd_hw_modes * get_mode(struct hostapd_hw_modes *modes,
3346 u16 num_modes,
3347 enum hostapd_hw_mode mode)
3348{
3349 u16 i;
3350
3351 for (i = 0; i < num_modes; i++) {
3352 if (modes[i].mode == mode)
3353 return &modes[i];
3354 }
3355
3356 return NULL;
3357}
3358
3359
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003360enum chan_allowed {
3361 NOT_ALLOWED, PASSIVE_ONLY, ALLOWED
3362};
3363
Dmitry Shmidt04949592012-07-19 12:16:46 -07003364static int has_channel(struct wpa_global *global,
3365 struct hostapd_hw_modes *mode, u8 chan, int *flags)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003366{
3367 int i;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003368 unsigned int freq;
3369
3370 freq = (mode->mode == HOSTAPD_MODE_IEEE80211A ? 5000 : 2407) +
3371 chan * 5;
3372 if (wpas_p2p_disallowed_freq(global, freq))
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003373 return NOT_ALLOWED;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003374
3375 for (i = 0; i < mode->num_channels; i++) {
3376 if (mode->channels[i].chan == chan) {
3377 if (flags)
3378 *flags = mode->channels[i].flag;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003379 if (mode->channels[i].flag &
3380 (HOSTAPD_CHAN_DISABLED |
3381 HOSTAPD_CHAN_RADAR))
3382 return NOT_ALLOWED;
3383 if (mode->channels[i].flag &
3384 (HOSTAPD_CHAN_PASSIVE_SCAN |
3385 HOSTAPD_CHAN_NO_IBSS))
3386 return PASSIVE_ONLY;
3387 return ALLOWED;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003388 }
3389 }
3390
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003391 return NOT_ALLOWED;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003392}
3393
3394
3395struct p2p_oper_class_map {
3396 enum hostapd_hw_mode mode;
3397 u8 op_class;
3398 u8 min_chan;
3399 u8 max_chan;
3400 u8 inc;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003401 enum { BW20, BW40PLUS, BW40MINUS, BW80 } bw;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003402};
3403
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003404static struct p2p_oper_class_map op_class[] = {
3405 { HOSTAPD_MODE_IEEE80211G, 81, 1, 13, 1, BW20 },
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003406#if 0 /* Do not enable HT40 on 2 GHz for now */
3407 { HOSTAPD_MODE_IEEE80211G, 83, 1, 9, 1, BW40PLUS },
3408 { HOSTAPD_MODE_IEEE80211G, 84, 5, 13, 1, BW40MINUS },
3409#endif
3410 { HOSTAPD_MODE_IEEE80211A, 115, 36, 48, 4, BW20 },
3411 { HOSTAPD_MODE_IEEE80211A, 124, 149, 161, 4, BW20 },
3412 { HOSTAPD_MODE_IEEE80211A, 116, 36, 44, 8, BW40PLUS },
3413 { HOSTAPD_MODE_IEEE80211A, 117, 40, 48, 8, BW40MINUS },
3414 { HOSTAPD_MODE_IEEE80211A, 126, 149, 157, 8, BW40PLUS },
3415 { HOSTAPD_MODE_IEEE80211A, 127, 153, 161, 8, BW40MINUS },
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003416
3417 /*
3418 * IEEE P802.11ac/D7.0 Table E-4 actually talks about channel center
3419 * frequency index 42, 58, 106, 122, 138, 155 with channel spacing of
3420 * 80 MHz, but currently use the following definition for simplicity
3421 * (these center frequencies are not actual channels, which makes
3422 * has_channel() fail). wpas_p2p_verify_80mhz() should take care of
3423 * removing invalid channels.
3424 */
3425 { HOSTAPD_MODE_IEEE80211A, 128, 36, 161, 4, BW80 },
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003426 { -1, 0, 0, 0, 0, BW20 }
3427};
3428
3429
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003430static int wpas_p2p_get_center_80mhz(struct wpa_supplicant *wpa_s,
3431 struct hostapd_hw_modes *mode,
3432 u8 channel)
3433{
3434 u8 center_channels[] = { 42, 58, 106, 122, 138, 155 };
3435 unsigned int i;
3436
3437 if (mode->mode != HOSTAPD_MODE_IEEE80211A)
3438 return 0;
3439
3440 for (i = 0; i < ARRAY_SIZE(center_channels); i++)
3441 /*
3442 * In 80 MHz, the bandwidth "spans" 12 channels (e.g., 36-48),
3443 * so the center channel is 6 channels away from the start/end.
3444 */
3445 if (channel >= center_channels[i] - 6 &&
3446 channel <= center_channels[i] + 6)
3447 return center_channels[i];
3448
3449 return 0;
3450}
3451
3452
3453static enum chan_allowed wpas_p2p_verify_80mhz(struct wpa_supplicant *wpa_s,
3454 struct hostapd_hw_modes *mode,
3455 u8 channel, u8 bw)
3456{
3457 u8 center_chan;
3458 int i, flags;
3459 enum chan_allowed res, ret = ALLOWED;
3460
3461 center_chan = wpas_p2p_get_center_80mhz(wpa_s, mode, channel);
3462 if (!center_chan)
3463 return NOT_ALLOWED;
3464 if (center_chan >= 58 && center_chan <= 138)
3465 return NOT_ALLOWED; /* Do not allow DFS channels for P2P */
3466
3467 /* check all the channels are available */
3468 for (i = 0; i < 4; i++) {
3469 int adj_chan = center_chan - 6 + i * 4;
3470
3471 res = has_channel(wpa_s->global, mode, adj_chan, &flags);
3472 if (res == NOT_ALLOWED)
3473 return NOT_ALLOWED;
3474 if (res == PASSIVE_ONLY)
3475 ret = PASSIVE_ONLY;
3476
3477 if (i == 0 && !(flags & HOSTAPD_CHAN_VHT_10_70))
3478 return NOT_ALLOWED;
3479 if (i == 1 && !(flags & HOSTAPD_CHAN_VHT_30_50))
3480 return NOT_ALLOWED;
3481 if (i == 2 && !(flags & HOSTAPD_CHAN_VHT_50_30))
3482 return NOT_ALLOWED;
3483 if (i == 3 && !(flags & HOSTAPD_CHAN_VHT_70_10))
3484 return NOT_ALLOWED;
3485 }
3486
3487 return ret;
3488}
3489
3490
3491static enum chan_allowed wpas_p2p_verify_channel(struct wpa_supplicant *wpa_s,
3492 struct hostapd_hw_modes *mode,
3493 u8 channel, u8 bw)
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003494{
Dmitry Shmidt96be6222014-02-13 10:16:51 -08003495 int flag = 0;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003496 enum chan_allowed res, res2;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003497
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003498 res2 = res = has_channel(wpa_s->global, mode, channel, &flag);
3499 if (bw == BW40MINUS) {
3500 if (!(flag & HOSTAPD_CHAN_HT40MINUS))
3501 return NOT_ALLOWED;
3502 res2 = has_channel(wpa_s->global, mode, channel - 4, NULL);
3503 } else if (bw == BW40PLUS) {
3504 if (!(flag & HOSTAPD_CHAN_HT40PLUS))
3505 return NOT_ALLOWED;
3506 res2 = has_channel(wpa_s->global, mode, channel + 4, NULL);
3507 } else if (bw == BW80) {
3508 res2 = wpas_p2p_verify_80mhz(wpa_s, mode, channel, bw);
3509 }
3510
3511 if (res == NOT_ALLOWED || res2 == NOT_ALLOWED)
3512 return NOT_ALLOWED;
3513 if (res == PASSIVE_ONLY || res2 == PASSIVE_ONLY)
3514 return PASSIVE_ONLY;
3515 return res;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003516}
3517
3518
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003519static int wpas_p2p_setup_channels(struct wpa_supplicant *wpa_s,
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003520 struct p2p_channels *chan,
3521 struct p2p_channels *cli_chan)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003522{
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003523 struct hostapd_hw_modes *mode;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003524 int cla, op, cli_cla;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003525
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003526 if (wpa_s->hw.modes == NULL) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003527 wpa_printf(MSG_DEBUG, "P2P: Driver did not support fetching "
3528 "of all supported channels; assume dualband "
3529 "support");
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003530 return wpas_p2p_default_channels(wpa_s, chan, cli_chan);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003531 }
3532
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003533 cla = cli_cla = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003534
3535 for (op = 0; op_class[op].op_class; op++) {
3536 struct p2p_oper_class_map *o = &op_class[op];
3537 u8 ch;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003538 struct p2p_reg_class *reg = NULL, *cli_reg = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003539
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003540 mode = get_mode(wpa_s->hw.modes, wpa_s->hw.num_modes, o->mode);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003541 if (mode == NULL)
3542 continue;
3543 for (ch = o->min_chan; ch <= o->max_chan; ch += o->inc) {
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003544 enum chan_allowed res;
3545 res = wpas_p2p_verify_channel(wpa_s, mode, ch, o->bw);
3546 if (res == ALLOWED) {
3547 if (reg == NULL) {
3548 wpa_printf(MSG_DEBUG, "P2P: Add operating class %u",
3549 o->op_class);
3550 reg = &chan->reg_class[cla];
3551 cla++;
3552 reg->reg_class = o->op_class;
3553 }
3554 reg->channel[reg->channels] = ch;
3555 reg->channels++;
3556 } else if (res == PASSIVE_ONLY &&
3557 wpa_s->conf->p2p_add_cli_chan) {
3558 if (cli_reg == NULL) {
3559 wpa_printf(MSG_DEBUG, "P2P: Add operating class %u (client only)",
3560 o->op_class);
3561 cli_reg = &cli_chan->reg_class[cli_cla];
3562 cli_cla++;
3563 cli_reg->reg_class = o->op_class;
3564 }
3565 cli_reg->channel[cli_reg->channels] = ch;
3566 cli_reg->channels++;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003567 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003568 }
3569 if (reg) {
3570 wpa_hexdump(MSG_DEBUG, "P2P: Channels",
3571 reg->channel, reg->channels);
3572 }
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003573 if (cli_reg) {
3574 wpa_hexdump(MSG_DEBUG, "P2P: Channels (client only)",
3575 cli_reg->channel, cli_reg->channels);
3576 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003577 }
3578
3579 chan->reg_classes = cla;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003580 cli_chan->reg_classes = cli_cla;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003581
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003582 return 0;
3583}
3584
3585
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003586int wpas_p2p_get_ht40_mode(struct wpa_supplicant *wpa_s,
3587 struct hostapd_hw_modes *mode, u8 channel)
3588{
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003589 int op;
3590 enum chan_allowed ret;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003591
3592 for (op = 0; op_class[op].op_class; op++) {
3593 struct p2p_oper_class_map *o = &op_class[op];
3594 u8 ch;
3595
3596 for (ch = o->min_chan; ch <= o->max_chan; ch += o->inc) {
3597 if (o->mode != HOSTAPD_MODE_IEEE80211A ||
3598 o->bw == BW20 || ch != channel)
3599 continue;
3600 ret = wpas_p2p_verify_channel(wpa_s, mode, ch, o->bw);
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003601 if (ret == ALLOWED)
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003602 return (o->bw == BW40MINUS) ? -1 : 1;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003603 }
3604 }
3605 return 0;
3606}
3607
3608
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003609int wpas_p2p_get_vht80_center(struct wpa_supplicant *wpa_s,
3610 struct hostapd_hw_modes *mode, u8 channel)
3611{
3612 if (!wpas_p2p_verify_channel(wpa_s, mode, channel, BW80))
3613 return 0;
3614
3615 return wpas_p2p_get_center_80mhz(wpa_s, mode, channel);
3616}
3617
3618
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003619static int wpas_get_noa(void *ctx, const u8 *interface_addr, u8 *buf,
3620 size_t buf_len)
3621{
3622 struct wpa_supplicant *wpa_s = ctx;
3623
3624 for (wpa_s = wpa_s->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
3625 if (os_memcmp(wpa_s->own_addr, interface_addr, ETH_ALEN) == 0)
3626 break;
3627 }
3628 if (wpa_s == NULL)
3629 return -1;
3630
3631 return wpa_drv_get_noa(wpa_s, buf, buf_len);
3632}
3633
3634
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08003635static int wpas_go_connected(void *ctx, const u8 *dev_addr)
3636{
3637 struct wpa_supplicant *wpa_s = ctx;
3638
3639 for (wpa_s = wpa_s->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
3640 struct wpa_ssid *ssid = wpa_s->current_ssid;
3641 if (ssid == NULL)
3642 continue;
3643 if (ssid->mode != WPAS_MODE_INFRA)
3644 continue;
3645 if (wpa_s->wpa_state != WPA_COMPLETED &&
3646 wpa_s->wpa_state != WPA_GROUP_HANDSHAKE)
3647 continue;
3648 if (os_memcmp(wpa_s->go_dev_addr, dev_addr, ETH_ALEN) == 0)
3649 return 1;
3650 }
3651
3652 return 0;
3653}
3654
3655
Dmitry Shmidt18463232014-01-24 12:29:41 -08003656static int wpas_is_concurrent_session_active(void *ctx)
3657{
3658 struct wpa_supplicant *wpa_s = ctx;
3659 struct wpa_supplicant *ifs;
3660
3661 for (ifs = wpa_s->global->ifaces; ifs; ifs = ifs->next) {
3662 if (ifs == wpa_s)
3663 continue;
3664 if (ifs->wpa_state > WPA_ASSOCIATED)
3665 return 1;
3666 }
3667 return 0;
3668}
3669
3670
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07003671static void wpas_p2p_debug_print(void *ctx, int level, const char *msg)
3672{
3673 struct wpa_supplicant *wpa_s = ctx;
3674 wpa_msg_global(wpa_s, level, "P2P: %s", msg);
3675}
3676
3677
Dmitry Shmidt34af3062013-07-11 10:46:32 -07003678int wpas_p2p_add_p2pdev_interface(struct wpa_supplicant *wpa_s)
3679{
3680 struct wpa_interface iface;
3681 struct wpa_supplicant *p2pdev_wpa_s;
3682 char ifname[100];
3683 char force_name[100];
3684 int ret;
3685
3686 os_snprintf(ifname, sizeof(ifname), P2P_MGMT_DEVICE_PREFIX "%s",
3687 wpa_s->ifname);
3688 force_name[0] = '\0';
3689 wpa_s->pending_interface_type = WPA_IF_P2P_DEVICE;
3690 ret = wpa_drv_if_add(wpa_s, WPA_IF_P2P_DEVICE, ifname, NULL, NULL,
3691 force_name, wpa_s->pending_interface_addr, NULL);
3692 if (ret < 0) {
3693 wpa_printf(MSG_DEBUG, "P2P: Failed to create P2P Device interface");
3694 return ret;
3695 }
3696 os_strlcpy(wpa_s->pending_interface_name, ifname,
3697 sizeof(wpa_s->pending_interface_name));
3698
3699 os_memset(&iface, 0, sizeof(iface));
3700 iface.p2p_mgmt = 1;
3701 iface.ifname = wpa_s->pending_interface_name;
3702 iface.driver = wpa_s->driver->name;
3703 iface.driver_param = wpa_s->conf->driver_param;
Dmitry Shmidt2ac5f602014-03-07 10:08:21 -08003704
3705 /*
3706 * If a P2P Device configuration file was given, use it as the interface
3707 * configuration file (instead of using parent's configuration file.
3708 */
3709 if (wpa_s->conf_p2p_dev) {
3710 iface.confname = wpa_s->conf_p2p_dev;
3711 iface.ctrl_interface = NULL;
3712 } else {
3713 iface.confname = wpa_s->confname;
3714 iface.ctrl_interface = wpa_s->conf->ctrl_interface;
3715 }
3716 iface.conf_p2p_dev = NULL;
3717
Dmitry Shmidt34af3062013-07-11 10:46:32 -07003718 p2pdev_wpa_s = wpa_supplicant_add_iface(wpa_s->global, &iface);
3719 if (!p2pdev_wpa_s) {
3720 wpa_printf(MSG_DEBUG, "P2P: Failed to add P2P Device interface");
3721 return -1;
3722 }
3723 p2pdev_wpa_s->parent = wpa_s;
3724
3725 wpa_s->pending_interface_name[0] = '\0';
3726 return 0;
3727}
3728
3729
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08003730static void wpas_presence_resp(void *ctx, const u8 *src, u8 status,
3731 const u8 *noa, size_t noa_len)
3732{
3733 struct wpa_supplicant *wpa_s, *intf = ctx;
3734 char hex[100];
3735
3736 for (wpa_s = intf->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
3737 if (wpa_s->waiting_presence_resp)
3738 break;
3739 }
3740 if (!wpa_s) {
3741 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: No group interface was waiting for presence response");
3742 return;
3743 }
3744 wpa_s->waiting_presence_resp = 0;
3745
3746 wpa_snprintf_hex(hex, sizeof(hex), noa, noa_len);
3747 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_PRESENCE_RESPONSE "src=" MACSTR
3748 " status=%u noa=%s", MAC2STR(src), status, hex);
3749}
3750
3751
Dmitry Shmidt5a1480c2014-05-12 09:46:02 -07003752static int _wpas_p2p_in_progress(void *ctx)
3753{
3754 struct wpa_supplicant *wpa_s = ctx;
3755 return wpas_p2p_in_progress(wpa_s);
3756}
3757
3758
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003759/**
3760 * wpas_p2p_init - Initialize P2P module for %wpa_supplicant
3761 * @global: Pointer to global data from wpa_supplicant_init()
3762 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
3763 * Returns: 0 on success, -1 on failure
3764 */
3765int wpas_p2p_init(struct wpa_global *global, struct wpa_supplicant *wpa_s)
3766{
3767 struct p2p_config p2p;
3768 unsigned int r;
3769 int i;
3770
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07003771 if (wpa_s->conf->p2p_disabled)
3772 return 0;
3773
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003774 if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_CAPABLE))
3775 return 0;
3776
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003777 if (global->p2p)
3778 return 0;
3779
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003780 os_memset(&p2p, 0, sizeof(p2p));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003781 p2p.cb_ctx = wpa_s;
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07003782 p2p.debug_print = wpas_p2p_debug_print;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003783 p2p.p2p_scan = wpas_p2p_scan;
3784 p2p.send_action = wpas_send_action;
3785 p2p.send_action_done = wpas_send_action_done;
3786 p2p.go_neg_completed = wpas_go_neg_completed;
3787 p2p.go_neg_req_rx = wpas_go_neg_req_rx;
3788 p2p.dev_found = wpas_dev_found;
3789 p2p.dev_lost = wpas_dev_lost;
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07003790 p2p.find_stopped = wpas_find_stopped;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003791 p2p.start_listen = wpas_start_listen;
3792 p2p.stop_listen = wpas_stop_listen;
3793 p2p.send_probe_resp = wpas_send_probe_resp;
3794 p2p.sd_request = wpas_sd_request;
3795 p2p.sd_response = wpas_sd_response;
3796 p2p.prov_disc_req = wpas_prov_disc_req;
3797 p2p.prov_disc_resp = wpas_prov_disc_resp;
Jouni Malinen75ecf522011-06-27 15:19:46 -07003798 p2p.prov_disc_fail = wpas_prov_disc_fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003799 p2p.invitation_process = wpas_invitation_process;
3800 p2p.invitation_received = wpas_invitation_received;
3801 p2p.invitation_result = wpas_invitation_result;
3802 p2p.get_noa = wpas_get_noa;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08003803 p2p.go_connected = wpas_go_connected;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08003804 p2p.presence_resp = wpas_presence_resp;
Dmitry Shmidt18463232014-01-24 12:29:41 -08003805 p2p.is_concurrent_session_active = wpas_is_concurrent_session_active;
Dmitry Shmidt5a1480c2014-05-12 09:46:02 -07003806 p2p.is_p2p_in_progress = _wpas_p2p_in_progress;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003807
3808 os_memcpy(wpa_s->global->p2p_dev_addr, wpa_s->own_addr, ETH_ALEN);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003809 os_memcpy(p2p.dev_addr, wpa_s->global->p2p_dev_addr, ETH_ALEN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003810 p2p.dev_name = wpa_s->conf->device_name;
3811 p2p.manufacturer = wpa_s->conf->manufacturer;
3812 p2p.model_name = wpa_s->conf->model_name;
3813 p2p.model_number = wpa_s->conf->model_number;
3814 p2p.serial_number = wpa_s->conf->serial_number;
3815 if (wpa_s->wps) {
3816 os_memcpy(p2p.uuid, wpa_s->wps->uuid, 16);
3817 p2p.config_methods = wpa_s->wps->config_methods;
3818 }
3819
3820 if (wpa_s->conf->p2p_listen_reg_class &&
3821 wpa_s->conf->p2p_listen_channel) {
3822 p2p.reg_class = wpa_s->conf->p2p_listen_reg_class;
3823 p2p.channel = wpa_s->conf->p2p_listen_channel;
3824 } else {
3825 p2p.reg_class = 81;
3826 /*
3827 * Pick one of the social channels randomly as the listen
3828 * channel.
3829 */
3830 os_get_random((u8 *) &r, sizeof(r));
3831 p2p.channel = 1 + (r % 3) * 5;
3832 }
3833 wpa_printf(MSG_DEBUG, "P2P: Own listen channel: %d", p2p.channel);
3834
3835 if (wpa_s->conf->p2p_oper_reg_class &&
3836 wpa_s->conf->p2p_oper_channel) {
3837 p2p.op_reg_class = wpa_s->conf->p2p_oper_reg_class;
3838 p2p.op_channel = wpa_s->conf->p2p_oper_channel;
3839 p2p.cfg_op_channel = 1;
3840 wpa_printf(MSG_DEBUG, "P2P: Configured operating channel: "
3841 "%d:%d", p2p.op_reg_class, p2p.op_channel);
3842
3843 } else {
3844 p2p.op_reg_class = 81;
3845 /*
3846 * Use random operation channel from (1, 6, 11) if no other
3847 * preference is indicated.
3848 */
3849 os_get_random((u8 *) &r, sizeof(r));
3850 p2p.op_channel = 1 + (r % 3) * 5;
3851 p2p.cfg_op_channel = 0;
3852 wpa_printf(MSG_DEBUG, "P2P: Random operating channel: "
3853 "%d:%d", p2p.op_reg_class, p2p.op_channel);
3854 }
Dmitry Shmidt44c95782013-05-17 09:51:35 -07003855
3856 if (wpa_s->conf->p2p_pref_chan && wpa_s->conf->num_p2p_pref_chan) {
3857 p2p.pref_chan = wpa_s->conf->p2p_pref_chan;
3858 p2p.num_pref_chan = wpa_s->conf->num_p2p_pref_chan;
3859 }
3860
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003861 if (wpa_s->conf->country[0] && wpa_s->conf->country[1]) {
3862 os_memcpy(p2p.country, wpa_s->conf->country, 2);
3863 p2p.country[2] = 0x04;
3864 } else
3865 os_memcpy(p2p.country, "XX\x04", 3);
3866
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003867 if (wpas_p2p_setup_channels(wpa_s, &p2p.channels, &p2p.cli_channels)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003868 wpa_printf(MSG_ERROR, "P2P: Failed to configure supported "
3869 "channel list");
3870 return -1;
3871 }
3872
3873 os_memcpy(p2p.pri_dev_type, wpa_s->conf->device_type,
3874 WPS_DEV_TYPE_LEN);
3875
3876 p2p.num_sec_dev_types = wpa_s->conf->num_sec_device_types;
3877 os_memcpy(p2p.sec_dev_type, wpa_s->conf->sec_device_type,
3878 p2p.num_sec_dev_types * WPS_DEV_TYPE_LEN);
3879
3880 p2p.concurrent_operations = !!(wpa_s->drv_flags &
3881 WPA_DRIVER_FLAGS_P2P_CONCURRENT);
3882
3883 p2p.max_peers = 100;
3884
3885 if (wpa_s->conf->p2p_ssid_postfix) {
3886 p2p.ssid_postfix_len =
3887 os_strlen(wpa_s->conf->p2p_ssid_postfix);
3888 if (p2p.ssid_postfix_len > sizeof(p2p.ssid_postfix))
3889 p2p.ssid_postfix_len = sizeof(p2p.ssid_postfix);
3890 os_memcpy(p2p.ssid_postfix, wpa_s->conf->p2p_ssid_postfix,
3891 p2p.ssid_postfix_len);
3892 }
3893
3894 p2p.p2p_intra_bss = wpa_s->conf->p2p_intra_bss;
3895
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08003896 p2p.max_listen = wpa_s->max_remain_on_chan;
3897
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003898 global->p2p = p2p_init(&p2p);
3899 if (global->p2p == NULL)
3900 return -1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003901 global->p2p_init_wpa_s = wpa_s;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003902
3903 for (i = 0; i < MAX_WPS_VENDOR_EXT; i++) {
3904 if (wpa_s->conf->wps_vendor_ext[i] == NULL)
3905 continue;
3906 p2p_add_wps_vendor_extension(
3907 global->p2p, wpa_s->conf->wps_vendor_ext[i]);
3908 }
3909
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003910 p2p_set_no_go_freq(global->p2p, &wpa_s->conf->p2p_no_go_freq);
3911
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003912 return 0;
3913}
3914
3915
3916/**
3917 * wpas_p2p_deinit - Deinitialize per-interface P2P data
3918 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
3919 *
3920 * This function deinitialize per-interface P2P data.
3921 */
3922void wpas_p2p_deinit(struct wpa_supplicant *wpa_s)
3923{
3924 if (wpa_s->driver && wpa_s->drv_priv)
3925 wpa_drv_probe_req_report(wpa_s, 0);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003926
3927 if (wpa_s->go_params) {
3928 /* Clear any stored provisioning info */
3929 p2p_clear_provisioning_info(
3930 wpa_s->global->p2p,
Dmitry Shmidt04949592012-07-19 12:16:46 -07003931 wpa_s->go_params->peer_device_addr);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003932 }
3933
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003934 os_free(wpa_s->go_params);
3935 wpa_s->go_params = NULL;
Dmitry Shmidt684785c2014-05-12 13:34:29 -07003936 eloop_cancel_timeout(wpas_p2p_psk_failure_removal, wpa_s, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003937 eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s, NULL);
3938 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
3939 wpa_s->p2p_long_listen = 0;
3940 eloop_cancel_timeout(wpas_p2p_long_listen_timeout, wpa_s, NULL);
3941 eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL);
3942 wpas_p2p_remove_pending_group_interface(wpa_s);
Dmitry Shmidt04f534e2013-12-09 15:50:16 -08003943 eloop_cancel_timeout(wpas_p2p_group_freq_conflict, wpa_s, NULL);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08003944 wpas_p2p_listen_work_done(wpa_s);
3945 if (wpa_s->p2p_send_action_work) {
3946 os_free(wpa_s->p2p_send_action_work->ctx);
3947 radio_work_done(wpa_s->p2p_send_action_work);
3948 wpa_s->p2p_send_action_work = NULL;
3949 }
3950 eloop_cancel_timeout(wpas_p2p_send_action_work_timeout, wpa_s, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003951
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08003952 wpabuf_free(wpa_s->p2p_oob_dev_pw);
3953 wpa_s->p2p_oob_dev_pw = NULL;
3954
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003955 /* TODO: remove group interface from the driver if this wpa_s instance
3956 * is on top of a P2P group interface */
3957}
3958
3959
3960/**
3961 * wpas_p2p_deinit_global - Deinitialize global P2P module
3962 * @global: Pointer to global data from wpa_supplicant_init()
3963 *
3964 * This function deinitializes the global (per device) P2P module.
3965 */
3966void wpas_p2p_deinit_global(struct wpa_global *global)
3967{
3968 struct wpa_supplicant *wpa_s, *tmp;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003969
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003970 wpa_s = global->ifaces;
3971 if (wpa_s)
3972 wpas_p2p_service_flush(wpa_s);
3973
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003974 if (global->p2p == NULL)
3975 return;
3976
3977 /* Remove remaining P2P group interfaces */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003978 while (wpa_s && wpa_s->p2p_group_interface != NOT_P2P_GROUP_INTERFACE)
3979 wpa_s = wpa_s->next;
3980 while (wpa_s) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003981 tmp = global->ifaces;
3982 while (tmp &&
3983 (tmp == wpa_s ||
3984 tmp->p2p_group_interface == NOT_P2P_GROUP_INTERFACE)) {
3985 tmp = tmp->next;
3986 }
3987 if (tmp == NULL)
3988 break;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003989 /* Disconnect from the P2P group and deinit the interface */
3990 wpas_p2p_disconnect(tmp);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003991 }
3992
3993 /*
3994 * Deinit GO data on any possibly remaining interface (if main
3995 * interface is used as GO).
3996 */
3997 for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
3998 if (wpa_s->ap_iface)
3999 wpas_p2p_group_deinit(wpa_s);
4000 }
4001
4002 p2p_deinit(global->p2p);
4003 global->p2p = NULL;
Dmitry Shmidt04949592012-07-19 12:16:46 -07004004 global->p2p_init_wpa_s = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004005}
4006
4007
4008static int wpas_p2p_create_iface(struct wpa_supplicant *wpa_s)
4009{
Dmitry Shmidt34af3062013-07-11 10:46:32 -07004010 if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_DEDICATED_P2P_DEVICE) &&
4011 wpa_s->conf->p2p_no_group_iface)
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08004012 return 0; /* separate interface disabled per configuration */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004013 if (wpa_s->drv_flags &
4014 (WPA_DRIVER_FLAGS_P2P_DEDICATED_INTERFACE |
4015 WPA_DRIVER_FLAGS_P2P_MGMT_AND_NON_P2P))
4016 return 1; /* P2P group requires a new interface in every case
4017 */
4018 if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_CONCURRENT))
4019 return 0; /* driver does not support concurrent operations */
4020 if (wpa_s->global->ifaces->next)
4021 return 1; /* more that one interface already in use */
4022 if (wpa_s->wpa_state >= WPA_AUTHENTICATING)
4023 return 1; /* this interface is already in use */
4024 return 0;
4025}
4026
4027
4028static int wpas_p2p_start_go_neg(struct wpa_supplicant *wpa_s,
4029 const u8 *peer_addr,
4030 enum p2p_wps_method wps_method,
4031 int go_intent, const u8 *own_interface_addr,
Dmitry Shmidt04949592012-07-19 12:16:46 -07004032 unsigned int force_freq, int persistent_group,
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08004033 struct wpa_ssid *ssid, unsigned int pref_freq)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004034{
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004035 if (persistent_group && wpa_s->conf->persistent_reconnect)
4036 persistent_group = 2;
4037
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004038 /*
4039 * Increase GO config timeout if HT40 is used since it takes some time
4040 * to scan channels for coex purposes before the BSS can be started.
4041 */
4042 p2p_set_config_timeout(wpa_s->global->p2p,
4043 wpa_s->p2p_go_ht40 ? 255 : 100, 20);
4044
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004045 return p2p_connect(wpa_s->global->p2p, peer_addr, wps_method,
4046 go_intent, own_interface_addr, force_freq,
Dmitry Shmidt04949592012-07-19 12:16:46 -07004047 persistent_group, ssid ? ssid->ssid : NULL,
4048 ssid ? ssid->ssid_len : 0,
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08004049 wpa_s->p2p_pd_before_go_neg, pref_freq,
4050 wps_method == WPS_NFC ? wpa_s->p2p_oob_dev_pw_id :
4051 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004052}
4053
4054
4055static int wpas_p2p_auth_go_neg(struct wpa_supplicant *wpa_s,
4056 const u8 *peer_addr,
4057 enum p2p_wps_method wps_method,
4058 int go_intent, const u8 *own_interface_addr,
Dmitry Shmidt04949592012-07-19 12:16:46 -07004059 unsigned int force_freq, int persistent_group,
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08004060 struct wpa_ssid *ssid, unsigned int pref_freq)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004061{
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004062 if (persistent_group && wpa_s->conf->persistent_reconnect)
4063 persistent_group = 2;
4064
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004065 return p2p_authorize(wpa_s->global->p2p, peer_addr, wps_method,
4066 go_intent, own_interface_addr, force_freq,
Dmitry Shmidt04949592012-07-19 12:16:46 -07004067 persistent_group, ssid ? ssid->ssid : NULL,
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08004068 ssid ? ssid->ssid_len : 0, pref_freq,
4069 wps_method == WPS_NFC ? wpa_s->p2p_oob_dev_pw_id :
4070 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004071}
4072
4073
4074static void wpas_p2p_check_join_scan_limit(struct wpa_supplicant *wpa_s)
4075{
4076 wpa_s->p2p_join_scan_count++;
4077 wpa_printf(MSG_DEBUG, "P2P: Join scan attempt %d",
4078 wpa_s->p2p_join_scan_count);
4079 if (wpa_s->p2p_join_scan_count > P2P_MAX_JOIN_SCAN_ATTEMPTS) {
4080 wpa_printf(MSG_DEBUG, "P2P: Failed to find GO " MACSTR
4081 " for join operationg - stop join attempt",
4082 MAC2STR(wpa_s->pending_join_iface_addr));
4083 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
Dmitry Shmidt04949592012-07-19 12:16:46 -07004084 if (wpa_s->p2p_auto_pd) {
4085 wpa_s->p2p_auto_pd = 0;
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07004086 wpa_msg_global(wpa_s, MSG_INFO,
4087 P2P_EVENT_PROV_DISC_FAILURE
4088 " p2p_dev_addr=" MACSTR " status=N/A",
4089 MAC2STR(wpa_s->pending_join_dev_addr));
Dmitry Shmidt04949592012-07-19 12:16:46 -07004090 return;
4091 }
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07004092 wpa_msg_global(wpa_s->parent, MSG_INFO,
4093 P2P_EVENT_GROUP_FORMATION_FAILURE);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004094 }
4095}
4096
4097
Dmitry Shmidt04949592012-07-19 12:16:46 -07004098static int wpas_check_freq_conflict(struct wpa_supplicant *wpa_s, int freq)
4099{
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07004100 int *freqs, res, num, i;
Dmitry Shmidt04949592012-07-19 12:16:46 -07004101
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07004102 if (wpas_p2p_num_unused_channels(wpa_s) > 0) {
4103 /* Multiple channels are supported and not all are in use */
Dmitry Shmidt04949592012-07-19 12:16:46 -07004104 return 0;
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07004105 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07004106
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07004107 freqs = os_calloc(wpa_s->num_multichan_concurrent, sizeof(int));
4108 if (!freqs)
4109 return 1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07004110
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07004111 num = wpas_p2p_valid_oper_freqs(wpa_s, freqs,
4112 wpa_s->num_multichan_concurrent);
4113 if (num < 0) {
4114 res = 1;
4115 goto exit_free;
4116 }
4117
4118 for (i = 0; i < num; i++) {
4119 if (freqs[i] == freq) {
4120 wpa_printf(MSG_DEBUG, "P2P: Frequency %d MHz in use by another virtual interface and can be used",
4121 freq);
4122 res = 0;
4123 goto exit_free;
Dmitry Shmidt04949592012-07-19 12:16:46 -07004124 }
4125 }
4126
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07004127 res = 1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07004128
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07004129exit_free:
4130 os_free(freqs);
4131 return res;
Dmitry Shmidt04949592012-07-19 12:16:46 -07004132}
4133
4134
4135static int wpas_p2p_peer_go(struct wpa_supplicant *wpa_s,
4136 const u8 *peer_dev_addr)
4137{
4138 struct wpa_bss *bss;
4139 int updated;
4140
4141 bss = wpa_bss_get_p2p_dev_addr(wpa_s, peer_dev_addr);
4142 if (bss == NULL)
4143 return -1;
4144 if (bss->last_update_idx < wpa_s->bss_update_idx) {
4145 wpa_printf(MSG_DEBUG, "P2P: Peer BSS entry not updated in the "
4146 "last scan");
4147 return 0;
4148 }
4149
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08004150 updated = os_reltime_before(&wpa_s->p2p_auto_started,
4151 &bss->last_update);
Dmitry Shmidt04949592012-07-19 12:16:46 -07004152 wpa_printf(MSG_DEBUG, "P2P: Current BSS entry for peer updated at "
4153 "%ld.%06ld (%supdated in last scan)",
4154 bss->last_update.sec, bss->last_update.usec,
4155 updated ? "": "not ");
4156
4157 return updated;
4158}
4159
4160
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004161static void wpas_p2p_scan_res_join(struct wpa_supplicant *wpa_s,
4162 struct wpa_scan_results *scan_res)
4163{
Dmitry Shmidt344abd32014-01-14 13:17:00 -08004164 struct wpa_bss *bss = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004165 int freq;
4166 u8 iface_addr[ETH_ALEN];
Dmitry Shmidt04949592012-07-19 12:16:46 -07004167
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004168 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
4169
4170 if (wpa_s->global->p2p_disabled)
4171 return;
4172
Dmitry Shmidt04949592012-07-19 12:16:46 -07004173 wpa_printf(MSG_DEBUG, "P2P: Scan results received (%d BSS) for %sjoin",
4174 scan_res ? (int) scan_res->num : -1,
4175 wpa_s->p2p_auto_join ? "auto_" : "");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004176
4177 if (scan_res)
4178 wpas_p2p_scan_res_handler(wpa_s, scan_res);
4179
Dmitry Shmidt04949592012-07-19 12:16:46 -07004180 if (wpa_s->p2p_auto_pd) {
4181 int join = wpas_p2p_peer_go(wpa_s,
4182 wpa_s->pending_join_dev_addr);
4183 if (join == 0 &&
4184 wpa_s->auto_pd_scan_retry < P2P_AUTO_PD_SCAN_ATTEMPTS) {
4185 wpa_s->auto_pd_scan_retry++;
Dmitry Shmidt444d5672013-04-01 13:08:44 -07004186 bss = wpa_bss_get_bssid_latest(
4187 wpa_s, wpa_s->pending_join_dev_addr);
Dmitry Shmidt04949592012-07-19 12:16:46 -07004188 if (bss) {
4189 freq = bss->freq;
4190 wpa_printf(MSG_DEBUG, "P2P: Scan retry %d for "
4191 "the peer " MACSTR " at %d MHz",
4192 wpa_s->auto_pd_scan_retry,
4193 MAC2STR(wpa_s->
4194 pending_join_dev_addr),
4195 freq);
Dmitry Shmidt344abd32014-01-14 13:17:00 -08004196 wpas_p2p_join_scan_req(wpa_s, freq, NULL, 0);
Dmitry Shmidt04949592012-07-19 12:16:46 -07004197 return;
4198 }
4199 }
4200
4201 if (join < 0)
4202 join = 0;
4203
4204 wpa_s->p2p_auto_pd = 0;
4205 wpa_s->pending_pd_use = join ? AUTO_PD_JOIN : AUTO_PD_GO_NEG;
4206 wpa_printf(MSG_DEBUG, "P2P: Auto PD with " MACSTR " join=%d",
4207 MAC2STR(wpa_s->pending_join_dev_addr), join);
4208 if (p2p_prov_disc_req(wpa_s->global->p2p,
4209 wpa_s->pending_join_dev_addr,
4210 wpa_s->pending_pd_config_methods, join,
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08004211 0, wpa_s->user_initiated_pd) < 0) {
Dmitry Shmidt04949592012-07-19 12:16:46 -07004212 wpa_s->p2p_auto_pd = 0;
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07004213 wpa_msg_global(wpa_s, MSG_INFO,
4214 P2P_EVENT_PROV_DISC_FAILURE
4215 " p2p_dev_addr=" MACSTR " status=N/A",
4216 MAC2STR(wpa_s->pending_join_dev_addr));
Dmitry Shmidt04949592012-07-19 12:16:46 -07004217 }
4218 return;
4219 }
4220
4221 if (wpa_s->p2p_auto_join) {
4222 int join = wpas_p2p_peer_go(wpa_s,
4223 wpa_s->pending_join_dev_addr);
4224 if (join < 0) {
4225 wpa_printf(MSG_DEBUG, "P2P: Peer was not found to be "
4226 "running a GO -> use GO Negotiation");
4227 wpas_p2p_connect(wpa_s, wpa_s->pending_join_dev_addr,
4228 wpa_s->p2p_pin, wpa_s->p2p_wps_method,
4229 wpa_s->p2p_persistent_group, 0, 0, 0,
4230 wpa_s->p2p_go_intent,
4231 wpa_s->p2p_connect_freq,
4232 wpa_s->p2p_persistent_id,
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004233 wpa_s->p2p_pd_before_go_neg,
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004234 wpa_s->p2p_go_ht40,
4235 wpa_s->p2p_go_vht);
Dmitry Shmidt04949592012-07-19 12:16:46 -07004236 return;
4237 }
4238
4239 wpa_printf(MSG_DEBUG, "P2P: Peer was found running GO%s -> "
4240 "try to join the group", join ? "" :
4241 " in older scan");
4242 if (!join)
4243 wpa_s->p2p_fallback_to_go_neg = 1;
4244 }
4245
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004246 freq = p2p_get_oper_freq(wpa_s->global->p2p,
4247 wpa_s->pending_join_iface_addr);
4248 if (freq < 0 &&
4249 p2p_get_interface_addr(wpa_s->global->p2p,
4250 wpa_s->pending_join_dev_addr,
4251 iface_addr) == 0 &&
4252 os_memcmp(iface_addr, wpa_s->pending_join_dev_addr, ETH_ALEN) != 0)
4253 {
4254 wpa_printf(MSG_DEBUG, "P2P: Overwrite pending interface "
4255 "address for join from " MACSTR " to " MACSTR
4256 " based on newly discovered P2P peer entry",
4257 MAC2STR(wpa_s->pending_join_iface_addr),
4258 MAC2STR(iface_addr));
4259 os_memcpy(wpa_s->pending_join_iface_addr, iface_addr,
4260 ETH_ALEN);
4261
4262 freq = p2p_get_oper_freq(wpa_s->global->p2p,
4263 wpa_s->pending_join_iface_addr);
4264 }
4265 if (freq >= 0) {
4266 wpa_printf(MSG_DEBUG, "P2P: Target GO operating frequency "
4267 "from P2P peer table: %d MHz", freq);
4268 }
Dmitry Shmidt344abd32014-01-14 13:17:00 -08004269 if (wpa_s->p2p_join_ssid_len) {
4270 wpa_printf(MSG_DEBUG, "P2P: Trying to find target GO BSS entry based on BSSID "
4271 MACSTR " and SSID %s",
4272 MAC2STR(wpa_s->pending_join_iface_addr),
4273 wpa_ssid_txt(wpa_s->p2p_join_ssid,
4274 wpa_s->p2p_join_ssid_len));
4275 bss = wpa_bss_get(wpa_s, wpa_s->pending_join_iface_addr,
4276 wpa_s->p2p_join_ssid,
4277 wpa_s->p2p_join_ssid_len);
4278 }
4279 if (!bss) {
4280 wpa_printf(MSG_DEBUG, "P2P: Trying to find target GO BSS entry based on BSSID "
4281 MACSTR, MAC2STR(wpa_s->pending_join_iface_addr));
4282 bss = wpa_bss_get_bssid_latest(wpa_s,
4283 wpa_s->pending_join_iface_addr);
4284 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004285 if (bss) {
4286 freq = bss->freq;
4287 wpa_printf(MSG_DEBUG, "P2P: Target GO operating frequency "
Dmitry Shmidt051af732013-10-22 13:52:46 -07004288 "from BSS table: %d MHz (SSID %s)", freq,
4289 wpa_ssid_txt(bss->ssid, bss->ssid_len));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004290 }
4291 if (freq > 0) {
4292 u16 method;
4293
Dmitry Shmidt04949592012-07-19 12:16:46 -07004294 if (wpas_check_freq_conflict(wpa_s, freq) > 0) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07004295 wpa_msg_global(wpa_s->parent, MSG_INFO,
4296 P2P_EVENT_GROUP_FORMATION_FAILURE
4297 "reason=FREQ_CONFLICT");
Dmitry Shmidt04949592012-07-19 12:16:46 -07004298 return;
4299 }
4300
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004301 wpa_printf(MSG_DEBUG, "P2P: Send Provision Discovery Request "
4302 "prior to joining an existing group (GO " MACSTR
4303 " freq=%u MHz)",
4304 MAC2STR(wpa_s->pending_join_dev_addr), freq);
4305 wpa_s->pending_pd_before_join = 1;
4306
4307 switch (wpa_s->pending_join_wps_method) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004308 case WPS_PIN_DISPLAY:
4309 method = WPS_CONFIG_KEYPAD;
4310 break;
4311 case WPS_PIN_KEYPAD:
4312 method = WPS_CONFIG_DISPLAY;
4313 break;
4314 case WPS_PBC:
4315 method = WPS_CONFIG_PUSHBUTTON;
4316 break;
4317 default:
4318 method = 0;
4319 break;
4320 }
4321
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004322 if ((p2p_get_provisioning_info(wpa_s->global->p2p,
4323 wpa_s->pending_join_dev_addr) ==
4324 method)) {
4325 /*
4326 * We have already performed provision discovery for
4327 * joining the group. Proceed directly to join
4328 * operation without duplicated provision discovery. */
4329 wpa_printf(MSG_DEBUG, "P2P: Provision discovery "
4330 "with " MACSTR " already done - proceed to "
4331 "join",
4332 MAC2STR(wpa_s->pending_join_dev_addr));
4333 wpa_s->pending_pd_before_join = 0;
4334 goto start;
4335 }
4336
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004337 if (p2p_prov_disc_req(wpa_s->global->p2p,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004338 wpa_s->pending_join_dev_addr, method, 1,
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08004339 freq, wpa_s->user_initiated_pd) < 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004340 wpa_printf(MSG_DEBUG, "P2P: Failed to send Provision "
4341 "Discovery Request before joining an "
4342 "existing group");
4343 wpa_s->pending_pd_before_join = 0;
4344 goto start;
4345 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004346 return;
4347 }
4348
4349 wpa_printf(MSG_DEBUG, "P2P: Failed to find BSS/GO - try again later");
4350 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
4351 eloop_register_timeout(1, 0, wpas_p2p_join_scan, wpa_s, NULL);
4352 wpas_p2p_check_join_scan_limit(wpa_s);
4353 return;
4354
4355start:
4356 /* Start join operation immediately */
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08004357 wpas_p2p_join_start(wpa_s, 0, NULL, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004358}
4359
4360
Dmitry Shmidt344abd32014-01-14 13:17:00 -08004361static void wpas_p2p_join_scan_req(struct wpa_supplicant *wpa_s, int freq,
4362 const u8 *ssid, size_t ssid_len)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004363{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004364 int ret;
4365 struct wpa_driver_scan_params params;
4366 struct wpabuf *wps_ie, *ies;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004367 size_t ielen;
Dmitry Shmidt04949592012-07-19 12:16:46 -07004368 int freqs[2] = { 0, 0 };
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004369
4370 os_memset(&params, 0, sizeof(params));
4371
4372 /* P2P Wildcard SSID */
4373 params.num_ssids = 1;
Dmitry Shmidt344abd32014-01-14 13:17:00 -08004374 if (ssid && ssid_len) {
4375 params.ssids[0].ssid = ssid;
4376 params.ssids[0].ssid_len = ssid_len;
4377 os_memcpy(wpa_s->p2p_join_ssid, ssid, ssid_len);
4378 wpa_s->p2p_join_ssid_len = ssid_len;
4379 } else {
4380 params.ssids[0].ssid = (u8 *) P2P_WILDCARD_SSID;
4381 params.ssids[0].ssid_len = P2P_WILDCARD_SSID_LEN;
4382 wpa_s->p2p_join_ssid_len = 0;
4383 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004384
4385 wpa_s->wps->dev.p2p = 1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07004386 wps_ie = wps_build_probe_req_ie(DEV_PW_DEFAULT, &wpa_s->wps->dev,
4387 wpa_s->wps->uuid, WPS_REQ_ENROLLEE, 0,
4388 NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004389 if (wps_ie == NULL) {
4390 wpas_p2p_scan_res_join(wpa_s, NULL);
4391 return;
4392 }
4393
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004394 ielen = p2p_scan_ie_buf_len(wpa_s->global->p2p);
4395 ies = wpabuf_alloc(wpabuf_len(wps_ie) + ielen);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004396 if (ies == NULL) {
4397 wpabuf_free(wps_ie);
4398 wpas_p2p_scan_res_join(wpa_s, NULL);
4399 return;
4400 }
4401 wpabuf_put_buf(ies, wps_ie);
4402 wpabuf_free(wps_ie);
4403
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08004404 p2p_scan_ie(wpa_s->global->p2p, ies, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004405
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004406 params.p2p_probe = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004407 params.extra_ies = wpabuf_head(ies);
4408 params.extra_ies_len = wpabuf_len(ies);
Dmitry Shmidt9e3f8ee2014-01-17 10:52:01 -08004409
4410 if (!freq) {
4411 int oper_freq;
4412 /*
4413 * If freq is not provided, check the operating freq of the GO
4414 * and use a single channel scan on if possible.
4415 */
4416 oper_freq = p2p_get_oper_freq(wpa_s->global->p2p,
4417 wpa_s->pending_join_iface_addr);
4418 if (oper_freq > 0)
4419 freq = oper_freq;
4420 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07004421 if (freq > 0) {
4422 freqs[0] = freq;
4423 params.freqs = freqs;
4424 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004425
4426 /*
4427 * Run a scan to update BSS table and start Provision Discovery once
4428 * the new scan results become available.
4429 */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004430 ret = wpa_drv_scan(wpa_s, &params);
Dmitry Shmidt444d5672013-04-01 13:08:44 -07004431 if (!ret) {
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08004432 os_get_reltime(&wpa_s->scan_trigger_time);
Dmitry Shmidt04949592012-07-19 12:16:46 -07004433 wpa_s->scan_res_handler = wpas_p2p_scan_res_join;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08004434 wpa_s->own_scan_requested = 1;
Dmitry Shmidt444d5672013-04-01 13:08:44 -07004435 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004436
4437 wpabuf_free(ies);
4438
4439 if (ret) {
4440 wpa_printf(MSG_DEBUG, "P2P: Failed to start scan for join - "
4441 "try again later");
4442 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
4443 eloop_register_timeout(1, 0, wpas_p2p_join_scan, wpa_s, NULL);
4444 wpas_p2p_check_join_scan_limit(wpa_s);
4445 }
4446}
4447
4448
Dmitry Shmidt04949592012-07-19 12:16:46 -07004449static void wpas_p2p_join_scan(void *eloop_ctx, void *timeout_ctx)
4450{
4451 struct wpa_supplicant *wpa_s = eloop_ctx;
Dmitry Shmidt344abd32014-01-14 13:17:00 -08004452 wpas_p2p_join_scan_req(wpa_s, 0, NULL, 0);
Dmitry Shmidt04949592012-07-19 12:16:46 -07004453}
4454
4455
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004456static int wpas_p2p_join(struct wpa_supplicant *wpa_s, const u8 *iface_addr,
Dmitry Shmidt04949592012-07-19 12:16:46 -07004457 const u8 *dev_addr, enum p2p_wps_method wps_method,
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08004458 int auto_join, int op_freq,
4459 const u8 *ssid, size_t ssid_len)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004460{
4461 wpa_printf(MSG_DEBUG, "P2P: Request to join existing group (iface "
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08004462 MACSTR " dev " MACSTR " op_freq=%d)%s",
4463 MAC2STR(iface_addr), MAC2STR(dev_addr), op_freq,
Dmitry Shmidt04949592012-07-19 12:16:46 -07004464 auto_join ? " (auto_join)" : "");
Dmitry Shmidt344abd32014-01-14 13:17:00 -08004465 if (ssid && ssid_len) {
4466 wpa_printf(MSG_DEBUG, "P2P: Group SSID specified: %s",
4467 wpa_ssid_txt(ssid, ssid_len));
4468 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004469
Dmitry Shmidt04949592012-07-19 12:16:46 -07004470 wpa_s->p2p_auto_pd = 0;
4471 wpa_s->p2p_auto_join = !!auto_join;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004472 os_memcpy(wpa_s->pending_join_iface_addr, iface_addr, ETH_ALEN);
4473 os_memcpy(wpa_s->pending_join_dev_addr, dev_addr, ETH_ALEN);
4474 wpa_s->pending_join_wps_method = wps_method;
4475
4476 /* Make sure we are not running find during connection establishment */
4477 wpas_p2p_stop_find(wpa_s);
4478
4479 wpa_s->p2p_join_scan_count = 0;
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08004480 wpas_p2p_join_scan_req(wpa_s, op_freq, ssid, ssid_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004481 return 0;
4482}
4483
4484
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08004485static int wpas_p2p_join_start(struct wpa_supplicant *wpa_s, int freq,
4486 const u8 *ssid, size_t ssid_len)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004487{
4488 struct wpa_supplicant *group;
4489 struct p2p_go_neg_results res;
Dmitry Shmidt04949592012-07-19 12:16:46 -07004490 struct wpa_bss *bss;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004491
4492 group = wpas_p2p_get_group_iface(wpa_s, 0, 0);
4493 if (group == NULL)
4494 return -1;
4495 if (group != wpa_s) {
4496 os_memcpy(group->p2p_pin, wpa_s->p2p_pin,
4497 sizeof(group->p2p_pin));
4498 group->p2p_wps_method = wpa_s->p2p_wps_method;
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08004499 } else {
4500 /*
4501 * Need to mark the current interface for p2p_group_formation
4502 * when a separate group interface is not used. This is needed
4503 * to allow p2p_cancel stop a pending p2p_connect-join.
4504 * wpas_p2p_init_group_interface() addresses this for the case
4505 * where a separate group interface is used.
4506 */
4507 wpa_s->global->p2p_group_formation = wpa_s;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004508 }
4509
4510 group->p2p_in_provisioning = 1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07004511 group->p2p_fallback_to_go_neg = wpa_s->p2p_fallback_to_go_neg;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004512
4513 os_memset(&res, 0, sizeof(res));
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08004514 os_memcpy(res.peer_device_addr, wpa_s->pending_join_dev_addr, ETH_ALEN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004515 os_memcpy(res.peer_interface_addr, wpa_s->pending_join_iface_addr,
4516 ETH_ALEN);
4517 res.wps_method = wpa_s->pending_join_wps_method;
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08004518 if (freq && ssid && ssid_len) {
4519 res.freq = freq;
4520 res.ssid_len = ssid_len;
4521 os_memcpy(res.ssid, ssid, ssid_len);
4522 } else {
4523 bss = wpa_bss_get_bssid_latest(wpa_s,
4524 wpa_s->pending_join_iface_addr);
4525 if (bss) {
4526 res.freq = bss->freq;
4527 res.ssid_len = bss->ssid_len;
4528 os_memcpy(res.ssid, bss->ssid, bss->ssid_len);
4529 wpa_printf(MSG_DEBUG, "P2P: Join target GO operating frequency from BSS table: %d MHz (SSID %s)",
4530 bss->freq,
4531 wpa_ssid_txt(bss->ssid, bss->ssid_len));
4532 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07004533 }
4534
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08004535 if (wpa_s->off_channel_freq || wpa_s->roc_waiting_drv_freq) {
4536 wpa_printf(MSG_DEBUG, "P2P: Cancel remain-on-channel prior to "
4537 "starting client");
4538 wpa_drv_cancel_remain_on_channel(wpa_s);
4539 wpa_s->off_channel_freq = 0;
4540 wpa_s->roc_waiting_drv_freq = 0;
4541 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004542 wpas_start_wps_enrollee(group, &res);
4543
4544 /*
4545 * Allow a longer timeout for join-a-running-group than normal 15
4546 * second group formation timeout since the GO may not have authorized
4547 * our connection yet.
4548 */
4549 eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s, NULL);
4550 eloop_register_timeout(60, 0, wpas_p2p_group_formation_timeout,
4551 wpa_s, NULL);
4552
4553 return 0;
4554}
4555
4556
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08004557static int wpas_p2p_setup_freqs(struct wpa_supplicant *wpa_s, int freq,
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004558 int *force_freq, int *pref_freq, int go)
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08004559{
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07004560 int *freqs, res;
4561 unsigned int freq_in_use = 0, num, i;
4562
4563 freqs = os_calloc(wpa_s->num_multichan_concurrent, sizeof(int));
4564 if (!freqs)
4565 return -1;
4566
4567 num = get_shared_radio_freqs(wpa_s, freqs,
4568 wpa_s->num_multichan_concurrent);
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07004569 wpa_printf(MSG_DEBUG,
4570 "P2P: Setup freqs: freq=%d num_MCC=%d shared_freqs=%u",
4571 freq, wpa_s->num_multichan_concurrent, num);
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07004572
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08004573 if (freq > 0) {
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004574 int ret;
4575 if (go)
4576 ret = p2p_supported_freq(wpa_s->global->p2p, freq);
4577 else
4578 ret = p2p_supported_freq_cli(wpa_s->global->p2p, freq);
4579 if (!ret) {
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08004580 wpa_printf(MSG_DEBUG, "P2P: The forced channel "
4581 "(%u MHz) is not supported for P2P uses",
4582 freq);
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07004583 res = -3;
4584 goto exit_free;
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08004585 }
4586
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07004587 for (i = 0; i < num; i++) {
4588 if (freqs[i] == freq)
4589 freq_in_use = 1;
4590 }
4591
4592 if (num == wpa_s->num_multichan_concurrent && !freq_in_use) {
4593 wpa_printf(MSG_DEBUG, "P2P: Cannot start P2P group on %u MHz as there are no available channels",
4594 freq);
4595 res = -2;
4596 goto exit_free;
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08004597 }
4598 wpa_printf(MSG_DEBUG, "P2P: Trying to force us to use the "
4599 "requested channel (%u MHz)", freq);
4600 *force_freq = freq;
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07004601 goto exit_ok;
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08004602 }
4603
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07004604 for (i = 0; i < num; i++) {
4605 if (!p2p_supported_freq(wpa_s->global->p2p, freqs[i]))
4606 continue;
4607
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07004608 if (*pref_freq == 0 && num < wpa_s->num_multichan_concurrent) {
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08004609 wpa_printf(MSG_DEBUG, "P2P: Try to prefer a frequency (%u MHz) we are already using",
4610 freqs[i]);
Dmitry Shmidt51a47d52013-09-10 10:52:57 -07004611 *pref_freq = freqs[i];
Dmitry Shmidt51a47d52013-09-10 10:52:57 -07004612 } else {
4613 wpa_printf(MSG_DEBUG, "P2P: Try to force us to use frequency (%u MHz) which is already in use",
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08004614 freqs[i]);
Dmitry Shmidt51a47d52013-09-10 10:52:57 -07004615 *force_freq = freqs[i];
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07004616 }
4617 break;
4618 }
4619
4620 if (i == num) {
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08004621 if (num < wpa_s->num_multichan_concurrent && num > 0) {
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07004622 wpa_printf(MSG_DEBUG, "P2P: Current operating channels are not available for P2P. Try to use another channel");
4623 *force_freq = 0;
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08004624 } else if (num < wpa_s->num_multichan_concurrent) {
4625 wpa_printf(MSG_DEBUG, "P2P: No current operating channels - try to use a new channel");
4626 *force_freq = 0;
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07004627 } else {
4628 wpa_printf(MSG_DEBUG, "P2P: All channels are in use and none of them are P2P enabled. Cannot start P2P group");
4629 res = -2;
4630 goto exit_free;
4631 }
4632 }
4633
4634exit_ok:
4635 res = 0;
4636exit_free:
4637 os_free(freqs);
4638 return res;
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08004639}
4640
4641
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004642/**
4643 * wpas_p2p_connect - Request P2P Group Formation to be started
4644 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
4645 * @peer_addr: Address of the peer P2P Device
4646 * @pin: PIN to use during provisioning or %NULL to indicate PBC mode
4647 * @persistent_group: Whether to create a persistent group
Dmitry Shmidt04949592012-07-19 12:16:46 -07004648 * @auto_join: Whether to select join vs. GO Negotiation automatically
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004649 * @join: Whether to join an existing group (as a client) instead of starting
4650 * Group Owner negotiation; @peer_addr is BSSID in that case
4651 * @auth: Whether to only authorize the connection instead of doing that and
4652 * initiating Group Owner negotiation
4653 * @go_intent: GO Intent or -1 to use default
4654 * @freq: Frequency for the group or 0 for auto-selection
Dmitry Shmidt04949592012-07-19 12:16:46 -07004655 * @persistent_id: Persistent group credentials to use for forcing GO
4656 * parameters or -1 to generate new values (SSID/passphrase)
4657 * @pd: Whether to send Provision Discovery prior to GO Negotiation as an
4658 * interoperability workaround when initiating group formation
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004659 * @ht40: Start GO with 40 MHz channel width
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004660 * @vht: Start GO with VHT support
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004661 * Returns: 0 or new PIN (if pin was %NULL) on success, -1 on unspecified
4662 * failure, -2 on failure due to channel not currently available,
4663 * -3 if forced channel is not supported
4664 */
4665int wpas_p2p_connect(struct wpa_supplicant *wpa_s, const u8 *peer_addr,
4666 const char *pin, enum p2p_wps_method wps_method,
Dmitry Shmidt04949592012-07-19 12:16:46 -07004667 int persistent_group, int auto_join, int join, int auth,
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004668 int go_intent, int freq, int persistent_id, int pd,
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004669 int ht40, int vht)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004670{
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07004671 int force_freq = 0, pref_freq = 0;
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08004672 int ret = 0, res;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004673 enum wpa_driver_if_type iftype;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004674 const u8 *if_addr;
Dmitry Shmidt04949592012-07-19 12:16:46 -07004675 struct wpa_ssid *ssid = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004676
4677 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
4678 return -1;
4679
Dmitry Shmidt04949592012-07-19 12:16:46 -07004680 if (persistent_id >= 0) {
4681 ssid = wpa_config_get_network(wpa_s->conf, persistent_id);
4682 if (ssid == NULL || ssid->disabled != 2 ||
4683 ssid->mode != WPAS_MODE_P2P_GO)
4684 return -1;
4685 }
4686
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07004687 os_free(wpa_s->global->add_psk);
4688 wpa_s->global->add_psk = NULL;
4689
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08004690 wpa_s->global->p2p_fail_on_wps_complete = 0;
4691
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004692 if (go_intent < 0)
4693 go_intent = wpa_s->conf->p2p_go_intent;
4694
4695 if (!auth)
4696 wpa_s->p2p_long_listen = 0;
4697
4698 wpa_s->p2p_wps_method = wps_method;
Dmitry Shmidt04949592012-07-19 12:16:46 -07004699 wpa_s->p2p_persistent_group = !!persistent_group;
4700 wpa_s->p2p_persistent_id = persistent_id;
4701 wpa_s->p2p_go_intent = go_intent;
4702 wpa_s->p2p_connect_freq = freq;
4703 wpa_s->p2p_fallback_to_go_neg = 0;
4704 wpa_s->p2p_pd_before_go_neg = !!pd;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004705 wpa_s->p2p_go_ht40 = !!ht40;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004706 wpa_s->p2p_go_vht = !!vht;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004707
4708 if (pin)
4709 os_strlcpy(wpa_s->p2p_pin, pin, sizeof(wpa_s->p2p_pin));
4710 else if (wps_method == WPS_PIN_DISPLAY) {
4711 ret = wps_generate_pin();
4712 os_snprintf(wpa_s->p2p_pin, sizeof(wpa_s->p2p_pin), "%08d",
4713 ret);
4714 wpa_printf(MSG_DEBUG, "P2P: Randomly generated PIN: %s",
4715 wpa_s->p2p_pin);
4716 } else
4717 wpa_s->p2p_pin[0] = '\0';
4718
Dmitry Shmidt04949592012-07-19 12:16:46 -07004719 if (join || auto_join) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004720 u8 iface_addr[ETH_ALEN], dev_addr[ETH_ALEN];
4721 if (auth) {
4722 wpa_printf(MSG_DEBUG, "P2P: Authorize invitation to "
4723 "connect a running group from " MACSTR,
4724 MAC2STR(peer_addr));
4725 os_memcpy(wpa_s->p2p_auth_invite, peer_addr, ETH_ALEN);
4726 return ret;
4727 }
4728 os_memcpy(dev_addr, peer_addr, ETH_ALEN);
4729 if (p2p_get_interface_addr(wpa_s->global->p2p, peer_addr,
4730 iface_addr) < 0) {
4731 os_memcpy(iface_addr, peer_addr, ETH_ALEN);
4732 p2p_get_dev_addr(wpa_s->global->p2p, peer_addr,
4733 dev_addr);
4734 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07004735 if (auto_join) {
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08004736 os_get_reltime(&wpa_s->p2p_auto_started);
Dmitry Shmidt04949592012-07-19 12:16:46 -07004737 wpa_printf(MSG_DEBUG, "P2P: Auto join started at "
4738 "%ld.%06ld",
4739 wpa_s->p2p_auto_started.sec,
4740 wpa_s->p2p_auto_started.usec);
4741 }
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08004742 wpa_s->user_initiated_pd = 1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07004743 if (wpas_p2p_join(wpa_s, iface_addr, dev_addr, wps_method,
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08004744 auto_join, freq, NULL, 0) < 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004745 return -1;
4746 return ret;
4747 }
4748
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004749 res = wpas_p2p_setup_freqs(wpa_s, freq, &force_freq, &pref_freq,
4750 go_intent == 15);
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08004751 if (res)
4752 return res;
Dmitry Shmidta0d265f2013-11-19 13:13:41 -08004753 wpas_p2p_set_own_freq_preference(wpa_s,
4754 force_freq ? force_freq : pref_freq);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004755
4756 wpa_s->create_p2p_iface = wpas_p2p_create_iface(wpa_s);
4757
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004758 if (wpa_s->create_p2p_iface) {
4759 /* Prepare to add a new interface for the group */
4760 iftype = WPA_IF_P2P_GROUP;
4761 if (go_intent == 15)
4762 iftype = WPA_IF_P2P_GO;
4763 if (wpas_p2p_add_group_interface(wpa_s, iftype) < 0) {
4764 wpa_printf(MSG_ERROR, "P2P: Failed to allocate a new "
4765 "interface for the group");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004766 return -1;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004767 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004768
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004769 if_addr = wpa_s->pending_interface_addr;
4770 } else
4771 if_addr = wpa_s->own_addr;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004772
4773 if (auth) {
4774 if (wpas_p2p_auth_go_neg(wpa_s, peer_addr, wps_method,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004775 go_intent, if_addr,
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08004776 force_freq, persistent_group, ssid,
4777 pref_freq) < 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004778 return -1;
4779 return ret;
4780 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004781
4782 if (wpas_p2p_start_go_neg(wpa_s, peer_addr, wps_method,
4783 go_intent, if_addr, force_freq,
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08004784 persistent_group, ssid, pref_freq) < 0) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004785 if (wpa_s->create_p2p_iface)
4786 wpas_p2p_remove_pending_group_interface(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004787 return -1;
4788 }
4789 return ret;
4790}
4791
4792
4793/**
4794 * wpas_p2p_remain_on_channel_cb - Indication of remain-on-channel start
4795 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
4796 * @freq: Frequency of the channel in MHz
4797 * @duration: Duration of the stay on the channel in milliseconds
4798 *
4799 * This callback is called when the driver indicates that it has started the
4800 * requested remain-on-channel duration.
4801 */
4802void wpas_p2p_remain_on_channel_cb(struct wpa_supplicant *wpa_s,
4803 unsigned int freq, unsigned int duration)
4804{
4805 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
4806 return;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004807 if (wpa_s->off_channel_freq == wpa_s->pending_listen_freq) {
4808 p2p_listen_cb(wpa_s->global->p2p, wpa_s->pending_listen_freq,
4809 wpa_s->pending_listen_duration);
4810 wpa_s->pending_listen_freq = 0;
Dmitry Shmidt7f93d6f2014-02-21 11:22:49 -08004811 } else {
4812 wpa_printf(MSG_DEBUG, "P2P: Ignore remain-on-channel callback (off_channel_freq=%u pending_listen_freq=%d freq=%u duration=%u)",
4813 wpa_s->off_channel_freq, wpa_s->pending_listen_freq,
4814 freq, duration);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004815 }
4816}
4817
4818
4819static int wpas_p2p_listen_start(struct wpa_supplicant *wpa_s,
4820 unsigned int timeout)
4821{
4822 /* Limit maximum Listen state time based on driver limitation. */
4823 if (timeout > wpa_s->max_remain_on_chan)
4824 timeout = wpa_s->max_remain_on_chan;
4825
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004826 return p2p_listen(wpa_s->global->p2p, timeout);
4827}
4828
4829
4830/**
4831 * wpas_p2p_cancel_remain_on_channel_cb - Remain-on-channel timeout
4832 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
4833 * @freq: Frequency of the channel in MHz
4834 *
4835 * This callback is called when the driver indicates that a remain-on-channel
4836 * operation has been completed, i.e., the duration on the requested channel
4837 * has timed out.
4838 */
4839void wpas_p2p_cancel_remain_on_channel_cb(struct wpa_supplicant *wpa_s,
4840 unsigned int freq)
4841{
4842 wpa_printf(MSG_DEBUG, "P2P: Cancel remain-on-channel callback "
4843 "(p2p_long_listen=%d ms pending_action_tx=%p)",
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08004844 wpa_s->p2p_long_listen, offchannel_pending_action_tx(wpa_s));
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08004845 wpas_p2p_listen_work_done(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004846 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
4847 return;
4848 if (p2p_listen_end(wpa_s->global->p2p, freq) > 0)
4849 return; /* P2P module started a new operation */
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08004850 if (offchannel_pending_action_tx(wpa_s))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004851 return;
4852 if (wpa_s->p2p_long_listen > 0)
4853 wpa_s->p2p_long_listen -= wpa_s->max_remain_on_chan;
4854 if (wpa_s->p2p_long_listen > 0) {
4855 wpa_printf(MSG_DEBUG, "P2P: Continuing long Listen state");
4856 wpas_p2p_listen_start(wpa_s, wpa_s->p2p_long_listen);
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07004857 } else {
4858 /*
4859 * When listen duration is over, stop listen & update p2p_state
4860 * to IDLE.
4861 */
4862 p2p_stop_listen(wpa_s->global->p2p);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004863 }
4864}
4865
4866
4867/**
4868 * wpas_p2p_group_remove - Remove a P2P group
4869 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
4870 * @ifname: Network interface name of the group interface or "*" to remove all
4871 * groups
4872 * Returns: 0 on success, -1 on failure
4873 *
4874 * This function is used to remove a P2P group. This can be used to disconnect
4875 * from a group in which the local end is a P2P Client or to end a P2P Group in
4876 * case the local end is the Group Owner. If a virtual network interface was
4877 * created for this group, that interface will be removed. Otherwise, only the
4878 * configured P2P group network will be removed from the interface.
4879 */
4880int wpas_p2p_group_remove(struct wpa_supplicant *wpa_s, const char *ifname)
4881{
4882 struct wpa_global *global = wpa_s->global;
4883
4884 if (os_strcmp(ifname, "*") == 0) {
4885 struct wpa_supplicant *prev;
4886 wpa_s = global->ifaces;
4887 while (wpa_s) {
4888 prev = wpa_s;
4889 wpa_s = wpa_s->next;
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07004890 if (prev->p2p_group_interface !=
4891 NOT_P2P_GROUP_INTERFACE ||
4892 (prev->current_ssid &&
4893 prev->current_ssid->p2p_group))
4894 wpas_p2p_disconnect(prev);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004895 }
4896 return 0;
4897 }
4898
4899 for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
4900 if (os_strcmp(wpa_s->ifname, ifname) == 0)
4901 break;
4902 }
4903
4904 return wpas_p2p_disconnect(wpa_s);
4905}
4906
4907
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07004908static int wpas_p2p_select_go_freq(struct wpa_supplicant *wpa_s, int freq)
4909{
4910 unsigned int r;
4911
4912 if (freq == 2) {
4913 wpa_printf(MSG_DEBUG, "P2P: Request to start GO on 2.4 GHz "
4914 "band");
4915 if (wpa_s->best_24_freq > 0 &&
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004916 p2p_supported_freq_go(wpa_s->global->p2p,
4917 wpa_s->best_24_freq)) {
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07004918 freq = wpa_s->best_24_freq;
4919 wpa_printf(MSG_DEBUG, "P2P: Use best 2.4 GHz band "
4920 "channel: %d MHz", freq);
4921 } else {
4922 os_get_random((u8 *) &r, sizeof(r));
4923 freq = 2412 + (r % 3) * 25;
4924 wpa_printf(MSG_DEBUG, "P2P: Use random 2.4 GHz band "
4925 "channel: %d MHz", freq);
4926 }
4927 }
4928
4929 if (freq == 5) {
4930 wpa_printf(MSG_DEBUG, "P2P: Request to start GO on 5 GHz "
4931 "band");
4932 if (wpa_s->best_5_freq > 0 &&
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004933 p2p_supported_freq_go(wpa_s->global->p2p,
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07004934 wpa_s->best_5_freq)) {
4935 freq = wpa_s->best_5_freq;
4936 wpa_printf(MSG_DEBUG, "P2P: Use best 5 GHz band "
4937 "channel: %d MHz", freq);
4938 } else {
4939 os_get_random((u8 *) &r, sizeof(r));
4940 freq = 5180 + (r % 4) * 20;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004941 if (!p2p_supported_freq_go(wpa_s->global->p2p, freq)) {
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07004942 wpa_printf(MSG_DEBUG, "P2P: Could not select "
4943 "5 GHz channel for P2P group");
4944 return -1;
4945 }
4946 wpa_printf(MSG_DEBUG, "P2P: Use random 5 GHz band "
4947 "channel: %d MHz", freq);
4948 }
4949 }
4950
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004951 if (freq > 0 && !p2p_supported_freq_go(wpa_s->global->p2p, freq)) {
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07004952 wpa_printf(MSG_DEBUG, "P2P: The forced channel for GO "
4953 "(%u MHz) is not supported for P2P uses",
4954 freq);
4955 return -1;
4956 }
4957
4958 return freq;
4959}
4960
4961
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004962static int wpas_p2p_init_go_params(struct wpa_supplicant *wpa_s,
4963 struct p2p_go_neg_results *params,
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004964 int freq, int ht40, int vht,
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08004965 const struct p2p_channels *channels)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004966{
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07004967 int res, *freqs;
Dmitry Shmidt44c95782013-05-17 09:51:35 -07004968 unsigned int pref_freq;
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07004969 unsigned int num, i;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004970
4971 os_memset(params, 0, sizeof(*params));
4972 params->role_go = 1;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004973 params->ht40 = ht40;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004974 params->vht = vht;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004975 if (freq) {
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08004976 if (!freq_included(channels, freq)) {
4977 wpa_printf(MSG_DEBUG, "P2P: Forced GO freq %d MHz not "
4978 "accepted", freq);
4979 return -1;
4980 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004981 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on forced "
4982 "frequency %d MHz", freq);
4983 params->freq = freq;
4984 } else if (wpa_s->conf->p2p_oper_reg_class == 81 &&
4985 wpa_s->conf->p2p_oper_channel >= 1 &&
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08004986 wpa_s->conf->p2p_oper_channel <= 11 &&
4987 freq_included(channels,
4988 2407 + 5 * wpa_s->conf->p2p_oper_channel)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004989 params->freq = 2407 + 5 * wpa_s->conf->p2p_oper_channel;
4990 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on configured "
4991 "frequency %d MHz", params->freq);
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08004992 } else if ((wpa_s->conf->p2p_oper_reg_class == 115 ||
4993 wpa_s->conf->p2p_oper_reg_class == 116 ||
4994 wpa_s->conf->p2p_oper_reg_class == 117 ||
4995 wpa_s->conf->p2p_oper_reg_class == 124 ||
4996 wpa_s->conf->p2p_oper_reg_class == 126 ||
4997 wpa_s->conf->p2p_oper_reg_class == 127) &&
4998 freq_included(channels,
4999 5000 + 5 * wpa_s->conf->p2p_oper_channel)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005000 params->freq = 5000 + 5 * wpa_s->conf->p2p_oper_channel;
5001 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on configured "
5002 "frequency %d MHz", params->freq);
5003 } else if (wpa_s->conf->p2p_oper_channel == 0 &&
5004 wpa_s->best_overall_freq > 0 &&
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07005005 p2p_supported_freq_go(wpa_s->global->p2p,
5006 wpa_s->best_overall_freq) &&
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08005007 freq_included(channels, wpa_s->best_overall_freq)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005008 params->freq = wpa_s->best_overall_freq;
5009 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on best overall "
5010 "channel %d MHz", params->freq);
5011 } else if (wpa_s->conf->p2p_oper_channel == 0 &&
5012 wpa_s->best_24_freq > 0 &&
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07005013 p2p_supported_freq_go(wpa_s->global->p2p,
5014 wpa_s->best_24_freq) &&
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08005015 freq_included(channels, wpa_s->best_24_freq)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005016 params->freq = wpa_s->best_24_freq;
5017 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on best 2.4 GHz "
5018 "channel %d MHz", params->freq);
5019 } else if (wpa_s->conf->p2p_oper_channel == 0 &&
5020 wpa_s->best_5_freq > 0 &&
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07005021 p2p_supported_freq_go(wpa_s->global->p2p,
5022 wpa_s->best_5_freq) &&
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08005023 freq_included(channels, wpa_s->best_5_freq)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005024 params->freq = wpa_s->best_5_freq;
5025 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on best 5 GHz "
5026 "channel %d MHz", params->freq);
Dmitry Shmidt44c95782013-05-17 09:51:35 -07005027 } else if ((pref_freq = p2p_get_pref_freq(wpa_s->global->p2p,
5028 channels))) {
5029 params->freq = pref_freq;
5030 wpa_printf(MSG_DEBUG, "P2P: Set GO freq %d MHz from preferred "
5031 "channels", params->freq);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005032 } else {
Dmitry Shmidt04949592012-07-19 12:16:46 -07005033 int chan;
5034 for (chan = 0; chan < 11; chan++) {
5035 params->freq = 2412 + chan * 5;
5036 if (!wpas_p2p_disallowed_freq(wpa_s->global,
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08005037 params->freq) &&
5038 freq_included(channels, params->freq))
Dmitry Shmidt04949592012-07-19 12:16:46 -07005039 break;
5040 }
5041 if (chan == 11) {
5042 wpa_printf(MSG_DEBUG, "P2P: No 2.4 GHz channel "
5043 "allowed");
5044 return -1;
5045 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005046 wpa_printf(MSG_DEBUG, "P2P: Set GO freq %d MHz (no preference "
5047 "known)", params->freq);
5048 }
5049
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07005050 freqs = os_calloc(wpa_s->num_multichan_concurrent, sizeof(int));
5051 if (!freqs)
5052 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005053
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07005054 res = wpas_p2p_valid_oper_freqs(wpa_s, freqs,
5055 wpa_s->num_multichan_concurrent);
5056 if (res < 0) {
5057 os_free(freqs);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005058 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005059 }
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07005060 num = res;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005061
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08005062 for (i = 0; i < num; i++) {
5063 if (freq && freqs[i] == freq)
5064 break;
5065 if (!freq && freq_included(channels, freqs[i])) {
5066 wpa_printf(MSG_DEBUG, "P2P: Force GO on a channel we are already using (%u MHz)",
5067 freqs[i]);
5068 params->freq = freqs[i];
5069 break;
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07005070 }
5071 }
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08005072
5073 if (i == num) {
5074 if (wpas_p2p_num_unused_channels(wpa_s) <= 0) {
5075 if (freq)
5076 wpa_printf(MSG_DEBUG, "P2P: Cannot force GO on freq (%u MHz) as all the channels are in use", freq);
5077 else
5078 wpa_printf(MSG_DEBUG, "P2P: Cannot force GO on any of the channels we are already using");
5079 os_free(freqs);
5080 return -1;
5081 } else if (num == 0) {
5082 wpa_printf(MSG_DEBUG, "P2P: Use one of the free channels");
5083 } else {
5084 wpa_printf(MSG_DEBUG, "P2P: Cannot force GO on any of the channels we are already using. Use one of the free channels");
5085 }
5086 }
5087
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07005088 os_free(freqs);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005089 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005090}
5091
5092
5093static struct wpa_supplicant *
5094wpas_p2p_get_group_iface(struct wpa_supplicant *wpa_s, int addr_allocated,
5095 int go)
5096{
5097 struct wpa_supplicant *group_wpa_s;
5098
Dmitry Shmidtaa532512012-09-24 10:35:31 -07005099 if (!wpas_p2p_create_iface(wpa_s)) {
5100 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Use same interface for group "
5101 "operations");
Dmitry Shmidt56052862013-10-04 10:23:25 -07005102 wpa_s->p2p_first_connection_timeout = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005103 return wpa_s;
Dmitry Shmidtaa532512012-09-24 10:35:31 -07005104 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005105
5106 if (wpas_p2p_add_group_interface(wpa_s, go ? WPA_IF_P2P_GO :
Dmitry Shmidtaa532512012-09-24 10:35:31 -07005107 WPA_IF_P2P_CLIENT) < 0) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07005108 wpa_msg_global(wpa_s, MSG_ERROR,
5109 "P2P: Failed to add group interface");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005110 return NULL;
Dmitry Shmidtaa532512012-09-24 10:35:31 -07005111 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005112 group_wpa_s = wpas_p2p_init_group_interface(wpa_s, go);
5113 if (group_wpa_s == NULL) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07005114 wpa_msg_global(wpa_s, MSG_ERROR,
5115 "P2P: Failed to initialize group interface");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005116 wpas_p2p_remove_pending_group_interface(wpa_s);
5117 return NULL;
5118 }
5119
Dmitry Shmidtaa532512012-09-24 10:35:31 -07005120 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Use separate group interface %s",
5121 group_wpa_s->ifname);
Dmitry Shmidt56052862013-10-04 10:23:25 -07005122 group_wpa_s->p2p_first_connection_timeout = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005123 return group_wpa_s;
5124}
5125
5126
5127/**
5128 * wpas_p2p_group_add - Add a new P2P group with local end as Group Owner
5129 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
5130 * @persistent_group: Whether to create a persistent group
5131 * @freq: Frequency for the group or 0 to indicate no hardcoding
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07005132 * @ht40: Start GO with 40 MHz channel width
5133 * @vht: Start GO with VHT support
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005134 * Returns: 0 on success, -1 on failure
5135 *
5136 * This function creates a new P2P group with the local end as the Group Owner,
5137 * i.e., without using Group Owner Negotiation.
5138 */
5139int wpas_p2p_group_add(struct wpa_supplicant *wpa_s, int persistent_group,
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07005140 int freq, int ht40, int vht)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005141{
5142 struct p2p_go_neg_results params;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005143
5144 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
5145 return -1;
5146
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07005147 os_free(wpa_s->global->add_psk);
5148 wpa_s->global->add_psk = NULL;
5149
Dmitry Shmidtdca39792011-09-06 11:17:33 -07005150 /* Make sure we are not running find during connection establishment */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005151 wpa_printf(MSG_DEBUG, "P2P: Stop any on-going P2P FIND");
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08005152 wpas_p2p_stop_find_oper(wpa_s);
Dmitry Shmidtdca39792011-09-06 11:17:33 -07005153
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07005154 freq = wpas_p2p_select_go_freq(wpa_s, freq);
5155 if (freq < 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005156 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005157
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07005158 if (wpas_p2p_init_go_params(wpa_s, &params, freq, ht40, vht, NULL))
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005159 return -1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07005160 if (params.freq &&
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07005161 !p2p_supported_freq_go(wpa_s->global->p2p, params.freq)) {
Dmitry Shmidt04949592012-07-19 12:16:46 -07005162 wpa_printf(MSG_DEBUG, "P2P: The selected channel for GO "
5163 "(%u MHz) is not supported for P2P uses",
5164 params.freq);
5165 return -1;
5166 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005167 p2p_go_params(wpa_s->global->p2p, &params);
5168 params.persistent_group = persistent_group;
5169
5170 wpa_s = wpas_p2p_get_group_iface(wpa_s, 0, 1);
5171 if (wpa_s == NULL)
5172 return -1;
5173 wpas_start_wps_go(wpa_s, &params, 0);
5174
5175 return 0;
5176}
5177
5178
5179static int wpas_start_p2p_client(struct wpa_supplicant *wpa_s,
Dmitry Shmidt15907092014-03-25 10:42:57 -07005180 struct wpa_ssid *params, int addr_allocated,
5181 int freq)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005182{
5183 struct wpa_ssid *ssid;
5184
5185 wpa_s = wpas_p2p_get_group_iface(wpa_s, addr_allocated, 0);
5186 if (wpa_s == NULL)
5187 return -1;
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07005188 wpa_s->p2p_last_4way_hs_fail = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005189
5190 wpa_supplicant_ap_deinit(wpa_s);
5191
5192 ssid = wpa_config_add_network(wpa_s->conf);
5193 if (ssid == NULL)
5194 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005195 wpa_config_set_network_defaults(ssid);
5196 ssid->temporary = 1;
5197 ssid->proto = WPA_PROTO_RSN;
5198 ssid->pairwise_cipher = WPA_CIPHER_CCMP;
5199 ssid->group_cipher = WPA_CIPHER_CCMP;
5200 ssid->key_mgmt = WPA_KEY_MGMT_PSK;
5201 ssid->ssid = os_malloc(params->ssid_len);
5202 if (ssid->ssid == NULL) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005203 wpa_config_remove_network(wpa_s->conf, ssid->id);
5204 return -1;
5205 }
5206 os_memcpy(ssid->ssid, params->ssid, params->ssid_len);
5207 ssid->ssid_len = params->ssid_len;
5208 ssid->p2p_group = 1;
5209 ssid->export_keys = 1;
5210 if (params->psk_set) {
5211 os_memcpy(ssid->psk, params->psk, 32);
5212 ssid->psk_set = 1;
5213 }
5214 if (params->passphrase)
5215 ssid->passphrase = os_strdup(params->passphrase);
5216
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005217 wpa_s->show_group_started = 1;
Dmitry Shmidt15907092014-03-25 10:42:57 -07005218 wpa_s->p2p_in_invitation = 1;
5219 wpa_s->p2p_invite_go_freq = freq;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005220
Dmitry Shmidt15907092014-03-25 10:42:57 -07005221 eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s->parent,
5222 NULL);
5223 eloop_register_timeout(P2P_MAX_INITIAL_CONN_WAIT, 0,
5224 wpas_p2p_group_formation_timeout,
5225 wpa_s->parent, NULL);
Dmitry Shmidtfa3fc4a2013-11-21 13:34:38 -08005226 wpa_supplicant_select_network(wpa_s, ssid);
5227
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005228 return 0;
5229}
5230
5231
5232int wpas_p2p_group_add_persistent(struct wpa_supplicant *wpa_s,
5233 struct wpa_ssid *ssid, int addr_allocated,
Dmitry Shmidt96be6222014-02-13 10:16:51 -08005234 int force_freq, int neg_freq, int ht40,
5235 int vht, const struct p2p_channels *channels,
Dmitry Shmidt56052862013-10-04 10:23:25 -07005236 int connection_timeout)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005237{
5238 struct p2p_go_neg_results params;
Dmitry Shmidt96be6222014-02-13 10:16:51 -08005239 int go = 0, freq;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005240
5241 if (ssid->disabled != 2 || ssid->ssid == NULL)
5242 return -1;
5243
5244 if (wpas_get_p2p_group(wpa_s, ssid->ssid, ssid->ssid_len, &go) &&
5245 go == (ssid->mode == WPAS_MODE_P2P_GO)) {
5246 wpa_printf(MSG_DEBUG, "P2P: Requested persistent group is "
5247 "already running");
5248 return 0;
5249 }
5250
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07005251 os_free(wpa_s->global->add_psk);
5252 wpa_s->global->add_psk = NULL;
5253
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005254 /* Make sure we are not running find during connection establishment */
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08005255 wpas_p2p_stop_find_oper(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005256
Dmitry Shmidt04949592012-07-19 12:16:46 -07005257 wpa_s->p2p_fallback_to_go_neg = 0;
5258
Dmitry Shmidt96be6222014-02-13 10:16:51 -08005259 if (force_freq > 0) {
5260 freq = wpas_p2p_select_go_freq(wpa_s, force_freq);
5261 if (freq < 0)
5262 return -1;
5263 } else {
5264 freq = wpas_p2p_select_go_freq(wpa_s, neg_freq);
5265 if (freq < 0 || (freq > 0 && !freq_included(channels, freq)))
5266 freq = 0;
5267 }
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07005268
Dmitry Shmidt15907092014-03-25 10:42:57 -07005269 if (ssid->mode == WPAS_MODE_INFRA)
5270 return wpas_start_p2p_client(wpa_s, ssid, addr_allocated, freq);
5271
5272 if (ssid->mode != WPAS_MODE_P2P_GO)
5273 return -1;
5274
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07005275 if (wpas_p2p_init_go_params(wpa_s, &params, freq, ht40, vht, channels))
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005276 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005277
5278 params.role_go = 1;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08005279 params.psk_set = ssid->psk_set;
5280 if (params.psk_set)
5281 os_memcpy(params.psk, ssid->psk, sizeof(params.psk));
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08005282 if (ssid->passphrase) {
5283 if (os_strlen(ssid->passphrase) >= sizeof(params.passphrase)) {
5284 wpa_printf(MSG_ERROR, "P2P: Invalid passphrase in "
5285 "persistent group");
5286 return -1;
5287 }
5288 os_strlcpy(params.passphrase, ssid->passphrase,
5289 sizeof(params.passphrase));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005290 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005291 os_memcpy(params.ssid, ssid->ssid, ssid->ssid_len);
5292 params.ssid_len = ssid->ssid_len;
5293 params.persistent_group = 1;
5294
5295 wpa_s = wpas_p2p_get_group_iface(wpa_s, addr_allocated, 1);
5296 if (wpa_s == NULL)
5297 return -1;
5298
Dmitry Shmidt56052862013-10-04 10:23:25 -07005299 wpa_s->p2p_first_connection_timeout = connection_timeout;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005300 wpas_start_wps_go(wpa_s, &params, 0);
5301
5302 return 0;
5303}
5304
5305
5306static void wpas_p2p_ie_update(void *ctx, struct wpabuf *beacon_ies,
5307 struct wpabuf *proberesp_ies)
5308{
5309 struct wpa_supplicant *wpa_s = ctx;
5310 if (wpa_s->ap_iface) {
5311 struct hostapd_data *hapd = wpa_s->ap_iface->bss[0];
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005312 if (!(hapd->conf->p2p & P2P_GROUP_OWNER)) {
5313 wpabuf_free(beacon_ies);
5314 wpabuf_free(proberesp_ies);
5315 return;
5316 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005317 if (beacon_ies) {
5318 wpabuf_free(hapd->p2p_beacon_ie);
5319 hapd->p2p_beacon_ie = beacon_ies;
5320 }
5321 wpabuf_free(hapd->p2p_probe_resp_ie);
5322 hapd->p2p_probe_resp_ie = proberesp_ies;
5323 } else {
5324 wpabuf_free(beacon_ies);
5325 wpabuf_free(proberesp_ies);
5326 }
5327 wpa_supplicant_ap_update_beacon(wpa_s);
5328}
5329
5330
5331static void wpas_p2p_idle_update(void *ctx, int idle)
5332{
5333 struct wpa_supplicant *wpa_s = ctx;
5334 if (!wpa_s->ap_iface)
5335 return;
5336 wpa_printf(MSG_DEBUG, "P2P: GO - group %sidle", idle ? "" : "not ");
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005337 if (idle) {
5338 if (wpa_s->global->p2p_fail_on_wps_complete &&
5339 wpa_s->p2p_in_provisioning) {
5340 wpas_p2p_grpform_fail_after_wps(wpa_s);
5341 return;
5342 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005343 wpas_p2p_set_group_idle_timeout(wpa_s);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005344 } else
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005345 eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL);
5346}
5347
5348
5349struct p2p_group * wpas_p2p_group_init(struct wpa_supplicant *wpa_s,
Dmitry Shmidt04949592012-07-19 12:16:46 -07005350 struct wpa_ssid *ssid)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005351{
5352 struct p2p_group *group;
5353 struct p2p_group_config *cfg;
5354
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005355 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
5356 return NULL;
5357
5358 cfg = os_zalloc(sizeof(*cfg));
5359 if (cfg == NULL)
5360 return NULL;
5361
Dmitry Shmidt04949592012-07-19 12:16:46 -07005362 if (ssid->p2p_persistent_group && wpa_s->conf->persistent_reconnect)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005363 cfg->persistent_group = 2;
Dmitry Shmidt04949592012-07-19 12:16:46 -07005364 else if (ssid->p2p_persistent_group)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005365 cfg->persistent_group = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005366 os_memcpy(cfg->interface_addr, wpa_s->own_addr, ETH_ALEN);
5367 if (wpa_s->max_stations &&
5368 wpa_s->max_stations < wpa_s->conf->max_num_sta)
5369 cfg->max_clients = wpa_s->max_stations;
5370 else
5371 cfg->max_clients = wpa_s->conf->max_num_sta;
Dmitry Shmidt04949592012-07-19 12:16:46 -07005372 os_memcpy(cfg->ssid, ssid->ssid, ssid->ssid_len);
5373 cfg->ssid_len = ssid->ssid_len;
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08005374 cfg->freq = ssid->frequency;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005375 cfg->cb_ctx = wpa_s;
5376 cfg->ie_update = wpas_p2p_ie_update;
5377 cfg->idle_update = wpas_p2p_idle_update;
5378
5379 group = p2p_group_init(wpa_s->global->p2p, cfg);
5380 if (group == NULL)
5381 os_free(cfg);
Dmitry Shmidt04949592012-07-19 12:16:46 -07005382 if (ssid->mode != WPAS_MODE_P2P_GROUP_FORMATION)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005383 p2p_group_notif_formation_done(group);
5384 wpa_s->p2p_group = group;
5385 return group;
5386}
5387
5388
5389void wpas_p2p_wps_success(struct wpa_supplicant *wpa_s, const u8 *peer_addr,
5390 int registrar)
5391{
Dmitry Shmidt04949592012-07-19 12:16:46 -07005392 struct wpa_ssid *ssid = wpa_s->current_ssid;
5393
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005394 if (!wpa_s->p2p_in_provisioning) {
5395 wpa_printf(MSG_DEBUG, "P2P: Ignore WPS success event - P2P "
5396 "provisioning not in progress");
5397 return;
5398 }
5399
Dmitry Shmidt04949592012-07-19 12:16:46 -07005400 if (ssid && ssid->mode == WPAS_MODE_INFRA) {
5401 u8 go_dev_addr[ETH_ALEN];
5402 os_memcpy(go_dev_addr, wpa_s->bssid, ETH_ALEN);
5403 wpas_p2p_persistent_group(wpa_s, go_dev_addr, ssid->ssid,
5404 ssid->ssid_len);
5405 /* Clear any stored provisioning info */
5406 p2p_clear_provisioning_info(wpa_s->global->p2p, go_dev_addr);
5407 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005408
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005409 eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s->parent,
5410 NULL);
Dmitry Shmidt56052862013-10-04 10:23:25 -07005411 wpa_s->p2p_go_group_formation_completed = 1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07005412 if (ssid && ssid->mode == WPAS_MODE_INFRA) {
5413 /*
5414 * Use a separate timeout for initial data connection to
5415 * complete to allow the group to be removed automatically if
5416 * something goes wrong in this step before the P2P group idle
5417 * timeout mechanism is taken into use.
5418 */
Dmitry Shmidt56052862013-10-04 10:23:25 -07005419 wpa_dbg(wpa_s, MSG_DEBUG,
5420 "P2P: Re-start group formation timeout (%d seconds) as client for initial connection",
5421 P2P_MAX_INITIAL_CONN_WAIT);
Dmitry Shmidt04949592012-07-19 12:16:46 -07005422 eloop_register_timeout(P2P_MAX_INITIAL_CONN_WAIT, 0,
5423 wpas_p2p_group_formation_timeout,
5424 wpa_s->parent, NULL);
Dmitry Shmidt56052862013-10-04 10:23:25 -07005425 } else if (ssid) {
5426 /*
5427 * Use a separate timeout for initial data connection to
5428 * complete to allow the group to be removed automatically if
5429 * the client does not complete data connection successfully.
5430 */
5431 wpa_dbg(wpa_s, MSG_DEBUG,
5432 "P2P: Re-start group formation timeout (%d seconds) as GO for initial connection",
5433 P2P_MAX_INITIAL_CONN_WAIT_GO);
5434 eloop_register_timeout(P2P_MAX_INITIAL_CONN_WAIT_GO, 0,
5435 wpas_p2p_group_formation_timeout,
5436 wpa_s->parent, NULL);
5437 /*
5438 * Complete group formation on first successful data connection
5439 */
5440 wpa_s->p2p_go_group_formation_completed = 0;
Dmitry Shmidt04949592012-07-19 12:16:46 -07005441 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005442 if (wpa_s->global->p2p)
5443 p2p_wps_success_cb(wpa_s->global->p2p, peer_addr);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005444 wpas_group_formation_completed(wpa_s, 1);
5445}
5446
5447
Jouni Malinen75ecf522011-06-27 15:19:46 -07005448void wpas_p2p_wps_failed(struct wpa_supplicant *wpa_s,
5449 struct wps_event_fail *fail)
5450{
5451 if (!wpa_s->p2p_in_provisioning) {
5452 wpa_printf(MSG_DEBUG, "P2P: Ignore WPS fail event - P2P "
5453 "provisioning not in progress");
5454 return;
5455 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005456
5457 if (wpa_s->go_params) {
5458 p2p_clear_provisioning_info(
5459 wpa_s->global->p2p,
Dmitry Shmidt04949592012-07-19 12:16:46 -07005460 wpa_s->go_params->peer_device_addr);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005461 }
5462
Jouni Malinen75ecf522011-06-27 15:19:46 -07005463 wpas_notify_p2p_wps_failed(wpa_s, fail);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005464
5465 if (wpa_s == wpa_s->global->p2p_group_formation) {
5466 /*
5467 * Allow some time for the failed WPS negotiation exchange to
5468 * complete, but remove the group since group formation cannot
5469 * succeed after provisioning failure.
5470 */
5471 wpa_printf(MSG_DEBUG, "P2P: WPS step failed during group formation - reject connection from timeout");
5472 wpa_s->global->p2p_fail_on_wps_complete = 1;
5473 eloop_deplete_timeout(0, 50000,
5474 wpas_p2p_group_formation_timeout,
5475 wpa_s->parent, NULL);
5476 }
5477}
5478
5479
5480int wpas_p2p_wps_eapol_cb(struct wpa_supplicant *wpa_s)
5481{
5482 if (!wpa_s->global->p2p_fail_on_wps_complete ||
5483 !wpa_s->p2p_in_provisioning)
5484 return 0;
5485
5486 wpas_p2p_grpform_fail_after_wps(wpa_s);
5487
5488 return 1;
Jouni Malinen75ecf522011-06-27 15:19:46 -07005489}
5490
5491
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005492int wpas_p2p_prov_disc(struct wpa_supplicant *wpa_s, const u8 *peer_addr,
Dmitry Shmidt04949592012-07-19 12:16:46 -07005493 const char *config_method,
5494 enum wpas_p2p_prov_disc_use use)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005495{
5496 u16 config_methods;
5497
Dmitry Shmidt04949592012-07-19 12:16:46 -07005498 wpa_s->p2p_fallback_to_go_neg = 0;
5499 wpa_s->pending_pd_use = NORMAL_PD;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005500 if (os_strncmp(config_method, "display", 7) == 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005501 config_methods = WPS_CONFIG_DISPLAY;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005502 else if (os_strncmp(config_method, "keypad", 6) == 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005503 config_methods = WPS_CONFIG_KEYPAD;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005504 else if (os_strncmp(config_method, "pbc", 3) == 0 ||
5505 os_strncmp(config_method, "pushbutton", 10) == 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005506 config_methods = WPS_CONFIG_PUSHBUTTON;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005507 else {
5508 wpa_printf(MSG_DEBUG, "P2P: Unknown config method");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005509 return -1;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005510 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005511
Dmitry Shmidt04949592012-07-19 12:16:46 -07005512 if (use == WPAS_P2P_PD_AUTO) {
5513 os_memcpy(wpa_s->pending_join_dev_addr, peer_addr, ETH_ALEN);
5514 wpa_s->pending_pd_config_methods = config_methods;
5515 wpa_s->p2p_auto_pd = 1;
5516 wpa_s->p2p_auto_join = 0;
5517 wpa_s->pending_pd_before_join = 0;
5518 wpa_s->auto_pd_scan_retry = 0;
5519 wpas_p2p_stop_find(wpa_s);
5520 wpa_s->p2p_join_scan_count = 0;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005521 os_get_reltime(&wpa_s->p2p_auto_started);
Dmitry Shmidt04949592012-07-19 12:16:46 -07005522 wpa_printf(MSG_DEBUG, "P2P: Auto PD started at %ld.%06ld",
5523 wpa_s->p2p_auto_started.sec,
5524 wpa_s->p2p_auto_started.usec);
5525 wpas_p2p_join_scan(wpa_s, NULL);
5526 return 0;
5527 }
5528
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005529 if (wpa_s->global->p2p == NULL || wpa_s->global->p2p_disabled)
5530 return -1;
5531
5532 return p2p_prov_disc_req(wpa_s->global->p2p, peer_addr,
Dmitry Shmidt04949592012-07-19 12:16:46 -07005533 config_methods, use == WPAS_P2P_PD_FOR_JOIN,
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08005534 0, 1);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005535}
5536
5537
5538int wpas_p2p_scan_result_text(const u8 *ies, size_t ies_len, char *buf,
5539 char *end)
5540{
5541 return p2p_scan_result_text(ies, ies_len, buf, end);
5542}
5543
5544
5545static void wpas_p2p_clear_pending_action_tx(struct wpa_supplicant *wpa_s)
5546{
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08005547 if (!offchannel_pending_action_tx(wpa_s))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005548 return;
5549
5550 wpa_printf(MSG_DEBUG, "P2P: Drop pending Action TX due to new "
5551 "operation request");
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08005552 offchannel_clear_pending_action_tx(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005553}
5554
5555
5556int wpas_p2p_find(struct wpa_supplicant *wpa_s, unsigned int timeout,
5557 enum p2p_discovery_type type,
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08005558 unsigned int num_req_dev_types, const u8 *req_dev_types,
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005559 const u8 *dev_id, unsigned int search_delay)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005560{
5561 wpas_p2p_clear_pending_action_tx(wpa_s);
5562 wpa_s->p2p_long_listen = 0;
5563
Dmitry Shmidt04949592012-07-19 12:16:46 -07005564 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL ||
5565 wpa_s->p2p_in_provisioning)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005566 return -1;
5567
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08005568 wpa_supplicant_cancel_sched_scan(wpa_s);
5569
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005570 return p2p_find(wpa_s->global->p2p, timeout, type,
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005571 num_req_dev_types, req_dev_types, dev_id,
5572 search_delay);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005573}
5574
5575
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08005576static int wpas_p2p_stop_find_oper(struct wpa_supplicant *wpa_s)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005577{
5578 wpas_p2p_clear_pending_action_tx(wpa_s);
5579 wpa_s->p2p_long_listen = 0;
5580 eloop_cancel_timeout(wpas_p2p_long_listen_timeout, wpa_s, NULL);
5581 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005582
5583 if (wpa_s->global->p2p)
5584 p2p_stop_find(wpa_s->global->p2p);
5585
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08005586 return 0;
5587}
5588
5589
5590void wpas_p2p_stop_find(struct wpa_supplicant *wpa_s)
5591{
5592 if (wpas_p2p_stop_find_oper(wpa_s) > 0)
5593 return;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005594 wpas_p2p_remove_pending_group_interface(wpa_s);
5595}
5596
5597
5598static void wpas_p2p_long_listen_timeout(void *eloop_ctx, void *timeout_ctx)
5599{
5600 struct wpa_supplicant *wpa_s = eloop_ctx;
5601 wpa_s->p2p_long_listen = 0;
5602}
5603
5604
5605int wpas_p2p_listen(struct wpa_supplicant *wpa_s, unsigned int timeout)
5606{
5607 int res;
5608
5609 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
5610 return -1;
5611
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08005612 wpa_supplicant_cancel_sched_scan(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005613 wpas_p2p_clear_pending_action_tx(wpa_s);
5614
5615 if (timeout == 0) {
5616 /*
5617 * This is a request for unlimited Listen state. However, at
5618 * least for now, this is mapped to a Listen state for one
5619 * hour.
5620 */
5621 timeout = 3600;
5622 }
5623 eloop_cancel_timeout(wpas_p2p_long_listen_timeout, wpa_s, NULL);
5624 wpa_s->p2p_long_listen = 0;
5625
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005626 /*
5627 * Stop previous find/listen operation to avoid trying to request a new
5628 * remain-on-channel operation while the driver is still running the
5629 * previous one.
5630 */
5631 if (wpa_s->global->p2p)
5632 p2p_stop_find(wpa_s->global->p2p);
5633
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005634 res = wpas_p2p_listen_start(wpa_s, timeout * 1000);
5635 if (res == 0 && timeout * 1000 > wpa_s->max_remain_on_chan) {
5636 wpa_s->p2p_long_listen = timeout * 1000;
5637 eloop_register_timeout(timeout, 0,
5638 wpas_p2p_long_listen_timeout,
5639 wpa_s, NULL);
5640 }
5641
5642 return res;
5643}
5644
5645
5646int wpas_p2p_assoc_req_ie(struct wpa_supplicant *wpa_s, struct wpa_bss *bss,
5647 u8 *buf, size_t len, int p2p_group)
5648{
5649 struct wpabuf *p2p_ie;
5650 int ret;
5651
5652 if (wpa_s->global->p2p_disabled)
5653 return -1;
5654 if (wpa_s->global->p2p == NULL)
5655 return -1;
5656 if (bss == NULL)
5657 return -1;
5658
5659 p2p_ie = wpa_bss_get_vendor_ie_multi(bss, P2P_IE_VENDOR_TYPE);
5660 ret = p2p_assoc_req_ie(wpa_s->global->p2p, bss->bssid, buf, len,
5661 p2p_group, p2p_ie);
5662 wpabuf_free(p2p_ie);
5663
5664 return ret;
5665}
5666
5667
5668int wpas_p2p_probe_req_rx(struct wpa_supplicant *wpa_s, const u8 *addr,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005669 const u8 *dst, const u8 *bssid,
Dmitry Shmidt04949592012-07-19 12:16:46 -07005670 const u8 *ie, size_t ie_len, int ssi_signal)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005671{
5672 if (wpa_s->global->p2p_disabled)
5673 return 0;
5674 if (wpa_s->global->p2p == NULL)
5675 return 0;
5676
Dmitry Shmidt04949592012-07-19 12:16:46 -07005677 switch (p2p_probe_req_rx(wpa_s->global->p2p, addr, dst, bssid,
5678 ie, ie_len)) {
5679 case P2P_PREQ_NOT_P2P:
5680 wpas_notify_preq(wpa_s, addr, dst, bssid, ie, ie_len,
5681 ssi_signal);
5682 /* fall through */
5683 case P2P_PREQ_MALFORMED:
5684 case P2P_PREQ_NOT_LISTEN:
5685 case P2P_PREQ_NOT_PROCESSED:
5686 default: /* make gcc happy */
5687 return 0;
5688 case P2P_PREQ_PROCESSED:
5689 return 1;
5690 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005691}
5692
5693
5694void wpas_p2p_rx_action(struct wpa_supplicant *wpa_s, const u8 *da,
5695 const u8 *sa, const u8 *bssid,
5696 u8 category, const u8 *data, size_t len, int freq)
5697{
5698 if (wpa_s->global->p2p_disabled)
5699 return;
5700 if (wpa_s->global->p2p == NULL)
5701 return;
5702
5703 p2p_rx_action(wpa_s->global->p2p, da, sa, bssid, category, data, len,
5704 freq);
5705}
5706
5707
5708void wpas_p2p_scan_ie(struct wpa_supplicant *wpa_s, struct wpabuf *ies)
5709{
5710 if (wpa_s->global->p2p_disabled)
5711 return;
5712 if (wpa_s->global->p2p == NULL)
5713 return;
5714
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08005715 p2p_scan_ie(wpa_s->global->p2p, ies, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005716}
5717
5718
5719void wpas_p2p_group_deinit(struct wpa_supplicant *wpa_s)
5720{
5721 p2p_group_deinit(wpa_s->p2p_group);
5722 wpa_s->p2p_group = NULL;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005723
5724 wpa_s->ap_configured_cb = NULL;
5725 wpa_s->ap_configured_cb_ctx = NULL;
5726 wpa_s->ap_configured_cb_data = NULL;
5727 wpa_s->connect_without_scan = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005728}
5729
5730
5731int wpas_p2p_reject(struct wpa_supplicant *wpa_s, const u8 *addr)
5732{
5733 wpa_s->p2p_long_listen = 0;
5734
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005735 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
5736 return -1;
5737
5738 return p2p_reject(wpa_s->global->p2p, addr);
5739}
5740
5741
5742/* Invite to reinvoke a persistent group */
5743int wpas_p2p_invite(struct wpa_supplicant *wpa_s, const u8 *peer_addr,
Jouni Malinen31be0a42012-08-31 21:20:51 +03005744 struct wpa_ssid *ssid, const u8 *go_dev_addr, int freq,
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07005745 int ht40, int vht, int pref_freq)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005746{
5747 enum p2p_invite_role role;
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07005748 u8 *bssid = NULL;
5749 int force_freq = 0;
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08005750 int res;
Dmitry Shmidta0d265f2013-11-19 13:13:41 -08005751 int no_pref_freq_given = pref_freq == 0;
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08005752
Dmitry Shmidt700a1372013-03-15 14:14:44 -07005753 wpa_s->global->p2p_invite_group = NULL;
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08005754 if (peer_addr)
5755 os_memcpy(wpa_s->p2p_auth_invite, peer_addr, ETH_ALEN);
5756 else
5757 os_memset(wpa_s->p2p_auth_invite, 0, ETH_ALEN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005758
Jouni Malinen31be0a42012-08-31 21:20:51 +03005759 wpa_s->p2p_persistent_go_freq = freq;
5760 wpa_s->p2p_go_ht40 = !!ht40;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005761 if (ssid->mode == WPAS_MODE_P2P_GO) {
5762 role = P2P_INVITE_ROLE_GO;
5763 if (peer_addr == NULL) {
5764 wpa_printf(MSG_DEBUG, "P2P: Missing peer "
5765 "address in invitation command");
5766 return -1;
5767 }
5768 if (wpas_p2p_create_iface(wpa_s)) {
5769 if (wpas_p2p_add_group_interface(wpa_s,
5770 WPA_IF_P2P_GO) < 0) {
5771 wpa_printf(MSG_ERROR, "P2P: Failed to "
5772 "allocate a new interface for the "
5773 "group");
5774 return -1;
5775 }
5776 bssid = wpa_s->pending_interface_addr;
5777 } else
5778 bssid = wpa_s->own_addr;
5779 } else {
5780 role = P2P_INVITE_ROLE_CLIENT;
5781 peer_addr = ssid->bssid;
5782 }
5783 wpa_s->pending_invite_ssid_id = ssid->id;
5784
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07005785 res = wpas_p2p_setup_freqs(wpa_s, freq, &force_freq, &pref_freq,
5786 role == P2P_INVITE_ROLE_GO);
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08005787 if (res)
5788 return res;
Irfan Sheriffaf84a572012-09-22 16:59:30 -07005789
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005790 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
5791 return -1;
5792
Dmitry Shmidta0d265f2013-11-19 13:13:41 -08005793 if (wpa_s->parent->conf->p2p_ignore_shared_freq &&
5794 no_pref_freq_given && pref_freq > 0 &&
5795 wpa_s->num_multichan_concurrent > 1 &&
5796 wpas_p2p_num_unused_channels(wpa_s) > 0) {
5797 wpa_printf(MSG_DEBUG, "P2P: Ignore own channel preference %d MHz for invitation due to p2p_ignore_shared_freq=1 configuration",
5798 pref_freq);
5799 pref_freq = 0;
5800 }
5801
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005802 return p2p_invite(wpa_s->global->p2p, peer_addr, role, bssid,
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08005803 ssid->ssid, ssid->ssid_len, force_freq, go_dev_addr,
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08005804 1, pref_freq, -1);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005805}
5806
5807
5808/* Invite to join an active group */
5809int wpas_p2p_invite_group(struct wpa_supplicant *wpa_s, const char *ifname,
5810 const u8 *peer_addr, const u8 *go_dev_addr)
5811{
5812 struct wpa_global *global = wpa_s->global;
5813 enum p2p_invite_role role;
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07005814 u8 *bssid = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005815 struct wpa_ssid *ssid;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08005816 int persistent;
Dmitry Shmidt051af732013-10-22 13:52:46 -07005817 int freq = 0, force_freq = 0, pref_freq = 0;
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08005818 int res;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005819
Jouni Malinen31be0a42012-08-31 21:20:51 +03005820 wpa_s->p2p_persistent_go_freq = 0;
5821 wpa_s->p2p_go_ht40 = 0;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07005822 wpa_s->p2p_go_vht = 0;
Jouni Malinen31be0a42012-08-31 21:20:51 +03005823
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005824 for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
5825 if (os_strcmp(wpa_s->ifname, ifname) == 0)
5826 break;
5827 }
5828 if (wpa_s == NULL) {
5829 wpa_printf(MSG_DEBUG, "P2P: Interface '%s' not found", ifname);
5830 return -1;
5831 }
5832
5833 ssid = wpa_s->current_ssid;
5834 if (ssid == NULL) {
5835 wpa_printf(MSG_DEBUG, "P2P: No current SSID to use for "
5836 "invitation");
5837 return -1;
5838 }
5839
Dmitry Shmidt700a1372013-03-15 14:14:44 -07005840 wpa_s->global->p2p_invite_group = wpa_s;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08005841 persistent = ssid->p2p_persistent_group &&
5842 wpas_p2p_get_persistent(wpa_s->parent, peer_addr,
5843 ssid->ssid, ssid->ssid_len);
5844
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005845 if (ssid->mode == WPAS_MODE_P2P_GO) {
5846 role = P2P_INVITE_ROLE_ACTIVE_GO;
5847 bssid = wpa_s->own_addr;
5848 if (go_dev_addr == NULL)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005849 go_dev_addr = wpa_s->global->p2p_dev_addr;
Dmitry Shmidt051af732013-10-22 13:52:46 -07005850 freq = ssid->frequency;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005851 } else {
5852 role = P2P_INVITE_ROLE_CLIENT;
5853 if (wpa_s->wpa_state < WPA_ASSOCIATED) {
5854 wpa_printf(MSG_DEBUG, "P2P: Not associated - cannot "
5855 "invite to current group");
5856 return -1;
5857 }
5858 bssid = wpa_s->bssid;
5859 if (go_dev_addr == NULL &&
5860 !is_zero_ether_addr(wpa_s->go_dev_addr))
5861 go_dev_addr = wpa_s->go_dev_addr;
Dmitry Shmidt051af732013-10-22 13:52:46 -07005862 freq = wpa_s->current_bss ? wpa_s->current_bss->freq :
5863 (int) wpa_s->assoc_freq;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005864 }
5865 wpa_s->parent->pending_invite_ssid_id = -1;
5866
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005867 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
5868 return -1;
5869
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07005870 res = wpas_p2p_setup_freqs(wpa_s, freq, &force_freq, &pref_freq,
5871 role == P2P_INVITE_ROLE_ACTIVE_GO);
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08005872 if (res)
5873 return res;
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07005874 wpas_p2p_set_own_freq_preference(wpa_s, force_freq);
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08005875
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005876 return p2p_invite(wpa_s->global->p2p, peer_addr, role, bssid,
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08005877 ssid->ssid, ssid->ssid_len, force_freq,
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08005878 go_dev_addr, persistent, pref_freq, -1);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005879}
5880
5881
5882void wpas_p2p_completed(struct wpa_supplicant *wpa_s)
5883{
5884 struct wpa_ssid *ssid = wpa_s->current_ssid;
5885 const char *ssid_txt;
5886 u8 go_dev_addr[ETH_ALEN];
Jouni Malinen75ecf522011-06-27 15:19:46 -07005887 int network_id = -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005888 int persistent;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005889 int freq;
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08005890 u8 ip[3 * 4];
5891 char ip_addr[100];
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005892
Dmitry Shmidt04949592012-07-19 12:16:46 -07005893 if (ssid == NULL || ssid->mode != WPAS_MODE_P2P_GROUP_FORMATION) {
5894 eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
5895 wpa_s->parent, NULL);
5896 }
5897
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005898 if (!wpa_s->show_group_started || !ssid)
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005899 return;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005900
5901 wpa_s->show_group_started = 0;
5902
5903 ssid_txt = wpa_ssid_txt(ssid->ssid, ssid->ssid_len);
5904 os_memset(go_dev_addr, 0, ETH_ALEN);
5905 if (ssid->bssid_set)
5906 os_memcpy(go_dev_addr, ssid->bssid, ETH_ALEN);
5907 persistent = wpas_p2p_persistent_group(wpa_s, go_dev_addr, ssid->ssid,
5908 ssid->ssid_len);
5909 os_memcpy(wpa_s->go_dev_addr, go_dev_addr, ETH_ALEN);
5910
5911 if (wpa_s->global->p2p_group_formation == wpa_s)
5912 wpa_s->global->p2p_group_formation = NULL;
5913
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005914 freq = wpa_s->current_bss ? wpa_s->current_bss->freq :
5915 (int) wpa_s->assoc_freq;
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08005916
5917 ip_addr[0] = '\0';
5918 if (wpa_sm_get_p2p_ip_addr(wpa_s->wpa, ip) == 0) {
5919 os_snprintf(ip_addr, sizeof(ip_addr), " ip_addr=%u.%u.%u.%u "
5920 "ip_mask=%u.%u.%u.%u go_ip_addr=%u.%u.%u.%u",
5921 ip[0], ip[1], ip[2], ip[3],
5922 ip[4], ip[5], ip[6], ip[7],
5923 ip[8], ip[9], ip[10], ip[11]);
5924 }
5925
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005926 if (ssid->passphrase == NULL && ssid->psk_set) {
5927 char psk[65];
5928 wpa_snprintf_hex(psk, sizeof(psk), ssid->psk, 32);
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07005929 wpa_msg_global(wpa_s->parent, MSG_INFO, P2P_EVENT_GROUP_STARTED
5930 "%s client ssid=\"%s\" freq=%d psk=%s "
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08005931 "go_dev_addr=" MACSTR "%s%s",
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07005932 wpa_s->ifname, ssid_txt, freq, psk,
5933 MAC2STR(go_dev_addr),
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08005934 persistent ? " [PERSISTENT]" : "", ip_addr);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005935 } else {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07005936 wpa_msg_global(wpa_s->parent, MSG_INFO, P2P_EVENT_GROUP_STARTED
5937 "%s client ssid=\"%s\" freq=%d "
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08005938 "passphrase=\"%s\" go_dev_addr=" MACSTR "%s%s",
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07005939 wpa_s->ifname, ssid_txt, freq,
5940 ssid->passphrase ? ssid->passphrase : "",
5941 MAC2STR(go_dev_addr),
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08005942 persistent ? " [PERSISTENT]" : "", ip_addr);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005943 }
5944
5945 if (persistent)
Jouni Malinen75ecf522011-06-27 15:19:46 -07005946 network_id = wpas_p2p_store_persistent_group(wpa_s->parent,
5947 ssid, go_dev_addr);
5948 if (network_id < 0)
5949 network_id = ssid->id;
5950 wpas_notify_p2p_group_started(wpa_s, ssid, network_id, 1);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005951}
5952
5953
5954int wpas_p2p_presence_req(struct wpa_supplicant *wpa_s, u32 duration1,
5955 u32 interval1, u32 duration2, u32 interval2)
5956{
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005957 int ret;
5958
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005959 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
5960 return -1;
5961
5962 if (wpa_s->wpa_state < WPA_ASSOCIATED ||
5963 wpa_s->current_ssid == NULL ||
5964 wpa_s->current_ssid->mode != WPAS_MODE_INFRA)
5965 return -1;
5966
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005967 ret = p2p_presence_req(wpa_s->global->p2p, wpa_s->bssid,
5968 wpa_s->own_addr, wpa_s->assoc_freq,
5969 duration1, interval1, duration2, interval2);
5970 if (ret == 0)
5971 wpa_s->waiting_presence_resp = 1;
5972
5973 return ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005974}
5975
5976
5977int wpas_p2p_ext_listen(struct wpa_supplicant *wpa_s, unsigned int period,
5978 unsigned int interval)
5979{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005980 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
5981 return -1;
5982
5983 return p2p_ext_listen(wpa_s->global->p2p, period, interval);
5984}
5985
5986
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005987static int wpas_p2p_is_client(struct wpa_supplicant *wpa_s)
5988{
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08005989 if (wpa_s->current_ssid == NULL) {
5990 /*
5991 * current_ssid can be cleared when P2P client interface gets
5992 * disconnected, so assume this interface was used as P2P
5993 * client.
5994 */
5995 return 1;
5996 }
5997 return wpa_s->current_ssid->p2p_group &&
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005998 wpa_s->current_ssid->mode == WPAS_MODE_INFRA;
5999}
6000
6001
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006002static void wpas_p2p_group_idle_timeout(void *eloop_ctx, void *timeout_ctx)
6003{
6004 struct wpa_supplicant *wpa_s = eloop_ctx;
6005
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006006 if (wpa_s->conf->p2p_group_idle == 0 && !wpas_p2p_is_client(wpa_s)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006007 wpa_printf(MSG_DEBUG, "P2P: Ignore group idle timeout - "
6008 "disabled");
6009 return;
6010 }
6011
Dmitry Shmidt04949592012-07-19 12:16:46 -07006012 wpa_printf(MSG_DEBUG, "P2P: Group idle timeout reached - terminate "
6013 "group");
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07006014 wpas_p2p_group_delete(wpa_s, P2P_GROUP_REMOVAL_IDLE_TIMEOUT);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006015}
6016
6017
6018static void wpas_p2p_set_group_idle_timeout(struct wpa_supplicant *wpa_s)
6019{
Dmitry Shmidt04949592012-07-19 12:16:46 -07006020 int timeout;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006021
Dmitry Shmidt04949592012-07-19 12:16:46 -07006022 if (eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL) > 0)
6023 wpa_printf(MSG_DEBUG, "P2P: Cancelled P2P group idle timeout");
6024
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006025 if (wpa_s->current_ssid == NULL || !wpa_s->current_ssid->p2p_group)
6026 return;
6027
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006028 timeout = wpa_s->conf->p2p_group_idle;
6029 if (wpa_s->current_ssid->mode == WPAS_MODE_INFRA &&
6030 (timeout == 0 || timeout > P2P_MAX_CLIENT_IDLE))
6031 timeout = P2P_MAX_CLIENT_IDLE;
6032
6033 if (timeout == 0)
6034 return;
6035
Dmitry Shmidt04949592012-07-19 12:16:46 -07006036 if (timeout < 0) {
6037 if (wpa_s->current_ssid->mode == WPAS_MODE_INFRA)
6038 timeout = 0; /* special client mode no-timeout */
6039 else
6040 return;
6041 }
6042
6043 if (wpa_s->p2p_in_provisioning) {
6044 /*
6045 * Use the normal group formation timeout during the
6046 * provisioning phase to avoid terminating this process too
6047 * early due to group idle timeout.
6048 */
6049 wpa_printf(MSG_DEBUG, "P2P: Do not use P2P group idle timeout "
6050 "during provisioning");
6051 return;
6052 }
Deepthi Gowri9e4e8ac2013-04-16 11:57:05 +05306053
Dmitry Shmidt04949592012-07-19 12:16:46 -07006054 if (wpa_s->show_group_started) {
6055 /*
6056 * Use the normal group formation timeout between the end of
6057 * the provisioning phase and completion of 4-way handshake to
6058 * avoid terminating this process too early due to group idle
6059 * timeout.
6060 */
6061 wpa_printf(MSG_DEBUG, "P2P: Do not use P2P group idle timeout "
6062 "while waiting for initial 4-way handshake to "
6063 "complete");
6064 return;
6065 }
6066
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006067 wpa_printf(MSG_DEBUG, "P2P: Set P2P group idle timeout to %u seconds",
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006068 timeout);
6069 eloop_register_timeout(timeout, 0, wpas_p2p_group_idle_timeout,
6070 wpa_s, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006071}
6072
6073
Jouni Malinen2b89da82012-08-31 22:04:41 +03006074/* Returns 1 if the interface was removed */
6075int wpas_p2p_deauth_notif(struct wpa_supplicant *wpa_s, const u8 *bssid,
6076 u16 reason_code, const u8 *ie, size_t ie_len,
6077 int locally_generated)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006078{
6079 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
Jouni Malinen2b89da82012-08-31 22:04:41 +03006080 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006081
Dmitry Shmidt04949592012-07-19 12:16:46 -07006082 if (!locally_generated)
6083 p2p_deauth_notif(wpa_s->global->p2p, bssid, reason_code, ie,
6084 ie_len);
6085
6086 if (reason_code == WLAN_REASON_DEAUTH_LEAVING && !locally_generated &&
6087 wpa_s->current_ssid &&
6088 wpa_s->current_ssid->p2p_group &&
6089 wpa_s->current_ssid->mode == WPAS_MODE_INFRA) {
6090 wpa_printf(MSG_DEBUG, "P2P: GO indicated that the P2P Group "
6091 "session is ending");
Jouni Malinen2b89da82012-08-31 22:04:41 +03006092 if (wpas_p2p_group_delete(wpa_s,
6093 P2P_GROUP_REMOVAL_GO_ENDING_SESSION)
6094 > 0)
6095 return 1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07006096 }
Jouni Malinen2b89da82012-08-31 22:04:41 +03006097
6098 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006099}
6100
6101
6102void wpas_p2p_disassoc_notif(struct wpa_supplicant *wpa_s, const u8 *bssid,
Dmitry Shmidt04949592012-07-19 12:16:46 -07006103 u16 reason_code, const u8 *ie, size_t ie_len,
6104 int locally_generated)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006105{
6106 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
6107 return;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006108
Dmitry Shmidt04949592012-07-19 12:16:46 -07006109 if (!locally_generated)
6110 p2p_disassoc_notif(wpa_s->global->p2p, bssid, reason_code, ie,
6111 ie_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006112}
6113
6114
6115void wpas_p2p_update_config(struct wpa_supplicant *wpa_s)
6116{
6117 struct p2p_data *p2p = wpa_s->global->p2p;
6118
6119 if (p2p == NULL)
6120 return;
6121
6122 if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_CAPABLE))
6123 return;
6124
6125 if (wpa_s->conf->changed_parameters & CFG_CHANGED_DEVICE_NAME)
6126 p2p_set_dev_name(p2p, wpa_s->conf->device_name);
6127
6128 if (wpa_s->conf->changed_parameters & CFG_CHANGED_DEVICE_TYPE)
6129 p2p_set_pri_dev_type(p2p, wpa_s->conf->device_type);
6130
6131 if (wpa_s->wps &&
6132 (wpa_s->conf->changed_parameters & CFG_CHANGED_CONFIG_METHODS))
6133 p2p_set_config_methods(p2p, wpa_s->wps->config_methods);
6134
6135 if (wpa_s->wps && (wpa_s->conf->changed_parameters & CFG_CHANGED_UUID))
6136 p2p_set_uuid(p2p, wpa_s->wps->uuid);
6137
6138 if (wpa_s->conf->changed_parameters & CFG_CHANGED_WPS_STRING) {
6139 p2p_set_manufacturer(p2p, wpa_s->conf->manufacturer);
6140 p2p_set_model_name(p2p, wpa_s->conf->model_name);
6141 p2p_set_model_number(p2p, wpa_s->conf->model_number);
6142 p2p_set_serial_number(p2p, wpa_s->conf->serial_number);
6143 }
6144
6145 if (wpa_s->conf->changed_parameters & CFG_CHANGED_SEC_DEVICE_TYPE)
6146 p2p_set_sec_dev_types(p2p,
6147 (void *) wpa_s->conf->sec_device_type,
6148 wpa_s->conf->num_sec_device_types);
6149
6150 if (wpa_s->conf->changed_parameters & CFG_CHANGED_VENDOR_EXTENSION) {
6151 int i;
6152 p2p_remove_wps_vendor_extensions(p2p);
6153 for (i = 0; i < MAX_WPS_VENDOR_EXT; i++) {
6154 if (wpa_s->conf->wps_vendor_ext[i] == NULL)
6155 continue;
6156 p2p_add_wps_vendor_extension(
6157 p2p, wpa_s->conf->wps_vendor_ext[i]);
6158 }
6159 }
6160
6161 if ((wpa_s->conf->changed_parameters & CFG_CHANGED_COUNTRY) &&
6162 wpa_s->conf->country[0] && wpa_s->conf->country[1]) {
6163 char country[3];
6164 country[0] = wpa_s->conf->country[0];
6165 country[1] = wpa_s->conf->country[1];
6166 country[2] = 0x04;
6167 p2p_set_country(p2p, country);
6168 }
6169
6170 if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_SSID_POSTFIX) {
6171 p2p_set_ssid_postfix(p2p, (u8 *) wpa_s->conf->p2p_ssid_postfix,
6172 wpa_s->conf->p2p_ssid_postfix ?
6173 os_strlen(wpa_s->conf->p2p_ssid_postfix) :
6174 0);
6175 }
6176
6177 if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_INTRA_BSS)
6178 p2p_set_intra_bss_dist(p2p, wpa_s->conf->p2p_intra_bss);
Jouni Malinen75ecf522011-06-27 15:19:46 -07006179
6180 if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_LISTEN_CHANNEL) {
6181 u8 reg_class, channel;
6182 int ret;
6183 unsigned int r;
6184 if (wpa_s->conf->p2p_listen_reg_class &&
6185 wpa_s->conf->p2p_listen_channel) {
6186 reg_class = wpa_s->conf->p2p_listen_reg_class;
6187 channel = wpa_s->conf->p2p_listen_channel;
6188 } else {
6189 reg_class = 81;
6190 /*
6191 * Pick one of the social channels randomly as the
6192 * listen channel.
6193 */
6194 os_get_random((u8 *) &r, sizeof(r));
6195 channel = 1 + (r % 3) * 5;
6196 }
6197 ret = p2p_set_listen_channel(p2p, reg_class, channel);
6198 if (ret)
6199 wpa_printf(MSG_ERROR, "P2P: Own listen channel update "
6200 "failed: %d", ret);
6201 }
6202 if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_OPER_CHANNEL) {
6203 u8 op_reg_class, op_channel, cfg_op_channel;
6204 int ret = 0;
6205 unsigned int r;
6206 if (wpa_s->conf->p2p_oper_reg_class &&
6207 wpa_s->conf->p2p_oper_channel) {
6208 op_reg_class = wpa_s->conf->p2p_oper_reg_class;
6209 op_channel = wpa_s->conf->p2p_oper_channel;
6210 cfg_op_channel = 1;
6211 } else {
6212 op_reg_class = 81;
6213 /*
6214 * Use random operation channel from (1, 6, 11)
6215 *if no other preference is indicated.
6216 */
6217 os_get_random((u8 *) &r, sizeof(r));
6218 op_channel = 1 + (r % 3) * 5;
6219 cfg_op_channel = 0;
6220 }
6221 ret = p2p_set_oper_channel(p2p, op_reg_class, op_channel,
6222 cfg_op_channel);
6223 if (ret)
6224 wpa_printf(MSG_ERROR, "P2P: Own oper channel update "
6225 "failed: %d", ret);
6226 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07006227
6228 if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_PREF_CHAN) {
6229 if (p2p_set_pref_chan(p2p, wpa_s->conf->num_p2p_pref_chan,
6230 wpa_s->conf->p2p_pref_chan) < 0) {
6231 wpa_printf(MSG_ERROR, "P2P: Preferred channel list "
6232 "update failed");
6233 }
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07006234
6235 if (p2p_set_no_go_freq(p2p, &wpa_s->conf->p2p_no_go_freq) < 0) {
6236 wpa_printf(MSG_ERROR, "P2P: No GO channel list "
6237 "update failed");
6238 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07006239 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006240}
6241
6242
6243int wpas_p2p_set_noa(struct wpa_supplicant *wpa_s, u8 count, int start,
6244 int duration)
6245{
6246 if (!wpa_s->ap_iface)
6247 return -1;
6248 return hostapd_p2p_set_noa(wpa_s->ap_iface->bss[0], count, start,
6249 duration);
6250}
6251
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006252
6253int wpas_p2p_set_cross_connect(struct wpa_supplicant *wpa_s, int enabled)
6254{
6255 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
6256 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006257
6258 wpa_s->global->cross_connection = enabled;
6259 p2p_set_cross_connect(wpa_s->global->p2p, enabled);
6260
6261 if (!enabled) {
6262 struct wpa_supplicant *iface;
6263
6264 for (iface = wpa_s->global->ifaces; iface; iface = iface->next)
6265 {
6266 if (iface->cross_connect_enabled == 0)
6267 continue;
6268
6269 iface->cross_connect_enabled = 0;
6270 iface->cross_connect_in_use = 0;
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07006271 wpa_msg_global(iface->parent, MSG_INFO,
6272 P2P_EVENT_CROSS_CONNECT_DISABLE "%s %s",
6273 iface->ifname,
6274 iface->cross_connect_uplink);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006275 }
6276 }
6277
6278 return 0;
6279}
6280
6281
6282static void wpas_p2p_enable_cross_connect(struct wpa_supplicant *uplink)
6283{
6284 struct wpa_supplicant *iface;
6285
6286 if (!uplink->global->cross_connection)
6287 return;
6288
6289 for (iface = uplink->global->ifaces; iface; iface = iface->next) {
6290 if (!iface->cross_connect_enabled)
6291 continue;
6292 if (os_strcmp(uplink->ifname, iface->cross_connect_uplink) !=
6293 0)
6294 continue;
6295 if (iface->ap_iface == NULL)
6296 continue;
6297 if (iface->cross_connect_in_use)
6298 continue;
6299
6300 iface->cross_connect_in_use = 1;
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07006301 wpa_msg_global(iface->parent, MSG_INFO,
6302 P2P_EVENT_CROSS_CONNECT_ENABLE "%s %s",
6303 iface->ifname, iface->cross_connect_uplink);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006304 }
6305}
6306
6307
6308static void wpas_p2p_disable_cross_connect(struct wpa_supplicant *uplink)
6309{
6310 struct wpa_supplicant *iface;
6311
6312 for (iface = uplink->global->ifaces; iface; iface = iface->next) {
6313 if (!iface->cross_connect_enabled)
6314 continue;
6315 if (os_strcmp(uplink->ifname, iface->cross_connect_uplink) !=
6316 0)
6317 continue;
6318 if (!iface->cross_connect_in_use)
6319 continue;
6320
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07006321 wpa_msg_global(iface->parent, MSG_INFO,
6322 P2P_EVENT_CROSS_CONNECT_DISABLE "%s %s",
6323 iface->ifname, iface->cross_connect_uplink);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006324 iface->cross_connect_in_use = 0;
6325 }
6326}
6327
6328
6329void wpas_p2p_notif_connected(struct wpa_supplicant *wpa_s)
6330{
6331 if (wpa_s->ap_iface || wpa_s->current_ssid == NULL ||
6332 wpa_s->current_ssid->mode != WPAS_MODE_INFRA ||
6333 wpa_s->cross_connect_disallowed)
6334 wpas_p2p_disable_cross_connect(wpa_s);
6335 else
6336 wpas_p2p_enable_cross_connect(wpa_s);
Dmitry Shmidt04949592012-07-19 12:16:46 -07006337 if (!wpa_s->ap_iface &&
6338 eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL) > 0)
6339 wpa_printf(MSG_DEBUG, "P2P: Cancelled P2P group idle timeout");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006340}
6341
6342
6343void wpas_p2p_notif_disconnected(struct wpa_supplicant *wpa_s)
6344{
6345 wpas_p2p_disable_cross_connect(wpa_s);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006346 if (!wpa_s->ap_iface &&
6347 !eloop_is_timeout_registered(wpas_p2p_group_idle_timeout,
6348 wpa_s, NULL))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006349 wpas_p2p_set_group_idle_timeout(wpa_s);
6350}
6351
6352
6353static void wpas_p2p_cross_connect_setup(struct wpa_supplicant *wpa_s)
6354{
6355 struct wpa_supplicant *iface;
6356
6357 if (!wpa_s->global->cross_connection)
6358 return;
6359
6360 for (iface = wpa_s->global->ifaces; iface; iface = iface->next) {
6361 if (iface == wpa_s)
6362 continue;
6363 if (iface->drv_flags &
6364 WPA_DRIVER_FLAGS_P2P_DEDICATED_INTERFACE)
6365 continue;
6366 if (iface->drv_flags & WPA_DRIVER_FLAGS_P2P_CAPABLE)
6367 continue;
6368
6369 wpa_s->cross_connect_enabled = 1;
6370 os_strlcpy(wpa_s->cross_connect_uplink, iface->ifname,
6371 sizeof(wpa_s->cross_connect_uplink));
6372 wpa_printf(MSG_DEBUG, "P2P: Enable cross connection from "
6373 "%s to %s whenever uplink is available",
6374 wpa_s->ifname, wpa_s->cross_connect_uplink);
6375
6376 if (iface->ap_iface || iface->current_ssid == NULL ||
6377 iface->current_ssid->mode != WPAS_MODE_INFRA ||
6378 iface->cross_connect_disallowed ||
6379 iface->wpa_state != WPA_COMPLETED)
6380 break;
6381
6382 wpa_s->cross_connect_in_use = 1;
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07006383 wpa_msg_global(wpa_s->parent, MSG_INFO,
6384 P2P_EVENT_CROSS_CONNECT_ENABLE "%s %s",
6385 wpa_s->ifname, wpa_s->cross_connect_uplink);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006386 break;
6387 }
6388}
6389
6390
6391int wpas_p2p_notif_pbc_overlap(struct wpa_supplicant *wpa_s)
6392{
6393 if (wpa_s->p2p_group_interface != P2P_GROUP_INTERFACE_CLIENT &&
6394 !wpa_s->p2p_in_provisioning)
6395 return 0; /* not P2P client operation */
6396
6397 wpa_printf(MSG_DEBUG, "P2P: Terminate connection due to WPS PBC "
6398 "session overlap");
6399 if (wpa_s != wpa_s->parent)
6400 wpa_msg_ctrl(wpa_s->parent, MSG_INFO, WPS_EVENT_OVERLAP);
Dmitry Shmidt34af3062013-07-11 10:46:32 -07006401 wpas_p2p_group_formation_failed(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006402 return 1;
6403}
6404
6405
Dmitry Shmidtdf5a7e42014-04-02 12:59:59 -07006406void wpas_p2p_pbc_overlap_cb(void *eloop_ctx, void *timeout_ctx)
6407{
6408 struct wpa_supplicant *wpa_s = eloop_ctx;
6409 wpas_p2p_notif_pbc_overlap(wpa_s);
6410}
6411
6412
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006413void wpas_p2p_update_channel_list(struct wpa_supplicant *wpa_s)
6414{
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07006415 struct p2p_channels chan, cli_chan;
Dmitry Shmidt684785c2014-05-12 13:34:29 -07006416 struct wpa_supplicant *ifs;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006417
6418 if (wpa_s->global == NULL || wpa_s->global->p2p == NULL)
6419 return;
6420
6421 os_memset(&chan, 0, sizeof(chan));
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07006422 os_memset(&cli_chan, 0, sizeof(cli_chan));
6423 if (wpas_p2p_setup_channels(wpa_s, &chan, &cli_chan)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006424 wpa_printf(MSG_ERROR, "P2P: Failed to update supported "
6425 "channel list");
6426 return;
6427 }
6428
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07006429 p2p_update_channel_list(wpa_s->global->p2p, &chan, &cli_chan);
Dmitry Shmidt684785c2014-05-12 13:34:29 -07006430
6431 for (ifs = wpa_s->global->ifaces; ifs; ifs = ifs->next) {
6432 int freq;
6433 if (!ifs->current_ssid ||
6434 !ifs->current_ssid->p2p_group ||
6435 (ifs->current_ssid->mode != WPAS_MODE_P2P_GO &&
6436 ifs->current_ssid->mode != WPAS_MODE_P2P_GROUP_FORMATION))
6437 continue;
6438 freq = ifs->current_ssid->frequency;
6439 if (freq_included(&chan, freq)) {
6440 wpa_dbg(ifs, MSG_DEBUG,
6441 "P2P GO operating frequency %d MHz in valid range",
6442 freq);
6443 continue;
6444 }
6445
6446 wpa_dbg(ifs, MSG_DEBUG,
6447 "P2P GO operating in invalid frequency %d MHz", freq);
6448 /* TODO: Consider using CSA or removing the group within
6449 * wpa_supplicant */
6450 wpa_msg(ifs, MSG_INFO, P2P_EVENT_REMOVE_AND_REFORM_GROUP);
6451 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006452}
6453
6454
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08006455static void wpas_p2p_scan_res_ignore(struct wpa_supplicant *wpa_s,
6456 struct wpa_scan_results *scan_res)
6457{
6458 wpa_printf(MSG_DEBUG, "P2P: Ignore scan results");
6459}
6460
6461
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006462int wpas_p2p_cancel(struct wpa_supplicant *wpa_s)
6463{
6464 struct wpa_global *global = wpa_s->global;
6465 int found = 0;
6466 const u8 *peer;
6467
6468 if (global->p2p == NULL)
6469 return -1;
6470
6471 wpa_printf(MSG_DEBUG, "P2P: Request to cancel group formation");
6472
6473 if (wpa_s->pending_interface_name[0] &&
6474 !is_zero_ether_addr(wpa_s->pending_interface_addr))
6475 found = 1;
6476
6477 peer = p2p_get_go_neg_peer(global->p2p);
6478 if (peer) {
6479 wpa_printf(MSG_DEBUG, "P2P: Unauthorize pending GO Neg peer "
6480 MACSTR, MAC2STR(peer));
6481 p2p_unauthorize(global->p2p, peer);
Dmitry Shmidt04949592012-07-19 12:16:46 -07006482 found = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006483 }
6484
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08006485 if (wpa_s->scan_res_handler == wpas_p2p_scan_res_join) {
6486 wpa_printf(MSG_DEBUG, "P2P: Stop pending scan for join");
6487 wpa_s->scan_res_handler = wpas_p2p_scan_res_ignore;
6488 found = 1;
6489 }
6490
6491 if (wpa_s->pending_pd_before_join) {
6492 wpa_printf(MSG_DEBUG, "P2P: Stop pending PD before join");
6493 wpa_s->pending_pd_before_join = 0;
6494 found = 1;
6495 }
6496
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006497 wpas_p2p_stop_find(wpa_s);
6498
6499 for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
6500 if (wpa_s == global->p2p_group_formation &&
6501 (wpa_s->p2p_in_provisioning ||
6502 wpa_s->parent->pending_interface_type ==
6503 WPA_IF_P2P_CLIENT)) {
6504 wpa_printf(MSG_DEBUG, "P2P: Interface %s in group "
6505 "formation found - cancelling",
6506 wpa_s->ifname);
6507 found = 1;
6508 eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
6509 wpa_s->parent, NULL);
Jouni Malinenadddfc42012-10-03 14:31:41 -07006510 if (wpa_s->p2p_in_provisioning) {
6511 wpas_group_formation_completed(wpa_s, 0);
6512 break;
6513 }
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07006514 wpas_p2p_group_delete(wpa_s,
6515 P2P_GROUP_REMOVAL_REQUESTED);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006516 break;
Dmitry Shmidt21de2142014-04-08 10:50:52 -07006517 } else if (wpa_s->p2p_in_invitation) {
6518 wpa_printf(MSG_DEBUG, "P2P: Interface %s in invitation found - cancelling",
6519 wpa_s->ifname);
6520 found = 1;
6521 wpas_p2p_group_formation_failed(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006522 }
6523 }
6524
6525 if (!found) {
6526 wpa_printf(MSG_DEBUG, "P2P: No ongoing group formation found");
6527 return -1;
6528 }
6529
6530 return 0;
6531}
6532
6533
6534void wpas_p2p_interface_unavailable(struct wpa_supplicant *wpa_s)
6535{
6536 if (wpa_s->current_ssid == NULL || !wpa_s->current_ssid->p2p_group)
6537 return;
6538
6539 wpa_printf(MSG_DEBUG, "P2P: Remove group due to driver resource not "
6540 "being available anymore");
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07006541 wpas_p2p_group_delete(wpa_s, P2P_GROUP_REMOVAL_UNAVAILABLE);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006542}
6543
6544
6545void wpas_p2p_update_best_channels(struct wpa_supplicant *wpa_s,
6546 int freq_24, int freq_5, int freq_overall)
6547{
6548 struct p2p_data *p2p = wpa_s->global->p2p;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08006549 if (p2p == NULL)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006550 return;
6551 p2p_set_best_channels(p2p, freq_24, freq_5, freq_overall);
6552}
6553
6554
6555int wpas_p2p_unauthorize(struct wpa_supplicant *wpa_s, const char *addr)
6556{
6557 u8 peer[ETH_ALEN];
6558 struct p2p_data *p2p = wpa_s->global->p2p;
6559
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08006560 if (p2p == NULL)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006561 return -1;
6562
6563 if (hwaddr_aton(addr, peer))
6564 return -1;
6565
6566 return p2p_unauthorize(p2p, peer);
6567}
6568
6569
6570/**
6571 * wpas_p2p_disconnect - Disconnect from a P2P Group
6572 * @wpa_s: Pointer to wpa_supplicant data
6573 * Returns: 0 on success, -1 on failure
6574 *
6575 * This can be used to disconnect from a group in which the local end is a P2P
6576 * Client or to end a P2P Group in case the local end is the Group Owner. If a
6577 * virtual network interface was created for this group, that interface will be
6578 * removed. Otherwise, only the configured P2P group network will be removed
6579 * from the interface.
6580 */
6581int wpas_p2p_disconnect(struct wpa_supplicant *wpa_s)
6582{
6583
6584 if (wpa_s == NULL)
6585 return -1;
6586
Jouni Malinen2b89da82012-08-31 22:04:41 +03006587 return wpas_p2p_group_delete(wpa_s, P2P_GROUP_REMOVAL_REQUESTED) < 0 ?
6588 -1 : 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006589}
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006590
6591
6592int wpas_p2p_in_progress(struct wpa_supplicant *wpa_s)
6593{
Dmitry Shmidtf8623282013-02-20 14:34:59 -08006594 int ret;
6595
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006596 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
6597 return 0;
6598
Dmitry Shmidtf8623282013-02-20 14:34:59 -08006599 ret = p2p_in_progress(wpa_s->global->p2p);
6600 if (ret == 0) {
6601 /*
6602 * Check whether there is an ongoing WPS provisioning step (or
6603 * other parts of group formation) on another interface since
6604 * p2p_in_progress() does not report this to avoid issues for
6605 * scans during such provisioning step.
6606 */
6607 if (wpa_s->global->p2p_group_formation &&
6608 wpa_s->global->p2p_group_formation != wpa_s) {
6609 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Another interface (%s) "
6610 "in group formation",
6611 wpa_s->global->p2p_group_formation->ifname);
6612 ret = 1;
6613 }
6614 }
6615
Dmitry Shmidt92c368d2013-08-29 12:37:21 -07006616 if (!ret && wpa_s->global->p2p_go_wait_client.sec) {
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08006617 struct os_reltime now;
6618 os_get_reltime(&now);
6619 if (os_reltime_expired(&now, &wpa_s->global->p2p_go_wait_client,
6620 P2P_MAX_INITIAL_CONN_WAIT_GO)) {
Dmitry Shmidt92c368d2013-08-29 12:37:21 -07006621 /* Wait for the first client has expired */
6622 wpa_s->global->p2p_go_wait_client.sec = 0;
6623 } else {
6624 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Waiting for initial client connection during group formation");
6625 ret = 1;
6626 }
6627 }
6628
Dmitry Shmidtf8623282013-02-20 14:34:59 -08006629 return ret;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006630}
6631
6632
6633void wpas_p2p_network_removed(struct wpa_supplicant *wpa_s,
6634 struct wpa_ssid *ssid)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006635{
6636 if (wpa_s->p2p_in_provisioning && ssid->p2p_group &&
6637 eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
6638 wpa_s->parent, NULL) > 0) {
Dmitry Shmidt04949592012-07-19 12:16:46 -07006639 /**
6640 * Remove the network by scheduling the group formation
6641 * timeout to happen immediately. The teardown code
6642 * needs to be scheduled to run asynch later so that we
6643 * don't delete data from under ourselves unexpectedly.
6644 * Calling wpas_p2p_group_formation_timeout directly
6645 * causes a series of crashes in WPS failure scenarios.
6646 */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006647 wpa_printf(MSG_DEBUG, "P2P: Canceled group formation due to "
6648 "P2P group network getting removed");
Dmitry Shmidt04949592012-07-19 12:16:46 -07006649 eloop_register_timeout(0, 0, wpas_p2p_group_formation_timeout,
6650 wpa_s->parent, NULL);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006651 }
6652}
6653
6654
6655struct wpa_ssid * wpas_p2p_get_persistent(struct wpa_supplicant *wpa_s,
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08006656 const u8 *addr, const u8 *ssid,
6657 size_t ssid_len)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006658{
6659 struct wpa_ssid *s;
6660 size_t i;
6661
6662 for (s = wpa_s->conf->ssid; s; s = s->next) {
6663 if (s->disabled != 2)
6664 continue;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08006665 if (ssid &&
6666 (ssid_len != s->ssid_len ||
6667 os_memcmp(ssid, s->ssid, ssid_len) != 0))
6668 continue;
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07006669 if (addr == NULL) {
6670 if (s->mode == WPAS_MODE_P2P_GO)
6671 return s;
6672 continue;
6673 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006674 if (os_memcmp(s->bssid, addr, ETH_ALEN) == 0)
6675 return s; /* peer is GO in the persistent group */
6676 if (s->mode != WPAS_MODE_P2P_GO || s->p2p_client_list == NULL)
6677 continue;
6678 for (i = 0; i < s->num_p2p_clients; i++) {
6679 if (os_memcmp(s->p2p_client_list + i * ETH_ALEN,
6680 addr, ETH_ALEN) == 0)
6681 return s; /* peer is P2P client in persistent
6682 * group */
6683 }
6684 }
6685
6686 return NULL;
6687}
6688
6689
6690void wpas_p2p_notify_ap_sta_authorized(struct wpa_supplicant *wpa_s,
6691 const u8 *addr)
6692{
Dmitry Shmidt56052862013-10-04 10:23:25 -07006693 if (eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
6694 wpa_s->parent, NULL) > 0) {
6695 /*
6696 * This can happen if WPS provisioning step is not terminated
6697 * cleanly (e.g., P2P Client does not send WSC_Done). Since the
6698 * peer was able to connect, there is no need to time out group
6699 * formation after this, though. In addition, this is used with
6700 * the initial connection wait on the GO as a separate formation
6701 * timeout and as such, expected to be hit after the initial WPS
6702 * provisioning step.
6703 */
6704 wpa_printf(MSG_DEBUG, "P2P: Canceled P2P group formation timeout on data connection");
6705 }
6706 if (!wpa_s->p2p_go_group_formation_completed) {
6707 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Marking group formation completed on GO on first data connection");
6708 wpa_s->p2p_go_group_formation_completed = 1;
6709 wpa_s->global->p2p_group_formation = NULL;
6710 wpa_s->p2p_in_provisioning = 0;
Dmitry Shmidt21de2142014-04-08 10:50:52 -07006711 wpa_s->p2p_in_invitation = 0;
Dmitry Shmidt56052862013-10-04 10:23:25 -07006712 }
Dmitry Shmidt92c368d2013-08-29 12:37:21 -07006713 wpa_s->global->p2p_go_wait_client.sec = 0;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006714 if (addr == NULL)
6715 return;
6716 wpas_p2p_add_persistent_group_client(wpa_s, addr);
6717}
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08006718
Dmitry Shmidt04949592012-07-19 12:16:46 -07006719
6720static void wpas_p2p_fallback_to_go_neg(struct wpa_supplicant *wpa_s,
6721 int group_added)
6722{
6723 struct wpa_supplicant *group = wpa_s;
Dmitry Shmidt04949592012-07-19 12:16:46 -07006724 if (wpa_s->global->p2p_group_formation)
6725 group = wpa_s->global->p2p_group_formation;
6726 wpa_s = wpa_s->parent;
6727 offchannel_send_action_done(wpa_s);
6728 if (group_added)
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07006729 wpas_p2p_group_delete(group, P2P_GROUP_REMOVAL_SILENT);
Dmitry Shmidt04949592012-07-19 12:16:46 -07006730 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Fall back to GO Negotiation");
6731 wpas_p2p_connect(wpa_s, wpa_s->pending_join_dev_addr, wpa_s->p2p_pin,
6732 wpa_s->p2p_wps_method, wpa_s->p2p_persistent_group, 0,
6733 0, 0, wpa_s->p2p_go_intent, wpa_s->p2p_connect_freq,
6734 wpa_s->p2p_persistent_id,
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07006735 wpa_s->p2p_pd_before_go_neg,
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07006736 wpa_s->p2p_go_ht40,
6737 wpa_s->p2p_go_vht);
Dmitry Shmidt04949592012-07-19 12:16:46 -07006738}
6739
6740
6741int wpas_p2p_scan_no_go_seen(struct wpa_supplicant *wpa_s)
6742{
6743 if (!wpa_s->p2p_fallback_to_go_neg ||
6744 wpa_s->p2p_in_provisioning <= 5)
6745 return 0;
6746
6747 if (wpas_p2p_peer_go(wpa_s, wpa_s->pending_join_dev_addr) > 0)
6748 return 0; /* peer operating as a GO */
6749
6750 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: GO not found for p2p_connect-auto - "
6751 "fallback to GO Negotiation");
6752 wpas_p2p_fallback_to_go_neg(wpa_s, 1);
6753
6754 return 1;
6755}
6756
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07006757
6758unsigned int wpas_p2p_search_delay(struct wpa_supplicant *wpa_s)
6759{
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07006760 struct wpa_supplicant *ifs;
6761
6762 if (wpa_s->wpa_state > WPA_SCANNING) {
6763 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Use %u ms search delay due to "
6764 "concurrent operation",
6765 P2P_CONCURRENT_SEARCH_DELAY);
6766 return P2P_CONCURRENT_SEARCH_DELAY;
6767 }
6768
Dmitry Shmidt01904cf2013-12-05 11:08:35 -08006769 dl_list_for_each(ifs, &wpa_s->radio->ifaces, struct wpa_supplicant,
6770 radio_list) {
6771 if (ifs != wpa_s && ifs->wpa_state > WPA_SCANNING) {
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07006772 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Use %u ms search "
6773 "delay due to concurrent operation on "
6774 "interface %s",
6775 P2P_CONCURRENT_SEARCH_DELAY, ifs->ifname);
6776 return P2P_CONCURRENT_SEARCH_DELAY;
6777 }
6778 }
6779
6780 return 0;
6781}
6782
Dmitry Shmidt37d4d6a2013-03-18 13:09:42 -07006783
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07006784static int wpas_p2p_remove_psk_entry(struct wpa_supplicant *wpa_s,
6785 struct wpa_ssid *s, const u8 *addr,
6786 int iface_addr)
6787{
6788 struct psk_list_entry *psk, *tmp;
6789 int changed = 0;
6790
6791 dl_list_for_each_safe(psk, tmp, &s->psk_list, struct psk_list_entry,
6792 list) {
6793 if ((iface_addr && !psk->p2p &&
6794 os_memcmp(addr, psk->addr, ETH_ALEN) == 0) ||
6795 (!iface_addr && psk->p2p &&
6796 os_memcmp(addr, psk->addr, ETH_ALEN) == 0)) {
6797 wpa_dbg(wpa_s, MSG_DEBUG,
6798 "P2P: Remove persistent group PSK list entry for "
6799 MACSTR " p2p=%u",
6800 MAC2STR(psk->addr), psk->p2p);
6801 dl_list_del(&psk->list);
6802 os_free(psk);
6803 changed++;
6804 }
6805 }
6806
6807 return changed;
6808}
6809
6810
6811void wpas_p2p_new_psk_cb(struct wpa_supplicant *wpa_s, const u8 *mac_addr,
6812 const u8 *p2p_dev_addr,
6813 const u8 *psk, size_t psk_len)
6814{
6815 struct wpa_ssid *ssid = wpa_s->current_ssid;
6816 struct wpa_ssid *persistent;
Dmitry Shmidt7832adb2014-04-29 10:53:02 -07006817 struct psk_list_entry *p, *last;
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07006818
6819 if (psk_len != sizeof(p->psk))
6820 return;
6821
6822 if (p2p_dev_addr) {
6823 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: New PSK for addr=" MACSTR
6824 " p2p_dev_addr=" MACSTR,
6825 MAC2STR(mac_addr), MAC2STR(p2p_dev_addr));
6826 if (is_zero_ether_addr(p2p_dev_addr))
6827 p2p_dev_addr = NULL;
6828 } else {
6829 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: New PSK for addr=" MACSTR,
6830 MAC2STR(mac_addr));
6831 }
6832
6833 if (ssid->mode == WPAS_MODE_P2P_GROUP_FORMATION) {
6834 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: new_psk_cb during group formation");
6835 /* To be added to persistent group once created */
6836 if (wpa_s->global->add_psk == NULL) {
6837 wpa_s->global->add_psk = os_zalloc(sizeof(*p));
6838 if (wpa_s->global->add_psk == NULL)
6839 return;
6840 }
6841 p = wpa_s->global->add_psk;
6842 if (p2p_dev_addr) {
6843 p->p2p = 1;
6844 os_memcpy(p->addr, p2p_dev_addr, ETH_ALEN);
6845 } else {
6846 p->p2p = 0;
6847 os_memcpy(p->addr, mac_addr, ETH_ALEN);
6848 }
6849 os_memcpy(p->psk, psk, psk_len);
6850 return;
6851 }
6852
6853 if (ssid->mode != WPAS_MODE_P2P_GO || !ssid->p2p_persistent_group) {
6854 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Ignore new_psk_cb on not-persistent GO");
6855 return;
6856 }
6857
6858 persistent = wpas_p2p_get_persistent(wpa_s->parent, NULL, ssid->ssid,
6859 ssid->ssid_len);
6860 if (!persistent) {
6861 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Could not find persistent group information to store the new PSK");
6862 return;
6863 }
6864
6865 p = os_zalloc(sizeof(*p));
6866 if (p == NULL)
6867 return;
6868 if (p2p_dev_addr) {
6869 p->p2p = 1;
6870 os_memcpy(p->addr, p2p_dev_addr, ETH_ALEN);
6871 } else {
6872 p->p2p = 0;
6873 os_memcpy(p->addr, mac_addr, ETH_ALEN);
6874 }
6875 os_memcpy(p->psk, psk, psk_len);
6876
Dmitry Shmidt7832adb2014-04-29 10:53:02 -07006877 if (dl_list_len(&persistent->psk_list) > P2P_MAX_STORED_CLIENTS &&
6878 (last = dl_list_last(&persistent->psk_list,
6879 struct psk_list_entry, list))) {
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07006880 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Remove oldest PSK entry for "
6881 MACSTR " (p2p=%u) to make room for a new one",
6882 MAC2STR(last->addr), last->p2p);
6883 dl_list_del(&last->list);
6884 os_free(last);
6885 }
6886
6887 wpas_p2p_remove_psk_entry(wpa_s->parent, persistent,
6888 p2p_dev_addr ? p2p_dev_addr : mac_addr,
6889 p2p_dev_addr == NULL);
6890 if (p2p_dev_addr) {
6891 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Add new PSK for p2p_dev_addr="
6892 MACSTR, MAC2STR(p2p_dev_addr));
6893 } else {
6894 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Add new PSK for addr=" MACSTR,
6895 MAC2STR(mac_addr));
6896 }
6897 dl_list_add(&persistent->psk_list, &p->list);
6898
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07006899 if (wpa_s->parent->conf->update_config &&
6900 wpa_config_write(wpa_s->parent->confname, wpa_s->parent->conf))
6901 wpa_printf(MSG_DEBUG, "P2P: Failed to update configuration");
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07006902}
6903
6904
6905static void wpas_p2p_remove_psk(struct wpa_supplicant *wpa_s,
6906 struct wpa_ssid *s, const u8 *addr,
6907 int iface_addr)
6908{
6909 int res;
6910
6911 res = wpas_p2p_remove_psk_entry(wpa_s, s, addr, iface_addr);
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08006912 if (res > 0 && wpa_s->conf->update_config &&
6913 wpa_config_write(wpa_s->confname, wpa_s->conf))
6914 wpa_dbg(wpa_s, MSG_DEBUG,
6915 "P2P: Failed to update configuration");
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07006916}
6917
6918
6919static void wpas_p2p_remove_client_go(struct wpa_supplicant *wpa_s,
6920 const u8 *peer, int iface_addr)
6921{
6922 struct hostapd_data *hapd;
6923 struct hostapd_wpa_psk *psk, *prev, *rem;
6924 struct sta_info *sta;
6925
6926 if (wpa_s->ap_iface == NULL || wpa_s->current_ssid == NULL ||
6927 wpa_s->current_ssid->mode != WPAS_MODE_P2P_GO)
6928 return;
6929
6930 /* Remove per-station PSK entry */
6931 hapd = wpa_s->ap_iface->bss[0];
6932 prev = NULL;
6933 psk = hapd->conf->ssid.wpa_psk;
6934 while (psk) {
6935 if ((iface_addr && os_memcmp(peer, psk->addr, ETH_ALEN) == 0) ||
6936 (!iface_addr &&
6937 os_memcmp(peer, psk->p2p_dev_addr, ETH_ALEN) == 0)) {
6938 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Remove operating group PSK entry for "
6939 MACSTR " iface_addr=%d",
6940 MAC2STR(peer), iface_addr);
6941 if (prev)
6942 prev->next = psk->next;
6943 else
6944 hapd->conf->ssid.wpa_psk = psk->next;
6945 rem = psk;
6946 psk = psk->next;
6947 os_free(rem);
6948 } else {
6949 prev = psk;
6950 psk = psk->next;
6951 }
6952 }
6953
6954 /* Disconnect from group */
6955 if (iface_addr)
6956 sta = ap_get_sta(hapd, peer);
6957 else
6958 sta = ap_get_sta_p2p(hapd, peer);
6959 if (sta) {
6960 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Disconnect peer " MACSTR
6961 " (iface_addr=%d) from group",
6962 MAC2STR(peer), iface_addr);
6963 hostapd_drv_sta_deauth(hapd, sta->addr,
6964 WLAN_REASON_DEAUTH_LEAVING);
6965 ap_sta_deauthenticate(hapd, sta, WLAN_REASON_DEAUTH_LEAVING);
6966 }
6967}
6968
6969
6970void wpas_p2p_remove_client(struct wpa_supplicant *wpa_s, const u8 *peer,
6971 int iface_addr)
6972{
6973 struct wpa_ssid *s;
6974 struct wpa_supplicant *w;
6975
6976 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Remove client " MACSTR, MAC2STR(peer));
6977
6978 /* Remove from any persistent group */
6979 for (s = wpa_s->parent->conf->ssid; s; s = s->next) {
6980 if (s->disabled != 2 || s->mode != WPAS_MODE_P2P_GO)
6981 continue;
6982 if (!iface_addr)
6983 wpas_remove_persistent_peer(wpa_s, s, peer, 0);
6984 wpas_p2p_remove_psk(wpa_s->parent, s, peer, iface_addr);
6985 }
6986
6987 /* Remove from any operating group */
6988 for (w = wpa_s->global->ifaces; w; w = w->next)
6989 wpas_p2p_remove_client_go(w, peer, iface_addr);
6990}
6991
6992
6993static void wpas_p2p_psk_failure_removal(void *eloop_ctx, void *timeout_ctx)
6994{
6995 struct wpa_supplicant *wpa_s = eloop_ctx;
6996 wpas_p2p_group_delete(wpa_s, P2P_GROUP_REMOVAL_PSK_FAILURE);
6997}
6998
6999
Dmitry Shmidt04f534e2013-12-09 15:50:16 -08007000static void wpas_p2p_group_freq_conflict(void *eloop_ctx, void *timeout_ctx)
7001{
7002 struct wpa_supplicant *wpa_s = eloop_ctx;
7003
7004 wpa_printf(MSG_DEBUG, "P2P: Frequency conflict - terminate group");
7005 wpas_p2p_group_delete(wpa_s, P2P_GROUP_REMOVAL_FREQ_CONFLICT);
7006}
7007
7008
7009int wpas_p2p_handle_frequency_conflicts(struct wpa_supplicant *wpa_s, int freq,
7010 struct wpa_ssid *ssid)
7011{
7012 struct wpa_supplicant *iface;
7013
7014 for (iface = wpa_s->global->ifaces; iface; iface = iface->next) {
7015 if (!iface->current_ssid ||
7016 iface->current_ssid->frequency == freq ||
7017 (iface->p2p_group_interface == NOT_P2P_GROUP_INTERFACE &&
7018 !iface->current_ssid->p2p_group))
7019 continue;
7020
7021 /* Remove the connection with least priority */
7022 if (!wpas_is_p2p_prioritized(iface)) {
7023 /* STA connection has priority over existing
7024 * P2P connection, so remove the interface. */
7025 wpa_printf(MSG_DEBUG, "P2P: Removing P2P connection due to single channel concurrent mode frequency conflict");
7026 eloop_register_timeout(0, 0,
7027 wpas_p2p_group_freq_conflict,
7028 iface, NULL);
7029 /* If connection in progress is P2P connection, do not
7030 * proceed for the connection. */
7031 if (wpa_s == iface)
7032 return -1;
7033 else
7034 return 0;
7035 } else {
7036 /* P2P connection has priority, disable the STA network
7037 */
7038 wpa_supplicant_disable_network(wpa_s->global->ifaces,
7039 ssid);
7040 wpa_msg(wpa_s->global->ifaces, MSG_INFO,
7041 WPA_EVENT_FREQ_CONFLICT " id=%d", ssid->id);
7042 os_memset(wpa_s->global->ifaces->pending_bssid, 0,
7043 ETH_ALEN);
7044 /* If P2P connection is in progress, continue
7045 * connecting...*/
7046 if (wpa_s == iface)
7047 return 0;
7048 else
7049 return -1;
7050 }
7051 }
7052
7053 return 0;
7054}
7055
7056
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07007057int wpas_p2p_4way_hs_failed(struct wpa_supplicant *wpa_s)
7058{
7059 struct wpa_ssid *ssid = wpa_s->current_ssid;
7060
7061 if (ssid == NULL || !ssid->p2p_group)
7062 return 0;
7063
7064 if (wpa_s->p2p_last_4way_hs_fail &&
7065 wpa_s->p2p_last_4way_hs_fail == ssid) {
7066 u8 go_dev_addr[ETH_ALEN];
7067 struct wpa_ssid *persistent;
7068
7069 if (wpas_p2p_persistent_group(wpa_s, go_dev_addr,
7070 ssid->ssid,
7071 ssid->ssid_len) <= 0) {
7072 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Could not determine whether 4-way handshake failures were for a persistent group");
7073 goto disconnect;
7074 }
7075
7076 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Two 4-way handshake failures for a P2P group - go_dev_addr="
7077 MACSTR, MAC2STR(go_dev_addr));
7078 persistent = wpas_p2p_get_persistent(wpa_s->parent, go_dev_addr,
7079 ssid->ssid,
7080 ssid->ssid_len);
7081 if (persistent == NULL || persistent->mode != WPAS_MODE_INFRA) {
7082 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: No matching persistent group stored");
7083 goto disconnect;
7084 }
7085 wpa_msg_global(wpa_s->parent, MSG_INFO,
7086 P2P_EVENT_PERSISTENT_PSK_FAIL "%d",
7087 persistent->id);
7088 disconnect:
7089 wpa_s->p2p_last_4way_hs_fail = NULL;
7090 /*
7091 * Remove the group from a timeout to avoid issues with caller
7092 * continuing to use the interface if this is on a P2P group
7093 * interface.
7094 */
7095 eloop_register_timeout(0, 0, wpas_p2p_psk_failure_removal,
7096 wpa_s, NULL);
7097 return 1;
7098 }
7099
7100 wpa_s->p2p_last_4way_hs_fail = ssid;
7101 return 0;
7102}
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08007103
7104
7105#ifdef CONFIG_WPS_NFC
7106
7107static struct wpabuf * wpas_p2p_nfc_handover(int ndef, struct wpabuf *wsc,
7108 struct wpabuf *p2p)
7109{
7110 struct wpabuf *ret;
7111 size_t wsc_len;
7112
7113 if (p2p == NULL) {
7114 wpabuf_free(wsc);
7115 wpa_printf(MSG_DEBUG, "P2P: No p2p buffer for handover");
7116 return NULL;
7117 }
7118
7119 wsc_len = wsc ? wpabuf_len(wsc) : 0;
7120 ret = wpabuf_alloc(2 + wsc_len + 2 + wpabuf_len(p2p));
7121 if (ret == NULL) {
7122 wpabuf_free(wsc);
7123 wpabuf_free(p2p);
7124 return NULL;
7125 }
7126
7127 wpabuf_put_be16(ret, wsc_len);
7128 if (wsc)
7129 wpabuf_put_buf(ret, wsc);
7130 wpabuf_put_be16(ret, wpabuf_len(p2p));
7131 wpabuf_put_buf(ret, p2p);
7132
7133 wpabuf_free(wsc);
7134 wpabuf_free(p2p);
7135 wpa_hexdump_buf(MSG_DEBUG,
7136 "P2P: Generated NFC connection handover message", ret);
7137
7138 if (ndef && ret) {
7139 struct wpabuf *tmp;
7140 tmp = ndef_build_p2p(ret);
7141 wpabuf_free(ret);
7142 if (tmp == NULL) {
7143 wpa_printf(MSG_DEBUG, "P2P: Failed to NDEF encapsulate handover request");
7144 return NULL;
7145 }
7146 ret = tmp;
7147 }
7148
7149 return ret;
7150}
7151
7152
7153static int wpas_p2p_cli_freq(struct wpa_supplicant *wpa_s,
7154 struct wpa_ssid **ssid, u8 *go_dev_addr)
7155{
7156 struct wpa_supplicant *iface;
7157
7158 if (go_dev_addr)
7159 os_memset(go_dev_addr, 0, ETH_ALEN);
7160 if (ssid)
7161 *ssid = NULL;
7162 for (iface = wpa_s->global->ifaces; iface; iface = iface->next) {
7163 if (iface->wpa_state < WPA_ASSOCIATING ||
7164 iface->current_ssid == NULL || iface->assoc_freq == 0 ||
7165 !iface->current_ssid->p2p_group ||
7166 iface->current_ssid->mode != WPAS_MODE_INFRA)
7167 continue;
7168 if (ssid)
7169 *ssid = iface->current_ssid;
7170 if (go_dev_addr)
7171 os_memcpy(go_dev_addr, iface->go_dev_addr, ETH_ALEN);
7172 return iface->assoc_freq;
7173 }
7174 return 0;
7175}
7176
7177
7178struct wpabuf * wpas_p2p_nfc_handover_req(struct wpa_supplicant *wpa_s,
7179 int ndef)
7180{
7181 struct wpabuf *wsc, *p2p;
7182 struct wpa_ssid *ssid;
7183 u8 go_dev_addr[ETH_ALEN];
7184 int cli_freq = wpas_p2p_cli_freq(wpa_s, &ssid, go_dev_addr);
7185
7186 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL) {
7187 wpa_printf(MSG_DEBUG, "P2P: P2P disabled - cannot build handover request");
7188 return NULL;
7189 }
7190
7191 if (wpa_s->conf->wps_nfc_dh_pubkey == NULL &&
7192 wps_nfc_gen_dh(&wpa_s->conf->wps_nfc_dh_pubkey,
7193 &wpa_s->conf->wps_nfc_dh_privkey) < 0) {
7194 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: No DH key available for handover request");
7195 return NULL;
7196 }
7197
7198 if (cli_freq == 0) {
7199 wsc = wps_build_nfc_handover_req_p2p(
7200 wpa_s->parent->wps, wpa_s->conf->wps_nfc_dh_pubkey);
7201 } else
7202 wsc = NULL;
7203 p2p = p2p_build_nfc_handover_req(wpa_s->global->p2p, cli_freq,
7204 go_dev_addr, ssid ? ssid->ssid : NULL,
7205 ssid ? ssid->ssid_len : 0);
7206
7207 return wpas_p2p_nfc_handover(ndef, wsc, p2p);
7208}
7209
7210
7211struct wpabuf * wpas_p2p_nfc_handover_sel(struct wpa_supplicant *wpa_s,
7212 int ndef, int tag)
7213{
7214 struct wpabuf *wsc, *p2p;
7215 struct wpa_ssid *ssid;
7216 u8 go_dev_addr[ETH_ALEN];
7217 int cli_freq = wpas_p2p_cli_freq(wpa_s, &ssid, go_dev_addr);
7218
7219 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
7220 return NULL;
7221
7222 if (!tag && wpa_s->conf->wps_nfc_dh_pubkey == NULL &&
7223 wps_nfc_gen_dh(&wpa_s->conf->wps_nfc_dh_pubkey,
7224 &wpa_s->conf->wps_nfc_dh_privkey) < 0)
7225 return NULL;
7226
7227 if (cli_freq == 0) {
7228 wsc = wps_build_nfc_handover_sel_p2p(
7229 wpa_s->parent->wps,
7230 tag ? wpa_s->conf->wps_nfc_dev_pw_id :
7231 DEV_PW_NFC_CONNECTION_HANDOVER,
7232 wpa_s->conf->wps_nfc_dh_pubkey,
7233 tag ? wpa_s->conf->wps_nfc_dev_pw : NULL);
7234 } else
7235 wsc = NULL;
7236 p2p = p2p_build_nfc_handover_sel(wpa_s->global->p2p, cli_freq,
7237 go_dev_addr, ssid ? ssid->ssid : NULL,
7238 ssid ? ssid->ssid_len : 0);
7239
7240 return wpas_p2p_nfc_handover(ndef, wsc, p2p);
7241}
7242
7243
7244static int wpas_p2p_nfc_join_group(struct wpa_supplicant *wpa_s,
7245 struct p2p_nfc_params *params)
7246{
7247 wpa_printf(MSG_DEBUG, "P2P: Initiate join-group based on NFC "
7248 "connection handover (freq=%d)",
7249 params->go_freq);
7250
7251 if (params->go_freq && params->go_ssid_len) {
7252 wpa_s->p2p_wps_method = WPS_NFC;
7253 wpa_s->pending_join_wps_method = WPS_NFC;
7254 os_memset(wpa_s->pending_join_iface_addr, 0, ETH_ALEN);
7255 os_memcpy(wpa_s->pending_join_dev_addr, params->go_dev_addr,
7256 ETH_ALEN);
7257 return wpas_p2p_join_start(wpa_s, params->go_freq,
7258 params->go_ssid,
7259 params->go_ssid_len);
7260 }
7261
7262 return wpas_p2p_connect(wpa_s, params->peer->p2p_device_addr, NULL,
7263 WPS_NFC, 0, 0, 1, 0, wpa_s->conf->p2p_go_intent,
7264 params->go_freq, -1, 0, 1, 1);
7265}
7266
7267
7268static int wpas_p2p_nfc_auth_join(struct wpa_supplicant *wpa_s,
7269 struct p2p_nfc_params *params, int tag)
7270{
7271 int res, persistent;
7272 struct wpa_ssid *ssid;
7273
7274 wpa_printf(MSG_DEBUG, "P2P: Authorize join-group based on NFC "
7275 "connection handover");
7276 for (wpa_s = wpa_s->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
7277 ssid = wpa_s->current_ssid;
7278 if (ssid == NULL)
7279 continue;
7280 if (ssid->mode != WPAS_MODE_P2P_GO)
7281 continue;
7282 if (wpa_s->ap_iface == NULL)
7283 continue;
7284 break;
7285 }
7286 if (wpa_s == NULL) {
7287 wpa_printf(MSG_DEBUG, "P2P: Could not find GO interface");
7288 return -1;
7289 }
7290
7291 if (wpa_s->parent->p2p_oob_dev_pw_id !=
7292 DEV_PW_NFC_CONNECTION_HANDOVER &&
7293 !wpa_s->parent->p2p_oob_dev_pw) {
7294 wpa_printf(MSG_DEBUG, "P2P: No NFC Dev Pw known");
7295 return -1;
7296 }
7297 res = wpas_ap_wps_add_nfc_pw(
7298 wpa_s, wpa_s->parent->p2p_oob_dev_pw_id,
7299 wpa_s->parent->p2p_oob_dev_pw,
7300 wpa_s->parent->p2p_peer_oob_pk_hash_known ?
7301 wpa_s->parent->p2p_peer_oob_pubkey_hash : NULL);
7302 if (res)
7303 return res;
7304
7305 if (!tag) {
7306 wpa_printf(MSG_DEBUG, "P2P: Negotiated handover - wait for peer to join without invitation");
7307 return 0;
7308 }
7309
7310 if (!params->peer ||
7311 !(params->peer->dev_capab & P2P_DEV_CAPAB_INVITATION_PROCEDURE))
7312 return 0;
7313
7314 wpa_printf(MSG_DEBUG, "P2P: Static handover - invite peer " MACSTR
7315 " to join", MAC2STR(params->peer->p2p_device_addr));
7316
7317 wpa_s->global->p2p_invite_group = wpa_s;
7318 persistent = ssid->p2p_persistent_group &&
7319 wpas_p2p_get_persistent(wpa_s->parent,
7320 params->peer->p2p_device_addr,
7321 ssid->ssid, ssid->ssid_len);
7322 wpa_s->parent->pending_invite_ssid_id = -1;
7323
7324 return p2p_invite(wpa_s->global->p2p, params->peer->p2p_device_addr,
7325 P2P_INVITE_ROLE_ACTIVE_GO, wpa_s->own_addr,
7326 ssid->ssid, ssid->ssid_len, ssid->frequency,
7327 wpa_s->global->p2p_dev_addr, persistent, 0,
7328 wpa_s->parent->p2p_oob_dev_pw_id);
7329}
7330
7331
7332static int wpas_p2p_nfc_init_go_neg(struct wpa_supplicant *wpa_s,
7333 struct p2p_nfc_params *params,
7334 int forced_freq)
7335{
7336 wpa_printf(MSG_DEBUG, "P2P: Initiate GO Negotiation based on NFC "
7337 "connection handover");
7338 return wpas_p2p_connect(wpa_s, params->peer->p2p_device_addr, NULL,
7339 WPS_NFC, 0, 0, 0, 0, wpa_s->conf->p2p_go_intent,
7340 forced_freq, -1, 0, 1, 1);
7341}
7342
7343
7344static int wpas_p2p_nfc_resp_go_neg(struct wpa_supplicant *wpa_s,
7345 struct p2p_nfc_params *params,
7346 int forced_freq)
7347{
7348 int res;
7349
7350 wpa_printf(MSG_DEBUG, "P2P: Authorize GO Negotiation based on NFC "
7351 "connection handover");
7352 res = wpas_p2p_connect(wpa_s, params->peer->p2p_device_addr, NULL,
7353 WPS_NFC, 0, 0, 0, 1, wpa_s->conf->p2p_go_intent,
7354 forced_freq, -1, 0, 1, 1);
7355 if (res)
7356 return res;
7357
7358 res = wpas_p2p_listen(wpa_s, 60);
7359 if (res) {
7360 p2p_unauthorize(wpa_s->global->p2p,
7361 params->peer->p2p_device_addr);
7362 }
7363
7364 return res;
7365}
7366
7367
7368static int wpas_p2p_nfc_connection_handover(struct wpa_supplicant *wpa_s,
7369 const struct wpabuf *data,
7370 int sel, int tag, int forced_freq)
7371{
7372 const u8 *pos, *end;
7373 u16 len, id;
7374 struct p2p_nfc_params params;
7375 int res;
7376
7377 os_memset(&params, 0, sizeof(params));
7378 params.sel = sel;
7379
7380 wpa_hexdump_buf(MSG_DEBUG, "P2P: Received NFC tag payload", data);
7381
7382 pos = wpabuf_head(data);
7383 end = pos + wpabuf_len(data);
7384
7385 if (end - pos < 2) {
7386 wpa_printf(MSG_DEBUG, "P2P: Not enough data for Length of WSC "
7387 "attributes");
7388 return -1;
7389 }
7390 len = WPA_GET_BE16(pos);
7391 pos += 2;
7392 if (pos + len > end) {
7393 wpa_printf(MSG_DEBUG, "P2P: Not enough data for WSC "
7394 "attributes");
7395 return -1;
7396 }
7397 params.wsc_attr = pos;
7398 params.wsc_len = len;
7399 pos += len;
7400
7401 if (end - pos < 2) {
7402 wpa_printf(MSG_DEBUG, "P2P: Not enough data for Length of P2P "
7403 "attributes");
7404 return -1;
7405 }
7406 len = WPA_GET_BE16(pos);
7407 pos += 2;
7408 if (pos + len > end) {
7409 wpa_printf(MSG_DEBUG, "P2P: Not enough data for P2P "
7410 "attributes");
7411 return -1;
7412 }
7413 params.p2p_attr = pos;
7414 params.p2p_len = len;
7415 pos += len;
7416
7417 wpa_hexdump(MSG_DEBUG, "P2P: WSC attributes",
7418 params.wsc_attr, params.wsc_len);
7419 wpa_hexdump(MSG_DEBUG, "P2P: P2P attributes",
7420 params.p2p_attr, params.p2p_len);
7421 if (pos < end) {
7422 wpa_hexdump(MSG_DEBUG,
7423 "P2P: Ignored extra data after P2P attributes",
7424 pos, end - pos);
7425 }
7426
7427 res = p2p_process_nfc_connection_handover(wpa_s->global->p2p, &params);
7428 if (res)
7429 return res;
7430
7431 if (params.next_step == NO_ACTION)
7432 return 0;
7433
7434 if (params.next_step == BOTH_GO) {
7435 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_NFC_BOTH_GO "peer=" MACSTR,
7436 MAC2STR(params.peer->p2p_device_addr));
7437 return 0;
7438 }
7439
7440 if (params.next_step == PEER_CLIENT) {
7441 if (!is_zero_ether_addr(params.go_dev_addr)) {
7442 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_NFC_PEER_CLIENT
7443 "peer=" MACSTR " freq=%d go_dev_addr=" MACSTR
7444 " ssid=\"%s\"",
7445 MAC2STR(params.peer->p2p_device_addr),
7446 params.go_freq,
7447 MAC2STR(params.go_dev_addr),
7448 wpa_ssid_txt(params.go_ssid,
7449 params.go_ssid_len));
7450 } else {
7451 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_NFC_PEER_CLIENT
7452 "peer=" MACSTR " freq=%d",
7453 MAC2STR(params.peer->p2p_device_addr),
7454 params.go_freq);
7455 }
7456 return 0;
7457 }
7458
7459 if (wpas_p2p_cli_freq(wpa_s, NULL, NULL)) {
7460 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_NFC_WHILE_CLIENT "peer="
7461 MACSTR, MAC2STR(params.peer->p2p_device_addr));
7462 return 0;
7463 }
7464
7465 wpabuf_free(wpa_s->p2p_oob_dev_pw);
7466 wpa_s->p2p_oob_dev_pw = NULL;
7467
7468 if (params.oob_dev_pw_len < WPS_OOB_PUBKEY_HASH_LEN + 2) {
7469 wpa_printf(MSG_DEBUG, "P2P: No peer OOB Dev Pw "
7470 "received");
7471 return -1;
7472 }
7473
7474 id = WPA_GET_BE16(params.oob_dev_pw + WPS_OOB_PUBKEY_HASH_LEN);
7475 wpa_printf(MSG_DEBUG, "P2P: Peer OOB Dev Pw %u", id);
7476 wpa_hexdump(MSG_DEBUG, "P2P: Peer OOB Public Key hash",
7477 params.oob_dev_pw, WPS_OOB_PUBKEY_HASH_LEN);
7478 os_memcpy(wpa_s->p2p_peer_oob_pubkey_hash,
7479 params.oob_dev_pw, WPS_OOB_PUBKEY_HASH_LEN);
7480 wpa_s->p2p_peer_oob_pk_hash_known = 1;
7481
7482 if (tag) {
7483 if (id < 0x10) {
7484 wpa_printf(MSG_DEBUG, "P2P: Static handover - invalid "
7485 "peer OOB Device Password Id %u", id);
7486 return -1;
7487 }
7488 wpa_printf(MSG_DEBUG, "P2P: Static handover - use peer OOB "
7489 "Device Password Id %u", id);
7490 wpa_hexdump_key(MSG_DEBUG, "P2P: Peer OOB Device Password",
7491 params.oob_dev_pw + WPS_OOB_PUBKEY_HASH_LEN + 2,
7492 params.oob_dev_pw_len -
7493 WPS_OOB_PUBKEY_HASH_LEN - 2);
7494 wpa_s->p2p_oob_dev_pw_id = id;
7495 wpa_s->p2p_oob_dev_pw = wpabuf_alloc_copy(
7496 params.oob_dev_pw + WPS_OOB_PUBKEY_HASH_LEN + 2,
7497 params.oob_dev_pw_len -
7498 WPS_OOB_PUBKEY_HASH_LEN - 2);
7499 if (wpa_s->p2p_oob_dev_pw == NULL)
7500 return -1;
7501
7502 if (wpa_s->conf->wps_nfc_dh_pubkey == NULL &&
7503 wps_nfc_gen_dh(&wpa_s->conf->wps_nfc_dh_pubkey,
7504 &wpa_s->conf->wps_nfc_dh_privkey) < 0)
7505 return -1;
7506 } else {
7507 wpa_printf(MSG_DEBUG, "P2P: Using abbreviated WPS handshake "
7508 "without Device Password");
7509 wpa_s->p2p_oob_dev_pw_id = DEV_PW_NFC_CONNECTION_HANDOVER;
7510 }
7511
7512 switch (params.next_step) {
7513 case NO_ACTION:
7514 case BOTH_GO:
7515 case PEER_CLIENT:
7516 /* already covered above */
7517 return 0;
7518 case JOIN_GROUP:
7519 return wpas_p2p_nfc_join_group(wpa_s, &params);
7520 case AUTH_JOIN:
7521 return wpas_p2p_nfc_auth_join(wpa_s, &params, tag);
7522 case INIT_GO_NEG:
7523 return wpas_p2p_nfc_init_go_neg(wpa_s, &params, forced_freq);
7524 case RESP_GO_NEG:
7525 /* TODO: use own OOB Dev Pw */
7526 return wpas_p2p_nfc_resp_go_neg(wpa_s, &params, forced_freq);
7527 }
7528
7529 return -1;
7530}
7531
7532
7533int wpas_p2p_nfc_tag_process(struct wpa_supplicant *wpa_s,
7534 const struct wpabuf *data, int forced_freq)
7535{
7536 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
7537 return -1;
7538
7539 return wpas_p2p_nfc_connection_handover(wpa_s, data, 1, 1, forced_freq);
7540}
7541
7542
7543int wpas_p2p_nfc_report_handover(struct wpa_supplicant *wpa_s, int init,
7544 const struct wpabuf *req,
7545 const struct wpabuf *sel, int forced_freq)
7546{
7547 struct wpabuf *tmp;
7548 int ret;
7549
7550 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
7551 return -1;
7552
7553 wpa_printf(MSG_DEBUG, "NFC: P2P connection handover reported");
7554
7555 wpa_hexdump_ascii(MSG_DEBUG, "NFC: Req",
7556 wpabuf_head(req), wpabuf_len(req));
7557 wpa_hexdump_ascii(MSG_DEBUG, "NFC: Sel",
7558 wpabuf_head(sel), wpabuf_len(sel));
7559 if (forced_freq)
7560 wpa_printf(MSG_DEBUG, "NFC: Forced freq %d", forced_freq);
7561 tmp = ndef_parse_p2p(init ? sel : req);
7562 if (tmp == NULL) {
7563 wpa_printf(MSG_DEBUG, "P2P: Could not parse NDEF");
7564 return -1;
7565 }
7566
7567 ret = wpas_p2p_nfc_connection_handover(wpa_s, tmp, init, 0,
7568 forced_freq);
7569 wpabuf_free(tmp);
7570
7571 return ret;
7572}
7573
7574
7575int wpas_p2p_nfc_tag_enabled(struct wpa_supplicant *wpa_s, int enabled)
7576{
7577 const u8 *if_addr;
7578 int go_intent = wpa_s->conf->p2p_go_intent;
7579 struct wpa_supplicant *iface;
7580
7581 if (wpa_s->global->p2p == NULL)
7582 return -1;
7583
7584 if (!enabled) {
7585 wpa_printf(MSG_DEBUG, "P2P: Disable use of own NFC Tag");
7586 for (iface = wpa_s->global->ifaces; iface; iface = iface->next)
7587 {
7588 if (!iface->ap_iface)
7589 continue;
7590 hostapd_wps_nfc_token_disable(iface->ap_iface->bss[0]);
7591 }
7592 p2p_set_authorized_oob_dev_pw_id(wpa_s->global->p2p, 0,
7593 0, NULL);
7594 if (wpa_s->p2p_nfc_tag_enabled)
7595 wpas_p2p_remove_pending_group_interface(wpa_s);
7596 wpa_s->p2p_nfc_tag_enabled = 0;
7597 return 0;
7598 }
7599
7600 if (wpa_s->global->p2p_disabled)
7601 return -1;
7602
7603 if (wpa_s->conf->wps_nfc_dh_pubkey == NULL ||
7604 wpa_s->conf->wps_nfc_dh_privkey == NULL ||
7605 wpa_s->conf->wps_nfc_dev_pw == NULL ||
7606 wpa_s->conf->wps_nfc_dev_pw_id < 0x10) {
7607 wpa_printf(MSG_DEBUG, "P2P: NFC password token not configured "
7608 "to allow static handover cases");
7609 return -1;
7610 }
7611
7612 wpa_printf(MSG_DEBUG, "P2P: Enable use of own NFC Tag");
7613
7614 wpa_s->p2p_oob_dev_pw_id = wpa_s->conf->wps_nfc_dev_pw_id;
7615 wpabuf_free(wpa_s->p2p_oob_dev_pw);
7616 wpa_s->p2p_oob_dev_pw = wpabuf_dup(wpa_s->conf->wps_nfc_dev_pw);
7617 if (wpa_s->p2p_oob_dev_pw == NULL)
7618 return -1;
7619 wpa_s->p2p_peer_oob_pk_hash_known = 0;
7620
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -07007621 if (wpa_s->p2p_group_interface == P2P_GROUP_INTERFACE_GO ||
7622 wpa_s->p2p_group_interface == P2P_GROUP_INTERFACE_CLIENT) {
7623 /*
7624 * P2P Group Interface present and the command came on group
7625 * interface, so enable the token for the current interface.
7626 */
7627 wpa_s->create_p2p_iface = 0;
7628 } else {
7629 wpa_s->create_p2p_iface = wpas_p2p_create_iface(wpa_s);
7630 }
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08007631
7632 if (wpa_s->create_p2p_iface) {
7633 enum wpa_driver_if_type iftype;
7634 /* Prepare to add a new interface for the group */
7635 iftype = WPA_IF_P2P_GROUP;
7636 if (go_intent == 15)
7637 iftype = WPA_IF_P2P_GO;
7638 if (wpas_p2p_add_group_interface(wpa_s, iftype) < 0) {
7639 wpa_printf(MSG_ERROR, "P2P: Failed to allocate a new "
7640 "interface for the group");
7641 return -1;
7642 }
7643
7644 if_addr = wpa_s->pending_interface_addr;
7645 } else
7646 if_addr = wpa_s->own_addr;
7647
7648 wpa_s->p2p_nfc_tag_enabled = enabled;
7649
7650 for (iface = wpa_s->global->ifaces; iface; iface = iface->next) {
7651 struct hostapd_data *hapd;
7652 if (iface->ap_iface == NULL)
7653 continue;
7654 hapd = iface->ap_iface->bss[0];
7655 wpabuf_free(hapd->conf->wps_nfc_dh_pubkey);
7656 hapd->conf->wps_nfc_dh_pubkey =
7657 wpabuf_dup(wpa_s->conf->wps_nfc_dh_pubkey);
7658 wpabuf_free(hapd->conf->wps_nfc_dh_privkey);
7659 hapd->conf->wps_nfc_dh_privkey =
7660 wpabuf_dup(wpa_s->conf->wps_nfc_dh_privkey);
7661 wpabuf_free(hapd->conf->wps_nfc_dev_pw);
7662 hapd->conf->wps_nfc_dev_pw =
7663 wpabuf_dup(wpa_s->conf->wps_nfc_dev_pw);
7664 hapd->conf->wps_nfc_dev_pw_id = wpa_s->conf->wps_nfc_dev_pw_id;
7665
7666 if (hostapd_wps_nfc_token_enable(iface->ap_iface->bss[0]) < 0) {
7667 wpa_dbg(iface, MSG_DEBUG,
7668 "P2P: Failed to enable NFC Tag for GO");
7669 }
7670 }
7671 p2p_set_authorized_oob_dev_pw_id(
7672 wpa_s->global->p2p, wpa_s->conf->wps_nfc_dev_pw_id, go_intent,
7673 if_addr);
7674
7675 return 0;
7676}
7677
7678#endif /* CONFIG_WPS_NFC */