blob: fa824914c934c37332b032be9ad3c59cec909792 [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
60 * WPS provisioning step.
61 */
62#define P2P_MAX_INITIAL_CONN_WAIT 10
63#endif /* P2P_MAX_INITIAL_CONN_WAIT */
64
Dmitry Shmidt92c368d2013-08-29 12:37:21 -070065#ifndef P2P_MAX_INITIAL_CONN_WAIT_GO
66/*
67 * How many seconds to wait for initial 4-way handshake to get completed after
68 * WPS provisioning step on the GO. This controls the extra time the P2P
69 * operation is considered to be in progress (e.g., to delay other scans) after
70 * WPS provisioning has been completed on the GO during group formation.
71 */
72#define P2P_MAX_INITIAL_CONN_WAIT_GO 10
73#endif /* P2P_MAX_INITIAL_CONN_WAIT_GO */
74
Dmitry Shmidt56052862013-10-04 10:23:25 -070075#ifndef P2P_MAX_INITIAL_CONN_WAIT_GO_REINVOKE
76/*
77 * How many seconds to wait for initial 4-way handshake to get completed after
78 * re-invocation of a persistent group on the GO when the client is expected
79 * to connect automatically (no user interaction).
80 */
81#define P2P_MAX_INITIAL_CONN_WAIT_GO_REINVOKE 15
82#endif /* P2P_MAX_INITIAL_CONN_WAIT_GO_REINVOKE */
83
Dmitry Shmidt61d9df32012-08-29 16:22:06 -070084#ifndef P2P_CONCURRENT_SEARCH_DELAY
Dmitry Shmidt61d9df32012-08-29 16:22:06 -070085#define P2P_CONCURRENT_SEARCH_DELAY 500
Dmitry Shmidt61d9df32012-08-29 16:22:06 -070086#endif /* P2P_CONCURRENT_SEARCH_DELAY */
87
Dmitry Shmidt34af3062013-07-11 10:46:32 -070088#define P2P_MGMT_DEVICE_PREFIX "p2p-dev-"
89
Dmitry Shmidt61d9df32012-08-29 16:22:06 -070090enum p2p_group_removal_reason {
91 P2P_GROUP_REMOVAL_UNKNOWN,
92 P2P_GROUP_REMOVAL_SILENT,
93 P2P_GROUP_REMOVAL_FORMATION_FAILED,
94 P2P_GROUP_REMOVAL_REQUESTED,
95 P2P_GROUP_REMOVAL_IDLE_TIMEOUT,
96 P2P_GROUP_REMOVAL_UNAVAILABLE,
97 P2P_GROUP_REMOVAL_GO_ENDING_SESSION,
Dmitry Shmidt04f534e2013-12-09 15:50:16 -080098 P2P_GROUP_REMOVAL_PSK_FAILURE,
99 P2P_GROUP_REMOVAL_FREQ_CONFLICT
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700100};
101
Jouni Malinendc7b7132012-09-14 12:53:47 -0700102
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700103static void wpas_p2p_long_listen_timeout(void *eloop_ctx, void *timeout_ctx);
104static struct wpa_supplicant *
105wpas_p2p_get_group_iface(struct wpa_supplicant *wpa_s, int addr_allocated,
106 int go);
Dmitry Shmidtcf32e602014-01-28 10:57:39 -0800107static int wpas_p2p_join_start(struct wpa_supplicant *wpa_s, int freq,
108 const u8 *ssid, size_t ssid_len);
Dmitry Shmidt344abd32014-01-14 13:17:00 -0800109static void wpas_p2p_join_scan_req(struct wpa_supplicant *wpa_s, int freq,
110 const u8 *ssid, size_t ssid_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700111static void wpas_p2p_join_scan(void *eloop_ctx, void *timeout_ctx);
112static int wpas_p2p_join(struct wpa_supplicant *wpa_s, const u8 *iface_addr,
Dmitry Shmidt04949592012-07-19 12:16:46 -0700113 const u8 *dev_addr, enum p2p_wps_method wps_method,
Dmitry Shmidtcf32e602014-01-28 10:57:39 -0800114 int auto_join, int freq,
115 const u8 *ssid, size_t ssid_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700116static int wpas_p2p_create_iface(struct wpa_supplicant *wpa_s);
117static void wpas_p2p_cross_connect_setup(struct wpa_supplicant *wpa_s);
118static void wpas_p2p_group_idle_timeout(void *eloop_ctx, void *timeout_ctx);
119static void wpas_p2p_set_group_idle_timeout(struct wpa_supplicant *wpa_s);
Dmitry Shmidtf8623282013-02-20 14:34:59 -0800120static void wpas_p2p_group_formation_timeout(void *eloop_ctx,
121 void *timeout_ctx);
Dmitry Shmidt04f534e2013-12-09 15:50:16 -0800122static void wpas_p2p_group_freq_conflict(void *eloop_ctx, void *timeout_ctx);
Dmitry Shmidt04949592012-07-19 12:16:46 -0700123static void wpas_p2p_fallback_to_go_neg(struct wpa_supplicant *wpa_s,
124 int group_added);
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800125static int wpas_p2p_stop_find_oper(struct wpa_supplicant *wpa_s);
Dmitry Shmidtbd14a572014-02-18 10:33:49 -0800126static void wpas_stop_listen(void *ctx);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700127
128
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -0700129/*
130 * Get the number of concurrent channels that the HW can operate, but that are
131 * currently not in use by any of the wpa_supplicant interfaces.
132 */
133static int wpas_p2p_num_unused_channels(struct wpa_supplicant *wpa_s)
134{
135 int *freqs;
Dmitry Shmidtb96dad42013-11-05 10:07:29 -0800136 int num, unused;
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -0700137
138 freqs = os_calloc(wpa_s->num_multichan_concurrent, sizeof(int));
139 if (!freqs)
140 return -1;
141
142 num = get_shared_radio_freqs(wpa_s, freqs,
143 wpa_s->num_multichan_concurrent);
144 os_free(freqs);
145
Dmitry Shmidtb96dad42013-11-05 10:07:29 -0800146 unused = wpa_s->num_multichan_concurrent - num;
147 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: num_unused_channels: %d", unused);
148 return unused;
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -0700149}
150
151
152/*
153 * Get the frequencies that are currently in use by one or more of the virtual
154 * interfaces, and that are also valid for P2P operation.
155 */
156static int wpas_p2p_valid_oper_freqs(struct wpa_supplicant *wpa_s,
157 int *p2p_freqs, unsigned int len)
158{
159 int *freqs;
160 unsigned int num, i, j;
161
162 freqs = os_calloc(wpa_s->num_multichan_concurrent, sizeof(int));
163 if (!freqs)
164 return -1;
165
166 num = get_shared_radio_freqs(wpa_s, freqs,
167 wpa_s->num_multichan_concurrent);
168
169 os_memset(p2p_freqs, 0, sizeof(int) * len);
170
171 for (i = 0, j = 0; i < num && j < len; i++) {
172 if (p2p_supported_freq(wpa_s->global->p2p, freqs[i]))
173 p2p_freqs[j++] = freqs[i];
174 }
175
176 os_free(freqs);
177
Dmitry Shmidtb96dad42013-11-05 10:07:29 -0800178 dump_freq_array(wpa_s, "valid for P2P", p2p_freqs, j);
179
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -0700180 return j;
181}
182
183
Dmitry Shmidt700a1372013-03-15 14:14:44 -0700184static void wpas_p2p_set_own_freq_preference(struct wpa_supplicant *wpa_s,
185 int freq)
186{
187 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
188 return;
Dmitry Shmidta0d265f2013-11-19 13:13:41 -0800189 if (wpa_s->parent->conf->p2p_ignore_shared_freq &&
190 freq > 0 && wpa_s->num_multichan_concurrent > 1 &&
191 wpas_p2p_num_unused_channels(wpa_s) > 0) {
192 wpa_printf(MSG_DEBUG, "P2P: Ignore own channel preference %d MHz due to p2p_ignore_shared_freq=1 configuration",
193 freq);
Dmitry Shmidt700a1372013-03-15 14:14:44 -0700194 freq = 0;
Dmitry Shmidta0d265f2013-11-19 13:13:41 -0800195 }
Dmitry Shmidt700a1372013-03-15 14:14:44 -0700196 p2p_set_own_freq_preference(wpa_s->global->p2p, freq);
197}
198
199
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700200static void wpas_p2p_scan_res_handler(struct wpa_supplicant *wpa_s,
201 struct wpa_scan_results *scan_res)
202{
203 size_t i;
204
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800205 if (wpa_s->p2p_scan_work) {
206 struct wpa_radio_work *work = wpa_s->p2p_scan_work;
207 wpa_s->p2p_scan_work = NULL;
208 radio_work_done(work);
209 }
210
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700211 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
212 return;
213
214 wpa_printf(MSG_DEBUG, "P2P: Scan results received (%d BSS)",
215 (int) scan_res->num);
216
217 for (i = 0; i < scan_res->num; i++) {
218 struct wpa_scan_res *bss = scan_res->res[i];
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800219 struct os_reltime time_tmp_age, entry_ts;
Dmitry Shmidt96571392013-10-14 12:54:46 -0700220 const u8 *ies;
221 size_t ies_len;
222
Dmitry Shmidtf8623282013-02-20 14:34:59 -0800223 time_tmp_age.sec = bss->age / 1000;
224 time_tmp_age.usec = (bss->age % 1000) * 1000;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800225 os_reltime_sub(&scan_res->fetch_time, &time_tmp_age, &entry_ts);
Dmitry Shmidt96571392013-10-14 12:54:46 -0700226
227 ies = (const u8 *) (bss + 1);
228 ies_len = bss->ie_len;
229 if (bss->beacon_ie_len > 0 &&
230 !wpa_scan_get_vendor_ie(bss, P2P_IE_VENDOR_TYPE) &&
231 wpa_scan_get_vendor_ie_beacon(bss, P2P_IE_VENDOR_TYPE)) {
232 wpa_printf(MSG_DEBUG, "P2P: Use P2P IE(s) from Beacon frame since no P2P IE(s) in Probe Response frames received for "
233 MACSTR, MAC2STR(bss->bssid));
234 ies = ies + ies_len;
235 ies_len = bss->beacon_ie_len;
236 }
237
238
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700239 if (p2p_scan_res_handler(wpa_s->global->p2p, bss->bssid,
Dmitry Shmidtf8623282013-02-20 14:34:59 -0800240 bss->freq, &entry_ts, bss->level,
Dmitry Shmidt96571392013-10-14 12:54:46 -0700241 ies, ies_len) > 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700242 break;
243 }
244
245 p2p_scan_res_handled(wpa_s->global->p2p);
246}
247
248
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800249static void wpas_p2p_trigger_scan_cb(struct wpa_radio_work *work, int deinit)
250{
251 struct wpa_supplicant *wpa_s = work->wpa_s;
252 struct wpa_driver_scan_params *params = work->ctx;
253 int ret;
254
255 if (deinit) {
Dmitry Shmidtbd14a572014-02-18 10:33:49 -0800256 if (!work->started) {
257 wpa_scan_free_params(params);
258 return;
259 }
260
261 wpa_s->p2p_scan_work = NULL;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800262 return;
263 }
264
265 ret = wpa_drv_scan(wpa_s, params);
266 wpa_scan_free_params(params);
267 work->ctx = NULL;
268 if (ret) {
269 radio_work_done(work);
270 return;
271 }
272
273 os_get_reltime(&wpa_s->scan_trigger_time);
274 wpa_s->scan_res_handler = wpas_p2p_scan_res_handler;
275 wpa_s->own_scan_requested = 1;
276 wpa_s->p2p_scan_work = work;
277}
278
279
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700280static int wpas_p2p_scan(void *ctx, enum p2p_scan_type type, int freq,
281 unsigned int num_req_dev_types,
Dmitry Shmidt04949592012-07-19 12:16:46 -0700282 const u8 *req_dev_types, const u8 *dev_id, u16 pw_id)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700283{
284 struct wpa_supplicant *wpa_s = ctx;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800285 struct wpa_driver_scan_params *params = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700286 struct wpabuf *wps_ie, *ies;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800287 size_t ielen;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800288 u8 *n;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700289
290 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
291 return -1;
292
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800293 if (wpa_s->p2p_scan_work) {
294 wpa_dbg(wpa_s, MSG_INFO, "P2P: Reject scan trigger since one is already pending");
295 return -1;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700296 }
297
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800298 params = os_zalloc(sizeof(*params));
299 if (params == NULL)
300 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700301
302 /* P2P Wildcard SSID */
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800303 params->num_ssids = 1;
304 n = os_malloc(P2P_WILDCARD_SSID_LEN);
305 if (n == NULL)
306 goto fail;
307 os_memcpy(n, P2P_WILDCARD_SSID, P2P_WILDCARD_SSID_LEN);
308 params->ssids[0].ssid = n;
309 params->ssids[0].ssid_len = P2P_WILDCARD_SSID_LEN;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700310
311 wpa_s->wps->dev.p2p = 1;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700312 wps_ie = wps_build_probe_req_ie(pw_id, &wpa_s->wps->dev,
313 wpa_s->wps->uuid, WPS_REQ_ENROLLEE,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700314 num_req_dev_types, req_dev_types);
315 if (wps_ie == NULL)
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800316 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700317
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800318 ielen = p2p_scan_ie_buf_len(wpa_s->global->p2p);
319 ies = wpabuf_alloc(wpabuf_len(wps_ie) + ielen);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700320 if (ies == NULL) {
321 wpabuf_free(wps_ie);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800322 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700323 }
324 wpabuf_put_buf(ies, wps_ie);
325 wpabuf_free(wps_ie);
326
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -0800327 p2p_scan_ie(wpa_s->global->p2p, ies, dev_id);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700328
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800329 params->p2p_probe = 1;
330 n = os_malloc(wpabuf_len(ies));
331 if (n == NULL) {
332 wpabuf_free(ies);
333 goto fail;
334 }
335 os_memcpy(n, wpabuf_head(ies), wpabuf_len(ies));
336 params->extra_ies = n;
337 params->extra_ies_len = wpabuf_len(ies);
338 wpabuf_free(ies);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700339
340 switch (type) {
341 case P2P_SCAN_SOCIAL:
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800342 params->freqs = os_malloc(4 * sizeof(int));
343 if (params->freqs == NULL)
344 goto fail;
345 params->freqs[0] = 2412;
346 params->freqs[1] = 2437;
347 params->freqs[2] = 2462;
348 params->freqs[3] = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700349 break;
350 case P2P_SCAN_FULL:
351 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700352 case P2P_SCAN_SOCIAL_PLUS_ONE:
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800353 params->freqs = os_malloc(5 * sizeof(int));
354 if (params->freqs == NULL)
355 goto fail;
356 params->freqs[0] = 2412;
357 params->freqs[1] = 2437;
358 params->freqs[2] = 2462;
359 params->freqs[3] = freq;
360 params->freqs[4] = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700361 break;
362 }
363
Dmitry Shmidtbd14a572014-02-18 10:33:49 -0800364 radio_remove_works(wpa_s, "p2p-scan", 0);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800365 if (radio_add_work(wpa_s, 0, "p2p-scan", 0, wpas_p2p_trigger_scan_cb,
366 params) < 0)
367 goto fail;
368 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700369
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800370fail:
371 wpa_scan_free_params(params);
372 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700373}
374
375
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700376static enum wpa_driver_if_type wpas_p2p_if_type(int p2p_group_interface)
377{
378 switch (p2p_group_interface) {
379 case P2P_GROUP_INTERFACE_PENDING:
380 return WPA_IF_P2P_GROUP;
381 case P2P_GROUP_INTERFACE_GO:
382 return WPA_IF_P2P_GO;
383 case P2P_GROUP_INTERFACE_CLIENT:
384 return WPA_IF_P2P_CLIENT;
385 }
386
387 return WPA_IF_P2P_GROUP;
388}
389
390
391static struct wpa_supplicant * wpas_get_p2p_group(struct wpa_supplicant *wpa_s,
392 const u8 *ssid,
393 size_t ssid_len, int *go)
394{
395 struct wpa_ssid *s;
396
397 for (wpa_s = wpa_s->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
398 for (s = wpa_s->conf->ssid; s; s = s->next) {
399 if (s->disabled != 0 || !s->p2p_group ||
400 s->ssid_len != ssid_len ||
401 os_memcmp(ssid, s->ssid, ssid_len) != 0)
402 continue;
403 if (s->mode == WPAS_MODE_P2P_GO &&
404 s != wpa_s->current_ssid)
405 continue;
406 if (go)
407 *go = s->mode == WPAS_MODE_P2P_GO;
408 return wpa_s;
409 }
410 }
411
412 return NULL;
413}
414
415
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700416static int wpas_p2p_group_delete(struct wpa_supplicant *wpa_s,
417 enum p2p_group_removal_reason removal_reason)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700418{
419 struct wpa_ssid *ssid;
420 char *gtype;
421 const char *reason;
422
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700423 ssid = wpa_s->current_ssid;
424 if (ssid == NULL) {
425 /*
426 * The current SSID was not known, but there may still be a
Dmitry Shmidtaa532512012-09-24 10:35:31 -0700427 * pending P2P group interface waiting for provisioning or a
428 * P2P group that is trying to reconnect.
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700429 */
430 ssid = wpa_s->conf->ssid;
431 while (ssid) {
Jouni Malinen9d712832012-10-05 11:01:57 -0700432 if (ssid->p2p_group && ssid->disabled != 2)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700433 break;
434 ssid = ssid->next;
435 }
Jouni Malinen5c44edb2012-08-31 21:35:32 +0300436 if (ssid == NULL &&
437 wpa_s->p2p_group_interface == NOT_P2P_GROUP_INTERFACE)
438 {
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700439 wpa_printf(MSG_ERROR, "P2P: P2P group interface "
440 "not found");
441 return -1;
442 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700443 }
444 if (wpa_s->p2p_group_interface == P2P_GROUP_INTERFACE_GO)
445 gtype = "GO";
446 else if (wpa_s->p2p_group_interface == P2P_GROUP_INTERFACE_CLIENT ||
447 (ssid && ssid->mode == WPAS_MODE_INFRA)) {
448 wpa_s->reassociate = 0;
449 wpa_s->disconnected = 1;
450 wpa_supplicant_deauthenticate(wpa_s,
451 WLAN_REASON_DEAUTH_LEAVING);
452 gtype = "client";
453 } else
454 gtype = "GO";
455 if (wpa_s->cross_connect_in_use) {
456 wpa_s->cross_connect_in_use = 0;
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -0700457 wpa_msg_global(wpa_s->parent, MSG_INFO,
458 P2P_EVENT_CROSS_CONNECT_DISABLE "%s %s",
459 wpa_s->ifname, wpa_s->cross_connect_uplink);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700460 }
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700461 switch (removal_reason) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700462 case P2P_GROUP_REMOVAL_REQUESTED:
463 reason = " reason=REQUESTED";
464 break;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700465 case P2P_GROUP_REMOVAL_FORMATION_FAILED:
466 reason = " reason=FORMATION_FAILED";
467 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700468 case P2P_GROUP_REMOVAL_IDLE_TIMEOUT:
469 reason = " reason=IDLE";
470 break;
471 case P2P_GROUP_REMOVAL_UNAVAILABLE:
472 reason = " reason=UNAVAILABLE";
473 break;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700474 case P2P_GROUP_REMOVAL_GO_ENDING_SESSION:
475 reason = " reason=GO_ENDING_SESSION";
476 break;
Dmitry Shmidt391c59f2013-09-03 12:16:28 -0700477 case P2P_GROUP_REMOVAL_PSK_FAILURE:
478 reason = " reason=PSK_FAILURE";
479 break;
Dmitry Shmidt04f534e2013-12-09 15:50:16 -0800480 case P2P_GROUP_REMOVAL_FREQ_CONFLICT:
481 reason = " reason=FREQ_CONFLICT";
482 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700483 default:
484 reason = "";
485 break;
486 }
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700487 if (removal_reason != P2P_GROUP_REMOVAL_SILENT) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -0700488 wpa_msg_global(wpa_s->parent, MSG_INFO,
489 P2P_EVENT_GROUP_REMOVED "%s %s%s",
490 wpa_s->ifname, gtype, reason);
Dmitry Shmidt04949592012-07-19 12:16:46 -0700491 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700492
Dmitry Shmidt04f534e2013-12-09 15:50:16 -0800493 if (eloop_cancel_timeout(wpas_p2p_group_freq_conflict, wpa_s, NULL) > 0)
494 wpa_printf(MSG_DEBUG, "P2P: Cancelled P2P group freq_conflict timeout");
Dmitry Shmidt04949592012-07-19 12:16:46 -0700495 if (eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL) > 0)
496 wpa_printf(MSG_DEBUG, "P2P: Cancelled P2P group idle timeout");
Dmitry Shmidtf8623282013-02-20 14:34:59 -0800497 if (eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
Dmitry Shmidt2f023192013-03-12 12:44:17 -0700498 wpa_s->parent, NULL) > 0) {
Dmitry Shmidtf8623282013-02-20 14:34:59 -0800499 wpa_printf(MSG_DEBUG, "P2P: Cancelled P2P group formation "
500 "timeout");
Dmitry Shmidt2f023192013-03-12 12:44:17 -0700501 wpa_s->p2p_in_provisioning = 0;
502 }
Dmitry Shmidt04949592012-07-19 12:16:46 -0700503
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
725#ifndef CONFIG_NO_CONFIG_WRITE
726 if (changed && wpa_s->conf->update_config &&
727 wpa_config_write(wpa_s->confname, wpa_s->conf)) {
728 wpa_printf(MSG_DEBUG, "P2P: Failed to update configuration");
729 }
730#endif /* CONFIG_NO_CONFIG_WRITE */
Jouni Malinen75ecf522011-06-27 15:19:46 -0700731
732 return s->id;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700733}
734
735
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800736static void wpas_p2p_add_persistent_group_client(struct wpa_supplicant *wpa_s,
737 const u8 *addr)
738{
739 struct wpa_ssid *ssid, *s;
740 u8 *n;
741 size_t i;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700742 int found = 0;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800743
744 ssid = wpa_s->current_ssid;
745 if (ssid == NULL || ssid->mode != WPAS_MODE_P2P_GO ||
746 !ssid->p2p_persistent_group)
747 return;
748
749 for (s = wpa_s->parent->conf->ssid; s; s = s->next) {
750 if (s->disabled != 2 || s->mode != WPAS_MODE_P2P_GO)
751 continue;
752
753 if (s->ssid_len == ssid->ssid_len &&
754 os_memcmp(s->ssid, ssid->ssid, s->ssid_len) == 0)
755 break;
756 }
757
758 if (s == NULL)
759 return;
760
761 for (i = 0; s->p2p_client_list && i < s->num_p2p_clients; i++) {
762 if (os_memcmp(s->p2p_client_list + i * ETH_ALEN, addr,
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700763 ETH_ALEN) != 0)
764 continue;
765
766 if (i == s->num_p2p_clients - 1)
767 return; /* already the most recent entry */
768
769 /* move the entry to mark it most recent */
770 os_memmove(s->p2p_client_list + i * ETH_ALEN,
771 s->p2p_client_list + (i + 1) * ETH_ALEN,
772 (s->num_p2p_clients - i - 1) * ETH_ALEN);
773 os_memcpy(s->p2p_client_list +
774 (s->num_p2p_clients - 1) * ETH_ALEN, addr, ETH_ALEN);
775 found = 1;
776 break;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800777 }
778
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700779 if (!found && s->num_p2p_clients < P2P_MAX_STORED_CLIENTS) {
780 n = os_realloc_array(s->p2p_client_list,
781 s->num_p2p_clients + 1, ETH_ALEN);
782 if (n == NULL)
783 return;
784 os_memcpy(n + s->num_p2p_clients * ETH_ALEN, addr, ETH_ALEN);
785 s->p2p_client_list = n;
786 s->num_p2p_clients++;
787 } else if (!found) {
788 /* Not enough room for an additional entry - drop the oldest
789 * entry */
790 os_memmove(s->p2p_client_list,
791 s->p2p_client_list + ETH_ALEN,
792 (s->num_p2p_clients - 1) * ETH_ALEN);
793 os_memcpy(s->p2p_client_list +
794 (s->num_p2p_clients - 1) * ETH_ALEN,
795 addr, ETH_ALEN);
796 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800797
798#ifndef CONFIG_NO_CONFIG_WRITE
799 if (wpa_s->parent->conf->update_config &&
800 wpa_config_write(wpa_s->parent->confname, wpa_s->parent->conf))
801 wpa_printf(MSG_DEBUG, "P2P: Failed to update configuration");
802#endif /* CONFIG_NO_CONFIG_WRITE */
803}
804
805
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700806static void wpas_group_formation_completed(struct wpa_supplicant *wpa_s,
807 int success)
808{
809 struct wpa_ssid *ssid;
810 const char *ssid_txt;
811 int client;
812 int persistent;
813 u8 go_dev_addr[ETH_ALEN];
Jouni Malinen75ecf522011-06-27 15:19:46 -0700814 int network_id = -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700815
816 /*
817 * This callback is likely called for the main interface. Update wpa_s
818 * to use the group interface if a new interface was created for the
819 * group.
820 */
821 if (wpa_s->global->p2p_group_formation)
822 wpa_s = wpa_s->global->p2p_group_formation;
Dmitry Shmidt56052862013-10-04 10:23:25 -0700823 if (wpa_s->p2p_go_group_formation_completed) {
824 wpa_s->global->p2p_group_formation = NULL;
825 wpa_s->p2p_in_provisioning = 0;
826 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700827
828 if (!success) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -0700829 wpa_msg_global(wpa_s->parent, MSG_INFO,
830 P2P_EVENT_GROUP_FORMATION_FAILURE);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700831 wpas_p2p_group_delete(wpa_s,
832 P2P_GROUP_REMOVAL_FORMATION_FAILED);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700833 return;
834 }
835
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -0700836 wpa_msg_global(wpa_s->parent, MSG_INFO,
837 P2P_EVENT_GROUP_FORMATION_SUCCESS);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700838
839 ssid = wpa_s->current_ssid;
840 if (ssid && ssid->mode == WPAS_MODE_P2P_GROUP_FORMATION) {
841 ssid->mode = WPAS_MODE_P2P_GO;
842 p2p_group_notif_formation_done(wpa_s->p2p_group);
843 wpa_supplicant_ap_mac_addr_filter(wpa_s, NULL);
844 }
845
846 persistent = 0;
847 if (ssid) {
848 ssid_txt = wpa_ssid_txt(ssid->ssid, ssid->ssid_len);
849 client = ssid->mode == WPAS_MODE_INFRA;
850 if (ssid->mode == WPAS_MODE_P2P_GO) {
851 persistent = ssid->p2p_persistent_group;
Dmitry Shmidt497c1d52011-07-21 15:19:46 -0700852 os_memcpy(go_dev_addr, wpa_s->global->p2p_dev_addr,
853 ETH_ALEN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700854 } else
855 persistent = wpas_p2p_persistent_group(wpa_s,
856 go_dev_addr,
857 ssid->ssid,
858 ssid->ssid_len);
859 } else {
860 ssid_txt = "";
861 client = wpa_s->p2p_group_interface ==
862 P2P_GROUP_INTERFACE_CLIENT;
863 os_memset(go_dev_addr, 0, ETH_ALEN);
864 }
865
866 wpa_s->show_group_started = 0;
867 if (client) {
868 /*
869 * Indicate event only after successfully completed 4-way
870 * handshake, i.e., when the interface is ready for data
871 * packets.
872 */
873 wpa_s->show_group_started = 1;
874 } else if (ssid && ssid->passphrase == NULL && ssid->psk_set) {
875 char psk[65];
876 wpa_snprintf_hex(psk, sizeof(psk), ssid->psk, 32);
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -0700877 wpa_msg_global(wpa_s->parent, MSG_INFO, P2P_EVENT_GROUP_STARTED
878 "%s GO ssid=\"%s\" freq=%d psk=%s go_dev_addr="
879 MACSTR "%s",
880 wpa_s->ifname, ssid_txt, ssid->frequency, psk,
881 MAC2STR(go_dev_addr),
882 persistent ? " [PERSISTENT]" : "");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700883 wpas_p2p_cross_connect_setup(wpa_s);
884 wpas_p2p_set_group_idle_timeout(wpa_s);
885 } else {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -0700886 wpa_msg_global(wpa_s->parent, MSG_INFO, P2P_EVENT_GROUP_STARTED
887 "%s GO ssid=\"%s\" freq=%d passphrase=\"%s\" "
888 "go_dev_addr=" MACSTR "%s",
889 wpa_s->ifname, ssid_txt,
890 ssid ? ssid->frequency : 0,
891 ssid && ssid->passphrase ? ssid->passphrase : "",
892 MAC2STR(go_dev_addr),
893 persistent ? " [PERSISTENT]" : "");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700894 wpas_p2p_cross_connect_setup(wpa_s);
895 wpas_p2p_set_group_idle_timeout(wpa_s);
896 }
897
898 if (persistent)
Jouni Malinen75ecf522011-06-27 15:19:46 -0700899 network_id = wpas_p2p_store_persistent_group(wpa_s->parent,
900 ssid, go_dev_addr);
Dmitry Shmidt391c59f2013-09-03 12:16:28 -0700901 else {
902 os_free(wpa_s->global->add_psk);
903 wpa_s->global->add_psk = NULL;
904 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800905 if (network_id < 0 && ssid)
Jouni Malinen75ecf522011-06-27 15:19:46 -0700906 network_id = ssid->id;
Dmitry Shmidt92c368d2013-08-29 12:37:21 -0700907 if (!client) {
Jouni Malinen75ecf522011-06-27 15:19:46 -0700908 wpas_notify_p2p_group_started(wpa_s, ssid, network_id, 0);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800909 os_get_reltime(&wpa_s->global->p2p_go_wait_client);
Dmitry Shmidt92c368d2013-08-29 12:37:21 -0700910 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700911}
912
913
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800914struct send_action_work {
915 unsigned int freq;
916 u8 dst[ETH_ALEN];
917 u8 src[ETH_ALEN];
918 u8 bssid[ETH_ALEN];
919 size_t len;
920 unsigned int wait_time;
921 u8 buf[0];
922};
923
924
925static void wpas_p2p_send_action_work_timeout(void *eloop_ctx,
926 void *timeout_ctx)
927{
928 struct wpa_supplicant *wpa_s = eloop_ctx;
929
930 if (!wpa_s->p2p_send_action_work)
931 return;
932
933 wpa_printf(MSG_DEBUG, "P2P: Send Action frame radio work timed out");
934 os_free(wpa_s->p2p_send_action_work->ctx);
935 radio_work_done(wpa_s->p2p_send_action_work);
936 wpa_s->p2p_send_action_work = NULL;
937}
938
939
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800940static void wpas_p2p_send_action_tx_status(struct wpa_supplicant *wpa_s,
941 unsigned int freq,
942 const u8 *dst, const u8 *src,
943 const u8 *bssid,
944 const u8 *data, size_t data_len,
945 enum offchannel_send_action_result
946 result)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700947{
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800948 enum p2p_send_action_result res = P2P_SEND_ACTION_SUCCESS;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700949
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800950 if (wpa_s->p2p_send_action_work) {
951 struct send_action_work *awork;
952 awork = wpa_s->p2p_send_action_work->ctx;
953 if (awork->wait_time == 0) {
954 os_free(awork);
955 radio_work_done(wpa_s->p2p_send_action_work);
956 wpa_s->p2p_send_action_work = NULL;
957 } else {
958 /*
959 * In theory, this should not be needed, but number of
960 * places in the P2P code is still using non-zero wait
961 * time for the last Action frame in the sequence and
962 * some of these do not call send_action_done().
963 */
964 eloop_cancel_timeout(wpas_p2p_send_action_work_timeout,
965 wpa_s, NULL);
966 eloop_register_timeout(
967 0, awork->wait_time * 1000,
968 wpas_p2p_send_action_work_timeout,
969 wpa_s, NULL);
970 }
971 }
972
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700973 if (wpa_s->global->p2p == NULL || wpa_s->global->p2p_disabled)
974 return;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700975
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800976 switch (result) {
977 case OFFCHANNEL_SEND_ACTION_SUCCESS:
978 res = P2P_SEND_ACTION_SUCCESS;
979 break;
980 case OFFCHANNEL_SEND_ACTION_NO_ACK:
981 res = P2P_SEND_ACTION_NO_ACK;
982 break;
983 case OFFCHANNEL_SEND_ACTION_FAILED:
984 res = P2P_SEND_ACTION_FAILED;
985 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700986 }
987
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800988 p2p_send_action_cb(wpa_s->global->p2p, freq, dst, src, bssid, res);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700989
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -0800990 if (result != OFFCHANNEL_SEND_ACTION_SUCCESS &&
991 wpa_s->pending_pd_before_join &&
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800992 (os_memcmp(dst, wpa_s->pending_join_dev_addr, ETH_ALEN) == 0 ||
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800993 os_memcmp(dst, wpa_s->pending_join_iface_addr, ETH_ALEN) == 0) &&
994 wpa_s->p2p_fallback_to_go_neg) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700995 wpa_s->pending_pd_before_join = 0;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800996 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: No ACK for PD Req "
997 "during p2p_connect-auto");
998 wpas_p2p_fallback_to_go_neg(wpa_s, 0);
999 return;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001000 }
1001}
1002
1003
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001004static void wpas_send_action_cb(struct wpa_radio_work *work, int deinit)
1005{
1006 struct wpa_supplicant *wpa_s = work->wpa_s;
1007 struct send_action_work *awork = work->ctx;
1008
1009 if (deinit) {
Dmitry Shmidtbd14a572014-02-18 10:33:49 -08001010 if (work->started) {
1011 eloop_cancel_timeout(wpas_p2p_send_action_work_timeout,
1012 wpa_s, NULL);
1013 wpa_s->p2p_send_action_work = NULL;
1014 offchannel_send_action_done(wpa_s);
1015 }
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001016 os_free(awork);
1017 return;
1018 }
1019
1020 if (offchannel_send_action(wpa_s, awork->freq, awork->dst, awork->src,
1021 awork->bssid, awork->buf, awork->len,
1022 awork->wait_time,
1023 wpas_p2p_send_action_tx_status, 1) < 0) {
1024 os_free(awork);
1025 radio_work_done(work);
1026 return;
1027 }
1028 wpa_s->p2p_send_action_work = work;
1029}
1030
1031
1032static int wpas_send_action_work(struct wpa_supplicant *wpa_s,
1033 unsigned int freq, const u8 *dst,
1034 const u8 *src, const u8 *bssid, const u8 *buf,
1035 size_t len, unsigned int wait_time)
1036{
1037 struct send_action_work *awork;
1038
1039 if (wpa_s->p2p_send_action_work) {
1040 wpa_printf(MSG_DEBUG, "P2P: Cannot schedule new p2p-send-action work since one is already pending");
1041 return -1;
1042 }
1043
1044 awork = os_zalloc(sizeof(*awork) + len);
1045 if (awork == NULL)
1046 return -1;
1047
1048 awork->freq = freq;
1049 os_memcpy(awork->dst, dst, ETH_ALEN);
1050 os_memcpy(awork->src, src, ETH_ALEN);
1051 os_memcpy(awork->bssid, bssid, ETH_ALEN);
1052 awork->len = len;
1053 awork->wait_time = wait_time;
1054 os_memcpy(awork->buf, buf, len);
1055
1056 if (radio_add_work(wpa_s, freq, "p2p-send-action", 0,
1057 wpas_send_action_cb, awork) < 0) {
1058 os_free(awork);
1059 return -1;
1060 }
1061
1062 return 0;
1063}
1064
1065
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001066static int wpas_send_action(void *ctx, unsigned int freq, const u8 *dst,
1067 const u8 *src, const u8 *bssid, const u8 *buf,
1068 size_t len, unsigned int wait_time)
1069{
1070 struct wpa_supplicant *wpa_s = ctx;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001071 int listen_freq = -1, send_freq = -1;
1072
1073 if (wpa_s->p2p_listen_work)
1074 listen_freq = wpa_s->p2p_listen_work->freq;
1075 if (wpa_s->p2p_send_action_work)
1076 send_freq = wpa_s->p2p_send_action_work->freq;
1077 if (listen_freq != (int) freq && send_freq != (int) freq) {
1078 wpa_printf(MSG_DEBUG, "P2P: Schedule new radio work for Action frame TX (listen_freq=%d send_freq=%d)",
1079 listen_freq, send_freq);
1080 return wpas_send_action_work(wpa_s, freq, dst, src, bssid, buf,
1081 len, wait_time);
1082 }
1083
1084 wpa_printf(MSG_DEBUG, "P2P: Use ongoing radio work for Action frame TX");
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001085 return offchannel_send_action(wpa_s, freq, dst, src, bssid, buf, len,
1086 wait_time,
1087 wpas_p2p_send_action_tx_status, 1);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001088}
1089
1090
1091static void wpas_send_action_done(void *ctx)
1092{
1093 struct wpa_supplicant *wpa_s = ctx;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001094
1095 if (wpa_s->p2p_send_action_work) {
1096 eloop_cancel_timeout(wpas_p2p_send_action_work_timeout,
1097 wpa_s, NULL);
1098 os_free(wpa_s->p2p_send_action_work->ctx);
1099 radio_work_done(wpa_s->p2p_send_action_work);
1100 wpa_s->p2p_send_action_work = NULL;
1101 }
1102
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001103 offchannel_send_action_done(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001104}
1105
1106
1107static int wpas_copy_go_neg_results(struct wpa_supplicant *wpa_s,
1108 struct p2p_go_neg_results *params)
1109{
1110 if (wpa_s->go_params == NULL) {
1111 wpa_s->go_params = os_malloc(sizeof(*params));
1112 if (wpa_s->go_params == NULL)
1113 return -1;
1114 }
1115 os_memcpy(wpa_s->go_params, params, sizeof(*params));
1116 return 0;
1117}
1118
1119
1120static void wpas_start_wps_enrollee(struct wpa_supplicant *wpa_s,
1121 struct p2p_go_neg_results *res)
1122{
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001123 wpa_printf(MSG_DEBUG, "P2P: Start WPS Enrollee for peer " MACSTR
1124 " dev_addr " MACSTR " wps_method %d",
1125 MAC2STR(res->peer_interface_addr),
1126 MAC2STR(res->peer_device_addr), res->wps_method);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001127 wpa_hexdump_ascii(MSG_DEBUG, "P2P: Start WPS Enrollee for SSID",
1128 res->ssid, res->ssid_len);
1129 wpa_supplicant_ap_deinit(wpa_s);
1130 wpas_copy_go_neg_results(wpa_s, res);
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001131 if (res->wps_method == WPS_PBC) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001132 wpas_wps_start_pbc(wpa_s, res->peer_interface_addr, 1);
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001133#ifdef CONFIG_WPS_NFC
1134 } else if (res->wps_method == WPS_NFC) {
1135 wpas_wps_start_nfc(wpa_s, res->peer_device_addr,
1136 res->peer_interface_addr,
1137 wpa_s->parent->p2p_oob_dev_pw,
1138 wpa_s->parent->p2p_oob_dev_pw_id, 1,
1139 wpa_s->parent->p2p_oob_dev_pw_id ==
1140 DEV_PW_NFC_CONNECTION_HANDOVER ?
1141 wpa_s->parent->p2p_peer_oob_pubkey_hash :
1142 NULL,
1143 NULL, 0, 0);
1144#endif /* CONFIG_WPS_NFC */
1145 } else {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001146 u16 dev_pw_id = DEV_PW_DEFAULT;
1147 if (wpa_s->p2p_wps_method == WPS_PIN_KEYPAD)
1148 dev_pw_id = DEV_PW_REGISTRAR_SPECIFIED;
1149 wpas_wps_start_pin(wpa_s, res->peer_interface_addr,
1150 wpa_s->p2p_pin, 1, dev_pw_id);
1151 }
1152}
1153
1154
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07001155static void wpas_p2p_add_psk_list(struct wpa_supplicant *wpa_s,
1156 struct wpa_ssid *ssid)
1157{
1158 struct wpa_ssid *persistent;
1159 struct psk_list_entry *psk;
1160 struct hostapd_data *hapd;
1161
1162 if (!wpa_s->ap_iface)
1163 return;
1164
1165 persistent = wpas_p2p_get_persistent(wpa_s->parent, NULL, ssid->ssid,
1166 ssid->ssid_len);
1167 if (persistent == NULL)
1168 return;
1169
1170 hapd = wpa_s->ap_iface->bss[0];
1171
1172 dl_list_for_each(psk, &persistent->psk_list, struct psk_list_entry,
1173 list) {
1174 struct hostapd_wpa_psk *hpsk;
1175
1176 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Add persistent group PSK entry for "
1177 MACSTR " psk=%d",
1178 MAC2STR(psk->addr), psk->p2p);
1179 hpsk = os_zalloc(sizeof(*hpsk));
1180 if (hpsk == NULL)
1181 break;
1182 os_memcpy(hpsk->psk, psk->psk, PMK_LEN);
1183 if (psk->p2p)
1184 os_memcpy(hpsk->p2p_dev_addr, psk->addr, ETH_ALEN);
1185 else
1186 os_memcpy(hpsk->addr, psk->addr, ETH_ALEN);
1187 hpsk->next = hapd->conf->ssid.wpa_psk;
1188 hapd->conf->ssid.wpa_psk = hpsk;
1189 }
1190}
1191
1192
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001193static void p2p_go_configured(void *ctx, void *data)
1194{
1195 struct wpa_supplicant *wpa_s = ctx;
1196 struct p2p_go_neg_results *params = data;
1197 struct wpa_ssid *ssid;
Jouni Malinen75ecf522011-06-27 15:19:46 -07001198 int network_id = -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001199
1200 ssid = wpa_s->current_ssid;
1201 if (ssid && ssid->mode == WPAS_MODE_P2P_GO) {
1202 wpa_printf(MSG_DEBUG, "P2P: Group setup without provisioning");
1203 if (wpa_s->global->p2p_group_formation == wpa_s)
1204 wpa_s->global->p2p_group_formation = NULL;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001205 if (os_strlen(params->passphrase) > 0) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07001206 wpa_msg_global(wpa_s->parent, MSG_INFO,
1207 P2P_EVENT_GROUP_STARTED
1208 "%s GO ssid=\"%s\" freq=%d "
1209 "passphrase=\"%s\" go_dev_addr=" MACSTR
1210 "%s", wpa_s->ifname,
1211 wpa_ssid_txt(ssid->ssid, ssid->ssid_len),
1212 ssid->frequency, params->passphrase,
1213 MAC2STR(wpa_s->global->p2p_dev_addr),
1214 params->persistent_group ?
1215 " [PERSISTENT]" : "");
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001216 } else {
1217 char psk[65];
1218 wpa_snprintf_hex(psk, sizeof(psk), params->psk,
1219 sizeof(params->psk));
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07001220 wpa_msg_global(wpa_s->parent, MSG_INFO,
1221 P2P_EVENT_GROUP_STARTED
1222 "%s GO ssid=\"%s\" freq=%d psk=%s "
1223 "go_dev_addr=" MACSTR "%s",
1224 wpa_s->ifname,
1225 wpa_ssid_txt(ssid->ssid, ssid->ssid_len),
1226 ssid->frequency, psk,
1227 MAC2STR(wpa_s->global->p2p_dev_addr),
1228 params->persistent_group ?
1229 " [PERSISTENT]" : "");
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001230 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001231
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001232 os_get_reltime(&wpa_s->global->p2p_go_wait_client);
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07001233 if (params->persistent_group) {
Jouni Malinen75ecf522011-06-27 15:19:46 -07001234 network_id = wpas_p2p_store_persistent_group(
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001235 wpa_s->parent, ssid,
Dmitry Shmidt497c1d52011-07-21 15:19:46 -07001236 wpa_s->global->p2p_dev_addr);
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07001237 wpas_p2p_add_psk_list(wpa_s, ssid);
1238 }
Jouni Malinen75ecf522011-06-27 15:19:46 -07001239 if (network_id < 0)
1240 network_id = ssid->id;
1241 wpas_notify_p2p_group_started(wpa_s, ssid, network_id, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001242 wpas_p2p_cross_connect_setup(wpa_s);
1243 wpas_p2p_set_group_idle_timeout(wpa_s);
Dmitry Shmidt56052862013-10-04 10:23:25 -07001244
1245 if (wpa_s->p2p_first_connection_timeout) {
1246 wpa_dbg(wpa_s, MSG_DEBUG,
1247 "P2P: Start group formation timeout of %d seconds until first data connection on GO",
1248 wpa_s->p2p_first_connection_timeout);
1249 wpa_s->p2p_go_group_formation_completed = 0;
1250 wpa_s->global->p2p_group_formation = wpa_s;
1251 eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
1252 wpa_s->parent, NULL);
1253 eloop_register_timeout(
1254 wpa_s->p2p_first_connection_timeout, 0,
1255 wpas_p2p_group_formation_timeout,
1256 wpa_s->parent, NULL);
1257 }
1258
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001259 return;
1260 }
1261
1262 wpa_printf(MSG_DEBUG, "P2P: Setting up WPS for GO provisioning");
1263 if (wpa_supplicant_ap_mac_addr_filter(wpa_s,
1264 params->peer_interface_addr)) {
1265 wpa_printf(MSG_DEBUG, "P2P: Failed to setup MAC address "
1266 "filtering");
1267 return;
1268 }
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001269 if (params->wps_method == WPS_PBC) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001270 wpa_supplicant_ap_wps_pbc(wpa_s, params->peer_interface_addr,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001271 params->peer_device_addr);
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001272#ifdef CONFIG_WPS_NFC
1273 } else if (params->wps_method == WPS_NFC) {
1274 if (wpa_s->parent->p2p_oob_dev_pw_id !=
1275 DEV_PW_NFC_CONNECTION_HANDOVER &&
1276 !wpa_s->parent->p2p_oob_dev_pw) {
1277 wpa_printf(MSG_DEBUG, "P2P: No NFC Dev Pw known");
1278 return;
1279 }
1280 wpas_ap_wps_add_nfc_pw(
1281 wpa_s, wpa_s->parent->p2p_oob_dev_pw_id,
1282 wpa_s->parent->p2p_oob_dev_pw,
1283 wpa_s->parent->p2p_peer_oob_pk_hash_known ?
1284 wpa_s->parent->p2p_peer_oob_pubkey_hash : NULL);
1285#endif /* CONFIG_WPS_NFC */
1286 } else if (wpa_s->p2p_pin[0])
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001287 wpa_supplicant_ap_wps_pin(wpa_s, params->peer_interface_addr,
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001288 wpa_s->p2p_pin, NULL, 0, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001289 os_free(wpa_s->go_params);
1290 wpa_s->go_params = NULL;
1291}
1292
1293
1294static void wpas_start_wps_go(struct wpa_supplicant *wpa_s,
1295 struct p2p_go_neg_results *params,
1296 int group_formation)
1297{
1298 struct wpa_ssid *ssid;
1299
Dmitry Shmidtaa532512012-09-24 10:35:31 -07001300 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Starting GO");
1301 if (wpas_copy_go_neg_results(wpa_s, params) < 0) {
1302 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Could not copy GO Negotiation "
1303 "results");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001304 return;
Dmitry Shmidtaa532512012-09-24 10:35:31 -07001305 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001306
1307 ssid = wpa_config_add_network(wpa_s->conf);
Dmitry Shmidtaa532512012-09-24 10:35:31 -07001308 if (ssid == NULL) {
1309 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Could not add network for GO");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001310 return;
Dmitry Shmidtaa532512012-09-24 10:35:31 -07001311 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001312
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001313 wpa_s->show_group_started = 0;
1314
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001315 wpa_config_set_network_defaults(ssid);
1316 ssid->temporary = 1;
1317 ssid->p2p_group = 1;
1318 ssid->p2p_persistent_group = params->persistent_group;
1319 ssid->mode = group_formation ? WPAS_MODE_P2P_GROUP_FORMATION :
1320 WPAS_MODE_P2P_GO;
1321 ssid->frequency = params->freq;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001322 ssid->ht40 = params->ht40;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07001323 ssid->vht = params->vht;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001324 ssid->ssid = os_zalloc(params->ssid_len + 1);
1325 if (ssid->ssid) {
1326 os_memcpy(ssid->ssid, params->ssid, params->ssid_len);
1327 ssid->ssid_len = params->ssid_len;
1328 }
1329 ssid->auth_alg = WPA_AUTH_ALG_OPEN;
1330 ssid->key_mgmt = WPA_KEY_MGMT_PSK;
1331 ssid->proto = WPA_PROTO_RSN;
1332 ssid->pairwise_cipher = WPA_CIPHER_CCMP;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001333 if (os_strlen(params->passphrase) > 0) {
1334 ssid->passphrase = os_strdup(params->passphrase);
1335 if (ssid->passphrase == NULL) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07001336 wpa_msg_global(wpa_s, MSG_ERROR,
1337 "P2P: Failed to copy passphrase for GO");
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001338 wpa_config_remove_network(wpa_s->conf, ssid->id);
1339 return;
1340 }
1341 } else
1342 ssid->passphrase = NULL;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001343 ssid->psk_set = params->psk_set;
1344 if (ssid->psk_set)
1345 os_memcpy(ssid->psk, params->psk, sizeof(ssid->psk));
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001346 else if (ssid->passphrase)
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001347 wpa_config_update_psk(ssid);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001348 ssid->ap_max_inactivity = wpa_s->parent->conf->p2p_go_max_inactivity;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001349
1350 wpa_s->ap_configured_cb = p2p_go_configured;
1351 wpa_s->ap_configured_cb_ctx = wpa_s;
1352 wpa_s->ap_configured_cb_data = wpa_s->go_params;
Jouni Malinen75ecf522011-06-27 15:19:46 -07001353 wpa_s->connect_without_scan = ssid;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001354 wpa_s->reassociate = 1;
1355 wpa_s->disconnected = 0;
Dmitry Shmidtaa532512012-09-24 10:35:31 -07001356 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Request scan (that will be skipped) to "
1357 "start GO)");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001358 wpa_supplicant_req_scan(wpa_s, 0, 0);
1359}
1360
1361
1362static void wpas_p2p_clone_config(struct wpa_supplicant *dst,
1363 const struct wpa_supplicant *src)
1364{
1365 struct wpa_config *d;
1366 const struct wpa_config *s;
1367
1368 d = dst->conf;
1369 s = src->conf;
1370
1371#define C(n) if (s->n) d->n = os_strdup(s->n)
1372 C(device_name);
1373 C(manufacturer);
1374 C(model_name);
1375 C(model_number);
1376 C(serial_number);
1377 C(config_methods);
1378#undef C
1379
1380 os_memcpy(d->device_type, s->device_type, WPS_DEV_TYPE_LEN);
1381 os_memcpy(d->sec_device_type, s->sec_device_type,
1382 sizeof(d->sec_device_type));
1383 d->num_sec_device_types = s->num_sec_device_types;
1384
1385 d->p2p_group_idle = s->p2p_group_idle;
1386 d->p2p_intra_bss = s->p2p_intra_bss;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001387 d->persistent_reconnect = s->persistent_reconnect;
Dmitry Shmidt04949592012-07-19 12:16:46 -07001388 d->max_num_sta = s->max_num_sta;
1389 d->pbc_in_m1 = s->pbc_in_m1;
Dmitry Shmidt444d5672013-04-01 13:08:44 -07001390 d->ignore_old_scan_res = s->ignore_old_scan_res;
Dmitry Shmidt51b6ea82013-05-08 10:42:09 -07001391 d->beacon_int = s->beacon_int;
Dmitry Shmidt18463232014-01-24 12:29:41 -08001392 d->dtim_period = s->dtim_period;
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07001393 d->disassoc_low_ack = s->disassoc_low_ack;
Dmitry Shmidt96571392013-10-14 12:54:46 -07001394 d->disable_scan_offload = s->disable_scan_offload;
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001395
1396 if (s->wps_nfc_dh_privkey && s->wps_nfc_dh_pubkey) {
1397 d->wps_nfc_dh_privkey = wpabuf_dup(s->wps_nfc_dh_privkey);
1398 d->wps_nfc_dh_pubkey = wpabuf_dup(s->wps_nfc_dh_pubkey);
1399 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001400}
1401
1402
Dmitry Shmidt34af3062013-07-11 10:46:32 -07001403static void wpas_p2p_get_group_ifname(struct wpa_supplicant *wpa_s,
1404 char *ifname, size_t len)
1405{
1406 char *ifname_ptr = wpa_s->ifname;
1407
1408 if (os_strncmp(wpa_s->ifname, P2P_MGMT_DEVICE_PREFIX,
1409 os_strlen(P2P_MGMT_DEVICE_PREFIX)) == 0) {
1410 ifname_ptr = os_strrchr(wpa_s->ifname, '-') + 1;
1411 }
1412
1413 os_snprintf(ifname, len, "p2p-%s-%d", ifname_ptr, wpa_s->p2p_group_idx);
1414 if (os_strlen(ifname) >= IFNAMSIZ &&
1415 os_strlen(wpa_s->ifname) < IFNAMSIZ) {
1416 /* Try to avoid going over the IFNAMSIZ length limit */
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07001417 os_snprintf(ifname, len, "p2p-%d", wpa_s->p2p_group_idx);
Dmitry Shmidt34af3062013-07-11 10:46:32 -07001418 }
1419}
1420
1421
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001422static int wpas_p2p_add_group_interface(struct wpa_supplicant *wpa_s,
1423 enum wpa_driver_if_type type)
1424{
1425 char ifname[120], force_ifname[120];
1426
1427 if (wpa_s->pending_interface_name[0]) {
1428 wpa_printf(MSG_DEBUG, "P2P: Pending virtual interface exists "
1429 "- skip creation of a new one");
1430 if (is_zero_ether_addr(wpa_s->pending_interface_addr)) {
1431 wpa_printf(MSG_DEBUG, "P2P: Pending virtual address "
1432 "unknown?! ifname='%s'",
1433 wpa_s->pending_interface_name);
1434 return -1;
1435 }
1436 return 0;
1437 }
1438
Dmitry Shmidt34af3062013-07-11 10:46:32 -07001439 wpas_p2p_get_group_ifname(wpa_s, ifname, sizeof(ifname));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001440 force_ifname[0] = '\0';
1441
1442 wpa_printf(MSG_DEBUG, "P2P: Create a new interface %s for the group",
1443 ifname);
1444 wpa_s->p2p_group_idx++;
1445
1446 wpa_s->pending_interface_type = type;
1447 if (wpa_drv_if_add(wpa_s, type, ifname, NULL, NULL, force_ifname,
1448 wpa_s->pending_interface_addr, NULL) < 0) {
1449 wpa_printf(MSG_ERROR, "P2P: Failed to create new group "
1450 "interface");
1451 return -1;
1452 }
1453
1454 if (force_ifname[0]) {
1455 wpa_printf(MSG_DEBUG, "P2P: Driver forced interface name %s",
1456 force_ifname);
1457 os_strlcpy(wpa_s->pending_interface_name, force_ifname,
1458 sizeof(wpa_s->pending_interface_name));
1459 } else
1460 os_strlcpy(wpa_s->pending_interface_name, ifname,
1461 sizeof(wpa_s->pending_interface_name));
1462 wpa_printf(MSG_DEBUG, "P2P: Created pending virtual interface %s addr "
1463 MACSTR, wpa_s->pending_interface_name,
1464 MAC2STR(wpa_s->pending_interface_addr));
1465
1466 return 0;
1467}
1468
1469
1470static void wpas_p2p_remove_pending_group_interface(
1471 struct wpa_supplicant *wpa_s)
1472{
1473 if (!wpa_s->pending_interface_name[0] ||
1474 is_zero_ether_addr(wpa_s->pending_interface_addr))
1475 return; /* No pending virtual interface */
1476
1477 wpa_printf(MSG_DEBUG, "P2P: Removing pending group interface %s",
1478 wpa_s->pending_interface_name);
1479 wpa_drv_if_remove(wpa_s, wpa_s->pending_interface_type,
1480 wpa_s->pending_interface_name);
1481 os_memset(wpa_s->pending_interface_addr, 0, ETH_ALEN);
1482 wpa_s->pending_interface_name[0] = '\0';
1483}
1484
1485
1486static struct wpa_supplicant *
1487wpas_p2p_init_group_interface(struct wpa_supplicant *wpa_s, int go)
1488{
1489 struct wpa_interface iface;
1490 struct wpa_supplicant *group_wpa_s;
1491
1492 if (!wpa_s->pending_interface_name[0]) {
1493 wpa_printf(MSG_ERROR, "P2P: No pending group interface");
1494 if (!wpas_p2p_create_iface(wpa_s))
1495 return NULL;
1496 /*
1497 * Something has forced us to remove the pending interface; try
1498 * to create a new one and hope for the best that we will get
1499 * the same local address.
1500 */
1501 if (wpas_p2p_add_group_interface(wpa_s, go ? WPA_IF_P2P_GO :
1502 WPA_IF_P2P_CLIENT) < 0)
1503 return NULL;
1504 }
1505
1506 os_memset(&iface, 0, sizeof(iface));
1507 iface.ifname = wpa_s->pending_interface_name;
1508 iface.driver = wpa_s->driver->name;
Dmitry Shmidt34af3062013-07-11 10:46:32 -07001509 if (wpa_s->conf->ctrl_interface == NULL &&
1510 wpa_s->parent != wpa_s &&
1511 wpa_s->p2p_mgmt &&
1512 (wpa_s->drv_flags & WPA_DRIVER_FLAGS_DEDICATED_P2P_DEVICE))
1513 iface.ctrl_interface = wpa_s->parent->conf->ctrl_interface;
1514 else
1515 iface.ctrl_interface = wpa_s->conf->ctrl_interface;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001516 iface.driver_param = wpa_s->conf->driver_param;
1517 group_wpa_s = wpa_supplicant_add_iface(wpa_s->global, &iface);
1518 if (group_wpa_s == NULL) {
1519 wpa_printf(MSG_ERROR, "P2P: Failed to create new "
1520 "wpa_supplicant interface");
1521 return NULL;
1522 }
1523 wpa_s->pending_interface_name[0] = '\0';
1524 group_wpa_s->parent = wpa_s;
1525 group_wpa_s->p2p_group_interface = go ? P2P_GROUP_INTERFACE_GO :
1526 P2P_GROUP_INTERFACE_CLIENT;
1527 wpa_s->global->p2p_group_formation = group_wpa_s;
1528
1529 wpas_p2p_clone_config(group_wpa_s, wpa_s);
1530
1531 return group_wpa_s;
1532}
1533
1534
1535static void wpas_p2p_group_formation_timeout(void *eloop_ctx,
1536 void *timeout_ctx)
1537{
1538 struct wpa_supplicant *wpa_s = eloop_ctx;
1539 wpa_printf(MSG_DEBUG, "P2P: Group Formation timed out");
Dmitry Shmidt34af3062013-07-11 10:46:32 -07001540 wpas_p2p_group_formation_failed(wpa_s);
1541}
1542
1543
1544void wpas_p2p_group_formation_failed(struct wpa_supplicant *wpa_s)
1545{
1546 eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
1547 wpa_s->parent, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001548 if (wpa_s->global->p2p)
1549 p2p_group_formation_failed(wpa_s->global->p2p);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001550 wpas_group_formation_completed(wpa_s, 0);
1551}
1552
1553
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001554static void wpas_p2p_grpform_fail_after_wps(struct wpa_supplicant *wpa_s)
1555{
1556 wpa_printf(MSG_DEBUG, "P2P: Reject group formation due to WPS provisioning failure");
1557 eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
1558 wpa_s->parent, NULL);
1559 eloop_register_timeout(0, 0, wpas_p2p_group_formation_timeout,
1560 wpa_s->parent, NULL);
1561 wpa_s->global->p2p_fail_on_wps_complete = 0;
1562}
1563
1564
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07001565void wpas_p2p_ap_setup_failed(struct wpa_supplicant *wpa_s)
1566{
1567 if (wpa_s->global->p2p_group_formation != wpa_s)
1568 return;
1569 /* Speed up group formation timeout since this cannot succeed */
1570 eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
1571 wpa_s->parent, NULL);
1572 eloop_register_timeout(0, 0, wpas_p2p_group_formation_timeout,
1573 wpa_s->parent, NULL);
1574}
1575
1576
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001577static void wpas_go_neg_completed(void *ctx, struct p2p_go_neg_results *res)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001578{
1579 struct wpa_supplicant *wpa_s = ctx;
1580
1581 if (wpa_s->off_channel_freq || wpa_s->roc_waiting_drv_freq) {
1582 wpa_drv_cancel_remain_on_channel(wpa_s);
1583 wpa_s->off_channel_freq = 0;
1584 wpa_s->roc_waiting_drv_freq = 0;
1585 }
1586
1587 if (res->status) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07001588 wpa_msg_global(wpa_s, MSG_INFO,
1589 P2P_EVENT_GO_NEG_FAILURE "status=%d",
1590 res->status);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001591 wpas_notify_p2p_go_neg_completed(wpa_s, res);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001592 wpas_p2p_remove_pending_group_interface(wpa_s);
1593 return;
1594 }
1595
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001596 if (wpa_s->p2p_go_ht40)
1597 res->ht40 = 1;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07001598 if (wpa_s->p2p_go_vht)
1599 res->vht = 1;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001600
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07001601 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_GO_NEG_SUCCESS "role=%s "
1602 "freq=%d ht40=%d peer_dev=" MACSTR " peer_iface=" MACSTR
1603 " wps_method=%s",
1604 res->role_go ? "GO" : "client", res->freq, res->ht40,
1605 MAC2STR(res->peer_device_addr),
1606 MAC2STR(res->peer_interface_addr),
1607 p2p_wps_method_text(res->wps_method));
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001608 wpas_notify_p2p_go_neg_completed(wpa_s, res);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001609
Dmitry Shmidt04949592012-07-19 12:16:46 -07001610 if (res->role_go && wpa_s->p2p_persistent_id >= 0) {
1611 struct wpa_ssid *ssid;
1612 ssid = wpa_config_get_network(wpa_s->conf,
1613 wpa_s->p2p_persistent_id);
1614 if (ssid && ssid->disabled == 2 &&
1615 ssid->mode == WPAS_MODE_P2P_GO && ssid->passphrase) {
1616 size_t len = os_strlen(ssid->passphrase);
1617 wpa_printf(MSG_DEBUG, "P2P: Override passphrase based "
1618 "on requested persistent group");
1619 os_memcpy(res->passphrase, ssid->passphrase, len);
1620 res->passphrase[len] = '\0';
1621 }
1622 }
1623
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001624 if (wpa_s->create_p2p_iface) {
1625 struct wpa_supplicant *group_wpa_s =
1626 wpas_p2p_init_group_interface(wpa_s, res->role_go);
1627 if (group_wpa_s == NULL) {
1628 wpas_p2p_remove_pending_group_interface(wpa_s);
1629 return;
1630 }
1631 if (group_wpa_s != wpa_s) {
1632 os_memcpy(group_wpa_s->p2p_pin, wpa_s->p2p_pin,
1633 sizeof(group_wpa_s->p2p_pin));
1634 group_wpa_s->p2p_wps_method = wpa_s->p2p_wps_method;
1635 }
1636 os_memset(wpa_s->pending_interface_addr, 0, ETH_ALEN);
1637 wpa_s->pending_interface_name[0] = '\0';
1638 group_wpa_s->p2p_in_provisioning = 1;
1639
1640 if (res->role_go)
1641 wpas_start_wps_go(group_wpa_s, res, 1);
1642 else
1643 wpas_start_wps_enrollee(group_wpa_s, res);
1644 } else {
1645 wpa_s->p2p_in_provisioning = 1;
1646 wpa_s->global->p2p_group_formation = wpa_s;
1647
1648 if (res->role_go)
1649 wpas_start_wps_go(wpa_s, res, 1);
1650 else
1651 wpas_start_wps_enrollee(ctx, res);
1652 }
1653
1654 wpa_s->p2p_long_listen = 0;
1655 eloop_cancel_timeout(wpas_p2p_long_listen_timeout, wpa_s, NULL);
1656
1657 eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s, NULL);
1658 eloop_register_timeout(15 + res->peer_config_timeout / 100,
1659 (res->peer_config_timeout % 100) * 10000,
1660 wpas_p2p_group_formation_timeout, wpa_s, NULL);
1661}
1662
1663
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001664static void wpas_go_neg_req_rx(void *ctx, const u8 *src, u16 dev_passwd_id)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001665{
1666 struct wpa_supplicant *wpa_s = ctx;
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07001667 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_GO_NEG_REQUEST MACSTR
1668 " dev_passwd_id=%u", MAC2STR(src), dev_passwd_id);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001669
1670 wpas_notify_p2p_go_neg_req(wpa_s, src, dev_passwd_id);
1671}
1672
1673
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001674static void wpas_dev_found(void *ctx, const u8 *addr,
1675 const struct p2p_peer_info *info,
1676 int new_device)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001677{
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001678#ifndef CONFIG_NO_STDOUT_DEBUG
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001679 struct wpa_supplicant *wpa_s = ctx;
1680 char devtype[WPS_DEV_TYPE_BUFSIZE];
Dmitry Shmidt04f534e2013-12-09 15:50:16 -08001681 char *wfd_dev_info_hex = NULL;
1682
Irfan Sheriff8367dc92012-09-09 17:08:19 -07001683#ifdef CONFIG_WIFI_DISPLAY
Dmitry Shmidt04f534e2013-12-09 15:50:16 -08001684 wfd_dev_info_hex = wifi_display_subelem_hex(info->wfd_subelems,
1685 WFD_SUBELEM_DEVICE_INFO);
Irfan Sheriff8367dc92012-09-09 17:08:19 -07001686#endif /* CONFIG_WIFI_DISPLAY */
Dmitry Shmidt04f534e2013-12-09 15:50:16 -08001687
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07001688 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_DEVICE_FOUND MACSTR
1689 " p2p_dev_addr=" MACSTR
1690 " pri_dev_type=%s name='%s' config_methods=0x%x "
1691 "dev_capab=0x%x group_capab=0x%x%s%s",
1692 MAC2STR(addr), MAC2STR(info->p2p_device_addr),
1693 wps_dev_type_bin2str(info->pri_dev_type, devtype,
1694 sizeof(devtype)),
1695 info->device_name, info->config_methods,
1696 info->dev_capab, info->group_capab,
Dmitry Shmidt04f534e2013-12-09 15:50:16 -08001697 wfd_dev_info_hex ? " wfd_dev_info=0x" : "",
1698 wfd_dev_info_hex ? wfd_dev_info_hex : "");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001699
Dmitry Shmidt04f534e2013-12-09 15:50:16 -08001700 os_free(wfd_dev_info_hex);
Dmitry Shmidt97672262014-02-03 13:02:54 -08001701#endif /* CONFIG_NO_STDOUT_DEBUG */
Dmitry Shmidt04f534e2013-12-09 15:50:16 -08001702
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001703 wpas_notify_p2p_device_found(ctx, info->p2p_device_addr, new_device);
1704}
1705
1706
1707static void wpas_dev_lost(void *ctx, const u8 *dev_addr)
1708{
1709 struct wpa_supplicant *wpa_s = ctx;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001710
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07001711 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_DEVICE_LOST
1712 "p2p_dev_addr=" MACSTR, MAC2STR(dev_addr));
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001713
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001714 wpas_notify_p2p_device_lost(wpa_s, dev_addr);
1715}
1716
1717
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07001718static void wpas_find_stopped(void *ctx)
1719{
1720 struct wpa_supplicant *wpa_s = ctx;
1721 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_FIND_STOPPED);
1722}
1723
1724
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001725struct wpas_p2p_listen_work {
1726 unsigned int freq;
1727 unsigned int duration;
1728 struct wpabuf *probe_resp_ie;
1729};
1730
1731
1732static void wpas_p2p_listen_work_free(struct wpas_p2p_listen_work *lwork)
1733{
1734 if (lwork == NULL)
1735 return;
1736 wpabuf_free(lwork->probe_resp_ie);
1737 os_free(lwork);
1738}
1739
1740
1741static void wpas_p2p_listen_work_done(struct wpa_supplicant *wpa_s)
1742{
1743 struct wpas_p2p_listen_work *lwork;
1744
1745 if (!wpa_s->p2p_listen_work)
1746 return;
1747
1748 lwork = wpa_s->p2p_listen_work->ctx;
1749 wpas_p2p_listen_work_free(lwork);
1750 radio_work_done(wpa_s->p2p_listen_work);
1751 wpa_s->p2p_listen_work = NULL;
1752}
1753
1754
1755static void wpas_start_listen_cb(struct wpa_radio_work *work, int deinit)
1756{
1757 struct wpa_supplicant *wpa_s = work->wpa_s;
1758 struct wpas_p2p_listen_work *lwork = work->ctx;
1759
1760 if (deinit) {
Dmitry Shmidtbd14a572014-02-18 10:33:49 -08001761 if (work->started) {
1762 wpa_s->p2p_listen_work = NULL;
1763 wpas_stop_listen(wpa_s);
1764 }
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001765 wpas_p2p_listen_work_free(lwork);
1766 return;
1767 }
1768
1769 wpa_s->p2p_listen_work = work;
1770
1771 wpa_drv_set_ap_wps_ie(wpa_s, NULL, lwork->probe_resp_ie, NULL);
1772
1773 if (wpa_drv_probe_req_report(wpa_s, 1) < 0) {
1774 wpa_printf(MSG_DEBUG, "P2P: Failed to request the driver to "
1775 "report received Probe Request frames");
1776 wpas_p2p_listen_work_done(wpa_s);
1777 return;
1778 }
1779
1780 wpa_s->pending_listen_freq = lwork->freq;
1781 wpa_s->pending_listen_duration = lwork->duration;
1782
1783 if (wpa_drv_remain_on_channel(wpa_s, lwork->freq, lwork->duration) < 0)
1784 {
1785 wpa_printf(MSG_DEBUG, "P2P: Failed to request the driver "
1786 "to remain on channel (%u MHz) for Listen "
1787 "state", lwork->freq);
1788 wpas_p2p_listen_work_done(wpa_s);
1789 wpa_s->pending_listen_freq = 0;
1790 return;
1791 }
1792 wpa_s->off_channel_freq = 0;
1793 wpa_s->roc_waiting_drv_freq = lwork->freq;
1794}
1795
1796
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001797static int wpas_start_listen(void *ctx, unsigned int freq,
1798 unsigned int duration,
1799 const struct wpabuf *probe_resp_ie)
1800{
1801 struct wpa_supplicant *wpa_s = ctx;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001802 struct wpas_p2p_listen_work *lwork;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001803
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001804 if (wpa_s->p2p_listen_work) {
1805 wpa_printf(MSG_DEBUG, "P2P: Reject start_listen since p2p_listen_work already exists");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001806 return -1;
1807 }
1808
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001809 lwork = os_zalloc(sizeof(*lwork));
1810 if (lwork == NULL)
1811 return -1;
1812 lwork->freq = freq;
1813 lwork->duration = duration;
1814 if (probe_resp_ie) {
1815 lwork->probe_resp_ie = wpabuf_dup(probe_resp_ie);
1816 if (lwork->probe_resp_ie == NULL) {
1817 wpas_p2p_listen_work_free(lwork);
1818 return -1;
1819 }
1820 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001821
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001822 if (radio_add_work(wpa_s, freq, "p2p-listen", 0, wpas_start_listen_cb,
1823 lwork) < 0) {
1824 wpas_p2p_listen_work_free(lwork);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001825 return -1;
1826 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001827
1828 return 0;
1829}
1830
1831
1832static void wpas_stop_listen(void *ctx)
1833{
1834 struct wpa_supplicant *wpa_s = ctx;
1835 if (wpa_s->off_channel_freq || wpa_s->roc_waiting_drv_freq) {
1836 wpa_drv_cancel_remain_on_channel(wpa_s);
1837 wpa_s->off_channel_freq = 0;
1838 wpa_s->roc_waiting_drv_freq = 0;
1839 }
1840 wpa_drv_set_ap_wps_ie(wpa_s, NULL, NULL, NULL);
1841 wpa_drv_probe_req_report(wpa_s, 0);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001842 wpas_p2p_listen_work_done(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001843}
1844
1845
1846static int wpas_send_probe_resp(void *ctx, const struct wpabuf *buf)
1847{
1848 struct wpa_supplicant *wpa_s = ctx;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001849 return wpa_drv_send_mlme(wpa_s, wpabuf_head(buf), wpabuf_len(buf), 1);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001850}
1851
1852
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001853/*
1854 * DNS Header section is used only to calculate compression pointers, so the
1855 * contents of this data does not matter, but the length needs to be reserved
1856 * in the virtual packet.
1857 */
1858#define DNS_HEADER_LEN 12
1859
1860/*
1861 * 27-octet in-memory packet from P2P specification containing two implied
1862 * queries for _tcp.lcoal. PTR IN and _udp.local. PTR IN
1863 */
1864#define P2P_SD_IN_MEMORY_LEN 27
1865
1866static int p2p_sd_dns_uncompress_label(char **upos, char *uend, u8 *start,
1867 u8 **spos, const u8 *end)
1868{
1869 while (*spos < end) {
1870 u8 val = ((*spos)[0] & 0xc0) >> 6;
1871 int len;
1872
1873 if (val == 1 || val == 2) {
1874 /* These are reserved values in RFC 1035 */
1875 wpa_printf(MSG_DEBUG, "P2P: Invalid domain name "
1876 "sequence starting with 0x%x", val);
1877 return -1;
1878 }
1879
1880 if (val == 3) {
1881 u16 offset;
1882 u8 *spos_tmp;
1883
1884 /* Offset */
1885 if (*spos + 2 > end) {
1886 wpa_printf(MSG_DEBUG, "P2P: No room for full "
1887 "DNS offset field");
1888 return -1;
1889 }
1890
1891 offset = (((*spos)[0] & 0x3f) << 8) | (*spos)[1];
1892 if (offset >= *spos - start) {
1893 wpa_printf(MSG_DEBUG, "P2P: Invalid DNS "
1894 "pointer offset %u", offset);
1895 return -1;
1896 }
1897
1898 (*spos) += 2;
1899 spos_tmp = start + offset;
1900 return p2p_sd_dns_uncompress_label(upos, uend, start,
1901 &spos_tmp,
1902 *spos - 2);
1903 }
1904
1905 /* Label */
1906 len = (*spos)[0] & 0x3f;
1907 if (len == 0)
1908 return 0;
1909
1910 (*spos)++;
1911 if (*spos + len > end) {
1912 wpa_printf(MSG_DEBUG, "P2P: Invalid domain name "
1913 "sequence - no room for label with length "
1914 "%u", len);
1915 return -1;
1916 }
1917
1918 if (*upos + len + 2 > uend)
1919 return -2;
1920
1921 os_memcpy(*upos, *spos, len);
1922 *spos += len;
1923 *upos += len;
1924 (*upos)[0] = '.';
1925 (*upos)++;
1926 (*upos)[0] = '\0';
1927 }
1928
1929 return 0;
1930}
1931
1932
1933/* Uncompress domain names per RFC 1035 using the P2P SD in-memory packet.
1934 * Returns -1 on parsing error (invalid input sequence), -2 if output buffer is
1935 * not large enough */
1936static int p2p_sd_dns_uncompress(char *buf, size_t buf_len, const u8 *msg,
1937 size_t msg_len, size_t offset)
1938{
1939 /* 27-octet in-memory packet from P2P specification */
1940 const char *prefix = "\x04_tcp\x05local\x00\x00\x0C\x00\x01"
1941 "\x04_udp\xC0\x11\x00\x0C\x00\x01";
1942 u8 *tmp, *end, *spos;
1943 char *upos, *uend;
1944 int ret = 0;
1945
1946 if (buf_len < 2)
1947 return -1;
1948 if (offset > msg_len)
1949 return -1;
1950
1951 tmp = os_malloc(DNS_HEADER_LEN + P2P_SD_IN_MEMORY_LEN + msg_len);
1952 if (tmp == NULL)
1953 return -1;
1954 spos = tmp + DNS_HEADER_LEN + P2P_SD_IN_MEMORY_LEN;
1955 end = spos + msg_len;
1956 spos += offset;
1957
1958 os_memset(tmp, 0, DNS_HEADER_LEN);
1959 os_memcpy(tmp + DNS_HEADER_LEN, prefix, P2P_SD_IN_MEMORY_LEN);
1960 os_memcpy(tmp + DNS_HEADER_LEN + P2P_SD_IN_MEMORY_LEN, msg, msg_len);
1961
1962 upos = buf;
1963 uend = buf + buf_len;
1964
1965 ret = p2p_sd_dns_uncompress_label(&upos, uend, tmp, &spos, end);
1966 if (ret) {
1967 os_free(tmp);
1968 return ret;
1969 }
1970
1971 if (upos == buf) {
1972 upos[0] = '.';
1973 upos[1] = '\0';
1974 } else if (upos[-1] == '.')
1975 upos[-1] = '\0';
1976
1977 os_free(tmp);
1978 return 0;
1979}
1980
1981
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001982static struct p2p_srv_bonjour *
1983wpas_p2p_service_get_bonjour(struct wpa_supplicant *wpa_s,
1984 const struct wpabuf *query)
1985{
1986 struct p2p_srv_bonjour *bsrv;
1987 size_t len;
1988
1989 len = wpabuf_len(query);
1990 dl_list_for_each(bsrv, &wpa_s->global->p2p_srv_bonjour,
1991 struct p2p_srv_bonjour, list) {
1992 if (len == wpabuf_len(bsrv->query) &&
1993 os_memcmp(wpabuf_head(query), wpabuf_head(bsrv->query),
1994 len) == 0)
1995 return bsrv;
1996 }
1997 return NULL;
1998}
1999
2000
2001static struct p2p_srv_upnp *
2002wpas_p2p_service_get_upnp(struct wpa_supplicant *wpa_s, u8 version,
2003 const char *service)
2004{
2005 struct p2p_srv_upnp *usrv;
2006
2007 dl_list_for_each(usrv, &wpa_s->global->p2p_srv_upnp,
2008 struct p2p_srv_upnp, list) {
2009 if (version == usrv->version &&
2010 os_strcmp(service, usrv->service) == 0)
2011 return usrv;
2012 }
2013 return NULL;
2014}
2015
2016
2017static void wpas_sd_add_proto_not_avail(struct wpabuf *resp, u8 srv_proto,
2018 u8 srv_trans_id)
2019{
2020 u8 *len_pos;
2021
2022 if (wpabuf_tailroom(resp) < 5)
2023 return;
2024
2025 /* Length (to be filled) */
2026 len_pos = wpabuf_put(resp, 2);
2027 wpabuf_put_u8(resp, srv_proto);
2028 wpabuf_put_u8(resp, srv_trans_id);
2029 /* Status Code */
2030 wpabuf_put_u8(resp, P2P_SD_PROTO_NOT_AVAILABLE);
2031 /* Response Data: empty */
2032 WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos - 2);
2033}
2034
2035
2036static void wpas_sd_all_bonjour(struct wpa_supplicant *wpa_s,
2037 struct wpabuf *resp, u8 srv_trans_id)
2038{
2039 struct p2p_srv_bonjour *bsrv;
2040 u8 *len_pos;
2041
2042 wpa_printf(MSG_DEBUG, "P2P: SD Request for all Bonjour services");
2043
2044 if (dl_list_empty(&wpa_s->global->p2p_srv_bonjour)) {
2045 wpa_printf(MSG_DEBUG, "P2P: Bonjour protocol not available");
2046 return;
2047 }
2048
2049 dl_list_for_each(bsrv, &wpa_s->global->p2p_srv_bonjour,
2050 struct p2p_srv_bonjour, list) {
2051 if (wpabuf_tailroom(resp) <
2052 5 + wpabuf_len(bsrv->query) + wpabuf_len(bsrv->resp))
2053 return;
2054 /* Length (to be filled) */
2055 len_pos = wpabuf_put(resp, 2);
2056 wpabuf_put_u8(resp, P2P_SERV_BONJOUR);
2057 wpabuf_put_u8(resp, srv_trans_id);
2058 /* Status Code */
2059 wpabuf_put_u8(resp, P2P_SD_SUCCESS);
2060 wpa_hexdump_ascii(MSG_DEBUG, "P2P: Matching Bonjour service",
2061 wpabuf_head(bsrv->resp),
2062 wpabuf_len(bsrv->resp));
2063 /* Response Data */
2064 wpabuf_put_buf(resp, bsrv->query); /* Key */
2065 wpabuf_put_buf(resp, bsrv->resp); /* Value */
2066 WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos -
2067 2);
2068 }
2069}
2070
2071
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002072static int match_bonjour_query(struct p2p_srv_bonjour *bsrv, const u8 *query,
2073 size_t query_len)
2074{
2075 char str_rx[256], str_srv[256];
2076
2077 if (query_len < 3 || wpabuf_len(bsrv->query) < 3)
2078 return 0; /* Too short to include DNS Type and Version */
2079 if (os_memcmp(query + query_len - 3,
2080 wpabuf_head_u8(bsrv->query) + wpabuf_len(bsrv->query) - 3,
2081 3) != 0)
2082 return 0; /* Mismatch in DNS Type or Version */
2083 if (query_len == wpabuf_len(bsrv->query) &&
2084 os_memcmp(query, wpabuf_head(bsrv->query), query_len - 3) == 0)
2085 return 1; /* Binary match */
2086
2087 if (p2p_sd_dns_uncompress(str_rx, sizeof(str_rx), query, query_len - 3,
2088 0))
2089 return 0; /* Failed to uncompress query */
2090 if (p2p_sd_dns_uncompress(str_srv, sizeof(str_srv),
2091 wpabuf_head(bsrv->query),
2092 wpabuf_len(bsrv->query) - 3, 0))
2093 return 0; /* Failed to uncompress service */
2094
2095 return os_strcmp(str_rx, str_srv) == 0;
2096}
2097
2098
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002099static void wpas_sd_req_bonjour(struct wpa_supplicant *wpa_s,
2100 struct wpabuf *resp, u8 srv_trans_id,
2101 const u8 *query, size_t query_len)
2102{
2103 struct p2p_srv_bonjour *bsrv;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002104 u8 *len_pos;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002105 int matches = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002106
2107 wpa_hexdump_ascii(MSG_DEBUG, "P2P: SD Request for Bonjour",
2108 query, query_len);
2109 if (dl_list_empty(&wpa_s->global->p2p_srv_bonjour)) {
2110 wpa_printf(MSG_DEBUG, "P2P: Bonjour protocol not available");
2111 wpas_sd_add_proto_not_avail(resp, P2P_SERV_BONJOUR,
2112 srv_trans_id);
2113 return;
2114 }
2115
2116 if (query_len == 0) {
2117 wpas_sd_all_bonjour(wpa_s, resp, srv_trans_id);
2118 return;
2119 }
2120
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002121 dl_list_for_each(bsrv, &wpa_s->global->p2p_srv_bonjour,
2122 struct p2p_srv_bonjour, list) {
2123 if (!match_bonjour_query(bsrv, query, query_len))
2124 continue;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002125
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002126 if (wpabuf_tailroom(resp) <
2127 5 + query_len + wpabuf_len(bsrv->resp))
2128 return;
2129
2130 matches++;
2131
2132 /* Length (to be filled) */
2133 len_pos = wpabuf_put(resp, 2);
2134 wpabuf_put_u8(resp, P2P_SERV_BONJOUR);
2135 wpabuf_put_u8(resp, srv_trans_id);
2136
2137 /* Status Code */
2138 wpabuf_put_u8(resp, P2P_SD_SUCCESS);
2139 wpa_hexdump_ascii(MSG_DEBUG, "P2P: Matching Bonjour service",
2140 wpabuf_head(bsrv->resp),
2141 wpabuf_len(bsrv->resp));
2142
2143 /* Response Data */
2144 wpabuf_put_data(resp, query, query_len); /* Key */
2145 wpabuf_put_buf(resp, bsrv->resp); /* Value */
2146
2147 WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos - 2);
2148 }
2149
2150 if (matches == 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002151 wpa_printf(MSG_DEBUG, "P2P: Requested Bonjour service not "
2152 "available");
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002153 if (wpabuf_tailroom(resp) < 5)
2154 return;
2155
2156 /* Length (to be filled) */
2157 len_pos = wpabuf_put(resp, 2);
2158 wpabuf_put_u8(resp, P2P_SERV_BONJOUR);
2159 wpabuf_put_u8(resp, srv_trans_id);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002160
2161 /* Status Code */
2162 wpabuf_put_u8(resp, P2P_SD_REQUESTED_INFO_NOT_AVAILABLE);
2163 /* Response Data: empty */
2164 WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos -
2165 2);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002166 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002167}
2168
2169
2170static void wpas_sd_all_upnp(struct wpa_supplicant *wpa_s,
2171 struct wpabuf *resp, u8 srv_trans_id)
2172{
2173 struct p2p_srv_upnp *usrv;
2174 u8 *len_pos;
2175
2176 wpa_printf(MSG_DEBUG, "P2P: SD Request for all UPnP services");
2177
2178 if (dl_list_empty(&wpa_s->global->p2p_srv_upnp)) {
2179 wpa_printf(MSG_DEBUG, "P2P: UPnP protocol not available");
2180 return;
2181 }
2182
2183 dl_list_for_each(usrv, &wpa_s->global->p2p_srv_upnp,
2184 struct p2p_srv_upnp, list) {
2185 if (wpabuf_tailroom(resp) < 5 + 1 + os_strlen(usrv->service))
2186 return;
2187
2188 /* Length (to be filled) */
2189 len_pos = wpabuf_put(resp, 2);
2190 wpabuf_put_u8(resp, P2P_SERV_UPNP);
2191 wpabuf_put_u8(resp, srv_trans_id);
2192
2193 /* Status Code */
2194 wpabuf_put_u8(resp, P2P_SD_SUCCESS);
2195 /* Response Data */
2196 wpabuf_put_u8(resp, usrv->version);
2197 wpa_printf(MSG_DEBUG, "P2P: Matching UPnP Service: %s",
2198 usrv->service);
2199 wpabuf_put_str(resp, usrv->service);
2200 WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos -
2201 2);
2202 }
2203}
2204
2205
2206static void wpas_sd_req_upnp(struct wpa_supplicant *wpa_s,
2207 struct wpabuf *resp, u8 srv_trans_id,
2208 const u8 *query, size_t query_len)
2209{
2210 struct p2p_srv_upnp *usrv;
2211 u8 *len_pos;
2212 u8 version;
2213 char *str;
2214 int count = 0;
2215
2216 wpa_hexdump_ascii(MSG_DEBUG, "P2P: SD Request for UPnP",
2217 query, query_len);
2218
2219 if (dl_list_empty(&wpa_s->global->p2p_srv_upnp)) {
2220 wpa_printf(MSG_DEBUG, "P2P: UPnP protocol not available");
2221 wpas_sd_add_proto_not_avail(resp, P2P_SERV_UPNP,
2222 srv_trans_id);
2223 return;
2224 }
2225
2226 if (query_len == 0) {
2227 wpas_sd_all_upnp(wpa_s, resp, srv_trans_id);
2228 return;
2229 }
2230
2231 if (wpabuf_tailroom(resp) < 5)
2232 return;
2233
2234 /* Length (to be filled) */
2235 len_pos = wpabuf_put(resp, 2);
2236 wpabuf_put_u8(resp, P2P_SERV_UPNP);
2237 wpabuf_put_u8(resp, srv_trans_id);
2238
2239 version = query[0];
2240 str = os_malloc(query_len);
2241 if (str == NULL)
2242 return;
2243 os_memcpy(str, query + 1, query_len - 1);
2244 str[query_len - 1] = '\0';
2245
2246 dl_list_for_each(usrv, &wpa_s->global->p2p_srv_upnp,
2247 struct p2p_srv_upnp, list) {
2248 if (version != usrv->version)
2249 continue;
2250
2251 if (os_strcmp(str, "ssdp:all") != 0 &&
2252 os_strstr(usrv->service, str) == NULL)
2253 continue;
2254
2255 if (wpabuf_tailroom(resp) < 2)
2256 break;
2257 if (count == 0) {
2258 /* Status Code */
2259 wpabuf_put_u8(resp, P2P_SD_SUCCESS);
2260 /* Response Data */
2261 wpabuf_put_u8(resp, version);
2262 } else
2263 wpabuf_put_u8(resp, ',');
2264
2265 count++;
2266
2267 wpa_printf(MSG_DEBUG, "P2P: Matching UPnP Service: %s",
2268 usrv->service);
2269 if (wpabuf_tailroom(resp) < os_strlen(usrv->service))
2270 break;
2271 wpabuf_put_str(resp, usrv->service);
2272 }
2273 os_free(str);
2274
2275 if (count == 0) {
2276 wpa_printf(MSG_DEBUG, "P2P: Requested UPnP service not "
2277 "available");
2278 /* Status Code */
2279 wpabuf_put_u8(resp, P2P_SD_REQUESTED_INFO_NOT_AVAILABLE);
2280 /* Response Data: empty */
2281 }
2282
2283 WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos - 2);
2284}
2285
2286
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002287#ifdef CONFIG_WIFI_DISPLAY
2288static void wpas_sd_req_wfd(struct wpa_supplicant *wpa_s,
2289 struct wpabuf *resp, u8 srv_trans_id,
2290 const u8 *query, size_t query_len)
2291{
2292 const u8 *pos;
2293 u8 role;
2294 u8 *len_pos;
2295
2296 wpa_hexdump(MSG_DEBUG, "P2P: SD Request for WFD", query, query_len);
2297
2298 if (!wpa_s->global->wifi_display) {
2299 wpa_printf(MSG_DEBUG, "P2P: WFD protocol not available");
2300 wpas_sd_add_proto_not_avail(resp, P2P_SERV_WIFI_DISPLAY,
2301 srv_trans_id);
2302 return;
2303 }
2304
2305 if (query_len < 1) {
2306 wpa_printf(MSG_DEBUG, "P2P: Missing WFD Requested Device "
2307 "Role");
2308 return;
2309 }
2310
2311 if (wpabuf_tailroom(resp) < 5)
2312 return;
2313
2314 pos = query;
2315 role = *pos++;
2316 wpa_printf(MSG_DEBUG, "P2P: WSD for device role 0x%x", role);
2317
2318 /* TODO: role specific handling */
2319
2320 /* Length (to be filled) */
2321 len_pos = wpabuf_put(resp, 2);
2322 wpabuf_put_u8(resp, P2P_SERV_WIFI_DISPLAY);
2323 wpabuf_put_u8(resp, srv_trans_id);
2324 wpabuf_put_u8(resp, P2P_SD_SUCCESS); /* Status Code */
2325
2326 while (pos < query + query_len) {
2327 if (*pos < MAX_WFD_SUBELEMS &&
2328 wpa_s->global->wfd_subelem[*pos] &&
2329 wpabuf_tailroom(resp) >=
2330 wpabuf_len(wpa_s->global->wfd_subelem[*pos])) {
2331 wpa_printf(MSG_DEBUG, "P2P: Add WSD response "
2332 "subelement %u", *pos);
2333 wpabuf_put_buf(resp, wpa_s->global->wfd_subelem[*pos]);
2334 }
2335 pos++;
2336 }
2337
2338 WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos - 2);
2339}
2340#endif /* CONFIG_WIFI_DISPLAY */
2341
2342
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002343static void wpas_sd_request(void *ctx, int freq, const u8 *sa, u8 dialog_token,
2344 u16 update_indic, const u8 *tlvs, size_t tlvs_len)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002345{
2346 struct wpa_supplicant *wpa_s = ctx;
2347 const u8 *pos = tlvs;
2348 const u8 *end = tlvs + tlvs_len;
2349 const u8 *tlv_end;
2350 u16 slen;
2351 struct wpabuf *resp;
2352 u8 srv_proto, srv_trans_id;
2353 size_t buf_len;
2354 char *buf;
2355
2356 wpa_hexdump(MSG_MSGDUMP, "P2P: Service Discovery Request TLVs",
2357 tlvs, tlvs_len);
2358 buf_len = 2 * tlvs_len + 1;
2359 buf = os_malloc(buf_len);
2360 if (buf) {
2361 wpa_snprintf_hex(buf, buf_len, tlvs, tlvs_len);
2362 wpa_msg_ctrl(wpa_s, MSG_INFO, P2P_EVENT_SERV_DISC_REQ "%d "
2363 MACSTR " %u %u %s",
2364 freq, MAC2STR(sa), dialog_token, update_indic,
2365 buf);
2366 os_free(buf);
2367 }
2368
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002369 if (wpa_s->p2p_sd_over_ctrl_iface) {
2370 wpas_notify_p2p_sd_request(wpa_s, freq, sa, dialog_token,
2371 update_indic, tlvs, tlvs_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002372 return; /* to be processed by an external program */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002373 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002374
2375 resp = wpabuf_alloc(10000);
2376 if (resp == NULL)
2377 return;
2378
2379 while (pos + 1 < end) {
2380 wpa_printf(MSG_DEBUG, "P2P: Service Request TLV");
2381 slen = WPA_GET_LE16(pos);
2382 pos += 2;
2383 if (pos + slen > end || slen < 2) {
2384 wpa_printf(MSG_DEBUG, "P2P: Unexpected Query Data "
2385 "length");
2386 wpabuf_free(resp);
2387 return;
2388 }
2389 tlv_end = pos + slen;
2390
2391 srv_proto = *pos++;
2392 wpa_printf(MSG_DEBUG, "P2P: Service Protocol Type %u",
2393 srv_proto);
2394 srv_trans_id = *pos++;
2395 wpa_printf(MSG_DEBUG, "P2P: Service Transaction ID %u",
2396 srv_trans_id);
2397
2398 wpa_hexdump(MSG_MSGDUMP, "P2P: Query Data",
2399 pos, tlv_end - pos);
2400
2401
2402 if (wpa_s->force_long_sd) {
2403 wpa_printf(MSG_DEBUG, "P2P: SD test - force long "
2404 "response");
2405 wpas_sd_all_bonjour(wpa_s, resp, srv_trans_id);
2406 wpas_sd_all_upnp(wpa_s, resp, srv_trans_id);
2407 goto done;
2408 }
2409
2410 switch (srv_proto) {
2411 case P2P_SERV_ALL_SERVICES:
2412 wpa_printf(MSG_DEBUG, "P2P: Service Discovery Request "
2413 "for all services");
2414 if (dl_list_empty(&wpa_s->global->p2p_srv_upnp) &&
2415 dl_list_empty(&wpa_s->global->p2p_srv_bonjour)) {
2416 wpa_printf(MSG_DEBUG, "P2P: No service "
2417 "discovery protocols available");
2418 wpas_sd_add_proto_not_avail(
2419 resp, P2P_SERV_ALL_SERVICES,
2420 srv_trans_id);
2421 break;
2422 }
2423 wpas_sd_all_bonjour(wpa_s, resp, srv_trans_id);
2424 wpas_sd_all_upnp(wpa_s, resp, srv_trans_id);
2425 break;
2426 case P2P_SERV_BONJOUR:
2427 wpas_sd_req_bonjour(wpa_s, resp, srv_trans_id,
2428 pos, tlv_end - pos);
2429 break;
2430 case P2P_SERV_UPNP:
2431 wpas_sd_req_upnp(wpa_s, resp, srv_trans_id,
2432 pos, tlv_end - pos);
2433 break;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002434#ifdef CONFIG_WIFI_DISPLAY
2435 case P2P_SERV_WIFI_DISPLAY:
2436 wpas_sd_req_wfd(wpa_s, resp, srv_trans_id,
2437 pos, tlv_end - pos);
2438 break;
2439#endif /* CONFIG_WIFI_DISPLAY */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002440 default:
2441 wpa_printf(MSG_DEBUG, "P2P: Unavailable service "
2442 "protocol %u", srv_proto);
2443 wpas_sd_add_proto_not_avail(resp, srv_proto,
2444 srv_trans_id);
2445 break;
2446 }
2447
2448 pos = tlv_end;
2449 }
2450
2451done:
2452 wpas_notify_p2p_sd_request(wpa_s, freq, sa, dialog_token,
2453 update_indic, tlvs, tlvs_len);
2454
2455 wpas_p2p_sd_response(wpa_s, freq, sa, dialog_token, resp);
2456
2457 wpabuf_free(resp);
2458}
2459
2460
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002461static void wpas_sd_response(void *ctx, const u8 *sa, u16 update_indic,
2462 const u8 *tlvs, size_t tlvs_len)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002463{
2464 struct wpa_supplicant *wpa_s = ctx;
2465 const u8 *pos = tlvs;
2466 const u8 *end = tlvs + tlvs_len;
2467 const u8 *tlv_end;
2468 u16 slen;
2469 size_t buf_len;
2470 char *buf;
2471
2472 wpa_hexdump(MSG_MSGDUMP, "P2P: Service Discovery Response TLVs",
2473 tlvs, tlvs_len);
2474 if (tlvs_len > 1500) {
2475 /* TODO: better way for handling this */
2476 wpa_msg_ctrl(wpa_s, MSG_INFO,
2477 P2P_EVENT_SERV_DISC_RESP MACSTR
2478 " %u <long response: %u bytes>",
2479 MAC2STR(sa), update_indic,
2480 (unsigned int) tlvs_len);
2481 } else {
2482 buf_len = 2 * tlvs_len + 1;
2483 buf = os_malloc(buf_len);
2484 if (buf) {
2485 wpa_snprintf_hex(buf, buf_len, tlvs, tlvs_len);
2486 wpa_msg_ctrl(wpa_s, MSG_INFO,
2487 P2P_EVENT_SERV_DISC_RESP MACSTR " %u %s",
2488 MAC2STR(sa), update_indic, buf);
2489 os_free(buf);
2490 }
2491 }
2492
2493 while (pos < end) {
2494 u8 srv_proto, srv_trans_id, status;
2495
2496 wpa_printf(MSG_DEBUG, "P2P: Service Response TLV");
2497 slen = WPA_GET_LE16(pos);
2498 pos += 2;
2499 if (pos + slen > end || slen < 3) {
2500 wpa_printf(MSG_DEBUG, "P2P: Unexpected Response Data "
2501 "length");
2502 return;
2503 }
2504 tlv_end = pos + slen;
2505
2506 srv_proto = *pos++;
2507 wpa_printf(MSG_DEBUG, "P2P: Service Protocol Type %u",
2508 srv_proto);
2509 srv_trans_id = *pos++;
2510 wpa_printf(MSG_DEBUG, "P2P: Service Transaction ID %u",
2511 srv_trans_id);
2512 status = *pos++;
2513 wpa_printf(MSG_DEBUG, "P2P: Status Code ID %u",
2514 status);
2515
2516 wpa_hexdump(MSG_MSGDUMP, "P2P: Response Data",
2517 pos, tlv_end - pos);
2518
2519 pos = tlv_end;
2520 }
2521
2522 wpas_notify_p2p_sd_response(wpa_s, sa, update_indic, tlvs, tlvs_len);
2523}
2524
2525
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002526u64 wpas_p2p_sd_request(struct wpa_supplicant *wpa_s, const u8 *dst,
2527 const struct wpabuf *tlvs)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002528{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002529 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002530 return 0;
2531 return (uintptr_t) p2p_sd_request(wpa_s->global->p2p, dst, tlvs);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002532}
2533
2534
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002535u64 wpas_p2p_sd_request_upnp(struct wpa_supplicant *wpa_s, const u8 *dst,
2536 u8 version, const char *query)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002537{
2538 struct wpabuf *tlvs;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002539 u64 ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002540
2541 tlvs = wpabuf_alloc(2 + 1 + 1 + 1 + os_strlen(query));
2542 if (tlvs == NULL)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002543 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002544 wpabuf_put_le16(tlvs, 1 + 1 + 1 + os_strlen(query));
2545 wpabuf_put_u8(tlvs, P2P_SERV_UPNP); /* Service Protocol Type */
2546 wpabuf_put_u8(tlvs, 1); /* Service Transaction ID */
2547 wpabuf_put_u8(tlvs, version);
2548 wpabuf_put_str(tlvs, query);
2549 ret = wpas_p2p_sd_request(wpa_s, dst, tlvs);
2550 wpabuf_free(tlvs);
2551 return ret;
2552}
2553
2554
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002555#ifdef CONFIG_WIFI_DISPLAY
2556
2557static u64 wpas_p2p_sd_request_wfd(struct wpa_supplicant *wpa_s, const u8 *dst,
2558 const struct wpabuf *tlvs)
2559{
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002560 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
2561 return 0;
2562 return (uintptr_t) p2p_sd_request_wfd(wpa_s->global->p2p, dst, tlvs);
2563}
2564
2565
2566#define MAX_WFD_SD_SUBELEMS 20
2567
2568static void wfd_add_sd_req_role(struct wpabuf *tlvs, u8 id, u8 role,
2569 const char *subelems)
2570{
2571 u8 *len;
2572 const char *pos;
2573 int val;
2574 int count = 0;
2575
2576 len = wpabuf_put(tlvs, 2);
2577 wpabuf_put_u8(tlvs, P2P_SERV_WIFI_DISPLAY); /* Service Protocol Type */
2578 wpabuf_put_u8(tlvs, id); /* Service Transaction ID */
2579
2580 wpabuf_put_u8(tlvs, role);
2581
2582 pos = subelems;
2583 while (*pos) {
2584 val = atoi(pos);
2585 if (val >= 0 && val < 256) {
2586 wpabuf_put_u8(tlvs, val);
2587 count++;
2588 if (count == MAX_WFD_SD_SUBELEMS)
2589 break;
2590 }
2591 pos = os_strchr(pos + 1, ',');
2592 if (pos == NULL)
2593 break;
2594 pos++;
2595 }
2596
2597 WPA_PUT_LE16(len, (u8 *) wpabuf_put(tlvs, 0) - len - 2);
2598}
2599
2600
2601u64 wpas_p2p_sd_request_wifi_display(struct wpa_supplicant *wpa_s,
2602 const u8 *dst, const char *role)
2603{
2604 struct wpabuf *tlvs;
2605 u64 ret;
2606 const char *subelems;
2607 u8 id = 1;
2608
2609 subelems = os_strchr(role, ' ');
2610 if (subelems == NULL)
2611 return 0;
2612 subelems++;
2613
2614 tlvs = wpabuf_alloc(4 * (2 + 1 + 1 + 1 + MAX_WFD_SD_SUBELEMS));
2615 if (tlvs == NULL)
2616 return 0;
2617
2618 if (os_strstr(role, "[source]"))
2619 wfd_add_sd_req_role(tlvs, id++, 0x00, subelems);
2620 if (os_strstr(role, "[pri-sink]"))
2621 wfd_add_sd_req_role(tlvs, id++, 0x01, subelems);
2622 if (os_strstr(role, "[sec-sink]"))
2623 wfd_add_sd_req_role(tlvs, id++, 0x02, subelems);
2624 if (os_strstr(role, "[source+sink]"))
2625 wfd_add_sd_req_role(tlvs, id++, 0x03, subelems);
2626
2627 ret = wpas_p2p_sd_request_wfd(wpa_s, dst, tlvs);
2628 wpabuf_free(tlvs);
2629 return ret;
2630}
2631
2632#endif /* CONFIG_WIFI_DISPLAY */
2633
2634
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002635int wpas_p2p_sd_cancel_request(struct wpa_supplicant *wpa_s, u64 req)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002636{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002637 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
2638 return -1;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002639 return p2p_sd_cancel_request(wpa_s->global->p2p,
2640 (void *) (uintptr_t) req);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002641}
2642
2643
2644void wpas_p2p_sd_response(struct wpa_supplicant *wpa_s, int freq,
2645 const u8 *dst, u8 dialog_token,
2646 const struct wpabuf *resp_tlvs)
2647{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002648 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
2649 return;
2650 p2p_sd_response(wpa_s->global->p2p, freq, dst, dialog_token,
2651 resp_tlvs);
2652}
2653
Dmitry Shmidteaf261d2013-08-14 15:30:08 -07002654
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002655void wpas_p2p_sd_service_update(struct wpa_supplicant *wpa_s)
2656{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002657 if (wpa_s->global->p2p)
2658 p2p_sd_service_update(wpa_s->global->p2p);
2659}
2660
2661
2662static void wpas_p2p_srv_bonjour_free(struct p2p_srv_bonjour *bsrv)
2663{
2664 dl_list_del(&bsrv->list);
2665 wpabuf_free(bsrv->query);
2666 wpabuf_free(bsrv->resp);
2667 os_free(bsrv);
2668}
2669
2670
2671static void wpas_p2p_srv_upnp_free(struct p2p_srv_upnp *usrv)
2672{
2673 dl_list_del(&usrv->list);
2674 os_free(usrv->service);
2675 os_free(usrv);
2676}
2677
2678
2679void wpas_p2p_service_flush(struct wpa_supplicant *wpa_s)
2680{
2681 struct p2p_srv_bonjour *bsrv, *bn;
2682 struct p2p_srv_upnp *usrv, *un;
2683
2684 dl_list_for_each_safe(bsrv, bn, &wpa_s->global->p2p_srv_bonjour,
2685 struct p2p_srv_bonjour, list)
2686 wpas_p2p_srv_bonjour_free(bsrv);
2687
2688 dl_list_for_each_safe(usrv, un, &wpa_s->global->p2p_srv_upnp,
2689 struct p2p_srv_upnp, list)
2690 wpas_p2p_srv_upnp_free(usrv);
2691
2692 wpas_p2p_sd_service_update(wpa_s);
2693}
2694
2695
2696int wpas_p2p_service_add_bonjour(struct wpa_supplicant *wpa_s,
2697 struct wpabuf *query, struct wpabuf *resp)
2698{
2699 struct p2p_srv_bonjour *bsrv;
2700
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002701 bsrv = os_zalloc(sizeof(*bsrv));
2702 if (bsrv == NULL)
2703 return -1;
2704 bsrv->query = query;
2705 bsrv->resp = resp;
2706 dl_list_add(&wpa_s->global->p2p_srv_bonjour, &bsrv->list);
2707
2708 wpas_p2p_sd_service_update(wpa_s);
2709 return 0;
2710}
2711
2712
2713int wpas_p2p_service_del_bonjour(struct wpa_supplicant *wpa_s,
2714 const struct wpabuf *query)
2715{
2716 struct p2p_srv_bonjour *bsrv;
2717
2718 bsrv = wpas_p2p_service_get_bonjour(wpa_s, query);
2719 if (bsrv == NULL)
2720 return -1;
2721 wpas_p2p_srv_bonjour_free(bsrv);
2722 wpas_p2p_sd_service_update(wpa_s);
2723 return 0;
2724}
2725
2726
2727int wpas_p2p_service_add_upnp(struct wpa_supplicant *wpa_s, u8 version,
2728 const char *service)
2729{
2730 struct p2p_srv_upnp *usrv;
2731
2732 if (wpas_p2p_service_get_upnp(wpa_s, version, service))
2733 return 0; /* Already listed */
2734 usrv = os_zalloc(sizeof(*usrv));
2735 if (usrv == NULL)
2736 return -1;
2737 usrv->version = version;
2738 usrv->service = os_strdup(service);
2739 if (usrv->service == NULL) {
2740 os_free(usrv);
2741 return -1;
2742 }
2743 dl_list_add(&wpa_s->global->p2p_srv_upnp, &usrv->list);
2744
2745 wpas_p2p_sd_service_update(wpa_s);
2746 return 0;
2747}
2748
2749
2750int wpas_p2p_service_del_upnp(struct wpa_supplicant *wpa_s, u8 version,
2751 const char *service)
2752{
2753 struct p2p_srv_upnp *usrv;
2754
2755 usrv = wpas_p2p_service_get_upnp(wpa_s, version, service);
2756 if (usrv == NULL)
2757 return -1;
2758 wpas_p2p_srv_upnp_free(usrv);
2759 wpas_p2p_sd_service_update(wpa_s);
2760 return 0;
2761}
2762
2763
2764static void wpas_prov_disc_local_display(struct wpa_supplicant *wpa_s,
2765 const u8 *peer, const char *params,
2766 unsigned int generated_pin)
2767{
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07002768 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_SHOW_PIN MACSTR
2769 " %08d%s", MAC2STR(peer), generated_pin, params);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002770}
2771
2772
2773static void wpas_prov_disc_local_keypad(struct wpa_supplicant *wpa_s,
2774 const u8 *peer, const char *params)
2775{
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07002776 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_ENTER_PIN MACSTR
2777 "%s", MAC2STR(peer), params);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002778}
2779
2780
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002781static void wpas_prov_disc_req(void *ctx, const u8 *peer, u16 config_methods,
2782 const u8 *dev_addr, const u8 *pri_dev_type,
2783 const char *dev_name, u16 supp_config_methods,
2784 u8 dev_capab, u8 group_capab, const u8 *group_id,
2785 size_t group_id_len)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002786{
2787 struct wpa_supplicant *wpa_s = ctx;
2788 char devtype[WPS_DEV_TYPE_BUFSIZE];
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002789 char params[300];
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002790 u8 empty_dev_type[8];
2791 unsigned int generated_pin = 0;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002792 struct wpa_supplicant *group = NULL;
2793
2794 if (group_id) {
2795 for (group = wpa_s->global->ifaces; group; group = group->next)
2796 {
2797 struct wpa_ssid *s = group->current_ssid;
2798 if (s != NULL &&
2799 s->mode == WPAS_MODE_P2P_GO &&
2800 group_id_len - ETH_ALEN == s->ssid_len &&
2801 os_memcmp(group_id + ETH_ALEN, s->ssid,
2802 s->ssid_len) == 0)
2803 break;
2804 }
2805 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002806
2807 if (pri_dev_type == NULL) {
2808 os_memset(empty_dev_type, 0, sizeof(empty_dev_type));
2809 pri_dev_type = empty_dev_type;
2810 }
2811 os_snprintf(params, sizeof(params), " p2p_dev_addr=" MACSTR
2812 " pri_dev_type=%s name='%s' config_methods=0x%x "
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002813 "dev_capab=0x%x group_capab=0x%x%s%s",
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002814 MAC2STR(dev_addr),
2815 wps_dev_type_bin2str(pri_dev_type, devtype,
2816 sizeof(devtype)),
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002817 dev_name, supp_config_methods, dev_capab, group_capab,
2818 group ? " group=" : "",
2819 group ? group->ifname : "");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002820 params[sizeof(params) - 1] = '\0';
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08002821
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002822 if (config_methods & WPS_CONFIG_DISPLAY) {
2823 generated_pin = wps_generate_pin();
2824 wpas_prov_disc_local_display(wpa_s, peer, params,
2825 generated_pin);
2826 } else if (config_methods & WPS_CONFIG_KEYPAD)
2827 wpas_prov_disc_local_keypad(wpa_s, peer, params);
2828 else if (config_methods & WPS_CONFIG_PUSHBUTTON)
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07002829 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_PBC_REQ
2830 MACSTR "%s", MAC2STR(peer), params);
Jouni Malinen75ecf522011-06-27 15:19:46 -07002831
2832 wpas_notify_p2p_provision_discovery(wpa_s, peer, 1 /* request */,
2833 P2P_PROV_DISC_SUCCESS,
2834 config_methods, generated_pin);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002835}
2836
2837
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002838static void wpas_prov_disc_resp(void *ctx, const u8 *peer, u16 config_methods)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002839{
2840 struct wpa_supplicant *wpa_s = ctx;
2841 unsigned int generated_pin = 0;
Dmitry Shmidt04949592012-07-19 12:16:46 -07002842 char params[20];
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002843
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08002844 if (wpa_s->pending_pd_before_join &&
2845 (os_memcmp(peer, wpa_s->pending_join_dev_addr, ETH_ALEN) == 0 ||
2846 os_memcmp(peer, wpa_s->pending_join_iface_addr, ETH_ALEN) == 0)) {
2847 wpa_s->pending_pd_before_join = 0;
2848 wpa_printf(MSG_DEBUG, "P2P: Starting pending "
2849 "join-existing-group operation");
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08002850 wpas_p2p_join_start(wpa_s, 0, NULL, 0);
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08002851 return;
2852 }
2853
Dmitry Shmidt04949592012-07-19 12:16:46 -07002854 if (wpa_s->pending_pd_use == AUTO_PD_JOIN ||
2855 wpa_s->pending_pd_use == AUTO_PD_GO_NEG)
2856 os_snprintf(params, sizeof(params), " peer_go=%d",
2857 wpa_s->pending_pd_use == AUTO_PD_JOIN);
2858 else
2859 params[0] = '\0';
2860
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002861 if (config_methods & WPS_CONFIG_DISPLAY)
Dmitry Shmidt04949592012-07-19 12:16:46 -07002862 wpas_prov_disc_local_keypad(wpa_s, peer, params);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002863 else if (config_methods & WPS_CONFIG_KEYPAD) {
2864 generated_pin = wps_generate_pin();
Dmitry Shmidt04949592012-07-19 12:16:46 -07002865 wpas_prov_disc_local_display(wpa_s, peer, params,
2866 generated_pin);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002867 } else if (config_methods & WPS_CONFIG_PUSHBUTTON)
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07002868 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_PBC_RESP
2869 MACSTR "%s", MAC2STR(peer), params);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002870
Jouni Malinen75ecf522011-06-27 15:19:46 -07002871 wpas_notify_p2p_provision_discovery(wpa_s, peer, 0 /* response */,
2872 P2P_PROV_DISC_SUCCESS,
2873 config_methods, generated_pin);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002874}
2875
2876
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002877static void wpas_prov_disc_fail(void *ctx, const u8 *peer,
2878 enum p2p_prov_disc_status status)
Jouni Malinen75ecf522011-06-27 15:19:46 -07002879{
2880 struct wpa_supplicant *wpa_s = ctx;
2881
Dmitry Shmidt04949592012-07-19 12:16:46 -07002882 if (wpa_s->p2p_fallback_to_go_neg) {
2883 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: PD for p2p_connect-auto "
2884 "failed - fall back to GO Negotiation");
2885 wpas_p2p_fallback_to_go_neg(wpa_s, 0);
2886 return;
2887 }
2888
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002889 if (status == P2P_PROV_DISC_TIMEOUT_JOIN) {
Dmitry Shmidt2b380482012-09-13 10:52:13 -07002890 wpa_s->pending_pd_before_join = 0;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002891 wpa_printf(MSG_DEBUG, "P2P: Starting pending "
2892 "join-existing-group operation (no ACK for PD "
2893 "Req attempts)");
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08002894 wpas_p2p_join_start(wpa_s, 0, NULL, 0);
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002895 return;
Dmitry Shmidt2b380482012-09-13 10:52:13 -07002896 }
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002897
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07002898 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_FAILURE
2899 " p2p_dev_addr=" MACSTR " status=%d",
2900 MAC2STR(peer), status);
Dmitry Shmidt04949592012-07-19 12:16:46 -07002901
Jouni Malinen75ecf522011-06-27 15:19:46 -07002902 wpas_notify_p2p_provision_discovery(wpa_s, peer, 0 /* response */,
2903 status, 0, 0);
2904}
2905
2906
Dmitry Shmidtf7e0a992013-05-23 11:03:10 -07002907static int freq_included(const struct p2p_channels *channels, unsigned int freq)
2908{
2909 if (channels == NULL)
2910 return 1; /* Assume no restrictions */
2911 return p2p_channels_includes_freq(channels, freq);
2912
2913}
2914
2915
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002916static u8 wpas_invitation_process(void *ctx, const u8 *sa, const u8 *bssid,
2917 const u8 *go_dev_addr, const u8 *ssid,
2918 size_t ssid_len, int *go, u8 *group_bssid,
Dmitry Shmidtf7e0a992013-05-23 11:03:10 -07002919 int *force_freq, int persistent_group,
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08002920 const struct p2p_channels *channels,
2921 int dev_pw_id)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002922{
2923 struct wpa_supplicant *wpa_s = ctx;
2924 struct wpa_ssid *s;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002925 int res;
2926 struct wpa_supplicant *grp;
2927
2928 if (!persistent_group) {
2929 wpa_printf(MSG_DEBUG, "P2P: Invitation from " MACSTR
Dmitry Shmidt344abd32014-01-14 13:17:00 -08002930 " to join an active group (SSID: %s)",
2931 MAC2STR(sa), wpa_ssid_txt(ssid, ssid_len));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002932 if (!is_zero_ether_addr(wpa_s->p2p_auth_invite) &&
2933 (os_memcmp(go_dev_addr, wpa_s->p2p_auth_invite, ETH_ALEN)
2934 == 0 ||
2935 os_memcmp(sa, wpa_s->p2p_auth_invite, ETH_ALEN) == 0)) {
2936 wpa_printf(MSG_DEBUG, "P2P: Accept previously "
2937 "authorized invitation");
2938 goto accept_inv;
2939 }
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08002940
2941#ifdef CONFIG_WPS_NFC
2942 if (dev_pw_id >= 0 && wpa_s->parent->p2p_nfc_tag_enabled &&
2943 dev_pw_id == wpa_s->parent->p2p_oob_dev_pw_id) {
2944 wpa_printf(MSG_DEBUG, "P2P: Accept invitation based on local enabled NFC Tag");
2945 wpa_s->parent->p2p_wps_method = WPS_NFC;
2946 wpa_s->parent->pending_join_wps_method = WPS_NFC;
2947 os_memcpy(wpa_s->parent->pending_join_dev_addr,
2948 go_dev_addr, ETH_ALEN);
2949 os_memcpy(wpa_s->parent->pending_join_iface_addr,
2950 bssid, ETH_ALEN);
2951 goto accept_inv;
2952 }
2953#endif /* CONFIG_WPS_NFC */
2954
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002955 /*
2956 * Do not accept the invitation automatically; notify user and
2957 * request approval.
2958 */
2959 return P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE;
2960 }
2961
2962 grp = wpas_get_p2p_group(wpa_s, ssid, ssid_len, go);
2963 if (grp) {
2964 wpa_printf(MSG_DEBUG, "P2P: Accept invitation to already "
2965 "running persistent group");
2966 if (*go)
2967 os_memcpy(group_bssid, grp->own_addr, ETH_ALEN);
2968 goto accept_inv;
2969 }
2970
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08002971 if (!is_zero_ether_addr(wpa_s->p2p_auth_invite) &&
2972 os_memcmp(sa, wpa_s->p2p_auth_invite, ETH_ALEN) == 0) {
2973 wpa_printf(MSG_DEBUG, "P2P: Accept previously initiated "
2974 "invitation to re-invoke a persistent group");
2975 } else if (!wpa_s->conf->persistent_reconnect)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002976 return P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE;
2977
2978 for (s = wpa_s->conf->ssid; s; s = s->next) {
2979 if (s->disabled == 2 &&
2980 os_memcmp(s->bssid, go_dev_addr, ETH_ALEN) == 0 &&
2981 s->ssid_len == ssid_len &&
2982 os_memcmp(ssid, s->ssid, ssid_len) == 0)
2983 break;
2984 }
2985
2986 if (!s) {
2987 wpa_printf(MSG_DEBUG, "P2P: Invitation from " MACSTR
2988 " requested reinvocation of an unknown group",
2989 MAC2STR(sa));
2990 return P2P_SC_FAIL_UNKNOWN_GROUP;
2991 }
2992
2993 if (s->mode == WPAS_MODE_P2P_GO && !wpas_p2p_create_iface(wpa_s)) {
2994 *go = 1;
2995 if (wpa_s->wpa_state >= WPA_AUTHENTICATING) {
2996 wpa_printf(MSG_DEBUG, "P2P: The only available "
2997 "interface is already in use - reject "
2998 "invitation");
2999 return P2P_SC_FAIL_UNABLE_TO_ACCOMMODATE;
3000 }
3001 os_memcpy(group_bssid, wpa_s->own_addr, ETH_ALEN);
3002 } else if (s->mode == WPAS_MODE_P2P_GO) {
3003 *go = 1;
3004 if (wpas_p2p_add_group_interface(wpa_s, WPA_IF_P2P_GO) < 0)
3005 {
3006 wpa_printf(MSG_ERROR, "P2P: Failed to allocate a new "
3007 "interface address for the group");
3008 return P2P_SC_FAIL_UNABLE_TO_ACCOMMODATE;
3009 }
3010 os_memcpy(group_bssid, wpa_s->pending_interface_addr,
3011 ETH_ALEN);
3012 }
3013
3014accept_inv:
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003015 wpas_p2p_set_own_freq_preference(wpa_s, 0);
3016
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07003017 /* Get one of the frequencies currently in use */
3018 if (wpas_p2p_valid_oper_freqs(wpa_s, &res, 1) > 0) {
Dmitry Shmidta0d265f2013-11-19 13:13:41 -08003019 wpa_printf(MSG_DEBUG, "P2P: Trying to prefer a channel already used by one of the interfaces");
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003020 wpas_p2p_set_own_freq_preference(wpa_s, res);
Dmitry Shmidta0d265f2013-11-19 13:13:41 -08003021
3022 if (wpa_s->num_multichan_concurrent < 2 ||
3023 wpas_p2p_num_unused_channels(wpa_s) < 1) {
3024 wpa_printf(MSG_DEBUG, "P2P: No extra channels available - trying to force channel to match a channel already used by one of the interfaces");
3025 *force_freq = res;
3026 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003027 }
3028
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07003029 if (*force_freq > 0 && wpa_s->num_multichan_concurrent > 1 &&
3030 wpas_p2p_num_unused_channels(wpa_s) > 0) {
Dmitry Shmidtf7e0a992013-05-23 11:03:10 -07003031 if (*go == 0) {
3032 /* We are the client */
3033 wpa_printf(MSG_DEBUG, "P2P: Peer was found to be "
3034 "running a GO but we are capable of MCC, "
3035 "figure out the best channel to use");
3036 *force_freq = 0;
3037 } else if (!freq_included(channels, *force_freq)) {
3038 /* We are the GO, and *force_freq is not in the
3039 * intersection */
3040 wpa_printf(MSG_DEBUG, "P2P: Forced GO freq %d MHz not "
3041 "in intersection but we are capable of MCC, "
3042 "figure out the best channel to use",
3043 *force_freq);
3044 *force_freq = 0;
3045 }
3046 }
3047
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003048 return P2P_SC_SUCCESS;
3049}
3050
3051
3052static void wpas_invitation_received(void *ctx, const u8 *sa, const u8 *bssid,
3053 const u8 *ssid, size_t ssid_len,
3054 const u8 *go_dev_addr, u8 status,
3055 int op_freq)
3056{
3057 struct wpa_supplicant *wpa_s = ctx;
3058 struct wpa_ssid *s;
3059
3060 for (s = wpa_s->conf->ssid; s; s = s->next) {
3061 if (s->disabled == 2 &&
3062 s->ssid_len == ssid_len &&
3063 os_memcmp(ssid, s->ssid, ssid_len) == 0)
3064 break;
3065 }
3066
3067 if (status == P2P_SC_SUCCESS) {
3068 wpa_printf(MSG_DEBUG, "P2P: Invitation from peer " MACSTR
Dmitry Shmidt344abd32014-01-14 13:17:00 -08003069 " was accepted; op_freq=%d MHz, SSID=%s",
3070 MAC2STR(sa), op_freq, wpa_ssid_txt(ssid, ssid_len));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003071 if (s) {
Dmitry Shmidt91c40cd2012-09-25 14:23:53 -07003072 int go = s->mode == WPAS_MODE_P2P_GO;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003073 wpas_p2p_group_add_persistent(
Dmitry Shmidt96be6222014-02-13 10:16:51 -08003074 wpa_s, s, go, 0, go ? op_freq : 0, 0, 0, NULL,
Dmitry Shmidt56052862013-10-04 10:23:25 -07003075 go ? P2P_MAX_INITIAL_CONN_WAIT_GO_REINVOKE : 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003076 } else if (bssid) {
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08003077 wpa_s->user_initiated_pd = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003078 wpas_p2p_join(wpa_s, bssid, go_dev_addr,
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08003079 wpa_s->p2p_wps_method, 0, op_freq,
Dmitry Shmidt344abd32014-01-14 13:17:00 -08003080 ssid, ssid_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003081 }
3082 return;
3083 }
3084
3085 if (status != P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE) {
3086 wpa_printf(MSG_DEBUG, "P2P: Invitation from peer " MACSTR
3087 " was rejected (status %u)", MAC2STR(sa), status);
3088 return;
3089 }
3090
3091 if (!s) {
3092 if (bssid) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07003093 wpa_msg_global(wpa_s, MSG_INFO,
3094 P2P_EVENT_INVITATION_RECEIVED
3095 "sa=" MACSTR " go_dev_addr=" MACSTR
3096 " bssid=" MACSTR " unknown-network",
3097 MAC2STR(sa), MAC2STR(go_dev_addr),
3098 MAC2STR(bssid));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003099 } else {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07003100 wpa_msg_global(wpa_s, MSG_INFO,
3101 P2P_EVENT_INVITATION_RECEIVED
3102 "sa=" MACSTR " go_dev_addr=" MACSTR
3103 " unknown-network",
3104 MAC2STR(sa), MAC2STR(go_dev_addr));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003105 }
3106 return;
3107 }
3108
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08003109 if (s->mode == WPAS_MODE_P2P_GO && op_freq) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07003110 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_INVITATION_RECEIVED
3111 "sa=" MACSTR " persistent=%d freq=%d",
3112 MAC2STR(sa), s->id, op_freq);
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08003113 } else {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07003114 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_INVITATION_RECEIVED
3115 "sa=" MACSTR " persistent=%d",
3116 MAC2STR(sa), s->id);
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08003117 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003118}
3119
3120
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003121static void wpas_remove_persistent_peer(struct wpa_supplicant *wpa_s,
3122 struct wpa_ssid *ssid,
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07003123 const u8 *peer, int inv)
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003124{
3125 size_t i;
3126
3127 if (ssid == NULL)
3128 return;
3129
3130 for (i = 0; ssid->p2p_client_list && i < ssid->num_p2p_clients; i++) {
3131 if (os_memcmp(ssid->p2p_client_list + i * ETH_ALEN, peer,
3132 ETH_ALEN) == 0)
3133 break;
3134 }
3135 if (i >= ssid->num_p2p_clients) {
3136 if (ssid->mode != WPAS_MODE_P2P_GO &&
3137 os_memcmp(ssid->bssid, peer, ETH_ALEN) == 0) {
3138 wpa_printf(MSG_DEBUG, "P2P: Remove persistent group %d "
3139 "due to invitation result", ssid->id);
3140 wpas_notify_network_removed(wpa_s, ssid);
3141 wpa_config_remove_network(wpa_s->conf, ssid->id);
3142 return;
3143 }
3144 return; /* Peer not found in client list */
3145 }
3146
3147 wpa_printf(MSG_DEBUG, "P2P: Remove peer " MACSTR " from persistent "
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07003148 "group %d client list%s",
3149 MAC2STR(peer), ssid->id,
3150 inv ? " due to invitation result" : "");
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003151 os_memmove(ssid->p2p_client_list + i * ETH_ALEN,
3152 ssid->p2p_client_list + (i + 1) * ETH_ALEN,
3153 (ssid->num_p2p_clients - i - 1) * ETH_ALEN);
3154 ssid->num_p2p_clients--;
3155#ifndef CONFIG_NO_CONFIG_WRITE
3156 if (wpa_s->parent->conf->update_config &&
3157 wpa_config_write(wpa_s->parent->confname, wpa_s->parent->conf))
3158 wpa_printf(MSG_DEBUG, "P2P: Failed to update configuration");
3159#endif /* CONFIG_NO_CONFIG_WRITE */
3160}
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 Shmidte0e48dc2013-11-18 12:00:06 -08003183 const u8 *peer, int neg_freq)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003184{
3185 struct wpa_supplicant *wpa_s = ctx;
3186 struct wpa_ssid *ssid;
3187
3188 if (bssid) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07003189 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_INVITATION_RESULT
3190 "status=%d " MACSTR,
3191 status, MAC2STR(bssid));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003192 } else {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07003193 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_INVITATION_RESULT
3194 "status=%d ", status);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003195 }
3196 wpas_notify_p2p_invitation_result(wpa_s, status, bssid);
3197
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003198 wpa_printf(MSG_DEBUG, "P2P: Invitation result - status=%d peer=" MACSTR,
3199 status, MAC2STR(peer));
3200 if (wpa_s->pending_invite_ssid_id == -1) {
3201 if (status == P2P_SC_FAIL_UNKNOWN_GROUP)
3202 wpas_remove_persistent_client(wpa_s, peer);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003203 return; /* Invitation to active group */
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003204 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003205
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08003206 if (status == P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE) {
3207 wpa_printf(MSG_DEBUG, "P2P: Waiting for peer to start another "
3208 "invitation exchange to indicate readiness for "
3209 "re-invocation");
3210 }
3211
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003212 if (status != P2P_SC_SUCCESS) {
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003213 if (status == P2P_SC_FAIL_UNKNOWN_GROUP) {
3214 ssid = wpa_config_get_network(
3215 wpa_s->conf, wpa_s->pending_invite_ssid_id);
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07003216 wpas_remove_persistent_peer(wpa_s, ssid, peer, 1);
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003217 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003218 wpas_p2p_remove_pending_group_interface(wpa_s);
3219 return;
3220 }
3221
3222 ssid = wpa_config_get_network(wpa_s->conf,
3223 wpa_s->pending_invite_ssid_id);
3224 if (ssid == NULL) {
3225 wpa_printf(MSG_ERROR, "P2P: Could not find persistent group "
3226 "data matching with invitation");
3227 return;
3228 }
3229
Jouni Malinen2c5b17d2012-10-13 21:52:46 -07003230 /*
3231 * The peer could have missed our ctrl::ack frame for Invitation
3232 * Response and continue retransmitting the frame. To reduce the
3233 * likelihood of the peer not getting successful TX status for the
3234 * Invitation Response frame, wait a short time here before starting
3235 * the persistent group so that we will remain on the current channel to
3236 * acknowledge any possible retransmission from the peer.
3237 */
3238 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: 50 ms wait on current channel before "
3239 "starting persistent group");
3240 os_sleep(0, 50000);
3241
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003242 wpas_p2p_group_add_persistent(wpa_s, ssid,
Jouni Malinen31be0a42012-08-31 21:20:51 +03003243 ssid->mode == WPAS_MODE_P2P_GO,
Dmitry Shmidt96be6222014-02-13 10:16:51 -08003244 wpa_s->p2p_persistent_go_freq,
3245 neg_freq,
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003246 wpa_s->p2p_go_ht40, wpa_s->p2p_go_vht,
3247 channels,
Dmitry Shmidt56052862013-10-04 10:23:25 -07003248 ssid->mode == WPAS_MODE_P2P_GO ?
3249 P2P_MAX_INITIAL_CONN_WAIT_GO_REINVOKE :
3250 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003251}
3252
3253
Dmitry Shmidt04949592012-07-19 12:16:46 -07003254static int wpas_p2p_disallowed_freq(struct wpa_global *global,
3255 unsigned int freq)
3256{
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08003257 if (freq_range_list_includes(&global->p2p_go_avoid_freq, freq))
3258 return 1;
Dmitry Shmidt4ce9c872013-10-24 11:08:13 -07003259 return freq_range_list_includes(&global->p2p_disallow_freq, freq);
Dmitry Shmidt04949592012-07-19 12:16:46 -07003260}
3261
3262
3263static void wpas_p2p_add_chan(struct p2p_reg_class *reg, u8 chan)
3264{
3265 reg->channel[reg->channels] = chan;
3266 reg->channels++;
3267}
3268
3269
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003270static int wpas_p2p_default_channels(struct wpa_supplicant *wpa_s,
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003271 struct p2p_channels *chan,
3272 struct p2p_channels *cli_chan)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003273{
3274 int i, cla = 0;
3275
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003276 os_memset(cli_chan, 0, sizeof(*cli_chan));
3277
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003278 wpa_printf(MSG_DEBUG, "P2P: Enable operating classes for 2.4 GHz "
3279 "band");
3280
3281 /* Operating class 81 - 2.4 GHz band channels 1..13 */
3282 chan->reg_class[cla].reg_class = 81;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003283 chan->reg_class[cla].channels = 0;
3284 for (i = 0; i < 11; i++) {
3285 if (!wpas_p2p_disallowed_freq(wpa_s->global, 2412 + i * 5))
3286 wpas_p2p_add_chan(&chan->reg_class[cla], i + 1);
3287 }
3288 if (chan->reg_class[cla].channels)
3289 cla++;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003290
3291 wpa_printf(MSG_DEBUG, "P2P: Enable operating classes for lower 5 GHz "
3292 "band");
3293
3294 /* Operating class 115 - 5 GHz, channels 36-48 */
3295 chan->reg_class[cla].reg_class = 115;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003296 chan->reg_class[cla].channels = 0;
3297 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 36 * 5))
3298 wpas_p2p_add_chan(&chan->reg_class[cla], 36);
3299 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 40 * 5))
3300 wpas_p2p_add_chan(&chan->reg_class[cla], 40);
3301 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 44 * 5))
3302 wpas_p2p_add_chan(&chan->reg_class[cla], 44);
3303 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 48 * 5))
3304 wpas_p2p_add_chan(&chan->reg_class[cla], 48);
3305 if (chan->reg_class[cla].channels)
3306 cla++;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003307
3308 wpa_printf(MSG_DEBUG, "P2P: Enable operating classes for higher 5 GHz "
3309 "band");
3310
3311 /* Operating class 124 - 5 GHz, channels 149,153,157,161 */
3312 chan->reg_class[cla].reg_class = 124;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003313 chan->reg_class[cla].channels = 0;
3314 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 149 * 5))
3315 wpas_p2p_add_chan(&chan->reg_class[cla], 149);
3316 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 153 * 5))
3317 wpas_p2p_add_chan(&chan->reg_class[cla], 153);
3318 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 156 * 5))
3319 wpas_p2p_add_chan(&chan->reg_class[cla], 157);
3320 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 161 * 5))
3321 wpas_p2p_add_chan(&chan->reg_class[cla], 161);
3322 if (chan->reg_class[cla].channels)
3323 cla++;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003324
3325 chan->reg_classes = cla;
3326 return 0;
3327}
3328
3329
3330static struct hostapd_hw_modes * get_mode(struct hostapd_hw_modes *modes,
3331 u16 num_modes,
3332 enum hostapd_hw_mode mode)
3333{
3334 u16 i;
3335
3336 for (i = 0; i < num_modes; i++) {
3337 if (modes[i].mode == mode)
3338 return &modes[i];
3339 }
3340
3341 return NULL;
3342}
3343
3344
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003345enum chan_allowed {
3346 NOT_ALLOWED, PASSIVE_ONLY, ALLOWED
3347};
3348
Dmitry Shmidt04949592012-07-19 12:16:46 -07003349static int has_channel(struct wpa_global *global,
3350 struct hostapd_hw_modes *mode, u8 chan, int *flags)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003351{
3352 int i;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003353 unsigned int freq;
3354
3355 freq = (mode->mode == HOSTAPD_MODE_IEEE80211A ? 5000 : 2407) +
3356 chan * 5;
3357 if (wpas_p2p_disallowed_freq(global, freq))
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003358 return NOT_ALLOWED;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003359
3360 for (i = 0; i < mode->num_channels; i++) {
3361 if (mode->channels[i].chan == chan) {
3362 if (flags)
3363 *flags = mode->channels[i].flag;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003364 if (mode->channels[i].flag &
3365 (HOSTAPD_CHAN_DISABLED |
3366 HOSTAPD_CHAN_RADAR))
3367 return NOT_ALLOWED;
3368 if (mode->channels[i].flag &
3369 (HOSTAPD_CHAN_PASSIVE_SCAN |
3370 HOSTAPD_CHAN_NO_IBSS))
3371 return PASSIVE_ONLY;
3372 return ALLOWED;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003373 }
3374 }
3375
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003376 return NOT_ALLOWED;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003377}
3378
3379
3380struct p2p_oper_class_map {
3381 enum hostapd_hw_mode mode;
3382 u8 op_class;
3383 u8 min_chan;
3384 u8 max_chan;
3385 u8 inc;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003386 enum { BW20, BW40PLUS, BW40MINUS, BW80 } bw;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003387};
3388
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003389static struct p2p_oper_class_map op_class[] = {
3390 { HOSTAPD_MODE_IEEE80211G, 81, 1, 13, 1, BW20 },
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003391#if 0 /* Do not enable HT40 on 2 GHz for now */
3392 { HOSTAPD_MODE_IEEE80211G, 83, 1, 9, 1, BW40PLUS },
3393 { HOSTAPD_MODE_IEEE80211G, 84, 5, 13, 1, BW40MINUS },
3394#endif
3395 { HOSTAPD_MODE_IEEE80211A, 115, 36, 48, 4, BW20 },
3396 { HOSTAPD_MODE_IEEE80211A, 124, 149, 161, 4, BW20 },
3397 { HOSTAPD_MODE_IEEE80211A, 116, 36, 44, 8, BW40PLUS },
3398 { HOSTAPD_MODE_IEEE80211A, 117, 40, 48, 8, BW40MINUS },
3399 { HOSTAPD_MODE_IEEE80211A, 126, 149, 157, 8, BW40PLUS },
3400 { HOSTAPD_MODE_IEEE80211A, 127, 153, 161, 8, BW40MINUS },
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003401
3402 /*
3403 * IEEE P802.11ac/D7.0 Table E-4 actually talks about channel center
3404 * frequency index 42, 58, 106, 122, 138, 155 with channel spacing of
3405 * 80 MHz, but currently use the following definition for simplicity
3406 * (these center frequencies are not actual channels, which makes
3407 * has_channel() fail). wpas_p2p_verify_80mhz() should take care of
3408 * removing invalid channels.
3409 */
3410 { HOSTAPD_MODE_IEEE80211A, 128, 36, 161, 4, BW80 },
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003411 { -1, 0, 0, 0, 0, BW20 }
3412};
3413
3414
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003415static int wpas_p2p_get_center_80mhz(struct wpa_supplicant *wpa_s,
3416 struct hostapd_hw_modes *mode,
3417 u8 channel)
3418{
3419 u8 center_channels[] = { 42, 58, 106, 122, 138, 155 };
3420 unsigned int i;
3421
3422 if (mode->mode != HOSTAPD_MODE_IEEE80211A)
3423 return 0;
3424
3425 for (i = 0; i < ARRAY_SIZE(center_channels); i++)
3426 /*
3427 * In 80 MHz, the bandwidth "spans" 12 channels (e.g., 36-48),
3428 * so the center channel is 6 channels away from the start/end.
3429 */
3430 if (channel >= center_channels[i] - 6 &&
3431 channel <= center_channels[i] + 6)
3432 return center_channels[i];
3433
3434 return 0;
3435}
3436
3437
3438static enum chan_allowed wpas_p2p_verify_80mhz(struct wpa_supplicant *wpa_s,
3439 struct hostapd_hw_modes *mode,
3440 u8 channel, u8 bw)
3441{
3442 u8 center_chan;
3443 int i, flags;
3444 enum chan_allowed res, ret = ALLOWED;
3445
3446 center_chan = wpas_p2p_get_center_80mhz(wpa_s, mode, channel);
3447 if (!center_chan)
3448 return NOT_ALLOWED;
3449 if (center_chan >= 58 && center_chan <= 138)
3450 return NOT_ALLOWED; /* Do not allow DFS channels for P2P */
3451
3452 /* check all the channels are available */
3453 for (i = 0; i < 4; i++) {
3454 int adj_chan = center_chan - 6 + i * 4;
3455
3456 res = has_channel(wpa_s->global, mode, adj_chan, &flags);
3457 if (res == NOT_ALLOWED)
3458 return NOT_ALLOWED;
3459 if (res == PASSIVE_ONLY)
3460 ret = PASSIVE_ONLY;
3461
3462 if (i == 0 && !(flags & HOSTAPD_CHAN_VHT_10_70))
3463 return NOT_ALLOWED;
3464 if (i == 1 && !(flags & HOSTAPD_CHAN_VHT_30_50))
3465 return NOT_ALLOWED;
3466 if (i == 2 && !(flags & HOSTAPD_CHAN_VHT_50_30))
3467 return NOT_ALLOWED;
3468 if (i == 3 && !(flags & HOSTAPD_CHAN_VHT_70_10))
3469 return NOT_ALLOWED;
3470 }
3471
3472 return ret;
3473}
3474
3475
3476static enum chan_allowed wpas_p2p_verify_channel(struct wpa_supplicant *wpa_s,
3477 struct hostapd_hw_modes *mode,
3478 u8 channel, u8 bw)
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003479{
Dmitry Shmidt96be6222014-02-13 10:16:51 -08003480 int flag = 0;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003481 enum chan_allowed res, res2;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003482
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003483 res2 = res = has_channel(wpa_s->global, mode, channel, &flag);
3484 if (bw == BW40MINUS) {
3485 if (!(flag & HOSTAPD_CHAN_HT40MINUS))
3486 return NOT_ALLOWED;
3487 res2 = has_channel(wpa_s->global, mode, channel - 4, NULL);
3488 } else if (bw == BW40PLUS) {
3489 if (!(flag & HOSTAPD_CHAN_HT40PLUS))
3490 return NOT_ALLOWED;
3491 res2 = has_channel(wpa_s->global, mode, channel + 4, NULL);
3492 } else if (bw == BW80) {
3493 res2 = wpas_p2p_verify_80mhz(wpa_s, mode, channel, bw);
3494 }
3495
3496 if (res == NOT_ALLOWED || res2 == NOT_ALLOWED)
3497 return NOT_ALLOWED;
3498 if (res == PASSIVE_ONLY || res2 == PASSIVE_ONLY)
3499 return PASSIVE_ONLY;
3500 return res;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003501}
3502
3503
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003504static int wpas_p2p_setup_channels(struct wpa_supplicant *wpa_s,
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003505 struct p2p_channels *chan,
3506 struct p2p_channels *cli_chan)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003507{
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003508 struct hostapd_hw_modes *mode;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003509 int cla, op, cli_cla;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003510
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003511 if (wpa_s->hw.modes == NULL) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003512 wpa_printf(MSG_DEBUG, "P2P: Driver did not support fetching "
3513 "of all supported channels; assume dualband "
3514 "support");
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003515 return wpas_p2p_default_channels(wpa_s, chan, cli_chan);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003516 }
3517
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003518 cla = cli_cla = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003519
3520 for (op = 0; op_class[op].op_class; op++) {
3521 struct p2p_oper_class_map *o = &op_class[op];
3522 u8 ch;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003523 struct p2p_reg_class *reg = NULL, *cli_reg = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003524
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003525 mode = get_mode(wpa_s->hw.modes, wpa_s->hw.num_modes, o->mode);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003526 if (mode == NULL)
3527 continue;
3528 for (ch = o->min_chan; ch <= o->max_chan; ch += o->inc) {
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003529 enum chan_allowed res;
3530 res = wpas_p2p_verify_channel(wpa_s, mode, ch, o->bw);
3531 if (res == ALLOWED) {
3532 if (reg == NULL) {
3533 wpa_printf(MSG_DEBUG, "P2P: Add operating class %u",
3534 o->op_class);
3535 reg = &chan->reg_class[cla];
3536 cla++;
3537 reg->reg_class = o->op_class;
3538 }
3539 reg->channel[reg->channels] = ch;
3540 reg->channels++;
3541 } else if (res == PASSIVE_ONLY &&
3542 wpa_s->conf->p2p_add_cli_chan) {
3543 if (cli_reg == NULL) {
3544 wpa_printf(MSG_DEBUG, "P2P: Add operating class %u (client only)",
3545 o->op_class);
3546 cli_reg = &cli_chan->reg_class[cli_cla];
3547 cli_cla++;
3548 cli_reg->reg_class = o->op_class;
3549 }
3550 cli_reg->channel[cli_reg->channels] = ch;
3551 cli_reg->channels++;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003552 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003553 }
3554 if (reg) {
3555 wpa_hexdump(MSG_DEBUG, "P2P: Channels",
3556 reg->channel, reg->channels);
3557 }
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003558 if (cli_reg) {
3559 wpa_hexdump(MSG_DEBUG, "P2P: Channels (client only)",
3560 cli_reg->channel, cli_reg->channels);
3561 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003562 }
3563
3564 chan->reg_classes = cla;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003565 cli_chan->reg_classes = cli_cla;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003566
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003567 return 0;
3568}
3569
3570
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003571int wpas_p2p_get_ht40_mode(struct wpa_supplicant *wpa_s,
3572 struct hostapd_hw_modes *mode, u8 channel)
3573{
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003574 int op;
3575 enum chan_allowed ret;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003576
3577 for (op = 0; op_class[op].op_class; op++) {
3578 struct p2p_oper_class_map *o = &op_class[op];
3579 u8 ch;
3580
3581 for (ch = o->min_chan; ch <= o->max_chan; ch += o->inc) {
3582 if (o->mode != HOSTAPD_MODE_IEEE80211A ||
3583 o->bw == BW20 || ch != channel)
3584 continue;
3585 ret = wpas_p2p_verify_channel(wpa_s, mode, ch, o->bw);
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003586 if (ret == ALLOWED)
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003587 return (o->bw == BW40MINUS) ? -1 : 1;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003588 }
3589 }
3590 return 0;
3591}
3592
3593
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003594int wpas_p2p_get_vht80_center(struct wpa_supplicant *wpa_s,
3595 struct hostapd_hw_modes *mode, u8 channel)
3596{
3597 if (!wpas_p2p_verify_channel(wpa_s, mode, channel, BW80))
3598 return 0;
3599
3600 return wpas_p2p_get_center_80mhz(wpa_s, mode, channel);
3601}
3602
3603
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003604static int wpas_get_noa(void *ctx, const u8 *interface_addr, u8 *buf,
3605 size_t buf_len)
3606{
3607 struct wpa_supplicant *wpa_s = ctx;
3608
3609 for (wpa_s = wpa_s->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
3610 if (os_memcmp(wpa_s->own_addr, interface_addr, ETH_ALEN) == 0)
3611 break;
3612 }
3613 if (wpa_s == NULL)
3614 return -1;
3615
3616 return wpa_drv_get_noa(wpa_s, buf, buf_len);
3617}
3618
3619
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08003620static int wpas_go_connected(void *ctx, const u8 *dev_addr)
3621{
3622 struct wpa_supplicant *wpa_s = ctx;
3623
3624 for (wpa_s = wpa_s->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
3625 struct wpa_ssid *ssid = wpa_s->current_ssid;
3626 if (ssid == NULL)
3627 continue;
3628 if (ssid->mode != WPAS_MODE_INFRA)
3629 continue;
3630 if (wpa_s->wpa_state != WPA_COMPLETED &&
3631 wpa_s->wpa_state != WPA_GROUP_HANDSHAKE)
3632 continue;
3633 if (os_memcmp(wpa_s->go_dev_addr, dev_addr, ETH_ALEN) == 0)
3634 return 1;
3635 }
3636
3637 return 0;
3638}
3639
3640
Dmitry Shmidt18463232014-01-24 12:29:41 -08003641static int wpas_is_concurrent_session_active(void *ctx)
3642{
3643 struct wpa_supplicant *wpa_s = ctx;
3644 struct wpa_supplicant *ifs;
3645
3646 for (ifs = wpa_s->global->ifaces; ifs; ifs = ifs->next) {
3647 if (ifs == wpa_s)
3648 continue;
3649 if (ifs->wpa_state > WPA_ASSOCIATED)
3650 return 1;
3651 }
3652 return 0;
3653}
3654
3655
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07003656static void wpas_p2p_debug_print(void *ctx, int level, const char *msg)
3657{
3658 struct wpa_supplicant *wpa_s = ctx;
3659 wpa_msg_global(wpa_s, level, "P2P: %s", msg);
3660}
3661
3662
Dmitry Shmidt34af3062013-07-11 10:46:32 -07003663int wpas_p2p_add_p2pdev_interface(struct wpa_supplicant *wpa_s)
3664{
3665 struct wpa_interface iface;
3666 struct wpa_supplicant *p2pdev_wpa_s;
3667 char ifname[100];
3668 char force_name[100];
3669 int ret;
3670
3671 os_snprintf(ifname, sizeof(ifname), P2P_MGMT_DEVICE_PREFIX "%s",
3672 wpa_s->ifname);
3673 force_name[0] = '\0';
3674 wpa_s->pending_interface_type = WPA_IF_P2P_DEVICE;
3675 ret = wpa_drv_if_add(wpa_s, WPA_IF_P2P_DEVICE, ifname, NULL, NULL,
3676 force_name, wpa_s->pending_interface_addr, NULL);
3677 if (ret < 0) {
3678 wpa_printf(MSG_DEBUG, "P2P: Failed to create P2P Device interface");
3679 return ret;
3680 }
3681 os_strlcpy(wpa_s->pending_interface_name, ifname,
3682 sizeof(wpa_s->pending_interface_name));
3683
3684 os_memset(&iface, 0, sizeof(iface));
3685 iface.p2p_mgmt = 1;
3686 iface.ifname = wpa_s->pending_interface_name;
3687 iface.driver = wpa_s->driver->name;
3688 iface.driver_param = wpa_s->conf->driver_param;
3689 iface.confname = wpa_s->confname;
3690 p2pdev_wpa_s = wpa_supplicant_add_iface(wpa_s->global, &iface);
3691 if (!p2pdev_wpa_s) {
3692 wpa_printf(MSG_DEBUG, "P2P: Failed to add P2P Device interface");
3693 return -1;
3694 }
3695 p2pdev_wpa_s->parent = wpa_s;
3696
3697 wpa_s->pending_interface_name[0] = '\0';
3698 return 0;
3699}
3700
3701
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08003702static void wpas_presence_resp(void *ctx, const u8 *src, u8 status,
3703 const u8 *noa, size_t noa_len)
3704{
3705 struct wpa_supplicant *wpa_s, *intf = ctx;
3706 char hex[100];
3707
3708 for (wpa_s = intf->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
3709 if (wpa_s->waiting_presence_resp)
3710 break;
3711 }
3712 if (!wpa_s) {
3713 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: No group interface was waiting for presence response");
3714 return;
3715 }
3716 wpa_s->waiting_presence_resp = 0;
3717
3718 wpa_snprintf_hex(hex, sizeof(hex), noa, noa_len);
3719 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_PRESENCE_RESPONSE "src=" MACSTR
3720 " status=%u noa=%s", MAC2STR(src), status, hex);
3721}
3722
3723
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003724/**
3725 * wpas_p2p_init - Initialize P2P module for %wpa_supplicant
3726 * @global: Pointer to global data from wpa_supplicant_init()
3727 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
3728 * Returns: 0 on success, -1 on failure
3729 */
3730int wpas_p2p_init(struct wpa_global *global, struct wpa_supplicant *wpa_s)
3731{
3732 struct p2p_config p2p;
3733 unsigned int r;
3734 int i;
3735
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07003736 if (wpa_s->conf->p2p_disabled)
3737 return 0;
3738
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003739 if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_CAPABLE))
3740 return 0;
3741
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003742 if (global->p2p)
3743 return 0;
3744
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003745 os_memset(&p2p, 0, sizeof(p2p));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003746 p2p.cb_ctx = wpa_s;
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07003747 p2p.debug_print = wpas_p2p_debug_print;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003748 p2p.p2p_scan = wpas_p2p_scan;
3749 p2p.send_action = wpas_send_action;
3750 p2p.send_action_done = wpas_send_action_done;
3751 p2p.go_neg_completed = wpas_go_neg_completed;
3752 p2p.go_neg_req_rx = wpas_go_neg_req_rx;
3753 p2p.dev_found = wpas_dev_found;
3754 p2p.dev_lost = wpas_dev_lost;
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07003755 p2p.find_stopped = wpas_find_stopped;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003756 p2p.start_listen = wpas_start_listen;
3757 p2p.stop_listen = wpas_stop_listen;
3758 p2p.send_probe_resp = wpas_send_probe_resp;
3759 p2p.sd_request = wpas_sd_request;
3760 p2p.sd_response = wpas_sd_response;
3761 p2p.prov_disc_req = wpas_prov_disc_req;
3762 p2p.prov_disc_resp = wpas_prov_disc_resp;
Jouni Malinen75ecf522011-06-27 15:19:46 -07003763 p2p.prov_disc_fail = wpas_prov_disc_fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003764 p2p.invitation_process = wpas_invitation_process;
3765 p2p.invitation_received = wpas_invitation_received;
3766 p2p.invitation_result = wpas_invitation_result;
3767 p2p.get_noa = wpas_get_noa;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08003768 p2p.go_connected = wpas_go_connected;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08003769 p2p.presence_resp = wpas_presence_resp;
Dmitry Shmidt18463232014-01-24 12:29:41 -08003770 p2p.is_concurrent_session_active = wpas_is_concurrent_session_active;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003771
3772 os_memcpy(wpa_s->global->p2p_dev_addr, wpa_s->own_addr, ETH_ALEN);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003773 os_memcpy(p2p.dev_addr, wpa_s->global->p2p_dev_addr, ETH_ALEN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003774 p2p.dev_name = wpa_s->conf->device_name;
3775 p2p.manufacturer = wpa_s->conf->manufacturer;
3776 p2p.model_name = wpa_s->conf->model_name;
3777 p2p.model_number = wpa_s->conf->model_number;
3778 p2p.serial_number = wpa_s->conf->serial_number;
3779 if (wpa_s->wps) {
3780 os_memcpy(p2p.uuid, wpa_s->wps->uuid, 16);
3781 p2p.config_methods = wpa_s->wps->config_methods;
3782 }
3783
3784 if (wpa_s->conf->p2p_listen_reg_class &&
3785 wpa_s->conf->p2p_listen_channel) {
3786 p2p.reg_class = wpa_s->conf->p2p_listen_reg_class;
3787 p2p.channel = wpa_s->conf->p2p_listen_channel;
3788 } else {
3789 p2p.reg_class = 81;
3790 /*
3791 * Pick one of the social channels randomly as the listen
3792 * channel.
3793 */
3794 os_get_random((u8 *) &r, sizeof(r));
3795 p2p.channel = 1 + (r % 3) * 5;
3796 }
3797 wpa_printf(MSG_DEBUG, "P2P: Own listen channel: %d", p2p.channel);
3798
3799 if (wpa_s->conf->p2p_oper_reg_class &&
3800 wpa_s->conf->p2p_oper_channel) {
3801 p2p.op_reg_class = wpa_s->conf->p2p_oper_reg_class;
3802 p2p.op_channel = wpa_s->conf->p2p_oper_channel;
3803 p2p.cfg_op_channel = 1;
3804 wpa_printf(MSG_DEBUG, "P2P: Configured operating channel: "
3805 "%d:%d", p2p.op_reg_class, p2p.op_channel);
3806
3807 } else {
3808 p2p.op_reg_class = 81;
3809 /*
3810 * Use random operation channel from (1, 6, 11) if no other
3811 * preference is indicated.
3812 */
3813 os_get_random((u8 *) &r, sizeof(r));
3814 p2p.op_channel = 1 + (r % 3) * 5;
3815 p2p.cfg_op_channel = 0;
3816 wpa_printf(MSG_DEBUG, "P2P: Random operating channel: "
3817 "%d:%d", p2p.op_reg_class, p2p.op_channel);
3818 }
Dmitry Shmidt44c95782013-05-17 09:51:35 -07003819
3820 if (wpa_s->conf->p2p_pref_chan && wpa_s->conf->num_p2p_pref_chan) {
3821 p2p.pref_chan = wpa_s->conf->p2p_pref_chan;
3822 p2p.num_pref_chan = wpa_s->conf->num_p2p_pref_chan;
3823 }
3824
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003825 if (wpa_s->conf->country[0] && wpa_s->conf->country[1]) {
3826 os_memcpy(p2p.country, wpa_s->conf->country, 2);
3827 p2p.country[2] = 0x04;
3828 } else
3829 os_memcpy(p2p.country, "XX\x04", 3);
3830
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003831 if (wpas_p2p_setup_channels(wpa_s, &p2p.channels, &p2p.cli_channels)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003832 wpa_printf(MSG_ERROR, "P2P: Failed to configure supported "
3833 "channel list");
3834 return -1;
3835 }
3836
3837 os_memcpy(p2p.pri_dev_type, wpa_s->conf->device_type,
3838 WPS_DEV_TYPE_LEN);
3839
3840 p2p.num_sec_dev_types = wpa_s->conf->num_sec_device_types;
3841 os_memcpy(p2p.sec_dev_type, wpa_s->conf->sec_device_type,
3842 p2p.num_sec_dev_types * WPS_DEV_TYPE_LEN);
3843
3844 p2p.concurrent_operations = !!(wpa_s->drv_flags &
3845 WPA_DRIVER_FLAGS_P2P_CONCURRENT);
3846
3847 p2p.max_peers = 100;
3848
3849 if (wpa_s->conf->p2p_ssid_postfix) {
3850 p2p.ssid_postfix_len =
3851 os_strlen(wpa_s->conf->p2p_ssid_postfix);
3852 if (p2p.ssid_postfix_len > sizeof(p2p.ssid_postfix))
3853 p2p.ssid_postfix_len = sizeof(p2p.ssid_postfix);
3854 os_memcpy(p2p.ssid_postfix, wpa_s->conf->p2p_ssid_postfix,
3855 p2p.ssid_postfix_len);
3856 }
3857
3858 p2p.p2p_intra_bss = wpa_s->conf->p2p_intra_bss;
3859
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08003860 p2p.max_listen = wpa_s->max_remain_on_chan;
3861
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003862 global->p2p = p2p_init(&p2p);
3863 if (global->p2p == NULL)
3864 return -1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003865 global->p2p_init_wpa_s = wpa_s;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003866
3867 for (i = 0; i < MAX_WPS_VENDOR_EXT; i++) {
3868 if (wpa_s->conf->wps_vendor_ext[i] == NULL)
3869 continue;
3870 p2p_add_wps_vendor_extension(
3871 global->p2p, wpa_s->conf->wps_vendor_ext[i]);
3872 }
3873
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003874 p2p_set_no_go_freq(global->p2p, &wpa_s->conf->p2p_no_go_freq);
3875
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003876 return 0;
3877}
3878
3879
3880/**
3881 * wpas_p2p_deinit - Deinitialize per-interface P2P data
3882 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
3883 *
3884 * This function deinitialize per-interface P2P data.
3885 */
3886void wpas_p2p_deinit(struct wpa_supplicant *wpa_s)
3887{
3888 if (wpa_s->driver && wpa_s->drv_priv)
3889 wpa_drv_probe_req_report(wpa_s, 0);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003890
3891 if (wpa_s->go_params) {
3892 /* Clear any stored provisioning info */
3893 p2p_clear_provisioning_info(
3894 wpa_s->global->p2p,
Dmitry Shmidt04949592012-07-19 12:16:46 -07003895 wpa_s->go_params->peer_device_addr);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003896 }
3897
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003898 os_free(wpa_s->go_params);
3899 wpa_s->go_params = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003900 eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s, NULL);
3901 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
3902 wpa_s->p2p_long_listen = 0;
3903 eloop_cancel_timeout(wpas_p2p_long_listen_timeout, wpa_s, NULL);
3904 eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL);
3905 wpas_p2p_remove_pending_group_interface(wpa_s);
Dmitry Shmidt04f534e2013-12-09 15:50:16 -08003906 eloop_cancel_timeout(wpas_p2p_group_freq_conflict, wpa_s, NULL);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08003907 wpas_p2p_listen_work_done(wpa_s);
3908 if (wpa_s->p2p_send_action_work) {
3909 os_free(wpa_s->p2p_send_action_work->ctx);
3910 radio_work_done(wpa_s->p2p_send_action_work);
3911 wpa_s->p2p_send_action_work = NULL;
3912 }
3913 eloop_cancel_timeout(wpas_p2p_send_action_work_timeout, wpa_s, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003914
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08003915 wpabuf_free(wpa_s->p2p_oob_dev_pw);
3916 wpa_s->p2p_oob_dev_pw = NULL;
3917
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003918 /* TODO: remove group interface from the driver if this wpa_s instance
3919 * is on top of a P2P group interface */
3920}
3921
3922
3923/**
3924 * wpas_p2p_deinit_global - Deinitialize global P2P module
3925 * @global: Pointer to global data from wpa_supplicant_init()
3926 *
3927 * This function deinitializes the global (per device) P2P module.
3928 */
3929void wpas_p2p_deinit_global(struct wpa_global *global)
3930{
3931 struct wpa_supplicant *wpa_s, *tmp;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003932
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003933 wpa_s = global->ifaces;
3934 if (wpa_s)
3935 wpas_p2p_service_flush(wpa_s);
3936
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003937 if (global->p2p == NULL)
3938 return;
3939
3940 /* Remove remaining P2P group interfaces */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003941 while (wpa_s && wpa_s->p2p_group_interface != NOT_P2P_GROUP_INTERFACE)
3942 wpa_s = wpa_s->next;
3943 while (wpa_s) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003944 tmp = global->ifaces;
3945 while (tmp &&
3946 (tmp == wpa_s ||
3947 tmp->p2p_group_interface == NOT_P2P_GROUP_INTERFACE)) {
3948 tmp = tmp->next;
3949 }
3950 if (tmp == NULL)
3951 break;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003952 /* Disconnect from the P2P group and deinit the interface */
3953 wpas_p2p_disconnect(tmp);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003954 }
3955
3956 /*
3957 * Deinit GO data on any possibly remaining interface (if main
3958 * interface is used as GO).
3959 */
3960 for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
3961 if (wpa_s->ap_iface)
3962 wpas_p2p_group_deinit(wpa_s);
3963 }
3964
3965 p2p_deinit(global->p2p);
3966 global->p2p = NULL;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003967 global->p2p_init_wpa_s = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003968}
3969
3970
3971static int wpas_p2p_create_iface(struct wpa_supplicant *wpa_s)
3972{
Dmitry Shmidt34af3062013-07-11 10:46:32 -07003973 if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_DEDICATED_P2P_DEVICE) &&
3974 wpa_s->conf->p2p_no_group_iface)
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08003975 return 0; /* separate interface disabled per configuration */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003976 if (wpa_s->drv_flags &
3977 (WPA_DRIVER_FLAGS_P2P_DEDICATED_INTERFACE |
3978 WPA_DRIVER_FLAGS_P2P_MGMT_AND_NON_P2P))
3979 return 1; /* P2P group requires a new interface in every case
3980 */
3981 if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_CONCURRENT))
3982 return 0; /* driver does not support concurrent operations */
3983 if (wpa_s->global->ifaces->next)
3984 return 1; /* more that one interface already in use */
3985 if (wpa_s->wpa_state >= WPA_AUTHENTICATING)
3986 return 1; /* this interface is already in use */
3987 return 0;
3988}
3989
3990
3991static int wpas_p2p_start_go_neg(struct wpa_supplicant *wpa_s,
3992 const u8 *peer_addr,
3993 enum p2p_wps_method wps_method,
3994 int go_intent, const u8 *own_interface_addr,
Dmitry Shmidt04949592012-07-19 12:16:46 -07003995 unsigned int force_freq, int persistent_group,
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08003996 struct wpa_ssid *ssid, unsigned int pref_freq)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003997{
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003998 if (persistent_group && wpa_s->conf->persistent_reconnect)
3999 persistent_group = 2;
4000
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004001 /*
4002 * Increase GO config timeout if HT40 is used since it takes some time
4003 * to scan channels for coex purposes before the BSS can be started.
4004 */
4005 p2p_set_config_timeout(wpa_s->global->p2p,
4006 wpa_s->p2p_go_ht40 ? 255 : 100, 20);
4007
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004008 return p2p_connect(wpa_s->global->p2p, peer_addr, wps_method,
4009 go_intent, own_interface_addr, force_freq,
Dmitry Shmidt04949592012-07-19 12:16:46 -07004010 persistent_group, ssid ? ssid->ssid : NULL,
4011 ssid ? ssid->ssid_len : 0,
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08004012 wpa_s->p2p_pd_before_go_neg, pref_freq,
4013 wps_method == WPS_NFC ? wpa_s->p2p_oob_dev_pw_id :
4014 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004015}
4016
4017
4018static int wpas_p2p_auth_go_neg(struct wpa_supplicant *wpa_s,
4019 const u8 *peer_addr,
4020 enum p2p_wps_method wps_method,
4021 int go_intent, const u8 *own_interface_addr,
Dmitry Shmidt04949592012-07-19 12:16:46 -07004022 unsigned int force_freq, int persistent_group,
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08004023 struct wpa_ssid *ssid, unsigned int pref_freq)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004024{
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004025 if (persistent_group && wpa_s->conf->persistent_reconnect)
4026 persistent_group = 2;
4027
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004028 return p2p_authorize(wpa_s->global->p2p, peer_addr, wps_method,
4029 go_intent, own_interface_addr, force_freq,
Dmitry Shmidt04949592012-07-19 12:16:46 -07004030 persistent_group, ssid ? ssid->ssid : NULL,
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08004031 ssid ? ssid->ssid_len : 0, pref_freq,
4032 wps_method == WPS_NFC ? wpa_s->p2p_oob_dev_pw_id :
4033 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004034}
4035
4036
4037static void wpas_p2p_check_join_scan_limit(struct wpa_supplicant *wpa_s)
4038{
4039 wpa_s->p2p_join_scan_count++;
4040 wpa_printf(MSG_DEBUG, "P2P: Join scan attempt %d",
4041 wpa_s->p2p_join_scan_count);
4042 if (wpa_s->p2p_join_scan_count > P2P_MAX_JOIN_SCAN_ATTEMPTS) {
4043 wpa_printf(MSG_DEBUG, "P2P: Failed to find GO " MACSTR
4044 " for join operationg - stop join attempt",
4045 MAC2STR(wpa_s->pending_join_iface_addr));
4046 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
Dmitry Shmidt04949592012-07-19 12:16:46 -07004047 if (wpa_s->p2p_auto_pd) {
4048 wpa_s->p2p_auto_pd = 0;
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07004049 wpa_msg_global(wpa_s, MSG_INFO,
4050 P2P_EVENT_PROV_DISC_FAILURE
4051 " p2p_dev_addr=" MACSTR " status=N/A",
4052 MAC2STR(wpa_s->pending_join_dev_addr));
Dmitry Shmidt04949592012-07-19 12:16:46 -07004053 return;
4054 }
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07004055 wpa_msg_global(wpa_s->parent, MSG_INFO,
4056 P2P_EVENT_GROUP_FORMATION_FAILURE);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004057 }
4058}
4059
4060
Dmitry Shmidt04949592012-07-19 12:16:46 -07004061static int wpas_check_freq_conflict(struct wpa_supplicant *wpa_s, int freq)
4062{
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07004063 int *freqs, res, num, i;
Dmitry Shmidt04949592012-07-19 12:16:46 -07004064
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07004065 if (wpas_p2p_num_unused_channels(wpa_s) > 0) {
4066 /* Multiple channels are supported and not all are in use */
Dmitry Shmidt04949592012-07-19 12:16:46 -07004067 return 0;
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07004068 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07004069
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07004070 freqs = os_calloc(wpa_s->num_multichan_concurrent, sizeof(int));
4071 if (!freqs)
4072 return 1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07004073
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07004074 num = wpas_p2p_valid_oper_freqs(wpa_s, freqs,
4075 wpa_s->num_multichan_concurrent);
4076 if (num < 0) {
4077 res = 1;
4078 goto exit_free;
4079 }
4080
4081 for (i = 0; i < num; i++) {
4082 if (freqs[i] == freq) {
4083 wpa_printf(MSG_DEBUG, "P2P: Frequency %d MHz in use by another virtual interface and can be used",
4084 freq);
4085 res = 0;
4086 goto exit_free;
Dmitry Shmidt04949592012-07-19 12:16:46 -07004087 }
4088 }
4089
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07004090 res = 1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07004091
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07004092exit_free:
4093 os_free(freqs);
4094 return res;
Dmitry Shmidt04949592012-07-19 12:16:46 -07004095}
4096
4097
4098static int wpas_p2p_peer_go(struct wpa_supplicant *wpa_s,
4099 const u8 *peer_dev_addr)
4100{
4101 struct wpa_bss *bss;
4102 int updated;
4103
4104 bss = wpa_bss_get_p2p_dev_addr(wpa_s, peer_dev_addr);
4105 if (bss == NULL)
4106 return -1;
4107 if (bss->last_update_idx < wpa_s->bss_update_idx) {
4108 wpa_printf(MSG_DEBUG, "P2P: Peer BSS entry not updated in the "
4109 "last scan");
4110 return 0;
4111 }
4112
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08004113 updated = os_reltime_before(&wpa_s->p2p_auto_started,
4114 &bss->last_update);
Dmitry Shmidt04949592012-07-19 12:16:46 -07004115 wpa_printf(MSG_DEBUG, "P2P: Current BSS entry for peer updated at "
4116 "%ld.%06ld (%supdated in last scan)",
4117 bss->last_update.sec, bss->last_update.usec,
4118 updated ? "": "not ");
4119
4120 return updated;
4121}
4122
4123
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004124static void wpas_p2p_scan_res_join(struct wpa_supplicant *wpa_s,
4125 struct wpa_scan_results *scan_res)
4126{
Dmitry Shmidt344abd32014-01-14 13:17:00 -08004127 struct wpa_bss *bss = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004128 int freq;
4129 u8 iface_addr[ETH_ALEN];
Dmitry Shmidt04949592012-07-19 12:16:46 -07004130
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004131 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
4132
4133 if (wpa_s->global->p2p_disabled)
4134 return;
4135
Dmitry Shmidt04949592012-07-19 12:16:46 -07004136 wpa_printf(MSG_DEBUG, "P2P: Scan results received (%d BSS) for %sjoin",
4137 scan_res ? (int) scan_res->num : -1,
4138 wpa_s->p2p_auto_join ? "auto_" : "");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004139
4140 if (scan_res)
4141 wpas_p2p_scan_res_handler(wpa_s, scan_res);
4142
Dmitry Shmidt04949592012-07-19 12:16:46 -07004143 if (wpa_s->p2p_auto_pd) {
4144 int join = wpas_p2p_peer_go(wpa_s,
4145 wpa_s->pending_join_dev_addr);
4146 if (join == 0 &&
4147 wpa_s->auto_pd_scan_retry < P2P_AUTO_PD_SCAN_ATTEMPTS) {
4148 wpa_s->auto_pd_scan_retry++;
Dmitry Shmidt444d5672013-04-01 13:08:44 -07004149 bss = wpa_bss_get_bssid_latest(
4150 wpa_s, wpa_s->pending_join_dev_addr);
Dmitry Shmidt04949592012-07-19 12:16:46 -07004151 if (bss) {
4152 freq = bss->freq;
4153 wpa_printf(MSG_DEBUG, "P2P: Scan retry %d for "
4154 "the peer " MACSTR " at %d MHz",
4155 wpa_s->auto_pd_scan_retry,
4156 MAC2STR(wpa_s->
4157 pending_join_dev_addr),
4158 freq);
Dmitry Shmidt344abd32014-01-14 13:17:00 -08004159 wpas_p2p_join_scan_req(wpa_s, freq, NULL, 0);
Dmitry Shmidt04949592012-07-19 12:16:46 -07004160 return;
4161 }
4162 }
4163
4164 if (join < 0)
4165 join = 0;
4166
4167 wpa_s->p2p_auto_pd = 0;
4168 wpa_s->pending_pd_use = join ? AUTO_PD_JOIN : AUTO_PD_GO_NEG;
4169 wpa_printf(MSG_DEBUG, "P2P: Auto PD with " MACSTR " join=%d",
4170 MAC2STR(wpa_s->pending_join_dev_addr), join);
4171 if (p2p_prov_disc_req(wpa_s->global->p2p,
4172 wpa_s->pending_join_dev_addr,
4173 wpa_s->pending_pd_config_methods, join,
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08004174 0, wpa_s->user_initiated_pd) < 0) {
Dmitry Shmidt04949592012-07-19 12:16:46 -07004175 wpa_s->p2p_auto_pd = 0;
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07004176 wpa_msg_global(wpa_s, MSG_INFO,
4177 P2P_EVENT_PROV_DISC_FAILURE
4178 " p2p_dev_addr=" MACSTR " status=N/A",
4179 MAC2STR(wpa_s->pending_join_dev_addr));
Dmitry Shmidt04949592012-07-19 12:16:46 -07004180 }
4181 return;
4182 }
4183
4184 if (wpa_s->p2p_auto_join) {
4185 int join = wpas_p2p_peer_go(wpa_s,
4186 wpa_s->pending_join_dev_addr);
4187 if (join < 0) {
4188 wpa_printf(MSG_DEBUG, "P2P: Peer was not found to be "
4189 "running a GO -> use GO Negotiation");
4190 wpas_p2p_connect(wpa_s, wpa_s->pending_join_dev_addr,
4191 wpa_s->p2p_pin, wpa_s->p2p_wps_method,
4192 wpa_s->p2p_persistent_group, 0, 0, 0,
4193 wpa_s->p2p_go_intent,
4194 wpa_s->p2p_connect_freq,
4195 wpa_s->p2p_persistent_id,
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004196 wpa_s->p2p_pd_before_go_neg,
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004197 wpa_s->p2p_go_ht40,
4198 wpa_s->p2p_go_vht);
Dmitry Shmidt04949592012-07-19 12:16:46 -07004199 return;
4200 }
4201
4202 wpa_printf(MSG_DEBUG, "P2P: Peer was found running GO%s -> "
4203 "try to join the group", join ? "" :
4204 " in older scan");
4205 if (!join)
4206 wpa_s->p2p_fallback_to_go_neg = 1;
4207 }
4208
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004209 freq = p2p_get_oper_freq(wpa_s->global->p2p,
4210 wpa_s->pending_join_iface_addr);
4211 if (freq < 0 &&
4212 p2p_get_interface_addr(wpa_s->global->p2p,
4213 wpa_s->pending_join_dev_addr,
4214 iface_addr) == 0 &&
4215 os_memcmp(iface_addr, wpa_s->pending_join_dev_addr, ETH_ALEN) != 0)
4216 {
4217 wpa_printf(MSG_DEBUG, "P2P: Overwrite pending interface "
4218 "address for join from " MACSTR " to " MACSTR
4219 " based on newly discovered P2P peer entry",
4220 MAC2STR(wpa_s->pending_join_iface_addr),
4221 MAC2STR(iface_addr));
4222 os_memcpy(wpa_s->pending_join_iface_addr, iface_addr,
4223 ETH_ALEN);
4224
4225 freq = p2p_get_oper_freq(wpa_s->global->p2p,
4226 wpa_s->pending_join_iface_addr);
4227 }
4228 if (freq >= 0) {
4229 wpa_printf(MSG_DEBUG, "P2P: Target GO operating frequency "
4230 "from P2P peer table: %d MHz", freq);
4231 }
Dmitry Shmidt344abd32014-01-14 13:17:00 -08004232 if (wpa_s->p2p_join_ssid_len) {
4233 wpa_printf(MSG_DEBUG, "P2P: Trying to find target GO BSS entry based on BSSID "
4234 MACSTR " and SSID %s",
4235 MAC2STR(wpa_s->pending_join_iface_addr),
4236 wpa_ssid_txt(wpa_s->p2p_join_ssid,
4237 wpa_s->p2p_join_ssid_len));
4238 bss = wpa_bss_get(wpa_s, wpa_s->pending_join_iface_addr,
4239 wpa_s->p2p_join_ssid,
4240 wpa_s->p2p_join_ssid_len);
4241 }
4242 if (!bss) {
4243 wpa_printf(MSG_DEBUG, "P2P: Trying to find target GO BSS entry based on BSSID "
4244 MACSTR, MAC2STR(wpa_s->pending_join_iface_addr));
4245 bss = wpa_bss_get_bssid_latest(wpa_s,
4246 wpa_s->pending_join_iface_addr);
4247 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004248 if (bss) {
4249 freq = bss->freq;
4250 wpa_printf(MSG_DEBUG, "P2P: Target GO operating frequency "
Dmitry Shmidt051af732013-10-22 13:52:46 -07004251 "from BSS table: %d MHz (SSID %s)", freq,
4252 wpa_ssid_txt(bss->ssid, bss->ssid_len));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004253 }
4254 if (freq > 0) {
4255 u16 method;
4256
Dmitry Shmidt04949592012-07-19 12:16:46 -07004257 if (wpas_check_freq_conflict(wpa_s, freq) > 0) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07004258 wpa_msg_global(wpa_s->parent, MSG_INFO,
4259 P2P_EVENT_GROUP_FORMATION_FAILURE
4260 "reason=FREQ_CONFLICT");
Dmitry Shmidt04949592012-07-19 12:16:46 -07004261 return;
4262 }
4263
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004264 wpa_printf(MSG_DEBUG, "P2P: Send Provision Discovery Request "
4265 "prior to joining an existing group (GO " MACSTR
4266 " freq=%u MHz)",
4267 MAC2STR(wpa_s->pending_join_dev_addr), freq);
4268 wpa_s->pending_pd_before_join = 1;
4269
4270 switch (wpa_s->pending_join_wps_method) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004271 case WPS_PIN_DISPLAY:
4272 method = WPS_CONFIG_KEYPAD;
4273 break;
4274 case WPS_PIN_KEYPAD:
4275 method = WPS_CONFIG_DISPLAY;
4276 break;
4277 case WPS_PBC:
4278 method = WPS_CONFIG_PUSHBUTTON;
4279 break;
4280 default:
4281 method = 0;
4282 break;
4283 }
4284
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004285 if ((p2p_get_provisioning_info(wpa_s->global->p2p,
4286 wpa_s->pending_join_dev_addr) ==
4287 method)) {
4288 /*
4289 * We have already performed provision discovery for
4290 * joining the group. Proceed directly to join
4291 * operation without duplicated provision discovery. */
4292 wpa_printf(MSG_DEBUG, "P2P: Provision discovery "
4293 "with " MACSTR " already done - proceed to "
4294 "join",
4295 MAC2STR(wpa_s->pending_join_dev_addr));
4296 wpa_s->pending_pd_before_join = 0;
4297 goto start;
4298 }
4299
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004300 if (p2p_prov_disc_req(wpa_s->global->p2p,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004301 wpa_s->pending_join_dev_addr, method, 1,
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08004302 freq, wpa_s->user_initiated_pd) < 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004303 wpa_printf(MSG_DEBUG, "P2P: Failed to send Provision "
4304 "Discovery Request before joining an "
4305 "existing group");
4306 wpa_s->pending_pd_before_join = 0;
4307 goto start;
4308 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004309 return;
4310 }
4311
4312 wpa_printf(MSG_DEBUG, "P2P: Failed to find BSS/GO - try again later");
4313 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
4314 eloop_register_timeout(1, 0, wpas_p2p_join_scan, wpa_s, NULL);
4315 wpas_p2p_check_join_scan_limit(wpa_s);
4316 return;
4317
4318start:
4319 /* Start join operation immediately */
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08004320 wpas_p2p_join_start(wpa_s, 0, NULL, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004321}
4322
4323
Dmitry Shmidt344abd32014-01-14 13:17:00 -08004324static void wpas_p2p_join_scan_req(struct wpa_supplicant *wpa_s, int freq,
4325 const u8 *ssid, size_t ssid_len)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004326{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004327 int ret;
4328 struct wpa_driver_scan_params params;
4329 struct wpabuf *wps_ie, *ies;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004330 size_t ielen;
Dmitry Shmidt04949592012-07-19 12:16:46 -07004331 int freqs[2] = { 0, 0 };
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004332
4333 os_memset(&params, 0, sizeof(params));
4334
4335 /* P2P Wildcard SSID */
4336 params.num_ssids = 1;
Dmitry Shmidt344abd32014-01-14 13:17:00 -08004337 if (ssid && ssid_len) {
4338 params.ssids[0].ssid = ssid;
4339 params.ssids[0].ssid_len = ssid_len;
4340 os_memcpy(wpa_s->p2p_join_ssid, ssid, ssid_len);
4341 wpa_s->p2p_join_ssid_len = ssid_len;
4342 } else {
4343 params.ssids[0].ssid = (u8 *) P2P_WILDCARD_SSID;
4344 params.ssids[0].ssid_len = P2P_WILDCARD_SSID_LEN;
4345 wpa_s->p2p_join_ssid_len = 0;
4346 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004347
4348 wpa_s->wps->dev.p2p = 1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07004349 wps_ie = wps_build_probe_req_ie(DEV_PW_DEFAULT, &wpa_s->wps->dev,
4350 wpa_s->wps->uuid, WPS_REQ_ENROLLEE, 0,
4351 NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004352 if (wps_ie == NULL) {
4353 wpas_p2p_scan_res_join(wpa_s, NULL);
4354 return;
4355 }
4356
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004357 ielen = p2p_scan_ie_buf_len(wpa_s->global->p2p);
4358 ies = wpabuf_alloc(wpabuf_len(wps_ie) + ielen);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004359 if (ies == NULL) {
4360 wpabuf_free(wps_ie);
4361 wpas_p2p_scan_res_join(wpa_s, NULL);
4362 return;
4363 }
4364 wpabuf_put_buf(ies, wps_ie);
4365 wpabuf_free(wps_ie);
4366
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08004367 p2p_scan_ie(wpa_s->global->p2p, ies, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004368
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004369 params.p2p_probe = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004370 params.extra_ies = wpabuf_head(ies);
4371 params.extra_ies_len = wpabuf_len(ies);
Dmitry Shmidt9e3f8ee2014-01-17 10:52:01 -08004372
4373 if (!freq) {
4374 int oper_freq;
4375 /*
4376 * If freq is not provided, check the operating freq of the GO
4377 * and use a single channel scan on if possible.
4378 */
4379 oper_freq = p2p_get_oper_freq(wpa_s->global->p2p,
4380 wpa_s->pending_join_iface_addr);
4381 if (oper_freq > 0)
4382 freq = oper_freq;
4383 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07004384 if (freq > 0) {
4385 freqs[0] = freq;
4386 params.freqs = freqs;
4387 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004388
4389 /*
4390 * Run a scan to update BSS table and start Provision Discovery once
4391 * the new scan results become available.
4392 */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004393 ret = wpa_drv_scan(wpa_s, &params);
Dmitry Shmidt444d5672013-04-01 13:08:44 -07004394 if (!ret) {
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08004395 os_get_reltime(&wpa_s->scan_trigger_time);
Dmitry Shmidt04949592012-07-19 12:16:46 -07004396 wpa_s->scan_res_handler = wpas_p2p_scan_res_join;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08004397 wpa_s->own_scan_requested = 1;
Dmitry Shmidt444d5672013-04-01 13:08:44 -07004398 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004399
4400 wpabuf_free(ies);
4401
4402 if (ret) {
4403 wpa_printf(MSG_DEBUG, "P2P: Failed to start scan for join - "
4404 "try again later");
4405 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
4406 eloop_register_timeout(1, 0, wpas_p2p_join_scan, wpa_s, NULL);
4407 wpas_p2p_check_join_scan_limit(wpa_s);
4408 }
4409}
4410
4411
Dmitry Shmidt04949592012-07-19 12:16:46 -07004412static void wpas_p2p_join_scan(void *eloop_ctx, void *timeout_ctx)
4413{
4414 struct wpa_supplicant *wpa_s = eloop_ctx;
Dmitry Shmidt344abd32014-01-14 13:17:00 -08004415 wpas_p2p_join_scan_req(wpa_s, 0, NULL, 0);
Dmitry Shmidt04949592012-07-19 12:16:46 -07004416}
4417
4418
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004419static int wpas_p2p_join(struct wpa_supplicant *wpa_s, const u8 *iface_addr,
Dmitry Shmidt04949592012-07-19 12:16:46 -07004420 const u8 *dev_addr, enum p2p_wps_method wps_method,
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08004421 int auto_join, int op_freq,
4422 const u8 *ssid, size_t ssid_len)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004423{
4424 wpa_printf(MSG_DEBUG, "P2P: Request to join existing group (iface "
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08004425 MACSTR " dev " MACSTR " op_freq=%d)%s",
4426 MAC2STR(iface_addr), MAC2STR(dev_addr), op_freq,
Dmitry Shmidt04949592012-07-19 12:16:46 -07004427 auto_join ? " (auto_join)" : "");
Dmitry Shmidt344abd32014-01-14 13:17:00 -08004428 if (ssid && ssid_len) {
4429 wpa_printf(MSG_DEBUG, "P2P: Group SSID specified: %s",
4430 wpa_ssid_txt(ssid, ssid_len));
4431 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004432
Dmitry Shmidt04949592012-07-19 12:16:46 -07004433 wpa_s->p2p_auto_pd = 0;
4434 wpa_s->p2p_auto_join = !!auto_join;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004435 os_memcpy(wpa_s->pending_join_iface_addr, iface_addr, ETH_ALEN);
4436 os_memcpy(wpa_s->pending_join_dev_addr, dev_addr, ETH_ALEN);
4437 wpa_s->pending_join_wps_method = wps_method;
4438
4439 /* Make sure we are not running find during connection establishment */
4440 wpas_p2p_stop_find(wpa_s);
4441
4442 wpa_s->p2p_join_scan_count = 0;
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08004443 wpas_p2p_join_scan_req(wpa_s, op_freq, ssid, ssid_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004444 return 0;
4445}
4446
4447
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08004448static int wpas_p2p_join_start(struct wpa_supplicant *wpa_s, int freq,
4449 const u8 *ssid, size_t ssid_len)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004450{
4451 struct wpa_supplicant *group;
4452 struct p2p_go_neg_results res;
Dmitry Shmidt04949592012-07-19 12:16:46 -07004453 struct wpa_bss *bss;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004454
4455 group = wpas_p2p_get_group_iface(wpa_s, 0, 0);
4456 if (group == NULL)
4457 return -1;
4458 if (group != wpa_s) {
4459 os_memcpy(group->p2p_pin, wpa_s->p2p_pin,
4460 sizeof(group->p2p_pin));
4461 group->p2p_wps_method = wpa_s->p2p_wps_method;
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08004462 } else {
4463 /*
4464 * Need to mark the current interface for p2p_group_formation
4465 * when a separate group interface is not used. This is needed
4466 * to allow p2p_cancel stop a pending p2p_connect-join.
4467 * wpas_p2p_init_group_interface() addresses this for the case
4468 * where a separate group interface is used.
4469 */
4470 wpa_s->global->p2p_group_formation = wpa_s;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004471 }
4472
4473 group->p2p_in_provisioning = 1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07004474 group->p2p_fallback_to_go_neg = wpa_s->p2p_fallback_to_go_neg;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004475
4476 os_memset(&res, 0, sizeof(res));
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08004477 os_memcpy(res.peer_device_addr, wpa_s->pending_join_dev_addr, ETH_ALEN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004478 os_memcpy(res.peer_interface_addr, wpa_s->pending_join_iface_addr,
4479 ETH_ALEN);
4480 res.wps_method = wpa_s->pending_join_wps_method;
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08004481 if (freq && ssid && ssid_len) {
4482 res.freq = freq;
4483 res.ssid_len = ssid_len;
4484 os_memcpy(res.ssid, ssid, ssid_len);
4485 } else {
4486 bss = wpa_bss_get_bssid_latest(wpa_s,
4487 wpa_s->pending_join_iface_addr);
4488 if (bss) {
4489 res.freq = bss->freq;
4490 res.ssid_len = bss->ssid_len;
4491 os_memcpy(res.ssid, bss->ssid, bss->ssid_len);
4492 wpa_printf(MSG_DEBUG, "P2P: Join target GO operating frequency from BSS table: %d MHz (SSID %s)",
4493 bss->freq,
4494 wpa_ssid_txt(bss->ssid, bss->ssid_len));
4495 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07004496 }
4497
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08004498 if (wpa_s->off_channel_freq || wpa_s->roc_waiting_drv_freq) {
4499 wpa_printf(MSG_DEBUG, "P2P: Cancel remain-on-channel prior to "
4500 "starting client");
4501 wpa_drv_cancel_remain_on_channel(wpa_s);
4502 wpa_s->off_channel_freq = 0;
4503 wpa_s->roc_waiting_drv_freq = 0;
4504 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004505 wpas_start_wps_enrollee(group, &res);
4506
4507 /*
4508 * Allow a longer timeout for join-a-running-group than normal 15
4509 * second group formation timeout since the GO may not have authorized
4510 * our connection yet.
4511 */
4512 eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s, NULL);
4513 eloop_register_timeout(60, 0, wpas_p2p_group_formation_timeout,
4514 wpa_s, NULL);
4515
4516 return 0;
4517}
4518
4519
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08004520static int wpas_p2p_setup_freqs(struct wpa_supplicant *wpa_s, int freq,
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004521 int *force_freq, int *pref_freq, int go)
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08004522{
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07004523 int *freqs, res;
4524 unsigned int freq_in_use = 0, num, i;
4525
4526 freqs = os_calloc(wpa_s->num_multichan_concurrent, sizeof(int));
4527 if (!freqs)
4528 return -1;
4529
4530 num = get_shared_radio_freqs(wpa_s, freqs,
4531 wpa_s->num_multichan_concurrent);
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07004532 wpa_printf(MSG_DEBUG,
4533 "P2P: Setup freqs: freq=%d num_MCC=%d shared_freqs=%u",
4534 freq, wpa_s->num_multichan_concurrent, num);
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07004535
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08004536 if (freq > 0) {
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004537 int ret;
4538 if (go)
4539 ret = p2p_supported_freq(wpa_s->global->p2p, freq);
4540 else
4541 ret = p2p_supported_freq_cli(wpa_s->global->p2p, freq);
4542 if (!ret) {
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08004543 wpa_printf(MSG_DEBUG, "P2P: The forced channel "
4544 "(%u MHz) is not supported for P2P uses",
4545 freq);
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07004546 res = -3;
4547 goto exit_free;
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08004548 }
4549
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07004550 for (i = 0; i < num; i++) {
4551 if (freqs[i] == freq)
4552 freq_in_use = 1;
4553 }
4554
4555 if (num == wpa_s->num_multichan_concurrent && !freq_in_use) {
4556 wpa_printf(MSG_DEBUG, "P2P: Cannot start P2P group on %u MHz as there are no available channels",
4557 freq);
4558 res = -2;
4559 goto exit_free;
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08004560 }
4561 wpa_printf(MSG_DEBUG, "P2P: Trying to force us to use the "
4562 "requested channel (%u MHz)", freq);
4563 *force_freq = freq;
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07004564 goto exit_ok;
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08004565 }
4566
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07004567 for (i = 0; i < num; i++) {
4568 if (!p2p_supported_freq(wpa_s->global->p2p, freqs[i]))
4569 continue;
4570
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07004571 if (*pref_freq == 0 && num < wpa_s->num_multichan_concurrent) {
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08004572 wpa_printf(MSG_DEBUG, "P2P: Try to prefer a frequency (%u MHz) we are already using",
4573 freqs[i]);
Dmitry Shmidt51a47d52013-09-10 10:52:57 -07004574 *pref_freq = freqs[i];
Dmitry Shmidt51a47d52013-09-10 10:52:57 -07004575 } else {
4576 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 -08004577 freqs[i]);
Dmitry Shmidt51a47d52013-09-10 10:52:57 -07004578 *force_freq = freqs[i];
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07004579 }
4580 break;
4581 }
4582
4583 if (i == num) {
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08004584 if (num < wpa_s->num_multichan_concurrent && num > 0) {
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07004585 wpa_printf(MSG_DEBUG, "P2P: Current operating channels are not available for P2P. Try to use another channel");
4586 *force_freq = 0;
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08004587 } else if (num < wpa_s->num_multichan_concurrent) {
4588 wpa_printf(MSG_DEBUG, "P2P: No current operating channels - try to use a new channel");
4589 *force_freq = 0;
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07004590 } else {
4591 wpa_printf(MSG_DEBUG, "P2P: All channels are in use and none of them are P2P enabled. Cannot start P2P group");
4592 res = -2;
4593 goto exit_free;
4594 }
4595 }
4596
4597exit_ok:
4598 res = 0;
4599exit_free:
4600 os_free(freqs);
4601 return res;
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08004602}
4603
4604
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004605/**
4606 * wpas_p2p_connect - Request P2P Group Formation to be started
4607 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
4608 * @peer_addr: Address of the peer P2P Device
4609 * @pin: PIN to use during provisioning or %NULL to indicate PBC mode
4610 * @persistent_group: Whether to create a persistent group
Dmitry Shmidt04949592012-07-19 12:16:46 -07004611 * @auto_join: Whether to select join vs. GO Negotiation automatically
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004612 * @join: Whether to join an existing group (as a client) instead of starting
4613 * Group Owner negotiation; @peer_addr is BSSID in that case
4614 * @auth: Whether to only authorize the connection instead of doing that and
4615 * initiating Group Owner negotiation
4616 * @go_intent: GO Intent or -1 to use default
4617 * @freq: Frequency for the group or 0 for auto-selection
Dmitry Shmidt04949592012-07-19 12:16:46 -07004618 * @persistent_id: Persistent group credentials to use for forcing GO
4619 * parameters or -1 to generate new values (SSID/passphrase)
4620 * @pd: Whether to send Provision Discovery prior to GO Negotiation as an
4621 * interoperability workaround when initiating group formation
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004622 * @ht40: Start GO with 40 MHz channel width
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004623 * @vht: Start GO with VHT support
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004624 * Returns: 0 or new PIN (if pin was %NULL) on success, -1 on unspecified
4625 * failure, -2 on failure due to channel not currently available,
4626 * -3 if forced channel is not supported
4627 */
4628int wpas_p2p_connect(struct wpa_supplicant *wpa_s, const u8 *peer_addr,
4629 const char *pin, enum p2p_wps_method wps_method,
Dmitry Shmidt04949592012-07-19 12:16:46 -07004630 int persistent_group, int auto_join, int join, int auth,
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004631 int go_intent, int freq, int persistent_id, int pd,
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004632 int ht40, int vht)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004633{
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07004634 int force_freq = 0, pref_freq = 0;
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08004635 int ret = 0, res;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004636 enum wpa_driver_if_type iftype;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004637 const u8 *if_addr;
Dmitry Shmidt04949592012-07-19 12:16:46 -07004638 struct wpa_ssid *ssid = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004639
4640 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
4641 return -1;
4642
Dmitry Shmidt04949592012-07-19 12:16:46 -07004643 if (persistent_id >= 0) {
4644 ssid = wpa_config_get_network(wpa_s->conf, persistent_id);
4645 if (ssid == NULL || ssid->disabled != 2 ||
4646 ssid->mode != WPAS_MODE_P2P_GO)
4647 return -1;
4648 }
4649
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07004650 os_free(wpa_s->global->add_psk);
4651 wpa_s->global->add_psk = NULL;
4652
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08004653 wpa_s->global->p2p_fail_on_wps_complete = 0;
4654
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004655 if (go_intent < 0)
4656 go_intent = wpa_s->conf->p2p_go_intent;
4657
4658 if (!auth)
4659 wpa_s->p2p_long_listen = 0;
4660
4661 wpa_s->p2p_wps_method = wps_method;
Dmitry Shmidt04949592012-07-19 12:16:46 -07004662 wpa_s->p2p_persistent_group = !!persistent_group;
4663 wpa_s->p2p_persistent_id = persistent_id;
4664 wpa_s->p2p_go_intent = go_intent;
4665 wpa_s->p2p_connect_freq = freq;
4666 wpa_s->p2p_fallback_to_go_neg = 0;
4667 wpa_s->p2p_pd_before_go_neg = !!pd;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004668 wpa_s->p2p_go_ht40 = !!ht40;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004669 wpa_s->p2p_go_vht = !!vht;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004670
4671 if (pin)
4672 os_strlcpy(wpa_s->p2p_pin, pin, sizeof(wpa_s->p2p_pin));
4673 else if (wps_method == WPS_PIN_DISPLAY) {
4674 ret = wps_generate_pin();
4675 os_snprintf(wpa_s->p2p_pin, sizeof(wpa_s->p2p_pin), "%08d",
4676 ret);
4677 wpa_printf(MSG_DEBUG, "P2P: Randomly generated PIN: %s",
4678 wpa_s->p2p_pin);
4679 } else
4680 wpa_s->p2p_pin[0] = '\0';
4681
Dmitry Shmidt04949592012-07-19 12:16:46 -07004682 if (join || auto_join) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004683 u8 iface_addr[ETH_ALEN], dev_addr[ETH_ALEN];
4684 if (auth) {
4685 wpa_printf(MSG_DEBUG, "P2P: Authorize invitation to "
4686 "connect a running group from " MACSTR,
4687 MAC2STR(peer_addr));
4688 os_memcpy(wpa_s->p2p_auth_invite, peer_addr, ETH_ALEN);
4689 return ret;
4690 }
4691 os_memcpy(dev_addr, peer_addr, ETH_ALEN);
4692 if (p2p_get_interface_addr(wpa_s->global->p2p, peer_addr,
4693 iface_addr) < 0) {
4694 os_memcpy(iface_addr, peer_addr, ETH_ALEN);
4695 p2p_get_dev_addr(wpa_s->global->p2p, peer_addr,
4696 dev_addr);
4697 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07004698 if (auto_join) {
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08004699 os_get_reltime(&wpa_s->p2p_auto_started);
Dmitry Shmidt04949592012-07-19 12:16:46 -07004700 wpa_printf(MSG_DEBUG, "P2P: Auto join started at "
4701 "%ld.%06ld",
4702 wpa_s->p2p_auto_started.sec,
4703 wpa_s->p2p_auto_started.usec);
4704 }
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08004705 wpa_s->user_initiated_pd = 1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07004706 if (wpas_p2p_join(wpa_s, iface_addr, dev_addr, wps_method,
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08004707 auto_join, freq, NULL, 0) < 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004708 return -1;
4709 return ret;
4710 }
4711
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004712 res = wpas_p2p_setup_freqs(wpa_s, freq, &force_freq, &pref_freq,
4713 go_intent == 15);
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08004714 if (res)
4715 return res;
Dmitry Shmidta0d265f2013-11-19 13:13:41 -08004716 wpas_p2p_set_own_freq_preference(wpa_s,
4717 force_freq ? force_freq : pref_freq);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004718
4719 wpa_s->create_p2p_iface = wpas_p2p_create_iface(wpa_s);
4720
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004721 if (wpa_s->create_p2p_iface) {
4722 /* Prepare to add a new interface for the group */
4723 iftype = WPA_IF_P2P_GROUP;
4724 if (go_intent == 15)
4725 iftype = WPA_IF_P2P_GO;
4726 if (wpas_p2p_add_group_interface(wpa_s, iftype) < 0) {
4727 wpa_printf(MSG_ERROR, "P2P: Failed to allocate a new "
4728 "interface for the group");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004729 return -1;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004730 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004731
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004732 if_addr = wpa_s->pending_interface_addr;
4733 } else
4734 if_addr = wpa_s->own_addr;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004735
4736 if (auth) {
4737 if (wpas_p2p_auth_go_neg(wpa_s, peer_addr, wps_method,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004738 go_intent, if_addr,
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08004739 force_freq, persistent_group, ssid,
4740 pref_freq) < 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004741 return -1;
4742 return ret;
4743 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004744
4745 if (wpas_p2p_start_go_neg(wpa_s, peer_addr, wps_method,
4746 go_intent, if_addr, force_freq,
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08004747 persistent_group, ssid, pref_freq) < 0) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004748 if (wpa_s->create_p2p_iface)
4749 wpas_p2p_remove_pending_group_interface(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004750 return -1;
4751 }
4752 return ret;
4753}
4754
4755
4756/**
4757 * wpas_p2p_remain_on_channel_cb - Indication of remain-on-channel start
4758 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
4759 * @freq: Frequency of the channel in MHz
4760 * @duration: Duration of the stay on the channel in milliseconds
4761 *
4762 * This callback is called when the driver indicates that it has started the
4763 * requested remain-on-channel duration.
4764 */
4765void wpas_p2p_remain_on_channel_cb(struct wpa_supplicant *wpa_s,
4766 unsigned int freq, unsigned int duration)
4767{
4768 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
4769 return;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004770 if (wpa_s->off_channel_freq == wpa_s->pending_listen_freq) {
4771 p2p_listen_cb(wpa_s->global->p2p, wpa_s->pending_listen_freq,
4772 wpa_s->pending_listen_duration);
4773 wpa_s->pending_listen_freq = 0;
4774 }
4775}
4776
4777
4778static int wpas_p2p_listen_start(struct wpa_supplicant *wpa_s,
4779 unsigned int timeout)
4780{
4781 /* Limit maximum Listen state time based on driver limitation. */
4782 if (timeout > wpa_s->max_remain_on_chan)
4783 timeout = wpa_s->max_remain_on_chan;
4784
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004785 return p2p_listen(wpa_s->global->p2p, timeout);
4786}
4787
4788
4789/**
4790 * wpas_p2p_cancel_remain_on_channel_cb - Remain-on-channel timeout
4791 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
4792 * @freq: Frequency of the channel in MHz
4793 *
4794 * This callback is called when the driver indicates that a remain-on-channel
4795 * operation has been completed, i.e., the duration on the requested channel
4796 * has timed out.
4797 */
4798void wpas_p2p_cancel_remain_on_channel_cb(struct wpa_supplicant *wpa_s,
4799 unsigned int freq)
4800{
4801 wpa_printf(MSG_DEBUG, "P2P: Cancel remain-on-channel callback "
4802 "(p2p_long_listen=%d ms pending_action_tx=%p)",
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08004803 wpa_s->p2p_long_listen, offchannel_pending_action_tx(wpa_s));
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08004804 wpas_p2p_listen_work_done(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004805 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
4806 return;
4807 if (p2p_listen_end(wpa_s->global->p2p, freq) > 0)
4808 return; /* P2P module started a new operation */
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08004809 if (offchannel_pending_action_tx(wpa_s))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004810 return;
4811 if (wpa_s->p2p_long_listen > 0)
4812 wpa_s->p2p_long_listen -= wpa_s->max_remain_on_chan;
4813 if (wpa_s->p2p_long_listen > 0) {
4814 wpa_printf(MSG_DEBUG, "P2P: Continuing long Listen state");
4815 wpas_p2p_listen_start(wpa_s, wpa_s->p2p_long_listen);
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07004816 } else {
4817 /*
4818 * When listen duration is over, stop listen & update p2p_state
4819 * to IDLE.
4820 */
4821 p2p_stop_listen(wpa_s->global->p2p);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004822 }
4823}
4824
4825
4826/**
4827 * wpas_p2p_group_remove - Remove a P2P group
4828 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
4829 * @ifname: Network interface name of the group interface or "*" to remove all
4830 * groups
4831 * Returns: 0 on success, -1 on failure
4832 *
4833 * This function is used to remove a P2P group. This can be used to disconnect
4834 * from a group in which the local end is a P2P Client or to end a P2P Group in
4835 * case the local end is the Group Owner. If a virtual network interface was
4836 * created for this group, that interface will be removed. Otherwise, only the
4837 * configured P2P group network will be removed from the interface.
4838 */
4839int wpas_p2p_group_remove(struct wpa_supplicant *wpa_s, const char *ifname)
4840{
4841 struct wpa_global *global = wpa_s->global;
4842
4843 if (os_strcmp(ifname, "*") == 0) {
4844 struct wpa_supplicant *prev;
4845 wpa_s = global->ifaces;
4846 while (wpa_s) {
4847 prev = wpa_s;
4848 wpa_s = wpa_s->next;
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07004849 if (prev->p2p_group_interface !=
4850 NOT_P2P_GROUP_INTERFACE ||
4851 (prev->current_ssid &&
4852 prev->current_ssid->p2p_group))
4853 wpas_p2p_disconnect(prev);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004854 }
4855 return 0;
4856 }
4857
4858 for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
4859 if (os_strcmp(wpa_s->ifname, ifname) == 0)
4860 break;
4861 }
4862
4863 return wpas_p2p_disconnect(wpa_s);
4864}
4865
4866
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07004867static int wpas_p2p_select_go_freq(struct wpa_supplicant *wpa_s, int freq)
4868{
4869 unsigned int r;
4870
4871 if (freq == 2) {
4872 wpa_printf(MSG_DEBUG, "P2P: Request to start GO on 2.4 GHz "
4873 "band");
4874 if (wpa_s->best_24_freq > 0 &&
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004875 p2p_supported_freq_go(wpa_s->global->p2p,
4876 wpa_s->best_24_freq)) {
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07004877 freq = wpa_s->best_24_freq;
4878 wpa_printf(MSG_DEBUG, "P2P: Use best 2.4 GHz band "
4879 "channel: %d MHz", freq);
4880 } else {
4881 os_get_random((u8 *) &r, sizeof(r));
4882 freq = 2412 + (r % 3) * 25;
4883 wpa_printf(MSG_DEBUG, "P2P: Use random 2.4 GHz band "
4884 "channel: %d MHz", freq);
4885 }
4886 }
4887
4888 if (freq == 5) {
4889 wpa_printf(MSG_DEBUG, "P2P: Request to start GO on 5 GHz "
4890 "band");
4891 if (wpa_s->best_5_freq > 0 &&
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004892 p2p_supported_freq_go(wpa_s->global->p2p,
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07004893 wpa_s->best_5_freq)) {
4894 freq = wpa_s->best_5_freq;
4895 wpa_printf(MSG_DEBUG, "P2P: Use best 5 GHz band "
4896 "channel: %d MHz", freq);
4897 } else {
4898 os_get_random((u8 *) &r, sizeof(r));
4899 freq = 5180 + (r % 4) * 20;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004900 if (!p2p_supported_freq_go(wpa_s->global->p2p, freq)) {
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07004901 wpa_printf(MSG_DEBUG, "P2P: Could not select "
4902 "5 GHz channel for P2P group");
4903 return -1;
4904 }
4905 wpa_printf(MSG_DEBUG, "P2P: Use random 5 GHz band "
4906 "channel: %d MHz", freq);
4907 }
4908 }
4909
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004910 if (freq > 0 && !p2p_supported_freq_go(wpa_s->global->p2p, freq)) {
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07004911 wpa_printf(MSG_DEBUG, "P2P: The forced channel for GO "
4912 "(%u MHz) is not supported for P2P uses",
4913 freq);
4914 return -1;
4915 }
4916
4917 return freq;
4918}
4919
4920
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004921static int wpas_p2p_init_go_params(struct wpa_supplicant *wpa_s,
4922 struct p2p_go_neg_results *params,
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004923 int freq, int ht40, int vht,
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08004924 const struct p2p_channels *channels)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004925{
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07004926 int res, *freqs;
Dmitry Shmidt44c95782013-05-17 09:51:35 -07004927 unsigned int pref_freq;
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07004928 unsigned int num, i;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004929
4930 os_memset(params, 0, sizeof(*params));
4931 params->role_go = 1;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004932 params->ht40 = ht40;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004933 params->vht = vht;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004934 if (freq) {
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08004935 if (!freq_included(channels, freq)) {
4936 wpa_printf(MSG_DEBUG, "P2P: Forced GO freq %d MHz not "
4937 "accepted", freq);
4938 return -1;
4939 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004940 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on forced "
4941 "frequency %d MHz", freq);
4942 params->freq = freq;
4943 } else if (wpa_s->conf->p2p_oper_reg_class == 81 &&
4944 wpa_s->conf->p2p_oper_channel >= 1 &&
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08004945 wpa_s->conf->p2p_oper_channel <= 11 &&
4946 freq_included(channels,
4947 2407 + 5 * wpa_s->conf->p2p_oper_channel)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004948 params->freq = 2407 + 5 * wpa_s->conf->p2p_oper_channel;
4949 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on configured "
4950 "frequency %d MHz", params->freq);
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08004951 } else if ((wpa_s->conf->p2p_oper_reg_class == 115 ||
4952 wpa_s->conf->p2p_oper_reg_class == 116 ||
4953 wpa_s->conf->p2p_oper_reg_class == 117 ||
4954 wpa_s->conf->p2p_oper_reg_class == 124 ||
4955 wpa_s->conf->p2p_oper_reg_class == 126 ||
4956 wpa_s->conf->p2p_oper_reg_class == 127) &&
4957 freq_included(channels,
4958 5000 + 5 * wpa_s->conf->p2p_oper_channel)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004959 params->freq = 5000 + 5 * wpa_s->conf->p2p_oper_channel;
4960 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on configured "
4961 "frequency %d MHz", params->freq);
4962 } else if (wpa_s->conf->p2p_oper_channel == 0 &&
4963 wpa_s->best_overall_freq > 0 &&
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004964 p2p_supported_freq_go(wpa_s->global->p2p,
4965 wpa_s->best_overall_freq) &&
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08004966 freq_included(channels, wpa_s->best_overall_freq)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004967 params->freq = wpa_s->best_overall_freq;
4968 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on best overall "
4969 "channel %d MHz", params->freq);
4970 } else if (wpa_s->conf->p2p_oper_channel == 0 &&
4971 wpa_s->best_24_freq > 0 &&
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004972 p2p_supported_freq_go(wpa_s->global->p2p,
4973 wpa_s->best_24_freq) &&
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08004974 freq_included(channels, wpa_s->best_24_freq)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004975 params->freq = wpa_s->best_24_freq;
4976 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on best 2.4 GHz "
4977 "channel %d MHz", params->freq);
4978 } else if (wpa_s->conf->p2p_oper_channel == 0 &&
4979 wpa_s->best_5_freq > 0 &&
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004980 p2p_supported_freq_go(wpa_s->global->p2p,
4981 wpa_s->best_5_freq) &&
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08004982 freq_included(channels, wpa_s->best_5_freq)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004983 params->freq = wpa_s->best_5_freq;
4984 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on best 5 GHz "
4985 "channel %d MHz", params->freq);
Dmitry Shmidt44c95782013-05-17 09:51:35 -07004986 } else if ((pref_freq = p2p_get_pref_freq(wpa_s->global->p2p,
4987 channels))) {
4988 params->freq = pref_freq;
4989 wpa_printf(MSG_DEBUG, "P2P: Set GO freq %d MHz from preferred "
4990 "channels", params->freq);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004991 } else {
Dmitry Shmidt04949592012-07-19 12:16:46 -07004992 int chan;
4993 for (chan = 0; chan < 11; chan++) {
4994 params->freq = 2412 + chan * 5;
4995 if (!wpas_p2p_disallowed_freq(wpa_s->global,
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08004996 params->freq) &&
4997 freq_included(channels, params->freq))
Dmitry Shmidt04949592012-07-19 12:16:46 -07004998 break;
4999 }
5000 if (chan == 11) {
5001 wpa_printf(MSG_DEBUG, "P2P: No 2.4 GHz channel "
5002 "allowed");
5003 return -1;
5004 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005005 wpa_printf(MSG_DEBUG, "P2P: Set GO freq %d MHz (no preference "
5006 "known)", params->freq);
5007 }
5008
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07005009 freqs = os_calloc(wpa_s->num_multichan_concurrent, sizeof(int));
5010 if (!freqs)
5011 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005012
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07005013 res = wpas_p2p_valid_oper_freqs(wpa_s, freqs,
5014 wpa_s->num_multichan_concurrent);
5015 if (res < 0) {
5016 os_free(freqs);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005017 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005018 }
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07005019 num = res;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005020
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08005021 for (i = 0; i < num; i++) {
5022 if (freq && freqs[i] == freq)
5023 break;
5024 if (!freq && freq_included(channels, freqs[i])) {
5025 wpa_printf(MSG_DEBUG, "P2P: Force GO on a channel we are already using (%u MHz)",
5026 freqs[i]);
5027 params->freq = freqs[i];
5028 break;
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07005029 }
5030 }
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08005031
5032 if (i == num) {
5033 if (wpas_p2p_num_unused_channels(wpa_s) <= 0) {
5034 if (freq)
5035 wpa_printf(MSG_DEBUG, "P2P: Cannot force GO on freq (%u MHz) as all the channels are in use", freq);
5036 else
5037 wpa_printf(MSG_DEBUG, "P2P: Cannot force GO on any of the channels we are already using");
5038 os_free(freqs);
5039 return -1;
5040 } else if (num == 0) {
5041 wpa_printf(MSG_DEBUG, "P2P: Use one of the free channels");
5042 } else {
5043 wpa_printf(MSG_DEBUG, "P2P: Cannot force GO on any of the channels we are already using. Use one of the free channels");
5044 }
5045 }
5046
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07005047 os_free(freqs);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005048 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005049}
5050
5051
5052static struct wpa_supplicant *
5053wpas_p2p_get_group_iface(struct wpa_supplicant *wpa_s, int addr_allocated,
5054 int go)
5055{
5056 struct wpa_supplicant *group_wpa_s;
5057
Dmitry Shmidtaa532512012-09-24 10:35:31 -07005058 if (!wpas_p2p_create_iface(wpa_s)) {
5059 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Use same interface for group "
5060 "operations");
Dmitry Shmidt56052862013-10-04 10:23:25 -07005061 wpa_s->p2p_first_connection_timeout = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005062 return wpa_s;
Dmitry Shmidtaa532512012-09-24 10:35:31 -07005063 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005064
5065 if (wpas_p2p_add_group_interface(wpa_s, go ? WPA_IF_P2P_GO :
Dmitry Shmidtaa532512012-09-24 10:35:31 -07005066 WPA_IF_P2P_CLIENT) < 0) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07005067 wpa_msg_global(wpa_s, MSG_ERROR,
5068 "P2P: Failed to add group interface");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005069 return NULL;
Dmitry Shmidtaa532512012-09-24 10:35:31 -07005070 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005071 group_wpa_s = wpas_p2p_init_group_interface(wpa_s, go);
5072 if (group_wpa_s == NULL) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07005073 wpa_msg_global(wpa_s, MSG_ERROR,
5074 "P2P: Failed to initialize group interface");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005075 wpas_p2p_remove_pending_group_interface(wpa_s);
5076 return NULL;
5077 }
5078
Dmitry Shmidtaa532512012-09-24 10:35:31 -07005079 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Use separate group interface %s",
5080 group_wpa_s->ifname);
Dmitry Shmidt56052862013-10-04 10:23:25 -07005081 group_wpa_s->p2p_first_connection_timeout = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005082 return group_wpa_s;
5083}
5084
5085
5086/**
5087 * wpas_p2p_group_add - Add a new P2P group with local end as Group Owner
5088 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
5089 * @persistent_group: Whether to create a persistent group
5090 * @freq: Frequency for the group or 0 to indicate no hardcoding
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07005091 * @ht40: Start GO with 40 MHz channel width
5092 * @vht: Start GO with VHT support
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005093 * Returns: 0 on success, -1 on failure
5094 *
5095 * This function creates a new P2P group with the local end as the Group Owner,
5096 * i.e., without using Group Owner Negotiation.
5097 */
5098int wpas_p2p_group_add(struct wpa_supplicant *wpa_s, int persistent_group,
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07005099 int freq, int ht40, int vht)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005100{
5101 struct p2p_go_neg_results params;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005102
5103 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
5104 return -1;
5105
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07005106 os_free(wpa_s->global->add_psk);
5107 wpa_s->global->add_psk = NULL;
5108
Dmitry Shmidtdca39792011-09-06 11:17:33 -07005109 /* Make sure we are not running find during connection establishment */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005110 wpa_printf(MSG_DEBUG, "P2P: Stop any on-going P2P FIND");
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08005111 wpas_p2p_stop_find_oper(wpa_s);
Dmitry Shmidtdca39792011-09-06 11:17:33 -07005112
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07005113 freq = wpas_p2p_select_go_freq(wpa_s, freq);
5114 if (freq < 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005115 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005116
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07005117 if (wpas_p2p_init_go_params(wpa_s, &params, freq, ht40, vht, NULL))
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005118 return -1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07005119 if (params.freq &&
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07005120 !p2p_supported_freq_go(wpa_s->global->p2p, params.freq)) {
Dmitry Shmidt04949592012-07-19 12:16:46 -07005121 wpa_printf(MSG_DEBUG, "P2P: The selected channel for GO "
5122 "(%u MHz) is not supported for P2P uses",
5123 params.freq);
5124 return -1;
5125 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005126 p2p_go_params(wpa_s->global->p2p, &params);
5127 params.persistent_group = persistent_group;
5128
5129 wpa_s = wpas_p2p_get_group_iface(wpa_s, 0, 1);
5130 if (wpa_s == NULL)
5131 return -1;
5132 wpas_start_wps_go(wpa_s, &params, 0);
5133
5134 return 0;
5135}
5136
5137
5138static int wpas_start_p2p_client(struct wpa_supplicant *wpa_s,
5139 struct wpa_ssid *params, int addr_allocated)
5140{
5141 struct wpa_ssid *ssid;
5142
5143 wpa_s = wpas_p2p_get_group_iface(wpa_s, addr_allocated, 0);
5144 if (wpa_s == NULL)
5145 return -1;
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07005146 wpa_s->p2p_last_4way_hs_fail = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005147
5148 wpa_supplicant_ap_deinit(wpa_s);
5149
5150 ssid = wpa_config_add_network(wpa_s->conf);
5151 if (ssid == NULL)
5152 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005153 wpa_config_set_network_defaults(ssid);
5154 ssid->temporary = 1;
5155 ssid->proto = WPA_PROTO_RSN;
5156 ssid->pairwise_cipher = WPA_CIPHER_CCMP;
5157 ssid->group_cipher = WPA_CIPHER_CCMP;
5158 ssid->key_mgmt = WPA_KEY_MGMT_PSK;
5159 ssid->ssid = os_malloc(params->ssid_len);
5160 if (ssid->ssid == NULL) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005161 wpa_config_remove_network(wpa_s->conf, ssid->id);
5162 return -1;
5163 }
5164 os_memcpy(ssid->ssid, params->ssid, params->ssid_len);
5165 ssid->ssid_len = params->ssid_len;
5166 ssid->p2p_group = 1;
5167 ssid->export_keys = 1;
5168 if (params->psk_set) {
5169 os_memcpy(ssid->psk, params->psk, 32);
5170 ssid->psk_set = 1;
5171 }
5172 if (params->passphrase)
5173 ssid->passphrase = os_strdup(params->passphrase);
5174
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005175 wpa_s->show_group_started = 1;
5176
Dmitry Shmidtfa3fc4a2013-11-21 13:34:38 -08005177 wpa_supplicant_select_network(wpa_s, ssid);
5178
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005179 return 0;
5180}
5181
5182
5183int wpas_p2p_group_add_persistent(struct wpa_supplicant *wpa_s,
5184 struct wpa_ssid *ssid, int addr_allocated,
Dmitry Shmidt96be6222014-02-13 10:16:51 -08005185 int force_freq, int neg_freq, int ht40,
5186 int vht, const struct p2p_channels *channels,
Dmitry Shmidt56052862013-10-04 10:23:25 -07005187 int connection_timeout)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005188{
5189 struct p2p_go_neg_results params;
Dmitry Shmidt96be6222014-02-13 10:16:51 -08005190 int go = 0, freq;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005191
5192 if (ssid->disabled != 2 || ssid->ssid == NULL)
5193 return -1;
5194
5195 if (wpas_get_p2p_group(wpa_s, ssid->ssid, ssid->ssid_len, &go) &&
5196 go == (ssid->mode == WPAS_MODE_P2P_GO)) {
5197 wpa_printf(MSG_DEBUG, "P2P: Requested persistent group is "
5198 "already running");
5199 return 0;
5200 }
5201
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07005202 os_free(wpa_s->global->add_psk);
5203 wpa_s->global->add_psk = NULL;
5204
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005205 /* Make sure we are not running find during connection establishment */
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08005206 wpas_p2p_stop_find_oper(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005207
Dmitry Shmidt04949592012-07-19 12:16:46 -07005208 wpa_s->p2p_fallback_to_go_neg = 0;
5209
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005210 if (ssid->mode == WPAS_MODE_INFRA)
5211 return wpas_start_p2p_client(wpa_s, ssid, addr_allocated);
5212
5213 if (ssid->mode != WPAS_MODE_P2P_GO)
5214 return -1;
5215
Dmitry Shmidt96be6222014-02-13 10:16:51 -08005216 if (force_freq > 0) {
5217 freq = wpas_p2p_select_go_freq(wpa_s, force_freq);
5218 if (freq < 0)
5219 return -1;
5220 } else {
5221 freq = wpas_p2p_select_go_freq(wpa_s, neg_freq);
5222 if (freq < 0 || (freq > 0 && !freq_included(channels, freq)))
5223 freq = 0;
5224 }
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07005225
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07005226 if (wpas_p2p_init_go_params(wpa_s, &params, freq, ht40, vht, channels))
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005227 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005228
5229 params.role_go = 1;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08005230 params.psk_set = ssid->psk_set;
5231 if (params.psk_set)
5232 os_memcpy(params.psk, ssid->psk, sizeof(params.psk));
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08005233 if (ssid->passphrase) {
5234 if (os_strlen(ssid->passphrase) >= sizeof(params.passphrase)) {
5235 wpa_printf(MSG_ERROR, "P2P: Invalid passphrase in "
5236 "persistent group");
5237 return -1;
5238 }
5239 os_strlcpy(params.passphrase, ssid->passphrase,
5240 sizeof(params.passphrase));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005241 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005242 os_memcpy(params.ssid, ssid->ssid, ssid->ssid_len);
5243 params.ssid_len = ssid->ssid_len;
5244 params.persistent_group = 1;
5245
5246 wpa_s = wpas_p2p_get_group_iface(wpa_s, addr_allocated, 1);
5247 if (wpa_s == NULL)
5248 return -1;
5249
Dmitry Shmidt56052862013-10-04 10:23:25 -07005250 wpa_s->p2p_first_connection_timeout = connection_timeout;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005251 wpas_start_wps_go(wpa_s, &params, 0);
5252
5253 return 0;
5254}
5255
5256
5257static void wpas_p2p_ie_update(void *ctx, struct wpabuf *beacon_ies,
5258 struct wpabuf *proberesp_ies)
5259{
5260 struct wpa_supplicant *wpa_s = ctx;
5261 if (wpa_s->ap_iface) {
5262 struct hostapd_data *hapd = wpa_s->ap_iface->bss[0];
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005263 if (!(hapd->conf->p2p & P2P_GROUP_OWNER)) {
5264 wpabuf_free(beacon_ies);
5265 wpabuf_free(proberesp_ies);
5266 return;
5267 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005268 if (beacon_ies) {
5269 wpabuf_free(hapd->p2p_beacon_ie);
5270 hapd->p2p_beacon_ie = beacon_ies;
5271 }
5272 wpabuf_free(hapd->p2p_probe_resp_ie);
5273 hapd->p2p_probe_resp_ie = proberesp_ies;
5274 } else {
5275 wpabuf_free(beacon_ies);
5276 wpabuf_free(proberesp_ies);
5277 }
5278 wpa_supplicant_ap_update_beacon(wpa_s);
5279}
5280
5281
5282static void wpas_p2p_idle_update(void *ctx, int idle)
5283{
5284 struct wpa_supplicant *wpa_s = ctx;
5285 if (!wpa_s->ap_iface)
5286 return;
5287 wpa_printf(MSG_DEBUG, "P2P: GO - group %sidle", idle ? "" : "not ");
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005288 if (idle) {
5289 if (wpa_s->global->p2p_fail_on_wps_complete &&
5290 wpa_s->p2p_in_provisioning) {
5291 wpas_p2p_grpform_fail_after_wps(wpa_s);
5292 return;
5293 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005294 wpas_p2p_set_group_idle_timeout(wpa_s);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005295 } else
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005296 eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL);
5297}
5298
5299
5300struct p2p_group * wpas_p2p_group_init(struct wpa_supplicant *wpa_s,
Dmitry Shmidt04949592012-07-19 12:16:46 -07005301 struct wpa_ssid *ssid)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005302{
5303 struct p2p_group *group;
5304 struct p2p_group_config *cfg;
5305
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005306 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
5307 return NULL;
5308
5309 cfg = os_zalloc(sizeof(*cfg));
5310 if (cfg == NULL)
5311 return NULL;
5312
Dmitry Shmidt04949592012-07-19 12:16:46 -07005313 if (ssid->p2p_persistent_group && wpa_s->conf->persistent_reconnect)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005314 cfg->persistent_group = 2;
Dmitry Shmidt04949592012-07-19 12:16:46 -07005315 else if (ssid->p2p_persistent_group)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005316 cfg->persistent_group = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005317 os_memcpy(cfg->interface_addr, wpa_s->own_addr, ETH_ALEN);
5318 if (wpa_s->max_stations &&
5319 wpa_s->max_stations < wpa_s->conf->max_num_sta)
5320 cfg->max_clients = wpa_s->max_stations;
5321 else
5322 cfg->max_clients = wpa_s->conf->max_num_sta;
Dmitry Shmidt04949592012-07-19 12:16:46 -07005323 os_memcpy(cfg->ssid, ssid->ssid, ssid->ssid_len);
5324 cfg->ssid_len = ssid->ssid_len;
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08005325 cfg->freq = ssid->frequency;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005326 cfg->cb_ctx = wpa_s;
5327 cfg->ie_update = wpas_p2p_ie_update;
5328 cfg->idle_update = wpas_p2p_idle_update;
5329
5330 group = p2p_group_init(wpa_s->global->p2p, cfg);
5331 if (group == NULL)
5332 os_free(cfg);
Dmitry Shmidt04949592012-07-19 12:16:46 -07005333 if (ssid->mode != WPAS_MODE_P2P_GROUP_FORMATION)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005334 p2p_group_notif_formation_done(group);
5335 wpa_s->p2p_group = group;
5336 return group;
5337}
5338
5339
5340void wpas_p2p_wps_success(struct wpa_supplicant *wpa_s, const u8 *peer_addr,
5341 int registrar)
5342{
Dmitry Shmidt04949592012-07-19 12:16:46 -07005343 struct wpa_ssid *ssid = wpa_s->current_ssid;
5344
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005345 if (!wpa_s->p2p_in_provisioning) {
5346 wpa_printf(MSG_DEBUG, "P2P: Ignore WPS success event - P2P "
5347 "provisioning not in progress");
5348 return;
5349 }
5350
Dmitry Shmidt04949592012-07-19 12:16:46 -07005351 if (ssid && ssid->mode == WPAS_MODE_INFRA) {
5352 u8 go_dev_addr[ETH_ALEN];
5353 os_memcpy(go_dev_addr, wpa_s->bssid, ETH_ALEN);
5354 wpas_p2p_persistent_group(wpa_s, go_dev_addr, ssid->ssid,
5355 ssid->ssid_len);
5356 /* Clear any stored provisioning info */
5357 p2p_clear_provisioning_info(wpa_s->global->p2p, go_dev_addr);
5358 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005359
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005360 eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s->parent,
5361 NULL);
Dmitry Shmidt56052862013-10-04 10:23:25 -07005362 wpa_s->p2p_go_group_formation_completed = 1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07005363 if (ssid && ssid->mode == WPAS_MODE_INFRA) {
5364 /*
5365 * Use a separate timeout for initial data connection to
5366 * complete to allow the group to be removed automatically if
5367 * something goes wrong in this step before the P2P group idle
5368 * timeout mechanism is taken into use.
5369 */
Dmitry Shmidt56052862013-10-04 10:23:25 -07005370 wpa_dbg(wpa_s, MSG_DEBUG,
5371 "P2P: Re-start group formation timeout (%d seconds) as client for initial connection",
5372 P2P_MAX_INITIAL_CONN_WAIT);
Dmitry Shmidt04949592012-07-19 12:16:46 -07005373 eloop_register_timeout(P2P_MAX_INITIAL_CONN_WAIT, 0,
5374 wpas_p2p_group_formation_timeout,
5375 wpa_s->parent, NULL);
Dmitry Shmidt56052862013-10-04 10:23:25 -07005376 } else if (ssid) {
5377 /*
5378 * Use a separate timeout for initial data connection to
5379 * complete to allow the group to be removed automatically if
5380 * the client does not complete data connection successfully.
5381 */
5382 wpa_dbg(wpa_s, MSG_DEBUG,
5383 "P2P: Re-start group formation timeout (%d seconds) as GO for initial connection",
5384 P2P_MAX_INITIAL_CONN_WAIT_GO);
5385 eloop_register_timeout(P2P_MAX_INITIAL_CONN_WAIT_GO, 0,
5386 wpas_p2p_group_formation_timeout,
5387 wpa_s->parent, NULL);
5388 /*
5389 * Complete group formation on first successful data connection
5390 */
5391 wpa_s->p2p_go_group_formation_completed = 0;
Dmitry Shmidt04949592012-07-19 12:16:46 -07005392 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005393 if (wpa_s->global->p2p)
5394 p2p_wps_success_cb(wpa_s->global->p2p, peer_addr);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005395 wpas_group_formation_completed(wpa_s, 1);
5396}
5397
5398
Jouni Malinen75ecf522011-06-27 15:19:46 -07005399void wpas_p2p_wps_failed(struct wpa_supplicant *wpa_s,
5400 struct wps_event_fail *fail)
5401{
5402 if (!wpa_s->p2p_in_provisioning) {
5403 wpa_printf(MSG_DEBUG, "P2P: Ignore WPS fail event - P2P "
5404 "provisioning not in progress");
5405 return;
5406 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005407
5408 if (wpa_s->go_params) {
5409 p2p_clear_provisioning_info(
5410 wpa_s->global->p2p,
Dmitry Shmidt04949592012-07-19 12:16:46 -07005411 wpa_s->go_params->peer_device_addr);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005412 }
5413
Jouni Malinen75ecf522011-06-27 15:19:46 -07005414 wpas_notify_p2p_wps_failed(wpa_s, fail);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005415
5416 if (wpa_s == wpa_s->global->p2p_group_formation) {
5417 /*
5418 * Allow some time for the failed WPS negotiation exchange to
5419 * complete, but remove the group since group formation cannot
5420 * succeed after provisioning failure.
5421 */
5422 wpa_printf(MSG_DEBUG, "P2P: WPS step failed during group formation - reject connection from timeout");
5423 wpa_s->global->p2p_fail_on_wps_complete = 1;
5424 eloop_deplete_timeout(0, 50000,
5425 wpas_p2p_group_formation_timeout,
5426 wpa_s->parent, NULL);
5427 }
5428}
5429
5430
5431int wpas_p2p_wps_eapol_cb(struct wpa_supplicant *wpa_s)
5432{
5433 if (!wpa_s->global->p2p_fail_on_wps_complete ||
5434 !wpa_s->p2p_in_provisioning)
5435 return 0;
5436
5437 wpas_p2p_grpform_fail_after_wps(wpa_s);
5438
5439 return 1;
Jouni Malinen75ecf522011-06-27 15:19:46 -07005440}
5441
5442
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005443int wpas_p2p_prov_disc(struct wpa_supplicant *wpa_s, const u8 *peer_addr,
Dmitry Shmidt04949592012-07-19 12:16:46 -07005444 const char *config_method,
5445 enum wpas_p2p_prov_disc_use use)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005446{
5447 u16 config_methods;
5448
Dmitry Shmidt04949592012-07-19 12:16:46 -07005449 wpa_s->p2p_fallback_to_go_neg = 0;
5450 wpa_s->pending_pd_use = NORMAL_PD;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005451 if (os_strncmp(config_method, "display", 7) == 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005452 config_methods = WPS_CONFIG_DISPLAY;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005453 else if (os_strncmp(config_method, "keypad", 6) == 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005454 config_methods = WPS_CONFIG_KEYPAD;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005455 else if (os_strncmp(config_method, "pbc", 3) == 0 ||
5456 os_strncmp(config_method, "pushbutton", 10) == 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005457 config_methods = WPS_CONFIG_PUSHBUTTON;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005458 else {
5459 wpa_printf(MSG_DEBUG, "P2P: Unknown config method");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005460 return -1;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005461 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005462
Dmitry Shmidt04949592012-07-19 12:16:46 -07005463 if (use == WPAS_P2P_PD_AUTO) {
5464 os_memcpy(wpa_s->pending_join_dev_addr, peer_addr, ETH_ALEN);
5465 wpa_s->pending_pd_config_methods = config_methods;
5466 wpa_s->p2p_auto_pd = 1;
5467 wpa_s->p2p_auto_join = 0;
5468 wpa_s->pending_pd_before_join = 0;
5469 wpa_s->auto_pd_scan_retry = 0;
5470 wpas_p2p_stop_find(wpa_s);
5471 wpa_s->p2p_join_scan_count = 0;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005472 os_get_reltime(&wpa_s->p2p_auto_started);
Dmitry Shmidt04949592012-07-19 12:16:46 -07005473 wpa_printf(MSG_DEBUG, "P2P: Auto PD started at %ld.%06ld",
5474 wpa_s->p2p_auto_started.sec,
5475 wpa_s->p2p_auto_started.usec);
5476 wpas_p2p_join_scan(wpa_s, NULL);
5477 return 0;
5478 }
5479
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005480 if (wpa_s->global->p2p == NULL || wpa_s->global->p2p_disabled)
5481 return -1;
5482
5483 return p2p_prov_disc_req(wpa_s->global->p2p, peer_addr,
Dmitry Shmidt04949592012-07-19 12:16:46 -07005484 config_methods, use == WPAS_P2P_PD_FOR_JOIN,
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08005485 0, 1);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005486}
5487
5488
5489int wpas_p2p_scan_result_text(const u8 *ies, size_t ies_len, char *buf,
5490 char *end)
5491{
5492 return p2p_scan_result_text(ies, ies_len, buf, end);
5493}
5494
5495
5496static void wpas_p2p_clear_pending_action_tx(struct wpa_supplicant *wpa_s)
5497{
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08005498 if (!offchannel_pending_action_tx(wpa_s))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005499 return;
5500
5501 wpa_printf(MSG_DEBUG, "P2P: Drop pending Action TX due to new "
5502 "operation request");
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08005503 offchannel_clear_pending_action_tx(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005504}
5505
5506
5507int wpas_p2p_find(struct wpa_supplicant *wpa_s, unsigned int timeout,
5508 enum p2p_discovery_type type,
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08005509 unsigned int num_req_dev_types, const u8 *req_dev_types,
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005510 const u8 *dev_id, unsigned int search_delay)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005511{
5512 wpas_p2p_clear_pending_action_tx(wpa_s);
5513 wpa_s->p2p_long_listen = 0;
5514
Dmitry Shmidt04949592012-07-19 12:16:46 -07005515 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL ||
5516 wpa_s->p2p_in_provisioning)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005517 return -1;
5518
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08005519 wpa_supplicant_cancel_sched_scan(wpa_s);
5520
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005521 return p2p_find(wpa_s->global->p2p, timeout, type,
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005522 num_req_dev_types, req_dev_types, dev_id,
5523 search_delay);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005524}
5525
5526
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08005527static int wpas_p2p_stop_find_oper(struct wpa_supplicant *wpa_s)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005528{
5529 wpas_p2p_clear_pending_action_tx(wpa_s);
5530 wpa_s->p2p_long_listen = 0;
5531 eloop_cancel_timeout(wpas_p2p_long_listen_timeout, wpa_s, NULL);
5532 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005533
5534 if (wpa_s->global->p2p)
5535 p2p_stop_find(wpa_s->global->p2p);
5536
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08005537 return 0;
5538}
5539
5540
5541void wpas_p2p_stop_find(struct wpa_supplicant *wpa_s)
5542{
5543 if (wpas_p2p_stop_find_oper(wpa_s) > 0)
5544 return;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005545 wpas_p2p_remove_pending_group_interface(wpa_s);
5546}
5547
5548
5549static void wpas_p2p_long_listen_timeout(void *eloop_ctx, void *timeout_ctx)
5550{
5551 struct wpa_supplicant *wpa_s = eloop_ctx;
5552 wpa_s->p2p_long_listen = 0;
5553}
5554
5555
5556int wpas_p2p_listen(struct wpa_supplicant *wpa_s, unsigned int timeout)
5557{
5558 int res;
5559
5560 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
5561 return -1;
5562
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08005563 wpa_supplicant_cancel_sched_scan(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005564 wpas_p2p_clear_pending_action_tx(wpa_s);
5565
5566 if (timeout == 0) {
5567 /*
5568 * This is a request for unlimited Listen state. However, at
5569 * least for now, this is mapped to a Listen state for one
5570 * hour.
5571 */
5572 timeout = 3600;
5573 }
5574 eloop_cancel_timeout(wpas_p2p_long_listen_timeout, wpa_s, NULL);
5575 wpa_s->p2p_long_listen = 0;
5576
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005577 /*
5578 * Stop previous find/listen operation to avoid trying to request a new
5579 * remain-on-channel operation while the driver is still running the
5580 * previous one.
5581 */
5582 if (wpa_s->global->p2p)
5583 p2p_stop_find(wpa_s->global->p2p);
5584
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005585 res = wpas_p2p_listen_start(wpa_s, timeout * 1000);
5586 if (res == 0 && timeout * 1000 > wpa_s->max_remain_on_chan) {
5587 wpa_s->p2p_long_listen = timeout * 1000;
5588 eloop_register_timeout(timeout, 0,
5589 wpas_p2p_long_listen_timeout,
5590 wpa_s, NULL);
5591 }
5592
5593 return res;
5594}
5595
5596
5597int wpas_p2p_assoc_req_ie(struct wpa_supplicant *wpa_s, struct wpa_bss *bss,
5598 u8 *buf, size_t len, int p2p_group)
5599{
5600 struct wpabuf *p2p_ie;
5601 int ret;
5602
5603 if (wpa_s->global->p2p_disabled)
5604 return -1;
5605 if (wpa_s->global->p2p == NULL)
5606 return -1;
5607 if (bss == NULL)
5608 return -1;
5609
5610 p2p_ie = wpa_bss_get_vendor_ie_multi(bss, P2P_IE_VENDOR_TYPE);
5611 ret = p2p_assoc_req_ie(wpa_s->global->p2p, bss->bssid, buf, len,
5612 p2p_group, p2p_ie);
5613 wpabuf_free(p2p_ie);
5614
5615 return ret;
5616}
5617
5618
5619int wpas_p2p_probe_req_rx(struct wpa_supplicant *wpa_s, const u8 *addr,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005620 const u8 *dst, const u8 *bssid,
Dmitry Shmidt04949592012-07-19 12:16:46 -07005621 const u8 *ie, size_t ie_len, int ssi_signal)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005622{
5623 if (wpa_s->global->p2p_disabled)
5624 return 0;
5625 if (wpa_s->global->p2p == NULL)
5626 return 0;
5627
Dmitry Shmidt04949592012-07-19 12:16:46 -07005628 switch (p2p_probe_req_rx(wpa_s->global->p2p, addr, dst, bssid,
5629 ie, ie_len)) {
5630 case P2P_PREQ_NOT_P2P:
5631 wpas_notify_preq(wpa_s, addr, dst, bssid, ie, ie_len,
5632 ssi_signal);
5633 /* fall through */
5634 case P2P_PREQ_MALFORMED:
5635 case P2P_PREQ_NOT_LISTEN:
5636 case P2P_PREQ_NOT_PROCESSED:
5637 default: /* make gcc happy */
5638 return 0;
5639 case P2P_PREQ_PROCESSED:
5640 return 1;
5641 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005642}
5643
5644
5645void wpas_p2p_rx_action(struct wpa_supplicant *wpa_s, const u8 *da,
5646 const u8 *sa, const u8 *bssid,
5647 u8 category, const u8 *data, size_t len, int freq)
5648{
5649 if (wpa_s->global->p2p_disabled)
5650 return;
5651 if (wpa_s->global->p2p == NULL)
5652 return;
5653
5654 p2p_rx_action(wpa_s->global->p2p, da, sa, bssid, category, data, len,
5655 freq);
5656}
5657
5658
5659void wpas_p2p_scan_ie(struct wpa_supplicant *wpa_s, struct wpabuf *ies)
5660{
5661 if (wpa_s->global->p2p_disabled)
5662 return;
5663 if (wpa_s->global->p2p == NULL)
5664 return;
5665
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08005666 p2p_scan_ie(wpa_s->global->p2p, ies, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005667}
5668
5669
5670void wpas_p2p_group_deinit(struct wpa_supplicant *wpa_s)
5671{
5672 p2p_group_deinit(wpa_s->p2p_group);
5673 wpa_s->p2p_group = NULL;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005674
5675 wpa_s->ap_configured_cb = NULL;
5676 wpa_s->ap_configured_cb_ctx = NULL;
5677 wpa_s->ap_configured_cb_data = NULL;
5678 wpa_s->connect_without_scan = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005679}
5680
5681
5682int wpas_p2p_reject(struct wpa_supplicant *wpa_s, const u8 *addr)
5683{
5684 wpa_s->p2p_long_listen = 0;
5685
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005686 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
5687 return -1;
5688
5689 return p2p_reject(wpa_s->global->p2p, addr);
5690}
5691
5692
5693/* Invite to reinvoke a persistent group */
5694int wpas_p2p_invite(struct wpa_supplicant *wpa_s, const u8 *peer_addr,
Jouni Malinen31be0a42012-08-31 21:20:51 +03005695 struct wpa_ssid *ssid, const u8 *go_dev_addr, int freq,
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07005696 int ht40, int vht, int pref_freq)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005697{
5698 enum p2p_invite_role role;
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07005699 u8 *bssid = NULL;
5700 int force_freq = 0;
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08005701 int res;
Dmitry Shmidta0d265f2013-11-19 13:13:41 -08005702 int no_pref_freq_given = pref_freq == 0;
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08005703
Dmitry Shmidt700a1372013-03-15 14:14:44 -07005704 wpa_s->global->p2p_invite_group = NULL;
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08005705 if (peer_addr)
5706 os_memcpy(wpa_s->p2p_auth_invite, peer_addr, ETH_ALEN);
5707 else
5708 os_memset(wpa_s->p2p_auth_invite, 0, ETH_ALEN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005709
Jouni Malinen31be0a42012-08-31 21:20:51 +03005710 wpa_s->p2p_persistent_go_freq = freq;
5711 wpa_s->p2p_go_ht40 = !!ht40;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005712 if (ssid->mode == WPAS_MODE_P2P_GO) {
5713 role = P2P_INVITE_ROLE_GO;
5714 if (peer_addr == NULL) {
5715 wpa_printf(MSG_DEBUG, "P2P: Missing peer "
5716 "address in invitation command");
5717 return -1;
5718 }
5719 if (wpas_p2p_create_iface(wpa_s)) {
5720 if (wpas_p2p_add_group_interface(wpa_s,
5721 WPA_IF_P2P_GO) < 0) {
5722 wpa_printf(MSG_ERROR, "P2P: Failed to "
5723 "allocate a new interface for the "
5724 "group");
5725 return -1;
5726 }
5727 bssid = wpa_s->pending_interface_addr;
5728 } else
5729 bssid = wpa_s->own_addr;
5730 } else {
5731 role = P2P_INVITE_ROLE_CLIENT;
5732 peer_addr = ssid->bssid;
5733 }
5734 wpa_s->pending_invite_ssid_id = ssid->id;
5735
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07005736 res = wpas_p2p_setup_freqs(wpa_s, freq, &force_freq, &pref_freq,
5737 role == P2P_INVITE_ROLE_GO);
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08005738 if (res)
5739 return res;
Irfan Sheriffaf84a572012-09-22 16:59:30 -07005740
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005741 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
5742 return -1;
5743
Dmitry Shmidta0d265f2013-11-19 13:13:41 -08005744 if (wpa_s->parent->conf->p2p_ignore_shared_freq &&
5745 no_pref_freq_given && pref_freq > 0 &&
5746 wpa_s->num_multichan_concurrent > 1 &&
5747 wpas_p2p_num_unused_channels(wpa_s) > 0) {
5748 wpa_printf(MSG_DEBUG, "P2P: Ignore own channel preference %d MHz for invitation due to p2p_ignore_shared_freq=1 configuration",
5749 pref_freq);
5750 pref_freq = 0;
5751 }
5752
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005753 return p2p_invite(wpa_s->global->p2p, peer_addr, role, bssid,
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08005754 ssid->ssid, ssid->ssid_len, force_freq, go_dev_addr,
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08005755 1, pref_freq, -1);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005756}
5757
5758
5759/* Invite to join an active group */
5760int wpas_p2p_invite_group(struct wpa_supplicant *wpa_s, const char *ifname,
5761 const u8 *peer_addr, const u8 *go_dev_addr)
5762{
5763 struct wpa_global *global = wpa_s->global;
5764 enum p2p_invite_role role;
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07005765 u8 *bssid = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005766 struct wpa_ssid *ssid;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08005767 int persistent;
Dmitry Shmidt051af732013-10-22 13:52:46 -07005768 int freq = 0, force_freq = 0, pref_freq = 0;
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08005769 int res;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005770
Jouni Malinen31be0a42012-08-31 21:20:51 +03005771 wpa_s->p2p_persistent_go_freq = 0;
5772 wpa_s->p2p_go_ht40 = 0;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07005773 wpa_s->p2p_go_vht = 0;
Jouni Malinen31be0a42012-08-31 21:20:51 +03005774
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005775 for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
5776 if (os_strcmp(wpa_s->ifname, ifname) == 0)
5777 break;
5778 }
5779 if (wpa_s == NULL) {
5780 wpa_printf(MSG_DEBUG, "P2P: Interface '%s' not found", ifname);
5781 return -1;
5782 }
5783
5784 ssid = wpa_s->current_ssid;
5785 if (ssid == NULL) {
5786 wpa_printf(MSG_DEBUG, "P2P: No current SSID to use for "
5787 "invitation");
5788 return -1;
5789 }
5790
Dmitry Shmidt700a1372013-03-15 14:14:44 -07005791 wpa_s->global->p2p_invite_group = wpa_s;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08005792 persistent = ssid->p2p_persistent_group &&
5793 wpas_p2p_get_persistent(wpa_s->parent, peer_addr,
5794 ssid->ssid, ssid->ssid_len);
5795
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005796 if (ssid->mode == WPAS_MODE_P2P_GO) {
5797 role = P2P_INVITE_ROLE_ACTIVE_GO;
5798 bssid = wpa_s->own_addr;
5799 if (go_dev_addr == NULL)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005800 go_dev_addr = wpa_s->global->p2p_dev_addr;
Dmitry Shmidt051af732013-10-22 13:52:46 -07005801 freq = ssid->frequency;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005802 } else {
5803 role = P2P_INVITE_ROLE_CLIENT;
5804 if (wpa_s->wpa_state < WPA_ASSOCIATED) {
5805 wpa_printf(MSG_DEBUG, "P2P: Not associated - cannot "
5806 "invite to current group");
5807 return -1;
5808 }
5809 bssid = wpa_s->bssid;
5810 if (go_dev_addr == NULL &&
5811 !is_zero_ether_addr(wpa_s->go_dev_addr))
5812 go_dev_addr = wpa_s->go_dev_addr;
Dmitry Shmidt051af732013-10-22 13:52:46 -07005813 freq = wpa_s->current_bss ? wpa_s->current_bss->freq :
5814 (int) wpa_s->assoc_freq;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005815 }
5816 wpa_s->parent->pending_invite_ssid_id = -1;
5817
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005818 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
5819 return -1;
5820
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07005821 res = wpas_p2p_setup_freqs(wpa_s, freq, &force_freq, &pref_freq,
5822 role == P2P_INVITE_ROLE_ACTIVE_GO);
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08005823 if (res)
5824 return res;
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07005825 wpas_p2p_set_own_freq_preference(wpa_s, force_freq);
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08005826
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005827 return p2p_invite(wpa_s->global->p2p, peer_addr, role, bssid,
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08005828 ssid->ssid, ssid->ssid_len, force_freq,
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08005829 go_dev_addr, persistent, pref_freq, -1);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005830}
5831
5832
5833void wpas_p2p_completed(struct wpa_supplicant *wpa_s)
5834{
5835 struct wpa_ssid *ssid = wpa_s->current_ssid;
5836 const char *ssid_txt;
5837 u8 go_dev_addr[ETH_ALEN];
Jouni Malinen75ecf522011-06-27 15:19:46 -07005838 int network_id = -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005839 int persistent;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005840 int freq;
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08005841 u8 ip[3 * 4];
5842 char ip_addr[100];
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005843
Dmitry Shmidt04949592012-07-19 12:16:46 -07005844 if (ssid == NULL || ssid->mode != WPAS_MODE_P2P_GROUP_FORMATION) {
5845 eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
5846 wpa_s->parent, NULL);
5847 }
5848
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005849 if (!wpa_s->show_group_started || !ssid)
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005850 return;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005851
5852 wpa_s->show_group_started = 0;
5853
5854 ssid_txt = wpa_ssid_txt(ssid->ssid, ssid->ssid_len);
5855 os_memset(go_dev_addr, 0, ETH_ALEN);
5856 if (ssid->bssid_set)
5857 os_memcpy(go_dev_addr, ssid->bssid, ETH_ALEN);
5858 persistent = wpas_p2p_persistent_group(wpa_s, go_dev_addr, ssid->ssid,
5859 ssid->ssid_len);
5860 os_memcpy(wpa_s->go_dev_addr, go_dev_addr, ETH_ALEN);
5861
5862 if (wpa_s->global->p2p_group_formation == wpa_s)
5863 wpa_s->global->p2p_group_formation = NULL;
5864
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005865 freq = wpa_s->current_bss ? wpa_s->current_bss->freq :
5866 (int) wpa_s->assoc_freq;
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08005867
5868 ip_addr[0] = '\0';
5869 if (wpa_sm_get_p2p_ip_addr(wpa_s->wpa, ip) == 0) {
5870 os_snprintf(ip_addr, sizeof(ip_addr), " ip_addr=%u.%u.%u.%u "
5871 "ip_mask=%u.%u.%u.%u go_ip_addr=%u.%u.%u.%u",
5872 ip[0], ip[1], ip[2], ip[3],
5873 ip[4], ip[5], ip[6], ip[7],
5874 ip[8], ip[9], ip[10], ip[11]);
5875 }
5876
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005877 if (ssid->passphrase == NULL && ssid->psk_set) {
5878 char psk[65];
5879 wpa_snprintf_hex(psk, sizeof(psk), ssid->psk, 32);
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07005880 wpa_msg_global(wpa_s->parent, MSG_INFO, P2P_EVENT_GROUP_STARTED
5881 "%s client ssid=\"%s\" freq=%d psk=%s "
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08005882 "go_dev_addr=" MACSTR "%s%s",
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07005883 wpa_s->ifname, ssid_txt, freq, psk,
5884 MAC2STR(go_dev_addr),
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08005885 persistent ? " [PERSISTENT]" : "", ip_addr);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005886 } else {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07005887 wpa_msg_global(wpa_s->parent, MSG_INFO, P2P_EVENT_GROUP_STARTED
5888 "%s client ssid=\"%s\" freq=%d "
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08005889 "passphrase=\"%s\" go_dev_addr=" MACSTR "%s%s",
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07005890 wpa_s->ifname, ssid_txt, freq,
5891 ssid->passphrase ? ssid->passphrase : "",
5892 MAC2STR(go_dev_addr),
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08005893 persistent ? " [PERSISTENT]" : "", ip_addr);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005894 }
5895
5896 if (persistent)
Jouni Malinen75ecf522011-06-27 15:19:46 -07005897 network_id = wpas_p2p_store_persistent_group(wpa_s->parent,
5898 ssid, go_dev_addr);
5899 if (network_id < 0)
5900 network_id = ssid->id;
5901 wpas_notify_p2p_group_started(wpa_s, ssid, network_id, 1);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005902}
5903
5904
5905int wpas_p2p_presence_req(struct wpa_supplicant *wpa_s, u32 duration1,
5906 u32 interval1, u32 duration2, u32 interval2)
5907{
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005908 int ret;
5909
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005910 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
5911 return -1;
5912
5913 if (wpa_s->wpa_state < WPA_ASSOCIATED ||
5914 wpa_s->current_ssid == NULL ||
5915 wpa_s->current_ssid->mode != WPAS_MODE_INFRA)
5916 return -1;
5917
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005918 ret = p2p_presence_req(wpa_s->global->p2p, wpa_s->bssid,
5919 wpa_s->own_addr, wpa_s->assoc_freq,
5920 duration1, interval1, duration2, interval2);
5921 if (ret == 0)
5922 wpa_s->waiting_presence_resp = 1;
5923
5924 return ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005925}
5926
5927
5928int wpas_p2p_ext_listen(struct wpa_supplicant *wpa_s, unsigned int period,
5929 unsigned int interval)
5930{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005931 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
5932 return -1;
5933
5934 return p2p_ext_listen(wpa_s->global->p2p, period, interval);
5935}
5936
5937
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005938static int wpas_p2p_is_client(struct wpa_supplicant *wpa_s)
5939{
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08005940 if (wpa_s->current_ssid == NULL) {
5941 /*
5942 * current_ssid can be cleared when P2P client interface gets
5943 * disconnected, so assume this interface was used as P2P
5944 * client.
5945 */
5946 return 1;
5947 }
5948 return wpa_s->current_ssid->p2p_group &&
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005949 wpa_s->current_ssid->mode == WPAS_MODE_INFRA;
5950}
5951
5952
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005953static void wpas_p2p_group_idle_timeout(void *eloop_ctx, void *timeout_ctx)
5954{
5955 struct wpa_supplicant *wpa_s = eloop_ctx;
5956
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005957 if (wpa_s->conf->p2p_group_idle == 0 && !wpas_p2p_is_client(wpa_s)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005958 wpa_printf(MSG_DEBUG, "P2P: Ignore group idle timeout - "
5959 "disabled");
5960 return;
5961 }
5962
Dmitry Shmidt04949592012-07-19 12:16:46 -07005963 wpa_printf(MSG_DEBUG, "P2P: Group idle timeout reached - terminate "
5964 "group");
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005965 wpas_p2p_group_delete(wpa_s, P2P_GROUP_REMOVAL_IDLE_TIMEOUT);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005966}
5967
5968
5969static void wpas_p2p_set_group_idle_timeout(struct wpa_supplicant *wpa_s)
5970{
Dmitry Shmidt04949592012-07-19 12:16:46 -07005971 int timeout;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005972
Dmitry Shmidt04949592012-07-19 12:16:46 -07005973 if (eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL) > 0)
5974 wpa_printf(MSG_DEBUG, "P2P: Cancelled P2P group idle timeout");
5975
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005976 if (wpa_s->current_ssid == NULL || !wpa_s->current_ssid->p2p_group)
5977 return;
5978
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005979 timeout = wpa_s->conf->p2p_group_idle;
5980 if (wpa_s->current_ssid->mode == WPAS_MODE_INFRA &&
5981 (timeout == 0 || timeout > P2P_MAX_CLIENT_IDLE))
5982 timeout = P2P_MAX_CLIENT_IDLE;
5983
5984 if (timeout == 0)
5985 return;
5986
Dmitry Shmidt04949592012-07-19 12:16:46 -07005987 if (timeout < 0) {
5988 if (wpa_s->current_ssid->mode == WPAS_MODE_INFRA)
5989 timeout = 0; /* special client mode no-timeout */
5990 else
5991 return;
5992 }
5993
5994 if (wpa_s->p2p_in_provisioning) {
5995 /*
5996 * Use the normal group formation timeout during the
5997 * provisioning phase to avoid terminating this process too
5998 * early due to group idle timeout.
5999 */
6000 wpa_printf(MSG_DEBUG, "P2P: Do not use P2P group idle timeout "
6001 "during provisioning");
6002 return;
6003 }
Deepthi Gowri9e4e8ac2013-04-16 11:57:05 +05306004
Dmitry Shmidt04949592012-07-19 12:16:46 -07006005 if (wpa_s->show_group_started) {
6006 /*
6007 * Use the normal group formation timeout between the end of
6008 * the provisioning phase and completion of 4-way handshake to
6009 * avoid terminating this process too early due to group idle
6010 * timeout.
6011 */
6012 wpa_printf(MSG_DEBUG, "P2P: Do not use P2P group idle timeout "
6013 "while waiting for initial 4-way handshake to "
6014 "complete");
6015 return;
6016 }
6017
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006018 wpa_printf(MSG_DEBUG, "P2P: Set P2P group idle timeout to %u seconds",
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006019 timeout);
6020 eloop_register_timeout(timeout, 0, wpas_p2p_group_idle_timeout,
6021 wpa_s, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006022}
6023
6024
Jouni Malinen2b89da82012-08-31 22:04:41 +03006025/* Returns 1 if the interface was removed */
6026int wpas_p2p_deauth_notif(struct wpa_supplicant *wpa_s, const u8 *bssid,
6027 u16 reason_code, const u8 *ie, size_t ie_len,
6028 int locally_generated)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006029{
6030 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
Jouni Malinen2b89da82012-08-31 22:04:41 +03006031 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006032
Dmitry Shmidt04949592012-07-19 12:16:46 -07006033 if (!locally_generated)
6034 p2p_deauth_notif(wpa_s->global->p2p, bssid, reason_code, ie,
6035 ie_len);
6036
6037 if (reason_code == WLAN_REASON_DEAUTH_LEAVING && !locally_generated &&
6038 wpa_s->current_ssid &&
6039 wpa_s->current_ssid->p2p_group &&
6040 wpa_s->current_ssid->mode == WPAS_MODE_INFRA) {
6041 wpa_printf(MSG_DEBUG, "P2P: GO indicated that the P2P Group "
6042 "session is ending");
Jouni Malinen2b89da82012-08-31 22:04:41 +03006043 if (wpas_p2p_group_delete(wpa_s,
6044 P2P_GROUP_REMOVAL_GO_ENDING_SESSION)
6045 > 0)
6046 return 1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07006047 }
Jouni Malinen2b89da82012-08-31 22:04:41 +03006048
6049 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006050}
6051
6052
6053void wpas_p2p_disassoc_notif(struct wpa_supplicant *wpa_s, const u8 *bssid,
Dmitry Shmidt04949592012-07-19 12:16:46 -07006054 u16 reason_code, const u8 *ie, size_t ie_len,
6055 int locally_generated)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006056{
6057 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
6058 return;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006059
Dmitry Shmidt04949592012-07-19 12:16:46 -07006060 if (!locally_generated)
6061 p2p_disassoc_notif(wpa_s->global->p2p, bssid, reason_code, ie,
6062 ie_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006063}
6064
6065
6066void wpas_p2p_update_config(struct wpa_supplicant *wpa_s)
6067{
6068 struct p2p_data *p2p = wpa_s->global->p2p;
6069
6070 if (p2p == NULL)
6071 return;
6072
6073 if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_CAPABLE))
6074 return;
6075
6076 if (wpa_s->conf->changed_parameters & CFG_CHANGED_DEVICE_NAME)
6077 p2p_set_dev_name(p2p, wpa_s->conf->device_name);
6078
6079 if (wpa_s->conf->changed_parameters & CFG_CHANGED_DEVICE_TYPE)
6080 p2p_set_pri_dev_type(p2p, wpa_s->conf->device_type);
6081
6082 if (wpa_s->wps &&
6083 (wpa_s->conf->changed_parameters & CFG_CHANGED_CONFIG_METHODS))
6084 p2p_set_config_methods(p2p, wpa_s->wps->config_methods);
6085
6086 if (wpa_s->wps && (wpa_s->conf->changed_parameters & CFG_CHANGED_UUID))
6087 p2p_set_uuid(p2p, wpa_s->wps->uuid);
6088
6089 if (wpa_s->conf->changed_parameters & CFG_CHANGED_WPS_STRING) {
6090 p2p_set_manufacturer(p2p, wpa_s->conf->manufacturer);
6091 p2p_set_model_name(p2p, wpa_s->conf->model_name);
6092 p2p_set_model_number(p2p, wpa_s->conf->model_number);
6093 p2p_set_serial_number(p2p, wpa_s->conf->serial_number);
6094 }
6095
6096 if (wpa_s->conf->changed_parameters & CFG_CHANGED_SEC_DEVICE_TYPE)
6097 p2p_set_sec_dev_types(p2p,
6098 (void *) wpa_s->conf->sec_device_type,
6099 wpa_s->conf->num_sec_device_types);
6100
6101 if (wpa_s->conf->changed_parameters & CFG_CHANGED_VENDOR_EXTENSION) {
6102 int i;
6103 p2p_remove_wps_vendor_extensions(p2p);
6104 for (i = 0; i < MAX_WPS_VENDOR_EXT; i++) {
6105 if (wpa_s->conf->wps_vendor_ext[i] == NULL)
6106 continue;
6107 p2p_add_wps_vendor_extension(
6108 p2p, wpa_s->conf->wps_vendor_ext[i]);
6109 }
6110 }
6111
6112 if ((wpa_s->conf->changed_parameters & CFG_CHANGED_COUNTRY) &&
6113 wpa_s->conf->country[0] && wpa_s->conf->country[1]) {
6114 char country[3];
6115 country[0] = wpa_s->conf->country[0];
6116 country[1] = wpa_s->conf->country[1];
6117 country[2] = 0x04;
6118 p2p_set_country(p2p, country);
6119 }
6120
6121 if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_SSID_POSTFIX) {
6122 p2p_set_ssid_postfix(p2p, (u8 *) wpa_s->conf->p2p_ssid_postfix,
6123 wpa_s->conf->p2p_ssid_postfix ?
6124 os_strlen(wpa_s->conf->p2p_ssid_postfix) :
6125 0);
6126 }
6127
6128 if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_INTRA_BSS)
6129 p2p_set_intra_bss_dist(p2p, wpa_s->conf->p2p_intra_bss);
Jouni Malinen75ecf522011-06-27 15:19:46 -07006130
6131 if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_LISTEN_CHANNEL) {
6132 u8 reg_class, channel;
6133 int ret;
6134 unsigned int r;
6135 if (wpa_s->conf->p2p_listen_reg_class &&
6136 wpa_s->conf->p2p_listen_channel) {
6137 reg_class = wpa_s->conf->p2p_listen_reg_class;
6138 channel = wpa_s->conf->p2p_listen_channel;
6139 } else {
6140 reg_class = 81;
6141 /*
6142 * Pick one of the social channels randomly as the
6143 * listen channel.
6144 */
6145 os_get_random((u8 *) &r, sizeof(r));
6146 channel = 1 + (r % 3) * 5;
6147 }
6148 ret = p2p_set_listen_channel(p2p, reg_class, channel);
6149 if (ret)
6150 wpa_printf(MSG_ERROR, "P2P: Own listen channel update "
6151 "failed: %d", ret);
6152 }
6153 if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_OPER_CHANNEL) {
6154 u8 op_reg_class, op_channel, cfg_op_channel;
6155 int ret = 0;
6156 unsigned int r;
6157 if (wpa_s->conf->p2p_oper_reg_class &&
6158 wpa_s->conf->p2p_oper_channel) {
6159 op_reg_class = wpa_s->conf->p2p_oper_reg_class;
6160 op_channel = wpa_s->conf->p2p_oper_channel;
6161 cfg_op_channel = 1;
6162 } else {
6163 op_reg_class = 81;
6164 /*
6165 * Use random operation channel from (1, 6, 11)
6166 *if no other preference is indicated.
6167 */
6168 os_get_random((u8 *) &r, sizeof(r));
6169 op_channel = 1 + (r % 3) * 5;
6170 cfg_op_channel = 0;
6171 }
6172 ret = p2p_set_oper_channel(p2p, op_reg_class, op_channel,
6173 cfg_op_channel);
6174 if (ret)
6175 wpa_printf(MSG_ERROR, "P2P: Own oper channel update "
6176 "failed: %d", ret);
6177 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07006178
6179 if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_PREF_CHAN) {
6180 if (p2p_set_pref_chan(p2p, wpa_s->conf->num_p2p_pref_chan,
6181 wpa_s->conf->p2p_pref_chan) < 0) {
6182 wpa_printf(MSG_ERROR, "P2P: Preferred channel list "
6183 "update failed");
6184 }
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07006185
6186 if (p2p_set_no_go_freq(p2p, &wpa_s->conf->p2p_no_go_freq) < 0) {
6187 wpa_printf(MSG_ERROR, "P2P: No GO channel list "
6188 "update failed");
6189 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07006190 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006191}
6192
6193
6194int wpas_p2p_set_noa(struct wpa_supplicant *wpa_s, u8 count, int start,
6195 int duration)
6196{
6197 if (!wpa_s->ap_iface)
6198 return -1;
6199 return hostapd_p2p_set_noa(wpa_s->ap_iface->bss[0], count, start,
6200 duration);
6201}
6202
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006203
6204int wpas_p2p_set_cross_connect(struct wpa_supplicant *wpa_s, int enabled)
6205{
6206 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
6207 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006208
6209 wpa_s->global->cross_connection = enabled;
6210 p2p_set_cross_connect(wpa_s->global->p2p, enabled);
6211
6212 if (!enabled) {
6213 struct wpa_supplicant *iface;
6214
6215 for (iface = wpa_s->global->ifaces; iface; iface = iface->next)
6216 {
6217 if (iface->cross_connect_enabled == 0)
6218 continue;
6219
6220 iface->cross_connect_enabled = 0;
6221 iface->cross_connect_in_use = 0;
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07006222 wpa_msg_global(iface->parent, MSG_INFO,
6223 P2P_EVENT_CROSS_CONNECT_DISABLE "%s %s",
6224 iface->ifname,
6225 iface->cross_connect_uplink);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006226 }
6227 }
6228
6229 return 0;
6230}
6231
6232
6233static void wpas_p2p_enable_cross_connect(struct wpa_supplicant *uplink)
6234{
6235 struct wpa_supplicant *iface;
6236
6237 if (!uplink->global->cross_connection)
6238 return;
6239
6240 for (iface = uplink->global->ifaces; iface; iface = iface->next) {
6241 if (!iface->cross_connect_enabled)
6242 continue;
6243 if (os_strcmp(uplink->ifname, iface->cross_connect_uplink) !=
6244 0)
6245 continue;
6246 if (iface->ap_iface == NULL)
6247 continue;
6248 if (iface->cross_connect_in_use)
6249 continue;
6250
6251 iface->cross_connect_in_use = 1;
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07006252 wpa_msg_global(iface->parent, MSG_INFO,
6253 P2P_EVENT_CROSS_CONNECT_ENABLE "%s %s",
6254 iface->ifname, iface->cross_connect_uplink);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006255 }
6256}
6257
6258
6259static void wpas_p2p_disable_cross_connect(struct wpa_supplicant *uplink)
6260{
6261 struct wpa_supplicant *iface;
6262
6263 for (iface = uplink->global->ifaces; iface; iface = iface->next) {
6264 if (!iface->cross_connect_enabled)
6265 continue;
6266 if (os_strcmp(uplink->ifname, iface->cross_connect_uplink) !=
6267 0)
6268 continue;
6269 if (!iface->cross_connect_in_use)
6270 continue;
6271
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07006272 wpa_msg_global(iface->parent, MSG_INFO,
6273 P2P_EVENT_CROSS_CONNECT_DISABLE "%s %s",
6274 iface->ifname, iface->cross_connect_uplink);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006275 iface->cross_connect_in_use = 0;
6276 }
6277}
6278
6279
6280void wpas_p2p_notif_connected(struct wpa_supplicant *wpa_s)
6281{
6282 if (wpa_s->ap_iface || wpa_s->current_ssid == NULL ||
6283 wpa_s->current_ssid->mode != WPAS_MODE_INFRA ||
6284 wpa_s->cross_connect_disallowed)
6285 wpas_p2p_disable_cross_connect(wpa_s);
6286 else
6287 wpas_p2p_enable_cross_connect(wpa_s);
Dmitry Shmidt04949592012-07-19 12:16:46 -07006288 if (!wpa_s->ap_iface &&
6289 eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL) > 0)
6290 wpa_printf(MSG_DEBUG, "P2P: Cancelled P2P group idle timeout");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006291}
6292
6293
6294void wpas_p2p_notif_disconnected(struct wpa_supplicant *wpa_s)
6295{
6296 wpas_p2p_disable_cross_connect(wpa_s);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006297 if (!wpa_s->ap_iface &&
6298 !eloop_is_timeout_registered(wpas_p2p_group_idle_timeout,
6299 wpa_s, NULL))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006300 wpas_p2p_set_group_idle_timeout(wpa_s);
6301}
6302
6303
6304static void wpas_p2p_cross_connect_setup(struct wpa_supplicant *wpa_s)
6305{
6306 struct wpa_supplicant *iface;
6307
6308 if (!wpa_s->global->cross_connection)
6309 return;
6310
6311 for (iface = wpa_s->global->ifaces; iface; iface = iface->next) {
6312 if (iface == wpa_s)
6313 continue;
6314 if (iface->drv_flags &
6315 WPA_DRIVER_FLAGS_P2P_DEDICATED_INTERFACE)
6316 continue;
6317 if (iface->drv_flags & WPA_DRIVER_FLAGS_P2P_CAPABLE)
6318 continue;
6319
6320 wpa_s->cross_connect_enabled = 1;
6321 os_strlcpy(wpa_s->cross_connect_uplink, iface->ifname,
6322 sizeof(wpa_s->cross_connect_uplink));
6323 wpa_printf(MSG_DEBUG, "P2P: Enable cross connection from "
6324 "%s to %s whenever uplink is available",
6325 wpa_s->ifname, wpa_s->cross_connect_uplink);
6326
6327 if (iface->ap_iface || iface->current_ssid == NULL ||
6328 iface->current_ssid->mode != WPAS_MODE_INFRA ||
6329 iface->cross_connect_disallowed ||
6330 iface->wpa_state != WPA_COMPLETED)
6331 break;
6332
6333 wpa_s->cross_connect_in_use = 1;
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07006334 wpa_msg_global(wpa_s->parent, MSG_INFO,
6335 P2P_EVENT_CROSS_CONNECT_ENABLE "%s %s",
6336 wpa_s->ifname, wpa_s->cross_connect_uplink);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006337 break;
6338 }
6339}
6340
6341
6342int wpas_p2p_notif_pbc_overlap(struct wpa_supplicant *wpa_s)
6343{
6344 if (wpa_s->p2p_group_interface != P2P_GROUP_INTERFACE_CLIENT &&
6345 !wpa_s->p2p_in_provisioning)
6346 return 0; /* not P2P client operation */
6347
6348 wpa_printf(MSG_DEBUG, "P2P: Terminate connection due to WPS PBC "
6349 "session overlap");
6350 if (wpa_s != wpa_s->parent)
6351 wpa_msg_ctrl(wpa_s->parent, MSG_INFO, WPS_EVENT_OVERLAP);
Dmitry Shmidt34af3062013-07-11 10:46:32 -07006352 wpas_p2p_group_formation_failed(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006353 return 1;
6354}
6355
6356
6357void wpas_p2p_update_channel_list(struct wpa_supplicant *wpa_s)
6358{
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07006359 struct p2p_channels chan, cli_chan;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006360
6361 if (wpa_s->global == NULL || wpa_s->global->p2p == NULL)
6362 return;
6363
6364 os_memset(&chan, 0, sizeof(chan));
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07006365 os_memset(&cli_chan, 0, sizeof(cli_chan));
6366 if (wpas_p2p_setup_channels(wpa_s, &chan, &cli_chan)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006367 wpa_printf(MSG_ERROR, "P2P: Failed to update supported "
6368 "channel list");
6369 return;
6370 }
6371
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07006372 p2p_update_channel_list(wpa_s->global->p2p, &chan, &cli_chan);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006373}
6374
6375
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08006376static void wpas_p2p_scan_res_ignore(struct wpa_supplicant *wpa_s,
6377 struct wpa_scan_results *scan_res)
6378{
6379 wpa_printf(MSG_DEBUG, "P2P: Ignore scan results");
6380}
6381
6382
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006383int wpas_p2p_cancel(struct wpa_supplicant *wpa_s)
6384{
6385 struct wpa_global *global = wpa_s->global;
6386 int found = 0;
6387 const u8 *peer;
6388
6389 if (global->p2p == NULL)
6390 return -1;
6391
6392 wpa_printf(MSG_DEBUG, "P2P: Request to cancel group formation");
6393
6394 if (wpa_s->pending_interface_name[0] &&
6395 !is_zero_ether_addr(wpa_s->pending_interface_addr))
6396 found = 1;
6397
6398 peer = p2p_get_go_neg_peer(global->p2p);
6399 if (peer) {
6400 wpa_printf(MSG_DEBUG, "P2P: Unauthorize pending GO Neg peer "
6401 MACSTR, MAC2STR(peer));
6402 p2p_unauthorize(global->p2p, peer);
Dmitry Shmidt04949592012-07-19 12:16:46 -07006403 found = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006404 }
6405
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08006406 if (wpa_s->scan_res_handler == wpas_p2p_scan_res_join) {
6407 wpa_printf(MSG_DEBUG, "P2P: Stop pending scan for join");
6408 wpa_s->scan_res_handler = wpas_p2p_scan_res_ignore;
6409 found = 1;
6410 }
6411
6412 if (wpa_s->pending_pd_before_join) {
6413 wpa_printf(MSG_DEBUG, "P2P: Stop pending PD before join");
6414 wpa_s->pending_pd_before_join = 0;
6415 found = 1;
6416 }
6417
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006418 wpas_p2p_stop_find(wpa_s);
6419
6420 for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
6421 if (wpa_s == global->p2p_group_formation &&
6422 (wpa_s->p2p_in_provisioning ||
6423 wpa_s->parent->pending_interface_type ==
6424 WPA_IF_P2P_CLIENT)) {
6425 wpa_printf(MSG_DEBUG, "P2P: Interface %s in group "
6426 "formation found - cancelling",
6427 wpa_s->ifname);
6428 found = 1;
6429 eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
6430 wpa_s->parent, NULL);
Jouni Malinenadddfc42012-10-03 14:31:41 -07006431 if (wpa_s->p2p_in_provisioning) {
6432 wpas_group_formation_completed(wpa_s, 0);
6433 break;
6434 }
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07006435 wpas_p2p_group_delete(wpa_s,
6436 P2P_GROUP_REMOVAL_REQUESTED);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006437 break;
6438 }
6439 }
6440
6441 if (!found) {
6442 wpa_printf(MSG_DEBUG, "P2P: No ongoing group formation found");
6443 return -1;
6444 }
6445
6446 return 0;
6447}
6448
6449
6450void wpas_p2p_interface_unavailable(struct wpa_supplicant *wpa_s)
6451{
6452 if (wpa_s->current_ssid == NULL || !wpa_s->current_ssid->p2p_group)
6453 return;
6454
6455 wpa_printf(MSG_DEBUG, "P2P: Remove group due to driver resource not "
6456 "being available anymore");
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07006457 wpas_p2p_group_delete(wpa_s, P2P_GROUP_REMOVAL_UNAVAILABLE);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006458}
6459
6460
6461void wpas_p2p_update_best_channels(struct wpa_supplicant *wpa_s,
6462 int freq_24, int freq_5, int freq_overall)
6463{
6464 struct p2p_data *p2p = wpa_s->global->p2p;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08006465 if (p2p == NULL)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006466 return;
6467 p2p_set_best_channels(p2p, freq_24, freq_5, freq_overall);
6468}
6469
6470
6471int wpas_p2p_unauthorize(struct wpa_supplicant *wpa_s, const char *addr)
6472{
6473 u8 peer[ETH_ALEN];
6474 struct p2p_data *p2p = wpa_s->global->p2p;
6475
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08006476 if (p2p == NULL)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006477 return -1;
6478
6479 if (hwaddr_aton(addr, peer))
6480 return -1;
6481
6482 return p2p_unauthorize(p2p, peer);
6483}
6484
6485
6486/**
6487 * wpas_p2p_disconnect - Disconnect from a P2P Group
6488 * @wpa_s: Pointer to wpa_supplicant data
6489 * Returns: 0 on success, -1 on failure
6490 *
6491 * This can be used to disconnect from a group in which the local end is a P2P
6492 * Client or to end a P2P Group in case the local end is the Group Owner. If a
6493 * virtual network interface was created for this group, that interface will be
6494 * removed. Otherwise, only the configured P2P group network will be removed
6495 * from the interface.
6496 */
6497int wpas_p2p_disconnect(struct wpa_supplicant *wpa_s)
6498{
6499
6500 if (wpa_s == NULL)
6501 return -1;
6502
Jouni Malinen2b89da82012-08-31 22:04:41 +03006503 return wpas_p2p_group_delete(wpa_s, P2P_GROUP_REMOVAL_REQUESTED) < 0 ?
6504 -1 : 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006505}
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006506
6507
6508int wpas_p2p_in_progress(struct wpa_supplicant *wpa_s)
6509{
Dmitry Shmidtf8623282013-02-20 14:34:59 -08006510 int ret;
6511
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006512 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
6513 return 0;
6514
Dmitry Shmidtf8623282013-02-20 14:34:59 -08006515 ret = p2p_in_progress(wpa_s->global->p2p);
6516 if (ret == 0) {
6517 /*
6518 * Check whether there is an ongoing WPS provisioning step (or
6519 * other parts of group formation) on another interface since
6520 * p2p_in_progress() does not report this to avoid issues for
6521 * scans during such provisioning step.
6522 */
6523 if (wpa_s->global->p2p_group_formation &&
6524 wpa_s->global->p2p_group_formation != wpa_s) {
6525 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Another interface (%s) "
6526 "in group formation",
6527 wpa_s->global->p2p_group_formation->ifname);
6528 ret = 1;
6529 }
6530 }
6531
Dmitry Shmidt92c368d2013-08-29 12:37:21 -07006532 if (!ret && wpa_s->global->p2p_go_wait_client.sec) {
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08006533 struct os_reltime now;
6534 os_get_reltime(&now);
6535 if (os_reltime_expired(&now, &wpa_s->global->p2p_go_wait_client,
6536 P2P_MAX_INITIAL_CONN_WAIT_GO)) {
Dmitry Shmidt92c368d2013-08-29 12:37:21 -07006537 /* Wait for the first client has expired */
6538 wpa_s->global->p2p_go_wait_client.sec = 0;
6539 } else {
6540 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Waiting for initial client connection during group formation");
6541 ret = 1;
6542 }
6543 }
6544
Dmitry Shmidtf8623282013-02-20 14:34:59 -08006545 return ret;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006546}
6547
6548
6549void wpas_p2p_network_removed(struct wpa_supplicant *wpa_s,
6550 struct wpa_ssid *ssid)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006551{
6552 if (wpa_s->p2p_in_provisioning && ssid->p2p_group &&
6553 eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
6554 wpa_s->parent, NULL) > 0) {
Dmitry Shmidt04949592012-07-19 12:16:46 -07006555 /**
6556 * Remove the network by scheduling the group formation
6557 * timeout to happen immediately. The teardown code
6558 * needs to be scheduled to run asynch later so that we
6559 * don't delete data from under ourselves unexpectedly.
6560 * Calling wpas_p2p_group_formation_timeout directly
6561 * causes a series of crashes in WPS failure scenarios.
6562 */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006563 wpa_printf(MSG_DEBUG, "P2P: Canceled group formation due to "
6564 "P2P group network getting removed");
Dmitry Shmidt04949592012-07-19 12:16:46 -07006565 eloop_register_timeout(0, 0, wpas_p2p_group_formation_timeout,
6566 wpa_s->parent, NULL);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006567 }
6568}
6569
6570
6571struct wpa_ssid * wpas_p2p_get_persistent(struct wpa_supplicant *wpa_s,
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08006572 const u8 *addr, const u8 *ssid,
6573 size_t ssid_len)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006574{
6575 struct wpa_ssid *s;
6576 size_t i;
6577
6578 for (s = wpa_s->conf->ssid; s; s = s->next) {
6579 if (s->disabled != 2)
6580 continue;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08006581 if (ssid &&
6582 (ssid_len != s->ssid_len ||
6583 os_memcmp(ssid, s->ssid, ssid_len) != 0))
6584 continue;
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07006585 if (addr == NULL) {
6586 if (s->mode == WPAS_MODE_P2P_GO)
6587 return s;
6588 continue;
6589 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006590 if (os_memcmp(s->bssid, addr, ETH_ALEN) == 0)
6591 return s; /* peer is GO in the persistent group */
6592 if (s->mode != WPAS_MODE_P2P_GO || s->p2p_client_list == NULL)
6593 continue;
6594 for (i = 0; i < s->num_p2p_clients; i++) {
6595 if (os_memcmp(s->p2p_client_list + i * ETH_ALEN,
6596 addr, ETH_ALEN) == 0)
6597 return s; /* peer is P2P client in persistent
6598 * group */
6599 }
6600 }
6601
6602 return NULL;
6603}
6604
6605
6606void wpas_p2p_notify_ap_sta_authorized(struct wpa_supplicant *wpa_s,
6607 const u8 *addr)
6608{
Dmitry Shmidt56052862013-10-04 10:23:25 -07006609 if (eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
6610 wpa_s->parent, NULL) > 0) {
6611 /*
6612 * This can happen if WPS provisioning step is not terminated
6613 * cleanly (e.g., P2P Client does not send WSC_Done). Since the
6614 * peer was able to connect, there is no need to time out group
6615 * formation after this, though. In addition, this is used with
6616 * the initial connection wait on the GO as a separate formation
6617 * timeout and as such, expected to be hit after the initial WPS
6618 * provisioning step.
6619 */
6620 wpa_printf(MSG_DEBUG, "P2P: Canceled P2P group formation timeout on data connection");
6621 }
6622 if (!wpa_s->p2p_go_group_formation_completed) {
6623 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Marking group formation completed on GO on first data connection");
6624 wpa_s->p2p_go_group_formation_completed = 1;
6625 wpa_s->global->p2p_group_formation = NULL;
6626 wpa_s->p2p_in_provisioning = 0;
6627 }
Dmitry Shmidt92c368d2013-08-29 12:37:21 -07006628 wpa_s->global->p2p_go_wait_client.sec = 0;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006629 if (addr == NULL)
6630 return;
6631 wpas_p2p_add_persistent_group_client(wpa_s, addr);
6632}
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08006633
Dmitry Shmidt04949592012-07-19 12:16:46 -07006634
6635static void wpas_p2p_fallback_to_go_neg(struct wpa_supplicant *wpa_s,
6636 int group_added)
6637{
6638 struct wpa_supplicant *group = wpa_s;
Dmitry Shmidt04949592012-07-19 12:16:46 -07006639 if (wpa_s->global->p2p_group_formation)
6640 group = wpa_s->global->p2p_group_formation;
6641 wpa_s = wpa_s->parent;
6642 offchannel_send_action_done(wpa_s);
6643 if (group_added)
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07006644 wpas_p2p_group_delete(group, P2P_GROUP_REMOVAL_SILENT);
Dmitry Shmidt04949592012-07-19 12:16:46 -07006645 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Fall back to GO Negotiation");
6646 wpas_p2p_connect(wpa_s, wpa_s->pending_join_dev_addr, wpa_s->p2p_pin,
6647 wpa_s->p2p_wps_method, wpa_s->p2p_persistent_group, 0,
6648 0, 0, wpa_s->p2p_go_intent, wpa_s->p2p_connect_freq,
6649 wpa_s->p2p_persistent_id,
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07006650 wpa_s->p2p_pd_before_go_neg,
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07006651 wpa_s->p2p_go_ht40,
6652 wpa_s->p2p_go_vht);
Dmitry Shmidt04949592012-07-19 12:16:46 -07006653}
6654
6655
6656int wpas_p2p_scan_no_go_seen(struct wpa_supplicant *wpa_s)
6657{
6658 if (!wpa_s->p2p_fallback_to_go_neg ||
6659 wpa_s->p2p_in_provisioning <= 5)
6660 return 0;
6661
6662 if (wpas_p2p_peer_go(wpa_s, wpa_s->pending_join_dev_addr) > 0)
6663 return 0; /* peer operating as a GO */
6664
6665 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: GO not found for p2p_connect-auto - "
6666 "fallback to GO Negotiation");
6667 wpas_p2p_fallback_to_go_neg(wpa_s, 1);
6668
6669 return 1;
6670}
6671
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07006672
6673unsigned int wpas_p2p_search_delay(struct wpa_supplicant *wpa_s)
6674{
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07006675 struct wpa_supplicant *ifs;
6676
6677 if (wpa_s->wpa_state > WPA_SCANNING) {
6678 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Use %u ms search delay due to "
6679 "concurrent operation",
6680 P2P_CONCURRENT_SEARCH_DELAY);
6681 return P2P_CONCURRENT_SEARCH_DELAY;
6682 }
6683
Dmitry Shmidt01904cf2013-12-05 11:08:35 -08006684 dl_list_for_each(ifs, &wpa_s->radio->ifaces, struct wpa_supplicant,
6685 radio_list) {
6686 if (ifs != wpa_s && ifs->wpa_state > WPA_SCANNING) {
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07006687 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Use %u ms search "
6688 "delay due to concurrent operation on "
6689 "interface %s",
6690 P2P_CONCURRENT_SEARCH_DELAY, ifs->ifname);
6691 return P2P_CONCURRENT_SEARCH_DELAY;
6692 }
6693 }
6694
6695 return 0;
6696}
6697
Dmitry Shmidt37d4d6a2013-03-18 13:09:42 -07006698
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07006699static int wpas_p2p_remove_psk_entry(struct wpa_supplicant *wpa_s,
6700 struct wpa_ssid *s, const u8 *addr,
6701 int iface_addr)
6702{
6703 struct psk_list_entry *psk, *tmp;
6704 int changed = 0;
6705
6706 dl_list_for_each_safe(psk, tmp, &s->psk_list, struct psk_list_entry,
6707 list) {
6708 if ((iface_addr && !psk->p2p &&
6709 os_memcmp(addr, psk->addr, ETH_ALEN) == 0) ||
6710 (!iface_addr && psk->p2p &&
6711 os_memcmp(addr, psk->addr, ETH_ALEN) == 0)) {
6712 wpa_dbg(wpa_s, MSG_DEBUG,
6713 "P2P: Remove persistent group PSK list entry for "
6714 MACSTR " p2p=%u",
6715 MAC2STR(psk->addr), psk->p2p);
6716 dl_list_del(&psk->list);
6717 os_free(psk);
6718 changed++;
6719 }
6720 }
6721
6722 return changed;
6723}
6724
6725
6726void wpas_p2p_new_psk_cb(struct wpa_supplicant *wpa_s, const u8 *mac_addr,
6727 const u8 *p2p_dev_addr,
6728 const u8 *psk, size_t psk_len)
6729{
6730 struct wpa_ssid *ssid = wpa_s->current_ssid;
6731 struct wpa_ssid *persistent;
6732 struct psk_list_entry *p;
6733
6734 if (psk_len != sizeof(p->psk))
6735 return;
6736
6737 if (p2p_dev_addr) {
6738 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: New PSK for addr=" MACSTR
6739 " p2p_dev_addr=" MACSTR,
6740 MAC2STR(mac_addr), MAC2STR(p2p_dev_addr));
6741 if (is_zero_ether_addr(p2p_dev_addr))
6742 p2p_dev_addr = NULL;
6743 } else {
6744 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: New PSK for addr=" MACSTR,
6745 MAC2STR(mac_addr));
6746 }
6747
6748 if (ssid->mode == WPAS_MODE_P2P_GROUP_FORMATION) {
6749 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: new_psk_cb during group formation");
6750 /* To be added to persistent group once created */
6751 if (wpa_s->global->add_psk == NULL) {
6752 wpa_s->global->add_psk = os_zalloc(sizeof(*p));
6753 if (wpa_s->global->add_psk == NULL)
6754 return;
6755 }
6756 p = wpa_s->global->add_psk;
6757 if (p2p_dev_addr) {
6758 p->p2p = 1;
6759 os_memcpy(p->addr, p2p_dev_addr, ETH_ALEN);
6760 } else {
6761 p->p2p = 0;
6762 os_memcpy(p->addr, mac_addr, ETH_ALEN);
6763 }
6764 os_memcpy(p->psk, psk, psk_len);
6765 return;
6766 }
6767
6768 if (ssid->mode != WPAS_MODE_P2P_GO || !ssid->p2p_persistent_group) {
6769 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Ignore new_psk_cb on not-persistent GO");
6770 return;
6771 }
6772
6773 persistent = wpas_p2p_get_persistent(wpa_s->parent, NULL, ssid->ssid,
6774 ssid->ssid_len);
6775 if (!persistent) {
6776 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Could not find persistent group information to store the new PSK");
6777 return;
6778 }
6779
6780 p = os_zalloc(sizeof(*p));
6781 if (p == NULL)
6782 return;
6783 if (p2p_dev_addr) {
6784 p->p2p = 1;
6785 os_memcpy(p->addr, p2p_dev_addr, ETH_ALEN);
6786 } else {
6787 p->p2p = 0;
6788 os_memcpy(p->addr, mac_addr, ETH_ALEN);
6789 }
6790 os_memcpy(p->psk, psk, psk_len);
6791
6792 if (dl_list_len(&persistent->psk_list) > P2P_MAX_STORED_CLIENTS) {
6793 struct psk_list_entry *last;
6794 last = dl_list_last(&persistent->psk_list,
6795 struct psk_list_entry, list);
6796 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Remove oldest PSK entry for "
6797 MACSTR " (p2p=%u) to make room for a new one",
6798 MAC2STR(last->addr), last->p2p);
6799 dl_list_del(&last->list);
6800 os_free(last);
6801 }
6802
6803 wpas_p2p_remove_psk_entry(wpa_s->parent, persistent,
6804 p2p_dev_addr ? p2p_dev_addr : mac_addr,
6805 p2p_dev_addr == NULL);
6806 if (p2p_dev_addr) {
6807 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Add new PSK for p2p_dev_addr="
6808 MACSTR, MAC2STR(p2p_dev_addr));
6809 } else {
6810 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Add new PSK for addr=" MACSTR,
6811 MAC2STR(mac_addr));
6812 }
6813 dl_list_add(&persistent->psk_list, &p->list);
6814
6815#ifndef CONFIG_NO_CONFIG_WRITE
6816 if (wpa_s->parent->conf->update_config &&
6817 wpa_config_write(wpa_s->parent->confname, wpa_s->parent->conf))
6818 wpa_printf(MSG_DEBUG, "P2P: Failed to update configuration");
6819#endif /* CONFIG_NO_CONFIG_WRITE */
6820}
6821
6822
6823static void wpas_p2p_remove_psk(struct wpa_supplicant *wpa_s,
6824 struct wpa_ssid *s, const u8 *addr,
6825 int iface_addr)
6826{
6827 int res;
6828
6829 res = wpas_p2p_remove_psk_entry(wpa_s, s, addr, iface_addr);
6830 if (res > 0) {
6831#ifndef CONFIG_NO_CONFIG_WRITE
6832 if (wpa_s->conf->update_config &&
6833 wpa_config_write(wpa_s->confname, wpa_s->conf))
6834 wpa_dbg(wpa_s, MSG_DEBUG,
6835 "P2P: Failed to update configuration");
6836#endif /* CONFIG_NO_CONFIG_WRITE */
6837 }
6838}
6839
6840
6841static void wpas_p2p_remove_client_go(struct wpa_supplicant *wpa_s,
6842 const u8 *peer, int iface_addr)
6843{
6844 struct hostapd_data *hapd;
6845 struct hostapd_wpa_psk *psk, *prev, *rem;
6846 struct sta_info *sta;
6847
6848 if (wpa_s->ap_iface == NULL || wpa_s->current_ssid == NULL ||
6849 wpa_s->current_ssid->mode != WPAS_MODE_P2P_GO)
6850 return;
6851
6852 /* Remove per-station PSK entry */
6853 hapd = wpa_s->ap_iface->bss[0];
6854 prev = NULL;
6855 psk = hapd->conf->ssid.wpa_psk;
6856 while (psk) {
6857 if ((iface_addr && os_memcmp(peer, psk->addr, ETH_ALEN) == 0) ||
6858 (!iface_addr &&
6859 os_memcmp(peer, psk->p2p_dev_addr, ETH_ALEN) == 0)) {
6860 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Remove operating group PSK entry for "
6861 MACSTR " iface_addr=%d",
6862 MAC2STR(peer), iface_addr);
6863 if (prev)
6864 prev->next = psk->next;
6865 else
6866 hapd->conf->ssid.wpa_psk = psk->next;
6867 rem = psk;
6868 psk = psk->next;
6869 os_free(rem);
6870 } else {
6871 prev = psk;
6872 psk = psk->next;
6873 }
6874 }
6875
6876 /* Disconnect from group */
6877 if (iface_addr)
6878 sta = ap_get_sta(hapd, peer);
6879 else
6880 sta = ap_get_sta_p2p(hapd, peer);
6881 if (sta) {
6882 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Disconnect peer " MACSTR
6883 " (iface_addr=%d) from group",
6884 MAC2STR(peer), iface_addr);
6885 hostapd_drv_sta_deauth(hapd, sta->addr,
6886 WLAN_REASON_DEAUTH_LEAVING);
6887 ap_sta_deauthenticate(hapd, sta, WLAN_REASON_DEAUTH_LEAVING);
6888 }
6889}
6890
6891
6892void wpas_p2p_remove_client(struct wpa_supplicant *wpa_s, const u8 *peer,
6893 int iface_addr)
6894{
6895 struct wpa_ssid *s;
6896 struct wpa_supplicant *w;
6897
6898 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Remove client " MACSTR, MAC2STR(peer));
6899
6900 /* Remove from any persistent group */
6901 for (s = wpa_s->parent->conf->ssid; s; s = s->next) {
6902 if (s->disabled != 2 || s->mode != WPAS_MODE_P2P_GO)
6903 continue;
6904 if (!iface_addr)
6905 wpas_remove_persistent_peer(wpa_s, s, peer, 0);
6906 wpas_p2p_remove_psk(wpa_s->parent, s, peer, iface_addr);
6907 }
6908
6909 /* Remove from any operating group */
6910 for (w = wpa_s->global->ifaces; w; w = w->next)
6911 wpas_p2p_remove_client_go(w, peer, iface_addr);
6912}
6913
6914
6915static void wpas_p2p_psk_failure_removal(void *eloop_ctx, void *timeout_ctx)
6916{
6917 struct wpa_supplicant *wpa_s = eloop_ctx;
6918 wpas_p2p_group_delete(wpa_s, P2P_GROUP_REMOVAL_PSK_FAILURE);
6919}
6920
6921
Dmitry Shmidt04f534e2013-12-09 15:50:16 -08006922static void wpas_p2p_group_freq_conflict(void *eloop_ctx, void *timeout_ctx)
6923{
6924 struct wpa_supplicant *wpa_s = eloop_ctx;
6925
6926 wpa_printf(MSG_DEBUG, "P2P: Frequency conflict - terminate group");
6927 wpas_p2p_group_delete(wpa_s, P2P_GROUP_REMOVAL_FREQ_CONFLICT);
6928}
6929
6930
6931int wpas_p2p_handle_frequency_conflicts(struct wpa_supplicant *wpa_s, int freq,
6932 struct wpa_ssid *ssid)
6933{
6934 struct wpa_supplicant *iface;
6935
6936 for (iface = wpa_s->global->ifaces; iface; iface = iface->next) {
6937 if (!iface->current_ssid ||
6938 iface->current_ssid->frequency == freq ||
6939 (iface->p2p_group_interface == NOT_P2P_GROUP_INTERFACE &&
6940 !iface->current_ssid->p2p_group))
6941 continue;
6942
6943 /* Remove the connection with least priority */
6944 if (!wpas_is_p2p_prioritized(iface)) {
6945 /* STA connection has priority over existing
6946 * P2P connection, so remove the interface. */
6947 wpa_printf(MSG_DEBUG, "P2P: Removing P2P connection due to single channel concurrent mode frequency conflict");
6948 eloop_register_timeout(0, 0,
6949 wpas_p2p_group_freq_conflict,
6950 iface, NULL);
6951 /* If connection in progress is P2P connection, do not
6952 * proceed for the connection. */
6953 if (wpa_s == iface)
6954 return -1;
6955 else
6956 return 0;
6957 } else {
6958 /* P2P connection has priority, disable the STA network
6959 */
6960 wpa_supplicant_disable_network(wpa_s->global->ifaces,
6961 ssid);
6962 wpa_msg(wpa_s->global->ifaces, MSG_INFO,
6963 WPA_EVENT_FREQ_CONFLICT " id=%d", ssid->id);
6964 os_memset(wpa_s->global->ifaces->pending_bssid, 0,
6965 ETH_ALEN);
6966 /* If P2P connection is in progress, continue
6967 * connecting...*/
6968 if (wpa_s == iface)
6969 return 0;
6970 else
6971 return -1;
6972 }
6973 }
6974
6975 return 0;
6976}
6977
6978
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07006979int wpas_p2p_4way_hs_failed(struct wpa_supplicant *wpa_s)
6980{
6981 struct wpa_ssid *ssid = wpa_s->current_ssid;
6982
6983 if (ssid == NULL || !ssid->p2p_group)
6984 return 0;
6985
6986 if (wpa_s->p2p_last_4way_hs_fail &&
6987 wpa_s->p2p_last_4way_hs_fail == ssid) {
6988 u8 go_dev_addr[ETH_ALEN];
6989 struct wpa_ssid *persistent;
6990
6991 if (wpas_p2p_persistent_group(wpa_s, go_dev_addr,
6992 ssid->ssid,
6993 ssid->ssid_len) <= 0) {
6994 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Could not determine whether 4-way handshake failures were for a persistent group");
6995 goto disconnect;
6996 }
6997
6998 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Two 4-way handshake failures for a P2P group - go_dev_addr="
6999 MACSTR, MAC2STR(go_dev_addr));
7000 persistent = wpas_p2p_get_persistent(wpa_s->parent, go_dev_addr,
7001 ssid->ssid,
7002 ssid->ssid_len);
7003 if (persistent == NULL || persistent->mode != WPAS_MODE_INFRA) {
7004 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: No matching persistent group stored");
7005 goto disconnect;
7006 }
7007 wpa_msg_global(wpa_s->parent, MSG_INFO,
7008 P2P_EVENT_PERSISTENT_PSK_FAIL "%d",
7009 persistent->id);
7010 disconnect:
7011 wpa_s->p2p_last_4way_hs_fail = NULL;
7012 /*
7013 * Remove the group from a timeout to avoid issues with caller
7014 * continuing to use the interface if this is on a P2P group
7015 * interface.
7016 */
7017 eloop_register_timeout(0, 0, wpas_p2p_psk_failure_removal,
7018 wpa_s, NULL);
7019 return 1;
7020 }
7021
7022 wpa_s->p2p_last_4way_hs_fail = ssid;
7023 return 0;
7024}
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08007025
7026
7027#ifdef CONFIG_WPS_NFC
7028
7029static struct wpabuf * wpas_p2p_nfc_handover(int ndef, struct wpabuf *wsc,
7030 struct wpabuf *p2p)
7031{
7032 struct wpabuf *ret;
7033 size_t wsc_len;
7034
7035 if (p2p == NULL) {
7036 wpabuf_free(wsc);
7037 wpa_printf(MSG_DEBUG, "P2P: No p2p buffer for handover");
7038 return NULL;
7039 }
7040
7041 wsc_len = wsc ? wpabuf_len(wsc) : 0;
7042 ret = wpabuf_alloc(2 + wsc_len + 2 + wpabuf_len(p2p));
7043 if (ret == NULL) {
7044 wpabuf_free(wsc);
7045 wpabuf_free(p2p);
7046 return NULL;
7047 }
7048
7049 wpabuf_put_be16(ret, wsc_len);
7050 if (wsc)
7051 wpabuf_put_buf(ret, wsc);
7052 wpabuf_put_be16(ret, wpabuf_len(p2p));
7053 wpabuf_put_buf(ret, p2p);
7054
7055 wpabuf_free(wsc);
7056 wpabuf_free(p2p);
7057 wpa_hexdump_buf(MSG_DEBUG,
7058 "P2P: Generated NFC connection handover message", ret);
7059
7060 if (ndef && ret) {
7061 struct wpabuf *tmp;
7062 tmp = ndef_build_p2p(ret);
7063 wpabuf_free(ret);
7064 if (tmp == NULL) {
7065 wpa_printf(MSG_DEBUG, "P2P: Failed to NDEF encapsulate handover request");
7066 return NULL;
7067 }
7068 ret = tmp;
7069 }
7070
7071 return ret;
7072}
7073
7074
7075static int wpas_p2p_cli_freq(struct wpa_supplicant *wpa_s,
7076 struct wpa_ssid **ssid, u8 *go_dev_addr)
7077{
7078 struct wpa_supplicant *iface;
7079
7080 if (go_dev_addr)
7081 os_memset(go_dev_addr, 0, ETH_ALEN);
7082 if (ssid)
7083 *ssid = NULL;
7084 for (iface = wpa_s->global->ifaces; iface; iface = iface->next) {
7085 if (iface->wpa_state < WPA_ASSOCIATING ||
7086 iface->current_ssid == NULL || iface->assoc_freq == 0 ||
7087 !iface->current_ssid->p2p_group ||
7088 iface->current_ssid->mode != WPAS_MODE_INFRA)
7089 continue;
7090 if (ssid)
7091 *ssid = iface->current_ssid;
7092 if (go_dev_addr)
7093 os_memcpy(go_dev_addr, iface->go_dev_addr, ETH_ALEN);
7094 return iface->assoc_freq;
7095 }
7096 return 0;
7097}
7098
7099
7100struct wpabuf * wpas_p2p_nfc_handover_req(struct wpa_supplicant *wpa_s,
7101 int ndef)
7102{
7103 struct wpabuf *wsc, *p2p;
7104 struct wpa_ssid *ssid;
7105 u8 go_dev_addr[ETH_ALEN];
7106 int cli_freq = wpas_p2p_cli_freq(wpa_s, &ssid, go_dev_addr);
7107
7108 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL) {
7109 wpa_printf(MSG_DEBUG, "P2P: P2P disabled - cannot build handover request");
7110 return NULL;
7111 }
7112
7113 if (wpa_s->conf->wps_nfc_dh_pubkey == NULL &&
7114 wps_nfc_gen_dh(&wpa_s->conf->wps_nfc_dh_pubkey,
7115 &wpa_s->conf->wps_nfc_dh_privkey) < 0) {
7116 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: No DH key available for handover request");
7117 return NULL;
7118 }
7119
7120 if (cli_freq == 0) {
7121 wsc = wps_build_nfc_handover_req_p2p(
7122 wpa_s->parent->wps, wpa_s->conf->wps_nfc_dh_pubkey);
7123 } else
7124 wsc = NULL;
7125 p2p = p2p_build_nfc_handover_req(wpa_s->global->p2p, cli_freq,
7126 go_dev_addr, ssid ? ssid->ssid : NULL,
7127 ssid ? ssid->ssid_len : 0);
7128
7129 return wpas_p2p_nfc_handover(ndef, wsc, p2p);
7130}
7131
7132
7133struct wpabuf * wpas_p2p_nfc_handover_sel(struct wpa_supplicant *wpa_s,
7134 int ndef, int tag)
7135{
7136 struct wpabuf *wsc, *p2p;
7137 struct wpa_ssid *ssid;
7138 u8 go_dev_addr[ETH_ALEN];
7139 int cli_freq = wpas_p2p_cli_freq(wpa_s, &ssid, go_dev_addr);
7140
7141 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
7142 return NULL;
7143
7144 if (!tag && wpa_s->conf->wps_nfc_dh_pubkey == NULL &&
7145 wps_nfc_gen_dh(&wpa_s->conf->wps_nfc_dh_pubkey,
7146 &wpa_s->conf->wps_nfc_dh_privkey) < 0)
7147 return NULL;
7148
7149 if (cli_freq == 0) {
7150 wsc = wps_build_nfc_handover_sel_p2p(
7151 wpa_s->parent->wps,
7152 tag ? wpa_s->conf->wps_nfc_dev_pw_id :
7153 DEV_PW_NFC_CONNECTION_HANDOVER,
7154 wpa_s->conf->wps_nfc_dh_pubkey,
7155 tag ? wpa_s->conf->wps_nfc_dev_pw : NULL);
7156 } else
7157 wsc = NULL;
7158 p2p = p2p_build_nfc_handover_sel(wpa_s->global->p2p, cli_freq,
7159 go_dev_addr, ssid ? ssid->ssid : NULL,
7160 ssid ? ssid->ssid_len : 0);
7161
7162 return wpas_p2p_nfc_handover(ndef, wsc, p2p);
7163}
7164
7165
7166static int wpas_p2p_nfc_join_group(struct wpa_supplicant *wpa_s,
7167 struct p2p_nfc_params *params)
7168{
7169 wpa_printf(MSG_DEBUG, "P2P: Initiate join-group based on NFC "
7170 "connection handover (freq=%d)",
7171 params->go_freq);
7172
7173 if (params->go_freq && params->go_ssid_len) {
7174 wpa_s->p2p_wps_method = WPS_NFC;
7175 wpa_s->pending_join_wps_method = WPS_NFC;
7176 os_memset(wpa_s->pending_join_iface_addr, 0, ETH_ALEN);
7177 os_memcpy(wpa_s->pending_join_dev_addr, params->go_dev_addr,
7178 ETH_ALEN);
7179 return wpas_p2p_join_start(wpa_s, params->go_freq,
7180 params->go_ssid,
7181 params->go_ssid_len);
7182 }
7183
7184 return wpas_p2p_connect(wpa_s, params->peer->p2p_device_addr, NULL,
7185 WPS_NFC, 0, 0, 1, 0, wpa_s->conf->p2p_go_intent,
7186 params->go_freq, -1, 0, 1, 1);
7187}
7188
7189
7190static int wpas_p2p_nfc_auth_join(struct wpa_supplicant *wpa_s,
7191 struct p2p_nfc_params *params, int tag)
7192{
7193 int res, persistent;
7194 struct wpa_ssid *ssid;
7195
7196 wpa_printf(MSG_DEBUG, "P2P: Authorize join-group based on NFC "
7197 "connection handover");
7198 for (wpa_s = wpa_s->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
7199 ssid = wpa_s->current_ssid;
7200 if (ssid == NULL)
7201 continue;
7202 if (ssid->mode != WPAS_MODE_P2P_GO)
7203 continue;
7204 if (wpa_s->ap_iface == NULL)
7205 continue;
7206 break;
7207 }
7208 if (wpa_s == NULL) {
7209 wpa_printf(MSG_DEBUG, "P2P: Could not find GO interface");
7210 return -1;
7211 }
7212
7213 if (wpa_s->parent->p2p_oob_dev_pw_id !=
7214 DEV_PW_NFC_CONNECTION_HANDOVER &&
7215 !wpa_s->parent->p2p_oob_dev_pw) {
7216 wpa_printf(MSG_DEBUG, "P2P: No NFC Dev Pw known");
7217 return -1;
7218 }
7219 res = wpas_ap_wps_add_nfc_pw(
7220 wpa_s, wpa_s->parent->p2p_oob_dev_pw_id,
7221 wpa_s->parent->p2p_oob_dev_pw,
7222 wpa_s->parent->p2p_peer_oob_pk_hash_known ?
7223 wpa_s->parent->p2p_peer_oob_pubkey_hash : NULL);
7224 if (res)
7225 return res;
7226
7227 if (!tag) {
7228 wpa_printf(MSG_DEBUG, "P2P: Negotiated handover - wait for peer to join without invitation");
7229 return 0;
7230 }
7231
7232 if (!params->peer ||
7233 !(params->peer->dev_capab & P2P_DEV_CAPAB_INVITATION_PROCEDURE))
7234 return 0;
7235
7236 wpa_printf(MSG_DEBUG, "P2P: Static handover - invite peer " MACSTR
7237 " to join", MAC2STR(params->peer->p2p_device_addr));
7238
7239 wpa_s->global->p2p_invite_group = wpa_s;
7240 persistent = ssid->p2p_persistent_group &&
7241 wpas_p2p_get_persistent(wpa_s->parent,
7242 params->peer->p2p_device_addr,
7243 ssid->ssid, ssid->ssid_len);
7244 wpa_s->parent->pending_invite_ssid_id = -1;
7245
7246 return p2p_invite(wpa_s->global->p2p, params->peer->p2p_device_addr,
7247 P2P_INVITE_ROLE_ACTIVE_GO, wpa_s->own_addr,
7248 ssid->ssid, ssid->ssid_len, ssid->frequency,
7249 wpa_s->global->p2p_dev_addr, persistent, 0,
7250 wpa_s->parent->p2p_oob_dev_pw_id);
7251}
7252
7253
7254static int wpas_p2p_nfc_init_go_neg(struct wpa_supplicant *wpa_s,
7255 struct p2p_nfc_params *params,
7256 int forced_freq)
7257{
7258 wpa_printf(MSG_DEBUG, "P2P: Initiate GO Negotiation based on NFC "
7259 "connection handover");
7260 return wpas_p2p_connect(wpa_s, params->peer->p2p_device_addr, NULL,
7261 WPS_NFC, 0, 0, 0, 0, wpa_s->conf->p2p_go_intent,
7262 forced_freq, -1, 0, 1, 1);
7263}
7264
7265
7266static int wpas_p2p_nfc_resp_go_neg(struct wpa_supplicant *wpa_s,
7267 struct p2p_nfc_params *params,
7268 int forced_freq)
7269{
7270 int res;
7271
7272 wpa_printf(MSG_DEBUG, "P2P: Authorize GO Negotiation based on NFC "
7273 "connection handover");
7274 res = wpas_p2p_connect(wpa_s, params->peer->p2p_device_addr, NULL,
7275 WPS_NFC, 0, 0, 0, 1, wpa_s->conf->p2p_go_intent,
7276 forced_freq, -1, 0, 1, 1);
7277 if (res)
7278 return res;
7279
7280 res = wpas_p2p_listen(wpa_s, 60);
7281 if (res) {
7282 p2p_unauthorize(wpa_s->global->p2p,
7283 params->peer->p2p_device_addr);
7284 }
7285
7286 return res;
7287}
7288
7289
7290static int wpas_p2p_nfc_connection_handover(struct wpa_supplicant *wpa_s,
7291 const struct wpabuf *data,
7292 int sel, int tag, int forced_freq)
7293{
7294 const u8 *pos, *end;
7295 u16 len, id;
7296 struct p2p_nfc_params params;
7297 int res;
7298
7299 os_memset(&params, 0, sizeof(params));
7300 params.sel = sel;
7301
7302 wpa_hexdump_buf(MSG_DEBUG, "P2P: Received NFC tag payload", data);
7303
7304 pos = wpabuf_head(data);
7305 end = pos + wpabuf_len(data);
7306
7307 if (end - pos < 2) {
7308 wpa_printf(MSG_DEBUG, "P2P: Not enough data for Length of WSC "
7309 "attributes");
7310 return -1;
7311 }
7312 len = WPA_GET_BE16(pos);
7313 pos += 2;
7314 if (pos + len > end) {
7315 wpa_printf(MSG_DEBUG, "P2P: Not enough data for WSC "
7316 "attributes");
7317 return -1;
7318 }
7319 params.wsc_attr = pos;
7320 params.wsc_len = len;
7321 pos += len;
7322
7323 if (end - pos < 2) {
7324 wpa_printf(MSG_DEBUG, "P2P: Not enough data for Length of P2P "
7325 "attributes");
7326 return -1;
7327 }
7328 len = WPA_GET_BE16(pos);
7329 pos += 2;
7330 if (pos + len > end) {
7331 wpa_printf(MSG_DEBUG, "P2P: Not enough data for P2P "
7332 "attributes");
7333 return -1;
7334 }
7335 params.p2p_attr = pos;
7336 params.p2p_len = len;
7337 pos += len;
7338
7339 wpa_hexdump(MSG_DEBUG, "P2P: WSC attributes",
7340 params.wsc_attr, params.wsc_len);
7341 wpa_hexdump(MSG_DEBUG, "P2P: P2P attributes",
7342 params.p2p_attr, params.p2p_len);
7343 if (pos < end) {
7344 wpa_hexdump(MSG_DEBUG,
7345 "P2P: Ignored extra data after P2P attributes",
7346 pos, end - pos);
7347 }
7348
7349 res = p2p_process_nfc_connection_handover(wpa_s->global->p2p, &params);
7350 if (res)
7351 return res;
7352
7353 if (params.next_step == NO_ACTION)
7354 return 0;
7355
7356 if (params.next_step == BOTH_GO) {
7357 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_NFC_BOTH_GO "peer=" MACSTR,
7358 MAC2STR(params.peer->p2p_device_addr));
7359 return 0;
7360 }
7361
7362 if (params.next_step == PEER_CLIENT) {
7363 if (!is_zero_ether_addr(params.go_dev_addr)) {
7364 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_NFC_PEER_CLIENT
7365 "peer=" MACSTR " freq=%d go_dev_addr=" MACSTR
7366 " ssid=\"%s\"",
7367 MAC2STR(params.peer->p2p_device_addr),
7368 params.go_freq,
7369 MAC2STR(params.go_dev_addr),
7370 wpa_ssid_txt(params.go_ssid,
7371 params.go_ssid_len));
7372 } else {
7373 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_NFC_PEER_CLIENT
7374 "peer=" MACSTR " freq=%d",
7375 MAC2STR(params.peer->p2p_device_addr),
7376 params.go_freq);
7377 }
7378 return 0;
7379 }
7380
7381 if (wpas_p2p_cli_freq(wpa_s, NULL, NULL)) {
7382 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_NFC_WHILE_CLIENT "peer="
7383 MACSTR, MAC2STR(params.peer->p2p_device_addr));
7384 return 0;
7385 }
7386
7387 wpabuf_free(wpa_s->p2p_oob_dev_pw);
7388 wpa_s->p2p_oob_dev_pw = NULL;
7389
7390 if (params.oob_dev_pw_len < WPS_OOB_PUBKEY_HASH_LEN + 2) {
7391 wpa_printf(MSG_DEBUG, "P2P: No peer OOB Dev Pw "
7392 "received");
7393 return -1;
7394 }
7395
7396 id = WPA_GET_BE16(params.oob_dev_pw + WPS_OOB_PUBKEY_HASH_LEN);
7397 wpa_printf(MSG_DEBUG, "P2P: Peer OOB Dev Pw %u", id);
7398 wpa_hexdump(MSG_DEBUG, "P2P: Peer OOB Public Key hash",
7399 params.oob_dev_pw, WPS_OOB_PUBKEY_HASH_LEN);
7400 os_memcpy(wpa_s->p2p_peer_oob_pubkey_hash,
7401 params.oob_dev_pw, WPS_OOB_PUBKEY_HASH_LEN);
7402 wpa_s->p2p_peer_oob_pk_hash_known = 1;
7403
7404 if (tag) {
7405 if (id < 0x10) {
7406 wpa_printf(MSG_DEBUG, "P2P: Static handover - invalid "
7407 "peer OOB Device Password Id %u", id);
7408 return -1;
7409 }
7410 wpa_printf(MSG_DEBUG, "P2P: Static handover - use peer OOB "
7411 "Device Password Id %u", id);
7412 wpa_hexdump_key(MSG_DEBUG, "P2P: Peer OOB Device Password",
7413 params.oob_dev_pw + WPS_OOB_PUBKEY_HASH_LEN + 2,
7414 params.oob_dev_pw_len -
7415 WPS_OOB_PUBKEY_HASH_LEN - 2);
7416 wpa_s->p2p_oob_dev_pw_id = id;
7417 wpa_s->p2p_oob_dev_pw = wpabuf_alloc_copy(
7418 params.oob_dev_pw + WPS_OOB_PUBKEY_HASH_LEN + 2,
7419 params.oob_dev_pw_len -
7420 WPS_OOB_PUBKEY_HASH_LEN - 2);
7421 if (wpa_s->p2p_oob_dev_pw == NULL)
7422 return -1;
7423
7424 if (wpa_s->conf->wps_nfc_dh_pubkey == NULL &&
7425 wps_nfc_gen_dh(&wpa_s->conf->wps_nfc_dh_pubkey,
7426 &wpa_s->conf->wps_nfc_dh_privkey) < 0)
7427 return -1;
7428 } else {
7429 wpa_printf(MSG_DEBUG, "P2P: Using abbreviated WPS handshake "
7430 "without Device Password");
7431 wpa_s->p2p_oob_dev_pw_id = DEV_PW_NFC_CONNECTION_HANDOVER;
7432 }
7433
7434 switch (params.next_step) {
7435 case NO_ACTION:
7436 case BOTH_GO:
7437 case PEER_CLIENT:
7438 /* already covered above */
7439 return 0;
7440 case JOIN_GROUP:
7441 return wpas_p2p_nfc_join_group(wpa_s, &params);
7442 case AUTH_JOIN:
7443 return wpas_p2p_nfc_auth_join(wpa_s, &params, tag);
7444 case INIT_GO_NEG:
7445 return wpas_p2p_nfc_init_go_neg(wpa_s, &params, forced_freq);
7446 case RESP_GO_NEG:
7447 /* TODO: use own OOB Dev Pw */
7448 return wpas_p2p_nfc_resp_go_neg(wpa_s, &params, forced_freq);
7449 }
7450
7451 return -1;
7452}
7453
7454
7455int wpas_p2p_nfc_tag_process(struct wpa_supplicant *wpa_s,
7456 const struct wpabuf *data, int forced_freq)
7457{
7458 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
7459 return -1;
7460
7461 return wpas_p2p_nfc_connection_handover(wpa_s, data, 1, 1, forced_freq);
7462}
7463
7464
7465int wpas_p2p_nfc_report_handover(struct wpa_supplicant *wpa_s, int init,
7466 const struct wpabuf *req,
7467 const struct wpabuf *sel, int forced_freq)
7468{
7469 struct wpabuf *tmp;
7470 int ret;
7471
7472 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
7473 return -1;
7474
7475 wpa_printf(MSG_DEBUG, "NFC: P2P connection handover reported");
7476
7477 wpa_hexdump_ascii(MSG_DEBUG, "NFC: Req",
7478 wpabuf_head(req), wpabuf_len(req));
7479 wpa_hexdump_ascii(MSG_DEBUG, "NFC: Sel",
7480 wpabuf_head(sel), wpabuf_len(sel));
7481 if (forced_freq)
7482 wpa_printf(MSG_DEBUG, "NFC: Forced freq %d", forced_freq);
7483 tmp = ndef_parse_p2p(init ? sel : req);
7484 if (tmp == NULL) {
7485 wpa_printf(MSG_DEBUG, "P2P: Could not parse NDEF");
7486 return -1;
7487 }
7488
7489 ret = wpas_p2p_nfc_connection_handover(wpa_s, tmp, init, 0,
7490 forced_freq);
7491 wpabuf_free(tmp);
7492
7493 return ret;
7494}
7495
7496
7497int wpas_p2p_nfc_tag_enabled(struct wpa_supplicant *wpa_s, int enabled)
7498{
7499 const u8 *if_addr;
7500 int go_intent = wpa_s->conf->p2p_go_intent;
7501 struct wpa_supplicant *iface;
7502
7503 if (wpa_s->global->p2p == NULL)
7504 return -1;
7505
7506 if (!enabled) {
7507 wpa_printf(MSG_DEBUG, "P2P: Disable use of own NFC Tag");
7508 for (iface = wpa_s->global->ifaces; iface; iface = iface->next)
7509 {
7510 if (!iface->ap_iface)
7511 continue;
7512 hostapd_wps_nfc_token_disable(iface->ap_iface->bss[0]);
7513 }
7514 p2p_set_authorized_oob_dev_pw_id(wpa_s->global->p2p, 0,
7515 0, NULL);
7516 if (wpa_s->p2p_nfc_tag_enabled)
7517 wpas_p2p_remove_pending_group_interface(wpa_s);
7518 wpa_s->p2p_nfc_tag_enabled = 0;
7519 return 0;
7520 }
7521
7522 if (wpa_s->global->p2p_disabled)
7523 return -1;
7524
7525 if (wpa_s->conf->wps_nfc_dh_pubkey == NULL ||
7526 wpa_s->conf->wps_nfc_dh_privkey == NULL ||
7527 wpa_s->conf->wps_nfc_dev_pw == NULL ||
7528 wpa_s->conf->wps_nfc_dev_pw_id < 0x10) {
7529 wpa_printf(MSG_DEBUG, "P2P: NFC password token not configured "
7530 "to allow static handover cases");
7531 return -1;
7532 }
7533
7534 wpa_printf(MSG_DEBUG, "P2P: Enable use of own NFC Tag");
7535
7536 wpa_s->p2p_oob_dev_pw_id = wpa_s->conf->wps_nfc_dev_pw_id;
7537 wpabuf_free(wpa_s->p2p_oob_dev_pw);
7538 wpa_s->p2p_oob_dev_pw = wpabuf_dup(wpa_s->conf->wps_nfc_dev_pw);
7539 if (wpa_s->p2p_oob_dev_pw == NULL)
7540 return -1;
7541 wpa_s->p2p_peer_oob_pk_hash_known = 0;
7542
7543 wpa_s->create_p2p_iface = wpas_p2p_create_iface(wpa_s);
7544
7545 if (wpa_s->create_p2p_iface) {
7546 enum wpa_driver_if_type iftype;
7547 /* Prepare to add a new interface for the group */
7548 iftype = WPA_IF_P2P_GROUP;
7549 if (go_intent == 15)
7550 iftype = WPA_IF_P2P_GO;
7551 if (wpas_p2p_add_group_interface(wpa_s, iftype) < 0) {
7552 wpa_printf(MSG_ERROR, "P2P: Failed to allocate a new "
7553 "interface for the group");
7554 return -1;
7555 }
7556
7557 if_addr = wpa_s->pending_interface_addr;
7558 } else
7559 if_addr = wpa_s->own_addr;
7560
7561 wpa_s->p2p_nfc_tag_enabled = enabled;
7562
7563 for (iface = wpa_s->global->ifaces; iface; iface = iface->next) {
7564 struct hostapd_data *hapd;
7565 if (iface->ap_iface == NULL)
7566 continue;
7567 hapd = iface->ap_iface->bss[0];
7568 wpabuf_free(hapd->conf->wps_nfc_dh_pubkey);
7569 hapd->conf->wps_nfc_dh_pubkey =
7570 wpabuf_dup(wpa_s->conf->wps_nfc_dh_pubkey);
7571 wpabuf_free(hapd->conf->wps_nfc_dh_privkey);
7572 hapd->conf->wps_nfc_dh_privkey =
7573 wpabuf_dup(wpa_s->conf->wps_nfc_dh_privkey);
7574 wpabuf_free(hapd->conf->wps_nfc_dev_pw);
7575 hapd->conf->wps_nfc_dev_pw =
7576 wpabuf_dup(wpa_s->conf->wps_nfc_dev_pw);
7577 hapd->conf->wps_nfc_dev_pw_id = wpa_s->conf->wps_nfc_dev_pw_id;
7578
7579 if (hostapd_wps_nfc_token_enable(iface->ap_iface->bss[0]) < 0) {
7580 wpa_dbg(iface, MSG_DEBUG,
7581 "P2P: Failed to enable NFC Tag for GO");
7582 }
7583 }
7584 p2p_set_authorized_oob_dev_pw_id(
7585 wpa_s->global->p2p, wpa_s->conf->wps_nfc_dev_pw_id, go_intent,
7586 if_addr);
7587
7588 return 0;
7589}
7590
7591#endif /* CONFIG_WPS_NFC */