blob: d91877cb35260e05879793f28e05e30a3c056e9d [file] [log] [blame]
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001/*
2 * wpa_supplicant - P2P
3 * Copyright (c) 2009-2010, Atheros Communications
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08004 * Copyright (c) 2010-2014, Jouni Malinen <j@w1.fi>
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005 *
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08006 * This software may be distributed under the terms of the BSD license.
7 * See README for more details.
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008 */
9
10#include "includes.h"
11
12#include "common.h"
13#include "eloop.h"
14#include "common/ieee802_11_common.h"
15#include "common/ieee802_11_defs.h"
16#include "common/wpa_ctrl.h"
17#include "wps/wps_i.h"
18#include "p2p/p2p.h"
19#include "ap/hostapd.h"
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080020#include "ap/ap_config.h"
Dmitry Shmidt391c59f2013-09-03 12:16:28 -070021#include "ap/sta_info.h"
22#include "ap/ap_drv_ops.h"
Dmitry Shmidtcf32e602014-01-28 10:57:39 -080023#include "ap/wps_hostapd.h"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070024#include "ap/p2p_hostapd.h"
Jouni Malinen75ecf522011-06-27 15:19:46 -070025#include "eapol_supp/eapol_supp_sm.h"
26#include "rsn_supp/wpa.h"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070027#include "wpa_supplicant_i.h"
28#include "driver_i.h"
29#include "ap.h"
30#include "config_ssid.h"
31#include "config.h"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070032#include "notify.h"
33#include "scan.h"
34#include "bss.h"
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080035#include "offchannel.h"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070036#include "wps_supplicant.h"
37#include "p2p_supplicant.h"
Dmitry Shmidt04f534e2013-12-09 15:50:16 -080038#include "wifi_display.h"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070039
40
41/*
42 * How many times to try to scan to find the GO before giving up on join
43 * request.
44 */
45#define P2P_MAX_JOIN_SCAN_ATTEMPTS 10
46
Dmitry Shmidt04949592012-07-19 12:16:46 -070047#define P2P_AUTO_PD_SCAN_ATTEMPTS 5
48
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080049#ifndef P2P_MAX_CLIENT_IDLE
50/*
51 * How many seconds to try to reconnect to the GO when connection in P2P client
52 * role has been lost.
53 */
54#define P2P_MAX_CLIENT_IDLE 10
55#endif /* P2P_MAX_CLIENT_IDLE */
56
Dmitry Shmidt04949592012-07-19 12:16:46 -070057#ifndef P2P_MAX_INITIAL_CONN_WAIT
58/*
59 * How many seconds to wait for initial 4-way handshake to get completed after
Dmitry Shmidt15907092014-03-25 10:42:57 -070060 * WPS provisioning step or after the re-invocation of a persistent group on a
61 * P2P Client.
Dmitry Shmidt04949592012-07-19 12:16:46 -070062 */
63#define P2P_MAX_INITIAL_CONN_WAIT 10
64#endif /* P2P_MAX_INITIAL_CONN_WAIT */
65
Dmitry Shmidt92c368d2013-08-29 12:37:21 -070066#ifndef P2P_MAX_INITIAL_CONN_WAIT_GO
67/*
68 * How many seconds to wait for initial 4-way handshake to get completed after
69 * WPS provisioning step on the GO. This controls the extra time the P2P
70 * operation is considered to be in progress (e.g., to delay other scans) after
71 * WPS provisioning has been completed on the GO during group formation.
72 */
73#define P2P_MAX_INITIAL_CONN_WAIT_GO 10
74#endif /* P2P_MAX_INITIAL_CONN_WAIT_GO */
75
Dmitry Shmidt56052862013-10-04 10:23:25 -070076#ifndef P2P_MAX_INITIAL_CONN_WAIT_GO_REINVOKE
77/*
78 * How many seconds to wait for initial 4-way handshake to get completed after
79 * re-invocation of a persistent group on the GO when the client is expected
80 * to connect automatically (no user interaction).
81 */
82#define P2P_MAX_INITIAL_CONN_WAIT_GO_REINVOKE 15
83#endif /* P2P_MAX_INITIAL_CONN_WAIT_GO_REINVOKE */
84
Dmitry Shmidt34af3062013-07-11 10:46:32 -070085#define P2P_MGMT_DEVICE_PREFIX "p2p-dev-"
86
Dmitry Shmidt61d9df32012-08-29 16:22:06 -070087enum p2p_group_removal_reason {
88 P2P_GROUP_REMOVAL_UNKNOWN,
89 P2P_GROUP_REMOVAL_SILENT,
90 P2P_GROUP_REMOVAL_FORMATION_FAILED,
91 P2P_GROUP_REMOVAL_REQUESTED,
92 P2P_GROUP_REMOVAL_IDLE_TIMEOUT,
93 P2P_GROUP_REMOVAL_UNAVAILABLE,
94 P2P_GROUP_REMOVAL_GO_ENDING_SESSION,
Dmitry Shmidt04f534e2013-12-09 15:50:16 -080095 P2P_GROUP_REMOVAL_PSK_FAILURE,
96 P2P_GROUP_REMOVAL_FREQ_CONFLICT
Dmitry Shmidt61d9df32012-08-29 16:22:06 -070097};
98
Jouni Malinendc7b7132012-09-14 12:53:47 -070099
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700100static void wpas_p2p_long_listen_timeout(void *eloop_ctx, void *timeout_ctx);
101static struct wpa_supplicant *
102wpas_p2p_get_group_iface(struct wpa_supplicant *wpa_s, int addr_allocated,
103 int go);
Dmitry Shmidtcf32e602014-01-28 10:57:39 -0800104static int wpas_p2p_join_start(struct wpa_supplicant *wpa_s, int freq,
105 const u8 *ssid, size_t ssid_len);
Dmitry Shmidt344abd32014-01-14 13:17:00 -0800106static void wpas_p2p_join_scan_req(struct wpa_supplicant *wpa_s, int freq,
107 const u8 *ssid, size_t ssid_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700108static void wpas_p2p_join_scan(void *eloop_ctx, void *timeout_ctx);
109static int wpas_p2p_join(struct wpa_supplicant *wpa_s, const u8 *iface_addr,
Dmitry Shmidt04949592012-07-19 12:16:46 -0700110 const u8 *dev_addr, enum p2p_wps_method wps_method,
Dmitry Shmidtcf32e602014-01-28 10:57:39 -0800111 int auto_join, int freq,
112 const u8 *ssid, size_t ssid_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700113static int wpas_p2p_create_iface(struct wpa_supplicant *wpa_s);
114static void wpas_p2p_cross_connect_setup(struct wpa_supplicant *wpa_s);
115static void wpas_p2p_group_idle_timeout(void *eloop_ctx, void *timeout_ctx);
116static void wpas_p2p_set_group_idle_timeout(struct wpa_supplicant *wpa_s);
Dmitry Shmidtf8623282013-02-20 14:34:59 -0800117static void wpas_p2p_group_formation_timeout(void *eloop_ctx,
118 void *timeout_ctx);
Dmitry Shmidt04f534e2013-12-09 15:50:16 -0800119static void wpas_p2p_group_freq_conflict(void *eloop_ctx, void *timeout_ctx);
Dmitry Shmidt04949592012-07-19 12:16:46 -0700120static void wpas_p2p_fallback_to_go_neg(struct wpa_supplicant *wpa_s,
121 int group_added);
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800122static int wpas_p2p_stop_find_oper(struct wpa_supplicant *wpa_s);
Dmitry Shmidtbd14a572014-02-18 10:33:49 -0800123static void wpas_stop_listen(void *ctx);
Dmitry Shmidt684785c2014-05-12 13:34:29 -0700124static void wpas_p2p_psk_failure_removal(void *eloop_ctx, void *timeout_ctx);
Dmitry Shmidt43cb5782014-06-16 16:23:22 -0700125static void wpas_p2p_group_deinit(struct wpa_supplicant *wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700126
127
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -0700128/*
129 * Get the number of concurrent channels that the HW can operate, but that are
130 * currently not in use by any of the wpa_supplicant interfaces.
131 */
132static int wpas_p2p_num_unused_channels(struct wpa_supplicant *wpa_s)
133{
134 int *freqs;
Dmitry Shmidtb96dad42013-11-05 10:07:29 -0800135 int num, unused;
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -0700136
137 freqs = os_calloc(wpa_s->num_multichan_concurrent, sizeof(int));
138 if (!freqs)
139 return -1;
140
141 num = get_shared_radio_freqs(wpa_s, freqs,
142 wpa_s->num_multichan_concurrent);
143 os_free(freqs);
144
Dmitry Shmidtb96dad42013-11-05 10:07:29 -0800145 unused = wpa_s->num_multichan_concurrent - num;
146 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: num_unused_channels: %d", unused);
147 return unused;
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -0700148}
149
150
151/*
152 * Get the frequencies that are currently in use by one or more of the virtual
153 * interfaces, and that are also valid for P2P operation.
154 */
Dmitry Shmidt43cb5782014-06-16 16:23:22 -0700155static unsigned int
156wpas_p2p_valid_oper_freqs(struct wpa_supplicant *wpa_s,
157 struct wpa_used_freq_data *p2p_freqs,
158 unsigned int len)
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -0700159{
Dmitry Shmidt43cb5782014-06-16 16:23:22 -0700160 struct wpa_used_freq_data *freqs;
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -0700161 unsigned int num, i, j;
162
Dmitry Shmidt43cb5782014-06-16 16:23:22 -0700163 freqs = os_calloc(wpa_s->num_multichan_concurrent,
164 sizeof(struct wpa_used_freq_data));
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -0700165 if (!freqs)
Dmitry Shmidt43cb5782014-06-16 16:23:22 -0700166 return 0;
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -0700167
Dmitry Shmidt43cb5782014-06-16 16:23:22 -0700168 num = get_shared_radio_freqs_data(wpa_s, freqs,
169 wpa_s->num_multichan_concurrent);
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -0700170
Dmitry Shmidt43cb5782014-06-16 16:23:22 -0700171 os_memset(p2p_freqs, 0, sizeof(struct wpa_used_freq_data) * len);
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -0700172
173 for (i = 0, j = 0; i < num && j < len; i++) {
Dmitry Shmidt43cb5782014-06-16 16:23:22 -0700174 if (p2p_supported_freq(wpa_s->global->p2p, freqs[i].freq))
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -0700175 p2p_freqs[j++] = freqs[i];
176 }
177
178 os_free(freqs);
179
Dmitry Shmidt43cb5782014-06-16 16:23:22 -0700180 dump_freq_data(wpa_s, "valid for P2P", p2p_freqs, j);
Dmitry Shmidtb96dad42013-11-05 10:07:29 -0800181
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -0700182 return j;
183}
184
185
Dmitry Shmidt700a1372013-03-15 14:14:44 -0700186static void wpas_p2p_set_own_freq_preference(struct wpa_supplicant *wpa_s,
187 int freq)
188{
189 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
190 return;
Dmitry Shmidta0d265f2013-11-19 13:13:41 -0800191 if (wpa_s->parent->conf->p2p_ignore_shared_freq &&
192 freq > 0 && wpa_s->num_multichan_concurrent > 1 &&
193 wpas_p2p_num_unused_channels(wpa_s) > 0) {
194 wpa_printf(MSG_DEBUG, "P2P: Ignore own channel preference %d MHz due to p2p_ignore_shared_freq=1 configuration",
195 freq);
Dmitry Shmidt700a1372013-03-15 14:14:44 -0700196 freq = 0;
Dmitry Shmidta0d265f2013-11-19 13:13:41 -0800197 }
Dmitry Shmidt700a1372013-03-15 14:14:44 -0700198 p2p_set_own_freq_preference(wpa_s->global->p2p, freq);
199}
200
201
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700202static void wpas_p2p_scan_res_handler(struct wpa_supplicant *wpa_s,
203 struct wpa_scan_results *scan_res)
204{
205 size_t i;
206
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800207 if (wpa_s->p2p_scan_work) {
208 struct wpa_radio_work *work = wpa_s->p2p_scan_work;
209 wpa_s->p2p_scan_work = NULL;
210 radio_work_done(work);
211 }
212
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700213 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
214 return;
215
216 wpa_printf(MSG_DEBUG, "P2P: Scan results received (%d BSS)",
217 (int) scan_res->num);
218
219 for (i = 0; i < scan_res->num; i++) {
220 struct wpa_scan_res *bss = scan_res->res[i];
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800221 struct os_reltime time_tmp_age, entry_ts;
Dmitry Shmidt96571392013-10-14 12:54:46 -0700222 const u8 *ies;
223 size_t ies_len;
224
Dmitry Shmidtf8623282013-02-20 14:34:59 -0800225 time_tmp_age.sec = bss->age / 1000;
226 time_tmp_age.usec = (bss->age % 1000) * 1000;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800227 os_reltime_sub(&scan_res->fetch_time, &time_tmp_age, &entry_ts);
Dmitry Shmidt96571392013-10-14 12:54:46 -0700228
229 ies = (const u8 *) (bss + 1);
230 ies_len = bss->ie_len;
231 if (bss->beacon_ie_len > 0 &&
232 !wpa_scan_get_vendor_ie(bss, P2P_IE_VENDOR_TYPE) &&
233 wpa_scan_get_vendor_ie_beacon(bss, P2P_IE_VENDOR_TYPE)) {
234 wpa_printf(MSG_DEBUG, "P2P: Use P2P IE(s) from Beacon frame since no P2P IE(s) in Probe Response frames received for "
235 MACSTR, MAC2STR(bss->bssid));
236 ies = ies + ies_len;
237 ies_len = bss->beacon_ie_len;
238 }
239
240
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700241 if (p2p_scan_res_handler(wpa_s->global->p2p, bss->bssid,
Dmitry Shmidtf8623282013-02-20 14:34:59 -0800242 bss->freq, &entry_ts, bss->level,
Dmitry Shmidt96571392013-10-14 12:54:46 -0700243 ies, ies_len) > 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700244 break;
245 }
246
247 p2p_scan_res_handled(wpa_s->global->p2p);
248}
249
250
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800251static void wpas_p2p_trigger_scan_cb(struct wpa_radio_work *work, int deinit)
252{
253 struct wpa_supplicant *wpa_s = work->wpa_s;
254 struct wpa_driver_scan_params *params = work->ctx;
255 int ret;
256
257 if (deinit) {
Dmitry Shmidtbd14a572014-02-18 10:33:49 -0800258 if (!work->started) {
259 wpa_scan_free_params(params);
260 return;
261 }
262
263 wpa_s->p2p_scan_work = NULL;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800264 return;
265 }
266
267 ret = wpa_drv_scan(wpa_s, params);
268 wpa_scan_free_params(params);
269 work->ctx = NULL;
270 if (ret) {
271 radio_work_done(work);
272 return;
273 }
274
275 os_get_reltime(&wpa_s->scan_trigger_time);
276 wpa_s->scan_res_handler = wpas_p2p_scan_res_handler;
277 wpa_s->own_scan_requested = 1;
278 wpa_s->p2p_scan_work = work;
279}
280
281
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700282static int wpas_p2p_scan(void *ctx, enum p2p_scan_type type, int freq,
283 unsigned int num_req_dev_types,
Dmitry Shmidt04949592012-07-19 12:16:46 -0700284 const u8 *req_dev_types, const u8 *dev_id, u16 pw_id)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700285{
286 struct wpa_supplicant *wpa_s = ctx;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800287 struct wpa_driver_scan_params *params = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700288 struct wpabuf *wps_ie, *ies;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800289 size_t ielen;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800290 u8 *n;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700291
292 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
293 return -1;
294
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800295 if (wpa_s->p2p_scan_work) {
296 wpa_dbg(wpa_s, MSG_INFO, "P2P: Reject scan trigger since one is already pending");
297 return -1;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700298 }
299
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800300 params = os_zalloc(sizeof(*params));
301 if (params == NULL)
302 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700303
304 /* P2P Wildcard SSID */
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800305 params->num_ssids = 1;
306 n = os_malloc(P2P_WILDCARD_SSID_LEN);
307 if (n == NULL)
308 goto fail;
309 os_memcpy(n, P2P_WILDCARD_SSID, P2P_WILDCARD_SSID_LEN);
310 params->ssids[0].ssid = n;
311 params->ssids[0].ssid_len = P2P_WILDCARD_SSID_LEN;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700312
313 wpa_s->wps->dev.p2p = 1;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700314 wps_ie = wps_build_probe_req_ie(pw_id, &wpa_s->wps->dev,
315 wpa_s->wps->uuid, WPS_REQ_ENROLLEE,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700316 num_req_dev_types, req_dev_types);
317 if (wps_ie == NULL)
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800318 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700319
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800320 ielen = p2p_scan_ie_buf_len(wpa_s->global->p2p);
321 ies = wpabuf_alloc(wpabuf_len(wps_ie) + ielen);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700322 if (ies == NULL) {
323 wpabuf_free(wps_ie);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800324 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700325 }
326 wpabuf_put_buf(ies, wps_ie);
327 wpabuf_free(wps_ie);
328
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -0800329 p2p_scan_ie(wpa_s->global->p2p, ies, dev_id);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700330
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800331 params->p2p_probe = 1;
332 n = os_malloc(wpabuf_len(ies));
333 if (n == NULL) {
334 wpabuf_free(ies);
335 goto fail;
336 }
337 os_memcpy(n, wpabuf_head(ies), wpabuf_len(ies));
338 params->extra_ies = n;
339 params->extra_ies_len = wpabuf_len(ies);
340 wpabuf_free(ies);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700341
342 switch (type) {
343 case P2P_SCAN_SOCIAL:
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800344 params->freqs = os_malloc(4 * sizeof(int));
345 if (params->freqs == NULL)
346 goto fail;
347 params->freqs[0] = 2412;
348 params->freqs[1] = 2437;
349 params->freqs[2] = 2462;
350 params->freqs[3] = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700351 break;
352 case P2P_SCAN_FULL:
353 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700354 case P2P_SCAN_SOCIAL_PLUS_ONE:
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800355 params->freqs = os_malloc(5 * sizeof(int));
356 if (params->freqs == NULL)
357 goto fail;
358 params->freqs[0] = 2412;
359 params->freqs[1] = 2437;
360 params->freqs[2] = 2462;
361 params->freqs[3] = freq;
362 params->freqs[4] = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700363 break;
364 }
365
Dmitry Shmidtbd14a572014-02-18 10:33:49 -0800366 radio_remove_works(wpa_s, "p2p-scan", 0);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800367 if (radio_add_work(wpa_s, 0, "p2p-scan", 0, wpas_p2p_trigger_scan_cb,
368 params) < 0)
369 goto fail;
370 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700371
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800372fail:
373 wpa_scan_free_params(params);
374 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700375}
376
377
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700378static enum wpa_driver_if_type wpas_p2p_if_type(int p2p_group_interface)
379{
380 switch (p2p_group_interface) {
381 case P2P_GROUP_INTERFACE_PENDING:
382 return WPA_IF_P2P_GROUP;
383 case P2P_GROUP_INTERFACE_GO:
384 return WPA_IF_P2P_GO;
385 case P2P_GROUP_INTERFACE_CLIENT:
386 return WPA_IF_P2P_CLIENT;
387 }
388
389 return WPA_IF_P2P_GROUP;
390}
391
392
393static struct wpa_supplicant * wpas_get_p2p_group(struct wpa_supplicant *wpa_s,
394 const u8 *ssid,
395 size_t ssid_len, int *go)
396{
397 struct wpa_ssid *s;
398
399 for (wpa_s = wpa_s->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
400 for (s = wpa_s->conf->ssid; s; s = s->next) {
401 if (s->disabled != 0 || !s->p2p_group ||
402 s->ssid_len != ssid_len ||
403 os_memcmp(ssid, s->ssid, ssid_len) != 0)
404 continue;
405 if (s->mode == WPAS_MODE_P2P_GO &&
406 s != wpa_s->current_ssid)
407 continue;
408 if (go)
409 *go = s->mode == WPAS_MODE_P2P_GO;
410 return wpa_s;
411 }
412 }
413
414 return NULL;
415}
416
417
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700418static int wpas_p2p_group_delete(struct wpa_supplicant *wpa_s,
419 enum p2p_group_removal_reason removal_reason)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700420{
421 struct wpa_ssid *ssid;
422 char *gtype;
423 const char *reason;
424
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700425 ssid = wpa_s->current_ssid;
426 if (ssid == NULL) {
427 /*
428 * The current SSID was not known, but there may still be a
Dmitry Shmidtaa532512012-09-24 10:35:31 -0700429 * pending P2P group interface waiting for provisioning or a
430 * P2P group that is trying to reconnect.
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700431 */
432 ssid = wpa_s->conf->ssid;
433 while (ssid) {
Jouni Malinen9d712832012-10-05 11:01:57 -0700434 if (ssid->p2p_group && ssid->disabled != 2)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700435 break;
436 ssid = ssid->next;
437 }
Jouni Malinen5c44edb2012-08-31 21:35:32 +0300438 if (ssid == NULL &&
439 wpa_s->p2p_group_interface == NOT_P2P_GROUP_INTERFACE)
440 {
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700441 wpa_printf(MSG_ERROR, "P2P: P2P group interface "
442 "not found");
443 return -1;
444 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700445 }
446 if (wpa_s->p2p_group_interface == P2P_GROUP_INTERFACE_GO)
447 gtype = "GO";
448 else if (wpa_s->p2p_group_interface == P2P_GROUP_INTERFACE_CLIENT ||
449 (ssid && ssid->mode == WPAS_MODE_INFRA)) {
450 wpa_s->reassociate = 0;
451 wpa_s->disconnected = 1;
452 wpa_supplicant_deauthenticate(wpa_s,
453 WLAN_REASON_DEAUTH_LEAVING);
454 gtype = "client";
455 } else
456 gtype = "GO";
457 if (wpa_s->cross_connect_in_use) {
458 wpa_s->cross_connect_in_use = 0;
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -0700459 wpa_msg_global(wpa_s->parent, MSG_INFO,
460 P2P_EVENT_CROSS_CONNECT_DISABLE "%s %s",
461 wpa_s->ifname, wpa_s->cross_connect_uplink);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700462 }
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700463 switch (removal_reason) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700464 case P2P_GROUP_REMOVAL_REQUESTED:
465 reason = " reason=REQUESTED";
466 break;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700467 case P2P_GROUP_REMOVAL_FORMATION_FAILED:
468 reason = " reason=FORMATION_FAILED";
469 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700470 case P2P_GROUP_REMOVAL_IDLE_TIMEOUT:
471 reason = " reason=IDLE";
472 break;
473 case P2P_GROUP_REMOVAL_UNAVAILABLE:
474 reason = " reason=UNAVAILABLE";
475 break;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700476 case P2P_GROUP_REMOVAL_GO_ENDING_SESSION:
477 reason = " reason=GO_ENDING_SESSION";
478 break;
Dmitry Shmidt391c59f2013-09-03 12:16:28 -0700479 case P2P_GROUP_REMOVAL_PSK_FAILURE:
480 reason = " reason=PSK_FAILURE";
481 break;
Dmitry Shmidt04f534e2013-12-09 15:50:16 -0800482 case P2P_GROUP_REMOVAL_FREQ_CONFLICT:
483 reason = " reason=FREQ_CONFLICT";
484 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700485 default:
486 reason = "";
487 break;
488 }
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700489 if (removal_reason != P2P_GROUP_REMOVAL_SILENT) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -0700490 wpa_msg_global(wpa_s->parent, MSG_INFO,
491 P2P_EVENT_GROUP_REMOVED "%s %s%s",
492 wpa_s->ifname, gtype, reason);
Dmitry Shmidt04949592012-07-19 12:16:46 -0700493 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700494
Dmitry Shmidt04f534e2013-12-09 15:50:16 -0800495 if (eloop_cancel_timeout(wpas_p2p_group_freq_conflict, wpa_s, NULL) > 0)
496 wpa_printf(MSG_DEBUG, "P2P: Cancelled P2P group freq_conflict timeout");
Dmitry Shmidt04949592012-07-19 12:16:46 -0700497 if (eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL) > 0)
498 wpa_printf(MSG_DEBUG, "P2P: Cancelled P2P group idle timeout");
Dmitry Shmidtf8623282013-02-20 14:34:59 -0800499 if (eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
Dmitry Shmidt2f023192013-03-12 12:44:17 -0700500 wpa_s->parent, NULL) > 0) {
Dmitry Shmidtf8623282013-02-20 14:34:59 -0800501 wpa_printf(MSG_DEBUG, "P2P: Cancelled P2P group formation "
502 "timeout");
Dmitry Shmidt2f023192013-03-12 12:44:17 -0700503 wpa_s->p2p_in_provisioning = 0;
504 }
Dmitry Shmidt04949592012-07-19 12:16:46 -0700505
Dmitry Shmidt15907092014-03-25 10:42:57 -0700506 wpa_s->p2p_in_invitation = 0;
507
Dmitry Shmidt96571392013-10-14 12:54:46 -0700508 /*
509 * Make sure wait for the first client does not remain active after the
510 * group has been removed.
511 */
512 wpa_s->global->p2p_go_wait_client.sec = 0;
513
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700514 if (removal_reason != P2P_GROUP_REMOVAL_SILENT && ssid)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700515 wpas_notify_p2p_group_removed(wpa_s, ssid, gtype);
516
517 if (wpa_s->p2p_group_interface != NOT_P2P_GROUP_INTERFACE) {
518 struct wpa_global *global;
519 char *ifname;
520 enum wpa_driver_if_type type;
521 wpa_printf(MSG_DEBUG, "P2P: Remove group interface %s",
522 wpa_s->ifname);
523 global = wpa_s->global;
524 ifname = os_strdup(wpa_s->ifname);
525 type = wpas_p2p_if_type(wpa_s->p2p_group_interface);
Dmitry Shmidte15c7b52011-08-03 15:04:35 -0700526 wpa_supplicant_remove_iface(wpa_s->global, wpa_s, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700527 wpa_s = global->ifaces;
528 if (wpa_s && ifname)
529 wpa_drv_if_remove(wpa_s, type, ifname);
530 os_free(ifname);
Jouni Malinen2b89da82012-08-31 22:04:41 +0300531 return 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700532 }
533
Dmitry Shmidt96571392013-10-14 12:54:46 -0700534 if (!wpa_s->p2p_go_group_formation_completed) {
535 wpa_s->global->p2p_group_formation = NULL;
536 wpa_s->p2p_in_provisioning = 0;
537 }
538
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800539 wpa_s->show_group_started = 0;
540 os_free(wpa_s->go_params);
541 wpa_s->go_params = NULL;
542
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800543 wpa_s->waiting_presence_resp = 0;
544
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700545 wpa_printf(MSG_DEBUG, "P2P: Remove temporary group network");
546 if (ssid && (ssid->p2p_group ||
547 ssid->mode == WPAS_MODE_P2P_GROUP_FORMATION ||
548 (ssid->key_mgmt & WPA_KEY_MGMT_WPS))) {
549 int id = ssid->id;
Jouni Malinen75ecf522011-06-27 15:19:46 -0700550 if (ssid == wpa_s->current_ssid) {
551 wpa_sm_set_config(wpa_s->wpa, NULL);
552 eapol_sm_notify_config(wpa_s->eapol, NULL, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700553 wpa_s->current_ssid = NULL;
Jouni Malinen75ecf522011-06-27 15:19:46 -0700554 }
555 /*
556 * Networks objects created during any P2P activities are not
557 * exposed out as they might/will confuse certain non-P2P aware
558 * applications since these network objects won't behave like
559 * regular ones.
560 *
561 * Likewise, we don't send out network removed signals for such
562 * network objects.
563 */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700564 wpa_config_remove_network(wpa_s->conf, id);
565 wpa_supplicant_clear_status(wpa_s);
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -0800566 wpa_supplicant_cancel_sched_scan(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700567 } else {
568 wpa_printf(MSG_DEBUG, "P2P: Temporary group network not "
569 "found");
570 }
Dmitry Shmidt04949592012-07-19 12:16:46 -0700571 if (wpa_s->ap_iface)
572 wpa_supplicant_ap_deinit(wpa_s);
573 else
574 wpa_drv_deinit_p2p_cli(wpa_s);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700575
576 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700577}
578
579
580static int wpas_p2p_persistent_group(struct wpa_supplicant *wpa_s,
581 u8 *go_dev_addr,
582 const u8 *ssid, size_t ssid_len)
583{
584 struct wpa_bss *bss;
585 const u8 *bssid;
586 struct wpabuf *p2p;
587 u8 group_capab;
588 const u8 *addr;
589
590 if (wpa_s->go_params)
591 bssid = wpa_s->go_params->peer_interface_addr;
592 else
593 bssid = wpa_s->bssid;
594
595 bss = wpa_bss_get(wpa_s, bssid, ssid, ssid_len);
Dmitry Shmidtcf32e602014-01-28 10:57:39 -0800596 if (bss == NULL && wpa_s->go_params &&
597 !is_zero_ether_addr(wpa_s->go_params->peer_device_addr))
598 bss = wpa_bss_get_p2p_dev_addr(
599 wpa_s, wpa_s->go_params->peer_device_addr);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700600 if (bss == NULL) {
601 u8 iface_addr[ETH_ALEN];
602 if (p2p_get_interface_addr(wpa_s->global->p2p, bssid,
603 iface_addr) == 0)
604 bss = wpa_bss_get(wpa_s, iface_addr, ssid, ssid_len);
605 }
606 if (bss == NULL) {
607 wpa_printf(MSG_DEBUG, "P2P: Could not figure out whether "
608 "group is persistent - BSS " MACSTR " not found",
609 MAC2STR(bssid));
610 return 0;
611 }
612
613 p2p = wpa_bss_get_vendor_ie_multi(bss, P2P_IE_VENDOR_TYPE);
Dmitry Shmidt96571392013-10-14 12:54:46 -0700614 if (p2p == NULL)
615 p2p = wpa_bss_get_vendor_ie_multi_beacon(bss,
616 P2P_IE_VENDOR_TYPE);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700617 if (p2p == NULL) {
618 wpa_printf(MSG_DEBUG, "P2P: Could not figure out whether "
619 "group is persistent - BSS " MACSTR
620 " did not include P2P IE", MAC2STR(bssid));
621 wpa_hexdump(MSG_DEBUG, "P2P: Probe Response IEs",
622 (u8 *) (bss + 1), bss->ie_len);
623 wpa_hexdump(MSG_DEBUG, "P2P: Beacon IEs",
624 ((u8 *) bss + 1) + bss->ie_len,
625 bss->beacon_ie_len);
626 return 0;
627 }
628
629 group_capab = p2p_get_group_capab(p2p);
630 addr = p2p_get_go_dev_addr(p2p);
631 wpa_printf(MSG_DEBUG, "P2P: Checking whether group is persistent: "
632 "group_capab=0x%x", group_capab);
633 if (addr) {
634 os_memcpy(go_dev_addr, addr, ETH_ALEN);
635 wpa_printf(MSG_DEBUG, "P2P: GO Device Address " MACSTR,
636 MAC2STR(addr));
637 } else
638 os_memset(go_dev_addr, 0, ETH_ALEN);
639 wpabuf_free(p2p);
640
641 wpa_printf(MSG_DEBUG, "P2P: BSS " MACSTR " group_capab=0x%x "
642 "go_dev_addr=" MACSTR,
643 MAC2STR(bssid), group_capab, MAC2STR(go_dev_addr));
644
645 return group_capab & P2P_GROUP_CAPAB_PERSISTENT_GROUP;
646}
647
648
Jouni Malinen75ecf522011-06-27 15:19:46 -0700649static int wpas_p2p_store_persistent_group(struct wpa_supplicant *wpa_s,
650 struct wpa_ssid *ssid,
651 const u8 *go_dev_addr)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700652{
653 struct wpa_ssid *s;
654 int changed = 0;
655
656 wpa_printf(MSG_DEBUG, "P2P: Storing credentials for a persistent "
657 "group (GO Dev Addr " MACSTR ")", MAC2STR(go_dev_addr));
658 for (s = wpa_s->conf->ssid; s; s = s->next) {
659 if (s->disabled == 2 &&
660 os_memcmp(go_dev_addr, s->bssid, ETH_ALEN) == 0 &&
661 s->ssid_len == ssid->ssid_len &&
662 os_memcmp(ssid->ssid, s->ssid, ssid->ssid_len) == 0)
663 break;
664 }
665
666 if (s) {
667 wpa_printf(MSG_DEBUG, "P2P: Update existing persistent group "
668 "entry");
669 if (ssid->passphrase && !s->passphrase)
670 changed = 1;
671 else if (ssid->passphrase && s->passphrase &&
672 os_strcmp(ssid->passphrase, s->passphrase) != 0)
673 changed = 1;
674 } else {
675 wpa_printf(MSG_DEBUG, "P2P: Create a new persistent group "
676 "entry");
677 changed = 1;
678 s = wpa_config_add_network(wpa_s->conf);
679 if (s == NULL)
Jouni Malinen75ecf522011-06-27 15:19:46 -0700680 return -1;
681
682 /*
683 * Instead of network_added we emit persistent_group_added
684 * notification. Also to keep the defense checks in
685 * persistent_group obj registration method, we set the
686 * relevant flags in s to designate it as a persistent group.
687 */
688 s->p2p_group = 1;
689 s->p2p_persistent_group = 1;
690 wpas_notify_persistent_group_added(wpa_s, s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700691 wpa_config_set_network_defaults(s);
692 }
693
694 s->p2p_group = 1;
695 s->p2p_persistent_group = 1;
696 s->disabled = 2;
697 s->bssid_set = 1;
698 os_memcpy(s->bssid, go_dev_addr, ETH_ALEN);
699 s->mode = ssid->mode;
700 s->auth_alg = WPA_AUTH_ALG_OPEN;
701 s->key_mgmt = WPA_KEY_MGMT_PSK;
702 s->proto = WPA_PROTO_RSN;
703 s->pairwise_cipher = WPA_CIPHER_CCMP;
704 s->export_keys = 1;
705 if (ssid->passphrase) {
706 os_free(s->passphrase);
707 s->passphrase = os_strdup(ssid->passphrase);
708 }
709 if (ssid->psk_set) {
710 s->psk_set = 1;
711 os_memcpy(s->psk, ssid->psk, 32);
712 }
713 if (s->passphrase && !s->psk_set)
714 wpa_config_update_psk(s);
715 if (s->ssid == NULL || s->ssid_len < ssid->ssid_len) {
716 os_free(s->ssid);
717 s->ssid = os_malloc(ssid->ssid_len);
718 }
719 if (s->ssid) {
720 s->ssid_len = ssid->ssid_len;
721 os_memcpy(s->ssid, ssid->ssid, s->ssid_len);
722 }
Dmitry Shmidt391c59f2013-09-03 12:16:28 -0700723 if (ssid->mode == WPAS_MODE_P2P_GO && wpa_s->global->add_psk) {
724 dl_list_add(&s->psk_list, &wpa_s->global->add_psk->list);
725 wpa_s->global->add_psk = NULL;
726 changed = 1;
727 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700728
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700729 if (changed && wpa_s->conf->update_config &&
730 wpa_config_write(wpa_s->confname, wpa_s->conf)) {
731 wpa_printf(MSG_DEBUG, "P2P: Failed to update configuration");
732 }
Jouni Malinen75ecf522011-06-27 15:19:46 -0700733
734 return s->id;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700735}
736
737
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800738static void wpas_p2p_add_persistent_group_client(struct wpa_supplicant *wpa_s,
739 const u8 *addr)
740{
741 struct wpa_ssid *ssid, *s;
742 u8 *n;
743 size_t i;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700744 int found = 0;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800745
746 ssid = wpa_s->current_ssid;
747 if (ssid == NULL || ssid->mode != WPAS_MODE_P2P_GO ||
748 !ssid->p2p_persistent_group)
749 return;
750
751 for (s = wpa_s->parent->conf->ssid; s; s = s->next) {
752 if (s->disabled != 2 || s->mode != WPAS_MODE_P2P_GO)
753 continue;
754
755 if (s->ssid_len == ssid->ssid_len &&
756 os_memcmp(s->ssid, ssid->ssid, s->ssid_len) == 0)
757 break;
758 }
759
760 if (s == NULL)
761 return;
762
763 for (i = 0; s->p2p_client_list && i < s->num_p2p_clients; i++) {
764 if (os_memcmp(s->p2p_client_list + i * ETH_ALEN, addr,
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700765 ETH_ALEN) != 0)
766 continue;
767
768 if (i == s->num_p2p_clients - 1)
769 return; /* already the most recent entry */
770
771 /* move the entry to mark it most recent */
772 os_memmove(s->p2p_client_list + i * ETH_ALEN,
773 s->p2p_client_list + (i + 1) * ETH_ALEN,
774 (s->num_p2p_clients - i - 1) * ETH_ALEN);
775 os_memcpy(s->p2p_client_list +
776 (s->num_p2p_clients - 1) * ETH_ALEN, addr, ETH_ALEN);
777 found = 1;
778 break;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800779 }
780
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700781 if (!found && s->num_p2p_clients < P2P_MAX_STORED_CLIENTS) {
782 n = os_realloc_array(s->p2p_client_list,
783 s->num_p2p_clients + 1, ETH_ALEN);
784 if (n == NULL)
785 return;
786 os_memcpy(n + s->num_p2p_clients * ETH_ALEN, addr, ETH_ALEN);
787 s->p2p_client_list = n;
788 s->num_p2p_clients++;
Dmitry Shmidt6aa8ae42014-07-07 09:31:33 -0700789 } else if (!found && s->p2p_client_list) {
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700790 /* Not enough room for an additional entry - drop the oldest
791 * entry */
792 os_memmove(s->p2p_client_list,
793 s->p2p_client_list + ETH_ALEN,
794 (s->num_p2p_clients - 1) * ETH_ALEN);
795 os_memcpy(s->p2p_client_list +
796 (s->num_p2p_clients - 1) * ETH_ALEN,
797 addr, ETH_ALEN);
798 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800799
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800800 if (wpa_s->parent->conf->update_config &&
801 wpa_config_write(wpa_s->parent->confname, wpa_s->parent->conf))
802 wpa_printf(MSG_DEBUG, "P2P: Failed to update configuration");
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800803}
804
805
Dmitry Shmidt7f0b69e2014-07-28 10:35:20 -0700806static void wpas_p2p_group_started(struct wpa_supplicant *wpa_s,
807 int go, struct wpa_ssid *ssid, int freq,
808 const u8 *psk, const char *passphrase,
809 const u8 *go_dev_addr, int persistent,
810 const char *extra)
811{
812 const char *ssid_txt;
813 char psk_txt[65];
814
815 if (psk)
816 wpa_snprintf_hex(psk_txt, sizeof(psk_txt), psk, 32);
817 else
818 psk_txt[0] = '\0';
819
820 if (ssid)
821 ssid_txt = wpa_ssid_txt(ssid->ssid, ssid->ssid_len);
822 else
823 ssid_txt = "";
824
825 if (passphrase && passphrase[0] == '\0')
826 passphrase = NULL;
827
828 /*
829 * Include PSK/passphrase only in the control interface message and
830 * leave it out from the debug log entry.
831 */
832 wpa_msg_global_ctrl(wpa_s->parent, MSG_INFO,
833 P2P_EVENT_GROUP_STARTED
834 "%s %s ssid=\"%s\" freq=%d%s%s%s%s%s go_dev_addr="
835 MACSTR "%s%s",
836 wpa_s->ifname, go ? "GO" : "client", ssid_txt, freq,
837 psk ? " psk=" : "", psk_txt,
838 passphrase ? " passphrase=\"" : "",
839 passphrase ? passphrase : "",
840 passphrase ? "\"" : "",
841 MAC2STR(go_dev_addr),
842 persistent ? " [PERSISTENT]" : "", extra);
843 wpa_printf(MSG_INFO, P2P_EVENT_GROUP_STARTED
844 "%s %s ssid=\"%s\" freq=%d go_dev_addr=" MACSTR "%s%s",
845 wpa_s->ifname, go ? "GO" : "client", ssid_txt, freq,
846 MAC2STR(go_dev_addr), persistent ? " [PERSISTENT]" : "",
847 extra);
848}
849
850
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700851static void wpas_group_formation_completed(struct wpa_supplicant *wpa_s,
852 int success)
853{
854 struct wpa_ssid *ssid;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700855 int client;
856 int persistent;
857 u8 go_dev_addr[ETH_ALEN];
Jouni Malinen75ecf522011-06-27 15:19:46 -0700858 int network_id = -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700859
860 /*
861 * This callback is likely called for the main interface. Update wpa_s
862 * to use the group interface if a new interface was created for the
863 * group.
864 */
865 if (wpa_s->global->p2p_group_formation)
866 wpa_s = wpa_s->global->p2p_group_formation;
Dmitry Shmidt56052862013-10-04 10:23:25 -0700867 if (wpa_s->p2p_go_group_formation_completed) {
868 wpa_s->global->p2p_group_formation = NULL;
869 wpa_s->p2p_in_provisioning = 0;
870 }
Dmitry Shmidt21de2142014-04-08 10:50:52 -0700871 wpa_s->p2p_in_invitation = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700872
873 if (!success) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -0700874 wpa_msg_global(wpa_s->parent, MSG_INFO,
875 P2P_EVENT_GROUP_FORMATION_FAILURE);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700876 wpas_p2p_group_delete(wpa_s,
877 P2P_GROUP_REMOVAL_FORMATION_FAILED);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700878 return;
879 }
880
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -0700881 wpa_msg_global(wpa_s->parent, MSG_INFO,
882 P2P_EVENT_GROUP_FORMATION_SUCCESS);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700883
884 ssid = wpa_s->current_ssid;
885 if (ssid && ssid->mode == WPAS_MODE_P2P_GROUP_FORMATION) {
886 ssid->mode = WPAS_MODE_P2P_GO;
887 p2p_group_notif_formation_done(wpa_s->p2p_group);
888 wpa_supplicant_ap_mac_addr_filter(wpa_s, NULL);
889 }
890
891 persistent = 0;
892 if (ssid) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700893 client = ssid->mode == WPAS_MODE_INFRA;
894 if (ssid->mode == WPAS_MODE_P2P_GO) {
895 persistent = ssid->p2p_persistent_group;
Dmitry Shmidt497c1d52011-07-21 15:19:46 -0700896 os_memcpy(go_dev_addr, wpa_s->global->p2p_dev_addr,
897 ETH_ALEN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700898 } else
899 persistent = wpas_p2p_persistent_group(wpa_s,
900 go_dev_addr,
901 ssid->ssid,
902 ssid->ssid_len);
903 } else {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700904 client = wpa_s->p2p_group_interface ==
905 P2P_GROUP_INTERFACE_CLIENT;
906 os_memset(go_dev_addr, 0, ETH_ALEN);
907 }
908
909 wpa_s->show_group_started = 0;
910 if (client) {
911 /*
912 * Indicate event only after successfully completed 4-way
913 * handshake, i.e., when the interface is ready for data
914 * packets.
915 */
916 wpa_s->show_group_started = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700917 } else {
Dmitry Shmidt7f0b69e2014-07-28 10:35:20 -0700918 wpas_p2p_group_started(wpa_s, 1, ssid,
919 ssid ? ssid->frequency : 0,
920 ssid && ssid->passphrase == NULL &&
921 ssid->psk_set ? ssid->psk : NULL,
922 ssid ? ssid->passphrase : NULL,
923 go_dev_addr, persistent, "");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700924 wpas_p2p_cross_connect_setup(wpa_s);
925 wpas_p2p_set_group_idle_timeout(wpa_s);
926 }
927
928 if (persistent)
Jouni Malinen75ecf522011-06-27 15:19:46 -0700929 network_id = wpas_p2p_store_persistent_group(wpa_s->parent,
930 ssid, go_dev_addr);
Dmitry Shmidt391c59f2013-09-03 12:16:28 -0700931 else {
932 os_free(wpa_s->global->add_psk);
933 wpa_s->global->add_psk = NULL;
934 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800935 if (network_id < 0 && ssid)
Jouni Malinen75ecf522011-06-27 15:19:46 -0700936 network_id = ssid->id;
Dmitry Shmidt92c368d2013-08-29 12:37:21 -0700937 if (!client) {
Jouni Malinen75ecf522011-06-27 15:19:46 -0700938 wpas_notify_p2p_group_started(wpa_s, ssid, network_id, 0);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800939 os_get_reltime(&wpa_s->global->p2p_go_wait_client);
Dmitry Shmidt92c368d2013-08-29 12:37:21 -0700940 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700941}
942
943
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800944struct send_action_work {
945 unsigned int freq;
946 u8 dst[ETH_ALEN];
947 u8 src[ETH_ALEN];
948 u8 bssid[ETH_ALEN];
949 size_t len;
950 unsigned int wait_time;
951 u8 buf[0];
952};
953
954
955static void wpas_p2p_send_action_work_timeout(void *eloop_ctx,
956 void *timeout_ctx)
957{
958 struct wpa_supplicant *wpa_s = eloop_ctx;
959
960 if (!wpa_s->p2p_send_action_work)
961 return;
962
963 wpa_printf(MSG_DEBUG, "P2P: Send Action frame radio work timed out");
964 os_free(wpa_s->p2p_send_action_work->ctx);
965 radio_work_done(wpa_s->p2p_send_action_work);
966 wpa_s->p2p_send_action_work = NULL;
967}
968
969
Dmitry Shmidt03658832014-08-13 11:03:49 -0700970static void wpas_p2p_action_tx_clear(struct wpa_supplicant *wpa_s)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700971{
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800972 if (wpa_s->p2p_send_action_work) {
973 struct send_action_work *awork;
974 awork = wpa_s->p2p_send_action_work->ctx;
975 if (awork->wait_time == 0) {
976 os_free(awork);
977 radio_work_done(wpa_s->p2p_send_action_work);
978 wpa_s->p2p_send_action_work = NULL;
979 } else {
980 /*
981 * In theory, this should not be needed, but number of
982 * places in the P2P code is still using non-zero wait
983 * time for the last Action frame in the sequence and
984 * some of these do not call send_action_done().
985 */
986 eloop_cancel_timeout(wpas_p2p_send_action_work_timeout,
987 wpa_s, NULL);
988 eloop_register_timeout(
989 0, awork->wait_time * 1000,
990 wpas_p2p_send_action_work_timeout,
991 wpa_s, NULL);
992 }
993 }
Dmitry Shmidt03658832014-08-13 11:03:49 -0700994}
995
996
997static void wpas_p2p_send_action_tx_status(struct wpa_supplicant *wpa_s,
998 unsigned int freq,
999 const u8 *dst, const u8 *src,
1000 const u8 *bssid,
1001 const u8 *data, size_t data_len,
1002 enum offchannel_send_action_result
1003 result)
1004{
1005 enum p2p_send_action_result res = P2P_SEND_ACTION_SUCCESS;
1006
1007 wpas_p2p_action_tx_clear(wpa_s);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001008
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001009 if (wpa_s->global->p2p == NULL || wpa_s->global->p2p_disabled)
1010 return;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001011
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001012 switch (result) {
1013 case OFFCHANNEL_SEND_ACTION_SUCCESS:
1014 res = P2P_SEND_ACTION_SUCCESS;
1015 break;
1016 case OFFCHANNEL_SEND_ACTION_NO_ACK:
1017 res = P2P_SEND_ACTION_NO_ACK;
1018 break;
1019 case OFFCHANNEL_SEND_ACTION_FAILED:
1020 res = P2P_SEND_ACTION_FAILED;
1021 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001022 }
1023
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001024 p2p_send_action_cb(wpa_s->global->p2p, freq, dst, src, bssid, res);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001025
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08001026 if (result != OFFCHANNEL_SEND_ACTION_SUCCESS &&
1027 wpa_s->pending_pd_before_join &&
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001028 (os_memcmp(dst, wpa_s->pending_join_dev_addr, ETH_ALEN) == 0 ||
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001029 os_memcmp(dst, wpa_s->pending_join_iface_addr, ETH_ALEN) == 0) &&
1030 wpa_s->p2p_fallback_to_go_neg) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001031 wpa_s->pending_pd_before_join = 0;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001032 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: No ACK for PD Req "
1033 "during p2p_connect-auto");
1034 wpas_p2p_fallback_to_go_neg(wpa_s, 0);
1035 return;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001036 }
1037}
1038
1039
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001040static void wpas_send_action_cb(struct wpa_radio_work *work, int deinit)
1041{
1042 struct wpa_supplicant *wpa_s = work->wpa_s;
1043 struct send_action_work *awork = work->ctx;
1044
1045 if (deinit) {
Dmitry Shmidtbd14a572014-02-18 10:33:49 -08001046 if (work->started) {
1047 eloop_cancel_timeout(wpas_p2p_send_action_work_timeout,
1048 wpa_s, NULL);
1049 wpa_s->p2p_send_action_work = NULL;
1050 offchannel_send_action_done(wpa_s);
1051 }
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001052 os_free(awork);
1053 return;
1054 }
1055
1056 if (offchannel_send_action(wpa_s, awork->freq, awork->dst, awork->src,
1057 awork->bssid, awork->buf, awork->len,
1058 awork->wait_time,
1059 wpas_p2p_send_action_tx_status, 1) < 0) {
1060 os_free(awork);
1061 radio_work_done(work);
1062 return;
1063 }
1064 wpa_s->p2p_send_action_work = work;
1065}
1066
1067
1068static int wpas_send_action_work(struct wpa_supplicant *wpa_s,
1069 unsigned int freq, const u8 *dst,
1070 const u8 *src, const u8 *bssid, const u8 *buf,
1071 size_t len, unsigned int wait_time)
1072{
1073 struct send_action_work *awork;
1074
1075 if (wpa_s->p2p_send_action_work) {
1076 wpa_printf(MSG_DEBUG, "P2P: Cannot schedule new p2p-send-action work since one is already pending");
1077 return -1;
1078 }
1079
1080 awork = os_zalloc(sizeof(*awork) + len);
1081 if (awork == NULL)
1082 return -1;
1083
1084 awork->freq = freq;
1085 os_memcpy(awork->dst, dst, ETH_ALEN);
1086 os_memcpy(awork->src, src, ETH_ALEN);
1087 os_memcpy(awork->bssid, bssid, ETH_ALEN);
1088 awork->len = len;
1089 awork->wait_time = wait_time;
1090 os_memcpy(awork->buf, buf, len);
1091
1092 if (radio_add_work(wpa_s, freq, "p2p-send-action", 0,
1093 wpas_send_action_cb, awork) < 0) {
1094 os_free(awork);
1095 return -1;
1096 }
1097
1098 return 0;
1099}
1100
1101
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001102static int wpas_send_action(void *ctx, unsigned int freq, const u8 *dst,
1103 const u8 *src, const u8 *bssid, const u8 *buf,
1104 size_t len, unsigned int wait_time)
1105{
1106 struct wpa_supplicant *wpa_s = ctx;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001107 int listen_freq = -1, send_freq = -1;
1108
1109 if (wpa_s->p2p_listen_work)
1110 listen_freq = wpa_s->p2p_listen_work->freq;
1111 if (wpa_s->p2p_send_action_work)
1112 send_freq = wpa_s->p2p_send_action_work->freq;
1113 if (listen_freq != (int) freq && send_freq != (int) freq) {
1114 wpa_printf(MSG_DEBUG, "P2P: Schedule new radio work for Action frame TX (listen_freq=%d send_freq=%d)",
1115 listen_freq, send_freq);
1116 return wpas_send_action_work(wpa_s, freq, dst, src, bssid, buf,
1117 len, wait_time);
1118 }
1119
1120 wpa_printf(MSG_DEBUG, "P2P: Use ongoing radio work for Action frame TX");
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001121 return offchannel_send_action(wpa_s, freq, dst, src, bssid, buf, len,
1122 wait_time,
1123 wpas_p2p_send_action_tx_status, 1);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001124}
1125
1126
1127static void wpas_send_action_done(void *ctx)
1128{
1129 struct wpa_supplicant *wpa_s = ctx;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001130
1131 if (wpa_s->p2p_send_action_work) {
1132 eloop_cancel_timeout(wpas_p2p_send_action_work_timeout,
1133 wpa_s, NULL);
1134 os_free(wpa_s->p2p_send_action_work->ctx);
1135 radio_work_done(wpa_s->p2p_send_action_work);
1136 wpa_s->p2p_send_action_work = NULL;
1137 }
1138
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001139 offchannel_send_action_done(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001140}
1141
1142
1143static int wpas_copy_go_neg_results(struct wpa_supplicant *wpa_s,
1144 struct p2p_go_neg_results *params)
1145{
1146 if (wpa_s->go_params == NULL) {
1147 wpa_s->go_params = os_malloc(sizeof(*params));
1148 if (wpa_s->go_params == NULL)
1149 return -1;
1150 }
1151 os_memcpy(wpa_s->go_params, params, sizeof(*params));
1152 return 0;
1153}
1154
1155
1156static void wpas_start_wps_enrollee(struct wpa_supplicant *wpa_s,
1157 struct p2p_go_neg_results *res)
1158{
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001159 wpa_printf(MSG_DEBUG, "P2P: Start WPS Enrollee for peer " MACSTR
1160 " dev_addr " MACSTR " wps_method %d",
1161 MAC2STR(res->peer_interface_addr),
1162 MAC2STR(res->peer_device_addr), res->wps_method);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001163 wpa_hexdump_ascii(MSG_DEBUG, "P2P: Start WPS Enrollee for SSID",
1164 res->ssid, res->ssid_len);
1165 wpa_supplicant_ap_deinit(wpa_s);
1166 wpas_copy_go_neg_results(wpa_s, res);
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001167 if (res->wps_method == WPS_PBC) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001168 wpas_wps_start_pbc(wpa_s, res->peer_interface_addr, 1);
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001169#ifdef CONFIG_WPS_NFC
1170 } else if (res->wps_method == WPS_NFC) {
1171 wpas_wps_start_nfc(wpa_s, res->peer_device_addr,
1172 res->peer_interface_addr,
1173 wpa_s->parent->p2p_oob_dev_pw,
1174 wpa_s->parent->p2p_oob_dev_pw_id, 1,
1175 wpa_s->parent->p2p_oob_dev_pw_id ==
1176 DEV_PW_NFC_CONNECTION_HANDOVER ?
1177 wpa_s->parent->p2p_peer_oob_pubkey_hash :
1178 NULL,
1179 NULL, 0, 0);
1180#endif /* CONFIG_WPS_NFC */
1181 } else {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001182 u16 dev_pw_id = DEV_PW_DEFAULT;
1183 if (wpa_s->p2p_wps_method == WPS_PIN_KEYPAD)
1184 dev_pw_id = DEV_PW_REGISTRAR_SPECIFIED;
1185 wpas_wps_start_pin(wpa_s, res->peer_interface_addr,
1186 wpa_s->p2p_pin, 1, dev_pw_id);
1187 }
1188}
1189
1190
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07001191static void wpas_p2p_add_psk_list(struct wpa_supplicant *wpa_s,
1192 struct wpa_ssid *ssid)
1193{
1194 struct wpa_ssid *persistent;
1195 struct psk_list_entry *psk;
1196 struct hostapd_data *hapd;
1197
1198 if (!wpa_s->ap_iface)
1199 return;
1200
1201 persistent = wpas_p2p_get_persistent(wpa_s->parent, NULL, ssid->ssid,
1202 ssid->ssid_len);
1203 if (persistent == NULL)
1204 return;
1205
1206 hapd = wpa_s->ap_iface->bss[0];
1207
1208 dl_list_for_each(psk, &persistent->psk_list, struct psk_list_entry,
1209 list) {
1210 struct hostapd_wpa_psk *hpsk;
1211
1212 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Add persistent group PSK entry for "
1213 MACSTR " psk=%d",
1214 MAC2STR(psk->addr), psk->p2p);
1215 hpsk = os_zalloc(sizeof(*hpsk));
1216 if (hpsk == NULL)
1217 break;
1218 os_memcpy(hpsk->psk, psk->psk, PMK_LEN);
1219 if (psk->p2p)
1220 os_memcpy(hpsk->p2p_dev_addr, psk->addr, ETH_ALEN);
1221 else
1222 os_memcpy(hpsk->addr, psk->addr, ETH_ALEN);
1223 hpsk->next = hapd->conf->ssid.wpa_psk;
1224 hapd->conf->ssid.wpa_psk = hpsk;
1225 }
1226}
1227
1228
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001229static void p2p_go_configured(void *ctx, void *data)
1230{
1231 struct wpa_supplicant *wpa_s = ctx;
1232 struct p2p_go_neg_results *params = data;
1233 struct wpa_ssid *ssid;
Jouni Malinen75ecf522011-06-27 15:19:46 -07001234 int network_id = -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001235
1236 ssid = wpa_s->current_ssid;
1237 if (ssid && ssid->mode == WPAS_MODE_P2P_GO) {
1238 wpa_printf(MSG_DEBUG, "P2P: Group setup without provisioning");
1239 if (wpa_s->global->p2p_group_formation == wpa_s)
1240 wpa_s->global->p2p_group_formation = NULL;
Dmitry Shmidt7f0b69e2014-07-28 10:35:20 -07001241 wpas_p2p_group_started(wpa_s, 1, ssid, ssid->frequency,
1242 params->passphrase[0] == '\0' ?
1243 params->psk : NULL,
1244 params->passphrase,
1245 wpa_s->global->p2p_dev_addr,
1246 params->persistent_group, "");
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001247
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001248 os_get_reltime(&wpa_s->global->p2p_go_wait_client);
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07001249 if (params->persistent_group) {
Jouni Malinen75ecf522011-06-27 15:19:46 -07001250 network_id = wpas_p2p_store_persistent_group(
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001251 wpa_s->parent, ssid,
Dmitry Shmidt497c1d52011-07-21 15:19:46 -07001252 wpa_s->global->p2p_dev_addr);
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07001253 wpas_p2p_add_psk_list(wpa_s, ssid);
1254 }
Jouni Malinen75ecf522011-06-27 15:19:46 -07001255 if (network_id < 0)
1256 network_id = ssid->id;
1257 wpas_notify_p2p_group_started(wpa_s, ssid, network_id, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001258 wpas_p2p_cross_connect_setup(wpa_s);
1259 wpas_p2p_set_group_idle_timeout(wpa_s);
Dmitry Shmidt56052862013-10-04 10:23:25 -07001260
1261 if (wpa_s->p2p_first_connection_timeout) {
1262 wpa_dbg(wpa_s, MSG_DEBUG,
1263 "P2P: Start group formation timeout of %d seconds until first data connection on GO",
1264 wpa_s->p2p_first_connection_timeout);
1265 wpa_s->p2p_go_group_formation_completed = 0;
1266 wpa_s->global->p2p_group_formation = wpa_s;
1267 eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
1268 wpa_s->parent, NULL);
1269 eloop_register_timeout(
1270 wpa_s->p2p_first_connection_timeout, 0,
1271 wpas_p2p_group_formation_timeout,
1272 wpa_s->parent, NULL);
1273 }
1274
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001275 return;
1276 }
1277
1278 wpa_printf(MSG_DEBUG, "P2P: Setting up WPS for GO provisioning");
1279 if (wpa_supplicant_ap_mac_addr_filter(wpa_s,
1280 params->peer_interface_addr)) {
1281 wpa_printf(MSG_DEBUG, "P2P: Failed to setup MAC address "
1282 "filtering");
1283 return;
1284 }
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001285 if (params->wps_method == WPS_PBC) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001286 wpa_supplicant_ap_wps_pbc(wpa_s, params->peer_interface_addr,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001287 params->peer_device_addr);
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001288#ifdef CONFIG_WPS_NFC
1289 } else if (params->wps_method == WPS_NFC) {
1290 if (wpa_s->parent->p2p_oob_dev_pw_id !=
1291 DEV_PW_NFC_CONNECTION_HANDOVER &&
1292 !wpa_s->parent->p2p_oob_dev_pw) {
1293 wpa_printf(MSG_DEBUG, "P2P: No NFC Dev Pw known");
1294 return;
1295 }
1296 wpas_ap_wps_add_nfc_pw(
1297 wpa_s, wpa_s->parent->p2p_oob_dev_pw_id,
1298 wpa_s->parent->p2p_oob_dev_pw,
1299 wpa_s->parent->p2p_peer_oob_pk_hash_known ?
1300 wpa_s->parent->p2p_peer_oob_pubkey_hash : NULL);
1301#endif /* CONFIG_WPS_NFC */
1302 } else if (wpa_s->p2p_pin[0])
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001303 wpa_supplicant_ap_wps_pin(wpa_s, params->peer_interface_addr,
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001304 wpa_s->p2p_pin, NULL, 0, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001305 os_free(wpa_s->go_params);
1306 wpa_s->go_params = NULL;
1307}
1308
1309
1310static void wpas_start_wps_go(struct wpa_supplicant *wpa_s,
1311 struct p2p_go_neg_results *params,
1312 int group_formation)
1313{
1314 struct wpa_ssid *ssid;
1315
Dmitry Shmidtaa532512012-09-24 10:35:31 -07001316 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Starting GO");
1317 if (wpas_copy_go_neg_results(wpa_s, params) < 0) {
1318 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Could not copy GO Negotiation "
1319 "results");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001320 return;
Dmitry Shmidtaa532512012-09-24 10:35:31 -07001321 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001322
1323 ssid = wpa_config_add_network(wpa_s->conf);
Dmitry Shmidtaa532512012-09-24 10:35:31 -07001324 if (ssid == NULL) {
1325 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Could not add network for GO");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001326 return;
Dmitry Shmidtaa532512012-09-24 10:35:31 -07001327 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001328
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001329 wpa_s->show_group_started = 0;
1330
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001331 wpa_config_set_network_defaults(ssid);
1332 ssid->temporary = 1;
1333 ssid->p2p_group = 1;
1334 ssid->p2p_persistent_group = params->persistent_group;
1335 ssid->mode = group_formation ? WPAS_MODE_P2P_GROUP_FORMATION :
1336 WPAS_MODE_P2P_GO;
1337 ssid->frequency = params->freq;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001338 ssid->ht40 = params->ht40;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07001339 ssid->vht = params->vht;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001340 ssid->ssid = os_zalloc(params->ssid_len + 1);
1341 if (ssid->ssid) {
1342 os_memcpy(ssid->ssid, params->ssid, params->ssid_len);
1343 ssid->ssid_len = params->ssid_len;
1344 }
1345 ssid->auth_alg = WPA_AUTH_ALG_OPEN;
1346 ssid->key_mgmt = WPA_KEY_MGMT_PSK;
1347 ssid->proto = WPA_PROTO_RSN;
1348 ssid->pairwise_cipher = WPA_CIPHER_CCMP;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001349 if (os_strlen(params->passphrase) > 0) {
1350 ssid->passphrase = os_strdup(params->passphrase);
1351 if (ssid->passphrase == NULL) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07001352 wpa_msg_global(wpa_s, MSG_ERROR,
1353 "P2P: Failed to copy passphrase for GO");
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001354 wpa_config_remove_network(wpa_s->conf, ssid->id);
1355 return;
1356 }
1357 } else
1358 ssid->passphrase = NULL;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001359 ssid->psk_set = params->psk_set;
1360 if (ssid->psk_set)
1361 os_memcpy(ssid->psk, params->psk, sizeof(ssid->psk));
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001362 else if (ssid->passphrase)
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001363 wpa_config_update_psk(ssid);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001364 ssid->ap_max_inactivity = wpa_s->parent->conf->p2p_go_max_inactivity;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001365
1366 wpa_s->ap_configured_cb = p2p_go_configured;
1367 wpa_s->ap_configured_cb_ctx = wpa_s;
1368 wpa_s->ap_configured_cb_data = wpa_s->go_params;
Dmitry Shmidt6dc03bd2014-05-16 10:40:13 -07001369 wpa_s->scan_req = NORMAL_SCAN_REQ;
Jouni Malinen75ecf522011-06-27 15:19:46 -07001370 wpa_s->connect_without_scan = ssid;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001371 wpa_s->reassociate = 1;
1372 wpa_s->disconnected = 0;
Dmitry Shmidtaa532512012-09-24 10:35:31 -07001373 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Request scan (that will be skipped) to "
1374 "start GO)");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001375 wpa_supplicant_req_scan(wpa_s, 0, 0);
1376}
1377
1378
1379static void wpas_p2p_clone_config(struct wpa_supplicant *dst,
1380 const struct wpa_supplicant *src)
1381{
1382 struct wpa_config *d;
1383 const struct wpa_config *s;
1384
1385 d = dst->conf;
1386 s = src->conf;
1387
1388#define C(n) if (s->n) d->n = os_strdup(s->n)
1389 C(device_name);
1390 C(manufacturer);
1391 C(model_name);
1392 C(model_number);
1393 C(serial_number);
1394 C(config_methods);
1395#undef C
1396
1397 os_memcpy(d->device_type, s->device_type, WPS_DEV_TYPE_LEN);
1398 os_memcpy(d->sec_device_type, s->sec_device_type,
1399 sizeof(d->sec_device_type));
1400 d->num_sec_device_types = s->num_sec_device_types;
1401
1402 d->p2p_group_idle = s->p2p_group_idle;
1403 d->p2p_intra_bss = s->p2p_intra_bss;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001404 d->persistent_reconnect = s->persistent_reconnect;
Dmitry Shmidt04949592012-07-19 12:16:46 -07001405 d->max_num_sta = s->max_num_sta;
1406 d->pbc_in_m1 = s->pbc_in_m1;
Dmitry Shmidt444d5672013-04-01 13:08:44 -07001407 d->ignore_old_scan_res = s->ignore_old_scan_res;
Dmitry Shmidt51b6ea82013-05-08 10:42:09 -07001408 d->beacon_int = s->beacon_int;
Dmitry Shmidt18463232014-01-24 12:29:41 -08001409 d->dtim_period = s->dtim_period;
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07001410 d->disassoc_low_ack = s->disassoc_low_ack;
Dmitry Shmidt96571392013-10-14 12:54:46 -07001411 d->disable_scan_offload = s->disable_scan_offload;
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001412
1413 if (s->wps_nfc_dh_privkey && s->wps_nfc_dh_pubkey) {
1414 d->wps_nfc_dh_privkey = wpabuf_dup(s->wps_nfc_dh_privkey);
1415 d->wps_nfc_dh_pubkey = wpabuf_dup(s->wps_nfc_dh_pubkey);
1416 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001417}
1418
1419
Dmitry Shmidt34af3062013-07-11 10:46:32 -07001420static void wpas_p2p_get_group_ifname(struct wpa_supplicant *wpa_s,
1421 char *ifname, size_t len)
1422{
1423 char *ifname_ptr = wpa_s->ifname;
1424
1425 if (os_strncmp(wpa_s->ifname, P2P_MGMT_DEVICE_PREFIX,
1426 os_strlen(P2P_MGMT_DEVICE_PREFIX)) == 0) {
1427 ifname_ptr = os_strrchr(wpa_s->ifname, '-') + 1;
1428 }
1429
1430 os_snprintf(ifname, len, "p2p-%s-%d", ifname_ptr, wpa_s->p2p_group_idx);
1431 if (os_strlen(ifname) >= IFNAMSIZ &&
1432 os_strlen(wpa_s->ifname) < IFNAMSIZ) {
1433 /* Try to avoid going over the IFNAMSIZ length limit */
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07001434 os_snprintf(ifname, len, "p2p-%d", wpa_s->p2p_group_idx);
Dmitry Shmidt34af3062013-07-11 10:46:32 -07001435 }
1436}
1437
1438
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001439static int wpas_p2p_add_group_interface(struct wpa_supplicant *wpa_s,
1440 enum wpa_driver_if_type type)
1441{
1442 char ifname[120], force_ifname[120];
1443
1444 if (wpa_s->pending_interface_name[0]) {
1445 wpa_printf(MSG_DEBUG, "P2P: Pending virtual interface exists "
1446 "- skip creation of a new one");
1447 if (is_zero_ether_addr(wpa_s->pending_interface_addr)) {
1448 wpa_printf(MSG_DEBUG, "P2P: Pending virtual address "
1449 "unknown?! ifname='%s'",
1450 wpa_s->pending_interface_name);
1451 return -1;
1452 }
1453 return 0;
1454 }
1455
Dmitry Shmidt34af3062013-07-11 10:46:32 -07001456 wpas_p2p_get_group_ifname(wpa_s, ifname, sizeof(ifname));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001457 force_ifname[0] = '\0';
1458
1459 wpa_printf(MSG_DEBUG, "P2P: Create a new interface %s for the group",
1460 ifname);
1461 wpa_s->p2p_group_idx++;
1462
1463 wpa_s->pending_interface_type = type;
1464 if (wpa_drv_if_add(wpa_s, type, ifname, NULL, NULL, force_ifname,
1465 wpa_s->pending_interface_addr, NULL) < 0) {
1466 wpa_printf(MSG_ERROR, "P2P: Failed to create new group "
1467 "interface");
1468 return -1;
1469 }
1470
1471 if (force_ifname[0]) {
1472 wpa_printf(MSG_DEBUG, "P2P: Driver forced interface name %s",
1473 force_ifname);
1474 os_strlcpy(wpa_s->pending_interface_name, force_ifname,
1475 sizeof(wpa_s->pending_interface_name));
1476 } else
1477 os_strlcpy(wpa_s->pending_interface_name, ifname,
1478 sizeof(wpa_s->pending_interface_name));
1479 wpa_printf(MSG_DEBUG, "P2P: Created pending virtual interface %s addr "
1480 MACSTR, wpa_s->pending_interface_name,
1481 MAC2STR(wpa_s->pending_interface_addr));
1482
1483 return 0;
1484}
1485
1486
1487static void wpas_p2p_remove_pending_group_interface(
1488 struct wpa_supplicant *wpa_s)
1489{
1490 if (!wpa_s->pending_interface_name[0] ||
1491 is_zero_ether_addr(wpa_s->pending_interface_addr))
1492 return; /* No pending virtual interface */
1493
1494 wpa_printf(MSG_DEBUG, "P2P: Removing pending group interface %s",
1495 wpa_s->pending_interface_name);
1496 wpa_drv_if_remove(wpa_s, wpa_s->pending_interface_type,
1497 wpa_s->pending_interface_name);
1498 os_memset(wpa_s->pending_interface_addr, 0, ETH_ALEN);
1499 wpa_s->pending_interface_name[0] = '\0';
1500}
1501
1502
1503static struct wpa_supplicant *
1504wpas_p2p_init_group_interface(struct wpa_supplicant *wpa_s, int go)
1505{
1506 struct wpa_interface iface;
1507 struct wpa_supplicant *group_wpa_s;
1508
1509 if (!wpa_s->pending_interface_name[0]) {
1510 wpa_printf(MSG_ERROR, "P2P: No pending group interface");
1511 if (!wpas_p2p_create_iface(wpa_s))
1512 return NULL;
1513 /*
1514 * Something has forced us to remove the pending interface; try
1515 * to create a new one and hope for the best that we will get
1516 * the same local address.
1517 */
1518 if (wpas_p2p_add_group_interface(wpa_s, go ? WPA_IF_P2P_GO :
1519 WPA_IF_P2P_CLIENT) < 0)
1520 return NULL;
1521 }
1522
1523 os_memset(&iface, 0, sizeof(iface));
1524 iface.ifname = wpa_s->pending_interface_name;
1525 iface.driver = wpa_s->driver->name;
Dmitry Shmidt34af3062013-07-11 10:46:32 -07001526 if (wpa_s->conf->ctrl_interface == NULL &&
1527 wpa_s->parent != wpa_s &&
1528 wpa_s->p2p_mgmt &&
1529 (wpa_s->drv_flags & WPA_DRIVER_FLAGS_DEDICATED_P2P_DEVICE))
1530 iface.ctrl_interface = wpa_s->parent->conf->ctrl_interface;
1531 else
1532 iface.ctrl_interface = wpa_s->conf->ctrl_interface;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001533 iface.driver_param = wpa_s->conf->driver_param;
1534 group_wpa_s = wpa_supplicant_add_iface(wpa_s->global, &iface);
1535 if (group_wpa_s == NULL) {
1536 wpa_printf(MSG_ERROR, "P2P: Failed to create new "
1537 "wpa_supplicant interface");
1538 return NULL;
1539 }
1540 wpa_s->pending_interface_name[0] = '\0';
1541 group_wpa_s->parent = wpa_s;
1542 group_wpa_s->p2p_group_interface = go ? P2P_GROUP_INTERFACE_GO :
1543 P2P_GROUP_INTERFACE_CLIENT;
1544 wpa_s->global->p2p_group_formation = group_wpa_s;
1545
1546 wpas_p2p_clone_config(group_wpa_s, wpa_s);
1547
1548 return group_wpa_s;
1549}
1550
1551
1552static void wpas_p2p_group_formation_timeout(void *eloop_ctx,
1553 void *timeout_ctx)
1554{
1555 struct wpa_supplicant *wpa_s = eloop_ctx;
1556 wpa_printf(MSG_DEBUG, "P2P: Group Formation timed out");
Dmitry Shmidt34af3062013-07-11 10:46:32 -07001557 wpas_p2p_group_formation_failed(wpa_s);
1558}
1559
1560
1561void wpas_p2p_group_formation_failed(struct wpa_supplicant *wpa_s)
1562{
1563 eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
1564 wpa_s->parent, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001565 if (wpa_s->global->p2p)
1566 p2p_group_formation_failed(wpa_s->global->p2p);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001567 wpas_group_formation_completed(wpa_s, 0);
1568}
1569
1570
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001571static void wpas_p2p_grpform_fail_after_wps(struct wpa_supplicant *wpa_s)
1572{
1573 wpa_printf(MSG_DEBUG, "P2P: Reject group formation due to WPS provisioning failure");
1574 eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
1575 wpa_s->parent, NULL);
1576 eloop_register_timeout(0, 0, wpas_p2p_group_formation_timeout,
1577 wpa_s->parent, NULL);
1578 wpa_s->global->p2p_fail_on_wps_complete = 0;
1579}
1580
1581
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07001582void wpas_p2p_ap_setup_failed(struct wpa_supplicant *wpa_s)
1583{
1584 if (wpa_s->global->p2p_group_formation != wpa_s)
1585 return;
1586 /* Speed up group formation timeout since this cannot succeed */
1587 eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
1588 wpa_s->parent, NULL);
1589 eloop_register_timeout(0, 0, wpas_p2p_group_formation_timeout,
1590 wpa_s->parent, NULL);
1591}
1592
1593
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001594static void wpas_go_neg_completed(void *ctx, struct p2p_go_neg_results *res)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001595{
1596 struct wpa_supplicant *wpa_s = ctx;
1597
1598 if (wpa_s->off_channel_freq || wpa_s->roc_waiting_drv_freq) {
1599 wpa_drv_cancel_remain_on_channel(wpa_s);
1600 wpa_s->off_channel_freq = 0;
1601 wpa_s->roc_waiting_drv_freq = 0;
1602 }
1603
1604 if (res->status) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07001605 wpa_msg_global(wpa_s, MSG_INFO,
1606 P2P_EVENT_GO_NEG_FAILURE "status=%d",
1607 res->status);
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 wpas_p2p_remove_pending_group_interface(wpa_s);
1610 return;
1611 }
1612
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001613 if (wpa_s->p2p_go_ht40)
1614 res->ht40 = 1;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07001615 if (wpa_s->p2p_go_vht)
1616 res->vht = 1;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001617
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07001618 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_GO_NEG_SUCCESS "role=%s "
1619 "freq=%d ht40=%d peer_dev=" MACSTR " peer_iface=" MACSTR
1620 " wps_method=%s",
1621 res->role_go ? "GO" : "client", res->freq, res->ht40,
1622 MAC2STR(res->peer_device_addr),
1623 MAC2STR(res->peer_interface_addr),
1624 p2p_wps_method_text(res->wps_method));
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001625 wpas_notify_p2p_go_neg_completed(wpa_s, res);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001626
Dmitry Shmidt04949592012-07-19 12:16:46 -07001627 if (res->role_go && wpa_s->p2p_persistent_id >= 0) {
1628 struct wpa_ssid *ssid;
1629 ssid = wpa_config_get_network(wpa_s->conf,
1630 wpa_s->p2p_persistent_id);
1631 if (ssid && ssid->disabled == 2 &&
1632 ssid->mode == WPAS_MODE_P2P_GO && ssid->passphrase) {
1633 size_t len = os_strlen(ssid->passphrase);
1634 wpa_printf(MSG_DEBUG, "P2P: Override passphrase based "
1635 "on requested persistent group");
1636 os_memcpy(res->passphrase, ssid->passphrase, len);
1637 res->passphrase[len] = '\0';
1638 }
1639 }
1640
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001641 if (wpa_s->create_p2p_iface) {
1642 struct wpa_supplicant *group_wpa_s =
1643 wpas_p2p_init_group_interface(wpa_s, res->role_go);
1644 if (group_wpa_s == NULL) {
1645 wpas_p2p_remove_pending_group_interface(wpa_s);
Dmitry Shmidtd11f0192014-03-24 12:09:47 -07001646 eloop_cancel_timeout(wpas_p2p_long_listen_timeout,
1647 wpa_s, NULL);
1648 wpas_p2p_group_formation_failed(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001649 return;
1650 }
1651 if (group_wpa_s != wpa_s) {
1652 os_memcpy(group_wpa_s->p2p_pin, wpa_s->p2p_pin,
1653 sizeof(group_wpa_s->p2p_pin));
1654 group_wpa_s->p2p_wps_method = wpa_s->p2p_wps_method;
1655 }
1656 os_memset(wpa_s->pending_interface_addr, 0, ETH_ALEN);
1657 wpa_s->pending_interface_name[0] = '\0';
1658 group_wpa_s->p2p_in_provisioning = 1;
1659
1660 if (res->role_go)
1661 wpas_start_wps_go(group_wpa_s, res, 1);
1662 else
1663 wpas_start_wps_enrollee(group_wpa_s, res);
1664 } else {
1665 wpa_s->p2p_in_provisioning = 1;
1666 wpa_s->global->p2p_group_formation = wpa_s;
1667
1668 if (res->role_go)
1669 wpas_start_wps_go(wpa_s, res, 1);
1670 else
1671 wpas_start_wps_enrollee(ctx, res);
1672 }
1673
1674 wpa_s->p2p_long_listen = 0;
1675 eloop_cancel_timeout(wpas_p2p_long_listen_timeout, wpa_s, NULL);
1676
1677 eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s, NULL);
1678 eloop_register_timeout(15 + res->peer_config_timeout / 100,
1679 (res->peer_config_timeout % 100) * 10000,
1680 wpas_p2p_group_formation_timeout, wpa_s, NULL);
1681}
1682
1683
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001684static void wpas_go_neg_req_rx(void *ctx, const u8 *src, u16 dev_passwd_id)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001685{
1686 struct wpa_supplicant *wpa_s = ctx;
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07001687 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_GO_NEG_REQUEST MACSTR
1688 " dev_passwd_id=%u", MAC2STR(src), dev_passwd_id);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001689
1690 wpas_notify_p2p_go_neg_req(wpa_s, src, dev_passwd_id);
1691}
1692
1693
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001694static void wpas_dev_found(void *ctx, const u8 *addr,
1695 const struct p2p_peer_info *info,
1696 int new_device)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001697{
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001698#ifndef CONFIG_NO_STDOUT_DEBUG
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001699 struct wpa_supplicant *wpa_s = ctx;
1700 char devtype[WPS_DEV_TYPE_BUFSIZE];
Dmitry Shmidt04f534e2013-12-09 15:50:16 -08001701 char *wfd_dev_info_hex = NULL;
1702
Irfan Sheriff8367dc92012-09-09 17:08:19 -07001703#ifdef CONFIG_WIFI_DISPLAY
Dmitry Shmidt04f534e2013-12-09 15:50:16 -08001704 wfd_dev_info_hex = wifi_display_subelem_hex(info->wfd_subelems,
1705 WFD_SUBELEM_DEVICE_INFO);
Irfan Sheriff8367dc92012-09-09 17:08:19 -07001706#endif /* CONFIG_WIFI_DISPLAY */
Dmitry Shmidt04f534e2013-12-09 15:50:16 -08001707
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07001708 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_DEVICE_FOUND MACSTR
1709 " p2p_dev_addr=" MACSTR
1710 " pri_dev_type=%s name='%s' config_methods=0x%x "
Dmitry Shmidt2e67f062014-07-16 09:55:28 -07001711 "dev_capab=0x%x group_capab=0x%x%s%s%s",
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07001712 MAC2STR(addr), MAC2STR(info->p2p_device_addr),
1713 wps_dev_type_bin2str(info->pri_dev_type, devtype,
1714 sizeof(devtype)),
1715 info->device_name, info->config_methods,
1716 info->dev_capab, info->group_capab,
Dmitry Shmidt04f534e2013-12-09 15:50:16 -08001717 wfd_dev_info_hex ? " wfd_dev_info=0x" : "",
Dmitry Shmidt2e67f062014-07-16 09:55:28 -07001718 wfd_dev_info_hex ? wfd_dev_info_hex : "",
1719 info->vendor_elems ? " vendor_elems=1" : "");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001720
Dmitry Shmidt04f534e2013-12-09 15:50:16 -08001721 os_free(wfd_dev_info_hex);
Dmitry Shmidt97672262014-02-03 13:02:54 -08001722#endif /* CONFIG_NO_STDOUT_DEBUG */
Dmitry Shmidt04f534e2013-12-09 15:50:16 -08001723
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001724 wpas_notify_p2p_device_found(ctx, info->p2p_device_addr, new_device);
1725}
1726
1727
1728static void wpas_dev_lost(void *ctx, const u8 *dev_addr)
1729{
1730 struct wpa_supplicant *wpa_s = ctx;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001731
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07001732 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_DEVICE_LOST
1733 "p2p_dev_addr=" MACSTR, MAC2STR(dev_addr));
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001734
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001735 wpas_notify_p2p_device_lost(wpa_s, dev_addr);
1736}
1737
1738
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07001739static void wpas_find_stopped(void *ctx)
1740{
1741 struct wpa_supplicant *wpa_s = ctx;
1742 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_FIND_STOPPED);
1743}
1744
1745
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001746struct wpas_p2p_listen_work {
1747 unsigned int freq;
1748 unsigned int duration;
1749 struct wpabuf *probe_resp_ie;
1750};
1751
1752
1753static void wpas_p2p_listen_work_free(struct wpas_p2p_listen_work *lwork)
1754{
1755 if (lwork == NULL)
1756 return;
1757 wpabuf_free(lwork->probe_resp_ie);
1758 os_free(lwork);
1759}
1760
1761
1762static void wpas_p2p_listen_work_done(struct wpa_supplicant *wpa_s)
1763{
1764 struct wpas_p2p_listen_work *lwork;
1765
1766 if (!wpa_s->p2p_listen_work)
1767 return;
1768
1769 lwork = wpa_s->p2p_listen_work->ctx;
1770 wpas_p2p_listen_work_free(lwork);
1771 radio_work_done(wpa_s->p2p_listen_work);
1772 wpa_s->p2p_listen_work = NULL;
1773}
1774
1775
1776static void wpas_start_listen_cb(struct wpa_radio_work *work, int deinit)
1777{
1778 struct wpa_supplicant *wpa_s = work->wpa_s;
1779 struct wpas_p2p_listen_work *lwork = work->ctx;
1780
1781 if (deinit) {
Dmitry Shmidtbd14a572014-02-18 10:33:49 -08001782 if (work->started) {
1783 wpa_s->p2p_listen_work = NULL;
1784 wpas_stop_listen(wpa_s);
1785 }
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001786 wpas_p2p_listen_work_free(lwork);
1787 return;
1788 }
1789
1790 wpa_s->p2p_listen_work = work;
1791
1792 wpa_drv_set_ap_wps_ie(wpa_s, NULL, lwork->probe_resp_ie, NULL);
1793
1794 if (wpa_drv_probe_req_report(wpa_s, 1) < 0) {
1795 wpa_printf(MSG_DEBUG, "P2P: Failed to request the driver to "
1796 "report received Probe Request frames");
1797 wpas_p2p_listen_work_done(wpa_s);
1798 return;
1799 }
1800
1801 wpa_s->pending_listen_freq = lwork->freq;
1802 wpa_s->pending_listen_duration = lwork->duration;
1803
1804 if (wpa_drv_remain_on_channel(wpa_s, lwork->freq, lwork->duration) < 0)
1805 {
1806 wpa_printf(MSG_DEBUG, "P2P: Failed to request the driver "
1807 "to remain on channel (%u MHz) for Listen "
1808 "state", lwork->freq);
1809 wpas_p2p_listen_work_done(wpa_s);
1810 wpa_s->pending_listen_freq = 0;
1811 return;
1812 }
1813 wpa_s->off_channel_freq = 0;
1814 wpa_s->roc_waiting_drv_freq = lwork->freq;
1815}
1816
1817
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001818static int wpas_start_listen(void *ctx, unsigned int freq,
1819 unsigned int duration,
1820 const struct wpabuf *probe_resp_ie)
1821{
1822 struct wpa_supplicant *wpa_s = ctx;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001823 struct wpas_p2p_listen_work *lwork;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001824
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001825 if (wpa_s->p2p_listen_work) {
1826 wpa_printf(MSG_DEBUG, "P2P: Reject start_listen since p2p_listen_work already exists");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001827 return -1;
1828 }
1829
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001830 lwork = os_zalloc(sizeof(*lwork));
1831 if (lwork == NULL)
1832 return -1;
1833 lwork->freq = freq;
1834 lwork->duration = duration;
1835 if (probe_resp_ie) {
1836 lwork->probe_resp_ie = wpabuf_dup(probe_resp_ie);
1837 if (lwork->probe_resp_ie == NULL) {
1838 wpas_p2p_listen_work_free(lwork);
1839 return -1;
1840 }
1841 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001842
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001843 if (radio_add_work(wpa_s, freq, "p2p-listen", 0, wpas_start_listen_cb,
1844 lwork) < 0) {
1845 wpas_p2p_listen_work_free(lwork);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001846 return -1;
1847 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001848
1849 return 0;
1850}
1851
1852
1853static void wpas_stop_listen(void *ctx)
1854{
1855 struct wpa_supplicant *wpa_s = ctx;
1856 if (wpa_s->off_channel_freq || wpa_s->roc_waiting_drv_freq) {
1857 wpa_drv_cancel_remain_on_channel(wpa_s);
1858 wpa_s->off_channel_freq = 0;
1859 wpa_s->roc_waiting_drv_freq = 0;
1860 }
1861 wpa_drv_set_ap_wps_ie(wpa_s, NULL, NULL, NULL);
1862 wpa_drv_probe_req_report(wpa_s, 0);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001863 wpas_p2p_listen_work_done(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001864}
1865
1866
1867static int wpas_send_probe_resp(void *ctx, const struct wpabuf *buf)
1868{
1869 struct wpa_supplicant *wpa_s = ctx;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001870 return wpa_drv_send_mlme(wpa_s, wpabuf_head(buf), wpabuf_len(buf), 1);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001871}
1872
1873
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001874/*
1875 * DNS Header section is used only to calculate compression pointers, so the
1876 * contents of this data does not matter, but the length needs to be reserved
1877 * in the virtual packet.
1878 */
1879#define DNS_HEADER_LEN 12
1880
1881/*
1882 * 27-octet in-memory packet from P2P specification containing two implied
1883 * queries for _tcp.lcoal. PTR IN and _udp.local. PTR IN
1884 */
1885#define P2P_SD_IN_MEMORY_LEN 27
1886
1887static int p2p_sd_dns_uncompress_label(char **upos, char *uend, u8 *start,
1888 u8 **spos, const u8 *end)
1889{
1890 while (*spos < end) {
1891 u8 val = ((*spos)[0] & 0xc0) >> 6;
1892 int len;
1893
1894 if (val == 1 || val == 2) {
1895 /* These are reserved values in RFC 1035 */
1896 wpa_printf(MSG_DEBUG, "P2P: Invalid domain name "
1897 "sequence starting with 0x%x", val);
1898 return -1;
1899 }
1900
1901 if (val == 3) {
1902 u16 offset;
1903 u8 *spos_tmp;
1904
1905 /* Offset */
1906 if (*spos + 2 > end) {
1907 wpa_printf(MSG_DEBUG, "P2P: No room for full "
1908 "DNS offset field");
1909 return -1;
1910 }
1911
1912 offset = (((*spos)[0] & 0x3f) << 8) | (*spos)[1];
1913 if (offset >= *spos - start) {
1914 wpa_printf(MSG_DEBUG, "P2P: Invalid DNS "
1915 "pointer offset %u", offset);
1916 return -1;
1917 }
1918
1919 (*spos) += 2;
1920 spos_tmp = start + offset;
1921 return p2p_sd_dns_uncompress_label(upos, uend, start,
1922 &spos_tmp,
1923 *spos - 2);
1924 }
1925
1926 /* Label */
1927 len = (*spos)[0] & 0x3f;
1928 if (len == 0)
1929 return 0;
1930
1931 (*spos)++;
1932 if (*spos + len > end) {
1933 wpa_printf(MSG_DEBUG, "P2P: Invalid domain name "
1934 "sequence - no room for label with length "
1935 "%u", len);
1936 return -1;
1937 }
1938
1939 if (*upos + len + 2 > uend)
1940 return -2;
1941
1942 os_memcpy(*upos, *spos, len);
1943 *spos += len;
1944 *upos += len;
1945 (*upos)[0] = '.';
1946 (*upos)++;
1947 (*upos)[0] = '\0';
1948 }
1949
1950 return 0;
1951}
1952
1953
1954/* Uncompress domain names per RFC 1035 using the P2P SD in-memory packet.
1955 * Returns -1 on parsing error (invalid input sequence), -2 if output buffer is
1956 * not large enough */
1957static int p2p_sd_dns_uncompress(char *buf, size_t buf_len, const u8 *msg,
1958 size_t msg_len, size_t offset)
1959{
1960 /* 27-octet in-memory packet from P2P specification */
1961 const char *prefix = "\x04_tcp\x05local\x00\x00\x0C\x00\x01"
1962 "\x04_udp\xC0\x11\x00\x0C\x00\x01";
1963 u8 *tmp, *end, *spos;
1964 char *upos, *uend;
1965 int ret = 0;
1966
1967 if (buf_len < 2)
1968 return -1;
1969 if (offset > msg_len)
1970 return -1;
1971
1972 tmp = os_malloc(DNS_HEADER_LEN + P2P_SD_IN_MEMORY_LEN + msg_len);
1973 if (tmp == NULL)
1974 return -1;
1975 spos = tmp + DNS_HEADER_LEN + P2P_SD_IN_MEMORY_LEN;
1976 end = spos + msg_len;
1977 spos += offset;
1978
1979 os_memset(tmp, 0, DNS_HEADER_LEN);
1980 os_memcpy(tmp + DNS_HEADER_LEN, prefix, P2P_SD_IN_MEMORY_LEN);
1981 os_memcpy(tmp + DNS_HEADER_LEN + P2P_SD_IN_MEMORY_LEN, msg, msg_len);
1982
1983 upos = buf;
1984 uend = buf + buf_len;
1985
1986 ret = p2p_sd_dns_uncompress_label(&upos, uend, tmp, &spos, end);
1987 if (ret) {
1988 os_free(tmp);
1989 return ret;
1990 }
1991
1992 if (upos == buf) {
1993 upos[0] = '.';
1994 upos[1] = '\0';
1995 } else if (upos[-1] == '.')
1996 upos[-1] = '\0';
1997
1998 os_free(tmp);
1999 return 0;
2000}
2001
2002
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002003static struct p2p_srv_bonjour *
2004wpas_p2p_service_get_bonjour(struct wpa_supplicant *wpa_s,
2005 const struct wpabuf *query)
2006{
2007 struct p2p_srv_bonjour *bsrv;
2008 size_t len;
2009
2010 len = wpabuf_len(query);
2011 dl_list_for_each(bsrv, &wpa_s->global->p2p_srv_bonjour,
2012 struct p2p_srv_bonjour, list) {
2013 if (len == wpabuf_len(bsrv->query) &&
2014 os_memcmp(wpabuf_head(query), wpabuf_head(bsrv->query),
2015 len) == 0)
2016 return bsrv;
2017 }
2018 return NULL;
2019}
2020
2021
2022static struct p2p_srv_upnp *
2023wpas_p2p_service_get_upnp(struct wpa_supplicant *wpa_s, u8 version,
2024 const char *service)
2025{
2026 struct p2p_srv_upnp *usrv;
2027
2028 dl_list_for_each(usrv, &wpa_s->global->p2p_srv_upnp,
2029 struct p2p_srv_upnp, list) {
2030 if (version == usrv->version &&
2031 os_strcmp(service, usrv->service) == 0)
2032 return usrv;
2033 }
2034 return NULL;
2035}
2036
2037
2038static void wpas_sd_add_proto_not_avail(struct wpabuf *resp, u8 srv_proto,
2039 u8 srv_trans_id)
2040{
2041 u8 *len_pos;
2042
2043 if (wpabuf_tailroom(resp) < 5)
2044 return;
2045
2046 /* Length (to be filled) */
2047 len_pos = wpabuf_put(resp, 2);
2048 wpabuf_put_u8(resp, srv_proto);
2049 wpabuf_put_u8(resp, srv_trans_id);
2050 /* Status Code */
2051 wpabuf_put_u8(resp, P2P_SD_PROTO_NOT_AVAILABLE);
2052 /* Response Data: empty */
2053 WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos - 2);
2054}
2055
2056
2057static void wpas_sd_all_bonjour(struct wpa_supplicant *wpa_s,
2058 struct wpabuf *resp, u8 srv_trans_id)
2059{
2060 struct p2p_srv_bonjour *bsrv;
2061 u8 *len_pos;
2062
2063 wpa_printf(MSG_DEBUG, "P2P: SD Request for all Bonjour services");
2064
2065 if (dl_list_empty(&wpa_s->global->p2p_srv_bonjour)) {
2066 wpa_printf(MSG_DEBUG, "P2P: Bonjour protocol not available");
2067 return;
2068 }
2069
2070 dl_list_for_each(bsrv, &wpa_s->global->p2p_srv_bonjour,
2071 struct p2p_srv_bonjour, list) {
2072 if (wpabuf_tailroom(resp) <
2073 5 + wpabuf_len(bsrv->query) + wpabuf_len(bsrv->resp))
2074 return;
2075 /* Length (to be filled) */
2076 len_pos = wpabuf_put(resp, 2);
2077 wpabuf_put_u8(resp, P2P_SERV_BONJOUR);
2078 wpabuf_put_u8(resp, srv_trans_id);
2079 /* Status Code */
2080 wpabuf_put_u8(resp, P2P_SD_SUCCESS);
2081 wpa_hexdump_ascii(MSG_DEBUG, "P2P: Matching Bonjour service",
2082 wpabuf_head(bsrv->resp),
2083 wpabuf_len(bsrv->resp));
2084 /* Response Data */
2085 wpabuf_put_buf(resp, bsrv->query); /* Key */
2086 wpabuf_put_buf(resp, bsrv->resp); /* Value */
2087 WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos -
2088 2);
2089 }
2090}
2091
2092
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002093static int match_bonjour_query(struct p2p_srv_bonjour *bsrv, const u8 *query,
2094 size_t query_len)
2095{
2096 char str_rx[256], str_srv[256];
2097
2098 if (query_len < 3 || wpabuf_len(bsrv->query) < 3)
2099 return 0; /* Too short to include DNS Type and Version */
2100 if (os_memcmp(query + query_len - 3,
2101 wpabuf_head_u8(bsrv->query) + wpabuf_len(bsrv->query) - 3,
2102 3) != 0)
2103 return 0; /* Mismatch in DNS Type or Version */
2104 if (query_len == wpabuf_len(bsrv->query) &&
2105 os_memcmp(query, wpabuf_head(bsrv->query), query_len - 3) == 0)
2106 return 1; /* Binary match */
2107
2108 if (p2p_sd_dns_uncompress(str_rx, sizeof(str_rx), query, query_len - 3,
2109 0))
2110 return 0; /* Failed to uncompress query */
2111 if (p2p_sd_dns_uncompress(str_srv, sizeof(str_srv),
2112 wpabuf_head(bsrv->query),
2113 wpabuf_len(bsrv->query) - 3, 0))
2114 return 0; /* Failed to uncompress service */
2115
2116 return os_strcmp(str_rx, str_srv) == 0;
2117}
2118
2119
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002120static void wpas_sd_req_bonjour(struct wpa_supplicant *wpa_s,
2121 struct wpabuf *resp, u8 srv_trans_id,
2122 const u8 *query, size_t query_len)
2123{
2124 struct p2p_srv_bonjour *bsrv;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002125 u8 *len_pos;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002126 int matches = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002127
2128 wpa_hexdump_ascii(MSG_DEBUG, "P2P: SD Request for Bonjour",
2129 query, query_len);
2130 if (dl_list_empty(&wpa_s->global->p2p_srv_bonjour)) {
2131 wpa_printf(MSG_DEBUG, "P2P: Bonjour protocol not available");
2132 wpas_sd_add_proto_not_avail(resp, P2P_SERV_BONJOUR,
2133 srv_trans_id);
2134 return;
2135 }
2136
2137 if (query_len == 0) {
2138 wpas_sd_all_bonjour(wpa_s, resp, srv_trans_id);
2139 return;
2140 }
2141
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002142 dl_list_for_each(bsrv, &wpa_s->global->p2p_srv_bonjour,
2143 struct p2p_srv_bonjour, list) {
2144 if (!match_bonjour_query(bsrv, query, query_len))
2145 continue;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002146
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002147 if (wpabuf_tailroom(resp) <
2148 5 + query_len + wpabuf_len(bsrv->resp))
2149 return;
2150
2151 matches++;
2152
2153 /* Length (to be filled) */
2154 len_pos = wpabuf_put(resp, 2);
2155 wpabuf_put_u8(resp, P2P_SERV_BONJOUR);
2156 wpabuf_put_u8(resp, srv_trans_id);
2157
2158 /* Status Code */
2159 wpabuf_put_u8(resp, P2P_SD_SUCCESS);
2160 wpa_hexdump_ascii(MSG_DEBUG, "P2P: Matching Bonjour service",
2161 wpabuf_head(bsrv->resp),
2162 wpabuf_len(bsrv->resp));
2163
2164 /* Response Data */
2165 wpabuf_put_data(resp, query, query_len); /* Key */
2166 wpabuf_put_buf(resp, bsrv->resp); /* Value */
2167
2168 WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos - 2);
2169 }
2170
2171 if (matches == 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002172 wpa_printf(MSG_DEBUG, "P2P: Requested Bonjour service not "
2173 "available");
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002174 if (wpabuf_tailroom(resp) < 5)
2175 return;
2176
2177 /* Length (to be filled) */
2178 len_pos = wpabuf_put(resp, 2);
2179 wpabuf_put_u8(resp, P2P_SERV_BONJOUR);
2180 wpabuf_put_u8(resp, srv_trans_id);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002181
2182 /* Status Code */
2183 wpabuf_put_u8(resp, P2P_SD_REQUESTED_INFO_NOT_AVAILABLE);
2184 /* Response Data: empty */
2185 WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos -
2186 2);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002187 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002188}
2189
2190
2191static void wpas_sd_all_upnp(struct wpa_supplicant *wpa_s,
2192 struct wpabuf *resp, u8 srv_trans_id)
2193{
2194 struct p2p_srv_upnp *usrv;
2195 u8 *len_pos;
2196
2197 wpa_printf(MSG_DEBUG, "P2P: SD Request for all UPnP services");
2198
2199 if (dl_list_empty(&wpa_s->global->p2p_srv_upnp)) {
2200 wpa_printf(MSG_DEBUG, "P2P: UPnP protocol not available");
2201 return;
2202 }
2203
2204 dl_list_for_each(usrv, &wpa_s->global->p2p_srv_upnp,
2205 struct p2p_srv_upnp, list) {
2206 if (wpabuf_tailroom(resp) < 5 + 1 + os_strlen(usrv->service))
2207 return;
2208
2209 /* Length (to be filled) */
2210 len_pos = wpabuf_put(resp, 2);
2211 wpabuf_put_u8(resp, P2P_SERV_UPNP);
2212 wpabuf_put_u8(resp, srv_trans_id);
2213
2214 /* Status Code */
2215 wpabuf_put_u8(resp, P2P_SD_SUCCESS);
2216 /* Response Data */
2217 wpabuf_put_u8(resp, usrv->version);
2218 wpa_printf(MSG_DEBUG, "P2P: Matching UPnP Service: %s",
2219 usrv->service);
2220 wpabuf_put_str(resp, usrv->service);
2221 WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos -
2222 2);
2223 }
2224}
2225
2226
2227static void wpas_sd_req_upnp(struct wpa_supplicant *wpa_s,
2228 struct wpabuf *resp, u8 srv_trans_id,
2229 const u8 *query, size_t query_len)
2230{
2231 struct p2p_srv_upnp *usrv;
2232 u8 *len_pos;
2233 u8 version;
2234 char *str;
2235 int count = 0;
2236
2237 wpa_hexdump_ascii(MSG_DEBUG, "P2P: SD Request for UPnP",
2238 query, query_len);
2239
2240 if (dl_list_empty(&wpa_s->global->p2p_srv_upnp)) {
2241 wpa_printf(MSG_DEBUG, "P2P: UPnP protocol not available");
2242 wpas_sd_add_proto_not_avail(resp, P2P_SERV_UPNP,
2243 srv_trans_id);
2244 return;
2245 }
2246
2247 if (query_len == 0) {
2248 wpas_sd_all_upnp(wpa_s, resp, srv_trans_id);
2249 return;
2250 }
2251
2252 if (wpabuf_tailroom(resp) < 5)
2253 return;
2254
2255 /* Length (to be filled) */
2256 len_pos = wpabuf_put(resp, 2);
2257 wpabuf_put_u8(resp, P2P_SERV_UPNP);
2258 wpabuf_put_u8(resp, srv_trans_id);
2259
2260 version = query[0];
2261 str = os_malloc(query_len);
2262 if (str == NULL)
2263 return;
2264 os_memcpy(str, query + 1, query_len - 1);
2265 str[query_len - 1] = '\0';
2266
2267 dl_list_for_each(usrv, &wpa_s->global->p2p_srv_upnp,
2268 struct p2p_srv_upnp, list) {
2269 if (version != usrv->version)
2270 continue;
2271
2272 if (os_strcmp(str, "ssdp:all") != 0 &&
2273 os_strstr(usrv->service, str) == NULL)
2274 continue;
2275
2276 if (wpabuf_tailroom(resp) < 2)
2277 break;
2278 if (count == 0) {
2279 /* Status Code */
2280 wpabuf_put_u8(resp, P2P_SD_SUCCESS);
2281 /* Response Data */
2282 wpabuf_put_u8(resp, version);
2283 } else
2284 wpabuf_put_u8(resp, ',');
2285
2286 count++;
2287
2288 wpa_printf(MSG_DEBUG, "P2P: Matching UPnP Service: %s",
2289 usrv->service);
2290 if (wpabuf_tailroom(resp) < os_strlen(usrv->service))
2291 break;
2292 wpabuf_put_str(resp, usrv->service);
2293 }
2294 os_free(str);
2295
2296 if (count == 0) {
2297 wpa_printf(MSG_DEBUG, "P2P: Requested UPnP service not "
2298 "available");
2299 /* Status Code */
2300 wpabuf_put_u8(resp, P2P_SD_REQUESTED_INFO_NOT_AVAILABLE);
2301 /* Response Data: empty */
2302 }
2303
2304 WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos - 2);
2305}
2306
2307
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002308#ifdef CONFIG_WIFI_DISPLAY
2309static void wpas_sd_req_wfd(struct wpa_supplicant *wpa_s,
2310 struct wpabuf *resp, u8 srv_trans_id,
2311 const u8 *query, size_t query_len)
2312{
2313 const u8 *pos;
2314 u8 role;
2315 u8 *len_pos;
2316
2317 wpa_hexdump(MSG_DEBUG, "P2P: SD Request for WFD", query, query_len);
2318
2319 if (!wpa_s->global->wifi_display) {
2320 wpa_printf(MSG_DEBUG, "P2P: WFD protocol not available");
2321 wpas_sd_add_proto_not_avail(resp, P2P_SERV_WIFI_DISPLAY,
2322 srv_trans_id);
2323 return;
2324 }
2325
2326 if (query_len < 1) {
2327 wpa_printf(MSG_DEBUG, "P2P: Missing WFD Requested Device "
2328 "Role");
2329 return;
2330 }
2331
2332 if (wpabuf_tailroom(resp) < 5)
2333 return;
2334
2335 pos = query;
2336 role = *pos++;
2337 wpa_printf(MSG_DEBUG, "P2P: WSD for device role 0x%x", role);
2338
2339 /* TODO: role specific handling */
2340
2341 /* Length (to be filled) */
2342 len_pos = wpabuf_put(resp, 2);
2343 wpabuf_put_u8(resp, P2P_SERV_WIFI_DISPLAY);
2344 wpabuf_put_u8(resp, srv_trans_id);
2345 wpabuf_put_u8(resp, P2P_SD_SUCCESS); /* Status Code */
2346
2347 while (pos < query + query_len) {
2348 if (*pos < MAX_WFD_SUBELEMS &&
2349 wpa_s->global->wfd_subelem[*pos] &&
2350 wpabuf_tailroom(resp) >=
2351 wpabuf_len(wpa_s->global->wfd_subelem[*pos])) {
2352 wpa_printf(MSG_DEBUG, "P2P: Add WSD response "
2353 "subelement %u", *pos);
2354 wpabuf_put_buf(resp, wpa_s->global->wfd_subelem[*pos]);
2355 }
2356 pos++;
2357 }
2358
2359 WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos - 2);
2360}
2361#endif /* CONFIG_WIFI_DISPLAY */
2362
2363
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002364static void wpas_sd_request(void *ctx, int freq, const u8 *sa, u8 dialog_token,
2365 u16 update_indic, const u8 *tlvs, size_t tlvs_len)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002366{
2367 struct wpa_supplicant *wpa_s = ctx;
2368 const u8 *pos = tlvs;
2369 const u8 *end = tlvs + tlvs_len;
2370 const u8 *tlv_end;
2371 u16 slen;
2372 struct wpabuf *resp;
2373 u8 srv_proto, srv_trans_id;
2374 size_t buf_len;
2375 char *buf;
2376
2377 wpa_hexdump(MSG_MSGDUMP, "P2P: Service Discovery Request TLVs",
2378 tlvs, tlvs_len);
2379 buf_len = 2 * tlvs_len + 1;
2380 buf = os_malloc(buf_len);
2381 if (buf) {
2382 wpa_snprintf_hex(buf, buf_len, tlvs, tlvs_len);
2383 wpa_msg_ctrl(wpa_s, MSG_INFO, P2P_EVENT_SERV_DISC_REQ "%d "
2384 MACSTR " %u %u %s",
2385 freq, MAC2STR(sa), dialog_token, update_indic,
2386 buf);
2387 os_free(buf);
2388 }
2389
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002390 if (wpa_s->p2p_sd_over_ctrl_iface) {
2391 wpas_notify_p2p_sd_request(wpa_s, freq, sa, dialog_token,
2392 update_indic, tlvs, tlvs_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002393 return; /* to be processed by an external program */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002394 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002395
2396 resp = wpabuf_alloc(10000);
2397 if (resp == NULL)
2398 return;
2399
2400 while (pos + 1 < end) {
2401 wpa_printf(MSG_DEBUG, "P2P: Service Request TLV");
2402 slen = WPA_GET_LE16(pos);
2403 pos += 2;
2404 if (pos + slen > end || slen < 2) {
2405 wpa_printf(MSG_DEBUG, "P2P: Unexpected Query Data "
2406 "length");
2407 wpabuf_free(resp);
2408 return;
2409 }
2410 tlv_end = pos + slen;
2411
2412 srv_proto = *pos++;
2413 wpa_printf(MSG_DEBUG, "P2P: Service Protocol Type %u",
2414 srv_proto);
2415 srv_trans_id = *pos++;
2416 wpa_printf(MSG_DEBUG, "P2P: Service Transaction ID %u",
2417 srv_trans_id);
2418
2419 wpa_hexdump(MSG_MSGDUMP, "P2P: Query Data",
2420 pos, tlv_end - pos);
2421
2422
2423 if (wpa_s->force_long_sd) {
2424 wpa_printf(MSG_DEBUG, "P2P: SD test - force long "
2425 "response");
2426 wpas_sd_all_bonjour(wpa_s, resp, srv_trans_id);
2427 wpas_sd_all_upnp(wpa_s, resp, srv_trans_id);
2428 goto done;
2429 }
2430
2431 switch (srv_proto) {
2432 case P2P_SERV_ALL_SERVICES:
2433 wpa_printf(MSG_DEBUG, "P2P: Service Discovery Request "
2434 "for all services");
2435 if (dl_list_empty(&wpa_s->global->p2p_srv_upnp) &&
2436 dl_list_empty(&wpa_s->global->p2p_srv_bonjour)) {
2437 wpa_printf(MSG_DEBUG, "P2P: No service "
2438 "discovery protocols available");
2439 wpas_sd_add_proto_not_avail(
2440 resp, P2P_SERV_ALL_SERVICES,
2441 srv_trans_id);
2442 break;
2443 }
2444 wpas_sd_all_bonjour(wpa_s, resp, srv_trans_id);
2445 wpas_sd_all_upnp(wpa_s, resp, srv_trans_id);
2446 break;
2447 case P2P_SERV_BONJOUR:
2448 wpas_sd_req_bonjour(wpa_s, resp, srv_trans_id,
2449 pos, tlv_end - pos);
2450 break;
2451 case P2P_SERV_UPNP:
2452 wpas_sd_req_upnp(wpa_s, resp, srv_trans_id,
2453 pos, tlv_end - pos);
2454 break;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002455#ifdef CONFIG_WIFI_DISPLAY
2456 case P2P_SERV_WIFI_DISPLAY:
2457 wpas_sd_req_wfd(wpa_s, resp, srv_trans_id,
2458 pos, tlv_end - pos);
2459 break;
2460#endif /* CONFIG_WIFI_DISPLAY */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002461 default:
2462 wpa_printf(MSG_DEBUG, "P2P: Unavailable service "
2463 "protocol %u", srv_proto);
2464 wpas_sd_add_proto_not_avail(resp, srv_proto,
2465 srv_trans_id);
2466 break;
2467 }
2468
2469 pos = tlv_end;
2470 }
2471
2472done:
2473 wpas_notify_p2p_sd_request(wpa_s, freq, sa, dialog_token,
2474 update_indic, tlvs, tlvs_len);
2475
2476 wpas_p2p_sd_response(wpa_s, freq, sa, dialog_token, resp);
2477
2478 wpabuf_free(resp);
2479}
2480
2481
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002482static void wpas_sd_response(void *ctx, const u8 *sa, u16 update_indic,
2483 const u8 *tlvs, size_t tlvs_len)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002484{
2485 struct wpa_supplicant *wpa_s = ctx;
2486 const u8 *pos = tlvs;
2487 const u8 *end = tlvs + tlvs_len;
2488 const u8 *tlv_end;
2489 u16 slen;
2490 size_t buf_len;
2491 char *buf;
2492
2493 wpa_hexdump(MSG_MSGDUMP, "P2P: Service Discovery Response TLVs",
2494 tlvs, tlvs_len);
2495 if (tlvs_len > 1500) {
2496 /* TODO: better way for handling this */
2497 wpa_msg_ctrl(wpa_s, MSG_INFO,
2498 P2P_EVENT_SERV_DISC_RESP MACSTR
2499 " %u <long response: %u bytes>",
2500 MAC2STR(sa), update_indic,
2501 (unsigned int) tlvs_len);
2502 } else {
2503 buf_len = 2 * tlvs_len + 1;
2504 buf = os_malloc(buf_len);
2505 if (buf) {
2506 wpa_snprintf_hex(buf, buf_len, tlvs, tlvs_len);
2507 wpa_msg_ctrl(wpa_s, MSG_INFO,
2508 P2P_EVENT_SERV_DISC_RESP MACSTR " %u %s",
2509 MAC2STR(sa), update_indic, buf);
2510 os_free(buf);
2511 }
2512 }
2513
2514 while (pos < end) {
2515 u8 srv_proto, srv_trans_id, status;
2516
2517 wpa_printf(MSG_DEBUG, "P2P: Service Response TLV");
2518 slen = WPA_GET_LE16(pos);
2519 pos += 2;
2520 if (pos + slen > end || slen < 3) {
2521 wpa_printf(MSG_DEBUG, "P2P: Unexpected Response Data "
2522 "length");
2523 return;
2524 }
2525 tlv_end = pos + slen;
2526
2527 srv_proto = *pos++;
2528 wpa_printf(MSG_DEBUG, "P2P: Service Protocol Type %u",
2529 srv_proto);
2530 srv_trans_id = *pos++;
2531 wpa_printf(MSG_DEBUG, "P2P: Service Transaction ID %u",
2532 srv_trans_id);
2533 status = *pos++;
2534 wpa_printf(MSG_DEBUG, "P2P: Status Code ID %u",
2535 status);
2536
2537 wpa_hexdump(MSG_MSGDUMP, "P2P: Response Data",
2538 pos, tlv_end - pos);
2539
2540 pos = tlv_end;
2541 }
2542
2543 wpas_notify_p2p_sd_response(wpa_s, sa, update_indic, tlvs, tlvs_len);
2544}
2545
2546
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002547u64 wpas_p2p_sd_request(struct wpa_supplicant *wpa_s, const u8 *dst,
2548 const struct wpabuf *tlvs)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002549{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002550 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002551 return 0;
2552 return (uintptr_t) p2p_sd_request(wpa_s->global->p2p, dst, tlvs);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002553}
2554
2555
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002556u64 wpas_p2p_sd_request_upnp(struct wpa_supplicant *wpa_s, const u8 *dst,
2557 u8 version, const char *query)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002558{
2559 struct wpabuf *tlvs;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002560 u64 ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002561
2562 tlvs = wpabuf_alloc(2 + 1 + 1 + 1 + os_strlen(query));
2563 if (tlvs == NULL)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002564 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002565 wpabuf_put_le16(tlvs, 1 + 1 + 1 + os_strlen(query));
2566 wpabuf_put_u8(tlvs, P2P_SERV_UPNP); /* Service Protocol Type */
2567 wpabuf_put_u8(tlvs, 1); /* Service Transaction ID */
2568 wpabuf_put_u8(tlvs, version);
2569 wpabuf_put_str(tlvs, query);
2570 ret = wpas_p2p_sd_request(wpa_s, dst, tlvs);
2571 wpabuf_free(tlvs);
2572 return ret;
2573}
2574
2575
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002576#ifdef CONFIG_WIFI_DISPLAY
2577
2578static u64 wpas_p2p_sd_request_wfd(struct wpa_supplicant *wpa_s, const u8 *dst,
2579 const struct wpabuf *tlvs)
2580{
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002581 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
2582 return 0;
2583 return (uintptr_t) p2p_sd_request_wfd(wpa_s->global->p2p, dst, tlvs);
2584}
2585
2586
2587#define MAX_WFD_SD_SUBELEMS 20
2588
2589static void wfd_add_sd_req_role(struct wpabuf *tlvs, u8 id, u8 role,
2590 const char *subelems)
2591{
2592 u8 *len;
2593 const char *pos;
2594 int val;
2595 int count = 0;
2596
2597 len = wpabuf_put(tlvs, 2);
2598 wpabuf_put_u8(tlvs, P2P_SERV_WIFI_DISPLAY); /* Service Protocol Type */
2599 wpabuf_put_u8(tlvs, id); /* Service Transaction ID */
2600
2601 wpabuf_put_u8(tlvs, role);
2602
2603 pos = subelems;
2604 while (*pos) {
2605 val = atoi(pos);
2606 if (val >= 0 && val < 256) {
2607 wpabuf_put_u8(tlvs, val);
2608 count++;
2609 if (count == MAX_WFD_SD_SUBELEMS)
2610 break;
2611 }
2612 pos = os_strchr(pos + 1, ',');
2613 if (pos == NULL)
2614 break;
2615 pos++;
2616 }
2617
2618 WPA_PUT_LE16(len, (u8 *) wpabuf_put(tlvs, 0) - len - 2);
2619}
2620
2621
2622u64 wpas_p2p_sd_request_wifi_display(struct wpa_supplicant *wpa_s,
2623 const u8 *dst, const char *role)
2624{
2625 struct wpabuf *tlvs;
2626 u64 ret;
2627 const char *subelems;
2628 u8 id = 1;
2629
2630 subelems = os_strchr(role, ' ');
2631 if (subelems == NULL)
2632 return 0;
2633 subelems++;
2634
2635 tlvs = wpabuf_alloc(4 * (2 + 1 + 1 + 1 + MAX_WFD_SD_SUBELEMS));
2636 if (tlvs == NULL)
2637 return 0;
2638
2639 if (os_strstr(role, "[source]"))
2640 wfd_add_sd_req_role(tlvs, id++, 0x00, subelems);
2641 if (os_strstr(role, "[pri-sink]"))
2642 wfd_add_sd_req_role(tlvs, id++, 0x01, subelems);
2643 if (os_strstr(role, "[sec-sink]"))
2644 wfd_add_sd_req_role(tlvs, id++, 0x02, subelems);
2645 if (os_strstr(role, "[source+sink]"))
2646 wfd_add_sd_req_role(tlvs, id++, 0x03, subelems);
2647
2648 ret = wpas_p2p_sd_request_wfd(wpa_s, dst, tlvs);
2649 wpabuf_free(tlvs);
2650 return ret;
2651}
2652
2653#endif /* CONFIG_WIFI_DISPLAY */
2654
2655
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002656int wpas_p2p_sd_cancel_request(struct wpa_supplicant *wpa_s, u64 req)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002657{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002658 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
2659 return -1;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002660 return p2p_sd_cancel_request(wpa_s->global->p2p,
2661 (void *) (uintptr_t) req);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002662}
2663
2664
2665void wpas_p2p_sd_response(struct wpa_supplicant *wpa_s, int freq,
2666 const u8 *dst, u8 dialog_token,
2667 const struct wpabuf *resp_tlvs)
2668{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002669 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
2670 return;
2671 p2p_sd_response(wpa_s->global->p2p, freq, dst, dialog_token,
2672 resp_tlvs);
2673}
2674
Dmitry Shmidteaf261d2013-08-14 15:30:08 -07002675
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002676void wpas_p2p_sd_service_update(struct wpa_supplicant *wpa_s)
2677{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002678 if (wpa_s->global->p2p)
2679 p2p_sd_service_update(wpa_s->global->p2p);
2680}
2681
2682
2683static void wpas_p2p_srv_bonjour_free(struct p2p_srv_bonjour *bsrv)
2684{
2685 dl_list_del(&bsrv->list);
2686 wpabuf_free(bsrv->query);
2687 wpabuf_free(bsrv->resp);
2688 os_free(bsrv);
2689}
2690
2691
2692static void wpas_p2p_srv_upnp_free(struct p2p_srv_upnp *usrv)
2693{
2694 dl_list_del(&usrv->list);
2695 os_free(usrv->service);
2696 os_free(usrv);
2697}
2698
2699
2700void wpas_p2p_service_flush(struct wpa_supplicant *wpa_s)
2701{
2702 struct p2p_srv_bonjour *bsrv, *bn;
2703 struct p2p_srv_upnp *usrv, *un;
2704
2705 dl_list_for_each_safe(bsrv, bn, &wpa_s->global->p2p_srv_bonjour,
2706 struct p2p_srv_bonjour, list)
2707 wpas_p2p_srv_bonjour_free(bsrv);
2708
2709 dl_list_for_each_safe(usrv, un, &wpa_s->global->p2p_srv_upnp,
2710 struct p2p_srv_upnp, list)
2711 wpas_p2p_srv_upnp_free(usrv);
2712
2713 wpas_p2p_sd_service_update(wpa_s);
2714}
2715
2716
2717int wpas_p2p_service_add_bonjour(struct wpa_supplicant *wpa_s,
2718 struct wpabuf *query, struct wpabuf *resp)
2719{
2720 struct p2p_srv_bonjour *bsrv;
2721
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002722 bsrv = os_zalloc(sizeof(*bsrv));
2723 if (bsrv == NULL)
2724 return -1;
2725 bsrv->query = query;
2726 bsrv->resp = resp;
2727 dl_list_add(&wpa_s->global->p2p_srv_bonjour, &bsrv->list);
2728
2729 wpas_p2p_sd_service_update(wpa_s);
2730 return 0;
2731}
2732
2733
2734int wpas_p2p_service_del_bonjour(struct wpa_supplicant *wpa_s,
2735 const struct wpabuf *query)
2736{
2737 struct p2p_srv_bonjour *bsrv;
2738
2739 bsrv = wpas_p2p_service_get_bonjour(wpa_s, query);
2740 if (bsrv == NULL)
2741 return -1;
2742 wpas_p2p_srv_bonjour_free(bsrv);
2743 wpas_p2p_sd_service_update(wpa_s);
2744 return 0;
2745}
2746
2747
2748int wpas_p2p_service_add_upnp(struct wpa_supplicant *wpa_s, u8 version,
2749 const char *service)
2750{
2751 struct p2p_srv_upnp *usrv;
2752
2753 if (wpas_p2p_service_get_upnp(wpa_s, version, service))
2754 return 0; /* Already listed */
2755 usrv = os_zalloc(sizeof(*usrv));
2756 if (usrv == NULL)
2757 return -1;
2758 usrv->version = version;
2759 usrv->service = os_strdup(service);
2760 if (usrv->service == NULL) {
2761 os_free(usrv);
2762 return -1;
2763 }
2764 dl_list_add(&wpa_s->global->p2p_srv_upnp, &usrv->list);
2765
2766 wpas_p2p_sd_service_update(wpa_s);
2767 return 0;
2768}
2769
2770
2771int wpas_p2p_service_del_upnp(struct wpa_supplicant *wpa_s, u8 version,
2772 const char *service)
2773{
2774 struct p2p_srv_upnp *usrv;
2775
2776 usrv = wpas_p2p_service_get_upnp(wpa_s, version, service);
2777 if (usrv == NULL)
2778 return -1;
2779 wpas_p2p_srv_upnp_free(usrv);
2780 wpas_p2p_sd_service_update(wpa_s);
2781 return 0;
2782}
2783
2784
2785static void wpas_prov_disc_local_display(struct wpa_supplicant *wpa_s,
2786 const u8 *peer, const char *params,
2787 unsigned int generated_pin)
2788{
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07002789 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_SHOW_PIN MACSTR
2790 " %08d%s", MAC2STR(peer), generated_pin, params);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002791}
2792
2793
2794static void wpas_prov_disc_local_keypad(struct wpa_supplicant *wpa_s,
2795 const u8 *peer, const char *params)
2796{
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07002797 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_ENTER_PIN MACSTR
2798 "%s", MAC2STR(peer), params);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002799}
2800
2801
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002802static void wpas_prov_disc_req(void *ctx, const u8 *peer, u16 config_methods,
2803 const u8 *dev_addr, const u8 *pri_dev_type,
2804 const char *dev_name, u16 supp_config_methods,
2805 u8 dev_capab, u8 group_capab, const u8 *group_id,
2806 size_t group_id_len)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002807{
2808 struct wpa_supplicant *wpa_s = ctx;
2809 char devtype[WPS_DEV_TYPE_BUFSIZE];
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002810 char params[300];
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002811 u8 empty_dev_type[8];
2812 unsigned int generated_pin = 0;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002813 struct wpa_supplicant *group = NULL;
2814
2815 if (group_id) {
2816 for (group = wpa_s->global->ifaces; group; group = group->next)
2817 {
2818 struct wpa_ssid *s = group->current_ssid;
2819 if (s != NULL &&
2820 s->mode == WPAS_MODE_P2P_GO &&
2821 group_id_len - ETH_ALEN == s->ssid_len &&
2822 os_memcmp(group_id + ETH_ALEN, s->ssid,
2823 s->ssid_len) == 0)
2824 break;
2825 }
2826 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002827
2828 if (pri_dev_type == NULL) {
2829 os_memset(empty_dev_type, 0, sizeof(empty_dev_type));
2830 pri_dev_type = empty_dev_type;
2831 }
2832 os_snprintf(params, sizeof(params), " p2p_dev_addr=" MACSTR
2833 " pri_dev_type=%s name='%s' config_methods=0x%x "
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002834 "dev_capab=0x%x group_capab=0x%x%s%s",
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002835 MAC2STR(dev_addr),
2836 wps_dev_type_bin2str(pri_dev_type, devtype,
2837 sizeof(devtype)),
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002838 dev_name, supp_config_methods, dev_capab, group_capab,
2839 group ? " group=" : "",
2840 group ? group->ifname : "");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002841 params[sizeof(params) - 1] = '\0';
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08002842
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002843 if (config_methods & WPS_CONFIG_DISPLAY) {
2844 generated_pin = wps_generate_pin();
2845 wpas_prov_disc_local_display(wpa_s, peer, params,
2846 generated_pin);
2847 } else if (config_methods & WPS_CONFIG_KEYPAD)
2848 wpas_prov_disc_local_keypad(wpa_s, peer, params);
2849 else if (config_methods & WPS_CONFIG_PUSHBUTTON)
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07002850 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_PBC_REQ
2851 MACSTR "%s", MAC2STR(peer), params);
Jouni Malinen75ecf522011-06-27 15:19:46 -07002852
2853 wpas_notify_p2p_provision_discovery(wpa_s, peer, 1 /* request */,
2854 P2P_PROV_DISC_SUCCESS,
2855 config_methods, generated_pin);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002856}
2857
2858
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002859static void wpas_prov_disc_resp(void *ctx, const u8 *peer, u16 config_methods)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002860{
2861 struct wpa_supplicant *wpa_s = ctx;
2862 unsigned int generated_pin = 0;
Dmitry Shmidt04949592012-07-19 12:16:46 -07002863 char params[20];
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002864
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08002865 if (wpa_s->pending_pd_before_join &&
2866 (os_memcmp(peer, wpa_s->pending_join_dev_addr, ETH_ALEN) == 0 ||
2867 os_memcmp(peer, wpa_s->pending_join_iface_addr, ETH_ALEN) == 0)) {
2868 wpa_s->pending_pd_before_join = 0;
2869 wpa_printf(MSG_DEBUG, "P2P: Starting pending "
2870 "join-existing-group operation");
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08002871 wpas_p2p_join_start(wpa_s, 0, NULL, 0);
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08002872 return;
2873 }
2874
Dmitry Shmidt04949592012-07-19 12:16:46 -07002875 if (wpa_s->pending_pd_use == AUTO_PD_JOIN ||
2876 wpa_s->pending_pd_use == AUTO_PD_GO_NEG)
2877 os_snprintf(params, sizeof(params), " peer_go=%d",
2878 wpa_s->pending_pd_use == AUTO_PD_JOIN);
2879 else
2880 params[0] = '\0';
2881
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002882 if (config_methods & WPS_CONFIG_DISPLAY)
Dmitry Shmidt04949592012-07-19 12:16:46 -07002883 wpas_prov_disc_local_keypad(wpa_s, peer, params);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002884 else if (config_methods & WPS_CONFIG_KEYPAD) {
2885 generated_pin = wps_generate_pin();
Dmitry Shmidt04949592012-07-19 12:16:46 -07002886 wpas_prov_disc_local_display(wpa_s, peer, params,
2887 generated_pin);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002888 } else if (config_methods & WPS_CONFIG_PUSHBUTTON)
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07002889 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_PBC_RESP
2890 MACSTR "%s", MAC2STR(peer), params);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002891
Jouni Malinen75ecf522011-06-27 15:19:46 -07002892 wpas_notify_p2p_provision_discovery(wpa_s, peer, 0 /* response */,
2893 P2P_PROV_DISC_SUCCESS,
2894 config_methods, generated_pin);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002895}
2896
2897
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002898static void wpas_prov_disc_fail(void *ctx, const u8 *peer,
2899 enum p2p_prov_disc_status status)
Jouni Malinen75ecf522011-06-27 15:19:46 -07002900{
2901 struct wpa_supplicant *wpa_s = ctx;
2902
Dmitry Shmidt04949592012-07-19 12:16:46 -07002903 if (wpa_s->p2p_fallback_to_go_neg) {
2904 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: PD for p2p_connect-auto "
2905 "failed - fall back to GO Negotiation");
2906 wpas_p2p_fallback_to_go_neg(wpa_s, 0);
2907 return;
2908 }
2909
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002910 if (status == P2P_PROV_DISC_TIMEOUT_JOIN) {
Dmitry Shmidt2b380482012-09-13 10:52:13 -07002911 wpa_s->pending_pd_before_join = 0;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002912 wpa_printf(MSG_DEBUG, "P2P: Starting pending "
2913 "join-existing-group operation (no ACK for PD "
2914 "Req attempts)");
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08002915 wpas_p2p_join_start(wpa_s, 0, NULL, 0);
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002916 return;
Dmitry Shmidt2b380482012-09-13 10:52:13 -07002917 }
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002918
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07002919 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_FAILURE
2920 " p2p_dev_addr=" MACSTR " status=%d",
2921 MAC2STR(peer), status);
Dmitry Shmidt04949592012-07-19 12:16:46 -07002922
Jouni Malinen75ecf522011-06-27 15:19:46 -07002923 wpas_notify_p2p_provision_discovery(wpa_s, peer, 0 /* response */,
2924 status, 0, 0);
2925}
2926
2927
Dmitry Shmidtf7e0a992013-05-23 11:03:10 -07002928static int freq_included(const struct p2p_channels *channels, unsigned int freq)
2929{
2930 if (channels == NULL)
2931 return 1; /* Assume no restrictions */
2932 return p2p_channels_includes_freq(channels, freq);
2933
2934}
2935
2936
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07002937/**
2938 * Pick the best frequency to use from all the currently used frequencies.
2939 */
2940static int wpas_p2p_pick_best_used_freq(struct wpa_supplicant *wpa_s,
2941 struct wpa_used_freq_data *freqs,
2942 unsigned int num)
2943{
2944 unsigned int i, c;
2945
2946 /* find a candidate freq that is supported by P2P */
2947 for (c = 0; c < num; c++)
2948 if (p2p_supported_freq(wpa_s->global->p2p, freqs[c].freq))
2949 break;
2950
2951 if (c == num)
2952 return 0;
2953
2954 /* once we have a candidate, try to find a 'better' one */
2955 for (i = c + 1; i < num; i++) {
2956 if (!p2p_supported_freq(wpa_s->global->p2p, freqs[i].freq))
2957 continue;
2958
2959 /*
2960 * 1. Infrastructure station interfaces have higher preference.
2961 * 2. P2P Clients have higher preference.
2962 * 3. All others.
2963 */
2964 if (freqs[i].flags & WPA_FREQ_USED_BY_INFRA_STATION) {
2965 c = i;
2966 break;
2967 }
2968
2969 if ((freqs[i].flags & WPA_FREQ_USED_BY_P2P_CLIENT))
2970 c = i;
2971 }
2972 return freqs[c].freq;
2973}
2974
2975
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002976static u8 wpas_invitation_process(void *ctx, const u8 *sa, const u8 *bssid,
2977 const u8 *go_dev_addr, const u8 *ssid,
2978 size_t ssid_len, int *go, u8 *group_bssid,
Dmitry Shmidtf7e0a992013-05-23 11:03:10 -07002979 int *force_freq, int persistent_group,
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08002980 const struct p2p_channels *channels,
2981 int dev_pw_id)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002982{
2983 struct wpa_supplicant *wpa_s = ctx;
2984 struct wpa_ssid *s;
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07002985 struct wpa_used_freq_data *freqs;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002986 struct wpa_supplicant *grp;
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07002987 int best_freq;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002988
2989 if (!persistent_group) {
2990 wpa_printf(MSG_DEBUG, "P2P: Invitation from " MACSTR
Dmitry Shmidt344abd32014-01-14 13:17:00 -08002991 " to join an active group (SSID: %s)",
2992 MAC2STR(sa), wpa_ssid_txt(ssid, ssid_len));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002993 if (!is_zero_ether_addr(wpa_s->p2p_auth_invite) &&
2994 (os_memcmp(go_dev_addr, wpa_s->p2p_auth_invite, ETH_ALEN)
2995 == 0 ||
2996 os_memcmp(sa, wpa_s->p2p_auth_invite, ETH_ALEN) == 0)) {
2997 wpa_printf(MSG_DEBUG, "P2P: Accept previously "
2998 "authorized invitation");
2999 goto accept_inv;
3000 }
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08003001
3002#ifdef CONFIG_WPS_NFC
3003 if (dev_pw_id >= 0 && wpa_s->parent->p2p_nfc_tag_enabled &&
3004 dev_pw_id == wpa_s->parent->p2p_oob_dev_pw_id) {
3005 wpa_printf(MSG_DEBUG, "P2P: Accept invitation based on local enabled NFC Tag");
3006 wpa_s->parent->p2p_wps_method = WPS_NFC;
3007 wpa_s->parent->pending_join_wps_method = WPS_NFC;
3008 os_memcpy(wpa_s->parent->pending_join_dev_addr,
3009 go_dev_addr, ETH_ALEN);
3010 os_memcpy(wpa_s->parent->pending_join_iface_addr,
3011 bssid, ETH_ALEN);
3012 goto accept_inv;
3013 }
3014#endif /* CONFIG_WPS_NFC */
3015
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003016 /*
3017 * Do not accept the invitation automatically; notify user and
3018 * request approval.
3019 */
3020 return P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE;
3021 }
3022
3023 grp = wpas_get_p2p_group(wpa_s, ssid, ssid_len, go);
3024 if (grp) {
3025 wpa_printf(MSG_DEBUG, "P2P: Accept invitation to already "
3026 "running persistent group");
3027 if (*go)
3028 os_memcpy(group_bssid, grp->own_addr, ETH_ALEN);
3029 goto accept_inv;
3030 }
3031
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08003032 if (!is_zero_ether_addr(wpa_s->p2p_auth_invite) &&
3033 os_memcmp(sa, wpa_s->p2p_auth_invite, ETH_ALEN) == 0) {
3034 wpa_printf(MSG_DEBUG, "P2P: Accept previously initiated "
3035 "invitation to re-invoke a persistent group");
Dmitry Shmidt76cd2cc2014-05-27 12:56:04 -07003036 os_memset(wpa_s->p2p_auth_invite, 0, ETH_ALEN);
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08003037 } else if (!wpa_s->conf->persistent_reconnect)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003038 return P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE;
3039
3040 for (s = wpa_s->conf->ssid; s; s = s->next) {
3041 if (s->disabled == 2 &&
3042 os_memcmp(s->bssid, go_dev_addr, ETH_ALEN) == 0 &&
3043 s->ssid_len == ssid_len &&
3044 os_memcmp(ssid, s->ssid, ssid_len) == 0)
3045 break;
3046 }
3047
3048 if (!s) {
3049 wpa_printf(MSG_DEBUG, "P2P: Invitation from " MACSTR
3050 " requested reinvocation of an unknown group",
3051 MAC2STR(sa));
3052 return P2P_SC_FAIL_UNKNOWN_GROUP;
3053 }
3054
3055 if (s->mode == WPAS_MODE_P2P_GO && !wpas_p2p_create_iface(wpa_s)) {
3056 *go = 1;
3057 if (wpa_s->wpa_state >= WPA_AUTHENTICATING) {
3058 wpa_printf(MSG_DEBUG, "P2P: The only available "
3059 "interface is already in use - reject "
3060 "invitation");
3061 return P2P_SC_FAIL_UNABLE_TO_ACCOMMODATE;
3062 }
3063 os_memcpy(group_bssid, wpa_s->own_addr, ETH_ALEN);
3064 } else if (s->mode == WPAS_MODE_P2P_GO) {
3065 *go = 1;
3066 if (wpas_p2p_add_group_interface(wpa_s, WPA_IF_P2P_GO) < 0)
3067 {
3068 wpa_printf(MSG_ERROR, "P2P: Failed to allocate a new "
3069 "interface address for the group");
3070 return P2P_SC_FAIL_UNABLE_TO_ACCOMMODATE;
3071 }
3072 os_memcpy(group_bssid, wpa_s->pending_interface_addr,
3073 ETH_ALEN);
3074 }
3075
3076accept_inv:
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003077 wpas_p2p_set_own_freq_preference(wpa_s, 0);
3078
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07003079 best_freq = 0;
3080 freqs = os_calloc(wpa_s->num_multichan_concurrent,
3081 sizeof(struct wpa_used_freq_data));
3082 if (freqs) {
3083 int num_channels = wpa_s->num_multichan_concurrent;
3084 int num = wpas_p2p_valid_oper_freqs(wpa_s, freqs, num_channels);
3085 best_freq = wpas_p2p_pick_best_used_freq(wpa_s, freqs, num);
3086 os_free(freqs);
3087 }
3088
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07003089 /* Get one of the frequencies currently in use */
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07003090 if (best_freq > 0) {
Dmitry Shmidta0d265f2013-11-19 13:13:41 -08003091 wpa_printf(MSG_DEBUG, "P2P: Trying to prefer a channel already used by one of the interfaces");
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07003092 wpas_p2p_set_own_freq_preference(wpa_s, best_freq);
Dmitry Shmidta0d265f2013-11-19 13:13:41 -08003093
3094 if (wpa_s->num_multichan_concurrent < 2 ||
3095 wpas_p2p_num_unused_channels(wpa_s) < 1) {
3096 wpa_printf(MSG_DEBUG, "P2P: No extra channels available - trying to force channel to match a channel already used by one of the interfaces");
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07003097 *force_freq = best_freq;
Dmitry Shmidta0d265f2013-11-19 13:13:41 -08003098 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003099 }
3100
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07003101 if (*force_freq > 0 && wpa_s->num_multichan_concurrent > 1 &&
3102 wpas_p2p_num_unused_channels(wpa_s) > 0) {
Dmitry Shmidtf7e0a992013-05-23 11:03:10 -07003103 if (*go == 0) {
3104 /* We are the client */
3105 wpa_printf(MSG_DEBUG, "P2P: Peer was found to be "
3106 "running a GO but we are capable of MCC, "
3107 "figure out the best channel to use");
3108 *force_freq = 0;
3109 } else if (!freq_included(channels, *force_freq)) {
3110 /* We are the GO, and *force_freq is not in the
3111 * intersection */
3112 wpa_printf(MSG_DEBUG, "P2P: Forced GO freq %d MHz not "
3113 "in intersection but we are capable of MCC, "
3114 "figure out the best channel to use",
3115 *force_freq);
3116 *force_freq = 0;
3117 }
3118 }
3119
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003120 return P2P_SC_SUCCESS;
3121}
3122
3123
3124static void wpas_invitation_received(void *ctx, const u8 *sa, const u8 *bssid,
3125 const u8 *ssid, size_t ssid_len,
3126 const u8 *go_dev_addr, u8 status,
3127 int op_freq)
3128{
3129 struct wpa_supplicant *wpa_s = ctx;
3130 struct wpa_ssid *s;
3131
3132 for (s = wpa_s->conf->ssid; s; s = s->next) {
3133 if (s->disabled == 2 &&
3134 s->ssid_len == ssid_len &&
3135 os_memcmp(ssid, s->ssid, ssid_len) == 0)
3136 break;
3137 }
3138
3139 if (status == P2P_SC_SUCCESS) {
3140 wpa_printf(MSG_DEBUG, "P2P: Invitation from peer " MACSTR
Dmitry Shmidt344abd32014-01-14 13:17:00 -08003141 " was accepted; op_freq=%d MHz, SSID=%s",
3142 MAC2STR(sa), op_freq, wpa_ssid_txt(ssid, ssid_len));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003143 if (s) {
Dmitry Shmidt91c40cd2012-09-25 14:23:53 -07003144 int go = s->mode == WPAS_MODE_P2P_GO;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003145 wpas_p2p_group_add_persistent(
Dmitry Shmidt15907092014-03-25 10:42:57 -07003146 wpa_s, s, go, 0, op_freq, 0, 0, NULL,
Dmitry Shmidt56052862013-10-04 10:23:25 -07003147 go ? P2P_MAX_INITIAL_CONN_WAIT_GO_REINVOKE : 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003148 } else if (bssid) {
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08003149 wpa_s->user_initiated_pd = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003150 wpas_p2p_join(wpa_s, bssid, go_dev_addr,
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08003151 wpa_s->p2p_wps_method, 0, op_freq,
Dmitry Shmidt344abd32014-01-14 13:17:00 -08003152 ssid, ssid_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003153 }
3154 return;
3155 }
3156
3157 if (status != P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE) {
3158 wpa_printf(MSG_DEBUG, "P2P: Invitation from peer " MACSTR
3159 " was rejected (status %u)", MAC2STR(sa), status);
3160 return;
3161 }
3162
3163 if (!s) {
3164 if (bssid) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07003165 wpa_msg_global(wpa_s, MSG_INFO,
3166 P2P_EVENT_INVITATION_RECEIVED
3167 "sa=" MACSTR " go_dev_addr=" MACSTR
3168 " bssid=" MACSTR " unknown-network",
3169 MAC2STR(sa), MAC2STR(go_dev_addr),
3170 MAC2STR(bssid));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003171 } else {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07003172 wpa_msg_global(wpa_s, MSG_INFO,
3173 P2P_EVENT_INVITATION_RECEIVED
3174 "sa=" MACSTR " go_dev_addr=" MACSTR
3175 " unknown-network",
3176 MAC2STR(sa), MAC2STR(go_dev_addr));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003177 }
3178 return;
3179 }
3180
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08003181 if (s->mode == WPAS_MODE_P2P_GO && op_freq) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07003182 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_INVITATION_RECEIVED
3183 "sa=" MACSTR " persistent=%d freq=%d",
3184 MAC2STR(sa), s->id, op_freq);
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08003185 } else {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07003186 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_INVITATION_RECEIVED
3187 "sa=" MACSTR " persistent=%d",
3188 MAC2STR(sa), s->id);
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08003189 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003190}
3191
3192
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003193static void wpas_remove_persistent_peer(struct wpa_supplicant *wpa_s,
3194 struct wpa_ssid *ssid,
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07003195 const u8 *peer, int inv)
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003196{
3197 size_t i;
3198
3199 if (ssid == NULL)
3200 return;
3201
3202 for (i = 0; ssid->p2p_client_list && i < ssid->num_p2p_clients; i++) {
3203 if (os_memcmp(ssid->p2p_client_list + i * ETH_ALEN, peer,
3204 ETH_ALEN) == 0)
3205 break;
3206 }
Dmitry Shmidt6aa8ae42014-07-07 09:31:33 -07003207 if (i >= ssid->num_p2p_clients || !ssid->p2p_client_list) {
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003208 if (ssid->mode != WPAS_MODE_P2P_GO &&
3209 os_memcmp(ssid->bssid, peer, ETH_ALEN) == 0) {
3210 wpa_printf(MSG_DEBUG, "P2P: Remove persistent group %d "
3211 "due to invitation result", ssid->id);
3212 wpas_notify_network_removed(wpa_s, ssid);
3213 wpa_config_remove_network(wpa_s->conf, ssid->id);
3214 return;
3215 }
3216 return; /* Peer not found in client list */
3217 }
3218
3219 wpa_printf(MSG_DEBUG, "P2P: Remove peer " MACSTR " from persistent "
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07003220 "group %d client list%s",
3221 MAC2STR(peer), ssid->id,
3222 inv ? " due to invitation result" : "");
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003223 os_memmove(ssid->p2p_client_list + i * ETH_ALEN,
3224 ssid->p2p_client_list + (i + 1) * ETH_ALEN,
3225 (ssid->num_p2p_clients - i - 1) * ETH_ALEN);
3226 ssid->num_p2p_clients--;
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003227 if (wpa_s->parent->conf->update_config &&
3228 wpa_config_write(wpa_s->parent->confname, wpa_s->parent->conf))
3229 wpa_printf(MSG_DEBUG, "P2P: Failed to update configuration");
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003230}
3231
3232
3233static void wpas_remove_persistent_client(struct wpa_supplicant *wpa_s,
3234 const u8 *peer)
3235{
3236 struct wpa_ssid *ssid;
3237
3238 wpa_s = wpa_s->global->p2p_invite_group;
3239 if (wpa_s == NULL)
3240 return; /* No known invitation group */
3241 ssid = wpa_s->current_ssid;
3242 if (ssid == NULL || ssid->mode != WPAS_MODE_P2P_GO ||
3243 !ssid->p2p_persistent_group)
3244 return; /* Not operating as a GO in persistent group */
3245 ssid = wpas_p2p_get_persistent(wpa_s->parent, peer,
3246 ssid->ssid, ssid->ssid_len);
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07003247 wpas_remove_persistent_peer(wpa_s, ssid, peer, 1);
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003248}
3249
3250
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08003251static void wpas_invitation_result(void *ctx, int status, const u8 *bssid,
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003252 const struct p2p_channels *channels,
Dmitry Shmidt15907092014-03-25 10:42:57 -07003253 const u8 *peer, int neg_freq,
3254 int peer_oper_freq)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003255{
3256 struct wpa_supplicant *wpa_s = ctx;
3257 struct wpa_ssid *ssid;
Dmitry Shmidt15907092014-03-25 10:42:57 -07003258 int freq;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003259
3260 if (bssid) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07003261 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_INVITATION_RESULT
3262 "status=%d " MACSTR,
3263 status, MAC2STR(bssid));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003264 } else {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07003265 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_INVITATION_RESULT
3266 "status=%d ", status);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003267 }
3268 wpas_notify_p2p_invitation_result(wpa_s, status, bssid);
3269
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003270 wpa_printf(MSG_DEBUG, "P2P: Invitation result - status=%d peer=" MACSTR,
3271 status, MAC2STR(peer));
3272 if (wpa_s->pending_invite_ssid_id == -1) {
3273 if (status == P2P_SC_FAIL_UNKNOWN_GROUP)
3274 wpas_remove_persistent_client(wpa_s, peer);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003275 return; /* Invitation to active group */
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003276 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003277
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08003278 if (status == P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE) {
3279 wpa_printf(MSG_DEBUG, "P2P: Waiting for peer to start another "
3280 "invitation exchange to indicate readiness for "
3281 "re-invocation");
3282 }
3283
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003284 if (status != P2P_SC_SUCCESS) {
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003285 if (status == P2P_SC_FAIL_UNKNOWN_GROUP) {
3286 ssid = wpa_config_get_network(
3287 wpa_s->conf, wpa_s->pending_invite_ssid_id);
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07003288 wpas_remove_persistent_peer(wpa_s, ssid, peer, 1);
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003289 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003290 wpas_p2p_remove_pending_group_interface(wpa_s);
3291 return;
3292 }
3293
3294 ssid = wpa_config_get_network(wpa_s->conf,
3295 wpa_s->pending_invite_ssid_id);
3296 if (ssid == NULL) {
3297 wpa_printf(MSG_ERROR, "P2P: Could not find persistent group "
3298 "data matching with invitation");
3299 return;
3300 }
3301
Jouni Malinen2c5b17d2012-10-13 21:52:46 -07003302 /*
3303 * The peer could have missed our ctrl::ack frame for Invitation
3304 * Response and continue retransmitting the frame. To reduce the
3305 * likelihood of the peer not getting successful TX status for the
3306 * Invitation Response frame, wait a short time here before starting
3307 * the persistent group so that we will remain on the current channel to
3308 * acknowledge any possible retransmission from the peer.
3309 */
3310 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: 50 ms wait on current channel before "
3311 "starting persistent group");
3312 os_sleep(0, 50000);
3313
Dmitry Shmidt15907092014-03-25 10:42:57 -07003314 if (neg_freq > 0 && ssid->mode == WPAS_MODE_P2P_GO &&
3315 freq_included(channels, neg_freq))
3316 freq = neg_freq;
3317 else if (peer_oper_freq > 0 && ssid->mode != WPAS_MODE_P2P_GO &&
3318 freq_included(channels, peer_oper_freq))
3319 freq = peer_oper_freq;
3320 else
3321 freq = 0;
3322
3323 wpa_printf(MSG_DEBUG, "P2P: Persistent group invitation success - op_freq=%d MHz SSID=%s",
3324 freq, wpa_ssid_txt(ssid->ssid, ssid->ssid_len));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003325 wpas_p2p_group_add_persistent(wpa_s, ssid,
Jouni Malinen31be0a42012-08-31 21:20:51 +03003326 ssid->mode == WPAS_MODE_P2P_GO,
Dmitry Shmidt96be6222014-02-13 10:16:51 -08003327 wpa_s->p2p_persistent_go_freq,
Dmitry Shmidt15907092014-03-25 10:42:57 -07003328 freq,
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003329 wpa_s->p2p_go_ht40, wpa_s->p2p_go_vht,
3330 channels,
Dmitry Shmidt56052862013-10-04 10:23:25 -07003331 ssid->mode == WPAS_MODE_P2P_GO ?
3332 P2P_MAX_INITIAL_CONN_WAIT_GO_REINVOKE :
3333 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003334}
3335
3336
Dmitry Shmidt04949592012-07-19 12:16:46 -07003337static int wpas_p2p_disallowed_freq(struct wpa_global *global,
3338 unsigned int freq)
3339{
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08003340 if (freq_range_list_includes(&global->p2p_go_avoid_freq, freq))
3341 return 1;
Dmitry Shmidt4ce9c872013-10-24 11:08:13 -07003342 return freq_range_list_includes(&global->p2p_disallow_freq, freq);
Dmitry Shmidt04949592012-07-19 12:16:46 -07003343}
3344
3345
3346static void wpas_p2p_add_chan(struct p2p_reg_class *reg, u8 chan)
3347{
3348 reg->channel[reg->channels] = chan;
3349 reg->channels++;
3350}
3351
3352
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003353static int wpas_p2p_default_channels(struct wpa_supplicant *wpa_s,
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003354 struct p2p_channels *chan,
3355 struct p2p_channels *cli_chan)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003356{
3357 int i, cla = 0;
3358
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003359 os_memset(cli_chan, 0, sizeof(*cli_chan));
3360
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003361 wpa_printf(MSG_DEBUG, "P2P: Enable operating classes for 2.4 GHz "
3362 "band");
3363
3364 /* Operating class 81 - 2.4 GHz band channels 1..13 */
3365 chan->reg_class[cla].reg_class = 81;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003366 chan->reg_class[cla].channels = 0;
3367 for (i = 0; i < 11; i++) {
3368 if (!wpas_p2p_disallowed_freq(wpa_s->global, 2412 + i * 5))
3369 wpas_p2p_add_chan(&chan->reg_class[cla], i + 1);
3370 }
3371 if (chan->reg_class[cla].channels)
3372 cla++;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003373
3374 wpa_printf(MSG_DEBUG, "P2P: Enable operating classes for lower 5 GHz "
3375 "band");
3376
3377 /* Operating class 115 - 5 GHz, channels 36-48 */
3378 chan->reg_class[cla].reg_class = 115;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003379 chan->reg_class[cla].channels = 0;
3380 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 36 * 5))
3381 wpas_p2p_add_chan(&chan->reg_class[cla], 36);
3382 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 40 * 5))
3383 wpas_p2p_add_chan(&chan->reg_class[cla], 40);
3384 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 44 * 5))
3385 wpas_p2p_add_chan(&chan->reg_class[cla], 44);
3386 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 48 * 5))
3387 wpas_p2p_add_chan(&chan->reg_class[cla], 48);
3388 if (chan->reg_class[cla].channels)
3389 cla++;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003390
3391 wpa_printf(MSG_DEBUG, "P2P: Enable operating classes for higher 5 GHz "
3392 "band");
3393
3394 /* Operating class 124 - 5 GHz, channels 149,153,157,161 */
3395 chan->reg_class[cla].reg_class = 124;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003396 chan->reg_class[cla].channels = 0;
3397 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 149 * 5))
3398 wpas_p2p_add_chan(&chan->reg_class[cla], 149);
3399 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 153 * 5))
3400 wpas_p2p_add_chan(&chan->reg_class[cla], 153);
3401 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 156 * 5))
3402 wpas_p2p_add_chan(&chan->reg_class[cla], 157);
3403 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 161 * 5))
3404 wpas_p2p_add_chan(&chan->reg_class[cla], 161);
3405 if (chan->reg_class[cla].channels)
3406 cla++;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003407
3408 chan->reg_classes = cla;
3409 return 0;
3410}
3411
3412
3413static struct hostapd_hw_modes * get_mode(struct hostapd_hw_modes *modes,
3414 u16 num_modes,
3415 enum hostapd_hw_mode mode)
3416{
3417 u16 i;
3418
3419 for (i = 0; i < num_modes; i++) {
3420 if (modes[i].mode == mode)
3421 return &modes[i];
3422 }
3423
3424 return NULL;
3425}
3426
3427
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003428enum chan_allowed {
3429 NOT_ALLOWED, PASSIVE_ONLY, ALLOWED
3430};
3431
Dmitry Shmidt04949592012-07-19 12:16:46 -07003432static int has_channel(struct wpa_global *global,
3433 struct hostapd_hw_modes *mode, u8 chan, int *flags)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003434{
3435 int i;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003436 unsigned int freq;
3437
3438 freq = (mode->mode == HOSTAPD_MODE_IEEE80211A ? 5000 : 2407) +
3439 chan * 5;
3440 if (wpas_p2p_disallowed_freq(global, freq))
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003441 return NOT_ALLOWED;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003442
3443 for (i = 0; i < mode->num_channels; i++) {
3444 if (mode->channels[i].chan == chan) {
3445 if (flags)
3446 *flags = mode->channels[i].flag;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003447 if (mode->channels[i].flag &
3448 (HOSTAPD_CHAN_DISABLED |
3449 HOSTAPD_CHAN_RADAR))
3450 return NOT_ALLOWED;
3451 if (mode->channels[i].flag &
3452 (HOSTAPD_CHAN_PASSIVE_SCAN |
3453 HOSTAPD_CHAN_NO_IBSS))
3454 return PASSIVE_ONLY;
3455 return ALLOWED;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003456 }
3457 }
3458
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003459 return NOT_ALLOWED;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003460}
3461
3462
3463struct p2p_oper_class_map {
3464 enum hostapd_hw_mode mode;
3465 u8 op_class;
3466 u8 min_chan;
3467 u8 max_chan;
3468 u8 inc;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003469 enum { BW20, BW40PLUS, BW40MINUS, BW80 } bw;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003470};
3471
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003472static struct p2p_oper_class_map op_class[] = {
3473 { HOSTAPD_MODE_IEEE80211G, 81, 1, 13, 1, BW20 },
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003474#if 0 /* Do not enable HT40 on 2 GHz for now */
3475 { HOSTAPD_MODE_IEEE80211G, 83, 1, 9, 1, BW40PLUS },
3476 { HOSTAPD_MODE_IEEE80211G, 84, 5, 13, 1, BW40MINUS },
3477#endif
3478 { HOSTAPD_MODE_IEEE80211A, 115, 36, 48, 4, BW20 },
3479 { HOSTAPD_MODE_IEEE80211A, 124, 149, 161, 4, BW20 },
3480 { HOSTAPD_MODE_IEEE80211A, 116, 36, 44, 8, BW40PLUS },
3481 { HOSTAPD_MODE_IEEE80211A, 117, 40, 48, 8, BW40MINUS },
3482 { HOSTAPD_MODE_IEEE80211A, 126, 149, 157, 8, BW40PLUS },
3483 { HOSTAPD_MODE_IEEE80211A, 127, 153, 161, 8, BW40MINUS },
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003484
3485 /*
3486 * IEEE P802.11ac/D7.0 Table E-4 actually talks about channel center
3487 * frequency index 42, 58, 106, 122, 138, 155 with channel spacing of
3488 * 80 MHz, but currently use the following definition for simplicity
3489 * (these center frequencies are not actual channels, which makes
3490 * has_channel() fail). wpas_p2p_verify_80mhz() should take care of
3491 * removing invalid channels.
3492 */
3493 { HOSTAPD_MODE_IEEE80211A, 128, 36, 161, 4, BW80 },
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003494 { -1, 0, 0, 0, 0, BW20 }
3495};
3496
3497
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003498static int wpas_p2p_get_center_80mhz(struct wpa_supplicant *wpa_s,
3499 struct hostapd_hw_modes *mode,
3500 u8 channel)
3501{
3502 u8 center_channels[] = { 42, 58, 106, 122, 138, 155 };
3503 unsigned int i;
3504
3505 if (mode->mode != HOSTAPD_MODE_IEEE80211A)
3506 return 0;
3507
3508 for (i = 0; i < ARRAY_SIZE(center_channels); i++)
3509 /*
3510 * In 80 MHz, the bandwidth "spans" 12 channels (e.g., 36-48),
3511 * so the center channel is 6 channels away from the start/end.
3512 */
3513 if (channel >= center_channels[i] - 6 &&
3514 channel <= center_channels[i] + 6)
3515 return center_channels[i];
3516
3517 return 0;
3518}
3519
3520
3521static enum chan_allowed wpas_p2p_verify_80mhz(struct wpa_supplicant *wpa_s,
3522 struct hostapd_hw_modes *mode,
3523 u8 channel, u8 bw)
3524{
3525 u8 center_chan;
3526 int i, flags;
3527 enum chan_allowed res, ret = ALLOWED;
3528
3529 center_chan = wpas_p2p_get_center_80mhz(wpa_s, mode, channel);
3530 if (!center_chan)
3531 return NOT_ALLOWED;
3532 if (center_chan >= 58 && center_chan <= 138)
3533 return NOT_ALLOWED; /* Do not allow DFS channels for P2P */
3534
3535 /* check all the channels are available */
3536 for (i = 0; i < 4; i++) {
3537 int adj_chan = center_chan - 6 + i * 4;
3538
3539 res = has_channel(wpa_s->global, mode, adj_chan, &flags);
3540 if (res == NOT_ALLOWED)
3541 return NOT_ALLOWED;
3542 if (res == PASSIVE_ONLY)
3543 ret = PASSIVE_ONLY;
3544
3545 if (i == 0 && !(flags & HOSTAPD_CHAN_VHT_10_70))
3546 return NOT_ALLOWED;
3547 if (i == 1 && !(flags & HOSTAPD_CHAN_VHT_30_50))
3548 return NOT_ALLOWED;
3549 if (i == 2 && !(flags & HOSTAPD_CHAN_VHT_50_30))
3550 return NOT_ALLOWED;
3551 if (i == 3 && !(flags & HOSTAPD_CHAN_VHT_70_10))
3552 return NOT_ALLOWED;
3553 }
3554
3555 return ret;
3556}
3557
3558
3559static enum chan_allowed wpas_p2p_verify_channel(struct wpa_supplicant *wpa_s,
3560 struct hostapd_hw_modes *mode,
3561 u8 channel, u8 bw)
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003562{
Dmitry Shmidt96be6222014-02-13 10:16:51 -08003563 int flag = 0;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003564 enum chan_allowed res, res2;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003565
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003566 res2 = res = has_channel(wpa_s->global, mode, channel, &flag);
3567 if (bw == BW40MINUS) {
3568 if (!(flag & HOSTAPD_CHAN_HT40MINUS))
3569 return NOT_ALLOWED;
3570 res2 = has_channel(wpa_s->global, mode, channel - 4, NULL);
3571 } else if (bw == BW40PLUS) {
3572 if (!(flag & HOSTAPD_CHAN_HT40PLUS))
3573 return NOT_ALLOWED;
3574 res2 = has_channel(wpa_s->global, mode, channel + 4, NULL);
3575 } else if (bw == BW80) {
3576 res2 = wpas_p2p_verify_80mhz(wpa_s, mode, channel, bw);
3577 }
3578
3579 if (res == NOT_ALLOWED || res2 == NOT_ALLOWED)
3580 return NOT_ALLOWED;
3581 if (res == PASSIVE_ONLY || res2 == PASSIVE_ONLY)
3582 return PASSIVE_ONLY;
3583 return res;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003584}
3585
3586
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003587static int wpas_p2p_setup_channels(struct wpa_supplicant *wpa_s,
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003588 struct p2p_channels *chan,
3589 struct p2p_channels *cli_chan)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003590{
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003591 struct hostapd_hw_modes *mode;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003592 int cla, op, cli_cla;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003593
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003594 if (wpa_s->hw.modes == NULL) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003595 wpa_printf(MSG_DEBUG, "P2P: Driver did not support fetching "
3596 "of all supported channels; assume dualband "
3597 "support");
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003598 return wpas_p2p_default_channels(wpa_s, chan, cli_chan);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003599 }
3600
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003601 cla = cli_cla = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003602
3603 for (op = 0; op_class[op].op_class; op++) {
3604 struct p2p_oper_class_map *o = &op_class[op];
3605 u8 ch;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003606 struct p2p_reg_class *reg = NULL, *cli_reg = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003607
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003608 mode = get_mode(wpa_s->hw.modes, wpa_s->hw.num_modes, o->mode);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003609 if (mode == NULL)
3610 continue;
3611 for (ch = o->min_chan; ch <= o->max_chan; ch += o->inc) {
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003612 enum chan_allowed res;
3613 res = wpas_p2p_verify_channel(wpa_s, mode, ch, o->bw);
3614 if (res == ALLOWED) {
3615 if (reg == NULL) {
3616 wpa_printf(MSG_DEBUG, "P2P: Add operating class %u",
3617 o->op_class);
3618 reg = &chan->reg_class[cla];
3619 cla++;
3620 reg->reg_class = o->op_class;
3621 }
3622 reg->channel[reg->channels] = ch;
3623 reg->channels++;
3624 } else if (res == PASSIVE_ONLY &&
3625 wpa_s->conf->p2p_add_cli_chan) {
3626 if (cli_reg == NULL) {
3627 wpa_printf(MSG_DEBUG, "P2P: Add operating class %u (client only)",
3628 o->op_class);
3629 cli_reg = &cli_chan->reg_class[cli_cla];
3630 cli_cla++;
3631 cli_reg->reg_class = o->op_class;
3632 }
3633 cli_reg->channel[cli_reg->channels] = ch;
3634 cli_reg->channels++;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003635 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003636 }
3637 if (reg) {
3638 wpa_hexdump(MSG_DEBUG, "P2P: Channels",
3639 reg->channel, reg->channels);
3640 }
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003641 if (cli_reg) {
3642 wpa_hexdump(MSG_DEBUG, "P2P: Channels (client only)",
3643 cli_reg->channel, cli_reg->channels);
3644 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003645 }
3646
3647 chan->reg_classes = cla;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003648 cli_chan->reg_classes = cli_cla;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003649
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003650 return 0;
3651}
3652
3653
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003654int wpas_p2p_get_ht40_mode(struct wpa_supplicant *wpa_s,
3655 struct hostapd_hw_modes *mode, u8 channel)
3656{
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003657 int op;
3658 enum chan_allowed ret;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003659
3660 for (op = 0; op_class[op].op_class; op++) {
3661 struct p2p_oper_class_map *o = &op_class[op];
3662 u8 ch;
3663
3664 for (ch = o->min_chan; ch <= o->max_chan; ch += o->inc) {
3665 if (o->mode != HOSTAPD_MODE_IEEE80211A ||
3666 o->bw == BW20 || ch != channel)
3667 continue;
3668 ret = wpas_p2p_verify_channel(wpa_s, mode, ch, o->bw);
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003669 if (ret == ALLOWED)
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003670 return (o->bw == BW40MINUS) ? -1 : 1;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003671 }
3672 }
3673 return 0;
3674}
3675
3676
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003677int wpas_p2p_get_vht80_center(struct wpa_supplicant *wpa_s,
3678 struct hostapd_hw_modes *mode, u8 channel)
3679{
3680 if (!wpas_p2p_verify_channel(wpa_s, mode, channel, BW80))
3681 return 0;
3682
3683 return wpas_p2p_get_center_80mhz(wpa_s, mode, channel);
3684}
3685
3686
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003687static int wpas_get_noa(void *ctx, const u8 *interface_addr, u8 *buf,
3688 size_t buf_len)
3689{
3690 struct wpa_supplicant *wpa_s = ctx;
3691
3692 for (wpa_s = wpa_s->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
3693 if (os_memcmp(wpa_s->own_addr, interface_addr, ETH_ALEN) == 0)
3694 break;
3695 }
3696 if (wpa_s == NULL)
3697 return -1;
3698
3699 return wpa_drv_get_noa(wpa_s, buf, buf_len);
3700}
3701
3702
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07003703struct wpa_supplicant * wpas_get_p2p_go_iface(struct wpa_supplicant *wpa_s,
3704 const u8 *ssid, size_t ssid_len)
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08003705{
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07003706 for (wpa_s = wpa_s->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
3707 struct wpa_ssid *s = wpa_s->current_ssid;
3708 if (s == NULL)
3709 continue;
3710 if (s->mode != WPAS_MODE_P2P_GO &&
3711 s->mode != WPAS_MODE_AP &&
3712 s->mode != WPAS_MODE_P2P_GROUP_FORMATION)
3713 continue;
3714 if (s->ssid_len != ssid_len ||
Dmitry Shmidt03658832014-08-13 11:03:49 -07003715 os_memcmp(ssid, s->ssid, ssid_len) != 0)
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07003716 continue;
3717 return wpa_s;
3718 }
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08003719
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07003720 return NULL;
3721
3722}
3723
3724
3725struct wpa_supplicant * wpas_get_p2p_client_iface(struct wpa_supplicant *wpa_s,
3726 const u8 *peer_dev_addr)
3727{
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08003728 for (wpa_s = wpa_s->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
3729 struct wpa_ssid *ssid = wpa_s->current_ssid;
3730 if (ssid == NULL)
3731 continue;
3732 if (ssid->mode != WPAS_MODE_INFRA)
3733 continue;
3734 if (wpa_s->wpa_state != WPA_COMPLETED &&
3735 wpa_s->wpa_state != WPA_GROUP_HANDSHAKE)
3736 continue;
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07003737 if (os_memcmp(wpa_s->go_dev_addr, peer_dev_addr, ETH_ALEN) == 0)
3738 return wpa_s;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08003739 }
3740
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07003741 return NULL;
3742}
3743
3744
3745static int wpas_go_connected(void *ctx, const u8 *dev_addr)
3746{
3747 struct wpa_supplicant *wpa_s = ctx;
3748
3749 return wpas_get_p2p_client_iface(wpa_s, dev_addr) != NULL;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08003750}
3751
3752
Dmitry Shmidt18463232014-01-24 12:29:41 -08003753static int wpas_is_concurrent_session_active(void *ctx)
3754{
3755 struct wpa_supplicant *wpa_s = ctx;
3756 struct wpa_supplicant *ifs;
3757
3758 for (ifs = wpa_s->global->ifaces; ifs; ifs = ifs->next) {
3759 if (ifs == wpa_s)
3760 continue;
3761 if (ifs->wpa_state > WPA_ASSOCIATED)
3762 return 1;
3763 }
3764 return 0;
3765}
3766
3767
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07003768static void wpas_p2p_debug_print(void *ctx, int level, const char *msg)
3769{
3770 struct wpa_supplicant *wpa_s = ctx;
3771 wpa_msg_global(wpa_s, level, "P2P: %s", msg);
3772}
3773
3774
Dmitry Shmidt34af3062013-07-11 10:46:32 -07003775int wpas_p2p_add_p2pdev_interface(struct wpa_supplicant *wpa_s)
3776{
3777 struct wpa_interface iface;
3778 struct wpa_supplicant *p2pdev_wpa_s;
3779 char ifname[100];
3780 char force_name[100];
3781 int ret;
3782
3783 os_snprintf(ifname, sizeof(ifname), P2P_MGMT_DEVICE_PREFIX "%s",
3784 wpa_s->ifname);
3785 force_name[0] = '\0';
3786 wpa_s->pending_interface_type = WPA_IF_P2P_DEVICE;
3787 ret = wpa_drv_if_add(wpa_s, WPA_IF_P2P_DEVICE, ifname, NULL, NULL,
3788 force_name, wpa_s->pending_interface_addr, NULL);
3789 if (ret < 0) {
3790 wpa_printf(MSG_DEBUG, "P2P: Failed to create P2P Device interface");
3791 return ret;
3792 }
3793 os_strlcpy(wpa_s->pending_interface_name, ifname,
3794 sizeof(wpa_s->pending_interface_name));
3795
3796 os_memset(&iface, 0, sizeof(iface));
3797 iface.p2p_mgmt = 1;
3798 iface.ifname = wpa_s->pending_interface_name;
3799 iface.driver = wpa_s->driver->name;
3800 iface.driver_param = wpa_s->conf->driver_param;
Dmitry Shmidt2ac5f602014-03-07 10:08:21 -08003801
3802 /*
3803 * If a P2P Device configuration file was given, use it as the interface
3804 * configuration file (instead of using parent's configuration file.
3805 */
3806 if (wpa_s->conf_p2p_dev) {
3807 iface.confname = wpa_s->conf_p2p_dev;
3808 iface.ctrl_interface = NULL;
3809 } else {
3810 iface.confname = wpa_s->confname;
3811 iface.ctrl_interface = wpa_s->conf->ctrl_interface;
3812 }
3813 iface.conf_p2p_dev = NULL;
3814
Dmitry Shmidt34af3062013-07-11 10:46:32 -07003815 p2pdev_wpa_s = wpa_supplicant_add_iface(wpa_s->global, &iface);
3816 if (!p2pdev_wpa_s) {
3817 wpa_printf(MSG_DEBUG, "P2P: Failed to add P2P Device interface");
3818 return -1;
3819 }
3820 p2pdev_wpa_s->parent = wpa_s;
3821
3822 wpa_s->pending_interface_name[0] = '\0';
3823 return 0;
3824}
3825
3826
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08003827static void wpas_presence_resp(void *ctx, const u8 *src, u8 status,
3828 const u8 *noa, size_t noa_len)
3829{
3830 struct wpa_supplicant *wpa_s, *intf = ctx;
3831 char hex[100];
3832
3833 for (wpa_s = intf->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
3834 if (wpa_s->waiting_presence_resp)
3835 break;
3836 }
3837 if (!wpa_s) {
3838 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: No group interface was waiting for presence response");
3839 return;
3840 }
3841 wpa_s->waiting_presence_resp = 0;
3842
3843 wpa_snprintf_hex(hex, sizeof(hex), noa, noa_len);
3844 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_PRESENCE_RESPONSE "src=" MACSTR
3845 " status=%u noa=%s", MAC2STR(src), status, hex);
3846}
3847
3848
Dmitry Shmidt5a1480c2014-05-12 09:46:02 -07003849static int _wpas_p2p_in_progress(void *ctx)
3850{
3851 struct wpa_supplicant *wpa_s = ctx;
3852 return wpas_p2p_in_progress(wpa_s);
3853}
3854
3855
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003856/**
3857 * wpas_p2p_init - Initialize P2P module for %wpa_supplicant
3858 * @global: Pointer to global data from wpa_supplicant_init()
3859 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
3860 * Returns: 0 on success, -1 on failure
3861 */
3862int wpas_p2p_init(struct wpa_global *global, struct wpa_supplicant *wpa_s)
3863{
3864 struct p2p_config p2p;
3865 unsigned int r;
3866 int i;
3867
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07003868 if (wpa_s->conf->p2p_disabled)
3869 return 0;
3870
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003871 if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_CAPABLE))
3872 return 0;
3873
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003874 if (global->p2p)
3875 return 0;
3876
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003877 os_memset(&p2p, 0, sizeof(p2p));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003878 p2p.cb_ctx = wpa_s;
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07003879 p2p.debug_print = wpas_p2p_debug_print;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003880 p2p.p2p_scan = wpas_p2p_scan;
3881 p2p.send_action = wpas_send_action;
3882 p2p.send_action_done = wpas_send_action_done;
3883 p2p.go_neg_completed = wpas_go_neg_completed;
3884 p2p.go_neg_req_rx = wpas_go_neg_req_rx;
3885 p2p.dev_found = wpas_dev_found;
3886 p2p.dev_lost = wpas_dev_lost;
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07003887 p2p.find_stopped = wpas_find_stopped;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003888 p2p.start_listen = wpas_start_listen;
3889 p2p.stop_listen = wpas_stop_listen;
3890 p2p.send_probe_resp = wpas_send_probe_resp;
3891 p2p.sd_request = wpas_sd_request;
3892 p2p.sd_response = wpas_sd_response;
3893 p2p.prov_disc_req = wpas_prov_disc_req;
3894 p2p.prov_disc_resp = wpas_prov_disc_resp;
Jouni Malinen75ecf522011-06-27 15:19:46 -07003895 p2p.prov_disc_fail = wpas_prov_disc_fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003896 p2p.invitation_process = wpas_invitation_process;
3897 p2p.invitation_received = wpas_invitation_received;
3898 p2p.invitation_result = wpas_invitation_result;
3899 p2p.get_noa = wpas_get_noa;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08003900 p2p.go_connected = wpas_go_connected;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08003901 p2p.presence_resp = wpas_presence_resp;
Dmitry Shmidt18463232014-01-24 12:29:41 -08003902 p2p.is_concurrent_session_active = wpas_is_concurrent_session_active;
Dmitry Shmidt5a1480c2014-05-12 09:46:02 -07003903 p2p.is_p2p_in_progress = _wpas_p2p_in_progress;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003904
3905 os_memcpy(wpa_s->global->p2p_dev_addr, wpa_s->own_addr, ETH_ALEN);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003906 os_memcpy(p2p.dev_addr, wpa_s->global->p2p_dev_addr, ETH_ALEN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003907 p2p.dev_name = wpa_s->conf->device_name;
3908 p2p.manufacturer = wpa_s->conf->manufacturer;
3909 p2p.model_name = wpa_s->conf->model_name;
3910 p2p.model_number = wpa_s->conf->model_number;
3911 p2p.serial_number = wpa_s->conf->serial_number;
3912 if (wpa_s->wps) {
3913 os_memcpy(p2p.uuid, wpa_s->wps->uuid, 16);
3914 p2p.config_methods = wpa_s->wps->config_methods;
3915 }
3916
3917 if (wpa_s->conf->p2p_listen_reg_class &&
3918 wpa_s->conf->p2p_listen_channel) {
3919 p2p.reg_class = wpa_s->conf->p2p_listen_reg_class;
3920 p2p.channel = wpa_s->conf->p2p_listen_channel;
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07003921 p2p.channel_forced = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003922 } else {
3923 p2p.reg_class = 81;
3924 /*
3925 * Pick one of the social channels randomly as the listen
3926 * channel.
3927 */
3928 os_get_random((u8 *) &r, sizeof(r));
3929 p2p.channel = 1 + (r % 3) * 5;
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07003930 p2p.channel_forced = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003931 }
3932 wpa_printf(MSG_DEBUG, "P2P: Own listen channel: %d", p2p.channel);
3933
3934 if (wpa_s->conf->p2p_oper_reg_class &&
3935 wpa_s->conf->p2p_oper_channel) {
3936 p2p.op_reg_class = wpa_s->conf->p2p_oper_reg_class;
3937 p2p.op_channel = wpa_s->conf->p2p_oper_channel;
3938 p2p.cfg_op_channel = 1;
3939 wpa_printf(MSG_DEBUG, "P2P: Configured operating channel: "
3940 "%d:%d", p2p.op_reg_class, p2p.op_channel);
3941
3942 } else {
3943 p2p.op_reg_class = 81;
3944 /*
3945 * Use random operation channel from (1, 6, 11) if no other
3946 * preference is indicated.
3947 */
3948 os_get_random((u8 *) &r, sizeof(r));
3949 p2p.op_channel = 1 + (r % 3) * 5;
3950 p2p.cfg_op_channel = 0;
3951 wpa_printf(MSG_DEBUG, "P2P: Random operating channel: "
3952 "%d:%d", p2p.op_reg_class, p2p.op_channel);
3953 }
Dmitry Shmidt44c95782013-05-17 09:51:35 -07003954
3955 if (wpa_s->conf->p2p_pref_chan && wpa_s->conf->num_p2p_pref_chan) {
3956 p2p.pref_chan = wpa_s->conf->p2p_pref_chan;
3957 p2p.num_pref_chan = wpa_s->conf->num_p2p_pref_chan;
3958 }
3959
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003960 if (wpa_s->conf->country[0] && wpa_s->conf->country[1]) {
3961 os_memcpy(p2p.country, wpa_s->conf->country, 2);
3962 p2p.country[2] = 0x04;
3963 } else
3964 os_memcpy(p2p.country, "XX\x04", 3);
3965
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003966 if (wpas_p2p_setup_channels(wpa_s, &p2p.channels, &p2p.cli_channels)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003967 wpa_printf(MSG_ERROR, "P2P: Failed to configure supported "
3968 "channel list");
3969 return -1;
3970 }
3971
3972 os_memcpy(p2p.pri_dev_type, wpa_s->conf->device_type,
3973 WPS_DEV_TYPE_LEN);
3974
3975 p2p.num_sec_dev_types = wpa_s->conf->num_sec_device_types;
3976 os_memcpy(p2p.sec_dev_type, wpa_s->conf->sec_device_type,
3977 p2p.num_sec_dev_types * WPS_DEV_TYPE_LEN);
3978
3979 p2p.concurrent_operations = !!(wpa_s->drv_flags &
3980 WPA_DRIVER_FLAGS_P2P_CONCURRENT);
3981
3982 p2p.max_peers = 100;
3983
3984 if (wpa_s->conf->p2p_ssid_postfix) {
3985 p2p.ssid_postfix_len =
3986 os_strlen(wpa_s->conf->p2p_ssid_postfix);
3987 if (p2p.ssid_postfix_len > sizeof(p2p.ssid_postfix))
3988 p2p.ssid_postfix_len = sizeof(p2p.ssid_postfix);
3989 os_memcpy(p2p.ssid_postfix, wpa_s->conf->p2p_ssid_postfix,
3990 p2p.ssid_postfix_len);
3991 }
3992
3993 p2p.p2p_intra_bss = wpa_s->conf->p2p_intra_bss;
3994
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08003995 p2p.max_listen = wpa_s->max_remain_on_chan;
3996
Dmitry Shmidt2271d3f2014-06-23 12:16:31 -07003997 if (wpa_s->conf->p2p_passphrase_len >= 8 &&
3998 wpa_s->conf->p2p_passphrase_len <= 63)
3999 p2p.passphrase_len = wpa_s->conf->p2p_passphrase_len;
4000 else
4001 p2p.passphrase_len = 8;
4002
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004003 global->p2p = p2p_init(&p2p);
4004 if (global->p2p == NULL)
4005 return -1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07004006 global->p2p_init_wpa_s = wpa_s;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004007
4008 for (i = 0; i < MAX_WPS_VENDOR_EXT; i++) {
4009 if (wpa_s->conf->wps_vendor_ext[i] == NULL)
4010 continue;
4011 p2p_add_wps_vendor_extension(
4012 global->p2p, wpa_s->conf->wps_vendor_ext[i]);
4013 }
4014
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004015 p2p_set_no_go_freq(global->p2p, &wpa_s->conf->p2p_no_go_freq);
4016
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004017 return 0;
4018}
4019
4020
4021/**
4022 * wpas_p2p_deinit - Deinitialize per-interface P2P data
4023 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
4024 *
4025 * This function deinitialize per-interface P2P data.
4026 */
4027void wpas_p2p_deinit(struct wpa_supplicant *wpa_s)
4028{
4029 if (wpa_s->driver && wpa_s->drv_priv)
4030 wpa_drv_probe_req_report(wpa_s, 0);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004031
4032 if (wpa_s->go_params) {
4033 /* Clear any stored provisioning info */
4034 p2p_clear_provisioning_info(
4035 wpa_s->global->p2p,
Dmitry Shmidt04949592012-07-19 12:16:46 -07004036 wpa_s->go_params->peer_device_addr);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004037 }
4038
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004039 os_free(wpa_s->go_params);
4040 wpa_s->go_params = NULL;
Dmitry Shmidt684785c2014-05-12 13:34:29 -07004041 eloop_cancel_timeout(wpas_p2p_psk_failure_removal, wpa_s, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004042 eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s, NULL);
4043 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
4044 wpa_s->p2p_long_listen = 0;
4045 eloop_cancel_timeout(wpas_p2p_long_listen_timeout, wpa_s, NULL);
4046 eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL);
4047 wpas_p2p_remove_pending_group_interface(wpa_s);
Dmitry Shmidt04f534e2013-12-09 15:50:16 -08004048 eloop_cancel_timeout(wpas_p2p_group_freq_conflict, wpa_s, NULL);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08004049 wpas_p2p_listen_work_done(wpa_s);
4050 if (wpa_s->p2p_send_action_work) {
4051 os_free(wpa_s->p2p_send_action_work->ctx);
4052 radio_work_done(wpa_s->p2p_send_action_work);
4053 wpa_s->p2p_send_action_work = NULL;
4054 }
4055 eloop_cancel_timeout(wpas_p2p_send_action_work_timeout, wpa_s, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004056
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08004057 wpabuf_free(wpa_s->p2p_oob_dev_pw);
4058 wpa_s->p2p_oob_dev_pw = NULL;
4059
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004060 /* TODO: remove group interface from the driver if this wpa_s instance
4061 * is on top of a P2P group interface */
4062}
4063
4064
4065/**
4066 * wpas_p2p_deinit_global - Deinitialize global P2P module
4067 * @global: Pointer to global data from wpa_supplicant_init()
4068 *
4069 * This function deinitializes the global (per device) P2P module.
4070 */
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07004071static void wpas_p2p_deinit_global(struct wpa_global *global)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004072{
4073 struct wpa_supplicant *wpa_s, *tmp;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004074
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004075 wpa_s = global->ifaces;
4076 if (wpa_s)
4077 wpas_p2p_service_flush(wpa_s);
4078
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004079 if (global->p2p == NULL)
4080 return;
4081
4082 /* Remove remaining P2P group interfaces */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004083 while (wpa_s && wpa_s->p2p_group_interface != NOT_P2P_GROUP_INTERFACE)
4084 wpa_s = wpa_s->next;
4085 while (wpa_s) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004086 tmp = global->ifaces;
4087 while (tmp &&
4088 (tmp == wpa_s ||
4089 tmp->p2p_group_interface == NOT_P2P_GROUP_INTERFACE)) {
4090 tmp = tmp->next;
4091 }
4092 if (tmp == NULL)
4093 break;
Dmitry Shmidt04949592012-07-19 12:16:46 -07004094 /* Disconnect from the P2P group and deinit the interface */
4095 wpas_p2p_disconnect(tmp);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004096 }
4097
4098 /*
4099 * Deinit GO data on any possibly remaining interface (if main
4100 * interface is used as GO).
4101 */
4102 for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
4103 if (wpa_s->ap_iface)
4104 wpas_p2p_group_deinit(wpa_s);
4105 }
4106
4107 p2p_deinit(global->p2p);
4108 global->p2p = NULL;
Dmitry Shmidt04949592012-07-19 12:16:46 -07004109 global->p2p_init_wpa_s = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004110}
4111
4112
4113static int wpas_p2p_create_iface(struct wpa_supplicant *wpa_s)
4114{
Dmitry Shmidt34af3062013-07-11 10:46:32 -07004115 if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_DEDICATED_P2P_DEVICE) &&
4116 wpa_s->conf->p2p_no_group_iface)
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08004117 return 0; /* separate interface disabled per configuration */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004118 if (wpa_s->drv_flags &
4119 (WPA_DRIVER_FLAGS_P2P_DEDICATED_INTERFACE |
4120 WPA_DRIVER_FLAGS_P2P_MGMT_AND_NON_P2P))
4121 return 1; /* P2P group requires a new interface in every case
4122 */
4123 if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_CONCURRENT))
4124 return 0; /* driver does not support concurrent operations */
4125 if (wpa_s->global->ifaces->next)
4126 return 1; /* more that one interface already in use */
4127 if (wpa_s->wpa_state >= WPA_AUTHENTICATING)
4128 return 1; /* this interface is already in use */
4129 return 0;
4130}
4131
4132
4133static int wpas_p2p_start_go_neg(struct wpa_supplicant *wpa_s,
4134 const u8 *peer_addr,
4135 enum p2p_wps_method wps_method,
4136 int go_intent, const u8 *own_interface_addr,
Dmitry Shmidt04949592012-07-19 12:16:46 -07004137 unsigned int force_freq, int persistent_group,
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08004138 struct wpa_ssid *ssid, unsigned int pref_freq)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004139{
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004140 if (persistent_group && wpa_s->conf->persistent_reconnect)
4141 persistent_group = 2;
4142
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004143 /*
4144 * Increase GO config timeout if HT40 is used since it takes some time
4145 * to scan channels for coex purposes before the BSS can be started.
4146 */
4147 p2p_set_config_timeout(wpa_s->global->p2p,
4148 wpa_s->p2p_go_ht40 ? 255 : 100, 20);
4149
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004150 return p2p_connect(wpa_s->global->p2p, peer_addr, wps_method,
4151 go_intent, own_interface_addr, force_freq,
Dmitry Shmidt04949592012-07-19 12:16:46 -07004152 persistent_group, ssid ? ssid->ssid : NULL,
4153 ssid ? ssid->ssid_len : 0,
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08004154 wpa_s->p2p_pd_before_go_neg, pref_freq,
4155 wps_method == WPS_NFC ? wpa_s->p2p_oob_dev_pw_id :
4156 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004157}
4158
4159
4160static int wpas_p2p_auth_go_neg(struct wpa_supplicant *wpa_s,
4161 const u8 *peer_addr,
4162 enum p2p_wps_method wps_method,
4163 int go_intent, const u8 *own_interface_addr,
Dmitry Shmidt04949592012-07-19 12:16:46 -07004164 unsigned int force_freq, int persistent_group,
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08004165 struct wpa_ssid *ssid, unsigned int pref_freq)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004166{
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004167 if (persistent_group && wpa_s->conf->persistent_reconnect)
4168 persistent_group = 2;
4169
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004170 return p2p_authorize(wpa_s->global->p2p, peer_addr, wps_method,
4171 go_intent, own_interface_addr, force_freq,
Dmitry Shmidt04949592012-07-19 12:16:46 -07004172 persistent_group, ssid ? ssid->ssid : NULL,
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08004173 ssid ? ssid->ssid_len : 0, pref_freq,
4174 wps_method == WPS_NFC ? wpa_s->p2p_oob_dev_pw_id :
4175 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004176}
4177
4178
4179static void wpas_p2p_check_join_scan_limit(struct wpa_supplicant *wpa_s)
4180{
4181 wpa_s->p2p_join_scan_count++;
4182 wpa_printf(MSG_DEBUG, "P2P: Join scan attempt %d",
4183 wpa_s->p2p_join_scan_count);
4184 if (wpa_s->p2p_join_scan_count > P2P_MAX_JOIN_SCAN_ATTEMPTS) {
4185 wpa_printf(MSG_DEBUG, "P2P: Failed to find GO " MACSTR
4186 " for join operationg - stop join attempt",
4187 MAC2STR(wpa_s->pending_join_iface_addr));
4188 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
Dmitry Shmidt04949592012-07-19 12:16:46 -07004189 if (wpa_s->p2p_auto_pd) {
4190 wpa_s->p2p_auto_pd = 0;
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07004191 wpa_msg_global(wpa_s, MSG_INFO,
4192 P2P_EVENT_PROV_DISC_FAILURE
4193 " p2p_dev_addr=" MACSTR " status=N/A",
4194 MAC2STR(wpa_s->pending_join_dev_addr));
Dmitry Shmidt04949592012-07-19 12:16:46 -07004195 return;
4196 }
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07004197 wpa_msg_global(wpa_s->parent, MSG_INFO,
4198 P2P_EVENT_GROUP_FORMATION_FAILURE);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004199 }
4200}
4201
4202
Dmitry Shmidt04949592012-07-19 12:16:46 -07004203static int wpas_check_freq_conflict(struct wpa_supplicant *wpa_s, int freq)
4204{
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07004205 int res;
4206 unsigned int num, i;
4207 struct wpa_used_freq_data *freqs;
Dmitry Shmidt04949592012-07-19 12:16:46 -07004208
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07004209 if (wpas_p2p_num_unused_channels(wpa_s) > 0) {
4210 /* Multiple channels are supported and not all are in use */
Dmitry Shmidt04949592012-07-19 12:16:46 -07004211 return 0;
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07004212 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07004213
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07004214 freqs = os_calloc(wpa_s->num_multichan_concurrent,
4215 sizeof(struct wpa_used_freq_data));
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07004216 if (!freqs)
4217 return 1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07004218
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07004219 num = wpas_p2p_valid_oper_freqs(wpa_s, freqs,
4220 wpa_s->num_multichan_concurrent);
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07004221
4222 for (i = 0; i < num; i++) {
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07004223 if (freqs[i].freq == freq) {
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07004224 wpa_printf(MSG_DEBUG, "P2P: Frequency %d MHz in use by another virtual interface and can be used",
4225 freq);
4226 res = 0;
4227 goto exit_free;
Dmitry Shmidt04949592012-07-19 12:16:46 -07004228 }
4229 }
4230
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07004231 wpa_printf(MSG_DEBUG, "P2P: No valid operating frequencies");
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07004232 res = 1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07004233
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07004234exit_free:
4235 os_free(freqs);
4236 return res;
Dmitry Shmidt04949592012-07-19 12:16:46 -07004237}
4238
4239
4240static int wpas_p2p_peer_go(struct wpa_supplicant *wpa_s,
4241 const u8 *peer_dev_addr)
4242{
4243 struct wpa_bss *bss;
4244 int updated;
4245
4246 bss = wpa_bss_get_p2p_dev_addr(wpa_s, peer_dev_addr);
4247 if (bss == NULL)
4248 return -1;
4249 if (bss->last_update_idx < wpa_s->bss_update_idx) {
4250 wpa_printf(MSG_DEBUG, "P2P: Peer BSS entry not updated in the "
4251 "last scan");
4252 return 0;
4253 }
4254
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08004255 updated = os_reltime_before(&wpa_s->p2p_auto_started,
4256 &bss->last_update);
Dmitry Shmidt04949592012-07-19 12:16:46 -07004257 wpa_printf(MSG_DEBUG, "P2P: Current BSS entry for peer updated at "
4258 "%ld.%06ld (%supdated in last scan)",
4259 bss->last_update.sec, bss->last_update.usec,
4260 updated ? "": "not ");
4261
4262 return updated;
4263}
4264
4265
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004266static void wpas_p2p_scan_res_join(struct wpa_supplicant *wpa_s,
4267 struct wpa_scan_results *scan_res)
4268{
Dmitry Shmidt344abd32014-01-14 13:17:00 -08004269 struct wpa_bss *bss = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004270 int freq;
4271 u8 iface_addr[ETH_ALEN];
Dmitry Shmidt04949592012-07-19 12:16:46 -07004272
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004273 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
4274
4275 if (wpa_s->global->p2p_disabled)
4276 return;
4277
Dmitry Shmidt04949592012-07-19 12:16:46 -07004278 wpa_printf(MSG_DEBUG, "P2P: Scan results received (%d BSS) for %sjoin",
4279 scan_res ? (int) scan_res->num : -1,
4280 wpa_s->p2p_auto_join ? "auto_" : "");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004281
4282 if (scan_res)
4283 wpas_p2p_scan_res_handler(wpa_s, scan_res);
4284
Dmitry Shmidt04949592012-07-19 12:16:46 -07004285 if (wpa_s->p2p_auto_pd) {
4286 int join = wpas_p2p_peer_go(wpa_s,
4287 wpa_s->pending_join_dev_addr);
4288 if (join == 0 &&
4289 wpa_s->auto_pd_scan_retry < P2P_AUTO_PD_SCAN_ATTEMPTS) {
4290 wpa_s->auto_pd_scan_retry++;
Dmitry Shmidt444d5672013-04-01 13:08:44 -07004291 bss = wpa_bss_get_bssid_latest(
4292 wpa_s, wpa_s->pending_join_dev_addr);
Dmitry Shmidt04949592012-07-19 12:16:46 -07004293 if (bss) {
4294 freq = bss->freq;
4295 wpa_printf(MSG_DEBUG, "P2P: Scan retry %d for "
4296 "the peer " MACSTR " at %d MHz",
4297 wpa_s->auto_pd_scan_retry,
4298 MAC2STR(wpa_s->
4299 pending_join_dev_addr),
4300 freq);
Dmitry Shmidt344abd32014-01-14 13:17:00 -08004301 wpas_p2p_join_scan_req(wpa_s, freq, NULL, 0);
Dmitry Shmidt04949592012-07-19 12:16:46 -07004302 return;
4303 }
4304 }
4305
4306 if (join < 0)
4307 join = 0;
4308
4309 wpa_s->p2p_auto_pd = 0;
4310 wpa_s->pending_pd_use = join ? AUTO_PD_JOIN : AUTO_PD_GO_NEG;
4311 wpa_printf(MSG_DEBUG, "P2P: Auto PD with " MACSTR " join=%d",
4312 MAC2STR(wpa_s->pending_join_dev_addr), join);
4313 if (p2p_prov_disc_req(wpa_s->global->p2p,
4314 wpa_s->pending_join_dev_addr,
4315 wpa_s->pending_pd_config_methods, join,
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08004316 0, wpa_s->user_initiated_pd) < 0) {
Dmitry Shmidt04949592012-07-19 12:16:46 -07004317 wpa_s->p2p_auto_pd = 0;
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07004318 wpa_msg_global(wpa_s, MSG_INFO,
4319 P2P_EVENT_PROV_DISC_FAILURE
4320 " p2p_dev_addr=" MACSTR " status=N/A",
4321 MAC2STR(wpa_s->pending_join_dev_addr));
Dmitry Shmidt04949592012-07-19 12:16:46 -07004322 }
4323 return;
4324 }
4325
4326 if (wpa_s->p2p_auto_join) {
4327 int join = wpas_p2p_peer_go(wpa_s,
4328 wpa_s->pending_join_dev_addr);
4329 if (join < 0) {
4330 wpa_printf(MSG_DEBUG, "P2P: Peer was not found to be "
4331 "running a GO -> use GO Negotiation");
4332 wpas_p2p_connect(wpa_s, wpa_s->pending_join_dev_addr,
4333 wpa_s->p2p_pin, wpa_s->p2p_wps_method,
4334 wpa_s->p2p_persistent_group, 0, 0, 0,
4335 wpa_s->p2p_go_intent,
4336 wpa_s->p2p_connect_freq,
4337 wpa_s->p2p_persistent_id,
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004338 wpa_s->p2p_pd_before_go_neg,
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004339 wpa_s->p2p_go_ht40,
4340 wpa_s->p2p_go_vht);
Dmitry Shmidt04949592012-07-19 12:16:46 -07004341 return;
4342 }
4343
4344 wpa_printf(MSG_DEBUG, "P2P: Peer was found running GO%s -> "
4345 "try to join the group", join ? "" :
4346 " in older scan");
4347 if (!join)
4348 wpa_s->p2p_fallback_to_go_neg = 1;
4349 }
4350
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004351 freq = p2p_get_oper_freq(wpa_s->global->p2p,
4352 wpa_s->pending_join_iface_addr);
4353 if (freq < 0 &&
4354 p2p_get_interface_addr(wpa_s->global->p2p,
4355 wpa_s->pending_join_dev_addr,
4356 iface_addr) == 0 &&
4357 os_memcmp(iface_addr, wpa_s->pending_join_dev_addr, ETH_ALEN) != 0)
4358 {
4359 wpa_printf(MSG_DEBUG, "P2P: Overwrite pending interface "
4360 "address for join from " MACSTR " to " MACSTR
4361 " based on newly discovered P2P peer entry",
4362 MAC2STR(wpa_s->pending_join_iface_addr),
4363 MAC2STR(iface_addr));
4364 os_memcpy(wpa_s->pending_join_iface_addr, iface_addr,
4365 ETH_ALEN);
4366
4367 freq = p2p_get_oper_freq(wpa_s->global->p2p,
4368 wpa_s->pending_join_iface_addr);
4369 }
4370 if (freq >= 0) {
4371 wpa_printf(MSG_DEBUG, "P2P: Target GO operating frequency "
4372 "from P2P peer table: %d MHz", freq);
4373 }
Dmitry Shmidt344abd32014-01-14 13:17:00 -08004374 if (wpa_s->p2p_join_ssid_len) {
4375 wpa_printf(MSG_DEBUG, "P2P: Trying to find target GO BSS entry based on BSSID "
4376 MACSTR " and SSID %s",
4377 MAC2STR(wpa_s->pending_join_iface_addr),
4378 wpa_ssid_txt(wpa_s->p2p_join_ssid,
4379 wpa_s->p2p_join_ssid_len));
4380 bss = wpa_bss_get(wpa_s, wpa_s->pending_join_iface_addr,
4381 wpa_s->p2p_join_ssid,
4382 wpa_s->p2p_join_ssid_len);
4383 }
4384 if (!bss) {
4385 wpa_printf(MSG_DEBUG, "P2P: Trying to find target GO BSS entry based on BSSID "
4386 MACSTR, MAC2STR(wpa_s->pending_join_iface_addr));
4387 bss = wpa_bss_get_bssid_latest(wpa_s,
4388 wpa_s->pending_join_iface_addr);
4389 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004390 if (bss) {
4391 freq = bss->freq;
4392 wpa_printf(MSG_DEBUG, "P2P: Target GO operating frequency "
Dmitry Shmidt051af732013-10-22 13:52:46 -07004393 "from BSS table: %d MHz (SSID %s)", freq,
4394 wpa_ssid_txt(bss->ssid, bss->ssid_len));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004395 }
4396 if (freq > 0) {
4397 u16 method;
4398
Dmitry Shmidt04949592012-07-19 12:16:46 -07004399 if (wpas_check_freq_conflict(wpa_s, freq) > 0) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07004400 wpa_msg_global(wpa_s->parent, MSG_INFO,
4401 P2P_EVENT_GROUP_FORMATION_FAILURE
4402 "reason=FREQ_CONFLICT");
Dmitry Shmidt04949592012-07-19 12:16:46 -07004403 return;
4404 }
4405
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004406 wpa_printf(MSG_DEBUG, "P2P: Send Provision Discovery Request "
4407 "prior to joining an existing group (GO " MACSTR
4408 " freq=%u MHz)",
4409 MAC2STR(wpa_s->pending_join_dev_addr), freq);
4410 wpa_s->pending_pd_before_join = 1;
4411
4412 switch (wpa_s->pending_join_wps_method) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004413 case WPS_PIN_DISPLAY:
4414 method = WPS_CONFIG_KEYPAD;
4415 break;
4416 case WPS_PIN_KEYPAD:
4417 method = WPS_CONFIG_DISPLAY;
4418 break;
4419 case WPS_PBC:
4420 method = WPS_CONFIG_PUSHBUTTON;
4421 break;
4422 default:
4423 method = 0;
4424 break;
4425 }
4426
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004427 if ((p2p_get_provisioning_info(wpa_s->global->p2p,
4428 wpa_s->pending_join_dev_addr) ==
4429 method)) {
4430 /*
4431 * We have already performed provision discovery for
4432 * joining the group. Proceed directly to join
4433 * operation without duplicated provision discovery. */
4434 wpa_printf(MSG_DEBUG, "P2P: Provision discovery "
4435 "with " MACSTR " already done - proceed to "
4436 "join",
4437 MAC2STR(wpa_s->pending_join_dev_addr));
4438 wpa_s->pending_pd_before_join = 0;
4439 goto start;
4440 }
4441
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004442 if (p2p_prov_disc_req(wpa_s->global->p2p,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004443 wpa_s->pending_join_dev_addr, method, 1,
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08004444 freq, wpa_s->user_initiated_pd) < 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004445 wpa_printf(MSG_DEBUG, "P2P: Failed to send Provision "
4446 "Discovery Request before joining an "
4447 "existing group");
4448 wpa_s->pending_pd_before_join = 0;
4449 goto start;
4450 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004451 return;
4452 }
4453
4454 wpa_printf(MSG_DEBUG, "P2P: Failed to find BSS/GO - try again later");
4455 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
4456 eloop_register_timeout(1, 0, wpas_p2p_join_scan, wpa_s, NULL);
4457 wpas_p2p_check_join_scan_limit(wpa_s);
4458 return;
4459
4460start:
4461 /* Start join operation immediately */
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08004462 wpas_p2p_join_start(wpa_s, 0, NULL, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004463}
4464
4465
Dmitry Shmidt344abd32014-01-14 13:17:00 -08004466static void wpas_p2p_join_scan_req(struct wpa_supplicant *wpa_s, int freq,
4467 const u8 *ssid, size_t ssid_len)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004468{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004469 int ret;
4470 struct wpa_driver_scan_params params;
4471 struct wpabuf *wps_ie, *ies;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004472 size_t ielen;
Dmitry Shmidt04949592012-07-19 12:16:46 -07004473 int freqs[2] = { 0, 0 };
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004474
4475 os_memset(&params, 0, sizeof(params));
4476
4477 /* P2P Wildcard SSID */
4478 params.num_ssids = 1;
Dmitry Shmidt344abd32014-01-14 13:17:00 -08004479 if (ssid && ssid_len) {
4480 params.ssids[0].ssid = ssid;
4481 params.ssids[0].ssid_len = ssid_len;
4482 os_memcpy(wpa_s->p2p_join_ssid, ssid, ssid_len);
4483 wpa_s->p2p_join_ssid_len = ssid_len;
4484 } else {
4485 params.ssids[0].ssid = (u8 *) P2P_WILDCARD_SSID;
4486 params.ssids[0].ssid_len = P2P_WILDCARD_SSID_LEN;
4487 wpa_s->p2p_join_ssid_len = 0;
4488 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004489
4490 wpa_s->wps->dev.p2p = 1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07004491 wps_ie = wps_build_probe_req_ie(DEV_PW_DEFAULT, &wpa_s->wps->dev,
4492 wpa_s->wps->uuid, WPS_REQ_ENROLLEE, 0,
4493 NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004494 if (wps_ie == NULL) {
4495 wpas_p2p_scan_res_join(wpa_s, NULL);
4496 return;
4497 }
4498
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004499 ielen = p2p_scan_ie_buf_len(wpa_s->global->p2p);
4500 ies = wpabuf_alloc(wpabuf_len(wps_ie) + ielen);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004501 if (ies == NULL) {
4502 wpabuf_free(wps_ie);
4503 wpas_p2p_scan_res_join(wpa_s, NULL);
4504 return;
4505 }
4506 wpabuf_put_buf(ies, wps_ie);
4507 wpabuf_free(wps_ie);
4508
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08004509 p2p_scan_ie(wpa_s->global->p2p, ies, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004510
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004511 params.p2p_probe = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004512 params.extra_ies = wpabuf_head(ies);
4513 params.extra_ies_len = wpabuf_len(ies);
Dmitry Shmidt9e3f8ee2014-01-17 10:52:01 -08004514
4515 if (!freq) {
4516 int oper_freq;
4517 /*
4518 * If freq is not provided, check the operating freq of the GO
4519 * and use a single channel scan on if possible.
4520 */
4521 oper_freq = p2p_get_oper_freq(wpa_s->global->p2p,
4522 wpa_s->pending_join_iface_addr);
4523 if (oper_freq > 0)
4524 freq = oper_freq;
4525 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07004526 if (freq > 0) {
4527 freqs[0] = freq;
4528 params.freqs = freqs;
4529 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004530
4531 /*
4532 * Run a scan to update BSS table and start Provision Discovery once
4533 * the new scan results become available.
4534 */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004535 ret = wpa_drv_scan(wpa_s, &params);
Dmitry Shmidt444d5672013-04-01 13:08:44 -07004536 if (!ret) {
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08004537 os_get_reltime(&wpa_s->scan_trigger_time);
Dmitry Shmidt04949592012-07-19 12:16:46 -07004538 wpa_s->scan_res_handler = wpas_p2p_scan_res_join;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08004539 wpa_s->own_scan_requested = 1;
Dmitry Shmidt444d5672013-04-01 13:08:44 -07004540 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004541
4542 wpabuf_free(ies);
4543
4544 if (ret) {
4545 wpa_printf(MSG_DEBUG, "P2P: Failed to start scan for join - "
4546 "try again later");
4547 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
4548 eloop_register_timeout(1, 0, wpas_p2p_join_scan, wpa_s, NULL);
4549 wpas_p2p_check_join_scan_limit(wpa_s);
4550 }
4551}
4552
4553
Dmitry Shmidt04949592012-07-19 12:16:46 -07004554static void wpas_p2p_join_scan(void *eloop_ctx, void *timeout_ctx)
4555{
4556 struct wpa_supplicant *wpa_s = eloop_ctx;
Dmitry Shmidt344abd32014-01-14 13:17:00 -08004557 wpas_p2p_join_scan_req(wpa_s, 0, NULL, 0);
Dmitry Shmidt04949592012-07-19 12:16:46 -07004558}
4559
4560
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004561static int wpas_p2p_join(struct wpa_supplicant *wpa_s, const u8 *iface_addr,
Dmitry Shmidt04949592012-07-19 12:16:46 -07004562 const u8 *dev_addr, enum p2p_wps_method wps_method,
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08004563 int auto_join, int op_freq,
4564 const u8 *ssid, size_t ssid_len)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004565{
4566 wpa_printf(MSG_DEBUG, "P2P: Request to join existing group (iface "
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08004567 MACSTR " dev " MACSTR " op_freq=%d)%s",
4568 MAC2STR(iface_addr), MAC2STR(dev_addr), op_freq,
Dmitry Shmidt04949592012-07-19 12:16:46 -07004569 auto_join ? " (auto_join)" : "");
Dmitry Shmidt344abd32014-01-14 13:17:00 -08004570 if (ssid && ssid_len) {
4571 wpa_printf(MSG_DEBUG, "P2P: Group SSID specified: %s",
4572 wpa_ssid_txt(ssid, ssid_len));
4573 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004574
Dmitry Shmidt04949592012-07-19 12:16:46 -07004575 wpa_s->p2p_auto_pd = 0;
4576 wpa_s->p2p_auto_join = !!auto_join;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004577 os_memcpy(wpa_s->pending_join_iface_addr, iface_addr, ETH_ALEN);
4578 os_memcpy(wpa_s->pending_join_dev_addr, dev_addr, ETH_ALEN);
4579 wpa_s->pending_join_wps_method = wps_method;
4580
4581 /* Make sure we are not running find during connection establishment */
4582 wpas_p2p_stop_find(wpa_s);
4583
4584 wpa_s->p2p_join_scan_count = 0;
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08004585 wpas_p2p_join_scan_req(wpa_s, op_freq, ssid, ssid_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004586 return 0;
4587}
4588
4589
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08004590static int wpas_p2p_join_start(struct wpa_supplicant *wpa_s, int freq,
4591 const u8 *ssid, size_t ssid_len)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004592{
4593 struct wpa_supplicant *group;
4594 struct p2p_go_neg_results res;
Dmitry Shmidt04949592012-07-19 12:16:46 -07004595 struct wpa_bss *bss;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004596
4597 group = wpas_p2p_get_group_iface(wpa_s, 0, 0);
4598 if (group == NULL)
4599 return -1;
4600 if (group != wpa_s) {
4601 os_memcpy(group->p2p_pin, wpa_s->p2p_pin,
4602 sizeof(group->p2p_pin));
4603 group->p2p_wps_method = wpa_s->p2p_wps_method;
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08004604 } else {
4605 /*
4606 * Need to mark the current interface for p2p_group_formation
4607 * when a separate group interface is not used. This is needed
4608 * to allow p2p_cancel stop a pending p2p_connect-join.
4609 * wpas_p2p_init_group_interface() addresses this for the case
4610 * where a separate group interface is used.
4611 */
4612 wpa_s->global->p2p_group_formation = wpa_s;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004613 }
4614
4615 group->p2p_in_provisioning = 1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07004616 group->p2p_fallback_to_go_neg = wpa_s->p2p_fallback_to_go_neg;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004617
4618 os_memset(&res, 0, sizeof(res));
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08004619 os_memcpy(res.peer_device_addr, wpa_s->pending_join_dev_addr, ETH_ALEN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004620 os_memcpy(res.peer_interface_addr, wpa_s->pending_join_iface_addr,
4621 ETH_ALEN);
4622 res.wps_method = wpa_s->pending_join_wps_method;
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08004623 if (freq && ssid && ssid_len) {
4624 res.freq = freq;
4625 res.ssid_len = ssid_len;
4626 os_memcpy(res.ssid, ssid, ssid_len);
4627 } else {
4628 bss = wpa_bss_get_bssid_latest(wpa_s,
4629 wpa_s->pending_join_iface_addr);
4630 if (bss) {
4631 res.freq = bss->freq;
4632 res.ssid_len = bss->ssid_len;
4633 os_memcpy(res.ssid, bss->ssid, bss->ssid_len);
4634 wpa_printf(MSG_DEBUG, "P2P: Join target GO operating frequency from BSS table: %d MHz (SSID %s)",
4635 bss->freq,
4636 wpa_ssid_txt(bss->ssid, bss->ssid_len));
4637 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07004638 }
4639
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08004640 if (wpa_s->off_channel_freq || wpa_s->roc_waiting_drv_freq) {
4641 wpa_printf(MSG_DEBUG, "P2P: Cancel remain-on-channel prior to "
4642 "starting client");
4643 wpa_drv_cancel_remain_on_channel(wpa_s);
4644 wpa_s->off_channel_freq = 0;
4645 wpa_s->roc_waiting_drv_freq = 0;
4646 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004647 wpas_start_wps_enrollee(group, &res);
4648
4649 /*
4650 * Allow a longer timeout for join-a-running-group than normal 15
4651 * second group formation timeout since the GO may not have authorized
4652 * our connection yet.
4653 */
4654 eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s, NULL);
4655 eloop_register_timeout(60, 0, wpas_p2p_group_formation_timeout,
4656 wpa_s, NULL);
4657
4658 return 0;
4659}
4660
4661
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08004662static int wpas_p2p_setup_freqs(struct wpa_supplicant *wpa_s, int freq,
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004663 int *force_freq, int *pref_freq, int go)
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08004664{
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07004665 struct wpa_used_freq_data *freqs;
4666 int res, best_freq, num_unused;
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07004667 unsigned int freq_in_use = 0, num, i;
4668
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07004669 freqs = os_calloc(wpa_s->num_multichan_concurrent,
4670 sizeof(struct wpa_used_freq_data));
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07004671 if (!freqs)
4672 return -1;
4673
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07004674 num = wpas_p2p_valid_oper_freqs(wpa_s, freqs,
4675 wpa_s->num_multichan_concurrent);
4676
4677 /*
4678 * It is possible that the total number of used frequencies is bigger
4679 * than the number of frequencies used for P2P, so get the system wide
4680 * number of unused frequencies.
4681 */
4682 num_unused = wpas_p2p_num_unused_channels(wpa_s);
4683
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07004684 wpa_printf(MSG_DEBUG,
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07004685 "P2P: Setup freqs: freq=%d num_MCC=%d shared_freqs=%u num_unused=%d",
4686 freq, wpa_s->num_multichan_concurrent, num, num_unused);
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07004687
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08004688 if (freq > 0) {
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004689 int ret;
4690 if (go)
4691 ret = p2p_supported_freq(wpa_s->global->p2p, freq);
4692 else
4693 ret = p2p_supported_freq_cli(wpa_s->global->p2p, freq);
4694 if (!ret) {
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08004695 wpa_printf(MSG_DEBUG, "P2P: The forced channel "
4696 "(%u MHz) is not supported for P2P uses",
4697 freq);
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07004698 res = -3;
4699 goto exit_free;
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08004700 }
4701
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07004702 for (i = 0; i < num; i++) {
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07004703 if (freqs[i].freq == freq)
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07004704 freq_in_use = 1;
4705 }
4706
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07004707 if (num_unused <= 0 && !freq_in_use) {
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07004708 wpa_printf(MSG_DEBUG, "P2P: Cannot start P2P group on %u MHz as there are no available channels",
4709 freq);
4710 res = -2;
4711 goto exit_free;
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08004712 }
4713 wpa_printf(MSG_DEBUG, "P2P: Trying to force us to use the "
4714 "requested channel (%u MHz)", freq);
4715 *force_freq = freq;
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07004716 goto exit_ok;
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08004717 }
4718
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07004719 best_freq = wpas_p2p_pick_best_used_freq(wpa_s, freqs, num);
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07004720
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07004721 /* We have a candidate frequency to use */
4722 if (best_freq > 0) {
4723 if (*pref_freq == 0 && num_unused > 0) {
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08004724 wpa_printf(MSG_DEBUG, "P2P: Try to prefer a frequency (%u MHz) we are already using",
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07004725 best_freq);
4726 *pref_freq = best_freq;
Dmitry Shmidt51a47d52013-09-10 10:52:57 -07004727 } else {
4728 wpa_printf(MSG_DEBUG, "P2P: Try to force us to use frequency (%u MHz) which is already in use",
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07004729 best_freq);
4730 *force_freq = best_freq;
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07004731 }
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07004732 } else if (num_unused > 0) {
4733 wpa_printf(MSG_DEBUG,
4734 "P2P: Current operating channels are not available for P2P. Try to use another channel");
4735 *force_freq = 0;
4736 } else {
4737 wpa_printf(MSG_DEBUG,
4738 "P2P: All channels are in use and none of them are P2P enabled. Cannot start P2P group");
4739 res = -2;
4740 goto exit_free;
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07004741 }
4742
4743exit_ok:
4744 res = 0;
4745exit_free:
4746 os_free(freqs);
4747 return res;
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08004748}
4749
4750
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004751/**
4752 * wpas_p2p_connect - Request P2P Group Formation to be started
4753 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
4754 * @peer_addr: Address of the peer P2P Device
4755 * @pin: PIN to use during provisioning or %NULL to indicate PBC mode
4756 * @persistent_group: Whether to create a persistent group
Dmitry Shmidt04949592012-07-19 12:16:46 -07004757 * @auto_join: Whether to select join vs. GO Negotiation automatically
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004758 * @join: Whether to join an existing group (as a client) instead of starting
4759 * Group Owner negotiation; @peer_addr is BSSID in that case
4760 * @auth: Whether to only authorize the connection instead of doing that and
4761 * initiating Group Owner negotiation
4762 * @go_intent: GO Intent or -1 to use default
4763 * @freq: Frequency for the group or 0 for auto-selection
Dmitry Shmidt04949592012-07-19 12:16:46 -07004764 * @persistent_id: Persistent group credentials to use for forcing GO
4765 * parameters or -1 to generate new values (SSID/passphrase)
4766 * @pd: Whether to send Provision Discovery prior to GO Negotiation as an
4767 * interoperability workaround when initiating group formation
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004768 * @ht40: Start GO with 40 MHz channel width
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004769 * @vht: Start GO with VHT support
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004770 * Returns: 0 or new PIN (if pin was %NULL) on success, -1 on unspecified
4771 * failure, -2 on failure due to channel not currently available,
4772 * -3 if forced channel is not supported
4773 */
4774int wpas_p2p_connect(struct wpa_supplicant *wpa_s, const u8 *peer_addr,
4775 const char *pin, enum p2p_wps_method wps_method,
Dmitry Shmidt04949592012-07-19 12:16:46 -07004776 int persistent_group, int auto_join, int join, int auth,
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004777 int go_intent, int freq, int persistent_id, int pd,
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004778 int ht40, int vht)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004779{
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07004780 int force_freq = 0, pref_freq = 0;
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08004781 int ret = 0, res;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004782 enum wpa_driver_if_type iftype;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004783 const u8 *if_addr;
Dmitry Shmidt04949592012-07-19 12:16:46 -07004784 struct wpa_ssid *ssid = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004785
4786 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
4787 return -1;
4788
Dmitry Shmidt04949592012-07-19 12:16:46 -07004789 if (persistent_id >= 0) {
4790 ssid = wpa_config_get_network(wpa_s->conf, persistent_id);
4791 if (ssid == NULL || ssid->disabled != 2 ||
4792 ssid->mode != WPAS_MODE_P2P_GO)
4793 return -1;
4794 }
4795
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07004796 os_free(wpa_s->global->add_psk);
4797 wpa_s->global->add_psk = NULL;
4798
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08004799 wpa_s->global->p2p_fail_on_wps_complete = 0;
4800
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004801 if (go_intent < 0)
4802 go_intent = wpa_s->conf->p2p_go_intent;
4803
4804 if (!auth)
4805 wpa_s->p2p_long_listen = 0;
4806
4807 wpa_s->p2p_wps_method = wps_method;
Dmitry Shmidt04949592012-07-19 12:16:46 -07004808 wpa_s->p2p_persistent_group = !!persistent_group;
4809 wpa_s->p2p_persistent_id = persistent_id;
4810 wpa_s->p2p_go_intent = go_intent;
4811 wpa_s->p2p_connect_freq = freq;
4812 wpa_s->p2p_fallback_to_go_neg = 0;
4813 wpa_s->p2p_pd_before_go_neg = !!pd;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004814 wpa_s->p2p_go_ht40 = !!ht40;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004815 wpa_s->p2p_go_vht = !!vht;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004816
4817 if (pin)
4818 os_strlcpy(wpa_s->p2p_pin, pin, sizeof(wpa_s->p2p_pin));
4819 else if (wps_method == WPS_PIN_DISPLAY) {
4820 ret = wps_generate_pin();
4821 os_snprintf(wpa_s->p2p_pin, sizeof(wpa_s->p2p_pin), "%08d",
4822 ret);
4823 wpa_printf(MSG_DEBUG, "P2P: Randomly generated PIN: %s",
4824 wpa_s->p2p_pin);
4825 } else
4826 wpa_s->p2p_pin[0] = '\0';
4827
Dmitry Shmidt04949592012-07-19 12:16:46 -07004828 if (join || auto_join) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004829 u8 iface_addr[ETH_ALEN], dev_addr[ETH_ALEN];
4830 if (auth) {
4831 wpa_printf(MSG_DEBUG, "P2P: Authorize invitation to "
4832 "connect a running group from " MACSTR,
4833 MAC2STR(peer_addr));
4834 os_memcpy(wpa_s->p2p_auth_invite, peer_addr, ETH_ALEN);
4835 return ret;
4836 }
4837 os_memcpy(dev_addr, peer_addr, ETH_ALEN);
4838 if (p2p_get_interface_addr(wpa_s->global->p2p, peer_addr,
4839 iface_addr) < 0) {
4840 os_memcpy(iface_addr, peer_addr, ETH_ALEN);
4841 p2p_get_dev_addr(wpa_s->global->p2p, peer_addr,
4842 dev_addr);
4843 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07004844 if (auto_join) {
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08004845 os_get_reltime(&wpa_s->p2p_auto_started);
Dmitry Shmidt04949592012-07-19 12:16:46 -07004846 wpa_printf(MSG_DEBUG, "P2P: Auto join started at "
4847 "%ld.%06ld",
4848 wpa_s->p2p_auto_started.sec,
4849 wpa_s->p2p_auto_started.usec);
4850 }
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08004851 wpa_s->user_initiated_pd = 1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07004852 if (wpas_p2p_join(wpa_s, iface_addr, dev_addr, wps_method,
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08004853 auto_join, freq, NULL, 0) < 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004854 return -1;
4855 return ret;
4856 }
4857
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004858 res = wpas_p2p_setup_freqs(wpa_s, freq, &force_freq, &pref_freq,
4859 go_intent == 15);
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08004860 if (res)
4861 return res;
Dmitry Shmidta0d265f2013-11-19 13:13:41 -08004862 wpas_p2p_set_own_freq_preference(wpa_s,
4863 force_freq ? force_freq : pref_freq);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004864
4865 wpa_s->create_p2p_iface = wpas_p2p_create_iface(wpa_s);
4866
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004867 if (wpa_s->create_p2p_iface) {
4868 /* Prepare to add a new interface for the group */
4869 iftype = WPA_IF_P2P_GROUP;
4870 if (go_intent == 15)
4871 iftype = WPA_IF_P2P_GO;
4872 if (wpas_p2p_add_group_interface(wpa_s, iftype) < 0) {
4873 wpa_printf(MSG_ERROR, "P2P: Failed to allocate a new "
4874 "interface for the group");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004875 return -1;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004876 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004877
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004878 if_addr = wpa_s->pending_interface_addr;
4879 } else
4880 if_addr = wpa_s->own_addr;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004881
4882 if (auth) {
4883 if (wpas_p2p_auth_go_neg(wpa_s, peer_addr, wps_method,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004884 go_intent, if_addr,
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08004885 force_freq, persistent_group, ssid,
4886 pref_freq) < 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004887 return -1;
4888 return ret;
4889 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004890
4891 if (wpas_p2p_start_go_neg(wpa_s, peer_addr, wps_method,
4892 go_intent, if_addr, force_freq,
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08004893 persistent_group, ssid, pref_freq) < 0) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004894 if (wpa_s->create_p2p_iface)
4895 wpas_p2p_remove_pending_group_interface(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004896 return -1;
4897 }
4898 return ret;
4899}
4900
4901
4902/**
4903 * wpas_p2p_remain_on_channel_cb - Indication of remain-on-channel start
4904 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
4905 * @freq: Frequency of the channel in MHz
4906 * @duration: Duration of the stay on the channel in milliseconds
4907 *
4908 * This callback is called when the driver indicates that it has started the
4909 * requested remain-on-channel duration.
4910 */
4911void wpas_p2p_remain_on_channel_cb(struct wpa_supplicant *wpa_s,
4912 unsigned int freq, unsigned int duration)
4913{
4914 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
4915 return;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004916 if (wpa_s->off_channel_freq == wpa_s->pending_listen_freq) {
4917 p2p_listen_cb(wpa_s->global->p2p, wpa_s->pending_listen_freq,
4918 wpa_s->pending_listen_duration);
4919 wpa_s->pending_listen_freq = 0;
Dmitry Shmidt7f93d6f2014-02-21 11:22:49 -08004920 } else {
4921 wpa_printf(MSG_DEBUG, "P2P: Ignore remain-on-channel callback (off_channel_freq=%u pending_listen_freq=%d freq=%u duration=%u)",
4922 wpa_s->off_channel_freq, wpa_s->pending_listen_freq,
4923 freq, duration);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004924 }
4925}
4926
4927
4928static int wpas_p2p_listen_start(struct wpa_supplicant *wpa_s,
4929 unsigned int timeout)
4930{
4931 /* Limit maximum Listen state time based on driver limitation. */
4932 if (timeout > wpa_s->max_remain_on_chan)
4933 timeout = wpa_s->max_remain_on_chan;
4934
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004935 return p2p_listen(wpa_s->global->p2p, timeout);
4936}
4937
4938
4939/**
4940 * wpas_p2p_cancel_remain_on_channel_cb - Remain-on-channel timeout
4941 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
4942 * @freq: Frequency of the channel in MHz
4943 *
4944 * This callback is called when the driver indicates that a remain-on-channel
4945 * operation has been completed, i.e., the duration on the requested channel
4946 * has timed out.
4947 */
4948void wpas_p2p_cancel_remain_on_channel_cb(struct wpa_supplicant *wpa_s,
4949 unsigned int freq)
4950{
4951 wpa_printf(MSG_DEBUG, "P2P: Cancel remain-on-channel callback "
4952 "(p2p_long_listen=%d ms pending_action_tx=%p)",
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08004953 wpa_s->p2p_long_listen, offchannel_pending_action_tx(wpa_s));
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08004954 wpas_p2p_listen_work_done(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004955 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
4956 return;
4957 if (p2p_listen_end(wpa_s->global->p2p, freq) > 0)
4958 return; /* P2P module started a new operation */
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08004959 if (offchannel_pending_action_tx(wpa_s))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004960 return;
4961 if (wpa_s->p2p_long_listen > 0)
4962 wpa_s->p2p_long_listen -= wpa_s->max_remain_on_chan;
4963 if (wpa_s->p2p_long_listen > 0) {
4964 wpa_printf(MSG_DEBUG, "P2P: Continuing long Listen state");
4965 wpas_p2p_listen_start(wpa_s, wpa_s->p2p_long_listen);
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07004966 } else {
4967 /*
4968 * When listen duration is over, stop listen & update p2p_state
4969 * to IDLE.
4970 */
4971 p2p_stop_listen(wpa_s->global->p2p);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004972 }
4973}
4974
4975
4976/**
4977 * wpas_p2p_group_remove - Remove a P2P group
4978 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
4979 * @ifname: Network interface name of the group interface or "*" to remove all
4980 * groups
4981 * Returns: 0 on success, -1 on failure
4982 *
4983 * This function is used to remove a P2P group. This can be used to disconnect
4984 * from a group in which the local end is a P2P Client or to end a P2P Group in
4985 * case the local end is the Group Owner. If a virtual network interface was
4986 * created for this group, that interface will be removed. Otherwise, only the
4987 * configured P2P group network will be removed from the interface.
4988 */
4989int wpas_p2p_group_remove(struct wpa_supplicant *wpa_s, const char *ifname)
4990{
4991 struct wpa_global *global = wpa_s->global;
4992
4993 if (os_strcmp(ifname, "*") == 0) {
4994 struct wpa_supplicant *prev;
4995 wpa_s = global->ifaces;
4996 while (wpa_s) {
4997 prev = wpa_s;
4998 wpa_s = wpa_s->next;
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07004999 if (prev->p2p_group_interface !=
5000 NOT_P2P_GROUP_INTERFACE ||
5001 (prev->current_ssid &&
5002 prev->current_ssid->p2p_group))
5003 wpas_p2p_disconnect(prev);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005004 }
5005 return 0;
5006 }
5007
5008 for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
5009 if (os_strcmp(wpa_s->ifname, ifname) == 0)
5010 break;
5011 }
5012
5013 return wpas_p2p_disconnect(wpa_s);
5014}
5015
5016
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07005017static int wpas_p2p_select_go_freq(struct wpa_supplicant *wpa_s, int freq)
5018{
5019 unsigned int r;
5020
5021 if (freq == 2) {
5022 wpa_printf(MSG_DEBUG, "P2P: Request to start GO on 2.4 GHz "
5023 "band");
5024 if (wpa_s->best_24_freq > 0 &&
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07005025 p2p_supported_freq_go(wpa_s->global->p2p,
5026 wpa_s->best_24_freq)) {
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07005027 freq = wpa_s->best_24_freq;
5028 wpa_printf(MSG_DEBUG, "P2P: Use best 2.4 GHz band "
5029 "channel: %d MHz", freq);
5030 } else {
5031 os_get_random((u8 *) &r, sizeof(r));
5032 freq = 2412 + (r % 3) * 25;
5033 wpa_printf(MSG_DEBUG, "P2P: Use random 2.4 GHz band "
5034 "channel: %d MHz", freq);
5035 }
5036 }
5037
5038 if (freq == 5) {
5039 wpa_printf(MSG_DEBUG, "P2P: Request to start GO on 5 GHz "
5040 "band");
5041 if (wpa_s->best_5_freq > 0 &&
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07005042 p2p_supported_freq_go(wpa_s->global->p2p,
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07005043 wpa_s->best_5_freq)) {
5044 freq = wpa_s->best_5_freq;
5045 wpa_printf(MSG_DEBUG, "P2P: Use best 5 GHz band "
5046 "channel: %d MHz", freq);
5047 } else {
5048 os_get_random((u8 *) &r, sizeof(r));
5049 freq = 5180 + (r % 4) * 20;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07005050 if (!p2p_supported_freq_go(wpa_s->global->p2p, freq)) {
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07005051 wpa_printf(MSG_DEBUG, "P2P: Could not select "
5052 "5 GHz channel for P2P group");
5053 return -1;
5054 }
5055 wpa_printf(MSG_DEBUG, "P2P: Use random 5 GHz band "
5056 "channel: %d MHz", freq);
5057 }
5058 }
5059
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07005060 if (freq > 0 && !p2p_supported_freq_go(wpa_s->global->p2p, freq)) {
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07005061 wpa_printf(MSG_DEBUG, "P2P: The forced channel for GO "
5062 "(%u MHz) is not supported for P2P uses",
5063 freq);
5064 return -1;
5065 }
5066
5067 return freq;
5068}
5069
5070
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07005071static int wpas_p2p_select_freq_no_pref(struct wpa_supplicant *wpa_s,
5072 struct p2p_go_neg_results *params,
5073 const struct p2p_channels *channels)
5074{
5075 unsigned int i, r;
5076
5077 /* first try some random selection of the social channels */
5078 os_get_random((u8 *) &r, sizeof(r));
5079
5080 for (i = 0; i < 3; i++) {
5081 params->freq = 2412 + ((r + i) % 3) * 25;
5082 if (!wpas_p2p_disallowed_freq(wpa_s->global, params->freq) &&
5083 freq_included(channels, params->freq))
5084 goto out;
5085 }
5086
5087 /* try all channels in reg. class 81 */
5088 for (i = 0; i < 11; i++) {
5089 params->freq = 2412 + i * 5;
5090 if (!wpas_p2p_disallowed_freq(wpa_s->global, params->freq) &&
5091 freq_included(channels, params->freq))
5092 goto out;
5093 }
5094
5095 wpa_printf(MSG_DEBUG, "P2P: No 2.4 GHz channel allowed");
5096 return -1;
5097out:
5098 wpa_printf(MSG_DEBUG, "P2P: Set GO freq %d MHz (no preference known)",
5099 params->freq);
5100 return 0;
5101}
5102
5103
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005104static int wpas_p2p_init_go_params(struct wpa_supplicant *wpa_s,
5105 struct p2p_go_neg_results *params,
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07005106 int freq, int ht40, int vht,
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08005107 const struct p2p_channels *channels)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005108{
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07005109 struct wpa_used_freq_data *freqs;
5110 unsigned int pref_freq, cand_freq;
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07005111 unsigned int num, i;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005112
5113 os_memset(params, 0, sizeof(*params));
5114 params->role_go = 1;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005115 params->ht40 = ht40;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07005116 params->vht = vht;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005117 if (freq) {
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08005118 if (!freq_included(channels, freq)) {
5119 wpa_printf(MSG_DEBUG, "P2P: Forced GO freq %d MHz not "
5120 "accepted", freq);
5121 return -1;
5122 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005123 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on forced "
5124 "frequency %d MHz", freq);
5125 params->freq = freq;
5126 } else if (wpa_s->conf->p2p_oper_reg_class == 81 &&
5127 wpa_s->conf->p2p_oper_channel >= 1 &&
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08005128 wpa_s->conf->p2p_oper_channel <= 11 &&
5129 freq_included(channels,
5130 2407 + 5 * wpa_s->conf->p2p_oper_channel)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005131 params->freq = 2407 + 5 * wpa_s->conf->p2p_oper_channel;
5132 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on configured "
5133 "frequency %d MHz", params->freq);
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08005134 } else if ((wpa_s->conf->p2p_oper_reg_class == 115 ||
5135 wpa_s->conf->p2p_oper_reg_class == 116 ||
5136 wpa_s->conf->p2p_oper_reg_class == 117 ||
5137 wpa_s->conf->p2p_oper_reg_class == 124 ||
5138 wpa_s->conf->p2p_oper_reg_class == 126 ||
5139 wpa_s->conf->p2p_oper_reg_class == 127) &&
5140 freq_included(channels,
5141 5000 + 5 * wpa_s->conf->p2p_oper_channel)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005142 params->freq = 5000 + 5 * wpa_s->conf->p2p_oper_channel;
5143 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on configured "
5144 "frequency %d MHz", params->freq);
5145 } else if (wpa_s->conf->p2p_oper_channel == 0 &&
5146 wpa_s->best_overall_freq > 0 &&
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07005147 p2p_supported_freq_go(wpa_s->global->p2p,
5148 wpa_s->best_overall_freq) &&
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08005149 freq_included(channels, wpa_s->best_overall_freq)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005150 params->freq = wpa_s->best_overall_freq;
5151 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on best overall "
5152 "channel %d MHz", params->freq);
5153 } else if (wpa_s->conf->p2p_oper_channel == 0 &&
5154 wpa_s->best_24_freq > 0 &&
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07005155 p2p_supported_freq_go(wpa_s->global->p2p,
5156 wpa_s->best_24_freq) &&
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08005157 freq_included(channels, wpa_s->best_24_freq)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005158 params->freq = wpa_s->best_24_freq;
5159 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on best 2.4 GHz "
5160 "channel %d MHz", params->freq);
5161 } else if (wpa_s->conf->p2p_oper_channel == 0 &&
5162 wpa_s->best_5_freq > 0 &&
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07005163 p2p_supported_freq_go(wpa_s->global->p2p,
5164 wpa_s->best_5_freq) &&
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08005165 freq_included(channels, wpa_s->best_5_freq)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005166 params->freq = wpa_s->best_5_freq;
5167 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on best 5 GHz "
5168 "channel %d MHz", params->freq);
Dmitry Shmidt44c95782013-05-17 09:51:35 -07005169 } else if ((pref_freq = p2p_get_pref_freq(wpa_s->global->p2p,
5170 channels))) {
5171 params->freq = pref_freq;
5172 wpa_printf(MSG_DEBUG, "P2P: Set GO freq %d MHz from preferred "
5173 "channels", params->freq);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005174 } else {
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07005175 /* no preference, select some channel */
5176 if (wpas_p2p_select_freq_no_pref(wpa_s, params, channels) < 0)
Dmitry Shmidt04949592012-07-19 12:16:46 -07005177 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005178 }
5179
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07005180 freqs = os_calloc(wpa_s->num_multichan_concurrent,
5181 sizeof(struct wpa_used_freq_data));
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07005182 if (!freqs)
5183 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005184
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07005185 num = wpas_p2p_valid_oper_freqs(wpa_s, freqs,
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07005186 wpa_s->num_multichan_concurrent);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005187
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07005188 cand_freq = wpas_p2p_pick_best_used_freq(wpa_s, freqs, num);
5189
5190 /* First try the best used frequency if possible */
5191 if (!freq && cand_freq > 0 && freq_included(channels, cand_freq)) {
5192 params->freq = cand_freq;
5193 } else if (!freq) {
5194 /* Try any of the used frequencies */
5195 for (i = 0; i < num; i++) {
5196 if (freq_included(channels, freqs[i].freq)) {
5197 wpa_printf(MSG_DEBUG, "P2P: Force GO on a channel we are already using (%u MHz)",
5198 freqs[i].freq);
5199 params->freq = freqs[i].freq;
5200 break;
5201 }
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07005202 }
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08005203
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07005204 if (i == num) {
5205 if (wpas_p2p_num_unused_channels(wpa_s) <= 0) {
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08005206 wpa_printf(MSG_DEBUG, "P2P: Cannot force GO on any of the channels we are already using");
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07005207 os_free(freqs);
5208 return -1;
5209 } else {
5210 wpa_printf(MSG_DEBUG, "P2P: Cannot force GO on any of the channels we are already using. Use one of the free channels");
5211 }
5212 }
5213 } else {
5214 for (i = 0; i < num; i++) {
5215 if (freqs[i].freq == freq)
5216 break;
5217 }
5218
5219 if (i == num) {
5220 if (wpas_p2p_num_unused_channels(wpa_s) <= 0) {
5221 if (freq)
5222 wpa_printf(MSG_DEBUG, "P2P: Cannot force GO on freq (%u MHz) as all the channels are in use", freq);
5223 os_free(freqs);
5224 return -1;
5225 } else {
5226 wpa_printf(MSG_DEBUG, "P2P: Use one of the free channels");
5227 }
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08005228 }
5229 }
5230
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07005231 os_free(freqs);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005232 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005233}
5234
5235
5236static struct wpa_supplicant *
5237wpas_p2p_get_group_iface(struct wpa_supplicant *wpa_s, int addr_allocated,
5238 int go)
5239{
5240 struct wpa_supplicant *group_wpa_s;
5241
Dmitry Shmidtaa532512012-09-24 10:35:31 -07005242 if (!wpas_p2p_create_iface(wpa_s)) {
5243 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Use same interface for group "
5244 "operations");
Dmitry Shmidt56052862013-10-04 10:23:25 -07005245 wpa_s->p2p_first_connection_timeout = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005246 return wpa_s;
Dmitry Shmidtaa532512012-09-24 10:35:31 -07005247 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005248
5249 if (wpas_p2p_add_group_interface(wpa_s, go ? WPA_IF_P2P_GO :
Dmitry Shmidtaa532512012-09-24 10:35:31 -07005250 WPA_IF_P2P_CLIENT) < 0) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07005251 wpa_msg_global(wpa_s, MSG_ERROR,
5252 "P2P: Failed to add group interface");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005253 return NULL;
Dmitry Shmidtaa532512012-09-24 10:35:31 -07005254 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005255 group_wpa_s = wpas_p2p_init_group_interface(wpa_s, go);
5256 if (group_wpa_s == NULL) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07005257 wpa_msg_global(wpa_s, MSG_ERROR,
5258 "P2P: Failed to initialize group interface");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005259 wpas_p2p_remove_pending_group_interface(wpa_s);
5260 return NULL;
5261 }
5262
Dmitry Shmidtaa532512012-09-24 10:35:31 -07005263 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Use separate group interface %s",
5264 group_wpa_s->ifname);
Dmitry Shmidt56052862013-10-04 10:23:25 -07005265 group_wpa_s->p2p_first_connection_timeout = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005266 return group_wpa_s;
5267}
5268
5269
5270/**
5271 * wpas_p2p_group_add - Add a new P2P group with local end as Group Owner
5272 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
5273 * @persistent_group: Whether to create a persistent group
5274 * @freq: Frequency for the group or 0 to indicate no hardcoding
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07005275 * @ht40: Start GO with 40 MHz channel width
5276 * @vht: Start GO with VHT support
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005277 * Returns: 0 on success, -1 on failure
5278 *
5279 * This function creates a new P2P group with the local end as the Group Owner,
5280 * i.e., without using Group Owner Negotiation.
5281 */
5282int wpas_p2p_group_add(struct wpa_supplicant *wpa_s, int persistent_group,
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07005283 int freq, int ht40, int vht)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005284{
5285 struct p2p_go_neg_results params;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005286
5287 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
5288 return -1;
5289
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07005290 os_free(wpa_s->global->add_psk);
5291 wpa_s->global->add_psk = NULL;
5292
Dmitry Shmidtdca39792011-09-06 11:17:33 -07005293 /* Make sure we are not running find during connection establishment */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005294 wpa_printf(MSG_DEBUG, "P2P: Stop any on-going P2P FIND");
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08005295 wpas_p2p_stop_find_oper(wpa_s);
Dmitry Shmidtdca39792011-09-06 11:17:33 -07005296
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07005297 freq = wpas_p2p_select_go_freq(wpa_s, freq);
5298 if (freq < 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005299 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005300
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07005301 if (wpas_p2p_init_go_params(wpa_s, &params, freq, ht40, vht, NULL))
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005302 return -1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07005303 if (params.freq &&
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07005304 !p2p_supported_freq_go(wpa_s->global->p2p, params.freq)) {
Dmitry Shmidt04949592012-07-19 12:16:46 -07005305 wpa_printf(MSG_DEBUG, "P2P: The selected channel for GO "
5306 "(%u MHz) is not supported for P2P uses",
5307 params.freq);
5308 return -1;
5309 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005310 p2p_go_params(wpa_s->global->p2p, &params);
5311 params.persistent_group = persistent_group;
5312
5313 wpa_s = wpas_p2p_get_group_iface(wpa_s, 0, 1);
5314 if (wpa_s == NULL)
5315 return -1;
5316 wpas_start_wps_go(wpa_s, &params, 0);
5317
5318 return 0;
5319}
5320
5321
5322static int wpas_start_p2p_client(struct wpa_supplicant *wpa_s,
Dmitry Shmidt15907092014-03-25 10:42:57 -07005323 struct wpa_ssid *params, int addr_allocated,
5324 int freq)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005325{
5326 struct wpa_ssid *ssid;
5327
5328 wpa_s = wpas_p2p_get_group_iface(wpa_s, addr_allocated, 0);
5329 if (wpa_s == NULL)
5330 return -1;
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07005331 wpa_s->p2p_last_4way_hs_fail = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005332
5333 wpa_supplicant_ap_deinit(wpa_s);
5334
5335 ssid = wpa_config_add_network(wpa_s->conf);
5336 if (ssid == NULL)
5337 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005338 wpa_config_set_network_defaults(ssid);
5339 ssid->temporary = 1;
5340 ssid->proto = WPA_PROTO_RSN;
5341 ssid->pairwise_cipher = WPA_CIPHER_CCMP;
5342 ssid->group_cipher = WPA_CIPHER_CCMP;
5343 ssid->key_mgmt = WPA_KEY_MGMT_PSK;
5344 ssid->ssid = os_malloc(params->ssid_len);
5345 if (ssid->ssid == NULL) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005346 wpa_config_remove_network(wpa_s->conf, ssid->id);
5347 return -1;
5348 }
5349 os_memcpy(ssid->ssid, params->ssid, params->ssid_len);
5350 ssid->ssid_len = params->ssid_len;
5351 ssid->p2p_group = 1;
5352 ssid->export_keys = 1;
5353 if (params->psk_set) {
5354 os_memcpy(ssid->psk, params->psk, 32);
5355 ssid->psk_set = 1;
5356 }
5357 if (params->passphrase)
5358 ssid->passphrase = os_strdup(params->passphrase);
5359
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005360 wpa_s->show_group_started = 1;
Dmitry Shmidt15907092014-03-25 10:42:57 -07005361 wpa_s->p2p_in_invitation = 1;
5362 wpa_s->p2p_invite_go_freq = freq;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005363
Dmitry Shmidt15907092014-03-25 10:42:57 -07005364 eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s->parent,
5365 NULL);
5366 eloop_register_timeout(P2P_MAX_INITIAL_CONN_WAIT, 0,
5367 wpas_p2p_group_formation_timeout,
5368 wpa_s->parent, NULL);
Dmitry Shmidtfa3fc4a2013-11-21 13:34:38 -08005369 wpa_supplicant_select_network(wpa_s, ssid);
5370
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005371 return 0;
5372}
5373
5374
5375int wpas_p2p_group_add_persistent(struct wpa_supplicant *wpa_s,
5376 struct wpa_ssid *ssid, int addr_allocated,
Dmitry Shmidt96be6222014-02-13 10:16:51 -08005377 int force_freq, int neg_freq, int ht40,
5378 int vht, const struct p2p_channels *channels,
Dmitry Shmidt56052862013-10-04 10:23:25 -07005379 int connection_timeout)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005380{
5381 struct p2p_go_neg_results params;
Dmitry Shmidt96be6222014-02-13 10:16:51 -08005382 int go = 0, freq;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005383
5384 if (ssid->disabled != 2 || ssid->ssid == NULL)
5385 return -1;
5386
5387 if (wpas_get_p2p_group(wpa_s, ssid->ssid, ssid->ssid_len, &go) &&
5388 go == (ssid->mode == WPAS_MODE_P2P_GO)) {
5389 wpa_printf(MSG_DEBUG, "P2P: Requested persistent group is "
5390 "already running");
5391 return 0;
5392 }
5393
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07005394 os_free(wpa_s->global->add_psk);
5395 wpa_s->global->add_psk = NULL;
5396
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005397 /* Make sure we are not running find during connection establishment */
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08005398 wpas_p2p_stop_find_oper(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005399
Dmitry Shmidt04949592012-07-19 12:16:46 -07005400 wpa_s->p2p_fallback_to_go_neg = 0;
5401
Dmitry Shmidt96be6222014-02-13 10:16:51 -08005402 if (force_freq > 0) {
5403 freq = wpas_p2p_select_go_freq(wpa_s, force_freq);
5404 if (freq < 0)
5405 return -1;
5406 } else {
5407 freq = wpas_p2p_select_go_freq(wpa_s, neg_freq);
5408 if (freq < 0 || (freq > 0 && !freq_included(channels, freq)))
5409 freq = 0;
5410 }
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07005411
Dmitry Shmidt15907092014-03-25 10:42:57 -07005412 if (ssid->mode == WPAS_MODE_INFRA)
5413 return wpas_start_p2p_client(wpa_s, ssid, addr_allocated, freq);
5414
5415 if (ssid->mode != WPAS_MODE_P2P_GO)
5416 return -1;
5417
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07005418 if (wpas_p2p_init_go_params(wpa_s, &params, freq, ht40, vht, channels))
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005419 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005420
5421 params.role_go = 1;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08005422 params.psk_set = ssid->psk_set;
5423 if (params.psk_set)
5424 os_memcpy(params.psk, ssid->psk, sizeof(params.psk));
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08005425 if (ssid->passphrase) {
5426 if (os_strlen(ssid->passphrase) >= sizeof(params.passphrase)) {
5427 wpa_printf(MSG_ERROR, "P2P: Invalid passphrase in "
5428 "persistent group");
5429 return -1;
5430 }
5431 os_strlcpy(params.passphrase, ssid->passphrase,
5432 sizeof(params.passphrase));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005433 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005434 os_memcpy(params.ssid, ssid->ssid, ssid->ssid_len);
5435 params.ssid_len = ssid->ssid_len;
5436 params.persistent_group = 1;
5437
5438 wpa_s = wpas_p2p_get_group_iface(wpa_s, addr_allocated, 1);
5439 if (wpa_s == NULL)
5440 return -1;
5441
Dmitry Shmidt56052862013-10-04 10:23:25 -07005442 wpa_s->p2p_first_connection_timeout = connection_timeout;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005443 wpas_start_wps_go(wpa_s, &params, 0);
5444
5445 return 0;
5446}
5447
5448
5449static void wpas_p2p_ie_update(void *ctx, struct wpabuf *beacon_ies,
5450 struct wpabuf *proberesp_ies)
5451{
5452 struct wpa_supplicant *wpa_s = ctx;
5453 if (wpa_s->ap_iface) {
5454 struct hostapd_data *hapd = wpa_s->ap_iface->bss[0];
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005455 if (!(hapd->conf->p2p & P2P_GROUP_OWNER)) {
5456 wpabuf_free(beacon_ies);
5457 wpabuf_free(proberesp_ies);
5458 return;
5459 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005460 if (beacon_ies) {
5461 wpabuf_free(hapd->p2p_beacon_ie);
5462 hapd->p2p_beacon_ie = beacon_ies;
5463 }
5464 wpabuf_free(hapd->p2p_probe_resp_ie);
5465 hapd->p2p_probe_resp_ie = proberesp_ies;
5466 } else {
5467 wpabuf_free(beacon_ies);
5468 wpabuf_free(proberesp_ies);
5469 }
5470 wpa_supplicant_ap_update_beacon(wpa_s);
5471}
5472
5473
5474static void wpas_p2p_idle_update(void *ctx, int idle)
5475{
5476 struct wpa_supplicant *wpa_s = ctx;
5477 if (!wpa_s->ap_iface)
5478 return;
5479 wpa_printf(MSG_DEBUG, "P2P: GO - group %sidle", idle ? "" : "not ");
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005480 if (idle) {
5481 if (wpa_s->global->p2p_fail_on_wps_complete &&
5482 wpa_s->p2p_in_provisioning) {
5483 wpas_p2p_grpform_fail_after_wps(wpa_s);
5484 return;
5485 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005486 wpas_p2p_set_group_idle_timeout(wpa_s);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005487 } else
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005488 eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL);
5489}
5490
5491
5492struct p2p_group * wpas_p2p_group_init(struct wpa_supplicant *wpa_s,
Dmitry Shmidt04949592012-07-19 12:16:46 -07005493 struct wpa_ssid *ssid)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005494{
5495 struct p2p_group *group;
5496 struct p2p_group_config *cfg;
5497
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005498 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
5499 return NULL;
5500
5501 cfg = os_zalloc(sizeof(*cfg));
5502 if (cfg == NULL)
5503 return NULL;
5504
Dmitry Shmidt04949592012-07-19 12:16:46 -07005505 if (ssid->p2p_persistent_group && wpa_s->conf->persistent_reconnect)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005506 cfg->persistent_group = 2;
Dmitry Shmidt04949592012-07-19 12:16:46 -07005507 else if (ssid->p2p_persistent_group)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005508 cfg->persistent_group = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005509 os_memcpy(cfg->interface_addr, wpa_s->own_addr, ETH_ALEN);
5510 if (wpa_s->max_stations &&
5511 wpa_s->max_stations < wpa_s->conf->max_num_sta)
5512 cfg->max_clients = wpa_s->max_stations;
5513 else
5514 cfg->max_clients = wpa_s->conf->max_num_sta;
Dmitry Shmidt04949592012-07-19 12:16:46 -07005515 os_memcpy(cfg->ssid, ssid->ssid, ssid->ssid_len);
5516 cfg->ssid_len = ssid->ssid_len;
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08005517 cfg->freq = ssid->frequency;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005518 cfg->cb_ctx = wpa_s;
5519 cfg->ie_update = wpas_p2p_ie_update;
5520 cfg->idle_update = wpas_p2p_idle_update;
5521
5522 group = p2p_group_init(wpa_s->global->p2p, cfg);
5523 if (group == NULL)
5524 os_free(cfg);
Dmitry Shmidt04949592012-07-19 12:16:46 -07005525 if (ssid->mode != WPAS_MODE_P2P_GROUP_FORMATION)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005526 p2p_group_notif_formation_done(group);
5527 wpa_s->p2p_group = group;
5528 return group;
5529}
5530
5531
5532void wpas_p2p_wps_success(struct wpa_supplicant *wpa_s, const u8 *peer_addr,
5533 int registrar)
5534{
Dmitry Shmidt04949592012-07-19 12:16:46 -07005535 struct wpa_ssid *ssid = wpa_s->current_ssid;
5536
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005537 if (!wpa_s->p2p_in_provisioning) {
5538 wpa_printf(MSG_DEBUG, "P2P: Ignore WPS success event - P2P "
5539 "provisioning not in progress");
5540 return;
5541 }
5542
Dmitry Shmidt04949592012-07-19 12:16:46 -07005543 if (ssid && ssid->mode == WPAS_MODE_INFRA) {
5544 u8 go_dev_addr[ETH_ALEN];
5545 os_memcpy(go_dev_addr, wpa_s->bssid, ETH_ALEN);
5546 wpas_p2p_persistent_group(wpa_s, go_dev_addr, ssid->ssid,
5547 ssid->ssid_len);
5548 /* Clear any stored provisioning info */
5549 p2p_clear_provisioning_info(wpa_s->global->p2p, go_dev_addr);
5550 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005551
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005552 eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s->parent,
5553 NULL);
Dmitry Shmidt56052862013-10-04 10:23:25 -07005554 wpa_s->p2p_go_group_formation_completed = 1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07005555 if (ssid && ssid->mode == WPAS_MODE_INFRA) {
5556 /*
5557 * Use a separate timeout for initial data connection to
5558 * complete to allow the group to be removed automatically if
5559 * something goes wrong in this step before the P2P group idle
5560 * timeout mechanism is taken into use.
5561 */
Dmitry Shmidt56052862013-10-04 10:23:25 -07005562 wpa_dbg(wpa_s, MSG_DEBUG,
5563 "P2P: Re-start group formation timeout (%d seconds) as client for initial connection",
5564 P2P_MAX_INITIAL_CONN_WAIT);
Dmitry Shmidt04949592012-07-19 12:16:46 -07005565 eloop_register_timeout(P2P_MAX_INITIAL_CONN_WAIT, 0,
5566 wpas_p2p_group_formation_timeout,
5567 wpa_s->parent, NULL);
Dmitry Shmidt56052862013-10-04 10:23:25 -07005568 } else if (ssid) {
5569 /*
5570 * Use a separate timeout for initial data connection to
5571 * complete to allow the group to be removed automatically if
5572 * the client does not complete data connection successfully.
5573 */
5574 wpa_dbg(wpa_s, MSG_DEBUG,
5575 "P2P: Re-start group formation timeout (%d seconds) as GO for initial connection",
5576 P2P_MAX_INITIAL_CONN_WAIT_GO);
5577 eloop_register_timeout(P2P_MAX_INITIAL_CONN_WAIT_GO, 0,
5578 wpas_p2p_group_formation_timeout,
5579 wpa_s->parent, NULL);
5580 /*
5581 * Complete group formation on first successful data connection
5582 */
5583 wpa_s->p2p_go_group_formation_completed = 0;
Dmitry Shmidt04949592012-07-19 12:16:46 -07005584 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005585 if (wpa_s->global->p2p)
5586 p2p_wps_success_cb(wpa_s->global->p2p, peer_addr);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005587 wpas_group_formation_completed(wpa_s, 1);
5588}
5589
5590
Jouni Malinen75ecf522011-06-27 15:19:46 -07005591void wpas_p2p_wps_failed(struct wpa_supplicant *wpa_s,
5592 struct wps_event_fail *fail)
5593{
5594 if (!wpa_s->p2p_in_provisioning) {
5595 wpa_printf(MSG_DEBUG, "P2P: Ignore WPS fail event - P2P "
5596 "provisioning not in progress");
5597 return;
5598 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005599
5600 if (wpa_s->go_params) {
5601 p2p_clear_provisioning_info(
5602 wpa_s->global->p2p,
Dmitry Shmidt04949592012-07-19 12:16:46 -07005603 wpa_s->go_params->peer_device_addr);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005604 }
5605
Jouni Malinen75ecf522011-06-27 15:19:46 -07005606 wpas_notify_p2p_wps_failed(wpa_s, fail);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005607
5608 if (wpa_s == wpa_s->global->p2p_group_formation) {
5609 /*
5610 * Allow some time for the failed WPS negotiation exchange to
5611 * complete, but remove the group since group formation cannot
5612 * succeed after provisioning failure.
5613 */
5614 wpa_printf(MSG_DEBUG, "P2P: WPS step failed during group formation - reject connection from timeout");
5615 wpa_s->global->p2p_fail_on_wps_complete = 1;
5616 eloop_deplete_timeout(0, 50000,
5617 wpas_p2p_group_formation_timeout,
5618 wpa_s->parent, NULL);
5619 }
5620}
5621
5622
5623int wpas_p2p_wps_eapol_cb(struct wpa_supplicant *wpa_s)
5624{
5625 if (!wpa_s->global->p2p_fail_on_wps_complete ||
5626 !wpa_s->p2p_in_provisioning)
5627 return 0;
5628
5629 wpas_p2p_grpform_fail_after_wps(wpa_s);
5630
5631 return 1;
Jouni Malinen75ecf522011-06-27 15:19:46 -07005632}
5633
5634
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005635int wpas_p2p_prov_disc(struct wpa_supplicant *wpa_s, const u8 *peer_addr,
Dmitry Shmidt04949592012-07-19 12:16:46 -07005636 const char *config_method,
5637 enum wpas_p2p_prov_disc_use use)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005638{
5639 u16 config_methods;
5640
Dmitry Shmidt04949592012-07-19 12:16:46 -07005641 wpa_s->p2p_fallback_to_go_neg = 0;
5642 wpa_s->pending_pd_use = NORMAL_PD;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005643 if (os_strncmp(config_method, "display", 7) == 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005644 config_methods = WPS_CONFIG_DISPLAY;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005645 else if (os_strncmp(config_method, "keypad", 6) == 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005646 config_methods = WPS_CONFIG_KEYPAD;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005647 else if (os_strncmp(config_method, "pbc", 3) == 0 ||
5648 os_strncmp(config_method, "pushbutton", 10) == 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005649 config_methods = WPS_CONFIG_PUSHBUTTON;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005650 else {
5651 wpa_printf(MSG_DEBUG, "P2P: Unknown config method");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005652 return -1;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005653 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005654
Dmitry Shmidt04949592012-07-19 12:16:46 -07005655 if (use == WPAS_P2P_PD_AUTO) {
5656 os_memcpy(wpa_s->pending_join_dev_addr, peer_addr, ETH_ALEN);
5657 wpa_s->pending_pd_config_methods = config_methods;
5658 wpa_s->p2p_auto_pd = 1;
5659 wpa_s->p2p_auto_join = 0;
5660 wpa_s->pending_pd_before_join = 0;
5661 wpa_s->auto_pd_scan_retry = 0;
5662 wpas_p2p_stop_find(wpa_s);
5663 wpa_s->p2p_join_scan_count = 0;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005664 os_get_reltime(&wpa_s->p2p_auto_started);
Dmitry Shmidt04949592012-07-19 12:16:46 -07005665 wpa_printf(MSG_DEBUG, "P2P: Auto PD started at %ld.%06ld",
5666 wpa_s->p2p_auto_started.sec,
5667 wpa_s->p2p_auto_started.usec);
5668 wpas_p2p_join_scan(wpa_s, NULL);
5669 return 0;
5670 }
5671
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005672 if (wpa_s->global->p2p == NULL || wpa_s->global->p2p_disabled)
5673 return -1;
5674
5675 return p2p_prov_disc_req(wpa_s->global->p2p, peer_addr,
Dmitry Shmidt04949592012-07-19 12:16:46 -07005676 config_methods, use == WPAS_P2P_PD_FOR_JOIN,
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08005677 0, 1);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005678}
5679
5680
5681int wpas_p2p_scan_result_text(const u8 *ies, size_t ies_len, char *buf,
5682 char *end)
5683{
5684 return p2p_scan_result_text(ies, ies_len, buf, end);
5685}
5686
5687
5688static void wpas_p2p_clear_pending_action_tx(struct wpa_supplicant *wpa_s)
5689{
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08005690 if (!offchannel_pending_action_tx(wpa_s))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005691 return;
5692
Dmitry Shmidt03658832014-08-13 11:03:49 -07005693 wpas_p2p_action_tx_clear(wpa_s);
5694
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005695 wpa_printf(MSG_DEBUG, "P2P: Drop pending Action TX due to new "
5696 "operation request");
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08005697 offchannel_clear_pending_action_tx(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005698}
5699
5700
5701int wpas_p2p_find(struct wpa_supplicant *wpa_s, unsigned int timeout,
5702 enum p2p_discovery_type type,
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08005703 unsigned int num_req_dev_types, const u8 *req_dev_types,
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005704 const u8 *dev_id, unsigned int search_delay)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005705{
5706 wpas_p2p_clear_pending_action_tx(wpa_s);
5707 wpa_s->p2p_long_listen = 0;
5708
Dmitry Shmidt04949592012-07-19 12:16:46 -07005709 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL ||
5710 wpa_s->p2p_in_provisioning)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005711 return -1;
5712
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08005713 wpa_supplicant_cancel_sched_scan(wpa_s);
5714
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005715 return p2p_find(wpa_s->global->p2p, timeout, type,
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005716 num_req_dev_types, req_dev_types, dev_id,
5717 search_delay);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005718}
5719
5720
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08005721static int wpas_p2p_stop_find_oper(struct wpa_supplicant *wpa_s)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005722{
5723 wpas_p2p_clear_pending_action_tx(wpa_s);
5724 wpa_s->p2p_long_listen = 0;
5725 eloop_cancel_timeout(wpas_p2p_long_listen_timeout, wpa_s, NULL);
5726 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005727
5728 if (wpa_s->global->p2p)
5729 p2p_stop_find(wpa_s->global->p2p);
5730
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08005731 return 0;
5732}
5733
5734
5735void wpas_p2p_stop_find(struct wpa_supplicant *wpa_s)
5736{
5737 if (wpas_p2p_stop_find_oper(wpa_s) > 0)
5738 return;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005739 wpas_p2p_remove_pending_group_interface(wpa_s);
5740}
5741
5742
5743static void wpas_p2p_long_listen_timeout(void *eloop_ctx, void *timeout_ctx)
5744{
5745 struct wpa_supplicant *wpa_s = eloop_ctx;
5746 wpa_s->p2p_long_listen = 0;
5747}
5748
5749
5750int wpas_p2p_listen(struct wpa_supplicant *wpa_s, unsigned int timeout)
5751{
5752 int res;
5753
5754 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
5755 return -1;
5756
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08005757 wpa_supplicant_cancel_sched_scan(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005758 wpas_p2p_clear_pending_action_tx(wpa_s);
5759
5760 if (timeout == 0) {
5761 /*
5762 * This is a request for unlimited Listen state. However, at
5763 * least for now, this is mapped to a Listen state for one
5764 * hour.
5765 */
5766 timeout = 3600;
5767 }
5768 eloop_cancel_timeout(wpas_p2p_long_listen_timeout, wpa_s, NULL);
5769 wpa_s->p2p_long_listen = 0;
5770
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005771 /*
5772 * Stop previous find/listen operation to avoid trying to request a new
5773 * remain-on-channel operation while the driver is still running the
5774 * previous one.
5775 */
5776 if (wpa_s->global->p2p)
5777 p2p_stop_find(wpa_s->global->p2p);
5778
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005779 res = wpas_p2p_listen_start(wpa_s, timeout * 1000);
5780 if (res == 0 && timeout * 1000 > wpa_s->max_remain_on_chan) {
5781 wpa_s->p2p_long_listen = timeout * 1000;
5782 eloop_register_timeout(timeout, 0,
5783 wpas_p2p_long_listen_timeout,
5784 wpa_s, NULL);
5785 }
5786
5787 return res;
5788}
5789
5790
5791int wpas_p2p_assoc_req_ie(struct wpa_supplicant *wpa_s, struct wpa_bss *bss,
5792 u8 *buf, size_t len, int p2p_group)
5793{
5794 struct wpabuf *p2p_ie;
5795 int ret;
5796
5797 if (wpa_s->global->p2p_disabled)
5798 return -1;
5799 if (wpa_s->global->p2p == NULL)
5800 return -1;
5801 if (bss == NULL)
5802 return -1;
5803
5804 p2p_ie = wpa_bss_get_vendor_ie_multi(bss, P2P_IE_VENDOR_TYPE);
5805 ret = p2p_assoc_req_ie(wpa_s->global->p2p, bss->bssid, buf, len,
5806 p2p_group, p2p_ie);
5807 wpabuf_free(p2p_ie);
5808
5809 return ret;
5810}
5811
5812
5813int wpas_p2p_probe_req_rx(struct wpa_supplicant *wpa_s, const u8 *addr,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005814 const u8 *dst, const u8 *bssid,
Dmitry Shmidt04949592012-07-19 12:16:46 -07005815 const u8 *ie, size_t ie_len, int ssi_signal)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005816{
5817 if (wpa_s->global->p2p_disabled)
5818 return 0;
5819 if (wpa_s->global->p2p == NULL)
5820 return 0;
5821
Dmitry Shmidt04949592012-07-19 12:16:46 -07005822 switch (p2p_probe_req_rx(wpa_s->global->p2p, addr, dst, bssid,
5823 ie, ie_len)) {
5824 case P2P_PREQ_NOT_P2P:
5825 wpas_notify_preq(wpa_s, addr, dst, bssid, ie, ie_len,
5826 ssi_signal);
5827 /* fall through */
5828 case P2P_PREQ_MALFORMED:
5829 case P2P_PREQ_NOT_LISTEN:
5830 case P2P_PREQ_NOT_PROCESSED:
5831 default: /* make gcc happy */
5832 return 0;
5833 case P2P_PREQ_PROCESSED:
5834 return 1;
5835 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005836}
5837
5838
5839void wpas_p2p_rx_action(struct wpa_supplicant *wpa_s, const u8 *da,
5840 const u8 *sa, const u8 *bssid,
5841 u8 category, const u8 *data, size_t len, int freq)
5842{
5843 if (wpa_s->global->p2p_disabled)
5844 return;
5845 if (wpa_s->global->p2p == NULL)
5846 return;
5847
5848 p2p_rx_action(wpa_s->global->p2p, da, sa, bssid, category, data, len,
5849 freq);
5850}
5851
5852
5853void wpas_p2p_scan_ie(struct wpa_supplicant *wpa_s, struct wpabuf *ies)
5854{
5855 if (wpa_s->global->p2p_disabled)
5856 return;
5857 if (wpa_s->global->p2p == NULL)
5858 return;
5859
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08005860 p2p_scan_ie(wpa_s->global->p2p, ies, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005861}
5862
5863
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07005864static void wpas_p2p_group_deinit(struct wpa_supplicant *wpa_s)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005865{
5866 p2p_group_deinit(wpa_s->p2p_group);
5867 wpa_s->p2p_group = NULL;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005868
5869 wpa_s->ap_configured_cb = NULL;
5870 wpa_s->ap_configured_cb_ctx = NULL;
5871 wpa_s->ap_configured_cb_data = NULL;
5872 wpa_s->connect_without_scan = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005873}
5874
5875
5876int wpas_p2p_reject(struct wpa_supplicant *wpa_s, const u8 *addr)
5877{
5878 wpa_s->p2p_long_listen = 0;
5879
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005880 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
5881 return -1;
5882
5883 return p2p_reject(wpa_s->global->p2p, addr);
5884}
5885
5886
5887/* Invite to reinvoke a persistent group */
5888int wpas_p2p_invite(struct wpa_supplicant *wpa_s, const u8 *peer_addr,
Jouni Malinen31be0a42012-08-31 21:20:51 +03005889 struct wpa_ssid *ssid, const u8 *go_dev_addr, int freq,
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07005890 int ht40, int vht, int pref_freq)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005891{
5892 enum p2p_invite_role role;
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07005893 u8 *bssid = NULL;
5894 int force_freq = 0;
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08005895 int res;
Dmitry Shmidta0d265f2013-11-19 13:13:41 -08005896 int no_pref_freq_given = pref_freq == 0;
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08005897
Dmitry Shmidt700a1372013-03-15 14:14:44 -07005898 wpa_s->global->p2p_invite_group = NULL;
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08005899 if (peer_addr)
5900 os_memcpy(wpa_s->p2p_auth_invite, peer_addr, ETH_ALEN);
5901 else
5902 os_memset(wpa_s->p2p_auth_invite, 0, ETH_ALEN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005903
Jouni Malinen31be0a42012-08-31 21:20:51 +03005904 wpa_s->p2p_persistent_go_freq = freq;
5905 wpa_s->p2p_go_ht40 = !!ht40;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005906 if (ssid->mode == WPAS_MODE_P2P_GO) {
5907 role = P2P_INVITE_ROLE_GO;
5908 if (peer_addr == NULL) {
5909 wpa_printf(MSG_DEBUG, "P2P: Missing peer "
5910 "address in invitation command");
5911 return -1;
5912 }
5913 if (wpas_p2p_create_iface(wpa_s)) {
5914 if (wpas_p2p_add_group_interface(wpa_s,
5915 WPA_IF_P2P_GO) < 0) {
5916 wpa_printf(MSG_ERROR, "P2P: Failed to "
5917 "allocate a new interface for the "
5918 "group");
5919 return -1;
5920 }
5921 bssid = wpa_s->pending_interface_addr;
5922 } else
5923 bssid = wpa_s->own_addr;
5924 } else {
5925 role = P2P_INVITE_ROLE_CLIENT;
5926 peer_addr = ssid->bssid;
5927 }
5928 wpa_s->pending_invite_ssid_id = ssid->id;
5929
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07005930 res = wpas_p2p_setup_freqs(wpa_s, freq, &force_freq, &pref_freq,
5931 role == P2P_INVITE_ROLE_GO);
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08005932 if (res)
5933 return res;
Irfan Sheriffaf84a572012-09-22 16:59:30 -07005934
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005935 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
5936 return -1;
5937
Dmitry Shmidta0d265f2013-11-19 13:13:41 -08005938 if (wpa_s->parent->conf->p2p_ignore_shared_freq &&
5939 no_pref_freq_given && pref_freq > 0 &&
5940 wpa_s->num_multichan_concurrent > 1 &&
5941 wpas_p2p_num_unused_channels(wpa_s) > 0) {
5942 wpa_printf(MSG_DEBUG, "P2P: Ignore own channel preference %d MHz for invitation due to p2p_ignore_shared_freq=1 configuration",
5943 pref_freq);
5944 pref_freq = 0;
5945 }
5946
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005947 return p2p_invite(wpa_s->global->p2p, peer_addr, role, bssid,
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08005948 ssid->ssid, ssid->ssid_len, force_freq, go_dev_addr,
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08005949 1, pref_freq, -1);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005950}
5951
5952
5953/* Invite to join an active group */
5954int wpas_p2p_invite_group(struct wpa_supplicant *wpa_s, const char *ifname,
5955 const u8 *peer_addr, const u8 *go_dev_addr)
5956{
5957 struct wpa_global *global = wpa_s->global;
5958 enum p2p_invite_role role;
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07005959 u8 *bssid = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005960 struct wpa_ssid *ssid;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08005961 int persistent;
Dmitry Shmidt051af732013-10-22 13:52:46 -07005962 int freq = 0, force_freq = 0, pref_freq = 0;
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08005963 int res;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005964
Jouni Malinen31be0a42012-08-31 21:20:51 +03005965 wpa_s->p2p_persistent_go_freq = 0;
5966 wpa_s->p2p_go_ht40 = 0;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07005967 wpa_s->p2p_go_vht = 0;
Jouni Malinen31be0a42012-08-31 21:20:51 +03005968
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005969 for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
5970 if (os_strcmp(wpa_s->ifname, ifname) == 0)
5971 break;
5972 }
5973 if (wpa_s == NULL) {
5974 wpa_printf(MSG_DEBUG, "P2P: Interface '%s' not found", ifname);
5975 return -1;
5976 }
5977
5978 ssid = wpa_s->current_ssid;
5979 if (ssid == NULL) {
5980 wpa_printf(MSG_DEBUG, "P2P: No current SSID to use for "
5981 "invitation");
5982 return -1;
5983 }
5984
Dmitry Shmidt700a1372013-03-15 14:14:44 -07005985 wpa_s->global->p2p_invite_group = wpa_s;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08005986 persistent = ssid->p2p_persistent_group &&
5987 wpas_p2p_get_persistent(wpa_s->parent, peer_addr,
5988 ssid->ssid, ssid->ssid_len);
5989
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005990 if (ssid->mode == WPAS_MODE_P2P_GO) {
5991 role = P2P_INVITE_ROLE_ACTIVE_GO;
5992 bssid = wpa_s->own_addr;
5993 if (go_dev_addr == NULL)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005994 go_dev_addr = wpa_s->global->p2p_dev_addr;
Dmitry Shmidt051af732013-10-22 13:52:46 -07005995 freq = ssid->frequency;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005996 } else {
5997 role = P2P_INVITE_ROLE_CLIENT;
5998 if (wpa_s->wpa_state < WPA_ASSOCIATED) {
5999 wpa_printf(MSG_DEBUG, "P2P: Not associated - cannot "
6000 "invite to current group");
6001 return -1;
6002 }
6003 bssid = wpa_s->bssid;
6004 if (go_dev_addr == NULL &&
6005 !is_zero_ether_addr(wpa_s->go_dev_addr))
6006 go_dev_addr = wpa_s->go_dev_addr;
Dmitry Shmidt051af732013-10-22 13:52:46 -07006007 freq = wpa_s->current_bss ? wpa_s->current_bss->freq :
6008 (int) wpa_s->assoc_freq;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006009 }
6010 wpa_s->parent->pending_invite_ssid_id = -1;
6011
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006012 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
6013 return -1;
6014
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07006015 res = wpas_p2p_setup_freqs(wpa_s, freq, &force_freq, &pref_freq,
6016 role == P2P_INVITE_ROLE_ACTIVE_GO);
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08006017 if (res)
6018 return res;
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07006019 wpas_p2p_set_own_freq_preference(wpa_s, force_freq);
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08006020
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006021 return p2p_invite(wpa_s->global->p2p, peer_addr, role, bssid,
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08006022 ssid->ssid, ssid->ssid_len, force_freq,
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08006023 go_dev_addr, persistent, pref_freq, -1);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006024}
6025
6026
6027void wpas_p2p_completed(struct wpa_supplicant *wpa_s)
6028{
6029 struct wpa_ssid *ssid = wpa_s->current_ssid;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006030 u8 go_dev_addr[ETH_ALEN];
Jouni Malinen75ecf522011-06-27 15:19:46 -07006031 int network_id = -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006032 int persistent;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006033 int freq;
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08006034 u8 ip[3 * 4];
6035 char ip_addr[100];
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006036
Dmitry Shmidt04949592012-07-19 12:16:46 -07006037 if (ssid == NULL || ssid->mode != WPAS_MODE_P2P_GROUP_FORMATION) {
6038 eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
6039 wpa_s->parent, NULL);
6040 }
6041
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006042 if (!wpa_s->show_group_started || !ssid)
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08006043 return;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006044
6045 wpa_s->show_group_started = 0;
6046
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006047 os_memset(go_dev_addr, 0, ETH_ALEN);
6048 if (ssid->bssid_set)
6049 os_memcpy(go_dev_addr, ssid->bssid, ETH_ALEN);
6050 persistent = wpas_p2p_persistent_group(wpa_s, go_dev_addr, ssid->ssid,
6051 ssid->ssid_len);
6052 os_memcpy(wpa_s->go_dev_addr, go_dev_addr, ETH_ALEN);
6053
6054 if (wpa_s->global->p2p_group_formation == wpa_s)
6055 wpa_s->global->p2p_group_formation = NULL;
6056
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006057 freq = wpa_s->current_bss ? wpa_s->current_bss->freq :
6058 (int) wpa_s->assoc_freq;
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08006059
6060 ip_addr[0] = '\0';
6061 if (wpa_sm_get_p2p_ip_addr(wpa_s->wpa, ip) == 0) {
6062 os_snprintf(ip_addr, sizeof(ip_addr), " ip_addr=%u.%u.%u.%u "
6063 "ip_mask=%u.%u.%u.%u go_ip_addr=%u.%u.%u.%u",
6064 ip[0], ip[1], ip[2], ip[3],
6065 ip[4], ip[5], ip[6], ip[7],
6066 ip[8], ip[9], ip[10], ip[11]);
6067 }
6068
Dmitry Shmidt7f0b69e2014-07-28 10:35:20 -07006069 wpas_p2p_group_started(wpa_s, 0, ssid, freq,
6070 ssid->passphrase == NULL && ssid->psk_set ?
6071 ssid->psk : NULL,
6072 ssid->passphrase, go_dev_addr, persistent,
6073 ip_addr);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006074
6075 if (persistent)
Jouni Malinen75ecf522011-06-27 15:19:46 -07006076 network_id = wpas_p2p_store_persistent_group(wpa_s->parent,
6077 ssid, go_dev_addr);
6078 if (network_id < 0)
6079 network_id = ssid->id;
6080 wpas_notify_p2p_group_started(wpa_s, ssid, network_id, 1);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006081}
6082
6083
6084int wpas_p2p_presence_req(struct wpa_supplicant *wpa_s, u32 duration1,
6085 u32 interval1, u32 duration2, u32 interval2)
6086{
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08006087 int ret;
6088
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006089 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
6090 return -1;
6091
6092 if (wpa_s->wpa_state < WPA_ASSOCIATED ||
6093 wpa_s->current_ssid == NULL ||
6094 wpa_s->current_ssid->mode != WPAS_MODE_INFRA)
6095 return -1;
6096
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08006097 ret = p2p_presence_req(wpa_s->global->p2p, wpa_s->bssid,
6098 wpa_s->own_addr, wpa_s->assoc_freq,
6099 duration1, interval1, duration2, interval2);
6100 if (ret == 0)
6101 wpa_s->waiting_presence_resp = 1;
6102
6103 return ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006104}
6105
6106
6107int wpas_p2p_ext_listen(struct wpa_supplicant *wpa_s, unsigned int period,
6108 unsigned int interval)
6109{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006110 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
6111 return -1;
6112
6113 return p2p_ext_listen(wpa_s->global->p2p, period, interval);
6114}
6115
6116
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006117static int wpas_p2p_is_client(struct wpa_supplicant *wpa_s)
6118{
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08006119 if (wpa_s->current_ssid == NULL) {
6120 /*
6121 * current_ssid can be cleared when P2P client interface gets
6122 * disconnected, so assume this interface was used as P2P
6123 * client.
6124 */
6125 return 1;
6126 }
6127 return wpa_s->current_ssid->p2p_group &&
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006128 wpa_s->current_ssid->mode == WPAS_MODE_INFRA;
6129}
6130
6131
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006132static void wpas_p2p_group_idle_timeout(void *eloop_ctx, void *timeout_ctx)
6133{
6134 struct wpa_supplicant *wpa_s = eloop_ctx;
6135
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006136 if (wpa_s->conf->p2p_group_idle == 0 && !wpas_p2p_is_client(wpa_s)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006137 wpa_printf(MSG_DEBUG, "P2P: Ignore group idle timeout - "
6138 "disabled");
6139 return;
6140 }
6141
Dmitry Shmidt04949592012-07-19 12:16:46 -07006142 wpa_printf(MSG_DEBUG, "P2P: Group idle timeout reached - terminate "
6143 "group");
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07006144 wpas_p2p_group_delete(wpa_s, P2P_GROUP_REMOVAL_IDLE_TIMEOUT);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006145}
6146
6147
6148static void wpas_p2p_set_group_idle_timeout(struct wpa_supplicant *wpa_s)
6149{
Dmitry Shmidt04949592012-07-19 12:16:46 -07006150 int timeout;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006151
Dmitry Shmidt04949592012-07-19 12:16:46 -07006152 if (eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL) > 0)
6153 wpa_printf(MSG_DEBUG, "P2P: Cancelled P2P group idle timeout");
6154
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006155 if (wpa_s->current_ssid == NULL || !wpa_s->current_ssid->p2p_group)
6156 return;
6157
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006158 timeout = wpa_s->conf->p2p_group_idle;
6159 if (wpa_s->current_ssid->mode == WPAS_MODE_INFRA &&
6160 (timeout == 0 || timeout > P2P_MAX_CLIENT_IDLE))
6161 timeout = P2P_MAX_CLIENT_IDLE;
6162
6163 if (timeout == 0)
6164 return;
6165
Dmitry Shmidt04949592012-07-19 12:16:46 -07006166 if (timeout < 0) {
6167 if (wpa_s->current_ssid->mode == WPAS_MODE_INFRA)
6168 timeout = 0; /* special client mode no-timeout */
6169 else
6170 return;
6171 }
6172
6173 if (wpa_s->p2p_in_provisioning) {
6174 /*
6175 * Use the normal group formation timeout during the
6176 * provisioning phase to avoid terminating this process too
6177 * early due to group idle timeout.
6178 */
6179 wpa_printf(MSG_DEBUG, "P2P: Do not use P2P group idle timeout "
6180 "during provisioning");
6181 return;
6182 }
Deepthi Gowri9e4e8ac2013-04-16 11:57:05 +05306183
Dmitry Shmidt04949592012-07-19 12:16:46 -07006184 if (wpa_s->show_group_started) {
6185 /*
6186 * Use the normal group formation timeout between the end of
6187 * the provisioning phase and completion of 4-way handshake to
6188 * avoid terminating this process too early due to group idle
6189 * timeout.
6190 */
6191 wpa_printf(MSG_DEBUG, "P2P: Do not use P2P group idle timeout "
6192 "while waiting for initial 4-way handshake to "
6193 "complete");
6194 return;
6195 }
6196
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006197 wpa_printf(MSG_DEBUG, "P2P: Set P2P group idle timeout to %u seconds",
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006198 timeout);
6199 eloop_register_timeout(timeout, 0, wpas_p2p_group_idle_timeout,
6200 wpa_s, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006201}
6202
6203
Jouni Malinen2b89da82012-08-31 22:04:41 +03006204/* Returns 1 if the interface was removed */
6205int wpas_p2p_deauth_notif(struct wpa_supplicant *wpa_s, const u8 *bssid,
6206 u16 reason_code, const u8 *ie, size_t ie_len,
6207 int locally_generated)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006208{
6209 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
Jouni Malinen2b89da82012-08-31 22:04:41 +03006210 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006211
Dmitry Shmidt04949592012-07-19 12:16:46 -07006212 if (!locally_generated)
6213 p2p_deauth_notif(wpa_s->global->p2p, bssid, reason_code, ie,
6214 ie_len);
6215
6216 if (reason_code == WLAN_REASON_DEAUTH_LEAVING && !locally_generated &&
6217 wpa_s->current_ssid &&
6218 wpa_s->current_ssid->p2p_group &&
6219 wpa_s->current_ssid->mode == WPAS_MODE_INFRA) {
6220 wpa_printf(MSG_DEBUG, "P2P: GO indicated that the P2P Group "
6221 "session is ending");
Jouni Malinen2b89da82012-08-31 22:04:41 +03006222 if (wpas_p2p_group_delete(wpa_s,
6223 P2P_GROUP_REMOVAL_GO_ENDING_SESSION)
6224 > 0)
6225 return 1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07006226 }
Jouni Malinen2b89da82012-08-31 22:04:41 +03006227
6228 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006229}
6230
6231
6232void wpas_p2p_disassoc_notif(struct wpa_supplicant *wpa_s, const u8 *bssid,
Dmitry Shmidt04949592012-07-19 12:16:46 -07006233 u16 reason_code, const u8 *ie, size_t ie_len,
6234 int locally_generated)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006235{
6236 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
6237 return;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006238
Dmitry Shmidt04949592012-07-19 12:16:46 -07006239 if (!locally_generated)
6240 p2p_disassoc_notif(wpa_s->global->p2p, bssid, reason_code, ie,
6241 ie_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006242}
6243
6244
6245void wpas_p2p_update_config(struct wpa_supplicant *wpa_s)
6246{
6247 struct p2p_data *p2p = wpa_s->global->p2p;
6248
6249 if (p2p == NULL)
6250 return;
6251
6252 if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_CAPABLE))
6253 return;
6254
6255 if (wpa_s->conf->changed_parameters & CFG_CHANGED_DEVICE_NAME)
6256 p2p_set_dev_name(p2p, wpa_s->conf->device_name);
6257
6258 if (wpa_s->conf->changed_parameters & CFG_CHANGED_DEVICE_TYPE)
6259 p2p_set_pri_dev_type(p2p, wpa_s->conf->device_type);
6260
6261 if (wpa_s->wps &&
6262 (wpa_s->conf->changed_parameters & CFG_CHANGED_CONFIG_METHODS))
6263 p2p_set_config_methods(p2p, wpa_s->wps->config_methods);
6264
6265 if (wpa_s->wps && (wpa_s->conf->changed_parameters & CFG_CHANGED_UUID))
6266 p2p_set_uuid(p2p, wpa_s->wps->uuid);
6267
6268 if (wpa_s->conf->changed_parameters & CFG_CHANGED_WPS_STRING) {
6269 p2p_set_manufacturer(p2p, wpa_s->conf->manufacturer);
6270 p2p_set_model_name(p2p, wpa_s->conf->model_name);
6271 p2p_set_model_number(p2p, wpa_s->conf->model_number);
6272 p2p_set_serial_number(p2p, wpa_s->conf->serial_number);
6273 }
6274
6275 if (wpa_s->conf->changed_parameters & CFG_CHANGED_SEC_DEVICE_TYPE)
6276 p2p_set_sec_dev_types(p2p,
6277 (void *) wpa_s->conf->sec_device_type,
6278 wpa_s->conf->num_sec_device_types);
6279
6280 if (wpa_s->conf->changed_parameters & CFG_CHANGED_VENDOR_EXTENSION) {
6281 int i;
6282 p2p_remove_wps_vendor_extensions(p2p);
6283 for (i = 0; i < MAX_WPS_VENDOR_EXT; i++) {
6284 if (wpa_s->conf->wps_vendor_ext[i] == NULL)
6285 continue;
6286 p2p_add_wps_vendor_extension(
6287 p2p, wpa_s->conf->wps_vendor_ext[i]);
6288 }
6289 }
6290
6291 if ((wpa_s->conf->changed_parameters & CFG_CHANGED_COUNTRY) &&
6292 wpa_s->conf->country[0] && wpa_s->conf->country[1]) {
6293 char country[3];
6294 country[0] = wpa_s->conf->country[0];
6295 country[1] = wpa_s->conf->country[1];
6296 country[2] = 0x04;
6297 p2p_set_country(p2p, country);
6298 }
6299
6300 if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_SSID_POSTFIX) {
6301 p2p_set_ssid_postfix(p2p, (u8 *) wpa_s->conf->p2p_ssid_postfix,
6302 wpa_s->conf->p2p_ssid_postfix ?
6303 os_strlen(wpa_s->conf->p2p_ssid_postfix) :
6304 0);
6305 }
6306
6307 if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_INTRA_BSS)
6308 p2p_set_intra_bss_dist(p2p, wpa_s->conf->p2p_intra_bss);
Jouni Malinen75ecf522011-06-27 15:19:46 -07006309
6310 if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_LISTEN_CHANNEL) {
6311 u8 reg_class, channel;
6312 int ret;
6313 unsigned int r;
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07006314 u8 channel_forced;
6315
Jouni Malinen75ecf522011-06-27 15:19:46 -07006316 if (wpa_s->conf->p2p_listen_reg_class &&
6317 wpa_s->conf->p2p_listen_channel) {
6318 reg_class = wpa_s->conf->p2p_listen_reg_class;
6319 channel = wpa_s->conf->p2p_listen_channel;
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07006320 channel_forced = 1;
Jouni Malinen75ecf522011-06-27 15:19:46 -07006321 } else {
6322 reg_class = 81;
6323 /*
6324 * Pick one of the social channels randomly as the
6325 * listen channel.
6326 */
6327 os_get_random((u8 *) &r, sizeof(r));
6328 channel = 1 + (r % 3) * 5;
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07006329 channel_forced = 0;
Jouni Malinen75ecf522011-06-27 15:19:46 -07006330 }
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07006331 ret = p2p_set_listen_channel(p2p, reg_class, channel,
6332 channel_forced);
Jouni Malinen75ecf522011-06-27 15:19:46 -07006333 if (ret)
6334 wpa_printf(MSG_ERROR, "P2P: Own listen channel update "
6335 "failed: %d", ret);
6336 }
6337 if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_OPER_CHANNEL) {
6338 u8 op_reg_class, op_channel, cfg_op_channel;
6339 int ret = 0;
6340 unsigned int r;
6341 if (wpa_s->conf->p2p_oper_reg_class &&
6342 wpa_s->conf->p2p_oper_channel) {
6343 op_reg_class = wpa_s->conf->p2p_oper_reg_class;
6344 op_channel = wpa_s->conf->p2p_oper_channel;
6345 cfg_op_channel = 1;
6346 } else {
6347 op_reg_class = 81;
6348 /*
6349 * Use random operation channel from (1, 6, 11)
6350 *if no other preference is indicated.
6351 */
6352 os_get_random((u8 *) &r, sizeof(r));
6353 op_channel = 1 + (r % 3) * 5;
6354 cfg_op_channel = 0;
6355 }
6356 ret = p2p_set_oper_channel(p2p, op_reg_class, op_channel,
6357 cfg_op_channel);
6358 if (ret)
6359 wpa_printf(MSG_ERROR, "P2P: Own oper channel update "
6360 "failed: %d", ret);
6361 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07006362
6363 if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_PREF_CHAN) {
6364 if (p2p_set_pref_chan(p2p, wpa_s->conf->num_p2p_pref_chan,
6365 wpa_s->conf->p2p_pref_chan) < 0) {
6366 wpa_printf(MSG_ERROR, "P2P: Preferred channel list "
6367 "update failed");
6368 }
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07006369
6370 if (p2p_set_no_go_freq(p2p, &wpa_s->conf->p2p_no_go_freq) < 0) {
6371 wpa_printf(MSG_ERROR, "P2P: No GO channel list "
6372 "update failed");
6373 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07006374 }
Dmitry Shmidt2271d3f2014-06-23 12:16:31 -07006375
6376 if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_PASSPHRASE_LEN)
6377 p2p_set_passphrase_len(p2p, wpa_s->conf->p2p_passphrase_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006378}
6379
6380
6381int wpas_p2p_set_noa(struct wpa_supplicant *wpa_s, u8 count, int start,
6382 int duration)
6383{
6384 if (!wpa_s->ap_iface)
6385 return -1;
6386 return hostapd_p2p_set_noa(wpa_s->ap_iface->bss[0], count, start,
6387 duration);
6388}
6389
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006390
6391int wpas_p2p_set_cross_connect(struct wpa_supplicant *wpa_s, int enabled)
6392{
6393 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
6394 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006395
6396 wpa_s->global->cross_connection = enabled;
6397 p2p_set_cross_connect(wpa_s->global->p2p, enabled);
6398
6399 if (!enabled) {
6400 struct wpa_supplicant *iface;
6401
6402 for (iface = wpa_s->global->ifaces; iface; iface = iface->next)
6403 {
6404 if (iface->cross_connect_enabled == 0)
6405 continue;
6406
6407 iface->cross_connect_enabled = 0;
6408 iface->cross_connect_in_use = 0;
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07006409 wpa_msg_global(iface->parent, MSG_INFO,
6410 P2P_EVENT_CROSS_CONNECT_DISABLE "%s %s",
6411 iface->ifname,
6412 iface->cross_connect_uplink);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006413 }
6414 }
6415
6416 return 0;
6417}
6418
6419
6420static void wpas_p2p_enable_cross_connect(struct wpa_supplicant *uplink)
6421{
6422 struct wpa_supplicant *iface;
6423
6424 if (!uplink->global->cross_connection)
6425 return;
6426
6427 for (iface = uplink->global->ifaces; iface; iface = iface->next) {
6428 if (!iface->cross_connect_enabled)
6429 continue;
6430 if (os_strcmp(uplink->ifname, iface->cross_connect_uplink) !=
6431 0)
6432 continue;
6433 if (iface->ap_iface == NULL)
6434 continue;
6435 if (iface->cross_connect_in_use)
6436 continue;
6437
6438 iface->cross_connect_in_use = 1;
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07006439 wpa_msg_global(iface->parent, MSG_INFO,
6440 P2P_EVENT_CROSS_CONNECT_ENABLE "%s %s",
6441 iface->ifname, iface->cross_connect_uplink);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006442 }
6443}
6444
6445
6446static void wpas_p2p_disable_cross_connect(struct wpa_supplicant *uplink)
6447{
6448 struct wpa_supplicant *iface;
6449
6450 for (iface = uplink->global->ifaces; iface; iface = iface->next) {
6451 if (!iface->cross_connect_enabled)
6452 continue;
6453 if (os_strcmp(uplink->ifname, iface->cross_connect_uplink) !=
6454 0)
6455 continue;
6456 if (!iface->cross_connect_in_use)
6457 continue;
6458
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07006459 wpa_msg_global(iface->parent, MSG_INFO,
6460 P2P_EVENT_CROSS_CONNECT_DISABLE "%s %s",
6461 iface->ifname, iface->cross_connect_uplink);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006462 iface->cross_connect_in_use = 0;
6463 }
6464}
6465
6466
6467void wpas_p2p_notif_connected(struct wpa_supplicant *wpa_s)
6468{
6469 if (wpa_s->ap_iface || wpa_s->current_ssid == NULL ||
6470 wpa_s->current_ssid->mode != WPAS_MODE_INFRA ||
6471 wpa_s->cross_connect_disallowed)
6472 wpas_p2p_disable_cross_connect(wpa_s);
6473 else
6474 wpas_p2p_enable_cross_connect(wpa_s);
Dmitry Shmidt04949592012-07-19 12:16:46 -07006475 if (!wpa_s->ap_iface &&
6476 eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL) > 0)
6477 wpa_printf(MSG_DEBUG, "P2P: Cancelled P2P group idle timeout");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006478}
6479
6480
6481void wpas_p2p_notif_disconnected(struct wpa_supplicant *wpa_s)
6482{
6483 wpas_p2p_disable_cross_connect(wpa_s);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006484 if (!wpa_s->ap_iface &&
6485 !eloop_is_timeout_registered(wpas_p2p_group_idle_timeout,
6486 wpa_s, NULL))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006487 wpas_p2p_set_group_idle_timeout(wpa_s);
6488}
6489
6490
6491static void wpas_p2p_cross_connect_setup(struct wpa_supplicant *wpa_s)
6492{
6493 struct wpa_supplicant *iface;
6494
6495 if (!wpa_s->global->cross_connection)
6496 return;
6497
6498 for (iface = wpa_s->global->ifaces; iface; iface = iface->next) {
6499 if (iface == wpa_s)
6500 continue;
6501 if (iface->drv_flags &
6502 WPA_DRIVER_FLAGS_P2P_DEDICATED_INTERFACE)
6503 continue;
6504 if (iface->drv_flags & WPA_DRIVER_FLAGS_P2P_CAPABLE)
6505 continue;
6506
6507 wpa_s->cross_connect_enabled = 1;
6508 os_strlcpy(wpa_s->cross_connect_uplink, iface->ifname,
6509 sizeof(wpa_s->cross_connect_uplink));
6510 wpa_printf(MSG_DEBUG, "P2P: Enable cross connection from "
6511 "%s to %s whenever uplink is available",
6512 wpa_s->ifname, wpa_s->cross_connect_uplink);
6513
6514 if (iface->ap_iface || iface->current_ssid == NULL ||
6515 iface->current_ssid->mode != WPAS_MODE_INFRA ||
6516 iface->cross_connect_disallowed ||
6517 iface->wpa_state != WPA_COMPLETED)
6518 break;
6519
6520 wpa_s->cross_connect_in_use = 1;
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07006521 wpa_msg_global(wpa_s->parent, MSG_INFO,
6522 P2P_EVENT_CROSS_CONNECT_ENABLE "%s %s",
6523 wpa_s->ifname, wpa_s->cross_connect_uplink);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006524 break;
6525 }
6526}
6527
6528
6529int wpas_p2p_notif_pbc_overlap(struct wpa_supplicant *wpa_s)
6530{
6531 if (wpa_s->p2p_group_interface != P2P_GROUP_INTERFACE_CLIENT &&
6532 !wpa_s->p2p_in_provisioning)
6533 return 0; /* not P2P client operation */
6534
6535 wpa_printf(MSG_DEBUG, "P2P: Terminate connection due to WPS PBC "
6536 "session overlap");
6537 if (wpa_s != wpa_s->parent)
6538 wpa_msg_ctrl(wpa_s->parent, MSG_INFO, WPS_EVENT_OVERLAP);
Dmitry Shmidt34af3062013-07-11 10:46:32 -07006539 wpas_p2p_group_formation_failed(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006540 return 1;
6541}
6542
6543
Dmitry Shmidtdf5a7e42014-04-02 12:59:59 -07006544void wpas_p2p_pbc_overlap_cb(void *eloop_ctx, void *timeout_ctx)
6545{
6546 struct wpa_supplicant *wpa_s = eloop_ctx;
6547 wpas_p2p_notif_pbc_overlap(wpa_s);
6548}
6549
6550
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006551void wpas_p2p_update_channel_list(struct wpa_supplicant *wpa_s)
6552{
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07006553 struct p2p_channels chan, cli_chan;
Dmitry Shmidt684785c2014-05-12 13:34:29 -07006554 struct wpa_supplicant *ifs;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006555
6556 if (wpa_s->global == NULL || wpa_s->global->p2p == NULL)
6557 return;
6558
6559 os_memset(&chan, 0, sizeof(chan));
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07006560 os_memset(&cli_chan, 0, sizeof(cli_chan));
6561 if (wpas_p2p_setup_channels(wpa_s, &chan, &cli_chan)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006562 wpa_printf(MSG_ERROR, "P2P: Failed to update supported "
6563 "channel list");
6564 return;
6565 }
6566
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07006567 p2p_update_channel_list(wpa_s->global->p2p, &chan, &cli_chan);
Dmitry Shmidt684785c2014-05-12 13:34:29 -07006568
6569 for (ifs = wpa_s->global->ifaces; ifs; ifs = ifs->next) {
6570 int freq;
6571 if (!ifs->current_ssid ||
6572 !ifs->current_ssid->p2p_group ||
6573 (ifs->current_ssid->mode != WPAS_MODE_P2P_GO &&
6574 ifs->current_ssid->mode != WPAS_MODE_P2P_GROUP_FORMATION))
6575 continue;
6576 freq = ifs->current_ssid->frequency;
6577 if (freq_included(&chan, freq)) {
6578 wpa_dbg(ifs, MSG_DEBUG,
6579 "P2P GO operating frequency %d MHz in valid range",
6580 freq);
6581 continue;
6582 }
6583
6584 wpa_dbg(ifs, MSG_DEBUG,
6585 "P2P GO operating in invalid frequency %d MHz", freq);
6586 /* TODO: Consider using CSA or removing the group within
6587 * wpa_supplicant */
6588 wpa_msg(ifs, MSG_INFO, P2P_EVENT_REMOVE_AND_REFORM_GROUP);
6589 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006590}
6591
6592
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08006593static void wpas_p2p_scan_res_ignore(struct wpa_supplicant *wpa_s,
6594 struct wpa_scan_results *scan_res)
6595{
6596 wpa_printf(MSG_DEBUG, "P2P: Ignore scan results");
6597}
6598
6599
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006600int wpas_p2p_cancel(struct wpa_supplicant *wpa_s)
6601{
6602 struct wpa_global *global = wpa_s->global;
6603 int found = 0;
6604 const u8 *peer;
6605
6606 if (global->p2p == NULL)
6607 return -1;
6608
6609 wpa_printf(MSG_DEBUG, "P2P: Request to cancel group formation");
6610
6611 if (wpa_s->pending_interface_name[0] &&
6612 !is_zero_ether_addr(wpa_s->pending_interface_addr))
6613 found = 1;
6614
6615 peer = p2p_get_go_neg_peer(global->p2p);
6616 if (peer) {
6617 wpa_printf(MSG_DEBUG, "P2P: Unauthorize pending GO Neg peer "
6618 MACSTR, MAC2STR(peer));
6619 p2p_unauthorize(global->p2p, peer);
Dmitry Shmidt04949592012-07-19 12:16:46 -07006620 found = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006621 }
6622
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08006623 if (wpa_s->scan_res_handler == wpas_p2p_scan_res_join) {
6624 wpa_printf(MSG_DEBUG, "P2P: Stop pending scan for join");
6625 wpa_s->scan_res_handler = wpas_p2p_scan_res_ignore;
6626 found = 1;
6627 }
6628
6629 if (wpa_s->pending_pd_before_join) {
6630 wpa_printf(MSG_DEBUG, "P2P: Stop pending PD before join");
6631 wpa_s->pending_pd_before_join = 0;
6632 found = 1;
6633 }
6634
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006635 wpas_p2p_stop_find(wpa_s);
6636
6637 for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
6638 if (wpa_s == global->p2p_group_formation &&
6639 (wpa_s->p2p_in_provisioning ||
6640 wpa_s->parent->pending_interface_type ==
6641 WPA_IF_P2P_CLIENT)) {
6642 wpa_printf(MSG_DEBUG, "P2P: Interface %s in group "
6643 "formation found - cancelling",
6644 wpa_s->ifname);
6645 found = 1;
6646 eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
6647 wpa_s->parent, NULL);
Jouni Malinenadddfc42012-10-03 14:31:41 -07006648 if (wpa_s->p2p_in_provisioning) {
6649 wpas_group_formation_completed(wpa_s, 0);
6650 break;
6651 }
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07006652 wpas_p2p_group_delete(wpa_s,
6653 P2P_GROUP_REMOVAL_REQUESTED);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006654 break;
Dmitry Shmidt21de2142014-04-08 10:50:52 -07006655 } else if (wpa_s->p2p_in_invitation) {
6656 wpa_printf(MSG_DEBUG, "P2P: Interface %s in invitation found - cancelling",
6657 wpa_s->ifname);
6658 found = 1;
6659 wpas_p2p_group_formation_failed(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006660 }
6661 }
6662
6663 if (!found) {
6664 wpa_printf(MSG_DEBUG, "P2P: No ongoing group formation found");
6665 return -1;
6666 }
6667
6668 return 0;
6669}
6670
6671
6672void wpas_p2p_interface_unavailable(struct wpa_supplicant *wpa_s)
6673{
6674 if (wpa_s->current_ssid == NULL || !wpa_s->current_ssid->p2p_group)
6675 return;
6676
6677 wpa_printf(MSG_DEBUG, "P2P: Remove group due to driver resource not "
6678 "being available anymore");
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07006679 wpas_p2p_group_delete(wpa_s, P2P_GROUP_REMOVAL_UNAVAILABLE);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006680}
6681
6682
6683void wpas_p2p_update_best_channels(struct wpa_supplicant *wpa_s,
6684 int freq_24, int freq_5, int freq_overall)
6685{
6686 struct p2p_data *p2p = wpa_s->global->p2p;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08006687 if (p2p == NULL)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006688 return;
6689 p2p_set_best_channels(p2p, freq_24, freq_5, freq_overall);
6690}
6691
6692
6693int wpas_p2p_unauthorize(struct wpa_supplicant *wpa_s, const char *addr)
6694{
6695 u8 peer[ETH_ALEN];
6696 struct p2p_data *p2p = wpa_s->global->p2p;
6697
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08006698 if (p2p == NULL)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006699 return -1;
6700
6701 if (hwaddr_aton(addr, peer))
6702 return -1;
6703
6704 return p2p_unauthorize(p2p, peer);
6705}
6706
6707
6708/**
6709 * wpas_p2p_disconnect - Disconnect from a P2P Group
6710 * @wpa_s: Pointer to wpa_supplicant data
6711 * Returns: 0 on success, -1 on failure
6712 *
6713 * This can be used to disconnect from a group in which the local end is a P2P
6714 * Client or to end a P2P Group in case the local end is the Group Owner. If a
6715 * virtual network interface was created for this group, that interface will be
6716 * removed. Otherwise, only the configured P2P group network will be removed
6717 * from the interface.
6718 */
6719int wpas_p2p_disconnect(struct wpa_supplicant *wpa_s)
6720{
6721
6722 if (wpa_s == NULL)
6723 return -1;
6724
Jouni Malinen2b89da82012-08-31 22:04:41 +03006725 return wpas_p2p_group_delete(wpa_s, P2P_GROUP_REMOVAL_REQUESTED) < 0 ?
6726 -1 : 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006727}
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006728
6729
6730int wpas_p2p_in_progress(struct wpa_supplicant *wpa_s)
6731{
Dmitry Shmidtf8623282013-02-20 14:34:59 -08006732 int ret;
6733
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006734 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
6735 return 0;
6736
Dmitry Shmidtf8623282013-02-20 14:34:59 -08006737 ret = p2p_in_progress(wpa_s->global->p2p);
6738 if (ret == 0) {
6739 /*
6740 * Check whether there is an ongoing WPS provisioning step (or
6741 * other parts of group formation) on another interface since
6742 * p2p_in_progress() does not report this to avoid issues for
6743 * scans during such provisioning step.
6744 */
6745 if (wpa_s->global->p2p_group_formation &&
6746 wpa_s->global->p2p_group_formation != wpa_s) {
6747 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Another interface (%s) "
6748 "in group formation",
6749 wpa_s->global->p2p_group_formation->ifname);
6750 ret = 1;
6751 }
6752 }
6753
Dmitry Shmidt92c368d2013-08-29 12:37:21 -07006754 if (!ret && wpa_s->global->p2p_go_wait_client.sec) {
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08006755 struct os_reltime now;
6756 os_get_reltime(&now);
6757 if (os_reltime_expired(&now, &wpa_s->global->p2p_go_wait_client,
6758 P2P_MAX_INITIAL_CONN_WAIT_GO)) {
Dmitry Shmidt92c368d2013-08-29 12:37:21 -07006759 /* Wait for the first client has expired */
6760 wpa_s->global->p2p_go_wait_client.sec = 0;
6761 } else {
6762 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Waiting for initial client connection during group formation");
6763 ret = 1;
6764 }
6765 }
6766
Dmitry Shmidtf8623282013-02-20 14:34:59 -08006767 return ret;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006768}
6769
6770
6771void wpas_p2p_network_removed(struct wpa_supplicant *wpa_s,
6772 struct wpa_ssid *ssid)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006773{
6774 if (wpa_s->p2p_in_provisioning && ssid->p2p_group &&
6775 eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
6776 wpa_s->parent, NULL) > 0) {
Dmitry Shmidt04949592012-07-19 12:16:46 -07006777 /**
6778 * Remove the network by scheduling the group formation
6779 * timeout to happen immediately. The teardown code
6780 * needs to be scheduled to run asynch later so that we
6781 * don't delete data from under ourselves unexpectedly.
6782 * Calling wpas_p2p_group_formation_timeout directly
6783 * causes a series of crashes in WPS failure scenarios.
6784 */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006785 wpa_printf(MSG_DEBUG, "P2P: Canceled group formation due to "
6786 "P2P group network getting removed");
Dmitry Shmidt04949592012-07-19 12:16:46 -07006787 eloop_register_timeout(0, 0, wpas_p2p_group_formation_timeout,
6788 wpa_s->parent, NULL);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006789 }
6790}
6791
6792
6793struct wpa_ssid * wpas_p2p_get_persistent(struct wpa_supplicant *wpa_s,
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08006794 const u8 *addr, const u8 *ssid,
6795 size_t ssid_len)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006796{
6797 struct wpa_ssid *s;
6798 size_t i;
6799
6800 for (s = wpa_s->conf->ssid; s; s = s->next) {
6801 if (s->disabled != 2)
6802 continue;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08006803 if (ssid &&
6804 (ssid_len != s->ssid_len ||
6805 os_memcmp(ssid, s->ssid, ssid_len) != 0))
6806 continue;
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07006807 if (addr == NULL) {
6808 if (s->mode == WPAS_MODE_P2P_GO)
6809 return s;
6810 continue;
6811 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006812 if (os_memcmp(s->bssid, addr, ETH_ALEN) == 0)
6813 return s; /* peer is GO in the persistent group */
6814 if (s->mode != WPAS_MODE_P2P_GO || s->p2p_client_list == NULL)
6815 continue;
6816 for (i = 0; i < s->num_p2p_clients; i++) {
6817 if (os_memcmp(s->p2p_client_list + i * ETH_ALEN,
6818 addr, ETH_ALEN) == 0)
6819 return s; /* peer is P2P client in persistent
6820 * group */
6821 }
6822 }
6823
6824 return NULL;
6825}
6826
6827
6828void wpas_p2p_notify_ap_sta_authorized(struct wpa_supplicant *wpa_s,
6829 const u8 *addr)
6830{
Dmitry Shmidt56052862013-10-04 10:23:25 -07006831 if (eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
6832 wpa_s->parent, NULL) > 0) {
6833 /*
6834 * This can happen if WPS provisioning step is not terminated
6835 * cleanly (e.g., P2P Client does not send WSC_Done). Since the
6836 * peer was able to connect, there is no need to time out group
6837 * formation after this, though. In addition, this is used with
6838 * the initial connection wait on the GO as a separate formation
6839 * timeout and as such, expected to be hit after the initial WPS
6840 * provisioning step.
6841 */
6842 wpa_printf(MSG_DEBUG, "P2P: Canceled P2P group formation timeout on data connection");
6843 }
6844 if (!wpa_s->p2p_go_group_formation_completed) {
6845 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Marking group formation completed on GO on first data connection");
6846 wpa_s->p2p_go_group_formation_completed = 1;
6847 wpa_s->global->p2p_group_formation = NULL;
6848 wpa_s->p2p_in_provisioning = 0;
Dmitry Shmidt21de2142014-04-08 10:50:52 -07006849 wpa_s->p2p_in_invitation = 0;
Dmitry Shmidt56052862013-10-04 10:23:25 -07006850 }
Dmitry Shmidt92c368d2013-08-29 12:37:21 -07006851 wpa_s->global->p2p_go_wait_client.sec = 0;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006852 if (addr == NULL)
6853 return;
6854 wpas_p2p_add_persistent_group_client(wpa_s, addr);
6855}
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08006856
Dmitry Shmidt04949592012-07-19 12:16:46 -07006857
6858static void wpas_p2p_fallback_to_go_neg(struct wpa_supplicant *wpa_s,
6859 int group_added)
6860{
6861 struct wpa_supplicant *group = wpa_s;
Dmitry Shmidt04949592012-07-19 12:16:46 -07006862 if (wpa_s->global->p2p_group_formation)
6863 group = wpa_s->global->p2p_group_formation;
6864 wpa_s = wpa_s->parent;
6865 offchannel_send_action_done(wpa_s);
6866 if (group_added)
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07006867 wpas_p2p_group_delete(group, P2P_GROUP_REMOVAL_SILENT);
Dmitry Shmidt04949592012-07-19 12:16:46 -07006868 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Fall back to GO Negotiation");
6869 wpas_p2p_connect(wpa_s, wpa_s->pending_join_dev_addr, wpa_s->p2p_pin,
6870 wpa_s->p2p_wps_method, wpa_s->p2p_persistent_group, 0,
6871 0, 0, wpa_s->p2p_go_intent, wpa_s->p2p_connect_freq,
6872 wpa_s->p2p_persistent_id,
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07006873 wpa_s->p2p_pd_before_go_neg,
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07006874 wpa_s->p2p_go_ht40,
6875 wpa_s->p2p_go_vht);
Dmitry Shmidt04949592012-07-19 12:16:46 -07006876}
6877
6878
6879int wpas_p2p_scan_no_go_seen(struct wpa_supplicant *wpa_s)
6880{
6881 if (!wpa_s->p2p_fallback_to_go_neg ||
6882 wpa_s->p2p_in_provisioning <= 5)
6883 return 0;
6884
6885 if (wpas_p2p_peer_go(wpa_s, wpa_s->pending_join_dev_addr) > 0)
6886 return 0; /* peer operating as a GO */
6887
6888 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: GO not found for p2p_connect-auto - "
6889 "fallback to GO Negotiation");
6890 wpas_p2p_fallback_to_go_neg(wpa_s, 1);
6891
6892 return 1;
6893}
6894
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07006895
6896unsigned int wpas_p2p_search_delay(struct wpa_supplicant *wpa_s)
6897{
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07006898 struct wpa_supplicant *ifs;
6899
6900 if (wpa_s->wpa_state > WPA_SCANNING) {
6901 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Use %u ms search delay due to "
6902 "concurrent operation",
Dmitry Shmidt09f57ba2014-06-10 16:07:13 -07006903 wpa_s->conf->p2p_search_delay);
6904 return wpa_s->conf->p2p_search_delay;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07006905 }
6906
Dmitry Shmidt01904cf2013-12-05 11:08:35 -08006907 dl_list_for_each(ifs, &wpa_s->radio->ifaces, struct wpa_supplicant,
6908 radio_list) {
6909 if (ifs != wpa_s && ifs->wpa_state > WPA_SCANNING) {
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07006910 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Use %u ms search "
6911 "delay due to concurrent operation on "
6912 "interface %s",
Dmitry Shmidt09f57ba2014-06-10 16:07:13 -07006913 wpa_s->conf->p2p_search_delay,
6914 ifs->ifname);
6915 return wpa_s->conf->p2p_search_delay;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07006916 }
6917 }
6918
6919 return 0;
6920}
6921
Dmitry Shmidt37d4d6a2013-03-18 13:09:42 -07006922
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07006923static int wpas_p2p_remove_psk_entry(struct wpa_supplicant *wpa_s,
6924 struct wpa_ssid *s, const u8 *addr,
6925 int iface_addr)
6926{
6927 struct psk_list_entry *psk, *tmp;
6928 int changed = 0;
6929
6930 dl_list_for_each_safe(psk, tmp, &s->psk_list, struct psk_list_entry,
6931 list) {
6932 if ((iface_addr && !psk->p2p &&
6933 os_memcmp(addr, psk->addr, ETH_ALEN) == 0) ||
6934 (!iface_addr && psk->p2p &&
6935 os_memcmp(addr, psk->addr, ETH_ALEN) == 0)) {
6936 wpa_dbg(wpa_s, MSG_DEBUG,
6937 "P2P: Remove persistent group PSK list entry for "
6938 MACSTR " p2p=%u",
6939 MAC2STR(psk->addr), psk->p2p);
6940 dl_list_del(&psk->list);
6941 os_free(psk);
6942 changed++;
6943 }
6944 }
6945
6946 return changed;
6947}
6948
6949
6950void wpas_p2p_new_psk_cb(struct wpa_supplicant *wpa_s, const u8 *mac_addr,
6951 const u8 *p2p_dev_addr,
6952 const u8 *psk, size_t psk_len)
6953{
6954 struct wpa_ssid *ssid = wpa_s->current_ssid;
6955 struct wpa_ssid *persistent;
Dmitry Shmidt7832adb2014-04-29 10:53:02 -07006956 struct psk_list_entry *p, *last;
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07006957
6958 if (psk_len != sizeof(p->psk))
6959 return;
6960
6961 if (p2p_dev_addr) {
6962 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: New PSK for addr=" MACSTR
6963 " p2p_dev_addr=" MACSTR,
6964 MAC2STR(mac_addr), MAC2STR(p2p_dev_addr));
6965 if (is_zero_ether_addr(p2p_dev_addr))
6966 p2p_dev_addr = NULL;
6967 } else {
6968 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: New PSK for addr=" MACSTR,
6969 MAC2STR(mac_addr));
6970 }
6971
6972 if (ssid->mode == WPAS_MODE_P2P_GROUP_FORMATION) {
6973 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: new_psk_cb during group formation");
6974 /* To be added to persistent group once created */
6975 if (wpa_s->global->add_psk == NULL) {
6976 wpa_s->global->add_psk = os_zalloc(sizeof(*p));
6977 if (wpa_s->global->add_psk == NULL)
6978 return;
6979 }
6980 p = wpa_s->global->add_psk;
6981 if (p2p_dev_addr) {
6982 p->p2p = 1;
6983 os_memcpy(p->addr, p2p_dev_addr, ETH_ALEN);
6984 } else {
6985 p->p2p = 0;
6986 os_memcpy(p->addr, mac_addr, ETH_ALEN);
6987 }
6988 os_memcpy(p->psk, psk, psk_len);
6989 return;
6990 }
6991
6992 if (ssid->mode != WPAS_MODE_P2P_GO || !ssid->p2p_persistent_group) {
6993 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Ignore new_psk_cb on not-persistent GO");
6994 return;
6995 }
6996
6997 persistent = wpas_p2p_get_persistent(wpa_s->parent, NULL, ssid->ssid,
6998 ssid->ssid_len);
6999 if (!persistent) {
7000 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Could not find persistent group information to store the new PSK");
7001 return;
7002 }
7003
7004 p = os_zalloc(sizeof(*p));
7005 if (p == NULL)
7006 return;
7007 if (p2p_dev_addr) {
7008 p->p2p = 1;
7009 os_memcpy(p->addr, p2p_dev_addr, ETH_ALEN);
7010 } else {
7011 p->p2p = 0;
7012 os_memcpy(p->addr, mac_addr, ETH_ALEN);
7013 }
7014 os_memcpy(p->psk, psk, psk_len);
7015
Dmitry Shmidt7832adb2014-04-29 10:53:02 -07007016 if (dl_list_len(&persistent->psk_list) > P2P_MAX_STORED_CLIENTS &&
7017 (last = dl_list_last(&persistent->psk_list,
7018 struct psk_list_entry, list))) {
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07007019 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Remove oldest PSK entry for "
7020 MACSTR " (p2p=%u) to make room for a new one",
7021 MAC2STR(last->addr), last->p2p);
7022 dl_list_del(&last->list);
7023 os_free(last);
7024 }
7025
7026 wpas_p2p_remove_psk_entry(wpa_s->parent, persistent,
7027 p2p_dev_addr ? p2p_dev_addr : mac_addr,
7028 p2p_dev_addr == NULL);
7029 if (p2p_dev_addr) {
7030 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Add new PSK for p2p_dev_addr="
7031 MACSTR, MAC2STR(p2p_dev_addr));
7032 } else {
7033 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Add new PSK for addr=" MACSTR,
7034 MAC2STR(mac_addr));
7035 }
7036 dl_list_add(&persistent->psk_list, &p->list);
7037
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07007038 if (wpa_s->parent->conf->update_config &&
7039 wpa_config_write(wpa_s->parent->confname, wpa_s->parent->conf))
7040 wpa_printf(MSG_DEBUG, "P2P: Failed to update configuration");
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07007041}
7042
7043
7044static void wpas_p2p_remove_psk(struct wpa_supplicant *wpa_s,
7045 struct wpa_ssid *s, const u8 *addr,
7046 int iface_addr)
7047{
7048 int res;
7049
7050 res = wpas_p2p_remove_psk_entry(wpa_s, s, addr, iface_addr);
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08007051 if (res > 0 && wpa_s->conf->update_config &&
7052 wpa_config_write(wpa_s->confname, wpa_s->conf))
7053 wpa_dbg(wpa_s, MSG_DEBUG,
7054 "P2P: Failed to update configuration");
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07007055}
7056
7057
7058static void wpas_p2p_remove_client_go(struct wpa_supplicant *wpa_s,
7059 const u8 *peer, int iface_addr)
7060{
7061 struct hostapd_data *hapd;
7062 struct hostapd_wpa_psk *psk, *prev, *rem;
7063 struct sta_info *sta;
7064
7065 if (wpa_s->ap_iface == NULL || wpa_s->current_ssid == NULL ||
7066 wpa_s->current_ssid->mode != WPAS_MODE_P2P_GO)
7067 return;
7068
7069 /* Remove per-station PSK entry */
7070 hapd = wpa_s->ap_iface->bss[0];
7071 prev = NULL;
7072 psk = hapd->conf->ssid.wpa_psk;
7073 while (psk) {
7074 if ((iface_addr && os_memcmp(peer, psk->addr, ETH_ALEN) == 0) ||
7075 (!iface_addr &&
7076 os_memcmp(peer, psk->p2p_dev_addr, ETH_ALEN) == 0)) {
7077 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Remove operating group PSK entry for "
7078 MACSTR " iface_addr=%d",
7079 MAC2STR(peer), iface_addr);
7080 if (prev)
7081 prev->next = psk->next;
7082 else
7083 hapd->conf->ssid.wpa_psk = psk->next;
7084 rem = psk;
7085 psk = psk->next;
7086 os_free(rem);
7087 } else {
7088 prev = psk;
7089 psk = psk->next;
7090 }
7091 }
7092
7093 /* Disconnect from group */
7094 if (iface_addr)
7095 sta = ap_get_sta(hapd, peer);
7096 else
7097 sta = ap_get_sta_p2p(hapd, peer);
7098 if (sta) {
7099 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Disconnect peer " MACSTR
7100 " (iface_addr=%d) from group",
7101 MAC2STR(peer), iface_addr);
7102 hostapd_drv_sta_deauth(hapd, sta->addr,
7103 WLAN_REASON_DEAUTH_LEAVING);
7104 ap_sta_deauthenticate(hapd, sta, WLAN_REASON_DEAUTH_LEAVING);
7105 }
7106}
7107
7108
7109void wpas_p2p_remove_client(struct wpa_supplicant *wpa_s, const u8 *peer,
7110 int iface_addr)
7111{
7112 struct wpa_ssid *s;
7113 struct wpa_supplicant *w;
7114
7115 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Remove client " MACSTR, MAC2STR(peer));
7116
7117 /* Remove from any persistent group */
7118 for (s = wpa_s->parent->conf->ssid; s; s = s->next) {
7119 if (s->disabled != 2 || s->mode != WPAS_MODE_P2P_GO)
7120 continue;
7121 if (!iface_addr)
7122 wpas_remove_persistent_peer(wpa_s, s, peer, 0);
7123 wpas_p2p_remove_psk(wpa_s->parent, s, peer, iface_addr);
7124 }
7125
7126 /* Remove from any operating group */
7127 for (w = wpa_s->global->ifaces; w; w = w->next)
7128 wpas_p2p_remove_client_go(w, peer, iface_addr);
7129}
7130
7131
7132static void wpas_p2p_psk_failure_removal(void *eloop_ctx, void *timeout_ctx)
7133{
7134 struct wpa_supplicant *wpa_s = eloop_ctx;
7135 wpas_p2p_group_delete(wpa_s, P2P_GROUP_REMOVAL_PSK_FAILURE);
7136}
7137
7138
Dmitry Shmidt04f534e2013-12-09 15:50:16 -08007139static void wpas_p2p_group_freq_conflict(void *eloop_ctx, void *timeout_ctx)
7140{
7141 struct wpa_supplicant *wpa_s = eloop_ctx;
7142
7143 wpa_printf(MSG_DEBUG, "P2P: Frequency conflict - terminate group");
7144 wpas_p2p_group_delete(wpa_s, P2P_GROUP_REMOVAL_FREQ_CONFLICT);
7145}
7146
7147
7148int wpas_p2p_handle_frequency_conflicts(struct wpa_supplicant *wpa_s, int freq,
7149 struct wpa_ssid *ssid)
7150{
7151 struct wpa_supplicant *iface;
7152
7153 for (iface = wpa_s->global->ifaces; iface; iface = iface->next) {
7154 if (!iface->current_ssid ||
7155 iface->current_ssid->frequency == freq ||
7156 (iface->p2p_group_interface == NOT_P2P_GROUP_INTERFACE &&
7157 !iface->current_ssid->p2p_group))
7158 continue;
7159
7160 /* Remove the connection with least priority */
7161 if (!wpas_is_p2p_prioritized(iface)) {
7162 /* STA connection has priority over existing
7163 * P2P connection, so remove the interface. */
7164 wpa_printf(MSG_DEBUG, "P2P: Removing P2P connection due to single channel concurrent mode frequency conflict");
7165 eloop_register_timeout(0, 0,
7166 wpas_p2p_group_freq_conflict,
7167 iface, NULL);
7168 /* If connection in progress is P2P connection, do not
7169 * proceed for the connection. */
7170 if (wpa_s == iface)
7171 return -1;
7172 else
7173 return 0;
7174 } else {
7175 /* P2P connection has priority, disable the STA network
7176 */
7177 wpa_supplicant_disable_network(wpa_s->global->ifaces,
7178 ssid);
7179 wpa_msg(wpa_s->global->ifaces, MSG_INFO,
7180 WPA_EVENT_FREQ_CONFLICT " id=%d", ssid->id);
7181 os_memset(wpa_s->global->ifaces->pending_bssid, 0,
7182 ETH_ALEN);
7183 /* If P2P connection is in progress, continue
7184 * connecting...*/
7185 if (wpa_s == iface)
7186 return 0;
7187 else
7188 return -1;
7189 }
7190 }
7191
7192 return 0;
7193}
7194
7195
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07007196int wpas_p2p_4way_hs_failed(struct wpa_supplicant *wpa_s)
7197{
7198 struct wpa_ssid *ssid = wpa_s->current_ssid;
7199
7200 if (ssid == NULL || !ssid->p2p_group)
7201 return 0;
7202
7203 if (wpa_s->p2p_last_4way_hs_fail &&
7204 wpa_s->p2p_last_4way_hs_fail == ssid) {
7205 u8 go_dev_addr[ETH_ALEN];
7206 struct wpa_ssid *persistent;
7207
7208 if (wpas_p2p_persistent_group(wpa_s, go_dev_addr,
7209 ssid->ssid,
7210 ssid->ssid_len) <= 0) {
7211 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Could not determine whether 4-way handshake failures were for a persistent group");
7212 goto disconnect;
7213 }
7214
7215 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Two 4-way handshake failures for a P2P group - go_dev_addr="
7216 MACSTR, MAC2STR(go_dev_addr));
7217 persistent = wpas_p2p_get_persistent(wpa_s->parent, go_dev_addr,
7218 ssid->ssid,
7219 ssid->ssid_len);
7220 if (persistent == NULL || persistent->mode != WPAS_MODE_INFRA) {
7221 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: No matching persistent group stored");
7222 goto disconnect;
7223 }
7224 wpa_msg_global(wpa_s->parent, MSG_INFO,
7225 P2P_EVENT_PERSISTENT_PSK_FAIL "%d",
7226 persistent->id);
7227 disconnect:
7228 wpa_s->p2p_last_4way_hs_fail = NULL;
7229 /*
7230 * Remove the group from a timeout to avoid issues with caller
7231 * continuing to use the interface if this is on a P2P group
7232 * interface.
7233 */
7234 eloop_register_timeout(0, 0, wpas_p2p_psk_failure_removal,
7235 wpa_s, NULL);
7236 return 1;
7237 }
7238
7239 wpa_s->p2p_last_4way_hs_fail = ssid;
7240 return 0;
7241}
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08007242
7243
7244#ifdef CONFIG_WPS_NFC
7245
7246static struct wpabuf * wpas_p2p_nfc_handover(int ndef, struct wpabuf *wsc,
7247 struct wpabuf *p2p)
7248{
7249 struct wpabuf *ret;
7250 size_t wsc_len;
7251
7252 if (p2p == NULL) {
7253 wpabuf_free(wsc);
7254 wpa_printf(MSG_DEBUG, "P2P: No p2p buffer for handover");
7255 return NULL;
7256 }
7257
7258 wsc_len = wsc ? wpabuf_len(wsc) : 0;
7259 ret = wpabuf_alloc(2 + wsc_len + 2 + wpabuf_len(p2p));
7260 if (ret == NULL) {
7261 wpabuf_free(wsc);
7262 wpabuf_free(p2p);
7263 return NULL;
7264 }
7265
7266 wpabuf_put_be16(ret, wsc_len);
7267 if (wsc)
7268 wpabuf_put_buf(ret, wsc);
7269 wpabuf_put_be16(ret, wpabuf_len(p2p));
7270 wpabuf_put_buf(ret, p2p);
7271
7272 wpabuf_free(wsc);
7273 wpabuf_free(p2p);
7274 wpa_hexdump_buf(MSG_DEBUG,
7275 "P2P: Generated NFC connection handover message", ret);
7276
7277 if (ndef && ret) {
7278 struct wpabuf *tmp;
7279 tmp = ndef_build_p2p(ret);
7280 wpabuf_free(ret);
7281 if (tmp == NULL) {
7282 wpa_printf(MSG_DEBUG, "P2P: Failed to NDEF encapsulate handover request");
7283 return NULL;
7284 }
7285 ret = tmp;
7286 }
7287
7288 return ret;
7289}
7290
7291
7292static int wpas_p2p_cli_freq(struct wpa_supplicant *wpa_s,
7293 struct wpa_ssid **ssid, u8 *go_dev_addr)
7294{
7295 struct wpa_supplicant *iface;
7296
7297 if (go_dev_addr)
7298 os_memset(go_dev_addr, 0, ETH_ALEN);
7299 if (ssid)
7300 *ssid = NULL;
7301 for (iface = wpa_s->global->ifaces; iface; iface = iface->next) {
7302 if (iface->wpa_state < WPA_ASSOCIATING ||
7303 iface->current_ssid == NULL || iface->assoc_freq == 0 ||
7304 !iface->current_ssid->p2p_group ||
7305 iface->current_ssid->mode != WPAS_MODE_INFRA)
7306 continue;
7307 if (ssid)
7308 *ssid = iface->current_ssid;
7309 if (go_dev_addr)
7310 os_memcpy(go_dev_addr, iface->go_dev_addr, ETH_ALEN);
7311 return iface->assoc_freq;
7312 }
7313 return 0;
7314}
7315
7316
7317struct wpabuf * wpas_p2p_nfc_handover_req(struct wpa_supplicant *wpa_s,
7318 int ndef)
7319{
7320 struct wpabuf *wsc, *p2p;
7321 struct wpa_ssid *ssid;
7322 u8 go_dev_addr[ETH_ALEN];
7323 int cli_freq = wpas_p2p_cli_freq(wpa_s, &ssid, go_dev_addr);
7324
7325 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL) {
7326 wpa_printf(MSG_DEBUG, "P2P: P2P disabled - cannot build handover request");
7327 return NULL;
7328 }
7329
7330 if (wpa_s->conf->wps_nfc_dh_pubkey == NULL &&
7331 wps_nfc_gen_dh(&wpa_s->conf->wps_nfc_dh_pubkey,
7332 &wpa_s->conf->wps_nfc_dh_privkey) < 0) {
7333 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: No DH key available for handover request");
7334 return NULL;
7335 }
7336
7337 if (cli_freq == 0) {
7338 wsc = wps_build_nfc_handover_req_p2p(
7339 wpa_s->parent->wps, wpa_s->conf->wps_nfc_dh_pubkey);
7340 } else
7341 wsc = NULL;
7342 p2p = p2p_build_nfc_handover_req(wpa_s->global->p2p, cli_freq,
7343 go_dev_addr, ssid ? ssid->ssid : NULL,
7344 ssid ? ssid->ssid_len : 0);
7345
7346 return wpas_p2p_nfc_handover(ndef, wsc, p2p);
7347}
7348
7349
7350struct wpabuf * wpas_p2p_nfc_handover_sel(struct wpa_supplicant *wpa_s,
7351 int ndef, int tag)
7352{
7353 struct wpabuf *wsc, *p2p;
7354 struct wpa_ssid *ssid;
7355 u8 go_dev_addr[ETH_ALEN];
7356 int cli_freq = wpas_p2p_cli_freq(wpa_s, &ssid, go_dev_addr);
7357
7358 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
7359 return NULL;
7360
7361 if (!tag && wpa_s->conf->wps_nfc_dh_pubkey == NULL &&
7362 wps_nfc_gen_dh(&wpa_s->conf->wps_nfc_dh_pubkey,
7363 &wpa_s->conf->wps_nfc_dh_privkey) < 0)
7364 return NULL;
7365
7366 if (cli_freq == 0) {
7367 wsc = wps_build_nfc_handover_sel_p2p(
7368 wpa_s->parent->wps,
7369 tag ? wpa_s->conf->wps_nfc_dev_pw_id :
7370 DEV_PW_NFC_CONNECTION_HANDOVER,
7371 wpa_s->conf->wps_nfc_dh_pubkey,
7372 tag ? wpa_s->conf->wps_nfc_dev_pw : NULL);
7373 } else
7374 wsc = NULL;
7375 p2p = p2p_build_nfc_handover_sel(wpa_s->global->p2p, cli_freq,
7376 go_dev_addr, ssid ? ssid->ssid : NULL,
7377 ssid ? ssid->ssid_len : 0);
7378
7379 return wpas_p2p_nfc_handover(ndef, wsc, p2p);
7380}
7381
7382
7383static int wpas_p2p_nfc_join_group(struct wpa_supplicant *wpa_s,
7384 struct p2p_nfc_params *params)
7385{
7386 wpa_printf(MSG_DEBUG, "P2P: Initiate join-group based on NFC "
7387 "connection handover (freq=%d)",
7388 params->go_freq);
7389
7390 if (params->go_freq && params->go_ssid_len) {
7391 wpa_s->p2p_wps_method = WPS_NFC;
7392 wpa_s->pending_join_wps_method = WPS_NFC;
7393 os_memset(wpa_s->pending_join_iface_addr, 0, ETH_ALEN);
7394 os_memcpy(wpa_s->pending_join_dev_addr, params->go_dev_addr,
7395 ETH_ALEN);
7396 return wpas_p2p_join_start(wpa_s, params->go_freq,
7397 params->go_ssid,
7398 params->go_ssid_len);
7399 }
7400
7401 return wpas_p2p_connect(wpa_s, params->peer->p2p_device_addr, NULL,
7402 WPS_NFC, 0, 0, 1, 0, wpa_s->conf->p2p_go_intent,
7403 params->go_freq, -1, 0, 1, 1);
7404}
7405
7406
7407static int wpas_p2p_nfc_auth_join(struct wpa_supplicant *wpa_s,
7408 struct p2p_nfc_params *params, int tag)
7409{
7410 int res, persistent;
7411 struct wpa_ssid *ssid;
7412
7413 wpa_printf(MSG_DEBUG, "P2P: Authorize join-group based on NFC "
7414 "connection handover");
7415 for (wpa_s = wpa_s->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
7416 ssid = wpa_s->current_ssid;
7417 if (ssid == NULL)
7418 continue;
7419 if (ssid->mode != WPAS_MODE_P2P_GO)
7420 continue;
7421 if (wpa_s->ap_iface == NULL)
7422 continue;
7423 break;
7424 }
7425 if (wpa_s == NULL) {
7426 wpa_printf(MSG_DEBUG, "P2P: Could not find GO interface");
7427 return -1;
7428 }
7429
7430 if (wpa_s->parent->p2p_oob_dev_pw_id !=
7431 DEV_PW_NFC_CONNECTION_HANDOVER &&
7432 !wpa_s->parent->p2p_oob_dev_pw) {
7433 wpa_printf(MSG_DEBUG, "P2P: No NFC Dev Pw known");
7434 return -1;
7435 }
7436 res = wpas_ap_wps_add_nfc_pw(
7437 wpa_s, wpa_s->parent->p2p_oob_dev_pw_id,
7438 wpa_s->parent->p2p_oob_dev_pw,
7439 wpa_s->parent->p2p_peer_oob_pk_hash_known ?
7440 wpa_s->parent->p2p_peer_oob_pubkey_hash : NULL);
7441 if (res)
7442 return res;
7443
7444 if (!tag) {
7445 wpa_printf(MSG_DEBUG, "P2P: Negotiated handover - wait for peer to join without invitation");
7446 return 0;
7447 }
7448
7449 if (!params->peer ||
7450 !(params->peer->dev_capab & P2P_DEV_CAPAB_INVITATION_PROCEDURE))
7451 return 0;
7452
7453 wpa_printf(MSG_DEBUG, "P2P: Static handover - invite peer " MACSTR
7454 " to join", MAC2STR(params->peer->p2p_device_addr));
7455
7456 wpa_s->global->p2p_invite_group = wpa_s;
7457 persistent = ssid->p2p_persistent_group &&
7458 wpas_p2p_get_persistent(wpa_s->parent,
7459 params->peer->p2p_device_addr,
7460 ssid->ssid, ssid->ssid_len);
7461 wpa_s->parent->pending_invite_ssid_id = -1;
7462
7463 return p2p_invite(wpa_s->global->p2p, params->peer->p2p_device_addr,
7464 P2P_INVITE_ROLE_ACTIVE_GO, wpa_s->own_addr,
7465 ssid->ssid, ssid->ssid_len, ssid->frequency,
7466 wpa_s->global->p2p_dev_addr, persistent, 0,
7467 wpa_s->parent->p2p_oob_dev_pw_id);
7468}
7469
7470
7471static int wpas_p2p_nfc_init_go_neg(struct wpa_supplicant *wpa_s,
7472 struct p2p_nfc_params *params,
7473 int forced_freq)
7474{
7475 wpa_printf(MSG_DEBUG, "P2P: Initiate GO Negotiation based on NFC "
7476 "connection handover");
7477 return wpas_p2p_connect(wpa_s, params->peer->p2p_device_addr, NULL,
7478 WPS_NFC, 0, 0, 0, 0, wpa_s->conf->p2p_go_intent,
7479 forced_freq, -1, 0, 1, 1);
7480}
7481
7482
7483static int wpas_p2p_nfc_resp_go_neg(struct wpa_supplicant *wpa_s,
7484 struct p2p_nfc_params *params,
7485 int forced_freq)
7486{
7487 int res;
7488
7489 wpa_printf(MSG_DEBUG, "P2P: Authorize GO Negotiation based on NFC "
7490 "connection handover");
7491 res = wpas_p2p_connect(wpa_s, params->peer->p2p_device_addr, NULL,
7492 WPS_NFC, 0, 0, 0, 1, wpa_s->conf->p2p_go_intent,
7493 forced_freq, -1, 0, 1, 1);
7494 if (res)
7495 return res;
7496
7497 res = wpas_p2p_listen(wpa_s, 60);
7498 if (res) {
7499 p2p_unauthorize(wpa_s->global->p2p,
7500 params->peer->p2p_device_addr);
7501 }
7502
7503 return res;
7504}
7505
7506
7507static int wpas_p2p_nfc_connection_handover(struct wpa_supplicant *wpa_s,
7508 const struct wpabuf *data,
7509 int sel, int tag, int forced_freq)
7510{
7511 const u8 *pos, *end;
7512 u16 len, id;
7513 struct p2p_nfc_params params;
7514 int res;
7515
7516 os_memset(&params, 0, sizeof(params));
7517 params.sel = sel;
7518
7519 wpa_hexdump_buf(MSG_DEBUG, "P2P: Received NFC tag payload", data);
7520
7521 pos = wpabuf_head(data);
7522 end = pos + wpabuf_len(data);
7523
7524 if (end - pos < 2) {
7525 wpa_printf(MSG_DEBUG, "P2P: Not enough data for Length of WSC "
7526 "attributes");
7527 return -1;
7528 }
7529 len = WPA_GET_BE16(pos);
7530 pos += 2;
7531 if (pos + len > end) {
7532 wpa_printf(MSG_DEBUG, "P2P: Not enough data for WSC "
7533 "attributes");
7534 return -1;
7535 }
7536 params.wsc_attr = pos;
7537 params.wsc_len = len;
7538 pos += len;
7539
7540 if (end - pos < 2) {
7541 wpa_printf(MSG_DEBUG, "P2P: Not enough data for Length of P2P "
7542 "attributes");
7543 return -1;
7544 }
7545 len = WPA_GET_BE16(pos);
7546 pos += 2;
7547 if (pos + len > end) {
7548 wpa_printf(MSG_DEBUG, "P2P: Not enough data for P2P "
7549 "attributes");
7550 return -1;
7551 }
7552 params.p2p_attr = pos;
7553 params.p2p_len = len;
7554 pos += len;
7555
7556 wpa_hexdump(MSG_DEBUG, "P2P: WSC attributes",
7557 params.wsc_attr, params.wsc_len);
7558 wpa_hexdump(MSG_DEBUG, "P2P: P2P attributes",
7559 params.p2p_attr, params.p2p_len);
7560 if (pos < end) {
7561 wpa_hexdump(MSG_DEBUG,
7562 "P2P: Ignored extra data after P2P attributes",
7563 pos, end - pos);
7564 }
7565
7566 res = p2p_process_nfc_connection_handover(wpa_s->global->p2p, &params);
7567 if (res)
7568 return res;
7569
7570 if (params.next_step == NO_ACTION)
7571 return 0;
7572
7573 if (params.next_step == BOTH_GO) {
7574 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_NFC_BOTH_GO "peer=" MACSTR,
7575 MAC2STR(params.peer->p2p_device_addr));
7576 return 0;
7577 }
7578
7579 if (params.next_step == PEER_CLIENT) {
7580 if (!is_zero_ether_addr(params.go_dev_addr)) {
7581 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_NFC_PEER_CLIENT
7582 "peer=" MACSTR " freq=%d go_dev_addr=" MACSTR
7583 " ssid=\"%s\"",
7584 MAC2STR(params.peer->p2p_device_addr),
7585 params.go_freq,
7586 MAC2STR(params.go_dev_addr),
7587 wpa_ssid_txt(params.go_ssid,
7588 params.go_ssid_len));
7589 } else {
7590 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_NFC_PEER_CLIENT
7591 "peer=" MACSTR " freq=%d",
7592 MAC2STR(params.peer->p2p_device_addr),
7593 params.go_freq);
7594 }
7595 return 0;
7596 }
7597
7598 if (wpas_p2p_cli_freq(wpa_s, NULL, NULL)) {
7599 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_NFC_WHILE_CLIENT "peer="
7600 MACSTR, MAC2STR(params.peer->p2p_device_addr));
7601 return 0;
7602 }
7603
7604 wpabuf_free(wpa_s->p2p_oob_dev_pw);
7605 wpa_s->p2p_oob_dev_pw = NULL;
7606
7607 if (params.oob_dev_pw_len < WPS_OOB_PUBKEY_HASH_LEN + 2) {
7608 wpa_printf(MSG_DEBUG, "P2P: No peer OOB Dev Pw "
7609 "received");
7610 return -1;
7611 }
7612
7613 id = WPA_GET_BE16(params.oob_dev_pw + WPS_OOB_PUBKEY_HASH_LEN);
7614 wpa_printf(MSG_DEBUG, "P2P: Peer OOB Dev Pw %u", id);
7615 wpa_hexdump(MSG_DEBUG, "P2P: Peer OOB Public Key hash",
7616 params.oob_dev_pw, WPS_OOB_PUBKEY_HASH_LEN);
7617 os_memcpy(wpa_s->p2p_peer_oob_pubkey_hash,
7618 params.oob_dev_pw, WPS_OOB_PUBKEY_HASH_LEN);
7619 wpa_s->p2p_peer_oob_pk_hash_known = 1;
7620
7621 if (tag) {
7622 if (id < 0x10) {
7623 wpa_printf(MSG_DEBUG, "P2P: Static handover - invalid "
7624 "peer OOB Device Password Id %u", id);
7625 return -1;
7626 }
7627 wpa_printf(MSG_DEBUG, "P2P: Static handover - use peer OOB "
7628 "Device Password Id %u", id);
7629 wpa_hexdump_key(MSG_DEBUG, "P2P: Peer OOB Device Password",
7630 params.oob_dev_pw + WPS_OOB_PUBKEY_HASH_LEN + 2,
7631 params.oob_dev_pw_len -
7632 WPS_OOB_PUBKEY_HASH_LEN - 2);
7633 wpa_s->p2p_oob_dev_pw_id = id;
7634 wpa_s->p2p_oob_dev_pw = wpabuf_alloc_copy(
7635 params.oob_dev_pw + WPS_OOB_PUBKEY_HASH_LEN + 2,
7636 params.oob_dev_pw_len -
7637 WPS_OOB_PUBKEY_HASH_LEN - 2);
7638 if (wpa_s->p2p_oob_dev_pw == NULL)
7639 return -1;
7640
7641 if (wpa_s->conf->wps_nfc_dh_pubkey == NULL &&
7642 wps_nfc_gen_dh(&wpa_s->conf->wps_nfc_dh_pubkey,
7643 &wpa_s->conf->wps_nfc_dh_privkey) < 0)
7644 return -1;
7645 } else {
7646 wpa_printf(MSG_DEBUG, "P2P: Using abbreviated WPS handshake "
7647 "without Device Password");
7648 wpa_s->p2p_oob_dev_pw_id = DEV_PW_NFC_CONNECTION_HANDOVER;
7649 }
7650
7651 switch (params.next_step) {
7652 case NO_ACTION:
7653 case BOTH_GO:
7654 case PEER_CLIENT:
7655 /* already covered above */
7656 return 0;
7657 case JOIN_GROUP:
7658 return wpas_p2p_nfc_join_group(wpa_s, &params);
7659 case AUTH_JOIN:
7660 return wpas_p2p_nfc_auth_join(wpa_s, &params, tag);
7661 case INIT_GO_NEG:
7662 return wpas_p2p_nfc_init_go_neg(wpa_s, &params, forced_freq);
7663 case RESP_GO_NEG:
7664 /* TODO: use own OOB Dev Pw */
7665 return wpas_p2p_nfc_resp_go_neg(wpa_s, &params, forced_freq);
7666 }
7667
7668 return -1;
7669}
7670
7671
7672int wpas_p2p_nfc_tag_process(struct wpa_supplicant *wpa_s,
7673 const struct wpabuf *data, int forced_freq)
7674{
7675 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
7676 return -1;
7677
7678 return wpas_p2p_nfc_connection_handover(wpa_s, data, 1, 1, forced_freq);
7679}
7680
7681
7682int wpas_p2p_nfc_report_handover(struct wpa_supplicant *wpa_s, int init,
7683 const struct wpabuf *req,
7684 const struct wpabuf *sel, int forced_freq)
7685{
7686 struct wpabuf *tmp;
7687 int ret;
7688
7689 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
7690 return -1;
7691
7692 wpa_printf(MSG_DEBUG, "NFC: P2P connection handover reported");
7693
7694 wpa_hexdump_ascii(MSG_DEBUG, "NFC: Req",
7695 wpabuf_head(req), wpabuf_len(req));
7696 wpa_hexdump_ascii(MSG_DEBUG, "NFC: Sel",
7697 wpabuf_head(sel), wpabuf_len(sel));
7698 if (forced_freq)
7699 wpa_printf(MSG_DEBUG, "NFC: Forced freq %d", forced_freq);
7700 tmp = ndef_parse_p2p(init ? sel : req);
7701 if (tmp == NULL) {
7702 wpa_printf(MSG_DEBUG, "P2P: Could not parse NDEF");
7703 return -1;
7704 }
7705
7706 ret = wpas_p2p_nfc_connection_handover(wpa_s, tmp, init, 0,
7707 forced_freq);
7708 wpabuf_free(tmp);
7709
7710 return ret;
7711}
7712
7713
7714int wpas_p2p_nfc_tag_enabled(struct wpa_supplicant *wpa_s, int enabled)
7715{
7716 const u8 *if_addr;
7717 int go_intent = wpa_s->conf->p2p_go_intent;
7718 struct wpa_supplicant *iface;
7719
7720 if (wpa_s->global->p2p == NULL)
7721 return -1;
7722
7723 if (!enabled) {
7724 wpa_printf(MSG_DEBUG, "P2P: Disable use of own NFC Tag");
7725 for (iface = wpa_s->global->ifaces; iface; iface = iface->next)
7726 {
7727 if (!iface->ap_iface)
7728 continue;
7729 hostapd_wps_nfc_token_disable(iface->ap_iface->bss[0]);
7730 }
7731 p2p_set_authorized_oob_dev_pw_id(wpa_s->global->p2p, 0,
7732 0, NULL);
7733 if (wpa_s->p2p_nfc_tag_enabled)
7734 wpas_p2p_remove_pending_group_interface(wpa_s);
7735 wpa_s->p2p_nfc_tag_enabled = 0;
7736 return 0;
7737 }
7738
7739 if (wpa_s->global->p2p_disabled)
7740 return -1;
7741
7742 if (wpa_s->conf->wps_nfc_dh_pubkey == NULL ||
7743 wpa_s->conf->wps_nfc_dh_privkey == NULL ||
7744 wpa_s->conf->wps_nfc_dev_pw == NULL ||
7745 wpa_s->conf->wps_nfc_dev_pw_id < 0x10) {
7746 wpa_printf(MSG_DEBUG, "P2P: NFC password token not configured "
7747 "to allow static handover cases");
7748 return -1;
7749 }
7750
7751 wpa_printf(MSG_DEBUG, "P2P: Enable use of own NFC Tag");
7752
7753 wpa_s->p2p_oob_dev_pw_id = wpa_s->conf->wps_nfc_dev_pw_id;
7754 wpabuf_free(wpa_s->p2p_oob_dev_pw);
7755 wpa_s->p2p_oob_dev_pw = wpabuf_dup(wpa_s->conf->wps_nfc_dev_pw);
7756 if (wpa_s->p2p_oob_dev_pw == NULL)
7757 return -1;
7758 wpa_s->p2p_peer_oob_pk_hash_known = 0;
7759
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -07007760 if (wpa_s->p2p_group_interface == P2P_GROUP_INTERFACE_GO ||
7761 wpa_s->p2p_group_interface == P2P_GROUP_INTERFACE_CLIENT) {
7762 /*
7763 * P2P Group Interface present and the command came on group
7764 * interface, so enable the token for the current interface.
7765 */
7766 wpa_s->create_p2p_iface = 0;
7767 } else {
7768 wpa_s->create_p2p_iface = wpas_p2p_create_iface(wpa_s);
7769 }
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08007770
7771 if (wpa_s->create_p2p_iface) {
7772 enum wpa_driver_if_type iftype;
7773 /* Prepare to add a new interface for the group */
7774 iftype = WPA_IF_P2P_GROUP;
7775 if (go_intent == 15)
7776 iftype = WPA_IF_P2P_GO;
7777 if (wpas_p2p_add_group_interface(wpa_s, iftype) < 0) {
7778 wpa_printf(MSG_ERROR, "P2P: Failed to allocate a new "
7779 "interface for the group");
7780 return -1;
7781 }
7782
7783 if_addr = wpa_s->pending_interface_addr;
7784 } else
7785 if_addr = wpa_s->own_addr;
7786
7787 wpa_s->p2p_nfc_tag_enabled = enabled;
7788
7789 for (iface = wpa_s->global->ifaces; iface; iface = iface->next) {
7790 struct hostapd_data *hapd;
7791 if (iface->ap_iface == NULL)
7792 continue;
7793 hapd = iface->ap_iface->bss[0];
7794 wpabuf_free(hapd->conf->wps_nfc_dh_pubkey);
7795 hapd->conf->wps_nfc_dh_pubkey =
7796 wpabuf_dup(wpa_s->conf->wps_nfc_dh_pubkey);
7797 wpabuf_free(hapd->conf->wps_nfc_dh_privkey);
7798 hapd->conf->wps_nfc_dh_privkey =
7799 wpabuf_dup(wpa_s->conf->wps_nfc_dh_privkey);
7800 wpabuf_free(hapd->conf->wps_nfc_dev_pw);
7801 hapd->conf->wps_nfc_dev_pw =
7802 wpabuf_dup(wpa_s->conf->wps_nfc_dev_pw);
7803 hapd->conf->wps_nfc_dev_pw_id = wpa_s->conf->wps_nfc_dev_pw_id;
7804
7805 if (hostapd_wps_nfc_token_enable(iface->ap_iface->bss[0]) < 0) {
7806 wpa_dbg(iface, MSG_DEBUG,
7807 "P2P: Failed to enable NFC Tag for GO");
7808 }
7809 }
7810 p2p_set_authorized_oob_dev_pw_id(
7811 wpa_s->global->p2p, wpa_s->conf->wps_nfc_dev_pw_id, go_intent,
7812 if_addr);
7813
7814 return 0;
7815}
7816
7817#endif /* CONFIG_WPS_NFC */
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07007818
7819
7820static void wpas_p2p_optimize_listen_channel(struct wpa_supplicant *wpa_s,
7821 struct wpa_used_freq_data *freqs,
7822 unsigned int num)
7823{
7824 u8 curr_chan, cand, chan;
7825 unsigned int i;
7826
7827 curr_chan = p2p_get_listen_channel(wpa_s->global->p2p);
7828 for (i = 0, cand = 0; i < num; i++) {
7829 ieee80211_freq_to_chan(freqs[i].freq, &chan);
7830 if (curr_chan == chan) {
7831 cand = 0;
7832 break;
7833 }
7834
7835 if (chan == 1 || chan == 6 || chan == 11)
7836 cand = chan;
7837 }
7838
7839 if (cand) {
7840 wpa_dbg(wpa_s, MSG_DEBUG,
7841 "P2P: Update Listen channel to %u baased on operating channel",
7842 cand);
7843 p2p_set_listen_channel(wpa_s->global->p2p, 81, cand, 0);
7844 }
7845}
7846
7847
7848void wpas_p2p_indicate_state_change(struct wpa_supplicant *wpa_s)
7849{
7850 struct wpa_used_freq_data *freqs;
7851 unsigned int num = wpa_s->num_multichan_concurrent;
7852
7853 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
7854 return;
7855
7856 /*
7857 * If possible, optimize the Listen channel to be a channel that is
7858 * already used by one of the other interfaces.
7859 */
7860 if (!wpa_s->conf->p2p_optimize_listen_chan)
7861 return;
7862
7863 if (!wpa_s->current_ssid || wpa_s->wpa_state != WPA_COMPLETED)
7864 return;
7865
7866 freqs = os_calloc(num, sizeof(struct wpa_used_freq_data));
7867 if (!freqs)
7868 return;
7869
7870 num = get_shared_radio_freqs_data(wpa_s, freqs, num);
7871
7872 wpas_p2p_optimize_listen_channel(wpa_s, freqs, num);
7873 os_free(freqs);
7874}
7875
7876
7877void wpas_p2p_deinit_iface(struct wpa_supplicant *wpa_s)
7878{
7879 if (wpa_s == wpa_s->parent)
7880 wpas_p2p_group_remove(wpa_s, "*");
7881 if (wpa_s == wpa_s->global->p2p_init_wpa_s && wpa_s->global->p2p) {
7882 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Disable P2P since removing "
7883 "the management interface is being removed");
7884 wpas_p2p_deinit_global(wpa_s->global);
7885 }
7886}
7887
7888
7889void wpas_p2p_ap_deinit(struct wpa_supplicant *wpa_s)
7890{
7891 if (wpa_s->ap_iface->bss)
7892 wpa_s->ap_iface->bss[0]->p2p_group = NULL;
7893 wpas_p2p_group_deinit(wpa_s);
7894}