blob: ccbb1b7df4158688eefa851f5c8eeaaabc2a663b [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
Vinayak Yadawad8db34572021-08-30 21:28:05 +0530107/* Check if frequency is 2GHz */
108#define IS_2GHZ(n) (n >= 2412 && n <= 2484)
109
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700110enum p2p_group_removal_reason {
111 P2P_GROUP_REMOVAL_UNKNOWN,
112 P2P_GROUP_REMOVAL_SILENT,
113 P2P_GROUP_REMOVAL_FORMATION_FAILED,
114 P2P_GROUP_REMOVAL_REQUESTED,
115 P2P_GROUP_REMOVAL_IDLE_TIMEOUT,
116 P2P_GROUP_REMOVAL_UNAVAILABLE,
117 P2P_GROUP_REMOVAL_GO_ENDING_SESSION,
Dmitry Shmidt04f534e2013-12-09 15:50:16 -0800118 P2P_GROUP_REMOVAL_PSK_FAILURE,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800119 P2P_GROUP_REMOVAL_FREQ_CONFLICT,
120 P2P_GROUP_REMOVAL_GO_LEAVE_CHANNEL
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700121};
122
Jouni Malinendc7b7132012-09-14 12:53:47 -0700123
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700124static void wpas_p2p_long_listen_timeout(void *eloop_ctx, void *timeout_ctx);
125static struct wpa_supplicant *
126wpas_p2p_get_group_iface(struct wpa_supplicant *wpa_s, int addr_allocated,
127 int go);
Dmitry Shmidtcf32e602014-01-28 10:57:39 -0800128static int wpas_p2p_join_start(struct wpa_supplicant *wpa_s, int freq,
129 const u8 *ssid, size_t ssid_len);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800130static int wpas_p2p_setup_freqs(struct wpa_supplicant *wpa_s, int freq,
131 int *force_freq, int *pref_freq, int go,
Sunil8cd6f4d2022-06-28 18:40:46 +0000132 struct weighted_pcl *pref_freq_list,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800133 unsigned int *num_pref_freq);
Dmitry Shmidt344abd32014-01-14 13:17:00 -0800134static void wpas_p2p_join_scan_req(struct wpa_supplicant *wpa_s, int freq,
135 const u8 *ssid, size_t ssid_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700136static void wpas_p2p_join_scan(void *eloop_ctx, void *timeout_ctx);
137static int wpas_p2p_join(struct wpa_supplicant *wpa_s, const u8 *iface_addr,
Dmitry Shmidt04949592012-07-19 12:16:46 -0700138 const u8 *dev_addr, enum p2p_wps_method wps_method,
Dmitry Shmidtcf32e602014-01-28 10:57:39 -0800139 int auto_join, int freq,
140 const u8 *ssid, size_t ssid_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700141static int wpas_p2p_create_iface(struct wpa_supplicant *wpa_s);
142static void wpas_p2p_cross_connect_setup(struct wpa_supplicant *wpa_s);
143static void wpas_p2p_group_idle_timeout(void *eloop_ctx, void *timeout_ctx);
144static void wpas_p2p_set_group_idle_timeout(struct wpa_supplicant *wpa_s);
Dmitry Shmidtf8623282013-02-20 14:34:59 -0800145static void wpas_p2p_group_formation_timeout(void *eloop_ctx,
146 void *timeout_ctx);
Dmitry Shmidt04f534e2013-12-09 15:50:16 -0800147static void wpas_p2p_group_freq_conflict(void *eloop_ctx, void *timeout_ctx);
Dmitry Shmidt7f656022015-02-25 14:36:37 -0800148static int wpas_p2p_fallback_to_go_neg(struct wpa_supplicant *wpa_s,
149 int group_added);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800150static void wpas_p2p_stop_find_oper(struct wpa_supplicant *wpa_s);
Dmitry Shmidtbd14a572014-02-18 10:33:49 -0800151static void wpas_stop_listen(void *ctx);
Dmitry Shmidt684785c2014-05-12 13:34:29 -0700152static void wpas_p2p_psk_failure_removal(void *eloop_ctx, void *timeout_ctx);
Dmitry Shmidt43cb5782014-06-16 16:23:22 -0700153static void wpas_p2p_group_deinit(struct wpa_supplicant *wpa_s);
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800154static int wpas_p2p_add_group_interface(struct wpa_supplicant *wpa_s,
155 enum wpa_driver_if_type type);
Dmitry Shmidta3dc3092015-06-23 11:21:28 -0700156static void wpas_p2p_group_formation_failed(struct wpa_supplicant *wpa_s,
157 int already_deleted);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800158static void wpas_p2p_optimize_listen_channel(struct wpa_supplicant *wpa_s,
159 struct wpa_used_freq_data *freqs,
160 unsigned int num);
161static void wpas_p2p_move_go(void *eloop_ctx, void *timeout_ctx);
162static int wpas_p2p_go_is_peer_freq(struct wpa_supplicant *wpa_s, int freq);
163static void
164wpas_p2p_consider_moving_gos(struct wpa_supplicant *wpa_s,
165 struct wpa_used_freq_data *freqs, unsigned int num,
166 enum wpas_p2p_channel_update_trig trig);
167static void wpas_p2p_reconsider_moving_go(void *eloop_ctx, void *timeout_ctx);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700168
169
Hai Shalomc1a21442022-02-04 13:43:00 -0800170static int wpas_get_6ghz_he_chwidth_capab(struct hostapd_hw_modes *mode)
171{
172 int he_capab = 0;
173
174 if (mode)
175 he_capab = mode->he_capab[WPAS_MODE_INFRA].phy_cap[
176 HE_PHYCAP_CHANNEL_WIDTH_SET_IDX];
177 return he_capab;
178}
179
180
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -0700181/*
182 * Get the number of concurrent channels that the HW can operate, but that are
183 * currently not in use by any of the wpa_supplicant interfaces.
184 */
185static int wpas_p2p_num_unused_channels(struct wpa_supplicant *wpa_s)
186{
187 int *freqs;
Dmitry Shmidtb96dad42013-11-05 10:07:29 -0800188 int num, unused;
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -0700189
190 freqs = os_calloc(wpa_s->num_multichan_concurrent, sizeof(int));
191 if (!freqs)
192 return -1;
193
194 num = get_shared_radio_freqs(wpa_s, freqs,
195 wpa_s->num_multichan_concurrent);
196 os_free(freqs);
197
Dmitry Shmidtb96dad42013-11-05 10:07:29 -0800198 unused = wpa_s->num_multichan_concurrent - num;
199 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: num_unused_channels: %d", unused);
200 return unused;
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -0700201}
202
203
204/*
205 * Get the frequencies that are currently in use by one or more of the virtual
206 * interfaces, and that are also valid for P2P operation.
207 */
Dmitry Shmidt43cb5782014-06-16 16:23:22 -0700208static unsigned int
209wpas_p2p_valid_oper_freqs(struct wpa_supplicant *wpa_s,
210 struct wpa_used_freq_data *p2p_freqs,
211 unsigned int len)
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -0700212{
Dmitry Shmidt43cb5782014-06-16 16:23:22 -0700213 struct wpa_used_freq_data *freqs;
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -0700214 unsigned int num, i, j;
215
Dmitry Shmidt43cb5782014-06-16 16:23:22 -0700216 freqs = os_calloc(wpa_s->num_multichan_concurrent,
217 sizeof(struct wpa_used_freq_data));
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -0700218 if (!freqs)
Dmitry Shmidt43cb5782014-06-16 16:23:22 -0700219 return 0;
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -0700220
Dmitry Shmidt43cb5782014-06-16 16:23:22 -0700221 num = get_shared_radio_freqs_data(wpa_s, freqs,
222 wpa_s->num_multichan_concurrent);
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -0700223
Dmitry Shmidt43cb5782014-06-16 16:23:22 -0700224 os_memset(p2p_freqs, 0, sizeof(struct wpa_used_freq_data) * len);
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -0700225
226 for (i = 0, j = 0; i < num && j < len; i++) {
Dmitry Shmidt43cb5782014-06-16 16:23:22 -0700227 if (p2p_supported_freq(wpa_s->global->p2p, freqs[i].freq))
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -0700228 p2p_freqs[j++] = freqs[i];
229 }
230
231 os_free(freqs);
232
Dmitry Shmidt43cb5782014-06-16 16:23:22 -0700233 dump_freq_data(wpa_s, "valid for P2P", p2p_freqs, j);
Dmitry Shmidtb96dad42013-11-05 10:07:29 -0800234
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -0700235 return j;
236}
237
238
Dmitry Shmidt700a1372013-03-15 14:14:44 -0700239static void wpas_p2p_set_own_freq_preference(struct wpa_supplicant *wpa_s,
240 int freq)
241{
242 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
243 return;
Dmitry Shmidt8bd70b72015-05-26 16:02:19 -0700244
245 /* Use the wpa_s used to control the P2P Device operation */
246 wpa_s = wpa_s->global->p2p_init_wpa_s;
247
248 if (wpa_s->conf->p2p_ignore_shared_freq &&
Dmitry Shmidta0d265f2013-11-19 13:13:41 -0800249 freq > 0 && wpa_s->num_multichan_concurrent > 1 &&
250 wpas_p2p_num_unused_channels(wpa_s) > 0) {
251 wpa_printf(MSG_DEBUG, "P2P: Ignore own channel preference %d MHz due to p2p_ignore_shared_freq=1 configuration",
252 freq);
Dmitry Shmidt700a1372013-03-15 14:14:44 -0700253 freq = 0;
Dmitry Shmidta0d265f2013-11-19 13:13:41 -0800254 }
Dmitry Shmidt700a1372013-03-15 14:14:44 -0700255 p2p_set_own_freq_preference(wpa_s->global->p2p, freq);
256}
257
258
Hai Shalom60840252021-02-19 19:02:11 -0800259static void wpas_p2p_scan_res_handled(struct wpa_supplicant *wpa_s)
260{
261 unsigned int delay = wpas_p2p_search_delay(wpa_s);
262
263 /* In case of concurrent P2P and external scans, delay P2P search. */
264 if (external_scan_running(wpa_s->radio)) {
265 delay = wpa_s->conf->p2p_search_delay;
266 wpa_printf(MSG_DEBUG,
267 "P2P: Delay next P2P search by %d ms to let externally triggered scan complete",
268 delay);
269 }
270
271 p2p_scan_res_handled(wpa_s->global->p2p, delay);
272}
273
274
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700275static void wpas_p2p_scan_res_handler(struct wpa_supplicant *wpa_s,
276 struct wpa_scan_results *scan_res)
277{
278 size_t i;
279
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800280 if (wpa_s->p2p_scan_work) {
281 struct wpa_radio_work *work = wpa_s->p2p_scan_work;
282 wpa_s->p2p_scan_work = NULL;
283 radio_work_done(work);
284 }
285
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700286 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
287 return;
288
289 wpa_printf(MSG_DEBUG, "P2P: Scan results received (%d BSS)",
290 (int) scan_res->num);
291
292 for (i = 0; i < scan_res->num; i++) {
293 struct wpa_scan_res *bss = scan_res->res[i];
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800294 struct os_reltime time_tmp_age, entry_ts;
Dmitry Shmidt96571392013-10-14 12:54:46 -0700295 const u8 *ies;
296 size_t ies_len;
297
Dmitry Shmidtf8623282013-02-20 14:34:59 -0800298 time_tmp_age.sec = bss->age / 1000;
299 time_tmp_age.usec = (bss->age % 1000) * 1000;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800300 os_reltime_sub(&scan_res->fetch_time, &time_tmp_age, &entry_ts);
Dmitry Shmidt96571392013-10-14 12:54:46 -0700301
302 ies = (const u8 *) (bss + 1);
303 ies_len = bss->ie_len;
304 if (bss->beacon_ie_len > 0 &&
305 !wpa_scan_get_vendor_ie(bss, P2P_IE_VENDOR_TYPE) &&
306 wpa_scan_get_vendor_ie_beacon(bss, P2P_IE_VENDOR_TYPE)) {
307 wpa_printf(MSG_DEBUG, "P2P: Use P2P IE(s) from Beacon frame since no P2P IE(s) in Probe Response frames received for "
308 MACSTR, MAC2STR(bss->bssid));
309 ies = ies + ies_len;
310 ies_len = bss->beacon_ie_len;
311 }
312
313
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700314 if (p2p_scan_res_handler(wpa_s->global->p2p, bss->bssid,
Dmitry Shmidtf8623282013-02-20 14:34:59 -0800315 bss->freq, &entry_ts, bss->level,
Dmitry Shmidt96571392013-10-14 12:54:46 -0700316 ies, ies_len) > 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700317 break;
318 }
319
Hai Shalom60840252021-02-19 19:02:11 -0800320 wpas_p2p_scan_res_handled(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700321}
322
323
Hai Shalom60840252021-02-19 19:02:11 -0800324static void wpas_p2p_scan_res_fail_handler(struct wpa_supplicant *wpa_s)
Hai Shalom899fcc72020-10-19 14:38:18 -0700325{
Hai Shalom60840252021-02-19 19:02:11 -0800326 if (wpa_s->p2p_scan_work) {
327 struct wpa_radio_work *work = wpa_s->p2p_scan_work;
Hai Shalom899fcc72020-10-19 14:38:18 -0700328
Hai Shalom60840252021-02-19 19:02:11 -0800329 wpa_s->p2p_scan_work = NULL;
330 radio_work_done(work);
Hai Shalom899fcc72020-10-19 14:38:18 -0700331 }
332
Hai Shalom60840252021-02-19 19:02:11 -0800333 if (wpa_s->global->p2p_disabled || !wpa_s->global->p2p)
334 return;
Hai Shalom899fcc72020-10-19 14:38:18 -0700335
Hai Shalom60840252021-02-19 19:02:11 -0800336 wpa_dbg(wpa_s, MSG_DEBUG,
337 "P2P: Failed to get scan results - try to continue");
338 wpas_p2p_scan_res_handled(wpa_s);
Hai Shalom899fcc72020-10-19 14:38:18 -0700339}
340
341
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800342static void wpas_p2p_trigger_scan_cb(struct wpa_radio_work *work, int deinit)
343{
344 struct wpa_supplicant *wpa_s = work->wpa_s;
345 struct wpa_driver_scan_params *params = work->ctx;
346 int ret;
347
348 if (deinit) {
Dmitry Shmidtbd14a572014-02-18 10:33:49 -0800349 if (!work->started) {
350 wpa_scan_free_params(params);
351 return;
352 }
353
354 wpa_s->p2p_scan_work = NULL;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800355 return;
356 }
357
Dmitry Shmidtebd93af2017-02-21 13:40:44 -0800358 if (wpa_s->clear_driver_scan_cache) {
359 wpa_printf(MSG_DEBUG,
360 "Request driver to clear scan cache due to local BSS flush");
361 params->only_new_results = 1;
362 }
Hai Shalom899fcc72020-10-19 14:38:18 -0700363
Hai Shalomc1a21442022-02-04 13:43:00 -0800364 if (!params->p2p_include_6ghz && !params->freqs) {
Hai Shalom899fcc72020-10-19 14:38:18 -0700365 wpa_printf(MSG_DEBUG,
Hai Shalomc1a21442022-02-04 13:43:00 -0800366 "P2P: Exclude 6 GHz channels - update the scan frequency list");
Hai Shalom60840252021-02-19 19:02:11 -0800367 wpa_add_scan_freqs_list(wpa_s, HOSTAPD_MODE_IEEE80211G, params,
368 0);
369 wpa_add_scan_freqs_list(wpa_s, HOSTAPD_MODE_IEEE80211A, params,
370 0);
Hai Shalom899fcc72020-10-19 14:38:18 -0700371 }
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800372 ret = wpa_drv_scan(wpa_s, params);
Dmitry Shmidtabb90a32016-12-05 15:34:39 -0800373 if (ret == 0)
374 wpa_s->curr_scan_cookie = params->scan_cookie;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800375 wpa_scan_free_params(params);
376 work->ctx = NULL;
377 if (ret) {
378 radio_work_done(work);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800379 p2p_notify_scan_trigger_status(wpa_s->global->p2p, ret);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800380 return;
381 }
382
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800383 p2p_notify_scan_trigger_status(wpa_s->global->p2p, ret);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800384 os_get_reltime(&wpa_s->scan_trigger_time);
385 wpa_s->scan_res_handler = wpas_p2p_scan_res_handler;
Hai Shalom60840252021-02-19 19:02:11 -0800386 wpa_s->scan_res_fail_handler = wpas_p2p_scan_res_fail_handler;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800387 wpa_s->own_scan_requested = 1;
Dmitry Shmidtebd93af2017-02-21 13:40:44 -0800388 wpa_s->clear_driver_scan_cache = 0;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800389 wpa_s->p2p_scan_work = work;
390}
391
392
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800393static int wpas_p2p_search_social_channel(struct wpa_supplicant *wpa_s,
394 int freq)
395{
396 if (wpa_s->global->p2p_24ghz_social_channels &&
397 (freq == 2412 || freq == 2437 || freq == 2462)) {
398 /*
399 * Search all social channels regardless of whether these have
400 * been disabled for P2P operating channel use to avoid missing
401 * peers.
402 */
403 return 1;
404 }
405 return p2p_supported_freq(wpa_s->global->p2p, freq);
406}
407
408
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700409static int wpas_p2p_scan(void *ctx, enum p2p_scan_type type, int freq,
410 unsigned int num_req_dev_types,
Hai Shalomc1a21442022-02-04 13:43:00 -0800411 const u8 *req_dev_types, const u8 *dev_id, u16 pw_id,
412 bool include_6ghz)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700413{
414 struct wpa_supplicant *wpa_s = ctx;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800415 struct wpa_driver_scan_params *params = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700416 struct wpabuf *wps_ie, *ies;
Dmitry Shmidt661b4f72014-09-29 14:58:27 -0700417 unsigned int num_channels = 0;
418 int social_channels_freq[] = { 2412, 2437, 2462, 60480 };
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800419 size_t ielen;
Dmitry Shmidt661b4f72014-09-29 14:58:27 -0700420 u8 *n, i;
Dmitry Shmidt9c175262016-03-03 10:20:07 -0800421 unsigned int bands;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700422
423 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
424 return -1;
425
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800426 if (wpa_s->p2p_scan_work) {
427 wpa_dbg(wpa_s, MSG_INFO, "P2P: Reject scan trigger since one is already pending");
428 return -1;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700429 }
430
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800431 params = os_zalloc(sizeof(*params));
432 if (params == NULL)
433 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700434
435 /* P2P Wildcard SSID */
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800436 params->num_ssids = 1;
437 n = os_malloc(P2P_WILDCARD_SSID_LEN);
438 if (n == NULL)
439 goto fail;
440 os_memcpy(n, P2P_WILDCARD_SSID, P2P_WILDCARD_SSID_LEN);
441 params->ssids[0].ssid = n;
442 params->ssids[0].ssid_len = P2P_WILDCARD_SSID_LEN;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700443
444 wpa_s->wps->dev.p2p = 1;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700445 wps_ie = wps_build_probe_req_ie(pw_id, &wpa_s->wps->dev,
446 wpa_s->wps->uuid, WPS_REQ_ENROLLEE,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700447 num_req_dev_types, req_dev_types);
448 if (wps_ie == NULL)
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800449 goto fail;
Hai Shalomc1a21442022-02-04 13:43:00 -0800450 if (!wpa_s->conf->p2p_6ghz_disable)
451 params->p2p_include_6ghz = include_6ghz;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700452 switch (type) {
453 case P2P_SCAN_SOCIAL:
Dmitry Shmidt661b4f72014-09-29 14:58:27 -0700454 params->freqs = os_calloc(ARRAY_SIZE(social_channels_freq) + 1,
455 sizeof(int));
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800456 if (params->freqs == NULL)
457 goto fail;
Dmitry Shmidt661b4f72014-09-29 14:58:27 -0700458 for (i = 0; i < ARRAY_SIZE(social_channels_freq); i++) {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800459 if (wpas_p2p_search_social_channel(
460 wpa_s, social_channels_freq[i]))
Dmitry Shmidt661b4f72014-09-29 14:58:27 -0700461 params->freqs[num_channels++] =
462 social_channels_freq[i];
463 }
464 params->freqs[num_channels++] = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700465 break;
466 case P2P_SCAN_FULL:
467 break;
Dmitry Shmidt203eadb2015-03-05 14:16:04 -0800468 case P2P_SCAN_SPECIFIC:
469 params->freqs = os_calloc(2, sizeof(int));
470 if (params->freqs == NULL)
471 goto fail;
472 params->freqs[0] = freq;
473 params->freqs[1] = 0;
474 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700475 case P2P_SCAN_SOCIAL_PLUS_ONE:
Dmitry Shmidt661b4f72014-09-29 14:58:27 -0700476 params->freqs = os_calloc(ARRAY_SIZE(social_channels_freq) + 2,
477 sizeof(int));
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800478 if (params->freqs == NULL)
479 goto fail;
Dmitry Shmidt661b4f72014-09-29 14:58:27 -0700480 for (i = 0; i < ARRAY_SIZE(social_channels_freq); i++) {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800481 if (wpas_p2p_search_social_channel(
482 wpa_s, social_channels_freq[i]))
Dmitry Shmidt661b4f72014-09-29 14:58:27 -0700483 params->freqs[num_channels++] =
484 social_channels_freq[i];
485 }
486 if (p2p_supported_freq(wpa_s->global->p2p, freq))
487 params->freqs[num_channels++] = freq;
488 params->freqs[num_channels++] = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700489 break;
490 }
491
Dmitry Shmidt9c175262016-03-03 10:20:07 -0800492 ielen = p2p_scan_ie_buf_len(wpa_s->global->p2p);
493 ies = wpabuf_alloc(wpabuf_len(wps_ie) + ielen);
494 if (ies == NULL) {
495 wpabuf_free(wps_ie);
496 goto fail;
497 }
498 wpabuf_put_buf(ies, wps_ie);
499 wpabuf_free(wps_ie);
500
501 bands = wpas_get_bands(wpa_s, params->freqs);
502 p2p_scan_ie(wpa_s->global->p2p, ies, dev_id, bands);
503
504 params->p2p_probe = 1;
505 n = os_malloc(wpabuf_len(ies));
506 if (n == NULL) {
507 wpabuf_free(ies);
508 goto fail;
509 }
510 os_memcpy(n, wpabuf_head(ies), wpabuf_len(ies));
511 params->extra_ies = n;
512 params->extra_ies_len = wpabuf_len(ies);
513 wpabuf_free(ies);
514
Dmitry Shmidtbd14a572014-02-18 10:33:49 -0800515 radio_remove_works(wpa_s, "p2p-scan", 0);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800516 if (radio_add_work(wpa_s, 0, "p2p-scan", 0, wpas_p2p_trigger_scan_cb,
517 params) < 0)
518 goto fail;
519 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700520
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800521fail:
522 wpa_scan_free_params(params);
523 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700524}
525
526
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700527static enum wpa_driver_if_type wpas_p2p_if_type(int p2p_group_interface)
528{
529 switch (p2p_group_interface) {
530 case P2P_GROUP_INTERFACE_PENDING:
531 return WPA_IF_P2P_GROUP;
532 case P2P_GROUP_INTERFACE_GO:
533 return WPA_IF_P2P_GO;
534 case P2P_GROUP_INTERFACE_CLIENT:
535 return WPA_IF_P2P_CLIENT;
536 }
537
538 return WPA_IF_P2P_GROUP;
539}
540
541
542static struct wpa_supplicant * wpas_get_p2p_group(struct wpa_supplicant *wpa_s,
543 const u8 *ssid,
544 size_t ssid_len, int *go)
545{
546 struct wpa_ssid *s;
547
548 for (wpa_s = wpa_s->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
549 for (s = wpa_s->conf->ssid; s; s = s->next) {
550 if (s->disabled != 0 || !s->p2p_group ||
551 s->ssid_len != ssid_len ||
552 os_memcmp(ssid, s->ssid, ssid_len) != 0)
553 continue;
554 if (s->mode == WPAS_MODE_P2P_GO &&
555 s != wpa_s->current_ssid)
556 continue;
557 if (go)
558 *go = s->mode == WPAS_MODE_P2P_GO;
559 return wpa_s;
560 }
561 }
562
563 return NULL;
564}
565
566
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800567static void run_wpas_p2p_disconnect(void *eloop_ctx, void *timeout_ctx)
568{
569 struct wpa_supplicant *wpa_s = eloop_ctx;
570 wpa_printf(MSG_DEBUG,
571 "P2P: Complete previously requested removal of %s",
572 wpa_s->ifname);
573 wpas_p2p_disconnect(wpa_s);
574}
575
576
577static int wpas_p2p_disconnect_safely(struct wpa_supplicant *wpa_s,
578 struct wpa_supplicant *calling_wpa_s)
579{
580 if (calling_wpa_s == wpa_s && wpa_s &&
581 wpa_s->p2p_group_interface != NOT_P2P_GROUP_INTERFACE) {
582 /*
583 * The calling wpa_s instance is going to be removed. Do that
584 * from an eloop callback to keep the instance available until
Hai Shalom899fcc72020-10-19 14:38:18 -0700585 * the caller has returned. This may be needed, e.g., to provide
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800586 * control interface responses on the per-interface socket.
587 */
588 if (eloop_register_timeout(0, 0, run_wpas_p2p_disconnect,
589 wpa_s, NULL) < 0)
590 return -1;
591 return 0;
592 }
593
594 return wpas_p2p_disconnect(wpa_s);
595}
596
597
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800598/* Determine total number of clients in active groups where we are the GO */
599static unsigned int p2p_group_go_member_count(struct wpa_supplicant *wpa_s)
600{
601 unsigned int count = 0;
602 struct wpa_ssid *s;
603
604 for (wpa_s = wpa_s->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
605 for (s = wpa_s->conf->ssid; s; s = s->next) {
606 wpa_printf(MSG_DEBUG,
607 "P2P: sup:%p ssid:%p disabled:%d p2p:%d mode:%d",
608 wpa_s, s, s->disabled, s->p2p_group,
609 s->mode);
610 if (!s->disabled && s->p2p_group &&
611 s->mode == WPAS_MODE_P2P_GO) {
612 count += p2p_get_group_num_members(
613 wpa_s->p2p_group);
614 }
615 }
616 }
617
618 return count;
619}
620
621
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800622static unsigned int p2p_is_active_persistent_group(struct wpa_supplicant *wpa_s)
623{
624 return !wpa_s->p2p_mgmt && wpa_s->current_ssid &&
625 !wpa_s->current_ssid->disabled &&
626 wpa_s->current_ssid->p2p_group &&
627 wpa_s->current_ssid->p2p_persistent_group;
628}
629
630
631static unsigned int p2p_is_active_persistent_go(struct wpa_supplicant *wpa_s)
632{
633 return p2p_is_active_persistent_group(wpa_s) &&
634 wpa_s->current_ssid->mode == WPAS_MODE_P2P_GO;
635}
636
637
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800638/* Find an interface for a P2P group where we are the GO */
639static struct wpa_supplicant *
640wpas_p2p_get_go_group(struct wpa_supplicant *wpa_s)
641{
642 struct wpa_supplicant *save = NULL;
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800643
644 if (!wpa_s)
645 return NULL;
646
647 for (wpa_s = wpa_s->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800648 if (!p2p_is_active_persistent_go(wpa_s))
649 continue;
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800650
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800651 /* Prefer a group with connected clients */
652 if (p2p_get_group_num_members(wpa_s->p2p_group))
653 return wpa_s;
654 save = wpa_s;
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800655 }
656
657 /* No group with connected clients, so pick the one without (if any) */
658 return save;
659}
660
661
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800662static unsigned int p2p_is_active_persistent_cli(struct wpa_supplicant *wpa_s)
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800663{
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800664 return p2p_is_active_persistent_group(wpa_s) &&
665 wpa_s->current_ssid->mode == WPAS_MODE_INFRA;
666}
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800667
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800668
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800669/* Find an interface for a P2P group where we are the P2P Client */
670static struct wpa_supplicant *
671wpas_p2p_get_cli_group(struct wpa_supplicant *wpa_s)
672{
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800673 for (wpa_s = wpa_s->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800674 if (p2p_is_active_persistent_cli(wpa_s))
675 return wpa_s;
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800676 }
677
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800678 return NULL;
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800679}
680
681
682/* Find a persistent group where we are the GO */
683static struct wpa_ssid *
684wpas_p2p_get_persistent_go(struct wpa_supplicant *wpa_s)
685{
686 struct wpa_ssid *s;
687
688 for (s = wpa_s->conf->ssid; s; s = s->next) {
689 if (s->disabled == 2 && s->mode == WPAS_MODE_P2P_GO)
690 return s;
691 }
692
693 return NULL;
694}
695
696
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800697static u8 p2ps_group_capability(void *ctx, u8 incoming, u8 role,
698 unsigned int *force_freq,
699 unsigned int *pref_freq)
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800700{
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800701 struct wpa_supplicant *wpa_s = ctx;
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800702 struct wpa_ssid *s;
703 u8 conncap = P2PS_SETUP_NONE;
704 unsigned int owned_members = 0;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800705 struct wpa_supplicant *go_wpa_s, *cli_wpa_s;
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800706 struct wpa_ssid *persistent_go;
707 int p2p_no_group_iface;
Sunil8cd6f4d2022-06-28 18:40:46 +0000708 struct weighted_pcl pref_freq_list[P2P_MAX_PREF_CHANNELS];
709 unsigned int size;
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800710
711 wpa_printf(MSG_DEBUG, "P2P: Conncap - in:%d role:%d", incoming, role);
712
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800713 if (force_freq)
714 *force_freq = 0;
715 if (pref_freq)
716 *pref_freq = 0;
717
718 size = P2P_MAX_PREF_CHANNELS;
719 if (force_freq && pref_freq &&
720 !wpas_p2p_setup_freqs(wpa_s, 0, (int *) force_freq,
721 (int *) pref_freq, 0, pref_freq_list, &size))
722 wpas_p2p_set_own_freq_preference(wpa_s,
723 *force_freq ? *force_freq :
724 *pref_freq);
725
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800726 /*
727 * For non-concurrent capable devices:
728 * If persistent_go, then no new.
729 * If GO, then no client.
730 * If client, then no GO.
731 */
732 go_wpa_s = wpas_p2p_get_go_group(wpa_s);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800733 if (go_wpa_s)
734 owned_members = p2p_get_group_num_members(go_wpa_s->p2p_group);
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800735 persistent_go = wpas_p2p_get_persistent_go(wpa_s);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800736 p2p_no_group_iface = !wpas_p2p_create_iface(wpa_s);
737 cli_wpa_s = wpas_p2p_get_cli_group(wpa_s);
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800738
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800739 wpa_printf(MSG_DEBUG,
740 "P2P: GO(iface)=%p members=%u CLI(iface)=%p persistent(ssid)=%p",
741 go_wpa_s, owned_members, cli_wpa_s, persistent_go);
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800742
743 /* If not concurrent, restrict our choices */
744 if (p2p_no_group_iface) {
745 wpa_printf(MSG_DEBUG, "P2P: p2p_no_group_iface");
746
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800747 if (cli_wpa_s)
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800748 return P2PS_SETUP_NONE;
749
750 if (go_wpa_s) {
751 if (role == P2PS_SETUP_CLIENT ||
752 incoming == P2PS_SETUP_GROUP_OWNER ||
753 p2p_client_limit_reached(go_wpa_s->p2p_group))
754 return P2PS_SETUP_NONE;
755
756 return P2PS_SETUP_GROUP_OWNER;
757 }
758
759 if (persistent_go) {
760 if (role == P2PS_SETUP_NONE || role == P2PS_SETUP_NEW) {
761 if (!incoming)
762 return P2PS_SETUP_GROUP_OWNER |
763 P2PS_SETUP_CLIENT;
764 if (incoming == P2PS_SETUP_NEW) {
765 u8 r;
766
767 if (os_get_random(&r, sizeof(r)) < 0 ||
768 (r & 1))
769 return P2PS_SETUP_CLIENT;
770 return P2PS_SETUP_GROUP_OWNER;
771 }
772 }
773 }
774 }
775
776 /* If a required role has been specified, handle it here */
777 if (role && role != P2PS_SETUP_NEW) {
778 switch (incoming) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800779 case P2PS_SETUP_GROUP_OWNER | P2PS_SETUP_NEW:
780 case P2PS_SETUP_GROUP_OWNER | P2PS_SETUP_CLIENT:
781 /*
782 * Peer has an active GO, so if the role allows it and
783 * we do not have any active roles, become client.
784 */
785 if ((role & P2PS_SETUP_CLIENT) && !go_wpa_s &&
786 !cli_wpa_s)
787 return P2PS_SETUP_CLIENT;
788
789 /* fall through */
790
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800791 case P2PS_SETUP_NONE:
792 case P2PS_SETUP_NEW:
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800793 conncap = role;
794 goto grp_owner;
795
796 case P2PS_SETUP_GROUP_OWNER:
797 /*
798 * Must be a complimentary role - cannot be a client to
799 * more than one peer.
800 */
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800801 if (incoming == role || cli_wpa_s)
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800802 return P2PS_SETUP_NONE;
803
804 return P2PS_SETUP_CLIENT;
805
806 case P2PS_SETUP_CLIENT:
807 /* Must be a complimentary role */
808 if (incoming != role) {
809 conncap = P2PS_SETUP_GROUP_OWNER;
810 goto grp_owner;
811 }
Roshan Pius3a1667e2018-07-03 15:17:14 -0700812 /* fall through */
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800813
814 default:
815 return P2PS_SETUP_NONE;
816 }
817 }
818
819 /*
820 * For now, we only will support ownership of one group, and being a
821 * client of one group. Therefore, if we have either an existing GO
822 * group, or an existing client group, we will not do a new GO
823 * negotiation, but rather try to re-use the existing groups.
824 */
825 switch (incoming) {
826 case P2PS_SETUP_NONE:
827 case P2PS_SETUP_NEW:
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800828 if (cli_wpa_s)
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800829 conncap = P2PS_SETUP_GROUP_OWNER;
830 else if (!owned_members)
831 conncap = P2PS_SETUP_NEW;
832 else if (incoming == P2PS_SETUP_NONE)
833 conncap = P2PS_SETUP_GROUP_OWNER | P2PS_SETUP_CLIENT;
834 else
835 conncap = P2PS_SETUP_CLIENT;
836 break;
837
838 case P2PS_SETUP_CLIENT:
839 conncap = P2PS_SETUP_GROUP_OWNER;
840 break;
841
842 case P2PS_SETUP_GROUP_OWNER:
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800843 if (!cli_wpa_s)
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800844 conncap = P2PS_SETUP_CLIENT;
845 break;
846
847 case P2PS_SETUP_GROUP_OWNER | P2PS_SETUP_NEW:
848 case P2PS_SETUP_GROUP_OWNER | P2PS_SETUP_CLIENT:
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800849 if (cli_wpa_s)
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800850 conncap = P2PS_SETUP_GROUP_OWNER;
851 else {
852 u8 r;
853
854 if (os_get_random(&r, sizeof(r)) < 0 ||
855 (r & 1))
856 conncap = P2PS_SETUP_CLIENT;
857 else
858 conncap = P2PS_SETUP_GROUP_OWNER;
859 }
860 break;
861
862 default:
863 return P2PS_SETUP_NONE;
864 }
865
866grp_owner:
867 if ((conncap & P2PS_SETUP_GROUP_OWNER) ||
868 (!incoming && (conncap & P2PS_SETUP_NEW))) {
869 if (go_wpa_s && p2p_client_limit_reached(go_wpa_s->p2p_group))
870 conncap &= ~P2PS_SETUP_GROUP_OWNER;
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800871
872 s = wpas_p2p_get_persistent_go(wpa_s);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800873 if (!s && !go_wpa_s && p2p_no_group_iface) {
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800874 p2p_set_intended_addr(wpa_s->global->p2p,
Dmitry Shmidt9c175262016-03-03 10:20:07 -0800875 wpa_s->p2p_mgmt ?
876 wpa_s->parent->own_addr :
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800877 wpa_s->own_addr);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800878 } else if (!s && !go_wpa_s) {
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800879 if (wpas_p2p_add_group_interface(wpa_s,
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -0800880 WPA_IF_P2P_GROUP) < 0) {
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800881 wpa_printf(MSG_ERROR,
882 "P2P: Failed to allocate a new interface for the group");
883 return P2PS_SETUP_NONE;
884 }
885 wpa_s->global->pending_group_iface_for_p2ps = 1;
886 p2p_set_intended_addr(wpa_s->global->p2p,
887 wpa_s->pending_interface_addr);
888 }
889 }
890
891 return conncap;
892}
893
894
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700895static int wpas_p2p_group_delete(struct wpa_supplicant *wpa_s,
896 enum p2p_group_removal_reason removal_reason)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700897{
898 struct wpa_ssid *ssid;
899 char *gtype;
900 const char *reason;
901
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700902 ssid = wpa_s->current_ssid;
903 if (ssid == NULL) {
904 /*
905 * The current SSID was not known, but there may still be a
Dmitry Shmidtaa532512012-09-24 10:35:31 -0700906 * pending P2P group interface waiting for provisioning or a
907 * P2P group that is trying to reconnect.
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700908 */
909 ssid = wpa_s->conf->ssid;
910 while (ssid) {
Jouni Malinen9d712832012-10-05 11:01:57 -0700911 if (ssid->p2p_group && ssid->disabled != 2)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700912 break;
913 ssid = ssid->next;
914 }
Jouni Malinen5c44edb2012-08-31 21:35:32 +0300915 if (ssid == NULL &&
916 wpa_s->p2p_group_interface == NOT_P2P_GROUP_INTERFACE)
917 {
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700918 wpa_printf(MSG_ERROR, "P2P: P2P group interface "
919 "not found");
920 return -1;
921 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700922 }
923 if (wpa_s->p2p_group_interface == P2P_GROUP_INTERFACE_GO)
924 gtype = "GO";
925 else if (wpa_s->p2p_group_interface == P2P_GROUP_INTERFACE_CLIENT ||
926 (ssid && ssid->mode == WPAS_MODE_INFRA)) {
927 wpa_s->reassociate = 0;
928 wpa_s->disconnected = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700929 gtype = "client";
930 } else
931 gtype = "GO";
Dmitry Shmidt661b4f72014-09-29 14:58:27 -0700932
933 if (removal_reason != P2P_GROUP_REMOVAL_SILENT && ssid)
934 wpas_notify_p2p_group_removed(wpa_s, ssid, gtype);
935
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800936 if (os_strcmp(gtype, "client") == 0) {
Dmitry Shmidt661b4f72014-09-29 14:58:27 -0700937 wpa_supplicant_deauthenticate(wpa_s, WLAN_REASON_DEAUTH_LEAVING);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800938 if (eloop_is_timeout_registered(wpas_p2p_psk_failure_removal,
939 wpa_s, NULL)) {
940 wpa_printf(MSG_DEBUG,
941 "P2P: PSK failure removal was scheduled, so use PSK failure as reason for group removal");
942 removal_reason = P2P_GROUP_REMOVAL_PSK_FAILURE;
943 eloop_cancel_timeout(wpas_p2p_psk_failure_removal,
944 wpa_s, NULL);
945 }
946 }
Dmitry Shmidt661b4f72014-09-29 14:58:27 -0700947
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700948 if (wpa_s->cross_connect_in_use) {
949 wpa_s->cross_connect_in_use = 0;
Dmitry Shmidt9c175262016-03-03 10:20:07 -0800950 wpa_msg_global(wpa_s->p2pdev, MSG_INFO,
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -0700951 P2P_EVENT_CROSS_CONNECT_DISABLE "%s %s",
952 wpa_s->ifname, wpa_s->cross_connect_uplink);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700953 }
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700954 switch (removal_reason) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700955 case P2P_GROUP_REMOVAL_REQUESTED:
956 reason = " reason=REQUESTED";
957 break;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700958 case P2P_GROUP_REMOVAL_FORMATION_FAILED:
959 reason = " reason=FORMATION_FAILED";
960 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700961 case P2P_GROUP_REMOVAL_IDLE_TIMEOUT:
962 reason = " reason=IDLE";
963 break;
964 case P2P_GROUP_REMOVAL_UNAVAILABLE:
965 reason = " reason=UNAVAILABLE";
966 break;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700967 case P2P_GROUP_REMOVAL_GO_ENDING_SESSION:
968 reason = " reason=GO_ENDING_SESSION";
969 break;
Dmitry Shmidt391c59f2013-09-03 12:16:28 -0700970 case P2P_GROUP_REMOVAL_PSK_FAILURE:
971 reason = " reason=PSK_FAILURE";
972 break;
Dmitry Shmidt04f534e2013-12-09 15:50:16 -0800973 case P2P_GROUP_REMOVAL_FREQ_CONFLICT:
974 reason = " reason=FREQ_CONFLICT";
975 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700976 default:
977 reason = "";
978 break;
979 }
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700980 if (removal_reason != P2P_GROUP_REMOVAL_SILENT) {
Dmitry Shmidt9c175262016-03-03 10:20:07 -0800981 wpa_msg_global(wpa_s->p2pdev, MSG_INFO,
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -0700982 P2P_EVENT_GROUP_REMOVED "%s %s%s",
983 wpa_s->ifname, gtype, reason);
Dmitry Shmidt04949592012-07-19 12:16:46 -0700984 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700985
Dmitry Shmidt04f534e2013-12-09 15:50:16 -0800986 if (eloop_cancel_timeout(wpas_p2p_group_freq_conflict, wpa_s, NULL) > 0)
987 wpa_printf(MSG_DEBUG, "P2P: Cancelled P2P group freq_conflict timeout");
Dmitry Shmidt04949592012-07-19 12:16:46 -0700988 if (eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL) > 0)
989 wpa_printf(MSG_DEBUG, "P2P: Cancelled P2P group idle timeout");
Dmitry Shmidtf8623282013-02-20 14:34:59 -0800990 if (eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
Dmitry Shmidt9c175262016-03-03 10:20:07 -0800991 wpa_s->p2pdev, NULL) > 0) {
Dmitry Shmidtf8623282013-02-20 14:34:59 -0800992 wpa_printf(MSG_DEBUG, "P2P: Cancelled P2P group formation "
993 "timeout");
Dmitry Shmidt2f023192013-03-12 12:44:17 -0700994 wpa_s->p2p_in_provisioning = 0;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800995 wpas_p2p_group_formation_failed(wpa_s, 1);
Dmitry Shmidt2f023192013-03-12 12:44:17 -0700996 }
Dmitry Shmidt04949592012-07-19 12:16:46 -0700997
Dmitry Shmidt15907092014-03-25 10:42:57 -0700998 wpa_s->p2p_in_invitation = 0;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800999 eloop_cancel_timeout(wpas_p2p_move_go, wpa_s, NULL);
1000 eloop_cancel_timeout(wpas_p2p_reconsider_moving_go, wpa_s, NULL);
Dmitry Shmidt15907092014-03-25 10:42:57 -07001001
Dmitry Shmidt96571392013-10-14 12:54:46 -07001002 /*
1003 * Make sure wait for the first client does not remain active after the
1004 * group has been removed.
1005 */
1006 wpa_s->global->p2p_go_wait_client.sec = 0;
1007
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001008 if (wpa_s->p2p_group_interface != NOT_P2P_GROUP_INTERFACE) {
1009 struct wpa_global *global;
1010 char *ifname;
1011 enum wpa_driver_if_type type;
1012 wpa_printf(MSG_DEBUG, "P2P: Remove group interface %s",
1013 wpa_s->ifname);
1014 global = wpa_s->global;
1015 ifname = os_strdup(wpa_s->ifname);
1016 type = wpas_p2p_if_type(wpa_s->p2p_group_interface);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001017 eloop_cancel_timeout(run_wpas_p2p_disconnect, wpa_s, NULL);
Dmitry Shmidte15c7b52011-08-03 15:04:35 -07001018 wpa_supplicant_remove_iface(wpa_s->global, wpa_s, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001019 wpa_s = global->ifaces;
1020 if (wpa_s && ifname)
1021 wpa_drv_if_remove(wpa_s, type, ifname);
1022 os_free(ifname);
Jouni Malinen2b89da82012-08-31 22:04:41 +03001023 return 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001024 }
1025
Dmitry Shmidt9c175262016-03-03 10:20:07 -08001026 /*
1027 * The primary interface was used for P2P group operations, so
1028 * need to reset its p2pdev.
1029 */
1030 wpa_s->p2pdev = wpa_s->parent;
1031
Dmitry Shmidt96571392013-10-14 12:54:46 -07001032 if (!wpa_s->p2p_go_group_formation_completed) {
1033 wpa_s->global->p2p_group_formation = NULL;
1034 wpa_s->p2p_in_provisioning = 0;
1035 }
1036
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001037 wpa_s->show_group_started = 0;
1038 os_free(wpa_s->go_params);
1039 wpa_s->go_params = NULL;
1040
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001041 os_free(wpa_s->p2p_group_common_freqs);
1042 wpa_s->p2p_group_common_freqs = NULL;
1043 wpa_s->p2p_group_common_freqs_num = 0;
Hai Shalom021b0b52019-04-10 11:17:58 -07001044 wpa_s->p2p_go_do_acs = 0;
Hai Shalomc1a21442022-02-04 13:43:00 -08001045 wpa_s->p2p_go_allow_dfs = 0;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001046
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001047 wpa_s->waiting_presence_resp = 0;
1048
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001049 wpa_printf(MSG_DEBUG, "P2P: Remove temporary group network");
1050 if (ssid && (ssid->p2p_group ||
1051 ssid->mode == WPAS_MODE_P2P_GROUP_FORMATION ||
1052 (ssid->key_mgmt & WPA_KEY_MGMT_WPS))) {
1053 int id = ssid->id;
Jouni Malinen75ecf522011-06-27 15:19:46 -07001054 if (ssid == wpa_s->current_ssid) {
1055 wpa_sm_set_config(wpa_s->wpa, NULL);
1056 eapol_sm_notify_config(wpa_s->eapol, NULL, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001057 wpa_s->current_ssid = NULL;
Jouni Malinen75ecf522011-06-27 15:19:46 -07001058 }
1059 /*
1060 * Networks objects created during any P2P activities are not
1061 * exposed out as they might/will confuse certain non-P2P aware
1062 * applications since these network objects won't behave like
1063 * regular ones.
1064 *
1065 * Likewise, we don't send out network removed signals for such
1066 * network objects.
1067 */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001068 wpa_config_remove_network(wpa_s->conf, id);
1069 wpa_supplicant_clear_status(wpa_s);
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08001070 wpa_supplicant_cancel_sched_scan(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001071 } else {
1072 wpa_printf(MSG_DEBUG, "P2P: Temporary group network not "
1073 "found");
1074 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07001075 if (wpa_s->ap_iface)
1076 wpa_supplicant_ap_deinit(wpa_s);
1077 else
1078 wpa_drv_deinit_p2p_cli(wpa_s);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001079
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001080 os_memset(wpa_s->go_dev_addr, 0, ETH_ALEN);
1081
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001082 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001083}
1084
1085
1086static int wpas_p2p_persistent_group(struct wpa_supplicant *wpa_s,
1087 u8 *go_dev_addr,
1088 const u8 *ssid, size_t ssid_len)
1089{
1090 struct wpa_bss *bss;
1091 const u8 *bssid;
1092 struct wpabuf *p2p;
1093 u8 group_capab;
1094 const u8 *addr;
1095
1096 if (wpa_s->go_params)
1097 bssid = wpa_s->go_params->peer_interface_addr;
1098 else
1099 bssid = wpa_s->bssid;
1100
1101 bss = wpa_bss_get(wpa_s, bssid, ssid, ssid_len);
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001102 if (bss == NULL && wpa_s->go_params &&
1103 !is_zero_ether_addr(wpa_s->go_params->peer_device_addr))
1104 bss = wpa_bss_get_p2p_dev_addr(
1105 wpa_s, wpa_s->go_params->peer_device_addr);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001106 if (bss == NULL) {
1107 u8 iface_addr[ETH_ALEN];
1108 if (p2p_get_interface_addr(wpa_s->global->p2p, bssid,
1109 iface_addr) == 0)
1110 bss = wpa_bss_get(wpa_s, iface_addr, ssid, ssid_len);
1111 }
1112 if (bss == NULL) {
1113 wpa_printf(MSG_DEBUG, "P2P: Could not figure out whether "
1114 "group is persistent - BSS " MACSTR " not found",
1115 MAC2STR(bssid));
1116 return 0;
1117 }
1118
1119 p2p = wpa_bss_get_vendor_ie_multi(bss, P2P_IE_VENDOR_TYPE);
Dmitry Shmidt96571392013-10-14 12:54:46 -07001120 if (p2p == NULL)
1121 p2p = wpa_bss_get_vendor_ie_multi_beacon(bss,
1122 P2P_IE_VENDOR_TYPE);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001123 if (p2p == NULL) {
1124 wpa_printf(MSG_DEBUG, "P2P: Could not figure out whether "
1125 "group is persistent - BSS " MACSTR
1126 " did not include P2P IE", MAC2STR(bssid));
1127 wpa_hexdump(MSG_DEBUG, "P2P: Probe Response IEs",
Hai Shalom60840252021-02-19 19:02:11 -08001128 wpa_bss_ie_ptr(bss), bss->ie_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001129 wpa_hexdump(MSG_DEBUG, "P2P: Beacon IEs",
Hai Shalom60840252021-02-19 19:02:11 -08001130 wpa_bss_ie_ptr(bss) + bss->ie_len,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001131 bss->beacon_ie_len);
1132 return 0;
1133 }
1134
1135 group_capab = p2p_get_group_capab(p2p);
1136 addr = p2p_get_go_dev_addr(p2p);
1137 wpa_printf(MSG_DEBUG, "P2P: Checking whether group is persistent: "
1138 "group_capab=0x%x", group_capab);
1139 if (addr) {
1140 os_memcpy(go_dev_addr, addr, ETH_ALEN);
1141 wpa_printf(MSG_DEBUG, "P2P: GO Device Address " MACSTR,
1142 MAC2STR(addr));
1143 } else
1144 os_memset(go_dev_addr, 0, ETH_ALEN);
1145 wpabuf_free(p2p);
1146
1147 wpa_printf(MSG_DEBUG, "P2P: BSS " MACSTR " group_capab=0x%x "
1148 "go_dev_addr=" MACSTR,
1149 MAC2STR(bssid), group_capab, MAC2STR(go_dev_addr));
1150
Dmitry Shmidt7f2c7532016-08-15 09:48:12 -07001151 return !!(group_capab & P2P_GROUP_CAPAB_PERSISTENT_GROUP);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001152}
1153
1154
Jouni Malinen75ecf522011-06-27 15:19:46 -07001155static int wpas_p2p_store_persistent_group(struct wpa_supplicant *wpa_s,
1156 struct wpa_ssid *ssid,
1157 const u8 *go_dev_addr)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001158{
1159 struct wpa_ssid *s;
1160 int changed = 0;
1161
1162 wpa_printf(MSG_DEBUG, "P2P: Storing credentials for a persistent "
1163 "group (GO Dev Addr " MACSTR ")", MAC2STR(go_dev_addr));
1164 for (s = wpa_s->conf->ssid; s; s = s->next) {
1165 if (s->disabled == 2 &&
1166 os_memcmp(go_dev_addr, s->bssid, ETH_ALEN) == 0 &&
1167 s->ssid_len == ssid->ssid_len &&
1168 os_memcmp(ssid->ssid, s->ssid, ssid->ssid_len) == 0)
1169 break;
1170 }
1171
1172 if (s) {
1173 wpa_printf(MSG_DEBUG, "P2P: Update existing persistent group "
1174 "entry");
1175 if (ssid->passphrase && !s->passphrase)
1176 changed = 1;
1177 else if (ssid->passphrase && s->passphrase &&
1178 os_strcmp(ssid->passphrase, s->passphrase) != 0)
1179 changed = 1;
1180 } else {
1181 wpa_printf(MSG_DEBUG, "P2P: Create a new persistent group "
1182 "entry");
1183 changed = 1;
1184 s = wpa_config_add_network(wpa_s->conf);
1185 if (s == NULL)
Jouni Malinen75ecf522011-06-27 15:19:46 -07001186 return -1;
1187
1188 /*
1189 * Instead of network_added we emit persistent_group_added
1190 * notification. Also to keep the defense checks in
1191 * persistent_group obj registration method, we set the
1192 * relevant flags in s to designate it as a persistent group.
1193 */
1194 s->p2p_group = 1;
1195 s->p2p_persistent_group = 1;
1196 wpas_notify_persistent_group_added(wpa_s, s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001197 wpa_config_set_network_defaults(s);
1198 }
1199
1200 s->p2p_group = 1;
1201 s->p2p_persistent_group = 1;
1202 s->disabled = 2;
1203 s->bssid_set = 1;
1204 os_memcpy(s->bssid, go_dev_addr, ETH_ALEN);
1205 s->mode = ssid->mode;
1206 s->auth_alg = WPA_AUTH_ALG_OPEN;
1207 s->key_mgmt = WPA_KEY_MGMT_PSK;
1208 s->proto = WPA_PROTO_RSN;
Dmitry Shmidte4663042016-04-04 10:07:49 -07001209 s->pbss = ssid->pbss;
1210 s->pairwise_cipher = ssid->pbss ? WPA_CIPHER_GCMP : WPA_CIPHER_CCMP;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001211 s->export_keys = 1;
1212 if (ssid->passphrase) {
1213 os_free(s->passphrase);
1214 s->passphrase = os_strdup(ssid->passphrase);
1215 }
1216 if (ssid->psk_set) {
1217 s->psk_set = 1;
1218 os_memcpy(s->psk, ssid->psk, 32);
1219 }
1220 if (s->passphrase && !s->psk_set)
1221 wpa_config_update_psk(s);
1222 if (s->ssid == NULL || s->ssid_len < ssid->ssid_len) {
1223 os_free(s->ssid);
1224 s->ssid = os_malloc(ssid->ssid_len);
1225 }
1226 if (s->ssid) {
1227 s->ssid_len = ssid->ssid_len;
1228 os_memcpy(s->ssid, ssid->ssid, s->ssid_len);
1229 }
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07001230 if (ssid->mode == WPAS_MODE_P2P_GO && wpa_s->global->add_psk) {
1231 dl_list_add(&s->psk_list, &wpa_s->global->add_psk->list);
1232 wpa_s->global->add_psk = NULL;
1233 changed = 1;
1234 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001235
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001236 if (changed && wpa_s->conf->update_config &&
1237 wpa_config_write(wpa_s->confname, wpa_s->conf)) {
1238 wpa_printf(MSG_DEBUG, "P2P: Failed to update configuration");
1239 }
Jouni Malinen75ecf522011-06-27 15:19:46 -07001240
1241 return s->id;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001242}
1243
1244
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001245static void wpas_p2p_add_persistent_group_client(struct wpa_supplicant *wpa_s,
1246 const u8 *addr)
1247{
1248 struct wpa_ssid *ssid, *s;
1249 u8 *n;
1250 size_t i;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001251 int found = 0;
Dmitry Shmidt8bd70b72015-05-26 16:02:19 -07001252 struct wpa_supplicant *p2p_wpa_s = wpa_s->global->p2p_init_wpa_s;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001253
1254 ssid = wpa_s->current_ssid;
1255 if (ssid == NULL || ssid->mode != WPAS_MODE_P2P_GO ||
1256 !ssid->p2p_persistent_group)
1257 return;
1258
Dmitry Shmidt8bd70b72015-05-26 16:02:19 -07001259 for (s = p2p_wpa_s->conf->ssid; s; s = s->next) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001260 if (s->disabled != 2 || s->mode != WPAS_MODE_P2P_GO)
1261 continue;
1262
1263 if (s->ssid_len == ssid->ssid_len &&
1264 os_memcmp(s->ssid, ssid->ssid, s->ssid_len) == 0)
1265 break;
1266 }
1267
1268 if (s == NULL)
1269 return;
1270
1271 for (i = 0; s->p2p_client_list && i < s->num_p2p_clients; i++) {
Dmitry Shmidtff787d52015-01-12 13:01:47 -08001272 if (os_memcmp(s->p2p_client_list + i * 2 * ETH_ALEN, addr,
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001273 ETH_ALEN) != 0)
1274 continue;
1275
1276 if (i == s->num_p2p_clients - 1)
1277 return; /* already the most recent entry */
1278
1279 /* move the entry to mark it most recent */
Dmitry Shmidtff787d52015-01-12 13:01:47 -08001280 os_memmove(s->p2p_client_list + i * 2 * ETH_ALEN,
1281 s->p2p_client_list + (i + 1) * 2 * ETH_ALEN,
1282 (s->num_p2p_clients - i - 1) * 2 * ETH_ALEN);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001283 os_memcpy(s->p2p_client_list +
Dmitry Shmidtff787d52015-01-12 13:01:47 -08001284 (s->num_p2p_clients - 1) * 2 * ETH_ALEN, addr,
1285 ETH_ALEN);
1286 os_memset(s->p2p_client_list +
1287 (s->num_p2p_clients - 1) * 2 * ETH_ALEN + ETH_ALEN,
1288 0xff, ETH_ALEN);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001289 found = 1;
1290 break;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001291 }
1292
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001293 if (!found && s->num_p2p_clients < P2P_MAX_STORED_CLIENTS) {
1294 n = os_realloc_array(s->p2p_client_list,
Dmitry Shmidtff787d52015-01-12 13:01:47 -08001295 s->num_p2p_clients + 1, 2 * ETH_ALEN);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001296 if (n == NULL)
1297 return;
Dmitry Shmidtff787d52015-01-12 13:01:47 -08001298 os_memcpy(n + s->num_p2p_clients * 2 * ETH_ALEN, addr,
1299 ETH_ALEN);
1300 os_memset(n + s->num_p2p_clients * 2 * ETH_ALEN + ETH_ALEN,
1301 0xff, ETH_ALEN);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001302 s->p2p_client_list = n;
1303 s->num_p2p_clients++;
Dmitry Shmidt6aa8ae42014-07-07 09:31:33 -07001304 } else if (!found && s->p2p_client_list) {
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001305 /* Not enough room for an additional entry - drop the oldest
1306 * entry */
1307 os_memmove(s->p2p_client_list,
Dmitry Shmidtff787d52015-01-12 13:01:47 -08001308 s->p2p_client_list + 2 * ETH_ALEN,
1309 (s->num_p2p_clients - 1) * 2 * ETH_ALEN);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001310 os_memcpy(s->p2p_client_list +
Dmitry Shmidtff787d52015-01-12 13:01:47 -08001311 (s->num_p2p_clients - 1) * 2 * ETH_ALEN,
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001312 addr, ETH_ALEN);
Dmitry Shmidtff787d52015-01-12 13:01:47 -08001313 os_memset(s->p2p_client_list +
1314 (s->num_p2p_clients - 1) * 2 * ETH_ALEN + ETH_ALEN,
1315 0xff, ETH_ALEN);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001316 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001317
Dmitry Shmidt8bd70b72015-05-26 16:02:19 -07001318 if (p2p_wpa_s->conf->update_config &&
1319 wpa_config_write(p2p_wpa_s->confname, p2p_wpa_s->conf))
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001320 wpa_printf(MSG_DEBUG, "P2P: Failed to update configuration");
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001321}
1322
1323
Dmitry Shmidt7f0b69e2014-07-28 10:35:20 -07001324static void wpas_p2p_group_started(struct wpa_supplicant *wpa_s,
1325 int go, struct wpa_ssid *ssid, int freq,
1326 const u8 *psk, const char *passphrase,
1327 const u8 *go_dev_addr, int persistent,
1328 const char *extra)
1329{
1330 const char *ssid_txt;
1331 char psk_txt[65];
1332
1333 if (psk)
1334 wpa_snprintf_hex(psk_txt, sizeof(psk_txt), psk, 32);
1335 else
1336 psk_txt[0] = '\0';
1337
1338 if (ssid)
1339 ssid_txt = wpa_ssid_txt(ssid->ssid, ssid->ssid_len);
1340 else
1341 ssid_txt = "";
1342
1343 if (passphrase && passphrase[0] == '\0')
1344 passphrase = NULL;
1345
1346 /*
1347 * Include PSK/passphrase only in the control interface message and
1348 * leave it out from the debug log entry.
1349 */
Dmitry Shmidt9c175262016-03-03 10:20:07 -08001350 wpa_msg_global_ctrl(wpa_s->p2pdev, MSG_INFO,
Dmitry Shmidt7f0b69e2014-07-28 10:35:20 -07001351 P2P_EVENT_GROUP_STARTED
1352 "%s %s ssid=\"%s\" freq=%d%s%s%s%s%s go_dev_addr="
1353 MACSTR "%s%s",
1354 wpa_s->ifname, go ? "GO" : "client", ssid_txt, freq,
1355 psk ? " psk=" : "", psk_txt,
1356 passphrase ? " passphrase=\"" : "",
1357 passphrase ? passphrase : "",
1358 passphrase ? "\"" : "",
1359 MAC2STR(go_dev_addr),
1360 persistent ? " [PERSISTENT]" : "", extra);
1361 wpa_printf(MSG_INFO, P2P_EVENT_GROUP_STARTED
1362 "%s %s ssid=\"%s\" freq=%d go_dev_addr=" MACSTR "%s%s",
1363 wpa_s->ifname, go ? "GO" : "client", ssid_txt, freq,
1364 MAC2STR(go_dev_addr), persistent ? " [PERSISTENT]" : "",
1365 extra);
1366}
1367
1368
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001369static void wpas_group_formation_completed(struct wpa_supplicant *wpa_s,
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07001370 int success, int already_deleted)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001371{
1372 struct wpa_ssid *ssid;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001373 int client;
1374 int persistent;
1375 u8 go_dev_addr[ETH_ALEN];
1376
1377 /*
1378 * This callback is likely called for the main interface. Update wpa_s
1379 * to use the group interface if a new interface was created for the
1380 * group.
1381 */
1382 if (wpa_s->global->p2p_group_formation)
1383 wpa_s = wpa_s->global->p2p_group_formation;
Dmitry Shmidt56052862013-10-04 10:23:25 -07001384 if (wpa_s->p2p_go_group_formation_completed) {
1385 wpa_s->global->p2p_group_formation = NULL;
1386 wpa_s->p2p_in_provisioning = 0;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001387 } else if (wpa_s->p2p_in_provisioning && !success) {
1388 wpa_msg(wpa_s, MSG_DEBUG,
1389 "P2P: Stop provisioning state due to failure");
1390 wpa_s->p2p_in_provisioning = 0;
Dmitry Shmidt56052862013-10-04 10:23:25 -07001391 }
Dmitry Shmidt21de2142014-04-08 10:50:52 -07001392 wpa_s->p2p_in_invitation = 0;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001393 wpa_s->group_formation_reported = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001394
1395 if (!success) {
Dmitry Shmidt9c175262016-03-03 10:20:07 -08001396 wpa_msg_global(wpa_s->p2pdev, MSG_INFO,
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07001397 P2P_EVENT_GROUP_FORMATION_FAILURE);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001398 wpas_notify_p2p_group_formation_failure(wpa_s, "");
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07001399 if (already_deleted)
1400 return;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001401 wpas_p2p_group_delete(wpa_s,
1402 P2P_GROUP_REMOVAL_FORMATION_FAILED);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001403 return;
1404 }
1405
Dmitry Shmidt9c175262016-03-03 10:20:07 -08001406 wpa_msg_global(wpa_s->p2pdev, MSG_INFO,
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07001407 P2P_EVENT_GROUP_FORMATION_SUCCESS);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001408
1409 ssid = wpa_s->current_ssid;
1410 if (ssid && ssid->mode == WPAS_MODE_P2P_GROUP_FORMATION) {
1411 ssid->mode = WPAS_MODE_P2P_GO;
1412 p2p_group_notif_formation_done(wpa_s->p2p_group);
1413 wpa_supplicant_ap_mac_addr_filter(wpa_s, NULL);
1414 }
1415
1416 persistent = 0;
1417 if (ssid) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001418 client = ssid->mode == WPAS_MODE_INFRA;
1419 if (ssid->mode == WPAS_MODE_P2P_GO) {
1420 persistent = ssid->p2p_persistent_group;
Dmitry Shmidt497c1d52011-07-21 15:19:46 -07001421 os_memcpy(go_dev_addr, wpa_s->global->p2p_dev_addr,
1422 ETH_ALEN);
George Burgess IV0d2d1bf2021-11-01 03:08:33 -07001423 } else {
1424 os_memset(go_dev_addr, 0, ETH_ALEN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001425 persistent = wpas_p2p_persistent_group(wpa_s,
1426 go_dev_addr,
1427 ssid->ssid,
1428 ssid->ssid_len);
George Burgess IV0d2d1bf2021-11-01 03:08:33 -07001429 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001430 } else {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001431 client = wpa_s->p2p_group_interface ==
1432 P2P_GROUP_INTERFACE_CLIENT;
1433 os_memset(go_dev_addr, 0, ETH_ALEN);
1434 }
1435
1436 wpa_s->show_group_started = 0;
1437 if (client) {
1438 /*
1439 * Indicate event only after successfully completed 4-way
1440 * handshake, i.e., when the interface is ready for data
1441 * packets.
1442 */
1443 wpa_s->show_group_started = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001444 } else {
Dmitry Shmidt7f0b69e2014-07-28 10:35:20 -07001445 wpas_p2p_group_started(wpa_s, 1, ssid,
1446 ssid ? ssid->frequency : 0,
1447 ssid && ssid->passphrase == NULL &&
1448 ssid->psk_set ? ssid->psk : NULL,
1449 ssid ? ssid->passphrase : NULL,
1450 go_dev_addr, persistent, "");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001451 wpas_p2p_cross_connect_setup(wpa_s);
1452 wpas_p2p_set_group_idle_timeout(wpa_s);
1453 }
1454
1455 if (persistent)
Dmitry Shmidtd5ab1b52016-06-21 12:38:41 -07001456 wpas_p2p_store_persistent_group(wpa_s->p2pdev,
1457 ssid, go_dev_addr);
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07001458 else {
1459 os_free(wpa_s->global->add_psk);
1460 wpa_s->global->add_psk = NULL;
1461 }
Dmitry Shmidtd5ab1b52016-06-21 12:38:41 -07001462
Dmitry Shmidt92c368d2013-08-29 12:37:21 -07001463 if (!client) {
Dmitry Shmidtabb90a32016-12-05 15:34:39 -08001464 wpas_notify_p2p_group_started(wpa_s, ssid, persistent, 0, NULL);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001465 os_get_reltime(&wpa_s->global->p2p_go_wait_client);
Dmitry Shmidt92c368d2013-08-29 12:37:21 -07001466 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001467}
1468
1469
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001470struct send_action_work {
1471 unsigned int freq;
1472 u8 dst[ETH_ALEN];
1473 u8 src[ETH_ALEN];
1474 u8 bssid[ETH_ALEN];
1475 size_t len;
1476 unsigned int wait_time;
1477 u8 buf[0];
1478};
1479
1480
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001481static void wpas_p2p_free_send_action_work(struct wpa_supplicant *wpa_s)
1482{
1483 struct send_action_work *awork = wpa_s->p2p_send_action_work->ctx;
1484
1485 wpa_printf(MSG_DEBUG,
1486 "P2P: Free Action frame radio work @%p (freq=%u dst="
1487 MACSTR " src=" MACSTR " bssid=" MACSTR " wait_time=%u)",
1488 wpa_s->p2p_send_action_work, awork->freq,
1489 MAC2STR(awork->dst), MAC2STR(awork->src),
1490 MAC2STR(awork->bssid), awork->wait_time);
1491 wpa_hexdump(MSG_DEBUG, "P2P: Freeing pending Action frame",
1492 awork->buf, awork->len);
1493 os_free(awork);
1494 wpa_s->p2p_send_action_work->ctx = NULL;
1495 radio_work_done(wpa_s->p2p_send_action_work);
1496 wpa_s->p2p_send_action_work = NULL;
1497}
1498
1499
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001500static void wpas_p2p_send_action_work_timeout(void *eloop_ctx,
1501 void *timeout_ctx)
1502{
1503 struct wpa_supplicant *wpa_s = eloop_ctx;
1504
1505 if (!wpa_s->p2p_send_action_work)
1506 return;
1507
1508 wpa_printf(MSG_DEBUG, "P2P: Send Action frame radio work timed out");
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001509 wpas_p2p_free_send_action_work(wpa_s);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001510}
1511
1512
Dmitry Shmidt03658832014-08-13 11:03:49 -07001513static void wpas_p2p_action_tx_clear(struct wpa_supplicant *wpa_s)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001514{
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001515 if (wpa_s->p2p_send_action_work) {
1516 struct send_action_work *awork;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001517
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001518 awork = wpa_s->p2p_send_action_work->ctx;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001519 wpa_printf(MSG_DEBUG,
1520 "P2P: Clear Action TX work @%p (wait_time=%u)",
1521 wpa_s->p2p_send_action_work, awork->wait_time);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001522 if (awork->wait_time == 0) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001523 wpas_p2p_free_send_action_work(wpa_s);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001524 } else {
1525 /*
1526 * In theory, this should not be needed, but number of
1527 * places in the P2P code is still using non-zero wait
1528 * time for the last Action frame in the sequence and
1529 * some of these do not call send_action_done().
1530 */
1531 eloop_cancel_timeout(wpas_p2p_send_action_work_timeout,
1532 wpa_s, NULL);
1533 eloop_register_timeout(
1534 0, awork->wait_time * 1000,
1535 wpas_p2p_send_action_work_timeout,
1536 wpa_s, NULL);
1537 }
1538 }
Dmitry Shmidt03658832014-08-13 11:03:49 -07001539}
1540
1541
1542static void wpas_p2p_send_action_tx_status(struct wpa_supplicant *wpa_s,
1543 unsigned int freq,
1544 const u8 *dst, const u8 *src,
1545 const u8 *bssid,
1546 const u8 *data, size_t data_len,
1547 enum offchannel_send_action_result
1548 result)
1549{
1550 enum p2p_send_action_result res = P2P_SEND_ACTION_SUCCESS;
1551
1552 wpas_p2p_action_tx_clear(wpa_s);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001553
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001554 if (wpa_s->global->p2p == NULL || wpa_s->global->p2p_disabled)
1555 return;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001556
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001557 switch (result) {
1558 case OFFCHANNEL_SEND_ACTION_SUCCESS:
1559 res = P2P_SEND_ACTION_SUCCESS;
1560 break;
1561 case OFFCHANNEL_SEND_ACTION_NO_ACK:
1562 res = P2P_SEND_ACTION_NO_ACK;
1563 break;
1564 case OFFCHANNEL_SEND_ACTION_FAILED:
1565 res = P2P_SEND_ACTION_FAILED;
1566 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001567 }
1568
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001569 p2p_send_action_cb(wpa_s->global->p2p, freq, dst, src, bssid, res);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001570
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08001571 if (result != OFFCHANNEL_SEND_ACTION_SUCCESS &&
1572 wpa_s->pending_pd_before_join &&
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001573 (os_memcmp(dst, wpa_s->pending_join_dev_addr, ETH_ALEN) == 0 ||
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001574 os_memcmp(dst, wpa_s->pending_join_iface_addr, ETH_ALEN) == 0) &&
1575 wpa_s->p2p_fallback_to_go_neg) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001576 wpa_s->pending_pd_before_join = 0;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001577 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: No ACK for PD Req "
1578 "during p2p_connect-auto");
Dmitry Shmidt9c175262016-03-03 10:20:07 -08001579 wpa_msg_global(wpa_s->p2pdev, MSG_INFO,
Dmitry Shmidt7f656022015-02-25 14:36:37 -08001580 P2P_EVENT_FALLBACK_TO_GO_NEG
1581 "reason=no-ACK-to-PD-Req");
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001582 wpas_p2p_fallback_to_go_neg(wpa_s, 0);
1583 return;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001584 }
1585}
1586
1587
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001588static void wpas_send_action_cb(struct wpa_radio_work *work, int deinit)
1589{
1590 struct wpa_supplicant *wpa_s = work->wpa_s;
1591 struct send_action_work *awork = work->ctx;
1592
1593 if (deinit) {
Dmitry Shmidtbd14a572014-02-18 10:33:49 -08001594 if (work->started) {
1595 eloop_cancel_timeout(wpas_p2p_send_action_work_timeout,
1596 wpa_s, NULL);
1597 wpa_s->p2p_send_action_work = NULL;
1598 offchannel_send_action_done(wpa_s);
1599 }
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001600 os_free(awork);
1601 return;
1602 }
1603
1604 if (offchannel_send_action(wpa_s, awork->freq, awork->dst, awork->src,
1605 awork->bssid, awork->buf, awork->len,
1606 awork->wait_time,
1607 wpas_p2p_send_action_tx_status, 1) < 0) {
1608 os_free(awork);
1609 radio_work_done(work);
1610 return;
1611 }
1612 wpa_s->p2p_send_action_work = work;
1613}
1614
1615
1616static int wpas_send_action_work(struct wpa_supplicant *wpa_s,
1617 unsigned int freq, const u8 *dst,
1618 const u8 *src, const u8 *bssid, const u8 *buf,
1619 size_t len, unsigned int wait_time)
1620{
1621 struct send_action_work *awork;
1622
Hai Shalom81f62d82019-07-22 12:10:00 -07001623 if (radio_work_pending(wpa_s, "p2p-send-action")) {
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001624 wpa_printf(MSG_DEBUG, "P2P: Cannot schedule new p2p-send-action work since one is already pending");
1625 return -1;
1626 }
1627
1628 awork = os_zalloc(sizeof(*awork) + len);
1629 if (awork == NULL)
1630 return -1;
1631
1632 awork->freq = freq;
1633 os_memcpy(awork->dst, dst, ETH_ALEN);
1634 os_memcpy(awork->src, src, ETH_ALEN);
1635 os_memcpy(awork->bssid, bssid, ETH_ALEN);
1636 awork->len = len;
1637 awork->wait_time = wait_time;
1638 os_memcpy(awork->buf, buf, len);
1639
Hai Shalom81f62d82019-07-22 12:10:00 -07001640 if (radio_add_work(wpa_s, freq, "p2p-send-action", 1,
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001641 wpas_send_action_cb, awork) < 0) {
1642 os_free(awork);
1643 return -1;
1644 }
1645
1646 return 0;
1647}
1648
1649
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001650static int wpas_send_action(void *ctx, unsigned int freq, const u8 *dst,
1651 const u8 *src, const u8 *bssid, const u8 *buf,
Hai Shalom021b0b52019-04-10 11:17:58 -07001652 size_t len, unsigned int wait_time, int *scheduled)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001653{
1654 struct wpa_supplicant *wpa_s = ctx;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001655 int listen_freq = -1, send_freq = -1;
1656
Hai Shalom021b0b52019-04-10 11:17:58 -07001657 if (scheduled)
1658 *scheduled = 0;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001659 if (wpa_s->p2p_listen_work)
1660 listen_freq = wpa_s->p2p_listen_work->freq;
1661 if (wpa_s->p2p_send_action_work)
1662 send_freq = wpa_s->p2p_send_action_work->freq;
1663 if (listen_freq != (int) freq && send_freq != (int) freq) {
Hai Shalom021b0b52019-04-10 11:17:58 -07001664 int res;
1665
1666 wpa_printf(MSG_DEBUG, "P2P: Schedule new radio work for Action frame TX (listen_freq=%d send_freq=%d freq=%u)",
1667 listen_freq, send_freq, freq);
1668 res = wpas_send_action_work(wpa_s, freq, dst, src, bssid, buf,
1669 len, wait_time);
1670 if (res == 0 && scheduled)
1671 *scheduled = 1;
1672 return res;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001673 }
1674
1675 wpa_printf(MSG_DEBUG, "P2P: Use ongoing radio work for Action frame TX");
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001676 return offchannel_send_action(wpa_s, freq, dst, src, bssid, buf, len,
1677 wait_time,
1678 wpas_p2p_send_action_tx_status, 1);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001679}
1680
1681
1682static void wpas_send_action_done(void *ctx)
1683{
1684 struct wpa_supplicant *wpa_s = ctx;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001685
1686 if (wpa_s->p2p_send_action_work) {
1687 eloop_cancel_timeout(wpas_p2p_send_action_work_timeout,
1688 wpa_s, NULL);
1689 os_free(wpa_s->p2p_send_action_work->ctx);
1690 radio_work_done(wpa_s->p2p_send_action_work);
1691 wpa_s->p2p_send_action_work = NULL;
1692 }
1693
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001694 offchannel_send_action_done(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001695}
1696
1697
1698static int wpas_copy_go_neg_results(struct wpa_supplicant *wpa_s,
1699 struct p2p_go_neg_results *params)
1700{
1701 if (wpa_s->go_params == NULL) {
1702 wpa_s->go_params = os_malloc(sizeof(*params));
1703 if (wpa_s->go_params == NULL)
1704 return -1;
1705 }
1706 os_memcpy(wpa_s->go_params, params, sizeof(*params));
1707 return 0;
1708}
1709
1710
1711static void wpas_start_wps_enrollee(struct wpa_supplicant *wpa_s,
1712 struct p2p_go_neg_results *res)
1713{
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001714 wpa_s->group_formation_reported = 0;
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001715 wpa_printf(MSG_DEBUG, "P2P: Start WPS Enrollee for peer " MACSTR
1716 " dev_addr " MACSTR " wps_method %d",
1717 MAC2STR(res->peer_interface_addr),
1718 MAC2STR(res->peer_device_addr), res->wps_method);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001719 wpa_hexdump_ascii(MSG_DEBUG, "P2P: Start WPS Enrollee for SSID",
1720 res->ssid, res->ssid_len);
1721 wpa_supplicant_ap_deinit(wpa_s);
1722 wpas_copy_go_neg_results(wpa_s, res);
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001723 if (res->wps_method == WPS_PBC) {
Hai Shalom021b0b52019-04-10 11:17:58 -07001724 wpas_wps_start_pbc(wpa_s, res->peer_interface_addr, 1, 0);
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001725#ifdef CONFIG_WPS_NFC
1726 } else if (res->wps_method == WPS_NFC) {
1727 wpas_wps_start_nfc(wpa_s, res->peer_device_addr,
1728 res->peer_interface_addr,
Dmitry Shmidt9c175262016-03-03 10:20:07 -08001729 wpa_s->p2pdev->p2p_oob_dev_pw,
1730 wpa_s->p2pdev->p2p_oob_dev_pw_id, 1,
1731 wpa_s->p2pdev->p2p_oob_dev_pw_id ==
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001732 DEV_PW_NFC_CONNECTION_HANDOVER ?
Dmitry Shmidt9c175262016-03-03 10:20:07 -08001733 wpa_s->p2pdev->p2p_peer_oob_pubkey_hash :
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001734 NULL,
1735 NULL, 0, 0);
1736#endif /* CONFIG_WPS_NFC */
1737 } else {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001738 u16 dev_pw_id = DEV_PW_DEFAULT;
Dmitry Shmidt216983b2015-02-06 10:50:36 -08001739 if (wpa_s->p2p_wps_method == WPS_P2PS)
1740 dev_pw_id = DEV_PW_P2PS_DEFAULT;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001741 if (wpa_s->p2p_wps_method == WPS_PIN_KEYPAD)
1742 dev_pw_id = DEV_PW_REGISTRAR_SPECIFIED;
1743 wpas_wps_start_pin(wpa_s, res->peer_interface_addr,
1744 wpa_s->p2p_pin, 1, dev_pw_id);
1745 }
1746}
1747
1748
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07001749static void wpas_p2p_add_psk_list(struct wpa_supplicant *wpa_s,
1750 struct wpa_ssid *ssid)
1751{
1752 struct wpa_ssid *persistent;
1753 struct psk_list_entry *psk;
1754 struct hostapd_data *hapd;
1755
1756 if (!wpa_s->ap_iface)
1757 return;
1758
Dmitry Shmidt9c175262016-03-03 10:20:07 -08001759 persistent = wpas_p2p_get_persistent(wpa_s->p2pdev, NULL, ssid->ssid,
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07001760 ssid->ssid_len);
1761 if (persistent == NULL)
1762 return;
1763
1764 hapd = wpa_s->ap_iface->bss[0];
1765
1766 dl_list_for_each(psk, &persistent->psk_list, struct psk_list_entry,
1767 list) {
1768 struct hostapd_wpa_psk *hpsk;
1769
1770 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Add persistent group PSK entry for "
1771 MACSTR " psk=%d",
1772 MAC2STR(psk->addr), psk->p2p);
1773 hpsk = os_zalloc(sizeof(*hpsk));
1774 if (hpsk == NULL)
1775 break;
1776 os_memcpy(hpsk->psk, psk->psk, PMK_LEN);
1777 if (psk->p2p)
1778 os_memcpy(hpsk->p2p_dev_addr, psk->addr, ETH_ALEN);
1779 else
1780 os_memcpy(hpsk->addr, psk->addr, ETH_ALEN);
1781 hpsk->next = hapd->conf->ssid.wpa_psk;
1782 hapd->conf->ssid.wpa_psk = hpsk;
1783 }
1784}
1785
1786
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001787static void p2p_go_dump_common_freqs(struct wpa_supplicant *wpa_s)
1788{
Roshan Pius3a1667e2018-07-03 15:17:14 -07001789 char buf[20 + P2P_MAX_CHANNELS * 6];
1790 char *pos, *end;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001791 unsigned int i;
Roshan Pius3a1667e2018-07-03 15:17:14 -07001792 int res;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001793
Roshan Pius3a1667e2018-07-03 15:17:14 -07001794 pos = buf;
1795 end = pos + sizeof(buf);
1796 for (i = 0; i < wpa_s->p2p_group_common_freqs_num; i++) {
1797 res = os_snprintf(pos, end - pos, " %d",
1798 wpa_s->p2p_group_common_freqs[i]);
1799 if (os_snprintf_error(end - pos, res))
1800 break;
1801 pos += res;
1802 }
1803 *pos = '\0';
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001804
Roshan Pius3a1667e2018-07-03 15:17:14 -07001805 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Common group frequencies:%s", buf);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001806}
1807
1808
1809static void p2p_go_save_group_common_freqs(struct wpa_supplicant *wpa_s,
1810 struct p2p_go_neg_results *params)
1811{
1812 unsigned int i, len = int_array_len(wpa_s->go_params->freq_list);
1813
1814 wpa_s->p2p_group_common_freqs_num = 0;
1815 os_free(wpa_s->p2p_group_common_freqs);
1816 wpa_s->p2p_group_common_freqs = os_calloc(len, sizeof(int));
1817 if (!wpa_s->p2p_group_common_freqs)
1818 return;
1819
1820 for (i = 0; i < len; i++) {
1821 if (!wpa_s->go_params->freq_list[i])
1822 break;
1823 wpa_s->p2p_group_common_freqs[i] =
1824 wpa_s->go_params->freq_list[i];
1825 }
1826 wpa_s->p2p_group_common_freqs_num = i;
1827}
1828
1829
Dmitry Shmidt216983b2015-02-06 10:50:36 -08001830static void p2p_config_write(struct wpa_supplicant *wpa_s)
1831{
1832#ifndef CONFIG_NO_CONFIG_WRITE
Dmitry Shmidt9c175262016-03-03 10:20:07 -08001833 if (wpa_s->p2pdev->conf->update_config &&
1834 wpa_config_write(wpa_s->p2pdev->confname, wpa_s->p2pdev->conf))
Dmitry Shmidt216983b2015-02-06 10:50:36 -08001835 wpa_printf(MSG_DEBUG, "P2P: Failed to update configuration");
1836#endif /* CONFIG_NO_CONFIG_WRITE */
1837}
1838
1839
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001840static void p2p_go_configured(void *ctx, void *data)
1841{
1842 struct wpa_supplicant *wpa_s = ctx;
1843 struct p2p_go_neg_results *params = data;
1844 struct wpa_ssid *ssid;
1845
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08001846 wpa_s->ap_configured_cb = NULL;
1847 wpa_s->ap_configured_cb_ctx = NULL;
1848 wpa_s->ap_configured_cb_data = NULL;
1849 if (!wpa_s->go_params) {
1850 wpa_printf(MSG_ERROR,
1851 "P2P: p2p_go_configured() called with wpa_s->go_params == NULL");
1852 return;
1853 }
1854
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001855 p2p_go_save_group_common_freqs(wpa_s, params);
1856 p2p_go_dump_common_freqs(wpa_s);
1857
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001858 ssid = wpa_s->current_ssid;
1859 if (ssid && ssid->mode == WPAS_MODE_P2P_GO) {
1860 wpa_printf(MSG_DEBUG, "P2P: Group setup without provisioning");
1861 if (wpa_s->global->p2p_group_formation == wpa_s)
1862 wpa_s->global->p2p_group_formation = NULL;
Dmitry Shmidt7f0b69e2014-07-28 10:35:20 -07001863 wpas_p2p_group_started(wpa_s, 1, ssid, ssid->frequency,
1864 params->passphrase[0] == '\0' ?
1865 params->psk : NULL,
1866 params->passphrase,
1867 wpa_s->global->p2p_dev_addr,
1868 params->persistent_group, "");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001869 wpa_s->group_formation_reported = 1;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001870
Dmitry Shmidt9c175262016-03-03 10:20:07 -08001871 if (wpa_s->p2pdev->p2ps_method_config_any) {
1872 if (is_zero_ether_addr(wpa_s->p2pdev->p2ps_join_addr)) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001873 wpa_dbg(wpa_s, MSG_DEBUG,
1874 "P2PS: Setting default PIN for ANY");
1875 wpa_supplicant_ap_wps_pin(wpa_s, NULL,
1876 "12345670", NULL, 0,
1877 0);
1878 } else {
1879 wpa_dbg(wpa_s, MSG_DEBUG,
1880 "P2PS: Setting default PIN for " MACSTR,
Dmitry Shmidt9c175262016-03-03 10:20:07 -08001881 MAC2STR(wpa_s->p2pdev->p2ps_join_addr));
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001882 wpa_supplicant_ap_wps_pin(
Dmitry Shmidt9c175262016-03-03 10:20:07 -08001883 wpa_s, wpa_s->p2pdev->p2ps_join_addr,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001884 "12345670", NULL, 0, 0);
1885 }
Dmitry Shmidt9c175262016-03-03 10:20:07 -08001886 wpa_s->p2pdev->p2ps_method_config_any = 0;
Dmitry Shmidt216983b2015-02-06 10:50:36 -08001887 }
1888
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001889 os_get_reltime(&wpa_s->global->p2p_go_wait_client);
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07001890 if (params->persistent_group) {
Dmitry Shmidtd5ab1b52016-06-21 12:38:41 -07001891 wpas_p2p_store_persistent_group(
Dmitry Shmidt9c175262016-03-03 10:20:07 -08001892 wpa_s->p2pdev, ssid,
Dmitry Shmidt497c1d52011-07-21 15:19:46 -07001893 wpa_s->global->p2p_dev_addr);
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07001894 wpas_p2p_add_psk_list(wpa_s, ssid);
1895 }
Dmitry Shmidtd5ab1b52016-06-21 12:38:41 -07001896
1897 wpas_notify_p2p_group_started(wpa_s, ssid,
Dmitry Shmidtabb90a32016-12-05 15:34:39 -08001898 params->persistent_group, 0,
1899 NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001900 wpas_p2p_cross_connect_setup(wpa_s);
1901 wpas_p2p_set_group_idle_timeout(wpa_s);
Dmitry Shmidt56052862013-10-04 10:23:25 -07001902
1903 if (wpa_s->p2p_first_connection_timeout) {
1904 wpa_dbg(wpa_s, MSG_DEBUG,
1905 "P2P: Start group formation timeout of %d seconds until first data connection on GO",
1906 wpa_s->p2p_first_connection_timeout);
1907 wpa_s->p2p_go_group_formation_completed = 0;
1908 wpa_s->global->p2p_group_formation = wpa_s;
1909 eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
Dmitry Shmidt9c175262016-03-03 10:20:07 -08001910 wpa_s->p2pdev, NULL);
Dmitry Shmidt56052862013-10-04 10:23:25 -07001911 eloop_register_timeout(
1912 wpa_s->p2p_first_connection_timeout, 0,
1913 wpas_p2p_group_formation_timeout,
Dmitry Shmidt9c175262016-03-03 10:20:07 -08001914 wpa_s->p2pdev, NULL);
Dmitry Shmidt56052862013-10-04 10:23:25 -07001915 }
1916
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001917 return;
1918 }
1919
1920 wpa_printf(MSG_DEBUG, "P2P: Setting up WPS for GO provisioning");
1921 if (wpa_supplicant_ap_mac_addr_filter(wpa_s,
1922 params->peer_interface_addr)) {
1923 wpa_printf(MSG_DEBUG, "P2P: Failed to setup MAC address "
1924 "filtering");
1925 return;
1926 }
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001927 if (params->wps_method == WPS_PBC) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001928 wpa_supplicant_ap_wps_pbc(wpa_s, params->peer_interface_addr,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001929 params->peer_device_addr);
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001930#ifdef CONFIG_WPS_NFC
1931 } else if (params->wps_method == WPS_NFC) {
Dmitry Shmidt9c175262016-03-03 10:20:07 -08001932 if (wpa_s->p2pdev->p2p_oob_dev_pw_id !=
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001933 DEV_PW_NFC_CONNECTION_HANDOVER &&
Dmitry Shmidt9c175262016-03-03 10:20:07 -08001934 !wpa_s->p2pdev->p2p_oob_dev_pw) {
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001935 wpa_printf(MSG_DEBUG, "P2P: No NFC Dev Pw known");
1936 return;
1937 }
1938 wpas_ap_wps_add_nfc_pw(
Dmitry Shmidt9c175262016-03-03 10:20:07 -08001939 wpa_s, wpa_s->p2pdev->p2p_oob_dev_pw_id,
1940 wpa_s->p2pdev->p2p_oob_dev_pw,
1941 wpa_s->p2pdev->p2p_peer_oob_pk_hash_known ?
1942 wpa_s->p2pdev->p2p_peer_oob_pubkey_hash : NULL);
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001943#endif /* CONFIG_WPS_NFC */
1944 } else if (wpa_s->p2p_pin[0])
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001945 wpa_supplicant_ap_wps_pin(wpa_s, params->peer_interface_addr,
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001946 wpa_s->p2p_pin, NULL, 0, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001947 os_free(wpa_s->go_params);
1948 wpa_s->go_params = NULL;
1949}
1950
1951
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08001952/**
1953 * wpas_p2p_freq_to_edmg_channel - Convert frequency into EDMG channel
1954 * @freq: Frequency (MHz) to convert
1955 * @op_class: Buffer for returning operating class
1956 * @op_edmg_channel: Buffer for returning channel number
1957 * Returns: 0 on success, -1 on failure
1958 *
1959 * This can be used to find the highest channel bonding which includes the
1960 * specified frequency.
1961 */
1962static int wpas_p2p_freq_to_edmg_channel(struct wpa_supplicant *wpa_s,
1963 unsigned int freq,
1964 u8 *op_class, u8 *op_edmg_channel)
1965{
1966 struct hostapd_hw_modes *hwmode;
1967 struct ieee80211_edmg_config edmg;
1968 unsigned int i;
1969 enum chan_width chanwidth[] = {
1970 CHAN_WIDTH_8640,
1971 CHAN_WIDTH_6480,
1972 CHAN_WIDTH_4320,
1973 };
1974
1975 if (!wpa_s->hw.modes)
1976 return -1;
1977
1978 hwmode = get_mode(wpa_s->hw.modes, wpa_s->hw.num_modes,
Hai Shalom60840252021-02-19 19:02:11 -08001979 HOSTAPD_MODE_IEEE80211AD, false);
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08001980 if (!hwmode) {
1981 wpa_printf(MSG_ERROR,
1982 "Unsupported AP mode: HOSTAPD_MODE_IEEE80211AD");
1983 return -1;
1984 }
1985
1986 /* Find the highest EDMG channel bandwidth to start the P2P GO */
1987 for (i = 0; i < ARRAY_SIZE(chanwidth); i++) {
1988 if (ieee80211_chaninfo_to_channel(freq, chanwidth[i], 0,
1989 op_class,
1990 op_edmg_channel) < 0)
1991 continue;
1992
1993 hostapd_encode_edmg_chan(1, *op_edmg_channel, 0, &edmg);
1994 if (edmg.channels &&
1995 ieee802_edmg_is_allowed(hwmode->edmg, edmg)) {
1996 wpa_printf(MSG_DEBUG,
1997 "Freq %u to EDMG channel %u at opclass %u",
1998 freq, *op_edmg_channel, *op_class);
1999 return 0;
2000 }
2001 }
2002
2003 return -1;
2004}
2005
2006
2007int wpas_p2p_try_edmg_channel(struct wpa_supplicant *wpa_s,
2008 struct p2p_go_neg_results *params)
2009{
2010 u8 op_channel, op_class;
2011 int freq;
2012
2013 /* Try social channel as primary channel frequency */
2014 freq = (!params->freq) ? 58320 + 1 * 2160 : params->freq;
2015
2016 if (wpas_p2p_freq_to_edmg_channel(wpa_s, freq, &op_class,
2017 &op_channel) == 0) {
2018 wpa_printf(MSG_DEBUG,
2019 "Freq %d will be used to set an EDMG connection (channel=%u opclass=%u)",
2020 freq, op_channel, op_class);
2021 params->freq = freq;
2022 return 0;
2023 }
2024
2025 return -1;
2026}
2027
2028
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002029static void wpas_start_wps_go(struct wpa_supplicant *wpa_s,
2030 struct p2p_go_neg_results *params,
2031 int group_formation)
2032{
2033 struct wpa_ssid *ssid;
2034
Dmitry Shmidtaa532512012-09-24 10:35:31 -07002035 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Starting GO");
2036 if (wpas_copy_go_neg_results(wpa_s, params) < 0) {
2037 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Could not copy GO Negotiation "
2038 "results");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002039 return;
Dmitry Shmidtaa532512012-09-24 10:35:31 -07002040 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002041
2042 ssid = wpa_config_add_network(wpa_s->conf);
Dmitry Shmidtaa532512012-09-24 10:35:31 -07002043 if (ssid == NULL) {
2044 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Could not add network for GO");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002045 return;
Dmitry Shmidtaa532512012-09-24 10:35:31 -07002046 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002047
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002048 wpa_s->show_group_started = 0;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002049 wpa_s->p2p_go_group_formation_completed = 0;
2050 wpa_s->group_formation_reported = 0;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002051 os_memset(wpa_s->go_dev_addr, 0, ETH_ALEN);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002052
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002053 wpa_config_set_network_defaults(ssid);
2054 ssid->temporary = 1;
2055 ssid->p2p_group = 1;
Dmitry Shmidtaca489e2016-09-28 15:44:14 -07002056 ssid->p2p_persistent_group = !!params->persistent_group;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002057 ssid->mode = group_formation ? WPAS_MODE_P2P_GROUP_FORMATION :
2058 WPAS_MODE_P2P_GO;
2059 ssid->frequency = params->freq;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002060 ssid->ht40 = params->ht40;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07002061 ssid->vht = params->vht;
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08002062 ssid->max_oper_chwidth = params->max_oper_chwidth;
2063 ssid->vht_center_freq2 = params->vht_center_freq2;
Hai Shalom74f70d42019-02-11 14:42:39 -08002064 ssid->he = params->he;
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08002065 if (params->edmg) {
2066 u8 op_channel, op_class;
2067
2068 if (!wpas_p2p_freq_to_edmg_channel(wpa_s, params->freq,
2069 &op_class, &op_channel)) {
2070 ssid->edmg_channel = op_channel;
2071 ssid->enable_edmg = params->edmg;
2072 } else {
2073 wpa_dbg(wpa_s, MSG_DEBUG,
2074 "P2P: Could not match EDMG channel, freq %d, for GO",
2075 params->freq);
2076 }
2077 }
2078
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002079 ssid->ssid = os_zalloc(params->ssid_len + 1);
2080 if (ssid->ssid) {
2081 os_memcpy(ssid->ssid, params->ssid, params->ssid_len);
2082 ssid->ssid_len = params->ssid_len;
2083 }
2084 ssid->auth_alg = WPA_AUTH_ALG_OPEN;
2085 ssid->key_mgmt = WPA_KEY_MGMT_PSK;
Hai Shalomc1a21442022-02-04 13:43:00 -08002086 if (is_6ghz_freq(ssid->frequency) &&
2087 is_p2p_6ghz_capable(wpa_s->global->p2p)) {
2088 ssid->auth_alg |= WPA_AUTH_ALG_SAE;
2089 ssid->key_mgmt = WPA_KEY_MGMT_SAE;
2090 ssid->ieee80211w = MGMT_FRAME_PROTECTION_REQUIRED;
2091 ssid->sae_pwe = 1;
2092 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Use SAE auth_alg and key_mgmt");
2093 } else {
2094 p2p_set_6ghz_dev_capab(wpa_s->global->p2p, false);
2095 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002096 ssid->proto = WPA_PROTO_RSN;
2097 ssid->pairwise_cipher = WPA_CIPHER_CCMP;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002098 ssid->group_cipher = WPA_CIPHER_CCMP;
2099 if (params->freq > 56160) {
2100 /*
2101 * Enable GCMP instead of CCMP as pairwise_cipher and
2102 * group_cipher in 60 GHz.
2103 */
2104 ssid->pairwise_cipher = WPA_CIPHER_GCMP;
2105 ssid->group_cipher = WPA_CIPHER_GCMP;
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08002106 /* P2P GO in 60 GHz is always a PCP (PBSS) */
2107 ssid->pbss = 1;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002108 }
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08002109 if (os_strlen(params->passphrase) > 0) {
2110 ssid->passphrase = os_strdup(params->passphrase);
2111 if (ssid->passphrase == NULL) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07002112 wpa_msg_global(wpa_s, MSG_ERROR,
2113 "P2P: Failed to copy passphrase for GO");
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08002114 wpa_config_remove_network(wpa_s->conf, ssid->id);
2115 return;
2116 }
2117 } else
2118 ssid->passphrase = NULL;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002119 ssid->psk_set = params->psk_set;
2120 if (ssid->psk_set)
2121 os_memcpy(ssid->psk, params->psk, sizeof(ssid->psk));
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08002122 else if (ssid->passphrase)
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002123 wpa_config_update_psk(ssid);
Dmitry Shmidt9c175262016-03-03 10:20:07 -08002124 ssid->ap_max_inactivity = wpa_s->p2pdev->conf->p2p_go_max_inactivity;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002125
2126 wpa_s->ap_configured_cb = p2p_go_configured;
2127 wpa_s->ap_configured_cb_ctx = wpa_s;
2128 wpa_s->ap_configured_cb_data = wpa_s->go_params;
Dmitry Shmidt6dc03bd2014-05-16 10:40:13 -07002129 wpa_s->scan_req = NORMAL_SCAN_REQ;
Jouni Malinen75ecf522011-06-27 15:19:46 -07002130 wpa_s->connect_without_scan = ssid;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002131 wpa_s->reassociate = 1;
2132 wpa_s->disconnected = 0;
Dmitry Shmidtaa532512012-09-24 10:35:31 -07002133 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Request scan (that will be skipped) to "
2134 "start GO)");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002135 wpa_supplicant_req_scan(wpa_s, 0, 0);
2136}
2137
2138
2139static void wpas_p2p_clone_config(struct wpa_supplicant *dst,
2140 const struct wpa_supplicant *src)
2141{
2142 struct wpa_config *d;
2143 const struct wpa_config *s;
2144
2145 d = dst->conf;
2146 s = src->conf;
2147
Dmitry Shmidt849734c2016-05-27 09:59:01 -07002148#define C(n) \
2149do { \
2150 if (s->n && !d->n) \
2151 d->n = os_strdup(s->n); \
2152} while (0)
2153
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002154 C(device_name);
2155 C(manufacturer);
2156 C(model_name);
2157 C(model_number);
2158 C(serial_number);
2159 C(config_methods);
2160#undef C
2161
2162 os_memcpy(d->device_type, s->device_type, WPS_DEV_TYPE_LEN);
2163 os_memcpy(d->sec_device_type, s->sec_device_type,
2164 sizeof(d->sec_device_type));
2165 d->num_sec_device_types = s->num_sec_device_types;
2166
2167 d->p2p_group_idle = s->p2p_group_idle;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002168 d->p2p_go_freq_change_policy = s->p2p_go_freq_change_policy;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002169 d->p2p_intra_bss = s->p2p_intra_bss;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002170 d->persistent_reconnect = s->persistent_reconnect;
Dmitry Shmidt04949592012-07-19 12:16:46 -07002171 d->max_num_sta = s->max_num_sta;
2172 d->pbc_in_m1 = s->pbc_in_m1;
Dmitry Shmidt444d5672013-04-01 13:08:44 -07002173 d->ignore_old_scan_res = s->ignore_old_scan_res;
Dmitry Shmidt51b6ea82013-05-08 10:42:09 -07002174 d->beacon_int = s->beacon_int;
Dmitry Shmidt18463232014-01-24 12:29:41 -08002175 d->dtim_period = s->dtim_period;
Dmitry Shmidt7f656022015-02-25 14:36:37 -08002176 d->p2p_go_ctwindow = s->p2p_go_ctwindow;
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07002177 d->disassoc_low_ack = s->disassoc_low_ack;
Dmitry Shmidt96571392013-10-14 12:54:46 -07002178 d->disable_scan_offload = s->disable_scan_offload;
Dmitry Shmidt807291d2015-01-27 13:40:23 -08002179 d->passive_scan = s->passive_scan;
Hai Shalomc1a21442022-02-04 13:43:00 -08002180 d->pmf = s->pmf;
2181 d->p2p_6ghz_disable = s->p2p_6ghz_disable;
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08002182
Dmitry Shmidt849734c2016-05-27 09:59:01 -07002183 if (s->wps_nfc_dh_privkey && s->wps_nfc_dh_pubkey &&
2184 !d->wps_nfc_pw_from_config) {
2185 wpabuf_free(d->wps_nfc_dh_privkey);
2186 wpabuf_free(d->wps_nfc_dh_pubkey);
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08002187 d->wps_nfc_dh_privkey = wpabuf_dup(s->wps_nfc_dh_privkey);
2188 d->wps_nfc_dh_pubkey = wpabuf_dup(s->wps_nfc_dh_pubkey);
2189 }
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07002190 d->p2p_cli_probe = s->p2p_cli_probe;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002191 d->go_interworking = s->go_interworking;
2192 d->go_access_network_type = s->go_access_network_type;
2193 d->go_internet = s->go_internet;
2194 d->go_venue_group = s->go_venue_group;
2195 d->go_venue_type = s->go_venue_type;
Hai Shalom60840252021-02-19 19:02:11 -08002196 d->p2p_add_cli_chan = s->p2p_add_cli_chan;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002197}
2198
2199
Dmitry Shmidt34af3062013-07-11 10:46:32 -07002200static void wpas_p2p_get_group_ifname(struct wpa_supplicant *wpa_s,
2201 char *ifname, size_t len)
2202{
2203 char *ifname_ptr = wpa_s->ifname;
2204
2205 if (os_strncmp(wpa_s->ifname, P2P_MGMT_DEVICE_PREFIX,
2206 os_strlen(P2P_MGMT_DEVICE_PREFIX)) == 0) {
2207 ifname_ptr = os_strrchr(wpa_s->ifname, '-') + 1;
2208 }
2209
2210 os_snprintf(ifname, len, "p2p-%s-%d", ifname_ptr, wpa_s->p2p_group_idx);
2211 if (os_strlen(ifname) >= IFNAMSIZ &&
2212 os_strlen(wpa_s->ifname) < IFNAMSIZ) {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002213 int res;
2214
Dmitry Shmidt34af3062013-07-11 10:46:32 -07002215 /* Try to avoid going over the IFNAMSIZ length limit */
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002216 res = os_snprintf(ifname, len, "p2p-%d", wpa_s->p2p_group_idx);
2217 if (os_snprintf_error(len, res) && len)
2218 ifname[len - 1] = '\0';
Dmitry Shmidt34af3062013-07-11 10:46:32 -07002219 }
2220}
2221
2222
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002223static int wpas_p2p_add_group_interface(struct wpa_supplicant *wpa_s,
2224 enum wpa_driver_if_type type)
2225{
2226 char ifname[120], force_ifname[120];
2227
2228 if (wpa_s->pending_interface_name[0]) {
2229 wpa_printf(MSG_DEBUG, "P2P: Pending virtual interface exists "
2230 "- skip creation of a new one");
2231 if (is_zero_ether_addr(wpa_s->pending_interface_addr)) {
2232 wpa_printf(MSG_DEBUG, "P2P: Pending virtual address "
2233 "unknown?! ifname='%s'",
2234 wpa_s->pending_interface_name);
2235 return -1;
2236 }
2237 return 0;
2238 }
2239
Dmitry Shmidt34af3062013-07-11 10:46:32 -07002240 wpas_p2p_get_group_ifname(wpa_s, ifname, sizeof(ifname));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002241 force_ifname[0] = '\0';
2242
2243 wpa_printf(MSG_DEBUG, "P2P: Create a new interface %s for the group",
2244 ifname);
2245 wpa_s->p2p_group_idx++;
2246
2247 wpa_s->pending_interface_type = type;
2248 if (wpa_drv_if_add(wpa_s, type, ifname, NULL, NULL, force_ifname,
2249 wpa_s->pending_interface_addr, NULL) < 0) {
2250 wpa_printf(MSG_ERROR, "P2P: Failed to create new group "
2251 "interface");
2252 return -1;
2253 }
2254
Jimmy Chen36c21992018-11-29 16:46:43 +08002255 if (wpa_s->conf->p2p_interface_random_mac_addr) {
Hai Shalom74f70d42019-02-11 14:42:39 -08002256 random_mac_addr(wpa_s->pending_interface_addr);
2257 wpa_printf(MSG_DEBUG, "P2P: Generate random MAC address " MACSTR
2258 " for the group",
2259 MAC2STR(wpa_s->pending_interface_addr));
Jimmy Chen36c21992018-11-29 16:46:43 +08002260 }
2261
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002262 if (force_ifname[0]) {
2263 wpa_printf(MSG_DEBUG, "P2P: Driver forced interface name %s",
2264 force_ifname);
2265 os_strlcpy(wpa_s->pending_interface_name, force_ifname,
2266 sizeof(wpa_s->pending_interface_name));
2267 } else
2268 os_strlcpy(wpa_s->pending_interface_name, ifname,
2269 sizeof(wpa_s->pending_interface_name));
2270 wpa_printf(MSG_DEBUG, "P2P: Created pending virtual interface %s addr "
2271 MACSTR, wpa_s->pending_interface_name,
2272 MAC2STR(wpa_s->pending_interface_addr));
2273
2274 return 0;
2275}
2276
2277
2278static void wpas_p2p_remove_pending_group_interface(
2279 struct wpa_supplicant *wpa_s)
2280{
2281 if (!wpa_s->pending_interface_name[0] ||
2282 is_zero_ether_addr(wpa_s->pending_interface_addr))
2283 return; /* No pending virtual interface */
2284
2285 wpa_printf(MSG_DEBUG, "P2P: Removing pending group interface %s",
2286 wpa_s->pending_interface_name);
2287 wpa_drv_if_remove(wpa_s, wpa_s->pending_interface_type,
2288 wpa_s->pending_interface_name);
2289 os_memset(wpa_s->pending_interface_addr, 0, ETH_ALEN);
2290 wpa_s->pending_interface_name[0] = '\0';
Dmitry Shmidt216983b2015-02-06 10:50:36 -08002291 wpa_s->global->pending_group_iface_for_p2ps = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002292}
2293
2294
2295static struct wpa_supplicant *
2296wpas_p2p_init_group_interface(struct wpa_supplicant *wpa_s, int go)
2297{
2298 struct wpa_interface iface;
2299 struct wpa_supplicant *group_wpa_s;
2300
2301 if (!wpa_s->pending_interface_name[0]) {
2302 wpa_printf(MSG_ERROR, "P2P: No pending group interface");
2303 if (!wpas_p2p_create_iface(wpa_s))
2304 return NULL;
2305 /*
2306 * Something has forced us to remove the pending interface; try
2307 * to create a new one and hope for the best that we will get
2308 * the same local address.
2309 */
2310 if (wpas_p2p_add_group_interface(wpa_s, go ? WPA_IF_P2P_GO :
2311 WPA_IF_P2P_CLIENT) < 0)
2312 return NULL;
2313 }
2314
2315 os_memset(&iface, 0, sizeof(iface));
2316 iface.ifname = wpa_s->pending_interface_name;
2317 iface.driver = wpa_s->driver->name;
Dmitry Shmidt34af3062013-07-11 10:46:32 -07002318 if (wpa_s->conf->ctrl_interface == NULL &&
2319 wpa_s->parent != wpa_s &&
2320 wpa_s->p2p_mgmt &&
2321 (wpa_s->drv_flags & WPA_DRIVER_FLAGS_DEDICATED_P2P_DEVICE))
2322 iface.ctrl_interface = wpa_s->parent->conf->ctrl_interface;
2323 else
2324 iface.ctrl_interface = wpa_s->conf->ctrl_interface;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002325 iface.driver_param = wpa_s->conf->driver_param;
Dmitry Shmidt203eadb2015-03-05 14:16:04 -08002326 group_wpa_s = wpa_supplicant_add_iface(wpa_s->global, &iface, wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002327 if (group_wpa_s == NULL) {
2328 wpa_printf(MSG_ERROR, "P2P: Failed to create new "
2329 "wpa_supplicant interface");
2330 return NULL;
2331 }
2332 wpa_s->pending_interface_name[0] = '\0';
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002333 group_wpa_s->p2p_group_interface = go ? P2P_GROUP_INTERFACE_GO :
2334 P2P_GROUP_INTERFACE_CLIENT;
2335 wpa_s->global->p2p_group_formation = group_wpa_s;
Dmitry Shmidt216983b2015-02-06 10:50:36 -08002336 wpa_s->global->pending_group_iface_for_p2ps = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002337
2338 wpas_p2p_clone_config(group_wpa_s, wpa_s);
2339
Jimmy Chen36c21992018-11-29 16:46:43 +08002340 if (wpa_s->conf->p2p_interface_random_mac_addr) {
Hai Shalom74f70d42019-02-11 14:42:39 -08002341 if (wpa_drv_set_mac_addr(group_wpa_s,
2342 wpa_s->pending_interface_addr) < 0) {
Jimmy Chen36c21992018-11-29 16:46:43 +08002343 wpa_msg(group_wpa_s, MSG_INFO,
2344 "Failed to set random MAC address");
Hai Shalom74f70d42019-02-11 14:42:39 -08002345 wpa_supplicant_remove_iface(wpa_s->global, group_wpa_s,
2346 0);
Jimmy Chen36c21992018-11-29 16:46:43 +08002347 return NULL;
2348 }
2349
2350 if (wpa_supplicant_update_mac_addr(group_wpa_s) < 0) {
2351 wpa_msg(group_wpa_s, MSG_INFO,
2352 "Could not update MAC address information");
Hai Shalom74f70d42019-02-11 14:42:39 -08002353 wpa_supplicant_remove_iface(wpa_s->global, group_wpa_s,
2354 0);
Jimmy Chen36c21992018-11-29 16:46:43 +08002355 return NULL;
2356 }
2357
Hai Shalom74f70d42019-02-11 14:42:39 -08002358 wpa_printf(MSG_DEBUG, "P2P: Using random MAC address " MACSTR
2359 " for the group",
2360 MAC2STR(wpa_s->pending_interface_addr));
Jimmy Chen36c21992018-11-29 16:46:43 +08002361 }
2362
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002363 return group_wpa_s;
2364}
2365
2366
2367static void wpas_p2p_group_formation_timeout(void *eloop_ctx,
2368 void *timeout_ctx)
2369{
2370 struct wpa_supplicant *wpa_s = eloop_ctx;
2371 wpa_printf(MSG_DEBUG, "P2P: Group Formation timed out");
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07002372 wpas_p2p_group_formation_failed(wpa_s, 0);
Dmitry Shmidt34af3062013-07-11 10:46:32 -07002373}
2374
2375
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07002376static void wpas_p2p_group_formation_failed(struct wpa_supplicant *wpa_s,
2377 int already_deleted)
Dmitry Shmidt34af3062013-07-11 10:46:32 -07002378{
2379 eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
Dmitry Shmidt9c175262016-03-03 10:20:07 -08002380 wpa_s->p2pdev, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002381 if (wpa_s->global->p2p)
2382 p2p_group_formation_failed(wpa_s->global->p2p);
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07002383 wpas_group_formation_completed(wpa_s, 0, already_deleted);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002384}
2385
2386
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002387static void wpas_p2p_grpform_fail_after_wps(struct wpa_supplicant *wpa_s)
2388{
2389 wpa_printf(MSG_DEBUG, "P2P: Reject group formation due to WPS provisioning failure");
2390 eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
Dmitry Shmidt9c175262016-03-03 10:20:07 -08002391 wpa_s->p2pdev, NULL);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002392 eloop_register_timeout(0, 0, wpas_p2p_group_formation_timeout,
Dmitry Shmidt9c175262016-03-03 10:20:07 -08002393 wpa_s->p2pdev, NULL);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002394 wpa_s->global->p2p_fail_on_wps_complete = 0;
2395}
2396
2397
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07002398void wpas_p2p_ap_setup_failed(struct wpa_supplicant *wpa_s)
2399{
2400 if (wpa_s->global->p2p_group_formation != wpa_s)
2401 return;
2402 /* Speed up group formation timeout since this cannot succeed */
2403 eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
Dmitry Shmidt9c175262016-03-03 10:20:07 -08002404 wpa_s->p2pdev, NULL);
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07002405 eloop_register_timeout(0, 0, wpas_p2p_group_formation_timeout,
Dmitry Shmidt9c175262016-03-03 10:20:07 -08002406 wpa_s->p2pdev, NULL);
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07002407}
2408
2409
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002410static void wpas_go_neg_completed(void *ctx, struct p2p_go_neg_results *res)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002411{
2412 struct wpa_supplicant *wpa_s = ctx;
Dmitry Shmidt9c175262016-03-03 10:20:07 -08002413 struct wpa_supplicant *group_wpa_s;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002414
2415 if (wpa_s->off_channel_freq || wpa_s->roc_waiting_drv_freq) {
2416 wpa_drv_cancel_remain_on_channel(wpa_s);
2417 wpa_s->off_channel_freq = 0;
2418 wpa_s->roc_waiting_drv_freq = 0;
2419 }
2420
2421 if (res->status) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07002422 wpa_msg_global(wpa_s, MSG_INFO,
2423 P2P_EVENT_GO_NEG_FAILURE "status=%d",
2424 res->status);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002425 wpas_notify_p2p_go_neg_completed(wpa_s, res);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002426 wpas_p2p_remove_pending_group_interface(wpa_s);
2427 return;
2428 }
2429
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002430 if (!res->role_go) {
2431 /* Inform driver of the operating channel of GO. */
2432 wpa_drv_set_prob_oper_freq(wpa_s, res->freq);
2433 }
2434
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002435 if (wpa_s->p2p_go_ht40)
2436 res->ht40 = 1;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07002437 if (wpa_s->p2p_go_vht)
2438 res->vht = 1;
Hai Shalomc3565922019-10-28 11:58:20 -07002439 if (wpa_s->p2p_go_he)
2440 res->he = 1;
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08002441 if (wpa_s->p2p_go_edmg)
2442 res->edmg = 1;
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08002443 res->max_oper_chwidth = wpa_s->p2p_go_max_oper_chwidth;
2444 res->vht_center_freq2 = wpa_s->p2p_go_vht_center_freq2;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002445
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07002446 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_GO_NEG_SUCCESS "role=%s "
2447 "freq=%d ht40=%d peer_dev=" MACSTR " peer_iface=" MACSTR
2448 " wps_method=%s",
2449 res->role_go ? "GO" : "client", res->freq, res->ht40,
2450 MAC2STR(res->peer_device_addr),
2451 MAC2STR(res->peer_interface_addr),
2452 p2p_wps_method_text(res->wps_method));
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002453 wpas_notify_p2p_go_neg_completed(wpa_s, res);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002454
Dmitry Shmidt04949592012-07-19 12:16:46 -07002455 if (res->role_go && wpa_s->p2p_persistent_id >= 0) {
2456 struct wpa_ssid *ssid;
2457 ssid = wpa_config_get_network(wpa_s->conf,
2458 wpa_s->p2p_persistent_id);
2459 if (ssid && ssid->disabled == 2 &&
2460 ssid->mode == WPAS_MODE_P2P_GO && ssid->passphrase) {
2461 size_t len = os_strlen(ssid->passphrase);
2462 wpa_printf(MSG_DEBUG, "P2P: Override passphrase based "
2463 "on requested persistent group");
2464 os_memcpy(res->passphrase, ssid->passphrase, len);
2465 res->passphrase[len] = '\0';
2466 }
2467 }
2468
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002469 if (wpa_s->create_p2p_iface) {
Dmitry Shmidt9c175262016-03-03 10:20:07 -08002470 group_wpa_s =
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002471 wpas_p2p_init_group_interface(wpa_s, res->role_go);
2472 if (group_wpa_s == NULL) {
2473 wpas_p2p_remove_pending_group_interface(wpa_s);
Dmitry Shmidtd11f0192014-03-24 12:09:47 -07002474 eloop_cancel_timeout(wpas_p2p_long_listen_timeout,
2475 wpa_s, NULL);
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07002476 wpas_p2p_group_formation_failed(wpa_s, 1);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002477 return;
2478 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002479 os_memset(wpa_s->pending_interface_addr, 0, ETH_ALEN);
2480 wpa_s->pending_interface_name[0] = '\0';
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002481 } else {
Dmitry Shmidt9c175262016-03-03 10:20:07 -08002482 group_wpa_s = wpa_s->parent;
2483 wpa_s->global->p2p_group_formation = group_wpa_s;
2484 if (group_wpa_s != wpa_s)
Dmitry Shmidt849734c2016-05-27 09:59:01 -07002485 wpas_p2p_clone_config(group_wpa_s, wpa_s);
Dmitry Shmidt9c175262016-03-03 10:20:07 -08002486 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002487
Dmitry Shmidt9c175262016-03-03 10:20:07 -08002488 group_wpa_s->p2p_in_provisioning = 1;
2489 group_wpa_s->p2pdev = wpa_s;
2490 if (group_wpa_s != wpa_s) {
2491 os_memcpy(group_wpa_s->p2p_pin, wpa_s->p2p_pin,
2492 sizeof(group_wpa_s->p2p_pin));
2493 group_wpa_s->p2p_wps_method = wpa_s->p2p_wps_method;
2494 }
2495 if (res->role_go) {
2496 wpas_start_wps_go(group_wpa_s, res, 1);
2497 } else {
2498 os_get_reltime(&group_wpa_s->scan_min_time);
2499 wpas_start_wps_enrollee(group_wpa_s, res);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002500 }
2501
Hai Shalomfdcde762020-04-02 11:19:20 -07002502 wpa_s->global->p2p_long_listen = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002503 eloop_cancel_timeout(wpas_p2p_long_listen_timeout, wpa_s, NULL);
2504
2505 eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s, NULL);
2506 eloop_register_timeout(15 + res->peer_config_timeout / 100,
2507 (res->peer_config_timeout % 100) * 10000,
2508 wpas_p2p_group_formation_timeout, wpa_s, NULL);
2509}
2510
2511
Dmitry Shmidt7a53dbb2015-06-11 13:13:53 -07002512static void wpas_go_neg_req_rx(void *ctx, const u8 *src, u16 dev_passwd_id,
2513 u8 go_intent)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002514{
2515 struct wpa_supplicant *wpa_s = ctx;
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07002516 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_GO_NEG_REQUEST MACSTR
Dmitry Shmidt7a53dbb2015-06-11 13:13:53 -07002517 " dev_passwd_id=%u go_intent=%u", MAC2STR(src),
2518 dev_passwd_id, go_intent);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002519
Dmitry Shmidt7a53dbb2015-06-11 13:13:53 -07002520 wpas_notify_p2p_go_neg_req(wpa_s, src, dev_passwd_id, go_intent);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002521}
2522
2523
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002524static void wpas_dev_found(void *ctx, const u8 *addr,
2525 const struct p2p_peer_info *info,
2526 int new_device)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002527{
Roshan Piusfd2fd662017-01-23 13:41:57 -08002528 u8 *wfd_dev_info = NULL;
2529 u8 wfd_dev_info_len = 0;
Jimmy Chen0133fc12021-03-04 13:56:11 +08002530 u8 *wfd_r2_dev_info = NULL;
Jimmy Chen57e19f52021-03-04 14:19:52 +08002531 u8 wfd_r2_dev_info_len = 0;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002532#ifndef CONFIG_NO_STDOUT_DEBUG
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002533 struct wpa_supplicant *wpa_s = ctx;
2534 char devtype[WPS_DEV_TYPE_BUFSIZE];
Dmitry Shmidt04f534e2013-12-09 15:50:16 -08002535 char *wfd_dev_info_hex = NULL;
Jimmy Chen0133fc12021-03-04 13:56:11 +08002536 char *wfd_r2_dev_info_hex = NULL;
Dmitry Shmidt04f534e2013-12-09 15:50:16 -08002537
Irfan Sheriff8367dc92012-09-09 17:08:19 -07002538#ifdef CONFIG_WIFI_DISPLAY
Dmitry Shmidt04f534e2013-12-09 15:50:16 -08002539 wfd_dev_info_hex = wifi_display_subelem_hex(info->wfd_subelems,
2540 WFD_SUBELEM_DEVICE_INFO);
Roshan Piusfd2fd662017-01-23 13:41:57 -08002541 if (wfd_dev_info_hex) {
2542 wfd_dev_info_len = strlen(wfd_dev_info_hex) / 2;
2543 wfd_dev_info = os_zalloc(wfd_dev_info_len);
2544 // Only used for notification, so not handling error.
2545 hexstr2bin(wfd_dev_info_hex, wfd_dev_info, wfd_dev_info_len);
2546 }
Jimmy Chen0133fc12021-03-04 13:56:11 +08002547
2548 wfd_r2_dev_info_hex = wifi_display_subelem_hex(info->wfd_subelems,
2549 WFD_SUBELEM_R2_DEVICE_INFO);
2550 if (wfd_r2_dev_info_hex) {
2551 wfd_r2_dev_info_len = strlen(wfd_r2_dev_info_hex) / 2;
2552 wfd_r2_dev_info = os_zalloc(wfd_r2_dev_info_len);
2553 // Only used for notification, so not handling error.
2554 hexstr2bin(wfd_r2_dev_info_hex, wfd_r2_dev_info, wfd_r2_dev_info_len);
2555 }
Irfan Sheriff8367dc92012-09-09 17:08:19 -07002556#endif /* CONFIG_WIFI_DISPLAY */
Dmitry Shmidt04f534e2013-12-09 15:50:16 -08002557
Dmitry Shmidt216983b2015-02-06 10:50:36 -08002558 if (info->p2ps_instance) {
2559 char str[256];
2560 const u8 *buf = wpabuf_head(info->p2ps_instance);
2561 size_t len = wpabuf_len(info->p2ps_instance);
2562
2563 while (len) {
2564 u32 id;
2565 u16 methods;
2566 u8 str_len;
2567
2568 if (len < 4 + 2 + 1)
2569 break;
2570 id = WPA_GET_LE32(buf);
2571 buf += sizeof(u32);
2572 methods = WPA_GET_BE16(buf);
2573 buf += sizeof(u16);
2574 str_len = *buf++;
2575 if (str_len > len - 4 - 2 - 1)
2576 break;
2577 os_memcpy(str, buf, str_len);
2578 str[str_len] = '\0';
2579 buf += str_len;
2580 len -= str_len + sizeof(u32) + sizeof(u16) + sizeof(u8);
2581
2582 wpa_msg_global(wpa_s, MSG_INFO,
2583 P2P_EVENT_DEVICE_FOUND MACSTR
2584 " p2p_dev_addr=" MACSTR
2585 " pri_dev_type=%s name='%s'"
2586 " config_methods=0x%x"
2587 " dev_capab=0x%x"
2588 " group_capab=0x%x"
2589 " adv_id=%x asp_svc=%s%s",
2590 MAC2STR(addr),
2591 MAC2STR(info->p2p_device_addr),
2592 wps_dev_type_bin2str(
2593 info->pri_dev_type,
2594 devtype, sizeof(devtype)),
2595 info->device_name, methods,
2596 info->dev_capab, info->group_capab,
2597 id, str,
2598 info->vendor_elems ?
2599 " vendor_elems=1" : "");
2600 }
2601 goto done;
2602 }
2603
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07002604 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_DEVICE_FOUND MACSTR
2605 " p2p_dev_addr=" MACSTR
2606 " pri_dev_type=%s name='%s' config_methods=0x%x "
Jimmy Chen0133fc12021-03-04 13:56:11 +08002607 "dev_capab=0x%x group_capab=0x%x%s%s%s%s%s new=%d",
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07002608 MAC2STR(addr), MAC2STR(info->p2p_device_addr),
2609 wps_dev_type_bin2str(info->pri_dev_type, devtype,
2610 sizeof(devtype)),
2611 info->device_name, info->config_methods,
2612 info->dev_capab, info->group_capab,
Dmitry Shmidt04f534e2013-12-09 15:50:16 -08002613 wfd_dev_info_hex ? " wfd_dev_info=0x" : "",
Dmitry Shmidt2e67f062014-07-16 09:55:28 -07002614 wfd_dev_info_hex ? wfd_dev_info_hex : "",
Jimmy Chen0133fc12021-03-04 13:56:11 +08002615 wfd_r2_dev_info_hex ? " wfd_r2_dev_info=0x" : "",
2616 wfd_r2_dev_info_hex ? wfd_r2_dev_info_hex : "",
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002617 info->vendor_elems ? " vendor_elems=1" : "",
2618 new_device);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002619
Dmitry Shmidt216983b2015-02-06 10:50:36 -08002620done:
Dmitry Shmidt04f534e2013-12-09 15:50:16 -08002621 os_free(wfd_dev_info_hex);
Jimmy Chen0133fc12021-03-04 13:56:11 +08002622 os_free(wfd_r2_dev_info_hex);
Dmitry Shmidt97672262014-02-03 13:02:54 -08002623#endif /* CONFIG_NO_STDOUT_DEBUG */
Dmitry Shmidt04f534e2013-12-09 15:50:16 -08002624
Roshan Piusfd2fd662017-01-23 13:41:57 -08002625 wpas_notify_p2p_device_found(ctx, addr, info, wfd_dev_info,
Jimmy Chen0133fc12021-03-04 13:56:11 +08002626 wfd_dev_info_len, wfd_r2_dev_info,
2627 wfd_r2_dev_info_len, new_device);
Roshan Piusfd2fd662017-01-23 13:41:57 -08002628 os_free(wfd_dev_info);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002629}
2630
2631
2632static void wpas_dev_lost(void *ctx, const u8 *dev_addr)
2633{
2634 struct wpa_supplicant *wpa_s = ctx;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002635
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07002636 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_DEVICE_LOST
2637 "p2p_dev_addr=" MACSTR, MAC2STR(dev_addr));
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002638
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002639 wpas_notify_p2p_device_lost(wpa_s, dev_addr);
2640}
2641
2642
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07002643static void wpas_find_stopped(void *ctx)
2644{
2645 struct wpa_supplicant *wpa_s = ctx;
Dmitry Shmidt9c175262016-03-03 10:20:07 -08002646
2647 if (wpa_s->p2p_scan_work && wpas_abort_ongoing_scan(wpa_s) < 0)
2648 wpa_printf(MSG_DEBUG, "P2P: Abort ongoing scan failed");
2649
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07002650 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_FIND_STOPPED);
Dmitry Shmidt8bd70b72015-05-26 16:02:19 -07002651 wpas_notify_p2p_find_stopped(wpa_s);
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07002652}
2653
2654
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002655struct wpas_p2p_listen_work {
2656 unsigned int freq;
2657 unsigned int duration;
2658 struct wpabuf *probe_resp_ie;
2659};
2660
2661
2662static void wpas_p2p_listen_work_free(struct wpas_p2p_listen_work *lwork)
2663{
2664 if (lwork == NULL)
2665 return;
2666 wpabuf_free(lwork->probe_resp_ie);
2667 os_free(lwork);
2668}
2669
2670
2671static void wpas_p2p_listen_work_done(struct wpa_supplicant *wpa_s)
2672{
2673 struct wpas_p2p_listen_work *lwork;
2674
2675 if (!wpa_s->p2p_listen_work)
2676 return;
2677
2678 lwork = wpa_s->p2p_listen_work->ctx;
2679 wpas_p2p_listen_work_free(lwork);
2680 radio_work_done(wpa_s->p2p_listen_work);
2681 wpa_s->p2p_listen_work = NULL;
2682}
2683
2684
2685static void wpas_start_listen_cb(struct wpa_radio_work *work, int deinit)
2686{
2687 struct wpa_supplicant *wpa_s = work->wpa_s;
2688 struct wpas_p2p_listen_work *lwork = work->ctx;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002689 unsigned int duration;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002690
2691 if (deinit) {
Dmitry Shmidtbd14a572014-02-18 10:33:49 -08002692 if (work->started) {
2693 wpa_s->p2p_listen_work = NULL;
2694 wpas_stop_listen(wpa_s);
2695 }
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002696 wpas_p2p_listen_work_free(lwork);
2697 return;
2698 }
2699
2700 wpa_s->p2p_listen_work = work;
2701
2702 wpa_drv_set_ap_wps_ie(wpa_s, NULL, lwork->probe_resp_ie, NULL);
2703
2704 if (wpa_drv_probe_req_report(wpa_s, 1) < 0) {
2705 wpa_printf(MSG_DEBUG, "P2P: Failed to request the driver to "
2706 "report received Probe Request frames");
2707 wpas_p2p_listen_work_done(wpa_s);
2708 return;
2709 }
2710
2711 wpa_s->pending_listen_freq = lwork->freq;
2712 wpa_s->pending_listen_duration = lwork->duration;
2713
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002714 duration = lwork->duration;
2715#ifdef CONFIG_TESTING_OPTIONS
2716 if (wpa_s->extra_roc_dur) {
2717 wpa_printf(MSG_DEBUG, "TESTING: Increase ROC duration %u -> %u",
2718 duration, duration + wpa_s->extra_roc_dur);
2719 duration += wpa_s->extra_roc_dur;
2720 }
2721#endif /* CONFIG_TESTING_OPTIONS */
2722
2723 if (wpa_drv_remain_on_channel(wpa_s, lwork->freq, duration) < 0) {
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002724 wpa_printf(MSG_DEBUG, "P2P: Failed to request the driver "
2725 "to remain on channel (%u MHz) for Listen "
2726 "state", lwork->freq);
2727 wpas_p2p_listen_work_done(wpa_s);
2728 wpa_s->pending_listen_freq = 0;
2729 return;
2730 }
2731 wpa_s->off_channel_freq = 0;
2732 wpa_s->roc_waiting_drv_freq = lwork->freq;
2733}
2734
2735
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002736static int wpas_start_listen(void *ctx, unsigned int freq,
2737 unsigned int duration,
2738 const struct wpabuf *probe_resp_ie)
2739{
2740 struct wpa_supplicant *wpa_s = ctx;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002741 struct wpas_p2p_listen_work *lwork;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002742
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002743 if (wpa_s->p2p_listen_work) {
2744 wpa_printf(MSG_DEBUG, "P2P: Reject start_listen since p2p_listen_work already exists");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002745 return -1;
2746 }
2747
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002748 lwork = os_zalloc(sizeof(*lwork));
2749 if (lwork == NULL)
2750 return -1;
2751 lwork->freq = freq;
2752 lwork->duration = duration;
2753 if (probe_resp_ie) {
2754 lwork->probe_resp_ie = wpabuf_dup(probe_resp_ie);
2755 if (lwork->probe_resp_ie == NULL) {
2756 wpas_p2p_listen_work_free(lwork);
2757 return -1;
2758 }
2759 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002760
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002761 if (radio_add_work(wpa_s, freq, "p2p-listen", 0, wpas_start_listen_cb,
2762 lwork) < 0) {
2763 wpas_p2p_listen_work_free(lwork);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002764 return -1;
2765 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002766
2767 return 0;
2768}
2769
2770
2771static void wpas_stop_listen(void *ctx)
2772{
2773 struct wpa_supplicant *wpa_s = ctx;
2774 if (wpa_s->off_channel_freq || wpa_s->roc_waiting_drv_freq) {
2775 wpa_drv_cancel_remain_on_channel(wpa_s);
2776 wpa_s->off_channel_freq = 0;
2777 wpa_s->roc_waiting_drv_freq = 0;
2778 }
2779 wpa_drv_set_ap_wps_ie(wpa_s, NULL, NULL, NULL);
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07002780
2781 /*
2782 * Don't cancel Probe Request RX reporting for a connected P2P Client
2783 * handling Probe Request frames.
2784 */
2785 if (!wpa_s->p2p_cli_probe)
2786 wpa_drv_probe_req_report(wpa_s, 0);
2787
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002788 wpas_p2p_listen_work_done(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002789}
2790
2791
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07002792static int wpas_send_probe_resp(void *ctx, const struct wpabuf *buf,
2793 unsigned int freq)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002794{
2795 struct wpa_supplicant *wpa_s = ctx;
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07002796 return wpa_drv_send_mlme(wpa_s, wpabuf_head(buf), wpabuf_len(buf), 1,
Hai Shalomfdcde762020-04-02 11:19:20 -07002797 freq, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002798}
2799
2800
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002801static void wpas_prov_disc_local_display(struct wpa_supplicant *wpa_s,
2802 const u8 *peer, const char *params,
2803 unsigned int generated_pin)
2804{
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07002805 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_SHOW_PIN MACSTR
2806 " %08d%s", MAC2STR(peer), generated_pin, params);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002807}
2808
2809
2810static void wpas_prov_disc_local_keypad(struct wpa_supplicant *wpa_s,
2811 const u8 *peer, const char *params)
2812{
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07002813 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_ENTER_PIN MACSTR
2814 "%s", MAC2STR(peer), params);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002815}
2816
2817
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002818static void wpas_prov_disc_req(void *ctx, const u8 *peer, u16 config_methods,
2819 const u8 *dev_addr, const u8 *pri_dev_type,
2820 const char *dev_name, u16 supp_config_methods,
2821 u8 dev_capab, u8 group_capab, const u8 *group_id,
2822 size_t group_id_len)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002823{
2824 struct wpa_supplicant *wpa_s = ctx;
2825 char devtype[WPS_DEV_TYPE_BUFSIZE];
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002826 char params[300];
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002827 u8 empty_dev_type[8];
2828 unsigned int generated_pin = 0;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002829 struct wpa_supplicant *group = NULL;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002830 int res;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002831
2832 if (group_id) {
2833 for (group = wpa_s->global->ifaces; group; group = group->next)
2834 {
2835 struct wpa_ssid *s = group->current_ssid;
2836 if (s != NULL &&
2837 s->mode == WPAS_MODE_P2P_GO &&
2838 group_id_len - ETH_ALEN == s->ssid_len &&
2839 os_memcmp(group_id + ETH_ALEN, s->ssid,
2840 s->ssid_len) == 0)
2841 break;
2842 }
2843 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002844
2845 if (pri_dev_type == NULL) {
2846 os_memset(empty_dev_type, 0, sizeof(empty_dev_type));
2847 pri_dev_type = empty_dev_type;
2848 }
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002849 res = os_snprintf(params, sizeof(params), " p2p_dev_addr=" MACSTR
2850 " pri_dev_type=%s name='%s' config_methods=0x%x "
2851 "dev_capab=0x%x group_capab=0x%x%s%s",
2852 MAC2STR(dev_addr),
2853 wps_dev_type_bin2str(pri_dev_type, devtype,
2854 sizeof(devtype)),
2855 dev_name, supp_config_methods, dev_capab, group_capab,
2856 group ? " group=" : "",
2857 group ? group->ifname : "");
2858 if (os_snprintf_error(sizeof(params), res))
2859 wpa_printf(MSG_DEBUG, "P2P: PD Request event truncated");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002860 params[sizeof(params) - 1] = '\0';
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08002861
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002862 if (config_methods & WPS_CONFIG_DISPLAY) {
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08002863 if (wps_generate_pin(&generated_pin) < 0) {
2864 wpa_printf(MSG_DEBUG, "P2P: Could not generate PIN");
2865 wpas_notify_p2p_provision_discovery(
2866 wpa_s, peer, 0 /* response */,
2867 P2P_PROV_DISC_INFO_UNAVAILABLE, 0, 0);
2868 return;
2869 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002870 wpas_prov_disc_local_display(wpa_s, peer, params,
2871 generated_pin);
2872 } else if (config_methods & WPS_CONFIG_KEYPAD)
2873 wpas_prov_disc_local_keypad(wpa_s, peer, params);
2874 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_REQ
2876 MACSTR "%s", MAC2STR(peer), params);
Jouni Malinen75ecf522011-06-27 15:19:46 -07002877
2878 wpas_notify_p2p_provision_discovery(wpa_s, peer, 1 /* request */,
2879 P2P_PROV_DISC_SUCCESS,
2880 config_methods, generated_pin);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002881}
2882
2883
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002884static void wpas_prov_disc_resp(void *ctx, const u8 *peer, u16 config_methods)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002885{
2886 struct wpa_supplicant *wpa_s = ctx;
2887 unsigned int generated_pin = 0;
Dmitry Shmidt04949592012-07-19 12:16:46 -07002888 char params[20];
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002889
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08002890 if (wpa_s->pending_pd_before_join &&
2891 (os_memcmp(peer, wpa_s->pending_join_dev_addr, ETH_ALEN) == 0 ||
2892 os_memcmp(peer, wpa_s->pending_join_iface_addr, ETH_ALEN) == 0)) {
2893 wpa_s->pending_pd_before_join = 0;
2894 wpa_printf(MSG_DEBUG, "P2P: Starting pending "
2895 "join-existing-group operation");
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08002896 wpas_p2p_join_start(wpa_s, 0, NULL, 0);
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08002897 return;
2898 }
2899
Dmitry Shmidt04949592012-07-19 12:16:46 -07002900 if (wpa_s->pending_pd_use == AUTO_PD_JOIN ||
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002901 wpa_s->pending_pd_use == AUTO_PD_GO_NEG) {
2902 int res;
2903
2904 res = os_snprintf(params, sizeof(params), " peer_go=%d",
2905 wpa_s->pending_pd_use == AUTO_PD_JOIN);
2906 if (os_snprintf_error(sizeof(params), res))
2907 params[sizeof(params) - 1] = '\0';
2908 } else
Dmitry Shmidt04949592012-07-19 12:16:46 -07002909 params[0] = '\0';
2910
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002911 if (config_methods & WPS_CONFIG_DISPLAY)
Dmitry Shmidt04949592012-07-19 12:16:46 -07002912 wpas_prov_disc_local_keypad(wpa_s, peer, params);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002913 else if (config_methods & WPS_CONFIG_KEYPAD) {
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08002914 if (wps_generate_pin(&generated_pin) < 0) {
2915 wpa_printf(MSG_DEBUG, "P2P: Could not generate PIN");
2916 wpas_notify_p2p_provision_discovery(
2917 wpa_s, peer, 0 /* response */,
2918 P2P_PROV_DISC_INFO_UNAVAILABLE, 0, 0);
2919 return;
2920 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07002921 wpas_prov_disc_local_display(wpa_s, peer, params,
2922 generated_pin);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002923 } else if (config_methods & WPS_CONFIG_PUSHBUTTON)
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07002924 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_PBC_RESP
2925 MACSTR "%s", MAC2STR(peer), params);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002926
Jouni Malinen75ecf522011-06-27 15:19:46 -07002927 wpas_notify_p2p_provision_discovery(wpa_s, peer, 0 /* response */,
2928 P2P_PROV_DISC_SUCCESS,
2929 config_methods, generated_pin);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002930}
2931
2932
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002933static void wpas_prov_disc_fail(void *ctx, const u8 *peer,
Dmitry Shmidt216983b2015-02-06 10:50:36 -08002934 enum p2p_prov_disc_status status,
2935 u32 adv_id, const u8 *adv_mac,
2936 const char *deferred_session_resp)
Jouni Malinen75ecf522011-06-27 15:19:46 -07002937{
2938 struct wpa_supplicant *wpa_s = ctx;
2939
Dmitry Shmidt04949592012-07-19 12:16:46 -07002940 if (wpa_s->p2p_fallback_to_go_neg) {
2941 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: PD for p2p_connect-auto "
2942 "failed - fall back to GO Negotiation");
Dmitry Shmidt9c175262016-03-03 10:20:07 -08002943 wpa_msg_global(wpa_s->p2pdev, MSG_INFO,
Dmitry Shmidt7f656022015-02-25 14:36:37 -08002944 P2P_EVENT_FALLBACK_TO_GO_NEG
2945 "reason=PD-failed");
Dmitry Shmidt04949592012-07-19 12:16:46 -07002946 wpas_p2p_fallback_to_go_neg(wpa_s, 0);
2947 return;
2948 }
2949
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002950 if (status == P2P_PROV_DISC_TIMEOUT_JOIN) {
Dmitry Shmidt2b380482012-09-13 10:52:13 -07002951 wpa_s->pending_pd_before_join = 0;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002952 wpa_printf(MSG_DEBUG, "P2P: Starting pending "
2953 "join-existing-group operation (no ACK for PD "
2954 "Req attempts)");
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08002955 wpas_p2p_join_start(wpa_s, 0, NULL, 0);
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002956 return;
Dmitry Shmidt2b380482012-09-13 10:52:13 -07002957 }
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002958
Dmitry Shmidt216983b2015-02-06 10:50:36 -08002959 if (adv_id && adv_mac && deferred_session_resp) {
2960 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_FAILURE
2961 " p2p_dev_addr=" MACSTR " status=%d adv_id=%x"
2962 " deferred_session_resp='%s'",
2963 MAC2STR(peer), status, adv_id,
2964 deferred_session_resp);
2965 } else if (adv_id && adv_mac) {
2966 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_FAILURE
2967 " p2p_dev_addr=" MACSTR " status=%d adv_id=%x",
2968 MAC2STR(peer), status, adv_id);
2969 } else {
2970 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_FAILURE
2971 " p2p_dev_addr=" MACSTR " status=%d",
2972 MAC2STR(peer), status);
2973 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07002974
Jouni Malinen75ecf522011-06-27 15:19:46 -07002975 wpas_notify_p2p_provision_discovery(wpa_s, peer, 0 /* response */,
2976 status, 0, 0);
2977}
2978
2979
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002980static int freq_included(struct wpa_supplicant *wpa_s,
2981 const struct p2p_channels *channels,
2982 unsigned int freq)
Dmitry Shmidtf7e0a992013-05-23 11:03:10 -07002983{
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002984 if ((channels == NULL || p2p_channels_includes_freq(channels, freq)) &&
2985 wpas_p2p_go_is_peer_freq(wpa_s, freq))
2986 return 1;
2987 return 0;
2988}
Dmitry Shmidtf7e0a992013-05-23 11:03:10 -07002989
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002990
2991static void wpas_p2p_go_update_common_freqs(struct wpa_supplicant *wpa_s)
2992{
2993 unsigned int num = P2P_MAX_CHANNELS;
2994 int *common_freqs;
2995 int ret;
2996
2997 p2p_go_dump_common_freqs(wpa_s);
2998 common_freqs = os_calloc(num, sizeof(int));
2999 if (!common_freqs)
3000 return;
3001
3002 ret = p2p_group_get_common_freqs(wpa_s->p2p_group, common_freqs, &num);
3003 if (ret < 0) {
3004 wpa_dbg(wpa_s, MSG_DEBUG,
3005 "P2P: Failed to get group common freqs");
3006 os_free(common_freqs);
3007 return;
3008 }
3009
3010 os_free(wpa_s->p2p_group_common_freqs);
3011 wpa_s->p2p_group_common_freqs = common_freqs;
3012 wpa_s->p2p_group_common_freqs_num = num;
3013 p2p_go_dump_common_freqs(wpa_s);
3014}
3015
3016
3017/*
3018 * Check if the given frequency is one of the possible operating frequencies
3019 * set after the completion of the GO Negotiation.
3020 */
3021static int wpas_p2p_go_is_peer_freq(struct wpa_supplicant *wpa_s, int freq)
3022{
3023 unsigned int i;
3024
3025 p2p_go_dump_common_freqs(wpa_s);
3026
3027 /* assume no restrictions */
3028 if (!wpa_s->p2p_group_common_freqs_num)
3029 return 1;
3030
3031 for (i = 0; i < wpa_s->p2p_group_common_freqs_num; i++) {
3032 if (wpa_s->p2p_group_common_freqs[i] == freq)
3033 return 1;
3034 }
3035 return 0;
3036}
3037
3038
3039static int wpas_sta_check_ecsa(struct hostapd_data *hapd,
3040 struct sta_info *sta, void *ctx)
3041{
3042 int *ecsa_support = ctx;
3043
3044 *ecsa_support &= sta->ecsa_supported;
3045
3046 return 0;
3047}
3048
3049
3050/* Check if all the peers support eCSA */
3051static int wpas_p2p_go_clients_support_ecsa(struct wpa_supplicant *wpa_s)
3052{
3053 int ecsa_support = 1;
3054
3055 ap_for_each_sta(wpa_s->ap_iface->bss[0], wpas_sta_check_ecsa,
3056 &ecsa_support);
3057
3058 return ecsa_support;
Dmitry Shmidtf7e0a992013-05-23 11:03:10 -07003059}
3060
3061
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07003062/**
3063 * Pick the best frequency to use from all the currently used frequencies.
3064 */
3065static int wpas_p2p_pick_best_used_freq(struct wpa_supplicant *wpa_s,
3066 struct wpa_used_freq_data *freqs,
3067 unsigned int num)
3068{
3069 unsigned int i, c;
3070
3071 /* find a candidate freq that is supported by P2P */
3072 for (c = 0; c < num; c++)
3073 if (p2p_supported_freq(wpa_s->global->p2p, freqs[c].freq))
3074 break;
3075
3076 if (c == num)
3077 return 0;
3078
3079 /* once we have a candidate, try to find a 'better' one */
3080 for (i = c + 1; i < num; i++) {
3081 if (!p2p_supported_freq(wpa_s->global->p2p, freqs[i].freq))
3082 continue;
3083
3084 /*
3085 * 1. Infrastructure station interfaces have higher preference.
3086 * 2. P2P Clients have higher preference.
3087 * 3. All others.
3088 */
3089 if (freqs[i].flags & WPA_FREQ_USED_BY_INFRA_STATION) {
3090 c = i;
3091 break;
3092 }
3093
3094 if ((freqs[i].flags & WPA_FREQ_USED_BY_P2P_CLIENT))
3095 c = i;
3096 }
3097 return freqs[c].freq;
3098}
3099
3100
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003101static u8 wpas_invitation_process(void *ctx, const u8 *sa, const u8 *bssid,
3102 const u8 *go_dev_addr, const u8 *ssid,
3103 size_t ssid_len, int *go, u8 *group_bssid,
Dmitry Shmidtf7e0a992013-05-23 11:03:10 -07003104 int *force_freq, int persistent_group,
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08003105 const struct p2p_channels *channels,
3106 int dev_pw_id)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003107{
3108 struct wpa_supplicant *wpa_s = ctx;
3109 struct wpa_ssid *s;
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07003110 struct wpa_used_freq_data *freqs;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003111 struct wpa_supplicant *grp;
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07003112 int best_freq;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003113
3114 if (!persistent_group) {
3115 wpa_printf(MSG_DEBUG, "P2P: Invitation from " MACSTR
Dmitry Shmidt344abd32014-01-14 13:17:00 -08003116 " to join an active group (SSID: %s)",
3117 MAC2STR(sa), wpa_ssid_txt(ssid, ssid_len));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003118 if (!is_zero_ether_addr(wpa_s->p2p_auth_invite) &&
3119 (os_memcmp(go_dev_addr, wpa_s->p2p_auth_invite, ETH_ALEN)
3120 == 0 ||
3121 os_memcmp(sa, wpa_s->p2p_auth_invite, ETH_ALEN) == 0)) {
3122 wpa_printf(MSG_DEBUG, "P2P: Accept previously "
3123 "authorized invitation");
3124 goto accept_inv;
3125 }
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08003126
3127#ifdef CONFIG_WPS_NFC
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08003128 if (dev_pw_id >= 0 && wpa_s->p2p_nfc_tag_enabled &&
3129 dev_pw_id == wpa_s->p2p_oob_dev_pw_id) {
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08003130 wpa_printf(MSG_DEBUG, "P2P: Accept invitation based on local enabled NFC Tag");
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08003131 wpa_s->p2p_wps_method = WPS_NFC;
3132 wpa_s->pending_join_wps_method = WPS_NFC;
3133 os_memcpy(wpa_s->pending_join_dev_addr,
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08003134 go_dev_addr, ETH_ALEN);
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08003135 os_memcpy(wpa_s->pending_join_iface_addr,
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08003136 bssid, ETH_ALEN);
3137 goto accept_inv;
3138 }
3139#endif /* CONFIG_WPS_NFC */
3140
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003141 /*
3142 * Do not accept the invitation automatically; notify user and
3143 * request approval.
3144 */
3145 return P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE;
3146 }
3147
3148 grp = wpas_get_p2p_group(wpa_s, ssid, ssid_len, go);
3149 if (grp) {
3150 wpa_printf(MSG_DEBUG, "P2P: Accept invitation to already "
3151 "running persistent group");
3152 if (*go)
3153 os_memcpy(group_bssid, grp->own_addr, ETH_ALEN);
3154 goto accept_inv;
3155 }
3156
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08003157 if (!is_zero_ether_addr(wpa_s->p2p_auth_invite) &&
3158 os_memcmp(sa, wpa_s->p2p_auth_invite, ETH_ALEN) == 0) {
3159 wpa_printf(MSG_DEBUG, "P2P: Accept previously initiated "
3160 "invitation to re-invoke a persistent group");
Dmitry Shmidt76cd2cc2014-05-27 12:56:04 -07003161 os_memset(wpa_s->p2p_auth_invite, 0, ETH_ALEN);
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08003162 } else if (!wpa_s->conf->persistent_reconnect)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003163 return P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE;
3164
3165 for (s = wpa_s->conf->ssid; s; s = s->next) {
3166 if (s->disabled == 2 &&
3167 os_memcmp(s->bssid, go_dev_addr, ETH_ALEN) == 0 &&
3168 s->ssid_len == ssid_len &&
3169 os_memcmp(ssid, s->ssid, ssid_len) == 0)
3170 break;
3171 }
3172
3173 if (!s) {
3174 wpa_printf(MSG_DEBUG, "P2P: Invitation from " MACSTR
3175 " requested reinvocation of an unknown group",
3176 MAC2STR(sa));
3177 return P2P_SC_FAIL_UNKNOWN_GROUP;
3178 }
3179
3180 if (s->mode == WPAS_MODE_P2P_GO && !wpas_p2p_create_iface(wpa_s)) {
3181 *go = 1;
3182 if (wpa_s->wpa_state >= WPA_AUTHENTICATING) {
3183 wpa_printf(MSG_DEBUG, "P2P: The only available "
3184 "interface is already in use - reject "
3185 "invitation");
3186 return P2P_SC_FAIL_UNABLE_TO_ACCOMMODATE;
3187 }
Dmitry Shmidt9c175262016-03-03 10:20:07 -08003188 if (wpa_s->p2p_mgmt)
3189 os_memcpy(group_bssid, wpa_s->parent->own_addr,
3190 ETH_ALEN);
3191 else
3192 os_memcpy(group_bssid, wpa_s->own_addr, ETH_ALEN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003193 } else if (s->mode == WPAS_MODE_P2P_GO) {
3194 *go = 1;
3195 if (wpas_p2p_add_group_interface(wpa_s, WPA_IF_P2P_GO) < 0)
3196 {
3197 wpa_printf(MSG_ERROR, "P2P: Failed to allocate a new "
3198 "interface address for the group");
3199 return P2P_SC_FAIL_UNABLE_TO_ACCOMMODATE;
3200 }
3201 os_memcpy(group_bssid, wpa_s->pending_interface_addr,
3202 ETH_ALEN);
3203 }
3204
3205accept_inv:
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003206 wpas_p2p_set_own_freq_preference(wpa_s, 0);
3207
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07003208 best_freq = 0;
3209 freqs = os_calloc(wpa_s->num_multichan_concurrent,
3210 sizeof(struct wpa_used_freq_data));
3211 if (freqs) {
3212 int num_channels = wpa_s->num_multichan_concurrent;
3213 int num = wpas_p2p_valid_oper_freqs(wpa_s, freqs, num_channels);
3214 best_freq = wpas_p2p_pick_best_used_freq(wpa_s, freqs, num);
3215 os_free(freqs);
3216 }
3217
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07003218 /* Get one of the frequencies currently in use */
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07003219 if (best_freq > 0) {
Dmitry Shmidta0d265f2013-11-19 13:13:41 -08003220 wpa_printf(MSG_DEBUG, "P2P: Trying to prefer a channel already used by one of the interfaces");
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07003221 wpas_p2p_set_own_freq_preference(wpa_s, best_freq);
Dmitry Shmidta0d265f2013-11-19 13:13:41 -08003222
3223 if (wpa_s->num_multichan_concurrent < 2 ||
3224 wpas_p2p_num_unused_channels(wpa_s) < 1) {
3225 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 -07003226 *force_freq = best_freq;
Dmitry Shmidta0d265f2013-11-19 13:13:41 -08003227 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003228 }
3229
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07003230 if (*force_freq > 0 && wpa_s->num_multichan_concurrent > 1 &&
3231 wpas_p2p_num_unused_channels(wpa_s) > 0) {
Dmitry Shmidtf7e0a992013-05-23 11:03:10 -07003232 if (*go == 0) {
3233 /* We are the client */
3234 wpa_printf(MSG_DEBUG, "P2P: Peer was found to be "
3235 "running a GO but we are capable of MCC, "
3236 "figure out the best channel to use");
3237 *force_freq = 0;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003238 } else if (!freq_included(wpa_s, channels, *force_freq)) {
Dmitry Shmidtf7e0a992013-05-23 11:03:10 -07003239 /* We are the GO, and *force_freq is not in the
3240 * intersection */
3241 wpa_printf(MSG_DEBUG, "P2P: Forced GO freq %d MHz not "
3242 "in intersection but we are capable of MCC, "
3243 "figure out the best channel to use",
3244 *force_freq);
3245 *force_freq = 0;
3246 }
3247 }
3248
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003249 return P2P_SC_SUCCESS;
3250}
3251
3252
3253static void wpas_invitation_received(void *ctx, const u8 *sa, const u8 *bssid,
3254 const u8 *ssid, size_t ssid_len,
3255 const u8 *go_dev_addr, u8 status,
3256 int op_freq)
3257{
3258 struct wpa_supplicant *wpa_s = ctx;
3259 struct wpa_ssid *s;
3260
3261 for (s = wpa_s->conf->ssid; s; s = s->next) {
3262 if (s->disabled == 2 &&
3263 s->ssid_len == ssid_len &&
3264 os_memcmp(ssid, s->ssid, ssid_len) == 0)
3265 break;
3266 }
3267
3268 if (status == P2P_SC_SUCCESS) {
3269 wpa_printf(MSG_DEBUG, "P2P: Invitation from peer " MACSTR
Dmitry Shmidt344abd32014-01-14 13:17:00 -08003270 " was accepted; op_freq=%d MHz, SSID=%s",
3271 MAC2STR(sa), op_freq, wpa_ssid_txt(ssid, ssid_len));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003272 if (s) {
Dmitry Shmidt91c40cd2012-09-25 14:23:53 -07003273 int go = s->mode == WPAS_MODE_P2P_GO;
Dmitry Shmidt849734c2016-05-27 09:59:01 -07003274 if (go) {
3275 wpa_msg_global(wpa_s, MSG_INFO,
3276 P2P_EVENT_INVITATION_ACCEPTED
3277 "sa=" MACSTR
3278 " persistent=%d freq=%d",
3279 MAC2STR(sa), s->id, op_freq);
3280 } else {
3281 wpa_msg_global(wpa_s, MSG_INFO,
3282 P2P_EVENT_INVITATION_ACCEPTED
3283 "sa=" MACSTR
3284 " persistent=%d",
3285 MAC2STR(sa), s->id);
3286 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003287 wpas_p2p_group_add_persistent(
Hai Shalomb755a2a2020-04-23 21:49:02 -07003288 wpa_s, s, go, 0, op_freq, 0,
3289 wpa_s->conf->p2p_go_ht40,
3290 wpa_s->conf->p2p_go_vht,
3291 0,
3292 wpa_s->conf->p2p_go_he,
3293 wpa_s->conf->p2p_go_edmg, NULL,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003294 go ? P2P_MAX_INITIAL_CONN_WAIT_GO_REINVOKE : 0,
Hai Shalomc1a21442022-02-04 13:43:00 -08003295 1, is_p2p_allow_6ghz(wpa_s->global->p2p));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003296 } else if (bssid) {
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08003297 wpa_s->user_initiated_pd = 0;
Dmitry Shmidt849734c2016-05-27 09:59:01 -07003298 wpa_msg_global(wpa_s, MSG_INFO,
3299 P2P_EVENT_INVITATION_ACCEPTED
3300 "sa=" MACSTR " go_dev_addr=" MACSTR
3301 " bssid=" MACSTR " unknown-network",
3302 MAC2STR(sa), MAC2STR(go_dev_addr),
3303 MAC2STR(bssid));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003304 wpas_p2p_join(wpa_s, bssid, go_dev_addr,
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08003305 wpa_s->p2p_wps_method, 0, op_freq,
Dmitry Shmidt344abd32014-01-14 13:17:00 -08003306 ssid, ssid_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003307 }
3308 return;
3309 }
3310
3311 if (status != P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE) {
3312 wpa_printf(MSG_DEBUG, "P2P: Invitation from peer " MACSTR
3313 " was rejected (status %u)", MAC2STR(sa), status);
3314 return;
3315 }
3316
3317 if (!s) {
3318 if (bssid) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07003319 wpa_msg_global(wpa_s, MSG_INFO,
3320 P2P_EVENT_INVITATION_RECEIVED
3321 "sa=" MACSTR " go_dev_addr=" MACSTR
3322 " bssid=" MACSTR " unknown-network",
3323 MAC2STR(sa), MAC2STR(go_dev_addr),
3324 MAC2STR(bssid));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003325 } else {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07003326 wpa_msg_global(wpa_s, MSG_INFO,
3327 P2P_EVENT_INVITATION_RECEIVED
3328 "sa=" MACSTR " go_dev_addr=" MACSTR
3329 " unknown-network",
3330 MAC2STR(sa), MAC2STR(go_dev_addr));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003331 }
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003332 wpas_notify_p2p_invitation_received(wpa_s, sa, go_dev_addr,
3333 bssid, 0, op_freq);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003334 return;
3335 }
3336
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08003337 if (s->mode == WPAS_MODE_P2P_GO && op_freq) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07003338 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_INVITATION_RECEIVED
3339 "sa=" MACSTR " persistent=%d freq=%d",
3340 MAC2STR(sa), s->id, op_freq);
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08003341 } else {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07003342 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_INVITATION_RECEIVED
3343 "sa=" MACSTR " persistent=%d",
3344 MAC2STR(sa), s->id);
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08003345 }
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003346 wpas_notify_p2p_invitation_received(wpa_s, sa, go_dev_addr, bssid,
3347 s->id, op_freq);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003348}
3349
3350
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003351static void wpas_remove_persistent_peer(struct wpa_supplicant *wpa_s,
3352 struct wpa_ssid *ssid,
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07003353 const u8 *peer, int inv)
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003354{
3355 size_t i;
Dmitry Shmidt8bd70b72015-05-26 16:02:19 -07003356 struct wpa_supplicant *p2p_wpa_s = wpa_s->global->p2p_init_wpa_s;
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003357
3358 if (ssid == NULL)
3359 return;
3360
3361 for (i = 0; ssid->p2p_client_list && i < ssid->num_p2p_clients; i++) {
Dmitry Shmidtff787d52015-01-12 13:01:47 -08003362 if (os_memcmp(ssid->p2p_client_list + i * 2 * ETH_ALEN, peer,
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003363 ETH_ALEN) == 0)
3364 break;
3365 }
Dmitry Shmidt6aa8ae42014-07-07 09:31:33 -07003366 if (i >= ssid->num_p2p_clients || !ssid->p2p_client_list) {
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003367 if (ssid->mode != WPAS_MODE_P2P_GO &&
3368 os_memcmp(ssid->bssid, peer, ETH_ALEN) == 0) {
3369 wpa_printf(MSG_DEBUG, "P2P: Remove persistent group %d "
3370 "due to invitation result", ssid->id);
3371 wpas_notify_network_removed(wpa_s, ssid);
3372 wpa_config_remove_network(wpa_s->conf, ssid->id);
3373 return;
3374 }
3375 return; /* Peer not found in client list */
3376 }
3377
3378 wpa_printf(MSG_DEBUG, "P2P: Remove peer " MACSTR " from persistent "
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07003379 "group %d client list%s",
3380 MAC2STR(peer), ssid->id,
3381 inv ? " due to invitation result" : "");
Dmitry Shmidtff787d52015-01-12 13:01:47 -08003382 os_memmove(ssid->p2p_client_list + i * 2 * ETH_ALEN,
3383 ssid->p2p_client_list + (i + 1) * 2 * ETH_ALEN,
3384 (ssid->num_p2p_clients - i - 1) * 2 * ETH_ALEN);
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003385 ssid->num_p2p_clients--;
Dmitry Shmidt8bd70b72015-05-26 16:02:19 -07003386 if (p2p_wpa_s->conf->update_config &&
3387 wpa_config_write(p2p_wpa_s->confname, p2p_wpa_s->conf))
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003388 wpa_printf(MSG_DEBUG, "P2P: Failed to update configuration");
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003389}
3390
3391
3392static void wpas_remove_persistent_client(struct wpa_supplicant *wpa_s,
3393 const u8 *peer)
3394{
3395 struct wpa_ssid *ssid;
3396
3397 wpa_s = wpa_s->global->p2p_invite_group;
3398 if (wpa_s == NULL)
3399 return; /* No known invitation group */
3400 ssid = wpa_s->current_ssid;
3401 if (ssid == NULL || ssid->mode != WPAS_MODE_P2P_GO ||
3402 !ssid->p2p_persistent_group)
3403 return; /* Not operating as a GO in persistent group */
Dmitry Shmidt9c175262016-03-03 10:20:07 -08003404 ssid = wpas_p2p_get_persistent(wpa_s->p2pdev, peer,
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003405 ssid->ssid, ssid->ssid_len);
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07003406 wpas_remove_persistent_peer(wpa_s, ssid, peer, 1);
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003407}
3408
3409
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08003410static void wpas_invitation_result(void *ctx, int status, const u8 *bssid,
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003411 const struct p2p_channels *channels,
Dmitry Shmidt15907092014-03-25 10:42:57 -07003412 const u8 *peer, int neg_freq,
3413 int peer_oper_freq)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003414{
3415 struct wpa_supplicant *wpa_s = ctx;
3416 struct wpa_ssid *ssid;
Dmitry Shmidt15907092014-03-25 10:42:57 -07003417 int freq;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003418
3419 if (bssid) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07003420 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_INVITATION_RESULT
3421 "status=%d " MACSTR,
3422 status, MAC2STR(bssid));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003423 } else {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07003424 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_INVITATION_RESULT
3425 "status=%d ", status);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003426 }
3427 wpas_notify_p2p_invitation_result(wpa_s, status, bssid);
3428
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003429 wpa_printf(MSG_DEBUG, "P2P: Invitation result - status=%d peer=" MACSTR,
3430 status, MAC2STR(peer));
3431 if (wpa_s->pending_invite_ssid_id == -1) {
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08003432 struct wpa_supplicant *group_if =
3433 wpa_s->global->p2p_invite_group;
3434
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003435 if (status == P2P_SC_FAIL_UNKNOWN_GROUP)
3436 wpas_remove_persistent_client(wpa_s, peer);
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08003437
3438 /*
3439 * Invitation to an active group. If this is successful and we
3440 * are the GO, set the client wait to postpone some concurrent
3441 * operations and to allow provisioning and connection to happen
3442 * more quickly.
3443 */
3444 if (status == P2P_SC_SUCCESS &&
3445 group_if && group_if->current_ssid &&
3446 group_if->current_ssid->mode == WPAS_MODE_P2P_GO) {
3447 os_get_reltime(&wpa_s->global->p2p_go_wait_client);
3448#ifdef CONFIG_TESTING_OPTIONS
3449 if (group_if->p2p_go_csa_on_inv) {
3450 wpa_printf(MSG_DEBUG,
3451 "Testing: force P2P GO CSA after invitation");
3452 eloop_cancel_timeout(
3453 wpas_p2p_reconsider_moving_go,
3454 wpa_s, NULL);
3455 eloop_register_timeout(
3456 0, 50000,
3457 wpas_p2p_reconsider_moving_go,
3458 wpa_s, NULL);
3459 }
3460#endif /* CONFIG_TESTING_OPTIONS */
3461 }
3462 return;
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003463 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003464
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08003465 if (status == P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE) {
3466 wpa_printf(MSG_DEBUG, "P2P: Waiting for peer to start another "
3467 "invitation exchange to indicate readiness for "
3468 "re-invocation");
3469 }
3470
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003471 if (status != P2P_SC_SUCCESS) {
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003472 if (status == P2P_SC_FAIL_UNKNOWN_GROUP) {
3473 ssid = wpa_config_get_network(
3474 wpa_s->conf, wpa_s->pending_invite_ssid_id);
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07003475 wpas_remove_persistent_peer(wpa_s, ssid, peer, 1);
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003476 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003477 wpas_p2p_remove_pending_group_interface(wpa_s);
3478 return;
3479 }
3480
3481 ssid = wpa_config_get_network(wpa_s->conf,
3482 wpa_s->pending_invite_ssid_id);
3483 if (ssid == NULL) {
3484 wpa_printf(MSG_ERROR, "P2P: Could not find persistent group "
3485 "data matching with invitation");
3486 return;
3487 }
3488
Jouni Malinen2c5b17d2012-10-13 21:52:46 -07003489 /*
3490 * The peer could have missed our ctrl::ack frame for Invitation
3491 * Response and continue retransmitting the frame. To reduce the
3492 * likelihood of the peer not getting successful TX status for the
3493 * Invitation Response frame, wait a short time here before starting
3494 * the persistent group so that we will remain on the current channel to
3495 * acknowledge any possible retransmission from the peer.
3496 */
3497 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: 50 ms wait on current channel before "
3498 "starting persistent group");
3499 os_sleep(0, 50000);
3500
Dmitry Shmidt15907092014-03-25 10:42:57 -07003501 if (neg_freq > 0 && ssid->mode == WPAS_MODE_P2P_GO &&
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003502 freq_included(wpa_s, channels, neg_freq))
Dmitry Shmidt15907092014-03-25 10:42:57 -07003503 freq = neg_freq;
3504 else if (peer_oper_freq > 0 && ssid->mode != WPAS_MODE_P2P_GO &&
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003505 freq_included(wpa_s, channels, peer_oper_freq))
Dmitry Shmidt15907092014-03-25 10:42:57 -07003506 freq = peer_oper_freq;
3507 else
3508 freq = 0;
3509
3510 wpa_printf(MSG_DEBUG, "P2P: Persistent group invitation success - op_freq=%d MHz SSID=%s",
3511 freq, wpa_ssid_txt(ssid->ssid, ssid->ssid_len));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003512 wpas_p2p_group_add_persistent(wpa_s, ssid,
Jouni Malinen31be0a42012-08-31 21:20:51 +03003513 ssid->mode == WPAS_MODE_P2P_GO,
Dmitry Shmidt96be6222014-02-13 10:16:51 -08003514 wpa_s->p2p_persistent_go_freq,
Dmitry Shmidt15907092014-03-25 10:42:57 -07003515 freq,
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08003516 wpa_s->p2p_go_vht_center_freq2,
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003517 wpa_s->p2p_go_ht40, wpa_s->p2p_go_vht,
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08003518 wpa_s->p2p_go_max_oper_chwidth,
Hai Shalom74f70d42019-02-11 14:42:39 -08003519 wpa_s->p2p_go_he,
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08003520 wpa_s->p2p_go_edmg,
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003521 channels,
Dmitry Shmidt56052862013-10-04 10:23:25 -07003522 ssid->mode == WPAS_MODE_P2P_GO ?
3523 P2P_MAX_INITIAL_CONN_WAIT_GO_REINVOKE :
Hai Shalomc1a21442022-02-04 13:43:00 -08003524 0, 1,
3525 is_p2p_allow_6ghz(wpa_s->global->p2p));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003526}
3527
3528
Dmitry Shmidt04949592012-07-19 12:16:46 -07003529static int wpas_p2p_disallowed_freq(struct wpa_global *global,
3530 unsigned int freq)
3531{
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08003532 if (freq_range_list_includes(&global->p2p_go_avoid_freq, freq))
3533 return 1;
Dmitry Shmidt4ce9c872013-10-24 11:08:13 -07003534 return freq_range_list_includes(&global->p2p_disallow_freq, freq);
Dmitry Shmidt04949592012-07-19 12:16:46 -07003535}
3536
3537
3538static void wpas_p2p_add_chan(struct p2p_reg_class *reg, u8 chan)
3539{
3540 reg->channel[reg->channels] = chan;
3541 reg->channels++;
3542}
3543
3544
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003545static int wpas_p2p_default_channels(struct wpa_supplicant *wpa_s,
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003546 struct p2p_channels *chan,
3547 struct p2p_channels *cli_chan)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003548{
3549 int i, cla = 0;
3550
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003551 wpa_s->global->p2p_24ghz_social_channels = 1;
3552
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003553 os_memset(cli_chan, 0, sizeof(*cli_chan));
3554
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003555 wpa_printf(MSG_DEBUG, "P2P: Enable operating classes for 2.4 GHz "
3556 "band");
3557
3558 /* Operating class 81 - 2.4 GHz band channels 1..13 */
3559 chan->reg_class[cla].reg_class = 81;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003560 chan->reg_class[cla].channels = 0;
3561 for (i = 0; i < 11; i++) {
3562 if (!wpas_p2p_disallowed_freq(wpa_s->global, 2412 + i * 5))
3563 wpas_p2p_add_chan(&chan->reg_class[cla], i + 1);
3564 }
3565 if (chan->reg_class[cla].channels)
3566 cla++;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003567
3568 wpa_printf(MSG_DEBUG, "P2P: Enable operating classes for lower 5 GHz "
3569 "band");
3570
3571 /* Operating class 115 - 5 GHz, channels 36-48 */
3572 chan->reg_class[cla].reg_class = 115;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003573 chan->reg_class[cla].channels = 0;
3574 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 36 * 5))
3575 wpas_p2p_add_chan(&chan->reg_class[cla], 36);
3576 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 40 * 5))
3577 wpas_p2p_add_chan(&chan->reg_class[cla], 40);
3578 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 44 * 5))
3579 wpas_p2p_add_chan(&chan->reg_class[cla], 44);
3580 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 48 * 5))
3581 wpas_p2p_add_chan(&chan->reg_class[cla], 48);
3582 if (chan->reg_class[cla].channels)
3583 cla++;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003584
3585 wpa_printf(MSG_DEBUG, "P2P: Enable operating classes for higher 5 GHz "
3586 "band");
3587
3588 /* Operating class 124 - 5 GHz, channels 149,153,157,161 */
3589 chan->reg_class[cla].reg_class = 124;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003590 chan->reg_class[cla].channels = 0;
3591 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 149 * 5))
3592 wpas_p2p_add_chan(&chan->reg_class[cla], 149);
3593 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 153 * 5))
3594 wpas_p2p_add_chan(&chan->reg_class[cla], 153);
3595 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 156 * 5))
3596 wpas_p2p_add_chan(&chan->reg_class[cla], 157);
3597 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 161 * 5))
3598 wpas_p2p_add_chan(&chan->reg_class[cla], 161);
3599 if (chan->reg_class[cla].channels)
3600 cla++;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003601
3602 chan->reg_classes = cla;
3603 return 0;
3604}
3605
3606
Hai Shalomc1a21442022-02-04 13:43:00 -08003607static enum chan_allowed has_channel(struct wpa_global *global,
3608 struct hostapd_hw_modes *mode, u8 op_class,
3609 u8 chan, int *flags)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003610{
3611 int i;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003612 unsigned int freq;
3613
Hai Shalomc1a21442022-02-04 13:43:00 -08003614 freq = ieee80211_chan_to_freq(NULL, op_class, chan);
Dmitry Shmidt04949592012-07-19 12:16:46 -07003615 if (wpas_p2p_disallowed_freq(global, freq))
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003616 return NOT_ALLOWED;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003617
3618 for (i = 0; i < mode->num_channels; i++) {
Hai Shalomc1a21442022-02-04 13:43:00 -08003619 if ((unsigned int) mode->channels[i].freq == freq) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003620 if (flags)
3621 *flags = mode->channels[i].flag;
Hai Shalomc1a21442022-02-04 13:43:00 -08003622 if (mode->channels[i].flag & HOSTAPD_CHAN_DISABLED)
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003623 return NOT_ALLOWED;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003624 if (mode->channels[i].flag & HOSTAPD_CHAN_NO_IR)
3625 return NO_IR;
Hai Shalomc1a21442022-02-04 13:43:00 -08003626 if (mode->channels[i].flag & HOSTAPD_CHAN_RADAR)
3627 return RADAR;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003628 return ALLOWED;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003629 }
3630 }
3631
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003632 return NOT_ALLOWED;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003633}
3634
3635
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003636static int wpas_p2p_get_center_80mhz(struct wpa_supplicant *wpa_s,
3637 struct hostapd_hw_modes *mode,
Hai Shalomc1a21442022-02-04 13:43:00 -08003638 u8 channel, const u8 *center_channels,
3639 size_t num_chan)
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003640{
Dmitry Shmidtaca489e2016-09-28 15:44:14 -07003641 size_t i;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003642
3643 if (mode->mode != HOSTAPD_MODE_IEEE80211A)
3644 return 0;
3645
Hai Shalomc1a21442022-02-04 13:43:00 -08003646 for (i = 0; i < num_chan; i++)
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003647 /*
3648 * In 80 MHz, the bandwidth "spans" 12 channels (e.g., 36-48),
3649 * so the center channel is 6 channels away from the start/end.
3650 */
3651 if (channel >= center_channels[i] - 6 &&
3652 channel <= center_channels[i] + 6)
3653 return center_channels[i];
3654
3655 return 0;
3656}
3657
3658
Hai Shalomc1a21442022-02-04 13:43:00 -08003659static const u8 center_channels_5ghz_80mhz[] = { 42, 58, 106, 122, 138,
3660 155, 171 };
3661static const u8 center_channels_6ghz_80mhz[] = { 7, 23, 39, 55, 71, 87, 103,
3662 119, 135, 151, 167, 183, 199,
3663 215 };
3664
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003665static enum chan_allowed wpas_p2p_verify_80mhz(struct wpa_supplicant *wpa_s,
3666 struct hostapd_hw_modes *mode,
Hai Shalomc1a21442022-02-04 13:43:00 -08003667 u8 op_class, u8 channel, u8 bw)
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003668{
3669 u8 center_chan;
3670 int i, flags;
3671 enum chan_allowed res, ret = ALLOWED;
Hai Shalomc1a21442022-02-04 13:43:00 -08003672 const u8 *chans;
3673 size_t num_chans;
3674 bool is_6ghz = is_6ghz_op_class(op_class);
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003675
Hai Shalomc1a21442022-02-04 13:43:00 -08003676 if (is_6ghz) {
3677 chans = center_channels_6ghz_80mhz;
3678 num_chans = ARRAY_SIZE(center_channels_6ghz_80mhz);
3679 } else {
3680 chans = center_channels_5ghz_80mhz;
3681 num_chans = ARRAY_SIZE(center_channels_5ghz_80mhz);
3682 }
3683 center_chan = wpas_p2p_get_center_80mhz(wpa_s, mode, channel,
3684 chans, num_chans);
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003685 if (!center_chan)
3686 return NOT_ALLOWED;
Hai Shalomc1a21442022-02-04 13:43:00 -08003687 if (!wpa_s->p2p_go_allow_dfs &&
3688 !is_6ghz && center_chan >= 58 && center_chan <= 138)
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003689 return NOT_ALLOWED; /* Do not allow DFS channels for P2P */
3690
3691 /* check all the channels are available */
3692 for (i = 0; i < 4; i++) {
3693 int adj_chan = center_chan - 6 + i * 4;
3694
Hai Shalomc1a21442022-02-04 13:43:00 -08003695 res = has_channel(wpa_s->global, mode, op_class, adj_chan,
3696 &flags);
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003697 if (res == NOT_ALLOWED)
3698 return NOT_ALLOWED;
Hai Shalomc1a21442022-02-04 13:43:00 -08003699 if (res == RADAR)
3700 ret = RADAR;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003701 if (res == NO_IR)
3702 ret = NO_IR;
Hai Shalomc1a21442022-02-04 13:43:00 -08003703 if (!is_6ghz) {
3704 if (i == 0 && !(flags & HOSTAPD_CHAN_VHT_10_70))
3705 return NOT_ALLOWED;
3706 if (i == 1 && !(flags & HOSTAPD_CHAN_VHT_30_50))
3707 return NOT_ALLOWED;
3708 if (i == 2 && !(flags & HOSTAPD_CHAN_VHT_50_30))
3709 return NOT_ALLOWED;
3710 if (i == 3 && !(flags & HOSTAPD_CHAN_VHT_70_10))
3711 return NOT_ALLOWED;
3712 } else if (is_6ghz &&
3713 (!(wpas_get_6ghz_he_chwidth_capab(mode) &
3714 HE_PHYCAP_CHANNEL_WIDTH_SET_40MHZ_80MHZ_IN_5G))) {
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003715 return NOT_ALLOWED;
Hai Shalomc1a21442022-02-04 13:43:00 -08003716 }
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003717 }
3718
3719 return ret;
3720}
3721
3722
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08003723static int wpas_p2p_get_center_160mhz(struct wpa_supplicant *wpa_s,
3724 struct hostapd_hw_modes *mode,
Hai Shalomc1a21442022-02-04 13:43:00 -08003725 u8 channel, const u8 *center_channels,
3726 size_t num_chan)
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08003727{
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08003728 unsigned int i;
3729
3730 if (mode->mode != HOSTAPD_MODE_IEEE80211A)
3731 return 0;
3732
Hai Shalomc1a21442022-02-04 13:43:00 -08003733 for (i = 0; i < num_chan; i++)
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08003734 /*
3735 * In 160 MHz, the bandwidth "spans" 28 channels (e.g., 36-64),
3736 * so the center channel is 14 channels away from the start/end.
3737 */
3738 if (channel >= center_channels[i] - 14 &&
3739 channel <= center_channels[i] + 14)
3740 return center_channels[i];
3741
3742 return 0;
3743}
3744
3745
Hai Shalomc1a21442022-02-04 13:43:00 -08003746static const u8 center_channels_5ghz_160mhz[] = { 50, 114, 163 };
3747static const u8 center_channels_6ghz_160mhz[] = { 15, 47, 79, 111, 143, 175,
3748 207 };
3749
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08003750static enum chan_allowed wpas_p2p_verify_160mhz(struct wpa_supplicant *wpa_s,
3751 struct hostapd_hw_modes *mode,
Hai Shalomc1a21442022-02-04 13:43:00 -08003752 u8 op_class, u8 channel, u8 bw)
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08003753{
3754 u8 center_chan;
3755 int i, flags;
3756 enum chan_allowed res, ret = ALLOWED;
Hai Shalomc1a21442022-02-04 13:43:00 -08003757 const u8 *chans;
3758 size_t num_chans;
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08003759
Hai Shalomc1a21442022-02-04 13:43:00 -08003760 if (is_6ghz_op_class(op_class)) {
3761 chans = center_channels_6ghz_160mhz;
3762 num_chans = ARRAY_SIZE(center_channels_6ghz_160mhz);
3763 } else {
3764 chans = center_channels_5ghz_160mhz;
3765 num_chans = ARRAY_SIZE(center_channels_5ghz_160mhz);
3766 }
3767 center_chan = wpas_p2p_get_center_160mhz(wpa_s, mode, channel,
3768 chans, num_chans);
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08003769 if (!center_chan)
3770 return NOT_ALLOWED;
3771 /* VHT 160 MHz uses DFS channels in most countries. */
3772
3773 /* Check all the channels are available */
3774 for (i = 0; i < 8; i++) {
3775 int adj_chan = center_chan - 14 + i * 4;
3776
Hai Shalomc1a21442022-02-04 13:43:00 -08003777 res = has_channel(wpa_s->global, mode, op_class, adj_chan,
3778 &flags);
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08003779 if (res == NOT_ALLOWED)
3780 return NOT_ALLOWED;
3781
Hai Shalomc1a21442022-02-04 13:43:00 -08003782 if (res == RADAR)
3783 ret = RADAR;
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08003784 if (res == NO_IR)
3785 ret = NO_IR;
3786
Hai Shalomc1a21442022-02-04 13:43:00 -08003787 if (!is_6ghz_op_class(op_class)) {
3788 if (i == 0 && !(flags & HOSTAPD_CHAN_VHT_10_150))
3789 return NOT_ALLOWED;
3790 if (i == 1 && !(flags & HOSTAPD_CHAN_VHT_30_130))
3791 return NOT_ALLOWED;
3792 if (i == 2 && !(flags & HOSTAPD_CHAN_VHT_50_110))
3793 return NOT_ALLOWED;
3794 if (i == 3 && !(flags & HOSTAPD_CHAN_VHT_70_90))
3795 return NOT_ALLOWED;
3796 if (i == 4 && !(flags & HOSTAPD_CHAN_VHT_90_70))
3797 return NOT_ALLOWED;
3798 if (i == 5 && !(flags & HOSTAPD_CHAN_VHT_110_50))
3799 return NOT_ALLOWED;
3800 if (i == 6 && !(flags & HOSTAPD_CHAN_VHT_130_30))
3801 return NOT_ALLOWED;
3802 if (i == 7 && !(flags & HOSTAPD_CHAN_VHT_150_10))
3803 return NOT_ALLOWED;
3804 } else if (is_6ghz_op_class(op_class) &&
3805 (!(wpas_get_6ghz_he_chwidth_capab(mode) &
3806 HE_PHYCAP_CHANNEL_WIDTH_SET_160MHZ_IN_5G))) {
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08003807 return NOT_ALLOWED;
Hai Shalomc1a21442022-02-04 13:43:00 -08003808 }
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08003809 }
3810
3811 return ret;
3812}
3813
3814
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08003815static enum chan_allowed wpas_p2p_verify_edmg(struct wpa_supplicant *wpa_s,
3816 struct hostapd_hw_modes *mode,
3817 u8 channel)
3818{
3819 struct ieee80211_edmg_config edmg;
3820
3821 hostapd_encode_edmg_chan(1, channel, 0, &edmg);
3822 if (edmg.channels && ieee802_edmg_is_allowed(mode->edmg, edmg))
3823 return ALLOWED;
3824
3825 return NOT_ALLOWED;
3826}
3827
3828
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003829static enum chan_allowed wpas_p2p_verify_channel(struct wpa_supplicant *wpa_s,
3830 struct hostapd_hw_modes *mode,
Hai Shalomc1a21442022-02-04 13:43:00 -08003831 u8 op_class, u8 channel, u8 bw)
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003832{
Dmitry Shmidt96be6222014-02-13 10:16:51 -08003833 int flag = 0;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003834 enum chan_allowed res, res2;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003835
Hai Shalomc1a21442022-02-04 13:43:00 -08003836 res2 = res = has_channel(wpa_s->global, mode, op_class, channel, &flag);
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003837 if (bw == BW40MINUS) {
3838 if (!(flag & HOSTAPD_CHAN_HT40MINUS))
3839 return NOT_ALLOWED;
Hai Shalomc1a21442022-02-04 13:43:00 -08003840 res2 = has_channel(wpa_s->global, mode, op_class, channel - 4,
3841 NULL);
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003842 } else if (bw == BW40PLUS) {
3843 if (!(flag & HOSTAPD_CHAN_HT40PLUS))
3844 return NOT_ALLOWED;
Hai Shalomc1a21442022-02-04 13:43:00 -08003845 res2 = has_channel(wpa_s->global, mode, op_class, channel + 4,
3846 NULL);
3847 } else if (is_6ghz_op_class(op_class) && bw == BW40) {
3848 if (mode->mode != HOSTAPD_MODE_IEEE80211A)
3849 return NOT_ALLOWED;
3850 if (get_6ghz_sec_channel(channel) < 0)
3851 res2 = has_channel(wpa_s->global, mode, op_class,
3852 channel - 4, NULL);
3853 else
3854 res2 = has_channel(wpa_s->global, mode, op_class,
3855 channel + 4, NULL);
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003856 } else if (bw == BW80) {
Hai Shalomc1a21442022-02-04 13:43:00 -08003857 res2 = wpas_p2p_verify_80mhz(wpa_s, mode, op_class, channel,
3858 bw);
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08003859 } else if (bw == BW160) {
Hai Shalomc1a21442022-02-04 13:43:00 -08003860 res2 = wpas_p2p_verify_160mhz(wpa_s, mode, op_class, channel,
3861 bw);
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08003862 } else if (bw == BW4320 || bw == BW6480 || bw == BW8640) {
3863 return wpas_p2p_verify_edmg(wpa_s, mode, channel);
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003864 }
3865
3866 if (res == NOT_ALLOWED || res2 == NOT_ALLOWED)
3867 return NOT_ALLOWED;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003868 if (res == NO_IR || res2 == NO_IR)
3869 return NO_IR;
Hai Shalomc1a21442022-02-04 13:43:00 -08003870 if (res == RADAR || res2 == RADAR)
3871 return RADAR;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003872 return res;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003873}
3874
3875
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003876static int wpas_p2p_setup_channels(struct wpa_supplicant *wpa_s,
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003877 struct p2p_channels *chan,
Hai Shalom60840252021-02-19 19:02:11 -08003878 struct p2p_channels *cli_chan,
3879 bool p2p_disable_6ghz)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003880{
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003881 struct hostapd_hw_modes *mode;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003882 int cla, op, cli_cla;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003883
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003884 if (wpa_s->hw.modes == NULL) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003885 wpa_printf(MSG_DEBUG, "P2P: Driver did not support fetching "
3886 "of all supported channels; assume dualband "
3887 "support");
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003888 return wpas_p2p_default_channels(wpa_s, chan, cli_chan);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003889 }
3890
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003891 cla = cli_cla = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003892
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08003893 for (op = 0; global_op_class[op].op_class; op++) {
3894 const struct oper_class_map *o = &global_op_class[op];
Hai Shalomc1a21442022-02-04 13:43:00 -08003895 unsigned int ch;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003896 struct p2p_reg_class *reg = NULL, *cli_reg = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003897
Hai Shalom899fcc72020-10-19 14:38:18 -07003898 if (o->p2p == NO_P2P_SUPP ||
Hai Shalom60840252021-02-19 19:02:11 -08003899 (is_6ghz_op_class(o->op_class) && p2p_disable_6ghz))
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08003900 continue;
3901
Hai Shalomfdcde762020-04-02 11:19:20 -07003902 mode = get_mode(wpa_s->hw.modes, wpa_s->hw.num_modes, o->mode,
3903 is_6ghz_op_class(o->op_class));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003904 if (mode == NULL)
3905 continue;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003906 if (mode->mode == HOSTAPD_MODE_IEEE80211G)
3907 wpa_s->global->p2p_24ghz_social_channels = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003908 for (ch = o->min_chan; ch <= o->max_chan; ch += o->inc) {
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003909 enum chan_allowed res;
Hai Shalom899fcc72020-10-19 14:38:18 -07003910
3911 /* Check for non-continuous jump in channel index
3912 * incrementation */
Hai Shalom60840252021-02-19 19:02:11 -08003913 if ((o->op_class >= 128 && o->op_class <= 130) &&
Hai Shalom899fcc72020-10-19 14:38:18 -07003914 ch < 149 && ch + o->inc > 149)
3915 ch = 149;
3916
Hai Shalomc1a21442022-02-04 13:43:00 -08003917 res = wpas_p2p_verify_channel(wpa_s, mode, o->op_class,
3918 ch, o->bw);
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003919 if (res == ALLOWED) {
3920 if (reg == NULL) {
Hai Shalomfdcde762020-04-02 11:19:20 -07003921 if (cla == P2P_MAX_REG_CLASSES)
3922 continue;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003923 wpa_printf(MSG_DEBUG, "P2P: Add operating class %u",
3924 o->op_class);
3925 reg = &chan->reg_class[cla];
3926 cla++;
3927 reg->reg_class = o->op_class;
3928 }
Hai Shalomfdcde762020-04-02 11:19:20 -07003929 if (reg->channels == P2P_MAX_REG_CLASS_CHANNELS)
3930 continue;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003931 reg->channel[reg->channels] = ch;
3932 reg->channels++;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003933 } else if (res == NO_IR &&
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003934 wpa_s->conf->p2p_add_cli_chan) {
3935 if (cli_reg == NULL) {
Hai Shalomfdcde762020-04-02 11:19:20 -07003936 if (cli_cla == P2P_MAX_REG_CLASSES)
3937 continue;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003938 wpa_printf(MSG_DEBUG, "P2P: Add operating class %u (client only)",
3939 o->op_class);
3940 cli_reg = &cli_chan->reg_class[cli_cla];
3941 cli_cla++;
3942 cli_reg->reg_class = o->op_class;
3943 }
Hai Shalomfdcde762020-04-02 11:19:20 -07003944 if (cli_reg->channels ==
3945 P2P_MAX_REG_CLASS_CHANNELS)
3946 continue;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003947 cli_reg->channel[cli_reg->channels] = ch;
3948 cli_reg->channels++;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003949 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003950 }
3951 if (reg) {
3952 wpa_hexdump(MSG_DEBUG, "P2P: Channels",
3953 reg->channel, reg->channels);
3954 }
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003955 if (cli_reg) {
3956 wpa_hexdump(MSG_DEBUG, "P2P: Channels (client only)",
3957 cli_reg->channel, cli_reg->channels);
3958 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003959 }
3960
3961 chan->reg_classes = cla;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003962 cli_chan->reg_classes = cli_cla;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003963
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003964 return 0;
3965}
3966
3967
Hai Shalomc1a21442022-02-04 13:43:00 -08003968int wpas_p2p_get_sec_channel_offset_40mhz(struct wpa_supplicant *wpa_s,
3969 struct hostapd_hw_modes *mode,
3970 u8 channel)
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003971{
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003972 int op;
3973 enum chan_allowed ret;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003974
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08003975 for (op = 0; global_op_class[op].op_class; op++) {
3976 const struct oper_class_map *o = &global_op_class[op];
Hai Shalomc1a21442022-02-04 13:43:00 -08003977 u16 ch;
3978 int chan = channel;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003979
Hai Shalomc1a21442022-02-04 13:43:00 -08003980 /* Allow DFS channels marked as NO_P2P_SUPP to be used with
3981 * driver offloaded DFS. */
3982 if ((o->p2p == NO_P2P_SUPP &&
3983 (!is_dfs_global_op_class(o->op_class) ||
3984 !wpa_s->p2p_go_allow_dfs)) ||
Hai Shalom899fcc72020-10-19 14:38:18 -07003985 (is_6ghz_op_class(o->op_class) &&
3986 wpa_s->conf->p2p_6ghz_disable))
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08003987 continue;
3988
Hai Shalomc1a21442022-02-04 13:43:00 -08003989 if (is_6ghz_op_class(o->op_class) && o->bw == BW40 &&
3990 get_6ghz_sec_channel(channel) < 0)
3991 chan = channel - 4;
3992
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003993 for (ch = o->min_chan; ch <= o->max_chan; ch += o->inc) {
3994 if (o->mode != HOSTAPD_MODE_IEEE80211A ||
Hai Shalomc1a21442022-02-04 13:43:00 -08003995 (o->bw != BW40PLUS && o->bw != BW40MINUS &&
3996 o->bw != BW40) ||
3997 ch != chan)
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003998 continue;
Hai Shalomc1a21442022-02-04 13:43:00 -08003999 ret = wpas_p2p_verify_channel(wpa_s, mode, o->op_class,
4000 ch, o->bw);
4001 if (ret == ALLOWED) {
4002 if (is_6ghz_op_class(o->op_class) &&
4003 o->bw == BW40)
4004 return get_6ghz_sec_channel(channel);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004005 return (o->bw == BW40MINUS) ? -1 : 1;
Hai Shalomc1a21442022-02-04 13:43:00 -08004006 }
4007 if (ret == RADAR && wpa_s->p2p_go_allow_dfs) {
4008 /* Allow RADAR channels used for driver
4009 * offloaded DFS */
4010 return (o->bw == BW40MINUS) ? -1 : 1;
4011 }
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004012 }
4013 }
4014 return 0;
4015}
4016
4017
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004018int wpas_p2p_get_vht80_center(struct wpa_supplicant *wpa_s,
Hai Shalomc1a21442022-02-04 13:43:00 -08004019 struct hostapd_hw_modes *mode, u8 channel,
4020 u8 op_class)
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004021{
Hai Shalomc1a21442022-02-04 13:43:00 -08004022 const u8 *chans;
4023 size_t num_chans;
4024 enum chan_allowed ret;
4025
4026 ret = wpas_p2p_verify_channel(wpa_s, mode, op_class, channel, BW80);
4027 if (!(ret == ALLOWED || (ret == RADAR && wpa_s->p2p_go_allow_dfs)))
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004028 return 0;
4029
Hai Shalomc1a21442022-02-04 13:43:00 -08004030 if (is_6ghz_op_class(op_class)) {
4031 chans = center_channels_6ghz_80mhz;
4032 num_chans = ARRAY_SIZE(center_channels_6ghz_80mhz);
4033 } else {
4034 chans = center_channels_5ghz_80mhz;
4035 num_chans = ARRAY_SIZE(center_channels_5ghz_80mhz);
4036 }
4037 return wpas_p2p_get_center_80mhz(wpa_s, mode, channel,
4038 chans, num_chans);
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004039}
4040
4041
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08004042int wpas_p2p_get_vht160_center(struct wpa_supplicant *wpa_s,
Hai Shalomc1a21442022-02-04 13:43:00 -08004043 struct hostapd_hw_modes *mode, u8 channel,
4044 u8 op_class)
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08004045{
Hai Shalomc1a21442022-02-04 13:43:00 -08004046 const u8 *chans;
4047 size_t num_chans;
4048 enum chan_allowed ret;
4049
4050 ret = wpas_p2p_verify_channel(wpa_s, mode, op_class, channel, BW160);
4051 if (!(ret == ALLOWED || (ret == RADAR && wpa_s->p2p_go_allow_dfs)))
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08004052 return 0;
Hai Shalomc1a21442022-02-04 13:43:00 -08004053 if (is_6ghz_op_class(op_class)) {
4054 chans = center_channels_6ghz_160mhz;
4055 num_chans = ARRAY_SIZE(center_channels_6ghz_160mhz);
4056 } else {
4057 chans = center_channels_5ghz_160mhz;
4058 num_chans = ARRAY_SIZE(center_channels_5ghz_160mhz);
4059 }
4060 return wpas_p2p_get_center_160mhz(wpa_s, mode, channel,
4061 chans, num_chans);
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08004062}
4063
4064
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004065static int wpas_get_noa(void *ctx, const u8 *interface_addr, u8 *buf,
4066 size_t buf_len)
4067{
4068 struct wpa_supplicant *wpa_s = ctx;
4069
4070 for (wpa_s = wpa_s->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
4071 if (os_memcmp(wpa_s->own_addr, interface_addr, ETH_ALEN) == 0)
4072 break;
4073 }
4074 if (wpa_s == NULL)
4075 return -1;
4076
4077 return wpa_drv_get_noa(wpa_s, buf, buf_len);
4078}
4079
4080
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07004081struct wpa_supplicant * wpas_get_p2p_go_iface(struct wpa_supplicant *wpa_s,
4082 const u8 *ssid, size_t ssid_len)
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08004083{
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07004084 for (wpa_s = wpa_s->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
4085 struct wpa_ssid *s = wpa_s->current_ssid;
4086 if (s == NULL)
4087 continue;
4088 if (s->mode != WPAS_MODE_P2P_GO &&
4089 s->mode != WPAS_MODE_AP &&
4090 s->mode != WPAS_MODE_P2P_GROUP_FORMATION)
4091 continue;
4092 if (s->ssid_len != ssid_len ||
Dmitry Shmidt03658832014-08-13 11:03:49 -07004093 os_memcmp(ssid, s->ssid, ssid_len) != 0)
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07004094 continue;
4095 return wpa_s;
4096 }
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08004097
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07004098 return NULL;
4099
4100}
4101
4102
4103struct wpa_supplicant * wpas_get_p2p_client_iface(struct wpa_supplicant *wpa_s,
4104 const u8 *peer_dev_addr)
4105{
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08004106 for (wpa_s = wpa_s->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
4107 struct wpa_ssid *ssid = wpa_s->current_ssid;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004108 if (ssid && (ssid->mode != WPAS_MODE_INFRA || !ssid->p2p_group))
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08004109 continue;
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07004110 if (os_memcmp(wpa_s->go_dev_addr, peer_dev_addr, ETH_ALEN) == 0)
4111 return wpa_s;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08004112 }
4113
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07004114 return NULL;
4115}
4116
4117
4118static int wpas_go_connected(void *ctx, const u8 *dev_addr)
4119{
4120 struct wpa_supplicant *wpa_s = ctx;
4121
4122 return wpas_get_p2p_client_iface(wpa_s, dev_addr) != NULL;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08004123}
4124
4125
Dmitry Shmidt18463232014-01-24 12:29:41 -08004126static int wpas_is_concurrent_session_active(void *ctx)
4127{
4128 struct wpa_supplicant *wpa_s = ctx;
4129 struct wpa_supplicant *ifs;
4130
4131 for (ifs = wpa_s->global->ifaces; ifs; ifs = ifs->next) {
4132 if (ifs == wpa_s)
4133 continue;
4134 if (ifs->wpa_state > WPA_ASSOCIATED)
4135 return 1;
4136 }
4137 return 0;
4138}
4139
4140
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07004141static void wpas_p2p_debug_print(void *ctx, int level, const char *msg)
4142{
4143 struct wpa_supplicant *wpa_s = ctx;
4144 wpa_msg_global(wpa_s, level, "P2P: %s", msg);
4145}
4146
4147
Dmitry Shmidt9ead16e2014-10-07 13:15:23 -07004148int wpas_p2p_add_p2pdev_interface(struct wpa_supplicant *wpa_s,
4149 const char *conf_p2p_dev)
Dmitry Shmidt34af3062013-07-11 10:46:32 -07004150{
4151 struct wpa_interface iface;
4152 struct wpa_supplicant *p2pdev_wpa_s;
4153 char ifname[100];
4154 char force_name[100];
4155 int ret;
Hai Shalom60840252021-02-19 19:02:11 -08004156 const u8 *if_addr = NULL;
Dmitry Shmidt34af3062013-07-11 10:46:32 -07004157
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004158 ret = os_snprintf(ifname, sizeof(ifname), P2P_MGMT_DEVICE_PREFIX "%s",
4159 wpa_s->ifname);
4160 if (os_snprintf_error(sizeof(ifname), ret))
4161 return -1;
Hai Shalom899fcc72020-10-19 14:38:18 -07004162 /* Cut length at the maximum size. Note that we don't need to ensure
4163 * collision free names here as the created interface is not a netdev.
4164 */
4165 ifname[IFNAMSIZ - 1] = '\0';
Dmitry Shmidt34af3062013-07-11 10:46:32 -07004166 force_name[0] = '\0';
4167 wpa_s->pending_interface_type = WPA_IF_P2P_DEVICE;
Hai Shalom60840252021-02-19 19:02:11 -08004168
4169 if (wpa_s->conf->p2p_device_random_mac_addr == 2 &&
4170 !is_zero_ether_addr(wpa_s->conf->p2p_device_persistent_mac_addr))
4171 if_addr = wpa_s->conf->p2p_device_persistent_mac_addr;
4172
4173 ret = wpa_drv_if_add(wpa_s, WPA_IF_P2P_DEVICE, ifname, if_addr, NULL,
Dmitry Shmidt34af3062013-07-11 10:46:32 -07004174 force_name, wpa_s->pending_interface_addr, NULL);
4175 if (ret < 0) {
4176 wpa_printf(MSG_DEBUG, "P2P: Failed to create P2P Device interface");
4177 return ret;
4178 }
4179 os_strlcpy(wpa_s->pending_interface_name, ifname,
4180 sizeof(wpa_s->pending_interface_name));
4181
4182 os_memset(&iface, 0, sizeof(iface));
4183 iface.p2p_mgmt = 1;
4184 iface.ifname = wpa_s->pending_interface_name;
4185 iface.driver = wpa_s->driver->name;
4186 iface.driver_param = wpa_s->conf->driver_param;
Dmitry Shmidt2ac5f602014-03-07 10:08:21 -08004187
4188 /*
4189 * If a P2P Device configuration file was given, use it as the interface
4190 * configuration file (instead of using parent's configuration file.
4191 */
Dmitry Shmidt9ead16e2014-10-07 13:15:23 -07004192 if (conf_p2p_dev) {
4193 iface.confname = conf_p2p_dev;
Dmitry Shmidt2ac5f602014-03-07 10:08:21 -08004194 iface.ctrl_interface = NULL;
4195 } else {
4196 iface.confname = wpa_s->confname;
4197 iface.ctrl_interface = wpa_s->conf->ctrl_interface;
4198 }
Dmitry Shmidt2ac5f602014-03-07 10:08:21 -08004199
Dmitry Shmidt203eadb2015-03-05 14:16:04 -08004200 p2pdev_wpa_s = wpa_supplicant_add_iface(wpa_s->global, &iface, wpa_s);
Dmitry Shmidt34af3062013-07-11 10:46:32 -07004201 if (!p2pdev_wpa_s) {
4202 wpa_printf(MSG_DEBUG, "P2P: Failed to add P2P Device interface");
4203 return -1;
4204 }
Dmitry Shmidt34af3062013-07-11 10:46:32 -07004205
Dmitry Shmidt9c175262016-03-03 10:20:07 -08004206 p2pdev_wpa_s->p2pdev = p2pdev_wpa_s;
Dmitry Shmidt34af3062013-07-11 10:46:32 -07004207 wpa_s->pending_interface_name[0] = '\0';
4208 return 0;
4209}
4210
4211
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08004212static void wpas_presence_resp(void *ctx, const u8 *src, u8 status,
4213 const u8 *noa, size_t noa_len)
4214{
4215 struct wpa_supplicant *wpa_s, *intf = ctx;
4216 char hex[100];
4217
4218 for (wpa_s = intf->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
4219 if (wpa_s->waiting_presence_resp)
4220 break;
4221 }
4222 if (!wpa_s) {
4223 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: No group interface was waiting for presence response");
4224 return;
4225 }
4226 wpa_s->waiting_presence_resp = 0;
4227
4228 wpa_snprintf_hex(hex, sizeof(hex), noa, noa_len);
4229 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_PRESENCE_RESPONSE "src=" MACSTR
4230 " status=%u noa=%s", MAC2STR(src), status, hex);
4231}
4232
4233
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004234static int wpas_get_persistent_group(void *ctx, const u8 *addr, const u8 *ssid,
4235 size_t ssid_len, u8 *go_dev_addr,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004236 u8 *ret_ssid, size_t *ret_ssid_len,
4237 u8 *intended_iface_addr)
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004238{
4239 struct wpa_supplicant *wpa_s = ctx;
4240 struct wpa_ssid *s;
4241
4242 s = wpas_p2p_get_persistent(wpa_s, addr, ssid, ssid_len);
4243 if (s) {
4244 os_memcpy(ret_ssid, s->ssid, s->ssid_len);
4245 *ret_ssid_len = s->ssid_len;
4246 os_memcpy(go_dev_addr, s->bssid, ETH_ALEN);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004247
4248 if (s->mode != WPAS_MODE_P2P_GO) {
4249 os_memset(intended_iface_addr, 0, ETH_ALEN);
4250 } else if (wpas_p2p_create_iface(wpa_s)) {
4251 if (wpas_p2p_add_group_interface(wpa_s, WPA_IF_P2P_GO))
4252 return 0;
4253
4254 os_memcpy(intended_iface_addr,
4255 wpa_s->pending_interface_addr, ETH_ALEN);
4256 } else {
4257 os_memcpy(intended_iface_addr, wpa_s->own_addr,
4258 ETH_ALEN);
4259 }
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004260 return 1;
4261 }
4262
4263 return 0;
4264}
4265
4266
4267static int wpas_get_go_info(void *ctx, u8 *intended_addr,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004268 u8 *ssid, size_t *ssid_len, int *group_iface,
4269 unsigned int *freq)
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004270{
4271 struct wpa_supplicant *wpa_s = ctx;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004272 struct wpa_supplicant *go;
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004273 struct wpa_ssid *s;
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004274
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004275 /*
4276 * group_iface will be set to 1 only if a dedicated interface for P2P
4277 * role is required. First, we try to reuse an active GO. However,
4278 * if it is not present, we will try to reactivate an existing
4279 * persistent group and set group_iface to 1, so the caller will know
4280 * that the pending interface should be used.
4281 */
4282 *group_iface = 0;
4283
4284 if (freq)
4285 *freq = 0;
4286
4287 go = wpas_p2p_get_go_group(wpa_s);
4288 if (!go) {
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004289 s = wpas_p2p_get_persistent_go(wpa_s);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004290 *group_iface = wpas_p2p_create_iface(wpa_s);
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004291 if (s)
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004292 os_memcpy(intended_addr, s->bssid, ETH_ALEN);
4293 else
4294 return 0;
4295 } else {
4296 s = go->current_ssid;
4297 os_memcpy(intended_addr, go->own_addr, ETH_ALEN);
4298 if (freq)
4299 *freq = go->assoc_freq;
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004300 }
4301
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004302 os_memcpy(ssid, s->ssid, s->ssid_len);
4303 *ssid_len = s->ssid_len;
4304
4305 return 1;
4306}
4307
4308
4309static int wpas_remove_stale_groups(void *ctx, const u8 *peer, const u8 *go,
4310 const u8 *ssid, size_t ssid_len)
4311{
4312 struct wpa_supplicant *wpa_s = ctx;
4313 struct wpa_ssid *s;
4314 int save_config = 0;
4315 size_t i;
4316
4317 /* Start with our first choice of Persistent Groups */
4318 while ((s = wpas_p2p_get_persistent(wpa_s, peer, NULL, 0))) {
4319 if (go && ssid && ssid_len &&
4320 s->ssid_len == ssid_len &&
4321 os_memcmp(go, s->bssid, ETH_ALEN) == 0 &&
4322 os_memcmp(ssid, s->ssid, ssid_len) == 0)
4323 break;
4324
4325 /* Remove stale persistent group */
4326 if (s->mode != WPAS_MODE_P2P_GO || s->num_p2p_clients <= 1) {
Hai Shalom74f70d42019-02-11 14:42:39 -08004327 wpa_dbg(wpa_s, MSG_DEBUG,
4328 "P2P: Remove stale persistent group id=%d",
4329 s->id);
4330 wpas_notify_persistent_group_removed(wpa_s, s);
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004331 wpa_config_remove_network(wpa_s->conf, s->id);
4332 save_config = 1;
4333 continue;
4334 }
4335
4336 for (i = 0; i < s->num_p2p_clients; i++) {
4337 if (os_memcmp(s->p2p_client_list + i * 2 * ETH_ALEN,
4338 peer, ETH_ALEN) != 0)
4339 continue;
4340
4341 os_memmove(s->p2p_client_list + i * 2 * ETH_ALEN,
4342 s->p2p_client_list + (i + 1) * 2 * ETH_ALEN,
4343 (s->num_p2p_clients - i - 1) * 2 * ETH_ALEN);
4344 break;
4345 }
4346 s->num_p2p_clients--;
4347 save_config = 1;
4348 }
4349
4350 if (save_config)
4351 p2p_config_write(wpa_s);
4352
4353 /* Return TRUE if valid SSID remains */
4354 return s != NULL;
4355}
4356
4357
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004358static void wpas_p2ps_get_feat_cap_str(char *buf, size_t buf_len,
4359 const u8 *feat_cap, size_t feat_cap_len)
4360{
4361 static const char pref[] = " feature_cap=";
4362 int ret;
4363
4364 buf[0] = '\0';
4365
4366 /*
4367 * We expect a feature capability to contain at least one byte to be
4368 * reported. The string buffer provided by the caller function is
4369 * expected to be big enough to contain all bytes of the attribute for
4370 * known specifications. This function truncates the reported bytes if
4371 * the feature capability data exceeds the string buffer size.
4372 */
4373 if (!feat_cap || !feat_cap_len || buf_len < sizeof(pref) + 2)
4374 return;
4375
4376 os_memcpy(buf, pref, sizeof(pref));
4377 ret = wpa_snprintf_hex(&buf[sizeof(pref) - 1],
4378 buf_len - sizeof(pref) + 1,
4379 feat_cap, feat_cap_len);
4380
4381 if (ret != (2 * (int) feat_cap_len))
4382 wpa_printf(MSG_WARNING, "P2PS feature_cap bytes truncated");
4383}
4384
4385
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004386static void wpas_p2ps_prov_complete(void *ctx, u8 status, const u8 *dev,
4387 const u8 *adv_mac, const u8 *ses_mac,
4388 const u8 *grp_mac, u32 adv_id, u32 ses_id,
4389 u8 conncap, int passwd_id,
4390 const u8 *persist_ssid,
4391 size_t persist_ssid_size, int response_done,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004392 int prov_start, const char *session_info,
4393 const u8 *feat_cap, size_t feat_cap_len,
Dmitry Shmidtde47be72016-01-07 12:52:55 -08004394 unsigned int freq,
4395 const u8 *group_ssid, size_t group_ssid_len)
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004396{
4397 struct wpa_supplicant *wpa_s = ctx;
4398 u8 mac[ETH_ALEN];
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004399 struct wpa_ssid *persistent_go, *stale, *s = NULL;
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004400 int save_config = 0;
4401 struct wpa_supplicant *go_wpa_s;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004402 char feat_cap_str[256];
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004403
4404 if (!dev)
4405 return;
4406
4407 os_memset(mac, 0, ETH_ALEN);
4408 if (!adv_mac)
4409 adv_mac = mac;
4410 if (!ses_mac)
4411 ses_mac = mac;
4412 if (!grp_mac)
4413 grp_mac = mac;
4414
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004415 wpas_p2ps_get_feat_cap_str(feat_cap_str, sizeof(feat_cap_str),
4416 feat_cap, feat_cap_len);
4417
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004418 if (prov_start) {
4419 if (session_info == NULL) {
4420 wpa_msg_global(wpa_s, MSG_INFO,
4421 P2P_EVENT_P2PS_PROVISION_START MACSTR
4422 " adv_id=%x conncap=%x"
4423 " adv_mac=" MACSTR
4424 " session=%x mac=" MACSTR
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004425 " dev_passwd_id=%d%s",
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004426 MAC2STR(dev), adv_id, conncap,
4427 MAC2STR(adv_mac),
4428 ses_id, MAC2STR(ses_mac),
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004429 passwd_id, feat_cap_str);
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004430 } else {
4431 wpa_msg_global(wpa_s, MSG_INFO,
4432 P2P_EVENT_P2PS_PROVISION_START MACSTR
4433 " adv_id=%x conncap=%x"
4434 " adv_mac=" MACSTR
4435 " session=%x mac=" MACSTR
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004436 " dev_passwd_id=%d info='%s'%s",
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004437 MAC2STR(dev), adv_id, conncap,
4438 MAC2STR(adv_mac),
4439 ses_id, MAC2STR(ses_mac),
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004440 passwd_id, session_info, feat_cap_str);
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004441 }
4442 return;
4443 }
4444
4445 go_wpa_s = wpas_p2p_get_go_group(wpa_s);
4446 persistent_go = wpas_p2p_get_persistent_go(wpa_s);
4447
4448 if (status && status != P2P_SC_SUCCESS_DEFERRED) {
4449 if (go_wpa_s && !p2p_group_go_member_count(wpa_s))
4450 wpas_p2p_group_remove(wpa_s, go_wpa_s->ifname);
4451
4452 if (persistent_go && !persistent_go->num_p2p_clients) {
4453 /* remove empty persistent GO */
Hai Shalom74f70d42019-02-11 14:42:39 -08004454 wpa_dbg(wpa_s, MSG_DEBUG,
4455 "P2P: Remove empty persistent group id=%d",
4456 persistent_go->id);
4457 wpas_notify_persistent_group_removed(wpa_s,
4458 persistent_go);
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004459 wpa_config_remove_network(wpa_s->conf,
4460 persistent_go->id);
4461 }
4462
4463 wpa_msg_global(wpa_s, MSG_INFO,
4464 P2P_EVENT_P2PS_PROVISION_DONE MACSTR
4465 " status=%d"
4466 " adv_id=%x adv_mac=" MACSTR
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004467 " session=%x mac=" MACSTR "%s",
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004468 MAC2STR(dev), status,
4469 adv_id, MAC2STR(adv_mac),
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004470 ses_id, MAC2STR(ses_mac), feat_cap_str);
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004471 return;
4472 }
4473
4474 /* Clean up stale persistent groups with this device */
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004475 if (persist_ssid && persist_ssid_size)
4476 s = wpas_p2p_get_persistent(wpa_s, dev, persist_ssid,
4477 persist_ssid_size);
4478
4479 if (persist_ssid && s && s->mode != WPAS_MODE_P2P_GO &&
4480 is_zero_ether_addr(grp_mac)) {
4481 wpa_dbg(wpa_s, MSG_ERROR,
4482 "P2P: Peer device is a GO in a persistent group, but it did not provide the intended MAC address");
4483 return;
4484 }
4485
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004486 for (;;) {
4487 stale = wpas_p2p_get_persistent(wpa_s, dev, NULL, 0);
4488 if (!stale)
4489 break;
4490
4491 if (s && s->ssid_len == stale->ssid_len &&
4492 os_memcmp(stale->bssid, s->bssid, ETH_ALEN) == 0 &&
4493 os_memcmp(stale->ssid, s->ssid, s->ssid_len) == 0)
4494 break;
4495
4496 /* Remove stale persistent group */
4497 if (stale->mode != WPAS_MODE_P2P_GO ||
4498 stale->num_p2p_clients <= 1) {
Hai Shalom74f70d42019-02-11 14:42:39 -08004499 wpa_dbg(wpa_s, MSG_DEBUG,
4500 "P2P: Remove stale persistent group id=%d",
4501 stale->id);
4502 wpas_notify_persistent_group_removed(wpa_s, stale);
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004503 wpa_config_remove_network(wpa_s->conf, stale->id);
4504 } else {
4505 size_t i;
4506
4507 for (i = 0; i < stale->num_p2p_clients; i++) {
4508 if (os_memcmp(stale->p2p_client_list +
4509 i * ETH_ALEN,
4510 dev, ETH_ALEN) == 0) {
4511 os_memmove(stale->p2p_client_list +
4512 i * ETH_ALEN,
4513 stale->p2p_client_list +
4514 (i + 1) * ETH_ALEN,
4515 (stale->num_p2p_clients -
4516 i - 1) * ETH_ALEN);
4517 break;
4518 }
4519 }
4520 stale->num_p2p_clients--;
4521 }
4522 save_config = 1;
4523 }
4524
4525 if (save_config)
4526 p2p_config_write(wpa_s);
4527
4528 if (s) {
4529 if (go_wpa_s && !p2p_group_go_member_count(wpa_s))
4530 wpas_p2p_group_remove(wpa_s, go_wpa_s->ifname);
4531
4532 if (persistent_go && s != persistent_go &&
4533 !persistent_go->num_p2p_clients) {
4534 /* remove empty persistent GO */
Hai Shalom74f70d42019-02-11 14:42:39 -08004535 wpa_dbg(wpa_s, MSG_DEBUG,
4536 "P2P: Remove empty persistent group id=%d",
4537 persistent_go->id);
4538 wpas_notify_persistent_group_removed(wpa_s,
4539 persistent_go);
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004540 wpa_config_remove_network(wpa_s->conf,
4541 persistent_go->id);
4542 /* Save config */
4543 }
4544
4545 wpa_msg_global(wpa_s, MSG_INFO,
4546 P2P_EVENT_P2PS_PROVISION_DONE MACSTR
4547 " status=%d"
4548 " adv_id=%x adv_mac=" MACSTR
4549 " session=%x mac=" MACSTR
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004550 " persist=%d%s",
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004551 MAC2STR(dev), status,
4552 adv_id, MAC2STR(adv_mac),
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004553 ses_id, MAC2STR(ses_mac), s->id, feat_cap_str);
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004554 return;
4555 }
4556
Hai Shalom5f92bc92019-04-18 11:54:11 -07004557 wpa_s->global->pending_p2ps_group = 0;
4558 wpa_s->global->pending_p2ps_group_freq = 0;
4559
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004560 if (conncap == P2PS_SETUP_GROUP_OWNER) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004561 /*
4562 * We need to copy the interface name. Simply saving a
4563 * pointer isn't enough, since if we use pending_interface_name
4564 * it will be overwritten when the group is added.
4565 */
4566 char go_ifname[100];
4567
4568 go_ifname[0] = '\0';
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004569 if (!go_wpa_s) {
Hai Shalom5f92bc92019-04-18 11:54:11 -07004570 if (!response_done) {
4571 wpa_s->global->pending_p2ps_group = 1;
4572 wpa_s->global->pending_p2ps_group_freq = freq;
4573 }
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004574
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004575 if (!wpas_p2p_create_iface(wpa_s))
4576 os_memcpy(go_ifname, wpa_s->ifname,
4577 sizeof(go_ifname));
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004578 else if (wpa_s->pending_interface_name[0])
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004579 os_memcpy(go_ifname,
4580 wpa_s->pending_interface_name,
4581 sizeof(go_ifname));
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004582
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004583 if (!go_ifname[0]) {
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004584 wpas_p2ps_prov_complete(
4585 wpa_s, P2P_SC_FAIL_UNKNOWN_GROUP,
4586 dev, adv_mac, ses_mac,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004587 grp_mac, adv_id, ses_id, 0, 0,
Dmitry Shmidtde47be72016-01-07 12:52:55 -08004588 NULL, 0, 0, 0, NULL, NULL, 0, 0,
4589 NULL, 0);
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004590 return;
4591 }
4592
4593 /* If PD Resp complete, start up the GO */
4594 if (response_done && persistent_go) {
4595 wpas_p2p_group_add_persistent(
4596 wpa_s, persistent_go,
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08004597 0, 0, freq, 0, 0, 0, 0, 0, 0, NULL,
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004598 persistent_go->mode ==
4599 WPAS_MODE_P2P_GO ?
4600 P2P_MAX_INITIAL_CONN_WAIT_GO_REINVOKE :
Hai Shalomc1a21442022-02-04 13:43:00 -08004601 0, 0, false);
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004602 } else if (response_done) {
Hai Shalom74f70d42019-02-11 14:42:39 -08004603 wpas_p2p_group_add(wpa_s, 1, freq,
Hai Shalomc1a21442022-02-04 13:43:00 -08004604 0, 0, 0, 0, 0, 0, false);
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004605 }
4606
4607 if (passwd_id == DEV_PW_P2PS_DEFAULT) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004608 os_memcpy(wpa_s->p2ps_join_addr, grp_mac,
4609 ETH_ALEN);
4610 wpa_s->p2ps_method_config_any = 1;
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004611 }
4612 } else if (passwd_id == DEV_PW_P2PS_DEFAULT) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004613 os_memcpy(go_ifname, go_wpa_s->ifname,
4614 sizeof(go_ifname));
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004615
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004616 if (is_zero_ether_addr(grp_mac)) {
4617 wpa_dbg(go_wpa_s, MSG_DEBUG,
4618 "P2P: Setting PIN-1 for ANY");
4619 wpa_supplicant_ap_wps_pin(go_wpa_s, NULL,
4620 "12345670", NULL, 0,
4621 0);
4622 } else {
4623 wpa_dbg(go_wpa_s, MSG_DEBUG,
4624 "P2P: Setting PIN-1 for " MACSTR,
4625 MAC2STR(grp_mac));
4626 wpa_supplicant_ap_wps_pin(go_wpa_s, grp_mac,
4627 "12345670", NULL, 0,
4628 0);
4629 }
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004630
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004631 os_memcpy(wpa_s->p2ps_join_addr, grp_mac, ETH_ALEN);
4632 wpa_s->p2ps_method_config_any = 1;
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004633 }
4634
4635 wpa_msg_global(wpa_s, MSG_INFO,
4636 P2P_EVENT_P2PS_PROVISION_DONE MACSTR
4637 " status=%d conncap=%x"
4638 " adv_id=%x adv_mac=" MACSTR
4639 " session=%x mac=" MACSTR
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004640 " dev_passwd_id=%d go=%s%s",
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004641 MAC2STR(dev), status, conncap,
4642 adv_id, MAC2STR(adv_mac),
4643 ses_id, MAC2STR(ses_mac),
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004644 passwd_id, go_ifname, feat_cap_str);
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004645 return;
4646 }
4647
4648 if (go_wpa_s && !p2p_group_go_member_count(wpa_s))
4649 wpas_p2p_group_remove(wpa_s, go_wpa_s->ifname);
4650
4651 if (persistent_go && !persistent_go->num_p2p_clients) {
4652 /* remove empty persistent GO */
Hai Shalom74f70d42019-02-11 14:42:39 -08004653 wpa_dbg(wpa_s, MSG_DEBUG,
4654 "P2P: Remove empty persistent group id=%d",
4655 persistent_go->id);
4656 wpas_notify_persistent_group_removed(wpa_s, persistent_go);
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004657 wpa_config_remove_network(wpa_s->conf, persistent_go->id);
4658 }
4659
4660 if (conncap == P2PS_SETUP_CLIENT) {
Dmitry Shmidtde47be72016-01-07 12:52:55 -08004661 char ssid_hex[32 * 2 + 1];
4662
4663 if (group_ssid)
4664 wpa_snprintf_hex(ssid_hex, sizeof(ssid_hex),
4665 group_ssid, group_ssid_len);
4666 else
4667 ssid_hex[0] = '\0';
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004668 wpa_msg_global(wpa_s, MSG_INFO,
4669 P2P_EVENT_P2PS_PROVISION_DONE MACSTR
4670 " status=%d conncap=%x"
4671 " adv_id=%x adv_mac=" MACSTR
4672 " session=%x mac=" MACSTR
Dmitry Shmidtde47be72016-01-07 12:52:55 -08004673 " dev_passwd_id=%d join=" MACSTR "%s%s%s",
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004674 MAC2STR(dev), status, conncap,
4675 adv_id, MAC2STR(adv_mac),
4676 ses_id, MAC2STR(ses_mac),
Dmitry Shmidtde47be72016-01-07 12:52:55 -08004677 passwd_id, MAC2STR(grp_mac), feat_cap_str,
4678 group_ssid ? " group_ssid=" : "", ssid_hex);
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004679 } else {
4680 wpa_msg_global(wpa_s, MSG_INFO,
4681 P2P_EVENT_P2PS_PROVISION_DONE MACSTR
4682 " status=%d conncap=%x"
4683 " adv_id=%x adv_mac=" MACSTR
4684 " session=%x mac=" MACSTR
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004685 " dev_passwd_id=%d%s",
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004686 MAC2STR(dev), status, conncap,
4687 adv_id, MAC2STR(adv_mac),
4688 ses_id, MAC2STR(ses_mac),
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004689 passwd_id, feat_cap_str);
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004690 }
4691}
4692
4693
Dmitry Shmidt5a1480c2014-05-12 09:46:02 -07004694static int _wpas_p2p_in_progress(void *ctx)
4695{
4696 struct wpa_supplicant *wpa_s = ctx;
4697 return wpas_p2p_in_progress(wpa_s);
4698}
4699
4700
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004701static int wpas_prov_disc_resp_cb(void *ctx)
4702{
4703 struct wpa_supplicant *wpa_s = ctx;
4704 struct wpa_ssid *persistent_go;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004705 unsigned int freq;
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004706
4707 if (!wpa_s->global->pending_p2ps_group)
4708 return 0;
4709
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004710 freq = wpa_s->global->pending_p2ps_group_freq;
4711 wpa_s->global->pending_p2ps_group_freq = 0;
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004712 wpa_s->global->pending_p2ps_group = 0;
4713
4714 if (wpas_p2p_get_go_group(wpa_s))
4715 return 0;
4716 persistent_go = wpas_p2p_get_persistent_go(wpa_s);
4717
4718 if (persistent_go) {
4719 wpas_p2p_group_add_persistent(
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08004720 wpa_s, persistent_go, 0, 0, 0, 0, 0, 0, 0, 0, 0,
4721 NULL,
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004722 persistent_go->mode == WPAS_MODE_P2P_GO ?
Hai Shalomc1a21442022-02-04 13:43:00 -08004723 P2P_MAX_INITIAL_CONN_WAIT_GO_REINVOKE : 0, 0,
4724 is_p2p_allow_6ghz(wpa_s->global->p2p));
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004725 } else {
Hai Shalomc1a21442022-02-04 13:43:00 -08004726 wpas_p2p_group_add(wpa_s, 1, freq, 0, 0, 0, 0, 0, 0,
4727 is_p2p_allow_6ghz(wpa_s->global->p2p));
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004728 }
4729
4730 return 1;
4731}
4732
4733
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004734static int wpas_p2p_get_pref_freq_list(void *ctx, int go,
4735 unsigned int *len,
Sunil8cd6f4d2022-06-28 18:40:46 +00004736 struct weighted_pcl *freq_list)
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004737{
4738 struct wpa_supplicant *wpa_s = ctx;
4739
4740 return wpa_drv_get_pref_freq_list(wpa_s, go ? WPA_IF_P2P_GO :
4741 WPA_IF_P2P_CLIENT, len, freq_list);
4742}
4743
Jimmy Chenf887c7b2018-11-13 15:19:57 +08004744int wpas_p2p_mac_setup(struct wpa_supplicant *wpa_s)
4745{
4746 u8 addr[ETH_ALEN] = {0};
4747
4748 if (wpa_s->conf->p2p_device_random_mac_addr == 0)
4749 return 0;
4750
Hai Shalom60840252021-02-19 19:02:11 -08004751 if (wpa_s->conf->p2p_device_random_mac_addr == 2) {
4752 if (is_zero_ether_addr(
4753 wpa_s->conf->p2p_device_persistent_mac_addr) &&
4754 !is_zero_ether_addr(wpa_s->own_addr)) {
4755 os_memcpy(wpa_s->conf->p2p_device_persistent_mac_addr,
4756 wpa_s->own_addr, ETH_ALEN);
4757 }
4758 return 0;
4759 }
4760
4761 if (!wpa_s->conf->ssid) {
Jimmy Chenf887c7b2018-11-13 15:19:57 +08004762 if (random_mac_addr(addr) < 0) {
4763 wpa_msg(wpa_s, MSG_INFO,
4764 "Failed to generate random MAC address");
4765 return -EINVAL;
4766 }
4767
Hai Shalom39ba6fc2019-01-22 12:40:38 -08004768 /* Store generated MAC address. */
4769 os_memcpy(wpa_s->conf->p2p_device_persistent_mac_addr, addr,
4770 ETH_ALEN);
Jimmy Chenf887c7b2018-11-13 15:19:57 +08004771 } else {
Hai Shalom39ba6fc2019-01-22 12:40:38 -08004772 /* If there are existing saved groups, restore last MAC address.
4773 * if there is no last used MAC address, the last one is
4774 * factory MAC. */
4775 if (is_zero_ether_addr(
4776 wpa_s->conf->p2p_device_persistent_mac_addr))
Jimmy Chenf887c7b2018-11-13 15:19:57 +08004777 return 0;
Hai Shalom39ba6fc2019-01-22 12:40:38 -08004778 os_memcpy(addr, wpa_s->conf->p2p_device_persistent_mac_addr,
4779 ETH_ALEN);
Jimmy Chenf887c7b2018-11-13 15:19:57 +08004780 wpa_msg(wpa_s, MSG_DEBUG, "Restore last used MAC address.");
4781 }
4782
4783 if (wpa_drv_set_mac_addr(wpa_s, addr) < 0) {
4784 wpa_msg(wpa_s, MSG_INFO,
4785 "Failed to set random MAC address");
4786 return -EINVAL;
4787 }
4788
4789 if (wpa_supplicant_update_mac_addr(wpa_s) < 0) {
4790 wpa_msg(wpa_s, MSG_INFO,
4791 "Could not update MAC address information");
4792 return -EINVAL;
4793 }
4794
4795 wpa_msg(wpa_s, MSG_DEBUG, "Using random MAC address " MACSTR,
4796 MAC2STR(addr));
4797
4798 return 0;
4799}
4800
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004801/**
4802 * wpas_p2p_init - Initialize P2P module for %wpa_supplicant
4803 * @global: Pointer to global data from wpa_supplicant_init()
4804 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
4805 * Returns: 0 on success, -1 on failure
4806 */
4807int wpas_p2p_init(struct wpa_global *global, struct wpa_supplicant *wpa_s)
4808{
4809 struct p2p_config p2p;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004810 int i;
4811
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07004812 if (wpa_s->conf->p2p_disabled)
4813 return 0;
4814
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004815 if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_CAPABLE))
4816 return 0;
4817
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004818 if (global->p2p)
4819 return 0;
4820
Jimmy Chenf887c7b2018-11-13 15:19:57 +08004821 if (wpas_p2p_mac_setup(wpa_s) < 0) {
4822 wpa_msg(wpa_s, MSG_ERROR,
4823 "Failed to initialize P2P random MAC address.");
4824 return -1;
4825 }
4826
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004827 os_memset(&p2p, 0, sizeof(p2p));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004828 p2p.cb_ctx = wpa_s;
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07004829 p2p.debug_print = wpas_p2p_debug_print;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004830 p2p.p2p_scan = wpas_p2p_scan;
4831 p2p.send_action = wpas_send_action;
4832 p2p.send_action_done = wpas_send_action_done;
4833 p2p.go_neg_completed = wpas_go_neg_completed;
4834 p2p.go_neg_req_rx = wpas_go_neg_req_rx;
4835 p2p.dev_found = wpas_dev_found;
4836 p2p.dev_lost = wpas_dev_lost;
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07004837 p2p.find_stopped = wpas_find_stopped;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004838 p2p.start_listen = wpas_start_listen;
4839 p2p.stop_listen = wpas_stop_listen;
4840 p2p.send_probe_resp = wpas_send_probe_resp;
4841 p2p.sd_request = wpas_sd_request;
4842 p2p.sd_response = wpas_sd_response;
4843 p2p.prov_disc_req = wpas_prov_disc_req;
4844 p2p.prov_disc_resp = wpas_prov_disc_resp;
Jouni Malinen75ecf522011-06-27 15:19:46 -07004845 p2p.prov_disc_fail = wpas_prov_disc_fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004846 p2p.invitation_process = wpas_invitation_process;
4847 p2p.invitation_received = wpas_invitation_received;
4848 p2p.invitation_result = wpas_invitation_result;
4849 p2p.get_noa = wpas_get_noa;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08004850 p2p.go_connected = wpas_go_connected;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08004851 p2p.presence_resp = wpas_presence_resp;
Dmitry Shmidt18463232014-01-24 12:29:41 -08004852 p2p.is_concurrent_session_active = wpas_is_concurrent_session_active;
Dmitry Shmidt5a1480c2014-05-12 09:46:02 -07004853 p2p.is_p2p_in_progress = _wpas_p2p_in_progress;
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004854 p2p.get_persistent_group = wpas_get_persistent_group;
4855 p2p.get_go_info = wpas_get_go_info;
4856 p2p.remove_stale_groups = wpas_remove_stale_groups;
4857 p2p.p2ps_prov_complete = wpas_p2ps_prov_complete;
4858 p2p.prov_disc_resp_cb = wpas_prov_disc_resp_cb;
4859 p2p.p2ps_group_capability = p2ps_group_capability;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004860 p2p.get_pref_freq_list = wpas_p2p_get_pref_freq_list;
Hai Shalom60840252021-02-19 19:02:11 -08004861 p2p.p2p_6ghz_disable = wpa_s->conf->p2p_6ghz_disable;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004862
4863 os_memcpy(wpa_s->global->p2p_dev_addr, wpa_s->own_addr, ETH_ALEN);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004864 os_memcpy(p2p.dev_addr, wpa_s->global->p2p_dev_addr, ETH_ALEN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004865 p2p.dev_name = wpa_s->conf->device_name;
4866 p2p.manufacturer = wpa_s->conf->manufacturer;
4867 p2p.model_name = wpa_s->conf->model_name;
4868 p2p.model_number = wpa_s->conf->model_number;
4869 p2p.serial_number = wpa_s->conf->serial_number;
4870 if (wpa_s->wps) {
4871 os_memcpy(p2p.uuid, wpa_s->wps->uuid, 16);
4872 p2p.config_methods = wpa_s->wps->config_methods;
4873 }
4874
Hai Shalom60840252021-02-19 19:02:11 -08004875 if (wpas_p2p_setup_channels(wpa_s, &p2p.channels, &p2p.cli_channels,
4876 p2p.p2p_6ghz_disable)) {
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07004877 wpa_printf(MSG_ERROR,
4878 "P2P: Failed to configure supported channel list");
4879 return -1;
4880 }
4881
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004882 if (wpa_s->conf->p2p_listen_reg_class &&
4883 wpa_s->conf->p2p_listen_channel) {
4884 p2p.reg_class = wpa_s->conf->p2p_listen_reg_class;
4885 p2p.channel = wpa_s->conf->p2p_listen_channel;
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07004886 p2p.channel_forced = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004887 } else {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004888 /*
4889 * Pick one of the social channels randomly as the listen
4890 * channel.
4891 */
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07004892 if (p2p_config_get_random_social(&p2p, &p2p.reg_class,
Hai Shalom74f70d42019-02-11 14:42:39 -08004893 &p2p.channel,
4894 &global->p2p_go_avoid_freq,
4895 &global->p2p_disallow_freq) !=
4896 0) {
Dmitry Shmidt1d755d02015-04-28 10:34:29 -07004897 wpa_printf(MSG_INFO,
4898 "P2P: No social channels supported by the driver - do not enable P2P");
4899 return 0;
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07004900 }
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07004901 p2p.channel_forced = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004902 }
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07004903 wpa_printf(MSG_DEBUG, "P2P: Own listen channel: %d:%d",
4904 p2p.reg_class, p2p.channel);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004905
4906 if (wpa_s->conf->p2p_oper_reg_class &&
4907 wpa_s->conf->p2p_oper_channel) {
4908 p2p.op_reg_class = wpa_s->conf->p2p_oper_reg_class;
4909 p2p.op_channel = wpa_s->conf->p2p_oper_channel;
4910 p2p.cfg_op_channel = 1;
4911 wpa_printf(MSG_DEBUG, "P2P: Configured operating channel: "
4912 "%d:%d", p2p.op_reg_class, p2p.op_channel);
4913
4914 } else {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004915 /*
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07004916 * Use random operation channel from 2.4 GHz band social
4917 * channels (1, 6, 11) or band 60 GHz social channel (2) if no
4918 * other preference is indicated.
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004919 */
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07004920 if (p2p_config_get_random_social(&p2p, &p2p.op_reg_class,
Hai Shalom74f70d42019-02-11 14:42:39 -08004921 &p2p.op_channel, NULL,
4922 NULL) != 0) {
4923 wpa_printf(MSG_INFO,
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07004924 "P2P: Failed to select random social channel as operation channel");
Hai Shalom74f70d42019-02-11 14:42:39 -08004925 p2p.op_reg_class = 0;
4926 p2p.op_channel = 0;
4927 /* This will be overridden during group setup in
4928 * p2p_prepare_channel(), so allow setup to continue. */
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07004929 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004930 p2p.cfg_op_channel = 0;
4931 wpa_printf(MSG_DEBUG, "P2P: Random operating channel: "
4932 "%d:%d", p2p.op_reg_class, p2p.op_channel);
4933 }
Dmitry Shmidt44c95782013-05-17 09:51:35 -07004934
4935 if (wpa_s->conf->p2p_pref_chan && wpa_s->conf->num_p2p_pref_chan) {
4936 p2p.pref_chan = wpa_s->conf->p2p_pref_chan;
4937 p2p.num_pref_chan = wpa_s->conf->num_p2p_pref_chan;
4938 }
4939
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004940 if (wpa_s->conf->country[0] && wpa_s->conf->country[1]) {
4941 os_memcpy(p2p.country, wpa_s->conf->country, 2);
4942 p2p.country[2] = 0x04;
4943 } else
4944 os_memcpy(p2p.country, "XX\x04", 3);
4945
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004946 os_memcpy(p2p.pri_dev_type, wpa_s->conf->device_type,
4947 WPS_DEV_TYPE_LEN);
4948
4949 p2p.num_sec_dev_types = wpa_s->conf->num_sec_device_types;
4950 os_memcpy(p2p.sec_dev_type, wpa_s->conf->sec_device_type,
4951 p2p.num_sec_dev_types * WPS_DEV_TYPE_LEN);
4952
4953 p2p.concurrent_operations = !!(wpa_s->drv_flags &
4954 WPA_DRIVER_FLAGS_P2P_CONCURRENT);
4955
4956 p2p.max_peers = 100;
4957
4958 if (wpa_s->conf->p2p_ssid_postfix) {
4959 p2p.ssid_postfix_len =
4960 os_strlen(wpa_s->conf->p2p_ssid_postfix);
4961 if (p2p.ssid_postfix_len > sizeof(p2p.ssid_postfix))
4962 p2p.ssid_postfix_len = sizeof(p2p.ssid_postfix);
4963 os_memcpy(p2p.ssid_postfix, wpa_s->conf->p2p_ssid_postfix,
4964 p2p.ssid_postfix_len);
4965 }
4966
4967 p2p.p2p_intra_bss = wpa_s->conf->p2p_intra_bss;
4968
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08004969 p2p.max_listen = wpa_s->max_remain_on_chan;
4970
Dmitry Shmidt2271d3f2014-06-23 12:16:31 -07004971 if (wpa_s->conf->p2p_passphrase_len >= 8 &&
4972 wpa_s->conf->p2p_passphrase_len <= 63)
4973 p2p.passphrase_len = wpa_s->conf->p2p_passphrase_len;
4974 else
4975 p2p.passphrase_len = 8;
4976
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004977 global->p2p = p2p_init(&p2p);
4978 if (global->p2p == NULL)
4979 return -1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07004980 global->p2p_init_wpa_s = wpa_s;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004981
4982 for (i = 0; i < MAX_WPS_VENDOR_EXT; i++) {
4983 if (wpa_s->conf->wps_vendor_ext[i] == NULL)
4984 continue;
4985 p2p_add_wps_vendor_extension(
4986 global->p2p, wpa_s->conf->wps_vendor_ext[i]);
4987 }
4988
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004989 p2p_set_no_go_freq(global->p2p, &wpa_s->conf->p2p_no_go_freq);
4990
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004991 return 0;
4992}
4993
4994
4995/**
4996 * wpas_p2p_deinit - Deinitialize per-interface P2P data
4997 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
4998 *
4999 * This function deinitialize per-interface P2P data.
5000 */
5001void wpas_p2p_deinit(struct wpa_supplicant *wpa_s)
5002{
5003 if (wpa_s->driver && wpa_s->drv_priv)
5004 wpa_drv_probe_req_report(wpa_s, 0);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005005
5006 if (wpa_s->go_params) {
5007 /* Clear any stored provisioning info */
5008 p2p_clear_provisioning_info(
5009 wpa_s->global->p2p,
Dmitry Shmidt04949592012-07-19 12:16:46 -07005010 wpa_s->go_params->peer_device_addr);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005011 }
5012
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005013 os_free(wpa_s->go_params);
5014 wpa_s->go_params = NULL;
Dmitry Shmidt684785c2014-05-12 13:34:29 -07005015 eloop_cancel_timeout(wpas_p2p_psk_failure_removal, wpa_s, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005016 eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s, NULL);
5017 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
Hai Shalomfdcde762020-04-02 11:19:20 -07005018 wpa_s->global->p2p_long_listen = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005019 eloop_cancel_timeout(wpas_p2p_long_listen_timeout, wpa_s, NULL);
5020 eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL);
5021 wpas_p2p_remove_pending_group_interface(wpa_s);
Dmitry Shmidt04f534e2013-12-09 15:50:16 -08005022 eloop_cancel_timeout(wpas_p2p_group_freq_conflict, wpa_s, NULL);
Hai Shalom899fcc72020-10-19 14:38:18 -07005023 eloop_cancel_timeout(wpas_p2p_reconsider_moving_go, wpa_s, NULL);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005024 wpas_p2p_listen_work_done(wpa_s);
5025 if (wpa_s->p2p_send_action_work) {
5026 os_free(wpa_s->p2p_send_action_work->ctx);
5027 radio_work_done(wpa_s->p2p_send_action_work);
5028 wpa_s->p2p_send_action_work = NULL;
5029 }
5030 eloop_cancel_timeout(wpas_p2p_send_action_work_timeout, wpa_s, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005031
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08005032 wpabuf_free(wpa_s->p2p_oob_dev_pw);
5033 wpa_s->p2p_oob_dev_pw = NULL;
5034
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005035 os_free(wpa_s->p2p_group_common_freqs);
5036 wpa_s->p2p_group_common_freqs = NULL;
5037 wpa_s->p2p_group_common_freqs_num = 0;
5038
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005039 /* TODO: remove group interface from the driver if this wpa_s instance
5040 * is on top of a P2P group interface */
5041}
5042
5043
5044/**
5045 * wpas_p2p_deinit_global - Deinitialize global P2P module
5046 * @global: Pointer to global data from wpa_supplicant_init()
5047 *
5048 * This function deinitializes the global (per device) P2P module.
5049 */
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07005050static void wpas_p2p_deinit_global(struct wpa_global *global)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005051{
5052 struct wpa_supplicant *wpa_s, *tmp;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005053
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005054 wpa_s = global->ifaces;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005055
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07005056 wpas_p2p_service_flush(global->p2p_init_wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005057
5058 /* Remove remaining P2P group interfaces */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005059 while (wpa_s && wpa_s->p2p_group_interface != NOT_P2P_GROUP_INTERFACE)
5060 wpa_s = wpa_s->next;
5061 while (wpa_s) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005062 tmp = global->ifaces;
5063 while (tmp &&
5064 (tmp == wpa_s ||
5065 tmp->p2p_group_interface == NOT_P2P_GROUP_INTERFACE)) {
5066 tmp = tmp->next;
5067 }
5068 if (tmp == NULL)
5069 break;
Dmitry Shmidt04949592012-07-19 12:16:46 -07005070 /* Disconnect from the P2P group and deinit the interface */
5071 wpas_p2p_disconnect(tmp);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005072 }
5073
5074 /*
5075 * Deinit GO data on any possibly remaining interface (if main
5076 * interface is used as GO).
5077 */
5078 for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
5079 if (wpa_s->ap_iface)
5080 wpas_p2p_group_deinit(wpa_s);
5081 }
5082
5083 p2p_deinit(global->p2p);
5084 global->p2p = NULL;
Dmitry Shmidt04949592012-07-19 12:16:46 -07005085 global->p2p_init_wpa_s = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005086}
5087
5088
5089static int wpas_p2p_create_iface(struct wpa_supplicant *wpa_s)
5090{
Dmitry Shmidt9c175262016-03-03 10:20:07 -08005091 if (wpa_s->conf->p2p_no_group_iface)
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08005092 return 0; /* separate interface disabled per configuration */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005093 if (wpa_s->drv_flags &
5094 (WPA_DRIVER_FLAGS_P2P_DEDICATED_INTERFACE |
5095 WPA_DRIVER_FLAGS_P2P_MGMT_AND_NON_P2P))
5096 return 1; /* P2P group requires a new interface in every case
5097 */
5098 if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_CONCURRENT))
5099 return 0; /* driver does not support concurrent operations */
5100 if (wpa_s->global->ifaces->next)
5101 return 1; /* more that one interface already in use */
5102 if (wpa_s->wpa_state >= WPA_AUTHENTICATING)
5103 return 1; /* this interface is already in use */
5104 return 0;
5105}
5106
5107
5108static int wpas_p2p_start_go_neg(struct wpa_supplicant *wpa_s,
5109 const u8 *peer_addr,
5110 enum p2p_wps_method wps_method,
5111 int go_intent, const u8 *own_interface_addr,
Dmitry Shmidt04949592012-07-19 12:16:46 -07005112 unsigned int force_freq, int persistent_group,
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08005113 struct wpa_ssid *ssid, unsigned int pref_freq)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005114{
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005115 if (persistent_group && wpa_s->conf->persistent_reconnect)
5116 persistent_group = 2;
5117
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005118 /*
5119 * Increase GO config timeout if HT40 is used since it takes some time
5120 * to scan channels for coex purposes before the BSS can be started.
5121 */
5122 p2p_set_config_timeout(wpa_s->global->p2p,
5123 wpa_s->p2p_go_ht40 ? 255 : 100, 20);
5124
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005125 return p2p_connect(wpa_s->global->p2p, peer_addr, wps_method,
5126 go_intent, own_interface_addr, force_freq,
Dmitry Shmidt04949592012-07-19 12:16:46 -07005127 persistent_group, ssid ? ssid->ssid : NULL,
5128 ssid ? ssid->ssid_len : 0,
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08005129 wpa_s->p2p_pd_before_go_neg, pref_freq,
5130 wps_method == WPS_NFC ? wpa_s->p2p_oob_dev_pw_id :
5131 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005132}
5133
5134
5135static int wpas_p2p_auth_go_neg(struct wpa_supplicant *wpa_s,
5136 const u8 *peer_addr,
5137 enum p2p_wps_method wps_method,
5138 int go_intent, const u8 *own_interface_addr,
Dmitry Shmidt04949592012-07-19 12:16:46 -07005139 unsigned int force_freq, int persistent_group,
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08005140 struct wpa_ssid *ssid, unsigned int pref_freq)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005141{
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005142 if (persistent_group && wpa_s->conf->persistent_reconnect)
5143 persistent_group = 2;
5144
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005145 return p2p_authorize(wpa_s->global->p2p, peer_addr, wps_method,
5146 go_intent, own_interface_addr, force_freq,
Dmitry Shmidt04949592012-07-19 12:16:46 -07005147 persistent_group, ssid ? ssid->ssid : NULL,
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08005148 ssid ? ssid->ssid_len : 0, pref_freq,
5149 wps_method == WPS_NFC ? wpa_s->p2p_oob_dev_pw_id :
5150 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005151}
5152
5153
5154static void wpas_p2p_check_join_scan_limit(struct wpa_supplicant *wpa_s)
5155{
5156 wpa_s->p2p_join_scan_count++;
5157 wpa_printf(MSG_DEBUG, "P2P: Join scan attempt %d",
5158 wpa_s->p2p_join_scan_count);
5159 if (wpa_s->p2p_join_scan_count > P2P_MAX_JOIN_SCAN_ATTEMPTS) {
5160 wpa_printf(MSG_DEBUG, "P2P: Failed to find GO " MACSTR
5161 " for join operationg - stop join attempt",
5162 MAC2STR(wpa_s->pending_join_iface_addr));
5163 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
Dmitry Shmidt04949592012-07-19 12:16:46 -07005164 if (wpa_s->p2p_auto_pd) {
5165 wpa_s->p2p_auto_pd = 0;
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07005166 wpa_msg_global(wpa_s, MSG_INFO,
5167 P2P_EVENT_PROV_DISC_FAILURE
5168 " p2p_dev_addr=" MACSTR " status=N/A",
5169 MAC2STR(wpa_s->pending_join_dev_addr));
Dmitry Shmidt04949592012-07-19 12:16:46 -07005170 return;
5171 }
Jimmy Chenb7b3f4d2020-09-02 16:50:11 +08005172 if (wpa_s->p2p_fallback_to_go_neg) {
5173 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Join operating "
5174 "failed - fall back to GO Negotiation");
5175 wpa_msg_global(wpa_s->p2pdev, MSG_INFO,
5176 P2P_EVENT_FALLBACK_TO_GO_NEG
5177 "reason=join-failed");
5178 wpas_p2p_fallback_to_go_neg(wpa_s, 0);
5179 return;
5180 }
Dmitry Shmidt9c175262016-03-03 10:20:07 -08005181 wpa_msg_global(wpa_s->p2pdev, MSG_INFO,
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07005182 P2P_EVENT_GROUP_FORMATION_FAILURE);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005183 wpas_notify_p2p_group_formation_failure(wpa_s, "");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005184 }
5185}
5186
5187
Dmitry Shmidt04949592012-07-19 12:16:46 -07005188static int wpas_check_freq_conflict(struct wpa_supplicant *wpa_s, int freq)
5189{
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07005190 int res;
5191 unsigned int num, i;
5192 struct wpa_used_freq_data *freqs;
Dmitry Shmidt04949592012-07-19 12:16:46 -07005193
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07005194 if (wpas_p2p_num_unused_channels(wpa_s) > 0) {
5195 /* Multiple channels are supported and not all are in use */
Dmitry Shmidt04949592012-07-19 12:16:46 -07005196 return 0;
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07005197 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07005198
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07005199 freqs = os_calloc(wpa_s->num_multichan_concurrent,
5200 sizeof(struct wpa_used_freq_data));
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07005201 if (!freqs)
5202 return 1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07005203
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07005204 num = wpas_p2p_valid_oper_freqs(wpa_s, freqs,
5205 wpa_s->num_multichan_concurrent);
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07005206
5207 for (i = 0; i < num; i++) {
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07005208 if (freqs[i].freq == freq) {
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07005209 wpa_printf(MSG_DEBUG, "P2P: Frequency %d MHz in use by another virtual interface and can be used",
5210 freq);
5211 res = 0;
5212 goto exit_free;
Dmitry Shmidt04949592012-07-19 12:16:46 -07005213 }
5214 }
5215
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07005216 wpa_printf(MSG_DEBUG, "P2P: No valid operating frequencies");
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07005217 res = 1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07005218
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07005219exit_free:
5220 os_free(freqs);
5221 return res;
Dmitry Shmidt04949592012-07-19 12:16:46 -07005222}
5223
5224
5225static int wpas_p2p_peer_go(struct wpa_supplicant *wpa_s,
5226 const u8 *peer_dev_addr)
5227{
5228 struct wpa_bss *bss;
5229 int updated;
5230
5231 bss = wpa_bss_get_p2p_dev_addr(wpa_s, peer_dev_addr);
5232 if (bss == NULL)
5233 return -1;
5234 if (bss->last_update_idx < wpa_s->bss_update_idx) {
5235 wpa_printf(MSG_DEBUG, "P2P: Peer BSS entry not updated in the "
5236 "last scan");
5237 return 0;
5238 }
5239
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005240 updated = os_reltime_before(&wpa_s->p2p_auto_started,
5241 &bss->last_update);
Dmitry Shmidt04949592012-07-19 12:16:46 -07005242 wpa_printf(MSG_DEBUG, "P2P: Current BSS entry for peer updated at "
5243 "%ld.%06ld (%supdated in last scan)",
5244 bss->last_update.sec, bss->last_update.usec,
5245 updated ? "": "not ");
5246
5247 return updated;
5248}
5249
5250
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005251static void wpas_p2p_scan_res_join(struct wpa_supplicant *wpa_s,
5252 struct wpa_scan_results *scan_res)
5253{
Dmitry Shmidt344abd32014-01-14 13:17:00 -08005254 struct wpa_bss *bss = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005255 int freq;
5256 u8 iface_addr[ETH_ALEN];
Dmitry Shmidt04949592012-07-19 12:16:46 -07005257
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005258 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
5259
5260 if (wpa_s->global->p2p_disabled)
5261 return;
5262
Dmitry Shmidt04949592012-07-19 12:16:46 -07005263 wpa_printf(MSG_DEBUG, "P2P: Scan results received (%d BSS) for %sjoin",
5264 scan_res ? (int) scan_res->num : -1,
5265 wpa_s->p2p_auto_join ? "auto_" : "");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005266
5267 if (scan_res)
5268 wpas_p2p_scan_res_handler(wpa_s, scan_res);
5269
Dmitry Shmidt04949592012-07-19 12:16:46 -07005270 if (wpa_s->p2p_auto_pd) {
5271 int join = wpas_p2p_peer_go(wpa_s,
5272 wpa_s->pending_join_dev_addr);
5273 if (join == 0 &&
5274 wpa_s->auto_pd_scan_retry < P2P_AUTO_PD_SCAN_ATTEMPTS) {
5275 wpa_s->auto_pd_scan_retry++;
Dmitry Shmidt444d5672013-04-01 13:08:44 -07005276 bss = wpa_bss_get_bssid_latest(
5277 wpa_s, wpa_s->pending_join_dev_addr);
Dmitry Shmidt04949592012-07-19 12:16:46 -07005278 if (bss) {
5279 freq = bss->freq;
5280 wpa_printf(MSG_DEBUG, "P2P: Scan retry %d for "
5281 "the peer " MACSTR " at %d MHz",
5282 wpa_s->auto_pd_scan_retry,
5283 MAC2STR(wpa_s->
5284 pending_join_dev_addr),
5285 freq);
Dmitry Shmidt344abd32014-01-14 13:17:00 -08005286 wpas_p2p_join_scan_req(wpa_s, freq, NULL, 0);
Dmitry Shmidt04949592012-07-19 12:16:46 -07005287 return;
5288 }
5289 }
5290
5291 if (join < 0)
5292 join = 0;
5293
5294 wpa_s->p2p_auto_pd = 0;
5295 wpa_s->pending_pd_use = join ? AUTO_PD_JOIN : AUTO_PD_GO_NEG;
5296 wpa_printf(MSG_DEBUG, "P2P: Auto PD with " MACSTR " join=%d",
5297 MAC2STR(wpa_s->pending_join_dev_addr), join);
5298 if (p2p_prov_disc_req(wpa_s->global->p2p,
Dmitry Shmidt216983b2015-02-06 10:50:36 -08005299 wpa_s->pending_join_dev_addr, NULL,
Dmitry Shmidt04949592012-07-19 12:16:46 -07005300 wpa_s->pending_pd_config_methods, join,
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08005301 0, wpa_s->user_initiated_pd) < 0) {
Dmitry Shmidt04949592012-07-19 12:16:46 -07005302 wpa_s->p2p_auto_pd = 0;
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07005303 wpa_msg_global(wpa_s, MSG_INFO,
5304 P2P_EVENT_PROV_DISC_FAILURE
5305 " p2p_dev_addr=" MACSTR " status=N/A",
5306 MAC2STR(wpa_s->pending_join_dev_addr));
Dmitry Shmidt04949592012-07-19 12:16:46 -07005307 }
5308 return;
5309 }
5310
5311 if (wpa_s->p2p_auto_join) {
5312 int join = wpas_p2p_peer_go(wpa_s,
5313 wpa_s->pending_join_dev_addr);
5314 if (join < 0) {
5315 wpa_printf(MSG_DEBUG, "P2P: Peer was not found to be "
5316 "running a GO -> use GO Negotiation");
Dmitry Shmidt9c175262016-03-03 10:20:07 -08005317 wpa_msg_global(wpa_s->p2pdev, MSG_INFO,
Dmitry Shmidt7f656022015-02-25 14:36:37 -08005318 P2P_EVENT_FALLBACK_TO_GO_NEG
5319 "reason=peer-not-running-GO");
Dmitry Shmidt04949592012-07-19 12:16:46 -07005320 wpas_p2p_connect(wpa_s, wpa_s->pending_join_dev_addr,
5321 wpa_s->p2p_pin, wpa_s->p2p_wps_method,
5322 wpa_s->p2p_persistent_group, 0, 0, 0,
5323 wpa_s->p2p_go_intent,
5324 wpa_s->p2p_connect_freq,
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08005325 wpa_s->p2p_go_vht_center_freq2,
Dmitry Shmidt04949592012-07-19 12:16:46 -07005326 wpa_s->p2p_persistent_id,
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005327 wpa_s->p2p_pd_before_go_neg,
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07005328 wpa_s->p2p_go_ht40,
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08005329 wpa_s->p2p_go_vht,
Dmitry Shmidtde47be72016-01-07 12:52:55 -08005330 wpa_s->p2p_go_max_oper_chwidth,
Hai Shalom74f70d42019-02-11 14:42:39 -08005331 wpa_s->p2p_go_he,
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08005332 wpa_s->p2p_go_edmg,
Hai Shalomc1a21442022-02-04 13:43:00 -08005333 NULL, 0,
5334 is_p2p_allow_6ghz(wpa_s->global->p2p));
Dmitry Shmidt04949592012-07-19 12:16:46 -07005335 return;
5336 }
5337
5338 wpa_printf(MSG_DEBUG, "P2P: Peer was found running GO%s -> "
5339 "try to join the group", join ? "" :
5340 " in older scan");
Dmitry Shmidt7f656022015-02-25 14:36:37 -08005341 if (!join) {
Dmitry Shmidt9c175262016-03-03 10:20:07 -08005342 wpa_msg_global(wpa_s->p2pdev, MSG_INFO,
Dmitry Shmidt7f656022015-02-25 14:36:37 -08005343 P2P_EVENT_FALLBACK_TO_GO_NEG_ENABLED);
Dmitry Shmidt04949592012-07-19 12:16:46 -07005344 wpa_s->p2p_fallback_to_go_neg = 1;
Dmitry Shmidt7f656022015-02-25 14:36:37 -08005345 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07005346 }
5347
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005348 freq = p2p_get_oper_freq(wpa_s->global->p2p,
5349 wpa_s->pending_join_iface_addr);
5350 if (freq < 0 &&
5351 p2p_get_interface_addr(wpa_s->global->p2p,
5352 wpa_s->pending_join_dev_addr,
5353 iface_addr) == 0 &&
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07005354 os_memcmp(iface_addr, wpa_s->pending_join_dev_addr, ETH_ALEN) != 0
5355 && !wpa_bss_get_bssid(wpa_s, wpa_s->pending_join_iface_addr)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005356 wpa_printf(MSG_DEBUG, "P2P: Overwrite pending interface "
5357 "address for join from " MACSTR " to " MACSTR
5358 " based on newly discovered P2P peer entry",
5359 MAC2STR(wpa_s->pending_join_iface_addr),
5360 MAC2STR(iface_addr));
5361 os_memcpy(wpa_s->pending_join_iface_addr, iface_addr,
5362 ETH_ALEN);
5363
5364 freq = p2p_get_oper_freq(wpa_s->global->p2p,
5365 wpa_s->pending_join_iface_addr);
5366 }
5367 if (freq >= 0) {
5368 wpa_printf(MSG_DEBUG, "P2P: Target GO operating frequency "
5369 "from P2P peer table: %d MHz", freq);
5370 }
Dmitry Shmidt344abd32014-01-14 13:17:00 -08005371 if (wpa_s->p2p_join_ssid_len) {
5372 wpa_printf(MSG_DEBUG, "P2P: Trying to find target GO BSS entry based on BSSID "
5373 MACSTR " and SSID %s",
5374 MAC2STR(wpa_s->pending_join_iface_addr),
5375 wpa_ssid_txt(wpa_s->p2p_join_ssid,
5376 wpa_s->p2p_join_ssid_len));
5377 bss = wpa_bss_get(wpa_s, wpa_s->pending_join_iface_addr,
5378 wpa_s->p2p_join_ssid,
5379 wpa_s->p2p_join_ssid_len);
Dmitry Shmidtde47be72016-01-07 12:52:55 -08005380 } else if (!bss) {
Dmitry Shmidt344abd32014-01-14 13:17:00 -08005381 wpa_printf(MSG_DEBUG, "P2P: Trying to find target GO BSS entry based on BSSID "
5382 MACSTR, MAC2STR(wpa_s->pending_join_iface_addr));
5383 bss = wpa_bss_get_bssid_latest(wpa_s,
5384 wpa_s->pending_join_iface_addr);
5385 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005386 if (bss) {
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07005387 u8 dev_addr[ETH_ALEN];
5388
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005389 freq = bss->freq;
5390 wpa_printf(MSG_DEBUG, "P2P: Target GO operating frequency "
Dmitry Shmidt051af732013-10-22 13:52:46 -07005391 "from BSS table: %d MHz (SSID %s)", freq,
5392 wpa_ssid_txt(bss->ssid, bss->ssid_len));
Hai Shalom60840252021-02-19 19:02:11 -08005393 if (p2p_parse_dev_addr(wpa_bss_ie_ptr(bss), bss->ie_len,
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07005394 dev_addr) == 0 &&
5395 os_memcmp(wpa_s->pending_join_dev_addr,
5396 wpa_s->pending_join_iface_addr, ETH_ALEN) == 0 &&
5397 os_memcmp(dev_addr, wpa_s->pending_join_dev_addr,
5398 ETH_ALEN) != 0) {
5399 wpa_printf(MSG_DEBUG,
5400 "P2P: Update target GO device address based on BSS entry: " MACSTR " (was " MACSTR ")",
5401 MAC2STR(dev_addr),
5402 MAC2STR(wpa_s->pending_join_dev_addr));
5403 os_memcpy(wpa_s->pending_join_dev_addr, dev_addr,
5404 ETH_ALEN);
5405 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005406 }
5407 if (freq > 0) {
5408 u16 method;
5409
Dmitry Shmidt04949592012-07-19 12:16:46 -07005410 if (wpas_check_freq_conflict(wpa_s, freq) > 0) {
Dmitry Shmidt9c175262016-03-03 10:20:07 -08005411 wpa_msg_global(wpa_s->p2pdev, MSG_INFO,
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07005412 P2P_EVENT_GROUP_FORMATION_FAILURE
5413 "reason=FREQ_CONFLICT");
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005414 wpas_notify_p2p_group_formation_failure(
5415 wpa_s, "FREQ_CONFLICT");
Dmitry Shmidt04949592012-07-19 12:16:46 -07005416 return;
5417 }
5418
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005419 wpa_printf(MSG_DEBUG, "P2P: Send Provision Discovery Request "
5420 "prior to joining an existing group (GO " MACSTR
5421 " freq=%u MHz)",
5422 MAC2STR(wpa_s->pending_join_dev_addr), freq);
5423 wpa_s->pending_pd_before_join = 1;
5424
5425 switch (wpa_s->pending_join_wps_method) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005426 case WPS_PIN_DISPLAY:
5427 method = WPS_CONFIG_KEYPAD;
5428 break;
5429 case WPS_PIN_KEYPAD:
5430 method = WPS_CONFIG_DISPLAY;
5431 break;
5432 case WPS_PBC:
5433 method = WPS_CONFIG_PUSHBUTTON;
5434 break;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005435 case WPS_P2PS:
5436 method = WPS_CONFIG_P2PS;
5437 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005438 default:
5439 method = 0;
5440 break;
5441 }
5442
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005443 if ((p2p_get_provisioning_info(wpa_s->global->p2p,
5444 wpa_s->pending_join_dev_addr) ==
5445 method)) {
5446 /*
5447 * We have already performed provision discovery for
5448 * joining the group. Proceed directly to join
5449 * operation without duplicated provision discovery. */
5450 wpa_printf(MSG_DEBUG, "P2P: Provision discovery "
5451 "with " MACSTR " already done - proceed to "
5452 "join",
5453 MAC2STR(wpa_s->pending_join_dev_addr));
5454 wpa_s->pending_pd_before_join = 0;
5455 goto start;
5456 }
5457
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005458 if (p2p_prov_disc_req(wpa_s->global->p2p,
Dmitry Shmidt216983b2015-02-06 10:50:36 -08005459 wpa_s->pending_join_dev_addr,
5460 NULL, method, 1,
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08005461 freq, wpa_s->user_initiated_pd) < 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005462 wpa_printf(MSG_DEBUG, "P2P: Failed to send Provision "
5463 "Discovery Request before joining an "
5464 "existing group");
5465 wpa_s->pending_pd_before_join = 0;
5466 goto start;
5467 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005468 return;
5469 }
5470
5471 wpa_printf(MSG_DEBUG, "P2P: Failed to find BSS/GO - try again later");
5472 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
5473 eloop_register_timeout(1, 0, wpas_p2p_join_scan, wpa_s, NULL);
5474 wpas_p2p_check_join_scan_limit(wpa_s);
5475 return;
5476
5477start:
5478 /* Start join operation immediately */
Dmitry Shmidtde47be72016-01-07 12:52:55 -08005479 wpas_p2p_join_start(wpa_s, 0, wpa_s->p2p_join_ssid,
5480 wpa_s->p2p_join_ssid_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005481}
5482
5483
Dmitry Shmidt344abd32014-01-14 13:17:00 -08005484static void wpas_p2p_join_scan_req(struct wpa_supplicant *wpa_s, int freq,
5485 const u8 *ssid, size_t ssid_len)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005486{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005487 int ret;
5488 struct wpa_driver_scan_params params;
5489 struct wpabuf *wps_ie, *ies;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005490 size_t ielen;
Dmitry Shmidt04949592012-07-19 12:16:46 -07005491 int freqs[2] = { 0, 0 };
Dmitry Shmidt9c175262016-03-03 10:20:07 -08005492 unsigned int bands;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005493
5494 os_memset(&params, 0, sizeof(params));
5495
5496 /* P2P Wildcard SSID */
5497 params.num_ssids = 1;
Dmitry Shmidt344abd32014-01-14 13:17:00 -08005498 if (ssid && ssid_len) {
5499 params.ssids[0].ssid = ssid;
5500 params.ssids[0].ssid_len = ssid_len;
5501 os_memcpy(wpa_s->p2p_join_ssid, ssid, ssid_len);
5502 wpa_s->p2p_join_ssid_len = ssid_len;
5503 } else {
5504 params.ssids[0].ssid = (u8 *) P2P_WILDCARD_SSID;
5505 params.ssids[0].ssid_len = P2P_WILDCARD_SSID_LEN;
5506 wpa_s->p2p_join_ssid_len = 0;
5507 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005508
5509 wpa_s->wps->dev.p2p = 1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07005510 wps_ie = wps_build_probe_req_ie(DEV_PW_DEFAULT, &wpa_s->wps->dev,
5511 wpa_s->wps->uuid, WPS_REQ_ENROLLEE, 0,
5512 NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005513 if (wps_ie == NULL) {
5514 wpas_p2p_scan_res_join(wpa_s, NULL);
5515 return;
5516 }
5517
Dmitry Shmidt9e3f8ee2014-01-17 10:52:01 -08005518 if (!freq) {
5519 int oper_freq;
5520 /*
5521 * If freq is not provided, check the operating freq of the GO
5522 * and use a single channel scan on if possible.
5523 */
5524 oper_freq = p2p_get_oper_freq(wpa_s->global->p2p,
5525 wpa_s->pending_join_iface_addr);
5526 if (oper_freq > 0)
5527 freq = oper_freq;
5528 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07005529 if (freq > 0) {
5530 freqs[0] = freq;
5531 params.freqs = freqs;
Hai Shalomc1a21442022-02-04 13:43:00 -08005532 } else if (wpa_s->conf->p2p_6ghz_disable ||
5533 !is_p2p_allow_6ghz(wpa_s->global->p2p)) {
Hai Shalom899fcc72020-10-19 14:38:18 -07005534 wpa_printf(MSG_DEBUG,
5535 "P2P: 6 GHz disabled - update the scan frequency list");
Hai Shalom60840252021-02-19 19:02:11 -08005536 wpa_add_scan_freqs_list(wpa_s, HOSTAPD_MODE_IEEE80211G, &params,
5537 0);
5538 wpa_add_scan_freqs_list(wpa_s, HOSTAPD_MODE_IEEE80211A, &params,
5539 0);
Dmitry Shmidt04949592012-07-19 12:16:46 -07005540 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005541
Dmitry Shmidt9c175262016-03-03 10:20:07 -08005542 ielen = p2p_scan_ie_buf_len(wpa_s->global->p2p);
5543 ies = wpabuf_alloc(wpabuf_len(wps_ie) + ielen);
5544 if (ies == NULL) {
5545 wpabuf_free(wps_ie);
5546 wpas_p2p_scan_res_join(wpa_s, NULL);
5547 return;
5548 }
5549 wpabuf_put_buf(ies, wps_ie);
5550 wpabuf_free(wps_ie);
5551
5552 bands = wpas_get_bands(wpa_s, freqs);
5553 p2p_scan_ie(wpa_s->global->p2p, ies, NULL, bands);
5554
5555 params.p2p_probe = 1;
5556 params.extra_ies = wpabuf_head(ies);
5557 params.extra_ies_len = wpabuf_len(ies);
5558
Dmitry Shmidtebd93af2017-02-21 13:40:44 -08005559 if (wpa_s->clear_driver_scan_cache) {
5560 wpa_printf(MSG_DEBUG,
5561 "Request driver to clear scan cache due to local BSS flush");
5562 params.only_new_results = 1;
5563 }
5564
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005565 /*
5566 * Run a scan to update BSS table and start Provision Discovery once
5567 * the new scan results become available.
5568 */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005569 ret = wpa_drv_scan(wpa_s, &params);
Hai Shalomc1a21442022-02-04 13:43:00 -08005570 if (params.freqs != freqs)
Hai Shalom899fcc72020-10-19 14:38:18 -07005571 os_free(params.freqs);
Dmitry Shmidt444d5672013-04-01 13:08:44 -07005572 if (!ret) {
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005573 os_get_reltime(&wpa_s->scan_trigger_time);
Dmitry Shmidt04949592012-07-19 12:16:46 -07005574 wpa_s->scan_res_handler = wpas_p2p_scan_res_join;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005575 wpa_s->own_scan_requested = 1;
Dmitry Shmidtebd93af2017-02-21 13:40:44 -08005576 wpa_s->clear_driver_scan_cache = 0;
Dmitry Shmidt444d5672013-04-01 13:08:44 -07005577 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005578
5579 wpabuf_free(ies);
5580
5581 if (ret) {
5582 wpa_printf(MSG_DEBUG, "P2P: Failed to start scan for join - "
5583 "try again later");
5584 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
5585 eloop_register_timeout(1, 0, wpas_p2p_join_scan, wpa_s, NULL);
5586 wpas_p2p_check_join_scan_limit(wpa_s);
5587 }
5588}
5589
5590
Dmitry Shmidt04949592012-07-19 12:16:46 -07005591static void wpas_p2p_join_scan(void *eloop_ctx, void *timeout_ctx)
5592{
5593 struct wpa_supplicant *wpa_s = eloop_ctx;
Dmitry Shmidt344abd32014-01-14 13:17:00 -08005594 wpas_p2p_join_scan_req(wpa_s, 0, NULL, 0);
Dmitry Shmidt04949592012-07-19 12:16:46 -07005595}
5596
5597
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005598static int wpas_p2p_join(struct wpa_supplicant *wpa_s, const u8 *iface_addr,
Dmitry Shmidt04949592012-07-19 12:16:46 -07005599 const u8 *dev_addr, enum p2p_wps_method wps_method,
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08005600 int auto_join, int op_freq,
5601 const u8 *ssid, size_t ssid_len)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005602{
5603 wpa_printf(MSG_DEBUG, "P2P: Request to join existing group (iface "
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08005604 MACSTR " dev " MACSTR " op_freq=%d)%s",
5605 MAC2STR(iface_addr), MAC2STR(dev_addr), op_freq,
Dmitry Shmidt04949592012-07-19 12:16:46 -07005606 auto_join ? " (auto_join)" : "");
Dmitry Shmidt344abd32014-01-14 13:17:00 -08005607 if (ssid && ssid_len) {
5608 wpa_printf(MSG_DEBUG, "P2P: Group SSID specified: %s",
5609 wpa_ssid_txt(ssid, ssid_len));
5610 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005611
Dmitry Shmidt04949592012-07-19 12:16:46 -07005612 wpa_s->p2p_auto_pd = 0;
5613 wpa_s->p2p_auto_join = !!auto_join;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005614 os_memcpy(wpa_s->pending_join_iface_addr, iface_addr, ETH_ALEN);
5615 os_memcpy(wpa_s->pending_join_dev_addr, dev_addr, ETH_ALEN);
5616 wpa_s->pending_join_wps_method = wps_method;
5617
5618 /* Make sure we are not running find during connection establishment */
5619 wpas_p2p_stop_find(wpa_s);
5620
5621 wpa_s->p2p_join_scan_count = 0;
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08005622 wpas_p2p_join_scan_req(wpa_s, op_freq, ssid, ssid_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005623 return 0;
5624}
5625
5626
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08005627static int wpas_p2p_join_start(struct wpa_supplicant *wpa_s, int freq,
5628 const u8 *ssid, size_t ssid_len)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005629{
5630 struct wpa_supplicant *group;
5631 struct p2p_go_neg_results res;
Dmitry Shmidt04949592012-07-19 12:16:46 -07005632 struct wpa_bss *bss;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005633
5634 group = wpas_p2p_get_group_iface(wpa_s, 0, 0);
5635 if (group == NULL)
5636 return -1;
5637 if (group != wpa_s) {
5638 os_memcpy(group->p2p_pin, wpa_s->p2p_pin,
5639 sizeof(group->p2p_pin));
5640 group->p2p_wps_method = wpa_s->p2p_wps_method;
5641 }
5642
Hai Shalom74f70d42019-02-11 14:42:39 -08005643 /*
5644 * Need to mark the current interface for p2p_group_formation
5645 * when a separate group interface is not used. This is needed
5646 * to allow p2p_cancel stop a pending p2p_connect-join.
5647 * wpas_p2p_init_group_interface() addresses this for the case
5648 * where a separate group interface is used.
5649 */
5650 if (group == wpa_s->parent)
5651 wpa_s->global->p2p_group_formation = group;
5652
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005653 group->p2p_in_provisioning = 1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07005654 group->p2p_fallback_to_go_neg = wpa_s->p2p_fallback_to_go_neg;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005655
5656 os_memset(&res, 0, sizeof(res));
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08005657 os_memcpy(res.peer_device_addr, wpa_s->pending_join_dev_addr, ETH_ALEN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005658 os_memcpy(res.peer_interface_addr, wpa_s->pending_join_iface_addr,
5659 ETH_ALEN);
5660 res.wps_method = wpa_s->pending_join_wps_method;
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08005661 if (freq && ssid && ssid_len) {
5662 res.freq = freq;
5663 res.ssid_len = ssid_len;
5664 os_memcpy(res.ssid, ssid, ssid_len);
5665 } else {
Dmitry Shmidtde47be72016-01-07 12:52:55 -08005666 if (ssid && ssid_len) {
5667 bss = wpa_bss_get(wpa_s, wpa_s->pending_join_iface_addr,
5668 ssid, ssid_len);
5669 } else {
5670 bss = wpa_bss_get_bssid_latest(
5671 wpa_s, wpa_s->pending_join_iface_addr);
5672 }
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08005673 if (bss) {
5674 res.freq = bss->freq;
5675 res.ssid_len = bss->ssid_len;
5676 os_memcpy(res.ssid, bss->ssid, bss->ssid_len);
5677 wpa_printf(MSG_DEBUG, "P2P: Join target GO operating frequency from BSS table: %d MHz (SSID %s)",
5678 bss->freq,
5679 wpa_ssid_txt(bss->ssid, bss->ssid_len));
Dmitry Shmidtde47be72016-01-07 12:52:55 -08005680 } else if (ssid && ssid_len) {
5681 res.ssid_len = ssid_len;
5682 os_memcpy(res.ssid, ssid, ssid_len);
5683 wpa_printf(MSG_DEBUG, "P2P: Join target GO (SSID %s)",
5684 wpa_ssid_txt(ssid, ssid_len));
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08005685 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07005686 }
5687
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08005688 if (wpa_s->off_channel_freq || wpa_s->roc_waiting_drv_freq) {
5689 wpa_printf(MSG_DEBUG, "P2P: Cancel remain-on-channel prior to "
5690 "starting client");
5691 wpa_drv_cancel_remain_on_channel(wpa_s);
5692 wpa_s->off_channel_freq = 0;
5693 wpa_s->roc_waiting_drv_freq = 0;
5694 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005695 wpas_start_wps_enrollee(group, &res);
5696
5697 /*
5698 * Allow a longer timeout for join-a-running-group than normal 15
5699 * second group formation timeout since the GO may not have authorized
5700 * our connection yet.
5701 */
5702 eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s, NULL);
5703 eloop_register_timeout(60, 0, wpas_p2p_group_formation_timeout,
5704 wpa_s, NULL);
5705
5706 return 0;
5707}
5708
5709
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08005710static int wpas_p2p_setup_freqs(struct wpa_supplicant *wpa_s, int freq,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005711 int *force_freq, int *pref_freq, int go,
Sunil8cd6f4d2022-06-28 18:40:46 +00005712 struct weighted_pcl *pref_freq_list,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005713 unsigned int *num_pref_freq)
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08005714{
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07005715 struct wpa_used_freq_data *freqs;
5716 int res, best_freq, num_unused;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005717 unsigned int freq_in_use = 0, num, i, max_pref_freq;
Vinayak Yadawad8db34572021-08-30 21:28:05 +05305718 int p2p_pref_freq;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005719
5720 max_pref_freq = *num_pref_freq;
5721 *num_pref_freq = 0;
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07005722
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07005723 freqs = os_calloc(wpa_s->num_multichan_concurrent,
5724 sizeof(struct wpa_used_freq_data));
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07005725 if (!freqs)
5726 return -1;
5727
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07005728 num = wpas_p2p_valid_oper_freqs(wpa_s, freqs,
5729 wpa_s->num_multichan_concurrent);
5730
5731 /*
5732 * It is possible that the total number of used frequencies is bigger
5733 * than the number of frequencies used for P2P, so get the system wide
5734 * number of unused frequencies.
5735 */
5736 num_unused = wpas_p2p_num_unused_channels(wpa_s);
5737
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07005738 wpa_printf(MSG_DEBUG,
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07005739 "P2P: Setup freqs: freq=%d num_MCC=%d shared_freqs=%u num_unused=%d",
5740 freq, wpa_s->num_multichan_concurrent, num, num_unused);
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07005741
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08005742 if (freq > 0) {
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07005743 int ret;
5744 if (go)
5745 ret = p2p_supported_freq(wpa_s->global->p2p, freq);
5746 else
5747 ret = p2p_supported_freq_cli(wpa_s->global->p2p, freq);
5748 if (!ret) {
Dmitry Shmidt203eadb2015-03-05 14:16:04 -08005749 if ((wpa_s->drv_flags & WPA_DRIVER_FLAGS_DFS_OFFLOAD) &&
Roshan Pius3a1667e2018-07-03 15:17:14 -07005750 ieee80211_is_dfs(freq, wpa_s->hw.modes,
5751 wpa_s->hw.num_modes)) {
Dmitry Shmidt203eadb2015-03-05 14:16:04 -08005752 /*
5753 * If freq is a DFS channel and DFS is offloaded
5754 * to the driver, allow P2P GO to use it.
5755 */
5756 wpa_printf(MSG_DEBUG,
5757 "P2P: The forced channel for GO (%u MHz) is DFS, and DFS is offloaded to the driver",
5758 freq);
5759 } else {
5760 wpa_printf(MSG_DEBUG,
5761 "P2P: The forced channel (%u MHz) is not supported for P2P uses",
5762 freq);
5763 res = -3;
5764 goto exit_free;
5765 }
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08005766 }
5767
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07005768 for (i = 0; i < num; i++) {
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07005769 if (freqs[i].freq == freq)
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07005770 freq_in_use = 1;
5771 }
5772
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07005773 if (num_unused <= 0 && !freq_in_use) {
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07005774 wpa_printf(MSG_DEBUG, "P2P: Cannot start P2P group on %u MHz as there are no available channels",
5775 freq);
5776 res = -2;
5777 goto exit_free;
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08005778 }
5779 wpa_printf(MSG_DEBUG, "P2P: Trying to force us to use the "
5780 "requested channel (%u MHz)", freq);
5781 *force_freq = freq;
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07005782 goto exit_ok;
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08005783 }
5784
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07005785 best_freq = wpas_p2p_pick_best_used_freq(wpa_s, freqs, num);
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07005786
Vinayak Yadawad8db34572021-08-30 21:28:05 +05305787 if (*pref_freq == 0) {
5788 if (wpa_s->conf->num_p2p_pref_chan && IS_2GHZ(best_freq)) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005789 i = 0;
Vinayak Yadawad8db34572021-08-30 21:28:05 +05305790 while (i < wpa_s->conf->num_p2p_pref_chan) {
5791 p2p_pref_freq = ieee80211_chan_to_freq(NULL,
5792 wpa_s->conf->p2p_pref_chan[i].op_class,
5793 wpa_s->conf->p2p_pref_chan[i].chan);
5794
5795 if (p2p_supported_freq(wpa_s->global->p2p, p2p_pref_freq) &&
5796 !wpas_p2p_disallowed_freq(wpa_s->global, p2p_pref_freq)) {
5797 best_freq = p2p_pref_freq;
5798 wpa_printf(MSG_DEBUG, "P2P: Using frequency (%u MHz) "
5799 "from P2P preferred channel list", best_freq);
5800 break;
5801 } else {
5802 wpa_printf(MSG_MSGDUMP, "P2P: Skipping preferred "
5803 "frequency (%u MHz) ", p2p_pref_freq);
5804 }
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005805 i++;
5806 }
Sreeramya Soratkalf928e8f2022-03-22 17:33:31 +05305807 } else if (!wpa_s->conf->num_p2p_pref_chan) {
Vinayak Yadawad8db34572021-08-30 21:28:05 +05305808 enum wpa_driver_if_type iface_type;
5809
5810 if (go)
5811 iface_type = WPA_IF_P2P_GO;
5812 else
5813 iface_type = WPA_IF_P2P_CLIENT;
5814
5815 wpa_printf(MSG_DEBUG, "P2P: best_freq=%d, go=%d",
5816 best_freq, go);
5817
5818 res = wpa_drv_get_pref_freq_list(wpa_s, iface_type,
5819 &max_pref_freq,
5820 pref_freq_list);
5821 if (!res && max_pref_freq > 0) {
5822 *num_pref_freq = max_pref_freq;
5823 i = 0;
5824 while (i < *num_pref_freq &&
5825 (!p2p_supported_freq(wpa_s->global->p2p,
Sunil8cd6f4d2022-06-28 18:40:46 +00005826 pref_freq_list[i].freq) ||
5827 wpas_p2p_disallowed_freq(
5828 wpa_s->global,
5829 pref_freq_list[i].freq) ||
5830 !p2p_pref_freq_allowed(&pref_freq_list[i],
5831 go))) {
Vinayak Yadawad8db34572021-08-30 21:28:05 +05305832 wpa_printf(MSG_DEBUG,
5833 "P2P: preferred_freq_list[%d]=%d is disallowed",
Sunil8cd6f4d2022-06-28 18:40:46 +00005834 i, pref_freq_list[i].freq);
Vinayak Yadawad8db34572021-08-30 21:28:05 +05305835 i++;
5836 }
5837 if (i != *num_pref_freq) {
Sunil8cd6f4d2022-06-28 18:40:46 +00005838 best_freq = pref_freq_list[i].freq;
Vinayak Yadawad8db34572021-08-30 21:28:05 +05305839 wpa_printf(MSG_DEBUG,
5840 "P2P: Using preferred_freq_list[%d]=%d",
5841 i, best_freq);
5842 } else {
5843 wpa_printf(MSG_DEBUG,
5844 "P2P: All driver preferred frequencies are "
5845 "disallowed for P2P use");
5846 *num_pref_freq = 0;
5847 }
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005848 } else {
5849 wpa_printf(MSG_DEBUG,
Vinayak Yadawad8db34572021-08-30 21:28:05 +05305850 "P2P: No preferred frequency list available");
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005851 }
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005852 }
5853 }
5854
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07005855 /* We have a candidate frequency to use */
5856 if (best_freq > 0) {
5857 if (*pref_freq == 0 && num_unused > 0) {
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08005858 wpa_printf(MSG_DEBUG, "P2P: Try to prefer a frequency (%u MHz) we are already using",
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07005859 best_freq);
5860 *pref_freq = best_freq;
Dmitry Shmidt51a47d52013-09-10 10:52:57 -07005861 } else {
5862 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 -07005863 best_freq);
5864 *force_freq = best_freq;
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07005865 }
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07005866 } else if (num_unused > 0) {
5867 wpa_printf(MSG_DEBUG,
5868 "P2P: Current operating channels are not available for P2P. Try to use another channel");
5869 *force_freq = 0;
5870 } else {
5871 wpa_printf(MSG_DEBUG,
5872 "P2P: All channels are in use and none of them are P2P enabled. Cannot start P2P group");
5873 res = -2;
5874 goto exit_free;
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07005875 }
5876
5877exit_ok:
5878 res = 0;
5879exit_free:
5880 os_free(freqs);
5881 return res;
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08005882}
5883
5884
Hai Shalomc1a21442022-02-04 13:43:00 -08005885static bool is_p2p_6ghz_supported(struct wpa_supplicant *wpa_s,
5886 const u8 *peer_addr)
5887{
5888 if (wpa_s->conf->p2p_6ghz_disable ||
5889 !get_mode(wpa_s->hw.modes, wpa_s->hw.num_modes,
5890 HOSTAPD_MODE_IEEE80211A, true))
5891 return false;
5892
5893 if (!p2p_wfd_enabled(wpa_s->global->p2p))
5894 return false;
5895 if (peer_addr && !p2p_peer_wfd_enabled(wpa_s->global->p2p, peer_addr))
5896 return false;
5897
5898 return true;
5899}
5900
5901
5902static int wpas_p2p_check_6ghz(struct wpa_supplicant *wpa_s,
5903 const u8 *peer_addr, bool allow_6ghz, int freq)
5904{
5905 if (allow_6ghz && is_p2p_6ghz_supported(wpa_s, peer_addr)) {
5906 wpa_printf(MSG_DEBUG,
5907 "P2P: Allow connection on 6 GHz channels");
5908 p2p_set_6ghz_dev_capab(wpa_s->global->p2p, true);
5909 } else {
5910 if (is_6ghz_freq(freq))
5911 return -2;
5912 p2p_set_6ghz_dev_capab(wpa_s->global->p2p, false);
5913 }
5914
5915 return 0;
5916}
5917
5918
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005919/**
5920 * wpas_p2p_connect - Request P2P Group Formation to be started
5921 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
5922 * @peer_addr: Address of the peer P2P Device
5923 * @pin: PIN to use during provisioning or %NULL to indicate PBC mode
5924 * @persistent_group: Whether to create a persistent group
Dmitry Shmidt04949592012-07-19 12:16:46 -07005925 * @auto_join: Whether to select join vs. GO Negotiation automatically
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005926 * @join: Whether to join an existing group (as a client) instead of starting
5927 * Group Owner negotiation; @peer_addr is BSSID in that case
5928 * @auth: Whether to only authorize the connection instead of doing that and
5929 * initiating Group Owner negotiation
5930 * @go_intent: GO Intent or -1 to use default
5931 * @freq: Frequency for the group or 0 for auto-selection
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08005932 * @freq2: Center frequency of segment 1 for the GO operating in VHT 80P80 mode
Dmitry Shmidt04949592012-07-19 12:16:46 -07005933 * @persistent_id: Persistent group credentials to use for forcing GO
5934 * parameters or -1 to generate new values (SSID/passphrase)
5935 * @pd: Whether to send Provision Discovery prior to GO Negotiation as an
5936 * interoperability workaround when initiating group formation
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005937 * @ht40: Start GO with 40 MHz channel width
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07005938 * @vht: Start GO with VHT support
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08005939 * @vht_chwidth: Channel width supported by GO operating with VHT support
Hai Shalom81f62d82019-07-22 12:10:00 -07005940 * (CHANWIDTH_*).
Dmitry Shmidtde47be72016-01-07 12:52:55 -08005941 * @group_ssid: Specific Group SSID for join or %NULL if not set
5942 * @group_ssid_len: Length of @group_ssid in octets
Hai Shalomc1a21442022-02-04 13:43:00 -08005943 * @allow_6ghz: Allow P2P connection on 6 GHz channels
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005944 * Returns: 0 or new PIN (if pin was %NULL) on success, -1 on unspecified
5945 * failure, -2 on failure due to channel not currently available,
5946 * -3 if forced channel is not supported
5947 */
5948int wpas_p2p_connect(struct wpa_supplicant *wpa_s, const u8 *peer_addr,
5949 const char *pin, enum p2p_wps_method wps_method,
Dmitry Shmidt04949592012-07-19 12:16:46 -07005950 int persistent_group, int auto_join, int join, int auth,
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08005951 int go_intent, int freq, unsigned int vht_center_freq2,
5952 int persistent_id, int pd, int ht40, int vht,
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08005953 unsigned int vht_chwidth, int he, int edmg,
Hai Shalomc1a21442022-02-04 13:43:00 -08005954 const u8 *group_ssid, size_t group_ssid_len,
5955 bool allow_6ghz)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005956{
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07005957 int force_freq = 0, pref_freq = 0;
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08005958 int ret = 0, res;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005959 enum wpa_driver_if_type iftype;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005960 const u8 *if_addr;
Dmitry Shmidt04949592012-07-19 12:16:46 -07005961 struct wpa_ssid *ssid = NULL;
Sunil8cd6f4d2022-06-28 18:40:46 +00005962 struct weighted_pcl pref_freq_list[P2P_MAX_PREF_CHANNELS];
5963 unsigned int size;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005964
5965 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
5966 return -1;
5967
Dmitry Shmidt04949592012-07-19 12:16:46 -07005968 if (persistent_id >= 0) {
5969 ssid = wpa_config_get_network(wpa_s->conf, persistent_id);
5970 if (ssid == NULL || ssid->disabled != 2 ||
5971 ssid->mode != WPAS_MODE_P2P_GO)
5972 return -1;
5973 }
5974
Hai Shalomc1a21442022-02-04 13:43:00 -08005975 if (wpas_p2p_check_6ghz(wpa_s, peer_addr, allow_6ghz, freq))
Hai Shalom899fcc72020-10-19 14:38:18 -07005976 return -2;
5977
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07005978 os_free(wpa_s->global->add_psk);
5979 wpa_s->global->add_psk = NULL;
5980
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005981 wpa_s->global->p2p_fail_on_wps_complete = 0;
Dmitry Shmidt216983b2015-02-06 10:50:36 -08005982 wpa_s->global->pending_p2ps_group = 0;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005983 wpa_s->global->pending_p2ps_group_freq = 0;
5984 wpa_s->p2ps_method_config_any = 0;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005985
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005986 if (go_intent < 0)
5987 go_intent = wpa_s->conf->p2p_go_intent;
5988
5989 if (!auth)
Hai Shalomfdcde762020-04-02 11:19:20 -07005990 wpa_s->global->p2p_long_listen = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005991
5992 wpa_s->p2p_wps_method = wps_method;
Dmitry Shmidt04949592012-07-19 12:16:46 -07005993 wpa_s->p2p_persistent_group = !!persistent_group;
5994 wpa_s->p2p_persistent_id = persistent_id;
5995 wpa_s->p2p_go_intent = go_intent;
5996 wpa_s->p2p_connect_freq = freq;
5997 wpa_s->p2p_fallback_to_go_neg = 0;
5998 wpa_s->p2p_pd_before_go_neg = !!pd;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005999 wpa_s->p2p_go_ht40 = !!ht40;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07006000 wpa_s->p2p_go_vht = !!vht;
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08006001 wpa_s->p2p_go_vht_center_freq2 = vht_center_freq2;
6002 wpa_s->p2p_go_max_oper_chwidth = vht_chwidth;
Hai Shalom74f70d42019-02-11 14:42:39 -08006003 wpa_s->p2p_go_he = !!he;
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08006004 wpa_s->p2p_go_edmg = !!edmg;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006005
6006 if (pin)
6007 os_strlcpy(wpa_s->p2p_pin, pin, sizeof(wpa_s->p2p_pin));
6008 else if (wps_method == WPS_PIN_DISPLAY) {
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08006009 if (wps_generate_pin((unsigned int *) &ret) < 0)
6010 return -1;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006011 res = os_snprintf(wpa_s->p2p_pin, sizeof(wpa_s->p2p_pin),
6012 "%08d", ret);
6013 if (os_snprintf_error(sizeof(wpa_s->p2p_pin), res))
6014 wpa_s->p2p_pin[sizeof(wpa_s->p2p_pin) - 1] = '\0';
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006015 wpa_printf(MSG_DEBUG, "P2P: Randomly generated PIN: %s",
6016 wpa_s->p2p_pin);
Dmitry Shmidt849734c2016-05-27 09:59:01 -07006017 } else if (wps_method == WPS_P2PS) {
6018 /* Force the P2Ps default PIN to be used */
6019 os_strlcpy(wpa_s->p2p_pin, "12345670", sizeof(wpa_s->p2p_pin));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006020 } else
6021 wpa_s->p2p_pin[0] = '\0';
6022
Dmitry Shmidt04949592012-07-19 12:16:46 -07006023 if (join || auto_join) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006024 u8 iface_addr[ETH_ALEN], dev_addr[ETH_ALEN];
6025 if (auth) {
6026 wpa_printf(MSG_DEBUG, "P2P: Authorize invitation to "
6027 "connect a running group from " MACSTR,
6028 MAC2STR(peer_addr));
6029 os_memcpy(wpa_s->p2p_auth_invite, peer_addr, ETH_ALEN);
6030 return ret;
6031 }
6032 os_memcpy(dev_addr, peer_addr, ETH_ALEN);
6033 if (p2p_get_interface_addr(wpa_s->global->p2p, peer_addr,
6034 iface_addr) < 0) {
6035 os_memcpy(iface_addr, peer_addr, ETH_ALEN);
6036 p2p_get_dev_addr(wpa_s->global->p2p, peer_addr,
6037 dev_addr);
6038 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07006039 if (auto_join) {
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08006040 os_get_reltime(&wpa_s->p2p_auto_started);
Dmitry Shmidt04949592012-07-19 12:16:46 -07006041 wpa_printf(MSG_DEBUG, "P2P: Auto join started at "
6042 "%ld.%06ld",
6043 wpa_s->p2p_auto_started.sec,
6044 wpa_s->p2p_auto_started.usec);
6045 }
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08006046 wpa_s->user_initiated_pd = 1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07006047 if (wpas_p2p_join(wpa_s, iface_addr, dev_addr, wps_method,
Dmitry Shmidtde47be72016-01-07 12:52:55 -08006048 auto_join, freq,
6049 group_ssid, group_ssid_len) < 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006050 return -1;
6051 return ret;
6052 }
6053
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006054 size = P2P_MAX_PREF_CHANNELS;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07006055 res = wpas_p2p_setup_freqs(wpa_s, freq, &force_freq, &pref_freq,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006056 go_intent == 15, pref_freq_list, &size);
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08006057 if (res)
6058 return res;
Dmitry Shmidta0d265f2013-11-19 13:13:41 -08006059 wpas_p2p_set_own_freq_preference(wpa_s,
6060 force_freq ? force_freq : pref_freq);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006061
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006062 p2p_set_own_pref_freq_list(wpa_s->global->p2p, pref_freq_list, size);
6063
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006064 wpa_s->create_p2p_iface = wpas_p2p_create_iface(wpa_s);
6065
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006066 if (wpa_s->create_p2p_iface) {
6067 /* Prepare to add a new interface for the group */
6068 iftype = WPA_IF_P2P_GROUP;
6069 if (go_intent == 15)
6070 iftype = WPA_IF_P2P_GO;
6071 if (wpas_p2p_add_group_interface(wpa_s, iftype) < 0) {
6072 wpa_printf(MSG_ERROR, "P2P: Failed to allocate a new "
6073 "interface for the group");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006074 return -1;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006075 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006076
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006077 if_addr = wpa_s->pending_interface_addr;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006078 } else {
Dmitry Shmidt9c175262016-03-03 10:20:07 -08006079 if (wpa_s->p2p_mgmt)
6080 if_addr = wpa_s->parent->own_addr;
6081 else
6082 if_addr = wpa_s->own_addr;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006083 os_memset(wpa_s->go_dev_addr, 0, ETH_ALEN);
6084 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006085
6086 if (auth) {
6087 if (wpas_p2p_auth_go_neg(wpa_s, peer_addr, wps_method,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006088 go_intent, if_addr,
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08006089 force_freq, persistent_group, ssid,
6090 pref_freq) < 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006091 return -1;
6092 return ret;
6093 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006094
6095 if (wpas_p2p_start_go_neg(wpa_s, peer_addr, wps_method,
6096 go_intent, if_addr, force_freq,
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08006097 persistent_group, ssid, pref_freq) < 0) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006098 if (wpa_s->create_p2p_iface)
6099 wpas_p2p_remove_pending_group_interface(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006100 return -1;
6101 }
6102 return ret;
6103}
6104
6105
6106/**
6107 * wpas_p2p_remain_on_channel_cb - Indication of remain-on-channel start
6108 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
6109 * @freq: Frequency of the channel in MHz
6110 * @duration: Duration of the stay on the channel in milliseconds
6111 *
6112 * This callback is called when the driver indicates that it has started the
6113 * requested remain-on-channel duration.
6114 */
6115void wpas_p2p_remain_on_channel_cb(struct wpa_supplicant *wpa_s,
6116 unsigned int freq, unsigned int duration)
6117{
6118 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
6119 return;
Dmitry Shmidt216983b2015-02-06 10:50:36 -08006120 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)",
6121 wpa_s->off_channel_freq, wpa_s->pending_listen_freq,
6122 wpa_s->roc_waiting_drv_freq, freq, duration);
6123 if (wpa_s->off_channel_freq &&
6124 wpa_s->off_channel_freq == wpa_s->pending_listen_freq) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006125 p2p_listen_cb(wpa_s->global->p2p, wpa_s->pending_listen_freq,
6126 wpa_s->pending_listen_duration);
6127 wpa_s->pending_listen_freq = 0;
Dmitry Shmidt7f93d6f2014-02-21 11:22:49 -08006128 } else {
6129 wpa_printf(MSG_DEBUG, "P2P: Ignore remain-on-channel callback (off_channel_freq=%u pending_listen_freq=%d freq=%u duration=%u)",
6130 wpa_s->off_channel_freq, wpa_s->pending_listen_freq,
6131 freq, duration);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006132 }
6133}
6134
6135
Jithu Jance57cea1a2014-08-20 10:22:04 -07006136int wpas_p2p_listen_start(struct wpa_supplicant *wpa_s, unsigned int timeout)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006137{
6138 /* Limit maximum Listen state time based on driver limitation. */
6139 if (timeout > wpa_s->max_remain_on_chan)
6140 timeout = wpa_s->max_remain_on_chan;
6141
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006142 return p2p_listen(wpa_s->global->p2p, timeout);
6143}
6144
6145
6146/**
6147 * wpas_p2p_cancel_remain_on_channel_cb - Remain-on-channel timeout
6148 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
6149 * @freq: Frequency of the channel in MHz
6150 *
6151 * This callback is called when the driver indicates that a remain-on-channel
6152 * operation has been completed, i.e., the duration on the requested channel
6153 * has timed out.
6154 */
6155void wpas_p2p_cancel_remain_on_channel_cb(struct wpa_supplicant *wpa_s,
6156 unsigned int freq)
6157{
6158 wpa_printf(MSG_DEBUG, "P2P: Cancel remain-on-channel callback "
6159 "(p2p_long_listen=%d ms pending_action_tx=%p)",
Hai Shalomfdcde762020-04-02 11:19:20 -07006160 wpa_s->global->p2p_long_listen,
6161 offchannel_pending_action_tx(wpa_s));
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08006162 wpas_p2p_listen_work_done(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006163 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
6164 return;
Hai Shalomfdcde762020-04-02 11:19:20 -07006165 if (wpa_s->global->p2p_long_listen > 0)
6166 wpa_s->global->p2p_long_listen -= wpa_s->max_remain_on_chan;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006167 if (p2p_listen_end(wpa_s->global->p2p, freq) > 0)
6168 return; /* P2P module started a new operation */
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08006169 if (offchannel_pending_action_tx(wpa_s))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006170 return;
Hai Shalomfdcde762020-04-02 11:19:20 -07006171 if (wpa_s->global->p2p_long_listen > 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006172 wpa_printf(MSG_DEBUG, "P2P: Continuing long Listen state");
Hai Shalomfdcde762020-04-02 11:19:20 -07006173 wpas_p2p_listen_start(wpa_s, wpa_s->global->p2p_long_listen);
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07006174 } else {
6175 /*
6176 * When listen duration is over, stop listen & update p2p_state
6177 * to IDLE.
6178 */
6179 p2p_stop_listen(wpa_s->global->p2p);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006180 }
6181}
6182
6183
6184/**
6185 * wpas_p2p_group_remove - Remove a P2P group
6186 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
6187 * @ifname: Network interface name of the group interface or "*" to remove all
6188 * groups
6189 * Returns: 0 on success, -1 on failure
6190 *
6191 * This function is used to remove a P2P group. This can be used to disconnect
6192 * from a group in which the local end is a P2P Client or to end a P2P Group in
6193 * case the local end is the Group Owner. If a virtual network interface was
6194 * created for this group, that interface will be removed. Otherwise, only the
6195 * configured P2P group network will be removed from the interface.
6196 */
6197int wpas_p2p_group_remove(struct wpa_supplicant *wpa_s, const char *ifname)
6198{
6199 struct wpa_global *global = wpa_s->global;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006200 struct wpa_supplicant *calling_wpa_s = wpa_s;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006201
6202 if (os_strcmp(ifname, "*") == 0) {
6203 struct wpa_supplicant *prev;
Hai Shalom899fcc72020-10-19 14:38:18 -07006204 bool calling_wpa_s_group_removed = false;
6205
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006206 wpa_s = global->ifaces;
6207 while (wpa_s) {
6208 prev = wpa_s;
6209 wpa_s = wpa_s->next;
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07006210 if (prev->p2p_group_interface !=
6211 NOT_P2P_GROUP_INTERFACE ||
6212 (prev->current_ssid &&
Hai Shalom899fcc72020-10-19 14:38:18 -07006213 prev->current_ssid->p2p_group)) {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006214 wpas_p2p_disconnect_safely(prev, calling_wpa_s);
Hai Shalom899fcc72020-10-19 14:38:18 -07006215 if (prev == calling_wpa_s)
6216 calling_wpa_s_group_removed = true;
6217 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006218 }
Hai Shalom899fcc72020-10-19 14:38:18 -07006219
6220 if (!calling_wpa_s_group_removed &&
6221 (calling_wpa_s->p2p_group_interface !=
6222 NOT_P2P_GROUP_INTERFACE ||
6223 (calling_wpa_s->current_ssid &&
6224 calling_wpa_s->current_ssid->p2p_group))) {
6225 wpa_printf(MSG_DEBUG, "Remove calling_wpa_s P2P group");
6226 wpas_p2p_disconnect_safely(calling_wpa_s,
6227 calling_wpa_s);
6228 }
6229
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006230 return 0;
6231 }
6232
6233 for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
6234 if (os_strcmp(wpa_s->ifname, ifname) == 0)
6235 break;
6236 }
6237
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006238 return wpas_p2p_disconnect_safely(wpa_s, calling_wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006239}
6240
6241
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07006242static int wpas_p2p_select_go_freq(struct wpa_supplicant *wpa_s, int freq)
6243{
6244 unsigned int r;
6245
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006246 if (!wpa_s->conf->num_p2p_pref_chan && !freq) {
6247 unsigned int i, size = P2P_MAX_PREF_CHANNELS;
Sunil8cd6f4d2022-06-28 18:40:46 +00006248 struct weighted_pcl pref_freq_list[P2P_MAX_PREF_CHANNELS];
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006249 int res;
6250
6251 res = wpa_drv_get_pref_freq_list(wpa_s, WPA_IF_P2P_GO,
6252 &size, pref_freq_list);
Hai Shalomc1a21442022-02-04 13:43:00 -08006253 if (!is_p2p_allow_6ghz(wpa_s->global->p2p))
6254 size = p2p_remove_6ghz_channels(pref_freq_list, size);
6255
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006256 if (!res && size > 0) {
6257 i = 0;
Dmitry Shmidt29333592017-01-09 12:27:11 -08006258 while (i < size &&
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07006259 (!p2p_supported_freq(wpa_s->global->p2p,
Sunil8cd6f4d2022-06-28 18:40:46 +00006260 pref_freq_list[i].freq) ||
6261 wpas_p2p_disallowed_freq(
6262 wpa_s->global,
6263 pref_freq_list[i].freq) ||
6264 !p2p_pref_freq_allowed(&pref_freq_list[i],
6265 true))) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006266 wpa_printf(MSG_DEBUG,
6267 "P2P: preferred_freq_list[%d]=%d is disallowed",
Sunil8cd6f4d2022-06-28 18:40:46 +00006268 i, pref_freq_list[i].freq);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006269 i++;
6270 }
6271 if (i != size) {
Sunil8cd6f4d2022-06-28 18:40:46 +00006272 freq = pref_freq_list[i].freq;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006273 wpa_printf(MSG_DEBUG,
6274 "P2P: Using preferred_freq_list[%d]=%d",
6275 i, freq);
6276 } else {
6277 wpa_printf(MSG_DEBUG,
6278 "P2P: All driver preferred frequencies are disallowed for P2P use");
6279 }
6280 } else {
6281 wpa_printf(MSG_DEBUG,
6282 "P2P: No preferred frequency list available");
6283 }
6284 }
6285
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07006286 if (freq == 2) {
6287 wpa_printf(MSG_DEBUG, "P2P: Request to start GO on 2.4 GHz "
6288 "band");
6289 if (wpa_s->best_24_freq > 0 &&
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07006290 p2p_supported_freq_go(wpa_s->global->p2p,
6291 wpa_s->best_24_freq)) {
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07006292 freq = wpa_s->best_24_freq;
6293 wpa_printf(MSG_DEBUG, "P2P: Use best 2.4 GHz band "
6294 "channel: %d MHz", freq);
6295 } else {
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07006296 if (os_get_random((u8 *) &r, sizeof(r)) < 0)
6297 return -1;
Jimmy Chen801bf462021-11-09 23:08:48 +08006298 int possible_2g_freqs[] = {
6299 /* operating class 81 */
6300 2412, 2437, 2462,
6301 };
6302 int possible_2g_freqs_num =
6303 sizeof(possible_2g_freqs)/sizeof(possible_2g_freqs[0]);
6304 int i;
6305 for (i = 0; i < possible_2g_freqs_num; i++, r++) {
6306 freq = possible_2g_freqs[r % possible_2g_freqs_num];
6307 if (p2p_supported_freq_go(wpa_s->global->p2p, freq)) {
6308 break;
6309 }
6310 }
6311
6312 if (i >= possible_2g_freqs_num) {
6313 wpa_printf(MSG_DEBUG, "P2P: Could not select "
6314 "2.4 GHz channel for P2P group");
6315 return -1;
6316 }
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07006317 wpa_printf(MSG_DEBUG, "P2P: Use random 2.4 GHz band "
6318 "channel: %d MHz", freq);
6319 }
6320 }
6321
6322 if (freq == 5) {
6323 wpa_printf(MSG_DEBUG, "P2P: Request to start GO on 5 GHz "
6324 "band");
6325 if (wpa_s->best_5_freq > 0 &&
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07006326 p2p_supported_freq_go(wpa_s->global->p2p,
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07006327 wpa_s->best_5_freq)) {
6328 freq = wpa_s->best_5_freq;
6329 wpa_printf(MSG_DEBUG, "P2P: Use best 5 GHz band "
6330 "channel: %d MHz", freq);
6331 } else {
Hai Shalomc1a21442022-02-04 13:43:00 -08006332 const int freqs[] = {
6333 /* operating class 115 */
6334 5180, 5200, 5220, 5240,
6335 /* operating class 124 */
6336 5745, 5765, 5785, 5805,
6337 };
6338 unsigned int i, num_freqs = ARRAY_SIZE(freqs);
6339
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07006340 if (os_get_random((u8 *) &r, sizeof(r)) < 0)
6341 return -1;
Jimmy Chen64b90632020-09-29 17:27:34 +08006342
6343 /*
6344 * most of 5G channels are DFS, only operating class 115 and 124
6345 * are available possibly, randomly pick a start to check them.
6346 */
6347 int possible_5g_freqs[] = {
6348 /* operating class 115 */
6349 5180, 5200, 5220, 5240,
6350 /* operating class 124 */
6351 5745, 5765, 5785, 5805,
6352 };
6353 int possible_5g_freqs_num =
6354 sizeof(possible_5g_freqs)/sizeof(possible_5g_freqs[0]);
6355
Jimmy Chen64b90632020-09-29 17:27:34 +08006356 for (i = 0; i < possible_5g_freqs_num; i++, r++) {
6357 if (p2p_supported_freq_go(
6358 wpa_s->global->p2p,
6359 possible_5g_freqs[r % possible_5g_freqs_num])) {
6360 freq = possible_5g_freqs[r % possible_5g_freqs_num];
6361 break;
6362 }
6363 }
6364
6365 if (i >= possible_5g_freqs_num) {
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07006366 wpa_printf(MSG_DEBUG, "P2P: Could not select "
6367 "5 GHz channel for P2P group");
6368 return -1;
6369 }
6370 wpa_printf(MSG_DEBUG, "P2P: Use random 5 GHz band "
6371 "channel: %d MHz", freq);
6372 }
6373 }
6374
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07006375 if (freq > 0 && !p2p_supported_freq_go(wpa_s->global->p2p, freq)) {
Dmitry Shmidt203eadb2015-03-05 14:16:04 -08006376 if ((wpa_s->drv_flags & WPA_DRIVER_FLAGS_DFS_OFFLOAD) &&
Roshan Pius3a1667e2018-07-03 15:17:14 -07006377 ieee80211_is_dfs(freq, wpa_s->hw.modes,
6378 wpa_s->hw.num_modes)) {
Dmitry Shmidt203eadb2015-03-05 14:16:04 -08006379 /*
6380 * If freq is a DFS channel and DFS is offloaded to the
6381 * driver, allow P2P GO to use it.
6382 */
6383 wpa_printf(MSG_DEBUG, "P2P: "
6384 "%s: The forced channel for GO (%u MHz) is DFS, and DFS is offloaded",
6385 __func__, freq);
6386 return freq;
6387 }
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07006388 wpa_printf(MSG_DEBUG, "P2P: The forced channel for GO "
6389 "(%u MHz) is not supported for P2P uses",
6390 freq);
6391 return -1;
6392 }
6393
6394 return freq;
6395}
6396
6397
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006398static int wpas_p2p_supported_freq_go(struct wpa_supplicant *wpa_s,
6399 const struct p2p_channels *channels,
6400 int freq)
6401{
6402 if (!wpas_p2p_disallowed_freq(wpa_s->global, freq) &&
6403 p2p_supported_freq_go(wpa_s->global->p2p, freq) &&
6404 freq_included(wpa_s, channels, freq))
6405 return 1;
6406 return 0;
6407}
6408
6409
6410static void wpas_p2p_select_go_freq_no_pref(struct wpa_supplicant *wpa_s,
6411 struct p2p_go_neg_results *params,
6412 const struct p2p_channels *channels)
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07006413{
6414 unsigned int i, r;
6415
Dmitry Shmidt203eadb2015-03-05 14:16:04 -08006416 /* try all channels in operating class 115 */
6417 for (i = 0; i < 4; i++) {
6418 params->freq = 5180 + i * 20;
Hai Shalomc1a21442022-02-04 13:43:00 -08006419 if (wpas_p2p_supported_freq_go(wpa_s, channels, params->freq))
Dmitry Shmidt203eadb2015-03-05 14:16:04 -08006420 goto out;
6421 }
6422
6423 /* try all channels in operating class 124 */
6424 for (i = 0; i < 4; i++) {
6425 params->freq = 5745 + i * 20;
Hai Shalomc1a21442022-02-04 13:43:00 -08006426 if (wpas_p2p_supported_freq_go(wpa_s, channels, params->freq))
Dmitry Shmidt203eadb2015-03-05 14:16:04 -08006427 goto out;
6428 }
6429
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07006430 /* try social channel class 180 channel 2 */
6431 params->freq = 58320 + 1 * 2160;
Hai Shalomc1a21442022-02-04 13:43:00 -08006432 if (wpas_p2p_supported_freq_go(wpa_s, channels, params->freq))
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07006433 goto out;
6434
6435 /* try all channels in reg. class 180 */
6436 for (i = 0; i < 4; i++) {
6437 params->freq = 58320 + i * 2160;
Hai Shalomc1a21442022-02-04 13:43:00 -08006438 if (wpas_p2p_supported_freq_go(wpa_s, channels, params->freq))
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07006439 goto out;
6440 }
6441
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07006442 /* try some random selection of the social channels */
6443 if (os_get_random((u8 *) &r, sizeof(r)) < 0)
6444 return;
6445
6446 for (i = 0; i < 3; i++) {
6447 params->freq = 2412 + ((r + i) % 3) * 25;
6448 if (wpas_p2p_supported_freq_go(wpa_s, channels, params->freq))
6449 goto out;
6450 }
6451
6452 /* try all other channels in operating class 81 */
6453 for (i = 0; i < 11; i++) {
6454 params->freq = 2412 + i * 5;
6455
6456 /* skip social channels; covered in the previous loop */
6457 if (params->freq == 2412 ||
6458 params->freq == 2437 ||
6459 params->freq == 2462)
6460 continue;
6461
6462 if (wpas_p2p_supported_freq_go(wpa_s, channels, params->freq))
6463 goto out;
6464 }
6465
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006466 params->freq = 0;
Dmitry Shmidt203eadb2015-03-05 14:16:04 -08006467 wpa_printf(MSG_DEBUG, "P2P: No 2.4, 5, or 60 GHz channel allowed");
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006468 return;
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07006469out:
6470 wpa_printf(MSG_DEBUG, "P2P: Set GO freq %d MHz (no preference known)",
6471 params->freq);
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07006472}
6473
6474
Roshan Pius3a1667e2018-07-03 15:17:14 -07006475static int wpas_same_band(int freq1, int freq2)
6476{
6477 enum hostapd_hw_mode mode1, mode2;
6478 u8 chan1, chan2;
6479
6480 mode1 = ieee80211_freq_to_chan(freq1, &chan1);
6481 mode2 = ieee80211_freq_to_chan(freq2, &chan2);
6482 if (mode1 == NUM_HOSTAPD_MODES)
6483 return 0;
6484 return mode1 == mode2;
6485}
6486
6487
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006488static int wpas_p2p_init_go_params(struct wpa_supplicant *wpa_s,
6489 struct p2p_go_neg_results *params,
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08006490 int freq, int vht_center_freq2, int ht40,
Hai Shalom74f70d42019-02-11 14:42:39 -08006491 int vht, int max_oper_chwidth, int he,
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08006492 int edmg,
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08006493 const struct p2p_channels *channels)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006494{
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07006495 struct wpa_used_freq_data *freqs;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006496 unsigned int cand;
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07006497 unsigned int num, i;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006498 int ignore_no_freqs = 0;
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08006499 int unused_channels = wpas_p2p_num_unused_channels(wpa_s) > 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006500
6501 os_memset(params, 0, sizeof(*params));
6502 params->role_go = 1;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07006503 params->ht40 = ht40;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07006504 params->vht = vht;
Hai Shalom74f70d42019-02-11 14:42:39 -08006505 params->he = he;
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08006506 params->max_oper_chwidth = max_oper_chwidth;
6507 params->vht_center_freq2 = vht_center_freq2;
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08006508 params->edmg = edmg;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006509
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07006510 freqs = os_calloc(wpa_s->num_multichan_concurrent,
6511 sizeof(struct wpa_used_freq_data));
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07006512 if (!freqs)
6513 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006514
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006515 num = get_shared_radio_freqs_data(wpa_s, freqs,
6516 wpa_s->num_multichan_concurrent);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006517
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006518 if (wpa_s->current_ssid &&
6519 wpa_s->current_ssid->mode == WPAS_MODE_P2P_GO &&
6520 wpa_s->wpa_state == WPA_COMPLETED) {
6521 wpa_printf(MSG_DEBUG, "P2P: %s called for an active GO",
6522 __func__);
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07006523
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006524 /*
6525 * If the frequency selection is done for an active P2P GO that
6526 * is not sharing a frequency, allow to select a new frequency
6527 * even if there are no unused frequencies as we are about to
6528 * move the P2P GO so its frequency can be re-used.
6529 */
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07006530 for (i = 0; i < num; i++) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006531 if (freqs[i].freq == wpa_s->current_ssid->frequency &&
6532 freqs[i].flags == 0) {
6533 ignore_no_freqs = 1;
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07006534 break;
6535 }
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07006536 }
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08006537 }
6538
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08006539 /* Try to use EDMG channel */
6540 if (params->edmg) {
6541 if (wpas_p2p_try_edmg_channel(wpa_s, params) == 0)
6542 goto success;
6543 params->edmg = 0;
6544 }
6545
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006546 /* try using the forced freq */
6547 if (freq) {
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07006548 if (wpas_p2p_disallowed_freq(wpa_s->global, freq) ||
6549 !freq_included(wpa_s, channels, freq)) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006550 wpa_printf(MSG_DEBUG,
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07006551 "P2P: Forced GO freq %d MHz disallowed",
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006552 freq);
6553 goto fail;
6554 }
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07006555 if (!p2p_supported_freq_go(wpa_s->global->p2p, freq)) {
6556 if ((wpa_s->drv_flags & WPA_DRIVER_FLAGS_DFS_OFFLOAD) &&
Roshan Pius3a1667e2018-07-03 15:17:14 -07006557 ieee80211_is_dfs(freq, wpa_s->hw.modes,
6558 wpa_s->hw.num_modes)) {
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07006559 /*
6560 * If freq is a DFS channel and DFS is offloaded
6561 * to the driver, allow P2P GO to use it.
6562 */
6563 wpa_printf(MSG_DEBUG,
6564 "P2P: %s: The forced channel for GO (%u MHz) requires DFS and DFS is offloaded",
6565 __func__, freq);
6566 } else {
6567 wpa_printf(MSG_DEBUG,
6568 "P2P: The forced channel for GO (%u MHz) is not supported for P2P uses",
6569 freq);
6570 goto fail;
6571 }
6572 }
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006573
6574 for (i = 0; i < num; i++) {
6575 if (freqs[i].freq == freq) {
6576 wpa_printf(MSG_DEBUG,
6577 "P2P: forced freq (%d MHz) is also shared",
6578 freq);
6579 params->freq = freq;
6580 goto success;
6581 }
6582 }
6583
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08006584 if (!ignore_no_freqs && !unused_channels) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006585 wpa_printf(MSG_DEBUG,
6586 "P2P: Cannot force GO on freq (%d MHz) as all the channels are in use",
6587 freq);
6588 goto fail;
6589 }
6590
6591 wpa_printf(MSG_DEBUG,
6592 "P2P: force GO freq (%d MHz) on a free channel",
6593 freq);
6594 params->freq = freq;
6595 goto success;
6596 }
6597
6598 /* consider using one of the shared frequencies */
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08006599 if (num &&
6600 (!wpa_s->conf->p2p_ignore_shared_freq || !unused_channels)) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006601 cand = wpas_p2p_pick_best_used_freq(wpa_s, freqs, num);
6602 if (wpas_p2p_supported_freq_go(wpa_s, channels, cand)) {
6603 wpa_printf(MSG_DEBUG,
6604 "P2P: Use shared freq (%d MHz) for GO",
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08006605 cand);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006606 params->freq = cand;
6607 goto success;
6608 }
6609
6610 /* try using one of the shared freqs */
6611 for (i = 0; i < num; i++) {
6612 if (wpas_p2p_supported_freq_go(wpa_s, channels,
6613 freqs[i].freq)) {
6614 wpa_printf(MSG_DEBUG,
6615 "P2P: Use shared freq (%d MHz) for GO",
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08006616 freqs[i].freq);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006617 params->freq = freqs[i].freq;
6618 goto success;
6619 }
6620 }
6621 }
6622
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08006623 if (!ignore_no_freqs && !unused_channels) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006624 wpa_printf(MSG_DEBUG,
6625 "P2P: Cannot force GO on any of the channels we are already using");
6626 goto fail;
6627 }
6628
6629 /* try using the setting from the configuration file */
6630 if (wpa_s->conf->p2p_oper_reg_class == 81 &&
6631 wpa_s->conf->p2p_oper_channel >= 1 &&
6632 wpa_s->conf->p2p_oper_channel <= 11 &&
6633 wpas_p2p_supported_freq_go(
6634 wpa_s, channels,
6635 2407 + 5 * wpa_s->conf->p2p_oper_channel)) {
6636 params->freq = 2407 + 5 * wpa_s->conf->p2p_oper_channel;
6637 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on configured "
6638 "frequency %d MHz", params->freq);
6639 goto success;
6640 }
6641
6642 if ((wpa_s->conf->p2p_oper_reg_class == 115 ||
6643 wpa_s->conf->p2p_oper_reg_class == 116 ||
6644 wpa_s->conf->p2p_oper_reg_class == 117 ||
6645 wpa_s->conf->p2p_oper_reg_class == 124 ||
6646 wpa_s->conf->p2p_oper_reg_class == 125 ||
6647 wpa_s->conf->p2p_oper_reg_class == 126 ||
6648 wpa_s->conf->p2p_oper_reg_class == 127) &&
6649 wpas_p2p_supported_freq_go(wpa_s, channels,
6650 5000 +
6651 5 * wpa_s->conf->p2p_oper_channel)) {
6652 params->freq = 5000 + 5 * wpa_s->conf->p2p_oper_channel;
6653 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on configured "
6654 "frequency %d MHz", params->freq);
6655 goto success;
6656 }
6657
6658 /* Try using best channels */
6659 if (wpa_s->conf->p2p_oper_channel == 0 &&
6660 wpa_s->best_overall_freq > 0 &&
6661 wpas_p2p_supported_freq_go(wpa_s, channels,
6662 wpa_s->best_overall_freq)) {
6663 params->freq = wpa_s->best_overall_freq;
6664 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on best overall "
6665 "channel %d MHz", params->freq);
6666 goto success;
6667 }
6668
6669 if (wpa_s->conf->p2p_oper_channel == 0 &&
6670 wpa_s->best_24_freq > 0 &&
6671 wpas_p2p_supported_freq_go(wpa_s, channels,
6672 wpa_s->best_24_freq)) {
6673 params->freq = wpa_s->best_24_freq;
6674 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on best 2.4 GHz "
6675 "channel %d MHz", params->freq);
6676 goto success;
6677 }
6678
6679 if (wpa_s->conf->p2p_oper_channel == 0 &&
6680 wpa_s->best_5_freq > 0 &&
6681 wpas_p2p_supported_freq_go(wpa_s, channels,
6682 wpa_s->best_5_freq)) {
6683 params->freq = wpa_s->best_5_freq;
6684 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on best 5 GHz "
6685 "channel %d MHz", params->freq);
6686 goto success;
6687 }
6688
6689 /* try using preferred channels */
6690 cand = p2p_get_pref_freq(wpa_s->global->p2p, channels);
6691 if (cand && wpas_p2p_supported_freq_go(wpa_s, channels, cand)) {
6692 params->freq = cand;
6693 wpa_printf(MSG_DEBUG, "P2P: Set GO freq %d MHz from preferred "
6694 "channels", params->freq);
6695 goto success;
6696 }
6697
Roshan Pius3a1667e2018-07-03 15:17:14 -07006698 /* Try using a channel that allows VHT to be used with 80 MHz */
6699 if (wpa_s->hw.modes && wpa_s->p2p_group_common_freqs) {
6700 for (i = 0; i < wpa_s->p2p_group_common_freqs_num; i++) {
6701 enum hostapd_hw_mode mode;
6702 struct hostapd_hw_modes *hwmode;
6703 u8 chan;
Hai Shalomc1a21442022-02-04 13:43:00 -08006704 u8 op_class;
Roshan Pius3a1667e2018-07-03 15:17:14 -07006705
6706 cand = wpa_s->p2p_group_common_freqs[i];
Hai Shalomc1a21442022-02-04 13:43:00 -08006707 op_class = is_6ghz_freq(cand) ? 133 : 128;
Roshan Pius3a1667e2018-07-03 15:17:14 -07006708 mode = ieee80211_freq_to_chan(cand, &chan);
6709 hwmode = get_mode(wpa_s->hw.modes, wpa_s->hw.num_modes,
Hai Shalomfdcde762020-04-02 11:19:20 -07006710 mode, is_6ghz_freq(cand));
Roshan Pius3a1667e2018-07-03 15:17:14 -07006711 if (!hwmode ||
Hai Shalomc1a21442022-02-04 13:43:00 -08006712 wpas_p2p_verify_channel(wpa_s, hwmode, op_class,
6713 chan, BW80) != ALLOWED)
Roshan Pius3a1667e2018-07-03 15:17:14 -07006714 continue;
6715 if (wpas_p2p_supported_freq_go(wpa_s, channels, cand)) {
6716 params->freq = cand;
6717 wpa_printf(MSG_DEBUG,
6718 "P2P: Use freq %d MHz common with the peer and allowing VHT80",
6719 params->freq);
6720 goto success;
6721 }
6722 }
6723 }
6724
6725 /* Try using a channel that allows HT to be used with 40 MHz on the same
6726 * band so that CSA can be used */
6727 if (wpa_s->current_ssid && wpa_s->hw.modes &&
6728 wpa_s->p2p_group_common_freqs) {
6729 for (i = 0; i < wpa_s->p2p_group_common_freqs_num; i++) {
6730 enum hostapd_hw_mode mode;
6731 struct hostapd_hw_modes *hwmode;
Hai Shalomc1a21442022-02-04 13:43:00 -08006732 u8 chan, op_class;
6733 bool is_6ghz, supported = false;
Roshan Pius3a1667e2018-07-03 15:17:14 -07006734
Hai Shalomc1a21442022-02-04 13:43:00 -08006735 is_6ghz = is_6ghz_freq(cand);
Roshan Pius3a1667e2018-07-03 15:17:14 -07006736 cand = wpa_s->p2p_group_common_freqs[i];
6737 mode = ieee80211_freq_to_chan(cand, &chan);
6738 hwmode = get_mode(wpa_s->hw.modes, wpa_s->hw.num_modes,
Hai Shalomc1a21442022-02-04 13:43:00 -08006739 mode, is_6ghz);
Roshan Pius3a1667e2018-07-03 15:17:14 -07006740 if (!wpas_same_band(wpa_s->current_ssid->frequency,
6741 cand) ||
Hai Shalomc1a21442022-02-04 13:43:00 -08006742 !hwmode)
Roshan Pius3a1667e2018-07-03 15:17:14 -07006743 continue;
Hai Shalomc1a21442022-02-04 13:43:00 -08006744 if (is_6ghz &&
6745 wpas_p2p_verify_channel(wpa_s, hwmode, 132, chan,
6746 BW40) == ALLOWED)
6747 supported = true;
6748
6749 if (!is_6ghz &&
6750 ieee80211_freq_to_channel_ext(
Sunil8cd6f4d2022-06-28 18:40:46 +00006751 cand, -1, CONF_OPER_CHWIDTH_USE_HT,
6752 &op_class, &chan) != NUM_HOSTAPD_MODES &&
Hai Shalomc1a21442022-02-04 13:43:00 -08006753 wpas_p2p_verify_channel(
6754 wpa_s, hwmode, op_class, chan,
6755 BW40MINUS) == ALLOWED)
6756 supported = true;
6757
6758 if (!supported && !is_6ghz &&
6759 ieee80211_freq_to_channel_ext(
Sunil8cd6f4d2022-06-28 18:40:46 +00006760 cand, 1, CONF_OPER_CHWIDTH_USE_HT,
6761 &op_class, &chan) != NUM_HOSTAPD_MODES &&
Hai Shalomc1a21442022-02-04 13:43:00 -08006762 wpas_p2p_verify_channel(
6763 wpa_s, hwmode, op_class, chan,
6764 BW40PLUS) == ALLOWED)
6765 supported = true;
6766
6767 if (!supported)
6768 continue;
6769
Roshan Pius3a1667e2018-07-03 15:17:14 -07006770 if (wpas_p2p_supported_freq_go(wpa_s, channels, cand)) {
6771 params->freq = cand;
6772 wpa_printf(MSG_DEBUG,
6773 "P2P: Use freq %d MHz common with the peer, allowing HT40, and maintaining same band",
6774 params->freq);
6775 goto success;
6776 }
6777 }
6778 }
6779
6780 /* Try using one of the group common freqs on the same band so that CSA
6781 * can be used */
6782 if (wpa_s->current_ssid && wpa_s->p2p_group_common_freqs) {
6783 for (i = 0; i < wpa_s->p2p_group_common_freqs_num; i++) {
6784 cand = wpa_s->p2p_group_common_freqs[i];
6785 if (!wpas_same_band(wpa_s->current_ssid->frequency,
6786 cand))
6787 continue;
6788 if (wpas_p2p_supported_freq_go(wpa_s, channels, cand)) {
6789 params->freq = cand;
6790 wpa_printf(MSG_DEBUG,
6791 "P2P: Use freq %d MHz common with the peer and maintaining same band",
6792 params->freq);
6793 goto success;
6794 }
6795 }
6796 }
6797
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006798 /* Try using one of the group common freqs */
6799 if (wpa_s->p2p_group_common_freqs) {
6800 for (i = 0; i < wpa_s->p2p_group_common_freqs_num; i++) {
6801 cand = wpa_s->p2p_group_common_freqs[i];
6802 if (wpas_p2p_supported_freq_go(wpa_s, channels, cand)) {
6803 params->freq = cand;
6804 wpa_printf(MSG_DEBUG,
6805 "P2P: Use freq %d MHz common with the peer",
6806 params->freq);
6807 goto success;
6808 }
6809 }
6810 }
6811
6812 /* no preference, select some channel */
6813 wpas_p2p_select_go_freq_no_pref(wpa_s, params, channels);
6814
6815 if (params->freq == 0) {
6816 wpa_printf(MSG_DEBUG, "P2P: did not find a freq for GO use");
6817 goto fail;
6818 }
6819
6820success:
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07006821 os_free(freqs);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006822 return 0;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006823fail:
6824 os_free(freqs);
6825 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006826}
6827
6828
6829static struct wpa_supplicant *
6830wpas_p2p_get_group_iface(struct wpa_supplicant *wpa_s, int addr_allocated,
6831 int go)
6832{
6833 struct wpa_supplicant *group_wpa_s;
6834
Dmitry Shmidtaa532512012-09-24 10:35:31 -07006835 if (!wpas_p2p_create_iface(wpa_s)) {
Dmitry Shmidt9c175262016-03-03 10:20:07 -08006836 if (wpa_s->p2p_mgmt) {
6837 /*
6838 * We may be called on the p2p_dev interface which
6839 * cannot be used for group operations, so always use
6840 * the primary interface.
6841 */
6842 wpa_s->parent->p2pdev = wpa_s;
6843 wpa_s = wpa_s->parent;
6844 }
6845 wpa_dbg(wpa_s, MSG_DEBUG,
6846 "P2P: Use primary interface for group operations");
Dmitry Shmidt56052862013-10-04 10:23:25 -07006847 wpa_s->p2p_first_connection_timeout = 0;
Dmitry Shmidt9c175262016-03-03 10:20:07 -08006848 if (wpa_s != wpa_s->p2pdev)
Dmitry Shmidt849734c2016-05-27 09:59:01 -07006849 wpas_p2p_clone_config(wpa_s, wpa_s->p2pdev);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006850 return wpa_s;
Dmitry Shmidtaa532512012-09-24 10:35:31 -07006851 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006852
6853 if (wpas_p2p_add_group_interface(wpa_s, go ? WPA_IF_P2P_GO :
Dmitry Shmidtaa532512012-09-24 10:35:31 -07006854 WPA_IF_P2P_CLIENT) < 0) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07006855 wpa_msg_global(wpa_s, MSG_ERROR,
6856 "P2P: Failed to add group interface");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006857 return NULL;
Dmitry Shmidtaa532512012-09-24 10:35:31 -07006858 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006859 group_wpa_s = wpas_p2p_init_group_interface(wpa_s, go);
6860 if (group_wpa_s == NULL) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07006861 wpa_msg_global(wpa_s, MSG_ERROR,
6862 "P2P: Failed to initialize group interface");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006863 wpas_p2p_remove_pending_group_interface(wpa_s);
6864 return NULL;
6865 }
6866
Roshan Pius3a1667e2018-07-03 15:17:14 -07006867 if (go && wpa_s->p2p_go_do_acs) {
6868 group_wpa_s->p2p_go_do_acs = wpa_s->p2p_go_do_acs;
6869 group_wpa_s->p2p_go_acs_band = wpa_s->p2p_go_acs_band;
6870 wpa_s->p2p_go_do_acs = 0;
6871 }
6872
Hai Shalomc1a21442022-02-04 13:43:00 -08006873 if (go && wpa_s->p2p_go_allow_dfs) {
6874 group_wpa_s->p2p_go_allow_dfs = wpa_s->p2p_go_allow_dfs;
6875 wpa_s->p2p_go_allow_dfs = 0;
6876 }
6877
Dmitry Shmidtaa532512012-09-24 10:35:31 -07006878 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Use separate group interface %s",
6879 group_wpa_s->ifname);
Dmitry Shmidt56052862013-10-04 10:23:25 -07006880 group_wpa_s->p2p_first_connection_timeout = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006881 return group_wpa_s;
6882}
6883
6884
6885/**
6886 * wpas_p2p_group_add - Add a new P2P group with local end as Group Owner
6887 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
6888 * @persistent_group: Whether to create a persistent group
6889 * @freq: Frequency for the group or 0 to indicate no hardcoding
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08006890 * @vht_center_freq2: segment_1 center frequency for GO operating in VHT 80P80
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07006891 * @ht40: Start GO with 40 MHz channel width
6892 * @vht: Start GO with VHT support
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08006893 * @vht_chwidth: channel bandwidth for GO operating with VHT support
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08006894 * @edmg: Start GO with EDMG support
Hai Shalomc1a21442022-02-04 13:43:00 -08006895 * @allow_6ghz: Allow P2P group creation on a 6 GHz channel
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006896 * Returns: 0 on success, -1 on failure
6897 *
6898 * This function creates a new P2P group with the local end as the Group Owner,
6899 * i.e., without using Group Owner Negotiation.
6900 */
6901int wpas_p2p_group_add(struct wpa_supplicant *wpa_s, int persistent_group,
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08006902 int freq, int vht_center_freq2, int ht40, int vht,
Hai Shalomc1a21442022-02-04 13:43:00 -08006903 int max_oper_chwidth, int he, int edmg,
6904 bool allow_6ghz)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006905{
6906 struct p2p_go_neg_results params;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006907
6908 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
6909 return -1;
Hai Shalomc1a21442022-02-04 13:43:00 -08006910 if (wpas_p2p_check_6ghz(wpa_s, NULL, allow_6ghz, freq))
6911 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006912
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07006913 os_free(wpa_s->global->add_psk);
6914 wpa_s->global->add_psk = NULL;
6915
Dmitry Shmidtdca39792011-09-06 11:17:33 -07006916 /* Make sure we are not running find during connection establishment */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006917 wpa_printf(MSG_DEBUG, "P2P: Stop any on-going P2P FIND");
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08006918 wpas_p2p_stop_find_oper(wpa_s);
Dmitry Shmidtdca39792011-09-06 11:17:33 -07006919
Roshan Pius3a1667e2018-07-03 15:17:14 -07006920 if (!wpa_s->p2p_go_do_acs) {
6921 freq = wpas_p2p_select_go_freq(wpa_s, freq);
6922 if (freq < 0)
6923 return -1;
6924 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006925
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08006926 if (wpas_p2p_init_go_params(wpa_s, &params, freq, vht_center_freq2,
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08006927 ht40, vht, max_oper_chwidth, he, edmg,
6928 NULL))
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006929 return -1;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07006930
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006931 p2p_go_params(wpa_s->global->p2p, &params);
6932 params.persistent_group = persistent_group;
6933
6934 wpa_s = wpas_p2p_get_group_iface(wpa_s, 0, 1);
6935 if (wpa_s == NULL)
6936 return -1;
6937 wpas_start_wps_go(wpa_s, &params, 0);
6938
6939 return 0;
6940}
6941
6942
6943static int wpas_start_p2p_client(struct wpa_supplicant *wpa_s,
Dmitry Shmidt15907092014-03-25 10:42:57 -07006944 struct wpa_ssid *params, int addr_allocated,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006945 int freq, int force_scan)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006946{
6947 struct wpa_ssid *ssid;
6948
6949 wpa_s = wpas_p2p_get_group_iface(wpa_s, addr_allocated, 0);
6950 if (wpa_s == NULL)
6951 return -1;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006952 if (force_scan)
6953 os_get_reltime(&wpa_s->scan_min_time);
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07006954 wpa_s->p2p_last_4way_hs_fail = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006955
6956 wpa_supplicant_ap_deinit(wpa_s);
6957
6958 ssid = wpa_config_add_network(wpa_s->conf);
6959 if (ssid == NULL)
6960 return -1;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006961 os_memset(wpa_s->go_dev_addr, 0, ETH_ALEN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006962 wpa_config_set_network_defaults(ssid);
6963 ssid->temporary = 1;
6964 ssid->proto = WPA_PROTO_RSN;
Dmitry Shmidte4663042016-04-04 10:07:49 -07006965 ssid->pbss = params->pbss;
6966 ssid->pairwise_cipher = params->pbss ? WPA_CIPHER_GCMP :
6967 WPA_CIPHER_CCMP;
6968 ssid->group_cipher = params->pbss ? WPA_CIPHER_GCMP : WPA_CIPHER_CCMP;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006969 ssid->key_mgmt = WPA_KEY_MGMT_PSK;
6970 ssid->ssid = os_malloc(params->ssid_len);
6971 if (ssid->ssid == NULL) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006972 wpa_config_remove_network(wpa_s->conf, ssid->id);
6973 return -1;
6974 }
6975 os_memcpy(ssid->ssid, params->ssid, params->ssid_len);
6976 ssid->ssid_len = params->ssid_len;
6977 ssid->p2p_group = 1;
6978 ssid->export_keys = 1;
6979 if (params->psk_set) {
6980 os_memcpy(ssid->psk, params->psk, 32);
6981 ssid->psk_set = 1;
6982 }
6983 if (params->passphrase)
6984 ssid->passphrase = os_strdup(params->passphrase);
6985
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006986 wpa_s->show_group_started = 1;
Dmitry Shmidt15907092014-03-25 10:42:57 -07006987 wpa_s->p2p_in_invitation = 1;
6988 wpa_s->p2p_invite_go_freq = freq;
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08006989 wpa_s->p2p_go_group_formation_completed = 0;
6990 wpa_s->global->p2p_group_formation = wpa_s;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006991
Dmitry Shmidt9c175262016-03-03 10:20:07 -08006992 eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s->p2pdev,
Dmitry Shmidt15907092014-03-25 10:42:57 -07006993 NULL);
6994 eloop_register_timeout(P2P_MAX_INITIAL_CONN_WAIT, 0,
6995 wpas_p2p_group_formation_timeout,
Dmitry Shmidt9c175262016-03-03 10:20:07 -08006996 wpa_s->p2pdev, NULL);
Dmitry Shmidtfa3fc4a2013-11-21 13:34:38 -08006997 wpa_supplicant_select_network(wpa_s, ssid);
6998
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006999 return 0;
7000}
7001
7002
7003int wpas_p2p_group_add_persistent(struct wpa_supplicant *wpa_s,
7004 struct wpa_ssid *ssid, int addr_allocated,
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08007005 int force_freq, int neg_freq,
7006 int vht_center_freq2, int ht40,
Hai Shalom74f70d42019-02-11 14:42:39 -08007007 int vht, int max_oper_chwidth, int he,
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08007008 int edmg,
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08007009 const struct p2p_channels *channels,
Hai Shalomc1a21442022-02-04 13:43:00 -08007010 int connection_timeout, int force_scan,
7011 bool allow_6ghz)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007012{
7013 struct p2p_go_neg_results params;
Dmitry Shmidt96be6222014-02-13 10:16:51 -08007014 int go = 0, freq;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007015
7016 if (ssid->disabled != 2 || ssid->ssid == NULL)
7017 return -1;
7018
7019 if (wpas_get_p2p_group(wpa_s, ssid->ssid, ssid->ssid_len, &go) &&
7020 go == (ssid->mode == WPAS_MODE_P2P_GO)) {
7021 wpa_printf(MSG_DEBUG, "P2P: Requested persistent group is "
7022 "already running");
Dmitry Shmidtb1e52102015-05-29 12:36:29 -07007023 if (go == 0 &&
7024 eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
Dmitry Shmidt9c175262016-03-03 10:20:07 -08007025 wpa_s->p2pdev, NULL)) {
Dmitry Shmidtb1e52102015-05-29 12:36:29 -07007026 /*
7027 * This can happen if Invitation Response frame was lost
7028 * and the peer (GO of a persistent group) tries to
7029 * invite us again. Reschedule the timeout to avoid
7030 * terminating the wait for the connection too early
7031 * since we now know that the peer is still trying to
7032 * invite us instead of having already started the GO.
7033 */
7034 wpa_printf(MSG_DEBUG,
7035 "P2P: Reschedule group formation timeout since peer is still trying to invite us");
7036 eloop_register_timeout(P2P_MAX_INITIAL_CONN_WAIT, 0,
7037 wpas_p2p_group_formation_timeout,
Dmitry Shmidt9c175262016-03-03 10:20:07 -08007038 wpa_s->p2pdev, NULL);
Dmitry Shmidtb1e52102015-05-29 12:36:29 -07007039 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007040 return 0;
7041 }
7042
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07007043 os_free(wpa_s->global->add_psk);
7044 wpa_s->global->add_psk = NULL;
7045
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007046 /* Make sure we are not running find during connection establishment */
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08007047 wpas_p2p_stop_find_oper(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007048
Dmitry Shmidt04949592012-07-19 12:16:46 -07007049 wpa_s->p2p_fallback_to_go_neg = 0;
7050
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007051 if (ssid->mode == WPAS_MODE_P2P_GO) {
7052 if (force_freq > 0) {
7053 freq = wpas_p2p_select_go_freq(wpa_s, force_freq);
7054 if (freq < 0)
7055 return -1;
7056 } else {
7057 freq = wpas_p2p_select_go_freq(wpa_s, neg_freq);
7058 if (freq < 0 ||
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08007059 (freq > 0 && !freq_included(wpa_s, channels, freq)))
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007060 freq = 0;
7061 }
Dmitry Shmidt912c6ec2015-03-30 13:16:51 -07007062 } else if (ssid->mode == WPAS_MODE_INFRA) {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007063 freq = neg_freq;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08007064 if (freq <= 0 || !freq_included(wpa_s, channels, freq)) {
Dmitry Shmidt912c6ec2015-03-30 13:16:51 -07007065 struct os_reltime now;
7066 struct wpa_bss *bss =
7067 wpa_bss_get_p2p_dev_addr(wpa_s, ssid->bssid);
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07007068
Dmitry Shmidt912c6ec2015-03-30 13:16:51 -07007069 os_get_reltime(&now);
7070 if (bss &&
7071 !os_reltime_expired(&now, &bss->last_update, 5) &&
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08007072 freq_included(wpa_s, channels, bss->freq))
Dmitry Shmidt912c6ec2015-03-30 13:16:51 -07007073 freq = bss->freq;
7074 else
7075 freq = 0;
7076 }
7077
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08007078 return wpas_start_p2p_client(wpa_s, ssid, addr_allocated, freq,
7079 force_scan);
Dmitry Shmidt912c6ec2015-03-30 13:16:51 -07007080 } else {
Dmitry Shmidt15907092014-03-25 10:42:57 -07007081 return -1;
Dmitry Shmidt912c6ec2015-03-30 13:16:51 -07007082 }
Dmitry Shmidt15907092014-03-25 10:42:57 -07007083
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08007084 if (wpas_p2p_init_go_params(wpa_s, &params, freq, vht_center_freq2,
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08007085 ht40, vht, max_oper_chwidth, he, edmg,
7086 channels))
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007087 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007088
7089 params.role_go = 1;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08007090 params.psk_set = ssid->psk_set;
7091 if (params.psk_set)
7092 os_memcpy(params.psk, ssid->psk, sizeof(params.psk));
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08007093 if (ssid->passphrase) {
7094 if (os_strlen(ssid->passphrase) >= sizeof(params.passphrase)) {
7095 wpa_printf(MSG_ERROR, "P2P: Invalid passphrase in "
7096 "persistent group");
7097 return -1;
7098 }
7099 os_strlcpy(params.passphrase, ssid->passphrase,
7100 sizeof(params.passphrase));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007101 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007102 os_memcpy(params.ssid, ssid->ssid, ssid->ssid_len);
7103 params.ssid_len = ssid->ssid_len;
7104 params.persistent_group = 1;
7105
7106 wpa_s = wpas_p2p_get_group_iface(wpa_s, addr_allocated, 1);
7107 if (wpa_s == NULL)
7108 return -1;
7109
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007110 p2p_channels_to_freqs(channels, params.freq_list, P2P_MAX_CHANNELS);
7111
Dmitry Shmidt56052862013-10-04 10:23:25 -07007112 wpa_s->p2p_first_connection_timeout = connection_timeout;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007113 wpas_start_wps_go(wpa_s, &params, 0);
7114
7115 return 0;
7116}
7117
7118
7119static void wpas_p2p_ie_update(void *ctx, struct wpabuf *beacon_ies,
7120 struct wpabuf *proberesp_ies)
7121{
7122 struct wpa_supplicant *wpa_s = ctx;
7123 if (wpa_s->ap_iface) {
7124 struct hostapd_data *hapd = wpa_s->ap_iface->bss[0];
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007125 if (!(hapd->conf->p2p & P2P_GROUP_OWNER)) {
7126 wpabuf_free(beacon_ies);
7127 wpabuf_free(proberesp_ies);
7128 return;
7129 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007130 if (beacon_ies) {
7131 wpabuf_free(hapd->p2p_beacon_ie);
7132 hapd->p2p_beacon_ie = beacon_ies;
7133 }
7134 wpabuf_free(hapd->p2p_probe_resp_ie);
7135 hapd->p2p_probe_resp_ie = proberesp_ies;
7136 } else {
7137 wpabuf_free(beacon_ies);
7138 wpabuf_free(proberesp_ies);
7139 }
7140 wpa_supplicant_ap_update_beacon(wpa_s);
7141}
7142
7143
7144static void wpas_p2p_idle_update(void *ctx, int idle)
7145{
7146 struct wpa_supplicant *wpa_s = ctx;
7147 if (!wpa_s->ap_iface)
7148 return;
7149 wpa_printf(MSG_DEBUG, "P2P: GO - group %sidle", idle ? "" : "not ");
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08007150 if (idle) {
7151 if (wpa_s->global->p2p_fail_on_wps_complete &&
7152 wpa_s->p2p_in_provisioning) {
7153 wpas_p2p_grpform_fail_after_wps(wpa_s);
7154 return;
7155 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007156 wpas_p2p_set_group_idle_timeout(wpa_s);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08007157 } else
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007158 eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL);
7159}
7160
7161
7162struct p2p_group * wpas_p2p_group_init(struct wpa_supplicant *wpa_s,
Dmitry Shmidt04949592012-07-19 12:16:46 -07007163 struct wpa_ssid *ssid)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007164{
7165 struct p2p_group *group;
7166 struct p2p_group_config *cfg;
7167
Dmitry Shmidt849734c2016-05-27 09:59:01 -07007168 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL ||
7169 !ssid->p2p_group)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007170 return NULL;
7171
7172 cfg = os_zalloc(sizeof(*cfg));
7173 if (cfg == NULL)
7174 return NULL;
7175
Dmitry Shmidt04949592012-07-19 12:16:46 -07007176 if (ssid->p2p_persistent_group && wpa_s->conf->persistent_reconnect)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007177 cfg->persistent_group = 2;
Dmitry Shmidt04949592012-07-19 12:16:46 -07007178 else if (ssid->p2p_persistent_group)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007179 cfg->persistent_group = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007180 os_memcpy(cfg->interface_addr, wpa_s->own_addr, ETH_ALEN);
7181 if (wpa_s->max_stations &&
7182 wpa_s->max_stations < wpa_s->conf->max_num_sta)
7183 cfg->max_clients = wpa_s->max_stations;
7184 else
7185 cfg->max_clients = wpa_s->conf->max_num_sta;
Dmitry Shmidt04949592012-07-19 12:16:46 -07007186 os_memcpy(cfg->ssid, ssid->ssid, ssid->ssid_len);
7187 cfg->ssid_len = ssid->ssid_len;
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08007188 cfg->freq = ssid->frequency;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007189 cfg->cb_ctx = wpa_s;
7190 cfg->ie_update = wpas_p2p_ie_update;
7191 cfg->idle_update = wpas_p2p_idle_update;
Dmitry Shmidte4663042016-04-04 10:07:49 -07007192 cfg->ip_addr_alloc = WPA_GET_BE32(wpa_s->p2pdev->conf->ip_addr_start)
7193 != 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007194
7195 group = p2p_group_init(wpa_s->global->p2p, cfg);
7196 if (group == NULL)
7197 os_free(cfg);
Dmitry Shmidt04949592012-07-19 12:16:46 -07007198 if (ssid->mode != WPAS_MODE_P2P_GROUP_FORMATION)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007199 p2p_group_notif_formation_done(group);
7200 wpa_s->p2p_group = group;
7201 return group;
7202}
7203
7204
7205void wpas_p2p_wps_success(struct wpa_supplicant *wpa_s, const u8 *peer_addr,
7206 int registrar)
7207{
Dmitry Shmidt04949592012-07-19 12:16:46 -07007208 struct wpa_ssid *ssid = wpa_s->current_ssid;
7209
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007210 if (!wpa_s->p2p_in_provisioning) {
7211 wpa_printf(MSG_DEBUG, "P2P: Ignore WPS success event - P2P "
7212 "provisioning not in progress");
7213 return;
7214 }
7215
Dmitry Shmidt04949592012-07-19 12:16:46 -07007216 if (ssid && ssid->mode == WPAS_MODE_INFRA) {
7217 u8 go_dev_addr[ETH_ALEN];
7218 os_memcpy(go_dev_addr, wpa_s->bssid, ETH_ALEN);
7219 wpas_p2p_persistent_group(wpa_s, go_dev_addr, ssid->ssid,
7220 ssid->ssid_len);
7221 /* Clear any stored provisioning info */
7222 p2p_clear_provisioning_info(wpa_s->global->p2p, go_dev_addr);
7223 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007224
Dmitry Shmidt9c175262016-03-03 10:20:07 -08007225 eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s->p2pdev,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007226 NULL);
Dmitry Shmidt56052862013-10-04 10:23:25 -07007227 wpa_s->p2p_go_group_formation_completed = 1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07007228 if (ssid && ssid->mode == WPAS_MODE_INFRA) {
7229 /*
7230 * Use a separate timeout for initial data connection to
7231 * complete to allow the group to be removed automatically if
7232 * something goes wrong in this step before the P2P group idle
7233 * timeout mechanism is taken into use.
7234 */
Dmitry Shmidt56052862013-10-04 10:23:25 -07007235 wpa_dbg(wpa_s, MSG_DEBUG,
7236 "P2P: Re-start group formation timeout (%d seconds) as client for initial connection",
7237 P2P_MAX_INITIAL_CONN_WAIT);
Dmitry Shmidt04949592012-07-19 12:16:46 -07007238 eloop_register_timeout(P2P_MAX_INITIAL_CONN_WAIT, 0,
7239 wpas_p2p_group_formation_timeout,
Dmitry Shmidt9c175262016-03-03 10:20:07 -08007240 wpa_s->p2pdev, NULL);
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08007241 /* Complete group formation on successful data connection. */
7242 wpa_s->p2p_go_group_formation_completed = 0;
Dmitry Shmidt56052862013-10-04 10:23:25 -07007243 } else if (ssid) {
7244 /*
7245 * Use a separate timeout for initial data connection to
7246 * complete to allow the group to be removed automatically if
7247 * the client does not complete data connection successfully.
7248 */
7249 wpa_dbg(wpa_s, MSG_DEBUG,
7250 "P2P: Re-start group formation timeout (%d seconds) as GO for initial connection",
7251 P2P_MAX_INITIAL_CONN_WAIT_GO);
7252 eloop_register_timeout(P2P_MAX_INITIAL_CONN_WAIT_GO, 0,
7253 wpas_p2p_group_formation_timeout,
Dmitry Shmidt9c175262016-03-03 10:20:07 -08007254 wpa_s->p2pdev, NULL);
Dmitry Shmidt56052862013-10-04 10:23:25 -07007255 /*
7256 * Complete group formation on first successful data connection
7257 */
7258 wpa_s->p2p_go_group_formation_completed = 0;
Dmitry Shmidt04949592012-07-19 12:16:46 -07007259 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007260 if (wpa_s->global->p2p)
7261 p2p_wps_success_cb(wpa_s->global->p2p, peer_addr);
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07007262 wpas_group_formation_completed(wpa_s, 1, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007263}
7264
7265
Jouni Malinen75ecf522011-06-27 15:19:46 -07007266void wpas_p2p_wps_failed(struct wpa_supplicant *wpa_s,
7267 struct wps_event_fail *fail)
7268{
7269 if (!wpa_s->p2p_in_provisioning) {
7270 wpa_printf(MSG_DEBUG, "P2P: Ignore WPS fail event - P2P "
7271 "provisioning not in progress");
7272 return;
7273 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007274
7275 if (wpa_s->go_params) {
7276 p2p_clear_provisioning_info(
7277 wpa_s->global->p2p,
Dmitry Shmidt04949592012-07-19 12:16:46 -07007278 wpa_s->go_params->peer_device_addr);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007279 }
7280
Jouni Malinen75ecf522011-06-27 15:19:46 -07007281 wpas_notify_p2p_wps_failed(wpa_s, fail);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08007282
7283 if (wpa_s == wpa_s->global->p2p_group_formation) {
7284 /*
7285 * Allow some time for the failed WPS negotiation exchange to
7286 * complete, but remove the group since group formation cannot
7287 * succeed after provisioning failure.
7288 */
7289 wpa_printf(MSG_DEBUG, "P2P: WPS step failed during group formation - reject connection from timeout");
7290 wpa_s->global->p2p_fail_on_wps_complete = 1;
7291 eloop_deplete_timeout(0, 50000,
7292 wpas_p2p_group_formation_timeout,
Dmitry Shmidt9c175262016-03-03 10:20:07 -08007293 wpa_s->p2pdev, NULL);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08007294 }
7295}
7296
7297
7298int wpas_p2p_wps_eapol_cb(struct wpa_supplicant *wpa_s)
7299{
7300 if (!wpa_s->global->p2p_fail_on_wps_complete ||
7301 !wpa_s->p2p_in_provisioning)
7302 return 0;
7303
7304 wpas_p2p_grpform_fail_after_wps(wpa_s);
7305
7306 return 1;
Jouni Malinen75ecf522011-06-27 15:19:46 -07007307}
7308
7309
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007310int wpas_p2p_prov_disc(struct wpa_supplicant *wpa_s, const u8 *peer_addr,
Dmitry Shmidt04949592012-07-19 12:16:46 -07007311 const char *config_method,
Dmitry Shmidt216983b2015-02-06 10:50:36 -08007312 enum wpas_p2p_prov_disc_use use,
7313 struct p2ps_provision *p2ps_prov)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007314{
7315 u16 config_methods;
7316
Dmitry Shmidt216983b2015-02-06 10:50:36 -08007317 wpa_s->global->pending_p2ps_group = 0;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08007318 wpa_s->global->pending_p2ps_group_freq = 0;
Dmitry Shmidt04949592012-07-19 12:16:46 -07007319 wpa_s->p2p_fallback_to_go_neg = 0;
7320 wpa_s->pending_pd_use = NORMAL_PD;
Dmitry Shmidt216983b2015-02-06 10:50:36 -08007321 if (p2ps_prov && use == WPAS_P2P_PD_FOR_ASP) {
7322 p2ps_prov->conncap = p2ps_group_capability(
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08007323 wpa_s, P2PS_SETUP_NONE, p2ps_prov->role,
7324 &p2ps_prov->force_freq, &p2ps_prov->pref_freq);
7325
Dmitry Shmidt216983b2015-02-06 10:50:36 -08007326 wpa_printf(MSG_DEBUG,
7327 "P2P: %s conncap: %d - ASP parsed: %x %x %d %s",
7328 __func__, p2ps_prov->conncap,
7329 p2ps_prov->adv_id, p2ps_prov->conncap,
7330 p2ps_prov->status, p2ps_prov->info);
7331
7332 config_methods = 0;
7333 } else if (os_strncmp(config_method, "display", 7) == 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007334 config_methods = WPS_CONFIG_DISPLAY;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007335 else if (os_strncmp(config_method, "keypad", 6) == 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007336 config_methods = WPS_CONFIG_KEYPAD;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007337 else if (os_strncmp(config_method, "pbc", 3) == 0 ||
7338 os_strncmp(config_method, "pushbutton", 10) == 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007339 config_methods = WPS_CONFIG_PUSHBUTTON;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007340 else {
7341 wpa_printf(MSG_DEBUG, "P2P: Unknown config method");
Dmitry Shmidt216983b2015-02-06 10:50:36 -08007342 os_free(p2ps_prov);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007343 return -1;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007344 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007345
Dmitry Shmidt04949592012-07-19 12:16:46 -07007346 if (use == WPAS_P2P_PD_AUTO) {
7347 os_memcpy(wpa_s->pending_join_dev_addr, peer_addr, ETH_ALEN);
7348 wpa_s->pending_pd_config_methods = config_methods;
7349 wpa_s->p2p_auto_pd = 1;
7350 wpa_s->p2p_auto_join = 0;
7351 wpa_s->pending_pd_before_join = 0;
7352 wpa_s->auto_pd_scan_retry = 0;
7353 wpas_p2p_stop_find(wpa_s);
7354 wpa_s->p2p_join_scan_count = 0;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08007355 os_get_reltime(&wpa_s->p2p_auto_started);
Dmitry Shmidt04949592012-07-19 12:16:46 -07007356 wpa_printf(MSG_DEBUG, "P2P: Auto PD started at %ld.%06ld",
7357 wpa_s->p2p_auto_started.sec,
7358 wpa_s->p2p_auto_started.usec);
7359 wpas_p2p_join_scan(wpa_s, NULL);
7360 return 0;
7361 }
7362
Dmitry Shmidt216983b2015-02-06 10:50:36 -08007363 if (wpa_s->global->p2p == NULL || wpa_s->global->p2p_disabled) {
7364 os_free(p2ps_prov);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007365 return -1;
Dmitry Shmidt216983b2015-02-06 10:50:36 -08007366 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007367
Dmitry Shmidt216983b2015-02-06 10:50:36 -08007368 return p2p_prov_disc_req(wpa_s->global->p2p, peer_addr, p2ps_prov,
Dmitry Shmidt04949592012-07-19 12:16:46 -07007369 config_methods, use == WPAS_P2P_PD_FOR_JOIN,
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08007370 0, 1);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007371}
7372
7373
7374int wpas_p2p_scan_result_text(const u8 *ies, size_t ies_len, char *buf,
7375 char *end)
7376{
7377 return p2p_scan_result_text(ies, ies_len, buf, end);
7378}
7379
7380
7381static void wpas_p2p_clear_pending_action_tx(struct wpa_supplicant *wpa_s)
7382{
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08007383 if (!offchannel_pending_action_tx(wpa_s))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007384 return;
7385
Dmitry Shmidtde47be72016-01-07 12:52:55 -08007386 if (wpa_s->p2p_send_action_work) {
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08007387 wpas_p2p_free_send_action_work(wpa_s);
Dmitry Shmidtde47be72016-01-07 12:52:55 -08007388 eloop_cancel_timeout(wpas_p2p_send_action_work_timeout,
7389 wpa_s, NULL);
7390 offchannel_send_action_done(wpa_s);
7391 }
Dmitry Shmidt03658832014-08-13 11:03:49 -07007392
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007393 wpa_printf(MSG_DEBUG, "P2P: Drop pending Action TX due to new "
7394 "operation request");
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08007395 offchannel_clear_pending_action_tx(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007396}
7397
7398
7399int wpas_p2p_find(struct wpa_supplicant *wpa_s, unsigned int timeout,
7400 enum p2p_discovery_type type,
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08007401 unsigned int num_req_dev_types, const u8 *req_dev_types,
Dmitry Shmidt216983b2015-02-06 10:50:36 -08007402 const u8 *dev_id, unsigned int search_delay,
Hai Shalomc1a21442022-02-04 13:43:00 -08007403 u8 seek_cnt, const char **seek_string, int freq,
7404 bool include_6ghz)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007405{
7406 wpas_p2p_clear_pending_action_tx(wpa_s);
Hai Shalomfdcde762020-04-02 11:19:20 -07007407 wpa_s->global->p2p_long_listen = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007408
Dmitry Shmidt04949592012-07-19 12:16:46 -07007409 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL ||
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07007410 wpa_s->p2p_in_provisioning) {
7411 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Reject p2p_find operation%s%s",
7412 (wpa_s->global->p2p_disabled || !wpa_s->global->p2p) ?
7413 " (P2P disabled)" : "",
7414 wpa_s->p2p_in_provisioning ?
7415 " (p2p_in_provisioning)" : "");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007416 return -1;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07007417 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007418
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08007419 wpa_supplicant_cancel_sched_scan(wpa_s);
7420
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007421 return p2p_find(wpa_s->global->p2p, timeout, type,
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07007422 num_req_dev_types, req_dev_types, dev_id,
Hai Shalomc1a21442022-02-04 13:43:00 -08007423 search_delay, seek_cnt, seek_string, freq,
7424 include_6ghz);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007425}
7426
7427
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007428static void wpas_p2p_scan_res_ignore_search(struct wpa_supplicant *wpa_s,
7429 struct wpa_scan_results *scan_res)
7430{
7431 wpa_printf(MSG_DEBUG, "P2P: Ignore scan results");
7432
7433 if (wpa_s->p2p_scan_work) {
7434 struct wpa_radio_work *work = wpa_s->p2p_scan_work;
7435 wpa_s->p2p_scan_work = NULL;
7436 radio_work_done(work);
7437 }
7438
7439 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
7440 return;
7441
7442 /*
7443 * Indicate that results have been processed so that the P2P module can
7444 * continue pending tasks.
7445 */
Hai Shalom60840252021-02-19 19:02:11 -08007446 wpas_p2p_scan_res_handled(wpa_s);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007447}
7448
7449
7450static void wpas_p2p_stop_find_oper(struct wpa_supplicant *wpa_s)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007451{
7452 wpas_p2p_clear_pending_action_tx(wpa_s);
Hai Shalomfdcde762020-04-02 11:19:20 -07007453 wpa_s->global->p2p_long_listen = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007454 eloop_cancel_timeout(wpas_p2p_long_listen_timeout, wpa_s, NULL);
7455 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007456
7457 if (wpa_s->global->p2p)
7458 p2p_stop_find(wpa_s->global->p2p);
7459
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007460 if (wpa_s->scan_res_handler == wpas_p2p_scan_res_handler) {
7461 wpa_printf(MSG_DEBUG,
7462 "P2P: Do not consider the scan results after stop_find");
7463 wpa_s->scan_res_handler = wpas_p2p_scan_res_ignore_search;
7464 }
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08007465}
7466
7467
7468void wpas_p2p_stop_find(struct wpa_supplicant *wpa_s)
7469{
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007470 wpas_p2p_stop_find_oper(wpa_s);
Dmitry Shmidt216983b2015-02-06 10:50:36 -08007471 if (!wpa_s->global->pending_group_iface_for_p2ps)
7472 wpas_p2p_remove_pending_group_interface(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007473}
7474
7475
7476static void wpas_p2p_long_listen_timeout(void *eloop_ctx, void *timeout_ctx)
7477{
7478 struct wpa_supplicant *wpa_s = eloop_ctx;
Hai Shalomfdcde762020-04-02 11:19:20 -07007479 wpa_s->global->p2p_long_listen = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007480}
7481
7482
7483int wpas_p2p_listen(struct wpa_supplicant *wpa_s, unsigned int timeout)
7484{
7485 int res;
7486
7487 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
7488 return -1;
7489
Dmitry Shmidt58d12ad2016-07-28 10:07:03 -07007490 if (wpa_s->p2p_lo_started) {
7491 wpa_printf(MSG_DEBUG,
7492 "P2P: Cannot start P2P listen, it is offloaded");
7493 return -1;
7494 }
7495
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08007496 wpa_supplicant_cancel_sched_scan(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007497 wpas_p2p_clear_pending_action_tx(wpa_s);
7498
7499 if (timeout == 0) {
7500 /*
7501 * This is a request for unlimited Listen state. However, at
7502 * least for now, this is mapped to a Listen state for one
7503 * hour.
7504 */
7505 timeout = 3600;
7506 }
7507 eloop_cancel_timeout(wpas_p2p_long_listen_timeout, wpa_s, NULL);
Hai Shalomfdcde762020-04-02 11:19:20 -07007508 wpa_s->global->p2p_long_listen = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007509
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007510 /*
7511 * Stop previous find/listen operation to avoid trying to request a new
7512 * remain-on-channel operation while the driver is still running the
7513 * previous one.
7514 */
7515 if (wpa_s->global->p2p)
7516 p2p_stop_find(wpa_s->global->p2p);
7517
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007518 res = wpas_p2p_listen_start(wpa_s, timeout * 1000);
7519 if (res == 0 && timeout * 1000 > wpa_s->max_remain_on_chan) {
Hai Shalomfdcde762020-04-02 11:19:20 -07007520 wpa_s->global->p2p_long_listen = timeout * 1000;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007521 eloop_register_timeout(timeout, 0,
7522 wpas_p2p_long_listen_timeout,
7523 wpa_s, NULL);
7524 }
7525
7526 return res;
7527}
7528
7529
7530int wpas_p2p_assoc_req_ie(struct wpa_supplicant *wpa_s, struct wpa_bss *bss,
7531 u8 *buf, size_t len, int p2p_group)
7532{
7533 struct wpabuf *p2p_ie;
7534 int ret;
7535
7536 if (wpa_s->global->p2p_disabled)
7537 return -1;
Dmitry Shmidt8bd70b72015-05-26 16:02:19 -07007538 /*
7539 * Advertize mandatory cross connection capability even on
7540 * p2p_disabled=1 interface when associating with a P2P Manager WLAN AP.
7541 */
7542 if (wpa_s->conf->p2p_disabled && p2p_group)
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07007543 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007544 if (wpa_s->global->p2p == NULL)
7545 return -1;
7546 if (bss == NULL)
7547 return -1;
7548
7549 p2p_ie = wpa_bss_get_vendor_ie_multi(bss, P2P_IE_VENDOR_TYPE);
7550 ret = p2p_assoc_req_ie(wpa_s->global->p2p, bss->bssid, buf, len,
7551 p2p_group, p2p_ie);
7552 wpabuf_free(p2p_ie);
7553
7554 return ret;
7555}
7556
7557
7558int wpas_p2p_probe_req_rx(struct wpa_supplicant *wpa_s, const u8 *addr,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007559 const u8 *dst, const u8 *bssid,
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07007560 const u8 *ie, size_t ie_len,
7561 unsigned int rx_freq, int ssi_signal)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007562{
7563 if (wpa_s->global->p2p_disabled)
7564 return 0;
7565 if (wpa_s->global->p2p == NULL)
7566 return 0;
7567
Dmitry Shmidt04949592012-07-19 12:16:46 -07007568 switch (p2p_probe_req_rx(wpa_s->global->p2p, addr, dst, bssid,
Dmitry Shmidt58d12ad2016-07-28 10:07:03 -07007569 ie, ie_len, rx_freq, wpa_s->p2p_lo_started)) {
Dmitry Shmidt04949592012-07-19 12:16:46 -07007570 case P2P_PREQ_NOT_P2P:
7571 wpas_notify_preq(wpa_s, addr, dst, bssid, ie, ie_len,
7572 ssi_signal);
7573 /* fall through */
7574 case P2P_PREQ_MALFORMED:
7575 case P2P_PREQ_NOT_LISTEN:
7576 case P2P_PREQ_NOT_PROCESSED:
7577 default: /* make gcc happy */
7578 return 0;
7579 case P2P_PREQ_PROCESSED:
7580 return 1;
7581 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007582}
7583
7584
7585void wpas_p2p_rx_action(struct wpa_supplicant *wpa_s, const u8 *da,
7586 const u8 *sa, const u8 *bssid,
7587 u8 category, const u8 *data, size_t len, int freq)
7588{
7589 if (wpa_s->global->p2p_disabled)
7590 return;
7591 if (wpa_s->global->p2p == NULL)
7592 return;
7593
7594 p2p_rx_action(wpa_s->global->p2p, da, sa, bssid, category, data, len,
7595 freq);
7596}
7597
7598
7599void wpas_p2p_scan_ie(struct wpa_supplicant *wpa_s, struct wpabuf *ies)
7600{
Dmitry Shmidt9c175262016-03-03 10:20:07 -08007601 unsigned int bands;
7602
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007603 if (wpa_s->global->p2p_disabled)
7604 return;
7605 if (wpa_s->global->p2p == NULL)
7606 return;
7607
Dmitry Shmidt9c175262016-03-03 10:20:07 -08007608 bands = wpas_get_bands(wpa_s, NULL);
7609 p2p_scan_ie(wpa_s->global->p2p, ies, NULL, bands);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007610}
7611
7612
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07007613static void wpas_p2p_group_deinit(struct wpa_supplicant *wpa_s)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007614{
7615 p2p_group_deinit(wpa_s->p2p_group);
7616 wpa_s->p2p_group = NULL;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007617
7618 wpa_s->ap_configured_cb = NULL;
7619 wpa_s->ap_configured_cb_ctx = NULL;
7620 wpa_s->ap_configured_cb_data = NULL;
7621 wpa_s->connect_without_scan = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007622}
7623
7624
7625int wpas_p2p_reject(struct wpa_supplicant *wpa_s, const u8 *addr)
7626{
Hai Shalomfdcde762020-04-02 11:19:20 -07007627 wpa_s->global->p2p_long_listen = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007628
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007629 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
7630 return -1;
7631
7632 return p2p_reject(wpa_s->global->p2p, addr);
7633}
7634
7635
7636/* Invite to reinvoke a persistent group */
7637int wpas_p2p_invite(struct wpa_supplicant *wpa_s, const u8 *peer_addr,
Jouni Malinen31be0a42012-08-31 21:20:51 +03007638 struct wpa_ssid *ssid, const u8 *go_dev_addr, int freq,
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08007639 int vht_center_freq2, int ht40, int vht, int max_chwidth,
Hai Shalomc1a21442022-02-04 13:43:00 -08007640 int pref_freq, int he, int edmg, bool allow_6ghz)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007641{
7642 enum p2p_invite_role role;
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07007643 u8 *bssid = NULL;
7644 int force_freq = 0;
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08007645 int res;
Dmitry Shmidta0d265f2013-11-19 13:13:41 -08007646 int no_pref_freq_given = pref_freq == 0;
Sunil8cd6f4d2022-06-28 18:40:46 +00007647 struct weighted_pcl pref_freq_list[P2P_MAX_PREF_CHANNELS];
7648 unsigned int size;
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08007649
Hai Shalomc1a21442022-02-04 13:43:00 -08007650 if (wpas_p2p_check_6ghz(wpa_s, NULL, allow_6ghz, freq))
7651 return -1;
7652
Dmitry Shmidt700a1372013-03-15 14:14:44 -07007653 wpa_s->global->p2p_invite_group = NULL;
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08007654 if (peer_addr)
7655 os_memcpy(wpa_s->p2p_auth_invite, peer_addr, ETH_ALEN);
7656 else
7657 os_memset(wpa_s->p2p_auth_invite, 0, ETH_ALEN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007658
Jouni Malinen31be0a42012-08-31 21:20:51 +03007659 wpa_s->p2p_persistent_go_freq = freq;
7660 wpa_s->p2p_go_ht40 = !!ht40;
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08007661 wpa_s->p2p_go_vht = !!vht;
Hai Shalom74f70d42019-02-11 14:42:39 -08007662 wpa_s->p2p_go_he = !!he;
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08007663 wpa_s->p2p_go_max_oper_chwidth = max_chwidth;
7664 wpa_s->p2p_go_vht_center_freq2 = vht_center_freq2;
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08007665 wpa_s->p2p_go_edmg = !!edmg;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007666 if (ssid->mode == WPAS_MODE_P2P_GO) {
7667 role = P2P_INVITE_ROLE_GO;
7668 if (peer_addr == NULL) {
7669 wpa_printf(MSG_DEBUG, "P2P: Missing peer "
7670 "address in invitation command");
7671 return -1;
7672 }
7673 if (wpas_p2p_create_iface(wpa_s)) {
7674 if (wpas_p2p_add_group_interface(wpa_s,
7675 WPA_IF_P2P_GO) < 0) {
7676 wpa_printf(MSG_ERROR, "P2P: Failed to "
7677 "allocate a new interface for the "
7678 "group");
7679 return -1;
7680 }
7681 bssid = wpa_s->pending_interface_addr;
Dmitry Shmidt9c175262016-03-03 10:20:07 -08007682 } else if (wpa_s->p2p_mgmt)
7683 bssid = wpa_s->parent->own_addr;
7684 else
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007685 bssid = wpa_s->own_addr;
7686 } else {
7687 role = P2P_INVITE_ROLE_CLIENT;
7688 peer_addr = ssid->bssid;
7689 }
7690 wpa_s->pending_invite_ssid_id = ssid->id;
7691
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08007692 size = P2P_MAX_PREF_CHANNELS;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07007693 res = wpas_p2p_setup_freqs(wpa_s, freq, &force_freq, &pref_freq,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08007694 role == P2P_INVITE_ROLE_GO,
7695 pref_freq_list, &size);
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08007696 if (res)
7697 return res;
Irfan Sheriffaf84a572012-09-22 16:59:30 -07007698
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007699 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
7700 return -1;
7701
Dmitry Shmidt7d56b752015-12-22 10:59:44 -08007702 p2p_set_own_pref_freq_list(wpa_s->global->p2p, pref_freq_list, size);
7703
Dmitry Shmidta0d265f2013-11-19 13:13:41 -08007704 if (wpa_s->parent->conf->p2p_ignore_shared_freq &&
7705 no_pref_freq_given && pref_freq > 0 &&
7706 wpa_s->num_multichan_concurrent > 1 &&
7707 wpas_p2p_num_unused_channels(wpa_s) > 0) {
7708 wpa_printf(MSG_DEBUG, "P2P: Ignore own channel preference %d MHz for invitation due to p2p_ignore_shared_freq=1 configuration",
7709 pref_freq);
7710 pref_freq = 0;
7711 }
7712
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08007713 /*
7714 * Stop any find/listen operations before invitation and possibly
7715 * connection establishment.
7716 */
7717 wpas_p2p_stop_find_oper(wpa_s);
7718
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007719 return p2p_invite(wpa_s->global->p2p, peer_addr, role, bssid,
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08007720 ssid->ssid, ssid->ssid_len, force_freq, go_dev_addr,
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08007721 1, pref_freq, -1);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007722}
7723
7724
7725/* Invite to join an active group */
7726int wpas_p2p_invite_group(struct wpa_supplicant *wpa_s, const char *ifname,
Hai Shalomc1a21442022-02-04 13:43:00 -08007727 const u8 *peer_addr, const u8 *go_dev_addr,
7728 bool allow_6ghz)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007729{
7730 struct wpa_global *global = wpa_s->global;
7731 enum p2p_invite_role role;
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07007732 u8 *bssid = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007733 struct wpa_ssid *ssid;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08007734 int persistent;
Dmitry Shmidt051af732013-10-22 13:52:46 -07007735 int freq = 0, force_freq = 0, pref_freq = 0;
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08007736 int res;
Sunil8cd6f4d2022-06-28 18:40:46 +00007737 struct weighted_pcl pref_freq_list[P2P_MAX_PREF_CHANNELS];
7738 unsigned int size;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007739
Jouni Malinen31be0a42012-08-31 21:20:51 +03007740 wpa_s->p2p_persistent_go_freq = 0;
7741 wpa_s->p2p_go_ht40 = 0;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07007742 wpa_s->p2p_go_vht = 0;
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08007743 wpa_s->p2p_go_vht_center_freq2 = 0;
7744 wpa_s->p2p_go_max_oper_chwidth = 0;
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08007745 wpa_s->p2p_go_edmg = 0;
Jouni Malinen31be0a42012-08-31 21:20:51 +03007746
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007747 for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
7748 if (os_strcmp(wpa_s->ifname, ifname) == 0)
7749 break;
7750 }
7751 if (wpa_s == NULL) {
7752 wpa_printf(MSG_DEBUG, "P2P: Interface '%s' not found", ifname);
7753 return -1;
7754 }
7755
7756 ssid = wpa_s->current_ssid;
7757 if (ssid == NULL) {
7758 wpa_printf(MSG_DEBUG, "P2P: No current SSID to use for "
7759 "invitation");
7760 return -1;
7761 }
7762
Dmitry Shmidt700a1372013-03-15 14:14:44 -07007763 wpa_s->global->p2p_invite_group = wpa_s;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08007764 persistent = ssid->p2p_persistent_group &&
Dmitry Shmidt9c175262016-03-03 10:20:07 -08007765 wpas_p2p_get_persistent(wpa_s->p2pdev, peer_addr,
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08007766 ssid->ssid, ssid->ssid_len);
7767
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007768 if (ssid->mode == WPAS_MODE_P2P_GO) {
7769 role = P2P_INVITE_ROLE_ACTIVE_GO;
7770 bssid = wpa_s->own_addr;
7771 if (go_dev_addr == NULL)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007772 go_dev_addr = wpa_s->global->p2p_dev_addr;
Dmitry Shmidt051af732013-10-22 13:52:46 -07007773 freq = ssid->frequency;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007774 } else {
7775 role = P2P_INVITE_ROLE_CLIENT;
7776 if (wpa_s->wpa_state < WPA_ASSOCIATED) {
7777 wpa_printf(MSG_DEBUG, "P2P: Not associated - cannot "
7778 "invite to current group");
7779 return -1;
7780 }
7781 bssid = wpa_s->bssid;
7782 if (go_dev_addr == NULL &&
7783 !is_zero_ether_addr(wpa_s->go_dev_addr))
7784 go_dev_addr = wpa_s->go_dev_addr;
Dmitry Shmidt051af732013-10-22 13:52:46 -07007785 freq = wpa_s->current_bss ? wpa_s->current_bss->freq :
7786 (int) wpa_s->assoc_freq;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007787 }
Dmitry Shmidt9c175262016-03-03 10:20:07 -08007788 wpa_s->p2pdev->pending_invite_ssid_id = -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007789
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007790 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
7791 return -1;
Hai Shalomc1a21442022-02-04 13:43:00 -08007792 if (wpas_p2p_check_6ghz(wpa_s, peer_addr, allow_6ghz, freq))
7793 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007794
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08007795 size = P2P_MAX_PREF_CHANNELS;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07007796 res = wpas_p2p_setup_freqs(wpa_s, freq, &force_freq, &pref_freq,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08007797 role == P2P_INVITE_ROLE_ACTIVE_GO,
7798 pref_freq_list, &size);
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08007799 if (res)
7800 return res;
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07007801 wpas_p2p_set_own_freq_preference(wpa_s, force_freq);
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08007802
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007803 return p2p_invite(wpa_s->global->p2p, peer_addr, role, bssid,
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08007804 ssid->ssid, ssid->ssid_len, force_freq,
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08007805 go_dev_addr, persistent, pref_freq, -1);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007806}
7807
7808
7809void wpas_p2p_completed(struct wpa_supplicant *wpa_s)
7810{
7811 struct wpa_ssid *ssid = wpa_s->current_ssid;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007812 u8 go_dev_addr[ETH_ALEN];
7813 int persistent;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007814 int freq;
Hai Shalom5f92bc92019-04-18 11:54:11 -07007815 u8 ip[3 * 4], *ip_ptr = NULL;
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08007816 char ip_addr[100];
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007817
Dmitry Shmidt04949592012-07-19 12:16:46 -07007818 if (ssid == NULL || ssid->mode != WPAS_MODE_P2P_GROUP_FORMATION) {
7819 eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
Dmitry Shmidt9c175262016-03-03 10:20:07 -08007820 wpa_s->p2pdev, NULL);
Dmitry Shmidt04949592012-07-19 12:16:46 -07007821 }
7822
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007823 if (!wpa_s->show_group_started || !ssid)
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08007824 return;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007825
7826 wpa_s->show_group_started = 0;
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08007827 if (!wpa_s->p2p_go_group_formation_completed &&
7828 wpa_s->global->p2p_group_formation == wpa_s) {
7829 wpa_dbg(wpa_s, MSG_DEBUG,
7830 "P2P: Marking group formation completed on client on data connection");
7831 wpa_s->p2p_go_group_formation_completed = 1;
7832 wpa_s->global->p2p_group_formation = NULL;
7833 wpa_s->p2p_in_provisioning = 0;
7834 wpa_s->p2p_in_invitation = 0;
7835 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007836
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007837 os_memset(go_dev_addr, 0, ETH_ALEN);
7838 if (ssid->bssid_set)
7839 os_memcpy(go_dev_addr, ssid->bssid, ETH_ALEN);
7840 persistent = wpas_p2p_persistent_group(wpa_s, go_dev_addr, ssid->ssid,
7841 ssid->ssid_len);
7842 os_memcpy(wpa_s->go_dev_addr, go_dev_addr, ETH_ALEN);
7843
7844 if (wpa_s->global->p2p_group_formation == wpa_s)
7845 wpa_s->global->p2p_group_formation = NULL;
7846
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007847 freq = wpa_s->current_bss ? wpa_s->current_bss->freq :
7848 (int) wpa_s->assoc_freq;
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08007849
7850 ip_addr[0] = '\0';
7851 if (wpa_sm_get_p2p_ip_addr(wpa_s->wpa, ip) == 0) {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007852 int res;
7853
7854 res = os_snprintf(ip_addr, sizeof(ip_addr),
7855 " ip_addr=%u.%u.%u.%u "
7856 "ip_mask=%u.%u.%u.%u go_ip_addr=%u.%u.%u.%u",
7857 ip[0], ip[1], ip[2], ip[3],
7858 ip[4], ip[5], ip[6], ip[7],
7859 ip[8], ip[9], ip[10], ip[11]);
7860 if (os_snprintf_error(sizeof(ip_addr), res))
7861 ip_addr[0] = '\0';
Hai Shalom5f92bc92019-04-18 11:54:11 -07007862 ip_ptr = ip;
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08007863 }
7864
Dmitry Shmidt7f0b69e2014-07-28 10:35:20 -07007865 wpas_p2p_group_started(wpa_s, 0, ssid, freq,
7866 ssid->passphrase == NULL && ssid->psk_set ?
7867 ssid->psk : NULL,
7868 ssid->passphrase, go_dev_addr, persistent,
7869 ip_addr);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007870
7871 if (persistent)
Dmitry Shmidtd5ab1b52016-06-21 12:38:41 -07007872 wpas_p2p_store_persistent_group(wpa_s->p2pdev,
7873 ssid, go_dev_addr);
7874
Hai Shalom5f92bc92019-04-18 11:54:11 -07007875 wpas_notify_p2p_group_started(wpa_s, ssid, persistent, 1, ip_ptr);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007876}
7877
7878
7879int wpas_p2p_presence_req(struct wpa_supplicant *wpa_s, u32 duration1,
7880 u32 interval1, u32 duration2, u32 interval2)
7881{
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08007882 int ret;
7883
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007884 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
7885 return -1;
7886
7887 if (wpa_s->wpa_state < WPA_ASSOCIATED ||
7888 wpa_s->current_ssid == NULL ||
7889 wpa_s->current_ssid->mode != WPAS_MODE_INFRA)
7890 return -1;
7891
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08007892 ret = p2p_presence_req(wpa_s->global->p2p, wpa_s->bssid,
7893 wpa_s->own_addr, wpa_s->assoc_freq,
7894 duration1, interval1, duration2, interval2);
7895 if (ret == 0)
7896 wpa_s->waiting_presence_resp = 1;
7897
7898 return ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007899}
7900
7901
7902int wpas_p2p_ext_listen(struct wpa_supplicant *wpa_s, unsigned int period,
7903 unsigned int interval)
7904{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007905 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
7906 return -1;
7907
7908 return p2p_ext_listen(wpa_s->global->p2p, period, interval);
7909}
7910
7911
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007912static int wpas_p2p_is_client(struct wpa_supplicant *wpa_s)
7913{
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08007914 if (wpa_s->current_ssid == NULL) {
7915 /*
7916 * current_ssid can be cleared when P2P client interface gets
7917 * disconnected, so assume this interface was used as P2P
7918 * client.
7919 */
7920 return 1;
7921 }
7922 return wpa_s->current_ssid->p2p_group &&
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007923 wpa_s->current_ssid->mode == WPAS_MODE_INFRA;
7924}
7925
7926
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007927static void wpas_p2p_group_idle_timeout(void *eloop_ctx, void *timeout_ctx)
7928{
7929 struct wpa_supplicant *wpa_s = eloop_ctx;
7930
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007931 if (wpa_s->conf->p2p_group_idle == 0 && !wpas_p2p_is_client(wpa_s)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007932 wpa_printf(MSG_DEBUG, "P2P: Ignore group idle timeout - "
7933 "disabled");
7934 return;
7935 }
7936
Dmitry Shmidt04949592012-07-19 12:16:46 -07007937 wpa_printf(MSG_DEBUG, "P2P: Group idle timeout reached - terminate "
7938 "group");
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07007939 wpas_p2p_group_delete(wpa_s, P2P_GROUP_REMOVAL_IDLE_TIMEOUT);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007940}
7941
7942
7943static void wpas_p2p_set_group_idle_timeout(struct wpa_supplicant *wpa_s)
7944{
Dmitry Shmidt04949592012-07-19 12:16:46 -07007945 int timeout;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007946
Dmitry Shmidt04949592012-07-19 12:16:46 -07007947 if (eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL) > 0)
7948 wpa_printf(MSG_DEBUG, "P2P: Cancelled P2P group idle timeout");
7949
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007950 if (wpa_s->current_ssid == NULL || !wpa_s->current_ssid->p2p_group)
7951 return;
7952
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007953 timeout = wpa_s->conf->p2p_group_idle;
7954 if (wpa_s->current_ssid->mode == WPAS_MODE_INFRA &&
7955 (timeout == 0 || timeout > P2P_MAX_CLIENT_IDLE))
7956 timeout = P2P_MAX_CLIENT_IDLE;
7957
7958 if (timeout == 0)
7959 return;
7960
Dmitry Shmidt04949592012-07-19 12:16:46 -07007961 if (timeout < 0) {
7962 if (wpa_s->current_ssid->mode == WPAS_MODE_INFRA)
7963 timeout = 0; /* special client mode no-timeout */
7964 else
7965 return;
7966 }
7967
7968 if (wpa_s->p2p_in_provisioning) {
7969 /*
7970 * Use the normal group formation timeout during the
7971 * provisioning phase to avoid terminating this process too
7972 * early due to group idle timeout.
7973 */
7974 wpa_printf(MSG_DEBUG, "P2P: Do not use P2P group idle timeout "
7975 "during provisioning");
7976 return;
7977 }
Deepthi Gowri9e4e8ac2013-04-16 11:57:05 +05307978
Dmitry Shmidt04949592012-07-19 12:16:46 -07007979 if (wpa_s->show_group_started) {
7980 /*
7981 * Use the normal group formation timeout between the end of
7982 * the provisioning phase and completion of 4-way handshake to
7983 * avoid terminating this process too early due to group idle
7984 * timeout.
7985 */
7986 wpa_printf(MSG_DEBUG, "P2P: Do not use P2P group idle timeout "
7987 "while waiting for initial 4-way handshake to "
7988 "complete");
7989 return;
7990 }
7991
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007992 wpa_printf(MSG_DEBUG, "P2P: Set P2P group idle timeout to %u seconds",
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007993 timeout);
7994 eloop_register_timeout(timeout, 0, wpas_p2p_group_idle_timeout,
7995 wpa_s, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007996}
7997
7998
Jouni Malinen2b89da82012-08-31 22:04:41 +03007999/* Returns 1 if the interface was removed */
8000int wpas_p2p_deauth_notif(struct wpa_supplicant *wpa_s, const u8 *bssid,
8001 u16 reason_code, const u8 *ie, size_t ie_len,
8002 int locally_generated)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008003{
8004 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
Jouni Malinen2b89da82012-08-31 22:04:41 +03008005 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008006
Dmitry Shmidt04949592012-07-19 12:16:46 -07008007 if (!locally_generated)
8008 p2p_deauth_notif(wpa_s->global->p2p, bssid, reason_code, ie,
8009 ie_len);
8010
8011 if (reason_code == WLAN_REASON_DEAUTH_LEAVING && !locally_generated &&
8012 wpa_s->current_ssid &&
8013 wpa_s->current_ssid->p2p_group &&
8014 wpa_s->current_ssid->mode == WPAS_MODE_INFRA) {
8015 wpa_printf(MSG_DEBUG, "P2P: GO indicated that the P2P Group "
8016 "session is ending");
Jouni Malinen2b89da82012-08-31 22:04:41 +03008017 if (wpas_p2p_group_delete(wpa_s,
8018 P2P_GROUP_REMOVAL_GO_ENDING_SESSION)
8019 > 0)
8020 return 1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07008021 }
Jouni Malinen2b89da82012-08-31 22:04:41 +03008022
8023 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008024}
8025
8026
8027void wpas_p2p_disassoc_notif(struct wpa_supplicant *wpa_s, const u8 *bssid,
Dmitry Shmidt04949592012-07-19 12:16:46 -07008028 u16 reason_code, const u8 *ie, size_t ie_len,
8029 int locally_generated)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008030{
8031 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
8032 return;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008033
Dmitry Shmidt04949592012-07-19 12:16:46 -07008034 if (!locally_generated)
8035 p2p_disassoc_notif(wpa_s->global->p2p, bssid, reason_code, ie,
8036 ie_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008037}
8038
8039
8040void wpas_p2p_update_config(struct wpa_supplicant *wpa_s)
8041{
8042 struct p2p_data *p2p = wpa_s->global->p2p;
8043
8044 if (p2p == NULL)
8045 return;
8046
8047 if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_CAPABLE))
8048 return;
8049
8050 if (wpa_s->conf->changed_parameters & CFG_CHANGED_DEVICE_NAME)
8051 p2p_set_dev_name(p2p, wpa_s->conf->device_name);
8052
8053 if (wpa_s->conf->changed_parameters & CFG_CHANGED_DEVICE_TYPE)
8054 p2p_set_pri_dev_type(p2p, wpa_s->conf->device_type);
8055
8056 if (wpa_s->wps &&
8057 (wpa_s->conf->changed_parameters & CFG_CHANGED_CONFIG_METHODS))
8058 p2p_set_config_methods(p2p, wpa_s->wps->config_methods);
8059
8060 if (wpa_s->wps && (wpa_s->conf->changed_parameters & CFG_CHANGED_UUID))
8061 p2p_set_uuid(p2p, wpa_s->wps->uuid);
8062
8063 if (wpa_s->conf->changed_parameters & CFG_CHANGED_WPS_STRING) {
8064 p2p_set_manufacturer(p2p, wpa_s->conf->manufacturer);
8065 p2p_set_model_name(p2p, wpa_s->conf->model_name);
8066 p2p_set_model_number(p2p, wpa_s->conf->model_number);
8067 p2p_set_serial_number(p2p, wpa_s->conf->serial_number);
8068 }
8069
8070 if (wpa_s->conf->changed_parameters & CFG_CHANGED_SEC_DEVICE_TYPE)
8071 p2p_set_sec_dev_types(p2p,
8072 (void *) wpa_s->conf->sec_device_type,
8073 wpa_s->conf->num_sec_device_types);
8074
8075 if (wpa_s->conf->changed_parameters & CFG_CHANGED_VENDOR_EXTENSION) {
8076 int i;
8077 p2p_remove_wps_vendor_extensions(p2p);
8078 for (i = 0; i < MAX_WPS_VENDOR_EXT; i++) {
8079 if (wpa_s->conf->wps_vendor_ext[i] == NULL)
8080 continue;
8081 p2p_add_wps_vendor_extension(
8082 p2p, wpa_s->conf->wps_vendor_ext[i]);
8083 }
8084 }
8085
8086 if ((wpa_s->conf->changed_parameters & CFG_CHANGED_COUNTRY) &&
8087 wpa_s->conf->country[0] && wpa_s->conf->country[1]) {
8088 char country[3];
8089 country[0] = wpa_s->conf->country[0];
8090 country[1] = wpa_s->conf->country[1];
8091 country[2] = 0x04;
8092 p2p_set_country(p2p, country);
8093 }
8094
8095 if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_SSID_POSTFIX) {
8096 p2p_set_ssid_postfix(p2p, (u8 *) wpa_s->conf->p2p_ssid_postfix,
8097 wpa_s->conf->p2p_ssid_postfix ?
8098 os_strlen(wpa_s->conf->p2p_ssid_postfix) :
8099 0);
8100 }
8101
8102 if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_INTRA_BSS)
8103 p2p_set_intra_bss_dist(p2p, wpa_s->conf->p2p_intra_bss);
Jouni Malinen75ecf522011-06-27 15:19:46 -07008104
8105 if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_LISTEN_CHANNEL) {
8106 u8 reg_class, channel;
8107 int ret;
8108 unsigned int r;
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07008109 u8 channel_forced;
8110
Jouni Malinen75ecf522011-06-27 15:19:46 -07008111 if (wpa_s->conf->p2p_listen_reg_class &&
8112 wpa_s->conf->p2p_listen_channel) {
8113 reg_class = wpa_s->conf->p2p_listen_reg_class;
8114 channel = wpa_s->conf->p2p_listen_channel;
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07008115 channel_forced = 1;
Jouni Malinen75ecf522011-06-27 15:19:46 -07008116 } else {
8117 reg_class = 81;
8118 /*
8119 * Pick one of the social channels randomly as the
8120 * listen channel.
8121 */
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07008122 if (os_get_random((u8 *) &r, sizeof(r)) < 0)
8123 channel = 1;
8124 else
8125 channel = 1 + (r % 3) * 5;
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07008126 channel_forced = 0;
Jouni Malinen75ecf522011-06-27 15:19:46 -07008127 }
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07008128 ret = p2p_set_listen_channel(p2p, reg_class, channel,
8129 channel_forced);
Jouni Malinen75ecf522011-06-27 15:19:46 -07008130 if (ret)
8131 wpa_printf(MSG_ERROR, "P2P: Own listen channel update "
8132 "failed: %d", ret);
8133 }
8134 if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_OPER_CHANNEL) {
8135 u8 op_reg_class, op_channel, cfg_op_channel;
8136 int ret = 0;
8137 unsigned int r;
8138 if (wpa_s->conf->p2p_oper_reg_class &&
8139 wpa_s->conf->p2p_oper_channel) {
8140 op_reg_class = wpa_s->conf->p2p_oper_reg_class;
8141 op_channel = wpa_s->conf->p2p_oper_channel;
8142 cfg_op_channel = 1;
8143 } else {
8144 op_reg_class = 81;
8145 /*
8146 * Use random operation channel from (1, 6, 11)
8147 *if no other preference is indicated.
8148 */
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07008149 if (os_get_random((u8 *) &r, sizeof(r)) < 0)
8150 op_channel = 1;
8151 else
8152 op_channel = 1 + (r % 3) * 5;
Jouni Malinen75ecf522011-06-27 15:19:46 -07008153 cfg_op_channel = 0;
8154 }
8155 ret = p2p_set_oper_channel(p2p, op_reg_class, op_channel,
8156 cfg_op_channel);
8157 if (ret)
8158 wpa_printf(MSG_ERROR, "P2P: Own oper channel update "
8159 "failed: %d", ret);
8160 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07008161
8162 if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_PREF_CHAN) {
8163 if (p2p_set_pref_chan(p2p, wpa_s->conf->num_p2p_pref_chan,
8164 wpa_s->conf->p2p_pref_chan) < 0) {
8165 wpa_printf(MSG_ERROR, "P2P: Preferred channel list "
8166 "update failed");
8167 }
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07008168
8169 if (p2p_set_no_go_freq(p2p, &wpa_s->conf->p2p_no_go_freq) < 0) {
8170 wpa_printf(MSG_ERROR, "P2P: No GO channel list "
8171 "update failed");
8172 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07008173 }
Dmitry Shmidt2271d3f2014-06-23 12:16:31 -07008174
8175 if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_PASSPHRASE_LEN)
8176 p2p_set_passphrase_len(p2p, wpa_s->conf->p2p_passphrase_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008177}
8178
8179
8180int wpas_p2p_set_noa(struct wpa_supplicant *wpa_s, u8 count, int start,
8181 int duration)
8182{
8183 if (!wpa_s->ap_iface)
8184 return -1;
8185 return hostapd_p2p_set_noa(wpa_s->ap_iface->bss[0], count, start,
8186 duration);
8187}
8188
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008189
8190int wpas_p2p_set_cross_connect(struct wpa_supplicant *wpa_s, int enabled)
8191{
8192 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
8193 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008194
8195 wpa_s->global->cross_connection = enabled;
8196 p2p_set_cross_connect(wpa_s->global->p2p, enabled);
8197
8198 if (!enabled) {
8199 struct wpa_supplicant *iface;
8200
8201 for (iface = wpa_s->global->ifaces; iface; iface = iface->next)
8202 {
8203 if (iface->cross_connect_enabled == 0)
8204 continue;
8205
8206 iface->cross_connect_enabled = 0;
8207 iface->cross_connect_in_use = 0;
Dmitry Shmidt9c175262016-03-03 10:20:07 -08008208 wpa_msg_global(iface->p2pdev, MSG_INFO,
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07008209 P2P_EVENT_CROSS_CONNECT_DISABLE "%s %s",
8210 iface->ifname,
8211 iface->cross_connect_uplink);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008212 }
8213 }
8214
8215 return 0;
8216}
8217
8218
8219static void wpas_p2p_enable_cross_connect(struct wpa_supplicant *uplink)
8220{
8221 struct wpa_supplicant *iface;
8222
8223 if (!uplink->global->cross_connection)
8224 return;
8225
8226 for (iface = uplink->global->ifaces; iface; iface = iface->next) {
8227 if (!iface->cross_connect_enabled)
8228 continue;
8229 if (os_strcmp(uplink->ifname, iface->cross_connect_uplink) !=
8230 0)
8231 continue;
8232 if (iface->ap_iface == NULL)
8233 continue;
8234 if (iface->cross_connect_in_use)
8235 continue;
8236
8237 iface->cross_connect_in_use = 1;
Dmitry Shmidt9c175262016-03-03 10:20:07 -08008238 wpa_msg_global(iface->p2pdev, MSG_INFO,
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07008239 P2P_EVENT_CROSS_CONNECT_ENABLE "%s %s",
8240 iface->ifname, iface->cross_connect_uplink);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008241 }
8242}
8243
8244
8245static void wpas_p2p_disable_cross_connect(struct wpa_supplicant *uplink)
8246{
8247 struct wpa_supplicant *iface;
8248
8249 for (iface = uplink->global->ifaces; iface; iface = iface->next) {
8250 if (!iface->cross_connect_enabled)
8251 continue;
8252 if (os_strcmp(uplink->ifname, iface->cross_connect_uplink) !=
8253 0)
8254 continue;
8255 if (!iface->cross_connect_in_use)
8256 continue;
8257
Dmitry Shmidt9c175262016-03-03 10:20:07 -08008258 wpa_msg_global(iface->p2pdev, MSG_INFO,
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07008259 P2P_EVENT_CROSS_CONNECT_DISABLE "%s %s",
8260 iface->ifname, iface->cross_connect_uplink);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008261 iface->cross_connect_in_use = 0;
8262 }
8263}
8264
8265
8266void wpas_p2p_notif_connected(struct wpa_supplicant *wpa_s)
8267{
8268 if (wpa_s->ap_iface || wpa_s->current_ssid == NULL ||
8269 wpa_s->current_ssid->mode != WPAS_MODE_INFRA ||
8270 wpa_s->cross_connect_disallowed)
8271 wpas_p2p_disable_cross_connect(wpa_s);
8272 else
8273 wpas_p2p_enable_cross_connect(wpa_s);
Dmitry Shmidt04949592012-07-19 12:16:46 -07008274 if (!wpa_s->ap_iface &&
8275 eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL) > 0)
8276 wpa_printf(MSG_DEBUG, "P2P: Cancelled P2P group idle timeout");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008277}
8278
8279
8280void wpas_p2p_notif_disconnected(struct wpa_supplicant *wpa_s)
8281{
8282 wpas_p2p_disable_cross_connect(wpa_s);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008283 if (!wpa_s->ap_iface &&
8284 !eloop_is_timeout_registered(wpas_p2p_group_idle_timeout,
8285 wpa_s, NULL))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008286 wpas_p2p_set_group_idle_timeout(wpa_s);
8287}
8288
8289
8290static void wpas_p2p_cross_connect_setup(struct wpa_supplicant *wpa_s)
8291{
8292 struct wpa_supplicant *iface;
8293
8294 if (!wpa_s->global->cross_connection)
8295 return;
8296
8297 for (iface = wpa_s->global->ifaces; iface; iface = iface->next) {
8298 if (iface == wpa_s)
8299 continue;
8300 if (iface->drv_flags &
8301 WPA_DRIVER_FLAGS_P2P_DEDICATED_INTERFACE)
8302 continue;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08008303 if ((iface->drv_flags & WPA_DRIVER_FLAGS_P2P_CAPABLE) &&
8304 iface != wpa_s->parent)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008305 continue;
8306
8307 wpa_s->cross_connect_enabled = 1;
8308 os_strlcpy(wpa_s->cross_connect_uplink, iface->ifname,
8309 sizeof(wpa_s->cross_connect_uplink));
8310 wpa_printf(MSG_DEBUG, "P2P: Enable cross connection from "
8311 "%s to %s whenever uplink is available",
8312 wpa_s->ifname, wpa_s->cross_connect_uplink);
8313
8314 if (iface->ap_iface || iface->current_ssid == NULL ||
8315 iface->current_ssid->mode != WPAS_MODE_INFRA ||
8316 iface->cross_connect_disallowed ||
8317 iface->wpa_state != WPA_COMPLETED)
8318 break;
8319
8320 wpa_s->cross_connect_in_use = 1;
Dmitry Shmidt9c175262016-03-03 10:20:07 -08008321 wpa_msg_global(wpa_s->p2pdev, MSG_INFO,
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07008322 P2P_EVENT_CROSS_CONNECT_ENABLE "%s %s",
8323 wpa_s->ifname, wpa_s->cross_connect_uplink);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008324 break;
8325 }
8326}
8327
8328
8329int wpas_p2p_notif_pbc_overlap(struct wpa_supplicant *wpa_s)
8330{
8331 if (wpa_s->p2p_group_interface != P2P_GROUP_INTERFACE_CLIENT &&
8332 !wpa_s->p2p_in_provisioning)
8333 return 0; /* not P2P client operation */
8334
8335 wpa_printf(MSG_DEBUG, "P2P: Terminate connection due to WPS PBC "
8336 "session overlap");
Dmitry Shmidt9c175262016-03-03 10:20:07 -08008337 if (wpa_s != wpa_s->p2pdev)
8338 wpa_msg_ctrl(wpa_s->p2pdev, MSG_INFO, WPS_EVENT_OVERLAP);
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07008339 wpas_p2p_group_formation_failed(wpa_s, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008340 return 1;
8341}
8342
8343
Dmitry Shmidtdf5a7e42014-04-02 12:59:59 -07008344void wpas_p2p_pbc_overlap_cb(void *eloop_ctx, void *timeout_ctx)
8345{
8346 struct wpa_supplicant *wpa_s = eloop_ctx;
8347 wpas_p2p_notif_pbc_overlap(wpa_s);
8348}
8349
8350
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08008351void wpas_p2p_update_channel_list(struct wpa_supplicant *wpa_s,
8352 enum wpas_p2p_channel_update_trig trig)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008353{
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07008354 struct p2p_channels chan, cli_chan;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08008355 struct wpa_used_freq_data *freqs = NULL;
8356 unsigned int num = wpa_s->num_multichan_concurrent;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008357
8358 if (wpa_s->global == NULL || wpa_s->global->p2p == NULL)
8359 return;
8360
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08008361 freqs = os_calloc(num, sizeof(struct wpa_used_freq_data));
8362 if (!freqs)
8363 return;
8364
8365 num = get_shared_radio_freqs_data(wpa_s, freqs, num);
8366
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008367 os_memset(&chan, 0, sizeof(chan));
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07008368 os_memset(&cli_chan, 0, sizeof(cli_chan));
Hai Shalom60840252021-02-19 19:02:11 -08008369 if (wpas_p2p_setup_channels(wpa_s, &chan, &cli_chan,
8370 is_p2p_6ghz_disabled(wpa_s->global->p2p))) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008371 wpa_printf(MSG_ERROR, "P2P: Failed to update supported "
8372 "channel list");
8373 return;
8374 }
8375
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07008376 p2p_update_channel_list(wpa_s->global->p2p, &chan, &cli_chan);
Dmitry Shmidt684785c2014-05-12 13:34:29 -07008377
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08008378 wpas_p2p_optimize_listen_channel(wpa_s, freqs, num);
Dmitry Shmidt684785c2014-05-12 13:34:29 -07008379
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08008380 /*
8381 * The used frequencies map changed, so it is possible that a GO is
8382 * using a channel that is no longer valid for P2P use. It is also
8383 * possible that due to policy consideration, it would be preferable to
8384 * move it to a frequency already used by other station interfaces.
8385 */
8386 wpas_p2p_consider_moving_gos(wpa_s, freqs, num, trig);
8387
8388 os_free(freqs);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008389}
8390
8391
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08008392static void wpas_p2p_scan_res_ignore(struct wpa_supplicant *wpa_s,
8393 struct wpa_scan_results *scan_res)
8394{
8395 wpa_printf(MSG_DEBUG, "P2P: Ignore scan results");
8396}
8397
8398
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008399int wpas_p2p_cancel(struct wpa_supplicant *wpa_s)
8400{
8401 struct wpa_global *global = wpa_s->global;
8402 int found = 0;
8403 const u8 *peer;
8404
8405 if (global->p2p == NULL)
8406 return -1;
8407
8408 wpa_printf(MSG_DEBUG, "P2P: Request to cancel group formation");
8409
8410 if (wpa_s->pending_interface_name[0] &&
8411 !is_zero_ether_addr(wpa_s->pending_interface_addr))
8412 found = 1;
8413
8414 peer = p2p_get_go_neg_peer(global->p2p);
8415 if (peer) {
8416 wpa_printf(MSG_DEBUG, "P2P: Unauthorize pending GO Neg peer "
8417 MACSTR, MAC2STR(peer));
8418 p2p_unauthorize(global->p2p, peer);
Dmitry Shmidt04949592012-07-19 12:16:46 -07008419 found = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008420 }
8421
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08008422 if (wpa_s->scan_res_handler == wpas_p2p_scan_res_join) {
8423 wpa_printf(MSG_DEBUG, "P2P: Stop pending scan for join");
8424 wpa_s->scan_res_handler = wpas_p2p_scan_res_ignore;
8425 found = 1;
8426 }
8427
8428 if (wpa_s->pending_pd_before_join) {
8429 wpa_printf(MSG_DEBUG, "P2P: Stop pending PD before join");
8430 wpa_s->pending_pd_before_join = 0;
8431 found = 1;
8432 }
8433
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008434 wpas_p2p_stop_find(wpa_s);
8435
8436 for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
8437 if (wpa_s == global->p2p_group_formation &&
8438 (wpa_s->p2p_in_provisioning ||
8439 wpa_s->parent->pending_interface_type ==
8440 WPA_IF_P2P_CLIENT)) {
8441 wpa_printf(MSG_DEBUG, "P2P: Interface %s in group "
8442 "formation found - cancelling",
8443 wpa_s->ifname);
8444 found = 1;
8445 eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
Dmitry Shmidt9c175262016-03-03 10:20:07 -08008446 wpa_s->p2pdev, NULL);
Jouni Malinenadddfc42012-10-03 14:31:41 -07008447 if (wpa_s->p2p_in_provisioning) {
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07008448 wpas_group_formation_completed(wpa_s, 0, 0);
Jouni Malinenadddfc42012-10-03 14:31:41 -07008449 break;
8450 }
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07008451 wpas_p2p_group_delete(wpa_s,
8452 P2P_GROUP_REMOVAL_REQUESTED);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008453 break;
Dmitry Shmidt21de2142014-04-08 10:50:52 -07008454 } else if (wpa_s->p2p_in_invitation) {
8455 wpa_printf(MSG_DEBUG, "P2P: Interface %s in invitation found - cancelling",
8456 wpa_s->ifname);
8457 found = 1;
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07008458 wpas_p2p_group_formation_failed(wpa_s, 0);
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08008459 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008460 }
8461 }
8462
8463 if (!found) {
8464 wpa_printf(MSG_DEBUG, "P2P: No ongoing group formation found");
8465 return -1;
8466 }
8467
8468 return 0;
8469}
8470
8471
8472void wpas_p2p_interface_unavailable(struct wpa_supplicant *wpa_s)
8473{
8474 if (wpa_s->current_ssid == NULL || !wpa_s->current_ssid->p2p_group)
8475 return;
8476
8477 wpa_printf(MSG_DEBUG, "P2P: Remove group due to driver resource not "
8478 "being available anymore");
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07008479 wpas_p2p_group_delete(wpa_s, P2P_GROUP_REMOVAL_UNAVAILABLE);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008480}
8481
8482
8483void wpas_p2p_update_best_channels(struct wpa_supplicant *wpa_s,
8484 int freq_24, int freq_5, int freq_overall)
8485{
8486 struct p2p_data *p2p = wpa_s->global->p2p;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08008487 if (p2p == NULL)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008488 return;
8489 p2p_set_best_channels(p2p, freq_24, freq_5, freq_overall);
8490}
8491
8492
8493int wpas_p2p_unauthorize(struct wpa_supplicant *wpa_s, const char *addr)
8494{
8495 u8 peer[ETH_ALEN];
8496 struct p2p_data *p2p = wpa_s->global->p2p;
8497
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08008498 if (p2p == NULL)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008499 return -1;
8500
8501 if (hwaddr_aton(addr, peer))
8502 return -1;
8503
8504 return p2p_unauthorize(p2p, peer);
8505}
8506
8507
8508/**
8509 * wpas_p2p_disconnect - Disconnect from a P2P Group
8510 * @wpa_s: Pointer to wpa_supplicant data
8511 * Returns: 0 on success, -1 on failure
8512 *
8513 * This can be used to disconnect from a group in which the local end is a P2P
8514 * Client or to end a P2P Group in case the local end is the Group Owner. If a
8515 * virtual network interface was created for this group, that interface will be
8516 * removed. Otherwise, only the configured P2P group network will be removed
8517 * from the interface.
8518 */
8519int wpas_p2p_disconnect(struct wpa_supplicant *wpa_s)
8520{
8521
8522 if (wpa_s == NULL)
8523 return -1;
8524
Jouni Malinen2b89da82012-08-31 22:04:41 +03008525 return wpas_p2p_group_delete(wpa_s, P2P_GROUP_REMOVAL_REQUESTED) < 0 ?
8526 -1 : 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008527}
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008528
8529
8530int wpas_p2p_in_progress(struct wpa_supplicant *wpa_s)
8531{
Dmitry Shmidtf8623282013-02-20 14:34:59 -08008532 int ret;
8533
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008534 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
8535 return 0;
8536
Dmitry Shmidtf8623282013-02-20 14:34:59 -08008537 ret = p2p_in_progress(wpa_s->global->p2p);
8538 if (ret == 0) {
8539 /*
8540 * Check whether there is an ongoing WPS provisioning step (or
8541 * other parts of group formation) on another interface since
8542 * p2p_in_progress() does not report this to avoid issues for
8543 * scans during such provisioning step.
8544 */
8545 if (wpa_s->global->p2p_group_formation &&
8546 wpa_s->global->p2p_group_formation != wpa_s) {
8547 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Another interface (%s) "
8548 "in group formation",
8549 wpa_s->global->p2p_group_formation->ifname);
8550 ret = 1;
8551 }
8552 }
8553
Dmitry Shmidt92c368d2013-08-29 12:37:21 -07008554 if (!ret && wpa_s->global->p2p_go_wait_client.sec) {
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08008555 struct os_reltime now;
8556 os_get_reltime(&now);
8557 if (os_reltime_expired(&now, &wpa_s->global->p2p_go_wait_client,
8558 P2P_MAX_INITIAL_CONN_WAIT_GO)) {
Dmitry Shmidt92c368d2013-08-29 12:37:21 -07008559 /* Wait for the first client has expired */
8560 wpa_s->global->p2p_go_wait_client.sec = 0;
8561 } else {
8562 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Waiting for initial client connection during group formation");
8563 ret = 1;
8564 }
8565 }
8566
Dmitry Shmidtf8623282013-02-20 14:34:59 -08008567 return ret;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008568}
8569
8570
8571void wpas_p2p_network_removed(struct wpa_supplicant *wpa_s,
8572 struct wpa_ssid *ssid)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008573{
8574 if (wpa_s->p2p_in_provisioning && ssid->p2p_group &&
8575 eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
Dmitry Shmidt9c175262016-03-03 10:20:07 -08008576 wpa_s->p2pdev, NULL) > 0) {
Dmitry Shmidt04949592012-07-19 12:16:46 -07008577 /**
8578 * Remove the network by scheduling the group formation
8579 * timeout to happen immediately. The teardown code
8580 * needs to be scheduled to run asynch later so that we
8581 * don't delete data from under ourselves unexpectedly.
8582 * Calling wpas_p2p_group_formation_timeout directly
8583 * causes a series of crashes in WPS failure scenarios.
8584 */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008585 wpa_printf(MSG_DEBUG, "P2P: Canceled group formation due to "
8586 "P2P group network getting removed");
Dmitry Shmidt04949592012-07-19 12:16:46 -07008587 eloop_register_timeout(0, 0, wpas_p2p_group_formation_timeout,
Dmitry Shmidt9c175262016-03-03 10:20:07 -08008588 wpa_s->p2pdev, NULL);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008589 }
8590}
8591
8592
8593struct wpa_ssid * wpas_p2p_get_persistent(struct wpa_supplicant *wpa_s,
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08008594 const u8 *addr, const u8 *ssid,
8595 size_t ssid_len)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008596{
8597 struct wpa_ssid *s;
8598 size_t i;
8599
8600 for (s = wpa_s->conf->ssid; s; s = s->next) {
8601 if (s->disabled != 2)
8602 continue;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08008603 if (ssid &&
8604 (ssid_len != s->ssid_len ||
8605 os_memcmp(ssid, s->ssid, ssid_len) != 0))
8606 continue;
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07008607 if (addr == NULL) {
8608 if (s->mode == WPAS_MODE_P2P_GO)
8609 return s;
8610 continue;
8611 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008612 if (os_memcmp(s->bssid, addr, ETH_ALEN) == 0)
8613 return s; /* peer is GO in the persistent group */
8614 if (s->mode != WPAS_MODE_P2P_GO || s->p2p_client_list == NULL)
8615 continue;
8616 for (i = 0; i < s->num_p2p_clients; i++) {
Dmitry Shmidtff787d52015-01-12 13:01:47 -08008617 if (os_memcmp(s->p2p_client_list + i * 2 * ETH_ALEN,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008618 addr, ETH_ALEN) == 0)
8619 return s; /* peer is P2P client in persistent
8620 * group */
8621 }
8622 }
8623
8624 return NULL;
8625}
8626
8627
8628void wpas_p2p_notify_ap_sta_authorized(struct wpa_supplicant *wpa_s,
8629 const u8 *addr)
8630{
Dmitry Shmidt56052862013-10-04 10:23:25 -07008631 if (eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
Dmitry Shmidt9c175262016-03-03 10:20:07 -08008632 wpa_s->p2pdev, NULL) > 0) {
Dmitry Shmidt56052862013-10-04 10:23:25 -07008633 /*
8634 * This can happen if WPS provisioning step is not terminated
8635 * cleanly (e.g., P2P Client does not send WSC_Done). Since the
8636 * peer was able to connect, there is no need to time out group
8637 * formation after this, though. In addition, this is used with
8638 * the initial connection wait on the GO as a separate formation
8639 * timeout and as such, expected to be hit after the initial WPS
8640 * provisioning step.
8641 */
8642 wpa_printf(MSG_DEBUG, "P2P: Canceled P2P group formation timeout on data connection");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08008643
8644 if (!wpa_s->p2p_go_group_formation_completed &&
8645 !wpa_s->group_formation_reported) {
8646 /*
8647 * GO has not yet notified group formation success since
8648 * the WPS step was not completed cleanly. Do that
8649 * notification now since the P2P Client was able to
8650 * connect and as such, must have received the
8651 * credential from the WPS step.
8652 */
8653 if (wpa_s->global->p2p)
8654 p2p_wps_success_cb(wpa_s->global->p2p, addr);
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07008655 wpas_group_formation_completed(wpa_s, 1, 0);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08008656 }
Dmitry Shmidt56052862013-10-04 10:23:25 -07008657 }
8658 if (!wpa_s->p2p_go_group_formation_completed) {
8659 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Marking group formation completed on GO on first data connection");
8660 wpa_s->p2p_go_group_formation_completed = 1;
8661 wpa_s->global->p2p_group_formation = NULL;
8662 wpa_s->p2p_in_provisioning = 0;
Dmitry Shmidt21de2142014-04-08 10:50:52 -07008663 wpa_s->p2p_in_invitation = 0;
Dmitry Shmidt56052862013-10-04 10:23:25 -07008664 }
Dmitry Shmidt92c368d2013-08-29 12:37:21 -07008665 wpa_s->global->p2p_go_wait_client.sec = 0;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008666 if (addr == NULL)
8667 return;
8668 wpas_p2p_add_persistent_group_client(wpa_s, addr);
8669}
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08008670
Dmitry Shmidt04949592012-07-19 12:16:46 -07008671
Dmitry Shmidt7f656022015-02-25 14:36:37 -08008672static int wpas_p2p_fallback_to_go_neg(struct wpa_supplicant *wpa_s,
8673 int group_added)
Dmitry Shmidt04949592012-07-19 12:16:46 -07008674{
8675 struct wpa_supplicant *group = wpa_s;
Dmitry Shmidt7f656022015-02-25 14:36:37 -08008676 int ret = 0;
8677
Dmitry Shmidt04949592012-07-19 12:16:46 -07008678 if (wpa_s->global->p2p_group_formation)
8679 group = wpa_s->global->p2p_group_formation;
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07008680 wpa_s = wpa_s->global->p2p_init_wpa_s;
Dmitry Shmidt04949592012-07-19 12:16:46 -07008681 offchannel_send_action_done(wpa_s);
8682 if (group_added)
Dmitry Shmidt7f656022015-02-25 14:36:37 -08008683 ret = wpas_p2p_group_delete(group, P2P_GROUP_REMOVAL_SILENT);
Dmitry Shmidt04949592012-07-19 12:16:46 -07008684 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Fall back to GO Negotiation");
8685 wpas_p2p_connect(wpa_s, wpa_s->pending_join_dev_addr, wpa_s->p2p_pin,
8686 wpa_s->p2p_wps_method, wpa_s->p2p_persistent_group, 0,
8687 0, 0, wpa_s->p2p_go_intent, wpa_s->p2p_connect_freq,
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08008688 wpa_s->p2p_go_vht_center_freq2,
Dmitry Shmidt04949592012-07-19 12:16:46 -07008689 wpa_s->p2p_persistent_id,
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07008690 wpa_s->p2p_pd_before_go_neg,
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07008691 wpa_s->p2p_go_ht40,
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08008692 wpa_s->p2p_go_vht,
Hai Shalom74f70d42019-02-11 14:42:39 -08008693 wpa_s->p2p_go_max_oper_chwidth,
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08008694 wpa_s->p2p_go_he,
8695 wpa_s->p2p_go_edmg,
Hai Shalomc1a21442022-02-04 13:43:00 -08008696 NULL, 0, is_p2p_allow_6ghz(wpa_s->global->p2p));
Dmitry Shmidt7f656022015-02-25 14:36:37 -08008697 return ret;
Dmitry Shmidt04949592012-07-19 12:16:46 -07008698}
8699
8700
8701int wpas_p2p_scan_no_go_seen(struct wpa_supplicant *wpa_s)
8702{
Dmitry Shmidt7f656022015-02-25 14:36:37 -08008703 int res;
8704
Dmitry Shmidt04949592012-07-19 12:16:46 -07008705 if (!wpa_s->p2p_fallback_to_go_neg ||
8706 wpa_s->p2p_in_provisioning <= 5)
8707 return 0;
8708
8709 if (wpas_p2p_peer_go(wpa_s, wpa_s->pending_join_dev_addr) > 0)
8710 return 0; /* peer operating as a GO */
8711
8712 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: GO not found for p2p_connect-auto - "
8713 "fallback to GO Negotiation");
Dmitry Shmidt9c175262016-03-03 10:20:07 -08008714 wpa_msg_global(wpa_s->p2pdev, MSG_INFO, P2P_EVENT_FALLBACK_TO_GO_NEG
Dmitry Shmidt7f656022015-02-25 14:36:37 -08008715 "reason=GO-not-found");
8716 res = wpas_p2p_fallback_to_go_neg(wpa_s, 1);
Dmitry Shmidt04949592012-07-19 12:16:46 -07008717
Dmitry Shmidt7f656022015-02-25 14:36:37 -08008718 return res == 1 ? 2 : 1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07008719}
8720
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07008721
8722unsigned int wpas_p2p_search_delay(struct wpa_supplicant *wpa_s)
8723{
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07008724 struct wpa_supplicant *ifs;
8725
8726 if (wpa_s->wpa_state > WPA_SCANNING) {
8727 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Use %u ms search delay due to "
8728 "concurrent operation",
Dmitry Shmidt09f57ba2014-06-10 16:07:13 -07008729 wpa_s->conf->p2p_search_delay);
8730 return wpa_s->conf->p2p_search_delay;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07008731 }
8732
Dmitry Shmidt01904cf2013-12-05 11:08:35 -08008733 dl_list_for_each(ifs, &wpa_s->radio->ifaces, struct wpa_supplicant,
8734 radio_list) {
8735 if (ifs != wpa_s && ifs->wpa_state > WPA_SCANNING) {
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07008736 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Use %u ms search "
8737 "delay due to concurrent operation on "
8738 "interface %s",
Dmitry Shmidt09f57ba2014-06-10 16:07:13 -07008739 wpa_s->conf->p2p_search_delay,
8740 ifs->ifname);
8741 return wpa_s->conf->p2p_search_delay;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07008742 }
8743 }
8744
8745 return 0;
8746}
8747
Dmitry Shmidt37d4d6a2013-03-18 13:09:42 -07008748
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07008749static int wpas_p2p_remove_psk_entry(struct wpa_supplicant *wpa_s,
8750 struct wpa_ssid *s, const u8 *addr,
8751 int iface_addr)
8752{
8753 struct psk_list_entry *psk, *tmp;
8754 int changed = 0;
8755
8756 dl_list_for_each_safe(psk, tmp, &s->psk_list, struct psk_list_entry,
8757 list) {
8758 if ((iface_addr && !psk->p2p &&
8759 os_memcmp(addr, psk->addr, ETH_ALEN) == 0) ||
8760 (!iface_addr && psk->p2p &&
8761 os_memcmp(addr, psk->addr, ETH_ALEN) == 0)) {
8762 wpa_dbg(wpa_s, MSG_DEBUG,
8763 "P2P: Remove persistent group PSK list entry for "
8764 MACSTR " p2p=%u",
8765 MAC2STR(psk->addr), psk->p2p);
8766 dl_list_del(&psk->list);
8767 os_free(psk);
8768 changed++;
8769 }
8770 }
8771
8772 return changed;
8773}
8774
8775
8776void wpas_p2p_new_psk_cb(struct wpa_supplicant *wpa_s, const u8 *mac_addr,
8777 const u8 *p2p_dev_addr,
8778 const u8 *psk, size_t psk_len)
8779{
8780 struct wpa_ssid *ssid = wpa_s->current_ssid;
8781 struct wpa_ssid *persistent;
Dmitry Shmidt7832adb2014-04-29 10:53:02 -07008782 struct psk_list_entry *p, *last;
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07008783
8784 if (psk_len != sizeof(p->psk))
8785 return;
8786
8787 if (p2p_dev_addr) {
8788 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: New PSK for addr=" MACSTR
8789 " p2p_dev_addr=" MACSTR,
8790 MAC2STR(mac_addr), MAC2STR(p2p_dev_addr));
8791 if (is_zero_ether_addr(p2p_dev_addr))
8792 p2p_dev_addr = NULL;
8793 } else {
8794 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: New PSK for addr=" MACSTR,
8795 MAC2STR(mac_addr));
8796 }
8797
8798 if (ssid->mode == WPAS_MODE_P2P_GROUP_FORMATION) {
8799 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: new_psk_cb during group formation");
8800 /* To be added to persistent group once created */
8801 if (wpa_s->global->add_psk == NULL) {
8802 wpa_s->global->add_psk = os_zalloc(sizeof(*p));
8803 if (wpa_s->global->add_psk == NULL)
8804 return;
8805 }
8806 p = wpa_s->global->add_psk;
8807 if (p2p_dev_addr) {
8808 p->p2p = 1;
8809 os_memcpy(p->addr, p2p_dev_addr, ETH_ALEN);
8810 } else {
8811 p->p2p = 0;
8812 os_memcpy(p->addr, mac_addr, ETH_ALEN);
8813 }
8814 os_memcpy(p->psk, psk, psk_len);
8815 return;
8816 }
8817
8818 if (ssid->mode != WPAS_MODE_P2P_GO || !ssid->p2p_persistent_group) {
8819 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Ignore new_psk_cb on not-persistent GO");
8820 return;
8821 }
8822
Dmitry Shmidt9c175262016-03-03 10:20:07 -08008823 persistent = wpas_p2p_get_persistent(wpa_s->p2pdev, NULL, ssid->ssid,
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07008824 ssid->ssid_len);
8825 if (!persistent) {
8826 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Could not find persistent group information to store the new PSK");
8827 return;
8828 }
8829
8830 p = os_zalloc(sizeof(*p));
8831 if (p == NULL)
8832 return;
8833 if (p2p_dev_addr) {
8834 p->p2p = 1;
8835 os_memcpy(p->addr, p2p_dev_addr, ETH_ALEN);
8836 } else {
8837 p->p2p = 0;
8838 os_memcpy(p->addr, mac_addr, ETH_ALEN);
8839 }
8840 os_memcpy(p->psk, psk, psk_len);
8841
Dmitry Shmidt7832adb2014-04-29 10:53:02 -07008842 if (dl_list_len(&persistent->psk_list) > P2P_MAX_STORED_CLIENTS &&
8843 (last = dl_list_last(&persistent->psk_list,
8844 struct psk_list_entry, list))) {
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07008845 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Remove oldest PSK entry for "
8846 MACSTR " (p2p=%u) to make room for a new one",
8847 MAC2STR(last->addr), last->p2p);
8848 dl_list_del(&last->list);
8849 os_free(last);
8850 }
8851
Dmitry Shmidt9c175262016-03-03 10:20:07 -08008852 wpas_p2p_remove_psk_entry(wpa_s->p2pdev, persistent,
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07008853 p2p_dev_addr ? p2p_dev_addr : mac_addr,
8854 p2p_dev_addr == NULL);
8855 if (p2p_dev_addr) {
8856 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Add new PSK for p2p_dev_addr="
8857 MACSTR, MAC2STR(p2p_dev_addr));
8858 } else {
8859 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Add new PSK for addr=" MACSTR,
8860 MAC2STR(mac_addr));
8861 }
8862 dl_list_add(&persistent->psk_list, &p->list);
8863
Dmitry Shmidt9c175262016-03-03 10:20:07 -08008864 if (wpa_s->p2pdev->conf->update_config &&
8865 wpa_config_write(wpa_s->p2pdev->confname, wpa_s->p2pdev->conf))
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07008866 wpa_printf(MSG_DEBUG, "P2P: Failed to update configuration");
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07008867}
8868
8869
8870static void wpas_p2p_remove_psk(struct wpa_supplicant *wpa_s,
8871 struct wpa_ssid *s, const u8 *addr,
8872 int iface_addr)
8873{
8874 int res;
8875
8876 res = wpas_p2p_remove_psk_entry(wpa_s, s, addr, iface_addr);
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08008877 if (res > 0 && wpa_s->conf->update_config &&
8878 wpa_config_write(wpa_s->confname, wpa_s->conf))
8879 wpa_dbg(wpa_s, MSG_DEBUG,
8880 "P2P: Failed to update configuration");
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07008881}
8882
8883
8884static void wpas_p2p_remove_client_go(struct wpa_supplicant *wpa_s,
8885 const u8 *peer, int iface_addr)
8886{
8887 struct hostapd_data *hapd;
8888 struct hostapd_wpa_psk *psk, *prev, *rem;
8889 struct sta_info *sta;
8890
8891 if (wpa_s->ap_iface == NULL || wpa_s->current_ssid == NULL ||
8892 wpa_s->current_ssid->mode != WPAS_MODE_P2P_GO)
8893 return;
8894
8895 /* Remove per-station PSK entry */
8896 hapd = wpa_s->ap_iface->bss[0];
8897 prev = NULL;
8898 psk = hapd->conf->ssid.wpa_psk;
8899 while (psk) {
8900 if ((iface_addr && os_memcmp(peer, psk->addr, ETH_ALEN) == 0) ||
8901 (!iface_addr &&
8902 os_memcmp(peer, psk->p2p_dev_addr, ETH_ALEN) == 0)) {
8903 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Remove operating group PSK entry for "
8904 MACSTR " iface_addr=%d",
8905 MAC2STR(peer), iface_addr);
8906 if (prev)
8907 prev->next = psk->next;
8908 else
8909 hapd->conf->ssid.wpa_psk = psk->next;
8910 rem = psk;
8911 psk = psk->next;
Sunil Ravia04bd252022-05-02 22:54:18 -07008912 bin_clear_free(rem, sizeof(*rem));
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07008913 } else {
8914 prev = psk;
8915 psk = psk->next;
8916 }
8917 }
8918
8919 /* Disconnect from group */
8920 if (iface_addr)
8921 sta = ap_get_sta(hapd, peer);
8922 else
8923 sta = ap_get_sta_p2p(hapd, peer);
8924 if (sta) {
8925 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Disconnect peer " MACSTR
8926 " (iface_addr=%d) from group",
8927 MAC2STR(peer), iface_addr);
8928 hostapd_drv_sta_deauth(hapd, sta->addr,
8929 WLAN_REASON_DEAUTH_LEAVING);
8930 ap_sta_deauthenticate(hapd, sta, WLAN_REASON_DEAUTH_LEAVING);
8931 }
8932}
8933
8934
8935void wpas_p2p_remove_client(struct wpa_supplicant *wpa_s, const u8 *peer,
8936 int iface_addr)
8937{
8938 struct wpa_ssid *s;
8939 struct wpa_supplicant *w;
Dmitry Shmidt8bd70b72015-05-26 16:02:19 -07008940 struct wpa_supplicant *p2p_wpa_s = wpa_s->global->p2p_init_wpa_s;
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07008941
8942 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Remove client " MACSTR, MAC2STR(peer));
8943
8944 /* Remove from any persistent group */
Dmitry Shmidt8bd70b72015-05-26 16:02:19 -07008945 for (s = p2p_wpa_s->conf->ssid; s; s = s->next) {
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07008946 if (s->disabled != 2 || s->mode != WPAS_MODE_P2P_GO)
8947 continue;
8948 if (!iface_addr)
Dmitry Shmidt8bd70b72015-05-26 16:02:19 -07008949 wpas_remove_persistent_peer(p2p_wpa_s, s, peer, 0);
8950 wpas_p2p_remove_psk(p2p_wpa_s, s, peer, iface_addr);
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07008951 }
8952
8953 /* Remove from any operating group */
8954 for (w = wpa_s->global->ifaces; w; w = w->next)
8955 wpas_p2p_remove_client_go(w, peer, iface_addr);
8956}
8957
8958
8959static void wpas_p2p_psk_failure_removal(void *eloop_ctx, void *timeout_ctx)
8960{
8961 struct wpa_supplicant *wpa_s = eloop_ctx;
8962 wpas_p2p_group_delete(wpa_s, P2P_GROUP_REMOVAL_PSK_FAILURE);
8963}
8964
8965
Dmitry Shmidt04f534e2013-12-09 15:50:16 -08008966static void wpas_p2p_group_freq_conflict(void *eloop_ctx, void *timeout_ctx)
8967{
8968 struct wpa_supplicant *wpa_s = eloop_ctx;
8969
8970 wpa_printf(MSG_DEBUG, "P2P: Frequency conflict - terminate group");
8971 wpas_p2p_group_delete(wpa_s, P2P_GROUP_REMOVAL_FREQ_CONFLICT);
8972}
8973
8974
8975int wpas_p2p_handle_frequency_conflicts(struct wpa_supplicant *wpa_s, int freq,
8976 struct wpa_ssid *ssid)
8977{
8978 struct wpa_supplicant *iface;
8979
8980 for (iface = wpa_s->global->ifaces; iface; iface = iface->next) {
8981 if (!iface->current_ssid ||
8982 iface->current_ssid->frequency == freq ||
8983 (iface->p2p_group_interface == NOT_P2P_GROUP_INTERFACE &&
8984 !iface->current_ssid->p2p_group))
8985 continue;
8986
8987 /* Remove the connection with least priority */
8988 if (!wpas_is_p2p_prioritized(iface)) {
8989 /* STA connection has priority over existing
8990 * P2P connection, so remove the interface. */
8991 wpa_printf(MSG_DEBUG, "P2P: Removing P2P connection due to single channel concurrent mode frequency conflict");
8992 eloop_register_timeout(0, 0,
8993 wpas_p2p_group_freq_conflict,
8994 iface, NULL);
8995 /* If connection in progress is P2P connection, do not
8996 * proceed for the connection. */
8997 if (wpa_s == iface)
8998 return -1;
8999 else
9000 return 0;
9001 } else {
9002 /* P2P connection has priority, disable the STA network
9003 */
9004 wpa_supplicant_disable_network(wpa_s->global->ifaces,
9005 ssid);
9006 wpa_msg(wpa_s->global->ifaces, MSG_INFO,
9007 WPA_EVENT_FREQ_CONFLICT " id=%d", ssid->id);
9008 os_memset(wpa_s->global->ifaces->pending_bssid, 0,
9009 ETH_ALEN);
9010 /* If P2P connection is in progress, continue
9011 * connecting...*/
9012 if (wpa_s == iface)
9013 return 0;
9014 else
9015 return -1;
9016 }
9017 }
9018
9019 return 0;
9020}
9021
9022
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07009023int wpas_p2p_4way_hs_failed(struct wpa_supplicant *wpa_s)
9024{
9025 struct wpa_ssid *ssid = wpa_s->current_ssid;
9026
9027 if (ssid == NULL || !ssid->p2p_group)
9028 return 0;
9029
9030 if (wpa_s->p2p_last_4way_hs_fail &&
9031 wpa_s->p2p_last_4way_hs_fail == ssid) {
9032 u8 go_dev_addr[ETH_ALEN];
9033 struct wpa_ssid *persistent;
9034
9035 if (wpas_p2p_persistent_group(wpa_s, go_dev_addr,
9036 ssid->ssid,
9037 ssid->ssid_len) <= 0) {
9038 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Could not determine whether 4-way handshake failures were for a persistent group");
9039 goto disconnect;
9040 }
9041
9042 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Two 4-way handshake failures for a P2P group - go_dev_addr="
9043 MACSTR, MAC2STR(go_dev_addr));
Dmitry Shmidt9c175262016-03-03 10:20:07 -08009044 persistent = wpas_p2p_get_persistent(wpa_s->p2pdev, go_dev_addr,
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07009045 ssid->ssid,
9046 ssid->ssid_len);
9047 if (persistent == NULL || persistent->mode != WPAS_MODE_INFRA) {
9048 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: No matching persistent group stored");
9049 goto disconnect;
9050 }
Dmitry Shmidt9c175262016-03-03 10:20:07 -08009051 wpa_msg_global(wpa_s->p2pdev, MSG_INFO,
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07009052 P2P_EVENT_PERSISTENT_PSK_FAIL "%d",
9053 persistent->id);
9054 disconnect:
9055 wpa_s->p2p_last_4way_hs_fail = NULL;
9056 /*
9057 * Remove the group from a timeout to avoid issues with caller
9058 * continuing to use the interface if this is on a P2P group
9059 * interface.
9060 */
9061 eloop_register_timeout(0, 0, wpas_p2p_psk_failure_removal,
9062 wpa_s, NULL);
9063 return 1;
9064 }
9065
9066 wpa_s->p2p_last_4way_hs_fail = ssid;
9067 return 0;
9068}
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08009069
9070
9071#ifdef CONFIG_WPS_NFC
9072
9073static struct wpabuf * wpas_p2p_nfc_handover(int ndef, struct wpabuf *wsc,
9074 struct wpabuf *p2p)
9075{
9076 struct wpabuf *ret;
9077 size_t wsc_len;
9078
9079 if (p2p == NULL) {
9080 wpabuf_free(wsc);
9081 wpa_printf(MSG_DEBUG, "P2P: No p2p buffer for handover");
9082 return NULL;
9083 }
9084
9085 wsc_len = wsc ? wpabuf_len(wsc) : 0;
9086 ret = wpabuf_alloc(2 + wsc_len + 2 + wpabuf_len(p2p));
9087 if (ret == NULL) {
9088 wpabuf_free(wsc);
9089 wpabuf_free(p2p);
9090 return NULL;
9091 }
9092
9093 wpabuf_put_be16(ret, wsc_len);
9094 if (wsc)
9095 wpabuf_put_buf(ret, wsc);
9096 wpabuf_put_be16(ret, wpabuf_len(p2p));
9097 wpabuf_put_buf(ret, p2p);
9098
9099 wpabuf_free(wsc);
9100 wpabuf_free(p2p);
9101 wpa_hexdump_buf(MSG_DEBUG,
9102 "P2P: Generated NFC connection handover message", ret);
9103
9104 if (ndef && ret) {
9105 struct wpabuf *tmp;
9106 tmp = ndef_build_p2p(ret);
9107 wpabuf_free(ret);
9108 if (tmp == NULL) {
9109 wpa_printf(MSG_DEBUG, "P2P: Failed to NDEF encapsulate handover request");
9110 return NULL;
9111 }
9112 ret = tmp;
9113 }
9114
9115 return ret;
9116}
9117
9118
9119static int wpas_p2p_cli_freq(struct wpa_supplicant *wpa_s,
9120 struct wpa_ssid **ssid, u8 *go_dev_addr)
9121{
9122 struct wpa_supplicant *iface;
9123
9124 if (go_dev_addr)
9125 os_memset(go_dev_addr, 0, ETH_ALEN);
9126 if (ssid)
9127 *ssid = NULL;
9128 for (iface = wpa_s->global->ifaces; iface; iface = iface->next) {
9129 if (iface->wpa_state < WPA_ASSOCIATING ||
9130 iface->current_ssid == NULL || iface->assoc_freq == 0 ||
9131 !iface->current_ssid->p2p_group ||
9132 iface->current_ssid->mode != WPAS_MODE_INFRA)
9133 continue;
9134 if (ssid)
9135 *ssid = iface->current_ssid;
9136 if (go_dev_addr)
9137 os_memcpy(go_dev_addr, iface->go_dev_addr, ETH_ALEN);
9138 return iface->assoc_freq;
9139 }
9140 return 0;
9141}
9142
9143
9144struct wpabuf * wpas_p2p_nfc_handover_req(struct wpa_supplicant *wpa_s,
9145 int ndef)
9146{
9147 struct wpabuf *wsc, *p2p;
9148 struct wpa_ssid *ssid;
9149 u8 go_dev_addr[ETH_ALEN];
9150 int cli_freq = wpas_p2p_cli_freq(wpa_s, &ssid, go_dev_addr);
9151
9152 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL) {
9153 wpa_printf(MSG_DEBUG, "P2P: P2P disabled - cannot build handover request");
9154 return NULL;
9155 }
9156
9157 if (wpa_s->conf->wps_nfc_dh_pubkey == NULL &&
9158 wps_nfc_gen_dh(&wpa_s->conf->wps_nfc_dh_pubkey,
9159 &wpa_s->conf->wps_nfc_dh_privkey) < 0) {
9160 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: No DH key available for handover request");
9161 return NULL;
9162 }
9163
9164 if (cli_freq == 0) {
9165 wsc = wps_build_nfc_handover_req_p2p(
9166 wpa_s->parent->wps, wpa_s->conf->wps_nfc_dh_pubkey);
9167 } else
9168 wsc = NULL;
9169 p2p = p2p_build_nfc_handover_req(wpa_s->global->p2p, cli_freq,
9170 go_dev_addr, ssid ? ssid->ssid : NULL,
9171 ssid ? ssid->ssid_len : 0);
9172
9173 return wpas_p2p_nfc_handover(ndef, wsc, p2p);
9174}
9175
9176
9177struct wpabuf * wpas_p2p_nfc_handover_sel(struct wpa_supplicant *wpa_s,
9178 int ndef, int tag)
9179{
9180 struct wpabuf *wsc, *p2p;
9181 struct wpa_ssid *ssid;
9182 u8 go_dev_addr[ETH_ALEN];
9183 int cli_freq = wpas_p2p_cli_freq(wpa_s, &ssid, go_dev_addr);
9184
9185 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
9186 return NULL;
9187
9188 if (!tag && wpa_s->conf->wps_nfc_dh_pubkey == NULL &&
9189 wps_nfc_gen_dh(&wpa_s->conf->wps_nfc_dh_pubkey,
9190 &wpa_s->conf->wps_nfc_dh_privkey) < 0)
9191 return NULL;
9192
9193 if (cli_freq == 0) {
9194 wsc = wps_build_nfc_handover_sel_p2p(
9195 wpa_s->parent->wps,
9196 tag ? wpa_s->conf->wps_nfc_dev_pw_id :
9197 DEV_PW_NFC_CONNECTION_HANDOVER,
9198 wpa_s->conf->wps_nfc_dh_pubkey,
9199 tag ? wpa_s->conf->wps_nfc_dev_pw : NULL);
9200 } else
9201 wsc = NULL;
9202 p2p = p2p_build_nfc_handover_sel(wpa_s->global->p2p, cli_freq,
9203 go_dev_addr, ssid ? ssid->ssid : NULL,
9204 ssid ? ssid->ssid_len : 0);
9205
9206 return wpas_p2p_nfc_handover(ndef, wsc, p2p);
9207}
9208
9209
9210static int wpas_p2p_nfc_join_group(struct wpa_supplicant *wpa_s,
9211 struct p2p_nfc_params *params)
9212{
9213 wpa_printf(MSG_DEBUG, "P2P: Initiate join-group based on NFC "
9214 "connection handover (freq=%d)",
9215 params->go_freq);
9216
9217 if (params->go_freq && params->go_ssid_len) {
9218 wpa_s->p2p_wps_method = WPS_NFC;
9219 wpa_s->pending_join_wps_method = WPS_NFC;
9220 os_memset(wpa_s->pending_join_iface_addr, 0, ETH_ALEN);
9221 os_memcpy(wpa_s->pending_join_dev_addr, params->go_dev_addr,
9222 ETH_ALEN);
9223 return wpas_p2p_join_start(wpa_s, params->go_freq,
9224 params->go_ssid,
9225 params->go_ssid_len);
9226 }
9227
9228 return wpas_p2p_connect(wpa_s, params->peer->p2p_device_addr, NULL,
9229 WPS_NFC, 0, 0, 1, 0, wpa_s->conf->p2p_go_intent,
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08009230 params->go_freq, wpa_s->p2p_go_vht_center_freq2,
Dmitry Shmidtde47be72016-01-07 12:52:55 -08009231 -1, 0, 1, 1, wpa_s->p2p_go_max_oper_chwidth,
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08009232 wpa_s->p2p_go_he, wpa_s->p2p_go_edmg,
Dmitry Shmidtde47be72016-01-07 12:52:55 -08009233 params->go_ssid_len ? params->go_ssid : NULL,
Hai Shalomc1a21442022-02-04 13:43:00 -08009234 params->go_ssid_len, false);
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08009235}
9236
9237
9238static int wpas_p2p_nfc_auth_join(struct wpa_supplicant *wpa_s,
9239 struct p2p_nfc_params *params, int tag)
9240{
9241 int res, persistent;
9242 struct wpa_ssid *ssid;
9243
9244 wpa_printf(MSG_DEBUG, "P2P: Authorize join-group based on NFC "
9245 "connection handover");
9246 for (wpa_s = wpa_s->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
9247 ssid = wpa_s->current_ssid;
9248 if (ssid == NULL)
9249 continue;
9250 if (ssid->mode != WPAS_MODE_P2P_GO)
9251 continue;
9252 if (wpa_s->ap_iface == NULL)
9253 continue;
9254 break;
9255 }
9256 if (wpa_s == NULL) {
9257 wpa_printf(MSG_DEBUG, "P2P: Could not find GO interface");
9258 return -1;
9259 }
9260
Dmitry Shmidt849734c2016-05-27 09:59:01 -07009261 if (wpa_s->p2pdev->p2p_oob_dev_pw_id !=
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08009262 DEV_PW_NFC_CONNECTION_HANDOVER &&
Dmitry Shmidt849734c2016-05-27 09:59:01 -07009263 !wpa_s->p2pdev->p2p_oob_dev_pw) {
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08009264 wpa_printf(MSG_DEBUG, "P2P: No NFC Dev Pw known");
9265 return -1;
9266 }
9267 res = wpas_ap_wps_add_nfc_pw(
Dmitry Shmidt849734c2016-05-27 09:59:01 -07009268 wpa_s, wpa_s->p2pdev->p2p_oob_dev_pw_id,
9269 wpa_s->p2pdev->p2p_oob_dev_pw,
9270 wpa_s->p2pdev->p2p_peer_oob_pk_hash_known ?
9271 wpa_s->p2pdev->p2p_peer_oob_pubkey_hash : NULL);
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08009272 if (res)
9273 return res;
9274
9275 if (!tag) {
9276 wpa_printf(MSG_DEBUG, "P2P: Negotiated handover - wait for peer to join without invitation");
9277 return 0;
9278 }
9279
9280 if (!params->peer ||
9281 !(params->peer->dev_capab & P2P_DEV_CAPAB_INVITATION_PROCEDURE))
9282 return 0;
9283
9284 wpa_printf(MSG_DEBUG, "P2P: Static handover - invite peer " MACSTR
9285 " to join", MAC2STR(params->peer->p2p_device_addr));
9286
9287 wpa_s->global->p2p_invite_group = wpa_s;
9288 persistent = ssid->p2p_persistent_group &&
Dmitry Shmidt849734c2016-05-27 09:59:01 -07009289 wpas_p2p_get_persistent(wpa_s->p2pdev,
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08009290 params->peer->p2p_device_addr,
9291 ssid->ssid, ssid->ssid_len);
Dmitry Shmidt849734c2016-05-27 09:59:01 -07009292 wpa_s->p2pdev->pending_invite_ssid_id = -1;
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08009293
9294 return p2p_invite(wpa_s->global->p2p, params->peer->p2p_device_addr,
9295 P2P_INVITE_ROLE_ACTIVE_GO, wpa_s->own_addr,
9296 ssid->ssid, ssid->ssid_len, ssid->frequency,
9297 wpa_s->global->p2p_dev_addr, persistent, 0,
Dmitry Shmidt849734c2016-05-27 09:59:01 -07009298 wpa_s->p2pdev->p2p_oob_dev_pw_id);
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08009299}
9300
9301
9302static int wpas_p2p_nfc_init_go_neg(struct wpa_supplicant *wpa_s,
9303 struct p2p_nfc_params *params,
9304 int forced_freq)
9305{
9306 wpa_printf(MSG_DEBUG, "P2P: Initiate GO Negotiation based on NFC "
9307 "connection handover");
9308 return wpas_p2p_connect(wpa_s, params->peer->p2p_device_addr, NULL,
9309 WPS_NFC, 0, 0, 0, 0, wpa_s->conf->p2p_go_intent,
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08009310 forced_freq, wpa_s->p2p_go_vht_center_freq2,
Dmitry Shmidtde47be72016-01-07 12:52:55 -08009311 -1, 0, 1, 1, wpa_s->p2p_go_max_oper_chwidth,
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08009312 wpa_s->p2p_go_he, wpa_s->p2p_go_edmg,
Hai Shalomc1a21442022-02-04 13:43:00 -08009313 NULL, 0, false);
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08009314}
9315
9316
9317static int wpas_p2p_nfc_resp_go_neg(struct wpa_supplicant *wpa_s,
9318 struct p2p_nfc_params *params,
9319 int forced_freq)
9320{
9321 int res;
9322
9323 wpa_printf(MSG_DEBUG, "P2P: Authorize GO Negotiation based on NFC "
9324 "connection handover");
9325 res = wpas_p2p_connect(wpa_s, params->peer->p2p_device_addr, NULL,
9326 WPS_NFC, 0, 0, 0, 1, wpa_s->conf->p2p_go_intent,
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08009327 forced_freq, wpa_s->p2p_go_vht_center_freq2,
Dmitry Shmidtde47be72016-01-07 12:52:55 -08009328 -1, 0, 1, 1, wpa_s->p2p_go_max_oper_chwidth,
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08009329 wpa_s->p2p_go_he, wpa_s->p2p_go_edmg,
Hai Shalomc1a21442022-02-04 13:43:00 -08009330 NULL, 0, false);
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08009331 if (res)
9332 return res;
9333
9334 res = wpas_p2p_listen(wpa_s, 60);
9335 if (res) {
9336 p2p_unauthorize(wpa_s->global->p2p,
9337 params->peer->p2p_device_addr);
9338 }
9339
9340 return res;
9341}
9342
9343
9344static int wpas_p2p_nfc_connection_handover(struct wpa_supplicant *wpa_s,
9345 const struct wpabuf *data,
9346 int sel, int tag, int forced_freq)
9347{
9348 const u8 *pos, *end;
9349 u16 len, id;
9350 struct p2p_nfc_params params;
9351 int res;
9352
9353 os_memset(&params, 0, sizeof(params));
9354 params.sel = sel;
9355
9356 wpa_hexdump_buf(MSG_DEBUG, "P2P: Received NFC tag payload", data);
9357
9358 pos = wpabuf_head(data);
9359 end = pos + wpabuf_len(data);
9360
9361 if (end - pos < 2) {
9362 wpa_printf(MSG_DEBUG, "P2P: Not enough data for Length of WSC "
9363 "attributes");
9364 return -1;
9365 }
9366 len = WPA_GET_BE16(pos);
9367 pos += 2;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08009368 if (len > end - pos) {
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08009369 wpa_printf(MSG_DEBUG, "P2P: Not enough data for WSC "
9370 "attributes");
9371 return -1;
9372 }
9373 params.wsc_attr = pos;
9374 params.wsc_len = len;
9375 pos += len;
9376
9377 if (end - pos < 2) {
9378 wpa_printf(MSG_DEBUG, "P2P: Not enough data for Length of P2P "
9379 "attributes");
9380 return -1;
9381 }
9382 len = WPA_GET_BE16(pos);
9383 pos += 2;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08009384 if (len > end - pos) {
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08009385 wpa_printf(MSG_DEBUG, "P2P: Not enough data for P2P "
9386 "attributes");
9387 return -1;
9388 }
9389 params.p2p_attr = pos;
9390 params.p2p_len = len;
9391 pos += len;
9392
9393 wpa_hexdump(MSG_DEBUG, "P2P: WSC attributes",
9394 params.wsc_attr, params.wsc_len);
9395 wpa_hexdump(MSG_DEBUG, "P2P: P2P attributes",
9396 params.p2p_attr, params.p2p_len);
9397 if (pos < end) {
9398 wpa_hexdump(MSG_DEBUG,
9399 "P2P: Ignored extra data after P2P attributes",
9400 pos, end - pos);
9401 }
9402
9403 res = p2p_process_nfc_connection_handover(wpa_s->global->p2p, &params);
9404 if (res)
9405 return res;
9406
9407 if (params.next_step == NO_ACTION)
9408 return 0;
9409
9410 if (params.next_step == BOTH_GO) {
9411 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_NFC_BOTH_GO "peer=" MACSTR,
9412 MAC2STR(params.peer->p2p_device_addr));
9413 return 0;
9414 }
9415
9416 if (params.next_step == PEER_CLIENT) {
9417 if (!is_zero_ether_addr(params.go_dev_addr)) {
9418 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_NFC_PEER_CLIENT
9419 "peer=" MACSTR " freq=%d go_dev_addr=" MACSTR
9420 " ssid=\"%s\"",
9421 MAC2STR(params.peer->p2p_device_addr),
9422 params.go_freq,
9423 MAC2STR(params.go_dev_addr),
9424 wpa_ssid_txt(params.go_ssid,
9425 params.go_ssid_len));
9426 } else {
9427 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_NFC_PEER_CLIENT
9428 "peer=" MACSTR " freq=%d",
9429 MAC2STR(params.peer->p2p_device_addr),
9430 params.go_freq);
9431 }
9432 return 0;
9433 }
9434
9435 if (wpas_p2p_cli_freq(wpa_s, NULL, NULL)) {
9436 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_NFC_WHILE_CLIENT "peer="
9437 MACSTR, MAC2STR(params.peer->p2p_device_addr));
9438 return 0;
9439 }
9440
9441 wpabuf_free(wpa_s->p2p_oob_dev_pw);
9442 wpa_s->p2p_oob_dev_pw = NULL;
9443
9444 if (params.oob_dev_pw_len < WPS_OOB_PUBKEY_HASH_LEN + 2) {
9445 wpa_printf(MSG_DEBUG, "P2P: No peer OOB Dev Pw "
9446 "received");
9447 return -1;
9448 }
9449
9450 id = WPA_GET_BE16(params.oob_dev_pw + WPS_OOB_PUBKEY_HASH_LEN);
9451 wpa_printf(MSG_DEBUG, "P2P: Peer OOB Dev Pw %u", id);
9452 wpa_hexdump(MSG_DEBUG, "P2P: Peer OOB Public Key hash",
9453 params.oob_dev_pw, WPS_OOB_PUBKEY_HASH_LEN);
9454 os_memcpy(wpa_s->p2p_peer_oob_pubkey_hash,
9455 params.oob_dev_pw, WPS_OOB_PUBKEY_HASH_LEN);
9456 wpa_s->p2p_peer_oob_pk_hash_known = 1;
9457
9458 if (tag) {
9459 if (id < 0x10) {
9460 wpa_printf(MSG_DEBUG, "P2P: Static handover - invalid "
9461 "peer OOB Device Password Id %u", id);
9462 return -1;
9463 }
9464 wpa_printf(MSG_DEBUG, "P2P: Static handover - use peer OOB "
9465 "Device Password Id %u", id);
9466 wpa_hexdump_key(MSG_DEBUG, "P2P: Peer OOB Device Password",
9467 params.oob_dev_pw + WPS_OOB_PUBKEY_HASH_LEN + 2,
9468 params.oob_dev_pw_len -
9469 WPS_OOB_PUBKEY_HASH_LEN - 2);
9470 wpa_s->p2p_oob_dev_pw_id = id;
9471 wpa_s->p2p_oob_dev_pw = wpabuf_alloc_copy(
9472 params.oob_dev_pw + WPS_OOB_PUBKEY_HASH_LEN + 2,
9473 params.oob_dev_pw_len -
9474 WPS_OOB_PUBKEY_HASH_LEN - 2);
9475 if (wpa_s->p2p_oob_dev_pw == NULL)
9476 return -1;
9477
9478 if (wpa_s->conf->wps_nfc_dh_pubkey == NULL &&
9479 wps_nfc_gen_dh(&wpa_s->conf->wps_nfc_dh_pubkey,
9480 &wpa_s->conf->wps_nfc_dh_privkey) < 0)
9481 return -1;
9482 } else {
9483 wpa_printf(MSG_DEBUG, "P2P: Using abbreviated WPS handshake "
9484 "without Device Password");
9485 wpa_s->p2p_oob_dev_pw_id = DEV_PW_NFC_CONNECTION_HANDOVER;
9486 }
9487
9488 switch (params.next_step) {
9489 case NO_ACTION:
9490 case BOTH_GO:
9491 case PEER_CLIENT:
9492 /* already covered above */
9493 return 0;
9494 case JOIN_GROUP:
9495 return wpas_p2p_nfc_join_group(wpa_s, &params);
9496 case AUTH_JOIN:
9497 return wpas_p2p_nfc_auth_join(wpa_s, &params, tag);
9498 case INIT_GO_NEG:
9499 return wpas_p2p_nfc_init_go_neg(wpa_s, &params, forced_freq);
9500 case RESP_GO_NEG:
9501 /* TODO: use own OOB Dev Pw */
9502 return wpas_p2p_nfc_resp_go_neg(wpa_s, &params, forced_freq);
9503 }
9504
9505 return -1;
9506}
9507
9508
9509int wpas_p2p_nfc_tag_process(struct wpa_supplicant *wpa_s,
9510 const struct wpabuf *data, int forced_freq)
9511{
9512 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
9513 return -1;
9514
9515 return wpas_p2p_nfc_connection_handover(wpa_s, data, 1, 1, forced_freq);
9516}
9517
9518
9519int wpas_p2p_nfc_report_handover(struct wpa_supplicant *wpa_s, int init,
9520 const struct wpabuf *req,
9521 const struct wpabuf *sel, int forced_freq)
9522{
9523 struct wpabuf *tmp;
9524 int ret;
9525
9526 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
9527 return -1;
9528
9529 wpa_printf(MSG_DEBUG, "NFC: P2P connection handover reported");
9530
9531 wpa_hexdump_ascii(MSG_DEBUG, "NFC: Req",
9532 wpabuf_head(req), wpabuf_len(req));
9533 wpa_hexdump_ascii(MSG_DEBUG, "NFC: Sel",
9534 wpabuf_head(sel), wpabuf_len(sel));
9535 if (forced_freq)
9536 wpa_printf(MSG_DEBUG, "NFC: Forced freq %d", forced_freq);
9537 tmp = ndef_parse_p2p(init ? sel : req);
9538 if (tmp == NULL) {
9539 wpa_printf(MSG_DEBUG, "P2P: Could not parse NDEF");
9540 return -1;
9541 }
9542
9543 ret = wpas_p2p_nfc_connection_handover(wpa_s, tmp, init, 0,
9544 forced_freq);
9545 wpabuf_free(tmp);
9546
9547 return ret;
9548}
9549
9550
9551int wpas_p2p_nfc_tag_enabled(struct wpa_supplicant *wpa_s, int enabled)
9552{
9553 const u8 *if_addr;
9554 int go_intent = wpa_s->conf->p2p_go_intent;
9555 struct wpa_supplicant *iface;
9556
9557 if (wpa_s->global->p2p == NULL)
9558 return -1;
9559
9560 if (!enabled) {
9561 wpa_printf(MSG_DEBUG, "P2P: Disable use of own NFC Tag");
9562 for (iface = wpa_s->global->ifaces; iface; iface = iface->next)
9563 {
9564 if (!iface->ap_iface)
9565 continue;
9566 hostapd_wps_nfc_token_disable(iface->ap_iface->bss[0]);
9567 }
9568 p2p_set_authorized_oob_dev_pw_id(wpa_s->global->p2p, 0,
9569 0, NULL);
9570 if (wpa_s->p2p_nfc_tag_enabled)
9571 wpas_p2p_remove_pending_group_interface(wpa_s);
9572 wpa_s->p2p_nfc_tag_enabled = 0;
9573 return 0;
9574 }
9575
9576 if (wpa_s->global->p2p_disabled)
9577 return -1;
9578
9579 if (wpa_s->conf->wps_nfc_dh_pubkey == NULL ||
9580 wpa_s->conf->wps_nfc_dh_privkey == NULL ||
9581 wpa_s->conf->wps_nfc_dev_pw == NULL ||
9582 wpa_s->conf->wps_nfc_dev_pw_id < 0x10) {
9583 wpa_printf(MSG_DEBUG, "P2P: NFC password token not configured "
9584 "to allow static handover cases");
9585 return -1;
9586 }
9587
9588 wpa_printf(MSG_DEBUG, "P2P: Enable use of own NFC Tag");
9589
9590 wpa_s->p2p_oob_dev_pw_id = wpa_s->conf->wps_nfc_dev_pw_id;
9591 wpabuf_free(wpa_s->p2p_oob_dev_pw);
9592 wpa_s->p2p_oob_dev_pw = wpabuf_dup(wpa_s->conf->wps_nfc_dev_pw);
9593 if (wpa_s->p2p_oob_dev_pw == NULL)
9594 return -1;
9595 wpa_s->p2p_peer_oob_pk_hash_known = 0;
9596
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -07009597 if (wpa_s->p2p_group_interface == P2P_GROUP_INTERFACE_GO ||
9598 wpa_s->p2p_group_interface == P2P_GROUP_INTERFACE_CLIENT) {
9599 /*
9600 * P2P Group Interface present and the command came on group
9601 * interface, so enable the token for the current interface.
9602 */
9603 wpa_s->create_p2p_iface = 0;
9604 } else {
9605 wpa_s->create_p2p_iface = wpas_p2p_create_iface(wpa_s);
9606 }
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08009607
9608 if (wpa_s->create_p2p_iface) {
9609 enum wpa_driver_if_type iftype;
9610 /* Prepare to add a new interface for the group */
9611 iftype = WPA_IF_P2P_GROUP;
9612 if (go_intent == 15)
9613 iftype = WPA_IF_P2P_GO;
9614 if (wpas_p2p_add_group_interface(wpa_s, iftype) < 0) {
9615 wpa_printf(MSG_ERROR, "P2P: Failed to allocate a new "
9616 "interface for the group");
9617 return -1;
9618 }
9619
9620 if_addr = wpa_s->pending_interface_addr;
Dmitry Shmidt9c175262016-03-03 10:20:07 -08009621 } else if (wpa_s->p2p_mgmt)
9622 if_addr = wpa_s->parent->own_addr;
9623 else
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08009624 if_addr = wpa_s->own_addr;
9625
9626 wpa_s->p2p_nfc_tag_enabled = enabled;
9627
9628 for (iface = wpa_s->global->ifaces; iface; iface = iface->next) {
9629 struct hostapd_data *hapd;
9630 if (iface->ap_iface == NULL)
9631 continue;
9632 hapd = iface->ap_iface->bss[0];
9633 wpabuf_free(hapd->conf->wps_nfc_dh_pubkey);
9634 hapd->conf->wps_nfc_dh_pubkey =
9635 wpabuf_dup(wpa_s->conf->wps_nfc_dh_pubkey);
9636 wpabuf_free(hapd->conf->wps_nfc_dh_privkey);
9637 hapd->conf->wps_nfc_dh_privkey =
9638 wpabuf_dup(wpa_s->conf->wps_nfc_dh_privkey);
9639 wpabuf_free(hapd->conf->wps_nfc_dev_pw);
9640 hapd->conf->wps_nfc_dev_pw =
9641 wpabuf_dup(wpa_s->conf->wps_nfc_dev_pw);
9642 hapd->conf->wps_nfc_dev_pw_id = wpa_s->conf->wps_nfc_dev_pw_id;
9643
9644 if (hostapd_wps_nfc_token_enable(iface->ap_iface->bss[0]) < 0) {
9645 wpa_dbg(iface, MSG_DEBUG,
9646 "P2P: Failed to enable NFC Tag for GO");
9647 }
9648 }
9649 p2p_set_authorized_oob_dev_pw_id(
9650 wpa_s->global->p2p, wpa_s->conf->wps_nfc_dev_pw_id, go_intent,
9651 if_addr);
9652
9653 return 0;
9654}
9655
9656#endif /* CONFIG_WPS_NFC */
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07009657
9658
9659static void wpas_p2p_optimize_listen_channel(struct wpa_supplicant *wpa_s,
9660 struct wpa_used_freq_data *freqs,
9661 unsigned int num)
9662{
9663 u8 curr_chan, cand, chan;
9664 unsigned int i;
9665
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08009666 /*
9667 * If possible, optimize the Listen channel to be a channel that is
9668 * already used by one of the other interfaces.
9669 */
9670 if (!wpa_s->conf->p2p_optimize_listen_chan)
9671 return;
9672
9673 if (!wpa_s->current_ssid || wpa_s->wpa_state != WPA_COMPLETED)
9674 return;
9675
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07009676 curr_chan = p2p_get_listen_channel(wpa_s->global->p2p);
9677 for (i = 0, cand = 0; i < num; i++) {
9678 ieee80211_freq_to_chan(freqs[i].freq, &chan);
9679 if (curr_chan == chan) {
9680 cand = 0;
9681 break;
9682 }
9683
9684 if (chan == 1 || chan == 6 || chan == 11)
9685 cand = chan;
9686 }
9687
9688 if (cand) {
9689 wpa_dbg(wpa_s, MSG_DEBUG,
Dmitry Shmidt216983b2015-02-06 10:50:36 -08009690 "P2P: Update Listen channel to %u based on operating channel",
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07009691 cand);
9692 p2p_set_listen_channel(wpa_s->global->p2p, 81, cand, 0);
9693 }
9694}
9695
9696
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08009697static int wpas_p2p_move_go_csa(struct wpa_supplicant *wpa_s)
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07009698{
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08009699 struct hostapd_config *conf;
9700 struct p2p_go_neg_results params;
9701 struct csa_settings csa_settings;
9702 struct wpa_ssid *current_ssid = wpa_s->current_ssid;
9703 int old_freq = current_ssid->frequency;
9704 int ret;
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07009705
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08009706 if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_AP_CSA)) {
9707 wpa_dbg(wpa_s, MSG_DEBUG, "CSA is not enabled");
9708 return -1;
9709 }
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07009710
9711 /*
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08009712 * TODO: This function may not always work correctly. For example,
9713 * when we have a running GO and a BSS on a DFS channel.
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07009714 */
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08009715 if (wpas_p2p_init_go_params(wpa_s, &params, 0, 0, 0, 0, 0, 0, 0,
9716 NULL)) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08009717 wpa_dbg(wpa_s, MSG_DEBUG,
9718 "P2P CSA: Failed to select new frequency for GO");
9719 return -1;
9720 }
9721
9722 if (current_ssid->frequency == params.freq) {
9723 wpa_dbg(wpa_s, MSG_DEBUG,
9724 "P2P CSA: Selected same frequency - not moving GO");
9725 return 0;
9726 }
9727
9728 conf = hostapd_config_defaults();
9729 if (!conf) {
9730 wpa_dbg(wpa_s, MSG_DEBUG,
9731 "P2P CSA: Failed to allocate default config");
9732 return -1;
9733 }
9734
9735 current_ssid->frequency = params.freq;
9736 if (wpa_supplicant_conf_ap_ht(wpa_s, current_ssid, conf)) {
9737 wpa_dbg(wpa_s, MSG_DEBUG,
9738 "P2P CSA: Failed to create new GO config");
9739 ret = -1;
9740 goto out;
9741 }
9742
9743 if (conf->hw_mode != wpa_s->ap_iface->current_mode->mode) {
9744 wpa_dbg(wpa_s, MSG_DEBUG,
9745 "P2P CSA: CSA to a different band is not supported");
9746 ret = -1;
9747 goto out;
9748 }
9749
9750 os_memset(&csa_settings, 0, sizeof(csa_settings));
9751 csa_settings.cs_count = P2P_GO_CSA_COUNT;
9752 csa_settings.block_tx = P2P_GO_CSA_BLOCK_TX;
9753 csa_settings.freq_params.freq = params.freq;
9754 csa_settings.freq_params.sec_channel_offset = conf->secondary_channel;
9755 csa_settings.freq_params.ht_enabled = conf->ieee80211n;
9756 csa_settings.freq_params.bandwidth = conf->secondary_channel ? 40 : 20;
9757
9758 if (conf->ieee80211ac) {
9759 int freq1 = 0, freq2 = 0;
9760 u8 chan, opclass;
9761
9762 if (ieee80211_freq_to_channel_ext(params.freq,
9763 conf->secondary_channel,
9764 conf->vht_oper_chwidth,
9765 &opclass, &chan) ==
9766 NUM_HOSTAPD_MODES) {
9767 wpa_printf(MSG_ERROR, "P2P CSA: Bad freq");
9768 ret = -1;
9769 goto out;
9770 }
9771
9772 if (conf->vht_oper_centr_freq_seg0_idx)
9773 freq1 = ieee80211_chan_to_freq(
9774 NULL, opclass,
9775 conf->vht_oper_centr_freq_seg0_idx);
9776
9777 if (conf->vht_oper_centr_freq_seg1_idx)
9778 freq2 = ieee80211_chan_to_freq(
9779 NULL, opclass,
9780 conf->vht_oper_centr_freq_seg1_idx);
9781
9782 if (freq1 < 0 || freq2 < 0) {
9783 wpa_dbg(wpa_s, MSG_DEBUG,
9784 "P2P CSA: Selected invalid VHT center freqs");
9785 ret = -1;
9786 goto out;
9787 }
9788
9789 csa_settings.freq_params.vht_enabled = conf->ieee80211ac;
9790 csa_settings.freq_params.center_freq1 = freq1;
9791 csa_settings.freq_params.center_freq2 = freq2;
9792
9793 switch (conf->vht_oper_chwidth) {
Sunil8cd6f4d2022-06-28 18:40:46 +00009794 case CONF_OPER_CHWIDTH_80MHZ:
9795 case CONF_OPER_CHWIDTH_80P80MHZ:
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08009796 csa_settings.freq_params.bandwidth = 80;
9797 break;
Sunil8cd6f4d2022-06-28 18:40:46 +00009798 case CONF_OPER_CHWIDTH_160MHZ:
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08009799 csa_settings.freq_params.bandwidth = 160;
9800 break;
Sunil8cd6f4d2022-06-28 18:40:46 +00009801 default:
9802 break;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08009803 }
9804 }
9805
9806 ret = ap_switch_channel(wpa_s, &csa_settings);
9807out:
9808 current_ssid->frequency = old_freq;
9809 hostapd_config_free(conf);
9810 return ret;
9811}
9812
9813
9814static void wpas_p2p_move_go_no_csa(struct wpa_supplicant *wpa_s)
9815{
9816 struct p2p_go_neg_results params;
9817 struct wpa_ssid *current_ssid = wpa_s->current_ssid;
Hai Shalom899fcc72020-10-19 14:38:18 -07009818 void (*ap_configured_cb)(void *ctx, void *data);
9819 void *ap_configured_cb_ctx, *ap_configured_cb_data;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08009820
9821 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_REMOVE_AND_REFORM_GROUP);
9822
9823 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Move GO from freq=%d MHz",
9824 current_ssid->frequency);
9825
9826 /* Stop the AP functionality */
9827 /* TODO: Should do this in a way that does not indicated to possible
9828 * P2P Clients in the group that the group is terminated. */
Hai Shalom899fcc72020-10-19 14:38:18 -07009829 /* If this action occurs before a group is started, the callback should
9830 * be preserved, or GROUP-STARTED event would be lost. If this action
9831 * occurs after a group is started, these pointers are all NULL and
9832 * harmless. */
9833 ap_configured_cb = wpa_s->ap_configured_cb;
9834 ap_configured_cb_ctx = wpa_s->ap_configured_cb_ctx;
9835 ap_configured_cb_data = wpa_s->ap_configured_cb_data;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08009836 wpa_supplicant_ap_deinit(wpa_s);
9837
9838 /* Reselect the GO frequency */
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08009839 if (wpas_p2p_init_go_params(wpa_s, &params, 0, 0, 0, 0, 0, 0, 0,
9840 NULL)) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08009841 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Failed to reselect freq");
9842 wpas_p2p_group_delete(wpa_s,
9843 P2P_GROUP_REMOVAL_GO_LEAVE_CHANNEL);
9844 return;
9845 }
9846 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: New freq selected for the GO (%u MHz)",
9847 params.freq);
9848
9849 if (params.freq &&
9850 !p2p_supported_freq_go(wpa_s->global->p2p, params.freq)) {
9851 wpa_printf(MSG_DEBUG,
9852 "P2P: Selected freq (%u MHz) is not valid for P2P",
9853 params.freq);
9854 wpas_p2p_group_delete(wpa_s,
9855 P2P_GROUP_REMOVAL_GO_LEAVE_CHANNEL);
9856 return;
9857 }
9858
Hai Shalom899fcc72020-10-19 14:38:18 -07009859 /* Restore preserved callback parameters */
Jimmy Chen4b2a5022020-08-26 18:55:45 +08009860 wpa_s->ap_configured_cb = ap_configured_cb;
9861 wpa_s->ap_configured_cb_ctx = ap_configured_cb_ctx;
9862 wpa_s->ap_configured_cb_data = ap_configured_cb_data;
Hai Shalom899fcc72020-10-19 14:38:18 -07009863
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08009864 /* Update the frequency */
9865 current_ssid->frequency = params.freq;
9866 wpa_s->connect_without_scan = current_ssid;
9867 wpa_s->reassociate = 1;
9868 wpa_s->disconnected = 0;
9869 wpa_supplicant_req_scan(wpa_s, 0, 0);
9870}
9871
9872
9873static void wpas_p2p_move_go(void *eloop_ctx, void *timeout_ctx)
9874{
9875 struct wpa_supplicant *wpa_s = eloop_ctx;
9876
9877 if (!wpa_s->ap_iface || !wpa_s->current_ssid)
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07009878 return;
9879
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08009880 wpas_p2p_go_update_common_freqs(wpa_s);
9881
9882 /* Do not move GO in the middle of a CSA */
9883 if (hostapd_csa_in_progress(wpa_s->ap_iface)) {
9884 wpa_printf(MSG_DEBUG,
9885 "P2P: CSA is in progress - not moving GO");
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07009886 return;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08009887 }
9888
9889 /*
9890 * First, try a channel switch flow. If it is not supported or fails,
9891 * take down the GO and bring it up again.
9892 */
9893 if (wpas_p2p_move_go_csa(wpa_s) < 0)
9894 wpas_p2p_move_go_no_csa(wpa_s);
9895}
9896
9897
9898static void wpas_p2p_reconsider_moving_go(void *eloop_ctx, void *timeout_ctx)
9899{
9900 struct wpa_supplicant *wpa_s = eloop_ctx;
9901 struct wpa_used_freq_data *freqs = NULL;
9902 unsigned int num = wpa_s->num_multichan_concurrent;
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07009903
9904 freqs = os_calloc(num, sizeof(struct wpa_used_freq_data));
9905 if (!freqs)
9906 return;
9907
9908 num = get_shared_radio_freqs_data(wpa_s, freqs, num);
9909
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08009910 /* Previous attempt to move a GO was not possible -- try again. */
9911 wpas_p2p_consider_moving_gos(wpa_s, freqs, num,
9912 WPAS_P2P_CHANNEL_UPDATE_ANY);
9913
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07009914 os_free(freqs);
9915}
9916
9917
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08009918/*
9919 * Consider moving a GO from its currently used frequency:
9920 * 1. It is possible that due to regulatory consideration the frequency
9921 * can no longer be used and there is a need to evacuate the GO.
9922 * 2. It is possible that due to MCC considerations, it would be preferable
9923 * to move the GO to a channel that is currently used by some other
9924 * station interface.
9925 *
9926 * In case a frequency that became invalid is once again valid, cancel a
9927 * previously initiated GO frequency change.
9928 */
9929static void wpas_p2p_consider_moving_one_go(struct wpa_supplicant *wpa_s,
9930 struct wpa_used_freq_data *freqs,
9931 unsigned int num)
9932{
9933 unsigned int i, invalid_freq = 0, policy_move = 0, flags = 0;
9934 unsigned int timeout;
9935 int freq;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07009936 int dfs_offload;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08009937
9938 wpas_p2p_go_update_common_freqs(wpa_s);
9939
9940 freq = wpa_s->current_ssid->frequency;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07009941 dfs_offload = (wpa_s->drv_flags & WPA_DRIVER_FLAGS_DFS_OFFLOAD) &&
Roshan Pius3a1667e2018-07-03 15:17:14 -07009942 ieee80211_is_dfs(freq, wpa_s->hw.modes, wpa_s->hw.num_modes);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08009943 for (i = 0, invalid_freq = 0; i < num; i++) {
9944 if (freqs[i].freq == freq) {
9945 flags = freqs[i].flags;
9946
9947 /* The channel is invalid, must change it */
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07009948 if (!p2p_supported_freq_go(wpa_s->global->p2p, freq) &&
9949 !dfs_offload) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08009950 wpa_dbg(wpa_s, MSG_DEBUG,
9951 "P2P: Freq=%d MHz no longer valid for GO",
9952 freq);
9953 invalid_freq = 1;
9954 }
9955 } else if (freqs[i].flags == 0) {
9956 /* Freq is not used by any other station interface */
9957 continue;
9958 } else if (!p2p_supported_freq(wpa_s->global->p2p,
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07009959 freqs[i].freq) && !dfs_offload) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08009960 /* Freq is not valid for P2P use cases */
9961 continue;
9962 } else if (wpa_s->conf->p2p_go_freq_change_policy ==
9963 P2P_GO_FREQ_MOVE_SCM) {
9964 policy_move = 1;
9965 } else if (wpa_s->conf->p2p_go_freq_change_policy ==
9966 P2P_GO_FREQ_MOVE_SCM_PEER_SUPPORTS &&
9967 wpas_p2p_go_is_peer_freq(wpa_s, freqs[i].freq)) {
9968 policy_move = 1;
9969 } else if ((wpa_s->conf->p2p_go_freq_change_policy ==
9970 P2P_GO_FREQ_MOVE_SCM_ECSA) &&
9971 wpas_p2p_go_is_peer_freq(wpa_s, freqs[i].freq)) {
9972 if (!p2p_get_group_num_members(wpa_s->p2p_group)) {
9973 policy_move = 1;
9974 } else if ((wpa_s->drv_flags &
9975 WPA_DRIVER_FLAGS_AP_CSA) &&
9976 wpas_p2p_go_clients_support_ecsa(wpa_s)) {
9977 u8 chan;
9978
9979 /*
9980 * We do not support CSA between bands, so move
9981 * GO only within the same band.
9982 */
9983 if (wpa_s->ap_iface->current_mode->mode ==
9984 ieee80211_freq_to_chan(freqs[i].freq,
9985 &chan))
9986 policy_move = 1;
9987 }
9988 }
9989 }
9990
9991 wpa_dbg(wpa_s, MSG_DEBUG,
9992 "P2P: GO move: invalid_freq=%u, policy_move=%u, flags=0x%X",
9993 invalid_freq, policy_move, flags);
9994
9995 /*
9996 * The channel is valid, or we are going to have a policy move, so
9997 * cancel timeout.
9998 */
9999 if (!invalid_freq || policy_move) {
10000 wpa_dbg(wpa_s, MSG_DEBUG,
10001 "P2P: Cancel a GO move from freq=%d MHz", freq);
10002 eloop_cancel_timeout(wpas_p2p_move_go, wpa_s, NULL);
10003
10004 if (wpas_p2p_in_progress(wpa_s)) {
10005 wpa_dbg(wpa_s, MSG_DEBUG,
10006 "P2P: GO move: policy CS is not allowed - setting timeout to re-consider GO move");
10007 eloop_cancel_timeout(wpas_p2p_reconsider_moving_go,
10008 wpa_s, NULL);
10009 eloop_register_timeout(P2P_RECONSIDER_GO_MOVE_DELAY, 0,
10010 wpas_p2p_reconsider_moving_go,
10011 wpa_s, NULL);
10012 return;
10013 }
10014 }
10015
10016 if (!invalid_freq && (!policy_move || flags != 0)) {
10017 wpa_dbg(wpa_s, MSG_DEBUG,
10018 "P2P: Not initiating a GO frequency change");
10019 return;
10020 }
10021
10022 /*
10023 * Do not consider moving GO if it is in the middle of a CSA. When the
10024 * CSA is finished this flow should be retriggered.
10025 */
10026 if (hostapd_csa_in_progress(wpa_s->ap_iface)) {
10027 wpa_dbg(wpa_s, MSG_DEBUG,
10028 "P2P: Not initiating a GO frequency change - CSA is in progress");
10029 return;
10030 }
10031
10032 if (invalid_freq && !wpas_p2p_disallowed_freq(wpa_s->global, freq))
10033 timeout = P2P_GO_FREQ_CHANGE_TIME;
10034 else
10035 timeout = 0;
10036
10037 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Move GO from freq=%d MHz in %d secs",
10038 freq, timeout);
10039 eloop_cancel_timeout(wpas_p2p_move_go, wpa_s, NULL);
10040 eloop_register_timeout(timeout, 0, wpas_p2p_move_go, wpa_s, NULL);
10041}
10042
10043
10044static void wpas_p2p_consider_moving_gos(struct wpa_supplicant *wpa_s,
10045 struct wpa_used_freq_data *freqs,
10046 unsigned int num,
10047 enum wpas_p2p_channel_update_trig trig)
10048{
10049 struct wpa_supplicant *ifs;
10050
10051 eloop_cancel_timeout(wpas_p2p_reconsider_moving_go, ELOOP_ALL_CTX,
10052 NULL);
10053
10054 /*
10055 * Travers all the radio interfaces, and for each GO interface, check
10056 * if there is a need to move the GO from the frequency it is using,
10057 * or in case the frequency is valid again, cancel the evacuation flow.
10058 */
10059 dl_list_for_each(ifs, &wpa_s->radio->ifaces, struct wpa_supplicant,
10060 radio_list) {
10061 if (ifs->current_ssid == NULL ||
10062 ifs->current_ssid->mode != WPAS_MODE_P2P_GO)
10063 continue;
10064
10065 /*
10066 * The GO was just started or completed channel switch, no need
10067 * to move it.
10068 */
10069 if (wpa_s == ifs &&
10070 (trig == WPAS_P2P_CHANNEL_UPDATE_STATE_CHANGE ||
10071 trig == WPAS_P2P_CHANNEL_UPDATE_CS)) {
10072 wpa_dbg(wpa_s, MSG_DEBUG,
10073 "P2P: GO move - schedule re-consideration");
10074 eloop_register_timeout(P2P_RECONSIDER_GO_MOVE_DELAY, 0,
10075 wpas_p2p_reconsider_moving_go,
10076 wpa_s, NULL);
10077 continue;
10078 }
10079
10080 wpas_p2p_consider_moving_one_go(ifs, freqs, num);
10081 }
10082}
10083
10084
10085void wpas_p2p_indicate_state_change(struct wpa_supplicant *wpa_s)
10086{
10087 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
10088 return;
10089
10090 wpas_p2p_update_channel_list(wpa_s,
10091 WPAS_P2P_CHANNEL_UPDATE_STATE_CHANGE);
10092}
10093
10094
Dmitry Shmidt43cb5782014-06-16 16:23:22 -070010095void wpas_p2p_deinit_iface(struct wpa_supplicant *wpa_s)
10096{
Dmitry Shmidt43cb5782014-06-16 16:23:22 -070010097 if (wpa_s == wpa_s->global->p2p_init_wpa_s && wpa_s->global->p2p) {
10098 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Disable P2P since removing "
10099 "the management interface is being removed");
10100 wpas_p2p_deinit_global(wpa_s->global);
10101 }
10102}
10103
10104
10105void wpas_p2p_ap_deinit(struct wpa_supplicant *wpa_s)
10106{
10107 if (wpa_s->ap_iface->bss)
10108 wpa_s->ap_iface->bss[0]->p2p_group = NULL;
10109 wpas_p2p_group_deinit(wpa_s);
10110}
Dmitry Shmidt58d12ad2016-07-28 10:07:03 -070010111
10112
10113int wpas_p2p_lo_start(struct wpa_supplicant *wpa_s, unsigned int freq,
10114 unsigned int period, unsigned int interval,
10115 unsigned int count)
10116{
10117 struct p2p_data *p2p = wpa_s->global->p2p;
10118 u8 *device_types;
10119 size_t dev_types_len;
10120 struct wpabuf *buf;
10121 int ret;
10122
10123 if (wpa_s->p2p_lo_started) {
10124 wpa_dbg(wpa_s, MSG_DEBUG,
10125 "P2P Listen offload is already started");
10126 return 0;
10127 }
10128
10129 if (wpa_s->global->p2p == NULL ||
10130 !(wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_LISTEN_OFFLOAD)) {
10131 wpa_printf(MSG_DEBUG, "P2P: Listen offload not supported");
10132 return -1;
10133 }
10134
10135 if (!p2p_supported_freq(wpa_s->global->p2p, freq)) {
10136 wpa_printf(MSG_ERROR, "P2P: Input channel not supported: %u",
10137 freq);
10138 return -1;
10139 }
10140
10141 /* Get device type */
10142 dev_types_len = (wpa_s->conf->num_sec_device_types + 1) *
10143 WPS_DEV_TYPE_LEN;
10144 device_types = os_malloc(dev_types_len);
10145 if (!device_types)
10146 return -1;
10147 os_memcpy(device_types, wpa_s->conf->device_type, WPS_DEV_TYPE_LEN);
10148 os_memcpy(&device_types[WPS_DEV_TYPE_LEN], wpa_s->conf->sec_device_type,
10149 wpa_s->conf->num_sec_device_types * WPS_DEV_TYPE_LEN);
10150
10151 /* Get Probe Response IE(s) */
10152 buf = p2p_build_probe_resp_template(p2p, freq);
10153 if (!buf) {
10154 os_free(device_types);
10155 return -1;
10156 }
10157
10158 ret = wpa_drv_p2p_lo_start(wpa_s, freq, period, interval, count,
10159 device_types, dev_types_len,
10160 wpabuf_mhead_u8(buf), wpabuf_len(buf));
10161 if (ret < 0)
10162 wpa_dbg(wpa_s, MSG_DEBUG,
10163 "P2P: Failed to start P2P listen offload");
10164
10165 os_free(device_types);
10166 wpabuf_free(buf);
10167
10168 if (ret == 0) {
10169 wpa_s->p2p_lo_started = 1;
10170
10171 /* Stop current P2P listen if any */
10172 wpas_stop_listen(wpa_s);
10173 }
10174
10175 return ret;
10176}
10177
10178
10179int wpas_p2p_lo_stop(struct wpa_supplicant *wpa_s)
10180{
10181 int ret;
10182
10183 if (!wpa_s->p2p_lo_started)
10184 return 0;
10185
10186 ret = wpa_drv_p2p_lo_stop(wpa_s);
10187 if (ret < 0)
10188 wpa_dbg(wpa_s, MSG_DEBUG,
10189 "P2P: Failed to stop P2P listen offload");
10190
10191 wpa_s->p2p_lo_started = 0;
10192 return ret;
10193}