blob: 59f95c355acf54fb1c298edd31cacb906e1afcbd [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 Shmidtcf32e602014-01-28 10:57:39 -08001549
1550 if (s->wps_nfc_dh_privkey && s->wps_nfc_dh_pubkey) {
1551 d->wps_nfc_dh_privkey = wpabuf_dup(s->wps_nfc_dh_privkey);
1552 d->wps_nfc_dh_pubkey = wpabuf_dup(s->wps_nfc_dh_pubkey);
1553 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001554}
1555
1556
Dmitry Shmidt34af3062013-07-11 10:46:32 -07001557static void wpas_p2p_get_group_ifname(struct wpa_supplicant *wpa_s,
1558 char *ifname, size_t len)
1559{
1560 char *ifname_ptr = wpa_s->ifname;
1561
1562 if (os_strncmp(wpa_s->ifname, P2P_MGMT_DEVICE_PREFIX,
1563 os_strlen(P2P_MGMT_DEVICE_PREFIX)) == 0) {
1564 ifname_ptr = os_strrchr(wpa_s->ifname, '-') + 1;
1565 }
1566
1567 os_snprintf(ifname, len, "p2p-%s-%d", ifname_ptr, wpa_s->p2p_group_idx);
1568 if (os_strlen(ifname) >= IFNAMSIZ &&
1569 os_strlen(wpa_s->ifname) < IFNAMSIZ) {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001570 int res;
1571
Dmitry Shmidt34af3062013-07-11 10:46:32 -07001572 /* Try to avoid going over the IFNAMSIZ length limit */
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001573 res = os_snprintf(ifname, len, "p2p-%d", wpa_s->p2p_group_idx);
1574 if (os_snprintf_error(len, res) && len)
1575 ifname[len - 1] = '\0';
Dmitry Shmidt34af3062013-07-11 10:46:32 -07001576 }
1577}
1578
1579
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001580static int wpas_p2p_add_group_interface(struct wpa_supplicant *wpa_s,
1581 enum wpa_driver_if_type type)
1582{
1583 char ifname[120], force_ifname[120];
1584
1585 if (wpa_s->pending_interface_name[0]) {
1586 wpa_printf(MSG_DEBUG, "P2P: Pending virtual interface exists "
1587 "- skip creation of a new one");
1588 if (is_zero_ether_addr(wpa_s->pending_interface_addr)) {
1589 wpa_printf(MSG_DEBUG, "P2P: Pending virtual address "
1590 "unknown?! ifname='%s'",
1591 wpa_s->pending_interface_name);
1592 return -1;
1593 }
1594 return 0;
1595 }
1596
Dmitry Shmidt34af3062013-07-11 10:46:32 -07001597 wpas_p2p_get_group_ifname(wpa_s, ifname, sizeof(ifname));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001598 force_ifname[0] = '\0';
1599
1600 wpa_printf(MSG_DEBUG, "P2P: Create a new interface %s for the group",
1601 ifname);
1602 wpa_s->p2p_group_idx++;
1603
1604 wpa_s->pending_interface_type = type;
1605 if (wpa_drv_if_add(wpa_s, type, ifname, NULL, NULL, force_ifname,
1606 wpa_s->pending_interface_addr, NULL) < 0) {
1607 wpa_printf(MSG_ERROR, "P2P: Failed to create new group "
1608 "interface");
1609 return -1;
1610 }
1611
1612 if (force_ifname[0]) {
1613 wpa_printf(MSG_DEBUG, "P2P: Driver forced interface name %s",
1614 force_ifname);
1615 os_strlcpy(wpa_s->pending_interface_name, force_ifname,
1616 sizeof(wpa_s->pending_interface_name));
1617 } else
1618 os_strlcpy(wpa_s->pending_interface_name, ifname,
1619 sizeof(wpa_s->pending_interface_name));
1620 wpa_printf(MSG_DEBUG, "P2P: Created pending virtual interface %s addr "
1621 MACSTR, wpa_s->pending_interface_name,
1622 MAC2STR(wpa_s->pending_interface_addr));
1623
1624 return 0;
1625}
1626
1627
1628static void wpas_p2p_remove_pending_group_interface(
1629 struct wpa_supplicant *wpa_s)
1630{
1631 if (!wpa_s->pending_interface_name[0] ||
1632 is_zero_ether_addr(wpa_s->pending_interface_addr))
1633 return; /* No pending virtual interface */
1634
1635 wpa_printf(MSG_DEBUG, "P2P: Removing pending group interface %s",
1636 wpa_s->pending_interface_name);
1637 wpa_drv_if_remove(wpa_s, wpa_s->pending_interface_type,
1638 wpa_s->pending_interface_name);
1639 os_memset(wpa_s->pending_interface_addr, 0, ETH_ALEN);
1640 wpa_s->pending_interface_name[0] = '\0';
1641}
1642
1643
1644static struct wpa_supplicant *
1645wpas_p2p_init_group_interface(struct wpa_supplicant *wpa_s, int go)
1646{
1647 struct wpa_interface iface;
1648 struct wpa_supplicant *group_wpa_s;
1649
1650 if (!wpa_s->pending_interface_name[0]) {
1651 wpa_printf(MSG_ERROR, "P2P: No pending group interface");
1652 if (!wpas_p2p_create_iface(wpa_s))
1653 return NULL;
1654 /*
1655 * Something has forced us to remove the pending interface; try
1656 * to create a new one and hope for the best that we will get
1657 * the same local address.
1658 */
1659 if (wpas_p2p_add_group_interface(wpa_s, go ? WPA_IF_P2P_GO :
1660 WPA_IF_P2P_CLIENT) < 0)
1661 return NULL;
1662 }
1663
1664 os_memset(&iface, 0, sizeof(iface));
1665 iface.ifname = wpa_s->pending_interface_name;
1666 iface.driver = wpa_s->driver->name;
Dmitry Shmidt34af3062013-07-11 10:46:32 -07001667 if (wpa_s->conf->ctrl_interface == NULL &&
1668 wpa_s->parent != wpa_s &&
1669 wpa_s->p2p_mgmt &&
1670 (wpa_s->drv_flags & WPA_DRIVER_FLAGS_DEDICATED_P2P_DEVICE))
1671 iface.ctrl_interface = wpa_s->parent->conf->ctrl_interface;
1672 else
1673 iface.ctrl_interface = wpa_s->conf->ctrl_interface;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001674 iface.driver_param = wpa_s->conf->driver_param;
1675 group_wpa_s = wpa_supplicant_add_iface(wpa_s->global, &iface);
1676 if (group_wpa_s == NULL) {
1677 wpa_printf(MSG_ERROR, "P2P: Failed to create new "
1678 "wpa_supplicant interface");
1679 return NULL;
1680 }
1681 wpa_s->pending_interface_name[0] = '\0';
1682 group_wpa_s->parent = wpa_s;
1683 group_wpa_s->p2p_group_interface = go ? P2P_GROUP_INTERFACE_GO :
1684 P2P_GROUP_INTERFACE_CLIENT;
1685 wpa_s->global->p2p_group_formation = group_wpa_s;
1686
1687 wpas_p2p_clone_config(group_wpa_s, wpa_s);
1688
1689 return group_wpa_s;
1690}
1691
1692
1693static void wpas_p2p_group_formation_timeout(void *eloop_ctx,
1694 void *timeout_ctx)
1695{
1696 struct wpa_supplicant *wpa_s = eloop_ctx;
1697 wpa_printf(MSG_DEBUG, "P2P: Group Formation timed out");
Dmitry Shmidt34af3062013-07-11 10:46:32 -07001698 wpas_p2p_group_formation_failed(wpa_s);
1699}
1700
1701
1702void wpas_p2p_group_formation_failed(struct wpa_supplicant *wpa_s)
1703{
1704 eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
1705 wpa_s->parent, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001706 if (wpa_s->global->p2p)
1707 p2p_group_formation_failed(wpa_s->global->p2p);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001708 wpas_group_formation_completed(wpa_s, 0);
1709}
1710
1711
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001712static void wpas_p2p_grpform_fail_after_wps(struct wpa_supplicant *wpa_s)
1713{
1714 wpa_printf(MSG_DEBUG, "P2P: Reject group formation due to WPS provisioning failure");
1715 eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
1716 wpa_s->parent, NULL);
1717 eloop_register_timeout(0, 0, wpas_p2p_group_formation_timeout,
1718 wpa_s->parent, NULL);
1719 wpa_s->global->p2p_fail_on_wps_complete = 0;
1720}
1721
1722
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07001723void wpas_p2p_ap_setup_failed(struct wpa_supplicant *wpa_s)
1724{
1725 if (wpa_s->global->p2p_group_formation != wpa_s)
1726 return;
1727 /* Speed up group formation timeout since this cannot succeed */
1728 eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
1729 wpa_s->parent, NULL);
1730 eloop_register_timeout(0, 0, wpas_p2p_group_formation_timeout,
1731 wpa_s->parent, NULL);
1732}
1733
1734
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001735static void wpas_go_neg_completed(void *ctx, struct p2p_go_neg_results *res)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001736{
1737 struct wpa_supplicant *wpa_s = ctx;
1738
1739 if (wpa_s->off_channel_freq || wpa_s->roc_waiting_drv_freq) {
1740 wpa_drv_cancel_remain_on_channel(wpa_s);
1741 wpa_s->off_channel_freq = 0;
1742 wpa_s->roc_waiting_drv_freq = 0;
1743 }
1744
1745 if (res->status) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07001746 wpa_msg_global(wpa_s, MSG_INFO,
1747 P2P_EVENT_GO_NEG_FAILURE "status=%d",
1748 res->status);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001749 wpas_notify_p2p_go_neg_completed(wpa_s, res);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001750 wpas_p2p_remove_pending_group_interface(wpa_s);
1751 return;
1752 }
1753
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001754 if (wpa_s->p2p_go_ht40)
1755 res->ht40 = 1;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07001756 if (wpa_s->p2p_go_vht)
1757 res->vht = 1;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001758
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07001759 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_GO_NEG_SUCCESS "role=%s "
1760 "freq=%d ht40=%d peer_dev=" MACSTR " peer_iface=" MACSTR
1761 " wps_method=%s",
1762 res->role_go ? "GO" : "client", res->freq, res->ht40,
1763 MAC2STR(res->peer_device_addr),
1764 MAC2STR(res->peer_interface_addr),
1765 p2p_wps_method_text(res->wps_method));
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001766 wpas_notify_p2p_go_neg_completed(wpa_s, res);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001767
Dmitry Shmidt04949592012-07-19 12:16:46 -07001768 if (res->role_go && wpa_s->p2p_persistent_id >= 0) {
1769 struct wpa_ssid *ssid;
1770 ssid = wpa_config_get_network(wpa_s->conf,
1771 wpa_s->p2p_persistent_id);
1772 if (ssid && ssid->disabled == 2 &&
1773 ssid->mode == WPAS_MODE_P2P_GO && ssid->passphrase) {
1774 size_t len = os_strlen(ssid->passphrase);
1775 wpa_printf(MSG_DEBUG, "P2P: Override passphrase based "
1776 "on requested persistent group");
1777 os_memcpy(res->passphrase, ssid->passphrase, len);
1778 res->passphrase[len] = '\0';
1779 }
1780 }
1781
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001782 if (wpa_s->create_p2p_iface) {
1783 struct wpa_supplicant *group_wpa_s =
1784 wpas_p2p_init_group_interface(wpa_s, res->role_go);
1785 if (group_wpa_s == NULL) {
1786 wpas_p2p_remove_pending_group_interface(wpa_s);
Dmitry Shmidtd11f0192014-03-24 12:09:47 -07001787 eloop_cancel_timeout(wpas_p2p_long_listen_timeout,
1788 wpa_s, NULL);
1789 wpas_p2p_group_formation_failed(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001790 return;
1791 }
1792 if (group_wpa_s != wpa_s) {
1793 os_memcpy(group_wpa_s->p2p_pin, wpa_s->p2p_pin,
1794 sizeof(group_wpa_s->p2p_pin));
1795 group_wpa_s->p2p_wps_method = wpa_s->p2p_wps_method;
1796 }
1797 os_memset(wpa_s->pending_interface_addr, 0, ETH_ALEN);
1798 wpa_s->pending_interface_name[0] = '\0';
1799 group_wpa_s->p2p_in_provisioning = 1;
1800
1801 if (res->role_go)
1802 wpas_start_wps_go(group_wpa_s, res, 1);
1803 else
1804 wpas_start_wps_enrollee(group_wpa_s, res);
1805 } else {
1806 wpa_s->p2p_in_provisioning = 1;
1807 wpa_s->global->p2p_group_formation = wpa_s;
1808
1809 if (res->role_go)
1810 wpas_start_wps_go(wpa_s, res, 1);
1811 else
1812 wpas_start_wps_enrollee(ctx, res);
1813 }
1814
1815 wpa_s->p2p_long_listen = 0;
1816 eloop_cancel_timeout(wpas_p2p_long_listen_timeout, wpa_s, NULL);
1817
1818 eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s, NULL);
1819 eloop_register_timeout(15 + res->peer_config_timeout / 100,
1820 (res->peer_config_timeout % 100) * 10000,
1821 wpas_p2p_group_formation_timeout, wpa_s, NULL);
1822}
1823
1824
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001825static void wpas_go_neg_req_rx(void *ctx, const u8 *src, u16 dev_passwd_id)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001826{
1827 struct wpa_supplicant *wpa_s = ctx;
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07001828 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_GO_NEG_REQUEST MACSTR
1829 " dev_passwd_id=%u", MAC2STR(src), dev_passwd_id);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001830
1831 wpas_notify_p2p_go_neg_req(wpa_s, src, dev_passwd_id);
1832}
1833
1834
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001835static void wpas_dev_found(void *ctx, const u8 *addr,
1836 const struct p2p_peer_info *info,
1837 int new_device)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001838{
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001839#ifndef CONFIG_NO_STDOUT_DEBUG
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001840 struct wpa_supplicant *wpa_s = ctx;
1841 char devtype[WPS_DEV_TYPE_BUFSIZE];
Dmitry Shmidt04f534e2013-12-09 15:50:16 -08001842 char *wfd_dev_info_hex = NULL;
1843
Irfan Sheriff8367dc92012-09-09 17:08:19 -07001844#ifdef CONFIG_WIFI_DISPLAY
Dmitry Shmidt04f534e2013-12-09 15:50:16 -08001845 wfd_dev_info_hex = wifi_display_subelem_hex(info->wfd_subelems,
1846 WFD_SUBELEM_DEVICE_INFO);
Irfan Sheriff8367dc92012-09-09 17:08:19 -07001847#endif /* CONFIG_WIFI_DISPLAY */
Dmitry Shmidt04f534e2013-12-09 15:50:16 -08001848
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07001849 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_DEVICE_FOUND MACSTR
1850 " p2p_dev_addr=" MACSTR
1851 " pri_dev_type=%s name='%s' config_methods=0x%x "
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001852 "dev_capab=0x%x group_capab=0x%x%s%s%s new=%d",
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07001853 MAC2STR(addr), MAC2STR(info->p2p_device_addr),
1854 wps_dev_type_bin2str(info->pri_dev_type, devtype,
1855 sizeof(devtype)),
1856 info->device_name, info->config_methods,
1857 info->dev_capab, info->group_capab,
Dmitry Shmidt04f534e2013-12-09 15:50:16 -08001858 wfd_dev_info_hex ? " wfd_dev_info=0x" : "",
Dmitry Shmidt2e67f062014-07-16 09:55:28 -07001859 wfd_dev_info_hex ? wfd_dev_info_hex : "",
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001860 info->vendor_elems ? " vendor_elems=1" : "",
1861 new_device);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001862
Dmitry Shmidt04f534e2013-12-09 15:50:16 -08001863 os_free(wfd_dev_info_hex);
Dmitry Shmidt97672262014-02-03 13:02:54 -08001864#endif /* CONFIG_NO_STDOUT_DEBUG */
Dmitry Shmidt04f534e2013-12-09 15:50:16 -08001865
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001866 wpas_notify_p2p_device_found(ctx, info->p2p_device_addr, new_device);
1867}
1868
1869
1870static void wpas_dev_lost(void *ctx, const u8 *dev_addr)
1871{
1872 struct wpa_supplicant *wpa_s = ctx;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001873
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07001874 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_DEVICE_LOST
1875 "p2p_dev_addr=" MACSTR, MAC2STR(dev_addr));
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001876
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001877 wpas_notify_p2p_device_lost(wpa_s, dev_addr);
1878}
1879
1880
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07001881static void wpas_find_stopped(void *ctx)
1882{
1883 struct wpa_supplicant *wpa_s = ctx;
1884 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_FIND_STOPPED);
1885}
1886
1887
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001888struct wpas_p2p_listen_work {
1889 unsigned int freq;
1890 unsigned int duration;
1891 struct wpabuf *probe_resp_ie;
1892};
1893
1894
1895static void wpas_p2p_listen_work_free(struct wpas_p2p_listen_work *lwork)
1896{
1897 if (lwork == NULL)
1898 return;
1899 wpabuf_free(lwork->probe_resp_ie);
1900 os_free(lwork);
1901}
1902
1903
1904static void wpas_p2p_listen_work_done(struct wpa_supplicant *wpa_s)
1905{
1906 struct wpas_p2p_listen_work *lwork;
1907
1908 if (!wpa_s->p2p_listen_work)
1909 return;
1910
1911 lwork = wpa_s->p2p_listen_work->ctx;
1912 wpas_p2p_listen_work_free(lwork);
1913 radio_work_done(wpa_s->p2p_listen_work);
1914 wpa_s->p2p_listen_work = NULL;
1915}
1916
1917
1918static void wpas_start_listen_cb(struct wpa_radio_work *work, int deinit)
1919{
1920 struct wpa_supplicant *wpa_s = work->wpa_s;
1921 struct wpas_p2p_listen_work *lwork = work->ctx;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001922 unsigned int duration;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001923
1924 if (deinit) {
Dmitry Shmidtbd14a572014-02-18 10:33:49 -08001925 if (work->started) {
1926 wpa_s->p2p_listen_work = NULL;
1927 wpas_stop_listen(wpa_s);
1928 }
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001929 wpas_p2p_listen_work_free(lwork);
1930 return;
1931 }
1932
1933 wpa_s->p2p_listen_work = work;
1934
1935 wpa_drv_set_ap_wps_ie(wpa_s, NULL, lwork->probe_resp_ie, NULL);
1936
1937 if (wpa_drv_probe_req_report(wpa_s, 1) < 0) {
1938 wpa_printf(MSG_DEBUG, "P2P: Failed to request the driver to "
1939 "report received Probe Request frames");
1940 wpas_p2p_listen_work_done(wpa_s);
1941 return;
1942 }
1943
1944 wpa_s->pending_listen_freq = lwork->freq;
1945 wpa_s->pending_listen_duration = lwork->duration;
1946
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001947 duration = lwork->duration;
1948#ifdef CONFIG_TESTING_OPTIONS
1949 if (wpa_s->extra_roc_dur) {
1950 wpa_printf(MSG_DEBUG, "TESTING: Increase ROC duration %u -> %u",
1951 duration, duration + wpa_s->extra_roc_dur);
1952 duration += wpa_s->extra_roc_dur;
1953 }
1954#endif /* CONFIG_TESTING_OPTIONS */
1955
1956 if (wpa_drv_remain_on_channel(wpa_s, lwork->freq, duration) < 0) {
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001957 wpa_printf(MSG_DEBUG, "P2P: Failed to request the driver "
1958 "to remain on channel (%u MHz) for Listen "
1959 "state", lwork->freq);
1960 wpas_p2p_listen_work_done(wpa_s);
1961 wpa_s->pending_listen_freq = 0;
1962 return;
1963 }
1964 wpa_s->off_channel_freq = 0;
1965 wpa_s->roc_waiting_drv_freq = lwork->freq;
1966}
1967
1968
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001969static int wpas_start_listen(void *ctx, unsigned int freq,
1970 unsigned int duration,
1971 const struct wpabuf *probe_resp_ie)
1972{
1973 struct wpa_supplicant *wpa_s = ctx;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001974 struct wpas_p2p_listen_work *lwork;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001975
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001976 if (wpa_s->p2p_listen_work) {
1977 wpa_printf(MSG_DEBUG, "P2P: Reject start_listen since p2p_listen_work already exists");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001978 return -1;
1979 }
1980
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001981 lwork = os_zalloc(sizeof(*lwork));
1982 if (lwork == NULL)
1983 return -1;
1984 lwork->freq = freq;
1985 lwork->duration = duration;
1986 if (probe_resp_ie) {
1987 lwork->probe_resp_ie = wpabuf_dup(probe_resp_ie);
1988 if (lwork->probe_resp_ie == NULL) {
1989 wpas_p2p_listen_work_free(lwork);
1990 return -1;
1991 }
1992 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001993
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001994 if (radio_add_work(wpa_s, freq, "p2p-listen", 0, wpas_start_listen_cb,
1995 lwork) < 0) {
1996 wpas_p2p_listen_work_free(lwork);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001997 return -1;
1998 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001999
2000 return 0;
2001}
2002
2003
2004static void wpas_stop_listen(void *ctx)
2005{
2006 struct wpa_supplicant *wpa_s = ctx;
2007 if (wpa_s->off_channel_freq || wpa_s->roc_waiting_drv_freq) {
2008 wpa_drv_cancel_remain_on_channel(wpa_s);
2009 wpa_s->off_channel_freq = 0;
2010 wpa_s->roc_waiting_drv_freq = 0;
2011 }
2012 wpa_drv_set_ap_wps_ie(wpa_s, NULL, NULL, NULL);
2013 wpa_drv_probe_req_report(wpa_s, 0);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002014 wpas_p2p_listen_work_done(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002015}
2016
2017
2018static int wpas_send_probe_resp(void *ctx, const struct wpabuf *buf)
2019{
2020 struct wpa_supplicant *wpa_s = ctx;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002021 return wpa_drv_send_mlme(wpa_s, wpabuf_head(buf), wpabuf_len(buf), 1);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002022}
2023
2024
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002025/*
2026 * DNS Header section is used only to calculate compression pointers, so the
2027 * contents of this data does not matter, but the length needs to be reserved
2028 * in the virtual packet.
2029 */
2030#define DNS_HEADER_LEN 12
2031
2032/*
2033 * 27-octet in-memory packet from P2P specification containing two implied
2034 * queries for _tcp.lcoal. PTR IN and _udp.local. PTR IN
2035 */
2036#define P2P_SD_IN_MEMORY_LEN 27
2037
2038static int p2p_sd_dns_uncompress_label(char **upos, char *uend, u8 *start,
2039 u8 **spos, const u8 *end)
2040{
2041 while (*spos < end) {
2042 u8 val = ((*spos)[0] & 0xc0) >> 6;
2043 int len;
2044
2045 if (val == 1 || val == 2) {
2046 /* These are reserved values in RFC 1035 */
2047 wpa_printf(MSG_DEBUG, "P2P: Invalid domain name "
2048 "sequence starting with 0x%x", val);
2049 return -1;
2050 }
2051
2052 if (val == 3) {
2053 u16 offset;
2054 u8 *spos_tmp;
2055
2056 /* Offset */
2057 if (*spos + 2 > end) {
2058 wpa_printf(MSG_DEBUG, "P2P: No room for full "
2059 "DNS offset field");
2060 return -1;
2061 }
2062
2063 offset = (((*spos)[0] & 0x3f) << 8) | (*spos)[1];
2064 if (offset >= *spos - start) {
2065 wpa_printf(MSG_DEBUG, "P2P: Invalid DNS "
2066 "pointer offset %u", offset);
2067 return -1;
2068 }
2069
2070 (*spos) += 2;
2071 spos_tmp = start + offset;
2072 return p2p_sd_dns_uncompress_label(upos, uend, start,
2073 &spos_tmp,
2074 *spos - 2);
2075 }
2076
2077 /* Label */
2078 len = (*spos)[0] & 0x3f;
2079 if (len == 0)
2080 return 0;
2081
2082 (*spos)++;
2083 if (*spos + len > end) {
2084 wpa_printf(MSG_DEBUG, "P2P: Invalid domain name "
2085 "sequence - no room for label with length "
2086 "%u", len);
2087 return -1;
2088 }
2089
2090 if (*upos + len + 2 > uend)
2091 return -2;
2092
2093 os_memcpy(*upos, *spos, len);
2094 *spos += len;
2095 *upos += len;
2096 (*upos)[0] = '.';
2097 (*upos)++;
2098 (*upos)[0] = '\0';
2099 }
2100
2101 return 0;
2102}
2103
2104
2105/* Uncompress domain names per RFC 1035 using the P2P SD in-memory packet.
2106 * Returns -1 on parsing error (invalid input sequence), -2 if output buffer is
2107 * not large enough */
2108static int p2p_sd_dns_uncompress(char *buf, size_t buf_len, const u8 *msg,
2109 size_t msg_len, size_t offset)
2110{
2111 /* 27-octet in-memory packet from P2P specification */
2112 const char *prefix = "\x04_tcp\x05local\x00\x00\x0C\x00\x01"
2113 "\x04_udp\xC0\x11\x00\x0C\x00\x01";
2114 u8 *tmp, *end, *spos;
2115 char *upos, *uend;
2116 int ret = 0;
2117
2118 if (buf_len < 2)
2119 return -1;
2120 if (offset > msg_len)
2121 return -1;
2122
2123 tmp = os_malloc(DNS_HEADER_LEN + P2P_SD_IN_MEMORY_LEN + msg_len);
2124 if (tmp == NULL)
2125 return -1;
2126 spos = tmp + DNS_HEADER_LEN + P2P_SD_IN_MEMORY_LEN;
2127 end = spos + msg_len;
2128 spos += offset;
2129
2130 os_memset(tmp, 0, DNS_HEADER_LEN);
2131 os_memcpy(tmp + DNS_HEADER_LEN, prefix, P2P_SD_IN_MEMORY_LEN);
2132 os_memcpy(tmp + DNS_HEADER_LEN + P2P_SD_IN_MEMORY_LEN, msg, msg_len);
2133
2134 upos = buf;
2135 uend = buf + buf_len;
2136
2137 ret = p2p_sd_dns_uncompress_label(&upos, uend, tmp, &spos, end);
2138 if (ret) {
2139 os_free(tmp);
2140 return ret;
2141 }
2142
2143 if (upos == buf) {
2144 upos[0] = '.';
2145 upos[1] = '\0';
2146 } else if (upos[-1] == '.')
2147 upos[-1] = '\0';
2148
2149 os_free(tmp);
2150 return 0;
2151}
2152
2153
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002154static struct p2p_srv_bonjour *
2155wpas_p2p_service_get_bonjour(struct wpa_supplicant *wpa_s,
2156 const struct wpabuf *query)
2157{
2158 struct p2p_srv_bonjour *bsrv;
2159 size_t len;
2160
2161 len = wpabuf_len(query);
2162 dl_list_for_each(bsrv, &wpa_s->global->p2p_srv_bonjour,
2163 struct p2p_srv_bonjour, list) {
2164 if (len == wpabuf_len(bsrv->query) &&
2165 os_memcmp(wpabuf_head(query), wpabuf_head(bsrv->query),
2166 len) == 0)
2167 return bsrv;
2168 }
2169 return NULL;
2170}
2171
2172
2173static struct p2p_srv_upnp *
2174wpas_p2p_service_get_upnp(struct wpa_supplicant *wpa_s, u8 version,
2175 const char *service)
2176{
2177 struct p2p_srv_upnp *usrv;
2178
2179 dl_list_for_each(usrv, &wpa_s->global->p2p_srv_upnp,
2180 struct p2p_srv_upnp, list) {
2181 if (version == usrv->version &&
2182 os_strcmp(service, usrv->service) == 0)
2183 return usrv;
2184 }
2185 return NULL;
2186}
2187
2188
2189static void wpas_sd_add_proto_not_avail(struct wpabuf *resp, u8 srv_proto,
2190 u8 srv_trans_id)
2191{
2192 u8 *len_pos;
2193
2194 if (wpabuf_tailroom(resp) < 5)
2195 return;
2196
2197 /* Length (to be filled) */
2198 len_pos = wpabuf_put(resp, 2);
2199 wpabuf_put_u8(resp, srv_proto);
2200 wpabuf_put_u8(resp, srv_trans_id);
2201 /* Status Code */
2202 wpabuf_put_u8(resp, P2P_SD_PROTO_NOT_AVAILABLE);
2203 /* Response Data: empty */
2204 WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos - 2);
2205}
2206
2207
2208static void wpas_sd_all_bonjour(struct wpa_supplicant *wpa_s,
2209 struct wpabuf *resp, u8 srv_trans_id)
2210{
2211 struct p2p_srv_bonjour *bsrv;
2212 u8 *len_pos;
2213
2214 wpa_printf(MSG_DEBUG, "P2P: SD Request for all Bonjour services");
2215
2216 if (dl_list_empty(&wpa_s->global->p2p_srv_bonjour)) {
2217 wpa_printf(MSG_DEBUG, "P2P: Bonjour protocol not available");
2218 return;
2219 }
2220
2221 dl_list_for_each(bsrv, &wpa_s->global->p2p_srv_bonjour,
2222 struct p2p_srv_bonjour, list) {
2223 if (wpabuf_tailroom(resp) <
2224 5 + wpabuf_len(bsrv->query) + wpabuf_len(bsrv->resp))
2225 return;
2226 /* Length (to be filled) */
2227 len_pos = wpabuf_put(resp, 2);
2228 wpabuf_put_u8(resp, P2P_SERV_BONJOUR);
2229 wpabuf_put_u8(resp, srv_trans_id);
2230 /* Status Code */
2231 wpabuf_put_u8(resp, P2P_SD_SUCCESS);
2232 wpa_hexdump_ascii(MSG_DEBUG, "P2P: Matching Bonjour service",
2233 wpabuf_head(bsrv->resp),
2234 wpabuf_len(bsrv->resp));
2235 /* Response Data */
2236 wpabuf_put_buf(resp, bsrv->query); /* Key */
2237 wpabuf_put_buf(resp, bsrv->resp); /* Value */
2238 WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos -
2239 2);
2240 }
2241}
2242
2243
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002244static int match_bonjour_query(struct p2p_srv_bonjour *bsrv, const u8 *query,
2245 size_t query_len)
2246{
2247 char str_rx[256], str_srv[256];
2248
2249 if (query_len < 3 || wpabuf_len(bsrv->query) < 3)
2250 return 0; /* Too short to include DNS Type and Version */
2251 if (os_memcmp(query + query_len - 3,
2252 wpabuf_head_u8(bsrv->query) + wpabuf_len(bsrv->query) - 3,
2253 3) != 0)
2254 return 0; /* Mismatch in DNS Type or Version */
2255 if (query_len == wpabuf_len(bsrv->query) &&
2256 os_memcmp(query, wpabuf_head(bsrv->query), query_len - 3) == 0)
2257 return 1; /* Binary match */
2258
2259 if (p2p_sd_dns_uncompress(str_rx, sizeof(str_rx), query, query_len - 3,
2260 0))
2261 return 0; /* Failed to uncompress query */
2262 if (p2p_sd_dns_uncompress(str_srv, sizeof(str_srv),
2263 wpabuf_head(bsrv->query),
2264 wpabuf_len(bsrv->query) - 3, 0))
2265 return 0; /* Failed to uncompress service */
2266
2267 return os_strcmp(str_rx, str_srv) == 0;
2268}
2269
2270
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002271static void wpas_sd_req_bonjour(struct wpa_supplicant *wpa_s,
2272 struct wpabuf *resp, u8 srv_trans_id,
2273 const u8 *query, size_t query_len)
2274{
2275 struct p2p_srv_bonjour *bsrv;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002276 u8 *len_pos;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002277 int matches = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002278
2279 wpa_hexdump_ascii(MSG_DEBUG, "P2P: SD Request for Bonjour",
2280 query, query_len);
2281 if (dl_list_empty(&wpa_s->global->p2p_srv_bonjour)) {
2282 wpa_printf(MSG_DEBUG, "P2P: Bonjour protocol not available");
2283 wpas_sd_add_proto_not_avail(resp, P2P_SERV_BONJOUR,
2284 srv_trans_id);
2285 return;
2286 }
2287
2288 if (query_len == 0) {
2289 wpas_sd_all_bonjour(wpa_s, resp, srv_trans_id);
2290 return;
2291 }
2292
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002293 dl_list_for_each(bsrv, &wpa_s->global->p2p_srv_bonjour,
2294 struct p2p_srv_bonjour, list) {
2295 if (!match_bonjour_query(bsrv, query, query_len))
2296 continue;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002297
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002298 if (wpabuf_tailroom(resp) <
2299 5 + query_len + wpabuf_len(bsrv->resp))
2300 return;
2301
2302 matches++;
2303
2304 /* Length (to be filled) */
2305 len_pos = wpabuf_put(resp, 2);
2306 wpabuf_put_u8(resp, P2P_SERV_BONJOUR);
2307 wpabuf_put_u8(resp, srv_trans_id);
2308
2309 /* Status Code */
2310 wpabuf_put_u8(resp, P2P_SD_SUCCESS);
2311 wpa_hexdump_ascii(MSG_DEBUG, "P2P: Matching Bonjour service",
2312 wpabuf_head(bsrv->resp),
2313 wpabuf_len(bsrv->resp));
2314
2315 /* Response Data */
2316 wpabuf_put_data(resp, query, query_len); /* Key */
2317 wpabuf_put_buf(resp, bsrv->resp); /* Value */
2318
2319 WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos - 2);
2320 }
2321
2322 if (matches == 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002323 wpa_printf(MSG_DEBUG, "P2P: Requested Bonjour service not "
2324 "available");
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002325 if (wpabuf_tailroom(resp) < 5)
2326 return;
2327
2328 /* Length (to be filled) */
2329 len_pos = wpabuf_put(resp, 2);
2330 wpabuf_put_u8(resp, P2P_SERV_BONJOUR);
2331 wpabuf_put_u8(resp, srv_trans_id);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002332
2333 /* Status Code */
2334 wpabuf_put_u8(resp, P2P_SD_REQUESTED_INFO_NOT_AVAILABLE);
2335 /* Response Data: empty */
2336 WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos -
2337 2);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002338 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002339}
2340
2341
2342static void wpas_sd_all_upnp(struct wpa_supplicant *wpa_s,
2343 struct wpabuf *resp, u8 srv_trans_id)
2344{
2345 struct p2p_srv_upnp *usrv;
2346 u8 *len_pos;
2347
2348 wpa_printf(MSG_DEBUG, "P2P: SD Request for all UPnP services");
2349
2350 if (dl_list_empty(&wpa_s->global->p2p_srv_upnp)) {
2351 wpa_printf(MSG_DEBUG, "P2P: UPnP protocol not available");
2352 return;
2353 }
2354
2355 dl_list_for_each(usrv, &wpa_s->global->p2p_srv_upnp,
2356 struct p2p_srv_upnp, list) {
2357 if (wpabuf_tailroom(resp) < 5 + 1 + os_strlen(usrv->service))
2358 return;
2359
2360 /* Length (to be filled) */
2361 len_pos = wpabuf_put(resp, 2);
2362 wpabuf_put_u8(resp, P2P_SERV_UPNP);
2363 wpabuf_put_u8(resp, srv_trans_id);
2364
2365 /* Status Code */
2366 wpabuf_put_u8(resp, P2P_SD_SUCCESS);
2367 /* Response Data */
2368 wpabuf_put_u8(resp, usrv->version);
2369 wpa_printf(MSG_DEBUG, "P2P: Matching UPnP Service: %s",
2370 usrv->service);
2371 wpabuf_put_str(resp, usrv->service);
2372 WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos -
2373 2);
2374 }
2375}
2376
2377
2378static void wpas_sd_req_upnp(struct wpa_supplicant *wpa_s,
2379 struct wpabuf *resp, u8 srv_trans_id,
2380 const u8 *query, size_t query_len)
2381{
2382 struct p2p_srv_upnp *usrv;
2383 u8 *len_pos;
2384 u8 version;
2385 char *str;
2386 int count = 0;
2387
2388 wpa_hexdump_ascii(MSG_DEBUG, "P2P: SD Request for UPnP",
2389 query, query_len);
2390
2391 if (dl_list_empty(&wpa_s->global->p2p_srv_upnp)) {
2392 wpa_printf(MSG_DEBUG, "P2P: UPnP protocol not available");
2393 wpas_sd_add_proto_not_avail(resp, P2P_SERV_UPNP,
2394 srv_trans_id);
2395 return;
2396 }
2397
2398 if (query_len == 0) {
2399 wpas_sd_all_upnp(wpa_s, resp, srv_trans_id);
2400 return;
2401 }
2402
2403 if (wpabuf_tailroom(resp) < 5)
2404 return;
2405
2406 /* Length (to be filled) */
2407 len_pos = wpabuf_put(resp, 2);
2408 wpabuf_put_u8(resp, P2P_SERV_UPNP);
2409 wpabuf_put_u8(resp, srv_trans_id);
2410
2411 version = query[0];
2412 str = os_malloc(query_len);
2413 if (str == NULL)
2414 return;
2415 os_memcpy(str, query + 1, query_len - 1);
2416 str[query_len - 1] = '\0';
2417
2418 dl_list_for_each(usrv, &wpa_s->global->p2p_srv_upnp,
2419 struct p2p_srv_upnp, list) {
2420 if (version != usrv->version)
2421 continue;
2422
2423 if (os_strcmp(str, "ssdp:all") != 0 &&
2424 os_strstr(usrv->service, str) == NULL)
2425 continue;
2426
2427 if (wpabuf_tailroom(resp) < 2)
2428 break;
2429 if (count == 0) {
2430 /* Status Code */
2431 wpabuf_put_u8(resp, P2P_SD_SUCCESS);
2432 /* Response Data */
2433 wpabuf_put_u8(resp, version);
2434 } else
2435 wpabuf_put_u8(resp, ',');
2436
2437 count++;
2438
2439 wpa_printf(MSG_DEBUG, "P2P: Matching UPnP Service: %s",
2440 usrv->service);
2441 if (wpabuf_tailroom(resp) < os_strlen(usrv->service))
2442 break;
2443 wpabuf_put_str(resp, usrv->service);
2444 }
2445 os_free(str);
2446
2447 if (count == 0) {
2448 wpa_printf(MSG_DEBUG, "P2P: Requested UPnP service not "
2449 "available");
2450 /* Status Code */
2451 wpabuf_put_u8(resp, P2P_SD_REQUESTED_INFO_NOT_AVAILABLE);
2452 /* Response Data: empty */
2453 }
2454
2455 WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos - 2);
2456}
2457
2458
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002459#ifdef CONFIG_WIFI_DISPLAY
2460static void wpas_sd_req_wfd(struct wpa_supplicant *wpa_s,
2461 struct wpabuf *resp, u8 srv_trans_id,
2462 const u8 *query, size_t query_len)
2463{
2464 const u8 *pos;
2465 u8 role;
2466 u8 *len_pos;
2467
2468 wpa_hexdump(MSG_DEBUG, "P2P: SD Request for WFD", query, query_len);
2469
2470 if (!wpa_s->global->wifi_display) {
2471 wpa_printf(MSG_DEBUG, "P2P: WFD protocol not available");
2472 wpas_sd_add_proto_not_avail(resp, P2P_SERV_WIFI_DISPLAY,
2473 srv_trans_id);
2474 return;
2475 }
2476
2477 if (query_len < 1) {
2478 wpa_printf(MSG_DEBUG, "P2P: Missing WFD Requested Device "
2479 "Role");
2480 return;
2481 }
2482
2483 if (wpabuf_tailroom(resp) < 5)
2484 return;
2485
2486 pos = query;
2487 role = *pos++;
2488 wpa_printf(MSG_DEBUG, "P2P: WSD for device role 0x%x", role);
2489
2490 /* TODO: role specific handling */
2491
2492 /* Length (to be filled) */
2493 len_pos = wpabuf_put(resp, 2);
2494 wpabuf_put_u8(resp, P2P_SERV_WIFI_DISPLAY);
2495 wpabuf_put_u8(resp, srv_trans_id);
2496 wpabuf_put_u8(resp, P2P_SD_SUCCESS); /* Status Code */
2497
2498 while (pos < query + query_len) {
2499 if (*pos < MAX_WFD_SUBELEMS &&
2500 wpa_s->global->wfd_subelem[*pos] &&
2501 wpabuf_tailroom(resp) >=
2502 wpabuf_len(wpa_s->global->wfd_subelem[*pos])) {
2503 wpa_printf(MSG_DEBUG, "P2P: Add WSD response "
2504 "subelement %u", *pos);
2505 wpabuf_put_buf(resp, wpa_s->global->wfd_subelem[*pos]);
2506 }
2507 pos++;
2508 }
2509
2510 WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos - 2);
2511}
2512#endif /* CONFIG_WIFI_DISPLAY */
2513
2514
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002515static void wpas_sd_request(void *ctx, int freq, const u8 *sa, u8 dialog_token,
2516 u16 update_indic, const u8 *tlvs, size_t tlvs_len)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002517{
2518 struct wpa_supplicant *wpa_s = ctx;
2519 const u8 *pos = tlvs;
2520 const u8 *end = tlvs + tlvs_len;
2521 const u8 *tlv_end;
2522 u16 slen;
2523 struct wpabuf *resp;
2524 u8 srv_proto, srv_trans_id;
2525 size_t buf_len;
2526 char *buf;
2527
2528 wpa_hexdump(MSG_MSGDUMP, "P2P: Service Discovery Request TLVs",
2529 tlvs, tlvs_len);
2530 buf_len = 2 * tlvs_len + 1;
2531 buf = os_malloc(buf_len);
2532 if (buf) {
2533 wpa_snprintf_hex(buf, buf_len, tlvs, tlvs_len);
2534 wpa_msg_ctrl(wpa_s, MSG_INFO, P2P_EVENT_SERV_DISC_REQ "%d "
2535 MACSTR " %u %u %s",
2536 freq, MAC2STR(sa), dialog_token, update_indic,
2537 buf);
2538 os_free(buf);
2539 }
2540
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002541 if (wpa_s->p2p_sd_over_ctrl_iface) {
2542 wpas_notify_p2p_sd_request(wpa_s, freq, sa, dialog_token,
2543 update_indic, tlvs, tlvs_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002544 return; /* to be processed by an external program */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002545 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002546
2547 resp = wpabuf_alloc(10000);
2548 if (resp == NULL)
2549 return;
2550
2551 while (pos + 1 < end) {
2552 wpa_printf(MSG_DEBUG, "P2P: Service Request TLV");
2553 slen = WPA_GET_LE16(pos);
2554 pos += 2;
2555 if (pos + slen > end || slen < 2) {
2556 wpa_printf(MSG_DEBUG, "P2P: Unexpected Query Data "
2557 "length");
2558 wpabuf_free(resp);
2559 return;
2560 }
2561 tlv_end = pos + slen;
2562
2563 srv_proto = *pos++;
2564 wpa_printf(MSG_DEBUG, "P2P: Service Protocol Type %u",
2565 srv_proto);
2566 srv_trans_id = *pos++;
2567 wpa_printf(MSG_DEBUG, "P2P: Service Transaction ID %u",
2568 srv_trans_id);
2569
2570 wpa_hexdump(MSG_MSGDUMP, "P2P: Query Data",
2571 pos, tlv_end - pos);
2572
2573
2574 if (wpa_s->force_long_sd) {
2575 wpa_printf(MSG_DEBUG, "P2P: SD test - force long "
2576 "response");
2577 wpas_sd_all_bonjour(wpa_s, resp, srv_trans_id);
2578 wpas_sd_all_upnp(wpa_s, resp, srv_trans_id);
2579 goto done;
2580 }
2581
2582 switch (srv_proto) {
2583 case P2P_SERV_ALL_SERVICES:
2584 wpa_printf(MSG_DEBUG, "P2P: Service Discovery Request "
2585 "for all services");
2586 if (dl_list_empty(&wpa_s->global->p2p_srv_upnp) &&
2587 dl_list_empty(&wpa_s->global->p2p_srv_bonjour)) {
2588 wpa_printf(MSG_DEBUG, "P2P: No service "
2589 "discovery protocols available");
2590 wpas_sd_add_proto_not_avail(
2591 resp, P2P_SERV_ALL_SERVICES,
2592 srv_trans_id);
2593 break;
2594 }
2595 wpas_sd_all_bonjour(wpa_s, resp, srv_trans_id);
2596 wpas_sd_all_upnp(wpa_s, resp, srv_trans_id);
2597 break;
2598 case P2P_SERV_BONJOUR:
2599 wpas_sd_req_bonjour(wpa_s, resp, srv_trans_id,
2600 pos, tlv_end - pos);
2601 break;
2602 case P2P_SERV_UPNP:
2603 wpas_sd_req_upnp(wpa_s, resp, srv_trans_id,
2604 pos, tlv_end - pos);
2605 break;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002606#ifdef CONFIG_WIFI_DISPLAY
2607 case P2P_SERV_WIFI_DISPLAY:
2608 wpas_sd_req_wfd(wpa_s, resp, srv_trans_id,
2609 pos, tlv_end - pos);
2610 break;
2611#endif /* CONFIG_WIFI_DISPLAY */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002612 default:
2613 wpa_printf(MSG_DEBUG, "P2P: Unavailable service "
2614 "protocol %u", srv_proto);
2615 wpas_sd_add_proto_not_avail(resp, srv_proto,
2616 srv_trans_id);
2617 break;
2618 }
2619
2620 pos = tlv_end;
2621 }
2622
2623done:
2624 wpas_notify_p2p_sd_request(wpa_s, freq, sa, dialog_token,
2625 update_indic, tlvs, tlvs_len);
2626
2627 wpas_p2p_sd_response(wpa_s, freq, sa, dialog_token, resp);
2628
2629 wpabuf_free(resp);
2630}
2631
2632
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002633static void wpas_sd_response(void *ctx, const u8 *sa, u16 update_indic,
2634 const u8 *tlvs, size_t tlvs_len)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002635{
2636 struct wpa_supplicant *wpa_s = ctx;
2637 const u8 *pos = tlvs;
2638 const u8 *end = tlvs + tlvs_len;
2639 const u8 *tlv_end;
2640 u16 slen;
2641 size_t buf_len;
2642 char *buf;
2643
2644 wpa_hexdump(MSG_MSGDUMP, "P2P: Service Discovery Response TLVs",
2645 tlvs, tlvs_len);
2646 if (tlvs_len > 1500) {
2647 /* TODO: better way for handling this */
2648 wpa_msg_ctrl(wpa_s, MSG_INFO,
2649 P2P_EVENT_SERV_DISC_RESP MACSTR
2650 " %u <long response: %u bytes>",
2651 MAC2STR(sa), update_indic,
2652 (unsigned int) tlvs_len);
2653 } else {
2654 buf_len = 2 * tlvs_len + 1;
2655 buf = os_malloc(buf_len);
2656 if (buf) {
2657 wpa_snprintf_hex(buf, buf_len, tlvs, tlvs_len);
2658 wpa_msg_ctrl(wpa_s, MSG_INFO,
2659 P2P_EVENT_SERV_DISC_RESP MACSTR " %u %s",
2660 MAC2STR(sa), update_indic, buf);
2661 os_free(buf);
2662 }
2663 }
2664
2665 while (pos < end) {
2666 u8 srv_proto, srv_trans_id, status;
2667
2668 wpa_printf(MSG_DEBUG, "P2P: Service Response TLV");
2669 slen = WPA_GET_LE16(pos);
2670 pos += 2;
2671 if (pos + slen > end || slen < 3) {
2672 wpa_printf(MSG_DEBUG, "P2P: Unexpected Response Data "
2673 "length");
2674 return;
2675 }
2676 tlv_end = pos + slen;
2677
2678 srv_proto = *pos++;
2679 wpa_printf(MSG_DEBUG, "P2P: Service Protocol Type %u",
2680 srv_proto);
2681 srv_trans_id = *pos++;
2682 wpa_printf(MSG_DEBUG, "P2P: Service Transaction ID %u",
2683 srv_trans_id);
2684 status = *pos++;
2685 wpa_printf(MSG_DEBUG, "P2P: Status Code ID %u",
2686 status);
2687
2688 wpa_hexdump(MSG_MSGDUMP, "P2P: Response Data",
2689 pos, tlv_end - pos);
2690
2691 pos = tlv_end;
2692 }
2693
2694 wpas_notify_p2p_sd_response(wpa_s, sa, update_indic, tlvs, tlvs_len);
2695}
2696
2697
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002698u64 wpas_p2p_sd_request(struct wpa_supplicant *wpa_s, const u8 *dst,
2699 const struct wpabuf *tlvs)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002700{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002701 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002702 return 0;
2703 return (uintptr_t) p2p_sd_request(wpa_s->global->p2p, dst, tlvs);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002704}
2705
2706
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002707u64 wpas_p2p_sd_request_upnp(struct wpa_supplicant *wpa_s, const u8 *dst,
2708 u8 version, const char *query)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002709{
2710 struct wpabuf *tlvs;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002711 u64 ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002712
2713 tlvs = wpabuf_alloc(2 + 1 + 1 + 1 + os_strlen(query));
2714 if (tlvs == NULL)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002715 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002716 wpabuf_put_le16(tlvs, 1 + 1 + 1 + os_strlen(query));
2717 wpabuf_put_u8(tlvs, P2P_SERV_UPNP); /* Service Protocol Type */
2718 wpabuf_put_u8(tlvs, 1); /* Service Transaction ID */
2719 wpabuf_put_u8(tlvs, version);
2720 wpabuf_put_str(tlvs, query);
2721 ret = wpas_p2p_sd_request(wpa_s, dst, tlvs);
2722 wpabuf_free(tlvs);
2723 return ret;
2724}
2725
2726
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002727#ifdef CONFIG_WIFI_DISPLAY
2728
2729static u64 wpas_p2p_sd_request_wfd(struct wpa_supplicant *wpa_s, const u8 *dst,
2730 const struct wpabuf *tlvs)
2731{
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002732 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
2733 return 0;
2734 return (uintptr_t) p2p_sd_request_wfd(wpa_s->global->p2p, dst, tlvs);
2735}
2736
2737
2738#define MAX_WFD_SD_SUBELEMS 20
2739
2740static void wfd_add_sd_req_role(struct wpabuf *tlvs, u8 id, u8 role,
2741 const char *subelems)
2742{
2743 u8 *len;
2744 const char *pos;
2745 int val;
2746 int count = 0;
2747
2748 len = wpabuf_put(tlvs, 2);
2749 wpabuf_put_u8(tlvs, P2P_SERV_WIFI_DISPLAY); /* Service Protocol Type */
2750 wpabuf_put_u8(tlvs, id); /* Service Transaction ID */
2751
2752 wpabuf_put_u8(tlvs, role);
2753
2754 pos = subelems;
2755 while (*pos) {
2756 val = atoi(pos);
2757 if (val >= 0 && val < 256) {
2758 wpabuf_put_u8(tlvs, val);
2759 count++;
2760 if (count == MAX_WFD_SD_SUBELEMS)
2761 break;
2762 }
2763 pos = os_strchr(pos + 1, ',');
2764 if (pos == NULL)
2765 break;
2766 pos++;
2767 }
2768
2769 WPA_PUT_LE16(len, (u8 *) wpabuf_put(tlvs, 0) - len - 2);
2770}
2771
2772
2773u64 wpas_p2p_sd_request_wifi_display(struct wpa_supplicant *wpa_s,
2774 const u8 *dst, const char *role)
2775{
2776 struct wpabuf *tlvs;
2777 u64 ret;
2778 const char *subelems;
2779 u8 id = 1;
2780
2781 subelems = os_strchr(role, ' ');
2782 if (subelems == NULL)
2783 return 0;
2784 subelems++;
2785
2786 tlvs = wpabuf_alloc(4 * (2 + 1 + 1 + 1 + MAX_WFD_SD_SUBELEMS));
2787 if (tlvs == NULL)
2788 return 0;
2789
2790 if (os_strstr(role, "[source]"))
2791 wfd_add_sd_req_role(tlvs, id++, 0x00, subelems);
2792 if (os_strstr(role, "[pri-sink]"))
2793 wfd_add_sd_req_role(tlvs, id++, 0x01, subelems);
2794 if (os_strstr(role, "[sec-sink]"))
2795 wfd_add_sd_req_role(tlvs, id++, 0x02, subelems);
2796 if (os_strstr(role, "[source+sink]"))
2797 wfd_add_sd_req_role(tlvs, id++, 0x03, subelems);
2798
2799 ret = wpas_p2p_sd_request_wfd(wpa_s, dst, tlvs);
2800 wpabuf_free(tlvs);
2801 return ret;
2802}
2803
2804#endif /* CONFIG_WIFI_DISPLAY */
2805
2806
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002807int wpas_p2p_sd_cancel_request(struct wpa_supplicant *wpa_s, u64 req)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002808{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002809 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
2810 return -1;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002811 return p2p_sd_cancel_request(wpa_s->global->p2p,
2812 (void *) (uintptr_t) req);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002813}
2814
2815
2816void wpas_p2p_sd_response(struct wpa_supplicant *wpa_s, int freq,
2817 const u8 *dst, u8 dialog_token,
2818 const struct wpabuf *resp_tlvs)
2819{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002820 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
2821 return;
2822 p2p_sd_response(wpa_s->global->p2p, freq, dst, dialog_token,
2823 resp_tlvs);
2824}
2825
Dmitry Shmidteaf261d2013-08-14 15:30:08 -07002826
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002827void wpas_p2p_sd_service_update(struct wpa_supplicant *wpa_s)
2828{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002829 if (wpa_s->global->p2p)
2830 p2p_sd_service_update(wpa_s->global->p2p);
2831}
2832
2833
2834static void wpas_p2p_srv_bonjour_free(struct p2p_srv_bonjour *bsrv)
2835{
2836 dl_list_del(&bsrv->list);
2837 wpabuf_free(bsrv->query);
2838 wpabuf_free(bsrv->resp);
2839 os_free(bsrv);
2840}
2841
2842
2843static void wpas_p2p_srv_upnp_free(struct p2p_srv_upnp *usrv)
2844{
2845 dl_list_del(&usrv->list);
2846 os_free(usrv->service);
2847 os_free(usrv);
2848}
2849
2850
2851void wpas_p2p_service_flush(struct wpa_supplicant *wpa_s)
2852{
2853 struct p2p_srv_bonjour *bsrv, *bn;
2854 struct p2p_srv_upnp *usrv, *un;
2855
2856 dl_list_for_each_safe(bsrv, bn, &wpa_s->global->p2p_srv_bonjour,
2857 struct p2p_srv_bonjour, list)
2858 wpas_p2p_srv_bonjour_free(bsrv);
2859
2860 dl_list_for_each_safe(usrv, un, &wpa_s->global->p2p_srv_upnp,
2861 struct p2p_srv_upnp, list)
2862 wpas_p2p_srv_upnp_free(usrv);
2863
2864 wpas_p2p_sd_service_update(wpa_s);
2865}
2866
2867
2868int wpas_p2p_service_add_bonjour(struct wpa_supplicant *wpa_s,
2869 struct wpabuf *query, struct wpabuf *resp)
2870{
2871 struct p2p_srv_bonjour *bsrv;
2872
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002873 bsrv = os_zalloc(sizeof(*bsrv));
2874 if (bsrv == NULL)
2875 return -1;
2876 bsrv->query = query;
2877 bsrv->resp = resp;
2878 dl_list_add(&wpa_s->global->p2p_srv_bonjour, &bsrv->list);
2879
2880 wpas_p2p_sd_service_update(wpa_s);
2881 return 0;
2882}
2883
2884
2885int wpas_p2p_service_del_bonjour(struct wpa_supplicant *wpa_s,
2886 const struct wpabuf *query)
2887{
2888 struct p2p_srv_bonjour *bsrv;
2889
2890 bsrv = wpas_p2p_service_get_bonjour(wpa_s, query);
2891 if (bsrv == NULL)
2892 return -1;
2893 wpas_p2p_srv_bonjour_free(bsrv);
2894 wpas_p2p_sd_service_update(wpa_s);
2895 return 0;
2896}
2897
2898
2899int wpas_p2p_service_add_upnp(struct wpa_supplicant *wpa_s, u8 version,
2900 const char *service)
2901{
2902 struct p2p_srv_upnp *usrv;
2903
2904 if (wpas_p2p_service_get_upnp(wpa_s, version, service))
2905 return 0; /* Already listed */
2906 usrv = os_zalloc(sizeof(*usrv));
2907 if (usrv == NULL)
2908 return -1;
2909 usrv->version = version;
2910 usrv->service = os_strdup(service);
2911 if (usrv->service == NULL) {
2912 os_free(usrv);
2913 return -1;
2914 }
2915 dl_list_add(&wpa_s->global->p2p_srv_upnp, &usrv->list);
2916
2917 wpas_p2p_sd_service_update(wpa_s);
2918 return 0;
2919}
2920
2921
2922int wpas_p2p_service_del_upnp(struct wpa_supplicant *wpa_s, u8 version,
2923 const char *service)
2924{
2925 struct p2p_srv_upnp *usrv;
2926
2927 usrv = wpas_p2p_service_get_upnp(wpa_s, version, service);
2928 if (usrv == NULL)
2929 return -1;
2930 wpas_p2p_srv_upnp_free(usrv);
2931 wpas_p2p_sd_service_update(wpa_s);
2932 return 0;
2933}
2934
2935
2936static void wpas_prov_disc_local_display(struct wpa_supplicant *wpa_s,
2937 const u8 *peer, const char *params,
2938 unsigned int generated_pin)
2939{
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07002940 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_SHOW_PIN MACSTR
2941 " %08d%s", MAC2STR(peer), generated_pin, params);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002942}
2943
2944
2945static void wpas_prov_disc_local_keypad(struct wpa_supplicant *wpa_s,
2946 const u8 *peer, const char *params)
2947{
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07002948 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_ENTER_PIN MACSTR
2949 "%s", MAC2STR(peer), params);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002950}
2951
2952
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002953static void wpas_prov_disc_req(void *ctx, const u8 *peer, u16 config_methods,
2954 const u8 *dev_addr, const u8 *pri_dev_type,
2955 const char *dev_name, u16 supp_config_methods,
2956 u8 dev_capab, u8 group_capab, const u8 *group_id,
2957 size_t group_id_len)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002958{
2959 struct wpa_supplicant *wpa_s = ctx;
2960 char devtype[WPS_DEV_TYPE_BUFSIZE];
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002961 char params[300];
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002962 u8 empty_dev_type[8];
2963 unsigned int generated_pin = 0;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002964 struct wpa_supplicant *group = NULL;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002965 int res;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002966
2967 if (group_id) {
2968 for (group = wpa_s->global->ifaces; group; group = group->next)
2969 {
2970 struct wpa_ssid *s = group->current_ssid;
2971 if (s != NULL &&
2972 s->mode == WPAS_MODE_P2P_GO &&
2973 group_id_len - ETH_ALEN == s->ssid_len &&
2974 os_memcmp(group_id + ETH_ALEN, s->ssid,
2975 s->ssid_len) == 0)
2976 break;
2977 }
2978 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002979
2980 if (pri_dev_type == NULL) {
2981 os_memset(empty_dev_type, 0, sizeof(empty_dev_type));
2982 pri_dev_type = empty_dev_type;
2983 }
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002984 res = os_snprintf(params, sizeof(params), " p2p_dev_addr=" MACSTR
2985 " pri_dev_type=%s name='%s' config_methods=0x%x "
2986 "dev_capab=0x%x group_capab=0x%x%s%s",
2987 MAC2STR(dev_addr),
2988 wps_dev_type_bin2str(pri_dev_type, devtype,
2989 sizeof(devtype)),
2990 dev_name, supp_config_methods, dev_capab, group_capab,
2991 group ? " group=" : "",
2992 group ? group->ifname : "");
2993 if (os_snprintf_error(sizeof(params), res))
2994 wpa_printf(MSG_DEBUG, "P2P: PD Request event truncated");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002995 params[sizeof(params) - 1] = '\0';
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08002996
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002997 if (config_methods & WPS_CONFIG_DISPLAY) {
2998 generated_pin = wps_generate_pin();
2999 wpas_prov_disc_local_display(wpa_s, peer, params,
3000 generated_pin);
3001 } else if (config_methods & WPS_CONFIG_KEYPAD)
3002 wpas_prov_disc_local_keypad(wpa_s, peer, params);
3003 else if (config_methods & WPS_CONFIG_PUSHBUTTON)
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07003004 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_PBC_REQ
3005 MACSTR "%s", MAC2STR(peer), params);
Jouni Malinen75ecf522011-06-27 15:19:46 -07003006
3007 wpas_notify_p2p_provision_discovery(wpa_s, peer, 1 /* request */,
3008 P2P_PROV_DISC_SUCCESS,
3009 config_methods, generated_pin);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003010}
3011
3012
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08003013static void wpas_prov_disc_resp(void *ctx, const u8 *peer, u16 config_methods)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003014{
3015 struct wpa_supplicant *wpa_s = ctx;
3016 unsigned int generated_pin = 0;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003017 char params[20];
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003018
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08003019 if (wpa_s->pending_pd_before_join &&
3020 (os_memcmp(peer, wpa_s->pending_join_dev_addr, ETH_ALEN) == 0 ||
3021 os_memcmp(peer, wpa_s->pending_join_iface_addr, ETH_ALEN) == 0)) {
3022 wpa_s->pending_pd_before_join = 0;
3023 wpa_printf(MSG_DEBUG, "P2P: Starting pending "
3024 "join-existing-group operation");
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08003025 wpas_p2p_join_start(wpa_s, 0, NULL, 0);
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08003026 return;
3027 }
3028
Dmitry Shmidt04949592012-07-19 12:16:46 -07003029 if (wpa_s->pending_pd_use == AUTO_PD_JOIN ||
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003030 wpa_s->pending_pd_use == AUTO_PD_GO_NEG) {
3031 int res;
3032
3033 res = os_snprintf(params, sizeof(params), " peer_go=%d",
3034 wpa_s->pending_pd_use == AUTO_PD_JOIN);
3035 if (os_snprintf_error(sizeof(params), res))
3036 params[sizeof(params) - 1] = '\0';
3037 } else
Dmitry Shmidt04949592012-07-19 12:16:46 -07003038 params[0] = '\0';
3039
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003040 if (config_methods & WPS_CONFIG_DISPLAY)
Dmitry Shmidt04949592012-07-19 12:16:46 -07003041 wpas_prov_disc_local_keypad(wpa_s, peer, params);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003042 else if (config_methods & WPS_CONFIG_KEYPAD) {
3043 generated_pin = wps_generate_pin();
Dmitry Shmidt04949592012-07-19 12:16:46 -07003044 wpas_prov_disc_local_display(wpa_s, peer, params,
3045 generated_pin);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003046 } else if (config_methods & WPS_CONFIG_PUSHBUTTON)
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07003047 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_PBC_RESP
3048 MACSTR "%s", MAC2STR(peer), params);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003049
Jouni Malinen75ecf522011-06-27 15:19:46 -07003050 wpas_notify_p2p_provision_discovery(wpa_s, peer, 0 /* response */,
3051 P2P_PROV_DISC_SUCCESS,
3052 config_methods, generated_pin);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003053}
3054
3055
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003056static void wpas_prov_disc_fail(void *ctx, const u8 *peer,
3057 enum p2p_prov_disc_status status)
Jouni Malinen75ecf522011-06-27 15:19:46 -07003058{
3059 struct wpa_supplicant *wpa_s = ctx;
3060
Dmitry Shmidt04949592012-07-19 12:16:46 -07003061 if (wpa_s->p2p_fallback_to_go_neg) {
3062 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: PD for p2p_connect-auto "
3063 "failed - fall back to GO Negotiation");
3064 wpas_p2p_fallback_to_go_neg(wpa_s, 0);
3065 return;
3066 }
3067
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08003068 if (status == P2P_PROV_DISC_TIMEOUT_JOIN) {
Dmitry Shmidt2b380482012-09-13 10:52:13 -07003069 wpa_s->pending_pd_before_join = 0;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08003070 wpa_printf(MSG_DEBUG, "P2P: Starting pending "
3071 "join-existing-group operation (no ACK for PD "
3072 "Req attempts)");
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08003073 wpas_p2p_join_start(wpa_s, 0, NULL, 0);
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08003074 return;
Dmitry Shmidt2b380482012-09-13 10:52:13 -07003075 }
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08003076
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07003077 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_FAILURE
3078 " p2p_dev_addr=" MACSTR " status=%d",
3079 MAC2STR(peer), status);
Dmitry Shmidt04949592012-07-19 12:16:46 -07003080
Jouni Malinen75ecf522011-06-27 15:19:46 -07003081 wpas_notify_p2p_provision_discovery(wpa_s, peer, 0 /* response */,
3082 status, 0, 0);
3083}
3084
3085
Dmitry Shmidtf7e0a992013-05-23 11:03:10 -07003086static int freq_included(const struct p2p_channels *channels, unsigned int freq)
3087{
3088 if (channels == NULL)
3089 return 1; /* Assume no restrictions */
3090 return p2p_channels_includes_freq(channels, freq);
3091
3092}
3093
3094
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07003095/**
3096 * Pick the best frequency to use from all the currently used frequencies.
3097 */
3098static int wpas_p2p_pick_best_used_freq(struct wpa_supplicant *wpa_s,
3099 struct wpa_used_freq_data *freqs,
3100 unsigned int num)
3101{
3102 unsigned int i, c;
3103
3104 /* find a candidate freq that is supported by P2P */
3105 for (c = 0; c < num; c++)
3106 if (p2p_supported_freq(wpa_s->global->p2p, freqs[c].freq))
3107 break;
3108
3109 if (c == num)
3110 return 0;
3111
3112 /* once we have a candidate, try to find a 'better' one */
3113 for (i = c + 1; i < num; i++) {
3114 if (!p2p_supported_freq(wpa_s->global->p2p, freqs[i].freq))
3115 continue;
3116
3117 /*
3118 * 1. Infrastructure station interfaces have higher preference.
3119 * 2. P2P Clients have higher preference.
3120 * 3. All others.
3121 */
3122 if (freqs[i].flags & WPA_FREQ_USED_BY_INFRA_STATION) {
3123 c = i;
3124 break;
3125 }
3126
3127 if ((freqs[i].flags & WPA_FREQ_USED_BY_P2P_CLIENT))
3128 c = i;
3129 }
3130 return freqs[c].freq;
3131}
3132
3133
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003134static u8 wpas_invitation_process(void *ctx, const u8 *sa, const u8 *bssid,
3135 const u8 *go_dev_addr, const u8 *ssid,
3136 size_t ssid_len, int *go, u8 *group_bssid,
Dmitry Shmidtf7e0a992013-05-23 11:03:10 -07003137 int *force_freq, int persistent_group,
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08003138 const struct p2p_channels *channels,
3139 int dev_pw_id)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003140{
3141 struct wpa_supplicant *wpa_s = ctx;
3142 struct wpa_ssid *s;
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07003143 struct wpa_used_freq_data *freqs;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003144 struct wpa_supplicant *grp;
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07003145 int best_freq;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003146
3147 if (!persistent_group) {
3148 wpa_printf(MSG_DEBUG, "P2P: Invitation from " MACSTR
Dmitry Shmidt344abd32014-01-14 13:17:00 -08003149 " to join an active group (SSID: %s)",
3150 MAC2STR(sa), wpa_ssid_txt(ssid, ssid_len));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003151 if (!is_zero_ether_addr(wpa_s->p2p_auth_invite) &&
3152 (os_memcmp(go_dev_addr, wpa_s->p2p_auth_invite, ETH_ALEN)
3153 == 0 ||
3154 os_memcmp(sa, wpa_s->p2p_auth_invite, ETH_ALEN) == 0)) {
3155 wpa_printf(MSG_DEBUG, "P2P: Accept previously "
3156 "authorized invitation");
3157 goto accept_inv;
3158 }
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08003159
3160#ifdef CONFIG_WPS_NFC
3161 if (dev_pw_id >= 0 && wpa_s->parent->p2p_nfc_tag_enabled &&
3162 dev_pw_id == wpa_s->parent->p2p_oob_dev_pw_id) {
3163 wpa_printf(MSG_DEBUG, "P2P: Accept invitation based on local enabled NFC Tag");
3164 wpa_s->parent->p2p_wps_method = WPS_NFC;
3165 wpa_s->parent->pending_join_wps_method = WPS_NFC;
3166 os_memcpy(wpa_s->parent->pending_join_dev_addr,
3167 go_dev_addr, ETH_ALEN);
3168 os_memcpy(wpa_s->parent->pending_join_iface_addr,
3169 bssid, ETH_ALEN);
3170 goto accept_inv;
3171 }
3172#endif /* CONFIG_WPS_NFC */
3173
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003174 /*
3175 * Do not accept the invitation automatically; notify user and
3176 * request approval.
3177 */
3178 return P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE;
3179 }
3180
3181 grp = wpas_get_p2p_group(wpa_s, ssid, ssid_len, go);
3182 if (grp) {
3183 wpa_printf(MSG_DEBUG, "P2P: Accept invitation to already "
3184 "running persistent group");
3185 if (*go)
3186 os_memcpy(group_bssid, grp->own_addr, ETH_ALEN);
3187 goto accept_inv;
3188 }
3189
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08003190 if (!is_zero_ether_addr(wpa_s->p2p_auth_invite) &&
3191 os_memcmp(sa, wpa_s->p2p_auth_invite, ETH_ALEN) == 0) {
3192 wpa_printf(MSG_DEBUG, "P2P: Accept previously initiated "
3193 "invitation to re-invoke a persistent group");
Dmitry Shmidt76cd2cc2014-05-27 12:56:04 -07003194 os_memset(wpa_s->p2p_auth_invite, 0, ETH_ALEN);
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08003195 } else if (!wpa_s->conf->persistent_reconnect)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003196 return P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE;
3197
3198 for (s = wpa_s->conf->ssid; s; s = s->next) {
3199 if (s->disabled == 2 &&
3200 os_memcmp(s->bssid, go_dev_addr, ETH_ALEN) == 0 &&
3201 s->ssid_len == ssid_len &&
3202 os_memcmp(ssid, s->ssid, ssid_len) == 0)
3203 break;
3204 }
3205
3206 if (!s) {
3207 wpa_printf(MSG_DEBUG, "P2P: Invitation from " MACSTR
3208 " requested reinvocation of an unknown group",
3209 MAC2STR(sa));
3210 return P2P_SC_FAIL_UNKNOWN_GROUP;
3211 }
3212
3213 if (s->mode == WPAS_MODE_P2P_GO && !wpas_p2p_create_iface(wpa_s)) {
3214 *go = 1;
3215 if (wpa_s->wpa_state >= WPA_AUTHENTICATING) {
3216 wpa_printf(MSG_DEBUG, "P2P: The only available "
3217 "interface is already in use - reject "
3218 "invitation");
3219 return P2P_SC_FAIL_UNABLE_TO_ACCOMMODATE;
3220 }
3221 os_memcpy(group_bssid, wpa_s->own_addr, ETH_ALEN);
3222 } else if (s->mode == WPAS_MODE_P2P_GO) {
3223 *go = 1;
3224 if (wpas_p2p_add_group_interface(wpa_s, WPA_IF_P2P_GO) < 0)
3225 {
3226 wpa_printf(MSG_ERROR, "P2P: Failed to allocate a new "
3227 "interface address for the group");
3228 return P2P_SC_FAIL_UNABLE_TO_ACCOMMODATE;
3229 }
3230 os_memcpy(group_bssid, wpa_s->pending_interface_addr,
3231 ETH_ALEN);
3232 }
3233
3234accept_inv:
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003235 wpas_p2p_set_own_freq_preference(wpa_s, 0);
3236
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07003237 best_freq = 0;
3238 freqs = os_calloc(wpa_s->num_multichan_concurrent,
3239 sizeof(struct wpa_used_freq_data));
3240 if (freqs) {
3241 int num_channels = wpa_s->num_multichan_concurrent;
3242 int num = wpas_p2p_valid_oper_freqs(wpa_s, freqs, num_channels);
3243 best_freq = wpas_p2p_pick_best_used_freq(wpa_s, freqs, num);
3244 os_free(freqs);
3245 }
3246
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07003247 /* Get one of the frequencies currently in use */
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07003248 if (best_freq > 0) {
Dmitry Shmidta0d265f2013-11-19 13:13:41 -08003249 wpa_printf(MSG_DEBUG, "P2P: Trying to prefer a channel already used by one of the interfaces");
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07003250 wpas_p2p_set_own_freq_preference(wpa_s, best_freq);
Dmitry Shmidta0d265f2013-11-19 13:13:41 -08003251
3252 if (wpa_s->num_multichan_concurrent < 2 ||
3253 wpas_p2p_num_unused_channels(wpa_s) < 1) {
3254 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 -07003255 *force_freq = best_freq;
Dmitry Shmidta0d265f2013-11-19 13:13:41 -08003256 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003257 }
3258
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07003259 if (*force_freq > 0 && wpa_s->num_multichan_concurrent > 1 &&
3260 wpas_p2p_num_unused_channels(wpa_s) > 0) {
Dmitry Shmidtf7e0a992013-05-23 11:03:10 -07003261 if (*go == 0) {
3262 /* We are the client */
3263 wpa_printf(MSG_DEBUG, "P2P: Peer was found to be "
3264 "running a GO but we are capable of MCC, "
3265 "figure out the best channel to use");
3266 *force_freq = 0;
3267 } else if (!freq_included(channels, *force_freq)) {
3268 /* We are the GO, and *force_freq is not in the
3269 * intersection */
3270 wpa_printf(MSG_DEBUG, "P2P: Forced GO freq %d MHz not "
3271 "in intersection but we are capable of MCC, "
3272 "figure out the best channel to use",
3273 *force_freq);
3274 *force_freq = 0;
3275 }
3276 }
3277
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003278 return P2P_SC_SUCCESS;
3279}
3280
3281
3282static void wpas_invitation_received(void *ctx, const u8 *sa, const u8 *bssid,
3283 const u8 *ssid, size_t ssid_len,
3284 const u8 *go_dev_addr, u8 status,
3285 int op_freq)
3286{
3287 struct wpa_supplicant *wpa_s = ctx;
3288 struct wpa_ssid *s;
3289
3290 for (s = wpa_s->conf->ssid; s; s = s->next) {
3291 if (s->disabled == 2 &&
3292 s->ssid_len == ssid_len &&
3293 os_memcmp(ssid, s->ssid, ssid_len) == 0)
3294 break;
3295 }
3296
3297 if (status == P2P_SC_SUCCESS) {
3298 wpa_printf(MSG_DEBUG, "P2P: Invitation from peer " MACSTR
Dmitry Shmidt344abd32014-01-14 13:17:00 -08003299 " was accepted; op_freq=%d MHz, SSID=%s",
3300 MAC2STR(sa), op_freq, wpa_ssid_txt(ssid, ssid_len));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003301 if (s) {
Dmitry Shmidt91c40cd2012-09-25 14:23:53 -07003302 int go = s->mode == WPAS_MODE_P2P_GO;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003303 wpas_p2p_group_add_persistent(
Dmitry Shmidt15907092014-03-25 10:42:57 -07003304 wpa_s, s, go, 0, op_freq, 0, 0, NULL,
Dmitry Shmidt56052862013-10-04 10:23:25 -07003305 go ? P2P_MAX_INITIAL_CONN_WAIT_GO_REINVOKE : 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003306 } else if (bssid) {
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08003307 wpa_s->user_initiated_pd = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003308 wpas_p2p_join(wpa_s, bssid, go_dev_addr,
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08003309 wpa_s->p2p_wps_method, 0, op_freq,
Dmitry Shmidt344abd32014-01-14 13:17:00 -08003310 ssid, ssid_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003311 }
3312 return;
3313 }
3314
3315 if (status != P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE) {
3316 wpa_printf(MSG_DEBUG, "P2P: Invitation from peer " MACSTR
3317 " was rejected (status %u)", MAC2STR(sa), status);
3318 return;
3319 }
3320
3321 if (!s) {
3322 if (bssid) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07003323 wpa_msg_global(wpa_s, MSG_INFO,
3324 P2P_EVENT_INVITATION_RECEIVED
3325 "sa=" MACSTR " go_dev_addr=" MACSTR
3326 " bssid=" MACSTR " unknown-network",
3327 MAC2STR(sa), MAC2STR(go_dev_addr),
3328 MAC2STR(bssid));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003329 } else {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07003330 wpa_msg_global(wpa_s, MSG_INFO,
3331 P2P_EVENT_INVITATION_RECEIVED
3332 "sa=" MACSTR " go_dev_addr=" MACSTR
3333 " unknown-network",
3334 MAC2STR(sa), MAC2STR(go_dev_addr));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003335 }
3336 return;
3337 }
3338
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08003339 if (s->mode == WPAS_MODE_P2P_GO && op_freq) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07003340 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_INVITATION_RECEIVED
3341 "sa=" MACSTR " persistent=%d freq=%d",
3342 MAC2STR(sa), s->id, op_freq);
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08003343 } else {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07003344 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_INVITATION_RECEIVED
3345 "sa=" MACSTR " persistent=%d",
3346 MAC2STR(sa), s->id);
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08003347 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003348}
3349
3350
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003351static void wpas_remove_persistent_peer(struct wpa_supplicant *wpa_s,
3352 struct wpa_ssid *ssid,
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07003353 const u8 *peer, int inv)
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003354{
3355 size_t i;
3356
3357 if (ssid == NULL)
3358 return;
3359
3360 for (i = 0; ssid->p2p_client_list && i < ssid->num_p2p_clients; i++) {
Dmitry Shmidtff787d52015-01-12 13:01:47 -08003361 if (os_memcmp(ssid->p2p_client_list + i * 2 * ETH_ALEN, peer,
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003362 ETH_ALEN) == 0)
3363 break;
3364 }
Dmitry Shmidt6aa8ae42014-07-07 09:31:33 -07003365 if (i >= ssid->num_p2p_clients || !ssid->p2p_client_list) {
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003366 if (ssid->mode != WPAS_MODE_P2P_GO &&
3367 os_memcmp(ssid->bssid, peer, ETH_ALEN) == 0) {
3368 wpa_printf(MSG_DEBUG, "P2P: Remove persistent group %d "
3369 "due to invitation result", ssid->id);
3370 wpas_notify_network_removed(wpa_s, ssid);
3371 wpa_config_remove_network(wpa_s->conf, ssid->id);
3372 return;
3373 }
3374 return; /* Peer not found in client list */
3375 }
3376
3377 wpa_printf(MSG_DEBUG, "P2P: Remove peer " MACSTR " from persistent "
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07003378 "group %d client list%s",
3379 MAC2STR(peer), ssid->id,
3380 inv ? " due to invitation result" : "");
Dmitry Shmidtff787d52015-01-12 13:01:47 -08003381 os_memmove(ssid->p2p_client_list + i * 2 * ETH_ALEN,
3382 ssid->p2p_client_list + (i + 1) * 2 * ETH_ALEN,
3383 (ssid->num_p2p_clients - i - 1) * 2 * ETH_ALEN);
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003384 ssid->num_p2p_clients--;
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003385 if (wpa_s->parent->conf->update_config &&
3386 wpa_config_write(wpa_s->parent->confname, wpa_s->parent->conf))
3387 wpa_printf(MSG_DEBUG, "P2P: Failed to update configuration");
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003388}
3389
3390
3391static void wpas_remove_persistent_client(struct wpa_supplicant *wpa_s,
3392 const u8 *peer)
3393{
3394 struct wpa_ssid *ssid;
3395
3396 wpa_s = wpa_s->global->p2p_invite_group;
3397 if (wpa_s == NULL)
3398 return; /* No known invitation group */
3399 ssid = wpa_s->current_ssid;
3400 if (ssid == NULL || ssid->mode != WPAS_MODE_P2P_GO ||
3401 !ssid->p2p_persistent_group)
3402 return; /* Not operating as a GO in persistent group */
3403 ssid = wpas_p2p_get_persistent(wpa_s->parent, peer,
3404 ssid->ssid, ssid->ssid_len);
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07003405 wpas_remove_persistent_peer(wpa_s, ssid, peer, 1);
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003406}
3407
3408
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08003409static void wpas_invitation_result(void *ctx, int status, const u8 *bssid,
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003410 const struct p2p_channels *channels,
Dmitry Shmidt15907092014-03-25 10:42:57 -07003411 const u8 *peer, int neg_freq,
3412 int peer_oper_freq)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003413{
3414 struct wpa_supplicant *wpa_s = ctx;
3415 struct wpa_ssid *ssid;
Dmitry Shmidt15907092014-03-25 10:42:57 -07003416 int freq;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003417
3418 if (bssid) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07003419 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_INVITATION_RESULT
3420 "status=%d " MACSTR,
3421 status, MAC2STR(bssid));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003422 } else {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07003423 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_INVITATION_RESULT
3424 "status=%d ", status);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003425 }
3426 wpas_notify_p2p_invitation_result(wpa_s, status, bssid);
3427
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003428 wpa_printf(MSG_DEBUG, "P2P: Invitation result - status=%d peer=" MACSTR,
3429 status, MAC2STR(peer));
3430 if (wpa_s->pending_invite_ssid_id == -1) {
3431 if (status == P2P_SC_FAIL_UNKNOWN_GROUP)
3432 wpas_remove_persistent_client(wpa_s, peer);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003433 return; /* Invitation to active group */
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003434 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003435
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08003436 if (status == P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE) {
3437 wpa_printf(MSG_DEBUG, "P2P: Waiting for peer to start another "
3438 "invitation exchange to indicate readiness for "
3439 "re-invocation");
3440 }
3441
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003442 if (status != P2P_SC_SUCCESS) {
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003443 if (status == P2P_SC_FAIL_UNKNOWN_GROUP) {
3444 ssid = wpa_config_get_network(
3445 wpa_s->conf, wpa_s->pending_invite_ssid_id);
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07003446 wpas_remove_persistent_peer(wpa_s, ssid, peer, 1);
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003447 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003448 wpas_p2p_remove_pending_group_interface(wpa_s);
3449 return;
3450 }
3451
3452 ssid = wpa_config_get_network(wpa_s->conf,
3453 wpa_s->pending_invite_ssid_id);
3454 if (ssid == NULL) {
3455 wpa_printf(MSG_ERROR, "P2P: Could not find persistent group "
3456 "data matching with invitation");
3457 return;
3458 }
3459
Jouni Malinen2c5b17d2012-10-13 21:52:46 -07003460 /*
3461 * The peer could have missed our ctrl::ack frame for Invitation
3462 * Response and continue retransmitting the frame. To reduce the
3463 * likelihood of the peer not getting successful TX status for the
3464 * Invitation Response frame, wait a short time here before starting
3465 * the persistent group so that we will remain on the current channel to
3466 * acknowledge any possible retransmission from the peer.
3467 */
3468 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: 50 ms wait on current channel before "
3469 "starting persistent group");
3470 os_sleep(0, 50000);
3471
Dmitry Shmidt15907092014-03-25 10:42:57 -07003472 if (neg_freq > 0 && ssid->mode == WPAS_MODE_P2P_GO &&
3473 freq_included(channels, neg_freq))
3474 freq = neg_freq;
3475 else if (peer_oper_freq > 0 && ssid->mode != WPAS_MODE_P2P_GO &&
3476 freq_included(channels, peer_oper_freq))
3477 freq = peer_oper_freq;
3478 else
3479 freq = 0;
3480
3481 wpa_printf(MSG_DEBUG, "P2P: Persistent group invitation success - op_freq=%d MHz SSID=%s",
3482 freq, wpa_ssid_txt(ssid->ssid, ssid->ssid_len));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003483 wpas_p2p_group_add_persistent(wpa_s, ssid,
Jouni Malinen31be0a42012-08-31 21:20:51 +03003484 ssid->mode == WPAS_MODE_P2P_GO,
Dmitry Shmidt96be6222014-02-13 10:16:51 -08003485 wpa_s->p2p_persistent_go_freq,
Dmitry Shmidt15907092014-03-25 10:42:57 -07003486 freq,
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003487 wpa_s->p2p_go_ht40, wpa_s->p2p_go_vht,
3488 channels,
Dmitry Shmidt56052862013-10-04 10:23:25 -07003489 ssid->mode == WPAS_MODE_P2P_GO ?
3490 P2P_MAX_INITIAL_CONN_WAIT_GO_REINVOKE :
3491 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003492}
3493
3494
Dmitry Shmidt04949592012-07-19 12:16:46 -07003495static int wpas_p2p_disallowed_freq(struct wpa_global *global,
3496 unsigned int freq)
3497{
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08003498 if (freq_range_list_includes(&global->p2p_go_avoid_freq, freq))
3499 return 1;
Dmitry Shmidt4ce9c872013-10-24 11:08:13 -07003500 return freq_range_list_includes(&global->p2p_disallow_freq, freq);
Dmitry Shmidt04949592012-07-19 12:16:46 -07003501}
3502
3503
3504static void wpas_p2p_add_chan(struct p2p_reg_class *reg, u8 chan)
3505{
3506 reg->channel[reg->channels] = chan;
3507 reg->channels++;
3508}
3509
3510
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003511static int wpas_p2p_default_channels(struct wpa_supplicant *wpa_s,
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003512 struct p2p_channels *chan,
3513 struct p2p_channels *cli_chan)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003514{
3515 int i, cla = 0;
3516
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003517 wpa_s->global->p2p_24ghz_social_channels = 1;
3518
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003519 os_memset(cli_chan, 0, sizeof(*cli_chan));
3520
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003521 wpa_printf(MSG_DEBUG, "P2P: Enable operating classes for 2.4 GHz "
3522 "band");
3523
3524 /* Operating class 81 - 2.4 GHz band channels 1..13 */
3525 chan->reg_class[cla].reg_class = 81;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003526 chan->reg_class[cla].channels = 0;
3527 for (i = 0; i < 11; i++) {
3528 if (!wpas_p2p_disallowed_freq(wpa_s->global, 2412 + i * 5))
3529 wpas_p2p_add_chan(&chan->reg_class[cla], i + 1);
3530 }
3531 if (chan->reg_class[cla].channels)
3532 cla++;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003533
3534 wpa_printf(MSG_DEBUG, "P2P: Enable operating classes for lower 5 GHz "
3535 "band");
3536
3537 /* Operating class 115 - 5 GHz, channels 36-48 */
3538 chan->reg_class[cla].reg_class = 115;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003539 chan->reg_class[cla].channels = 0;
3540 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 36 * 5))
3541 wpas_p2p_add_chan(&chan->reg_class[cla], 36);
3542 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 40 * 5))
3543 wpas_p2p_add_chan(&chan->reg_class[cla], 40);
3544 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 44 * 5))
3545 wpas_p2p_add_chan(&chan->reg_class[cla], 44);
3546 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 48 * 5))
3547 wpas_p2p_add_chan(&chan->reg_class[cla], 48);
3548 if (chan->reg_class[cla].channels)
3549 cla++;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003550
3551 wpa_printf(MSG_DEBUG, "P2P: Enable operating classes for higher 5 GHz "
3552 "band");
3553
3554 /* Operating class 124 - 5 GHz, channels 149,153,157,161 */
3555 chan->reg_class[cla].reg_class = 124;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003556 chan->reg_class[cla].channels = 0;
3557 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 149 * 5))
3558 wpas_p2p_add_chan(&chan->reg_class[cla], 149);
3559 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 153 * 5))
3560 wpas_p2p_add_chan(&chan->reg_class[cla], 153);
3561 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 156 * 5))
3562 wpas_p2p_add_chan(&chan->reg_class[cla], 157);
3563 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 161 * 5))
3564 wpas_p2p_add_chan(&chan->reg_class[cla], 161);
3565 if (chan->reg_class[cla].channels)
3566 cla++;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003567
3568 chan->reg_classes = cla;
3569 return 0;
3570}
3571
3572
3573static struct hostapd_hw_modes * get_mode(struct hostapd_hw_modes *modes,
3574 u16 num_modes,
3575 enum hostapd_hw_mode mode)
3576{
3577 u16 i;
3578
3579 for (i = 0; i < num_modes; i++) {
3580 if (modes[i].mode == mode)
3581 return &modes[i];
3582 }
3583
3584 return NULL;
3585}
3586
3587
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003588enum chan_allowed {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003589 NOT_ALLOWED, NO_IR, ALLOWED
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003590};
3591
Dmitry Shmidt04949592012-07-19 12:16:46 -07003592static int has_channel(struct wpa_global *global,
3593 struct hostapd_hw_modes *mode, u8 chan, int *flags)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003594{
3595 int i;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003596 unsigned int freq;
3597
3598 freq = (mode->mode == HOSTAPD_MODE_IEEE80211A ? 5000 : 2407) +
3599 chan * 5;
3600 if (wpas_p2p_disallowed_freq(global, freq))
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003601 return NOT_ALLOWED;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003602
3603 for (i = 0; i < mode->num_channels; i++) {
3604 if (mode->channels[i].chan == chan) {
3605 if (flags)
3606 *flags = mode->channels[i].flag;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003607 if (mode->channels[i].flag &
3608 (HOSTAPD_CHAN_DISABLED |
3609 HOSTAPD_CHAN_RADAR))
3610 return NOT_ALLOWED;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003611 if (mode->channels[i].flag & HOSTAPD_CHAN_NO_IR)
3612 return NO_IR;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003613 return ALLOWED;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003614 }
3615 }
3616
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003617 return NOT_ALLOWED;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003618}
3619
3620
3621struct p2p_oper_class_map {
3622 enum hostapd_hw_mode mode;
3623 u8 op_class;
3624 u8 min_chan;
3625 u8 max_chan;
3626 u8 inc;
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07003627 enum { BW20, BW40PLUS, BW40MINUS, BW80, BW2160 } bw;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003628};
3629
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003630static struct p2p_oper_class_map op_class[] = {
3631 { HOSTAPD_MODE_IEEE80211G, 81, 1, 13, 1, BW20 },
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003632#if 0 /* Do not enable HT40 on 2 GHz for now */
3633 { HOSTAPD_MODE_IEEE80211G, 83, 1, 9, 1, BW40PLUS },
3634 { HOSTAPD_MODE_IEEE80211G, 84, 5, 13, 1, BW40MINUS },
3635#endif
3636 { HOSTAPD_MODE_IEEE80211A, 115, 36, 48, 4, BW20 },
3637 { HOSTAPD_MODE_IEEE80211A, 124, 149, 161, 4, BW20 },
3638 { HOSTAPD_MODE_IEEE80211A, 116, 36, 44, 8, BW40PLUS },
3639 { HOSTAPD_MODE_IEEE80211A, 117, 40, 48, 8, BW40MINUS },
3640 { HOSTAPD_MODE_IEEE80211A, 126, 149, 157, 8, BW40PLUS },
3641 { HOSTAPD_MODE_IEEE80211A, 127, 153, 161, 8, BW40MINUS },
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003642
3643 /*
3644 * IEEE P802.11ac/D7.0 Table E-4 actually talks about channel center
3645 * frequency index 42, 58, 106, 122, 138, 155 with channel spacing of
3646 * 80 MHz, but currently use the following definition for simplicity
3647 * (these center frequencies are not actual channels, which makes
3648 * has_channel() fail). wpas_p2p_verify_80mhz() should take care of
3649 * removing invalid channels.
3650 */
3651 { HOSTAPD_MODE_IEEE80211A, 128, 36, 161, 4, BW80 },
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07003652 { HOSTAPD_MODE_IEEE80211AD, 180, 1, 4, 1, BW2160 },
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003653 { -1, 0, 0, 0, 0, BW20 }
3654};
3655
3656
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003657static int wpas_p2p_get_center_80mhz(struct wpa_supplicant *wpa_s,
3658 struct hostapd_hw_modes *mode,
3659 u8 channel)
3660{
3661 u8 center_channels[] = { 42, 58, 106, 122, 138, 155 };
3662 unsigned int i;
3663
3664 if (mode->mode != HOSTAPD_MODE_IEEE80211A)
3665 return 0;
3666
3667 for (i = 0; i < ARRAY_SIZE(center_channels); i++)
3668 /*
3669 * In 80 MHz, the bandwidth "spans" 12 channels (e.g., 36-48),
3670 * so the center channel is 6 channels away from the start/end.
3671 */
3672 if (channel >= center_channels[i] - 6 &&
3673 channel <= center_channels[i] + 6)
3674 return center_channels[i];
3675
3676 return 0;
3677}
3678
3679
3680static enum chan_allowed wpas_p2p_verify_80mhz(struct wpa_supplicant *wpa_s,
3681 struct hostapd_hw_modes *mode,
3682 u8 channel, u8 bw)
3683{
3684 u8 center_chan;
3685 int i, flags;
3686 enum chan_allowed res, ret = ALLOWED;
3687
3688 center_chan = wpas_p2p_get_center_80mhz(wpa_s, mode, channel);
3689 if (!center_chan)
3690 return NOT_ALLOWED;
3691 if (center_chan >= 58 && center_chan <= 138)
3692 return NOT_ALLOWED; /* Do not allow DFS channels for P2P */
3693
3694 /* check all the channels are available */
3695 for (i = 0; i < 4; i++) {
3696 int adj_chan = center_chan - 6 + i * 4;
3697
3698 res = has_channel(wpa_s->global, mode, adj_chan, &flags);
3699 if (res == NOT_ALLOWED)
3700 return NOT_ALLOWED;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003701 if (res == NO_IR)
3702 ret = NO_IR;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003703
3704 if (i == 0 && !(flags & HOSTAPD_CHAN_VHT_10_70))
3705 return NOT_ALLOWED;
3706 if (i == 1 && !(flags & HOSTAPD_CHAN_VHT_30_50))
3707 return NOT_ALLOWED;
3708 if (i == 2 && !(flags & HOSTAPD_CHAN_VHT_50_30))
3709 return NOT_ALLOWED;
3710 if (i == 3 && !(flags & HOSTAPD_CHAN_VHT_70_10))
3711 return NOT_ALLOWED;
3712 }
3713
3714 return ret;
3715}
3716
3717
3718static enum chan_allowed wpas_p2p_verify_channel(struct wpa_supplicant *wpa_s,
3719 struct hostapd_hw_modes *mode,
3720 u8 channel, u8 bw)
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003721{
Dmitry Shmidt96be6222014-02-13 10:16:51 -08003722 int flag = 0;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003723 enum chan_allowed res, res2;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003724
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003725 res2 = res = has_channel(wpa_s->global, mode, channel, &flag);
3726 if (bw == BW40MINUS) {
3727 if (!(flag & HOSTAPD_CHAN_HT40MINUS))
3728 return NOT_ALLOWED;
3729 res2 = has_channel(wpa_s->global, mode, channel - 4, NULL);
3730 } else if (bw == BW40PLUS) {
3731 if (!(flag & HOSTAPD_CHAN_HT40PLUS))
3732 return NOT_ALLOWED;
3733 res2 = has_channel(wpa_s->global, mode, channel + 4, NULL);
3734 } else if (bw == BW80) {
3735 res2 = wpas_p2p_verify_80mhz(wpa_s, mode, channel, bw);
3736 }
3737
3738 if (res == NOT_ALLOWED || res2 == NOT_ALLOWED)
3739 return NOT_ALLOWED;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003740 if (res == NO_IR || res2 == NO_IR)
3741 return NO_IR;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003742 return res;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003743}
3744
3745
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003746static int wpas_p2p_setup_channels(struct wpa_supplicant *wpa_s,
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003747 struct p2p_channels *chan,
3748 struct p2p_channels *cli_chan)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003749{
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003750 struct hostapd_hw_modes *mode;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003751 int cla, op, cli_cla;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003752
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003753 if (wpa_s->hw.modes == NULL) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003754 wpa_printf(MSG_DEBUG, "P2P: Driver did not support fetching "
3755 "of all supported channels; assume dualband "
3756 "support");
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003757 return wpas_p2p_default_channels(wpa_s, chan, cli_chan);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003758 }
3759
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003760 cla = cli_cla = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003761
3762 for (op = 0; op_class[op].op_class; op++) {
3763 struct p2p_oper_class_map *o = &op_class[op];
3764 u8 ch;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003765 struct p2p_reg_class *reg = NULL, *cli_reg = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003766
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003767 mode = get_mode(wpa_s->hw.modes, wpa_s->hw.num_modes, o->mode);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003768 if (mode == NULL)
3769 continue;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003770 if (mode->mode == HOSTAPD_MODE_IEEE80211G)
3771 wpa_s->global->p2p_24ghz_social_channels = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003772 for (ch = o->min_chan; ch <= o->max_chan; ch += o->inc) {
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003773 enum chan_allowed res;
3774 res = wpas_p2p_verify_channel(wpa_s, mode, ch, o->bw);
3775 if (res == ALLOWED) {
3776 if (reg == NULL) {
3777 wpa_printf(MSG_DEBUG, "P2P: Add operating class %u",
3778 o->op_class);
3779 reg = &chan->reg_class[cla];
3780 cla++;
3781 reg->reg_class = o->op_class;
3782 }
3783 reg->channel[reg->channels] = ch;
3784 reg->channels++;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003785 } else if (res == NO_IR &&
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003786 wpa_s->conf->p2p_add_cli_chan) {
3787 if (cli_reg == NULL) {
3788 wpa_printf(MSG_DEBUG, "P2P: Add operating class %u (client only)",
3789 o->op_class);
3790 cli_reg = &cli_chan->reg_class[cli_cla];
3791 cli_cla++;
3792 cli_reg->reg_class = o->op_class;
3793 }
3794 cli_reg->channel[cli_reg->channels] = ch;
3795 cli_reg->channels++;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003796 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003797 }
3798 if (reg) {
3799 wpa_hexdump(MSG_DEBUG, "P2P: Channels",
3800 reg->channel, reg->channels);
3801 }
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003802 if (cli_reg) {
3803 wpa_hexdump(MSG_DEBUG, "P2P: Channels (client only)",
3804 cli_reg->channel, cli_reg->channels);
3805 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003806 }
3807
3808 chan->reg_classes = cla;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003809 cli_chan->reg_classes = cli_cla;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003810
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003811 return 0;
3812}
3813
3814
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003815int wpas_p2p_get_ht40_mode(struct wpa_supplicant *wpa_s,
3816 struct hostapd_hw_modes *mode, u8 channel)
3817{
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003818 int op;
3819 enum chan_allowed ret;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003820
3821 for (op = 0; op_class[op].op_class; op++) {
3822 struct p2p_oper_class_map *o = &op_class[op];
3823 u8 ch;
3824
3825 for (ch = o->min_chan; ch <= o->max_chan; ch += o->inc) {
3826 if (o->mode != HOSTAPD_MODE_IEEE80211A ||
3827 o->bw == BW20 || ch != channel)
3828 continue;
3829 ret = wpas_p2p_verify_channel(wpa_s, mode, ch, o->bw);
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003830 if (ret == ALLOWED)
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003831 return (o->bw == BW40MINUS) ? -1 : 1;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003832 }
3833 }
3834 return 0;
3835}
3836
3837
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003838int wpas_p2p_get_vht80_center(struct wpa_supplicant *wpa_s,
3839 struct hostapd_hw_modes *mode, u8 channel)
3840{
3841 if (!wpas_p2p_verify_channel(wpa_s, mode, channel, BW80))
3842 return 0;
3843
3844 return wpas_p2p_get_center_80mhz(wpa_s, mode, channel);
3845}
3846
3847
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003848static int wpas_get_noa(void *ctx, const u8 *interface_addr, u8 *buf,
3849 size_t buf_len)
3850{
3851 struct wpa_supplicant *wpa_s = ctx;
3852
3853 for (wpa_s = wpa_s->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
3854 if (os_memcmp(wpa_s->own_addr, interface_addr, ETH_ALEN) == 0)
3855 break;
3856 }
3857 if (wpa_s == NULL)
3858 return -1;
3859
3860 return wpa_drv_get_noa(wpa_s, buf, buf_len);
3861}
3862
3863
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07003864struct wpa_supplicant * wpas_get_p2p_go_iface(struct wpa_supplicant *wpa_s,
3865 const u8 *ssid, size_t ssid_len)
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08003866{
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07003867 for (wpa_s = wpa_s->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
3868 struct wpa_ssid *s = wpa_s->current_ssid;
3869 if (s == NULL)
3870 continue;
3871 if (s->mode != WPAS_MODE_P2P_GO &&
3872 s->mode != WPAS_MODE_AP &&
3873 s->mode != WPAS_MODE_P2P_GROUP_FORMATION)
3874 continue;
3875 if (s->ssid_len != ssid_len ||
Dmitry Shmidt03658832014-08-13 11:03:49 -07003876 os_memcmp(ssid, s->ssid, ssid_len) != 0)
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07003877 continue;
3878 return wpa_s;
3879 }
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08003880
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07003881 return NULL;
3882
3883}
3884
3885
3886struct wpa_supplicant * wpas_get_p2p_client_iface(struct wpa_supplicant *wpa_s,
3887 const u8 *peer_dev_addr)
3888{
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08003889 for (wpa_s = wpa_s->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
3890 struct wpa_ssid *ssid = wpa_s->current_ssid;
3891 if (ssid == NULL)
3892 continue;
3893 if (ssid->mode != WPAS_MODE_INFRA)
3894 continue;
3895 if (wpa_s->wpa_state != WPA_COMPLETED &&
3896 wpa_s->wpa_state != WPA_GROUP_HANDSHAKE)
3897 continue;
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07003898 if (os_memcmp(wpa_s->go_dev_addr, peer_dev_addr, ETH_ALEN) == 0)
3899 return wpa_s;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08003900 }
3901
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07003902 return NULL;
3903}
3904
3905
3906static int wpas_go_connected(void *ctx, const u8 *dev_addr)
3907{
3908 struct wpa_supplicant *wpa_s = ctx;
3909
3910 return wpas_get_p2p_client_iface(wpa_s, dev_addr) != NULL;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08003911}
3912
3913
Dmitry Shmidt18463232014-01-24 12:29:41 -08003914static int wpas_is_concurrent_session_active(void *ctx)
3915{
3916 struct wpa_supplicant *wpa_s = ctx;
3917 struct wpa_supplicant *ifs;
3918
3919 for (ifs = wpa_s->global->ifaces; ifs; ifs = ifs->next) {
3920 if (ifs == wpa_s)
3921 continue;
3922 if (ifs->wpa_state > WPA_ASSOCIATED)
3923 return 1;
3924 }
3925 return 0;
3926}
3927
3928
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07003929static void wpas_p2p_debug_print(void *ctx, int level, const char *msg)
3930{
3931 struct wpa_supplicant *wpa_s = ctx;
3932 wpa_msg_global(wpa_s, level, "P2P: %s", msg);
3933}
3934
3935
Dmitry Shmidt9ead16e2014-10-07 13:15:23 -07003936int wpas_p2p_add_p2pdev_interface(struct wpa_supplicant *wpa_s,
3937 const char *conf_p2p_dev)
Dmitry Shmidt34af3062013-07-11 10:46:32 -07003938{
3939 struct wpa_interface iface;
3940 struct wpa_supplicant *p2pdev_wpa_s;
3941 char ifname[100];
3942 char force_name[100];
3943 int ret;
3944
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003945 ret = os_snprintf(ifname, sizeof(ifname), P2P_MGMT_DEVICE_PREFIX "%s",
3946 wpa_s->ifname);
3947 if (os_snprintf_error(sizeof(ifname), ret))
3948 return -1;
Dmitry Shmidt34af3062013-07-11 10:46:32 -07003949 force_name[0] = '\0';
3950 wpa_s->pending_interface_type = WPA_IF_P2P_DEVICE;
3951 ret = wpa_drv_if_add(wpa_s, WPA_IF_P2P_DEVICE, ifname, NULL, NULL,
3952 force_name, wpa_s->pending_interface_addr, NULL);
3953 if (ret < 0) {
3954 wpa_printf(MSG_DEBUG, "P2P: Failed to create P2P Device interface");
3955 return ret;
3956 }
3957 os_strlcpy(wpa_s->pending_interface_name, ifname,
3958 sizeof(wpa_s->pending_interface_name));
3959
3960 os_memset(&iface, 0, sizeof(iface));
3961 iface.p2p_mgmt = 1;
3962 iface.ifname = wpa_s->pending_interface_name;
3963 iface.driver = wpa_s->driver->name;
3964 iface.driver_param = wpa_s->conf->driver_param;
Dmitry Shmidt2ac5f602014-03-07 10:08:21 -08003965
3966 /*
3967 * If a P2P Device configuration file was given, use it as the interface
3968 * configuration file (instead of using parent's configuration file.
3969 */
Dmitry Shmidt9ead16e2014-10-07 13:15:23 -07003970 if (conf_p2p_dev) {
3971 iface.confname = conf_p2p_dev;
Dmitry Shmidt2ac5f602014-03-07 10:08:21 -08003972 iface.ctrl_interface = NULL;
3973 } else {
3974 iface.confname = wpa_s->confname;
3975 iface.ctrl_interface = wpa_s->conf->ctrl_interface;
3976 }
3977 iface.conf_p2p_dev = NULL;
3978
Dmitry Shmidt34af3062013-07-11 10:46:32 -07003979 p2pdev_wpa_s = wpa_supplicant_add_iface(wpa_s->global, &iface);
3980 if (!p2pdev_wpa_s) {
3981 wpa_printf(MSG_DEBUG, "P2P: Failed to add P2P Device interface");
3982 return -1;
3983 }
3984 p2pdev_wpa_s->parent = wpa_s;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003985 wpa_s->p2p_dev = p2pdev_wpa_s;
Dmitry Shmidt34af3062013-07-11 10:46:32 -07003986
3987 wpa_s->pending_interface_name[0] = '\0';
3988 return 0;
3989}
3990
3991
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08003992static void wpas_presence_resp(void *ctx, const u8 *src, u8 status,
3993 const u8 *noa, size_t noa_len)
3994{
3995 struct wpa_supplicant *wpa_s, *intf = ctx;
3996 char hex[100];
3997
3998 for (wpa_s = intf->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
3999 if (wpa_s->waiting_presence_resp)
4000 break;
4001 }
4002 if (!wpa_s) {
4003 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: No group interface was waiting for presence response");
4004 return;
4005 }
4006 wpa_s->waiting_presence_resp = 0;
4007
4008 wpa_snprintf_hex(hex, sizeof(hex), noa, noa_len);
4009 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_PRESENCE_RESPONSE "src=" MACSTR
4010 " status=%u noa=%s", MAC2STR(src), status, hex);
4011}
4012
4013
Dmitry Shmidt5a1480c2014-05-12 09:46:02 -07004014static int _wpas_p2p_in_progress(void *ctx)
4015{
4016 struct wpa_supplicant *wpa_s = ctx;
4017 return wpas_p2p_in_progress(wpa_s);
4018}
4019
4020
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004021/**
4022 * wpas_p2p_init - Initialize P2P module for %wpa_supplicant
4023 * @global: Pointer to global data from wpa_supplicant_init()
4024 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
4025 * Returns: 0 on success, -1 on failure
4026 */
4027int wpas_p2p_init(struct wpa_global *global, struct wpa_supplicant *wpa_s)
4028{
4029 struct p2p_config p2p;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004030 int i;
4031
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07004032 if (wpa_s->conf->p2p_disabled)
4033 return 0;
4034
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004035 if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_CAPABLE))
4036 return 0;
4037
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004038 if (global->p2p)
4039 return 0;
4040
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004041 os_memset(&p2p, 0, sizeof(p2p));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004042 p2p.cb_ctx = wpa_s;
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07004043 p2p.debug_print = wpas_p2p_debug_print;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004044 p2p.p2p_scan = wpas_p2p_scan;
4045 p2p.send_action = wpas_send_action;
4046 p2p.send_action_done = wpas_send_action_done;
4047 p2p.go_neg_completed = wpas_go_neg_completed;
4048 p2p.go_neg_req_rx = wpas_go_neg_req_rx;
4049 p2p.dev_found = wpas_dev_found;
4050 p2p.dev_lost = wpas_dev_lost;
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07004051 p2p.find_stopped = wpas_find_stopped;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004052 p2p.start_listen = wpas_start_listen;
4053 p2p.stop_listen = wpas_stop_listen;
4054 p2p.send_probe_resp = wpas_send_probe_resp;
4055 p2p.sd_request = wpas_sd_request;
4056 p2p.sd_response = wpas_sd_response;
4057 p2p.prov_disc_req = wpas_prov_disc_req;
4058 p2p.prov_disc_resp = wpas_prov_disc_resp;
Jouni Malinen75ecf522011-06-27 15:19:46 -07004059 p2p.prov_disc_fail = wpas_prov_disc_fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004060 p2p.invitation_process = wpas_invitation_process;
4061 p2p.invitation_received = wpas_invitation_received;
4062 p2p.invitation_result = wpas_invitation_result;
4063 p2p.get_noa = wpas_get_noa;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08004064 p2p.go_connected = wpas_go_connected;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08004065 p2p.presence_resp = wpas_presence_resp;
Dmitry Shmidt18463232014-01-24 12:29:41 -08004066 p2p.is_concurrent_session_active = wpas_is_concurrent_session_active;
Dmitry Shmidt5a1480c2014-05-12 09:46:02 -07004067 p2p.is_p2p_in_progress = _wpas_p2p_in_progress;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004068
4069 os_memcpy(wpa_s->global->p2p_dev_addr, wpa_s->own_addr, ETH_ALEN);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004070 os_memcpy(p2p.dev_addr, wpa_s->global->p2p_dev_addr, ETH_ALEN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004071 p2p.dev_name = wpa_s->conf->device_name;
4072 p2p.manufacturer = wpa_s->conf->manufacturer;
4073 p2p.model_name = wpa_s->conf->model_name;
4074 p2p.model_number = wpa_s->conf->model_number;
4075 p2p.serial_number = wpa_s->conf->serial_number;
4076 if (wpa_s->wps) {
4077 os_memcpy(p2p.uuid, wpa_s->wps->uuid, 16);
4078 p2p.config_methods = wpa_s->wps->config_methods;
4079 }
4080
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07004081 if (wpas_p2p_setup_channels(wpa_s, &p2p.channels, &p2p.cli_channels)) {
4082 wpa_printf(MSG_ERROR,
4083 "P2P: Failed to configure supported channel list");
4084 return -1;
4085 }
4086
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004087 if (wpa_s->conf->p2p_listen_reg_class &&
4088 wpa_s->conf->p2p_listen_channel) {
4089 p2p.reg_class = wpa_s->conf->p2p_listen_reg_class;
4090 p2p.channel = wpa_s->conf->p2p_listen_channel;
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07004091 p2p.channel_forced = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004092 } else {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004093 /*
4094 * Pick one of the social channels randomly as the listen
4095 * channel.
4096 */
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07004097 if (p2p_config_get_random_social(&p2p, &p2p.reg_class,
4098 &p2p.channel) != 0) {
4099 wpa_printf(MSG_ERROR,
4100 "P2P: Failed to select random social channel as listen channel");
4101 return -1;
4102 }
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07004103 p2p.channel_forced = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004104 }
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07004105 wpa_printf(MSG_DEBUG, "P2P: Own listen channel: %d:%d",
4106 p2p.reg_class, p2p.channel);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004107
4108 if (wpa_s->conf->p2p_oper_reg_class &&
4109 wpa_s->conf->p2p_oper_channel) {
4110 p2p.op_reg_class = wpa_s->conf->p2p_oper_reg_class;
4111 p2p.op_channel = wpa_s->conf->p2p_oper_channel;
4112 p2p.cfg_op_channel = 1;
4113 wpa_printf(MSG_DEBUG, "P2P: Configured operating channel: "
4114 "%d:%d", p2p.op_reg_class, p2p.op_channel);
4115
4116 } else {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004117 /*
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07004118 * Use random operation channel from 2.4 GHz band social
4119 * channels (1, 6, 11) or band 60 GHz social channel (2) if no
4120 * other preference is indicated.
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004121 */
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07004122 if (p2p_config_get_random_social(&p2p, &p2p.op_reg_class,
4123 &p2p.op_channel) != 0) {
4124 wpa_printf(MSG_ERROR,
4125 "P2P: Failed to select random social channel as operation channel");
4126 return -1;
4127 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004128 p2p.cfg_op_channel = 0;
4129 wpa_printf(MSG_DEBUG, "P2P: Random operating channel: "
4130 "%d:%d", p2p.op_reg_class, p2p.op_channel);
4131 }
Dmitry Shmidt44c95782013-05-17 09:51:35 -07004132
4133 if (wpa_s->conf->p2p_pref_chan && wpa_s->conf->num_p2p_pref_chan) {
4134 p2p.pref_chan = wpa_s->conf->p2p_pref_chan;
4135 p2p.num_pref_chan = wpa_s->conf->num_p2p_pref_chan;
4136 }
4137
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004138 if (wpa_s->conf->country[0] && wpa_s->conf->country[1]) {
4139 os_memcpy(p2p.country, wpa_s->conf->country, 2);
4140 p2p.country[2] = 0x04;
4141 } else
4142 os_memcpy(p2p.country, "XX\x04", 3);
4143
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004144 os_memcpy(p2p.pri_dev_type, wpa_s->conf->device_type,
4145 WPS_DEV_TYPE_LEN);
4146
4147 p2p.num_sec_dev_types = wpa_s->conf->num_sec_device_types;
4148 os_memcpy(p2p.sec_dev_type, wpa_s->conf->sec_device_type,
4149 p2p.num_sec_dev_types * WPS_DEV_TYPE_LEN);
4150
4151 p2p.concurrent_operations = !!(wpa_s->drv_flags &
4152 WPA_DRIVER_FLAGS_P2P_CONCURRENT);
4153
4154 p2p.max_peers = 100;
4155
4156 if (wpa_s->conf->p2p_ssid_postfix) {
4157 p2p.ssid_postfix_len =
4158 os_strlen(wpa_s->conf->p2p_ssid_postfix);
4159 if (p2p.ssid_postfix_len > sizeof(p2p.ssid_postfix))
4160 p2p.ssid_postfix_len = sizeof(p2p.ssid_postfix);
4161 os_memcpy(p2p.ssid_postfix, wpa_s->conf->p2p_ssid_postfix,
4162 p2p.ssid_postfix_len);
4163 }
4164
4165 p2p.p2p_intra_bss = wpa_s->conf->p2p_intra_bss;
4166
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08004167 p2p.max_listen = wpa_s->max_remain_on_chan;
4168
Dmitry Shmidt2271d3f2014-06-23 12:16:31 -07004169 if (wpa_s->conf->p2p_passphrase_len >= 8 &&
4170 wpa_s->conf->p2p_passphrase_len <= 63)
4171 p2p.passphrase_len = wpa_s->conf->p2p_passphrase_len;
4172 else
4173 p2p.passphrase_len = 8;
4174
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004175 global->p2p = p2p_init(&p2p);
4176 if (global->p2p == NULL)
4177 return -1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07004178 global->p2p_init_wpa_s = wpa_s;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004179
4180 for (i = 0; i < MAX_WPS_VENDOR_EXT; i++) {
4181 if (wpa_s->conf->wps_vendor_ext[i] == NULL)
4182 continue;
4183 p2p_add_wps_vendor_extension(
4184 global->p2p, wpa_s->conf->wps_vendor_ext[i]);
4185 }
4186
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004187 p2p_set_no_go_freq(global->p2p, &wpa_s->conf->p2p_no_go_freq);
4188
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004189 return 0;
4190}
4191
4192
4193/**
4194 * wpas_p2p_deinit - Deinitialize per-interface P2P data
4195 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
4196 *
4197 * This function deinitialize per-interface P2P data.
4198 */
4199void wpas_p2p_deinit(struct wpa_supplicant *wpa_s)
4200{
4201 if (wpa_s->driver && wpa_s->drv_priv)
4202 wpa_drv_probe_req_report(wpa_s, 0);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004203
4204 if (wpa_s->go_params) {
4205 /* Clear any stored provisioning info */
4206 p2p_clear_provisioning_info(
4207 wpa_s->global->p2p,
Dmitry Shmidt04949592012-07-19 12:16:46 -07004208 wpa_s->go_params->peer_device_addr);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004209 }
4210
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004211 os_free(wpa_s->go_params);
4212 wpa_s->go_params = NULL;
Dmitry Shmidt684785c2014-05-12 13:34:29 -07004213 eloop_cancel_timeout(wpas_p2p_psk_failure_removal, wpa_s, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004214 eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s, NULL);
4215 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
4216 wpa_s->p2p_long_listen = 0;
4217 eloop_cancel_timeout(wpas_p2p_long_listen_timeout, wpa_s, NULL);
4218 eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL);
4219 wpas_p2p_remove_pending_group_interface(wpa_s);
Dmitry Shmidt04f534e2013-12-09 15:50:16 -08004220 eloop_cancel_timeout(wpas_p2p_group_freq_conflict, wpa_s, NULL);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08004221 wpas_p2p_listen_work_done(wpa_s);
4222 if (wpa_s->p2p_send_action_work) {
4223 os_free(wpa_s->p2p_send_action_work->ctx);
4224 radio_work_done(wpa_s->p2p_send_action_work);
4225 wpa_s->p2p_send_action_work = NULL;
4226 }
4227 eloop_cancel_timeout(wpas_p2p_send_action_work_timeout, wpa_s, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004228
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08004229 wpabuf_free(wpa_s->p2p_oob_dev_pw);
4230 wpa_s->p2p_oob_dev_pw = NULL;
4231
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004232 os_free(wpa_s->p2p_group_common_freqs);
4233 wpa_s->p2p_group_common_freqs = NULL;
4234 wpa_s->p2p_group_common_freqs_num = 0;
4235
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004236 /* TODO: remove group interface from the driver if this wpa_s instance
4237 * is on top of a P2P group interface */
4238}
4239
4240
4241/**
4242 * wpas_p2p_deinit_global - Deinitialize global P2P module
4243 * @global: Pointer to global data from wpa_supplicant_init()
4244 *
4245 * This function deinitializes the global (per device) P2P module.
4246 */
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07004247static void wpas_p2p_deinit_global(struct wpa_global *global)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004248{
4249 struct wpa_supplicant *wpa_s, *tmp;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004250
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004251 wpa_s = global->ifaces;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004252
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07004253 wpas_p2p_service_flush(global->p2p_init_wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004254
4255 /* Remove remaining P2P group interfaces */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004256 while (wpa_s && wpa_s->p2p_group_interface != NOT_P2P_GROUP_INTERFACE)
4257 wpa_s = wpa_s->next;
4258 while (wpa_s) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004259 tmp = global->ifaces;
4260 while (tmp &&
4261 (tmp == wpa_s ||
4262 tmp->p2p_group_interface == NOT_P2P_GROUP_INTERFACE)) {
4263 tmp = tmp->next;
4264 }
4265 if (tmp == NULL)
4266 break;
Dmitry Shmidt04949592012-07-19 12:16:46 -07004267 /* Disconnect from the P2P group and deinit the interface */
4268 wpas_p2p_disconnect(tmp);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004269 }
4270
4271 /*
4272 * Deinit GO data on any possibly remaining interface (if main
4273 * interface is used as GO).
4274 */
4275 for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
4276 if (wpa_s->ap_iface)
4277 wpas_p2p_group_deinit(wpa_s);
4278 }
4279
4280 p2p_deinit(global->p2p);
4281 global->p2p = NULL;
Dmitry Shmidt04949592012-07-19 12:16:46 -07004282 global->p2p_init_wpa_s = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004283}
4284
4285
4286static int wpas_p2p_create_iface(struct wpa_supplicant *wpa_s)
4287{
Dmitry Shmidt34af3062013-07-11 10:46:32 -07004288 if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_DEDICATED_P2P_DEVICE) &&
4289 wpa_s->conf->p2p_no_group_iface)
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08004290 return 0; /* separate interface disabled per configuration */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004291 if (wpa_s->drv_flags &
4292 (WPA_DRIVER_FLAGS_P2P_DEDICATED_INTERFACE |
4293 WPA_DRIVER_FLAGS_P2P_MGMT_AND_NON_P2P))
4294 return 1; /* P2P group requires a new interface in every case
4295 */
4296 if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_CONCURRENT))
4297 return 0; /* driver does not support concurrent operations */
4298 if (wpa_s->global->ifaces->next)
4299 return 1; /* more that one interface already in use */
4300 if (wpa_s->wpa_state >= WPA_AUTHENTICATING)
4301 return 1; /* this interface is already in use */
4302 return 0;
4303}
4304
4305
4306static int wpas_p2p_start_go_neg(struct wpa_supplicant *wpa_s,
4307 const u8 *peer_addr,
4308 enum p2p_wps_method wps_method,
4309 int go_intent, const u8 *own_interface_addr,
Dmitry Shmidt04949592012-07-19 12:16:46 -07004310 unsigned int force_freq, int persistent_group,
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08004311 struct wpa_ssid *ssid, unsigned int pref_freq)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004312{
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004313 if (persistent_group && wpa_s->conf->persistent_reconnect)
4314 persistent_group = 2;
4315
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004316 /*
4317 * Increase GO config timeout if HT40 is used since it takes some time
4318 * to scan channels for coex purposes before the BSS can be started.
4319 */
4320 p2p_set_config_timeout(wpa_s->global->p2p,
4321 wpa_s->p2p_go_ht40 ? 255 : 100, 20);
4322
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004323 return p2p_connect(wpa_s->global->p2p, peer_addr, wps_method,
4324 go_intent, own_interface_addr, force_freq,
Dmitry Shmidt04949592012-07-19 12:16:46 -07004325 persistent_group, ssid ? ssid->ssid : NULL,
4326 ssid ? ssid->ssid_len : 0,
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08004327 wpa_s->p2p_pd_before_go_neg, pref_freq,
4328 wps_method == WPS_NFC ? wpa_s->p2p_oob_dev_pw_id :
4329 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004330}
4331
4332
4333static int wpas_p2p_auth_go_neg(struct wpa_supplicant *wpa_s,
4334 const u8 *peer_addr,
4335 enum p2p_wps_method wps_method,
4336 int go_intent, const u8 *own_interface_addr,
Dmitry Shmidt04949592012-07-19 12:16:46 -07004337 unsigned int force_freq, int persistent_group,
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08004338 struct wpa_ssid *ssid, unsigned int pref_freq)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004339{
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004340 if (persistent_group && wpa_s->conf->persistent_reconnect)
4341 persistent_group = 2;
4342
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004343 return p2p_authorize(wpa_s->global->p2p, peer_addr, wps_method,
4344 go_intent, own_interface_addr, force_freq,
Dmitry Shmidt04949592012-07-19 12:16:46 -07004345 persistent_group, ssid ? ssid->ssid : NULL,
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08004346 ssid ? ssid->ssid_len : 0, pref_freq,
4347 wps_method == WPS_NFC ? wpa_s->p2p_oob_dev_pw_id :
4348 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004349}
4350
4351
4352static void wpas_p2p_check_join_scan_limit(struct wpa_supplicant *wpa_s)
4353{
4354 wpa_s->p2p_join_scan_count++;
4355 wpa_printf(MSG_DEBUG, "P2P: Join scan attempt %d",
4356 wpa_s->p2p_join_scan_count);
4357 if (wpa_s->p2p_join_scan_count > P2P_MAX_JOIN_SCAN_ATTEMPTS) {
4358 wpa_printf(MSG_DEBUG, "P2P: Failed to find GO " MACSTR
4359 " for join operationg - stop join attempt",
4360 MAC2STR(wpa_s->pending_join_iface_addr));
4361 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
Dmitry Shmidt04949592012-07-19 12:16:46 -07004362 if (wpa_s->p2p_auto_pd) {
4363 wpa_s->p2p_auto_pd = 0;
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07004364 wpa_msg_global(wpa_s, MSG_INFO,
4365 P2P_EVENT_PROV_DISC_FAILURE
4366 " p2p_dev_addr=" MACSTR " status=N/A",
4367 MAC2STR(wpa_s->pending_join_dev_addr));
Dmitry Shmidt04949592012-07-19 12:16:46 -07004368 return;
4369 }
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07004370 wpa_msg_global(wpa_s->parent, MSG_INFO,
4371 P2P_EVENT_GROUP_FORMATION_FAILURE);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004372 }
4373}
4374
4375
Dmitry Shmidt04949592012-07-19 12:16:46 -07004376static int wpas_check_freq_conflict(struct wpa_supplicant *wpa_s, int freq)
4377{
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07004378 int res;
4379 unsigned int num, i;
4380 struct wpa_used_freq_data *freqs;
Dmitry Shmidt04949592012-07-19 12:16:46 -07004381
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07004382 if (wpas_p2p_num_unused_channels(wpa_s) > 0) {
4383 /* Multiple channels are supported and not all are in use */
Dmitry Shmidt04949592012-07-19 12:16:46 -07004384 return 0;
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07004385 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07004386
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07004387 freqs = os_calloc(wpa_s->num_multichan_concurrent,
4388 sizeof(struct wpa_used_freq_data));
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07004389 if (!freqs)
4390 return 1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07004391
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07004392 num = wpas_p2p_valid_oper_freqs(wpa_s, freqs,
4393 wpa_s->num_multichan_concurrent);
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07004394
4395 for (i = 0; i < num; i++) {
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07004396 if (freqs[i].freq == freq) {
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07004397 wpa_printf(MSG_DEBUG, "P2P: Frequency %d MHz in use by another virtual interface and can be used",
4398 freq);
4399 res = 0;
4400 goto exit_free;
Dmitry Shmidt04949592012-07-19 12:16:46 -07004401 }
4402 }
4403
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07004404 wpa_printf(MSG_DEBUG, "P2P: No valid operating frequencies");
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07004405 res = 1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07004406
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07004407exit_free:
4408 os_free(freqs);
4409 return res;
Dmitry Shmidt04949592012-07-19 12:16:46 -07004410}
4411
4412
4413static int wpas_p2p_peer_go(struct wpa_supplicant *wpa_s,
4414 const u8 *peer_dev_addr)
4415{
4416 struct wpa_bss *bss;
4417 int updated;
4418
4419 bss = wpa_bss_get_p2p_dev_addr(wpa_s, peer_dev_addr);
4420 if (bss == NULL)
4421 return -1;
4422 if (bss->last_update_idx < wpa_s->bss_update_idx) {
4423 wpa_printf(MSG_DEBUG, "P2P: Peer BSS entry not updated in the "
4424 "last scan");
4425 return 0;
4426 }
4427
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08004428 updated = os_reltime_before(&wpa_s->p2p_auto_started,
4429 &bss->last_update);
Dmitry Shmidt04949592012-07-19 12:16:46 -07004430 wpa_printf(MSG_DEBUG, "P2P: Current BSS entry for peer updated at "
4431 "%ld.%06ld (%supdated in last scan)",
4432 bss->last_update.sec, bss->last_update.usec,
4433 updated ? "": "not ");
4434
4435 return updated;
4436}
4437
4438
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004439static void wpas_p2p_scan_res_join(struct wpa_supplicant *wpa_s,
4440 struct wpa_scan_results *scan_res)
4441{
Dmitry Shmidt344abd32014-01-14 13:17:00 -08004442 struct wpa_bss *bss = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004443 int freq;
4444 u8 iface_addr[ETH_ALEN];
Dmitry Shmidt04949592012-07-19 12:16:46 -07004445
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004446 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
4447
4448 if (wpa_s->global->p2p_disabled)
4449 return;
4450
Dmitry Shmidt04949592012-07-19 12:16:46 -07004451 wpa_printf(MSG_DEBUG, "P2P: Scan results received (%d BSS) for %sjoin",
4452 scan_res ? (int) scan_res->num : -1,
4453 wpa_s->p2p_auto_join ? "auto_" : "");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004454
4455 if (scan_res)
4456 wpas_p2p_scan_res_handler(wpa_s, scan_res);
4457
Dmitry Shmidt04949592012-07-19 12:16:46 -07004458 if (wpa_s->p2p_auto_pd) {
4459 int join = wpas_p2p_peer_go(wpa_s,
4460 wpa_s->pending_join_dev_addr);
4461 if (join == 0 &&
4462 wpa_s->auto_pd_scan_retry < P2P_AUTO_PD_SCAN_ATTEMPTS) {
4463 wpa_s->auto_pd_scan_retry++;
Dmitry Shmidt444d5672013-04-01 13:08:44 -07004464 bss = wpa_bss_get_bssid_latest(
4465 wpa_s, wpa_s->pending_join_dev_addr);
Dmitry Shmidt04949592012-07-19 12:16:46 -07004466 if (bss) {
4467 freq = bss->freq;
4468 wpa_printf(MSG_DEBUG, "P2P: Scan retry %d for "
4469 "the peer " MACSTR " at %d MHz",
4470 wpa_s->auto_pd_scan_retry,
4471 MAC2STR(wpa_s->
4472 pending_join_dev_addr),
4473 freq);
Dmitry Shmidt344abd32014-01-14 13:17:00 -08004474 wpas_p2p_join_scan_req(wpa_s, freq, NULL, 0);
Dmitry Shmidt04949592012-07-19 12:16:46 -07004475 return;
4476 }
4477 }
4478
4479 if (join < 0)
4480 join = 0;
4481
4482 wpa_s->p2p_auto_pd = 0;
4483 wpa_s->pending_pd_use = join ? AUTO_PD_JOIN : AUTO_PD_GO_NEG;
4484 wpa_printf(MSG_DEBUG, "P2P: Auto PD with " MACSTR " join=%d",
4485 MAC2STR(wpa_s->pending_join_dev_addr), join);
4486 if (p2p_prov_disc_req(wpa_s->global->p2p,
4487 wpa_s->pending_join_dev_addr,
4488 wpa_s->pending_pd_config_methods, join,
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08004489 0, wpa_s->user_initiated_pd) < 0) {
Dmitry Shmidt04949592012-07-19 12:16:46 -07004490 wpa_s->p2p_auto_pd = 0;
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07004491 wpa_msg_global(wpa_s, MSG_INFO,
4492 P2P_EVENT_PROV_DISC_FAILURE
4493 " p2p_dev_addr=" MACSTR " status=N/A",
4494 MAC2STR(wpa_s->pending_join_dev_addr));
Dmitry Shmidt04949592012-07-19 12:16:46 -07004495 }
4496 return;
4497 }
4498
4499 if (wpa_s->p2p_auto_join) {
4500 int join = wpas_p2p_peer_go(wpa_s,
4501 wpa_s->pending_join_dev_addr);
4502 if (join < 0) {
4503 wpa_printf(MSG_DEBUG, "P2P: Peer was not found to be "
4504 "running a GO -> use GO Negotiation");
4505 wpas_p2p_connect(wpa_s, wpa_s->pending_join_dev_addr,
4506 wpa_s->p2p_pin, wpa_s->p2p_wps_method,
4507 wpa_s->p2p_persistent_group, 0, 0, 0,
4508 wpa_s->p2p_go_intent,
4509 wpa_s->p2p_connect_freq,
4510 wpa_s->p2p_persistent_id,
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004511 wpa_s->p2p_pd_before_go_neg,
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004512 wpa_s->p2p_go_ht40,
4513 wpa_s->p2p_go_vht);
Dmitry Shmidt04949592012-07-19 12:16:46 -07004514 return;
4515 }
4516
4517 wpa_printf(MSG_DEBUG, "P2P: Peer was found running GO%s -> "
4518 "try to join the group", join ? "" :
4519 " in older scan");
4520 if (!join)
4521 wpa_s->p2p_fallback_to_go_neg = 1;
4522 }
4523
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004524 freq = p2p_get_oper_freq(wpa_s->global->p2p,
4525 wpa_s->pending_join_iface_addr);
4526 if (freq < 0 &&
4527 p2p_get_interface_addr(wpa_s->global->p2p,
4528 wpa_s->pending_join_dev_addr,
4529 iface_addr) == 0 &&
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07004530 os_memcmp(iface_addr, wpa_s->pending_join_dev_addr, ETH_ALEN) != 0
4531 && !wpa_bss_get_bssid(wpa_s, wpa_s->pending_join_iface_addr)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004532 wpa_printf(MSG_DEBUG, "P2P: Overwrite pending interface "
4533 "address for join from " MACSTR " to " MACSTR
4534 " based on newly discovered P2P peer entry",
4535 MAC2STR(wpa_s->pending_join_iface_addr),
4536 MAC2STR(iface_addr));
4537 os_memcpy(wpa_s->pending_join_iface_addr, iface_addr,
4538 ETH_ALEN);
4539
4540 freq = p2p_get_oper_freq(wpa_s->global->p2p,
4541 wpa_s->pending_join_iface_addr);
4542 }
4543 if (freq >= 0) {
4544 wpa_printf(MSG_DEBUG, "P2P: Target GO operating frequency "
4545 "from P2P peer table: %d MHz", freq);
4546 }
Dmitry Shmidt344abd32014-01-14 13:17:00 -08004547 if (wpa_s->p2p_join_ssid_len) {
4548 wpa_printf(MSG_DEBUG, "P2P: Trying to find target GO BSS entry based on BSSID "
4549 MACSTR " and SSID %s",
4550 MAC2STR(wpa_s->pending_join_iface_addr),
4551 wpa_ssid_txt(wpa_s->p2p_join_ssid,
4552 wpa_s->p2p_join_ssid_len));
4553 bss = wpa_bss_get(wpa_s, wpa_s->pending_join_iface_addr,
4554 wpa_s->p2p_join_ssid,
4555 wpa_s->p2p_join_ssid_len);
4556 }
4557 if (!bss) {
4558 wpa_printf(MSG_DEBUG, "P2P: Trying to find target GO BSS entry based on BSSID "
4559 MACSTR, MAC2STR(wpa_s->pending_join_iface_addr));
4560 bss = wpa_bss_get_bssid_latest(wpa_s,
4561 wpa_s->pending_join_iface_addr);
4562 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004563 if (bss) {
4564 freq = bss->freq;
4565 wpa_printf(MSG_DEBUG, "P2P: Target GO operating frequency "
Dmitry Shmidt051af732013-10-22 13:52:46 -07004566 "from BSS table: %d MHz (SSID %s)", freq,
4567 wpa_ssid_txt(bss->ssid, bss->ssid_len));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004568 }
4569 if (freq > 0) {
4570 u16 method;
4571
Dmitry Shmidt04949592012-07-19 12:16:46 -07004572 if (wpas_check_freq_conflict(wpa_s, freq) > 0) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07004573 wpa_msg_global(wpa_s->parent, MSG_INFO,
4574 P2P_EVENT_GROUP_FORMATION_FAILURE
4575 "reason=FREQ_CONFLICT");
Dmitry Shmidt04949592012-07-19 12:16:46 -07004576 return;
4577 }
4578
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004579 wpa_printf(MSG_DEBUG, "P2P: Send Provision Discovery Request "
4580 "prior to joining an existing group (GO " MACSTR
4581 " freq=%u MHz)",
4582 MAC2STR(wpa_s->pending_join_dev_addr), freq);
4583 wpa_s->pending_pd_before_join = 1;
4584
4585 switch (wpa_s->pending_join_wps_method) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004586 case WPS_PIN_DISPLAY:
4587 method = WPS_CONFIG_KEYPAD;
4588 break;
4589 case WPS_PIN_KEYPAD:
4590 method = WPS_CONFIG_DISPLAY;
4591 break;
4592 case WPS_PBC:
4593 method = WPS_CONFIG_PUSHBUTTON;
4594 break;
4595 default:
4596 method = 0;
4597 break;
4598 }
4599
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004600 if ((p2p_get_provisioning_info(wpa_s->global->p2p,
4601 wpa_s->pending_join_dev_addr) ==
4602 method)) {
4603 /*
4604 * We have already performed provision discovery for
4605 * joining the group. Proceed directly to join
4606 * operation without duplicated provision discovery. */
4607 wpa_printf(MSG_DEBUG, "P2P: Provision discovery "
4608 "with " MACSTR " already done - proceed to "
4609 "join",
4610 MAC2STR(wpa_s->pending_join_dev_addr));
4611 wpa_s->pending_pd_before_join = 0;
4612 goto start;
4613 }
4614
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004615 if (p2p_prov_disc_req(wpa_s->global->p2p,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004616 wpa_s->pending_join_dev_addr, method, 1,
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08004617 freq, wpa_s->user_initiated_pd) < 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004618 wpa_printf(MSG_DEBUG, "P2P: Failed to send Provision "
4619 "Discovery Request before joining an "
4620 "existing group");
4621 wpa_s->pending_pd_before_join = 0;
4622 goto start;
4623 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004624 return;
4625 }
4626
4627 wpa_printf(MSG_DEBUG, "P2P: Failed to find BSS/GO - try again later");
4628 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
4629 eloop_register_timeout(1, 0, wpas_p2p_join_scan, wpa_s, NULL);
4630 wpas_p2p_check_join_scan_limit(wpa_s);
4631 return;
4632
4633start:
4634 /* Start join operation immediately */
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08004635 wpas_p2p_join_start(wpa_s, 0, NULL, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004636}
4637
4638
Dmitry Shmidt344abd32014-01-14 13:17:00 -08004639static void wpas_p2p_join_scan_req(struct wpa_supplicant *wpa_s, int freq,
4640 const u8 *ssid, size_t ssid_len)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004641{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004642 int ret;
4643 struct wpa_driver_scan_params params;
4644 struct wpabuf *wps_ie, *ies;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004645 size_t ielen;
Dmitry Shmidt04949592012-07-19 12:16:46 -07004646 int freqs[2] = { 0, 0 };
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004647
4648 os_memset(&params, 0, sizeof(params));
4649
4650 /* P2P Wildcard SSID */
4651 params.num_ssids = 1;
Dmitry Shmidt344abd32014-01-14 13:17:00 -08004652 if (ssid && ssid_len) {
4653 params.ssids[0].ssid = ssid;
4654 params.ssids[0].ssid_len = ssid_len;
4655 os_memcpy(wpa_s->p2p_join_ssid, ssid, ssid_len);
4656 wpa_s->p2p_join_ssid_len = ssid_len;
4657 } else {
4658 params.ssids[0].ssid = (u8 *) P2P_WILDCARD_SSID;
4659 params.ssids[0].ssid_len = P2P_WILDCARD_SSID_LEN;
4660 wpa_s->p2p_join_ssid_len = 0;
4661 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004662
4663 wpa_s->wps->dev.p2p = 1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07004664 wps_ie = wps_build_probe_req_ie(DEV_PW_DEFAULT, &wpa_s->wps->dev,
4665 wpa_s->wps->uuid, WPS_REQ_ENROLLEE, 0,
4666 NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004667 if (wps_ie == NULL) {
4668 wpas_p2p_scan_res_join(wpa_s, NULL);
4669 return;
4670 }
4671
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004672 ielen = p2p_scan_ie_buf_len(wpa_s->global->p2p);
4673 ies = wpabuf_alloc(wpabuf_len(wps_ie) + ielen);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004674 if (ies == NULL) {
4675 wpabuf_free(wps_ie);
4676 wpas_p2p_scan_res_join(wpa_s, NULL);
4677 return;
4678 }
4679 wpabuf_put_buf(ies, wps_ie);
4680 wpabuf_free(wps_ie);
4681
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08004682 p2p_scan_ie(wpa_s->global->p2p, ies, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004683
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004684 params.p2p_probe = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004685 params.extra_ies = wpabuf_head(ies);
4686 params.extra_ies_len = wpabuf_len(ies);
Dmitry Shmidt9e3f8ee2014-01-17 10:52:01 -08004687
4688 if (!freq) {
4689 int oper_freq;
4690 /*
4691 * If freq is not provided, check the operating freq of the GO
4692 * and use a single channel scan on if possible.
4693 */
4694 oper_freq = p2p_get_oper_freq(wpa_s->global->p2p,
4695 wpa_s->pending_join_iface_addr);
4696 if (oper_freq > 0)
4697 freq = oper_freq;
4698 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07004699 if (freq > 0) {
4700 freqs[0] = freq;
4701 params.freqs = freqs;
4702 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004703
4704 /*
4705 * Run a scan to update BSS table and start Provision Discovery once
4706 * the new scan results become available.
4707 */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004708 ret = wpa_drv_scan(wpa_s, &params);
Dmitry Shmidt444d5672013-04-01 13:08:44 -07004709 if (!ret) {
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08004710 os_get_reltime(&wpa_s->scan_trigger_time);
Dmitry Shmidt04949592012-07-19 12:16:46 -07004711 wpa_s->scan_res_handler = wpas_p2p_scan_res_join;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08004712 wpa_s->own_scan_requested = 1;
Dmitry Shmidt444d5672013-04-01 13:08:44 -07004713 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004714
4715 wpabuf_free(ies);
4716
4717 if (ret) {
4718 wpa_printf(MSG_DEBUG, "P2P: Failed to start scan for join - "
4719 "try again later");
4720 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
4721 eloop_register_timeout(1, 0, wpas_p2p_join_scan, wpa_s, NULL);
4722 wpas_p2p_check_join_scan_limit(wpa_s);
4723 }
4724}
4725
4726
Dmitry Shmidt04949592012-07-19 12:16:46 -07004727static void wpas_p2p_join_scan(void *eloop_ctx, void *timeout_ctx)
4728{
4729 struct wpa_supplicant *wpa_s = eloop_ctx;
Dmitry Shmidt344abd32014-01-14 13:17:00 -08004730 wpas_p2p_join_scan_req(wpa_s, 0, NULL, 0);
Dmitry Shmidt04949592012-07-19 12:16:46 -07004731}
4732
4733
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004734static int wpas_p2p_join(struct wpa_supplicant *wpa_s, const u8 *iface_addr,
Dmitry Shmidt04949592012-07-19 12:16:46 -07004735 const u8 *dev_addr, enum p2p_wps_method wps_method,
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08004736 int auto_join, int op_freq,
4737 const u8 *ssid, size_t ssid_len)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004738{
4739 wpa_printf(MSG_DEBUG, "P2P: Request to join existing group (iface "
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08004740 MACSTR " dev " MACSTR " op_freq=%d)%s",
4741 MAC2STR(iface_addr), MAC2STR(dev_addr), op_freq,
Dmitry Shmidt04949592012-07-19 12:16:46 -07004742 auto_join ? " (auto_join)" : "");
Dmitry Shmidt344abd32014-01-14 13:17:00 -08004743 if (ssid && ssid_len) {
4744 wpa_printf(MSG_DEBUG, "P2P: Group SSID specified: %s",
4745 wpa_ssid_txt(ssid, ssid_len));
4746 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004747
Dmitry Shmidt04949592012-07-19 12:16:46 -07004748 wpa_s->p2p_auto_pd = 0;
4749 wpa_s->p2p_auto_join = !!auto_join;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004750 os_memcpy(wpa_s->pending_join_iface_addr, iface_addr, ETH_ALEN);
4751 os_memcpy(wpa_s->pending_join_dev_addr, dev_addr, ETH_ALEN);
4752 wpa_s->pending_join_wps_method = wps_method;
4753
4754 /* Make sure we are not running find during connection establishment */
4755 wpas_p2p_stop_find(wpa_s);
4756
4757 wpa_s->p2p_join_scan_count = 0;
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08004758 wpas_p2p_join_scan_req(wpa_s, op_freq, ssid, ssid_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004759 return 0;
4760}
4761
4762
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08004763static int wpas_p2p_join_start(struct wpa_supplicant *wpa_s, int freq,
4764 const u8 *ssid, size_t ssid_len)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004765{
4766 struct wpa_supplicant *group;
4767 struct p2p_go_neg_results res;
Dmitry Shmidt04949592012-07-19 12:16:46 -07004768 struct wpa_bss *bss;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004769
4770 group = wpas_p2p_get_group_iface(wpa_s, 0, 0);
4771 if (group == NULL)
4772 return -1;
4773 if (group != wpa_s) {
4774 os_memcpy(group->p2p_pin, wpa_s->p2p_pin,
4775 sizeof(group->p2p_pin));
4776 group->p2p_wps_method = wpa_s->p2p_wps_method;
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08004777 } else {
4778 /*
4779 * Need to mark the current interface for p2p_group_formation
4780 * when a separate group interface is not used. This is needed
4781 * to allow p2p_cancel stop a pending p2p_connect-join.
4782 * wpas_p2p_init_group_interface() addresses this for the case
4783 * where a separate group interface is used.
4784 */
4785 wpa_s->global->p2p_group_formation = wpa_s;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004786 }
4787
4788 group->p2p_in_provisioning = 1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07004789 group->p2p_fallback_to_go_neg = wpa_s->p2p_fallback_to_go_neg;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004790
4791 os_memset(&res, 0, sizeof(res));
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08004792 os_memcpy(res.peer_device_addr, wpa_s->pending_join_dev_addr, ETH_ALEN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004793 os_memcpy(res.peer_interface_addr, wpa_s->pending_join_iface_addr,
4794 ETH_ALEN);
4795 res.wps_method = wpa_s->pending_join_wps_method;
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08004796 if (freq && ssid && ssid_len) {
4797 res.freq = freq;
4798 res.ssid_len = ssid_len;
4799 os_memcpy(res.ssid, ssid, ssid_len);
4800 } else {
4801 bss = wpa_bss_get_bssid_latest(wpa_s,
4802 wpa_s->pending_join_iface_addr);
4803 if (bss) {
4804 res.freq = bss->freq;
4805 res.ssid_len = bss->ssid_len;
4806 os_memcpy(res.ssid, bss->ssid, bss->ssid_len);
4807 wpa_printf(MSG_DEBUG, "P2P: Join target GO operating frequency from BSS table: %d MHz (SSID %s)",
4808 bss->freq,
4809 wpa_ssid_txt(bss->ssid, bss->ssid_len));
4810 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07004811 }
4812
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08004813 if (wpa_s->off_channel_freq || wpa_s->roc_waiting_drv_freq) {
4814 wpa_printf(MSG_DEBUG, "P2P: Cancel remain-on-channel prior to "
4815 "starting client");
4816 wpa_drv_cancel_remain_on_channel(wpa_s);
4817 wpa_s->off_channel_freq = 0;
4818 wpa_s->roc_waiting_drv_freq = 0;
4819 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004820 wpas_start_wps_enrollee(group, &res);
4821
4822 /*
4823 * Allow a longer timeout for join-a-running-group than normal 15
4824 * second group formation timeout since the GO may not have authorized
4825 * our connection yet.
4826 */
4827 eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s, NULL);
4828 eloop_register_timeout(60, 0, wpas_p2p_group_formation_timeout,
4829 wpa_s, NULL);
4830
4831 return 0;
4832}
4833
4834
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08004835static int wpas_p2p_setup_freqs(struct wpa_supplicant *wpa_s, int freq,
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004836 int *force_freq, int *pref_freq, int go)
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08004837{
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07004838 struct wpa_used_freq_data *freqs;
4839 int res, best_freq, num_unused;
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07004840 unsigned int freq_in_use = 0, num, i;
4841
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07004842 freqs = os_calloc(wpa_s->num_multichan_concurrent,
4843 sizeof(struct wpa_used_freq_data));
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07004844 if (!freqs)
4845 return -1;
4846
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07004847 num = wpas_p2p_valid_oper_freqs(wpa_s, freqs,
4848 wpa_s->num_multichan_concurrent);
4849
4850 /*
4851 * It is possible that the total number of used frequencies is bigger
4852 * than the number of frequencies used for P2P, so get the system wide
4853 * number of unused frequencies.
4854 */
4855 num_unused = wpas_p2p_num_unused_channels(wpa_s);
4856
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07004857 wpa_printf(MSG_DEBUG,
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07004858 "P2P: Setup freqs: freq=%d num_MCC=%d shared_freqs=%u num_unused=%d",
4859 freq, wpa_s->num_multichan_concurrent, num, num_unused);
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07004860
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08004861 if (freq > 0) {
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004862 int ret;
4863 if (go)
4864 ret = p2p_supported_freq(wpa_s->global->p2p, freq);
4865 else
4866 ret = p2p_supported_freq_cli(wpa_s->global->p2p, freq);
4867 if (!ret) {
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08004868 wpa_printf(MSG_DEBUG, "P2P: The forced channel "
4869 "(%u MHz) is not supported for P2P uses",
4870 freq);
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07004871 res = -3;
4872 goto exit_free;
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08004873 }
4874
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07004875 for (i = 0; i < num; i++) {
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07004876 if (freqs[i].freq == freq)
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07004877 freq_in_use = 1;
4878 }
4879
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07004880 if (num_unused <= 0 && !freq_in_use) {
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07004881 wpa_printf(MSG_DEBUG, "P2P: Cannot start P2P group on %u MHz as there are no available channels",
4882 freq);
4883 res = -2;
4884 goto exit_free;
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08004885 }
4886 wpa_printf(MSG_DEBUG, "P2P: Trying to force us to use the "
4887 "requested channel (%u MHz)", freq);
4888 *force_freq = freq;
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07004889 goto exit_ok;
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08004890 }
4891
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07004892 best_freq = wpas_p2p_pick_best_used_freq(wpa_s, freqs, num);
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07004893
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07004894 /* We have a candidate frequency to use */
4895 if (best_freq > 0) {
4896 if (*pref_freq == 0 && num_unused > 0) {
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08004897 wpa_printf(MSG_DEBUG, "P2P: Try to prefer a frequency (%u MHz) we are already using",
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07004898 best_freq);
4899 *pref_freq = best_freq;
Dmitry Shmidt51a47d52013-09-10 10:52:57 -07004900 } else {
4901 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 -07004902 best_freq);
4903 *force_freq = best_freq;
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07004904 }
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07004905 } else if (num_unused > 0) {
4906 wpa_printf(MSG_DEBUG,
4907 "P2P: Current operating channels are not available for P2P. Try to use another channel");
4908 *force_freq = 0;
4909 } else {
4910 wpa_printf(MSG_DEBUG,
4911 "P2P: All channels are in use and none of them are P2P enabled. Cannot start P2P group");
4912 res = -2;
4913 goto exit_free;
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07004914 }
4915
4916exit_ok:
4917 res = 0;
4918exit_free:
4919 os_free(freqs);
4920 return res;
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08004921}
4922
4923
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004924/**
4925 * wpas_p2p_connect - Request P2P Group Formation to be started
4926 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
4927 * @peer_addr: Address of the peer P2P Device
4928 * @pin: PIN to use during provisioning or %NULL to indicate PBC mode
4929 * @persistent_group: Whether to create a persistent group
Dmitry Shmidt04949592012-07-19 12:16:46 -07004930 * @auto_join: Whether to select join vs. GO Negotiation automatically
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004931 * @join: Whether to join an existing group (as a client) instead of starting
4932 * Group Owner negotiation; @peer_addr is BSSID in that case
4933 * @auth: Whether to only authorize the connection instead of doing that and
4934 * initiating Group Owner negotiation
4935 * @go_intent: GO Intent or -1 to use default
4936 * @freq: Frequency for the group or 0 for auto-selection
Dmitry Shmidt04949592012-07-19 12:16:46 -07004937 * @persistent_id: Persistent group credentials to use for forcing GO
4938 * parameters or -1 to generate new values (SSID/passphrase)
4939 * @pd: Whether to send Provision Discovery prior to GO Negotiation as an
4940 * interoperability workaround when initiating group formation
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004941 * @ht40: Start GO with 40 MHz channel width
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004942 * @vht: Start GO with VHT support
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004943 * Returns: 0 or new PIN (if pin was %NULL) on success, -1 on unspecified
4944 * failure, -2 on failure due to channel not currently available,
4945 * -3 if forced channel is not supported
4946 */
4947int wpas_p2p_connect(struct wpa_supplicant *wpa_s, const u8 *peer_addr,
4948 const char *pin, enum p2p_wps_method wps_method,
Dmitry Shmidt04949592012-07-19 12:16:46 -07004949 int persistent_group, int auto_join, int join, int auth,
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004950 int go_intent, int freq, int persistent_id, int pd,
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004951 int ht40, int vht)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004952{
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07004953 int force_freq = 0, pref_freq = 0;
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08004954 int ret = 0, res;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004955 enum wpa_driver_if_type iftype;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004956 const u8 *if_addr;
Dmitry Shmidt04949592012-07-19 12:16:46 -07004957 struct wpa_ssid *ssid = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004958
4959 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
4960 return -1;
4961
Dmitry Shmidt04949592012-07-19 12:16:46 -07004962 if (persistent_id >= 0) {
4963 ssid = wpa_config_get_network(wpa_s->conf, persistent_id);
4964 if (ssid == NULL || ssid->disabled != 2 ||
4965 ssid->mode != WPAS_MODE_P2P_GO)
4966 return -1;
4967 }
4968
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07004969 os_free(wpa_s->global->add_psk);
4970 wpa_s->global->add_psk = NULL;
4971
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08004972 wpa_s->global->p2p_fail_on_wps_complete = 0;
4973
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004974 if (go_intent < 0)
4975 go_intent = wpa_s->conf->p2p_go_intent;
4976
4977 if (!auth)
4978 wpa_s->p2p_long_listen = 0;
4979
4980 wpa_s->p2p_wps_method = wps_method;
Dmitry Shmidt04949592012-07-19 12:16:46 -07004981 wpa_s->p2p_persistent_group = !!persistent_group;
4982 wpa_s->p2p_persistent_id = persistent_id;
4983 wpa_s->p2p_go_intent = go_intent;
4984 wpa_s->p2p_connect_freq = freq;
4985 wpa_s->p2p_fallback_to_go_neg = 0;
4986 wpa_s->p2p_pd_before_go_neg = !!pd;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004987 wpa_s->p2p_go_ht40 = !!ht40;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004988 wpa_s->p2p_go_vht = !!vht;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004989
4990 if (pin)
4991 os_strlcpy(wpa_s->p2p_pin, pin, sizeof(wpa_s->p2p_pin));
4992 else if (wps_method == WPS_PIN_DISPLAY) {
4993 ret = wps_generate_pin();
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004994 res = os_snprintf(wpa_s->p2p_pin, sizeof(wpa_s->p2p_pin),
4995 "%08d", ret);
4996 if (os_snprintf_error(sizeof(wpa_s->p2p_pin), res))
4997 wpa_s->p2p_pin[sizeof(wpa_s->p2p_pin) - 1] = '\0';
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004998 wpa_printf(MSG_DEBUG, "P2P: Randomly generated PIN: %s",
4999 wpa_s->p2p_pin);
5000 } else
5001 wpa_s->p2p_pin[0] = '\0';
5002
Dmitry Shmidt04949592012-07-19 12:16:46 -07005003 if (join || auto_join) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005004 u8 iface_addr[ETH_ALEN], dev_addr[ETH_ALEN];
5005 if (auth) {
5006 wpa_printf(MSG_DEBUG, "P2P: Authorize invitation to "
5007 "connect a running group from " MACSTR,
5008 MAC2STR(peer_addr));
5009 os_memcpy(wpa_s->p2p_auth_invite, peer_addr, ETH_ALEN);
5010 return ret;
5011 }
5012 os_memcpy(dev_addr, peer_addr, ETH_ALEN);
5013 if (p2p_get_interface_addr(wpa_s->global->p2p, peer_addr,
5014 iface_addr) < 0) {
5015 os_memcpy(iface_addr, peer_addr, ETH_ALEN);
5016 p2p_get_dev_addr(wpa_s->global->p2p, peer_addr,
5017 dev_addr);
5018 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07005019 if (auto_join) {
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005020 os_get_reltime(&wpa_s->p2p_auto_started);
Dmitry Shmidt04949592012-07-19 12:16:46 -07005021 wpa_printf(MSG_DEBUG, "P2P: Auto join started at "
5022 "%ld.%06ld",
5023 wpa_s->p2p_auto_started.sec,
5024 wpa_s->p2p_auto_started.usec);
5025 }
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08005026 wpa_s->user_initiated_pd = 1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07005027 if (wpas_p2p_join(wpa_s, iface_addr, dev_addr, wps_method,
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08005028 auto_join, freq, NULL, 0) < 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005029 return -1;
5030 return ret;
5031 }
5032
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07005033 res = wpas_p2p_setup_freqs(wpa_s, freq, &force_freq, &pref_freq,
5034 go_intent == 15);
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08005035 if (res)
5036 return res;
Dmitry Shmidta0d265f2013-11-19 13:13:41 -08005037 wpas_p2p_set_own_freq_preference(wpa_s,
5038 force_freq ? force_freq : pref_freq);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005039
5040 wpa_s->create_p2p_iface = wpas_p2p_create_iface(wpa_s);
5041
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005042 if (wpa_s->create_p2p_iface) {
5043 /* Prepare to add a new interface for the group */
5044 iftype = WPA_IF_P2P_GROUP;
5045 if (go_intent == 15)
5046 iftype = WPA_IF_P2P_GO;
5047 if (wpas_p2p_add_group_interface(wpa_s, iftype) < 0) {
5048 wpa_printf(MSG_ERROR, "P2P: Failed to allocate a new "
5049 "interface for the group");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005050 return -1;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005051 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005052
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005053 if_addr = wpa_s->pending_interface_addr;
5054 } else
5055 if_addr = wpa_s->own_addr;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005056
5057 if (auth) {
5058 if (wpas_p2p_auth_go_neg(wpa_s, peer_addr, wps_method,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005059 go_intent, if_addr,
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08005060 force_freq, persistent_group, ssid,
5061 pref_freq) < 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005062 return -1;
5063 return ret;
5064 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005065
5066 if (wpas_p2p_start_go_neg(wpa_s, peer_addr, wps_method,
5067 go_intent, if_addr, force_freq,
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08005068 persistent_group, ssid, pref_freq) < 0) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005069 if (wpa_s->create_p2p_iface)
5070 wpas_p2p_remove_pending_group_interface(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005071 return -1;
5072 }
5073 return ret;
5074}
5075
5076
5077/**
5078 * wpas_p2p_remain_on_channel_cb - Indication of remain-on-channel start
5079 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
5080 * @freq: Frequency of the channel in MHz
5081 * @duration: Duration of the stay on the channel in milliseconds
5082 *
5083 * This callback is called when the driver indicates that it has started the
5084 * requested remain-on-channel duration.
5085 */
5086void wpas_p2p_remain_on_channel_cb(struct wpa_supplicant *wpa_s,
5087 unsigned int freq, unsigned int duration)
5088{
5089 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
5090 return;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005091 if (wpa_s->off_channel_freq == wpa_s->pending_listen_freq) {
5092 p2p_listen_cb(wpa_s->global->p2p, wpa_s->pending_listen_freq,
5093 wpa_s->pending_listen_duration);
5094 wpa_s->pending_listen_freq = 0;
Dmitry Shmidt7f93d6f2014-02-21 11:22:49 -08005095 } else {
5096 wpa_printf(MSG_DEBUG, "P2P: Ignore remain-on-channel callback (off_channel_freq=%u pending_listen_freq=%d freq=%u duration=%u)",
5097 wpa_s->off_channel_freq, wpa_s->pending_listen_freq,
5098 freq, duration);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005099 }
5100}
5101
5102
Jithu Jance57cea1a2014-08-20 10:22:04 -07005103int wpas_p2p_listen_start(struct wpa_supplicant *wpa_s, unsigned int timeout)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005104{
5105 /* Limit maximum Listen state time based on driver limitation. */
5106 if (timeout > wpa_s->max_remain_on_chan)
5107 timeout = wpa_s->max_remain_on_chan;
5108
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005109 return p2p_listen(wpa_s->global->p2p, timeout);
5110}
5111
5112
5113/**
5114 * wpas_p2p_cancel_remain_on_channel_cb - Remain-on-channel timeout
5115 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
5116 * @freq: Frequency of the channel in MHz
5117 *
5118 * This callback is called when the driver indicates that a remain-on-channel
5119 * operation has been completed, i.e., the duration on the requested channel
5120 * has timed out.
5121 */
5122void wpas_p2p_cancel_remain_on_channel_cb(struct wpa_supplicant *wpa_s,
5123 unsigned int freq)
5124{
5125 wpa_printf(MSG_DEBUG, "P2P: Cancel remain-on-channel callback "
5126 "(p2p_long_listen=%d ms pending_action_tx=%p)",
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08005127 wpa_s->p2p_long_listen, offchannel_pending_action_tx(wpa_s));
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005128 wpas_p2p_listen_work_done(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005129 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
5130 return;
Jithu Jance57cea1a2014-08-20 10:22:04 -07005131 if (wpa_s->p2p_long_listen > 0)
5132 wpa_s->p2p_long_listen -= wpa_s->max_remain_on_chan;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005133 if (p2p_listen_end(wpa_s->global->p2p, freq) > 0)
5134 return; /* P2P module started a new operation */
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08005135 if (offchannel_pending_action_tx(wpa_s))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005136 return;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005137 if (wpa_s->p2p_long_listen > 0) {
5138 wpa_printf(MSG_DEBUG, "P2P: Continuing long Listen state");
5139 wpas_p2p_listen_start(wpa_s, wpa_s->p2p_long_listen);
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07005140 } else {
5141 /*
5142 * When listen duration is over, stop listen & update p2p_state
5143 * to IDLE.
5144 */
5145 p2p_stop_listen(wpa_s->global->p2p);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005146 }
5147}
5148
5149
5150/**
5151 * wpas_p2p_group_remove - Remove a P2P group
5152 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
5153 * @ifname: Network interface name of the group interface or "*" to remove all
5154 * groups
5155 * Returns: 0 on success, -1 on failure
5156 *
5157 * This function is used to remove a P2P group. This can be used to disconnect
5158 * from a group in which the local end is a P2P Client or to end a P2P Group in
5159 * case the local end is the Group Owner. If a virtual network interface was
5160 * created for this group, that interface will be removed. Otherwise, only the
5161 * configured P2P group network will be removed from the interface.
5162 */
5163int wpas_p2p_group_remove(struct wpa_supplicant *wpa_s, const char *ifname)
5164{
5165 struct wpa_global *global = wpa_s->global;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005166 struct wpa_supplicant *calling_wpa_s = wpa_s;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005167
5168 if (os_strcmp(ifname, "*") == 0) {
5169 struct wpa_supplicant *prev;
5170 wpa_s = global->ifaces;
5171 while (wpa_s) {
5172 prev = wpa_s;
5173 wpa_s = wpa_s->next;
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07005174 if (prev->p2p_group_interface !=
5175 NOT_P2P_GROUP_INTERFACE ||
5176 (prev->current_ssid &&
5177 prev->current_ssid->p2p_group))
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005178 wpas_p2p_disconnect_safely(prev, calling_wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005179 }
5180 return 0;
5181 }
5182
5183 for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
5184 if (os_strcmp(wpa_s->ifname, ifname) == 0)
5185 break;
5186 }
5187
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005188 return wpas_p2p_disconnect_safely(wpa_s, calling_wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005189}
5190
5191
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07005192static int wpas_p2p_select_go_freq(struct wpa_supplicant *wpa_s, int freq)
5193{
5194 unsigned int r;
5195
5196 if (freq == 2) {
5197 wpa_printf(MSG_DEBUG, "P2P: Request to start GO on 2.4 GHz "
5198 "band");
5199 if (wpa_s->best_24_freq > 0 &&
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07005200 p2p_supported_freq_go(wpa_s->global->p2p,
5201 wpa_s->best_24_freq)) {
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07005202 freq = wpa_s->best_24_freq;
5203 wpa_printf(MSG_DEBUG, "P2P: Use best 2.4 GHz band "
5204 "channel: %d MHz", freq);
5205 } else {
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07005206 if (os_get_random((u8 *) &r, sizeof(r)) < 0)
5207 return -1;
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07005208 freq = 2412 + (r % 3) * 25;
5209 wpa_printf(MSG_DEBUG, "P2P: Use random 2.4 GHz band "
5210 "channel: %d MHz", freq);
5211 }
5212 }
5213
5214 if (freq == 5) {
5215 wpa_printf(MSG_DEBUG, "P2P: Request to start GO on 5 GHz "
5216 "band");
5217 if (wpa_s->best_5_freq > 0 &&
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07005218 p2p_supported_freq_go(wpa_s->global->p2p,
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07005219 wpa_s->best_5_freq)) {
5220 freq = wpa_s->best_5_freq;
5221 wpa_printf(MSG_DEBUG, "P2P: Use best 5 GHz band "
5222 "channel: %d MHz", freq);
5223 } else {
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07005224 if (os_get_random((u8 *) &r, sizeof(r)) < 0)
5225 return -1;
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07005226 freq = 5180 + (r % 4) * 20;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07005227 if (!p2p_supported_freq_go(wpa_s->global->p2p, freq)) {
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07005228 wpa_printf(MSG_DEBUG, "P2P: Could not select "
5229 "5 GHz channel for P2P group");
5230 return -1;
5231 }
5232 wpa_printf(MSG_DEBUG, "P2P: Use random 5 GHz band "
5233 "channel: %d MHz", freq);
5234 }
5235 }
5236
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07005237 if (freq > 0 && !p2p_supported_freq_go(wpa_s->global->p2p, freq)) {
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07005238 wpa_printf(MSG_DEBUG, "P2P: The forced channel for GO "
5239 "(%u MHz) is not supported for P2P uses",
5240 freq);
5241 return -1;
5242 }
5243
5244 return freq;
5245}
5246
5247
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07005248static int wpas_p2p_select_freq_no_pref(struct wpa_supplicant *wpa_s,
5249 struct p2p_go_neg_results *params,
5250 const struct p2p_channels *channels)
5251{
5252 unsigned int i, r;
5253
5254 /* first try some random selection of the social channels */
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07005255 if (os_get_random((u8 *) &r, sizeof(r)) < 0)
5256 return -1;
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07005257
5258 for (i = 0; i < 3; i++) {
5259 params->freq = 2412 + ((r + i) % 3) * 25;
5260 if (!wpas_p2p_disallowed_freq(wpa_s->global, params->freq) &&
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07005261 freq_included(channels, params->freq) &&
5262 p2p_supported_freq(wpa_s->global->p2p, params->freq))
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07005263 goto out;
5264 }
5265
5266 /* try all channels in reg. class 81 */
5267 for (i = 0; i < 11; i++) {
5268 params->freq = 2412 + i * 5;
5269 if (!wpas_p2p_disallowed_freq(wpa_s->global, params->freq) &&
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07005270 freq_included(channels, params->freq) &&
5271 p2p_supported_freq(wpa_s->global->p2p, params->freq))
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07005272 goto out;
5273 }
5274
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07005275 /* try social channel class 180 channel 2 */
5276 params->freq = 58320 + 1 * 2160;
5277 if (!wpas_p2p_disallowed_freq(wpa_s->global, params->freq) &&
5278 freq_included(channels, params->freq) &&
5279 p2p_supported_freq(wpa_s->global->p2p, params->freq))
5280 goto out;
5281
5282 /* try all channels in reg. class 180 */
5283 for (i = 0; i < 4; i++) {
5284 params->freq = 58320 + i * 2160;
5285 if (!wpas_p2p_disallowed_freq(wpa_s->global, params->freq) &&
5286 freq_included(channels, params->freq) &&
5287 p2p_supported_freq(wpa_s->global->p2p, params->freq))
5288 goto out;
5289 }
5290
5291 wpa_printf(MSG_DEBUG, "P2P: No 2.4 and 60 GHz channel allowed");
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07005292 return -1;
5293out:
5294 wpa_printf(MSG_DEBUG, "P2P: Set GO freq %d MHz (no preference known)",
5295 params->freq);
5296 return 0;
5297}
5298
5299
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005300static int wpas_p2p_init_go_params(struct wpa_supplicant *wpa_s,
5301 struct p2p_go_neg_results *params,
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07005302 int freq, int ht40, int vht,
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08005303 const struct p2p_channels *channels)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005304{
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07005305 struct wpa_used_freq_data *freqs;
5306 unsigned int pref_freq, cand_freq;
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07005307 unsigned int num, i;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005308
5309 os_memset(params, 0, sizeof(*params));
5310 params->role_go = 1;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005311 params->ht40 = ht40;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07005312 params->vht = vht;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005313 if (freq) {
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08005314 if (!freq_included(channels, freq)) {
5315 wpa_printf(MSG_DEBUG, "P2P: Forced GO freq %d MHz not "
5316 "accepted", freq);
5317 return -1;
5318 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005319 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on forced "
5320 "frequency %d MHz", freq);
5321 params->freq = freq;
5322 } else if (wpa_s->conf->p2p_oper_reg_class == 81 &&
5323 wpa_s->conf->p2p_oper_channel >= 1 &&
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08005324 wpa_s->conf->p2p_oper_channel <= 11 &&
5325 freq_included(channels,
5326 2407 + 5 * wpa_s->conf->p2p_oper_channel)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005327 params->freq = 2407 + 5 * wpa_s->conf->p2p_oper_channel;
5328 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on configured "
5329 "frequency %d MHz", params->freq);
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08005330 } else if ((wpa_s->conf->p2p_oper_reg_class == 115 ||
5331 wpa_s->conf->p2p_oper_reg_class == 116 ||
5332 wpa_s->conf->p2p_oper_reg_class == 117 ||
5333 wpa_s->conf->p2p_oper_reg_class == 124 ||
5334 wpa_s->conf->p2p_oper_reg_class == 126 ||
5335 wpa_s->conf->p2p_oper_reg_class == 127) &&
5336 freq_included(channels,
5337 5000 + 5 * wpa_s->conf->p2p_oper_channel)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005338 params->freq = 5000 + 5 * wpa_s->conf->p2p_oper_channel;
5339 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on configured "
5340 "frequency %d MHz", params->freq);
5341 } else if (wpa_s->conf->p2p_oper_channel == 0 &&
5342 wpa_s->best_overall_freq > 0 &&
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07005343 p2p_supported_freq_go(wpa_s->global->p2p,
5344 wpa_s->best_overall_freq) &&
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08005345 freq_included(channels, wpa_s->best_overall_freq)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005346 params->freq = wpa_s->best_overall_freq;
5347 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on best overall "
5348 "channel %d MHz", params->freq);
5349 } else if (wpa_s->conf->p2p_oper_channel == 0 &&
5350 wpa_s->best_24_freq > 0 &&
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07005351 p2p_supported_freq_go(wpa_s->global->p2p,
5352 wpa_s->best_24_freq) &&
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08005353 freq_included(channels, wpa_s->best_24_freq)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005354 params->freq = wpa_s->best_24_freq;
5355 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on best 2.4 GHz "
5356 "channel %d MHz", params->freq);
5357 } else if (wpa_s->conf->p2p_oper_channel == 0 &&
5358 wpa_s->best_5_freq > 0 &&
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07005359 p2p_supported_freq_go(wpa_s->global->p2p,
5360 wpa_s->best_5_freq) &&
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08005361 freq_included(channels, wpa_s->best_5_freq)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005362 params->freq = wpa_s->best_5_freq;
5363 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on best 5 GHz "
5364 "channel %d MHz", params->freq);
Dmitry Shmidt44c95782013-05-17 09:51:35 -07005365 } else if ((pref_freq = p2p_get_pref_freq(wpa_s->global->p2p,
5366 channels))) {
5367 params->freq = pref_freq;
5368 wpa_printf(MSG_DEBUG, "P2P: Set GO freq %d MHz from preferred "
5369 "channels", params->freq);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005370 } else {
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07005371 /* no preference, select some channel */
5372 if (wpas_p2p_select_freq_no_pref(wpa_s, params, channels) < 0)
Dmitry Shmidt04949592012-07-19 12:16:46 -07005373 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005374 }
5375
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07005376 freqs = os_calloc(wpa_s->num_multichan_concurrent,
5377 sizeof(struct wpa_used_freq_data));
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07005378 if (!freqs)
5379 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005380
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07005381 num = wpas_p2p_valid_oper_freqs(wpa_s, freqs,
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07005382 wpa_s->num_multichan_concurrent);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005383
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07005384 cand_freq = wpas_p2p_pick_best_used_freq(wpa_s, freqs, num);
5385
5386 /* First try the best used frequency if possible */
5387 if (!freq && cand_freq > 0 && freq_included(channels, cand_freq)) {
5388 params->freq = cand_freq;
5389 } else if (!freq) {
5390 /* Try any of the used frequencies */
5391 for (i = 0; i < num; i++) {
5392 if (freq_included(channels, freqs[i].freq)) {
5393 wpa_printf(MSG_DEBUG, "P2P: Force GO on a channel we are already using (%u MHz)",
5394 freqs[i].freq);
5395 params->freq = freqs[i].freq;
5396 break;
5397 }
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07005398 }
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08005399
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07005400 if (i == num) {
5401 if (wpas_p2p_num_unused_channels(wpa_s) <= 0) {
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08005402 wpa_printf(MSG_DEBUG, "P2P: Cannot force GO on any of the channels we are already using");
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07005403 os_free(freqs);
5404 return -1;
5405 } else {
5406 wpa_printf(MSG_DEBUG, "P2P: Cannot force GO on any of the channels we are already using. Use one of the free channels");
5407 }
5408 }
5409 } else {
5410 for (i = 0; i < num; i++) {
5411 if (freqs[i].freq == freq)
5412 break;
5413 }
5414
5415 if (i == num) {
5416 if (wpas_p2p_num_unused_channels(wpa_s) <= 0) {
5417 if (freq)
5418 wpa_printf(MSG_DEBUG, "P2P: Cannot force GO on freq (%u MHz) as all the channels are in use", freq);
5419 os_free(freqs);
5420 return -1;
5421 } else {
5422 wpa_printf(MSG_DEBUG, "P2P: Use one of the free channels");
5423 }
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08005424 }
5425 }
5426
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07005427 os_free(freqs);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005428 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005429}
5430
5431
5432static struct wpa_supplicant *
5433wpas_p2p_get_group_iface(struct wpa_supplicant *wpa_s, int addr_allocated,
5434 int go)
5435{
5436 struct wpa_supplicant *group_wpa_s;
5437
Dmitry Shmidtaa532512012-09-24 10:35:31 -07005438 if (!wpas_p2p_create_iface(wpa_s)) {
5439 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Use same interface for group "
5440 "operations");
Dmitry Shmidt56052862013-10-04 10:23:25 -07005441 wpa_s->p2p_first_connection_timeout = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005442 return wpa_s;
Dmitry Shmidtaa532512012-09-24 10:35:31 -07005443 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005444
5445 if (wpas_p2p_add_group_interface(wpa_s, go ? WPA_IF_P2P_GO :
Dmitry Shmidtaa532512012-09-24 10:35:31 -07005446 WPA_IF_P2P_CLIENT) < 0) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07005447 wpa_msg_global(wpa_s, MSG_ERROR,
5448 "P2P: Failed to add group interface");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005449 return NULL;
Dmitry Shmidtaa532512012-09-24 10:35:31 -07005450 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005451 group_wpa_s = wpas_p2p_init_group_interface(wpa_s, go);
5452 if (group_wpa_s == NULL) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07005453 wpa_msg_global(wpa_s, MSG_ERROR,
5454 "P2P: Failed to initialize group interface");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005455 wpas_p2p_remove_pending_group_interface(wpa_s);
5456 return NULL;
5457 }
5458
Dmitry Shmidtaa532512012-09-24 10:35:31 -07005459 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Use separate group interface %s",
5460 group_wpa_s->ifname);
Dmitry Shmidt56052862013-10-04 10:23:25 -07005461 group_wpa_s->p2p_first_connection_timeout = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005462 return group_wpa_s;
5463}
5464
5465
5466/**
5467 * wpas_p2p_group_add - Add a new P2P group with local end as Group Owner
5468 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
5469 * @persistent_group: Whether to create a persistent group
5470 * @freq: Frequency for the group or 0 to indicate no hardcoding
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07005471 * @ht40: Start GO with 40 MHz channel width
5472 * @vht: Start GO with VHT support
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005473 * Returns: 0 on success, -1 on failure
5474 *
5475 * This function creates a new P2P group with the local end as the Group Owner,
5476 * i.e., without using Group Owner Negotiation.
5477 */
5478int wpas_p2p_group_add(struct wpa_supplicant *wpa_s, int persistent_group,
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07005479 int freq, int ht40, int vht)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005480{
5481 struct p2p_go_neg_results params;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005482
5483 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
5484 return -1;
5485
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07005486 os_free(wpa_s->global->add_psk);
5487 wpa_s->global->add_psk = NULL;
5488
Dmitry Shmidtdca39792011-09-06 11:17:33 -07005489 /* Make sure we are not running find during connection establishment */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005490 wpa_printf(MSG_DEBUG, "P2P: Stop any on-going P2P FIND");
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08005491 wpas_p2p_stop_find_oper(wpa_s);
Dmitry Shmidtdca39792011-09-06 11:17:33 -07005492
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07005493 freq = wpas_p2p_select_go_freq(wpa_s, freq);
5494 if (freq < 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005495 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005496
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07005497 if (wpas_p2p_init_go_params(wpa_s, &params, freq, ht40, vht, NULL))
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005498 return -1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07005499 if (params.freq &&
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07005500 !p2p_supported_freq_go(wpa_s->global->p2p, params.freq)) {
Dmitry Shmidt04949592012-07-19 12:16:46 -07005501 wpa_printf(MSG_DEBUG, "P2P: The selected channel for GO "
5502 "(%u MHz) is not supported for P2P uses",
5503 params.freq);
5504 return -1;
5505 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005506 p2p_go_params(wpa_s->global->p2p, &params);
5507 params.persistent_group = persistent_group;
5508
5509 wpa_s = wpas_p2p_get_group_iface(wpa_s, 0, 1);
5510 if (wpa_s == NULL)
5511 return -1;
5512 wpas_start_wps_go(wpa_s, &params, 0);
5513
5514 return 0;
5515}
5516
5517
5518static int wpas_start_p2p_client(struct wpa_supplicant *wpa_s,
Dmitry Shmidt15907092014-03-25 10:42:57 -07005519 struct wpa_ssid *params, int addr_allocated,
5520 int freq)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005521{
5522 struct wpa_ssid *ssid;
5523
5524 wpa_s = wpas_p2p_get_group_iface(wpa_s, addr_allocated, 0);
5525 if (wpa_s == NULL)
5526 return -1;
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07005527 wpa_s->p2p_last_4way_hs_fail = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005528
5529 wpa_supplicant_ap_deinit(wpa_s);
5530
5531 ssid = wpa_config_add_network(wpa_s->conf);
5532 if (ssid == NULL)
5533 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005534 wpa_config_set_network_defaults(ssid);
5535 ssid->temporary = 1;
5536 ssid->proto = WPA_PROTO_RSN;
5537 ssid->pairwise_cipher = WPA_CIPHER_CCMP;
5538 ssid->group_cipher = WPA_CIPHER_CCMP;
5539 ssid->key_mgmt = WPA_KEY_MGMT_PSK;
5540 ssid->ssid = os_malloc(params->ssid_len);
5541 if (ssid->ssid == NULL) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005542 wpa_config_remove_network(wpa_s->conf, ssid->id);
5543 return -1;
5544 }
5545 os_memcpy(ssid->ssid, params->ssid, params->ssid_len);
5546 ssid->ssid_len = params->ssid_len;
5547 ssid->p2p_group = 1;
5548 ssid->export_keys = 1;
5549 if (params->psk_set) {
5550 os_memcpy(ssid->psk, params->psk, 32);
5551 ssid->psk_set = 1;
5552 }
5553 if (params->passphrase)
5554 ssid->passphrase = os_strdup(params->passphrase);
5555
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005556 wpa_s->show_group_started = 1;
Dmitry Shmidt15907092014-03-25 10:42:57 -07005557 wpa_s->p2p_in_invitation = 1;
5558 wpa_s->p2p_invite_go_freq = freq;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005559
Dmitry Shmidt15907092014-03-25 10:42:57 -07005560 eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s->parent,
5561 NULL);
5562 eloop_register_timeout(P2P_MAX_INITIAL_CONN_WAIT, 0,
5563 wpas_p2p_group_formation_timeout,
5564 wpa_s->parent, NULL);
Dmitry Shmidtfa3fc4a2013-11-21 13:34:38 -08005565 wpa_supplicant_select_network(wpa_s, ssid);
5566
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005567 return 0;
5568}
5569
5570
5571int wpas_p2p_group_add_persistent(struct wpa_supplicant *wpa_s,
5572 struct wpa_ssid *ssid, int addr_allocated,
Dmitry Shmidt96be6222014-02-13 10:16:51 -08005573 int force_freq, int neg_freq, int ht40,
5574 int vht, const struct p2p_channels *channels,
Dmitry Shmidt56052862013-10-04 10:23:25 -07005575 int connection_timeout)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005576{
5577 struct p2p_go_neg_results params;
Dmitry Shmidt96be6222014-02-13 10:16:51 -08005578 int go = 0, freq;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005579
5580 if (ssid->disabled != 2 || ssid->ssid == NULL)
5581 return -1;
5582
5583 if (wpas_get_p2p_group(wpa_s, ssid->ssid, ssid->ssid_len, &go) &&
5584 go == (ssid->mode == WPAS_MODE_P2P_GO)) {
5585 wpa_printf(MSG_DEBUG, "P2P: Requested persistent group is "
5586 "already running");
5587 return 0;
5588 }
5589
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07005590 os_free(wpa_s->global->add_psk);
5591 wpa_s->global->add_psk = NULL;
5592
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005593 /* Make sure we are not running find during connection establishment */
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08005594 wpas_p2p_stop_find_oper(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005595
Dmitry Shmidt04949592012-07-19 12:16:46 -07005596 wpa_s->p2p_fallback_to_go_neg = 0;
5597
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005598 if (ssid->mode == WPAS_MODE_P2P_GO) {
5599 if (force_freq > 0) {
5600 freq = wpas_p2p_select_go_freq(wpa_s, force_freq);
5601 if (freq < 0)
5602 return -1;
5603 } else {
5604 freq = wpas_p2p_select_go_freq(wpa_s, neg_freq);
5605 if (freq < 0 ||
5606 (freq > 0 && !freq_included(channels, freq)))
5607 freq = 0;
5608 }
Dmitry Shmidt96be6222014-02-13 10:16:51 -08005609 } else {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005610 freq = neg_freq;
5611 if (freq < 0 ||
5612 (freq > 0 && !freq_included(channels, freq)))
Dmitry Shmidt96be6222014-02-13 10:16:51 -08005613 freq = 0;
5614 }
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07005615
Dmitry Shmidt15907092014-03-25 10:42:57 -07005616 if (ssid->mode == WPAS_MODE_INFRA)
5617 return wpas_start_p2p_client(wpa_s, ssid, addr_allocated, freq);
5618
5619 if (ssid->mode != WPAS_MODE_P2P_GO)
5620 return -1;
5621
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07005622 if (wpas_p2p_init_go_params(wpa_s, &params, freq, ht40, vht, channels))
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005623 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005624
5625 params.role_go = 1;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08005626 params.psk_set = ssid->psk_set;
5627 if (params.psk_set)
5628 os_memcpy(params.psk, ssid->psk, sizeof(params.psk));
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08005629 if (ssid->passphrase) {
5630 if (os_strlen(ssid->passphrase) >= sizeof(params.passphrase)) {
5631 wpa_printf(MSG_ERROR, "P2P: Invalid passphrase in "
5632 "persistent group");
5633 return -1;
5634 }
5635 os_strlcpy(params.passphrase, ssid->passphrase,
5636 sizeof(params.passphrase));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005637 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005638 os_memcpy(params.ssid, ssid->ssid, ssid->ssid_len);
5639 params.ssid_len = ssid->ssid_len;
5640 params.persistent_group = 1;
5641
5642 wpa_s = wpas_p2p_get_group_iface(wpa_s, addr_allocated, 1);
5643 if (wpa_s == NULL)
5644 return -1;
5645
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005646 p2p_channels_to_freqs(channels, params.freq_list, P2P_MAX_CHANNELS);
5647
Dmitry Shmidt56052862013-10-04 10:23:25 -07005648 wpa_s->p2p_first_connection_timeout = connection_timeout;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005649 wpas_start_wps_go(wpa_s, &params, 0);
5650
5651 return 0;
5652}
5653
5654
5655static void wpas_p2p_ie_update(void *ctx, struct wpabuf *beacon_ies,
5656 struct wpabuf *proberesp_ies)
5657{
5658 struct wpa_supplicant *wpa_s = ctx;
5659 if (wpa_s->ap_iface) {
5660 struct hostapd_data *hapd = wpa_s->ap_iface->bss[0];
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005661 if (!(hapd->conf->p2p & P2P_GROUP_OWNER)) {
5662 wpabuf_free(beacon_ies);
5663 wpabuf_free(proberesp_ies);
5664 return;
5665 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005666 if (beacon_ies) {
5667 wpabuf_free(hapd->p2p_beacon_ie);
5668 hapd->p2p_beacon_ie = beacon_ies;
5669 }
5670 wpabuf_free(hapd->p2p_probe_resp_ie);
5671 hapd->p2p_probe_resp_ie = proberesp_ies;
5672 } else {
5673 wpabuf_free(beacon_ies);
5674 wpabuf_free(proberesp_ies);
5675 }
5676 wpa_supplicant_ap_update_beacon(wpa_s);
5677}
5678
5679
5680static void wpas_p2p_idle_update(void *ctx, int idle)
5681{
5682 struct wpa_supplicant *wpa_s = ctx;
5683 if (!wpa_s->ap_iface)
5684 return;
5685 wpa_printf(MSG_DEBUG, "P2P: GO - group %sidle", idle ? "" : "not ");
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005686 if (idle) {
5687 if (wpa_s->global->p2p_fail_on_wps_complete &&
5688 wpa_s->p2p_in_provisioning) {
5689 wpas_p2p_grpform_fail_after_wps(wpa_s);
5690 return;
5691 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005692 wpas_p2p_set_group_idle_timeout(wpa_s);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005693 } else
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005694 eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL);
5695}
5696
5697
5698struct p2p_group * wpas_p2p_group_init(struct wpa_supplicant *wpa_s,
Dmitry Shmidt04949592012-07-19 12:16:46 -07005699 struct wpa_ssid *ssid)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005700{
5701 struct p2p_group *group;
5702 struct p2p_group_config *cfg;
5703
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005704 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
5705 return NULL;
5706
5707 cfg = os_zalloc(sizeof(*cfg));
5708 if (cfg == NULL)
5709 return NULL;
5710
Dmitry Shmidt04949592012-07-19 12:16:46 -07005711 if (ssid->p2p_persistent_group && wpa_s->conf->persistent_reconnect)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005712 cfg->persistent_group = 2;
Dmitry Shmidt04949592012-07-19 12:16:46 -07005713 else if (ssid->p2p_persistent_group)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005714 cfg->persistent_group = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005715 os_memcpy(cfg->interface_addr, wpa_s->own_addr, ETH_ALEN);
5716 if (wpa_s->max_stations &&
5717 wpa_s->max_stations < wpa_s->conf->max_num_sta)
5718 cfg->max_clients = wpa_s->max_stations;
5719 else
5720 cfg->max_clients = wpa_s->conf->max_num_sta;
Dmitry Shmidt04949592012-07-19 12:16:46 -07005721 os_memcpy(cfg->ssid, ssid->ssid, ssid->ssid_len);
5722 cfg->ssid_len = ssid->ssid_len;
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08005723 cfg->freq = ssid->frequency;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005724 cfg->cb_ctx = wpa_s;
5725 cfg->ie_update = wpas_p2p_ie_update;
5726 cfg->idle_update = wpas_p2p_idle_update;
5727
5728 group = p2p_group_init(wpa_s->global->p2p, cfg);
5729 if (group == NULL)
5730 os_free(cfg);
Dmitry Shmidt04949592012-07-19 12:16:46 -07005731 if (ssid->mode != WPAS_MODE_P2P_GROUP_FORMATION)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005732 p2p_group_notif_formation_done(group);
5733 wpa_s->p2p_group = group;
5734 return group;
5735}
5736
5737
5738void wpas_p2p_wps_success(struct wpa_supplicant *wpa_s, const u8 *peer_addr,
5739 int registrar)
5740{
Dmitry Shmidt04949592012-07-19 12:16:46 -07005741 struct wpa_ssid *ssid = wpa_s->current_ssid;
5742
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005743 if (!wpa_s->p2p_in_provisioning) {
5744 wpa_printf(MSG_DEBUG, "P2P: Ignore WPS success event - P2P "
5745 "provisioning not in progress");
5746 return;
5747 }
5748
Dmitry Shmidt04949592012-07-19 12:16:46 -07005749 if (ssid && ssid->mode == WPAS_MODE_INFRA) {
5750 u8 go_dev_addr[ETH_ALEN];
5751 os_memcpy(go_dev_addr, wpa_s->bssid, ETH_ALEN);
5752 wpas_p2p_persistent_group(wpa_s, go_dev_addr, ssid->ssid,
5753 ssid->ssid_len);
5754 /* Clear any stored provisioning info */
5755 p2p_clear_provisioning_info(wpa_s->global->p2p, go_dev_addr);
5756 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005757
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005758 eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s->parent,
5759 NULL);
Dmitry Shmidt56052862013-10-04 10:23:25 -07005760 wpa_s->p2p_go_group_formation_completed = 1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07005761 if (ssid && ssid->mode == WPAS_MODE_INFRA) {
5762 /*
5763 * Use a separate timeout for initial data connection to
5764 * complete to allow the group to be removed automatically if
5765 * something goes wrong in this step before the P2P group idle
5766 * timeout mechanism is taken into use.
5767 */
Dmitry Shmidt56052862013-10-04 10:23:25 -07005768 wpa_dbg(wpa_s, MSG_DEBUG,
5769 "P2P: Re-start group formation timeout (%d seconds) as client for initial connection",
5770 P2P_MAX_INITIAL_CONN_WAIT);
Dmitry Shmidt04949592012-07-19 12:16:46 -07005771 eloop_register_timeout(P2P_MAX_INITIAL_CONN_WAIT, 0,
5772 wpas_p2p_group_formation_timeout,
5773 wpa_s->parent, NULL);
Dmitry Shmidt56052862013-10-04 10:23:25 -07005774 } else if (ssid) {
5775 /*
5776 * Use a separate timeout for initial data connection to
5777 * complete to allow the group to be removed automatically if
5778 * the client does not complete data connection successfully.
5779 */
5780 wpa_dbg(wpa_s, MSG_DEBUG,
5781 "P2P: Re-start group formation timeout (%d seconds) as GO for initial connection",
5782 P2P_MAX_INITIAL_CONN_WAIT_GO);
5783 eloop_register_timeout(P2P_MAX_INITIAL_CONN_WAIT_GO, 0,
5784 wpas_p2p_group_formation_timeout,
5785 wpa_s->parent, NULL);
5786 /*
5787 * Complete group formation on first successful data connection
5788 */
5789 wpa_s->p2p_go_group_formation_completed = 0;
Dmitry Shmidt04949592012-07-19 12:16:46 -07005790 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005791 if (wpa_s->global->p2p)
5792 p2p_wps_success_cb(wpa_s->global->p2p, peer_addr);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005793 wpas_group_formation_completed(wpa_s, 1);
5794}
5795
5796
Jouni Malinen75ecf522011-06-27 15:19:46 -07005797void wpas_p2p_wps_failed(struct wpa_supplicant *wpa_s,
5798 struct wps_event_fail *fail)
5799{
5800 if (!wpa_s->p2p_in_provisioning) {
5801 wpa_printf(MSG_DEBUG, "P2P: Ignore WPS fail event - P2P "
5802 "provisioning not in progress");
5803 return;
5804 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005805
5806 if (wpa_s->go_params) {
5807 p2p_clear_provisioning_info(
5808 wpa_s->global->p2p,
Dmitry Shmidt04949592012-07-19 12:16:46 -07005809 wpa_s->go_params->peer_device_addr);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005810 }
5811
Jouni Malinen75ecf522011-06-27 15:19:46 -07005812 wpas_notify_p2p_wps_failed(wpa_s, fail);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005813
5814 if (wpa_s == wpa_s->global->p2p_group_formation) {
5815 /*
5816 * Allow some time for the failed WPS negotiation exchange to
5817 * complete, but remove the group since group formation cannot
5818 * succeed after provisioning failure.
5819 */
5820 wpa_printf(MSG_DEBUG, "P2P: WPS step failed during group formation - reject connection from timeout");
5821 wpa_s->global->p2p_fail_on_wps_complete = 1;
5822 eloop_deplete_timeout(0, 50000,
5823 wpas_p2p_group_formation_timeout,
5824 wpa_s->parent, NULL);
5825 }
5826}
5827
5828
5829int wpas_p2p_wps_eapol_cb(struct wpa_supplicant *wpa_s)
5830{
5831 if (!wpa_s->global->p2p_fail_on_wps_complete ||
5832 !wpa_s->p2p_in_provisioning)
5833 return 0;
5834
5835 wpas_p2p_grpform_fail_after_wps(wpa_s);
5836
5837 return 1;
Jouni Malinen75ecf522011-06-27 15:19:46 -07005838}
5839
5840
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005841int wpas_p2p_prov_disc(struct wpa_supplicant *wpa_s, const u8 *peer_addr,
Dmitry Shmidt04949592012-07-19 12:16:46 -07005842 const char *config_method,
5843 enum wpas_p2p_prov_disc_use use)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005844{
5845 u16 config_methods;
5846
Dmitry Shmidt04949592012-07-19 12:16:46 -07005847 wpa_s->p2p_fallback_to_go_neg = 0;
5848 wpa_s->pending_pd_use = NORMAL_PD;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005849 if (os_strncmp(config_method, "display", 7) == 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005850 config_methods = WPS_CONFIG_DISPLAY;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005851 else if (os_strncmp(config_method, "keypad", 6) == 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005852 config_methods = WPS_CONFIG_KEYPAD;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005853 else if (os_strncmp(config_method, "pbc", 3) == 0 ||
5854 os_strncmp(config_method, "pushbutton", 10) == 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005855 config_methods = WPS_CONFIG_PUSHBUTTON;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005856 else {
5857 wpa_printf(MSG_DEBUG, "P2P: Unknown config method");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005858 return -1;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005859 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005860
Dmitry Shmidt04949592012-07-19 12:16:46 -07005861 if (use == WPAS_P2P_PD_AUTO) {
5862 os_memcpy(wpa_s->pending_join_dev_addr, peer_addr, ETH_ALEN);
5863 wpa_s->pending_pd_config_methods = config_methods;
5864 wpa_s->p2p_auto_pd = 1;
5865 wpa_s->p2p_auto_join = 0;
5866 wpa_s->pending_pd_before_join = 0;
5867 wpa_s->auto_pd_scan_retry = 0;
5868 wpas_p2p_stop_find(wpa_s);
5869 wpa_s->p2p_join_scan_count = 0;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005870 os_get_reltime(&wpa_s->p2p_auto_started);
Dmitry Shmidt04949592012-07-19 12:16:46 -07005871 wpa_printf(MSG_DEBUG, "P2P: Auto PD started at %ld.%06ld",
5872 wpa_s->p2p_auto_started.sec,
5873 wpa_s->p2p_auto_started.usec);
5874 wpas_p2p_join_scan(wpa_s, NULL);
5875 return 0;
5876 }
5877
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005878 if (wpa_s->global->p2p == NULL || wpa_s->global->p2p_disabled)
5879 return -1;
5880
5881 return p2p_prov_disc_req(wpa_s->global->p2p, peer_addr,
Dmitry Shmidt04949592012-07-19 12:16:46 -07005882 config_methods, use == WPAS_P2P_PD_FOR_JOIN,
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08005883 0, 1);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005884}
5885
5886
5887int wpas_p2p_scan_result_text(const u8 *ies, size_t ies_len, char *buf,
5888 char *end)
5889{
5890 return p2p_scan_result_text(ies, ies_len, buf, end);
5891}
5892
5893
5894static void wpas_p2p_clear_pending_action_tx(struct wpa_supplicant *wpa_s)
5895{
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08005896 if (!offchannel_pending_action_tx(wpa_s))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005897 return;
5898
Dmitry Shmidt03658832014-08-13 11:03:49 -07005899 wpas_p2p_action_tx_clear(wpa_s);
5900
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005901 wpa_printf(MSG_DEBUG, "P2P: Drop pending Action TX due to new "
5902 "operation request");
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08005903 offchannel_clear_pending_action_tx(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005904}
5905
5906
5907int wpas_p2p_find(struct wpa_supplicant *wpa_s, unsigned int timeout,
5908 enum p2p_discovery_type type,
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08005909 unsigned int num_req_dev_types, const u8 *req_dev_types,
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005910 const u8 *dev_id, unsigned int search_delay)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005911{
5912 wpas_p2p_clear_pending_action_tx(wpa_s);
5913 wpa_s->p2p_long_listen = 0;
5914
Dmitry Shmidt04949592012-07-19 12:16:46 -07005915 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL ||
5916 wpa_s->p2p_in_provisioning)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005917 return -1;
5918
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08005919 wpa_supplicant_cancel_sched_scan(wpa_s);
5920
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005921 return p2p_find(wpa_s->global->p2p, timeout, type,
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005922 num_req_dev_types, req_dev_types, dev_id,
5923 search_delay);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005924}
5925
5926
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005927static void wpas_p2p_scan_res_ignore_search(struct wpa_supplicant *wpa_s,
5928 struct wpa_scan_results *scan_res)
5929{
5930 wpa_printf(MSG_DEBUG, "P2P: Ignore scan results");
5931
5932 if (wpa_s->p2p_scan_work) {
5933 struct wpa_radio_work *work = wpa_s->p2p_scan_work;
5934 wpa_s->p2p_scan_work = NULL;
5935 radio_work_done(work);
5936 }
5937
5938 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
5939 return;
5940
5941 /*
5942 * Indicate that results have been processed so that the P2P module can
5943 * continue pending tasks.
5944 */
5945 p2p_scan_res_handled(wpa_s->global->p2p);
5946}
5947
5948
5949static void wpas_p2p_stop_find_oper(struct wpa_supplicant *wpa_s)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005950{
5951 wpas_p2p_clear_pending_action_tx(wpa_s);
5952 wpa_s->p2p_long_listen = 0;
5953 eloop_cancel_timeout(wpas_p2p_long_listen_timeout, wpa_s, NULL);
5954 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005955
5956 if (wpa_s->global->p2p)
5957 p2p_stop_find(wpa_s->global->p2p);
5958
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005959 if (wpa_s->scan_res_handler == wpas_p2p_scan_res_handler) {
5960 wpa_printf(MSG_DEBUG,
5961 "P2P: Do not consider the scan results after stop_find");
5962 wpa_s->scan_res_handler = wpas_p2p_scan_res_ignore_search;
5963 }
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08005964}
5965
5966
5967void wpas_p2p_stop_find(struct wpa_supplicant *wpa_s)
5968{
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005969 wpas_p2p_stop_find_oper(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005970 wpas_p2p_remove_pending_group_interface(wpa_s);
5971}
5972
5973
5974static void wpas_p2p_long_listen_timeout(void *eloop_ctx, void *timeout_ctx)
5975{
5976 struct wpa_supplicant *wpa_s = eloop_ctx;
5977 wpa_s->p2p_long_listen = 0;
5978}
5979
5980
5981int wpas_p2p_listen(struct wpa_supplicant *wpa_s, unsigned int timeout)
5982{
5983 int res;
5984
5985 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
5986 return -1;
5987
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08005988 wpa_supplicant_cancel_sched_scan(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005989 wpas_p2p_clear_pending_action_tx(wpa_s);
5990
5991 if (timeout == 0) {
5992 /*
5993 * This is a request for unlimited Listen state. However, at
5994 * least for now, this is mapped to a Listen state for one
5995 * hour.
5996 */
5997 timeout = 3600;
5998 }
5999 eloop_cancel_timeout(wpas_p2p_long_listen_timeout, wpa_s, NULL);
6000 wpa_s->p2p_long_listen = 0;
6001
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006002 /*
6003 * Stop previous find/listen operation to avoid trying to request a new
6004 * remain-on-channel operation while the driver is still running the
6005 * previous one.
6006 */
6007 if (wpa_s->global->p2p)
6008 p2p_stop_find(wpa_s->global->p2p);
6009
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006010 res = wpas_p2p_listen_start(wpa_s, timeout * 1000);
6011 if (res == 0 && timeout * 1000 > wpa_s->max_remain_on_chan) {
6012 wpa_s->p2p_long_listen = timeout * 1000;
6013 eloop_register_timeout(timeout, 0,
6014 wpas_p2p_long_listen_timeout,
6015 wpa_s, NULL);
6016 }
6017
6018 return res;
6019}
6020
6021
6022int wpas_p2p_assoc_req_ie(struct wpa_supplicant *wpa_s, struct wpa_bss *bss,
6023 u8 *buf, size_t len, int p2p_group)
6024{
6025 struct wpabuf *p2p_ie;
6026 int ret;
6027
6028 if (wpa_s->global->p2p_disabled)
6029 return -1;
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07006030 if (wpa_s->conf->p2p_disabled)
6031 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006032 if (wpa_s->global->p2p == NULL)
6033 return -1;
6034 if (bss == NULL)
6035 return -1;
6036
6037 p2p_ie = wpa_bss_get_vendor_ie_multi(bss, P2P_IE_VENDOR_TYPE);
6038 ret = p2p_assoc_req_ie(wpa_s->global->p2p, bss->bssid, buf, len,
6039 p2p_group, p2p_ie);
6040 wpabuf_free(p2p_ie);
6041
6042 return ret;
6043}
6044
6045
6046int wpas_p2p_probe_req_rx(struct wpa_supplicant *wpa_s, const u8 *addr,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006047 const u8 *dst, const u8 *bssid,
Dmitry Shmidt04949592012-07-19 12:16:46 -07006048 const u8 *ie, size_t ie_len, int ssi_signal)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006049{
6050 if (wpa_s->global->p2p_disabled)
6051 return 0;
6052 if (wpa_s->global->p2p == NULL)
6053 return 0;
6054
Dmitry Shmidt04949592012-07-19 12:16:46 -07006055 switch (p2p_probe_req_rx(wpa_s->global->p2p, addr, dst, bssid,
6056 ie, ie_len)) {
6057 case P2P_PREQ_NOT_P2P:
6058 wpas_notify_preq(wpa_s, addr, dst, bssid, ie, ie_len,
6059 ssi_signal);
6060 /* fall through */
6061 case P2P_PREQ_MALFORMED:
6062 case P2P_PREQ_NOT_LISTEN:
6063 case P2P_PREQ_NOT_PROCESSED:
6064 default: /* make gcc happy */
6065 return 0;
6066 case P2P_PREQ_PROCESSED:
6067 return 1;
6068 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006069}
6070
6071
6072void wpas_p2p_rx_action(struct wpa_supplicant *wpa_s, const u8 *da,
6073 const u8 *sa, const u8 *bssid,
6074 u8 category, const u8 *data, size_t len, int freq)
6075{
6076 if (wpa_s->global->p2p_disabled)
6077 return;
6078 if (wpa_s->global->p2p == NULL)
6079 return;
6080
6081 p2p_rx_action(wpa_s->global->p2p, da, sa, bssid, category, data, len,
6082 freq);
6083}
6084
6085
6086void wpas_p2p_scan_ie(struct wpa_supplicant *wpa_s, struct wpabuf *ies)
6087{
6088 if (wpa_s->global->p2p_disabled)
6089 return;
6090 if (wpa_s->global->p2p == NULL)
6091 return;
6092
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08006093 p2p_scan_ie(wpa_s->global->p2p, ies, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006094}
6095
6096
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07006097static void wpas_p2p_group_deinit(struct wpa_supplicant *wpa_s)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006098{
6099 p2p_group_deinit(wpa_s->p2p_group);
6100 wpa_s->p2p_group = NULL;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006101
6102 wpa_s->ap_configured_cb = NULL;
6103 wpa_s->ap_configured_cb_ctx = NULL;
6104 wpa_s->ap_configured_cb_data = NULL;
6105 wpa_s->connect_without_scan = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006106}
6107
6108
6109int wpas_p2p_reject(struct wpa_supplicant *wpa_s, const u8 *addr)
6110{
6111 wpa_s->p2p_long_listen = 0;
6112
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006113 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
6114 return -1;
6115
6116 return p2p_reject(wpa_s->global->p2p, addr);
6117}
6118
6119
6120/* Invite to reinvoke a persistent group */
6121int wpas_p2p_invite(struct wpa_supplicant *wpa_s, const u8 *peer_addr,
Jouni Malinen31be0a42012-08-31 21:20:51 +03006122 struct wpa_ssid *ssid, const u8 *go_dev_addr, int freq,
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07006123 int ht40, int vht, int pref_freq)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006124{
6125 enum p2p_invite_role role;
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07006126 u8 *bssid = NULL;
6127 int force_freq = 0;
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08006128 int res;
Dmitry Shmidta0d265f2013-11-19 13:13:41 -08006129 int no_pref_freq_given = pref_freq == 0;
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08006130
Dmitry Shmidt700a1372013-03-15 14:14:44 -07006131 wpa_s->global->p2p_invite_group = NULL;
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08006132 if (peer_addr)
6133 os_memcpy(wpa_s->p2p_auth_invite, peer_addr, ETH_ALEN);
6134 else
6135 os_memset(wpa_s->p2p_auth_invite, 0, ETH_ALEN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006136
Jouni Malinen31be0a42012-08-31 21:20:51 +03006137 wpa_s->p2p_persistent_go_freq = freq;
6138 wpa_s->p2p_go_ht40 = !!ht40;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006139 if (ssid->mode == WPAS_MODE_P2P_GO) {
6140 role = P2P_INVITE_ROLE_GO;
6141 if (peer_addr == NULL) {
6142 wpa_printf(MSG_DEBUG, "P2P: Missing peer "
6143 "address in invitation command");
6144 return -1;
6145 }
6146 if (wpas_p2p_create_iface(wpa_s)) {
6147 if (wpas_p2p_add_group_interface(wpa_s,
6148 WPA_IF_P2P_GO) < 0) {
6149 wpa_printf(MSG_ERROR, "P2P: Failed to "
6150 "allocate a new interface for the "
6151 "group");
6152 return -1;
6153 }
6154 bssid = wpa_s->pending_interface_addr;
6155 } else
6156 bssid = wpa_s->own_addr;
6157 } else {
6158 role = P2P_INVITE_ROLE_CLIENT;
6159 peer_addr = ssid->bssid;
6160 }
6161 wpa_s->pending_invite_ssid_id = ssid->id;
6162
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07006163 res = wpas_p2p_setup_freqs(wpa_s, freq, &force_freq, &pref_freq,
6164 role == P2P_INVITE_ROLE_GO);
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08006165 if (res)
6166 return res;
Irfan Sheriffaf84a572012-09-22 16:59:30 -07006167
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006168 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
6169 return -1;
6170
Dmitry Shmidta0d265f2013-11-19 13:13:41 -08006171 if (wpa_s->parent->conf->p2p_ignore_shared_freq &&
6172 no_pref_freq_given && pref_freq > 0 &&
6173 wpa_s->num_multichan_concurrent > 1 &&
6174 wpas_p2p_num_unused_channels(wpa_s) > 0) {
6175 wpa_printf(MSG_DEBUG, "P2P: Ignore own channel preference %d MHz for invitation due to p2p_ignore_shared_freq=1 configuration",
6176 pref_freq);
6177 pref_freq = 0;
6178 }
6179
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006180 return p2p_invite(wpa_s->global->p2p, peer_addr, role, bssid,
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08006181 ssid->ssid, ssid->ssid_len, force_freq, go_dev_addr,
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08006182 1, pref_freq, -1);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006183}
6184
6185
6186/* Invite to join an active group */
6187int wpas_p2p_invite_group(struct wpa_supplicant *wpa_s, const char *ifname,
6188 const u8 *peer_addr, const u8 *go_dev_addr)
6189{
6190 struct wpa_global *global = wpa_s->global;
6191 enum p2p_invite_role role;
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07006192 u8 *bssid = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006193 struct wpa_ssid *ssid;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08006194 int persistent;
Dmitry Shmidt051af732013-10-22 13:52:46 -07006195 int freq = 0, force_freq = 0, pref_freq = 0;
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08006196 int res;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006197
Jouni Malinen31be0a42012-08-31 21:20:51 +03006198 wpa_s->p2p_persistent_go_freq = 0;
6199 wpa_s->p2p_go_ht40 = 0;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07006200 wpa_s->p2p_go_vht = 0;
Jouni Malinen31be0a42012-08-31 21:20:51 +03006201
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006202 for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
6203 if (os_strcmp(wpa_s->ifname, ifname) == 0)
6204 break;
6205 }
6206 if (wpa_s == NULL) {
6207 wpa_printf(MSG_DEBUG, "P2P: Interface '%s' not found", ifname);
6208 return -1;
6209 }
6210
6211 ssid = wpa_s->current_ssid;
6212 if (ssid == NULL) {
6213 wpa_printf(MSG_DEBUG, "P2P: No current SSID to use for "
6214 "invitation");
6215 return -1;
6216 }
6217
Dmitry Shmidt700a1372013-03-15 14:14:44 -07006218 wpa_s->global->p2p_invite_group = wpa_s;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08006219 persistent = ssid->p2p_persistent_group &&
6220 wpas_p2p_get_persistent(wpa_s->parent, peer_addr,
6221 ssid->ssid, ssid->ssid_len);
6222
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006223 if (ssid->mode == WPAS_MODE_P2P_GO) {
6224 role = P2P_INVITE_ROLE_ACTIVE_GO;
6225 bssid = wpa_s->own_addr;
6226 if (go_dev_addr == NULL)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006227 go_dev_addr = wpa_s->global->p2p_dev_addr;
Dmitry Shmidt051af732013-10-22 13:52:46 -07006228 freq = ssid->frequency;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006229 } else {
6230 role = P2P_INVITE_ROLE_CLIENT;
6231 if (wpa_s->wpa_state < WPA_ASSOCIATED) {
6232 wpa_printf(MSG_DEBUG, "P2P: Not associated - cannot "
6233 "invite to current group");
6234 return -1;
6235 }
6236 bssid = wpa_s->bssid;
6237 if (go_dev_addr == NULL &&
6238 !is_zero_ether_addr(wpa_s->go_dev_addr))
6239 go_dev_addr = wpa_s->go_dev_addr;
Dmitry Shmidt051af732013-10-22 13:52:46 -07006240 freq = wpa_s->current_bss ? wpa_s->current_bss->freq :
6241 (int) wpa_s->assoc_freq;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006242 }
6243 wpa_s->parent->pending_invite_ssid_id = -1;
6244
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006245 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
6246 return -1;
6247
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07006248 res = wpas_p2p_setup_freqs(wpa_s, freq, &force_freq, &pref_freq,
6249 role == P2P_INVITE_ROLE_ACTIVE_GO);
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08006250 if (res)
6251 return res;
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07006252 wpas_p2p_set_own_freq_preference(wpa_s, force_freq);
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08006253
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006254 return p2p_invite(wpa_s->global->p2p, peer_addr, role, bssid,
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08006255 ssid->ssid, ssid->ssid_len, force_freq,
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08006256 go_dev_addr, persistent, pref_freq, -1);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006257}
6258
6259
6260void wpas_p2p_completed(struct wpa_supplicant *wpa_s)
6261{
6262 struct wpa_ssid *ssid = wpa_s->current_ssid;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006263 u8 go_dev_addr[ETH_ALEN];
Jouni Malinen75ecf522011-06-27 15:19:46 -07006264 int network_id = -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006265 int persistent;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006266 int freq;
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08006267 u8 ip[3 * 4];
6268 char ip_addr[100];
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006269
Dmitry Shmidt04949592012-07-19 12:16:46 -07006270 if (ssid == NULL || ssid->mode != WPAS_MODE_P2P_GROUP_FORMATION) {
6271 eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
6272 wpa_s->parent, NULL);
6273 }
6274
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006275 if (!wpa_s->show_group_started || !ssid)
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08006276 return;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006277
6278 wpa_s->show_group_started = 0;
6279
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006280 os_memset(go_dev_addr, 0, ETH_ALEN);
6281 if (ssid->bssid_set)
6282 os_memcpy(go_dev_addr, ssid->bssid, ETH_ALEN);
6283 persistent = wpas_p2p_persistent_group(wpa_s, go_dev_addr, ssid->ssid,
6284 ssid->ssid_len);
6285 os_memcpy(wpa_s->go_dev_addr, go_dev_addr, ETH_ALEN);
6286
6287 if (wpa_s->global->p2p_group_formation == wpa_s)
6288 wpa_s->global->p2p_group_formation = NULL;
6289
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006290 freq = wpa_s->current_bss ? wpa_s->current_bss->freq :
6291 (int) wpa_s->assoc_freq;
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08006292
6293 ip_addr[0] = '\0';
6294 if (wpa_sm_get_p2p_ip_addr(wpa_s->wpa, ip) == 0) {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006295 int res;
6296
6297 res = os_snprintf(ip_addr, sizeof(ip_addr),
6298 " ip_addr=%u.%u.%u.%u "
6299 "ip_mask=%u.%u.%u.%u go_ip_addr=%u.%u.%u.%u",
6300 ip[0], ip[1], ip[2], ip[3],
6301 ip[4], ip[5], ip[6], ip[7],
6302 ip[8], ip[9], ip[10], ip[11]);
6303 if (os_snprintf_error(sizeof(ip_addr), res))
6304 ip_addr[0] = '\0';
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08006305 }
6306
Dmitry Shmidt7f0b69e2014-07-28 10:35:20 -07006307 wpas_p2p_group_started(wpa_s, 0, ssid, freq,
6308 ssid->passphrase == NULL && ssid->psk_set ?
6309 ssid->psk : NULL,
6310 ssid->passphrase, go_dev_addr, persistent,
6311 ip_addr);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006312
6313 if (persistent)
Jouni Malinen75ecf522011-06-27 15:19:46 -07006314 network_id = wpas_p2p_store_persistent_group(wpa_s->parent,
6315 ssid, go_dev_addr);
6316 if (network_id < 0)
6317 network_id = ssid->id;
6318 wpas_notify_p2p_group_started(wpa_s, ssid, network_id, 1);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006319}
6320
6321
6322int wpas_p2p_presence_req(struct wpa_supplicant *wpa_s, u32 duration1,
6323 u32 interval1, u32 duration2, u32 interval2)
6324{
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08006325 int ret;
6326
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006327 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
6328 return -1;
6329
6330 if (wpa_s->wpa_state < WPA_ASSOCIATED ||
6331 wpa_s->current_ssid == NULL ||
6332 wpa_s->current_ssid->mode != WPAS_MODE_INFRA)
6333 return -1;
6334
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08006335 ret = p2p_presence_req(wpa_s->global->p2p, wpa_s->bssid,
6336 wpa_s->own_addr, wpa_s->assoc_freq,
6337 duration1, interval1, duration2, interval2);
6338 if (ret == 0)
6339 wpa_s->waiting_presence_resp = 1;
6340
6341 return ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006342}
6343
6344
6345int wpas_p2p_ext_listen(struct wpa_supplicant *wpa_s, unsigned int period,
6346 unsigned int interval)
6347{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006348 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
6349 return -1;
6350
6351 return p2p_ext_listen(wpa_s->global->p2p, period, interval);
6352}
6353
6354
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006355static int wpas_p2p_is_client(struct wpa_supplicant *wpa_s)
6356{
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08006357 if (wpa_s->current_ssid == NULL) {
6358 /*
6359 * current_ssid can be cleared when P2P client interface gets
6360 * disconnected, so assume this interface was used as P2P
6361 * client.
6362 */
6363 return 1;
6364 }
6365 return wpa_s->current_ssid->p2p_group &&
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006366 wpa_s->current_ssid->mode == WPAS_MODE_INFRA;
6367}
6368
6369
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006370static void wpas_p2p_group_idle_timeout(void *eloop_ctx, void *timeout_ctx)
6371{
6372 struct wpa_supplicant *wpa_s = eloop_ctx;
6373
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006374 if (wpa_s->conf->p2p_group_idle == 0 && !wpas_p2p_is_client(wpa_s)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006375 wpa_printf(MSG_DEBUG, "P2P: Ignore group idle timeout - "
6376 "disabled");
6377 return;
6378 }
6379
Dmitry Shmidt04949592012-07-19 12:16:46 -07006380 wpa_printf(MSG_DEBUG, "P2P: Group idle timeout reached - terminate "
6381 "group");
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07006382 wpas_p2p_group_delete(wpa_s, P2P_GROUP_REMOVAL_IDLE_TIMEOUT);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006383}
6384
6385
6386static void wpas_p2p_set_group_idle_timeout(struct wpa_supplicant *wpa_s)
6387{
Dmitry Shmidt04949592012-07-19 12:16:46 -07006388 int timeout;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006389
Dmitry Shmidt04949592012-07-19 12:16:46 -07006390 if (eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL) > 0)
6391 wpa_printf(MSG_DEBUG, "P2P: Cancelled P2P group idle timeout");
6392
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006393 if (wpa_s->current_ssid == NULL || !wpa_s->current_ssid->p2p_group)
6394 return;
6395
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006396 timeout = wpa_s->conf->p2p_group_idle;
6397 if (wpa_s->current_ssid->mode == WPAS_MODE_INFRA &&
6398 (timeout == 0 || timeout > P2P_MAX_CLIENT_IDLE))
6399 timeout = P2P_MAX_CLIENT_IDLE;
6400
6401 if (timeout == 0)
6402 return;
6403
Dmitry Shmidt04949592012-07-19 12:16:46 -07006404 if (timeout < 0) {
6405 if (wpa_s->current_ssid->mode == WPAS_MODE_INFRA)
6406 timeout = 0; /* special client mode no-timeout */
6407 else
6408 return;
6409 }
6410
6411 if (wpa_s->p2p_in_provisioning) {
6412 /*
6413 * Use the normal group formation timeout during the
6414 * provisioning phase to avoid terminating this process too
6415 * early due to group idle timeout.
6416 */
6417 wpa_printf(MSG_DEBUG, "P2P: Do not use P2P group idle timeout "
6418 "during provisioning");
6419 return;
6420 }
Deepthi Gowri9e4e8ac2013-04-16 11:57:05 +05306421
Dmitry Shmidt04949592012-07-19 12:16:46 -07006422 if (wpa_s->show_group_started) {
6423 /*
6424 * Use the normal group formation timeout between the end of
6425 * the provisioning phase and completion of 4-way handshake to
6426 * avoid terminating this process too early due to group idle
6427 * timeout.
6428 */
6429 wpa_printf(MSG_DEBUG, "P2P: Do not use P2P group idle timeout "
6430 "while waiting for initial 4-way handshake to "
6431 "complete");
6432 return;
6433 }
6434
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006435 wpa_printf(MSG_DEBUG, "P2P: Set P2P group idle timeout to %u seconds",
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006436 timeout);
6437 eloop_register_timeout(timeout, 0, wpas_p2p_group_idle_timeout,
6438 wpa_s, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006439}
6440
6441
Jouni Malinen2b89da82012-08-31 22:04:41 +03006442/* Returns 1 if the interface was removed */
6443int wpas_p2p_deauth_notif(struct wpa_supplicant *wpa_s, const u8 *bssid,
6444 u16 reason_code, const u8 *ie, size_t ie_len,
6445 int locally_generated)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006446{
6447 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
Jouni Malinen2b89da82012-08-31 22:04:41 +03006448 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006449
Dmitry Shmidt04949592012-07-19 12:16:46 -07006450 if (!locally_generated)
6451 p2p_deauth_notif(wpa_s->global->p2p, bssid, reason_code, ie,
6452 ie_len);
6453
6454 if (reason_code == WLAN_REASON_DEAUTH_LEAVING && !locally_generated &&
6455 wpa_s->current_ssid &&
6456 wpa_s->current_ssid->p2p_group &&
6457 wpa_s->current_ssid->mode == WPAS_MODE_INFRA) {
6458 wpa_printf(MSG_DEBUG, "P2P: GO indicated that the P2P Group "
6459 "session is ending");
Jouni Malinen2b89da82012-08-31 22:04:41 +03006460 if (wpas_p2p_group_delete(wpa_s,
6461 P2P_GROUP_REMOVAL_GO_ENDING_SESSION)
6462 > 0)
6463 return 1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07006464 }
Jouni Malinen2b89da82012-08-31 22:04:41 +03006465
6466 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006467}
6468
6469
6470void wpas_p2p_disassoc_notif(struct wpa_supplicant *wpa_s, const u8 *bssid,
Dmitry Shmidt04949592012-07-19 12:16:46 -07006471 u16 reason_code, const u8 *ie, size_t ie_len,
6472 int locally_generated)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006473{
6474 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
6475 return;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006476
Dmitry Shmidt04949592012-07-19 12:16:46 -07006477 if (!locally_generated)
6478 p2p_disassoc_notif(wpa_s->global->p2p, bssid, reason_code, ie,
6479 ie_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006480}
6481
6482
6483void wpas_p2p_update_config(struct wpa_supplicant *wpa_s)
6484{
6485 struct p2p_data *p2p = wpa_s->global->p2p;
6486
6487 if (p2p == NULL)
6488 return;
6489
6490 if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_CAPABLE))
6491 return;
6492
6493 if (wpa_s->conf->changed_parameters & CFG_CHANGED_DEVICE_NAME)
6494 p2p_set_dev_name(p2p, wpa_s->conf->device_name);
6495
6496 if (wpa_s->conf->changed_parameters & CFG_CHANGED_DEVICE_TYPE)
6497 p2p_set_pri_dev_type(p2p, wpa_s->conf->device_type);
6498
6499 if (wpa_s->wps &&
6500 (wpa_s->conf->changed_parameters & CFG_CHANGED_CONFIG_METHODS))
6501 p2p_set_config_methods(p2p, wpa_s->wps->config_methods);
6502
6503 if (wpa_s->wps && (wpa_s->conf->changed_parameters & CFG_CHANGED_UUID))
6504 p2p_set_uuid(p2p, wpa_s->wps->uuid);
6505
6506 if (wpa_s->conf->changed_parameters & CFG_CHANGED_WPS_STRING) {
6507 p2p_set_manufacturer(p2p, wpa_s->conf->manufacturer);
6508 p2p_set_model_name(p2p, wpa_s->conf->model_name);
6509 p2p_set_model_number(p2p, wpa_s->conf->model_number);
6510 p2p_set_serial_number(p2p, wpa_s->conf->serial_number);
6511 }
6512
6513 if (wpa_s->conf->changed_parameters & CFG_CHANGED_SEC_DEVICE_TYPE)
6514 p2p_set_sec_dev_types(p2p,
6515 (void *) wpa_s->conf->sec_device_type,
6516 wpa_s->conf->num_sec_device_types);
6517
6518 if (wpa_s->conf->changed_parameters & CFG_CHANGED_VENDOR_EXTENSION) {
6519 int i;
6520 p2p_remove_wps_vendor_extensions(p2p);
6521 for (i = 0; i < MAX_WPS_VENDOR_EXT; i++) {
6522 if (wpa_s->conf->wps_vendor_ext[i] == NULL)
6523 continue;
6524 p2p_add_wps_vendor_extension(
6525 p2p, wpa_s->conf->wps_vendor_ext[i]);
6526 }
6527 }
6528
6529 if ((wpa_s->conf->changed_parameters & CFG_CHANGED_COUNTRY) &&
6530 wpa_s->conf->country[0] && wpa_s->conf->country[1]) {
6531 char country[3];
6532 country[0] = wpa_s->conf->country[0];
6533 country[1] = wpa_s->conf->country[1];
6534 country[2] = 0x04;
6535 p2p_set_country(p2p, country);
6536 }
6537
6538 if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_SSID_POSTFIX) {
6539 p2p_set_ssid_postfix(p2p, (u8 *) wpa_s->conf->p2p_ssid_postfix,
6540 wpa_s->conf->p2p_ssid_postfix ?
6541 os_strlen(wpa_s->conf->p2p_ssid_postfix) :
6542 0);
6543 }
6544
6545 if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_INTRA_BSS)
6546 p2p_set_intra_bss_dist(p2p, wpa_s->conf->p2p_intra_bss);
Jouni Malinen75ecf522011-06-27 15:19:46 -07006547
6548 if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_LISTEN_CHANNEL) {
6549 u8 reg_class, channel;
6550 int ret;
6551 unsigned int r;
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07006552 u8 channel_forced;
6553
Jouni Malinen75ecf522011-06-27 15:19:46 -07006554 if (wpa_s->conf->p2p_listen_reg_class &&
6555 wpa_s->conf->p2p_listen_channel) {
6556 reg_class = wpa_s->conf->p2p_listen_reg_class;
6557 channel = wpa_s->conf->p2p_listen_channel;
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07006558 channel_forced = 1;
Jouni Malinen75ecf522011-06-27 15:19:46 -07006559 } else {
6560 reg_class = 81;
6561 /*
6562 * Pick one of the social channels randomly as the
6563 * listen channel.
6564 */
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07006565 if (os_get_random((u8 *) &r, sizeof(r)) < 0)
6566 channel = 1;
6567 else
6568 channel = 1 + (r % 3) * 5;
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07006569 channel_forced = 0;
Jouni Malinen75ecf522011-06-27 15:19:46 -07006570 }
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07006571 ret = p2p_set_listen_channel(p2p, reg_class, channel,
6572 channel_forced);
Jouni Malinen75ecf522011-06-27 15:19:46 -07006573 if (ret)
6574 wpa_printf(MSG_ERROR, "P2P: Own listen channel update "
6575 "failed: %d", ret);
6576 }
6577 if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_OPER_CHANNEL) {
6578 u8 op_reg_class, op_channel, cfg_op_channel;
6579 int ret = 0;
6580 unsigned int r;
6581 if (wpa_s->conf->p2p_oper_reg_class &&
6582 wpa_s->conf->p2p_oper_channel) {
6583 op_reg_class = wpa_s->conf->p2p_oper_reg_class;
6584 op_channel = wpa_s->conf->p2p_oper_channel;
6585 cfg_op_channel = 1;
6586 } else {
6587 op_reg_class = 81;
6588 /*
6589 * Use random operation channel from (1, 6, 11)
6590 *if no other preference is indicated.
6591 */
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07006592 if (os_get_random((u8 *) &r, sizeof(r)) < 0)
6593 op_channel = 1;
6594 else
6595 op_channel = 1 + (r % 3) * 5;
Jouni Malinen75ecf522011-06-27 15:19:46 -07006596 cfg_op_channel = 0;
6597 }
6598 ret = p2p_set_oper_channel(p2p, op_reg_class, op_channel,
6599 cfg_op_channel);
6600 if (ret)
6601 wpa_printf(MSG_ERROR, "P2P: Own oper channel update "
6602 "failed: %d", ret);
6603 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07006604
6605 if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_PREF_CHAN) {
6606 if (p2p_set_pref_chan(p2p, wpa_s->conf->num_p2p_pref_chan,
6607 wpa_s->conf->p2p_pref_chan) < 0) {
6608 wpa_printf(MSG_ERROR, "P2P: Preferred channel list "
6609 "update failed");
6610 }
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07006611
6612 if (p2p_set_no_go_freq(p2p, &wpa_s->conf->p2p_no_go_freq) < 0) {
6613 wpa_printf(MSG_ERROR, "P2P: No GO channel list "
6614 "update failed");
6615 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07006616 }
Dmitry Shmidt2271d3f2014-06-23 12:16:31 -07006617
6618 if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_PASSPHRASE_LEN)
6619 p2p_set_passphrase_len(p2p, wpa_s->conf->p2p_passphrase_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006620}
6621
6622
6623int wpas_p2p_set_noa(struct wpa_supplicant *wpa_s, u8 count, int start,
6624 int duration)
6625{
6626 if (!wpa_s->ap_iface)
6627 return -1;
6628 return hostapd_p2p_set_noa(wpa_s->ap_iface->bss[0], count, start,
6629 duration);
6630}
6631
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006632
6633int wpas_p2p_set_cross_connect(struct wpa_supplicant *wpa_s, int enabled)
6634{
6635 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
6636 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006637
6638 wpa_s->global->cross_connection = enabled;
6639 p2p_set_cross_connect(wpa_s->global->p2p, enabled);
6640
6641 if (!enabled) {
6642 struct wpa_supplicant *iface;
6643
6644 for (iface = wpa_s->global->ifaces; iface; iface = iface->next)
6645 {
6646 if (iface->cross_connect_enabled == 0)
6647 continue;
6648
6649 iface->cross_connect_enabled = 0;
6650 iface->cross_connect_in_use = 0;
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07006651 wpa_msg_global(iface->parent, MSG_INFO,
6652 P2P_EVENT_CROSS_CONNECT_DISABLE "%s %s",
6653 iface->ifname,
6654 iface->cross_connect_uplink);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006655 }
6656 }
6657
6658 return 0;
6659}
6660
6661
6662static void wpas_p2p_enable_cross_connect(struct wpa_supplicant *uplink)
6663{
6664 struct wpa_supplicant *iface;
6665
6666 if (!uplink->global->cross_connection)
6667 return;
6668
6669 for (iface = uplink->global->ifaces; iface; iface = iface->next) {
6670 if (!iface->cross_connect_enabled)
6671 continue;
6672 if (os_strcmp(uplink->ifname, iface->cross_connect_uplink) !=
6673 0)
6674 continue;
6675 if (iface->ap_iface == NULL)
6676 continue;
6677 if (iface->cross_connect_in_use)
6678 continue;
6679
6680 iface->cross_connect_in_use = 1;
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07006681 wpa_msg_global(iface->parent, MSG_INFO,
6682 P2P_EVENT_CROSS_CONNECT_ENABLE "%s %s",
6683 iface->ifname, iface->cross_connect_uplink);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006684 }
6685}
6686
6687
6688static void wpas_p2p_disable_cross_connect(struct wpa_supplicant *uplink)
6689{
6690 struct wpa_supplicant *iface;
6691
6692 for (iface = uplink->global->ifaces; iface; iface = iface->next) {
6693 if (!iface->cross_connect_enabled)
6694 continue;
6695 if (os_strcmp(uplink->ifname, iface->cross_connect_uplink) !=
6696 0)
6697 continue;
6698 if (!iface->cross_connect_in_use)
6699 continue;
6700
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07006701 wpa_msg_global(iface->parent, MSG_INFO,
6702 P2P_EVENT_CROSS_CONNECT_DISABLE "%s %s",
6703 iface->ifname, iface->cross_connect_uplink);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006704 iface->cross_connect_in_use = 0;
6705 }
6706}
6707
6708
6709void wpas_p2p_notif_connected(struct wpa_supplicant *wpa_s)
6710{
6711 if (wpa_s->ap_iface || wpa_s->current_ssid == NULL ||
6712 wpa_s->current_ssid->mode != WPAS_MODE_INFRA ||
6713 wpa_s->cross_connect_disallowed)
6714 wpas_p2p_disable_cross_connect(wpa_s);
6715 else
6716 wpas_p2p_enable_cross_connect(wpa_s);
Dmitry Shmidt04949592012-07-19 12:16:46 -07006717 if (!wpa_s->ap_iface &&
6718 eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL) > 0)
6719 wpa_printf(MSG_DEBUG, "P2P: Cancelled P2P group idle timeout");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006720}
6721
6722
6723void wpas_p2p_notif_disconnected(struct wpa_supplicant *wpa_s)
6724{
6725 wpas_p2p_disable_cross_connect(wpa_s);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006726 if (!wpa_s->ap_iface &&
6727 !eloop_is_timeout_registered(wpas_p2p_group_idle_timeout,
6728 wpa_s, NULL))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006729 wpas_p2p_set_group_idle_timeout(wpa_s);
6730}
6731
6732
6733static void wpas_p2p_cross_connect_setup(struct wpa_supplicant *wpa_s)
6734{
6735 struct wpa_supplicant *iface;
6736
6737 if (!wpa_s->global->cross_connection)
6738 return;
6739
6740 for (iface = wpa_s->global->ifaces; iface; iface = iface->next) {
6741 if (iface == wpa_s)
6742 continue;
6743 if (iface->drv_flags &
6744 WPA_DRIVER_FLAGS_P2P_DEDICATED_INTERFACE)
6745 continue;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006746 if ((iface->drv_flags & WPA_DRIVER_FLAGS_P2P_CAPABLE) &&
6747 iface != wpa_s->parent)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006748 continue;
6749
6750 wpa_s->cross_connect_enabled = 1;
6751 os_strlcpy(wpa_s->cross_connect_uplink, iface->ifname,
6752 sizeof(wpa_s->cross_connect_uplink));
6753 wpa_printf(MSG_DEBUG, "P2P: Enable cross connection from "
6754 "%s to %s whenever uplink is available",
6755 wpa_s->ifname, wpa_s->cross_connect_uplink);
6756
6757 if (iface->ap_iface || iface->current_ssid == NULL ||
6758 iface->current_ssid->mode != WPAS_MODE_INFRA ||
6759 iface->cross_connect_disallowed ||
6760 iface->wpa_state != WPA_COMPLETED)
6761 break;
6762
6763 wpa_s->cross_connect_in_use = 1;
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07006764 wpa_msg_global(wpa_s->parent, MSG_INFO,
6765 P2P_EVENT_CROSS_CONNECT_ENABLE "%s %s",
6766 wpa_s->ifname, wpa_s->cross_connect_uplink);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006767 break;
6768 }
6769}
6770
6771
6772int wpas_p2p_notif_pbc_overlap(struct wpa_supplicant *wpa_s)
6773{
6774 if (wpa_s->p2p_group_interface != P2P_GROUP_INTERFACE_CLIENT &&
6775 !wpa_s->p2p_in_provisioning)
6776 return 0; /* not P2P client operation */
6777
6778 wpa_printf(MSG_DEBUG, "P2P: Terminate connection due to WPS PBC "
6779 "session overlap");
6780 if (wpa_s != wpa_s->parent)
6781 wpa_msg_ctrl(wpa_s->parent, MSG_INFO, WPS_EVENT_OVERLAP);
Dmitry Shmidt34af3062013-07-11 10:46:32 -07006782 wpas_p2p_group_formation_failed(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006783 return 1;
6784}
6785
6786
Dmitry Shmidtdf5a7e42014-04-02 12:59:59 -07006787void wpas_p2p_pbc_overlap_cb(void *eloop_ctx, void *timeout_ctx)
6788{
6789 struct wpa_supplicant *wpa_s = eloop_ctx;
6790 wpas_p2p_notif_pbc_overlap(wpa_s);
6791}
6792
6793
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006794void wpas_p2p_update_channel_list(struct wpa_supplicant *wpa_s)
6795{
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07006796 struct p2p_channels chan, cli_chan;
Dmitry Shmidt684785c2014-05-12 13:34:29 -07006797 struct wpa_supplicant *ifs;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006798
6799 if (wpa_s->global == NULL || wpa_s->global->p2p == NULL)
6800 return;
6801
6802 os_memset(&chan, 0, sizeof(chan));
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07006803 os_memset(&cli_chan, 0, sizeof(cli_chan));
6804 if (wpas_p2p_setup_channels(wpa_s, &chan, &cli_chan)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006805 wpa_printf(MSG_ERROR, "P2P: Failed to update supported "
6806 "channel list");
6807 return;
6808 }
6809
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07006810 p2p_update_channel_list(wpa_s->global->p2p, &chan, &cli_chan);
Dmitry Shmidt684785c2014-05-12 13:34:29 -07006811
6812 for (ifs = wpa_s->global->ifaces; ifs; ifs = ifs->next) {
6813 int freq;
6814 if (!ifs->current_ssid ||
6815 !ifs->current_ssid->p2p_group ||
6816 (ifs->current_ssid->mode != WPAS_MODE_P2P_GO &&
6817 ifs->current_ssid->mode != WPAS_MODE_P2P_GROUP_FORMATION))
6818 continue;
6819 freq = ifs->current_ssid->frequency;
6820 if (freq_included(&chan, freq)) {
6821 wpa_dbg(ifs, MSG_DEBUG,
6822 "P2P GO operating frequency %d MHz in valid range",
6823 freq);
6824 continue;
6825 }
6826
6827 wpa_dbg(ifs, MSG_DEBUG,
6828 "P2P GO operating in invalid frequency %d MHz", freq);
6829 /* TODO: Consider using CSA or removing the group within
6830 * wpa_supplicant */
6831 wpa_msg(ifs, MSG_INFO, P2P_EVENT_REMOVE_AND_REFORM_GROUP);
6832 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006833}
6834
6835
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08006836static void wpas_p2p_scan_res_ignore(struct wpa_supplicant *wpa_s,
6837 struct wpa_scan_results *scan_res)
6838{
6839 wpa_printf(MSG_DEBUG, "P2P: Ignore scan results");
6840}
6841
6842
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006843int wpas_p2p_cancel(struct wpa_supplicant *wpa_s)
6844{
6845 struct wpa_global *global = wpa_s->global;
6846 int found = 0;
6847 const u8 *peer;
6848
6849 if (global->p2p == NULL)
6850 return -1;
6851
6852 wpa_printf(MSG_DEBUG, "P2P: Request to cancel group formation");
6853
6854 if (wpa_s->pending_interface_name[0] &&
6855 !is_zero_ether_addr(wpa_s->pending_interface_addr))
6856 found = 1;
6857
6858 peer = p2p_get_go_neg_peer(global->p2p);
6859 if (peer) {
6860 wpa_printf(MSG_DEBUG, "P2P: Unauthorize pending GO Neg peer "
6861 MACSTR, MAC2STR(peer));
6862 p2p_unauthorize(global->p2p, peer);
Dmitry Shmidt04949592012-07-19 12:16:46 -07006863 found = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006864 }
6865
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08006866 if (wpa_s->scan_res_handler == wpas_p2p_scan_res_join) {
6867 wpa_printf(MSG_DEBUG, "P2P: Stop pending scan for join");
6868 wpa_s->scan_res_handler = wpas_p2p_scan_res_ignore;
6869 found = 1;
6870 }
6871
6872 if (wpa_s->pending_pd_before_join) {
6873 wpa_printf(MSG_DEBUG, "P2P: Stop pending PD before join");
6874 wpa_s->pending_pd_before_join = 0;
6875 found = 1;
6876 }
6877
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006878 wpas_p2p_stop_find(wpa_s);
6879
6880 for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
6881 if (wpa_s == global->p2p_group_formation &&
6882 (wpa_s->p2p_in_provisioning ||
6883 wpa_s->parent->pending_interface_type ==
6884 WPA_IF_P2P_CLIENT)) {
6885 wpa_printf(MSG_DEBUG, "P2P: Interface %s in group "
6886 "formation found - cancelling",
6887 wpa_s->ifname);
6888 found = 1;
6889 eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
6890 wpa_s->parent, NULL);
Jouni Malinenadddfc42012-10-03 14:31:41 -07006891 if (wpa_s->p2p_in_provisioning) {
6892 wpas_group_formation_completed(wpa_s, 0);
6893 break;
6894 }
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07006895 wpas_p2p_group_delete(wpa_s,
6896 P2P_GROUP_REMOVAL_REQUESTED);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006897 break;
Dmitry Shmidt21de2142014-04-08 10:50:52 -07006898 } else if (wpa_s->p2p_in_invitation) {
6899 wpa_printf(MSG_DEBUG, "P2P: Interface %s in invitation found - cancelling",
6900 wpa_s->ifname);
6901 found = 1;
6902 wpas_p2p_group_formation_failed(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006903 }
6904 }
6905
6906 if (!found) {
6907 wpa_printf(MSG_DEBUG, "P2P: No ongoing group formation found");
6908 return -1;
6909 }
6910
6911 return 0;
6912}
6913
6914
6915void wpas_p2p_interface_unavailable(struct wpa_supplicant *wpa_s)
6916{
6917 if (wpa_s->current_ssid == NULL || !wpa_s->current_ssid->p2p_group)
6918 return;
6919
6920 wpa_printf(MSG_DEBUG, "P2P: Remove group due to driver resource not "
6921 "being available anymore");
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07006922 wpas_p2p_group_delete(wpa_s, P2P_GROUP_REMOVAL_UNAVAILABLE);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006923}
6924
6925
6926void wpas_p2p_update_best_channels(struct wpa_supplicant *wpa_s,
6927 int freq_24, int freq_5, int freq_overall)
6928{
6929 struct p2p_data *p2p = wpa_s->global->p2p;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08006930 if (p2p == NULL)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006931 return;
6932 p2p_set_best_channels(p2p, freq_24, freq_5, freq_overall);
6933}
6934
6935
6936int wpas_p2p_unauthorize(struct wpa_supplicant *wpa_s, const char *addr)
6937{
6938 u8 peer[ETH_ALEN];
6939 struct p2p_data *p2p = wpa_s->global->p2p;
6940
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08006941 if (p2p == NULL)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006942 return -1;
6943
6944 if (hwaddr_aton(addr, peer))
6945 return -1;
6946
6947 return p2p_unauthorize(p2p, peer);
6948}
6949
6950
6951/**
6952 * wpas_p2p_disconnect - Disconnect from a P2P Group
6953 * @wpa_s: Pointer to wpa_supplicant data
6954 * Returns: 0 on success, -1 on failure
6955 *
6956 * This can be used to disconnect from a group in which the local end is a P2P
6957 * Client or to end a P2P Group in case the local end is the Group Owner. If a
6958 * virtual network interface was created for this group, that interface will be
6959 * removed. Otherwise, only the configured P2P group network will be removed
6960 * from the interface.
6961 */
6962int wpas_p2p_disconnect(struct wpa_supplicant *wpa_s)
6963{
6964
6965 if (wpa_s == NULL)
6966 return -1;
6967
Jouni Malinen2b89da82012-08-31 22:04:41 +03006968 return wpas_p2p_group_delete(wpa_s, P2P_GROUP_REMOVAL_REQUESTED) < 0 ?
6969 -1 : 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006970}
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006971
6972
6973int wpas_p2p_in_progress(struct wpa_supplicant *wpa_s)
6974{
Dmitry Shmidtf8623282013-02-20 14:34:59 -08006975 int ret;
6976
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006977 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
6978 return 0;
6979
Dmitry Shmidtf8623282013-02-20 14:34:59 -08006980 ret = p2p_in_progress(wpa_s->global->p2p);
6981 if (ret == 0) {
6982 /*
6983 * Check whether there is an ongoing WPS provisioning step (or
6984 * other parts of group formation) on another interface since
6985 * p2p_in_progress() does not report this to avoid issues for
6986 * scans during such provisioning step.
6987 */
6988 if (wpa_s->global->p2p_group_formation &&
6989 wpa_s->global->p2p_group_formation != wpa_s) {
6990 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Another interface (%s) "
6991 "in group formation",
6992 wpa_s->global->p2p_group_formation->ifname);
6993 ret = 1;
6994 }
6995 }
6996
Dmitry Shmidt92c368d2013-08-29 12:37:21 -07006997 if (!ret && wpa_s->global->p2p_go_wait_client.sec) {
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08006998 struct os_reltime now;
6999 os_get_reltime(&now);
7000 if (os_reltime_expired(&now, &wpa_s->global->p2p_go_wait_client,
7001 P2P_MAX_INITIAL_CONN_WAIT_GO)) {
Dmitry Shmidt92c368d2013-08-29 12:37:21 -07007002 /* Wait for the first client has expired */
7003 wpa_s->global->p2p_go_wait_client.sec = 0;
7004 } else {
7005 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Waiting for initial client connection during group formation");
7006 ret = 1;
7007 }
7008 }
7009
Dmitry Shmidtf8623282013-02-20 14:34:59 -08007010 return ret;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007011}
7012
7013
7014void wpas_p2p_network_removed(struct wpa_supplicant *wpa_s,
7015 struct wpa_ssid *ssid)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007016{
7017 if (wpa_s->p2p_in_provisioning && ssid->p2p_group &&
7018 eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
7019 wpa_s->parent, NULL) > 0) {
Dmitry Shmidt04949592012-07-19 12:16:46 -07007020 /**
7021 * Remove the network by scheduling the group formation
7022 * timeout to happen immediately. The teardown code
7023 * needs to be scheduled to run asynch later so that we
7024 * don't delete data from under ourselves unexpectedly.
7025 * Calling wpas_p2p_group_formation_timeout directly
7026 * causes a series of crashes in WPS failure scenarios.
7027 */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007028 wpa_printf(MSG_DEBUG, "P2P: Canceled group formation due to "
7029 "P2P group network getting removed");
Dmitry Shmidt04949592012-07-19 12:16:46 -07007030 eloop_register_timeout(0, 0, wpas_p2p_group_formation_timeout,
7031 wpa_s->parent, NULL);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007032 }
7033}
7034
7035
7036struct wpa_ssid * wpas_p2p_get_persistent(struct wpa_supplicant *wpa_s,
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08007037 const u8 *addr, const u8 *ssid,
7038 size_t ssid_len)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007039{
7040 struct wpa_ssid *s;
7041 size_t i;
7042
7043 for (s = wpa_s->conf->ssid; s; s = s->next) {
7044 if (s->disabled != 2)
7045 continue;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08007046 if (ssid &&
7047 (ssid_len != s->ssid_len ||
7048 os_memcmp(ssid, s->ssid, ssid_len) != 0))
7049 continue;
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07007050 if (addr == NULL) {
7051 if (s->mode == WPAS_MODE_P2P_GO)
7052 return s;
7053 continue;
7054 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007055 if (os_memcmp(s->bssid, addr, ETH_ALEN) == 0)
7056 return s; /* peer is GO in the persistent group */
7057 if (s->mode != WPAS_MODE_P2P_GO || s->p2p_client_list == NULL)
7058 continue;
7059 for (i = 0; i < s->num_p2p_clients; i++) {
Dmitry Shmidtff787d52015-01-12 13:01:47 -08007060 if (os_memcmp(s->p2p_client_list + i * 2 * ETH_ALEN,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007061 addr, ETH_ALEN) == 0)
7062 return s; /* peer is P2P client in persistent
7063 * group */
7064 }
7065 }
7066
7067 return NULL;
7068}
7069
7070
7071void wpas_p2p_notify_ap_sta_authorized(struct wpa_supplicant *wpa_s,
7072 const u8 *addr)
7073{
Dmitry Shmidt56052862013-10-04 10:23:25 -07007074 if (eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
7075 wpa_s->parent, NULL) > 0) {
7076 /*
7077 * This can happen if WPS provisioning step is not terminated
7078 * cleanly (e.g., P2P Client does not send WSC_Done). Since the
7079 * peer was able to connect, there is no need to time out group
7080 * formation after this, though. In addition, this is used with
7081 * the initial connection wait on the GO as a separate formation
7082 * timeout and as such, expected to be hit after the initial WPS
7083 * provisioning step.
7084 */
7085 wpa_printf(MSG_DEBUG, "P2P: Canceled P2P group formation timeout on data connection");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007086
7087 if (!wpa_s->p2p_go_group_formation_completed &&
7088 !wpa_s->group_formation_reported) {
7089 /*
7090 * GO has not yet notified group formation success since
7091 * the WPS step was not completed cleanly. Do that
7092 * notification now since the P2P Client was able to
7093 * connect and as such, must have received the
7094 * credential from the WPS step.
7095 */
7096 if (wpa_s->global->p2p)
7097 p2p_wps_success_cb(wpa_s->global->p2p, addr);
7098 wpas_group_formation_completed(wpa_s, 1);
7099 }
Dmitry Shmidt56052862013-10-04 10:23:25 -07007100 }
7101 if (!wpa_s->p2p_go_group_formation_completed) {
7102 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Marking group formation completed on GO on first data connection");
7103 wpa_s->p2p_go_group_formation_completed = 1;
7104 wpa_s->global->p2p_group_formation = NULL;
7105 wpa_s->p2p_in_provisioning = 0;
Dmitry Shmidt21de2142014-04-08 10:50:52 -07007106 wpa_s->p2p_in_invitation = 0;
Dmitry Shmidt56052862013-10-04 10:23:25 -07007107 }
Dmitry Shmidt92c368d2013-08-29 12:37:21 -07007108 wpa_s->global->p2p_go_wait_client.sec = 0;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007109 if (addr == NULL)
7110 return;
7111 wpas_p2p_add_persistent_group_client(wpa_s, addr);
7112}
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08007113
Dmitry Shmidt04949592012-07-19 12:16:46 -07007114
7115static void wpas_p2p_fallback_to_go_neg(struct wpa_supplicant *wpa_s,
7116 int group_added)
7117{
7118 struct wpa_supplicant *group = wpa_s;
Dmitry Shmidt04949592012-07-19 12:16:46 -07007119 if (wpa_s->global->p2p_group_formation)
7120 group = wpa_s->global->p2p_group_formation;
7121 wpa_s = wpa_s->parent;
7122 offchannel_send_action_done(wpa_s);
7123 if (group_added)
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07007124 wpas_p2p_group_delete(group, P2P_GROUP_REMOVAL_SILENT);
Dmitry Shmidt04949592012-07-19 12:16:46 -07007125 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Fall back to GO Negotiation");
7126 wpas_p2p_connect(wpa_s, wpa_s->pending_join_dev_addr, wpa_s->p2p_pin,
7127 wpa_s->p2p_wps_method, wpa_s->p2p_persistent_group, 0,
7128 0, 0, wpa_s->p2p_go_intent, wpa_s->p2p_connect_freq,
7129 wpa_s->p2p_persistent_id,
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07007130 wpa_s->p2p_pd_before_go_neg,
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07007131 wpa_s->p2p_go_ht40,
7132 wpa_s->p2p_go_vht);
Dmitry Shmidt04949592012-07-19 12:16:46 -07007133}
7134
7135
7136int wpas_p2p_scan_no_go_seen(struct wpa_supplicant *wpa_s)
7137{
7138 if (!wpa_s->p2p_fallback_to_go_neg ||
7139 wpa_s->p2p_in_provisioning <= 5)
7140 return 0;
7141
7142 if (wpas_p2p_peer_go(wpa_s, wpa_s->pending_join_dev_addr) > 0)
7143 return 0; /* peer operating as a GO */
7144
7145 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: GO not found for p2p_connect-auto - "
7146 "fallback to GO Negotiation");
7147 wpas_p2p_fallback_to_go_neg(wpa_s, 1);
7148
7149 return 1;
7150}
7151
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07007152
7153unsigned int wpas_p2p_search_delay(struct wpa_supplicant *wpa_s)
7154{
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07007155 struct wpa_supplicant *ifs;
7156
7157 if (wpa_s->wpa_state > WPA_SCANNING) {
7158 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Use %u ms search delay due to "
7159 "concurrent operation",
Dmitry Shmidt09f57ba2014-06-10 16:07:13 -07007160 wpa_s->conf->p2p_search_delay);
7161 return wpa_s->conf->p2p_search_delay;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07007162 }
7163
Dmitry Shmidt01904cf2013-12-05 11:08:35 -08007164 dl_list_for_each(ifs, &wpa_s->radio->ifaces, struct wpa_supplicant,
7165 radio_list) {
7166 if (ifs != wpa_s && ifs->wpa_state > WPA_SCANNING) {
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07007167 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Use %u ms search "
7168 "delay due to concurrent operation on "
7169 "interface %s",
Dmitry Shmidt09f57ba2014-06-10 16:07:13 -07007170 wpa_s->conf->p2p_search_delay,
7171 ifs->ifname);
7172 return wpa_s->conf->p2p_search_delay;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07007173 }
7174 }
7175
7176 return 0;
7177}
7178
Dmitry Shmidt37d4d6a2013-03-18 13:09:42 -07007179
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07007180static int wpas_p2p_remove_psk_entry(struct wpa_supplicant *wpa_s,
7181 struct wpa_ssid *s, const u8 *addr,
7182 int iface_addr)
7183{
7184 struct psk_list_entry *psk, *tmp;
7185 int changed = 0;
7186
7187 dl_list_for_each_safe(psk, tmp, &s->psk_list, struct psk_list_entry,
7188 list) {
7189 if ((iface_addr && !psk->p2p &&
7190 os_memcmp(addr, psk->addr, ETH_ALEN) == 0) ||
7191 (!iface_addr && psk->p2p &&
7192 os_memcmp(addr, psk->addr, ETH_ALEN) == 0)) {
7193 wpa_dbg(wpa_s, MSG_DEBUG,
7194 "P2P: Remove persistent group PSK list entry for "
7195 MACSTR " p2p=%u",
7196 MAC2STR(psk->addr), psk->p2p);
7197 dl_list_del(&psk->list);
7198 os_free(psk);
7199 changed++;
7200 }
7201 }
7202
7203 return changed;
7204}
7205
7206
7207void wpas_p2p_new_psk_cb(struct wpa_supplicant *wpa_s, const u8 *mac_addr,
7208 const u8 *p2p_dev_addr,
7209 const u8 *psk, size_t psk_len)
7210{
7211 struct wpa_ssid *ssid = wpa_s->current_ssid;
7212 struct wpa_ssid *persistent;
Dmitry Shmidt7832adb2014-04-29 10:53:02 -07007213 struct psk_list_entry *p, *last;
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07007214
7215 if (psk_len != sizeof(p->psk))
7216 return;
7217
7218 if (p2p_dev_addr) {
7219 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: New PSK for addr=" MACSTR
7220 " p2p_dev_addr=" MACSTR,
7221 MAC2STR(mac_addr), MAC2STR(p2p_dev_addr));
7222 if (is_zero_ether_addr(p2p_dev_addr))
7223 p2p_dev_addr = NULL;
7224 } else {
7225 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: New PSK for addr=" MACSTR,
7226 MAC2STR(mac_addr));
7227 }
7228
7229 if (ssid->mode == WPAS_MODE_P2P_GROUP_FORMATION) {
7230 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: new_psk_cb during group formation");
7231 /* To be added to persistent group once created */
7232 if (wpa_s->global->add_psk == NULL) {
7233 wpa_s->global->add_psk = os_zalloc(sizeof(*p));
7234 if (wpa_s->global->add_psk == NULL)
7235 return;
7236 }
7237 p = wpa_s->global->add_psk;
7238 if (p2p_dev_addr) {
7239 p->p2p = 1;
7240 os_memcpy(p->addr, p2p_dev_addr, ETH_ALEN);
7241 } else {
7242 p->p2p = 0;
7243 os_memcpy(p->addr, mac_addr, ETH_ALEN);
7244 }
7245 os_memcpy(p->psk, psk, psk_len);
7246 return;
7247 }
7248
7249 if (ssid->mode != WPAS_MODE_P2P_GO || !ssid->p2p_persistent_group) {
7250 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Ignore new_psk_cb on not-persistent GO");
7251 return;
7252 }
7253
7254 persistent = wpas_p2p_get_persistent(wpa_s->parent, NULL, ssid->ssid,
7255 ssid->ssid_len);
7256 if (!persistent) {
7257 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Could not find persistent group information to store the new PSK");
7258 return;
7259 }
7260
7261 p = os_zalloc(sizeof(*p));
7262 if (p == NULL)
7263 return;
7264 if (p2p_dev_addr) {
7265 p->p2p = 1;
7266 os_memcpy(p->addr, p2p_dev_addr, ETH_ALEN);
7267 } else {
7268 p->p2p = 0;
7269 os_memcpy(p->addr, mac_addr, ETH_ALEN);
7270 }
7271 os_memcpy(p->psk, psk, psk_len);
7272
Dmitry Shmidt7832adb2014-04-29 10:53:02 -07007273 if (dl_list_len(&persistent->psk_list) > P2P_MAX_STORED_CLIENTS &&
7274 (last = dl_list_last(&persistent->psk_list,
7275 struct psk_list_entry, list))) {
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07007276 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Remove oldest PSK entry for "
7277 MACSTR " (p2p=%u) to make room for a new one",
7278 MAC2STR(last->addr), last->p2p);
7279 dl_list_del(&last->list);
7280 os_free(last);
7281 }
7282
7283 wpas_p2p_remove_psk_entry(wpa_s->parent, persistent,
7284 p2p_dev_addr ? p2p_dev_addr : mac_addr,
7285 p2p_dev_addr == NULL);
7286 if (p2p_dev_addr) {
7287 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Add new PSK for p2p_dev_addr="
7288 MACSTR, MAC2STR(p2p_dev_addr));
7289 } else {
7290 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Add new PSK for addr=" MACSTR,
7291 MAC2STR(mac_addr));
7292 }
7293 dl_list_add(&persistent->psk_list, &p->list);
7294
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07007295 if (wpa_s->parent->conf->update_config &&
7296 wpa_config_write(wpa_s->parent->confname, wpa_s->parent->conf))
7297 wpa_printf(MSG_DEBUG, "P2P: Failed to update configuration");
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07007298}
7299
7300
7301static void wpas_p2p_remove_psk(struct wpa_supplicant *wpa_s,
7302 struct wpa_ssid *s, const u8 *addr,
7303 int iface_addr)
7304{
7305 int res;
7306
7307 res = wpas_p2p_remove_psk_entry(wpa_s, s, addr, iface_addr);
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08007308 if (res > 0 && wpa_s->conf->update_config &&
7309 wpa_config_write(wpa_s->confname, wpa_s->conf))
7310 wpa_dbg(wpa_s, MSG_DEBUG,
7311 "P2P: Failed to update configuration");
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07007312}
7313
7314
7315static void wpas_p2p_remove_client_go(struct wpa_supplicant *wpa_s,
7316 const u8 *peer, int iface_addr)
7317{
7318 struct hostapd_data *hapd;
7319 struct hostapd_wpa_psk *psk, *prev, *rem;
7320 struct sta_info *sta;
7321
7322 if (wpa_s->ap_iface == NULL || wpa_s->current_ssid == NULL ||
7323 wpa_s->current_ssid->mode != WPAS_MODE_P2P_GO)
7324 return;
7325
7326 /* Remove per-station PSK entry */
7327 hapd = wpa_s->ap_iface->bss[0];
7328 prev = NULL;
7329 psk = hapd->conf->ssid.wpa_psk;
7330 while (psk) {
7331 if ((iface_addr && os_memcmp(peer, psk->addr, ETH_ALEN) == 0) ||
7332 (!iface_addr &&
7333 os_memcmp(peer, psk->p2p_dev_addr, ETH_ALEN) == 0)) {
7334 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Remove operating group PSK entry for "
7335 MACSTR " iface_addr=%d",
7336 MAC2STR(peer), iface_addr);
7337 if (prev)
7338 prev->next = psk->next;
7339 else
7340 hapd->conf->ssid.wpa_psk = psk->next;
7341 rem = psk;
7342 psk = psk->next;
7343 os_free(rem);
7344 } else {
7345 prev = psk;
7346 psk = psk->next;
7347 }
7348 }
7349
7350 /* Disconnect from group */
7351 if (iface_addr)
7352 sta = ap_get_sta(hapd, peer);
7353 else
7354 sta = ap_get_sta_p2p(hapd, peer);
7355 if (sta) {
7356 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Disconnect peer " MACSTR
7357 " (iface_addr=%d) from group",
7358 MAC2STR(peer), iface_addr);
7359 hostapd_drv_sta_deauth(hapd, sta->addr,
7360 WLAN_REASON_DEAUTH_LEAVING);
7361 ap_sta_deauthenticate(hapd, sta, WLAN_REASON_DEAUTH_LEAVING);
7362 }
7363}
7364
7365
7366void wpas_p2p_remove_client(struct wpa_supplicant *wpa_s, const u8 *peer,
7367 int iface_addr)
7368{
7369 struct wpa_ssid *s;
7370 struct wpa_supplicant *w;
7371
7372 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Remove client " MACSTR, MAC2STR(peer));
7373
7374 /* Remove from any persistent group */
7375 for (s = wpa_s->parent->conf->ssid; s; s = s->next) {
7376 if (s->disabled != 2 || s->mode != WPAS_MODE_P2P_GO)
7377 continue;
7378 if (!iface_addr)
7379 wpas_remove_persistent_peer(wpa_s, s, peer, 0);
7380 wpas_p2p_remove_psk(wpa_s->parent, s, peer, iface_addr);
7381 }
7382
7383 /* Remove from any operating group */
7384 for (w = wpa_s->global->ifaces; w; w = w->next)
7385 wpas_p2p_remove_client_go(w, peer, iface_addr);
7386}
7387
7388
7389static void wpas_p2p_psk_failure_removal(void *eloop_ctx, void *timeout_ctx)
7390{
7391 struct wpa_supplicant *wpa_s = eloop_ctx;
7392 wpas_p2p_group_delete(wpa_s, P2P_GROUP_REMOVAL_PSK_FAILURE);
7393}
7394
7395
Dmitry Shmidt04f534e2013-12-09 15:50:16 -08007396static void wpas_p2p_group_freq_conflict(void *eloop_ctx, void *timeout_ctx)
7397{
7398 struct wpa_supplicant *wpa_s = eloop_ctx;
7399
7400 wpa_printf(MSG_DEBUG, "P2P: Frequency conflict - terminate group");
7401 wpas_p2p_group_delete(wpa_s, P2P_GROUP_REMOVAL_FREQ_CONFLICT);
7402}
7403
7404
7405int wpas_p2p_handle_frequency_conflicts(struct wpa_supplicant *wpa_s, int freq,
7406 struct wpa_ssid *ssid)
7407{
7408 struct wpa_supplicant *iface;
7409
7410 for (iface = wpa_s->global->ifaces; iface; iface = iface->next) {
7411 if (!iface->current_ssid ||
7412 iface->current_ssid->frequency == freq ||
7413 (iface->p2p_group_interface == NOT_P2P_GROUP_INTERFACE &&
7414 !iface->current_ssid->p2p_group))
7415 continue;
7416
7417 /* Remove the connection with least priority */
7418 if (!wpas_is_p2p_prioritized(iface)) {
7419 /* STA connection has priority over existing
7420 * P2P connection, so remove the interface. */
7421 wpa_printf(MSG_DEBUG, "P2P: Removing P2P connection due to single channel concurrent mode frequency conflict");
7422 eloop_register_timeout(0, 0,
7423 wpas_p2p_group_freq_conflict,
7424 iface, NULL);
7425 /* If connection in progress is P2P connection, do not
7426 * proceed for the connection. */
7427 if (wpa_s == iface)
7428 return -1;
7429 else
7430 return 0;
7431 } else {
7432 /* P2P connection has priority, disable the STA network
7433 */
7434 wpa_supplicant_disable_network(wpa_s->global->ifaces,
7435 ssid);
7436 wpa_msg(wpa_s->global->ifaces, MSG_INFO,
7437 WPA_EVENT_FREQ_CONFLICT " id=%d", ssid->id);
7438 os_memset(wpa_s->global->ifaces->pending_bssid, 0,
7439 ETH_ALEN);
7440 /* If P2P connection is in progress, continue
7441 * connecting...*/
7442 if (wpa_s == iface)
7443 return 0;
7444 else
7445 return -1;
7446 }
7447 }
7448
7449 return 0;
7450}
7451
7452
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07007453int wpas_p2p_4way_hs_failed(struct wpa_supplicant *wpa_s)
7454{
7455 struct wpa_ssid *ssid = wpa_s->current_ssid;
7456
7457 if (ssid == NULL || !ssid->p2p_group)
7458 return 0;
7459
7460 if (wpa_s->p2p_last_4way_hs_fail &&
7461 wpa_s->p2p_last_4way_hs_fail == ssid) {
7462 u8 go_dev_addr[ETH_ALEN];
7463 struct wpa_ssid *persistent;
7464
7465 if (wpas_p2p_persistent_group(wpa_s, go_dev_addr,
7466 ssid->ssid,
7467 ssid->ssid_len) <= 0) {
7468 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Could not determine whether 4-way handshake failures were for a persistent group");
7469 goto disconnect;
7470 }
7471
7472 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Two 4-way handshake failures for a P2P group - go_dev_addr="
7473 MACSTR, MAC2STR(go_dev_addr));
7474 persistent = wpas_p2p_get_persistent(wpa_s->parent, go_dev_addr,
7475 ssid->ssid,
7476 ssid->ssid_len);
7477 if (persistent == NULL || persistent->mode != WPAS_MODE_INFRA) {
7478 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: No matching persistent group stored");
7479 goto disconnect;
7480 }
7481 wpa_msg_global(wpa_s->parent, MSG_INFO,
7482 P2P_EVENT_PERSISTENT_PSK_FAIL "%d",
7483 persistent->id);
7484 disconnect:
7485 wpa_s->p2p_last_4way_hs_fail = NULL;
7486 /*
7487 * Remove the group from a timeout to avoid issues with caller
7488 * continuing to use the interface if this is on a P2P group
7489 * interface.
7490 */
7491 eloop_register_timeout(0, 0, wpas_p2p_psk_failure_removal,
7492 wpa_s, NULL);
7493 return 1;
7494 }
7495
7496 wpa_s->p2p_last_4way_hs_fail = ssid;
7497 return 0;
7498}
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08007499
7500
7501#ifdef CONFIG_WPS_NFC
7502
7503static struct wpabuf * wpas_p2p_nfc_handover(int ndef, struct wpabuf *wsc,
7504 struct wpabuf *p2p)
7505{
7506 struct wpabuf *ret;
7507 size_t wsc_len;
7508
7509 if (p2p == NULL) {
7510 wpabuf_free(wsc);
7511 wpa_printf(MSG_DEBUG, "P2P: No p2p buffer for handover");
7512 return NULL;
7513 }
7514
7515 wsc_len = wsc ? wpabuf_len(wsc) : 0;
7516 ret = wpabuf_alloc(2 + wsc_len + 2 + wpabuf_len(p2p));
7517 if (ret == NULL) {
7518 wpabuf_free(wsc);
7519 wpabuf_free(p2p);
7520 return NULL;
7521 }
7522
7523 wpabuf_put_be16(ret, wsc_len);
7524 if (wsc)
7525 wpabuf_put_buf(ret, wsc);
7526 wpabuf_put_be16(ret, wpabuf_len(p2p));
7527 wpabuf_put_buf(ret, p2p);
7528
7529 wpabuf_free(wsc);
7530 wpabuf_free(p2p);
7531 wpa_hexdump_buf(MSG_DEBUG,
7532 "P2P: Generated NFC connection handover message", ret);
7533
7534 if (ndef && ret) {
7535 struct wpabuf *tmp;
7536 tmp = ndef_build_p2p(ret);
7537 wpabuf_free(ret);
7538 if (tmp == NULL) {
7539 wpa_printf(MSG_DEBUG, "P2P: Failed to NDEF encapsulate handover request");
7540 return NULL;
7541 }
7542 ret = tmp;
7543 }
7544
7545 return ret;
7546}
7547
7548
7549static int wpas_p2p_cli_freq(struct wpa_supplicant *wpa_s,
7550 struct wpa_ssid **ssid, u8 *go_dev_addr)
7551{
7552 struct wpa_supplicant *iface;
7553
7554 if (go_dev_addr)
7555 os_memset(go_dev_addr, 0, ETH_ALEN);
7556 if (ssid)
7557 *ssid = NULL;
7558 for (iface = wpa_s->global->ifaces; iface; iface = iface->next) {
7559 if (iface->wpa_state < WPA_ASSOCIATING ||
7560 iface->current_ssid == NULL || iface->assoc_freq == 0 ||
7561 !iface->current_ssid->p2p_group ||
7562 iface->current_ssid->mode != WPAS_MODE_INFRA)
7563 continue;
7564 if (ssid)
7565 *ssid = iface->current_ssid;
7566 if (go_dev_addr)
7567 os_memcpy(go_dev_addr, iface->go_dev_addr, ETH_ALEN);
7568 return iface->assoc_freq;
7569 }
7570 return 0;
7571}
7572
7573
7574struct wpabuf * wpas_p2p_nfc_handover_req(struct wpa_supplicant *wpa_s,
7575 int ndef)
7576{
7577 struct wpabuf *wsc, *p2p;
7578 struct wpa_ssid *ssid;
7579 u8 go_dev_addr[ETH_ALEN];
7580 int cli_freq = wpas_p2p_cli_freq(wpa_s, &ssid, go_dev_addr);
7581
7582 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL) {
7583 wpa_printf(MSG_DEBUG, "P2P: P2P disabled - cannot build handover request");
7584 return NULL;
7585 }
7586
7587 if (wpa_s->conf->wps_nfc_dh_pubkey == NULL &&
7588 wps_nfc_gen_dh(&wpa_s->conf->wps_nfc_dh_pubkey,
7589 &wpa_s->conf->wps_nfc_dh_privkey) < 0) {
7590 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: No DH key available for handover request");
7591 return NULL;
7592 }
7593
7594 if (cli_freq == 0) {
7595 wsc = wps_build_nfc_handover_req_p2p(
7596 wpa_s->parent->wps, wpa_s->conf->wps_nfc_dh_pubkey);
7597 } else
7598 wsc = NULL;
7599 p2p = p2p_build_nfc_handover_req(wpa_s->global->p2p, cli_freq,
7600 go_dev_addr, ssid ? ssid->ssid : NULL,
7601 ssid ? ssid->ssid_len : 0);
7602
7603 return wpas_p2p_nfc_handover(ndef, wsc, p2p);
7604}
7605
7606
7607struct wpabuf * wpas_p2p_nfc_handover_sel(struct wpa_supplicant *wpa_s,
7608 int ndef, int tag)
7609{
7610 struct wpabuf *wsc, *p2p;
7611 struct wpa_ssid *ssid;
7612 u8 go_dev_addr[ETH_ALEN];
7613 int cli_freq = wpas_p2p_cli_freq(wpa_s, &ssid, go_dev_addr);
7614
7615 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
7616 return NULL;
7617
7618 if (!tag && wpa_s->conf->wps_nfc_dh_pubkey == NULL &&
7619 wps_nfc_gen_dh(&wpa_s->conf->wps_nfc_dh_pubkey,
7620 &wpa_s->conf->wps_nfc_dh_privkey) < 0)
7621 return NULL;
7622
7623 if (cli_freq == 0) {
7624 wsc = wps_build_nfc_handover_sel_p2p(
7625 wpa_s->parent->wps,
7626 tag ? wpa_s->conf->wps_nfc_dev_pw_id :
7627 DEV_PW_NFC_CONNECTION_HANDOVER,
7628 wpa_s->conf->wps_nfc_dh_pubkey,
7629 tag ? wpa_s->conf->wps_nfc_dev_pw : NULL);
7630 } else
7631 wsc = NULL;
7632 p2p = p2p_build_nfc_handover_sel(wpa_s->global->p2p, cli_freq,
7633 go_dev_addr, ssid ? ssid->ssid : NULL,
7634 ssid ? ssid->ssid_len : 0);
7635
7636 return wpas_p2p_nfc_handover(ndef, wsc, p2p);
7637}
7638
7639
7640static int wpas_p2p_nfc_join_group(struct wpa_supplicant *wpa_s,
7641 struct p2p_nfc_params *params)
7642{
7643 wpa_printf(MSG_DEBUG, "P2P: Initiate join-group based on NFC "
7644 "connection handover (freq=%d)",
7645 params->go_freq);
7646
7647 if (params->go_freq && params->go_ssid_len) {
7648 wpa_s->p2p_wps_method = WPS_NFC;
7649 wpa_s->pending_join_wps_method = WPS_NFC;
7650 os_memset(wpa_s->pending_join_iface_addr, 0, ETH_ALEN);
7651 os_memcpy(wpa_s->pending_join_dev_addr, params->go_dev_addr,
7652 ETH_ALEN);
7653 return wpas_p2p_join_start(wpa_s, params->go_freq,
7654 params->go_ssid,
7655 params->go_ssid_len);
7656 }
7657
7658 return wpas_p2p_connect(wpa_s, params->peer->p2p_device_addr, NULL,
7659 WPS_NFC, 0, 0, 1, 0, wpa_s->conf->p2p_go_intent,
7660 params->go_freq, -1, 0, 1, 1);
7661}
7662
7663
7664static int wpas_p2p_nfc_auth_join(struct wpa_supplicant *wpa_s,
7665 struct p2p_nfc_params *params, int tag)
7666{
7667 int res, persistent;
7668 struct wpa_ssid *ssid;
7669
7670 wpa_printf(MSG_DEBUG, "P2P: Authorize join-group based on NFC "
7671 "connection handover");
7672 for (wpa_s = wpa_s->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
7673 ssid = wpa_s->current_ssid;
7674 if (ssid == NULL)
7675 continue;
7676 if (ssid->mode != WPAS_MODE_P2P_GO)
7677 continue;
7678 if (wpa_s->ap_iface == NULL)
7679 continue;
7680 break;
7681 }
7682 if (wpa_s == NULL) {
7683 wpa_printf(MSG_DEBUG, "P2P: Could not find GO interface");
7684 return -1;
7685 }
7686
7687 if (wpa_s->parent->p2p_oob_dev_pw_id !=
7688 DEV_PW_NFC_CONNECTION_HANDOVER &&
7689 !wpa_s->parent->p2p_oob_dev_pw) {
7690 wpa_printf(MSG_DEBUG, "P2P: No NFC Dev Pw known");
7691 return -1;
7692 }
7693 res = wpas_ap_wps_add_nfc_pw(
7694 wpa_s, wpa_s->parent->p2p_oob_dev_pw_id,
7695 wpa_s->parent->p2p_oob_dev_pw,
7696 wpa_s->parent->p2p_peer_oob_pk_hash_known ?
7697 wpa_s->parent->p2p_peer_oob_pubkey_hash : NULL);
7698 if (res)
7699 return res;
7700
7701 if (!tag) {
7702 wpa_printf(MSG_DEBUG, "P2P: Negotiated handover - wait for peer to join without invitation");
7703 return 0;
7704 }
7705
7706 if (!params->peer ||
7707 !(params->peer->dev_capab & P2P_DEV_CAPAB_INVITATION_PROCEDURE))
7708 return 0;
7709
7710 wpa_printf(MSG_DEBUG, "P2P: Static handover - invite peer " MACSTR
7711 " to join", MAC2STR(params->peer->p2p_device_addr));
7712
7713 wpa_s->global->p2p_invite_group = wpa_s;
7714 persistent = ssid->p2p_persistent_group &&
7715 wpas_p2p_get_persistent(wpa_s->parent,
7716 params->peer->p2p_device_addr,
7717 ssid->ssid, ssid->ssid_len);
7718 wpa_s->parent->pending_invite_ssid_id = -1;
7719
7720 return p2p_invite(wpa_s->global->p2p, params->peer->p2p_device_addr,
7721 P2P_INVITE_ROLE_ACTIVE_GO, wpa_s->own_addr,
7722 ssid->ssid, ssid->ssid_len, ssid->frequency,
7723 wpa_s->global->p2p_dev_addr, persistent, 0,
7724 wpa_s->parent->p2p_oob_dev_pw_id);
7725}
7726
7727
7728static int wpas_p2p_nfc_init_go_neg(struct wpa_supplicant *wpa_s,
7729 struct p2p_nfc_params *params,
7730 int forced_freq)
7731{
7732 wpa_printf(MSG_DEBUG, "P2P: Initiate GO Negotiation based on NFC "
7733 "connection handover");
7734 return wpas_p2p_connect(wpa_s, params->peer->p2p_device_addr, NULL,
7735 WPS_NFC, 0, 0, 0, 0, wpa_s->conf->p2p_go_intent,
7736 forced_freq, -1, 0, 1, 1);
7737}
7738
7739
7740static int wpas_p2p_nfc_resp_go_neg(struct wpa_supplicant *wpa_s,
7741 struct p2p_nfc_params *params,
7742 int forced_freq)
7743{
7744 int res;
7745
7746 wpa_printf(MSG_DEBUG, "P2P: Authorize GO Negotiation based on NFC "
7747 "connection handover");
7748 res = wpas_p2p_connect(wpa_s, params->peer->p2p_device_addr, NULL,
7749 WPS_NFC, 0, 0, 0, 1, wpa_s->conf->p2p_go_intent,
7750 forced_freq, -1, 0, 1, 1);
7751 if (res)
7752 return res;
7753
7754 res = wpas_p2p_listen(wpa_s, 60);
7755 if (res) {
7756 p2p_unauthorize(wpa_s->global->p2p,
7757 params->peer->p2p_device_addr);
7758 }
7759
7760 return res;
7761}
7762
7763
7764static int wpas_p2p_nfc_connection_handover(struct wpa_supplicant *wpa_s,
7765 const struct wpabuf *data,
7766 int sel, int tag, int forced_freq)
7767{
7768 const u8 *pos, *end;
7769 u16 len, id;
7770 struct p2p_nfc_params params;
7771 int res;
7772
7773 os_memset(&params, 0, sizeof(params));
7774 params.sel = sel;
7775
7776 wpa_hexdump_buf(MSG_DEBUG, "P2P: Received NFC tag payload", data);
7777
7778 pos = wpabuf_head(data);
7779 end = pos + wpabuf_len(data);
7780
7781 if (end - pos < 2) {
7782 wpa_printf(MSG_DEBUG, "P2P: Not enough data for Length of WSC "
7783 "attributes");
7784 return -1;
7785 }
7786 len = WPA_GET_BE16(pos);
7787 pos += 2;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007788 if (len > end - pos) {
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08007789 wpa_printf(MSG_DEBUG, "P2P: Not enough data for WSC "
7790 "attributes");
7791 return -1;
7792 }
7793 params.wsc_attr = pos;
7794 params.wsc_len = len;
7795 pos += len;
7796
7797 if (end - pos < 2) {
7798 wpa_printf(MSG_DEBUG, "P2P: Not enough data for Length of P2P "
7799 "attributes");
7800 return -1;
7801 }
7802 len = WPA_GET_BE16(pos);
7803 pos += 2;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007804 if (len > end - pos) {
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08007805 wpa_printf(MSG_DEBUG, "P2P: Not enough data for P2P "
7806 "attributes");
7807 return -1;
7808 }
7809 params.p2p_attr = pos;
7810 params.p2p_len = len;
7811 pos += len;
7812
7813 wpa_hexdump(MSG_DEBUG, "P2P: WSC attributes",
7814 params.wsc_attr, params.wsc_len);
7815 wpa_hexdump(MSG_DEBUG, "P2P: P2P attributes",
7816 params.p2p_attr, params.p2p_len);
7817 if (pos < end) {
7818 wpa_hexdump(MSG_DEBUG,
7819 "P2P: Ignored extra data after P2P attributes",
7820 pos, end - pos);
7821 }
7822
7823 res = p2p_process_nfc_connection_handover(wpa_s->global->p2p, &params);
7824 if (res)
7825 return res;
7826
7827 if (params.next_step == NO_ACTION)
7828 return 0;
7829
7830 if (params.next_step == BOTH_GO) {
7831 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_NFC_BOTH_GO "peer=" MACSTR,
7832 MAC2STR(params.peer->p2p_device_addr));
7833 return 0;
7834 }
7835
7836 if (params.next_step == PEER_CLIENT) {
7837 if (!is_zero_ether_addr(params.go_dev_addr)) {
7838 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_NFC_PEER_CLIENT
7839 "peer=" MACSTR " freq=%d go_dev_addr=" MACSTR
7840 " ssid=\"%s\"",
7841 MAC2STR(params.peer->p2p_device_addr),
7842 params.go_freq,
7843 MAC2STR(params.go_dev_addr),
7844 wpa_ssid_txt(params.go_ssid,
7845 params.go_ssid_len));
7846 } else {
7847 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_NFC_PEER_CLIENT
7848 "peer=" MACSTR " freq=%d",
7849 MAC2STR(params.peer->p2p_device_addr),
7850 params.go_freq);
7851 }
7852 return 0;
7853 }
7854
7855 if (wpas_p2p_cli_freq(wpa_s, NULL, NULL)) {
7856 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_NFC_WHILE_CLIENT "peer="
7857 MACSTR, MAC2STR(params.peer->p2p_device_addr));
7858 return 0;
7859 }
7860
7861 wpabuf_free(wpa_s->p2p_oob_dev_pw);
7862 wpa_s->p2p_oob_dev_pw = NULL;
7863
7864 if (params.oob_dev_pw_len < WPS_OOB_PUBKEY_HASH_LEN + 2) {
7865 wpa_printf(MSG_DEBUG, "P2P: No peer OOB Dev Pw "
7866 "received");
7867 return -1;
7868 }
7869
7870 id = WPA_GET_BE16(params.oob_dev_pw + WPS_OOB_PUBKEY_HASH_LEN);
7871 wpa_printf(MSG_DEBUG, "P2P: Peer OOB Dev Pw %u", id);
7872 wpa_hexdump(MSG_DEBUG, "P2P: Peer OOB Public Key hash",
7873 params.oob_dev_pw, WPS_OOB_PUBKEY_HASH_LEN);
7874 os_memcpy(wpa_s->p2p_peer_oob_pubkey_hash,
7875 params.oob_dev_pw, WPS_OOB_PUBKEY_HASH_LEN);
7876 wpa_s->p2p_peer_oob_pk_hash_known = 1;
7877
7878 if (tag) {
7879 if (id < 0x10) {
7880 wpa_printf(MSG_DEBUG, "P2P: Static handover - invalid "
7881 "peer OOB Device Password Id %u", id);
7882 return -1;
7883 }
7884 wpa_printf(MSG_DEBUG, "P2P: Static handover - use peer OOB "
7885 "Device Password Id %u", id);
7886 wpa_hexdump_key(MSG_DEBUG, "P2P: Peer OOB Device Password",
7887 params.oob_dev_pw + WPS_OOB_PUBKEY_HASH_LEN + 2,
7888 params.oob_dev_pw_len -
7889 WPS_OOB_PUBKEY_HASH_LEN - 2);
7890 wpa_s->p2p_oob_dev_pw_id = id;
7891 wpa_s->p2p_oob_dev_pw = wpabuf_alloc_copy(
7892 params.oob_dev_pw + WPS_OOB_PUBKEY_HASH_LEN + 2,
7893 params.oob_dev_pw_len -
7894 WPS_OOB_PUBKEY_HASH_LEN - 2);
7895 if (wpa_s->p2p_oob_dev_pw == NULL)
7896 return -1;
7897
7898 if (wpa_s->conf->wps_nfc_dh_pubkey == NULL &&
7899 wps_nfc_gen_dh(&wpa_s->conf->wps_nfc_dh_pubkey,
7900 &wpa_s->conf->wps_nfc_dh_privkey) < 0)
7901 return -1;
7902 } else {
7903 wpa_printf(MSG_DEBUG, "P2P: Using abbreviated WPS handshake "
7904 "without Device Password");
7905 wpa_s->p2p_oob_dev_pw_id = DEV_PW_NFC_CONNECTION_HANDOVER;
7906 }
7907
7908 switch (params.next_step) {
7909 case NO_ACTION:
7910 case BOTH_GO:
7911 case PEER_CLIENT:
7912 /* already covered above */
7913 return 0;
7914 case JOIN_GROUP:
7915 return wpas_p2p_nfc_join_group(wpa_s, &params);
7916 case AUTH_JOIN:
7917 return wpas_p2p_nfc_auth_join(wpa_s, &params, tag);
7918 case INIT_GO_NEG:
7919 return wpas_p2p_nfc_init_go_neg(wpa_s, &params, forced_freq);
7920 case RESP_GO_NEG:
7921 /* TODO: use own OOB Dev Pw */
7922 return wpas_p2p_nfc_resp_go_neg(wpa_s, &params, forced_freq);
7923 }
7924
7925 return -1;
7926}
7927
7928
7929int wpas_p2p_nfc_tag_process(struct wpa_supplicant *wpa_s,
7930 const struct wpabuf *data, int forced_freq)
7931{
7932 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
7933 return -1;
7934
7935 return wpas_p2p_nfc_connection_handover(wpa_s, data, 1, 1, forced_freq);
7936}
7937
7938
7939int wpas_p2p_nfc_report_handover(struct wpa_supplicant *wpa_s, int init,
7940 const struct wpabuf *req,
7941 const struct wpabuf *sel, int forced_freq)
7942{
7943 struct wpabuf *tmp;
7944 int ret;
7945
7946 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
7947 return -1;
7948
7949 wpa_printf(MSG_DEBUG, "NFC: P2P connection handover reported");
7950
7951 wpa_hexdump_ascii(MSG_DEBUG, "NFC: Req",
7952 wpabuf_head(req), wpabuf_len(req));
7953 wpa_hexdump_ascii(MSG_DEBUG, "NFC: Sel",
7954 wpabuf_head(sel), wpabuf_len(sel));
7955 if (forced_freq)
7956 wpa_printf(MSG_DEBUG, "NFC: Forced freq %d", forced_freq);
7957 tmp = ndef_parse_p2p(init ? sel : req);
7958 if (tmp == NULL) {
7959 wpa_printf(MSG_DEBUG, "P2P: Could not parse NDEF");
7960 return -1;
7961 }
7962
7963 ret = wpas_p2p_nfc_connection_handover(wpa_s, tmp, init, 0,
7964 forced_freq);
7965 wpabuf_free(tmp);
7966
7967 return ret;
7968}
7969
7970
7971int wpas_p2p_nfc_tag_enabled(struct wpa_supplicant *wpa_s, int enabled)
7972{
7973 const u8 *if_addr;
7974 int go_intent = wpa_s->conf->p2p_go_intent;
7975 struct wpa_supplicant *iface;
7976
7977 if (wpa_s->global->p2p == NULL)
7978 return -1;
7979
7980 if (!enabled) {
7981 wpa_printf(MSG_DEBUG, "P2P: Disable use of own NFC Tag");
7982 for (iface = wpa_s->global->ifaces; iface; iface = iface->next)
7983 {
7984 if (!iface->ap_iface)
7985 continue;
7986 hostapd_wps_nfc_token_disable(iface->ap_iface->bss[0]);
7987 }
7988 p2p_set_authorized_oob_dev_pw_id(wpa_s->global->p2p, 0,
7989 0, NULL);
7990 if (wpa_s->p2p_nfc_tag_enabled)
7991 wpas_p2p_remove_pending_group_interface(wpa_s);
7992 wpa_s->p2p_nfc_tag_enabled = 0;
7993 return 0;
7994 }
7995
7996 if (wpa_s->global->p2p_disabled)
7997 return -1;
7998
7999 if (wpa_s->conf->wps_nfc_dh_pubkey == NULL ||
8000 wpa_s->conf->wps_nfc_dh_privkey == NULL ||
8001 wpa_s->conf->wps_nfc_dev_pw == NULL ||
8002 wpa_s->conf->wps_nfc_dev_pw_id < 0x10) {
8003 wpa_printf(MSG_DEBUG, "P2P: NFC password token not configured "
8004 "to allow static handover cases");
8005 return -1;
8006 }
8007
8008 wpa_printf(MSG_DEBUG, "P2P: Enable use of own NFC Tag");
8009
8010 wpa_s->p2p_oob_dev_pw_id = wpa_s->conf->wps_nfc_dev_pw_id;
8011 wpabuf_free(wpa_s->p2p_oob_dev_pw);
8012 wpa_s->p2p_oob_dev_pw = wpabuf_dup(wpa_s->conf->wps_nfc_dev_pw);
8013 if (wpa_s->p2p_oob_dev_pw == NULL)
8014 return -1;
8015 wpa_s->p2p_peer_oob_pk_hash_known = 0;
8016
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -07008017 if (wpa_s->p2p_group_interface == P2P_GROUP_INTERFACE_GO ||
8018 wpa_s->p2p_group_interface == P2P_GROUP_INTERFACE_CLIENT) {
8019 /*
8020 * P2P Group Interface present and the command came on group
8021 * interface, so enable the token for the current interface.
8022 */
8023 wpa_s->create_p2p_iface = 0;
8024 } else {
8025 wpa_s->create_p2p_iface = wpas_p2p_create_iface(wpa_s);
8026 }
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08008027
8028 if (wpa_s->create_p2p_iface) {
8029 enum wpa_driver_if_type iftype;
8030 /* Prepare to add a new interface for the group */
8031 iftype = WPA_IF_P2P_GROUP;
8032 if (go_intent == 15)
8033 iftype = WPA_IF_P2P_GO;
8034 if (wpas_p2p_add_group_interface(wpa_s, iftype) < 0) {
8035 wpa_printf(MSG_ERROR, "P2P: Failed to allocate a new "
8036 "interface for the group");
8037 return -1;
8038 }
8039
8040 if_addr = wpa_s->pending_interface_addr;
8041 } else
8042 if_addr = wpa_s->own_addr;
8043
8044 wpa_s->p2p_nfc_tag_enabled = enabled;
8045
8046 for (iface = wpa_s->global->ifaces; iface; iface = iface->next) {
8047 struct hostapd_data *hapd;
8048 if (iface->ap_iface == NULL)
8049 continue;
8050 hapd = iface->ap_iface->bss[0];
8051 wpabuf_free(hapd->conf->wps_nfc_dh_pubkey);
8052 hapd->conf->wps_nfc_dh_pubkey =
8053 wpabuf_dup(wpa_s->conf->wps_nfc_dh_pubkey);
8054 wpabuf_free(hapd->conf->wps_nfc_dh_privkey);
8055 hapd->conf->wps_nfc_dh_privkey =
8056 wpabuf_dup(wpa_s->conf->wps_nfc_dh_privkey);
8057 wpabuf_free(hapd->conf->wps_nfc_dev_pw);
8058 hapd->conf->wps_nfc_dev_pw =
8059 wpabuf_dup(wpa_s->conf->wps_nfc_dev_pw);
8060 hapd->conf->wps_nfc_dev_pw_id = wpa_s->conf->wps_nfc_dev_pw_id;
8061
8062 if (hostapd_wps_nfc_token_enable(iface->ap_iface->bss[0]) < 0) {
8063 wpa_dbg(iface, MSG_DEBUG,
8064 "P2P: Failed to enable NFC Tag for GO");
8065 }
8066 }
8067 p2p_set_authorized_oob_dev_pw_id(
8068 wpa_s->global->p2p, wpa_s->conf->wps_nfc_dev_pw_id, go_intent,
8069 if_addr);
8070
8071 return 0;
8072}
8073
8074#endif /* CONFIG_WPS_NFC */
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07008075
8076
8077static void wpas_p2p_optimize_listen_channel(struct wpa_supplicant *wpa_s,
8078 struct wpa_used_freq_data *freqs,
8079 unsigned int num)
8080{
8081 u8 curr_chan, cand, chan;
8082 unsigned int i;
8083
8084 curr_chan = p2p_get_listen_channel(wpa_s->global->p2p);
8085 for (i = 0, cand = 0; i < num; i++) {
8086 ieee80211_freq_to_chan(freqs[i].freq, &chan);
8087 if (curr_chan == chan) {
8088 cand = 0;
8089 break;
8090 }
8091
8092 if (chan == 1 || chan == 6 || chan == 11)
8093 cand = chan;
8094 }
8095
8096 if (cand) {
8097 wpa_dbg(wpa_s, MSG_DEBUG,
8098 "P2P: Update Listen channel to %u baased on operating channel",
8099 cand);
8100 p2p_set_listen_channel(wpa_s->global->p2p, 81, cand, 0);
8101 }
8102}
8103
8104
8105void wpas_p2p_indicate_state_change(struct wpa_supplicant *wpa_s)
8106{
8107 struct wpa_used_freq_data *freqs;
8108 unsigned int num = wpa_s->num_multichan_concurrent;
8109
8110 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
8111 return;
8112
8113 /*
8114 * If possible, optimize the Listen channel to be a channel that is
8115 * already used by one of the other interfaces.
8116 */
8117 if (!wpa_s->conf->p2p_optimize_listen_chan)
8118 return;
8119
8120 if (!wpa_s->current_ssid || wpa_s->wpa_state != WPA_COMPLETED)
8121 return;
8122
8123 freqs = os_calloc(num, sizeof(struct wpa_used_freq_data));
8124 if (!freqs)
8125 return;
8126
8127 num = get_shared_radio_freqs_data(wpa_s, freqs, num);
8128
8129 wpas_p2p_optimize_listen_channel(wpa_s, freqs, num);
8130 os_free(freqs);
8131}
8132
8133
8134void wpas_p2p_deinit_iface(struct wpa_supplicant *wpa_s)
8135{
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07008136 if (wpa_s == wpa_s->global->p2p_init_wpa_s && wpa_s->global->p2p) {
8137 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Disable P2P since removing "
8138 "the management interface is being removed");
8139 wpas_p2p_deinit_global(wpa_s->global);
8140 }
8141}
8142
8143
8144void wpas_p2p_ap_deinit(struct wpa_supplicant *wpa_s)
8145{
8146 if (wpa_s->ap_iface->bss)
8147 wpa_s->ap_iface->bss[0]->p2p_group = NULL;
8148 wpas_p2p_group_deinit(wpa_s);
8149}