blob: 65c1b486f9fe8dbd092954a6b2ff035b223ea1be [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 Shmidt6c0da2b2015-01-05 13:08:17 -0800122static void 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);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800272 p2p_notify_scan_trigger_status(wpa_s->global->p2p, ret);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800273 return;
274 }
275
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800276 p2p_notify_scan_trigger_status(wpa_s->global->p2p, ret);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800277 os_get_reltime(&wpa_s->scan_trigger_time);
278 wpa_s->scan_res_handler = wpas_p2p_scan_res_handler;
279 wpa_s->own_scan_requested = 1;
280 wpa_s->p2p_scan_work = work;
281}
282
283
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800284static int wpas_p2p_search_social_channel(struct wpa_supplicant *wpa_s,
285 int freq)
286{
287 if (wpa_s->global->p2p_24ghz_social_channels &&
288 (freq == 2412 || freq == 2437 || freq == 2462)) {
289 /*
290 * Search all social channels regardless of whether these have
291 * been disabled for P2P operating channel use to avoid missing
292 * peers.
293 */
294 return 1;
295 }
296 return p2p_supported_freq(wpa_s->global->p2p, freq);
297}
298
299
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700300static int wpas_p2p_scan(void *ctx, enum p2p_scan_type type, int freq,
301 unsigned int num_req_dev_types,
Dmitry Shmidt04949592012-07-19 12:16:46 -0700302 const u8 *req_dev_types, const u8 *dev_id, u16 pw_id)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700303{
304 struct wpa_supplicant *wpa_s = ctx;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800305 struct wpa_driver_scan_params *params = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700306 struct wpabuf *wps_ie, *ies;
Dmitry Shmidt661b4f72014-09-29 14:58:27 -0700307 unsigned int num_channels = 0;
308 int social_channels_freq[] = { 2412, 2437, 2462, 60480 };
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800309 size_t ielen;
Dmitry Shmidt661b4f72014-09-29 14:58:27 -0700310 u8 *n, i;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700311
312 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
313 return -1;
314
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800315 if (wpa_s->p2p_scan_work) {
316 wpa_dbg(wpa_s, MSG_INFO, "P2P: Reject scan trigger since one is already pending");
317 return -1;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700318 }
319
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800320 params = os_zalloc(sizeof(*params));
321 if (params == NULL)
322 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700323
324 /* P2P Wildcard SSID */
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800325 params->num_ssids = 1;
326 n = os_malloc(P2P_WILDCARD_SSID_LEN);
327 if (n == NULL)
328 goto fail;
329 os_memcpy(n, P2P_WILDCARD_SSID, P2P_WILDCARD_SSID_LEN);
330 params->ssids[0].ssid = n;
331 params->ssids[0].ssid_len = P2P_WILDCARD_SSID_LEN;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700332
333 wpa_s->wps->dev.p2p = 1;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700334 wps_ie = wps_build_probe_req_ie(pw_id, &wpa_s->wps->dev,
335 wpa_s->wps->uuid, WPS_REQ_ENROLLEE,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700336 num_req_dev_types, req_dev_types);
337 if (wps_ie == NULL)
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800338 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700339
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800340 ielen = p2p_scan_ie_buf_len(wpa_s->global->p2p);
341 ies = wpabuf_alloc(wpabuf_len(wps_ie) + ielen);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700342 if (ies == NULL) {
343 wpabuf_free(wps_ie);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800344 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700345 }
346 wpabuf_put_buf(ies, wps_ie);
347 wpabuf_free(wps_ie);
348
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -0800349 p2p_scan_ie(wpa_s->global->p2p, ies, dev_id);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700350
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800351 params->p2p_probe = 1;
352 n = os_malloc(wpabuf_len(ies));
353 if (n == NULL) {
354 wpabuf_free(ies);
355 goto fail;
356 }
357 os_memcpy(n, wpabuf_head(ies), wpabuf_len(ies));
358 params->extra_ies = n;
359 params->extra_ies_len = wpabuf_len(ies);
360 wpabuf_free(ies);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700361
362 switch (type) {
363 case P2P_SCAN_SOCIAL:
Dmitry Shmidt661b4f72014-09-29 14:58:27 -0700364 params->freqs = os_calloc(ARRAY_SIZE(social_channels_freq) + 1,
365 sizeof(int));
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800366 if (params->freqs == NULL)
367 goto fail;
Dmitry Shmidt661b4f72014-09-29 14:58:27 -0700368 for (i = 0; i < ARRAY_SIZE(social_channels_freq); i++) {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800369 if (wpas_p2p_search_social_channel(
370 wpa_s, social_channels_freq[i]))
Dmitry Shmidt661b4f72014-09-29 14:58:27 -0700371 params->freqs[num_channels++] =
372 social_channels_freq[i];
373 }
374 params->freqs[num_channels++] = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700375 break;
376 case P2P_SCAN_FULL:
377 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700378 case P2P_SCAN_SOCIAL_PLUS_ONE:
Dmitry Shmidt661b4f72014-09-29 14:58:27 -0700379 params->freqs = os_calloc(ARRAY_SIZE(social_channels_freq) + 2,
380 sizeof(int));
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800381 if (params->freqs == NULL)
382 goto fail;
Dmitry Shmidt661b4f72014-09-29 14:58:27 -0700383 for (i = 0; i < ARRAY_SIZE(social_channels_freq); i++) {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800384 if (wpas_p2p_search_social_channel(
385 wpa_s, social_channels_freq[i]))
Dmitry Shmidt661b4f72014-09-29 14:58:27 -0700386 params->freqs[num_channels++] =
387 social_channels_freq[i];
388 }
389 if (p2p_supported_freq(wpa_s->global->p2p, freq))
390 params->freqs[num_channels++] = freq;
391 params->freqs[num_channels++] = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700392 break;
393 }
394
Dmitry Shmidtbd14a572014-02-18 10:33:49 -0800395 radio_remove_works(wpa_s, "p2p-scan", 0);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800396 if (radio_add_work(wpa_s, 0, "p2p-scan", 0, wpas_p2p_trigger_scan_cb,
397 params) < 0)
398 goto fail;
399 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700400
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800401fail:
402 wpa_scan_free_params(params);
403 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700404}
405
406
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700407static enum wpa_driver_if_type wpas_p2p_if_type(int p2p_group_interface)
408{
409 switch (p2p_group_interface) {
410 case P2P_GROUP_INTERFACE_PENDING:
411 return WPA_IF_P2P_GROUP;
412 case P2P_GROUP_INTERFACE_GO:
413 return WPA_IF_P2P_GO;
414 case P2P_GROUP_INTERFACE_CLIENT:
415 return WPA_IF_P2P_CLIENT;
416 }
417
418 return WPA_IF_P2P_GROUP;
419}
420
421
422static struct wpa_supplicant * wpas_get_p2p_group(struct wpa_supplicant *wpa_s,
423 const u8 *ssid,
424 size_t ssid_len, int *go)
425{
426 struct wpa_ssid *s;
427
428 for (wpa_s = wpa_s->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
429 for (s = wpa_s->conf->ssid; s; s = s->next) {
430 if (s->disabled != 0 || !s->p2p_group ||
431 s->ssid_len != ssid_len ||
432 os_memcmp(ssid, s->ssid, ssid_len) != 0)
433 continue;
434 if (s->mode == WPAS_MODE_P2P_GO &&
435 s != wpa_s->current_ssid)
436 continue;
437 if (go)
438 *go = s->mode == WPAS_MODE_P2P_GO;
439 return wpa_s;
440 }
441 }
442
443 return NULL;
444}
445
446
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800447static void run_wpas_p2p_disconnect(void *eloop_ctx, void *timeout_ctx)
448{
449 struct wpa_supplicant *wpa_s = eloop_ctx;
450 wpa_printf(MSG_DEBUG,
451 "P2P: Complete previously requested removal of %s",
452 wpa_s->ifname);
453 wpas_p2p_disconnect(wpa_s);
454}
455
456
457static int wpas_p2p_disconnect_safely(struct wpa_supplicant *wpa_s,
458 struct wpa_supplicant *calling_wpa_s)
459{
460 if (calling_wpa_s == wpa_s && wpa_s &&
461 wpa_s->p2p_group_interface != NOT_P2P_GROUP_INTERFACE) {
462 /*
463 * The calling wpa_s instance is going to be removed. Do that
464 * from an eloop callback to keep the instance available until
465 * the caller has returned. This my be needed, e.g., to provide
466 * control interface responses on the per-interface socket.
467 */
468 if (eloop_register_timeout(0, 0, run_wpas_p2p_disconnect,
469 wpa_s, NULL) < 0)
470 return -1;
471 return 0;
472 }
473
474 return wpas_p2p_disconnect(wpa_s);
475}
476
477
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700478static int wpas_p2p_group_delete(struct wpa_supplicant *wpa_s,
479 enum p2p_group_removal_reason removal_reason)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700480{
481 struct wpa_ssid *ssid;
482 char *gtype;
483 const char *reason;
484
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700485 ssid = wpa_s->current_ssid;
486 if (ssid == NULL) {
487 /*
488 * The current SSID was not known, but there may still be a
Dmitry Shmidtaa532512012-09-24 10:35:31 -0700489 * pending P2P group interface waiting for provisioning or a
490 * P2P group that is trying to reconnect.
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700491 */
492 ssid = wpa_s->conf->ssid;
493 while (ssid) {
Jouni Malinen9d712832012-10-05 11:01:57 -0700494 if (ssid->p2p_group && ssid->disabled != 2)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700495 break;
496 ssid = ssid->next;
497 }
Jouni Malinen5c44edb2012-08-31 21:35:32 +0300498 if (ssid == NULL &&
499 wpa_s->p2p_group_interface == NOT_P2P_GROUP_INTERFACE)
500 {
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700501 wpa_printf(MSG_ERROR, "P2P: P2P group interface "
502 "not found");
503 return -1;
504 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700505 }
506 if (wpa_s->p2p_group_interface == P2P_GROUP_INTERFACE_GO)
507 gtype = "GO";
508 else if (wpa_s->p2p_group_interface == P2P_GROUP_INTERFACE_CLIENT ||
509 (ssid && ssid->mode == WPAS_MODE_INFRA)) {
510 wpa_s->reassociate = 0;
511 wpa_s->disconnected = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700512 gtype = "client";
513 } else
514 gtype = "GO";
Dmitry Shmidt661b4f72014-09-29 14:58:27 -0700515
516 if (removal_reason != P2P_GROUP_REMOVAL_SILENT && ssid)
517 wpas_notify_p2p_group_removed(wpa_s, ssid, gtype);
518
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800519 if (os_strcmp(gtype, "client") == 0) {
Dmitry Shmidt661b4f72014-09-29 14:58:27 -0700520 wpa_supplicant_deauthenticate(wpa_s, WLAN_REASON_DEAUTH_LEAVING);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800521 if (eloop_is_timeout_registered(wpas_p2p_psk_failure_removal,
522 wpa_s, NULL)) {
523 wpa_printf(MSG_DEBUG,
524 "P2P: PSK failure removal was scheduled, so use PSK failure as reason for group removal");
525 removal_reason = P2P_GROUP_REMOVAL_PSK_FAILURE;
526 eloop_cancel_timeout(wpas_p2p_psk_failure_removal,
527 wpa_s, NULL);
528 }
529 }
Dmitry Shmidt661b4f72014-09-29 14:58:27 -0700530
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700531 if (wpa_s->cross_connect_in_use) {
532 wpa_s->cross_connect_in_use = 0;
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -0700533 wpa_msg_global(wpa_s->parent, MSG_INFO,
534 P2P_EVENT_CROSS_CONNECT_DISABLE "%s %s",
535 wpa_s->ifname, wpa_s->cross_connect_uplink);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700536 }
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700537 switch (removal_reason) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700538 case P2P_GROUP_REMOVAL_REQUESTED:
539 reason = " reason=REQUESTED";
540 break;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700541 case P2P_GROUP_REMOVAL_FORMATION_FAILED:
542 reason = " reason=FORMATION_FAILED";
543 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700544 case P2P_GROUP_REMOVAL_IDLE_TIMEOUT:
545 reason = " reason=IDLE";
546 break;
547 case P2P_GROUP_REMOVAL_UNAVAILABLE:
548 reason = " reason=UNAVAILABLE";
549 break;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700550 case P2P_GROUP_REMOVAL_GO_ENDING_SESSION:
551 reason = " reason=GO_ENDING_SESSION";
552 break;
Dmitry Shmidt391c59f2013-09-03 12:16:28 -0700553 case P2P_GROUP_REMOVAL_PSK_FAILURE:
554 reason = " reason=PSK_FAILURE";
555 break;
Dmitry Shmidt04f534e2013-12-09 15:50:16 -0800556 case P2P_GROUP_REMOVAL_FREQ_CONFLICT:
557 reason = " reason=FREQ_CONFLICT";
558 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700559 default:
560 reason = "";
561 break;
562 }
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700563 if (removal_reason != P2P_GROUP_REMOVAL_SILENT) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -0700564 wpa_msg_global(wpa_s->parent, MSG_INFO,
565 P2P_EVENT_GROUP_REMOVED "%s %s%s",
566 wpa_s->ifname, gtype, reason);
Dmitry Shmidt04949592012-07-19 12:16:46 -0700567 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700568
Dmitry Shmidt04f534e2013-12-09 15:50:16 -0800569 if (eloop_cancel_timeout(wpas_p2p_group_freq_conflict, wpa_s, NULL) > 0)
570 wpa_printf(MSG_DEBUG, "P2P: Cancelled P2P group freq_conflict timeout");
Dmitry Shmidt04949592012-07-19 12:16:46 -0700571 if (eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL) > 0)
572 wpa_printf(MSG_DEBUG, "P2P: Cancelled P2P group idle timeout");
Dmitry Shmidtf8623282013-02-20 14:34:59 -0800573 if (eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
Dmitry Shmidt2f023192013-03-12 12:44:17 -0700574 wpa_s->parent, NULL) > 0) {
Dmitry Shmidtf8623282013-02-20 14:34:59 -0800575 wpa_printf(MSG_DEBUG, "P2P: Cancelled P2P group formation "
576 "timeout");
Dmitry Shmidt2f023192013-03-12 12:44:17 -0700577 wpa_s->p2p_in_provisioning = 0;
578 }
Dmitry Shmidt04949592012-07-19 12:16:46 -0700579
Dmitry Shmidt15907092014-03-25 10:42:57 -0700580 wpa_s->p2p_in_invitation = 0;
581
Dmitry Shmidt96571392013-10-14 12:54:46 -0700582 /*
583 * Make sure wait for the first client does not remain active after the
584 * group has been removed.
585 */
586 wpa_s->global->p2p_go_wait_client.sec = 0;
587
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700588 if (wpa_s->p2p_group_interface != NOT_P2P_GROUP_INTERFACE) {
589 struct wpa_global *global;
590 char *ifname;
591 enum wpa_driver_if_type type;
592 wpa_printf(MSG_DEBUG, "P2P: Remove group interface %s",
593 wpa_s->ifname);
594 global = wpa_s->global;
595 ifname = os_strdup(wpa_s->ifname);
596 type = wpas_p2p_if_type(wpa_s->p2p_group_interface);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800597 eloop_cancel_timeout(run_wpas_p2p_disconnect, wpa_s, NULL);
Dmitry Shmidte15c7b52011-08-03 15:04:35 -0700598 wpa_supplicant_remove_iface(wpa_s->global, wpa_s, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700599 wpa_s = global->ifaces;
600 if (wpa_s && ifname)
601 wpa_drv_if_remove(wpa_s, type, ifname);
602 os_free(ifname);
Jouni Malinen2b89da82012-08-31 22:04:41 +0300603 return 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700604 }
605
Dmitry Shmidt96571392013-10-14 12:54:46 -0700606 if (!wpa_s->p2p_go_group_formation_completed) {
607 wpa_s->global->p2p_group_formation = NULL;
608 wpa_s->p2p_in_provisioning = 0;
609 }
610
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800611 wpa_s->show_group_started = 0;
612 os_free(wpa_s->go_params);
613 wpa_s->go_params = NULL;
614
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800615 os_free(wpa_s->p2p_group_common_freqs);
616 wpa_s->p2p_group_common_freqs = NULL;
617 wpa_s->p2p_group_common_freqs_num = 0;
618
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800619 wpa_s->waiting_presence_resp = 0;
620
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700621 wpa_printf(MSG_DEBUG, "P2P: Remove temporary group network");
622 if (ssid && (ssid->p2p_group ||
623 ssid->mode == WPAS_MODE_P2P_GROUP_FORMATION ||
624 (ssid->key_mgmt & WPA_KEY_MGMT_WPS))) {
625 int id = ssid->id;
Jouni Malinen75ecf522011-06-27 15:19:46 -0700626 if (ssid == wpa_s->current_ssid) {
627 wpa_sm_set_config(wpa_s->wpa, NULL);
628 eapol_sm_notify_config(wpa_s->eapol, NULL, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700629 wpa_s->current_ssid = NULL;
Jouni Malinen75ecf522011-06-27 15:19:46 -0700630 }
631 /*
632 * Networks objects created during any P2P activities are not
633 * exposed out as they might/will confuse certain non-P2P aware
634 * applications since these network objects won't behave like
635 * regular ones.
636 *
637 * Likewise, we don't send out network removed signals for such
638 * network objects.
639 */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700640 wpa_config_remove_network(wpa_s->conf, id);
641 wpa_supplicant_clear_status(wpa_s);
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -0800642 wpa_supplicant_cancel_sched_scan(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700643 } else {
644 wpa_printf(MSG_DEBUG, "P2P: Temporary group network not "
645 "found");
646 }
Dmitry Shmidt04949592012-07-19 12:16:46 -0700647 if (wpa_s->ap_iface)
648 wpa_supplicant_ap_deinit(wpa_s);
649 else
650 wpa_drv_deinit_p2p_cli(wpa_s);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700651
652 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700653}
654
655
656static int wpas_p2p_persistent_group(struct wpa_supplicant *wpa_s,
657 u8 *go_dev_addr,
658 const u8 *ssid, size_t ssid_len)
659{
660 struct wpa_bss *bss;
661 const u8 *bssid;
662 struct wpabuf *p2p;
663 u8 group_capab;
664 const u8 *addr;
665
666 if (wpa_s->go_params)
667 bssid = wpa_s->go_params->peer_interface_addr;
668 else
669 bssid = wpa_s->bssid;
670
671 bss = wpa_bss_get(wpa_s, bssid, ssid, ssid_len);
Dmitry Shmidtcf32e602014-01-28 10:57:39 -0800672 if (bss == NULL && wpa_s->go_params &&
673 !is_zero_ether_addr(wpa_s->go_params->peer_device_addr))
674 bss = wpa_bss_get_p2p_dev_addr(
675 wpa_s, wpa_s->go_params->peer_device_addr);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700676 if (bss == NULL) {
677 u8 iface_addr[ETH_ALEN];
678 if (p2p_get_interface_addr(wpa_s->global->p2p, bssid,
679 iface_addr) == 0)
680 bss = wpa_bss_get(wpa_s, iface_addr, ssid, ssid_len);
681 }
682 if (bss == NULL) {
683 wpa_printf(MSG_DEBUG, "P2P: Could not figure out whether "
684 "group is persistent - BSS " MACSTR " not found",
685 MAC2STR(bssid));
686 return 0;
687 }
688
689 p2p = wpa_bss_get_vendor_ie_multi(bss, P2P_IE_VENDOR_TYPE);
Dmitry Shmidt96571392013-10-14 12:54:46 -0700690 if (p2p == NULL)
691 p2p = wpa_bss_get_vendor_ie_multi_beacon(bss,
692 P2P_IE_VENDOR_TYPE);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700693 if (p2p == NULL) {
694 wpa_printf(MSG_DEBUG, "P2P: Could not figure out whether "
695 "group is persistent - BSS " MACSTR
696 " did not include P2P IE", MAC2STR(bssid));
697 wpa_hexdump(MSG_DEBUG, "P2P: Probe Response IEs",
698 (u8 *) (bss + 1), bss->ie_len);
699 wpa_hexdump(MSG_DEBUG, "P2P: Beacon IEs",
700 ((u8 *) bss + 1) + bss->ie_len,
701 bss->beacon_ie_len);
702 return 0;
703 }
704
705 group_capab = p2p_get_group_capab(p2p);
706 addr = p2p_get_go_dev_addr(p2p);
707 wpa_printf(MSG_DEBUG, "P2P: Checking whether group is persistent: "
708 "group_capab=0x%x", group_capab);
709 if (addr) {
710 os_memcpy(go_dev_addr, addr, ETH_ALEN);
711 wpa_printf(MSG_DEBUG, "P2P: GO Device Address " MACSTR,
712 MAC2STR(addr));
713 } else
714 os_memset(go_dev_addr, 0, ETH_ALEN);
715 wpabuf_free(p2p);
716
717 wpa_printf(MSG_DEBUG, "P2P: BSS " MACSTR " group_capab=0x%x "
718 "go_dev_addr=" MACSTR,
719 MAC2STR(bssid), group_capab, MAC2STR(go_dev_addr));
720
721 return group_capab & P2P_GROUP_CAPAB_PERSISTENT_GROUP;
722}
723
724
Jouni Malinen75ecf522011-06-27 15:19:46 -0700725static int wpas_p2p_store_persistent_group(struct wpa_supplicant *wpa_s,
726 struct wpa_ssid *ssid,
727 const u8 *go_dev_addr)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700728{
729 struct wpa_ssid *s;
730 int changed = 0;
731
732 wpa_printf(MSG_DEBUG, "P2P: Storing credentials for a persistent "
733 "group (GO Dev Addr " MACSTR ")", MAC2STR(go_dev_addr));
734 for (s = wpa_s->conf->ssid; s; s = s->next) {
735 if (s->disabled == 2 &&
736 os_memcmp(go_dev_addr, s->bssid, ETH_ALEN) == 0 &&
737 s->ssid_len == ssid->ssid_len &&
738 os_memcmp(ssid->ssid, s->ssid, ssid->ssid_len) == 0)
739 break;
740 }
741
742 if (s) {
743 wpa_printf(MSG_DEBUG, "P2P: Update existing persistent group "
744 "entry");
745 if (ssid->passphrase && !s->passphrase)
746 changed = 1;
747 else if (ssid->passphrase && s->passphrase &&
748 os_strcmp(ssid->passphrase, s->passphrase) != 0)
749 changed = 1;
750 } else {
751 wpa_printf(MSG_DEBUG, "P2P: Create a new persistent group "
752 "entry");
753 changed = 1;
754 s = wpa_config_add_network(wpa_s->conf);
755 if (s == NULL)
Jouni Malinen75ecf522011-06-27 15:19:46 -0700756 return -1;
757
758 /*
759 * Instead of network_added we emit persistent_group_added
760 * notification. Also to keep the defense checks in
761 * persistent_group obj registration method, we set the
762 * relevant flags in s to designate it as a persistent group.
763 */
764 s->p2p_group = 1;
765 s->p2p_persistent_group = 1;
766 wpas_notify_persistent_group_added(wpa_s, s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700767 wpa_config_set_network_defaults(s);
768 }
769
770 s->p2p_group = 1;
771 s->p2p_persistent_group = 1;
772 s->disabled = 2;
773 s->bssid_set = 1;
774 os_memcpy(s->bssid, go_dev_addr, ETH_ALEN);
775 s->mode = ssid->mode;
776 s->auth_alg = WPA_AUTH_ALG_OPEN;
777 s->key_mgmt = WPA_KEY_MGMT_PSK;
778 s->proto = WPA_PROTO_RSN;
779 s->pairwise_cipher = WPA_CIPHER_CCMP;
780 s->export_keys = 1;
781 if (ssid->passphrase) {
782 os_free(s->passphrase);
783 s->passphrase = os_strdup(ssid->passphrase);
784 }
785 if (ssid->psk_set) {
786 s->psk_set = 1;
787 os_memcpy(s->psk, ssid->psk, 32);
788 }
789 if (s->passphrase && !s->psk_set)
790 wpa_config_update_psk(s);
791 if (s->ssid == NULL || s->ssid_len < ssid->ssid_len) {
792 os_free(s->ssid);
793 s->ssid = os_malloc(ssid->ssid_len);
794 }
795 if (s->ssid) {
796 s->ssid_len = ssid->ssid_len;
797 os_memcpy(s->ssid, ssid->ssid, s->ssid_len);
798 }
Dmitry Shmidt391c59f2013-09-03 12:16:28 -0700799 if (ssid->mode == WPAS_MODE_P2P_GO && wpa_s->global->add_psk) {
800 dl_list_add(&s->psk_list, &wpa_s->global->add_psk->list);
801 wpa_s->global->add_psk = NULL;
802 changed = 1;
803 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700804
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700805 if (changed && wpa_s->conf->update_config &&
806 wpa_config_write(wpa_s->confname, wpa_s->conf)) {
807 wpa_printf(MSG_DEBUG, "P2P: Failed to update configuration");
808 }
Jouni Malinen75ecf522011-06-27 15:19:46 -0700809
810 return s->id;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700811}
812
813
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800814static void wpas_p2p_add_persistent_group_client(struct wpa_supplicant *wpa_s,
815 const u8 *addr)
816{
817 struct wpa_ssid *ssid, *s;
818 u8 *n;
819 size_t i;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700820 int found = 0;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800821
822 ssid = wpa_s->current_ssid;
823 if (ssid == NULL || ssid->mode != WPAS_MODE_P2P_GO ||
824 !ssid->p2p_persistent_group)
825 return;
826
827 for (s = wpa_s->parent->conf->ssid; s; s = s->next) {
828 if (s->disabled != 2 || s->mode != WPAS_MODE_P2P_GO)
829 continue;
830
831 if (s->ssid_len == ssid->ssid_len &&
832 os_memcmp(s->ssid, ssid->ssid, s->ssid_len) == 0)
833 break;
834 }
835
836 if (s == NULL)
837 return;
838
839 for (i = 0; s->p2p_client_list && i < s->num_p2p_clients; i++) {
Dmitry Shmidtff787d52015-01-12 13:01:47 -0800840 if (os_memcmp(s->p2p_client_list + i * 2 * ETH_ALEN, addr,
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700841 ETH_ALEN) != 0)
842 continue;
843
844 if (i == s->num_p2p_clients - 1)
845 return; /* already the most recent entry */
846
847 /* move the entry to mark it most recent */
Dmitry Shmidtff787d52015-01-12 13:01:47 -0800848 os_memmove(s->p2p_client_list + i * 2 * ETH_ALEN,
849 s->p2p_client_list + (i + 1) * 2 * ETH_ALEN,
850 (s->num_p2p_clients - i - 1) * 2 * ETH_ALEN);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700851 os_memcpy(s->p2p_client_list +
Dmitry Shmidtff787d52015-01-12 13:01:47 -0800852 (s->num_p2p_clients - 1) * 2 * ETH_ALEN, addr,
853 ETH_ALEN);
854 os_memset(s->p2p_client_list +
855 (s->num_p2p_clients - 1) * 2 * ETH_ALEN + ETH_ALEN,
856 0xff, ETH_ALEN);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700857 found = 1;
858 break;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800859 }
860
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700861 if (!found && s->num_p2p_clients < P2P_MAX_STORED_CLIENTS) {
862 n = os_realloc_array(s->p2p_client_list,
Dmitry Shmidtff787d52015-01-12 13:01:47 -0800863 s->num_p2p_clients + 1, 2 * ETH_ALEN);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700864 if (n == NULL)
865 return;
Dmitry Shmidtff787d52015-01-12 13:01:47 -0800866 os_memcpy(n + s->num_p2p_clients * 2 * ETH_ALEN, addr,
867 ETH_ALEN);
868 os_memset(n + s->num_p2p_clients * 2 * ETH_ALEN + ETH_ALEN,
869 0xff, ETH_ALEN);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700870 s->p2p_client_list = n;
871 s->num_p2p_clients++;
Dmitry Shmidt6aa8ae42014-07-07 09:31:33 -0700872 } else if (!found && s->p2p_client_list) {
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700873 /* Not enough room for an additional entry - drop the oldest
874 * entry */
875 os_memmove(s->p2p_client_list,
Dmitry Shmidtff787d52015-01-12 13:01:47 -0800876 s->p2p_client_list + 2 * ETH_ALEN,
877 (s->num_p2p_clients - 1) * 2 * ETH_ALEN);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700878 os_memcpy(s->p2p_client_list +
Dmitry Shmidtff787d52015-01-12 13:01:47 -0800879 (s->num_p2p_clients - 1) * 2 * ETH_ALEN,
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700880 addr, ETH_ALEN);
Dmitry Shmidtff787d52015-01-12 13:01:47 -0800881 os_memset(s->p2p_client_list +
882 (s->num_p2p_clients - 1) * 2 * ETH_ALEN + ETH_ALEN,
883 0xff, ETH_ALEN);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700884 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800885
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800886 if (wpa_s->parent->conf->update_config &&
887 wpa_config_write(wpa_s->parent->confname, wpa_s->parent->conf))
888 wpa_printf(MSG_DEBUG, "P2P: Failed to update configuration");
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800889}
890
891
Dmitry Shmidt7f0b69e2014-07-28 10:35:20 -0700892static void wpas_p2p_group_started(struct wpa_supplicant *wpa_s,
893 int go, struct wpa_ssid *ssid, int freq,
894 const u8 *psk, const char *passphrase,
895 const u8 *go_dev_addr, int persistent,
896 const char *extra)
897{
898 const char *ssid_txt;
899 char psk_txt[65];
900
901 if (psk)
902 wpa_snprintf_hex(psk_txt, sizeof(psk_txt), psk, 32);
903 else
904 psk_txt[0] = '\0';
905
906 if (ssid)
907 ssid_txt = wpa_ssid_txt(ssid->ssid, ssid->ssid_len);
908 else
909 ssid_txt = "";
910
911 if (passphrase && passphrase[0] == '\0')
912 passphrase = NULL;
913
914 /*
915 * Include PSK/passphrase only in the control interface message and
916 * leave it out from the debug log entry.
917 */
918 wpa_msg_global_ctrl(wpa_s->parent, MSG_INFO,
919 P2P_EVENT_GROUP_STARTED
920 "%s %s ssid=\"%s\" freq=%d%s%s%s%s%s go_dev_addr="
921 MACSTR "%s%s",
922 wpa_s->ifname, go ? "GO" : "client", ssid_txt, freq,
923 psk ? " psk=" : "", psk_txt,
924 passphrase ? " passphrase=\"" : "",
925 passphrase ? passphrase : "",
926 passphrase ? "\"" : "",
927 MAC2STR(go_dev_addr),
928 persistent ? " [PERSISTENT]" : "", extra);
929 wpa_printf(MSG_INFO, P2P_EVENT_GROUP_STARTED
930 "%s %s ssid=\"%s\" freq=%d go_dev_addr=" MACSTR "%s%s",
931 wpa_s->ifname, go ? "GO" : "client", ssid_txt, freq,
932 MAC2STR(go_dev_addr), persistent ? " [PERSISTENT]" : "",
933 extra);
934}
935
936
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700937static void wpas_group_formation_completed(struct wpa_supplicant *wpa_s,
938 int success)
939{
940 struct wpa_ssid *ssid;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700941 int client;
942 int persistent;
943 u8 go_dev_addr[ETH_ALEN];
Jouni Malinen75ecf522011-06-27 15:19:46 -0700944 int network_id = -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700945
946 /*
947 * This callback is likely called for the main interface. Update wpa_s
948 * to use the group interface if a new interface was created for the
949 * group.
950 */
951 if (wpa_s->global->p2p_group_formation)
952 wpa_s = wpa_s->global->p2p_group_formation;
Dmitry Shmidt56052862013-10-04 10:23:25 -0700953 if (wpa_s->p2p_go_group_formation_completed) {
954 wpa_s->global->p2p_group_formation = NULL;
955 wpa_s->p2p_in_provisioning = 0;
956 }
Dmitry Shmidt21de2142014-04-08 10:50:52 -0700957 wpa_s->p2p_in_invitation = 0;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800958 wpa_s->group_formation_reported = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700959
960 if (!success) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -0700961 wpa_msg_global(wpa_s->parent, MSG_INFO,
962 P2P_EVENT_GROUP_FORMATION_FAILURE);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700963 wpas_p2p_group_delete(wpa_s,
964 P2P_GROUP_REMOVAL_FORMATION_FAILED);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700965 return;
966 }
967
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -0700968 wpa_msg_global(wpa_s->parent, MSG_INFO,
969 P2P_EVENT_GROUP_FORMATION_SUCCESS);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700970
971 ssid = wpa_s->current_ssid;
972 if (ssid && ssid->mode == WPAS_MODE_P2P_GROUP_FORMATION) {
973 ssid->mode = WPAS_MODE_P2P_GO;
974 p2p_group_notif_formation_done(wpa_s->p2p_group);
975 wpa_supplicant_ap_mac_addr_filter(wpa_s, NULL);
976 }
977
978 persistent = 0;
979 if (ssid) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700980 client = ssid->mode == WPAS_MODE_INFRA;
981 if (ssid->mode == WPAS_MODE_P2P_GO) {
982 persistent = ssid->p2p_persistent_group;
Dmitry Shmidt497c1d52011-07-21 15:19:46 -0700983 os_memcpy(go_dev_addr, wpa_s->global->p2p_dev_addr,
984 ETH_ALEN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700985 } else
986 persistent = wpas_p2p_persistent_group(wpa_s,
987 go_dev_addr,
988 ssid->ssid,
989 ssid->ssid_len);
990 } else {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700991 client = wpa_s->p2p_group_interface ==
992 P2P_GROUP_INTERFACE_CLIENT;
993 os_memset(go_dev_addr, 0, ETH_ALEN);
994 }
995
996 wpa_s->show_group_started = 0;
997 if (client) {
998 /*
999 * Indicate event only after successfully completed 4-way
1000 * handshake, i.e., when the interface is ready for data
1001 * packets.
1002 */
1003 wpa_s->show_group_started = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001004 } else {
Dmitry Shmidt7f0b69e2014-07-28 10:35:20 -07001005 wpas_p2p_group_started(wpa_s, 1, ssid,
1006 ssid ? ssid->frequency : 0,
1007 ssid && ssid->passphrase == NULL &&
1008 ssid->psk_set ? ssid->psk : NULL,
1009 ssid ? ssid->passphrase : NULL,
1010 go_dev_addr, persistent, "");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001011 wpas_p2p_cross_connect_setup(wpa_s);
1012 wpas_p2p_set_group_idle_timeout(wpa_s);
1013 }
1014
1015 if (persistent)
Jouni Malinen75ecf522011-06-27 15:19:46 -07001016 network_id = wpas_p2p_store_persistent_group(wpa_s->parent,
1017 ssid, go_dev_addr);
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07001018 else {
1019 os_free(wpa_s->global->add_psk);
1020 wpa_s->global->add_psk = NULL;
1021 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001022 if (network_id < 0 && ssid)
Jouni Malinen75ecf522011-06-27 15:19:46 -07001023 network_id = ssid->id;
Dmitry Shmidt92c368d2013-08-29 12:37:21 -07001024 if (!client) {
Jouni Malinen75ecf522011-06-27 15:19:46 -07001025 wpas_notify_p2p_group_started(wpa_s, ssid, network_id, 0);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001026 os_get_reltime(&wpa_s->global->p2p_go_wait_client);
Dmitry Shmidt92c368d2013-08-29 12:37:21 -07001027 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001028}
1029
1030
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001031struct send_action_work {
1032 unsigned int freq;
1033 u8 dst[ETH_ALEN];
1034 u8 src[ETH_ALEN];
1035 u8 bssid[ETH_ALEN];
1036 size_t len;
1037 unsigned int wait_time;
1038 u8 buf[0];
1039};
1040
1041
1042static void wpas_p2p_send_action_work_timeout(void *eloop_ctx,
1043 void *timeout_ctx)
1044{
1045 struct wpa_supplicant *wpa_s = eloop_ctx;
1046
1047 if (!wpa_s->p2p_send_action_work)
1048 return;
1049
1050 wpa_printf(MSG_DEBUG, "P2P: Send Action frame radio work timed out");
1051 os_free(wpa_s->p2p_send_action_work->ctx);
1052 radio_work_done(wpa_s->p2p_send_action_work);
1053 wpa_s->p2p_send_action_work = NULL;
1054}
1055
1056
Dmitry Shmidt03658832014-08-13 11:03:49 -07001057static void wpas_p2p_action_tx_clear(struct wpa_supplicant *wpa_s)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001058{
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001059 if (wpa_s->p2p_send_action_work) {
1060 struct send_action_work *awork;
1061 awork = wpa_s->p2p_send_action_work->ctx;
1062 if (awork->wait_time == 0) {
1063 os_free(awork);
1064 radio_work_done(wpa_s->p2p_send_action_work);
1065 wpa_s->p2p_send_action_work = NULL;
1066 } else {
1067 /*
1068 * In theory, this should not be needed, but number of
1069 * places in the P2P code is still using non-zero wait
1070 * time for the last Action frame in the sequence and
1071 * some of these do not call send_action_done().
1072 */
1073 eloop_cancel_timeout(wpas_p2p_send_action_work_timeout,
1074 wpa_s, NULL);
1075 eloop_register_timeout(
1076 0, awork->wait_time * 1000,
1077 wpas_p2p_send_action_work_timeout,
1078 wpa_s, NULL);
1079 }
1080 }
Dmitry Shmidt03658832014-08-13 11:03:49 -07001081}
1082
1083
1084static void wpas_p2p_send_action_tx_status(struct wpa_supplicant *wpa_s,
1085 unsigned int freq,
1086 const u8 *dst, const u8 *src,
1087 const u8 *bssid,
1088 const u8 *data, size_t data_len,
1089 enum offchannel_send_action_result
1090 result)
1091{
1092 enum p2p_send_action_result res = P2P_SEND_ACTION_SUCCESS;
1093
1094 wpas_p2p_action_tx_clear(wpa_s);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001095
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001096 if (wpa_s->global->p2p == NULL || wpa_s->global->p2p_disabled)
1097 return;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001098
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001099 switch (result) {
1100 case OFFCHANNEL_SEND_ACTION_SUCCESS:
1101 res = P2P_SEND_ACTION_SUCCESS;
1102 break;
1103 case OFFCHANNEL_SEND_ACTION_NO_ACK:
1104 res = P2P_SEND_ACTION_NO_ACK;
1105 break;
1106 case OFFCHANNEL_SEND_ACTION_FAILED:
1107 res = P2P_SEND_ACTION_FAILED;
1108 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001109 }
1110
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001111 p2p_send_action_cb(wpa_s->global->p2p, freq, dst, src, bssid, res);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001112
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08001113 if (result != OFFCHANNEL_SEND_ACTION_SUCCESS &&
1114 wpa_s->pending_pd_before_join &&
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001115 (os_memcmp(dst, wpa_s->pending_join_dev_addr, ETH_ALEN) == 0 ||
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001116 os_memcmp(dst, wpa_s->pending_join_iface_addr, ETH_ALEN) == 0) &&
1117 wpa_s->p2p_fallback_to_go_neg) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001118 wpa_s->pending_pd_before_join = 0;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001119 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: No ACK for PD Req "
1120 "during p2p_connect-auto");
1121 wpas_p2p_fallback_to_go_neg(wpa_s, 0);
1122 return;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001123 }
1124}
1125
1126
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001127static void wpas_send_action_cb(struct wpa_radio_work *work, int deinit)
1128{
1129 struct wpa_supplicant *wpa_s = work->wpa_s;
1130 struct send_action_work *awork = work->ctx;
1131
1132 if (deinit) {
Dmitry Shmidtbd14a572014-02-18 10:33:49 -08001133 if (work->started) {
1134 eloop_cancel_timeout(wpas_p2p_send_action_work_timeout,
1135 wpa_s, NULL);
1136 wpa_s->p2p_send_action_work = NULL;
1137 offchannel_send_action_done(wpa_s);
1138 }
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001139 os_free(awork);
1140 return;
1141 }
1142
1143 if (offchannel_send_action(wpa_s, awork->freq, awork->dst, awork->src,
1144 awork->bssid, awork->buf, awork->len,
1145 awork->wait_time,
1146 wpas_p2p_send_action_tx_status, 1) < 0) {
1147 os_free(awork);
1148 radio_work_done(work);
1149 return;
1150 }
1151 wpa_s->p2p_send_action_work = work;
1152}
1153
1154
1155static int wpas_send_action_work(struct wpa_supplicant *wpa_s,
1156 unsigned int freq, const u8 *dst,
1157 const u8 *src, const u8 *bssid, const u8 *buf,
1158 size_t len, unsigned int wait_time)
1159{
1160 struct send_action_work *awork;
1161
1162 if (wpa_s->p2p_send_action_work) {
1163 wpa_printf(MSG_DEBUG, "P2P: Cannot schedule new p2p-send-action work since one is already pending");
1164 return -1;
1165 }
1166
1167 awork = os_zalloc(sizeof(*awork) + len);
1168 if (awork == NULL)
1169 return -1;
1170
1171 awork->freq = freq;
1172 os_memcpy(awork->dst, dst, ETH_ALEN);
1173 os_memcpy(awork->src, src, ETH_ALEN);
1174 os_memcpy(awork->bssid, bssid, ETH_ALEN);
1175 awork->len = len;
1176 awork->wait_time = wait_time;
1177 os_memcpy(awork->buf, buf, len);
1178
1179 if (radio_add_work(wpa_s, freq, "p2p-send-action", 0,
1180 wpas_send_action_cb, awork) < 0) {
1181 os_free(awork);
1182 return -1;
1183 }
1184
1185 return 0;
1186}
1187
1188
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001189static int wpas_send_action(void *ctx, unsigned int freq, const u8 *dst,
1190 const u8 *src, const u8 *bssid, const u8 *buf,
1191 size_t len, unsigned int wait_time)
1192{
1193 struct wpa_supplicant *wpa_s = ctx;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001194 int listen_freq = -1, send_freq = -1;
1195
1196 if (wpa_s->p2p_listen_work)
1197 listen_freq = wpa_s->p2p_listen_work->freq;
1198 if (wpa_s->p2p_send_action_work)
1199 send_freq = wpa_s->p2p_send_action_work->freq;
1200 if (listen_freq != (int) freq && send_freq != (int) freq) {
1201 wpa_printf(MSG_DEBUG, "P2P: Schedule new radio work for Action frame TX (listen_freq=%d send_freq=%d)",
1202 listen_freq, send_freq);
1203 return wpas_send_action_work(wpa_s, freq, dst, src, bssid, buf,
1204 len, wait_time);
1205 }
1206
1207 wpa_printf(MSG_DEBUG, "P2P: Use ongoing radio work for Action frame TX");
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001208 return offchannel_send_action(wpa_s, freq, dst, src, bssid, buf, len,
1209 wait_time,
1210 wpas_p2p_send_action_tx_status, 1);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001211}
1212
1213
1214static void wpas_send_action_done(void *ctx)
1215{
1216 struct wpa_supplicant *wpa_s = ctx;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001217
1218 if (wpa_s->p2p_send_action_work) {
1219 eloop_cancel_timeout(wpas_p2p_send_action_work_timeout,
1220 wpa_s, NULL);
1221 os_free(wpa_s->p2p_send_action_work->ctx);
1222 radio_work_done(wpa_s->p2p_send_action_work);
1223 wpa_s->p2p_send_action_work = NULL;
1224 }
1225
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001226 offchannel_send_action_done(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001227}
1228
1229
1230static int wpas_copy_go_neg_results(struct wpa_supplicant *wpa_s,
1231 struct p2p_go_neg_results *params)
1232{
1233 if (wpa_s->go_params == NULL) {
1234 wpa_s->go_params = os_malloc(sizeof(*params));
1235 if (wpa_s->go_params == NULL)
1236 return -1;
1237 }
1238 os_memcpy(wpa_s->go_params, params, sizeof(*params));
1239 return 0;
1240}
1241
1242
1243static void wpas_start_wps_enrollee(struct wpa_supplicant *wpa_s,
1244 struct p2p_go_neg_results *res)
1245{
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001246 wpa_s->group_formation_reported = 0;
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001247 wpa_printf(MSG_DEBUG, "P2P: Start WPS Enrollee for peer " MACSTR
1248 " dev_addr " MACSTR " wps_method %d",
1249 MAC2STR(res->peer_interface_addr),
1250 MAC2STR(res->peer_device_addr), res->wps_method);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001251 wpa_hexdump_ascii(MSG_DEBUG, "P2P: Start WPS Enrollee for SSID",
1252 res->ssid, res->ssid_len);
1253 wpa_supplicant_ap_deinit(wpa_s);
1254 wpas_copy_go_neg_results(wpa_s, res);
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001255 if (res->wps_method == WPS_PBC) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001256 wpas_wps_start_pbc(wpa_s, res->peer_interface_addr, 1);
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001257#ifdef CONFIG_WPS_NFC
1258 } else if (res->wps_method == WPS_NFC) {
1259 wpas_wps_start_nfc(wpa_s, res->peer_device_addr,
1260 res->peer_interface_addr,
1261 wpa_s->parent->p2p_oob_dev_pw,
1262 wpa_s->parent->p2p_oob_dev_pw_id, 1,
1263 wpa_s->parent->p2p_oob_dev_pw_id ==
1264 DEV_PW_NFC_CONNECTION_HANDOVER ?
1265 wpa_s->parent->p2p_peer_oob_pubkey_hash :
1266 NULL,
1267 NULL, 0, 0);
1268#endif /* CONFIG_WPS_NFC */
1269 } else {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001270 u16 dev_pw_id = DEV_PW_DEFAULT;
1271 if (wpa_s->p2p_wps_method == WPS_PIN_KEYPAD)
1272 dev_pw_id = DEV_PW_REGISTRAR_SPECIFIED;
1273 wpas_wps_start_pin(wpa_s, res->peer_interface_addr,
1274 wpa_s->p2p_pin, 1, dev_pw_id);
1275 }
1276}
1277
1278
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07001279static void wpas_p2p_add_psk_list(struct wpa_supplicant *wpa_s,
1280 struct wpa_ssid *ssid)
1281{
1282 struct wpa_ssid *persistent;
1283 struct psk_list_entry *psk;
1284 struct hostapd_data *hapd;
1285
1286 if (!wpa_s->ap_iface)
1287 return;
1288
1289 persistent = wpas_p2p_get_persistent(wpa_s->parent, NULL, ssid->ssid,
1290 ssid->ssid_len);
1291 if (persistent == NULL)
1292 return;
1293
1294 hapd = wpa_s->ap_iface->bss[0];
1295
1296 dl_list_for_each(psk, &persistent->psk_list, struct psk_list_entry,
1297 list) {
1298 struct hostapd_wpa_psk *hpsk;
1299
1300 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Add persistent group PSK entry for "
1301 MACSTR " psk=%d",
1302 MAC2STR(psk->addr), psk->p2p);
1303 hpsk = os_zalloc(sizeof(*hpsk));
1304 if (hpsk == NULL)
1305 break;
1306 os_memcpy(hpsk->psk, psk->psk, PMK_LEN);
1307 if (psk->p2p)
1308 os_memcpy(hpsk->p2p_dev_addr, psk->addr, ETH_ALEN);
1309 else
1310 os_memcpy(hpsk->addr, psk->addr, ETH_ALEN);
1311 hpsk->next = hapd->conf->ssid.wpa_psk;
1312 hapd->conf->ssid.wpa_psk = hpsk;
1313 }
1314}
1315
1316
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001317static void p2p_go_dump_common_freqs(struct wpa_supplicant *wpa_s)
1318{
1319 unsigned int i;
1320
1321 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Common group frequencies (len=%u):",
1322 wpa_s->p2p_group_common_freqs_num);
1323
1324 for (i = 0; i < wpa_s->p2p_group_common_freqs_num; i++)
1325 wpa_dbg(wpa_s, MSG_DEBUG, "freq[%u]: %d",
1326 i, wpa_s->p2p_group_common_freqs[i]);
1327}
1328
1329
1330static void p2p_go_save_group_common_freqs(struct wpa_supplicant *wpa_s,
1331 struct p2p_go_neg_results *params)
1332{
1333 unsigned int i, len = int_array_len(wpa_s->go_params->freq_list);
1334
1335 wpa_s->p2p_group_common_freqs_num = 0;
1336 os_free(wpa_s->p2p_group_common_freqs);
1337 wpa_s->p2p_group_common_freqs = os_calloc(len, sizeof(int));
1338 if (!wpa_s->p2p_group_common_freqs)
1339 return;
1340
1341 for (i = 0; i < len; i++) {
1342 if (!wpa_s->go_params->freq_list[i])
1343 break;
1344 wpa_s->p2p_group_common_freqs[i] =
1345 wpa_s->go_params->freq_list[i];
1346 }
1347 wpa_s->p2p_group_common_freqs_num = i;
1348}
1349
1350
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001351static void p2p_go_configured(void *ctx, void *data)
1352{
1353 struct wpa_supplicant *wpa_s = ctx;
1354 struct p2p_go_neg_results *params = data;
1355 struct wpa_ssid *ssid;
Jouni Malinen75ecf522011-06-27 15:19:46 -07001356 int network_id = -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001357
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001358 p2p_go_save_group_common_freqs(wpa_s, params);
1359 p2p_go_dump_common_freqs(wpa_s);
1360
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001361 ssid = wpa_s->current_ssid;
1362 if (ssid && ssid->mode == WPAS_MODE_P2P_GO) {
1363 wpa_printf(MSG_DEBUG, "P2P: Group setup without provisioning");
1364 if (wpa_s->global->p2p_group_formation == wpa_s)
1365 wpa_s->global->p2p_group_formation = NULL;
Dmitry Shmidt7f0b69e2014-07-28 10:35:20 -07001366 wpas_p2p_group_started(wpa_s, 1, ssid, ssid->frequency,
1367 params->passphrase[0] == '\0' ?
1368 params->psk : NULL,
1369 params->passphrase,
1370 wpa_s->global->p2p_dev_addr,
1371 params->persistent_group, "");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001372 wpa_s->group_formation_reported = 1;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001373
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001374 os_get_reltime(&wpa_s->global->p2p_go_wait_client);
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07001375 if (params->persistent_group) {
Jouni Malinen75ecf522011-06-27 15:19:46 -07001376 network_id = wpas_p2p_store_persistent_group(
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001377 wpa_s->parent, ssid,
Dmitry Shmidt497c1d52011-07-21 15:19:46 -07001378 wpa_s->global->p2p_dev_addr);
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07001379 wpas_p2p_add_psk_list(wpa_s, ssid);
1380 }
Jouni Malinen75ecf522011-06-27 15:19:46 -07001381 if (network_id < 0)
1382 network_id = ssid->id;
1383 wpas_notify_p2p_group_started(wpa_s, ssid, network_id, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001384 wpas_p2p_cross_connect_setup(wpa_s);
1385 wpas_p2p_set_group_idle_timeout(wpa_s);
Dmitry Shmidt56052862013-10-04 10:23:25 -07001386
1387 if (wpa_s->p2p_first_connection_timeout) {
1388 wpa_dbg(wpa_s, MSG_DEBUG,
1389 "P2P: Start group formation timeout of %d seconds until first data connection on GO",
1390 wpa_s->p2p_first_connection_timeout);
1391 wpa_s->p2p_go_group_formation_completed = 0;
1392 wpa_s->global->p2p_group_formation = wpa_s;
1393 eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
1394 wpa_s->parent, NULL);
1395 eloop_register_timeout(
1396 wpa_s->p2p_first_connection_timeout, 0,
1397 wpas_p2p_group_formation_timeout,
1398 wpa_s->parent, NULL);
1399 }
1400
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001401 return;
1402 }
1403
1404 wpa_printf(MSG_DEBUG, "P2P: Setting up WPS for GO provisioning");
1405 if (wpa_supplicant_ap_mac_addr_filter(wpa_s,
1406 params->peer_interface_addr)) {
1407 wpa_printf(MSG_DEBUG, "P2P: Failed to setup MAC address "
1408 "filtering");
1409 return;
1410 }
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001411 if (params->wps_method == WPS_PBC) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001412 wpa_supplicant_ap_wps_pbc(wpa_s, params->peer_interface_addr,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001413 params->peer_device_addr);
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001414#ifdef CONFIG_WPS_NFC
1415 } else if (params->wps_method == WPS_NFC) {
1416 if (wpa_s->parent->p2p_oob_dev_pw_id !=
1417 DEV_PW_NFC_CONNECTION_HANDOVER &&
1418 !wpa_s->parent->p2p_oob_dev_pw) {
1419 wpa_printf(MSG_DEBUG, "P2P: No NFC Dev Pw known");
1420 return;
1421 }
1422 wpas_ap_wps_add_nfc_pw(
1423 wpa_s, wpa_s->parent->p2p_oob_dev_pw_id,
1424 wpa_s->parent->p2p_oob_dev_pw,
1425 wpa_s->parent->p2p_peer_oob_pk_hash_known ?
1426 wpa_s->parent->p2p_peer_oob_pubkey_hash : NULL);
1427#endif /* CONFIG_WPS_NFC */
1428 } else if (wpa_s->p2p_pin[0])
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001429 wpa_supplicant_ap_wps_pin(wpa_s, params->peer_interface_addr,
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001430 wpa_s->p2p_pin, NULL, 0, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001431 os_free(wpa_s->go_params);
1432 wpa_s->go_params = NULL;
1433}
1434
1435
1436static void wpas_start_wps_go(struct wpa_supplicant *wpa_s,
1437 struct p2p_go_neg_results *params,
1438 int group_formation)
1439{
1440 struct wpa_ssid *ssid;
1441
Dmitry Shmidtaa532512012-09-24 10:35:31 -07001442 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Starting GO");
1443 if (wpas_copy_go_neg_results(wpa_s, params) < 0) {
1444 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Could not copy GO Negotiation "
1445 "results");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001446 return;
Dmitry Shmidtaa532512012-09-24 10:35:31 -07001447 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001448
1449 ssid = wpa_config_add_network(wpa_s->conf);
Dmitry Shmidtaa532512012-09-24 10:35:31 -07001450 if (ssid == NULL) {
1451 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Could not add network for GO");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001452 return;
Dmitry Shmidtaa532512012-09-24 10:35:31 -07001453 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001454
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001455 wpa_s->show_group_started = 0;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001456 wpa_s->p2p_go_group_formation_completed = 0;
1457 wpa_s->group_formation_reported = 0;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001458
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001459 wpa_config_set_network_defaults(ssid);
1460 ssid->temporary = 1;
1461 ssid->p2p_group = 1;
1462 ssid->p2p_persistent_group = params->persistent_group;
1463 ssid->mode = group_formation ? WPAS_MODE_P2P_GROUP_FORMATION :
1464 WPAS_MODE_P2P_GO;
1465 ssid->frequency = params->freq;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001466 ssid->ht40 = params->ht40;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07001467 ssid->vht = params->vht;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001468 ssid->ssid = os_zalloc(params->ssid_len + 1);
1469 if (ssid->ssid) {
1470 os_memcpy(ssid->ssid, params->ssid, params->ssid_len);
1471 ssid->ssid_len = params->ssid_len;
1472 }
1473 ssid->auth_alg = WPA_AUTH_ALG_OPEN;
1474 ssid->key_mgmt = WPA_KEY_MGMT_PSK;
1475 ssid->proto = WPA_PROTO_RSN;
1476 ssid->pairwise_cipher = WPA_CIPHER_CCMP;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001477 ssid->group_cipher = WPA_CIPHER_CCMP;
1478 if (params->freq > 56160) {
1479 /*
1480 * Enable GCMP instead of CCMP as pairwise_cipher and
1481 * group_cipher in 60 GHz.
1482 */
1483 ssid->pairwise_cipher = WPA_CIPHER_GCMP;
1484 ssid->group_cipher = WPA_CIPHER_GCMP;
1485 }
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001486 if (os_strlen(params->passphrase) > 0) {
1487 ssid->passphrase = os_strdup(params->passphrase);
1488 if (ssid->passphrase == NULL) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07001489 wpa_msg_global(wpa_s, MSG_ERROR,
1490 "P2P: Failed to copy passphrase for GO");
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001491 wpa_config_remove_network(wpa_s->conf, ssid->id);
1492 return;
1493 }
1494 } else
1495 ssid->passphrase = NULL;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001496 ssid->psk_set = params->psk_set;
1497 if (ssid->psk_set)
1498 os_memcpy(ssid->psk, params->psk, sizeof(ssid->psk));
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001499 else if (ssid->passphrase)
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001500 wpa_config_update_psk(ssid);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001501 ssid->ap_max_inactivity = wpa_s->parent->conf->p2p_go_max_inactivity;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001502
1503 wpa_s->ap_configured_cb = p2p_go_configured;
1504 wpa_s->ap_configured_cb_ctx = wpa_s;
1505 wpa_s->ap_configured_cb_data = wpa_s->go_params;
Dmitry Shmidt6dc03bd2014-05-16 10:40:13 -07001506 wpa_s->scan_req = NORMAL_SCAN_REQ;
Jouni Malinen75ecf522011-06-27 15:19:46 -07001507 wpa_s->connect_without_scan = ssid;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001508 wpa_s->reassociate = 1;
1509 wpa_s->disconnected = 0;
Dmitry Shmidtaa532512012-09-24 10:35:31 -07001510 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Request scan (that will be skipped) to "
1511 "start GO)");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001512 wpa_supplicant_req_scan(wpa_s, 0, 0);
1513}
1514
1515
1516static void wpas_p2p_clone_config(struct wpa_supplicant *dst,
1517 const struct wpa_supplicant *src)
1518{
1519 struct wpa_config *d;
1520 const struct wpa_config *s;
1521
1522 d = dst->conf;
1523 s = src->conf;
1524
1525#define C(n) if (s->n) d->n = os_strdup(s->n)
1526 C(device_name);
1527 C(manufacturer);
1528 C(model_name);
1529 C(model_number);
1530 C(serial_number);
1531 C(config_methods);
1532#undef C
1533
1534 os_memcpy(d->device_type, s->device_type, WPS_DEV_TYPE_LEN);
1535 os_memcpy(d->sec_device_type, s->sec_device_type,
1536 sizeof(d->sec_device_type));
1537 d->num_sec_device_types = s->num_sec_device_types;
1538
1539 d->p2p_group_idle = s->p2p_group_idle;
1540 d->p2p_intra_bss = s->p2p_intra_bss;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001541 d->persistent_reconnect = s->persistent_reconnect;
Dmitry Shmidt04949592012-07-19 12:16:46 -07001542 d->max_num_sta = s->max_num_sta;
1543 d->pbc_in_m1 = s->pbc_in_m1;
Dmitry Shmidt444d5672013-04-01 13:08:44 -07001544 d->ignore_old_scan_res = s->ignore_old_scan_res;
Dmitry Shmidt51b6ea82013-05-08 10:42:09 -07001545 d->beacon_int = s->beacon_int;
Dmitry Shmidt18463232014-01-24 12:29:41 -08001546 d->dtim_period = s->dtim_period;
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07001547 d->disassoc_low_ack = s->disassoc_low_ack;
Dmitry Shmidt96571392013-10-14 12:54:46 -07001548 d->disable_scan_offload = s->disable_scan_offload;
Dmitry Shmidt807291d2015-01-27 13:40:23 -08001549 d->passive_scan = s->passive_scan;
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001550
1551 if (s->wps_nfc_dh_privkey && s->wps_nfc_dh_pubkey) {
1552 d->wps_nfc_dh_privkey = wpabuf_dup(s->wps_nfc_dh_privkey);
1553 d->wps_nfc_dh_pubkey = wpabuf_dup(s->wps_nfc_dh_pubkey);
1554 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001555}
1556
1557
Dmitry Shmidt34af3062013-07-11 10:46:32 -07001558static void wpas_p2p_get_group_ifname(struct wpa_supplicant *wpa_s,
1559 char *ifname, size_t len)
1560{
1561 char *ifname_ptr = wpa_s->ifname;
1562
1563 if (os_strncmp(wpa_s->ifname, P2P_MGMT_DEVICE_PREFIX,
1564 os_strlen(P2P_MGMT_DEVICE_PREFIX)) == 0) {
1565 ifname_ptr = os_strrchr(wpa_s->ifname, '-') + 1;
1566 }
1567
1568 os_snprintf(ifname, len, "p2p-%s-%d", ifname_ptr, wpa_s->p2p_group_idx);
1569 if (os_strlen(ifname) >= IFNAMSIZ &&
1570 os_strlen(wpa_s->ifname) < IFNAMSIZ) {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001571 int res;
1572
Dmitry Shmidt34af3062013-07-11 10:46:32 -07001573 /* Try to avoid going over the IFNAMSIZ length limit */
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001574 res = os_snprintf(ifname, len, "p2p-%d", wpa_s->p2p_group_idx);
1575 if (os_snprintf_error(len, res) && len)
1576 ifname[len - 1] = '\0';
Dmitry Shmidt34af3062013-07-11 10:46:32 -07001577 }
1578}
1579
1580
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001581static int wpas_p2p_add_group_interface(struct wpa_supplicant *wpa_s,
1582 enum wpa_driver_if_type type)
1583{
1584 char ifname[120], force_ifname[120];
1585
1586 if (wpa_s->pending_interface_name[0]) {
1587 wpa_printf(MSG_DEBUG, "P2P: Pending virtual interface exists "
1588 "- skip creation of a new one");
1589 if (is_zero_ether_addr(wpa_s->pending_interface_addr)) {
1590 wpa_printf(MSG_DEBUG, "P2P: Pending virtual address "
1591 "unknown?! ifname='%s'",
1592 wpa_s->pending_interface_name);
1593 return -1;
1594 }
1595 return 0;
1596 }
1597
Dmitry Shmidt34af3062013-07-11 10:46:32 -07001598 wpas_p2p_get_group_ifname(wpa_s, ifname, sizeof(ifname));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001599 force_ifname[0] = '\0';
1600
1601 wpa_printf(MSG_DEBUG, "P2P: Create a new interface %s for the group",
1602 ifname);
1603 wpa_s->p2p_group_idx++;
1604
1605 wpa_s->pending_interface_type = type;
1606 if (wpa_drv_if_add(wpa_s, type, ifname, NULL, NULL, force_ifname,
1607 wpa_s->pending_interface_addr, NULL) < 0) {
1608 wpa_printf(MSG_ERROR, "P2P: Failed to create new group "
1609 "interface");
1610 return -1;
1611 }
1612
1613 if (force_ifname[0]) {
1614 wpa_printf(MSG_DEBUG, "P2P: Driver forced interface name %s",
1615 force_ifname);
1616 os_strlcpy(wpa_s->pending_interface_name, force_ifname,
1617 sizeof(wpa_s->pending_interface_name));
1618 } else
1619 os_strlcpy(wpa_s->pending_interface_name, ifname,
1620 sizeof(wpa_s->pending_interface_name));
1621 wpa_printf(MSG_DEBUG, "P2P: Created pending virtual interface %s addr "
1622 MACSTR, wpa_s->pending_interface_name,
1623 MAC2STR(wpa_s->pending_interface_addr));
1624
1625 return 0;
1626}
1627
1628
1629static void wpas_p2p_remove_pending_group_interface(
1630 struct wpa_supplicant *wpa_s)
1631{
1632 if (!wpa_s->pending_interface_name[0] ||
1633 is_zero_ether_addr(wpa_s->pending_interface_addr))
1634 return; /* No pending virtual interface */
1635
1636 wpa_printf(MSG_DEBUG, "P2P: Removing pending group interface %s",
1637 wpa_s->pending_interface_name);
1638 wpa_drv_if_remove(wpa_s, wpa_s->pending_interface_type,
1639 wpa_s->pending_interface_name);
1640 os_memset(wpa_s->pending_interface_addr, 0, ETH_ALEN);
1641 wpa_s->pending_interface_name[0] = '\0';
1642}
1643
1644
1645static struct wpa_supplicant *
1646wpas_p2p_init_group_interface(struct wpa_supplicant *wpa_s, int go)
1647{
1648 struct wpa_interface iface;
1649 struct wpa_supplicant *group_wpa_s;
1650
1651 if (!wpa_s->pending_interface_name[0]) {
1652 wpa_printf(MSG_ERROR, "P2P: No pending group interface");
1653 if (!wpas_p2p_create_iface(wpa_s))
1654 return NULL;
1655 /*
1656 * Something has forced us to remove the pending interface; try
1657 * to create a new one and hope for the best that we will get
1658 * the same local address.
1659 */
1660 if (wpas_p2p_add_group_interface(wpa_s, go ? WPA_IF_P2P_GO :
1661 WPA_IF_P2P_CLIENT) < 0)
1662 return NULL;
1663 }
1664
1665 os_memset(&iface, 0, sizeof(iface));
1666 iface.ifname = wpa_s->pending_interface_name;
1667 iface.driver = wpa_s->driver->name;
Dmitry Shmidt34af3062013-07-11 10:46:32 -07001668 if (wpa_s->conf->ctrl_interface == NULL &&
1669 wpa_s->parent != wpa_s &&
1670 wpa_s->p2p_mgmt &&
1671 (wpa_s->drv_flags & WPA_DRIVER_FLAGS_DEDICATED_P2P_DEVICE))
1672 iface.ctrl_interface = wpa_s->parent->conf->ctrl_interface;
1673 else
1674 iface.ctrl_interface = wpa_s->conf->ctrl_interface;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001675 iface.driver_param = wpa_s->conf->driver_param;
1676 group_wpa_s = wpa_supplicant_add_iface(wpa_s->global, &iface);
1677 if (group_wpa_s == NULL) {
1678 wpa_printf(MSG_ERROR, "P2P: Failed to create new "
1679 "wpa_supplicant interface");
1680 return NULL;
1681 }
1682 wpa_s->pending_interface_name[0] = '\0';
1683 group_wpa_s->parent = wpa_s;
1684 group_wpa_s->p2p_group_interface = go ? P2P_GROUP_INTERFACE_GO :
1685 P2P_GROUP_INTERFACE_CLIENT;
1686 wpa_s->global->p2p_group_formation = group_wpa_s;
1687
1688 wpas_p2p_clone_config(group_wpa_s, wpa_s);
1689
1690 return group_wpa_s;
1691}
1692
1693
1694static void wpas_p2p_group_formation_timeout(void *eloop_ctx,
1695 void *timeout_ctx)
1696{
1697 struct wpa_supplicant *wpa_s = eloop_ctx;
1698 wpa_printf(MSG_DEBUG, "P2P: Group Formation timed out");
Dmitry Shmidt34af3062013-07-11 10:46:32 -07001699 wpas_p2p_group_formation_failed(wpa_s);
1700}
1701
1702
1703void wpas_p2p_group_formation_failed(struct wpa_supplicant *wpa_s)
1704{
1705 eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
1706 wpa_s->parent, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001707 if (wpa_s->global->p2p)
1708 p2p_group_formation_failed(wpa_s->global->p2p);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001709 wpas_group_formation_completed(wpa_s, 0);
1710}
1711
1712
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001713static void wpas_p2p_grpform_fail_after_wps(struct wpa_supplicant *wpa_s)
1714{
1715 wpa_printf(MSG_DEBUG, "P2P: Reject group formation due to WPS provisioning failure");
1716 eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
1717 wpa_s->parent, NULL);
1718 eloop_register_timeout(0, 0, wpas_p2p_group_formation_timeout,
1719 wpa_s->parent, NULL);
1720 wpa_s->global->p2p_fail_on_wps_complete = 0;
1721}
1722
1723
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07001724void wpas_p2p_ap_setup_failed(struct wpa_supplicant *wpa_s)
1725{
1726 if (wpa_s->global->p2p_group_formation != wpa_s)
1727 return;
1728 /* Speed up group formation timeout since this cannot succeed */
1729 eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
1730 wpa_s->parent, NULL);
1731 eloop_register_timeout(0, 0, wpas_p2p_group_formation_timeout,
1732 wpa_s->parent, NULL);
1733}
1734
1735
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001736static void wpas_go_neg_completed(void *ctx, struct p2p_go_neg_results *res)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001737{
1738 struct wpa_supplicant *wpa_s = ctx;
1739
1740 if (wpa_s->off_channel_freq || wpa_s->roc_waiting_drv_freq) {
1741 wpa_drv_cancel_remain_on_channel(wpa_s);
1742 wpa_s->off_channel_freq = 0;
1743 wpa_s->roc_waiting_drv_freq = 0;
1744 }
1745
1746 if (res->status) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07001747 wpa_msg_global(wpa_s, MSG_INFO,
1748 P2P_EVENT_GO_NEG_FAILURE "status=%d",
1749 res->status);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001750 wpas_notify_p2p_go_neg_completed(wpa_s, res);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001751 wpas_p2p_remove_pending_group_interface(wpa_s);
1752 return;
1753 }
1754
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001755 if (wpa_s->p2p_go_ht40)
1756 res->ht40 = 1;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07001757 if (wpa_s->p2p_go_vht)
1758 res->vht = 1;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001759
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07001760 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_GO_NEG_SUCCESS "role=%s "
1761 "freq=%d ht40=%d peer_dev=" MACSTR " peer_iface=" MACSTR
1762 " wps_method=%s",
1763 res->role_go ? "GO" : "client", res->freq, res->ht40,
1764 MAC2STR(res->peer_device_addr),
1765 MAC2STR(res->peer_interface_addr),
1766 p2p_wps_method_text(res->wps_method));
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001767 wpas_notify_p2p_go_neg_completed(wpa_s, res);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001768
Dmitry Shmidt04949592012-07-19 12:16:46 -07001769 if (res->role_go && wpa_s->p2p_persistent_id >= 0) {
1770 struct wpa_ssid *ssid;
1771 ssid = wpa_config_get_network(wpa_s->conf,
1772 wpa_s->p2p_persistent_id);
1773 if (ssid && ssid->disabled == 2 &&
1774 ssid->mode == WPAS_MODE_P2P_GO && ssid->passphrase) {
1775 size_t len = os_strlen(ssid->passphrase);
1776 wpa_printf(MSG_DEBUG, "P2P: Override passphrase based "
1777 "on requested persistent group");
1778 os_memcpy(res->passphrase, ssid->passphrase, len);
1779 res->passphrase[len] = '\0';
1780 }
1781 }
1782
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001783 if (wpa_s->create_p2p_iface) {
1784 struct wpa_supplicant *group_wpa_s =
1785 wpas_p2p_init_group_interface(wpa_s, res->role_go);
1786 if (group_wpa_s == NULL) {
1787 wpas_p2p_remove_pending_group_interface(wpa_s);
Dmitry Shmidtd11f0192014-03-24 12:09:47 -07001788 eloop_cancel_timeout(wpas_p2p_long_listen_timeout,
1789 wpa_s, NULL);
1790 wpas_p2p_group_formation_failed(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001791 return;
1792 }
1793 if (group_wpa_s != wpa_s) {
1794 os_memcpy(group_wpa_s->p2p_pin, wpa_s->p2p_pin,
1795 sizeof(group_wpa_s->p2p_pin));
1796 group_wpa_s->p2p_wps_method = wpa_s->p2p_wps_method;
1797 }
1798 os_memset(wpa_s->pending_interface_addr, 0, ETH_ALEN);
1799 wpa_s->pending_interface_name[0] = '\0';
1800 group_wpa_s->p2p_in_provisioning = 1;
1801
1802 if (res->role_go)
1803 wpas_start_wps_go(group_wpa_s, res, 1);
1804 else
1805 wpas_start_wps_enrollee(group_wpa_s, res);
1806 } else {
1807 wpa_s->p2p_in_provisioning = 1;
1808 wpa_s->global->p2p_group_formation = wpa_s;
1809
1810 if (res->role_go)
1811 wpas_start_wps_go(wpa_s, res, 1);
1812 else
1813 wpas_start_wps_enrollee(ctx, res);
1814 }
1815
1816 wpa_s->p2p_long_listen = 0;
1817 eloop_cancel_timeout(wpas_p2p_long_listen_timeout, wpa_s, NULL);
1818
1819 eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s, NULL);
1820 eloop_register_timeout(15 + res->peer_config_timeout / 100,
1821 (res->peer_config_timeout % 100) * 10000,
1822 wpas_p2p_group_formation_timeout, wpa_s, NULL);
1823}
1824
1825
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001826static void wpas_go_neg_req_rx(void *ctx, const u8 *src, u16 dev_passwd_id)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001827{
1828 struct wpa_supplicant *wpa_s = ctx;
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07001829 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_GO_NEG_REQUEST MACSTR
1830 " dev_passwd_id=%u", MAC2STR(src), dev_passwd_id);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001831
1832 wpas_notify_p2p_go_neg_req(wpa_s, src, dev_passwd_id);
1833}
1834
1835
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001836static void wpas_dev_found(void *ctx, const u8 *addr,
1837 const struct p2p_peer_info *info,
1838 int new_device)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001839{
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001840#ifndef CONFIG_NO_STDOUT_DEBUG
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001841 struct wpa_supplicant *wpa_s = ctx;
1842 char devtype[WPS_DEV_TYPE_BUFSIZE];
Dmitry Shmidt04f534e2013-12-09 15:50:16 -08001843 char *wfd_dev_info_hex = NULL;
1844
Irfan Sheriff8367dc92012-09-09 17:08:19 -07001845#ifdef CONFIG_WIFI_DISPLAY
Dmitry Shmidt04f534e2013-12-09 15:50:16 -08001846 wfd_dev_info_hex = wifi_display_subelem_hex(info->wfd_subelems,
1847 WFD_SUBELEM_DEVICE_INFO);
Irfan Sheriff8367dc92012-09-09 17:08:19 -07001848#endif /* CONFIG_WIFI_DISPLAY */
Dmitry Shmidt04f534e2013-12-09 15:50:16 -08001849
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07001850 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_DEVICE_FOUND MACSTR
1851 " p2p_dev_addr=" MACSTR
1852 " pri_dev_type=%s name='%s' config_methods=0x%x "
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001853 "dev_capab=0x%x group_capab=0x%x%s%s%s new=%d",
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07001854 MAC2STR(addr), MAC2STR(info->p2p_device_addr),
1855 wps_dev_type_bin2str(info->pri_dev_type, devtype,
1856 sizeof(devtype)),
1857 info->device_name, info->config_methods,
1858 info->dev_capab, info->group_capab,
Dmitry Shmidt04f534e2013-12-09 15:50:16 -08001859 wfd_dev_info_hex ? " wfd_dev_info=0x" : "",
Dmitry Shmidt2e67f062014-07-16 09:55:28 -07001860 wfd_dev_info_hex ? wfd_dev_info_hex : "",
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001861 info->vendor_elems ? " vendor_elems=1" : "",
1862 new_device);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001863
Dmitry Shmidt04f534e2013-12-09 15:50:16 -08001864 os_free(wfd_dev_info_hex);
Dmitry Shmidt97672262014-02-03 13:02:54 -08001865#endif /* CONFIG_NO_STDOUT_DEBUG */
Dmitry Shmidt04f534e2013-12-09 15:50:16 -08001866
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001867 wpas_notify_p2p_device_found(ctx, info->p2p_device_addr, new_device);
1868}
1869
1870
1871static void wpas_dev_lost(void *ctx, const u8 *dev_addr)
1872{
1873 struct wpa_supplicant *wpa_s = ctx;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001874
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07001875 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_DEVICE_LOST
1876 "p2p_dev_addr=" MACSTR, MAC2STR(dev_addr));
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001877
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001878 wpas_notify_p2p_device_lost(wpa_s, dev_addr);
1879}
1880
1881
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07001882static void wpas_find_stopped(void *ctx)
1883{
1884 struct wpa_supplicant *wpa_s = ctx;
1885 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_FIND_STOPPED);
1886}
1887
1888
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001889struct wpas_p2p_listen_work {
1890 unsigned int freq;
1891 unsigned int duration;
1892 struct wpabuf *probe_resp_ie;
1893};
1894
1895
1896static void wpas_p2p_listen_work_free(struct wpas_p2p_listen_work *lwork)
1897{
1898 if (lwork == NULL)
1899 return;
1900 wpabuf_free(lwork->probe_resp_ie);
1901 os_free(lwork);
1902}
1903
1904
1905static void wpas_p2p_listen_work_done(struct wpa_supplicant *wpa_s)
1906{
1907 struct wpas_p2p_listen_work *lwork;
1908
1909 if (!wpa_s->p2p_listen_work)
1910 return;
1911
1912 lwork = wpa_s->p2p_listen_work->ctx;
1913 wpas_p2p_listen_work_free(lwork);
1914 radio_work_done(wpa_s->p2p_listen_work);
1915 wpa_s->p2p_listen_work = NULL;
1916}
1917
1918
1919static void wpas_start_listen_cb(struct wpa_radio_work *work, int deinit)
1920{
1921 struct wpa_supplicant *wpa_s = work->wpa_s;
1922 struct wpas_p2p_listen_work *lwork = work->ctx;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001923 unsigned int duration;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001924
1925 if (deinit) {
Dmitry Shmidtbd14a572014-02-18 10:33:49 -08001926 if (work->started) {
1927 wpa_s->p2p_listen_work = NULL;
1928 wpas_stop_listen(wpa_s);
1929 }
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001930 wpas_p2p_listen_work_free(lwork);
1931 return;
1932 }
1933
1934 wpa_s->p2p_listen_work = work;
1935
1936 wpa_drv_set_ap_wps_ie(wpa_s, NULL, lwork->probe_resp_ie, NULL);
1937
1938 if (wpa_drv_probe_req_report(wpa_s, 1) < 0) {
1939 wpa_printf(MSG_DEBUG, "P2P: Failed to request the driver to "
1940 "report received Probe Request frames");
1941 wpas_p2p_listen_work_done(wpa_s);
1942 return;
1943 }
1944
1945 wpa_s->pending_listen_freq = lwork->freq;
1946 wpa_s->pending_listen_duration = lwork->duration;
1947
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001948 duration = lwork->duration;
1949#ifdef CONFIG_TESTING_OPTIONS
1950 if (wpa_s->extra_roc_dur) {
1951 wpa_printf(MSG_DEBUG, "TESTING: Increase ROC duration %u -> %u",
1952 duration, duration + wpa_s->extra_roc_dur);
1953 duration += wpa_s->extra_roc_dur;
1954 }
1955#endif /* CONFIG_TESTING_OPTIONS */
1956
1957 if (wpa_drv_remain_on_channel(wpa_s, lwork->freq, duration) < 0) {
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001958 wpa_printf(MSG_DEBUG, "P2P: Failed to request the driver "
1959 "to remain on channel (%u MHz) for Listen "
1960 "state", lwork->freq);
1961 wpas_p2p_listen_work_done(wpa_s);
1962 wpa_s->pending_listen_freq = 0;
1963 return;
1964 }
1965 wpa_s->off_channel_freq = 0;
1966 wpa_s->roc_waiting_drv_freq = lwork->freq;
1967}
1968
1969
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001970static int wpas_start_listen(void *ctx, unsigned int freq,
1971 unsigned int duration,
1972 const struct wpabuf *probe_resp_ie)
1973{
1974 struct wpa_supplicant *wpa_s = ctx;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001975 struct wpas_p2p_listen_work *lwork;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001976
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001977 if (wpa_s->p2p_listen_work) {
1978 wpa_printf(MSG_DEBUG, "P2P: Reject start_listen since p2p_listen_work already exists");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001979 return -1;
1980 }
1981
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001982 lwork = os_zalloc(sizeof(*lwork));
1983 if (lwork == NULL)
1984 return -1;
1985 lwork->freq = freq;
1986 lwork->duration = duration;
1987 if (probe_resp_ie) {
1988 lwork->probe_resp_ie = wpabuf_dup(probe_resp_ie);
1989 if (lwork->probe_resp_ie == NULL) {
1990 wpas_p2p_listen_work_free(lwork);
1991 return -1;
1992 }
1993 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001994
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001995 if (radio_add_work(wpa_s, freq, "p2p-listen", 0, wpas_start_listen_cb,
1996 lwork) < 0) {
1997 wpas_p2p_listen_work_free(lwork);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001998 return -1;
1999 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002000
2001 return 0;
2002}
2003
2004
2005static void wpas_stop_listen(void *ctx)
2006{
2007 struct wpa_supplicant *wpa_s = ctx;
2008 if (wpa_s->off_channel_freq || wpa_s->roc_waiting_drv_freq) {
2009 wpa_drv_cancel_remain_on_channel(wpa_s);
2010 wpa_s->off_channel_freq = 0;
2011 wpa_s->roc_waiting_drv_freq = 0;
2012 }
2013 wpa_drv_set_ap_wps_ie(wpa_s, NULL, NULL, NULL);
2014 wpa_drv_probe_req_report(wpa_s, 0);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002015 wpas_p2p_listen_work_done(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002016}
2017
2018
2019static int wpas_send_probe_resp(void *ctx, const struct wpabuf *buf)
2020{
2021 struct wpa_supplicant *wpa_s = ctx;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002022 return wpa_drv_send_mlme(wpa_s, wpabuf_head(buf), wpabuf_len(buf), 1);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002023}
2024
2025
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002026/*
2027 * DNS Header section is used only to calculate compression pointers, so the
2028 * contents of this data does not matter, but the length needs to be reserved
2029 * in the virtual packet.
2030 */
2031#define DNS_HEADER_LEN 12
2032
2033/*
2034 * 27-octet in-memory packet from P2P specification containing two implied
2035 * queries for _tcp.lcoal. PTR IN and _udp.local. PTR IN
2036 */
2037#define P2P_SD_IN_MEMORY_LEN 27
2038
2039static int p2p_sd_dns_uncompress_label(char **upos, char *uend, u8 *start,
2040 u8 **spos, const u8 *end)
2041{
2042 while (*spos < end) {
2043 u8 val = ((*spos)[0] & 0xc0) >> 6;
2044 int len;
2045
2046 if (val == 1 || val == 2) {
2047 /* These are reserved values in RFC 1035 */
2048 wpa_printf(MSG_DEBUG, "P2P: Invalid domain name "
2049 "sequence starting with 0x%x", val);
2050 return -1;
2051 }
2052
2053 if (val == 3) {
2054 u16 offset;
2055 u8 *spos_tmp;
2056
2057 /* Offset */
2058 if (*spos + 2 > end) {
2059 wpa_printf(MSG_DEBUG, "P2P: No room for full "
2060 "DNS offset field");
2061 return -1;
2062 }
2063
2064 offset = (((*spos)[0] & 0x3f) << 8) | (*spos)[1];
2065 if (offset >= *spos - start) {
2066 wpa_printf(MSG_DEBUG, "P2P: Invalid DNS "
2067 "pointer offset %u", offset);
2068 return -1;
2069 }
2070
2071 (*spos) += 2;
2072 spos_tmp = start + offset;
2073 return p2p_sd_dns_uncompress_label(upos, uend, start,
2074 &spos_tmp,
2075 *spos - 2);
2076 }
2077
2078 /* Label */
2079 len = (*spos)[0] & 0x3f;
2080 if (len == 0)
2081 return 0;
2082
2083 (*spos)++;
2084 if (*spos + len > end) {
2085 wpa_printf(MSG_DEBUG, "P2P: Invalid domain name "
2086 "sequence - no room for label with length "
2087 "%u", len);
2088 return -1;
2089 }
2090
2091 if (*upos + len + 2 > uend)
2092 return -2;
2093
2094 os_memcpy(*upos, *spos, len);
2095 *spos += len;
2096 *upos += len;
2097 (*upos)[0] = '.';
2098 (*upos)++;
2099 (*upos)[0] = '\0';
2100 }
2101
2102 return 0;
2103}
2104
2105
2106/* Uncompress domain names per RFC 1035 using the P2P SD in-memory packet.
2107 * Returns -1 on parsing error (invalid input sequence), -2 if output buffer is
2108 * not large enough */
2109static int p2p_sd_dns_uncompress(char *buf, size_t buf_len, const u8 *msg,
2110 size_t msg_len, size_t offset)
2111{
2112 /* 27-octet in-memory packet from P2P specification */
2113 const char *prefix = "\x04_tcp\x05local\x00\x00\x0C\x00\x01"
2114 "\x04_udp\xC0\x11\x00\x0C\x00\x01";
2115 u8 *tmp, *end, *spos;
2116 char *upos, *uend;
2117 int ret = 0;
2118
2119 if (buf_len < 2)
2120 return -1;
2121 if (offset > msg_len)
2122 return -1;
2123
2124 tmp = os_malloc(DNS_HEADER_LEN + P2P_SD_IN_MEMORY_LEN + msg_len);
2125 if (tmp == NULL)
2126 return -1;
2127 spos = tmp + DNS_HEADER_LEN + P2P_SD_IN_MEMORY_LEN;
2128 end = spos + msg_len;
2129 spos += offset;
2130
2131 os_memset(tmp, 0, DNS_HEADER_LEN);
2132 os_memcpy(tmp + DNS_HEADER_LEN, prefix, P2P_SD_IN_MEMORY_LEN);
2133 os_memcpy(tmp + DNS_HEADER_LEN + P2P_SD_IN_MEMORY_LEN, msg, msg_len);
2134
2135 upos = buf;
2136 uend = buf + buf_len;
2137
2138 ret = p2p_sd_dns_uncompress_label(&upos, uend, tmp, &spos, end);
2139 if (ret) {
2140 os_free(tmp);
2141 return ret;
2142 }
2143
2144 if (upos == buf) {
2145 upos[0] = '.';
2146 upos[1] = '\0';
2147 } else if (upos[-1] == '.')
2148 upos[-1] = '\0';
2149
2150 os_free(tmp);
2151 return 0;
2152}
2153
2154
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002155static struct p2p_srv_bonjour *
2156wpas_p2p_service_get_bonjour(struct wpa_supplicant *wpa_s,
2157 const struct wpabuf *query)
2158{
2159 struct p2p_srv_bonjour *bsrv;
2160 size_t len;
2161
2162 len = wpabuf_len(query);
2163 dl_list_for_each(bsrv, &wpa_s->global->p2p_srv_bonjour,
2164 struct p2p_srv_bonjour, list) {
2165 if (len == wpabuf_len(bsrv->query) &&
2166 os_memcmp(wpabuf_head(query), wpabuf_head(bsrv->query),
2167 len) == 0)
2168 return bsrv;
2169 }
2170 return NULL;
2171}
2172
2173
2174static struct p2p_srv_upnp *
2175wpas_p2p_service_get_upnp(struct wpa_supplicant *wpa_s, u8 version,
2176 const char *service)
2177{
2178 struct p2p_srv_upnp *usrv;
2179
2180 dl_list_for_each(usrv, &wpa_s->global->p2p_srv_upnp,
2181 struct p2p_srv_upnp, list) {
2182 if (version == usrv->version &&
2183 os_strcmp(service, usrv->service) == 0)
2184 return usrv;
2185 }
2186 return NULL;
2187}
2188
2189
2190static void wpas_sd_add_proto_not_avail(struct wpabuf *resp, u8 srv_proto,
2191 u8 srv_trans_id)
2192{
2193 u8 *len_pos;
2194
2195 if (wpabuf_tailroom(resp) < 5)
2196 return;
2197
2198 /* Length (to be filled) */
2199 len_pos = wpabuf_put(resp, 2);
2200 wpabuf_put_u8(resp, srv_proto);
2201 wpabuf_put_u8(resp, srv_trans_id);
2202 /* Status Code */
2203 wpabuf_put_u8(resp, P2P_SD_PROTO_NOT_AVAILABLE);
2204 /* Response Data: empty */
2205 WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos - 2);
2206}
2207
2208
2209static void wpas_sd_all_bonjour(struct wpa_supplicant *wpa_s,
2210 struct wpabuf *resp, u8 srv_trans_id)
2211{
2212 struct p2p_srv_bonjour *bsrv;
2213 u8 *len_pos;
2214
2215 wpa_printf(MSG_DEBUG, "P2P: SD Request for all Bonjour services");
2216
2217 if (dl_list_empty(&wpa_s->global->p2p_srv_bonjour)) {
2218 wpa_printf(MSG_DEBUG, "P2P: Bonjour protocol not available");
2219 return;
2220 }
2221
2222 dl_list_for_each(bsrv, &wpa_s->global->p2p_srv_bonjour,
2223 struct p2p_srv_bonjour, list) {
2224 if (wpabuf_tailroom(resp) <
2225 5 + wpabuf_len(bsrv->query) + wpabuf_len(bsrv->resp))
2226 return;
2227 /* Length (to be filled) */
2228 len_pos = wpabuf_put(resp, 2);
2229 wpabuf_put_u8(resp, P2P_SERV_BONJOUR);
2230 wpabuf_put_u8(resp, srv_trans_id);
2231 /* Status Code */
2232 wpabuf_put_u8(resp, P2P_SD_SUCCESS);
2233 wpa_hexdump_ascii(MSG_DEBUG, "P2P: Matching Bonjour service",
2234 wpabuf_head(bsrv->resp),
2235 wpabuf_len(bsrv->resp));
2236 /* Response Data */
2237 wpabuf_put_buf(resp, bsrv->query); /* Key */
2238 wpabuf_put_buf(resp, bsrv->resp); /* Value */
2239 WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos -
2240 2);
2241 }
2242}
2243
2244
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002245static int match_bonjour_query(struct p2p_srv_bonjour *bsrv, const u8 *query,
2246 size_t query_len)
2247{
2248 char str_rx[256], str_srv[256];
2249
2250 if (query_len < 3 || wpabuf_len(bsrv->query) < 3)
2251 return 0; /* Too short to include DNS Type and Version */
2252 if (os_memcmp(query + query_len - 3,
2253 wpabuf_head_u8(bsrv->query) + wpabuf_len(bsrv->query) - 3,
2254 3) != 0)
2255 return 0; /* Mismatch in DNS Type or Version */
2256 if (query_len == wpabuf_len(bsrv->query) &&
2257 os_memcmp(query, wpabuf_head(bsrv->query), query_len - 3) == 0)
2258 return 1; /* Binary match */
2259
2260 if (p2p_sd_dns_uncompress(str_rx, sizeof(str_rx), query, query_len - 3,
2261 0))
2262 return 0; /* Failed to uncompress query */
2263 if (p2p_sd_dns_uncompress(str_srv, sizeof(str_srv),
2264 wpabuf_head(bsrv->query),
2265 wpabuf_len(bsrv->query) - 3, 0))
2266 return 0; /* Failed to uncompress service */
2267
2268 return os_strcmp(str_rx, str_srv) == 0;
2269}
2270
2271
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002272static void wpas_sd_req_bonjour(struct wpa_supplicant *wpa_s,
2273 struct wpabuf *resp, u8 srv_trans_id,
2274 const u8 *query, size_t query_len)
2275{
2276 struct p2p_srv_bonjour *bsrv;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002277 u8 *len_pos;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002278 int matches = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002279
2280 wpa_hexdump_ascii(MSG_DEBUG, "P2P: SD Request for Bonjour",
2281 query, query_len);
2282 if (dl_list_empty(&wpa_s->global->p2p_srv_bonjour)) {
2283 wpa_printf(MSG_DEBUG, "P2P: Bonjour protocol not available");
2284 wpas_sd_add_proto_not_avail(resp, P2P_SERV_BONJOUR,
2285 srv_trans_id);
2286 return;
2287 }
2288
2289 if (query_len == 0) {
2290 wpas_sd_all_bonjour(wpa_s, resp, srv_trans_id);
2291 return;
2292 }
2293
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002294 dl_list_for_each(bsrv, &wpa_s->global->p2p_srv_bonjour,
2295 struct p2p_srv_bonjour, list) {
2296 if (!match_bonjour_query(bsrv, query, query_len))
2297 continue;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002298
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002299 if (wpabuf_tailroom(resp) <
2300 5 + query_len + wpabuf_len(bsrv->resp))
2301 return;
2302
2303 matches++;
2304
2305 /* Length (to be filled) */
2306 len_pos = wpabuf_put(resp, 2);
2307 wpabuf_put_u8(resp, P2P_SERV_BONJOUR);
2308 wpabuf_put_u8(resp, srv_trans_id);
2309
2310 /* Status Code */
2311 wpabuf_put_u8(resp, P2P_SD_SUCCESS);
2312 wpa_hexdump_ascii(MSG_DEBUG, "P2P: Matching Bonjour service",
2313 wpabuf_head(bsrv->resp),
2314 wpabuf_len(bsrv->resp));
2315
2316 /* Response Data */
2317 wpabuf_put_data(resp, query, query_len); /* Key */
2318 wpabuf_put_buf(resp, bsrv->resp); /* Value */
2319
2320 WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos - 2);
2321 }
2322
2323 if (matches == 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002324 wpa_printf(MSG_DEBUG, "P2P: Requested Bonjour service not "
2325 "available");
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002326 if (wpabuf_tailroom(resp) < 5)
2327 return;
2328
2329 /* Length (to be filled) */
2330 len_pos = wpabuf_put(resp, 2);
2331 wpabuf_put_u8(resp, P2P_SERV_BONJOUR);
2332 wpabuf_put_u8(resp, srv_trans_id);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002333
2334 /* Status Code */
2335 wpabuf_put_u8(resp, P2P_SD_REQUESTED_INFO_NOT_AVAILABLE);
2336 /* Response Data: empty */
2337 WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos -
2338 2);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002339 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002340}
2341
2342
2343static void wpas_sd_all_upnp(struct wpa_supplicant *wpa_s,
2344 struct wpabuf *resp, u8 srv_trans_id)
2345{
2346 struct p2p_srv_upnp *usrv;
2347 u8 *len_pos;
2348
2349 wpa_printf(MSG_DEBUG, "P2P: SD Request for all UPnP services");
2350
2351 if (dl_list_empty(&wpa_s->global->p2p_srv_upnp)) {
2352 wpa_printf(MSG_DEBUG, "P2P: UPnP protocol not available");
2353 return;
2354 }
2355
2356 dl_list_for_each(usrv, &wpa_s->global->p2p_srv_upnp,
2357 struct p2p_srv_upnp, list) {
2358 if (wpabuf_tailroom(resp) < 5 + 1 + os_strlen(usrv->service))
2359 return;
2360
2361 /* Length (to be filled) */
2362 len_pos = wpabuf_put(resp, 2);
2363 wpabuf_put_u8(resp, P2P_SERV_UPNP);
2364 wpabuf_put_u8(resp, srv_trans_id);
2365
2366 /* Status Code */
2367 wpabuf_put_u8(resp, P2P_SD_SUCCESS);
2368 /* Response Data */
2369 wpabuf_put_u8(resp, usrv->version);
2370 wpa_printf(MSG_DEBUG, "P2P: Matching UPnP Service: %s",
2371 usrv->service);
2372 wpabuf_put_str(resp, usrv->service);
2373 WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos -
2374 2);
2375 }
2376}
2377
2378
2379static void wpas_sd_req_upnp(struct wpa_supplicant *wpa_s,
2380 struct wpabuf *resp, u8 srv_trans_id,
2381 const u8 *query, size_t query_len)
2382{
2383 struct p2p_srv_upnp *usrv;
2384 u8 *len_pos;
2385 u8 version;
2386 char *str;
2387 int count = 0;
2388
2389 wpa_hexdump_ascii(MSG_DEBUG, "P2P: SD Request for UPnP",
2390 query, query_len);
2391
2392 if (dl_list_empty(&wpa_s->global->p2p_srv_upnp)) {
2393 wpa_printf(MSG_DEBUG, "P2P: UPnP protocol not available");
2394 wpas_sd_add_proto_not_avail(resp, P2P_SERV_UPNP,
2395 srv_trans_id);
2396 return;
2397 }
2398
2399 if (query_len == 0) {
2400 wpas_sd_all_upnp(wpa_s, resp, srv_trans_id);
2401 return;
2402 }
2403
2404 if (wpabuf_tailroom(resp) < 5)
2405 return;
2406
2407 /* Length (to be filled) */
2408 len_pos = wpabuf_put(resp, 2);
2409 wpabuf_put_u8(resp, P2P_SERV_UPNP);
2410 wpabuf_put_u8(resp, srv_trans_id);
2411
2412 version = query[0];
2413 str = os_malloc(query_len);
2414 if (str == NULL)
2415 return;
2416 os_memcpy(str, query + 1, query_len - 1);
2417 str[query_len - 1] = '\0';
2418
2419 dl_list_for_each(usrv, &wpa_s->global->p2p_srv_upnp,
2420 struct p2p_srv_upnp, list) {
2421 if (version != usrv->version)
2422 continue;
2423
2424 if (os_strcmp(str, "ssdp:all") != 0 &&
2425 os_strstr(usrv->service, str) == NULL)
2426 continue;
2427
2428 if (wpabuf_tailroom(resp) < 2)
2429 break;
2430 if (count == 0) {
2431 /* Status Code */
2432 wpabuf_put_u8(resp, P2P_SD_SUCCESS);
2433 /* Response Data */
2434 wpabuf_put_u8(resp, version);
2435 } else
2436 wpabuf_put_u8(resp, ',');
2437
2438 count++;
2439
2440 wpa_printf(MSG_DEBUG, "P2P: Matching UPnP Service: %s",
2441 usrv->service);
2442 if (wpabuf_tailroom(resp) < os_strlen(usrv->service))
2443 break;
2444 wpabuf_put_str(resp, usrv->service);
2445 }
2446 os_free(str);
2447
2448 if (count == 0) {
2449 wpa_printf(MSG_DEBUG, "P2P: Requested UPnP service not "
2450 "available");
2451 /* Status Code */
2452 wpabuf_put_u8(resp, P2P_SD_REQUESTED_INFO_NOT_AVAILABLE);
2453 /* Response Data: empty */
2454 }
2455
2456 WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos - 2);
2457}
2458
2459
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002460#ifdef CONFIG_WIFI_DISPLAY
2461static void wpas_sd_req_wfd(struct wpa_supplicant *wpa_s,
2462 struct wpabuf *resp, u8 srv_trans_id,
2463 const u8 *query, size_t query_len)
2464{
2465 const u8 *pos;
2466 u8 role;
2467 u8 *len_pos;
2468
2469 wpa_hexdump(MSG_DEBUG, "P2P: SD Request for WFD", query, query_len);
2470
2471 if (!wpa_s->global->wifi_display) {
2472 wpa_printf(MSG_DEBUG, "P2P: WFD protocol not available");
2473 wpas_sd_add_proto_not_avail(resp, P2P_SERV_WIFI_DISPLAY,
2474 srv_trans_id);
2475 return;
2476 }
2477
2478 if (query_len < 1) {
2479 wpa_printf(MSG_DEBUG, "P2P: Missing WFD Requested Device "
2480 "Role");
2481 return;
2482 }
2483
2484 if (wpabuf_tailroom(resp) < 5)
2485 return;
2486
2487 pos = query;
2488 role = *pos++;
2489 wpa_printf(MSG_DEBUG, "P2P: WSD for device role 0x%x", role);
2490
2491 /* TODO: role specific handling */
2492
2493 /* Length (to be filled) */
2494 len_pos = wpabuf_put(resp, 2);
2495 wpabuf_put_u8(resp, P2P_SERV_WIFI_DISPLAY);
2496 wpabuf_put_u8(resp, srv_trans_id);
2497 wpabuf_put_u8(resp, P2P_SD_SUCCESS); /* Status Code */
2498
2499 while (pos < query + query_len) {
2500 if (*pos < MAX_WFD_SUBELEMS &&
2501 wpa_s->global->wfd_subelem[*pos] &&
2502 wpabuf_tailroom(resp) >=
2503 wpabuf_len(wpa_s->global->wfd_subelem[*pos])) {
2504 wpa_printf(MSG_DEBUG, "P2P: Add WSD response "
2505 "subelement %u", *pos);
2506 wpabuf_put_buf(resp, wpa_s->global->wfd_subelem[*pos]);
2507 }
2508 pos++;
2509 }
2510
2511 WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos - 2);
2512}
2513#endif /* CONFIG_WIFI_DISPLAY */
2514
2515
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002516static void wpas_sd_request(void *ctx, int freq, const u8 *sa, u8 dialog_token,
2517 u16 update_indic, const u8 *tlvs, size_t tlvs_len)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002518{
2519 struct wpa_supplicant *wpa_s = ctx;
2520 const u8 *pos = tlvs;
2521 const u8 *end = tlvs + tlvs_len;
2522 const u8 *tlv_end;
2523 u16 slen;
2524 struct wpabuf *resp;
2525 u8 srv_proto, srv_trans_id;
2526 size_t buf_len;
2527 char *buf;
2528
2529 wpa_hexdump(MSG_MSGDUMP, "P2P: Service Discovery Request TLVs",
2530 tlvs, tlvs_len);
2531 buf_len = 2 * tlvs_len + 1;
2532 buf = os_malloc(buf_len);
2533 if (buf) {
2534 wpa_snprintf_hex(buf, buf_len, tlvs, tlvs_len);
2535 wpa_msg_ctrl(wpa_s, MSG_INFO, P2P_EVENT_SERV_DISC_REQ "%d "
2536 MACSTR " %u %u %s",
2537 freq, MAC2STR(sa), dialog_token, update_indic,
2538 buf);
2539 os_free(buf);
2540 }
2541
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002542 if (wpa_s->p2p_sd_over_ctrl_iface) {
2543 wpas_notify_p2p_sd_request(wpa_s, freq, sa, dialog_token,
2544 update_indic, tlvs, tlvs_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002545 return; /* to be processed by an external program */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002546 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002547
2548 resp = wpabuf_alloc(10000);
2549 if (resp == NULL)
2550 return;
2551
2552 while (pos + 1 < end) {
2553 wpa_printf(MSG_DEBUG, "P2P: Service Request TLV");
2554 slen = WPA_GET_LE16(pos);
2555 pos += 2;
2556 if (pos + slen > end || slen < 2) {
2557 wpa_printf(MSG_DEBUG, "P2P: Unexpected Query Data "
2558 "length");
2559 wpabuf_free(resp);
2560 return;
2561 }
2562 tlv_end = pos + slen;
2563
2564 srv_proto = *pos++;
2565 wpa_printf(MSG_DEBUG, "P2P: Service Protocol Type %u",
2566 srv_proto);
2567 srv_trans_id = *pos++;
2568 wpa_printf(MSG_DEBUG, "P2P: Service Transaction ID %u",
2569 srv_trans_id);
2570
2571 wpa_hexdump(MSG_MSGDUMP, "P2P: Query Data",
2572 pos, tlv_end - pos);
2573
2574
2575 if (wpa_s->force_long_sd) {
2576 wpa_printf(MSG_DEBUG, "P2P: SD test - force long "
2577 "response");
2578 wpas_sd_all_bonjour(wpa_s, resp, srv_trans_id);
2579 wpas_sd_all_upnp(wpa_s, resp, srv_trans_id);
2580 goto done;
2581 }
2582
2583 switch (srv_proto) {
2584 case P2P_SERV_ALL_SERVICES:
2585 wpa_printf(MSG_DEBUG, "P2P: Service Discovery Request "
2586 "for all services");
2587 if (dl_list_empty(&wpa_s->global->p2p_srv_upnp) &&
2588 dl_list_empty(&wpa_s->global->p2p_srv_bonjour)) {
2589 wpa_printf(MSG_DEBUG, "P2P: No service "
2590 "discovery protocols available");
2591 wpas_sd_add_proto_not_avail(
2592 resp, P2P_SERV_ALL_SERVICES,
2593 srv_trans_id);
2594 break;
2595 }
2596 wpas_sd_all_bonjour(wpa_s, resp, srv_trans_id);
2597 wpas_sd_all_upnp(wpa_s, resp, srv_trans_id);
2598 break;
2599 case P2P_SERV_BONJOUR:
2600 wpas_sd_req_bonjour(wpa_s, resp, srv_trans_id,
2601 pos, tlv_end - pos);
2602 break;
2603 case P2P_SERV_UPNP:
2604 wpas_sd_req_upnp(wpa_s, resp, srv_trans_id,
2605 pos, tlv_end - pos);
2606 break;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002607#ifdef CONFIG_WIFI_DISPLAY
2608 case P2P_SERV_WIFI_DISPLAY:
2609 wpas_sd_req_wfd(wpa_s, resp, srv_trans_id,
2610 pos, tlv_end - pos);
2611 break;
2612#endif /* CONFIG_WIFI_DISPLAY */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002613 default:
2614 wpa_printf(MSG_DEBUG, "P2P: Unavailable service "
2615 "protocol %u", srv_proto);
2616 wpas_sd_add_proto_not_avail(resp, srv_proto,
2617 srv_trans_id);
2618 break;
2619 }
2620
2621 pos = tlv_end;
2622 }
2623
2624done:
2625 wpas_notify_p2p_sd_request(wpa_s, freq, sa, dialog_token,
2626 update_indic, tlvs, tlvs_len);
2627
2628 wpas_p2p_sd_response(wpa_s, freq, sa, dialog_token, resp);
2629
2630 wpabuf_free(resp);
2631}
2632
2633
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002634static void wpas_sd_response(void *ctx, const u8 *sa, u16 update_indic,
2635 const u8 *tlvs, size_t tlvs_len)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002636{
2637 struct wpa_supplicant *wpa_s = ctx;
2638 const u8 *pos = tlvs;
2639 const u8 *end = tlvs + tlvs_len;
2640 const u8 *tlv_end;
2641 u16 slen;
2642 size_t buf_len;
2643 char *buf;
2644
2645 wpa_hexdump(MSG_MSGDUMP, "P2P: Service Discovery Response TLVs",
2646 tlvs, tlvs_len);
2647 if (tlvs_len > 1500) {
2648 /* TODO: better way for handling this */
2649 wpa_msg_ctrl(wpa_s, MSG_INFO,
2650 P2P_EVENT_SERV_DISC_RESP MACSTR
2651 " %u <long response: %u bytes>",
2652 MAC2STR(sa), update_indic,
2653 (unsigned int) tlvs_len);
2654 } else {
2655 buf_len = 2 * tlvs_len + 1;
2656 buf = os_malloc(buf_len);
2657 if (buf) {
2658 wpa_snprintf_hex(buf, buf_len, tlvs, tlvs_len);
2659 wpa_msg_ctrl(wpa_s, MSG_INFO,
2660 P2P_EVENT_SERV_DISC_RESP MACSTR " %u %s",
2661 MAC2STR(sa), update_indic, buf);
2662 os_free(buf);
2663 }
2664 }
2665
2666 while (pos < end) {
2667 u8 srv_proto, srv_trans_id, status;
2668
2669 wpa_printf(MSG_DEBUG, "P2P: Service Response TLV");
2670 slen = WPA_GET_LE16(pos);
2671 pos += 2;
2672 if (pos + slen > end || slen < 3) {
2673 wpa_printf(MSG_DEBUG, "P2P: Unexpected Response Data "
2674 "length");
2675 return;
2676 }
2677 tlv_end = pos + slen;
2678
2679 srv_proto = *pos++;
2680 wpa_printf(MSG_DEBUG, "P2P: Service Protocol Type %u",
2681 srv_proto);
2682 srv_trans_id = *pos++;
2683 wpa_printf(MSG_DEBUG, "P2P: Service Transaction ID %u",
2684 srv_trans_id);
2685 status = *pos++;
2686 wpa_printf(MSG_DEBUG, "P2P: Status Code ID %u",
2687 status);
2688
2689 wpa_hexdump(MSG_MSGDUMP, "P2P: Response Data",
2690 pos, tlv_end - pos);
2691
2692 pos = tlv_end;
2693 }
2694
2695 wpas_notify_p2p_sd_response(wpa_s, sa, update_indic, tlvs, tlvs_len);
2696}
2697
2698
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002699u64 wpas_p2p_sd_request(struct wpa_supplicant *wpa_s, const u8 *dst,
2700 const struct wpabuf *tlvs)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002701{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002702 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002703 return 0;
2704 return (uintptr_t) p2p_sd_request(wpa_s->global->p2p, dst, tlvs);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002705}
2706
2707
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002708u64 wpas_p2p_sd_request_upnp(struct wpa_supplicant *wpa_s, const u8 *dst,
2709 u8 version, const char *query)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002710{
2711 struct wpabuf *tlvs;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002712 u64 ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002713
2714 tlvs = wpabuf_alloc(2 + 1 + 1 + 1 + os_strlen(query));
2715 if (tlvs == NULL)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002716 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002717 wpabuf_put_le16(tlvs, 1 + 1 + 1 + os_strlen(query));
2718 wpabuf_put_u8(tlvs, P2P_SERV_UPNP); /* Service Protocol Type */
2719 wpabuf_put_u8(tlvs, 1); /* Service Transaction ID */
2720 wpabuf_put_u8(tlvs, version);
2721 wpabuf_put_str(tlvs, query);
2722 ret = wpas_p2p_sd_request(wpa_s, dst, tlvs);
2723 wpabuf_free(tlvs);
2724 return ret;
2725}
2726
2727
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002728#ifdef CONFIG_WIFI_DISPLAY
2729
2730static u64 wpas_p2p_sd_request_wfd(struct wpa_supplicant *wpa_s, const u8 *dst,
2731 const struct wpabuf *tlvs)
2732{
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002733 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
2734 return 0;
2735 return (uintptr_t) p2p_sd_request_wfd(wpa_s->global->p2p, dst, tlvs);
2736}
2737
2738
2739#define MAX_WFD_SD_SUBELEMS 20
2740
2741static void wfd_add_sd_req_role(struct wpabuf *tlvs, u8 id, u8 role,
2742 const char *subelems)
2743{
2744 u8 *len;
2745 const char *pos;
2746 int val;
2747 int count = 0;
2748
2749 len = wpabuf_put(tlvs, 2);
2750 wpabuf_put_u8(tlvs, P2P_SERV_WIFI_DISPLAY); /* Service Protocol Type */
2751 wpabuf_put_u8(tlvs, id); /* Service Transaction ID */
2752
2753 wpabuf_put_u8(tlvs, role);
2754
2755 pos = subelems;
2756 while (*pos) {
2757 val = atoi(pos);
2758 if (val >= 0 && val < 256) {
2759 wpabuf_put_u8(tlvs, val);
2760 count++;
2761 if (count == MAX_WFD_SD_SUBELEMS)
2762 break;
2763 }
2764 pos = os_strchr(pos + 1, ',');
2765 if (pos == NULL)
2766 break;
2767 pos++;
2768 }
2769
2770 WPA_PUT_LE16(len, (u8 *) wpabuf_put(tlvs, 0) - len - 2);
2771}
2772
2773
2774u64 wpas_p2p_sd_request_wifi_display(struct wpa_supplicant *wpa_s,
2775 const u8 *dst, const char *role)
2776{
2777 struct wpabuf *tlvs;
2778 u64 ret;
2779 const char *subelems;
2780 u8 id = 1;
2781
2782 subelems = os_strchr(role, ' ');
2783 if (subelems == NULL)
2784 return 0;
2785 subelems++;
2786
2787 tlvs = wpabuf_alloc(4 * (2 + 1 + 1 + 1 + MAX_WFD_SD_SUBELEMS));
2788 if (tlvs == NULL)
2789 return 0;
2790
2791 if (os_strstr(role, "[source]"))
2792 wfd_add_sd_req_role(tlvs, id++, 0x00, subelems);
2793 if (os_strstr(role, "[pri-sink]"))
2794 wfd_add_sd_req_role(tlvs, id++, 0x01, subelems);
2795 if (os_strstr(role, "[sec-sink]"))
2796 wfd_add_sd_req_role(tlvs, id++, 0x02, subelems);
2797 if (os_strstr(role, "[source+sink]"))
2798 wfd_add_sd_req_role(tlvs, id++, 0x03, subelems);
2799
2800 ret = wpas_p2p_sd_request_wfd(wpa_s, dst, tlvs);
2801 wpabuf_free(tlvs);
2802 return ret;
2803}
2804
2805#endif /* CONFIG_WIFI_DISPLAY */
2806
2807
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002808int wpas_p2p_sd_cancel_request(struct wpa_supplicant *wpa_s, u64 req)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002809{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002810 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
2811 return -1;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002812 return p2p_sd_cancel_request(wpa_s->global->p2p,
2813 (void *) (uintptr_t) req);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002814}
2815
2816
2817void wpas_p2p_sd_response(struct wpa_supplicant *wpa_s, int freq,
2818 const u8 *dst, u8 dialog_token,
2819 const struct wpabuf *resp_tlvs)
2820{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002821 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
2822 return;
2823 p2p_sd_response(wpa_s->global->p2p, freq, dst, dialog_token,
2824 resp_tlvs);
2825}
2826
Dmitry Shmidteaf261d2013-08-14 15:30:08 -07002827
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002828void wpas_p2p_sd_service_update(struct wpa_supplicant *wpa_s)
2829{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002830 if (wpa_s->global->p2p)
2831 p2p_sd_service_update(wpa_s->global->p2p);
2832}
2833
2834
2835static void wpas_p2p_srv_bonjour_free(struct p2p_srv_bonjour *bsrv)
2836{
2837 dl_list_del(&bsrv->list);
2838 wpabuf_free(bsrv->query);
2839 wpabuf_free(bsrv->resp);
2840 os_free(bsrv);
2841}
2842
2843
2844static void wpas_p2p_srv_upnp_free(struct p2p_srv_upnp *usrv)
2845{
2846 dl_list_del(&usrv->list);
2847 os_free(usrv->service);
2848 os_free(usrv);
2849}
2850
2851
2852void wpas_p2p_service_flush(struct wpa_supplicant *wpa_s)
2853{
2854 struct p2p_srv_bonjour *bsrv, *bn;
2855 struct p2p_srv_upnp *usrv, *un;
2856
2857 dl_list_for_each_safe(bsrv, bn, &wpa_s->global->p2p_srv_bonjour,
2858 struct p2p_srv_bonjour, list)
2859 wpas_p2p_srv_bonjour_free(bsrv);
2860
2861 dl_list_for_each_safe(usrv, un, &wpa_s->global->p2p_srv_upnp,
2862 struct p2p_srv_upnp, list)
2863 wpas_p2p_srv_upnp_free(usrv);
2864
2865 wpas_p2p_sd_service_update(wpa_s);
2866}
2867
2868
2869int wpas_p2p_service_add_bonjour(struct wpa_supplicant *wpa_s,
2870 struct wpabuf *query, struct wpabuf *resp)
2871{
2872 struct p2p_srv_bonjour *bsrv;
2873
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002874 bsrv = os_zalloc(sizeof(*bsrv));
2875 if (bsrv == NULL)
2876 return -1;
2877 bsrv->query = query;
2878 bsrv->resp = resp;
2879 dl_list_add(&wpa_s->global->p2p_srv_bonjour, &bsrv->list);
2880
2881 wpas_p2p_sd_service_update(wpa_s);
2882 return 0;
2883}
2884
2885
2886int wpas_p2p_service_del_bonjour(struct wpa_supplicant *wpa_s,
2887 const struct wpabuf *query)
2888{
2889 struct p2p_srv_bonjour *bsrv;
2890
2891 bsrv = wpas_p2p_service_get_bonjour(wpa_s, query);
2892 if (bsrv == NULL)
2893 return -1;
2894 wpas_p2p_srv_bonjour_free(bsrv);
2895 wpas_p2p_sd_service_update(wpa_s);
2896 return 0;
2897}
2898
2899
2900int wpas_p2p_service_add_upnp(struct wpa_supplicant *wpa_s, u8 version,
2901 const char *service)
2902{
2903 struct p2p_srv_upnp *usrv;
2904
2905 if (wpas_p2p_service_get_upnp(wpa_s, version, service))
2906 return 0; /* Already listed */
2907 usrv = os_zalloc(sizeof(*usrv));
2908 if (usrv == NULL)
2909 return -1;
2910 usrv->version = version;
2911 usrv->service = os_strdup(service);
2912 if (usrv->service == NULL) {
2913 os_free(usrv);
2914 return -1;
2915 }
2916 dl_list_add(&wpa_s->global->p2p_srv_upnp, &usrv->list);
2917
2918 wpas_p2p_sd_service_update(wpa_s);
2919 return 0;
2920}
2921
2922
2923int wpas_p2p_service_del_upnp(struct wpa_supplicant *wpa_s, u8 version,
2924 const char *service)
2925{
2926 struct p2p_srv_upnp *usrv;
2927
2928 usrv = wpas_p2p_service_get_upnp(wpa_s, version, service);
2929 if (usrv == NULL)
2930 return -1;
2931 wpas_p2p_srv_upnp_free(usrv);
2932 wpas_p2p_sd_service_update(wpa_s);
2933 return 0;
2934}
2935
2936
2937static void wpas_prov_disc_local_display(struct wpa_supplicant *wpa_s,
2938 const u8 *peer, const char *params,
2939 unsigned int generated_pin)
2940{
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07002941 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_SHOW_PIN MACSTR
2942 " %08d%s", MAC2STR(peer), generated_pin, params);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002943}
2944
2945
2946static void wpas_prov_disc_local_keypad(struct wpa_supplicant *wpa_s,
2947 const u8 *peer, const char *params)
2948{
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07002949 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_ENTER_PIN MACSTR
2950 "%s", MAC2STR(peer), params);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002951}
2952
2953
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002954static void wpas_prov_disc_req(void *ctx, const u8 *peer, u16 config_methods,
2955 const u8 *dev_addr, const u8 *pri_dev_type,
2956 const char *dev_name, u16 supp_config_methods,
2957 u8 dev_capab, u8 group_capab, const u8 *group_id,
2958 size_t group_id_len)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002959{
2960 struct wpa_supplicant *wpa_s = ctx;
2961 char devtype[WPS_DEV_TYPE_BUFSIZE];
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002962 char params[300];
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002963 u8 empty_dev_type[8];
2964 unsigned int generated_pin = 0;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002965 struct wpa_supplicant *group = NULL;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002966 int res;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002967
2968 if (group_id) {
2969 for (group = wpa_s->global->ifaces; group; group = group->next)
2970 {
2971 struct wpa_ssid *s = group->current_ssid;
2972 if (s != NULL &&
2973 s->mode == WPAS_MODE_P2P_GO &&
2974 group_id_len - ETH_ALEN == s->ssid_len &&
2975 os_memcmp(group_id + ETH_ALEN, s->ssid,
2976 s->ssid_len) == 0)
2977 break;
2978 }
2979 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002980
2981 if (pri_dev_type == NULL) {
2982 os_memset(empty_dev_type, 0, sizeof(empty_dev_type));
2983 pri_dev_type = empty_dev_type;
2984 }
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002985 res = os_snprintf(params, sizeof(params), " p2p_dev_addr=" MACSTR
2986 " pri_dev_type=%s name='%s' config_methods=0x%x "
2987 "dev_capab=0x%x group_capab=0x%x%s%s",
2988 MAC2STR(dev_addr),
2989 wps_dev_type_bin2str(pri_dev_type, devtype,
2990 sizeof(devtype)),
2991 dev_name, supp_config_methods, dev_capab, group_capab,
2992 group ? " group=" : "",
2993 group ? group->ifname : "");
2994 if (os_snprintf_error(sizeof(params), res))
2995 wpa_printf(MSG_DEBUG, "P2P: PD Request event truncated");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002996 params[sizeof(params) - 1] = '\0';
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08002997
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002998 if (config_methods & WPS_CONFIG_DISPLAY) {
2999 generated_pin = wps_generate_pin();
3000 wpas_prov_disc_local_display(wpa_s, peer, params,
3001 generated_pin);
3002 } else if (config_methods & WPS_CONFIG_KEYPAD)
3003 wpas_prov_disc_local_keypad(wpa_s, peer, params);
3004 else if (config_methods & WPS_CONFIG_PUSHBUTTON)
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07003005 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_PBC_REQ
3006 MACSTR "%s", MAC2STR(peer), params);
Jouni Malinen75ecf522011-06-27 15:19:46 -07003007
3008 wpas_notify_p2p_provision_discovery(wpa_s, peer, 1 /* request */,
3009 P2P_PROV_DISC_SUCCESS,
3010 config_methods, generated_pin);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003011}
3012
3013
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08003014static void wpas_prov_disc_resp(void *ctx, const u8 *peer, u16 config_methods)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003015{
3016 struct wpa_supplicant *wpa_s = ctx;
3017 unsigned int generated_pin = 0;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003018 char params[20];
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003019
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08003020 if (wpa_s->pending_pd_before_join &&
3021 (os_memcmp(peer, wpa_s->pending_join_dev_addr, ETH_ALEN) == 0 ||
3022 os_memcmp(peer, wpa_s->pending_join_iface_addr, ETH_ALEN) == 0)) {
3023 wpa_s->pending_pd_before_join = 0;
3024 wpa_printf(MSG_DEBUG, "P2P: Starting pending "
3025 "join-existing-group operation");
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08003026 wpas_p2p_join_start(wpa_s, 0, NULL, 0);
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08003027 return;
3028 }
3029
Dmitry Shmidt04949592012-07-19 12:16:46 -07003030 if (wpa_s->pending_pd_use == AUTO_PD_JOIN ||
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003031 wpa_s->pending_pd_use == AUTO_PD_GO_NEG) {
3032 int res;
3033
3034 res = os_snprintf(params, sizeof(params), " peer_go=%d",
3035 wpa_s->pending_pd_use == AUTO_PD_JOIN);
3036 if (os_snprintf_error(sizeof(params), res))
3037 params[sizeof(params) - 1] = '\0';
3038 } else
Dmitry Shmidt04949592012-07-19 12:16:46 -07003039 params[0] = '\0';
3040
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003041 if (config_methods & WPS_CONFIG_DISPLAY)
Dmitry Shmidt04949592012-07-19 12:16:46 -07003042 wpas_prov_disc_local_keypad(wpa_s, peer, params);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003043 else if (config_methods & WPS_CONFIG_KEYPAD) {
3044 generated_pin = wps_generate_pin();
Dmitry Shmidt04949592012-07-19 12:16:46 -07003045 wpas_prov_disc_local_display(wpa_s, peer, params,
3046 generated_pin);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003047 } else if (config_methods & WPS_CONFIG_PUSHBUTTON)
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07003048 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_PBC_RESP
3049 MACSTR "%s", MAC2STR(peer), params);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003050
Jouni Malinen75ecf522011-06-27 15:19:46 -07003051 wpas_notify_p2p_provision_discovery(wpa_s, peer, 0 /* response */,
3052 P2P_PROV_DISC_SUCCESS,
3053 config_methods, generated_pin);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003054}
3055
3056
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003057static void wpas_prov_disc_fail(void *ctx, const u8 *peer,
3058 enum p2p_prov_disc_status status)
Jouni Malinen75ecf522011-06-27 15:19:46 -07003059{
3060 struct wpa_supplicant *wpa_s = ctx;
3061
Dmitry Shmidt04949592012-07-19 12:16:46 -07003062 if (wpa_s->p2p_fallback_to_go_neg) {
3063 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: PD for p2p_connect-auto "
3064 "failed - fall back to GO Negotiation");
3065 wpas_p2p_fallback_to_go_neg(wpa_s, 0);
3066 return;
3067 }
3068
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08003069 if (status == P2P_PROV_DISC_TIMEOUT_JOIN) {
Dmitry Shmidt2b380482012-09-13 10:52:13 -07003070 wpa_s->pending_pd_before_join = 0;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08003071 wpa_printf(MSG_DEBUG, "P2P: Starting pending "
3072 "join-existing-group operation (no ACK for PD "
3073 "Req attempts)");
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08003074 wpas_p2p_join_start(wpa_s, 0, NULL, 0);
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08003075 return;
Dmitry Shmidt2b380482012-09-13 10:52:13 -07003076 }
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08003077
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07003078 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_FAILURE
3079 " p2p_dev_addr=" MACSTR " status=%d",
3080 MAC2STR(peer), status);
Dmitry Shmidt04949592012-07-19 12:16:46 -07003081
Jouni Malinen75ecf522011-06-27 15:19:46 -07003082 wpas_notify_p2p_provision_discovery(wpa_s, peer, 0 /* response */,
3083 status, 0, 0);
3084}
3085
3086
Dmitry Shmidtf7e0a992013-05-23 11:03:10 -07003087static int freq_included(const struct p2p_channels *channels, unsigned int freq)
3088{
3089 if (channels == NULL)
3090 return 1; /* Assume no restrictions */
3091 return p2p_channels_includes_freq(channels, freq);
3092
3093}
3094
3095
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07003096/**
3097 * Pick the best frequency to use from all the currently used frequencies.
3098 */
3099static int wpas_p2p_pick_best_used_freq(struct wpa_supplicant *wpa_s,
3100 struct wpa_used_freq_data *freqs,
3101 unsigned int num)
3102{
3103 unsigned int i, c;
3104
3105 /* find a candidate freq that is supported by P2P */
3106 for (c = 0; c < num; c++)
3107 if (p2p_supported_freq(wpa_s->global->p2p, freqs[c].freq))
3108 break;
3109
3110 if (c == num)
3111 return 0;
3112
3113 /* once we have a candidate, try to find a 'better' one */
3114 for (i = c + 1; i < num; i++) {
3115 if (!p2p_supported_freq(wpa_s->global->p2p, freqs[i].freq))
3116 continue;
3117
3118 /*
3119 * 1. Infrastructure station interfaces have higher preference.
3120 * 2. P2P Clients have higher preference.
3121 * 3. All others.
3122 */
3123 if (freqs[i].flags & WPA_FREQ_USED_BY_INFRA_STATION) {
3124 c = i;
3125 break;
3126 }
3127
3128 if ((freqs[i].flags & WPA_FREQ_USED_BY_P2P_CLIENT))
3129 c = i;
3130 }
3131 return freqs[c].freq;
3132}
3133
3134
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003135static u8 wpas_invitation_process(void *ctx, const u8 *sa, const u8 *bssid,
3136 const u8 *go_dev_addr, const u8 *ssid,
3137 size_t ssid_len, int *go, u8 *group_bssid,
Dmitry Shmidtf7e0a992013-05-23 11:03:10 -07003138 int *force_freq, int persistent_group,
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08003139 const struct p2p_channels *channels,
3140 int dev_pw_id)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003141{
3142 struct wpa_supplicant *wpa_s = ctx;
3143 struct wpa_ssid *s;
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07003144 struct wpa_used_freq_data *freqs;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003145 struct wpa_supplicant *grp;
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07003146 int best_freq;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003147
3148 if (!persistent_group) {
3149 wpa_printf(MSG_DEBUG, "P2P: Invitation from " MACSTR
Dmitry Shmidt344abd32014-01-14 13:17:00 -08003150 " to join an active group (SSID: %s)",
3151 MAC2STR(sa), wpa_ssid_txt(ssid, ssid_len));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003152 if (!is_zero_ether_addr(wpa_s->p2p_auth_invite) &&
3153 (os_memcmp(go_dev_addr, wpa_s->p2p_auth_invite, ETH_ALEN)
3154 == 0 ||
3155 os_memcmp(sa, wpa_s->p2p_auth_invite, ETH_ALEN) == 0)) {
3156 wpa_printf(MSG_DEBUG, "P2P: Accept previously "
3157 "authorized invitation");
3158 goto accept_inv;
3159 }
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08003160
3161#ifdef CONFIG_WPS_NFC
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08003162 if (dev_pw_id >= 0 && wpa_s->p2p_nfc_tag_enabled &&
3163 dev_pw_id == wpa_s->p2p_oob_dev_pw_id) {
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08003164 wpa_printf(MSG_DEBUG, "P2P: Accept invitation based on local enabled NFC Tag");
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08003165 wpa_s->p2p_wps_method = WPS_NFC;
3166 wpa_s->pending_join_wps_method = WPS_NFC;
3167 os_memcpy(wpa_s->pending_join_dev_addr,
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08003168 go_dev_addr, ETH_ALEN);
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08003169 os_memcpy(wpa_s->pending_join_iface_addr,
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08003170 bssid, ETH_ALEN);
3171 goto accept_inv;
3172 }
3173#endif /* CONFIG_WPS_NFC */
3174
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003175 /*
3176 * Do not accept the invitation automatically; notify user and
3177 * request approval.
3178 */
3179 return P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE;
3180 }
3181
3182 grp = wpas_get_p2p_group(wpa_s, ssid, ssid_len, go);
3183 if (grp) {
3184 wpa_printf(MSG_DEBUG, "P2P: Accept invitation to already "
3185 "running persistent group");
3186 if (*go)
3187 os_memcpy(group_bssid, grp->own_addr, ETH_ALEN);
3188 goto accept_inv;
3189 }
3190
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08003191 if (!is_zero_ether_addr(wpa_s->p2p_auth_invite) &&
3192 os_memcmp(sa, wpa_s->p2p_auth_invite, ETH_ALEN) == 0) {
3193 wpa_printf(MSG_DEBUG, "P2P: Accept previously initiated "
3194 "invitation to re-invoke a persistent group");
Dmitry Shmidt76cd2cc2014-05-27 12:56:04 -07003195 os_memset(wpa_s->p2p_auth_invite, 0, ETH_ALEN);
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08003196 } else if (!wpa_s->conf->persistent_reconnect)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003197 return P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE;
3198
3199 for (s = wpa_s->conf->ssid; s; s = s->next) {
3200 if (s->disabled == 2 &&
3201 os_memcmp(s->bssid, go_dev_addr, ETH_ALEN) == 0 &&
3202 s->ssid_len == ssid_len &&
3203 os_memcmp(ssid, s->ssid, ssid_len) == 0)
3204 break;
3205 }
3206
3207 if (!s) {
3208 wpa_printf(MSG_DEBUG, "P2P: Invitation from " MACSTR
3209 " requested reinvocation of an unknown group",
3210 MAC2STR(sa));
3211 return P2P_SC_FAIL_UNKNOWN_GROUP;
3212 }
3213
3214 if (s->mode == WPAS_MODE_P2P_GO && !wpas_p2p_create_iface(wpa_s)) {
3215 *go = 1;
3216 if (wpa_s->wpa_state >= WPA_AUTHENTICATING) {
3217 wpa_printf(MSG_DEBUG, "P2P: The only available "
3218 "interface is already in use - reject "
3219 "invitation");
3220 return P2P_SC_FAIL_UNABLE_TO_ACCOMMODATE;
3221 }
3222 os_memcpy(group_bssid, wpa_s->own_addr, ETH_ALEN);
3223 } else if (s->mode == WPAS_MODE_P2P_GO) {
3224 *go = 1;
3225 if (wpas_p2p_add_group_interface(wpa_s, WPA_IF_P2P_GO) < 0)
3226 {
3227 wpa_printf(MSG_ERROR, "P2P: Failed to allocate a new "
3228 "interface address for the group");
3229 return P2P_SC_FAIL_UNABLE_TO_ACCOMMODATE;
3230 }
3231 os_memcpy(group_bssid, wpa_s->pending_interface_addr,
3232 ETH_ALEN);
3233 }
3234
3235accept_inv:
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003236 wpas_p2p_set_own_freq_preference(wpa_s, 0);
3237
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07003238 best_freq = 0;
3239 freqs = os_calloc(wpa_s->num_multichan_concurrent,
3240 sizeof(struct wpa_used_freq_data));
3241 if (freqs) {
3242 int num_channels = wpa_s->num_multichan_concurrent;
3243 int num = wpas_p2p_valid_oper_freqs(wpa_s, freqs, num_channels);
3244 best_freq = wpas_p2p_pick_best_used_freq(wpa_s, freqs, num);
3245 os_free(freqs);
3246 }
3247
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07003248 /* Get one of the frequencies currently in use */
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07003249 if (best_freq > 0) {
Dmitry Shmidta0d265f2013-11-19 13:13:41 -08003250 wpa_printf(MSG_DEBUG, "P2P: Trying to prefer a channel already used by one of the interfaces");
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07003251 wpas_p2p_set_own_freq_preference(wpa_s, best_freq);
Dmitry Shmidta0d265f2013-11-19 13:13:41 -08003252
3253 if (wpa_s->num_multichan_concurrent < 2 ||
3254 wpas_p2p_num_unused_channels(wpa_s) < 1) {
3255 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 -07003256 *force_freq = best_freq;
Dmitry Shmidta0d265f2013-11-19 13:13:41 -08003257 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003258 }
3259
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07003260 if (*force_freq > 0 && wpa_s->num_multichan_concurrent > 1 &&
3261 wpas_p2p_num_unused_channels(wpa_s) > 0) {
Dmitry Shmidtf7e0a992013-05-23 11:03:10 -07003262 if (*go == 0) {
3263 /* We are the client */
3264 wpa_printf(MSG_DEBUG, "P2P: Peer was found to be "
3265 "running a GO but we are capable of MCC, "
3266 "figure out the best channel to use");
3267 *force_freq = 0;
3268 } else if (!freq_included(channels, *force_freq)) {
3269 /* We are the GO, and *force_freq is not in the
3270 * intersection */
3271 wpa_printf(MSG_DEBUG, "P2P: Forced GO freq %d MHz not "
3272 "in intersection but we are capable of MCC, "
3273 "figure out the best channel to use",
3274 *force_freq);
3275 *force_freq = 0;
3276 }
3277 }
3278
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003279 return P2P_SC_SUCCESS;
3280}
3281
3282
3283static void wpas_invitation_received(void *ctx, const u8 *sa, const u8 *bssid,
3284 const u8 *ssid, size_t ssid_len,
3285 const u8 *go_dev_addr, u8 status,
3286 int op_freq)
3287{
3288 struct wpa_supplicant *wpa_s = ctx;
3289 struct wpa_ssid *s;
3290
3291 for (s = wpa_s->conf->ssid; s; s = s->next) {
3292 if (s->disabled == 2 &&
3293 s->ssid_len == ssid_len &&
3294 os_memcmp(ssid, s->ssid, ssid_len) == 0)
3295 break;
3296 }
3297
3298 if (status == P2P_SC_SUCCESS) {
3299 wpa_printf(MSG_DEBUG, "P2P: Invitation from peer " MACSTR
Dmitry Shmidt344abd32014-01-14 13:17:00 -08003300 " was accepted; op_freq=%d MHz, SSID=%s",
3301 MAC2STR(sa), op_freq, wpa_ssid_txt(ssid, ssid_len));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003302 if (s) {
Dmitry Shmidt91c40cd2012-09-25 14:23:53 -07003303 int go = s->mode == WPAS_MODE_P2P_GO;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003304 wpas_p2p_group_add_persistent(
Dmitry Shmidt15907092014-03-25 10:42:57 -07003305 wpa_s, s, go, 0, op_freq, 0, 0, NULL,
Dmitry Shmidt56052862013-10-04 10:23:25 -07003306 go ? P2P_MAX_INITIAL_CONN_WAIT_GO_REINVOKE : 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003307 } else if (bssid) {
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08003308 wpa_s->user_initiated_pd = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003309 wpas_p2p_join(wpa_s, bssid, go_dev_addr,
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08003310 wpa_s->p2p_wps_method, 0, op_freq,
Dmitry Shmidt344abd32014-01-14 13:17:00 -08003311 ssid, ssid_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003312 }
3313 return;
3314 }
3315
3316 if (status != P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE) {
3317 wpa_printf(MSG_DEBUG, "P2P: Invitation from peer " MACSTR
3318 " was rejected (status %u)", MAC2STR(sa), status);
3319 return;
3320 }
3321
3322 if (!s) {
3323 if (bssid) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07003324 wpa_msg_global(wpa_s, MSG_INFO,
3325 P2P_EVENT_INVITATION_RECEIVED
3326 "sa=" MACSTR " go_dev_addr=" MACSTR
3327 " bssid=" MACSTR " unknown-network",
3328 MAC2STR(sa), MAC2STR(go_dev_addr),
3329 MAC2STR(bssid));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003330 } else {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07003331 wpa_msg_global(wpa_s, MSG_INFO,
3332 P2P_EVENT_INVITATION_RECEIVED
3333 "sa=" MACSTR " go_dev_addr=" MACSTR
3334 " unknown-network",
3335 MAC2STR(sa), MAC2STR(go_dev_addr));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003336 }
3337 return;
3338 }
3339
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08003340 if (s->mode == WPAS_MODE_P2P_GO && op_freq) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07003341 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_INVITATION_RECEIVED
3342 "sa=" MACSTR " persistent=%d freq=%d",
3343 MAC2STR(sa), s->id, op_freq);
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08003344 } else {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07003345 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_INVITATION_RECEIVED
3346 "sa=" MACSTR " persistent=%d",
3347 MAC2STR(sa), s->id);
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08003348 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003349}
3350
3351
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003352static void wpas_remove_persistent_peer(struct wpa_supplicant *wpa_s,
3353 struct wpa_ssid *ssid,
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07003354 const u8 *peer, int inv)
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003355{
3356 size_t i;
3357
3358 if (ssid == NULL)
3359 return;
3360
3361 for (i = 0; ssid->p2p_client_list && i < ssid->num_p2p_clients; i++) {
Dmitry Shmidtff787d52015-01-12 13:01:47 -08003362 if (os_memcmp(ssid->p2p_client_list + i * 2 * ETH_ALEN, peer,
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003363 ETH_ALEN) == 0)
3364 break;
3365 }
Dmitry Shmidt6aa8ae42014-07-07 09:31:33 -07003366 if (i >= ssid->num_p2p_clients || !ssid->p2p_client_list) {
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003367 if (ssid->mode != WPAS_MODE_P2P_GO &&
3368 os_memcmp(ssid->bssid, peer, ETH_ALEN) == 0) {
3369 wpa_printf(MSG_DEBUG, "P2P: Remove persistent group %d "
3370 "due to invitation result", ssid->id);
3371 wpas_notify_network_removed(wpa_s, ssid);
3372 wpa_config_remove_network(wpa_s->conf, ssid->id);
3373 return;
3374 }
3375 return; /* Peer not found in client list */
3376 }
3377
3378 wpa_printf(MSG_DEBUG, "P2P: Remove peer " MACSTR " from persistent "
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07003379 "group %d client list%s",
3380 MAC2STR(peer), ssid->id,
3381 inv ? " due to invitation result" : "");
Dmitry Shmidtff787d52015-01-12 13:01:47 -08003382 os_memmove(ssid->p2p_client_list + i * 2 * ETH_ALEN,
3383 ssid->p2p_client_list + (i + 1) * 2 * ETH_ALEN,
3384 (ssid->num_p2p_clients - i - 1) * 2 * ETH_ALEN);
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003385 ssid->num_p2p_clients--;
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003386 if (wpa_s->parent->conf->update_config &&
3387 wpa_config_write(wpa_s->parent->confname, wpa_s->parent->conf))
3388 wpa_printf(MSG_DEBUG, "P2P: Failed to update configuration");
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003389}
3390
3391
3392static void wpas_remove_persistent_client(struct wpa_supplicant *wpa_s,
3393 const u8 *peer)
3394{
3395 struct wpa_ssid *ssid;
3396
3397 wpa_s = wpa_s->global->p2p_invite_group;
3398 if (wpa_s == NULL)
3399 return; /* No known invitation group */
3400 ssid = wpa_s->current_ssid;
3401 if (ssid == NULL || ssid->mode != WPAS_MODE_P2P_GO ||
3402 !ssid->p2p_persistent_group)
3403 return; /* Not operating as a GO in persistent group */
3404 ssid = wpas_p2p_get_persistent(wpa_s->parent, peer,
3405 ssid->ssid, ssid->ssid_len);
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07003406 wpas_remove_persistent_peer(wpa_s, ssid, peer, 1);
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003407}
3408
3409
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08003410static void wpas_invitation_result(void *ctx, int status, const u8 *bssid,
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003411 const struct p2p_channels *channels,
Dmitry Shmidt15907092014-03-25 10:42:57 -07003412 const u8 *peer, int neg_freq,
3413 int peer_oper_freq)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003414{
3415 struct wpa_supplicant *wpa_s = ctx;
3416 struct wpa_ssid *ssid;
Dmitry Shmidt15907092014-03-25 10:42:57 -07003417 int freq;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003418
3419 if (bssid) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07003420 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_INVITATION_RESULT
3421 "status=%d " MACSTR,
3422 status, MAC2STR(bssid));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003423 } else {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07003424 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_INVITATION_RESULT
3425 "status=%d ", status);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003426 }
3427 wpas_notify_p2p_invitation_result(wpa_s, status, bssid);
3428
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003429 wpa_printf(MSG_DEBUG, "P2P: Invitation result - status=%d peer=" MACSTR,
3430 status, MAC2STR(peer));
3431 if (wpa_s->pending_invite_ssid_id == -1) {
3432 if (status == P2P_SC_FAIL_UNKNOWN_GROUP)
3433 wpas_remove_persistent_client(wpa_s, peer);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003434 return; /* Invitation to active group */
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003435 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003436
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08003437 if (status == P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE) {
3438 wpa_printf(MSG_DEBUG, "P2P: Waiting for peer to start another "
3439 "invitation exchange to indicate readiness for "
3440 "re-invocation");
3441 }
3442
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003443 if (status != P2P_SC_SUCCESS) {
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003444 if (status == P2P_SC_FAIL_UNKNOWN_GROUP) {
3445 ssid = wpa_config_get_network(
3446 wpa_s->conf, wpa_s->pending_invite_ssid_id);
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07003447 wpas_remove_persistent_peer(wpa_s, ssid, peer, 1);
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003448 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003449 wpas_p2p_remove_pending_group_interface(wpa_s);
3450 return;
3451 }
3452
3453 ssid = wpa_config_get_network(wpa_s->conf,
3454 wpa_s->pending_invite_ssid_id);
3455 if (ssid == NULL) {
3456 wpa_printf(MSG_ERROR, "P2P: Could not find persistent group "
3457 "data matching with invitation");
3458 return;
3459 }
3460
Jouni Malinen2c5b17d2012-10-13 21:52:46 -07003461 /*
3462 * The peer could have missed our ctrl::ack frame for Invitation
3463 * Response and continue retransmitting the frame. To reduce the
3464 * likelihood of the peer not getting successful TX status for the
3465 * Invitation Response frame, wait a short time here before starting
3466 * the persistent group so that we will remain on the current channel to
3467 * acknowledge any possible retransmission from the peer.
3468 */
3469 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: 50 ms wait on current channel before "
3470 "starting persistent group");
3471 os_sleep(0, 50000);
3472
Dmitry Shmidt15907092014-03-25 10:42:57 -07003473 if (neg_freq > 0 && ssid->mode == WPAS_MODE_P2P_GO &&
3474 freq_included(channels, neg_freq))
3475 freq = neg_freq;
3476 else if (peer_oper_freq > 0 && ssid->mode != WPAS_MODE_P2P_GO &&
3477 freq_included(channels, peer_oper_freq))
3478 freq = peer_oper_freq;
3479 else
3480 freq = 0;
3481
3482 wpa_printf(MSG_DEBUG, "P2P: Persistent group invitation success - op_freq=%d MHz SSID=%s",
3483 freq, wpa_ssid_txt(ssid->ssid, ssid->ssid_len));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003484 wpas_p2p_group_add_persistent(wpa_s, ssid,
Jouni Malinen31be0a42012-08-31 21:20:51 +03003485 ssid->mode == WPAS_MODE_P2P_GO,
Dmitry Shmidt96be6222014-02-13 10:16:51 -08003486 wpa_s->p2p_persistent_go_freq,
Dmitry Shmidt15907092014-03-25 10:42:57 -07003487 freq,
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003488 wpa_s->p2p_go_ht40, wpa_s->p2p_go_vht,
3489 channels,
Dmitry Shmidt56052862013-10-04 10:23:25 -07003490 ssid->mode == WPAS_MODE_P2P_GO ?
3491 P2P_MAX_INITIAL_CONN_WAIT_GO_REINVOKE :
3492 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003493}
3494
3495
Dmitry Shmidt04949592012-07-19 12:16:46 -07003496static int wpas_p2p_disallowed_freq(struct wpa_global *global,
3497 unsigned int freq)
3498{
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08003499 if (freq_range_list_includes(&global->p2p_go_avoid_freq, freq))
3500 return 1;
Dmitry Shmidt4ce9c872013-10-24 11:08:13 -07003501 return freq_range_list_includes(&global->p2p_disallow_freq, freq);
Dmitry Shmidt04949592012-07-19 12:16:46 -07003502}
3503
3504
3505static void wpas_p2p_add_chan(struct p2p_reg_class *reg, u8 chan)
3506{
3507 reg->channel[reg->channels] = chan;
3508 reg->channels++;
3509}
3510
3511
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003512static int wpas_p2p_default_channels(struct wpa_supplicant *wpa_s,
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003513 struct p2p_channels *chan,
3514 struct p2p_channels *cli_chan)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003515{
3516 int i, cla = 0;
3517
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003518 wpa_s->global->p2p_24ghz_social_channels = 1;
3519
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003520 os_memset(cli_chan, 0, sizeof(*cli_chan));
3521
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003522 wpa_printf(MSG_DEBUG, "P2P: Enable operating classes for 2.4 GHz "
3523 "band");
3524
3525 /* Operating class 81 - 2.4 GHz band channels 1..13 */
3526 chan->reg_class[cla].reg_class = 81;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003527 chan->reg_class[cla].channels = 0;
3528 for (i = 0; i < 11; i++) {
3529 if (!wpas_p2p_disallowed_freq(wpa_s->global, 2412 + i * 5))
3530 wpas_p2p_add_chan(&chan->reg_class[cla], i + 1);
3531 }
3532 if (chan->reg_class[cla].channels)
3533 cla++;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003534
3535 wpa_printf(MSG_DEBUG, "P2P: Enable operating classes for lower 5 GHz "
3536 "band");
3537
3538 /* Operating class 115 - 5 GHz, channels 36-48 */
3539 chan->reg_class[cla].reg_class = 115;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003540 chan->reg_class[cla].channels = 0;
3541 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 36 * 5))
3542 wpas_p2p_add_chan(&chan->reg_class[cla], 36);
3543 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 40 * 5))
3544 wpas_p2p_add_chan(&chan->reg_class[cla], 40);
3545 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 44 * 5))
3546 wpas_p2p_add_chan(&chan->reg_class[cla], 44);
3547 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 48 * 5))
3548 wpas_p2p_add_chan(&chan->reg_class[cla], 48);
3549 if (chan->reg_class[cla].channels)
3550 cla++;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003551
3552 wpa_printf(MSG_DEBUG, "P2P: Enable operating classes for higher 5 GHz "
3553 "band");
3554
3555 /* Operating class 124 - 5 GHz, channels 149,153,157,161 */
3556 chan->reg_class[cla].reg_class = 124;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003557 chan->reg_class[cla].channels = 0;
3558 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 149 * 5))
3559 wpas_p2p_add_chan(&chan->reg_class[cla], 149);
3560 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 153 * 5))
3561 wpas_p2p_add_chan(&chan->reg_class[cla], 153);
3562 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 156 * 5))
3563 wpas_p2p_add_chan(&chan->reg_class[cla], 157);
3564 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 161 * 5))
3565 wpas_p2p_add_chan(&chan->reg_class[cla], 161);
3566 if (chan->reg_class[cla].channels)
3567 cla++;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003568
3569 chan->reg_classes = cla;
3570 return 0;
3571}
3572
3573
3574static struct hostapd_hw_modes * get_mode(struct hostapd_hw_modes *modes,
3575 u16 num_modes,
3576 enum hostapd_hw_mode mode)
3577{
3578 u16 i;
3579
3580 for (i = 0; i < num_modes; i++) {
3581 if (modes[i].mode == mode)
3582 return &modes[i];
3583 }
3584
3585 return NULL;
3586}
3587
3588
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003589enum chan_allowed {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003590 NOT_ALLOWED, NO_IR, ALLOWED
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003591};
3592
Dmitry Shmidt04949592012-07-19 12:16:46 -07003593static int has_channel(struct wpa_global *global,
3594 struct hostapd_hw_modes *mode, u8 chan, int *flags)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003595{
3596 int i;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003597 unsigned int freq;
3598
3599 freq = (mode->mode == HOSTAPD_MODE_IEEE80211A ? 5000 : 2407) +
3600 chan * 5;
3601 if (wpas_p2p_disallowed_freq(global, freq))
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003602 return NOT_ALLOWED;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003603
3604 for (i = 0; i < mode->num_channels; i++) {
3605 if (mode->channels[i].chan == chan) {
3606 if (flags)
3607 *flags = mode->channels[i].flag;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003608 if (mode->channels[i].flag &
3609 (HOSTAPD_CHAN_DISABLED |
3610 HOSTAPD_CHAN_RADAR))
3611 return NOT_ALLOWED;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003612 if (mode->channels[i].flag & HOSTAPD_CHAN_NO_IR)
3613 return NO_IR;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003614 return ALLOWED;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003615 }
3616 }
3617
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003618 return NOT_ALLOWED;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003619}
3620
3621
3622struct p2p_oper_class_map {
3623 enum hostapd_hw_mode mode;
3624 u8 op_class;
3625 u8 min_chan;
3626 u8 max_chan;
3627 u8 inc;
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07003628 enum { BW20, BW40PLUS, BW40MINUS, BW80, BW2160 } bw;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003629};
3630
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003631static struct p2p_oper_class_map op_class[] = {
3632 { HOSTAPD_MODE_IEEE80211G, 81, 1, 13, 1, BW20 },
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003633#if 0 /* Do not enable HT40 on 2 GHz for now */
3634 { HOSTAPD_MODE_IEEE80211G, 83, 1, 9, 1, BW40PLUS },
3635 { HOSTAPD_MODE_IEEE80211G, 84, 5, 13, 1, BW40MINUS },
3636#endif
3637 { HOSTAPD_MODE_IEEE80211A, 115, 36, 48, 4, BW20 },
3638 { HOSTAPD_MODE_IEEE80211A, 124, 149, 161, 4, BW20 },
3639 { HOSTAPD_MODE_IEEE80211A, 116, 36, 44, 8, BW40PLUS },
3640 { HOSTAPD_MODE_IEEE80211A, 117, 40, 48, 8, BW40MINUS },
3641 { HOSTAPD_MODE_IEEE80211A, 126, 149, 157, 8, BW40PLUS },
3642 { HOSTAPD_MODE_IEEE80211A, 127, 153, 161, 8, BW40MINUS },
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003643
3644 /*
3645 * IEEE P802.11ac/D7.0 Table E-4 actually talks about channel center
3646 * frequency index 42, 58, 106, 122, 138, 155 with channel spacing of
3647 * 80 MHz, but currently use the following definition for simplicity
3648 * (these center frequencies are not actual channels, which makes
3649 * has_channel() fail). wpas_p2p_verify_80mhz() should take care of
3650 * removing invalid channels.
3651 */
3652 { HOSTAPD_MODE_IEEE80211A, 128, 36, 161, 4, BW80 },
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07003653 { HOSTAPD_MODE_IEEE80211AD, 180, 1, 4, 1, BW2160 },
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003654 { -1, 0, 0, 0, 0, BW20 }
3655};
3656
3657
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003658static int wpas_p2p_get_center_80mhz(struct wpa_supplicant *wpa_s,
3659 struct hostapd_hw_modes *mode,
3660 u8 channel)
3661{
3662 u8 center_channels[] = { 42, 58, 106, 122, 138, 155 };
3663 unsigned int i;
3664
3665 if (mode->mode != HOSTAPD_MODE_IEEE80211A)
3666 return 0;
3667
3668 for (i = 0; i < ARRAY_SIZE(center_channels); i++)
3669 /*
3670 * In 80 MHz, the bandwidth "spans" 12 channels (e.g., 36-48),
3671 * so the center channel is 6 channels away from the start/end.
3672 */
3673 if (channel >= center_channels[i] - 6 &&
3674 channel <= center_channels[i] + 6)
3675 return center_channels[i];
3676
3677 return 0;
3678}
3679
3680
3681static enum chan_allowed wpas_p2p_verify_80mhz(struct wpa_supplicant *wpa_s,
3682 struct hostapd_hw_modes *mode,
3683 u8 channel, u8 bw)
3684{
3685 u8 center_chan;
3686 int i, flags;
3687 enum chan_allowed res, ret = ALLOWED;
3688
3689 center_chan = wpas_p2p_get_center_80mhz(wpa_s, mode, channel);
3690 if (!center_chan)
3691 return NOT_ALLOWED;
3692 if (center_chan >= 58 && center_chan <= 138)
3693 return NOT_ALLOWED; /* Do not allow DFS channels for P2P */
3694
3695 /* check all the channels are available */
3696 for (i = 0; i < 4; i++) {
3697 int adj_chan = center_chan - 6 + i * 4;
3698
3699 res = has_channel(wpa_s->global, mode, adj_chan, &flags);
3700 if (res == NOT_ALLOWED)
3701 return NOT_ALLOWED;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003702 if (res == NO_IR)
3703 ret = NO_IR;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003704
3705 if (i == 0 && !(flags & HOSTAPD_CHAN_VHT_10_70))
3706 return NOT_ALLOWED;
3707 if (i == 1 && !(flags & HOSTAPD_CHAN_VHT_30_50))
3708 return NOT_ALLOWED;
3709 if (i == 2 && !(flags & HOSTAPD_CHAN_VHT_50_30))
3710 return NOT_ALLOWED;
3711 if (i == 3 && !(flags & HOSTAPD_CHAN_VHT_70_10))
3712 return NOT_ALLOWED;
3713 }
3714
3715 return ret;
3716}
3717
3718
3719static enum chan_allowed wpas_p2p_verify_channel(struct wpa_supplicant *wpa_s,
3720 struct hostapd_hw_modes *mode,
3721 u8 channel, u8 bw)
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003722{
Dmitry Shmidt96be6222014-02-13 10:16:51 -08003723 int flag = 0;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003724 enum chan_allowed res, res2;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003725
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003726 res2 = res = has_channel(wpa_s->global, mode, channel, &flag);
3727 if (bw == BW40MINUS) {
3728 if (!(flag & HOSTAPD_CHAN_HT40MINUS))
3729 return NOT_ALLOWED;
3730 res2 = has_channel(wpa_s->global, mode, channel - 4, NULL);
3731 } else if (bw == BW40PLUS) {
3732 if (!(flag & HOSTAPD_CHAN_HT40PLUS))
3733 return NOT_ALLOWED;
3734 res2 = has_channel(wpa_s->global, mode, channel + 4, NULL);
3735 } else if (bw == BW80) {
3736 res2 = wpas_p2p_verify_80mhz(wpa_s, mode, channel, bw);
3737 }
3738
3739 if (res == NOT_ALLOWED || res2 == NOT_ALLOWED)
3740 return NOT_ALLOWED;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003741 if (res == NO_IR || res2 == NO_IR)
3742 return NO_IR;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003743 return res;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003744}
3745
3746
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003747static int wpas_p2p_setup_channels(struct wpa_supplicant *wpa_s,
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003748 struct p2p_channels *chan,
3749 struct p2p_channels *cli_chan)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003750{
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003751 struct hostapd_hw_modes *mode;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003752 int cla, op, cli_cla;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003753
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003754 if (wpa_s->hw.modes == NULL) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003755 wpa_printf(MSG_DEBUG, "P2P: Driver did not support fetching "
3756 "of all supported channels; assume dualband "
3757 "support");
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003758 return wpas_p2p_default_channels(wpa_s, chan, cli_chan);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003759 }
3760
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003761 cla = cli_cla = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003762
3763 for (op = 0; op_class[op].op_class; op++) {
3764 struct p2p_oper_class_map *o = &op_class[op];
3765 u8 ch;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003766 struct p2p_reg_class *reg = NULL, *cli_reg = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003767
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003768 mode = get_mode(wpa_s->hw.modes, wpa_s->hw.num_modes, o->mode);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003769 if (mode == NULL)
3770 continue;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003771 if (mode->mode == HOSTAPD_MODE_IEEE80211G)
3772 wpa_s->global->p2p_24ghz_social_channels = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003773 for (ch = o->min_chan; ch <= o->max_chan; ch += o->inc) {
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003774 enum chan_allowed res;
3775 res = wpas_p2p_verify_channel(wpa_s, mode, ch, o->bw);
3776 if (res == ALLOWED) {
3777 if (reg == NULL) {
3778 wpa_printf(MSG_DEBUG, "P2P: Add operating class %u",
3779 o->op_class);
3780 reg = &chan->reg_class[cla];
3781 cla++;
3782 reg->reg_class = o->op_class;
3783 }
3784 reg->channel[reg->channels] = ch;
3785 reg->channels++;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003786 } else if (res == NO_IR &&
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003787 wpa_s->conf->p2p_add_cli_chan) {
3788 if (cli_reg == NULL) {
3789 wpa_printf(MSG_DEBUG, "P2P: Add operating class %u (client only)",
3790 o->op_class);
3791 cli_reg = &cli_chan->reg_class[cli_cla];
3792 cli_cla++;
3793 cli_reg->reg_class = o->op_class;
3794 }
3795 cli_reg->channel[cli_reg->channels] = ch;
3796 cli_reg->channels++;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003797 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003798 }
3799 if (reg) {
3800 wpa_hexdump(MSG_DEBUG, "P2P: Channels",
3801 reg->channel, reg->channels);
3802 }
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003803 if (cli_reg) {
3804 wpa_hexdump(MSG_DEBUG, "P2P: Channels (client only)",
3805 cli_reg->channel, cli_reg->channels);
3806 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003807 }
3808
3809 chan->reg_classes = cla;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003810 cli_chan->reg_classes = cli_cla;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003811
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003812 return 0;
3813}
3814
3815
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003816int wpas_p2p_get_ht40_mode(struct wpa_supplicant *wpa_s,
3817 struct hostapd_hw_modes *mode, u8 channel)
3818{
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003819 int op;
3820 enum chan_allowed ret;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003821
3822 for (op = 0; op_class[op].op_class; op++) {
3823 struct p2p_oper_class_map *o = &op_class[op];
3824 u8 ch;
3825
3826 for (ch = o->min_chan; ch <= o->max_chan; ch += o->inc) {
3827 if (o->mode != HOSTAPD_MODE_IEEE80211A ||
3828 o->bw == BW20 || ch != channel)
3829 continue;
3830 ret = wpas_p2p_verify_channel(wpa_s, mode, ch, o->bw);
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003831 if (ret == ALLOWED)
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003832 return (o->bw == BW40MINUS) ? -1 : 1;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003833 }
3834 }
3835 return 0;
3836}
3837
3838
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003839int wpas_p2p_get_vht80_center(struct wpa_supplicant *wpa_s,
3840 struct hostapd_hw_modes *mode, u8 channel)
3841{
3842 if (!wpas_p2p_verify_channel(wpa_s, mode, channel, BW80))
3843 return 0;
3844
3845 return wpas_p2p_get_center_80mhz(wpa_s, mode, channel);
3846}
3847
3848
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003849static int wpas_get_noa(void *ctx, const u8 *interface_addr, u8 *buf,
3850 size_t buf_len)
3851{
3852 struct wpa_supplicant *wpa_s = ctx;
3853
3854 for (wpa_s = wpa_s->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
3855 if (os_memcmp(wpa_s->own_addr, interface_addr, ETH_ALEN) == 0)
3856 break;
3857 }
3858 if (wpa_s == NULL)
3859 return -1;
3860
3861 return wpa_drv_get_noa(wpa_s, buf, buf_len);
3862}
3863
3864
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07003865struct wpa_supplicant * wpas_get_p2p_go_iface(struct wpa_supplicant *wpa_s,
3866 const u8 *ssid, size_t ssid_len)
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08003867{
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07003868 for (wpa_s = wpa_s->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
3869 struct wpa_ssid *s = wpa_s->current_ssid;
3870 if (s == NULL)
3871 continue;
3872 if (s->mode != WPAS_MODE_P2P_GO &&
3873 s->mode != WPAS_MODE_AP &&
3874 s->mode != WPAS_MODE_P2P_GROUP_FORMATION)
3875 continue;
3876 if (s->ssid_len != ssid_len ||
Dmitry Shmidt03658832014-08-13 11:03:49 -07003877 os_memcmp(ssid, s->ssid, ssid_len) != 0)
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07003878 continue;
3879 return wpa_s;
3880 }
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08003881
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07003882 return NULL;
3883
3884}
3885
3886
3887struct wpa_supplicant * wpas_get_p2p_client_iface(struct wpa_supplicant *wpa_s,
3888 const u8 *peer_dev_addr)
3889{
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08003890 for (wpa_s = wpa_s->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
3891 struct wpa_ssid *ssid = wpa_s->current_ssid;
3892 if (ssid == NULL)
3893 continue;
3894 if (ssid->mode != WPAS_MODE_INFRA)
3895 continue;
3896 if (wpa_s->wpa_state != WPA_COMPLETED &&
3897 wpa_s->wpa_state != WPA_GROUP_HANDSHAKE)
3898 continue;
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07003899 if (os_memcmp(wpa_s->go_dev_addr, peer_dev_addr, ETH_ALEN) == 0)
3900 return wpa_s;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08003901 }
3902
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07003903 return NULL;
3904}
3905
3906
3907static int wpas_go_connected(void *ctx, const u8 *dev_addr)
3908{
3909 struct wpa_supplicant *wpa_s = ctx;
3910
3911 return wpas_get_p2p_client_iface(wpa_s, dev_addr) != NULL;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08003912}
3913
3914
Dmitry Shmidt18463232014-01-24 12:29:41 -08003915static int wpas_is_concurrent_session_active(void *ctx)
3916{
3917 struct wpa_supplicant *wpa_s = ctx;
3918 struct wpa_supplicant *ifs;
3919
3920 for (ifs = wpa_s->global->ifaces; ifs; ifs = ifs->next) {
3921 if (ifs == wpa_s)
3922 continue;
3923 if (ifs->wpa_state > WPA_ASSOCIATED)
3924 return 1;
3925 }
3926 return 0;
3927}
3928
3929
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07003930static void wpas_p2p_debug_print(void *ctx, int level, const char *msg)
3931{
3932 struct wpa_supplicant *wpa_s = ctx;
3933 wpa_msg_global(wpa_s, level, "P2P: %s", msg);
3934}
3935
3936
Dmitry Shmidt9ead16e2014-10-07 13:15:23 -07003937int wpas_p2p_add_p2pdev_interface(struct wpa_supplicant *wpa_s,
3938 const char *conf_p2p_dev)
Dmitry Shmidt34af3062013-07-11 10:46:32 -07003939{
3940 struct wpa_interface iface;
3941 struct wpa_supplicant *p2pdev_wpa_s;
3942 char ifname[100];
3943 char force_name[100];
3944 int ret;
3945
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003946 ret = os_snprintf(ifname, sizeof(ifname), P2P_MGMT_DEVICE_PREFIX "%s",
3947 wpa_s->ifname);
3948 if (os_snprintf_error(sizeof(ifname), ret))
3949 return -1;
Dmitry Shmidt34af3062013-07-11 10:46:32 -07003950 force_name[0] = '\0';
3951 wpa_s->pending_interface_type = WPA_IF_P2P_DEVICE;
3952 ret = wpa_drv_if_add(wpa_s, WPA_IF_P2P_DEVICE, ifname, NULL, NULL,
3953 force_name, wpa_s->pending_interface_addr, NULL);
3954 if (ret < 0) {
3955 wpa_printf(MSG_DEBUG, "P2P: Failed to create P2P Device interface");
3956 return ret;
3957 }
3958 os_strlcpy(wpa_s->pending_interface_name, ifname,
3959 sizeof(wpa_s->pending_interface_name));
3960
3961 os_memset(&iface, 0, sizeof(iface));
3962 iface.p2p_mgmt = 1;
3963 iface.ifname = wpa_s->pending_interface_name;
3964 iface.driver = wpa_s->driver->name;
3965 iface.driver_param = wpa_s->conf->driver_param;
Dmitry Shmidt2ac5f602014-03-07 10:08:21 -08003966
3967 /*
3968 * If a P2P Device configuration file was given, use it as the interface
3969 * configuration file (instead of using parent's configuration file.
3970 */
Dmitry Shmidt9ead16e2014-10-07 13:15:23 -07003971 if (conf_p2p_dev) {
3972 iface.confname = conf_p2p_dev;
Dmitry Shmidt2ac5f602014-03-07 10:08:21 -08003973 iface.ctrl_interface = NULL;
3974 } else {
3975 iface.confname = wpa_s->confname;
3976 iface.ctrl_interface = wpa_s->conf->ctrl_interface;
3977 }
3978 iface.conf_p2p_dev = NULL;
3979
Dmitry Shmidt34af3062013-07-11 10:46:32 -07003980 p2pdev_wpa_s = wpa_supplicant_add_iface(wpa_s->global, &iface);
3981 if (!p2pdev_wpa_s) {
3982 wpa_printf(MSG_DEBUG, "P2P: Failed to add P2P Device interface");
3983 return -1;
3984 }
3985 p2pdev_wpa_s->parent = wpa_s;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003986 wpa_s->p2p_dev = p2pdev_wpa_s;
Dmitry Shmidt34af3062013-07-11 10:46:32 -07003987
3988 wpa_s->pending_interface_name[0] = '\0';
3989 return 0;
3990}
3991
3992
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08003993static void wpas_presence_resp(void *ctx, const u8 *src, u8 status,
3994 const u8 *noa, size_t noa_len)
3995{
3996 struct wpa_supplicant *wpa_s, *intf = ctx;
3997 char hex[100];
3998
3999 for (wpa_s = intf->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
4000 if (wpa_s->waiting_presence_resp)
4001 break;
4002 }
4003 if (!wpa_s) {
4004 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: No group interface was waiting for presence response");
4005 return;
4006 }
4007 wpa_s->waiting_presence_resp = 0;
4008
4009 wpa_snprintf_hex(hex, sizeof(hex), noa, noa_len);
4010 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_PRESENCE_RESPONSE "src=" MACSTR
4011 " status=%u noa=%s", MAC2STR(src), status, hex);
4012}
4013
4014
Dmitry Shmidt5a1480c2014-05-12 09:46:02 -07004015static int _wpas_p2p_in_progress(void *ctx)
4016{
4017 struct wpa_supplicant *wpa_s = ctx;
4018 return wpas_p2p_in_progress(wpa_s);
4019}
4020
4021
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004022/**
4023 * wpas_p2p_init - Initialize P2P module for %wpa_supplicant
4024 * @global: Pointer to global data from wpa_supplicant_init()
4025 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
4026 * Returns: 0 on success, -1 on failure
4027 */
4028int wpas_p2p_init(struct wpa_global *global, struct wpa_supplicant *wpa_s)
4029{
4030 struct p2p_config p2p;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004031 int i;
4032
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07004033 if (wpa_s->conf->p2p_disabled)
4034 return 0;
4035
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004036 if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_CAPABLE))
4037 return 0;
4038
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004039 if (global->p2p)
4040 return 0;
4041
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004042 os_memset(&p2p, 0, sizeof(p2p));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004043 p2p.cb_ctx = wpa_s;
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07004044 p2p.debug_print = wpas_p2p_debug_print;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004045 p2p.p2p_scan = wpas_p2p_scan;
4046 p2p.send_action = wpas_send_action;
4047 p2p.send_action_done = wpas_send_action_done;
4048 p2p.go_neg_completed = wpas_go_neg_completed;
4049 p2p.go_neg_req_rx = wpas_go_neg_req_rx;
4050 p2p.dev_found = wpas_dev_found;
4051 p2p.dev_lost = wpas_dev_lost;
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07004052 p2p.find_stopped = wpas_find_stopped;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004053 p2p.start_listen = wpas_start_listen;
4054 p2p.stop_listen = wpas_stop_listen;
4055 p2p.send_probe_resp = wpas_send_probe_resp;
4056 p2p.sd_request = wpas_sd_request;
4057 p2p.sd_response = wpas_sd_response;
4058 p2p.prov_disc_req = wpas_prov_disc_req;
4059 p2p.prov_disc_resp = wpas_prov_disc_resp;
Jouni Malinen75ecf522011-06-27 15:19:46 -07004060 p2p.prov_disc_fail = wpas_prov_disc_fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004061 p2p.invitation_process = wpas_invitation_process;
4062 p2p.invitation_received = wpas_invitation_received;
4063 p2p.invitation_result = wpas_invitation_result;
4064 p2p.get_noa = wpas_get_noa;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08004065 p2p.go_connected = wpas_go_connected;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08004066 p2p.presence_resp = wpas_presence_resp;
Dmitry Shmidt18463232014-01-24 12:29:41 -08004067 p2p.is_concurrent_session_active = wpas_is_concurrent_session_active;
Dmitry Shmidt5a1480c2014-05-12 09:46:02 -07004068 p2p.is_p2p_in_progress = _wpas_p2p_in_progress;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004069
4070 os_memcpy(wpa_s->global->p2p_dev_addr, wpa_s->own_addr, ETH_ALEN);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004071 os_memcpy(p2p.dev_addr, wpa_s->global->p2p_dev_addr, ETH_ALEN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004072 p2p.dev_name = wpa_s->conf->device_name;
4073 p2p.manufacturer = wpa_s->conf->manufacturer;
4074 p2p.model_name = wpa_s->conf->model_name;
4075 p2p.model_number = wpa_s->conf->model_number;
4076 p2p.serial_number = wpa_s->conf->serial_number;
4077 if (wpa_s->wps) {
4078 os_memcpy(p2p.uuid, wpa_s->wps->uuid, 16);
4079 p2p.config_methods = wpa_s->wps->config_methods;
4080 }
4081
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07004082 if (wpas_p2p_setup_channels(wpa_s, &p2p.channels, &p2p.cli_channels)) {
4083 wpa_printf(MSG_ERROR,
4084 "P2P: Failed to configure supported channel list");
4085 return -1;
4086 }
4087
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004088 if (wpa_s->conf->p2p_listen_reg_class &&
4089 wpa_s->conf->p2p_listen_channel) {
4090 p2p.reg_class = wpa_s->conf->p2p_listen_reg_class;
4091 p2p.channel = wpa_s->conf->p2p_listen_channel;
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07004092 p2p.channel_forced = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004093 } else {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004094 /*
4095 * Pick one of the social channels randomly as the listen
4096 * channel.
4097 */
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07004098 if (p2p_config_get_random_social(&p2p, &p2p.reg_class,
4099 &p2p.channel) != 0) {
4100 wpa_printf(MSG_ERROR,
4101 "P2P: Failed to select random social channel as listen channel");
4102 return -1;
4103 }
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07004104 p2p.channel_forced = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004105 }
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07004106 wpa_printf(MSG_DEBUG, "P2P: Own listen channel: %d:%d",
4107 p2p.reg_class, p2p.channel);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004108
4109 if (wpa_s->conf->p2p_oper_reg_class &&
4110 wpa_s->conf->p2p_oper_channel) {
4111 p2p.op_reg_class = wpa_s->conf->p2p_oper_reg_class;
4112 p2p.op_channel = wpa_s->conf->p2p_oper_channel;
4113 p2p.cfg_op_channel = 1;
4114 wpa_printf(MSG_DEBUG, "P2P: Configured operating channel: "
4115 "%d:%d", p2p.op_reg_class, p2p.op_channel);
4116
4117 } else {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004118 /*
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07004119 * Use random operation channel from 2.4 GHz band social
4120 * channels (1, 6, 11) or band 60 GHz social channel (2) if no
4121 * other preference is indicated.
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004122 */
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07004123 if (p2p_config_get_random_social(&p2p, &p2p.op_reg_class,
4124 &p2p.op_channel) != 0) {
4125 wpa_printf(MSG_ERROR,
4126 "P2P: Failed to select random social channel as operation channel");
4127 return -1;
4128 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004129 p2p.cfg_op_channel = 0;
4130 wpa_printf(MSG_DEBUG, "P2P: Random operating channel: "
4131 "%d:%d", p2p.op_reg_class, p2p.op_channel);
4132 }
Dmitry Shmidt44c95782013-05-17 09:51:35 -07004133
4134 if (wpa_s->conf->p2p_pref_chan && wpa_s->conf->num_p2p_pref_chan) {
4135 p2p.pref_chan = wpa_s->conf->p2p_pref_chan;
4136 p2p.num_pref_chan = wpa_s->conf->num_p2p_pref_chan;
4137 }
4138
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004139 if (wpa_s->conf->country[0] && wpa_s->conf->country[1]) {
4140 os_memcpy(p2p.country, wpa_s->conf->country, 2);
4141 p2p.country[2] = 0x04;
4142 } else
4143 os_memcpy(p2p.country, "XX\x04", 3);
4144
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004145 os_memcpy(p2p.pri_dev_type, wpa_s->conf->device_type,
4146 WPS_DEV_TYPE_LEN);
4147
4148 p2p.num_sec_dev_types = wpa_s->conf->num_sec_device_types;
4149 os_memcpy(p2p.sec_dev_type, wpa_s->conf->sec_device_type,
4150 p2p.num_sec_dev_types * WPS_DEV_TYPE_LEN);
4151
4152 p2p.concurrent_operations = !!(wpa_s->drv_flags &
4153 WPA_DRIVER_FLAGS_P2P_CONCURRENT);
4154
4155 p2p.max_peers = 100;
4156
4157 if (wpa_s->conf->p2p_ssid_postfix) {
4158 p2p.ssid_postfix_len =
4159 os_strlen(wpa_s->conf->p2p_ssid_postfix);
4160 if (p2p.ssid_postfix_len > sizeof(p2p.ssid_postfix))
4161 p2p.ssid_postfix_len = sizeof(p2p.ssid_postfix);
4162 os_memcpy(p2p.ssid_postfix, wpa_s->conf->p2p_ssid_postfix,
4163 p2p.ssid_postfix_len);
4164 }
4165
4166 p2p.p2p_intra_bss = wpa_s->conf->p2p_intra_bss;
4167
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08004168 p2p.max_listen = wpa_s->max_remain_on_chan;
4169
Dmitry Shmidt2271d3f2014-06-23 12:16:31 -07004170 if (wpa_s->conf->p2p_passphrase_len >= 8 &&
4171 wpa_s->conf->p2p_passphrase_len <= 63)
4172 p2p.passphrase_len = wpa_s->conf->p2p_passphrase_len;
4173 else
4174 p2p.passphrase_len = 8;
4175
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004176 global->p2p = p2p_init(&p2p);
4177 if (global->p2p == NULL)
4178 return -1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07004179 global->p2p_init_wpa_s = wpa_s;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004180
4181 for (i = 0; i < MAX_WPS_VENDOR_EXT; i++) {
4182 if (wpa_s->conf->wps_vendor_ext[i] == NULL)
4183 continue;
4184 p2p_add_wps_vendor_extension(
4185 global->p2p, wpa_s->conf->wps_vendor_ext[i]);
4186 }
4187
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004188 p2p_set_no_go_freq(global->p2p, &wpa_s->conf->p2p_no_go_freq);
4189
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004190 return 0;
4191}
4192
4193
4194/**
4195 * wpas_p2p_deinit - Deinitialize per-interface P2P data
4196 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
4197 *
4198 * This function deinitialize per-interface P2P data.
4199 */
4200void wpas_p2p_deinit(struct wpa_supplicant *wpa_s)
4201{
4202 if (wpa_s->driver && wpa_s->drv_priv)
4203 wpa_drv_probe_req_report(wpa_s, 0);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004204
4205 if (wpa_s->go_params) {
4206 /* Clear any stored provisioning info */
4207 p2p_clear_provisioning_info(
4208 wpa_s->global->p2p,
Dmitry Shmidt04949592012-07-19 12:16:46 -07004209 wpa_s->go_params->peer_device_addr);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004210 }
4211
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004212 os_free(wpa_s->go_params);
4213 wpa_s->go_params = NULL;
Dmitry Shmidt684785c2014-05-12 13:34:29 -07004214 eloop_cancel_timeout(wpas_p2p_psk_failure_removal, wpa_s, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004215 eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s, NULL);
4216 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
4217 wpa_s->p2p_long_listen = 0;
4218 eloop_cancel_timeout(wpas_p2p_long_listen_timeout, wpa_s, NULL);
4219 eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL);
4220 wpas_p2p_remove_pending_group_interface(wpa_s);
Dmitry Shmidt04f534e2013-12-09 15:50:16 -08004221 eloop_cancel_timeout(wpas_p2p_group_freq_conflict, wpa_s, NULL);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08004222 wpas_p2p_listen_work_done(wpa_s);
4223 if (wpa_s->p2p_send_action_work) {
4224 os_free(wpa_s->p2p_send_action_work->ctx);
4225 radio_work_done(wpa_s->p2p_send_action_work);
4226 wpa_s->p2p_send_action_work = NULL;
4227 }
4228 eloop_cancel_timeout(wpas_p2p_send_action_work_timeout, wpa_s, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004229
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08004230 wpabuf_free(wpa_s->p2p_oob_dev_pw);
4231 wpa_s->p2p_oob_dev_pw = NULL;
4232
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004233 os_free(wpa_s->p2p_group_common_freqs);
4234 wpa_s->p2p_group_common_freqs = NULL;
4235 wpa_s->p2p_group_common_freqs_num = 0;
4236
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004237 /* TODO: remove group interface from the driver if this wpa_s instance
4238 * is on top of a P2P group interface */
4239}
4240
4241
4242/**
4243 * wpas_p2p_deinit_global - Deinitialize global P2P module
4244 * @global: Pointer to global data from wpa_supplicant_init()
4245 *
4246 * This function deinitializes the global (per device) P2P module.
4247 */
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07004248static void wpas_p2p_deinit_global(struct wpa_global *global)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004249{
4250 struct wpa_supplicant *wpa_s, *tmp;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004251
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004252 wpa_s = global->ifaces;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004253
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07004254 wpas_p2p_service_flush(global->p2p_init_wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004255
4256 /* Remove remaining P2P group interfaces */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004257 while (wpa_s && wpa_s->p2p_group_interface != NOT_P2P_GROUP_INTERFACE)
4258 wpa_s = wpa_s->next;
4259 while (wpa_s) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004260 tmp = global->ifaces;
4261 while (tmp &&
4262 (tmp == wpa_s ||
4263 tmp->p2p_group_interface == NOT_P2P_GROUP_INTERFACE)) {
4264 tmp = tmp->next;
4265 }
4266 if (tmp == NULL)
4267 break;
Dmitry Shmidt04949592012-07-19 12:16:46 -07004268 /* Disconnect from the P2P group and deinit the interface */
4269 wpas_p2p_disconnect(tmp);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004270 }
4271
4272 /*
4273 * Deinit GO data on any possibly remaining interface (if main
4274 * interface is used as GO).
4275 */
4276 for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
4277 if (wpa_s->ap_iface)
4278 wpas_p2p_group_deinit(wpa_s);
4279 }
4280
4281 p2p_deinit(global->p2p);
4282 global->p2p = NULL;
Dmitry Shmidt04949592012-07-19 12:16:46 -07004283 global->p2p_init_wpa_s = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004284}
4285
4286
4287static int wpas_p2p_create_iface(struct wpa_supplicant *wpa_s)
4288{
Dmitry Shmidt34af3062013-07-11 10:46:32 -07004289 if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_DEDICATED_P2P_DEVICE) &&
4290 wpa_s->conf->p2p_no_group_iface)
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08004291 return 0; /* separate interface disabled per configuration */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004292 if (wpa_s->drv_flags &
4293 (WPA_DRIVER_FLAGS_P2P_DEDICATED_INTERFACE |
4294 WPA_DRIVER_FLAGS_P2P_MGMT_AND_NON_P2P))
4295 return 1; /* P2P group requires a new interface in every case
4296 */
4297 if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_CONCURRENT))
4298 return 0; /* driver does not support concurrent operations */
4299 if (wpa_s->global->ifaces->next)
4300 return 1; /* more that one interface already in use */
4301 if (wpa_s->wpa_state >= WPA_AUTHENTICATING)
4302 return 1; /* this interface is already in use */
4303 return 0;
4304}
4305
4306
4307static int wpas_p2p_start_go_neg(struct wpa_supplicant *wpa_s,
4308 const u8 *peer_addr,
4309 enum p2p_wps_method wps_method,
4310 int go_intent, const u8 *own_interface_addr,
Dmitry Shmidt04949592012-07-19 12:16:46 -07004311 unsigned int force_freq, int persistent_group,
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08004312 struct wpa_ssid *ssid, unsigned int pref_freq)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004313{
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004314 if (persistent_group && wpa_s->conf->persistent_reconnect)
4315 persistent_group = 2;
4316
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004317 /*
4318 * Increase GO config timeout if HT40 is used since it takes some time
4319 * to scan channels for coex purposes before the BSS can be started.
4320 */
4321 p2p_set_config_timeout(wpa_s->global->p2p,
4322 wpa_s->p2p_go_ht40 ? 255 : 100, 20);
4323
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004324 return p2p_connect(wpa_s->global->p2p, peer_addr, wps_method,
4325 go_intent, own_interface_addr, force_freq,
Dmitry Shmidt04949592012-07-19 12:16:46 -07004326 persistent_group, ssid ? ssid->ssid : NULL,
4327 ssid ? ssid->ssid_len : 0,
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08004328 wpa_s->p2p_pd_before_go_neg, pref_freq,
4329 wps_method == WPS_NFC ? wpa_s->p2p_oob_dev_pw_id :
4330 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004331}
4332
4333
4334static int wpas_p2p_auth_go_neg(struct wpa_supplicant *wpa_s,
4335 const u8 *peer_addr,
4336 enum p2p_wps_method wps_method,
4337 int go_intent, const u8 *own_interface_addr,
Dmitry Shmidt04949592012-07-19 12:16:46 -07004338 unsigned int force_freq, int persistent_group,
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08004339 struct wpa_ssid *ssid, unsigned int pref_freq)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004340{
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004341 if (persistent_group && wpa_s->conf->persistent_reconnect)
4342 persistent_group = 2;
4343
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004344 return p2p_authorize(wpa_s->global->p2p, peer_addr, wps_method,
4345 go_intent, own_interface_addr, force_freq,
Dmitry Shmidt04949592012-07-19 12:16:46 -07004346 persistent_group, ssid ? ssid->ssid : NULL,
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08004347 ssid ? ssid->ssid_len : 0, pref_freq,
4348 wps_method == WPS_NFC ? wpa_s->p2p_oob_dev_pw_id :
4349 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004350}
4351
4352
4353static void wpas_p2p_check_join_scan_limit(struct wpa_supplicant *wpa_s)
4354{
4355 wpa_s->p2p_join_scan_count++;
4356 wpa_printf(MSG_DEBUG, "P2P: Join scan attempt %d",
4357 wpa_s->p2p_join_scan_count);
4358 if (wpa_s->p2p_join_scan_count > P2P_MAX_JOIN_SCAN_ATTEMPTS) {
4359 wpa_printf(MSG_DEBUG, "P2P: Failed to find GO " MACSTR
4360 " for join operationg - stop join attempt",
4361 MAC2STR(wpa_s->pending_join_iface_addr));
4362 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
Dmitry Shmidt04949592012-07-19 12:16:46 -07004363 if (wpa_s->p2p_auto_pd) {
4364 wpa_s->p2p_auto_pd = 0;
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07004365 wpa_msg_global(wpa_s, MSG_INFO,
4366 P2P_EVENT_PROV_DISC_FAILURE
4367 " p2p_dev_addr=" MACSTR " status=N/A",
4368 MAC2STR(wpa_s->pending_join_dev_addr));
Dmitry Shmidt04949592012-07-19 12:16:46 -07004369 return;
4370 }
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07004371 wpa_msg_global(wpa_s->parent, MSG_INFO,
4372 P2P_EVENT_GROUP_FORMATION_FAILURE);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004373 }
4374}
4375
4376
Dmitry Shmidt04949592012-07-19 12:16:46 -07004377static int wpas_check_freq_conflict(struct wpa_supplicant *wpa_s, int freq)
4378{
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07004379 int res;
4380 unsigned int num, i;
4381 struct wpa_used_freq_data *freqs;
Dmitry Shmidt04949592012-07-19 12:16:46 -07004382
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07004383 if (wpas_p2p_num_unused_channels(wpa_s) > 0) {
4384 /* Multiple channels are supported and not all are in use */
Dmitry Shmidt04949592012-07-19 12:16:46 -07004385 return 0;
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07004386 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07004387
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07004388 freqs = os_calloc(wpa_s->num_multichan_concurrent,
4389 sizeof(struct wpa_used_freq_data));
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07004390 if (!freqs)
4391 return 1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07004392
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07004393 num = wpas_p2p_valid_oper_freqs(wpa_s, freqs,
4394 wpa_s->num_multichan_concurrent);
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07004395
4396 for (i = 0; i < num; i++) {
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07004397 if (freqs[i].freq == freq) {
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07004398 wpa_printf(MSG_DEBUG, "P2P: Frequency %d MHz in use by another virtual interface and can be used",
4399 freq);
4400 res = 0;
4401 goto exit_free;
Dmitry Shmidt04949592012-07-19 12:16:46 -07004402 }
4403 }
4404
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07004405 wpa_printf(MSG_DEBUG, "P2P: No valid operating frequencies");
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07004406 res = 1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07004407
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07004408exit_free:
4409 os_free(freqs);
4410 return res;
Dmitry Shmidt04949592012-07-19 12:16:46 -07004411}
4412
4413
4414static int wpas_p2p_peer_go(struct wpa_supplicant *wpa_s,
4415 const u8 *peer_dev_addr)
4416{
4417 struct wpa_bss *bss;
4418 int updated;
4419
4420 bss = wpa_bss_get_p2p_dev_addr(wpa_s, peer_dev_addr);
4421 if (bss == NULL)
4422 return -1;
4423 if (bss->last_update_idx < wpa_s->bss_update_idx) {
4424 wpa_printf(MSG_DEBUG, "P2P: Peer BSS entry not updated in the "
4425 "last scan");
4426 return 0;
4427 }
4428
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08004429 updated = os_reltime_before(&wpa_s->p2p_auto_started,
4430 &bss->last_update);
Dmitry Shmidt04949592012-07-19 12:16:46 -07004431 wpa_printf(MSG_DEBUG, "P2P: Current BSS entry for peer updated at "
4432 "%ld.%06ld (%supdated in last scan)",
4433 bss->last_update.sec, bss->last_update.usec,
4434 updated ? "": "not ");
4435
4436 return updated;
4437}
4438
4439
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004440static void wpas_p2p_scan_res_join(struct wpa_supplicant *wpa_s,
4441 struct wpa_scan_results *scan_res)
4442{
Dmitry Shmidt344abd32014-01-14 13:17:00 -08004443 struct wpa_bss *bss = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004444 int freq;
4445 u8 iface_addr[ETH_ALEN];
Dmitry Shmidt04949592012-07-19 12:16:46 -07004446
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004447 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
4448
4449 if (wpa_s->global->p2p_disabled)
4450 return;
4451
Dmitry Shmidt04949592012-07-19 12:16:46 -07004452 wpa_printf(MSG_DEBUG, "P2P: Scan results received (%d BSS) for %sjoin",
4453 scan_res ? (int) scan_res->num : -1,
4454 wpa_s->p2p_auto_join ? "auto_" : "");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004455
4456 if (scan_res)
4457 wpas_p2p_scan_res_handler(wpa_s, scan_res);
4458
Dmitry Shmidt04949592012-07-19 12:16:46 -07004459 if (wpa_s->p2p_auto_pd) {
4460 int join = wpas_p2p_peer_go(wpa_s,
4461 wpa_s->pending_join_dev_addr);
4462 if (join == 0 &&
4463 wpa_s->auto_pd_scan_retry < P2P_AUTO_PD_SCAN_ATTEMPTS) {
4464 wpa_s->auto_pd_scan_retry++;
Dmitry Shmidt444d5672013-04-01 13:08:44 -07004465 bss = wpa_bss_get_bssid_latest(
4466 wpa_s, wpa_s->pending_join_dev_addr);
Dmitry Shmidt04949592012-07-19 12:16:46 -07004467 if (bss) {
4468 freq = bss->freq;
4469 wpa_printf(MSG_DEBUG, "P2P: Scan retry %d for "
4470 "the peer " MACSTR " at %d MHz",
4471 wpa_s->auto_pd_scan_retry,
4472 MAC2STR(wpa_s->
4473 pending_join_dev_addr),
4474 freq);
Dmitry Shmidt344abd32014-01-14 13:17:00 -08004475 wpas_p2p_join_scan_req(wpa_s, freq, NULL, 0);
Dmitry Shmidt04949592012-07-19 12:16:46 -07004476 return;
4477 }
4478 }
4479
4480 if (join < 0)
4481 join = 0;
4482
4483 wpa_s->p2p_auto_pd = 0;
4484 wpa_s->pending_pd_use = join ? AUTO_PD_JOIN : AUTO_PD_GO_NEG;
4485 wpa_printf(MSG_DEBUG, "P2P: Auto PD with " MACSTR " join=%d",
4486 MAC2STR(wpa_s->pending_join_dev_addr), join);
4487 if (p2p_prov_disc_req(wpa_s->global->p2p,
4488 wpa_s->pending_join_dev_addr,
4489 wpa_s->pending_pd_config_methods, join,
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08004490 0, wpa_s->user_initiated_pd) < 0) {
Dmitry Shmidt04949592012-07-19 12:16:46 -07004491 wpa_s->p2p_auto_pd = 0;
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07004492 wpa_msg_global(wpa_s, MSG_INFO,
4493 P2P_EVENT_PROV_DISC_FAILURE
4494 " p2p_dev_addr=" MACSTR " status=N/A",
4495 MAC2STR(wpa_s->pending_join_dev_addr));
Dmitry Shmidt04949592012-07-19 12:16:46 -07004496 }
4497 return;
4498 }
4499
4500 if (wpa_s->p2p_auto_join) {
4501 int join = wpas_p2p_peer_go(wpa_s,
4502 wpa_s->pending_join_dev_addr);
4503 if (join < 0) {
4504 wpa_printf(MSG_DEBUG, "P2P: Peer was not found to be "
4505 "running a GO -> use GO Negotiation");
4506 wpas_p2p_connect(wpa_s, wpa_s->pending_join_dev_addr,
4507 wpa_s->p2p_pin, wpa_s->p2p_wps_method,
4508 wpa_s->p2p_persistent_group, 0, 0, 0,
4509 wpa_s->p2p_go_intent,
4510 wpa_s->p2p_connect_freq,
4511 wpa_s->p2p_persistent_id,
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004512 wpa_s->p2p_pd_before_go_neg,
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004513 wpa_s->p2p_go_ht40,
4514 wpa_s->p2p_go_vht);
Dmitry Shmidt04949592012-07-19 12:16:46 -07004515 return;
4516 }
4517
4518 wpa_printf(MSG_DEBUG, "P2P: Peer was found running GO%s -> "
4519 "try to join the group", join ? "" :
4520 " in older scan");
4521 if (!join)
4522 wpa_s->p2p_fallback_to_go_neg = 1;
4523 }
4524
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004525 freq = p2p_get_oper_freq(wpa_s->global->p2p,
4526 wpa_s->pending_join_iface_addr);
4527 if (freq < 0 &&
4528 p2p_get_interface_addr(wpa_s->global->p2p,
4529 wpa_s->pending_join_dev_addr,
4530 iface_addr) == 0 &&
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07004531 os_memcmp(iface_addr, wpa_s->pending_join_dev_addr, ETH_ALEN) != 0
4532 && !wpa_bss_get_bssid(wpa_s, wpa_s->pending_join_iface_addr)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004533 wpa_printf(MSG_DEBUG, "P2P: Overwrite pending interface "
4534 "address for join from " MACSTR " to " MACSTR
4535 " based on newly discovered P2P peer entry",
4536 MAC2STR(wpa_s->pending_join_iface_addr),
4537 MAC2STR(iface_addr));
4538 os_memcpy(wpa_s->pending_join_iface_addr, iface_addr,
4539 ETH_ALEN);
4540
4541 freq = p2p_get_oper_freq(wpa_s->global->p2p,
4542 wpa_s->pending_join_iface_addr);
4543 }
4544 if (freq >= 0) {
4545 wpa_printf(MSG_DEBUG, "P2P: Target GO operating frequency "
4546 "from P2P peer table: %d MHz", freq);
4547 }
Dmitry Shmidt344abd32014-01-14 13:17:00 -08004548 if (wpa_s->p2p_join_ssid_len) {
4549 wpa_printf(MSG_DEBUG, "P2P: Trying to find target GO BSS entry based on BSSID "
4550 MACSTR " and SSID %s",
4551 MAC2STR(wpa_s->pending_join_iface_addr),
4552 wpa_ssid_txt(wpa_s->p2p_join_ssid,
4553 wpa_s->p2p_join_ssid_len));
4554 bss = wpa_bss_get(wpa_s, wpa_s->pending_join_iface_addr,
4555 wpa_s->p2p_join_ssid,
4556 wpa_s->p2p_join_ssid_len);
4557 }
4558 if (!bss) {
4559 wpa_printf(MSG_DEBUG, "P2P: Trying to find target GO BSS entry based on BSSID "
4560 MACSTR, MAC2STR(wpa_s->pending_join_iface_addr));
4561 bss = wpa_bss_get_bssid_latest(wpa_s,
4562 wpa_s->pending_join_iface_addr);
4563 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004564 if (bss) {
4565 freq = bss->freq;
4566 wpa_printf(MSG_DEBUG, "P2P: Target GO operating frequency "
Dmitry Shmidt051af732013-10-22 13:52:46 -07004567 "from BSS table: %d MHz (SSID %s)", freq,
4568 wpa_ssid_txt(bss->ssid, bss->ssid_len));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004569 }
4570 if (freq > 0) {
4571 u16 method;
4572
Dmitry Shmidt04949592012-07-19 12:16:46 -07004573 if (wpas_check_freq_conflict(wpa_s, freq) > 0) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07004574 wpa_msg_global(wpa_s->parent, MSG_INFO,
4575 P2P_EVENT_GROUP_FORMATION_FAILURE
4576 "reason=FREQ_CONFLICT");
Dmitry Shmidt04949592012-07-19 12:16:46 -07004577 return;
4578 }
4579
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004580 wpa_printf(MSG_DEBUG, "P2P: Send Provision Discovery Request "
4581 "prior to joining an existing group (GO " MACSTR
4582 " freq=%u MHz)",
4583 MAC2STR(wpa_s->pending_join_dev_addr), freq);
4584 wpa_s->pending_pd_before_join = 1;
4585
4586 switch (wpa_s->pending_join_wps_method) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004587 case WPS_PIN_DISPLAY:
4588 method = WPS_CONFIG_KEYPAD;
4589 break;
4590 case WPS_PIN_KEYPAD:
4591 method = WPS_CONFIG_DISPLAY;
4592 break;
4593 case WPS_PBC:
4594 method = WPS_CONFIG_PUSHBUTTON;
4595 break;
4596 default:
4597 method = 0;
4598 break;
4599 }
4600
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004601 if ((p2p_get_provisioning_info(wpa_s->global->p2p,
4602 wpa_s->pending_join_dev_addr) ==
4603 method)) {
4604 /*
4605 * We have already performed provision discovery for
4606 * joining the group. Proceed directly to join
4607 * operation without duplicated provision discovery. */
4608 wpa_printf(MSG_DEBUG, "P2P: Provision discovery "
4609 "with " MACSTR " already done - proceed to "
4610 "join",
4611 MAC2STR(wpa_s->pending_join_dev_addr));
4612 wpa_s->pending_pd_before_join = 0;
4613 goto start;
4614 }
4615
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004616 if (p2p_prov_disc_req(wpa_s->global->p2p,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004617 wpa_s->pending_join_dev_addr, method, 1,
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08004618 freq, wpa_s->user_initiated_pd) < 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004619 wpa_printf(MSG_DEBUG, "P2P: Failed to send Provision "
4620 "Discovery Request before joining an "
4621 "existing group");
4622 wpa_s->pending_pd_before_join = 0;
4623 goto start;
4624 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004625 return;
4626 }
4627
4628 wpa_printf(MSG_DEBUG, "P2P: Failed to find BSS/GO - try again later");
4629 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
4630 eloop_register_timeout(1, 0, wpas_p2p_join_scan, wpa_s, NULL);
4631 wpas_p2p_check_join_scan_limit(wpa_s);
4632 return;
4633
4634start:
4635 /* Start join operation immediately */
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08004636 wpas_p2p_join_start(wpa_s, 0, NULL, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004637}
4638
4639
Dmitry Shmidt344abd32014-01-14 13:17:00 -08004640static void wpas_p2p_join_scan_req(struct wpa_supplicant *wpa_s, int freq,
4641 const u8 *ssid, size_t ssid_len)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004642{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004643 int ret;
4644 struct wpa_driver_scan_params params;
4645 struct wpabuf *wps_ie, *ies;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004646 size_t ielen;
Dmitry Shmidt04949592012-07-19 12:16:46 -07004647 int freqs[2] = { 0, 0 };
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004648
4649 os_memset(&params, 0, sizeof(params));
4650
4651 /* P2P Wildcard SSID */
4652 params.num_ssids = 1;
Dmitry Shmidt344abd32014-01-14 13:17:00 -08004653 if (ssid && ssid_len) {
4654 params.ssids[0].ssid = ssid;
4655 params.ssids[0].ssid_len = ssid_len;
4656 os_memcpy(wpa_s->p2p_join_ssid, ssid, ssid_len);
4657 wpa_s->p2p_join_ssid_len = ssid_len;
4658 } else {
4659 params.ssids[0].ssid = (u8 *) P2P_WILDCARD_SSID;
4660 params.ssids[0].ssid_len = P2P_WILDCARD_SSID_LEN;
4661 wpa_s->p2p_join_ssid_len = 0;
4662 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004663
4664 wpa_s->wps->dev.p2p = 1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07004665 wps_ie = wps_build_probe_req_ie(DEV_PW_DEFAULT, &wpa_s->wps->dev,
4666 wpa_s->wps->uuid, WPS_REQ_ENROLLEE, 0,
4667 NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004668 if (wps_ie == NULL) {
4669 wpas_p2p_scan_res_join(wpa_s, NULL);
4670 return;
4671 }
4672
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004673 ielen = p2p_scan_ie_buf_len(wpa_s->global->p2p);
4674 ies = wpabuf_alloc(wpabuf_len(wps_ie) + ielen);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004675 if (ies == NULL) {
4676 wpabuf_free(wps_ie);
4677 wpas_p2p_scan_res_join(wpa_s, NULL);
4678 return;
4679 }
4680 wpabuf_put_buf(ies, wps_ie);
4681 wpabuf_free(wps_ie);
4682
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08004683 p2p_scan_ie(wpa_s->global->p2p, ies, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004684
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004685 params.p2p_probe = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004686 params.extra_ies = wpabuf_head(ies);
4687 params.extra_ies_len = wpabuf_len(ies);
Dmitry Shmidt9e3f8ee2014-01-17 10:52:01 -08004688
4689 if (!freq) {
4690 int oper_freq;
4691 /*
4692 * If freq is not provided, check the operating freq of the GO
4693 * and use a single channel scan on if possible.
4694 */
4695 oper_freq = p2p_get_oper_freq(wpa_s->global->p2p,
4696 wpa_s->pending_join_iface_addr);
4697 if (oper_freq > 0)
4698 freq = oper_freq;
4699 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07004700 if (freq > 0) {
4701 freqs[0] = freq;
4702 params.freqs = freqs;
4703 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004704
4705 /*
4706 * Run a scan to update BSS table and start Provision Discovery once
4707 * the new scan results become available.
4708 */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004709 ret = wpa_drv_scan(wpa_s, &params);
Dmitry Shmidt444d5672013-04-01 13:08:44 -07004710 if (!ret) {
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08004711 os_get_reltime(&wpa_s->scan_trigger_time);
Dmitry Shmidt04949592012-07-19 12:16:46 -07004712 wpa_s->scan_res_handler = wpas_p2p_scan_res_join;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08004713 wpa_s->own_scan_requested = 1;
Dmitry Shmidt444d5672013-04-01 13:08:44 -07004714 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004715
4716 wpabuf_free(ies);
4717
4718 if (ret) {
4719 wpa_printf(MSG_DEBUG, "P2P: Failed to start scan for join - "
4720 "try again later");
4721 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
4722 eloop_register_timeout(1, 0, wpas_p2p_join_scan, wpa_s, NULL);
4723 wpas_p2p_check_join_scan_limit(wpa_s);
4724 }
4725}
4726
4727
Dmitry Shmidt04949592012-07-19 12:16:46 -07004728static void wpas_p2p_join_scan(void *eloop_ctx, void *timeout_ctx)
4729{
4730 struct wpa_supplicant *wpa_s = eloop_ctx;
Dmitry Shmidt344abd32014-01-14 13:17:00 -08004731 wpas_p2p_join_scan_req(wpa_s, 0, NULL, 0);
Dmitry Shmidt04949592012-07-19 12:16:46 -07004732}
4733
4734
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004735static int wpas_p2p_join(struct wpa_supplicant *wpa_s, const u8 *iface_addr,
Dmitry Shmidt04949592012-07-19 12:16:46 -07004736 const u8 *dev_addr, enum p2p_wps_method wps_method,
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08004737 int auto_join, int op_freq,
4738 const u8 *ssid, size_t ssid_len)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004739{
4740 wpa_printf(MSG_DEBUG, "P2P: Request to join existing group (iface "
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08004741 MACSTR " dev " MACSTR " op_freq=%d)%s",
4742 MAC2STR(iface_addr), MAC2STR(dev_addr), op_freq,
Dmitry Shmidt04949592012-07-19 12:16:46 -07004743 auto_join ? " (auto_join)" : "");
Dmitry Shmidt344abd32014-01-14 13:17:00 -08004744 if (ssid && ssid_len) {
4745 wpa_printf(MSG_DEBUG, "P2P: Group SSID specified: %s",
4746 wpa_ssid_txt(ssid, ssid_len));
4747 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004748
Dmitry Shmidt04949592012-07-19 12:16:46 -07004749 wpa_s->p2p_auto_pd = 0;
4750 wpa_s->p2p_auto_join = !!auto_join;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004751 os_memcpy(wpa_s->pending_join_iface_addr, iface_addr, ETH_ALEN);
4752 os_memcpy(wpa_s->pending_join_dev_addr, dev_addr, ETH_ALEN);
4753 wpa_s->pending_join_wps_method = wps_method;
4754
4755 /* Make sure we are not running find during connection establishment */
4756 wpas_p2p_stop_find(wpa_s);
4757
4758 wpa_s->p2p_join_scan_count = 0;
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08004759 wpas_p2p_join_scan_req(wpa_s, op_freq, ssid, ssid_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004760 return 0;
4761}
4762
4763
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08004764static int wpas_p2p_join_start(struct wpa_supplicant *wpa_s, int freq,
4765 const u8 *ssid, size_t ssid_len)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004766{
4767 struct wpa_supplicant *group;
4768 struct p2p_go_neg_results res;
Dmitry Shmidt04949592012-07-19 12:16:46 -07004769 struct wpa_bss *bss;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004770
4771 group = wpas_p2p_get_group_iface(wpa_s, 0, 0);
4772 if (group == NULL)
4773 return -1;
4774 if (group != wpa_s) {
4775 os_memcpy(group->p2p_pin, wpa_s->p2p_pin,
4776 sizeof(group->p2p_pin));
4777 group->p2p_wps_method = wpa_s->p2p_wps_method;
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08004778 } else {
4779 /*
4780 * Need to mark the current interface for p2p_group_formation
4781 * when a separate group interface is not used. This is needed
4782 * to allow p2p_cancel stop a pending p2p_connect-join.
4783 * wpas_p2p_init_group_interface() addresses this for the case
4784 * where a separate group interface is used.
4785 */
4786 wpa_s->global->p2p_group_formation = wpa_s;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004787 }
4788
4789 group->p2p_in_provisioning = 1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07004790 group->p2p_fallback_to_go_neg = wpa_s->p2p_fallback_to_go_neg;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004791
4792 os_memset(&res, 0, sizeof(res));
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08004793 os_memcpy(res.peer_device_addr, wpa_s->pending_join_dev_addr, ETH_ALEN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004794 os_memcpy(res.peer_interface_addr, wpa_s->pending_join_iface_addr,
4795 ETH_ALEN);
4796 res.wps_method = wpa_s->pending_join_wps_method;
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08004797 if (freq && ssid && ssid_len) {
4798 res.freq = freq;
4799 res.ssid_len = ssid_len;
4800 os_memcpy(res.ssid, ssid, ssid_len);
4801 } else {
4802 bss = wpa_bss_get_bssid_latest(wpa_s,
4803 wpa_s->pending_join_iface_addr);
4804 if (bss) {
4805 res.freq = bss->freq;
4806 res.ssid_len = bss->ssid_len;
4807 os_memcpy(res.ssid, bss->ssid, bss->ssid_len);
4808 wpa_printf(MSG_DEBUG, "P2P: Join target GO operating frequency from BSS table: %d MHz (SSID %s)",
4809 bss->freq,
4810 wpa_ssid_txt(bss->ssid, bss->ssid_len));
4811 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07004812 }
4813
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08004814 if (wpa_s->off_channel_freq || wpa_s->roc_waiting_drv_freq) {
4815 wpa_printf(MSG_DEBUG, "P2P: Cancel remain-on-channel prior to "
4816 "starting client");
4817 wpa_drv_cancel_remain_on_channel(wpa_s);
4818 wpa_s->off_channel_freq = 0;
4819 wpa_s->roc_waiting_drv_freq = 0;
4820 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004821 wpas_start_wps_enrollee(group, &res);
4822
4823 /*
4824 * Allow a longer timeout for join-a-running-group than normal 15
4825 * second group formation timeout since the GO may not have authorized
4826 * our connection yet.
4827 */
4828 eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s, NULL);
4829 eloop_register_timeout(60, 0, wpas_p2p_group_formation_timeout,
4830 wpa_s, NULL);
4831
4832 return 0;
4833}
4834
4835
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08004836static int wpas_p2p_setup_freqs(struct wpa_supplicant *wpa_s, int freq,
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004837 int *force_freq, int *pref_freq, int go)
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08004838{
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07004839 struct wpa_used_freq_data *freqs;
4840 int res, best_freq, num_unused;
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07004841 unsigned int freq_in_use = 0, num, i;
4842
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07004843 freqs = os_calloc(wpa_s->num_multichan_concurrent,
4844 sizeof(struct wpa_used_freq_data));
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07004845 if (!freqs)
4846 return -1;
4847
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07004848 num = wpas_p2p_valid_oper_freqs(wpa_s, freqs,
4849 wpa_s->num_multichan_concurrent);
4850
4851 /*
4852 * It is possible that the total number of used frequencies is bigger
4853 * than the number of frequencies used for P2P, so get the system wide
4854 * number of unused frequencies.
4855 */
4856 num_unused = wpas_p2p_num_unused_channels(wpa_s);
4857
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07004858 wpa_printf(MSG_DEBUG,
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07004859 "P2P: Setup freqs: freq=%d num_MCC=%d shared_freqs=%u num_unused=%d",
4860 freq, wpa_s->num_multichan_concurrent, num, num_unused);
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07004861
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08004862 if (freq > 0) {
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004863 int ret;
4864 if (go)
4865 ret = p2p_supported_freq(wpa_s->global->p2p, freq);
4866 else
4867 ret = p2p_supported_freq_cli(wpa_s->global->p2p, freq);
4868 if (!ret) {
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08004869 wpa_printf(MSG_DEBUG, "P2P: The forced channel "
4870 "(%u MHz) is not supported for P2P uses",
4871 freq);
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07004872 res = -3;
4873 goto exit_free;
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08004874 }
4875
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07004876 for (i = 0; i < num; i++) {
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07004877 if (freqs[i].freq == freq)
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07004878 freq_in_use = 1;
4879 }
4880
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07004881 if (num_unused <= 0 && !freq_in_use) {
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07004882 wpa_printf(MSG_DEBUG, "P2P: Cannot start P2P group on %u MHz as there are no available channels",
4883 freq);
4884 res = -2;
4885 goto exit_free;
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08004886 }
4887 wpa_printf(MSG_DEBUG, "P2P: Trying to force us to use the "
4888 "requested channel (%u MHz)", freq);
4889 *force_freq = freq;
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07004890 goto exit_ok;
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08004891 }
4892
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07004893 best_freq = wpas_p2p_pick_best_used_freq(wpa_s, freqs, num);
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07004894
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07004895 /* We have a candidate frequency to use */
4896 if (best_freq > 0) {
4897 if (*pref_freq == 0 && num_unused > 0) {
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08004898 wpa_printf(MSG_DEBUG, "P2P: Try to prefer a frequency (%u MHz) we are already using",
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07004899 best_freq);
4900 *pref_freq = best_freq;
Dmitry Shmidt51a47d52013-09-10 10:52:57 -07004901 } else {
4902 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 -07004903 best_freq);
4904 *force_freq = best_freq;
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07004905 }
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07004906 } else if (num_unused > 0) {
4907 wpa_printf(MSG_DEBUG,
4908 "P2P: Current operating channels are not available for P2P. Try to use another channel");
4909 *force_freq = 0;
4910 } else {
4911 wpa_printf(MSG_DEBUG,
4912 "P2P: All channels are in use and none of them are P2P enabled. Cannot start P2P group");
4913 res = -2;
4914 goto exit_free;
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07004915 }
4916
4917exit_ok:
4918 res = 0;
4919exit_free:
4920 os_free(freqs);
4921 return res;
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08004922}
4923
4924
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004925/**
4926 * wpas_p2p_connect - Request P2P Group Formation to be started
4927 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
4928 * @peer_addr: Address of the peer P2P Device
4929 * @pin: PIN to use during provisioning or %NULL to indicate PBC mode
4930 * @persistent_group: Whether to create a persistent group
Dmitry Shmidt04949592012-07-19 12:16:46 -07004931 * @auto_join: Whether to select join vs. GO Negotiation automatically
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004932 * @join: Whether to join an existing group (as a client) instead of starting
4933 * Group Owner negotiation; @peer_addr is BSSID in that case
4934 * @auth: Whether to only authorize the connection instead of doing that and
4935 * initiating Group Owner negotiation
4936 * @go_intent: GO Intent or -1 to use default
4937 * @freq: Frequency for the group or 0 for auto-selection
Dmitry Shmidt04949592012-07-19 12:16:46 -07004938 * @persistent_id: Persistent group credentials to use for forcing GO
4939 * parameters or -1 to generate new values (SSID/passphrase)
4940 * @pd: Whether to send Provision Discovery prior to GO Negotiation as an
4941 * interoperability workaround when initiating group formation
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004942 * @ht40: Start GO with 40 MHz channel width
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004943 * @vht: Start GO with VHT support
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004944 * Returns: 0 or new PIN (if pin was %NULL) on success, -1 on unspecified
4945 * failure, -2 on failure due to channel not currently available,
4946 * -3 if forced channel is not supported
4947 */
4948int wpas_p2p_connect(struct wpa_supplicant *wpa_s, const u8 *peer_addr,
4949 const char *pin, enum p2p_wps_method wps_method,
Dmitry Shmidt04949592012-07-19 12:16:46 -07004950 int persistent_group, int auto_join, int join, int auth,
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004951 int go_intent, int freq, int persistent_id, int pd,
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004952 int ht40, int vht)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004953{
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07004954 int force_freq = 0, pref_freq = 0;
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08004955 int ret = 0, res;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004956 enum wpa_driver_if_type iftype;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004957 const u8 *if_addr;
Dmitry Shmidt04949592012-07-19 12:16:46 -07004958 struct wpa_ssid *ssid = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004959
4960 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
4961 return -1;
4962
Dmitry Shmidt04949592012-07-19 12:16:46 -07004963 if (persistent_id >= 0) {
4964 ssid = wpa_config_get_network(wpa_s->conf, persistent_id);
4965 if (ssid == NULL || ssid->disabled != 2 ||
4966 ssid->mode != WPAS_MODE_P2P_GO)
4967 return -1;
4968 }
4969
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07004970 os_free(wpa_s->global->add_psk);
4971 wpa_s->global->add_psk = NULL;
4972
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08004973 wpa_s->global->p2p_fail_on_wps_complete = 0;
4974
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004975 if (go_intent < 0)
4976 go_intent = wpa_s->conf->p2p_go_intent;
4977
4978 if (!auth)
4979 wpa_s->p2p_long_listen = 0;
4980
4981 wpa_s->p2p_wps_method = wps_method;
Dmitry Shmidt04949592012-07-19 12:16:46 -07004982 wpa_s->p2p_persistent_group = !!persistent_group;
4983 wpa_s->p2p_persistent_id = persistent_id;
4984 wpa_s->p2p_go_intent = go_intent;
4985 wpa_s->p2p_connect_freq = freq;
4986 wpa_s->p2p_fallback_to_go_neg = 0;
4987 wpa_s->p2p_pd_before_go_neg = !!pd;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004988 wpa_s->p2p_go_ht40 = !!ht40;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004989 wpa_s->p2p_go_vht = !!vht;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004990
4991 if (pin)
4992 os_strlcpy(wpa_s->p2p_pin, pin, sizeof(wpa_s->p2p_pin));
4993 else if (wps_method == WPS_PIN_DISPLAY) {
4994 ret = wps_generate_pin();
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004995 res = os_snprintf(wpa_s->p2p_pin, sizeof(wpa_s->p2p_pin),
4996 "%08d", ret);
4997 if (os_snprintf_error(sizeof(wpa_s->p2p_pin), res))
4998 wpa_s->p2p_pin[sizeof(wpa_s->p2p_pin) - 1] = '\0';
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004999 wpa_printf(MSG_DEBUG, "P2P: Randomly generated PIN: %s",
5000 wpa_s->p2p_pin);
5001 } else
5002 wpa_s->p2p_pin[0] = '\0';
5003
Dmitry Shmidt04949592012-07-19 12:16:46 -07005004 if (join || auto_join) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005005 u8 iface_addr[ETH_ALEN], dev_addr[ETH_ALEN];
5006 if (auth) {
5007 wpa_printf(MSG_DEBUG, "P2P: Authorize invitation to "
5008 "connect a running group from " MACSTR,
5009 MAC2STR(peer_addr));
5010 os_memcpy(wpa_s->p2p_auth_invite, peer_addr, ETH_ALEN);
5011 return ret;
5012 }
5013 os_memcpy(dev_addr, peer_addr, ETH_ALEN);
5014 if (p2p_get_interface_addr(wpa_s->global->p2p, peer_addr,
5015 iface_addr) < 0) {
5016 os_memcpy(iface_addr, peer_addr, ETH_ALEN);
5017 p2p_get_dev_addr(wpa_s->global->p2p, peer_addr,
5018 dev_addr);
5019 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07005020 if (auto_join) {
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005021 os_get_reltime(&wpa_s->p2p_auto_started);
Dmitry Shmidt04949592012-07-19 12:16:46 -07005022 wpa_printf(MSG_DEBUG, "P2P: Auto join started at "
5023 "%ld.%06ld",
5024 wpa_s->p2p_auto_started.sec,
5025 wpa_s->p2p_auto_started.usec);
5026 }
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08005027 wpa_s->user_initiated_pd = 1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07005028 if (wpas_p2p_join(wpa_s, iface_addr, dev_addr, wps_method,
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08005029 auto_join, freq, NULL, 0) < 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005030 return -1;
5031 return ret;
5032 }
5033
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07005034 res = wpas_p2p_setup_freqs(wpa_s, freq, &force_freq, &pref_freq,
5035 go_intent == 15);
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08005036 if (res)
5037 return res;
Dmitry Shmidta0d265f2013-11-19 13:13:41 -08005038 wpas_p2p_set_own_freq_preference(wpa_s,
5039 force_freq ? force_freq : pref_freq);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005040
5041 wpa_s->create_p2p_iface = wpas_p2p_create_iface(wpa_s);
5042
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005043 if (wpa_s->create_p2p_iface) {
5044 /* Prepare to add a new interface for the group */
5045 iftype = WPA_IF_P2P_GROUP;
5046 if (go_intent == 15)
5047 iftype = WPA_IF_P2P_GO;
5048 if (wpas_p2p_add_group_interface(wpa_s, iftype) < 0) {
5049 wpa_printf(MSG_ERROR, "P2P: Failed to allocate a new "
5050 "interface for the group");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005051 return -1;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005052 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005053
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005054 if_addr = wpa_s->pending_interface_addr;
5055 } else
5056 if_addr = wpa_s->own_addr;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005057
5058 if (auth) {
5059 if (wpas_p2p_auth_go_neg(wpa_s, peer_addr, wps_method,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005060 go_intent, if_addr,
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08005061 force_freq, persistent_group, ssid,
5062 pref_freq) < 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005063 return -1;
5064 return ret;
5065 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005066
5067 if (wpas_p2p_start_go_neg(wpa_s, peer_addr, wps_method,
5068 go_intent, if_addr, force_freq,
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08005069 persistent_group, ssid, pref_freq) < 0) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005070 if (wpa_s->create_p2p_iface)
5071 wpas_p2p_remove_pending_group_interface(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005072 return -1;
5073 }
5074 return ret;
5075}
5076
5077
5078/**
5079 * wpas_p2p_remain_on_channel_cb - Indication of remain-on-channel start
5080 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
5081 * @freq: Frequency of the channel in MHz
5082 * @duration: Duration of the stay on the channel in milliseconds
5083 *
5084 * This callback is called when the driver indicates that it has started the
5085 * requested remain-on-channel duration.
5086 */
5087void wpas_p2p_remain_on_channel_cb(struct wpa_supplicant *wpa_s,
5088 unsigned int freq, unsigned int duration)
5089{
5090 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
5091 return;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005092 if (wpa_s->off_channel_freq == wpa_s->pending_listen_freq) {
5093 p2p_listen_cb(wpa_s->global->p2p, wpa_s->pending_listen_freq,
5094 wpa_s->pending_listen_duration);
5095 wpa_s->pending_listen_freq = 0;
Dmitry Shmidt7f93d6f2014-02-21 11:22:49 -08005096 } else {
5097 wpa_printf(MSG_DEBUG, "P2P: Ignore remain-on-channel callback (off_channel_freq=%u pending_listen_freq=%d freq=%u duration=%u)",
5098 wpa_s->off_channel_freq, wpa_s->pending_listen_freq,
5099 freq, duration);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005100 }
5101}
5102
5103
Jithu Jance57cea1a2014-08-20 10:22:04 -07005104int wpas_p2p_listen_start(struct wpa_supplicant *wpa_s, unsigned int timeout)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005105{
5106 /* Limit maximum Listen state time based on driver limitation. */
5107 if (timeout > wpa_s->max_remain_on_chan)
5108 timeout = wpa_s->max_remain_on_chan;
5109
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005110 return p2p_listen(wpa_s->global->p2p, timeout);
5111}
5112
5113
5114/**
5115 * wpas_p2p_cancel_remain_on_channel_cb - Remain-on-channel timeout
5116 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
5117 * @freq: Frequency of the channel in MHz
5118 *
5119 * This callback is called when the driver indicates that a remain-on-channel
5120 * operation has been completed, i.e., the duration on the requested channel
5121 * has timed out.
5122 */
5123void wpas_p2p_cancel_remain_on_channel_cb(struct wpa_supplicant *wpa_s,
5124 unsigned int freq)
5125{
5126 wpa_printf(MSG_DEBUG, "P2P: Cancel remain-on-channel callback "
5127 "(p2p_long_listen=%d ms pending_action_tx=%p)",
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08005128 wpa_s->p2p_long_listen, offchannel_pending_action_tx(wpa_s));
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005129 wpas_p2p_listen_work_done(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005130 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
5131 return;
Jithu Jance57cea1a2014-08-20 10:22:04 -07005132 if (wpa_s->p2p_long_listen > 0)
5133 wpa_s->p2p_long_listen -= wpa_s->max_remain_on_chan;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005134 if (p2p_listen_end(wpa_s->global->p2p, freq) > 0)
5135 return; /* P2P module started a new operation */
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08005136 if (offchannel_pending_action_tx(wpa_s))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005137 return;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005138 if (wpa_s->p2p_long_listen > 0) {
5139 wpa_printf(MSG_DEBUG, "P2P: Continuing long Listen state");
5140 wpas_p2p_listen_start(wpa_s, wpa_s->p2p_long_listen);
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07005141 } else {
5142 /*
5143 * When listen duration is over, stop listen & update p2p_state
5144 * to IDLE.
5145 */
5146 p2p_stop_listen(wpa_s->global->p2p);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005147 }
5148}
5149
5150
5151/**
5152 * wpas_p2p_group_remove - Remove a P2P group
5153 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
5154 * @ifname: Network interface name of the group interface or "*" to remove all
5155 * groups
5156 * Returns: 0 on success, -1 on failure
5157 *
5158 * This function is used to remove a P2P group. This can be used to disconnect
5159 * from a group in which the local end is a P2P Client or to end a P2P Group in
5160 * case the local end is the Group Owner. If a virtual network interface was
5161 * created for this group, that interface will be removed. Otherwise, only the
5162 * configured P2P group network will be removed from the interface.
5163 */
5164int wpas_p2p_group_remove(struct wpa_supplicant *wpa_s, const char *ifname)
5165{
5166 struct wpa_global *global = wpa_s->global;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005167 struct wpa_supplicant *calling_wpa_s = wpa_s;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005168
5169 if (os_strcmp(ifname, "*") == 0) {
5170 struct wpa_supplicant *prev;
5171 wpa_s = global->ifaces;
5172 while (wpa_s) {
5173 prev = wpa_s;
5174 wpa_s = wpa_s->next;
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07005175 if (prev->p2p_group_interface !=
5176 NOT_P2P_GROUP_INTERFACE ||
5177 (prev->current_ssid &&
5178 prev->current_ssid->p2p_group))
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005179 wpas_p2p_disconnect_safely(prev, calling_wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005180 }
5181 return 0;
5182 }
5183
5184 for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
5185 if (os_strcmp(wpa_s->ifname, ifname) == 0)
5186 break;
5187 }
5188
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005189 return wpas_p2p_disconnect_safely(wpa_s, calling_wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005190}
5191
5192
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07005193static int wpas_p2p_select_go_freq(struct wpa_supplicant *wpa_s, int freq)
5194{
5195 unsigned int r;
5196
5197 if (freq == 2) {
5198 wpa_printf(MSG_DEBUG, "P2P: Request to start GO on 2.4 GHz "
5199 "band");
5200 if (wpa_s->best_24_freq > 0 &&
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07005201 p2p_supported_freq_go(wpa_s->global->p2p,
5202 wpa_s->best_24_freq)) {
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07005203 freq = wpa_s->best_24_freq;
5204 wpa_printf(MSG_DEBUG, "P2P: Use best 2.4 GHz band "
5205 "channel: %d MHz", freq);
5206 } else {
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07005207 if (os_get_random((u8 *) &r, sizeof(r)) < 0)
5208 return -1;
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07005209 freq = 2412 + (r % 3) * 25;
5210 wpa_printf(MSG_DEBUG, "P2P: Use random 2.4 GHz band "
5211 "channel: %d MHz", freq);
5212 }
5213 }
5214
5215 if (freq == 5) {
5216 wpa_printf(MSG_DEBUG, "P2P: Request to start GO on 5 GHz "
5217 "band");
5218 if (wpa_s->best_5_freq > 0 &&
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07005219 p2p_supported_freq_go(wpa_s->global->p2p,
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07005220 wpa_s->best_5_freq)) {
5221 freq = wpa_s->best_5_freq;
5222 wpa_printf(MSG_DEBUG, "P2P: Use best 5 GHz band "
5223 "channel: %d MHz", freq);
5224 } else {
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07005225 if (os_get_random((u8 *) &r, sizeof(r)) < 0)
5226 return -1;
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07005227 freq = 5180 + (r % 4) * 20;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07005228 if (!p2p_supported_freq_go(wpa_s->global->p2p, freq)) {
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07005229 wpa_printf(MSG_DEBUG, "P2P: Could not select "
5230 "5 GHz channel for P2P group");
5231 return -1;
5232 }
5233 wpa_printf(MSG_DEBUG, "P2P: Use random 5 GHz band "
5234 "channel: %d MHz", freq);
5235 }
5236 }
5237
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07005238 if (freq > 0 && !p2p_supported_freq_go(wpa_s->global->p2p, freq)) {
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07005239 wpa_printf(MSG_DEBUG, "P2P: The forced channel for GO "
5240 "(%u MHz) is not supported for P2P uses",
5241 freq);
5242 return -1;
5243 }
5244
5245 return freq;
5246}
5247
5248
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07005249static int wpas_p2p_select_freq_no_pref(struct wpa_supplicant *wpa_s,
5250 struct p2p_go_neg_results *params,
5251 const struct p2p_channels *channels)
5252{
5253 unsigned int i, r;
5254
5255 /* first try some random selection of the social channels */
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07005256 if (os_get_random((u8 *) &r, sizeof(r)) < 0)
5257 return -1;
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07005258
5259 for (i = 0; i < 3; i++) {
5260 params->freq = 2412 + ((r + i) % 3) * 25;
5261 if (!wpas_p2p_disallowed_freq(wpa_s->global, params->freq) &&
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07005262 freq_included(channels, params->freq) &&
5263 p2p_supported_freq(wpa_s->global->p2p, params->freq))
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07005264 goto out;
5265 }
5266
5267 /* try all channels in reg. class 81 */
5268 for (i = 0; i < 11; i++) {
5269 params->freq = 2412 + i * 5;
5270 if (!wpas_p2p_disallowed_freq(wpa_s->global, params->freq) &&
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07005271 freq_included(channels, params->freq) &&
5272 p2p_supported_freq(wpa_s->global->p2p, params->freq))
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07005273 goto out;
5274 }
5275
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07005276 /* try social channel class 180 channel 2 */
5277 params->freq = 58320 + 1 * 2160;
5278 if (!wpas_p2p_disallowed_freq(wpa_s->global, params->freq) &&
5279 freq_included(channels, params->freq) &&
5280 p2p_supported_freq(wpa_s->global->p2p, params->freq))
5281 goto out;
5282
5283 /* try all channels in reg. class 180 */
5284 for (i = 0; i < 4; i++) {
5285 params->freq = 58320 + i * 2160;
5286 if (!wpas_p2p_disallowed_freq(wpa_s->global, params->freq) &&
5287 freq_included(channels, params->freq) &&
5288 p2p_supported_freq(wpa_s->global->p2p, params->freq))
5289 goto out;
5290 }
5291
5292 wpa_printf(MSG_DEBUG, "P2P: No 2.4 and 60 GHz channel allowed");
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07005293 return -1;
5294out:
5295 wpa_printf(MSG_DEBUG, "P2P: Set GO freq %d MHz (no preference known)",
5296 params->freq);
5297 return 0;
5298}
5299
5300
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005301static int wpas_p2p_init_go_params(struct wpa_supplicant *wpa_s,
5302 struct p2p_go_neg_results *params,
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07005303 int freq, int ht40, int vht,
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08005304 const struct p2p_channels *channels)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005305{
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07005306 struct wpa_used_freq_data *freqs;
5307 unsigned int pref_freq, cand_freq;
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07005308 unsigned int num, i;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005309
5310 os_memset(params, 0, sizeof(*params));
5311 params->role_go = 1;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005312 params->ht40 = ht40;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07005313 params->vht = vht;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005314 if (freq) {
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08005315 if (!freq_included(channels, freq)) {
5316 wpa_printf(MSG_DEBUG, "P2P: Forced GO freq %d MHz not "
5317 "accepted", freq);
5318 return -1;
5319 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005320 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on forced "
5321 "frequency %d MHz", freq);
5322 params->freq = freq;
5323 } else if (wpa_s->conf->p2p_oper_reg_class == 81 &&
5324 wpa_s->conf->p2p_oper_channel >= 1 &&
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08005325 wpa_s->conf->p2p_oper_channel <= 11 &&
5326 freq_included(channels,
5327 2407 + 5 * wpa_s->conf->p2p_oper_channel)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005328 params->freq = 2407 + 5 * wpa_s->conf->p2p_oper_channel;
5329 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on configured "
5330 "frequency %d MHz", params->freq);
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08005331 } else if ((wpa_s->conf->p2p_oper_reg_class == 115 ||
5332 wpa_s->conf->p2p_oper_reg_class == 116 ||
5333 wpa_s->conf->p2p_oper_reg_class == 117 ||
5334 wpa_s->conf->p2p_oper_reg_class == 124 ||
5335 wpa_s->conf->p2p_oper_reg_class == 126 ||
5336 wpa_s->conf->p2p_oper_reg_class == 127) &&
5337 freq_included(channels,
5338 5000 + 5 * wpa_s->conf->p2p_oper_channel)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005339 params->freq = 5000 + 5 * wpa_s->conf->p2p_oper_channel;
5340 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on configured "
5341 "frequency %d MHz", params->freq);
5342 } else if (wpa_s->conf->p2p_oper_channel == 0 &&
5343 wpa_s->best_overall_freq > 0 &&
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07005344 p2p_supported_freq_go(wpa_s->global->p2p,
5345 wpa_s->best_overall_freq) &&
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08005346 freq_included(channels, wpa_s->best_overall_freq)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005347 params->freq = wpa_s->best_overall_freq;
5348 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on best overall "
5349 "channel %d MHz", params->freq);
5350 } else if (wpa_s->conf->p2p_oper_channel == 0 &&
5351 wpa_s->best_24_freq > 0 &&
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07005352 p2p_supported_freq_go(wpa_s->global->p2p,
5353 wpa_s->best_24_freq) &&
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08005354 freq_included(channels, wpa_s->best_24_freq)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005355 params->freq = wpa_s->best_24_freq;
5356 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on best 2.4 GHz "
5357 "channel %d MHz", params->freq);
5358 } else if (wpa_s->conf->p2p_oper_channel == 0 &&
5359 wpa_s->best_5_freq > 0 &&
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07005360 p2p_supported_freq_go(wpa_s->global->p2p,
5361 wpa_s->best_5_freq) &&
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08005362 freq_included(channels, wpa_s->best_5_freq)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005363 params->freq = wpa_s->best_5_freq;
5364 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on best 5 GHz "
5365 "channel %d MHz", params->freq);
Dmitry Shmidt44c95782013-05-17 09:51:35 -07005366 } else if ((pref_freq = p2p_get_pref_freq(wpa_s->global->p2p,
5367 channels))) {
5368 params->freq = pref_freq;
5369 wpa_printf(MSG_DEBUG, "P2P: Set GO freq %d MHz from preferred "
5370 "channels", params->freq);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005371 } else {
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07005372 /* no preference, select some channel */
5373 if (wpas_p2p_select_freq_no_pref(wpa_s, params, channels) < 0)
Dmitry Shmidt04949592012-07-19 12:16:46 -07005374 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005375 }
5376
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07005377 freqs = os_calloc(wpa_s->num_multichan_concurrent,
5378 sizeof(struct wpa_used_freq_data));
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07005379 if (!freqs)
5380 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005381
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07005382 num = wpas_p2p_valid_oper_freqs(wpa_s, freqs,
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07005383 wpa_s->num_multichan_concurrent);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005384
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07005385 cand_freq = wpas_p2p_pick_best_used_freq(wpa_s, freqs, num);
5386
5387 /* First try the best used frequency if possible */
5388 if (!freq && cand_freq > 0 && freq_included(channels, cand_freq)) {
5389 params->freq = cand_freq;
5390 } else if (!freq) {
5391 /* Try any of the used frequencies */
5392 for (i = 0; i < num; i++) {
5393 if (freq_included(channels, freqs[i].freq)) {
5394 wpa_printf(MSG_DEBUG, "P2P: Force GO on a channel we are already using (%u MHz)",
5395 freqs[i].freq);
5396 params->freq = freqs[i].freq;
5397 break;
5398 }
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07005399 }
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08005400
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07005401 if (i == num) {
5402 if (wpas_p2p_num_unused_channels(wpa_s) <= 0) {
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08005403 wpa_printf(MSG_DEBUG, "P2P: Cannot force GO on any of the channels we are already using");
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07005404 os_free(freqs);
5405 return -1;
5406 } else {
5407 wpa_printf(MSG_DEBUG, "P2P: Cannot force GO on any of the channels we are already using. Use one of the free channels");
5408 }
5409 }
5410 } else {
5411 for (i = 0; i < num; i++) {
5412 if (freqs[i].freq == freq)
5413 break;
5414 }
5415
5416 if (i == num) {
5417 if (wpas_p2p_num_unused_channels(wpa_s) <= 0) {
5418 if (freq)
5419 wpa_printf(MSG_DEBUG, "P2P: Cannot force GO on freq (%u MHz) as all the channels are in use", freq);
5420 os_free(freqs);
5421 return -1;
5422 } else {
5423 wpa_printf(MSG_DEBUG, "P2P: Use one of the free channels");
5424 }
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08005425 }
5426 }
5427
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07005428 os_free(freqs);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005429 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005430}
5431
5432
5433static struct wpa_supplicant *
5434wpas_p2p_get_group_iface(struct wpa_supplicant *wpa_s, int addr_allocated,
5435 int go)
5436{
5437 struct wpa_supplicant *group_wpa_s;
5438
Dmitry Shmidtaa532512012-09-24 10:35:31 -07005439 if (!wpas_p2p_create_iface(wpa_s)) {
5440 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Use same interface for group "
5441 "operations");
Dmitry Shmidt56052862013-10-04 10:23:25 -07005442 wpa_s->p2p_first_connection_timeout = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005443 return wpa_s;
Dmitry Shmidtaa532512012-09-24 10:35:31 -07005444 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005445
5446 if (wpas_p2p_add_group_interface(wpa_s, go ? WPA_IF_P2P_GO :
Dmitry Shmidtaa532512012-09-24 10:35:31 -07005447 WPA_IF_P2P_CLIENT) < 0) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07005448 wpa_msg_global(wpa_s, MSG_ERROR,
5449 "P2P: Failed to add group interface");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005450 return NULL;
Dmitry Shmidtaa532512012-09-24 10:35:31 -07005451 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005452 group_wpa_s = wpas_p2p_init_group_interface(wpa_s, go);
5453 if (group_wpa_s == NULL) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07005454 wpa_msg_global(wpa_s, MSG_ERROR,
5455 "P2P: Failed to initialize group interface");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005456 wpas_p2p_remove_pending_group_interface(wpa_s);
5457 return NULL;
5458 }
5459
Dmitry Shmidtaa532512012-09-24 10:35:31 -07005460 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Use separate group interface %s",
5461 group_wpa_s->ifname);
Dmitry Shmidt56052862013-10-04 10:23:25 -07005462 group_wpa_s->p2p_first_connection_timeout = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005463 return group_wpa_s;
5464}
5465
5466
5467/**
5468 * wpas_p2p_group_add - Add a new P2P group with local end as Group Owner
5469 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
5470 * @persistent_group: Whether to create a persistent group
5471 * @freq: Frequency for the group or 0 to indicate no hardcoding
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07005472 * @ht40: Start GO with 40 MHz channel width
5473 * @vht: Start GO with VHT support
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005474 * Returns: 0 on success, -1 on failure
5475 *
5476 * This function creates a new P2P group with the local end as the Group Owner,
5477 * i.e., without using Group Owner Negotiation.
5478 */
5479int wpas_p2p_group_add(struct wpa_supplicant *wpa_s, int persistent_group,
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07005480 int freq, int ht40, int vht)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005481{
5482 struct p2p_go_neg_results params;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005483
5484 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
5485 return -1;
5486
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07005487 os_free(wpa_s->global->add_psk);
5488 wpa_s->global->add_psk = NULL;
5489
Dmitry Shmidtdca39792011-09-06 11:17:33 -07005490 /* Make sure we are not running find during connection establishment */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005491 wpa_printf(MSG_DEBUG, "P2P: Stop any on-going P2P FIND");
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08005492 wpas_p2p_stop_find_oper(wpa_s);
Dmitry Shmidtdca39792011-09-06 11:17:33 -07005493
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07005494 freq = wpas_p2p_select_go_freq(wpa_s, freq);
5495 if (freq < 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005496 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005497
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07005498 if (wpas_p2p_init_go_params(wpa_s, &params, freq, ht40, vht, NULL))
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005499 return -1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07005500 if (params.freq &&
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07005501 !p2p_supported_freq_go(wpa_s->global->p2p, params.freq)) {
Dmitry Shmidt04949592012-07-19 12:16:46 -07005502 wpa_printf(MSG_DEBUG, "P2P: The selected channel for GO "
5503 "(%u MHz) is not supported for P2P uses",
5504 params.freq);
5505 return -1;
5506 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005507 p2p_go_params(wpa_s->global->p2p, &params);
5508 params.persistent_group = persistent_group;
5509
5510 wpa_s = wpas_p2p_get_group_iface(wpa_s, 0, 1);
5511 if (wpa_s == NULL)
5512 return -1;
5513 wpas_start_wps_go(wpa_s, &params, 0);
5514
5515 return 0;
5516}
5517
5518
5519static int wpas_start_p2p_client(struct wpa_supplicant *wpa_s,
Dmitry Shmidt15907092014-03-25 10:42:57 -07005520 struct wpa_ssid *params, int addr_allocated,
5521 int freq)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005522{
5523 struct wpa_ssid *ssid;
5524
5525 wpa_s = wpas_p2p_get_group_iface(wpa_s, addr_allocated, 0);
5526 if (wpa_s == NULL)
5527 return -1;
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07005528 wpa_s->p2p_last_4way_hs_fail = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005529
5530 wpa_supplicant_ap_deinit(wpa_s);
5531
5532 ssid = wpa_config_add_network(wpa_s->conf);
5533 if (ssid == NULL)
5534 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005535 wpa_config_set_network_defaults(ssid);
5536 ssid->temporary = 1;
5537 ssid->proto = WPA_PROTO_RSN;
5538 ssid->pairwise_cipher = WPA_CIPHER_CCMP;
5539 ssid->group_cipher = WPA_CIPHER_CCMP;
5540 ssid->key_mgmt = WPA_KEY_MGMT_PSK;
5541 ssid->ssid = os_malloc(params->ssid_len);
5542 if (ssid->ssid == NULL) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005543 wpa_config_remove_network(wpa_s->conf, ssid->id);
5544 return -1;
5545 }
5546 os_memcpy(ssid->ssid, params->ssid, params->ssid_len);
5547 ssid->ssid_len = params->ssid_len;
5548 ssid->p2p_group = 1;
5549 ssid->export_keys = 1;
5550 if (params->psk_set) {
5551 os_memcpy(ssid->psk, params->psk, 32);
5552 ssid->psk_set = 1;
5553 }
5554 if (params->passphrase)
5555 ssid->passphrase = os_strdup(params->passphrase);
5556
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005557 wpa_s->show_group_started = 1;
Dmitry Shmidt15907092014-03-25 10:42:57 -07005558 wpa_s->p2p_in_invitation = 1;
5559 wpa_s->p2p_invite_go_freq = freq;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005560
Dmitry Shmidt15907092014-03-25 10:42:57 -07005561 eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s->parent,
5562 NULL);
5563 eloop_register_timeout(P2P_MAX_INITIAL_CONN_WAIT, 0,
5564 wpas_p2p_group_formation_timeout,
5565 wpa_s->parent, NULL);
Dmitry Shmidtfa3fc4a2013-11-21 13:34:38 -08005566 wpa_supplicant_select_network(wpa_s, ssid);
5567
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005568 return 0;
5569}
5570
5571
5572int wpas_p2p_group_add_persistent(struct wpa_supplicant *wpa_s,
5573 struct wpa_ssid *ssid, int addr_allocated,
Dmitry Shmidt96be6222014-02-13 10:16:51 -08005574 int force_freq, int neg_freq, int ht40,
5575 int vht, const struct p2p_channels *channels,
Dmitry Shmidt56052862013-10-04 10:23:25 -07005576 int connection_timeout)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005577{
5578 struct p2p_go_neg_results params;
Dmitry Shmidt96be6222014-02-13 10:16:51 -08005579 int go = 0, freq;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005580
5581 if (ssid->disabled != 2 || ssid->ssid == NULL)
5582 return -1;
5583
5584 if (wpas_get_p2p_group(wpa_s, ssid->ssid, ssid->ssid_len, &go) &&
5585 go == (ssid->mode == WPAS_MODE_P2P_GO)) {
5586 wpa_printf(MSG_DEBUG, "P2P: Requested persistent group is "
5587 "already running");
5588 return 0;
5589 }
5590
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07005591 os_free(wpa_s->global->add_psk);
5592 wpa_s->global->add_psk = NULL;
5593
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005594 /* Make sure we are not running find during connection establishment */
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08005595 wpas_p2p_stop_find_oper(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005596
Dmitry Shmidt04949592012-07-19 12:16:46 -07005597 wpa_s->p2p_fallback_to_go_neg = 0;
5598
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005599 if (ssid->mode == WPAS_MODE_P2P_GO) {
5600 if (force_freq > 0) {
5601 freq = wpas_p2p_select_go_freq(wpa_s, force_freq);
5602 if (freq < 0)
5603 return -1;
5604 } else {
5605 freq = wpas_p2p_select_go_freq(wpa_s, neg_freq);
5606 if (freq < 0 ||
5607 (freq > 0 && !freq_included(channels, freq)))
5608 freq = 0;
5609 }
Dmitry Shmidt96be6222014-02-13 10:16:51 -08005610 } else {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005611 freq = neg_freq;
5612 if (freq < 0 ||
5613 (freq > 0 && !freq_included(channels, freq)))
Dmitry Shmidt96be6222014-02-13 10:16:51 -08005614 freq = 0;
5615 }
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07005616
Dmitry Shmidt15907092014-03-25 10:42:57 -07005617 if (ssid->mode == WPAS_MODE_INFRA)
5618 return wpas_start_p2p_client(wpa_s, ssid, addr_allocated, freq);
5619
5620 if (ssid->mode != WPAS_MODE_P2P_GO)
5621 return -1;
5622
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07005623 if (wpas_p2p_init_go_params(wpa_s, &params, freq, ht40, vht, channels))
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005624 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005625
5626 params.role_go = 1;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08005627 params.psk_set = ssid->psk_set;
5628 if (params.psk_set)
5629 os_memcpy(params.psk, ssid->psk, sizeof(params.psk));
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08005630 if (ssid->passphrase) {
5631 if (os_strlen(ssid->passphrase) >= sizeof(params.passphrase)) {
5632 wpa_printf(MSG_ERROR, "P2P: Invalid passphrase in "
5633 "persistent group");
5634 return -1;
5635 }
5636 os_strlcpy(params.passphrase, ssid->passphrase,
5637 sizeof(params.passphrase));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005638 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005639 os_memcpy(params.ssid, ssid->ssid, ssid->ssid_len);
5640 params.ssid_len = ssid->ssid_len;
5641 params.persistent_group = 1;
5642
5643 wpa_s = wpas_p2p_get_group_iface(wpa_s, addr_allocated, 1);
5644 if (wpa_s == NULL)
5645 return -1;
5646
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005647 p2p_channels_to_freqs(channels, params.freq_list, P2P_MAX_CHANNELS);
5648
Dmitry Shmidt56052862013-10-04 10:23:25 -07005649 wpa_s->p2p_first_connection_timeout = connection_timeout;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005650 wpas_start_wps_go(wpa_s, &params, 0);
5651
5652 return 0;
5653}
5654
5655
5656static void wpas_p2p_ie_update(void *ctx, struct wpabuf *beacon_ies,
5657 struct wpabuf *proberesp_ies)
5658{
5659 struct wpa_supplicant *wpa_s = ctx;
5660 if (wpa_s->ap_iface) {
5661 struct hostapd_data *hapd = wpa_s->ap_iface->bss[0];
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005662 if (!(hapd->conf->p2p & P2P_GROUP_OWNER)) {
5663 wpabuf_free(beacon_ies);
5664 wpabuf_free(proberesp_ies);
5665 return;
5666 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005667 if (beacon_ies) {
5668 wpabuf_free(hapd->p2p_beacon_ie);
5669 hapd->p2p_beacon_ie = beacon_ies;
5670 }
5671 wpabuf_free(hapd->p2p_probe_resp_ie);
5672 hapd->p2p_probe_resp_ie = proberesp_ies;
5673 } else {
5674 wpabuf_free(beacon_ies);
5675 wpabuf_free(proberesp_ies);
5676 }
5677 wpa_supplicant_ap_update_beacon(wpa_s);
5678}
5679
5680
5681static void wpas_p2p_idle_update(void *ctx, int idle)
5682{
5683 struct wpa_supplicant *wpa_s = ctx;
5684 if (!wpa_s->ap_iface)
5685 return;
5686 wpa_printf(MSG_DEBUG, "P2P: GO - group %sidle", idle ? "" : "not ");
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005687 if (idle) {
5688 if (wpa_s->global->p2p_fail_on_wps_complete &&
5689 wpa_s->p2p_in_provisioning) {
5690 wpas_p2p_grpform_fail_after_wps(wpa_s);
5691 return;
5692 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005693 wpas_p2p_set_group_idle_timeout(wpa_s);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005694 } else
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005695 eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL);
5696}
5697
5698
5699struct p2p_group * wpas_p2p_group_init(struct wpa_supplicant *wpa_s,
Dmitry Shmidt04949592012-07-19 12:16:46 -07005700 struct wpa_ssid *ssid)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005701{
5702 struct p2p_group *group;
5703 struct p2p_group_config *cfg;
5704
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005705 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
5706 return NULL;
5707
5708 cfg = os_zalloc(sizeof(*cfg));
5709 if (cfg == NULL)
5710 return NULL;
5711
Dmitry Shmidt04949592012-07-19 12:16:46 -07005712 if (ssid->p2p_persistent_group && wpa_s->conf->persistent_reconnect)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005713 cfg->persistent_group = 2;
Dmitry Shmidt04949592012-07-19 12:16:46 -07005714 else if (ssid->p2p_persistent_group)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005715 cfg->persistent_group = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005716 os_memcpy(cfg->interface_addr, wpa_s->own_addr, ETH_ALEN);
5717 if (wpa_s->max_stations &&
5718 wpa_s->max_stations < wpa_s->conf->max_num_sta)
5719 cfg->max_clients = wpa_s->max_stations;
5720 else
5721 cfg->max_clients = wpa_s->conf->max_num_sta;
Dmitry Shmidt04949592012-07-19 12:16:46 -07005722 os_memcpy(cfg->ssid, ssid->ssid, ssid->ssid_len);
5723 cfg->ssid_len = ssid->ssid_len;
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08005724 cfg->freq = ssid->frequency;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005725 cfg->cb_ctx = wpa_s;
5726 cfg->ie_update = wpas_p2p_ie_update;
5727 cfg->idle_update = wpas_p2p_idle_update;
5728
5729 group = p2p_group_init(wpa_s->global->p2p, cfg);
5730 if (group == NULL)
5731 os_free(cfg);
Dmitry Shmidt04949592012-07-19 12:16:46 -07005732 if (ssid->mode != WPAS_MODE_P2P_GROUP_FORMATION)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005733 p2p_group_notif_formation_done(group);
5734 wpa_s->p2p_group = group;
5735 return group;
5736}
5737
5738
5739void wpas_p2p_wps_success(struct wpa_supplicant *wpa_s, const u8 *peer_addr,
5740 int registrar)
5741{
Dmitry Shmidt04949592012-07-19 12:16:46 -07005742 struct wpa_ssid *ssid = wpa_s->current_ssid;
5743
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005744 if (!wpa_s->p2p_in_provisioning) {
5745 wpa_printf(MSG_DEBUG, "P2P: Ignore WPS success event - P2P "
5746 "provisioning not in progress");
5747 return;
5748 }
5749
Dmitry Shmidt04949592012-07-19 12:16:46 -07005750 if (ssid && ssid->mode == WPAS_MODE_INFRA) {
5751 u8 go_dev_addr[ETH_ALEN];
5752 os_memcpy(go_dev_addr, wpa_s->bssid, ETH_ALEN);
5753 wpas_p2p_persistent_group(wpa_s, go_dev_addr, ssid->ssid,
5754 ssid->ssid_len);
5755 /* Clear any stored provisioning info */
5756 p2p_clear_provisioning_info(wpa_s->global->p2p, go_dev_addr);
5757 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005758
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005759 eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s->parent,
5760 NULL);
Dmitry Shmidt56052862013-10-04 10:23:25 -07005761 wpa_s->p2p_go_group_formation_completed = 1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07005762 if (ssid && ssid->mode == WPAS_MODE_INFRA) {
5763 /*
5764 * Use a separate timeout for initial data connection to
5765 * complete to allow the group to be removed automatically if
5766 * something goes wrong in this step before the P2P group idle
5767 * timeout mechanism is taken into use.
5768 */
Dmitry Shmidt56052862013-10-04 10:23:25 -07005769 wpa_dbg(wpa_s, MSG_DEBUG,
5770 "P2P: Re-start group formation timeout (%d seconds) as client for initial connection",
5771 P2P_MAX_INITIAL_CONN_WAIT);
Dmitry Shmidt04949592012-07-19 12:16:46 -07005772 eloop_register_timeout(P2P_MAX_INITIAL_CONN_WAIT, 0,
5773 wpas_p2p_group_formation_timeout,
5774 wpa_s->parent, NULL);
Dmitry Shmidt56052862013-10-04 10:23:25 -07005775 } else if (ssid) {
5776 /*
5777 * Use a separate timeout for initial data connection to
5778 * complete to allow the group to be removed automatically if
5779 * the client does not complete data connection successfully.
5780 */
5781 wpa_dbg(wpa_s, MSG_DEBUG,
5782 "P2P: Re-start group formation timeout (%d seconds) as GO for initial connection",
5783 P2P_MAX_INITIAL_CONN_WAIT_GO);
5784 eloop_register_timeout(P2P_MAX_INITIAL_CONN_WAIT_GO, 0,
5785 wpas_p2p_group_formation_timeout,
5786 wpa_s->parent, NULL);
5787 /*
5788 * Complete group formation on first successful data connection
5789 */
5790 wpa_s->p2p_go_group_formation_completed = 0;
Dmitry Shmidt04949592012-07-19 12:16:46 -07005791 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005792 if (wpa_s->global->p2p)
5793 p2p_wps_success_cb(wpa_s->global->p2p, peer_addr);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005794 wpas_group_formation_completed(wpa_s, 1);
5795}
5796
5797
Jouni Malinen75ecf522011-06-27 15:19:46 -07005798void wpas_p2p_wps_failed(struct wpa_supplicant *wpa_s,
5799 struct wps_event_fail *fail)
5800{
5801 if (!wpa_s->p2p_in_provisioning) {
5802 wpa_printf(MSG_DEBUG, "P2P: Ignore WPS fail event - P2P "
5803 "provisioning not in progress");
5804 return;
5805 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005806
5807 if (wpa_s->go_params) {
5808 p2p_clear_provisioning_info(
5809 wpa_s->global->p2p,
Dmitry Shmidt04949592012-07-19 12:16:46 -07005810 wpa_s->go_params->peer_device_addr);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005811 }
5812
Jouni Malinen75ecf522011-06-27 15:19:46 -07005813 wpas_notify_p2p_wps_failed(wpa_s, fail);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005814
5815 if (wpa_s == wpa_s->global->p2p_group_formation) {
5816 /*
5817 * Allow some time for the failed WPS negotiation exchange to
5818 * complete, but remove the group since group formation cannot
5819 * succeed after provisioning failure.
5820 */
5821 wpa_printf(MSG_DEBUG, "P2P: WPS step failed during group formation - reject connection from timeout");
5822 wpa_s->global->p2p_fail_on_wps_complete = 1;
5823 eloop_deplete_timeout(0, 50000,
5824 wpas_p2p_group_formation_timeout,
5825 wpa_s->parent, NULL);
5826 }
5827}
5828
5829
5830int wpas_p2p_wps_eapol_cb(struct wpa_supplicant *wpa_s)
5831{
5832 if (!wpa_s->global->p2p_fail_on_wps_complete ||
5833 !wpa_s->p2p_in_provisioning)
5834 return 0;
5835
5836 wpas_p2p_grpform_fail_after_wps(wpa_s);
5837
5838 return 1;
Jouni Malinen75ecf522011-06-27 15:19:46 -07005839}
5840
5841
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005842int wpas_p2p_prov_disc(struct wpa_supplicant *wpa_s, const u8 *peer_addr,
Dmitry Shmidt04949592012-07-19 12:16:46 -07005843 const char *config_method,
5844 enum wpas_p2p_prov_disc_use use)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005845{
5846 u16 config_methods;
5847
Dmitry Shmidt04949592012-07-19 12:16:46 -07005848 wpa_s->p2p_fallback_to_go_neg = 0;
5849 wpa_s->pending_pd_use = NORMAL_PD;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005850 if (os_strncmp(config_method, "display", 7) == 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005851 config_methods = WPS_CONFIG_DISPLAY;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005852 else if (os_strncmp(config_method, "keypad", 6) == 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005853 config_methods = WPS_CONFIG_KEYPAD;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005854 else if (os_strncmp(config_method, "pbc", 3) == 0 ||
5855 os_strncmp(config_method, "pushbutton", 10) == 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005856 config_methods = WPS_CONFIG_PUSHBUTTON;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005857 else {
5858 wpa_printf(MSG_DEBUG, "P2P: Unknown config method");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005859 return -1;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005860 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005861
Dmitry Shmidt04949592012-07-19 12:16:46 -07005862 if (use == WPAS_P2P_PD_AUTO) {
5863 os_memcpy(wpa_s->pending_join_dev_addr, peer_addr, ETH_ALEN);
5864 wpa_s->pending_pd_config_methods = config_methods;
5865 wpa_s->p2p_auto_pd = 1;
5866 wpa_s->p2p_auto_join = 0;
5867 wpa_s->pending_pd_before_join = 0;
5868 wpa_s->auto_pd_scan_retry = 0;
5869 wpas_p2p_stop_find(wpa_s);
5870 wpa_s->p2p_join_scan_count = 0;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005871 os_get_reltime(&wpa_s->p2p_auto_started);
Dmitry Shmidt04949592012-07-19 12:16:46 -07005872 wpa_printf(MSG_DEBUG, "P2P: Auto PD started at %ld.%06ld",
5873 wpa_s->p2p_auto_started.sec,
5874 wpa_s->p2p_auto_started.usec);
5875 wpas_p2p_join_scan(wpa_s, NULL);
5876 return 0;
5877 }
5878
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005879 if (wpa_s->global->p2p == NULL || wpa_s->global->p2p_disabled)
5880 return -1;
5881
5882 return p2p_prov_disc_req(wpa_s->global->p2p, peer_addr,
Dmitry Shmidt04949592012-07-19 12:16:46 -07005883 config_methods, use == WPAS_P2P_PD_FOR_JOIN,
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08005884 0, 1);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005885}
5886
5887
5888int wpas_p2p_scan_result_text(const u8 *ies, size_t ies_len, char *buf,
5889 char *end)
5890{
5891 return p2p_scan_result_text(ies, ies_len, buf, end);
5892}
5893
5894
5895static void wpas_p2p_clear_pending_action_tx(struct wpa_supplicant *wpa_s)
5896{
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08005897 if (!offchannel_pending_action_tx(wpa_s))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005898 return;
5899
Dmitry Shmidt03658832014-08-13 11:03:49 -07005900 wpas_p2p_action_tx_clear(wpa_s);
5901
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005902 wpa_printf(MSG_DEBUG, "P2P: Drop pending Action TX due to new "
5903 "operation request");
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08005904 offchannel_clear_pending_action_tx(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005905}
5906
5907
5908int wpas_p2p_find(struct wpa_supplicant *wpa_s, unsigned int timeout,
5909 enum p2p_discovery_type type,
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08005910 unsigned int num_req_dev_types, const u8 *req_dev_types,
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005911 const u8 *dev_id, unsigned int search_delay)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005912{
5913 wpas_p2p_clear_pending_action_tx(wpa_s);
5914 wpa_s->p2p_long_listen = 0;
5915
Dmitry Shmidt04949592012-07-19 12:16:46 -07005916 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL ||
5917 wpa_s->p2p_in_provisioning)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005918 return -1;
5919
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08005920 wpa_supplicant_cancel_sched_scan(wpa_s);
5921
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005922 return p2p_find(wpa_s->global->p2p, timeout, type,
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005923 num_req_dev_types, req_dev_types, dev_id,
5924 search_delay);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005925}
5926
5927
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005928static void wpas_p2p_scan_res_ignore_search(struct wpa_supplicant *wpa_s,
5929 struct wpa_scan_results *scan_res)
5930{
5931 wpa_printf(MSG_DEBUG, "P2P: Ignore scan results");
5932
5933 if (wpa_s->p2p_scan_work) {
5934 struct wpa_radio_work *work = wpa_s->p2p_scan_work;
5935 wpa_s->p2p_scan_work = NULL;
5936 radio_work_done(work);
5937 }
5938
5939 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
5940 return;
5941
5942 /*
5943 * Indicate that results have been processed so that the P2P module can
5944 * continue pending tasks.
5945 */
5946 p2p_scan_res_handled(wpa_s->global->p2p);
5947}
5948
5949
5950static void wpas_p2p_stop_find_oper(struct wpa_supplicant *wpa_s)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005951{
5952 wpas_p2p_clear_pending_action_tx(wpa_s);
5953 wpa_s->p2p_long_listen = 0;
5954 eloop_cancel_timeout(wpas_p2p_long_listen_timeout, wpa_s, NULL);
5955 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005956
5957 if (wpa_s->global->p2p)
5958 p2p_stop_find(wpa_s->global->p2p);
5959
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005960 if (wpa_s->scan_res_handler == wpas_p2p_scan_res_handler) {
5961 wpa_printf(MSG_DEBUG,
5962 "P2P: Do not consider the scan results after stop_find");
5963 wpa_s->scan_res_handler = wpas_p2p_scan_res_ignore_search;
5964 }
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08005965}
5966
5967
5968void wpas_p2p_stop_find(struct wpa_supplicant *wpa_s)
5969{
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005970 wpas_p2p_stop_find_oper(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005971 wpas_p2p_remove_pending_group_interface(wpa_s);
5972}
5973
5974
5975static void wpas_p2p_long_listen_timeout(void *eloop_ctx, void *timeout_ctx)
5976{
5977 struct wpa_supplicant *wpa_s = eloop_ctx;
5978 wpa_s->p2p_long_listen = 0;
5979}
5980
5981
5982int wpas_p2p_listen(struct wpa_supplicant *wpa_s, unsigned int timeout)
5983{
5984 int res;
5985
5986 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
5987 return -1;
5988
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08005989 wpa_supplicant_cancel_sched_scan(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005990 wpas_p2p_clear_pending_action_tx(wpa_s);
5991
5992 if (timeout == 0) {
5993 /*
5994 * This is a request for unlimited Listen state. However, at
5995 * least for now, this is mapped to a Listen state for one
5996 * hour.
5997 */
5998 timeout = 3600;
5999 }
6000 eloop_cancel_timeout(wpas_p2p_long_listen_timeout, wpa_s, NULL);
6001 wpa_s->p2p_long_listen = 0;
6002
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006003 /*
6004 * Stop previous find/listen operation to avoid trying to request a new
6005 * remain-on-channel operation while the driver is still running the
6006 * previous one.
6007 */
6008 if (wpa_s->global->p2p)
6009 p2p_stop_find(wpa_s->global->p2p);
6010
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006011 res = wpas_p2p_listen_start(wpa_s, timeout * 1000);
6012 if (res == 0 && timeout * 1000 > wpa_s->max_remain_on_chan) {
6013 wpa_s->p2p_long_listen = timeout * 1000;
6014 eloop_register_timeout(timeout, 0,
6015 wpas_p2p_long_listen_timeout,
6016 wpa_s, NULL);
6017 }
6018
6019 return res;
6020}
6021
6022
6023int wpas_p2p_assoc_req_ie(struct wpa_supplicant *wpa_s, struct wpa_bss *bss,
6024 u8 *buf, size_t len, int p2p_group)
6025{
6026 struct wpabuf *p2p_ie;
6027 int ret;
6028
6029 if (wpa_s->global->p2p_disabled)
6030 return -1;
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07006031 if (wpa_s->conf->p2p_disabled)
6032 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006033 if (wpa_s->global->p2p == NULL)
6034 return -1;
6035 if (bss == NULL)
6036 return -1;
6037
6038 p2p_ie = wpa_bss_get_vendor_ie_multi(bss, P2P_IE_VENDOR_TYPE);
6039 ret = p2p_assoc_req_ie(wpa_s->global->p2p, bss->bssid, buf, len,
6040 p2p_group, p2p_ie);
6041 wpabuf_free(p2p_ie);
6042
6043 return ret;
6044}
6045
6046
6047int wpas_p2p_probe_req_rx(struct wpa_supplicant *wpa_s, const u8 *addr,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006048 const u8 *dst, const u8 *bssid,
Dmitry Shmidt04949592012-07-19 12:16:46 -07006049 const u8 *ie, size_t ie_len, int ssi_signal)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006050{
6051 if (wpa_s->global->p2p_disabled)
6052 return 0;
6053 if (wpa_s->global->p2p == NULL)
6054 return 0;
6055
Dmitry Shmidt04949592012-07-19 12:16:46 -07006056 switch (p2p_probe_req_rx(wpa_s->global->p2p, addr, dst, bssid,
6057 ie, ie_len)) {
6058 case P2P_PREQ_NOT_P2P:
6059 wpas_notify_preq(wpa_s, addr, dst, bssid, ie, ie_len,
6060 ssi_signal);
6061 /* fall through */
6062 case P2P_PREQ_MALFORMED:
6063 case P2P_PREQ_NOT_LISTEN:
6064 case P2P_PREQ_NOT_PROCESSED:
6065 default: /* make gcc happy */
6066 return 0;
6067 case P2P_PREQ_PROCESSED:
6068 return 1;
6069 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006070}
6071
6072
6073void wpas_p2p_rx_action(struct wpa_supplicant *wpa_s, const u8 *da,
6074 const u8 *sa, const u8 *bssid,
6075 u8 category, const u8 *data, size_t len, int freq)
6076{
6077 if (wpa_s->global->p2p_disabled)
6078 return;
6079 if (wpa_s->global->p2p == NULL)
6080 return;
6081
6082 p2p_rx_action(wpa_s->global->p2p, da, sa, bssid, category, data, len,
6083 freq);
6084}
6085
6086
6087void wpas_p2p_scan_ie(struct wpa_supplicant *wpa_s, struct wpabuf *ies)
6088{
6089 if (wpa_s->global->p2p_disabled)
6090 return;
6091 if (wpa_s->global->p2p == NULL)
6092 return;
6093
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08006094 p2p_scan_ie(wpa_s->global->p2p, ies, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006095}
6096
6097
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07006098static void wpas_p2p_group_deinit(struct wpa_supplicant *wpa_s)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006099{
6100 p2p_group_deinit(wpa_s->p2p_group);
6101 wpa_s->p2p_group = NULL;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006102
6103 wpa_s->ap_configured_cb = NULL;
6104 wpa_s->ap_configured_cb_ctx = NULL;
6105 wpa_s->ap_configured_cb_data = NULL;
6106 wpa_s->connect_without_scan = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006107}
6108
6109
6110int wpas_p2p_reject(struct wpa_supplicant *wpa_s, const u8 *addr)
6111{
6112 wpa_s->p2p_long_listen = 0;
6113
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006114 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
6115 return -1;
6116
6117 return p2p_reject(wpa_s->global->p2p, addr);
6118}
6119
6120
6121/* Invite to reinvoke a persistent group */
6122int wpas_p2p_invite(struct wpa_supplicant *wpa_s, const u8 *peer_addr,
Jouni Malinen31be0a42012-08-31 21:20:51 +03006123 struct wpa_ssid *ssid, const u8 *go_dev_addr, int freq,
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07006124 int ht40, int vht, int pref_freq)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006125{
6126 enum p2p_invite_role role;
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07006127 u8 *bssid = NULL;
6128 int force_freq = 0;
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08006129 int res;
Dmitry Shmidta0d265f2013-11-19 13:13:41 -08006130 int no_pref_freq_given = pref_freq == 0;
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08006131
Dmitry Shmidt700a1372013-03-15 14:14:44 -07006132 wpa_s->global->p2p_invite_group = NULL;
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08006133 if (peer_addr)
6134 os_memcpy(wpa_s->p2p_auth_invite, peer_addr, ETH_ALEN);
6135 else
6136 os_memset(wpa_s->p2p_auth_invite, 0, ETH_ALEN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006137
Jouni Malinen31be0a42012-08-31 21:20:51 +03006138 wpa_s->p2p_persistent_go_freq = freq;
6139 wpa_s->p2p_go_ht40 = !!ht40;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006140 if (ssid->mode == WPAS_MODE_P2P_GO) {
6141 role = P2P_INVITE_ROLE_GO;
6142 if (peer_addr == NULL) {
6143 wpa_printf(MSG_DEBUG, "P2P: Missing peer "
6144 "address in invitation command");
6145 return -1;
6146 }
6147 if (wpas_p2p_create_iface(wpa_s)) {
6148 if (wpas_p2p_add_group_interface(wpa_s,
6149 WPA_IF_P2P_GO) < 0) {
6150 wpa_printf(MSG_ERROR, "P2P: Failed to "
6151 "allocate a new interface for the "
6152 "group");
6153 return -1;
6154 }
6155 bssid = wpa_s->pending_interface_addr;
6156 } else
6157 bssid = wpa_s->own_addr;
6158 } else {
6159 role = P2P_INVITE_ROLE_CLIENT;
6160 peer_addr = ssid->bssid;
6161 }
6162 wpa_s->pending_invite_ssid_id = ssid->id;
6163
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07006164 res = wpas_p2p_setup_freqs(wpa_s, freq, &force_freq, &pref_freq,
6165 role == P2P_INVITE_ROLE_GO);
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08006166 if (res)
6167 return res;
Irfan Sheriffaf84a572012-09-22 16:59:30 -07006168
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006169 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
6170 return -1;
6171
Dmitry Shmidta0d265f2013-11-19 13:13:41 -08006172 if (wpa_s->parent->conf->p2p_ignore_shared_freq &&
6173 no_pref_freq_given && pref_freq > 0 &&
6174 wpa_s->num_multichan_concurrent > 1 &&
6175 wpas_p2p_num_unused_channels(wpa_s) > 0) {
6176 wpa_printf(MSG_DEBUG, "P2P: Ignore own channel preference %d MHz for invitation due to p2p_ignore_shared_freq=1 configuration",
6177 pref_freq);
6178 pref_freq = 0;
6179 }
6180
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08006181 /*
6182 * Stop any find/listen operations before invitation and possibly
6183 * connection establishment.
6184 */
6185 wpas_p2p_stop_find_oper(wpa_s);
6186
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006187 return p2p_invite(wpa_s->global->p2p, peer_addr, role, bssid,
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08006188 ssid->ssid, ssid->ssid_len, force_freq, go_dev_addr,
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08006189 1, pref_freq, -1);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006190}
6191
6192
6193/* Invite to join an active group */
6194int wpas_p2p_invite_group(struct wpa_supplicant *wpa_s, const char *ifname,
6195 const u8 *peer_addr, const u8 *go_dev_addr)
6196{
6197 struct wpa_global *global = wpa_s->global;
6198 enum p2p_invite_role role;
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07006199 u8 *bssid = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006200 struct wpa_ssid *ssid;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08006201 int persistent;
Dmitry Shmidt051af732013-10-22 13:52:46 -07006202 int freq = 0, force_freq = 0, pref_freq = 0;
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08006203 int res;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006204
Jouni Malinen31be0a42012-08-31 21:20:51 +03006205 wpa_s->p2p_persistent_go_freq = 0;
6206 wpa_s->p2p_go_ht40 = 0;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07006207 wpa_s->p2p_go_vht = 0;
Jouni Malinen31be0a42012-08-31 21:20:51 +03006208
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006209 for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
6210 if (os_strcmp(wpa_s->ifname, ifname) == 0)
6211 break;
6212 }
6213 if (wpa_s == NULL) {
6214 wpa_printf(MSG_DEBUG, "P2P: Interface '%s' not found", ifname);
6215 return -1;
6216 }
6217
6218 ssid = wpa_s->current_ssid;
6219 if (ssid == NULL) {
6220 wpa_printf(MSG_DEBUG, "P2P: No current SSID to use for "
6221 "invitation");
6222 return -1;
6223 }
6224
Dmitry Shmidt700a1372013-03-15 14:14:44 -07006225 wpa_s->global->p2p_invite_group = wpa_s;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08006226 persistent = ssid->p2p_persistent_group &&
6227 wpas_p2p_get_persistent(wpa_s->parent, peer_addr,
6228 ssid->ssid, ssid->ssid_len);
6229
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006230 if (ssid->mode == WPAS_MODE_P2P_GO) {
6231 role = P2P_INVITE_ROLE_ACTIVE_GO;
6232 bssid = wpa_s->own_addr;
6233 if (go_dev_addr == NULL)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006234 go_dev_addr = wpa_s->global->p2p_dev_addr;
Dmitry Shmidt051af732013-10-22 13:52:46 -07006235 freq = ssid->frequency;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006236 } else {
6237 role = P2P_INVITE_ROLE_CLIENT;
6238 if (wpa_s->wpa_state < WPA_ASSOCIATED) {
6239 wpa_printf(MSG_DEBUG, "P2P: Not associated - cannot "
6240 "invite to current group");
6241 return -1;
6242 }
6243 bssid = wpa_s->bssid;
6244 if (go_dev_addr == NULL &&
6245 !is_zero_ether_addr(wpa_s->go_dev_addr))
6246 go_dev_addr = wpa_s->go_dev_addr;
Dmitry Shmidt051af732013-10-22 13:52:46 -07006247 freq = wpa_s->current_bss ? wpa_s->current_bss->freq :
6248 (int) wpa_s->assoc_freq;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006249 }
6250 wpa_s->parent->pending_invite_ssid_id = -1;
6251
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006252 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
6253 return -1;
6254
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07006255 res = wpas_p2p_setup_freqs(wpa_s, freq, &force_freq, &pref_freq,
6256 role == P2P_INVITE_ROLE_ACTIVE_GO);
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08006257 if (res)
6258 return res;
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07006259 wpas_p2p_set_own_freq_preference(wpa_s, force_freq);
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08006260
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006261 return p2p_invite(wpa_s->global->p2p, peer_addr, role, bssid,
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08006262 ssid->ssid, ssid->ssid_len, force_freq,
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08006263 go_dev_addr, persistent, pref_freq, -1);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006264}
6265
6266
6267void wpas_p2p_completed(struct wpa_supplicant *wpa_s)
6268{
6269 struct wpa_ssid *ssid = wpa_s->current_ssid;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006270 u8 go_dev_addr[ETH_ALEN];
Jouni Malinen75ecf522011-06-27 15:19:46 -07006271 int network_id = -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006272 int persistent;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006273 int freq;
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08006274 u8 ip[3 * 4];
6275 char ip_addr[100];
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006276
Dmitry Shmidt04949592012-07-19 12:16:46 -07006277 if (ssid == NULL || ssid->mode != WPAS_MODE_P2P_GROUP_FORMATION) {
6278 eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
6279 wpa_s->parent, NULL);
6280 }
6281
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006282 if (!wpa_s->show_group_started || !ssid)
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08006283 return;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006284
6285 wpa_s->show_group_started = 0;
6286
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006287 os_memset(go_dev_addr, 0, ETH_ALEN);
6288 if (ssid->bssid_set)
6289 os_memcpy(go_dev_addr, ssid->bssid, ETH_ALEN);
6290 persistent = wpas_p2p_persistent_group(wpa_s, go_dev_addr, ssid->ssid,
6291 ssid->ssid_len);
6292 os_memcpy(wpa_s->go_dev_addr, go_dev_addr, ETH_ALEN);
6293
6294 if (wpa_s->global->p2p_group_formation == wpa_s)
6295 wpa_s->global->p2p_group_formation = NULL;
6296
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006297 freq = wpa_s->current_bss ? wpa_s->current_bss->freq :
6298 (int) wpa_s->assoc_freq;
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08006299
6300 ip_addr[0] = '\0';
6301 if (wpa_sm_get_p2p_ip_addr(wpa_s->wpa, ip) == 0) {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006302 int res;
6303
6304 res = os_snprintf(ip_addr, sizeof(ip_addr),
6305 " ip_addr=%u.%u.%u.%u "
6306 "ip_mask=%u.%u.%u.%u go_ip_addr=%u.%u.%u.%u",
6307 ip[0], ip[1], ip[2], ip[3],
6308 ip[4], ip[5], ip[6], ip[7],
6309 ip[8], ip[9], ip[10], ip[11]);
6310 if (os_snprintf_error(sizeof(ip_addr), res))
6311 ip_addr[0] = '\0';
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08006312 }
6313
Dmitry Shmidt7f0b69e2014-07-28 10:35:20 -07006314 wpas_p2p_group_started(wpa_s, 0, ssid, freq,
6315 ssid->passphrase == NULL && ssid->psk_set ?
6316 ssid->psk : NULL,
6317 ssid->passphrase, go_dev_addr, persistent,
6318 ip_addr);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006319
6320 if (persistent)
Jouni Malinen75ecf522011-06-27 15:19:46 -07006321 network_id = wpas_p2p_store_persistent_group(wpa_s->parent,
6322 ssid, go_dev_addr);
6323 if (network_id < 0)
6324 network_id = ssid->id;
6325 wpas_notify_p2p_group_started(wpa_s, ssid, network_id, 1);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006326}
6327
6328
6329int wpas_p2p_presence_req(struct wpa_supplicant *wpa_s, u32 duration1,
6330 u32 interval1, u32 duration2, u32 interval2)
6331{
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08006332 int ret;
6333
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006334 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
6335 return -1;
6336
6337 if (wpa_s->wpa_state < WPA_ASSOCIATED ||
6338 wpa_s->current_ssid == NULL ||
6339 wpa_s->current_ssid->mode != WPAS_MODE_INFRA)
6340 return -1;
6341
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08006342 ret = p2p_presence_req(wpa_s->global->p2p, wpa_s->bssid,
6343 wpa_s->own_addr, wpa_s->assoc_freq,
6344 duration1, interval1, duration2, interval2);
6345 if (ret == 0)
6346 wpa_s->waiting_presence_resp = 1;
6347
6348 return ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006349}
6350
6351
6352int wpas_p2p_ext_listen(struct wpa_supplicant *wpa_s, unsigned int period,
6353 unsigned int interval)
6354{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006355 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
6356 return -1;
6357
6358 return p2p_ext_listen(wpa_s->global->p2p, period, interval);
6359}
6360
6361
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006362static int wpas_p2p_is_client(struct wpa_supplicant *wpa_s)
6363{
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08006364 if (wpa_s->current_ssid == NULL) {
6365 /*
6366 * current_ssid can be cleared when P2P client interface gets
6367 * disconnected, so assume this interface was used as P2P
6368 * client.
6369 */
6370 return 1;
6371 }
6372 return wpa_s->current_ssid->p2p_group &&
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006373 wpa_s->current_ssid->mode == WPAS_MODE_INFRA;
6374}
6375
6376
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006377static void wpas_p2p_group_idle_timeout(void *eloop_ctx, void *timeout_ctx)
6378{
6379 struct wpa_supplicant *wpa_s = eloop_ctx;
6380
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006381 if (wpa_s->conf->p2p_group_idle == 0 && !wpas_p2p_is_client(wpa_s)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006382 wpa_printf(MSG_DEBUG, "P2P: Ignore group idle timeout - "
6383 "disabled");
6384 return;
6385 }
6386
Dmitry Shmidt04949592012-07-19 12:16:46 -07006387 wpa_printf(MSG_DEBUG, "P2P: Group idle timeout reached - terminate "
6388 "group");
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07006389 wpas_p2p_group_delete(wpa_s, P2P_GROUP_REMOVAL_IDLE_TIMEOUT);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006390}
6391
6392
6393static void wpas_p2p_set_group_idle_timeout(struct wpa_supplicant *wpa_s)
6394{
Dmitry Shmidt04949592012-07-19 12:16:46 -07006395 int timeout;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006396
Dmitry Shmidt04949592012-07-19 12:16:46 -07006397 if (eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL) > 0)
6398 wpa_printf(MSG_DEBUG, "P2P: Cancelled P2P group idle timeout");
6399
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006400 if (wpa_s->current_ssid == NULL || !wpa_s->current_ssid->p2p_group)
6401 return;
6402
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006403 timeout = wpa_s->conf->p2p_group_idle;
6404 if (wpa_s->current_ssid->mode == WPAS_MODE_INFRA &&
6405 (timeout == 0 || timeout > P2P_MAX_CLIENT_IDLE))
6406 timeout = P2P_MAX_CLIENT_IDLE;
6407
6408 if (timeout == 0)
6409 return;
6410
Dmitry Shmidt04949592012-07-19 12:16:46 -07006411 if (timeout < 0) {
6412 if (wpa_s->current_ssid->mode == WPAS_MODE_INFRA)
6413 timeout = 0; /* special client mode no-timeout */
6414 else
6415 return;
6416 }
6417
6418 if (wpa_s->p2p_in_provisioning) {
6419 /*
6420 * Use the normal group formation timeout during the
6421 * provisioning phase to avoid terminating this process too
6422 * early due to group idle timeout.
6423 */
6424 wpa_printf(MSG_DEBUG, "P2P: Do not use P2P group idle timeout "
6425 "during provisioning");
6426 return;
6427 }
Deepthi Gowri9e4e8ac2013-04-16 11:57:05 +05306428
Dmitry Shmidt04949592012-07-19 12:16:46 -07006429 if (wpa_s->show_group_started) {
6430 /*
6431 * Use the normal group formation timeout between the end of
6432 * the provisioning phase and completion of 4-way handshake to
6433 * avoid terminating this process too early due to group idle
6434 * timeout.
6435 */
6436 wpa_printf(MSG_DEBUG, "P2P: Do not use P2P group idle timeout "
6437 "while waiting for initial 4-way handshake to "
6438 "complete");
6439 return;
6440 }
6441
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006442 wpa_printf(MSG_DEBUG, "P2P: Set P2P group idle timeout to %u seconds",
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006443 timeout);
6444 eloop_register_timeout(timeout, 0, wpas_p2p_group_idle_timeout,
6445 wpa_s, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006446}
6447
6448
Jouni Malinen2b89da82012-08-31 22:04:41 +03006449/* Returns 1 if the interface was removed */
6450int wpas_p2p_deauth_notif(struct wpa_supplicant *wpa_s, const u8 *bssid,
6451 u16 reason_code, const u8 *ie, size_t ie_len,
6452 int locally_generated)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006453{
6454 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
Jouni Malinen2b89da82012-08-31 22:04:41 +03006455 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006456
Dmitry Shmidt04949592012-07-19 12:16:46 -07006457 if (!locally_generated)
6458 p2p_deauth_notif(wpa_s->global->p2p, bssid, reason_code, ie,
6459 ie_len);
6460
6461 if (reason_code == WLAN_REASON_DEAUTH_LEAVING && !locally_generated &&
6462 wpa_s->current_ssid &&
6463 wpa_s->current_ssid->p2p_group &&
6464 wpa_s->current_ssid->mode == WPAS_MODE_INFRA) {
6465 wpa_printf(MSG_DEBUG, "P2P: GO indicated that the P2P Group "
6466 "session is ending");
Jouni Malinen2b89da82012-08-31 22:04:41 +03006467 if (wpas_p2p_group_delete(wpa_s,
6468 P2P_GROUP_REMOVAL_GO_ENDING_SESSION)
6469 > 0)
6470 return 1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07006471 }
Jouni Malinen2b89da82012-08-31 22:04:41 +03006472
6473 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006474}
6475
6476
6477void wpas_p2p_disassoc_notif(struct wpa_supplicant *wpa_s, const u8 *bssid,
Dmitry Shmidt04949592012-07-19 12:16:46 -07006478 u16 reason_code, const u8 *ie, size_t ie_len,
6479 int locally_generated)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006480{
6481 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
6482 return;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006483
Dmitry Shmidt04949592012-07-19 12:16:46 -07006484 if (!locally_generated)
6485 p2p_disassoc_notif(wpa_s->global->p2p, bssid, reason_code, ie,
6486 ie_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006487}
6488
6489
6490void wpas_p2p_update_config(struct wpa_supplicant *wpa_s)
6491{
6492 struct p2p_data *p2p = wpa_s->global->p2p;
6493
6494 if (p2p == NULL)
6495 return;
6496
6497 if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_CAPABLE))
6498 return;
6499
6500 if (wpa_s->conf->changed_parameters & CFG_CHANGED_DEVICE_NAME)
6501 p2p_set_dev_name(p2p, wpa_s->conf->device_name);
6502
6503 if (wpa_s->conf->changed_parameters & CFG_CHANGED_DEVICE_TYPE)
6504 p2p_set_pri_dev_type(p2p, wpa_s->conf->device_type);
6505
6506 if (wpa_s->wps &&
6507 (wpa_s->conf->changed_parameters & CFG_CHANGED_CONFIG_METHODS))
6508 p2p_set_config_methods(p2p, wpa_s->wps->config_methods);
6509
6510 if (wpa_s->wps && (wpa_s->conf->changed_parameters & CFG_CHANGED_UUID))
6511 p2p_set_uuid(p2p, wpa_s->wps->uuid);
6512
6513 if (wpa_s->conf->changed_parameters & CFG_CHANGED_WPS_STRING) {
6514 p2p_set_manufacturer(p2p, wpa_s->conf->manufacturer);
6515 p2p_set_model_name(p2p, wpa_s->conf->model_name);
6516 p2p_set_model_number(p2p, wpa_s->conf->model_number);
6517 p2p_set_serial_number(p2p, wpa_s->conf->serial_number);
6518 }
6519
6520 if (wpa_s->conf->changed_parameters & CFG_CHANGED_SEC_DEVICE_TYPE)
6521 p2p_set_sec_dev_types(p2p,
6522 (void *) wpa_s->conf->sec_device_type,
6523 wpa_s->conf->num_sec_device_types);
6524
6525 if (wpa_s->conf->changed_parameters & CFG_CHANGED_VENDOR_EXTENSION) {
6526 int i;
6527 p2p_remove_wps_vendor_extensions(p2p);
6528 for (i = 0; i < MAX_WPS_VENDOR_EXT; i++) {
6529 if (wpa_s->conf->wps_vendor_ext[i] == NULL)
6530 continue;
6531 p2p_add_wps_vendor_extension(
6532 p2p, wpa_s->conf->wps_vendor_ext[i]);
6533 }
6534 }
6535
6536 if ((wpa_s->conf->changed_parameters & CFG_CHANGED_COUNTRY) &&
6537 wpa_s->conf->country[0] && wpa_s->conf->country[1]) {
6538 char country[3];
6539 country[0] = wpa_s->conf->country[0];
6540 country[1] = wpa_s->conf->country[1];
6541 country[2] = 0x04;
6542 p2p_set_country(p2p, country);
6543 }
6544
6545 if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_SSID_POSTFIX) {
6546 p2p_set_ssid_postfix(p2p, (u8 *) wpa_s->conf->p2p_ssid_postfix,
6547 wpa_s->conf->p2p_ssid_postfix ?
6548 os_strlen(wpa_s->conf->p2p_ssid_postfix) :
6549 0);
6550 }
6551
6552 if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_INTRA_BSS)
6553 p2p_set_intra_bss_dist(p2p, wpa_s->conf->p2p_intra_bss);
Jouni Malinen75ecf522011-06-27 15:19:46 -07006554
6555 if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_LISTEN_CHANNEL) {
6556 u8 reg_class, channel;
6557 int ret;
6558 unsigned int r;
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07006559 u8 channel_forced;
6560
Jouni Malinen75ecf522011-06-27 15:19:46 -07006561 if (wpa_s->conf->p2p_listen_reg_class &&
6562 wpa_s->conf->p2p_listen_channel) {
6563 reg_class = wpa_s->conf->p2p_listen_reg_class;
6564 channel = wpa_s->conf->p2p_listen_channel;
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07006565 channel_forced = 1;
Jouni Malinen75ecf522011-06-27 15:19:46 -07006566 } else {
6567 reg_class = 81;
6568 /*
6569 * Pick one of the social channels randomly as the
6570 * listen channel.
6571 */
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07006572 if (os_get_random((u8 *) &r, sizeof(r)) < 0)
6573 channel = 1;
6574 else
6575 channel = 1 + (r % 3) * 5;
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07006576 channel_forced = 0;
Jouni Malinen75ecf522011-06-27 15:19:46 -07006577 }
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07006578 ret = p2p_set_listen_channel(p2p, reg_class, channel,
6579 channel_forced);
Jouni Malinen75ecf522011-06-27 15:19:46 -07006580 if (ret)
6581 wpa_printf(MSG_ERROR, "P2P: Own listen channel update "
6582 "failed: %d", ret);
6583 }
6584 if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_OPER_CHANNEL) {
6585 u8 op_reg_class, op_channel, cfg_op_channel;
6586 int ret = 0;
6587 unsigned int r;
6588 if (wpa_s->conf->p2p_oper_reg_class &&
6589 wpa_s->conf->p2p_oper_channel) {
6590 op_reg_class = wpa_s->conf->p2p_oper_reg_class;
6591 op_channel = wpa_s->conf->p2p_oper_channel;
6592 cfg_op_channel = 1;
6593 } else {
6594 op_reg_class = 81;
6595 /*
6596 * Use random operation channel from (1, 6, 11)
6597 *if no other preference is indicated.
6598 */
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07006599 if (os_get_random((u8 *) &r, sizeof(r)) < 0)
6600 op_channel = 1;
6601 else
6602 op_channel = 1 + (r % 3) * 5;
Jouni Malinen75ecf522011-06-27 15:19:46 -07006603 cfg_op_channel = 0;
6604 }
6605 ret = p2p_set_oper_channel(p2p, op_reg_class, op_channel,
6606 cfg_op_channel);
6607 if (ret)
6608 wpa_printf(MSG_ERROR, "P2P: Own oper channel update "
6609 "failed: %d", ret);
6610 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07006611
6612 if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_PREF_CHAN) {
6613 if (p2p_set_pref_chan(p2p, wpa_s->conf->num_p2p_pref_chan,
6614 wpa_s->conf->p2p_pref_chan) < 0) {
6615 wpa_printf(MSG_ERROR, "P2P: Preferred channel list "
6616 "update failed");
6617 }
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07006618
6619 if (p2p_set_no_go_freq(p2p, &wpa_s->conf->p2p_no_go_freq) < 0) {
6620 wpa_printf(MSG_ERROR, "P2P: No GO channel list "
6621 "update failed");
6622 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07006623 }
Dmitry Shmidt2271d3f2014-06-23 12:16:31 -07006624
6625 if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_PASSPHRASE_LEN)
6626 p2p_set_passphrase_len(p2p, wpa_s->conf->p2p_passphrase_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006627}
6628
6629
6630int wpas_p2p_set_noa(struct wpa_supplicant *wpa_s, u8 count, int start,
6631 int duration)
6632{
6633 if (!wpa_s->ap_iface)
6634 return -1;
6635 return hostapd_p2p_set_noa(wpa_s->ap_iface->bss[0], count, start,
6636 duration);
6637}
6638
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006639
6640int wpas_p2p_set_cross_connect(struct wpa_supplicant *wpa_s, int enabled)
6641{
6642 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
6643 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006644
6645 wpa_s->global->cross_connection = enabled;
6646 p2p_set_cross_connect(wpa_s->global->p2p, enabled);
6647
6648 if (!enabled) {
6649 struct wpa_supplicant *iface;
6650
6651 for (iface = wpa_s->global->ifaces; iface; iface = iface->next)
6652 {
6653 if (iface->cross_connect_enabled == 0)
6654 continue;
6655
6656 iface->cross_connect_enabled = 0;
6657 iface->cross_connect_in_use = 0;
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07006658 wpa_msg_global(iface->parent, MSG_INFO,
6659 P2P_EVENT_CROSS_CONNECT_DISABLE "%s %s",
6660 iface->ifname,
6661 iface->cross_connect_uplink);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006662 }
6663 }
6664
6665 return 0;
6666}
6667
6668
6669static void wpas_p2p_enable_cross_connect(struct wpa_supplicant *uplink)
6670{
6671 struct wpa_supplicant *iface;
6672
6673 if (!uplink->global->cross_connection)
6674 return;
6675
6676 for (iface = uplink->global->ifaces; iface; iface = iface->next) {
6677 if (!iface->cross_connect_enabled)
6678 continue;
6679 if (os_strcmp(uplink->ifname, iface->cross_connect_uplink) !=
6680 0)
6681 continue;
6682 if (iface->ap_iface == NULL)
6683 continue;
6684 if (iface->cross_connect_in_use)
6685 continue;
6686
6687 iface->cross_connect_in_use = 1;
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07006688 wpa_msg_global(iface->parent, MSG_INFO,
6689 P2P_EVENT_CROSS_CONNECT_ENABLE "%s %s",
6690 iface->ifname, iface->cross_connect_uplink);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006691 }
6692}
6693
6694
6695static void wpas_p2p_disable_cross_connect(struct wpa_supplicant *uplink)
6696{
6697 struct wpa_supplicant *iface;
6698
6699 for (iface = uplink->global->ifaces; iface; iface = iface->next) {
6700 if (!iface->cross_connect_enabled)
6701 continue;
6702 if (os_strcmp(uplink->ifname, iface->cross_connect_uplink) !=
6703 0)
6704 continue;
6705 if (!iface->cross_connect_in_use)
6706 continue;
6707
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07006708 wpa_msg_global(iface->parent, MSG_INFO,
6709 P2P_EVENT_CROSS_CONNECT_DISABLE "%s %s",
6710 iface->ifname, iface->cross_connect_uplink);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006711 iface->cross_connect_in_use = 0;
6712 }
6713}
6714
6715
6716void wpas_p2p_notif_connected(struct wpa_supplicant *wpa_s)
6717{
6718 if (wpa_s->ap_iface || wpa_s->current_ssid == NULL ||
6719 wpa_s->current_ssid->mode != WPAS_MODE_INFRA ||
6720 wpa_s->cross_connect_disallowed)
6721 wpas_p2p_disable_cross_connect(wpa_s);
6722 else
6723 wpas_p2p_enable_cross_connect(wpa_s);
Dmitry Shmidt04949592012-07-19 12:16:46 -07006724 if (!wpa_s->ap_iface &&
6725 eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL) > 0)
6726 wpa_printf(MSG_DEBUG, "P2P: Cancelled P2P group idle timeout");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006727}
6728
6729
6730void wpas_p2p_notif_disconnected(struct wpa_supplicant *wpa_s)
6731{
6732 wpas_p2p_disable_cross_connect(wpa_s);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006733 if (!wpa_s->ap_iface &&
6734 !eloop_is_timeout_registered(wpas_p2p_group_idle_timeout,
6735 wpa_s, NULL))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006736 wpas_p2p_set_group_idle_timeout(wpa_s);
6737}
6738
6739
6740static void wpas_p2p_cross_connect_setup(struct wpa_supplicant *wpa_s)
6741{
6742 struct wpa_supplicant *iface;
6743
6744 if (!wpa_s->global->cross_connection)
6745 return;
6746
6747 for (iface = wpa_s->global->ifaces; iface; iface = iface->next) {
6748 if (iface == wpa_s)
6749 continue;
6750 if (iface->drv_flags &
6751 WPA_DRIVER_FLAGS_P2P_DEDICATED_INTERFACE)
6752 continue;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006753 if ((iface->drv_flags & WPA_DRIVER_FLAGS_P2P_CAPABLE) &&
6754 iface != wpa_s->parent)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006755 continue;
6756
6757 wpa_s->cross_connect_enabled = 1;
6758 os_strlcpy(wpa_s->cross_connect_uplink, iface->ifname,
6759 sizeof(wpa_s->cross_connect_uplink));
6760 wpa_printf(MSG_DEBUG, "P2P: Enable cross connection from "
6761 "%s to %s whenever uplink is available",
6762 wpa_s->ifname, wpa_s->cross_connect_uplink);
6763
6764 if (iface->ap_iface || iface->current_ssid == NULL ||
6765 iface->current_ssid->mode != WPAS_MODE_INFRA ||
6766 iface->cross_connect_disallowed ||
6767 iface->wpa_state != WPA_COMPLETED)
6768 break;
6769
6770 wpa_s->cross_connect_in_use = 1;
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07006771 wpa_msg_global(wpa_s->parent, MSG_INFO,
6772 P2P_EVENT_CROSS_CONNECT_ENABLE "%s %s",
6773 wpa_s->ifname, wpa_s->cross_connect_uplink);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006774 break;
6775 }
6776}
6777
6778
6779int wpas_p2p_notif_pbc_overlap(struct wpa_supplicant *wpa_s)
6780{
6781 if (wpa_s->p2p_group_interface != P2P_GROUP_INTERFACE_CLIENT &&
6782 !wpa_s->p2p_in_provisioning)
6783 return 0; /* not P2P client operation */
6784
6785 wpa_printf(MSG_DEBUG, "P2P: Terminate connection due to WPS PBC "
6786 "session overlap");
6787 if (wpa_s != wpa_s->parent)
6788 wpa_msg_ctrl(wpa_s->parent, MSG_INFO, WPS_EVENT_OVERLAP);
Dmitry Shmidt34af3062013-07-11 10:46:32 -07006789 wpas_p2p_group_formation_failed(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006790 return 1;
6791}
6792
6793
Dmitry Shmidtdf5a7e42014-04-02 12:59:59 -07006794void wpas_p2p_pbc_overlap_cb(void *eloop_ctx, void *timeout_ctx)
6795{
6796 struct wpa_supplicant *wpa_s = eloop_ctx;
6797 wpas_p2p_notif_pbc_overlap(wpa_s);
6798}
6799
6800
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006801void wpas_p2p_update_channel_list(struct wpa_supplicant *wpa_s)
6802{
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07006803 struct p2p_channels chan, cli_chan;
Dmitry Shmidt684785c2014-05-12 13:34:29 -07006804 struct wpa_supplicant *ifs;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006805
6806 if (wpa_s->global == NULL || wpa_s->global->p2p == NULL)
6807 return;
6808
6809 os_memset(&chan, 0, sizeof(chan));
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07006810 os_memset(&cli_chan, 0, sizeof(cli_chan));
6811 if (wpas_p2p_setup_channels(wpa_s, &chan, &cli_chan)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006812 wpa_printf(MSG_ERROR, "P2P: Failed to update supported "
6813 "channel list");
6814 return;
6815 }
6816
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07006817 p2p_update_channel_list(wpa_s->global->p2p, &chan, &cli_chan);
Dmitry Shmidt684785c2014-05-12 13:34:29 -07006818
6819 for (ifs = wpa_s->global->ifaces; ifs; ifs = ifs->next) {
6820 int freq;
6821 if (!ifs->current_ssid ||
6822 !ifs->current_ssid->p2p_group ||
6823 (ifs->current_ssid->mode != WPAS_MODE_P2P_GO &&
6824 ifs->current_ssid->mode != WPAS_MODE_P2P_GROUP_FORMATION))
6825 continue;
6826 freq = ifs->current_ssid->frequency;
6827 if (freq_included(&chan, freq)) {
6828 wpa_dbg(ifs, MSG_DEBUG,
6829 "P2P GO operating frequency %d MHz in valid range",
6830 freq);
6831 continue;
6832 }
6833
6834 wpa_dbg(ifs, MSG_DEBUG,
6835 "P2P GO operating in invalid frequency %d MHz", freq);
6836 /* TODO: Consider using CSA or removing the group within
6837 * wpa_supplicant */
6838 wpa_msg(ifs, MSG_INFO, P2P_EVENT_REMOVE_AND_REFORM_GROUP);
6839 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006840}
6841
6842
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08006843static void wpas_p2p_scan_res_ignore(struct wpa_supplicant *wpa_s,
6844 struct wpa_scan_results *scan_res)
6845{
6846 wpa_printf(MSG_DEBUG, "P2P: Ignore scan results");
6847}
6848
6849
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006850int wpas_p2p_cancel(struct wpa_supplicant *wpa_s)
6851{
6852 struct wpa_global *global = wpa_s->global;
6853 int found = 0;
6854 const u8 *peer;
6855
6856 if (global->p2p == NULL)
6857 return -1;
6858
6859 wpa_printf(MSG_DEBUG, "P2P: Request to cancel group formation");
6860
6861 if (wpa_s->pending_interface_name[0] &&
6862 !is_zero_ether_addr(wpa_s->pending_interface_addr))
6863 found = 1;
6864
6865 peer = p2p_get_go_neg_peer(global->p2p);
6866 if (peer) {
6867 wpa_printf(MSG_DEBUG, "P2P: Unauthorize pending GO Neg peer "
6868 MACSTR, MAC2STR(peer));
6869 p2p_unauthorize(global->p2p, peer);
Dmitry Shmidt04949592012-07-19 12:16:46 -07006870 found = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006871 }
6872
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08006873 if (wpa_s->scan_res_handler == wpas_p2p_scan_res_join) {
6874 wpa_printf(MSG_DEBUG, "P2P: Stop pending scan for join");
6875 wpa_s->scan_res_handler = wpas_p2p_scan_res_ignore;
6876 found = 1;
6877 }
6878
6879 if (wpa_s->pending_pd_before_join) {
6880 wpa_printf(MSG_DEBUG, "P2P: Stop pending PD before join");
6881 wpa_s->pending_pd_before_join = 0;
6882 found = 1;
6883 }
6884
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006885 wpas_p2p_stop_find(wpa_s);
6886
6887 for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
6888 if (wpa_s == global->p2p_group_formation &&
6889 (wpa_s->p2p_in_provisioning ||
6890 wpa_s->parent->pending_interface_type ==
6891 WPA_IF_P2P_CLIENT)) {
6892 wpa_printf(MSG_DEBUG, "P2P: Interface %s in group "
6893 "formation found - cancelling",
6894 wpa_s->ifname);
6895 found = 1;
6896 eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
6897 wpa_s->parent, NULL);
Jouni Malinenadddfc42012-10-03 14:31:41 -07006898 if (wpa_s->p2p_in_provisioning) {
6899 wpas_group_formation_completed(wpa_s, 0);
6900 break;
6901 }
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07006902 wpas_p2p_group_delete(wpa_s,
6903 P2P_GROUP_REMOVAL_REQUESTED);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006904 break;
Dmitry Shmidt21de2142014-04-08 10:50:52 -07006905 } else if (wpa_s->p2p_in_invitation) {
6906 wpa_printf(MSG_DEBUG, "P2P: Interface %s in invitation found - cancelling",
6907 wpa_s->ifname);
6908 found = 1;
6909 wpas_p2p_group_formation_failed(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006910 }
6911 }
6912
6913 if (!found) {
6914 wpa_printf(MSG_DEBUG, "P2P: No ongoing group formation found");
6915 return -1;
6916 }
6917
6918 return 0;
6919}
6920
6921
6922void wpas_p2p_interface_unavailable(struct wpa_supplicant *wpa_s)
6923{
6924 if (wpa_s->current_ssid == NULL || !wpa_s->current_ssid->p2p_group)
6925 return;
6926
6927 wpa_printf(MSG_DEBUG, "P2P: Remove group due to driver resource not "
6928 "being available anymore");
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07006929 wpas_p2p_group_delete(wpa_s, P2P_GROUP_REMOVAL_UNAVAILABLE);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006930}
6931
6932
6933void wpas_p2p_update_best_channels(struct wpa_supplicant *wpa_s,
6934 int freq_24, int freq_5, int freq_overall)
6935{
6936 struct p2p_data *p2p = wpa_s->global->p2p;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08006937 if (p2p == NULL)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006938 return;
6939 p2p_set_best_channels(p2p, freq_24, freq_5, freq_overall);
6940}
6941
6942
6943int wpas_p2p_unauthorize(struct wpa_supplicant *wpa_s, const char *addr)
6944{
6945 u8 peer[ETH_ALEN];
6946 struct p2p_data *p2p = wpa_s->global->p2p;
6947
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08006948 if (p2p == NULL)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006949 return -1;
6950
6951 if (hwaddr_aton(addr, peer))
6952 return -1;
6953
6954 return p2p_unauthorize(p2p, peer);
6955}
6956
6957
6958/**
6959 * wpas_p2p_disconnect - Disconnect from a P2P Group
6960 * @wpa_s: Pointer to wpa_supplicant data
6961 * Returns: 0 on success, -1 on failure
6962 *
6963 * This can be used to disconnect from a group in which the local end is a P2P
6964 * Client or to end a P2P Group in case the local end is the Group Owner. If a
6965 * virtual network interface was created for this group, that interface will be
6966 * removed. Otherwise, only the configured P2P group network will be removed
6967 * from the interface.
6968 */
6969int wpas_p2p_disconnect(struct wpa_supplicant *wpa_s)
6970{
6971
6972 if (wpa_s == NULL)
6973 return -1;
6974
Jouni Malinen2b89da82012-08-31 22:04:41 +03006975 return wpas_p2p_group_delete(wpa_s, P2P_GROUP_REMOVAL_REQUESTED) < 0 ?
6976 -1 : 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006977}
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006978
6979
6980int wpas_p2p_in_progress(struct wpa_supplicant *wpa_s)
6981{
Dmitry Shmidtf8623282013-02-20 14:34:59 -08006982 int ret;
6983
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006984 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
6985 return 0;
6986
Dmitry Shmidtf8623282013-02-20 14:34:59 -08006987 ret = p2p_in_progress(wpa_s->global->p2p);
6988 if (ret == 0) {
6989 /*
6990 * Check whether there is an ongoing WPS provisioning step (or
6991 * other parts of group formation) on another interface since
6992 * p2p_in_progress() does not report this to avoid issues for
6993 * scans during such provisioning step.
6994 */
6995 if (wpa_s->global->p2p_group_formation &&
6996 wpa_s->global->p2p_group_formation != wpa_s) {
6997 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Another interface (%s) "
6998 "in group formation",
6999 wpa_s->global->p2p_group_formation->ifname);
7000 ret = 1;
7001 }
7002 }
7003
Dmitry Shmidt92c368d2013-08-29 12:37:21 -07007004 if (!ret && wpa_s->global->p2p_go_wait_client.sec) {
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08007005 struct os_reltime now;
7006 os_get_reltime(&now);
7007 if (os_reltime_expired(&now, &wpa_s->global->p2p_go_wait_client,
7008 P2P_MAX_INITIAL_CONN_WAIT_GO)) {
Dmitry Shmidt92c368d2013-08-29 12:37:21 -07007009 /* Wait for the first client has expired */
7010 wpa_s->global->p2p_go_wait_client.sec = 0;
7011 } else {
7012 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Waiting for initial client connection during group formation");
7013 ret = 1;
7014 }
7015 }
7016
Dmitry Shmidtf8623282013-02-20 14:34:59 -08007017 return ret;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007018}
7019
7020
7021void wpas_p2p_network_removed(struct wpa_supplicant *wpa_s,
7022 struct wpa_ssid *ssid)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007023{
7024 if (wpa_s->p2p_in_provisioning && ssid->p2p_group &&
7025 eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
7026 wpa_s->parent, NULL) > 0) {
Dmitry Shmidt04949592012-07-19 12:16:46 -07007027 /**
7028 * Remove the network by scheduling the group formation
7029 * timeout to happen immediately. The teardown code
7030 * needs to be scheduled to run asynch later so that we
7031 * don't delete data from under ourselves unexpectedly.
7032 * Calling wpas_p2p_group_formation_timeout directly
7033 * causes a series of crashes in WPS failure scenarios.
7034 */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007035 wpa_printf(MSG_DEBUG, "P2P: Canceled group formation due to "
7036 "P2P group network getting removed");
Dmitry Shmidt04949592012-07-19 12:16:46 -07007037 eloop_register_timeout(0, 0, wpas_p2p_group_formation_timeout,
7038 wpa_s->parent, NULL);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007039 }
7040}
7041
7042
7043struct wpa_ssid * wpas_p2p_get_persistent(struct wpa_supplicant *wpa_s,
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08007044 const u8 *addr, const u8 *ssid,
7045 size_t ssid_len)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007046{
7047 struct wpa_ssid *s;
7048 size_t i;
7049
7050 for (s = wpa_s->conf->ssid; s; s = s->next) {
7051 if (s->disabled != 2)
7052 continue;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08007053 if (ssid &&
7054 (ssid_len != s->ssid_len ||
7055 os_memcmp(ssid, s->ssid, ssid_len) != 0))
7056 continue;
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07007057 if (addr == NULL) {
7058 if (s->mode == WPAS_MODE_P2P_GO)
7059 return s;
7060 continue;
7061 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007062 if (os_memcmp(s->bssid, addr, ETH_ALEN) == 0)
7063 return s; /* peer is GO in the persistent group */
7064 if (s->mode != WPAS_MODE_P2P_GO || s->p2p_client_list == NULL)
7065 continue;
7066 for (i = 0; i < s->num_p2p_clients; i++) {
Dmitry Shmidtff787d52015-01-12 13:01:47 -08007067 if (os_memcmp(s->p2p_client_list + i * 2 * ETH_ALEN,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007068 addr, ETH_ALEN) == 0)
7069 return s; /* peer is P2P client in persistent
7070 * group */
7071 }
7072 }
7073
7074 return NULL;
7075}
7076
7077
7078void wpas_p2p_notify_ap_sta_authorized(struct wpa_supplicant *wpa_s,
7079 const u8 *addr)
7080{
Dmitry Shmidt56052862013-10-04 10:23:25 -07007081 if (eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
7082 wpa_s->parent, NULL) > 0) {
7083 /*
7084 * This can happen if WPS provisioning step is not terminated
7085 * cleanly (e.g., P2P Client does not send WSC_Done). Since the
7086 * peer was able to connect, there is no need to time out group
7087 * formation after this, though. In addition, this is used with
7088 * the initial connection wait on the GO as a separate formation
7089 * timeout and as such, expected to be hit after the initial WPS
7090 * provisioning step.
7091 */
7092 wpa_printf(MSG_DEBUG, "P2P: Canceled P2P group formation timeout on data connection");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007093
7094 if (!wpa_s->p2p_go_group_formation_completed &&
7095 !wpa_s->group_formation_reported) {
7096 /*
7097 * GO has not yet notified group formation success since
7098 * the WPS step was not completed cleanly. Do that
7099 * notification now since the P2P Client was able to
7100 * connect and as such, must have received the
7101 * credential from the WPS step.
7102 */
7103 if (wpa_s->global->p2p)
7104 p2p_wps_success_cb(wpa_s->global->p2p, addr);
7105 wpas_group_formation_completed(wpa_s, 1);
7106 }
Dmitry Shmidt56052862013-10-04 10:23:25 -07007107 }
7108 if (!wpa_s->p2p_go_group_formation_completed) {
7109 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Marking group formation completed on GO on first data connection");
7110 wpa_s->p2p_go_group_formation_completed = 1;
7111 wpa_s->global->p2p_group_formation = NULL;
7112 wpa_s->p2p_in_provisioning = 0;
Dmitry Shmidt21de2142014-04-08 10:50:52 -07007113 wpa_s->p2p_in_invitation = 0;
Dmitry Shmidt56052862013-10-04 10:23:25 -07007114 }
Dmitry Shmidt92c368d2013-08-29 12:37:21 -07007115 wpa_s->global->p2p_go_wait_client.sec = 0;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007116 if (addr == NULL)
7117 return;
7118 wpas_p2p_add_persistent_group_client(wpa_s, addr);
7119}
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08007120
Dmitry Shmidt04949592012-07-19 12:16:46 -07007121
7122static void wpas_p2p_fallback_to_go_neg(struct wpa_supplicant *wpa_s,
7123 int group_added)
7124{
7125 struct wpa_supplicant *group = wpa_s;
Dmitry Shmidt04949592012-07-19 12:16:46 -07007126 if (wpa_s->global->p2p_group_formation)
7127 group = wpa_s->global->p2p_group_formation;
7128 wpa_s = wpa_s->parent;
7129 offchannel_send_action_done(wpa_s);
7130 if (group_added)
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07007131 wpas_p2p_group_delete(group, P2P_GROUP_REMOVAL_SILENT);
Dmitry Shmidt04949592012-07-19 12:16:46 -07007132 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Fall back to GO Negotiation");
7133 wpas_p2p_connect(wpa_s, wpa_s->pending_join_dev_addr, wpa_s->p2p_pin,
7134 wpa_s->p2p_wps_method, wpa_s->p2p_persistent_group, 0,
7135 0, 0, wpa_s->p2p_go_intent, wpa_s->p2p_connect_freq,
7136 wpa_s->p2p_persistent_id,
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07007137 wpa_s->p2p_pd_before_go_neg,
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07007138 wpa_s->p2p_go_ht40,
7139 wpa_s->p2p_go_vht);
Dmitry Shmidt04949592012-07-19 12:16:46 -07007140}
7141
7142
7143int wpas_p2p_scan_no_go_seen(struct wpa_supplicant *wpa_s)
7144{
7145 if (!wpa_s->p2p_fallback_to_go_neg ||
7146 wpa_s->p2p_in_provisioning <= 5)
7147 return 0;
7148
7149 if (wpas_p2p_peer_go(wpa_s, wpa_s->pending_join_dev_addr) > 0)
7150 return 0; /* peer operating as a GO */
7151
7152 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: GO not found for p2p_connect-auto - "
7153 "fallback to GO Negotiation");
7154 wpas_p2p_fallback_to_go_neg(wpa_s, 1);
7155
7156 return 1;
7157}
7158
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07007159
7160unsigned int wpas_p2p_search_delay(struct wpa_supplicant *wpa_s)
7161{
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07007162 struct wpa_supplicant *ifs;
7163
7164 if (wpa_s->wpa_state > WPA_SCANNING) {
7165 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Use %u ms search delay due to "
7166 "concurrent operation",
Dmitry Shmidt09f57ba2014-06-10 16:07:13 -07007167 wpa_s->conf->p2p_search_delay);
7168 return wpa_s->conf->p2p_search_delay;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07007169 }
7170
Dmitry Shmidt01904cf2013-12-05 11:08:35 -08007171 dl_list_for_each(ifs, &wpa_s->radio->ifaces, struct wpa_supplicant,
7172 radio_list) {
7173 if (ifs != wpa_s && ifs->wpa_state > WPA_SCANNING) {
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07007174 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Use %u ms search "
7175 "delay due to concurrent operation on "
7176 "interface %s",
Dmitry Shmidt09f57ba2014-06-10 16:07:13 -07007177 wpa_s->conf->p2p_search_delay,
7178 ifs->ifname);
7179 return wpa_s->conf->p2p_search_delay;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07007180 }
7181 }
7182
7183 return 0;
7184}
7185
Dmitry Shmidt37d4d6a2013-03-18 13:09:42 -07007186
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07007187static int wpas_p2p_remove_psk_entry(struct wpa_supplicant *wpa_s,
7188 struct wpa_ssid *s, const u8 *addr,
7189 int iface_addr)
7190{
7191 struct psk_list_entry *psk, *tmp;
7192 int changed = 0;
7193
7194 dl_list_for_each_safe(psk, tmp, &s->psk_list, struct psk_list_entry,
7195 list) {
7196 if ((iface_addr && !psk->p2p &&
7197 os_memcmp(addr, psk->addr, ETH_ALEN) == 0) ||
7198 (!iface_addr && psk->p2p &&
7199 os_memcmp(addr, psk->addr, ETH_ALEN) == 0)) {
7200 wpa_dbg(wpa_s, MSG_DEBUG,
7201 "P2P: Remove persistent group PSK list entry for "
7202 MACSTR " p2p=%u",
7203 MAC2STR(psk->addr), psk->p2p);
7204 dl_list_del(&psk->list);
7205 os_free(psk);
7206 changed++;
7207 }
7208 }
7209
7210 return changed;
7211}
7212
7213
7214void wpas_p2p_new_psk_cb(struct wpa_supplicant *wpa_s, const u8 *mac_addr,
7215 const u8 *p2p_dev_addr,
7216 const u8 *psk, size_t psk_len)
7217{
7218 struct wpa_ssid *ssid = wpa_s->current_ssid;
7219 struct wpa_ssid *persistent;
Dmitry Shmidt7832adb2014-04-29 10:53:02 -07007220 struct psk_list_entry *p, *last;
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07007221
7222 if (psk_len != sizeof(p->psk))
7223 return;
7224
7225 if (p2p_dev_addr) {
7226 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: New PSK for addr=" MACSTR
7227 " p2p_dev_addr=" MACSTR,
7228 MAC2STR(mac_addr), MAC2STR(p2p_dev_addr));
7229 if (is_zero_ether_addr(p2p_dev_addr))
7230 p2p_dev_addr = NULL;
7231 } else {
7232 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: New PSK for addr=" MACSTR,
7233 MAC2STR(mac_addr));
7234 }
7235
7236 if (ssid->mode == WPAS_MODE_P2P_GROUP_FORMATION) {
7237 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: new_psk_cb during group formation");
7238 /* To be added to persistent group once created */
7239 if (wpa_s->global->add_psk == NULL) {
7240 wpa_s->global->add_psk = os_zalloc(sizeof(*p));
7241 if (wpa_s->global->add_psk == NULL)
7242 return;
7243 }
7244 p = wpa_s->global->add_psk;
7245 if (p2p_dev_addr) {
7246 p->p2p = 1;
7247 os_memcpy(p->addr, p2p_dev_addr, ETH_ALEN);
7248 } else {
7249 p->p2p = 0;
7250 os_memcpy(p->addr, mac_addr, ETH_ALEN);
7251 }
7252 os_memcpy(p->psk, psk, psk_len);
7253 return;
7254 }
7255
7256 if (ssid->mode != WPAS_MODE_P2P_GO || !ssid->p2p_persistent_group) {
7257 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Ignore new_psk_cb on not-persistent GO");
7258 return;
7259 }
7260
7261 persistent = wpas_p2p_get_persistent(wpa_s->parent, NULL, ssid->ssid,
7262 ssid->ssid_len);
7263 if (!persistent) {
7264 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Could not find persistent group information to store the new PSK");
7265 return;
7266 }
7267
7268 p = os_zalloc(sizeof(*p));
7269 if (p == NULL)
7270 return;
7271 if (p2p_dev_addr) {
7272 p->p2p = 1;
7273 os_memcpy(p->addr, p2p_dev_addr, ETH_ALEN);
7274 } else {
7275 p->p2p = 0;
7276 os_memcpy(p->addr, mac_addr, ETH_ALEN);
7277 }
7278 os_memcpy(p->psk, psk, psk_len);
7279
Dmitry Shmidt7832adb2014-04-29 10:53:02 -07007280 if (dl_list_len(&persistent->psk_list) > P2P_MAX_STORED_CLIENTS &&
7281 (last = dl_list_last(&persistent->psk_list,
7282 struct psk_list_entry, list))) {
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07007283 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Remove oldest PSK entry for "
7284 MACSTR " (p2p=%u) to make room for a new one",
7285 MAC2STR(last->addr), last->p2p);
7286 dl_list_del(&last->list);
7287 os_free(last);
7288 }
7289
7290 wpas_p2p_remove_psk_entry(wpa_s->parent, persistent,
7291 p2p_dev_addr ? p2p_dev_addr : mac_addr,
7292 p2p_dev_addr == NULL);
7293 if (p2p_dev_addr) {
7294 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Add new PSK for p2p_dev_addr="
7295 MACSTR, MAC2STR(p2p_dev_addr));
7296 } else {
7297 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Add new PSK for addr=" MACSTR,
7298 MAC2STR(mac_addr));
7299 }
7300 dl_list_add(&persistent->psk_list, &p->list);
7301
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07007302 if (wpa_s->parent->conf->update_config &&
7303 wpa_config_write(wpa_s->parent->confname, wpa_s->parent->conf))
7304 wpa_printf(MSG_DEBUG, "P2P: Failed to update configuration");
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07007305}
7306
7307
7308static void wpas_p2p_remove_psk(struct wpa_supplicant *wpa_s,
7309 struct wpa_ssid *s, const u8 *addr,
7310 int iface_addr)
7311{
7312 int res;
7313
7314 res = wpas_p2p_remove_psk_entry(wpa_s, s, addr, iface_addr);
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08007315 if (res > 0 && wpa_s->conf->update_config &&
7316 wpa_config_write(wpa_s->confname, wpa_s->conf))
7317 wpa_dbg(wpa_s, MSG_DEBUG,
7318 "P2P: Failed to update configuration");
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07007319}
7320
7321
7322static void wpas_p2p_remove_client_go(struct wpa_supplicant *wpa_s,
7323 const u8 *peer, int iface_addr)
7324{
7325 struct hostapd_data *hapd;
7326 struct hostapd_wpa_psk *psk, *prev, *rem;
7327 struct sta_info *sta;
7328
7329 if (wpa_s->ap_iface == NULL || wpa_s->current_ssid == NULL ||
7330 wpa_s->current_ssid->mode != WPAS_MODE_P2P_GO)
7331 return;
7332
7333 /* Remove per-station PSK entry */
7334 hapd = wpa_s->ap_iface->bss[0];
7335 prev = NULL;
7336 psk = hapd->conf->ssid.wpa_psk;
7337 while (psk) {
7338 if ((iface_addr && os_memcmp(peer, psk->addr, ETH_ALEN) == 0) ||
7339 (!iface_addr &&
7340 os_memcmp(peer, psk->p2p_dev_addr, ETH_ALEN) == 0)) {
7341 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Remove operating group PSK entry for "
7342 MACSTR " iface_addr=%d",
7343 MAC2STR(peer), iface_addr);
7344 if (prev)
7345 prev->next = psk->next;
7346 else
7347 hapd->conf->ssid.wpa_psk = psk->next;
7348 rem = psk;
7349 psk = psk->next;
7350 os_free(rem);
7351 } else {
7352 prev = psk;
7353 psk = psk->next;
7354 }
7355 }
7356
7357 /* Disconnect from group */
7358 if (iface_addr)
7359 sta = ap_get_sta(hapd, peer);
7360 else
7361 sta = ap_get_sta_p2p(hapd, peer);
7362 if (sta) {
7363 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Disconnect peer " MACSTR
7364 " (iface_addr=%d) from group",
7365 MAC2STR(peer), iface_addr);
7366 hostapd_drv_sta_deauth(hapd, sta->addr,
7367 WLAN_REASON_DEAUTH_LEAVING);
7368 ap_sta_deauthenticate(hapd, sta, WLAN_REASON_DEAUTH_LEAVING);
7369 }
7370}
7371
7372
7373void wpas_p2p_remove_client(struct wpa_supplicant *wpa_s, const u8 *peer,
7374 int iface_addr)
7375{
7376 struct wpa_ssid *s;
7377 struct wpa_supplicant *w;
7378
7379 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Remove client " MACSTR, MAC2STR(peer));
7380
7381 /* Remove from any persistent group */
7382 for (s = wpa_s->parent->conf->ssid; s; s = s->next) {
7383 if (s->disabled != 2 || s->mode != WPAS_MODE_P2P_GO)
7384 continue;
7385 if (!iface_addr)
7386 wpas_remove_persistent_peer(wpa_s, s, peer, 0);
7387 wpas_p2p_remove_psk(wpa_s->parent, s, peer, iface_addr);
7388 }
7389
7390 /* Remove from any operating group */
7391 for (w = wpa_s->global->ifaces; w; w = w->next)
7392 wpas_p2p_remove_client_go(w, peer, iface_addr);
7393}
7394
7395
7396static void wpas_p2p_psk_failure_removal(void *eloop_ctx, void *timeout_ctx)
7397{
7398 struct wpa_supplicant *wpa_s = eloop_ctx;
7399 wpas_p2p_group_delete(wpa_s, P2P_GROUP_REMOVAL_PSK_FAILURE);
7400}
7401
7402
Dmitry Shmidt04f534e2013-12-09 15:50:16 -08007403static void wpas_p2p_group_freq_conflict(void *eloop_ctx, void *timeout_ctx)
7404{
7405 struct wpa_supplicant *wpa_s = eloop_ctx;
7406
7407 wpa_printf(MSG_DEBUG, "P2P: Frequency conflict - terminate group");
7408 wpas_p2p_group_delete(wpa_s, P2P_GROUP_REMOVAL_FREQ_CONFLICT);
7409}
7410
7411
7412int wpas_p2p_handle_frequency_conflicts(struct wpa_supplicant *wpa_s, int freq,
7413 struct wpa_ssid *ssid)
7414{
7415 struct wpa_supplicant *iface;
7416
7417 for (iface = wpa_s->global->ifaces; iface; iface = iface->next) {
7418 if (!iface->current_ssid ||
7419 iface->current_ssid->frequency == freq ||
7420 (iface->p2p_group_interface == NOT_P2P_GROUP_INTERFACE &&
7421 !iface->current_ssid->p2p_group))
7422 continue;
7423
7424 /* Remove the connection with least priority */
7425 if (!wpas_is_p2p_prioritized(iface)) {
7426 /* STA connection has priority over existing
7427 * P2P connection, so remove the interface. */
7428 wpa_printf(MSG_DEBUG, "P2P: Removing P2P connection due to single channel concurrent mode frequency conflict");
7429 eloop_register_timeout(0, 0,
7430 wpas_p2p_group_freq_conflict,
7431 iface, NULL);
7432 /* If connection in progress is P2P connection, do not
7433 * proceed for the connection. */
7434 if (wpa_s == iface)
7435 return -1;
7436 else
7437 return 0;
7438 } else {
7439 /* P2P connection has priority, disable the STA network
7440 */
7441 wpa_supplicant_disable_network(wpa_s->global->ifaces,
7442 ssid);
7443 wpa_msg(wpa_s->global->ifaces, MSG_INFO,
7444 WPA_EVENT_FREQ_CONFLICT " id=%d", ssid->id);
7445 os_memset(wpa_s->global->ifaces->pending_bssid, 0,
7446 ETH_ALEN);
7447 /* If P2P connection is in progress, continue
7448 * connecting...*/
7449 if (wpa_s == iface)
7450 return 0;
7451 else
7452 return -1;
7453 }
7454 }
7455
7456 return 0;
7457}
7458
7459
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07007460int wpas_p2p_4way_hs_failed(struct wpa_supplicant *wpa_s)
7461{
7462 struct wpa_ssid *ssid = wpa_s->current_ssid;
7463
7464 if (ssid == NULL || !ssid->p2p_group)
7465 return 0;
7466
7467 if (wpa_s->p2p_last_4way_hs_fail &&
7468 wpa_s->p2p_last_4way_hs_fail == ssid) {
7469 u8 go_dev_addr[ETH_ALEN];
7470 struct wpa_ssid *persistent;
7471
7472 if (wpas_p2p_persistent_group(wpa_s, go_dev_addr,
7473 ssid->ssid,
7474 ssid->ssid_len) <= 0) {
7475 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Could not determine whether 4-way handshake failures were for a persistent group");
7476 goto disconnect;
7477 }
7478
7479 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Two 4-way handshake failures for a P2P group - go_dev_addr="
7480 MACSTR, MAC2STR(go_dev_addr));
7481 persistent = wpas_p2p_get_persistent(wpa_s->parent, go_dev_addr,
7482 ssid->ssid,
7483 ssid->ssid_len);
7484 if (persistent == NULL || persistent->mode != WPAS_MODE_INFRA) {
7485 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: No matching persistent group stored");
7486 goto disconnect;
7487 }
7488 wpa_msg_global(wpa_s->parent, MSG_INFO,
7489 P2P_EVENT_PERSISTENT_PSK_FAIL "%d",
7490 persistent->id);
7491 disconnect:
7492 wpa_s->p2p_last_4way_hs_fail = NULL;
7493 /*
7494 * Remove the group from a timeout to avoid issues with caller
7495 * continuing to use the interface if this is on a P2P group
7496 * interface.
7497 */
7498 eloop_register_timeout(0, 0, wpas_p2p_psk_failure_removal,
7499 wpa_s, NULL);
7500 return 1;
7501 }
7502
7503 wpa_s->p2p_last_4way_hs_fail = ssid;
7504 return 0;
7505}
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08007506
7507
7508#ifdef CONFIG_WPS_NFC
7509
7510static struct wpabuf * wpas_p2p_nfc_handover(int ndef, struct wpabuf *wsc,
7511 struct wpabuf *p2p)
7512{
7513 struct wpabuf *ret;
7514 size_t wsc_len;
7515
7516 if (p2p == NULL) {
7517 wpabuf_free(wsc);
7518 wpa_printf(MSG_DEBUG, "P2P: No p2p buffer for handover");
7519 return NULL;
7520 }
7521
7522 wsc_len = wsc ? wpabuf_len(wsc) : 0;
7523 ret = wpabuf_alloc(2 + wsc_len + 2 + wpabuf_len(p2p));
7524 if (ret == NULL) {
7525 wpabuf_free(wsc);
7526 wpabuf_free(p2p);
7527 return NULL;
7528 }
7529
7530 wpabuf_put_be16(ret, wsc_len);
7531 if (wsc)
7532 wpabuf_put_buf(ret, wsc);
7533 wpabuf_put_be16(ret, wpabuf_len(p2p));
7534 wpabuf_put_buf(ret, p2p);
7535
7536 wpabuf_free(wsc);
7537 wpabuf_free(p2p);
7538 wpa_hexdump_buf(MSG_DEBUG,
7539 "P2P: Generated NFC connection handover message", ret);
7540
7541 if (ndef && ret) {
7542 struct wpabuf *tmp;
7543 tmp = ndef_build_p2p(ret);
7544 wpabuf_free(ret);
7545 if (tmp == NULL) {
7546 wpa_printf(MSG_DEBUG, "P2P: Failed to NDEF encapsulate handover request");
7547 return NULL;
7548 }
7549 ret = tmp;
7550 }
7551
7552 return ret;
7553}
7554
7555
7556static int wpas_p2p_cli_freq(struct wpa_supplicant *wpa_s,
7557 struct wpa_ssid **ssid, u8 *go_dev_addr)
7558{
7559 struct wpa_supplicant *iface;
7560
7561 if (go_dev_addr)
7562 os_memset(go_dev_addr, 0, ETH_ALEN);
7563 if (ssid)
7564 *ssid = NULL;
7565 for (iface = wpa_s->global->ifaces; iface; iface = iface->next) {
7566 if (iface->wpa_state < WPA_ASSOCIATING ||
7567 iface->current_ssid == NULL || iface->assoc_freq == 0 ||
7568 !iface->current_ssid->p2p_group ||
7569 iface->current_ssid->mode != WPAS_MODE_INFRA)
7570 continue;
7571 if (ssid)
7572 *ssid = iface->current_ssid;
7573 if (go_dev_addr)
7574 os_memcpy(go_dev_addr, iface->go_dev_addr, ETH_ALEN);
7575 return iface->assoc_freq;
7576 }
7577 return 0;
7578}
7579
7580
7581struct wpabuf * wpas_p2p_nfc_handover_req(struct wpa_supplicant *wpa_s,
7582 int ndef)
7583{
7584 struct wpabuf *wsc, *p2p;
7585 struct wpa_ssid *ssid;
7586 u8 go_dev_addr[ETH_ALEN];
7587 int cli_freq = wpas_p2p_cli_freq(wpa_s, &ssid, go_dev_addr);
7588
7589 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL) {
7590 wpa_printf(MSG_DEBUG, "P2P: P2P disabled - cannot build handover request");
7591 return NULL;
7592 }
7593
7594 if (wpa_s->conf->wps_nfc_dh_pubkey == NULL &&
7595 wps_nfc_gen_dh(&wpa_s->conf->wps_nfc_dh_pubkey,
7596 &wpa_s->conf->wps_nfc_dh_privkey) < 0) {
7597 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: No DH key available for handover request");
7598 return NULL;
7599 }
7600
7601 if (cli_freq == 0) {
7602 wsc = wps_build_nfc_handover_req_p2p(
7603 wpa_s->parent->wps, wpa_s->conf->wps_nfc_dh_pubkey);
7604 } else
7605 wsc = NULL;
7606 p2p = p2p_build_nfc_handover_req(wpa_s->global->p2p, cli_freq,
7607 go_dev_addr, ssid ? ssid->ssid : NULL,
7608 ssid ? ssid->ssid_len : 0);
7609
7610 return wpas_p2p_nfc_handover(ndef, wsc, p2p);
7611}
7612
7613
7614struct wpabuf * wpas_p2p_nfc_handover_sel(struct wpa_supplicant *wpa_s,
7615 int ndef, int tag)
7616{
7617 struct wpabuf *wsc, *p2p;
7618 struct wpa_ssid *ssid;
7619 u8 go_dev_addr[ETH_ALEN];
7620 int cli_freq = wpas_p2p_cli_freq(wpa_s, &ssid, go_dev_addr);
7621
7622 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
7623 return NULL;
7624
7625 if (!tag && wpa_s->conf->wps_nfc_dh_pubkey == NULL &&
7626 wps_nfc_gen_dh(&wpa_s->conf->wps_nfc_dh_pubkey,
7627 &wpa_s->conf->wps_nfc_dh_privkey) < 0)
7628 return NULL;
7629
7630 if (cli_freq == 0) {
7631 wsc = wps_build_nfc_handover_sel_p2p(
7632 wpa_s->parent->wps,
7633 tag ? wpa_s->conf->wps_nfc_dev_pw_id :
7634 DEV_PW_NFC_CONNECTION_HANDOVER,
7635 wpa_s->conf->wps_nfc_dh_pubkey,
7636 tag ? wpa_s->conf->wps_nfc_dev_pw : NULL);
7637 } else
7638 wsc = NULL;
7639 p2p = p2p_build_nfc_handover_sel(wpa_s->global->p2p, cli_freq,
7640 go_dev_addr, ssid ? ssid->ssid : NULL,
7641 ssid ? ssid->ssid_len : 0);
7642
7643 return wpas_p2p_nfc_handover(ndef, wsc, p2p);
7644}
7645
7646
7647static int wpas_p2p_nfc_join_group(struct wpa_supplicant *wpa_s,
7648 struct p2p_nfc_params *params)
7649{
7650 wpa_printf(MSG_DEBUG, "P2P: Initiate join-group based on NFC "
7651 "connection handover (freq=%d)",
7652 params->go_freq);
7653
7654 if (params->go_freq && params->go_ssid_len) {
7655 wpa_s->p2p_wps_method = WPS_NFC;
7656 wpa_s->pending_join_wps_method = WPS_NFC;
7657 os_memset(wpa_s->pending_join_iface_addr, 0, ETH_ALEN);
7658 os_memcpy(wpa_s->pending_join_dev_addr, params->go_dev_addr,
7659 ETH_ALEN);
7660 return wpas_p2p_join_start(wpa_s, params->go_freq,
7661 params->go_ssid,
7662 params->go_ssid_len);
7663 }
7664
7665 return wpas_p2p_connect(wpa_s, params->peer->p2p_device_addr, NULL,
7666 WPS_NFC, 0, 0, 1, 0, wpa_s->conf->p2p_go_intent,
7667 params->go_freq, -1, 0, 1, 1);
7668}
7669
7670
7671static int wpas_p2p_nfc_auth_join(struct wpa_supplicant *wpa_s,
7672 struct p2p_nfc_params *params, int tag)
7673{
7674 int res, persistent;
7675 struct wpa_ssid *ssid;
7676
7677 wpa_printf(MSG_DEBUG, "P2P: Authorize join-group based on NFC "
7678 "connection handover");
7679 for (wpa_s = wpa_s->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
7680 ssid = wpa_s->current_ssid;
7681 if (ssid == NULL)
7682 continue;
7683 if (ssid->mode != WPAS_MODE_P2P_GO)
7684 continue;
7685 if (wpa_s->ap_iface == NULL)
7686 continue;
7687 break;
7688 }
7689 if (wpa_s == NULL) {
7690 wpa_printf(MSG_DEBUG, "P2P: Could not find GO interface");
7691 return -1;
7692 }
7693
7694 if (wpa_s->parent->p2p_oob_dev_pw_id !=
7695 DEV_PW_NFC_CONNECTION_HANDOVER &&
7696 !wpa_s->parent->p2p_oob_dev_pw) {
7697 wpa_printf(MSG_DEBUG, "P2P: No NFC Dev Pw known");
7698 return -1;
7699 }
7700 res = wpas_ap_wps_add_nfc_pw(
7701 wpa_s, wpa_s->parent->p2p_oob_dev_pw_id,
7702 wpa_s->parent->p2p_oob_dev_pw,
7703 wpa_s->parent->p2p_peer_oob_pk_hash_known ?
7704 wpa_s->parent->p2p_peer_oob_pubkey_hash : NULL);
7705 if (res)
7706 return res;
7707
7708 if (!tag) {
7709 wpa_printf(MSG_DEBUG, "P2P: Negotiated handover - wait for peer to join without invitation");
7710 return 0;
7711 }
7712
7713 if (!params->peer ||
7714 !(params->peer->dev_capab & P2P_DEV_CAPAB_INVITATION_PROCEDURE))
7715 return 0;
7716
7717 wpa_printf(MSG_DEBUG, "P2P: Static handover - invite peer " MACSTR
7718 " to join", MAC2STR(params->peer->p2p_device_addr));
7719
7720 wpa_s->global->p2p_invite_group = wpa_s;
7721 persistent = ssid->p2p_persistent_group &&
7722 wpas_p2p_get_persistent(wpa_s->parent,
7723 params->peer->p2p_device_addr,
7724 ssid->ssid, ssid->ssid_len);
7725 wpa_s->parent->pending_invite_ssid_id = -1;
7726
7727 return p2p_invite(wpa_s->global->p2p, params->peer->p2p_device_addr,
7728 P2P_INVITE_ROLE_ACTIVE_GO, wpa_s->own_addr,
7729 ssid->ssid, ssid->ssid_len, ssid->frequency,
7730 wpa_s->global->p2p_dev_addr, persistent, 0,
7731 wpa_s->parent->p2p_oob_dev_pw_id);
7732}
7733
7734
7735static int wpas_p2p_nfc_init_go_neg(struct wpa_supplicant *wpa_s,
7736 struct p2p_nfc_params *params,
7737 int forced_freq)
7738{
7739 wpa_printf(MSG_DEBUG, "P2P: Initiate GO Negotiation based on NFC "
7740 "connection handover");
7741 return wpas_p2p_connect(wpa_s, params->peer->p2p_device_addr, NULL,
7742 WPS_NFC, 0, 0, 0, 0, wpa_s->conf->p2p_go_intent,
7743 forced_freq, -1, 0, 1, 1);
7744}
7745
7746
7747static int wpas_p2p_nfc_resp_go_neg(struct wpa_supplicant *wpa_s,
7748 struct p2p_nfc_params *params,
7749 int forced_freq)
7750{
7751 int res;
7752
7753 wpa_printf(MSG_DEBUG, "P2P: Authorize GO Negotiation based on NFC "
7754 "connection handover");
7755 res = wpas_p2p_connect(wpa_s, params->peer->p2p_device_addr, NULL,
7756 WPS_NFC, 0, 0, 0, 1, wpa_s->conf->p2p_go_intent,
7757 forced_freq, -1, 0, 1, 1);
7758 if (res)
7759 return res;
7760
7761 res = wpas_p2p_listen(wpa_s, 60);
7762 if (res) {
7763 p2p_unauthorize(wpa_s->global->p2p,
7764 params->peer->p2p_device_addr);
7765 }
7766
7767 return res;
7768}
7769
7770
7771static int wpas_p2p_nfc_connection_handover(struct wpa_supplicant *wpa_s,
7772 const struct wpabuf *data,
7773 int sel, int tag, int forced_freq)
7774{
7775 const u8 *pos, *end;
7776 u16 len, id;
7777 struct p2p_nfc_params params;
7778 int res;
7779
7780 os_memset(&params, 0, sizeof(params));
7781 params.sel = sel;
7782
7783 wpa_hexdump_buf(MSG_DEBUG, "P2P: Received NFC tag payload", data);
7784
7785 pos = wpabuf_head(data);
7786 end = pos + wpabuf_len(data);
7787
7788 if (end - pos < 2) {
7789 wpa_printf(MSG_DEBUG, "P2P: Not enough data for Length of WSC "
7790 "attributes");
7791 return -1;
7792 }
7793 len = WPA_GET_BE16(pos);
7794 pos += 2;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007795 if (len > end - pos) {
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08007796 wpa_printf(MSG_DEBUG, "P2P: Not enough data for WSC "
7797 "attributes");
7798 return -1;
7799 }
7800 params.wsc_attr = pos;
7801 params.wsc_len = len;
7802 pos += len;
7803
7804 if (end - pos < 2) {
7805 wpa_printf(MSG_DEBUG, "P2P: Not enough data for Length of P2P "
7806 "attributes");
7807 return -1;
7808 }
7809 len = WPA_GET_BE16(pos);
7810 pos += 2;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007811 if (len > end - pos) {
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08007812 wpa_printf(MSG_DEBUG, "P2P: Not enough data for P2P "
7813 "attributes");
7814 return -1;
7815 }
7816 params.p2p_attr = pos;
7817 params.p2p_len = len;
7818 pos += len;
7819
7820 wpa_hexdump(MSG_DEBUG, "P2P: WSC attributes",
7821 params.wsc_attr, params.wsc_len);
7822 wpa_hexdump(MSG_DEBUG, "P2P: P2P attributes",
7823 params.p2p_attr, params.p2p_len);
7824 if (pos < end) {
7825 wpa_hexdump(MSG_DEBUG,
7826 "P2P: Ignored extra data after P2P attributes",
7827 pos, end - pos);
7828 }
7829
7830 res = p2p_process_nfc_connection_handover(wpa_s->global->p2p, &params);
7831 if (res)
7832 return res;
7833
7834 if (params.next_step == NO_ACTION)
7835 return 0;
7836
7837 if (params.next_step == BOTH_GO) {
7838 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_NFC_BOTH_GO "peer=" MACSTR,
7839 MAC2STR(params.peer->p2p_device_addr));
7840 return 0;
7841 }
7842
7843 if (params.next_step == PEER_CLIENT) {
7844 if (!is_zero_ether_addr(params.go_dev_addr)) {
7845 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_NFC_PEER_CLIENT
7846 "peer=" MACSTR " freq=%d go_dev_addr=" MACSTR
7847 " ssid=\"%s\"",
7848 MAC2STR(params.peer->p2p_device_addr),
7849 params.go_freq,
7850 MAC2STR(params.go_dev_addr),
7851 wpa_ssid_txt(params.go_ssid,
7852 params.go_ssid_len));
7853 } else {
7854 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_NFC_PEER_CLIENT
7855 "peer=" MACSTR " freq=%d",
7856 MAC2STR(params.peer->p2p_device_addr),
7857 params.go_freq);
7858 }
7859 return 0;
7860 }
7861
7862 if (wpas_p2p_cli_freq(wpa_s, NULL, NULL)) {
7863 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_NFC_WHILE_CLIENT "peer="
7864 MACSTR, MAC2STR(params.peer->p2p_device_addr));
7865 return 0;
7866 }
7867
7868 wpabuf_free(wpa_s->p2p_oob_dev_pw);
7869 wpa_s->p2p_oob_dev_pw = NULL;
7870
7871 if (params.oob_dev_pw_len < WPS_OOB_PUBKEY_HASH_LEN + 2) {
7872 wpa_printf(MSG_DEBUG, "P2P: No peer OOB Dev Pw "
7873 "received");
7874 return -1;
7875 }
7876
7877 id = WPA_GET_BE16(params.oob_dev_pw + WPS_OOB_PUBKEY_HASH_LEN);
7878 wpa_printf(MSG_DEBUG, "P2P: Peer OOB Dev Pw %u", id);
7879 wpa_hexdump(MSG_DEBUG, "P2P: Peer OOB Public Key hash",
7880 params.oob_dev_pw, WPS_OOB_PUBKEY_HASH_LEN);
7881 os_memcpy(wpa_s->p2p_peer_oob_pubkey_hash,
7882 params.oob_dev_pw, WPS_OOB_PUBKEY_HASH_LEN);
7883 wpa_s->p2p_peer_oob_pk_hash_known = 1;
7884
7885 if (tag) {
7886 if (id < 0x10) {
7887 wpa_printf(MSG_DEBUG, "P2P: Static handover - invalid "
7888 "peer OOB Device Password Id %u", id);
7889 return -1;
7890 }
7891 wpa_printf(MSG_DEBUG, "P2P: Static handover - use peer OOB "
7892 "Device Password Id %u", id);
7893 wpa_hexdump_key(MSG_DEBUG, "P2P: Peer OOB Device Password",
7894 params.oob_dev_pw + WPS_OOB_PUBKEY_HASH_LEN + 2,
7895 params.oob_dev_pw_len -
7896 WPS_OOB_PUBKEY_HASH_LEN - 2);
7897 wpa_s->p2p_oob_dev_pw_id = id;
7898 wpa_s->p2p_oob_dev_pw = wpabuf_alloc_copy(
7899 params.oob_dev_pw + WPS_OOB_PUBKEY_HASH_LEN + 2,
7900 params.oob_dev_pw_len -
7901 WPS_OOB_PUBKEY_HASH_LEN - 2);
7902 if (wpa_s->p2p_oob_dev_pw == NULL)
7903 return -1;
7904
7905 if (wpa_s->conf->wps_nfc_dh_pubkey == NULL &&
7906 wps_nfc_gen_dh(&wpa_s->conf->wps_nfc_dh_pubkey,
7907 &wpa_s->conf->wps_nfc_dh_privkey) < 0)
7908 return -1;
7909 } else {
7910 wpa_printf(MSG_DEBUG, "P2P: Using abbreviated WPS handshake "
7911 "without Device Password");
7912 wpa_s->p2p_oob_dev_pw_id = DEV_PW_NFC_CONNECTION_HANDOVER;
7913 }
7914
7915 switch (params.next_step) {
7916 case NO_ACTION:
7917 case BOTH_GO:
7918 case PEER_CLIENT:
7919 /* already covered above */
7920 return 0;
7921 case JOIN_GROUP:
7922 return wpas_p2p_nfc_join_group(wpa_s, &params);
7923 case AUTH_JOIN:
7924 return wpas_p2p_nfc_auth_join(wpa_s, &params, tag);
7925 case INIT_GO_NEG:
7926 return wpas_p2p_nfc_init_go_neg(wpa_s, &params, forced_freq);
7927 case RESP_GO_NEG:
7928 /* TODO: use own OOB Dev Pw */
7929 return wpas_p2p_nfc_resp_go_neg(wpa_s, &params, forced_freq);
7930 }
7931
7932 return -1;
7933}
7934
7935
7936int wpas_p2p_nfc_tag_process(struct wpa_supplicant *wpa_s,
7937 const struct wpabuf *data, int forced_freq)
7938{
7939 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
7940 return -1;
7941
7942 return wpas_p2p_nfc_connection_handover(wpa_s, data, 1, 1, forced_freq);
7943}
7944
7945
7946int wpas_p2p_nfc_report_handover(struct wpa_supplicant *wpa_s, int init,
7947 const struct wpabuf *req,
7948 const struct wpabuf *sel, int forced_freq)
7949{
7950 struct wpabuf *tmp;
7951 int ret;
7952
7953 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
7954 return -1;
7955
7956 wpa_printf(MSG_DEBUG, "NFC: P2P connection handover reported");
7957
7958 wpa_hexdump_ascii(MSG_DEBUG, "NFC: Req",
7959 wpabuf_head(req), wpabuf_len(req));
7960 wpa_hexdump_ascii(MSG_DEBUG, "NFC: Sel",
7961 wpabuf_head(sel), wpabuf_len(sel));
7962 if (forced_freq)
7963 wpa_printf(MSG_DEBUG, "NFC: Forced freq %d", forced_freq);
7964 tmp = ndef_parse_p2p(init ? sel : req);
7965 if (tmp == NULL) {
7966 wpa_printf(MSG_DEBUG, "P2P: Could not parse NDEF");
7967 return -1;
7968 }
7969
7970 ret = wpas_p2p_nfc_connection_handover(wpa_s, tmp, init, 0,
7971 forced_freq);
7972 wpabuf_free(tmp);
7973
7974 return ret;
7975}
7976
7977
7978int wpas_p2p_nfc_tag_enabled(struct wpa_supplicant *wpa_s, int enabled)
7979{
7980 const u8 *if_addr;
7981 int go_intent = wpa_s->conf->p2p_go_intent;
7982 struct wpa_supplicant *iface;
7983
7984 if (wpa_s->global->p2p == NULL)
7985 return -1;
7986
7987 if (!enabled) {
7988 wpa_printf(MSG_DEBUG, "P2P: Disable use of own NFC Tag");
7989 for (iface = wpa_s->global->ifaces; iface; iface = iface->next)
7990 {
7991 if (!iface->ap_iface)
7992 continue;
7993 hostapd_wps_nfc_token_disable(iface->ap_iface->bss[0]);
7994 }
7995 p2p_set_authorized_oob_dev_pw_id(wpa_s->global->p2p, 0,
7996 0, NULL);
7997 if (wpa_s->p2p_nfc_tag_enabled)
7998 wpas_p2p_remove_pending_group_interface(wpa_s);
7999 wpa_s->p2p_nfc_tag_enabled = 0;
8000 return 0;
8001 }
8002
8003 if (wpa_s->global->p2p_disabled)
8004 return -1;
8005
8006 if (wpa_s->conf->wps_nfc_dh_pubkey == NULL ||
8007 wpa_s->conf->wps_nfc_dh_privkey == NULL ||
8008 wpa_s->conf->wps_nfc_dev_pw == NULL ||
8009 wpa_s->conf->wps_nfc_dev_pw_id < 0x10) {
8010 wpa_printf(MSG_DEBUG, "P2P: NFC password token not configured "
8011 "to allow static handover cases");
8012 return -1;
8013 }
8014
8015 wpa_printf(MSG_DEBUG, "P2P: Enable use of own NFC Tag");
8016
8017 wpa_s->p2p_oob_dev_pw_id = wpa_s->conf->wps_nfc_dev_pw_id;
8018 wpabuf_free(wpa_s->p2p_oob_dev_pw);
8019 wpa_s->p2p_oob_dev_pw = wpabuf_dup(wpa_s->conf->wps_nfc_dev_pw);
8020 if (wpa_s->p2p_oob_dev_pw == NULL)
8021 return -1;
8022 wpa_s->p2p_peer_oob_pk_hash_known = 0;
8023
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -07008024 if (wpa_s->p2p_group_interface == P2P_GROUP_INTERFACE_GO ||
8025 wpa_s->p2p_group_interface == P2P_GROUP_INTERFACE_CLIENT) {
8026 /*
8027 * P2P Group Interface present and the command came on group
8028 * interface, so enable the token for the current interface.
8029 */
8030 wpa_s->create_p2p_iface = 0;
8031 } else {
8032 wpa_s->create_p2p_iface = wpas_p2p_create_iface(wpa_s);
8033 }
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08008034
8035 if (wpa_s->create_p2p_iface) {
8036 enum wpa_driver_if_type iftype;
8037 /* Prepare to add a new interface for the group */
8038 iftype = WPA_IF_P2P_GROUP;
8039 if (go_intent == 15)
8040 iftype = WPA_IF_P2P_GO;
8041 if (wpas_p2p_add_group_interface(wpa_s, iftype) < 0) {
8042 wpa_printf(MSG_ERROR, "P2P: Failed to allocate a new "
8043 "interface for the group");
8044 return -1;
8045 }
8046
8047 if_addr = wpa_s->pending_interface_addr;
8048 } else
8049 if_addr = wpa_s->own_addr;
8050
8051 wpa_s->p2p_nfc_tag_enabled = enabled;
8052
8053 for (iface = wpa_s->global->ifaces; iface; iface = iface->next) {
8054 struct hostapd_data *hapd;
8055 if (iface->ap_iface == NULL)
8056 continue;
8057 hapd = iface->ap_iface->bss[0];
8058 wpabuf_free(hapd->conf->wps_nfc_dh_pubkey);
8059 hapd->conf->wps_nfc_dh_pubkey =
8060 wpabuf_dup(wpa_s->conf->wps_nfc_dh_pubkey);
8061 wpabuf_free(hapd->conf->wps_nfc_dh_privkey);
8062 hapd->conf->wps_nfc_dh_privkey =
8063 wpabuf_dup(wpa_s->conf->wps_nfc_dh_privkey);
8064 wpabuf_free(hapd->conf->wps_nfc_dev_pw);
8065 hapd->conf->wps_nfc_dev_pw =
8066 wpabuf_dup(wpa_s->conf->wps_nfc_dev_pw);
8067 hapd->conf->wps_nfc_dev_pw_id = wpa_s->conf->wps_nfc_dev_pw_id;
8068
8069 if (hostapd_wps_nfc_token_enable(iface->ap_iface->bss[0]) < 0) {
8070 wpa_dbg(iface, MSG_DEBUG,
8071 "P2P: Failed to enable NFC Tag for GO");
8072 }
8073 }
8074 p2p_set_authorized_oob_dev_pw_id(
8075 wpa_s->global->p2p, wpa_s->conf->wps_nfc_dev_pw_id, go_intent,
8076 if_addr);
8077
8078 return 0;
8079}
8080
8081#endif /* CONFIG_WPS_NFC */
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07008082
8083
8084static void wpas_p2p_optimize_listen_channel(struct wpa_supplicant *wpa_s,
8085 struct wpa_used_freq_data *freqs,
8086 unsigned int num)
8087{
8088 u8 curr_chan, cand, chan;
8089 unsigned int i;
8090
8091 curr_chan = p2p_get_listen_channel(wpa_s->global->p2p);
8092 for (i = 0, cand = 0; i < num; i++) {
8093 ieee80211_freq_to_chan(freqs[i].freq, &chan);
8094 if (curr_chan == chan) {
8095 cand = 0;
8096 break;
8097 }
8098
8099 if (chan == 1 || chan == 6 || chan == 11)
8100 cand = chan;
8101 }
8102
8103 if (cand) {
8104 wpa_dbg(wpa_s, MSG_DEBUG,
8105 "P2P: Update Listen channel to %u baased on operating channel",
8106 cand);
8107 p2p_set_listen_channel(wpa_s->global->p2p, 81, cand, 0);
8108 }
8109}
8110
8111
8112void wpas_p2p_indicate_state_change(struct wpa_supplicant *wpa_s)
8113{
8114 struct wpa_used_freq_data *freqs;
8115 unsigned int num = wpa_s->num_multichan_concurrent;
8116
8117 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
8118 return;
8119
8120 /*
8121 * If possible, optimize the Listen channel to be a channel that is
8122 * already used by one of the other interfaces.
8123 */
8124 if (!wpa_s->conf->p2p_optimize_listen_chan)
8125 return;
8126
8127 if (!wpa_s->current_ssid || wpa_s->wpa_state != WPA_COMPLETED)
8128 return;
8129
8130 freqs = os_calloc(num, sizeof(struct wpa_used_freq_data));
8131 if (!freqs)
8132 return;
8133
8134 num = get_shared_radio_freqs_data(wpa_s, freqs, num);
8135
8136 wpas_p2p_optimize_listen_channel(wpa_s, freqs, num);
8137 os_free(freqs);
8138}
8139
8140
8141void wpas_p2p_deinit_iface(struct wpa_supplicant *wpa_s)
8142{
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07008143 if (wpa_s == wpa_s->global->p2p_init_wpa_s && wpa_s->global->p2p) {
8144 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Disable P2P since removing "
8145 "the management interface is being removed");
8146 wpas_p2p_deinit_global(wpa_s->global);
8147 }
8148}
8149
8150
8151void wpas_p2p_ap_deinit(struct wpa_supplicant *wpa_s)
8152{
8153 if (wpa_s->ap_iface->bss)
8154 wpa_s->ap_iface->bss[0]->p2p_group = NULL;
8155 wpas_p2p_group_deinit(wpa_s);
8156}