blob: 346326f06881192b615b5912dac0761ad2c0da75 [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"
Dmitry Shmidt203eadb2015-03-05 14:16:04 -080025#include "ap/dfs.h"
Jouni Malinen75ecf522011-06-27 15:19:46 -070026#include "eapol_supp/eapol_supp_sm.h"
27#include "rsn_supp/wpa.h"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070028#include "wpa_supplicant_i.h"
29#include "driver_i.h"
30#include "ap.h"
31#include "config_ssid.h"
32#include "config.h"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070033#include "notify.h"
34#include "scan.h"
35#include "bss.h"
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080036#include "offchannel.h"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070037#include "wps_supplicant.h"
38#include "p2p_supplicant.h"
Dmitry Shmidt04f534e2013-12-09 15:50:16 -080039#include "wifi_display.h"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070040
41
42/*
43 * How many times to try to scan to find the GO before giving up on join
44 * request.
45 */
46#define P2P_MAX_JOIN_SCAN_ATTEMPTS 10
47
Dmitry Shmidt04949592012-07-19 12:16:46 -070048#define P2P_AUTO_PD_SCAN_ATTEMPTS 5
49
Dmitry Shmidtd80a4012015-11-05 16:35:40 -080050/**
51 * Defines time interval in seconds when a GO needs to evacuate a frequency that
52 * it is currently using, but is no longer valid for P2P use cases.
53 */
54#define P2P_GO_FREQ_CHANGE_TIME 5
55
56/**
57 * Defines CSA parameters which are used when GO evacuates the no longer valid
58 * channel (and if the driver supports channel switch).
59 */
60#define P2P_GO_CSA_COUNT 7
61#define P2P_GO_CSA_BLOCK_TX 0
62
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080063#ifndef P2P_MAX_CLIENT_IDLE
64/*
65 * How many seconds to try to reconnect to the GO when connection in P2P client
66 * role has been lost.
67 */
68#define P2P_MAX_CLIENT_IDLE 10
69#endif /* P2P_MAX_CLIENT_IDLE */
70
Dmitry Shmidt04949592012-07-19 12:16:46 -070071#ifndef P2P_MAX_INITIAL_CONN_WAIT
72/*
73 * How many seconds to wait for initial 4-way handshake to get completed after
Dmitry Shmidt15907092014-03-25 10:42:57 -070074 * WPS provisioning step or after the re-invocation of a persistent group on a
75 * P2P Client.
Dmitry Shmidt04949592012-07-19 12:16:46 -070076 */
77#define P2P_MAX_INITIAL_CONN_WAIT 10
78#endif /* P2P_MAX_INITIAL_CONN_WAIT */
79
Dmitry Shmidt92c368d2013-08-29 12:37:21 -070080#ifndef P2P_MAX_INITIAL_CONN_WAIT_GO
81/*
82 * How many seconds to wait for initial 4-way handshake to get completed after
83 * WPS provisioning step on the GO. This controls the extra time the P2P
84 * operation is considered to be in progress (e.g., to delay other scans) after
85 * WPS provisioning has been completed on the GO during group formation.
86 */
87#define P2P_MAX_INITIAL_CONN_WAIT_GO 10
88#endif /* P2P_MAX_INITIAL_CONN_WAIT_GO */
89
Dmitry Shmidt56052862013-10-04 10:23:25 -070090#ifndef P2P_MAX_INITIAL_CONN_WAIT_GO_REINVOKE
91/*
92 * How many seconds to wait for initial 4-way handshake to get completed after
93 * re-invocation of a persistent group on the GO when the client is expected
94 * to connect automatically (no user interaction).
95 */
96#define P2P_MAX_INITIAL_CONN_WAIT_GO_REINVOKE 15
97#endif /* P2P_MAX_INITIAL_CONN_WAIT_GO_REINVOKE */
98
Dmitry Shmidt34af3062013-07-11 10:46:32 -070099#define P2P_MGMT_DEVICE_PREFIX "p2p-dev-"
100
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800101/*
102 * How many seconds to wait to re-attempt to move GOs, in case previous attempt
103 * was not possible.
104 */
105#define P2P_RECONSIDER_GO_MOVE_DELAY 30
106
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700107enum p2p_group_removal_reason {
108 P2P_GROUP_REMOVAL_UNKNOWN,
109 P2P_GROUP_REMOVAL_SILENT,
110 P2P_GROUP_REMOVAL_FORMATION_FAILED,
111 P2P_GROUP_REMOVAL_REQUESTED,
112 P2P_GROUP_REMOVAL_IDLE_TIMEOUT,
113 P2P_GROUP_REMOVAL_UNAVAILABLE,
114 P2P_GROUP_REMOVAL_GO_ENDING_SESSION,
Dmitry Shmidt04f534e2013-12-09 15:50:16 -0800115 P2P_GROUP_REMOVAL_PSK_FAILURE,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800116 P2P_GROUP_REMOVAL_FREQ_CONFLICT,
117 P2P_GROUP_REMOVAL_GO_LEAVE_CHANNEL
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700118};
119
Jouni Malinendc7b7132012-09-14 12:53:47 -0700120
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700121static void wpas_p2p_long_listen_timeout(void *eloop_ctx, void *timeout_ctx);
122static struct wpa_supplicant *
123wpas_p2p_get_group_iface(struct wpa_supplicant *wpa_s, int addr_allocated,
124 int go);
Dmitry Shmidtcf32e602014-01-28 10:57:39 -0800125static int wpas_p2p_join_start(struct wpa_supplicant *wpa_s, int freq,
126 const u8 *ssid, size_t ssid_len);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800127static int wpas_p2p_setup_freqs(struct wpa_supplicant *wpa_s, int freq,
128 int *force_freq, int *pref_freq, int go,
129 unsigned int *pref_freq_list,
130 unsigned int *num_pref_freq);
Dmitry Shmidt344abd32014-01-14 13:17:00 -0800131static void wpas_p2p_join_scan_req(struct wpa_supplicant *wpa_s, int freq,
132 const u8 *ssid, size_t ssid_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700133static void wpas_p2p_join_scan(void *eloop_ctx, void *timeout_ctx);
134static int wpas_p2p_join(struct wpa_supplicant *wpa_s, const u8 *iface_addr,
Dmitry Shmidt04949592012-07-19 12:16:46 -0700135 const u8 *dev_addr, enum p2p_wps_method wps_method,
Dmitry Shmidtcf32e602014-01-28 10:57:39 -0800136 int auto_join, int freq,
137 const u8 *ssid, size_t ssid_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700138static int wpas_p2p_create_iface(struct wpa_supplicant *wpa_s);
139static void wpas_p2p_cross_connect_setup(struct wpa_supplicant *wpa_s);
140static void wpas_p2p_group_idle_timeout(void *eloop_ctx, void *timeout_ctx);
141static void wpas_p2p_set_group_idle_timeout(struct wpa_supplicant *wpa_s);
Dmitry Shmidtf8623282013-02-20 14:34:59 -0800142static void wpas_p2p_group_formation_timeout(void *eloop_ctx,
143 void *timeout_ctx);
Dmitry Shmidt04f534e2013-12-09 15:50:16 -0800144static void wpas_p2p_group_freq_conflict(void *eloop_ctx, void *timeout_ctx);
Dmitry Shmidt7f656022015-02-25 14:36:37 -0800145static int wpas_p2p_fallback_to_go_neg(struct wpa_supplicant *wpa_s,
146 int group_added);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800147static void wpas_p2p_stop_find_oper(struct wpa_supplicant *wpa_s);
Dmitry Shmidtbd14a572014-02-18 10:33:49 -0800148static void wpas_stop_listen(void *ctx);
Dmitry Shmidt684785c2014-05-12 13:34:29 -0700149static void wpas_p2p_psk_failure_removal(void *eloop_ctx, void *timeout_ctx);
Dmitry Shmidt43cb5782014-06-16 16:23:22 -0700150static void wpas_p2p_group_deinit(struct wpa_supplicant *wpa_s);
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800151static int wpas_p2p_add_group_interface(struct wpa_supplicant *wpa_s,
152 enum wpa_driver_if_type type);
Dmitry Shmidta3dc3092015-06-23 11:21:28 -0700153static void wpas_p2p_group_formation_failed(struct wpa_supplicant *wpa_s,
154 int already_deleted);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800155static void wpas_p2p_optimize_listen_channel(struct wpa_supplicant *wpa_s,
156 struct wpa_used_freq_data *freqs,
157 unsigned int num);
158static void wpas_p2p_move_go(void *eloop_ctx, void *timeout_ctx);
159static int wpas_p2p_go_is_peer_freq(struct wpa_supplicant *wpa_s, int freq);
160static void
161wpas_p2p_consider_moving_gos(struct wpa_supplicant *wpa_s,
162 struct wpa_used_freq_data *freqs, unsigned int num,
163 enum wpas_p2p_channel_update_trig trig);
164static void wpas_p2p_reconsider_moving_go(void *eloop_ctx, void *timeout_ctx);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700165
166
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -0700167/*
168 * Get the number of concurrent channels that the HW can operate, but that are
169 * currently not in use by any of the wpa_supplicant interfaces.
170 */
171static int wpas_p2p_num_unused_channels(struct wpa_supplicant *wpa_s)
172{
173 int *freqs;
Dmitry Shmidtb96dad42013-11-05 10:07:29 -0800174 int num, unused;
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -0700175
176 freqs = os_calloc(wpa_s->num_multichan_concurrent, sizeof(int));
177 if (!freqs)
178 return -1;
179
180 num = get_shared_radio_freqs(wpa_s, freqs,
181 wpa_s->num_multichan_concurrent);
182 os_free(freqs);
183
Dmitry Shmidtb96dad42013-11-05 10:07:29 -0800184 unused = wpa_s->num_multichan_concurrent - num;
185 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: num_unused_channels: %d", unused);
186 return unused;
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -0700187}
188
189
190/*
191 * Get the frequencies that are currently in use by one or more of the virtual
192 * interfaces, and that are also valid for P2P operation.
193 */
Dmitry Shmidt43cb5782014-06-16 16:23:22 -0700194static unsigned int
195wpas_p2p_valid_oper_freqs(struct wpa_supplicant *wpa_s,
196 struct wpa_used_freq_data *p2p_freqs,
197 unsigned int len)
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -0700198{
Dmitry Shmidt43cb5782014-06-16 16:23:22 -0700199 struct wpa_used_freq_data *freqs;
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -0700200 unsigned int num, i, j;
201
Dmitry Shmidt43cb5782014-06-16 16:23:22 -0700202 freqs = os_calloc(wpa_s->num_multichan_concurrent,
203 sizeof(struct wpa_used_freq_data));
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -0700204 if (!freqs)
Dmitry Shmidt43cb5782014-06-16 16:23:22 -0700205 return 0;
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -0700206
Dmitry Shmidt43cb5782014-06-16 16:23:22 -0700207 num = get_shared_radio_freqs_data(wpa_s, freqs,
208 wpa_s->num_multichan_concurrent);
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -0700209
Dmitry Shmidt43cb5782014-06-16 16:23:22 -0700210 os_memset(p2p_freqs, 0, sizeof(struct wpa_used_freq_data) * len);
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -0700211
212 for (i = 0, j = 0; i < num && j < len; i++) {
Dmitry Shmidt43cb5782014-06-16 16:23:22 -0700213 if (p2p_supported_freq(wpa_s->global->p2p, freqs[i].freq))
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -0700214 p2p_freqs[j++] = freqs[i];
215 }
216
217 os_free(freqs);
218
Dmitry Shmidt43cb5782014-06-16 16:23:22 -0700219 dump_freq_data(wpa_s, "valid for P2P", p2p_freqs, j);
Dmitry Shmidtb96dad42013-11-05 10:07:29 -0800220
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -0700221 return j;
222}
223
224
Dmitry Shmidt700a1372013-03-15 14:14:44 -0700225static void wpas_p2p_set_own_freq_preference(struct wpa_supplicant *wpa_s,
226 int freq)
227{
228 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
229 return;
Dmitry Shmidt8bd70b72015-05-26 16:02:19 -0700230
231 /* Use the wpa_s used to control the P2P Device operation */
232 wpa_s = wpa_s->global->p2p_init_wpa_s;
233
234 if (wpa_s->conf->p2p_ignore_shared_freq &&
Dmitry Shmidta0d265f2013-11-19 13:13:41 -0800235 freq > 0 && wpa_s->num_multichan_concurrent > 1 &&
236 wpas_p2p_num_unused_channels(wpa_s) > 0) {
237 wpa_printf(MSG_DEBUG, "P2P: Ignore own channel preference %d MHz due to p2p_ignore_shared_freq=1 configuration",
238 freq);
Dmitry Shmidt700a1372013-03-15 14:14:44 -0700239 freq = 0;
Dmitry Shmidta0d265f2013-11-19 13:13:41 -0800240 }
Dmitry Shmidt700a1372013-03-15 14:14:44 -0700241 p2p_set_own_freq_preference(wpa_s->global->p2p, freq);
242}
243
244
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700245static void wpas_p2p_scan_res_handler(struct wpa_supplicant *wpa_s,
246 struct wpa_scan_results *scan_res)
247{
248 size_t i;
249
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800250 if (wpa_s->p2p_scan_work) {
251 struct wpa_radio_work *work = wpa_s->p2p_scan_work;
252 wpa_s->p2p_scan_work = NULL;
253 radio_work_done(work);
254 }
255
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700256 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
257 return;
258
259 wpa_printf(MSG_DEBUG, "P2P: Scan results received (%d BSS)",
260 (int) scan_res->num);
261
262 for (i = 0; i < scan_res->num; i++) {
263 struct wpa_scan_res *bss = scan_res->res[i];
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800264 struct os_reltime time_tmp_age, entry_ts;
Dmitry Shmidt96571392013-10-14 12:54:46 -0700265 const u8 *ies;
266 size_t ies_len;
267
Dmitry Shmidtf8623282013-02-20 14:34:59 -0800268 time_tmp_age.sec = bss->age / 1000;
269 time_tmp_age.usec = (bss->age % 1000) * 1000;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800270 os_reltime_sub(&scan_res->fetch_time, &time_tmp_age, &entry_ts);
Dmitry Shmidt96571392013-10-14 12:54:46 -0700271
272 ies = (const u8 *) (bss + 1);
273 ies_len = bss->ie_len;
274 if (bss->beacon_ie_len > 0 &&
275 !wpa_scan_get_vendor_ie(bss, P2P_IE_VENDOR_TYPE) &&
276 wpa_scan_get_vendor_ie_beacon(bss, P2P_IE_VENDOR_TYPE)) {
277 wpa_printf(MSG_DEBUG, "P2P: Use P2P IE(s) from Beacon frame since no P2P IE(s) in Probe Response frames received for "
278 MACSTR, MAC2STR(bss->bssid));
279 ies = ies + ies_len;
280 ies_len = bss->beacon_ie_len;
281 }
282
283
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700284 if (p2p_scan_res_handler(wpa_s->global->p2p, bss->bssid,
Dmitry Shmidtf8623282013-02-20 14:34:59 -0800285 bss->freq, &entry_ts, bss->level,
Dmitry Shmidt96571392013-10-14 12:54:46 -0700286 ies, ies_len) > 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700287 break;
288 }
289
290 p2p_scan_res_handled(wpa_s->global->p2p);
291}
292
293
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800294static void wpas_p2p_trigger_scan_cb(struct wpa_radio_work *work, int deinit)
295{
296 struct wpa_supplicant *wpa_s = work->wpa_s;
297 struct wpa_driver_scan_params *params = work->ctx;
298 int ret;
299
300 if (deinit) {
Dmitry Shmidtbd14a572014-02-18 10:33:49 -0800301 if (!work->started) {
302 wpa_scan_free_params(params);
303 return;
304 }
305
306 wpa_s->p2p_scan_work = NULL;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800307 return;
308 }
309
Dmitry Shmidtebd93af2017-02-21 13:40:44 -0800310 if (wpa_s->clear_driver_scan_cache) {
311 wpa_printf(MSG_DEBUG,
312 "Request driver to clear scan cache due to local BSS flush");
313 params->only_new_results = 1;
314 }
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800315 ret = wpa_drv_scan(wpa_s, params);
Dmitry Shmidtabb90a32016-12-05 15:34:39 -0800316 if (ret == 0)
317 wpa_s->curr_scan_cookie = params->scan_cookie;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800318 wpa_scan_free_params(params);
319 work->ctx = NULL;
320 if (ret) {
321 radio_work_done(work);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800322 p2p_notify_scan_trigger_status(wpa_s->global->p2p, ret);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800323 return;
324 }
325
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800326 p2p_notify_scan_trigger_status(wpa_s->global->p2p, ret);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800327 os_get_reltime(&wpa_s->scan_trigger_time);
328 wpa_s->scan_res_handler = wpas_p2p_scan_res_handler;
329 wpa_s->own_scan_requested = 1;
Dmitry Shmidtebd93af2017-02-21 13:40:44 -0800330 wpa_s->clear_driver_scan_cache = 0;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800331 wpa_s->p2p_scan_work = work;
332}
333
334
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800335static int wpas_p2p_search_social_channel(struct wpa_supplicant *wpa_s,
336 int freq)
337{
338 if (wpa_s->global->p2p_24ghz_social_channels &&
339 (freq == 2412 || freq == 2437 || freq == 2462)) {
340 /*
341 * Search all social channels regardless of whether these have
342 * been disabled for P2P operating channel use to avoid missing
343 * peers.
344 */
345 return 1;
346 }
347 return p2p_supported_freq(wpa_s->global->p2p, freq);
348}
349
350
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700351static int wpas_p2p_scan(void *ctx, enum p2p_scan_type type, int freq,
352 unsigned int num_req_dev_types,
Dmitry Shmidt04949592012-07-19 12:16:46 -0700353 const u8 *req_dev_types, const u8 *dev_id, u16 pw_id)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700354{
355 struct wpa_supplicant *wpa_s = ctx;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800356 struct wpa_driver_scan_params *params = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700357 struct wpabuf *wps_ie, *ies;
Dmitry Shmidt661b4f72014-09-29 14:58:27 -0700358 unsigned int num_channels = 0;
359 int social_channels_freq[] = { 2412, 2437, 2462, 60480 };
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800360 size_t ielen;
Dmitry Shmidt661b4f72014-09-29 14:58:27 -0700361 u8 *n, i;
Dmitry Shmidt9c175262016-03-03 10:20:07 -0800362 unsigned int bands;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700363
364 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
365 return -1;
366
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800367 if (wpa_s->p2p_scan_work) {
368 wpa_dbg(wpa_s, MSG_INFO, "P2P: Reject scan trigger since one is already pending");
369 return -1;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700370 }
371
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800372 params = os_zalloc(sizeof(*params));
373 if (params == NULL)
374 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700375
376 /* P2P Wildcard SSID */
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800377 params->num_ssids = 1;
378 n = os_malloc(P2P_WILDCARD_SSID_LEN);
379 if (n == NULL)
380 goto fail;
381 os_memcpy(n, P2P_WILDCARD_SSID, P2P_WILDCARD_SSID_LEN);
382 params->ssids[0].ssid = n;
383 params->ssids[0].ssid_len = P2P_WILDCARD_SSID_LEN;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700384
385 wpa_s->wps->dev.p2p = 1;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700386 wps_ie = wps_build_probe_req_ie(pw_id, &wpa_s->wps->dev,
387 wpa_s->wps->uuid, WPS_REQ_ENROLLEE,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700388 num_req_dev_types, req_dev_types);
389 if (wps_ie == NULL)
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800390 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700391
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700392 switch (type) {
393 case P2P_SCAN_SOCIAL:
Dmitry Shmidt661b4f72014-09-29 14:58:27 -0700394 params->freqs = os_calloc(ARRAY_SIZE(social_channels_freq) + 1,
395 sizeof(int));
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800396 if (params->freqs == NULL)
397 goto fail;
Dmitry Shmidt661b4f72014-09-29 14:58:27 -0700398 for (i = 0; i < ARRAY_SIZE(social_channels_freq); i++) {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800399 if (wpas_p2p_search_social_channel(
400 wpa_s, social_channels_freq[i]))
Dmitry Shmidt661b4f72014-09-29 14:58:27 -0700401 params->freqs[num_channels++] =
402 social_channels_freq[i];
403 }
404 params->freqs[num_channels++] = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700405 break;
406 case P2P_SCAN_FULL:
407 break;
Dmitry Shmidt203eadb2015-03-05 14:16:04 -0800408 case P2P_SCAN_SPECIFIC:
409 params->freqs = os_calloc(2, sizeof(int));
410 if (params->freqs == NULL)
411 goto fail;
412 params->freqs[0] = freq;
413 params->freqs[1] = 0;
414 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700415 case P2P_SCAN_SOCIAL_PLUS_ONE:
Dmitry Shmidt661b4f72014-09-29 14:58:27 -0700416 params->freqs = os_calloc(ARRAY_SIZE(social_channels_freq) + 2,
417 sizeof(int));
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800418 if (params->freqs == NULL)
419 goto fail;
Dmitry Shmidt661b4f72014-09-29 14:58:27 -0700420 for (i = 0; i < ARRAY_SIZE(social_channels_freq); i++) {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800421 if (wpas_p2p_search_social_channel(
422 wpa_s, social_channels_freq[i]))
Dmitry Shmidt661b4f72014-09-29 14:58:27 -0700423 params->freqs[num_channels++] =
424 social_channels_freq[i];
425 }
426 if (p2p_supported_freq(wpa_s->global->p2p, freq))
427 params->freqs[num_channels++] = freq;
428 params->freqs[num_channels++] = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700429 break;
430 }
431
Dmitry Shmidt9c175262016-03-03 10:20:07 -0800432 ielen = p2p_scan_ie_buf_len(wpa_s->global->p2p);
433 ies = wpabuf_alloc(wpabuf_len(wps_ie) + ielen);
434 if (ies == NULL) {
435 wpabuf_free(wps_ie);
436 goto fail;
437 }
438 wpabuf_put_buf(ies, wps_ie);
439 wpabuf_free(wps_ie);
440
441 bands = wpas_get_bands(wpa_s, params->freqs);
442 p2p_scan_ie(wpa_s->global->p2p, ies, dev_id, bands);
443
444 params->p2p_probe = 1;
445 n = os_malloc(wpabuf_len(ies));
446 if (n == NULL) {
447 wpabuf_free(ies);
448 goto fail;
449 }
450 os_memcpy(n, wpabuf_head(ies), wpabuf_len(ies));
451 params->extra_ies = n;
452 params->extra_ies_len = wpabuf_len(ies);
453 wpabuf_free(ies);
454
Dmitry Shmidtbd14a572014-02-18 10:33:49 -0800455 radio_remove_works(wpa_s, "p2p-scan", 0);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800456 if (radio_add_work(wpa_s, 0, "p2p-scan", 0, wpas_p2p_trigger_scan_cb,
457 params) < 0)
458 goto fail;
459 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700460
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800461fail:
462 wpa_scan_free_params(params);
463 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700464}
465
466
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700467static enum wpa_driver_if_type wpas_p2p_if_type(int p2p_group_interface)
468{
469 switch (p2p_group_interface) {
470 case P2P_GROUP_INTERFACE_PENDING:
471 return WPA_IF_P2P_GROUP;
472 case P2P_GROUP_INTERFACE_GO:
473 return WPA_IF_P2P_GO;
474 case P2P_GROUP_INTERFACE_CLIENT:
475 return WPA_IF_P2P_CLIENT;
476 }
477
478 return WPA_IF_P2P_GROUP;
479}
480
481
482static struct wpa_supplicant * wpas_get_p2p_group(struct wpa_supplicant *wpa_s,
483 const u8 *ssid,
484 size_t ssid_len, int *go)
485{
486 struct wpa_ssid *s;
487
488 for (wpa_s = wpa_s->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
489 for (s = wpa_s->conf->ssid; s; s = s->next) {
490 if (s->disabled != 0 || !s->p2p_group ||
491 s->ssid_len != ssid_len ||
492 os_memcmp(ssid, s->ssid, ssid_len) != 0)
493 continue;
494 if (s->mode == WPAS_MODE_P2P_GO &&
495 s != wpa_s->current_ssid)
496 continue;
497 if (go)
498 *go = s->mode == WPAS_MODE_P2P_GO;
499 return wpa_s;
500 }
501 }
502
503 return NULL;
504}
505
506
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800507static void run_wpas_p2p_disconnect(void *eloop_ctx, void *timeout_ctx)
508{
509 struct wpa_supplicant *wpa_s = eloop_ctx;
510 wpa_printf(MSG_DEBUG,
511 "P2P: Complete previously requested removal of %s",
512 wpa_s->ifname);
513 wpas_p2p_disconnect(wpa_s);
514}
515
516
517static int wpas_p2p_disconnect_safely(struct wpa_supplicant *wpa_s,
518 struct wpa_supplicant *calling_wpa_s)
519{
520 if (calling_wpa_s == wpa_s && wpa_s &&
521 wpa_s->p2p_group_interface != NOT_P2P_GROUP_INTERFACE) {
522 /*
523 * The calling wpa_s instance is going to be removed. Do that
524 * from an eloop callback to keep the instance available until
525 * the caller has returned. This my be needed, e.g., to provide
526 * control interface responses on the per-interface socket.
527 */
528 if (eloop_register_timeout(0, 0, run_wpas_p2p_disconnect,
529 wpa_s, NULL) < 0)
530 return -1;
531 return 0;
532 }
533
534 return wpas_p2p_disconnect(wpa_s);
535}
536
537
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800538/* Determine total number of clients in active groups where we are the GO */
539static unsigned int p2p_group_go_member_count(struct wpa_supplicant *wpa_s)
540{
541 unsigned int count = 0;
542 struct wpa_ssid *s;
543
544 for (wpa_s = wpa_s->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
545 for (s = wpa_s->conf->ssid; s; s = s->next) {
546 wpa_printf(MSG_DEBUG,
547 "P2P: sup:%p ssid:%p disabled:%d p2p:%d mode:%d",
548 wpa_s, s, s->disabled, s->p2p_group,
549 s->mode);
550 if (!s->disabled && s->p2p_group &&
551 s->mode == WPAS_MODE_P2P_GO) {
552 count += p2p_get_group_num_members(
553 wpa_s->p2p_group);
554 }
555 }
556 }
557
558 return count;
559}
560
561
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800562static unsigned int p2p_is_active_persistent_group(struct wpa_supplicant *wpa_s)
563{
564 return !wpa_s->p2p_mgmt && wpa_s->current_ssid &&
565 !wpa_s->current_ssid->disabled &&
566 wpa_s->current_ssid->p2p_group &&
567 wpa_s->current_ssid->p2p_persistent_group;
568}
569
570
571static unsigned int p2p_is_active_persistent_go(struct wpa_supplicant *wpa_s)
572{
573 return p2p_is_active_persistent_group(wpa_s) &&
574 wpa_s->current_ssid->mode == WPAS_MODE_P2P_GO;
575}
576
577
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800578/* Find an interface for a P2P group where we are the GO */
579static struct wpa_supplicant *
580wpas_p2p_get_go_group(struct wpa_supplicant *wpa_s)
581{
582 struct wpa_supplicant *save = NULL;
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800583
584 if (!wpa_s)
585 return NULL;
586
587 for (wpa_s = wpa_s->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800588 if (!p2p_is_active_persistent_go(wpa_s))
589 continue;
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800590
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800591 /* Prefer a group with connected clients */
592 if (p2p_get_group_num_members(wpa_s->p2p_group))
593 return wpa_s;
594 save = wpa_s;
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800595 }
596
597 /* No group with connected clients, so pick the one without (if any) */
598 return save;
599}
600
601
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800602static unsigned int p2p_is_active_persistent_cli(struct wpa_supplicant *wpa_s)
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800603{
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800604 return p2p_is_active_persistent_group(wpa_s) &&
605 wpa_s->current_ssid->mode == WPAS_MODE_INFRA;
606}
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800607
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800608
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800609/* Find an interface for a P2P group where we are the P2P Client */
610static struct wpa_supplicant *
611wpas_p2p_get_cli_group(struct wpa_supplicant *wpa_s)
612{
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800613 for (wpa_s = wpa_s->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800614 if (p2p_is_active_persistent_cli(wpa_s))
615 return wpa_s;
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800616 }
617
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800618 return NULL;
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800619}
620
621
622/* Find a persistent group where we are the GO */
623static struct wpa_ssid *
624wpas_p2p_get_persistent_go(struct wpa_supplicant *wpa_s)
625{
626 struct wpa_ssid *s;
627
628 for (s = wpa_s->conf->ssid; s; s = s->next) {
629 if (s->disabled == 2 && s->mode == WPAS_MODE_P2P_GO)
630 return s;
631 }
632
633 return NULL;
634}
635
636
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800637static u8 p2ps_group_capability(void *ctx, u8 incoming, u8 role,
638 unsigned int *force_freq,
639 unsigned int *pref_freq)
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800640{
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800641 struct wpa_supplicant *wpa_s = ctx;
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800642 struct wpa_ssid *s;
643 u8 conncap = P2PS_SETUP_NONE;
644 unsigned int owned_members = 0;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800645 struct wpa_supplicant *go_wpa_s, *cli_wpa_s;
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800646 struct wpa_ssid *persistent_go;
647 int p2p_no_group_iface;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800648 unsigned int pref_freq_list[P2P_MAX_PREF_CHANNELS], size;
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800649
650 wpa_printf(MSG_DEBUG, "P2P: Conncap - in:%d role:%d", incoming, role);
651
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800652 if (force_freq)
653 *force_freq = 0;
654 if (pref_freq)
655 *pref_freq = 0;
656
657 size = P2P_MAX_PREF_CHANNELS;
658 if (force_freq && pref_freq &&
659 !wpas_p2p_setup_freqs(wpa_s, 0, (int *) force_freq,
660 (int *) pref_freq, 0, pref_freq_list, &size))
661 wpas_p2p_set_own_freq_preference(wpa_s,
662 *force_freq ? *force_freq :
663 *pref_freq);
664
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800665 /*
666 * For non-concurrent capable devices:
667 * If persistent_go, then no new.
668 * If GO, then no client.
669 * If client, then no GO.
670 */
671 go_wpa_s = wpas_p2p_get_go_group(wpa_s);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800672 if (go_wpa_s)
673 owned_members = p2p_get_group_num_members(go_wpa_s->p2p_group);
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800674 persistent_go = wpas_p2p_get_persistent_go(wpa_s);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800675 p2p_no_group_iface = !wpas_p2p_create_iface(wpa_s);
676 cli_wpa_s = wpas_p2p_get_cli_group(wpa_s);
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800677
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800678 wpa_printf(MSG_DEBUG,
679 "P2P: GO(iface)=%p members=%u CLI(iface)=%p persistent(ssid)=%p",
680 go_wpa_s, owned_members, cli_wpa_s, persistent_go);
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800681
682 /* If not concurrent, restrict our choices */
683 if (p2p_no_group_iface) {
684 wpa_printf(MSG_DEBUG, "P2P: p2p_no_group_iface");
685
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800686 if (cli_wpa_s)
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800687 return P2PS_SETUP_NONE;
688
689 if (go_wpa_s) {
690 if (role == P2PS_SETUP_CLIENT ||
691 incoming == P2PS_SETUP_GROUP_OWNER ||
692 p2p_client_limit_reached(go_wpa_s->p2p_group))
693 return P2PS_SETUP_NONE;
694
695 return P2PS_SETUP_GROUP_OWNER;
696 }
697
698 if (persistent_go) {
699 if (role == P2PS_SETUP_NONE || role == P2PS_SETUP_NEW) {
700 if (!incoming)
701 return P2PS_SETUP_GROUP_OWNER |
702 P2PS_SETUP_CLIENT;
703 if (incoming == P2PS_SETUP_NEW) {
704 u8 r;
705
706 if (os_get_random(&r, sizeof(r)) < 0 ||
707 (r & 1))
708 return P2PS_SETUP_CLIENT;
709 return P2PS_SETUP_GROUP_OWNER;
710 }
711 }
712 }
713 }
714
715 /* If a required role has been specified, handle it here */
716 if (role && role != P2PS_SETUP_NEW) {
717 switch (incoming) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800718 case P2PS_SETUP_GROUP_OWNER | P2PS_SETUP_NEW:
719 case P2PS_SETUP_GROUP_OWNER | P2PS_SETUP_CLIENT:
720 /*
721 * Peer has an active GO, so if the role allows it and
722 * we do not have any active roles, become client.
723 */
724 if ((role & P2PS_SETUP_CLIENT) && !go_wpa_s &&
725 !cli_wpa_s)
726 return P2PS_SETUP_CLIENT;
727
728 /* fall through */
729
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800730 case P2PS_SETUP_NONE:
731 case P2PS_SETUP_NEW:
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800732 conncap = role;
733 goto grp_owner;
734
735 case P2PS_SETUP_GROUP_OWNER:
736 /*
737 * Must be a complimentary role - cannot be a client to
738 * more than one peer.
739 */
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800740 if (incoming == role || cli_wpa_s)
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800741 return P2PS_SETUP_NONE;
742
743 return P2PS_SETUP_CLIENT;
744
745 case P2PS_SETUP_CLIENT:
746 /* Must be a complimentary role */
747 if (incoming != role) {
748 conncap = P2PS_SETUP_GROUP_OWNER;
749 goto grp_owner;
750 }
Roshan Pius3a1667e2018-07-03 15:17:14 -0700751 /* fall through */
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800752
753 default:
754 return P2PS_SETUP_NONE;
755 }
756 }
757
758 /*
759 * For now, we only will support ownership of one group, and being a
760 * client of one group. Therefore, if we have either an existing GO
761 * group, or an existing client group, we will not do a new GO
762 * negotiation, but rather try to re-use the existing groups.
763 */
764 switch (incoming) {
765 case P2PS_SETUP_NONE:
766 case P2PS_SETUP_NEW:
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800767 if (cli_wpa_s)
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800768 conncap = P2PS_SETUP_GROUP_OWNER;
769 else if (!owned_members)
770 conncap = P2PS_SETUP_NEW;
771 else if (incoming == P2PS_SETUP_NONE)
772 conncap = P2PS_SETUP_GROUP_OWNER | P2PS_SETUP_CLIENT;
773 else
774 conncap = P2PS_SETUP_CLIENT;
775 break;
776
777 case P2PS_SETUP_CLIENT:
778 conncap = P2PS_SETUP_GROUP_OWNER;
779 break;
780
781 case P2PS_SETUP_GROUP_OWNER:
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800782 if (!cli_wpa_s)
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800783 conncap = P2PS_SETUP_CLIENT;
784 break;
785
786 case P2PS_SETUP_GROUP_OWNER | P2PS_SETUP_NEW:
787 case P2PS_SETUP_GROUP_OWNER | P2PS_SETUP_CLIENT:
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800788 if (cli_wpa_s)
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800789 conncap = P2PS_SETUP_GROUP_OWNER;
790 else {
791 u8 r;
792
793 if (os_get_random(&r, sizeof(r)) < 0 ||
794 (r & 1))
795 conncap = P2PS_SETUP_CLIENT;
796 else
797 conncap = P2PS_SETUP_GROUP_OWNER;
798 }
799 break;
800
801 default:
802 return P2PS_SETUP_NONE;
803 }
804
805grp_owner:
806 if ((conncap & P2PS_SETUP_GROUP_OWNER) ||
807 (!incoming && (conncap & P2PS_SETUP_NEW))) {
808 if (go_wpa_s && p2p_client_limit_reached(go_wpa_s->p2p_group))
809 conncap &= ~P2PS_SETUP_GROUP_OWNER;
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800810
811 s = wpas_p2p_get_persistent_go(wpa_s);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800812 if (!s && !go_wpa_s && p2p_no_group_iface) {
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800813 p2p_set_intended_addr(wpa_s->global->p2p,
Dmitry Shmidt9c175262016-03-03 10:20:07 -0800814 wpa_s->p2p_mgmt ?
815 wpa_s->parent->own_addr :
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800816 wpa_s->own_addr);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800817 } else if (!s && !go_wpa_s) {
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800818 if (wpas_p2p_add_group_interface(wpa_s,
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -0800819 WPA_IF_P2P_GROUP) < 0) {
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800820 wpa_printf(MSG_ERROR,
821 "P2P: Failed to allocate a new interface for the group");
822 return P2PS_SETUP_NONE;
823 }
824 wpa_s->global->pending_group_iface_for_p2ps = 1;
825 p2p_set_intended_addr(wpa_s->global->p2p,
826 wpa_s->pending_interface_addr);
827 }
828 }
829
830 return conncap;
831}
832
833
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700834static int wpas_p2p_group_delete(struct wpa_supplicant *wpa_s,
835 enum p2p_group_removal_reason removal_reason)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700836{
837 struct wpa_ssid *ssid;
838 char *gtype;
839 const char *reason;
840
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700841 ssid = wpa_s->current_ssid;
842 if (ssid == NULL) {
843 /*
844 * The current SSID was not known, but there may still be a
Dmitry Shmidtaa532512012-09-24 10:35:31 -0700845 * pending P2P group interface waiting for provisioning or a
846 * P2P group that is trying to reconnect.
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700847 */
848 ssid = wpa_s->conf->ssid;
849 while (ssid) {
Jouni Malinen9d712832012-10-05 11:01:57 -0700850 if (ssid->p2p_group && ssid->disabled != 2)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700851 break;
852 ssid = ssid->next;
853 }
Jouni Malinen5c44edb2012-08-31 21:35:32 +0300854 if (ssid == NULL &&
855 wpa_s->p2p_group_interface == NOT_P2P_GROUP_INTERFACE)
856 {
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700857 wpa_printf(MSG_ERROR, "P2P: P2P group interface "
858 "not found");
859 return -1;
860 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700861 }
862 if (wpa_s->p2p_group_interface == P2P_GROUP_INTERFACE_GO)
863 gtype = "GO";
864 else if (wpa_s->p2p_group_interface == P2P_GROUP_INTERFACE_CLIENT ||
865 (ssid && ssid->mode == WPAS_MODE_INFRA)) {
866 wpa_s->reassociate = 0;
867 wpa_s->disconnected = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700868 gtype = "client";
869 } else
870 gtype = "GO";
Dmitry Shmidt661b4f72014-09-29 14:58:27 -0700871
872 if (removal_reason != P2P_GROUP_REMOVAL_SILENT && ssid)
873 wpas_notify_p2p_group_removed(wpa_s, ssid, gtype);
874
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800875 if (os_strcmp(gtype, "client") == 0) {
Dmitry Shmidt661b4f72014-09-29 14:58:27 -0700876 wpa_supplicant_deauthenticate(wpa_s, WLAN_REASON_DEAUTH_LEAVING);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800877 if (eloop_is_timeout_registered(wpas_p2p_psk_failure_removal,
878 wpa_s, NULL)) {
879 wpa_printf(MSG_DEBUG,
880 "P2P: PSK failure removal was scheduled, so use PSK failure as reason for group removal");
881 removal_reason = P2P_GROUP_REMOVAL_PSK_FAILURE;
882 eloop_cancel_timeout(wpas_p2p_psk_failure_removal,
883 wpa_s, NULL);
884 }
885 }
Dmitry Shmidt661b4f72014-09-29 14:58:27 -0700886
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700887 if (wpa_s->cross_connect_in_use) {
888 wpa_s->cross_connect_in_use = 0;
Dmitry Shmidt9c175262016-03-03 10:20:07 -0800889 wpa_msg_global(wpa_s->p2pdev, MSG_INFO,
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -0700890 P2P_EVENT_CROSS_CONNECT_DISABLE "%s %s",
891 wpa_s->ifname, wpa_s->cross_connect_uplink);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700892 }
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700893 switch (removal_reason) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700894 case P2P_GROUP_REMOVAL_REQUESTED:
895 reason = " reason=REQUESTED";
896 break;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700897 case P2P_GROUP_REMOVAL_FORMATION_FAILED:
898 reason = " reason=FORMATION_FAILED";
899 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700900 case P2P_GROUP_REMOVAL_IDLE_TIMEOUT:
901 reason = " reason=IDLE";
902 break;
903 case P2P_GROUP_REMOVAL_UNAVAILABLE:
904 reason = " reason=UNAVAILABLE";
905 break;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700906 case P2P_GROUP_REMOVAL_GO_ENDING_SESSION:
907 reason = " reason=GO_ENDING_SESSION";
908 break;
Dmitry Shmidt391c59f2013-09-03 12:16:28 -0700909 case P2P_GROUP_REMOVAL_PSK_FAILURE:
910 reason = " reason=PSK_FAILURE";
911 break;
Dmitry Shmidt04f534e2013-12-09 15:50:16 -0800912 case P2P_GROUP_REMOVAL_FREQ_CONFLICT:
913 reason = " reason=FREQ_CONFLICT";
914 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700915 default:
916 reason = "";
917 break;
918 }
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700919 if (removal_reason != P2P_GROUP_REMOVAL_SILENT) {
Dmitry Shmidt9c175262016-03-03 10:20:07 -0800920 wpa_msg_global(wpa_s->p2pdev, MSG_INFO,
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -0700921 P2P_EVENT_GROUP_REMOVED "%s %s%s",
922 wpa_s->ifname, gtype, reason);
Dmitry Shmidt04949592012-07-19 12:16:46 -0700923 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700924
Dmitry Shmidt04f534e2013-12-09 15:50:16 -0800925 if (eloop_cancel_timeout(wpas_p2p_group_freq_conflict, wpa_s, NULL) > 0)
926 wpa_printf(MSG_DEBUG, "P2P: Cancelled P2P group freq_conflict timeout");
Dmitry Shmidt04949592012-07-19 12:16:46 -0700927 if (eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL) > 0)
928 wpa_printf(MSG_DEBUG, "P2P: Cancelled P2P group idle timeout");
Dmitry Shmidtf8623282013-02-20 14:34:59 -0800929 if (eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
Dmitry Shmidt9c175262016-03-03 10:20:07 -0800930 wpa_s->p2pdev, NULL) > 0) {
Dmitry Shmidtf8623282013-02-20 14:34:59 -0800931 wpa_printf(MSG_DEBUG, "P2P: Cancelled P2P group formation "
932 "timeout");
Dmitry Shmidt2f023192013-03-12 12:44:17 -0700933 wpa_s->p2p_in_provisioning = 0;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800934 wpas_p2p_group_formation_failed(wpa_s, 1);
Dmitry Shmidt2f023192013-03-12 12:44:17 -0700935 }
Dmitry Shmidt04949592012-07-19 12:16:46 -0700936
Dmitry Shmidt15907092014-03-25 10:42:57 -0700937 wpa_s->p2p_in_invitation = 0;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800938 eloop_cancel_timeout(wpas_p2p_move_go, wpa_s, NULL);
939 eloop_cancel_timeout(wpas_p2p_reconsider_moving_go, wpa_s, NULL);
Dmitry Shmidt15907092014-03-25 10:42:57 -0700940
Dmitry Shmidt96571392013-10-14 12:54:46 -0700941 /*
942 * Make sure wait for the first client does not remain active after the
943 * group has been removed.
944 */
945 wpa_s->global->p2p_go_wait_client.sec = 0;
946
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700947 if (wpa_s->p2p_group_interface != NOT_P2P_GROUP_INTERFACE) {
948 struct wpa_global *global;
949 char *ifname;
950 enum wpa_driver_if_type type;
951 wpa_printf(MSG_DEBUG, "P2P: Remove group interface %s",
952 wpa_s->ifname);
953 global = wpa_s->global;
954 ifname = os_strdup(wpa_s->ifname);
955 type = wpas_p2p_if_type(wpa_s->p2p_group_interface);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800956 eloop_cancel_timeout(run_wpas_p2p_disconnect, wpa_s, NULL);
Dmitry Shmidte15c7b52011-08-03 15:04:35 -0700957 wpa_supplicant_remove_iface(wpa_s->global, wpa_s, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700958 wpa_s = global->ifaces;
959 if (wpa_s && ifname)
960 wpa_drv_if_remove(wpa_s, type, ifname);
961 os_free(ifname);
Jouni Malinen2b89da82012-08-31 22:04:41 +0300962 return 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700963 }
964
Dmitry Shmidt9c175262016-03-03 10:20:07 -0800965 /*
966 * The primary interface was used for P2P group operations, so
967 * need to reset its p2pdev.
968 */
969 wpa_s->p2pdev = wpa_s->parent;
970
Dmitry Shmidt96571392013-10-14 12:54:46 -0700971 if (!wpa_s->p2p_go_group_formation_completed) {
972 wpa_s->global->p2p_group_formation = NULL;
973 wpa_s->p2p_in_provisioning = 0;
974 }
975
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800976 wpa_s->show_group_started = 0;
977 os_free(wpa_s->go_params);
978 wpa_s->go_params = NULL;
979
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800980 os_free(wpa_s->p2p_group_common_freqs);
981 wpa_s->p2p_group_common_freqs = NULL;
982 wpa_s->p2p_group_common_freqs_num = 0;
983
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800984 wpa_s->waiting_presence_resp = 0;
985
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700986 wpa_printf(MSG_DEBUG, "P2P: Remove temporary group network");
987 if (ssid && (ssid->p2p_group ||
988 ssid->mode == WPAS_MODE_P2P_GROUP_FORMATION ||
989 (ssid->key_mgmt & WPA_KEY_MGMT_WPS))) {
990 int id = ssid->id;
Jouni Malinen75ecf522011-06-27 15:19:46 -0700991 if (ssid == wpa_s->current_ssid) {
992 wpa_sm_set_config(wpa_s->wpa, NULL);
993 eapol_sm_notify_config(wpa_s->eapol, NULL, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700994 wpa_s->current_ssid = NULL;
Jouni Malinen75ecf522011-06-27 15:19:46 -0700995 }
996 /*
997 * Networks objects created during any P2P activities are not
998 * exposed out as they might/will confuse certain non-P2P aware
999 * applications since these network objects won't behave like
1000 * regular ones.
1001 *
1002 * Likewise, we don't send out network removed signals for such
1003 * network objects.
1004 */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001005 wpa_config_remove_network(wpa_s->conf, id);
1006 wpa_supplicant_clear_status(wpa_s);
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08001007 wpa_supplicant_cancel_sched_scan(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001008 } else {
1009 wpa_printf(MSG_DEBUG, "P2P: Temporary group network not "
1010 "found");
1011 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07001012 if (wpa_s->ap_iface)
1013 wpa_supplicant_ap_deinit(wpa_s);
1014 else
1015 wpa_drv_deinit_p2p_cli(wpa_s);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001016
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001017 os_memset(wpa_s->go_dev_addr, 0, ETH_ALEN);
1018
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001019 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001020}
1021
1022
1023static int wpas_p2p_persistent_group(struct wpa_supplicant *wpa_s,
1024 u8 *go_dev_addr,
1025 const u8 *ssid, size_t ssid_len)
1026{
1027 struct wpa_bss *bss;
1028 const u8 *bssid;
1029 struct wpabuf *p2p;
1030 u8 group_capab;
1031 const u8 *addr;
1032
1033 if (wpa_s->go_params)
1034 bssid = wpa_s->go_params->peer_interface_addr;
1035 else
1036 bssid = wpa_s->bssid;
1037
1038 bss = wpa_bss_get(wpa_s, bssid, ssid, ssid_len);
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001039 if (bss == NULL && wpa_s->go_params &&
1040 !is_zero_ether_addr(wpa_s->go_params->peer_device_addr))
1041 bss = wpa_bss_get_p2p_dev_addr(
1042 wpa_s, wpa_s->go_params->peer_device_addr);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001043 if (bss == NULL) {
1044 u8 iface_addr[ETH_ALEN];
1045 if (p2p_get_interface_addr(wpa_s->global->p2p, bssid,
1046 iface_addr) == 0)
1047 bss = wpa_bss_get(wpa_s, iface_addr, ssid, ssid_len);
1048 }
1049 if (bss == NULL) {
1050 wpa_printf(MSG_DEBUG, "P2P: Could not figure out whether "
1051 "group is persistent - BSS " MACSTR " not found",
1052 MAC2STR(bssid));
1053 return 0;
1054 }
1055
1056 p2p = wpa_bss_get_vendor_ie_multi(bss, P2P_IE_VENDOR_TYPE);
Dmitry Shmidt96571392013-10-14 12:54:46 -07001057 if (p2p == NULL)
1058 p2p = wpa_bss_get_vendor_ie_multi_beacon(bss,
1059 P2P_IE_VENDOR_TYPE);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001060 if (p2p == NULL) {
1061 wpa_printf(MSG_DEBUG, "P2P: Could not figure out whether "
1062 "group is persistent - BSS " MACSTR
1063 " did not include P2P IE", MAC2STR(bssid));
1064 wpa_hexdump(MSG_DEBUG, "P2P: Probe Response IEs",
1065 (u8 *) (bss + 1), bss->ie_len);
1066 wpa_hexdump(MSG_DEBUG, "P2P: Beacon IEs",
1067 ((u8 *) bss + 1) + bss->ie_len,
1068 bss->beacon_ie_len);
1069 return 0;
1070 }
1071
1072 group_capab = p2p_get_group_capab(p2p);
1073 addr = p2p_get_go_dev_addr(p2p);
1074 wpa_printf(MSG_DEBUG, "P2P: Checking whether group is persistent: "
1075 "group_capab=0x%x", group_capab);
1076 if (addr) {
1077 os_memcpy(go_dev_addr, addr, ETH_ALEN);
1078 wpa_printf(MSG_DEBUG, "P2P: GO Device Address " MACSTR,
1079 MAC2STR(addr));
1080 } else
1081 os_memset(go_dev_addr, 0, ETH_ALEN);
1082 wpabuf_free(p2p);
1083
1084 wpa_printf(MSG_DEBUG, "P2P: BSS " MACSTR " group_capab=0x%x "
1085 "go_dev_addr=" MACSTR,
1086 MAC2STR(bssid), group_capab, MAC2STR(go_dev_addr));
1087
Dmitry Shmidt7f2c7532016-08-15 09:48:12 -07001088 return !!(group_capab & P2P_GROUP_CAPAB_PERSISTENT_GROUP);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001089}
1090
1091
Jouni Malinen75ecf522011-06-27 15:19:46 -07001092static int wpas_p2p_store_persistent_group(struct wpa_supplicant *wpa_s,
1093 struct wpa_ssid *ssid,
1094 const u8 *go_dev_addr)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001095{
1096 struct wpa_ssid *s;
1097 int changed = 0;
1098
1099 wpa_printf(MSG_DEBUG, "P2P: Storing credentials for a persistent "
1100 "group (GO Dev Addr " MACSTR ")", MAC2STR(go_dev_addr));
1101 for (s = wpa_s->conf->ssid; s; s = s->next) {
1102 if (s->disabled == 2 &&
1103 os_memcmp(go_dev_addr, s->bssid, ETH_ALEN) == 0 &&
1104 s->ssid_len == ssid->ssid_len &&
1105 os_memcmp(ssid->ssid, s->ssid, ssid->ssid_len) == 0)
1106 break;
1107 }
1108
1109 if (s) {
1110 wpa_printf(MSG_DEBUG, "P2P: Update existing persistent group "
1111 "entry");
1112 if (ssid->passphrase && !s->passphrase)
1113 changed = 1;
1114 else if (ssid->passphrase && s->passphrase &&
1115 os_strcmp(ssid->passphrase, s->passphrase) != 0)
1116 changed = 1;
1117 } else {
1118 wpa_printf(MSG_DEBUG, "P2P: Create a new persistent group "
1119 "entry");
1120 changed = 1;
1121 s = wpa_config_add_network(wpa_s->conf);
1122 if (s == NULL)
Jouni Malinen75ecf522011-06-27 15:19:46 -07001123 return -1;
1124
1125 /*
1126 * Instead of network_added we emit persistent_group_added
1127 * notification. Also to keep the defense checks in
1128 * persistent_group obj registration method, we set the
1129 * relevant flags in s to designate it as a persistent group.
1130 */
1131 s->p2p_group = 1;
1132 s->p2p_persistent_group = 1;
1133 wpas_notify_persistent_group_added(wpa_s, s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001134 wpa_config_set_network_defaults(s);
1135 }
1136
1137 s->p2p_group = 1;
1138 s->p2p_persistent_group = 1;
1139 s->disabled = 2;
1140 s->bssid_set = 1;
1141 os_memcpy(s->bssid, go_dev_addr, ETH_ALEN);
1142 s->mode = ssid->mode;
1143 s->auth_alg = WPA_AUTH_ALG_OPEN;
1144 s->key_mgmt = WPA_KEY_MGMT_PSK;
1145 s->proto = WPA_PROTO_RSN;
Dmitry Shmidte4663042016-04-04 10:07:49 -07001146 s->pbss = ssid->pbss;
1147 s->pairwise_cipher = ssid->pbss ? WPA_CIPHER_GCMP : WPA_CIPHER_CCMP;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001148 s->export_keys = 1;
1149 if (ssid->passphrase) {
1150 os_free(s->passphrase);
1151 s->passphrase = os_strdup(ssid->passphrase);
1152 }
1153 if (ssid->psk_set) {
1154 s->psk_set = 1;
1155 os_memcpy(s->psk, ssid->psk, 32);
1156 }
1157 if (s->passphrase && !s->psk_set)
1158 wpa_config_update_psk(s);
1159 if (s->ssid == NULL || s->ssid_len < ssid->ssid_len) {
1160 os_free(s->ssid);
1161 s->ssid = os_malloc(ssid->ssid_len);
1162 }
1163 if (s->ssid) {
1164 s->ssid_len = ssid->ssid_len;
1165 os_memcpy(s->ssid, ssid->ssid, s->ssid_len);
1166 }
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07001167 if (ssid->mode == WPAS_MODE_P2P_GO && wpa_s->global->add_psk) {
1168 dl_list_add(&s->psk_list, &wpa_s->global->add_psk->list);
1169 wpa_s->global->add_psk = NULL;
1170 changed = 1;
1171 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001172
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001173 if (changed && wpa_s->conf->update_config &&
1174 wpa_config_write(wpa_s->confname, wpa_s->conf)) {
1175 wpa_printf(MSG_DEBUG, "P2P: Failed to update configuration");
1176 }
Jouni Malinen75ecf522011-06-27 15:19:46 -07001177
1178 return s->id;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001179}
1180
1181
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001182static void wpas_p2p_add_persistent_group_client(struct wpa_supplicant *wpa_s,
1183 const u8 *addr)
1184{
1185 struct wpa_ssid *ssid, *s;
1186 u8 *n;
1187 size_t i;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001188 int found = 0;
Dmitry Shmidt8bd70b72015-05-26 16:02:19 -07001189 struct wpa_supplicant *p2p_wpa_s = wpa_s->global->p2p_init_wpa_s;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001190
1191 ssid = wpa_s->current_ssid;
1192 if (ssid == NULL || ssid->mode != WPAS_MODE_P2P_GO ||
1193 !ssid->p2p_persistent_group)
1194 return;
1195
Dmitry Shmidt8bd70b72015-05-26 16:02:19 -07001196 for (s = p2p_wpa_s->conf->ssid; s; s = s->next) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001197 if (s->disabled != 2 || s->mode != WPAS_MODE_P2P_GO)
1198 continue;
1199
1200 if (s->ssid_len == ssid->ssid_len &&
1201 os_memcmp(s->ssid, ssid->ssid, s->ssid_len) == 0)
1202 break;
1203 }
1204
1205 if (s == NULL)
1206 return;
1207
1208 for (i = 0; s->p2p_client_list && i < s->num_p2p_clients; i++) {
Dmitry Shmidtff787d52015-01-12 13:01:47 -08001209 if (os_memcmp(s->p2p_client_list + i * 2 * ETH_ALEN, addr,
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001210 ETH_ALEN) != 0)
1211 continue;
1212
1213 if (i == s->num_p2p_clients - 1)
1214 return; /* already the most recent entry */
1215
1216 /* move the entry to mark it most recent */
Dmitry Shmidtff787d52015-01-12 13:01:47 -08001217 os_memmove(s->p2p_client_list + i * 2 * ETH_ALEN,
1218 s->p2p_client_list + (i + 1) * 2 * ETH_ALEN,
1219 (s->num_p2p_clients - i - 1) * 2 * ETH_ALEN);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001220 os_memcpy(s->p2p_client_list +
Dmitry Shmidtff787d52015-01-12 13:01:47 -08001221 (s->num_p2p_clients - 1) * 2 * ETH_ALEN, addr,
1222 ETH_ALEN);
1223 os_memset(s->p2p_client_list +
1224 (s->num_p2p_clients - 1) * 2 * ETH_ALEN + ETH_ALEN,
1225 0xff, ETH_ALEN);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001226 found = 1;
1227 break;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001228 }
1229
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001230 if (!found && s->num_p2p_clients < P2P_MAX_STORED_CLIENTS) {
1231 n = os_realloc_array(s->p2p_client_list,
Dmitry Shmidtff787d52015-01-12 13:01:47 -08001232 s->num_p2p_clients + 1, 2 * ETH_ALEN);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001233 if (n == NULL)
1234 return;
Dmitry Shmidtff787d52015-01-12 13:01:47 -08001235 os_memcpy(n + s->num_p2p_clients * 2 * ETH_ALEN, addr,
1236 ETH_ALEN);
1237 os_memset(n + s->num_p2p_clients * 2 * ETH_ALEN + ETH_ALEN,
1238 0xff, ETH_ALEN);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001239 s->p2p_client_list = n;
1240 s->num_p2p_clients++;
Dmitry Shmidt6aa8ae42014-07-07 09:31:33 -07001241 } else if (!found && s->p2p_client_list) {
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001242 /* Not enough room for an additional entry - drop the oldest
1243 * entry */
1244 os_memmove(s->p2p_client_list,
Dmitry Shmidtff787d52015-01-12 13:01:47 -08001245 s->p2p_client_list + 2 * ETH_ALEN,
1246 (s->num_p2p_clients - 1) * 2 * ETH_ALEN);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001247 os_memcpy(s->p2p_client_list +
Dmitry Shmidtff787d52015-01-12 13:01:47 -08001248 (s->num_p2p_clients - 1) * 2 * ETH_ALEN,
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001249 addr, ETH_ALEN);
Dmitry Shmidtff787d52015-01-12 13:01:47 -08001250 os_memset(s->p2p_client_list +
1251 (s->num_p2p_clients - 1) * 2 * ETH_ALEN + ETH_ALEN,
1252 0xff, ETH_ALEN);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001253 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001254
Dmitry Shmidt8bd70b72015-05-26 16:02:19 -07001255 if (p2p_wpa_s->conf->update_config &&
1256 wpa_config_write(p2p_wpa_s->confname, p2p_wpa_s->conf))
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001257 wpa_printf(MSG_DEBUG, "P2P: Failed to update configuration");
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001258}
1259
1260
Dmitry Shmidt7f0b69e2014-07-28 10:35:20 -07001261static void wpas_p2p_group_started(struct wpa_supplicant *wpa_s,
1262 int go, struct wpa_ssid *ssid, int freq,
1263 const u8 *psk, const char *passphrase,
1264 const u8 *go_dev_addr, int persistent,
1265 const char *extra)
1266{
1267 const char *ssid_txt;
1268 char psk_txt[65];
1269
1270 if (psk)
1271 wpa_snprintf_hex(psk_txt, sizeof(psk_txt), psk, 32);
1272 else
1273 psk_txt[0] = '\0';
1274
1275 if (ssid)
1276 ssid_txt = wpa_ssid_txt(ssid->ssid, ssid->ssid_len);
1277 else
1278 ssid_txt = "";
1279
1280 if (passphrase && passphrase[0] == '\0')
1281 passphrase = NULL;
1282
1283 /*
1284 * Include PSK/passphrase only in the control interface message and
1285 * leave it out from the debug log entry.
1286 */
Dmitry Shmidt9c175262016-03-03 10:20:07 -08001287 wpa_msg_global_ctrl(wpa_s->p2pdev, MSG_INFO,
Dmitry Shmidt7f0b69e2014-07-28 10:35:20 -07001288 P2P_EVENT_GROUP_STARTED
1289 "%s %s ssid=\"%s\" freq=%d%s%s%s%s%s go_dev_addr="
1290 MACSTR "%s%s",
1291 wpa_s->ifname, go ? "GO" : "client", ssid_txt, freq,
1292 psk ? " psk=" : "", psk_txt,
1293 passphrase ? " passphrase=\"" : "",
1294 passphrase ? passphrase : "",
1295 passphrase ? "\"" : "",
1296 MAC2STR(go_dev_addr),
1297 persistent ? " [PERSISTENT]" : "", extra);
1298 wpa_printf(MSG_INFO, P2P_EVENT_GROUP_STARTED
1299 "%s %s ssid=\"%s\" freq=%d go_dev_addr=" MACSTR "%s%s",
1300 wpa_s->ifname, go ? "GO" : "client", ssid_txt, freq,
1301 MAC2STR(go_dev_addr), persistent ? " [PERSISTENT]" : "",
1302 extra);
1303}
1304
1305
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001306static void wpas_group_formation_completed(struct wpa_supplicant *wpa_s,
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07001307 int success, int already_deleted)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001308{
1309 struct wpa_ssid *ssid;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001310 int client;
1311 int persistent;
1312 u8 go_dev_addr[ETH_ALEN];
1313
1314 /*
1315 * This callback is likely called for the main interface. Update wpa_s
1316 * to use the group interface if a new interface was created for the
1317 * group.
1318 */
1319 if (wpa_s->global->p2p_group_formation)
1320 wpa_s = wpa_s->global->p2p_group_formation;
Dmitry Shmidt56052862013-10-04 10:23:25 -07001321 if (wpa_s->p2p_go_group_formation_completed) {
1322 wpa_s->global->p2p_group_formation = NULL;
1323 wpa_s->p2p_in_provisioning = 0;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001324 } else if (wpa_s->p2p_in_provisioning && !success) {
1325 wpa_msg(wpa_s, MSG_DEBUG,
1326 "P2P: Stop provisioning state due to failure");
1327 wpa_s->p2p_in_provisioning = 0;
Dmitry Shmidt56052862013-10-04 10:23:25 -07001328 }
Dmitry Shmidt21de2142014-04-08 10:50:52 -07001329 wpa_s->p2p_in_invitation = 0;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001330 wpa_s->group_formation_reported = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001331
1332 if (!success) {
Dmitry Shmidt9c175262016-03-03 10:20:07 -08001333 wpa_msg_global(wpa_s->p2pdev, MSG_INFO,
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07001334 P2P_EVENT_GROUP_FORMATION_FAILURE);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001335 wpas_notify_p2p_group_formation_failure(wpa_s, "");
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07001336 if (already_deleted)
1337 return;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001338 wpas_p2p_group_delete(wpa_s,
1339 P2P_GROUP_REMOVAL_FORMATION_FAILED);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001340 return;
1341 }
1342
Dmitry Shmidt9c175262016-03-03 10:20:07 -08001343 wpa_msg_global(wpa_s->p2pdev, MSG_INFO,
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07001344 P2P_EVENT_GROUP_FORMATION_SUCCESS);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001345
1346 ssid = wpa_s->current_ssid;
1347 if (ssid && ssid->mode == WPAS_MODE_P2P_GROUP_FORMATION) {
1348 ssid->mode = WPAS_MODE_P2P_GO;
1349 p2p_group_notif_formation_done(wpa_s->p2p_group);
1350 wpa_supplicant_ap_mac_addr_filter(wpa_s, NULL);
1351 }
1352
1353 persistent = 0;
1354 if (ssid) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001355 client = ssid->mode == WPAS_MODE_INFRA;
1356 if (ssid->mode == WPAS_MODE_P2P_GO) {
1357 persistent = ssid->p2p_persistent_group;
Dmitry Shmidt497c1d52011-07-21 15:19:46 -07001358 os_memcpy(go_dev_addr, wpa_s->global->p2p_dev_addr,
1359 ETH_ALEN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001360 } else
1361 persistent = wpas_p2p_persistent_group(wpa_s,
1362 go_dev_addr,
1363 ssid->ssid,
1364 ssid->ssid_len);
1365 } else {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001366 client = wpa_s->p2p_group_interface ==
1367 P2P_GROUP_INTERFACE_CLIENT;
1368 os_memset(go_dev_addr, 0, ETH_ALEN);
1369 }
1370
1371 wpa_s->show_group_started = 0;
1372 if (client) {
1373 /*
1374 * Indicate event only after successfully completed 4-way
1375 * handshake, i.e., when the interface is ready for data
1376 * packets.
1377 */
1378 wpa_s->show_group_started = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001379 } else {
Dmitry Shmidt7f0b69e2014-07-28 10:35:20 -07001380 wpas_p2p_group_started(wpa_s, 1, ssid,
1381 ssid ? ssid->frequency : 0,
1382 ssid && ssid->passphrase == NULL &&
1383 ssid->psk_set ? ssid->psk : NULL,
1384 ssid ? ssid->passphrase : NULL,
1385 go_dev_addr, persistent, "");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001386 wpas_p2p_cross_connect_setup(wpa_s);
1387 wpas_p2p_set_group_idle_timeout(wpa_s);
1388 }
1389
1390 if (persistent)
Dmitry Shmidtd5ab1b52016-06-21 12:38:41 -07001391 wpas_p2p_store_persistent_group(wpa_s->p2pdev,
1392 ssid, go_dev_addr);
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07001393 else {
1394 os_free(wpa_s->global->add_psk);
1395 wpa_s->global->add_psk = NULL;
1396 }
Dmitry Shmidtd5ab1b52016-06-21 12:38:41 -07001397
Dmitry Shmidt92c368d2013-08-29 12:37:21 -07001398 if (!client) {
Dmitry Shmidtabb90a32016-12-05 15:34:39 -08001399 wpas_notify_p2p_group_started(wpa_s, ssid, persistent, 0, NULL);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001400 os_get_reltime(&wpa_s->global->p2p_go_wait_client);
Dmitry Shmidt92c368d2013-08-29 12:37:21 -07001401 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001402}
1403
1404
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001405struct send_action_work {
1406 unsigned int freq;
1407 u8 dst[ETH_ALEN];
1408 u8 src[ETH_ALEN];
1409 u8 bssid[ETH_ALEN];
1410 size_t len;
1411 unsigned int wait_time;
1412 u8 buf[0];
1413};
1414
1415
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001416static void wpas_p2p_free_send_action_work(struct wpa_supplicant *wpa_s)
1417{
1418 struct send_action_work *awork = wpa_s->p2p_send_action_work->ctx;
1419
1420 wpa_printf(MSG_DEBUG,
1421 "P2P: Free Action frame radio work @%p (freq=%u dst="
1422 MACSTR " src=" MACSTR " bssid=" MACSTR " wait_time=%u)",
1423 wpa_s->p2p_send_action_work, awork->freq,
1424 MAC2STR(awork->dst), MAC2STR(awork->src),
1425 MAC2STR(awork->bssid), awork->wait_time);
1426 wpa_hexdump(MSG_DEBUG, "P2P: Freeing pending Action frame",
1427 awork->buf, awork->len);
1428 os_free(awork);
1429 wpa_s->p2p_send_action_work->ctx = NULL;
1430 radio_work_done(wpa_s->p2p_send_action_work);
1431 wpa_s->p2p_send_action_work = NULL;
1432}
1433
1434
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001435static void wpas_p2p_send_action_work_timeout(void *eloop_ctx,
1436 void *timeout_ctx)
1437{
1438 struct wpa_supplicant *wpa_s = eloop_ctx;
1439
1440 if (!wpa_s->p2p_send_action_work)
1441 return;
1442
1443 wpa_printf(MSG_DEBUG, "P2P: Send Action frame radio work timed out");
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001444 wpas_p2p_free_send_action_work(wpa_s);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001445}
1446
1447
Dmitry Shmidt03658832014-08-13 11:03:49 -07001448static void wpas_p2p_action_tx_clear(struct wpa_supplicant *wpa_s)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001449{
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001450 if (wpa_s->p2p_send_action_work) {
1451 struct send_action_work *awork;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001452
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001453 awork = wpa_s->p2p_send_action_work->ctx;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001454 wpa_printf(MSG_DEBUG,
1455 "P2P: Clear Action TX work @%p (wait_time=%u)",
1456 wpa_s->p2p_send_action_work, awork->wait_time);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001457 if (awork->wait_time == 0) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001458 wpas_p2p_free_send_action_work(wpa_s);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001459 } else {
1460 /*
1461 * In theory, this should not be needed, but number of
1462 * places in the P2P code is still using non-zero wait
1463 * time for the last Action frame in the sequence and
1464 * some of these do not call send_action_done().
1465 */
1466 eloop_cancel_timeout(wpas_p2p_send_action_work_timeout,
1467 wpa_s, NULL);
1468 eloop_register_timeout(
1469 0, awork->wait_time * 1000,
1470 wpas_p2p_send_action_work_timeout,
1471 wpa_s, NULL);
1472 }
1473 }
Dmitry Shmidt03658832014-08-13 11:03:49 -07001474}
1475
1476
1477static void wpas_p2p_send_action_tx_status(struct wpa_supplicant *wpa_s,
1478 unsigned int freq,
1479 const u8 *dst, const u8 *src,
1480 const u8 *bssid,
1481 const u8 *data, size_t data_len,
1482 enum offchannel_send_action_result
1483 result)
1484{
1485 enum p2p_send_action_result res = P2P_SEND_ACTION_SUCCESS;
1486
1487 wpas_p2p_action_tx_clear(wpa_s);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001488
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001489 if (wpa_s->global->p2p == NULL || wpa_s->global->p2p_disabled)
1490 return;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001491
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001492 switch (result) {
1493 case OFFCHANNEL_SEND_ACTION_SUCCESS:
1494 res = P2P_SEND_ACTION_SUCCESS;
1495 break;
1496 case OFFCHANNEL_SEND_ACTION_NO_ACK:
1497 res = P2P_SEND_ACTION_NO_ACK;
1498 break;
1499 case OFFCHANNEL_SEND_ACTION_FAILED:
1500 res = P2P_SEND_ACTION_FAILED;
1501 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001502 }
1503
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001504 p2p_send_action_cb(wpa_s->global->p2p, freq, dst, src, bssid, res);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001505
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08001506 if (result != OFFCHANNEL_SEND_ACTION_SUCCESS &&
1507 wpa_s->pending_pd_before_join &&
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001508 (os_memcmp(dst, wpa_s->pending_join_dev_addr, ETH_ALEN) == 0 ||
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001509 os_memcmp(dst, wpa_s->pending_join_iface_addr, ETH_ALEN) == 0) &&
1510 wpa_s->p2p_fallback_to_go_neg) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001511 wpa_s->pending_pd_before_join = 0;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001512 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: No ACK for PD Req "
1513 "during p2p_connect-auto");
Dmitry Shmidt9c175262016-03-03 10:20:07 -08001514 wpa_msg_global(wpa_s->p2pdev, MSG_INFO,
Dmitry Shmidt7f656022015-02-25 14:36:37 -08001515 P2P_EVENT_FALLBACK_TO_GO_NEG
1516 "reason=no-ACK-to-PD-Req");
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001517 wpas_p2p_fallback_to_go_neg(wpa_s, 0);
1518 return;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001519 }
1520}
1521
1522
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001523static void wpas_send_action_cb(struct wpa_radio_work *work, int deinit)
1524{
1525 struct wpa_supplicant *wpa_s = work->wpa_s;
1526 struct send_action_work *awork = work->ctx;
1527
1528 if (deinit) {
Dmitry Shmidtbd14a572014-02-18 10:33:49 -08001529 if (work->started) {
1530 eloop_cancel_timeout(wpas_p2p_send_action_work_timeout,
1531 wpa_s, NULL);
1532 wpa_s->p2p_send_action_work = NULL;
1533 offchannel_send_action_done(wpa_s);
1534 }
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001535 os_free(awork);
1536 return;
1537 }
1538
1539 if (offchannel_send_action(wpa_s, awork->freq, awork->dst, awork->src,
1540 awork->bssid, awork->buf, awork->len,
1541 awork->wait_time,
1542 wpas_p2p_send_action_tx_status, 1) < 0) {
1543 os_free(awork);
1544 radio_work_done(work);
1545 return;
1546 }
1547 wpa_s->p2p_send_action_work = work;
1548}
1549
1550
1551static int wpas_send_action_work(struct wpa_supplicant *wpa_s,
1552 unsigned int freq, const u8 *dst,
1553 const u8 *src, const u8 *bssid, const u8 *buf,
1554 size_t len, unsigned int wait_time)
1555{
1556 struct send_action_work *awork;
1557
1558 if (wpa_s->p2p_send_action_work) {
1559 wpa_printf(MSG_DEBUG, "P2P: Cannot schedule new p2p-send-action work since one is already pending");
1560 return -1;
1561 }
1562
1563 awork = os_zalloc(sizeof(*awork) + len);
1564 if (awork == NULL)
1565 return -1;
1566
1567 awork->freq = freq;
1568 os_memcpy(awork->dst, dst, ETH_ALEN);
1569 os_memcpy(awork->src, src, ETH_ALEN);
1570 os_memcpy(awork->bssid, bssid, ETH_ALEN);
1571 awork->len = len;
1572 awork->wait_time = wait_time;
1573 os_memcpy(awork->buf, buf, len);
1574
1575 if (radio_add_work(wpa_s, freq, "p2p-send-action", 0,
1576 wpas_send_action_cb, awork) < 0) {
1577 os_free(awork);
1578 return -1;
1579 }
1580
1581 return 0;
1582}
1583
1584
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001585static int wpas_send_action(void *ctx, unsigned int freq, const u8 *dst,
1586 const u8 *src, const u8 *bssid, const u8 *buf,
1587 size_t len, unsigned int wait_time)
1588{
1589 struct wpa_supplicant *wpa_s = ctx;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001590 int listen_freq = -1, send_freq = -1;
1591
1592 if (wpa_s->p2p_listen_work)
1593 listen_freq = wpa_s->p2p_listen_work->freq;
1594 if (wpa_s->p2p_send_action_work)
1595 send_freq = wpa_s->p2p_send_action_work->freq;
1596 if (listen_freq != (int) freq && send_freq != (int) freq) {
1597 wpa_printf(MSG_DEBUG, "P2P: Schedule new radio work for Action frame TX (listen_freq=%d send_freq=%d)",
1598 listen_freq, send_freq);
1599 return wpas_send_action_work(wpa_s, freq, dst, src, bssid, buf,
1600 len, wait_time);
1601 }
1602
1603 wpa_printf(MSG_DEBUG, "P2P: Use ongoing radio work for Action frame TX");
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001604 return offchannel_send_action(wpa_s, freq, dst, src, bssid, buf, len,
1605 wait_time,
1606 wpas_p2p_send_action_tx_status, 1);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001607}
1608
1609
1610static void wpas_send_action_done(void *ctx)
1611{
1612 struct wpa_supplicant *wpa_s = ctx;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001613
1614 if (wpa_s->p2p_send_action_work) {
1615 eloop_cancel_timeout(wpas_p2p_send_action_work_timeout,
1616 wpa_s, NULL);
1617 os_free(wpa_s->p2p_send_action_work->ctx);
1618 radio_work_done(wpa_s->p2p_send_action_work);
1619 wpa_s->p2p_send_action_work = NULL;
1620 }
1621
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001622 offchannel_send_action_done(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001623}
1624
1625
1626static int wpas_copy_go_neg_results(struct wpa_supplicant *wpa_s,
1627 struct p2p_go_neg_results *params)
1628{
1629 if (wpa_s->go_params == NULL) {
1630 wpa_s->go_params = os_malloc(sizeof(*params));
1631 if (wpa_s->go_params == NULL)
1632 return -1;
1633 }
1634 os_memcpy(wpa_s->go_params, params, sizeof(*params));
1635 return 0;
1636}
1637
1638
1639static void wpas_start_wps_enrollee(struct wpa_supplicant *wpa_s,
1640 struct p2p_go_neg_results *res)
1641{
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001642 wpa_s->group_formation_reported = 0;
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001643 wpa_printf(MSG_DEBUG, "P2P: Start WPS Enrollee for peer " MACSTR
1644 " dev_addr " MACSTR " wps_method %d",
1645 MAC2STR(res->peer_interface_addr),
1646 MAC2STR(res->peer_device_addr), res->wps_method);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001647 wpa_hexdump_ascii(MSG_DEBUG, "P2P: Start WPS Enrollee for SSID",
1648 res->ssid, res->ssid_len);
1649 wpa_supplicant_ap_deinit(wpa_s);
1650 wpas_copy_go_neg_results(wpa_s, res);
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001651 if (res->wps_method == WPS_PBC) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001652 wpas_wps_start_pbc(wpa_s, res->peer_interface_addr, 1);
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001653#ifdef CONFIG_WPS_NFC
1654 } else if (res->wps_method == WPS_NFC) {
1655 wpas_wps_start_nfc(wpa_s, res->peer_device_addr,
1656 res->peer_interface_addr,
Dmitry Shmidt9c175262016-03-03 10:20:07 -08001657 wpa_s->p2pdev->p2p_oob_dev_pw,
1658 wpa_s->p2pdev->p2p_oob_dev_pw_id, 1,
1659 wpa_s->p2pdev->p2p_oob_dev_pw_id ==
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001660 DEV_PW_NFC_CONNECTION_HANDOVER ?
Dmitry Shmidt9c175262016-03-03 10:20:07 -08001661 wpa_s->p2pdev->p2p_peer_oob_pubkey_hash :
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001662 NULL,
1663 NULL, 0, 0);
1664#endif /* CONFIG_WPS_NFC */
1665 } else {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001666 u16 dev_pw_id = DEV_PW_DEFAULT;
Dmitry Shmidt216983b2015-02-06 10:50:36 -08001667 if (wpa_s->p2p_wps_method == WPS_P2PS)
1668 dev_pw_id = DEV_PW_P2PS_DEFAULT;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001669 if (wpa_s->p2p_wps_method == WPS_PIN_KEYPAD)
1670 dev_pw_id = DEV_PW_REGISTRAR_SPECIFIED;
1671 wpas_wps_start_pin(wpa_s, res->peer_interface_addr,
1672 wpa_s->p2p_pin, 1, dev_pw_id);
1673 }
1674}
1675
1676
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07001677static void wpas_p2p_add_psk_list(struct wpa_supplicant *wpa_s,
1678 struct wpa_ssid *ssid)
1679{
1680 struct wpa_ssid *persistent;
1681 struct psk_list_entry *psk;
1682 struct hostapd_data *hapd;
1683
1684 if (!wpa_s->ap_iface)
1685 return;
1686
Dmitry Shmidt9c175262016-03-03 10:20:07 -08001687 persistent = wpas_p2p_get_persistent(wpa_s->p2pdev, NULL, ssid->ssid,
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07001688 ssid->ssid_len);
1689 if (persistent == NULL)
1690 return;
1691
1692 hapd = wpa_s->ap_iface->bss[0];
1693
1694 dl_list_for_each(psk, &persistent->psk_list, struct psk_list_entry,
1695 list) {
1696 struct hostapd_wpa_psk *hpsk;
1697
1698 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Add persistent group PSK entry for "
1699 MACSTR " psk=%d",
1700 MAC2STR(psk->addr), psk->p2p);
1701 hpsk = os_zalloc(sizeof(*hpsk));
1702 if (hpsk == NULL)
1703 break;
1704 os_memcpy(hpsk->psk, psk->psk, PMK_LEN);
1705 if (psk->p2p)
1706 os_memcpy(hpsk->p2p_dev_addr, psk->addr, ETH_ALEN);
1707 else
1708 os_memcpy(hpsk->addr, psk->addr, ETH_ALEN);
1709 hpsk->next = hapd->conf->ssid.wpa_psk;
1710 hapd->conf->ssid.wpa_psk = hpsk;
1711 }
1712}
1713
1714
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001715static void p2p_go_dump_common_freqs(struct wpa_supplicant *wpa_s)
1716{
Roshan Pius3a1667e2018-07-03 15:17:14 -07001717 char buf[20 + P2P_MAX_CHANNELS * 6];
1718 char *pos, *end;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001719 unsigned int i;
Roshan Pius3a1667e2018-07-03 15:17:14 -07001720 int res;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001721
Roshan Pius3a1667e2018-07-03 15:17:14 -07001722 pos = buf;
1723 end = pos + sizeof(buf);
1724 for (i = 0; i < wpa_s->p2p_group_common_freqs_num; i++) {
1725 res = os_snprintf(pos, end - pos, " %d",
1726 wpa_s->p2p_group_common_freqs[i]);
1727 if (os_snprintf_error(end - pos, res))
1728 break;
1729 pos += res;
1730 }
1731 *pos = '\0';
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001732
Roshan Pius3a1667e2018-07-03 15:17:14 -07001733 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Common group frequencies:%s", buf);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001734}
1735
1736
1737static void p2p_go_save_group_common_freqs(struct wpa_supplicant *wpa_s,
1738 struct p2p_go_neg_results *params)
1739{
1740 unsigned int i, len = int_array_len(wpa_s->go_params->freq_list);
1741
1742 wpa_s->p2p_group_common_freqs_num = 0;
1743 os_free(wpa_s->p2p_group_common_freqs);
1744 wpa_s->p2p_group_common_freqs = os_calloc(len, sizeof(int));
1745 if (!wpa_s->p2p_group_common_freqs)
1746 return;
1747
1748 for (i = 0; i < len; i++) {
1749 if (!wpa_s->go_params->freq_list[i])
1750 break;
1751 wpa_s->p2p_group_common_freqs[i] =
1752 wpa_s->go_params->freq_list[i];
1753 }
1754 wpa_s->p2p_group_common_freqs_num = i;
1755}
1756
1757
Dmitry Shmidt216983b2015-02-06 10:50:36 -08001758static void p2p_config_write(struct wpa_supplicant *wpa_s)
1759{
1760#ifndef CONFIG_NO_CONFIG_WRITE
Dmitry Shmidt9c175262016-03-03 10:20:07 -08001761 if (wpa_s->p2pdev->conf->update_config &&
1762 wpa_config_write(wpa_s->p2pdev->confname, wpa_s->p2pdev->conf))
Dmitry Shmidt216983b2015-02-06 10:50:36 -08001763 wpa_printf(MSG_DEBUG, "P2P: Failed to update configuration");
1764#endif /* CONFIG_NO_CONFIG_WRITE */
1765}
1766
1767
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001768static void p2p_go_configured(void *ctx, void *data)
1769{
1770 struct wpa_supplicant *wpa_s = ctx;
1771 struct p2p_go_neg_results *params = data;
1772 struct wpa_ssid *ssid;
1773
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08001774 wpa_s->ap_configured_cb = NULL;
1775 wpa_s->ap_configured_cb_ctx = NULL;
1776 wpa_s->ap_configured_cb_data = NULL;
1777 if (!wpa_s->go_params) {
1778 wpa_printf(MSG_ERROR,
1779 "P2P: p2p_go_configured() called with wpa_s->go_params == NULL");
1780 return;
1781 }
1782
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001783 p2p_go_save_group_common_freqs(wpa_s, params);
1784 p2p_go_dump_common_freqs(wpa_s);
1785
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001786 ssid = wpa_s->current_ssid;
1787 if (ssid && ssid->mode == WPAS_MODE_P2P_GO) {
1788 wpa_printf(MSG_DEBUG, "P2P: Group setup without provisioning");
1789 if (wpa_s->global->p2p_group_formation == wpa_s)
1790 wpa_s->global->p2p_group_formation = NULL;
Dmitry Shmidt7f0b69e2014-07-28 10:35:20 -07001791 wpas_p2p_group_started(wpa_s, 1, ssid, ssid->frequency,
1792 params->passphrase[0] == '\0' ?
1793 params->psk : NULL,
1794 params->passphrase,
1795 wpa_s->global->p2p_dev_addr,
1796 params->persistent_group, "");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001797 wpa_s->group_formation_reported = 1;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001798
Dmitry Shmidt9c175262016-03-03 10:20:07 -08001799 if (wpa_s->p2pdev->p2ps_method_config_any) {
1800 if (is_zero_ether_addr(wpa_s->p2pdev->p2ps_join_addr)) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001801 wpa_dbg(wpa_s, MSG_DEBUG,
1802 "P2PS: Setting default PIN for ANY");
1803 wpa_supplicant_ap_wps_pin(wpa_s, NULL,
1804 "12345670", NULL, 0,
1805 0);
1806 } else {
1807 wpa_dbg(wpa_s, MSG_DEBUG,
1808 "P2PS: Setting default PIN for " MACSTR,
Dmitry Shmidt9c175262016-03-03 10:20:07 -08001809 MAC2STR(wpa_s->p2pdev->p2ps_join_addr));
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001810 wpa_supplicant_ap_wps_pin(
Dmitry Shmidt9c175262016-03-03 10:20:07 -08001811 wpa_s, wpa_s->p2pdev->p2ps_join_addr,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001812 "12345670", NULL, 0, 0);
1813 }
Dmitry Shmidt9c175262016-03-03 10:20:07 -08001814 wpa_s->p2pdev->p2ps_method_config_any = 0;
Dmitry Shmidt216983b2015-02-06 10:50:36 -08001815 }
1816
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001817 os_get_reltime(&wpa_s->global->p2p_go_wait_client);
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07001818 if (params->persistent_group) {
Dmitry Shmidtd5ab1b52016-06-21 12:38:41 -07001819 wpas_p2p_store_persistent_group(
Dmitry Shmidt9c175262016-03-03 10:20:07 -08001820 wpa_s->p2pdev, ssid,
Dmitry Shmidt497c1d52011-07-21 15:19:46 -07001821 wpa_s->global->p2p_dev_addr);
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07001822 wpas_p2p_add_psk_list(wpa_s, ssid);
1823 }
Dmitry Shmidtd5ab1b52016-06-21 12:38:41 -07001824
1825 wpas_notify_p2p_group_started(wpa_s, ssid,
Dmitry Shmidtabb90a32016-12-05 15:34:39 -08001826 params->persistent_group, 0,
1827 NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001828 wpas_p2p_cross_connect_setup(wpa_s);
1829 wpas_p2p_set_group_idle_timeout(wpa_s);
Dmitry Shmidt56052862013-10-04 10:23:25 -07001830
1831 if (wpa_s->p2p_first_connection_timeout) {
1832 wpa_dbg(wpa_s, MSG_DEBUG,
1833 "P2P: Start group formation timeout of %d seconds until first data connection on GO",
1834 wpa_s->p2p_first_connection_timeout);
1835 wpa_s->p2p_go_group_formation_completed = 0;
1836 wpa_s->global->p2p_group_formation = wpa_s;
1837 eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
Dmitry Shmidt9c175262016-03-03 10:20:07 -08001838 wpa_s->p2pdev, NULL);
Dmitry Shmidt56052862013-10-04 10:23:25 -07001839 eloop_register_timeout(
1840 wpa_s->p2p_first_connection_timeout, 0,
1841 wpas_p2p_group_formation_timeout,
Dmitry Shmidt9c175262016-03-03 10:20:07 -08001842 wpa_s->p2pdev, NULL);
Dmitry Shmidt56052862013-10-04 10:23:25 -07001843 }
1844
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001845 return;
1846 }
1847
1848 wpa_printf(MSG_DEBUG, "P2P: Setting up WPS for GO provisioning");
1849 if (wpa_supplicant_ap_mac_addr_filter(wpa_s,
1850 params->peer_interface_addr)) {
1851 wpa_printf(MSG_DEBUG, "P2P: Failed to setup MAC address "
1852 "filtering");
1853 return;
1854 }
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001855 if (params->wps_method == WPS_PBC) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001856 wpa_supplicant_ap_wps_pbc(wpa_s, params->peer_interface_addr,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001857 params->peer_device_addr);
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001858#ifdef CONFIG_WPS_NFC
1859 } else if (params->wps_method == WPS_NFC) {
Dmitry Shmidt9c175262016-03-03 10:20:07 -08001860 if (wpa_s->p2pdev->p2p_oob_dev_pw_id !=
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001861 DEV_PW_NFC_CONNECTION_HANDOVER &&
Dmitry Shmidt9c175262016-03-03 10:20:07 -08001862 !wpa_s->p2pdev->p2p_oob_dev_pw) {
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001863 wpa_printf(MSG_DEBUG, "P2P: No NFC Dev Pw known");
1864 return;
1865 }
1866 wpas_ap_wps_add_nfc_pw(
Dmitry Shmidt9c175262016-03-03 10:20:07 -08001867 wpa_s, wpa_s->p2pdev->p2p_oob_dev_pw_id,
1868 wpa_s->p2pdev->p2p_oob_dev_pw,
1869 wpa_s->p2pdev->p2p_peer_oob_pk_hash_known ?
1870 wpa_s->p2pdev->p2p_peer_oob_pubkey_hash : NULL);
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001871#endif /* CONFIG_WPS_NFC */
1872 } else if (wpa_s->p2p_pin[0])
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001873 wpa_supplicant_ap_wps_pin(wpa_s, params->peer_interface_addr,
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001874 wpa_s->p2p_pin, NULL, 0, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001875 os_free(wpa_s->go_params);
1876 wpa_s->go_params = NULL;
1877}
1878
1879
1880static void wpas_start_wps_go(struct wpa_supplicant *wpa_s,
1881 struct p2p_go_neg_results *params,
1882 int group_formation)
1883{
1884 struct wpa_ssid *ssid;
1885
Dmitry Shmidtaa532512012-09-24 10:35:31 -07001886 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Starting GO");
1887 if (wpas_copy_go_neg_results(wpa_s, params) < 0) {
1888 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Could not copy GO Negotiation "
1889 "results");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001890 return;
Dmitry Shmidtaa532512012-09-24 10:35:31 -07001891 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001892
1893 ssid = wpa_config_add_network(wpa_s->conf);
Dmitry Shmidtaa532512012-09-24 10:35:31 -07001894 if (ssid == NULL) {
1895 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Could not add network for GO");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001896 return;
Dmitry Shmidtaa532512012-09-24 10:35:31 -07001897 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001898
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001899 wpa_s->show_group_started = 0;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001900 wpa_s->p2p_go_group_formation_completed = 0;
1901 wpa_s->group_formation_reported = 0;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001902 os_memset(wpa_s->go_dev_addr, 0, ETH_ALEN);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001903
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001904 wpa_config_set_network_defaults(ssid);
1905 ssid->temporary = 1;
1906 ssid->p2p_group = 1;
Dmitry Shmidtaca489e2016-09-28 15:44:14 -07001907 ssid->p2p_persistent_group = !!params->persistent_group;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001908 ssid->mode = group_formation ? WPAS_MODE_P2P_GROUP_FORMATION :
1909 WPAS_MODE_P2P_GO;
1910 ssid->frequency = params->freq;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001911 ssid->ht40 = params->ht40;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07001912 ssid->vht = params->vht;
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08001913 ssid->max_oper_chwidth = params->max_oper_chwidth;
1914 ssid->vht_center_freq2 = params->vht_center_freq2;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001915 ssid->ssid = os_zalloc(params->ssid_len + 1);
1916 if (ssid->ssid) {
1917 os_memcpy(ssid->ssid, params->ssid, params->ssid_len);
1918 ssid->ssid_len = params->ssid_len;
1919 }
1920 ssid->auth_alg = WPA_AUTH_ALG_OPEN;
1921 ssid->key_mgmt = WPA_KEY_MGMT_PSK;
1922 ssid->proto = WPA_PROTO_RSN;
1923 ssid->pairwise_cipher = WPA_CIPHER_CCMP;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001924 ssid->group_cipher = WPA_CIPHER_CCMP;
1925 if (params->freq > 56160) {
1926 /*
1927 * Enable GCMP instead of CCMP as pairwise_cipher and
1928 * group_cipher in 60 GHz.
1929 */
1930 ssid->pairwise_cipher = WPA_CIPHER_GCMP;
1931 ssid->group_cipher = WPA_CIPHER_GCMP;
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08001932 /* P2P GO in 60 GHz is always a PCP (PBSS) */
1933 ssid->pbss = 1;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001934 }
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001935 if (os_strlen(params->passphrase) > 0) {
1936 ssid->passphrase = os_strdup(params->passphrase);
1937 if (ssid->passphrase == NULL) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07001938 wpa_msg_global(wpa_s, MSG_ERROR,
1939 "P2P: Failed to copy passphrase for GO");
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001940 wpa_config_remove_network(wpa_s->conf, ssid->id);
1941 return;
1942 }
1943 } else
1944 ssid->passphrase = NULL;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001945 ssid->psk_set = params->psk_set;
1946 if (ssid->psk_set)
1947 os_memcpy(ssid->psk, params->psk, sizeof(ssid->psk));
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001948 else if (ssid->passphrase)
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001949 wpa_config_update_psk(ssid);
Dmitry Shmidt9c175262016-03-03 10:20:07 -08001950 ssid->ap_max_inactivity = wpa_s->p2pdev->conf->p2p_go_max_inactivity;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001951
1952 wpa_s->ap_configured_cb = p2p_go_configured;
1953 wpa_s->ap_configured_cb_ctx = wpa_s;
1954 wpa_s->ap_configured_cb_data = wpa_s->go_params;
Dmitry Shmidt6dc03bd2014-05-16 10:40:13 -07001955 wpa_s->scan_req = NORMAL_SCAN_REQ;
Jouni Malinen75ecf522011-06-27 15:19:46 -07001956 wpa_s->connect_without_scan = ssid;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001957 wpa_s->reassociate = 1;
1958 wpa_s->disconnected = 0;
Dmitry Shmidtaa532512012-09-24 10:35:31 -07001959 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Request scan (that will be skipped) to "
1960 "start GO)");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001961 wpa_supplicant_req_scan(wpa_s, 0, 0);
1962}
1963
1964
1965static void wpas_p2p_clone_config(struct wpa_supplicant *dst,
1966 const struct wpa_supplicant *src)
1967{
1968 struct wpa_config *d;
1969 const struct wpa_config *s;
1970
1971 d = dst->conf;
1972 s = src->conf;
1973
Dmitry Shmidt849734c2016-05-27 09:59:01 -07001974#define C(n) \
1975do { \
1976 if (s->n && !d->n) \
1977 d->n = os_strdup(s->n); \
1978} while (0)
1979
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001980 C(device_name);
1981 C(manufacturer);
1982 C(model_name);
1983 C(model_number);
1984 C(serial_number);
1985 C(config_methods);
1986#undef C
1987
1988 os_memcpy(d->device_type, s->device_type, WPS_DEV_TYPE_LEN);
1989 os_memcpy(d->sec_device_type, s->sec_device_type,
1990 sizeof(d->sec_device_type));
1991 d->num_sec_device_types = s->num_sec_device_types;
1992
1993 d->p2p_group_idle = s->p2p_group_idle;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001994 d->p2p_go_freq_change_policy = s->p2p_go_freq_change_policy;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001995 d->p2p_intra_bss = s->p2p_intra_bss;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001996 d->persistent_reconnect = s->persistent_reconnect;
Dmitry Shmidt04949592012-07-19 12:16:46 -07001997 d->max_num_sta = s->max_num_sta;
1998 d->pbc_in_m1 = s->pbc_in_m1;
Dmitry Shmidt444d5672013-04-01 13:08:44 -07001999 d->ignore_old_scan_res = s->ignore_old_scan_res;
Dmitry Shmidt51b6ea82013-05-08 10:42:09 -07002000 d->beacon_int = s->beacon_int;
Dmitry Shmidt18463232014-01-24 12:29:41 -08002001 d->dtim_period = s->dtim_period;
Dmitry Shmidt7f656022015-02-25 14:36:37 -08002002 d->p2p_go_ctwindow = s->p2p_go_ctwindow;
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07002003 d->disassoc_low_ack = s->disassoc_low_ack;
Dmitry Shmidt96571392013-10-14 12:54:46 -07002004 d->disable_scan_offload = s->disable_scan_offload;
Dmitry Shmidt807291d2015-01-27 13:40:23 -08002005 d->passive_scan = s->passive_scan;
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08002006
Dmitry Shmidt849734c2016-05-27 09:59:01 -07002007 if (s->wps_nfc_dh_privkey && s->wps_nfc_dh_pubkey &&
2008 !d->wps_nfc_pw_from_config) {
2009 wpabuf_free(d->wps_nfc_dh_privkey);
2010 wpabuf_free(d->wps_nfc_dh_pubkey);
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08002011 d->wps_nfc_dh_privkey = wpabuf_dup(s->wps_nfc_dh_privkey);
2012 d->wps_nfc_dh_pubkey = wpabuf_dup(s->wps_nfc_dh_pubkey);
2013 }
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07002014 d->p2p_cli_probe = s->p2p_cli_probe;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002015 d->go_interworking = s->go_interworking;
2016 d->go_access_network_type = s->go_access_network_type;
2017 d->go_internet = s->go_internet;
2018 d->go_venue_group = s->go_venue_group;
2019 d->go_venue_type = s->go_venue_type;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002020}
2021
2022
Dmitry Shmidt34af3062013-07-11 10:46:32 -07002023static void wpas_p2p_get_group_ifname(struct wpa_supplicant *wpa_s,
2024 char *ifname, size_t len)
2025{
2026 char *ifname_ptr = wpa_s->ifname;
2027
2028 if (os_strncmp(wpa_s->ifname, P2P_MGMT_DEVICE_PREFIX,
2029 os_strlen(P2P_MGMT_DEVICE_PREFIX)) == 0) {
2030 ifname_ptr = os_strrchr(wpa_s->ifname, '-') + 1;
2031 }
2032
2033 os_snprintf(ifname, len, "p2p-%s-%d", ifname_ptr, wpa_s->p2p_group_idx);
2034 if (os_strlen(ifname) >= IFNAMSIZ &&
2035 os_strlen(wpa_s->ifname) < IFNAMSIZ) {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002036 int res;
2037
Dmitry Shmidt34af3062013-07-11 10:46:32 -07002038 /* Try to avoid going over the IFNAMSIZ length limit */
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002039 res = os_snprintf(ifname, len, "p2p-%d", wpa_s->p2p_group_idx);
2040 if (os_snprintf_error(len, res) && len)
2041 ifname[len - 1] = '\0';
Dmitry Shmidt34af3062013-07-11 10:46:32 -07002042 }
2043}
2044
2045
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002046static int wpas_p2p_add_group_interface(struct wpa_supplicant *wpa_s,
2047 enum wpa_driver_if_type type)
2048{
2049 char ifname[120], force_ifname[120];
2050
2051 if (wpa_s->pending_interface_name[0]) {
2052 wpa_printf(MSG_DEBUG, "P2P: Pending virtual interface exists "
2053 "- skip creation of a new one");
2054 if (is_zero_ether_addr(wpa_s->pending_interface_addr)) {
2055 wpa_printf(MSG_DEBUG, "P2P: Pending virtual address "
2056 "unknown?! ifname='%s'",
2057 wpa_s->pending_interface_name);
2058 return -1;
2059 }
2060 return 0;
2061 }
2062
Dmitry Shmidt34af3062013-07-11 10:46:32 -07002063 wpas_p2p_get_group_ifname(wpa_s, ifname, sizeof(ifname));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002064 force_ifname[0] = '\0';
2065
2066 wpa_printf(MSG_DEBUG, "P2P: Create a new interface %s for the group",
2067 ifname);
2068 wpa_s->p2p_group_idx++;
2069
2070 wpa_s->pending_interface_type = type;
2071 if (wpa_drv_if_add(wpa_s, type, ifname, NULL, NULL, force_ifname,
2072 wpa_s->pending_interface_addr, NULL) < 0) {
2073 wpa_printf(MSG_ERROR, "P2P: Failed to create new group "
2074 "interface");
2075 return -1;
2076 }
2077
2078 if (force_ifname[0]) {
2079 wpa_printf(MSG_DEBUG, "P2P: Driver forced interface name %s",
2080 force_ifname);
2081 os_strlcpy(wpa_s->pending_interface_name, force_ifname,
2082 sizeof(wpa_s->pending_interface_name));
2083 } else
2084 os_strlcpy(wpa_s->pending_interface_name, ifname,
2085 sizeof(wpa_s->pending_interface_name));
2086 wpa_printf(MSG_DEBUG, "P2P: Created pending virtual interface %s addr "
2087 MACSTR, wpa_s->pending_interface_name,
2088 MAC2STR(wpa_s->pending_interface_addr));
2089
2090 return 0;
2091}
2092
2093
2094static void wpas_p2p_remove_pending_group_interface(
2095 struct wpa_supplicant *wpa_s)
2096{
2097 if (!wpa_s->pending_interface_name[0] ||
2098 is_zero_ether_addr(wpa_s->pending_interface_addr))
2099 return; /* No pending virtual interface */
2100
2101 wpa_printf(MSG_DEBUG, "P2P: Removing pending group interface %s",
2102 wpa_s->pending_interface_name);
2103 wpa_drv_if_remove(wpa_s, wpa_s->pending_interface_type,
2104 wpa_s->pending_interface_name);
2105 os_memset(wpa_s->pending_interface_addr, 0, ETH_ALEN);
2106 wpa_s->pending_interface_name[0] = '\0';
Dmitry Shmidt216983b2015-02-06 10:50:36 -08002107 wpa_s->global->pending_group_iface_for_p2ps = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002108}
2109
2110
2111static struct wpa_supplicant *
2112wpas_p2p_init_group_interface(struct wpa_supplicant *wpa_s, int go)
2113{
2114 struct wpa_interface iface;
2115 struct wpa_supplicant *group_wpa_s;
2116
2117 if (!wpa_s->pending_interface_name[0]) {
2118 wpa_printf(MSG_ERROR, "P2P: No pending group interface");
2119 if (!wpas_p2p_create_iface(wpa_s))
2120 return NULL;
2121 /*
2122 * Something has forced us to remove the pending interface; try
2123 * to create a new one and hope for the best that we will get
2124 * the same local address.
2125 */
2126 if (wpas_p2p_add_group_interface(wpa_s, go ? WPA_IF_P2P_GO :
2127 WPA_IF_P2P_CLIENT) < 0)
2128 return NULL;
2129 }
2130
2131 os_memset(&iface, 0, sizeof(iface));
2132 iface.ifname = wpa_s->pending_interface_name;
2133 iface.driver = wpa_s->driver->name;
Dmitry Shmidt34af3062013-07-11 10:46:32 -07002134 if (wpa_s->conf->ctrl_interface == NULL &&
2135 wpa_s->parent != wpa_s &&
2136 wpa_s->p2p_mgmt &&
2137 (wpa_s->drv_flags & WPA_DRIVER_FLAGS_DEDICATED_P2P_DEVICE))
2138 iface.ctrl_interface = wpa_s->parent->conf->ctrl_interface;
2139 else
2140 iface.ctrl_interface = wpa_s->conf->ctrl_interface;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002141 iface.driver_param = wpa_s->conf->driver_param;
Dmitry Shmidt203eadb2015-03-05 14:16:04 -08002142 group_wpa_s = wpa_supplicant_add_iface(wpa_s->global, &iface, wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002143 if (group_wpa_s == NULL) {
2144 wpa_printf(MSG_ERROR, "P2P: Failed to create new "
2145 "wpa_supplicant interface");
2146 return NULL;
2147 }
2148 wpa_s->pending_interface_name[0] = '\0';
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002149 group_wpa_s->p2p_group_interface = go ? P2P_GROUP_INTERFACE_GO :
2150 P2P_GROUP_INTERFACE_CLIENT;
2151 wpa_s->global->p2p_group_formation = group_wpa_s;
Dmitry Shmidt216983b2015-02-06 10:50:36 -08002152 wpa_s->global->pending_group_iface_for_p2ps = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002153
2154 wpas_p2p_clone_config(group_wpa_s, wpa_s);
2155
2156 return group_wpa_s;
2157}
2158
2159
2160static void wpas_p2p_group_formation_timeout(void *eloop_ctx,
2161 void *timeout_ctx)
2162{
2163 struct wpa_supplicant *wpa_s = eloop_ctx;
2164 wpa_printf(MSG_DEBUG, "P2P: Group Formation timed out");
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07002165 wpas_p2p_group_formation_failed(wpa_s, 0);
Dmitry Shmidt34af3062013-07-11 10:46:32 -07002166}
2167
2168
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07002169static void wpas_p2p_group_formation_failed(struct wpa_supplicant *wpa_s,
2170 int already_deleted)
Dmitry Shmidt34af3062013-07-11 10:46:32 -07002171{
2172 eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
Dmitry Shmidt9c175262016-03-03 10:20:07 -08002173 wpa_s->p2pdev, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002174 if (wpa_s->global->p2p)
2175 p2p_group_formation_failed(wpa_s->global->p2p);
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07002176 wpas_group_formation_completed(wpa_s, 0, already_deleted);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002177}
2178
2179
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002180static void wpas_p2p_grpform_fail_after_wps(struct wpa_supplicant *wpa_s)
2181{
2182 wpa_printf(MSG_DEBUG, "P2P: Reject group formation due to WPS provisioning failure");
2183 eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
Dmitry Shmidt9c175262016-03-03 10:20:07 -08002184 wpa_s->p2pdev, NULL);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002185 eloop_register_timeout(0, 0, wpas_p2p_group_formation_timeout,
Dmitry Shmidt9c175262016-03-03 10:20:07 -08002186 wpa_s->p2pdev, NULL);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002187 wpa_s->global->p2p_fail_on_wps_complete = 0;
2188}
2189
2190
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07002191void wpas_p2p_ap_setup_failed(struct wpa_supplicant *wpa_s)
2192{
2193 if (wpa_s->global->p2p_group_formation != wpa_s)
2194 return;
2195 /* Speed up group formation timeout since this cannot succeed */
2196 eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
Dmitry Shmidt9c175262016-03-03 10:20:07 -08002197 wpa_s->p2pdev, NULL);
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07002198 eloop_register_timeout(0, 0, wpas_p2p_group_formation_timeout,
Dmitry Shmidt9c175262016-03-03 10:20:07 -08002199 wpa_s->p2pdev, NULL);
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07002200}
2201
2202
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002203static void wpas_go_neg_completed(void *ctx, struct p2p_go_neg_results *res)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002204{
2205 struct wpa_supplicant *wpa_s = ctx;
Dmitry Shmidt9c175262016-03-03 10:20:07 -08002206 struct wpa_supplicant *group_wpa_s;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002207
2208 if (wpa_s->off_channel_freq || wpa_s->roc_waiting_drv_freq) {
2209 wpa_drv_cancel_remain_on_channel(wpa_s);
2210 wpa_s->off_channel_freq = 0;
2211 wpa_s->roc_waiting_drv_freq = 0;
2212 }
2213
2214 if (res->status) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07002215 wpa_msg_global(wpa_s, MSG_INFO,
2216 P2P_EVENT_GO_NEG_FAILURE "status=%d",
2217 res->status);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002218 wpas_notify_p2p_go_neg_completed(wpa_s, res);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002219 wpas_p2p_remove_pending_group_interface(wpa_s);
2220 return;
2221 }
2222
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002223 if (!res->role_go) {
2224 /* Inform driver of the operating channel of GO. */
2225 wpa_drv_set_prob_oper_freq(wpa_s, res->freq);
2226 }
2227
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002228 if (wpa_s->p2p_go_ht40)
2229 res->ht40 = 1;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07002230 if (wpa_s->p2p_go_vht)
2231 res->vht = 1;
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08002232 res->max_oper_chwidth = wpa_s->p2p_go_max_oper_chwidth;
2233 res->vht_center_freq2 = wpa_s->p2p_go_vht_center_freq2;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002234
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07002235 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_GO_NEG_SUCCESS "role=%s "
2236 "freq=%d ht40=%d peer_dev=" MACSTR " peer_iface=" MACSTR
2237 " wps_method=%s",
2238 res->role_go ? "GO" : "client", res->freq, res->ht40,
2239 MAC2STR(res->peer_device_addr),
2240 MAC2STR(res->peer_interface_addr),
2241 p2p_wps_method_text(res->wps_method));
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002242 wpas_notify_p2p_go_neg_completed(wpa_s, res);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002243
Dmitry Shmidt04949592012-07-19 12:16:46 -07002244 if (res->role_go && wpa_s->p2p_persistent_id >= 0) {
2245 struct wpa_ssid *ssid;
2246 ssid = wpa_config_get_network(wpa_s->conf,
2247 wpa_s->p2p_persistent_id);
2248 if (ssid && ssid->disabled == 2 &&
2249 ssid->mode == WPAS_MODE_P2P_GO && ssid->passphrase) {
2250 size_t len = os_strlen(ssid->passphrase);
2251 wpa_printf(MSG_DEBUG, "P2P: Override passphrase based "
2252 "on requested persistent group");
2253 os_memcpy(res->passphrase, ssid->passphrase, len);
2254 res->passphrase[len] = '\0';
2255 }
2256 }
2257
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002258 if (wpa_s->create_p2p_iface) {
Dmitry Shmidt9c175262016-03-03 10:20:07 -08002259 group_wpa_s =
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002260 wpas_p2p_init_group_interface(wpa_s, res->role_go);
2261 if (group_wpa_s == NULL) {
2262 wpas_p2p_remove_pending_group_interface(wpa_s);
Dmitry Shmidtd11f0192014-03-24 12:09:47 -07002263 eloop_cancel_timeout(wpas_p2p_long_listen_timeout,
2264 wpa_s, NULL);
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07002265 wpas_p2p_group_formation_failed(wpa_s, 1);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002266 return;
2267 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002268 os_memset(wpa_s->pending_interface_addr, 0, ETH_ALEN);
2269 wpa_s->pending_interface_name[0] = '\0';
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002270 } else {
Dmitry Shmidt9c175262016-03-03 10:20:07 -08002271 group_wpa_s = wpa_s->parent;
2272 wpa_s->global->p2p_group_formation = group_wpa_s;
2273 if (group_wpa_s != wpa_s)
Dmitry Shmidt849734c2016-05-27 09:59:01 -07002274 wpas_p2p_clone_config(group_wpa_s, wpa_s);
Dmitry Shmidt9c175262016-03-03 10:20:07 -08002275 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002276
Dmitry Shmidt9c175262016-03-03 10:20:07 -08002277 group_wpa_s->p2p_in_provisioning = 1;
2278 group_wpa_s->p2pdev = wpa_s;
2279 if (group_wpa_s != wpa_s) {
2280 os_memcpy(group_wpa_s->p2p_pin, wpa_s->p2p_pin,
2281 sizeof(group_wpa_s->p2p_pin));
2282 group_wpa_s->p2p_wps_method = wpa_s->p2p_wps_method;
2283 }
2284 if (res->role_go) {
2285 wpas_start_wps_go(group_wpa_s, res, 1);
2286 } else {
2287 os_get_reltime(&group_wpa_s->scan_min_time);
2288 wpas_start_wps_enrollee(group_wpa_s, res);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002289 }
2290
2291 wpa_s->p2p_long_listen = 0;
2292 eloop_cancel_timeout(wpas_p2p_long_listen_timeout, wpa_s, NULL);
2293
2294 eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s, NULL);
2295 eloop_register_timeout(15 + res->peer_config_timeout / 100,
2296 (res->peer_config_timeout % 100) * 10000,
2297 wpas_p2p_group_formation_timeout, wpa_s, NULL);
2298}
2299
2300
Dmitry Shmidt7a53dbb2015-06-11 13:13:53 -07002301static void wpas_go_neg_req_rx(void *ctx, const u8 *src, u16 dev_passwd_id,
2302 u8 go_intent)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002303{
2304 struct wpa_supplicant *wpa_s = ctx;
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07002305 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_GO_NEG_REQUEST MACSTR
Dmitry Shmidt7a53dbb2015-06-11 13:13:53 -07002306 " dev_passwd_id=%u go_intent=%u", MAC2STR(src),
2307 dev_passwd_id, go_intent);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002308
Dmitry Shmidt7a53dbb2015-06-11 13:13:53 -07002309 wpas_notify_p2p_go_neg_req(wpa_s, src, dev_passwd_id, go_intent);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002310}
2311
2312
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002313static void wpas_dev_found(void *ctx, const u8 *addr,
2314 const struct p2p_peer_info *info,
2315 int new_device)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002316{
Roshan Piusfd2fd662017-01-23 13:41:57 -08002317 u8 *wfd_dev_info = NULL;
2318 u8 wfd_dev_info_len = 0;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002319#ifndef CONFIG_NO_STDOUT_DEBUG
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002320 struct wpa_supplicant *wpa_s = ctx;
2321 char devtype[WPS_DEV_TYPE_BUFSIZE];
Dmitry Shmidt04f534e2013-12-09 15:50:16 -08002322 char *wfd_dev_info_hex = NULL;
2323
Irfan Sheriff8367dc92012-09-09 17:08:19 -07002324#ifdef CONFIG_WIFI_DISPLAY
Dmitry Shmidt04f534e2013-12-09 15:50:16 -08002325 wfd_dev_info_hex = wifi_display_subelem_hex(info->wfd_subelems,
2326 WFD_SUBELEM_DEVICE_INFO);
Roshan Piusfd2fd662017-01-23 13:41:57 -08002327 if (wfd_dev_info_hex) {
2328 wfd_dev_info_len = strlen(wfd_dev_info_hex) / 2;
2329 wfd_dev_info = os_zalloc(wfd_dev_info_len);
2330 // Only used for notification, so not handling error.
2331 hexstr2bin(wfd_dev_info_hex, wfd_dev_info, wfd_dev_info_len);
2332 }
Irfan Sheriff8367dc92012-09-09 17:08:19 -07002333#endif /* CONFIG_WIFI_DISPLAY */
Dmitry Shmidt04f534e2013-12-09 15:50:16 -08002334
Dmitry Shmidt216983b2015-02-06 10:50:36 -08002335 if (info->p2ps_instance) {
2336 char str[256];
2337 const u8 *buf = wpabuf_head(info->p2ps_instance);
2338 size_t len = wpabuf_len(info->p2ps_instance);
2339
2340 while (len) {
2341 u32 id;
2342 u16 methods;
2343 u8 str_len;
2344
2345 if (len < 4 + 2 + 1)
2346 break;
2347 id = WPA_GET_LE32(buf);
2348 buf += sizeof(u32);
2349 methods = WPA_GET_BE16(buf);
2350 buf += sizeof(u16);
2351 str_len = *buf++;
2352 if (str_len > len - 4 - 2 - 1)
2353 break;
2354 os_memcpy(str, buf, str_len);
2355 str[str_len] = '\0';
2356 buf += str_len;
2357 len -= str_len + sizeof(u32) + sizeof(u16) + sizeof(u8);
2358
2359 wpa_msg_global(wpa_s, MSG_INFO,
2360 P2P_EVENT_DEVICE_FOUND MACSTR
2361 " p2p_dev_addr=" MACSTR
2362 " pri_dev_type=%s name='%s'"
2363 " config_methods=0x%x"
2364 " dev_capab=0x%x"
2365 " group_capab=0x%x"
2366 " adv_id=%x asp_svc=%s%s",
2367 MAC2STR(addr),
2368 MAC2STR(info->p2p_device_addr),
2369 wps_dev_type_bin2str(
2370 info->pri_dev_type,
2371 devtype, sizeof(devtype)),
2372 info->device_name, methods,
2373 info->dev_capab, info->group_capab,
2374 id, str,
2375 info->vendor_elems ?
2376 " vendor_elems=1" : "");
2377 }
2378 goto done;
2379 }
2380
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07002381 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_DEVICE_FOUND MACSTR
2382 " p2p_dev_addr=" MACSTR
2383 " pri_dev_type=%s name='%s' config_methods=0x%x "
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002384 "dev_capab=0x%x group_capab=0x%x%s%s%s new=%d",
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07002385 MAC2STR(addr), MAC2STR(info->p2p_device_addr),
2386 wps_dev_type_bin2str(info->pri_dev_type, devtype,
2387 sizeof(devtype)),
2388 info->device_name, info->config_methods,
2389 info->dev_capab, info->group_capab,
Dmitry Shmidt04f534e2013-12-09 15:50:16 -08002390 wfd_dev_info_hex ? " wfd_dev_info=0x" : "",
Dmitry Shmidt2e67f062014-07-16 09:55:28 -07002391 wfd_dev_info_hex ? wfd_dev_info_hex : "",
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002392 info->vendor_elems ? " vendor_elems=1" : "",
2393 new_device);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002394
Dmitry Shmidt216983b2015-02-06 10:50:36 -08002395done:
Dmitry Shmidt04f534e2013-12-09 15:50:16 -08002396 os_free(wfd_dev_info_hex);
Dmitry Shmidt97672262014-02-03 13:02:54 -08002397#endif /* CONFIG_NO_STDOUT_DEBUG */
Dmitry Shmidt04f534e2013-12-09 15:50:16 -08002398
Roshan Piusfd2fd662017-01-23 13:41:57 -08002399 wpas_notify_p2p_device_found(ctx, addr, info, wfd_dev_info,
2400 wfd_dev_info_len, new_device);
2401 os_free(wfd_dev_info);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002402}
2403
2404
2405static void wpas_dev_lost(void *ctx, const u8 *dev_addr)
2406{
2407 struct wpa_supplicant *wpa_s = ctx;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002408
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07002409 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_DEVICE_LOST
2410 "p2p_dev_addr=" MACSTR, MAC2STR(dev_addr));
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002411
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002412 wpas_notify_p2p_device_lost(wpa_s, dev_addr);
2413}
2414
2415
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07002416static void wpas_find_stopped(void *ctx)
2417{
2418 struct wpa_supplicant *wpa_s = ctx;
Dmitry Shmidt9c175262016-03-03 10:20:07 -08002419
2420 if (wpa_s->p2p_scan_work && wpas_abort_ongoing_scan(wpa_s) < 0)
2421 wpa_printf(MSG_DEBUG, "P2P: Abort ongoing scan failed");
2422
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07002423 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_FIND_STOPPED);
Dmitry Shmidt8bd70b72015-05-26 16:02:19 -07002424 wpas_notify_p2p_find_stopped(wpa_s);
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07002425}
2426
2427
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002428struct wpas_p2p_listen_work {
2429 unsigned int freq;
2430 unsigned int duration;
2431 struct wpabuf *probe_resp_ie;
2432};
2433
2434
2435static void wpas_p2p_listen_work_free(struct wpas_p2p_listen_work *lwork)
2436{
2437 if (lwork == NULL)
2438 return;
2439 wpabuf_free(lwork->probe_resp_ie);
2440 os_free(lwork);
2441}
2442
2443
2444static void wpas_p2p_listen_work_done(struct wpa_supplicant *wpa_s)
2445{
2446 struct wpas_p2p_listen_work *lwork;
2447
2448 if (!wpa_s->p2p_listen_work)
2449 return;
2450
2451 lwork = wpa_s->p2p_listen_work->ctx;
2452 wpas_p2p_listen_work_free(lwork);
2453 radio_work_done(wpa_s->p2p_listen_work);
2454 wpa_s->p2p_listen_work = NULL;
2455}
2456
2457
2458static void wpas_start_listen_cb(struct wpa_radio_work *work, int deinit)
2459{
2460 struct wpa_supplicant *wpa_s = work->wpa_s;
2461 struct wpas_p2p_listen_work *lwork = work->ctx;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002462 unsigned int duration;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002463
2464 if (deinit) {
Dmitry Shmidtbd14a572014-02-18 10:33:49 -08002465 if (work->started) {
2466 wpa_s->p2p_listen_work = NULL;
2467 wpas_stop_listen(wpa_s);
2468 }
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002469 wpas_p2p_listen_work_free(lwork);
2470 return;
2471 }
2472
2473 wpa_s->p2p_listen_work = work;
2474
2475 wpa_drv_set_ap_wps_ie(wpa_s, NULL, lwork->probe_resp_ie, NULL);
2476
2477 if (wpa_drv_probe_req_report(wpa_s, 1) < 0) {
2478 wpa_printf(MSG_DEBUG, "P2P: Failed to request the driver to "
2479 "report received Probe Request frames");
2480 wpas_p2p_listen_work_done(wpa_s);
2481 return;
2482 }
2483
2484 wpa_s->pending_listen_freq = lwork->freq;
2485 wpa_s->pending_listen_duration = lwork->duration;
2486
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002487 duration = lwork->duration;
2488#ifdef CONFIG_TESTING_OPTIONS
2489 if (wpa_s->extra_roc_dur) {
2490 wpa_printf(MSG_DEBUG, "TESTING: Increase ROC duration %u -> %u",
2491 duration, duration + wpa_s->extra_roc_dur);
2492 duration += wpa_s->extra_roc_dur;
2493 }
2494#endif /* CONFIG_TESTING_OPTIONS */
2495
2496 if (wpa_drv_remain_on_channel(wpa_s, lwork->freq, duration) < 0) {
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002497 wpa_printf(MSG_DEBUG, "P2P: Failed to request the driver "
2498 "to remain on channel (%u MHz) for Listen "
2499 "state", lwork->freq);
2500 wpas_p2p_listen_work_done(wpa_s);
2501 wpa_s->pending_listen_freq = 0;
2502 return;
2503 }
2504 wpa_s->off_channel_freq = 0;
2505 wpa_s->roc_waiting_drv_freq = lwork->freq;
2506}
2507
2508
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002509static int wpas_start_listen(void *ctx, unsigned int freq,
2510 unsigned int duration,
2511 const struct wpabuf *probe_resp_ie)
2512{
2513 struct wpa_supplicant *wpa_s = ctx;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002514 struct wpas_p2p_listen_work *lwork;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002515
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002516 if (wpa_s->p2p_listen_work) {
2517 wpa_printf(MSG_DEBUG, "P2P: Reject start_listen since p2p_listen_work already exists");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002518 return -1;
2519 }
2520
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002521 lwork = os_zalloc(sizeof(*lwork));
2522 if (lwork == NULL)
2523 return -1;
2524 lwork->freq = freq;
2525 lwork->duration = duration;
2526 if (probe_resp_ie) {
2527 lwork->probe_resp_ie = wpabuf_dup(probe_resp_ie);
2528 if (lwork->probe_resp_ie == NULL) {
2529 wpas_p2p_listen_work_free(lwork);
2530 return -1;
2531 }
2532 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002533
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002534 if (radio_add_work(wpa_s, freq, "p2p-listen", 0, wpas_start_listen_cb,
2535 lwork) < 0) {
2536 wpas_p2p_listen_work_free(lwork);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002537 return -1;
2538 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002539
2540 return 0;
2541}
2542
2543
2544static void wpas_stop_listen(void *ctx)
2545{
2546 struct wpa_supplicant *wpa_s = ctx;
2547 if (wpa_s->off_channel_freq || wpa_s->roc_waiting_drv_freq) {
2548 wpa_drv_cancel_remain_on_channel(wpa_s);
2549 wpa_s->off_channel_freq = 0;
2550 wpa_s->roc_waiting_drv_freq = 0;
2551 }
2552 wpa_drv_set_ap_wps_ie(wpa_s, NULL, NULL, NULL);
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07002553
2554 /*
2555 * Don't cancel Probe Request RX reporting for a connected P2P Client
2556 * handling Probe Request frames.
2557 */
2558 if (!wpa_s->p2p_cli_probe)
2559 wpa_drv_probe_req_report(wpa_s, 0);
2560
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002561 wpas_p2p_listen_work_done(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002562}
2563
2564
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07002565static int wpas_send_probe_resp(void *ctx, const struct wpabuf *buf,
2566 unsigned int freq)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002567{
2568 struct wpa_supplicant *wpa_s = ctx;
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07002569 return wpa_drv_send_mlme(wpa_s, wpabuf_head(buf), wpabuf_len(buf), 1,
2570 freq);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002571}
2572
2573
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002574static void wpas_prov_disc_local_display(struct wpa_supplicant *wpa_s,
2575 const u8 *peer, const char *params,
2576 unsigned int generated_pin)
2577{
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07002578 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_SHOW_PIN MACSTR
2579 " %08d%s", MAC2STR(peer), generated_pin, params);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002580}
2581
2582
2583static void wpas_prov_disc_local_keypad(struct wpa_supplicant *wpa_s,
2584 const u8 *peer, const char *params)
2585{
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07002586 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_ENTER_PIN MACSTR
2587 "%s", MAC2STR(peer), params);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002588}
2589
2590
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002591static void wpas_prov_disc_req(void *ctx, const u8 *peer, u16 config_methods,
2592 const u8 *dev_addr, const u8 *pri_dev_type,
2593 const char *dev_name, u16 supp_config_methods,
2594 u8 dev_capab, u8 group_capab, const u8 *group_id,
2595 size_t group_id_len)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002596{
2597 struct wpa_supplicant *wpa_s = ctx;
2598 char devtype[WPS_DEV_TYPE_BUFSIZE];
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002599 char params[300];
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002600 u8 empty_dev_type[8];
2601 unsigned int generated_pin = 0;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002602 struct wpa_supplicant *group = NULL;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002603 int res;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002604
2605 if (group_id) {
2606 for (group = wpa_s->global->ifaces; group; group = group->next)
2607 {
2608 struct wpa_ssid *s = group->current_ssid;
2609 if (s != NULL &&
2610 s->mode == WPAS_MODE_P2P_GO &&
2611 group_id_len - ETH_ALEN == s->ssid_len &&
2612 os_memcmp(group_id + ETH_ALEN, s->ssid,
2613 s->ssid_len) == 0)
2614 break;
2615 }
2616 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002617
2618 if (pri_dev_type == NULL) {
2619 os_memset(empty_dev_type, 0, sizeof(empty_dev_type));
2620 pri_dev_type = empty_dev_type;
2621 }
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002622 res = os_snprintf(params, sizeof(params), " p2p_dev_addr=" MACSTR
2623 " pri_dev_type=%s name='%s' config_methods=0x%x "
2624 "dev_capab=0x%x group_capab=0x%x%s%s",
2625 MAC2STR(dev_addr),
2626 wps_dev_type_bin2str(pri_dev_type, devtype,
2627 sizeof(devtype)),
2628 dev_name, supp_config_methods, dev_capab, group_capab,
2629 group ? " group=" : "",
2630 group ? group->ifname : "");
2631 if (os_snprintf_error(sizeof(params), res))
2632 wpa_printf(MSG_DEBUG, "P2P: PD Request event truncated");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002633 params[sizeof(params) - 1] = '\0';
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08002634
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002635 if (config_methods & WPS_CONFIG_DISPLAY) {
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08002636 if (wps_generate_pin(&generated_pin) < 0) {
2637 wpa_printf(MSG_DEBUG, "P2P: Could not generate PIN");
2638 wpas_notify_p2p_provision_discovery(
2639 wpa_s, peer, 0 /* response */,
2640 P2P_PROV_DISC_INFO_UNAVAILABLE, 0, 0);
2641 return;
2642 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002643 wpas_prov_disc_local_display(wpa_s, peer, params,
2644 generated_pin);
2645 } else if (config_methods & WPS_CONFIG_KEYPAD)
2646 wpas_prov_disc_local_keypad(wpa_s, peer, params);
2647 else if (config_methods & WPS_CONFIG_PUSHBUTTON)
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07002648 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_PBC_REQ
2649 MACSTR "%s", MAC2STR(peer), params);
Jouni Malinen75ecf522011-06-27 15:19:46 -07002650
2651 wpas_notify_p2p_provision_discovery(wpa_s, peer, 1 /* request */,
2652 P2P_PROV_DISC_SUCCESS,
2653 config_methods, generated_pin);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002654}
2655
2656
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002657static void wpas_prov_disc_resp(void *ctx, const u8 *peer, u16 config_methods)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002658{
2659 struct wpa_supplicant *wpa_s = ctx;
2660 unsigned int generated_pin = 0;
Dmitry Shmidt04949592012-07-19 12:16:46 -07002661 char params[20];
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002662
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08002663 if (wpa_s->pending_pd_before_join &&
2664 (os_memcmp(peer, wpa_s->pending_join_dev_addr, ETH_ALEN) == 0 ||
2665 os_memcmp(peer, wpa_s->pending_join_iface_addr, ETH_ALEN) == 0)) {
2666 wpa_s->pending_pd_before_join = 0;
2667 wpa_printf(MSG_DEBUG, "P2P: Starting pending "
2668 "join-existing-group operation");
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08002669 wpas_p2p_join_start(wpa_s, 0, NULL, 0);
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08002670 return;
2671 }
2672
Dmitry Shmidt04949592012-07-19 12:16:46 -07002673 if (wpa_s->pending_pd_use == AUTO_PD_JOIN ||
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002674 wpa_s->pending_pd_use == AUTO_PD_GO_NEG) {
2675 int res;
2676
2677 res = os_snprintf(params, sizeof(params), " peer_go=%d",
2678 wpa_s->pending_pd_use == AUTO_PD_JOIN);
2679 if (os_snprintf_error(sizeof(params), res))
2680 params[sizeof(params) - 1] = '\0';
2681 } else
Dmitry Shmidt04949592012-07-19 12:16:46 -07002682 params[0] = '\0';
2683
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002684 if (config_methods & WPS_CONFIG_DISPLAY)
Dmitry Shmidt04949592012-07-19 12:16:46 -07002685 wpas_prov_disc_local_keypad(wpa_s, peer, params);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002686 else if (config_methods & WPS_CONFIG_KEYPAD) {
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08002687 if (wps_generate_pin(&generated_pin) < 0) {
2688 wpa_printf(MSG_DEBUG, "P2P: Could not generate PIN");
2689 wpas_notify_p2p_provision_discovery(
2690 wpa_s, peer, 0 /* response */,
2691 P2P_PROV_DISC_INFO_UNAVAILABLE, 0, 0);
2692 return;
2693 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07002694 wpas_prov_disc_local_display(wpa_s, peer, params,
2695 generated_pin);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002696 } else if (config_methods & WPS_CONFIG_PUSHBUTTON)
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07002697 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_PBC_RESP
2698 MACSTR "%s", MAC2STR(peer), params);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002699
Jouni Malinen75ecf522011-06-27 15:19:46 -07002700 wpas_notify_p2p_provision_discovery(wpa_s, peer, 0 /* response */,
2701 P2P_PROV_DISC_SUCCESS,
2702 config_methods, generated_pin);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002703}
2704
2705
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002706static void wpas_prov_disc_fail(void *ctx, const u8 *peer,
Dmitry Shmidt216983b2015-02-06 10:50:36 -08002707 enum p2p_prov_disc_status status,
2708 u32 adv_id, const u8 *adv_mac,
2709 const char *deferred_session_resp)
Jouni Malinen75ecf522011-06-27 15:19:46 -07002710{
2711 struct wpa_supplicant *wpa_s = ctx;
2712
Dmitry Shmidt04949592012-07-19 12:16:46 -07002713 if (wpa_s->p2p_fallback_to_go_neg) {
2714 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: PD for p2p_connect-auto "
2715 "failed - fall back to GO Negotiation");
Dmitry Shmidt9c175262016-03-03 10:20:07 -08002716 wpa_msg_global(wpa_s->p2pdev, MSG_INFO,
Dmitry Shmidt7f656022015-02-25 14:36:37 -08002717 P2P_EVENT_FALLBACK_TO_GO_NEG
2718 "reason=PD-failed");
Dmitry Shmidt04949592012-07-19 12:16:46 -07002719 wpas_p2p_fallback_to_go_neg(wpa_s, 0);
2720 return;
2721 }
2722
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002723 if (status == P2P_PROV_DISC_TIMEOUT_JOIN) {
Dmitry Shmidt2b380482012-09-13 10:52:13 -07002724 wpa_s->pending_pd_before_join = 0;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002725 wpa_printf(MSG_DEBUG, "P2P: Starting pending "
2726 "join-existing-group operation (no ACK for PD "
2727 "Req attempts)");
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08002728 wpas_p2p_join_start(wpa_s, 0, NULL, 0);
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002729 return;
Dmitry Shmidt2b380482012-09-13 10:52:13 -07002730 }
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002731
Dmitry Shmidt216983b2015-02-06 10:50:36 -08002732 if (adv_id && adv_mac && deferred_session_resp) {
2733 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_FAILURE
2734 " p2p_dev_addr=" MACSTR " status=%d adv_id=%x"
2735 " deferred_session_resp='%s'",
2736 MAC2STR(peer), status, adv_id,
2737 deferred_session_resp);
2738 } else if (adv_id && adv_mac) {
2739 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_FAILURE
2740 " p2p_dev_addr=" MACSTR " status=%d adv_id=%x",
2741 MAC2STR(peer), status, adv_id);
2742 } else {
2743 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_FAILURE
2744 " p2p_dev_addr=" MACSTR " status=%d",
2745 MAC2STR(peer), status);
2746 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07002747
Jouni Malinen75ecf522011-06-27 15:19:46 -07002748 wpas_notify_p2p_provision_discovery(wpa_s, peer, 0 /* response */,
2749 status, 0, 0);
2750}
2751
2752
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002753static int freq_included(struct wpa_supplicant *wpa_s,
2754 const struct p2p_channels *channels,
2755 unsigned int freq)
Dmitry Shmidtf7e0a992013-05-23 11:03:10 -07002756{
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002757 if ((channels == NULL || p2p_channels_includes_freq(channels, freq)) &&
2758 wpas_p2p_go_is_peer_freq(wpa_s, freq))
2759 return 1;
2760 return 0;
2761}
Dmitry Shmidtf7e0a992013-05-23 11:03:10 -07002762
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002763
2764static void wpas_p2p_go_update_common_freqs(struct wpa_supplicant *wpa_s)
2765{
2766 unsigned int num = P2P_MAX_CHANNELS;
2767 int *common_freqs;
2768 int ret;
2769
2770 p2p_go_dump_common_freqs(wpa_s);
2771 common_freqs = os_calloc(num, sizeof(int));
2772 if (!common_freqs)
2773 return;
2774
2775 ret = p2p_group_get_common_freqs(wpa_s->p2p_group, common_freqs, &num);
2776 if (ret < 0) {
2777 wpa_dbg(wpa_s, MSG_DEBUG,
2778 "P2P: Failed to get group common freqs");
2779 os_free(common_freqs);
2780 return;
2781 }
2782
2783 os_free(wpa_s->p2p_group_common_freqs);
2784 wpa_s->p2p_group_common_freqs = common_freqs;
2785 wpa_s->p2p_group_common_freqs_num = num;
2786 p2p_go_dump_common_freqs(wpa_s);
2787}
2788
2789
2790/*
2791 * Check if the given frequency is one of the possible operating frequencies
2792 * set after the completion of the GO Negotiation.
2793 */
2794static int wpas_p2p_go_is_peer_freq(struct wpa_supplicant *wpa_s, int freq)
2795{
2796 unsigned int i;
2797
2798 p2p_go_dump_common_freqs(wpa_s);
2799
2800 /* assume no restrictions */
2801 if (!wpa_s->p2p_group_common_freqs_num)
2802 return 1;
2803
2804 for (i = 0; i < wpa_s->p2p_group_common_freqs_num; i++) {
2805 if (wpa_s->p2p_group_common_freqs[i] == freq)
2806 return 1;
2807 }
2808 return 0;
2809}
2810
2811
2812static int wpas_sta_check_ecsa(struct hostapd_data *hapd,
2813 struct sta_info *sta, void *ctx)
2814{
2815 int *ecsa_support = ctx;
2816
2817 *ecsa_support &= sta->ecsa_supported;
2818
2819 return 0;
2820}
2821
2822
2823/* Check if all the peers support eCSA */
2824static int wpas_p2p_go_clients_support_ecsa(struct wpa_supplicant *wpa_s)
2825{
2826 int ecsa_support = 1;
2827
2828 ap_for_each_sta(wpa_s->ap_iface->bss[0], wpas_sta_check_ecsa,
2829 &ecsa_support);
2830
2831 return ecsa_support;
Dmitry Shmidtf7e0a992013-05-23 11:03:10 -07002832}
2833
2834
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07002835/**
2836 * Pick the best frequency to use from all the currently used frequencies.
2837 */
2838static int wpas_p2p_pick_best_used_freq(struct wpa_supplicant *wpa_s,
2839 struct wpa_used_freq_data *freqs,
2840 unsigned int num)
2841{
2842 unsigned int i, c;
2843
2844 /* find a candidate freq that is supported by P2P */
2845 for (c = 0; c < num; c++)
2846 if (p2p_supported_freq(wpa_s->global->p2p, freqs[c].freq))
2847 break;
2848
2849 if (c == num)
2850 return 0;
2851
2852 /* once we have a candidate, try to find a 'better' one */
2853 for (i = c + 1; i < num; i++) {
2854 if (!p2p_supported_freq(wpa_s->global->p2p, freqs[i].freq))
2855 continue;
2856
2857 /*
2858 * 1. Infrastructure station interfaces have higher preference.
2859 * 2. P2P Clients have higher preference.
2860 * 3. All others.
2861 */
2862 if (freqs[i].flags & WPA_FREQ_USED_BY_INFRA_STATION) {
2863 c = i;
2864 break;
2865 }
2866
2867 if ((freqs[i].flags & WPA_FREQ_USED_BY_P2P_CLIENT))
2868 c = i;
2869 }
2870 return freqs[c].freq;
2871}
2872
2873
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002874static u8 wpas_invitation_process(void *ctx, const u8 *sa, const u8 *bssid,
2875 const u8 *go_dev_addr, const u8 *ssid,
2876 size_t ssid_len, int *go, u8 *group_bssid,
Dmitry Shmidtf7e0a992013-05-23 11:03:10 -07002877 int *force_freq, int persistent_group,
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08002878 const struct p2p_channels *channels,
2879 int dev_pw_id)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002880{
2881 struct wpa_supplicant *wpa_s = ctx;
2882 struct wpa_ssid *s;
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07002883 struct wpa_used_freq_data *freqs;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002884 struct wpa_supplicant *grp;
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07002885 int best_freq;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002886
2887 if (!persistent_group) {
2888 wpa_printf(MSG_DEBUG, "P2P: Invitation from " MACSTR
Dmitry Shmidt344abd32014-01-14 13:17:00 -08002889 " to join an active group (SSID: %s)",
2890 MAC2STR(sa), wpa_ssid_txt(ssid, ssid_len));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002891 if (!is_zero_ether_addr(wpa_s->p2p_auth_invite) &&
2892 (os_memcmp(go_dev_addr, wpa_s->p2p_auth_invite, ETH_ALEN)
2893 == 0 ||
2894 os_memcmp(sa, wpa_s->p2p_auth_invite, ETH_ALEN) == 0)) {
2895 wpa_printf(MSG_DEBUG, "P2P: Accept previously "
2896 "authorized invitation");
2897 goto accept_inv;
2898 }
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08002899
2900#ifdef CONFIG_WPS_NFC
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08002901 if (dev_pw_id >= 0 && wpa_s->p2p_nfc_tag_enabled &&
2902 dev_pw_id == wpa_s->p2p_oob_dev_pw_id) {
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08002903 wpa_printf(MSG_DEBUG, "P2P: Accept invitation based on local enabled NFC Tag");
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08002904 wpa_s->p2p_wps_method = WPS_NFC;
2905 wpa_s->pending_join_wps_method = WPS_NFC;
2906 os_memcpy(wpa_s->pending_join_dev_addr,
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08002907 go_dev_addr, ETH_ALEN);
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08002908 os_memcpy(wpa_s->pending_join_iface_addr,
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08002909 bssid, ETH_ALEN);
2910 goto accept_inv;
2911 }
2912#endif /* CONFIG_WPS_NFC */
2913
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002914 /*
2915 * Do not accept the invitation automatically; notify user and
2916 * request approval.
2917 */
2918 return P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE;
2919 }
2920
2921 grp = wpas_get_p2p_group(wpa_s, ssid, ssid_len, go);
2922 if (grp) {
2923 wpa_printf(MSG_DEBUG, "P2P: Accept invitation to already "
2924 "running persistent group");
2925 if (*go)
2926 os_memcpy(group_bssid, grp->own_addr, ETH_ALEN);
2927 goto accept_inv;
2928 }
2929
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08002930 if (!is_zero_ether_addr(wpa_s->p2p_auth_invite) &&
2931 os_memcmp(sa, wpa_s->p2p_auth_invite, ETH_ALEN) == 0) {
2932 wpa_printf(MSG_DEBUG, "P2P: Accept previously initiated "
2933 "invitation to re-invoke a persistent group");
Dmitry Shmidt76cd2cc2014-05-27 12:56:04 -07002934 os_memset(wpa_s->p2p_auth_invite, 0, ETH_ALEN);
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08002935 } else if (!wpa_s->conf->persistent_reconnect)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002936 return P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE;
2937
2938 for (s = wpa_s->conf->ssid; s; s = s->next) {
2939 if (s->disabled == 2 &&
2940 os_memcmp(s->bssid, go_dev_addr, ETH_ALEN) == 0 &&
2941 s->ssid_len == ssid_len &&
2942 os_memcmp(ssid, s->ssid, ssid_len) == 0)
2943 break;
2944 }
2945
2946 if (!s) {
2947 wpa_printf(MSG_DEBUG, "P2P: Invitation from " MACSTR
2948 " requested reinvocation of an unknown group",
2949 MAC2STR(sa));
2950 return P2P_SC_FAIL_UNKNOWN_GROUP;
2951 }
2952
2953 if (s->mode == WPAS_MODE_P2P_GO && !wpas_p2p_create_iface(wpa_s)) {
2954 *go = 1;
2955 if (wpa_s->wpa_state >= WPA_AUTHENTICATING) {
2956 wpa_printf(MSG_DEBUG, "P2P: The only available "
2957 "interface is already in use - reject "
2958 "invitation");
2959 return P2P_SC_FAIL_UNABLE_TO_ACCOMMODATE;
2960 }
Dmitry Shmidt9c175262016-03-03 10:20:07 -08002961 if (wpa_s->p2p_mgmt)
2962 os_memcpy(group_bssid, wpa_s->parent->own_addr,
2963 ETH_ALEN);
2964 else
2965 os_memcpy(group_bssid, wpa_s->own_addr, ETH_ALEN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002966 } else if (s->mode == WPAS_MODE_P2P_GO) {
2967 *go = 1;
2968 if (wpas_p2p_add_group_interface(wpa_s, WPA_IF_P2P_GO) < 0)
2969 {
2970 wpa_printf(MSG_ERROR, "P2P: Failed to allocate a new "
2971 "interface address for the group");
2972 return P2P_SC_FAIL_UNABLE_TO_ACCOMMODATE;
2973 }
2974 os_memcpy(group_bssid, wpa_s->pending_interface_addr,
2975 ETH_ALEN);
2976 }
2977
2978accept_inv:
Dmitry Shmidt700a1372013-03-15 14:14:44 -07002979 wpas_p2p_set_own_freq_preference(wpa_s, 0);
2980
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07002981 best_freq = 0;
2982 freqs = os_calloc(wpa_s->num_multichan_concurrent,
2983 sizeof(struct wpa_used_freq_data));
2984 if (freqs) {
2985 int num_channels = wpa_s->num_multichan_concurrent;
2986 int num = wpas_p2p_valid_oper_freqs(wpa_s, freqs, num_channels);
2987 best_freq = wpas_p2p_pick_best_used_freq(wpa_s, freqs, num);
2988 os_free(freqs);
2989 }
2990
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07002991 /* Get one of the frequencies currently in use */
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07002992 if (best_freq > 0) {
Dmitry Shmidta0d265f2013-11-19 13:13:41 -08002993 wpa_printf(MSG_DEBUG, "P2P: Trying to prefer a channel already used by one of the interfaces");
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07002994 wpas_p2p_set_own_freq_preference(wpa_s, best_freq);
Dmitry Shmidta0d265f2013-11-19 13:13:41 -08002995
2996 if (wpa_s->num_multichan_concurrent < 2 ||
2997 wpas_p2p_num_unused_channels(wpa_s) < 1) {
2998 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 -07002999 *force_freq = best_freq;
Dmitry Shmidta0d265f2013-11-19 13:13:41 -08003000 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003001 }
3002
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07003003 if (*force_freq > 0 && wpa_s->num_multichan_concurrent > 1 &&
3004 wpas_p2p_num_unused_channels(wpa_s) > 0) {
Dmitry Shmidtf7e0a992013-05-23 11:03:10 -07003005 if (*go == 0) {
3006 /* We are the client */
3007 wpa_printf(MSG_DEBUG, "P2P: Peer was found to be "
3008 "running a GO but we are capable of MCC, "
3009 "figure out the best channel to use");
3010 *force_freq = 0;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003011 } else if (!freq_included(wpa_s, channels, *force_freq)) {
Dmitry Shmidtf7e0a992013-05-23 11:03:10 -07003012 /* We are the GO, and *force_freq is not in the
3013 * intersection */
3014 wpa_printf(MSG_DEBUG, "P2P: Forced GO freq %d MHz not "
3015 "in intersection but we are capable of MCC, "
3016 "figure out the best channel to use",
3017 *force_freq);
3018 *force_freq = 0;
3019 }
3020 }
3021
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003022 return P2P_SC_SUCCESS;
3023}
3024
3025
3026static void wpas_invitation_received(void *ctx, const u8 *sa, const u8 *bssid,
3027 const u8 *ssid, size_t ssid_len,
3028 const u8 *go_dev_addr, u8 status,
3029 int op_freq)
3030{
3031 struct wpa_supplicant *wpa_s = ctx;
3032 struct wpa_ssid *s;
3033
3034 for (s = wpa_s->conf->ssid; s; s = s->next) {
3035 if (s->disabled == 2 &&
3036 s->ssid_len == ssid_len &&
3037 os_memcmp(ssid, s->ssid, ssid_len) == 0)
3038 break;
3039 }
3040
3041 if (status == P2P_SC_SUCCESS) {
3042 wpa_printf(MSG_DEBUG, "P2P: Invitation from peer " MACSTR
Dmitry Shmidt344abd32014-01-14 13:17:00 -08003043 " was accepted; op_freq=%d MHz, SSID=%s",
3044 MAC2STR(sa), op_freq, wpa_ssid_txt(ssid, ssid_len));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003045 if (s) {
Dmitry Shmidt91c40cd2012-09-25 14:23:53 -07003046 int go = s->mode == WPAS_MODE_P2P_GO;
Dmitry Shmidt849734c2016-05-27 09:59:01 -07003047 if (go) {
3048 wpa_msg_global(wpa_s, MSG_INFO,
3049 P2P_EVENT_INVITATION_ACCEPTED
3050 "sa=" MACSTR
3051 " persistent=%d freq=%d",
3052 MAC2STR(sa), s->id, op_freq);
3053 } else {
3054 wpa_msg_global(wpa_s, MSG_INFO,
3055 P2P_EVENT_INVITATION_ACCEPTED
3056 "sa=" MACSTR
3057 " persistent=%d",
3058 MAC2STR(sa), s->id);
3059 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003060 wpas_p2p_group_add_persistent(
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08003061 wpa_s, s, go, 0, op_freq, 0, 0, 0, 0, NULL,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003062 go ? P2P_MAX_INITIAL_CONN_WAIT_GO_REINVOKE : 0,
3063 1);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003064 } else if (bssid) {
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08003065 wpa_s->user_initiated_pd = 0;
Dmitry Shmidt849734c2016-05-27 09:59:01 -07003066 wpa_msg_global(wpa_s, MSG_INFO,
3067 P2P_EVENT_INVITATION_ACCEPTED
3068 "sa=" MACSTR " go_dev_addr=" MACSTR
3069 " bssid=" MACSTR " unknown-network",
3070 MAC2STR(sa), MAC2STR(go_dev_addr),
3071 MAC2STR(bssid));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003072 wpas_p2p_join(wpa_s, bssid, go_dev_addr,
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08003073 wpa_s->p2p_wps_method, 0, op_freq,
Dmitry Shmidt344abd32014-01-14 13:17:00 -08003074 ssid, ssid_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003075 }
3076 return;
3077 }
3078
3079 if (status != P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE) {
3080 wpa_printf(MSG_DEBUG, "P2P: Invitation from peer " MACSTR
3081 " was rejected (status %u)", MAC2STR(sa), status);
3082 return;
3083 }
3084
3085 if (!s) {
3086 if (bssid) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07003087 wpa_msg_global(wpa_s, MSG_INFO,
3088 P2P_EVENT_INVITATION_RECEIVED
3089 "sa=" MACSTR " go_dev_addr=" MACSTR
3090 " bssid=" MACSTR " unknown-network",
3091 MAC2STR(sa), MAC2STR(go_dev_addr),
3092 MAC2STR(bssid));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003093 } else {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07003094 wpa_msg_global(wpa_s, MSG_INFO,
3095 P2P_EVENT_INVITATION_RECEIVED
3096 "sa=" MACSTR " go_dev_addr=" MACSTR
3097 " unknown-network",
3098 MAC2STR(sa), MAC2STR(go_dev_addr));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003099 }
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003100 wpas_notify_p2p_invitation_received(wpa_s, sa, go_dev_addr,
3101 bssid, 0, op_freq);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003102 return;
3103 }
3104
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08003105 if (s->mode == WPAS_MODE_P2P_GO && op_freq) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07003106 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_INVITATION_RECEIVED
3107 "sa=" MACSTR " persistent=%d freq=%d",
3108 MAC2STR(sa), s->id, op_freq);
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08003109 } else {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07003110 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_INVITATION_RECEIVED
3111 "sa=" MACSTR " persistent=%d",
3112 MAC2STR(sa), s->id);
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08003113 }
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003114 wpas_notify_p2p_invitation_received(wpa_s, sa, go_dev_addr, bssid,
3115 s->id, op_freq);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003116}
3117
3118
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003119static void wpas_remove_persistent_peer(struct wpa_supplicant *wpa_s,
3120 struct wpa_ssid *ssid,
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07003121 const u8 *peer, int inv)
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003122{
3123 size_t i;
Dmitry Shmidt8bd70b72015-05-26 16:02:19 -07003124 struct wpa_supplicant *p2p_wpa_s = wpa_s->global->p2p_init_wpa_s;
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003125
3126 if (ssid == NULL)
3127 return;
3128
3129 for (i = 0; ssid->p2p_client_list && i < ssid->num_p2p_clients; i++) {
Dmitry Shmidtff787d52015-01-12 13:01:47 -08003130 if (os_memcmp(ssid->p2p_client_list + i * 2 * ETH_ALEN, peer,
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003131 ETH_ALEN) == 0)
3132 break;
3133 }
Dmitry Shmidt6aa8ae42014-07-07 09:31:33 -07003134 if (i >= ssid->num_p2p_clients || !ssid->p2p_client_list) {
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003135 if (ssid->mode != WPAS_MODE_P2P_GO &&
3136 os_memcmp(ssid->bssid, peer, ETH_ALEN) == 0) {
3137 wpa_printf(MSG_DEBUG, "P2P: Remove persistent group %d "
3138 "due to invitation result", ssid->id);
3139 wpas_notify_network_removed(wpa_s, ssid);
3140 wpa_config_remove_network(wpa_s->conf, ssid->id);
3141 return;
3142 }
3143 return; /* Peer not found in client list */
3144 }
3145
3146 wpa_printf(MSG_DEBUG, "P2P: Remove peer " MACSTR " from persistent "
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07003147 "group %d client list%s",
3148 MAC2STR(peer), ssid->id,
3149 inv ? " due to invitation result" : "");
Dmitry Shmidtff787d52015-01-12 13:01:47 -08003150 os_memmove(ssid->p2p_client_list + i * 2 * ETH_ALEN,
3151 ssid->p2p_client_list + (i + 1) * 2 * ETH_ALEN,
3152 (ssid->num_p2p_clients - i - 1) * 2 * ETH_ALEN);
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003153 ssid->num_p2p_clients--;
Dmitry Shmidt8bd70b72015-05-26 16:02:19 -07003154 if (p2p_wpa_s->conf->update_config &&
3155 wpa_config_write(p2p_wpa_s->confname, p2p_wpa_s->conf))
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003156 wpa_printf(MSG_DEBUG, "P2P: Failed to update configuration");
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003157}
3158
3159
3160static void wpas_remove_persistent_client(struct wpa_supplicant *wpa_s,
3161 const u8 *peer)
3162{
3163 struct wpa_ssid *ssid;
3164
3165 wpa_s = wpa_s->global->p2p_invite_group;
3166 if (wpa_s == NULL)
3167 return; /* No known invitation group */
3168 ssid = wpa_s->current_ssid;
3169 if (ssid == NULL || ssid->mode != WPAS_MODE_P2P_GO ||
3170 !ssid->p2p_persistent_group)
3171 return; /* Not operating as a GO in persistent group */
Dmitry Shmidt9c175262016-03-03 10:20:07 -08003172 ssid = wpas_p2p_get_persistent(wpa_s->p2pdev, peer,
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003173 ssid->ssid, ssid->ssid_len);
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07003174 wpas_remove_persistent_peer(wpa_s, ssid, peer, 1);
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003175}
3176
3177
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08003178static void wpas_invitation_result(void *ctx, int status, const u8 *bssid,
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003179 const struct p2p_channels *channels,
Dmitry Shmidt15907092014-03-25 10:42:57 -07003180 const u8 *peer, int neg_freq,
3181 int peer_oper_freq)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003182{
3183 struct wpa_supplicant *wpa_s = ctx;
3184 struct wpa_ssid *ssid;
Dmitry Shmidt15907092014-03-25 10:42:57 -07003185 int freq;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003186
3187 if (bssid) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07003188 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_INVITATION_RESULT
3189 "status=%d " MACSTR,
3190 status, MAC2STR(bssid));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003191 } else {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07003192 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_INVITATION_RESULT
3193 "status=%d ", status);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003194 }
3195 wpas_notify_p2p_invitation_result(wpa_s, status, bssid);
3196
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003197 wpa_printf(MSG_DEBUG, "P2P: Invitation result - status=%d peer=" MACSTR,
3198 status, MAC2STR(peer));
3199 if (wpa_s->pending_invite_ssid_id == -1) {
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08003200 struct wpa_supplicant *group_if =
3201 wpa_s->global->p2p_invite_group;
3202
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003203 if (status == P2P_SC_FAIL_UNKNOWN_GROUP)
3204 wpas_remove_persistent_client(wpa_s, peer);
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08003205
3206 /*
3207 * Invitation to an active group. If this is successful and we
3208 * are the GO, set the client wait to postpone some concurrent
3209 * operations and to allow provisioning and connection to happen
3210 * more quickly.
3211 */
3212 if (status == P2P_SC_SUCCESS &&
3213 group_if && group_if->current_ssid &&
3214 group_if->current_ssid->mode == WPAS_MODE_P2P_GO) {
3215 os_get_reltime(&wpa_s->global->p2p_go_wait_client);
3216#ifdef CONFIG_TESTING_OPTIONS
3217 if (group_if->p2p_go_csa_on_inv) {
3218 wpa_printf(MSG_DEBUG,
3219 "Testing: force P2P GO CSA after invitation");
3220 eloop_cancel_timeout(
3221 wpas_p2p_reconsider_moving_go,
3222 wpa_s, NULL);
3223 eloop_register_timeout(
3224 0, 50000,
3225 wpas_p2p_reconsider_moving_go,
3226 wpa_s, NULL);
3227 }
3228#endif /* CONFIG_TESTING_OPTIONS */
3229 }
3230 return;
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003231 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003232
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08003233 if (status == P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE) {
3234 wpa_printf(MSG_DEBUG, "P2P: Waiting for peer to start another "
3235 "invitation exchange to indicate readiness for "
3236 "re-invocation");
3237 }
3238
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003239 if (status != P2P_SC_SUCCESS) {
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003240 if (status == P2P_SC_FAIL_UNKNOWN_GROUP) {
3241 ssid = wpa_config_get_network(
3242 wpa_s->conf, wpa_s->pending_invite_ssid_id);
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07003243 wpas_remove_persistent_peer(wpa_s, ssid, peer, 1);
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003244 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003245 wpas_p2p_remove_pending_group_interface(wpa_s);
3246 return;
3247 }
3248
3249 ssid = wpa_config_get_network(wpa_s->conf,
3250 wpa_s->pending_invite_ssid_id);
3251 if (ssid == NULL) {
3252 wpa_printf(MSG_ERROR, "P2P: Could not find persistent group "
3253 "data matching with invitation");
3254 return;
3255 }
3256
Jouni Malinen2c5b17d2012-10-13 21:52:46 -07003257 /*
3258 * The peer could have missed our ctrl::ack frame for Invitation
3259 * Response and continue retransmitting the frame. To reduce the
3260 * likelihood of the peer not getting successful TX status for the
3261 * Invitation Response frame, wait a short time here before starting
3262 * the persistent group so that we will remain on the current channel to
3263 * acknowledge any possible retransmission from the peer.
3264 */
3265 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: 50 ms wait on current channel before "
3266 "starting persistent group");
3267 os_sleep(0, 50000);
3268
Dmitry Shmidt15907092014-03-25 10:42:57 -07003269 if (neg_freq > 0 && ssid->mode == WPAS_MODE_P2P_GO &&
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003270 freq_included(wpa_s, channels, neg_freq))
Dmitry Shmidt15907092014-03-25 10:42:57 -07003271 freq = neg_freq;
3272 else if (peer_oper_freq > 0 && ssid->mode != WPAS_MODE_P2P_GO &&
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003273 freq_included(wpa_s, channels, peer_oper_freq))
Dmitry Shmidt15907092014-03-25 10:42:57 -07003274 freq = peer_oper_freq;
3275 else
3276 freq = 0;
3277
3278 wpa_printf(MSG_DEBUG, "P2P: Persistent group invitation success - op_freq=%d MHz SSID=%s",
3279 freq, wpa_ssid_txt(ssid->ssid, ssid->ssid_len));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003280 wpas_p2p_group_add_persistent(wpa_s, ssid,
Jouni Malinen31be0a42012-08-31 21:20:51 +03003281 ssid->mode == WPAS_MODE_P2P_GO,
Dmitry Shmidt96be6222014-02-13 10:16:51 -08003282 wpa_s->p2p_persistent_go_freq,
Dmitry Shmidt15907092014-03-25 10:42:57 -07003283 freq,
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08003284 wpa_s->p2p_go_vht_center_freq2,
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003285 wpa_s->p2p_go_ht40, wpa_s->p2p_go_vht,
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08003286 wpa_s->p2p_go_max_oper_chwidth,
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003287 channels,
Dmitry Shmidt56052862013-10-04 10:23:25 -07003288 ssid->mode == WPAS_MODE_P2P_GO ?
3289 P2P_MAX_INITIAL_CONN_WAIT_GO_REINVOKE :
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003290 0, 1);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003291}
3292
3293
Dmitry Shmidt04949592012-07-19 12:16:46 -07003294static int wpas_p2p_disallowed_freq(struct wpa_global *global,
3295 unsigned int freq)
3296{
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08003297 if (freq_range_list_includes(&global->p2p_go_avoid_freq, freq))
3298 return 1;
Dmitry Shmidt4ce9c872013-10-24 11:08:13 -07003299 return freq_range_list_includes(&global->p2p_disallow_freq, freq);
Dmitry Shmidt04949592012-07-19 12:16:46 -07003300}
3301
3302
3303static void wpas_p2p_add_chan(struct p2p_reg_class *reg, u8 chan)
3304{
3305 reg->channel[reg->channels] = chan;
3306 reg->channels++;
3307}
3308
3309
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003310static int wpas_p2p_default_channels(struct wpa_supplicant *wpa_s,
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003311 struct p2p_channels *chan,
3312 struct p2p_channels *cli_chan)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003313{
3314 int i, cla = 0;
3315
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003316 wpa_s->global->p2p_24ghz_social_channels = 1;
3317
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003318 os_memset(cli_chan, 0, sizeof(*cli_chan));
3319
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003320 wpa_printf(MSG_DEBUG, "P2P: Enable operating classes for 2.4 GHz "
3321 "band");
3322
3323 /* Operating class 81 - 2.4 GHz band channels 1..13 */
3324 chan->reg_class[cla].reg_class = 81;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003325 chan->reg_class[cla].channels = 0;
3326 for (i = 0; i < 11; i++) {
3327 if (!wpas_p2p_disallowed_freq(wpa_s->global, 2412 + i * 5))
3328 wpas_p2p_add_chan(&chan->reg_class[cla], i + 1);
3329 }
3330 if (chan->reg_class[cla].channels)
3331 cla++;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003332
3333 wpa_printf(MSG_DEBUG, "P2P: Enable operating classes for lower 5 GHz "
3334 "band");
3335
3336 /* Operating class 115 - 5 GHz, channels 36-48 */
3337 chan->reg_class[cla].reg_class = 115;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003338 chan->reg_class[cla].channels = 0;
3339 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 36 * 5))
3340 wpas_p2p_add_chan(&chan->reg_class[cla], 36);
3341 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 40 * 5))
3342 wpas_p2p_add_chan(&chan->reg_class[cla], 40);
3343 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 44 * 5))
3344 wpas_p2p_add_chan(&chan->reg_class[cla], 44);
3345 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 48 * 5))
3346 wpas_p2p_add_chan(&chan->reg_class[cla], 48);
3347 if (chan->reg_class[cla].channels)
3348 cla++;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003349
3350 wpa_printf(MSG_DEBUG, "P2P: Enable operating classes for higher 5 GHz "
3351 "band");
3352
3353 /* Operating class 124 - 5 GHz, channels 149,153,157,161 */
3354 chan->reg_class[cla].reg_class = 124;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003355 chan->reg_class[cla].channels = 0;
3356 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 149 * 5))
3357 wpas_p2p_add_chan(&chan->reg_class[cla], 149);
3358 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 153 * 5))
3359 wpas_p2p_add_chan(&chan->reg_class[cla], 153);
3360 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 156 * 5))
3361 wpas_p2p_add_chan(&chan->reg_class[cla], 157);
3362 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 161 * 5))
3363 wpas_p2p_add_chan(&chan->reg_class[cla], 161);
3364 if (chan->reg_class[cla].channels)
3365 cla++;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003366
3367 chan->reg_classes = cla;
3368 return 0;
3369}
3370
3371
Dmitry Shmidt04949592012-07-19 12:16:46 -07003372static int has_channel(struct wpa_global *global,
3373 struct hostapd_hw_modes *mode, u8 chan, int *flags)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003374{
3375 int i;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003376 unsigned int freq;
3377
3378 freq = (mode->mode == HOSTAPD_MODE_IEEE80211A ? 5000 : 2407) +
3379 chan * 5;
3380 if (wpas_p2p_disallowed_freq(global, freq))
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003381 return NOT_ALLOWED;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003382
3383 for (i = 0; i < mode->num_channels; i++) {
3384 if (mode->channels[i].chan == chan) {
3385 if (flags)
3386 *flags = mode->channels[i].flag;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003387 if (mode->channels[i].flag &
3388 (HOSTAPD_CHAN_DISABLED |
3389 HOSTAPD_CHAN_RADAR))
3390 return NOT_ALLOWED;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003391 if (mode->channels[i].flag & HOSTAPD_CHAN_NO_IR)
3392 return NO_IR;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003393 return ALLOWED;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003394 }
3395 }
3396
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003397 return NOT_ALLOWED;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003398}
3399
3400
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003401static int wpas_p2p_get_center_80mhz(struct wpa_supplicant *wpa_s,
3402 struct hostapd_hw_modes *mode,
3403 u8 channel)
3404{
3405 u8 center_channels[] = { 42, 58, 106, 122, 138, 155 };
Dmitry Shmidtaca489e2016-09-28 15:44:14 -07003406 size_t i;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003407
3408 if (mode->mode != HOSTAPD_MODE_IEEE80211A)
3409 return 0;
3410
3411 for (i = 0; i < ARRAY_SIZE(center_channels); i++)
3412 /*
3413 * In 80 MHz, the bandwidth "spans" 12 channels (e.g., 36-48),
3414 * so the center channel is 6 channels away from the start/end.
3415 */
3416 if (channel >= center_channels[i] - 6 &&
3417 channel <= center_channels[i] + 6)
3418 return center_channels[i];
3419
3420 return 0;
3421}
3422
3423
3424static enum chan_allowed wpas_p2p_verify_80mhz(struct wpa_supplicant *wpa_s,
3425 struct hostapd_hw_modes *mode,
3426 u8 channel, u8 bw)
3427{
3428 u8 center_chan;
3429 int i, flags;
3430 enum chan_allowed res, ret = ALLOWED;
3431
3432 center_chan = wpas_p2p_get_center_80mhz(wpa_s, mode, channel);
3433 if (!center_chan)
3434 return NOT_ALLOWED;
3435 if (center_chan >= 58 && center_chan <= 138)
3436 return NOT_ALLOWED; /* Do not allow DFS channels for P2P */
3437
3438 /* check all the channels are available */
3439 for (i = 0; i < 4; i++) {
3440 int adj_chan = center_chan - 6 + i * 4;
3441
3442 res = has_channel(wpa_s->global, mode, adj_chan, &flags);
3443 if (res == NOT_ALLOWED)
3444 return NOT_ALLOWED;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003445 if (res == NO_IR)
3446 ret = NO_IR;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003447
3448 if (i == 0 && !(flags & HOSTAPD_CHAN_VHT_10_70))
3449 return NOT_ALLOWED;
3450 if (i == 1 && !(flags & HOSTAPD_CHAN_VHT_30_50))
3451 return NOT_ALLOWED;
3452 if (i == 2 && !(flags & HOSTAPD_CHAN_VHT_50_30))
3453 return NOT_ALLOWED;
3454 if (i == 3 && !(flags & HOSTAPD_CHAN_VHT_70_10))
3455 return NOT_ALLOWED;
3456 }
3457
3458 return ret;
3459}
3460
3461
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08003462static int wpas_p2p_get_center_160mhz(struct wpa_supplicant *wpa_s,
3463 struct hostapd_hw_modes *mode,
3464 u8 channel)
3465{
3466 u8 center_channels[] = { 50, 114 };
3467 unsigned int i;
3468
3469 if (mode->mode != HOSTAPD_MODE_IEEE80211A)
3470 return 0;
3471
3472 for (i = 0; i < ARRAY_SIZE(center_channels); i++)
3473 /*
3474 * In 160 MHz, the bandwidth "spans" 28 channels (e.g., 36-64),
3475 * so the center channel is 14 channels away from the start/end.
3476 */
3477 if (channel >= center_channels[i] - 14 &&
3478 channel <= center_channels[i] + 14)
3479 return center_channels[i];
3480
3481 return 0;
3482}
3483
3484
3485static enum chan_allowed wpas_p2p_verify_160mhz(struct wpa_supplicant *wpa_s,
3486 struct hostapd_hw_modes *mode,
3487 u8 channel, u8 bw)
3488{
3489 u8 center_chan;
3490 int i, flags;
3491 enum chan_allowed res, ret = ALLOWED;
3492
3493 center_chan = wpas_p2p_get_center_160mhz(wpa_s, mode, channel);
3494 if (!center_chan)
3495 return NOT_ALLOWED;
3496 /* VHT 160 MHz uses DFS channels in most countries. */
3497
3498 /* Check all the channels are available */
3499 for (i = 0; i < 8; i++) {
3500 int adj_chan = center_chan - 14 + i * 4;
3501
3502 res = has_channel(wpa_s->global, mode, adj_chan, &flags);
3503 if (res == NOT_ALLOWED)
3504 return NOT_ALLOWED;
3505
3506 if (res == NO_IR)
3507 ret = NO_IR;
3508
3509 if (i == 0 && !(flags & HOSTAPD_CHAN_VHT_10_150))
3510 return NOT_ALLOWED;
3511 if (i == 1 && !(flags & HOSTAPD_CHAN_VHT_30_130))
3512 return NOT_ALLOWED;
3513 if (i == 2 && !(flags & HOSTAPD_CHAN_VHT_50_110))
3514 return NOT_ALLOWED;
3515 if (i == 3 && !(flags & HOSTAPD_CHAN_VHT_70_90))
3516 return NOT_ALLOWED;
3517 if (i == 4 && !(flags & HOSTAPD_CHAN_VHT_90_70))
3518 return NOT_ALLOWED;
3519 if (i == 5 && !(flags & HOSTAPD_CHAN_VHT_110_50))
3520 return NOT_ALLOWED;
3521 if (i == 6 && !(flags & HOSTAPD_CHAN_VHT_130_30))
3522 return NOT_ALLOWED;
3523 if (i == 7 && !(flags & HOSTAPD_CHAN_VHT_150_10))
3524 return NOT_ALLOWED;
3525 }
3526
3527 return ret;
3528}
3529
3530
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003531static enum chan_allowed wpas_p2p_verify_channel(struct wpa_supplicant *wpa_s,
3532 struct hostapd_hw_modes *mode,
3533 u8 channel, u8 bw)
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003534{
Dmitry Shmidt96be6222014-02-13 10:16:51 -08003535 int flag = 0;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003536 enum chan_allowed res, res2;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003537
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003538 res2 = res = has_channel(wpa_s->global, mode, channel, &flag);
3539 if (bw == BW40MINUS) {
3540 if (!(flag & HOSTAPD_CHAN_HT40MINUS))
3541 return NOT_ALLOWED;
3542 res2 = has_channel(wpa_s->global, mode, channel - 4, NULL);
3543 } else if (bw == BW40PLUS) {
3544 if (!(flag & HOSTAPD_CHAN_HT40PLUS))
3545 return NOT_ALLOWED;
3546 res2 = has_channel(wpa_s->global, mode, channel + 4, NULL);
3547 } else if (bw == BW80) {
3548 res2 = wpas_p2p_verify_80mhz(wpa_s, mode, channel, bw);
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08003549 } else if (bw == BW160) {
3550 res2 = wpas_p2p_verify_160mhz(wpa_s, mode, channel, bw);
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003551 }
3552
3553 if (res == NOT_ALLOWED || res2 == NOT_ALLOWED)
3554 return NOT_ALLOWED;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003555 if (res == NO_IR || res2 == NO_IR)
3556 return NO_IR;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003557 return res;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003558}
3559
3560
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003561static int wpas_p2p_setup_channels(struct wpa_supplicant *wpa_s,
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003562 struct p2p_channels *chan,
3563 struct p2p_channels *cli_chan)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003564{
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003565 struct hostapd_hw_modes *mode;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003566 int cla, op, cli_cla;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003567
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003568 if (wpa_s->hw.modes == NULL) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003569 wpa_printf(MSG_DEBUG, "P2P: Driver did not support fetching "
3570 "of all supported channels; assume dualband "
3571 "support");
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003572 return wpas_p2p_default_channels(wpa_s, chan, cli_chan);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003573 }
3574
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003575 cla = cli_cla = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003576
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08003577 for (op = 0; global_op_class[op].op_class; op++) {
3578 const struct oper_class_map *o = &global_op_class[op];
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003579 u8 ch;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003580 struct p2p_reg_class *reg = NULL, *cli_reg = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003581
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08003582 if (o->p2p == NO_P2P_SUPP)
3583 continue;
3584
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003585 mode = get_mode(wpa_s->hw.modes, wpa_s->hw.num_modes, o->mode);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003586 if (mode == NULL)
3587 continue;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003588 if (mode->mode == HOSTAPD_MODE_IEEE80211G)
3589 wpa_s->global->p2p_24ghz_social_channels = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003590 for (ch = o->min_chan; ch <= o->max_chan; ch += o->inc) {
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003591 enum chan_allowed res;
3592 res = wpas_p2p_verify_channel(wpa_s, mode, ch, o->bw);
3593 if (res == ALLOWED) {
3594 if (reg == NULL) {
3595 wpa_printf(MSG_DEBUG, "P2P: Add operating class %u",
3596 o->op_class);
3597 reg = &chan->reg_class[cla];
3598 cla++;
3599 reg->reg_class = o->op_class;
3600 }
3601 reg->channel[reg->channels] = ch;
3602 reg->channels++;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003603 } else if (res == NO_IR &&
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003604 wpa_s->conf->p2p_add_cli_chan) {
3605 if (cli_reg == NULL) {
3606 wpa_printf(MSG_DEBUG, "P2P: Add operating class %u (client only)",
3607 o->op_class);
3608 cli_reg = &cli_chan->reg_class[cli_cla];
3609 cli_cla++;
3610 cli_reg->reg_class = o->op_class;
3611 }
3612 cli_reg->channel[cli_reg->channels] = ch;
3613 cli_reg->channels++;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003614 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003615 }
3616 if (reg) {
3617 wpa_hexdump(MSG_DEBUG, "P2P: Channels",
3618 reg->channel, reg->channels);
3619 }
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003620 if (cli_reg) {
3621 wpa_hexdump(MSG_DEBUG, "P2P: Channels (client only)",
3622 cli_reg->channel, cli_reg->channels);
3623 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003624 }
3625
3626 chan->reg_classes = cla;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003627 cli_chan->reg_classes = cli_cla;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003628
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003629 return 0;
3630}
3631
3632
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003633int wpas_p2p_get_ht40_mode(struct wpa_supplicant *wpa_s,
3634 struct hostapd_hw_modes *mode, u8 channel)
3635{
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003636 int op;
3637 enum chan_allowed ret;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003638
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08003639 for (op = 0; global_op_class[op].op_class; op++) {
3640 const struct oper_class_map *o = &global_op_class[op];
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003641 u8 ch;
3642
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08003643 if (o->p2p == NO_P2P_SUPP)
3644 continue;
3645
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003646 for (ch = o->min_chan; ch <= o->max_chan; ch += o->inc) {
3647 if (o->mode != HOSTAPD_MODE_IEEE80211A ||
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07003648 (o->bw != BW40PLUS && o->bw != BW40MINUS) ||
3649 ch != channel)
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003650 continue;
3651 ret = wpas_p2p_verify_channel(wpa_s, mode, ch, o->bw);
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003652 if (ret == ALLOWED)
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003653 return (o->bw == BW40MINUS) ? -1 : 1;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003654 }
3655 }
3656 return 0;
3657}
3658
3659
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003660int wpas_p2p_get_vht80_center(struct wpa_supplicant *wpa_s,
3661 struct hostapd_hw_modes *mode, u8 channel)
3662{
3663 if (!wpas_p2p_verify_channel(wpa_s, mode, channel, BW80))
3664 return 0;
3665
3666 return wpas_p2p_get_center_80mhz(wpa_s, mode, channel);
3667}
3668
3669
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08003670int wpas_p2p_get_vht160_center(struct wpa_supplicant *wpa_s,
3671 struct hostapd_hw_modes *mode, u8 channel)
3672{
3673 if (!wpas_p2p_verify_channel(wpa_s, mode, channel, BW160))
3674 return 0;
3675 return wpas_p2p_get_center_160mhz(wpa_s, mode, channel);
3676}
3677
3678
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003679static int wpas_get_noa(void *ctx, const u8 *interface_addr, u8 *buf,
3680 size_t buf_len)
3681{
3682 struct wpa_supplicant *wpa_s = ctx;
3683
3684 for (wpa_s = wpa_s->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
3685 if (os_memcmp(wpa_s->own_addr, interface_addr, ETH_ALEN) == 0)
3686 break;
3687 }
3688 if (wpa_s == NULL)
3689 return -1;
3690
3691 return wpa_drv_get_noa(wpa_s, buf, buf_len);
3692}
3693
3694
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07003695struct wpa_supplicant * wpas_get_p2p_go_iface(struct wpa_supplicant *wpa_s,
3696 const u8 *ssid, size_t ssid_len)
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08003697{
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07003698 for (wpa_s = wpa_s->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
3699 struct wpa_ssid *s = wpa_s->current_ssid;
3700 if (s == NULL)
3701 continue;
3702 if (s->mode != WPAS_MODE_P2P_GO &&
3703 s->mode != WPAS_MODE_AP &&
3704 s->mode != WPAS_MODE_P2P_GROUP_FORMATION)
3705 continue;
3706 if (s->ssid_len != ssid_len ||
Dmitry Shmidt03658832014-08-13 11:03:49 -07003707 os_memcmp(ssid, s->ssid, ssid_len) != 0)
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07003708 continue;
3709 return wpa_s;
3710 }
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08003711
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07003712 return NULL;
3713
3714}
3715
3716
3717struct wpa_supplicant * wpas_get_p2p_client_iface(struct wpa_supplicant *wpa_s,
3718 const u8 *peer_dev_addr)
3719{
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08003720 for (wpa_s = wpa_s->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
3721 struct wpa_ssid *ssid = wpa_s->current_ssid;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003722 if (ssid && (ssid->mode != WPAS_MODE_INFRA || !ssid->p2p_group))
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08003723 continue;
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07003724 if (os_memcmp(wpa_s->go_dev_addr, peer_dev_addr, ETH_ALEN) == 0)
3725 return wpa_s;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08003726 }
3727
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07003728 return NULL;
3729}
3730
3731
3732static int wpas_go_connected(void *ctx, const u8 *dev_addr)
3733{
3734 struct wpa_supplicant *wpa_s = ctx;
3735
3736 return wpas_get_p2p_client_iface(wpa_s, dev_addr) != NULL;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08003737}
3738
3739
Dmitry Shmidt18463232014-01-24 12:29:41 -08003740static int wpas_is_concurrent_session_active(void *ctx)
3741{
3742 struct wpa_supplicant *wpa_s = ctx;
3743 struct wpa_supplicant *ifs;
3744
3745 for (ifs = wpa_s->global->ifaces; ifs; ifs = ifs->next) {
3746 if (ifs == wpa_s)
3747 continue;
3748 if (ifs->wpa_state > WPA_ASSOCIATED)
3749 return 1;
3750 }
3751 return 0;
3752}
3753
3754
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07003755static void wpas_p2p_debug_print(void *ctx, int level, const char *msg)
3756{
3757 struct wpa_supplicant *wpa_s = ctx;
3758 wpa_msg_global(wpa_s, level, "P2P: %s", msg);
3759}
3760
3761
Dmitry Shmidt9ead16e2014-10-07 13:15:23 -07003762int wpas_p2p_add_p2pdev_interface(struct wpa_supplicant *wpa_s,
3763 const char *conf_p2p_dev)
Dmitry Shmidt34af3062013-07-11 10:46:32 -07003764{
3765 struct wpa_interface iface;
3766 struct wpa_supplicant *p2pdev_wpa_s;
3767 char ifname[100];
3768 char force_name[100];
3769 int ret;
3770
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003771 ret = os_snprintf(ifname, sizeof(ifname), P2P_MGMT_DEVICE_PREFIX "%s",
3772 wpa_s->ifname);
3773 if (os_snprintf_error(sizeof(ifname), ret))
3774 return -1;
Dmitry Shmidt34af3062013-07-11 10:46:32 -07003775 force_name[0] = '\0';
3776 wpa_s->pending_interface_type = WPA_IF_P2P_DEVICE;
3777 ret = wpa_drv_if_add(wpa_s, WPA_IF_P2P_DEVICE, ifname, NULL, NULL,
3778 force_name, wpa_s->pending_interface_addr, NULL);
3779 if (ret < 0) {
3780 wpa_printf(MSG_DEBUG, "P2P: Failed to create P2P Device interface");
3781 return ret;
3782 }
3783 os_strlcpy(wpa_s->pending_interface_name, ifname,
3784 sizeof(wpa_s->pending_interface_name));
3785
3786 os_memset(&iface, 0, sizeof(iface));
3787 iface.p2p_mgmt = 1;
3788 iface.ifname = wpa_s->pending_interface_name;
3789 iface.driver = wpa_s->driver->name;
3790 iface.driver_param = wpa_s->conf->driver_param;
Dmitry Shmidt2ac5f602014-03-07 10:08:21 -08003791
3792 /*
3793 * If a P2P Device configuration file was given, use it as the interface
3794 * configuration file (instead of using parent's configuration file.
3795 */
Dmitry Shmidt9ead16e2014-10-07 13:15:23 -07003796 if (conf_p2p_dev) {
3797 iface.confname = conf_p2p_dev;
Dmitry Shmidt2ac5f602014-03-07 10:08:21 -08003798 iface.ctrl_interface = NULL;
3799 } else {
3800 iface.confname = wpa_s->confname;
3801 iface.ctrl_interface = wpa_s->conf->ctrl_interface;
3802 }
Dmitry Shmidt2ac5f602014-03-07 10:08:21 -08003803
Dmitry Shmidt203eadb2015-03-05 14:16:04 -08003804 p2pdev_wpa_s = wpa_supplicant_add_iface(wpa_s->global, &iface, wpa_s);
Dmitry Shmidt34af3062013-07-11 10:46:32 -07003805 if (!p2pdev_wpa_s) {
3806 wpa_printf(MSG_DEBUG, "P2P: Failed to add P2P Device interface");
3807 return -1;
3808 }
Dmitry Shmidt34af3062013-07-11 10:46:32 -07003809
Dmitry Shmidt9c175262016-03-03 10:20:07 -08003810 p2pdev_wpa_s->p2pdev = p2pdev_wpa_s;
Dmitry Shmidt34af3062013-07-11 10:46:32 -07003811 wpa_s->pending_interface_name[0] = '\0';
3812 return 0;
3813}
3814
3815
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08003816static void wpas_presence_resp(void *ctx, const u8 *src, u8 status,
3817 const u8 *noa, size_t noa_len)
3818{
3819 struct wpa_supplicant *wpa_s, *intf = ctx;
3820 char hex[100];
3821
3822 for (wpa_s = intf->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
3823 if (wpa_s->waiting_presence_resp)
3824 break;
3825 }
3826 if (!wpa_s) {
3827 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: No group interface was waiting for presence response");
3828 return;
3829 }
3830 wpa_s->waiting_presence_resp = 0;
3831
3832 wpa_snprintf_hex(hex, sizeof(hex), noa, noa_len);
3833 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_PRESENCE_RESPONSE "src=" MACSTR
3834 " status=%u noa=%s", MAC2STR(src), status, hex);
3835}
3836
3837
Dmitry Shmidt216983b2015-02-06 10:50:36 -08003838static int wpas_get_persistent_group(void *ctx, const u8 *addr, const u8 *ssid,
3839 size_t ssid_len, u8 *go_dev_addr,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003840 u8 *ret_ssid, size_t *ret_ssid_len,
3841 u8 *intended_iface_addr)
Dmitry Shmidt216983b2015-02-06 10:50:36 -08003842{
3843 struct wpa_supplicant *wpa_s = ctx;
3844 struct wpa_ssid *s;
3845
3846 s = wpas_p2p_get_persistent(wpa_s, addr, ssid, ssid_len);
3847 if (s) {
3848 os_memcpy(ret_ssid, s->ssid, s->ssid_len);
3849 *ret_ssid_len = s->ssid_len;
3850 os_memcpy(go_dev_addr, s->bssid, ETH_ALEN);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003851
3852 if (s->mode != WPAS_MODE_P2P_GO) {
3853 os_memset(intended_iface_addr, 0, ETH_ALEN);
3854 } else if (wpas_p2p_create_iface(wpa_s)) {
3855 if (wpas_p2p_add_group_interface(wpa_s, WPA_IF_P2P_GO))
3856 return 0;
3857
3858 os_memcpy(intended_iface_addr,
3859 wpa_s->pending_interface_addr, ETH_ALEN);
3860 } else {
3861 os_memcpy(intended_iface_addr, wpa_s->own_addr,
3862 ETH_ALEN);
3863 }
Dmitry Shmidt216983b2015-02-06 10:50:36 -08003864 return 1;
3865 }
3866
3867 return 0;
3868}
3869
3870
3871static int wpas_get_go_info(void *ctx, u8 *intended_addr,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003872 u8 *ssid, size_t *ssid_len, int *group_iface,
3873 unsigned int *freq)
Dmitry Shmidt216983b2015-02-06 10:50:36 -08003874{
3875 struct wpa_supplicant *wpa_s = ctx;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003876 struct wpa_supplicant *go;
Dmitry Shmidt216983b2015-02-06 10:50:36 -08003877 struct wpa_ssid *s;
Dmitry Shmidt216983b2015-02-06 10:50:36 -08003878
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003879 /*
3880 * group_iface will be set to 1 only if a dedicated interface for P2P
3881 * role is required. First, we try to reuse an active GO. However,
3882 * if it is not present, we will try to reactivate an existing
3883 * persistent group and set group_iface to 1, so the caller will know
3884 * that the pending interface should be used.
3885 */
3886 *group_iface = 0;
3887
3888 if (freq)
3889 *freq = 0;
3890
3891 go = wpas_p2p_get_go_group(wpa_s);
3892 if (!go) {
Dmitry Shmidt216983b2015-02-06 10:50:36 -08003893 s = wpas_p2p_get_persistent_go(wpa_s);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003894 *group_iface = wpas_p2p_create_iface(wpa_s);
Dmitry Shmidt216983b2015-02-06 10:50:36 -08003895 if (s)
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003896 os_memcpy(intended_addr, s->bssid, ETH_ALEN);
3897 else
3898 return 0;
3899 } else {
3900 s = go->current_ssid;
3901 os_memcpy(intended_addr, go->own_addr, ETH_ALEN);
3902 if (freq)
3903 *freq = go->assoc_freq;
Dmitry Shmidt216983b2015-02-06 10:50:36 -08003904 }
3905
Dmitry Shmidt216983b2015-02-06 10:50:36 -08003906 os_memcpy(ssid, s->ssid, s->ssid_len);
3907 *ssid_len = s->ssid_len;
3908
3909 return 1;
3910}
3911
3912
3913static int wpas_remove_stale_groups(void *ctx, const u8 *peer, const u8 *go,
3914 const u8 *ssid, size_t ssid_len)
3915{
3916 struct wpa_supplicant *wpa_s = ctx;
3917 struct wpa_ssid *s;
3918 int save_config = 0;
3919 size_t i;
3920
3921 /* Start with our first choice of Persistent Groups */
3922 while ((s = wpas_p2p_get_persistent(wpa_s, peer, NULL, 0))) {
3923 if (go && ssid && ssid_len &&
3924 s->ssid_len == ssid_len &&
3925 os_memcmp(go, s->bssid, ETH_ALEN) == 0 &&
3926 os_memcmp(ssid, s->ssid, ssid_len) == 0)
3927 break;
3928
3929 /* Remove stale persistent group */
3930 if (s->mode != WPAS_MODE_P2P_GO || s->num_p2p_clients <= 1) {
3931 wpa_config_remove_network(wpa_s->conf, s->id);
3932 save_config = 1;
3933 continue;
3934 }
3935
3936 for (i = 0; i < s->num_p2p_clients; i++) {
3937 if (os_memcmp(s->p2p_client_list + i * 2 * ETH_ALEN,
3938 peer, ETH_ALEN) != 0)
3939 continue;
3940
3941 os_memmove(s->p2p_client_list + i * 2 * ETH_ALEN,
3942 s->p2p_client_list + (i + 1) * 2 * ETH_ALEN,
3943 (s->num_p2p_clients - i - 1) * 2 * ETH_ALEN);
3944 break;
3945 }
3946 s->num_p2p_clients--;
3947 save_config = 1;
3948 }
3949
3950 if (save_config)
3951 p2p_config_write(wpa_s);
3952
3953 /* Return TRUE if valid SSID remains */
3954 return s != NULL;
3955}
3956
3957
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003958static void wpas_p2ps_get_feat_cap_str(char *buf, size_t buf_len,
3959 const u8 *feat_cap, size_t feat_cap_len)
3960{
3961 static const char pref[] = " feature_cap=";
3962 int ret;
3963
3964 buf[0] = '\0';
3965
3966 /*
3967 * We expect a feature capability to contain at least one byte to be
3968 * reported. The string buffer provided by the caller function is
3969 * expected to be big enough to contain all bytes of the attribute for
3970 * known specifications. This function truncates the reported bytes if
3971 * the feature capability data exceeds the string buffer size.
3972 */
3973 if (!feat_cap || !feat_cap_len || buf_len < sizeof(pref) + 2)
3974 return;
3975
3976 os_memcpy(buf, pref, sizeof(pref));
3977 ret = wpa_snprintf_hex(&buf[sizeof(pref) - 1],
3978 buf_len - sizeof(pref) + 1,
3979 feat_cap, feat_cap_len);
3980
3981 if (ret != (2 * (int) feat_cap_len))
3982 wpa_printf(MSG_WARNING, "P2PS feature_cap bytes truncated");
3983}
3984
3985
Dmitry Shmidt216983b2015-02-06 10:50:36 -08003986static void wpas_p2ps_prov_complete(void *ctx, u8 status, const u8 *dev,
3987 const u8 *adv_mac, const u8 *ses_mac,
3988 const u8 *grp_mac, u32 adv_id, u32 ses_id,
3989 u8 conncap, int passwd_id,
3990 const u8 *persist_ssid,
3991 size_t persist_ssid_size, int response_done,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003992 int prov_start, const char *session_info,
3993 const u8 *feat_cap, size_t feat_cap_len,
Dmitry Shmidtde47be72016-01-07 12:52:55 -08003994 unsigned int freq,
3995 const u8 *group_ssid, size_t group_ssid_len)
Dmitry Shmidt216983b2015-02-06 10:50:36 -08003996{
3997 struct wpa_supplicant *wpa_s = ctx;
3998 u8 mac[ETH_ALEN];
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003999 struct wpa_ssid *persistent_go, *stale, *s = NULL;
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004000 int save_config = 0;
4001 struct wpa_supplicant *go_wpa_s;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004002 char feat_cap_str[256];
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004003
4004 if (!dev)
4005 return;
4006
4007 os_memset(mac, 0, ETH_ALEN);
4008 if (!adv_mac)
4009 adv_mac = mac;
4010 if (!ses_mac)
4011 ses_mac = mac;
4012 if (!grp_mac)
4013 grp_mac = mac;
4014
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004015 wpas_p2ps_get_feat_cap_str(feat_cap_str, sizeof(feat_cap_str),
4016 feat_cap, feat_cap_len);
4017
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004018 if (prov_start) {
4019 if (session_info == NULL) {
4020 wpa_msg_global(wpa_s, MSG_INFO,
4021 P2P_EVENT_P2PS_PROVISION_START MACSTR
4022 " adv_id=%x conncap=%x"
4023 " adv_mac=" MACSTR
4024 " session=%x mac=" MACSTR
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004025 " dev_passwd_id=%d%s",
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004026 MAC2STR(dev), adv_id, conncap,
4027 MAC2STR(adv_mac),
4028 ses_id, MAC2STR(ses_mac),
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004029 passwd_id, feat_cap_str);
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004030 } else {
4031 wpa_msg_global(wpa_s, MSG_INFO,
4032 P2P_EVENT_P2PS_PROVISION_START MACSTR
4033 " adv_id=%x conncap=%x"
4034 " adv_mac=" MACSTR
4035 " session=%x mac=" MACSTR
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004036 " dev_passwd_id=%d info='%s'%s",
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004037 MAC2STR(dev), adv_id, conncap,
4038 MAC2STR(adv_mac),
4039 ses_id, MAC2STR(ses_mac),
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004040 passwd_id, session_info, feat_cap_str);
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004041 }
4042 return;
4043 }
4044
4045 go_wpa_s = wpas_p2p_get_go_group(wpa_s);
4046 persistent_go = wpas_p2p_get_persistent_go(wpa_s);
4047
4048 if (status && status != P2P_SC_SUCCESS_DEFERRED) {
4049 if (go_wpa_s && !p2p_group_go_member_count(wpa_s))
4050 wpas_p2p_group_remove(wpa_s, go_wpa_s->ifname);
4051
4052 if (persistent_go && !persistent_go->num_p2p_clients) {
4053 /* remove empty persistent GO */
4054 wpa_config_remove_network(wpa_s->conf,
4055 persistent_go->id);
4056 }
4057
4058 wpa_msg_global(wpa_s, MSG_INFO,
4059 P2P_EVENT_P2PS_PROVISION_DONE MACSTR
4060 " status=%d"
4061 " adv_id=%x adv_mac=" MACSTR
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004062 " session=%x mac=" MACSTR "%s",
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004063 MAC2STR(dev), status,
4064 adv_id, MAC2STR(adv_mac),
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004065 ses_id, MAC2STR(ses_mac), feat_cap_str);
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004066 return;
4067 }
4068
4069 /* Clean up stale persistent groups with this device */
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004070 if (persist_ssid && persist_ssid_size)
4071 s = wpas_p2p_get_persistent(wpa_s, dev, persist_ssid,
4072 persist_ssid_size);
4073
4074 if (persist_ssid && s && s->mode != WPAS_MODE_P2P_GO &&
4075 is_zero_ether_addr(grp_mac)) {
4076 wpa_dbg(wpa_s, MSG_ERROR,
4077 "P2P: Peer device is a GO in a persistent group, but it did not provide the intended MAC address");
4078 return;
4079 }
4080
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004081 for (;;) {
4082 stale = wpas_p2p_get_persistent(wpa_s, dev, NULL, 0);
4083 if (!stale)
4084 break;
4085
4086 if (s && s->ssid_len == stale->ssid_len &&
4087 os_memcmp(stale->bssid, s->bssid, ETH_ALEN) == 0 &&
4088 os_memcmp(stale->ssid, s->ssid, s->ssid_len) == 0)
4089 break;
4090
4091 /* Remove stale persistent group */
4092 if (stale->mode != WPAS_MODE_P2P_GO ||
4093 stale->num_p2p_clients <= 1) {
4094 wpa_config_remove_network(wpa_s->conf, stale->id);
4095 } else {
4096 size_t i;
4097
4098 for (i = 0; i < stale->num_p2p_clients; i++) {
4099 if (os_memcmp(stale->p2p_client_list +
4100 i * ETH_ALEN,
4101 dev, ETH_ALEN) == 0) {
4102 os_memmove(stale->p2p_client_list +
4103 i * ETH_ALEN,
4104 stale->p2p_client_list +
4105 (i + 1) * ETH_ALEN,
4106 (stale->num_p2p_clients -
4107 i - 1) * ETH_ALEN);
4108 break;
4109 }
4110 }
4111 stale->num_p2p_clients--;
4112 }
4113 save_config = 1;
4114 }
4115
4116 if (save_config)
4117 p2p_config_write(wpa_s);
4118
4119 if (s) {
4120 if (go_wpa_s && !p2p_group_go_member_count(wpa_s))
4121 wpas_p2p_group_remove(wpa_s, go_wpa_s->ifname);
4122
4123 if (persistent_go && s != persistent_go &&
4124 !persistent_go->num_p2p_clients) {
4125 /* remove empty persistent GO */
4126 wpa_config_remove_network(wpa_s->conf,
4127 persistent_go->id);
4128 /* Save config */
4129 }
4130
4131 wpa_msg_global(wpa_s, MSG_INFO,
4132 P2P_EVENT_P2PS_PROVISION_DONE MACSTR
4133 " status=%d"
4134 " adv_id=%x adv_mac=" MACSTR
4135 " session=%x mac=" MACSTR
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004136 " persist=%d%s",
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004137 MAC2STR(dev), status,
4138 adv_id, MAC2STR(adv_mac),
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004139 ses_id, MAC2STR(ses_mac), s->id, feat_cap_str);
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004140 return;
4141 }
4142
4143 if (conncap == P2PS_SETUP_GROUP_OWNER) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004144 /*
4145 * We need to copy the interface name. Simply saving a
4146 * pointer isn't enough, since if we use pending_interface_name
4147 * it will be overwritten when the group is added.
4148 */
4149 char go_ifname[100];
4150
4151 go_ifname[0] = '\0';
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004152 if (!go_wpa_s) {
4153 wpa_s->global->pending_p2ps_group = 1;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004154 wpa_s->global->pending_p2ps_group_freq = freq;
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004155
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004156 if (!wpas_p2p_create_iface(wpa_s))
4157 os_memcpy(go_ifname, wpa_s->ifname,
4158 sizeof(go_ifname));
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004159 else if (wpa_s->pending_interface_name[0])
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004160 os_memcpy(go_ifname,
4161 wpa_s->pending_interface_name,
4162 sizeof(go_ifname));
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004163
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004164 if (!go_ifname[0]) {
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004165 wpas_p2ps_prov_complete(
4166 wpa_s, P2P_SC_FAIL_UNKNOWN_GROUP,
4167 dev, adv_mac, ses_mac,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004168 grp_mac, adv_id, ses_id, 0, 0,
Dmitry Shmidtde47be72016-01-07 12:52:55 -08004169 NULL, 0, 0, 0, NULL, NULL, 0, 0,
4170 NULL, 0);
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004171 return;
4172 }
4173
4174 /* If PD Resp complete, start up the GO */
4175 if (response_done && persistent_go) {
4176 wpas_p2p_group_add_persistent(
4177 wpa_s, persistent_go,
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08004178 0, 0, freq, 0, 0, 0, 0, NULL,
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004179 persistent_go->mode ==
4180 WPAS_MODE_P2P_GO ?
4181 P2P_MAX_INITIAL_CONN_WAIT_GO_REINVOKE :
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004182 0, 0);
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004183 } else if (response_done) {
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08004184 wpas_p2p_group_add(wpa_s, 1, freq, 0, 0, 0, 0);
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004185 }
4186
4187 if (passwd_id == DEV_PW_P2PS_DEFAULT) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004188 os_memcpy(wpa_s->p2ps_join_addr, grp_mac,
4189 ETH_ALEN);
4190 wpa_s->p2ps_method_config_any = 1;
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004191 }
4192 } else if (passwd_id == DEV_PW_P2PS_DEFAULT) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004193 os_memcpy(go_ifname, go_wpa_s->ifname,
4194 sizeof(go_ifname));
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004195
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004196 if (is_zero_ether_addr(grp_mac)) {
4197 wpa_dbg(go_wpa_s, MSG_DEBUG,
4198 "P2P: Setting PIN-1 for ANY");
4199 wpa_supplicant_ap_wps_pin(go_wpa_s, NULL,
4200 "12345670", NULL, 0,
4201 0);
4202 } else {
4203 wpa_dbg(go_wpa_s, MSG_DEBUG,
4204 "P2P: Setting PIN-1 for " MACSTR,
4205 MAC2STR(grp_mac));
4206 wpa_supplicant_ap_wps_pin(go_wpa_s, grp_mac,
4207 "12345670", NULL, 0,
4208 0);
4209 }
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004210
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004211 os_memcpy(wpa_s->p2ps_join_addr, grp_mac, ETH_ALEN);
4212 wpa_s->p2ps_method_config_any = 1;
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004213 }
4214
4215 wpa_msg_global(wpa_s, MSG_INFO,
4216 P2P_EVENT_P2PS_PROVISION_DONE MACSTR
4217 " status=%d conncap=%x"
4218 " adv_id=%x adv_mac=" MACSTR
4219 " session=%x mac=" MACSTR
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004220 " dev_passwd_id=%d go=%s%s",
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004221 MAC2STR(dev), status, conncap,
4222 adv_id, MAC2STR(adv_mac),
4223 ses_id, MAC2STR(ses_mac),
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004224 passwd_id, go_ifname, feat_cap_str);
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004225 return;
4226 }
4227
4228 if (go_wpa_s && !p2p_group_go_member_count(wpa_s))
4229 wpas_p2p_group_remove(wpa_s, go_wpa_s->ifname);
4230
4231 if (persistent_go && !persistent_go->num_p2p_clients) {
4232 /* remove empty persistent GO */
4233 wpa_config_remove_network(wpa_s->conf, persistent_go->id);
4234 }
4235
4236 if (conncap == P2PS_SETUP_CLIENT) {
Dmitry Shmidtde47be72016-01-07 12:52:55 -08004237 char ssid_hex[32 * 2 + 1];
4238
4239 if (group_ssid)
4240 wpa_snprintf_hex(ssid_hex, sizeof(ssid_hex),
4241 group_ssid, group_ssid_len);
4242 else
4243 ssid_hex[0] = '\0';
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004244 wpa_msg_global(wpa_s, MSG_INFO,
4245 P2P_EVENT_P2PS_PROVISION_DONE MACSTR
4246 " status=%d conncap=%x"
4247 " adv_id=%x adv_mac=" MACSTR
4248 " session=%x mac=" MACSTR
Dmitry Shmidtde47be72016-01-07 12:52:55 -08004249 " dev_passwd_id=%d join=" MACSTR "%s%s%s",
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004250 MAC2STR(dev), status, conncap,
4251 adv_id, MAC2STR(adv_mac),
4252 ses_id, MAC2STR(ses_mac),
Dmitry Shmidtde47be72016-01-07 12:52:55 -08004253 passwd_id, MAC2STR(grp_mac), feat_cap_str,
4254 group_ssid ? " group_ssid=" : "", ssid_hex);
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004255 } else {
4256 wpa_msg_global(wpa_s, MSG_INFO,
4257 P2P_EVENT_P2PS_PROVISION_DONE MACSTR
4258 " status=%d conncap=%x"
4259 " adv_id=%x adv_mac=" MACSTR
4260 " session=%x mac=" MACSTR
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004261 " dev_passwd_id=%d%s",
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004262 MAC2STR(dev), status, conncap,
4263 adv_id, MAC2STR(adv_mac),
4264 ses_id, MAC2STR(ses_mac),
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004265 passwd_id, feat_cap_str);
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004266 }
4267}
4268
4269
Dmitry Shmidt5a1480c2014-05-12 09:46:02 -07004270static int _wpas_p2p_in_progress(void *ctx)
4271{
4272 struct wpa_supplicant *wpa_s = ctx;
4273 return wpas_p2p_in_progress(wpa_s);
4274}
4275
4276
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004277static int wpas_prov_disc_resp_cb(void *ctx)
4278{
4279 struct wpa_supplicant *wpa_s = ctx;
4280 struct wpa_ssid *persistent_go;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004281 unsigned int freq;
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004282
4283 if (!wpa_s->global->pending_p2ps_group)
4284 return 0;
4285
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004286 freq = wpa_s->global->pending_p2ps_group_freq;
4287 wpa_s->global->pending_p2ps_group_freq = 0;
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004288 wpa_s->global->pending_p2ps_group = 0;
4289
4290 if (wpas_p2p_get_go_group(wpa_s))
4291 return 0;
4292 persistent_go = wpas_p2p_get_persistent_go(wpa_s);
4293
4294 if (persistent_go) {
4295 wpas_p2p_group_add_persistent(
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08004296 wpa_s, persistent_go, 0, 0, 0, 0, 0, 0, 0, NULL,
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004297 persistent_go->mode == WPAS_MODE_P2P_GO ?
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004298 P2P_MAX_INITIAL_CONN_WAIT_GO_REINVOKE : 0, 0);
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004299 } else {
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08004300 wpas_p2p_group_add(wpa_s, 1, freq, 0, 0, 0, 0);
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004301 }
4302
4303 return 1;
4304}
4305
4306
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004307static int wpas_p2p_get_pref_freq_list(void *ctx, int go,
4308 unsigned int *len,
4309 unsigned int *freq_list)
4310{
4311 struct wpa_supplicant *wpa_s = ctx;
4312
4313 return wpa_drv_get_pref_freq_list(wpa_s, go ? WPA_IF_P2P_GO :
4314 WPA_IF_P2P_CLIENT, len, freq_list);
4315}
4316
Jimmy Chenf887c7b2018-11-13 15:19:57 +08004317int wpas_p2p_mac_setup(struct wpa_supplicant *wpa_s)
4318{
4319 u8 addr[ETH_ALEN] = {0};
4320
4321 if (wpa_s->conf->p2p_device_random_mac_addr == 0)
4322 return 0;
4323
4324 if (wpa_s->conf->ssid == NULL) {
4325 if (random_mac_addr(addr) < 0) {
4326 wpa_msg(wpa_s, MSG_INFO,
4327 "Failed to generate random MAC address");
4328 return -EINVAL;
4329 }
4330
4331 // store generated MAC address.
4332 if (wpa_s->conf->p2p_device_persistent_mac_addr)
4333 os_free(wpa_s->conf->p2p_device_persistent_mac_addr);
4334 size_t mac_addr_str_len = sizeof("00:00:00:00:00:00");
4335 wpa_s->conf->p2p_device_persistent_mac_addr =
4336 os_zalloc(mac_addr_str_len + 1);
4337 os_snprintf(wpa_s->conf->p2p_device_persistent_mac_addr,
4338 mac_addr_str_len, MACSTR, MAC2STR(addr));
4339 } else {
4340 // If there are existing saved groups, restore last MAC address.
4341 // if there is no last used MAC address, the last one is factory MAC.
4342 if (!wpa_s->conf->p2p_device_persistent_mac_addr)
4343 return 0;
4344
4345 if (hwaddr_aton(wpa_s->conf->p2p_device_persistent_mac_addr, addr) < 0)
4346 return -EINVAL;
4347 wpa_msg(wpa_s, MSG_DEBUG, "Restore last used MAC address.");
4348 }
4349
4350 if (wpa_drv_set_mac_addr(wpa_s, addr) < 0) {
4351 wpa_msg(wpa_s, MSG_INFO,
4352 "Failed to set random MAC address");
4353 return -EINVAL;
4354 }
4355
4356 if (wpa_supplicant_update_mac_addr(wpa_s) < 0) {
4357 wpa_msg(wpa_s, MSG_INFO,
4358 "Could not update MAC address information");
4359 return -EINVAL;
4360 }
4361
4362 wpa_msg(wpa_s, MSG_DEBUG, "Using random MAC address " MACSTR,
4363 MAC2STR(addr));
4364
4365 return 0;
4366}
4367
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004368
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004369/**
4370 * wpas_p2p_init - Initialize P2P module for %wpa_supplicant
4371 * @global: Pointer to global data from wpa_supplicant_init()
4372 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
4373 * Returns: 0 on success, -1 on failure
4374 */
4375int wpas_p2p_init(struct wpa_global *global, struct wpa_supplicant *wpa_s)
4376{
4377 struct p2p_config p2p;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004378 int i;
4379
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07004380 if (wpa_s->conf->p2p_disabled)
4381 return 0;
4382
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004383 if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_CAPABLE))
4384 return 0;
4385
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004386 if (global->p2p)
4387 return 0;
4388
Jimmy Chenf887c7b2018-11-13 15:19:57 +08004389 if (wpas_p2p_mac_setup(wpa_s) < 0) {
4390 wpa_msg(wpa_s, MSG_ERROR,
4391 "Failed to initialize P2P random MAC address.");
4392 return -1;
4393 }
4394
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004395 os_memset(&p2p, 0, sizeof(p2p));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004396 p2p.cb_ctx = wpa_s;
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07004397 p2p.debug_print = wpas_p2p_debug_print;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004398 p2p.p2p_scan = wpas_p2p_scan;
4399 p2p.send_action = wpas_send_action;
4400 p2p.send_action_done = wpas_send_action_done;
4401 p2p.go_neg_completed = wpas_go_neg_completed;
4402 p2p.go_neg_req_rx = wpas_go_neg_req_rx;
4403 p2p.dev_found = wpas_dev_found;
4404 p2p.dev_lost = wpas_dev_lost;
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07004405 p2p.find_stopped = wpas_find_stopped;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004406 p2p.start_listen = wpas_start_listen;
4407 p2p.stop_listen = wpas_stop_listen;
4408 p2p.send_probe_resp = wpas_send_probe_resp;
4409 p2p.sd_request = wpas_sd_request;
4410 p2p.sd_response = wpas_sd_response;
4411 p2p.prov_disc_req = wpas_prov_disc_req;
4412 p2p.prov_disc_resp = wpas_prov_disc_resp;
Jouni Malinen75ecf522011-06-27 15:19:46 -07004413 p2p.prov_disc_fail = wpas_prov_disc_fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004414 p2p.invitation_process = wpas_invitation_process;
4415 p2p.invitation_received = wpas_invitation_received;
4416 p2p.invitation_result = wpas_invitation_result;
4417 p2p.get_noa = wpas_get_noa;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08004418 p2p.go_connected = wpas_go_connected;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08004419 p2p.presence_resp = wpas_presence_resp;
Dmitry Shmidt18463232014-01-24 12:29:41 -08004420 p2p.is_concurrent_session_active = wpas_is_concurrent_session_active;
Dmitry Shmidt5a1480c2014-05-12 09:46:02 -07004421 p2p.is_p2p_in_progress = _wpas_p2p_in_progress;
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004422 p2p.get_persistent_group = wpas_get_persistent_group;
4423 p2p.get_go_info = wpas_get_go_info;
4424 p2p.remove_stale_groups = wpas_remove_stale_groups;
4425 p2p.p2ps_prov_complete = wpas_p2ps_prov_complete;
4426 p2p.prov_disc_resp_cb = wpas_prov_disc_resp_cb;
4427 p2p.p2ps_group_capability = p2ps_group_capability;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004428 p2p.get_pref_freq_list = wpas_p2p_get_pref_freq_list;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004429
4430 os_memcpy(wpa_s->global->p2p_dev_addr, wpa_s->own_addr, ETH_ALEN);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004431 os_memcpy(p2p.dev_addr, wpa_s->global->p2p_dev_addr, ETH_ALEN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004432 p2p.dev_name = wpa_s->conf->device_name;
4433 p2p.manufacturer = wpa_s->conf->manufacturer;
4434 p2p.model_name = wpa_s->conf->model_name;
4435 p2p.model_number = wpa_s->conf->model_number;
4436 p2p.serial_number = wpa_s->conf->serial_number;
4437 if (wpa_s->wps) {
4438 os_memcpy(p2p.uuid, wpa_s->wps->uuid, 16);
4439 p2p.config_methods = wpa_s->wps->config_methods;
4440 }
4441
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07004442 if (wpas_p2p_setup_channels(wpa_s, &p2p.channels, &p2p.cli_channels)) {
4443 wpa_printf(MSG_ERROR,
4444 "P2P: Failed to configure supported channel list");
4445 return -1;
4446 }
4447
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004448 if (wpa_s->conf->p2p_listen_reg_class &&
4449 wpa_s->conf->p2p_listen_channel) {
4450 p2p.reg_class = wpa_s->conf->p2p_listen_reg_class;
4451 p2p.channel = wpa_s->conf->p2p_listen_channel;
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07004452 p2p.channel_forced = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004453 } else {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004454 /*
4455 * Pick one of the social channels randomly as the listen
4456 * channel.
4457 */
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07004458 if (p2p_config_get_random_social(&p2p, &p2p.reg_class,
4459 &p2p.channel) != 0) {
Dmitry Shmidt1d755d02015-04-28 10:34:29 -07004460 wpa_printf(MSG_INFO,
4461 "P2P: No social channels supported by the driver - do not enable P2P");
4462 return 0;
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07004463 }
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07004464 p2p.channel_forced = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004465 }
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07004466 wpa_printf(MSG_DEBUG, "P2P: Own listen channel: %d:%d",
4467 p2p.reg_class, p2p.channel);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004468
4469 if (wpa_s->conf->p2p_oper_reg_class &&
4470 wpa_s->conf->p2p_oper_channel) {
4471 p2p.op_reg_class = wpa_s->conf->p2p_oper_reg_class;
4472 p2p.op_channel = wpa_s->conf->p2p_oper_channel;
4473 p2p.cfg_op_channel = 1;
4474 wpa_printf(MSG_DEBUG, "P2P: Configured operating channel: "
4475 "%d:%d", p2p.op_reg_class, p2p.op_channel);
4476
4477 } else {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004478 /*
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07004479 * Use random operation channel from 2.4 GHz band social
4480 * channels (1, 6, 11) or band 60 GHz social channel (2) if no
4481 * other preference is indicated.
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004482 */
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07004483 if (p2p_config_get_random_social(&p2p, &p2p.op_reg_class,
4484 &p2p.op_channel) != 0) {
4485 wpa_printf(MSG_ERROR,
4486 "P2P: Failed to select random social channel as operation channel");
4487 return -1;
4488 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004489 p2p.cfg_op_channel = 0;
4490 wpa_printf(MSG_DEBUG, "P2P: Random operating channel: "
4491 "%d:%d", p2p.op_reg_class, p2p.op_channel);
4492 }
Dmitry Shmidt44c95782013-05-17 09:51:35 -07004493
4494 if (wpa_s->conf->p2p_pref_chan && wpa_s->conf->num_p2p_pref_chan) {
4495 p2p.pref_chan = wpa_s->conf->p2p_pref_chan;
4496 p2p.num_pref_chan = wpa_s->conf->num_p2p_pref_chan;
4497 }
4498
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004499 if (wpa_s->conf->country[0] && wpa_s->conf->country[1]) {
4500 os_memcpy(p2p.country, wpa_s->conf->country, 2);
4501 p2p.country[2] = 0x04;
4502 } else
4503 os_memcpy(p2p.country, "XX\x04", 3);
4504
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004505 os_memcpy(p2p.pri_dev_type, wpa_s->conf->device_type,
4506 WPS_DEV_TYPE_LEN);
4507
4508 p2p.num_sec_dev_types = wpa_s->conf->num_sec_device_types;
4509 os_memcpy(p2p.sec_dev_type, wpa_s->conf->sec_device_type,
4510 p2p.num_sec_dev_types * WPS_DEV_TYPE_LEN);
4511
4512 p2p.concurrent_operations = !!(wpa_s->drv_flags &
4513 WPA_DRIVER_FLAGS_P2P_CONCURRENT);
4514
4515 p2p.max_peers = 100;
4516
4517 if (wpa_s->conf->p2p_ssid_postfix) {
4518 p2p.ssid_postfix_len =
4519 os_strlen(wpa_s->conf->p2p_ssid_postfix);
4520 if (p2p.ssid_postfix_len > sizeof(p2p.ssid_postfix))
4521 p2p.ssid_postfix_len = sizeof(p2p.ssid_postfix);
4522 os_memcpy(p2p.ssid_postfix, wpa_s->conf->p2p_ssid_postfix,
4523 p2p.ssid_postfix_len);
4524 }
4525
4526 p2p.p2p_intra_bss = wpa_s->conf->p2p_intra_bss;
4527
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08004528 p2p.max_listen = wpa_s->max_remain_on_chan;
4529
Dmitry Shmidt2271d3f2014-06-23 12:16:31 -07004530 if (wpa_s->conf->p2p_passphrase_len >= 8 &&
4531 wpa_s->conf->p2p_passphrase_len <= 63)
4532 p2p.passphrase_len = wpa_s->conf->p2p_passphrase_len;
4533 else
4534 p2p.passphrase_len = 8;
4535
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004536 global->p2p = p2p_init(&p2p);
4537 if (global->p2p == NULL)
4538 return -1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07004539 global->p2p_init_wpa_s = wpa_s;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004540
4541 for (i = 0; i < MAX_WPS_VENDOR_EXT; i++) {
4542 if (wpa_s->conf->wps_vendor_ext[i] == NULL)
4543 continue;
4544 p2p_add_wps_vendor_extension(
4545 global->p2p, wpa_s->conf->wps_vendor_ext[i]);
4546 }
4547
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004548 p2p_set_no_go_freq(global->p2p, &wpa_s->conf->p2p_no_go_freq);
4549
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004550 return 0;
4551}
4552
4553
4554/**
4555 * wpas_p2p_deinit - Deinitialize per-interface P2P data
4556 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
4557 *
4558 * This function deinitialize per-interface P2P data.
4559 */
4560void wpas_p2p_deinit(struct wpa_supplicant *wpa_s)
4561{
4562 if (wpa_s->driver && wpa_s->drv_priv)
4563 wpa_drv_probe_req_report(wpa_s, 0);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004564
4565 if (wpa_s->go_params) {
4566 /* Clear any stored provisioning info */
4567 p2p_clear_provisioning_info(
4568 wpa_s->global->p2p,
Dmitry Shmidt04949592012-07-19 12:16:46 -07004569 wpa_s->go_params->peer_device_addr);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004570 }
4571
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004572 os_free(wpa_s->go_params);
4573 wpa_s->go_params = NULL;
Dmitry Shmidt684785c2014-05-12 13:34:29 -07004574 eloop_cancel_timeout(wpas_p2p_psk_failure_removal, wpa_s, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004575 eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s, NULL);
4576 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
4577 wpa_s->p2p_long_listen = 0;
4578 eloop_cancel_timeout(wpas_p2p_long_listen_timeout, wpa_s, NULL);
4579 eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL);
4580 wpas_p2p_remove_pending_group_interface(wpa_s);
Dmitry Shmidt04f534e2013-12-09 15:50:16 -08004581 eloop_cancel_timeout(wpas_p2p_group_freq_conflict, wpa_s, NULL);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08004582 wpas_p2p_listen_work_done(wpa_s);
4583 if (wpa_s->p2p_send_action_work) {
4584 os_free(wpa_s->p2p_send_action_work->ctx);
4585 radio_work_done(wpa_s->p2p_send_action_work);
4586 wpa_s->p2p_send_action_work = NULL;
4587 }
4588 eloop_cancel_timeout(wpas_p2p_send_action_work_timeout, wpa_s, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004589
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08004590 wpabuf_free(wpa_s->p2p_oob_dev_pw);
4591 wpa_s->p2p_oob_dev_pw = NULL;
4592
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004593 os_free(wpa_s->p2p_group_common_freqs);
4594 wpa_s->p2p_group_common_freqs = NULL;
4595 wpa_s->p2p_group_common_freqs_num = 0;
4596
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004597 /* TODO: remove group interface from the driver if this wpa_s instance
4598 * is on top of a P2P group interface */
4599}
4600
4601
4602/**
4603 * wpas_p2p_deinit_global - Deinitialize global P2P module
4604 * @global: Pointer to global data from wpa_supplicant_init()
4605 *
4606 * This function deinitializes the global (per device) P2P module.
4607 */
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07004608static void wpas_p2p_deinit_global(struct wpa_global *global)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004609{
4610 struct wpa_supplicant *wpa_s, *tmp;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004611
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004612 wpa_s = global->ifaces;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004613
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07004614 wpas_p2p_service_flush(global->p2p_init_wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004615
4616 /* Remove remaining P2P group interfaces */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004617 while (wpa_s && wpa_s->p2p_group_interface != NOT_P2P_GROUP_INTERFACE)
4618 wpa_s = wpa_s->next;
4619 while (wpa_s) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004620 tmp = global->ifaces;
4621 while (tmp &&
4622 (tmp == wpa_s ||
4623 tmp->p2p_group_interface == NOT_P2P_GROUP_INTERFACE)) {
4624 tmp = tmp->next;
4625 }
4626 if (tmp == NULL)
4627 break;
Dmitry Shmidt04949592012-07-19 12:16:46 -07004628 /* Disconnect from the P2P group and deinit the interface */
4629 wpas_p2p_disconnect(tmp);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004630 }
4631
4632 /*
4633 * Deinit GO data on any possibly remaining interface (if main
4634 * interface is used as GO).
4635 */
4636 for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
4637 if (wpa_s->ap_iface)
4638 wpas_p2p_group_deinit(wpa_s);
4639 }
4640
4641 p2p_deinit(global->p2p);
4642 global->p2p = NULL;
Dmitry Shmidt04949592012-07-19 12:16:46 -07004643 global->p2p_init_wpa_s = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004644}
4645
4646
4647static int wpas_p2p_create_iface(struct wpa_supplicant *wpa_s)
4648{
Dmitry Shmidt9c175262016-03-03 10:20:07 -08004649 if (wpa_s->conf->p2p_no_group_iface)
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08004650 return 0; /* separate interface disabled per configuration */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004651 if (wpa_s->drv_flags &
4652 (WPA_DRIVER_FLAGS_P2P_DEDICATED_INTERFACE |
4653 WPA_DRIVER_FLAGS_P2P_MGMT_AND_NON_P2P))
4654 return 1; /* P2P group requires a new interface in every case
4655 */
4656 if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_CONCURRENT))
4657 return 0; /* driver does not support concurrent operations */
4658 if (wpa_s->global->ifaces->next)
4659 return 1; /* more that one interface already in use */
4660 if (wpa_s->wpa_state >= WPA_AUTHENTICATING)
4661 return 1; /* this interface is already in use */
4662 return 0;
4663}
4664
4665
4666static int wpas_p2p_start_go_neg(struct wpa_supplicant *wpa_s,
4667 const u8 *peer_addr,
4668 enum p2p_wps_method wps_method,
4669 int go_intent, const u8 *own_interface_addr,
Dmitry Shmidt04949592012-07-19 12:16:46 -07004670 unsigned int force_freq, int persistent_group,
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08004671 struct wpa_ssid *ssid, unsigned int pref_freq)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004672{
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004673 if (persistent_group && wpa_s->conf->persistent_reconnect)
4674 persistent_group = 2;
4675
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004676 /*
4677 * Increase GO config timeout if HT40 is used since it takes some time
4678 * to scan channels for coex purposes before the BSS can be started.
4679 */
4680 p2p_set_config_timeout(wpa_s->global->p2p,
4681 wpa_s->p2p_go_ht40 ? 255 : 100, 20);
4682
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004683 return p2p_connect(wpa_s->global->p2p, peer_addr, wps_method,
4684 go_intent, own_interface_addr, force_freq,
Dmitry Shmidt04949592012-07-19 12:16:46 -07004685 persistent_group, ssid ? ssid->ssid : NULL,
4686 ssid ? ssid->ssid_len : 0,
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08004687 wpa_s->p2p_pd_before_go_neg, pref_freq,
4688 wps_method == WPS_NFC ? wpa_s->p2p_oob_dev_pw_id :
4689 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004690}
4691
4692
4693static int wpas_p2p_auth_go_neg(struct wpa_supplicant *wpa_s,
4694 const u8 *peer_addr,
4695 enum p2p_wps_method wps_method,
4696 int go_intent, const u8 *own_interface_addr,
Dmitry Shmidt04949592012-07-19 12:16:46 -07004697 unsigned int force_freq, int persistent_group,
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08004698 struct wpa_ssid *ssid, unsigned int pref_freq)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004699{
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004700 if (persistent_group && wpa_s->conf->persistent_reconnect)
4701 persistent_group = 2;
4702
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004703 return p2p_authorize(wpa_s->global->p2p, peer_addr, wps_method,
4704 go_intent, own_interface_addr, force_freq,
Dmitry Shmidt04949592012-07-19 12:16:46 -07004705 persistent_group, ssid ? ssid->ssid : NULL,
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08004706 ssid ? ssid->ssid_len : 0, pref_freq,
4707 wps_method == WPS_NFC ? wpa_s->p2p_oob_dev_pw_id :
4708 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004709}
4710
4711
4712static void wpas_p2p_check_join_scan_limit(struct wpa_supplicant *wpa_s)
4713{
4714 wpa_s->p2p_join_scan_count++;
4715 wpa_printf(MSG_DEBUG, "P2P: Join scan attempt %d",
4716 wpa_s->p2p_join_scan_count);
4717 if (wpa_s->p2p_join_scan_count > P2P_MAX_JOIN_SCAN_ATTEMPTS) {
4718 wpa_printf(MSG_DEBUG, "P2P: Failed to find GO " MACSTR
4719 " for join operationg - stop join attempt",
4720 MAC2STR(wpa_s->pending_join_iface_addr));
4721 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
Dmitry Shmidt04949592012-07-19 12:16:46 -07004722 if (wpa_s->p2p_auto_pd) {
4723 wpa_s->p2p_auto_pd = 0;
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07004724 wpa_msg_global(wpa_s, MSG_INFO,
4725 P2P_EVENT_PROV_DISC_FAILURE
4726 " p2p_dev_addr=" MACSTR " status=N/A",
4727 MAC2STR(wpa_s->pending_join_dev_addr));
Dmitry Shmidt04949592012-07-19 12:16:46 -07004728 return;
4729 }
Dmitry Shmidt9c175262016-03-03 10:20:07 -08004730 wpa_msg_global(wpa_s->p2pdev, MSG_INFO,
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07004731 P2P_EVENT_GROUP_FORMATION_FAILURE);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004732 wpas_notify_p2p_group_formation_failure(wpa_s, "");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004733 }
4734}
4735
4736
Dmitry Shmidt04949592012-07-19 12:16:46 -07004737static int wpas_check_freq_conflict(struct wpa_supplicant *wpa_s, int freq)
4738{
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07004739 int res;
4740 unsigned int num, i;
4741 struct wpa_used_freq_data *freqs;
Dmitry Shmidt04949592012-07-19 12:16:46 -07004742
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07004743 if (wpas_p2p_num_unused_channels(wpa_s) > 0) {
4744 /* Multiple channels are supported and not all are in use */
Dmitry Shmidt04949592012-07-19 12:16:46 -07004745 return 0;
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07004746 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07004747
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07004748 freqs = os_calloc(wpa_s->num_multichan_concurrent,
4749 sizeof(struct wpa_used_freq_data));
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07004750 if (!freqs)
4751 return 1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07004752
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07004753 num = wpas_p2p_valid_oper_freqs(wpa_s, freqs,
4754 wpa_s->num_multichan_concurrent);
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07004755
4756 for (i = 0; i < num; i++) {
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07004757 if (freqs[i].freq == freq) {
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07004758 wpa_printf(MSG_DEBUG, "P2P: Frequency %d MHz in use by another virtual interface and can be used",
4759 freq);
4760 res = 0;
4761 goto exit_free;
Dmitry Shmidt04949592012-07-19 12:16:46 -07004762 }
4763 }
4764
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07004765 wpa_printf(MSG_DEBUG, "P2P: No valid operating frequencies");
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07004766 res = 1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07004767
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07004768exit_free:
4769 os_free(freqs);
4770 return res;
Dmitry Shmidt04949592012-07-19 12:16:46 -07004771}
4772
4773
4774static int wpas_p2p_peer_go(struct wpa_supplicant *wpa_s,
4775 const u8 *peer_dev_addr)
4776{
4777 struct wpa_bss *bss;
4778 int updated;
4779
4780 bss = wpa_bss_get_p2p_dev_addr(wpa_s, peer_dev_addr);
4781 if (bss == NULL)
4782 return -1;
4783 if (bss->last_update_idx < wpa_s->bss_update_idx) {
4784 wpa_printf(MSG_DEBUG, "P2P: Peer BSS entry not updated in the "
4785 "last scan");
4786 return 0;
4787 }
4788
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08004789 updated = os_reltime_before(&wpa_s->p2p_auto_started,
4790 &bss->last_update);
Dmitry Shmidt04949592012-07-19 12:16:46 -07004791 wpa_printf(MSG_DEBUG, "P2P: Current BSS entry for peer updated at "
4792 "%ld.%06ld (%supdated in last scan)",
4793 bss->last_update.sec, bss->last_update.usec,
4794 updated ? "": "not ");
4795
4796 return updated;
4797}
4798
4799
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004800static void wpas_p2p_scan_res_join(struct wpa_supplicant *wpa_s,
4801 struct wpa_scan_results *scan_res)
4802{
Dmitry Shmidt344abd32014-01-14 13:17:00 -08004803 struct wpa_bss *bss = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004804 int freq;
4805 u8 iface_addr[ETH_ALEN];
Dmitry Shmidt04949592012-07-19 12:16:46 -07004806
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004807 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
4808
4809 if (wpa_s->global->p2p_disabled)
4810 return;
4811
Dmitry Shmidt04949592012-07-19 12:16:46 -07004812 wpa_printf(MSG_DEBUG, "P2P: Scan results received (%d BSS) for %sjoin",
4813 scan_res ? (int) scan_res->num : -1,
4814 wpa_s->p2p_auto_join ? "auto_" : "");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004815
4816 if (scan_res)
4817 wpas_p2p_scan_res_handler(wpa_s, scan_res);
4818
Dmitry Shmidt04949592012-07-19 12:16:46 -07004819 if (wpa_s->p2p_auto_pd) {
4820 int join = wpas_p2p_peer_go(wpa_s,
4821 wpa_s->pending_join_dev_addr);
4822 if (join == 0 &&
4823 wpa_s->auto_pd_scan_retry < P2P_AUTO_PD_SCAN_ATTEMPTS) {
4824 wpa_s->auto_pd_scan_retry++;
Dmitry Shmidt444d5672013-04-01 13:08:44 -07004825 bss = wpa_bss_get_bssid_latest(
4826 wpa_s, wpa_s->pending_join_dev_addr);
Dmitry Shmidt04949592012-07-19 12:16:46 -07004827 if (bss) {
4828 freq = bss->freq;
4829 wpa_printf(MSG_DEBUG, "P2P: Scan retry %d for "
4830 "the peer " MACSTR " at %d MHz",
4831 wpa_s->auto_pd_scan_retry,
4832 MAC2STR(wpa_s->
4833 pending_join_dev_addr),
4834 freq);
Dmitry Shmidt344abd32014-01-14 13:17:00 -08004835 wpas_p2p_join_scan_req(wpa_s, freq, NULL, 0);
Dmitry Shmidt04949592012-07-19 12:16:46 -07004836 return;
4837 }
4838 }
4839
4840 if (join < 0)
4841 join = 0;
4842
4843 wpa_s->p2p_auto_pd = 0;
4844 wpa_s->pending_pd_use = join ? AUTO_PD_JOIN : AUTO_PD_GO_NEG;
4845 wpa_printf(MSG_DEBUG, "P2P: Auto PD with " MACSTR " join=%d",
4846 MAC2STR(wpa_s->pending_join_dev_addr), join);
4847 if (p2p_prov_disc_req(wpa_s->global->p2p,
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004848 wpa_s->pending_join_dev_addr, NULL,
Dmitry Shmidt04949592012-07-19 12:16:46 -07004849 wpa_s->pending_pd_config_methods, join,
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08004850 0, wpa_s->user_initiated_pd) < 0) {
Dmitry Shmidt04949592012-07-19 12:16:46 -07004851 wpa_s->p2p_auto_pd = 0;
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07004852 wpa_msg_global(wpa_s, MSG_INFO,
4853 P2P_EVENT_PROV_DISC_FAILURE
4854 " p2p_dev_addr=" MACSTR " status=N/A",
4855 MAC2STR(wpa_s->pending_join_dev_addr));
Dmitry Shmidt04949592012-07-19 12:16:46 -07004856 }
4857 return;
4858 }
4859
4860 if (wpa_s->p2p_auto_join) {
4861 int join = wpas_p2p_peer_go(wpa_s,
4862 wpa_s->pending_join_dev_addr);
4863 if (join < 0) {
4864 wpa_printf(MSG_DEBUG, "P2P: Peer was not found to be "
4865 "running a GO -> use GO Negotiation");
Dmitry Shmidt9c175262016-03-03 10:20:07 -08004866 wpa_msg_global(wpa_s->p2pdev, MSG_INFO,
Dmitry Shmidt7f656022015-02-25 14:36:37 -08004867 P2P_EVENT_FALLBACK_TO_GO_NEG
4868 "reason=peer-not-running-GO");
Dmitry Shmidt04949592012-07-19 12:16:46 -07004869 wpas_p2p_connect(wpa_s, wpa_s->pending_join_dev_addr,
4870 wpa_s->p2p_pin, wpa_s->p2p_wps_method,
4871 wpa_s->p2p_persistent_group, 0, 0, 0,
4872 wpa_s->p2p_go_intent,
4873 wpa_s->p2p_connect_freq,
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08004874 wpa_s->p2p_go_vht_center_freq2,
Dmitry Shmidt04949592012-07-19 12:16:46 -07004875 wpa_s->p2p_persistent_id,
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004876 wpa_s->p2p_pd_before_go_neg,
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004877 wpa_s->p2p_go_ht40,
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08004878 wpa_s->p2p_go_vht,
Dmitry Shmidtde47be72016-01-07 12:52:55 -08004879 wpa_s->p2p_go_max_oper_chwidth,
4880 NULL, 0);
Dmitry Shmidt04949592012-07-19 12:16:46 -07004881 return;
4882 }
4883
4884 wpa_printf(MSG_DEBUG, "P2P: Peer was found running GO%s -> "
4885 "try to join the group", join ? "" :
4886 " in older scan");
Dmitry Shmidt7f656022015-02-25 14:36:37 -08004887 if (!join) {
Dmitry Shmidt9c175262016-03-03 10:20:07 -08004888 wpa_msg_global(wpa_s->p2pdev, MSG_INFO,
Dmitry Shmidt7f656022015-02-25 14:36:37 -08004889 P2P_EVENT_FALLBACK_TO_GO_NEG_ENABLED);
Dmitry Shmidt04949592012-07-19 12:16:46 -07004890 wpa_s->p2p_fallback_to_go_neg = 1;
Dmitry Shmidt7f656022015-02-25 14:36:37 -08004891 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07004892 }
4893
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004894 freq = p2p_get_oper_freq(wpa_s->global->p2p,
4895 wpa_s->pending_join_iface_addr);
4896 if (freq < 0 &&
4897 p2p_get_interface_addr(wpa_s->global->p2p,
4898 wpa_s->pending_join_dev_addr,
4899 iface_addr) == 0 &&
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07004900 os_memcmp(iface_addr, wpa_s->pending_join_dev_addr, ETH_ALEN) != 0
4901 && !wpa_bss_get_bssid(wpa_s, wpa_s->pending_join_iface_addr)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004902 wpa_printf(MSG_DEBUG, "P2P: Overwrite pending interface "
4903 "address for join from " MACSTR " to " MACSTR
4904 " based on newly discovered P2P peer entry",
4905 MAC2STR(wpa_s->pending_join_iface_addr),
4906 MAC2STR(iface_addr));
4907 os_memcpy(wpa_s->pending_join_iface_addr, iface_addr,
4908 ETH_ALEN);
4909
4910 freq = p2p_get_oper_freq(wpa_s->global->p2p,
4911 wpa_s->pending_join_iface_addr);
4912 }
4913 if (freq >= 0) {
4914 wpa_printf(MSG_DEBUG, "P2P: Target GO operating frequency "
4915 "from P2P peer table: %d MHz", freq);
4916 }
Dmitry Shmidt344abd32014-01-14 13:17:00 -08004917 if (wpa_s->p2p_join_ssid_len) {
4918 wpa_printf(MSG_DEBUG, "P2P: Trying to find target GO BSS entry based on BSSID "
4919 MACSTR " and SSID %s",
4920 MAC2STR(wpa_s->pending_join_iface_addr),
4921 wpa_ssid_txt(wpa_s->p2p_join_ssid,
4922 wpa_s->p2p_join_ssid_len));
4923 bss = wpa_bss_get(wpa_s, wpa_s->pending_join_iface_addr,
4924 wpa_s->p2p_join_ssid,
4925 wpa_s->p2p_join_ssid_len);
Dmitry Shmidtde47be72016-01-07 12:52:55 -08004926 } else if (!bss) {
Dmitry Shmidt344abd32014-01-14 13:17:00 -08004927 wpa_printf(MSG_DEBUG, "P2P: Trying to find target GO BSS entry based on BSSID "
4928 MACSTR, MAC2STR(wpa_s->pending_join_iface_addr));
4929 bss = wpa_bss_get_bssid_latest(wpa_s,
4930 wpa_s->pending_join_iface_addr);
4931 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004932 if (bss) {
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07004933 u8 dev_addr[ETH_ALEN];
4934
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004935 freq = bss->freq;
4936 wpa_printf(MSG_DEBUG, "P2P: Target GO operating frequency "
Dmitry Shmidt051af732013-10-22 13:52:46 -07004937 "from BSS table: %d MHz (SSID %s)", freq,
4938 wpa_ssid_txt(bss->ssid, bss->ssid_len));
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07004939 if (p2p_parse_dev_addr((const u8 *) (bss + 1), bss->ie_len,
4940 dev_addr) == 0 &&
4941 os_memcmp(wpa_s->pending_join_dev_addr,
4942 wpa_s->pending_join_iface_addr, ETH_ALEN) == 0 &&
4943 os_memcmp(dev_addr, wpa_s->pending_join_dev_addr,
4944 ETH_ALEN) != 0) {
4945 wpa_printf(MSG_DEBUG,
4946 "P2P: Update target GO device address based on BSS entry: " MACSTR " (was " MACSTR ")",
4947 MAC2STR(dev_addr),
4948 MAC2STR(wpa_s->pending_join_dev_addr));
4949 os_memcpy(wpa_s->pending_join_dev_addr, dev_addr,
4950 ETH_ALEN);
4951 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004952 }
4953 if (freq > 0) {
4954 u16 method;
4955
Dmitry Shmidt04949592012-07-19 12:16:46 -07004956 if (wpas_check_freq_conflict(wpa_s, freq) > 0) {
Dmitry Shmidt9c175262016-03-03 10:20:07 -08004957 wpa_msg_global(wpa_s->p2pdev, MSG_INFO,
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07004958 P2P_EVENT_GROUP_FORMATION_FAILURE
4959 "reason=FREQ_CONFLICT");
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004960 wpas_notify_p2p_group_formation_failure(
4961 wpa_s, "FREQ_CONFLICT");
Dmitry Shmidt04949592012-07-19 12:16:46 -07004962 return;
4963 }
4964
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004965 wpa_printf(MSG_DEBUG, "P2P: Send Provision Discovery Request "
4966 "prior to joining an existing group (GO " MACSTR
4967 " freq=%u MHz)",
4968 MAC2STR(wpa_s->pending_join_dev_addr), freq);
4969 wpa_s->pending_pd_before_join = 1;
4970
4971 switch (wpa_s->pending_join_wps_method) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004972 case WPS_PIN_DISPLAY:
4973 method = WPS_CONFIG_KEYPAD;
4974 break;
4975 case WPS_PIN_KEYPAD:
4976 method = WPS_CONFIG_DISPLAY;
4977 break;
4978 case WPS_PBC:
4979 method = WPS_CONFIG_PUSHBUTTON;
4980 break;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004981 case WPS_P2PS:
4982 method = WPS_CONFIG_P2PS;
4983 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004984 default:
4985 method = 0;
4986 break;
4987 }
4988
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004989 if ((p2p_get_provisioning_info(wpa_s->global->p2p,
4990 wpa_s->pending_join_dev_addr) ==
4991 method)) {
4992 /*
4993 * We have already performed provision discovery for
4994 * joining the group. Proceed directly to join
4995 * operation without duplicated provision discovery. */
4996 wpa_printf(MSG_DEBUG, "P2P: Provision discovery "
4997 "with " MACSTR " already done - proceed to "
4998 "join",
4999 MAC2STR(wpa_s->pending_join_dev_addr));
5000 wpa_s->pending_pd_before_join = 0;
5001 goto start;
5002 }
5003
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005004 if (p2p_prov_disc_req(wpa_s->global->p2p,
Dmitry Shmidt216983b2015-02-06 10:50:36 -08005005 wpa_s->pending_join_dev_addr,
5006 NULL, method, 1,
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08005007 freq, wpa_s->user_initiated_pd) < 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005008 wpa_printf(MSG_DEBUG, "P2P: Failed to send Provision "
5009 "Discovery Request before joining an "
5010 "existing group");
5011 wpa_s->pending_pd_before_join = 0;
5012 goto start;
5013 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005014 return;
5015 }
5016
5017 wpa_printf(MSG_DEBUG, "P2P: Failed to find BSS/GO - try again later");
5018 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
5019 eloop_register_timeout(1, 0, wpas_p2p_join_scan, wpa_s, NULL);
5020 wpas_p2p_check_join_scan_limit(wpa_s);
5021 return;
5022
5023start:
5024 /* Start join operation immediately */
Dmitry Shmidtde47be72016-01-07 12:52:55 -08005025 wpas_p2p_join_start(wpa_s, 0, wpa_s->p2p_join_ssid,
5026 wpa_s->p2p_join_ssid_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005027}
5028
5029
Dmitry Shmidt344abd32014-01-14 13:17:00 -08005030static void wpas_p2p_join_scan_req(struct wpa_supplicant *wpa_s, int freq,
5031 const u8 *ssid, size_t ssid_len)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005032{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005033 int ret;
5034 struct wpa_driver_scan_params params;
5035 struct wpabuf *wps_ie, *ies;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005036 size_t ielen;
Dmitry Shmidt04949592012-07-19 12:16:46 -07005037 int freqs[2] = { 0, 0 };
Dmitry Shmidt9c175262016-03-03 10:20:07 -08005038 unsigned int bands;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005039
5040 os_memset(&params, 0, sizeof(params));
5041
5042 /* P2P Wildcard SSID */
5043 params.num_ssids = 1;
Dmitry Shmidt344abd32014-01-14 13:17:00 -08005044 if (ssid && ssid_len) {
5045 params.ssids[0].ssid = ssid;
5046 params.ssids[0].ssid_len = ssid_len;
5047 os_memcpy(wpa_s->p2p_join_ssid, ssid, ssid_len);
5048 wpa_s->p2p_join_ssid_len = ssid_len;
5049 } else {
5050 params.ssids[0].ssid = (u8 *) P2P_WILDCARD_SSID;
5051 params.ssids[0].ssid_len = P2P_WILDCARD_SSID_LEN;
5052 wpa_s->p2p_join_ssid_len = 0;
5053 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005054
5055 wpa_s->wps->dev.p2p = 1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07005056 wps_ie = wps_build_probe_req_ie(DEV_PW_DEFAULT, &wpa_s->wps->dev,
5057 wpa_s->wps->uuid, WPS_REQ_ENROLLEE, 0,
5058 NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005059 if (wps_ie == NULL) {
5060 wpas_p2p_scan_res_join(wpa_s, NULL);
5061 return;
5062 }
5063
Dmitry Shmidt9e3f8ee2014-01-17 10:52:01 -08005064 if (!freq) {
5065 int oper_freq;
5066 /*
5067 * If freq is not provided, check the operating freq of the GO
5068 * and use a single channel scan on if possible.
5069 */
5070 oper_freq = p2p_get_oper_freq(wpa_s->global->p2p,
5071 wpa_s->pending_join_iface_addr);
5072 if (oper_freq > 0)
5073 freq = oper_freq;
5074 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07005075 if (freq > 0) {
5076 freqs[0] = freq;
5077 params.freqs = freqs;
5078 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005079
Dmitry Shmidt9c175262016-03-03 10:20:07 -08005080 ielen = p2p_scan_ie_buf_len(wpa_s->global->p2p);
5081 ies = wpabuf_alloc(wpabuf_len(wps_ie) + ielen);
5082 if (ies == NULL) {
5083 wpabuf_free(wps_ie);
5084 wpas_p2p_scan_res_join(wpa_s, NULL);
5085 return;
5086 }
5087 wpabuf_put_buf(ies, wps_ie);
5088 wpabuf_free(wps_ie);
5089
5090 bands = wpas_get_bands(wpa_s, freqs);
5091 p2p_scan_ie(wpa_s->global->p2p, ies, NULL, bands);
5092
5093 params.p2p_probe = 1;
5094 params.extra_ies = wpabuf_head(ies);
5095 params.extra_ies_len = wpabuf_len(ies);
5096
Dmitry Shmidtebd93af2017-02-21 13:40:44 -08005097 if (wpa_s->clear_driver_scan_cache) {
5098 wpa_printf(MSG_DEBUG,
5099 "Request driver to clear scan cache due to local BSS flush");
5100 params.only_new_results = 1;
5101 }
5102
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005103 /*
5104 * Run a scan to update BSS table and start Provision Discovery once
5105 * the new scan results become available.
5106 */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005107 ret = wpa_drv_scan(wpa_s, &params);
Dmitry Shmidt444d5672013-04-01 13:08:44 -07005108 if (!ret) {
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005109 os_get_reltime(&wpa_s->scan_trigger_time);
Dmitry Shmidt04949592012-07-19 12:16:46 -07005110 wpa_s->scan_res_handler = wpas_p2p_scan_res_join;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005111 wpa_s->own_scan_requested = 1;
Dmitry Shmidtebd93af2017-02-21 13:40:44 -08005112 wpa_s->clear_driver_scan_cache = 0;
Dmitry Shmidt444d5672013-04-01 13:08:44 -07005113 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005114
5115 wpabuf_free(ies);
5116
5117 if (ret) {
5118 wpa_printf(MSG_DEBUG, "P2P: Failed to start scan for join - "
5119 "try again later");
5120 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
5121 eloop_register_timeout(1, 0, wpas_p2p_join_scan, wpa_s, NULL);
5122 wpas_p2p_check_join_scan_limit(wpa_s);
5123 }
5124}
5125
5126
Dmitry Shmidt04949592012-07-19 12:16:46 -07005127static void wpas_p2p_join_scan(void *eloop_ctx, void *timeout_ctx)
5128{
5129 struct wpa_supplicant *wpa_s = eloop_ctx;
Dmitry Shmidt344abd32014-01-14 13:17:00 -08005130 wpas_p2p_join_scan_req(wpa_s, 0, NULL, 0);
Dmitry Shmidt04949592012-07-19 12:16:46 -07005131}
5132
5133
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005134static int wpas_p2p_join(struct wpa_supplicant *wpa_s, const u8 *iface_addr,
Dmitry Shmidt04949592012-07-19 12:16:46 -07005135 const u8 *dev_addr, enum p2p_wps_method wps_method,
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08005136 int auto_join, int op_freq,
5137 const u8 *ssid, size_t ssid_len)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005138{
5139 wpa_printf(MSG_DEBUG, "P2P: Request to join existing group (iface "
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08005140 MACSTR " dev " MACSTR " op_freq=%d)%s",
5141 MAC2STR(iface_addr), MAC2STR(dev_addr), op_freq,
Dmitry Shmidt04949592012-07-19 12:16:46 -07005142 auto_join ? " (auto_join)" : "");
Dmitry Shmidt344abd32014-01-14 13:17:00 -08005143 if (ssid && ssid_len) {
5144 wpa_printf(MSG_DEBUG, "P2P: Group SSID specified: %s",
5145 wpa_ssid_txt(ssid, ssid_len));
5146 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005147
Dmitry Shmidt04949592012-07-19 12:16:46 -07005148 wpa_s->p2p_auto_pd = 0;
5149 wpa_s->p2p_auto_join = !!auto_join;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005150 os_memcpy(wpa_s->pending_join_iface_addr, iface_addr, ETH_ALEN);
5151 os_memcpy(wpa_s->pending_join_dev_addr, dev_addr, ETH_ALEN);
5152 wpa_s->pending_join_wps_method = wps_method;
5153
5154 /* Make sure we are not running find during connection establishment */
5155 wpas_p2p_stop_find(wpa_s);
5156
5157 wpa_s->p2p_join_scan_count = 0;
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08005158 wpas_p2p_join_scan_req(wpa_s, op_freq, ssid, ssid_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005159 return 0;
5160}
5161
5162
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08005163static int wpas_p2p_join_start(struct wpa_supplicant *wpa_s, int freq,
5164 const u8 *ssid, size_t ssid_len)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005165{
5166 struct wpa_supplicant *group;
5167 struct p2p_go_neg_results res;
Dmitry Shmidt04949592012-07-19 12:16:46 -07005168 struct wpa_bss *bss;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005169
5170 group = wpas_p2p_get_group_iface(wpa_s, 0, 0);
5171 if (group == NULL)
5172 return -1;
5173 if (group != wpa_s) {
5174 os_memcpy(group->p2p_pin, wpa_s->p2p_pin,
5175 sizeof(group->p2p_pin));
5176 group->p2p_wps_method = wpa_s->p2p_wps_method;
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08005177 } else {
5178 /*
5179 * Need to mark the current interface for p2p_group_formation
5180 * when a separate group interface is not used. This is needed
5181 * to allow p2p_cancel stop a pending p2p_connect-join.
5182 * wpas_p2p_init_group_interface() addresses this for the case
5183 * where a separate group interface is used.
5184 */
5185 wpa_s->global->p2p_group_formation = wpa_s;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005186 }
5187
5188 group->p2p_in_provisioning = 1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07005189 group->p2p_fallback_to_go_neg = wpa_s->p2p_fallback_to_go_neg;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005190
5191 os_memset(&res, 0, sizeof(res));
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08005192 os_memcpy(res.peer_device_addr, wpa_s->pending_join_dev_addr, ETH_ALEN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005193 os_memcpy(res.peer_interface_addr, wpa_s->pending_join_iface_addr,
5194 ETH_ALEN);
5195 res.wps_method = wpa_s->pending_join_wps_method;
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08005196 if (freq && ssid && ssid_len) {
5197 res.freq = freq;
5198 res.ssid_len = ssid_len;
5199 os_memcpy(res.ssid, ssid, ssid_len);
5200 } else {
Dmitry Shmidtde47be72016-01-07 12:52:55 -08005201 if (ssid && ssid_len) {
5202 bss = wpa_bss_get(wpa_s, wpa_s->pending_join_iface_addr,
5203 ssid, ssid_len);
5204 } else {
5205 bss = wpa_bss_get_bssid_latest(
5206 wpa_s, wpa_s->pending_join_iface_addr);
5207 }
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08005208 if (bss) {
5209 res.freq = bss->freq;
5210 res.ssid_len = bss->ssid_len;
5211 os_memcpy(res.ssid, bss->ssid, bss->ssid_len);
5212 wpa_printf(MSG_DEBUG, "P2P: Join target GO operating frequency from BSS table: %d MHz (SSID %s)",
5213 bss->freq,
5214 wpa_ssid_txt(bss->ssid, bss->ssid_len));
Dmitry Shmidtde47be72016-01-07 12:52:55 -08005215 } else if (ssid && ssid_len) {
5216 res.ssid_len = ssid_len;
5217 os_memcpy(res.ssid, ssid, ssid_len);
5218 wpa_printf(MSG_DEBUG, "P2P: Join target GO (SSID %s)",
5219 wpa_ssid_txt(ssid, ssid_len));
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08005220 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07005221 }
5222
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08005223 if (wpa_s->off_channel_freq || wpa_s->roc_waiting_drv_freq) {
5224 wpa_printf(MSG_DEBUG, "P2P: Cancel remain-on-channel prior to "
5225 "starting client");
5226 wpa_drv_cancel_remain_on_channel(wpa_s);
5227 wpa_s->off_channel_freq = 0;
5228 wpa_s->roc_waiting_drv_freq = 0;
5229 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005230 wpas_start_wps_enrollee(group, &res);
5231
5232 /*
5233 * Allow a longer timeout for join-a-running-group than normal 15
5234 * second group formation timeout since the GO may not have authorized
5235 * our connection yet.
5236 */
5237 eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s, NULL);
5238 eloop_register_timeout(60, 0, wpas_p2p_group_formation_timeout,
5239 wpa_s, NULL);
5240
5241 return 0;
5242}
5243
5244
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08005245static int wpas_p2p_setup_freqs(struct wpa_supplicant *wpa_s, int freq,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005246 int *force_freq, int *pref_freq, int go,
5247 unsigned int *pref_freq_list,
5248 unsigned int *num_pref_freq)
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08005249{
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07005250 struct wpa_used_freq_data *freqs;
5251 int res, best_freq, num_unused;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005252 unsigned int freq_in_use = 0, num, i, max_pref_freq;
5253
5254 max_pref_freq = *num_pref_freq;
5255 *num_pref_freq = 0;
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07005256
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07005257 freqs = os_calloc(wpa_s->num_multichan_concurrent,
5258 sizeof(struct wpa_used_freq_data));
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07005259 if (!freqs)
5260 return -1;
5261
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07005262 num = wpas_p2p_valid_oper_freqs(wpa_s, freqs,
5263 wpa_s->num_multichan_concurrent);
5264
5265 /*
5266 * It is possible that the total number of used frequencies is bigger
5267 * than the number of frequencies used for P2P, so get the system wide
5268 * number of unused frequencies.
5269 */
5270 num_unused = wpas_p2p_num_unused_channels(wpa_s);
5271
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07005272 wpa_printf(MSG_DEBUG,
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07005273 "P2P: Setup freqs: freq=%d num_MCC=%d shared_freqs=%u num_unused=%d",
5274 freq, wpa_s->num_multichan_concurrent, num, num_unused);
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07005275
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08005276 if (freq > 0) {
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07005277 int ret;
5278 if (go)
5279 ret = p2p_supported_freq(wpa_s->global->p2p, freq);
5280 else
5281 ret = p2p_supported_freq_cli(wpa_s->global->p2p, freq);
5282 if (!ret) {
Dmitry Shmidt203eadb2015-03-05 14:16:04 -08005283 if ((wpa_s->drv_flags & WPA_DRIVER_FLAGS_DFS_OFFLOAD) &&
Roshan Pius3a1667e2018-07-03 15:17:14 -07005284 ieee80211_is_dfs(freq, wpa_s->hw.modes,
5285 wpa_s->hw.num_modes)) {
Dmitry Shmidt203eadb2015-03-05 14:16:04 -08005286 /*
5287 * If freq is a DFS channel and DFS is offloaded
5288 * to the driver, allow P2P GO to use it.
5289 */
5290 wpa_printf(MSG_DEBUG,
5291 "P2P: The forced channel for GO (%u MHz) is DFS, and DFS is offloaded to the driver",
5292 freq);
5293 } else {
5294 wpa_printf(MSG_DEBUG,
5295 "P2P: The forced channel (%u MHz) is not supported for P2P uses",
5296 freq);
5297 res = -3;
5298 goto exit_free;
5299 }
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08005300 }
5301
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07005302 for (i = 0; i < num; i++) {
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07005303 if (freqs[i].freq == freq)
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07005304 freq_in_use = 1;
5305 }
5306
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07005307 if (num_unused <= 0 && !freq_in_use) {
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07005308 wpa_printf(MSG_DEBUG, "P2P: Cannot start P2P group on %u MHz as there are no available channels",
5309 freq);
5310 res = -2;
5311 goto exit_free;
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08005312 }
5313 wpa_printf(MSG_DEBUG, "P2P: Trying to force us to use the "
5314 "requested channel (%u MHz)", freq);
5315 *force_freq = freq;
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07005316 goto exit_ok;
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08005317 }
5318
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07005319 best_freq = wpas_p2p_pick_best_used_freq(wpa_s, freqs, num);
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07005320
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005321 if (!wpa_s->conf->num_p2p_pref_chan && *pref_freq == 0) {
5322 enum wpa_driver_if_type iface_type;
5323
5324 if (go)
5325 iface_type = WPA_IF_P2P_GO;
5326 else
5327 iface_type = WPA_IF_P2P_CLIENT;
5328
5329 wpa_printf(MSG_DEBUG, "P2P: best_freq=%d, go=%d",
5330 best_freq, go);
5331
5332 res = wpa_drv_get_pref_freq_list(wpa_s, iface_type,
5333 &max_pref_freq,
5334 pref_freq_list);
5335 if (!res && max_pref_freq > 0) {
5336 *num_pref_freq = max_pref_freq;
5337 i = 0;
Dmitry Shmidt29333592017-01-09 12:27:11 -08005338 while (i < *num_pref_freq &&
5339 (!p2p_supported_freq(wpa_s->global->p2p,
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08005340 pref_freq_list[i]) ||
5341 wpas_p2p_disallowed_freq(wpa_s->global,
Dmitry Shmidt29333592017-01-09 12:27:11 -08005342 pref_freq_list[i]))) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005343 wpa_printf(MSG_DEBUG,
5344 "P2P: preferred_freq_list[%d]=%d is disallowed",
5345 i, pref_freq_list[i]);
5346 i++;
5347 }
5348 if (i != *num_pref_freq) {
5349 best_freq = pref_freq_list[i];
5350 wpa_printf(MSG_DEBUG,
5351 "P2P: Using preferred_freq_list[%d]=%d",
5352 i, best_freq);
5353 } else {
5354 wpa_printf(MSG_DEBUG,
5355 "P2P: All driver preferred frequencies are disallowed for P2P use");
5356 *num_pref_freq = 0;
5357 }
5358 } else {
5359 wpa_printf(MSG_DEBUG,
5360 "P2P: No preferred frequency list available");
5361 }
5362 }
5363
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07005364 /* We have a candidate frequency to use */
5365 if (best_freq > 0) {
5366 if (*pref_freq == 0 && num_unused > 0) {
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08005367 wpa_printf(MSG_DEBUG, "P2P: Try to prefer a frequency (%u MHz) we are already using",
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07005368 best_freq);
5369 *pref_freq = best_freq;
Dmitry Shmidt51a47d52013-09-10 10:52:57 -07005370 } else {
5371 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 -07005372 best_freq);
5373 *force_freq = best_freq;
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07005374 }
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07005375 } else if (num_unused > 0) {
5376 wpa_printf(MSG_DEBUG,
5377 "P2P: Current operating channels are not available for P2P. Try to use another channel");
5378 *force_freq = 0;
5379 } else {
5380 wpa_printf(MSG_DEBUG,
5381 "P2P: All channels are in use and none of them are P2P enabled. Cannot start P2P group");
5382 res = -2;
5383 goto exit_free;
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07005384 }
5385
5386exit_ok:
5387 res = 0;
5388exit_free:
5389 os_free(freqs);
5390 return res;
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08005391}
5392
5393
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005394/**
5395 * wpas_p2p_connect - Request P2P Group Formation to be started
5396 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
5397 * @peer_addr: Address of the peer P2P Device
5398 * @pin: PIN to use during provisioning or %NULL to indicate PBC mode
5399 * @persistent_group: Whether to create a persistent group
Dmitry Shmidt04949592012-07-19 12:16:46 -07005400 * @auto_join: Whether to select join vs. GO Negotiation automatically
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005401 * @join: Whether to join an existing group (as a client) instead of starting
5402 * Group Owner negotiation; @peer_addr is BSSID in that case
5403 * @auth: Whether to only authorize the connection instead of doing that and
5404 * initiating Group Owner negotiation
5405 * @go_intent: GO Intent or -1 to use default
5406 * @freq: Frequency for the group or 0 for auto-selection
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08005407 * @freq2: Center frequency of segment 1 for the GO operating in VHT 80P80 mode
Dmitry Shmidt04949592012-07-19 12:16:46 -07005408 * @persistent_id: Persistent group credentials to use for forcing GO
5409 * parameters or -1 to generate new values (SSID/passphrase)
5410 * @pd: Whether to send Provision Discovery prior to GO Negotiation as an
5411 * interoperability workaround when initiating group formation
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005412 * @ht40: Start GO with 40 MHz channel width
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07005413 * @vht: Start GO with VHT support
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08005414 * @vht_chwidth: Channel width supported by GO operating with VHT support
5415 * (VHT_CHANWIDTH_*).
Dmitry Shmidtde47be72016-01-07 12:52:55 -08005416 * @group_ssid: Specific Group SSID for join or %NULL if not set
5417 * @group_ssid_len: Length of @group_ssid in octets
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005418 * Returns: 0 or new PIN (if pin was %NULL) on success, -1 on unspecified
5419 * failure, -2 on failure due to channel not currently available,
5420 * -3 if forced channel is not supported
5421 */
5422int wpas_p2p_connect(struct wpa_supplicant *wpa_s, const u8 *peer_addr,
5423 const char *pin, enum p2p_wps_method wps_method,
Dmitry Shmidt04949592012-07-19 12:16:46 -07005424 int persistent_group, int auto_join, int join, int auth,
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08005425 int go_intent, int freq, unsigned int vht_center_freq2,
5426 int persistent_id, int pd, int ht40, int vht,
Dmitry Shmidtde47be72016-01-07 12:52:55 -08005427 unsigned int vht_chwidth, const u8 *group_ssid,
5428 size_t group_ssid_len)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005429{
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07005430 int force_freq = 0, pref_freq = 0;
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08005431 int ret = 0, res;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005432 enum wpa_driver_if_type iftype;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005433 const u8 *if_addr;
Dmitry Shmidt04949592012-07-19 12:16:46 -07005434 struct wpa_ssid *ssid = NULL;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005435 unsigned int pref_freq_list[P2P_MAX_PREF_CHANNELS], size;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005436
5437 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
5438 return -1;
5439
Dmitry Shmidt04949592012-07-19 12:16:46 -07005440 if (persistent_id >= 0) {
5441 ssid = wpa_config_get_network(wpa_s->conf, persistent_id);
5442 if (ssid == NULL || ssid->disabled != 2 ||
5443 ssid->mode != WPAS_MODE_P2P_GO)
5444 return -1;
5445 }
5446
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07005447 os_free(wpa_s->global->add_psk);
5448 wpa_s->global->add_psk = NULL;
5449
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005450 wpa_s->global->p2p_fail_on_wps_complete = 0;
Dmitry Shmidt216983b2015-02-06 10:50:36 -08005451 wpa_s->global->pending_p2ps_group = 0;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005452 wpa_s->global->pending_p2ps_group_freq = 0;
5453 wpa_s->p2ps_method_config_any = 0;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005454
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005455 if (go_intent < 0)
5456 go_intent = wpa_s->conf->p2p_go_intent;
5457
5458 if (!auth)
5459 wpa_s->p2p_long_listen = 0;
5460
5461 wpa_s->p2p_wps_method = wps_method;
Dmitry Shmidt04949592012-07-19 12:16:46 -07005462 wpa_s->p2p_persistent_group = !!persistent_group;
5463 wpa_s->p2p_persistent_id = persistent_id;
5464 wpa_s->p2p_go_intent = go_intent;
5465 wpa_s->p2p_connect_freq = freq;
5466 wpa_s->p2p_fallback_to_go_neg = 0;
5467 wpa_s->p2p_pd_before_go_neg = !!pd;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005468 wpa_s->p2p_go_ht40 = !!ht40;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07005469 wpa_s->p2p_go_vht = !!vht;
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08005470 wpa_s->p2p_go_vht_center_freq2 = vht_center_freq2;
5471 wpa_s->p2p_go_max_oper_chwidth = vht_chwidth;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005472
5473 if (pin)
5474 os_strlcpy(wpa_s->p2p_pin, pin, sizeof(wpa_s->p2p_pin));
5475 else if (wps_method == WPS_PIN_DISPLAY) {
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08005476 if (wps_generate_pin((unsigned int *) &ret) < 0)
5477 return -1;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005478 res = os_snprintf(wpa_s->p2p_pin, sizeof(wpa_s->p2p_pin),
5479 "%08d", ret);
5480 if (os_snprintf_error(sizeof(wpa_s->p2p_pin), res))
5481 wpa_s->p2p_pin[sizeof(wpa_s->p2p_pin) - 1] = '\0';
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005482 wpa_printf(MSG_DEBUG, "P2P: Randomly generated PIN: %s",
5483 wpa_s->p2p_pin);
Dmitry Shmidt849734c2016-05-27 09:59:01 -07005484 } else if (wps_method == WPS_P2PS) {
5485 /* Force the P2Ps default PIN to be used */
5486 os_strlcpy(wpa_s->p2p_pin, "12345670", sizeof(wpa_s->p2p_pin));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005487 } else
5488 wpa_s->p2p_pin[0] = '\0';
5489
Dmitry Shmidt04949592012-07-19 12:16:46 -07005490 if (join || auto_join) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005491 u8 iface_addr[ETH_ALEN], dev_addr[ETH_ALEN];
5492 if (auth) {
5493 wpa_printf(MSG_DEBUG, "P2P: Authorize invitation to "
5494 "connect a running group from " MACSTR,
5495 MAC2STR(peer_addr));
5496 os_memcpy(wpa_s->p2p_auth_invite, peer_addr, ETH_ALEN);
5497 return ret;
5498 }
5499 os_memcpy(dev_addr, peer_addr, ETH_ALEN);
5500 if (p2p_get_interface_addr(wpa_s->global->p2p, peer_addr,
5501 iface_addr) < 0) {
5502 os_memcpy(iface_addr, peer_addr, ETH_ALEN);
5503 p2p_get_dev_addr(wpa_s->global->p2p, peer_addr,
5504 dev_addr);
5505 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07005506 if (auto_join) {
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005507 os_get_reltime(&wpa_s->p2p_auto_started);
Dmitry Shmidt04949592012-07-19 12:16:46 -07005508 wpa_printf(MSG_DEBUG, "P2P: Auto join started at "
5509 "%ld.%06ld",
5510 wpa_s->p2p_auto_started.sec,
5511 wpa_s->p2p_auto_started.usec);
5512 }
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08005513 wpa_s->user_initiated_pd = 1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07005514 if (wpas_p2p_join(wpa_s, iface_addr, dev_addr, wps_method,
Dmitry Shmidtde47be72016-01-07 12:52:55 -08005515 auto_join, freq,
5516 group_ssid, group_ssid_len) < 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005517 return -1;
5518 return ret;
5519 }
5520
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005521 size = P2P_MAX_PREF_CHANNELS;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07005522 res = wpas_p2p_setup_freqs(wpa_s, freq, &force_freq, &pref_freq,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005523 go_intent == 15, pref_freq_list, &size);
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08005524 if (res)
5525 return res;
Dmitry Shmidta0d265f2013-11-19 13:13:41 -08005526 wpas_p2p_set_own_freq_preference(wpa_s,
5527 force_freq ? force_freq : pref_freq);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005528
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005529 p2p_set_own_pref_freq_list(wpa_s->global->p2p, pref_freq_list, size);
5530
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005531 wpa_s->create_p2p_iface = wpas_p2p_create_iface(wpa_s);
5532
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005533 if (wpa_s->create_p2p_iface) {
5534 /* Prepare to add a new interface for the group */
5535 iftype = WPA_IF_P2P_GROUP;
5536 if (go_intent == 15)
5537 iftype = WPA_IF_P2P_GO;
5538 if (wpas_p2p_add_group_interface(wpa_s, iftype) < 0) {
5539 wpa_printf(MSG_ERROR, "P2P: Failed to allocate a new "
5540 "interface for the group");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005541 return -1;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005542 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005543
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005544 if_addr = wpa_s->pending_interface_addr;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005545 } else {
Dmitry Shmidt9c175262016-03-03 10:20:07 -08005546 if (wpa_s->p2p_mgmt)
5547 if_addr = wpa_s->parent->own_addr;
5548 else
5549 if_addr = wpa_s->own_addr;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005550 os_memset(wpa_s->go_dev_addr, 0, ETH_ALEN);
5551 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005552
5553 if (auth) {
5554 if (wpas_p2p_auth_go_neg(wpa_s, peer_addr, wps_method,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005555 go_intent, if_addr,
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08005556 force_freq, persistent_group, ssid,
5557 pref_freq) < 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005558 return -1;
5559 return ret;
5560 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005561
5562 if (wpas_p2p_start_go_neg(wpa_s, peer_addr, wps_method,
5563 go_intent, if_addr, force_freq,
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08005564 persistent_group, ssid, pref_freq) < 0) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005565 if (wpa_s->create_p2p_iface)
5566 wpas_p2p_remove_pending_group_interface(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005567 return -1;
5568 }
5569 return ret;
5570}
5571
5572
5573/**
5574 * wpas_p2p_remain_on_channel_cb - Indication of remain-on-channel start
5575 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
5576 * @freq: Frequency of the channel in MHz
5577 * @duration: Duration of the stay on the channel in milliseconds
5578 *
5579 * This callback is called when the driver indicates that it has started the
5580 * requested remain-on-channel duration.
5581 */
5582void wpas_p2p_remain_on_channel_cb(struct wpa_supplicant *wpa_s,
5583 unsigned int freq, unsigned int duration)
5584{
5585 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
5586 return;
Dmitry Shmidt216983b2015-02-06 10:50:36 -08005587 wpa_printf(MSG_DEBUG, "P2P: remain-on-channel callback (off_channel_freq=%u pending_listen_freq=%d roc_waiting_drv_freq=%d freq=%u duration=%u)",
5588 wpa_s->off_channel_freq, wpa_s->pending_listen_freq,
5589 wpa_s->roc_waiting_drv_freq, freq, duration);
5590 if (wpa_s->off_channel_freq &&
5591 wpa_s->off_channel_freq == wpa_s->pending_listen_freq) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005592 p2p_listen_cb(wpa_s->global->p2p, wpa_s->pending_listen_freq,
5593 wpa_s->pending_listen_duration);
5594 wpa_s->pending_listen_freq = 0;
Dmitry Shmidt7f93d6f2014-02-21 11:22:49 -08005595 } else {
5596 wpa_printf(MSG_DEBUG, "P2P: Ignore remain-on-channel callback (off_channel_freq=%u pending_listen_freq=%d freq=%u duration=%u)",
5597 wpa_s->off_channel_freq, wpa_s->pending_listen_freq,
5598 freq, duration);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005599 }
5600}
5601
5602
Jithu Jance57cea1a2014-08-20 10:22:04 -07005603int wpas_p2p_listen_start(struct wpa_supplicant *wpa_s, unsigned int timeout)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005604{
5605 /* Limit maximum Listen state time based on driver limitation. */
5606 if (timeout > wpa_s->max_remain_on_chan)
5607 timeout = wpa_s->max_remain_on_chan;
5608
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005609 return p2p_listen(wpa_s->global->p2p, timeout);
5610}
5611
5612
5613/**
5614 * wpas_p2p_cancel_remain_on_channel_cb - Remain-on-channel timeout
5615 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
5616 * @freq: Frequency of the channel in MHz
5617 *
5618 * This callback is called when the driver indicates that a remain-on-channel
5619 * operation has been completed, i.e., the duration on the requested channel
5620 * has timed out.
5621 */
5622void wpas_p2p_cancel_remain_on_channel_cb(struct wpa_supplicant *wpa_s,
5623 unsigned int freq)
5624{
5625 wpa_printf(MSG_DEBUG, "P2P: Cancel remain-on-channel callback "
5626 "(p2p_long_listen=%d ms pending_action_tx=%p)",
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08005627 wpa_s->p2p_long_listen, offchannel_pending_action_tx(wpa_s));
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005628 wpas_p2p_listen_work_done(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005629 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
5630 return;
Jithu Jance57cea1a2014-08-20 10:22:04 -07005631 if (wpa_s->p2p_long_listen > 0)
5632 wpa_s->p2p_long_listen -= wpa_s->max_remain_on_chan;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005633 if (p2p_listen_end(wpa_s->global->p2p, freq) > 0)
5634 return; /* P2P module started a new operation */
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08005635 if (offchannel_pending_action_tx(wpa_s))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005636 return;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005637 if (wpa_s->p2p_long_listen > 0) {
5638 wpa_printf(MSG_DEBUG, "P2P: Continuing long Listen state");
5639 wpas_p2p_listen_start(wpa_s, wpa_s->p2p_long_listen);
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07005640 } else {
5641 /*
5642 * When listen duration is over, stop listen & update p2p_state
5643 * to IDLE.
5644 */
5645 p2p_stop_listen(wpa_s->global->p2p);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005646 }
5647}
5648
5649
5650/**
5651 * wpas_p2p_group_remove - Remove a P2P group
5652 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
5653 * @ifname: Network interface name of the group interface or "*" to remove all
5654 * groups
5655 * Returns: 0 on success, -1 on failure
5656 *
5657 * This function is used to remove a P2P group. This can be used to disconnect
5658 * from a group in which the local end is a P2P Client or to end a P2P Group in
5659 * case the local end is the Group Owner. If a virtual network interface was
5660 * created for this group, that interface will be removed. Otherwise, only the
5661 * configured P2P group network will be removed from the interface.
5662 */
5663int wpas_p2p_group_remove(struct wpa_supplicant *wpa_s, const char *ifname)
5664{
5665 struct wpa_global *global = wpa_s->global;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005666 struct wpa_supplicant *calling_wpa_s = wpa_s;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005667
5668 if (os_strcmp(ifname, "*") == 0) {
5669 struct wpa_supplicant *prev;
5670 wpa_s = global->ifaces;
5671 while (wpa_s) {
5672 prev = wpa_s;
5673 wpa_s = wpa_s->next;
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07005674 if (prev->p2p_group_interface !=
5675 NOT_P2P_GROUP_INTERFACE ||
5676 (prev->current_ssid &&
5677 prev->current_ssid->p2p_group))
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005678 wpas_p2p_disconnect_safely(prev, calling_wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005679 }
5680 return 0;
5681 }
5682
5683 for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
5684 if (os_strcmp(wpa_s->ifname, ifname) == 0)
5685 break;
5686 }
5687
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005688 return wpas_p2p_disconnect_safely(wpa_s, calling_wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005689}
5690
5691
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07005692static int wpas_p2p_select_go_freq(struct wpa_supplicant *wpa_s, int freq)
5693{
5694 unsigned int r;
5695
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005696 if (!wpa_s->conf->num_p2p_pref_chan && !freq) {
5697 unsigned int i, size = P2P_MAX_PREF_CHANNELS;
5698 unsigned int pref_freq_list[P2P_MAX_PREF_CHANNELS];
5699 int res;
5700
5701 res = wpa_drv_get_pref_freq_list(wpa_s, WPA_IF_P2P_GO,
5702 &size, pref_freq_list);
5703 if (!res && size > 0) {
5704 i = 0;
Dmitry Shmidt29333592017-01-09 12:27:11 -08005705 while (i < size &&
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07005706 (!p2p_supported_freq(wpa_s->global->p2p,
5707 pref_freq_list[i]) ||
5708 wpas_p2p_disallowed_freq(wpa_s->global,
5709 pref_freq_list[i]))) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005710 wpa_printf(MSG_DEBUG,
5711 "P2P: preferred_freq_list[%d]=%d is disallowed",
5712 i, pref_freq_list[i]);
5713 i++;
5714 }
5715 if (i != size) {
5716 freq = pref_freq_list[i];
5717 wpa_printf(MSG_DEBUG,
5718 "P2P: Using preferred_freq_list[%d]=%d",
5719 i, freq);
5720 } else {
5721 wpa_printf(MSG_DEBUG,
5722 "P2P: All driver preferred frequencies are disallowed for P2P use");
5723 }
5724 } else {
5725 wpa_printf(MSG_DEBUG,
5726 "P2P: No preferred frequency list available");
5727 }
5728 }
5729
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07005730 if (freq == 2) {
5731 wpa_printf(MSG_DEBUG, "P2P: Request to start GO on 2.4 GHz "
5732 "band");
5733 if (wpa_s->best_24_freq > 0 &&
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07005734 p2p_supported_freq_go(wpa_s->global->p2p,
5735 wpa_s->best_24_freq)) {
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07005736 freq = wpa_s->best_24_freq;
5737 wpa_printf(MSG_DEBUG, "P2P: Use best 2.4 GHz band "
5738 "channel: %d MHz", freq);
5739 } else {
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07005740 if (os_get_random((u8 *) &r, sizeof(r)) < 0)
5741 return -1;
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07005742 freq = 2412 + (r % 3) * 25;
5743 wpa_printf(MSG_DEBUG, "P2P: Use random 2.4 GHz band "
5744 "channel: %d MHz", freq);
5745 }
5746 }
5747
5748 if (freq == 5) {
5749 wpa_printf(MSG_DEBUG, "P2P: Request to start GO on 5 GHz "
5750 "band");
5751 if (wpa_s->best_5_freq > 0 &&
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07005752 p2p_supported_freq_go(wpa_s->global->p2p,
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07005753 wpa_s->best_5_freq)) {
5754 freq = wpa_s->best_5_freq;
5755 wpa_printf(MSG_DEBUG, "P2P: Use best 5 GHz band "
5756 "channel: %d MHz", freq);
5757 } else {
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07005758 if (os_get_random((u8 *) &r, sizeof(r)) < 0)
5759 return -1;
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07005760 freq = 5180 + (r % 4) * 20;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07005761 if (!p2p_supported_freq_go(wpa_s->global->p2p, freq)) {
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07005762 wpa_printf(MSG_DEBUG, "P2P: Could not select "
5763 "5 GHz channel for P2P group");
5764 return -1;
5765 }
5766 wpa_printf(MSG_DEBUG, "P2P: Use random 5 GHz band "
5767 "channel: %d MHz", freq);
5768 }
5769 }
5770
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07005771 if (freq > 0 && !p2p_supported_freq_go(wpa_s->global->p2p, freq)) {
Dmitry Shmidt203eadb2015-03-05 14:16:04 -08005772 if ((wpa_s->drv_flags & WPA_DRIVER_FLAGS_DFS_OFFLOAD) &&
Roshan Pius3a1667e2018-07-03 15:17:14 -07005773 ieee80211_is_dfs(freq, wpa_s->hw.modes,
5774 wpa_s->hw.num_modes)) {
Dmitry Shmidt203eadb2015-03-05 14:16:04 -08005775 /*
5776 * If freq is a DFS channel and DFS is offloaded to the
5777 * driver, allow P2P GO to use it.
5778 */
5779 wpa_printf(MSG_DEBUG, "P2P: "
5780 "%s: The forced channel for GO (%u MHz) is DFS, and DFS is offloaded",
5781 __func__, freq);
5782 return freq;
5783 }
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07005784 wpa_printf(MSG_DEBUG, "P2P: The forced channel for GO "
5785 "(%u MHz) is not supported for P2P uses",
5786 freq);
5787 return -1;
5788 }
5789
5790 return freq;
5791}
5792
5793
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005794static int wpas_p2p_supported_freq_go(struct wpa_supplicant *wpa_s,
5795 const struct p2p_channels *channels,
5796 int freq)
5797{
5798 if (!wpas_p2p_disallowed_freq(wpa_s->global, freq) &&
5799 p2p_supported_freq_go(wpa_s->global->p2p, freq) &&
5800 freq_included(wpa_s, channels, freq))
5801 return 1;
5802 return 0;
5803}
5804
5805
5806static void wpas_p2p_select_go_freq_no_pref(struct wpa_supplicant *wpa_s,
5807 struct p2p_go_neg_results *params,
5808 const struct p2p_channels *channels)
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07005809{
5810 unsigned int i, r;
5811
Dmitry Shmidt203eadb2015-03-05 14:16:04 -08005812 /* try all channels in operating class 115 */
5813 for (i = 0; i < 4; i++) {
5814 params->freq = 5180 + i * 20;
5815 if (!wpas_p2p_disallowed_freq(wpa_s->global, params->freq) &&
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005816 freq_included(wpa_s, channels, params->freq) &&
Dmitry Shmidt203eadb2015-03-05 14:16:04 -08005817 p2p_supported_freq(wpa_s->global->p2p, params->freq))
5818 goto out;
5819 }
5820
5821 /* try all channels in operating class 124 */
5822 for (i = 0; i < 4; i++) {
5823 params->freq = 5745 + i * 20;
5824 if (!wpas_p2p_disallowed_freq(wpa_s->global, params->freq) &&
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005825 freq_included(wpa_s, channels, params->freq) &&
Dmitry Shmidt203eadb2015-03-05 14:16:04 -08005826 p2p_supported_freq(wpa_s->global->p2p, params->freq))
5827 goto out;
5828 }
5829
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07005830 /* try social channel class 180 channel 2 */
5831 params->freq = 58320 + 1 * 2160;
5832 if (!wpas_p2p_disallowed_freq(wpa_s->global, params->freq) &&
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005833 freq_included(wpa_s, channels, params->freq) &&
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07005834 p2p_supported_freq(wpa_s->global->p2p, params->freq))
5835 goto out;
5836
5837 /* try all channels in reg. class 180 */
5838 for (i = 0; i < 4; i++) {
5839 params->freq = 58320 + i * 2160;
5840 if (!wpas_p2p_disallowed_freq(wpa_s->global, params->freq) &&
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005841 freq_included(wpa_s, channels, params->freq) &&
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07005842 p2p_supported_freq(wpa_s->global->p2p, params->freq))
5843 goto out;
5844 }
5845
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07005846 /* try some random selection of the social channels */
5847 if (os_get_random((u8 *) &r, sizeof(r)) < 0)
5848 return;
5849
5850 for (i = 0; i < 3; i++) {
5851 params->freq = 2412 + ((r + i) % 3) * 25;
5852 if (wpas_p2p_supported_freq_go(wpa_s, channels, params->freq))
5853 goto out;
5854 }
5855
5856 /* try all other channels in operating class 81 */
5857 for (i = 0; i < 11; i++) {
5858 params->freq = 2412 + i * 5;
5859
5860 /* skip social channels; covered in the previous loop */
5861 if (params->freq == 2412 ||
5862 params->freq == 2437 ||
5863 params->freq == 2462)
5864 continue;
5865
5866 if (wpas_p2p_supported_freq_go(wpa_s, channels, params->freq))
5867 goto out;
5868 }
5869
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005870 params->freq = 0;
Dmitry Shmidt203eadb2015-03-05 14:16:04 -08005871 wpa_printf(MSG_DEBUG, "P2P: No 2.4, 5, or 60 GHz channel allowed");
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005872 return;
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07005873out:
5874 wpa_printf(MSG_DEBUG, "P2P: Set GO freq %d MHz (no preference known)",
5875 params->freq);
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07005876}
5877
5878
Roshan Pius3a1667e2018-07-03 15:17:14 -07005879static int wpas_same_band(int freq1, int freq2)
5880{
5881 enum hostapd_hw_mode mode1, mode2;
5882 u8 chan1, chan2;
5883
5884 mode1 = ieee80211_freq_to_chan(freq1, &chan1);
5885 mode2 = ieee80211_freq_to_chan(freq2, &chan2);
5886 if (mode1 == NUM_HOSTAPD_MODES)
5887 return 0;
5888 return mode1 == mode2;
5889}
5890
5891
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005892static int wpas_p2p_init_go_params(struct wpa_supplicant *wpa_s,
5893 struct p2p_go_neg_results *params,
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08005894 int freq, int vht_center_freq2, int ht40,
5895 int vht, int max_oper_chwidth,
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08005896 const struct p2p_channels *channels)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005897{
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07005898 struct wpa_used_freq_data *freqs;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005899 unsigned int cand;
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07005900 unsigned int num, i;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005901 int ignore_no_freqs = 0;
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08005902 int unused_channels = wpas_p2p_num_unused_channels(wpa_s) > 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005903
5904 os_memset(params, 0, sizeof(*params));
5905 params->role_go = 1;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005906 params->ht40 = ht40;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07005907 params->vht = vht;
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08005908 params->max_oper_chwidth = max_oper_chwidth;
5909 params->vht_center_freq2 = vht_center_freq2;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005910
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07005911 freqs = os_calloc(wpa_s->num_multichan_concurrent,
5912 sizeof(struct wpa_used_freq_data));
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07005913 if (!freqs)
5914 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005915
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005916 num = get_shared_radio_freqs_data(wpa_s, freqs,
5917 wpa_s->num_multichan_concurrent);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005918
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005919 if (wpa_s->current_ssid &&
5920 wpa_s->current_ssid->mode == WPAS_MODE_P2P_GO &&
5921 wpa_s->wpa_state == WPA_COMPLETED) {
5922 wpa_printf(MSG_DEBUG, "P2P: %s called for an active GO",
5923 __func__);
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07005924
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005925 /*
5926 * If the frequency selection is done for an active P2P GO that
5927 * is not sharing a frequency, allow to select a new frequency
5928 * even if there are no unused frequencies as we are about to
5929 * move the P2P GO so its frequency can be re-used.
5930 */
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07005931 for (i = 0; i < num; i++) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005932 if (freqs[i].freq == wpa_s->current_ssid->frequency &&
5933 freqs[i].flags == 0) {
5934 ignore_no_freqs = 1;
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07005935 break;
5936 }
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07005937 }
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08005938 }
5939
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005940 /* try using the forced freq */
5941 if (freq) {
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07005942 if (wpas_p2p_disallowed_freq(wpa_s->global, freq) ||
5943 !freq_included(wpa_s, channels, freq)) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005944 wpa_printf(MSG_DEBUG,
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07005945 "P2P: Forced GO freq %d MHz disallowed",
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005946 freq);
5947 goto fail;
5948 }
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07005949 if (!p2p_supported_freq_go(wpa_s->global->p2p, freq)) {
5950 if ((wpa_s->drv_flags & WPA_DRIVER_FLAGS_DFS_OFFLOAD) &&
Roshan Pius3a1667e2018-07-03 15:17:14 -07005951 ieee80211_is_dfs(freq, wpa_s->hw.modes,
5952 wpa_s->hw.num_modes)) {
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07005953 /*
5954 * If freq is a DFS channel and DFS is offloaded
5955 * to the driver, allow P2P GO to use it.
5956 */
5957 wpa_printf(MSG_DEBUG,
5958 "P2P: %s: The forced channel for GO (%u MHz) requires DFS and DFS is offloaded",
5959 __func__, freq);
5960 } else {
5961 wpa_printf(MSG_DEBUG,
5962 "P2P: The forced channel for GO (%u MHz) is not supported for P2P uses",
5963 freq);
5964 goto fail;
5965 }
5966 }
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005967
5968 for (i = 0; i < num; i++) {
5969 if (freqs[i].freq == freq) {
5970 wpa_printf(MSG_DEBUG,
5971 "P2P: forced freq (%d MHz) is also shared",
5972 freq);
5973 params->freq = freq;
5974 goto success;
5975 }
5976 }
5977
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08005978 if (!ignore_no_freqs && !unused_channels) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005979 wpa_printf(MSG_DEBUG,
5980 "P2P: Cannot force GO on freq (%d MHz) as all the channels are in use",
5981 freq);
5982 goto fail;
5983 }
5984
5985 wpa_printf(MSG_DEBUG,
5986 "P2P: force GO freq (%d MHz) on a free channel",
5987 freq);
5988 params->freq = freq;
5989 goto success;
5990 }
5991
5992 /* consider using one of the shared frequencies */
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08005993 if (num &&
5994 (!wpa_s->conf->p2p_ignore_shared_freq || !unused_channels)) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005995 cand = wpas_p2p_pick_best_used_freq(wpa_s, freqs, num);
5996 if (wpas_p2p_supported_freq_go(wpa_s, channels, cand)) {
5997 wpa_printf(MSG_DEBUG,
5998 "P2P: Use shared freq (%d MHz) for GO",
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08005999 cand);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006000 params->freq = cand;
6001 goto success;
6002 }
6003
6004 /* try using one of the shared freqs */
6005 for (i = 0; i < num; i++) {
6006 if (wpas_p2p_supported_freq_go(wpa_s, channels,
6007 freqs[i].freq)) {
6008 wpa_printf(MSG_DEBUG,
6009 "P2P: Use shared freq (%d MHz) for GO",
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08006010 freqs[i].freq);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006011 params->freq = freqs[i].freq;
6012 goto success;
6013 }
6014 }
6015 }
6016
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08006017 if (!ignore_no_freqs && !unused_channels) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006018 wpa_printf(MSG_DEBUG,
6019 "P2P: Cannot force GO on any of the channels we are already using");
6020 goto fail;
6021 }
6022
6023 /* try using the setting from the configuration file */
6024 if (wpa_s->conf->p2p_oper_reg_class == 81 &&
6025 wpa_s->conf->p2p_oper_channel >= 1 &&
6026 wpa_s->conf->p2p_oper_channel <= 11 &&
6027 wpas_p2p_supported_freq_go(
6028 wpa_s, channels,
6029 2407 + 5 * wpa_s->conf->p2p_oper_channel)) {
6030 params->freq = 2407 + 5 * wpa_s->conf->p2p_oper_channel;
6031 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on configured "
6032 "frequency %d MHz", params->freq);
6033 goto success;
6034 }
6035
6036 if ((wpa_s->conf->p2p_oper_reg_class == 115 ||
6037 wpa_s->conf->p2p_oper_reg_class == 116 ||
6038 wpa_s->conf->p2p_oper_reg_class == 117 ||
6039 wpa_s->conf->p2p_oper_reg_class == 124 ||
6040 wpa_s->conf->p2p_oper_reg_class == 125 ||
6041 wpa_s->conf->p2p_oper_reg_class == 126 ||
6042 wpa_s->conf->p2p_oper_reg_class == 127) &&
6043 wpas_p2p_supported_freq_go(wpa_s, channels,
6044 5000 +
6045 5 * wpa_s->conf->p2p_oper_channel)) {
6046 params->freq = 5000 + 5 * wpa_s->conf->p2p_oper_channel;
6047 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on configured "
6048 "frequency %d MHz", params->freq);
6049 goto success;
6050 }
6051
6052 /* Try using best channels */
6053 if (wpa_s->conf->p2p_oper_channel == 0 &&
6054 wpa_s->best_overall_freq > 0 &&
6055 wpas_p2p_supported_freq_go(wpa_s, channels,
6056 wpa_s->best_overall_freq)) {
6057 params->freq = wpa_s->best_overall_freq;
6058 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on best overall "
6059 "channel %d MHz", params->freq);
6060 goto success;
6061 }
6062
6063 if (wpa_s->conf->p2p_oper_channel == 0 &&
6064 wpa_s->best_24_freq > 0 &&
6065 wpas_p2p_supported_freq_go(wpa_s, channels,
6066 wpa_s->best_24_freq)) {
6067 params->freq = wpa_s->best_24_freq;
6068 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on best 2.4 GHz "
6069 "channel %d MHz", params->freq);
6070 goto success;
6071 }
6072
6073 if (wpa_s->conf->p2p_oper_channel == 0 &&
6074 wpa_s->best_5_freq > 0 &&
6075 wpas_p2p_supported_freq_go(wpa_s, channels,
6076 wpa_s->best_5_freq)) {
6077 params->freq = wpa_s->best_5_freq;
6078 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on best 5 GHz "
6079 "channel %d MHz", params->freq);
6080 goto success;
6081 }
6082
6083 /* try using preferred channels */
6084 cand = p2p_get_pref_freq(wpa_s->global->p2p, channels);
6085 if (cand && wpas_p2p_supported_freq_go(wpa_s, channels, cand)) {
6086 params->freq = cand;
6087 wpa_printf(MSG_DEBUG, "P2P: Set GO freq %d MHz from preferred "
6088 "channels", params->freq);
6089 goto success;
6090 }
6091
Roshan Pius3a1667e2018-07-03 15:17:14 -07006092 /* Try using a channel that allows VHT to be used with 80 MHz */
6093 if (wpa_s->hw.modes && wpa_s->p2p_group_common_freqs) {
6094 for (i = 0; i < wpa_s->p2p_group_common_freqs_num; i++) {
6095 enum hostapd_hw_mode mode;
6096 struct hostapd_hw_modes *hwmode;
6097 u8 chan;
6098
6099 cand = wpa_s->p2p_group_common_freqs[i];
6100 mode = ieee80211_freq_to_chan(cand, &chan);
6101 hwmode = get_mode(wpa_s->hw.modes, wpa_s->hw.num_modes,
6102 mode);
6103 if (!hwmode ||
6104 wpas_p2p_verify_channel(wpa_s, hwmode, chan,
6105 BW80) != ALLOWED)
6106 continue;
6107 if (wpas_p2p_supported_freq_go(wpa_s, channels, cand)) {
6108 params->freq = cand;
6109 wpa_printf(MSG_DEBUG,
6110 "P2P: Use freq %d MHz common with the peer and allowing VHT80",
6111 params->freq);
6112 goto success;
6113 }
6114 }
6115 }
6116
6117 /* Try using a channel that allows HT to be used with 40 MHz on the same
6118 * band so that CSA can be used */
6119 if (wpa_s->current_ssid && wpa_s->hw.modes &&
6120 wpa_s->p2p_group_common_freqs) {
6121 for (i = 0; i < wpa_s->p2p_group_common_freqs_num; i++) {
6122 enum hostapd_hw_mode mode;
6123 struct hostapd_hw_modes *hwmode;
6124 u8 chan;
6125
6126 cand = wpa_s->p2p_group_common_freqs[i];
6127 mode = ieee80211_freq_to_chan(cand, &chan);
6128 hwmode = get_mode(wpa_s->hw.modes, wpa_s->hw.num_modes,
6129 mode);
6130 if (!wpas_same_band(wpa_s->current_ssid->frequency,
6131 cand) ||
6132 !hwmode ||
6133 (wpas_p2p_verify_channel(wpa_s, hwmode, chan,
6134 BW40MINUS) != ALLOWED &&
6135 wpas_p2p_verify_channel(wpa_s, hwmode, chan,
6136 BW40PLUS) != ALLOWED))
6137 continue;
6138 if (wpas_p2p_supported_freq_go(wpa_s, channels, cand)) {
6139 params->freq = cand;
6140 wpa_printf(MSG_DEBUG,
6141 "P2P: Use freq %d MHz common with the peer, allowing HT40, and maintaining same band",
6142 params->freq);
6143 goto success;
6144 }
6145 }
6146 }
6147
6148 /* Try using one of the group common freqs on the same band so that CSA
6149 * can be used */
6150 if (wpa_s->current_ssid && wpa_s->p2p_group_common_freqs) {
6151 for (i = 0; i < wpa_s->p2p_group_common_freqs_num; i++) {
6152 cand = wpa_s->p2p_group_common_freqs[i];
6153 if (!wpas_same_band(wpa_s->current_ssid->frequency,
6154 cand))
6155 continue;
6156 if (wpas_p2p_supported_freq_go(wpa_s, channels, cand)) {
6157 params->freq = cand;
6158 wpa_printf(MSG_DEBUG,
6159 "P2P: Use freq %d MHz common with the peer and maintaining same band",
6160 params->freq);
6161 goto success;
6162 }
6163 }
6164 }
6165
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006166 /* Try using one of the group common freqs */
6167 if (wpa_s->p2p_group_common_freqs) {
6168 for (i = 0; i < wpa_s->p2p_group_common_freqs_num; i++) {
6169 cand = wpa_s->p2p_group_common_freqs[i];
6170 if (wpas_p2p_supported_freq_go(wpa_s, channels, cand)) {
6171 params->freq = cand;
6172 wpa_printf(MSG_DEBUG,
6173 "P2P: Use freq %d MHz common with the peer",
6174 params->freq);
6175 goto success;
6176 }
6177 }
6178 }
6179
6180 /* no preference, select some channel */
6181 wpas_p2p_select_go_freq_no_pref(wpa_s, params, channels);
6182
6183 if (params->freq == 0) {
6184 wpa_printf(MSG_DEBUG, "P2P: did not find a freq for GO use");
6185 goto fail;
6186 }
6187
6188success:
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07006189 os_free(freqs);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006190 return 0;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006191fail:
6192 os_free(freqs);
6193 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006194}
6195
6196
6197static struct wpa_supplicant *
6198wpas_p2p_get_group_iface(struct wpa_supplicant *wpa_s, int addr_allocated,
6199 int go)
6200{
6201 struct wpa_supplicant *group_wpa_s;
6202
Dmitry Shmidtaa532512012-09-24 10:35:31 -07006203 if (!wpas_p2p_create_iface(wpa_s)) {
Dmitry Shmidt9c175262016-03-03 10:20:07 -08006204 if (wpa_s->p2p_mgmt) {
6205 /*
6206 * We may be called on the p2p_dev interface which
6207 * cannot be used for group operations, so always use
6208 * the primary interface.
6209 */
6210 wpa_s->parent->p2pdev = wpa_s;
6211 wpa_s = wpa_s->parent;
6212 }
6213 wpa_dbg(wpa_s, MSG_DEBUG,
6214 "P2P: Use primary interface for group operations");
Dmitry Shmidt56052862013-10-04 10:23:25 -07006215 wpa_s->p2p_first_connection_timeout = 0;
Dmitry Shmidt9c175262016-03-03 10:20:07 -08006216 if (wpa_s != wpa_s->p2pdev)
Dmitry Shmidt849734c2016-05-27 09:59:01 -07006217 wpas_p2p_clone_config(wpa_s, wpa_s->p2pdev);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006218 return wpa_s;
Dmitry Shmidtaa532512012-09-24 10:35:31 -07006219 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006220
6221 if (wpas_p2p_add_group_interface(wpa_s, go ? WPA_IF_P2P_GO :
Dmitry Shmidtaa532512012-09-24 10:35:31 -07006222 WPA_IF_P2P_CLIENT) < 0) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07006223 wpa_msg_global(wpa_s, MSG_ERROR,
6224 "P2P: Failed to add group interface");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006225 return NULL;
Dmitry Shmidtaa532512012-09-24 10:35:31 -07006226 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006227 group_wpa_s = wpas_p2p_init_group_interface(wpa_s, go);
6228 if (group_wpa_s == NULL) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07006229 wpa_msg_global(wpa_s, MSG_ERROR,
6230 "P2P: Failed to initialize group interface");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006231 wpas_p2p_remove_pending_group_interface(wpa_s);
6232 return NULL;
6233 }
6234
Roshan Pius3a1667e2018-07-03 15:17:14 -07006235 if (go && wpa_s->p2p_go_do_acs) {
6236 group_wpa_s->p2p_go_do_acs = wpa_s->p2p_go_do_acs;
6237 group_wpa_s->p2p_go_acs_band = wpa_s->p2p_go_acs_band;
6238 wpa_s->p2p_go_do_acs = 0;
6239 }
6240
Dmitry Shmidtaa532512012-09-24 10:35:31 -07006241 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Use separate group interface %s",
6242 group_wpa_s->ifname);
Dmitry Shmidt56052862013-10-04 10:23:25 -07006243 group_wpa_s->p2p_first_connection_timeout = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006244 return group_wpa_s;
6245}
6246
6247
6248/**
6249 * wpas_p2p_group_add - Add a new P2P group with local end as Group Owner
6250 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
6251 * @persistent_group: Whether to create a persistent group
6252 * @freq: Frequency for the group or 0 to indicate no hardcoding
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08006253 * @vht_center_freq2: segment_1 center frequency for GO operating in VHT 80P80
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07006254 * @ht40: Start GO with 40 MHz channel width
6255 * @vht: Start GO with VHT support
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08006256 * @vht_chwidth: channel bandwidth for GO operating with VHT support
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006257 * Returns: 0 on success, -1 on failure
6258 *
6259 * This function creates a new P2P group with the local end as the Group Owner,
6260 * i.e., without using Group Owner Negotiation.
6261 */
6262int wpas_p2p_group_add(struct wpa_supplicant *wpa_s, int persistent_group,
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08006263 int freq, int vht_center_freq2, int ht40, int vht,
6264 int max_oper_chwidth)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006265{
6266 struct p2p_go_neg_results params;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006267
6268 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
6269 return -1;
6270
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07006271 os_free(wpa_s->global->add_psk);
6272 wpa_s->global->add_psk = NULL;
6273
Dmitry Shmidtdca39792011-09-06 11:17:33 -07006274 /* Make sure we are not running find during connection establishment */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006275 wpa_printf(MSG_DEBUG, "P2P: Stop any on-going P2P FIND");
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08006276 wpas_p2p_stop_find_oper(wpa_s);
Dmitry Shmidtdca39792011-09-06 11:17:33 -07006277
Roshan Pius3a1667e2018-07-03 15:17:14 -07006278 if (!wpa_s->p2p_go_do_acs) {
6279 freq = wpas_p2p_select_go_freq(wpa_s, freq);
6280 if (freq < 0)
6281 return -1;
6282 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006283
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08006284 if (wpas_p2p_init_go_params(wpa_s, &params, freq, vht_center_freq2,
6285 ht40, vht, max_oper_chwidth, NULL))
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006286 return -1;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07006287
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006288 p2p_go_params(wpa_s->global->p2p, &params);
6289 params.persistent_group = persistent_group;
6290
6291 wpa_s = wpas_p2p_get_group_iface(wpa_s, 0, 1);
6292 if (wpa_s == NULL)
6293 return -1;
6294 wpas_start_wps_go(wpa_s, &params, 0);
6295
6296 return 0;
6297}
6298
6299
6300static int wpas_start_p2p_client(struct wpa_supplicant *wpa_s,
Dmitry Shmidt15907092014-03-25 10:42:57 -07006301 struct wpa_ssid *params, int addr_allocated,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006302 int freq, int force_scan)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006303{
6304 struct wpa_ssid *ssid;
6305
6306 wpa_s = wpas_p2p_get_group_iface(wpa_s, addr_allocated, 0);
6307 if (wpa_s == NULL)
6308 return -1;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006309 if (force_scan)
6310 os_get_reltime(&wpa_s->scan_min_time);
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07006311 wpa_s->p2p_last_4way_hs_fail = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006312
6313 wpa_supplicant_ap_deinit(wpa_s);
6314
6315 ssid = wpa_config_add_network(wpa_s->conf);
6316 if (ssid == NULL)
6317 return -1;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006318 os_memset(wpa_s->go_dev_addr, 0, ETH_ALEN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006319 wpa_config_set_network_defaults(ssid);
6320 ssid->temporary = 1;
6321 ssid->proto = WPA_PROTO_RSN;
Dmitry Shmidte4663042016-04-04 10:07:49 -07006322 ssid->pbss = params->pbss;
6323 ssid->pairwise_cipher = params->pbss ? WPA_CIPHER_GCMP :
6324 WPA_CIPHER_CCMP;
6325 ssid->group_cipher = params->pbss ? WPA_CIPHER_GCMP : WPA_CIPHER_CCMP;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006326 ssid->key_mgmt = WPA_KEY_MGMT_PSK;
6327 ssid->ssid = os_malloc(params->ssid_len);
6328 if (ssid->ssid == NULL) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006329 wpa_config_remove_network(wpa_s->conf, ssid->id);
6330 return -1;
6331 }
6332 os_memcpy(ssid->ssid, params->ssid, params->ssid_len);
6333 ssid->ssid_len = params->ssid_len;
6334 ssid->p2p_group = 1;
6335 ssid->export_keys = 1;
6336 if (params->psk_set) {
6337 os_memcpy(ssid->psk, params->psk, 32);
6338 ssid->psk_set = 1;
6339 }
6340 if (params->passphrase)
6341 ssid->passphrase = os_strdup(params->passphrase);
6342
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006343 wpa_s->show_group_started = 1;
Dmitry Shmidt15907092014-03-25 10:42:57 -07006344 wpa_s->p2p_in_invitation = 1;
6345 wpa_s->p2p_invite_go_freq = freq;
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08006346 wpa_s->p2p_go_group_formation_completed = 0;
6347 wpa_s->global->p2p_group_formation = wpa_s;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006348
Dmitry Shmidt9c175262016-03-03 10:20:07 -08006349 eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s->p2pdev,
Dmitry Shmidt15907092014-03-25 10:42:57 -07006350 NULL);
6351 eloop_register_timeout(P2P_MAX_INITIAL_CONN_WAIT, 0,
6352 wpas_p2p_group_formation_timeout,
Dmitry Shmidt9c175262016-03-03 10:20:07 -08006353 wpa_s->p2pdev, NULL);
Dmitry Shmidtfa3fc4a2013-11-21 13:34:38 -08006354 wpa_supplicant_select_network(wpa_s, ssid);
6355
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006356 return 0;
6357}
6358
6359
6360int wpas_p2p_group_add_persistent(struct wpa_supplicant *wpa_s,
6361 struct wpa_ssid *ssid, int addr_allocated,
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08006362 int force_freq, int neg_freq,
6363 int vht_center_freq2, int ht40,
6364 int vht, int max_oper_chwidth,
6365 const struct p2p_channels *channels,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006366 int connection_timeout, int force_scan)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006367{
6368 struct p2p_go_neg_results params;
Dmitry Shmidt96be6222014-02-13 10:16:51 -08006369 int go = 0, freq;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006370
6371 if (ssid->disabled != 2 || ssid->ssid == NULL)
6372 return -1;
6373
6374 if (wpas_get_p2p_group(wpa_s, ssid->ssid, ssid->ssid_len, &go) &&
6375 go == (ssid->mode == WPAS_MODE_P2P_GO)) {
6376 wpa_printf(MSG_DEBUG, "P2P: Requested persistent group is "
6377 "already running");
Dmitry Shmidtb1e52102015-05-29 12:36:29 -07006378 if (go == 0 &&
6379 eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
Dmitry Shmidt9c175262016-03-03 10:20:07 -08006380 wpa_s->p2pdev, NULL)) {
Dmitry Shmidtb1e52102015-05-29 12:36:29 -07006381 /*
6382 * This can happen if Invitation Response frame was lost
6383 * and the peer (GO of a persistent group) tries to
6384 * invite us again. Reschedule the timeout to avoid
6385 * terminating the wait for the connection too early
6386 * since we now know that the peer is still trying to
6387 * invite us instead of having already started the GO.
6388 */
6389 wpa_printf(MSG_DEBUG,
6390 "P2P: Reschedule group formation timeout since peer is still trying to invite us");
6391 eloop_register_timeout(P2P_MAX_INITIAL_CONN_WAIT, 0,
6392 wpas_p2p_group_formation_timeout,
Dmitry Shmidt9c175262016-03-03 10:20:07 -08006393 wpa_s->p2pdev, NULL);
Dmitry Shmidtb1e52102015-05-29 12:36:29 -07006394 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006395 return 0;
6396 }
6397
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07006398 os_free(wpa_s->global->add_psk);
6399 wpa_s->global->add_psk = NULL;
6400
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006401 /* Make sure we are not running find during connection establishment */
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08006402 wpas_p2p_stop_find_oper(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006403
Dmitry Shmidt04949592012-07-19 12:16:46 -07006404 wpa_s->p2p_fallback_to_go_neg = 0;
6405
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006406 if (ssid->mode == WPAS_MODE_P2P_GO) {
6407 if (force_freq > 0) {
6408 freq = wpas_p2p_select_go_freq(wpa_s, force_freq);
6409 if (freq < 0)
6410 return -1;
6411 } else {
6412 freq = wpas_p2p_select_go_freq(wpa_s, neg_freq);
6413 if (freq < 0 ||
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006414 (freq > 0 && !freq_included(wpa_s, channels, freq)))
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006415 freq = 0;
6416 }
Dmitry Shmidt912c6ec2015-03-30 13:16:51 -07006417 } else if (ssid->mode == WPAS_MODE_INFRA) {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006418 freq = neg_freq;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006419 if (freq <= 0 || !freq_included(wpa_s, channels, freq)) {
Dmitry Shmidt912c6ec2015-03-30 13:16:51 -07006420 struct os_reltime now;
6421 struct wpa_bss *bss =
6422 wpa_bss_get_p2p_dev_addr(wpa_s, ssid->bssid);
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07006423
Dmitry Shmidt912c6ec2015-03-30 13:16:51 -07006424 os_get_reltime(&now);
6425 if (bss &&
6426 !os_reltime_expired(&now, &bss->last_update, 5) &&
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006427 freq_included(wpa_s, channels, bss->freq))
Dmitry Shmidt912c6ec2015-03-30 13:16:51 -07006428 freq = bss->freq;
6429 else
6430 freq = 0;
6431 }
6432
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006433 return wpas_start_p2p_client(wpa_s, ssid, addr_allocated, freq,
6434 force_scan);
Dmitry Shmidt912c6ec2015-03-30 13:16:51 -07006435 } else {
Dmitry Shmidt15907092014-03-25 10:42:57 -07006436 return -1;
Dmitry Shmidt912c6ec2015-03-30 13:16:51 -07006437 }
Dmitry Shmidt15907092014-03-25 10:42:57 -07006438
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08006439 if (wpas_p2p_init_go_params(wpa_s, &params, freq, vht_center_freq2,
6440 ht40, vht, max_oper_chwidth, channels))
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006441 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006442
6443 params.role_go = 1;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08006444 params.psk_set = ssid->psk_set;
6445 if (params.psk_set)
6446 os_memcpy(params.psk, ssid->psk, sizeof(params.psk));
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08006447 if (ssid->passphrase) {
6448 if (os_strlen(ssid->passphrase) >= sizeof(params.passphrase)) {
6449 wpa_printf(MSG_ERROR, "P2P: Invalid passphrase in "
6450 "persistent group");
6451 return -1;
6452 }
6453 os_strlcpy(params.passphrase, ssid->passphrase,
6454 sizeof(params.passphrase));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006455 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006456 os_memcpy(params.ssid, ssid->ssid, ssid->ssid_len);
6457 params.ssid_len = ssid->ssid_len;
6458 params.persistent_group = 1;
6459
6460 wpa_s = wpas_p2p_get_group_iface(wpa_s, addr_allocated, 1);
6461 if (wpa_s == NULL)
6462 return -1;
6463
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006464 p2p_channels_to_freqs(channels, params.freq_list, P2P_MAX_CHANNELS);
6465
Dmitry Shmidt56052862013-10-04 10:23:25 -07006466 wpa_s->p2p_first_connection_timeout = connection_timeout;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006467 wpas_start_wps_go(wpa_s, &params, 0);
6468
6469 return 0;
6470}
6471
6472
6473static void wpas_p2p_ie_update(void *ctx, struct wpabuf *beacon_ies,
6474 struct wpabuf *proberesp_ies)
6475{
6476 struct wpa_supplicant *wpa_s = ctx;
6477 if (wpa_s->ap_iface) {
6478 struct hostapd_data *hapd = wpa_s->ap_iface->bss[0];
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006479 if (!(hapd->conf->p2p & P2P_GROUP_OWNER)) {
6480 wpabuf_free(beacon_ies);
6481 wpabuf_free(proberesp_ies);
6482 return;
6483 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006484 if (beacon_ies) {
6485 wpabuf_free(hapd->p2p_beacon_ie);
6486 hapd->p2p_beacon_ie = beacon_ies;
6487 }
6488 wpabuf_free(hapd->p2p_probe_resp_ie);
6489 hapd->p2p_probe_resp_ie = proberesp_ies;
6490 } else {
6491 wpabuf_free(beacon_ies);
6492 wpabuf_free(proberesp_ies);
6493 }
6494 wpa_supplicant_ap_update_beacon(wpa_s);
6495}
6496
6497
6498static void wpas_p2p_idle_update(void *ctx, int idle)
6499{
6500 struct wpa_supplicant *wpa_s = ctx;
6501 if (!wpa_s->ap_iface)
6502 return;
6503 wpa_printf(MSG_DEBUG, "P2P: GO - group %sidle", idle ? "" : "not ");
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08006504 if (idle) {
6505 if (wpa_s->global->p2p_fail_on_wps_complete &&
6506 wpa_s->p2p_in_provisioning) {
6507 wpas_p2p_grpform_fail_after_wps(wpa_s);
6508 return;
6509 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006510 wpas_p2p_set_group_idle_timeout(wpa_s);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08006511 } else
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006512 eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL);
6513}
6514
6515
6516struct p2p_group * wpas_p2p_group_init(struct wpa_supplicant *wpa_s,
Dmitry Shmidt04949592012-07-19 12:16:46 -07006517 struct wpa_ssid *ssid)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006518{
6519 struct p2p_group *group;
6520 struct p2p_group_config *cfg;
6521
Dmitry Shmidt849734c2016-05-27 09:59:01 -07006522 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL ||
6523 !ssid->p2p_group)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006524 return NULL;
6525
6526 cfg = os_zalloc(sizeof(*cfg));
6527 if (cfg == NULL)
6528 return NULL;
6529
Dmitry Shmidt04949592012-07-19 12:16:46 -07006530 if (ssid->p2p_persistent_group && wpa_s->conf->persistent_reconnect)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006531 cfg->persistent_group = 2;
Dmitry Shmidt04949592012-07-19 12:16:46 -07006532 else if (ssid->p2p_persistent_group)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006533 cfg->persistent_group = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006534 os_memcpy(cfg->interface_addr, wpa_s->own_addr, ETH_ALEN);
6535 if (wpa_s->max_stations &&
6536 wpa_s->max_stations < wpa_s->conf->max_num_sta)
6537 cfg->max_clients = wpa_s->max_stations;
6538 else
6539 cfg->max_clients = wpa_s->conf->max_num_sta;
Dmitry Shmidt04949592012-07-19 12:16:46 -07006540 os_memcpy(cfg->ssid, ssid->ssid, ssid->ssid_len);
6541 cfg->ssid_len = ssid->ssid_len;
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08006542 cfg->freq = ssid->frequency;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006543 cfg->cb_ctx = wpa_s;
6544 cfg->ie_update = wpas_p2p_ie_update;
6545 cfg->idle_update = wpas_p2p_idle_update;
Dmitry Shmidte4663042016-04-04 10:07:49 -07006546 cfg->ip_addr_alloc = WPA_GET_BE32(wpa_s->p2pdev->conf->ip_addr_start)
6547 != 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006548
6549 group = p2p_group_init(wpa_s->global->p2p, cfg);
6550 if (group == NULL)
6551 os_free(cfg);
Dmitry Shmidt04949592012-07-19 12:16:46 -07006552 if (ssid->mode != WPAS_MODE_P2P_GROUP_FORMATION)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006553 p2p_group_notif_formation_done(group);
6554 wpa_s->p2p_group = group;
6555 return group;
6556}
6557
6558
6559void wpas_p2p_wps_success(struct wpa_supplicant *wpa_s, const u8 *peer_addr,
6560 int registrar)
6561{
Dmitry Shmidt04949592012-07-19 12:16:46 -07006562 struct wpa_ssid *ssid = wpa_s->current_ssid;
6563
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006564 if (!wpa_s->p2p_in_provisioning) {
6565 wpa_printf(MSG_DEBUG, "P2P: Ignore WPS success event - P2P "
6566 "provisioning not in progress");
6567 return;
6568 }
6569
Dmitry Shmidt04949592012-07-19 12:16:46 -07006570 if (ssid && ssid->mode == WPAS_MODE_INFRA) {
6571 u8 go_dev_addr[ETH_ALEN];
6572 os_memcpy(go_dev_addr, wpa_s->bssid, ETH_ALEN);
6573 wpas_p2p_persistent_group(wpa_s, go_dev_addr, ssid->ssid,
6574 ssid->ssid_len);
6575 /* Clear any stored provisioning info */
6576 p2p_clear_provisioning_info(wpa_s->global->p2p, go_dev_addr);
6577 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006578
Dmitry Shmidt9c175262016-03-03 10:20:07 -08006579 eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s->p2pdev,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006580 NULL);
Dmitry Shmidt56052862013-10-04 10:23:25 -07006581 wpa_s->p2p_go_group_formation_completed = 1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07006582 if (ssid && ssid->mode == WPAS_MODE_INFRA) {
6583 /*
6584 * Use a separate timeout for initial data connection to
6585 * complete to allow the group to be removed automatically if
6586 * something goes wrong in this step before the P2P group idle
6587 * timeout mechanism is taken into use.
6588 */
Dmitry Shmidt56052862013-10-04 10:23:25 -07006589 wpa_dbg(wpa_s, MSG_DEBUG,
6590 "P2P: Re-start group formation timeout (%d seconds) as client for initial connection",
6591 P2P_MAX_INITIAL_CONN_WAIT);
Dmitry Shmidt04949592012-07-19 12:16:46 -07006592 eloop_register_timeout(P2P_MAX_INITIAL_CONN_WAIT, 0,
6593 wpas_p2p_group_formation_timeout,
Dmitry Shmidt9c175262016-03-03 10:20:07 -08006594 wpa_s->p2pdev, NULL);
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08006595 /* Complete group formation on successful data connection. */
6596 wpa_s->p2p_go_group_formation_completed = 0;
Dmitry Shmidt56052862013-10-04 10:23:25 -07006597 } else if (ssid) {
6598 /*
6599 * Use a separate timeout for initial data connection to
6600 * complete to allow the group to be removed automatically if
6601 * the client does not complete data connection successfully.
6602 */
6603 wpa_dbg(wpa_s, MSG_DEBUG,
6604 "P2P: Re-start group formation timeout (%d seconds) as GO for initial connection",
6605 P2P_MAX_INITIAL_CONN_WAIT_GO);
6606 eloop_register_timeout(P2P_MAX_INITIAL_CONN_WAIT_GO, 0,
6607 wpas_p2p_group_formation_timeout,
Dmitry Shmidt9c175262016-03-03 10:20:07 -08006608 wpa_s->p2pdev, NULL);
Dmitry Shmidt56052862013-10-04 10:23:25 -07006609 /*
6610 * Complete group formation on first successful data connection
6611 */
6612 wpa_s->p2p_go_group_formation_completed = 0;
Dmitry Shmidt04949592012-07-19 12:16:46 -07006613 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006614 if (wpa_s->global->p2p)
6615 p2p_wps_success_cb(wpa_s->global->p2p, peer_addr);
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07006616 wpas_group_formation_completed(wpa_s, 1, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006617}
6618
6619
Jouni Malinen75ecf522011-06-27 15:19:46 -07006620void wpas_p2p_wps_failed(struct wpa_supplicant *wpa_s,
6621 struct wps_event_fail *fail)
6622{
6623 if (!wpa_s->p2p_in_provisioning) {
6624 wpa_printf(MSG_DEBUG, "P2P: Ignore WPS fail event - P2P "
6625 "provisioning not in progress");
6626 return;
6627 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006628
6629 if (wpa_s->go_params) {
6630 p2p_clear_provisioning_info(
6631 wpa_s->global->p2p,
Dmitry Shmidt04949592012-07-19 12:16:46 -07006632 wpa_s->go_params->peer_device_addr);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006633 }
6634
Jouni Malinen75ecf522011-06-27 15:19:46 -07006635 wpas_notify_p2p_wps_failed(wpa_s, fail);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08006636
6637 if (wpa_s == wpa_s->global->p2p_group_formation) {
6638 /*
6639 * Allow some time for the failed WPS negotiation exchange to
6640 * complete, but remove the group since group formation cannot
6641 * succeed after provisioning failure.
6642 */
6643 wpa_printf(MSG_DEBUG, "P2P: WPS step failed during group formation - reject connection from timeout");
6644 wpa_s->global->p2p_fail_on_wps_complete = 1;
6645 eloop_deplete_timeout(0, 50000,
6646 wpas_p2p_group_formation_timeout,
Dmitry Shmidt9c175262016-03-03 10:20:07 -08006647 wpa_s->p2pdev, NULL);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08006648 }
6649}
6650
6651
6652int wpas_p2p_wps_eapol_cb(struct wpa_supplicant *wpa_s)
6653{
6654 if (!wpa_s->global->p2p_fail_on_wps_complete ||
6655 !wpa_s->p2p_in_provisioning)
6656 return 0;
6657
6658 wpas_p2p_grpform_fail_after_wps(wpa_s);
6659
6660 return 1;
Jouni Malinen75ecf522011-06-27 15:19:46 -07006661}
6662
6663
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006664int wpas_p2p_prov_disc(struct wpa_supplicant *wpa_s, const u8 *peer_addr,
Dmitry Shmidt04949592012-07-19 12:16:46 -07006665 const char *config_method,
Dmitry Shmidt216983b2015-02-06 10:50:36 -08006666 enum wpas_p2p_prov_disc_use use,
6667 struct p2ps_provision *p2ps_prov)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006668{
6669 u16 config_methods;
6670
Dmitry Shmidt216983b2015-02-06 10:50:36 -08006671 wpa_s->global->pending_p2ps_group = 0;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006672 wpa_s->global->pending_p2ps_group_freq = 0;
Dmitry Shmidt04949592012-07-19 12:16:46 -07006673 wpa_s->p2p_fallback_to_go_neg = 0;
6674 wpa_s->pending_pd_use = NORMAL_PD;
Dmitry Shmidt216983b2015-02-06 10:50:36 -08006675 if (p2ps_prov && use == WPAS_P2P_PD_FOR_ASP) {
6676 p2ps_prov->conncap = p2ps_group_capability(
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006677 wpa_s, P2PS_SETUP_NONE, p2ps_prov->role,
6678 &p2ps_prov->force_freq, &p2ps_prov->pref_freq);
6679
Dmitry Shmidt216983b2015-02-06 10:50:36 -08006680 wpa_printf(MSG_DEBUG,
6681 "P2P: %s conncap: %d - ASP parsed: %x %x %d %s",
6682 __func__, p2ps_prov->conncap,
6683 p2ps_prov->adv_id, p2ps_prov->conncap,
6684 p2ps_prov->status, p2ps_prov->info);
6685
6686 config_methods = 0;
6687 } else if (os_strncmp(config_method, "display", 7) == 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006688 config_methods = WPS_CONFIG_DISPLAY;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006689 else if (os_strncmp(config_method, "keypad", 6) == 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006690 config_methods = WPS_CONFIG_KEYPAD;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006691 else if (os_strncmp(config_method, "pbc", 3) == 0 ||
6692 os_strncmp(config_method, "pushbutton", 10) == 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006693 config_methods = WPS_CONFIG_PUSHBUTTON;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006694 else {
6695 wpa_printf(MSG_DEBUG, "P2P: Unknown config method");
Dmitry Shmidt216983b2015-02-06 10:50:36 -08006696 os_free(p2ps_prov);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006697 return -1;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006698 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006699
Dmitry Shmidt04949592012-07-19 12:16:46 -07006700 if (use == WPAS_P2P_PD_AUTO) {
6701 os_memcpy(wpa_s->pending_join_dev_addr, peer_addr, ETH_ALEN);
6702 wpa_s->pending_pd_config_methods = config_methods;
6703 wpa_s->p2p_auto_pd = 1;
6704 wpa_s->p2p_auto_join = 0;
6705 wpa_s->pending_pd_before_join = 0;
6706 wpa_s->auto_pd_scan_retry = 0;
6707 wpas_p2p_stop_find(wpa_s);
6708 wpa_s->p2p_join_scan_count = 0;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08006709 os_get_reltime(&wpa_s->p2p_auto_started);
Dmitry Shmidt04949592012-07-19 12:16:46 -07006710 wpa_printf(MSG_DEBUG, "P2P: Auto PD started at %ld.%06ld",
6711 wpa_s->p2p_auto_started.sec,
6712 wpa_s->p2p_auto_started.usec);
6713 wpas_p2p_join_scan(wpa_s, NULL);
6714 return 0;
6715 }
6716
Dmitry Shmidt216983b2015-02-06 10:50:36 -08006717 if (wpa_s->global->p2p == NULL || wpa_s->global->p2p_disabled) {
6718 os_free(p2ps_prov);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006719 return -1;
Dmitry Shmidt216983b2015-02-06 10:50:36 -08006720 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006721
Dmitry Shmidt216983b2015-02-06 10:50:36 -08006722 return p2p_prov_disc_req(wpa_s->global->p2p, peer_addr, p2ps_prov,
Dmitry Shmidt04949592012-07-19 12:16:46 -07006723 config_methods, use == WPAS_P2P_PD_FOR_JOIN,
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08006724 0, 1);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006725}
6726
6727
6728int wpas_p2p_scan_result_text(const u8 *ies, size_t ies_len, char *buf,
6729 char *end)
6730{
6731 return p2p_scan_result_text(ies, ies_len, buf, end);
6732}
6733
6734
6735static void wpas_p2p_clear_pending_action_tx(struct wpa_supplicant *wpa_s)
6736{
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08006737 if (!offchannel_pending_action_tx(wpa_s))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006738 return;
6739
Dmitry Shmidtde47be72016-01-07 12:52:55 -08006740 if (wpa_s->p2p_send_action_work) {
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08006741 wpas_p2p_free_send_action_work(wpa_s);
Dmitry Shmidtde47be72016-01-07 12:52:55 -08006742 eloop_cancel_timeout(wpas_p2p_send_action_work_timeout,
6743 wpa_s, NULL);
6744 offchannel_send_action_done(wpa_s);
6745 }
Dmitry Shmidt03658832014-08-13 11:03:49 -07006746
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006747 wpa_printf(MSG_DEBUG, "P2P: Drop pending Action TX due to new "
6748 "operation request");
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08006749 offchannel_clear_pending_action_tx(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006750}
6751
6752
6753int wpas_p2p_find(struct wpa_supplicant *wpa_s, unsigned int timeout,
6754 enum p2p_discovery_type type,
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08006755 unsigned int num_req_dev_types, const u8 *req_dev_types,
Dmitry Shmidt216983b2015-02-06 10:50:36 -08006756 const u8 *dev_id, unsigned int search_delay,
Dmitry Shmidt203eadb2015-03-05 14:16:04 -08006757 u8 seek_cnt, const char **seek_string, int freq)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006758{
6759 wpas_p2p_clear_pending_action_tx(wpa_s);
6760 wpa_s->p2p_long_listen = 0;
6761
Dmitry Shmidt04949592012-07-19 12:16:46 -07006762 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL ||
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07006763 wpa_s->p2p_in_provisioning) {
6764 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Reject p2p_find operation%s%s",
6765 (wpa_s->global->p2p_disabled || !wpa_s->global->p2p) ?
6766 " (P2P disabled)" : "",
6767 wpa_s->p2p_in_provisioning ?
6768 " (p2p_in_provisioning)" : "");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006769 return -1;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07006770 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006771
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08006772 wpa_supplicant_cancel_sched_scan(wpa_s);
6773
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006774 return p2p_find(wpa_s->global->p2p, timeout, type,
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07006775 num_req_dev_types, req_dev_types, dev_id,
Dmitry Shmidt203eadb2015-03-05 14:16:04 -08006776 search_delay, seek_cnt, seek_string, freq);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006777}
6778
6779
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006780static void wpas_p2p_scan_res_ignore_search(struct wpa_supplicant *wpa_s,
6781 struct wpa_scan_results *scan_res)
6782{
6783 wpa_printf(MSG_DEBUG, "P2P: Ignore scan results");
6784
6785 if (wpa_s->p2p_scan_work) {
6786 struct wpa_radio_work *work = wpa_s->p2p_scan_work;
6787 wpa_s->p2p_scan_work = NULL;
6788 radio_work_done(work);
6789 }
6790
6791 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
6792 return;
6793
6794 /*
6795 * Indicate that results have been processed so that the P2P module can
6796 * continue pending tasks.
6797 */
6798 p2p_scan_res_handled(wpa_s->global->p2p);
6799}
6800
6801
6802static void wpas_p2p_stop_find_oper(struct wpa_supplicant *wpa_s)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006803{
6804 wpas_p2p_clear_pending_action_tx(wpa_s);
6805 wpa_s->p2p_long_listen = 0;
6806 eloop_cancel_timeout(wpas_p2p_long_listen_timeout, wpa_s, NULL);
6807 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006808
6809 if (wpa_s->global->p2p)
6810 p2p_stop_find(wpa_s->global->p2p);
6811
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006812 if (wpa_s->scan_res_handler == wpas_p2p_scan_res_handler) {
6813 wpa_printf(MSG_DEBUG,
6814 "P2P: Do not consider the scan results after stop_find");
6815 wpa_s->scan_res_handler = wpas_p2p_scan_res_ignore_search;
6816 }
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08006817}
6818
6819
6820void wpas_p2p_stop_find(struct wpa_supplicant *wpa_s)
6821{
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006822 wpas_p2p_stop_find_oper(wpa_s);
Dmitry Shmidt216983b2015-02-06 10:50:36 -08006823 if (!wpa_s->global->pending_group_iface_for_p2ps)
6824 wpas_p2p_remove_pending_group_interface(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006825}
6826
6827
6828static void wpas_p2p_long_listen_timeout(void *eloop_ctx, void *timeout_ctx)
6829{
6830 struct wpa_supplicant *wpa_s = eloop_ctx;
6831 wpa_s->p2p_long_listen = 0;
6832}
6833
6834
6835int wpas_p2p_listen(struct wpa_supplicant *wpa_s, unsigned int timeout)
6836{
6837 int res;
6838
6839 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
6840 return -1;
6841
Dmitry Shmidt58d12ad2016-07-28 10:07:03 -07006842 if (wpa_s->p2p_lo_started) {
6843 wpa_printf(MSG_DEBUG,
6844 "P2P: Cannot start P2P listen, it is offloaded");
6845 return -1;
6846 }
6847
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08006848 wpa_supplicant_cancel_sched_scan(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006849 wpas_p2p_clear_pending_action_tx(wpa_s);
6850
6851 if (timeout == 0) {
6852 /*
6853 * This is a request for unlimited Listen state. However, at
6854 * least for now, this is mapped to a Listen state for one
6855 * hour.
6856 */
6857 timeout = 3600;
6858 }
6859 eloop_cancel_timeout(wpas_p2p_long_listen_timeout, wpa_s, NULL);
6860 wpa_s->p2p_long_listen = 0;
6861
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006862 /*
6863 * Stop previous find/listen operation to avoid trying to request a new
6864 * remain-on-channel operation while the driver is still running the
6865 * previous one.
6866 */
6867 if (wpa_s->global->p2p)
6868 p2p_stop_find(wpa_s->global->p2p);
6869
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006870 res = wpas_p2p_listen_start(wpa_s, timeout * 1000);
6871 if (res == 0 && timeout * 1000 > wpa_s->max_remain_on_chan) {
6872 wpa_s->p2p_long_listen = timeout * 1000;
6873 eloop_register_timeout(timeout, 0,
6874 wpas_p2p_long_listen_timeout,
6875 wpa_s, NULL);
6876 }
6877
6878 return res;
6879}
6880
6881
6882int wpas_p2p_assoc_req_ie(struct wpa_supplicant *wpa_s, struct wpa_bss *bss,
6883 u8 *buf, size_t len, int p2p_group)
6884{
6885 struct wpabuf *p2p_ie;
6886 int ret;
6887
6888 if (wpa_s->global->p2p_disabled)
6889 return -1;
Dmitry Shmidt8bd70b72015-05-26 16:02:19 -07006890 /*
6891 * Advertize mandatory cross connection capability even on
6892 * p2p_disabled=1 interface when associating with a P2P Manager WLAN AP.
6893 */
6894 if (wpa_s->conf->p2p_disabled && p2p_group)
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07006895 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006896 if (wpa_s->global->p2p == NULL)
6897 return -1;
6898 if (bss == NULL)
6899 return -1;
6900
6901 p2p_ie = wpa_bss_get_vendor_ie_multi(bss, P2P_IE_VENDOR_TYPE);
6902 ret = p2p_assoc_req_ie(wpa_s->global->p2p, bss->bssid, buf, len,
6903 p2p_group, p2p_ie);
6904 wpabuf_free(p2p_ie);
6905
6906 return ret;
6907}
6908
6909
6910int wpas_p2p_probe_req_rx(struct wpa_supplicant *wpa_s, const u8 *addr,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006911 const u8 *dst, const u8 *bssid,
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07006912 const u8 *ie, size_t ie_len,
6913 unsigned int rx_freq, int ssi_signal)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006914{
6915 if (wpa_s->global->p2p_disabled)
6916 return 0;
6917 if (wpa_s->global->p2p == NULL)
6918 return 0;
6919
Dmitry Shmidt04949592012-07-19 12:16:46 -07006920 switch (p2p_probe_req_rx(wpa_s->global->p2p, addr, dst, bssid,
Dmitry Shmidt58d12ad2016-07-28 10:07:03 -07006921 ie, ie_len, rx_freq, wpa_s->p2p_lo_started)) {
Dmitry Shmidt04949592012-07-19 12:16:46 -07006922 case P2P_PREQ_NOT_P2P:
6923 wpas_notify_preq(wpa_s, addr, dst, bssid, ie, ie_len,
6924 ssi_signal);
6925 /* fall through */
6926 case P2P_PREQ_MALFORMED:
6927 case P2P_PREQ_NOT_LISTEN:
6928 case P2P_PREQ_NOT_PROCESSED:
6929 default: /* make gcc happy */
6930 return 0;
6931 case P2P_PREQ_PROCESSED:
6932 return 1;
6933 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006934}
6935
6936
6937void wpas_p2p_rx_action(struct wpa_supplicant *wpa_s, const u8 *da,
6938 const u8 *sa, const u8 *bssid,
6939 u8 category, const u8 *data, size_t len, int freq)
6940{
6941 if (wpa_s->global->p2p_disabled)
6942 return;
6943 if (wpa_s->global->p2p == NULL)
6944 return;
6945
6946 p2p_rx_action(wpa_s->global->p2p, da, sa, bssid, category, data, len,
6947 freq);
6948}
6949
6950
6951void wpas_p2p_scan_ie(struct wpa_supplicant *wpa_s, struct wpabuf *ies)
6952{
Dmitry Shmidt9c175262016-03-03 10:20:07 -08006953 unsigned int bands;
6954
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006955 if (wpa_s->global->p2p_disabled)
6956 return;
6957 if (wpa_s->global->p2p == NULL)
6958 return;
6959
Dmitry Shmidt9c175262016-03-03 10:20:07 -08006960 bands = wpas_get_bands(wpa_s, NULL);
6961 p2p_scan_ie(wpa_s->global->p2p, ies, NULL, bands);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006962}
6963
6964
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07006965static void wpas_p2p_group_deinit(struct wpa_supplicant *wpa_s)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006966{
6967 p2p_group_deinit(wpa_s->p2p_group);
6968 wpa_s->p2p_group = NULL;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006969
6970 wpa_s->ap_configured_cb = NULL;
6971 wpa_s->ap_configured_cb_ctx = NULL;
6972 wpa_s->ap_configured_cb_data = NULL;
6973 wpa_s->connect_without_scan = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006974}
6975
6976
6977int wpas_p2p_reject(struct wpa_supplicant *wpa_s, const u8 *addr)
6978{
6979 wpa_s->p2p_long_listen = 0;
6980
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006981 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
6982 return -1;
6983
6984 return p2p_reject(wpa_s->global->p2p, addr);
6985}
6986
6987
6988/* Invite to reinvoke a persistent group */
6989int wpas_p2p_invite(struct wpa_supplicant *wpa_s, const u8 *peer_addr,
Jouni Malinen31be0a42012-08-31 21:20:51 +03006990 struct wpa_ssid *ssid, const u8 *go_dev_addr, int freq,
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08006991 int vht_center_freq2, int ht40, int vht, int max_chwidth,
6992 int pref_freq)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006993{
6994 enum p2p_invite_role role;
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07006995 u8 *bssid = NULL;
6996 int force_freq = 0;
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08006997 int res;
Dmitry Shmidta0d265f2013-11-19 13:13:41 -08006998 int no_pref_freq_given = pref_freq == 0;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006999 unsigned int pref_freq_list[P2P_MAX_PREF_CHANNELS], size;
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08007000
Dmitry Shmidt700a1372013-03-15 14:14:44 -07007001 wpa_s->global->p2p_invite_group = NULL;
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08007002 if (peer_addr)
7003 os_memcpy(wpa_s->p2p_auth_invite, peer_addr, ETH_ALEN);
7004 else
7005 os_memset(wpa_s->p2p_auth_invite, 0, ETH_ALEN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007006
Jouni Malinen31be0a42012-08-31 21:20:51 +03007007 wpa_s->p2p_persistent_go_freq = freq;
7008 wpa_s->p2p_go_ht40 = !!ht40;
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08007009 wpa_s->p2p_go_vht = !!vht;
7010 wpa_s->p2p_go_max_oper_chwidth = max_chwidth;
7011 wpa_s->p2p_go_vht_center_freq2 = vht_center_freq2;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007012 if (ssid->mode == WPAS_MODE_P2P_GO) {
7013 role = P2P_INVITE_ROLE_GO;
7014 if (peer_addr == NULL) {
7015 wpa_printf(MSG_DEBUG, "P2P: Missing peer "
7016 "address in invitation command");
7017 return -1;
7018 }
7019 if (wpas_p2p_create_iface(wpa_s)) {
7020 if (wpas_p2p_add_group_interface(wpa_s,
7021 WPA_IF_P2P_GO) < 0) {
7022 wpa_printf(MSG_ERROR, "P2P: Failed to "
7023 "allocate a new interface for the "
7024 "group");
7025 return -1;
7026 }
7027 bssid = wpa_s->pending_interface_addr;
Dmitry Shmidt9c175262016-03-03 10:20:07 -08007028 } else if (wpa_s->p2p_mgmt)
7029 bssid = wpa_s->parent->own_addr;
7030 else
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007031 bssid = wpa_s->own_addr;
7032 } else {
7033 role = P2P_INVITE_ROLE_CLIENT;
7034 peer_addr = ssid->bssid;
7035 }
7036 wpa_s->pending_invite_ssid_id = ssid->id;
7037
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08007038 size = P2P_MAX_PREF_CHANNELS;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07007039 res = wpas_p2p_setup_freqs(wpa_s, freq, &force_freq, &pref_freq,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08007040 role == P2P_INVITE_ROLE_GO,
7041 pref_freq_list, &size);
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08007042 if (res)
7043 return res;
Irfan Sheriffaf84a572012-09-22 16:59:30 -07007044
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007045 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
7046 return -1;
7047
Dmitry Shmidt7d56b752015-12-22 10:59:44 -08007048 p2p_set_own_pref_freq_list(wpa_s->global->p2p, pref_freq_list, size);
7049
Dmitry Shmidta0d265f2013-11-19 13:13:41 -08007050 if (wpa_s->parent->conf->p2p_ignore_shared_freq &&
7051 no_pref_freq_given && pref_freq > 0 &&
7052 wpa_s->num_multichan_concurrent > 1 &&
7053 wpas_p2p_num_unused_channels(wpa_s) > 0) {
7054 wpa_printf(MSG_DEBUG, "P2P: Ignore own channel preference %d MHz for invitation due to p2p_ignore_shared_freq=1 configuration",
7055 pref_freq);
7056 pref_freq = 0;
7057 }
7058
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08007059 /*
7060 * Stop any find/listen operations before invitation and possibly
7061 * connection establishment.
7062 */
7063 wpas_p2p_stop_find_oper(wpa_s);
7064
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007065 return p2p_invite(wpa_s->global->p2p, peer_addr, role, bssid,
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08007066 ssid->ssid, ssid->ssid_len, force_freq, go_dev_addr,
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08007067 1, pref_freq, -1);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007068}
7069
7070
7071/* Invite to join an active group */
7072int wpas_p2p_invite_group(struct wpa_supplicant *wpa_s, const char *ifname,
7073 const u8 *peer_addr, const u8 *go_dev_addr)
7074{
7075 struct wpa_global *global = wpa_s->global;
7076 enum p2p_invite_role role;
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07007077 u8 *bssid = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007078 struct wpa_ssid *ssid;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08007079 int persistent;
Dmitry Shmidt051af732013-10-22 13:52:46 -07007080 int freq = 0, force_freq = 0, pref_freq = 0;
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08007081 int res;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08007082 unsigned int pref_freq_list[P2P_MAX_PREF_CHANNELS], size;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007083
Jouni Malinen31be0a42012-08-31 21:20:51 +03007084 wpa_s->p2p_persistent_go_freq = 0;
7085 wpa_s->p2p_go_ht40 = 0;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07007086 wpa_s->p2p_go_vht = 0;
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08007087 wpa_s->p2p_go_vht_center_freq2 = 0;
7088 wpa_s->p2p_go_max_oper_chwidth = 0;
Jouni Malinen31be0a42012-08-31 21:20:51 +03007089
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007090 for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
7091 if (os_strcmp(wpa_s->ifname, ifname) == 0)
7092 break;
7093 }
7094 if (wpa_s == NULL) {
7095 wpa_printf(MSG_DEBUG, "P2P: Interface '%s' not found", ifname);
7096 return -1;
7097 }
7098
7099 ssid = wpa_s->current_ssid;
7100 if (ssid == NULL) {
7101 wpa_printf(MSG_DEBUG, "P2P: No current SSID to use for "
7102 "invitation");
7103 return -1;
7104 }
7105
Dmitry Shmidt700a1372013-03-15 14:14:44 -07007106 wpa_s->global->p2p_invite_group = wpa_s;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08007107 persistent = ssid->p2p_persistent_group &&
Dmitry Shmidt9c175262016-03-03 10:20:07 -08007108 wpas_p2p_get_persistent(wpa_s->p2pdev, peer_addr,
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08007109 ssid->ssid, ssid->ssid_len);
7110
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007111 if (ssid->mode == WPAS_MODE_P2P_GO) {
7112 role = P2P_INVITE_ROLE_ACTIVE_GO;
7113 bssid = wpa_s->own_addr;
7114 if (go_dev_addr == NULL)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007115 go_dev_addr = wpa_s->global->p2p_dev_addr;
Dmitry Shmidt051af732013-10-22 13:52:46 -07007116 freq = ssid->frequency;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007117 } else {
7118 role = P2P_INVITE_ROLE_CLIENT;
7119 if (wpa_s->wpa_state < WPA_ASSOCIATED) {
7120 wpa_printf(MSG_DEBUG, "P2P: Not associated - cannot "
7121 "invite to current group");
7122 return -1;
7123 }
7124 bssid = wpa_s->bssid;
7125 if (go_dev_addr == NULL &&
7126 !is_zero_ether_addr(wpa_s->go_dev_addr))
7127 go_dev_addr = wpa_s->go_dev_addr;
Dmitry Shmidt051af732013-10-22 13:52:46 -07007128 freq = wpa_s->current_bss ? wpa_s->current_bss->freq :
7129 (int) wpa_s->assoc_freq;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007130 }
Dmitry Shmidt9c175262016-03-03 10:20:07 -08007131 wpa_s->p2pdev->pending_invite_ssid_id = -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007132
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007133 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
7134 return -1;
7135
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08007136 size = P2P_MAX_PREF_CHANNELS;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07007137 res = wpas_p2p_setup_freqs(wpa_s, freq, &force_freq, &pref_freq,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08007138 role == P2P_INVITE_ROLE_ACTIVE_GO,
7139 pref_freq_list, &size);
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08007140 if (res)
7141 return res;
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07007142 wpas_p2p_set_own_freq_preference(wpa_s, force_freq);
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08007143
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007144 return p2p_invite(wpa_s->global->p2p, peer_addr, role, bssid,
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08007145 ssid->ssid, ssid->ssid_len, force_freq,
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08007146 go_dev_addr, persistent, pref_freq, -1);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007147}
7148
7149
7150void wpas_p2p_completed(struct wpa_supplicant *wpa_s)
7151{
7152 struct wpa_ssid *ssid = wpa_s->current_ssid;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007153 u8 go_dev_addr[ETH_ALEN];
7154 int persistent;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007155 int freq;
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08007156 u8 ip[3 * 4];
7157 char ip_addr[100];
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007158
Dmitry Shmidt04949592012-07-19 12:16:46 -07007159 if (ssid == NULL || ssid->mode != WPAS_MODE_P2P_GROUP_FORMATION) {
7160 eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
Dmitry Shmidt9c175262016-03-03 10:20:07 -08007161 wpa_s->p2pdev, NULL);
Dmitry Shmidt04949592012-07-19 12:16:46 -07007162 }
7163
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007164 if (!wpa_s->show_group_started || !ssid)
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08007165 return;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007166
7167 wpa_s->show_group_started = 0;
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08007168 if (!wpa_s->p2p_go_group_formation_completed &&
7169 wpa_s->global->p2p_group_formation == wpa_s) {
7170 wpa_dbg(wpa_s, MSG_DEBUG,
7171 "P2P: Marking group formation completed on client on data connection");
7172 wpa_s->p2p_go_group_formation_completed = 1;
7173 wpa_s->global->p2p_group_formation = NULL;
7174 wpa_s->p2p_in_provisioning = 0;
7175 wpa_s->p2p_in_invitation = 0;
7176 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007177
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007178 os_memset(go_dev_addr, 0, ETH_ALEN);
7179 if (ssid->bssid_set)
7180 os_memcpy(go_dev_addr, ssid->bssid, ETH_ALEN);
7181 persistent = wpas_p2p_persistent_group(wpa_s, go_dev_addr, ssid->ssid,
7182 ssid->ssid_len);
7183 os_memcpy(wpa_s->go_dev_addr, go_dev_addr, ETH_ALEN);
7184
7185 if (wpa_s->global->p2p_group_formation == wpa_s)
7186 wpa_s->global->p2p_group_formation = NULL;
7187
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007188 freq = wpa_s->current_bss ? wpa_s->current_bss->freq :
7189 (int) wpa_s->assoc_freq;
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08007190
7191 ip_addr[0] = '\0';
7192 if (wpa_sm_get_p2p_ip_addr(wpa_s->wpa, ip) == 0) {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007193 int res;
7194
7195 res = os_snprintf(ip_addr, sizeof(ip_addr),
7196 " ip_addr=%u.%u.%u.%u "
7197 "ip_mask=%u.%u.%u.%u go_ip_addr=%u.%u.%u.%u",
7198 ip[0], ip[1], ip[2], ip[3],
7199 ip[4], ip[5], ip[6], ip[7],
7200 ip[8], ip[9], ip[10], ip[11]);
7201 if (os_snprintf_error(sizeof(ip_addr), res))
7202 ip_addr[0] = '\0';
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08007203 }
7204
Dmitry Shmidt7f0b69e2014-07-28 10:35:20 -07007205 wpas_p2p_group_started(wpa_s, 0, ssid, freq,
7206 ssid->passphrase == NULL && ssid->psk_set ?
7207 ssid->psk : NULL,
7208 ssid->passphrase, go_dev_addr, persistent,
7209 ip_addr);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007210
7211 if (persistent)
Dmitry Shmidtd5ab1b52016-06-21 12:38:41 -07007212 wpas_p2p_store_persistent_group(wpa_s->p2pdev,
7213 ssid, go_dev_addr);
7214
Dmitry Shmidtabb90a32016-12-05 15:34:39 -08007215 wpas_notify_p2p_group_started(wpa_s, ssid, persistent, 1, ip);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007216}
7217
7218
7219int wpas_p2p_presence_req(struct wpa_supplicant *wpa_s, u32 duration1,
7220 u32 interval1, u32 duration2, u32 interval2)
7221{
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08007222 int ret;
7223
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007224 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
7225 return -1;
7226
7227 if (wpa_s->wpa_state < WPA_ASSOCIATED ||
7228 wpa_s->current_ssid == NULL ||
7229 wpa_s->current_ssid->mode != WPAS_MODE_INFRA)
7230 return -1;
7231
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08007232 ret = p2p_presence_req(wpa_s->global->p2p, wpa_s->bssid,
7233 wpa_s->own_addr, wpa_s->assoc_freq,
7234 duration1, interval1, duration2, interval2);
7235 if (ret == 0)
7236 wpa_s->waiting_presence_resp = 1;
7237
7238 return ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007239}
7240
7241
7242int wpas_p2p_ext_listen(struct wpa_supplicant *wpa_s, unsigned int period,
7243 unsigned int interval)
7244{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007245 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
7246 return -1;
7247
7248 return p2p_ext_listen(wpa_s->global->p2p, period, interval);
7249}
7250
7251
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007252static int wpas_p2p_is_client(struct wpa_supplicant *wpa_s)
7253{
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08007254 if (wpa_s->current_ssid == NULL) {
7255 /*
7256 * current_ssid can be cleared when P2P client interface gets
7257 * disconnected, so assume this interface was used as P2P
7258 * client.
7259 */
7260 return 1;
7261 }
7262 return wpa_s->current_ssid->p2p_group &&
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007263 wpa_s->current_ssid->mode == WPAS_MODE_INFRA;
7264}
7265
7266
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007267static void wpas_p2p_group_idle_timeout(void *eloop_ctx, void *timeout_ctx)
7268{
7269 struct wpa_supplicant *wpa_s = eloop_ctx;
7270
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007271 if (wpa_s->conf->p2p_group_idle == 0 && !wpas_p2p_is_client(wpa_s)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007272 wpa_printf(MSG_DEBUG, "P2P: Ignore group idle timeout - "
7273 "disabled");
7274 return;
7275 }
7276
Dmitry Shmidt04949592012-07-19 12:16:46 -07007277 wpa_printf(MSG_DEBUG, "P2P: Group idle timeout reached - terminate "
7278 "group");
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07007279 wpas_p2p_group_delete(wpa_s, P2P_GROUP_REMOVAL_IDLE_TIMEOUT);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007280}
7281
7282
7283static void wpas_p2p_set_group_idle_timeout(struct wpa_supplicant *wpa_s)
7284{
Dmitry Shmidt04949592012-07-19 12:16:46 -07007285 int timeout;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007286
Dmitry Shmidt04949592012-07-19 12:16:46 -07007287 if (eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL) > 0)
7288 wpa_printf(MSG_DEBUG, "P2P: Cancelled P2P group idle timeout");
7289
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007290 if (wpa_s->current_ssid == NULL || !wpa_s->current_ssid->p2p_group)
7291 return;
7292
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007293 timeout = wpa_s->conf->p2p_group_idle;
7294 if (wpa_s->current_ssid->mode == WPAS_MODE_INFRA &&
7295 (timeout == 0 || timeout > P2P_MAX_CLIENT_IDLE))
7296 timeout = P2P_MAX_CLIENT_IDLE;
7297
7298 if (timeout == 0)
7299 return;
7300
Dmitry Shmidt04949592012-07-19 12:16:46 -07007301 if (timeout < 0) {
7302 if (wpa_s->current_ssid->mode == WPAS_MODE_INFRA)
7303 timeout = 0; /* special client mode no-timeout */
7304 else
7305 return;
7306 }
7307
7308 if (wpa_s->p2p_in_provisioning) {
7309 /*
7310 * Use the normal group formation timeout during the
7311 * provisioning phase to avoid terminating this process too
7312 * early due to group idle timeout.
7313 */
7314 wpa_printf(MSG_DEBUG, "P2P: Do not use P2P group idle timeout "
7315 "during provisioning");
7316 return;
7317 }
Deepthi Gowri9e4e8ac2013-04-16 11:57:05 +05307318
Dmitry Shmidt04949592012-07-19 12:16:46 -07007319 if (wpa_s->show_group_started) {
7320 /*
7321 * Use the normal group formation timeout between the end of
7322 * the provisioning phase and completion of 4-way handshake to
7323 * avoid terminating this process too early due to group idle
7324 * timeout.
7325 */
7326 wpa_printf(MSG_DEBUG, "P2P: Do not use P2P group idle timeout "
7327 "while waiting for initial 4-way handshake to "
7328 "complete");
7329 return;
7330 }
7331
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007332 wpa_printf(MSG_DEBUG, "P2P: Set P2P group idle timeout to %u seconds",
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007333 timeout);
7334 eloop_register_timeout(timeout, 0, wpas_p2p_group_idle_timeout,
7335 wpa_s, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007336}
7337
7338
Jouni Malinen2b89da82012-08-31 22:04:41 +03007339/* Returns 1 if the interface was removed */
7340int wpas_p2p_deauth_notif(struct wpa_supplicant *wpa_s, const u8 *bssid,
7341 u16 reason_code, const u8 *ie, size_t ie_len,
7342 int locally_generated)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007343{
7344 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
Jouni Malinen2b89da82012-08-31 22:04:41 +03007345 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007346
Dmitry Shmidt04949592012-07-19 12:16:46 -07007347 if (!locally_generated)
7348 p2p_deauth_notif(wpa_s->global->p2p, bssid, reason_code, ie,
7349 ie_len);
7350
7351 if (reason_code == WLAN_REASON_DEAUTH_LEAVING && !locally_generated &&
7352 wpa_s->current_ssid &&
7353 wpa_s->current_ssid->p2p_group &&
7354 wpa_s->current_ssid->mode == WPAS_MODE_INFRA) {
7355 wpa_printf(MSG_DEBUG, "P2P: GO indicated that the P2P Group "
7356 "session is ending");
Jouni Malinen2b89da82012-08-31 22:04:41 +03007357 if (wpas_p2p_group_delete(wpa_s,
7358 P2P_GROUP_REMOVAL_GO_ENDING_SESSION)
7359 > 0)
7360 return 1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07007361 }
Jouni Malinen2b89da82012-08-31 22:04:41 +03007362
7363 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007364}
7365
7366
7367void wpas_p2p_disassoc_notif(struct wpa_supplicant *wpa_s, const u8 *bssid,
Dmitry Shmidt04949592012-07-19 12:16:46 -07007368 u16 reason_code, const u8 *ie, size_t ie_len,
7369 int locally_generated)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007370{
7371 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
7372 return;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007373
Dmitry Shmidt04949592012-07-19 12:16:46 -07007374 if (!locally_generated)
7375 p2p_disassoc_notif(wpa_s->global->p2p, bssid, reason_code, ie,
7376 ie_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007377}
7378
7379
7380void wpas_p2p_update_config(struct wpa_supplicant *wpa_s)
7381{
7382 struct p2p_data *p2p = wpa_s->global->p2p;
7383
7384 if (p2p == NULL)
7385 return;
7386
7387 if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_CAPABLE))
7388 return;
7389
7390 if (wpa_s->conf->changed_parameters & CFG_CHANGED_DEVICE_NAME)
7391 p2p_set_dev_name(p2p, wpa_s->conf->device_name);
7392
7393 if (wpa_s->conf->changed_parameters & CFG_CHANGED_DEVICE_TYPE)
7394 p2p_set_pri_dev_type(p2p, wpa_s->conf->device_type);
7395
7396 if (wpa_s->wps &&
7397 (wpa_s->conf->changed_parameters & CFG_CHANGED_CONFIG_METHODS))
7398 p2p_set_config_methods(p2p, wpa_s->wps->config_methods);
7399
7400 if (wpa_s->wps && (wpa_s->conf->changed_parameters & CFG_CHANGED_UUID))
7401 p2p_set_uuid(p2p, wpa_s->wps->uuid);
7402
7403 if (wpa_s->conf->changed_parameters & CFG_CHANGED_WPS_STRING) {
7404 p2p_set_manufacturer(p2p, wpa_s->conf->manufacturer);
7405 p2p_set_model_name(p2p, wpa_s->conf->model_name);
7406 p2p_set_model_number(p2p, wpa_s->conf->model_number);
7407 p2p_set_serial_number(p2p, wpa_s->conf->serial_number);
7408 }
7409
7410 if (wpa_s->conf->changed_parameters & CFG_CHANGED_SEC_DEVICE_TYPE)
7411 p2p_set_sec_dev_types(p2p,
7412 (void *) wpa_s->conf->sec_device_type,
7413 wpa_s->conf->num_sec_device_types);
7414
7415 if (wpa_s->conf->changed_parameters & CFG_CHANGED_VENDOR_EXTENSION) {
7416 int i;
7417 p2p_remove_wps_vendor_extensions(p2p);
7418 for (i = 0; i < MAX_WPS_VENDOR_EXT; i++) {
7419 if (wpa_s->conf->wps_vendor_ext[i] == NULL)
7420 continue;
7421 p2p_add_wps_vendor_extension(
7422 p2p, wpa_s->conf->wps_vendor_ext[i]);
7423 }
7424 }
7425
7426 if ((wpa_s->conf->changed_parameters & CFG_CHANGED_COUNTRY) &&
7427 wpa_s->conf->country[0] && wpa_s->conf->country[1]) {
7428 char country[3];
7429 country[0] = wpa_s->conf->country[0];
7430 country[1] = wpa_s->conf->country[1];
7431 country[2] = 0x04;
7432 p2p_set_country(p2p, country);
7433 }
7434
7435 if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_SSID_POSTFIX) {
7436 p2p_set_ssid_postfix(p2p, (u8 *) wpa_s->conf->p2p_ssid_postfix,
7437 wpa_s->conf->p2p_ssid_postfix ?
7438 os_strlen(wpa_s->conf->p2p_ssid_postfix) :
7439 0);
7440 }
7441
7442 if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_INTRA_BSS)
7443 p2p_set_intra_bss_dist(p2p, wpa_s->conf->p2p_intra_bss);
Jouni Malinen75ecf522011-06-27 15:19:46 -07007444
7445 if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_LISTEN_CHANNEL) {
7446 u8 reg_class, channel;
7447 int ret;
7448 unsigned int r;
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07007449 u8 channel_forced;
7450
Jouni Malinen75ecf522011-06-27 15:19:46 -07007451 if (wpa_s->conf->p2p_listen_reg_class &&
7452 wpa_s->conf->p2p_listen_channel) {
7453 reg_class = wpa_s->conf->p2p_listen_reg_class;
7454 channel = wpa_s->conf->p2p_listen_channel;
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07007455 channel_forced = 1;
Jouni Malinen75ecf522011-06-27 15:19:46 -07007456 } else {
7457 reg_class = 81;
7458 /*
7459 * Pick one of the social channels randomly as the
7460 * listen channel.
7461 */
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07007462 if (os_get_random((u8 *) &r, sizeof(r)) < 0)
7463 channel = 1;
7464 else
7465 channel = 1 + (r % 3) * 5;
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07007466 channel_forced = 0;
Jouni Malinen75ecf522011-06-27 15:19:46 -07007467 }
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07007468 ret = p2p_set_listen_channel(p2p, reg_class, channel,
7469 channel_forced);
Jouni Malinen75ecf522011-06-27 15:19:46 -07007470 if (ret)
7471 wpa_printf(MSG_ERROR, "P2P: Own listen channel update "
7472 "failed: %d", ret);
7473 }
7474 if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_OPER_CHANNEL) {
7475 u8 op_reg_class, op_channel, cfg_op_channel;
7476 int ret = 0;
7477 unsigned int r;
7478 if (wpa_s->conf->p2p_oper_reg_class &&
7479 wpa_s->conf->p2p_oper_channel) {
7480 op_reg_class = wpa_s->conf->p2p_oper_reg_class;
7481 op_channel = wpa_s->conf->p2p_oper_channel;
7482 cfg_op_channel = 1;
7483 } else {
7484 op_reg_class = 81;
7485 /*
7486 * Use random operation channel from (1, 6, 11)
7487 *if no other preference is indicated.
7488 */
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07007489 if (os_get_random((u8 *) &r, sizeof(r)) < 0)
7490 op_channel = 1;
7491 else
7492 op_channel = 1 + (r % 3) * 5;
Jouni Malinen75ecf522011-06-27 15:19:46 -07007493 cfg_op_channel = 0;
7494 }
7495 ret = p2p_set_oper_channel(p2p, op_reg_class, op_channel,
7496 cfg_op_channel);
7497 if (ret)
7498 wpa_printf(MSG_ERROR, "P2P: Own oper channel update "
7499 "failed: %d", ret);
7500 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07007501
7502 if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_PREF_CHAN) {
7503 if (p2p_set_pref_chan(p2p, wpa_s->conf->num_p2p_pref_chan,
7504 wpa_s->conf->p2p_pref_chan) < 0) {
7505 wpa_printf(MSG_ERROR, "P2P: Preferred channel list "
7506 "update failed");
7507 }
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07007508
7509 if (p2p_set_no_go_freq(p2p, &wpa_s->conf->p2p_no_go_freq) < 0) {
7510 wpa_printf(MSG_ERROR, "P2P: No GO channel list "
7511 "update failed");
7512 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07007513 }
Dmitry Shmidt2271d3f2014-06-23 12:16:31 -07007514
7515 if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_PASSPHRASE_LEN)
7516 p2p_set_passphrase_len(p2p, wpa_s->conf->p2p_passphrase_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007517}
7518
7519
7520int wpas_p2p_set_noa(struct wpa_supplicant *wpa_s, u8 count, int start,
7521 int duration)
7522{
7523 if (!wpa_s->ap_iface)
7524 return -1;
7525 return hostapd_p2p_set_noa(wpa_s->ap_iface->bss[0], count, start,
7526 duration);
7527}
7528
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007529
7530int wpas_p2p_set_cross_connect(struct wpa_supplicant *wpa_s, int enabled)
7531{
7532 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
7533 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007534
7535 wpa_s->global->cross_connection = enabled;
7536 p2p_set_cross_connect(wpa_s->global->p2p, enabled);
7537
7538 if (!enabled) {
7539 struct wpa_supplicant *iface;
7540
7541 for (iface = wpa_s->global->ifaces; iface; iface = iface->next)
7542 {
7543 if (iface->cross_connect_enabled == 0)
7544 continue;
7545
7546 iface->cross_connect_enabled = 0;
7547 iface->cross_connect_in_use = 0;
Dmitry Shmidt9c175262016-03-03 10:20:07 -08007548 wpa_msg_global(iface->p2pdev, MSG_INFO,
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07007549 P2P_EVENT_CROSS_CONNECT_DISABLE "%s %s",
7550 iface->ifname,
7551 iface->cross_connect_uplink);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007552 }
7553 }
7554
7555 return 0;
7556}
7557
7558
7559static void wpas_p2p_enable_cross_connect(struct wpa_supplicant *uplink)
7560{
7561 struct wpa_supplicant *iface;
7562
7563 if (!uplink->global->cross_connection)
7564 return;
7565
7566 for (iface = uplink->global->ifaces; iface; iface = iface->next) {
7567 if (!iface->cross_connect_enabled)
7568 continue;
7569 if (os_strcmp(uplink->ifname, iface->cross_connect_uplink) !=
7570 0)
7571 continue;
7572 if (iface->ap_iface == NULL)
7573 continue;
7574 if (iface->cross_connect_in_use)
7575 continue;
7576
7577 iface->cross_connect_in_use = 1;
Dmitry Shmidt9c175262016-03-03 10:20:07 -08007578 wpa_msg_global(iface->p2pdev, MSG_INFO,
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07007579 P2P_EVENT_CROSS_CONNECT_ENABLE "%s %s",
7580 iface->ifname, iface->cross_connect_uplink);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007581 }
7582}
7583
7584
7585static void wpas_p2p_disable_cross_connect(struct wpa_supplicant *uplink)
7586{
7587 struct wpa_supplicant *iface;
7588
7589 for (iface = uplink->global->ifaces; iface; iface = iface->next) {
7590 if (!iface->cross_connect_enabled)
7591 continue;
7592 if (os_strcmp(uplink->ifname, iface->cross_connect_uplink) !=
7593 0)
7594 continue;
7595 if (!iface->cross_connect_in_use)
7596 continue;
7597
Dmitry Shmidt9c175262016-03-03 10:20:07 -08007598 wpa_msg_global(iface->p2pdev, MSG_INFO,
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07007599 P2P_EVENT_CROSS_CONNECT_DISABLE "%s %s",
7600 iface->ifname, iface->cross_connect_uplink);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007601 iface->cross_connect_in_use = 0;
7602 }
7603}
7604
7605
7606void wpas_p2p_notif_connected(struct wpa_supplicant *wpa_s)
7607{
7608 if (wpa_s->ap_iface || wpa_s->current_ssid == NULL ||
7609 wpa_s->current_ssid->mode != WPAS_MODE_INFRA ||
7610 wpa_s->cross_connect_disallowed)
7611 wpas_p2p_disable_cross_connect(wpa_s);
7612 else
7613 wpas_p2p_enable_cross_connect(wpa_s);
Dmitry Shmidt04949592012-07-19 12:16:46 -07007614 if (!wpa_s->ap_iface &&
7615 eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL) > 0)
7616 wpa_printf(MSG_DEBUG, "P2P: Cancelled P2P group idle timeout");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007617}
7618
7619
7620void wpas_p2p_notif_disconnected(struct wpa_supplicant *wpa_s)
7621{
7622 wpas_p2p_disable_cross_connect(wpa_s);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007623 if (!wpa_s->ap_iface &&
7624 !eloop_is_timeout_registered(wpas_p2p_group_idle_timeout,
7625 wpa_s, NULL))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007626 wpas_p2p_set_group_idle_timeout(wpa_s);
7627}
7628
7629
7630static void wpas_p2p_cross_connect_setup(struct wpa_supplicant *wpa_s)
7631{
7632 struct wpa_supplicant *iface;
7633
7634 if (!wpa_s->global->cross_connection)
7635 return;
7636
7637 for (iface = wpa_s->global->ifaces; iface; iface = iface->next) {
7638 if (iface == wpa_s)
7639 continue;
7640 if (iface->drv_flags &
7641 WPA_DRIVER_FLAGS_P2P_DEDICATED_INTERFACE)
7642 continue;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007643 if ((iface->drv_flags & WPA_DRIVER_FLAGS_P2P_CAPABLE) &&
7644 iface != wpa_s->parent)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007645 continue;
7646
7647 wpa_s->cross_connect_enabled = 1;
7648 os_strlcpy(wpa_s->cross_connect_uplink, iface->ifname,
7649 sizeof(wpa_s->cross_connect_uplink));
7650 wpa_printf(MSG_DEBUG, "P2P: Enable cross connection from "
7651 "%s to %s whenever uplink is available",
7652 wpa_s->ifname, wpa_s->cross_connect_uplink);
7653
7654 if (iface->ap_iface || iface->current_ssid == NULL ||
7655 iface->current_ssid->mode != WPAS_MODE_INFRA ||
7656 iface->cross_connect_disallowed ||
7657 iface->wpa_state != WPA_COMPLETED)
7658 break;
7659
7660 wpa_s->cross_connect_in_use = 1;
Dmitry Shmidt9c175262016-03-03 10:20:07 -08007661 wpa_msg_global(wpa_s->p2pdev, MSG_INFO,
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07007662 P2P_EVENT_CROSS_CONNECT_ENABLE "%s %s",
7663 wpa_s->ifname, wpa_s->cross_connect_uplink);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007664 break;
7665 }
7666}
7667
7668
7669int wpas_p2p_notif_pbc_overlap(struct wpa_supplicant *wpa_s)
7670{
7671 if (wpa_s->p2p_group_interface != P2P_GROUP_INTERFACE_CLIENT &&
7672 !wpa_s->p2p_in_provisioning)
7673 return 0; /* not P2P client operation */
7674
7675 wpa_printf(MSG_DEBUG, "P2P: Terminate connection due to WPS PBC "
7676 "session overlap");
Dmitry Shmidt9c175262016-03-03 10:20:07 -08007677 if (wpa_s != wpa_s->p2pdev)
7678 wpa_msg_ctrl(wpa_s->p2pdev, MSG_INFO, WPS_EVENT_OVERLAP);
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07007679 wpas_p2p_group_formation_failed(wpa_s, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007680 return 1;
7681}
7682
7683
Dmitry Shmidtdf5a7e42014-04-02 12:59:59 -07007684void wpas_p2p_pbc_overlap_cb(void *eloop_ctx, void *timeout_ctx)
7685{
7686 struct wpa_supplicant *wpa_s = eloop_ctx;
7687 wpas_p2p_notif_pbc_overlap(wpa_s);
7688}
7689
7690
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08007691void wpas_p2p_update_channel_list(struct wpa_supplicant *wpa_s,
7692 enum wpas_p2p_channel_update_trig trig)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007693{
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07007694 struct p2p_channels chan, cli_chan;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08007695 struct wpa_used_freq_data *freqs = NULL;
7696 unsigned int num = wpa_s->num_multichan_concurrent;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007697
7698 if (wpa_s->global == NULL || wpa_s->global->p2p == NULL)
7699 return;
7700
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08007701 freqs = os_calloc(num, sizeof(struct wpa_used_freq_data));
7702 if (!freqs)
7703 return;
7704
7705 num = get_shared_radio_freqs_data(wpa_s, freqs, num);
7706
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007707 os_memset(&chan, 0, sizeof(chan));
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07007708 os_memset(&cli_chan, 0, sizeof(cli_chan));
7709 if (wpas_p2p_setup_channels(wpa_s, &chan, &cli_chan)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007710 wpa_printf(MSG_ERROR, "P2P: Failed to update supported "
7711 "channel list");
7712 return;
7713 }
7714
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07007715 p2p_update_channel_list(wpa_s->global->p2p, &chan, &cli_chan);
Dmitry Shmidt684785c2014-05-12 13:34:29 -07007716
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08007717 wpas_p2p_optimize_listen_channel(wpa_s, freqs, num);
Dmitry Shmidt684785c2014-05-12 13:34:29 -07007718
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08007719 /*
7720 * The used frequencies map changed, so it is possible that a GO is
7721 * using a channel that is no longer valid for P2P use. It is also
7722 * possible that due to policy consideration, it would be preferable to
7723 * move it to a frequency already used by other station interfaces.
7724 */
7725 wpas_p2p_consider_moving_gos(wpa_s, freqs, num, trig);
7726
7727 os_free(freqs);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007728}
7729
7730
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08007731static void wpas_p2p_scan_res_ignore(struct wpa_supplicant *wpa_s,
7732 struct wpa_scan_results *scan_res)
7733{
7734 wpa_printf(MSG_DEBUG, "P2P: Ignore scan results");
7735}
7736
7737
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007738int wpas_p2p_cancel(struct wpa_supplicant *wpa_s)
7739{
7740 struct wpa_global *global = wpa_s->global;
7741 int found = 0;
7742 const u8 *peer;
7743
7744 if (global->p2p == NULL)
7745 return -1;
7746
7747 wpa_printf(MSG_DEBUG, "P2P: Request to cancel group formation");
7748
7749 if (wpa_s->pending_interface_name[0] &&
7750 !is_zero_ether_addr(wpa_s->pending_interface_addr))
7751 found = 1;
7752
7753 peer = p2p_get_go_neg_peer(global->p2p);
7754 if (peer) {
7755 wpa_printf(MSG_DEBUG, "P2P: Unauthorize pending GO Neg peer "
7756 MACSTR, MAC2STR(peer));
7757 p2p_unauthorize(global->p2p, peer);
Dmitry Shmidt04949592012-07-19 12:16:46 -07007758 found = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007759 }
7760
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08007761 if (wpa_s->scan_res_handler == wpas_p2p_scan_res_join) {
7762 wpa_printf(MSG_DEBUG, "P2P: Stop pending scan for join");
7763 wpa_s->scan_res_handler = wpas_p2p_scan_res_ignore;
7764 found = 1;
7765 }
7766
7767 if (wpa_s->pending_pd_before_join) {
7768 wpa_printf(MSG_DEBUG, "P2P: Stop pending PD before join");
7769 wpa_s->pending_pd_before_join = 0;
7770 found = 1;
7771 }
7772
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007773 wpas_p2p_stop_find(wpa_s);
7774
7775 for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
7776 if (wpa_s == global->p2p_group_formation &&
7777 (wpa_s->p2p_in_provisioning ||
7778 wpa_s->parent->pending_interface_type ==
7779 WPA_IF_P2P_CLIENT)) {
7780 wpa_printf(MSG_DEBUG, "P2P: Interface %s in group "
7781 "formation found - cancelling",
7782 wpa_s->ifname);
7783 found = 1;
7784 eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
Dmitry Shmidt9c175262016-03-03 10:20:07 -08007785 wpa_s->p2pdev, NULL);
Jouni Malinenadddfc42012-10-03 14:31:41 -07007786 if (wpa_s->p2p_in_provisioning) {
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07007787 wpas_group_formation_completed(wpa_s, 0, 0);
Jouni Malinenadddfc42012-10-03 14:31:41 -07007788 break;
7789 }
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07007790 wpas_p2p_group_delete(wpa_s,
7791 P2P_GROUP_REMOVAL_REQUESTED);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007792 break;
Dmitry Shmidt21de2142014-04-08 10:50:52 -07007793 } else if (wpa_s->p2p_in_invitation) {
7794 wpa_printf(MSG_DEBUG, "P2P: Interface %s in invitation found - cancelling",
7795 wpa_s->ifname);
7796 found = 1;
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07007797 wpas_p2p_group_formation_failed(wpa_s, 0);
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08007798 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007799 }
7800 }
7801
7802 if (!found) {
7803 wpa_printf(MSG_DEBUG, "P2P: No ongoing group formation found");
7804 return -1;
7805 }
7806
7807 return 0;
7808}
7809
7810
7811void wpas_p2p_interface_unavailable(struct wpa_supplicant *wpa_s)
7812{
7813 if (wpa_s->current_ssid == NULL || !wpa_s->current_ssid->p2p_group)
7814 return;
7815
7816 wpa_printf(MSG_DEBUG, "P2P: Remove group due to driver resource not "
7817 "being available anymore");
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07007818 wpas_p2p_group_delete(wpa_s, P2P_GROUP_REMOVAL_UNAVAILABLE);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007819}
7820
7821
7822void wpas_p2p_update_best_channels(struct wpa_supplicant *wpa_s,
7823 int freq_24, int freq_5, int freq_overall)
7824{
7825 struct p2p_data *p2p = wpa_s->global->p2p;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08007826 if (p2p == NULL)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007827 return;
7828 p2p_set_best_channels(p2p, freq_24, freq_5, freq_overall);
7829}
7830
7831
7832int wpas_p2p_unauthorize(struct wpa_supplicant *wpa_s, const char *addr)
7833{
7834 u8 peer[ETH_ALEN];
7835 struct p2p_data *p2p = wpa_s->global->p2p;
7836
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08007837 if (p2p == NULL)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007838 return -1;
7839
7840 if (hwaddr_aton(addr, peer))
7841 return -1;
7842
7843 return p2p_unauthorize(p2p, peer);
7844}
7845
7846
7847/**
7848 * wpas_p2p_disconnect - Disconnect from a P2P Group
7849 * @wpa_s: Pointer to wpa_supplicant data
7850 * Returns: 0 on success, -1 on failure
7851 *
7852 * This can be used to disconnect from a group in which the local end is a P2P
7853 * Client or to end a P2P Group in case the local end is the Group Owner. If a
7854 * virtual network interface was created for this group, that interface will be
7855 * removed. Otherwise, only the configured P2P group network will be removed
7856 * from the interface.
7857 */
7858int wpas_p2p_disconnect(struct wpa_supplicant *wpa_s)
7859{
7860
7861 if (wpa_s == NULL)
7862 return -1;
7863
Jouni Malinen2b89da82012-08-31 22:04:41 +03007864 return wpas_p2p_group_delete(wpa_s, P2P_GROUP_REMOVAL_REQUESTED) < 0 ?
7865 -1 : 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007866}
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007867
7868
7869int wpas_p2p_in_progress(struct wpa_supplicant *wpa_s)
7870{
Dmitry Shmidtf8623282013-02-20 14:34:59 -08007871 int ret;
7872
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007873 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
7874 return 0;
7875
Dmitry Shmidtf8623282013-02-20 14:34:59 -08007876 ret = p2p_in_progress(wpa_s->global->p2p);
7877 if (ret == 0) {
7878 /*
7879 * Check whether there is an ongoing WPS provisioning step (or
7880 * other parts of group formation) on another interface since
7881 * p2p_in_progress() does not report this to avoid issues for
7882 * scans during such provisioning step.
7883 */
7884 if (wpa_s->global->p2p_group_formation &&
7885 wpa_s->global->p2p_group_formation != wpa_s) {
7886 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Another interface (%s) "
7887 "in group formation",
7888 wpa_s->global->p2p_group_formation->ifname);
7889 ret = 1;
7890 }
7891 }
7892
Dmitry Shmidt92c368d2013-08-29 12:37:21 -07007893 if (!ret && wpa_s->global->p2p_go_wait_client.sec) {
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08007894 struct os_reltime now;
7895 os_get_reltime(&now);
7896 if (os_reltime_expired(&now, &wpa_s->global->p2p_go_wait_client,
7897 P2P_MAX_INITIAL_CONN_WAIT_GO)) {
Dmitry Shmidt92c368d2013-08-29 12:37:21 -07007898 /* Wait for the first client has expired */
7899 wpa_s->global->p2p_go_wait_client.sec = 0;
7900 } else {
7901 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Waiting for initial client connection during group formation");
7902 ret = 1;
7903 }
7904 }
7905
Dmitry Shmidtf8623282013-02-20 14:34:59 -08007906 return ret;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007907}
7908
7909
7910void wpas_p2p_network_removed(struct wpa_supplicant *wpa_s,
7911 struct wpa_ssid *ssid)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007912{
7913 if (wpa_s->p2p_in_provisioning && ssid->p2p_group &&
7914 eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
Dmitry Shmidt9c175262016-03-03 10:20:07 -08007915 wpa_s->p2pdev, NULL) > 0) {
Dmitry Shmidt04949592012-07-19 12:16:46 -07007916 /**
7917 * Remove the network by scheduling the group formation
7918 * timeout to happen immediately. The teardown code
7919 * needs to be scheduled to run asynch later so that we
7920 * don't delete data from under ourselves unexpectedly.
7921 * Calling wpas_p2p_group_formation_timeout directly
7922 * causes a series of crashes in WPS failure scenarios.
7923 */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007924 wpa_printf(MSG_DEBUG, "P2P: Canceled group formation due to "
7925 "P2P group network getting removed");
Dmitry Shmidt04949592012-07-19 12:16:46 -07007926 eloop_register_timeout(0, 0, wpas_p2p_group_formation_timeout,
Dmitry Shmidt9c175262016-03-03 10:20:07 -08007927 wpa_s->p2pdev, NULL);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007928 }
7929}
7930
7931
7932struct wpa_ssid * wpas_p2p_get_persistent(struct wpa_supplicant *wpa_s,
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08007933 const u8 *addr, const u8 *ssid,
7934 size_t ssid_len)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007935{
7936 struct wpa_ssid *s;
7937 size_t i;
7938
7939 for (s = wpa_s->conf->ssid; s; s = s->next) {
7940 if (s->disabled != 2)
7941 continue;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08007942 if (ssid &&
7943 (ssid_len != s->ssid_len ||
7944 os_memcmp(ssid, s->ssid, ssid_len) != 0))
7945 continue;
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07007946 if (addr == NULL) {
7947 if (s->mode == WPAS_MODE_P2P_GO)
7948 return s;
7949 continue;
7950 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007951 if (os_memcmp(s->bssid, addr, ETH_ALEN) == 0)
7952 return s; /* peer is GO in the persistent group */
7953 if (s->mode != WPAS_MODE_P2P_GO || s->p2p_client_list == NULL)
7954 continue;
7955 for (i = 0; i < s->num_p2p_clients; i++) {
Dmitry Shmidtff787d52015-01-12 13:01:47 -08007956 if (os_memcmp(s->p2p_client_list + i * 2 * ETH_ALEN,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007957 addr, ETH_ALEN) == 0)
7958 return s; /* peer is P2P client in persistent
7959 * group */
7960 }
7961 }
7962
7963 return NULL;
7964}
7965
7966
7967void wpas_p2p_notify_ap_sta_authorized(struct wpa_supplicant *wpa_s,
7968 const u8 *addr)
7969{
Dmitry Shmidt56052862013-10-04 10:23:25 -07007970 if (eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
Dmitry Shmidt9c175262016-03-03 10:20:07 -08007971 wpa_s->p2pdev, NULL) > 0) {
Dmitry Shmidt56052862013-10-04 10:23:25 -07007972 /*
7973 * This can happen if WPS provisioning step is not terminated
7974 * cleanly (e.g., P2P Client does not send WSC_Done). Since the
7975 * peer was able to connect, there is no need to time out group
7976 * formation after this, though. In addition, this is used with
7977 * the initial connection wait on the GO as a separate formation
7978 * timeout and as such, expected to be hit after the initial WPS
7979 * provisioning step.
7980 */
7981 wpa_printf(MSG_DEBUG, "P2P: Canceled P2P group formation timeout on data connection");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007982
7983 if (!wpa_s->p2p_go_group_formation_completed &&
7984 !wpa_s->group_formation_reported) {
7985 /*
7986 * GO has not yet notified group formation success since
7987 * the WPS step was not completed cleanly. Do that
7988 * notification now since the P2P Client was able to
7989 * connect and as such, must have received the
7990 * credential from the WPS step.
7991 */
7992 if (wpa_s->global->p2p)
7993 p2p_wps_success_cb(wpa_s->global->p2p, addr);
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07007994 wpas_group_formation_completed(wpa_s, 1, 0);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007995 }
Dmitry Shmidt56052862013-10-04 10:23:25 -07007996 }
7997 if (!wpa_s->p2p_go_group_formation_completed) {
7998 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Marking group formation completed on GO on first data connection");
7999 wpa_s->p2p_go_group_formation_completed = 1;
8000 wpa_s->global->p2p_group_formation = NULL;
8001 wpa_s->p2p_in_provisioning = 0;
Dmitry Shmidt21de2142014-04-08 10:50:52 -07008002 wpa_s->p2p_in_invitation = 0;
Dmitry Shmidt56052862013-10-04 10:23:25 -07008003 }
Dmitry Shmidt92c368d2013-08-29 12:37:21 -07008004 wpa_s->global->p2p_go_wait_client.sec = 0;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008005 if (addr == NULL)
8006 return;
8007 wpas_p2p_add_persistent_group_client(wpa_s, addr);
8008}
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08008009
Dmitry Shmidt04949592012-07-19 12:16:46 -07008010
Dmitry Shmidt7f656022015-02-25 14:36:37 -08008011static int wpas_p2p_fallback_to_go_neg(struct wpa_supplicant *wpa_s,
8012 int group_added)
Dmitry Shmidt04949592012-07-19 12:16:46 -07008013{
8014 struct wpa_supplicant *group = wpa_s;
Dmitry Shmidt7f656022015-02-25 14:36:37 -08008015 int ret = 0;
8016
Dmitry Shmidt04949592012-07-19 12:16:46 -07008017 if (wpa_s->global->p2p_group_formation)
8018 group = wpa_s->global->p2p_group_formation;
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07008019 wpa_s = wpa_s->global->p2p_init_wpa_s;
Dmitry Shmidt04949592012-07-19 12:16:46 -07008020 offchannel_send_action_done(wpa_s);
8021 if (group_added)
Dmitry Shmidt7f656022015-02-25 14:36:37 -08008022 ret = wpas_p2p_group_delete(group, P2P_GROUP_REMOVAL_SILENT);
Dmitry Shmidt04949592012-07-19 12:16:46 -07008023 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Fall back to GO Negotiation");
8024 wpas_p2p_connect(wpa_s, wpa_s->pending_join_dev_addr, wpa_s->p2p_pin,
8025 wpa_s->p2p_wps_method, wpa_s->p2p_persistent_group, 0,
8026 0, 0, wpa_s->p2p_go_intent, wpa_s->p2p_connect_freq,
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08008027 wpa_s->p2p_go_vht_center_freq2,
Dmitry Shmidt04949592012-07-19 12:16:46 -07008028 wpa_s->p2p_persistent_id,
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07008029 wpa_s->p2p_pd_before_go_neg,
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07008030 wpa_s->p2p_go_ht40,
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08008031 wpa_s->p2p_go_vht,
Dmitry Shmidtde47be72016-01-07 12:52:55 -08008032 wpa_s->p2p_go_max_oper_chwidth, NULL, 0);
Dmitry Shmidt7f656022015-02-25 14:36:37 -08008033 return ret;
Dmitry Shmidt04949592012-07-19 12:16:46 -07008034}
8035
8036
8037int wpas_p2p_scan_no_go_seen(struct wpa_supplicant *wpa_s)
8038{
Dmitry Shmidt7f656022015-02-25 14:36:37 -08008039 int res;
8040
Dmitry Shmidt04949592012-07-19 12:16:46 -07008041 if (!wpa_s->p2p_fallback_to_go_neg ||
8042 wpa_s->p2p_in_provisioning <= 5)
8043 return 0;
8044
8045 if (wpas_p2p_peer_go(wpa_s, wpa_s->pending_join_dev_addr) > 0)
8046 return 0; /* peer operating as a GO */
8047
8048 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: GO not found for p2p_connect-auto - "
8049 "fallback to GO Negotiation");
Dmitry Shmidt9c175262016-03-03 10:20:07 -08008050 wpa_msg_global(wpa_s->p2pdev, MSG_INFO, P2P_EVENT_FALLBACK_TO_GO_NEG
Dmitry Shmidt7f656022015-02-25 14:36:37 -08008051 "reason=GO-not-found");
8052 res = wpas_p2p_fallback_to_go_neg(wpa_s, 1);
Dmitry Shmidt04949592012-07-19 12:16:46 -07008053
Dmitry Shmidt7f656022015-02-25 14:36:37 -08008054 return res == 1 ? 2 : 1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07008055}
8056
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07008057
8058unsigned int wpas_p2p_search_delay(struct wpa_supplicant *wpa_s)
8059{
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07008060 struct wpa_supplicant *ifs;
8061
8062 if (wpa_s->wpa_state > WPA_SCANNING) {
8063 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Use %u ms search delay due to "
8064 "concurrent operation",
Dmitry Shmidt09f57ba2014-06-10 16:07:13 -07008065 wpa_s->conf->p2p_search_delay);
8066 return wpa_s->conf->p2p_search_delay;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07008067 }
8068
Dmitry Shmidt01904cf2013-12-05 11:08:35 -08008069 dl_list_for_each(ifs, &wpa_s->radio->ifaces, struct wpa_supplicant,
8070 radio_list) {
8071 if (ifs != wpa_s && ifs->wpa_state > WPA_SCANNING) {
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07008072 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Use %u ms search "
8073 "delay due to concurrent operation on "
8074 "interface %s",
Dmitry Shmidt09f57ba2014-06-10 16:07:13 -07008075 wpa_s->conf->p2p_search_delay,
8076 ifs->ifname);
8077 return wpa_s->conf->p2p_search_delay;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07008078 }
8079 }
8080
8081 return 0;
8082}
8083
Dmitry Shmidt37d4d6a2013-03-18 13:09:42 -07008084
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07008085static int wpas_p2p_remove_psk_entry(struct wpa_supplicant *wpa_s,
8086 struct wpa_ssid *s, const u8 *addr,
8087 int iface_addr)
8088{
8089 struct psk_list_entry *psk, *tmp;
8090 int changed = 0;
8091
8092 dl_list_for_each_safe(psk, tmp, &s->psk_list, struct psk_list_entry,
8093 list) {
8094 if ((iface_addr && !psk->p2p &&
8095 os_memcmp(addr, psk->addr, ETH_ALEN) == 0) ||
8096 (!iface_addr && psk->p2p &&
8097 os_memcmp(addr, psk->addr, ETH_ALEN) == 0)) {
8098 wpa_dbg(wpa_s, MSG_DEBUG,
8099 "P2P: Remove persistent group PSK list entry for "
8100 MACSTR " p2p=%u",
8101 MAC2STR(psk->addr), psk->p2p);
8102 dl_list_del(&psk->list);
8103 os_free(psk);
8104 changed++;
8105 }
8106 }
8107
8108 return changed;
8109}
8110
8111
8112void wpas_p2p_new_psk_cb(struct wpa_supplicant *wpa_s, const u8 *mac_addr,
8113 const u8 *p2p_dev_addr,
8114 const u8 *psk, size_t psk_len)
8115{
8116 struct wpa_ssid *ssid = wpa_s->current_ssid;
8117 struct wpa_ssid *persistent;
Dmitry Shmidt7832adb2014-04-29 10:53:02 -07008118 struct psk_list_entry *p, *last;
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07008119
8120 if (psk_len != sizeof(p->psk))
8121 return;
8122
8123 if (p2p_dev_addr) {
8124 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: New PSK for addr=" MACSTR
8125 " p2p_dev_addr=" MACSTR,
8126 MAC2STR(mac_addr), MAC2STR(p2p_dev_addr));
8127 if (is_zero_ether_addr(p2p_dev_addr))
8128 p2p_dev_addr = NULL;
8129 } else {
8130 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: New PSK for addr=" MACSTR,
8131 MAC2STR(mac_addr));
8132 }
8133
8134 if (ssid->mode == WPAS_MODE_P2P_GROUP_FORMATION) {
8135 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: new_psk_cb during group formation");
8136 /* To be added to persistent group once created */
8137 if (wpa_s->global->add_psk == NULL) {
8138 wpa_s->global->add_psk = os_zalloc(sizeof(*p));
8139 if (wpa_s->global->add_psk == NULL)
8140 return;
8141 }
8142 p = wpa_s->global->add_psk;
8143 if (p2p_dev_addr) {
8144 p->p2p = 1;
8145 os_memcpy(p->addr, p2p_dev_addr, ETH_ALEN);
8146 } else {
8147 p->p2p = 0;
8148 os_memcpy(p->addr, mac_addr, ETH_ALEN);
8149 }
8150 os_memcpy(p->psk, psk, psk_len);
8151 return;
8152 }
8153
8154 if (ssid->mode != WPAS_MODE_P2P_GO || !ssid->p2p_persistent_group) {
8155 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Ignore new_psk_cb on not-persistent GO");
8156 return;
8157 }
8158
Dmitry Shmidt9c175262016-03-03 10:20:07 -08008159 persistent = wpas_p2p_get_persistent(wpa_s->p2pdev, NULL, ssid->ssid,
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07008160 ssid->ssid_len);
8161 if (!persistent) {
8162 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Could not find persistent group information to store the new PSK");
8163 return;
8164 }
8165
8166 p = os_zalloc(sizeof(*p));
8167 if (p == NULL)
8168 return;
8169 if (p2p_dev_addr) {
8170 p->p2p = 1;
8171 os_memcpy(p->addr, p2p_dev_addr, ETH_ALEN);
8172 } else {
8173 p->p2p = 0;
8174 os_memcpy(p->addr, mac_addr, ETH_ALEN);
8175 }
8176 os_memcpy(p->psk, psk, psk_len);
8177
Dmitry Shmidt7832adb2014-04-29 10:53:02 -07008178 if (dl_list_len(&persistent->psk_list) > P2P_MAX_STORED_CLIENTS &&
8179 (last = dl_list_last(&persistent->psk_list,
8180 struct psk_list_entry, list))) {
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07008181 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Remove oldest PSK entry for "
8182 MACSTR " (p2p=%u) to make room for a new one",
8183 MAC2STR(last->addr), last->p2p);
8184 dl_list_del(&last->list);
8185 os_free(last);
8186 }
8187
Dmitry Shmidt9c175262016-03-03 10:20:07 -08008188 wpas_p2p_remove_psk_entry(wpa_s->p2pdev, persistent,
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07008189 p2p_dev_addr ? p2p_dev_addr : mac_addr,
8190 p2p_dev_addr == NULL);
8191 if (p2p_dev_addr) {
8192 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Add new PSK for p2p_dev_addr="
8193 MACSTR, MAC2STR(p2p_dev_addr));
8194 } else {
8195 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Add new PSK for addr=" MACSTR,
8196 MAC2STR(mac_addr));
8197 }
8198 dl_list_add(&persistent->psk_list, &p->list);
8199
Dmitry Shmidt9c175262016-03-03 10:20:07 -08008200 if (wpa_s->p2pdev->conf->update_config &&
8201 wpa_config_write(wpa_s->p2pdev->confname, wpa_s->p2pdev->conf))
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07008202 wpa_printf(MSG_DEBUG, "P2P: Failed to update configuration");
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07008203}
8204
8205
8206static void wpas_p2p_remove_psk(struct wpa_supplicant *wpa_s,
8207 struct wpa_ssid *s, const u8 *addr,
8208 int iface_addr)
8209{
8210 int res;
8211
8212 res = wpas_p2p_remove_psk_entry(wpa_s, s, addr, iface_addr);
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08008213 if (res > 0 && wpa_s->conf->update_config &&
8214 wpa_config_write(wpa_s->confname, wpa_s->conf))
8215 wpa_dbg(wpa_s, MSG_DEBUG,
8216 "P2P: Failed to update configuration");
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07008217}
8218
8219
8220static void wpas_p2p_remove_client_go(struct wpa_supplicant *wpa_s,
8221 const u8 *peer, int iface_addr)
8222{
8223 struct hostapd_data *hapd;
8224 struct hostapd_wpa_psk *psk, *prev, *rem;
8225 struct sta_info *sta;
8226
8227 if (wpa_s->ap_iface == NULL || wpa_s->current_ssid == NULL ||
8228 wpa_s->current_ssid->mode != WPAS_MODE_P2P_GO)
8229 return;
8230
8231 /* Remove per-station PSK entry */
8232 hapd = wpa_s->ap_iface->bss[0];
8233 prev = NULL;
8234 psk = hapd->conf->ssid.wpa_psk;
8235 while (psk) {
8236 if ((iface_addr && os_memcmp(peer, psk->addr, ETH_ALEN) == 0) ||
8237 (!iface_addr &&
8238 os_memcmp(peer, psk->p2p_dev_addr, ETH_ALEN) == 0)) {
8239 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Remove operating group PSK entry for "
8240 MACSTR " iface_addr=%d",
8241 MAC2STR(peer), iface_addr);
8242 if (prev)
8243 prev->next = psk->next;
8244 else
8245 hapd->conf->ssid.wpa_psk = psk->next;
8246 rem = psk;
8247 psk = psk->next;
8248 os_free(rem);
8249 } else {
8250 prev = psk;
8251 psk = psk->next;
8252 }
8253 }
8254
8255 /* Disconnect from group */
8256 if (iface_addr)
8257 sta = ap_get_sta(hapd, peer);
8258 else
8259 sta = ap_get_sta_p2p(hapd, peer);
8260 if (sta) {
8261 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Disconnect peer " MACSTR
8262 " (iface_addr=%d) from group",
8263 MAC2STR(peer), iface_addr);
8264 hostapd_drv_sta_deauth(hapd, sta->addr,
8265 WLAN_REASON_DEAUTH_LEAVING);
8266 ap_sta_deauthenticate(hapd, sta, WLAN_REASON_DEAUTH_LEAVING);
8267 }
8268}
8269
8270
8271void wpas_p2p_remove_client(struct wpa_supplicant *wpa_s, const u8 *peer,
8272 int iface_addr)
8273{
8274 struct wpa_ssid *s;
8275 struct wpa_supplicant *w;
Dmitry Shmidt8bd70b72015-05-26 16:02:19 -07008276 struct wpa_supplicant *p2p_wpa_s = wpa_s->global->p2p_init_wpa_s;
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07008277
8278 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Remove client " MACSTR, MAC2STR(peer));
8279
8280 /* Remove from any persistent group */
Dmitry Shmidt8bd70b72015-05-26 16:02:19 -07008281 for (s = p2p_wpa_s->conf->ssid; s; s = s->next) {
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07008282 if (s->disabled != 2 || s->mode != WPAS_MODE_P2P_GO)
8283 continue;
8284 if (!iface_addr)
Dmitry Shmidt8bd70b72015-05-26 16:02:19 -07008285 wpas_remove_persistent_peer(p2p_wpa_s, s, peer, 0);
8286 wpas_p2p_remove_psk(p2p_wpa_s, s, peer, iface_addr);
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07008287 }
8288
8289 /* Remove from any operating group */
8290 for (w = wpa_s->global->ifaces; w; w = w->next)
8291 wpas_p2p_remove_client_go(w, peer, iface_addr);
8292}
8293
8294
8295static void wpas_p2p_psk_failure_removal(void *eloop_ctx, void *timeout_ctx)
8296{
8297 struct wpa_supplicant *wpa_s = eloop_ctx;
8298 wpas_p2p_group_delete(wpa_s, P2P_GROUP_REMOVAL_PSK_FAILURE);
8299}
8300
8301
Dmitry Shmidt04f534e2013-12-09 15:50:16 -08008302static void wpas_p2p_group_freq_conflict(void *eloop_ctx, void *timeout_ctx)
8303{
8304 struct wpa_supplicant *wpa_s = eloop_ctx;
8305
8306 wpa_printf(MSG_DEBUG, "P2P: Frequency conflict - terminate group");
8307 wpas_p2p_group_delete(wpa_s, P2P_GROUP_REMOVAL_FREQ_CONFLICT);
8308}
8309
8310
8311int wpas_p2p_handle_frequency_conflicts(struct wpa_supplicant *wpa_s, int freq,
8312 struct wpa_ssid *ssid)
8313{
8314 struct wpa_supplicant *iface;
8315
8316 for (iface = wpa_s->global->ifaces; iface; iface = iface->next) {
8317 if (!iface->current_ssid ||
8318 iface->current_ssid->frequency == freq ||
8319 (iface->p2p_group_interface == NOT_P2P_GROUP_INTERFACE &&
8320 !iface->current_ssid->p2p_group))
8321 continue;
8322
8323 /* Remove the connection with least priority */
8324 if (!wpas_is_p2p_prioritized(iface)) {
8325 /* STA connection has priority over existing
8326 * P2P connection, so remove the interface. */
8327 wpa_printf(MSG_DEBUG, "P2P: Removing P2P connection due to single channel concurrent mode frequency conflict");
8328 eloop_register_timeout(0, 0,
8329 wpas_p2p_group_freq_conflict,
8330 iface, NULL);
8331 /* If connection in progress is P2P connection, do not
8332 * proceed for the connection. */
8333 if (wpa_s == iface)
8334 return -1;
8335 else
8336 return 0;
8337 } else {
8338 /* P2P connection has priority, disable the STA network
8339 */
8340 wpa_supplicant_disable_network(wpa_s->global->ifaces,
8341 ssid);
8342 wpa_msg(wpa_s->global->ifaces, MSG_INFO,
8343 WPA_EVENT_FREQ_CONFLICT " id=%d", ssid->id);
8344 os_memset(wpa_s->global->ifaces->pending_bssid, 0,
8345 ETH_ALEN);
8346 /* If P2P connection is in progress, continue
8347 * connecting...*/
8348 if (wpa_s == iface)
8349 return 0;
8350 else
8351 return -1;
8352 }
8353 }
8354
8355 return 0;
8356}
8357
8358
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07008359int wpas_p2p_4way_hs_failed(struct wpa_supplicant *wpa_s)
8360{
8361 struct wpa_ssid *ssid = wpa_s->current_ssid;
8362
8363 if (ssid == NULL || !ssid->p2p_group)
8364 return 0;
8365
8366 if (wpa_s->p2p_last_4way_hs_fail &&
8367 wpa_s->p2p_last_4way_hs_fail == ssid) {
8368 u8 go_dev_addr[ETH_ALEN];
8369 struct wpa_ssid *persistent;
8370
8371 if (wpas_p2p_persistent_group(wpa_s, go_dev_addr,
8372 ssid->ssid,
8373 ssid->ssid_len) <= 0) {
8374 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Could not determine whether 4-way handshake failures were for a persistent group");
8375 goto disconnect;
8376 }
8377
8378 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Two 4-way handshake failures for a P2P group - go_dev_addr="
8379 MACSTR, MAC2STR(go_dev_addr));
Dmitry Shmidt9c175262016-03-03 10:20:07 -08008380 persistent = wpas_p2p_get_persistent(wpa_s->p2pdev, go_dev_addr,
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07008381 ssid->ssid,
8382 ssid->ssid_len);
8383 if (persistent == NULL || persistent->mode != WPAS_MODE_INFRA) {
8384 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: No matching persistent group stored");
8385 goto disconnect;
8386 }
Dmitry Shmidt9c175262016-03-03 10:20:07 -08008387 wpa_msg_global(wpa_s->p2pdev, MSG_INFO,
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07008388 P2P_EVENT_PERSISTENT_PSK_FAIL "%d",
8389 persistent->id);
8390 disconnect:
8391 wpa_s->p2p_last_4way_hs_fail = NULL;
8392 /*
8393 * Remove the group from a timeout to avoid issues with caller
8394 * continuing to use the interface if this is on a P2P group
8395 * interface.
8396 */
8397 eloop_register_timeout(0, 0, wpas_p2p_psk_failure_removal,
8398 wpa_s, NULL);
8399 return 1;
8400 }
8401
8402 wpa_s->p2p_last_4way_hs_fail = ssid;
8403 return 0;
8404}
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08008405
8406
8407#ifdef CONFIG_WPS_NFC
8408
8409static struct wpabuf * wpas_p2p_nfc_handover(int ndef, struct wpabuf *wsc,
8410 struct wpabuf *p2p)
8411{
8412 struct wpabuf *ret;
8413 size_t wsc_len;
8414
8415 if (p2p == NULL) {
8416 wpabuf_free(wsc);
8417 wpa_printf(MSG_DEBUG, "P2P: No p2p buffer for handover");
8418 return NULL;
8419 }
8420
8421 wsc_len = wsc ? wpabuf_len(wsc) : 0;
8422 ret = wpabuf_alloc(2 + wsc_len + 2 + wpabuf_len(p2p));
8423 if (ret == NULL) {
8424 wpabuf_free(wsc);
8425 wpabuf_free(p2p);
8426 return NULL;
8427 }
8428
8429 wpabuf_put_be16(ret, wsc_len);
8430 if (wsc)
8431 wpabuf_put_buf(ret, wsc);
8432 wpabuf_put_be16(ret, wpabuf_len(p2p));
8433 wpabuf_put_buf(ret, p2p);
8434
8435 wpabuf_free(wsc);
8436 wpabuf_free(p2p);
8437 wpa_hexdump_buf(MSG_DEBUG,
8438 "P2P: Generated NFC connection handover message", ret);
8439
8440 if (ndef && ret) {
8441 struct wpabuf *tmp;
8442 tmp = ndef_build_p2p(ret);
8443 wpabuf_free(ret);
8444 if (tmp == NULL) {
8445 wpa_printf(MSG_DEBUG, "P2P: Failed to NDEF encapsulate handover request");
8446 return NULL;
8447 }
8448 ret = tmp;
8449 }
8450
8451 return ret;
8452}
8453
8454
8455static int wpas_p2p_cli_freq(struct wpa_supplicant *wpa_s,
8456 struct wpa_ssid **ssid, u8 *go_dev_addr)
8457{
8458 struct wpa_supplicant *iface;
8459
8460 if (go_dev_addr)
8461 os_memset(go_dev_addr, 0, ETH_ALEN);
8462 if (ssid)
8463 *ssid = NULL;
8464 for (iface = wpa_s->global->ifaces; iface; iface = iface->next) {
8465 if (iface->wpa_state < WPA_ASSOCIATING ||
8466 iface->current_ssid == NULL || iface->assoc_freq == 0 ||
8467 !iface->current_ssid->p2p_group ||
8468 iface->current_ssid->mode != WPAS_MODE_INFRA)
8469 continue;
8470 if (ssid)
8471 *ssid = iface->current_ssid;
8472 if (go_dev_addr)
8473 os_memcpy(go_dev_addr, iface->go_dev_addr, ETH_ALEN);
8474 return iface->assoc_freq;
8475 }
8476 return 0;
8477}
8478
8479
8480struct wpabuf * wpas_p2p_nfc_handover_req(struct wpa_supplicant *wpa_s,
8481 int ndef)
8482{
8483 struct wpabuf *wsc, *p2p;
8484 struct wpa_ssid *ssid;
8485 u8 go_dev_addr[ETH_ALEN];
8486 int cli_freq = wpas_p2p_cli_freq(wpa_s, &ssid, go_dev_addr);
8487
8488 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL) {
8489 wpa_printf(MSG_DEBUG, "P2P: P2P disabled - cannot build handover request");
8490 return NULL;
8491 }
8492
8493 if (wpa_s->conf->wps_nfc_dh_pubkey == NULL &&
8494 wps_nfc_gen_dh(&wpa_s->conf->wps_nfc_dh_pubkey,
8495 &wpa_s->conf->wps_nfc_dh_privkey) < 0) {
8496 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: No DH key available for handover request");
8497 return NULL;
8498 }
8499
8500 if (cli_freq == 0) {
8501 wsc = wps_build_nfc_handover_req_p2p(
8502 wpa_s->parent->wps, wpa_s->conf->wps_nfc_dh_pubkey);
8503 } else
8504 wsc = NULL;
8505 p2p = p2p_build_nfc_handover_req(wpa_s->global->p2p, cli_freq,
8506 go_dev_addr, ssid ? ssid->ssid : NULL,
8507 ssid ? ssid->ssid_len : 0);
8508
8509 return wpas_p2p_nfc_handover(ndef, wsc, p2p);
8510}
8511
8512
8513struct wpabuf * wpas_p2p_nfc_handover_sel(struct wpa_supplicant *wpa_s,
8514 int ndef, int tag)
8515{
8516 struct wpabuf *wsc, *p2p;
8517 struct wpa_ssid *ssid;
8518 u8 go_dev_addr[ETH_ALEN];
8519 int cli_freq = wpas_p2p_cli_freq(wpa_s, &ssid, go_dev_addr);
8520
8521 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
8522 return NULL;
8523
8524 if (!tag && wpa_s->conf->wps_nfc_dh_pubkey == NULL &&
8525 wps_nfc_gen_dh(&wpa_s->conf->wps_nfc_dh_pubkey,
8526 &wpa_s->conf->wps_nfc_dh_privkey) < 0)
8527 return NULL;
8528
8529 if (cli_freq == 0) {
8530 wsc = wps_build_nfc_handover_sel_p2p(
8531 wpa_s->parent->wps,
8532 tag ? wpa_s->conf->wps_nfc_dev_pw_id :
8533 DEV_PW_NFC_CONNECTION_HANDOVER,
8534 wpa_s->conf->wps_nfc_dh_pubkey,
8535 tag ? wpa_s->conf->wps_nfc_dev_pw : NULL);
8536 } else
8537 wsc = NULL;
8538 p2p = p2p_build_nfc_handover_sel(wpa_s->global->p2p, cli_freq,
8539 go_dev_addr, ssid ? ssid->ssid : NULL,
8540 ssid ? ssid->ssid_len : 0);
8541
8542 return wpas_p2p_nfc_handover(ndef, wsc, p2p);
8543}
8544
8545
8546static int wpas_p2p_nfc_join_group(struct wpa_supplicant *wpa_s,
8547 struct p2p_nfc_params *params)
8548{
8549 wpa_printf(MSG_DEBUG, "P2P: Initiate join-group based on NFC "
8550 "connection handover (freq=%d)",
8551 params->go_freq);
8552
8553 if (params->go_freq && params->go_ssid_len) {
8554 wpa_s->p2p_wps_method = WPS_NFC;
8555 wpa_s->pending_join_wps_method = WPS_NFC;
8556 os_memset(wpa_s->pending_join_iface_addr, 0, ETH_ALEN);
8557 os_memcpy(wpa_s->pending_join_dev_addr, params->go_dev_addr,
8558 ETH_ALEN);
8559 return wpas_p2p_join_start(wpa_s, params->go_freq,
8560 params->go_ssid,
8561 params->go_ssid_len);
8562 }
8563
8564 return wpas_p2p_connect(wpa_s, params->peer->p2p_device_addr, NULL,
8565 WPS_NFC, 0, 0, 1, 0, wpa_s->conf->p2p_go_intent,
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08008566 params->go_freq, wpa_s->p2p_go_vht_center_freq2,
Dmitry Shmidtde47be72016-01-07 12:52:55 -08008567 -1, 0, 1, 1, wpa_s->p2p_go_max_oper_chwidth,
8568 params->go_ssid_len ? params->go_ssid : NULL,
8569 params->go_ssid_len);
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08008570}
8571
8572
8573static int wpas_p2p_nfc_auth_join(struct wpa_supplicant *wpa_s,
8574 struct p2p_nfc_params *params, int tag)
8575{
8576 int res, persistent;
8577 struct wpa_ssid *ssid;
8578
8579 wpa_printf(MSG_DEBUG, "P2P: Authorize join-group based on NFC "
8580 "connection handover");
8581 for (wpa_s = wpa_s->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
8582 ssid = wpa_s->current_ssid;
8583 if (ssid == NULL)
8584 continue;
8585 if (ssid->mode != WPAS_MODE_P2P_GO)
8586 continue;
8587 if (wpa_s->ap_iface == NULL)
8588 continue;
8589 break;
8590 }
8591 if (wpa_s == NULL) {
8592 wpa_printf(MSG_DEBUG, "P2P: Could not find GO interface");
8593 return -1;
8594 }
8595
Dmitry Shmidt849734c2016-05-27 09:59:01 -07008596 if (wpa_s->p2pdev->p2p_oob_dev_pw_id !=
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08008597 DEV_PW_NFC_CONNECTION_HANDOVER &&
Dmitry Shmidt849734c2016-05-27 09:59:01 -07008598 !wpa_s->p2pdev->p2p_oob_dev_pw) {
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08008599 wpa_printf(MSG_DEBUG, "P2P: No NFC Dev Pw known");
8600 return -1;
8601 }
8602 res = wpas_ap_wps_add_nfc_pw(
Dmitry Shmidt849734c2016-05-27 09:59:01 -07008603 wpa_s, wpa_s->p2pdev->p2p_oob_dev_pw_id,
8604 wpa_s->p2pdev->p2p_oob_dev_pw,
8605 wpa_s->p2pdev->p2p_peer_oob_pk_hash_known ?
8606 wpa_s->p2pdev->p2p_peer_oob_pubkey_hash : NULL);
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08008607 if (res)
8608 return res;
8609
8610 if (!tag) {
8611 wpa_printf(MSG_DEBUG, "P2P: Negotiated handover - wait for peer to join without invitation");
8612 return 0;
8613 }
8614
8615 if (!params->peer ||
8616 !(params->peer->dev_capab & P2P_DEV_CAPAB_INVITATION_PROCEDURE))
8617 return 0;
8618
8619 wpa_printf(MSG_DEBUG, "P2P: Static handover - invite peer " MACSTR
8620 " to join", MAC2STR(params->peer->p2p_device_addr));
8621
8622 wpa_s->global->p2p_invite_group = wpa_s;
8623 persistent = ssid->p2p_persistent_group &&
Dmitry Shmidt849734c2016-05-27 09:59:01 -07008624 wpas_p2p_get_persistent(wpa_s->p2pdev,
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08008625 params->peer->p2p_device_addr,
8626 ssid->ssid, ssid->ssid_len);
Dmitry Shmidt849734c2016-05-27 09:59:01 -07008627 wpa_s->p2pdev->pending_invite_ssid_id = -1;
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08008628
8629 return p2p_invite(wpa_s->global->p2p, params->peer->p2p_device_addr,
8630 P2P_INVITE_ROLE_ACTIVE_GO, wpa_s->own_addr,
8631 ssid->ssid, ssid->ssid_len, ssid->frequency,
8632 wpa_s->global->p2p_dev_addr, persistent, 0,
Dmitry Shmidt849734c2016-05-27 09:59:01 -07008633 wpa_s->p2pdev->p2p_oob_dev_pw_id);
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08008634}
8635
8636
8637static int wpas_p2p_nfc_init_go_neg(struct wpa_supplicant *wpa_s,
8638 struct p2p_nfc_params *params,
8639 int forced_freq)
8640{
8641 wpa_printf(MSG_DEBUG, "P2P: Initiate GO Negotiation based on NFC "
8642 "connection handover");
8643 return wpas_p2p_connect(wpa_s, params->peer->p2p_device_addr, NULL,
8644 WPS_NFC, 0, 0, 0, 0, wpa_s->conf->p2p_go_intent,
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08008645 forced_freq, wpa_s->p2p_go_vht_center_freq2,
Dmitry Shmidtde47be72016-01-07 12:52:55 -08008646 -1, 0, 1, 1, wpa_s->p2p_go_max_oper_chwidth,
8647 NULL, 0);
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08008648}
8649
8650
8651static int wpas_p2p_nfc_resp_go_neg(struct wpa_supplicant *wpa_s,
8652 struct p2p_nfc_params *params,
8653 int forced_freq)
8654{
8655 int res;
8656
8657 wpa_printf(MSG_DEBUG, "P2P: Authorize GO Negotiation based on NFC "
8658 "connection handover");
8659 res = wpas_p2p_connect(wpa_s, params->peer->p2p_device_addr, NULL,
8660 WPS_NFC, 0, 0, 0, 1, wpa_s->conf->p2p_go_intent,
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08008661 forced_freq, wpa_s->p2p_go_vht_center_freq2,
Dmitry Shmidtde47be72016-01-07 12:52:55 -08008662 -1, 0, 1, 1, wpa_s->p2p_go_max_oper_chwidth,
8663 NULL, 0);
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08008664 if (res)
8665 return res;
8666
8667 res = wpas_p2p_listen(wpa_s, 60);
8668 if (res) {
8669 p2p_unauthorize(wpa_s->global->p2p,
8670 params->peer->p2p_device_addr);
8671 }
8672
8673 return res;
8674}
8675
8676
8677static int wpas_p2p_nfc_connection_handover(struct wpa_supplicant *wpa_s,
8678 const struct wpabuf *data,
8679 int sel, int tag, int forced_freq)
8680{
8681 const u8 *pos, *end;
8682 u16 len, id;
8683 struct p2p_nfc_params params;
8684 int res;
8685
8686 os_memset(&params, 0, sizeof(params));
8687 params.sel = sel;
8688
8689 wpa_hexdump_buf(MSG_DEBUG, "P2P: Received NFC tag payload", data);
8690
8691 pos = wpabuf_head(data);
8692 end = pos + wpabuf_len(data);
8693
8694 if (end - pos < 2) {
8695 wpa_printf(MSG_DEBUG, "P2P: Not enough data for Length of WSC "
8696 "attributes");
8697 return -1;
8698 }
8699 len = WPA_GET_BE16(pos);
8700 pos += 2;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08008701 if (len > end - pos) {
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08008702 wpa_printf(MSG_DEBUG, "P2P: Not enough data for WSC "
8703 "attributes");
8704 return -1;
8705 }
8706 params.wsc_attr = pos;
8707 params.wsc_len = len;
8708 pos += len;
8709
8710 if (end - pos < 2) {
8711 wpa_printf(MSG_DEBUG, "P2P: Not enough data for Length of P2P "
8712 "attributes");
8713 return -1;
8714 }
8715 len = WPA_GET_BE16(pos);
8716 pos += 2;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08008717 if (len > end - pos) {
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08008718 wpa_printf(MSG_DEBUG, "P2P: Not enough data for P2P "
8719 "attributes");
8720 return -1;
8721 }
8722 params.p2p_attr = pos;
8723 params.p2p_len = len;
8724 pos += len;
8725
8726 wpa_hexdump(MSG_DEBUG, "P2P: WSC attributes",
8727 params.wsc_attr, params.wsc_len);
8728 wpa_hexdump(MSG_DEBUG, "P2P: P2P attributes",
8729 params.p2p_attr, params.p2p_len);
8730 if (pos < end) {
8731 wpa_hexdump(MSG_DEBUG,
8732 "P2P: Ignored extra data after P2P attributes",
8733 pos, end - pos);
8734 }
8735
8736 res = p2p_process_nfc_connection_handover(wpa_s->global->p2p, &params);
8737 if (res)
8738 return res;
8739
8740 if (params.next_step == NO_ACTION)
8741 return 0;
8742
8743 if (params.next_step == BOTH_GO) {
8744 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_NFC_BOTH_GO "peer=" MACSTR,
8745 MAC2STR(params.peer->p2p_device_addr));
8746 return 0;
8747 }
8748
8749 if (params.next_step == PEER_CLIENT) {
8750 if (!is_zero_ether_addr(params.go_dev_addr)) {
8751 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_NFC_PEER_CLIENT
8752 "peer=" MACSTR " freq=%d go_dev_addr=" MACSTR
8753 " ssid=\"%s\"",
8754 MAC2STR(params.peer->p2p_device_addr),
8755 params.go_freq,
8756 MAC2STR(params.go_dev_addr),
8757 wpa_ssid_txt(params.go_ssid,
8758 params.go_ssid_len));
8759 } else {
8760 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_NFC_PEER_CLIENT
8761 "peer=" MACSTR " freq=%d",
8762 MAC2STR(params.peer->p2p_device_addr),
8763 params.go_freq);
8764 }
8765 return 0;
8766 }
8767
8768 if (wpas_p2p_cli_freq(wpa_s, NULL, NULL)) {
8769 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_NFC_WHILE_CLIENT "peer="
8770 MACSTR, MAC2STR(params.peer->p2p_device_addr));
8771 return 0;
8772 }
8773
8774 wpabuf_free(wpa_s->p2p_oob_dev_pw);
8775 wpa_s->p2p_oob_dev_pw = NULL;
8776
8777 if (params.oob_dev_pw_len < WPS_OOB_PUBKEY_HASH_LEN + 2) {
8778 wpa_printf(MSG_DEBUG, "P2P: No peer OOB Dev Pw "
8779 "received");
8780 return -1;
8781 }
8782
8783 id = WPA_GET_BE16(params.oob_dev_pw + WPS_OOB_PUBKEY_HASH_LEN);
8784 wpa_printf(MSG_DEBUG, "P2P: Peer OOB Dev Pw %u", id);
8785 wpa_hexdump(MSG_DEBUG, "P2P: Peer OOB Public Key hash",
8786 params.oob_dev_pw, WPS_OOB_PUBKEY_HASH_LEN);
8787 os_memcpy(wpa_s->p2p_peer_oob_pubkey_hash,
8788 params.oob_dev_pw, WPS_OOB_PUBKEY_HASH_LEN);
8789 wpa_s->p2p_peer_oob_pk_hash_known = 1;
8790
8791 if (tag) {
8792 if (id < 0x10) {
8793 wpa_printf(MSG_DEBUG, "P2P: Static handover - invalid "
8794 "peer OOB Device Password Id %u", id);
8795 return -1;
8796 }
8797 wpa_printf(MSG_DEBUG, "P2P: Static handover - use peer OOB "
8798 "Device Password Id %u", id);
8799 wpa_hexdump_key(MSG_DEBUG, "P2P: Peer OOB Device Password",
8800 params.oob_dev_pw + WPS_OOB_PUBKEY_HASH_LEN + 2,
8801 params.oob_dev_pw_len -
8802 WPS_OOB_PUBKEY_HASH_LEN - 2);
8803 wpa_s->p2p_oob_dev_pw_id = id;
8804 wpa_s->p2p_oob_dev_pw = wpabuf_alloc_copy(
8805 params.oob_dev_pw + WPS_OOB_PUBKEY_HASH_LEN + 2,
8806 params.oob_dev_pw_len -
8807 WPS_OOB_PUBKEY_HASH_LEN - 2);
8808 if (wpa_s->p2p_oob_dev_pw == NULL)
8809 return -1;
8810
8811 if (wpa_s->conf->wps_nfc_dh_pubkey == NULL &&
8812 wps_nfc_gen_dh(&wpa_s->conf->wps_nfc_dh_pubkey,
8813 &wpa_s->conf->wps_nfc_dh_privkey) < 0)
8814 return -1;
8815 } else {
8816 wpa_printf(MSG_DEBUG, "P2P: Using abbreviated WPS handshake "
8817 "without Device Password");
8818 wpa_s->p2p_oob_dev_pw_id = DEV_PW_NFC_CONNECTION_HANDOVER;
8819 }
8820
8821 switch (params.next_step) {
8822 case NO_ACTION:
8823 case BOTH_GO:
8824 case PEER_CLIENT:
8825 /* already covered above */
8826 return 0;
8827 case JOIN_GROUP:
8828 return wpas_p2p_nfc_join_group(wpa_s, &params);
8829 case AUTH_JOIN:
8830 return wpas_p2p_nfc_auth_join(wpa_s, &params, tag);
8831 case INIT_GO_NEG:
8832 return wpas_p2p_nfc_init_go_neg(wpa_s, &params, forced_freq);
8833 case RESP_GO_NEG:
8834 /* TODO: use own OOB Dev Pw */
8835 return wpas_p2p_nfc_resp_go_neg(wpa_s, &params, forced_freq);
8836 }
8837
8838 return -1;
8839}
8840
8841
8842int wpas_p2p_nfc_tag_process(struct wpa_supplicant *wpa_s,
8843 const struct wpabuf *data, int forced_freq)
8844{
8845 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
8846 return -1;
8847
8848 return wpas_p2p_nfc_connection_handover(wpa_s, data, 1, 1, forced_freq);
8849}
8850
8851
8852int wpas_p2p_nfc_report_handover(struct wpa_supplicant *wpa_s, int init,
8853 const struct wpabuf *req,
8854 const struct wpabuf *sel, int forced_freq)
8855{
8856 struct wpabuf *tmp;
8857 int ret;
8858
8859 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
8860 return -1;
8861
8862 wpa_printf(MSG_DEBUG, "NFC: P2P connection handover reported");
8863
8864 wpa_hexdump_ascii(MSG_DEBUG, "NFC: Req",
8865 wpabuf_head(req), wpabuf_len(req));
8866 wpa_hexdump_ascii(MSG_DEBUG, "NFC: Sel",
8867 wpabuf_head(sel), wpabuf_len(sel));
8868 if (forced_freq)
8869 wpa_printf(MSG_DEBUG, "NFC: Forced freq %d", forced_freq);
8870 tmp = ndef_parse_p2p(init ? sel : req);
8871 if (tmp == NULL) {
8872 wpa_printf(MSG_DEBUG, "P2P: Could not parse NDEF");
8873 return -1;
8874 }
8875
8876 ret = wpas_p2p_nfc_connection_handover(wpa_s, tmp, init, 0,
8877 forced_freq);
8878 wpabuf_free(tmp);
8879
8880 return ret;
8881}
8882
8883
8884int wpas_p2p_nfc_tag_enabled(struct wpa_supplicant *wpa_s, int enabled)
8885{
8886 const u8 *if_addr;
8887 int go_intent = wpa_s->conf->p2p_go_intent;
8888 struct wpa_supplicant *iface;
8889
8890 if (wpa_s->global->p2p == NULL)
8891 return -1;
8892
8893 if (!enabled) {
8894 wpa_printf(MSG_DEBUG, "P2P: Disable use of own NFC Tag");
8895 for (iface = wpa_s->global->ifaces; iface; iface = iface->next)
8896 {
8897 if (!iface->ap_iface)
8898 continue;
8899 hostapd_wps_nfc_token_disable(iface->ap_iface->bss[0]);
8900 }
8901 p2p_set_authorized_oob_dev_pw_id(wpa_s->global->p2p, 0,
8902 0, NULL);
8903 if (wpa_s->p2p_nfc_tag_enabled)
8904 wpas_p2p_remove_pending_group_interface(wpa_s);
8905 wpa_s->p2p_nfc_tag_enabled = 0;
8906 return 0;
8907 }
8908
8909 if (wpa_s->global->p2p_disabled)
8910 return -1;
8911
8912 if (wpa_s->conf->wps_nfc_dh_pubkey == NULL ||
8913 wpa_s->conf->wps_nfc_dh_privkey == NULL ||
8914 wpa_s->conf->wps_nfc_dev_pw == NULL ||
8915 wpa_s->conf->wps_nfc_dev_pw_id < 0x10) {
8916 wpa_printf(MSG_DEBUG, "P2P: NFC password token not configured "
8917 "to allow static handover cases");
8918 return -1;
8919 }
8920
8921 wpa_printf(MSG_DEBUG, "P2P: Enable use of own NFC Tag");
8922
8923 wpa_s->p2p_oob_dev_pw_id = wpa_s->conf->wps_nfc_dev_pw_id;
8924 wpabuf_free(wpa_s->p2p_oob_dev_pw);
8925 wpa_s->p2p_oob_dev_pw = wpabuf_dup(wpa_s->conf->wps_nfc_dev_pw);
8926 if (wpa_s->p2p_oob_dev_pw == NULL)
8927 return -1;
8928 wpa_s->p2p_peer_oob_pk_hash_known = 0;
8929
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -07008930 if (wpa_s->p2p_group_interface == P2P_GROUP_INTERFACE_GO ||
8931 wpa_s->p2p_group_interface == P2P_GROUP_INTERFACE_CLIENT) {
8932 /*
8933 * P2P Group Interface present and the command came on group
8934 * interface, so enable the token for the current interface.
8935 */
8936 wpa_s->create_p2p_iface = 0;
8937 } else {
8938 wpa_s->create_p2p_iface = wpas_p2p_create_iface(wpa_s);
8939 }
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08008940
8941 if (wpa_s->create_p2p_iface) {
8942 enum wpa_driver_if_type iftype;
8943 /* Prepare to add a new interface for the group */
8944 iftype = WPA_IF_P2P_GROUP;
8945 if (go_intent == 15)
8946 iftype = WPA_IF_P2P_GO;
8947 if (wpas_p2p_add_group_interface(wpa_s, iftype) < 0) {
8948 wpa_printf(MSG_ERROR, "P2P: Failed to allocate a new "
8949 "interface for the group");
8950 return -1;
8951 }
8952
8953 if_addr = wpa_s->pending_interface_addr;
Dmitry Shmidt9c175262016-03-03 10:20:07 -08008954 } else if (wpa_s->p2p_mgmt)
8955 if_addr = wpa_s->parent->own_addr;
8956 else
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08008957 if_addr = wpa_s->own_addr;
8958
8959 wpa_s->p2p_nfc_tag_enabled = enabled;
8960
8961 for (iface = wpa_s->global->ifaces; iface; iface = iface->next) {
8962 struct hostapd_data *hapd;
8963 if (iface->ap_iface == NULL)
8964 continue;
8965 hapd = iface->ap_iface->bss[0];
8966 wpabuf_free(hapd->conf->wps_nfc_dh_pubkey);
8967 hapd->conf->wps_nfc_dh_pubkey =
8968 wpabuf_dup(wpa_s->conf->wps_nfc_dh_pubkey);
8969 wpabuf_free(hapd->conf->wps_nfc_dh_privkey);
8970 hapd->conf->wps_nfc_dh_privkey =
8971 wpabuf_dup(wpa_s->conf->wps_nfc_dh_privkey);
8972 wpabuf_free(hapd->conf->wps_nfc_dev_pw);
8973 hapd->conf->wps_nfc_dev_pw =
8974 wpabuf_dup(wpa_s->conf->wps_nfc_dev_pw);
8975 hapd->conf->wps_nfc_dev_pw_id = wpa_s->conf->wps_nfc_dev_pw_id;
8976
8977 if (hostapd_wps_nfc_token_enable(iface->ap_iface->bss[0]) < 0) {
8978 wpa_dbg(iface, MSG_DEBUG,
8979 "P2P: Failed to enable NFC Tag for GO");
8980 }
8981 }
8982 p2p_set_authorized_oob_dev_pw_id(
8983 wpa_s->global->p2p, wpa_s->conf->wps_nfc_dev_pw_id, go_intent,
8984 if_addr);
8985
8986 return 0;
8987}
8988
8989#endif /* CONFIG_WPS_NFC */
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07008990
8991
8992static void wpas_p2p_optimize_listen_channel(struct wpa_supplicant *wpa_s,
8993 struct wpa_used_freq_data *freqs,
8994 unsigned int num)
8995{
8996 u8 curr_chan, cand, chan;
8997 unsigned int i;
8998
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08008999 /*
9000 * If possible, optimize the Listen channel to be a channel that is
9001 * already used by one of the other interfaces.
9002 */
9003 if (!wpa_s->conf->p2p_optimize_listen_chan)
9004 return;
9005
9006 if (!wpa_s->current_ssid || wpa_s->wpa_state != WPA_COMPLETED)
9007 return;
9008
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07009009 curr_chan = p2p_get_listen_channel(wpa_s->global->p2p);
9010 for (i = 0, cand = 0; i < num; i++) {
9011 ieee80211_freq_to_chan(freqs[i].freq, &chan);
9012 if (curr_chan == chan) {
9013 cand = 0;
9014 break;
9015 }
9016
9017 if (chan == 1 || chan == 6 || chan == 11)
9018 cand = chan;
9019 }
9020
9021 if (cand) {
9022 wpa_dbg(wpa_s, MSG_DEBUG,
Dmitry Shmidt216983b2015-02-06 10:50:36 -08009023 "P2P: Update Listen channel to %u based on operating channel",
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07009024 cand);
9025 p2p_set_listen_channel(wpa_s->global->p2p, 81, cand, 0);
9026 }
9027}
9028
9029
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08009030static int wpas_p2p_move_go_csa(struct wpa_supplicant *wpa_s)
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07009031{
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08009032 struct hostapd_config *conf;
9033 struct p2p_go_neg_results params;
9034 struct csa_settings csa_settings;
9035 struct wpa_ssid *current_ssid = wpa_s->current_ssid;
9036 int old_freq = current_ssid->frequency;
9037 int ret;
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07009038
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08009039 if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_AP_CSA)) {
9040 wpa_dbg(wpa_s, MSG_DEBUG, "CSA is not enabled");
9041 return -1;
9042 }
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07009043
9044 /*
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08009045 * TODO: This function may not always work correctly. For example,
9046 * when we have a running GO and a BSS on a DFS channel.
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07009047 */
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08009048 if (wpas_p2p_init_go_params(wpa_s, &params, 0, 0, 0, 0, 0, NULL)) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08009049 wpa_dbg(wpa_s, MSG_DEBUG,
9050 "P2P CSA: Failed to select new frequency for GO");
9051 return -1;
9052 }
9053
9054 if (current_ssid->frequency == params.freq) {
9055 wpa_dbg(wpa_s, MSG_DEBUG,
9056 "P2P CSA: Selected same frequency - not moving GO");
9057 return 0;
9058 }
9059
9060 conf = hostapd_config_defaults();
9061 if (!conf) {
9062 wpa_dbg(wpa_s, MSG_DEBUG,
9063 "P2P CSA: Failed to allocate default config");
9064 return -1;
9065 }
9066
9067 current_ssid->frequency = params.freq;
9068 if (wpa_supplicant_conf_ap_ht(wpa_s, current_ssid, conf)) {
9069 wpa_dbg(wpa_s, MSG_DEBUG,
9070 "P2P CSA: Failed to create new GO config");
9071 ret = -1;
9072 goto out;
9073 }
9074
9075 if (conf->hw_mode != wpa_s->ap_iface->current_mode->mode) {
9076 wpa_dbg(wpa_s, MSG_DEBUG,
9077 "P2P CSA: CSA to a different band is not supported");
9078 ret = -1;
9079 goto out;
9080 }
9081
9082 os_memset(&csa_settings, 0, sizeof(csa_settings));
9083 csa_settings.cs_count = P2P_GO_CSA_COUNT;
9084 csa_settings.block_tx = P2P_GO_CSA_BLOCK_TX;
9085 csa_settings.freq_params.freq = params.freq;
9086 csa_settings.freq_params.sec_channel_offset = conf->secondary_channel;
9087 csa_settings.freq_params.ht_enabled = conf->ieee80211n;
9088 csa_settings.freq_params.bandwidth = conf->secondary_channel ? 40 : 20;
9089
9090 if (conf->ieee80211ac) {
9091 int freq1 = 0, freq2 = 0;
9092 u8 chan, opclass;
9093
9094 if (ieee80211_freq_to_channel_ext(params.freq,
9095 conf->secondary_channel,
9096 conf->vht_oper_chwidth,
9097 &opclass, &chan) ==
9098 NUM_HOSTAPD_MODES) {
9099 wpa_printf(MSG_ERROR, "P2P CSA: Bad freq");
9100 ret = -1;
9101 goto out;
9102 }
9103
9104 if (conf->vht_oper_centr_freq_seg0_idx)
9105 freq1 = ieee80211_chan_to_freq(
9106 NULL, opclass,
9107 conf->vht_oper_centr_freq_seg0_idx);
9108
9109 if (conf->vht_oper_centr_freq_seg1_idx)
9110 freq2 = ieee80211_chan_to_freq(
9111 NULL, opclass,
9112 conf->vht_oper_centr_freq_seg1_idx);
9113
9114 if (freq1 < 0 || freq2 < 0) {
9115 wpa_dbg(wpa_s, MSG_DEBUG,
9116 "P2P CSA: Selected invalid VHT center freqs");
9117 ret = -1;
9118 goto out;
9119 }
9120
9121 csa_settings.freq_params.vht_enabled = conf->ieee80211ac;
9122 csa_settings.freq_params.center_freq1 = freq1;
9123 csa_settings.freq_params.center_freq2 = freq2;
9124
9125 switch (conf->vht_oper_chwidth) {
9126 case VHT_CHANWIDTH_80MHZ:
9127 case VHT_CHANWIDTH_80P80MHZ:
9128 csa_settings.freq_params.bandwidth = 80;
9129 break;
9130 case VHT_CHANWIDTH_160MHZ:
9131 csa_settings.freq_params.bandwidth = 160;
9132 break;
9133 }
9134 }
9135
9136 ret = ap_switch_channel(wpa_s, &csa_settings);
9137out:
9138 current_ssid->frequency = old_freq;
9139 hostapd_config_free(conf);
9140 return ret;
9141}
9142
9143
9144static void wpas_p2p_move_go_no_csa(struct wpa_supplicant *wpa_s)
9145{
9146 struct p2p_go_neg_results params;
9147 struct wpa_ssid *current_ssid = wpa_s->current_ssid;
9148
9149 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_REMOVE_AND_REFORM_GROUP);
9150
9151 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Move GO from freq=%d MHz",
9152 current_ssid->frequency);
9153
9154 /* Stop the AP functionality */
9155 /* TODO: Should do this in a way that does not indicated to possible
9156 * P2P Clients in the group that the group is terminated. */
9157 wpa_supplicant_ap_deinit(wpa_s);
9158
9159 /* Reselect the GO frequency */
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08009160 if (wpas_p2p_init_go_params(wpa_s, &params, 0, 0, 0, 0, 0, NULL)) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08009161 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Failed to reselect freq");
9162 wpas_p2p_group_delete(wpa_s,
9163 P2P_GROUP_REMOVAL_GO_LEAVE_CHANNEL);
9164 return;
9165 }
9166 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: New freq selected for the GO (%u MHz)",
9167 params.freq);
9168
9169 if (params.freq &&
9170 !p2p_supported_freq_go(wpa_s->global->p2p, params.freq)) {
9171 wpa_printf(MSG_DEBUG,
9172 "P2P: Selected freq (%u MHz) is not valid for P2P",
9173 params.freq);
9174 wpas_p2p_group_delete(wpa_s,
9175 P2P_GROUP_REMOVAL_GO_LEAVE_CHANNEL);
9176 return;
9177 }
9178
9179 /* Update the frequency */
9180 current_ssid->frequency = params.freq;
9181 wpa_s->connect_without_scan = current_ssid;
9182 wpa_s->reassociate = 1;
9183 wpa_s->disconnected = 0;
9184 wpa_supplicant_req_scan(wpa_s, 0, 0);
9185}
9186
9187
9188static void wpas_p2p_move_go(void *eloop_ctx, void *timeout_ctx)
9189{
9190 struct wpa_supplicant *wpa_s = eloop_ctx;
9191
9192 if (!wpa_s->ap_iface || !wpa_s->current_ssid)
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07009193 return;
9194
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08009195 wpas_p2p_go_update_common_freqs(wpa_s);
9196
9197 /* Do not move GO in the middle of a CSA */
9198 if (hostapd_csa_in_progress(wpa_s->ap_iface)) {
9199 wpa_printf(MSG_DEBUG,
9200 "P2P: CSA is in progress - not moving GO");
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07009201 return;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08009202 }
9203
9204 /*
9205 * First, try a channel switch flow. If it is not supported or fails,
9206 * take down the GO and bring it up again.
9207 */
9208 if (wpas_p2p_move_go_csa(wpa_s) < 0)
9209 wpas_p2p_move_go_no_csa(wpa_s);
9210}
9211
9212
9213static void wpas_p2p_reconsider_moving_go(void *eloop_ctx, void *timeout_ctx)
9214{
9215 struct wpa_supplicant *wpa_s = eloop_ctx;
9216 struct wpa_used_freq_data *freqs = NULL;
9217 unsigned int num = wpa_s->num_multichan_concurrent;
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07009218
9219 freqs = os_calloc(num, sizeof(struct wpa_used_freq_data));
9220 if (!freqs)
9221 return;
9222
9223 num = get_shared_radio_freqs_data(wpa_s, freqs, num);
9224
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08009225 /* Previous attempt to move a GO was not possible -- try again. */
9226 wpas_p2p_consider_moving_gos(wpa_s, freqs, num,
9227 WPAS_P2P_CHANNEL_UPDATE_ANY);
9228
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07009229 os_free(freqs);
9230}
9231
9232
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08009233/*
9234 * Consider moving a GO from its currently used frequency:
9235 * 1. It is possible that due to regulatory consideration the frequency
9236 * can no longer be used and there is a need to evacuate the GO.
9237 * 2. It is possible that due to MCC considerations, it would be preferable
9238 * to move the GO to a channel that is currently used by some other
9239 * station interface.
9240 *
9241 * In case a frequency that became invalid is once again valid, cancel a
9242 * previously initiated GO frequency change.
9243 */
9244static void wpas_p2p_consider_moving_one_go(struct wpa_supplicant *wpa_s,
9245 struct wpa_used_freq_data *freqs,
9246 unsigned int num)
9247{
9248 unsigned int i, invalid_freq = 0, policy_move = 0, flags = 0;
9249 unsigned int timeout;
9250 int freq;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07009251 int dfs_offload;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08009252
9253 wpas_p2p_go_update_common_freqs(wpa_s);
9254
9255 freq = wpa_s->current_ssid->frequency;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07009256 dfs_offload = (wpa_s->drv_flags & WPA_DRIVER_FLAGS_DFS_OFFLOAD) &&
Roshan Pius3a1667e2018-07-03 15:17:14 -07009257 ieee80211_is_dfs(freq, wpa_s->hw.modes, wpa_s->hw.num_modes);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08009258 for (i = 0, invalid_freq = 0; i < num; i++) {
9259 if (freqs[i].freq == freq) {
9260 flags = freqs[i].flags;
9261
9262 /* The channel is invalid, must change it */
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07009263 if (!p2p_supported_freq_go(wpa_s->global->p2p, freq) &&
9264 !dfs_offload) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08009265 wpa_dbg(wpa_s, MSG_DEBUG,
9266 "P2P: Freq=%d MHz no longer valid for GO",
9267 freq);
9268 invalid_freq = 1;
9269 }
9270 } else if (freqs[i].flags == 0) {
9271 /* Freq is not used by any other station interface */
9272 continue;
9273 } else if (!p2p_supported_freq(wpa_s->global->p2p,
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07009274 freqs[i].freq) && !dfs_offload) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08009275 /* Freq is not valid for P2P use cases */
9276 continue;
9277 } else if (wpa_s->conf->p2p_go_freq_change_policy ==
9278 P2P_GO_FREQ_MOVE_SCM) {
9279 policy_move = 1;
9280 } else if (wpa_s->conf->p2p_go_freq_change_policy ==
9281 P2P_GO_FREQ_MOVE_SCM_PEER_SUPPORTS &&
9282 wpas_p2p_go_is_peer_freq(wpa_s, freqs[i].freq)) {
9283 policy_move = 1;
9284 } else if ((wpa_s->conf->p2p_go_freq_change_policy ==
9285 P2P_GO_FREQ_MOVE_SCM_ECSA) &&
9286 wpas_p2p_go_is_peer_freq(wpa_s, freqs[i].freq)) {
9287 if (!p2p_get_group_num_members(wpa_s->p2p_group)) {
9288 policy_move = 1;
9289 } else if ((wpa_s->drv_flags &
9290 WPA_DRIVER_FLAGS_AP_CSA) &&
9291 wpas_p2p_go_clients_support_ecsa(wpa_s)) {
9292 u8 chan;
9293
9294 /*
9295 * We do not support CSA between bands, so move
9296 * GO only within the same band.
9297 */
9298 if (wpa_s->ap_iface->current_mode->mode ==
9299 ieee80211_freq_to_chan(freqs[i].freq,
9300 &chan))
9301 policy_move = 1;
9302 }
9303 }
9304 }
9305
9306 wpa_dbg(wpa_s, MSG_DEBUG,
9307 "P2P: GO move: invalid_freq=%u, policy_move=%u, flags=0x%X",
9308 invalid_freq, policy_move, flags);
9309
9310 /*
9311 * The channel is valid, or we are going to have a policy move, so
9312 * cancel timeout.
9313 */
9314 if (!invalid_freq || policy_move) {
9315 wpa_dbg(wpa_s, MSG_DEBUG,
9316 "P2P: Cancel a GO move from freq=%d MHz", freq);
9317 eloop_cancel_timeout(wpas_p2p_move_go, wpa_s, NULL);
9318
9319 if (wpas_p2p_in_progress(wpa_s)) {
9320 wpa_dbg(wpa_s, MSG_DEBUG,
9321 "P2P: GO move: policy CS is not allowed - setting timeout to re-consider GO move");
9322 eloop_cancel_timeout(wpas_p2p_reconsider_moving_go,
9323 wpa_s, NULL);
9324 eloop_register_timeout(P2P_RECONSIDER_GO_MOVE_DELAY, 0,
9325 wpas_p2p_reconsider_moving_go,
9326 wpa_s, NULL);
9327 return;
9328 }
9329 }
9330
9331 if (!invalid_freq && (!policy_move || flags != 0)) {
9332 wpa_dbg(wpa_s, MSG_DEBUG,
9333 "P2P: Not initiating a GO frequency change");
9334 return;
9335 }
9336
9337 /*
9338 * Do not consider moving GO if it is in the middle of a CSA. When the
9339 * CSA is finished this flow should be retriggered.
9340 */
9341 if (hostapd_csa_in_progress(wpa_s->ap_iface)) {
9342 wpa_dbg(wpa_s, MSG_DEBUG,
9343 "P2P: Not initiating a GO frequency change - CSA is in progress");
9344 return;
9345 }
9346
9347 if (invalid_freq && !wpas_p2p_disallowed_freq(wpa_s->global, freq))
9348 timeout = P2P_GO_FREQ_CHANGE_TIME;
9349 else
9350 timeout = 0;
9351
9352 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Move GO from freq=%d MHz in %d secs",
9353 freq, timeout);
9354 eloop_cancel_timeout(wpas_p2p_move_go, wpa_s, NULL);
9355 eloop_register_timeout(timeout, 0, wpas_p2p_move_go, wpa_s, NULL);
9356}
9357
9358
9359static void wpas_p2p_consider_moving_gos(struct wpa_supplicant *wpa_s,
9360 struct wpa_used_freq_data *freqs,
9361 unsigned int num,
9362 enum wpas_p2p_channel_update_trig trig)
9363{
9364 struct wpa_supplicant *ifs;
9365
9366 eloop_cancel_timeout(wpas_p2p_reconsider_moving_go, ELOOP_ALL_CTX,
9367 NULL);
9368
9369 /*
9370 * Travers all the radio interfaces, and for each GO interface, check
9371 * if there is a need to move the GO from the frequency it is using,
9372 * or in case the frequency is valid again, cancel the evacuation flow.
9373 */
9374 dl_list_for_each(ifs, &wpa_s->radio->ifaces, struct wpa_supplicant,
9375 radio_list) {
9376 if (ifs->current_ssid == NULL ||
9377 ifs->current_ssid->mode != WPAS_MODE_P2P_GO)
9378 continue;
9379
9380 /*
9381 * The GO was just started or completed channel switch, no need
9382 * to move it.
9383 */
9384 if (wpa_s == ifs &&
9385 (trig == WPAS_P2P_CHANNEL_UPDATE_STATE_CHANGE ||
9386 trig == WPAS_P2P_CHANNEL_UPDATE_CS)) {
9387 wpa_dbg(wpa_s, MSG_DEBUG,
9388 "P2P: GO move - schedule re-consideration");
9389 eloop_register_timeout(P2P_RECONSIDER_GO_MOVE_DELAY, 0,
9390 wpas_p2p_reconsider_moving_go,
9391 wpa_s, NULL);
9392 continue;
9393 }
9394
9395 wpas_p2p_consider_moving_one_go(ifs, freqs, num);
9396 }
9397}
9398
9399
9400void wpas_p2p_indicate_state_change(struct wpa_supplicant *wpa_s)
9401{
9402 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
9403 return;
9404
9405 wpas_p2p_update_channel_list(wpa_s,
9406 WPAS_P2P_CHANNEL_UPDATE_STATE_CHANGE);
9407}
9408
9409
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07009410void wpas_p2p_deinit_iface(struct wpa_supplicant *wpa_s)
9411{
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07009412 if (wpa_s == wpa_s->global->p2p_init_wpa_s && wpa_s->global->p2p) {
9413 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Disable P2P since removing "
9414 "the management interface is being removed");
9415 wpas_p2p_deinit_global(wpa_s->global);
9416 }
9417}
9418
9419
9420void wpas_p2p_ap_deinit(struct wpa_supplicant *wpa_s)
9421{
9422 if (wpa_s->ap_iface->bss)
9423 wpa_s->ap_iface->bss[0]->p2p_group = NULL;
9424 wpas_p2p_group_deinit(wpa_s);
9425}
Dmitry Shmidt58d12ad2016-07-28 10:07:03 -07009426
9427
9428int wpas_p2p_lo_start(struct wpa_supplicant *wpa_s, unsigned int freq,
9429 unsigned int period, unsigned int interval,
9430 unsigned int count)
9431{
9432 struct p2p_data *p2p = wpa_s->global->p2p;
9433 u8 *device_types;
9434 size_t dev_types_len;
9435 struct wpabuf *buf;
9436 int ret;
9437
9438 if (wpa_s->p2p_lo_started) {
9439 wpa_dbg(wpa_s, MSG_DEBUG,
9440 "P2P Listen offload is already started");
9441 return 0;
9442 }
9443
9444 if (wpa_s->global->p2p == NULL ||
9445 !(wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_LISTEN_OFFLOAD)) {
9446 wpa_printf(MSG_DEBUG, "P2P: Listen offload not supported");
9447 return -1;
9448 }
9449
9450 if (!p2p_supported_freq(wpa_s->global->p2p, freq)) {
9451 wpa_printf(MSG_ERROR, "P2P: Input channel not supported: %u",
9452 freq);
9453 return -1;
9454 }
9455
9456 /* Get device type */
9457 dev_types_len = (wpa_s->conf->num_sec_device_types + 1) *
9458 WPS_DEV_TYPE_LEN;
9459 device_types = os_malloc(dev_types_len);
9460 if (!device_types)
9461 return -1;
9462 os_memcpy(device_types, wpa_s->conf->device_type, WPS_DEV_TYPE_LEN);
9463 os_memcpy(&device_types[WPS_DEV_TYPE_LEN], wpa_s->conf->sec_device_type,
9464 wpa_s->conf->num_sec_device_types * WPS_DEV_TYPE_LEN);
9465
9466 /* Get Probe Response IE(s) */
9467 buf = p2p_build_probe_resp_template(p2p, freq);
9468 if (!buf) {
9469 os_free(device_types);
9470 return -1;
9471 }
9472
9473 ret = wpa_drv_p2p_lo_start(wpa_s, freq, period, interval, count,
9474 device_types, dev_types_len,
9475 wpabuf_mhead_u8(buf), wpabuf_len(buf));
9476 if (ret < 0)
9477 wpa_dbg(wpa_s, MSG_DEBUG,
9478 "P2P: Failed to start P2P listen offload");
9479
9480 os_free(device_types);
9481 wpabuf_free(buf);
9482
9483 if (ret == 0) {
9484 wpa_s->p2p_lo_started = 1;
9485
9486 /* Stop current P2P listen if any */
9487 wpas_stop_listen(wpa_s);
9488 }
9489
9490 return ret;
9491}
9492
9493
9494int wpas_p2p_lo_stop(struct wpa_supplicant *wpa_s)
9495{
9496 int ret;
9497
9498 if (!wpa_s->p2p_lo_started)
9499 return 0;
9500
9501 ret = wpa_drv_p2p_lo_stop(wpa_s);
9502 if (ret < 0)
9503 wpa_dbg(wpa_s, MSG_DEBUG,
9504 "P2P: Failed to stop P2P listen offload");
9505
9506 wpa_s->p2p_lo_started = 0;
9507 return ret;
9508}