blob: dc5c16db096473ec27e7cb7dfdd6b7e9c64b098d [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
Hai Shalom899fcc72020-10-19 14:38:18 -0700294static int wpas_p2p_add_scan_freq_list(struct wpa_supplicant *wpa_s,
295 enum hostapd_hw_mode band,
296 struct wpa_driver_scan_params *params)
297{
298 struct hostapd_hw_modes *mode;
299 int num_chans = 0;
300 int *freqs, i;
301
302 mode = get_mode(wpa_s->hw.modes, wpa_s->hw.num_modes, band, 0);
303 if (!mode)
304 return -1;
305
306 if (params->freqs) {
307 while (params->freqs[num_chans])
308 num_chans++;
309 }
310
311 freqs = os_realloc(params->freqs,
312 (num_chans + mode->num_channels + 1) * sizeof(int));
313 if (!freqs)
314 return -1;
315
316 params->freqs = freqs;
317
318 for (i = 0; i < mode->num_channels; i++) {
319 if (mode->channels[i].flag & HOSTAPD_CHAN_DISABLED)
320 continue;
321 params->freqs[num_chans++] = mode->channels[i].freq;
322 }
323 params->freqs[num_chans] = 0;
324
325 return 0;
326}
327
328
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800329static void wpas_p2p_trigger_scan_cb(struct wpa_radio_work *work, int deinit)
330{
331 struct wpa_supplicant *wpa_s = work->wpa_s;
332 struct wpa_driver_scan_params *params = work->ctx;
333 int ret;
334
335 if (deinit) {
Dmitry Shmidtbd14a572014-02-18 10:33:49 -0800336 if (!work->started) {
337 wpa_scan_free_params(params);
338 return;
339 }
340
341 wpa_s->p2p_scan_work = NULL;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800342 return;
343 }
344
Dmitry Shmidtebd93af2017-02-21 13:40:44 -0800345 if (wpa_s->clear_driver_scan_cache) {
346 wpa_printf(MSG_DEBUG,
347 "Request driver to clear scan cache due to local BSS flush");
348 params->only_new_results = 1;
349 }
Hai Shalom899fcc72020-10-19 14:38:18 -0700350
351 if (wpa_s->conf->p2p_6ghz_disable && !params->freqs) {
352 wpa_printf(MSG_DEBUG,
353 "P2P: 6 GHz disabled - update the scan frequency list");
354 wpas_p2p_add_scan_freq_list(wpa_s, HOSTAPD_MODE_IEEE80211G,
355 params);
356 wpas_p2p_add_scan_freq_list(wpa_s, HOSTAPD_MODE_IEEE80211A,
357 params);
358 }
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800359 ret = wpa_drv_scan(wpa_s, params);
Dmitry Shmidtabb90a32016-12-05 15:34:39 -0800360 if (ret == 0)
361 wpa_s->curr_scan_cookie = params->scan_cookie;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800362 wpa_scan_free_params(params);
363 work->ctx = NULL;
364 if (ret) {
365 radio_work_done(work);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800366 p2p_notify_scan_trigger_status(wpa_s->global->p2p, ret);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800367 return;
368 }
369
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800370 p2p_notify_scan_trigger_status(wpa_s->global->p2p, ret);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800371 os_get_reltime(&wpa_s->scan_trigger_time);
372 wpa_s->scan_res_handler = wpas_p2p_scan_res_handler;
373 wpa_s->own_scan_requested = 1;
Dmitry Shmidtebd93af2017-02-21 13:40:44 -0800374 wpa_s->clear_driver_scan_cache = 0;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800375 wpa_s->p2p_scan_work = work;
376}
377
378
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800379static int wpas_p2p_search_social_channel(struct wpa_supplicant *wpa_s,
380 int freq)
381{
382 if (wpa_s->global->p2p_24ghz_social_channels &&
383 (freq == 2412 || freq == 2437 || freq == 2462)) {
384 /*
385 * Search all social channels regardless of whether these have
386 * been disabled for P2P operating channel use to avoid missing
387 * peers.
388 */
389 return 1;
390 }
391 return p2p_supported_freq(wpa_s->global->p2p, freq);
392}
393
394
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700395static int wpas_p2p_scan(void *ctx, enum p2p_scan_type type, int freq,
396 unsigned int num_req_dev_types,
Dmitry Shmidt04949592012-07-19 12:16:46 -0700397 const u8 *req_dev_types, const u8 *dev_id, u16 pw_id)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700398{
399 struct wpa_supplicant *wpa_s = ctx;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800400 struct wpa_driver_scan_params *params = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700401 struct wpabuf *wps_ie, *ies;
Dmitry Shmidt661b4f72014-09-29 14:58:27 -0700402 unsigned int num_channels = 0;
403 int social_channels_freq[] = { 2412, 2437, 2462, 60480 };
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800404 size_t ielen;
Dmitry Shmidt661b4f72014-09-29 14:58:27 -0700405 u8 *n, i;
Dmitry Shmidt9c175262016-03-03 10:20:07 -0800406 unsigned int bands;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700407
408 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
409 return -1;
410
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800411 if (wpa_s->p2p_scan_work) {
412 wpa_dbg(wpa_s, MSG_INFO, "P2P: Reject scan trigger since one is already pending");
413 return -1;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700414 }
415
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800416 params = os_zalloc(sizeof(*params));
417 if (params == NULL)
418 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700419
420 /* P2P Wildcard SSID */
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800421 params->num_ssids = 1;
422 n = os_malloc(P2P_WILDCARD_SSID_LEN);
423 if (n == NULL)
424 goto fail;
425 os_memcpy(n, P2P_WILDCARD_SSID, P2P_WILDCARD_SSID_LEN);
426 params->ssids[0].ssid = n;
427 params->ssids[0].ssid_len = P2P_WILDCARD_SSID_LEN;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700428
429 wpa_s->wps->dev.p2p = 1;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700430 wps_ie = wps_build_probe_req_ie(pw_id, &wpa_s->wps->dev,
431 wpa_s->wps->uuid, WPS_REQ_ENROLLEE,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700432 num_req_dev_types, req_dev_types);
433 if (wps_ie == NULL)
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800434 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700435
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700436 switch (type) {
437 case P2P_SCAN_SOCIAL:
Dmitry Shmidt661b4f72014-09-29 14:58:27 -0700438 params->freqs = os_calloc(ARRAY_SIZE(social_channels_freq) + 1,
439 sizeof(int));
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800440 if (params->freqs == NULL)
441 goto fail;
Dmitry Shmidt661b4f72014-09-29 14:58:27 -0700442 for (i = 0; i < ARRAY_SIZE(social_channels_freq); i++) {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800443 if (wpas_p2p_search_social_channel(
444 wpa_s, social_channels_freq[i]))
Dmitry Shmidt661b4f72014-09-29 14:58:27 -0700445 params->freqs[num_channels++] =
446 social_channels_freq[i];
447 }
448 params->freqs[num_channels++] = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700449 break;
450 case P2P_SCAN_FULL:
451 break;
Dmitry Shmidt203eadb2015-03-05 14:16:04 -0800452 case P2P_SCAN_SPECIFIC:
453 params->freqs = os_calloc(2, sizeof(int));
454 if (params->freqs == NULL)
455 goto fail;
456 params->freqs[0] = freq;
457 params->freqs[1] = 0;
458 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700459 case P2P_SCAN_SOCIAL_PLUS_ONE:
Dmitry Shmidt661b4f72014-09-29 14:58:27 -0700460 params->freqs = os_calloc(ARRAY_SIZE(social_channels_freq) + 2,
461 sizeof(int));
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800462 if (params->freqs == NULL)
463 goto fail;
Dmitry Shmidt661b4f72014-09-29 14:58:27 -0700464 for (i = 0; i < ARRAY_SIZE(social_channels_freq); i++) {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800465 if (wpas_p2p_search_social_channel(
466 wpa_s, social_channels_freq[i]))
Dmitry Shmidt661b4f72014-09-29 14:58:27 -0700467 params->freqs[num_channels++] =
468 social_channels_freq[i];
469 }
470 if (p2p_supported_freq(wpa_s->global->p2p, freq))
471 params->freqs[num_channels++] = freq;
472 params->freqs[num_channels++] = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700473 break;
474 }
475
Dmitry Shmidt9c175262016-03-03 10:20:07 -0800476 ielen = p2p_scan_ie_buf_len(wpa_s->global->p2p);
477 ies = wpabuf_alloc(wpabuf_len(wps_ie) + ielen);
478 if (ies == NULL) {
479 wpabuf_free(wps_ie);
480 goto fail;
481 }
482 wpabuf_put_buf(ies, wps_ie);
483 wpabuf_free(wps_ie);
484
485 bands = wpas_get_bands(wpa_s, params->freqs);
486 p2p_scan_ie(wpa_s->global->p2p, ies, dev_id, bands);
487
488 params->p2p_probe = 1;
489 n = os_malloc(wpabuf_len(ies));
490 if (n == NULL) {
491 wpabuf_free(ies);
492 goto fail;
493 }
494 os_memcpy(n, wpabuf_head(ies), wpabuf_len(ies));
495 params->extra_ies = n;
496 params->extra_ies_len = wpabuf_len(ies);
497 wpabuf_free(ies);
498
Dmitry Shmidtbd14a572014-02-18 10:33:49 -0800499 radio_remove_works(wpa_s, "p2p-scan", 0);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800500 if (radio_add_work(wpa_s, 0, "p2p-scan", 0, wpas_p2p_trigger_scan_cb,
501 params) < 0)
502 goto fail;
503 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700504
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800505fail:
506 wpa_scan_free_params(params);
507 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700508}
509
510
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700511static enum wpa_driver_if_type wpas_p2p_if_type(int p2p_group_interface)
512{
513 switch (p2p_group_interface) {
514 case P2P_GROUP_INTERFACE_PENDING:
515 return WPA_IF_P2P_GROUP;
516 case P2P_GROUP_INTERFACE_GO:
517 return WPA_IF_P2P_GO;
518 case P2P_GROUP_INTERFACE_CLIENT:
519 return WPA_IF_P2P_CLIENT;
520 }
521
522 return WPA_IF_P2P_GROUP;
523}
524
525
526static struct wpa_supplicant * wpas_get_p2p_group(struct wpa_supplicant *wpa_s,
527 const u8 *ssid,
528 size_t ssid_len, int *go)
529{
530 struct wpa_ssid *s;
531
532 for (wpa_s = wpa_s->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
533 for (s = wpa_s->conf->ssid; s; s = s->next) {
534 if (s->disabled != 0 || !s->p2p_group ||
535 s->ssid_len != ssid_len ||
536 os_memcmp(ssid, s->ssid, ssid_len) != 0)
537 continue;
538 if (s->mode == WPAS_MODE_P2P_GO &&
539 s != wpa_s->current_ssid)
540 continue;
541 if (go)
542 *go = s->mode == WPAS_MODE_P2P_GO;
543 return wpa_s;
544 }
545 }
546
547 return NULL;
548}
549
550
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800551static void run_wpas_p2p_disconnect(void *eloop_ctx, void *timeout_ctx)
552{
553 struct wpa_supplicant *wpa_s = eloop_ctx;
554 wpa_printf(MSG_DEBUG,
555 "P2P: Complete previously requested removal of %s",
556 wpa_s->ifname);
557 wpas_p2p_disconnect(wpa_s);
558}
559
560
561static int wpas_p2p_disconnect_safely(struct wpa_supplicant *wpa_s,
562 struct wpa_supplicant *calling_wpa_s)
563{
564 if (calling_wpa_s == wpa_s && wpa_s &&
565 wpa_s->p2p_group_interface != NOT_P2P_GROUP_INTERFACE) {
566 /*
567 * The calling wpa_s instance is going to be removed. Do that
568 * from an eloop callback to keep the instance available until
Hai Shalom899fcc72020-10-19 14:38:18 -0700569 * the caller has returned. This may be needed, e.g., to provide
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800570 * control interface responses on the per-interface socket.
571 */
572 if (eloop_register_timeout(0, 0, run_wpas_p2p_disconnect,
573 wpa_s, NULL) < 0)
574 return -1;
575 return 0;
576 }
577
578 return wpas_p2p_disconnect(wpa_s);
579}
580
581
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800582/* Determine total number of clients in active groups where we are the GO */
583static unsigned int p2p_group_go_member_count(struct wpa_supplicant *wpa_s)
584{
585 unsigned int count = 0;
586 struct wpa_ssid *s;
587
588 for (wpa_s = wpa_s->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
589 for (s = wpa_s->conf->ssid; s; s = s->next) {
590 wpa_printf(MSG_DEBUG,
591 "P2P: sup:%p ssid:%p disabled:%d p2p:%d mode:%d",
592 wpa_s, s, s->disabled, s->p2p_group,
593 s->mode);
594 if (!s->disabled && s->p2p_group &&
595 s->mode == WPAS_MODE_P2P_GO) {
596 count += p2p_get_group_num_members(
597 wpa_s->p2p_group);
598 }
599 }
600 }
601
602 return count;
603}
604
605
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800606static unsigned int p2p_is_active_persistent_group(struct wpa_supplicant *wpa_s)
607{
608 return !wpa_s->p2p_mgmt && wpa_s->current_ssid &&
609 !wpa_s->current_ssid->disabled &&
610 wpa_s->current_ssid->p2p_group &&
611 wpa_s->current_ssid->p2p_persistent_group;
612}
613
614
615static unsigned int p2p_is_active_persistent_go(struct wpa_supplicant *wpa_s)
616{
617 return p2p_is_active_persistent_group(wpa_s) &&
618 wpa_s->current_ssid->mode == WPAS_MODE_P2P_GO;
619}
620
621
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800622/* Find an interface for a P2P group where we are the GO */
623static struct wpa_supplicant *
624wpas_p2p_get_go_group(struct wpa_supplicant *wpa_s)
625{
626 struct wpa_supplicant *save = NULL;
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800627
628 if (!wpa_s)
629 return NULL;
630
631 for (wpa_s = wpa_s->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800632 if (!p2p_is_active_persistent_go(wpa_s))
633 continue;
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800634
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800635 /* Prefer a group with connected clients */
636 if (p2p_get_group_num_members(wpa_s->p2p_group))
637 return wpa_s;
638 save = wpa_s;
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800639 }
640
641 /* No group with connected clients, so pick the one without (if any) */
642 return save;
643}
644
645
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800646static unsigned int p2p_is_active_persistent_cli(struct wpa_supplicant *wpa_s)
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800647{
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800648 return p2p_is_active_persistent_group(wpa_s) &&
649 wpa_s->current_ssid->mode == WPAS_MODE_INFRA;
650}
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800651
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800652
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800653/* Find an interface for a P2P group where we are the P2P Client */
654static struct wpa_supplicant *
655wpas_p2p_get_cli_group(struct wpa_supplicant *wpa_s)
656{
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800657 for (wpa_s = wpa_s->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800658 if (p2p_is_active_persistent_cli(wpa_s))
659 return wpa_s;
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800660 }
661
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800662 return NULL;
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800663}
664
665
666/* Find a persistent group where we are the GO */
667static struct wpa_ssid *
668wpas_p2p_get_persistent_go(struct wpa_supplicant *wpa_s)
669{
670 struct wpa_ssid *s;
671
672 for (s = wpa_s->conf->ssid; s; s = s->next) {
673 if (s->disabled == 2 && s->mode == WPAS_MODE_P2P_GO)
674 return s;
675 }
676
677 return NULL;
678}
679
680
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800681static u8 p2ps_group_capability(void *ctx, u8 incoming, u8 role,
682 unsigned int *force_freq,
683 unsigned int *pref_freq)
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800684{
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800685 struct wpa_supplicant *wpa_s = ctx;
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800686 struct wpa_ssid *s;
687 u8 conncap = P2PS_SETUP_NONE;
688 unsigned int owned_members = 0;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800689 struct wpa_supplicant *go_wpa_s, *cli_wpa_s;
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800690 struct wpa_ssid *persistent_go;
691 int p2p_no_group_iface;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800692 unsigned int pref_freq_list[P2P_MAX_PREF_CHANNELS], size;
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800693
694 wpa_printf(MSG_DEBUG, "P2P: Conncap - in:%d role:%d", incoming, role);
695
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800696 if (force_freq)
697 *force_freq = 0;
698 if (pref_freq)
699 *pref_freq = 0;
700
701 size = P2P_MAX_PREF_CHANNELS;
702 if (force_freq && pref_freq &&
703 !wpas_p2p_setup_freqs(wpa_s, 0, (int *) force_freq,
704 (int *) pref_freq, 0, pref_freq_list, &size))
705 wpas_p2p_set_own_freq_preference(wpa_s,
706 *force_freq ? *force_freq :
707 *pref_freq);
708
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800709 /*
710 * For non-concurrent capable devices:
711 * If persistent_go, then no new.
712 * If GO, then no client.
713 * If client, then no GO.
714 */
715 go_wpa_s = wpas_p2p_get_go_group(wpa_s);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800716 if (go_wpa_s)
717 owned_members = p2p_get_group_num_members(go_wpa_s->p2p_group);
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800718 persistent_go = wpas_p2p_get_persistent_go(wpa_s);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800719 p2p_no_group_iface = !wpas_p2p_create_iface(wpa_s);
720 cli_wpa_s = wpas_p2p_get_cli_group(wpa_s);
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800721
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800722 wpa_printf(MSG_DEBUG,
723 "P2P: GO(iface)=%p members=%u CLI(iface)=%p persistent(ssid)=%p",
724 go_wpa_s, owned_members, cli_wpa_s, persistent_go);
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800725
726 /* If not concurrent, restrict our choices */
727 if (p2p_no_group_iface) {
728 wpa_printf(MSG_DEBUG, "P2P: p2p_no_group_iface");
729
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800730 if (cli_wpa_s)
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800731 return P2PS_SETUP_NONE;
732
733 if (go_wpa_s) {
734 if (role == P2PS_SETUP_CLIENT ||
735 incoming == P2PS_SETUP_GROUP_OWNER ||
736 p2p_client_limit_reached(go_wpa_s->p2p_group))
737 return P2PS_SETUP_NONE;
738
739 return P2PS_SETUP_GROUP_OWNER;
740 }
741
742 if (persistent_go) {
743 if (role == P2PS_SETUP_NONE || role == P2PS_SETUP_NEW) {
744 if (!incoming)
745 return P2PS_SETUP_GROUP_OWNER |
746 P2PS_SETUP_CLIENT;
747 if (incoming == P2PS_SETUP_NEW) {
748 u8 r;
749
750 if (os_get_random(&r, sizeof(r)) < 0 ||
751 (r & 1))
752 return P2PS_SETUP_CLIENT;
753 return P2PS_SETUP_GROUP_OWNER;
754 }
755 }
756 }
757 }
758
759 /* If a required role has been specified, handle it here */
760 if (role && role != P2PS_SETUP_NEW) {
761 switch (incoming) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800762 case P2PS_SETUP_GROUP_OWNER | P2PS_SETUP_NEW:
763 case P2PS_SETUP_GROUP_OWNER | P2PS_SETUP_CLIENT:
764 /*
765 * Peer has an active GO, so if the role allows it and
766 * we do not have any active roles, become client.
767 */
768 if ((role & P2PS_SETUP_CLIENT) && !go_wpa_s &&
769 !cli_wpa_s)
770 return P2PS_SETUP_CLIENT;
771
772 /* fall through */
773
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800774 case P2PS_SETUP_NONE:
775 case P2PS_SETUP_NEW:
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800776 conncap = role;
777 goto grp_owner;
778
779 case P2PS_SETUP_GROUP_OWNER:
780 /*
781 * Must be a complimentary role - cannot be a client to
782 * more than one peer.
783 */
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800784 if (incoming == role || cli_wpa_s)
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800785 return P2PS_SETUP_NONE;
786
787 return P2PS_SETUP_CLIENT;
788
789 case P2PS_SETUP_CLIENT:
790 /* Must be a complimentary role */
791 if (incoming != role) {
792 conncap = P2PS_SETUP_GROUP_OWNER;
793 goto grp_owner;
794 }
Roshan Pius3a1667e2018-07-03 15:17:14 -0700795 /* fall through */
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800796
797 default:
798 return P2PS_SETUP_NONE;
799 }
800 }
801
802 /*
803 * For now, we only will support ownership of one group, and being a
804 * client of one group. Therefore, if we have either an existing GO
805 * group, or an existing client group, we will not do a new GO
806 * negotiation, but rather try to re-use the existing groups.
807 */
808 switch (incoming) {
809 case P2PS_SETUP_NONE:
810 case P2PS_SETUP_NEW:
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800811 if (cli_wpa_s)
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800812 conncap = P2PS_SETUP_GROUP_OWNER;
813 else if (!owned_members)
814 conncap = P2PS_SETUP_NEW;
815 else if (incoming == P2PS_SETUP_NONE)
816 conncap = P2PS_SETUP_GROUP_OWNER | P2PS_SETUP_CLIENT;
817 else
818 conncap = P2PS_SETUP_CLIENT;
819 break;
820
821 case P2PS_SETUP_CLIENT:
822 conncap = P2PS_SETUP_GROUP_OWNER;
823 break;
824
825 case P2PS_SETUP_GROUP_OWNER:
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800826 if (!cli_wpa_s)
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800827 conncap = P2PS_SETUP_CLIENT;
828 break;
829
830 case P2PS_SETUP_GROUP_OWNER | P2PS_SETUP_NEW:
831 case P2PS_SETUP_GROUP_OWNER | P2PS_SETUP_CLIENT:
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800832 if (cli_wpa_s)
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800833 conncap = P2PS_SETUP_GROUP_OWNER;
834 else {
835 u8 r;
836
837 if (os_get_random(&r, sizeof(r)) < 0 ||
838 (r & 1))
839 conncap = P2PS_SETUP_CLIENT;
840 else
841 conncap = P2PS_SETUP_GROUP_OWNER;
842 }
843 break;
844
845 default:
846 return P2PS_SETUP_NONE;
847 }
848
849grp_owner:
850 if ((conncap & P2PS_SETUP_GROUP_OWNER) ||
851 (!incoming && (conncap & P2PS_SETUP_NEW))) {
852 if (go_wpa_s && p2p_client_limit_reached(go_wpa_s->p2p_group))
853 conncap &= ~P2PS_SETUP_GROUP_OWNER;
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800854
855 s = wpas_p2p_get_persistent_go(wpa_s);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800856 if (!s && !go_wpa_s && p2p_no_group_iface) {
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800857 p2p_set_intended_addr(wpa_s->global->p2p,
Dmitry Shmidt9c175262016-03-03 10:20:07 -0800858 wpa_s->p2p_mgmt ?
859 wpa_s->parent->own_addr :
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800860 wpa_s->own_addr);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800861 } else if (!s && !go_wpa_s) {
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800862 if (wpas_p2p_add_group_interface(wpa_s,
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -0800863 WPA_IF_P2P_GROUP) < 0) {
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800864 wpa_printf(MSG_ERROR,
865 "P2P: Failed to allocate a new interface for the group");
866 return P2PS_SETUP_NONE;
867 }
868 wpa_s->global->pending_group_iface_for_p2ps = 1;
869 p2p_set_intended_addr(wpa_s->global->p2p,
870 wpa_s->pending_interface_addr);
871 }
872 }
873
874 return conncap;
875}
876
877
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700878static int wpas_p2p_group_delete(struct wpa_supplicant *wpa_s,
879 enum p2p_group_removal_reason removal_reason)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700880{
881 struct wpa_ssid *ssid;
882 char *gtype;
883 const char *reason;
884
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700885 ssid = wpa_s->current_ssid;
886 if (ssid == NULL) {
887 /*
888 * The current SSID was not known, but there may still be a
Dmitry Shmidtaa532512012-09-24 10:35:31 -0700889 * pending P2P group interface waiting for provisioning or a
890 * P2P group that is trying to reconnect.
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700891 */
892 ssid = wpa_s->conf->ssid;
893 while (ssid) {
Jouni Malinen9d712832012-10-05 11:01:57 -0700894 if (ssid->p2p_group && ssid->disabled != 2)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700895 break;
896 ssid = ssid->next;
897 }
Jouni Malinen5c44edb2012-08-31 21:35:32 +0300898 if (ssid == NULL &&
899 wpa_s->p2p_group_interface == NOT_P2P_GROUP_INTERFACE)
900 {
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700901 wpa_printf(MSG_ERROR, "P2P: P2P group interface "
902 "not found");
903 return -1;
904 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700905 }
906 if (wpa_s->p2p_group_interface == P2P_GROUP_INTERFACE_GO)
907 gtype = "GO";
908 else if (wpa_s->p2p_group_interface == P2P_GROUP_INTERFACE_CLIENT ||
909 (ssid && ssid->mode == WPAS_MODE_INFRA)) {
910 wpa_s->reassociate = 0;
911 wpa_s->disconnected = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700912 gtype = "client";
913 } else
914 gtype = "GO";
Dmitry Shmidt661b4f72014-09-29 14:58:27 -0700915
916 if (removal_reason != P2P_GROUP_REMOVAL_SILENT && ssid)
917 wpas_notify_p2p_group_removed(wpa_s, ssid, gtype);
918
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800919 if (os_strcmp(gtype, "client") == 0) {
Dmitry Shmidt661b4f72014-09-29 14:58:27 -0700920 wpa_supplicant_deauthenticate(wpa_s, WLAN_REASON_DEAUTH_LEAVING);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800921 if (eloop_is_timeout_registered(wpas_p2p_psk_failure_removal,
922 wpa_s, NULL)) {
923 wpa_printf(MSG_DEBUG,
924 "P2P: PSK failure removal was scheduled, so use PSK failure as reason for group removal");
925 removal_reason = P2P_GROUP_REMOVAL_PSK_FAILURE;
926 eloop_cancel_timeout(wpas_p2p_psk_failure_removal,
927 wpa_s, NULL);
928 }
929 }
Dmitry Shmidt661b4f72014-09-29 14:58:27 -0700930
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700931 if (wpa_s->cross_connect_in_use) {
932 wpa_s->cross_connect_in_use = 0;
Dmitry Shmidt9c175262016-03-03 10:20:07 -0800933 wpa_msg_global(wpa_s->p2pdev, MSG_INFO,
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -0700934 P2P_EVENT_CROSS_CONNECT_DISABLE "%s %s",
935 wpa_s->ifname, wpa_s->cross_connect_uplink);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700936 }
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700937 switch (removal_reason) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700938 case P2P_GROUP_REMOVAL_REQUESTED:
939 reason = " reason=REQUESTED";
940 break;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700941 case P2P_GROUP_REMOVAL_FORMATION_FAILED:
942 reason = " reason=FORMATION_FAILED";
943 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700944 case P2P_GROUP_REMOVAL_IDLE_TIMEOUT:
945 reason = " reason=IDLE";
946 break;
947 case P2P_GROUP_REMOVAL_UNAVAILABLE:
948 reason = " reason=UNAVAILABLE";
949 break;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700950 case P2P_GROUP_REMOVAL_GO_ENDING_SESSION:
951 reason = " reason=GO_ENDING_SESSION";
952 break;
Dmitry Shmidt391c59f2013-09-03 12:16:28 -0700953 case P2P_GROUP_REMOVAL_PSK_FAILURE:
954 reason = " reason=PSK_FAILURE";
955 break;
Dmitry Shmidt04f534e2013-12-09 15:50:16 -0800956 case P2P_GROUP_REMOVAL_FREQ_CONFLICT:
957 reason = " reason=FREQ_CONFLICT";
958 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700959 default:
960 reason = "";
961 break;
962 }
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700963 if (removal_reason != P2P_GROUP_REMOVAL_SILENT) {
Dmitry Shmidt9c175262016-03-03 10:20:07 -0800964 wpa_msg_global(wpa_s->p2pdev, MSG_INFO,
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -0700965 P2P_EVENT_GROUP_REMOVED "%s %s%s",
966 wpa_s->ifname, gtype, reason);
Dmitry Shmidt04949592012-07-19 12:16:46 -0700967 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700968
Dmitry Shmidt04f534e2013-12-09 15:50:16 -0800969 if (eloop_cancel_timeout(wpas_p2p_group_freq_conflict, wpa_s, NULL) > 0)
970 wpa_printf(MSG_DEBUG, "P2P: Cancelled P2P group freq_conflict timeout");
Dmitry Shmidt04949592012-07-19 12:16:46 -0700971 if (eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL) > 0)
972 wpa_printf(MSG_DEBUG, "P2P: Cancelled P2P group idle timeout");
Dmitry Shmidtf8623282013-02-20 14:34:59 -0800973 if (eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
Dmitry Shmidt9c175262016-03-03 10:20:07 -0800974 wpa_s->p2pdev, NULL) > 0) {
Dmitry Shmidtf8623282013-02-20 14:34:59 -0800975 wpa_printf(MSG_DEBUG, "P2P: Cancelled P2P group formation "
976 "timeout");
Dmitry Shmidt2f023192013-03-12 12:44:17 -0700977 wpa_s->p2p_in_provisioning = 0;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800978 wpas_p2p_group_formation_failed(wpa_s, 1);
Dmitry Shmidt2f023192013-03-12 12:44:17 -0700979 }
Dmitry Shmidt04949592012-07-19 12:16:46 -0700980
Dmitry Shmidt15907092014-03-25 10:42:57 -0700981 wpa_s->p2p_in_invitation = 0;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800982 eloop_cancel_timeout(wpas_p2p_move_go, wpa_s, NULL);
983 eloop_cancel_timeout(wpas_p2p_reconsider_moving_go, wpa_s, NULL);
Dmitry Shmidt15907092014-03-25 10:42:57 -0700984
Dmitry Shmidt96571392013-10-14 12:54:46 -0700985 /*
986 * Make sure wait for the first client does not remain active after the
987 * group has been removed.
988 */
989 wpa_s->global->p2p_go_wait_client.sec = 0;
990
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700991 if (wpa_s->p2p_group_interface != NOT_P2P_GROUP_INTERFACE) {
992 struct wpa_global *global;
993 char *ifname;
994 enum wpa_driver_if_type type;
995 wpa_printf(MSG_DEBUG, "P2P: Remove group interface %s",
996 wpa_s->ifname);
997 global = wpa_s->global;
998 ifname = os_strdup(wpa_s->ifname);
999 type = wpas_p2p_if_type(wpa_s->p2p_group_interface);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001000 eloop_cancel_timeout(run_wpas_p2p_disconnect, wpa_s, NULL);
Dmitry Shmidte15c7b52011-08-03 15:04:35 -07001001 wpa_supplicant_remove_iface(wpa_s->global, wpa_s, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001002 wpa_s = global->ifaces;
1003 if (wpa_s && ifname)
1004 wpa_drv_if_remove(wpa_s, type, ifname);
1005 os_free(ifname);
Jouni Malinen2b89da82012-08-31 22:04:41 +03001006 return 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001007 }
1008
Dmitry Shmidt9c175262016-03-03 10:20:07 -08001009 /*
1010 * The primary interface was used for P2P group operations, so
1011 * need to reset its p2pdev.
1012 */
1013 wpa_s->p2pdev = wpa_s->parent;
1014
Dmitry Shmidt96571392013-10-14 12:54:46 -07001015 if (!wpa_s->p2p_go_group_formation_completed) {
1016 wpa_s->global->p2p_group_formation = NULL;
1017 wpa_s->p2p_in_provisioning = 0;
1018 }
1019
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001020 wpa_s->show_group_started = 0;
1021 os_free(wpa_s->go_params);
1022 wpa_s->go_params = NULL;
1023
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001024 os_free(wpa_s->p2p_group_common_freqs);
1025 wpa_s->p2p_group_common_freqs = NULL;
1026 wpa_s->p2p_group_common_freqs_num = 0;
Hai Shalom021b0b52019-04-10 11:17:58 -07001027 wpa_s->p2p_go_do_acs = 0;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001028
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001029 wpa_s->waiting_presence_resp = 0;
1030
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001031 wpa_printf(MSG_DEBUG, "P2P: Remove temporary group network");
1032 if (ssid && (ssid->p2p_group ||
1033 ssid->mode == WPAS_MODE_P2P_GROUP_FORMATION ||
1034 (ssid->key_mgmt & WPA_KEY_MGMT_WPS))) {
1035 int id = ssid->id;
Jouni Malinen75ecf522011-06-27 15:19:46 -07001036 if (ssid == wpa_s->current_ssid) {
1037 wpa_sm_set_config(wpa_s->wpa, NULL);
1038 eapol_sm_notify_config(wpa_s->eapol, NULL, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001039 wpa_s->current_ssid = NULL;
Jouni Malinen75ecf522011-06-27 15:19:46 -07001040 }
1041 /*
1042 * Networks objects created during any P2P activities are not
1043 * exposed out as they might/will confuse certain non-P2P aware
1044 * applications since these network objects won't behave like
1045 * regular ones.
1046 *
1047 * Likewise, we don't send out network removed signals for such
1048 * network objects.
1049 */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001050 wpa_config_remove_network(wpa_s->conf, id);
1051 wpa_supplicant_clear_status(wpa_s);
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08001052 wpa_supplicant_cancel_sched_scan(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001053 } else {
1054 wpa_printf(MSG_DEBUG, "P2P: Temporary group network not "
1055 "found");
1056 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07001057 if (wpa_s->ap_iface)
1058 wpa_supplicant_ap_deinit(wpa_s);
1059 else
1060 wpa_drv_deinit_p2p_cli(wpa_s);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001061
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001062 os_memset(wpa_s->go_dev_addr, 0, ETH_ALEN);
1063
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001064 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001065}
1066
1067
1068static int wpas_p2p_persistent_group(struct wpa_supplicant *wpa_s,
1069 u8 *go_dev_addr,
1070 const u8 *ssid, size_t ssid_len)
1071{
1072 struct wpa_bss *bss;
1073 const u8 *bssid;
1074 struct wpabuf *p2p;
1075 u8 group_capab;
1076 const u8 *addr;
1077
1078 if (wpa_s->go_params)
1079 bssid = wpa_s->go_params->peer_interface_addr;
1080 else
1081 bssid = wpa_s->bssid;
1082
1083 bss = wpa_bss_get(wpa_s, bssid, ssid, ssid_len);
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001084 if (bss == NULL && wpa_s->go_params &&
1085 !is_zero_ether_addr(wpa_s->go_params->peer_device_addr))
1086 bss = wpa_bss_get_p2p_dev_addr(
1087 wpa_s, wpa_s->go_params->peer_device_addr);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001088 if (bss == NULL) {
1089 u8 iface_addr[ETH_ALEN];
1090 if (p2p_get_interface_addr(wpa_s->global->p2p, bssid,
1091 iface_addr) == 0)
1092 bss = wpa_bss_get(wpa_s, iface_addr, ssid, ssid_len);
1093 }
1094 if (bss == NULL) {
1095 wpa_printf(MSG_DEBUG, "P2P: Could not figure out whether "
1096 "group is persistent - BSS " MACSTR " not found",
1097 MAC2STR(bssid));
1098 return 0;
1099 }
1100
1101 p2p = wpa_bss_get_vendor_ie_multi(bss, P2P_IE_VENDOR_TYPE);
Dmitry Shmidt96571392013-10-14 12:54:46 -07001102 if (p2p == NULL)
1103 p2p = wpa_bss_get_vendor_ie_multi_beacon(bss,
1104 P2P_IE_VENDOR_TYPE);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001105 if (p2p == NULL) {
1106 wpa_printf(MSG_DEBUG, "P2P: Could not figure out whether "
1107 "group is persistent - BSS " MACSTR
1108 " did not include P2P IE", MAC2STR(bssid));
1109 wpa_hexdump(MSG_DEBUG, "P2P: Probe Response IEs",
1110 (u8 *) (bss + 1), bss->ie_len);
1111 wpa_hexdump(MSG_DEBUG, "P2P: Beacon IEs",
1112 ((u8 *) bss + 1) + bss->ie_len,
1113 bss->beacon_ie_len);
1114 return 0;
1115 }
1116
1117 group_capab = p2p_get_group_capab(p2p);
1118 addr = p2p_get_go_dev_addr(p2p);
1119 wpa_printf(MSG_DEBUG, "P2P: Checking whether group is persistent: "
1120 "group_capab=0x%x", group_capab);
1121 if (addr) {
1122 os_memcpy(go_dev_addr, addr, ETH_ALEN);
1123 wpa_printf(MSG_DEBUG, "P2P: GO Device Address " MACSTR,
1124 MAC2STR(addr));
1125 } else
1126 os_memset(go_dev_addr, 0, ETH_ALEN);
1127 wpabuf_free(p2p);
1128
1129 wpa_printf(MSG_DEBUG, "P2P: BSS " MACSTR " group_capab=0x%x "
1130 "go_dev_addr=" MACSTR,
1131 MAC2STR(bssid), group_capab, MAC2STR(go_dev_addr));
1132
Dmitry Shmidt7f2c7532016-08-15 09:48:12 -07001133 return !!(group_capab & P2P_GROUP_CAPAB_PERSISTENT_GROUP);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001134}
1135
1136
Jouni Malinen75ecf522011-06-27 15:19:46 -07001137static int wpas_p2p_store_persistent_group(struct wpa_supplicant *wpa_s,
1138 struct wpa_ssid *ssid,
1139 const u8 *go_dev_addr)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001140{
1141 struct wpa_ssid *s;
1142 int changed = 0;
1143
1144 wpa_printf(MSG_DEBUG, "P2P: Storing credentials for a persistent "
1145 "group (GO Dev Addr " MACSTR ")", MAC2STR(go_dev_addr));
1146 for (s = wpa_s->conf->ssid; s; s = s->next) {
1147 if (s->disabled == 2 &&
1148 os_memcmp(go_dev_addr, s->bssid, ETH_ALEN) == 0 &&
1149 s->ssid_len == ssid->ssid_len &&
1150 os_memcmp(ssid->ssid, s->ssid, ssid->ssid_len) == 0)
1151 break;
1152 }
1153
1154 if (s) {
1155 wpa_printf(MSG_DEBUG, "P2P: Update existing persistent group "
1156 "entry");
1157 if (ssid->passphrase && !s->passphrase)
1158 changed = 1;
1159 else if (ssid->passphrase && s->passphrase &&
1160 os_strcmp(ssid->passphrase, s->passphrase) != 0)
1161 changed = 1;
1162 } else {
1163 wpa_printf(MSG_DEBUG, "P2P: Create a new persistent group "
1164 "entry");
1165 changed = 1;
1166 s = wpa_config_add_network(wpa_s->conf);
1167 if (s == NULL)
Jouni Malinen75ecf522011-06-27 15:19:46 -07001168 return -1;
1169
1170 /*
1171 * Instead of network_added we emit persistent_group_added
1172 * notification. Also to keep the defense checks in
1173 * persistent_group obj registration method, we set the
1174 * relevant flags in s to designate it as a persistent group.
1175 */
1176 s->p2p_group = 1;
1177 s->p2p_persistent_group = 1;
1178 wpas_notify_persistent_group_added(wpa_s, s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001179 wpa_config_set_network_defaults(s);
1180 }
1181
1182 s->p2p_group = 1;
1183 s->p2p_persistent_group = 1;
1184 s->disabled = 2;
1185 s->bssid_set = 1;
1186 os_memcpy(s->bssid, go_dev_addr, ETH_ALEN);
1187 s->mode = ssid->mode;
1188 s->auth_alg = WPA_AUTH_ALG_OPEN;
1189 s->key_mgmt = WPA_KEY_MGMT_PSK;
1190 s->proto = WPA_PROTO_RSN;
Dmitry Shmidte4663042016-04-04 10:07:49 -07001191 s->pbss = ssid->pbss;
1192 s->pairwise_cipher = ssid->pbss ? WPA_CIPHER_GCMP : WPA_CIPHER_CCMP;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001193 s->export_keys = 1;
1194 if (ssid->passphrase) {
1195 os_free(s->passphrase);
1196 s->passphrase = os_strdup(ssid->passphrase);
1197 }
1198 if (ssid->psk_set) {
1199 s->psk_set = 1;
1200 os_memcpy(s->psk, ssid->psk, 32);
1201 }
1202 if (s->passphrase && !s->psk_set)
1203 wpa_config_update_psk(s);
1204 if (s->ssid == NULL || s->ssid_len < ssid->ssid_len) {
1205 os_free(s->ssid);
1206 s->ssid = os_malloc(ssid->ssid_len);
1207 }
1208 if (s->ssid) {
1209 s->ssid_len = ssid->ssid_len;
1210 os_memcpy(s->ssid, ssid->ssid, s->ssid_len);
1211 }
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07001212 if (ssid->mode == WPAS_MODE_P2P_GO && wpa_s->global->add_psk) {
1213 dl_list_add(&s->psk_list, &wpa_s->global->add_psk->list);
1214 wpa_s->global->add_psk = NULL;
1215 changed = 1;
1216 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001217
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001218 if (changed && wpa_s->conf->update_config &&
1219 wpa_config_write(wpa_s->confname, wpa_s->conf)) {
1220 wpa_printf(MSG_DEBUG, "P2P: Failed to update configuration");
1221 }
Jouni Malinen75ecf522011-06-27 15:19:46 -07001222
1223 return s->id;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001224}
1225
1226
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001227static void wpas_p2p_add_persistent_group_client(struct wpa_supplicant *wpa_s,
1228 const u8 *addr)
1229{
1230 struct wpa_ssid *ssid, *s;
1231 u8 *n;
1232 size_t i;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001233 int found = 0;
Dmitry Shmidt8bd70b72015-05-26 16:02:19 -07001234 struct wpa_supplicant *p2p_wpa_s = wpa_s->global->p2p_init_wpa_s;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001235
1236 ssid = wpa_s->current_ssid;
1237 if (ssid == NULL || ssid->mode != WPAS_MODE_P2P_GO ||
1238 !ssid->p2p_persistent_group)
1239 return;
1240
Dmitry Shmidt8bd70b72015-05-26 16:02:19 -07001241 for (s = p2p_wpa_s->conf->ssid; s; s = s->next) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001242 if (s->disabled != 2 || s->mode != WPAS_MODE_P2P_GO)
1243 continue;
1244
1245 if (s->ssid_len == ssid->ssid_len &&
1246 os_memcmp(s->ssid, ssid->ssid, s->ssid_len) == 0)
1247 break;
1248 }
1249
1250 if (s == NULL)
1251 return;
1252
1253 for (i = 0; s->p2p_client_list && i < s->num_p2p_clients; i++) {
Dmitry Shmidtff787d52015-01-12 13:01:47 -08001254 if (os_memcmp(s->p2p_client_list + i * 2 * ETH_ALEN, addr,
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001255 ETH_ALEN) != 0)
1256 continue;
1257
1258 if (i == s->num_p2p_clients - 1)
1259 return; /* already the most recent entry */
1260
1261 /* move the entry to mark it most recent */
Dmitry Shmidtff787d52015-01-12 13:01:47 -08001262 os_memmove(s->p2p_client_list + i * 2 * ETH_ALEN,
1263 s->p2p_client_list + (i + 1) * 2 * ETH_ALEN,
1264 (s->num_p2p_clients - i - 1) * 2 * ETH_ALEN);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001265 os_memcpy(s->p2p_client_list +
Dmitry Shmidtff787d52015-01-12 13:01:47 -08001266 (s->num_p2p_clients - 1) * 2 * ETH_ALEN, addr,
1267 ETH_ALEN);
1268 os_memset(s->p2p_client_list +
1269 (s->num_p2p_clients - 1) * 2 * ETH_ALEN + ETH_ALEN,
1270 0xff, ETH_ALEN);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001271 found = 1;
1272 break;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001273 }
1274
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001275 if (!found && s->num_p2p_clients < P2P_MAX_STORED_CLIENTS) {
1276 n = os_realloc_array(s->p2p_client_list,
Dmitry Shmidtff787d52015-01-12 13:01:47 -08001277 s->num_p2p_clients + 1, 2 * ETH_ALEN);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001278 if (n == NULL)
1279 return;
Dmitry Shmidtff787d52015-01-12 13:01:47 -08001280 os_memcpy(n + s->num_p2p_clients * 2 * ETH_ALEN, addr,
1281 ETH_ALEN);
1282 os_memset(n + s->num_p2p_clients * 2 * ETH_ALEN + ETH_ALEN,
1283 0xff, ETH_ALEN);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001284 s->p2p_client_list = n;
1285 s->num_p2p_clients++;
Dmitry Shmidt6aa8ae42014-07-07 09:31:33 -07001286 } else if (!found && s->p2p_client_list) {
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001287 /* Not enough room for an additional entry - drop the oldest
1288 * entry */
1289 os_memmove(s->p2p_client_list,
Dmitry Shmidtff787d52015-01-12 13:01:47 -08001290 s->p2p_client_list + 2 * ETH_ALEN,
1291 (s->num_p2p_clients - 1) * 2 * ETH_ALEN);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001292 os_memcpy(s->p2p_client_list +
Dmitry Shmidtff787d52015-01-12 13:01:47 -08001293 (s->num_p2p_clients - 1) * 2 * ETH_ALEN,
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001294 addr, ETH_ALEN);
Dmitry Shmidtff787d52015-01-12 13:01:47 -08001295 os_memset(s->p2p_client_list +
1296 (s->num_p2p_clients - 1) * 2 * ETH_ALEN + ETH_ALEN,
1297 0xff, ETH_ALEN);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001298 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001299
Dmitry Shmidt8bd70b72015-05-26 16:02:19 -07001300 if (p2p_wpa_s->conf->update_config &&
1301 wpa_config_write(p2p_wpa_s->confname, p2p_wpa_s->conf))
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001302 wpa_printf(MSG_DEBUG, "P2P: Failed to update configuration");
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001303}
1304
1305
Dmitry Shmidt7f0b69e2014-07-28 10:35:20 -07001306static void wpas_p2p_group_started(struct wpa_supplicant *wpa_s,
1307 int go, struct wpa_ssid *ssid, int freq,
1308 const u8 *psk, const char *passphrase,
1309 const u8 *go_dev_addr, int persistent,
1310 const char *extra)
1311{
1312 const char *ssid_txt;
1313 char psk_txt[65];
1314
1315 if (psk)
1316 wpa_snprintf_hex(psk_txt, sizeof(psk_txt), psk, 32);
1317 else
1318 psk_txt[0] = '\0';
1319
1320 if (ssid)
1321 ssid_txt = wpa_ssid_txt(ssid->ssid, ssid->ssid_len);
1322 else
1323 ssid_txt = "";
1324
1325 if (passphrase && passphrase[0] == '\0')
1326 passphrase = NULL;
1327
1328 /*
1329 * Include PSK/passphrase only in the control interface message and
1330 * leave it out from the debug log entry.
1331 */
Dmitry Shmidt9c175262016-03-03 10:20:07 -08001332 wpa_msg_global_ctrl(wpa_s->p2pdev, MSG_INFO,
Dmitry Shmidt7f0b69e2014-07-28 10:35:20 -07001333 P2P_EVENT_GROUP_STARTED
1334 "%s %s ssid=\"%s\" freq=%d%s%s%s%s%s go_dev_addr="
1335 MACSTR "%s%s",
1336 wpa_s->ifname, go ? "GO" : "client", ssid_txt, freq,
1337 psk ? " psk=" : "", psk_txt,
1338 passphrase ? " passphrase=\"" : "",
1339 passphrase ? passphrase : "",
1340 passphrase ? "\"" : "",
1341 MAC2STR(go_dev_addr),
1342 persistent ? " [PERSISTENT]" : "", extra);
1343 wpa_printf(MSG_INFO, P2P_EVENT_GROUP_STARTED
1344 "%s %s ssid=\"%s\" freq=%d go_dev_addr=" MACSTR "%s%s",
1345 wpa_s->ifname, go ? "GO" : "client", ssid_txt, freq,
1346 MAC2STR(go_dev_addr), persistent ? " [PERSISTENT]" : "",
1347 extra);
1348}
1349
1350
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001351static void wpas_group_formation_completed(struct wpa_supplicant *wpa_s,
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07001352 int success, int already_deleted)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001353{
1354 struct wpa_ssid *ssid;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001355 int client;
1356 int persistent;
1357 u8 go_dev_addr[ETH_ALEN];
1358
1359 /*
1360 * This callback is likely called for the main interface. Update wpa_s
1361 * to use the group interface if a new interface was created for the
1362 * group.
1363 */
1364 if (wpa_s->global->p2p_group_formation)
1365 wpa_s = wpa_s->global->p2p_group_formation;
Dmitry Shmidt56052862013-10-04 10:23:25 -07001366 if (wpa_s->p2p_go_group_formation_completed) {
1367 wpa_s->global->p2p_group_formation = NULL;
1368 wpa_s->p2p_in_provisioning = 0;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001369 } else if (wpa_s->p2p_in_provisioning && !success) {
1370 wpa_msg(wpa_s, MSG_DEBUG,
1371 "P2P: Stop provisioning state due to failure");
1372 wpa_s->p2p_in_provisioning = 0;
Dmitry Shmidt56052862013-10-04 10:23:25 -07001373 }
Dmitry Shmidt21de2142014-04-08 10:50:52 -07001374 wpa_s->p2p_in_invitation = 0;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001375 wpa_s->group_formation_reported = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001376
1377 if (!success) {
Dmitry Shmidt9c175262016-03-03 10:20:07 -08001378 wpa_msg_global(wpa_s->p2pdev, MSG_INFO,
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07001379 P2P_EVENT_GROUP_FORMATION_FAILURE);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001380 wpas_notify_p2p_group_formation_failure(wpa_s, "");
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07001381 if (already_deleted)
1382 return;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001383 wpas_p2p_group_delete(wpa_s,
1384 P2P_GROUP_REMOVAL_FORMATION_FAILED);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001385 return;
1386 }
1387
Dmitry Shmidt9c175262016-03-03 10:20:07 -08001388 wpa_msg_global(wpa_s->p2pdev, MSG_INFO,
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07001389 P2P_EVENT_GROUP_FORMATION_SUCCESS);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001390
1391 ssid = wpa_s->current_ssid;
1392 if (ssid && ssid->mode == WPAS_MODE_P2P_GROUP_FORMATION) {
1393 ssid->mode = WPAS_MODE_P2P_GO;
1394 p2p_group_notif_formation_done(wpa_s->p2p_group);
1395 wpa_supplicant_ap_mac_addr_filter(wpa_s, NULL);
1396 }
1397
1398 persistent = 0;
1399 if (ssid) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001400 client = ssid->mode == WPAS_MODE_INFRA;
1401 if (ssid->mode == WPAS_MODE_P2P_GO) {
1402 persistent = ssid->p2p_persistent_group;
Dmitry Shmidt497c1d52011-07-21 15:19:46 -07001403 os_memcpy(go_dev_addr, wpa_s->global->p2p_dev_addr,
1404 ETH_ALEN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001405 } else
1406 persistent = wpas_p2p_persistent_group(wpa_s,
1407 go_dev_addr,
1408 ssid->ssid,
1409 ssid->ssid_len);
1410 } else {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001411 client = wpa_s->p2p_group_interface ==
1412 P2P_GROUP_INTERFACE_CLIENT;
1413 os_memset(go_dev_addr, 0, ETH_ALEN);
1414 }
1415
1416 wpa_s->show_group_started = 0;
1417 if (client) {
1418 /*
1419 * Indicate event only after successfully completed 4-way
1420 * handshake, i.e., when the interface is ready for data
1421 * packets.
1422 */
1423 wpa_s->show_group_started = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001424 } else {
Dmitry Shmidt7f0b69e2014-07-28 10:35:20 -07001425 wpas_p2p_group_started(wpa_s, 1, ssid,
1426 ssid ? ssid->frequency : 0,
1427 ssid && ssid->passphrase == NULL &&
1428 ssid->psk_set ? ssid->psk : NULL,
1429 ssid ? ssid->passphrase : NULL,
1430 go_dev_addr, persistent, "");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001431 wpas_p2p_cross_connect_setup(wpa_s);
1432 wpas_p2p_set_group_idle_timeout(wpa_s);
1433 }
1434
1435 if (persistent)
Dmitry Shmidtd5ab1b52016-06-21 12:38:41 -07001436 wpas_p2p_store_persistent_group(wpa_s->p2pdev,
1437 ssid, go_dev_addr);
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07001438 else {
1439 os_free(wpa_s->global->add_psk);
1440 wpa_s->global->add_psk = NULL;
1441 }
Dmitry Shmidtd5ab1b52016-06-21 12:38:41 -07001442
Dmitry Shmidt92c368d2013-08-29 12:37:21 -07001443 if (!client) {
Dmitry Shmidtabb90a32016-12-05 15:34:39 -08001444 wpas_notify_p2p_group_started(wpa_s, ssid, persistent, 0, NULL);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001445 os_get_reltime(&wpa_s->global->p2p_go_wait_client);
Dmitry Shmidt92c368d2013-08-29 12:37:21 -07001446 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001447}
1448
1449
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001450struct send_action_work {
1451 unsigned int freq;
1452 u8 dst[ETH_ALEN];
1453 u8 src[ETH_ALEN];
1454 u8 bssid[ETH_ALEN];
1455 size_t len;
1456 unsigned int wait_time;
1457 u8 buf[0];
1458};
1459
1460
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001461static void wpas_p2p_free_send_action_work(struct wpa_supplicant *wpa_s)
1462{
1463 struct send_action_work *awork = wpa_s->p2p_send_action_work->ctx;
1464
1465 wpa_printf(MSG_DEBUG,
1466 "P2P: Free Action frame radio work @%p (freq=%u dst="
1467 MACSTR " src=" MACSTR " bssid=" MACSTR " wait_time=%u)",
1468 wpa_s->p2p_send_action_work, awork->freq,
1469 MAC2STR(awork->dst), MAC2STR(awork->src),
1470 MAC2STR(awork->bssid), awork->wait_time);
1471 wpa_hexdump(MSG_DEBUG, "P2P: Freeing pending Action frame",
1472 awork->buf, awork->len);
1473 os_free(awork);
1474 wpa_s->p2p_send_action_work->ctx = NULL;
1475 radio_work_done(wpa_s->p2p_send_action_work);
1476 wpa_s->p2p_send_action_work = NULL;
1477}
1478
1479
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001480static void wpas_p2p_send_action_work_timeout(void *eloop_ctx,
1481 void *timeout_ctx)
1482{
1483 struct wpa_supplicant *wpa_s = eloop_ctx;
1484
1485 if (!wpa_s->p2p_send_action_work)
1486 return;
1487
1488 wpa_printf(MSG_DEBUG, "P2P: Send Action frame radio work timed out");
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001489 wpas_p2p_free_send_action_work(wpa_s);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001490}
1491
1492
Dmitry Shmidt03658832014-08-13 11:03:49 -07001493static void wpas_p2p_action_tx_clear(struct wpa_supplicant *wpa_s)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001494{
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001495 if (wpa_s->p2p_send_action_work) {
1496 struct send_action_work *awork;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001497
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001498 awork = wpa_s->p2p_send_action_work->ctx;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001499 wpa_printf(MSG_DEBUG,
1500 "P2P: Clear Action TX work @%p (wait_time=%u)",
1501 wpa_s->p2p_send_action_work, awork->wait_time);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001502 if (awork->wait_time == 0) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001503 wpas_p2p_free_send_action_work(wpa_s);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001504 } else {
1505 /*
1506 * In theory, this should not be needed, but number of
1507 * places in the P2P code is still using non-zero wait
1508 * time for the last Action frame in the sequence and
1509 * some of these do not call send_action_done().
1510 */
1511 eloop_cancel_timeout(wpas_p2p_send_action_work_timeout,
1512 wpa_s, NULL);
1513 eloop_register_timeout(
1514 0, awork->wait_time * 1000,
1515 wpas_p2p_send_action_work_timeout,
1516 wpa_s, NULL);
1517 }
1518 }
Dmitry Shmidt03658832014-08-13 11:03:49 -07001519}
1520
1521
1522static void wpas_p2p_send_action_tx_status(struct wpa_supplicant *wpa_s,
1523 unsigned int freq,
1524 const u8 *dst, const u8 *src,
1525 const u8 *bssid,
1526 const u8 *data, size_t data_len,
1527 enum offchannel_send_action_result
1528 result)
1529{
1530 enum p2p_send_action_result res = P2P_SEND_ACTION_SUCCESS;
1531
1532 wpas_p2p_action_tx_clear(wpa_s);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001533
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001534 if (wpa_s->global->p2p == NULL || wpa_s->global->p2p_disabled)
1535 return;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001536
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001537 switch (result) {
1538 case OFFCHANNEL_SEND_ACTION_SUCCESS:
1539 res = P2P_SEND_ACTION_SUCCESS;
1540 break;
1541 case OFFCHANNEL_SEND_ACTION_NO_ACK:
1542 res = P2P_SEND_ACTION_NO_ACK;
1543 break;
1544 case OFFCHANNEL_SEND_ACTION_FAILED:
1545 res = P2P_SEND_ACTION_FAILED;
1546 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001547 }
1548
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001549 p2p_send_action_cb(wpa_s->global->p2p, freq, dst, src, bssid, res);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001550
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08001551 if (result != OFFCHANNEL_SEND_ACTION_SUCCESS &&
1552 wpa_s->pending_pd_before_join &&
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001553 (os_memcmp(dst, wpa_s->pending_join_dev_addr, ETH_ALEN) == 0 ||
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001554 os_memcmp(dst, wpa_s->pending_join_iface_addr, ETH_ALEN) == 0) &&
1555 wpa_s->p2p_fallback_to_go_neg) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001556 wpa_s->pending_pd_before_join = 0;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001557 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: No ACK for PD Req "
1558 "during p2p_connect-auto");
Dmitry Shmidt9c175262016-03-03 10:20:07 -08001559 wpa_msg_global(wpa_s->p2pdev, MSG_INFO,
Dmitry Shmidt7f656022015-02-25 14:36:37 -08001560 P2P_EVENT_FALLBACK_TO_GO_NEG
1561 "reason=no-ACK-to-PD-Req");
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001562 wpas_p2p_fallback_to_go_neg(wpa_s, 0);
1563 return;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001564 }
1565}
1566
1567
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001568static void wpas_send_action_cb(struct wpa_radio_work *work, int deinit)
1569{
1570 struct wpa_supplicant *wpa_s = work->wpa_s;
1571 struct send_action_work *awork = work->ctx;
1572
1573 if (deinit) {
Dmitry Shmidtbd14a572014-02-18 10:33:49 -08001574 if (work->started) {
1575 eloop_cancel_timeout(wpas_p2p_send_action_work_timeout,
1576 wpa_s, NULL);
1577 wpa_s->p2p_send_action_work = NULL;
1578 offchannel_send_action_done(wpa_s);
1579 }
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001580 os_free(awork);
1581 return;
1582 }
1583
1584 if (offchannel_send_action(wpa_s, awork->freq, awork->dst, awork->src,
1585 awork->bssid, awork->buf, awork->len,
1586 awork->wait_time,
1587 wpas_p2p_send_action_tx_status, 1) < 0) {
1588 os_free(awork);
1589 radio_work_done(work);
1590 return;
1591 }
1592 wpa_s->p2p_send_action_work = work;
1593}
1594
1595
1596static int wpas_send_action_work(struct wpa_supplicant *wpa_s,
1597 unsigned int freq, const u8 *dst,
1598 const u8 *src, const u8 *bssid, const u8 *buf,
1599 size_t len, unsigned int wait_time)
1600{
1601 struct send_action_work *awork;
1602
Hai Shalom81f62d82019-07-22 12:10:00 -07001603 if (radio_work_pending(wpa_s, "p2p-send-action")) {
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001604 wpa_printf(MSG_DEBUG, "P2P: Cannot schedule new p2p-send-action work since one is already pending");
1605 return -1;
1606 }
1607
1608 awork = os_zalloc(sizeof(*awork) + len);
1609 if (awork == NULL)
1610 return -1;
1611
1612 awork->freq = freq;
1613 os_memcpy(awork->dst, dst, ETH_ALEN);
1614 os_memcpy(awork->src, src, ETH_ALEN);
1615 os_memcpy(awork->bssid, bssid, ETH_ALEN);
1616 awork->len = len;
1617 awork->wait_time = wait_time;
1618 os_memcpy(awork->buf, buf, len);
1619
Hai Shalom81f62d82019-07-22 12:10:00 -07001620 if (radio_add_work(wpa_s, freq, "p2p-send-action", 1,
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001621 wpas_send_action_cb, awork) < 0) {
1622 os_free(awork);
1623 return -1;
1624 }
1625
1626 return 0;
1627}
1628
1629
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001630static int wpas_send_action(void *ctx, unsigned int freq, const u8 *dst,
1631 const u8 *src, const u8 *bssid, const u8 *buf,
Hai Shalom021b0b52019-04-10 11:17:58 -07001632 size_t len, unsigned int wait_time, int *scheduled)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001633{
1634 struct wpa_supplicant *wpa_s = ctx;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001635 int listen_freq = -1, send_freq = -1;
1636
Hai Shalom021b0b52019-04-10 11:17:58 -07001637 if (scheduled)
1638 *scheduled = 0;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001639 if (wpa_s->p2p_listen_work)
1640 listen_freq = wpa_s->p2p_listen_work->freq;
1641 if (wpa_s->p2p_send_action_work)
1642 send_freq = wpa_s->p2p_send_action_work->freq;
1643 if (listen_freq != (int) freq && send_freq != (int) freq) {
Hai Shalom021b0b52019-04-10 11:17:58 -07001644 int res;
1645
1646 wpa_printf(MSG_DEBUG, "P2P: Schedule new radio work for Action frame TX (listen_freq=%d send_freq=%d freq=%u)",
1647 listen_freq, send_freq, freq);
1648 res = wpas_send_action_work(wpa_s, freq, dst, src, bssid, buf,
1649 len, wait_time);
1650 if (res == 0 && scheduled)
1651 *scheduled = 1;
1652 return res;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001653 }
1654
1655 wpa_printf(MSG_DEBUG, "P2P: Use ongoing radio work for Action frame TX");
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001656 return offchannel_send_action(wpa_s, freq, dst, src, bssid, buf, len,
1657 wait_time,
1658 wpas_p2p_send_action_tx_status, 1);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001659}
1660
1661
1662static void wpas_send_action_done(void *ctx)
1663{
1664 struct wpa_supplicant *wpa_s = ctx;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001665
1666 if (wpa_s->p2p_send_action_work) {
1667 eloop_cancel_timeout(wpas_p2p_send_action_work_timeout,
1668 wpa_s, NULL);
1669 os_free(wpa_s->p2p_send_action_work->ctx);
1670 radio_work_done(wpa_s->p2p_send_action_work);
1671 wpa_s->p2p_send_action_work = NULL;
1672 }
1673
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001674 offchannel_send_action_done(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001675}
1676
1677
1678static int wpas_copy_go_neg_results(struct wpa_supplicant *wpa_s,
1679 struct p2p_go_neg_results *params)
1680{
1681 if (wpa_s->go_params == NULL) {
1682 wpa_s->go_params = os_malloc(sizeof(*params));
1683 if (wpa_s->go_params == NULL)
1684 return -1;
1685 }
1686 os_memcpy(wpa_s->go_params, params, sizeof(*params));
1687 return 0;
1688}
1689
1690
1691static void wpas_start_wps_enrollee(struct wpa_supplicant *wpa_s,
1692 struct p2p_go_neg_results *res)
1693{
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001694 wpa_s->group_formation_reported = 0;
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001695 wpa_printf(MSG_DEBUG, "P2P: Start WPS Enrollee for peer " MACSTR
1696 " dev_addr " MACSTR " wps_method %d",
1697 MAC2STR(res->peer_interface_addr),
1698 MAC2STR(res->peer_device_addr), res->wps_method);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001699 wpa_hexdump_ascii(MSG_DEBUG, "P2P: Start WPS Enrollee for SSID",
1700 res->ssid, res->ssid_len);
1701 wpa_supplicant_ap_deinit(wpa_s);
1702 wpas_copy_go_neg_results(wpa_s, res);
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001703 if (res->wps_method == WPS_PBC) {
Hai Shalom021b0b52019-04-10 11:17:58 -07001704 wpas_wps_start_pbc(wpa_s, res->peer_interface_addr, 1, 0);
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001705#ifdef CONFIG_WPS_NFC
1706 } else if (res->wps_method == WPS_NFC) {
1707 wpas_wps_start_nfc(wpa_s, res->peer_device_addr,
1708 res->peer_interface_addr,
Dmitry Shmidt9c175262016-03-03 10:20:07 -08001709 wpa_s->p2pdev->p2p_oob_dev_pw,
1710 wpa_s->p2pdev->p2p_oob_dev_pw_id, 1,
1711 wpa_s->p2pdev->p2p_oob_dev_pw_id ==
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001712 DEV_PW_NFC_CONNECTION_HANDOVER ?
Dmitry Shmidt9c175262016-03-03 10:20:07 -08001713 wpa_s->p2pdev->p2p_peer_oob_pubkey_hash :
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001714 NULL,
1715 NULL, 0, 0);
1716#endif /* CONFIG_WPS_NFC */
1717 } else {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001718 u16 dev_pw_id = DEV_PW_DEFAULT;
Dmitry Shmidt216983b2015-02-06 10:50:36 -08001719 if (wpa_s->p2p_wps_method == WPS_P2PS)
1720 dev_pw_id = DEV_PW_P2PS_DEFAULT;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001721 if (wpa_s->p2p_wps_method == WPS_PIN_KEYPAD)
1722 dev_pw_id = DEV_PW_REGISTRAR_SPECIFIED;
1723 wpas_wps_start_pin(wpa_s, res->peer_interface_addr,
1724 wpa_s->p2p_pin, 1, dev_pw_id);
1725 }
1726}
1727
1728
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07001729static void wpas_p2p_add_psk_list(struct wpa_supplicant *wpa_s,
1730 struct wpa_ssid *ssid)
1731{
1732 struct wpa_ssid *persistent;
1733 struct psk_list_entry *psk;
1734 struct hostapd_data *hapd;
1735
1736 if (!wpa_s->ap_iface)
1737 return;
1738
Dmitry Shmidt9c175262016-03-03 10:20:07 -08001739 persistent = wpas_p2p_get_persistent(wpa_s->p2pdev, NULL, ssid->ssid,
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07001740 ssid->ssid_len);
1741 if (persistent == NULL)
1742 return;
1743
1744 hapd = wpa_s->ap_iface->bss[0];
1745
1746 dl_list_for_each(psk, &persistent->psk_list, struct psk_list_entry,
1747 list) {
1748 struct hostapd_wpa_psk *hpsk;
1749
1750 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Add persistent group PSK entry for "
1751 MACSTR " psk=%d",
1752 MAC2STR(psk->addr), psk->p2p);
1753 hpsk = os_zalloc(sizeof(*hpsk));
1754 if (hpsk == NULL)
1755 break;
1756 os_memcpy(hpsk->psk, psk->psk, PMK_LEN);
1757 if (psk->p2p)
1758 os_memcpy(hpsk->p2p_dev_addr, psk->addr, ETH_ALEN);
1759 else
1760 os_memcpy(hpsk->addr, psk->addr, ETH_ALEN);
1761 hpsk->next = hapd->conf->ssid.wpa_psk;
1762 hapd->conf->ssid.wpa_psk = hpsk;
1763 }
1764}
1765
1766
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001767static void p2p_go_dump_common_freqs(struct wpa_supplicant *wpa_s)
1768{
Roshan Pius3a1667e2018-07-03 15:17:14 -07001769 char buf[20 + P2P_MAX_CHANNELS * 6];
1770 char *pos, *end;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001771 unsigned int i;
Roshan Pius3a1667e2018-07-03 15:17:14 -07001772 int res;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001773
Roshan Pius3a1667e2018-07-03 15:17:14 -07001774 pos = buf;
1775 end = pos + sizeof(buf);
1776 for (i = 0; i < wpa_s->p2p_group_common_freqs_num; i++) {
1777 res = os_snprintf(pos, end - pos, " %d",
1778 wpa_s->p2p_group_common_freqs[i]);
1779 if (os_snprintf_error(end - pos, res))
1780 break;
1781 pos += res;
1782 }
1783 *pos = '\0';
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001784
Roshan Pius3a1667e2018-07-03 15:17:14 -07001785 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Common group frequencies:%s", buf);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001786}
1787
1788
1789static void p2p_go_save_group_common_freqs(struct wpa_supplicant *wpa_s,
1790 struct p2p_go_neg_results *params)
1791{
1792 unsigned int i, len = int_array_len(wpa_s->go_params->freq_list);
1793
1794 wpa_s->p2p_group_common_freqs_num = 0;
1795 os_free(wpa_s->p2p_group_common_freqs);
1796 wpa_s->p2p_group_common_freqs = os_calloc(len, sizeof(int));
1797 if (!wpa_s->p2p_group_common_freqs)
1798 return;
1799
1800 for (i = 0; i < len; i++) {
1801 if (!wpa_s->go_params->freq_list[i])
1802 break;
1803 wpa_s->p2p_group_common_freqs[i] =
1804 wpa_s->go_params->freq_list[i];
1805 }
1806 wpa_s->p2p_group_common_freqs_num = i;
1807}
1808
1809
Dmitry Shmidt216983b2015-02-06 10:50:36 -08001810static void p2p_config_write(struct wpa_supplicant *wpa_s)
1811{
1812#ifndef CONFIG_NO_CONFIG_WRITE
Dmitry Shmidt9c175262016-03-03 10:20:07 -08001813 if (wpa_s->p2pdev->conf->update_config &&
1814 wpa_config_write(wpa_s->p2pdev->confname, wpa_s->p2pdev->conf))
Dmitry Shmidt216983b2015-02-06 10:50:36 -08001815 wpa_printf(MSG_DEBUG, "P2P: Failed to update configuration");
1816#endif /* CONFIG_NO_CONFIG_WRITE */
1817}
1818
1819
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001820static void p2p_go_configured(void *ctx, void *data)
1821{
1822 struct wpa_supplicant *wpa_s = ctx;
1823 struct p2p_go_neg_results *params = data;
1824 struct wpa_ssid *ssid;
1825
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08001826 wpa_s->ap_configured_cb = NULL;
1827 wpa_s->ap_configured_cb_ctx = NULL;
1828 wpa_s->ap_configured_cb_data = NULL;
1829 if (!wpa_s->go_params) {
1830 wpa_printf(MSG_ERROR,
1831 "P2P: p2p_go_configured() called with wpa_s->go_params == NULL");
1832 return;
1833 }
1834
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001835 p2p_go_save_group_common_freqs(wpa_s, params);
1836 p2p_go_dump_common_freqs(wpa_s);
1837
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001838 ssid = wpa_s->current_ssid;
1839 if (ssid && ssid->mode == WPAS_MODE_P2P_GO) {
1840 wpa_printf(MSG_DEBUG, "P2P: Group setup without provisioning");
1841 if (wpa_s->global->p2p_group_formation == wpa_s)
1842 wpa_s->global->p2p_group_formation = NULL;
Dmitry Shmidt7f0b69e2014-07-28 10:35:20 -07001843 wpas_p2p_group_started(wpa_s, 1, ssid, ssid->frequency,
1844 params->passphrase[0] == '\0' ?
1845 params->psk : NULL,
1846 params->passphrase,
1847 wpa_s->global->p2p_dev_addr,
1848 params->persistent_group, "");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001849 wpa_s->group_formation_reported = 1;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001850
Dmitry Shmidt9c175262016-03-03 10:20:07 -08001851 if (wpa_s->p2pdev->p2ps_method_config_any) {
1852 if (is_zero_ether_addr(wpa_s->p2pdev->p2ps_join_addr)) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001853 wpa_dbg(wpa_s, MSG_DEBUG,
1854 "P2PS: Setting default PIN for ANY");
1855 wpa_supplicant_ap_wps_pin(wpa_s, NULL,
1856 "12345670", NULL, 0,
1857 0);
1858 } else {
1859 wpa_dbg(wpa_s, MSG_DEBUG,
1860 "P2PS: Setting default PIN for " MACSTR,
Dmitry Shmidt9c175262016-03-03 10:20:07 -08001861 MAC2STR(wpa_s->p2pdev->p2ps_join_addr));
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001862 wpa_supplicant_ap_wps_pin(
Dmitry Shmidt9c175262016-03-03 10:20:07 -08001863 wpa_s, wpa_s->p2pdev->p2ps_join_addr,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001864 "12345670", NULL, 0, 0);
1865 }
Dmitry Shmidt9c175262016-03-03 10:20:07 -08001866 wpa_s->p2pdev->p2ps_method_config_any = 0;
Dmitry Shmidt216983b2015-02-06 10:50:36 -08001867 }
1868
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001869 os_get_reltime(&wpa_s->global->p2p_go_wait_client);
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07001870 if (params->persistent_group) {
Dmitry Shmidtd5ab1b52016-06-21 12:38:41 -07001871 wpas_p2p_store_persistent_group(
Dmitry Shmidt9c175262016-03-03 10:20:07 -08001872 wpa_s->p2pdev, ssid,
Dmitry Shmidt497c1d52011-07-21 15:19:46 -07001873 wpa_s->global->p2p_dev_addr);
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07001874 wpas_p2p_add_psk_list(wpa_s, ssid);
1875 }
Dmitry Shmidtd5ab1b52016-06-21 12:38:41 -07001876
1877 wpas_notify_p2p_group_started(wpa_s, ssid,
Dmitry Shmidtabb90a32016-12-05 15:34:39 -08001878 params->persistent_group, 0,
1879 NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001880 wpas_p2p_cross_connect_setup(wpa_s);
1881 wpas_p2p_set_group_idle_timeout(wpa_s);
Dmitry Shmidt56052862013-10-04 10:23:25 -07001882
1883 if (wpa_s->p2p_first_connection_timeout) {
1884 wpa_dbg(wpa_s, MSG_DEBUG,
1885 "P2P: Start group formation timeout of %d seconds until first data connection on GO",
1886 wpa_s->p2p_first_connection_timeout);
1887 wpa_s->p2p_go_group_formation_completed = 0;
1888 wpa_s->global->p2p_group_formation = wpa_s;
1889 eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
Dmitry Shmidt9c175262016-03-03 10:20:07 -08001890 wpa_s->p2pdev, NULL);
Dmitry Shmidt56052862013-10-04 10:23:25 -07001891 eloop_register_timeout(
1892 wpa_s->p2p_first_connection_timeout, 0,
1893 wpas_p2p_group_formation_timeout,
Dmitry Shmidt9c175262016-03-03 10:20:07 -08001894 wpa_s->p2pdev, NULL);
Dmitry Shmidt56052862013-10-04 10:23:25 -07001895 }
1896
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001897 return;
1898 }
1899
1900 wpa_printf(MSG_DEBUG, "P2P: Setting up WPS for GO provisioning");
1901 if (wpa_supplicant_ap_mac_addr_filter(wpa_s,
1902 params->peer_interface_addr)) {
1903 wpa_printf(MSG_DEBUG, "P2P: Failed to setup MAC address "
1904 "filtering");
1905 return;
1906 }
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001907 if (params->wps_method == WPS_PBC) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001908 wpa_supplicant_ap_wps_pbc(wpa_s, params->peer_interface_addr,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001909 params->peer_device_addr);
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001910#ifdef CONFIG_WPS_NFC
1911 } else if (params->wps_method == WPS_NFC) {
Dmitry Shmidt9c175262016-03-03 10:20:07 -08001912 if (wpa_s->p2pdev->p2p_oob_dev_pw_id !=
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001913 DEV_PW_NFC_CONNECTION_HANDOVER &&
Dmitry Shmidt9c175262016-03-03 10:20:07 -08001914 !wpa_s->p2pdev->p2p_oob_dev_pw) {
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001915 wpa_printf(MSG_DEBUG, "P2P: No NFC Dev Pw known");
1916 return;
1917 }
1918 wpas_ap_wps_add_nfc_pw(
Dmitry Shmidt9c175262016-03-03 10:20:07 -08001919 wpa_s, wpa_s->p2pdev->p2p_oob_dev_pw_id,
1920 wpa_s->p2pdev->p2p_oob_dev_pw,
1921 wpa_s->p2pdev->p2p_peer_oob_pk_hash_known ?
1922 wpa_s->p2pdev->p2p_peer_oob_pubkey_hash : NULL);
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001923#endif /* CONFIG_WPS_NFC */
1924 } else if (wpa_s->p2p_pin[0])
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001925 wpa_supplicant_ap_wps_pin(wpa_s, params->peer_interface_addr,
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001926 wpa_s->p2p_pin, NULL, 0, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001927 os_free(wpa_s->go_params);
1928 wpa_s->go_params = NULL;
1929}
1930
1931
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08001932/**
1933 * wpas_p2p_freq_to_edmg_channel - Convert frequency into EDMG channel
1934 * @freq: Frequency (MHz) to convert
1935 * @op_class: Buffer for returning operating class
1936 * @op_edmg_channel: Buffer for returning channel number
1937 * Returns: 0 on success, -1 on failure
1938 *
1939 * This can be used to find the highest channel bonding which includes the
1940 * specified frequency.
1941 */
1942static int wpas_p2p_freq_to_edmg_channel(struct wpa_supplicant *wpa_s,
1943 unsigned int freq,
1944 u8 *op_class, u8 *op_edmg_channel)
1945{
1946 struct hostapd_hw_modes *hwmode;
1947 struct ieee80211_edmg_config edmg;
1948 unsigned int i;
1949 enum chan_width chanwidth[] = {
1950 CHAN_WIDTH_8640,
1951 CHAN_WIDTH_6480,
1952 CHAN_WIDTH_4320,
1953 };
1954
1955 if (!wpa_s->hw.modes)
1956 return -1;
1957
1958 hwmode = get_mode(wpa_s->hw.modes, wpa_s->hw.num_modes,
Hai Shalomfdcde762020-04-02 11:19:20 -07001959 HOSTAPD_MODE_IEEE80211AD, 0);
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08001960 if (!hwmode) {
1961 wpa_printf(MSG_ERROR,
1962 "Unsupported AP mode: HOSTAPD_MODE_IEEE80211AD");
1963 return -1;
1964 }
1965
1966 /* Find the highest EDMG channel bandwidth to start the P2P GO */
1967 for (i = 0; i < ARRAY_SIZE(chanwidth); i++) {
1968 if (ieee80211_chaninfo_to_channel(freq, chanwidth[i], 0,
1969 op_class,
1970 op_edmg_channel) < 0)
1971 continue;
1972
1973 hostapd_encode_edmg_chan(1, *op_edmg_channel, 0, &edmg);
1974 if (edmg.channels &&
1975 ieee802_edmg_is_allowed(hwmode->edmg, edmg)) {
1976 wpa_printf(MSG_DEBUG,
1977 "Freq %u to EDMG channel %u at opclass %u",
1978 freq, *op_edmg_channel, *op_class);
1979 return 0;
1980 }
1981 }
1982
1983 return -1;
1984}
1985
1986
1987int wpas_p2p_try_edmg_channel(struct wpa_supplicant *wpa_s,
1988 struct p2p_go_neg_results *params)
1989{
1990 u8 op_channel, op_class;
1991 int freq;
1992
1993 /* Try social channel as primary channel frequency */
1994 freq = (!params->freq) ? 58320 + 1 * 2160 : params->freq;
1995
1996 if (wpas_p2p_freq_to_edmg_channel(wpa_s, freq, &op_class,
1997 &op_channel) == 0) {
1998 wpa_printf(MSG_DEBUG,
1999 "Freq %d will be used to set an EDMG connection (channel=%u opclass=%u)",
2000 freq, op_channel, op_class);
2001 params->freq = freq;
2002 return 0;
2003 }
2004
2005 return -1;
2006}
2007
2008
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002009static void wpas_start_wps_go(struct wpa_supplicant *wpa_s,
2010 struct p2p_go_neg_results *params,
2011 int group_formation)
2012{
2013 struct wpa_ssid *ssid;
2014
Dmitry Shmidtaa532512012-09-24 10:35:31 -07002015 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Starting GO");
2016 if (wpas_copy_go_neg_results(wpa_s, params) < 0) {
2017 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Could not copy GO Negotiation "
2018 "results");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002019 return;
Dmitry Shmidtaa532512012-09-24 10:35:31 -07002020 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002021
2022 ssid = wpa_config_add_network(wpa_s->conf);
Dmitry Shmidtaa532512012-09-24 10:35:31 -07002023 if (ssid == NULL) {
2024 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Could not add network for GO");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002025 return;
Dmitry Shmidtaa532512012-09-24 10:35:31 -07002026 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002027
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002028 wpa_s->show_group_started = 0;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002029 wpa_s->p2p_go_group_formation_completed = 0;
2030 wpa_s->group_formation_reported = 0;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002031 os_memset(wpa_s->go_dev_addr, 0, ETH_ALEN);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002032
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002033 wpa_config_set_network_defaults(ssid);
2034 ssid->temporary = 1;
2035 ssid->p2p_group = 1;
Dmitry Shmidtaca489e2016-09-28 15:44:14 -07002036 ssid->p2p_persistent_group = !!params->persistent_group;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002037 ssid->mode = group_formation ? WPAS_MODE_P2P_GROUP_FORMATION :
2038 WPAS_MODE_P2P_GO;
2039 ssid->frequency = params->freq;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002040 ssid->ht40 = params->ht40;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07002041 ssid->vht = params->vht;
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08002042 ssid->max_oper_chwidth = params->max_oper_chwidth;
2043 ssid->vht_center_freq2 = params->vht_center_freq2;
Hai Shalom74f70d42019-02-11 14:42:39 -08002044 ssid->he = params->he;
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08002045 if (params->edmg) {
2046 u8 op_channel, op_class;
2047
2048 if (!wpas_p2p_freq_to_edmg_channel(wpa_s, params->freq,
2049 &op_class, &op_channel)) {
2050 ssid->edmg_channel = op_channel;
2051 ssid->enable_edmg = params->edmg;
2052 } else {
2053 wpa_dbg(wpa_s, MSG_DEBUG,
2054 "P2P: Could not match EDMG channel, freq %d, for GO",
2055 params->freq);
2056 }
2057 }
2058
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002059 ssid->ssid = os_zalloc(params->ssid_len + 1);
2060 if (ssid->ssid) {
2061 os_memcpy(ssid->ssid, params->ssid, params->ssid_len);
2062 ssid->ssid_len = params->ssid_len;
2063 }
2064 ssid->auth_alg = WPA_AUTH_ALG_OPEN;
2065 ssid->key_mgmt = WPA_KEY_MGMT_PSK;
2066 ssid->proto = WPA_PROTO_RSN;
2067 ssid->pairwise_cipher = WPA_CIPHER_CCMP;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002068 ssid->group_cipher = WPA_CIPHER_CCMP;
2069 if (params->freq > 56160) {
2070 /*
2071 * Enable GCMP instead of CCMP as pairwise_cipher and
2072 * group_cipher in 60 GHz.
2073 */
2074 ssid->pairwise_cipher = WPA_CIPHER_GCMP;
2075 ssid->group_cipher = WPA_CIPHER_GCMP;
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08002076 /* P2P GO in 60 GHz is always a PCP (PBSS) */
2077 ssid->pbss = 1;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002078 }
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08002079 if (os_strlen(params->passphrase) > 0) {
2080 ssid->passphrase = os_strdup(params->passphrase);
2081 if (ssid->passphrase == NULL) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07002082 wpa_msg_global(wpa_s, MSG_ERROR,
2083 "P2P: Failed to copy passphrase for GO");
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08002084 wpa_config_remove_network(wpa_s->conf, ssid->id);
2085 return;
2086 }
2087 } else
2088 ssid->passphrase = NULL;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002089 ssid->psk_set = params->psk_set;
2090 if (ssid->psk_set)
2091 os_memcpy(ssid->psk, params->psk, sizeof(ssid->psk));
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08002092 else if (ssid->passphrase)
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002093 wpa_config_update_psk(ssid);
Dmitry Shmidt9c175262016-03-03 10:20:07 -08002094 ssid->ap_max_inactivity = wpa_s->p2pdev->conf->p2p_go_max_inactivity;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002095
2096 wpa_s->ap_configured_cb = p2p_go_configured;
2097 wpa_s->ap_configured_cb_ctx = wpa_s;
2098 wpa_s->ap_configured_cb_data = wpa_s->go_params;
Dmitry Shmidt6dc03bd2014-05-16 10:40:13 -07002099 wpa_s->scan_req = NORMAL_SCAN_REQ;
Jouni Malinen75ecf522011-06-27 15:19:46 -07002100 wpa_s->connect_without_scan = ssid;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002101 wpa_s->reassociate = 1;
2102 wpa_s->disconnected = 0;
Dmitry Shmidtaa532512012-09-24 10:35:31 -07002103 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Request scan (that will be skipped) to "
2104 "start GO)");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002105 wpa_supplicant_req_scan(wpa_s, 0, 0);
2106}
2107
2108
2109static void wpas_p2p_clone_config(struct wpa_supplicant *dst,
2110 const struct wpa_supplicant *src)
2111{
2112 struct wpa_config *d;
2113 const struct wpa_config *s;
2114
2115 d = dst->conf;
2116 s = src->conf;
2117
Dmitry Shmidt849734c2016-05-27 09:59:01 -07002118#define C(n) \
2119do { \
2120 if (s->n && !d->n) \
2121 d->n = os_strdup(s->n); \
2122} while (0)
2123
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002124 C(device_name);
2125 C(manufacturer);
2126 C(model_name);
2127 C(model_number);
2128 C(serial_number);
2129 C(config_methods);
2130#undef C
2131
2132 os_memcpy(d->device_type, s->device_type, WPS_DEV_TYPE_LEN);
2133 os_memcpy(d->sec_device_type, s->sec_device_type,
2134 sizeof(d->sec_device_type));
2135 d->num_sec_device_types = s->num_sec_device_types;
2136
2137 d->p2p_group_idle = s->p2p_group_idle;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002138 d->p2p_go_freq_change_policy = s->p2p_go_freq_change_policy;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002139 d->p2p_intra_bss = s->p2p_intra_bss;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002140 d->persistent_reconnect = s->persistent_reconnect;
Dmitry Shmidt04949592012-07-19 12:16:46 -07002141 d->max_num_sta = s->max_num_sta;
2142 d->pbc_in_m1 = s->pbc_in_m1;
Dmitry Shmidt444d5672013-04-01 13:08:44 -07002143 d->ignore_old_scan_res = s->ignore_old_scan_res;
Dmitry Shmidt51b6ea82013-05-08 10:42:09 -07002144 d->beacon_int = s->beacon_int;
Dmitry Shmidt18463232014-01-24 12:29:41 -08002145 d->dtim_period = s->dtim_period;
Dmitry Shmidt7f656022015-02-25 14:36:37 -08002146 d->p2p_go_ctwindow = s->p2p_go_ctwindow;
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07002147 d->disassoc_low_ack = s->disassoc_low_ack;
Dmitry Shmidt96571392013-10-14 12:54:46 -07002148 d->disable_scan_offload = s->disable_scan_offload;
Dmitry Shmidt807291d2015-01-27 13:40:23 -08002149 d->passive_scan = s->passive_scan;
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08002150
Dmitry Shmidt849734c2016-05-27 09:59:01 -07002151 if (s->wps_nfc_dh_privkey && s->wps_nfc_dh_pubkey &&
2152 !d->wps_nfc_pw_from_config) {
2153 wpabuf_free(d->wps_nfc_dh_privkey);
2154 wpabuf_free(d->wps_nfc_dh_pubkey);
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08002155 d->wps_nfc_dh_privkey = wpabuf_dup(s->wps_nfc_dh_privkey);
2156 d->wps_nfc_dh_pubkey = wpabuf_dup(s->wps_nfc_dh_pubkey);
2157 }
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07002158 d->p2p_cli_probe = s->p2p_cli_probe;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002159 d->go_interworking = s->go_interworking;
2160 d->go_access_network_type = s->go_access_network_type;
2161 d->go_internet = s->go_internet;
2162 d->go_venue_group = s->go_venue_group;
2163 d->go_venue_type = s->go_venue_type;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002164}
2165
2166
Dmitry Shmidt34af3062013-07-11 10:46:32 -07002167static void wpas_p2p_get_group_ifname(struct wpa_supplicant *wpa_s,
2168 char *ifname, size_t len)
2169{
2170 char *ifname_ptr = wpa_s->ifname;
2171
2172 if (os_strncmp(wpa_s->ifname, P2P_MGMT_DEVICE_PREFIX,
2173 os_strlen(P2P_MGMT_DEVICE_PREFIX)) == 0) {
2174 ifname_ptr = os_strrchr(wpa_s->ifname, '-') + 1;
2175 }
2176
2177 os_snprintf(ifname, len, "p2p-%s-%d", ifname_ptr, wpa_s->p2p_group_idx);
2178 if (os_strlen(ifname) >= IFNAMSIZ &&
2179 os_strlen(wpa_s->ifname) < IFNAMSIZ) {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002180 int res;
2181
Dmitry Shmidt34af3062013-07-11 10:46:32 -07002182 /* Try to avoid going over the IFNAMSIZ length limit */
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002183 res = os_snprintf(ifname, len, "p2p-%d", wpa_s->p2p_group_idx);
2184 if (os_snprintf_error(len, res) && len)
2185 ifname[len - 1] = '\0';
Dmitry Shmidt34af3062013-07-11 10:46:32 -07002186 }
2187}
2188
2189
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002190static int wpas_p2p_add_group_interface(struct wpa_supplicant *wpa_s,
2191 enum wpa_driver_if_type type)
2192{
2193 char ifname[120], force_ifname[120];
2194
2195 if (wpa_s->pending_interface_name[0]) {
2196 wpa_printf(MSG_DEBUG, "P2P: Pending virtual interface exists "
2197 "- skip creation of a new one");
2198 if (is_zero_ether_addr(wpa_s->pending_interface_addr)) {
2199 wpa_printf(MSG_DEBUG, "P2P: Pending virtual address "
2200 "unknown?! ifname='%s'",
2201 wpa_s->pending_interface_name);
2202 return -1;
2203 }
2204 return 0;
2205 }
2206
Dmitry Shmidt34af3062013-07-11 10:46:32 -07002207 wpas_p2p_get_group_ifname(wpa_s, ifname, sizeof(ifname));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002208 force_ifname[0] = '\0';
2209
2210 wpa_printf(MSG_DEBUG, "P2P: Create a new interface %s for the group",
2211 ifname);
2212 wpa_s->p2p_group_idx++;
2213
2214 wpa_s->pending_interface_type = type;
2215 if (wpa_drv_if_add(wpa_s, type, ifname, NULL, NULL, force_ifname,
2216 wpa_s->pending_interface_addr, NULL) < 0) {
2217 wpa_printf(MSG_ERROR, "P2P: Failed to create new group "
2218 "interface");
2219 return -1;
2220 }
2221
Jimmy Chen36c21992018-11-29 16:46:43 +08002222 if (wpa_s->conf->p2p_interface_random_mac_addr) {
Hai Shalom74f70d42019-02-11 14:42:39 -08002223 random_mac_addr(wpa_s->pending_interface_addr);
2224 wpa_printf(MSG_DEBUG, "P2P: Generate random MAC address " MACSTR
2225 " for the group",
2226 MAC2STR(wpa_s->pending_interface_addr));
Jimmy Chen36c21992018-11-29 16:46:43 +08002227 }
2228
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002229 if (force_ifname[0]) {
2230 wpa_printf(MSG_DEBUG, "P2P: Driver forced interface name %s",
2231 force_ifname);
2232 os_strlcpy(wpa_s->pending_interface_name, force_ifname,
2233 sizeof(wpa_s->pending_interface_name));
2234 } else
2235 os_strlcpy(wpa_s->pending_interface_name, ifname,
2236 sizeof(wpa_s->pending_interface_name));
2237 wpa_printf(MSG_DEBUG, "P2P: Created pending virtual interface %s addr "
2238 MACSTR, wpa_s->pending_interface_name,
2239 MAC2STR(wpa_s->pending_interface_addr));
2240
2241 return 0;
2242}
2243
2244
2245static void wpas_p2p_remove_pending_group_interface(
2246 struct wpa_supplicant *wpa_s)
2247{
2248 if (!wpa_s->pending_interface_name[0] ||
2249 is_zero_ether_addr(wpa_s->pending_interface_addr))
2250 return; /* No pending virtual interface */
2251
2252 wpa_printf(MSG_DEBUG, "P2P: Removing pending group interface %s",
2253 wpa_s->pending_interface_name);
2254 wpa_drv_if_remove(wpa_s, wpa_s->pending_interface_type,
2255 wpa_s->pending_interface_name);
2256 os_memset(wpa_s->pending_interface_addr, 0, ETH_ALEN);
2257 wpa_s->pending_interface_name[0] = '\0';
Dmitry Shmidt216983b2015-02-06 10:50:36 -08002258 wpa_s->global->pending_group_iface_for_p2ps = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002259}
2260
2261
2262static struct wpa_supplicant *
2263wpas_p2p_init_group_interface(struct wpa_supplicant *wpa_s, int go)
2264{
2265 struct wpa_interface iface;
2266 struct wpa_supplicant *group_wpa_s;
2267
2268 if (!wpa_s->pending_interface_name[0]) {
2269 wpa_printf(MSG_ERROR, "P2P: No pending group interface");
2270 if (!wpas_p2p_create_iface(wpa_s))
2271 return NULL;
2272 /*
2273 * Something has forced us to remove the pending interface; try
2274 * to create a new one and hope for the best that we will get
2275 * the same local address.
2276 */
2277 if (wpas_p2p_add_group_interface(wpa_s, go ? WPA_IF_P2P_GO :
2278 WPA_IF_P2P_CLIENT) < 0)
2279 return NULL;
2280 }
2281
2282 os_memset(&iface, 0, sizeof(iface));
2283 iface.ifname = wpa_s->pending_interface_name;
2284 iface.driver = wpa_s->driver->name;
Dmitry Shmidt34af3062013-07-11 10:46:32 -07002285 if (wpa_s->conf->ctrl_interface == NULL &&
2286 wpa_s->parent != wpa_s &&
2287 wpa_s->p2p_mgmt &&
2288 (wpa_s->drv_flags & WPA_DRIVER_FLAGS_DEDICATED_P2P_DEVICE))
2289 iface.ctrl_interface = wpa_s->parent->conf->ctrl_interface;
2290 else
2291 iface.ctrl_interface = wpa_s->conf->ctrl_interface;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002292 iface.driver_param = wpa_s->conf->driver_param;
Dmitry Shmidt203eadb2015-03-05 14:16:04 -08002293 group_wpa_s = wpa_supplicant_add_iface(wpa_s->global, &iface, wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002294 if (group_wpa_s == NULL) {
2295 wpa_printf(MSG_ERROR, "P2P: Failed to create new "
2296 "wpa_supplicant interface");
2297 return NULL;
2298 }
2299 wpa_s->pending_interface_name[0] = '\0';
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002300 group_wpa_s->p2p_group_interface = go ? P2P_GROUP_INTERFACE_GO :
2301 P2P_GROUP_INTERFACE_CLIENT;
2302 wpa_s->global->p2p_group_formation = group_wpa_s;
Dmitry Shmidt216983b2015-02-06 10:50:36 -08002303 wpa_s->global->pending_group_iface_for_p2ps = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002304
2305 wpas_p2p_clone_config(group_wpa_s, wpa_s);
2306
Jimmy Chen36c21992018-11-29 16:46:43 +08002307 if (wpa_s->conf->p2p_interface_random_mac_addr) {
Hai Shalom74f70d42019-02-11 14:42:39 -08002308 if (wpa_drv_set_mac_addr(group_wpa_s,
2309 wpa_s->pending_interface_addr) < 0) {
Jimmy Chen36c21992018-11-29 16:46:43 +08002310 wpa_msg(group_wpa_s, MSG_INFO,
2311 "Failed to set random MAC address");
Hai Shalom74f70d42019-02-11 14:42:39 -08002312 wpa_supplicant_remove_iface(wpa_s->global, group_wpa_s,
2313 0);
Jimmy Chen36c21992018-11-29 16:46:43 +08002314 return NULL;
2315 }
2316
2317 if (wpa_supplicant_update_mac_addr(group_wpa_s) < 0) {
2318 wpa_msg(group_wpa_s, MSG_INFO,
2319 "Could not update MAC address information");
Hai Shalom74f70d42019-02-11 14:42:39 -08002320 wpa_supplicant_remove_iface(wpa_s->global, group_wpa_s,
2321 0);
Jimmy Chen36c21992018-11-29 16:46:43 +08002322 return NULL;
2323 }
2324
Hai Shalom74f70d42019-02-11 14:42:39 -08002325 wpa_printf(MSG_DEBUG, "P2P: Using random MAC address " MACSTR
2326 " for the group",
2327 MAC2STR(wpa_s->pending_interface_addr));
Jimmy Chen36c21992018-11-29 16:46:43 +08002328 }
2329
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002330 return group_wpa_s;
2331}
2332
2333
2334static void wpas_p2p_group_formation_timeout(void *eloop_ctx,
2335 void *timeout_ctx)
2336{
2337 struct wpa_supplicant *wpa_s = eloop_ctx;
2338 wpa_printf(MSG_DEBUG, "P2P: Group Formation timed out");
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07002339 wpas_p2p_group_formation_failed(wpa_s, 0);
Dmitry Shmidt34af3062013-07-11 10:46:32 -07002340}
2341
2342
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07002343static void wpas_p2p_group_formation_failed(struct wpa_supplicant *wpa_s,
2344 int already_deleted)
Dmitry Shmidt34af3062013-07-11 10:46:32 -07002345{
2346 eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
Dmitry Shmidt9c175262016-03-03 10:20:07 -08002347 wpa_s->p2pdev, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002348 if (wpa_s->global->p2p)
2349 p2p_group_formation_failed(wpa_s->global->p2p);
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07002350 wpas_group_formation_completed(wpa_s, 0, already_deleted);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002351}
2352
2353
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002354static void wpas_p2p_grpform_fail_after_wps(struct wpa_supplicant *wpa_s)
2355{
2356 wpa_printf(MSG_DEBUG, "P2P: Reject group formation due to WPS provisioning failure");
2357 eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
Dmitry Shmidt9c175262016-03-03 10:20:07 -08002358 wpa_s->p2pdev, NULL);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002359 eloop_register_timeout(0, 0, wpas_p2p_group_formation_timeout,
Dmitry Shmidt9c175262016-03-03 10:20:07 -08002360 wpa_s->p2pdev, NULL);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002361 wpa_s->global->p2p_fail_on_wps_complete = 0;
2362}
2363
2364
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07002365void wpas_p2p_ap_setup_failed(struct wpa_supplicant *wpa_s)
2366{
2367 if (wpa_s->global->p2p_group_formation != wpa_s)
2368 return;
2369 /* Speed up group formation timeout since this cannot succeed */
2370 eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
Dmitry Shmidt9c175262016-03-03 10:20:07 -08002371 wpa_s->p2pdev, NULL);
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07002372 eloop_register_timeout(0, 0, wpas_p2p_group_formation_timeout,
Dmitry Shmidt9c175262016-03-03 10:20:07 -08002373 wpa_s->p2pdev, NULL);
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07002374}
2375
2376
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002377static void wpas_go_neg_completed(void *ctx, struct p2p_go_neg_results *res)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002378{
2379 struct wpa_supplicant *wpa_s = ctx;
Dmitry Shmidt9c175262016-03-03 10:20:07 -08002380 struct wpa_supplicant *group_wpa_s;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002381
2382 if (wpa_s->off_channel_freq || wpa_s->roc_waiting_drv_freq) {
2383 wpa_drv_cancel_remain_on_channel(wpa_s);
2384 wpa_s->off_channel_freq = 0;
2385 wpa_s->roc_waiting_drv_freq = 0;
2386 }
2387
2388 if (res->status) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07002389 wpa_msg_global(wpa_s, MSG_INFO,
2390 P2P_EVENT_GO_NEG_FAILURE "status=%d",
2391 res->status);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002392 wpas_notify_p2p_go_neg_completed(wpa_s, res);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002393 wpas_p2p_remove_pending_group_interface(wpa_s);
2394 return;
2395 }
2396
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002397 if (!res->role_go) {
2398 /* Inform driver of the operating channel of GO. */
2399 wpa_drv_set_prob_oper_freq(wpa_s, res->freq);
2400 }
2401
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002402 if (wpa_s->p2p_go_ht40)
2403 res->ht40 = 1;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07002404 if (wpa_s->p2p_go_vht)
2405 res->vht = 1;
Hai Shalomc3565922019-10-28 11:58:20 -07002406 if (wpa_s->p2p_go_he)
2407 res->he = 1;
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08002408 if (wpa_s->p2p_go_edmg)
2409 res->edmg = 1;
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08002410 res->max_oper_chwidth = wpa_s->p2p_go_max_oper_chwidth;
2411 res->vht_center_freq2 = wpa_s->p2p_go_vht_center_freq2;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002412
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07002413 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_GO_NEG_SUCCESS "role=%s "
2414 "freq=%d ht40=%d peer_dev=" MACSTR " peer_iface=" MACSTR
2415 " wps_method=%s",
2416 res->role_go ? "GO" : "client", res->freq, res->ht40,
2417 MAC2STR(res->peer_device_addr),
2418 MAC2STR(res->peer_interface_addr),
2419 p2p_wps_method_text(res->wps_method));
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002420 wpas_notify_p2p_go_neg_completed(wpa_s, res);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002421
Dmitry Shmidt04949592012-07-19 12:16:46 -07002422 if (res->role_go && wpa_s->p2p_persistent_id >= 0) {
2423 struct wpa_ssid *ssid;
2424 ssid = wpa_config_get_network(wpa_s->conf,
2425 wpa_s->p2p_persistent_id);
2426 if (ssid && ssid->disabled == 2 &&
2427 ssid->mode == WPAS_MODE_P2P_GO && ssid->passphrase) {
2428 size_t len = os_strlen(ssid->passphrase);
2429 wpa_printf(MSG_DEBUG, "P2P: Override passphrase based "
2430 "on requested persistent group");
2431 os_memcpy(res->passphrase, ssid->passphrase, len);
2432 res->passphrase[len] = '\0';
2433 }
2434 }
2435
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002436 if (wpa_s->create_p2p_iface) {
Dmitry Shmidt9c175262016-03-03 10:20:07 -08002437 group_wpa_s =
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002438 wpas_p2p_init_group_interface(wpa_s, res->role_go);
2439 if (group_wpa_s == NULL) {
2440 wpas_p2p_remove_pending_group_interface(wpa_s);
Dmitry Shmidtd11f0192014-03-24 12:09:47 -07002441 eloop_cancel_timeout(wpas_p2p_long_listen_timeout,
2442 wpa_s, NULL);
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07002443 wpas_p2p_group_formation_failed(wpa_s, 1);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002444 return;
2445 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002446 os_memset(wpa_s->pending_interface_addr, 0, ETH_ALEN);
2447 wpa_s->pending_interface_name[0] = '\0';
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002448 } else {
Dmitry Shmidt9c175262016-03-03 10:20:07 -08002449 group_wpa_s = wpa_s->parent;
2450 wpa_s->global->p2p_group_formation = group_wpa_s;
2451 if (group_wpa_s != wpa_s)
Dmitry Shmidt849734c2016-05-27 09:59:01 -07002452 wpas_p2p_clone_config(group_wpa_s, wpa_s);
Dmitry Shmidt9c175262016-03-03 10:20:07 -08002453 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002454
Dmitry Shmidt9c175262016-03-03 10:20:07 -08002455 group_wpa_s->p2p_in_provisioning = 1;
2456 group_wpa_s->p2pdev = wpa_s;
2457 if (group_wpa_s != wpa_s) {
2458 os_memcpy(group_wpa_s->p2p_pin, wpa_s->p2p_pin,
2459 sizeof(group_wpa_s->p2p_pin));
2460 group_wpa_s->p2p_wps_method = wpa_s->p2p_wps_method;
2461 }
2462 if (res->role_go) {
2463 wpas_start_wps_go(group_wpa_s, res, 1);
2464 } else {
2465 os_get_reltime(&group_wpa_s->scan_min_time);
2466 wpas_start_wps_enrollee(group_wpa_s, res);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002467 }
2468
Hai Shalomfdcde762020-04-02 11:19:20 -07002469 wpa_s->global->p2p_long_listen = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002470 eloop_cancel_timeout(wpas_p2p_long_listen_timeout, wpa_s, NULL);
2471
2472 eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s, NULL);
2473 eloop_register_timeout(15 + res->peer_config_timeout / 100,
2474 (res->peer_config_timeout % 100) * 10000,
2475 wpas_p2p_group_formation_timeout, wpa_s, NULL);
2476}
2477
2478
Dmitry Shmidt7a53dbb2015-06-11 13:13:53 -07002479static void wpas_go_neg_req_rx(void *ctx, const u8 *src, u16 dev_passwd_id,
2480 u8 go_intent)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002481{
2482 struct wpa_supplicant *wpa_s = ctx;
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07002483 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_GO_NEG_REQUEST MACSTR
Dmitry Shmidt7a53dbb2015-06-11 13:13:53 -07002484 " dev_passwd_id=%u go_intent=%u", MAC2STR(src),
2485 dev_passwd_id, go_intent);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002486
Dmitry Shmidt7a53dbb2015-06-11 13:13:53 -07002487 wpas_notify_p2p_go_neg_req(wpa_s, src, dev_passwd_id, go_intent);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002488}
2489
2490
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002491static void wpas_dev_found(void *ctx, const u8 *addr,
2492 const struct p2p_peer_info *info,
2493 int new_device)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002494{
Roshan Piusfd2fd662017-01-23 13:41:57 -08002495 u8 *wfd_dev_info = NULL;
2496 u8 wfd_dev_info_len = 0;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002497#ifndef CONFIG_NO_STDOUT_DEBUG
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002498 struct wpa_supplicant *wpa_s = ctx;
2499 char devtype[WPS_DEV_TYPE_BUFSIZE];
Dmitry Shmidt04f534e2013-12-09 15:50:16 -08002500 char *wfd_dev_info_hex = NULL;
2501
Irfan Sheriff8367dc92012-09-09 17:08:19 -07002502#ifdef CONFIG_WIFI_DISPLAY
Dmitry Shmidt04f534e2013-12-09 15:50:16 -08002503 wfd_dev_info_hex = wifi_display_subelem_hex(info->wfd_subelems,
2504 WFD_SUBELEM_DEVICE_INFO);
Roshan Piusfd2fd662017-01-23 13:41:57 -08002505 if (wfd_dev_info_hex) {
2506 wfd_dev_info_len = strlen(wfd_dev_info_hex) / 2;
2507 wfd_dev_info = os_zalloc(wfd_dev_info_len);
2508 // Only used for notification, so not handling error.
2509 hexstr2bin(wfd_dev_info_hex, wfd_dev_info, wfd_dev_info_len);
2510 }
Irfan Sheriff8367dc92012-09-09 17:08:19 -07002511#endif /* CONFIG_WIFI_DISPLAY */
Dmitry Shmidt04f534e2013-12-09 15:50:16 -08002512
Dmitry Shmidt216983b2015-02-06 10:50:36 -08002513 if (info->p2ps_instance) {
2514 char str[256];
2515 const u8 *buf = wpabuf_head(info->p2ps_instance);
2516 size_t len = wpabuf_len(info->p2ps_instance);
2517
2518 while (len) {
2519 u32 id;
2520 u16 methods;
2521 u8 str_len;
2522
2523 if (len < 4 + 2 + 1)
2524 break;
2525 id = WPA_GET_LE32(buf);
2526 buf += sizeof(u32);
2527 methods = WPA_GET_BE16(buf);
2528 buf += sizeof(u16);
2529 str_len = *buf++;
2530 if (str_len > len - 4 - 2 - 1)
2531 break;
2532 os_memcpy(str, buf, str_len);
2533 str[str_len] = '\0';
2534 buf += str_len;
2535 len -= str_len + sizeof(u32) + sizeof(u16) + sizeof(u8);
2536
2537 wpa_msg_global(wpa_s, MSG_INFO,
2538 P2P_EVENT_DEVICE_FOUND MACSTR
2539 " p2p_dev_addr=" MACSTR
2540 " pri_dev_type=%s name='%s'"
2541 " config_methods=0x%x"
2542 " dev_capab=0x%x"
2543 " group_capab=0x%x"
2544 " adv_id=%x asp_svc=%s%s",
2545 MAC2STR(addr),
2546 MAC2STR(info->p2p_device_addr),
2547 wps_dev_type_bin2str(
2548 info->pri_dev_type,
2549 devtype, sizeof(devtype)),
2550 info->device_name, methods,
2551 info->dev_capab, info->group_capab,
2552 id, str,
2553 info->vendor_elems ?
2554 " vendor_elems=1" : "");
2555 }
2556 goto done;
2557 }
2558
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07002559 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_DEVICE_FOUND MACSTR
2560 " p2p_dev_addr=" MACSTR
2561 " pri_dev_type=%s name='%s' config_methods=0x%x "
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002562 "dev_capab=0x%x group_capab=0x%x%s%s%s new=%d",
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07002563 MAC2STR(addr), MAC2STR(info->p2p_device_addr),
2564 wps_dev_type_bin2str(info->pri_dev_type, devtype,
2565 sizeof(devtype)),
2566 info->device_name, info->config_methods,
2567 info->dev_capab, info->group_capab,
Dmitry Shmidt04f534e2013-12-09 15:50:16 -08002568 wfd_dev_info_hex ? " wfd_dev_info=0x" : "",
Dmitry Shmidt2e67f062014-07-16 09:55:28 -07002569 wfd_dev_info_hex ? wfd_dev_info_hex : "",
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002570 info->vendor_elems ? " vendor_elems=1" : "",
2571 new_device);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002572
Dmitry Shmidt216983b2015-02-06 10:50:36 -08002573done:
Dmitry Shmidt04f534e2013-12-09 15:50:16 -08002574 os_free(wfd_dev_info_hex);
Dmitry Shmidt97672262014-02-03 13:02:54 -08002575#endif /* CONFIG_NO_STDOUT_DEBUG */
Dmitry Shmidt04f534e2013-12-09 15:50:16 -08002576
Roshan Piusfd2fd662017-01-23 13:41:57 -08002577 wpas_notify_p2p_device_found(ctx, addr, info, wfd_dev_info,
2578 wfd_dev_info_len, new_device);
2579 os_free(wfd_dev_info);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002580}
2581
2582
2583static void wpas_dev_lost(void *ctx, const u8 *dev_addr)
2584{
2585 struct wpa_supplicant *wpa_s = ctx;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002586
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07002587 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_DEVICE_LOST
2588 "p2p_dev_addr=" MACSTR, MAC2STR(dev_addr));
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002589
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002590 wpas_notify_p2p_device_lost(wpa_s, dev_addr);
2591}
2592
2593
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07002594static void wpas_find_stopped(void *ctx)
2595{
2596 struct wpa_supplicant *wpa_s = ctx;
Dmitry Shmidt9c175262016-03-03 10:20:07 -08002597
2598 if (wpa_s->p2p_scan_work && wpas_abort_ongoing_scan(wpa_s) < 0)
2599 wpa_printf(MSG_DEBUG, "P2P: Abort ongoing scan failed");
2600
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07002601 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_FIND_STOPPED);
Dmitry Shmidt8bd70b72015-05-26 16:02:19 -07002602 wpas_notify_p2p_find_stopped(wpa_s);
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07002603}
2604
2605
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002606struct wpas_p2p_listen_work {
2607 unsigned int freq;
2608 unsigned int duration;
2609 struct wpabuf *probe_resp_ie;
2610};
2611
2612
2613static void wpas_p2p_listen_work_free(struct wpas_p2p_listen_work *lwork)
2614{
2615 if (lwork == NULL)
2616 return;
2617 wpabuf_free(lwork->probe_resp_ie);
2618 os_free(lwork);
2619}
2620
2621
2622static void wpas_p2p_listen_work_done(struct wpa_supplicant *wpa_s)
2623{
2624 struct wpas_p2p_listen_work *lwork;
2625
2626 if (!wpa_s->p2p_listen_work)
2627 return;
2628
2629 lwork = wpa_s->p2p_listen_work->ctx;
2630 wpas_p2p_listen_work_free(lwork);
2631 radio_work_done(wpa_s->p2p_listen_work);
2632 wpa_s->p2p_listen_work = NULL;
2633}
2634
2635
2636static void wpas_start_listen_cb(struct wpa_radio_work *work, int deinit)
2637{
2638 struct wpa_supplicant *wpa_s = work->wpa_s;
2639 struct wpas_p2p_listen_work *lwork = work->ctx;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002640 unsigned int duration;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002641
2642 if (deinit) {
Dmitry Shmidtbd14a572014-02-18 10:33:49 -08002643 if (work->started) {
2644 wpa_s->p2p_listen_work = NULL;
2645 wpas_stop_listen(wpa_s);
2646 }
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002647 wpas_p2p_listen_work_free(lwork);
2648 return;
2649 }
2650
2651 wpa_s->p2p_listen_work = work;
2652
2653 wpa_drv_set_ap_wps_ie(wpa_s, NULL, lwork->probe_resp_ie, NULL);
2654
2655 if (wpa_drv_probe_req_report(wpa_s, 1) < 0) {
2656 wpa_printf(MSG_DEBUG, "P2P: Failed to request the driver to "
2657 "report received Probe Request frames");
2658 wpas_p2p_listen_work_done(wpa_s);
2659 return;
2660 }
2661
2662 wpa_s->pending_listen_freq = lwork->freq;
2663 wpa_s->pending_listen_duration = lwork->duration;
2664
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002665 duration = lwork->duration;
2666#ifdef CONFIG_TESTING_OPTIONS
2667 if (wpa_s->extra_roc_dur) {
2668 wpa_printf(MSG_DEBUG, "TESTING: Increase ROC duration %u -> %u",
2669 duration, duration + wpa_s->extra_roc_dur);
2670 duration += wpa_s->extra_roc_dur;
2671 }
2672#endif /* CONFIG_TESTING_OPTIONS */
2673
2674 if (wpa_drv_remain_on_channel(wpa_s, lwork->freq, duration) < 0) {
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002675 wpa_printf(MSG_DEBUG, "P2P: Failed to request the driver "
2676 "to remain on channel (%u MHz) for Listen "
2677 "state", lwork->freq);
2678 wpas_p2p_listen_work_done(wpa_s);
2679 wpa_s->pending_listen_freq = 0;
2680 return;
2681 }
2682 wpa_s->off_channel_freq = 0;
2683 wpa_s->roc_waiting_drv_freq = lwork->freq;
2684}
2685
2686
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002687static int wpas_start_listen(void *ctx, unsigned int freq,
2688 unsigned int duration,
2689 const struct wpabuf *probe_resp_ie)
2690{
2691 struct wpa_supplicant *wpa_s = ctx;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002692 struct wpas_p2p_listen_work *lwork;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002693
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002694 if (wpa_s->p2p_listen_work) {
2695 wpa_printf(MSG_DEBUG, "P2P: Reject start_listen since p2p_listen_work already exists");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002696 return -1;
2697 }
2698
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002699 lwork = os_zalloc(sizeof(*lwork));
2700 if (lwork == NULL)
2701 return -1;
2702 lwork->freq = freq;
2703 lwork->duration = duration;
2704 if (probe_resp_ie) {
2705 lwork->probe_resp_ie = wpabuf_dup(probe_resp_ie);
2706 if (lwork->probe_resp_ie == NULL) {
2707 wpas_p2p_listen_work_free(lwork);
2708 return -1;
2709 }
2710 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002711
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002712 if (radio_add_work(wpa_s, freq, "p2p-listen", 0, wpas_start_listen_cb,
2713 lwork) < 0) {
2714 wpas_p2p_listen_work_free(lwork);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002715 return -1;
2716 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002717
2718 return 0;
2719}
2720
2721
2722static void wpas_stop_listen(void *ctx)
2723{
2724 struct wpa_supplicant *wpa_s = ctx;
2725 if (wpa_s->off_channel_freq || wpa_s->roc_waiting_drv_freq) {
2726 wpa_drv_cancel_remain_on_channel(wpa_s);
2727 wpa_s->off_channel_freq = 0;
2728 wpa_s->roc_waiting_drv_freq = 0;
2729 }
2730 wpa_drv_set_ap_wps_ie(wpa_s, NULL, NULL, NULL);
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07002731
2732 /*
2733 * Don't cancel Probe Request RX reporting for a connected P2P Client
2734 * handling Probe Request frames.
2735 */
2736 if (!wpa_s->p2p_cli_probe)
2737 wpa_drv_probe_req_report(wpa_s, 0);
2738
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002739 wpas_p2p_listen_work_done(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002740}
2741
2742
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07002743static int wpas_send_probe_resp(void *ctx, const struct wpabuf *buf,
2744 unsigned int freq)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002745{
2746 struct wpa_supplicant *wpa_s = ctx;
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07002747 return wpa_drv_send_mlme(wpa_s, wpabuf_head(buf), wpabuf_len(buf), 1,
Hai Shalomfdcde762020-04-02 11:19:20 -07002748 freq, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002749}
2750
2751
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002752static void wpas_prov_disc_local_display(struct wpa_supplicant *wpa_s,
2753 const u8 *peer, const char *params,
2754 unsigned int generated_pin)
2755{
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07002756 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_SHOW_PIN MACSTR
2757 " %08d%s", MAC2STR(peer), generated_pin, params);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002758}
2759
2760
2761static void wpas_prov_disc_local_keypad(struct wpa_supplicant *wpa_s,
2762 const u8 *peer, const char *params)
2763{
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07002764 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_ENTER_PIN MACSTR
2765 "%s", MAC2STR(peer), params);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002766}
2767
2768
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002769static void wpas_prov_disc_req(void *ctx, const u8 *peer, u16 config_methods,
2770 const u8 *dev_addr, const u8 *pri_dev_type,
2771 const char *dev_name, u16 supp_config_methods,
2772 u8 dev_capab, u8 group_capab, const u8 *group_id,
2773 size_t group_id_len)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002774{
2775 struct wpa_supplicant *wpa_s = ctx;
2776 char devtype[WPS_DEV_TYPE_BUFSIZE];
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002777 char params[300];
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002778 u8 empty_dev_type[8];
2779 unsigned int generated_pin = 0;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002780 struct wpa_supplicant *group = NULL;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002781 int res;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002782
2783 if (group_id) {
2784 for (group = wpa_s->global->ifaces; group; group = group->next)
2785 {
2786 struct wpa_ssid *s = group->current_ssid;
2787 if (s != NULL &&
2788 s->mode == WPAS_MODE_P2P_GO &&
2789 group_id_len - ETH_ALEN == s->ssid_len &&
2790 os_memcmp(group_id + ETH_ALEN, s->ssid,
2791 s->ssid_len) == 0)
2792 break;
2793 }
2794 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002795
2796 if (pri_dev_type == NULL) {
2797 os_memset(empty_dev_type, 0, sizeof(empty_dev_type));
2798 pri_dev_type = empty_dev_type;
2799 }
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002800 res = os_snprintf(params, sizeof(params), " p2p_dev_addr=" MACSTR
2801 " pri_dev_type=%s name='%s' config_methods=0x%x "
2802 "dev_capab=0x%x group_capab=0x%x%s%s",
2803 MAC2STR(dev_addr),
2804 wps_dev_type_bin2str(pri_dev_type, devtype,
2805 sizeof(devtype)),
2806 dev_name, supp_config_methods, dev_capab, group_capab,
2807 group ? " group=" : "",
2808 group ? group->ifname : "");
2809 if (os_snprintf_error(sizeof(params), res))
2810 wpa_printf(MSG_DEBUG, "P2P: PD Request event truncated");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002811 params[sizeof(params) - 1] = '\0';
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08002812
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002813 if (config_methods & WPS_CONFIG_DISPLAY) {
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08002814 if (wps_generate_pin(&generated_pin) < 0) {
2815 wpa_printf(MSG_DEBUG, "P2P: Could not generate PIN");
2816 wpas_notify_p2p_provision_discovery(
2817 wpa_s, peer, 0 /* response */,
2818 P2P_PROV_DISC_INFO_UNAVAILABLE, 0, 0);
2819 return;
2820 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002821 wpas_prov_disc_local_display(wpa_s, peer, params,
2822 generated_pin);
2823 } else if (config_methods & WPS_CONFIG_KEYPAD)
2824 wpas_prov_disc_local_keypad(wpa_s, peer, params);
2825 else if (config_methods & WPS_CONFIG_PUSHBUTTON)
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07002826 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_PBC_REQ
2827 MACSTR "%s", MAC2STR(peer), params);
Jouni Malinen75ecf522011-06-27 15:19:46 -07002828
2829 wpas_notify_p2p_provision_discovery(wpa_s, peer, 1 /* request */,
2830 P2P_PROV_DISC_SUCCESS,
2831 config_methods, generated_pin);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002832}
2833
2834
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002835static void wpas_prov_disc_resp(void *ctx, const u8 *peer, u16 config_methods)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002836{
2837 struct wpa_supplicant *wpa_s = ctx;
2838 unsigned int generated_pin = 0;
Dmitry Shmidt04949592012-07-19 12:16:46 -07002839 char params[20];
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002840
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08002841 if (wpa_s->pending_pd_before_join &&
2842 (os_memcmp(peer, wpa_s->pending_join_dev_addr, ETH_ALEN) == 0 ||
2843 os_memcmp(peer, wpa_s->pending_join_iface_addr, ETH_ALEN) == 0)) {
2844 wpa_s->pending_pd_before_join = 0;
2845 wpa_printf(MSG_DEBUG, "P2P: Starting pending "
2846 "join-existing-group operation");
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08002847 wpas_p2p_join_start(wpa_s, 0, NULL, 0);
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08002848 return;
2849 }
2850
Dmitry Shmidt04949592012-07-19 12:16:46 -07002851 if (wpa_s->pending_pd_use == AUTO_PD_JOIN ||
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002852 wpa_s->pending_pd_use == AUTO_PD_GO_NEG) {
2853 int res;
2854
2855 res = os_snprintf(params, sizeof(params), " peer_go=%d",
2856 wpa_s->pending_pd_use == AUTO_PD_JOIN);
2857 if (os_snprintf_error(sizeof(params), res))
2858 params[sizeof(params) - 1] = '\0';
2859 } else
Dmitry Shmidt04949592012-07-19 12:16:46 -07002860 params[0] = '\0';
2861
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002862 if (config_methods & WPS_CONFIG_DISPLAY)
Dmitry Shmidt04949592012-07-19 12:16:46 -07002863 wpas_prov_disc_local_keypad(wpa_s, peer, params);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002864 else if (config_methods & WPS_CONFIG_KEYPAD) {
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08002865 if (wps_generate_pin(&generated_pin) < 0) {
2866 wpa_printf(MSG_DEBUG, "P2P: Could not generate PIN");
2867 wpas_notify_p2p_provision_discovery(
2868 wpa_s, peer, 0 /* response */,
2869 P2P_PROV_DISC_INFO_UNAVAILABLE, 0, 0);
2870 return;
2871 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07002872 wpas_prov_disc_local_display(wpa_s, peer, params,
2873 generated_pin);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002874 } else if (config_methods & WPS_CONFIG_PUSHBUTTON)
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07002875 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_PBC_RESP
2876 MACSTR "%s", MAC2STR(peer), params);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002877
Jouni Malinen75ecf522011-06-27 15:19:46 -07002878 wpas_notify_p2p_provision_discovery(wpa_s, peer, 0 /* response */,
2879 P2P_PROV_DISC_SUCCESS,
2880 config_methods, generated_pin);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002881}
2882
2883
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002884static void wpas_prov_disc_fail(void *ctx, const u8 *peer,
Dmitry Shmidt216983b2015-02-06 10:50:36 -08002885 enum p2p_prov_disc_status status,
2886 u32 adv_id, const u8 *adv_mac,
2887 const char *deferred_session_resp)
Jouni Malinen75ecf522011-06-27 15:19:46 -07002888{
2889 struct wpa_supplicant *wpa_s = ctx;
2890
Dmitry Shmidt04949592012-07-19 12:16:46 -07002891 if (wpa_s->p2p_fallback_to_go_neg) {
2892 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: PD for p2p_connect-auto "
2893 "failed - fall back to GO Negotiation");
Dmitry Shmidt9c175262016-03-03 10:20:07 -08002894 wpa_msg_global(wpa_s->p2pdev, MSG_INFO,
Dmitry Shmidt7f656022015-02-25 14:36:37 -08002895 P2P_EVENT_FALLBACK_TO_GO_NEG
2896 "reason=PD-failed");
Dmitry Shmidt04949592012-07-19 12:16:46 -07002897 wpas_p2p_fallback_to_go_neg(wpa_s, 0);
2898 return;
2899 }
2900
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002901 if (status == P2P_PROV_DISC_TIMEOUT_JOIN) {
Dmitry Shmidt2b380482012-09-13 10:52:13 -07002902 wpa_s->pending_pd_before_join = 0;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002903 wpa_printf(MSG_DEBUG, "P2P: Starting pending "
2904 "join-existing-group operation (no ACK for PD "
2905 "Req attempts)");
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08002906 wpas_p2p_join_start(wpa_s, 0, NULL, 0);
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002907 return;
Dmitry Shmidt2b380482012-09-13 10:52:13 -07002908 }
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002909
Dmitry Shmidt216983b2015-02-06 10:50:36 -08002910 if (adv_id && adv_mac && deferred_session_resp) {
2911 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_FAILURE
2912 " p2p_dev_addr=" MACSTR " status=%d adv_id=%x"
2913 " deferred_session_resp='%s'",
2914 MAC2STR(peer), status, adv_id,
2915 deferred_session_resp);
2916 } else if (adv_id && adv_mac) {
2917 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_FAILURE
2918 " p2p_dev_addr=" MACSTR " status=%d adv_id=%x",
2919 MAC2STR(peer), status, adv_id);
2920 } else {
2921 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_FAILURE
2922 " p2p_dev_addr=" MACSTR " status=%d",
2923 MAC2STR(peer), status);
2924 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07002925
Jouni Malinen75ecf522011-06-27 15:19:46 -07002926 wpas_notify_p2p_provision_discovery(wpa_s, peer, 0 /* response */,
2927 status, 0, 0);
2928}
2929
2930
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002931static int freq_included(struct wpa_supplicant *wpa_s,
2932 const struct p2p_channels *channels,
2933 unsigned int freq)
Dmitry Shmidtf7e0a992013-05-23 11:03:10 -07002934{
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002935 if ((channels == NULL || p2p_channels_includes_freq(channels, freq)) &&
2936 wpas_p2p_go_is_peer_freq(wpa_s, freq))
2937 return 1;
2938 return 0;
2939}
Dmitry Shmidtf7e0a992013-05-23 11:03:10 -07002940
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002941
2942static void wpas_p2p_go_update_common_freqs(struct wpa_supplicant *wpa_s)
2943{
2944 unsigned int num = P2P_MAX_CHANNELS;
2945 int *common_freqs;
2946 int ret;
2947
2948 p2p_go_dump_common_freqs(wpa_s);
2949 common_freqs = os_calloc(num, sizeof(int));
2950 if (!common_freqs)
2951 return;
2952
2953 ret = p2p_group_get_common_freqs(wpa_s->p2p_group, common_freqs, &num);
2954 if (ret < 0) {
2955 wpa_dbg(wpa_s, MSG_DEBUG,
2956 "P2P: Failed to get group common freqs");
2957 os_free(common_freqs);
2958 return;
2959 }
2960
2961 os_free(wpa_s->p2p_group_common_freqs);
2962 wpa_s->p2p_group_common_freqs = common_freqs;
2963 wpa_s->p2p_group_common_freqs_num = num;
2964 p2p_go_dump_common_freqs(wpa_s);
2965}
2966
2967
2968/*
2969 * Check if the given frequency is one of the possible operating frequencies
2970 * set after the completion of the GO Negotiation.
2971 */
2972static int wpas_p2p_go_is_peer_freq(struct wpa_supplicant *wpa_s, int freq)
2973{
2974 unsigned int i;
2975
2976 p2p_go_dump_common_freqs(wpa_s);
2977
2978 /* assume no restrictions */
2979 if (!wpa_s->p2p_group_common_freqs_num)
2980 return 1;
2981
2982 for (i = 0; i < wpa_s->p2p_group_common_freqs_num; i++) {
2983 if (wpa_s->p2p_group_common_freqs[i] == freq)
2984 return 1;
2985 }
2986 return 0;
2987}
2988
2989
2990static int wpas_sta_check_ecsa(struct hostapd_data *hapd,
2991 struct sta_info *sta, void *ctx)
2992{
2993 int *ecsa_support = ctx;
2994
2995 *ecsa_support &= sta->ecsa_supported;
2996
2997 return 0;
2998}
2999
3000
3001/* Check if all the peers support eCSA */
3002static int wpas_p2p_go_clients_support_ecsa(struct wpa_supplicant *wpa_s)
3003{
3004 int ecsa_support = 1;
3005
3006 ap_for_each_sta(wpa_s->ap_iface->bss[0], wpas_sta_check_ecsa,
3007 &ecsa_support);
3008
3009 return ecsa_support;
Dmitry Shmidtf7e0a992013-05-23 11:03:10 -07003010}
3011
3012
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07003013/**
3014 * Pick the best frequency to use from all the currently used frequencies.
3015 */
3016static int wpas_p2p_pick_best_used_freq(struct wpa_supplicant *wpa_s,
3017 struct wpa_used_freq_data *freqs,
3018 unsigned int num)
3019{
3020 unsigned int i, c;
3021
3022 /* find a candidate freq that is supported by P2P */
3023 for (c = 0; c < num; c++)
3024 if (p2p_supported_freq(wpa_s->global->p2p, freqs[c].freq))
3025 break;
3026
3027 if (c == num)
3028 return 0;
3029
3030 /* once we have a candidate, try to find a 'better' one */
3031 for (i = c + 1; i < num; i++) {
3032 if (!p2p_supported_freq(wpa_s->global->p2p, freqs[i].freq))
3033 continue;
3034
3035 /*
3036 * 1. Infrastructure station interfaces have higher preference.
3037 * 2. P2P Clients have higher preference.
3038 * 3. All others.
3039 */
3040 if (freqs[i].flags & WPA_FREQ_USED_BY_INFRA_STATION) {
3041 c = i;
3042 break;
3043 }
3044
3045 if ((freqs[i].flags & WPA_FREQ_USED_BY_P2P_CLIENT))
3046 c = i;
3047 }
3048 return freqs[c].freq;
3049}
3050
3051
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003052static u8 wpas_invitation_process(void *ctx, const u8 *sa, const u8 *bssid,
3053 const u8 *go_dev_addr, const u8 *ssid,
3054 size_t ssid_len, int *go, u8 *group_bssid,
Dmitry Shmidtf7e0a992013-05-23 11:03:10 -07003055 int *force_freq, int persistent_group,
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08003056 const struct p2p_channels *channels,
3057 int dev_pw_id)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003058{
3059 struct wpa_supplicant *wpa_s = ctx;
3060 struct wpa_ssid *s;
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07003061 struct wpa_used_freq_data *freqs;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003062 struct wpa_supplicant *grp;
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07003063 int best_freq;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003064
3065 if (!persistent_group) {
3066 wpa_printf(MSG_DEBUG, "P2P: Invitation from " MACSTR
Dmitry Shmidt344abd32014-01-14 13:17:00 -08003067 " to join an active group (SSID: %s)",
3068 MAC2STR(sa), wpa_ssid_txt(ssid, ssid_len));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003069 if (!is_zero_ether_addr(wpa_s->p2p_auth_invite) &&
3070 (os_memcmp(go_dev_addr, wpa_s->p2p_auth_invite, ETH_ALEN)
3071 == 0 ||
3072 os_memcmp(sa, wpa_s->p2p_auth_invite, ETH_ALEN) == 0)) {
3073 wpa_printf(MSG_DEBUG, "P2P: Accept previously "
3074 "authorized invitation");
3075 goto accept_inv;
3076 }
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08003077
3078#ifdef CONFIG_WPS_NFC
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08003079 if (dev_pw_id >= 0 && wpa_s->p2p_nfc_tag_enabled &&
3080 dev_pw_id == wpa_s->p2p_oob_dev_pw_id) {
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08003081 wpa_printf(MSG_DEBUG, "P2P: Accept invitation based on local enabled NFC Tag");
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08003082 wpa_s->p2p_wps_method = WPS_NFC;
3083 wpa_s->pending_join_wps_method = WPS_NFC;
3084 os_memcpy(wpa_s->pending_join_dev_addr,
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08003085 go_dev_addr, ETH_ALEN);
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08003086 os_memcpy(wpa_s->pending_join_iface_addr,
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08003087 bssid, ETH_ALEN);
3088 goto accept_inv;
3089 }
3090#endif /* CONFIG_WPS_NFC */
3091
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003092 /*
3093 * Do not accept the invitation automatically; notify user and
3094 * request approval.
3095 */
3096 return P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE;
3097 }
3098
3099 grp = wpas_get_p2p_group(wpa_s, ssid, ssid_len, go);
3100 if (grp) {
3101 wpa_printf(MSG_DEBUG, "P2P: Accept invitation to already "
3102 "running persistent group");
3103 if (*go)
3104 os_memcpy(group_bssid, grp->own_addr, ETH_ALEN);
3105 goto accept_inv;
3106 }
3107
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08003108 if (!is_zero_ether_addr(wpa_s->p2p_auth_invite) &&
3109 os_memcmp(sa, wpa_s->p2p_auth_invite, ETH_ALEN) == 0) {
3110 wpa_printf(MSG_DEBUG, "P2P: Accept previously initiated "
3111 "invitation to re-invoke a persistent group");
Dmitry Shmidt76cd2cc2014-05-27 12:56:04 -07003112 os_memset(wpa_s->p2p_auth_invite, 0, ETH_ALEN);
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08003113 } else if (!wpa_s->conf->persistent_reconnect)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003114 return P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE;
3115
3116 for (s = wpa_s->conf->ssid; s; s = s->next) {
3117 if (s->disabled == 2 &&
3118 os_memcmp(s->bssid, go_dev_addr, ETH_ALEN) == 0 &&
3119 s->ssid_len == ssid_len &&
3120 os_memcmp(ssid, s->ssid, ssid_len) == 0)
3121 break;
3122 }
3123
3124 if (!s) {
3125 wpa_printf(MSG_DEBUG, "P2P: Invitation from " MACSTR
3126 " requested reinvocation of an unknown group",
3127 MAC2STR(sa));
3128 return P2P_SC_FAIL_UNKNOWN_GROUP;
3129 }
3130
3131 if (s->mode == WPAS_MODE_P2P_GO && !wpas_p2p_create_iface(wpa_s)) {
3132 *go = 1;
3133 if (wpa_s->wpa_state >= WPA_AUTHENTICATING) {
3134 wpa_printf(MSG_DEBUG, "P2P: The only available "
3135 "interface is already in use - reject "
3136 "invitation");
3137 return P2P_SC_FAIL_UNABLE_TO_ACCOMMODATE;
3138 }
Dmitry Shmidt9c175262016-03-03 10:20:07 -08003139 if (wpa_s->p2p_mgmt)
3140 os_memcpy(group_bssid, wpa_s->parent->own_addr,
3141 ETH_ALEN);
3142 else
3143 os_memcpy(group_bssid, wpa_s->own_addr, ETH_ALEN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003144 } else if (s->mode == WPAS_MODE_P2P_GO) {
3145 *go = 1;
3146 if (wpas_p2p_add_group_interface(wpa_s, WPA_IF_P2P_GO) < 0)
3147 {
3148 wpa_printf(MSG_ERROR, "P2P: Failed to allocate a new "
3149 "interface address for the group");
3150 return P2P_SC_FAIL_UNABLE_TO_ACCOMMODATE;
3151 }
3152 os_memcpy(group_bssid, wpa_s->pending_interface_addr,
3153 ETH_ALEN);
3154 }
3155
3156accept_inv:
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003157 wpas_p2p_set_own_freq_preference(wpa_s, 0);
3158
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07003159 best_freq = 0;
3160 freqs = os_calloc(wpa_s->num_multichan_concurrent,
3161 sizeof(struct wpa_used_freq_data));
3162 if (freqs) {
3163 int num_channels = wpa_s->num_multichan_concurrent;
3164 int num = wpas_p2p_valid_oper_freqs(wpa_s, freqs, num_channels);
3165 best_freq = wpas_p2p_pick_best_used_freq(wpa_s, freqs, num);
3166 os_free(freqs);
3167 }
3168
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07003169 /* Get one of the frequencies currently in use */
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07003170 if (best_freq > 0) {
Dmitry Shmidta0d265f2013-11-19 13:13:41 -08003171 wpa_printf(MSG_DEBUG, "P2P: Trying to prefer a channel already used by one of the interfaces");
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07003172 wpas_p2p_set_own_freq_preference(wpa_s, best_freq);
Dmitry Shmidta0d265f2013-11-19 13:13:41 -08003173
3174 if (wpa_s->num_multichan_concurrent < 2 ||
3175 wpas_p2p_num_unused_channels(wpa_s) < 1) {
3176 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 -07003177 *force_freq = best_freq;
Dmitry Shmidta0d265f2013-11-19 13:13:41 -08003178 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003179 }
3180
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07003181 if (*force_freq > 0 && wpa_s->num_multichan_concurrent > 1 &&
3182 wpas_p2p_num_unused_channels(wpa_s) > 0) {
Dmitry Shmidtf7e0a992013-05-23 11:03:10 -07003183 if (*go == 0) {
3184 /* We are the client */
3185 wpa_printf(MSG_DEBUG, "P2P: Peer was found to be "
3186 "running a GO but we are capable of MCC, "
3187 "figure out the best channel to use");
3188 *force_freq = 0;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003189 } else if (!freq_included(wpa_s, channels, *force_freq)) {
Dmitry Shmidtf7e0a992013-05-23 11:03:10 -07003190 /* We are the GO, and *force_freq is not in the
3191 * intersection */
3192 wpa_printf(MSG_DEBUG, "P2P: Forced GO freq %d MHz not "
3193 "in intersection but we are capable of MCC, "
3194 "figure out the best channel to use",
3195 *force_freq);
3196 *force_freq = 0;
3197 }
3198 }
3199
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003200 return P2P_SC_SUCCESS;
3201}
3202
3203
3204static void wpas_invitation_received(void *ctx, const u8 *sa, const u8 *bssid,
3205 const u8 *ssid, size_t ssid_len,
3206 const u8 *go_dev_addr, u8 status,
3207 int op_freq)
3208{
3209 struct wpa_supplicant *wpa_s = ctx;
3210 struct wpa_ssid *s;
3211
3212 for (s = wpa_s->conf->ssid; s; s = s->next) {
3213 if (s->disabled == 2 &&
3214 s->ssid_len == ssid_len &&
3215 os_memcmp(ssid, s->ssid, ssid_len) == 0)
3216 break;
3217 }
3218
3219 if (status == P2P_SC_SUCCESS) {
3220 wpa_printf(MSG_DEBUG, "P2P: Invitation from peer " MACSTR
Dmitry Shmidt344abd32014-01-14 13:17:00 -08003221 " was accepted; op_freq=%d MHz, SSID=%s",
3222 MAC2STR(sa), op_freq, wpa_ssid_txt(ssid, ssid_len));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003223 if (s) {
Dmitry Shmidt91c40cd2012-09-25 14:23:53 -07003224 int go = s->mode == WPAS_MODE_P2P_GO;
Dmitry Shmidt849734c2016-05-27 09:59:01 -07003225 if (go) {
3226 wpa_msg_global(wpa_s, MSG_INFO,
3227 P2P_EVENT_INVITATION_ACCEPTED
3228 "sa=" MACSTR
3229 " persistent=%d freq=%d",
3230 MAC2STR(sa), s->id, op_freq);
3231 } else {
3232 wpa_msg_global(wpa_s, MSG_INFO,
3233 P2P_EVENT_INVITATION_ACCEPTED
3234 "sa=" MACSTR
3235 " persistent=%d",
3236 MAC2STR(sa), s->id);
3237 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003238 wpas_p2p_group_add_persistent(
Hai Shalomb755a2a2020-04-23 21:49:02 -07003239 wpa_s, s, go, 0, op_freq, 0,
3240 wpa_s->conf->p2p_go_ht40,
3241 wpa_s->conf->p2p_go_vht,
3242 0,
3243 wpa_s->conf->p2p_go_he,
3244 wpa_s->conf->p2p_go_edmg, NULL,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003245 go ? P2P_MAX_INITIAL_CONN_WAIT_GO_REINVOKE : 0,
3246 1);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003247 } else if (bssid) {
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08003248 wpa_s->user_initiated_pd = 0;
Dmitry Shmidt849734c2016-05-27 09:59:01 -07003249 wpa_msg_global(wpa_s, MSG_INFO,
3250 P2P_EVENT_INVITATION_ACCEPTED
3251 "sa=" MACSTR " go_dev_addr=" MACSTR
3252 " bssid=" MACSTR " unknown-network",
3253 MAC2STR(sa), MAC2STR(go_dev_addr),
3254 MAC2STR(bssid));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003255 wpas_p2p_join(wpa_s, bssid, go_dev_addr,
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08003256 wpa_s->p2p_wps_method, 0, op_freq,
Dmitry Shmidt344abd32014-01-14 13:17:00 -08003257 ssid, ssid_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003258 }
3259 return;
3260 }
3261
3262 if (status != P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE) {
3263 wpa_printf(MSG_DEBUG, "P2P: Invitation from peer " MACSTR
3264 " was rejected (status %u)", MAC2STR(sa), status);
3265 return;
3266 }
3267
3268 if (!s) {
3269 if (bssid) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07003270 wpa_msg_global(wpa_s, MSG_INFO,
3271 P2P_EVENT_INVITATION_RECEIVED
3272 "sa=" MACSTR " go_dev_addr=" MACSTR
3273 " bssid=" MACSTR " unknown-network",
3274 MAC2STR(sa), MAC2STR(go_dev_addr),
3275 MAC2STR(bssid));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003276 } else {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07003277 wpa_msg_global(wpa_s, MSG_INFO,
3278 P2P_EVENT_INVITATION_RECEIVED
3279 "sa=" MACSTR " go_dev_addr=" MACSTR
3280 " unknown-network",
3281 MAC2STR(sa), MAC2STR(go_dev_addr));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003282 }
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003283 wpas_notify_p2p_invitation_received(wpa_s, sa, go_dev_addr,
3284 bssid, 0, op_freq);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003285 return;
3286 }
3287
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08003288 if (s->mode == WPAS_MODE_P2P_GO && op_freq) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07003289 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_INVITATION_RECEIVED
3290 "sa=" MACSTR " persistent=%d freq=%d",
3291 MAC2STR(sa), s->id, op_freq);
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08003292 } else {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07003293 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_INVITATION_RECEIVED
3294 "sa=" MACSTR " persistent=%d",
3295 MAC2STR(sa), s->id);
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08003296 }
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003297 wpas_notify_p2p_invitation_received(wpa_s, sa, go_dev_addr, bssid,
3298 s->id, op_freq);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003299}
3300
3301
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003302static void wpas_remove_persistent_peer(struct wpa_supplicant *wpa_s,
3303 struct wpa_ssid *ssid,
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07003304 const u8 *peer, int inv)
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003305{
3306 size_t i;
Dmitry Shmidt8bd70b72015-05-26 16:02:19 -07003307 struct wpa_supplicant *p2p_wpa_s = wpa_s->global->p2p_init_wpa_s;
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003308
3309 if (ssid == NULL)
3310 return;
3311
3312 for (i = 0; ssid->p2p_client_list && i < ssid->num_p2p_clients; i++) {
Dmitry Shmidtff787d52015-01-12 13:01:47 -08003313 if (os_memcmp(ssid->p2p_client_list + i * 2 * ETH_ALEN, peer,
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003314 ETH_ALEN) == 0)
3315 break;
3316 }
Dmitry Shmidt6aa8ae42014-07-07 09:31:33 -07003317 if (i >= ssid->num_p2p_clients || !ssid->p2p_client_list) {
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003318 if (ssid->mode != WPAS_MODE_P2P_GO &&
3319 os_memcmp(ssid->bssid, peer, ETH_ALEN) == 0) {
3320 wpa_printf(MSG_DEBUG, "P2P: Remove persistent group %d "
3321 "due to invitation result", ssid->id);
3322 wpas_notify_network_removed(wpa_s, ssid);
3323 wpa_config_remove_network(wpa_s->conf, ssid->id);
3324 return;
3325 }
3326 return; /* Peer not found in client list */
3327 }
3328
3329 wpa_printf(MSG_DEBUG, "P2P: Remove peer " MACSTR " from persistent "
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07003330 "group %d client list%s",
3331 MAC2STR(peer), ssid->id,
3332 inv ? " due to invitation result" : "");
Dmitry Shmidtff787d52015-01-12 13:01:47 -08003333 os_memmove(ssid->p2p_client_list + i * 2 * ETH_ALEN,
3334 ssid->p2p_client_list + (i + 1) * 2 * ETH_ALEN,
3335 (ssid->num_p2p_clients - i - 1) * 2 * ETH_ALEN);
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003336 ssid->num_p2p_clients--;
Dmitry Shmidt8bd70b72015-05-26 16:02:19 -07003337 if (p2p_wpa_s->conf->update_config &&
3338 wpa_config_write(p2p_wpa_s->confname, p2p_wpa_s->conf))
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003339 wpa_printf(MSG_DEBUG, "P2P: Failed to update configuration");
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003340}
3341
3342
3343static void wpas_remove_persistent_client(struct wpa_supplicant *wpa_s,
3344 const u8 *peer)
3345{
3346 struct wpa_ssid *ssid;
3347
3348 wpa_s = wpa_s->global->p2p_invite_group;
3349 if (wpa_s == NULL)
3350 return; /* No known invitation group */
3351 ssid = wpa_s->current_ssid;
3352 if (ssid == NULL || ssid->mode != WPAS_MODE_P2P_GO ||
3353 !ssid->p2p_persistent_group)
3354 return; /* Not operating as a GO in persistent group */
Dmitry Shmidt9c175262016-03-03 10:20:07 -08003355 ssid = wpas_p2p_get_persistent(wpa_s->p2pdev, peer,
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003356 ssid->ssid, ssid->ssid_len);
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07003357 wpas_remove_persistent_peer(wpa_s, ssid, peer, 1);
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003358}
3359
3360
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08003361static void wpas_invitation_result(void *ctx, int status, const u8 *bssid,
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003362 const struct p2p_channels *channels,
Dmitry Shmidt15907092014-03-25 10:42:57 -07003363 const u8 *peer, int neg_freq,
3364 int peer_oper_freq)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003365{
3366 struct wpa_supplicant *wpa_s = ctx;
3367 struct wpa_ssid *ssid;
Dmitry Shmidt15907092014-03-25 10:42:57 -07003368 int freq;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003369
3370 if (bssid) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07003371 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_INVITATION_RESULT
3372 "status=%d " MACSTR,
3373 status, MAC2STR(bssid));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003374 } else {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07003375 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_INVITATION_RESULT
3376 "status=%d ", status);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003377 }
3378 wpas_notify_p2p_invitation_result(wpa_s, status, bssid);
3379
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003380 wpa_printf(MSG_DEBUG, "P2P: Invitation result - status=%d peer=" MACSTR,
3381 status, MAC2STR(peer));
3382 if (wpa_s->pending_invite_ssid_id == -1) {
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08003383 struct wpa_supplicant *group_if =
3384 wpa_s->global->p2p_invite_group;
3385
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003386 if (status == P2P_SC_FAIL_UNKNOWN_GROUP)
3387 wpas_remove_persistent_client(wpa_s, peer);
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08003388
3389 /*
3390 * Invitation to an active group. If this is successful and we
3391 * are the GO, set the client wait to postpone some concurrent
3392 * operations and to allow provisioning and connection to happen
3393 * more quickly.
3394 */
3395 if (status == P2P_SC_SUCCESS &&
3396 group_if && group_if->current_ssid &&
3397 group_if->current_ssid->mode == WPAS_MODE_P2P_GO) {
3398 os_get_reltime(&wpa_s->global->p2p_go_wait_client);
3399#ifdef CONFIG_TESTING_OPTIONS
3400 if (group_if->p2p_go_csa_on_inv) {
3401 wpa_printf(MSG_DEBUG,
3402 "Testing: force P2P GO CSA after invitation");
3403 eloop_cancel_timeout(
3404 wpas_p2p_reconsider_moving_go,
3405 wpa_s, NULL);
3406 eloop_register_timeout(
3407 0, 50000,
3408 wpas_p2p_reconsider_moving_go,
3409 wpa_s, NULL);
3410 }
3411#endif /* CONFIG_TESTING_OPTIONS */
3412 }
3413 return;
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003414 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003415
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08003416 if (status == P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE) {
3417 wpa_printf(MSG_DEBUG, "P2P: Waiting for peer to start another "
3418 "invitation exchange to indicate readiness for "
3419 "re-invocation");
3420 }
3421
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003422 if (status != P2P_SC_SUCCESS) {
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003423 if (status == P2P_SC_FAIL_UNKNOWN_GROUP) {
3424 ssid = wpa_config_get_network(
3425 wpa_s->conf, wpa_s->pending_invite_ssid_id);
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07003426 wpas_remove_persistent_peer(wpa_s, ssid, peer, 1);
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003427 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003428 wpas_p2p_remove_pending_group_interface(wpa_s);
3429 return;
3430 }
3431
3432 ssid = wpa_config_get_network(wpa_s->conf,
3433 wpa_s->pending_invite_ssid_id);
3434 if (ssid == NULL) {
3435 wpa_printf(MSG_ERROR, "P2P: Could not find persistent group "
3436 "data matching with invitation");
3437 return;
3438 }
3439
Jouni Malinen2c5b17d2012-10-13 21:52:46 -07003440 /*
3441 * The peer could have missed our ctrl::ack frame for Invitation
3442 * Response and continue retransmitting the frame. To reduce the
3443 * likelihood of the peer not getting successful TX status for the
3444 * Invitation Response frame, wait a short time here before starting
3445 * the persistent group so that we will remain on the current channel to
3446 * acknowledge any possible retransmission from the peer.
3447 */
3448 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: 50 ms wait on current channel before "
3449 "starting persistent group");
3450 os_sleep(0, 50000);
3451
Dmitry Shmidt15907092014-03-25 10:42:57 -07003452 if (neg_freq > 0 && ssid->mode == WPAS_MODE_P2P_GO &&
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003453 freq_included(wpa_s, channels, neg_freq))
Dmitry Shmidt15907092014-03-25 10:42:57 -07003454 freq = neg_freq;
3455 else if (peer_oper_freq > 0 && ssid->mode != WPAS_MODE_P2P_GO &&
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003456 freq_included(wpa_s, channels, peer_oper_freq))
Dmitry Shmidt15907092014-03-25 10:42:57 -07003457 freq = peer_oper_freq;
3458 else
3459 freq = 0;
3460
3461 wpa_printf(MSG_DEBUG, "P2P: Persistent group invitation success - op_freq=%d MHz SSID=%s",
3462 freq, wpa_ssid_txt(ssid->ssid, ssid->ssid_len));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003463 wpas_p2p_group_add_persistent(wpa_s, ssid,
Jouni Malinen31be0a42012-08-31 21:20:51 +03003464 ssid->mode == WPAS_MODE_P2P_GO,
Dmitry Shmidt96be6222014-02-13 10:16:51 -08003465 wpa_s->p2p_persistent_go_freq,
Dmitry Shmidt15907092014-03-25 10:42:57 -07003466 freq,
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08003467 wpa_s->p2p_go_vht_center_freq2,
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003468 wpa_s->p2p_go_ht40, wpa_s->p2p_go_vht,
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08003469 wpa_s->p2p_go_max_oper_chwidth,
Hai Shalom74f70d42019-02-11 14:42:39 -08003470 wpa_s->p2p_go_he,
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08003471 wpa_s->p2p_go_edmg,
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003472 channels,
Dmitry Shmidt56052862013-10-04 10:23:25 -07003473 ssid->mode == WPAS_MODE_P2P_GO ?
3474 P2P_MAX_INITIAL_CONN_WAIT_GO_REINVOKE :
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003475 0, 1);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003476}
3477
3478
Dmitry Shmidt04949592012-07-19 12:16:46 -07003479static int wpas_p2p_disallowed_freq(struct wpa_global *global,
3480 unsigned int freq)
3481{
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08003482 if (freq_range_list_includes(&global->p2p_go_avoid_freq, freq))
3483 return 1;
Dmitry Shmidt4ce9c872013-10-24 11:08:13 -07003484 return freq_range_list_includes(&global->p2p_disallow_freq, freq);
Dmitry Shmidt04949592012-07-19 12:16:46 -07003485}
3486
3487
3488static void wpas_p2p_add_chan(struct p2p_reg_class *reg, u8 chan)
3489{
3490 reg->channel[reg->channels] = chan;
3491 reg->channels++;
3492}
3493
3494
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003495static int wpas_p2p_default_channels(struct wpa_supplicant *wpa_s,
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003496 struct p2p_channels *chan,
3497 struct p2p_channels *cli_chan)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003498{
3499 int i, cla = 0;
3500
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003501 wpa_s->global->p2p_24ghz_social_channels = 1;
3502
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003503 os_memset(cli_chan, 0, sizeof(*cli_chan));
3504
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003505 wpa_printf(MSG_DEBUG, "P2P: Enable operating classes for 2.4 GHz "
3506 "band");
3507
3508 /* Operating class 81 - 2.4 GHz band channels 1..13 */
3509 chan->reg_class[cla].reg_class = 81;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003510 chan->reg_class[cla].channels = 0;
3511 for (i = 0; i < 11; i++) {
3512 if (!wpas_p2p_disallowed_freq(wpa_s->global, 2412 + i * 5))
3513 wpas_p2p_add_chan(&chan->reg_class[cla], i + 1);
3514 }
3515 if (chan->reg_class[cla].channels)
3516 cla++;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003517
3518 wpa_printf(MSG_DEBUG, "P2P: Enable operating classes for lower 5 GHz "
3519 "band");
3520
3521 /* Operating class 115 - 5 GHz, channels 36-48 */
3522 chan->reg_class[cla].reg_class = 115;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003523 chan->reg_class[cla].channels = 0;
3524 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 36 * 5))
3525 wpas_p2p_add_chan(&chan->reg_class[cla], 36);
3526 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 40 * 5))
3527 wpas_p2p_add_chan(&chan->reg_class[cla], 40);
3528 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 44 * 5))
3529 wpas_p2p_add_chan(&chan->reg_class[cla], 44);
3530 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 48 * 5))
3531 wpas_p2p_add_chan(&chan->reg_class[cla], 48);
3532 if (chan->reg_class[cla].channels)
3533 cla++;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003534
3535 wpa_printf(MSG_DEBUG, "P2P: Enable operating classes for higher 5 GHz "
3536 "band");
3537
3538 /* Operating class 124 - 5 GHz, channels 149,153,157,161 */
3539 chan->reg_class[cla].reg_class = 124;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003540 chan->reg_class[cla].channels = 0;
3541 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 149 * 5))
3542 wpas_p2p_add_chan(&chan->reg_class[cla], 149);
3543 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 153 * 5))
3544 wpas_p2p_add_chan(&chan->reg_class[cla], 153);
3545 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 156 * 5))
3546 wpas_p2p_add_chan(&chan->reg_class[cla], 157);
3547 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 161 * 5))
3548 wpas_p2p_add_chan(&chan->reg_class[cla], 161);
3549 if (chan->reg_class[cla].channels)
3550 cla++;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003551
3552 chan->reg_classes = cla;
3553 return 0;
3554}
3555
3556
Dmitry Shmidt04949592012-07-19 12:16:46 -07003557static int has_channel(struct wpa_global *global,
3558 struct hostapd_hw_modes *mode, u8 chan, int *flags)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003559{
3560 int i;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003561 unsigned int freq;
3562
3563 freq = (mode->mode == HOSTAPD_MODE_IEEE80211A ? 5000 : 2407) +
3564 chan * 5;
3565 if (wpas_p2p_disallowed_freq(global, freq))
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003566 return NOT_ALLOWED;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003567
3568 for (i = 0; i < mode->num_channels; i++) {
3569 if (mode->channels[i].chan == chan) {
3570 if (flags)
3571 *flags = mode->channels[i].flag;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003572 if (mode->channels[i].flag &
3573 (HOSTAPD_CHAN_DISABLED |
3574 HOSTAPD_CHAN_RADAR))
3575 return NOT_ALLOWED;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003576 if (mode->channels[i].flag & HOSTAPD_CHAN_NO_IR)
3577 return NO_IR;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003578 return ALLOWED;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003579 }
3580 }
3581
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003582 return NOT_ALLOWED;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003583}
3584
3585
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003586static int wpas_p2p_get_center_80mhz(struct wpa_supplicant *wpa_s,
3587 struct hostapd_hw_modes *mode,
3588 u8 channel)
3589{
3590 u8 center_channels[] = { 42, 58, 106, 122, 138, 155 };
Dmitry Shmidtaca489e2016-09-28 15:44:14 -07003591 size_t i;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003592
3593 if (mode->mode != HOSTAPD_MODE_IEEE80211A)
3594 return 0;
3595
3596 for (i = 0; i < ARRAY_SIZE(center_channels); i++)
3597 /*
3598 * In 80 MHz, the bandwidth "spans" 12 channels (e.g., 36-48),
3599 * so the center channel is 6 channels away from the start/end.
3600 */
3601 if (channel >= center_channels[i] - 6 &&
3602 channel <= center_channels[i] + 6)
3603 return center_channels[i];
3604
3605 return 0;
3606}
3607
3608
3609static enum chan_allowed wpas_p2p_verify_80mhz(struct wpa_supplicant *wpa_s,
3610 struct hostapd_hw_modes *mode,
3611 u8 channel, u8 bw)
3612{
3613 u8 center_chan;
3614 int i, flags;
3615 enum chan_allowed res, ret = ALLOWED;
3616
3617 center_chan = wpas_p2p_get_center_80mhz(wpa_s, mode, channel);
3618 if (!center_chan)
3619 return NOT_ALLOWED;
3620 if (center_chan >= 58 && center_chan <= 138)
3621 return NOT_ALLOWED; /* Do not allow DFS channels for P2P */
3622
3623 /* check all the channels are available */
3624 for (i = 0; i < 4; i++) {
3625 int adj_chan = center_chan - 6 + i * 4;
3626
3627 res = has_channel(wpa_s->global, mode, adj_chan, &flags);
3628 if (res == NOT_ALLOWED)
3629 return NOT_ALLOWED;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003630 if (res == NO_IR)
3631 ret = NO_IR;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003632
3633 if (i == 0 && !(flags & HOSTAPD_CHAN_VHT_10_70))
3634 return NOT_ALLOWED;
3635 if (i == 1 && !(flags & HOSTAPD_CHAN_VHT_30_50))
3636 return NOT_ALLOWED;
3637 if (i == 2 && !(flags & HOSTAPD_CHAN_VHT_50_30))
3638 return NOT_ALLOWED;
3639 if (i == 3 && !(flags & HOSTAPD_CHAN_VHT_70_10))
3640 return NOT_ALLOWED;
3641 }
3642
3643 return ret;
3644}
3645
3646
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08003647static int wpas_p2p_get_center_160mhz(struct wpa_supplicant *wpa_s,
3648 struct hostapd_hw_modes *mode,
3649 u8 channel)
3650{
3651 u8 center_channels[] = { 50, 114 };
3652 unsigned int i;
3653
3654 if (mode->mode != HOSTAPD_MODE_IEEE80211A)
3655 return 0;
3656
3657 for (i = 0; i < ARRAY_SIZE(center_channels); i++)
3658 /*
3659 * In 160 MHz, the bandwidth "spans" 28 channels (e.g., 36-64),
3660 * so the center channel is 14 channels away from the start/end.
3661 */
3662 if (channel >= center_channels[i] - 14 &&
3663 channel <= center_channels[i] + 14)
3664 return center_channels[i];
3665
3666 return 0;
3667}
3668
3669
3670static enum chan_allowed wpas_p2p_verify_160mhz(struct wpa_supplicant *wpa_s,
3671 struct hostapd_hw_modes *mode,
3672 u8 channel, u8 bw)
3673{
3674 u8 center_chan;
3675 int i, flags;
3676 enum chan_allowed res, ret = ALLOWED;
3677
3678 center_chan = wpas_p2p_get_center_160mhz(wpa_s, mode, channel);
3679 if (!center_chan)
3680 return NOT_ALLOWED;
3681 /* VHT 160 MHz uses DFS channels in most countries. */
3682
3683 /* Check all the channels are available */
3684 for (i = 0; i < 8; i++) {
3685 int adj_chan = center_chan - 14 + i * 4;
3686
3687 res = has_channel(wpa_s->global, mode, adj_chan, &flags);
3688 if (res == NOT_ALLOWED)
3689 return NOT_ALLOWED;
3690
3691 if (res == NO_IR)
3692 ret = NO_IR;
3693
3694 if (i == 0 && !(flags & HOSTAPD_CHAN_VHT_10_150))
3695 return NOT_ALLOWED;
3696 if (i == 1 && !(flags & HOSTAPD_CHAN_VHT_30_130))
3697 return NOT_ALLOWED;
3698 if (i == 2 && !(flags & HOSTAPD_CHAN_VHT_50_110))
3699 return NOT_ALLOWED;
3700 if (i == 3 && !(flags & HOSTAPD_CHAN_VHT_70_90))
3701 return NOT_ALLOWED;
3702 if (i == 4 && !(flags & HOSTAPD_CHAN_VHT_90_70))
3703 return NOT_ALLOWED;
3704 if (i == 5 && !(flags & HOSTAPD_CHAN_VHT_110_50))
3705 return NOT_ALLOWED;
3706 if (i == 6 && !(flags & HOSTAPD_CHAN_VHT_130_30))
3707 return NOT_ALLOWED;
3708 if (i == 7 && !(flags & HOSTAPD_CHAN_VHT_150_10))
3709 return NOT_ALLOWED;
3710 }
3711
3712 return ret;
3713}
3714
3715
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08003716static enum chan_allowed wpas_p2p_verify_edmg(struct wpa_supplicant *wpa_s,
3717 struct hostapd_hw_modes *mode,
3718 u8 channel)
3719{
3720 struct ieee80211_edmg_config edmg;
3721
3722 hostapd_encode_edmg_chan(1, channel, 0, &edmg);
3723 if (edmg.channels && ieee802_edmg_is_allowed(mode->edmg, edmg))
3724 return ALLOWED;
3725
3726 return NOT_ALLOWED;
3727}
3728
3729
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003730static enum chan_allowed wpas_p2p_verify_channel(struct wpa_supplicant *wpa_s,
3731 struct hostapd_hw_modes *mode,
3732 u8 channel, u8 bw)
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003733{
Dmitry Shmidt96be6222014-02-13 10:16:51 -08003734 int flag = 0;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003735 enum chan_allowed res, res2;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003736
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003737 res2 = res = has_channel(wpa_s->global, mode, channel, &flag);
3738 if (bw == BW40MINUS) {
3739 if (!(flag & HOSTAPD_CHAN_HT40MINUS))
3740 return NOT_ALLOWED;
3741 res2 = has_channel(wpa_s->global, mode, channel - 4, NULL);
3742 } else if (bw == BW40PLUS) {
3743 if (!(flag & HOSTAPD_CHAN_HT40PLUS))
3744 return NOT_ALLOWED;
3745 res2 = has_channel(wpa_s->global, mode, channel + 4, NULL);
3746 } else if (bw == BW80) {
3747 res2 = wpas_p2p_verify_80mhz(wpa_s, mode, channel, bw);
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08003748 } else if (bw == BW160) {
3749 res2 = wpas_p2p_verify_160mhz(wpa_s, mode, channel, bw);
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08003750 } else if (bw == BW4320 || bw == BW6480 || bw == BW8640) {
3751 return wpas_p2p_verify_edmg(wpa_s, mode, channel);
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003752 }
3753
3754 if (res == NOT_ALLOWED || res2 == NOT_ALLOWED)
3755 return NOT_ALLOWED;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003756 if (res == NO_IR || res2 == NO_IR)
3757 return NO_IR;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003758 return res;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003759}
3760
3761
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003762static int wpas_p2p_setup_channels(struct wpa_supplicant *wpa_s,
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003763 struct p2p_channels *chan,
3764 struct p2p_channels *cli_chan)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003765{
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003766 struct hostapd_hw_modes *mode;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003767 int cla, op, cli_cla;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003768
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003769 if (wpa_s->hw.modes == NULL) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003770 wpa_printf(MSG_DEBUG, "P2P: Driver did not support fetching "
3771 "of all supported channels; assume dualband "
3772 "support");
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003773 return wpas_p2p_default_channels(wpa_s, chan, cli_chan);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003774 }
3775
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003776 cla = cli_cla = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003777
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08003778 for (op = 0; global_op_class[op].op_class; op++) {
3779 const struct oper_class_map *o = &global_op_class[op];
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003780 u8 ch;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003781 struct p2p_reg_class *reg = NULL, *cli_reg = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003782
Hai Shalom899fcc72020-10-19 14:38:18 -07003783 if (o->p2p == NO_P2P_SUPP ||
3784 (is_6ghz_op_class(o->op_class) &&
3785 wpa_s->conf->p2p_6ghz_disable))
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08003786 continue;
3787
Hai Shalomfdcde762020-04-02 11:19:20 -07003788 mode = get_mode(wpa_s->hw.modes, wpa_s->hw.num_modes, o->mode,
3789 is_6ghz_op_class(o->op_class));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003790 if (mode == NULL)
3791 continue;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003792 if (mode->mode == HOSTAPD_MODE_IEEE80211G)
3793 wpa_s->global->p2p_24ghz_social_channels = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003794 for (ch = o->min_chan; ch <= o->max_chan; ch += o->inc) {
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003795 enum chan_allowed res;
Hai Shalom899fcc72020-10-19 14:38:18 -07003796
3797 /* Check for non-continuous jump in channel index
3798 * incrementation */
3799 if ((o->op_class == 128 || o->op_class == 130) &&
3800 ch < 149 && ch + o->inc > 149)
3801 ch = 149;
3802
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003803 res = wpas_p2p_verify_channel(wpa_s, mode, ch, o->bw);
3804 if (res == ALLOWED) {
3805 if (reg == NULL) {
Hai Shalomfdcde762020-04-02 11:19:20 -07003806 if (cla == P2P_MAX_REG_CLASSES)
3807 continue;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003808 wpa_printf(MSG_DEBUG, "P2P: Add operating class %u",
3809 o->op_class);
3810 reg = &chan->reg_class[cla];
3811 cla++;
3812 reg->reg_class = o->op_class;
3813 }
Hai Shalomfdcde762020-04-02 11:19:20 -07003814 if (reg->channels == P2P_MAX_REG_CLASS_CHANNELS)
3815 continue;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003816 reg->channel[reg->channels] = ch;
3817 reg->channels++;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003818 } else if (res == NO_IR &&
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003819 wpa_s->conf->p2p_add_cli_chan) {
3820 if (cli_reg == NULL) {
Hai Shalomfdcde762020-04-02 11:19:20 -07003821 if (cli_cla == P2P_MAX_REG_CLASSES)
3822 continue;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003823 wpa_printf(MSG_DEBUG, "P2P: Add operating class %u (client only)",
3824 o->op_class);
3825 cli_reg = &cli_chan->reg_class[cli_cla];
3826 cli_cla++;
3827 cli_reg->reg_class = o->op_class;
3828 }
Hai Shalomfdcde762020-04-02 11:19:20 -07003829 if (cli_reg->channels ==
3830 P2P_MAX_REG_CLASS_CHANNELS)
3831 continue;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003832 cli_reg->channel[cli_reg->channels] = ch;
3833 cli_reg->channels++;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003834 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003835 }
3836 if (reg) {
3837 wpa_hexdump(MSG_DEBUG, "P2P: Channels",
3838 reg->channel, reg->channels);
3839 }
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003840 if (cli_reg) {
3841 wpa_hexdump(MSG_DEBUG, "P2P: Channels (client only)",
3842 cli_reg->channel, cli_reg->channels);
3843 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003844 }
3845
3846 chan->reg_classes = cla;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003847 cli_chan->reg_classes = cli_cla;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003848
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003849 return 0;
3850}
3851
3852
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003853int wpas_p2p_get_ht40_mode(struct wpa_supplicant *wpa_s,
3854 struct hostapd_hw_modes *mode, u8 channel)
3855{
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003856 int op;
3857 enum chan_allowed ret;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003858
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08003859 for (op = 0; global_op_class[op].op_class; op++) {
3860 const struct oper_class_map *o = &global_op_class[op];
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003861 u8 ch;
3862
Hai Shalom899fcc72020-10-19 14:38:18 -07003863 if (o->p2p == NO_P2P_SUPP ||
3864 (is_6ghz_op_class(o->op_class) &&
3865 wpa_s->conf->p2p_6ghz_disable))
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08003866 continue;
3867
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003868 for (ch = o->min_chan; ch <= o->max_chan; ch += o->inc) {
3869 if (o->mode != HOSTAPD_MODE_IEEE80211A ||
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07003870 (o->bw != BW40PLUS && o->bw != BW40MINUS) ||
3871 ch != channel)
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003872 continue;
3873 ret = wpas_p2p_verify_channel(wpa_s, mode, ch, o->bw);
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003874 if (ret == ALLOWED)
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003875 return (o->bw == BW40MINUS) ? -1 : 1;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003876 }
3877 }
3878 return 0;
3879}
3880
3881
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003882int wpas_p2p_get_vht80_center(struct wpa_supplicant *wpa_s,
3883 struct hostapd_hw_modes *mode, u8 channel)
3884{
3885 if (!wpas_p2p_verify_channel(wpa_s, mode, channel, BW80))
3886 return 0;
3887
3888 return wpas_p2p_get_center_80mhz(wpa_s, mode, channel);
3889}
3890
3891
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08003892int wpas_p2p_get_vht160_center(struct wpa_supplicant *wpa_s,
3893 struct hostapd_hw_modes *mode, u8 channel)
3894{
3895 if (!wpas_p2p_verify_channel(wpa_s, mode, channel, BW160))
3896 return 0;
3897 return wpas_p2p_get_center_160mhz(wpa_s, mode, channel);
3898}
3899
3900
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003901static int wpas_get_noa(void *ctx, const u8 *interface_addr, u8 *buf,
3902 size_t buf_len)
3903{
3904 struct wpa_supplicant *wpa_s = ctx;
3905
3906 for (wpa_s = wpa_s->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
3907 if (os_memcmp(wpa_s->own_addr, interface_addr, ETH_ALEN) == 0)
3908 break;
3909 }
3910 if (wpa_s == NULL)
3911 return -1;
3912
3913 return wpa_drv_get_noa(wpa_s, buf, buf_len);
3914}
3915
3916
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07003917struct wpa_supplicant * wpas_get_p2p_go_iface(struct wpa_supplicant *wpa_s,
3918 const u8 *ssid, size_t ssid_len)
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08003919{
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07003920 for (wpa_s = wpa_s->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
3921 struct wpa_ssid *s = wpa_s->current_ssid;
3922 if (s == NULL)
3923 continue;
3924 if (s->mode != WPAS_MODE_P2P_GO &&
3925 s->mode != WPAS_MODE_AP &&
3926 s->mode != WPAS_MODE_P2P_GROUP_FORMATION)
3927 continue;
3928 if (s->ssid_len != ssid_len ||
Dmitry Shmidt03658832014-08-13 11:03:49 -07003929 os_memcmp(ssid, s->ssid, ssid_len) != 0)
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07003930 continue;
3931 return wpa_s;
3932 }
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08003933
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07003934 return NULL;
3935
3936}
3937
3938
3939struct wpa_supplicant * wpas_get_p2p_client_iface(struct wpa_supplicant *wpa_s,
3940 const u8 *peer_dev_addr)
3941{
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08003942 for (wpa_s = wpa_s->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
3943 struct wpa_ssid *ssid = wpa_s->current_ssid;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003944 if (ssid && (ssid->mode != WPAS_MODE_INFRA || !ssid->p2p_group))
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08003945 continue;
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07003946 if (os_memcmp(wpa_s->go_dev_addr, peer_dev_addr, ETH_ALEN) == 0)
3947 return wpa_s;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08003948 }
3949
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07003950 return NULL;
3951}
3952
3953
3954static int wpas_go_connected(void *ctx, const u8 *dev_addr)
3955{
3956 struct wpa_supplicant *wpa_s = ctx;
3957
3958 return wpas_get_p2p_client_iface(wpa_s, dev_addr) != NULL;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08003959}
3960
3961
Dmitry Shmidt18463232014-01-24 12:29:41 -08003962static int wpas_is_concurrent_session_active(void *ctx)
3963{
3964 struct wpa_supplicant *wpa_s = ctx;
3965 struct wpa_supplicant *ifs;
3966
3967 for (ifs = wpa_s->global->ifaces; ifs; ifs = ifs->next) {
3968 if (ifs == wpa_s)
3969 continue;
3970 if (ifs->wpa_state > WPA_ASSOCIATED)
3971 return 1;
3972 }
3973 return 0;
3974}
3975
3976
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07003977static void wpas_p2p_debug_print(void *ctx, int level, const char *msg)
3978{
3979 struct wpa_supplicant *wpa_s = ctx;
3980 wpa_msg_global(wpa_s, level, "P2P: %s", msg);
3981}
3982
3983
Dmitry Shmidt9ead16e2014-10-07 13:15:23 -07003984int wpas_p2p_add_p2pdev_interface(struct wpa_supplicant *wpa_s,
3985 const char *conf_p2p_dev)
Dmitry Shmidt34af3062013-07-11 10:46:32 -07003986{
3987 struct wpa_interface iface;
3988 struct wpa_supplicant *p2pdev_wpa_s;
3989 char ifname[100];
3990 char force_name[100];
3991 int ret;
3992
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003993 ret = os_snprintf(ifname, sizeof(ifname), P2P_MGMT_DEVICE_PREFIX "%s",
3994 wpa_s->ifname);
3995 if (os_snprintf_error(sizeof(ifname), ret))
3996 return -1;
Hai Shalom899fcc72020-10-19 14:38:18 -07003997 /* Cut length at the maximum size. Note that we don't need to ensure
3998 * collision free names here as the created interface is not a netdev.
3999 */
4000 ifname[IFNAMSIZ - 1] = '\0';
Dmitry Shmidt34af3062013-07-11 10:46:32 -07004001 force_name[0] = '\0';
4002 wpa_s->pending_interface_type = WPA_IF_P2P_DEVICE;
4003 ret = wpa_drv_if_add(wpa_s, WPA_IF_P2P_DEVICE, ifname, NULL, NULL,
4004 force_name, wpa_s->pending_interface_addr, NULL);
4005 if (ret < 0) {
4006 wpa_printf(MSG_DEBUG, "P2P: Failed to create P2P Device interface");
4007 return ret;
4008 }
4009 os_strlcpy(wpa_s->pending_interface_name, ifname,
4010 sizeof(wpa_s->pending_interface_name));
4011
4012 os_memset(&iface, 0, sizeof(iface));
4013 iface.p2p_mgmt = 1;
4014 iface.ifname = wpa_s->pending_interface_name;
4015 iface.driver = wpa_s->driver->name;
4016 iface.driver_param = wpa_s->conf->driver_param;
Dmitry Shmidt2ac5f602014-03-07 10:08:21 -08004017
4018 /*
4019 * If a P2P Device configuration file was given, use it as the interface
4020 * configuration file (instead of using parent's configuration file.
4021 */
Dmitry Shmidt9ead16e2014-10-07 13:15:23 -07004022 if (conf_p2p_dev) {
4023 iface.confname = conf_p2p_dev;
Dmitry Shmidt2ac5f602014-03-07 10:08:21 -08004024 iface.ctrl_interface = NULL;
4025 } else {
4026 iface.confname = wpa_s->confname;
4027 iface.ctrl_interface = wpa_s->conf->ctrl_interface;
4028 }
Dmitry Shmidt2ac5f602014-03-07 10:08:21 -08004029
Dmitry Shmidt203eadb2015-03-05 14:16:04 -08004030 p2pdev_wpa_s = wpa_supplicant_add_iface(wpa_s->global, &iface, wpa_s);
Dmitry Shmidt34af3062013-07-11 10:46:32 -07004031 if (!p2pdev_wpa_s) {
4032 wpa_printf(MSG_DEBUG, "P2P: Failed to add P2P Device interface");
4033 return -1;
4034 }
Dmitry Shmidt34af3062013-07-11 10:46:32 -07004035
Dmitry Shmidt9c175262016-03-03 10:20:07 -08004036 p2pdev_wpa_s->p2pdev = p2pdev_wpa_s;
Dmitry Shmidt34af3062013-07-11 10:46:32 -07004037 wpa_s->pending_interface_name[0] = '\0';
4038 return 0;
4039}
4040
4041
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08004042static void wpas_presence_resp(void *ctx, const u8 *src, u8 status,
4043 const u8 *noa, size_t noa_len)
4044{
4045 struct wpa_supplicant *wpa_s, *intf = ctx;
4046 char hex[100];
4047
4048 for (wpa_s = intf->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
4049 if (wpa_s->waiting_presence_resp)
4050 break;
4051 }
4052 if (!wpa_s) {
4053 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: No group interface was waiting for presence response");
4054 return;
4055 }
4056 wpa_s->waiting_presence_resp = 0;
4057
4058 wpa_snprintf_hex(hex, sizeof(hex), noa, noa_len);
4059 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_PRESENCE_RESPONSE "src=" MACSTR
4060 " status=%u noa=%s", MAC2STR(src), status, hex);
4061}
4062
4063
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004064static int wpas_get_persistent_group(void *ctx, const u8 *addr, const u8 *ssid,
4065 size_t ssid_len, u8 *go_dev_addr,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004066 u8 *ret_ssid, size_t *ret_ssid_len,
4067 u8 *intended_iface_addr)
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004068{
4069 struct wpa_supplicant *wpa_s = ctx;
4070 struct wpa_ssid *s;
4071
4072 s = wpas_p2p_get_persistent(wpa_s, addr, ssid, ssid_len);
4073 if (s) {
4074 os_memcpy(ret_ssid, s->ssid, s->ssid_len);
4075 *ret_ssid_len = s->ssid_len;
4076 os_memcpy(go_dev_addr, s->bssid, ETH_ALEN);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004077
4078 if (s->mode != WPAS_MODE_P2P_GO) {
4079 os_memset(intended_iface_addr, 0, ETH_ALEN);
4080 } else if (wpas_p2p_create_iface(wpa_s)) {
4081 if (wpas_p2p_add_group_interface(wpa_s, WPA_IF_P2P_GO))
4082 return 0;
4083
4084 os_memcpy(intended_iface_addr,
4085 wpa_s->pending_interface_addr, ETH_ALEN);
4086 } else {
4087 os_memcpy(intended_iface_addr, wpa_s->own_addr,
4088 ETH_ALEN);
4089 }
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004090 return 1;
4091 }
4092
4093 return 0;
4094}
4095
4096
4097static int wpas_get_go_info(void *ctx, u8 *intended_addr,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004098 u8 *ssid, size_t *ssid_len, int *group_iface,
4099 unsigned int *freq)
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004100{
4101 struct wpa_supplicant *wpa_s = ctx;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004102 struct wpa_supplicant *go;
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004103 struct wpa_ssid *s;
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004104
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004105 /*
4106 * group_iface will be set to 1 only if a dedicated interface for P2P
4107 * role is required. First, we try to reuse an active GO. However,
4108 * if it is not present, we will try to reactivate an existing
4109 * persistent group and set group_iface to 1, so the caller will know
4110 * that the pending interface should be used.
4111 */
4112 *group_iface = 0;
4113
4114 if (freq)
4115 *freq = 0;
4116
4117 go = wpas_p2p_get_go_group(wpa_s);
4118 if (!go) {
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004119 s = wpas_p2p_get_persistent_go(wpa_s);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004120 *group_iface = wpas_p2p_create_iface(wpa_s);
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004121 if (s)
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004122 os_memcpy(intended_addr, s->bssid, ETH_ALEN);
4123 else
4124 return 0;
4125 } else {
4126 s = go->current_ssid;
4127 os_memcpy(intended_addr, go->own_addr, ETH_ALEN);
4128 if (freq)
4129 *freq = go->assoc_freq;
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004130 }
4131
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004132 os_memcpy(ssid, s->ssid, s->ssid_len);
4133 *ssid_len = s->ssid_len;
4134
4135 return 1;
4136}
4137
4138
4139static int wpas_remove_stale_groups(void *ctx, const u8 *peer, const u8 *go,
4140 const u8 *ssid, size_t ssid_len)
4141{
4142 struct wpa_supplicant *wpa_s = ctx;
4143 struct wpa_ssid *s;
4144 int save_config = 0;
4145 size_t i;
4146
4147 /* Start with our first choice of Persistent Groups */
4148 while ((s = wpas_p2p_get_persistent(wpa_s, peer, NULL, 0))) {
4149 if (go && ssid && ssid_len &&
4150 s->ssid_len == ssid_len &&
4151 os_memcmp(go, s->bssid, ETH_ALEN) == 0 &&
4152 os_memcmp(ssid, s->ssid, ssid_len) == 0)
4153 break;
4154
4155 /* Remove stale persistent group */
4156 if (s->mode != WPAS_MODE_P2P_GO || s->num_p2p_clients <= 1) {
Hai Shalom74f70d42019-02-11 14:42:39 -08004157 wpa_dbg(wpa_s, MSG_DEBUG,
4158 "P2P: Remove stale persistent group id=%d",
4159 s->id);
4160 wpas_notify_persistent_group_removed(wpa_s, s);
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004161 wpa_config_remove_network(wpa_s->conf, s->id);
4162 save_config = 1;
4163 continue;
4164 }
4165
4166 for (i = 0; i < s->num_p2p_clients; i++) {
4167 if (os_memcmp(s->p2p_client_list + i * 2 * ETH_ALEN,
4168 peer, ETH_ALEN) != 0)
4169 continue;
4170
4171 os_memmove(s->p2p_client_list + i * 2 * ETH_ALEN,
4172 s->p2p_client_list + (i + 1) * 2 * ETH_ALEN,
4173 (s->num_p2p_clients - i - 1) * 2 * ETH_ALEN);
4174 break;
4175 }
4176 s->num_p2p_clients--;
4177 save_config = 1;
4178 }
4179
4180 if (save_config)
4181 p2p_config_write(wpa_s);
4182
4183 /* Return TRUE if valid SSID remains */
4184 return s != NULL;
4185}
4186
4187
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004188static void wpas_p2ps_get_feat_cap_str(char *buf, size_t buf_len,
4189 const u8 *feat_cap, size_t feat_cap_len)
4190{
4191 static const char pref[] = " feature_cap=";
4192 int ret;
4193
4194 buf[0] = '\0';
4195
4196 /*
4197 * We expect a feature capability to contain at least one byte to be
4198 * reported. The string buffer provided by the caller function is
4199 * expected to be big enough to contain all bytes of the attribute for
4200 * known specifications. This function truncates the reported bytes if
4201 * the feature capability data exceeds the string buffer size.
4202 */
4203 if (!feat_cap || !feat_cap_len || buf_len < sizeof(pref) + 2)
4204 return;
4205
4206 os_memcpy(buf, pref, sizeof(pref));
4207 ret = wpa_snprintf_hex(&buf[sizeof(pref) - 1],
4208 buf_len - sizeof(pref) + 1,
4209 feat_cap, feat_cap_len);
4210
4211 if (ret != (2 * (int) feat_cap_len))
4212 wpa_printf(MSG_WARNING, "P2PS feature_cap bytes truncated");
4213}
4214
4215
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004216static void wpas_p2ps_prov_complete(void *ctx, u8 status, const u8 *dev,
4217 const u8 *adv_mac, const u8 *ses_mac,
4218 const u8 *grp_mac, u32 adv_id, u32 ses_id,
4219 u8 conncap, int passwd_id,
4220 const u8 *persist_ssid,
4221 size_t persist_ssid_size, int response_done,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004222 int prov_start, const char *session_info,
4223 const u8 *feat_cap, size_t feat_cap_len,
Dmitry Shmidtde47be72016-01-07 12:52:55 -08004224 unsigned int freq,
4225 const u8 *group_ssid, size_t group_ssid_len)
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004226{
4227 struct wpa_supplicant *wpa_s = ctx;
4228 u8 mac[ETH_ALEN];
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004229 struct wpa_ssid *persistent_go, *stale, *s = NULL;
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004230 int save_config = 0;
4231 struct wpa_supplicant *go_wpa_s;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004232 char feat_cap_str[256];
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004233
4234 if (!dev)
4235 return;
4236
4237 os_memset(mac, 0, ETH_ALEN);
4238 if (!adv_mac)
4239 adv_mac = mac;
4240 if (!ses_mac)
4241 ses_mac = mac;
4242 if (!grp_mac)
4243 grp_mac = mac;
4244
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004245 wpas_p2ps_get_feat_cap_str(feat_cap_str, sizeof(feat_cap_str),
4246 feat_cap, feat_cap_len);
4247
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004248 if (prov_start) {
4249 if (session_info == NULL) {
4250 wpa_msg_global(wpa_s, MSG_INFO,
4251 P2P_EVENT_P2PS_PROVISION_START MACSTR
4252 " adv_id=%x conncap=%x"
4253 " adv_mac=" MACSTR
4254 " session=%x mac=" MACSTR
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004255 " dev_passwd_id=%d%s",
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004256 MAC2STR(dev), adv_id, conncap,
4257 MAC2STR(adv_mac),
4258 ses_id, MAC2STR(ses_mac),
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004259 passwd_id, feat_cap_str);
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004260 } else {
4261 wpa_msg_global(wpa_s, MSG_INFO,
4262 P2P_EVENT_P2PS_PROVISION_START MACSTR
4263 " adv_id=%x conncap=%x"
4264 " adv_mac=" MACSTR
4265 " session=%x mac=" MACSTR
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004266 " dev_passwd_id=%d info='%s'%s",
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004267 MAC2STR(dev), adv_id, conncap,
4268 MAC2STR(adv_mac),
4269 ses_id, MAC2STR(ses_mac),
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004270 passwd_id, session_info, feat_cap_str);
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004271 }
4272 return;
4273 }
4274
4275 go_wpa_s = wpas_p2p_get_go_group(wpa_s);
4276 persistent_go = wpas_p2p_get_persistent_go(wpa_s);
4277
4278 if (status && status != P2P_SC_SUCCESS_DEFERRED) {
4279 if (go_wpa_s && !p2p_group_go_member_count(wpa_s))
4280 wpas_p2p_group_remove(wpa_s, go_wpa_s->ifname);
4281
4282 if (persistent_go && !persistent_go->num_p2p_clients) {
4283 /* remove empty persistent GO */
Hai Shalom74f70d42019-02-11 14:42:39 -08004284 wpa_dbg(wpa_s, MSG_DEBUG,
4285 "P2P: Remove empty persistent group id=%d",
4286 persistent_go->id);
4287 wpas_notify_persistent_group_removed(wpa_s,
4288 persistent_go);
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004289 wpa_config_remove_network(wpa_s->conf,
4290 persistent_go->id);
4291 }
4292
4293 wpa_msg_global(wpa_s, MSG_INFO,
4294 P2P_EVENT_P2PS_PROVISION_DONE MACSTR
4295 " status=%d"
4296 " adv_id=%x adv_mac=" MACSTR
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004297 " session=%x mac=" MACSTR "%s",
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004298 MAC2STR(dev), status,
4299 adv_id, MAC2STR(adv_mac),
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004300 ses_id, MAC2STR(ses_mac), feat_cap_str);
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004301 return;
4302 }
4303
4304 /* Clean up stale persistent groups with this device */
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004305 if (persist_ssid && persist_ssid_size)
4306 s = wpas_p2p_get_persistent(wpa_s, dev, persist_ssid,
4307 persist_ssid_size);
4308
4309 if (persist_ssid && s && s->mode != WPAS_MODE_P2P_GO &&
4310 is_zero_ether_addr(grp_mac)) {
4311 wpa_dbg(wpa_s, MSG_ERROR,
4312 "P2P: Peer device is a GO in a persistent group, but it did not provide the intended MAC address");
4313 return;
4314 }
4315
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004316 for (;;) {
4317 stale = wpas_p2p_get_persistent(wpa_s, dev, NULL, 0);
4318 if (!stale)
4319 break;
4320
4321 if (s && s->ssid_len == stale->ssid_len &&
4322 os_memcmp(stale->bssid, s->bssid, ETH_ALEN) == 0 &&
4323 os_memcmp(stale->ssid, s->ssid, s->ssid_len) == 0)
4324 break;
4325
4326 /* Remove stale persistent group */
4327 if (stale->mode != WPAS_MODE_P2P_GO ||
4328 stale->num_p2p_clients <= 1) {
Hai Shalom74f70d42019-02-11 14:42:39 -08004329 wpa_dbg(wpa_s, MSG_DEBUG,
4330 "P2P: Remove stale persistent group id=%d",
4331 stale->id);
4332 wpas_notify_persistent_group_removed(wpa_s, stale);
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004333 wpa_config_remove_network(wpa_s->conf, stale->id);
4334 } else {
4335 size_t i;
4336
4337 for (i = 0; i < stale->num_p2p_clients; i++) {
4338 if (os_memcmp(stale->p2p_client_list +
4339 i * ETH_ALEN,
4340 dev, ETH_ALEN) == 0) {
4341 os_memmove(stale->p2p_client_list +
4342 i * ETH_ALEN,
4343 stale->p2p_client_list +
4344 (i + 1) * ETH_ALEN,
4345 (stale->num_p2p_clients -
4346 i - 1) * ETH_ALEN);
4347 break;
4348 }
4349 }
4350 stale->num_p2p_clients--;
4351 }
4352 save_config = 1;
4353 }
4354
4355 if (save_config)
4356 p2p_config_write(wpa_s);
4357
4358 if (s) {
4359 if (go_wpa_s && !p2p_group_go_member_count(wpa_s))
4360 wpas_p2p_group_remove(wpa_s, go_wpa_s->ifname);
4361
4362 if (persistent_go && s != persistent_go &&
4363 !persistent_go->num_p2p_clients) {
4364 /* remove empty persistent GO */
Hai Shalom74f70d42019-02-11 14:42:39 -08004365 wpa_dbg(wpa_s, MSG_DEBUG,
4366 "P2P: Remove empty persistent group id=%d",
4367 persistent_go->id);
4368 wpas_notify_persistent_group_removed(wpa_s,
4369 persistent_go);
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004370 wpa_config_remove_network(wpa_s->conf,
4371 persistent_go->id);
4372 /* Save config */
4373 }
4374
4375 wpa_msg_global(wpa_s, MSG_INFO,
4376 P2P_EVENT_P2PS_PROVISION_DONE MACSTR
4377 " status=%d"
4378 " adv_id=%x adv_mac=" MACSTR
4379 " session=%x mac=" MACSTR
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004380 " persist=%d%s",
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004381 MAC2STR(dev), status,
4382 adv_id, MAC2STR(adv_mac),
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004383 ses_id, MAC2STR(ses_mac), s->id, feat_cap_str);
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004384 return;
4385 }
4386
Hai Shalom5f92bc92019-04-18 11:54:11 -07004387 wpa_s->global->pending_p2ps_group = 0;
4388 wpa_s->global->pending_p2ps_group_freq = 0;
4389
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004390 if (conncap == P2PS_SETUP_GROUP_OWNER) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004391 /*
4392 * We need to copy the interface name. Simply saving a
4393 * pointer isn't enough, since if we use pending_interface_name
4394 * it will be overwritten when the group is added.
4395 */
4396 char go_ifname[100];
4397
4398 go_ifname[0] = '\0';
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004399 if (!go_wpa_s) {
Hai Shalom5f92bc92019-04-18 11:54:11 -07004400 if (!response_done) {
4401 wpa_s->global->pending_p2ps_group = 1;
4402 wpa_s->global->pending_p2ps_group_freq = freq;
4403 }
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004404
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004405 if (!wpas_p2p_create_iface(wpa_s))
4406 os_memcpy(go_ifname, wpa_s->ifname,
4407 sizeof(go_ifname));
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004408 else if (wpa_s->pending_interface_name[0])
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004409 os_memcpy(go_ifname,
4410 wpa_s->pending_interface_name,
4411 sizeof(go_ifname));
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004412
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004413 if (!go_ifname[0]) {
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004414 wpas_p2ps_prov_complete(
4415 wpa_s, P2P_SC_FAIL_UNKNOWN_GROUP,
4416 dev, adv_mac, ses_mac,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004417 grp_mac, adv_id, ses_id, 0, 0,
Dmitry Shmidtde47be72016-01-07 12:52:55 -08004418 NULL, 0, 0, 0, NULL, NULL, 0, 0,
4419 NULL, 0);
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004420 return;
4421 }
4422
4423 /* If PD Resp complete, start up the GO */
4424 if (response_done && persistent_go) {
4425 wpas_p2p_group_add_persistent(
4426 wpa_s, persistent_go,
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08004427 0, 0, freq, 0, 0, 0, 0, 0, 0, NULL,
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004428 persistent_go->mode ==
4429 WPAS_MODE_P2P_GO ?
4430 P2P_MAX_INITIAL_CONN_WAIT_GO_REINVOKE :
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004431 0, 0);
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004432 } else if (response_done) {
Hai Shalom74f70d42019-02-11 14:42:39 -08004433 wpas_p2p_group_add(wpa_s, 1, freq,
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08004434 0, 0, 0, 0, 0, 0);
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004435 }
4436
4437 if (passwd_id == DEV_PW_P2PS_DEFAULT) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004438 os_memcpy(wpa_s->p2ps_join_addr, grp_mac,
4439 ETH_ALEN);
4440 wpa_s->p2ps_method_config_any = 1;
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004441 }
4442 } else if (passwd_id == DEV_PW_P2PS_DEFAULT) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004443 os_memcpy(go_ifname, go_wpa_s->ifname,
4444 sizeof(go_ifname));
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004445
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004446 if (is_zero_ether_addr(grp_mac)) {
4447 wpa_dbg(go_wpa_s, MSG_DEBUG,
4448 "P2P: Setting PIN-1 for ANY");
4449 wpa_supplicant_ap_wps_pin(go_wpa_s, NULL,
4450 "12345670", NULL, 0,
4451 0);
4452 } else {
4453 wpa_dbg(go_wpa_s, MSG_DEBUG,
4454 "P2P: Setting PIN-1 for " MACSTR,
4455 MAC2STR(grp_mac));
4456 wpa_supplicant_ap_wps_pin(go_wpa_s, grp_mac,
4457 "12345670", NULL, 0,
4458 0);
4459 }
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004460
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004461 os_memcpy(wpa_s->p2ps_join_addr, grp_mac, ETH_ALEN);
4462 wpa_s->p2ps_method_config_any = 1;
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004463 }
4464
4465 wpa_msg_global(wpa_s, MSG_INFO,
4466 P2P_EVENT_P2PS_PROVISION_DONE MACSTR
4467 " status=%d conncap=%x"
4468 " adv_id=%x adv_mac=" MACSTR
4469 " session=%x mac=" MACSTR
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004470 " dev_passwd_id=%d go=%s%s",
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004471 MAC2STR(dev), status, conncap,
4472 adv_id, MAC2STR(adv_mac),
4473 ses_id, MAC2STR(ses_mac),
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004474 passwd_id, go_ifname, feat_cap_str);
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004475 return;
4476 }
4477
4478 if (go_wpa_s && !p2p_group_go_member_count(wpa_s))
4479 wpas_p2p_group_remove(wpa_s, go_wpa_s->ifname);
4480
4481 if (persistent_go && !persistent_go->num_p2p_clients) {
4482 /* remove empty persistent GO */
Hai Shalom74f70d42019-02-11 14:42:39 -08004483 wpa_dbg(wpa_s, MSG_DEBUG,
4484 "P2P: Remove empty persistent group id=%d",
4485 persistent_go->id);
4486 wpas_notify_persistent_group_removed(wpa_s, persistent_go);
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004487 wpa_config_remove_network(wpa_s->conf, persistent_go->id);
4488 }
4489
4490 if (conncap == P2PS_SETUP_CLIENT) {
Dmitry Shmidtde47be72016-01-07 12:52:55 -08004491 char ssid_hex[32 * 2 + 1];
4492
4493 if (group_ssid)
4494 wpa_snprintf_hex(ssid_hex, sizeof(ssid_hex),
4495 group_ssid, group_ssid_len);
4496 else
4497 ssid_hex[0] = '\0';
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004498 wpa_msg_global(wpa_s, MSG_INFO,
4499 P2P_EVENT_P2PS_PROVISION_DONE MACSTR
4500 " status=%d conncap=%x"
4501 " adv_id=%x adv_mac=" MACSTR
4502 " session=%x mac=" MACSTR
Dmitry Shmidtde47be72016-01-07 12:52:55 -08004503 " dev_passwd_id=%d join=" MACSTR "%s%s%s",
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004504 MAC2STR(dev), status, conncap,
4505 adv_id, MAC2STR(adv_mac),
4506 ses_id, MAC2STR(ses_mac),
Dmitry Shmidtde47be72016-01-07 12:52:55 -08004507 passwd_id, MAC2STR(grp_mac), feat_cap_str,
4508 group_ssid ? " group_ssid=" : "", ssid_hex);
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004509 } else {
4510 wpa_msg_global(wpa_s, MSG_INFO,
4511 P2P_EVENT_P2PS_PROVISION_DONE MACSTR
4512 " status=%d conncap=%x"
4513 " adv_id=%x adv_mac=" MACSTR
4514 " session=%x mac=" MACSTR
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004515 " dev_passwd_id=%d%s",
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004516 MAC2STR(dev), status, conncap,
4517 adv_id, MAC2STR(adv_mac),
4518 ses_id, MAC2STR(ses_mac),
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004519 passwd_id, feat_cap_str);
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004520 }
4521}
4522
4523
Dmitry Shmidt5a1480c2014-05-12 09:46:02 -07004524static int _wpas_p2p_in_progress(void *ctx)
4525{
4526 struct wpa_supplicant *wpa_s = ctx;
4527 return wpas_p2p_in_progress(wpa_s);
4528}
4529
4530
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004531static int wpas_prov_disc_resp_cb(void *ctx)
4532{
4533 struct wpa_supplicant *wpa_s = ctx;
4534 struct wpa_ssid *persistent_go;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004535 unsigned int freq;
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004536
4537 if (!wpa_s->global->pending_p2ps_group)
4538 return 0;
4539
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004540 freq = wpa_s->global->pending_p2ps_group_freq;
4541 wpa_s->global->pending_p2ps_group_freq = 0;
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004542 wpa_s->global->pending_p2ps_group = 0;
4543
4544 if (wpas_p2p_get_go_group(wpa_s))
4545 return 0;
4546 persistent_go = wpas_p2p_get_persistent_go(wpa_s);
4547
4548 if (persistent_go) {
4549 wpas_p2p_group_add_persistent(
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08004550 wpa_s, persistent_go, 0, 0, 0, 0, 0, 0, 0, 0, 0,
4551 NULL,
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004552 persistent_go->mode == WPAS_MODE_P2P_GO ?
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004553 P2P_MAX_INITIAL_CONN_WAIT_GO_REINVOKE : 0, 0);
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004554 } else {
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08004555 wpas_p2p_group_add(wpa_s, 1, freq, 0, 0, 0, 0, 0, 0);
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004556 }
4557
4558 return 1;
4559}
4560
4561
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004562static int wpas_p2p_get_pref_freq_list(void *ctx, int go,
4563 unsigned int *len,
4564 unsigned int *freq_list)
4565{
4566 struct wpa_supplicant *wpa_s = ctx;
4567
4568 return wpa_drv_get_pref_freq_list(wpa_s, go ? WPA_IF_P2P_GO :
4569 WPA_IF_P2P_CLIENT, len, freq_list);
4570}
4571
Jimmy Chenf887c7b2018-11-13 15:19:57 +08004572int wpas_p2p_mac_setup(struct wpa_supplicant *wpa_s)
4573{
4574 u8 addr[ETH_ALEN] = {0};
4575
4576 if (wpa_s->conf->p2p_device_random_mac_addr == 0)
4577 return 0;
4578
4579 if (wpa_s->conf->ssid == NULL) {
4580 if (random_mac_addr(addr) < 0) {
4581 wpa_msg(wpa_s, MSG_INFO,
4582 "Failed to generate random MAC address");
4583 return -EINVAL;
4584 }
4585
Hai Shalom39ba6fc2019-01-22 12:40:38 -08004586 /* Store generated MAC address. */
4587 os_memcpy(wpa_s->conf->p2p_device_persistent_mac_addr, addr,
4588 ETH_ALEN);
Jimmy Chenf887c7b2018-11-13 15:19:57 +08004589 } else {
Hai Shalom39ba6fc2019-01-22 12:40:38 -08004590 /* If there are existing saved groups, restore last MAC address.
4591 * if there is no last used MAC address, the last one is
4592 * factory MAC. */
4593 if (is_zero_ether_addr(
4594 wpa_s->conf->p2p_device_persistent_mac_addr))
Jimmy Chenf887c7b2018-11-13 15:19:57 +08004595 return 0;
Hai Shalom39ba6fc2019-01-22 12:40:38 -08004596 os_memcpy(addr, wpa_s->conf->p2p_device_persistent_mac_addr,
4597 ETH_ALEN);
Jimmy Chenf887c7b2018-11-13 15:19:57 +08004598 wpa_msg(wpa_s, MSG_DEBUG, "Restore last used MAC address.");
4599 }
4600
4601 if (wpa_drv_set_mac_addr(wpa_s, addr) < 0) {
4602 wpa_msg(wpa_s, MSG_INFO,
4603 "Failed to set random MAC address");
4604 return -EINVAL;
4605 }
4606
4607 if (wpa_supplicant_update_mac_addr(wpa_s) < 0) {
4608 wpa_msg(wpa_s, MSG_INFO,
4609 "Could not update MAC address information");
4610 return -EINVAL;
4611 }
4612
4613 wpa_msg(wpa_s, MSG_DEBUG, "Using random MAC address " MACSTR,
4614 MAC2STR(addr));
4615
4616 return 0;
4617}
4618
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004619/**
4620 * wpas_p2p_init - Initialize P2P module for %wpa_supplicant
4621 * @global: Pointer to global data from wpa_supplicant_init()
4622 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
4623 * Returns: 0 on success, -1 on failure
4624 */
4625int wpas_p2p_init(struct wpa_global *global, struct wpa_supplicant *wpa_s)
4626{
4627 struct p2p_config p2p;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004628 int i;
4629
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07004630 if (wpa_s->conf->p2p_disabled)
4631 return 0;
4632
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004633 if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_CAPABLE))
4634 return 0;
4635
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004636 if (global->p2p)
4637 return 0;
4638
Jimmy Chenf887c7b2018-11-13 15:19:57 +08004639 if (wpas_p2p_mac_setup(wpa_s) < 0) {
4640 wpa_msg(wpa_s, MSG_ERROR,
4641 "Failed to initialize P2P random MAC address.");
4642 return -1;
4643 }
4644
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004645 os_memset(&p2p, 0, sizeof(p2p));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004646 p2p.cb_ctx = wpa_s;
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07004647 p2p.debug_print = wpas_p2p_debug_print;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004648 p2p.p2p_scan = wpas_p2p_scan;
4649 p2p.send_action = wpas_send_action;
4650 p2p.send_action_done = wpas_send_action_done;
4651 p2p.go_neg_completed = wpas_go_neg_completed;
4652 p2p.go_neg_req_rx = wpas_go_neg_req_rx;
4653 p2p.dev_found = wpas_dev_found;
4654 p2p.dev_lost = wpas_dev_lost;
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07004655 p2p.find_stopped = wpas_find_stopped;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004656 p2p.start_listen = wpas_start_listen;
4657 p2p.stop_listen = wpas_stop_listen;
4658 p2p.send_probe_resp = wpas_send_probe_resp;
4659 p2p.sd_request = wpas_sd_request;
4660 p2p.sd_response = wpas_sd_response;
4661 p2p.prov_disc_req = wpas_prov_disc_req;
4662 p2p.prov_disc_resp = wpas_prov_disc_resp;
Jouni Malinen75ecf522011-06-27 15:19:46 -07004663 p2p.prov_disc_fail = wpas_prov_disc_fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004664 p2p.invitation_process = wpas_invitation_process;
4665 p2p.invitation_received = wpas_invitation_received;
4666 p2p.invitation_result = wpas_invitation_result;
4667 p2p.get_noa = wpas_get_noa;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08004668 p2p.go_connected = wpas_go_connected;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08004669 p2p.presence_resp = wpas_presence_resp;
Dmitry Shmidt18463232014-01-24 12:29:41 -08004670 p2p.is_concurrent_session_active = wpas_is_concurrent_session_active;
Dmitry Shmidt5a1480c2014-05-12 09:46:02 -07004671 p2p.is_p2p_in_progress = _wpas_p2p_in_progress;
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004672 p2p.get_persistent_group = wpas_get_persistent_group;
4673 p2p.get_go_info = wpas_get_go_info;
4674 p2p.remove_stale_groups = wpas_remove_stale_groups;
4675 p2p.p2ps_prov_complete = wpas_p2ps_prov_complete;
4676 p2p.prov_disc_resp_cb = wpas_prov_disc_resp_cb;
4677 p2p.p2ps_group_capability = p2ps_group_capability;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004678 p2p.get_pref_freq_list = wpas_p2p_get_pref_freq_list;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004679
4680 os_memcpy(wpa_s->global->p2p_dev_addr, wpa_s->own_addr, ETH_ALEN);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004681 os_memcpy(p2p.dev_addr, wpa_s->global->p2p_dev_addr, ETH_ALEN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004682 p2p.dev_name = wpa_s->conf->device_name;
4683 p2p.manufacturer = wpa_s->conf->manufacturer;
4684 p2p.model_name = wpa_s->conf->model_name;
4685 p2p.model_number = wpa_s->conf->model_number;
4686 p2p.serial_number = wpa_s->conf->serial_number;
4687 if (wpa_s->wps) {
4688 os_memcpy(p2p.uuid, wpa_s->wps->uuid, 16);
4689 p2p.config_methods = wpa_s->wps->config_methods;
4690 }
4691
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07004692 if (wpas_p2p_setup_channels(wpa_s, &p2p.channels, &p2p.cli_channels)) {
4693 wpa_printf(MSG_ERROR,
4694 "P2P: Failed to configure supported channel list");
4695 return -1;
4696 }
4697
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004698 if (wpa_s->conf->p2p_listen_reg_class &&
4699 wpa_s->conf->p2p_listen_channel) {
4700 p2p.reg_class = wpa_s->conf->p2p_listen_reg_class;
4701 p2p.channel = wpa_s->conf->p2p_listen_channel;
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07004702 p2p.channel_forced = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004703 } else {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004704 /*
4705 * Pick one of the social channels randomly as the listen
4706 * channel.
4707 */
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07004708 if (p2p_config_get_random_social(&p2p, &p2p.reg_class,
Hai Shalom74f70d42019-02-11 14:42:39 -08004709 &p2p.channel,
4710 &global->p2p_go_avoid_freq,
4711 &global->p2p_disallow_freq) !=
4712 0) {
Dmitry Shmidt1d755d02015-04-28 10:34:29 -07004713 wpa_printf(MSG_INFO,
4714 "P2P: No social channels supported by the driver - do not enable P2P");
4715 return 0;
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07004716 }
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07004717 p2p.channel_forced = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004718 }
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07004719 wpa_printf(MSG_DEBUG, "P2P: Own listen channel: %d:%d",
4720 p2p.reg_class, p2p.channel);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004721
4722 if (wpa_s->conf->p2p_oper_reg_class &&
4723 wpa_s->conf->p2p_oper_channel) {
4724 p2p.op_reg_class = wpa_s->conf->p2p_oper_reg_class;
4725 p2p.op_channel = wpa_s->conf->p2p_oper_channel;
4726 p2p.cfg_op_channel = 1;
4727 wpa_printf(MSG_DEBUG, "P2P: Configured operating channel: "
4728 "%d:%d", p2p.op_reg_class, p2p.op_channel);
4729
4730 } else {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004731 /*
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07004732 * Use random operation channel from 2.4 GHz band social
4733 * channels (1, 6, 11) or band 60 GHz social channel (2) if no
4734 * other preference is indicated.
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004735 */
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07004736 if (p2p_config_get_random_social(&p2p, &p2p.op_reg_class,
Hai Shalom74f70d42019-02-11 14:42:39 -08004737 &p2p.op_channel, NULL,
4738 NULL) != 0) {
4739 wpa_printf(MSG_INFO,
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07004740 "P2P: Failed to select random social channel as operation channel");
Hai Shalom74f70d42019-02-11 14:42:39 -08004741 p2p.op_reg_class = 0;
4742 p2p.op_channel = 0;
4743 /* This will be overridden during group setup in
4744 * p2p_prepare_channel(), so allow setup to continue. */
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07004745 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004746 p2p.cfg_op_channel = 0;
4747 wpa_printf(MSG_DEBUG, "P2P: Random operating channel: "
4748 "%d:%d", p2p.op_reg_class, p2p.op_channel);
4749 }
Dmitry Shmidt44c95782013-05-17 09:51:35 -07004750
4751 if (wpa_s->conf->p2p_pref_chan && wpa_s->conf->num_p2p_pref_chan) {
4752 p2p.pref_chan = wpa_s->conf->p2p_pref_chan;
4753 p2p.num_pref_chan = wpa_s->conf->num_p2p_pref_chan;
4754 }
4755
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004756 if (wpa_s->conf->country[0] && wpa_s->conf->country[1]) {
4757 os_memcpy(p2p.country, wpa_s->conf->country, 2);
4758 p2p.country[2] = 0x04;
4759 } else
4760 os_memcpy(p2p.country, "XX\x04", 3);
4761
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004762 os_memcpy(p2p.pri_dev_type, wpa_s->conf->device_type,
4763 WPS_DEV_TYPE_LEN);
4764
4765 p2p.num_sec_dev_types = wpa_s->conf->num_sec_device_types;
4766 os_memcpy(p2p.sec_dev_type, wpa_s->conf->sec_device_type,
4767 p2p.num_sec_dev_types * WPS_DEV_TYPE_LEN);
4768
4769 p2p.concurrent_operations = !!(wpa_s->drv_flags &
4770 WPA_DRIVER_FLAGS_P2P_CONCURRENT);
4771
4772 p2p.max_peers = 100;
4773
4774 if (wpa_s->conf->p2p_ssid_postfix) {
4775 p2p.ssid_postfix_len =
4776 os_strlen(wpa_s->conf->p2p_ssid_postfix);
4777 if (p2p.ssid_postfix_len > sizeof(p2p.ssid_postfix))
4778 p2p.ssid_postfix_len = sizeof(p2p.ssid_postfix);
4779 os_memcpy(p2p.ssid_postfix, wpa_s->conf->p2p_ssid_postfix,
4780 p2p.ssid_postfix_len);
4781 }
4782
4783 p2p.p2p_intra_bss = wpa_s->conf->p2p_intra_bss;
4784
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08004785 p2p.max_listen = wpa_s->max_remain_on_chan;
4786
Dmitry Shmidt2271d3f2014-06-23 12:16:31 -07004787 if (wpa_s->conf->p2p_passphrase_len >= 8 &&
4788 wpa_s->conf->p2p_passphrase_len <= 63)
4789 p2p.passphrase_len = wpa_s->conf->p2p_passphrase_len;
4790 else
4791 p2p.passphrase_len = 8;
4792
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004793 global->p2p = p2p_init(&p2p);
4794 if (global->p2p == NULL)
4795 return -1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07004796 global->p2p_init_wpa_s = wpa_s;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004797
4798 for (i = 0; i < MAX_WPS_VENDOR_EXT; i++) {
4799 if (wpa_s->conf->wps_vendor_ext[i] == NULL)
4800 continue;
4801 p2p_add_wps_vendor_extension(
4802 global->p2p, wpa_s->conf->wps_vendor_ext[i]);
4803 }
4804
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004805 p2p_set_no_go_freq(global->p2p, &wpa_s->conf->p2p_no_go_freq);
4806
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004807 return 0;
4808}
4809
4810
4811/**
4812 * wpas_p2p_deinit - Deinitialize per-interface P2P data
4813 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
4814 *
4815 * This function deinitialize per-interface P2P data.
4816 */
4817void wpas_p2p_deinit(struct wpa_supplicant *wpa_s)
4818{
4819 if (wpa_s->driver && wpa_s->drv_priv)
4820 wpa_drv_probe_req_report(wpa_s, 0);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004821
4822 if (wpa_s->go_params) {
4823 /* Clear any stored provisioning info */
4824 p2p_clear_provisioning_info(
4825 wpa_s->global->p2p,
Dmitry Shmidt04949592012-07-19 12:16:46 -07004826 wpa_s->go_params->peer_device_addr);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004827 }
4828
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004829 os_free(wpa_s->go_params);
4830 wpa_s->go_params = NULL;
Dmitry Shmidt684785c2014-05-12 13:34:29 -07004831 eloop_cancel_timeout(wpas_p2p_psk_failure_removal, wpa_s, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004832 eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s, NULL);
4833 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
Hai Shalomfdcde762020-04-02 11:19:20 -07004834 wpa_s->global->p2p_long_listen = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004835 eloop_cancel_timeout(wpas_p2p_long_listen_timeout, wpa_s, NULL);
4836 eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL);
4837 wpas_p2p_remove_pending_group_interface(wpa_s);
Dmitry Shmidt04f534e2013-12-09 15:50:16 -08004838 eloop_cancel_timeout(wpas_p2p_group_freq_conflict, wpa_s, NULL);
Hai Shalom899fcc72020-10-19 14:38:18 -07004839 eloop_cancel_timeout(wpas_p2p_reconsider_moving_go, wpa_s, NULL);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08004840 wpas_p2p_listen_work_done(wpa_s);
4841 if (wpa_s->p2p_send_action_work) {
4842 os_free(wpa_s->p2p_send_action_work->ctx);
4843 radio_work_done(wpa_s->p2p_send_action_work);
4844 wpa_s->p2p_send_action_work = NULL;
4845 }
4846 eloop_cancel_timeout(wpas_p2p_send_action_work_timeout, wpa_s, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004847
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08004848 wpabuf_free(wpa_s->p2p_oob_dev_pw);
4849 wpa_s->p2p_oob_dev_pw = NULL;
4850
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004851 os_free(wpa_s->p2p_group_common_freqs);
4852 wpa_s->p2p_group_common_freqs = NULL;
4853 wpa_s->p2p_group_common_freqs_num = 0;
4854
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004855 /* TODO: remove group interface from the driver if this wpa_s instance
4856 * is on top of a P2P group interface */
4857}
4858
4859
4860/**
4861 * wpas_p2p_deinit_global - Deinitialize global P2P module
4862 * @global: Pointer to global data from wpa_supplicant_init()
4863 *
4864 * This function deinitializes the global (per device) P2P module.
4865 */
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07004866static void wpas_p2p_deinit_global(struct wpa_global *global)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004867{
4868 struct wpa_supplicant *wpa_s, *tmp;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004869
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004870 wpa_s = global->ifaces;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004871
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07004872 wpas_p2p_service_flush(global->p2p_init_wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004873
4874 /* Remove remaining P2P group interfaces */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004875 while (wpa_s && wpa_s->p2p_group_interface != NOT_P2P_GROUP_INTERFACE)
4876 wpa_s = wpa_s->next;
4877 while (wpa_s) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004878 tmp = global->ifaces;
4879 while (tmp &&
4880 (tmp == wpa_s ||
4881 tmp->p2p_group_interface == NOT_P2P_GROUP_INTERFACE)) {
4882 tmp = tmp->next;
4883 }
4884 if (tmp == NULL)
4885 break;
Dmitry Shmidt04949592012-07-19 12:16:46 -07004886 /* Disconnect from the P2P group and deinit the interface */
4887 wpas_p2p_disconnect(tmp);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004888 }
4889
4890 /*
4891 * Deinit GO data on any possibly remaining interface (if main
4892 * interface is used as GO).
4893 */
4894 for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
4895 if (wpa_s->ap_iface)
4896 wpas_p2p_group_deinit(wpa_s);
4897 }
4898
4899 p2p_deinit(global->p2p);
4900 global->p2p = NULL;
Dmitry Shmidt04949592012-07-19 12:16:46 -07004901 global->p2p_init_wpa_s = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004902}
4903
4904
4905static int wpas_p2p_create_iface(struct wpa_supplicant *wpa_s)
4906{
Dmitry Shmidt9c175262016-03-03 10:20:07 -08004907 if (wpa_s->conf->p2p_no_group_iface)
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08004908 return 0; /* separate interface disabled per configuration */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004909 if (wpa_s->drv_flags &
4910 (WPA_DRIVER_FLAGS_P2P_DEDICATED_INTERFACE |
4911 WPA_DRIVER_FLAGS_P2P_MGMT_AND_NON_P2P))
4912 return 1; /* P2P group requires a new interface in every case
4913 */
4914 if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_CONCURRENT))
4915 return 0; /* driver does not support concurrent operations */
4916 if (wpa_s->global->ifaces->next)
4917 return 1; /* more that one interface already in use */
4918 if (wpa_s->wpa_state >= WPA_AUTHENTICATING)
4919 return 1; /* this interface is already in use */
4920 return 0;
4921}
4922
4923
4924static int wpas_p2p_start_go_neg(struct wpa_supplicant *wpa_s,
4925 const u8 *peer_addr,
4926 enum p2p_wps_method wps_method,
4927 int go_intent, const u8 *own_interface_addr,
Dmitry Shmidt04949592012-07-19 12:16:46 -07004928 unsigned int force_freq, int persistent_group,
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08004929 struct wpa_ssid *ssid, unsigned int pref_freq)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004930{
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004931 if (persistent_group && wpa_s->conf->persistent_reconnect)
4932 persistent_group = 2;
4933
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004934 /*
4935 * Increase GO config timeout if HT40 is used since it takes some time
4936 * to scan channels for coex purposes before the BSS can be started.
4937 */
4938 p2p_set_config_timeout(wpa_s->global->p2p,
4939 wpa_s->p2p_go_ht40 ? 255 : 100, 20);
4940
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004941 return p2p_connect(wpa_s->global->p2p, peer_addr, wps_method,
4942 go_intent, own_interface_addr, force_freq,
Dmitry Shmidt04949592012-07-19 12:16:46 -07004943 persistent_group, ssid ? ssid->ssid : NULL,
4944 ssid ? ssid->ssid_len : 0,
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08004945 wpa_s->p2p_pd_before_go_neg, pref_freq,
4946 wps_method == WPS_NFC ? wpa_s->p2p_oob_dev_pw_id :
4947 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004948}
4949
4950
4951static int wpas_p2p_auth_go_neg(struct wpa_supplicant *wpa_s,
4952 const u8 *peer_addr,
4953 enum p2p_wps_method wps_method,
4954 int go_intent, const u8 *own_interface_addr,
Dmitry Shmidt04949592012-07-19 12:16:46 -07004955 unsigned int force_freq, int persistent_group,
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08004956 struct wpa_ssid *ssid, unsigned int pref_freq)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004957{
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004958 if (persistent_group && wpa_s->conf->persistent_reconnect)
4959 persistent_group = 2;
4960
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004961 return p2p_authorize(wpa_s->global->p2p, peer_addr, wps_method,
4962 go_intent, own_interface_addr, force_freq,
Dmitry Shmidt04949592012-07-19 12:16:46 -07004963 persistent_group, ssid ? ssid->ssid : NULL,
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08004964 ssid ? ssid->ssid_len : 0, pref_freq,
4965 wps_method == WPS_NFC ? wpa_s->p2p_oob_dev_pw_id :
4966 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004967}
4968
4969
4970static void wpas_p2p_check_join_scan_limit(struct wpa_supplicant *wpa_s)
4971{
4972 wpa_s->p2p_join_scan_count++;
4973 wpa_printf(MSG_DEBUG, "P2P: Join scan attempt %d",
4974 wpa_s->p2p_join_scan_count);
4975 if (wpa_s->p2p_join_scan_count > P2P_MAX_JOIN_SCAN_ATTEMPTS) {
4976 wpa_printf(MSG_DEBUG, "P2P: Failed to find GO " MACSTR
4977 " for join operationg - stop join attempt",
4978 MAC2STR(wpa_s->pending_join_iface_addr));
4979 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
Dmitry Shmidt04949592012-07-19 12:16:46 -07004980 if (wpa_s->p2p_auto_pd) {
4981 wpa_s->p2p_auto_pd = 0;
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07004982 wpa_msg_global(wpa_s, MSG_INFO,
4983 P2P_EVENT_PROV_DISC_FAILURE
4984 " p2p_dev_addr=" MACSTR " status=N/A",
4985 MAC2STR(wpa_s->pending_join_dev_addr));
Dmitry Shmidt04949592012-07-19 12:16:46 -07004986 return;
4987 }
Jimmy Chenb7b3f4d2020-09-02 16:50:11 +08004988 if (wpa_s->p2p_fallback_to_go_neg) {
4989 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Join operating "
4990 "failed - fall back to GO Negotiation");
4991 wpa_msg_global(wpa_s->p2pdev, MSG_INFO,
4992 P2P_EVENT_FALLBACK_TO_GO_NEG
4993 "reason=join-failed");
4994 wpas_p2p_fallback_to_go_neg(wpa_s, 0);
4995 return;
4996 }
Dmitry Shmidt9c175262016-03-03 10:20:07 -08004997 wpa_msg_global(wpa_s->p2pdev, MSG_INFO,
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07004998 P2P_EVENT_GROUP_FORMATION_FAILURE);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004999 wpas_notify_p2p_group_formation_failure(wpa_s, "");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005000 }
5001}
5002
5003
Dmitry Shmidt04949592012-07-19 12:16:46 -07005004static int wpas_check_freq_conflict(struct wpa_supplicant *wpa_s, int freq)
5005{
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07005006 int res;
5007 unsigned int num, i;
5008 struct wpa_used_freq_data *freqs;
Dmitry Shmidt04949592012-07-19 12:16:46 -07005009
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07005010 if (wpas_p2p_num_unused_channels(wpa_s) > 0) {
5011 /* Multiple channels are supported and not all are in use */
Dmitry Shmidt04949592012-07-19 12:16:46 -07005012 return 0;
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07005013 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07005014
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07005015 freqs = os_calloc(wpa_s->num_multichan_concurrent,
5016 sizeof(struct wpa_used_freq_data));
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07005017 if (!freqs)
5018 return 1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07005019
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07005020 num = wpas_p2p_valid_oper_freqs(wpa_s, freqs,
5021 wpa_s->num_multichan_concurrent);
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07005022
5023 for (i = 0; i < num; i++) {
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07005024 if (freqs[i].freq == freq) {
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07005025 wpa_printf(MSG_DEBUG, "P2P: Frequency %d MHz in use by another virtual interface and can be used",
5026 freq);
5027 res = 0;
5028 goto exit_free;
Dmitry Shmidt04949592012-07-19 12:16:46 -07005029 }
5030 }
5031
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07005032 wpa_printf(MSG_DEBUG, "P2P: No valid operating frequencies");
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07005033 res = 1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07005034
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07005035exit_free:
5036 os_free(freqs);
5037 return res;
Dmitry Shmidt04949592012-07-19 12:16:46 -07005038}
5039
5040
5041static int wpas_p2p_peer_go(struct wpa_supplicant *wpa_s,
5042 const u8 *peer_dev_addr)
5043{
5044 struct wpa_bss *bss;
5045 int updated;
5046
5047 bss = wpa_bss_get_p2p_dev_addr(wpa_s, peer_dev_addr);
5048 if (bss == NULL)
5049 return -1;
5050 if (bss->last_update_idx < wpa_s->bss_update_idx) {
5051 wpa_printf(MSG_DEBUG, "P2P: Peer BSS entry not updated in the "
5052 "last scan");
5053 return 0;
5054 }
5055
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005056 updated = os_reltime_before(&wpa_s->p2p_auto_started,
5057 &bss->last_update);
Dmitry Shmidt04949592012-07-19 12:16:46 -07005058 wpa_printf(MSG_DEBUG, "P2P: Current BSS entry for peer updated at "
5059 "%ld.%06ld (%supdated in last scan)",
5060 bss->last_update.sec, bss->last_update.usec,
5061 updated ? "": "not ");
5062
5063 return updated;
5064}
5065
5066
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005067static void wpas_p2p_scan_res_join(struct wpa_supplicant *wpa_s,
5068 struct wpa_scan_results *scan_res)
5069{
Dmitry Shmidt344abd32014-01-14 13:17:00 -08005070 struct wpa_bss *bss = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005071 int freq;
5072 u8 iface_addr[ETH_ALEN];
Dmitry Shmidt04949592012-07-19 12:16:46 -07005073
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005074 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
5075
5076 if (wpa_s->global->p2p_disabled)
5077 return;
5078
Dmitry Shmidt04949592012-07-19 12:16:46 -07005079 wpa_printf(MSG_DEBUG, "P2P: Scan results received (%d BSS) for %sjoin",
5080 scan_res ? (int) scan_res->num : -1,
5081 wpa_s->p2p_auto_join ? "auto_" : "");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005082
5083 if (scan_res)
5084 wpas_p2p_scan_res_handler(wpa_s, scan_res);
5085
Dmitry Shmidt04949592012-07-19 12:16:46 -07005086 if (wpa_s->p2p_auto_pd) {
5087 int join = wpas_p2p_peer_go(wpa_s,
5088 wpa_s->pending_join_dev_addr);
5089 if (join == 0 &&
5090 wpa_s->auto_pd_scan_retry < P2P_AUTO_PD_SCAN_ATTEMPTS) {
5091 wpa_s->auto_pd_scan_retry++;
Dmitry Shmidt444d5672013-04-01 13:08:44 -07005092 bss = wpa_bss_get_bssid_latest(
5093 wpa_s, wpa_s->pending_join_dev_addr);
Dmitry Shmidt04949592012-07-19 12:16:46 -07005094 if (bss) {
5095 freq = bss->freq;
5096 wpa_printf(MSG_DEBUG, "P2P: Scan retry %d for "
5097 "the peer " MACSTR " at %d MHz",
5098 wpa_s->auto_pd_scan_retry,
5099 MAC2STR(wpa_s->
5100 pending_join_dev_addr),
5101 freq);
Dmitry Shmidt344abd32014-01-14 13:17:00 -08005102 wpas_p2p_join_scan_req(wpa_s, freq, NULL, 0);
Dmitry Shmidt04949592012-07-19 12:16:46 -07005103 return;
5104 }
5105 }
5106
5107 if (join < 0)
5108 join = 0;
5109
5110 wpa_s->p2p_auto_pd = 0;
5111 wpa_s->pending_pd_use = join ? AUTO_PD_JOIN : AUTO_PD_GO_NEG;
5112 wpa_printf(MSG_DEBUG, "P2P: Auto PD with " MACSTR " join=%d",
5113 MAC2STR(wpa_s->pending_join_dev_addr), join);
5114 if (p2p_prov_disc_req(wpa_s->global->p2p,
Dmitry Shmidt216983b2015-02-06 10:50:36 -08005115 wpa_s->pending_join_dev_addr, NULL,
Dmitry Shmidt04949592012-07-19 12:16:46 -07005116 wpa_s->pending_pd_config_methods, join,
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08005117 0, wpa_s->user_initiated_pd) < 0) {
Dmitry Shmidt04949592012-07-19 12:16:46 -07005118 wpa_s->p2p_auto_pd = 0;
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07005119 wpa_msg_global(wpa_s, MSG_INFO,
5120 P2P_EVENT_PROV_DISC_FAILURE
5121 " p2p_dev_addr=" MACSTR " status=N/A",
5122 MAC2STR(wpa_s->pending_join_dev_addr));
Dmitry Shmidt04949592012-07-19 12:16:46 -07005123 }
5124 return;
5125 }
5126
5127 if (wpa_s->p2p_auto_join) {
5128 int join = wpas_p2p_peer_go(wpa_s,
5129 wpa_s->pending_join_dev_addr);
5130 if (join < 0) {
5131 wpa_printf(MSG_DEBUG, "P2P: Peer was not found to be "
5132 "running a GO -> use GO Negotiation");
Dmitry Shmidt9c175262016-03-03 10:20:07 -08005133 wpa_msg_global(wpa_s->p2pdev, MSG_INFO,
Dmitry Shmidt7f656022015-02-25 14:36:37 -08005134 P2P_EVENT_FALLBACK_TO_GO_NEG
5135 "reason=peer-not-running-GO");
Dmitry Shmidt04949592012-07-19 12:16:46 -07005136 wpas_p2p_connect(wpa_s, wpa_s->pending_join_dev_addr,
5137 wpa_s->p2p_pin, wpa_s->p2p_wps_method,
5138 wpa_s->p2p_persistent_group, 0, 0, 0,
5139 wpa_s->p2p_go_intent,
5140 wpa_s->p2p_connect_freq,
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08005141 wpa_s->p2p_go_vht_center_freq2,
Dmitry Shmidt04949592012-07-19 12:16:46 -07005142 wpa_s->p2p_persistent_id,
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005143 wpa_s->p2p_pd_before_go_neg,
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07005144 wpa_s->p2p_go_ht40,
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08005145 wpa_s->p2p_go_vht,
Dmitry Shmidtde47be72016-01-07 12:52:55 -08005146 wpa_s->p2p_go_max_oper_chwidth,
Hai Shalom74f70d42019-02-11 14:42:39 -08005147 wpa_s->p2p_go_he,
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08005148 wpa_s->p2p_go_edmg,
Dmitry Shmidtde47be72016-01-07 12:52:55 -08005149 NULL, 0);
Dmitry Shmidt04949592012-07-19 12:16:46 -07005150 return;
5151 }
5152
5153 wpa_printf(MSG_DEBUG, "P2P: Peer was found running GO%s -> "
5154 "try to join the group", join ? "" :
5155 " in older scan");
Dmitry Shmidt7f656022015-02-25 14:36:37 -08005156 if (!join) {
Dmitry Shmidt9c175262016-03-03 10:20:07 -08005157 wpa_msg_global(wpa_s->p2pdev, MSG_INFO,
Dmitry Shmidt7f656022015-02-25 14:36:37 -08005158 P2P_EVENT_FALLBACK_TO_GO_NEG_ENABLED);
Dmitry Shmidt04949592012-07-19 12:16:46 -07005159 wpa_s->p2p_fallback_to_go_neg = 1;
Dmitry Shmidt7f656022015-02-25 14:36:37 -08005160 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07005161 }
5162
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005163 freq = p2p_get_oper_freq(wpa_s->global->p2p,
5164 wpa_s->pending_join_iface_addr);
5165 if (freq < 0 &&
5166 p2p_get_interface_addr(wpa_s->global->p2p,
5167 wpa_s->pending_join_dev_addr,
5168 iface_addr) == 0 &&
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07005169 os_memcmp(iface_addr, wpa_s->pending_join_dev_addr, ETH_ALEN) != 0
5170 && !wpa_bss_get_bssid(wpa_s, wpa_s->pending_join_iface_addr)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005171 wpa_printf(MSG_DEBUG, "P2P: Overwrite pending interface "
5172 "address for join from " MACSTR " to " MACSTR
5173 " based on newly discovered P2P peer entry",
5174 MAC2STR(wpa_s->pending_join_iface_addr),
5175 MAC2STR(iface_addr));
5176 os_memcpy(wpa_s->pending_join_iface_addr, iface_addr,
5177 ETH_ALEN);
5178
5179 freq = p2p_get_oper_freq(wpa_s->global->p2p,
5180 wpa_s->pending_join_iface_addr);
5181 }
5182 if (freq >= 0) {
5183 wpa_printf(MSG_DEBUG, "P2P: Target GO operating frequency "
5184 "from P2P peer table: %d MHz", freq);
5185 }
Dmitry Shmidt344abd32014-01-14 13:17:00 -08005186 if (wpa_s->p2p_join_ssid_len) {
5187 wpa_printf(MSG_DEBUG, "P2P: Trying to find target GO BSS entry based on BSSID "
5188 MACSTR " and SSID %s",
5189 MAC2STR(wpa_s->pending_join_iface_addr),
5190 wpa_ssid_txt(wpa_s->p2p_join_ssid,
5191 wpa_s->p2p_join_ssid_len));
5192 bss = wpa_bss_get(wpa_s, wpa_s->pending_join_iface_addr,
5193 wpa_s->p2p_join_ssid,
5194 wpa_s->p2p_join_ssid_len);
Dmitry Shmidtde47be72016-01-07 12:52:55 -08005195 } else if (!bss) {
Dmitry Shmidt344abd32014-01-14 13:17:00 -08005196 wpa_printf(MSG_DEBUG, "P2P: Trying to find target GO BSS entry based on BSSID "
5197 MACSTR, MAC2STR(wpa_s->pending_join_iface_addr));
5198 bss = wpa_bss_get_bssid_latest(wpa_s,
5199 wpa_s->pending_join_iface_addr);
5200 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005201 if (bss) {
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07005202 u8 dev_addr[ETH_ALEN];
5203
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005204 freq = bss->freq;
5205 wpa_printf(MSG_DEBUG, "P2P: Target GO operating frequency "
Dmitry Shmidt051af732013-10-22 13:52:46 -07005206 "from BSS table: %d MHz (SSID %s)", freq,
5207 wpa_ssid_txt(bss->ssid, bss->ssid_len));
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07005208 if (p2p_parse_dev_addr((const u8 *) (bss + 1), bss->ie_len,
5209 dev_addr) == 0 &&
5210 os_memcmp(wpa_s->pending_join_dev_addr,
5211 wpa_s->pending_join_iface_addr, ETH_ALEN) == 0 &&
5212 os_memcmp(dev_addr, wpa_s->pending_join_dev_addr,
5213 ETH_ALEN) != 0) {
5214 wpa_printf(MSG_DEBUG,
5215 "P2P: Update target GO device address based on BSS entry: " MACSTR " (was " MACSTR ")",
5216 MAC2STR(dev_addr),
5217 MAC2STR(wpa_s->pending_join_dev_addr));
5218 os_memcpy(wpa_s->pending_join_dev_addr, dev_addr,
5219 ETH_ALEN);
5220 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005221 }
5222 if (freq > 0) {
5223 u16 method;
5224
Dmitry Shmidt04949592012-07-19 12:16:46 -07005225 if (wpas_check_freq_conflict(wpa_s, freq) > 0) {
Dmitry Shmidt9c175262016-03-03 10:20:07 -08005226 wpa_msg_global(wpa_s->p2pdev, MSG_INFO,
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07005227 P2P_EVENT_GROUP_FORMATION_FAILURE
5228 "reason=FREQ_CONFLICT");
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005229 wpas_notify_p2p_group_formation_failure(
5230 wpa_s, "FREQ_CONFLICT");
Dmitry Shmidt04949592012-07-19 12:16:46 -07005231 return;
5232 }
5233
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005234 wpa_printf(MSG_DEBUG, "P2P: Send Provision Discovery Request "
5235 "prior to joining an existing group (GO " MACSTR
5236 " freq=%u MHz)",
5237 MAC2STR(wpa_s->pending_join_dev_addr), freq);
5238 wpa_s->pending_pd_before_join = 1;
5239
5240 switch (wpa_s->pending_join_wps_method) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005241 case WPS_PIN_DISPLAY:
5242 method = WPS_CONFIG_KEYPAD;
5243 break;
5244 case WPS_PIN_KEYPAD:
5245 method = WPS_CONFIG_DISPLAY;
5246 break;
5247 case WPS_PBC:
5248 method = WPS_CONFIG_PUSHBUTTON;
5249 break;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005250 case WPS_P2PS:
5251 method = WPS_CONFIG_P2PS;
5252 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005253 default:
5254 method = 0;
5255 break;
5256 }
5257
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005258 if ((p2p_get_provisioning_info(wpa_s->global->p2p,
5259 wpa_s->pending_join_dev_addr) ==
5260 method)) {
5261 /*
5262 * We have already performed provision discovery for
5263 * joining the group. Proceed directly to join
5264 * operation without duplicated provision discovery. */
5265 wpa_printf(MSG_DEBUG, "P2P: Provision discovery "
5266 "with " MACSTR " already done - proceed to "
5267 "join",
5268 MAC2STR(wpa_s->pending_join_dev_addr));
5269 wpa_s->pending_pd_before_join = 0;
5270 goto start;
5271 }
5272
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005273 if (p2p_prov_disc_req(wpa_s->global->p2p,
Dmitry Shmidt216983b2015-02-06 10:50:36 -08005274 wpa_s->pending_join_dev_addr,
5275 NULL, method, 1,
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08005276 freq, wpa_s->user_initiated_pd) < 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005277 wpa_printf(MSG_DEBUG, "P2P: Failed to send Provision "
5278 "Discovery Request before joining an "
5279 "existing group");
5280 wpa_s->pending_pd_before_join = 0;
5281 goto start;
5282 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005283 return;
5284 }
5285
5286 wpa_printf(MSG_DEBUG, "P2P: Failed to find BSS/GO - try again later");
5287 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
5288 eloop_register_timeout(1, 0, wpas_p2p_join_scan, wpa_s, NULL);
5289 wpas_p2p_check_join_scan_limit(wpa_s);
5290 return;
5291
5292start:
5293 /* Start join operation immediately */
Dmitry Shmidtde47be72016-01-07 12:52:55 -08005294 wpas_p2p_join_start(wpa_s, 0, wpa_s->p2p_join_ssid,
5295 wpa_s->p2p_join_ssid_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005296}
5297
5298
Dmitry Shmidt344abd32014-01-14 13:17:00 -08005299static void wpas_p2p_join_scan_req(struct wpa_supplicant *wpa_s, int freq,
5300 const u8 *ssid, size_t ssid_len)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005301{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005302 int ret;
5303 struct wpa_driver_scan_params params;
5304 struct wpabuf *wps_ie, *ies;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005305 size_t ielen;
Dmitry Shmidt04949592012-07-19 12:16:46 -07005306 int freqs[2] = { 0, 0 };
Dmitry Shmidt9c175262016-03-03 10:20:07 -08005307 unsigned int bands;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005308
5309 os_memset(&params, 0, sizeof(params));
5310
5311 /* P2P Wildcard SSID */
5312 params.num_ssids = 1;
Dmitry Shmidt344abd32014-01-14 13:17:00 -08005313 if (ssid && ssid_len) {
5314 params.ssids[0].ssid = ssid;
5315 params.ssids[0].ssid_len = ssid_len;
5316 os_memcpy(wpa_s->p2p_join_ssid, ssid, ssid_len);
5317 wpa_s->p2p_join_ssid_len = ssid_len;
5318 } else {
5319 params.ssids[0].ssid = (u8 *) P2P_WILDCARD_SSID;
5320 params.ssids[0].ssid_len = P2P_WILDCARD_SSID_LEN;
5321 wpa_s->p2p_join_ssid_len = 0;
5322 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005323
5324 wpa_s->wps->dev.p2p = 1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07005325 wps_ie = wps_build_probe_req_ie(DEV_PW_DEFAULT, &wpa_s->wps->dev,
5326 wpa_s->wps->uuid, WPS_REQ_ENROLLEE, 0,
5327 NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005328 if (wps_ie == NULL) {
5329 wpas_p2p_scan_res_join(wpa_s, NULL);
5330 return;
5331 }
5332
Dmitry Shmidt9e3f8ee2014-01-17 10:52:01 -08005333 if (!freq) {
5334 int oper_freq;
5335 /*
5336 * If freq is not provided, check the operating freq of the GO
5337 * and use a single channel scan on if possible.
5338 */
5339 oper_freq = p2p_get_oper_freq(wpa_s->global->p2p,
5340 wpa_s->pending_join_iface_addr);
5341 if (oper_freq > 0)
5342 freq = oper_freq;
5343 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07005344 if (freq > 0) {
5345 freqs[0] = freq;
5346 params.freqs = freqs;
Hai Shalom899fcc72020-10-19 14:38:18 -07005347 } else if (wpa_s->conf->p2p_6ghz_disable) {
5348 wpa_printf(MSG_DEBUG,
5349 "P2P: 6 GHz disabled - update the scan frequency list");
5350 wpas_p2p_add_scan_freq_list(wpa_s, HOSTAPD_MODE_IEEE80211G,
5351 &params);
5352 wpas_p2p_add_scan_freq_list(wpa_s, HOSTAPD_MODE_IEEE80211A,
5353 &params);
Dmitry Shmidt04949592012-07-19 12:16:46 -07005354 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005355
Dmitry Shmidt9c175262016-03-03 10:20:07 -08005356 ielen = p2p_scan_ie_buf_len(wpa_s->global->p2p);
5357 ies = wpabuf_alloc(wpabuf_len(wps_ie) + ielen);
5358 if (ies == NULL) {
5359 wpabuf_free(wps_ie);
5360 wpas_p2p_scan_res_join(wpa_s, NULL);
5361 return;
5362 }
5363 wpabuf_put_buf(ies, wps_ie);
5364 wpabuf_free(wps_ie);
5365
5366 bands = wpas_get_bands(wpa_s, freqs);
5367 p2p_scan_ie(wpa_s->global->p2p, ies, NULL, bands);
5368
5369 params.p2p_probe = 1;
5370 params.extra_ies = wpabuf_head(ies);
5371 params.extra_ies_len = wpabuf_len(ies);
5372
Dmitry Shmidtebd93af2017-02-21 13:40:44 -08005373 if (wpa_s->clear_driver_scan_cache) {
5374 wpa_printf(MSG_DEBUG,
5375 "Request driver to clear scan cache due to local BSS flush");
5376 params.only_new_results = 1;
5377 }
5378
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005379 /*
5380 * Run a scan to update BSS table and start Provision Discovery once
5381 * the new scan results become available.
5382 */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005383 ret = wpa_drv_scan(wpa_s, &params);
Hai Shalom899fcc72020-10-19 14:38:18 -07005384 if (wpa_s->conf->p2p_6ghz_disable && params.freqs != freqs)
5385 os_free(params.freqs);
Dmitry Shmidt444d5672013-04-01 13:08:44 -07005386 if (!ret) {
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005387 os_get_reltime(&wpa_s->scan_trigger_time);
Dmitry Shmidt04949592012-07-19 12:16:46 -07005388 wpa_s->scan_res_handler = wpas_p2p_scan_res_join;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005389 wpa_s->own_scan_requested = 1;
Dmitry Shmidtebd93af2017-02-21 13:40:44 -08005390 wpa_s->clear_driver_scan_cache = 0;
Dmitry Shmidt444d5672013-04-01 13:08:44 -07005391 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005392
5393 wpabuf_free(ies);
5394
5395 if (ret) {
5396 wpa_printf(MSG_DEBUG, "P2P: Failed to start scan for join - "
5397 "try again later");
5398 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
5399 eloop_register_timeout(1, 0, wpas_p2p_join_scan, wpa_s, NULL);
5400 wpas_p2p_check_join_scan_limit(wpa_s);
5401 }
5402}
5403
5404
Dmitry Shmidt04949592012-07-19 12:16:46 -07005405static void wpas_p2p_join_scan(void *eloop_ctx, void *timeout_ctx)
5406{
5407 struct wpa_supplicant *wpa_s = eloop_ctx;
Dmitry Shmidt344abd32014-01-14 13:17:00 -08005408 wpas_p2p_join_scan_req(wpa_s, 0, NULL, 0);
Dmitry Shmidt04949592012-07-19 12:16:46 -07005409}
5410
5411
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005412static int wpas_p2p_join(struct wpa_supplicant *wpa_s, const u8 *iface_addr,
Dmitry Shmidt04949592012-07-19 12:16:46 -07005413 const u8 *dev_addr, enum p2p_wps_method wps_method,
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08005414 int auto_join, int op_freq,
5415 const u8 *ssid, size_t ssid_len)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005416{
5417 wpa_printf(MSG_DEBUG, "P2P: Request to join existing group (iface "
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08005418 MACSTR " dev " MACSTR " op_freq=%d)%s",
5419 MAC2STR(iface_addr), MAC2STR(dev_addr), op_freq,
Dmitry Shmidt04949592012-07-19 12:16:46 -07005420 auto_join ? " (auto_join)" : "");
Dmitry Shmidt344abd32014-01-14 13:17:00 -08005421 if (ssid && ssid_len) {
5422 wpa_printf(MSG_DEBUG, "P2P: Group SSID specified: %s",
5423 wpa_ssid_txt(ssid, ssid_len));
5424 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005425
Dmitry Shmidt04949592012-07-19 12:16:46 -07005426 wpa_s->p2p_auto_pd = 0;
5427 wpa_s->p2p_auto_join = !!auto_join;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005428 os_memcpy(wpa_s->pending_join_iface_addr, iface_addr, ETH_ALEN);
5429 os_memcpy(wpa_s->pending_join_dev_addr, dev_addr, ETH_ALEN);
5430 wpa_s->pending_join_wps_method = wps_method;
5431
5432 /* Make sure we are not running find during connection establishment */
5433 wpas_p2p_stop_find(wpa_s);
5434
5435 wpa_s->p2p_join_scan_count = 0;
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08005436 wpas_p2p_join_scan_req(wpa_s, op_freq, ssid, ssid_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005437 return 0;
5438}
5439
5440
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08005441static int wpas_p2p_join_start(struct wpa_supplicant *wpa_s, int freq,
5442 const u8 *ssid, size_t ssid_len)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005443{
5444 struct wpa_supplicant *group;
5445 struct p2p_go_neg_results res;
Dmitry Shmidt04949592012-07-19 12:16:46 -07005446 struct wpa_bss *bss;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005447
5448 group = wpas_p2p_get_group_iface(wpa_s, 0, 0);
5449 if (group == NULL)
5450 return -1;
5451 if (group != wpa_s) {
5452 os_memcpy(group->p2p_pin, wpa_s->p2p_pin,
5453 sizeof(group->p2p_pin));
5454 group->p2p_wps_method = wpa_s->p2p_wps_method;
5455 }
5456
Hai Shalom74f70d42019-02-11 14:42:39 -08005457 /*
5458 * Need to mark the current interface for p2p_group_formation
5459 * when a separate group interface is not used. This is needed
5460 * to allow p2p_cancel stop a pending p2p_connect-join.
5461 * wpas_p2p_init_group_interface() addresses this for the case
5462 * where a separate group interface is used.
5463 */
5464 if (group == wpa_s->parent)
5465 wpa_s->global->p2p_group_formation = group;
5466
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005467 group->p2p_in_provisioning = 1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07005468 group->p2p_fallback_to_go_neg = wpa_s->p2p_fallback_to_go_neg;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005469
5470 os_memset(&res, 0, sizeof(res));
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08005471 os_memcpy(res.peer_device_addr, wpa_s->pending_join_dev_addr, ETH_ALEN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005472 os_memcpy(res.peer_interface_addr, wpa_s->pending_join_iface_addr,
5473 ETH_ALEN);
5474 res.wps_method = wpa_s->pending_join_wps_method;
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08005475 if (freq && ssid && ssid_len) {
5476 res.freq = freq;
5477 res.ssid_len = ssid_len;
5478 os_memcpy(res.ssid, ssid, ssid_len);
5479 } else {
Dmitry Shmidtde47be72016-01-07 12:52:55 -08005480 if (ssid && ssid_len) {
5481 bss = wpa_bss_get(wpa_s, wpa_s->pending_join_iface_addr,
5482 ssid, ssid_len);
5483 } else {
5484 bss = wpa_bss_get_bssid_latest(
5485 wpa_s, wpa_s->pending_join_iface_addr);
5486 }
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08005487 if (bss) {
5488 res.freq = bss->freq;
5489 res.ssid_len = bss->ssid_len;
5490 os_memcpy(res.ssid, bss->ssid, bss->ssid_len);
5491 wpa_printf(MSG_DEBUG, "P2P: Join target GO operating frequency from BSS table: %d MHz (SSID %s)",
5492 bss->freq,
5493 wpa_ssid_txt(bss->ssid, bss->ssid_len));
Dmitry Shmidtde47be72016-01-07 12:52:55 -08005494 } else if (ssid && ssid_len) {
5495 res.ssid_len = ssid_len;
5496 os_memcpy(res.ssid, ssid, ssid_len);
5497 wpa_printf(MSG_DEBUG, "P2P: Join target GO (SSID %s)",
5498 wpa_ssid_txt(ssid, ssid_len));
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08005499 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07005500 }
5501
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08005502 if (wpa_s->off_channel_freq || wpa_s->roc_waiting_drv_freq) {
5503 wpa_printf(MSG_DEBUG, "P2P: Cancel remain-on-channel prior to "
5504 "starting client");
5505 wpa_drv_cancel_remain_on_channel(wpa_s);
5506 wpa_s->off_channel_freq = 0;
5507 wpa_s->roc_waiting_drv_freq = 0;
5508 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005509 wpas_start_wps_enrollee(group, &res);
5510
5511 /*
5512 * Allow a longer timeout for join-a-running-group than normal 15
5513 * second group formation timeout since the GO may not have authorized
5514 * our connection yet.
5515 */
5516 eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s, NULL);
5517 eloop_register_timeout(60, 0, wpas_p2p_group_formation_timeout,
5518 wpa_s, NULL);
5519
5520 return 0;
5521}
5522
5523
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08005524static int wpas_p2p_setup_freqs(struct wpa_supplicant *wpa_s, int freq,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005525 int *force_freq, int *pref_freq, int go,
5526 unsigned int *pref_freq_list,
5527 unsigned int *num_pref_freq)
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08005528{
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07005529 struct wpa_used_freq_data *freqs;
5530 int res, best_freq, num_unused;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005531 unsigned int freq_in_use = 0, num, i, max_pref_freq;
5532
5533 max_pref_freq = *num_pref_freq;
5534 *num_pref_freq = 0;
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07005535
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07005536 freqs = os_calloc(wpa_s->num_multichan_concurrent,
5537 sizeof(struct wpa_used_freq_data));
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07005538 if (!freqs)
5539 return -1;
5540
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07005541 num = wpas_p2p_valid_oper_freqs(wpa_s, freqs,
5542 wpa_s->num_multichan_concurrent);
5543
5544 /*
5545 * It is possible that the total number of used frequencies is bigger
5546 * than the number of frequencies used for P2P, so get the system wide
5547 * number of unused frequencies.
5548 */
5549 num_unused = wpas_p2p_num_unused_channels(wpa_s);
5550
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07005551 wpa_printf(MSG_DEBUG,
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07005552 "P2P: Setup freqs: freq=%d num_MCC=%d shared_freqs=%u num_unused=%d",
5553 freq, wpa_s->num_multichan_concurrent, num, num_unused);
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07005554
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08005555 if (freq > 0) {
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07005556 int ret;
5557 if (go)
5558 ret = p2p_supported_freq(wpa_s->global->p2p, freq);
5559 else
5560 ret = p2p_supported_freq_cli(wpa_s->global->p2p, freq);
5561 if (!ret) {
Dmitry Shmidt203eadb2015-03-05 14:16:04 -08005562 if ((wpa_s->drv_flags & WPA_DRIVER_FLAGS_DFS_OFFLOAD) &&
Roshan Pius3a1667e2018-07-03 15:17:14 -07005563 ieee80211_is_dfs(freq, wpa_s->hw.modes,
5564 wpa_s->hw.num_modes)) {
Dmitry Shmidt203eadb2015-03-05 14:16:04 -08005565 /*
5566 * If freq is a DFS channel and DFS is offloaded
5567 * to the driver, allow P2P GO to use it.
5568 */
5569 wpa_printf(MSG_DEBUG,
5570 "P2P: The forced channel for GO (%u MHz) is DFS, and DFS is offloaded to the driver",
5571 freq);
5572 } else {
5573 wpa_printf(MSG_DEBUG,
5574 "P2P: The forced channel (%u MHz) is not supported for P2P uses",
5575 freq);
5576 res = -3;
5577 goto exit_free;
5578 }
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08005579 }
5580
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07005581 for (i = 0; i < num; i++) {
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07005582 if (freqs[i].freq == freq)
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07005583 freq_in_use = 1;
5584 }
5585
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07005586 if (num_unused <= 0 && !freq_in_use) {
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07005587 wpa_printf(MSG_DEBUG, "P2P: Cannot start P2P group on %u MHz as there are no available channels",
5588 freq);
5589 res = -2;
5590 goto exit_free;
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08005591 }
5592 wpa_printf(MSG_DEBUG, "P2P: Trying to force us to use the "
5593 "requested channel (%u MHz)", freq);
5594 *force_freq = freq;
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07005595 goto exit_ok;
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08005596 }
5597
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07005598 best_freq = wpas_p2p_pick_best_used_freq(wpa_s, freqs, num);
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07005599
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005600 if (!wpa_s->conf->num_p2p_pref_chan && *pref_freq == 0) {
5601 enum wpa_driver_if_type iface_type;
5602
5603 if (go)
5604 iface_type = WPA_IF_P2P_GO;
5605 else
5606 iface_type = WPA_IF_P2P_CLIENT;
5607
5608 wpa_printf(MSG_DEBUG, "P2P: best_freq=%d, go=%d",
5609 best_freq, go);
5610
5611 res = wpa_drv_get_pref_freq_list(wpa_s, iface_type,
5612 &max_pref_freq,
5613 pref_freq_list);
5614 if (!res && max_pref_freq > 0) {
5615 *num_pref_freq = max_pref_freq;
5616 i = 0;
Dmitry Shmidt29333592017-01-09 12:27:11 -08005617 while (i < *num_pref_freq &&
5618 (!p2p_supported_freq(wpa_s->global->p2p,
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08005619 pref_freq_list[i]) ||
5620 wpas_p2p_disallowed_freq(wpa_s->global,
Dmitry Shmidt29333592017-01-09 12:27:11 -08005621 pref_freq_list[i]))) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005622 wpa_printf(MSG_DEBUG,
5623 "P2P: preferred_freq_list[%d]=%d is disallowed",
5624 i, pref_freq_list[i]);
5625 i++;
5626 }
5627 if (i != *num_pref_freq) {
5628 best_freq = pref_freq_list[i];
5629 wpa_printf(MSG_DEBUG,
5630 "P2P: Using preferred_freq_list[%d]=%d",
5631 i, best_freq);
5632 } else {
5633 wpa_printf(MSG_DEBUG,
5634 "P2P: All driver preferred frequencies are disallowed for P2P use");
5635 *num_pref_freq = 0;
5636 }
5637 } else {
5638 wpa_printf(MSG_DEBUG,
5639 "P2P: No preferred frequency list available");
5640 }
5641 }
5642
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07005643 /* We have a candidate frequency to use */
5644 if (best_freq > 0) {
5645 if (*pref_freq == 0 && num_unused > 0) {
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08005646 wpa_printf(MSG_DEBUG, "P2P: Try to prefer a frequency (%u MHz) we are already using",
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07005647 best_freq);
5648 *pref_freq = best_freq;
Dmitry Shmidt51a47d52013-09-10 10:52:57 -07005649 } else {
5650 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 -07005651 best_freq);
5652 *force_freq = best_freq;
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07005653 }
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07005654 } else if (num_unused > 0) {
5655 wpa_printf(MSG_DEBUG,
5656 "P2P: Current operating channels are not available for P2P. Try to use another channel");
5657 *force_freq = 0;
5658 } else {
5659 wpa_printf(MSG_DEBUG,
5660 "P2P: All channels are in use and none of them are P2P enabled. Cannot start P2P group");
5661 res = -2;
5662 goto exit_free;
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07005663 }
5664
5665exit_ok:
5666 res = 0;
5667exit_free:
5668 os_free(freqs);
5669 return res;
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08005670}
5671
5672
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005673/**
5674 * wpas_p2p_connect - Request P2P Group Formation to be started
5675 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
5676 * @peer_addr: Address of the peer P2P Device
5677 * @pin: PIN to use during provisioning or %NULL to indicate PBC mode
5678 * @persistent_group: Whether to create a persistent group
Dmitry Shmidt04949592012-07-19 12:16:46 -07005679 * @auto_join: Whether to select join vs. GO Negotiation automatically
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005680 * @join: Whether to join an existing group (as a client) instead of starting
5681 * Group Owner negotiation; @peer_addr is BSSID in that case
5682 * @auth: Whether to only authorize the connection instead of doing that and
5683 * initiating Group Owner negotiation
5684 * @go_intent: GO Intent or -1 to use default
5685 * @freq: Frequency for the group or 0 for auto-selection
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08005686 * @freq2: Center frequency of segment 1 for the GO operating in VHT 80P80 mode
Dmitry Shmidt04949592012-07-19 12:16:46 -07005687 * @persistent_id: Persistent group credentials to use for forcing GO
5688 * parameters or -1 to generate new values (SSID/passphrase)
5689 * @pd: Whether to send Provision Discovery prior to GO Negotiation as an
5690 * interoperability workaround when initiating group formation
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005691 * @ht40: Start GO with 40 MHz channel width
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07005692 * @vht: Start GO with VHT support
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08005693 * @vht_chwidth: Channel width supported by GO operating with VHT support
Hai Shalom81f62d82019-07-22 12:10:00 -07005694 * (CHANWIDTH_*).
Dmitry Shmidtde47be72016-01-07 12:52:55 -08005695 * @group_ssid: Specific Group SSID for join or %NULL if not set
5696 * @group_ssid_len: Length of @group_ssid in octets
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005697 * Returns: 0 or new PIN (if pin was %NULL) on success, -1 on unspecified
5698 * failure, -2 on failure due to channel not currently available,
5699 * -3 if forced channel is not supported
5700 */
5701int wpas_p2p_connect(struct wpa_supplicant *wpa_s, const u8 *peer_addr,
5702 const char *pin, enum p2p_wps_method wps_method,
Dmitry Shmidt04949592012-07-19 12:16:46 -07005703 int persistent_group, int auto_join, int join, int auth,
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08005704 int go_intent, int freq, unsigned int vht_center_freq2,
5705 int persistent_id, int pd, int ht40, int vht,
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08005706 unsigned int vht_chwidth, int he, int edmg,
5707 const u8 *group_ssid, size_t group_ssid_len)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005708{
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07005709 int force_freq = 0, pref_freq = 0;
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08005710 int ret = 0, res;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005711 enum wpa_driver_if_type iftype;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005712 const u8 *if_addr;
Dmitry Shmidt04949592012-07-19 12:16:46 -07005713 struct wpa_ssid *ssid = NULL;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005714 unsigned int pref_freq_list[P2P_MAX_PREF_CHANNELS], size;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005715
5716 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
5717 return -1;
5718
Dmitry Shmidt04949592012-07-19 12:16:46 -07005719 if (persistent_id >= 0) {
5720 ssid = wpa_config_get_network(wpa_s->conf, persistent_id);
5721 if (ssid == NULL || ssid->disabled != 2 ||
5722 ssid->mode != WPAS_MODE_P2P_GO)
5723 return -1;
5724 }
5725
Hai Shalom899fcc72020-10-19 14:38:18 -07005726 if (is_6ghz_freq(freq) && wpa_s->conf->p2p_6ghz_disable)
5727 return -2;
5728
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07005729 os_free(wpa_s->global->add_psk);
5730 wpa_s->global->add_psk = NULL;
5731
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005732 wpa_s->global->p2p_fail_on_wps_complete = 0;
Dmitry Shmidt216983b2015-02-06 10:50:36 -08005733 wpa_s->global->pending_p2ps_group = 0;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005734 wpa_s->global->pending_p2ps_group_freq = 0;
5735 wpa_s->p2ps_method_config_any = 0;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005736
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005737 if (go_intent < 0)
5738 go_intent = wpa_s->conf->p2p_go_intent;
5739
5740 if (!auth)
Hai Shalomfdcde762020-04-02 11:19:20 -07005741 wpa_s->global->p2p_long_listen = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005742
5743 wpa_s->p2p_wps_method = wps_method;
Dmitry Shmidt04949592012-07-19 12:16:46 -07005744 wpa_s->p2p_persistent_group = !!persistent_group;
5745 wpa_s->p2p_persistent_id = persistent_id;
5746 wpa_s->p2p_go_intent = go_intent;
5747 wpa_s->p2p_connect_freq = freq;
5748 wpa_s->p2p_fallback_to_go_neg = 0;
5749 wpa_s->p2p_pd_before_go_neg = !!pd;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005750 wpa_s->p2p_go_ht40 = !!ht40;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07005751 wpa_s->p2p_go_vht = !!vht;
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08005752 wpa_s->p2p_go_vht_center_freq2 = vht_center_freq2;
5753 wpa_s->p2p_go_max_oper_chwidth = vht_chwidth;
Hai Shalom74f70d42019-02-11 14:42:39 -08005754 wpa_s->p2p_go_he = !!he;
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08005755 wpa_s->p2p_go_edmg = !!edmg;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005756
5757 if (pin)
5758 os_strlcpy(wpa_s->p2p_pin, pin, sizeof(wpa_s->p2p_pin));
5759 else if (wps_method == WPS_PIN_DISPLAY) {
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08005760 if (wps_generate_pin((unsigned int *) &ret) < 0)
5761 return -1;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005762 res = os_snprintf(wpa_s->p2p_pin, sizeof(wpa_s->p2p_pin),
5763 "%08d", ret);
5764 if (os_snprintf_error(sizeof(wpa_s->p2p_pin), res))
5765 wpa_s->p2p_pin[sizeof(wpa_s->p2p_pin) - 1] = '\0';
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005766 wpa_printf(MSG_DEBUG, "P2P: Randomly generated PIN: %s",
5767 wpa_s->p2p_pin);
Dmitry Shmidt849734c2016-05-27 09:59:01 -07005768 } else if (wps_method == WPS_P2PS) {
5769 /* Force the P2Ps default PIN to be used */
5770 os_strlcpy(wpa_s->p2p_pin, "12345670", sizeof(wpa_s->p2p_pin));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005771 } else
5772 wpa_s->p2p_pin[0] = '\0';
5773
Dmitry Shmidt04949592012-07-19 12:16:46 -07005774 if (join || auto_join) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005775 u8 iface_addr[ETH_ALEN], dev_addr[ETH_ALEN];
5776 if (auth) {
5777 wpa_printf(MSG_DEBUG, "P2P: Authorize invitation to "
5778 "connect a running group from " MACSTR,
5779 MAC2STR(peer_addr));
5780 os_memcpy(wpa_s->p2p_auth_invite, peer_addr, ETH_ALEN);
5781 return ret;
5782 }
5783 os_memcpy(dev_addr, peer_addr, ETH_ALEN);
5784 if (p2p_get_interface_addr(wpa_s->global->p2p, peer_addr,
5785 iface_addr) < 0) {
5786 os_memcpy(iface_addr, peer_addr, ETH_ALEN);
5787 p2p_get_dev_addr(wpa_s->global->p2p, peer_addr,
5788 dev_addr);
5789 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07005790 if (auto_join) {
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005791 os_get_reltime(&wpa_s->p2p_auto_started);
Dmitry Shmidt04949592012-07-19 12:16:46 -07005792 wpa_printf(MSG_DEBUG, "P2P: Auto join started at "
5793 "%ld.%06ld",
5794 wpa_s->p2p_auto_started.sec,
5795 wpa_s->p2p_auto_started.usec);
5796 }
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08005797 wpa_s->user_initiated_pd = 1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07005798 if (wpas_p2p_join(wpa_s, iface_addr, dev_addr, wps_method,
Dmitry Shmidtde47be72016-01-07 12:52:55 -08005799 auto_join, freq,
5800 group_ssid, group_ssid_len) < 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005801 return -1;
5802 return ret;
5803 }
5804
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005805 size = P2P_MAX_PREF_CHANNELS;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07005806 res = wpas_p2p_setup_freqs(wpa_s, freq, &force_freq, &pref_freq,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005807 go_intent == 15, pref_freq_list, &size);
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08005808 if (res)
5809 return res;
Dmitry Shmidta0d265f2013-11-19 13:13:41 -08005810 wpas_p2p_set_own_freq_preference(wpa_s,
5811 force_freq ? force_freq : pref_freq);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005812
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005813 p2p_set_own_pref_freq_list(wpa_s->global->p2p, pref_freq_list, size);
5814
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005815 wpa_s->create_p2p_iface = wpas_p2p_create_iface(wpa_s);
5816
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005817 if (wpa_s->create_p2p_iface) {
5818 /* Prepare to add a new interface for the group */
5819 iftype = WPA_IF_P2P_GROUP;
5820 if (go_intent == 15)
5821 iftype = WPA_IF_P2P_GO;
5822 if (wpas_p2p_add_group_interface(wpa_s, iftype) < 0) {
5823 wpa_printf(MSG_ERROR, "P2P: Failed to allocate a new "
5824 "interface for the group");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005825 return -1;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005826 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005827
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005828 if_addr = wpa_s->pending_interface_addr;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005829 } else {
Dmitry Shmidt9c175262016-03-03 10:20:07 -08005830 if (wpa_s->p2p_mgmt)
5831 if_addr = wpa_s->parent->own_addr;
5832 else
5833 if_addr = wpa_s->own_addr;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005834 os_memset(wpa_s->go_dev_addr, 0, ETH_ALEN);
5835 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005836
5837 if (auth) {
5838 if (wpas_p2p_auth_go_neg(wpa_s, peer_addr, wps_method,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005839 go_intent, if_addr,
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08005840 force_freq, persistent_group, ssid,
5841 pref_freq) < 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005842 return -1;
5843 return ret;
5844 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005845
5846 if (wpas_p2p_start_go_neg(wpa_s, peer_addr, wps_method,
5847 go_intent, if_addr, force_freq,
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08005848 persistent_group, ssid, pref_freq) < 0) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005849 if (wpa_s->create_p2p_iface)
5850 wpas_p2p_remove_pending_group_interface(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005851 return -1;
5852 }
5853 return ret;
5854}
5855
5856
5857/**
5858 * wpas_p2p_remain_on_channel_cb - Indication of remain-on-channel start
5859 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
5860 * @freq: Frequency of the channel in MHz
5861 * @duration: Duration of the stay on the channel in milliseconds
5862 *
5863 * This callback is called when the driver indicates that it has started the
5864 * requested remain-on-channel duration.
5865 */
5866void wpas_p2p_remain_on_channel_cb(struct wpa_supplicant *wpa_s,
5867 unsigned int freq, unsigned int duration)
5868{
5869 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
5870 return;
Dmitry Shmidt216983b2015-02-06 10:50:36 -08005871 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)",
5872 wpa_s->off_channel_freq, wpa_s->pending_listen_freq,
5873 wpa_s->roc_waiting_drv_freq, freq, duration);
5874 if (wpa_s->off_channel_freq &&
5875 wpa_s->off_channel_freq == wpa_s->pending_listen_freq) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005876 p2p_listen_cb(wpa_s->global->p2p, wpa_s->pending_listen_freq,
5877 wpa_s->pending_listen_duration);
5878 wpa_s->pending_listen_freq = 0;
Dmitry Shmidt7f93d6f2014-02-21 11:22:49 -08005879 } else {
5880 wpa_printf(MSG_DEBUG, "P2P: Ignore remain-on-channel callback (off_channel_freq=%u pending_listen_freq=%d freq=%u duration=%u)",
5881 wpa_s->off_channel_freq, wpa_s->pending_listen_freq,
5882 freq, duration);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005883 }
5884}
5885
5886
Jithu Jance57cea1a2014-08-20 10:22:04 -07005887int wpas_p2p_listen_start(struct wpa_supplicant *wpa_s, unsigned int timeout)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005888{
5889 /* Limit maximum Listen state time based on driver limitation. */
5890 if (timeout > wpa_s->max_remain_on_chan)
5891 timeout = wpa_s->max_remain_on_chan;
5892
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005893 return p2p_listen(wpa_s->global->p2p, timeout);
5894}
5895
5896
5897/**
5898 * wpas_p2p_cancel_remain_on_channel_cb - Remain-on-channel timeout
5899 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
5900 * @freq: Frequency of the channel in MHz
5901 *
5902 * This callback is called when the driver indicates that a remain-on-channel
5903 * operation has been completed, i.e., the duration on the requested channel
5904 * has timed out.
5905 */
5906void wpas_p2p_cancel_remain_on_channel_cb(struct wpa_supplicant *wpa_s,
5907 unsigned int freq)
5908{
5909 wpa_printf(MSG_DEBUG, "P2P: Cancel remain-on-channel callback "
5910 "(p2p_long_listen=%d ms pending_action_tx=%p)",
Hai Shalomfdcde762020-04-02 11:19:20 -07005911 wpa_s->global->p2p_long_listen,
5912 offchannel_pending_action_tx(wpa_s));
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005913 wpas_p2p_listen_work_done(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005914 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
5915 return;
Hai Shalomfdcde762020-04-02 11:19:20 -07005916 if (wpa_s->global->p2p_long_listen > 0)
5917 wpa_s->global->p2p_long_listen -= wpa_s->max_remain_on_chan;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005918 if (p2p_listen_end(wpa_s->global->p2p, freq) > 0)
5919 return; /* P2P module started a new operation */
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08005920 if (offchannel_pending_action_tx(wpa_s))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005921 return;
Hai Shalomfdcde762020-04-02 11:19:20 -07005922 if (wpa_s->global->p2p_long_listen > 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005923 wpa_printf(MSG_DEBUG, "P2P: Continuing long Listen state");
Hai Shalomfdcde762020-04-02 11:19:20 -07005924 wpas_p2p_listen_start(wpa_s, wpa_s->global->p2p_long_listen);
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07005925 } else {
5926 /*
5927 * When listen duration is over, stop listen & update p2p_state
5928 * to IDLE.
5929 */
5930 p2p_stop_listen(wpa_s->global->p2p);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005931 }
5932}
5933
5934
5935/**
5936 * wpas_p2p_group_remove - Remove a P2P group
5937 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
5938 * @ifname: Network interface name of the group interface or "*" to remove all
5939 * groups
5940 * Returns: 0 on success, -1 on failure
5941 *
5942 * This function is used to remove a P2P group. This can be used to disconnect
5943 * from a group in which the local end is a P2P Client or to end a P2P Group in
5944 * case the local end is the Group Owner. If a virtual network interface was
5945 * created for this group, that interface will be removed. Otherwise, only the
5946 * configured P2P group network will be removed from the interface.
5947 */
5948int wpas_p2p_group_remove(struct wpa_supplicant *wpa_s, const char *ifname)
5949{
5950 struct wpa_global *global = wpa_s->global;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005951 struct wpa_supplicant *calling_wpa_s = wpa_s;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005952
5953 if (os_strcmp(ifname, "*") == 0) {
5954 struct wpa_supplicant *prev;
Hai Shalom899fcc72020-10-19 14:38:18 -07005955 bool calling_wpa_s_group_removed = false;
5956
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005957 wpa_s = global->ifaces;
5958 while (wpa_s) {
5959 prev = wpa_s;
5960 wpa_s = wpa_s->next;
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07005961 if (prev->p2p_group_interface !=
5962 NOT_P2P_GROUP_INTERFACE ||
5963 (prev->current_ssid &&
Hai Shalom899fcc72020-10-19 14:38:18 -07005964 prev->current_ssid->p2p_group)) {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005965 wpas_p2p_disconnect_safely(prev, calling_wpa_s);
Hai Shalom899fcc72020-10-19 14:38:18 -07005966 if (prev == calling_wpa_s)
5967 calling_wpa_s_group_removed = true;
5968 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005969 }
Hai Shalom899fcc72020-10-19 14:38:18 -07005970
5971 if (!calling_wpa_s_group_removed &&
5972 (calling_wpa_s->p2p_group_interface !=
5973 NOT_P2P_GROUP_INTERFACE ||
5974 (calling_wpa_s->current_ssid &&
5975 calling_wpa_s->current_ssid->p2p_group))) {
5976 wpa_printf(MSG_DEBUG, "Remove calling_wpa_s P2P group");
5977 wpas_p2p_disconnect_safely(calling_wpa_s,
5978 calling_wpa_s);
5979 }
5980
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005981 return 0;
5982 }
5983
5984 for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
5985 if (os_strcmp(wpa_s->ifname, ifname) == 0)
5986 break;
5987 }
5988
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005989 return wpas_p2p_disconnect_safely(wpa_s, calling_wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005990}
5991
5992
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07005993static int wpas_p2p_select_go_freq(struct wpa_supplicant *wpa_s, int freq)
5994{
5995 unsigned int r;
5996
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005997 if (!wpa_s->conf->num_p2p_pref_chan && !freq) {
5998 unsigned int i, size = P2P_MAX_PREF_CHANNELS;
5999 unsigned int pref_freq_list[P2P_MAX_PREF_CHANNELS];
6000 int res;
6001
6002 res = wpa_drv_get_pref_freq_list(wpa_s, WPA_IF_P2P_GO,
6003 &size, pref_freq_list);
6004 if (!res && size > 0) {
6005 i = 0;
Dmitry Shmidt29333592017-01-09 12:27:11 -08006006 while (i < size &&
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07006007 (!p2p_supported_freq(wpa_s->global->p2p,
6008 pref_freq_list[i]) ||
6009 wpas_p2p_disallowed_freq(wpa_s->global,
6010 pref_freq_list[i]))) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006011 wpa_printf(MSG_DEBUG,
6012 "P2P: preferred_freq_list[%d]=%d is disallowed",
6013 i, pref_freq_list[i]);
6014 i++;
6015 }
6016 if (i != size) {
6017 freq = pref_freq_list[i];
6018 wpa_printf(MSG_DEBUG,
6019 "P2P: Using preferred_freq_list[%d]=%d",
6020 i, freq);
6021 } else {
6022 wpa_printf(MSG_DEBUG,
6023 "P2P: All driver preferred frequencies are disallowed for P2P use");
6024 }
6025 } else {
6026 wpa_printf(MSG_DEBUG,
6027 "P2P: No preferred frequency list available");
6028 }
6029 }
6030
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07006031 if (freq == 2) {
6032 wpa_printf(MSG_DEBUG, "P2P: Request to start GO on 2.4 GHz "
6033 "band");
6034 if (wpa_s->best_24_freq > 0 &&
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07006035 p2p_supported_freq_go(wpa_s->global->p2p,
6036 wpa_s->best_24_freq)) {
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07006037 freq = wpa_s->best_24_freq;
6038 wpa_printf(MSG_DEBUG, "P2P: Use best 2.4 GHz band "
6039 "channel: %d MHz", freq);
6040 } else {
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07006041 if (os_get_random((u8 *) &r, sizeof(r)) < 0)
6042 return -1;
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07006043 freq = 2412 + (r % 3) * 25;
6044 wpa_printf(MSG_DEBUG, "P2P: Use random 2.4 GHz band "
6045 "channel: %d MHz", freq);
6046 }
6047 }
6048
6049 if (freq == 5) {
6050 wpa_printf(MSG_DEBUG, "P2P: Request to start GO on 5 GHz "
6051 "band");
6052 if (wpa_s->best_5_freq > 0 &&
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07006053 p2p_supported_freq_go(wpa_s->global->p2p,
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07006054 wpa_s->best_5_freq)) {
6055 freq = wpa_s->best_5_freq;
6056 wpa_printf(MSG_DEBUG, "P2P: Use best 5 GHz band "
6057 "channel: %d MHz", freq);
6058 } else {
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07006059 if (os_get_random((u8 *) &r, sizeof(r)) < 0)
6060 return -1;
Jimmy Chen64b90632020-09-29 17:27:34 +08006061
6062 /*
6063 * most of 5G channels are DFS, only operating class 115 and 124
6064 * are available possibly, randomly pick a start to check them.
6065 */
6066 int possible_5g_freqs[] = {
6067 /* operating class 115 */
6068 5180, 5200, 5220, 5240,
6069 /* operating class 124 */
6070 5745, 5765, 5785, 5805,
6071 };
6072 int possible_5g_freqs_num =
6073 sizeof(possible_5g_freqs)/sizeof(possible_5g_freqs[0]);
6074
6075 int i;
6076 for (i = 0; i < possible_5g_freqs_num; i++, r++) {
6077 if (p2p_supported_freq_go(
6078 wpa_s->global->p2p,
6079 possible_5g_freqs[r % possible_5g_freqs_num])) {
6080 freq = possible_5g_freqs[r % possible_5g_freqs_num];
6081 break;
6082 }
6083 }
6084
6085 if (i >= possible_5g_freqs_num) {
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07006086 wpa_printf(MSG_DEBUG, "P2P: Could not select "
6087 "5 GHz channel for P2P group");
6088 return -1;
6089 }
6090 wpa_printf(MSG_DEBUG, "P2P: Use random 5 GHz band "
6091 "channel: %d MHz", freq);
6092 }
6093 }
6094
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07006095 if (freq > 0 && !p2p_supported_freq_go(wpa_s->global->p2p, freq)) {
Dmitry Shmidt203eadb2015-03-05 14:16:04 -08006096 if ((wpa_s->drv_flags & WPA_DRIVER_FLAGS_DFS_OFFLOAD) &&
Roshan Pius3a1667e2018-07-03 15:17:14 -07006097 ieee80211_is_dfs(freq, wpa_s->hw.modes,
6098 wpa_s->hw.num_modes)) {
Dmitry Shmidt203eadb2015-03-05 14:16:04 -08006099 /*
6100 * If freq is a DFS channel and DFS is offloaded to the
6101 * driver, allow P2P GO to use it.
6102 */
6103 wpa_printf(MSG_DEBUG, "P2P: "
6104 "%s: The forced channel for GO (%u MHz) is DFS, and DFS is offloaded",
6105 __func__, freq);
6106 return freq;
6107 }
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07006108 wpa_printf(MSG_DEBUG, "P2P: The forced channel for GO "
6109 "(%u MHz) is not supported for P2P uses",
6110 freq);
6111 return -1;
6112 }
6113
6114 return freq;
6115}
6116
6117
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006118static int wpas_p2p_supported_freq_go(struct wpa_supplicant *wpa_s,
6119 const struct p2p_channels *channels,
6120 int freq)
6121{
6122 if (!wpas_p2p_disallowed_freq(wpa_s->global, freq) &&
6123 p2p_supported_freq_go(wpa_s->global->p2p, freq) &&
6124 freq_included(wpa_s, channels, freq))
6125 return 1;
6126 return 0;
6127}
6128
6129
6130static void wpas_p2p_select_go_freq_no_pref(struct wpa_supplicant *wpa_s,
6131 struct p2p_go_neg_results *params,
6132 const struct p2p_channels *channels)
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07006133{
6134 unsigned int i, r;
6135
Dmitry Shmidt203eadb2015-03-05 14:16:04 -08006136 /* try all channels in operating class 115 */
6137 for (i = 0; i < 4; i++) {
6138 params->freq = 5180 + i * 20;
6139 if (!wpas_p2p_disallowed_freq(wpa_s->global, params->freq) &&
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006140 freq_included(wpa_s, channels, params->freq) &&
Dmitry Shmidt203eadb2015-03-05 14:16:04 -08006141 p2p_supported_freq(wpa_s->global->p2p, params->freq))
6142 goto out;
6143 }
6144
6145 /* try all channels in operating class 124 */
6146 for (i = 0; i < 4; i++) {
6147 params->freq = 5745 + i * 20;
6148 if (!wpas_p2p_disallowed_freq(wpa_s->global, params->freq) &&
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006149 freq_included(wpa_s, channels, params->freq) &&
Dmitry Shmidt203eadb2015-03-05 14:16:04 -08006150 p2p_supported_freq(wpa_s->global->p2p, params->freq))
6151 goto out;
6152 }
6153
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07006154 /* try social channel class 180 channel 2 */
6155 params->freq = 58320 + 1 * 2160;
6156 if (!wpas_p2p_disallowed_freq(wpa_s->global, params->freq) &&
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006157 freq_included(wpa_s, channels, params->freq) &&
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07006158 p2p_supported_freq(wpa_s->global->p2p, params->freq))
6159 goto out;
6160
6161 /* try all channels in reg. class 180 */
6162 for (i = 0; i < 4; i++) {
6163 params->freq = 58320 + i * 2160;
6164 if (!wpas_p2p_disallowed_freq(wpa_s->global, params->freq) &&
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006165 freq_included(wpa_s, channels, params->freq) &&
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07006166 p2p_supported_freq(wpa_s->global->p2p, params->freq))
6167 goto out;
6168 }
6169
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07006170 /* try some random selection of the social channels */
6171 if (os_get_random((u8 *) &r, sizeof(r)) < 0)
6172 return;
6173
6174 for (i = 0; i < 3; i++) {
6175 params->freq = 2412 + ((r + i) % 3) * 25;
6176 if (wpas_p2p_supported_freq_go(wpa_s, channels, params->freq))
6177 goto out;
6178 }
6179
6180 /* try all other channels in operating class 81 */
6181 for (i = 0; i < 11; i++) {
6182 params->freq = 2412 + i * 5;
6183
6184 /* skip social channels; covered in the previous loop */
6185 if (params->freq == 2412 ||
6186 params->freq == 2437 ||
6187 params->freq == 2462)
6188 continue;
6189
6190 if (wpas_p2p_supported_freq_go(wpa_s, channels, params->freq))
6191 goto out;
6192 }
6193
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006194 params->freq = 0;
Dmitry Shmidt203eadb2015-03-05 14:16:04 -08006195 wpa_printf(MSG_DEBUG, "P2P: No 2.4, 5, or 60 GHz channel allowed");
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006196 return;
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07006197out:
6198 wpa_printf(MSG_DEBUG, "P2P: Set GO freq %d MHz (no preference known)",
6199 params->freq);
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07006200}
6201
6202
Roshan Pius3a1667e2018-07-03 15:17:14 -07006203static int wpas_same_band(int freq1, int freq2)
6204{
6205 enum hostapd_hw_mode mode1, mode2;
6206 u8 chan1, chan2;
6207
6208 mode1 = ieee80211_freq_to_chan(freq1, &chan1);
6209 mode2 = ieee80211_freq_to_chan(freq2, &chan2);
6210 if (mode1 == NUM_HOSTAPD_MODES)
6211 return 0;
6212 return mode1 == mode2;
6213}
6214
6215
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006216static int wpas_p2p_init_go_params(struct wpa_supplicant *wpa_s,
6217 struct p2p_go_neg_results *params,
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08006218 int freq, int vht_center_freq2, int ht40,
Hai Shalom74f70d42019-02-11 14:42:39 -08006219 int vht, int max_oper_chwidth, int he,
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08006220 int edmg,
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08006221 const struct p2p_channels *channels)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006222{
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07006223 struct wpa_used_freq_data *freqs;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006224 unsigned int cand;
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07006225 unsigned int num, i;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006226 int ignore_no_freqs = 0;
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08006227 int unused_channels = wpas_p2p_num_unused_channels(wpa_s) > 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006228
6229 os_memset(params, 0, sizeof(*params));
6230 params->role_go = 1;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07006231 params->ht40 = ht40;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07006232 params->vht = vht;
Hai Shalom74f70d42019-02-11 14:42:39 -08006233 params->he = he;
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08006234 params->max_oper_chwidth = max_oper_chwidth;
6235 params->vht_center_freq2 = vht_center_freq2;
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08006236 params->edmg = edmg;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006237
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07006238 freqs = os_calloc(wpa_s->num_multichan_concurrent,
6239 sizeof(struct wpa_used_freq_data));
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07006240 if (!freqs)
6241 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006242
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006243 num = get_shared_radio_freqs_data(wpa_s, freqs,
6244 wpa_s->num_multichan_concurrent);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006245
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006246 if (wpa_s->current_ssid &&
6247 wpa_s->current_ssid->mode == WPAS_MODE_P2P_GO &&
6248 wpa_s->wpa_state == WPA_COMPLETED) {
6249 wpa_printf(MSG_DEBUG, "P2P: %s called for an active GO",
6250 __func__);
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07006251
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006252 /*
6253 * If the frequency selection is done for an active P2P GO that
6254 * is not sharing a frequency, allow to select a new frequency
6255 * even if there are no unused frequencies as we are about to
6256 * move the P2P GO so its frequency can be re-used.
6257 */
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07006258 for (i = 0; i < num; i++) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006259 if (freqs[i].freq == wpa_s->current_ssid->frequency &&
6260 freqs[i].flags == 0) {
6261 ignore_no_freqs = 1;
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07006262 break;
6263 }
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07006264 }
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08006265 }
6266
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08006267 /* Try to use EDMG channel */
6268 if (params->edmg) {
6269 if (wpas_p2p_try_edmg_channel(wpa_s, params) == 0)
6270 goto success;
6271 params->edmg = 0;
6272 }
6273
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006274 /* try using the forced freq */
6275 if (freq) {
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07006276 if (wpas_p2p_disallowed_freq(wpa_s->global, freq) ||
6277 !freq_included(wpa_s, channels, freq)) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006278 wpa_printf(MSG_DEBUG,
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07006279 "P2P: Forced GO freq %d MHz disallowed",
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006280 freq);
6281 goto fail;
6282 }
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07006283 if (!p2p_supported_freq_go(wpa_s->global->p2p, freq)) {
6284 if ((wpa_s->drv_flags & WPA_DRIVER_FLAGS_DFS_OFFLOAD) &&
Roshan Pius3a1667e2018-07-03 15:17:14 -07006285 ieee80211_is_dfs(freq, wpa_s->hw.modes,
6286 wpa_s->hw.num_modes)) {
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07006287 /*
6288 * If freq is a DFS channel and DFS is offloaded
6289 * to the driver, allow P2P GO to use it.
6290 */
6291 wpa_printf(MSG_DEBUG,
6292 "P2P: %s: The forced channel for GO (%u MHz) requires DFS and DFS is offloaded",
6293 __func__, freq);
6294 } else {
6295 wpa_printf(MSG_DEBUG,
6296 "P2P: The forced channel for GO (%u MHz) is not supported for P2P uses",
6297 freq);
6298 goto fail;
6299 }
6300 }
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006301
6302 for (i = 0; i < num; i++) {
6303 if (freqs[i].freq == freq) {
6304 wpa_printf(MSG_DEBUG,
6305 "P2P: forced freq (%d MHz) is also shared",
6306 freq);
6307 params->freq = freq;
6308 goto success;
6309 }
6310 }
6311
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08006312 if (!ignore_no_freqs && !unused_channels) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006313 wpa_printf(MSG_DEBUG,
6314 "P2P: Cannot force GO on freq (%d MHz) as all the channels are in use",
6315 freq);
6316 goto fail;
6317 }
6318
6319 wpa_printf(MSG_DEBUG,
6320 "P2P: force GO freq (%d MHz) on a free channel",
6321 freq);
6322 params->freq = freq;
6323 goto success;
6324 }
6325
6326 /* consider using one of the shared frequencies */
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08006327 if (num &&
6328 (!wpa_s->conf->p2p_ignore_shared_freq || !unused_channels)) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006329 cand = wpas_p2p_pick_best_used_freq(wpa_s, freqs, num);
6330 if (wpas_p2p_supported_freq_go(wpa_s, channels, cand)) {
6331 wpa_printf(MSG_DEBUG,
6332 "P2P: Use shared freq (%d MHz) for GO",
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08006333 cand);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006334 params->freq = cand;
6335 goto success;
6336 }
6337
6338 /* try using one of the shared freqs */
6339 for (i = 0; i < num; i++) {
6340 if (wpas_p2p_supported_freq_go(wpa_s, channels,
6341 freqs[i].freq)) {
6342 wpa_printf(MSG_DEBUG,
6343 "P2P: Use shared freq (%d MHz) for GO",
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08006344 freqs[i].freq);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006345 params->freq = freqs[i].freq;
6346 goto success;
6347 }
6348 }
6349 }
6350
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08006351 if (!ignore_no_freqs && !unused_channels) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006352 wpa_printf(MSG_DEBUG,
6353 "P2P: Cannot force GO on any of the channels we are already using");
6354 goto fail;
6355 }
6356
6357 /* try using the setting from the configuration file */
6358 if (wpa_s->conf->p2p_oper_reg_class == 81 &&
6359 wpa_s->conf->p2p_oper_channel >= 1 &&
6360 wpa_s->conf->p2p_oper_channel <= 11 &&
6361 wpas_p2p_supported_freq_go(
6362 wpa_s, channels,
6363 2407 + 5 * wpa_s->conf->p2p_oper_channel)) {
6364 params->freq = 2407 + 5 * wpa_s->conf->p2p_oper_channel;
6365 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on configured "
6366 "frequency %d MHz", params->freq);
6367 goto success;
6368 }
6369
6370 if ((wpa_s->conf->p2p_oper_reg_class == 115 ||
6371 wpa_s->conf->p2p_oper_reg_class == 116 ||
6372 wpa_s->conf->p2p_oper_reg_class == 117 ||
6373 wpa_s->conf->p2p_oper_reg_class == 124 ||
6374 wpa_s->conf->p2p_oper_reg_class == 125 ||
6375 wpa_s->conf->p2p_oper_reg_class == 126 ||
6376 wpa_s->conf->p2p_oper_reg_class == 127) &&
6377 wpas_p2p_supported_freq_go(wpa_s, channels,
6378 5000 +
6379 5 * wpa_s->conf->p2p_oper_channel)) {
6380 params->freq = 5000 + 5 * wpa_s->conf->p2p_oper_channel;
6381 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on configured "
6382 "frequency %d MHz", params->freq);
6383 goto success;
6384 }
6385
6386 /* Try using best channels */
6387 if (wpa_s->conf->p2p_oper_channel == 0 &&
6388 wpa_s->best_overall_freq > 0 &&
6389 wpas_p2p_supported_freq_go(wpa_s, channels,
6390 wpa_s->best_overall_freq)) {
6391 params->freq = wpa_s->best_overall_freq;
6392 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on best overall "
6393 "channel %d MHz", params->freq);
6394 goto success;
6395 }
6396
6397 if (wpa_s->conf->p2p_oper_channel == 0 &&
6398 wpa_s->best_24_freq > 0 &&
6399 wpas_p2p_supported_freq_go(wpa_s, channels,
6400 wpa_s->best_24_freq)) {
6401 params->freq = wpa_s->best_24_freq;
6402 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on best 2.4 GHz "
6403 "channel %d MHz", params->freq);
6404 goto success;
6405 }
6406
6407 if (wpa_s->conf->p2p_oper_channel == 0 &&
6408 wpa_s->best_5_freq > 0 &&
6409 wpas_p2p_supported_freq_go(wpa_s, channels,
6410 wpa_s->best_5_freq)) {
6411 params->freq = wpa_s->best_5_freq;
6412 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on best 5 GHz "
6413 "channel %d MHz", params->freq);
6414 goto success;
6415 }
6416
6417 /* try using preferred channels */
6418 cand = p2p_get_pref_freq(wpa_s->global->p2p, channels);
6419 if (cand && wpas_p2p_supported_freq_go(wpa_s, channels, cand)) {
6420 params->freq = cand;
6421 wpa_printf(MSG_DEBUG, "P2P: Set GO freq %d MHz from preferred "
6422 "channels", params->freq);
6423 goto success;
6424 }
6425
Roshan Pius3a1667e2018-07-03 15:17:14 -07006426 /* Try using a channel that allows VHT to be used with 80 MHz */
6427 if (wpa_s->hw.modes && wpa_s->p2p_group_common_freqs) {
6428 for (i = 0; i < wpa_s->p2p_group_common_freqs_num; i++) {
6429 enum hostapd_hw_mode mode;
6430 struct hostapd_hw_modes *hwmode;
6431 u8 chan;
6432
6433 cand = wpa_s->p2p_group_common_freqs[i];
6434 mode = ieee80211_freq_to_chan(cand, &chan);
6435 hwmode = get_mode(wpa_s->hw.modes, wpa_s->hw.num_modes,
Hai Shalomfdcde762020-04-02 11:19:20 -07006436 mode, is_6ghz_freq(cand));
Roshan Pius3a1667e2018-07-03 15:17:14 -07006437 if (!hwmode ||
6438 wpas_p2p_verify_channel(wpa_s, hwmode, chan,
6439 BW80) != ALLOWED)
6440 continue;
6441 if (wpas_p2p_supported_freq_go(wpa_s, channels, cand)) {
6442 params->freq = cand;
6443 wpa_printf(MSG_DEBUG,
6444 "P2P: Use freq %d MHz common with the peer and allowing VHT80",
6445 params->freq);
6446 goto success;
6447 }
6448 }
6449 }
6450
6451 /* Try using a channel that allows HT to be used with 40 MHz on the same
6452 * band so that CSA can be used */
6453 if (wpa_s->current_ssid && wpa_s->hw.modes &&
6454 wpa_s->p2p_group_common_freqs) {
6455 for (i = 0; i < wpa_s->p2p_group_common_freqs_num; i++) {
6456 enum hostapd_hw_mode mode;
6457 struct hostapd_hw_modes *hwmode;
6458 u8 chan;
6459
6460 cand = wpa_s->p2p_group_common_freqs[i];
6461 mode = ieee80211_freq_to_chan(cand, &chan);
6462 hwmode = get_mode(wpa_s->hw.modes, wpa_s->hw.num_modes,
Hai Shalomfdcde762020-04-02 11:19:20 -07006463 mode, is_6ghz_freq(cand));
Roshan Pius3a1667e2018-07-03 15:17:14 -07006464 if (!wpas_same_band(wpa_s->current_ssid->frequency,
6465 cand) ||
6466 !hwmode ||
6467 (wpas_p2p_verify_channel(wpa_s, hwmode, chan,
6468 BW40MINUS) != ALLOWED &&
6469 wpas_p2p_verify_channel(wpa_s, hwmode, chan,
6470 BW40PLUS) != ALLOWED))
6471 continue;
6472 if (wpas_p2p_supported_freq_go(wpa_s, channels, cand)) {
6473 params->freq = cand;
6474 wpa_printf(MSG_DEBUG,
6475 "P2P: Use freq %d MHz common with the peer, allowing HT40, and maintaining same band",
6476 params->freq);
6477 goto success;
6478 }
6479 }
6480 }
6481
6482 /* Try using one of the group common freqs on the same band so that CSA
6483 * can be used */
6484 if (wpa_s->current_ssid && wpa_s->p2p_group_common_freqs) {
6485 for (i = 0; i < wpa_s->p2p_group_common_freqs_num; i++) {
6486 cand = wpa_s->p2p_group_common_freqs[i];
6487 if (!wpas_same_band(wpa_s->current_ssid->frequency,
6488 cand))
6489 continue;
6490 if (wpas_p2p_supported_freq_go(wpa_s, channels, cand)) {
6491 params->freq = cand;
6492 wpa_printf(MSG_DEBUG,
6493 "P2P: Use freq %d MHz common with the peer and maintaining same band",
6494 params->freq);
6495 goto success;
6496 }
6497 }
6498 }
6499
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006500 /* Try using one of the group common freqs */
6501 if (wpa_s->p2p_group_common_freqs) {
6502 for (i = 0; i < wpa_s->p2p_group_common_freqs_num; i++) {
6503 cand = wpa_s->p2p_group_common_freqs[i];
6504 if (wpas_p2p_supported_freq_go(wpa_s, channels, cand)) {
6505 params->freq = cand;
6506 wpa_printf(MSG_DEBUG,
6507 "P2P: Use freq %d MHz common with the peer",
6508 params->freq);
6509 goto success;
6510 }
6511 }
6512 }
6513
6514 /* no preference, select some channel */
6515 wpas_p2p_select_go_freq_no_pref(wpa_s, params, channels);
6516
6517 if (params->freq == 0) {
6518 wpa_printf(MSG_DEBUG, "P2P: did not find a freq for GO use");
6519 goto fail;
6520 }
6521
6522success:
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07006523 os_free(freqs);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006524 return 0;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006525fail:
6526 os_free(freqs);
6527 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006528}
6529
6530
6531static struct wpa_supplicant *
6532wpas_p2p_get_group_iface(struct wpa_supplicant *wpa_s, int addr_allocated,
6533 int go)
6534{
6535 struct wpa_supplicant *group_wpa_s;
6536
Dmitry Shmidtaa532512012-09-24 10:35:31 -07006537 if (!wpas_p2p_create_iface(wpa_s)) {
Dmitry Shmidt9c175262016-03-03 10:20:07 -08006538 if (wpa_s->p2p_mgmt) {
6539 /*
6540 * We may be called on the p2p_dev interface which
6541 * cannot be used for group operations, so always use
6542 * the primary interface.
6543 */
6544 wpa_s->parent->p2pdev = wpa_s;
6545 wpa_s = wpa_s->parent;
6546 }
6547 wpa_dbg(wpa_s, MSG_DEBUG,
6548 "P2P: Use primary interface for group operations");
Dmitry Shmidt56052862013-10-04 10:23:25 -07006549 wpa_s->p2p_first_connection_timeout = 0;
Dmitry Shmidt9c175262016-03-03 10:20:07 -08006550 if (wpa_s != wpa_s->p2pdev)
Dmitry Shmidt849734c2016-05-27 09:59:01 -07006551 wpas_p2p_clone_config(wpa_s, wpa_s->p2pdev);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006552 return wpa_s;
Dmitry Shmidtaa532512012-09-24 10:35:31 -07006553 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006554
6555 if (wpas_p2p_add_group_interface(wpa_s, go ? WPA_IF_P2P_GO :
Dmitry Shmidtaa532512012-09-24 10:35:31 -07006556 WPA_IF_P2P_CLIENT) < 0) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07006557 wpa_msg_global(wpa_s, MSG_ERROR,
6558 "P2P: Failed to add group interface");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006559 return NULL;
Dmitry Shmidtaa532512012-09-24 10:35:31 -07006560 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006561 group_wpa_s = wpas_p2p_init_group_interface(wpa_s, go);
6562 if (group_wpa_s == NULL) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07006563 wpa_msg_global(wpa_s, MSG_ERROR,
6564 "P2P: Failed to initialize group interface");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006565 wpas_p2p_remove_pending_group_interface(wpa_s);
6566 return NULL;
6567 }
6568
Roshan Pius3a1667e2018-07-03 15:17:14 -07006569 if (go && wpa_s->p2p_go_do_acs) {
6570 group_wpa_s->p2p_go_do_acs = wpa_s->p2p_go_do_acs;
6571 group_wpa_s->p2p_go_acs_band = wpa_s->p2p_go_acs_band;
6572 wpa_s->p2p_go_do_acs = 0;
6573 }
6574
Dmitry Shmidtaa532512012-09-24 10:35:31 -07006575 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Use separate group interface %s",
6576 group_wpa_s->ifname);
Dmitry Shmidt56052862013-10-04 10:23:25 -07006577 group_wpa_s->p2p_first_connection_timeout = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006578 return group_wpa_s;
6579}
6580
6581
6582/**
6583 * wpas_p2p_group_add - Add a new P2P group with local end as Group Owner
6584 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
6585 * @persistent_group: Whether to create a persistent group
6586 * @freq: Frequency for the group or 0 to indicate no hardcoding
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08006587 * @vht_center_freq2: segment_1 center frequency for GO operating in VHT 80P80
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07006588 * @ht40: Start GO with 40 MHz channel width
6589 * @vht: Start GO with VHT support
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08006590 * @vht_chwidth: channel bandwidth for GO operating with VHT support
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08006591 * @edmg: Start GO with EDMG support
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006592 * Returns: 0 on success, -1 on failure
6593 *
6594 * This function creates a new P2P group with the local end as the Group Owner,
6595 * i.e., without using Group Owner Negotiation.
6596 */
6597int wpas_p2p_group_add(struct wpa_supplicant *wpa_s, int persistent_group,
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08006598 int freq, int vht_center_freq2, int ht40, int vht,
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08006599 int max_oper_chwidth, int he, int edmg)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006600{
6601 struct p2p_go_neg_results params;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006602
6603 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
6604 return -1;
6605
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07006606 os_free(wpa_s->global->add_psk);
6607 wpa_s->global->add_psk = NULL;
6608
Dmitry Shmidtdca39792011-09-06 11:17:33 -07006609 /* Make sure we are not running find during connection establishment */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006610 wpa_printf(MSG_DEBUG, "P2P: Stop any on-going P2P FIND");
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08006611 wpas_p2p_stop_find_oper(wpa_s);
Dmitry Shmidtdca39792011-09-06 11:17:33 -07006612
Roshan Pius3a1667e2018-07-03 15:17:14 -07006613 if (!wpa_s->p2p_go_do_acs) {
6614 freq = wpas_p2p_select_go_freq(wpa_s, freq);
6615 if (freq < 0)
6616 return -1;
6617 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006618
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08006619 if (wpas_p2p_init_go_params(wpa_s, &params, freq, vht_center_freq2,
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08006620 ht40, vht, max_oper_chwidth, he, edmg,
6621 NULL))
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006622 return -1;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07006623
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006624 p2p_go_params(wpa_s->global->p2p, &params);
6625 params.persistent_group = persistent_group;
6626
6627 wpa_s = wpas_p2p_get_group_iface(wpa_s, 0, 1);
6628 if (wpa_s == NULL)
6629 return -1;
6630 wpas_start_wps_go(wpa_s, &params, 0);
6631
6632 return 0;
6633}
6634
6635
6636static int wpas_start_p2p_client(struct wpa_supplicant *wpa_s,
Dmitry Shmidt15907092014-03-25 10:42:57 -07006637 struct wpa_ssid *params, int addr_allocated,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006638 int freq, int force_scan)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006639{
6640 struct wpa_ssid *ssid;
6641
6642 wpa_s = wpas_p2p_get_group_iface(wpa_s, addr_allocated, 0);
6643 if (wpa_s == NULL)
6644 return -1;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006645 if (force_scan)
6646 os_get_reltime(&wpa_s->scan_min_time);
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07006647 wpa_s->p2p_last_4way_hs_fail = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006648
6649 wpa_supplicant_ap_deinit(wpa_s);
6650
6651 ssid = wpa_config_add_network(wpa_s->conf);
6652 if (ssid == NULL)
6653 return -1;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006654 os_memset(wpa_s->go_dev_addr, 0, ETH_ALEN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006655 wpa_config_set_network_defaults(ssid);
6656 ssid->temporary = 1;
6657 ssid->proto = WPA_PROTO_RSN;
Dmitry Shmidte4663042016-04-04 10:07:49 -07006658 ssid->pbss = params->pbss;
6659 ssid->pairwise_cipher = params->pbss ? WPA_CIPHER_GCMP :
6660 WPA_CIPHER_CCMP;
6661 ssid->group_cipher = params->pbss ? WPA_CIPHER_GCMP : WPA_CIPHER_CCMP;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006662 ssid->key_mgmt = WPA_KEY_MGMT_PSK;
6663 ssid->ssid = os_malloc(params->ssid_len);
6664 if (ssid->ssid == NULL) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006665 wpa_config_remove_network(wpa_s->conf, ssid->id);
6666 return -1;
6667 }
6668 os_memcpy(ssid->ssid, params->ssid, params->ssid_len);
6669 ssid->ssid_len = params->ssid_len;
6670 ssid->p2p_group = 1;
6671 ssid->export_keys = 1;
6672 if (params->psk_set) {
6673 os_memcpy(ssid->psk, params->psk, 32);
6674 ssid->psk_set = 1;
6675 }
6676 if (params->passphrase)
6677 ssid->passphrase = os_strdup(params->passphrase);
6678
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006679 wpa_s->show_group_started = 1;
Dmitry Shmidt15907092014-03-25 10:42:57 -07006680 wpa_s->p2p_in_invitation = 1;
6681 wpa_s->p2p_invite_go_freq = freq;
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08006682 wpa_s->p2p_go_group_formation_completed = 0;
6683 wpa_s->global->p2p_group_formation = wpa_s;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006684
Dmitry Shmidt9c175262016-03-03 10:20:07 -08006685 eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s->p2pdev,
Dmitry Shmidt15907092014-03-25 10:42:57 -07006686 NULL);
6687 eloop_register_timeout(P2P_MAX_INITIAL_CONN_WAIT, 0,
6688 wpas_p2p_group_formation_timeout,
Dmitry Shmidt9c175262016-03-03 10:20:07 -08006689 wpa_s->p2pdev, NULL);
Dmitry Shmidtfa3fc4a2013-11-21 13:34:38 -08006690 wpa_supplicant_select_network(wpa_s, ssid);
6691
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006692 return 0;
6693}
6694
6695
6696int wpas_p2p_group_add_persistent(struct wpa_supplicant *wpa_s,
6697 struct wpa_ssid *ssid, int addr_allocated,
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08006698 int force_freq, int neg_freq,
6699 int vht_center_freq2, int ht40,
Hai Shalom74f70d42019-02-11 14:42:39 -08006700 int vht, int max_oper_chwidth, int he,
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08006701 int edmg,
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08006702 const struct p2p_channels *channels,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006703 int connection_timeout, int force_scan)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006704{
6705 struct p2p_go_neg_results params;
Dmitry Shmidt96be6222014-02-13 10:16:51 -08006706 int go = 0, freq;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006707
6708 if (ssid->disabled != 2 || ssid->ssid == NULL)
6709 return -1;
6710
6711 if (wpas_get_p2p_group(wpa_s, ssid->ssid, ssid->ssid_len, &go) &&
6712 go == (ssid->mode == WPAS_MODE_P2P_GO)) {
6713 wpa_printf(MSG_DEBUG, "P2P: Requested persistent group is "
6714 "already running");
Dmitry Shmidtb1e52102015-05-29 12:36:29 -07006715 if (go == 0 &&
6716 eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
Dmitry Shmidt9c175262016-03-03 10:20:07 -08006717 wpa_s->p2pdev, NULL)) {
Dmitry Shmidtb1e52102015-05-29 12:36:29 -07006718 /*
6719 * This can happen if Invitation Response frame was lost
6720 * and the peer (GO of a persistent group) tries to
6721 * invite us again. Reschedule the timeout to avoid
6722 * terminating the wait for the connection too early
6723 * since we now know that the peer is still trying to
6724 * invite us instead of having already started the GO.
6725 */
6726 wpa_printf(MSG_DEBUG,
6727 "P2P: Reschedule group formation timeout since peer is still trying to invite us");
6728 eloop_register_timeout(P2P_MAX_INITIAL_CONN_WAIT, 0,
6729 wpas_p2p_group_formation_timeout,
Dmitry Shmidt9c175262016-03-03 10:20:07 -08006730 wpa_s->p2pdev, NULL);
Dmitry Shmidtb1e52102015-05-29 12:36:29 -07006731 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006732 return 0;
6733 }
6734
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07006735 os_free(wpa_s->global->add_psk);
6736 wpa_s->global->add_psk = NULL;
6737
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006738 /* Make sure we are not running find during connection establishment */
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08006739 wpas_p2p_stop_find_oper(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006740
Dmitry Shmidt04949592012-07-19 12:16:46 -07006741 wpa_s->p2p_fallback_to_go_neg = 0;
6742
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006743 if (ssid->mode == WPAS_MODE_P2P_GO) {
6744 if (force_freq > 0) {
6745 freq = wpas_p2p_select_go_freq(wpa_s, force_freq);
6746 if (freq < 0)
6747 return -1;
6748 } else {
6749 freq = wpas_p2p_select_go_freq(wpa_s, neg_freq);
6750 if (freq < 0 ||
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006751 (freq > 0 && !freq_included(wpa_s, channels, freq)))
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006752 freq = 0;
6753 }
Dmitry Shmidt912c6ec2015-03-30 13:16:51 -07006754 } else if (ssid->mode == WPAS_MODE_INFRA) {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006755 freq = neg_freq;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006756 if (freq <= 0 || !freq_included(wpa_s, channels, freq)) {
Dmitry Shmidt912c6ec2015-03-30 13:16:51 -07006757 struct os_reltime now;
6758 struct wpa_bss *bss =
6759 wpa_bss_get_p2p_dev_addr(wpa_s, ssid->bssid);
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07006760
Dmitry Shmidt912c6ec2015-03-30 13:16:51 -07006761 os_get_reltime(&now);
6762 if (bss &&
6763 !os_reltime_expired(&now, &bss->last_update, 5) &&
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006764 freq_included(wpa_s, channels, bss->freq))
Dmitry Shmidt912c6ec2015-03-30 13:16:51 -07006765 freq = bss->freq;
6766 else
6767 freq = 0;
6768 }
6769
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006770 return wpas_start_p2p_client(wpa_s, ssid, addr_allocated, freq,
6771 force_scan);
Dmitry Shmidt912c6ec2015-03-30 13:16:51 -07006772 } else {
Dmitry Shmidt15907092014-03-25 10:42:57 -07006773 return -1;
Dmitry Shmidt912c6ec2015-03-30 13:16:51 -07006774 }
Dmitry Shmidt15907092014-03-25 10:42:57 -07006775
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08006776 if (wpas_p2p_init_go_params(wpa_s, &params, freq, vht_center_freq2,
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08006777 ht40, vht, max_oper_chwidth, he, edmg,
6778 channels))
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006779 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006780
6781 params.role_go = 1;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08006782 params.psk_set = ssid->psk_set;
6783 if (params.psk_set)
6784 os_memcpy(params.psk, ssid->psk, sizeof(params.psk));
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08006785 if (ssid->passphrase) {
6786 if (os_strlen(ssid->passphrase) >= sizeof(params.passphrase)) {
6787 wpa_printf(MSG_ERROR, "P2P: Invalid passphrase in "
6788 "persistent group");
6789 return -1;
6790 }
6791 os_strlcpy(params.passphrase, ssid->passphrase,
6792 sizeof(params.passphrase));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006793 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006794 os_memcpy(params.ssid, ssid->ssid, ssid->ssid_len);
6795 params.ssid_len = ssid->ssid_len;
6796 params.persistent_group = 1;
6797
6798 wpa_s = wpas_p2p_get_group_iface(wpa_s, addr_allocated, 1);
6799 if (wpa_s == NULL)
6800 return -1;
6801
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006802 p2p_channels_to_freqs(channels, params.freq_list, P2P_MAX_CHANNELS);
6803
Dmitry Shmidt56052862013-10-04 10:23:25 -07006804 wpa_s->p2p_first_connection_timeout = connection_timeout;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006805 wpas_start_wps_go(wpa_s, &params, 0);
6806
6807 return 0;
6808}
6809
6810
6811static void wpas_p2p_ie_update(void *ctx, struct wpabuf *beacon_ies,
6812 struct wpabuf *proberesp_ies)
6813{
6814 struct wpa_supplicant *wpa_s = ctx;
6815 if (wpa_s->ap_iface) {
6816 struct hostapd_data *hapd = wpa_s->ap_iface->bss[0];
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006817 if (!(hapd->conf->p2p & P2P_GROUP_OWNER)) {
6818 wpabuf_free(beacon_ies);
6819 wpabuf_free(proberesp_ies);
6820 return;
6821 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006822 if (beacon_ies) {
6823 wpabuf_free(hapd->p2p_beacon_ie);
6824 hapd->p2p_beacon_ie = beacon_ies;
6825 }
6826 wpabuf_free(hapd->p2p_probe_resp_ie);
6827 hapd->p2p_probe_resp_ie = proberesp_ies;
6828 } else {
6829 wpabuf_free(beacon_ies);
6830 wpabuf_free(proberesp_ies);
6831 }
6832 wpa_supplicant_ap_update_beacon(wpa_s);
6833}
6834
6835
6836static void wpas_p2p_idle_update(void *ctx, int idle)
6837{
6838 struct wpa_supplicant *wpa_s = ctx;
6839 if (!wpa_s->ap_iface)
6840 return;
6841 wpa_printf(MSG_DEBUG, "P2P: GO - group %sidle", idle ? "" : "not ");
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08006842 if (idle) {
6843 if (wpa_s->global->p2p_fail_on_wps_complete &&
6844 wpa_s->p2p_in_provisioning) {
6845 wpas_p2p_grpform_fail_after_wps(wpa_s);
6846 return;
6847 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006848 wpas_p2p_set_group_idle_timeout(wpa_s);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08006849 } else
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006850 eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL);
6851}
6852
6853
6854struct p2p_group * wpas_p2p_group_init(struct wpa_supplicant *wpa_s,
Dmitry Shmidt04949592012-07-19 12:16:46 -07006855 struct wpa_ssid *ssid)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006856{
6857 struct p2p_group *group;
6858 struct p2p_group_config *cfg;
6859
Dmitry Shmidt849734c2016-05-27 09:59:01 -07006860 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL ||
6861 !ssid->p2p_group)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006862 return NULL;
6863
6864 cfg = os_zalloc(sizeof(*cfg));
6865 if (cfg == NULL)
6866 return NULL;
6867
Dmitry Shmidt04949592012-07-19 12:16:46 -07006868 if (ssid->p2p_persistent_group && wpa_s->conf->persistent_reconnect)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006869 cfg->persistent_group = 2;
Dmitry Shmidt04949592012-07-19 12:16:46 -07006870 else if (ssid->p2p_persistent_group)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006871 cfg->persistent_group = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006872 os_memcpy(cfg->interface_addr, wpa_s->own_addr, ETH_ALEN);
6873 if (wpa_s->max_stations &&
6874 wpa_s->max_stations < wpa_s->conf->max_num_sta)
6875 cfg->max_clients = wpa_s->max_stations;
6876 else
6877 cfg->max_clients = wpa_s->conf->max_num_sta;
Dmitry Shmidt04949592012-07-19 12:16:46 -07006878 os_memcpy(cfg->ssid, ssid->ssid, ssid->ssid_len);
6879 cfg->ssid_len = ssid->ssid_len;
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08006880 cfg->freq = ssid->frequency;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006881 cfg->cb_ctx = wpa_s;
6882 cfg->ie_update = wpas_p2p_ie_update;
6883 cfg->idle_update = wpas_p2p_idle_update;
Dmitry Shmidte4663042016-04-04 10:07:49 -07006884 cfg->ip_addr_alloc = WPA_GET_BE32(wpa_s->p2pdev->conf->ip_addr_start)
6885 != 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006886
6887 group = p2p_group_init(wpa_s->global->p2p, cfg);
6888 if (group == NULL)
6889 os_free(cfg);
Dmitry Shmidt04949592012-07-19 12:16:46 -07006890 if (ssid->mode != WPAS_MODE_P2P_GROUP_FORMATION)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006891 p2p_group_notif_formation_done(group);
6892 wpa_s->p2p_group = group;
6893 return group;
6894}
6895
6896
6897void wpas_p2p_wps_success(struct wpa_supplicant *wpa_s, const u8 *peer_addr,
6898 int registrar)
6899{
Dmitry Shmidt04949592012-07-19 12:16:46 -07006900 struct wpa_ssid *ssid = wpa_s->current_ssid;
6901
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006902 if (!wpa_s->p2p_in_provisioning) {
6903 wpa_printf(MSG_DEBUG, "P2P: Ignore WPS success event - P2P "
6904 "provisioning not in progress");
6905 return;
6906 }
6907
Dmitry Shmidt04949592012-07-19 12:16:46 -07006908 if (ssid && ssid->mode == WPAS_MODE_INFRA) {
6909 u8 go_dev_addr[ETH_ALEN];
6910 os_memcpy(go_dev_addr, wpa_s->bssid, ETH_ALEN);
6911 wpas_p2p_persistent_group(wpa_s, go_dev_addr, ssid->ssid,
6912 ssid->ssid_len);
6913 /* Clear any stored provisioning info */
6914 p2p_clear_provisioning_info(wpa_s->global->p2p, go_dev_addr);
6915 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006916
Dmitry Shmidt9c175262016-03-03 10:20:07 -08006917 eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s->p2pdev,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006918 NULL);
Dmitry Shmidt56052862013-10-04 10:23:25 -07006919 wpa_s->p2p_go_group_formation_completed = 1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07006920 if (ssid && ssid->mode == WPAS_MODE_INFRA) {
6921 /*
6922 * Use a separate timeout for initial data connection to
6923 * complete to allow the group to be removed automatically if
6924 * something goes wrong in this step before the P2P group idle
6925 * timeout mechanism is taken into use.
6926 */
Dmitry Shmidt56052862013-10-04 10:23:25 -07006927 wpa_dbg(wpa_s, MSG_DEBUG,
6928 "P2P: Re-start group formation timeout (%d seconds) as client for initial connection",
6929 P2P_MAX_INITIAL_CONN_WAIT);
Dmitry Shmidt04949592012-07-19 12:16:46 -07006930 eloop_register_timeout(P2P_MAX_INITIAL_CONN_WAIT, 0,
6931 wpas_p2p_group_formation_timeout,
Dmitry Shmidt9c175262016-03-03 10:20:07 -08006932 wpa_s->p2pdev, NULL);
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08006933 /* Complete group formation on successful data connection. */
6934 wpa_s->p2p_go_group_formation_completed = 0;
Dmitry Shmidt56052862013-10-04 10:23:25 -07006935 } else if (ssid) {
6936 /*
6937 * Use a separate timeout for initial data connection to
6938 * complete to allow the group to be removed automatically if
6939 * the client does not complete data connection successfully.
6940 */
6941 wpa_dbg(wpa_s, MSG_DEBUG,
6942 "P2P: Re-start group formation timeout (%d seconds) as GO for initial connection",
6943 P2P_MAX_INITIAL_CONN_WAIT_GO);
6944 eloop_register_timeout(P2P_MAX_INITIAL_CONN_WAIT_GO, 0,
6945 wpas_p2p_group_formation_timeout,
Dmitry Shmidt9c175262016-03-03 10:20:07 -08006946 wpa_s->p2pdev, NULL);
Dmitry Shmidt56052862013-10-04 10:23:25 -07006947 /*
6948 * Complete group formation on first successful data connection
6949 */
6950 wpa_s->p2p_go_group_formation_completed = 0;
Dmitry Shmidt04949592012-07-19 12:16:46 -07006951 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006952 if (wpa_s->global->p2p)
6953 p2p_wps_success_cb(wpa_s->global->p2p, peer_addr);
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07006954 wpas_group_formation_completed(wpa_s, 1, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006955}
6956
6957
Jouni Malinen75ecf522011-06-27 15:19:46 -07006958void wpas_p2p_wps_failed(struct wpa_supplicant *wpa_s,
6959 struct wps_event_fail *fail)
6960{
6961 if (!wpa_s->p2p_in_provisioning) {
6962 wpa_printf(MSG_DEBUG, "P2P: Ignore WPS fail event - P2P "
6963 "provisioning not in progress");
6964 return;
6965 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006966
6967 if (wpa_s->go_params) {
6968 p2p_clear_provisioning_info(
6969 wpa_s->global->p2p,
Dmitry Shmidt04949592012-07-19 12:16:46 -07006970 wpa_s->go_params->peer_device_addr);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006971 }
6972
Jouni Malinen75ecf522011-06-27 15:19:46 -07006973 wpas_notify_p2p_wps_failed(wpa_s, fail);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08006974
6975 if (wpa_s == wpa_s->global->p2p_group_formation) {
6976 /*
6977 * Allow some time for the failed WPS negotiation exchange to
6978 * complete, but remove the group since group formation cannot
6979 * succeed after provisioning failure.
6980 */
6981 wpa_printf(MSG_DEBUG, "P2P: WPS step failed during group formation - reject connection from timeout");
6982 wpa_s->global->p2p_fail_on_wps_complete = 1;
6983 eloop_deplete_timeout(0, 50000,
6984 wpas_p2p_group_formation_timeout,
Dmitry Shmidt9c175262016-03-03 10:20:07 -08006985 wpa_s->p2pdev, NULL);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08006986 }
6987}
6988
6989
6990int wpas_p2p_wps_eapol_cb(struct wpa_supplicant *wpa_s)
6991{
6992 if (!wpa_s->global->p2p_fail_on_wps_complete ||
6993 !wpa_s->p2p_in_provisioning)
6994 return 0;
6995
6996 wpas_p2p_grpform_fail_after_wps(wpa_s);
6997
6998 return 1;
Jouni Malinen75ecf522011-06-27 15:19:46 -07006999}
7000
7001
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007002int wpas_p2p_prov_disc(struct wpa_supplicant *wpa_s, const u8 *peer_addr,
Dmitry Shmidt04949592012-07-19 12:16:46 -07007003 const char *config_method,
Dmitry Shmidt216983b2015-02-06 10:50:36 -08007004 enum wpas_p2p_prov_disc_use use,
7005 struct p2ps_provision *p2ps_prov)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007006{
7007 u16 config_methods;
7008
Dmitry Shmidt216983b2015-02-06 10:50:36 -08007009 wpa_s->global->pending_p2ps_group = 0;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08007010 wpa_s->global->pending_p2ps_group_freq = 0;
Dmitry Shmidt04949592012-07-19 12:16:46 -07007011 wpa_s->p2p_fallback_to_go_neg = 0;
7012 wpa_s->pending_pd_use = NORMAL_PD;
Dmitry Shmidt216983b2015-02-06 10:50:36 -08007013 if (p2ps_prov && use == WPAS_P2P_PD_FOR_ASP) {
7014 p2ps_prov->conncap = p2ps_group_capability(
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08007015 wpa_s, P2PS_SETUP_NONE, p2ps_prov->role,
7016 &p2ps_prov->force_freq, &p2ps_prov->pref_freq);
7017
Dmitry Shmidt216983b2015-02-06 10:50:36 -08007018 wpa_printf(MSG_DEBUG,
7019 "P2P: %s conncap: %d - ASP parsed: %x %x %d %s",
7020 __func__, p2ps_prov->conncap,
7021 p2ps_prov->adv_id, p2ps_prov->conncap,
7022 p2ps_prov->status, p2ps_prov->info);
7023
7024 config_methods = 0;
7025 } else if (os_strncmp(config_method, "display", 7) == 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007026 config_methods = WPS_CONFIG_DISPLAY;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007027 else if (os_strncmp(config_method, "keypad", 6) == 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007028 config_methods = WPS_CONFIG_KEYPAD;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007029 else if (os_strncmp(config_method, "pbc", 3) == 0 ||
7030 os_strncmp(config_method, "pushbutton", 10) == 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007031 config_methods = WPS_CONFIG_PUSHBUTTON;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007032 else {
7033 wpa_printf(MSG_DEBUG, "P2P: Unknown config method");
Dmitry Shmidt216983b2015-02-06 10:50:36 -08007034 os_free(p2ps_prov);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007035 return -1;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007036 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007037
Dmitry Shmidt04949592012-07-19 12:16:46 -07007038 if (use == WPAS_P2P_PD_AUTO) {
7039 os_memcpy(wpa_s->pending_join_dev_addr, peer_addr, ETH_ALEN);
7040 wpa_s->pending_pd_config_methods = config_methods;
7041 wpa_s->p2p_auto_pd = 1;
7042 wpa_s->p2p_auto_join = 0;
7043 wpa_s->pending_pd_before_join = 0;
7044 wpa_s->auto_pd_scan_retry = 0;
7045 wpas_p2p_stop_find(wpa_s);
7046 wpa_s->p2p_join_scan_count = 0;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08007047 os_get_reltime(&wpa_s->p2p_auto_started);
Dmitry Shmidt04949592012-07-19 12:16:46 -07007048 wpa_printf(MSG_DEBUG, "P2P: Auto PD started at %ld.%06ld",
7049 wpa_s->p2p_auto_started.sec,
7050 wpa_s->p2p_auto_started.usec);
7051 wpas_p2p_join_scan(wpa_s, NULL);
7052 return 0;
7053 }
7054
Dmitry Shmidt216983b2015-02-06 10:50:36 -08007055 if (wpa_s->global->p2p == NULL || wpa_s->global->p2p_disabled) {
7056 os_free(p2ps_prov);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007057 return -1;
Dmitry Shmidt216983b2015-02-06 10:50:36 -08007058 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007059
Dmitry Shmidt216983b2015-02-06 10:50:36 -08007060 return p2p_prov_disc_req(wpa_s->global->p2p, peer_addr, p2ps_prov,
Dmitry Shmidt04949592012-07-19 12:16:46 -07007061 config_methods, use == WPAS_P2P_PD_FOR_JOIN,
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08007062 0, 1);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007063}
7064
7065
7066int wpas_p2p_scan_result_text(const u8 *ies, size_t ies_len, char *buf,
7067 char *end)
7068{
7069 return p2p_scan_result_text(ies, ies_len, buf, end);
7070}
7071
7072
7073static void wpas_p2p_clear_pending_action_tx(struct wpa_supplicant *wpa_s)
7074{
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08007075 if (!offchannel_pending_action_tx(wpa_s))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007076 return;
7077
Dmitry Shmidtde47be72016-01-07 12:52:55 -08007078 if (wpa_s->p2p_send_action_work) {
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08007079 wpas_p2p_free_send_action_work(wpa_s);
Dmitry Shmidtde47be72016-01-07 12:52:55 -08007080 eloop_cancel_timeout(wpas_p2p_send_action_work_timeout,
7081 wpa_s, NULL);
7082 offchannel_send_action_done(wpa_s);
7083 }
Dmitry Shmidt03658832014-08-13 11:03:49 -07007084
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007085 wpa_printf(MSG_DEBUG, "P2P: Drop pending Action TX due to new "
7086 "operation request");
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08007087 offchannel_clear_pending_action_tx(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007088}
7089
7090
7091int wpas_p2p_find(struct wpa_supplicant *wpa_s, unsigned int timeout,
7092 enum p2p_discovery_type type,
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08007093 unsigned int num_req_dev_types, const u8 *req_dev_types,
Dmitry Shmidt216983b2015-02-06 10:50:36 -08007094 const u8 *dev_id, unsigned int search_delay,
Dmitry Shmidt203eadb2015-03-05 14:16:04 -08007095 u8 seek_cnt, const char **seek_string, int freq)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007096{
7097 wpas_p2p_clear_pending_action_tx(wpa_s);
Hai Shalomfdcde762020-04-02 11:19:20 -07007098 wpa_s->global->p2p_long_listen = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007099
Dmitry Shmidt04949592012-07-19 12:16:46 -07007100 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL ||
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07007101 wpa_s->p2p_in_provisioning) {
7102 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Reject p2p_find operation%s%s",
7103 (wpa_s->global->p2p_disabled || !wpa_s->global->p2p) ?
7104 " (P2P disabled)" : "",
7105 wpa_s->p2p_in_provisioning ?
7106 " (p2p_in_provisioning)" : "");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007107 return -1;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07007108 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007109
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08007110 wpa_supplicant_cancel_sched_scan(wpa_s);
7111
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007112 return p2p_find(wpa_s->global->p2p, timeout, type,
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07007113 num_req_dev_types, req_dev_types, dev_id,
Dmitry Shmidt203eadb2015-03-05 14:16:04 -08007114 search_delay, seek_cnt, seek_string, freq);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007115}
7116
7117
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007118static void wpas_p2p_scan_res_ignore_search(struct wpa_supplicant *wpa_s,
7119 struct wpa_scan_results *scan_res)
7120{
7121 wpa_printf(MSG_DEBUG, "P2P: Ignore scan results");
7122
7123 if (wpa_s->p2p_scan_work) {
7124 struct wpa_radio_work *work = wpa_s->p2p_scan_work;
7125 wpa_s->p2p_scan_work = NULL;
7126 radio_work_done(work);
7127 }
7128
7129 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
7130 return;
7131
7132 /*
7133 * Indicate that results have been processed so that the P2P module can
7134 * continue pending tasks.
7135 */
7136 p2p_scan_res_handled(wpa_s->global->p2p);
7137}
7138
7139
7140static void wpas_p2p_stop_find_oper(struct wpa_supplicant *wpa_s)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007141{
7142 wpas_p2p_clear_pending_action_tx(wpa_s);
Hai Shalomfdcde762020-04-02 11:19:20 -07007143 wpa_s->global->p2p_long_listen = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007144 eloop_cancel_timeout(wpas_p2p_long_listen_timeout, wpa_s, NULL);
7145 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007146
7147 if (wpa_s->global->p2p)
7148 p2p_stop_find(wpa_s->global->p2p);
7149
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007150 if (wpa_s->scan_res_handler == wpas_p2p_scan_res_handler) {
7151 wpa_printf(MSG_DEBUG,
7152 "P2P: Do not consider the scan results after stop_find");
7153 wpa_s->scan_res_handler = wpas_p2p_scan_res_ignore_search;
7154 }
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08007155}
7156
7157
7158void wpas_p2p_stop_find(struct wpa_supplicant *wpa_s)
7159{
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007160 wpas_p2p_stop_find_oper(wpa_s);
Dmitry Shmidt216983b2015-02-06 10:50:36 -08007161 if (!wpa_s->global->pending_group_iface_for_p2ps)
7162 wpas_p2p_remove_pending_group_interface(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007163}
7164
7165
7166static void wpas_p2p_long_listen_timeout(void *eloop_ctx, void *timeout_ctx)
7167{
7168 struct wpa_supplicant *wpa_s = eloop_ctx;
Hai Shalomfdcde762020-04-02 11:19:20 -07007169 wpa_s->global->p2p_long_listen = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007170}
7171
7172
7173int wpas_p2p_listen(struct wpa_supplicant *wpa_s, unsigned int timeout)
7174{
7175 int res;
7176
7177 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
7178 return -1;
7179
Dmitry Shmidt58d12ad2016-07-28 10:07:03 -07007180 if (wpa_s->p2p_lo_started) {
7181 wpa_printf(MSG_DEBUG,
7182 "P2P: Cannot start P2P listen, it is offloaded");
7183 return -1;
7184 }
7185
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08007186 wpa_supplicant_cancel_sched_scan(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007187 wpas_p2p_clear_pending_action_tx(wpa_s);
7188
7189 if (timeout == 0) {
7190 /*
7191 * This is a request for unlimited Listen state. However, at
7192 * least for now, this is mapped to a Listen state for one
7193 * hour.
7194 */
7195 timeout = 3600;
7196 }
7197 eloop_cancel_timeout(wpas_p2p_long_listen_timeout, wpa_s, NULL);
Hai Shalomfdcde762020-04-02 11:19:20 -07007198 wpa_s->global->p2p_long_listen = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007199
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007200 /*
7201 * Stop previous find/listen operation to avoid trying to request a new
7202 * remain-on-channel operation while the driver is still running the
7203 * previous one.
7204 */
7205 if (wpa_s->global->p2p)
7206 p2p_stop_find(wpa_s->global->p2p);
7207
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007208 res = wpas_p2p_listen_start(wpa_s, timeout * 1000);
7209 if (res == 0 && timeout * 1000 > wpa_s->max_remain_on_chan) {
Hai Shalomfdcde762020-04-02 11:19:20 -07007210 wpa_s->global->p2p_long_listen = timeout * 1000;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007211 eloop_register_timeout(timeout, 0,
7212 wpas_p2p_long_listen_timeout,
7213 wpa_s, NULL);
7214 }
7215
7216 return res;
7217}
7218
7219
7220int wpas_p2p_assoc_req_ie(struct wpa_supplicant *wpa_s, struct wpa_bss *bss,
7221 u8 *buf, size_t len, int p2p_group)
7222{
7223 struct wpabuf *p2p_ie;
7224 int ret;
7225
7226 if (wpa_s->global->p2p_disabled)
7227 return -1;
Dmitry Shmidt8bd70b72015-05-26 16:02:19 -07007228 /*
7229 * Advertize mandatory cross connection capability even on
7230 * p2p_disabled=1 interface when associating with a P2P Manager WLAN AP.
7231 */
7232 if (wpa_s->conf->p2p_disabled && p2p_group)
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07007233 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007234 if (wpa_s->global->p2p == NULL)
7235 return -1;
7236 if (bss == NULL)
7237 return -1;
7238
7239 p2p_ie = wpa_bss_get_vendor_ie_multi(bss, P2P_IE_VENDOR_TYPE);
7240 ret = p2p_assoc_req_ie(wpa_s->global->p2p, bss->bssid, buf, len,
7241 p2p_group, p2p_ie);
7242 wpabuf_free(p2p_ie);
7243
7244 return ret;
7245}
7246
7247
7248int wpas_p2p_probe_req_rx(struct wpa_supplicant *wpa_s, const u8 *addr,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007249 const u8 *dst, const u8 *bssid,
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07007250 const u8 *ie, size_t ie_len,
7251 unsigned int rx_freq, int ssi_signal)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007252{
7253 if (wpa_s->global->p2p_disabled)
7254 return 0;
7255 if (wpa_s->global->p2p == NULL)
7256 return 0;
7257
Dmitry Shmidt04949592012-07-19 12:16:46 -07007258 switch (p2p_probe_req_rx(wpa_s->global->p2p, addr, dst, bssid,
Dmitry Shmidt58d12ad2016-07-28 10:07:03 -07007259 ie, ie_len, rx_freq, wpa_s->p2p_lo_started)) {
Dmitry Shmidt04949592012-07-19 12:16:46 -07007260 case P2P_PREQ_NOT_P2P:
7261 wpas_notify_preq(wpa_s, addr, dst, bssid, ie, ie_len,
7262 ssi_signal);
7263 /* fall through */
7264 case P2P_PREQ_MALFORMED:
7265 case P2P_PREQ_NOT_LISTEN:
7266 case P2P_PREQ_NOT_PROCESSED:
7267 default: /* make gcc happy */
7268 return 0;
7269 case P2P_PREQ_PROCESSED:
7270 return 1;
7271 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007272}
7273
7274
7275void wpas_p2p_rx_action(struct wpa_supplicant *wpa_s, const u8 *da,
7276 const u8 *sa, const u8 *bssid,
7277 u8 category, const u8 *data, size_t len, int freq)
7278{
7279 if (wpa_s->global->p2p_disabled)
7280 return;
7281 if (wpa_s->global->p2p == NULL)
7282 return;
7283
7284 p2p_rx_action(wpa_s->global->p2p, da, sa, bssid, category, data, len,
7285 freq);
7286}
7287
7288
7289void wpas_p2p_scan_ie(struct wpa_supplicant *wpa_s, struct wpabuf *ies)
7290{
Dmitry Shmidt9c175262016-03-03 10:20:07 -08007291 unsigned int bands;
7292
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007293 if (wpa_s->global->p2p_disabled)
7294 return;
7295 if (wpa_s->global->p2p == NULL)
7296 return;
7297
Dmitry Shmidt9c175262016-03-03 10:20:07 -08007298 bands = wpas_get_bands(wpa_s, NULL);
7299 p2p_scan_ie(wpa_s->global->p2p, ies, NULL, bands);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007300}
7301
7302
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07007303static void wpas_p2p_group_deinit(struct wpa_supplicant *wpa_s)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007304{
7305 p2p_group_deinit(wpa_s->p2p_group);
7306 wpa_s->p2p_group = NULL;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007307
7308 wpa_s->ap_configured_cb = NULL;
7309 wpa_s->ap_configured_cb_ctx = NULL;
7310 wpa_s->ap_configured_cb_data = NULL;
7311 wpa_s->connect_without_scan = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007312}
7313
7314
7315int wpas_p2p_reject(struct wpa_supplicant *wpa_s, const u8 *addr)
7316{
Hai Shalomfdcde762020-04-02 11:19:20 -07007317 wpa_s->global->p2p_long_listen = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007318
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007319 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
7320 return -1;
7321
7322 return p2p_reject(wpa_s->global->p2p, addr);
7323}
7324
7325
7326/* Invite to reinvoke a persistent group */
7327int wpas_p2p_invite(struct wpa_supplicant *wpa_s, const u8 *peer_addr,
Jouni Malinen31be0a42012-08-31 21:20:51 +03007328 struct wpa_ssid *ssid, const u8 *go_dev_addr, int freq,
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08007329 int vht_center_freq2, int ht40, int vht, int max_chwidth,
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08007330 int pref_freq, int he, int edmg)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007331{
7332 enum p2p_invite_role role;
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07007333 u8 *bssid = NULL;
7334 int force_freq = 0;
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08007335 int res;
Dmitry Shmidta0d265f2013-11-19 13:13:41 -08007336 int no_pref_freq_given = pref_freq == 0;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08007337 unsigned int pref_freq_list[P2P_MAX_PREF_CHANNELS], size;
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08007338
Dmitry Shmidt700a1372013-03-15 14:14:44 -07007339 wpa_s->global->p2p_invite_group = NULL;
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08007340 if (peer_addr)
7341 os_memcpy(wpa_s->p2p_auth_invite, peer_addr, ETH_ALEN);
7342 else
7343 os_memset(wpa_s->p2p_auth_invite, 0, ETH_ALEN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007344
Jouni Malinen31be0a42012-08-31 21:20:51 +03007345 wpa_s->p2p_persistent_go_freq = freq;
7346 wpa_s->p2p_go_ht40 = !!ht40;
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08007347 wpa_s->p2p_go_vht = !!vht;
Hai Shalom74f70d42019-02-11 14:42:39 -08007348 wpa_s->p2p_go_he = !!he;
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08007349 wpa_s->p2p_go_max_oper_chwidth = max_chwidth;
7350 wpa_s->p2p_go_vht_center_freq2 = vht_center_freq2;
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08007351 wpa_s->p2p_go_edmg = !!edmg;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007352 if (ssid->mode == WPAS_MODE_P2P_GO) {
7353 role = P2P_INVITE_ROLE_GO;
7354 if (peer_addr == NULL) {
7355 wpa_printf(MSG_DEBUG, "P2P: Missing peer "
7356 "address in invitation command");
7357 return -1;
7358 }
7359 if (wpas_p2p_create_iface(wpa_s)) {
7360 if (wpas_p2p_add_group_interface(wpa_s,
7361 WPA_IF_P2P_GO) < 0) {
7362 wpa_printf(MSG_ERROR, "P2P: Failed to "
7363 "allocate a new interface for the "
7364 "group");
7365 return -1;
7366 }
7367 bssid = wpa_s->pending_interface_addr;
Dmitry Shmidt9c175262016-03-03 10:20:07 -08007368 } else if (wpa_s->p2p_mgmt)
7369 bssid = wpa_s->parent->own_addr;
7370 else
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007371 bssid = wpa_s->own_addr;
7372 } else {
7373 role = P2P_INVITE_ROLE_CLIENT;
7374 peer_addr = ssid->bssid;
7375 }
7376 wpa_s->pending_invite_ssid_id = ssid->id;
7377
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08007378 size = P2P_MAX_PREF_CHANNELS;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07007379 res = wpas_p2p_setup_freqs(wpa_s, freq, &force_freq, &pref_freq,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08007380 role == P2P_INVITE_ROLE_GO,
7381 pref_freq_list, &size);
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08007382 if (res)
7383 return res;
Irfan Sheriffaf84a572012-09-22 16:59:30 -07007384
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007385 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
7386 return -1;
7387
Dmitry Shmidt7d56b752015-12-22 10:59:44 -08007388 p2p_set_own_pref_freq_list(wpa_s->global->p2p, pref_freq_list, size);
7389
Dmitry Shmidta0d265f2013-11-19 13:13:41 -08007390 if (wpa_s->parent->conf->p2p_ignore_shared_freq &&
7391 no_pref_freq_given && pref_freq > 0 &&
7392 wpa_s->num_multichan_concurrent > 1 &&
7393 wpas_p2p_num_unused_channels(wpa_s) > 0) {
7394 wpa_printf(MSG_DEBUG, "P2P: Ignore own channel preference %d MHz for invitation due to p2p_ignore_shared_freq=1 configuration",
7395 pref_freq);
7396 pref_freq = 0;
7397 }
7398
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08007399 /*
7400 * Stop any find/listen operations before invitation and possibly
7401 * connection establishment.
7402 */
7403 wpas_p2p_stop_find_oper(wpa_s);
7404
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007405 return p2p_invite(wpa_s->global->p2p, peer_addr, role, bssid,
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08007406 ssid->ssid, ssid->ssid_len, force_freq, go_dev_addr,
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08007407 1, pref_freq, -1);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007408}
7409
7410
7411/* Invite to join an active group */
7412int wpas_p2p_invite_group(struct wpa_supplicant *wpa_s, const char *ifname,
7413 const u8 *peer_addr, const u8 *go_dev_addr)
7414{
7415 struct wpa_global *global = wpa_s->global;
7416 enum p2p_invite_role role;
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07007417 u8 *bssid = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007418 struct wpa_ssid *ssid;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08007419 int persistent;
Dmitry Shmidt051af732013-10-22 13:52:46 -07007420 int freq = 0, force_freq = 0, pref_freq = 0;
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08007421 int res;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08007422 unsigned int pref_freq_list[P2P_MAX_PREF_CHANNELS], size;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007423
Jouni Malinen31be0a42012-08-31 21:20:51 +03007424 wpa_s->p2p_persistent_go_freq = 0;
7425 wpa_s->p2p_go_ht40 = 0;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07007426 wpa_s->p2p_go_vht = 0;
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08007427 wpa_s->p2p_go_vht_center_freq2 = 0;
7428 wpa_s->p2p_go_max_oper_chwidth = 0;
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08007429 wpa_s->p2p_go_edmg = 0;
Jouni Malinen31be0a42012-08-31 21:20:51 +03007430
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007431 for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
7432 if (os_strcmp(wpa_s->ifname, ifname) == 0)
7433 break;
7434 }
7435 if (wpa_s == NULL) {
7436 wpa_printf(MSG_DEBUG, "P2P: Interface '%s' not found", ifname);
7437 return -1;
7438 }
7439
7440 ssid = wpa_s->current_ssid;
7441 if (ssid == NULL) {
7442 wpa_printf(MSG_DEBUG, "P2P: No current SSID to use for "
7443 "invitation");
7444 return -1;
7445 }
7446
Dmitry Shmidt700a1372013-03-15 14:14:44 -07007447 wpa_s->global->p2p_invite_group = wpa_s;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08007448 persistent = ssid->p2p_persistent_group &&
Dmitry Shmidt9c175262016-03-03 10:20:07 -08007449 wpas_p2p_get_persistent(wpa_s->p2pdev, peer_addr,
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08007450 ssid->ssid, ssid->ssid_len);
7451
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007452 if (ssid->mode == WPAS_MODE_P2P_GO) {
7453 role = P2P_INVITE_ROLE_ACTIVE_GO;
7454 bssid = wpa_s->own_addr;
7455 if (go_dev_addr == NULL)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007456 go_dev_addr = wpa_s->global->p2p_dev_addr;
Dmitry Shmidt051af732013-10-22 13:52:46 -07007457 freq = ssid->frequency;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007458 } else {
7459 role = P2P_INVITE_ROLE_CLIENT;
7460 if (wpa_s->wpa_state < WPA_ASSOCIATED) {
7461 wpa_printf(MSG_DEBUG, "P2P: Not associated - cannot "
7462 "invite to current group");
7463 return -1;
7464 }
7465 bssid = wpa_s->bssid;
7466 if (go_dev_addr == NULL &&
7467 !is_zero_ether_addr(wpa_s->go_dev_addr))
7468 go_dev_addr = wpa_s->go_dev_addr;
Dmitry Shmidt051af732013-10-22 13:52:46 -07007469 freq = wpa_s->current_bss ? wpa_s->current_bss->freq :
7470 (int) wpa_s->assoc_freq;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007471 }
Dmitry Shmidt9c175262016-03-03 10:20:07 -08007472 wpa_s->p2pdev->pending_invite_ssid_id = -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007473
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007474 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
7475 return -1;
7476
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08007477 size = P2P_MAX_PREF_CHANNELS;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07007478 res = wpas_p2p_setup_freqs(wpa_s, freq, &force_freq, &pref_freq,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08007479 role == P2P_INVITE_ROLE_ACTIVE_GO,
7480 pref_freq_list, &size);
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08007481 if (res)
7482 return res;
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07007483 wpas_p2p_set_own_freq_preference(wpa_s, force_freq);
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08007484
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007485 return p2p_invite(wpa_s->global->p2p, peer_addr, role, bssid,
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08007486 ssid->ssid, ssid->ssid_len, force_freq,
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08007487 go_dev_addr, persistent, pref_freq, -1);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007488}
7489
7490
7491void wpas_p2p_completed(struct wpa_supplicant *wpa_s)
7492{
7493 struct wpa_ssid *ssid = wpa_s->current_ssid;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007494 u8 go_dev_addr[ETH_ALEN];
7495 int persistent;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007496 int freq;
Hai Shalom5f92bc92019-04-18 11:54:11 -07007497 u8 ip[3 * 4], *ip_ptr = NULL;
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08007498 char ip_addr[100];
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007499
Dmitry Shmidt04949592012-07-19 12:16:46 -07007500 if (ssid == NULL || ssid->mode != WPAS_MODE_P2P_GROUP_FORMATION) {
7501 eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
Dmitry Shmidt9c175262016-03-03 10:20:07 -08007502 wpa_s->p2pdev, NULL);
Dmitry Shmidt04949592012-07-19 12:16:46 -07007503 }
7504
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007505 if (!wpa_s->show_group_started || !ssid)
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08007506 return;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007507
7508 wpa_s->show_group_started = 0;
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08007509 if (!wpa_s->p2p_go_group_formation_completed &&
7510 wpa_s->global->p2p_group_formation == wpa_s) {
7511 wpa_dbg(wpa_s, MSG_DEBUG,
7512 "P2P: Marking group formation completed on client on data connection");
7513 wpa_s->p2p_go_group_formation_completed = 1;
7514 wpa_s->global->p2p_group_formation = NULL;
7515 wpa_s->p2p_in_provisioning = 0;
7516 wpa_s->p2p_in_invitation = 0;
7517 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007518
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007519 os_memset(go_dev_addr, 0, ETH_ALEN);
7520 if (ssid->bssid_set)
7521 os_memcpy(go_dev_addr, ssid->bssid, ETH_ALEN);
7522 persistent = wpas_p2p_persistent_group(wpa_s, go_dev_addr, ssid->ssid,
7523 ssid->ssid_len);
7524 os_memcpy(wpa_s->go_dev_addr, go_dev_addr, ETH_ALEN);
7525
7526 if (wpa_s->global->p2p_group_formation == wpa_s)
7527 wpa_s->global->p2p_group_formation = NULL;
7528
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007529 freq = wpa_s->current_bss ? wpa_s->current_bss->freq :
7530 (int) wpa_s->assoc_freq;
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08007531
7532 ip_addr[0] = '\0';
7533 if (wpa_sm_get_p2p_ip_addr(wpa_s->wpa, ip) == 0) {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007534 int res;
7535
7536 res = os_snprintf(ip_addr, sizeof(ip_addr),
7537 " ip_addr=%u.%u.%u.%u "
7538 "ip_mask=%u.%u.%u.%u go_ip_addr=%u.%u.%u.%u",
7539 ip[0], ip[1], ip[2], ip[3],
7540 ip[4], ip[5], ip[6], ip[7],
7541 ip[8], ip[9], ip[10], ip[11]);
7542 if (os_snprintf_error(sizeof(ip_addr), res))
7543 ip_addr[0] = '\0';
Hai Shalom5f92bc92019-04-18 11:54:11 -07007544 ip_ptr = ip;
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08007545 }
7546
Dmitry Shmidt7f0b69e2014-07-28 10:35:20 -07007547 wpas_p2p_group_started(wpa_s, 0, ssid, freq,
7548 ssid->passphrase == NULL && ssid->psk_set ?
7549 ssid->psk : NULL,
7550 ssid->passphrase, go_dev_addr, persistent,
7551 ip_addr);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007552
7553 if (persistent)
Dmitry Shmidtd5ab1b52016-06-21 12:38:41 -07007554 wpas_p2p_store_persistent_group(wpa_s->p2pdev,
7555 ssid, go_dev_addr);
7556
Hai Shalom5f92bc92019-04-18 11:54:11 -07007557 wpas_notify_p2p_group_started(wpa_s, ssid, persistent, 1, ip_ptr);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007558}
7559
7560
7561int wpas_p2p_presence_req(struct wpa_supplicant *wpa_s, u32 duration1,
7562 u32 interval1, u32 duration2, u32 interval2)
7563{
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08007564 int ret;
7565
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007566 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
7567 return -1;
7568
7569 if (wpa_s->wpa_state < WPA_ASSOCIATED ||
7570 wpa_s->current_ssid == NULL ||
7571 wpa_s->current_ssid->mode != WPAS_MODE_INFRA)
7572 return -1;
7573
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08007574 ret = p2p_presence_req(wpa_s->global->p2p, wpa_s->bssid,
7575 wpa_s->own_addr, wpa_s->assoc_freq,
7576 duration1, interval1, duration2, interval2);
7577 if (ret == 0)
7578 wpa_s->waiting_presence_resp = 1;
7579
7580 return ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007581}
7582
7583
7584int wpas_p2p_ext_listen(struct wpa_supplicant *wpa_s, unsigned int period,
7585 unsigned int interval)
7586{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007587 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
7588 return -1;
7589
7590 return p2p_ext_listen(wpa_s->global->p2p, period, interval);
7591}
7592
7593
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007594static int wpas_p2p_is_client(struct wpa_supplicant *wpa_s)
7595{
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08007596 if (wpa_s->current_ssid == NULL) {
7597 /*
7598 * current_ssid can be cleared when P2P client interface gets
7599 * disconnected, so assume this interface was used as P2P
7600 * client.
7601 */
7602 return 1;
7603 }
7604 return wpa_s->current_ssid->p2p_group &&
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007605 wpa_s->current_ssid->mode == WPAS_MODE_INFRA;
7606}
7607
7608
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007609static void wpas_p2p_group_idle_timeout(void *eloop_ctx, void *timeout_ctx)
7610{
7611 struct wpa_supplicant *wpa_s = eloop_ctx;
7612
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007613 if (wpa_s->conf->p2p_group_idle == 0 && !wpas_p2p_is_client(wpa_s)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007614 wpa_printf(MSG_DEBUG, "P2P: Ignore group idle timeout - "
7615 "disabled");
7616 return;
7617 }
7618
Dmitry Shmidt04949592012-07-19 12:16:46 -07007619 wpa_printf(MSG_DEBUG, "P2P: Group idle timeout reached - terminate "
7620 "group");
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07007621 wpas_p2p_group_delete(wpa_s, P2P_GROUP_REMOVAL_IDLE_TIMEOUT);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007622}
7623
7624
7625static void wpas_p2p_set_group_idle_timeout(struct wpa_supplicant *wpa_s)
7626{
Dmitry Shmidt04949592012-07-19 12:16:46 -07007627 int timeout;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007628
Dmitry Shmidt04949592012-07-19 12:16:46 -07007629 if (eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL) > 0)
7630 wpa_printf(MSG_DEBUG, "P2P: Cancelled P2P group idle timeout");
7631
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007632 if (wpa_s->current_ssid == NULL || !wpa_s->current_ssid->p2p_group)
7633 return;
7634
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007635 timeout = wpa_s->conf->p2p_group_idle;
7636 if (wpa_s->current_ssid->mode == WPAS_MODE_INFRA &&
7637 (timeout == 0 || timeout > P2P_MAX_CLIENT_IDLE))
7638 timeout = P2P_MAX_CLIENT_IDLE;
7639
7640 if (timeout == 0)
7641 return;
7642
Dmitry Shmidt04949592012-07-19 12:16:46 -07007643 if (timeout < 0) {
7644 if (wpa_s->current_ssid->mode == WPAS_MODE_INFRA)
7645 timeout = 0; /* special client mode no-timeout */
7646 else
7647 return;
7648 }
7649
7650 if (wpa_s->p2p_in_provisioning) {
7651 /*
7652 * Use the normal group formation timeout during the
7653 * provisioning phase to avoid terminating this process too
7654 * early due to group idle timeout.
7655 */
7656 wpa_printf(MSG_DEBUG, "P2P: Do not use P2P group idle timeout "
7657 "during provisioning");
7658 return;
7659 }
Deepthi Gowri9e4e8ac2013-04-16 11:57:05 +05307660
Dmitry Shmidt04949592012-07-19 12:16:46 -07007661 if (wpa_s->show_group_started) {
7662 /*
7663 * Use the normal group formation timeout between the end of
7664 * the provisioning phase and completion of 4-way handshake to
7665 * avoid terminating this process too early due to group idle
7666 * timeout.
7667 */
7668 wpa_printf(MSG_DEBUG, "P2P: Do not use P2P group idle timeout "
7669 "while waiting for initial 4-way handshake to "
7670 "complete");
7671 return;
7672 }
7673
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007674 wpa_printf(MSG_DEBUG, "P2P: Set P2P group idle timeout to %u seconds",
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007675 timeout);
7676 eloop_register_timeout(timeout, 0, wpas_p2p_group_idle_timeout,
7677 wpa_s, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007678}
7679
7680
Jouni Malinen2b89da82012-08-31 22:04:41 +03007681/* Returns 1 if the interface was removed */
7682int wpas_p2p_deauth_notif(struct wpa_supplicant *wpa_s, const u8 *bssid,
7683 u16 reason_code, const u8 *ie, size_t ie_len,
7684 int locally_generated)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007685{
7686 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
Jouni Malinen2b89da82012-08-31 22:04:41 +03007687 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007688
Dmitry Shmidt04949592012-07-19 12:16:46 -07007689 if (!locally_generated)
7690 p2p_deauth_notif(wpa_s->global->p2p, bssid, reason_code, ie,
7691 ie_len);
7692
7693 if (reason_code == WLAN_REASON_DEAUTH_LEAVING && !locally_generated &&
7694 wpa_s->current_ssid &&
7695 wpa_s->current_ssid->p2p_group &&
7696 wpa_s->current_ssid->mode == WPAS_MODE_INFRA) {
7697 wpa_printf(MSG_DEBUG, "P2P: GO indicated that the P2P Group "
7698 "session is ending");
Jouni Malinen2b89da82012-08-31 22:04:41 +03007699 if (wpas_p2p_group_delete(wpa_s,
7700 P2P_GROUP_REMOVAL_GO_ENDING_SESSION)
7701 > 0)
7702 return 1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07007703 }
Jouni Malinen2b89da82012-08-31 22:04:41 +03007704
7705 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007706}
7707
7708
7709void wpas_p2p_disassoc_notif(struct wpa_supplicant *wpa_s, const u8 *bssid,
Dmitry Shmidt04949592012-07-19 12:16:46 -07007710 u16 reason_code, const u8 *ie, size_t ie_len,
7711 int locally_generated)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007712{
7713 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
7714 return;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007715
Dmitry Shmidt04949592012-07-19 12:16:46 -07007716 if (!locally_generated)
7717 p2p_disassoc_notif(wpa_s->global->p2p, bssid, reason_code, ie,
7718 ie_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007719}
7720
7721
7722void wpas_p2p_update_config(struct wpa_supplicant *wpa_s)
7723{
7724 struct p2p_data *p2p = wpa_s->global->p2p;
7725
7726 if (p2p == NULL)
7727 return;
7728
7729 if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_CAPABLE))
7730 return;
7731
7732 if (wpa_s->conf->changed_parameters & CFG_CHANGED_DEVICE_NAME)
7733 p2p_set_dev_name(p2p, wpa_s->conf->device_name);
7734
7735 if (wpa_s->conf->changed_parameters & CFG_CHANGED_DEVICE_TYPE)
7736 p2p_set_pri_dev_type(p2p, wpa_s->conf->device_type);
7737
7738 if (wpa_s->wps &&
7739 (wpa_s->conf->changed_parameters & CFG_CHANGED_CONFIG_METHODS))
7740 p2p_set_config_methods(p2p, wpa_s->wps->config_methods);
7741
7742 if (wpa_s->wps && (wpa_s->conf->changed_parameters & CFG_CHANGED_UUID))
7743 p2p_set_uuid(p2p, wpa_s->wps->uuid);
7744
7745 if (wpa_s->conf->changed_parameters & CFG_CHANGED_WPS_STRING) {
7746 p2p_set_manufacturer(p2p, wpa_s->conf->manufacturer);
7747 p2p_set_model_name(p2p, wpa_s->conf->model_name);
7748 p2p_set_model_number(p2p, wpa_s->conf->model_number);
7749 p2p_set_serial_number(p2p, wpa_s->conf->serial_number);
7750 }
7751
7752 if (wpa_s->conf->changed_parameters & CFG_CHANGED_SEC_DEVICE_TYPE)
7753 p2p_set_sec_dev_types(p2p,
7754 (void *) wpa_s->conf->sec_device_type,
7755 wpa_s->conf->num_sec_device_types);
7756
7757 if (wpa_s->conf->changed_parameters & CFG_CHANGED_VENDOR_EXTENSION) {
7758 int i;
7759 p2p_remove_wps_vendor_extensions(p2p);
7760 for (i = 0; i < MAX_WPS_VENDOR_EXT; i++) {
7761 if (wpa_s->conf->wps_vendor_ext[i] == NULL)
7762 continue;
7763 p2p_add_wps_vendor_extension(
7764 p2p, wpa_s->conf->wps_vendor_ext[i]);
7765 }
7766 }
7767
7768 if ((wpa_s->conf->changed_parameters & CFG_CHANGED_COUNTRY) &&
7769 wpa_s->conf->country[0] && wpa_s->conf->country[1]) {
7770 char country[3];
7771 country[0] = wpa_s->conf->country[0];
7772 country[1] = wpa_s->conf->country[1];
7773 country[2] = 0x04;
7774 p2p_set_country(p2p, country);
7775 }
7776
7777 if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_SSID_POSTFIX) {
7778 p2p_set_ssid_postfix(p2p, (u8 *) wpa_s->conf->p2p_ssid_postfix,
7779 wpa_s->conf->p2p_ssid_postfix ?
7780 os_strlen(wpa_s->conf->p2p_ssid_postfix) :
7781 0);
7782 }
7783
7784 if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_INTRA_BSS)
7785 p2p_set_intra_bss_dist(p2p, wpa_s->conf->p2p_intra_bss);
Jouni Malinen75ecf522011-06-27 15:19:46 -07007786
7787 if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_LISTEN_CHANNEL) {
7788 u8 reg_class, channel;
7789 int ret;
7790 unsigned int r;
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07007791 u8 channel_forced;
7792
Jouni Malinen75ecf522011-06-27 15:19:46 -07007793 if (wpa_s->conf->p2p_listen_reg_class &&
7794 wpa_s->conf->p2p_listen_channel) {
7795 reg_class = wpa_s->conf->p2p_listen_reg_class;
7796 channel = wpa_s->conf->p2p_listen_channel;
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07007797 channel_forced = 1;
Jouni Malinen75ecf522011-06-27 15:19:46 -07007798 } else {
7799 reg_class = 81;
7800 /*
7801 * Pick one of the social channels randomly as the
7802 * listen channel.
7803 */
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07007804 if (os_get_random((u8 *) &r, sizeof(r)) < 0)
7805 channel = 1;
7806 else
7807 channel = 1 + (r % 3) * 5;
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07007808 channel_forced = 0;
Jouni Malinen75ecf522011-06-27 15:19:46 -07007809 }
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07007810 ret = p2p_set_listen_channel(p2p, reg_class, channel,
7811 channel_forced);
Jouni Malinen75ecf522011-06-27 15:19:46 -07007812 if (ret)
7813 wpa_printf(MSG_ERROR, "P2P: Own listen channel update "
7814 "failed: %d", ret);
7815 }
7816 if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_OPER_CHANNEL) {
7817 u8 op_reg_class, op_channel, cfg_op_channel;
7818 int ret = 0;
7819 unsigned int r;
7820 if (wpa_s->conf->p2p_oper_reg_class &&
7821 wpa_s->conf->p2p_oper_channel) {
7822 op_reg_class = wpa_s->conf->p2p_oper_reg_class;
7823 op_channel = wpa_s->conf->p2p_oper_channel;
7824 cfg_op_channel = 1;
7825 } else {
7826 op_reg_class = 81;
7827 /*
7828 * Use random operation channel from (1, 6, 11)
7829 *if no other preference is indicated.
7830 */
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07007831 if (os_get_random((u8 *) &r, sizeof(r)) < 0)
7832 op_channel = 1;
7833 else
7834 op_channel = 1 + (r % 3) * 5;
Jouni Malinen75ecf522011-06-27 15:19:46 -07007835 cfg_op_channel = 0;
7836 }
7837 ret = p2p_set_oper_channel(p2p, op_reg_class, op_channel,
7838 cfg_op_channel);
7839 if (ret)
7840 wpa_printf(MSG_ERROR, "P2P: Own oper channel update "
7841 "failed: %d", ret);
7842 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07007843
7844 if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_PREF_CHAN) {
7845 if (p2p_set_pref_chan(p2p, wpa_s->conf->num_p2p_pref_chan,
7846 wpa_s->conf->p2p_pref_chan) < 0) {
7847 wpa_printf(MSG_ERROR, "P2P: Preferred channel list "
7848 "update failed");
7849 }
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07007850
7851 if (p2p_set_no_go_freq(p2p, &wpa_s->conf->p2p_no_go_freq) < 0) {
7852 wpa_printf(MSG_ERROR, "P2P: No GO channel list "
7853 "update failed");
7854 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07007855 }
Dmitry Shmidt2271d3f2014-06-23 12:16:31 -07007856
7857 if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_PASSPHRASE_LEN)
7858 p2p_set_passphrase_len(p2p, wpa_s->conf->p2p_passphrase_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007859}
7860
7861
7862int wpas_p2p_set_noa(struct wpa_supplicant *wpa_s, u8 count, int start,
7863 int duration)
7864{
7865 if (!wpa_s->ap_iface)
7866 return -1;
7867 return hostapd_p2p_set_noa(wpa_s->ap_iface->bss[0], count, start,
7868 duration);
7869}
7870
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007871
7872int wpas_p2p_set_cross_connect(struct wpa_supplicant *wpa_s, int enabled)
7873{
7874 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
7875 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007876
7877 wpa_s->global->cross_connection = enabled;
7878 p2p_set_cross_connect(wpa_s->global->p2p, enabled);
7879
7880 if (!enabled) {
7881 struct wpa_supplicant *iface;
7882
7883 for (iface = wpa_s->global->ifaces; iface; iface = iface->next)
7884 {
7885 if (iface->cross_connect_enabled == 0)
7886 continue;
7887
7888 iface->cross_connect_enabled = 0;
7889 iface->cross_connect_in_use = 0;
Dmitry Shmidt9c175262016-03-03 10:20:07 -08007890 wpa_msg_global(iface->p2pdev, MSG_INFO,
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07007891 P2P_EVENT_CROSS_CONNECT_DISABLE "%s %s",
7892 iface->ifname,
7893 iface->cross_connect_uplink);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007894 }
7895 }
7896
7897 return 0;
7898}
7899
7900
7901static void wpas_p2p_enable_cross_connect(struct wpa_supplicant *uplink)
7902{
7903 struct wpa_supplicant *iface;
7904
7905 if (!uplink->global->cross_connection)
7906 return;
7907
7908 for (iface = uplink->global->ifaces; iface; iface = iface->next) {
7909 if (!iface->cross_connect_enabled)
7910 continue;
7911 if (os_strcmp(uplink->ifname, iface->cross_connect_uplink) !=
7912 0)
7913 continue;
7914 if (iface->ap_iface == NULL)
7915 continue;
7916 if (iface->cross_connect_in_use)
7917 continue;
7918
7919 iface->cross_connect_in_use = 1;
Dmitry Shmidt9c175262016-03-03 10:20:07 -08007920 wpa_msg_global(iface->p2pdev, MSG_INFO,
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07007921 P2P_EVENT_CROSS_CONNECT_ENABLE "%s %s",
7922 iface->ifname, iface->cross_connect_uplink);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007923 }
7924}
7925
7926
7927static void wpas_p2p_disable_cross_connect(struct wpa_supplicant *uplink)
7928{
7929 struct wpa_supplicant *iface;
7930
7931 for (iface = uplink->global->ifaces; iface; iface = iface->next) {
7932 if (!iface->cross_connect_enabled)
7933 continue;
7934 if (os_strcmp(uplink->ifname, iface->cross_connect_uplink) !=
7935 0)
7936 continue;
7937 if (!iface->cross_connect_in_use)
7938 continue;
7939
Dmitry Shmidt9c175262016-03-03 10:20:07 -08007940 wpa_msg_global(iface->p2pdev, MSG_INFO,
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07007941 P2P_EVENT_CROSS_CONNECT_DISABLE "%s %s",
7942 iface->ifname, iface->cross_connect_uplink);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007943 iface->cross_connect_in_use = 0;
7944 }
7945}
7946
7947
7948void wpas_p2p_notif_connected(struct wpa_supplicant *wpa_s)
7949{
7950 if (wpa_s->ap_iface || wpa_s->current_ssid == NULL ||
7951 wpa_s->current_ssid->mode != WPAS_MODE_INFRA ||
7952 wpa_s->cross_connect_disallowed)
7953 wpas_p2p_disable_cross_connect(wpa_s);
7954 else
7955 wpas_p2p_enable_cross_connect(wpa_s);
Dmitry Shmidt04949592012-07-19 12:16:46 -07007956 if (!wpa_s->ap_iface &&
7957 eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL) > 0)
7958 wpa_printf(MSG_DEBUG, "P2P: Cancelled P2P group idle timeout");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007959}
7960
7961
7962void wpas_p2p_notif_disconnected(struct wpa_supplicant *wpa_s)
7963{
7964 wpas_p2p_disable_cross_connect(wpa_s);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007965 if (!wpa_s->ap_iface &&
7966 !eloop_is_timeout_registered(wpas_p2p_group_idle_timeout,
7967 wpa_s, NULL))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007968 wpas_p2p_set_group_idle_timeout(wpa_s);
7969}
7970
7971
7972static void wpas_p2p_cross_connect_setup(struct wpa_supplicant *wpa_s)
7973{
7974 struct wpa_supplicant *iface;
7975
7976 if (!wpa_s->global->cross_connection)
7977 return;
7978
7979 for (iface = wpa_s->global->ifaces; iface; iface = iface->next) {
7980 if (iface == wpa_s)
7981 continue;
7982 if (iface->drv_flags &
7983 WPA_DRIVER_FLAGS_P2P_DEDICATED_INTERFACE)
7984 continue;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007985 if ((iface->drv_flags & WPA_DRIVER_FLAGS_P2P_CAPABLE) &&
7986 iface != wpa_s->parent)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007987 continue;
7988
7989 wpa_s->cross_connect_enabled = 1;
7990 os_strlcpy(wpa_s->cross_connect_uplink, iface->ifname,
7991 sizeof(wpa_s->cross_connect_uplink));
7992 wpa_printf(MSG_DEBUG, "P2P: Enable cross connection from "
7993 "%s to %s whenever uplink is available",
7994 wpa_s->ifname, wpa_s->cross_connect_uplink);
7995
7996 if (iface->ap_iface || iface->current_ssid == NULL ||
7997 iface->current_ssid->mode != WPAS_MODE_INFRA ||
7998 iface->cross_connect_disallowed ||
7999 iface->wpa_state != WPA_COMPLETED)
8000 break;
8001
8002 wpa_s->cross_connect_in_use = 1;
Dmitry Shmidt9c175262016-03-03 10:20:07 -08008003 wpa_msg_global(wpa_s->p2pdev, MSG_INFO,
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07008004 P2P_EVENT_CROSS_CONNECT_ENABLE "%s %s",
8005 wpa_s->ifname, wpa_s->cross_connect_uplink);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008006 break;
8007 }
8008}
8009
8010
8011int wpas_p2p_notif_pbc_overlap(struct wpa_supplicant *wpa_s)
8012{
8013 if (wpa_s->p2p_group_interface != P2P_GROUP_INTERFACE_CLIENT &&
8014 !wpa_s->p2p_in_provisioning)
8015 return 0; /* not P2P client operation */
8016
8017 wpa_printf(MSG_DEBUG, "P2P: Terminate connection due to WPS PBC "
8018 "session overlap");
Dmitry Shmidt9c175262016-03-03 10:20:07 -08008019 if (wpa_s != wpa_s->p2pdev)
8020 wpa_msg_ctrl(wpa_s->p2pdev, MSG_INFO, WPS_EVENT_OVERLAP);
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07008021 wpas_p2p_group_formation_failed(wpa_s, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008022 return 1;
8023}
8024
8025
Dmitry Shmidtdf5a7e42014-04-02 12:59:59 -07008026void wpas_p2p_pbc_overlap_cb(void *eloop_ctx, void *timeout_ctx)
8027{
8028 struct wpa_supplicant *wpa_s = eloop_ctx;
8029 wpas_p2p_notif_pbc_overlap(wpa_s);
8030}
8031
8032
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08008033void wpas_p2p_update_channel_list(struct wpa_supplicant *wpa_s,
8034 enum wpas_p2p_channel_update_trig trig)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008035{
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07008036 struct p2p_channels chan, cli_chan;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08008037 struct wpa_used_freq_data *freqs = NULL;
8038 unsigned int num = wpa_s->num_multichan_concurrent;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008039
8040 if (wpa_s->global == NULL || wpa_s->global->p2p == NULL)
8041 return;
8042
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08008043 freqs = os_calloc(num, sizeof(struct wpa_used_freq_data));
8044 if (!freqs)
8045 return;
8046
8047 num = get_shared_radio_freqs_data(wpa_s, freqs, num);
8048
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008049 os_memset(&chan, 0, sizeof(chan));
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07008050 os_memset(&cli_chan, 0, sizeof(cli_chan));
8051 if (wpas_p2p_setup_channels(wpa_s, &chan, &cli_chan)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008052 wpa_printf(MSG_ERROR, "P2P: Failed to update supported "
8053 "channel list");
8054 return;
8055 }
8056
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07008057 p2p_update_channel_list(wpa_s->global->p2p, &chan, &cli_chan);
Dmitry Shmidt684785c2014-05-12 13:34:29 -07008058
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08008059 wpas_p2p_optimize_listen_channel(wpa_s, freqs, num);
Dmitry Shmidt684785c2014-05-12 13:34:29 -07008060
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08008061 /*
8062 * The used frequencies map changed, so it is possible that a GO is
8063 * using a channel that is no longer valid for P2P use. It is also
8064 * possible that due to policy consideration, it would be preferable to
8065 * move it to a frequency already used by other station interfaces.
8066 */
8067 wpas_p2p_consider_moving_gos(wpa_s, freqs, num, trig);
8068
8069 os_free(freqs);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008070}
8071
8072
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08008073static void wpas_p2p_scan_res_ignore(struct wpa_supplicant *wpa_s,
8074 struct wpa_scan_results *scan_res)
8075{
8076 wpa_printf(MSG_DEBUG, "P2P: Ignore scan results");
8077}
8078
8079
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008080int wpas_p2p_cancel(struct wpa_supplicant *wpa_s)
8081{
8082 struct wpa_global *global = wpa_s->global;
8083 int found = 0;
8084 const u8 *peer;
8085
8086 if (global->p2p == NULL)
8087 return -1;
8088
8089 wpa_printf(MSG_DEBUG, "P2P: Request to cancel group formation");
8090
8091 if (wpa_s->pending_interface_name[0] &&
8092 !is_zero_ether_addr(wpa_s->pending_interface_addr))
8093 found = 1;
8094
8095 peer = p2p_get_go_neg_peer(global->p2p);
8096 if (peer) {
8097 wpa_printf(MSG_DEBUG, "P2P: Unauthorize pending GO Neg peer "
8098 MACSTR, MAC2STR(peer));
8099 p2p_unauthorize(global->p2p, peer);
Dmitry Shmidt04949592012-07-19 12:16:46 -07008100 found = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008101 }
8102
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08008103 if (wpa_s->scan_res_handler == wpas_p2p_scan_res_join) {
8104 wpa_printf(MSG_DEBUG, "P2P: Stop pending scan for join");
8105 wpa_s->scan_res_handler = wpas_p2p_scan_res_ignore;
8106 found = 1;
8107 }
8108
8109 if (wpa_s->pending_pd_before_join) {
8110 wpa_printf(MSG_DEBUG, "P2P: Stop pending PD before join");
8111 wpa_s->pending_pd_before_join = 0;
8112 found = 1;
8113 }
8114
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008115 wpas_p2p_stop_find(wpa_s);
8116
8117 for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
8118 if (wpa_s == global->p2p_group_formation &&
8119 (wpa_s->p2p_in_provisioning ||
8120 wpa_s->parent->pending_interface_type ==
8121 WPA_IF_P2P_CLIENT)) {
8122 wpa_printf(MSG_DEBUG, "P2P: Interface %s in group "
8123 "formation found - cancelling",
8124 wpa_s->ifname);
8125 found = 1;
8126 eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
Dmitry Shmidt9c175262016-03-03 10:20:07 -08008127 wpa_s->p2pdev, NULL);
Jouni Malinenadddfc42012-10-03 14:31:41 -07008128 if (wpa_s->p2p_in_provisioning) {
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07008129 wpas_group_formation_completed(wpa_s, 0, 0);
Jouni Malinenadddfc42012-10-03 14:31:41 -07008130 break;
8131 }
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07008132 wpas_p2p_group_delete(wpa_s,
8133 P2P_GROUP_REMOVAL_REQUESTED);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008134 break;
Dmitry Shmidt21de2142014-04-08 10:50:52 -07008135 } else if (wpa_s->p2p_in_invitation) {
8136 wpa_printf(MSG_DEBUG, "P2P: Interface %s in invitation found - cancelling",
8137 wpa_s->ifname);
8138 found = 1;
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07008139 wpas_p2p_group_formation_failed(wpa_s, 0);
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08008140 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008141 }
8142 }
8143
8144 if (!found) {
8145 wpa_printf(MSG_DEBUG, "P2P: No ongoing group formation found");
8146 return -1;
8147 }
8148
8149 return 0;
8150}
8151
8152
8153void wpas_p2p_interface_unavailable(struct wpa_supplicant *wpa_s)
8154{
8155 if (wpa_s->current_ssid == NULL || !wpa_s->current_ssid->p2p_group)
8156 return;
8157
8158 wpa_printf(MSG_DEBUG, "P2P: Remove group due to driver resource not "
8159 "being available anymore");
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07008160 wpas_p2p_group_delete(wpa_s, P2P_GROUP_REMOVAL_UNAVAILABLE);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008161}
8162
8163
8164void wpas_p2p_update_best_channels(struct wpa_supplicant *wpa_s,
8165 int freq_24, int freq_5, int freq_overall)
8166{
8167 struct p2p_data *p2p = wpa_s->global->p2p;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08008168 if (p2p == NULL)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008169 return;
8170 p2p_set_best_channels(p2p, freq_24, freq_5, freq_overall);
8171}
8172
8173
8174int wpas_p2p_unauthorize(struct wpa_supplicant *wpa_s, const char *addr)
8175{
8176 u8 peer[ETH_ALEN];
8177 struct p2p_data *p2p = wpa_s->global->p2p;
8178
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08008179 if (p2p == NULL)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008180 return -1;
8181
8182 if (hwaddr_aton(addr, peer))
8183 return -1;
8184
8185 return p2p_unauthorize(p2p, peer);
8186}
8187
8188
8189/**
8190 * wpas_p2p_disconnect - Disconnect from a P2P Group
8191 * @wpa_s: Pointer to wpa_supplicant data
8192 * Returns: 0 on success, -1 on failure
8193 *
8194 * This can be used to disconnect from a group in which the local end is a P2P
8195 * Client or to end a P2P Group in case the local end is the Group Owner. If a
8196 * virtual network interface was created for this group, that interface will be
8197 * removed. Otherwise, only the configured P2P group network will be removed
8198 * from the interface.
8199 */
8200int wpas_p2p_disconnect(struct wpa_supplicant *wpa_s)
8201{
8202
8203 if (wpa_s == NULL)
8204 return -1;
8205
Jouni Malinen2b89da82012-08-31 22:04:41 +03008206 return wpas_p2p_group_delete(wpa_s, P2P_GROUP_REMOVAL_REQUESTED) < 0 ?
8207 -1 : 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008208}
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008209
8210
8211int wpas_p2p_in_progress(struct wpa_supplicant *wpa_s)
8212{
Dmitry Shmidtf8623282013-02-20 14:34:59 -08008213 int ret;
8214
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008215 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
8216 return 0;
8217
Dmitry Shmidtf8623282013-02-20 14:34:59 -08008218 ret = p2p_in_progress(wpa_s->global->p2p);
8219 if (ret == 0) {
8220 /*
8221 * Check whether there is an ongoing WPS provisioning step (or
8222 * other parts of group formation) on another interface since
8223 * p2p_in_progress() does not report this to avoid issues for
8224 * scans during such provisioning step.
8225 */
8226 if (wpa_s->global->p2p_group_formation &&
8227 wpa_s->global->p2p_group_formation != wpa_s) {
8228 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Another interface (%s) "
8229 "in group formation",
8230 wpa_s->global->p2p_group_formation->ifname);
8231 ret = 1;
8232 }
8233 }
8234
Dmitry Shmidt92c368d2013-08-29 12:37:21 -07008235 if (!ret && wpa_s->global->p2p_go_wait_client.sec) {
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08008236 struct os_reltime now;
8237 os_get_reltime(&now);
8238 if (os_reltime_expired(&now, &wpa_s->global->p2p_go_wait_client,
8239 P2P_MAX_INITIAL_CONN_WAIT_GO)) {
Dmitry Shmidt92c368d2013-08-29 12:37:21 -07008240 /* Wait for the first client has expired */
8241 wpa_s->global->p2p_go_wait_client.sec = 0;
8242 } else {
8243 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Waiting for initial client connection during group formation");
8244 ret = 1;
8245 }
8246 }
8247
Dmitry Shmidtf8623282013-02-20 14:34:59 -08008248 return ret;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008249}
8250
8251
8252void wpas_p2p_network_removed(struct wpa_supplicant *wpa_s,
8253 struct wpa_ssid *ssid)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008254{
8255 if (wpa_s->p2p_in_provisioning && ssid->p2p_group &&
8256 eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
Dmitry Shmidt9c175262016-03-03 10:20:07 -08008257 wpa_s->p2pdev, NULL) > 0) {
Dmitry Shmidt04949592012-07-19 12:16:46 -07008258 /**
8259 * Remove the network by scheduling the group formation
8260 * timeout to happen immediately. The teardown code
8261 * needs to be scheduled to run asynch later so that we
8262 * don't delete data from under ourselves unexpectedly.
8263 * Calling wpas_p2p_group_formation_timeout directly
8264 * causes a series of crashes in WPS failure scenarios.
8265 */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008266 wpa_printf(MSG_DEBUG, "P2P: Canceled group formation due to "
8267 "P2P group network getting removed");
Dmitry Shmidt04949592012-07-19 12:16:46 -07008268 eloop_register_timeout(0, 0, wpas_p2p_group_formation_timeout,
Dmitry Shmidt9c175262016-03-03 10:20:07 -08008269 wpa_s->p2pdev, NULL);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008270 }
8271}
8272
8273
8274struct wpa_ssid * wpas_p2p_get_persistent(struct wpa_supplicant *wpa_s,
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08008275 const u8 *addr, const u8 *ssid,
8276 size_t ssid_len)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008277{
8278 struct wpa_ssid *s;
8279 size_t i;
8280
8281 for (s = wpa_s->conf->ssid; s; s = s->next) {
8282 if (s->disabled != 2)
8283 continue;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08008284 if (ssid &&
8285 (ssid_len != s->ssid_len ||
8286 os_memcmp(ssid, s->ssid, ssid_len) != 0))
8287 continue;
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07008288 if (addr == NULL) {
8289 if (s->mode == WPAS_MODE_P2P_GO)
8290 return s;
8291 continue;
8292 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008293 if (os_memcmp(s->bssid, addr, ETH_ALEN) == 0)
8294 return s; /* peer is GO in the persistent group */
8295 if (s->mode != WPAS_MODE_P2P_GO || s->p2p_client_list == NULL)
8296 continue;
8297 for (i = 0; i < s->num_p2p_clients; i++) {
Dmitry Shmidtff787d52015-01-12 13:01:47 -08008298 if (os_memcmp(s->p2p_client_list + i * 2 * ETH_ALEN,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008299 addr, ETH_ALEN) == 0)
8300 return s; /* peer is P2P client in persistent
8301 * group */
8302 }
8303 }
8304
8305 return NULL;
8306}
8307
8308
8309void wpas_p2p_notify_ap_sta_authorized(struct wpa_supplicant *wpa_s,
8310 const u8 *addr)
8311{
Dmitry Shmidt56052862013-10-04 10:23:25 -07008312 if (eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
Dmitry Shmidt9c175262016-03-03 10:20:07 -08008313 wpa_s->p2pdev, NULL) > 0) {
Dmitry Shmidt56052862013-10-04 10:23:25 -07008314 /*
8315 * This can happen if WPS provisioning step is not terminated
8316 * cleanly (e.g., P2P Client does not send WSC_Done). Since the
8317 * peer was able to connect, there is no need to time out group
8318 * formation after this, though. In addition, this is used with
8319 * the initial connection wait on the GO as a separate formation
8320 * timeout and as such, expected to be hit after the initial WPS
8321 * provisioning step.
8322 */
8323 wpa_printf(MSG_DEBUG, "P2P: Canceled P2P group formation timeout on data connection");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08008324
8325 if (!wpa_s->p2p_go_group_formation_completed &&
8326 !wpa_s->group_formation_reported) {
8327 /*
8328 * GO has not yet notified group formation success since
8329 * the WPS step was not completed cleanly. Do that
8330 * notification now since the P2P Client was able to
8331 * connect and as such, must have received the
8332 * credential from the WPS step.
8333 */
8334 if (wpa_s->global->p2p)
8335 p2p_wps_success_cb(wpa_s->global->p2p, addr);
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07008336 wpas_group_formation_completed(wpa_s, 1, 0);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08008337 }
Dmitry Shmidt56052862013-10-04 10:23:25 -07008338 }
8339 if (!wpa_s->p2p_go_group_formation_completed) {
8340 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Marking group formation completed on GO on first data connection");
8341 wpa_s->p2p_go_group_formation_completed = 1;
8342 wpa_s->global->p2p_group_formation = NULL;
8343 wpa_s->p2p_in_provisioning = 0;
Dmitry Shmidt21de2142014-04-08 10:50:52 -07008344 wpa_s->p2p_in_invitation = 0;
Dmitry Shmidt56052862013-10-04 10:23:25 -07008345 }
Dmitry Shmidt92c368d2013-08-29 12:37:21 -07008346 wpa_s->global->p2p_go_wait_client.sec = 0;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008347 if (addr == NULL)
8348 return;
8349 wpas_p2p_add_persistent_group_client(wpa_s, addr);
8350}
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08008351
Dmitry Shmidt04949592012-07-19 12:16:46 -07008352
Dmitry Shmidt7f656022015-02-25 14:36:37 -08008353static int wpas_p2p_fallback_to_go_neg(struct wpa_supplicant *wpa_s,
8354 int group_added)
Dmitry Shmidt04949592012-07-19 12:16:46 -07008355{
8356 struct wpa_supplicant *group = wpa_s;
Dmitry Shmidt7f656022015-02-25 14:36:37 -08008357 int ret = 0;
8358
Dmitry Shmidt04949592012-07-19 12:16:46 -07008359 if (wpa_s->global->p2p_group_formation)
8360 group = wpa_s->global->p2p_group_formation;
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07008361 wpa_s = wpa_s->global->p2p_init_wpa_s;
Dmitry Shmidt04949592012-07-19 12:16:46 -07008362 offchannel_send_action_done(wpa_s);
8363 if (group_added)
Dmitry Shmidt7f656022015-02-25 14:36:37 -08008364 ret = wpas_p2p_group_delete(group, P2P_GROUP_REMOVAL_SILENT);
Dmitry Shmidt04949592012-07-19 12:16:46 -07008365 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Fall back to GO Negotiation");
8366 wpas_p2p_connect(wpa_s, wpa_s->pending_join_dev_addr, wpa_s->p2p_pin,
8367 wpa_s->p2p_wps_method, wpa_s->p2p_persistent_group, 0,
8368 0, 0, wpa_s->p2p_go_intent, wpa_s->p2p_connect_freq,
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08008369 wpa_s->p2p_go_vht_center_freq2,
Dmitry Shmidt04949592012-07-19 12:16:46 -07008370 wpa_s->p2p_persistent_id,
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07008371 wpa_s->p2p_pd_before_go_neg,
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07008372 wpa_s->p2p_go_ht40,
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08008373 wpa_s->p2p_go_vht,
Hai Shalom74f70d42019-02-11 14:42:39 -08008374 wpa_s->p2p_go_max_oper_chwidth,
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08008375 wpa_s->p2p_go_he,
8376 wpa_s->p2p_go_edmg,
8377 NULL, 0);
Dmitry Shmidt7f656022015-02-25 14:36:37 -08008378 return ret;
Dmitry Shmidt04949592012-07-19 12:16:46 -07008379}
8380
8381
8382int wpas_p2p_scan_no_go_seen(struct wpa_supplicant *wpa_s)
8383{
Dmitry Shmidt7f656022015-02-25 14:36:37 -08008384 int res;
8385
Dmitry Shmidt04949592012-07-19 12:16:46 -07008386 if (!wpa_s->p2p_fallback_to_go_neg ||
8387 wpa_s->p2p_in_provisioning <= 5)
8388 return 0;
8389
8390 if (wpas_p2p_peer_go(wpa_s, wpa_s->pending_join_dev_addr) > 0)
8391 return 0; /* peer operating as a GO */
8392
8393 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: GO not found for p2p_connect-auto - "
8394 "fallback to GO Negotiation");
Dmitry Shmidt9c175262016-03-03 10:20:07 -08008395 wpa_msg_global(wpa_s->p2pdev, MSG_INFO, P2P_EVENT_FALLBACK_TO_GO_NEG
Dmitry Shmidt7f656022015-02-25 14:36:37 -08008396 "reason=GO-not-found");
8397 res = wpas_p2p_fallback_to_go_neg(wpa_s, 1);
Dmitry Shmidt04949592012-07-19 12:16:46 -07008398
Dmitry Shmidt7f656022015-02-25 14:36:37 -08008399 return res == 1 ? 2 : 1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07008400}
8401
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07008402
8403unsigned int wpas_p2p_search_delay(struct wpa_supplicant *wpa_s)
8404{
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07008405 struct wpa_supplicant *ifs;
8406
8407 if (wpa_s->wpa_state > WPA_SCANNING) {
8408 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Use %u ms search delay due to "
8409 "concurrent operation",
Dmitry Shmidt09f57ba2014-06-10 16:07:13 -07008410 wpa_s->conf->p2p_search_delay);
8411 return wpa_s->conf->p2p_search_delay;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07008412 }
8413
Dmitry Shmidt01904cf2013-12-05 11:08:35 -08008414 dl_list_for_each(ifs, &wpa_s->radio->ifaces, struct wpa_supplicant,
8415 radio_list) {
8416 if (ifs != wpa_s && ifs->wpa_state > WPA_SCANNING) {
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07008417 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Use %u ms search "
8418 "delay due to concurrent operation on "
8419 "interface %s",
Dmitry Shmidt09f57ba2014-06-10 16:07:13 -07008420 wpa_s->conf->p2p_search_delay,
8421 ifs->ifname);
8422 return wpa_s->conf->p2p_search_delay;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07008423 }
8424 }
8425
8426 return 0;
8427}
8428
Dmitry Shmidt37d4d6a2013-03-18 13:09:42 -07008429
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07008430static int wpas_p2p_remove_psk_entry(struct wpa_supplicant *wpa_s,
8431 struct wpa_ssid *s, const u8 *addr,
8432 int iface_addr)
8433{
8434 struct psk_list_entry *psk, *tmp;
8435 int changed = 0;
8436
8437 dl_list_for_each_safe(psk, tmp, &s->psk_list, struct psk_list_entry,
8438 list) {
8439 if ((iface_addr && !psk->p2p &&
8440 os_memcmp(addr, psk->addr, ETH_ALEN) == 0) ||
8441 (!iface_addr && psk->p2p &&
8442 os_memcmp(addr, psk->addr, ETH_ALEN) == 0)) {
8443 wpa_dbg(wpa_s, MSG_DEBUG,
8444 "P2P: Remove persistent group PSK list entry for "
8445 MACSTR " p2p=%u",
8446 MAC2STR(psk->addr), psk->p2p);
8447 dl_list_del(&psk->list);
8448 os_free(psk);
8449 changed++;
8450 }
8451 }
8452
8453 return changed;
8454}
8455
8456
8457void wpas_p2p_new_psk_cb(struct wpa_supplicant *wpa_s, const u8 *mac_addr,
8458 const u8 *p2p_dev_addr,
8459 const u8 *psk, size_t psk_len)
8460{
8461 struct wpa_ssid *ssid = wpa_s->current_ssid;
8462 struct wpa_ssid *persistent;
Dmitry Shmidt7832adb2014-04-29 10:53:02 -07008463 struct psk_list_entry *p, *last;
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07008464
8465 if (psk_len != sizeof(p->psk))
8466 return;
8467
8468 if (p2p_dev_addr) {
8469 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: New PSK for addr=" MACSTR
8470 " p2p_dev_addr=" MACSTR,
8471 MAC2STR(mac_addr), MAC2STR(p2p_dev_addr));
8472 if (is_zero_ether_addr(p2p_dev_addr))
8473 p2p_dev_addr = NULL;
8474 } else {
8475 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: New PSK for addr=" MACSTR,
8476 MAC2STR(mac_addr));
8477 }
8478
8479 if (ssid->mode == WPAS_MODE_P2P_GROUP_FORMATION) {
8480 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: new_psk_cb during group formation");
8481 /* To be added to persistent group once created */
8482 if (wpa_s->global->add_psk == NULL) {
8483 wpa_s->global->add_psk = os_zalloc(sizeof(*p));
8484 if (wpa_s->global->add_psk == NULL)
8485 return;
8486 }
8487 p = wpa_s->global->add_psk;
8488 if (p2p_dev_addr) {
8489 p->p2p = 1;
8490 os_memcpy(p->addr, p2p_dev_addr, ETH_ALEN);
8491 } else {
8492 p->p2p = 0;
8493 os_memcpy(p->addr, mac_addr, ETH_ALEN);
8494 }
8495 os_memcpy(p->psk, psk, psk_len);
8496 return;
8497 }
8498
8499 if (ssid->mode != WPAS_MODE_P2P_GO || !ssid->p2p_persistent_group) {
8500 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Ignore new_psk_cb on not-persistent GO");
8501 return;
8502 }
8503
Dmitry Shmidt9c175262016-03-03 10:20:07 -08008504 persistent = wpas_p2p_get_persistent(wpa_s->p2pdev, NULL, ssid->ssid,
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07008505 ssid->ssid_len);
8506 if (!persistent) {
8507 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Could not find persistent group information to store the new PSK");
8508 return;
8509 }
8510
8511 p = os_zalloc(sizeof(*p));
8512 if (p == NULL)
8513 return;
8514 if (p2p_dev_addr) {
8515 p->p2p = 1;
8516 os_memcpy(p->addr, p2p_dev_addr, ETH_ALEN);
8517 } else {
8518 p->p2p = 0;
8519 os_memcpy(p->addr, mac_addr, ETH_ALEN);
8520 }
8521 os_memcpy(p->psk, psk, psk_len);
8522
Dmitry Shmidt7832adb2014-04-29 10:53:02 -07008523 if (dl_list_len(&persistent->psk_list) > P2P_MAX_STORED_CLIENTS &&
8524 (last = dl_list_last(&persistent->psk_list,
8525 struct psk_list_entry, list))) {
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07008526 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Remove oldest PSK entry for "
8527 MACSTR " (p2p=%u) to make room for a new one",
8528 MAC2STR(last->addr), last->p2p);
8529 dl_list_del(&last->list);
8530 os_free(last);
8531 }
8532
Dmitry Shmidt9c175262016-03-03 10:20:07 -08008533 wpas_p2p_remove_psk_entry(wpa_s->p2pdev, persistent,
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07008534 p2p_dev_addr ? p2p_dev_addr : mac_addr,
8535 p2p_dev_addr == NULL);
8536 if (p2p_dev_addr) {
8537 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Add new PSK for p2p_dev_addr="
8538 MACSTR, MAC2STR(p2p_dev_addr));
8539 } else {
8540 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Add new PSK for addr=" MACSTR,
8541 MAC2STR(mac_addr));
8542 }
8543 dl_list_add(&persistent->psk_list, &p->list);
8544
Dmitry Shmidt9c175262016-03-03 10:20:07 -08008545 if (wpa_s->p2pdev->conf->update_config &&
8546 wpa_config_write(wpa_s->p2pdev->confname, wpa_s->p2pdev->conf))
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07008547 wpa_printf(MSG_DEBUG, "P2P: Failed to update configuration");
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07008548}
8549
8550
8551static void wpas_p2p_remove_psk(struct wpa_supplicant *wpa_s,
8552 struct wpa_ssid *s, const u8 *addr,
8553 int iface_addr)
8554{
8555 int res;
8556
8557 res = wpas_p2p_remove_psk_entry(wpa_s, s, addr, iface_addr);
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08008558 if (res > 0 && wpa_s->conf->update_config &&
8559 wpa_config_write(wpa_s->confname, wpa_s->conf))
8560 wpa_dbg(wpa_s, MSG_DEBUG,
8561 "P2P: Failed to update configuration");
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07008562}
8563
8564
8565static void wpas_p2p_remove_client_go(struct wpa_supplicant *wpa_s,
8566 const u8 *peer, int iface_addr)
8567{
8568 struct hostapd_data *hapd;
8569 struct hostapd_wpa_psk *psk, *prev, *rem;
8570 struct sta_info *sta;
8571
8572 if (wpa_s->ap_iface == NULL || wpa_s->current_ssid == NULL ||
8573 wpa_s->current_ssid->mode != WPAS_MODE_P2P_GO)
8574 return;
8575
8576 /* Remove per-station PSK entry */
8577 hapd = wpa_s->ap_iface->bss[0];
8578 prev = NULL;
8579 psk = hapd->conf->ssid.wpa_psk;
8580 while (psk) {
8581 if ((iface_addr && os_memcmp(peer, psk->addr, ETH_ALEN) == 0) ||
8582 (!iface_addr &&
8583 os_memcmp(peer, psk->p2p_dev_addr, ETH_ALEN) == 0)) {
8584 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Remove operating group PSK entry for "
8585 MACSTR " iface_addr=%d",
8586 MAC2STR(peer), iface_addr);
8587 if (prev)
8588 prev->next = psk->next;
8589 else
8590 hapd->conf->ssid.wpa_psk = psk->next;
8591 rem = psk;
8592 psk = psk->next;
8593 os_free(rem);
8594 } else {
8595 prev = psk;
8596 psk = psk->next;
8597 }
8598 }
8599
8600 /* Disconnect from group */
8601 if (iface_addr)
8602 sta = ap_get_sta(hapd, peer);
8603 else
8604 sta = ap_get_sta_p2p(hapd, peer);
8605 if (sta) {
8606 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Disconnect peer " MACSTR
8607 " (iface_addr=%d) from group",
8608 MAC2STR(peer), iface_addr);
8609 hostapd_drv_sta_deauth(hapd, sta->addr,
8610 WLAN_REASON_DEAUTH_LEAVING);
8611 ap_sta_deauthenticate(hapd, sta, WLAN_REASON_DEAUTH_LEAVING);
8612 }
8613}
8614
8615
8616void wpas_p2p_remove_client(struct wpa_supplicant *wpa_s, const u8 *peer,
8617 int iface_addr)
8618{
8619 struct wpa_ssid *s;
8620 struct wpa_supplicant *w;
Dmitry Shmidt8bd70b72015-05-26 16:02:19 -07008621 struct wpa_supplicant *p2p_wpa_s = wpa_s->global->p2p_init_wpa_s;
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07008622
8623 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Remove client " MACSTR, MAC2STR(peer));
8624
8625 /* Remove from any persistent group */
Dmitry Shmidt8bd70b72015-05-26 16:02:19 -07008626 for (s = p2p_wpa_s->conf->ssid; s; s = s->next) {
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07008627 if (s->disabled != 2 || s->mode != WPAS_MODE_P2P_GO)
8628 continue;
8629 if (!iface_addr)
Dmitry Shmidt8bd70b72015-05-26 16:02:19 -07008630 wpas_remove_persistent_peer(p2p_wpa_s, s, peer, 0);
8631 wpas_p2p_remove_psk(p2p_wpa_s, s, peer, iface_addr);
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07008632 }
8633
8634 /* Remove from any operating group */
8635 for (w = wpa_s->global->ifaces; w; w = w->next)
8636 wpas_p2p_remove_client_go(w, peer, iface_addr);
8637}
8638
8639
8640static void wpas_p2p_psk_failure_removal(void *eloop_ctx, void *timeout_ctx)
8641{
8642 struct wpa_supplicant *wpa_s = eloop_ctx;
8643 wpas_p2p_group_delete(wpa_s, P2P_GROUP_REMOVAL_PSK_FAILURE);
8644}
8645
8646
Dmitry Shmidt04f534e2013-12-09 15:50:16 -08008647static void wpas_p2p_group_freq_conflict(void *eloop_ctx, void *timeout_ctx)
8648{
8649 struct wpa_supplicant *wpa_s = eloop_ctx;
8650
8651 wpa_printf(MSG_DEBUG, "P2P: Frequency conflict - terminate group");
8652 wpas_p2p_group_delete(wpa_s, P2P_GROUP_REMOVAL_FREQ_CONFLICT);
8653}
8654
8655
8656int wpas_p2p_handle_frequency_conflicts(struct wpa_supplicant *wpa_s, int freq,
8657 struct wpa_ssid *ssid)
8658{
8659 struct wpa_supplicant *iface;
8660
8661 for (iface = wpa_s->global->ifaces; iface; iface = iface->next) {
8662 if (!iface->current_ssid ||
8663 iface->current_ssid->frequency == freq ||
8664 (iface->p2p_group_interface == NOT_P2P_GROUP_INTERFACE &&
8665 !iface->current_ssid->p2p_group))
8666 continue;
8667
8668 /* Remove the connection with least priority */
8669 if (!wpas_is_p2p_prioritized(iface)) {
8670 /* STA connection has priority over existing
8671 * P2P connection, so remove the interface. */
8672 wpa_printf(MSG_DEBUG, "P2P: Removing P2P connection due to single channel concurrent mode frequency conflict");
8673 eloop_register_timeout(0, 0,
8674 wpas_p2p_group_freq_conflict,
8675 iface, NULL);
8676 /* If connection in progress is P2P connection, do not
8677 * proceed for the connection. */
8678 if (wpa_s == iface)
8679 return -1;
8680 else
8681 return 0;
8682 } else {
8683 /* P2P connection has priority, disable the STA network
8684 */
8685 wpa_supplicant_disable_network(wpa_s->global->ifaces,
8686 ssid);
8687 wpa_msg(wpa_s->global->ifaces, MSG_INFO,
8688 WPA_EVENT_FREQ_CONFLICT " id=%d", ssid->id);
8689 os_memset(wpa_s->global->ifaces->pending_bssid, 0,
8690 ETH_ALEN);
8691 /* If P2P connection is in progress, continue
8692 * connecting...*/
8693 if (wpa_s == iface)
8694 return 0;
8695 else
8696 return -1;
8697 }
8698 }
8699
8700 return 0;
8701}
8702
8703
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07008704int wpas_p2p_4way_hs_failed(struct wpa_supplicant *wpa_s)
8705{
8706 struct wpa_ssid *ssid = wpa_s->current_ssid;
8707
8708 if (ssid == NULL || !ssid->p2p_group)
8709 return 0;
8710
8711 if (wpa_s->p2p_last_4way_hs_fail &&
8712 wpa_s->p2p_last_4way_hs_fail == ssid) {
8713 u8 go_dev_addr[ETH_ALEN];
8714 struct wpa_ssid *persistent;
8715
8716 if (wpas_p2p_persistent_group(wpa_s, go_dev_addr,
8717 ssid->ssid,
8718 ssid->ssid_len) <= 0) {
8719 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Could not determine whether 4-way handshake failures were for a persistent group");
8720 goto disconnect;
8721 }
8722
8723 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Two 4-way handshake failures for a P2P group - go_dev_addr="
8724 MACSTR, MAC2STR(go_dev_addr));
Dmitry Shmidt9c175262016-03-03 10:20:07 -08008725 persistent = wpas_p2p_get_persistent(wpa_s->p2pdev, go_dev_addr,
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07008726 ssid->ssid,
8727 ssid->ssid_len);
8728 if (persistent == NULL || persistent->mode != WPAS_MODE_INFRA) {
8729 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: No matching persistent group stored");
8730 goto disconnect;
8731 }
Dmitry Shmidt9c175262016-03-03 10:20:07 -08008732 wpa_msg_global(wpa_s->p2pdev, MSG_INFO,
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07008733 P2P_EVENT_PERSISTENT_PSK_FAIL "%d",
8734 persistent->id);
8735 disconnect:
8736 wpa_s->p2p_last_4way_hs_fail = NULL;
8737 /*
8738 * Remove the group from a timeout to avoid issues with caller
8739 * continuing to use the interface if this is on a P2P group
8740 * interface.
8741 */
8742 eloop_register_timeout(0, 0, wpas_p2p_psk_failure_removal,
8743 wpa_s, NULL);
8744 return 1;
8745 }
8746
8747 wpa_s->p2p_last_4way_hs_fail = ssid;
8748 return 0;
8749}
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08008750
8751
8752#ifdef CONFIG_WPS_NFC
8753
8754static struct wpabuf * wpas_p2p_nfc_handover(int ndef, struct wpabuf *wsc,
8755 struct wpabuf *p2p)
8756{
8757 struct wpabuf *ret;
8758 size_t wsc_len;
8759
8760 if (p2p == NULL) {
8761 wpabuf_free(wsc);
8762 wpa_printf(MSG_DEBUG, "P2P: No p2p buffer for handover");
8763 return NULL;
8764 }
8765
8766 wsc_len = wsc ? wpabuf_len(wsc) : 0;
8767 ret = wpabuf_alloc(2 + wsc_len + 2 + wpabuf_len(p2p));
8768 if (ret == NULL) {
8769 wpabuf_free(wsc);
8770 wpabuf_free(p2p);
8771 return NULL;
8772 }
8773
8774 wpabuf_put_be16(ret, wsc_len);
8775 if (wsc)
8776 wpabuf_put_buf(ret, wsc);
8777 wpabuf_put_be16(ret, wpabuf_len(p2p));
8778 wpabuf_put_buf(ret, p2p);
8779
8780 wpabuf_free(wsc);
8781 wpabuf_free(p2p);
8782 wpa_hexdump_buf(MSG_DEBUG,
8783 "P2P: Generated NFC connection handover message", ret);
8784
8785 if (ndef && ret) {
8786 struct wpabuf *tmp;
8787 tmp = ndef_build_p2p(ret);
8788 wpabuf_free(ret);
8789 if (tmp == NULL) {
8790 wpa_printf(MSG_DEBUG, "P2P: Failed to NDEF encapsulate handover request");
8791 return NULL;
8792 }
8793 ret = tmp;
8794 }
8795
8796 return ret;
8797}
8798
8799
8800static int wpas_p2p_cli_freq(struct wpa_supplicant *wpa_s,
8801 struct wpa_ssid **ssid, u8 *go_dev_addr)
8802{
8803 struct wpa_supplicant *iface;
8804
8805 if (go_dev_addr)
8806 os_memset(go_dev_addr, 0, ETH_ALEN);
8807 if (ssid)
8808 *ssid = NULL;
8809 for (iface = wpa_s->global->ifaces; iface; iface = iface->next) {
8810 if (iface->wpa_state < WPA_ASSOCIATING ||
8811 iface->current_ssid == NULL || iface->assoc_freq == 0 ||
8812 !iface->current_ssid->p2p_group ||
8813 iface->current_ssid->mode != WPAS_MODE_INFRA)
8814 continue;
8815 if (ssid)
8816 *ssid = iface->current_ssid;
8817 if (go_dev_addr)
8818 os_memcpy(go_dev_addr, iface->go_dev_addr, ETH_ALEN);
8819 return iface->assoc_freq;
8820 }
8821 return 0;
8822}
8823
8824
8825struct wpabuf * wpas_p2p_nfc_handover_req(struct wpa_supplicant *wpa_s,
8826 int ndef)
8827{
8828 struct wpabuf *wsc, *p2p;
8829 struct wpa_ssid *ssid;
8830 u8 go_dev_addr[ETH_ALEN];
8831 int cli_freq = wpas_p2p_cli_freq(wpa_s, &ssid, go_dev_addr);
8832
8833 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL) {
8834 wpa_printf(MSG_DEBUG, "P2P: P2P disabled - cannot build handover request");
8835 return NULL;
8836 }
8837
8838 if (wpa_s->conf->wps_nfc_dh_pubkey == NULL &&
8839 wps_nfc_gen_dh(&wpa_s->conf->wps_nfc_dh_pubkey,
8840 &wpa_s->conf->wps_nfc_dh_privkey) < 0) {
8841 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: No DH key available for handover request");
8842 return NULL;
8843 }
8844
8845 if (cli_freq == 0) {
8846 wsc = wps_build_nfc_handover_req_p2p(
8847 wpa_s->parent->wps, wpa_s->conf->wps_nfc_dh_pubkey);
8848 } else
8849 wsc = NULL;
8850 p2p = p2p_build_nfc_handover_req(wpa_s->global->p2p, cli_freq,
8851 go_dev_addr, ssid ? ssid->ssid : NULL,
8852 ssid ? ssid->ssid_len : 0);
8853
8854 return wpas_p2p_nfc_handover(ndef, wsc, p2p);
8855}
8856
8857
8858struct wpabuf * wpas_p2p_nfc_handover_sel(struct wpa_supplicant *wpa_s,
8859 int ndef, int tag)
8860{
8861 struct wpabuf *wsc, *p2p;
8862 struct wpa_ssid *ssid;
8863 u8 go_dev_addr[ETH_ALEN];
8864 int cli_freq = wpas_p2p_cli_freq(wpa_s, &ssid, go_dev_addr);
8865
8866 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
8867 return NULL;
8868
8869 if (!tag && wpa_s->conf->wps_nfc_dh_pubkey == NULL &&
8870 wps_nfc_gen_dh(&wpa_s->conf->wps_nfc_dh_pubkey,
8871 &wpa_s->conf->wps_nfc_dh_privkey) < 0)
8872 return NULL;
8873
8874 if (cli_freq == 0) {
8875 wsc = wps_build_nfc_handover_sel_p2p(
8876 wpa_s->parent->wps,
8877 tag ? wpa_s->conf->wps_nfc_dev_pw_id :
8878 DEV_PW_NFC_CONNECTION_HANDOVER,
8879 wpa_s->conf->wps_nfc_dh_pubkey,
8880 tag ? wpa_s->conf->wps_nfc_dev_pw : NULL);
8881 } else
8882 wsc = NULL;
8883 p2p = p2p_build_nfc_handover_sel(wpa_s->global->p2p, cli_freq,
8884 go_dev_addr, ssid ? ssid->ssid : NULL,
8885 ssid ? ssid->ssid_len : 0);
8886
8887 return wpas_p2p_nfc_handover(ndef, wsc, p2p);
8888}
8889
8890
8891static int wpas_p2p_nfc_join_group(struct wpa_supplicant *wpa_s,
8892 struct p2p_nfc_params *params)
8893{
8894 wpa_printf(MSG_DEBUG, "P2P: Initiate join-group based on NFC "
8895 "connection handover (freq=%d)",
8896 params->go_freq);
8897
8898 if (params->go_freq && params->go_ssid_len) {
8899 wpa_s->p2p_wps_method = WPS_NFC;
8900 wpa_s->pending_join_wps_method = WPS_NFC;
8901 os_memset(wpa_s->pending_join_iface_addr, 0, ETH_ALEN);
8902 os_memcpy(wpa_s->pending_join_dev_addr, params->go_dev_addr,
8903 ETH_ALEN);
8904 return wpas_p2p_join_start(wpa_s, params->go_freq,
8905 params->go_ssid,
8906 params->go_ssid_len);
8907 }
8908
8909 return wpas_p2p_connect(wpa_s, params->peer->p2p_device_addr, NULL,
8910 WPS_NFC, 0, 0, 1, 0, wpa_s->conf->p2p_go_intent,
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08008911 params->go_freq, wpa_s->p2p_go_vht_center_freq2,
Dmitry Shmidtde47be72016-01-07 12:52:55 -08008912 -1, 0, 1, 1, wpa_s->p2p_go_max_oper_chwidth,
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08008913 wpa_s->p2p_go_he, wpa_s->p2p_go_edmg,
Dmitry Shmidtde47be72016-01-07 12:52:55 -08008914 params->go_ssid_len ? params->go_ssid : NULL,
8915 params->go_ssid_len);
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08008916}
8917
8918
8919static int wpas_p2p_nfc_auth_join(struct wpa_supplicant *wpa_s,
8920 struct p2p_nfc_params *params, int tag)
8921{
8922 int res, persistent;
8923 struct wpa_ssid *ssid;
8924
8925 wpa_printf(MSG_DEBUG, "P2P: Authorize join-group based on NFC "
8926 "connection handover");
8927 for (wpa_s = wpa_s->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
8928 ssid = wpa_s->current_ssid;
8929 if (ssid == NULL)
8930 continue;
8931 if (ssid->mode != WPAS_MODE_P2P_GO)
8932 continue;
8933 if (wpa_s->ap_iface == NULL)
8934 continue;
8935 break;
8936 }
8937 if (wpa_s == NULL) {
8938 wpa_printf(MSG_DEBUG, "P2P: Could not find GO interface");
8939 return -1;
8940 }
8941
Dmitry Shmidt849734c2016-05-27 09:59:01 -07008942 if (wpa_s->p2pdev->p2p_oob_dev_pw_id !=
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08008943 DEV_PW_NFC_CONNECTION_HANDOVER &&
Dmitry Shmidt849734c2016-05-27 09:59:01 -07008944 !wpa_s->p2pdev->p2p_oob_dev_pw) {
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08008945 wpa_printf(MSG_DEBUG, "P2P: No NFC Dev Pw known");
8946 return -1;
8947 }
8948 res = wpas_ap_wps_add_nfc_pw(
Dmitry Shmidt849734c2016-05-27 09:59:01 -07008949 wpa_s, wpa_s->p2pdev->p2p_oob_dev_pw_id,
8950 wpa_s->p2pdev->p2p_oob_dev_pw,
8951 wpa_s->p2pdev->p2p_peer_oob_pk_hash_known ?
8952 wpa_s->p2pdev->p2p_peer_oob_pubkey_hash : NULL);
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08008953 if (res)
8954 return res;
8955
8956 if (!tag) {
8957 wpa_printf(MSG_DEBUG, "P2P: Negotiated handover - wait for peer to join without invitation");
8958 return 0;
8959 }
8960
8961 if (!params->peer ||
8962 !(params->peer->dev_capab & P2P_DEV_CAPAB_INVITATION_PROCEDURE))
8963 return 0;
8964
8965 wpa_printf(MSG_DEBUG, "P2P: Static handover - invite peer " MACSTR
8966 " to join", MAC2STR(params->peer->p2p_device_addr));
8967
8968 wpa_s->global->p2p_invite_group = wpa_s;
8969 persistent = ssid->p2p_persistent_group &&
Dmitry Shmidt849734c2016-05-27 09:59:01 -07008970 wpas_p2p_get_persistent(wpa_s->p2pdev,
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08008971 params->peer->p2p_device_addr,
8972 ssid->ssid, ssid->ssid_len);
Dmitry Shmidt849734c2016-05-27 09:59:01 -07008973 wpa_s->p2pdev->pending_invite_ssid_id = -1;
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08008974
8975 return p2p_invite(wpa_s->global->p2p, params->peer->p2p_device_addr,
8976 P2P_INVITE_ROLE_ACTIVE_GO, wpa_s->own_addr,
8977 ssid->ssid, ssid->ssid_len, ssid->frequency,
8978 wpa_s->global->p2p_dev_addr, persistent, 0,
Dmitry Shmidt849734c2016-05-27 09:59:01 -07008979 wpa_s->p2pdev->p2p_oob_dev_pw_id);
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08008980}
8981
8982
8983static int wpas_p2p_nfc_init_go_neg(struct wpa_supplicant *wpa_s,
8984 struct p2p_nfc_params *params,
8985 int forced_freq)
8986{
8987 wpa_printf(MSG_DEBUG, "P2P: Initiate GO Negotiation based on NFC "
8988 "connection handover");
8989 return wpas_p2p_connect(wpa_s, params->peer->p2p_device_addr, NULL,
8990 WPS_NFC, 0, 0, 0, 0, wpa_s->conf->p2p_go_intent,
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08008991 forced_freq, wpa_s->p2p_go_vht_center_freq2,
Dmitry Shmidtde47be72016-01-07 12:52:55 -08008992 -1, 0, 1, 1, wpa_s->p2p_go_max_oper_chwidth,
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08008993 wpa_s->p2p_go_he, wpa_s->p2p_go_edmg,
8994 NULL, 0);
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08008995}
8996
8997
8998static int wpas_p2p_nfc_resp_go_neg(struct wpa_supplicant *wpa_s,
8999 struct p2p_nfc_params *params,
9000 int forced_freq)
9001{
9002 int res;
9003
9004 wpa_printf(MSG_DEBUG, "P2P: Authorize GO Negotiation based on NFC "
9005 "connection handover");
9006 res = wpas_p2p_connect(wpa_s, params->peer->p2p_device_addr, NULL,
9007 WPS_NFC, 0, 0, 0, 1, wpa_s->conf->p2p_go_intent,
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08009008 forced_freq, wpa_s->p2p_go_vht_center_freq2,
Dmitry Shmidtde47be72016-01-07 12:52:55 -08009009 -1, 0, 1, 1, wpa_s->p2p_go_max_oper_chwidth,
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08009010 wpa_s->p2p_go_he, wpa_s->p2p_go_edmg,
9011 NULL, 0);
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08009012 if (res)
9013 return res;
9014
9015 res = wpas_p2p_listen(wpa_s, 60);
9016 if (res) {
9017 p2p_unauthorize(wpa_s->global->p2p,
9018 params->peer->p2p_device_addr);
9019 }
9020
9021 return res;
9022}
9023
9024
9025static int wpas_p2p_nfc_connection_handover(struct wpa_supplicant *wpa_s,
9026 const struct wpabuf *data,
9027 int sel, int tag, int forced_freq)
9028{
9029 const u8 *pos, *end;
9030 u16 len, id;
9031 struct p2p_nfc_params params;
9032 int res;
9033
9034 os_memset(&params, 0, sizeof(params));
9035 params.sel = sel;
9036
9037 wpa_hexdump_buf(MSG_DEBUG, "P2P: Received NFC tag payload", data);
9038
9039 pos = wpabuf_head(data);
9040 end = pos + wpabuf_len(data);
9041
9042 if (end - pos < 2) {
9043 wpa_printf(MSG_DEBUG, "P2P: Not enough data for Length of WSC "
9044 "attributes");
9045 return -1;
9046 }
9047 len = WPA_GET_BE16(pos);
9048 pos += 2;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08009049 if (len > end - pos) {
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08009050 wpa_printf(MSG_DEBUG, "P2P: Not enough data for WSC "
9051 "attributes");
9052 return -1;
9053 }
9054 params.wsc_attr = pos;
9055 params.wsc_len = len;
9056 pos += len;
9057
9058 if (end - pos < 2) {
9059 wpa_printf(MSG_DEBUG, "P2P: Not enough data for Length of P2P "
9060 "attributes");
9061 return -1;
9062 }
9063 len = WPA_GET_BE16(pos);
9064 pos += 2;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08009065 if (len > end - pos) {
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08009066 wpa_printf(MSG_DEBUG, "P2P: Not enough data for P2P "
9067 "attributes");
9068 return -1;
9069 }
9070 params.p2p_attr = pos;
9071 params.p2p_len = len;
9072 pos += len;
9073
9074 wpa_hexdump(MSG_DEBUG, "P2P: WSC attributes",
9075 params.wsc_attr, params.wsc_len);
9076 wpa_hexdump(MSG_DEBUG, "P2P: P2P attributes",
9077 params.p2p_attr, params.p2p_len);
9078 if (pos < end) {
9079 wpa_hexdump(MSG_DEBUG,
9080 "P2P: Ignored extra data after P2P attributes",
9081 pos, end - pos);
9082 }
9083
9084 res = p2p_process_nfc_connection_handover(wpa_s->global->p2p, &params);
9085 if (res)
9086 return res;
9087
9088 if (params.next_step == NO_ACTION)
9089 return 0;
9090
9091 if (params.next_step == BOTH_GO) {
9092 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_NFC_BOTH_GO "peer=" MACSTR,
9093 MAC2STR(params.peer->p2p_device_addr));
9094 return 0;
9095 }
9096
9097 if (params.next_step == PEER_CLIENT) {
9098 if (!is_zero_ether_addr(params.go_dev_addr)) {
9099 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_NFC_PEER_CLIENT
9100 "peer=" MACSTR " freq=%d go_dev_addr=" MACSTR
9101 " ssid=\"%s\"",
9102 MAC2STR(params.peer->p2p_device_addr),
9103 params.go_freq,
9104 MAC2STR(params.go_dev_addr),
9105 wpa_ssid_txt(params.go_ssid,
9106 params.go_ssid_len));
9107 } else {
9108 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_NFC_PEER_CLIENT
9109 "peer=" MACSTR " freq=%d",
9110 MAC2STR(params.peer->p2p_device_addr),
9111 params.go_freq);
9112 }
9113 return 0;
9114 }
9115
9116 if (wpas_p2p_cli_freq(wpa_s, NULL, NULL)) {
9117 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_NFC_WHILE_CLIENT "peer="
9118 MACSTR, MAC2STR(params.peer->p2p_device_addr));
9119 return 0;
9120 }
9121
9122 wpabuf_free(wpa_s->p2p_oob_dev_pw);
9123 wpa_s->p2p_oob_dev_pw = NULL;
9124
9125 if (params.oob_dev_pw_len < WPS_OOB_PUBKEY_HASH_LEN + 2) {
9126 wpa_printf(MSG_DEBUG, "P2P: No peer OOB Dev Pw "
9127 "received");
9128 return -1;
9129 }
9130
9131 id = WPA_GET_BE16(params.oob_dev_pw + WPS_OOB_PUBKEY_HASH_LEN);
9132 wpa_printf(MSG_DEBUG, "P2P: Peer OOB Dev Pw %u", id);
9133 wpa_hexdump(MSG_DEBUG, "P2P: Peer OOB Public Key hash",
9134 params.oob_dev_pw, WPS_OOB_PUBKEY_HASH_LEN);
9135 os_memcpy(wpa_s->p2p_peer_oob_pubkey_hash,
9136 params.oob_dev_pw, WPS_OOB_PUBKEY_HASH_LEN);
9137 wpa_s->p2p_peer_oob_pk_hash_known = 1;
9138
9139 if (tag) {
9140 if (id < 0x10) {
9141 wpa_printf(MSG_DEBUG, "P2P: Static handover - invalid "
9142 "peer OOB Device Password Id %u", id);
9143 return -1;
9144 }
9145 wpa_printf(MSG_DEBUG, "P2P: Static handover - use peer OOB "
9146 "Device Password Id %u", id);
9147 wpa_hexdump_key(MSG_DEBUG, "P2P: Peer OOB Device Password",
9148 params.oob_dev_pw + WPS_OOB_PUBKEY_HASH_LEN + 2,
9149 params.oob_dev_pw_len -
9150 WPS_OOB_PUBKEY_HASH_LEN - 2);
9151 wpa_s->p2p_oob_dev_pw_id = id;
9152 wpa_s->p2p_oob_dev_pw = wpabuf_alloc_copy(
9153 params.oob_dev_pw + WPS_OOB_PUBKEY_HASH_LEN + 2,
9154 params.oob_dev_pw_len -
9155 WPS_OOB_PUBKEY_HASH_LEN - 2);
9156 if (wpa_s->p2p_oob_dev_pw == NULL)
9157 return -1;
9158
9159 if (wpa_s->conf->wps_nfc_dh_pubkey == NULL &&
9160 wps_nfc_gen_dh(&wpa_s->conf->wps_nfc_dh_pubkey,
9161 &wpa_s->conf->wps_nfc_dh_privkey) < 0)
9162 return -1;
9163 } else {
9164 wpa_printf(MSG_DEBUG, "P2P: Using abbreviated WPS handshake "
9165 "without Device Password");
9166 wpa_s->p2p_oob_dev_pw_id = DEV_PW_NFC_CONNECTION_HANDOVER;
9167 }
9168
9169 switch (params.next_step) {
9170 case NO_ACTION:
9171 case BOTH_GO:
9172 case PEER_CLIENT:
9173 /* already covered above */
9174 return 0;
9175 case JOIN_GROUP:
9176 return wpas_p2p_nfc_join_group(wpa_s, &params);
9177 case AUTH_JOIN:
9178 return wpas_p2p_nfc_auth_join(wpa_s, &params, tag);
9179 case INIT_GO_NEG:
9180 return wpas_p2p_nfc_init_go_neg(wpa_s, &params, forced_freq);
9181 case RESP_GO_NEG:
9182 /* TODO: use own OOB Dev Pw */
9183 return wpas_p2p_nfc_resp_go_neg(wpa_s, &params, forced_freq);
9184 }
9185
9186 return -1;
9187}
9188
9189
9190int wpas_p2p_nfc_tag_process(struct wpa_supplicant *wpa_s,
9191 const struct wpabuf *data, int forced_freq)
9192{
9193 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
9194 return -1;
9195
9196 return wpas_p2p_nfc_connection_handover(wpa_s, data, 1, 1, forced_freq);
9197}
9198
9199
9200int wpas_p2p_nfc_report_handover(struct wpa_supplicant *wpa_s, int init,
9201 const struct wpabuf *req,
9202 const struct wpabuf *sel, int forced_freq)
9203{
9204 struct wpabuf *tmp;
9205 int ret;
9206
9207 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
9208 return -1;
9209
9210 wpa_printf(MSG_DEBUG, "NFC: P2P connection handover reported");
9211
9212 wpa_hexdump_ascii(MSG_DEBUG, "NFC: Req",
9213 wpabuf_head(req), wpabuf_len(req));
9214 wpa_hexdump_ascii(MSG_DEBUG, "NFC: Sel",
9215 wpabuf_head(sel), wpabuf_len(sel));
9216 if (forced_freq)
9217 wpa_printf(MSG_DEBUG, "NFC: Forced freq %d", forced_freq);
9218 tmp = ndef_parse_p2p(init ? sel : req);
9219 if (tmp == NULL) {
9220 wpa_printf(MSG_DEBUG, "P2P: Could not parse NDEF");
9221 return -1;
9222 }
9223
9224 ret = wpas_p2p_nfc_connection_handover(wpa_s, tmp, init, 0,
9225 forced_freq);
9226 wpabuf_free(tmp);
9227
9228 return ret;
9229}
9230
9231
9232int wpas_p2p_nfc_tag_enabled(struct wpa_supplicant *wpa_s, int enabled)
9233{
9234 const u8 *if_addr;
9235 int go_intent = wpa_s->conf->p2p_go_intent;
9236 struct wpa_supplicant *iface;
9237
9238 if (wpa_s->global->p2p == NULL)
9239 return -1;
9240
9241 if (!enabled) {
9242 wpa_printf(MSG_DEBUG, "P2P: Disable use of own NFC Tag");
9243 for (iface = wpa_s->global->ifaces; iface; iface = iface->next)
9244 {
9245 if (!iface->ap_iface)
9246 continue;
9247 hostapd_wps_nfc_token_disable(iface->ap_iface->bss[0]);
9248 }
9249 p2p_set_authorized_oob_dev_pw_id(wpa_s->global->p2p, 0,
9250 0, NULL);
9251 if (wpa_s->p2p_nfc_tag_enabled)
9252 wpas_p2p_remove_pending_group_interface(wpa_s);
9253 wpa_s->p2p_nfc_tag_enabled = 0;
9254 return 0;
9255 }
9256
9257 if (wpa_s->global->p2p_disabled)
9258 return -1;
9259
9260 if (wpa_s->conf->wps_nfc_dh_pubkey == NULL ||
9261 wpa_s->conf->wps_nfc_dh_privkey == NULL ||
9262 wpa_s->conf->wps_nfc_dev_pw == NULL ||
9263 wpa_s->conf->wps_nfc_dev_pw_id < 0x10) {
9264 wpa_printf(MSG_DEBUG, "P2P: NFC password token not configured "
9265 "to allow static handover cases");
9266 return -1;
9267 }
9268
9269 wpa_printf(MSG_DEBUG, "P2P: Enable use of own NFC Tag");
9270
9271 wpa_s->p2p_oob_dev_pw_id = wpa_s->conf->wps_nfc_dev_pw_id;
9272 wpabuf_free(wpa_s->p2p_oob_dev_pw);
9273 wpa_s->p2p_oob_dev_pw = wpabuf_dup(wpa_s->conf->wps_nfc_dev_pw);
9274 if (wpa_s->p2p_oob_dev_pw == NULL)
9275 return -1;
9276 wpa_s->p2p_peer_oob_pk_hash_known = 0;
9277
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -07009278 if (wpa_s->p2p_group_interface == P2P_GROUP_INTERFACE_GO ||
9279 wpa_s->p2p_group_interface == P2P_GROUP_INTERFACE_CLIENT) {
9280 /*
9281 * P2P Group Interface present and the command came on group
9282 * interface, so enable the token for the current interface.
9283 */
9284 wpa_s->create_p2p_iface = 0;
9285 } else {
9286 wpa_s->create_p2p_iface = wpas_p2p_create_iface(wpa_s);
9287 }
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08009288
9289 if (wpa_s->create_p2p_iface) {
9290 enum wpa_driver_if_type iftype;
9291 /* Prepare to add a new interface for the group */
9292 iftype = WPA_IF_P2P_GROUP;
9293 if (go_intent == 15)
9294 iftype = WPA_IF_P2P_GO;
9295 if (wpas_p2p_add_group_interface(wpa_s, iftype) < 0) {
9296 wpa_printf(MSG_ERROR, "P2P: Failed to allocate a new "
9297 "interface for the group");
9298 return -1;
9299 }
9300
9301 if_addr = wpa_s->pending_interface_addr;
Dmitry Shmidt9c175262016-03-03 10:20:07 -08009302 } else if (wpa_s->p2p_mgmt)
9303 if_addr = wpa_s->parent->own_addr;
9304 else
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08009305 if_addr = wpa_s->own_addr;
9306
9307 wpa_s->p2p_nfc_tag_enabled = enabled;
9308
9309 for (iface = wpa_s->global->ifaces; iface; iface = iface->next) {
9310 struct hostapd_data *hapd;
9311 if (iface->ap_iface == NULL)
9312 continue;
9313 hapd = iface->ap_iface->bss[0];
9314 wpabuf_free(hapd->conf->wps_nfc_dh_pubkey);
9315 hapd->conf->wps_nfc_dh_pubkey =
9316 wpabuf_dup(wpa_s->conf->wps_nfc_dh_pubkey);
9317 wpabuf_free(hapd->conf->wps_nfc_dh_privkey);
9318 hapd->conf->wps_nfc_dh_privkey =
9319 wpabuf_dup(wpa_s->conf->wps_nfc_dh_privkey);
9320 wpabuf_free(hapd->conf->wps_nfc_dev_pw);
9321 hapd->conf->wps_nfc_dev_pw =
9322 wpabuf_dup(wpa_s->conf->wps_nfc_dev_pw);
9323 hapd->conf->wps_nfc_dev_pw_id = wpa_s->conf->wps_nfc_dev_pw_id;
9324
9325 if (hostapd_wps_nfc_token_enable(iface->ap_iface->bss[0]) < 0) {
9326 wpa_dbg(iface, MSG_DEBUG,
9327 "P2P: Failed to enable NFC Tag for GO");
9328 }
9329 }
9330 p2p_set_authorized_oob_dev_pw_id(
9331 wpa_s->global->p2p, wpa_s->conf->wps_nfc_dev_pw_id, go_intent,
9332 if_addr);
9333
9334 return 0;
9335}
9336
9337#endif /* CONFIG_WPS_NFC */
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07009338
9339
9340static void wpas_p2p_optimize_listen_channel(struct wpa_supplicant *wpa_s,
9341 struct wpa_used_freq_data *freqs,
9342 unsigned int num)
9343{
9344 u8 curr_chan, cand, chan;
9345 unsigned int i;
9346
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08009347 /*
9348 * If possible, optimize the Listen channel to be a channel that is
9349 * already used by one of the other interfaces.
9350 */
9351 if (!wpa_s->conf->p2p_optimize_listen_chan)
9352 return;
9353
9354 if (!wpa_s->current_ssid || wpa_s->wpa_state != WPA_COMPLETED)
9355 return;
9356
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07009357 curr_chan = p2p_get_listen_channel(wpa_s->global->p2p);
9358 for (i = 0, cand = 0; i < num; i++) {
9359 ieee80211_freq_to_chan(freqs[i].freq, &chan);
9360 if (curr_chan == chan) {
9361 cand = 0;
9362 break;
9363 }
9364
9365 if (chan == 1 || chan == 6 || chan == 11)
9366 cand = chan;
9367 }
9368
9369 if (cand) {
9370 wpa_dbg(wpa_s, MSG_DEBUG,
Dmitry Shmidt216983b2015-02-06 10:50:36 -08009371 "P2P: Update Listen channel to %u based on operating channel",
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07009372 cand);
9373 p2p_set_listen_channel(wpa_s->global->p2p, 81, cand, 0);
9374 }
9375}
9376
9377
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08009378static int wpas_p2p_move_go_csa(struct wpa_supplicant *wpa_s)
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07009379{
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08009380 struct hostapd_config *conf;
9381 struct p2p_go_neg_results params;
9382 struct csa_settings csa_settings;
9383 struct wpa_ssid *current_ssid = wpa_s->current_ssid;
9384 int old_freq = current_ssid->frequency;
9385 int ret;
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07009386
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08009387 if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_AP_CSA)) {
9388 wpa_dbg(wpa_s, MSG_DEBUG, "CSA is not enabled");
9389 return -1;
9390 }
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07009391
9392 /*
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08009393 * TODO: This function may not always work correctly. For example,
9394 * when we have a running GO and a BSS on a DFS channel.
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07009395 */
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08009396 if (wpas_p2p_init_go_params(wpa_s, &params, 0, 0, 0, 0, 0, 0, 0,
9397 NULL)) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08009398 wpa_dbg(wpa_s, MSG_DEBUG,
9399 "P2P CSA: Failed to select new frequency for GO");
9400 return -1;
9401 }
9402
9403 if (current_ssid->frequency == params.freq) {
9404 wpa_dbg(wpa_s, MSG_DEBUG,
9405 "P2P CSA: Selected same frequency - not moving GO");
9406 return 0;
9407 }
9408
9409 conf = hostapd_config_defaults();
9410 if (!conf) {
9411 wpa_dbg(wpa_s, MSG_DEBUG,
9412 "P2P CSA: Failed to allocate default config");
9413 return -1;
9414 }
9415
9416 current_ssid->frequency = params.freq;
9417 if (wpa_supplicant_conf_ap_ht(wpa_s, current_ssid, conf)) {
9418 wpa_dbg(wpa_s, MSG_DEBUG,
9419 "P2P CSA: Failed to create new GO config");
9420 ret = -1;
9421 goto out;
9422 }
9423
9424 if (conf->hw_mode != wpa_s->ap_iface->current_mode->mode) {
9425 wpa_dbg(wpa_s, MSG_DEBUG,
9426 "P2P CSA: CSA to a different band is not supported");
9427 ret = -1;
9428 goto out;
9429 }
9430
9431 os_memset(&csa_settings, 0, sizeof(csa_settings));
9432 csa_settings.cs_count = P2P_GO_CSA_COUNT;
9433 csa_settings.block_tx = P2P_GO_CSA_BLOCK_TX;
9434 csa_settings.freq_params.freq = params.freq;
9435 csa_settings.freq_params.sec_channel_offset = conf->secondary_channel;
9436 csa_settings.freq_params.ht_enabled = conf->ieee80211n;
9437 csa_settings.freq_params.bandwidth = conf->secondary_channel ? 40 : 20;
9438
9439 if (conf->ieee80211ac) {
9440 int freq1 = 0, freq2 = 0;
9441 u8 chan, opclass;
9442
9443 if (ieee80211_freq_to_channel_ext(params.freq,
9444 conf->secondary_channel,
9445 conf->vht_oper_chwidth,
9446 &opclass, &chan) ==
9447 NUM_HOSTAPD_MODES) {
9448 wpa_printf(MSG_ERROR, "P2P CSA: Bad freq");
9449 ret = -1;
9450 goto out;
9451 }
9452
9453 if (conf->vht_oper_centr_freq_seg0_idx)
9454 freq1 = ieee80211_chan_to_freq(
9455 NULL, opclass,
9456 conf->vht_oper_centr_freq_seg0_idx);
9457
9458 if (conf->vht_oper_centr_freq_seg1_idx)
9459 freq2 = ieee80211_chan_to_freq(
9460 NULL, opclass,
9461 conf->vht_oper_centr_freq_seg1_idx);
9462
9463 if (freq1 < 0 || freq2 < 0) {
9464 wpa_dbg(wpa_s, MSG_DEBUG,
9465 "P2P CSA: Selected invalid VHT center freqs");
9466 ret = -1;
9467 goto out;
9468 }
9469
9470 csa_settings.freq_params.vht_enabled = conf->ieee80211ac;
9471 csa_settings.freq_params.center_freq1 = freq1;
9472 csa_settings.freq_params.center_freq2 = freq2;
9473
9474 switch (conf->vht_oper_chwidth) {
Hai Shalom81f62d82019-07-22 12:10:00 -07009475 case CHANWIDTH_80MHZ:
9476 case CHANWIDTH_80P80MHZ:
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08009477 csa_settings.freq_params.bandwidth = 80;
9478 break;
Hai Shalom81f62d82019-07-22 12:10:00 -07009479 case CHANWIDTH_160MHZ:
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08009480 csa_settings.freq_params.bandwidth = 160;
9481 break;
9482 }
9483 }
9484
9485 ret = ap_switch_channel(wpa_s, &csa_settings);
9486out:
9487 current_ssid->frequency = old_freq;
9488 hostapd_config_free(conf);
9489 return ret;
9490}
9491
9492
9493static void wpas_p2p_move_go_no_csa(struct wpa_supplicant *wpa_s)
9494{
9495 struct p2p_go_neg_results params;
9496 struct wpa_ssid *current_ssid = wpa_s->current_ssid;
Hai Shalom899fcc72020-10-19 14:38:18 -07009497 void (*ap_configured_cb)(void *ctx, void *data);
9498 void *ap_configured_cb_ctx, *ap_configured_cb_data;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08009499
9500 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_REMOVE_AND_REFORM_GROUP);
9501
9502 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Move GO from freq=%d MHz",
9503 current_ssid->frequency);
9504
9505 /* Stop the AP functionality */
9506 /* TODO: Should do this in a way that does not indicated to possible
9507 * P2P Clients in the group that the group is terminated. */
Hai Shalom899fcc72020-10-19 14:38:18 -07009508 /* If this action occurs before a group is started, the callback should
9509 * be preserved, or GROUP-STARTED event would be lost. If this action
9510 * occurs after a group is started, these pointers are all NULL and
9511 * harmless. */
9512 ap_configured_cb = wpa_s->ap_configured_cb;
9513 ap_configured_cb_ctx = wpa_s->ap_configured_cb_ctx;
9514 ap_configured_cb_data = wpa_s->ap_configured_cb_data;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08009515 wpa_supplicant_ap_deinit(wpa_s);
9516
9517 /* Reselect the GO frequency */
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08009518 if (wpas_p2p_init_go_params(wpa_s, &params, 0, 0, 0, 0, 0, 0, 0,
9519 NULL)) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08009520 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Failed to reselect freq");
9521 wpas_p2p_group_delete(wpa_s,
9522 P2P_GROUP_REMOVAL_GO_LEAVE_CHANNEL);
9523 return;
9524 }
9525 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: New freq selected for the GO (%u MHz)",
9526 params.freq);
9527
9528 if (params.freq &&
9529 !p2p_supported_freq_go(wpa_s->global->p2p, params.freq)) {
9530 wpa_printf(MSG_DEBUG,
9531 "P2P: Selected freq (%u MHz) is not valid for P2P",
9532 params.freq);
9533 wpas_p2p_group_delete(wpa_s,
9534 P2P_GROUP_REMOVAL_GO_LEAVE_CHANNEL);
9535 return;
9536 }
9537
Hai Shalom899fcc72020-10-19 14:38:18 -07009538 /* Restore preserved callback parameters */
Jimmy Chen4b2a5022020-08-26 18:55:45 +08009539 wpa_s->ap_configured_cb = ap_configured_cb;
9540 wpa_s->ap_configured_cb_ctx = ap_configured_cb_ctx;
9541 wpa_s->ap_configured_cb_data = ap_configured_cb_data;
Hai Shalom899fcc72020-10-19 14:38:18 -07009542
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08009543 /* Update the frequency */
9544 current_ssid->frequency = params.freq;
9545 wpa_s->connect_without_scan = current_ssid;
9546 wpa_s->reassociate = 1;
9547 wpa_s->disconnected = 0;
9548 wpa_supplicant_req_scan(wpa_s, 0, 0);
9549}
9550
9551
9552static void wpas_p2p_move_go(void *eloop_ctx, void *timeout_ctx)
9553{
9554 struct wpa_supplicant *wpa_s = eloop_ctx;
9555
9556 if (!wpa_s->ap_iface || !wpa_s->current_ssid)
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07009557 return;
9558
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08009559 wpas_p2p_go_update_common_freqs(wpa_s);
9560
9561 /* Do not move GO in the middle of a CSA */
9562 if (hostapd_csa_in_progress(wpa_s->ap_iface)) {
9563 wpa_printf(MSG_DEBUG,
9564 "P2P: CSA is in progress - not moving GO");
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07009565 return;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08009566 }
9567
9568 /*
9569 * First, try a channel switch flow. If it is not supported or fails,
9570 * take down the GO and bring it up again.
9571 */
9572 if (wpas_p2p_move_go_csa(wpa_s) < 0)
9573 wpas_p2p_move_go_no_csa(wpa_s);
9574}
9575
9576
9577static void wpas_p2p_reconsider_moving_go(void *eloop_ctx, void *timeout_ctx)
9578{
9579 struct wpa_supplicant *wpa_s = eloop_ctx;
9580 struct wpa_used_freq_data *freqs = NULL;
9581 unsigned int num = wpa_s->num_multichan_concurrent;
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07009582
9583 freqs = os_calloc(num, sizeof(struct wpa_used_freq_data));
9584 if (!freqs)
9585 return;
9586
9587 num = get_shared_radio_freqs_data(wpa_s, freqs, num);
9588
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08009589 /* Previous attempt to move a GO was not possible -- try again. */
9590 wpas_p2p_consider_moving_gos(wpa_s, freqs, num,
9591 WPAS_P2P_CHANNEL_UPDATE_ANY);
9592
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07009593 os_free(freqs);
9594}
9595
9596
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08009597/*
9598 * Consider moving a GO from its currently used frequency:
9599 * 1. It is possible that due to regulatory consideration the frequency
9600 * can no longer be used and there is a need to evacuate the GO.
9601 * 2. It is possible that due to MCC considerations, it would be preferable
9602 * to move the GO to a channel that is currently used by some other
9603 * station interface.
9604 *
9605 * In case a frequency that became invalid is once again valid, cancel a
9606 * previously initiated GO frequency change.
9607 */
9608static void wpas_p2p_consider_moving_one_go(struct wpa_supplicant *wpa_s,
9609 struct wpa_used_freq_data *freqs,
9610 unsigned int num)
9611{
9612 unsigned int i, invalid_freq = 0, policy_move = 0, flags = 0;
9613 unsigned int timeout;
9614 int freq;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07009615 int dfs_offload;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08009616
9617 wpas_p2p_go_update_common_freqs(wpa_s);
9618
9619 freq = wpa_s->current_ssid->frequency;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07009620 dfs_offload = (wpa_s->drv_flags & WPA_DRIVER_FLAGS_DFS_OFFLOAD) &&
Roshan Pius3a1667e2018-07-03 15:17:14 -07009621 ieee80211_is_dfs(freq, wpa_s->hw.modes, wpa_s->hw.num_modes);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08009622 for (i = 0, invalid_freq = 0; i < num; i++) {
9623 if (freqs[i].freq == freq) {
9624 flags = freqs[i].flags;
9625
9626 /* The channel is invalid, must change it */
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07009627 if (!p2p_supported_freq_go(wpa_s->global->p2p, freq) &&
9628 !dfs_offload) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08009629 wpa_dbg(wpa_s, MSG_DEBUG,
9630 "P2P: Freq=%d MHz no longer valid for GO",
9631 freq);
9632 invalid_freq = 1;
9633 }
9634 } else if (freqs[i].flags == 0) {
9635 /* Freq is not used by any other station interface */
9636 continue;
9637 } else if (!p2p_supported_freq(wpa_s->global->p2p,
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07009638 freqs[i].freq) && !dfs_offload) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08009639 /* Freq is not valid for P2P use cases */
9640 continue;
9641 } else if (wpa_s->conf->p2p_go_freq_change_policy ==
9642 P2P_GO_FREQ_MOVE_SCM) {
9643 policy_move = 1;
9644 } else if (wpa_s->conf->p2p_go_freq_change_policy ==
9645 P2P_GO_FREQ_MOVE_SCM_PEER_SUPPORTS &&
9646 wpas_p2p_go_is_peer_freq(wpa_s, freqs[i].freq)) {
9647 policy_move = 1;
9648 } else if ((wpa_s->conf->p2p_go_freq_change_policy ==
9649 P2P_GO_FREQ_MOVE_SCM_ECSA) &&
9650 wpas_p2p_go_is_peer_freq(wpa_s, freqs[i].freq)) {
9651 if (!p2p_get_group_num_members(wpa_s->p2p_group)) {
9652 policy_move = 1;
9653 } else if ((wpa_s->drv_flags &
9654 WPA_DRIVER_FLAGS_AP_CSA) &&
9655 wpas_p2p_go_clients_support_ecsa(wpa_s)) {
9656 u8 chan;
9657
9658 /*
9659 * We do not support CSA between bands, so move
9660 * GO only within the same band.
9661 */
9662 if (wpa_s->ap_iface->current_mode->mode ==
9663 ieee80211_freq_to_chan(freqs[i].freq,
9664 &chan))
9665 policy_move = 1;
9666 }
9667 }
9668 }
9669
9670 wpa_dbg(wpa_s, MSG_DEBUG,
9671 "P2P: GO move: invalid_freq=%u, policy_move=%u, flags=0x%X",
9672 invalid_freq, policy_move, flags);
9673
9674 /*
9675 * The channel is valid, or we are going to have a policy move, so
9676 * cancel timeout.
9677 */
9678 if (!invalid_freq || policy_move) {
9679 wpa_dbg(wpa_s, MSG_DEBUG,
9680 "P2P: Cancel a GO move from freq=%d MHz", freq);
9681 eloop_cancel_timeout(wpas_p2p_move_go, wpa_s, NULL);
9682
9683 if (wpas_p2p_in_progress(wpa_s)) {
9684 wpa_dbg(wpa_s, MSG_DEBUG,
9685 "P2P: GO move: policy CS is not allowed - setting timeout to re-consider GO move");
9686 eloop_cancel_timeout(wpas_p2p_reconsider_moving_go,
9687 wpa_s, NULL);
9688 eloop_register_timeout(P2P_RECONSIDER_GO_MOVE_DELAY, 0,
9689 wpas_p2p_reconsider_moving_go,
9690 wpa_s, NULL);
9691 return;
9692 }
9693 }
9694
9695 if (!invalid_freq && (!policy_move || flags != 0)) {
9696 wpa_dbg(wpa_s, MSG_DEBUG,
9697 "P2P: Not initiating a GO frequency change");
9698 return;
9699 }
9700
9701 /*
9702 * Do not consider moving GO if it is in the middle of a CSA. When the
9703 * CSA is finished this flow should be retriggered.
9704 */
9705 if (hostapd_csa_in_progress(wpa_s->ap_iface)) {
9706 wpa_dbg(wpa_s, MSG_DEBUG,
9707 "P2P: Not initiating a GO frequency change - CSA is in progress");
9708 return;
9709 }
9710
9711 if (invalid_freq && !wpas_p2p_disallowed_freq(wpa_s->global, freq))
9712 timeout = P2P_GO_FREQ_CHANGE_TIME;
9713 else
9714 timeout = 0;
9715
9716 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Move GO from freq=%d MHz in %d secs",
9717 freq, timeout);
9718 eloop_cancel_timeout(wpas_p2p_move_go, wpa_s, NULL);
9719 eloop_register_timeout(timeout, 0, wpas_p2p_move_go, wpa_s, NULL);
9720}
9721
9722
9723static void wpas_p2p_consider_moving_gos(struct wpa_supplicant *wpa_s,
9724 struct wpa_used_freq_data *freqs,
9725 unsigned int num,
9726 enum wpas_p2p_channel_update_trig trig)
9727{
9728 struct wpa_supplicant *ifs;
9729
9730 eloop_cancel_timeout(wpas_p2p_reconsider_moving_go, ELOOP_ALL_CTX,
9731 NULL);
9732
9733 /*
9734 * Travers all the radio interfaces, and for each GO interface, check
9735 * if there is a need to move the GO from the frequency it is using,
9736 * or in case the frequency is valid again, cancel the evacuation flow.
9737 */
9738 dl_list_for_each(ifs, &wpa_s->radio->ifaces, struct wpa_supplicant,
9739 radio_list) {
9740 if (ifs->current_ssid == NULL ||
9741 ifs->current_ssid->mode != WPAS_MODE_P2P_GO)
9742 continue;
9743
9744 /*
9745 * The GO was just started or completed channel switch, no need
9746 * to move it.
9747 */
9748 if (wpa_s == ifs &&
9749 (trig == WPAS_P2P_CHANNEL_UPDATE_STATE_CHANGE ||
9750 trig == WPAS_P2P_CHANNEL_UPDATE_CS)) {
9751 wpa_dbg(wpa_s, MSG_DEBUG,
9752 "P2P: GO move - schedule re-consideration");
9753 eloop_register_timeout(P2P_RECONSIDER_GO_MOVE_DELAY, 0,
9754 wpas_p2p_reconsider_moving_go,
9755 wpa_s, NULL);
9756 continue;
9757 }
9758
9759 wpas_p2p_consider_moving_one_go(ifs, freqs, num);
9760 }
9761}
9762
9763
9764void wpas_p2p_indicate_state_change(struct wpa_supplicant *wpa_s)
9765{
9766 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
9767 return;
9768
9769 wpas_p2p_update_channel_list(wpa_s,
9770 WPAS_P2P_CHANNEL_UPDATE_STATE_CHANGE);
9771}
9772
9773
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07009774void wpas_p2p_deinit_iface(struct wpa_supplicant *wpa_s)
9775{
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07009776 if (wpa_s == wpa_s->global->p2p_init_wpa_s && wpa_s->global->p2p) {
9777 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Disable P2P since removing "
9778 "the management interface is being removed");
9779 wpas_p2p_deinit_global(wpa_s->global);
9780 }
9781}
9782
9783
9784void wpas_p2p_ap_deinit(struct wpa_supplicant *wpa_s)
9785{
9786 if (wpa_s->ap_iface->bss)
9787 wpa_s->ap_iface->bss[0]->p2p_group = NULL;
9788 wpas_p2p_group_deinit(wpa_s);
9789}
Dmitry Shmidt58d12ad2016-07-28 10:07:03 -07009790
9791
9792int wpas_p2p_lo_start(struct wpa_supplicant *wpa_s, unsigned int freq,
9793 unsigned int period, unsigned int interval,
9794 unsigned int count)
9795{
9796 struct p2p_data *p2p = wpa_s->global->p2p;
9797 u8 *device_types;
9798 size_t dev_types_len;
9799 struct wpabuf *buf;
9800 int ret;
9801
9802 if (wpa_s->p2p_lo_started) {
9803 wpa_dbg(wpa_s, MSG_DEBUG,
9804 "P2P Listen offload is already started");
9805 return 0;
9806 }
9807
9808 if (wpa_s->global->p2p == NULL ||
9809 !(wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_LISTEN_OFFLOAD)) {
9810 wpa_printf(MSG_DEBUG, "P2P: Listen offload not supported");
9811 return -1;
9812 }
9813
9814 if (!p2p_supported_freq(wpa_s->global->p2p, freq)) {
9815 wpa_printf(MSG_ERROR, "P2P: Input channel not supported: %u",
9816 freq);
9817 return -1;
9818 }
9819
9820 /* Get device type */
9821 dev_types_len = (wpa_s->conf->num_sec_device_types + 1) *
9822 WPS_DEV_TYPE_LEN;
9823 device_types = os_malloc(dev_types_len);
9824 if (!device_types)
9825 return -1;
9826 os_memcpy(device_types, wpa_s->conf->device_type, WPS_DEV_TYPE_LEN);
9827 os_memcpy(&device_types[WPS_DEV_TYPE_LEN], wpa_s->conf->sec_device_type,
9828 wpa_s->conf->num_sec_device_types * WPS_DEV_TYPE_LEN);
9829
9830 /* Get Probe Response IE(s) */
9831 buf = p2p_build_probe_resp_template(p2p, freq);
9832 if (!buf) {
9833 os_free(device_types);
9834 return -1;
9835 }
9836
9837 ret = wpa_drv_p2p_lo_start(wpa_s, freq, period, interval, count,
9838 device_types, dev_types_len,
9839 wpabuf_mhead_u8(buf), wpabuf_len(buf));
9840 if (ret < 0)
9841 wpa_dbg(wpa_s, MSG_DEBUG,
9842 "P2P: Failed to start P2P listen offload");
9843
9844 os_free(device_types);
9845 wpabuf_free(buf);
9846
9847 if (ret == 0) {
9848 wpa_s->p2p_lo_started = 1;
9849
9850 /* Stop current P2P listen if any */
9851 wpas_stop_listen(wpa_s);
9852 }
9853
9854 return ret;
9855}
9856
9857
9858int wpas_p2p_lo_stop(struct wpa_supplicant *wpa_s)
9859{
9860 int ret;
9861
9862 if (!wpa_s->p2p_lo_started)
9863 return 0;
9864
9865 ret = wpa_drv_p2p_lo_stop(wpa_s);
9866 if (ret < 0)
9867 wpa_dbg(wpa_s, MSG_DEBUG,
9868 "P2P: Failed to stop P2P listen offload");
9869
9870 wpa_s->p2p_lo_started = 0;
9871 return ret;
9872}