blob: 50ee1e08c79f7ef75157d357fbbab3707c528308 [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 Shmidt34af3062013-07-11 10:46:32 -070085#define P2P_MGMT_DEVICE_PREFIX "p2p-dev-"
86
Dmitry Shmidt61d9df32012-08-29 16:22:06 -070087enum p2p_group_removal_reason {
88 P2P_GROUP_REMOVAL_UNKNOWN,
89 P2P_GROUP_REMOVAL_SILENT,
90 P2P_GROUP_REMOVAL_FORMATION_FAILED,
91 P2P_GROUP_REMOVAL_REQUESTED,
92 P2P_GROUP_REMOVAL_IDLE_TIMEOUT,
93 P2P_GROUP_REMOVAL_UNAVAILABLE,
94 P2P_GROUP_REMOVAL_GO_ENDING_SESSION,
Dmitry Shmidt04f534e2013-12-09 15:50:16 -080095 P2P_GROUP_REMOVAL_PSK_FAILURE,
96 P2P_GROUP_REMOVAL_FREQ_CONFLICT
Dmitry Shmidt61d9df32012-08-29 16:22:06 -070097};
98
Jouni Malinendc7b7132012-09-14 12:53:47 -070099
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700100static void wpas_p2p_long_listen_timeout(void *eloop_ctx, void *timeout_ctx);
101static struct wpa_supplicant *
102wpas_p2p_get_group_iface(struct wpa_supplicant *wpa_s, int addr_allocated,
103 int go);
Dmitry Shmidtcf32e602014-01-28 10:57:39 -0800104static int wpas_p2p_join_start(struct wpa_supplicant *wpa_s, int freq,
105 const u8 *ssid, size_t ssid_len);
Dmitry Shmidt344abd32014-01-14 13:17:00 -0800106static void wpas_p2p_join_scan_req(struct wpa_supplicant *wpa_s, int freq,
107 const u8 *ssid, size_t ssid_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700108static void wpas_p2p_join_scan(void *eloop_ctx, void *timeout_ctx);
109static int wpas_p2p_join(struct wpa_supplicant *wpa_s, const u8 *iface_addr,
Dmitry Shmidt04949592012-07-19 12:16:46 -0700110 const u8 *dev_addr, enum p2p_wps_method wps_method,
Dmitry Shmidtcf32e602014-01-28 10:57:39 -0800111 int auto_join, int freq,
112 const u8 *ssid, size_t ssid_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700113static int wpas_p2p_create_iface(struct wpa_supplicant *wpa_s);
114static void wpas_p2p_cross_connect_setup(struct wpa_supplicant *wpa_s);
115static void wpas_p2p_group_idle_timeout(void *eloop_ctx, void *timeout_ctx);
116static void wpas_p2p_set_group_idle_timeout(struct wpa_supplicant *wpa_s);
Dmitry Shmidtf8623282013-02-20 14:34:59 -0800117static void wpas_p2p_group_formation_timeout(void *eloop_ctx,
118 void *timeout_ctx);
Dmitry Shmidt04f534e2013-12-09 15:50:16 -0800119static void wpas_p2p_group_freq_conflict(void *eloop_ctx, void *timeout_ctx);
Dmitry Shmidt04949592012-07-19 12:16:46 -0700120static void wpas_p2p_fallback_to_go_neg(struct wpa_supplicant *wpa_s,
121 int group_added);
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800122static int wpas_p2p_stop_find_oper(struct wpa_supplicant *wpa_s);
Dmitry Shmidtbd14a572014-02-18 10:33:49 -0800123static void wpas_stop_listen(void *ctx);
Dmitry Shmidt684785c2014-05-12 13:34:29 -0700124static void wpas_p2p_psk_failure_removal(void *eloop_ctx, void *timeout_ctx);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700125
126
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -0700127/*
128 * Get the number of concurrent channels that the HW can operate, but that are
129 * currently not in use by any of the wpa_supplicant interfaces.
130 */
131static int wpas_p2p_num_unused_channels(struct wpa_supplicant *wpa_s)
132{
133 int *freqs;
Dmitry Shmidtb96dad42013-11-05 10:07:29 -0800134 int num, unused;
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -0700135
136 freqs = os_calloc(wpa_s->num_multichan_concurrent, sizeof(int));
137 if (!freqs)
138 return -1;
139
140 num = get_shared_radio_freqs(wpa_s, freqs,
141 wpa_s->num_multichan_concurrent);
142 os_free(freqs);
143
Dmitry Shmidtb96dad42013-11-05 10:07:29 -0800144 unused = wpa_s->num_multichan_concurrent - num;
145 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: num_unused_channels: %d", unused);
146 return unused;
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -0700147}
148
149
150/*
151 * Get the frequencies that are currently in use by one or more of the virtual
152 * interfaces, and that are also valid for P2P operation.
153 */
154static int wpas_p2p_valid_oper_freqs(struct wpa_supplicant *wpa_s,
155 int *p2p_freqs, unsigned int len)
156{
157 int *freqs;
158 unsigned int num, i, j;
159
160 freqs = os_calloc(wpa_s->num_multichan_concurrent, sizeof(int));
161 if (!freqs)
162 return -1;
163
164 num = get_shared_radio_freqs(wpa_s, freqs,
165 wpa_s->num_multichan_concurrent);
166
167 os_memset(p2p_freqs, 0, sizeof(int) * len);
168
169 for (i = 0, j = 0; i < num && j < len; i++) {
170 if (p2p_supported_freq(wpa_s->global->p2p, freqs[i]))
171 p2p_freqs[j++] = freqs[i];
172 }
173
174 os_free(freqs);
175
Dmitry Shmidtb96dad42013-11-05 10:07:29 -0800176 dump_freq_array(wpa_s, "valid for P2P", p2p_freqs, j);
177
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -0700178 return j;
179}
180
181
Dmitry Shmidt700a1372013-03-15 14:14:44 -0700182static void wpas_p2p_set_own_freq_preference(struct wpa_supplicant *wpa_s,
183 int freq)
184{
185 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
186 return;
Dmitry Shmidta0d265f2013-11-19 13:13:41 -0800187 if (wpa_s->parent->conf->p2p_ignore_shared_freq &&
188 freq > 0 && wpa_s->num_multichan_concurrent > 1 &&
189 wpas_p2p_num_unused_channels(wpa_s) > 0) {
190 wpa_printf(MSG_DEBUG, "P2P: Ignore own channel preference %d MHz due to p2p_ignore_shared_freq=1 configuration",
191 freq);
Dmitry Shmidt700a1372013-03-15 14:14:44 -0700192 freq = 0;
Dmitry Shmidta0d265f2013-11-19 13:13:41 -0800193 }
Dmitry Shmidt700a1372013-03-15 14:14:44 -0700194 p2p_set_own_freq_preference(wpa_s->global->p2p, freq);
195}
196
197
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700198static void wpas_p2p_scan_res_handler(struct wpa_supplicant *wpa_s,
199 struct wpa_scan_results *scan_res)
200{
201 size_t i;
202
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800203 if (wpa_s->p2p_scan_work) {
204 struct wpa_radio_work *work = wpa_s->p2p_scan_work;
205 wpa_s->p2p_scan_work = NULL;
206 radio_work_done(work);
207 }
208
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700209 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
210 return;
211
212 wpa_printf(MSG_DEBUG, "P2P: Scan results received (%d BSS)",
213 (int) scan_res->num);
214
215 for (i = 0; i < scan_res->num; i++) {
216 struct wpa_scan_res *bss = scan_res->res[i];
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800217 struct os_reltime time_tmp_age, entry_ts;
Dmitry Shmidt96571392013-10-14 12:54:46 -0700218 const u8 *ies;
219 size_t ies_len;
220
Dmitry Shmidtf8623282013-02-20 14:34:59 -0800221 time_tmp_age.sec = bss->age / 1000;
222 time_tmp_age.usec = (bss->age % 1000) * 1000;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800223 os_reltime_sub(&scan_res->fetch_time, &time_tmp_age, &entry_ts);
Dmitry Shmidt96571392013-10-14 12:54:46 -0700224
225 ies = (const u8 *) (bss + 1);
226 ies_len = bss->ie_len;
227 if (bss->beacon_ie_len > 0 &&
228 !wpa_scan_get_vendor_ie(bss, P2P_IE_VENDOR_TYPE) &&
229 wpa_scan_get_vendor_ie_beacon(bss, P2P_IE_VENDOR_TYPE)) {
230 wpa_printf(MSG_DEBUG, "P2P: Use P2P IE(s) from Beacon frame since no P2P IE(s) in Probe Response frames received for "
231 MACSTR, MAC2STR(bss->bssid));
232 ies = ies + ies_len;
233 ies_len = bss->beacon_ie_len;
234 }
235
236
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700237 if (p2p_scan_res_handler(wpa_s->global->p2p, bss->bssid,
Dmitry Shmidtf8623282013-02-20 14:34:59 -0800238 bss->freq, &entry_ts, bss->level,
Dmitry Shmidt96571392013-10-14 12:54:46 -0700239 ies, ies_len) > 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700240 break;
241 }
242
243 p2p_scan_res_handled(wpa_s->global->p2p);
244}
245
246
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800247static void wpas_p2p_trigger_scan_cb(struct wpa_radio_work *work, int deinit)
248{
249 struct wpa_supplicant *wpa_s = work->wpa_s;
250 struct wpa_driver_scan_params *params = work->ctx;
251 int ret;
252
253 if (deinit) {
Dmitry Shmidtbd14a572014-02-18 10:33:49 -0800254 if (!work->started) {
255 wpa_scan_free_params(params);
256 return;
257 }
258
259 wpa_s->p2p_scan_work = NULL;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800260 return;
261 }
262
263 ret = wpa_drv_scan(wpa_s, params);
264 wpa_scan_free_params(params);
265 work->ctx = NULL;
266 if (ret) {
267 radio_work_done(work);
268 return;
269 }
270
271 os_get_reltime(&wpa_s->scan_trigger_time);
272 wpa_s->scan_res_handler = wpas_p2p_scan_res_handler;
273 wpa_s->own_scan_requested = 1;
274 wpa_s->p2p_scan_work = work;
275}
276
277
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700278static int wpas_p2p_scan(void *ctx, enum p2p_scan_type type, int freq,
279 unsigned int num_req_dev_types,
Dmitry Shmidt04949592012-07-19 12:16:46 -0700280 const u8 *req_dev_types, const u8 *dev_id, u16 pw_id)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700281{
282 struct wpa_supplicant *wpa_s = ctx;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800283 struct wpa_driver_scan_params *params = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700284 struct wpabuf *wps_ie, *ies;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800285 size_t ielen;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800286 u8 *n;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700287
288 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
289 return -1;
290
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800291 if (wpa_s->p2p_scan_work) {
292 wpa_dbg(wpa_s, MSG_INFO, "P2P: Reject scan trigger since one is already pending");
293 return -1;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700294 }
295
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800296 params = os_zalloc(sizeof(*params));
297 if (params == NULL)
298 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700299
300 /* P2P Wildcard SSID */
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800301 params->num_ssids = 1;
302 n = os_malloc(P2P_WILDCARD_SSID_LEN);
303 if (n == NULL)
304 goto fail;
305 os_memcpy(n, P2P_WILDCARD_SSID, P2P_WILDCARD_SSID_LEN);
306 params->ssids[0].ssid = n;
307 params->ssids[0].ssid_len = P2P_WILDCARD_SSID_LEN;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700308
309 wpa_s->wps->dev.p2p = 1;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700310 wps_ie = wps_build_probe_req_ie(pw_id, &wpa_s->wps->dev,
311 wpa_s->wps->uuid, WPS_REQ_ENROLLEE,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700312 num_req_dev_types, req_dev_types);
313 if (wps_ie == NULL)
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800314 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700315
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800316 ielen = p2p_scan_ie_buf_len(wpa_s->global->p2p);
317 ies = wpabuf_alloc(wpabuf_len(wps_ie) + ielen);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700318 if (ies == NULL) {
319 wpabuf_free(wps_ie);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800320 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700321 }
322 wpabuf_put_buf(ies, wps_ie);
323 wpabuf_free(wps_ie);
324
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -0800325 p2p_scan_ie(wpa_s->global->p2p, ies, dev_id);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700326
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800327 params->p2p_probe = 1;
328 n = os_malloc(wpabuf_len(ies));
329 if (n == NULL) {
330 wpabuf_free(ies);
331 goto fail;
332 }
333 os_memcpy(n, wpabuf_head(ies), wpabuf_len(ies));
334 params->extra_ies = n;
335 params->extra_ies_len = wpabuf_len(ies);
336 wpabuf_free(ies);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700337
338 switch (type) {
339 case P2P_SCAN_SOCIAL:
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800340 params->freqs = os_malloc(4 * sizeof(int));
341 if (params->freqs == NULL)
342 goto fail;
343 params->freqs[0] = 2412;
344 params->freqs[1] = 2437;
345 params->freqs[2] = 2462;
346 params->freqs[3] = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700347 break;
348 case P2P_SCAN_FULL:
349 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700350 case P2P_SCAN_SOCIAL_PLUS_ONE:
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800351 params->freqs = os_malloc(5 * sizeof(int));
352 if (params->freqs == NULL)
353 goto fail;
354 params->freqs[0] = 2412;
355 params->freqs[1] = 2437;
356 params->freqs[2] = 2462;
357 params->freqs[3] = freq;
358 params->freqs[4] = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700359 break;
360 }
361
Dmitry Shmidtbd14a572014-02-18 10:33:49 -0800362 radio_remove_works(wpa_s, "p2p-scan", 0);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800363 if (radio_add_work(wpa_s, 0, "p2p-scan", 0, wpas_p2p_trigger_scan_cb,
364 params) < 0)
365 goto fail;
366 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700367
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800368fail:
369 wpa_scan_free_params(params);
370 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700371}
372
373
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700374static enum wpa_driver_if_type wpas_p2p_if_type(int p2p_group_interface)
375{
376 switch (p2p_group_interface) {
377 case P2P_GROUP_INTERFACE_PENDING:
378 return WPA_IF_P2P_GROUP;
379 case P2P_GROUP_INTERFACE_GO:
380 return WPA_IF_P2P_GO;
381 case P2P_GROUP_INTERFACE_CLIENT:
382 return WPA_IF_P2P_CLIENT;
383 }
384
385 return WPA_IF_P2P_GROUP;
386}
387
388
389static struct wpa_supplicant * wpas_get_p2p_group(struct wpa_supplicant *wpa_s,
390 const u8 *ssid,
391 size_t ssid_len, int *go)
392{
393 struct wpa_ssid *s;
394
395 for (wpa_s = wpa_s->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
396 for (s = wpa_s->conf->ssid; s; s = s->next) {
397 if (s->disabled != 0 || !s->p2p_group ||
398 s->ssid_len != ssid_len ||
399 os_memcmp(ssid, s->ssid, ssid_len) != 0)
400 continue;
401 if (s->mode == WPAS_MODE_P2P_GO &&
402 s != wpa_s->current_ssid)
403 continue;
404 if (go)
405 *go = s->mode == WPAS_MODE_P2P_GO;
406 return wpa_s;
407 }
408 }
409
410 return NULL;
411}
412
413
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700414static int wpas_p2p_group_delete(struct wpa_supplicant *wpa_s,
415 enum p2p_group_removal_reason removal_reason)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700416{
417 struct wpa_ssid *ssid;
418 char *gtype;
419 const char *reason;
420
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700421 ssid = wpa_s->current_ssid;
422 if (ssid == NULL) {
423 /*
424 * The current SSID was not known, but there may still be a
Dmitry Shmidtaa532512012-09-24 10:35:31 -0700425 * pending P2P group interface waiting for provisioning or a
426 * P2P group that is trying to reconnect.
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700427 */
428 ssid = wpa_s->conf->ssid;
429 while (ssid) {
Jouni Malinen9d712832012-10-05 11:01:57 -0700430 if (ssid->p2p_group && ssid->disabled != 2)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700431 break;
432 ssid = ssid->next;
433 }
Jouni Malinen5c44edb2012-08-31 21:35:32 +0300434 if (ssid == NULL &&
435 wpa_s->p2p_group_interface == NOT_P2P_GROUP_INTERFACE)
436 {
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700437 wpa_printf(MSG_ERROR, "P2P: P2P group interface "
438 "not found");
439 return -1;
440 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700441 }
442 if (wpa_s->p2p_group_interface == P2P_GROUP_INTERFACE_GO)
443 gtype = "GO";
444 else if (wpa_s->p2p_group_interface == P2P_GROUP_INTERFACE_CLIENT ||
445 (ssid && ssid->mode == WPAS_MODE_INFRA)) {
446 wpa_s->reassociate = 0;
447 wpa_s->disconnected = 1;
448 wpa_supplicant_deauthenticate(wpa_s,
449 WLAN_REASON_DEAUTH_LEAVING);
450 gtype = "client";
451 } else
452 gtype = "GO";
453 if (wpa_s->cross_connect_in_use) {
454 wpa_s->cross_connect_in_use = 0;
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -0700455 wpa_msg_global(wpa_s->parent, MSG_INFO,
456 P2P_EVENT_CROSS_CONNECT_DISABLE "%s %s",
457 wpa_s->ifname, wpa_s->cross_connect_uplink);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700458 }
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700459 switch (removal_reason) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700460 case P2P_GROUP_REMOVAL_REQUESTED:
461 reason = " reason=REQUESTED";
462 break;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700463 case P2P_GROUP_REMOVAL_FORMATION_FAILED:
464 reason = " reason=FORMATION_FAILED";
465 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700466 case P2P_GROUP_REMOVAL_IDLE_TIMEOUT:
467 reason = " reason=IDLE";
468 break;
469 case P2P_GROUP_REMOVAL_UNAVAILABLE:
470 reason = " reason=UNAVAILABLE";
471 break;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700472 case P2P_GROUP_REMOVAL_GO_ENDING_SESSION:
473 reason = " reason=GO_ENDING_SESSION";
474 break;
Dmitry Shmidt391c59f2013-09-03 12:16:28 -0700475 case P2P_GROUP_REMOVAL_PSK_FAILURE:
476 reason = " reason=PSK_FAILURE";
477 break;
Dmitry Shmidt04f534e2013-12-09 15:50:16 -0800478 case P2P_GROUP_REMOVAL_FREQ_CONFLICT:
479 reason = " reason=FREQ_CONFLICT";
480 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700481 default:
482 reason = "";
483 break;
484 }
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700485 if (removal_reason != P2P_GROUP_REMOVAL_SILENT) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -0700486 wpa_msg_global(wpa_s->parent, MSG_INFO,
487 P2P_EVENT_GROUP_REMOVED "%s %s%s",
488 wpa_s->ifname, gtype, reason);
Dmitry Shmidt04949592012-07-19 12:16:46 -0700489 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700490
Dmitry Shmidt04f534e2013-12-09 15:50:16 -0800491 if (eloop_cancel_timeout(wpas_p2p_group_freq_conflict, wpa_s, NULL) > 0)
492 wpa_printf(MSG_DEBUG, "P2P: Cancelled P2P group freq_conflict timeout");
Dmitry Shmidt04949592012-07-19 12:16:46 -0700493 if (eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL) > 0)
494 wpa_printf(MSG_DEBUG, "P2P: Cancelled P2P group idle timeout");
Dmitry Shmidtf8623282013-02-20 14:34:59 -0800495 if (eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
Dmitry Shmidt2f023192013-03-12 12:44:17 -0700496 wpa_s->parent, NULL) > 0) {
Dmitry Shmidtf8623282013-02-20 14:34:59 -0800497 wpa_printf(MSG_DEBUG, "P2P: Cancelled P2P group formation "
498 "timeout");
Dmitry Shmidt2f023192013-03-12 12:44:17 -0700499 wpa_s->p2p_in_provisioning = 0;
500 }
Dmitry Shmidt04949592012-07-19 12:16:46 -0700501
Dmitry Shmidt15907092014-03-25 10:42:57 -0700502 wpa_s->p2p_in_invitation = 0;
503
Dmitry Shmidt96571392013-10-14 12:54:46 -0700504 /*
505 * Make sure wait for the first client does not remain active after the
506 * group has been removed.
507 */
508 wpa_s->global->p2p_go_wait_client.sec = 0;
509
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700510 if (removal_reason != P2P_GROUP_REMOVAL_SILENT && ssid)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700511 wpas_notify_p2p_group_removed(wpa_s, ssid, gtype);
512
513 if (wpa_s->p2p_group_interface != NOT_P2P_GROUP_INTERFACE) {
514 struct wpa_global *global;
515 char *ifname;
516 enum wpa_driver_if_type type;
517 wpa_printf(MSG_DEBUG, "P2P: Remove group interface %s",
518 wpa_s->ifname);
519 global = wpa_s->global;
520 ifname = os_strdup(wpa_s->ifname);
521 type = wpas_p2p_if_type(wpa_s->p2p_group_interface);
Dmitry Shmidte15c7b52011-08-03 15:04:35 -0700522 wpa_supplicant_remove_iface(wpa_s->global, wpa_s, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700523 wpa_s = global->ifaces;
524 if (wpa_s && ifname)
525 wpa_drv_if_remove(wpa_s, type, ifname);
526 os_free(ifname);
Jouni Malinen2b89da82012-08-31 22:04:41 +0300527 return 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700528 }
529
Dmitry Shmidt96571392013-10-14 12:54:46 -0700530 if (!wpa_s->p2p_go_group_formation_completed) {
531 wpa_s->global->p2p_group_formation = NULL;
532 wpa_s->p2p_in_provisioning = 0;
533 }
534
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800535 wpa_s->show_group_started = 0;
536 os_free(wpa_s->go_params);
537 wpa_s->go_params = NULL;
538
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800539 wpa_s->waiting_presence_resp = 0;
540
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700541 wpa_printf(MSG_DEBUG, "P2P: Remove temporary group network");
542 if (ssid && (ssid->p2p_group ||
543 ssid->mode == WPAS_MODE_P2P_GROUP_FORMATION ||
544 (ssid->key_mgmt & WPA_KEY_MGMT_WPS))) {
545 int id = ssid->id;
Jouni Malinen75ecf522011-06-27 15:19:46 -0700546 if (ssid == wpa_s->current_ssid) {
547 wpa_sm_set_config(wpa_s->wpa, NULL);
548 eapol_sm_notify_config(wpa_s->eapol, NULL, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700549 wpa_s->current_ssid = NULL;
Jouni Malinen75ecf522011-06-27 15:19:46 -0700550 }
551 /*
552 * Networks objects created during any P2P activities are not
553 * exposed out as they might/will confuse certain non-P2P aware
554 * applications since these network objects won't behave like
555 * regular ones.
556 *
557 * Likewise, we don't send out network removed signals for such
558 * network objects.
559 */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700560 wpa_config_remove_network(wpa_s->conf, id);
561 wpa_supplicant_clear_status(wpa_s);
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -0800562 wpa_supplicant_cancel_sched_scan(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700563 } else {
564 wpa_printf(MSG_DEBUG, "P2P: Temporary group network not "
565 "found");
566 }
Dmitry Shmidt04949592012-07-19 12:16:46 -0700567 if (wpa_s->ap_iface)
568 wpa_supplicant_ap_deinit(wpa_s);
569 else
570 wpa_drv_deinit_p2p_cli(wpa_s);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700571
572 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700573}
574
575
576static int wpas_p2p_persistent_group(struct wpa_supplicant *wpa_s,
577 u8 *go_dev_addr,
578 const u8 *ssid, size_t ssid_len)
579{
580 struct wpa_bss *bss;
581 const u8 *bssid;
582 struct wpabuf *p2p;
583 u8 group_capab;
584 const u8 *addr;
585
586 if (wpa_s->go_params)
587 bssid = wpa_s->go_params->peer_interface_addr;
588 else
589 bssid = wpa_s->bssid;
590
591 bss = wpa_bss_get(wpa_s, bssid, ssid, ssid_len);
Dmitry Shmidtcf32e602014-01-28 10:57:39 -0800592 if (bss == NULL && wpa_s->go_params &&
593 !is_zero_ether_addr(wpa_s->go_params->peer_device_addr))
594 bss = wpa_bss_get_p2p_dev_addr(
595 wpa_s, wpa_s->go_params->peer_device_addr);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700596 if (bss == NULL) {
597 u8 iface_addr[ETH_ALEN];
598 if (p2p_get_interface_addr(wpa_s->global->p2p, bssid,
599 iface_addr) == 0)
600 bss = wpa_bss_get(wpa_s, iface_addr, ssid, ssid_len);
601 }
602 if (bss == NULL) {
603 wpa_printf(MSG_DEBUG, "P2P: Could not figure out whether "
604 "group is persistent - BSS " MACSTR " not found",
605 MAC2STR(bssid));
606 return 0;
607 }
608
609 p2p = wpa_bss_get_vendor_ie_multi(bss, P2P_IE_VENDOR_TYPE);
Dmitry Shmidt96571392013-10-14 12:54:46 -0700610 if (p2p == NULL)
611 p2p = wpa_bss_get_vendor_ie_multi_beacon(bss,
612 P2P_IE_VENDOR_TYPE);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700613 if (p2p == NULL) {
614 wpa_printf(MSG_DEBUG, "P2P: Could not figure out whether "
615 "group is persistent - BSS " MACSTR
616 " did not include P2P IE", MAC2STR(bssid));
617 wpa_hexdump(MSG_DEBUG, "P2P: Probe Response IEs",
618 (u8 *) (bss + 1), bss->ie_len);
619 wpa_hexdump(MSG_DEBUG, "P2P: Beacon IEs",
620 ((u8 *) bss + 1) + bss->ie_len,
621 bss->beacon_ie_len);
622 return 0;
623 }
624
625 group_capab = p2p_get_group_capab(p2p);
626 addr = p2p_get_go_dev_addr(p2p);
627 wpa_printf(MSG_DEBUG, "P2P: Checking whether group is persistent: "
628 "group_capab=0x%x", group_capab);
629 if (addr) {
630 os_memcpy(go_dev_addr, addr, ETH_ALEN);
631 wpa_printf(MSG_DEBUG, "P2P: GO Device Address " MACSTR,
632 MAC2STR(addr));
633 } else
634 os_memset(go_dev_addr, 0, ETH_ALEN);
635 wpabuf_free(p2p);
636
637 wpa_printf(MSG_DEBUG, "P2P: BSS " MACSTR " group_capab=0x%x "
638 "go_dev_addr=" MACSTR,
639 MAC2STR(bssid), group_capab, MAC2STR(go_dev_addr));
640
641 return group_capab & P2P_GROUP_CAPAB_PERSISTENT_GROUP;
642}
643
644
Jouni Malinen75ecf522011-06-27 15:19:46 -0700645static int wpas_p2p_store_persistent_group(struct wpa_supplicant *wpa_s,
646 struct wpa_ssid *ssid,
647 const u8 *go_dev_addr)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700648{
649 struct wpa_ssid *s;
650 int changed = 0;
651
652 wpa_printf(MSG_DEBUG, "P2P: Storing credentials for a persistent "
653 "group (GO Dev Addr " MACSTR ")", MAC2STR(go_dev_addr));
654 for (s = wpa_s->conf->ssid; s; s = s->next) {
655 if (s->disabled == 2 &&
656 os_memcmp(go_dev_addr, s->bssid, ETH_ALEN) == 0 &&
657 s->ssid_len == ssid->ssid_len &&
658 os_memcmp(ssid->ssid, s->ssid, ssid->ssid_len) == 0)
659 break;
660 }
661
662 if (s) {
663 wpa_printf(MSG_DEBUG, "P2P: Update existing persistent group "
664 "entry");
665 if (ssid->passphrase && !s->passphrase)
666 changed = 1;
667 else if (ssid->passphrase && s->passphrase &&
668 os_strcmp(ssid->passphrase, s->passphrase) != 0)
669 changed = 1;
670 } else {
671 wpa_printf(MSG_DEBUG, "P2P: Create a new persistent group "
672 "entry");
673 changed = 1;
674 s = wpa_config_add_network(wpa_s->conf);
675 if (s == NULL)
Jouni Malinen75ecf522011-06-27 15:19:46 -0700676 return -1;
677
678 /*
679 * Instead of network_added we emit persistent_group_added
680 * notification. Also to keep the defense checks in
681 * persistent_group obj registration method, we set the
682 * relevant flags in s to designate it as a persistent group.
683 */
684 s->p2p_group = 1;
685 s->p2p_persistent_group = 1;
686 wpas_notify_persistent_group_added(wpa_s, s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700687 wpa_config_set_network_defaults(s);
688 }
689
690 s->p2p_group = 1;
691 s->p2p_persistent_group = 1;
692 s->disabled = 2;
693 s->bssid_set = 1;
694 os_memcpy(s->bssid, go_dev_addr, ETH_ALEN);
695 s->mode = ssid->mode;
696 s->auth_alg = WPA_AUTH_ALG_OPEN;
697 s->key_mgmt = WPA_KEY_MGMT_PSK;
698 s->proto = WPA_PROTO_RSN;
699 s->pairwise_cipher = WPA_CIPHER_CCMP;
700 s->export_keys = 1;
701 if (ssid->passphrase) {
702 os_free(s->passphrase);
703 s->passphrase = os_strdup(ssid->passphrase);
704 }
705 if (ssid->psk_set) {
706 s->psk_set = 1;
707 os_memcpy(s->psk, ssid->psk, 32);
708 }
709 if (s->passphrase && !s->psk_set)
710 wpa_config_update_psk(s);
711 if (s->ssid == NULL || s->ssid_len < ssid->ssid_len) {
712 os_free(s->ssid);
713 s->ssid = os_malloc(ssid->ssid_len);
714 }
715 if (s->ssid) {
716 s->ssid_len = ssid->ssid_len;
717 os_memcpy(s->ssid, ssid->ssid, s->ssid_len);
718 }
Dmitry Shmidt391c59f2013-09-03 12:16:28 -0700719 if (ssid->mode == WPAS_MODE_P2P_GO && wpa_s->global->add_psk) {
720 dl_list_add(&s->psk_list, &wpa_s->global->add_psk->list);
721 wpa_s->global->add_psk = NULL;
722 changed = 1;
723 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700724
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700725 if (changed && wpa_s->conf->update_config &&
726 wpa_config_write(wpa_s->confname, wpa_s->conf)) {
727 wpa_printf(MSG_DEBUG, "P2P: Failed to update configuration");
728 }
Jouni Malinen75ecf522011-06-27 15:19:46 -0700729
730 return s->id;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700731}
732
733
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800734static void wpas_p2p_add_persistent_group_client(struct wpa_supplicant *wpa_s,
735 const u8 *addr)
736{
737 struct wpa_ssid *ssid, *s;
738 u8 *n;
739 size_t i;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700740 int found = 0;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800741
742 ssid = wpa_s->current_ssid;
743 if (ssid == NULL || ssid->mode != WPAS_MODE_P2P_GO ||
744 !ssid->p2p_persistent_group)
745 return;
746
747 for (s = wpa_s->parent->conf->ssid; s; s = s->next) {
748 if (s->disabled != 2 || s->mode != WPAS_MODE_P2P_GO)
749 continue;
750
751 if (s->ssid_len == ssid->ssid_len &&
752 os_memcmp(s->ssid, ssid->ssid, s->ssid_len) == 0)
753 break;
754 }
755
756 if (s == NULL)
757 return;
758
759 for (i = 0; s->p2p_client_list && i < s->num_p2p_clients; i++) {
760 if (os_memcmp(s->p2p_client_list + i * ETH_ALEN, addr,
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700761 ETH_ALEN) != 0)
762 continue;
763
764 if (i == s->num_p2p_clients - 1)
765 return; /* already the most recent entry */
766
767 /* move the entry to mark it most recent */
768 os_memmove(s->p2p_client_list + i * ETH_ALEN,
769 s->p2p_client_list + (i + 1) * ETH_ALEN,
770 (s->num_p2p_clients - i - 1) * ETH_ALEN);
771 os_memcpy(s->p2p_client_list +
772 (s->num_p2p_clients - 1) * ETH_ALEN, addr, ETH_ALEN);
773 found = 1;
774 break;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800775 }
776
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700777 if (!found && s->num_p2p_clients < P2P_MAX_STORED_CLIENTS) {
778 n = os_realloc_array(s->p2p_client_list,
779 s->num_p2p_clients + 1, ETH_ALEN);
780 if (n == NULL)
781 return;
782 os_memcpy(n + s->num_p2p_clients * ETH_ALEN, addr, ETH_ALEN);
783 s->p2p_client_list = n;
784 s->num_p2p_clients++;
785 } else if (!found) {
786 /* Not enough room for an additional entry - drop the oldest
787 * entry */
788 os_memmove(s->p2p_client_list,
789 s->p2p_client_list + ETH_ALEN,
790 (s->num_p2p_clients - 1) * ETH_ALEN);
791 os_memcpy(s->p2p_client_list +
792 (s->num_p2p_clients - 1) * ETH_ALEN,
793 addr, ETH_ALEN);
794 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800795
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800796 if (wpa_s->parent->conf->update_config &&
797 wpa_config_write(wpa_s->parent->confname, wpa_s->parent->conf))
798 wpa_printf(MSG_DEBUG, "P2P: Failed to update configuration");
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800799}
800
801
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700802static void wpas_group_formation_completed(struct wpa_supplicant *wpa_s,
803 int success)
804{
805 struct wpa_ssid *ssid;
806 const char *ssid_txt;
807 int client;
808 int persistent;
809 u8 go_dev_addr[ETH_ALEN];
Jouni Malinen75ecf522011-06-27 15:19:46 -0700810 int network_id = -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700811
812 /*
813 * This callback is likely called for the main interface. Update wpa_s
814 * to use the group interface if a new interface was created for the
815 * group.
816 */
817 if (wpa_s->global->p2p_group_formation)
818 wpa_s = wpa_s->global->p2p_group_formation;
Dmitry Shmidt56052862013-10-04 10:23:25 -0700819 if (wpa_s->p2p_go_group_formation_completed) {
820 wpa_s->global->p2p_group_formation = NULL;
821 wpa_s->p2p_in_provisioning = 0;
822 }
Dmitry Shmidt21de2142014-04-08 10:50:52 -0700823 wpa_s->p2p_in_invitation = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700824
825 if (!success) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -0700826 wpa_msg_global(wpa_s->parent, MSG_INFO,
827 P2P_EVENT_GROUP_FORMATION_FAILURE);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700828 wpas_p2p_group_delete(wpa_s,
829 P2P_GROUP_REMOVAL_FORMATION_FAILED);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700830 return;
831 }
832
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -0700833 wpa_msg_global(wpa_s->parent, MSG_INFO,
834 P2P_EVENT_GROUP_FORMATION_SUCCESS);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700835
836 ssid = wpa_s->current_ssid;
837 if (ssid && ssid->mode == WPAS_MODE_P2P_GROUP_FORMATION) {
838 ssid->mode = WPAS_MODE_P2P_GO;
839 p2p_group_notif_formation_done(wpa_s->p2p_group);
840 wpa_supplicant_ap_mac_addr_filter(wpa_s, NULL);
841 }
842
843 persistent = 0;
844 if (ssid) {
845 ssid_txt = wpa_ssid_txt(ssid->ssid, ssid->ssid_len);
846 client = ssid->mode == WPAS_MODE_INFRA;
847 if (ssid->mode == WPAS_MODE_P2P_GO) {
848 persistent = ssid->p2p_persistent_group;
Dmitry Shmidt497c1d52011-07-21 15:19:46 -0700849 os_memcpy(go_dev_addr, wpa_s->global->p2p_dev_addr,
850 ETH_ALEN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700851 } else
852 persistent = wpas_p2p_persistent_group(wpa_s,
853 go_dev_addr,
854 ssid->ssid,
855 ssid->ssid_len);
856 } else {
857 ssid_txt = "";
858 client = wpa_s->p2p_group_interface ==
859 P2P_GROUP_INTERFACE_CLIENT;
860 os_memset(go_dev_addr, 0, ETH_ALEN);
861 }
862
863 wpa_s->show_group_started = 0;
864 if (client) {
865 /*
866 * Indicate event only after successfully completed 4-way
867 * handshake, i.e., when the interface is ready for data
868 * packets.
869 */
870 wpa_s->show_group_started = 1;
871 } else if (ssid && ssid->passphrase == NULL && ssid->psk_set) {
872 char psk[65];
873 wpa_snprintf_hex(psk, sizeof(psk), ssid->psk, 32);
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -0700874 wpa_msg_global(wpa_s->parent, MSG_INFO, P2P_EVENT_GROUP_STARTED
875 "%s GO ssid=\"%s\" freq=%d psk=%s go_dev_addr="
876 MACSTR "%s",
877 wpa_s->ifname, ssid_txt, ssid->frequency, psk,
878 MAC2STR(go_dev_addr),
879 persistent ? " [PERSISTENT]" : "");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700880 wpas_p2p_cross_connect_setup(wpa_s);
881 wpas_p2p_set_group_idle_timeout(wpa_s);
882 } else {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -0700883 wpa_msg_global(wpa_s->parent, MSG_INFO, P2P_EVENT_GROUP_STARTED
884 "%s GO ssid=\"%s\" freq=%d passphrase=\"%s\" "
885 "go_dev_addr=" MACSTR "%s",
886 wpa_s->ifname, ssid_txt,
887 ssid ? ssid->frequency : 0,
888 ssid && ssid->passphrase ? ssid->passphrase : "",
889 MAC2STR(go_dev_addr),
890 persistent ? " [PERSISTENT]" : "");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700891 wpas_p2p_cross_connect_setup(wpa_s);
892 wpas_p2p_set_group_idle_timeout(wpa_s);
893 }
894
895 if (persistent)
Jouni Malinen75ecf522011-06-27 15:19:46 -0700896 network_id = wpas_p2p_store_persistent_group(wpa_s->parent,
897 ssid, go_dev_addr);
Dmitry Shmidt391c59f2013-09-03 12:16:28 -0700898 else {
899 os_free(wpa_s->global->add_psk);
900 wpa_s->global->add_psk = NULL;
901 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800902 if (network_id < 0 && ssid)
Jouni Malinen75ecf522011-06-27 15:19:46 -0700903 network_id = ssid->id;
Dmitry Shmidt92c368d2013-08-29 12:37:21 -0700904 if (!client) {
Jouni Malinen75ecf522011-06-27 15:19:46 -0700905 wpas_notify_p2p_group_started(wpa_s, ssid, network_id, 0);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800906 os_get_reltime(&wpa_s->global->p2p_go_wait_client);
Dmitry Shmidt92c368d2013-08-29 12:37:21 -0700907 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700908}
909
910
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800911struct send_action_work {
912 unsigned int freq;
913 u8 dst[ETH_ALEN];
914 u8 src[ETH_ALEN];
915 u8 bssid[ETH_ALEN];
916 size_t len;
917 unsigned int wait_time;
918 u8 buf[0];
919};
920
921
922static void wpas_p2p_send_action_work_timeout(void *eloop_ctx,
923 void *timeout_ctx)
924{
925 struct wpa_supplicant *wpa_s = eloop_ctx;
926
927 if (!wpa_s->p2p_send_action_work)
928 return;
929
930 wpa_printf(MSG_DEBUG, "P2P: Send Action frame radio work timed out");
931 os_free(wpa_s->p2p_send_action_work->ctx);
932 radio_work_done(wpa_s->p2p_send_action_work);
933 wpa_s->p2p_send_action_work = NULL;
934}
935
936
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800937static void wpas_p2p_send_action_tx_status(struct wpa_supplicant *wpa_s,
938 unsigned int freq,
939 const u8 *dst, const u8 *src,
940 const u8 *bssid,
941 const u8 *data, size_t data_len,
942 enum offchannel_send_action_result
943 result)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700944{
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800945 enum p2p_send_action_result res = P2P_SEND_ACTION_SUCCESS;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700946
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800947 if (wpa_s->p2p_send_action_work) {
948 struct send_action_work *awork;
949 awork = wpa_s->p2p_send_action_work->ctx;
950 if (awork->wait_time == 0) {
951 os_free(awork);
952 radio_work_done(wpa_s->p2p_send_action_work);
953 wpa_s->p2p_send_action_work = NULL;
954 } else {
955 /*
956 * In theory, this should not be needed, but number of
957 * places in the P2P code is still using non-zero wait
958 * time for the last Action frame in the sequence and
959 * some of these do not call send_action_done().
960 */
961 eloop_cancel_timeout(wpas_p2p_send_action_work_timeout,
962 wpa_s, NULL);
963 eloop_register_timeout(
964 0, awork->wait_time * 1000,
965 wpas_p2p_send_action_work_timeout,
966 wpa_s, NULL);
967 }
968 }
969
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700970 if (wpa_s->global->p2p == NULL || wpa_s->global->p2p_disabled)
971 return;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700972
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800973 switch (result) {
974 case OFFCHANNEL_SEND_ACTION_SUCCESS:
975 res = P2P_SEND_ACTION_SUCCESS;
976 break;
977 case OFFCHANNEL_SEND_ACTION_NO_ACK:
978 res = P2P_SEND_ACTION_NO_ACK;
979 break;
980 case OFFCHANNEL_SEND_ACTION_FAILED:
981 res = P2P_SEND_ACTION_FAILED;
982 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700983 }
984
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800985 p2p_send_action_cb(wpa_s->global->p2p, freq, dst, src, bssid, res);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700986
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -0800987 if (result != OFFCHANNEL_SEND_ACTION_SUCCESS &&
988 wpa_s->pending_pd_before_join &&
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800989 (os_memcmp(dst, wpa_s->pending_join_dev_addr, ETH_ALEN) == 0 ||
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800990 os_memcmp(dst, wpa_s->pending_join_iface_addr, ETH_ALEN) == 0) &&
991 wpa_s->p2p_fallback_to_go_neg) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700992 wpa_s->pending_pd_before_join = 0;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800993 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: No ACK for PD Req "
994 "during p2p_connect-auto");
995 wpas_p2p_fallback_to_go_neg(wpa_s, 0);
996 return;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700997 }
998}
999
1000
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001001static void wpas_send_action_cb(struct wpa_radio_work *work, int deinit)
1002{
1003 struct wpa_supplicant *wpa_s = work->wpa_s;
1004 struct send_action_work *awork = work->ctx;
1005
1006 if (deinit) {
Dmitry Shmidtbd14a572014-02-18 10:33:49 -08001007 if (work->started) {
1008 eloop_cancel_timeout(wpas_p2p_send_action_work_timeout,
1009 wpa_s, NULL);
1010 wpa_s->p2p_send_action_work = NULL;
1011 offchannel_send_action_done(wpa_s);
1012 }
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001013 os_free(awork);
1014 return;
1015 }
1016
1017 if (offchannel_send_action(wpa_s, awork->freq, awork->dst, awork->src,
1018 awork->bssid, awork->buf, awork->len,
1019 awork->wait_time,
1020 wpas_p2p_send_action_tx_status, 1) < 0) {
1021 os_free(awork);
1022 radio_work_done(work);
1023 return;
1024 }
1025 wpa_s->p2p_send_action_work = work;
1026}
1027
1028
1029static int wpas_send_action_work(struct wpa_supplicant *wpa_s,
1030 unsigned int freq, const u8 *dst,
1031 const u8 *src, const u8 *bssid, const u8 *buf,
1032 size_t len, unsigned int wait_time)
1033{
1034 struct send_action_work *awork;
1035
1036 if (wpa_s->p2p_send_action_work) {
1037 wpa_printf(MSG_DEBUG, "P2P: Cannot schedule new p2p-send-action work since one is already pending");
1038 return -1;
1039 }
1040
1041 awork = os_zalloc(sizeof(*awork) + len);
1042 if (awork == NULL)
1043 return -1;
1044
1045 awork->freq = freq;
1046 os_memcpy(awork->dst, dst, ETH_ALEN);
1047 os_memcpy(awork->src, src, ETH_ALEN);
1048 os_memcpy(awork->bssid, bssid, ETH_ALEN);
1049 awork->len = len;
1050 awork->wait_time = wait_time;
1051 os_memcpy(awork->buf, buf, len);
1052
1053 if (radio_add_work(wpa_s, freq, "p2p-send-action", 0,
1054 wpas_send_action_cb, awork) < 0) {
1055 os_free(awork);
1056 return -1;
1057 }
1058
1059 return 0;
1060}
1061
1062
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001063static int wpas_send_action(void *ctx, unsigned int freq, const u8 *dst,
1064 const u8 *src, const u8 *bssid, const u8 *buf,
1065 size_t len, unsigned int wait_time)
1066{
1067 struct wpa_supplicant *wpa_s = ctx;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001068 int listen_freq = -1, send_freq = -1;
1069
1070 if (wpa_s->p2p_listen_work)
1071 listen_freq = wpa_s->p2p_listen_work->freq;
1072 if (wpa_s->p2p_send_action_work)
1073 send_freq = wpa_s->p2p_send_action_work->freq;
1074 if (listen_freq != (int) freq && send_freq != (int) freq) {
1075 wpa_printf(MSG_DEBUG, "P2P: Schedule new radio work for Action frame TX (listen_freq=%d send_freq=%d)",
1076 listen_freq, send_freq);
1077 return wpas_send_action_work(wpa_s, freq, dst, src, bssid, buf,
1078 len, wait_time);
1079 }
1080
1081 wpa_printf(MSG_DEBUG, "P2P: Use ongoing radio work for Action frame TX");
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001082 return offchannel_send_action(wpa_s, freq, dst, src, bssid, buf, len,
1083 wait_time,
1084 wpas_p2p_send_action_tx_status, 1);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001085}
1086
1087
1088static void wpas_send_action_done(void *ctx)
1089{
1090 struct wpa_supplicant *wpa_s = ctx;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001091
1092 if (wpa_s->p2p_send_action_work) {
1093 eloop_cancel_timeout(wpas_p2p_send_action_work_timeout,
1094 wpa_s, NULL);
1095 os_free(wpa_s->p2p_send_action_work->ctx);
1096 radio_work_done(wpa_s->p2p_send_action_work);
1097 wpa_s->p2p_send_action_work = NULL;
1098 }
1099
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001100 offchannel_send_action_done(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001101}
1102
1103
1104static int wpas_copy_go_neg_results(struct wpa_supplicant *wpa_s,
1105 struct p2p_go_neg_results *params)
1106{
1107 if (wpa_s->go_params == NULL) {
1108 wpa_s->go_params = os_malloc(sizeof(*params));
1109 if (wpa_s->go_params == NULL)
1110 return -1;
1111 }
1112 os_memcpy(wpa_s->go_params, params, sizeof(*params));
1113 return 0;
1114}
1115
1116
1117static void wpas_start_wps_enrollee(struct wpa_supplicant *wpa_s,
1118 struct p2p_go_neg_results *res)
1119{
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001120 wpa_printf(MSG_DEBUG, "P2P: Start WPS Enrollee for peer " MACSTR
1121 " dev_addr " MACSTR " wps_method %d",
1122 MAC2STR(res->peer_interface_addr),
1123 MAC2STR(res->peer_device_addr), res->wps_method);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001124 wpa_hexdump_ascii(MSG_DEBUG, "P2P: Start WPS Enrollee for SSID",
1125 res->ssid, res->ssid_len);
1126 wpa_supplicant_ap_deinit(wpa_s);
1127 wpas_copy_go_neg_results(wpa_s, res);
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001128 if (res->wps_method == WPS_PBC) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001129 wpas_wps_start_pbc(wpa_s, res->peer_interface_addr, 1);
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001130#ifdef CONFIG_WPS_NFC
1131 } else if (res->wps_method == WPS_NFC) {
1132 wpas_wps_start_nfc(wpa_s, res->peer_device_addr,
1133 res->peer_interface_addr,
1134 wpa_s->parent->p2p_oob_dev_pw,
1135 wpa_s->parent->p2p_oob_dev_pw_id, 1,
1136 wpa_s->parent->p2p_oob_dev_pw_id ==
1137 DEV_PW_NFC_CONNECTION_HANDOVER ?
1138 wpa_s->parent->p2p_peer_oob_pubkey_hash :
1139 NULL,
1140 NULL, 0, 0);
1141#endif /* CONFIG_WPS_NFC */
1142 } else {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001143 u16 dev_pw_id = DEV_PW_DEFAULT;
1144 if (wpa_s->p2p_wps_method == WPS_PIN_KEYPAD)
1145 dev_pw_id = DEV_PW_REGISTRAR_SPECIFIED;
1146 wpas_wps_start_pin(wpa_s, res->peer_interface_addr,
1147 wpa_s->p2p_pin, 1, dev_pw_id);
1148 }
1149}
1150
1151
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07001152static void wpas_p2p_add_psk_list(struct wpa_supplicant *wpa_s,
1153 struct wpa_ssid *ssid)
1154{
1155 struct wpa_ssid *persistent;
1156 struct psk_list_entry *psk;
1157 struct hostapd_data *hapd;
1158
1159 if (!wpa_s->ap_iface)
1160 return;
1161
1162 persistent = wpas_p2p_get_persistent(wpa_s->parent, NULL, ssid->ssid,
1163 ssid->ssid_len);
1164 if (persistent == NULL)
1165 return;
1166
1167 hapd = wpa_s->ap_iface->bss[0];
1168
1169 dl_list_for_each(psk, &persistent->psk_list, struct psk_list_entry,
1170 list) {
1171 struct hostapd_wpa_psk *hpsk;
1172
1173 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Add persistent group PSK entry for "
1174 MACSTR " psk=%d",
1175 MAC2STR(psk->addr), psk->p2p);
1176 hpsk = os_zalloc(sizeof(*hpsk));
1177 if (hpsk == NULL)
1178 break;
1179 os_memcpy(hpsk->psk, psk->psk, PMK_LEN);
1180 if (psk->p2p)
1181 os_memcpy(hpsk->p2p_dev_addr, psk->addr, ETH_ALEN);
1182 else
1183 os_memcpy(hpsk->addr, psk->addr, ETH_ALEN);
1184 hpsk->next = hapd->conf->ssid.wpa_psk;
1185 hapd->conf->ssid.wpa_psk = hpsk;
1186 }
1187}
1188
1189
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001190static void p2p_go_configured(void *ctx, void *data)
1191{
1192 struct wpa_supplicant *wpa_s = ctx;
1193 struct p2p_go_neg_results *params = data;
1194 struct wpa_ssid *ssid;
Jouni Malinen75ecf522011-06-27 15:19:46 -07001195 int network_id = -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001196
1197 ssid = wpa_s->current_ssid;
1198 if (ssid && ssid->mode == WPAS_MODE_P2P_GO) {
1199 wpa_printf(MSG_DEBUG, "P2P: Group setup without provisioning");
1200 if (wpa_s->global->p2p_group_formation == wpa_s)
1201 wpa_s->global->p2p_group_formation = NULL;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001202 if (os_strlen(params->passphrase) > 0) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07001203 wpa_msg_global(wpa_s->parent, MSG_INFO,
1204 P2P_EVENT_GROUP_STARTED
1205 "%s GO ssid=\"%s\" freq=%d "
1206 "passphrase=\"%s\" go_dev_addr=" MACSTR
1207 "%s", wpa_s->ifname,
1208 wpa_ssid_txt(ssid->ssid, ssid->ssid_len),
1209 ssid->frequency, params->passphrase,
1210 MAC2STR(wpa_s->global->p2p_dev_addr),
1211 params->persistent_group ?
1212 " [PERSISTENT]" : "");
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001213 } else {
1214 char psk[65];
1215 wpa_snprintf_hex(psk, sizeof(psk), params->psk,
1216 sizeof(params->psk));
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07001217 wpa_msg_global(wpa_s->parent, MSG_INFO,
1218 P2P_EVENT_GROUP_STARTED
1219 "%s GO ssid=\"%s\" freq=%d psk=%s "
1220 "go_dev_addr=" MACSTR "%s",
1221 wpa_s->ifname,
1222 wpa_ssid_txt(ssid->ssid, ssid->ssid_len),
1223 ssid->frequency, psk,
1224 MAC2STR(wpa_s->global->p2p_dev_addr),
1225 params->persistent_group ?
1226 " [PERSISTENT]" : "");
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001227 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001228
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001229 os_get_reltime(&wpa_s->global->p2p_go_wait_client);
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07001230 if (params->persistent_group) {
Jouni Malinen75ecf522011-06-27 15:19:46 -07001231 network_id = wpas_p2p_store_persistent_group(
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001232 wpa_s->parent, ssid,
Dmitry Shmidt497c1d52011-07-21 15:19:46 -07001233 wpa_s->global->p2p_dev_addr);
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07001234 wpas_p2p_add_psk_list(wpa_s, ssid);
1235 }
Jouni Malinen75ecf522011-06-27 15:19:46 -07001236 if (network_id < 0)
1237 network_id = ssid->id;
1238 wpas_notify_p2p_group_started(wpa_s, ssid, network_id, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001239 wpas_p2p_cross_connect_setup(wpa_s);
1240 wpas_p2p_set_group_idle_timeout(wpa_s);
Dmitry Shmidt56052862013-10-04 10:23:25 -07001241
1242 if (wpa_s->p2p_first_connection_timeout) {
1243 wpa_dbg(wpa_s, MSG_DEBUG,
1244 "P2P: Start group formation timeout of %d seconds until first data connection on GO",
1245 wpa_s->p2p_first_connection_timeout);
1246 wpa_s->p2p_go_group_formation_completed = 0;
1247 wpa_s->global->p2p_group_formation = wpa_s;
1248 eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
1249 wpa_s->parent, NULL);
1250 eloop_register_timeout(
1251 wpa_s->p2p_first_connection_timeout, 0,
1252 wpas_p2p_group_formation_timeout,
1253 wpa_s->parent, NULL);
1254 }
1255
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001256 return;
1257 }
1258
1259 wpa_printf(MSG_DEBUG, "P2P: Setting up WPS for GO provisioning");
1260 if (wpa_supplicant_ap_mac_addr_filter(wpa_s,
1261 params->peer_interface_addr)) {
1262 wpa_printf(MSG_DEBUG, "P2P: Failed to setup MAC address "
1263 "filtering");
1264 return;
1265 }
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001266 if (params->wps_method == WPS_PBC) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001267 wpa_supplicant_ap_wps_pbc(wpa_s, params->peer_interface_addr,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001268 params->peer_device_addr);
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001269#ifdef CONFIG_WPS_NFC
1270 } else if (params->wps_method == WPS_NFC) {
1271 if (wpa_s->parent->p2p_oob_dev_pw_id !=
1272 DEV_PW_NFC_CONNECTION_HANDOVER &&
1273 !wpa_s->parent->p2p_oob_dev_pw) {
1274 wpa_printf(MSG_DEBUG, "P2P: No NFC Dev Pw known");
1275 return;
1276 }
1277 wpas_ap_wps_add_nfc_pw(
1278 wpa_s, wpa_s->parent->p2p_oob_dev_pw_id,
1279 wpa_s->parent->p2p_oob_dev_pw,
1280 wpa_s->parent->p2p_peer_oob_pk_hash_known ?
1281 wpa_s->parent->p2p_peer_oob_pubkey_hash : NULL);
1282#endif /* CONFIG_WPS_NFC */
1283 } else if (wpa_s->p2p_pin[0])
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001284 wpa_supplicant_ap_wps_pin(wpa_s, params->peer_interface_addr,
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001285 wpa_s->p2p_pin, NULL, 0, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001286 os_free(wpa_s->go_params);
1287 wpa_s->go_params = NULL;
1288}
1289
1290
1291static void wpas_start_wps_go(struct wpa_supplicant *wpa_s,
1292 struct p2p_go_neg_results *params,
1293 int group_formation)
1294{
1295 struct wpa_ssid *ssid;
1296
Dmitry Shmidtaa532512012-09-24 10:35:31 -07001297 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Starting GO");
1298 if (wpas_copy_go_neg_results(wpa_s, params) < 0) {
1299 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Could not copy GO Negotiation "
1300 "results");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001301 return;
Dmitry Shmidtaa532512012-09-24 10:35:31 -07001302 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001303
1304 ssid = wpa_config_add_network(wpa_s->conf);
Dmitry Shmidtaa532512012-09-24 10:35:31 -07001305 if (ssid == NULL) {
1306 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Could not add network for GO");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001307 return;
Dmitry Shmidtaa532512012-09-24 10:35:31 -07001308 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001309
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001310 wpa_s->show_group_started = 0;
1311
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001312 wpa_config_set_network_defaults(ssid);
1313 ssid->temporary = 1;
1314 ssid->p2p_group = 1;
1315 ssid->p2p_persistent_group = params->persistent_group;
1316 ssid->mode = group_formation ? WPAS_MODE_P2P_GROUP_FORMATION :
1317 WPAS_MODE_P2P_GO;
1318 ssid->frequency = params->freq;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001319 ssid->ht40 = params->ht40;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07001320 ssid->vht = params->vht;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001321 ssid->ssid = os_zalloc(params->ssid_len + 1);
1322 if (ssid->ssid) {
1323 os_memcpy(ssid->ssid, params->ssid, params->ssid_len);
1324 ssid->ssid_len = params->ssid_len;
1325 }
1326 ssid->auth_alg = WPA_AUTH_ALG_OPEN;
1327 ssid->key_mgmt = WPA_KEY_MGMT_PSK;
1328 ssid->proto = WPA_PROTO_RSN;
1329 ssid->pairwise_cipher = WPA_CIPHER_CCMP;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001330 if (os_strlen(params->passphrase) > 0) {
1331 ssid->passphrase = os_strdup(params->passphrase);
1332 if (ssid->passphrase == NULL) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07001333 wpa_msg_global(wpa_s, MSG_ERROR,
1334 "P2P: Failed to copy passphrase for GO");
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001335 wpa_config_remove_network(wpa_s->conf, ssid->id);
1336 return;
1337 }
1338 } else
1339 ssid->passphrase = NULL;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001340 ssid->psk_set = params->psk_set;
1341 if (ssid->psk_set)
1342 os_memcpy(ssid->psk, params->psk, sizeof(ssid->psk));
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001343 else if (ssid->passphrase)
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001344 wpa_config_update_psk(ssid);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001345 ssid->ap_max_inactivity = wpa_s->parent->conf->p2p_go_max_inactivity;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001346
1347 wpa_s->ap_configured_cb = p2p_go_configured;
1348 wpa_s->ap_configured_cb_ctx = wpa_s;
1349 wpa_s->ap_configured_cb_data = wpa_s->go_params;
Dmitry Shmidt6dc03bd2014-05-16 10:40:13 -07001350 wpa_s->scan_req = NORMAL_SCAN_REQ;
Jouni Malinen75ecf522011-06-27 15:19:46 -07001351 wpa_s->connect_without_scan = ssid;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001352 wpa_s->reassociate = 1;
1353 wpa_s->disconnected = 0;
Dmitry Shmidtaa532512012-09-24 10:35:31 -07001354 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Request scan (that will be skipped) to "
1355 "start GO)");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001356 wpa_supplicant_req_scan(wpa_s, 0, 0);
1357}
1358
1359
1360static void wpas_p2p_clone_config(struct wpa_supplicant *dst,
1361 const struct wpa_supplicant *src)
1362{
1363 struct wpa_config *d;
1364 const struct wpa_config *s;
1365
1366 d = dst->conf;
1367 s = src->conf;
1368
1369#define C(n) if (s->n) d->n = os_strdup(s->n)
1370 C(device_name);
1371 C(manufacturer);
1372 C(model_name);
1373 C(model_number);
1374 C(serial_number);
1375 C(config_methods);
1376#undef C
1377
1378 os_memcpy(d->device_type, s->device_type, WPS_DEV_TYPE_LEN);
1379 os_memcpy(d->sec_device_type, s->sec_device_type,
1380 sizeof(d->sec_device_type));
1381 d->num_sec_device_types = s->num_sec_device_types;
1382
1383 d->p2p_group_idle = s->p2p_group_idle;
1384 d->p2p_intra_bss = s->p2p_intra_bss;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001385 d->persistent_reconnect = s->persistent_reconnect;
Dmitry Shmidt04949592012-07-19 12:16:46 -07001386 d->max_num_sta = s->max_num_sta;
1387 d->pbc_in_m1 = s->pbc_in_m1;
Dmitry Shmidt444d5672013-04-01 13:08:44 -07001388 d->ignore_old_scan_res = s->ignore_old_scan_res;
Dmitry Shmidt51b6ea82013-05-08 10:42:09 -07001389 d->beacon_int = s->beacon_int;
Dmitry Shmidt18463232014-01-24 12:29:41 -08001390 d->dtim_period = s->dtim_period;
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07001391 d->disassoc_low_ack = s->disassoc_low_ack;
Dmitry Shmidt96571392013-10-14 12:54:46 -07001392 d->disable_scan_offload = s->disable_scan_offload;
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001393
1394 if (s->wps_nfc_dh_privkey && s->wps_nfc_dh_pubkey) {
1395 d->wps_nfc_dh_privkey = wpabuf_dup(s->wps_nfc_dh_privkey);
1396 d->wps_nfc_dh_pubkey = wpabuf_dup(s->wps_nfc_dh_pubkey);
1397 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001398}
1399
1400
Dmitry Shmidt34af3062013-07-11 10:46:32 -07001401static void wpas_p2p_get_group_ifname(struct wpa_supplicant *wpa_s,
1402 char *ifname, size_t len)
1403{
1404 char *ifname_ptr = wpa_s->ifname;
1405
1406 if (os_strncmp(wpa_s->ifname, P2P_MGMT_DEVICE_PREFIX,
1407 os_strlen(P2P_MGMT_DEVICE_PREFIX)) == 0) {
1408 ifname_ptr = os_strrchr(wpa_s->ifname, '-') + 1;
1409 }
1410
1411 os_snprintf(ifname, len, "p2p-%s-%d", ifname_ptr, wpa_s->p2p_group_idx);
1412 if (os_strlen(ifname) >= IFNAMSIZ &&
1413 os_strlen(wpa_s->ifname) < IFNAMSIZ) {
1414 /* Try to avoid going over the IFNAMSIZ length limit */
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07001415 os_snprintf(ifname, len, "p2p-%d", wpa_s->p2p_group_idx);
Dmitry Shmidt34af3062013-07-11 10:46:32 -07001416 }
1417}
1418
1419
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001420static int wpas_p2p_add_group_interface(struct wpa_supplicant *wpa_s,
1421 enum wpa_driver_if_type type)
1422{
1423 char ifname[120], force_ifname[120];
1424
1425 if (wpa_s->pending_interface_name[0]) {
1426 wpa_printf(MSG_DEBUG, "P2P: Pending virtual interface exists "
1427 "- skip creation of a new one");
1428 if (is_zero_ether_addr(wpa_s->pending_interface_addr)) {
1429 wpa_printf(MSG_DEBUG, "P2P: Pending virtual address "
1430 "unknown?! ifname='%s'",
1431 wpa_s->pending_interface_name);
1432 return -1;
1433 }
1434 return 0;
1435 }
1436
Dmitry Shmidt34af3062013-07-11 10:46:32 -07001437 wpas_p2p_get_group_ifname(wpa_s, ifname, sizeof(ifname));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001438 force_ifname[0] = '\0';
1439
1440 wpa_printf(MSG_DEBUG, "P2P: Create a new interface %s for the group",
1441 ifname);
1442 wpa_s->p2p_group_idx++;
1443
1444 wpa_s->pending_interface_type = type;
1445 if (wpa_drv_if_add(wpa_s, type, ifname, NULL, NULL, force_ifname,
1446 wpa_s->pending_interface_addr, NULL) < 0) {
1447 wpa_printf(MSG_ERROR, "P2P: Failed to create new group "
1448 "interface");
1449 return -1;
1450 }
1451
1452 if (force_ifname[0]) {
1453 wpa_printf(MSG_DEBUG, "P2P: Driver forced interface name %s",
1454 force_ifname);
1455 os_strlcpy(wpa_s->pending_interface_name, force_ifname,
1456 sizeof(wpa_s->pending_interface_name));
1457 } else
1458 os_strlcpy(wpa_s->pending_interface_name, ifname,
1459 sizeof(wpa_s->pending_interface_name));
1460 wpa_printf(MSG_DEBUG, "P2P: Created pending virtual interface %s addr "
1461 MACSTR, wpa_s->pending_interface_name,
1462 MAC2STR(wpa_s->pending_interface_addr));
1463
1464 return 0;
1465}
1466
1467
1468static void wpas_p2p_remove_pending_group_interface(
1469 struct wpa_supplicant *wpa_s)
1470{
1471 if (!wpa_s->pending_interface_name[0] ||
1472 is_zero_ether_addr(wpa_s->pending_interface_addr))
1473 return; /* No pending virtual interface */
1474
1475 wpa_printf(MSG_DEBUG, "P2P: Removing pending group interface %s",
1476 wpa_s->pending_interface_name);
1477 wpa_drv_if_remove(wpa_s, wpa_s->pending_interface_type,
1478 wpa_s->pending_interface_name);
1479 os_memset(wpa_s->pending_interface_addr, 0, ETH_ALEN);
1480 wpa_s->pending_interface_name[0] = '\0';
1481}
1482
1483
1484static struct wpa_supplicant *
1485wpas_p2p_init_group_interface(struct wpa_supplicant *wpa_s, int go)
1486{
1487 struct wpa_interface iface;
1488 struct wpa_supplicant *group_wpa_s;
1489
1490 if (!wpa_s->pending_interface_name[0]) {
1491 wpa_printf(MSG_ERROR, "P2P: No pending group interface");
1492 if (!wpas_p2p_create_iface(wpa_s))
1493 return NULL;
1494 /*
1495 * Something has forced us to remove the pending interface; try
1496 * to create a new one and hope for the best that we will get
1497 * the same local address.
1498 */
1499 if (wpas_p2p_add_group_interface(wpa_s, go ? WPA_IF_P2P_GO :
1500 WPA_IF_P2P_CLIENT) < 0)
1501 return NULL;
1502 }
1503
1504 os_memset(&iface, 0, sizeof(iface));
1505 iface.ifname = wpa_s->pending_interface_name;
1506 iface.driver = wpa_s->driver->name;
Dmitry Shmidt34af3062013-07-11 10:46:32 -07001507 if (wpa_s->conf->ctrl_interface == NULL &&
1508 wpa_s->parent != wpa_s &&
1509 wpa_s->p2p_mgmt &&
1510 (wpa_s->drv_flags & WPA_DRIVER_FLAGS_DEDICATED_P2P_DEVICE))
1511 iface.ctrl_interface = wpa_s->parent->conf->ctrl_interface;
1512 else
1513 iface.ctrl_interface = wpa_s->conf->ctrl_interface;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001514 iface.driver_param = wpa_s->conf->driver_param;
1515 group_wpa_s = wpa_supplicant_add_iface(wpa_s->global, &iface);
1516 if (group_wpa_s == NULL) {
1517 wpa_printf(MSG_ERROR, "P2P: Failed to create new "
1518 "wpa_supplicant interface");
1519 return NULL;
1520 }
1521 wpa_s->pending_interface_name[0] = '\0';
1522 group_wpa_s->parent = wpa_s;
1523 group_wpa_s->p2p_group_interface = go ? P2P_GROUP_INTERFACE_GO :
1524 P2P_GROUP_INTERFACE_CLIENT;
1525 wpa_s->global->p2p_group_formation = group_wpa_s;
1526
1527 wpas_p2p_clone_config(group_wpa_s, wpa_s);
1528
1529 return group_wpa_s;
1530}
1531
1532
1533static void wpas_p2p_group_formation_timeout(void *eloop_ctx,
1534 void *timeout_ctx)
1535{
1536 struct wpa_supplicant *wpa_s = eloop_ctx;
1537 wpa_printf(MSG_DEBUG, "P2P: Group Formation timed out");
Dmitry Shmidt34af3062013-07-11 10:46:32 -07001538 wpas_p2p_group_formation_failed(wpa_s);
1539}
1540
1541
1542void wpas_p2p_group_formation_failed(struct wpa_supplicant *wpa_s)
1543{
1544 eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
1545 wpa_s->parent, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001546 if (wpa_s->global->p2p)
1547 p2p_group_formation_failed(wpa_s->global->p2p);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001548 wpas_group_formation_completed(wpa_s, 0);
1549}
1550
1551
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001552static void wpas_p2p_grpform_fail_after_wps(struct wpa_supplicant *wpa_s)
1553{
1554 wpa_printf(MSG_DEBUG, "P2P: Reject group formation due to WPS provisioning failure");
1555 eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
1556 wpa_s->parent, NULL);
1557 eloop_register_timeout(0, 0, wpas_p2p_group_formation_timeout,
1558 wpa_s->parent, NULL);
1559 wpa_s->global->p2p_fail_on_wps_complete = 0;
1560}
1561
1562
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07001563void wpas_p2p_ap_setup_failed(struct wpa_supplicant *wpa_s)
1564{
1565 if (wpa_s->global->p2p_group_formation != wpa_s)
1566 return;
1567 /* Speed up group formation timeout since this cannot succeed */
1568 eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
1569 wpa_s->parent, NULL);
1570 eloop_register_timeout(0, 0, wpas_p2p_group_formation_timeout,
1571 wpa_s->parent, NULL);
1572}
1573
1574
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001575static void wpas_go_neg_completed(void *ctx, struct p2p_go_neg_results *res)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001576{
1577 struct wpa_supplicant *wpa_s = ctx;
1578
1579 if (wpa_s->off_channel_freq || wpa_s->roc_waiting_drv_freq) {
1580 wpa_drv_cancel_remain_on_channel(wpa_s);
1581 wpa_s->off_channel_freq = 0;
1582 wpa_s->roc_waiting_drv_freq = 0;
1583 }
1584
1585 if (res->status) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07001586 wpa_msg_global(wpa_s, MSG_INFO,
1587 P2P_EVENT_GO_NEG_FAILURE "status=%d",
1588 res->status);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001589 wpas_notify_p2p_go_neg_completed(wpa_s, res);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001590 wpas_p2p_remove_pending_group_interface(wpa_s);
1591 return;
1592 }
1593
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001594 if (wpa_s->p2p_go_ht40)
1595 res->ht40 = 1;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07001596 if (wpa_s->p2p_go_vht)
1597 res->vht = 1;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001598
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07001599 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_GO_NEG_SUCCESS "role=%s "
1600 "freq=%d ht40=%d peer_dev=" MACSTR " peer_iface=" MACSTR
1601 " wps_method=%s",
1602 res->role_go ? "GO" : "client", res->freq, res->ht40,
1603 MAC2STR(res->peer_device_addr),
1604 MAC2STR(res->peer_interface_addr),
1605 p2p_wps_method_text(res->wps_method));
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001606 wpas_notify_p2p_go_neg_completed(wpa_s, res);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001607
Dmitry Shmidt04949592012-07-19 12:16:46 -07001608 if (res->role_go && wpa_s->p2p_persistent_id >= 0) {
1609 struct wpa_ssid *ssid;
1610 ssid = wpa_config_get_network(wpa_s->conf,
1611 wpa_s->p2p_persistent_id);
1612 if (ssid && ssid->disabled == 2 &&
1613 ssid->mode == WPAS_MODE_P2P_GO && ssid->passphrase) {
1614 size_t len = os_strlen(ssid->passphrase);
1615 wpa_printf(MSG_DEBUG, "P2P: Override passphrase based "
1616 "on requested persistent group");
1617 os_memcpy(res->passphrase, ssid->passphrase, len);
1618 res->passphrase[len] = '\0';
1619 }
1620 }
1621
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001622 if (wpa_s->create_p2p_iface) {
1623 struct wpa_supplicant *group_wpa_s =
1624 wpas_p2p_init_group_interface(wpa_s, res->role_go);
1625 if (group_wpa_s == NULL) {
1626 wpas_p2p_remove_pending_group_interface(wpa_s);
Dmitry Shmidtd11f0192014-03-24 12:09:47 -07001627 eloop_cancel_timeout(wpas_p2p_long_listen_timeout,
1628 wpa_s, NULL);
1629 wpas_p2p_group_formation_failed(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001630 return;
1631 }
1632 if (group_wpa_s != wpa_s) {
1633 os_memcpy(group_wpa_s->p2p_pin, wpa_s->p2p_pin,
1634 sizeof(group_wpa_s->p2p_pin));
1635 group_wpa_s->p2p_wps_method = wpa_s->p2p_wps_method;
1636 }
1637 os_memset(wpa_s->pending_interface_addr, 0, ETH_ALEN);
1638 wpa_s->pending_interface_name[0] = '\0';
1639 group_wpa_s->p2p_in_provisioning = 1;
1640
1641 if (res->role_go)
1642 wpas_start_wps_go(group_wpa_s, res, 1);
1643 else
1644 wpas_start_wps_enrollee(group_wpa_s, res);
1645 } else {
1646 wpa_s->p2p_in_provisioning = 1;
1647 wpa_s->global->p2p_group_formation = wpa_s;
1648
1649 if (res->role_go)
1650 wpas_start_wps_go(wpa_s, res, 1);
1651 else
1652 wpas_start_wps_enrollee(ctx, res);
1653 }
1654
1655 wpa_s->p2p_long_listen = 0;
1656 eloop_cancel_timeout(wpas_p2p_long_listen_timeout, wpa_s, NULL);
1657
1658 eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s, NULL);
1659 eloop_register_timeout(15 + res->peer_config_timeout / 100,
1660 (res->peer_config_timeout % 100) * 10000,
1661 wpas_p2p_group_formation_timeout, wpa_s, NULL);
1662}
1663
1664
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001665static void wpas_go_neg_req_rx(void *ctx, const u8 *src, u16 dev_passwd_id)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001666{
1667 struct wpa_supplicant *wpa_s = ctx;
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07001668 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_GO_NEG_REQUEST MACSTR
1669 " dev_passwd_id=%u", MAC2STR(src), dev_passwd_id);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001670
1671 wpas_notify_p2p_go_neg_req(wpa_s, src, dev_passwd_id);
1672}
1673
1674
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001675static void wpas_dev_found(void *ctx, const u8 *addr,
1676 const struct p2p_peer_info *info,
1677 int new_device)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001678{
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001679#ifndef CONFIG_NO_STDOUT_DEBUG
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001680 struct wpa_supplicant *wpa_s = ctx;
1681 char devtype[WPS_DEV_TYPE_BUFSIZE];
Dmitry Shmidt04f534e2013-12-09 15:50:16 -08001682 char *wfd_dev_info_hex = NULL;
1683
Irfan Sheriff8367dc92012-09-09 17:08:19 -07001684#ifdef CONFIG_WIFI_DISPLAY
Dmitry Shmidt04f534e2013-12-09 15:50:16 -08001685 wfd_dev_info_hex = wifi_display_subelem_hex(info->wfd_subelems,
1686 WFD_SUBELEM_DEVICE_INFO);
Irfan Sheriff8367dc92012-09-09 17:08:19 -07001687#endif /* CONFIG_WIFI_DISPLAY */
Dmitry Shmidt04f534e2013-12-09 15:50:16 -08001688
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07001689 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_DEVICE_FOUND MACSTR
1690 " p2p_dev_addr=" MACSTR
1691 " pri_dev_type=%s name='%s' config_methods=0x%x "
1692 "dev_capab=0x%x group_capab=0x%x%s%s",
1693 MAC2STR(addr), MAC2STR(info->p2p_device_addr),
1694 wps_dev_type_bin2str(info->pri_dev_type, devtype,
1695 sizeof(devtype)),
1696 info->device_name, info->config_methods,
1697 info->dev_capab, info->group_capab,
Dmitry Shmidt04f534e2013-12-09 15:50:16 -08001698 wfd_dev_info_hex ? " wfd_dev_info=0x" : "",
1699 wfd_dev_info_hex ? wfd_dev_info_hex : "");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001700
Dmitry Shmidt04f534e2013-12-09 15:50:16 -08001701 os_free(wfd_dev_info_hex);
Dmitry Shmidt97672262014-02-03 13:02:54 -08001702#endif /* CONFIG_NO_STDOUT_DEBUG */
Dmitry Shmidt04f534e2013-12-09 15:50:16 -08001703
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001704 wpas_notify_p2p_device_found(ctx, info->p2p_device_addr, new_device);
1705}
1706
1707
1708static void wpas_dev_lost(void *ctx, const u8 *dev_addr)
1709{
1710 struct wpa_supplicant *wpa_s = ctx;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001711
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07001712 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_DEVICE_LOST
1713 "p2p_dev_addr=" MACSTR, MAC2STR(dev_addr));
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001714
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001715 wpas_notify_p2p_device_lost(wpa_s, dev_addr);
1716}
1717
1718
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07001719static void wpas_find_stopped(void *ctx)
1720{
1721 struct wpa_supplicant *wpa_s = ctx;
1722 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_FIND_STOPPED);
1723}
1724
1725
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001726struct wpas_p2p_listen_work {
1727 unsigned int freq;
1728 unsigned int duration;
1729 struct wpabuf *probe_resp_ie;
1730};
1731
1732
1733static void wpas_p2p_listen_work_free(struct wpas_p2p_listen_work *lwork)
1734{
1735 if (lwork == NULL)
1736 return;
1737 wpabuf_free(lwork->probe_resp_ie);
1738 os_free(lwork);
1739}
1740
1741
1742static void wpas_p2p_listen_work_done(struct wpa_supplicant *wpa_s)
1743{
1744 struct wpas_p2p_listen_work *lwork;
1745
1746 if (!wpa_s->p2p_listen_work)
1747 return;
1748
1749 lwork = wpa_s->p2p_listen_work->ctx;
1750 wpas_p2p_listen_work_free(lwork);
1751 radio_work_done(wpa_s->p2p_listen_work);
1752 wpa_s->p2p_listen_work = NULL;
1753}
1754
1755
1756static void wpas_start_listen_cb(struct wpa_radio_work *work, int deinit)
1757{
1758 struct wpa_supplicant *wpa_s = work->wpa_s;
1759 struct wpas_p2p_listen_work *lwork = work->ctx;
1760
1761 if (deinit) {
Dmitry Shmidtbd14a572014-02-18 10:33:49 -08001762 if (work->started) {
1763 wpa_s->p2p_listen_work = NULL;
1764 wpas_stop_listen(wpa_s);
1765 }
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001766 wpas_p2p_listen_work_free(lwork);
1767 return;
1768 }
1769
1770 wpa_s->p2p_listen_work = work;
1771
1772 wpa_drv_set_ap_wps_ie(wpa_s, NULL, lwork->probe_resp_ie, NULL);
1773
1774 if (wpa_drv_probe_req_report(wpa_s, 1) < 0) {
1775 wpa_printf(MSG_DEBUG, "P2P: Failed to request the driver to "
1776 "report received Probe Request frames");
1777 wpas_p2p_listen_work_done(wpa_s);
1778 return;
1779 }
1780
1781 wpa_s->pending_listen_freq = lwork->freq;
1782 wpa_s->pending_listen_duration = lwork->duration;
1783
1784 if (wpa_drv_remain_on_channel(wpa_s, lwork->freq, lwork->duration) < 0)
1785 {
1786 wpa_printf(MSG_DEBUG, "P2P: Failed to request the driver "
1787 "to remain on channel (%u MHz) for Listen "
1788 "state", lwork->freq);
1789 wpas_p2p_listen_work_done(wpa_s);
1790 wpa_s->pending_listen_freq = 0;
1791 return;
1792 }
1793 wpa_s->off_channel_freq = 0;
1794 wpa_s->roc_waiting_drv_freq = lwork->freq;
1795}
1796
1797
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001798static int wpas_start_listen(void *ctx, unsigned int freq,
1799 unsigned int duration,
1800 const struct wpabuf *probe_resp_ie)
1801{
1802 struct wpa_supplicant *wpa_s = ctx;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001803 struct wpas_p2p_listen_work *lwork;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001804
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001805 if (wpa_s->p2p_listen_work) {
1806 wpa_printf(MSG_DEBUG, "P2P: Reject start_listen since p2p_listen_work already exists");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001807 return -1;
1808 }
1809
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001810 lwork = os_zalloc(sizeof(*lwork));
1811 if (lwork == NULL)
1812 return -1;
1813 lwork->freq = freq;
1814 lwork->duration = duration;
1815 if (probe_resp_ie) {
1816 lwork->probe_resp_ie = wpabuf_dup(probe_resp_ie);
1817 if (lwork->probe_resp_ie == NULL) {
1818 wpas_p2p_listen_work_free(lwork);
1819 return -1;
1820 }
1821 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001822
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001823 if (radio_add_work(wpa_s, freq, "p2p-listen", 0, wpas_start_listen_cb,
1824 lwork) < 0) {
1825 wpas_p2p_listen_work_free(lwork);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001826 return -1;
1827 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001828
1829 return 0;
1830}
1831
1832
1833static void wpas_stop_listen(void *ctx)
1834{
1835 struct wpa_supplicant *wpa_s = ctx;
1836 if (wpa_s->off_channel_freq || wpa_s->roc_waiting_drv_freq) {
1837 wpa_drv_cancel_remain_on_channel(wpa_s);
1838 wpa_s->off_channel_freq = 0;
1839 wpa_s->roc_waiting_drv_freq = 0;
1840 }
1841 wpa_drv_set_ap_wps_ie(wpa_s, NULL, NULL, NULL);
1842 wpa_drv_probe_req_report(wpa_s, 0);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001843 wpas_p2p_listen_work_done(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001844}
1845
1846
1847static int wpas_send_probe_resp(void *ctx, const struct wpabuf *buf)
1848{
1849 struct wpa_supplicant *wpa_s = ctx;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001850 return wpa_drv_send_mlme(wpa_s, wpabuf_head(buf), wpabuf_len(buf), 1);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001851}
1852
1853
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001854/*
1855 * DNS Header section is used only to calculate compression pointers, so the
1856 * contents of this data does not matter, but the length needs to be reserved
1857 * in the virtual packet.
1858 */
1859#define DNS_HEADER_LEN 12
1860
1861/*
1862 * 27-octet in-memory packet from P2P specification containing two implied
1863 * queries for _tcp.lcoal. PTR IN and _udp.local. PTR IN
1864 */
1865#define P2P_SD_IN_MEMORY_LEN 27
1866
1867static int p2p_sd_dns_uncompress_label(char **upos, char *uend, u8 *start,
1868 u8 **spos, const u8 *end)
1869{
1870 while (*spos < end) {
1871 u8 val = ((*spos)[0] & 0xc0) >> 6;
1872 int len;
1873
1874 if (val == 1 || val == 2) {
1875 /* These are reserved values in RFC 1035 */
1876 wpa_printf(MSG_DEBUG, "P2P: Invalid domain name "
1877 "sequence starting with 0x%x", val);
1878 return -1;
1879 }
1880
1881 if (val == 3) {
1882 u16 offset;
1883 u8 *spos_tmp;
1884
1885 /* Offset */
1886 if (*spos + 2 > end) {
1887 wpa_printf(MSG_DEBUG, "P2P: No room for full "
1888 "DNS offset field");
1889 return -1;
1890 }
1891
1892 offset = (((*spos)[0] & 0x3f) << 8) | (*spos)[1];
1893 if (offset >= *spos - start) {
1894 wpa_printf(MSG_DEBUG, "P2P: Invalid DNS "
1895 "pointer offset %u", offset);
1896 return -1;
1897 }
1898
1899 (*spos) += 2;
1900 spos_tmp = start + offset;
1901 return p2p_sd_dns_uncompress_label(upos, uend, start,
1902 &spos_tmp,
1903 *spos - 2);
1904 }
1905
1906 /* Label */
1907 len = (*spos)[0] & 0x3f;
1908 if (len == 0)
1909 return 0;
1910
1911 (*spos)++;
1912 if (*spos + len > end) {
1913 wpa_printf(MSG_DEBUG, "P2P: Invalid domain name "
1914 "sequence - no room for label with length "
1915 "%u", len);
1916 return -1;
1917 }
1918
1919 if (*upos + len + 2 > uend)
1920 return -2;
1921
1922 os_memcpy(*upos, *spos, len);
1923 *spos += len;
1924 *upos += len;
1925 (*upos)[0] = '.';
1926 (*upos)++;
1927 (*upos)[0] = '\0';
1928 }
1929
1930 return 0;
1931}
1932
1933
1934/* Uncompress domain names per RFC 1035 using the P2P SD in-memory packet.
1935 * Returns -1 on parsing error (invalid input sequence), -2 if output buffer is
1936 * not large enough */
1937static int p2p_sd_dns_uncompress(char *buf, size_t buf_len, const u8 *msg,
1938 size_t msg_len, size_t offset)
1939{
1940 /* 27-octet in-memory packet from P2P specification */
1941 const char *prefix = "\x04_tcp\x05local\x00\x00\x0C\x00\x01"
1942 "\x04_udp\xC0\x11\x00\x0C\x00\x01";
1943 u8 *tmp, *end, *spos;
1944 char *upos, *uend;
1945 int ret = 0;
1946
1947 if (buf_len < 2)
1948 return -1;
1949 if (offset > msg_len)
1950 return -1;
1951
1952 tmp = os_malloc(DNS_HEADER_LEN + P2P_SD_IN_MEMORY_LEN + msg_len);
1953 if (tmp == NULL)
1954 return -1;
1955 spos = tmp + DNS_HEADER_LEN + P2P_SD_IN_MEMORY_LEN;
1956 end = spos + msg_len;
1957 spos += offset;
1958
1959 os_memset(tmp, 0, DNS_HEADER_LEN);
1960 os_memcpy(tmp + DNS_HEADER_LEN, prefix, P2P_SD_IN_MEMORY_LEN);
1961 os_memcpy(tmp + DNS_HEADER_LEN + P2P_SD_IN_MEMORY_LEN, msg, msg_len);
1962
1963 upos = buf;
1964 uend = buf + buf_len;
1965
1966 ret = p2p_sd_dns_uncompress_label(&upos, uend, tmp, &spos, end);
1967 if (ret) {
1968 os_free(tmp);
1969 return ret;
1970 }
1971
1972 if (upos == buf) {
1973 upos[0] = '.';
1974 upos[1] = '\0';
1975 } else if (upos[-1] == '.')
1976 upos[-1] = '\0';
1977
1978 os_free(tmp);
1979 return 0;
1980}
1981
1982
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001983static struct p2p_srv_bonjour *
1984wpas_p2p_service_get_bonjour(struct wpa_supplicant *wpa_s,
1985 const struct wpabuf *query)
1986{
1987 struct p2p_srv_bonjour *bsrv;
1988 size_t len;
1989
1990 len = wpabuf_len(query);
1991 dl_list_for_each(bsrv, &wpa_s->global->p2p_srv_bonjour,
1992 struct p2p_srv_bonjour, list) {
1993 if (len == wpabuf_len(bsrv->query) &&
1994 os_memcmp(wpabuf_head(query), wpabuf_head(bsrv->query),
1995 len) == 0)
1996 return bsrv;
1997 }
1998 return NULL;
1999}
2000
2001
2002static struct p2p_srv_upnp *
2003wpas_p2p_service_get_upnp(struct wpa_supplicant *wpa_s, u8 version,
2004 const char *service)
2005{
2006 struct p2p_srv_upnp *usrv;
2007
2008 dl_list_for_each(usrv, &wpa_s->global->p2p_srv_upnp,
2009 struct p2p_srv_upnp, list) {
2010 if (version == usrv->version &&
2011 os_strcmp(service, usrv->service) == 0)
2012 return usrv;
2013 }
2014 return NULL;
2015}
2016
2017
2018static void wpas_sd_add_proto_not_avail(struct wpabuf *resp, u8 srv_proto,
2019 u8 srv_trans_id)
2020{
2021 u8 *len_pos;
2022
2023 if (wpabuf_tailroom(resp) < 5)
2024 return;
2025
2026 /* Length (to be filled) */
2027 len_pos = wpabuf_put(resp, 2);
2028 wpabuf_put_u8(resp, srv_proto);
2029 wpabuf_put_u8(resp, srv_trans_id);
2030 /* Status Code */
2031 wpabuf_put_u8(resp, P2P_SD_PROTO_NOT_AVAILABLE);
2032 /* Response Data: empty */
2033 WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos - 2);
2034}
2035
2036
2037static void wpas_sd_all_bonjour(struct wpa_supplicant *wpa_s,
2038 struct wpabuf *resp, u8 srv_trans_id)
2039{
2040 struct p2p_srv_bonjour *bsrv;
2041 u8 *len_pos;
2042
2043 wpa_printf(MSG_DEBUG, "P2P: SD Request for all Bonjour services");
2044
2045 if (dl_list_empty(&wpa_s->global->p2p_srv_bonjour)) {
2046 wpa_printf(MSG_DEBUG, "P2P: Bonjour protocol not available");
2047 return;
2048 }
2049
2050 dl_list_for_each(bsrv, &wpa_s->global->p2p_srv_bonjour,
2051 struct p2p_srv_bonjour, list) {
2052 if (wpabuf_tailroom(resp) <
2053 5 + wpabuf_len(bsrv->query) + wpabuf_len(bsrv->resp))
2054 return;
2055 /* Length (to be filled) */
2056 len_pos = wpabuf_put(resp, 2);
2057 wpabuf_put_u8(resp, P2P_SERV_BONJOUR);
2058 wpabuf_put_u8(resp, srv_trans_id);
2059 /* Status Code */
2060 wpabuf_put_u8(resp, P2P_SD_SUCCESS);
2061 wpa_hexdump_ascii(MSG_DEBUG, "P2P: Matching Bonjour service",
2062 wpabuf_head(bsrv->resp),
2063 wpabuf_len(bsrv->resp));
2064 /* Response Data */
2065 wpabuf_put_buf(resp, bsrv->query); /* Key */
2066 wpabuf_put_buf(resp, bsrv->resp); /* Value */
2067 WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos -
2068 2);
2069 }
2070}
2071
2072
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002073static int match_bonjour_query(struct p2p_srv_bonjour *bsrv, const u8 *query,
2074 size_t query_len)
2075{
2076 char str_rx[256], str_srv[256];
2077
2078 if (query_len < 3 || wpabuf_len(bsrv->query) < 3)
2079 return 0; /* Too short to include DNS Type and Version */
2080 if (os_memcmp(query + query_len - 3,
2081 wpabuf_head_u8(bsrv->query) + wpabuf_len(bsrv->query) - 3,
2082 3) != 0)
2083 return 0; /* Mismatch in DNS Type or Version */
2084 if (query_len == wpabuf_len(bsrv->query) &&
2085 os_memcmp(query, wpabuf_head(bsrv->query), query_len - 3) == 0)
2086 return 1; /* Binary match */
2087
2088 if (p2p_sd_dns_uncompress(str_rx, sizeof(str_rx), query, query_len - 3,
2089 0))
2090 return 0; /* Failed to uncompress query */
2091 if (p2p_sd_dns_uncompress(str_srv, sizeof(str_srv),
2092 wpabuf_head(bsrv->query),
2093 wpabuf_len(bsrv->query) - 3, 0))
2094 return 0; /* Failed to uncompress service */
2095
2096 return os_strcmp(str_rx, str_srv) == 0;
2097}
2098
2099
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002100static void wpas_sd_req_bonjour(struct wpa_supplicant *wpa_s,
2101 struct wpabuf *resp, u8 srv_trans_id,
2102 const u8 *query, size_t query_len)
2103{
2104 struct p2p_srv_bonjour *bsrv;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002105 u8 *len_pos;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002106 int matches = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002107
2108 wpa_hexdump_ascii(MSG_DEBUG, "P2P: SD Request for Bonjour",
2109 query, query_len);
2110 if (dl_list_empty(&wpa_s->global->p2p_srv_bonjour)) {
2111 wpa_printf(MSG_DEBUG, "P2P: Bonjour protocol not available");
2112 wpas_sd_add_proto_not_avail(resp, P2P_SERV_BONJOUR,
2113 srv_trans_id);
2114 return;
2115 }
2116
2117 if (query_len == 0) {
2118 wpas_sd_all_bonjour(wpa_s, resp, srv_trans_id);
2119 return;
2120 }
2121
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002122 dl_list_for_each(bsrv, &wpa_s->global->p2p_srv_bonjour,
2123 struct p2p_srv_bonjour, list) {
2124 if (!match_bonjour_query(bsrv, query, query_len))
2125 continue;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002126
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002127 if (wpabuf_tailroom(resp) <
2128 5 + query_len + wpabuf_len(bsrv->resp))
2129 return;
2130
2131 matches++;
2132
2133 /* Length (to be filled) */
2134 len_pos = wpabuf_put(resp, 2);
2135 wpabuf_put_u8(resp, P2P_SERV_BONJOUR);
2136 wpabuf_put_u8(resp, srv_trans_id);
2137
2138 /* Status Code */
2139 wpabuf_put_u8(resp, P2P_SD_SUCCESS);
2140 wpa_hexdump_ascii(MSG_DEBUG, "P2P: Matching Bonjour service",
2141 wpabuf_head(bsrv->resp),
2142 wpabuf_len(bsrv->resp));
2143
2144 /* Response Data */
2145 wpabuf_put_data(resp, query, query_len); /* Key */
2146 wpabuf_put_buf(resp, bsrv->resp); /* Value */
2147
2148 WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos - 2);
2149 }
2150
2151 if (matches == 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002152 wpa_printf(MSG_DEBUG, "P2P: Requested Bonjour service not "
2153 "available");
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002154 if (wpabuf_tailroom(resp) < 5)
2155 return;
2156
2157 /* Length (to be filled) */
2158 len_pos = wpabuf_put(resp, 2);
2159 wpabuf_put_u8(resp, P2P_SERV_BONJOUR);
2160 wpabuf_put_u8(resp, srv_trans_id);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002161
2162 /* Status Code */
2163 wpabuf_put_u8(resp, P2P_SD_REQUESTED_INFO_NOT_AVAILABLE);
2164 /* Response Data: empty */
2165 WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos -
2166 2);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002167 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002168}
2169
2170
2171static void wpas_sd_all_upnp(struct wpa_supplicant *wpa_s,
2172 struct wpabuf *resp, u8 srv_trans_id)
2173{
2174 struct p2p_srv_upnp *usrv;
2175 u8 *len_pos;
2176
2177 wpa_printf(MSG_DEBUG, "P2P: SD Request for all UPnP services");
2178
2179 if (dl_list_empty(&wpa_s->global->p2p_srv_upnp)) {
2180 wpa_printf(MSG_DEBUG, "P2P: UPnP protocol not available");
2181 return;
2182 }
2183
2184 dl_list_for_each(usrv, &wpa_s->global->p2p_srv_upnp,
2185 struct p2p_srv_upnp, list) {
2186 if (wpabuf_tailroom(resp) < 5 + 1 + os_strlen(usrv->service))
2187 return;
2188
2189 /* Length (to be filled) */
2190 len_pos = wpabuf_put(resp, 2);
2191 wpabuf_put_u8(resp, P2P_SERV_UPNP);
2192 wpabuf_put_u8(resp, srv_trans_id);
2193
2194 /* Status Code */
2195 wpabuf_put_u8(resp, P2P_SD_SUCCESS);
2196 /* Response Data */
2197 wpabuf_put_u8(resp, usrv->version);
2198 wpa_printf(MSG_DEBUG, "P2P: Matching UPnP Service: %s",
2199 usrv->service);
2200 wpabuf_put_str(resp, usrv->service);
2201 WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos -
2202 2);
2203 }
2204}
2205
2206
2207static void wpas_sd_req_upnp(struct wpa_supplicant *wpa_s,
2208 struct wpabuf *resp, u8 srv_trans_id,
2209 const u8 *query, size_t query_len)
2210{
2211 struct p2p_srv_upnp *usrv;
2212 u8 *len_pos;
2213 u8 version;
2214 char *str;
2215 int count = 0;
2216
2217 wpa_hexdump_ascii(MSG_DEBUG, "P2P: SD Request for UPnP",
2218 query, query_len);
2219
2220 if (dl_list_empty(&wpa_s->global->p2p_srv_upnp)) {
2221 wpa_printf(MSG_DEBUG, "P2P: UPnP protocol not available");
2222 wpas_sd_add_proto_not_avail(resp, P2P_SERV_UPNP,
2223 srv_trans_id);
2224 return;
2225 }
2226
2227 if (query_len == 0) {
2228 wpas_sd_all_upnp(wpa_s, resp, srv_trans_id);
2229 return;
2230 }
2231
2232 if (wpabuf_tailroom(resp) < 5)
2233 return;
2234
2235 /* Length (to be filled) */
2236 len_pos = wpabuf_put(resp, 2);
2237 wpabuf_put_u8(resp, P2P_SERV_UPNP);
2238 wpabuf_put_u8(resp, srv_trans_id);
2239
2240 version = query[0];
2241 str = os_malloc(query_len);
2242 if (str == NULL)
2243 return;
2244 os_memcpy(str, query + 1, query_len - 1);
2245 str[query_len - 1] = '\0';
2246
2247 dl_list_for_each(usrv, &wpa_s->global->p2p_srv_upnp,
2248 struct p2p_srv_upnp, list) {
2249 if (version != usrv->version)
2250 continue;
2251
2252 if (os_strcmp(str, "ssdp:all") != 0 &&
2253 os_strstr(usrv->service, str) == NULL)
2254 continue;
2255
2256 if (wpabuf_tailroom(resp) < 2)
2257 break;
2258 if (count == 0) {
2259 /* Status Code */
2260 wpabuf_put_u8(resp, P2P_SD_SUCCESS);
2261 /* Response Data */
2262 wpabuf_put_u8(resp, version);
2263 } else
2264 wpabuf_put_u8(resp, ',');
2265
2266 count++;
2267
2268 wpa_printf(MSG_DEBUG, "P2P: Matching UPnP Service: %s",
2269 usrv->service);
2270 if (wpabuf_tailroom(resp) < os_strlen(usrv->service))
2271 break;
2272 wpabuf_put_str(resp, usrv->service);
2273 }
2274 os_free(str);
2275
2276 if (count == 0) {
2277 wpa_printf(MSG_DEBUG, "P2P: Requested UPnP service not "
2278 "available");
2279 /* Status Code */
2280 wpabuf_put_u8(resp, P2P_SD_REQUESTED_INFO_NOT_AVAILABLE);
2281 /* Response Data: empty */
2282 }
2283
2284 WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos - 2);
2285}
2286
2287
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002288#ifdef CONFIG_WIFI_DISPLAY
2289static void wpas_sd_req_wfd(struct wpa_supplicant *wpa_s,
2290 struct wpabuf *resp, u8 srv_trans_id,
2291 const u8 *query, size_t query_len)
2292{
2293 const u8 *pos;
2294 u8 role;
2295 u8 *len_pos;
2296
2297 wpa_hexdump(MSG_DEBUG, "P2P: SD Request for WFD", query, query_len);
2298
2299 if (!wpa_s->global->wifi_display) {
2300 wpa_printf(MSG_DEBUG, "P2P: WFD protocol not available");
2301 wpas_sd_add_proto_not_avail(resp, P2P_SERV_WIFI_DISPLAY,
2302 srv_trans_id);
2303 return;
2304 }
2305
2306 if (query_len < 1) {
2307 wpa_printf(MSG_DEBUG, "P2P: Missing WFD Requested Device "
2308 "Role");
2309 return;
2310 }
2311
2312 if (wpabuf_tailroom(resp) < 5)
2313 return;
2314
2315 pos = query;
2316 role = *pos++;
2317 wpa_printf(MSG_DEBUG, "P2P: WSD for device role 0x%x", role);
2318
2319 /* TODO: role specific handling */
2320
2321 /* Length (to be filled) */
2322 len_pos = wpabuf_put(resp, 2);
2323 wpabuf_put_u8(resp, P2P_SERV_WIFI_DISPLAY);
2324 wpabuf_put_u8(resp, srv_trans_id);
2325 wpabuf_put_u8(resp, P2P_SD_SUCCESS); /* Status Code */
2326
2327 while (pos < query + query_len) {
2328 if (*pos < MAX_WFD_SUBELEMS &&
2329 wpa_s->global->wfd_subelem[*pos] &&
2330 wpabuf_tailroom(resp) >=
2331 wpabuf_len(wpa_s->global->wfd_subelem[*pos])) {
2332 wpa_printf(MSG_DEBUG, "P2P: Add WSD response "
2333 "subelement %u", *pos);
2334 wpabuf_put_buf(resp, wpa_s->global->wfd_subelem[*pos]);
2335 }
2336 pos++;
2337 }
2338
2339 WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos - 2);
2340}
2341#endif /* CONFIG_WIFI_DISPLAY */
2342
2343
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002344static void wpas_sd_request(void *ctx, int freq, const u8 *sa, u8 dialog_token,
2345 u16 update_indic, const u8 *tlvs, size_t tlvs_len)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002346{
2347 struct wpa_supplicant *wpa_s = ctx;
2348 const u8 *pos = tlvs;
2349 const u8 *end = tlvs + tlvs_len;
2350 const u8 *tlv_end;
2351 u16 slen;
2352 struct wpabuf *resp;
2353 u8 srv_proto, srv_trans_id;
2354 size_t buf_len;
2355 char *buf;
2356
2357 wpa_hexdump(MSG_MSGDUMP, "P2P: Service Discovery Request TLVs",
2358 tlvs, tlvs_len);
2359 buf_len = 2 * tlvs_len + 1;
2360 buf = os_malloc(buf_len);
2361 if (buf) {
2362 wpa_snprintf_hex(buf, buf_len, tlvs, tlvs_len);
2363 wpa_msg_ctrl(wpa_s, MSG_INFO, P2P_EVENT_SERV_DISC_REQ "%d "
2364 MACSTR " %u %u %s",
2365 freq, MAC2STR(sa), dialog_token, update_indic,
2366 buf);
2367 os_free(buf);
2368 }
2369
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002370 if (wpa_s->p2p_sd_over_ctrl_iface) {
2371 wpas_notify_p2p_sd_request(wpa_s, freq, sa, dialog_token,
2372 update_indic, tlvs, tlvs_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002373 return; /* to be processed by an external program */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002374 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002375
2376 resp = wpabuf_alloc(10000);
2377 if (resp == NULL)
2378 return;
2379
2380 while (pos + 1 < end) {
2381 wpa_printf(MSG_DEBUG, "P2P: Service Request TLV");
2382 slen = WPA_GET_LE16(pos);
2383 pos += 2;
2384 if (pos + slen > end || slen < 2) {
2385 wpa_printf(MSG_DEBUG, "P2P: Unexpected Query Data "
2386 "length");
2387 wpabuf_free(resp);
2388 return;
2389 }
2390 tlv_end = pos + slen;
2391
2392 srv_proto = *pos++;
2393 wpa_printf(MSG_DEBUG, "P2P: Service Protocol Type %u",
2394 srv_proto);
2395 srv_trans_id = *pos++;
2396 wpa_printf(MSG_DEBUG, "P2P: Service Transaction ID %u",
2397 srv_trans_id);
2398
2399 wpa_hexdump(MSG_MSGDUMP, "P2P: Query Data",
2400 pos, tlv_end - pos);
2401
2402
2403 if (wpa_s->force_long_sd) {
2404 wpa_printf(MSG_DEBUG, "P2P: SD test - force long "
2405 "response");
2406 wpas_sd_all_bonjour(wpa_s, resp, srv_trans_id);
2407 wpas_sd_all_upnp(wpa_s, resp, srv_trans_id);
2408 goto done;
2409 }
2410
2411 switch (srv_proto) {
2412 case P2P_SERV_ALL_SERVICES:
2413 wpa_printf(MSG_DEBUG, "P2P: Service Discovery Request "
2414 "for all services");
2415 if (dl_list_empty(&wpa_s->global->p2p_srv_upnp) &&
2416 dl_list_empty(&wpa_s->global->p2p_srv_bonjour)) {
2417 wpa_printf(MSG_DEBUG, "P2P: No service "
2418 "discovery protocols available");
2419 wpas_sd_add_proto_not_avail(
2420 resp, P2P_SERV_ALL_SERVICES,
2421 srv_trans_id);
2422 break;
2423 }
2424 wpas_sd_all_bonjour(wpa_s, resp, srv_trans_id);
2425 wpas_sd_all_upnp(wpa_s, resp, srv_trans_id);
2426 break;
2427 case P2P_SERV_BONJOUR:
2428 wpas_sd_req_bonjour(wpa_s, resp, srv_trans_id,
2429 pos, tlv_end - pos);
2430 break;
2431 case P2P_SERV_UPNP:
2432 wpas_sd_req_upnp(wpa_s, resp, srv_trans_id,
2433 pos, tlv_end - pos);
2434 break;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002435#ifdef CONFIG_WIFI_DISPLAY
2436 case P2P_SERV_WIFI_DISPLAY:
2437 wpas_sd_req_wfd(wpa_s, resp, srv_trans_id,
2438 pos, tlv_end - pos);
2439 break;
2440#endif /* CONFIG_WIFI_DISPLAY */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002441 default:
2442 wpa_printf(MSG_DEBUG, "P2P: Unavailable service "
2443 "protocol %u", srv_proto);
2444 wpas_sd_add_proto_not_avail(resp, srv_proto,
2445 srv_trans_id);
2446 break;
2447 }
2448
2449 pos = tlv_end;
2450 }
2451
2452done:
2453 wpas_notify_p2p_sd_request(wpa_s, freq, sa, dialog_token,
2454 update_indic, tlvs, tlvs_len);
2455
2456 wpas_p2p_sd_response(wpa_s, freq, sa, dialog_token, resp);
2457
2458 wpabuf_free(resp);
2459}
2460
2461
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002462static void wpas_sd_response(void *ctx, const u8 *sa, u16 update_indic,
2463 const u8 *tlvs, size_t tlvs_len)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002464{
2465 struct wpa_supplicant *wpa_s = ctx;
2466 const u8 *pos = tlvs;
2467 const u8 *end = tlvs + tlvs_len;
2468 const u8 *tlv_end;
2469 u16 slen;
2470 size_t buf_len;
2471 char *buf;
2472
2473 wpa_hexdump(MSG_MSGDUMP, "P2P: Service Discovery Response TLVs",
2474 tlvs, tlvs_len);
2475 if (tlvs_len > 1500) {
2476 /* TODO: better way for handling this */
2477 wpa_msg_ctrl(wpa_s, MSG_INFO,
2478 P2P_EVENT_SERV_DISC_RESP MACSTR
2479 " %u <long response: %u bytes>",
2480 MAC2STR(sa), update_indic,
2481 (unsigned int) tlvs_len);
2482 } else {
2483 buf_len = 2 * tlvs_len + 1;
2484 buf = os_malloc(buf_len);
2485 if (buf) {
2486 wpa_snprintf_hex(buf, buf_len, tlvs, tlvs_len);
2487 wpa_msg_ctrl(wpa_s, MSG_INFO,
2488 P2P_EVENT_SERV_DISC_RESP MACSTR " %u %s",
2489 MAC2STR(sa), update_indic, buf);
2490 os_free(buf);
2491 }
2492 }
2493
2494 while (pos < end) {
2495 u8 srv_proto, srv_trans_id, status;
2496
2497 wpa_printf(MSG_DEBUG, "P2P: Service Response TLV");
2498 slen = WPA_GET_LE16(pos);
2499 pos += 2;
2500 if (pos + slen > end || slen < 3) {
2501 wpa_printf(MSG_DEBUG, "P2P: Unexpected Response Data "
2502 "length");
2503 return;
2504 }
2505 tlv_end = pos + slen;
2506
2507 srv_proto = *pos++;
2508 wpa_printf(MSG_DEBUG, "P2P: Service Protocol Type %u",
2509 srv_proto);
2510 srv_trans_id = *pos++;
2511 wpa_printf(MSG_DEBUG, "P2P: Service Transaction ID %u",
2512 srv_trans_id);
2513 status = *pos++;
2514 wpa_printf(MSG_DEBUG, "P2P: Status Code ID %u",
2515 status);
2516
2517 wpa_hexdump(MSG_MSGDUMP, "P2P: Response Data",
2518 pos, tlv_end - pos);
2519
2520 pos = tlv_end;
2521 }
2522
2523 wpas_notify_p2p_sd_response(wpa_s, sa, update_indic, tlvs, tlvs_len);
2524}
2525
2526
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002527u64 wpas_p2p_sd_request(struct wpa_supplicant *wpa_s, const u8 *dst,
2528 const struct wpabuf *tlvs)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002529{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002530 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002531 return 0;
2532 return (uintptr_t) p2p_sd_request(wpa_s->global->p2p, dst, tlvs);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002533}
2534
2535
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002536u64 wpas_p2p_sd_request_upnp(struct wpa_supplicant *wpa_s, const u8 *dst,
2537 u8 version, const char *query)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002538{
2539 struct wpabuf *tlvs;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002540 u64 ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002541
2542 tlvs = wpabuf_alloc(2 + 1 + 1 + 1 + os_strlen(query));
2543 if (tlvs == NULL)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002544 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002545 wpabuf_put_le16(tlvs, 1 + 1 + 1 + os_strlen(query));
2546 wpabuf_put_u8(tlvs, P2P_SERV_UPNP); /* Service Protocol Type */
2547 wpabuf_put_u8(tlvs, 1); /* Service Transaction ID */
2548 wpabuf_put_u8(tlvs, version);
2549 wpabuf_put_str(tlvs, query);
2550 ret = wpas_p2p_sd_request(wpa_s, dst, tlvs);
2551 wpabuf_free(tlvs);
2552 return ret;
2553}
2554
2555
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002556#ifdef CONFIG_WIFI_DISPLAY
2557
2558static u64 wpas_p2p_sd_request_wfd(struct wpa_supplicant *wpa_s, const u8 *dst,
2559 const struct wpabuf *tlvs)
2560{
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002561 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
2562 return 0;
2563 return (uintptr_t) p2p_sd_request_wfd(wpa_s->global->p2p, dst, tlvs);
2564}
2565
2566
2567#define MAX_WFD_SD_SUBELEMS 20
2568
2569static void wfd_add_sd_req_role(struct wpabuf *tlvs, u8 id, u8 role,
2570 const char *subelems)
2571{
2572 u8 *len;
2573 const char *pos;
2574 int val;
2575 int count = 0;
2576
2577 len = wpabuf_put(tlvs, 2);
2578 wpabuf_put_u8(tlvs, P2P_SERV_WIFI_DISPLAY); /* Service Protocol Type */
2579 wpabuf_put_u8(tlvs, id); /* Service Transaction ID */
2580
2581 wpabuf_put_u8(tlvs, role);
2582
2583 pos = subelems;
2584 while (*pos) {
2585 val = atoi(pos);
2586 if (val >= 0 && val < 256) {
2587 wpabuf_put_u8(tlvs, val);
2588 count++;
2589 if (count == MAX_WFD_SD_SUBELEMS)
2590 break;
2591 }
2592 pos = os_strchr(pos + 1, ',');
2593 if (pos == NULL)
2594 break;
2595 pos++;
2596 }
2597
2598 WPA_PUT_LE16(len, (u8 *) wpabuf_put(tlvs, 0) - len - 2);
2599}
2600
2601
2602u64 wpas_p2p_sd_request_wifi_display(struct wpa_supplicant *wpa_s,
2603 const u8 *dst, const char *role)
2604{
2605 struct wpabuf *tlvs;
2606 u64 ret;
2607 const char *subelems;
2608 u8 id = 1;
2609
2610 subelems = os_strchr(role, ' ');
2611 if (subelems == NULL)
2612 return 0;
2613 subelems++;
2614
2615 tlvs = wpabuf_alloc(4 * (2 + 1 + 1 + 1 + MAX_WFD_SD_SUBELEMS));
2616 if (tlvs == NULL)
2617 return 0;
2618
2619 if (os_strstr(role, "[source]"))
2620 wfd_add_sd_req_role(tlvs, id++, 0x00, subelems);
2621 if (os_strstr(role, "[pri-sink]"))
2622 wfd_add_sd_req_role(tlvs, id++, 0x01, subelems);
2623 if (os_strstr(role, "[sec-sink]"))
2624 wfd_add_sd_req_role(tlvs, id++, 0x02, subelems);
2625 if (os_strstr(role, "[source+sink]"))
2626 wfd_add_sd_req_role(tlvs, id++, 0x03, subelems);
2627
2628 ret = wpas_p2p_sd_request_wfd(wpa_s, dst, tlvs);
2629 wpabuf_free(tlvs);
2630 return ret;
2631}
2632
2633#endif /* CONFIG_WIFI_DISPLAY */
2634
2635
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002636int wpas_p2p_sd_cancel_request(struct wpa_supplicant *wpa_s, u64 req)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002637{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002638 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
2639 return -1;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002640 return p2p_sd_cancel_request(wpa_s->global->p2p,
2641 (void *) (uintptr_t) req);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002642}
2643
2644
2645void wpas_p2p_sd_response(struct wpa_supplicant *wpa_s, int freq,
2646 const u8 *dst, u8 dialog_token,
2647 const struct wpabuf *resp_tlvs)
2648{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002649 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
2650 return;
2651 p2p_sd_response(wpa_s->global->p2p, freq, dst, dialog_token,
2652 resp_tlvs);
2653}
2654
Dmitry Shmidteaf261d2013-08-14 15:30:08 -07002655
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002656void wpas_p2p_sd_service_update(struct wpa_supplicant *wpa_s)
2657{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002658 if (wpa_s->global->p2p)
2659 p2p_sd_service_update(wpa_s->global->p2p);
2660}
2661
2662
2663static void wpas_p2p_srv_bonjour_free(struct p2p_srv_bonjour *bsrv)
2664{
2665 dl_list_del(&bsrv->list);
2666 wpabuf_free(bsrv->query);
2667 wpabuf_free(bsrv->resp);
2668 os_free(bsrv);
2669}
2670
2671
2672static void wpas_p2p_srv_upnp_free(struct p2p_srv_upnp *usrv)
2673{
2674 dl_list_del(&usrv->list);
2675 os_free(usrv->service);
2676 os_free(usrv);
2677}
2678
2679
2680void wpas_p2p_service_flush(struct wpa_supplicant *wpa_s)
2681{
2682 struct p2p_srv_bonjour *bsrv, *bn;
2683 struct p2p_srv_upnp *usrv, *un;
2684
2685 dl_list_for_each_safe(bsrv, bn, &wpa_s->global->p2p_srv_bonjour,
2686 struct p2p_srv_bonjour, list)
2687 wpas_p2p_srv_bonjour_free(bsrv);
2688
2689 dl_list_for_each_safe(usrv, un, &wpa_s->global->p2p_srv_upnp,
2690 struct p2p_srv_upnp, list)
2691 wpas_p2p_srv_upnp_free(usrv);
2692
2693 wpas_p2p_sd_service_update(wpa_s);
2694}
2695
2696
2697int wpas_p2p_service_add_bonjour(struct wpa_supplicant *wpa_s,
2698 struct wpabuf *query, struct wpabuf *resp)
2699{
2700 struct p2p_srv_bonjour *bsrv;
2701
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002702 bsrv = os_zalloc(sizeof(*bsrv));
2703 if (bsrv == NULL)
2704 return -1;
2705 bsrv->query = query;
2706 bsrv->resp = resp;
2707 dl_list_add(&wpa_s->global->p2p_srv_bonjour, &bsrv->list);
2708
2709 wpas_p2p_sd_service_update(wpa_s);
2710 return 0;
2711}
2712
2713
2714int wpas_p2p_service_del_bonjour(struct wpa_supplicant *wpa_s,
2715 const struct wpabuf *query)
2716{
2717 struct p2p_srv_bonjour *bsrv;
2718
2719 bsrv = wpas_p2p_service_get_bonjour(wpa_s, query);
2720 if (bsrv == NULL)
2721 return -1;
2722 wpas_p2p_srv_bonjour_free(bsrv);
2723 wpas_p2p_sd_service_update(wpa_s);
2724 return 0;
2725}
2726
2727
2728int wpas_p2p_service_add_upnp(struct wpa_supplicant *wpa_s, u8 version,
2729 const char *service)
2730{
2731 struct p2p_srv_upnp *usrv;
2732
2733 if (wpas_p2p_service_get_upnp(wpa_s, version, service))
2734 return 0; /* Already listed */
2735 usrv = os_zalloc(sizeof(*usrv));
2736 if (usrv == NULL)
2737 return -1;
2738 usrv->version = version;
2739 usrv->service = os_strdup(service);
2740 if (usrv->service == NULL) {
2741 os_free(usrv);
2742 return -1;
2743 }
2744 dl_list_add(&wpa_s->global->p2p_srv_upnp, &usrv->list);
2745
2746 wpas_p2p_sd_service_update(wpa_s);
2747 return 0;
2748}
2749
2750
2751int wpas_p2p_service_del_upnp(struct wpa_supplicant *wpa_s, u8 version,
2752 const char *service)
2753{
2754 struct p2p_srv_upnp *usrv;
2755
2756 usrv = wpas_p2p_service_get_upnp(wpa_s, version, service);
2757 if (usrv == NULL)
2758 return -1;
2759 wpas_p2p_srv_upnp_free(usrv);
2760 wpas_p2p_sd_service_update(wpa_s);
2761 return 0;
2762}
2763
2764
2765static void wpas_prov_disc_local_display(struct wpa_supplicant *wpa_s,
2766 const u8 *peer, const char *params,
2767 unsigned int generated_pin)
2768{
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07002769 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_SHOW_PIN MACSTR
2770 " %08d%s", MAC2STR(peer), generated_pin, params);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002771}
2772
2773
2774static void wpas_prov_disc_local_keypad(struct wpa_supplicant *wpa_s,
2775 const u8 *peer, const char *params)
2776{
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07002777 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_ENTER_PIN MACSTR
2778 "%s", MAC2STR(peer), params);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002779}
2780
2781
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002782static void wpas_prov_disc_req(void *ctx, const u8 *peer, u16 config_methods,
2783 const u8 *dev_addr, const u8 *pri_dev_type,
2784 const char *dev_name, u16 supp_config_methods,
2785 u8 dev_capab, u8 group_capab, const u8 *group_id,
2786 size_t group_id_len)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002787{
2788 struct wpa_supplicant *wpa_s = ctx;
2789 char devtype[WPS_DEV_TYPE_BUFSIZE];
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002790 char params[300];
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002791 u8 empty_dev_type[8];
2792 unsigned int generated_pin = 0;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002793 struct wpa_supplicant *group = NULL;
2794
2795 if (group_id) {
2796 for (group = wpa_s->global->ifaces; group; group = group->next)
2797 {
2798 struct wpa_ssid *s = group->current_ssid;
2799 if (s != NULL &&
2800 s->mode == WPAS_MODE_P2P_GO &&
2801 group_id_len - ETH_ALEN == s->ssid_len &&
2802 os_memcmp(group_id + ETH_ALEN, s->ssid,
2803 s->ssid_len) == 0)
2804 break;
2805 }
2806 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002807
2808 if (pri_dev_type == NULL) {
2809 os_memset(empty_dev_type, 0, sizeof(empty_dev_type));
2810 pri_dev_type = empty_dev_type;
2811 }
2812 os_snprintf(params, sizeof(params), " p2p_dev_addr=" MACSTR
2813 " pri_dev_type=%s name='%s' config_methods=0x%x "
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002814 "dev_capab=0x%x group_capab=0x%x%s%s",
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002815 MAC2STR(dev_addr),
2816 wps_dev_type_bin2str(pri_dev_type, devtype,
2817 sizeof(devtype)),
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002818 dev_name, supp_config_methods, dev_capab, group_capab,
2819 group ? " group=" : "",
2820 group ? group->ifname : "");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002821 params[sizeof(params) - 1] = '\0';
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08002822
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002823 if (config_methods & WPS_CONFIG_DISPLAY) {
2824 generated_pin = wps_generate_pin();
2825 wpas_prov_disc_local_display(wpa_s, peer, params,
2826 generated_pin);
2827 } else if (config_methods & WPS_CONFIG_KEYPAD)
2828 wpas_prov_disc_local_keypad(wpa_s, peer, params);
2829 else if (config_methods & WPS_CONFIG_PUSHBUTTON)
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07002830 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_PBC_REQ
2831 MACSTR "%s", MAC2STR(peer), params);
Jouni Malinen75ecf522011-06-27 15:19:46 -07002832
2833 wpas_notify_p2p_provision_discovery(wpa_s, peer, 1 /* request */,
2834 P2P_PROV_DISC_SUCCESS,
2835 config_methods, generated_pin);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002836}
2837
2838
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002839static void wpas_prov_disc_resp(void *ctx, const u8 *peer, u16 config_methods)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002840{
2841 struct wpa_supplicant *wpa_s = ctx;
2842 unsigned int generated_pin = 0;
Dmitry Shmidt04949592012-07-19 12:16:46 -07002843 char params[20];
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002844
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08002845 if (wpa_s->pending_pd_before_join &&
2846 (os_memcmp(peer, wpa_s->pending_join_dev_addr, ETH_ALEN) == 0 ||
2847 os_memcmp(peer, wpa_s->pending_join_iface_addr, ETH_ALEN) == 0)) {
2848 wpa_s->pending_pd_before_join = 0;
2849 wpa_printf(MSG_DEBUG, "P2P: Starting pending "
2850 "join-existing-group operation");
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08002851 wpas_p2p_join_start(wpa_s, 0, NULL, 0);
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08002852 return;
2853 }
2854
Dmitry Shmidt04949592012-07-19 12:16:46 -07002855 if (wpa_s->pending_pd_use == AUTO_PD_JOIN ||
2856 wpa_s->pending_pd_use == AUTO_PD_GO_NEG)
2857 os_snprintf(params, sizeof(params), " peer_go=%d",
2858 wpa_s->pending_pd_use == AUTO_PD_JOIN);
2859 else
2860 params[0] = '\0';
2861
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002862 if (config_methods & WPS_CONFIG_DISPLAY)
Dmitry Shmidt04949592012-07-19 12:16:46 -07002863 wpas_prov_disc_local_keypad(wpa_s, peer, params);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002864 else if (config_methods & WPS_CONFIG_KEYPAD) {
2865 generated_pin = wps_generate_pin();
Dmitry Shmidt04949592012-07-19 12:16:46 -07002866 wpas_prov_disc_local_display(wpa_s, peer, params,
2867 generated_pin);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002868 } else if (config_methods & WPS_CONFIG_PUSHBUTTON)
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07002869 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_PBC_RESP
2870 MACSTR "%s", MAC2STR(peer), params);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002871
Jouni Malinen75ecf522011-06-27 15:19:46 -07002872 wpas_notify_p2p_provision_discovery(wpa_s, peer, 0 /* response */,
2873 P2P_PROV_DISC_SUCCESS,
2874 config_methods, generated_pin);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002875}
2876
2877
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002878static void wpas_prov_disc_fail(void *ctx, const u8 *peer,
2879 enum p2p_prov_disc_status status)
Jouni Malinen75ecf522011-06-27 15:19:46 -07002880{
2881 struct wpa_supplicant *wpa_s = ctx;
2882
Dmitry Shmidt04949592012-07-19 12:16:46 -07002883 if (wpa_s->p2p_fallback_to_go_neg) {
2884 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: PD for p2p_connect-auto "
2885 "failed - fall back to GO Negotiation");
2886 wpas_p2p_fallback_to_go_neg(wpa_s, 0);
2887 return;
2888 }
2889
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002890 if (status == P2P_PROV_DISC_TIMEOUT_JOIN) {
Dmitry Shmidt2b380482012-09-13 10:52:13 -07002891 wpa_s->pending_pd_before_join = 0;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002892 wpa_printf(MSG_DEBUG, "P2P: Starting pending "
2893 "join-existing-group operation (no ACK for PD "
2894 "Req attempts)");
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08002895 wpas_p2p_join_start(wpa_s, 0, NULL, 0);
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002896 return;
Dmitry Shmidt2b380482012-09-13 10:52:13 -07002897 }
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002898
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07002899 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_FAILURE
2900 " p2p_dev_addr=" MACSTR " status=%d",
2901 MAC2STR(peer), status);
Dmitry Shmidt04949592012-07-19 12:16:46 -07002902
Jouni Malinen75ecf522011-06-27 15:19:46 -07002903 wpas_notify_p2p_provision_discovery(wpa_s, peer, 0 /* response */,
2904 status, 0, 0);
2905}
2906
2907
Dmitry Shmidtf7e0a992013-05-23 11:03:10 -07002908static int freq_included(const struct p2p_channels *channels, unsigned int freq)
2909{
2910 if (channels == NULL)
2911 return 1; /* Assume no restrictions */
2912 return p2p_channels_includes_freq(channels, freq);
2913
2914}
2915
2916
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002917static u8 wpas_invitation_process(void *ctx, const u8 *sa, const u8 *bssid,
2918 const u8 *go_dev_addr, const u8 *ssid,
2919 size_t ssid_len, int *go, u8 *group_bssid,
Dmitry Shmidtf7e0a992013-05-23 11:03:10 -07002920 int *force_freq, int persistent_group,
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08002921 const struct p2p_channels *channels,
2922 int dev_pw_id)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002923{
2924 struct wpa_supplicant *wpa_s = ctx;
2925 struct wpa_ssid *s;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002926 int res;
2927 struct wpa_supplicant *grp;
2928
2929 if (!persistent_group) {
2930 wpa_printf(MSG_DEBUG, "P2P: Invitation from " MACSTR
Dmitry Shmidt344abd32014-01-14 13:17:00 -08002931 " to join an active group (SSID: %s)",
2932 MAC2STR(sa), wpa_ssid_txt(ssid, ssid_len));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002933 if (!is_zero_ether_addr(wpa_s->p2p_auth_invite) &&
2934 (os_memcmp(go_dev_addr, wpa_s->p2p_auth_invite, ETH_ALEN)
2935 == 0 ||
2936 os_memcmp(sa, wpa_s->p2p_auth_invite, ETH_ALEN) == 0)) {
2937 wpa_printf(MSG_DEBUG, "P2P: Accept previously "
2938 "authorized invitation");
2939 goto accept_inv;
2940 }
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08002941
2942#ifdef CONFIG_WPS_NFC
2943 if (dev_pw_id >= 0 && wpa_s->parent->p2p_nfc_tag_enabled &&
2944 dev_pw_id == wpa_s->parent->p2p_oob_dev_pw_id) {
2945 wpa_printf(MSG_DEBUG, "P2P: Accept invitation based on local enabled NFC Tag");
2946 wpa_s->parent->p2p_wps_method = WPS_NFC;
2947 wpa_s->parent->pending_join_wps_method = WPS_NFC;
2948 os_memcpy(wpa_s->parent->pending_join_dev_addr,
2949 go_dev_addr, ETH_ALEN);
2950 os_memcpy(wpa_s->parent->pending_join_iface_addr,
2951 bssid, ETH_ALEN);
2952 goto accept_inv;
2953 }
2954#endif /* CONFIG_WPS_NFC */
2955
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002956 /*
2957 * Do not accept the invitation automatically; notify user and
2958 * request approval.
2959 */
2960 return P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE;
2961 }
2962
2963 grp = wpas_get_p2p_group(wpa_s, ssid, ssid_len, go);
2964 if (grp) {
2965 wpa_printf(MSG_DEBUG, "P2P: Accept invitation to already "
2966 "running persistent group");
2967 if (*go)
2968 os_memcpy(group_bssid, grp->own_addr, ETH_ALEN);
2969 goto accept_inv;
2970 }
2971
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08002972 if (!is_zero_ether_addr(wpa_s->p2p_auth_invite) &&
2973 os_memcmp(sa, wpa_s->p2p_auth_invite, ETH_ALEN) == 0) {
2974 wpa_printf(MSG_DEBUG, "P2P: Accept previously initiated "
2975 "invitation to re-invoke a persistent group");
Dmitry Shmidt76cd2cc2014-05-27 12:56:04 -07002976 os_memset(wpa_s->p2p_auth_invite, 0, ETH_ALEN);
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08002977 } else if (!wpa_s->conf->persistent_reconnect)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002978 return P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE;
2979
2980 for (s = wpa_s->conf->ssid; s; s = s->next) {
2981 if (s->disabled == 2 &&
2982 os_memcmp(s->bssid, go_dev_addr, ETH_ALEN) == 0 &&
2983 s->ssid_len == ssid_len &&
2984 os_memcmp(ssid, s->ssid, ssid_len) == 0)
2985 break;
2986 }
2987
2988 if (!s) {
2989 wpa_printf(MSG_DEBUG, "P2P: Invitation from " MACSTR
2990 " requested reinvocation of an unknown group",
2991 MAC2STR(sa));
2992 return P2P_SC_FAIL_UNKNOWN_GROUP;
2993 }
2994
2995 if (s->mode == WPAS_MODE_P2P_GO && !wpas_p2p_create_iface(wpa_s)) {
2996 *go = 1;
2997 if (wpa_s->wpa_state >= WPA_AUTHENTICATING) {
2998 wpa_printf(MSG_DEBUG, "P2P: The only available "
2999 "interface is already in use - reject "
3000 "invitation");
3001 return P2P_SC_FAIL_UNABLE_TO_ACCOMMODATE;
3002 }
3003 os_memcpy(group_bssid, wpa_s->own_addr, ETH_ALEN);
3004 } else if (s->mode == WPAS_MODE_P2P_GO) {
3005 *go = 1;
3006 if (wpas_p2p_add_group_interface(wpa_s, WPA_IF_P2P_GO) < 0)
3007 {
3008 wpa_printf(MSG_ERROR, "P2P: Failed to allocate a new "
3009 "interface address for the group");
3010 return P2P_SC_FAIL_UNABLE_TO_ACCOMMODATE;
3011 }
3012 os_memcpy(group_bssid, wpa_s->pending_interface_addr,
3013 ETH_ALEN);
3014 }
3015
3016accept_inv:
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003017 wpas_p2p_set_own_freq_preference(wpa_s, 0);
3018
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07003019 /* Get one of the frequencies currently in use */
3020 if (wpas_p2p_valid_oper_freqs(wpa_s, &res, 1) > 0) {
Dmitry Shmidta0d265f2013-11-19 13:13:41 -08003021 wpa_printf(MSG_DEBUG, "P2P: Trying to prefer a channel already used by one of the interfaces");
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003022 wpas_p2p_set_own_freq_preference(wpa_s, res);
Dmitry Shmidta0d265f2013-11-19 13:13:41 -08003023
3024 if (wpa_s->num_multichan_concurrent < 2 ||
3025 wpas_p2p_num_unused_channels(wpa_s) < 1) {
3026 wpa_printf(MSG_DEBUG, "P2P: No extra channels available - trying to force channel to match a channel already used by one of the interfaces");
3027 *force_freq = res;
3028 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003029 }
3030
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07003031 if (*force_freq > 0 && wpa_s->num_multichan_concurrent > 1 &&
3032 wpas_p2p_num_unused_channels(wpa_s) > 0) {
Dmitry Shmidtf7e0a992013-05-23 11:03:10 -07003033 if (*go == 0) {
3034 /* We are the client */
3035 wpa_printf(MSG_DEBUG, "P2P: Peer was found to be "
3036 "running a GO but we are capable of MCC, "
3037 "figure out the best channel to use");
3038 *force_freq = 0;
3039 } else if (!freq_included(channels, *force_freq)) {
3040 /* We are the GO, and *force_freq is not in the
3041 * intersection */
3042 wpa_printf(MSG_DEBUG, "P2P: Forced GO freq %d MHz not "
3043 "in intersection but we are capable of MCC, "
3044 "figure out the best channel to use",
3045 *force_freq);
3046 *force_freq = 0;
3047 }
3048 }
3049
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003050 return P2P_SC_SUCCESS;
3051}
3052
3053
3054static void wpas_invitation_received(void *ctx, const u8 *sa, const u8 *bssid,
3055 const u8 *ssid, size_t ssid_len,
3056 const u8 *go_dev_addr, u8 status,
3057 int op_freq)
3058{
3059 struct wpa_supplicant *wpa_s = ctx;
3060 struct wpa_ssid *s;
3061
3062 for (s = wpa_s->conf->ssid; s; s = s->next) {
3063 if (s->disabled == 2 &&
3064 s->ssid_len == ssid_len &&
3065 os_memcmp(ssid, s->ssid, ssid_len) == 0)
3066 break;
3067 }
3068
3069 if (status == P2P_SC_SUCCESS) {
3070 wpa_printf(MSG_DEBUG, "P2P: Invitation from peer " MACSTR
Dmitry Shmidt344abd32014-01-14 13:17:00 -08003071 " was accepted; op_freq=%d MHz, SSID=%s",
3072 MAC2STR(sa), op_freq, wpa_ssid_txt(ssid, ssid_len));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003073 if (s) {
Dmitry Shmidt91c40cd2012-09-25 14:23:53 -07003074 int go = s->mode == WPAS_MODE_P2P_GO;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003075 wpas_p2p_group_add_persistent(
Dmitry Shmidt15907092014-03-25 10:42:57 -07003076 wpa_s, s, go, 0, op_freq, 0, 0, NULL,
Dmitry Shmidt56052862013-10-04 10:23:25 -07003077 go ? P2P_MAX_INITIAL_CONN_WAIT_GO_REINVOKE : 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003078 } else if (bssid) {
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08003079 wpa_s->user_initiated_pd = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003080 wpas_p2p_join(wpa_s, bssid, go_dev_addr,
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08003081 wpa_s->p2p_wps_method, 0, op_freq,
Dmitry Shmidt344abd32014-01-14 13:17:00 -08003082 ssid, ssid_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003083 }
3084 return;
3085 }
3086
3087 if (status != P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE) {
3088 wpa_printf(MSG_DEBUG, "P2P: Invitation from peer " MACSTR
3089 " was rejected (status %u)", MAC2STR(sa), status);
3090 return;
3091 }
3092
3093 if (!s) {
3094 if (bssid) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07003095 wpa_msg_global(wpa_s, MSG_INFO,
3096 P2P_EVENT_INVITATION_RECEIVED
3097 "sa=" MACSTR " go_dev_addr=" MACSTR
3098 " bssid=" MACSTR " unknown-network",
3099 MAC2STR(sa), MAC2STR(go_dev_addr),
3100 MAC2STR(bssid));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003101 } else {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07003102 wpa_msg_global(wpa_s, MSG_INFO,
3103 P2P_EVENT_INVITATION_RECEIVED
3104 "sa=" MACSTR " go_dev_addr=" MACSTR
3105 " unknown-network",
3106 MAC2STR(sa), MAC2STR(go_dev_addr));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003107 }
3108 return;
3109 }
3110
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08003111 if (s->mode == WPAS_MODE_P2P_GO && op_freq) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07003112 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_INVITATION_RECEIVED
3113 "sa=" MACSTR " persistent=%d freq=%d",
3114 MAC2STR(sa), s->id, op_freq);
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08003115 } else {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07003116 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_INVITATION_RECEIVED
3117 "sa=" MACSTR " persistent=%d",
3118 MAC2STR(sa), s->id);
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08003119 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003120}
3121
3122
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003123static void wpas_remove_persistent_peer(struct wpa_supplicant *wpa_s,
3124 struct wpa_ssid *ssid,
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07003125 const u8 *peer, int inv)
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003126{
3127 size_t i;
3128
3129 if (ssid == NULL)
3130 return;
3131
3132 for (i = 0; ssid->p2p_client_list && i < ssid->num_p2p_clients; i++) {
3133 if (os_memcmp(ssid->p2p_client_list + i * ETH_ALEN, peer,
3134 ETH_ALEN) == 0)
3135 break;
3136 }
3137 if (i >= ssid->num_p2p_clients) {
3138 if (ssid->mode != WPAS_MODE_P2P_GO &&
3139 os_memcmp(ssid->bssid, peer, ETH_ALEN) == 0) {
3140 wpa_printf(MSG_DEBUG, "P2P: Remove persistent group %d "
3141 "due to invitation result", ssid->id);
3142 wpas_notify_network_removed(wpa_s, ssid);
3143 wpa_config_remove_network(wpa_s->conf, ssid->id);
3144 return;
3145 }
3146 return; /* Peer not found in client list */
3147 }
3148
3149 wpa_printf(MSG_DEBUG, "P2P: Remove peer " MACSTR " from persistent "
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07003150 "group %d client list%s",
3151 MAC2STR(peer), ssid->id,
3152 inv ? " due to invitation result" : "");
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003153 os_memmove(ssid->p2p_client_list + i * ETH_ALEN,
3154 ssid->p2p_client_list + (i + 1) * ETH_ALEN,
3155 (ssid->num_p2p_clients - i - 1) * ETH_ALEN);
3156 ssid->num_p2p_clients--;
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003157 if (wpa_s->parent->conf->update_config &&
3158 wpa_config_write(wpa_s->parent->confname, wpa_s->parent->conf))
3159 wpa_printf(MSG_DEBUG, "P2P: Failed to update configuration");
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003160}
3161
3162
3163static void wpas_remove_persistent_client(struct wpa_supplicant *wpa_s,
3164 const u8 *peer)
3165{
3166 struct wpa_ssid *ssid;
3167
3168 wpa_s = wpa_s->global->p2p_invite_group;
3169 if (wpa_s == NULL)
3170 return; /* No known invitation group */
3171 ssid = wpa_s->current_ssid;
3172 if (ssid == NULL || ssid->mode != WPAS_MODE_P2P_GO ||
3173 !ssid->p2p_persistent_group)
3174 return; /* Not operating as a GO in persistent group */
3175 ssid = wpas_p2p_get_persistent(wpa_s->parent, peer,
3176 ssid->ssid, ssid->ssid_len);
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07003177 wpas_remove_persistent_peer(wpa_s, ssid, peer, 1);
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003178}
3179
3180
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08003181static void wpas_invitation_result(void *ctx, int status, const u8 *bssid,
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003182 const struct p2p_channels *channels,
Dmitry Shmidt15907092014-03-25 10:42:57 -07003183 const u8 *peer, int neg_freq,
3184 int peer_oper_freq)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003185{
3186 struct wpa_supplicant *wpa_s = ctx;
3187 struct wpa_ssid *ssid;
Dmitry Shmidt15907092014-03-25 10:42:57 -07003188 int freq;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003189
3190 if (bssid) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07003191 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_INVITATION_RESULT
3192 "status=%d " MACSTR,
3193 status, MAC2STR(bssid));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003194 } else {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07003195 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_INVITATION_RESULT
3196 "status=%d ", status);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003197 }
3198 wpas_notify_p2p_invitation_result(wpa_s, status, bssid);
3199
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003200 wpa_printf(MSG_DEBUG, "P2P: Invitation result - status=%d peer=" MACSTR,
3201 status, MAC2STR(peer));
3202 if (wpa_s->pending_invite_ssid_id == -1) {
3203 if (status == P2P_SC_FAIL_UNKNOWN_GROUP)
3204 wpas_remove_persistent_client(wpa_s, peer);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003205 return; /* Invitation to active group */
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003206 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003207
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08003208 if (status == P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE) {
3209 wpa_printf(MSG_DEBUG, "P2P: Waiting for peer to start another "
3210 "invitation exchange to indicate readiness for "
3211 "re-invocation");
3212 }
3213
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003214 if (status != P2P_SC_SUCCESS) {
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003215 if (status == P2P_SC_FAIL_UNKNOWN_GROUP) {
3216 ssid = wpa_config_get_network(
3217 wpa_s->conf, wpa_s->pending_invite_ssid_id);
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07003218 wpas_remove_persistent_peer(wpa_s, ssid, peer, 1);
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003219 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003220 wpas_p2p_remove_pending_group_interface(wpa_s);
3221 return;
3222 }
3223
3224 ssid = wpa_config_get_network(wpa_s->conf,
3225 wpa_s->pending_invite_ssid_id);
3226 if (ssid == NULL) {
3227 wpa_printf(MSG_ERROR, "P2P: Could not find persistent group "
3228 "data matching with invitation");
3229 return;
3230 }
3231
Jouni Malinen2c5b17d2012-10-13 21:52:46 -07003232 /*
3233 * The peer could have missed our ctrl::ack frame for Invitation
3234 * Response and continue retransmitting the frame. To reduce the
3235 * likelihood of the peer not getting successful TX status for the
3236 * Invitation Response frame, wait a short time here before starting
3237 * the persistent group so that we will remain on the current channel to
3238 * acknowledge any possible retransmission from the peer.
3239 */
3240 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: 50 ms wait on current channel before "
3241 "starting persistent group");
3242 os_sleep(0, 50000);
3243
Dmitry Shmidt15907092014-03-25 10:42:57 -07003244 if (neg_freq > 0 && ssid->mode == WPAS_MODE_P2P_GO &&
3245 freq_included(channels, neg_freq))
3246 freq = neg_freq;
3247 else if (peer_oper_freq > 0 && ssid->mode != WPAS_MODE_P2P_GO &&
3248 freq_included(channels, peer_oper_freq))
3249 freq = peer_oper_freq;
3250 else
3251 freq = 0;
3252
3253 wpa_printf(MSG_DEBUG, "P2P: Persistent group invitation success - op_freq=%d MHz SSID=%s",
3254 freq, wpa_ssid_txt(ssid->ssid, ssid->ssid_len));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003255 wpas_p2p_group_add_persistent(wpa_s, ssid,
Jouni Malinen31be0a42012-08-31 21:20:51 +03003256 ssid->mode == WPAS_MODE_P2P_GO,
Dmitry Shmidt96be6222014-02-13 10:16:51 -08003257 wpa_s->p2p_persistent_go_freq,
Dmitry Shmidt15907092014-03-25 10:42:57 -07003258 freq,
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003259 wpa_s->p2p_go_ht40, wpa_s->p2p_go_vht,
3260 channels,
Dmitry Shmidt56052862013-10-04 10:23:25 -07003261 ssid->mode == WPAS_MODE_P2P_GO ?
3262 P2P_MAX_INITIAL_CONN_WAIT_GO_REINVOKE :
3263 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003264}
3265
3266
Dmitry Shmidt04949592012-07-19 12:16:46 -07003267static int wpas_p2p_disallowed_freq(struct wpa_global *global,
3268 unsigned int freq)
3269{
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08003270 if (freq_range_list_includes(&global->p2p_go_avoid_freq, freq))
3271 return 1;
Dmitry Shmidt4ce9c872013-10-24 11:08:13 -07003272 return freq_range_list_includes(&global->p2p_disallow_freq, freq);
Dmitry Shmidt04949592012-07-19 12:16:46 -07003273}
3274
3275
3276static void wpas_p2p_add_chan(struct p2p_reg_class *reg, u8 chan)
3277{
3278 reg->channel[reg->channels] = chan;
3279 reg->channels++;
3280}
3281
3282
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003283static int wpas_p2p_default_channels(struct wpa_supplicant *wpa_s,
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003284 struct p2p_channels *chan,
3285 struct p2p_channels *cli_chan)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003286{
3287 int i, cla = 0;
3288
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003289 os_memset(cli_chan, 0, sizeof(*cli_chan));
3290
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003291 wpa_printf(MSG_DEBUG, "P2P: Enable operating classes for 2.4 GHz "
3292 "band");
3293
3294 /* Operating class 81 - 2.4 GHz band channels 1..13 */
3295 chan->reg_class[cla].reg_class = 81;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003296 chan->reg_class[cla].channels = 0;
3297 for (i = 0; i < 11; i++) {
3298 if (!wpas_p2p_disallowed_freq(wpa_s->global, 2412 + i * 5))
3299 wpas_p2p_add_chan(&chan->reg_class[cla], i + 1);
3300 }
3301 if (chan->reg_class[cla].channels)
3302 cla++;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003303
3304 wpa_printf(MSG_DEBUG, "P2P: Enable operating classes for lower 5 GHz "
3305 "band");
3306
3307 /* Operating class 115 - 5 GHz, channels 36-48 */
3308 chan->reg_class[cla].reg_class = 115;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003309 chan->reg_class[cla].channels = 0;
3310 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 36 * 5))
3311 wpas_p2p_add_chan(&chan->reg_class[cla], 36);
3312 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 40 * 5))
3313 wpas_p2p_add_chan(&chan->reg_class[cla], 40);
3314 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 44 * 5))
3315 wpas_p2p_add_chan(&chan->reg_class[cla], 44);
3316 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 48 * 5))
3317 wpas_p2p_add_chan(&chan->reg_class[cla], 48);
3318 if (chan->reg_class[cla].channels)
3319 cla++;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003320
3321 wpa_printf(MSG_DEBUG, "P2P: Enable operating classes for higher 5 GHz "
3322 "band");
3323
3324 /* Operating class 124 - 5 GHz, channels 149,153,157,161 */
3325 chan->reg_class[cla].reg_class = 124;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003326 chan->reg_class[cla].channels = 0;
3327 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 149 * 5))
3328 wpas_p2p_add_chan(&chan->reg_class[cla], 149);
3329 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 153 * 5))
3330 wpas_p2p_add_chan(&chan->reg_class[cla], 153);
3331 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 156 * 5))
3332 wpas_p2p_add_chan(&chan->reg_class[cla], 157);
3333 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 161 * 5))
3334 wpas_p2p_add_chan(&chan->reg_class[cla], 161);
3335 if (chan->reg_class[cla].channels)
3336 cla++;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003337
3338 chan->reg_classes = cla;
3339 return 0;
3340}
3341
3342
3343static struct hostapd_hw_modes * get_mode(struct hostapd_hw_modes *modes,
3344 u16 num_modes,
3345 enum hostapd_hw_mode mode)
3346{
3347 u16 i;
3348
3349 for (i = 0; i < num_modes; i++) {
3350 if (modes[i].mode == mode)
3351 return &modes[i];
3352 }
3353
3354 return NULL;
3355}
3356
3357
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003358enum chan_allowed {
3359 NOT_ALLOWED, PASSIVE_ONLY, ALLOWED
3360};
3361
Dmitry Shmidt04949592012-07-19 12:16:46 -07003362static int has_channel(struct wpa_global *global,
3363 struct hostapd_hw_modes *mode, u8 chan, int *flags)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003364{
3365 int i;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003366 unsigned int freq;
3367
3368 freq = (mode->mode == HOSTAPD_MODE_IEEE80211A ? 5000 : 2407) +
3369 chan * 5;
3370 if (wpas_p2p_disallowed_freq(global, freq))
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003371 return NOT_ALLOWED;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003372
3373 for (i = 0; i < mode->num_channels; i++) {
3374 if (mode->channels[i].chan == chan) {
3375 if (flags)
3376 *flags = mode->channels[i].flag;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003377 if (mode->channels[i].flag &
3378 (HOSTAPD_CHAN_DISABLED |
3379 HOSTAPD_CHAN_RADAR))
3380 return NOT_ALLOWED;
3381 if (mode->channels[i].flag &
3382 (HOSTAPD_CHAN_PASSIVE_SCAN |
3383 HOSTAPD_CHAN_NO_IBSS))
3384 return PASSIVE_ONLY;
3385 return ALLOWED;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003386 }
3387 }
3388
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003389 return NOT_ALLOWED;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003390}
3391
3392
3393struct p2p_oper_class_map {
3394 enum hostapd_hw_mode mode;
3395 u8 op_class;
3396 u8 min_chan;
3397 u8 max_chan;
3398 u8 inc;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003399 enum { BW20, BW40PLUS, BW40MINUS, BW80 } bw;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003400};
3401
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003402static struct p2p_oper_class_map op_class[] = {
3403 { HOSTAPD_MODE_IEEE80211G, 81, 1, 13, 1, BW20 },
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003404#if 0 /* Do not enable HT40 on 2 GHz for now */
3405 { HOSTAPD_MODE_IEEE80211G, 83, 1, 9, 1, BW40PLUS },
3406 { HOSTAPD_MODE_IEEE80211G, 84, 5, 13, 1, BW40MINUS },
3407#endif
3408 { HOSTAPD_MODE_IEEE80211A, 115, 36, 48, 4, BW20 },
3409 { HOSTAPD_MODE_IEEE80211A, 124, 149, 161, 4, BW20 },
3410 { HOSTAPD_MODE_IEEE80211A, 116, 36, 44, 8, BW40PLUS },
3411 { HOSTAPD_MODE_IEEE80211A, 117, 40, 48, 8, BW40MINUS },
3412 { HOSTAPD_MODE_IEEE80211A, 126, 149, 157, 8, BW40PLUS },
3413 { HOSTAPD_MODE_IEEE80211A, 127, 153, 161, 8, BW40MINUS },
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003414
3415 /*
3416 * IEEE P802.11ac/D7.0 Table E-4 actually talks about channel center
3417 * frequency index 42, 58, 106, 122, 138, 155 with channel spacing of
3418 * 80 MHz, but currently use the following definition for simplicity
3419 * (these center frequencies are not actual channels, which makes
3420 * has_channel() fail). wpas_p2p_verify_80mhz() should take care of
3421 * removing invalid channels.
3422 */
3423 { HOSTAPD_MODE_IEEE80211A, 128, 36, 161, 4, BW80 },
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003424 { -1, 0, 0, 0, 0, BW20 }
3425};
3426
3427
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003428static int wpas_p2p_get_center_80mhz(struct wpa_supplicant *wpa_s,
3429 struct hostapd_hw_modes *mode,
3430 u8 channel)
3431{
3432 u8 center_channels[] = { 42, 58, 106, 122, 138, 155 };
3433 unsigned int i;
3434
3435 if (mode->mode != HOSTAPD_MODE_IEEE80211A)
3436 return 0;
3437
3438 for (i = 0; i < ARRAY_SIZE(center_channels); i++)
3439 /*
3440 * In 80 MHz, the bandwidth "spans" 12 channels (e.g., 36-48),
3441 * so the center channel is 6 channels away from the start/end.
3442 */
3443 if (channel >= center_channels[i] - 6 &&
3444 channel <= center_channels[i] + 6)
3445 return center_channels[i];
3446
3447 return 0;
3448}
3449
3450
3451static enum chan_allowed wpas_p2p_verify_80mhz(struct wpa_supplicant *wpa_s,
3452 struct hostapd_hw_modes *mode,
3453 u8 channel, u8 bw)
3454{
3455 u8 center_chan;
3456 int i, flags;
3457 enum chan_allowed res, ret = ALLOWED;
3458
3459 center_chan = wpas_p2p_get_center_80mhz(wpa_s, mode, channel);
3460 if (!center_chan)
3461 return NOT_ALLOWED;
3462 if (center_chan >= 58 && center_chan <= 138)
3463 return NOT_ALLOWED; /* Do not allow DFS channels for P2P */
3464
3465 /* check all the channels are available */
3466 for (i = 0; i < 4; i++) {
3467 int adj_chan = center_chan - 6 + i * 4;
3468
3469 res = has_channel(wpa_s->global, mode, adj_chan, &flags);
3470 if (res == NOT_ALLOWED)
3471 return NOT_ALLOWED;
3472 if (res == PASSIVE_ONLY)
3473 ret = PASSIVE_ONLY;
3474
3475 if (i == 0 && !(flags & HOSTAPD_CHAN_VHT_10_70))
3476 return NOT_ALLOWED;
3477 if (i == 1 && !(flags & HOSTAPD_CHAN_VHT_30_50))
3478 return NOT_ALLOWED;
3479 if (i == 2 && !(flags & HOSTAPD_CHAN_VHT_50_30))
3480 return NOT_ALLOWED;
3481 if (i == 3 && !(flags & HOSTAPD_CHAN_VHT_70_10))
3482 return NOT_ALLOWED;
3483 }
3484
3485 return ret;
3486}
3487
3488
3489static enum chan_allowed wpas_p2p_verify_channel(struct wpa_supplicant *wpa_s,
3490 struct hostapd_hw_modes *mode,
3491 u8 channel, u8 bw)
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003492{
Dmitry Shmidt96be6222014-02-13 10:16:51 -08003493 int flag = 0;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003494 enum chan_allowed res, res2;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003495
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003496 res2 = res = has_channel(wpa_s->global, mode, channel, &flag);
3497 if (bw == BW40MINUS) {
3498 if (!(flag & HOSTAPD_CHAN_HT40MINUS))
3499 return NOT_ALLOWED;
3500 res2 = has_channel(wpa_s->global, mode, channel - 4, NULL);
3501 } else if (bw == BW40PLUS) {
3502 if (!(flag & HOSTAPD_CHAN_HT40PLUS))
3503 return NOT_ALLOWED;
3504 res2 = has_channel(wpa_s->global, mode, channel + 4, NULL);
3505 } else if (bw == BW80) {
3506 res2 = wpas_p2p_verify_80mhz(wpa_s, mode, channel, bw);
3507 }
3508
3509 if (res == NOT_ALLOWED || res2 == NOT_ALLOWED)
3510 return NOT_ALLOWED;
3511 if (res == PASSIVE_ONLY || res2 == PASSIVE_ONLY)
3512 return PASSIVE_ONLY;
3513 return res;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003514}
3515
3516
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003517static int wpas_p2p_setup_channels(struct wpa_supplicant *wpa_s,
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003518 struct p2p_channels *chan,
3519 struct p2p_channels *cli_chan)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003520{
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003521 struct hostapd_hw_modes *mode;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003522 int cla, op, cli_cla;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003523
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003524 if (wpa_s->hw.modes == NULL) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003525 wpa_printf(MSG_DEBUG, "P2P: Driver did not support fetching "
3526 "of all supported channels; assume dualband "
3527 "support");
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003528 return wpas_p2p_default_channels(wpa_s, chan, cli_chan);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003529 }
3530
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003531 cla = cli_cla = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003532
3533 for (op = 0; op_class[op].op_class; op++) {
3534 struct p2p_oper_class_map *o = &op_class[op];
3535 u8 ch;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003536 struct p2p_reg_class *reg = NULL, *cli_reg = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003537
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003538 mode = get_mode(wpa_s->hw.modes, wpa_s->hw.num_modes, o->mode);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003539 if (mode == NULL)
3540 continue;
3541 for (ch = o->min_chan; ch <= o->max_chan; ch += o->inc) {
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003542 enum chan_allowed res;
3543 res = wpas_p2p_verify_channel(wpa_s, mode, ch, o->bw);
3544 if (res == ALLOWED) {
3545 if (reg == NULL) {
3546 wpa_printf(MSG_DEBUG, "P2P: Add operating class %u",
3547 o->op_class);
3548 reg = &chan->reg_class[cla];
3549 cla++;
3550 reg->reg_class = o->op_class;
3551 }
3552 reg->channel[reg->channels] = ch;
3553 reg->channels++;
3554 } else if (res == PASSIVE_ONLY &&
3555 wpa_s->conf->p2p_add_cli_chan) {
3556 if (cli_reg == NULL) {
3557 wpa_printf(MSG_DEBUG, "P2P: Add operating class %u (client only)",
3558 o->op_class);
3559 cli_reg = &cli_chan->reg_class[cli_cla];
3560 cli_cla++;
3561 cli_reg->reg_class = o->op_class;
3562 }
3563 cli_reg->channel[cli_reg->channels] = ch;
3564 cli_reg->channels++;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003565 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003566 }
3567 if (reg) {
3568 wpa_hexdump(MSG_DEBUG, "P2P: Channels",
3569 reg->channel, reg->channels);
3570 }
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003571 if (cli_reg) {
3572 wpa_hexdump(MSG_DEBUG, "P2P: Channels (client only)",
3573 cli_reg->channel, cli_reg->channels);
3574 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003575 }
3576
3577 chan->reg_classes = cla;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003578 cli_chan->reg_classes = cli_cla;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003579
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003580 return 0;
3581}
3582
3583
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003584int wpas_p2p_get_ht40_mode(struct wpa_supplicant *wpa_s,
3585 struct hostapd_hw_modes *mode, u8 channel)
3586{
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003587 int op;
3588 enum chan_allowed ret;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003589
3590 for (op = 0; op_class[op].op_class; op++) {
3591 struct p2p_oper_class_map *o = &op_class[op];
3592 u8 ch;
3593
3594 for (ch = o->min_chan; ch <= o->max_chan; ch += o->inc) {
3595 if (o->mode != HOSTAPD_MODE_IEEE80211A ||
3596 o->bw == BW20 || ch != channel)
3597 continue;
3598 ret = wpas_p2p_verify_channel(wpa_s, mode, ch, o->bw);
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003599 if (ret == ALLOWED)
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003600 return (o->bw == BW40MINUS) ? -1 : 1;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003601 }
3602 }
3603 return 0;
3604}
3605
3606
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003607int wpas_p2p_get_vht80_center(struct wpa_supplicant *wpa_s,
3608 struct hostapd_hw_modes *mode, u8 channel)
3609{
3610 if (!wpas_p2p_verify_channel(wpa_s, mode, channel, BW80))
3611 return 0;
3612
3613 return wpas_p2p_get_center_80mhz(wpa_s, mode, channel);
3614}
3615
3616
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003617static int wpas_get_noa(void *ctx, const u8 *interface_addr, u8 *buf,
3618 size_t buf_len)
3619{
3620 struct wpa_supplicant *wpa_s = ctx;
3621
3622 for (wpa_s = wpa_s->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
3623 if (os_memcmp(wpa_s->own_addr, interface_addr, ETH_ALEN) == 0)
3624 break;
3625 }
3626 if (wpa_s == NULL)
3627 return -1;
3628
3629 return wpa_drv_get_noa(wpa_s, buf, buf_len);
3630}
3631
3632
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08003633static int wpas_go_connected(void *ctx, const u8 *dev_addr)
3634{
3635 struct wpa_supplicant *wpa_s = ctx;
3636
3637 for (wpa_s = wpa_s->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
3638 struct wpa_ssid *ssid = wpa_s->current_ssid;
3639 if (ssid == NULL)
3640 continue;
3641 if (ssid->mode != WPAS_MODE_INFRA)
3642 continue;
3643 if (wpa_s->wpa_state != WPA_COMPLETED &&
3644 wpa_s->wpa_state != WPA_GROUP_HANDSHAKE)
3645 continue;
3646 if (os_memcmp(wpa_s->go_dev_addr, dev_addr, ETH_ALEN) == 0)
3647 return 1;
3648 }
3649
3650 return 0;
3651}
3652
3653
Dmitry Shmidt18463232014-01-24 12:29:41 -08003654static int wpas_is_concurrent_session_active(void *ctx)
3655{
3656 struct wpa_supplicant *wpa_s = ctx;
3657 struct wpa_supplicant *ifs;
3658
3659 for (ifs = wpa_s->global->ifaces; ifs; ifs = ifs->next) {
3660 if (ifs == wpa_s)
3661 continue;
3662 if (ifs->wpa_state > WPA_ASSOCIATED)
3663 return 1;
3664 }
3665 return 0;
3666}
3667
3668
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07003669static void wpas_p2p_debug_print(void *ctx, int level, const char *msg)
3670{
3671 struct wpa_supplicant *wpa_s = ctx;
3672 wpa_msg_global(wpa_s, level, "P2P: %s", msg);
3673}
3674
3675
Dmitry Shmidt34af3062013-07-11 10:46:32 -07003676int wpas_p2p_add_p2pdev_interface(struct wpa_supplicant *wpa_s)
3677{
3678 struct wpa_interface iface;
3679 struct wpa_supplicant *p2pdev_wpa_s;
3680 char ifname[100];
3681 char force_name[100];
3682 int ret;
3683
3684 os_snprintf(ifname, sizeof(ifname), P2P_MGMT_DEVICE_PREFIX "%s",
3685 wpa_s->ifname);
3686 force_name[0] = '\0';
3687 wpa_s->pending_interface_type = WPA_IF_P2P_DEVICE;
3688 ret = wpa_drv_if_add(wpa_s, WPA_IF_P2P_DEVICE, ifname, NULL, NULL,
3689 force_name, wpa_s->pending_interface_addr, NULL);
3690 if (ret < 0) {
3691 wpa_printf(MSG_DEBUG, "P2P: Failed to create P2P Device interface");
3692 return ret;
3693 }
3694 os_strlcpy(wpa_s->pending_interface_name, ifname,
3695 sizeof(wpa_s->pending_interface_name));
3696
3697 os_memset(&iface, 0, sizeof(iface));
3698 iface.p2p_mgmt = 1;
3699 iface.ifname = wpa_s->pending_interface_name;
3700 iface.driver = wpa_s->driver->name;
3701 iface.driver_param = wpa_s->conf->driver_param;
Dmitry Shmidt2ac5f602014-03-07 10:08:21 -08003702
3703 /*
3704 * If a P2P Device configuration file was given, use it as the interface
3705 * configuration file (instead of using parent's configuration file.
3706 */
3707 if (wpa_s->conf_p2p_dev) {
3708 iface.confname = wpa_s->conf_p2p_dev;
3709 iface.ctrl_interface = NULL;
3710 } else {
3711 iface.confname = wpa_s->confname;
3712 iface.ctrl_interface = wpa_s->conf->ctrl_interface;
3713 }
3714 iface.conf_p2p_dev = NULL;
3715
Dmitry Shmidt34af3062013-07-11 10:46:32 -07003716 p2pdev_wpa_s = wpa_supplicant_add_iface(wpa_s->global, &iface);
3717 if (!p2pdev_wpa_s) {
3718 wpa_printf(MSG_DEBUG, "P2P: Failed to add P2P Device interface");
3719 return -1;
3720 }
3721 p2pdev_wpa_s->parent = wpa_s;
3722
3723 wpa_s->pending_interface_name[0] = '\0';
3724 return 0;
3725}
3726
3727
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08003728static void wpas_presence_resp(void *ctx, const u8 *src, u8 status,
3729 const u8 *noa, size_t noa_len)
3730{
3731 struct wpa_supplicant *wpa_s, *intf = ctx;
3732 char hex[100];
3733
3734 for (wpa_s = intf->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
3735 if (wpa_s->waiting_presence_resp)
3736 break;
3737 }
3738 if (!wpa_s) {
3739 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: No group interface was waiting for presence response");
3740 return;
3741 }
3742 wpa_s->waiting_presence_resp = 0;
3743
3744 wpa_snprintf_hex(hex, sizeof(hex), noa, noa_len);
3745 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_PRESENCE_RESPONSE "src=" MACSTR
3746 " status=%u noa=%s", MAC2STR(src), status, hex);
3747}
3748
3749
Dmitry Shmidt5a1480c2014-05-12 09:46:02 -07003750static int _wpas_p2p_in_progress(void *ctx)
3751{
3752 struct wpa_supplicant *wpa_s = ctx;
3753 return wpas_p2p_in_progress(wpa_s);
3754}
3755
3756
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003757/**
3758 * wpas_p2p_init - Initialize P2P module for %wpa_supplicant
3759 * @global: Pointer to global data from wpa_supplicant_init()
3760 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
3761 * Returns: 0 on success, -1 on failure
3762 */
3763int wpas_p2p_init(struct wpa_global *global, struct wpa_supplicant *wpa_s)
3764{
3765 struct p2p_config p2p;
3766 unsigned int r;
3767 int i;
3768
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07003769 if (wpa_s->conf->p2p_disabled)
3770 return 0;
3771
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003772 if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_CAPABLE))
3773 return 0;
3774
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003775 if (global->p2p)
3776 return 0;
3777
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003778 os_memset(&p2p, 0, sizeof(p2p));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003779 p2p.cb_ctx = wpa_s;
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07003780 p2p.debug_print = wpas_p2p_debug_print;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003781 p2p.p2p_scan = wpas_p2p_scan;
3782 p2p.send_action = wpas_send_action;
3783 p2p.send_action_done = wpas_send_action_done;
3784 p2p.go_neg_completed = wpas_go_neg_completed;
3785 p2p.go_neg_req_rx = wpas_go_neg_req_rx;
3786 p2p.dev_found = wpas_dev_found;
3787 p2p.dev_lost = wpas_dev_lost;
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07003788 p2p.find_stopped = wpas_find_stopped;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003789 p2p.start_listen = wpas_start_listen;
3790 p2p.stop_listen = wpas_stop_listen;
3791 p2p.send_probe_resp = wpas_send_probe_resp;
3792 p2p.sd_request = wpas_sd_request;
3793 p2p.sd_response = wpas_sd_response;
3794 p2p.prov_disc_req = wpas_prov_disc_req;
3795 p2p.prov_disc_resp = wpas_prov_disc_resp;
Jouni Malinen75ecf522011-06-27 15:19:46 -07003796 p2p.prov_disc_fail = wpas_prov_disc_fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003797 p2p.invitation_process = wpas_invitation_process;
3798 p2p.invitation_received = wpas_invitation_received;
3799 p2p.invitation_result = wpas_invitation_result;
3800 p2p.get_noa = wpas_get_noa;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08003801 p2p.go_connected = wpas_go_connected;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08003802 p2p.presence_resp = wpas_presence_resp;
Dmitry Shmidt18463232014-01-24 12:29:41 -08003803 p2p.is_concurrent_session_active = wpas_is_concurrent_session_active;
Dmitry Shmidt5a1480c2014-05-12 09:46:02 -07003804 p2p.is_p2p_in_progress = _wpas_p2p_in_progress;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003805
3806 os_memcpy(wpa_s->global->p2p_dev_addr, wpa_s->own_addr, ETH_ALEN);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003807 os_memcpy(p2p.dev_addr, wpa_s->global->p2p_dev_addr, ETH_ALEN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003808 p2p.dev_name = wpa_s->conf->device_name;
3809 p2p.manufacturer = wpa_s->conf->manufacturer;
3810 p2p.model_name = wpa_s->conf->model_name;
3811 p2p.model_number = wpa_s->conf->model_number;
3812 p2p.serial_number = wpa_s->conf->serial_number;
3813 if (wpa_s->wps) {
3814 os_memcpy(p2p.uuid, wpa_s->wps->uuid, 16);
3815 p2p.config_methods = wpa_s->wps->config_methods;
3816 }
3817
3818 if (wpa_s->conf->p2p_listen_reg_class &&
3819 wpa_s->conf->p2p_listen_channel) {
3820 p2p.reg_class = wpa_s->conf->p2p_listen_reg_class;
3821 p2p.channel = wpa_s->conf->p2p_listen_channel;
3822 } else {
3823 p2p.reg_class = 81;
3824 /*
3825 * Pick one of the social channels randomly as the listen
3826 * channel.
3827 */
3828 os_get_random((u8 *) &r, sizeof(r));
3829 p2p.channel = 1 + (r % 3) * 5;
3830 }
3831 wpa_printf(MSG_DEBUG, "P2P: Own listen channel: %d", p2p.channel);
3832
3833 if (wpa_s->conf->p2p_oper_reg_class &&
3834 wpa_s->conf->p2p_oper_channel) {
3835 p2p.op_reg_class = wpa_s->conf->p2p_oper_reg_class;
3836 p2p.op_channel = wpa_s->conf->p2p_oper_channel;
3837 p2p.cfg_op_channel = 1;
3838 wpa_printf(MSG_DEBUG, "P2P: Configured operating channel: "
3839 "%d:%d", p2p.op_reg_class, p2p.op_channel);
3840
3841 } else {
3842 p2p.op_reg_class = 81;
3843 /*
3844 * Use random operation channel from (1, 6, 11) if no other
3845 * preference is indicated.
3846 */
3847 os_get_random((u8 *) &r, sizeof(r));
3848 p2p.op_channel = 1 + (r % 3) * 5;
3849 p2p.cfg_op_channel = 0;
3850 wpa_printf(MSG_DEBUG, "P2P: Random operating channel: "
3851 "%d:%d", p2p.op_reg_class, p2p.op_channel);
3852 }
Dmitry Shmidt44c95782013-05-17 09:51:35 -07003853
3854 if (wpa_s->conf->p2p_pref_chan && wpa_s->conf->num_p2p_pref_chan) {
3855 p2p.pref_chan = wpa_s->conf->p2p_pref_chan;
3856 p2p.num_pref_chan = wpa_s->conf->num_p2p_pref_chan;
3857 }
3858
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003859 if (wpa_s->conf->country[0] && wpa_s->conf->country[1]) {
3860 os_memcpy(p2p.country, wpa_s->conf->country, 2);
3861 p2p.country[2] = 0x04;
3862 } else
3863 os_memcpy(p2p.country, "XX\x04", 3);
3864
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003865 if (wpas_p2p_setup_channels(wpa_s, &p2p.channels, &p2p.cli_channels)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003866 wpa_printf(MSG_ERROR, "P2P: Failed to configure supported "
3867 "channel list");
3868 return -1;
3869 }
3870
3871 os_memcpy(p2p.pri_dev_type, wpa_s->conf->device_type,
3872 WPS_DEV_TYPE_LEN);
3873
3874 p2p.num_sec_dev_types = wpa_s->conf->num_sec_device_types;
3875 os_memcpy(p2p.sec_dev_type, wpa_s->conf->sec_device_type,
3876 p2p.num_sec_dev_types * WPS_DEV_TYPE_LEN);
3877
3878 p2p.concurrent_operations = !!(wpa_s->drv_flags &
3879 WPA_DRIVER_FLAGS_P2P_CONCURRENT);
3880
3881 p2p.max_peers = 100;
3882
3883 if (wpa_s->conf->p2p_ssid_postfix) {
3884 p2p.ssid_postfix_len =
3885 os_strlen(wpa_s->conf->p2p_ssid_postfix);
3886 if (p2p.ssid_postfix_len > sizeof(p2p.ssid_postfix))
3887 p2p.ssid_postfix_len = sizeof(p2p.ssid_postfix);
3888 os_memcpy(p2p.ssid_postfix, wpa_s->conf->p2p_ssid_postfix,
3889 p2p.ssid_postfix_len);
3890 }
3891
3892 p2p.p2p_intra_bss = wpa_s->conf->p2p_intra_bss;
3893
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08003894 p2p.max_listen = wpa_s->max_remain_on_chan;
3895
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003896 global->p2p = p2p_init(&p2p);
3897 if (global->p2p == NULL)
3898 return -1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003899 global->p2p_init_wpa_s = wpa_s;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003900
3901 for (i = 0; i < MAX_WPS_VENDOR_EXT; i++) {
3902 if (wpa_s->conf->wps_vendor_ext[i] == NULL)
3903 continue;
3904 p2p_add_wps_vendor_extension(
3905 global->p2p, wpa_s->conf->wps_vendor_ext[i]);
3906 }
3907
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003908 p2p_set_no_go_freq(global->p2p, &wpa_s->conf->p2p_no_go_freq);
3909
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003910 return 0;
3911}
3912
3913
3914/**
3915 * wpas_p2p_deinit - Deinitialize per-interface P2P data
3916 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
3917 *
3918 * This function deinitialize per-interface P2P data.
3919 */
3920void wpas_p2p_deinit(struct wpa_supplicant *wpa_s)
3921{
3922 if (wpa_s->driver && wpa_s->drv_priv)
3923 wpa_drv_probe_req_report(wpa_s, 0);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003924
3925 if (wpa_s->go_params) {
3926 /* Clear any stored provisioning info */
3927 p2p_clear_provisioning_info(
3928 wpa_s->global->p2p,
Dmitry Shmidt04949592012-07-19 12:16:46 -07003929 wpa_s->go_params->peer_device_addr);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003930 }
3931
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003932 os_free(wpa_s->go_params);
3933 wpa_s->go_params = NULL;
Dmitry Shmidt684785c2014-05-12 13:34:29 -07003934 eloop_cancel_timeout(wpas_p2p_psk_failure_removal, wpa_s, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003935 eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s, NULL);
3936 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
3937 wpa_s->p2p_long_listen = 0;
3938 eloop_cancel_timeout(wpas_p2p_long_listen_timeout, wpa_s, NULL);
3939 eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL);
3940 wpas_p2p_remove_pending_group_interface(wpa_s);
Dmitry Shmidt04f534e2013-12-09 15:50:16 -08003941 eloop_cancel_timeout(wpas_p2p_group_freq_conflict, wpa_s, NULL);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08003942 wpas_p2p_listen_work_done(wpa_s);
3943 if (wpa_s->p2p_send_action_work) {
3944 os_free(wpa_s->p2p_send_action_work->ctx);
3945 radio_work_done(wpa_s->p2p_send_action_work);
3946 wpa_s->p2p_send_action_work = NULL;
3947 }
3948 eloop_cancel_timeout(wpas_p2p_send_action_work_timeout, wpa_s, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003949
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08003950 wpabuf_free(wpa_s->p2p_oob_dev_pw);
3951 wpa_s->p2p_oob_dev_pw = NULL;
3952
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003953 /* TODO: remove group interface from the driver if this wpa_s instance
3954 * is on top of a P2P group interface */
3955}
3956
3957
3958/**
3959 * wpas_p2p_deinit_global - Deinitialize global P2P module
3960 * @global: Pointer to global data from wpa_supplicant_init()
3961 *
3962 * This function deinitializes the global (per device) P2P module.
3963 */
3964void wpas_p2p_deinit_global(struct wpa_global *global)
3965{
3966 struct wpa_supplicant *wpa_s, *tmp;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003967
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003968 wpa_s = global->ifaces;
3969 if (wpa_s)
3970 wpas_p2p_service_flush(wpa_s);
3971
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003972 if (global->p2p == NULL)
3973 return;
3974
3975 /* Remove remaining P2P group interfaces */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003976 while (wpa_s && wpa_s->p2p_group_interface != NOT_P2P_GROUP_INTERFACE)
3977 wpa_s = wpa_s->next;
3978 while (wpa_s) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003979 tmp = global->ifaces;
3980 while (tmp &&
3981 (tmp == wpa_s ||
3982 tmp->p2p_group_interface == NOT_P2P_GROUP_INTERFACE)) {
3983 tmp = tmp->next;
3984 }
3985 if (tmp == NULL)
3986 break;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003987 /* Disconnect from the P2P group and deinit the interface */
3988 wpas_p2p_disconnect(tmp);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003989 }
3990
3991 /*
3992 * Deinit GO data on any possibly remaining interface (if main
3993 * interface is used as GO).
3994 */
3995 for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
3996 if (wpa_s->ap_iface)
3997 wpas_p2p_group_deinit(wpa_s);
3998 }
3999
4000 p2p_deinit(global->p2p);
4001 global->p2p = NULL;
Dmitry Shmidt04949592012-07-19 12:16:46 -07004002 global->p2p_init_wpa_s = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004003}
4004
4005
4006static int wpas_p2p_create_iface(struct wpa_supplicant *wpa_s)
4007{
Dmitry Shmidt34af3062013-07-11 10:46:32 -07004008 if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_DEDICATED_P2P_DEVICE) &&
4009 wpa_s->conf->p2p_no_group_iface)
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08004010 return 0; /* separate interface disabled per configuration */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004011 if (wpa_s->drv_flags &
4012 (WPA_DRIVER_FLAGS_P2P_DEDICATED_INTERFACE |
4013 WPA_DRIVER_FLAGS_P2P_MGMT_AND_NON_P2P))
4014 return 1; /* P2P group requires a new interface in every case
4015 */
4016 if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_CONCURRENT))
4017 return 0; /* driver does not support concurrent operations */
4018 if (wpa_s->global->ifaces->next)
4019 return 1; /* more that one interface already in use */
4020 if (wpa_s->wpa_state >= WPA_AUTHENTICATING)
4021 return 1; /* this interface is already in use */
4022 return 0;
4023}
4024
4025
4026static int wpas_p2p_start_go_neg(struct wpa_supplicant *wpa_s,
4027 const u8 *peer_addr,
4028 enum p2p_wps_method wps_method,
4029 int go_intent, const u8 *own_interface_addr,
Dmitry Shmidt04949592012-07-19 12:16:46 -07004030 unsigned int force_freq, int persistent_group,
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08004031 struct wpa_ssid *ssid, unsigned int pref_freq)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004032{
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004033 if (persistent_group && wpa_s->conf->persistent_reconnect)
4034 persistent_group = 2;
4035
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004036 /*
4037 * Increase GO config timeout if HT40 is used since it takes some time
4038 * to scan channels for coex purposes before the BSS can be started.
4039 */
4040 p2p_set_config_timeout(wpa_s->global->p2p,
4041 wpa_s->p2p_go_ht40 ? 255 : 100, 20);
4042
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004043 return p2p_connect(wpa_s->global->p2p, peer_addr, wps_method,
4044 go_intent, own_interface_addr, force_freq,
Dmitry Shmidt04949592012-07-19 12:16:46 -07004045 persistent_group, ssid ? ssid->ssid : NULL,
4046 ssid ? ssid->ssid_len : 0,
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08004047 wpa_s->p2p_pd_before_go_neg, pref_freq,
4048 wps_method == WPS_NFC ? wpa_s->p2p_oob_dev_pw_id :
4049 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004050}
4051
4052
4053static int wpas_p2p_auth_go_neg(struct wpa_supplicant *wpa_s,
4054 const u8 *peer_addr,
4055 enum p2p_wps_method wps_method,
4056 int go_intent, const u8 *own_interface_addr,
Dmitry Shmidt04949592012-07-19 12:16:46 -07004057 unsigned int force_freq, int persistent_group,
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08004058 struct wpa_ssid *ssid, unsigned int pref_freq)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004059{
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004060 if (persistent_group && wpa_s->conf->persistent_reconnect)
4061 persistent_group = 2;
4062
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004063 return p2p_authorize(wpa_s->global->p2p, peer_addr, wps_method,
4064 go_intent, own_interface_addr, force_freq,
Dmitry Shmidt04949592012-07-19 12:16:46 -07004065 persistent_group, ssid ? ssid->ssid : NULL,
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08004066 ssid ? ssid->ssid_len : 0, pref_freq,
4067 wps_method == WPS_NFC ? wpa_s->p2p_oob_dev_pw_id :
4068 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004069}
4070
4071
4072static void wpas_p2p_check_join_scan_limit(struct wpa_supplicant *wpa_s)
4073{
4074 wpa_s->p2p_join_scan_count++;
4075 wpa_printf(MSG_DEBUG, "P2P: Join scan attempt %d",
4076 wpa_s->p2p_join_scan_count);
4077 if (wpa_s->p2p_join_scan_count > P2P_MAX_JOIN_SCAN_ATTEMPTS) {
4078 wpa_printf(MSG_DEBUG, "P2P: Failed to find GO " MACSTR
4079 " for join operationg - stop join attempt",
4080 MAC2STR(wpa_s->pending_join_iface_addr));
4081 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
Dmitry Shmidt04949592012-07-19 12:16:46 -07004082 if (wpa_s->p2p_auto_pd) {
4083 wpa_s->p2p_auto_pd = 0;
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07004084 wpa_msg_global(wpa_s, MSG_INFO,
4085 P2P_EVENT_PROV_DISC_FAILURE
4086 " p2p_dev_addr=" MACSTR " status=N/A",
4087 MAC2STR(wpa_s->pending_join_dev_addr));
Dmitry Shmidt04949592012-07-19 12:16:46 -07004088 return;
4089 }
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07004090 wpa_msg_global(wpa_s->parent, MSG_INFO,
4091 P2P_EVENT_GROUP_FORMATION_FAILURE);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004092 }
4093}
4094
4095
Dmitry Shmidt04949592012-07-19 12:16:46 -07004096static int wpas_check_freq_conflict(struct wpa_supplicant *wpa_s, int freq)
4097{
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07004098 int *freqs, res, num, i;
Dmitry Shmidt04949592012-07-19 12:16:46 -07004099
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07004100 if (wpas_p2p_num_unused_channels(wpa_s) > 0) {
4101 /* Multiple channels are supported and not all are in use */
Dmitry Shmidt04949592012-07-19 12:16:46 -07004102 return 0;
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07004103 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07004104
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07004105 freqs = os_calloc(wpa_s->num_multichan_concurrent, sizeof(int));
4106 if (!freqs)
4107 return 1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07004108
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07004109 num = wpas_p2p_valid_oper_freqs(wpa_s, freqs,
4110 wpa_s->num_multichan_concurrent);
4111 if (num < 0) {
4112 res = 1;
4113 goto exit_free;
4114 }
4115
4116 for (i = 0; i < num; i++) {
4117 if (freqs[i] == freq) {
4118 wpa_printf(MSG_DEBUG, "P2P: Frequency %d MHz in use by another virtual interface and can be used",
4119 freq);
4120 res = 0;
4121 goto exit_free;
Dmitry Shmidt04949592012-07-19 12:16:46 -07004122 }
4123 }
4124
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07004125 res = 1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07004126
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07004127exit_free:
4128 os_free(freqs);
4129 return res;
Dmitry Shmidt04949592012-07-19 12:16:46 -07004130}
4131
4132
4133static int wpas_p2p_peer_go(struct wpa_supplicant *wpa_s,
4134 const u8 *peer_dev_addr)
4135{
4136 struct wpa_bss *bss;
4137 int updated;
4138
4139 bss = wpa_bss_get_p2p_dev_addr(wpa_s, peer_dev_addr);
4140 if (bss == NULL)
4141 return -1;
4142 if (bss->last_update_idx < wpa_s->bss_update_idx) {
4143 wpa_printf(MSG_DEBUG, "P2P: Peer BSS entry not updated in the "
4144 "last scan");
4145 return 0;
4146 }
4147
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08004148 updated = os_reltime_before(&wpa_s->p2p_auto_started,
4149 &bss->last_update);
Dmitry Shmidt04949592012-07-19 12:16:46 -07004150 wpa_printf(MSG_DEBUG, "P2P: Current BSS entry for peer updated at "
4151 "%ld.%06ld (%supdated in last scan)",
4152 bss->last_update.sec, bss->last_update.usec,
4153 updated ? "": "not ");
4154
4155 return updated;
4156}
4157
4158
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004159static void wpas_p2p_scan_res_join(struct wpa_supplicant *wpa_s,
4160 struct wpa_scan_results *scan_res)
4161{
Dmitry Shmidt344abd32014-01-14 13:17:00 -08004162 struct wpa_bss *bss = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004163 int freq;
4164 u8 iface_addr[ETH_ALEN];
Dmitry Shmidt04949592012-07-19 12:16:46 -07004165
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004166 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
4167
4168 if (wpa_s->global->p2p_disabled)
4169 return;
4170
Dmitry Shmidt04949592012-07-19 12:16:46 -07004171 wpa_printf(MSG_DEBUG, "P2P: Scan results received (%d BSS) for %sjoin",
4172 scan_res ? (int) scan_res->num : -1,
4173 wpa_s->p2p_auto_join ? "auto_" : "");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004174
4175 if (scan_res)
4176 wpas_p2p_scan_res_handler(wpa_s, scan_res);
4177
Dmitry Shmidt04949592012-07-19 12:16:46 -07004178 if (wpa_s->p2p_auto_pd) {
4179 int join = wpas_p2p_peer_go(wpa_s,
4180 wpa_s->pending_join_dev_addr);
4181 if (join == 0 &&
4182 wpa_s->auto_pd_scan_retry < P2P_AUTO_PD_SCAN_ATTEMPTS) {
4183 wpa_s->auto_pd_scan_retry++;
Dmitry Shmidt444d5672013-04-01 13:08:44 -07004184 bss = wpa_bss_get_bssid_latest(
4185 wpa_s, wpa_s->pending_join_dev_addr);
Dmitry Shmidt04949592012-07-19 12:16:46 -07004186 if (bss) {
4187 freq = bss->freq;
4188 wpa_printf(MSG_DEBUG, "P2P: Scan retry %d for "
4189 "the peer " MACSTR " at %d MHz",
4190 wpa_s->auto_pd_scan_retry,
4191 MAC2STR(wpa_s->
4192 pending_join_dev_addr),
4193 freq);
Dmitry Shmidt344abd32014-01-14 13:17:00 -08004194 wpas_p2p_join_scan_req(wpa_s, freq, NULL, 0);
Dmitry Shmidt04949592012-07-19 12:16:46 -07004195 return;
4196 }
4197 }
4198
4199 if (join < 0)
4200 join = 0;
4201
4202 wpa_s->p2p_auto_pd = 0;
4203 wpa_s->pending_pd_use = join ? AUTO_PD_JOIN : AUTO_PD_GO_NEG;
4204 wpa_printf(MSG_DEBUG, "P2P: Auto PD with " MACSTR " join=%d",
4205 MAC2STR(wpa_s->pending_join_dev_addr), join);
4206 if (p2p_prov_disc_req(wpa_s->global->p2p,
4207 wpa_s->pending_join_dev_addr,
4208 wpa_s->pending_pd_config_methods, join,
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08004209 0, wpa_s->user_initiated_pd) < 0) {
Dmitry Shmidt04949592012-07-19 12:16:46 -07004210 wpa_s->p2p_auto_pd = 0;
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07004211 wpa_msg_global(wpa_s, MSG_INFO,
4212 P2P_EVENT_PROV_DISC_FAILURE
4213 " p2p_dev_addr=" MACSTR " status=N/A",
4214 MAC2STR(wpa_s->pending_join_dev_addr));
Dmitry Shmidt04949592012-07-19 12:16:46 -07004215 }
4216 return;
4217 }
4218
4219 if (wpa_s->p2p_auto_join) {
4220 int join = wpas_p2p_peer_go(wpa_s,
4221 wpa_s->pending_join_dev_addr);
4222 if (join < 0) {
4223 wpa_printf(MSG_DEBUG, "P2P: Peer was not found to be "
4224 "running a GO -> use GO Negotiation");
4225 wpas_p2p_connect(wpa_s, wpa_s->pending_join_dev_addr,
4226 wpa_s->p2p_pin, wpa_s->p2p_wps_method,
4227 wpa_s->p2p_persistent_group, 0, 0, 0,
4228 wpa_s->p2p_go_intent,
4229 wpa_s->p2p_connect_freq,
4230 wpa_s->p2p_persistent_id,
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004231 wpa_s->p2p_pd_before_go_neg,
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004232 wpa_s->p2p_go_ht40,
4233 wpa_s->p2p_go_vht);
Dmitry Shmidt04949592012-07-19 12:16:46 -07004234 return;
4235 }
4236
4237 wpa_printf(MSG_DEBUG, "P2P: Peer was found running GO%s -> "
4238 "try to join the group", join ? "" :
4239 " in older scan");
4240 if (!join)
4241 wpa_s->p2p_fallback_to_go_neg = 1;
4242 }
4243
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004244 freq = p2p_get_oper_freq(wpa_s->global->p2p,
4245 wpa_s->pending_join_iface_addr);
4246 if (freq < 0 &&
4247 p2p_get_interface_addr(wpa_s->global->p2p,
4248 wpa_s->pending_join_dev_addr,
4249 iface_addr) == 0 &&
4250 os_memcmp(iface_addr, wpa_s->pending_join_dev_addr, ETH_ALEN) != 0)
4251 {
4252 wpa_printf(MSG_DEBUG, "P2P: Overwrite pending interface "
4253 "address for join from " MACSTR " to " MACSTR
4254 " based on newly discovered P2P peer entry",
4255 MAC2STR(wpa_s->pending_join_iface_addr),
4256 MAC2STR(iface_addr));
4257 os_memcpy(wpa_s->pending_join_iface_addr, iface_addr,
4258 ETH_ALEN);
4259
4260 freq = p2p_get_oper_freq(wpa_s->global->p2p,
4261 wpa_s->pending_join_iface_addr);
4262 }
4263 if (freq >= 0) {
4264 wpa_printf(MSG_DEBUG, "P2P: Target GO operating frequency "
4265 "from P2P peer table: %d MHz", freq);
4266 }
Dmitry Shmidt344abd32014-01-14 13:17:00 -08004267 if (wpa_s->p2p_join_ssid_len) {
4268 wpa_printf(MSG_DEBUG, "P2P: Trying to find target GO BSS entry based on BSSID "
4269 MACSTR " and SSID %s",
4270 MAC2STR(wpa_s->pending_join_iface_addr),
4271 wpa_ssid_txt(wpa_s->p2p_join_ssid,
4272 wpa_s->p2p_join_ssid_len));
4273 bss = wpa_bss_get(wpa_s, wpa_s->pending_join_iface_addr,
4274 wpa_s->p2p_join_ssid,
4275 wpa_s->p2p_join_ssid_len);
4276 }
4277 if (!bss) {
4278 wpa_printf(MSG_DEBUG, "P2P: Trying to find target GO BSS entry based on BSSID "
4279 MACSTR, MAC2STR(wpa_s->pending_join_iface_addr));
4280 bss = wpa_bss_get_bssid_latest(wpa_s,
4281 wpa_s->pending_join_iface_addr);
4282 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004283 if (bss) {
4284 freq = bss->freq;
4285 wpa_printf(MSG_DEBUG, "P2P: Target GO operating frequency "
Dmitry Shmidt051af732013-10-22 13:52:46 -07004286 "from BSS table: %d MHz (SSID %s)", freq,
4287 wpa_ssid_txt(bss->ssid, bss->ssid_len));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004288 }
4289 if (freq > 0) {
4290 u16 method;
4291
Dmitry Shmidt04949592012-07-19 12:16:46 -07004292 if (wpas_check_freq_conflict(wpa_s, freq) > 0) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07004293 wpa_msg_global(wpa_s->parent, MSG_INFO,
4294 P2P_EVENT_GROUP_FORMATION_FAILURE
4295 "reason=FREQ_CONFLICT");
Dmitry Shmidt04949592012-07-19 12:16:46 -07004296 return;
4297 }
4298
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004299 wpa_printf(MSG_DEBUG, "P2P: Send Provision Discovery Request "
4300 "prior to joining an existing group (GO " MACSTR
4301 " freq=%u MHz)",
4302 MAC2STR(wpa_s->pending_join_dev_addr), freq);
4303 wpa_s->pending_pd_before_join = 1;
4304
4305 switch (wpa_s->pending_join_wps_method) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004306 case WPS_PIN_DISPLAY:
4307 method = WPS_CONFIG_KEYPAD;
4308 break;
4309 case WPS_PIN_KEYPAD:
4310 method = WPS_CONFIG_DISPLAY;
4311 break;
4312 case WPS_PBC:
4313 method = WPS_CONFIG_PUSHBUTTON;
4314 break;
4315 default:
4316 method = 0;
4317 break;
4318 }
4319
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004320 if ((p2p_get_provisioning_info(wpa_s->global->p2p,
4321 wpa_s->pending_join_dev_addr) ==
4322 method)) {
4323 /*
4324 * We have already performed provision discovery for
4325 * joining the group. Proceed directly to join
4326 * operation without duplicated provision discovery. */
4327 wpa_printf(MSG_DEBUG, "P2P: Provision discovery "
4328 "with " MACSTR " already done - proceed to "
4329 "join",
4330 MAC2STR(wpa_s->pending_join_dev_addr));
4331 wpa_s->pending_pd_before_join = 0;
4332 goto start;
4333 }
4334
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004335 if (p2p_prov_disc_req(wpa_s->global->p2p,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004336 wpa_s->pending_join_dev_addr, method, 1,
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08004337 freq, wpa_s->user_initiated_pd) < 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004338 wpa_printf(MSG_DEBUG, "P2P: Failed to send Provision "
4339 "Discovery Request before joining an "
4340 "existing group");
4341 wpa_s->pending_pd_before_join = 0;
4342 goto start;
4343 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004344 return;
4345 }
4346
4347 wpa_printf(MSG_DEBUG, "P2P: Failed to find BSS/GO - try again later");
4348 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
4349 eloop_register_timeout(1, 0, wpas_p2p_join_scan, wpa_s, NULL);
4350 wpas_p2p_check_join_scan_limit(wpa_s);
4351 return;
4352
4353start:
4354 /* Start join operation immediately */
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08004355 wpas_p2p_join_start(wpa_s, 0, NULL, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004356}
4357
4358
Dmitry Shmidt344abd32014-01-14 13:17:00 -08004359static void wpas_p2p_join_scan_req(struct wpa_supplicant *wpa_s, int freq,
4360 const u8 *ssid, size_t ssid_len)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004361{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004362 int ret;
4363 struct wpa_driver_scan_params params;
4364 struct wpabuf *wps_ie, *ies;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004365 size_t ielen;
Dmitry Shmidt04949592012-07-19 12:16:46 -07004366 int freqs[2] = { 0, 0 };
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004367
4368 os_memset(&params, 0, sizeof(params));
4369
4370 /* P2P Wildcard SSID */
4371 params.num_ssids = 1;
Dmitry Shmidt344abd32014-01-14 13:17:00 -08004372 if (ssid && ssid_len) {
4373 params.ssids[0].ssid = ssid;
4374 params.ssids[0].ssid_len = ssid_len;
4375 os_memcpy(wpa_s->p2p_join_ssid, ssid, ssid_len);
4376 wpa_s->p2p_join_ssid_len = ssid_len;
4377 } else {
4378 params.ssids[0].ssid = (u8 *) P2P_WILDCARD_SSID;
4379 params.ssids[0].ssid_len = P2P_WILDCARD_SSID_LEN;
4380 wpa_s->p2p_join_ssid_len = 0;
4381 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004382
4383 wpa_s->wps->dev.p2p = 1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07004384 wps_ie = wps_build_probe_req_ie(DEV_PW_DEFAULT, &wpa_s->wps->dev,
4385 wpa_s->wps->uuid, WPS_REQ_ENROLLEE, 0,
4386 NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004387 if (wps_ie == NULL) {
4388 wpas_p2p_scan_res_join(wpa_s, NULL);
4389 return;
4390 }
4391
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004392 ielen = p2p_scan_ie_buf_len(wpa_s->global->p2p);
4393 ies = wpabuf_alloc(wpabuf_len(wps_ie) + ielen);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004394 if (ies == NULL) {
4395 wpabuf_free(wps_ie);
4396 wpas_p2p_scan_res_join(wpa_s, NULL);
4397 return;
4398 }
4399 wpabuf_put_buf(ies, wps_ie);
4400 wpabuf_free(wps_ie);
4401
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08004402 p2p_scan_ie(wpa_s->global->p2p, ies, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004403
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004404 params.p2p_probe = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004405 params.extra_ies = wpabuf_head(ies);
4406 params.extra_ies_len = wpabuf_len(ies);
Dmitry Shmidt9e3f8ee2014-01-17 10:52:01 -08004407
4408 if (!freq) {
4409 int oper_freq;
4410 /*
4411 * If freq is not provided, check the operating freq of the GO
4412 * and use a single channel scan on if possible.
4413 */
4414 oper_freq = p2p_get_oper_freq(wpa_s->global->p2p,
4415 wpa_s->pending_join_iface_addr);
4416 if (oper_freq > 0)
4417 freq = oper_freq;
4418 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07004419 if (freq > 0) {
4420 freqs[0] = freq;
4421 params.freqs = freqs;
4422 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004423
4424 /*
4425 * Run a scan to update BSS table and start Provision Discovery once
4426 * the new scan results become available.
4427 */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004428 ret = wpa_drv_scan(wpa_s, &params);
Dmitry Shmidt444d5672013-04-01 13:08:44 -07004429 if (!ret) {
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08004430 os_get_reltime(&wpa_s->scan_trigger_time);
Dmitry Shmidt04949592012-07-19 12:16:46 -07004431 wpa_s->scan_res_handler = wpas_p2p_scan_res_join;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08004432 wpa_s->own_scan_requested = 1;
Dmitry Shmidt444d5672013-04-01 13:08:44 -07004433 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004434
4435 wpabuf_free(ies);
4436
4437 if (ret) {
4438 wpa_printf(MSG_DEBUG, "P2P: Failed to start scan for join - "
4439 "try again later");
4440 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
4441 eloop_register_timeout(1, 0, wpas_p2p_join_scan, wpa_s, NULL);
4442 wpas_p2p_check_join_scan_limit(wpa_s);
4443 }
4444}
4445
4446
Dmitry Shmidt04949592012-07-19 12:16:46 -07004447static void wpas_p2p_join_scan(void *eloop_ctx, void *timeout_ctx)
4448{
4449 struct wpa_supplicant *wpa_s = eloop_ctx;
Dmitry Shmidt344abd32014-01-14 13:17:00 -08004450 wpas_p2p_join_scan_req(wpa_s, 0, NULL, 0);
Dmitry Shmidt04949592012-07-19 12:16:46 -07004451}
4452
4453
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004454static int wpas_p2p_join(struct wpa_supplicant *wpa_s, const u8 *iface_addr,
Dmitry Shmidt04949592012-07-19 12:16:46 -07004455 const u8 *dev_addr, enum p2p_wps_method wps_method,
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08004456 int auto_join, int op_freq,
4457 const u8 *ssid, size_t ssid_len)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004458{
4459 wpa_printf(MSG_DEBUG, "P2P: Request to join existing group (iface "
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08004460 MACSTR " dev " MACSTR " op_freq=%d)%s",
4461 MAC2STR(iface_addr), MAC2STR(dev_addr), op_freq,
Dmitry Shmidt04949592012-07-19 12:16:46 -07004462 auto_join ? " (auto_join)" : "");
Dmitry Shmidt344abd32014-01-14 13:17:00 -08004463 if (ssid && ssid_len) {
4464 wpa_printf(MSG_DEBUG, "P2P: Group SSID specified: %s",
4465 wpa_ssid_txt(ssid, ssid_len));
4466 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004467
Dmitry Shmidt04949592012-07-19 12:16:46 -07004468 wpa_s->p2p_auto_pd = 0;
4469 wpa_s->p2p_auto_join = !!auto_join;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004470 os_memcpy(wpa_s->pending_join_iface_addr, iface_addr, ETH_ALEN);
4471 os_memcpy(wpa_s->pending_join_dev_addr, dev_addr, ETH_ALEN);
4472 wpa_s->pending_join_wps_method = wps_method;
4473
4474 /* Make sure we are not running find during connection establishment */
4475 wpas_p2p_stop_find(wpa_s);
4476
4477 wpa_s->p2p_join_scan_count = 0;
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08004478 wpas_p2p_join_scan_req(wpa_s, op_freq, ssid, ssid_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004479 return 0;
4480}
4481
4482
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08004483static int wpas_p2p_join_start(struct wpa_supplicant *wpa_s, int freq,
4484 const u8 *ssid, size_t ssid_len)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004485{
4486 struct wpa_supplicant *group;
4487 struct p2p_go_neg_results res;
Dmitry Shmidt04949592012-07-19 12:16:46 -07004488 struct wpa_bss *bss;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004489
4490 group = wpas_p2p_get_group_iface(wpa_s, 0, 0);
4491 if (group == NULL)
4492 return -1;
4493 if (group != wpa_s) {
4494 os_memcpy(group->p2p_pin, wpa_s->p2p_pin,
4495 sizeof(group->p2p_pin));
4496 group->p2p_wps_method = wpa_s->p2p_wps_method;
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08004497 } else {
4498 /*
4499 * Need to mark the current interface for p2p_group_formation
4500 * when a separate group interface is not used. This is needed
4501 * to allow p2p_cancel stop a pending p2p_connect-join.
4502 * wpas_p2p_init_group_interface() addresses this for the case
4503 * where a separate group interface is used.
4504 */
4505 wpa_s->global->p2p_group_formation = wpa_s;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004506 }
4507
4508 group->p2p_in_provisioning = 1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07004509 group->p2p_fallback_to_go_neg = wpa_s->p2p_fallback_to_go_neg;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004510
4511 os_memset(&res, 0, sizeof(res));
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08004512 os_memcpy(res.peer_device_addr, wpa_s->pending_join_dev_addr, ETH_ALEN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004513 os_memcpy(res.peer_interface_addr, wpa_s->pending_join_iface_addr,
4514 ETH_ALEN);
4515 res.wps_method = wpa_s->pending_join_wps_method;
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08004516 if (freq && ssid && ssid_len) {
4517 res.freq = freq;
4518 res.ssid_len = ssid_len;
4519 os_memcpy(res.ssid, ssid, ssid_len);
4520 } else {
4521 bss = wpa_bss_get_bssid_latest(wpa_s,
4522 wpa_s->pending_join_iface_addr);
4523 if (bss) {
4524 res.freq = bss->freq;
4525 res.ssid_len = bss->ssid_len;
4526 os_memcpy(res.ssid, bss->ssid, bss->ssid_len);
4527 wpa_printf(MSG_DEBUG, "P2P: Join target GO operating frequency from BSS table: %d MHz (SSID %s)",
4528 bss->freq,
4529 wpa_ssid_txt(bss->ssid, bss->ssid_len));
4530 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07004531 }
4532
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08004533 if (wpa_s->off_channel_freq || wpa_s->roc_waiting_drv_freq) {
4534 wpa_printf(MSG_DEBUG, "P2P: Cancel remain-on-channel prior to "
4535 "starting client");
4536 wpa_drv_cancel_remain_on_channel(wpa_s);
4537 wpa_s->off_channel_freq = 0;
4538 wpa_s->roc_waiting_drv_freq = 0;
4539 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004540 wpas_start_wps_enrollee(group, &res);
4541
4542 /*
4543 * Allow a longer timeout for join-a-running-group than normal 15
4544 * second group formation timeout since the GO may not have authorized
4545 * our connection yet.
4546 */
4547 eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s, NULL);
4548 eloop_register_timeout(60, 0, wpas_p2p_group_formation_timeout,
4549 wpa_s, NULL);
4550
4551 return 0;
4552}
4553
4554
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08004555static int wpas_p2p_setup_freqs(struct wpa_supplicant *wpa_s, int freq,
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004556 int *force_freq, int *pref_freq, int go)
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08004557{
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07004558 int *freqs, res;
4559 unsigned int freq_in_use = 0, num, i;
4560
4561 freqs = os_calloc(wpa_s->num_multichan_concurrent, sizeof(int));
4562 if (!freqs)
4563 return -1;
4564
4565 num = get_shared_radio_freqs(wpa_s, freqs,
4566 wpa_s->num_multichan_concurrent);
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07004567 wpa_printf(MSG_DEBUG,
4568 "P2P: Setup freqs: freq=%d num_MCC=%d shared_freqs=%u",
4569 freq, wpa_s->num_multichan_concurrent, num);
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07004570
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08004571 if (freq > 0) {
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004572 int ret;
4573 if (go)
4574 ret = p2p_supported_freq(wpa_s->global->p2p, freq);
4575 else
4576 ret = p2p_supported_freq_cli(wpa_s->global->p2p, freq);
4577 if (!ret) {
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08004578 wpa_printf(MSG_DEBUG, "P2P: The forced channel "
4579 "(%u MHz) is not supported for P2P uses",
4580 freq);
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07004581 res = -3;
4582 goto exit_free;
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08004583 }
4584
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07004585 for (i = 0; i < num; i++) {
4586 if (freqs[i] == freq)
4587 freq_in_use = 1;
4588 }
4589
4590 if (num == wpa_s->num_multichan_concurrent && !freq_in_use) {
4591 wpa_printf(MSG_DEBUG, "P2P: Cannot start P2P group on %u MHz as there are no available channels",
4592 freq);
4593 res = -2;
4594 goto exit_free;
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08004595 }
4596 wpa_printf(MSG_DEBUG, "P2P: Trying to force us to use the "
4597 "requested channel (%u MHz)", freq);
4598 *force_freq = freq;
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07004599 goto exit_ok;
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08004600 }
4601
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07004602 for (i = 0; i < num; i++) {
4603 if (!p2p_supported_freq(wpa_s->global->p2p, freqs[i]))
4604 continue;
4605
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07004606 if (*pref_freq == 0 && num < wpa_s->num_multichan_concurrent) {
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08004607 wpa_printf(MSG_DEBUG, "P2P: Try to prefer a frequency (%u MHz) we are already using",
4608 freqs[i]);
Dmitry Shmidt51a47d52013-09-10 10:52:57 -07004609 *pref_freq = freqs[i];
Dmitry Shmidt51a47d52013-09-10 10:52:57 -07004610 } else {
4611 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 -08004612 freqs[i]);
Dmitry Shmidt51a47d52013-09-10 10:52:57 -07004613 *force_freq = freqs[i];
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07004614 }
4615 break;
4616 }
4617
4618 if (i == num) {
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08004619 if (num < wpa_s->num_multichan_concurrent && num > 0) {
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07004620 wpa_printf(MSG_DEBUG, "P2P: Current operating channels are not available for P2P. Try to use another channel");
4621 *force_freq = 0;
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08004622 } else if (num < wpa_s->num_multichan_concurrent) {
4623 wpa_printf(MSG_DEBUG, "P2P: No current operating channels - try to use a new channel");
4624 *force_freq = 0;
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07004625 } else {
4626 wpa_printf(MSG_DEBUG, "P2P: All channels are in use and none of them are P2P enabled. Cannot start P2P group");
4627 res = -2;
4628 goto exit_free;
4629 }
4630 }
4631
4632exit_ok:
4633 res = 0;
4634exit_free:
4635 os_free(freqs);
4636 return res;
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08004637}
4638
4639
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004640/**
4641 * wpas_p2p_connect - Request P2P Group Formation to be started
4642 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
4643 * @peer_addr: Address of the peer P2P Device
4644 * @pin: PIN to use during provisioning or %NULL to indicate PBC mode
4645 * @persistent_group: Whether to create a persistent group
Dmitry Shmidt04949592012-07-19 12:16:46 -07004646 * @auto_join: Whether to select join vs. GO Negotiation automatically
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004647 * @join: Whether to join an existing group (as a client) instead of starting
4648 * Group Owner negotiation; @peer_addr is BSSID in that case
4649 * @auth: Whether to only authorize the connection instead of doing that and
4650 * initiating Group Owner negotiation
4651 * @go_intent: GO Intent or -1 to use default
4652 * @freq: Frequency for the group or 0 for auto-selection
Dmitry Shmidt04949592012-07-19 12:16:46 -07004653 * @persistent_id: Persistent group credentials to use for forcing GO
4654 * parameters or -1 to generate new values (SSID/passphrase)
4655 * @pd: Whether to send Provision Discovery prior to GO Negotiation as an
4656 * interoperability workaround when initiating group formation
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004657 * @ht40: Start GO with 40 MHz channel width
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004658 * @vht: Start GO with VHT support
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004659 * Returns: 0 or new PIN (if pin was %NULL) on success, -1 on unspecified
4660 * failure, -2 on failure due to channel not currently available,
4661 * -3 if forced channel is not supported
4662 */
4663int wpas_p2p_connect(struct wpa_supplicant *wpa_s, const u8 *peer_addr,
4664 const char *pin, enum p2p_wps_method wps_method,
Dmitry Shmidt04949592012-07-19 12:16:46 -07004665 int persistent_group, int auto_join, int join, int auth,
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004666 int go_intent, int freq, int persistent_id, int pd,
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004667 int ht40, int vht)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004668{
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07004669 int force_freq = 0, pref_freq = 0;
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08004670 int ret = 0, res;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004671 enum wpa_driver_if_type iftype;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004672 const u8 *if_addr;
Dmitry Shmidt04949592012-07-19 12:16:46 -07004673 struct wpa_ssid *ssid = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004674
4675 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
4676 return -1;
4677
Dmitry Shmidt04949592012-07-19 12:16:46 -07004678 if (persistent_id >= 0) {
4679 ssid = wpa_config_get_network(wpa_s->conf, persistent_id);
4680 if (ssid == NULL || ssid->disabled != 2 ||
4681 ssid->mode != WPAS_MODE_P2P_GO)
4682 return -1;
4683 }
4684
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07004685 os_free(wpa_s->global->add_psk);
4686 wpa_s->global->add_psk = NULL;
4687
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08004688 wpa_s->global->p2p_fail_on_wps_complete = 0;
4689
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004690 if (go_intent < 0)
4691 go_intent = wpa_s->conf->p2p_go_intent;
4692
4693 if (!auth)
4694 wpa_s->p2p_long_listen = 0;
4695
4696 wpa_s->p2p_wps_method = wps_method;
Dmitry Shmidt04949592012-07-19 12:16:46 -07004697 wpa_s->p2p_persistent_group = !!persistent_group;
4698 wpa_s->p2p_persistent_id = persistent_id;
4699 wpa_s->p2p_go_intent = go_intent;
4700 wpa_s->p2p_connect_freq = freq;
4701 wpa_s->p2p_fallback_to_go_neg = 0;
4702 wpa_s->p2p_pd_before_go_neg = !!pd;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004703 wpa_s->p2p_go_ht40 = !!ht40;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004704 wpa_s->p2p_go_vht = !!vht;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004705
4706 if (pin)
4707 os_strlcpy(wpa_s->p2p_pin, pin, sizeof(wpa_s->p2p_pin));
4708 else if (wps_method == WPS_PIN_DISPLAY) {
4709 ret = wps_generate_pin();
4710 os_snprintf(wpa_s->p2p_pin, sizeof(wpa_s->p2p_pin), "%08d",
4711 ret);
4712 wpa_printf(MSG_DEBUG, "P2P: Randomly generated PIN: %s",
4713 wpa_s->p2p_pin);
4714 } else
4715 wpa_s->p2p_pin[0] = '\0';
4716
Dmitry Shmidt04949592012-07-19 12:16:46 -07004717 if (join || auto_join) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004718 u8 iface_addr[ETH_ALEN], dev_addr[ETH_ALEN];
4719 if (auth) {
4720 wpa_printf(MSG_DEBUG, "P2P: Authorize invitation to "
4721 "connect a running group from " MACSTR,
4722 MAC2STR(peer_addr));
4723 os_memcpy(wpa_s->p2p_auth_invite, peer_addr, ETH_ALEN);
4724 return ret;
4725 }
4726 os_memcpy(dev_addr, peer_addr, ETH_ALEN);
4727 if (p2p_get_interface_addr(wpa_s->global->p2p, peer_addr,
4728 iface_addr) < 0) {
4729 os_memcpy(iface_addr, peer_addr, ETH_ALEN);
4730 p2p_get_dev_addr(wpa_s->global->p2p, peer_addr,
4731 dev_addr);
4732 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07004733 if (auto_join) {
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08004734 os_get_reltime(&wpa_s->p2p_auto_started);
Dmitry Shmidt04949592012-07-19 12:16:46 -07004735 wpa_printf(MSG_DEBUG, "P2P: Auto join started at "
4736 "%ld.%06ld",
4737 wpa_s->p2p_auto_started.sec,
4738 wpa_s->p2p_auto_started.usec);
4739 }
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08004740 wpa_s->user_initiated_pd = 1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07004741 if (wpas_p2p_join(wpa_s, iface_addr, dev_addr, wps_method,
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08004742 auto_join, freq, NULL, 0) < 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004743 return -1;
4744 return ret;
4745 }
4746
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004747 res = wpas_p2p_setup_freqs(wpa_s, freq, &force_freq, &pref_freq,
4748 go_intent == 15);
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08004749 if (res)
4750 return res;
Dmitry Shmidta0d265f2013-11-19 13:13:41 -08004751 wpas_p2p_set_own_freq_preference(wpa_s,
4752 force_freq ? force_freq : pref_freq);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004753
4754 wpa_s->create_p2p_iface = wpas_p2p_create_iface(wpa_s);
4755
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004756 if (wpa_s->create_p2p_iface) {
4757 /* Prepare to add a new interface for the group */
4758 iftype = WPA_IF_P2P_GROUP;
4759 if (go_intent == 15)
4760 iftype = WPA_IF_P2P_GO;
4761 if (wpas_p2p_add_group_interface(wpa_s, iftype) < 0) {
4762 wpa_printf(MSG_ERROR, "P2P: Failed to allocate a new "
4763 "interface for the group");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004764 return -1;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004765 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004766
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004767 if_addr = wpa_s->pending_interface_addr;
4768 } else
4769 if_addr = wpa_s->own_addr;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004770
4771 if (auth) {
4772 if (wpas_p2p_auth_go_neg(wpa_s, peer_addr, wps_method,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004773 go_intent, if_addr,
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08004774 force_freq, persistent_group, ssid,
4775 pref_freq) < 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004776 return -1;
4777 return ret;
4778 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004779
4780 if (wpas_p2p_start_go_neg(wpa_s, peer_addr, wps_method,
4781 go_intent, if_addr, force_freq,
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08004782 persistent_group, ssid, pref_freq) < 0) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004783 if (wpa_s->create_p2p_iface)
4784 wpas_p2p_remove_pending_group_interface(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004785 return -1;
4786 }
4787 return ret;
4788}
4789
4790
4791/**
4792 * wpas_p2p_remain_on_channel_cb - Indication of remain-on-channel start
4793 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
4794 * @freq: Frequency of the channel in MHz
4795 * @duration: Duration of the stay on the channel in milliseconds
4796 *
4797 * This callback is called when the driver indicates that it has started the
4798 * requested remain-on-channel duration.
4799 */
4800void wpas_p2p_remain_on_channel_cb(struct wpa_supplicant *wpa_s,
4801 unsigned int freq, unsigned int duration)
4802{
4803 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
4804 return;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004805 if (wpa_s->off_channel_freq == wpa_s->pending_listen_freq) {
4806 p2p_listen_cb(wpa_s->global->p2p, wpa_s->pending_listen_freq,
4807 wpa_s->pending_listen_duration);
4808 wpa_s->pending_listen_freq = 0;
Dmitry Shmidt7f93d6f2014-02-21 11:22:49 -08004809 } else {
4810 wpa_printf(MSG_DEBUG, "P2P: Ignore remain-on-channel callback (off_channel_freq=%u pending_listen_freq=%d freq=%u duration=%u)",
4811 wpa_s->off_channel_freq, wpa_s->pending_listen_freq,
4812 freq, duration);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004813 }
4814}
4815
4816
4817static int wpas_p2p_listen_start(struct wpa_supplicant *wpa_s,
4818 unsigned int timeout)
4819{
4820 /* Limit maximum Listen state time based on driver limitation. */
4821 if (timeout > wpa_s->max_remain_on_chan)
4822 timeout = wpa_s->max_remain_on_chan;
4823
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004824 return p2p_listen(wpa_s->global->p2p, timeout);
4825}
4826
4827
4828/**
4829 * wpas_p2p_cancel_remain_on_channel_cb - Remain-on-channel timeout
4830 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
4831 * @freq: Frequency of the channel in MHz
4832 *
4833 * This callback is called when the driver indicates that a remain-on-channel
4834 * operation has been completed, i.e., the duration on the requested channel
4835 * has timed out.
4836 */
4837void wpas_p2p_cancel_remain_on_channel_cb(struct wpa_supplicant *wpa_s,
4838 unsigned int freq)
4839{
4840 wpa_printf(MSG_DEBUG, "P2P: Cancel remain-on-channel callback "
4841 "(p2p_long_listen=%d ms pending_action_tx=%p)",
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08004842 wpa_s->p2p_long_listen, offchannel_pending_action_tx(wpa_s));
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08004843 wpas_p2p_listen_work_done(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004844 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
4845 return;
4846 if (p2p_listen_end(wpa_s->global->p2p, freq) > 0)
4847 return; /* P2P module started a new operation */
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08004848 if (offchannel_pending_action_tx(wpa_s))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004849 return;
4850 if (wpa_s->p2p_long_listen > 0)
4851 wpa_s->p2p_long_listen -= wpa_s->max_remain_on_chan;
4852 if (wpa_s->p2p_long_listen > 0) {
4853 wpa_printf(MSG_DEBUG, "P2P: Continuing long Listen state");
4854 wpas_p2p_listen_start(wpa_s, wpa_s->p2p_long_listen);
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07004855 } else {
4856 /*
4857 * When listen duration is over, stop listen & update p2p_state
4858 * to IDLE.
4859 */
4860 p2p_stop_listen(wpa_s->global->p2p);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004861 }
4862}
4863
4864
4865/**
4866 * wpas_p2p_group_remove - Remove a P2P group
4867 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
4868 * @ifname: Network interface name of the group interface or "*" to remove all
4869 * groups
4870 * Returns: 0 on success, -1 on failure
4871 *
4872 * This function is used to remove a P2P group. This can be used to disconnect
4873 * from a group in which the local end is a P2P Client or to end a P2P Group in
4874 * case the local end is the Group Owner. If a virtual network interface was
4875 * created for this group, that interface will be removed. Otherwise, only the
4876 * configured P2P group network will be removed from the interface.
4877 */
4878int wpas_p2p_group_remove(struct wpa_supplicant *wpa_s, const char *ifname)
4879{
4880 struct wpa_global *global = wpa_s->global;
4881
4882 if (os_strcmp(ifname, "*") == 0) {
4883 struct wpa_supplicant *prev;
4884 wpa_s = global->ifaces;
4885 while (wpa_s) {
4886 prev = wpa_s;
4887 wpa_s = wpa_s->next;
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07004888 if (prev->p2p_group_interface !=
4889 NOT_P2P_GROUP_INTERFACE ||
4890 (prev->current_ssid &&
4891 prev->current_ssid->p2p_group))
4892 wpas_p2p_disconnect(prev);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004893 }
4894 return 0;
4895 }
4896
4897 for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
4898 if (os_strcmp(wpa_s->ifname, ifname) == 0)
4899 break;
4900 }
4901
4902 return wpas_p2p_disconnect(wpa_s);
4903}
4904
4905
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07004906static int wpas_p2p_select_go_freq(struct wpa_supplicant *wpa_s, int freq)
4907{
4908 unsigned int r;
4909
4910 if (freq == 2) {
4911 wpa_printf(MSG_DEBUG, "P2P: Request to start GO on 2.4 GHz "
4912 "band");
4913 if (wpa_s->best_24_freq > 0 &&
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004914 p2p_supported_freq_go(wpa_s->global->p2p,
4915 wpa_s->best_24_freq)) {
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07004916 freq = wpa_s->best_24_freq;
4917 wpa_printf(MSG_DEBUG, "P2P: Use best 2.4 GHz band "
4918 "channel: %d MHz", freq);
4919 } else {
4920 os_get_random((u8 *) &r, sizeof(r));
4921 freq = 2412 + (r % 3) * 25;
4922 wpa_printf(MSG_DEBUG, "P2P: Use random 2.4 GHz band "
4923 "channel: %d MHz", freq);
4924 }
4925 }
4926
4927 if (freq == 5) {
4928 wpa_printf(MSG_DEBUG, "P2P: Request to start GO on 5 GHz "
4929 "band");
4930 if (wpa_s->best_5_freq > 0 &&
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004931 p2p_supported_freq_go(wpa_s->global->p2p,
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07004932 wpa_s->best_5_freq)) {
4933 freq = wpa_s->best_5_freq;
4934 wpa_printf(MSG_DEBUG, "P2P: Use best 5 GHz band "
4935 "channel: %d MHz", freq);
4936 } else {
4937 os_get_random((u8 *) &r, sizeof(r));
4938 freq = 5180 + (r % 4) * 20;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004939 if (!p2p_supported_freq_go(wpa_s->global->p2p, freq)) {
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07004940 wpa_printf(MSG_DEBUG, "P2P: Could not select "
4941 "5 GHz channel for P2P group");
4942 return -1;
4943 }
4944 wpa_printf(MSG_DEBUG, "P2P: Use random 5 GHz band "
4945 "channel: %d MHz", freq);
4946 }
4947 }
4948
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004949 if (freq > 0 && !p2p_supported_freq_go(wpa_s->global->p2p, freq)) {
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07004950 wpa_printf(MSG_DEBUG, "P2P: The forced channel for GO "
4951 "(%u MHz) is not supported for P2P uses",
4952 freq);
4953 return -1;
4954 }
4955
4956 return freq;
4957}
4958
4959
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004960static int wpas_p2p_init_go_params(struct wpa_supplicant *wpa_s,
4961 struct p2p_go_neg_results *params,
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004962 int freq, int ht40, int vht,
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08004963 const struct p2p_channels *channels)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004964{
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07004965 int res, *freqs;
Dmitry Shmidt44c95782013-05-17 09:51:35 -07004966 unsigned int pref_freq;
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07004967 unsigned int num, i;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004968
4969 os_memset(params, 0, sizeof(*params));
4970 params->role_go = 1;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004971 params->ht40 = ht40;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004972 params->vht = vht;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004973 if (freq) {
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08004974 if (!freq_included(channels, freq)) {
4975 wpa_printf(MSG_DEBUG, "P2P: Forced GO freq %d MHz not "
4976 "accepted", freq);
4977 return -1;
4978 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004979 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on forced "
4980 "frequency %d MHz", freq);
4981 params->freq = freq;
4982 } else if (wpa_s->conf->p2p_oper_reg_class == 81 &&
4983 wpa_s->conf->p2p_oper_channel >= 1 &&
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08004984 wpa_s->conf->p2p_oper_channel <= 11 &&
4985 freq_included(channels,
4986 2407 + 5 * wpa_s->conf->p2p_oper_channel)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004987 params->freq = 2407 + 5 * wpa_s->conf->p2p_oper_channel;
4988 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on configured "
4989 "frequency %d MHz", params->freq);
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08004990 } else if ((wpa_s->conf->p2p_oper_reg_class == 115 ||
4991 wpa_s->conf->p2p_oper_reg_class == 116 ||
4992 wpa_s->conf->p2p_oper_reg_class == 117 ||
4993 wpa_s->conf->p2p_oper_reg_class == 124 ||
4994 wpa_s->conf->p2p_oper_reg_class == 126 ||
4995 wpa_s->conf->p2p_oper_reg_class == 127) &&
4996 freq_included(channels,
4997 5000 + 5 * wpa_s->conf->p2p_oper_channel)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004998 params->freq = 5000 + 5 * wpa_s->conf->p2p_oper_channel;
4999 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on configured "
5000 "frequency %d MHz", params->freq);
5001 } else if (wpa_s->conf->p2p_oper_channel == 0 &&
5002 wpa_s->best_overall_freq > 0 &&
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07005003 p2p_supported_freq_go(wpa_s->global->p2p,
5004 wpa_s->best_overall_freq) &&
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08005005 freq_included(channels, wpa_s->best_overall_freq)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005006 params->freq = wpa_s->best_overall_freq;
5007 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on best overall "
5008 "channel %d MHz", params->freq);
5009 } else if (wpa_s->conf->p2p_oper_channel == 0 &&
5010 wpa_s->best_24_freq > 0 &&
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07005011 p2p_supported_freq_go(wpa_s->global->p2p,
5012 wpa_s->best_24_freq) &&
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08005013 freq_included(channels, wpa_s->best_24_freq)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005014 params->freq = wpa_s->best_24_freq;
5015 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on best 2.4 GHz "
5016 "channel %d MHz", params->freq);
5017 } else if (wpa_s->conf->p2p_oper_channel == 0 &&
5018 wpa_s->best_5_freq > 0 &&
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07005019 p2p_supported_freq_go(wpa_s->global->p2p,
5020 wpa_s->best_5_freq) &&
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08005021 freq_included(channels, wpa_s->best_5_freq)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005022 params->freq = wpa_s->best_5_freq;
5023 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on best 5 GHz "
5024 "channel %d MHz", params->freq);
Dmitry Shmidt44c95782013-05-17 09:51:35 -07005025 } else if ((pref_freq = p2p_get_pref_freq(wpa_s->global->p2p,
5026 channels))) {
5027 params->freq = pref_freq;
5028 wpa_printf(MSG_DEBUG, "P2P: Set GO freq %d MHz from preferred "
5029 "channels", params->freq);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005030 } else {
Dmitry Shmidt04949592012-07-19 12:16:46 -07005031 int chan;
5032 for (chan = 0; chan < 11; chan++) {
5033 params->freq = 2412 + chan * 5;
5034 if (!wpas_p2p_disallowed_freq(wpa_s->global,
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08005035 params->freq) &&
5036 freq_included(channels, params->freq))
Dmitry Shmidt04949592012-07-19 12:16:46 -07005037 break;
5038 }
5039 if (chan == 11) {
5040 wpa_printf(MSG_DEBUG, "P2P: No 2.4 GHz channel "
5041 "allowed");
5042 return -1;
5043 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005044 wpa_printf(MSG_DEBUG, "P2P: Set GO freq %d MHz (no preference "
5045 "known)", params->freq);
5046 }
5047
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07005048 freqs = os_calloc(wpa_s->num_multichan_concurrent, sizeof(int));
5049 if (!freqs)
5050 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005051
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07005052 res = wpas_p2p_valid_oper_freqs(wpa_s, freqs,
5053 wpa_s->num_multichan_concurrent);
5054 if (res < 0) {
5055 os_free(freqs);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005056 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005057 }
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07005058 num = res;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005059
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08005060 for (i = 0; i < num; i++) {
5061 if (freq && freqs[i] == freq)
5062 break;
5063 if (!freq && freq_included(channels, freqs[i])) {
5064 wpa_printf(MSG_DEBUG, "P2P: Force GO on a channel we are already using (%u MHz)",
5065 freqs[i]);
5066 params->freq = freqs[i];
5067 break;
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07005068 }
5069 }
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08005070
5071 if (i == num) {
5072 if (wpas_p2p_num_unused_channels(wpa_s) <= 0) {
5073 if (freq)
5074 wpa_printf(MSG_DEBUG, "P2P: Cannot force GO on freq (%u MHz) as all the channels are in use", freq);
5075 else
5076 wpa_printf(MSG_DEBUG, "P2P: Cannot force GO on any of the channels we are already using");
5077 os_free(freqs);
5078 return -1;
5079 } else if (num == 0) {
5080 wpa_printf(MSG_DEBUG, "P2P: Use one of the free channels");
5081 } else {
5082 wpa_printf(MSG_DEBUG, "P2P: Cannot force GO on any of the channels we are already using. Use one of the free channels");
5083 }
5084 }
5085
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07005086 os_free(freqs);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005087 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005088}
5089
5090
5091static struct wpa_supplicant *
5092wpas_p2p_get_group_iface(struct wpa_supplicant *wpa_s, int addr_allocated,
5093 int go)
5094{
5095 struct wpa_supplicant *group_wpa_s;
5096
Dmitry Shmidtaa532512012-09-24 10:35:31 -07005097 if (!wpas_p2p_create_iface(wpa_s)) {
5098 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Use same interface for group "
5099 "operations");
Dmitry Shmidt56052862013-10-04 10:23:25 -07005100 wpa_s->p2p_first_connection_timeout = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005101 return wpa_s;
Dmitry Shmidtaa532512012-09-24 10:35:31 -07005102 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005103
5104 if (wpas_p2p_add_group_interface(wpa_s, go ? WPA_IF_P2P_GO :
Dmitry Shmidtaa532512012-09-24 10:35:31 -07005105 WPA_IF_P2P_CLIENT) < 0) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07005106 wpa_msg_global(wpa_s, MSG_ERROR,
5107 "P2P: Failed to add group interface");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005108 return NULL;
Dmitry Shmidtaa532512012-09-24 10:35:31 -07005109 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005110 group_wpa_s = wpas_p2p_init_group_interface(wpa_s, go);
5111 if (group_wpa_s == NULL) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07005112 wpa_msg_global(wpa_s, MSG_ERROR,
5113 "P2P: Failed to initialize group interface");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005114 wpas_p2p_remove_pending_group_interface(wpa_s);
5115 return NULL;
5116 }
5117
Dmitry Shmidtaa532512012-09-24 10:35:31 -07005118 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Use separate group interface %s",
5119 group_wpa_s->ifname);
Dmitry Shmidt56052862013-10-04 10:23:25 -07005120 group_wpa_s->p2p_first_connection_timeout = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005121 return group_wpa_s;
5122}
5123
5124
5125/**
5126 * wpas_p2p_group_add - Add a new P2P group with local end as Group Owner
5127 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
5128 * @persistent_group: Whether to create a persistent group
5129 * @freq: Frequency for the group or 0 to indicate no hardcoding
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07005130 * @ht40: Start GO with 40 MHz channel width
5131 * @vht: Start GO with VHT support
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005132 * Returns: 0 on success, -1 on failure
5133 *
5134 * This function creates a new P2P group with the local end as the Group Owner,
5135 * i.e., without using Group Owner Negotiation.
5136 */
5137int wpas_p2p_group_add(struct wpa_supplicant *wpa_s, int persistent_group,
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07005138 int freq, int ht40, int vht)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005139{
5140 struct p2p_go_neg_results params;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005141
5142 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
5143 return -1;
5144
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07005145 os_free(wpa_s->global->add_psk);
5146 wpa_s->global->add_psk = NULL;
5147
Dmitry Shmidtdca39792011-09-06 11:17:33 -07005148 /* Make sure we are not running find during connection establishment */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005149 wpa_printf(MSG_DEBUG, "P2P: Stop any on-going P2P FIND");
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08005150 wpas_p2p_stop_find_oper(wpa_s);
Dmitry Shmidtdca39792011-09-06 11:17:33 -07005151
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07005152 freq = wpas_p2p_select_go_freq(wpa_s, freq);
5153 if (freq < 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005154 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005155
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07005156 if (wpas_p2p_init_go_params(wpa_s, &params, freq, ht40, vht, NULL))
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005157 return -1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07005158 if (params.freq &&
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07005159 !p2p_supported_freq_go(wpa_s->global->p2p, params.freq)) {
Dmitry Shmidt04949592012-07-19 12:16:46 -07005160 wpa_printf(MSG_DEBUG, "P2P: The selected channel for GO "
5161 "(%u MHz) is not supported for P2P uses",
5162 params.freq);
5163 return -1;
5164 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005165 p2p_go_params(wpa_s->global->p2p, &params);
5166 params.persistent_group = persistent_group;
5167
5168 wpa_s = wpas_p2p_get_group_iface(wpa_s, 0, 1);
5169 if (wpa_s == NULL)
5170 return -1;
5171 wpas_start_wps_go(wpa_s, &params, 0);
5172
5173 return 0;
5174}
5175
5176
5177static int wpas_start_p2p_client(struct wpa_supplicant *wpa_s,
Dmitry Shmidt15907092014-03-25 10:42:57 -07005178 struct wpa_ssid *params, int addr_allocated,
5179 int freq)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005180{
5181 struct wpa_ssid *ssid;
5182
5183 wpa_s = wpas_p2p_get_group_iface(wpa_s, addr_allocated, 0);
5184 if (wpa_s == NULL)
5185 return -1;
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07005186 wpa_s->p2p_last_4way_hs_fail = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005187
5188 wpa_supplicant_ap_deinit(wpa_s);
5189
5190 ssid = wpa_config_add_network(wpa_s->conf);
5191 if (ssid == NULL)
5192 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005193 wpa_config_set_network_defaults(ssid);
5194 ssid->temporary = 1;
5195 ssid->proto = WPA_PROTO_RSN;
5196 ssid->pairwise_cipher = WPA_CIPHER_CCMP;
5197 ssid->group_cipher = WPA_CIPHER_CCMP;
5198 ssid->key_mgmt = WPA_KEY_MGMT_PSK;
5199 ssid->ssid = os_malloc(params->ssid_len);
5200 if (ssid->ssid == NULL) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005201 wpa_config_remove_network(wpa_s->conf, ssid->id);
5202 return -1;
5203 }
5204 os_memcpy(ssid->ssid, params->ssid, params->ssid_len);
5205 ssid->ssid_len = params->ssid_len;
5206 ssid->p2p_group = 1;
5207 ssid->export_keys = 1;
5208 if (params->psk_set) {
5209 os_memcpy(ssid->psk, params->psk, 32);
5210 ssid->psk_set = 1;
5211 }
5212 if (params->passphrase)
5213 ssid->passphrase = os_strdup(params->passphrase);
5214
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005215 wpa_s->show_group_started = 1;
Dmitry Shmidt15907092014-03-25 10:42:57 -07005216 wpa_s->p2p_in_invitation = 1;
5217 wpa_s->p2p_invite_go_freq = freq;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005218
Dmitry Shmidt15907092014-03-25 10:42:57 -07005219 eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s->parent,
5220 NULL);
5221 eloop_register_timeout(P2P_MAX_INITIAL_CONN_WAIT, 0,
5222 wpas_p2p_group_formation_timeout,
5223 wpa_s->parent, NULL);
Dmitry Shmidtfa3fc4a2013-11-21 13:34:38 -08005224 wpa_supplicant_select_network(wpa_s, ssid);
5225
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005226 return 0;
5227}
5228
5229
5230int wpas_p2p_group_add_persistent(struct wpa_supplicant *wpa_s,
5231 struct wpa_ssid *ssid, int addr_allocated,
Dmitry Shmidt96be6222014-02-13 10:16:51 -08005232 int force_freq, int neg_freq, int ht40,
5233 int vht, const struct p2p_channels *channels,
Dmitry Shmidt56052862013-10-04 10:23:25 -07005234 int connection_timeout)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005235{
5236 struct p2p_go_neg_results params;
Dmitry Shmidt96be6222014-02-13 10:16:51 -08005237 int go = 0, freq;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005238
5239 if (ssid->disabled != 2 || ssid->ssid == NULL)
5240 return -1;
5241
5242 if (wpas_get_p2p_group(wpa_s, ssid->ssid, ssid->ssid_len, &go) &&
5243 go == (ssid->mode == WPAS_MODE_P2P_GO)) {
5244 wpa_printf(MSG_DEBUG, "P2P: Requested persistent group is "
5245 "already running");
5246 return 0;
5247 }
5248
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07005249 os_free(wpa_s->global->add_psk);
5250 wpa_s->global->add_psk = NULL;
5251
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005252 /* Make sure we are not running find during connection establishment */
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08005253 wpas_p2p_stop_find_oper(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005254
Dmitry Shmidt04949592012-07-19 12:16:46 -07005255 wpa_s->p2p_fallback_to_go_neg = 0;
5256
Dmitry Shmidt96be6222014-02-13 10:16:51 -08005257 if (force_freq > 0) {
5258 freq = wpas_p2p_select_go_freq(wpa_s, force_freq);
5259 if (freq < 0)
5260 return -1;
5261 } else {
5262 freq = wpas_p2p_select_go_freq(wpa_s, neg_freq);
5263 if (freq < 0 || (freq > 0 && !freq_included(channels, freq)))
5264 freq = 0;
5265 }
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07005266
Dmitry Shmidt15907092014-03-25 10:42:57 -07005267 if (ssid->mode == WPAS_MODE_INFRA)
5268 return wpas_start_p2p_client(wpa_s, ssid, addr_allocated, freq);
5269
5270 if (ssid->mode != WPAS_MODE_P2P_GO)
5271 return -1;
5272
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07005273 if (wpas_p2p_init_go_params(wpa_s, &params, freq, ht40, vht, channels))
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005274 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005275
5276 params.role_go = 1;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08005277 params.psk_set = ssid->psk_set;
5278 if (params.psk_set)
5279 os_memcpy(params.psk, ssid->psk, sizeof(params.psk));
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08005280 if (ssid->passphrase) {
5281 if (os_strlen(ssid->passphrase) >= sizeof(params.passphrase)) {
5282 wpa_printf(MSG_ERROR, "P2P: Invalid passphrase in "
5283 "persistent group");
5284 return -1;
5285 }
5286 os_strlcpy(params.passphrase, ssid->passphrase,
5287 sizeof(params.passphrase));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005288 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005289 os_memcpy(params.ssid, ssid->ssid, ssid->ssid_len);
5290 params.ssid_len = ssid->ssid_len;
5291 params.persistent_group = 1;
5292
5293 wpa_s = wpas_p2p_get_group_iface(wpa_s, addr_allocated, 1);
5294 if (wpa_s == NULL)
5295 return -1;
5296
Dmitry Shmidt56052862013-10-04 10:23:25 -07005297 wpa_s->p2p_first_connection_timeout = connection_timeout;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005298 wpas_start_wps_go(wpa_s, &params, 0);
5299
5300 return 0;
5301}
5302
5303
5304static void wpas_p2p_ie_update(void *ctx, struct wpabuf *beacon_ies,
5305 struct wpabuf *proberesp_ies)
5306{
5307 struct wpa_supplicant *wpa_s = ctx;
5308 if (wpa_s->ap_iface) {
5309 struct hostapd_data *hapd = wpa_s->ap_iface->bss[0];
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005310 if (!(hapd->conf->p2p & P2P_GROUP_OWNER)) {
5311 wpabuf_free(beacon_ies);
5312 wpabuf_free(proberesp_ies);
5313 return;
5314 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005315 if (beacon_ies) {
5316 wpabuf_free(hapd->p2p_beacon_ie);
5317 hapd->p2p_beacon_ie = beacon_ies;
5318 }
5319 wpabuf_free(hapd->p2p_probe_resp_ie);
5320 hapd->p2p_probe_resp_ie = proberesp_ies;
5321 } else {
5322 wpabuf_free(beacon_ies);
5323 wpabuf_free(proberesp_ies);
5324 }
5325 wpa_supplicant_ap_update_beacon(wpa_s);
5326}
5327
5328
5329static void wpas_p2p_idle_update(void *ctx, int idle)
5330{
5331 struct wpa_supplicant *wpa_s = ctx;
5332 if (!wpa_s->ap_iface)
5333 return;
5334 wpa_printf(MSG_DEBUG, "P2P: GO - group %sidle", idle ? "" : "not ");
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005335 if (idle) {
5336 if (wpa_s->global->p2p_fail_on_wps_complete &&
5337 wpa_s->p2p_in_provisioning) {
5338 wpas_p2p_grpform_fail_after_wps(wpa_s);
5339 return;
5340 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005341 wpas_p2p_set_group_idle_timeout(wpa_s);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005342 } else
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005343 eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL);
5344}
5345
5346
5347struct p2p_group * wpas_p2p_group_init(struct wpa_supplicant *wpa_s,
Dmitry Shmidt04949592012-07-19 12:16:46 -07005348 struct wpa_ssid *ssid)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005349{
5350 struct p2p_group *group;
5351 struct p2p_group_config *cfg;
5352
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005353 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
5354 return NULL;
5355
5356 cfg = os_zalloc(sizeof(*cfg));
5357 if (cfg == NULL)
5358 return NULL;
5359
Dmitry Shmidt04949592012-07-19 12:16:46 -07005360 if (ssid->p2p_persistent_group && wpa_s->conf->persistent_reconnect)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005361 cfg->persistent_group = 2;
Dmitry Shmidt04949592012-07-19 12:16:46 -07005362 else if (ssid->p2p_persistent_group)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005363 cfg->persistent_group = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005364 os_memcpy(cfg->interface_addr, wpa_s->own_addr, ETH_ALEN);
5365 if (wpa_s->max_stations &&
5366 wpa_s->max_stations < wpa_s->conf->max_num_sta)
5367 cfg->max_clients = wpa_s->max_stations;
5368 else
5369 cfg->max_clients = wpa_s->conf->max_num_sta;
Dmitry Shmidt04949592012-07-19 12:16:46 -07005370 os_memcpy(cfg->ssid, ssid->ssid, ssid->ssid_len);
5371 cfg->ssid_len = ssid->ssid_len;
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08005372 cfg->freq = ssid->frequency;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005373 cfg->cb_ctx = wpa_s;
5374 cfg->ie_update = wpas_p2p_ie_update;
5375 cfg->idle_update = wpas_p2p_idle_update;
5376
5377 group = p2p_group_init(wpa_s->global->p2p, cfg);
5378 if (group == NULL)
5379 os_free(cfg);
Dmitry Shmidt04949592012-07-19 12:16:46 -07005380 if (ssid->mode != WPAS_MODE_P2P_GROUP_FORMATION)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005381 p2p_group_notif_formation_done(group);
5382 wpa_s->p2p_group = group;
5383 return group;
5384}
5385
5386
5387void wpas_p2p_wps_success(struct wpa_supplicant *wpa_s, const u8 *peer_addr,
5388 int registrar)
5389{
Dmitry Shmidt04949592012-07-19 12:16:46 -07005390 struct wpa_ssid *ssid = wpa_s->current_ssid;
5391
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005392 if (!wpa_s->p2p_in_provisioning) {
5393 wpa_printf(MSG_DEBUG, "P2P: Ignore WPS success event - P2P "
5394 "provisioning not in progress");
5395 return;
5396 }
5397
Dmitry Shmidt04949592012-07-19 12:16:46 -07005398 if (ssid && ssid->mode == WPAS_MODE_INFRA) {
5399 u8 go_dev_addr[ETH_ALEN];
5400 os_memcpy(go_dev_addr, wpa_s->bssid, ETH_ALEN);
5401 wpas_p2p_persistent_group(wpa_s, go_dev_addr, ssid->ssid,
5402 ssid->ssid_len);
5403 /* Clear any stored provisioning info */
5404 p2p_clear_provisioning_info(wpa_s->global->p2p, go_dev_addr);
5405 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005406
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005407 eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s->parent,
5408 NULL);
Dmitry Shmidt56052862013-10-04 10:23:25 -07005409 wpa_s->p2p_go_group_formation_completed = 1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07005410 if (ssid && ssid->mode == WPAS_MODE_INFRA) {
5411 /*
5412 * Use a separate timeout for initial data connection to
5413 * complete to allow the group to be removed automatically if
5414 * something goes wrong in this step before the P2P group idle
5415 * timeout mechanism is taken into use.
5416 */
Dmitry Shmidt56052862013-10-04 10:23:25 -07005417 wpa_dbg(wpa_s, MSG_DEBUG,
5418 "P2P: Re-start group formation timeout (%d seconds) as client for initial connection",
5419 P2P_MAX_INITIAL_CONN_WAIT);
Dmitry Shmidt04949592012-07-19 12:16:46 -07005420 eloop_register_timeout(P2P_MAX_INITIAL_CONN_WAIT, 0,
5421 wpas_p2p_group_formation_timeout,
5422 wpa_s->parent, NULL);
Dmitry Shmidt56052862013-10-04 10:23:25 -07005423 } else if (ssid) {
5424 /*
5425 * Use a separate timeout for initial data connection to
5426 * complete to allow the group to be removed automatically if
5427 * the client does not complete data connection successfully.
5428 */
5429 wpa_dbg(wpa_s, MSG_DEBUG,
5430 "P2P: Re-start group formation timeout (%d seconds) as GO for initial connection",
5431 P2P_MAX_INITIAL_CONN_WAIT_GO);
5432 eloop_register_timeout(P2P_MAX_INITIAL_CONN_WAIT_GO, 0,
5433 wpas_p2p_group_formation_timeout,
5434 wpa_s->parent, NULL);
5435 /*
5436 * Complete group formation on first successful data connection
5437 */
5438 wpa_s->p2p_go_group_formation_completed = 0;
Dmitry Shmidt04949592012-07-19 12:16:46 -07005439 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005440 if (wpa_s->global->p2p)
5441 p2p_wps_success_cb(wpa_s->global->p2p, peer_addr);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005442 wpas_group_formation_completed(wpa_s, 1);
5443}
5444
5445
Jouni Malinen75ecf522011-06-27 15:19:46 -07005446void wpas_p2p_wps_failed(struct wpa_supplicant *wpa_s,
5447 struct wps_event_fail *fail)
5448{
5449 if (!wpa_s->p2p_in_provisioning) {
5450 wpa_printf(MSG_DEBUG, "P2P: Ignore WPS fail event - P2P "
5451 "provisioning not in progress");
5452 return;
5453 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005454
5455 if (wpa_s->go_params) {
5456 p2p_clear_provisioning_info(
5457 wpa_s->global->p2p,
Dmitry Shmidt04949592012-07-19 12:16:46 -07005458 wpa_s->go_params->peer_device_addr);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005459 }
5460
Jouni Malinen75ecf522011-06-27 15:19:46 -07005461 wpas_notify_p2p_wps_failed(wpa_s, fail);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005462
5463 if (wpa_s == wpa_s->global->p2p_group_formation) {
5464 /*
5465 * Allow some time for the failed WPS negotiation exchange to
5466 * complete, but remove the group since group formation cannot
5467 * succeed after provisioning failure.
5468 */
5469 wpa_printf(MSG_DEBUG, "P2P: WPS step failed during group formation - reject connection from timeout");
5470 wpa_s->global->p2p_fail_on_wps_complete = 1;
5471 eloop_deplete_timeout(0, 50000,
5472 wpas_p2p_group_formation_timeout,
5473 wpa_s->parent, NULL);
5474 }
5475}
5476
5477
5478int wpas_p2p_wps_eapol_cb(struct wpa_supplicant *wpa_s)
5479{
5480 if (!wpa_s->global->p2p_fail_on_wps_complete ||
5481 !wpa_s->p2p_in_provisioning)
5482 return 0;
5483
5484 wpas_p2p_grpform_fail_after_wps(wpa_s);
5485
5486 return 1;
Jouni Malinen75ecf522011-06-27 15:19:46 -07005487}
5488
5489
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005490int wpas_p2p_prov_disc(struct wpa_supplicant *wpa_s, const u8 *peer_addr,
Dmitry Shmidt04949592012-07-19 12:16:46 -07005491 const char *config_method,
5492 enum wpas_p2p_prov_disc_use use)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005493{
5494 u16 config_methods;
5495
Dmitry Shmidt04949592012-07-19 12:16:46 -07005496 wpa_s->p2p_fallback_to_go_neg = 0;
5497 wpa_s->pending_pd_use = NORMAL_PD;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005498 if (os_strncmp(config_method, "display", 7) == 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005499 config_methods = WPS_CONFIG_DISPLAY;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005500 else if (os_strncmp(config_method, "keypad", 6) == 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005501 config_methods = WPS_CONFIG_KEYPAD;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005502 else if (os_strncmp(config_method, "pbc", 3) == 0 ||
5503 os_strncmp(config_method, "pushbutton", 10) == 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005504 config_methods = WPS_CONFIG_PUSHBUTTON;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005505 else {
5506 wpa_printf(MSG_DEBUG, "P2P: Unknown config method");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005507 return -1;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005508 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005509
Dmitry Shmidt04949592012-07-19 12:16:46 -07005510 if (use == WPAS_P2P_PD_AUTO) {
5511 os_memcpy(wpa_s->pending_join_dev_addr, peer_addr, ETH_ALEN);
5512 wpa_s->pending_pd_config_methods = config_methods;
5513 wpa_s->p2p_auto_pd = 1;
5514 wpa_s->p2p_auto_join = 0;
5515 wpa_s->pending_pd_before_join = 0;
5516 wpa_s->auto_pd_scan_retry = 0;
5517 wpas_p2p_stop_find(wpa_s);
5518 wpa_s->p2p_join_scan_count = 0;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005519 os_get_reltime(&wpa_s->p2p_auto_started);
Dmitry Shmidt04949592012-07-19 12:16:46 -07005520 wpa_printf(MSG_DEBUG, "P2P: Auto PD started at %ld.%06ld",
5521 wpa_s->p2p_auto_started.sec,
5522 wpa_s->p2p_auto_started.usec);
5523 wpas_p2p_join_scan(wpa_s, NULL);
5524 return 0;
5525 }
5526
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005527 if (wpa_s->global->p2p == NULL || wpa_s->global->p2p_disabled)
5528 return -1;
5529
5530 return p2p_prov_disc_req(wpa_s->global->p2p, peer_addr,
Dmitry Shmidt04949592012-07-19 12:16:46 -07005531 config_methods, use == WPAS_P2P_PD_FOR_JOIN,
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08005532 0, 1);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005533}
5534
5535
5536int wpas_p2p_scan_result_text(const u8 *ies, size_t ies_len, char *buf,
5537 char *end)
5538{
5539 return p2p_scan_result_text(ies, ies_len, buf, end);
5540}
5541
5542
5543static void wpas_p2p_clear_pending_action_tx(struct wpa_supplicant *wpa_s)
5544{
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08005545 if (!offchannel_pending_action_tx(wpa_s))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005546 return;
5547
5548 wpa_printf(MSG_DEBUG, "P2P: Drop pending Action TX due to new "
5549 "operation request");
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08005550 offchannel_clear_pending_action_tx(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005551}
5552
5553
5554int wpas_p2p_find(struct wpa_supplicant *wpa_s, unsigned int timeout,
5555 enum p2p_discovery_type type,
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08005556 unsigned int num_req_dev_types, const u8 *req_dev_types,
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005557 const u8 *dev_id, unsigned int search_delay)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005558{
5559 wpas_p2p_clear_pending_action_tx(wpa_s);
5560 wpa_s->p2p_long_listen = 0;
5561
Dmitry Shmidt04949592012-07-19 12:16:46 -07005562 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL ||
5563 wpa_s->p2p_in_provisioning)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005564 return -1;
5565
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08005566 wpa_supplicant_cancel_sched_scan(wpa_s);
5567
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005568 return p2p_find(wpa_s->global->p2p, timeout, type,
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005569 num_req_dev_types, req_dev_types, dev_id,
5570 search_delay);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005571}
5572
5573
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08005574static int wpas_p2p_stop_find_oper(struct wpa_supplicant *wpa_s)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005575{
5576 wpas_p2p_clear_pending_action_tx(wpa_s);
5577 wpa_s->p2p_long_listen = 0;
5578 eloop_cancel_timeout(wpas_p2p_long_listen_timeout, wpa_s, NULL);
5579 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005580
5581 if (wpa_s->global->p2p)
5582 p2p_stop_find(wpa_s->global->p2p);
5583
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08005584 return 0;
5585}
5586
5587
5588void wpas_p2p_stop_find(struct wpa_supplicant *wpa_s)
5589{
5590 if (wpas_p2p_stop_find_oper(wpa_s) > 0)
5591 return;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005592 wpas_p2p_remove_pending_group_interface(wpa_s);
5593}
5594
5595
5596static void wpas_p2p_long_listen_timeout(void *eloop_ctx, void *timeout_ctx)
5597{
5598 struct wpa_supplicant *wpa_s = eloop_ctx;
5599 wpa_s->p2p_long_listen = 0;
5600}
5601
5602
5603int wpas_p2p_listen(struct wpa_supplicant *wpa_s, unsigned int timeout)
5604{
5605 int res;
5606
5607 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
5608 return -1;
5609
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08005610 wpa_supplicant_cancel_sched_scan(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005611 wpas_p2p_clear_pending_action_tx(wpa_s);
5612
5613 if (timeout == 0) {
5614 /*
5615 * This is a request for unlimited Listen state. However, at
5616 * least for now, this is mapped to a Listen state for one
5617 * hour.
5618 */
5619 timeout = 3600;
5620 }
5621 eloop_cancel_timeout(wpas_p2p_long_listen_timeout, wpa_s, NULL);
5622 wpa_s->p2p_long_listen = 0;
5623
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005624 /*
5625 * Stop previous find/listen operation to avoid trying to request a new
5626 * remain-on-channel operation while the driver is still running the
5627 * previous one.
5628 */
5629 if (wpa_s->global->p2p)
5630 p2p_stop_find(wpa_s->global->p2p);
5631
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005632 res = wpas_p2p_listen_start(wpa_s, timeout * 1000);
5633 if (res == 0 && timeout * 1000 > wpa_s->max_remain_on_chan) {
5634 wpa_s->p2p_long_listen = timeout * 1000;
5635 eloop_register_timeout(timeout, 0,
5636 wpas_p2p_long_listen_timeout,
5637 wpa_s, NULL);
5638 }
5639
5640 return res;
5641}
5642
5643
5644int wpas_p2p_assoc_req_ie(struct wpa_supplicant *wpa_s, struct wpa_bss *bss,
5645 u8 *buf, size_t len, int p2p_group)
5646{
5647 struct wpabuf *p2p_ie;
5648 int ret;
5649
5650 if (wpa_s->global->p2p_disabled)
5651 return -1;
5652 if (wpa_s->global->p2p == NULL)
5653 return -1;
5654 if (bss == NULL)
5655 return -1;
5656
5657 p2p_ie = wpa_bss_get_vendor_ie_multi(bss, P2P_IE_VENDOR_TYPE);
5658 ret = p2p_assoc_req_ie(wpa_s->global->p2p, bss->bssid, buf, len,
5659 p2p_group, p2p_ie);
5660 wpabuf_free(p2p_ie);
5661
5662 return ret;
5663}
5664
5665
5666int wpas_p2p_probe_req_rx(struct wpa_supplicant *wpa_s, const u8 *addr,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005667 const u8 *dst, const u8 *bssid,
Dmitry Shmidt04949592012-07-19 12:16:46 -07005668 const u8 *ie, size_t ie_len, int ssi_signal)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005669{
5670 if (wpa_s->global->p2p_disabled)
5671 return 0;
5672 if (wpa_s->global->p2p == NULL)
5673 return 0;
5674
Dmitry Shmidt04949592012-07-19 12:16:46 -07005675 switch (p2p_probe_req_rx(wpa_s->global->p2p, addr, dst, bssid,
5676 ie, ie_len)) {
5677 case P2P_PREQ_NOT_P2P:
5678 wpas_notify_preq(wpa_s, addr, dst, bssid, ie, ie_len,
5679 ssi_signal);
5680 /* fall through */
5681 case P2P_PREQ_MALFORMED:
5682 case P2P_PREQ_NOT_LISTEN:
5683 case P2P_PREQ_NOT_PROCESSED:
5684 default: /* make gcc happy */
5685 return 0;
5686 case P2P_PREQ_PROCESSED:
5687 return 1;
5688 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005689}
5690
5691
5692void wpas_p2p_rx_action(struct wpa_supplicant *wpa_s, const u8 *da,
5693 const u8 *sa, const u8 *bssid,
5694 u8 category, const u8 *data, size_t len, int freq)
5695{
5696 if (wpa_s->global->p2p_disabled)
5697 return;
5698 if (wpa_s->global->p2p == NULL)
5699 return;
5700
5701 p2p_rx_action(wpa_s->global->p2p, da, sa, bssid, category, data, len,
5702 freq);
5703}
5704
5705
5706void wpas_p2p_scan_ie(struct wpa_supplicant *wpa_s, struct wpabuf *ies)
5707{
5708 if (wpa_s->global->p2p_disabled)
5709 return;
5710 if (wpa_s->global->p2p == NULL)
5711 return;
5712
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08005713 p2p_scan_ie(wpa_s->global->p2p, ies, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005714}
5715
5716
5717void wpas_p2p_group_deinit(struct wpa_supplicant *wpa_s)
5718{
5719 p2p_group_deinit(wpa_s->p2p_group);
5720 wpa_s->p2p_group = NULL;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005721
5722 wpa_s->ap_configured_cb = NULL;
5723 wpa_s->ap_configured_cb_ctx = NULL;
5724 wpa_s->ap_configured_cb_data = NULL;
5725 wpa_s->connect_without_scan = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005726}
5727
5728
5729int wpas_p2p_reject(struct wpa_supplicant *wpa_s, const u8 *addr)
5730{
5731 wpa_s->p2p_long_listen = 0;
5732
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005733 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
5734 return -1;
5735
5736 return p2p_reject(wpa_s->global->p2p, addr);
5737}
5738
5739
5740/* Invite to reinvoke a persistent group */
5741int wpas_p2p_invite(struct wpa_supplicant *wpa_s, const u8 *peer_addr,
Jouni Malinen31be0a42012-08-31 21:20:51 +03005742 struct wpa_ssid *ssid, const u8 *go_dev_addr, int freq,
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07005743 int ht40, int vht, int pref_freq)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005744{
5745 enum p2p_invite_role role;
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07005746 u8 *bssid = NULL;
5747 int force_freq = 0;
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08005748 int res;
Dmitry Shmidta0d265f2013-11-19 13:13:41 -08005749 int no_pref_freq_given = pref_freq == 0;
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08005750
Dmitry Shmidt700a1372013-03-15 14:14:44 -07005751 wpa_s->global->p2p_invite_group = NULL;
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08005752 if (peer_addr)
5753 os_memcpy(wpa_s->p2p_auth_invite, peer_addr, ETH_ALEN);
5754 else
5755 os_memset(wpa_s->p2p_auth_invite, 0, ETH_ALEN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005756
Jouni Malinen31be0a42012-08-31 21:20:51 +03005757 wpa_s->p2p_persistent_go_freq = freq;
5758 wpa_s->p2p_go_ht40 = !!ht40;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005759 if (ssid->mode == WPAS_MODE_P2P_GO) {
5760 role = P2P_INVITE_ROLE_GO;
5761 if (peer_addr == NULL) {
5762 wpa_printf(MSG_DEBUG, "P2P: Missing peer "
5763 "address in invitation command");
5764 return -1;
5765 }
5766 if (wpas_p2p_create_iface(wpa_s)) {
5767 if (wpas_p2p_add_group_interface(wpa_s,
5768 WPA_IF_P2P_GO) < 0) {
5769 wpa_printf(MSG_ERROR, "P2P: Failed to "
5770 "allocate a new interface for the "
5771 "group");
5772 return -1;
5773 }
5774 bssid = wpa_s->pending_interface_addr;
5775 } else
5776 bssid = wpa_s->own_addr;
5777 } else {
5778 role = P2P_INVITE_ROLE_CLIENT;
5779 peer_addr = ssid->bssid;
5780 }
5781 wpa_s->pending_invite_ssid_id = ssid->id;
5782
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07005783 res = wpas_p2p_setup_freqs(wpa_s, freq, &force_freq, &pref_freq,
5784 role == P2P_INVITE_ROLE_GO);
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08005785 if (res)
5786 return res;
Irfan Sheriffaf84a572012-09-22 16:59:30 -07005787
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005788 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
5789 return -1;
5790
Dmitry Shmidta0d265f2013-11-19 13:13:41 -08005791 if (wpa_s->parent->conf->p2p_ignore_shared_freq &&
5792 no_pref_freq_given && pref_freq > 0 &&
5793 wpa_s->num_multichan_concurrent > 1 &&
5794 wpas_p2p_num_unused_channels(wpa_s) > 0) {
5795 wpa_printf(MSG_DEBUG, "P2P: Ignore own channel preference %d MHz for invitation due to p2p_ignore_shared_freq=1 configuration",
5796 pref_freq);
5797 pref_freq = 0;
5798 }
5799
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005800 return p2p_invite(wpa_s->global->p2p, peer_addr, role, bssid,
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08005801 ssid->ssid, ssid->ssid_len, force_freq, go_dev_addr,
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08005802 1, pref_freq, -1);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005803}
5804
5805
5806/* Invite to join an active group */
5807int wpas_p2p_invite_group(struct wpa_supplicant *wpa_s, const char *ifname,
5808 const u8 *peer_addr, const u8 *go_dev_addr)
5809{
5810 struct wpa_global *global = wpa_s->global;
5811 enum p2p_invite_role role;
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07005812 u8 *bssid = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005813 struct wpa_ssid *ssid;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08005814 int persistent;
Dmitry Shmidt051af732013-10-22 13:52:46 -07005815 int freq = 0, force_freq = 0, pref_freq = 0;
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08005816 int res;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005817
Jouni Malinen31be0a42012-08-31 21:20:51 +03005818 wpa_s->p2p_persistent_go_freq = 0;
5819 wpa_s->p2p_go_ht40 = 0;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07005820 wpa_s->p2p_go_vht = 0;
Jouni Malinen31be0a42012-08-31 21:20:51 +03005821
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005822 for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
5823 if (os_strcmp(wpa_s->ifname, ifname) == 0)
5824 break;
5825 }
5826 if (wpa_s == NULL) {
5827 wpa_printf(MSG_DEBUG, "P2P: Interface '%s' not found", ifname);
5828 return -1;
5829 }
5830
5831 ssid = wpa_s->current_ssid;
5832 if (ssid == NULL) {
5833 wpa_printf(MSG_DEBUG, "P2P: No current SSID to use for "
5834 "invitation");
5835 return -1;
5836 }
5837
Dmitry Shmidt700a1372013-03-15 14:14:44 -07005838 wpa_s->global->p2p_invite_group = wpa_s;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08005839 persistent = ssid->p2p_persistent_group &&
5840 wpas_p2p_get_persistent(wpa_s->parent, peer_addr,
5841 ssid->ssid, ssid->ssid_len);
5842
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005843 if (ssid->mode == WPAS_MODE_P2P_GO) {
5844 role = P2P_INVITE_ROLE_ACTIVE_GO;
5845 bssid = wpa_s->own_addr;
5846 if (go_dev_addr == NULL)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005847 go_dev_addr = wpa_s->global->p2p_dev_addr;
Dmitry Shmidt051af732013-10-22 13:52:46 -07005848 freq = ssid->frequency;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005849 } else {
5850 role = P2P_INVITE_ROLE_CLIENT;
5851 if (wpa_s->wpa_state < WPA_ASSOCIATED) {
5852 wpa_printf(MSG_DEBUG, "P2P: Not associated - cannot "
5853 "invite to current group");
5854 return -1;
5855 }
5856 bssid = wpa_s->bssid;
5857 if (go_dev_addr == NULL &&
5858 !is_zero_ether_addr(wpa_s->go_dev_addr))
5859 go_dev_addr = wpa_s->go_dev_addr;
Dmitry Shmidt051af732013-10-22 13:52:46 -07005860 freq = wpa_s->current_bss ? wpa_s->current_bss->freq :
5861 (int) wpa_s->assoc_freq;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005862 }
5863 wpa_s->parent->pending_invite_ssid_id = -1;
5864
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005865 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
5866 return -1;
5867
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07005868 res = wpas_p2p_setup_freqs(wpa_s, freq, &force_freq, &pref_freq,
5869 role == P2P_INVITE_ROLE_ACTIVE_GO);
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08005870 if (res)
5871 return res;
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07005872 wpas_p2p_set_own_freq_preference(wpa_s, force_freq);
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08005873
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005874 return p2p_invite(wpa_s->global->p2p, peer_addr, role, bssid,
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08005875 ssid->ssid, ssid->ssid_len, force_freq,
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08005876 go_dev_addr, persistent, pref_freq, -1);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005877}
5878
5879
5880void wpas_p2p_completed(struct wpa_supplicant *wpa_s)
5881{
5882 struct wpa_ssid *ssid = wpa_s->current_ssid;
5883 const char *ssid_txt;
5884 u8 go_dev_addr[ETH_ALEN];
Jouni Malinen75ecf522011-06-27 15:19:46 -07005885 int network_id = -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005886 int persistent;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005887 int freq;
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08005888 u8 ip[3 * 4];
5889 char ip_addr[100];
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005890
Dmitry Shmidt04949592012-07-19 12:16:46 -07005891 if (ssid == NULL || ssid->mode != WPAS_MODE_P2P_GROUP_FORMATION) {
5892 eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
5893 wpa_s->parent, NULL);
5894 }
5895
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005896 if (!wpa_s->show_group_started || !ssid)
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005897 return;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005898
5899 wpa_s->show_group_started = 0;
5900
5901 ssid_txt = wpa_ssid_txt(ssid->ssid, ssid->ssid_len);
5902 os_memset(go_dev_addr, 0, ETH_ALEN);
5903 if (ssid->bssid_set)
5904 os_memcpy(go_dev_addr, ssid->bssid, ETH_ALEN);
5905 persistent = wpas_p2p_persistent_group(wpa_s, go_dev_addr, ssid->ssid,
5906 ssid->ssid_len);
5907 os_memcpy(wpa_s->go_dev_addr, go_dev_addr, ETH_ALEN);
5908
5909 if (wpa_s->global->p2p_group_formation == wpa_s)
5910 wpa_s->global->p2p_group_formation = NULL;
5911
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005912 freq = wpa_s->current_bss ? wpa_s->current_bss->freq :
5913 (int) wpa_s->assoc_freq;
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08005914
5915 ip_addr[0] = '\0';
5916 if (wpa_sm_get_p2p_ip_addr(wpa_s->wpa, ip) == 0) {
5917 os_snprintf(ip_addr, sizeof(ip_addr), " ip_addr=%u.%u.%u.%u "
5918 "ip_mask=%u.%u.%u.%u go_ip_addr=%u.%u.%u.%u",
5919 ip[0], ip[1], ip[2], ip[3],
5920 ip[4], ip[5], ip[6], ip[7],
5921 ip[8], ip[9], ip[10], ip[11]);
5922 }
5923
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005924 if (ssid->passphrase == NULL && ssid->psk_set) {
5925 char psk[65];
5926 wpa_snprintf_hex(psk, sizeof(psk), ssid->psk, 32);
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07005927 wpa_msg_global(wpa_s->parent, MSG_INFO, P2P_EVENT_GROUP_STARTED
5928 "%s client ssid=\"%s\" freq=%d psk=%s "
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08005929 "go_dev_addr=" MACSTR "%s%s",
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07005930 wpa_s->ifname, ssid_txt, freq, psk,
5931 MAC2STR(go_dev_addr),
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08005932 persistent ? " [PERSISTENT]" : "", ip_addr);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005933 } else {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07005934 wpa_msg_global(wpa_s->parent, MSG_INFO, P2P_EVENT_GROUP_STARTED
5935 "%s client ssid=\"%s\" freq=%d "
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08005936 "passphrase=\"%s\" go_dev_addr=" MACSTR "%s%s",
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07005937 wpa_s->ifname, ssid_txt, freq,
5938 ssid->passphrase ? ssid->passphrase : "",
5939 MAC2STR(go_dev_addr),
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08005940 persistent ? " [PERSISTENT]" : "", ip_addr);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005941 }
5942
5943 if (persistent)
Jouni Malinen75ecf522011-06-27 15:19:46 -07005944 network_id = wpas_p2p_store_persistent_group(wpa_s->parent,
5945 ssid, go_dev_addr);
5946 if (network_id < 0)
5947 network_id = ssid->id;
5948 wpas_notify_p2p_group_started(wpa_s, ssid, network_id, 1);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005949}
5950
5951
5952int wpas_p2p_presence_req(struct wpa_supplicant *wpa_s, u32 duration1,
5953 u32 interval1, u32 duration2, u32 interval2)
5954{
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005955 int ret;
5956
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005957 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
5958 return -1;
5959
5960 if (wpa_s->wpa_state < WPA_ASSOCIATED ||
5961 wpa_s->current_ssid == NULL ||
5962 wpa_s->current_ssid->mode != WPAS_MODE_INFRA)
5963 return -1;
5964
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005965 ret = p2p_presence_req(wpa_s->global->p2p, wpa_s->bssid,
5966 wpa_s->own_addr, wpa_s->assoc_freq,
5967 duration1, interval1, duration2, interval2);
5968 if (ret == 0)
5969 wpa_s->waiting_presence_resp = 1;
5970
5971 return ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005972}
5973
5974
5975int wpas_p2p_ext_listen(struct wpa_supplicant *wpa_s, unsigned int period,
5976 unsigned int interval)
5977{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005978 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
5979 return -1;
5980
5981 return p2p_ext_listen(wpa_s->global->p2p, period, interval);
5982}
5983
5984
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005985static int wpas_p2p_is_client(struct wpa_supplicant *wpa_s)
5986{
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08005987 if (wpa_s->current_ssid == NULL) {
5988 /*
5989 * current_ssid can be cleared when P2P client interface gets
5990 * disconnected, so assume this interface was used as P2P
5991 * client.
5992 */
5993 return 1;
5994 }
5995 return wpa_s->current_ssid->p2p_group &&
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005996 wpa_s->current_ssid->mode == WPAS_MODE_INFRA;
5997}
5998
5999
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006000static void wpas_p2p_group_idle_timeout(void *eloop_ctx, void *timeout_ctx)
6001{
6002 struct wpa_supplicant *wpa_s = eloop_ctx;
6003
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006004 if (wpa_s->conf->p2p_group_idle == 0 && !wpas_p2p_is_client(wpa_s)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006005 wpa_printf(MSG_DEBUG, "P2P: Ignore group idle timeout - "
6006 "disabled");
6007 return;
6008 }
6009
Dmitry Shmidt04949592012-07-19 12:16:46 -07006010 wpa_printf(MSG_DEBUG, "P2P: Group idle timeout reached - terminate "
6011 "group");
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07006012 wpas_p2p_group_delete(wpa_s, P2P_GROUP_REMOVAL_IDLE_TIMEOUT);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006013}
6014
6015
6016static void wpas_p2p_set_group_idle_timeout(struct wpa_supplicant *wpa_s)
6017{
Dmitry Shmidt04949592012-07-19 12:16:46 -07006018 int timeout;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006019
Dmitry Shmidt04949592012-07-19 12:16:46 -07006020 if (eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL) > 0)
6021 wpa_printf(MSG_DEBUG, "P2P: Cancelled P2P group idle timeout");
6022
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006023 if (wpa_s->current_ssid == NULL || !wpa_s->current_ssid->p2p_group)
6024 return;
6025
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006026 timeout = wpa_s->conf->p2p_group_idle;
6027 if (wpa_s->current_ssid->mode == WPAS_MODE_INFRA &&
6028 (timeout == 0 || timeout > P2P_MAX_CLIENT_IDLE))
6029 timeout = P2P_MAX_CLIENT_IDLE;
6030
6031 if (timeout == 0)
6032 return;
6033
Dmitry Shmidt04949592012-07-19 12:16:46 -07006034 if (timeout < 0) {
6035 if (wpa_s->current_ssid->mode == WPAS_MODE_INFRA)
6036 timeout = 0; /* special client mode no-timeout */
6037 else
6038 return;
6039 }
6040
6041 if (wpa_s->p2p_in_provisioning) {
6042 /*
6043 * Use the normal group formation timeout during the
6044 * provisioning phase to avoid terminating this process too
6045 * early due to group idle timeout.
6046 */
6047 wpa_printf(MSG_DEBUG, "P2P: Do not use P2P group idle timeout "
6048 "during provisioning");
6049 return;
6050 }
Deepthi Gowri9e4e8ac2013-04-16 11:57:05 +05306051
Dmitry Shmidt04949592012-07-19 12:16:46 -07006052 if (wpa_s->show_group_started) {
6053 /*
6054 * Use the normal group formation timeout between the end of
6055 * the provisioning phase and completion of 4-way handshake to
6056 * avoid terminating this process too early due to group idle
6057 * timeout.
6058 */
6059 wpa_printf(MSG_DEBUG, "P2P: Do not use P2P group idle timeout "
6060 "while waiting for initial 4-way handshake to "
6061 "complete");
6062 return;
6063 }
6064
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006065 wpa_printf(MSG_DEBUG, "P2P: Set P2P group idle timeout to %u seconds",
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006066 timeout);
6067 eloop_register_timeout(timeout, 0, wpas_p2p_group_idle_timeout,
6068 wpa_s, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006069}
6070
6071
Jouni Malinen2b89da82012-08-31 22:04:41 +03006072/* Returns 1 if the interface was removed */
6073int wpas_p2p_deauth_notif(struct wpa_supplicant *wpa_s, const u8 *bssid,
6074 u16 reason_code, const u8 *ie, size_t ie_len,
6075 int locally_generated)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006076{
6077 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
Jouni Malinen2b89da82012-08-31 22:04:41 +03006078 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006079
Dmitry Shmidt04949592012-07-19 12:16:46 -07006080 if (!locally_generated)
6081 p2p_deauth_notif(wpa_s->global->p2p, bssid, reason_code, ie,
6082 ie_len);
6083
6084 if (reason_code == WLAN_REASON_DEAUTH_LEAVING && !locally_generated &&
6085 wpa_s->current_ssid &&
6086 wpa_s->current_ssid->p2p_group &&
6087 wpa_s->current_ssid->mode == WPAS_MODE_INFRA) {
6088 wpa_printf(MSG_DEBUG, "P2P: GO indicated that the P2P Group "
6089 "session is ending");
Jouni Malinen2b89da82012-08-31 22:04:41 +03006090 if (wpas_p2p_group_delete(wpa_s,
6091 P2P_GROUP_REMOVAL_GO_ENDING_SESSION)
6092 > 0)
6093 return 1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07006094 }
Jouni Malinen2b89da82012-08-31 22:04:41 +03006095
6096 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006097}
6098
6099
6100void wpas_p2p_disassoc_notif(struct wpa_supplicant *wpa_s, const u8 *bssid,
Dmitry Shmidt04949592012-07-19 12:16:46 -07006101 u16 reason_code, const u8 *ie, size_t ie_len,
6102 int locally_generated)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006103{
6104 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
6105 return;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006106
Dmitry Shmidt04949592012-07-19 12:16:46 -07006107 if (!locally_generated)
6108 p2p_disassoc_notif(wpa_s->global->p2p, bssid, reason_code, ie,
6109 ie_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006110}
6111
6112
6113void wpas_p2p_update_config(struct wpa_supplicant *wpa_s)
6114{
6115 struct p2p_data *p2p = wpa_s->global->p2p;
6116
6117 if (p2p == NULL)
6118 return;
6119
6120 if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_CAPABLE))
6121 return;
6122
6123 if (wpa_s->conf->changed_parameters & CFG_CHANGED_DEVICE_NAME)
6124 p2p_set_dev_name(p2p, wpa_s->conf->device_name);
6125
6126 if (wpa_s->conf->changed_parameters & CFG_CHANGED_DEVICE_TYPE)
6127 p2p_set_pri_dev_type(p2p, wpa_s->conf->device_type);
6128
6129 if (wpa_s->wps &&
6130 (wpa_s->conf->changed_parameters & CFG_CHANGED_CONFIG_METHODS))
6131 p2p_set_config_methods(p2p, wpa_s->wps->config_methods);
6132
6133 if (wpa_s->wps && (wpa_s->conf->changed_parameters & CFG_CHANGED_UUID))
6134 p2p_set_uuid(p2p, wpa_s->wps->uuid);
6135
6136 if (wpa_s->conf->changed_parameters & CFG_CHANGED_WPS_STRING) {
6137 p2p_set_manufacturer(p2p, wpa_s->conf->manufacturer);
6138 p2p_set_model_name(p2p, wpa_s->conf->model_name);
6139 p2p_set_model_number(p2p, wpa_s->conf->model_number);
6140 p2p_set_serial_number(p2p, wpa_s->conf->serial_number);
6141 }
6142
6143 if (wpa_s->conf->changed_parameters & CFG_CHANGED_SEC_DEVICE_TYPE)
6144 p2p_set_sec_dev_types(p2p,
6145 (void *) wpa_s->conf->sec_device_type,
6146 wpa_s->conf->num_sec_device_types);
6147
6148 if (wpa_s->conf->changed_parameters & CFG_CHANGED_VENDOR_EXTENSION) {
6149 int i;
6150 p2p_remove_wps_vendor_extensions(p2p);
6151 for (i = 0; i < MAX_WPS_VENDOR_EXT; i++) {
6152 if (wpa_s->conf->wps_vendor_ext[i] == NULL)
6153 continue;
6154 p2p_add_wps_vendor_extension(
6155 p2p, wpa_s->conf->wps_vendor_ext[i]);
6156 }
6157 }
6158
6159 if ((wpa_s->conf->changed_parameters & CFG_CHANGED_COUNTRY) &&
6160 wpa_s->conf->country[0] && wpa_s->conf->country[1]) {
6161 char country[3];
6162 country[0] = wpa_s->conf->country[0];
6163 country[1] = wpa_s->conf->country[1];
6164 country[2] = 0x04;
6165 p2p_set_country(p2p, country);
6166 }
6167
6168 if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_SSID_POSTFIX) {
6169 p2p_set_ssid_postfix(p2p, (u8 *) wpa_s->conf->p2p_ssid_postfix,
6170 wpa_s->conf->p2p_ssid_postfix ?
6171 os_strlen(wpa_s->conf->p2p_ssid_postfix) :
6172 0);
6173 }
6174
6175 if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_INTRA_BSS)
6176 p2p_set_intra_bss_dist(p2p, wpa_s->conf->p2p_intra_bss);
Jouni Malinen75ecf522011-06-27 15:19:46 -07006177
6178 if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_LISTEN_CHANNEL) {
6179 u8 reg_class, channel;
6180 int ret;
6181 unsigned int r;
6182 if (wpa_s->conf->p2p_listen_reg_class &&
6183 wpa_s->conf->p2p_listen_channel) {
6184 reg_class = wpa_s->conf->p2p_listen_reg_class;
6185 channel = wpa_s->conf->p2p_listen_channel;
6186 } else {
6187 reg_class = 81;
6188 /*
6189 * Pick one of the social channels randomly as the
6190 * listen channel.
6191 */
6192 os_get_random((u8 *) &r, sizeof(r));
6193 channel = 1 + (r % 3) * 5;
6194 }
6195 ret = p2p_set_listen_channel(p2p, reg_class, channel);
6196 if (ret)
6197 wpa_printf(MSG_ERROR, "P2P: Own listen channel update "
6198 "failed: %d", ret);
6199 }
6200 if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_OPER_CHANNEL) {
6201 u8 op_reg_class, op_channel, cfg_op_channel;
6202 int ret = 0;
6203 unsigned int r;
6204 if (wpa_s->conf->p2p_oper_reg_class &&
6205 wpa_s->conf->p2p_oper_channel) {
6206 op_reg_class = wpa_s->conf->p2p_oper_reg_class;
6207 op_channel = wpa_s->conf->p2p_oper_channel;
6208 cfg_op_channel = 1;
6209 } else {
6210 op_reg_class = 81;
6211 /*
6212 * Use random operation channel from (1, 6, 11)
6213 *if no other preference is indicated.
6214 */
6215 os_get_random((u8 *) &r, sizeof(r));
6216 op_channel = 1 + (r % 3) * 5;
6217 cfg_op_channel = 0;
6218 }
6219 ret = p2p_set_oper_channel(p2p, op_reg_class, op_channel,
6220 cfg_op_channel);
6221 if (ret)
6222 wpa_printf(MSG_ERROR, "P2P: Own oper channel update "
6223 "failed: %d", ret);
6224 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07006225
6226 if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_PREF_CHAN) {
6227 if (p2p_set_pref_chan(p2p, wpa_s->conf->num_p2p_pref_chan,
6228 wpa_s->conf->p2p_pref_chan) < 0) {
6229 wpa_printf(MSG_ERROR, "P2P: Preferred channel list "
6230 "update failed");
6231 }
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07006232
6233 if (p2p_set_no_go_freq(p2p, &wpa_s->conf->p2p_no_go_freq) < 0) {
6234 wpa_printf(MSG_ERROR, "P2P: No GO channel list "
6235 "update failed");
6236 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07006237 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006238}
6239
6240
6241int wpas_p2p_set_noa(struct wpa_supplicant *wpa_s, u8 count, int start,
6242 int duration)
6243{
6244 if (!wpa_s->ap_iface)
6245 return -1;
6246 return hostapd_p2p_set_noa(wpa_s->ap_iface->bss[0], count, start,
6247 duration);
6248}
6249
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006250
6251int wpas_p2p_set_cross_connect(struct wpa_supplicant *wpa_s, int enabled)
6252{
6253 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
6254 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006255
6256 wpa_s->global->cross_connection = enabled;
6257 p2p_set_cross_connect(wpa_s->global->p2p, enabled);
6258
6259 if (!enabled) {
6260 struct wpa_supplicant *iface;
6261
6262 for (iface = wpa_s->global->ifaces; iface; iface = iface->next)
6263 {
6264 if (iface->cross_connect_enabled == 0)
6265 continue;
6266
6267 iface->cross_connect_enabled = 0;
6268 iface->cross_connect_in_use = 0;
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07006269 wpa_msg_global(iface->parent, MSG_INFO,
6270 P2P_EVENT_CROSS_CONNECT_DISABLE "%s %s",
6271 iface->ifname,
6272 iface->cross_connect_uplink);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006273 }
6274 }
6275
6276 return 0;
6277}
6278
6279
6280static void wpas_p2p_enable_cross_connect(struct wpa_supplicant *uplink)
6281{
6282 struct wpa_supplicant *iface;
6283
6284 if (!uplink->global->cross_connection)
6285 return;
6286
6287 for (iface = uplink->global->ifaces; iface; iface = iface->next) {
6288 if (!iface->cross_connect_enabled)
6289 continue;
6290 if (os_strcmp(uplink->ifname, iface->cross_connect_uplink) !=
6291 0)
6292 continue;
6293 if (iface->ap_iface == NULL)
6294 continue;
6295 if (iface->cross_connect_in_use)
6296 continue;
6297
6298 iface->cross_connect_in_use = 1;
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07006299 wpa_msg_global(iface->parent, MSG_INFO,
6300 P2P_EVENT_CROSS_CONNECT_ENABLE "%s %s",
6301 iface->ifname, iface->cross_connect_uplink);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006302 }
6303}
6304
6305
6306static void wpas_p2p_disable_cross_connect(struct wpa_supplicant *uplink)
6307{
6308 struct wpa_supplicant *iface;
6309
6310 for (iface = uplink->global->ifaces; iface; iface = iface->next) {
6311 if (!iface->cross_connect_enabled)
6312 continue;
6313 if (os_strcmp(uplink->ifname, iface->cross_connect_uplink) !=
6314 0)
6315 continue;
6316 if (!iface->cross_connect_in_use)
6317 continue;
6318
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07006319 wpa_msg_global(iface->parent, MSG_INFO,
6320 P2P_EVENT_CROSS_CONNECT_DISABLE "%s %s",
6321 iface->ifname, iface->cross_connect_uplink);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006322 iface->cross_connect_in_use = 0;
6323 }
6324}
6325
6326
6327void wpas_p2p_notif_connected(struct wpa_supplicant *wpa_s)
6328{
6329 if (wpa_s->ap_iface || wpa_s->current_ssid == NULL ||
6330 wpa_s->current_ssid->mode != WPAS_MODE_INFRA ||
6331 wpa_s->cross_connect_disallowed)
6332 wpas_p2p_disable_cross_connect(wpa_s);
6333 else
6334 wpas_p2p_enable_cross_connect(wpa_s);
Dmitry Shmidt04949592012-07-19 12:16:46 -07006335 if (!wpa_s->ap_iface &&
6336 eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL) > 0)
6337 wpa_printf(MSG_DEBUG, "P2P: Cancelled P2P group idle timeout");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006338}
6339
6340
6341void wpas_p2p_notif_disconnected(struct wpa_supplicant *wpa_s)
6342{
6343 wpas_p2p_disable_cross_connect(wpa_s);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006344 if (!wpa_s->ap_iface &&
6345 !eloop_is_timeout_registered(wpas_p2p_group_idle_timeout,
6346 wpa_s, NULL))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006347 wpas_p2p_set_group_idle_timeout(wpa_s);
6348}
6349
6350
6351static void wpas_p2p_cross_connect_setup(struct wpa_supplicant *wpa_s)
6352{
6353 struct wpa_supplicant *iface;
6354
6355 if (!wpa_s->global->cross_connection)
6356 return;
6357
6358 for (iface = wpa_s->global->ifaces; iface; iface = iface->next) {
6359 if (iface == wpa_s)
6360 continue;
6361 if (iface->drv_flags &
6362 WPA_DRIVER_FLAGS_P2P_DEDICATED_INTERFACE)
6363 continue;
6364 if (iface->drv_flags & WPA_DRIVER_FLAGS_P2P_CAPABLE)
6365 continue;
6366
6367 wpa_s->cross_connect_enabled = 1;
6368 os_strlcpy(wpa_s->cross_connect_uplink, iface->ifname,
6369 sizeof(wpa_s->cross_connect_uplink));
6370 wpa_printf(MSG_DEBUG, "P2P: Enable cross connection from "
6371 "%s to %s whenever uplink is available",
6372 wpa_s->ifname, wpa_s->cross_connect_uplink);
6373
6374 if (iface->ap_iface || iface->current_ssid == NULL ||
6375 iface->current_ssid->mode != WPAS_MODE_INFRA ||
6376 iface->cross_connect_disallowed ||
6377 iface->wpa_state != WPA_COMPLETED)
6378 break;
6379
6380 wpa_s->cross_connect_in_use = 1;
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07006381 wpa_msg_global(wpa_s->parent, MSG_INFO,
6382 P2P_EVENT_CROSS_CONNECT_ENABLE "%s %s",
6383 wpa_s->ifname, wpa_s->cross_connect_uplink);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006384 break;
6385 }
6386}
6387
6388
6389int wpas_p2p_notif_pbc_overlap(struct wpa_supplicant *wpa_s)
6390{
6391 if (wpa_s->p2p_group_interface != P2P_GROUP_INTERFACE_CLIENT &&
6392 !wpa_s->p2p_in_provisioning)
6393 return 0; /* not P2P client operation */
6394
6395 wpa_printf(MSG_DEBUG, "P2P: Terminate connection due to WPS PBC "
6396 "session overlap");
6397 if (wpa_s != wpa_s->parent)
6398 wpa_msg_ctrl(wpa_s->parent, MSG_INFO, WPS_EVENT_OVERLAP);
Dmitry Shmidt34af3062013-07-11 10:46:32 -07006399 wpas_p2p_group_formation_failed(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006400 return 1;
6401}
6402
6403
Dmitry Shmidtdf5a7e42014-04-02 12:59:59 -07006404void wpas_p2p_pbc_overlap_cb(void *eloop_ctx, void *timeout_ctx)
6405{
6406 struct wpa_supplicant *wpa_s = eloop_ctx;
6407 wpas_p2p_notif_pbc_overlap(wpa_s);
6408}
6409
6410
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006411void wpas_p2p_update_channel_list(struct wpa_supplicant *wpa_s)
6412{
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07006413 struct p2p_channels chan, cli_chan;
Dmitry Shmidt684785c2014-05-12 13:34:29 -07006414 struct wpa_supplicant *ifs;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006415
6416 if (wpa_s->global == NULL || wpa_s->global->p2p == NULL)
6417 return;
6418
6419 os_memset(&chan, 0, sizeof(chan));
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07006420 os_memset(&cli_chan, 0, sizeof(cli_chan));
6421 if (wpas_p2p_setup_channels(wpa_s, &chan, &cli_chan)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006422 wpa_printf(MSG_ERROR, "P2P: Failed to update supported "
6423 "channel list");
6424 return;
6425 }
6426
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07006427 p2p_update_channel_list(wpa_s->global->p2p, &chan, &cli_chan);
Dmitry Shmidt684785c2014-05-12 13:34:29 -07006428
6429 for (ifs = wpa_s->global->ifaces; ifs; ifs = ifs->next) {
6430 int freq;
6431 if (!ifs->current_ssid ||
6432 !ifs->current_ssid->p2p_group ||
6433 (ifs->current_ssid->mode != WPAS_MODE_P2P_GO &&
6434 ifs->current_ssid->mode != WPAS_MODE_P2P_GROUP_FORMATION))
6435 continue;
6436 freq = ifs->current_ssid->frequency;
6437 if (freq_included(&chan, freq)) {
6438 wpa_dbg(ifs, MSG_DEBUG,
6439 "P2P GO operating frequency %d MHz in valid range",
6440 freq);
6441 continue;
6442 }
6443
6444 wpa_dbg(ifs, MSG_DEBUG,
6445 "P2P GO operating in invalid frequency %d MHz", freq);
6446 /* TODO: Consider using CSA or removing the group within
6447 * wpa_supplicant */
6448 wpa_msg(ifs, MSG_INFO, P2P_EVENT_REMOVE_AND_REFORM_GROUP);
6449 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006450}
6451
6452
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08006453static void wpas_p2p_scan_res_ignore(struct wpa_supplicant *wpa_s,
6454 struct wpa_scan_results *scan_res)
6455{
6456 wpa_printf(MSG_DEBUG, "P2P: Ignore scan results");
6457}
6458
6459
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006460int wpas_p2p_cancel(struct wpa_supplicant *wpa_s)
6461{
6462 struct wpa_global *global = wpa_s->global;
6463 int found = 0;
6464 const u8 *peer;
6465
6466 if (global->p2p == NULL)
6467 return -1;
6468
6469 wpa_printf(MSG_DEBUG, "P2P: Request to cancel group formation");
6470
6471 if (wpa_s->pending_interface_name[0] &&
6472 !is_zero_ether_addr(wpa_s->pending_interface_addr))
6473 found = 1;
6474
6475 peer = p2p_get_go_neg_peer(global->p2p);
6476 if (peer) {
6477 wpa_printf(MSG_DEBUG, "P2P: Unauthorize pending GO Neg peer "
6478 MACSTR, MAC2STR(peer));
6479 p2p_unauthorize(global->p2p, peer);
Dmitry Shmidt04949592012-07-19 12:16:46 -07006480 found = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006481 }
6482
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08006483 if (wpa_s->scan_res_handler == wpas_p2p_scan_res_join) {
6484 wpa_printf(MSG_DEBUG, "P2P: Stop pending scan for join");
6485 wpa_s->scan_res_handler = wpas_p2p_scan_res_ignore;
6486 found = 1;
6487 }
6488
6489 if (wpa_s->pending_pd_before_join) {
6490 wpa_printf(MSG_DEBUG, "P2P: Stop pending PD before join");
6491 wpa_s->pending_pd_before_join = 0;
6492 found = 1;
6493 }
6494
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006495 wpas_p2p_stop_find(wpa_s);
6496
6497 for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
6498 if (wpa_s == global->p2p_group_formation &&
6499 (wpa_s->p2p_in_provisioning ||
6500 wpa_s->parent->pending_interface_type ==
6501 WPA_IF_P2P_CLIENT)) {
6502 wpa_printf(MSG_DEBUG, "P2P: Interface %s in group "
6503 "formation found - cancelling",
6504 wpa_s->ifname);
6505 found = 1;
6506 eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
6507 wpa_s->parent, NULL);
Jouni Malinenadddfc42012-10-03 14:31:41 -07006508 if (wpa_s->p2p_in_provisioning) {
6509 wpas_group_formation_completed(wpa_s, 0);
6510 break;
6511 }
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07006512 wpas_p2p_group_delete(wpa_s,
6513 P2P_GROUP_REMOVAL_REQUESTED);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006514 break;
Dmitry Shmidt21de2142014-04-08 10:50:52 -07006515 } else if (wpa_s->p2p_in_invitation) {
6516 wpa_printf(MSG_DEBUG, "P2P: Interface %s in invitation found - cancelling",
6517 wpa_s->ifname);
6518 found = 1;
6519 wpas_p2p_group_formation_failed(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006520 }
6521 }
6522
6523 if (!found) {
6524 wpa_printf(MSG_DEBUG, "P2P: No ongoing group formation found");
6525 return -1;
6526 }
6527
6528 return 0;
6529}
6530
6531
6532void wpas_p2p_interface_unavailable(struct wpa_supplicant *wpa_s)
6533{
6534 if (wpa_s->current_ssid == NULL || !wpa_s->current_ssid->p2p_group)
6535 return;
6536
6537 wpa_printf(MSG_DEBUG, "P2P: Remove group due to driver resource not "
6538 "being available anymore");
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07006539 wpas_p2p_group_delete(wpa_s, P2P_GROUP_REMOVAL_UNAVAILABLE);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006540}
6541
6542
6543void wpas_p2p_update_best_channels(struct wpa_supplicant *wpa_s,
6544 int freq_24, int freq_5, int freq_overall)
6545{
6546 struct p2p_data *p2p = wpa_s->global->p2p;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08006547 if (p2p == NULL)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006548 return;
6549 p2p_set_best_channels(p2p, freq_24, freq_5, freq_overall);
6550}
6551
6552
6553int wpas_p2p_unauthorize(struct wpa_supplicant *wpa_s, const char *addr)
6554{
6555 u8 peer[ETH_ALEN];
6556 struct p2p_data *p2p = wpa_s->global->p2p;
6557
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08006558 if (p2p == NULL)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006559 return -1;
6560
6561 if (hwaddr_aton(addr, peer))
6562 return -1;
6563
6564 return p2p_unauthorize(p2p, peer);
6565}
6566
6567
6568/**
6569 * wpas_p2p_disconnect - Disconnect from a P2P Group
6570 * @wpa_s: Pointer to wpa_supplicant data
6571 * Returns: 0 on success, -1 on failure
6572 *
6573 * This can be used to disconnect from a group in which the local end is a P2P
6574 * Client or to end a P2P Group in case the local end is the Group Owner. If a
6575 * virtual network interface was created for this group, that interface will be
6576 * removed. Otherwise, only the configured P2P group network will be removed
6577 * from the interface.
6578 */
6579int wpas_p2p_disconnect(struct wpa_supplicant *wpa_s)
6580{
6581
6582 if (wpa_s == NULL)
6583 return -1;
6584
Jouni Malinen2b89da82012-08-31 22:04:41 +03006585 return wpas_p2p_group_delete(wpa_s, P2P_GROUP_REMOVAL_REQUESTED) < 0 ?
6586 -1 : 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006587}
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006588
6589
6590int wpas_p2p_in_progress(struct wpa_supplicant *wpa_s)
6591{
Dmitry Shmidtf8623282013-02-20 14:34:59 -08006592 int ret;
6593
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006594 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
6595 return 0;
6596
Dmitry Shmidtf8623282013-02-20 14:34:59 -08006597 ret = p2p_in_progress(wpa_s->global->p2p);
6598 if (ret == 0) {
6599 /*
6600 * Check whether there is an ongoing WPS provisioning step (or
6601 * other parts of group formation) on another interface since
6602 * p2p_in_progress() does not report this to avoid issues for
6603 * scans during such provisioning step.
6604 */
6605 if (wpa_s->global->p2p_group_formation &&
6606 wpa_s->global->p2p_group_formation != wpa_s) {
6607 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Another interface (%s) "
6608 "in group formation",
6609 wpa_s->global->p2p_group_formation->ifname);
6610 ret = 1;
6611 }
6612 }
6613
Dmitry Shmidt92c368d2013-08-29 12:37:21 -07006614 if (!ret && wpa_s->global->p2p_go_wait_client.sec) {
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08006615 struct os_reltime now;
6616 os_get_reltime(&now);
6617 if (os_reltime_expired(&now, &wpa_s->global->p2p_go_wait_client,
6618 P2P_MAX_INITIAL_CONN_WAIT_GO)) {
Dmitry Shmidt92c368d2013-08-29 12:37:21 -07006619 /* Wait for the first client has expired */
6620 wpa_s->global->p2p_go_wait_client.sec = 0;
6621 } else {
6622 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Waiting for initial client connection during group formation");
6623 ret = 1;
6624 }
6625 }
6626
Dmitry Shmidtf8623282013-02-20 14:34:59 -08006627 return ret;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006628}
6629
6630
6631void wpas_p2p_network_removed(struct wpa_supplicant *wpa_s,
6632 struct wpa_ssid *ssid)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006633{
6634 if (wpa_s->p2p_in_provisioning && ssid->p2p_group &&
6635 eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
6636 wpa_s->parent, NULL) > 0) {
Dmitry Shmidt04949592012-07-19 12:16:46 -07006637 /**
6638 * Remove the network by scheduling the group formation
6639 * timeout to happen immediately. The teardown code
6640 * needs to be scheduled to run asynch later so that we
6641 * don't delete data from under ourselves unexpectedly.
6642 * Calling wpas_p2p_group_formation_timeout directly
6643 * causes a series of crashes in WPS failure scenarios.
6644 */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006645 wpa_printf(MSG_DEBUG, "P2P: Canceled group formation due to "
6646 "P2P group network getting removed");
Dmitry Shmidt04949592012-07-19 12:16:46 -07006647 eloop_register_timeout(0, 0, wpas_p2p_group_formation_timeout,
6648 wpa_s->parent, NULL);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006649 }
6650}
6651
6652
6653struct wpa_ssid * wpas_p2p_get_persistent(struct wpa_supplicant *wpa_s,
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08006654 const u8 *addr, const u8 *ssid,
6655 size_t ssid_len)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006656{
6657 struct wpa_ssid *s;
6658 size_t i;
6659
6660 for (s = wpa_s->conf->ssid; s; s = s->next) {
6661 if (s->disabled != 2)
6662 continue;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08006663 if (ssid &&
6664 (ssid_len != s->ssid_len ||
6665 os_memcmp(ssid, s->ssid, ssid_len) != 0))
6666 continue;
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07006667 if (addr == NULL) {
6668 if (s->mode == WPAS_MODE_P2P_GO)
6669 return s;
6670 continue;
6671 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006672 if (os_memcmp(s->bssid, addr, ETH_ALEN) == 0)
6673 return s; /* peer is GO in the persistent group */
6674 if (s->mode != WPAS_MODE_P2P_GO || s->p2p_client_list == NULL)
6675 continue;
6676 for (i = 0; i < s->num_p2p_clients; i++) {
6677 if (os_memcmp(s->p2p_client_list + i * ETH_ALEN,
6678 addr, ETH_ALEN) == 0)
6679 return s; /* peer is P2P client in persistent
6680 * group */
6681 }
6682 }
6683
6684 return NULL;
6685}
6686
6687
6688void wpas_p2p_notify_ap_sta_authorized(struct wpa_supplicant *wpa_s,
6689 const u8 *addr)
6690{
Dmitry Shmidt56052862013-10-04 10:23:25 -07006691 if (eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
6692 wpa_s->parent, NULL) > 0) {
6693 /*
6694 * This can happen if WPS provisioning step is not terminated
6695 * cleanly (e.g., P2P Client does not send WSC_Done). Since the
6696 * peer was able to connect, there is no need to time out group
6697 * formation after this, though. In addition, this is used with
6698 * the initial connection wait on the GO as a separate formation
6699 * timeout and as such, expected to be hit after the initial WPS
6700 * provisioning step.
6701 */
6702 wpa_printf(MSG_DEBUG, "P2P: Canceled P2P group formation timeout on data connection");
6703 }
6704 if (!wpa_s->p2p_go_group_formation_completed) {
6705 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Marking group formation completed on GO on first data connection");
6706 wpa_s->p2p_go_group_formation_completed = 1;
6707 wpa_s->global->p2p_group_formation = NULL;
6708 wpa_s->p2p_in_provisioning = 0;
Dmitry Shmidt21de2142014-04-08 10:50:52 -07006709 wpa_s->p2p_in_invitation = 0;
Dmitry Shmidt56052862013-10-04 10:23:25 -07006710 }
Dmitry Shmidt92c368d2013-08-29 12:37:21 -07006711 wpa_s->global->p2p_go_wait_client.sec = 0;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006712 if (addr == NULL)
6713 return;
6714 wpas_p2p_add_persistent_group_client(wpa_s, addr);
6715}
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08006716
Dmitry Shmidt04949592012-07-19 12:16:46 -07006717
6718static void wpas_p2p_fallback_to_go_neg(struct wpa_supplicant *wpa_s,
6719 int group_added)
6720{
6721 struct wpa_supplicant *group = wpa_s;
Dmitry Shmidt04949592012-07-19 12:16:46 -07006722 if (wpa_s->global->p2p_group_formation)
6723 group = wpa_s->global->p2p_group_formation;
6724 wpa_s = wpa_s->parent;
6725 offchannel_send_action_done(wpa_s);
6726 if (group_added)
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07006727 wpas_p2p_group_delete(group, P2P_GROUP_REMOVAL_SILENT);
Dmitry Shmidt04949592012-07-19 12:16:46 -07006728 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Fall back to GO Negotiation");
6729 wpas_p2p_connect(wpa_s, wpa_s->pending_join_dev_addr, wpa_s->p2p_pin,
6730 wpa_s->p2p_wps_method, wpa_s->p2p_persistent_group, 0,
6731 0, 0, wpa_s->p2p_go_intent, wpa_s->p2p_connect_freq,
6732 wpa_s->p2p_persistent_id,
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07006733 wpa_s->p2p_pd_before_go_neg,
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07006734 wpa_s->p2p_go_ht40,
6735 wpa_s->p2p_go_vht);
Dmitry Shmidt04949592012-07-19 12:16:46 -07006736}
6737
6738
6739int wpas_p2p_scan_no_go_seen(struct wpa_supplicant *wpa_s)
6740{
6741 if (!wpa_s->p2p_fallback_to_go_neg ||
6742 wpa_s->p2p_in_provisioning <= 5)
6743 return 0;
6744
6745 if (wpas_p2p_peer_go(wpa_s, wpa_s->pending_join_dev_addr) > 0)
6746 return 0; /* peer operating as a GO */
6747
6748 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: GO not found for p2p_connect-auto - "
6749 "fallback to GO Negotiation");
6750 wpas_p2p_fallback_to_go_neg(wpa_s, 1);
6751
6752 return 1;
6753}
6754
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07006755
6756unsigned int wpas_p2p_search_delay(struct wpa_supplicant *wpa_s)
6757{
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07006758 struct wpa_supplicant *ifs;
6759
6760 if (wpa_s->wpa_state > WPA_SCANNING) {
6761 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Use %u ms search delay due to "
6762 "concurrent operation",
Dmitry Shmidt09f57ba2014-06-10 16:07:13 -07006763 wpa_s->conf->p2p_search_delay);
6764 return wpa_s->conf->p2p_search_delay;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07006765 }
6766
Dmitry Shmidt01904cf2013-12-05 11:08:35 -08006767 dl_list_for_each(ifs, &wpa_s->radio->ifaces, struct wpa_supplicant,
6768 radio_list) {
6769 if (ifs != wpa_s && ifs->wpa_state > WPA_SCANNING) {
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07006770 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Use %u ms search "
6771 "delay due to concurrent operation on "
6772 "interface %s",
Dmitry Shmidt09f57ba2014-06-10 16:07:13 -07006773 wpa_s->conf->p2p_search_delay,
6774 ifs->ifname);
6775 return wpa_s->conf->p2p_search_delay;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07006776 }
6777 }
6778
6779 return 0;
6780}
6781
Dmitry Shmidt37d4d6a2013-03-18 13:09:42 -07006782
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07006783static int wpas_p2p_remove_psk_entry(struct wpa_supplicant *wpa_s,
6784 struct wpa_ssid *s, const u8 *addr,
6785 int iface_addr)
6786{
6787 struct psk_list_entry *psk, *tmp;
6788 int changed = 0;
6789
6790 dl_list_for_each_safe(psk, tmp, &s->psk_list, struct psk_list_entry,
6791 list) {
6792 if ((iface_addr && !psk->p2p &&
6793 os_memcmp(addr, psk->addr, ETH_ALEN) == 0) ||
6794 (!iface_addr && psk->p2p &&
6795 os_memcmp(addr, psk->addr, ETH_ALEN) == 0)) {
6796 wpa_dbg(wpa_s, MSG_DEBUG,
6797 "P2P: Remove persistent group PSK list entry for "
6798 MACSTR " p2p=%u",
6799 MAC2STR(psk->addr), psk->p2p);
6800 dl_list_del(&psk->list);
6801 os_free(psk);
6802 changed++;
6803 }
6804 }
6805
6806 return changed;
6807}
6808
6809
6810void wpas_p2p_new_psk_cb(struct wpa_supplicant *wpa_s, const u8 *mac_addr,
6811 const u8 *p2p_dev_addr,
6812 const u8 *psk, size_t psk_len)
6813{
6814 struct wpa_ssid *ssid = wpa_s->current_ssid;
6815 struct wpa_ssid *persistent;
Dmitry Shmidt7832adb2014-04-29 10:53:02 -07006816 struct psk_list_entry *p, *last;
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07006817
6818 if (psk_len != sizeof(p->psk))
6819 return;
6820
6821 if (p2p_dev_addr) {
6822 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: New PSK for addr=" MACSTR
6823 " p2p_dev_addr=" MACSTR,
6824 MAC2STR(mac_addr), MAC2STR(p2p_dev_addr));
6825 if (is_zero_ether_addr(p2p_dev_addr))
6826 p2p_dev_addr = NULL;
6827 } else {
6828 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: New PSK for addr=" MACSTR,
6829 MAC2STR(mac_addr));
6830 }
6831
6832 if (ssid->mode == WPAS_MODE_P2P_GROUP_FORMATION) {
6833 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: new_psk_cb during group formation");
6834 /* To be added to persistent group once created */
6835 if (wpa_s->global->add_psk == NULL) {
6836 wpa_s->global->add_psk = os_zalloc(sizeof(*p));
6837 if (wpa_s->global->add_psk == NULL)
6838 return;
6839 }
6840 p = wpa_s->global->add_psk;
6841 if (p2p_dev_addr) {
6842 p->p2p = 1;
6843 os_memcpy(p->addr, p2p_dev_addr, ETH_ALEN);
6844 } else {
6845 p->p2p = 0;
6846 os_memcpy(p->addr, mac_addr, ETH_ALEN);
6847 }
6848 os_memcpy(p->psk, psk, psk_len);
6849 return;
6850 }
6851
6852 if (ssid->mode != WPAS_MODE_P2P_GO || !ssid->p2p_persistent_group) {
6853 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Ignore new_psk_cb on not-persistent GO");
6854 return;
6855 }
6856
6857 persistent = wpas_p2p_get_persistent(wpa_s->parent, NULL, ssid->ssid,
6858 ssid->ssid_len);
6859 if (!persistent) {
6860 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Could not find persistent group information to store the new PSK");
6861 return;
6862 }
6863
6864 p = os_zalloc(sizeof(*p));
6865 if (p == NULL)
6866 return;
6867 if (p2p_dev_addr) {
6868 p->p2p = 1;
6869 os_memcpy(p->addr, p2p_dev_addr, ETH_ALEN);
6870 } else {
6871 p->p2p = 0;
6872 os_memcpy(p->addr, mac_addr, ETH_ALEN);
6873 }
6874 os_memcpy(p->psk, psk, psk_len);
6875
Dmitry Shmidt7832adb2014-04-29 10:53:02 -07006876 if (dl_list_len(&persistent->psk_list) > P2P_MAX_STORED_CLIENTS &&
6877 (last = dl_list_last(&persistent->psk_list,
6878 struct psk_list_entry, list))) {
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07006879 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Remove oldest PSK entry for "
6880 MACSTR " (p2p=%u) to make room for a new one",
6881 MAC2STR(last->addr), last->p2p);
6882 dl_list_del(&last->list);
6883 os_free(last);
6884 }
6885
6886 wpas_p2p_remove_psk_entry(wpa_s->parent, persistent,
6887 p2p_dev_addr ? p2p_dev_addr : mac_addr,
6888 p2p_dev_addr == NULL);
6889 if (p2p_dev_addr) {
6890 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Add new PSK for p2p_dev_addr="
6891 MACSTR, MAC2STR(p2p_dev_addr));
6892 } else {
6893 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Add new PSK for addr=" MACSTR,
6894 MAC2STR(mac_addr));
6895 }
6896 dl_list_add(&persistent->psk_list, &p->list);
6897
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07006898 if (wpa_s->parent->conf->update_config &&
6899 wpa_config_write(wpa_s->parent->confname, wpa_s->parent->conf))
6900 wpa_printf(MSG_DEBUG, "P2P: Failed to update configuration");
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07006901}
6902
6903
6904static void wpas_p2p_remove_psk(struct wpa_supplicant *wpa_s,
6905 struct wpa_ssid *s, const u8 *addr,
6906 int iface_addr)
6907{
6908 int res;
6909
6910 res = wpas_p2p_remove_psk_entry(wpa_s, s, addr, iface_addr);
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08006911 if (res > 0 && wpa_s->conf->update_config &&
6912 wpa_config_write(wpa_s->confname, wpa_s->conf))
6913 wpa_dbg(wpa_s, MSG_DEBUG,
6914 "P2P: Failed to update configuration");
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07006915}
6916
6917
6918static void wpas_p2p_remove_client_go(struct wpa_supplicant *wpa_s,
6919 const u8 *peer, int iface_addr)
6920{
6921 struct hostapd_data *hapd;
6922 struct hostapd_wpa_psk *psk, *prev, *rem;
6923 struct sta_info *sta;
6924
6925 if (wpa_s->ap_iface == NULL || wpa_s->current_ssid == NULL ||
6926 wpa_s->current_ssid->mode != WPAS_MODE_P2P_GO)
6927 return;
6928
6929 /* Remove per-station PSK entry */
6930 hapd = wpa_s->ap_iface->bss[0];
6931 prev = NULL;
6932 psk = hapd->conf->ssid.wpa_psk;
6933 while (psk) {
6934 if ((iface_addr && os_memcmp(peer, psk->addr, ETH_ALEN) == 0) ||
6935 (!iface_addr &&
6936 os_memcmp(peer, psk->p2p_dev_addr, ETH_ALEN) == 0)) {
6937 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Remove operating group PSK entry for "
6938 MACSTR " iface_addr=%d",
6939 MAC2STR(peer), iface_addr);
6940 if (prev)
6941 prev->next = psk->next;
6942 else
6943 hapd->conf->ssid.wpa_psk = psk->next;
6944 rem = psk;
6945 psk = psk->next;
6946 os_free(rem);
6947 } else {
6948 prev = psk;
6949 psk = psk->next;
6950 }
6951 }
6952
6953 /* Disconnect from group */
6954 if (iface_addr)
6955 sta = ap_get_sta(hapd, peer);
6956 else
6957 sta = ap_get_sta_p2p(hapd, peer);
6958 if (sta) {
6959 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Disconnect peer " MACSTR
6960 " (iface_addr=%d) from group",
6961 MAC2STR(peer), iface_addr);
6962 hostapd_drv_sta_deauth(hapd, sta->addr,
6963 WLAN_REASON_DEAUTH_LEAVING);
6964 ap_sta_deauthenticate(hapd, sta, WLAN_REASON_DEAUTH_LEAVING);
6965 }
6966}
6967
6968
6969void wpas_p2p_remove_client(struct wpa_supplicant *wpa_s, const u8 *peer,
6970 int iface_addr)
6971{
6972 struct wpa_ssid *s;
6973 struct wpa_supplicant *w;
6974
6975 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Remove client " MACSTR, MAC2STR(peer));
6976
6977 /* Remove from any persistent group */
6978 for (s = wpa_s->parent->conf->ssid; s; s = s->next) {
6979 if (s->disabled != 2 || s->mode != WPAS_MODE_P2P_GO)
6980 continue;
6981 if (!iface_addr)
6982 wpas_remove_persistent_peer(wpa_s, s, peer, 0);
6983 wpas_p2p_remove_psk(wpa_s->parent, s, peer, iface_addr);
6984 }
6985
6986 /* Remove from any operating group */
6987 for (w = wpa_s->global->ifaces; w; w = w->next)
6988 wpas_p2p_remove_client_go(w, peer, iface_addr);
6989}
6990
6991
6992static void wpas_p2p_psk_failure_removal(void *eloop_ctx, void *timeout_ctx)
6993{
6994 struct wpa_supplicant *wpa_s = eloop_ctx;
6995 wpas_p2p_group_delete(wpa_s, P2P_GROUP_REMOVAL_PSK_FAILURE);
6996}
6997
6998
Dmitry Shmidt04f534e2013-12-09 15:50:16 -08006999static void wpas_p2p_group_freq_conflict(void *eloop_ctx, void *timeout_ctx)
7000{
7001 struct wpa_supplicant *wpa_s = eloop_ctx;
7002
7003 wpa_printf(MSG_DEBUG, "P2P: Frequency conflict - terminate group");
7004 wpas_p2p_group_delete(wpa_s, P2P_GROUP_REMOVAL_FREQ_CONFLICT);
7005}
7006
7007
7008int wpas_p2p_handle_frequency_conflicts(struct wpa_supplicant *wpa_s, int freq,
7009 struct wpa_ssid *ssid)
7010{
7011 struct wpa_supplicant *iface;
7012
7013 for (iface = wpa_s->global->ifaces; iface; iface = iface->next) {
7014 if (!iface->current_ssid ||
7015 iface->current_ssid->frequency == freq ||
7016 (iface->p2p_group_interface == NOT_P2P_GROUP_INTERFACE &&
7017 !iface->current_ssid->p2p_group))
7018 continue;
7019
7020 /* Remove the connection with least priority */
7021 if (!wpas_is_p2p_prioritized(iface)) {
7022 /* STA connection has priority over existing
7023 * P2P connection, so remove the interface. */
7024 wpa_printf(MSG_DEBUG, "P2P: Removing P2P connection due to single channel concurrent mode frequency conflict");
7025 eloop_register_timeout(0, 0,
7026 wpas_p2p_group_freq_conflict,
7027 iface, NULL);
7028 /* If connection in progress is P2P connection, do not
7029 * proceed for the connection. */
7030 if (wpa_s == iface)
7031 return -1;
7032 else
7033 return 0;
7034 } else {
7035 /* P2P connection has priority, disable the STA network
7036 */
7037 wpa_supplicant_disable_network(wpa_s->global->ifaces,
7038 ssid);
7039 wpa_msg(wpa_s->global->ifaces, MSG_INFO,
7040 WPA_EVENT_FREQ_CONFLICT " id=%d", ssid->id);
7041 os_memset(wpa_s->global->ifaces->pending_bssid, 0,
7042 ETH_ALEN);
7043 /* If P2P connection is in progress, continue
7044 * connecting...*/
7045 if (wpa_s == iface)
7046 return 0;
7047 else
7048 return -1;
7049 }
7050 }
7051
7052 return 0;
7053}
7054
7055
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07007056int wpas_p2p_4way_hs_failed(struct wpa_supplicant *wpa_s)
7057{
7058 struct wpa_ssid *ssid = wpa_s->current_ssid;
7059
7060 if (ssid == NULL || !ssid->p2p_group)
7061 return 0;
7062
7063 if (wpa_s->p2p_last_4way_hs_fail &&
7064 wpa_s->p2p_last_4way_hs_fail == ssid) {
7065 u8 go_dev_addr[ETH_ALEN];
7066 struct wpa_ssid *persistent;
7067
7068 if (wpas_p2p_persistent_group(wpa_s, go_dev_addr,
7069 ssid->ssid,
7070 ssid->ssid_len) <= 0) {
7071 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Could not determine whether 4-way handshake failures were for a persistent group");
7072 goto disconnect;
7073 }
7074
7075 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Two 4-way handshake failures for a P2P group - go_dev_addr="
7076 MACSTR, MAC2STR(go_dev_addr));
7077 persistent = wpas_p2p_get_persistent(wpa_s->parent, go_dev_addr,
7078 ssid->ssid,
7079 ssid->ssid_len);
7080 if (persistent == NULL || persistent->mode != WPAS_MODE_INFRA) {
7081 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: No matching persistent group stored");
7082 goto disconnect;
7083 }
7084 wpa_msg_global(wpa_s->parent, MSG_INFO,
7085 P2P_EVENT_PERSISTENT_PSK_FAIL "%d",
7086 persistent->id);
7087 disconnect:
7088 wpa_s->p2p_last_4way_hs_fail = NULL;
7089 /*
7090 * Remove the group from a timeout to avoid issues with caller
7091 * continuing to use the interface if this is on a P2P group
7092 * interface.
7093 */
7094 eloop_register_timeout(0, 0, wpas_p2p_psk_failure_removal,
7095 wpa_s, NULL);
7096 return 1;
7097 }
7098
7099 wpa_s->p2p_last_4way_hs_fail = ssid;
7100 return 0;
7101}
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08007102
7103
7104#ifdef CONFIG_WPS_NFC
7105
7106static struct wpabuf * wpas_p2p_nfc_handover(int ndef, struct wpabuf *wsc,
7107 struct wpabuf *p2p)
7108{
7109 struct wpabuf *ret;
7110 size_t wsc_len;
7111
7112 if (p2p == NULL) {
7113 wpabuf_free(wsc);
7114 wpa_printf(MSG_DEBUG, "P2P: No p2p buffer for handover");
7115 return NULL;
7116 }
7117
7118 wsc_len = wsc ? wpabuf_len(wsc) : 0;
7119 ret = wpabuf_alloc(2 + wsc_len + 2 + wpabuf_len(p2p));
7120 if (ret == NULL) {
7121 wpabuf_free(wsc);
7122 wpabuf_free(p2p);
7123 return NULL;
7124 }
7125
7126 wpabuf_put_be16(ret, wsc_len);
7127 if (wsc)
7128 wpabuf_put_buf(ret, wsc);
7129 wpabuf_put_be16(ret, wpabuf_len(p2p));
7130 wpabuf_put_buf(ret, p2p);
7131
7132 wpabuf_free(wsc);
7133 wpabuf_free(p2p);
7134 wpa_hexdump_buf(MSG_DEBUG,
7135 "P2P: Generated NFC connection handover message", ret);
7136
7137 if (ndef && ret) {
7138 struct wpabuf *tmp;
7139 tmp = ndef_build_p2p(ret);
7140 wpabuf_free(ret);
7141 if (tmp == NULL) {
7142 wpa_printf(MSG_DEBUG, "P2P: Failed to NDEF encapsulate handover request");
7143 return NULL;
7144 }
7145 ret = tmp;
7146 }
7147
7148 return ret;
7149}
7150
7151
7152static int wpas_p2p_cli_freq(struct wpa_supplicant *wpa_s,
7153 struct wpa_ssid **ssid, u8 *go_dev_addr)
7154{
7155 struct wpa_supplicant *iface;
7156
7157 if (go_dev_addr)
7158 os_memset(go_dev_addr, 0, ETH_ALEN);
7159 if (ssid)
7160 *ssid = NULL;
7161 for (iface = wpa_s->global->ifaces; iface; iface = iface->next) {
7162 if (iface->wpa_state < WPA_ASSOCIATING ||
7163 iface->current_ssid == NULL || iface->assoc_freq == 0 ||
7164 !iface->current_ssid->p2p_group ||
7165 iface->current_ssid->mode != WPAS_MODE_INFRA)
7166 continue;
7167 if (ssid)
7168 *ssid = iface->current_ssid;
7169 if (go_dev_addr)
7170 os_memcpy(go_dev_addr, iface->go_dev_addr, ETH_ALEN);
7171 return iface->assoc_freq;
7172 }
7173 return 0;
7174}
7175
7176
7177struct wpabuf * wpas_p2p_nfc_handover_req(struct wpa_supplicant *wpa_s,
7178 int ndef)
7179{
7180 struct wpabuf *wsc, *p2p;
7181 struct wpa_ssid *ssid;
7182 u8 go_dev_addr[ETH_ALEN];
7183 int cli_freq = wpas_p2p_cli_freq(wpa_s, &ssid, go_dev_addr);
7184
7185 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL) {
7186 wpa_printf(MSG_DEBUG, "P2P: P2P disabled - cannot build handover request");
7187 return NULL;
7188 }
7189
7190 if (wpa_s->conf->wps_nfc_dh_pubkey == NULL &&
7191 wps_nfc_gen_dh(&wpa_s->conf->wps_nfc_dh_pubkey,
7192 &wpa_s->conf->wps_nfc_dh_privkey) < 0) {
7193 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: No DH key available for handover request");
7194 return NULL;
7195 }
7196
7197 if (cli_freq == 0) {
7198 wsc = wps_build_nfc_handover_req_p2p(
7199 wpa_s->parent->wps, wpa_s->conf->wps_nfc_dh_pubkey);
7200 } else
7201 wsc = NULL;
7202 p2p = p2p_build_nfc_handover_req(wpa_s->global->p2p, cli_freq,
7203 go_dev_addr, ssid ? ssid->ssid : NULL,
7204 ssid ? ssid->ssid_len : 0);
7205
7206 return wpas_p2p_nfc_handover(ndef, wsc, p2p);
7207}
7208
7209
7210struct wpabuf * wpas_p2p_nfc_handover_sel(struct wpa_supplicant *wpa_s,
7211 int ndef, int tag)
7212{
7213 struct wpabuf *wsc, *p2p;
7214 struct wpa_ssid *ssid;
7215 u8 go_dev_addr[ETH_ALEN];
7216 int cli_freq = wpas_p2p_cli_freq(wpa_s, &ssid, go_dev_addr);
7217
7218 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
7219 return NULL;
7220
7221 if (!tag && wpa_s->conf->wps_nfc_dh_pubkey == NULL &&
7222 wps_nfc_gen_dh(&wpa_s->conf->wps_nfc_dh_pubkey,
7223 &wpa_s->conf->wps_nfc_dh_privkey) < 0)
7224 return NULL;
7225
7226 if (cli_freq == 0) {
7227 wsc = wps_build_nfc_handover_sel_p2p(
7228 wpa_s->parent->wps,
7229 tag ? wpa_s->conf->wps_nfc_dev_pw_id :
7230 DEV_PW_NFC_CONNECTION_HANDOVER,
7231 wpa_s->conf->wps_nfc_dh_pubkey,
7232 tag ? wpa_s->conf->wps_nfc_dev_pw : NULL);
7233 } else
7234 wsc = NULL;
7235 p2p = p2p_build_nfc_handover_sel(wpa_s->global->p2p, cli_freq,
7236 go_dev_addr, ssid ? ssid->ssid : NULL,
7237 ssid ? ssid->ssid_len : 0);
7238
7239 return wpas_p2p_nfc_handover(ndef, wsc, p2p);
7240}
7241
7242
7243static int wpas_p2p_nfc_join_group(struct wpa_supplicant *wpa_s,
7244 struct p2p_nfc_params *params)
7245{
7246 wpa_printf(MSG_DEBUG, "P2P: Initiate join-group based on NFC "
7247 "connection handover (freq=%d)",
7248 params->go_freq);
7249
7250 if (params->go_freq && params->go_ssid_len) {
7251 wpa_s->p2p_wps_method = WPS_NFC;
7252 wpa_s->pending_join_wps_method = WPS_NFC;
7253 os_memset(wpa_s->pending_join_iface_addr, 0, ETH_ALEN);
7254 os_memcpy(wpa_s->pending_join_dev_addr, params->go_dev_addr,
7255 ETH_ALEN);
7256 return wpas_p2p_join_start(wpa_s, params->go_freq,
7257 params->go_ssid,
7258 params->go_ssid_len);
7259 }
7260
7261 return wpas_p2p_connect(wpa_s, params->peer->p2p_device_addr, NULL,
7262 WPS_NFC, 0, 0, 1, 0, wpa_s->conf->p2p_go_intent,
7263 params->go_freq, -1, 0, 1, 1);
7264}
7265
7266
7267static int wpas_p2p_nfc_auth_join(struct wpa_supplicant *wpa_s,
7268 struct p2p_nfc_params *params, int tag)
7269{
7270 int res, persistent;
7271 struct wpa_ssid *ssid;
7272
7273 wpa_printf(MSG_DEBUG, "P2P: Authorize join-group based on NFC "
7274 "connection handover");
7275 for (wpa_s = wpa_s->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
7276 ssid = wpa_s->current_ssid;
7277 if (ssid == NULL)
7278 continue;
7279 if (ssid->mode != WPAS_MODE_P2P_GO)
7280 continue;
7281 if (wpa_s->ap_iface == NULL)
7282 continue;
7283 break;
7284 }
7285 if (wpa_s == NULL) {
7286 wpa_printf(MSG_DEBUG, "P2P: Could not find GO interface");
7287 return -1;
7288 }
7289
7290 if (wpa_s->parent->p2p_oob_dev_pw_id !=
7291 DEV_PW_NFC_CONNECTION_HANDOVER &&
7292 !wpa_s->parent->p2p_oob_dev_pw) {
7293 wpa_printf(MSG_DEBUG, "P2P: No NFC Dev Pw known");
7294 return -1;
7295 }
7296 res = wpas_ap_wps_add_nfc_pw(
7297 wpa_s, wpa_s->parent->p2p_oob_dev_pw_id,
7298 wpa_s->parent->p2p_oob_dev_pw,
7299 wpa_s->parent->p2p_peer_oob_pk_hash_known ?
7300 wpa_s->parent->p2p_peer_oob_pubkey_hash : NULL);
7301 if (res)
7302 return res;
7303
7304 if (!tag) {
7305 wpa_printf(MSG_DEBUG, "P2P: Negotiated handover - wait for peer to join without invitation");
7306 return 0;
7307 }
7308
7309 if (!params->peer ||
7310 !(params->peer->dev_capab & P2P_DEV_CAPAB_INVITATION_PROCEDURE))
7311 return 0;
7312
7313 wpa_printf(MSG_DEBUG, "P2P: Static handover - invite peer " MACSTR
7314 " to join", MAC2STR(params->peer->p2p_device_addr));
7315
7316 wpa_s->global->p2p_invite_group = wpa_s;
7317 persistent = ssid->p2p_persistent_group &&
7318 wpas_p2p_get_persistent(wpa_s->parent,
7319 params->peer->p2p_device_addr,
7320 ssid->ssid, ssid->ssid_len);
7321 wpa_s->parent->pending_invite_ssid_id = -1;
7322
7323 return p2p_invite(wpa_s->global->p2p, params->peer->p2p_device_addr,
7324 P2P_INVITE_ROLE_ACTIVE_GO, wpa_s->own_addr,
7325 ssid->ssid, ssid->ssid_len, ssid->frequency,
7326 wpa_s->global->p2p_dev_addr, persistent, 0,
7327 wpa_s->parent->p2p_oob_dev_pw_id);
7328}
7329
7330
7331static int wpas_p2p_nfc_init_go_neg(struct wpa_supplicant *wpa_s,
7332 struct p2p_nfc_params *params,
7333 int forced_freq)
7334{
7335 wpa_printf(MSG_DEBUG, "P2P: Initiate GO Negotiation based on NFC "
7336 "connection handover");
7337 return wpas_p2p_connect(wpa_s, params->peer->p2p_device_addr, NULL,
7338 WPS_NFC, 0, 0, 0, 0, wpa_s->conf->p2p_go_intent,
7339 forced_freq, -1, 0, 1, 1);
7340}
7341
7342
7343static int wpas_p2p_nfc_resp_go_neg(struct wpa_supplicant *wpa_s,
7344 struct p2p_nfc_params *params,
7345 int forced_freq)
7346{
7347 int res;
7348
7349 wpa_printf(MSG_DEBUG, "P2P: Authorize GO Negotiation based on NFC "
7350 "connection handover");
7351 res = wpas_p2p_connect(wpa_s, params->peer->p2p_device_addr, NULL,
7352 WPS_NFC, 0, 0, 0, 1, wpa_s->conf->p2p_go_intent,
7353 forced_freq, -1, 0, 1, 1);
7354 if (res)
7355 return res;
7356
7357 res = wpas_p2p_listen(wpa_s, 60);
7358 if (res) {
7359 p2p_unauthorize(wpa_s->global->p2p,
7360 params->peer->p2p_device_addr);
7361 }
7362
7363 return res;
7364}
7365
7366
7367static int wpas_p2p_nfc_connection_handover(struct wpa_supplicant *wpa_s,
7368 const struct wpabuf *data,
7369 int sel, int tag, int forced_freq)
7370{
7371 const u8 *pos, *end;
7372 u16 len, id;
7373 struct p2p_nfc_params params;
7374 int res;
7375
7376 os_memset(&params, 0, sizeof(params));
7377 params.sel = sel;
7378
7379 wpa_hexdump_buf(MSG_DEBUG, "P2P: Received NFC tag payload", data);
7380
7381 pos = wpabuf_head(data);
7382 end = pos + wpabuf_len(data);
7383
7384 if (end - pos < 2) {
7385 wpa_printf(MSG_DEBUG, "P2P: Not enough data for Length of WSC "
7386 "attributes");
7387 return -1;
7388 }
7389 len = WPA_GET_BE16(pos);
7390 pos += 2;
7391 if (pos + len > end) {
7392 wpa_printf(MSG_DEBUG, "P2P: Not enough data for WSC "
7393 "attributes");
7394 return -1;
7395 }
7396 params.wsc_attr = pos;
7397 params.wsc_len = len;
7398 pos += len;
7399
7400 if (end - pos < 2) {
7401 wpa_printf(MSG_DEBUG, "P2P: Not enough data for Length of P2P "
7402 "attributes");
7403 return -1;
7404 }
7405 len = WPA_GET_BE16(pos);
7406 pos += 2;
7407 if (pos + len > end) {
7408 wpa_printf(MSG_DEBUG, "P2P: Not enough data for P2P "
7409 "attributes");
7410 return -1;
7411 }
7412 params.p2p_attr = pos;
7413 params.p2p_len = len;
7414 pos += len;
7415
7416 wpa_hexdump(MSG_DEBUG, "P2P: WSC attributes",
7417 params.wsc_attr, params.wsc_len);
7418 wpa_hexdump(MSG_DEBUG, "P2P: P2P attributes",
7419 params.p2p_attr, params.p2p_len);
7420 if (pos < end) {
7421 wpa_hexdump(MSG_DEBUG,
7422 "P2P: Ignored extra data after P2P attributes",
7423 pos, end - pos);
7424 }
7425
7426 res = p2p_process_nfc_connection_handover(wpa_s->global->p2p, &params);
7427 if (res)
7428 return res;
7429
7430 if (params.next_step == NO_ACTION)
7431 return 0;
7432
7433 if (params.next_step == BOTH_GO) {
7434 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_NFC_BOTH_GO "peer=" MACSTR,
7435 MAC2STR(params.peer->p2p_device_addr));
7436 return 0;
7437 }
7438
7439 if (params.next_step == PEER_CLIENT) {
7440 if (!is_zero_ether_addr(params.go_dev_addr)) {
7441 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_NFC_PEER_CLIENT
7442 "peer=" MACSTR " freq=%d go_dev_addr=" MACSTR
7443 " ssid=\"%s\"",
7444 MAC2STR(params.peer->p2p_device_addr),
7445 params.go_freq,
7446 MAC2STR(params.go_dev_addr),
7447 wpa_ssid_txt(params.go_ssid,
7448 params.go_ssid_len));
7449 } else {
7450 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_NFC_PEER_CLIENT
7451 "peer=" MACSTR " freq=%d",
7452 MAC2STR(params.peer->p2p_device_addr),
7453 params.go_freq);
7454 }
7455 return 0;
7456 }
7457
7458 if (wpas_p2p_cli_freq(wpa_s, NULL, NULL)) {
7459 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_NFC_WHILE_CLIENT "peer="
7460 MACSTR, MAC2STR(params.peer->p2p_device_addr));
7461 return 0;
7462 }
7463
7464 wpabuf_free(wpa_s->p2p_oob_dev_pw);
7465 wpa_s->p2p_oob_dev_pw = NULL;
7466
7467 if (params.oob_dev_pw_len < WPS_OOB_PUBKEY_HASH_LEN + 2) {
7468 wpa_printf(MSG_DEBUG, "P2P: No peer OOB Dev Pw "
7469 "received");
7470 return -1;
7471 }
7472
7473 id = WPA_GET_BE16(params.oob_dev_pw + WPS_OOB_PUBKEY_HASH_LEN);
7474 wpa_printf(MSG_DEBUG, "P2P: Peer OOB Dev Pw %u", id);
7475 wpa_hexdump(MSG_DEBUG, "P2P: Peer OOB Public Key hash",
7476 params.oob_dev_pw, WPS_OOB_PUBKEY_HASH_LEN);
7477 os_memcpy(wpa_s->p2p_peer_oob_pubkey_hash,
7478 params.oob_dev_pw, WPS_OOB_PUBKEY_HASH_LEN);
7479 wpa_s->p2p_peer_oob_pk_hash_known = 1;
7480
7481 if (tag) {
7482 if (id < 0x10) {
7483 wpa_printf(MSG_DEBUG, "P2P: Static handover - invalid "
7484 "peer OOB Device Password Id %u", id);
7485 return -1;
7486 }
7487 wpa_printf(MSG_DEBUG, "P2P: Static handover - use peer OOB "
7488 "Device Password Id %u", id);
7489 wpa_hexdump_key(MSG_DEBUG, "P2P: Peer OOB Device Password",
7490 params.oob_dev_pw + WPS_OOB_PUBKEY_HASH_LEN + 2,
7491 params.oob_dev_pw_len -
7492 WPS_OOB_PUBKEY_HASH_LEN - 2);
7493 wpa_s->p2p_oob_dev_pw_id = id;
7494 wpa_s->p2p_oob_dev_pw = wpabuf_alloc_copy(
7495 params.oob_dev_pw + WPS_OOB_PUBKEY_HASH_LEN + 2,
7496 params.oob_dev_pw_len -
7497 WPS_OOB_PUBKEY_HASH_LEN - 2);
7498 if (wpa_s->p2p_oob_dev_pw == NULL)
7499 return -1;
7500
7501 if (wpa_s->conf->wps_nfc_dh_pubkey == NULL &&
7502 wps_nfc_gen_dh(&wpa_s->conf->wps_nfc_dh_pubkey,
7503 &wpa_s->conf->wps_nfc_dh_privkey) < 0)
7504 return -1;
7505 } else {
7506 wpa_printf(MSG_DEBUG, "P2P: Using abbreviated WPS handshake "
7507 "without Device Password");
7508 wpa_s->p2p_oob_dev_pw_id = DEV_PW_NFC_CONNECTION_HANDOVER;
7509 }
7510
7511 switch (params.next_step) {
7512 case NO_ACTION:
7513 case BOTH_GO:
7514 case PEER_CLIENT:
7515 /* already covered above */
7516 return 0;
7517 case JOIN_GROUP:
7518 return wpas_p2p_nfc_join_group(wpa_s, &params);
7519 case AUTH_JOIN:
7520 return wpas_p2p_nfc_auth_join(wpa_s, &params, tag);
7521 case INIT_GO_NEG:
7522 return wpas_p2p_nfc_init_go_neg(wpa_s, &params, forced_freq);
7523 case RESP_GO_NEG:
7524 /* TODO: use own OOB Dev Pw */
7525 return wpas_p2p_nfc_resp_go_neg(wpa_s, &params, forced_freq);
7526 }
7527
7528 return -1;
7529}
7530
7531
7532int wpas_p2p_nfc_tag_process(struct wpa_supplicant *wpa_s,
7533 const struct wpabuf *data, int forced_freq)
7534{
7535 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
7536 return -1;
7537
7538 return wpas_p2p_nfc_connection_handover(wpa_s, data, 1, 1, forced_freq);
7539}
7540
7541
7542int wpas_p2p_nfc_report_handover(struct wpa_supplicant *wpa_s, int init,
7543 const struct wpabuf *req,
7544 const struct wpabuf *sel, int forced_freq)
7545{
7546 struct wpabuf *tmp;
7547 int ret;
7548
7549 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
7550 return -1;
7551
7552 wpa_printf(MSG_DEBUG, "NFC: P2P connection handover reported");
7553
7554 wpa_hexdump_ascii(MSG_DEBUG, "NFC: Req",
7555 wpabuf_head(req), wpabuf_len(req));
7556 wpa_hexdump_ascii(MSG_DEBUG, "NFC: Sel",
7557 wpabuf_head(sel), wpabuf_len(sel));
7558 if (forced_freq)
7559 wpa_printf(MSG_DEBUG, "NFC: Forced freq %d", forced_freq);
7560 tmp = ndef_parse_p2p(init ? sel : req);
7561 if (tmp == NULL) {
7562 wpa_printf(MSG_DEBUG, "P2P: Could not parse NDEF");
7563 return -1;
7564 }
7565
7566 ret = wpas_p2p_nfc_connection_handover(wpa_s, tmp, init, 0,
7567 forced_freq);
7568 wpabuf_free(tmp);
7569
7570 return ret;
7571}
7572
7573
7574int wpas_p2p_nfc_tag_enabled(struct wpa_supplicant *wpa_s, int enabled)
7575{
7576 const u8 *if_addr;
7577 int go_intent = wpa_s->conf->p2p_go_intent;
7578 struct wpa_supplicant *iface;
7579
7580 if (wpa_s->global->p2p == NULL)
7581 return -1;
7582
7583 if (!enabled) {
7584 wpa_printf(MSG_DEBUG, "P2P: Disable use of own NFC Tag");
7585 for (iface = wpa_s->global->ifaces; iface; iface = iface->next)
7586 {
7587 if (!iface->ap_iface)
7588 continue;
7589 hostapd_wps_nfc_token_disable(iface->ap_iface->bss[0]);
7590 }
7591 p2p_set_authorized_oob_dev_pw_id(wpa_s->global->p2p, 0,
7592 0, NULL);
7593 if (wpa_s->p2p_nfc_tag_enabled)
7594 wpas_p2p_remove_pending_group_interface(wpa_s);
7595 wpa_s->p2p_nfc_tag_enabled = 0;
7596 return 0;
7597 }
7598
7599 if (wpa_s->global->p2p_disabled)
7600 return -1;
7601
7602 if (wpa_s->conf->wps_nfc_dh_pubkey == NULL ||
7603 wpa_s->conf->wps_nfc_dh_privkey == NULL ||
7604 wpa_s->conf->wps_nfc_dev_pw == NULL ||
7605 wpa_s->conf->wps_nfc_dev_pw_id < 0x10) {
7606 wpa_printf(MSG_DEBUG, "P2P: NFC password token not configured "
7607 "to allow static handover cases");
7608 return -1;
7609 }
7610
7611 wpa_printf(MSG_DEBUG, "P2P: Enable use of own NFC Tag");
7612
7613 wpa_s->p2p_oob_dev_pw_id = wpa_s->conf->wps_nfc_dev_pw_id;
7614 wpabuf_free(wpa_s->p2p_oob_dev_pw);
7615 wpa_s->p2p_oob_dev_pw = wpabuf_dup(wpa_s->conf->wps_nfc_dev_pw);
7616 if (wpa_s->p2p_oob_dev_pw == NULL)
7617 return -1;
7618 wpa_s->p2p_peer_oob_pk_hash_known = 0;
7619
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -07007620 if (wpa_s->p2p_group_interface == P2P_GROUP_INTERFACE_GO ||
7621 wpa_s->p2p_group_interface == P2P_GROUP_INTERFACE_CLIENT) {
7622 /*
7623 * P2P Group Interface present and the command came on group
7624 * interface, so enable the token for the current interface.
7625 */
7626 wpa_s->create_p2p_iface = 0;
7627 } else {
7628 wpa_s->create_p2p_iface = wpas_p2p_create_iface(wpa_s);
7629 }
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08007630
7631 if (wpa_s->create_p2p_iface) {
7632 enum wpa_driver_if_type iftype;
7633 /* Prepare to add a new interface for the group */
7634 iftype = WPA_IF_P2P_GROUP;
7635 if (go_intent == 15)
7636 iftype = WPA_IF_P2P_GO;
7637 if (wpas_p2p_add_group_interface(wpa_s, iftype) < 0) {
7638 wpa_printf(MSG_ERROR, "P2P: Failed to allocate a new "
7639 "interface for the group");
7640 return -1;
7641 }
7642
7643 if_addr = wpa_s->pending_interface_addr;
7644 } else
7645 if_addr = wpa_s->own_addr;
7646
7647 wpa_s->p2p_nfc_tag_enabled = enabled;
7648
7649 for (iface = wpa_s->global->ifaces; iface; iface = iface->next) {
7650 struct hostapd_data *hapd;
7651 if (iface->ap_iface == NULL)
7652 continue;
7653 hapd = iface->ap_iface->bss[0];
7654 wpabuf_free(hapd->conf->wps_nfc_dh_pubkey);
7655 hapd->conf->wps_nfc_dh_pubkey =
7656 wpabuf_dup(wpa_s->conf->wps_nfc_dh_pubkey);
7657 wpabuf_free(hapd->conf->wps_nfc_dh_privkey);
7658 hapd->conf->wps_nfc_dh_privkey =
7659 wpabuf_dup(wpa_s->conf->wps_nfc_dh_privkey);
7660 wpabuf_free(hapd->conf->wps_nfc_dev_pw);
7661 hapd->conf->wps_nfc_dev_pw =
7662 wpabuf_dup(wpa_s->conf->wps_nfc_dev_pw);
7663 hapd->conf->wps_nfc_dev_pw_id = wpa_s->conf->wps_nfc_dev_pw_id;
7664
7665 if (hostapd_wps_nfc_token_enable(iface->ap_iface->bss[0]) < 0) {
7666 wpa_dbg(iface, MSG_DEBUG,
7667 "P2P: Failed to enable NFC Tag for GO");
7668 }
7669 }
7670 p2p_set_authorized_oob_dev_pw_id(
7671 wpa_s->global->p2p, wpa_s->conf->wps_nfc_dev_pw_id, go_intent,
7672 if_addr);
7673
7674 return 0;
7675}
7676
7677#endif /* CONFIG_WPS_NFC */