blob: db9917796e2285ceaa2346d56e289f8c225370c1 [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);
Sunil Ravi036cec52023-03-29 11:35:17 -0700168static int wpas_p2p_disallowed_freq(struct wpa_global *global,
169 unsigned int freq);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700170
171
Hai Shalomc1a21442022-02-04 13:43:00 -0800172static int wpas_get_6ghz_he_chwidth_capab(struct hostapd_hw_modes *mode)
173{
174 int he_capab = 0;
175
176 if (mode)
177 he_capab = mode->he_capab[WPAS_MODE_INFRA].phy_cap[
178 HE_PHYCAP_CHANNEL_WIDTH_SET_IDX];
179 return he_capab;
180}
181
182
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -0700183/*
184 * Get the number of concurrent channels that the HW can operate, but that are
185 * currently not in use by any of the wpa_supplicant interfaces.
186 */
187static int wpas_p2p_num_unused_channels(struct wpa_supplicant *wpa_s)
188{
189 int *freqs;
Dmitry Shmidtb96dad42013-11-05 10:07:29 -0800190 int num, unused;
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -0700191
192 freqs = os_calloc(wpa_s->num_multichan_concurrent, sizeof(int));
193 if (!freqs)
194 return -1;
195
196 num = get_shared_radio_freqs(wpa_s, freqs,
Sunil Ravi77d572f2023-01-17 23:58:31 +0000197 wpa_s->num_multichan_concurrent, false);
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -0700198 os_free(freqs);
199
Dmitry Shmidtb96dad42013-11-05 10:07:29 -0800200 unused = wpa_s->num_multichan_concurrent - num;
201 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: num_unused_channels: %d", unused);
202 return unused;
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -0700203}
204
205
206/*
207 * Get the frequencies that are currently in use by one or more of the virtual
208 * interfaces, and that are also valid for P2P operation.
209 */
Dmitry Shmidt43cb5782014-06-16 16:23:22 -0700210static unsigned int
211wpas_p2p_valid_oper_freqs(struct wpa_supplicant *wpa_s,
212 struct wpa_used_freq_data *p2p_freqs,
213 unsigned int len)
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -0700214{
Dmitry Shmidt43cb5782014-06-16 16:23:22 -0700215 struct wpa_used_freq_data *freqs;
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -0700216 unsigned int num, i, j;
217
Dmitry Shmidt43cb5782014-06-16 16:23:22 -0700218 freqs = os_calloc(wpa_s->num_multichan_concurrent,
219 sizeof(struct wpa_used_freq_data));
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -0700220 if (!freqs)
Dmitry Shmidt43cb5782014-06-16 16:23:22 -0700221 return 0;
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -0700222
Dmitry Shmidt43cb5782014-06-16 16:23:22 -0700223 num = get_shared_radio_freqs_data(wpa_s, freqs,
Sunil Ravi77d572f2023-01-17 23:58:31 +0000224 wpa_s->num_multichan_concurrent,
225 false);
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -0700226
Dmitry Shmidt43cb5782014-06-16 16:23:22 -0700227 os_memset(p2p_freqs, 0, sizeof(struct wpa_used_freq_data) * len);
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -0700228
229 for (i = 0, j = 0; i < num && j < len; i++) {
Dmitry Shmidt43cb5782014-06-16 16:23:22 -0700230 if (p2p_supported_freq(wpa_s->global->p2p, freqs[i].freq))
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -0700231 p2p_freqs[j++] = freqs[i];
232 }
233
234 os_free(freqs);
235
Dmitry Shmidt43cb5782014-06-16 16:23:22 -0700236 dump_freq_data(wpa_s, "valid for P2P", p2p_freqs, j);
Dmitry Shmidtb96dad42013-11-05 10:07:29 -0800237
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -0700238 return j;
239}
240
241
Dmitry Shmidt700a1372013-03-15 14:14:44 -0700242static void wpas_p2p_set_own_freq_preference(struct wpa_supplicant *wpa_s,
243 int freq)
244{
245 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
246 return;
Dmitry Shmidt8bd70b72015-05-26 16:02:19 -0700247
248 /* Use the wpa_s used to control the P2P Device operation */
249 wpa_s = wpa_s->global->p2p_init_wpa_s;
250
251 if (wpa_s->conf->p2p_ignore_shared_freq &&
Dmitry Shmidta0d265f2013-11-19 13:13:41 -0800252 freq > 0 && wpa_s->num_multichan_concurrent > 1 &&
253 wpas_p2p_num_unused_channels(wpa_s) > 0) {
254 wpa_printf(MSG_DEBUG, "P2P: Ignore own channel preference %d MHz due to p2p_ignore_shared_freq=1 configuration",
255 freq);
Dmitry Shmidt700a1372013-03-15 14:14:44 -0700256 freq = 0;
Dmitry Shmidta0d265f2013-11-19 13:13:41 -0800257 }
Dmitry Shmidt700a1372013-03-15 14:14:44 -0700258 p2p_set_own_freq_preference(wpa_s->global->p2p, freq);
259}
260
261
Hai Shalom60840252021-02-19 19:02:11 -0800262static void wpas_p2p_scan_res_handled(struct wpa_supplicant *wpa_s)
263{
264 unsigned int delay = wpas_p2p_search_delay(wpa_s);
265
266 /* In case of concurrent P2P and external scans, delay P2P search. */
267 if (external_scan_running(wpa_s->radio)) {
268 delay = wpa_s->conf->p2p_search_delay;
269 wpa_printf(MSG_DEBUG,
270 "P2P: Delay next P2P search by %d ms to let externally triggered scan complete",
271 delay);
272 }
273
274 p2p_scan_res_handled(wpa_s->global->p2p, delay);
275}
276
277
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700278static void wpas_p2p_scan_res_handler(struct wpa_supplicant *wpa_s,
279 struct wpa_scan_results *scan_res)
280{
281 size_t i;
282
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800283 if (wpa_s->p2p_scan_work) {
284 struct wpa_radio_work *work = wpa_s->p2p_scan_work;
285 wpa_s->p2p_scan_work = NULL;
286 radio_work_done(work);
287 }
288
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700289 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
290 return;
291
292 wpa_printf(MSG_DEBUG, "P2P: Scan results received (%d BSS)",
293 (int) scan_res->num);
294
295 for (i = 0; i < scan_res->num; i++) {
296 struct wpa_scan_res *bss = scan_res->res[i];
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800297 struct os_reltime time_tmp_age, entry_ts;
Dmitry Shmidt96571392013-10-14 12:54:46 -0700298 const u8 *ies;
299 size_t ies_len;
300
Dmitry Shmidtf8623282013-02-20 14:34:59 -0800301 time_tmp_age.sec = bss->age / 1000;
302 time_tmp_age.usec = (bss->age % 1000) * 1000;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800303 os_reltime_sub(&scan_res->fetch_time, &time_tmp_age, &entry_ts);
Dmitry Shmidt96571392013-10-14 12:54:46 -0700304
305 ies = (const u8 *) (bss + 1);
306 ies_len = bss->ie_len;
307 if (bss->beacon_ie_len > 0 &&
308 !wpa_scan_get_vendor_ie(bss, P2P_IE_VENDOR_TYPE) &&
309 wpa_scan_get_vendor_ie_beacon(bss, P2P_IE_VENDOR_TYPE)) {
310 wpa_printf(MSG_DEBUG, "P2P: Use P2P IE(s) from Beacon frame since no P2P IE(s) in Probe Response frames received for "
311 MACSTR, MAC2STR(bss->bssid));
312 ies = ies + ies_len;
313 ies_len = bss->beacon_ie_len;
314 }
315
316
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700317 if (p2p_scan_res_handler(wpa_s->global->p2p, bss->bssid,
Dmitry Shmidtf8623282013-02-20 14:34:59 -0800318 bss->freq, &entry_ts, bss->level,
Dmitry Shmidt96571392013-10-14 12:54:46 -0700319 ies, ies_len) > 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700320 break;
321 }
322
Hai Shalom60840252021-02-19 19:02:11 -0800323 wpas_p2p_scan_res_handled(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700324}
325
326
Hai Shalom60840252021-02-19 19:02:11 -0800327static void wpas_p2p_scan_res_fail_handler(struct wpa_supplicant *wpa_s)
Hai Shalom899fcc72020-10-19 14:38:18 -0700328{
Hai Shalom60840252021-02-19 19:02:11 -0800329 if (wpa_s->p2p_scan_work) {
330 struct wpa_radio_work *work = wpa_s->p2p_scan_work;
Hai Shalom899fcc72020-10-19 14:38:18 -0700331
Hai Shalom60840252021-02-19 19:02:11 -0800332 wpa_s->p2p_scan_work = NULL;
333 radio_work_done(work);
Hai Shalom899fcc72020-10-19 14:38:18 -0700334 }
335
Hai Shalom60840252021-02-19 19:02:11 -0800336 if (wpa_s->global->p2p_disabled || !wpa_s->global->p2p)
337 return;
Hai Shalom899fcc72020-10-19 14:38:18 -0700338
Hai Shalom60840252021-02-19 19:02:11 -0800339 wpa_dbg(wpa_s, MSG_DEBUG,
340 "P2P: Failed to get scan results - try to continue");
341 wpas_p2p_scan_res_handled(wpa_s);
Hai Shalom899fcc72020-10-19 14:38:18 -0700342}
343
344
Sunil Ravi77d572f2023-01-17 23:58:31 +0000345void wpas_p2p_scan_freqs(struct wpa_supplicant *wpa_s,
346 struct wpa_driver_scan_params *params,
347 bool include_6ghz)
348{
349 wpa_add_scan_freqs_list(wpa_s, HOSTAPD_MODE_IEEE80211A,
350 params, false, false, false);
351 wpa_add_scan_freqs_list(wpa_s, HOSTAPD_MODE_IEEE80211G,
352 params, false, false, false);
353 wpa_add_scan_freqs_list(wpa_s, HOSTAPD_MODE_IEEE80211AD,
354 params, false, false, false);
355 if (!wpa_s->conf->p2p_6ghz_disable &&
356 is_p2p_allow_6ghz(wpa_s->global->p2p) && include_6ghz)
357 wpa_add_scan_freqs_list(wpa_s, HOSTAPD_MODE_IEEE80211A,
358 params, true, true, false);
359}
360
361
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800362static void wpas_p2p_trigger_scan_cb(struct wpa_radio_work *work, int deinit)
363{
364 struct wpa_supplicant *wpa_s = work->wpa_s;
365 struct wpa_driver_scan_params *params = work->ctx;
366 int ret;
367
368 if (deinit) {
Dmitry Shmidtbd14a572014-02-18 10:33:49 -0800369 if (!work->started) {
370 wpa_scan_free_params(params);
371 return;
372 }
373
374 wpa_s->p2p_scan_work = NULL;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800375 return;
376 }
377
Dmitry Shmidtebd93af2017-02-21 13:40:44 -0800378 if (wpa_s->clear_driver_scan_cache) {
379 wpa_printf(MSG_DEBUG,
380 "Request driver to clear scan cache due to local BSS flush");
381 params->only_new_results = 1;
382 }
Hai Shalom899fcc72020-10-19 14:38:18 -0700383
Sunil Ravi77d572f2023-01-17 23:58:31 +0000384 if (!params->freqs)
385 wpas_p2p_scan_freqs(wpa_s, params, params->p2p_include_6ghz);
386
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800387 ret = wpa_drv_scan(wpa_s, params);
Dmitry Shmidtabb90a32016-12-05 15:34:39 -0800388 if (ret == 0)
389 wpa_s->curr_scan_cookie = params->scan_cookie;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800390 wpa_scan_free_params(params);
391 work->ctx = NULL;
392 if (ret) {
393 radio_work_done(work);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800394 p2p_notify_scan_trigger_status(wpa_s->global->p2p, ret);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800395 return;
396 }
397
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800398 p2p_notify_scan_trigger_status(wpa_s->global->p2p, ret);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800399 os_get_reltime(&wpa_s->scan_trigger_time);
400 wpa_s->scan_res_handler = wpas_p2p_scan_res_handler;
Hai Shalom60840252021-02-19 19:02:11 -0800401 wpa_s->scan_res_fail_handler = wpas_p2p_scan_res_fail_handler;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800402 wpa_s->own_scan_requested = 1;
Dmitry Shmidtebd93af2017-02-21 13:40:44 -0800403 wpa_s->clear_driver_scan_cache = 0;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800404 wpa_s->p2p_scan_work = work;
405}
406
407
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800408static int wpas_p2p_search_social_channel(struct wpa_supplicant *wpa_s,
409 int freq)
410{
411 if (wpa_s->global->p2p_24ghz_social_channels &&
412 (freq == 2412 || freq == 2437 || freq == 2462)) {
413 /*
414 * Search all social channels regardless of whether these have
415 * been disabled for P2P operating channel use to avoid missing
416 * peers.
417 */
418 return 1;
419 }
420 return p2p_supported_freq(wpa_s->global->p2p, freq);
421}
422
423
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700424static int wpas_p2p_scan(void *ctx, enum p2p_scan_type type, int freq,
425 unsigned int num_req_dev_types,
Hai Shalomc1a21442022-02-04 13:43:00 -0800426 const u8 *req_dev_types, const u8 *dev_id, u16 pw_id,
427 bool include_6ghz)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700428{
429 struct wpa_supplicant *wpa_s = ctx;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800430 struct wpa_driver_scan_params *params = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700431 struct wpabuf *wps_ie, *ies;
Dmitry Shmidt661b4f72014-09-29 14:58:27 -0700432 unsigned int num_channels = 0;
433 int social_channels_freq[] = { 2412, 2437, 2462, 60480 };
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800434 size_t ielen;
Dmitry Shmidt661b4f72014-09-29 14:58:27 -0700435 u8 *n, i;
Dmitry Shmidt9c175262016-03-03 10:20:07 -0800436 unsigned int bands;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700437
438 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
439 return -1;
440
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800441 if (wpa_s->p2p_scan_work) {
442 wpa_dbg(wpa_s, MSG_INFO, "P2P: Reject scan trigger since one is already pending");
443 return -1;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700444 }
445
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800446 params = os_zalloc(sizeof(*params));
447 if (params == NULL)
448 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700449
450 /* P2P Wildcard SSID */
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800451 params->num_ssids = 1;
452 n = os_malloc(P2P_WILDCARD_SSID_LEN);
453 if (n == NULL)
454 goto fail;
455 os_memcpy(n, P2P_WILDCARD_SSID, P2P_WILDCARD_SSID_LEN);
456 params->ssids[0].ssid = n;
457 params->ssids[0].ssid_len = P2P_WILDCARD_SSID_LEN;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700458
459 wpa_s->wps->dev.p2p = 1;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700460 wps_ie = wps_build_probe_req_ie(pw_id, &wpa_s->wps->dev,
461 wpa_s->wps->uuid, WPS_REQ_ENROLLEE,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700462 num_req_dev_types, req_dev_types);
463 if (wps_ie == NULL)
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800464 goto fail;
Sunil Ravi77d572f2023-01-17 23:58:31 +0000465
466 /*
467 * In case 6 GHz channels are requested as part of the P2P scan, only
468 * the PSCs would be included as P2P GOs are not expected to be
469 * collocated, i.e., they would not be announced in the RNR element of
470 * other APs.
471 */
Hai Shalomc1a21442022-02-04 13:43:00 -0800472 if (!wpa_s->conf->p2p_6ghz_disable)
473 params->p2p_include_6ghz = include_6ghz;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700474 switch (type) {
475 case P2P_SCAN_SOCIAL:
Dmitry Shmidt661b4f72014-09-29 14:58:27 -0700476 params->freqs = os_calloc(ARRAY_SIZE(social_channels_freq) + 1,
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 params->freqs[num_channels++] = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700487 break;
488 case P2P_SCAN_FULL:
489 break;
Dmitry Shmidt203eadb2015-03-05 14:16:04 -0800490 case P2P_SCAN_SPECIFIC:
491 params->freqs = os_calloc(2, sizeof(int));
492 if (params->freqs == NULL)
493 goto fail;
494 params->freqs[0] = freq;
495 params->freqs[1] = 0;
496 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700497 case P2P_SCAN_SOCIAL_PLUS_ONE:
Dmitry Shmidt661b4f72014-09-29 14:58:27 -0700498 params->freqs = os_calloc(ARRAY_SIZE(social_channels_freq) + 2,
499 sizeof(int));
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800500 if (params->freqs == NULL)
501 goto fail;
Dmitry Shmidt661b4f72014-09-29 14:58:27 -0700502 for (i = 0; i < ARRAY_SIZE(social_channels_freq); i++) {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800503 if (wpas_p2p_search_social_channel(
504 wpa_s, social_channels_freq[i]))
Dmitry Shmidt661b4f72014-09-29 14:58:27 -0700505 params->freqs[num_channels++] =
506 social_channels_freq[i];
507 }
508 if (p2p_supported_freq(wpa_s->global->p2p, freq))
509 params->freqs[num_channels++] = freq;
510 params->freqs[num_channels++] = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700511 break;
512 }
513
Dmitry Shmidt9c175262016-03-03 10:20:07 -0800514 ielen = p2p_scan_ie_buf_len(wpa_s->global->p2p);
515 ies = wpabuf_alloc(wpabuf_len(wps_ie) + ielen);
516 if (ies == NULL) {
517 wpabuf_free(wps_ie);
518 goto fail;
519 }
520 wpabuf_put_buf(ies, wps_ie);
521 wpabuf_free(wps_ie);
522
523 bands = wpas_get_bands(wpa_s, params->freqs);
524 p2p_scan_ie(wpa_s->global->p2p, ies, dev_id, bands);
525
526 params->p2p_probe = 1;
527 n = os_malloc(wpabuf_len(ies));
528 if (n == NULL) {
529 wpabuf_free(ies);
530 goto fail;
531 }
532 os_memcpy(n, wpabuf_head(ies), wpabuf_len(ies));
533 params->extra_ies = n;
534 params->extra_ies_len = wpabuf_len(ies);
535 wpabuf_free(ies);
536
Dmitry Shmidtbd14a572014-02-18 10:33:49 -0800537 radio_remove_works(wpa_s, "p2p-scan", 0);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800538 if (radio_add_work(wpa_s, 0, "p2p-scan", 0, wpas_p2p_trigger_scan_cb,
539 params) < 0)
540 goto fail;
541 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700542
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800543fail:
544 wpa_scan_free_params(params);
545 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700546}
547
548
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700549static enum wpa_driver_if_type wpas_p2p_if_type(int p2p_group_interface)
550{
551 switch (p2p_group_interface) {
552 case P2P_GROUP_INTERFACE_PENDING:
553 return WPA_IF_P2P_GROUP;
554 case P2P_GROUP_INTERFACE_GO:
555 return WPA_IF_P2P_GO;
556 case P2P_GROUP_INTERFACE_CLIENT:
557 return WPA_IF_P2P_CLIENT;
Sunil Ravi77d572f2023-01-17 23:58:31 +0000558 default:
559 return WPA_IF_P2P_GROUP;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700560 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700561}
562
563
564static struct wpa_supplicant * wpas_get_p2p_group(struct wpa_supplicant *wpa_s,
565 const u8 *ssid,
566 size_t ssid_len, int *go)
567{
568 struct wpa_ssid *s;
569
570 for (wpa_s = wpa_s->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
571 for (s = wpa_s->conf->ssid; s; s = s->next) {
572 if (s->disabled != 0 || !s->p2p_group ||
573 s->ssid_len != ssid_len ||
574 os_memcmp(ssid, s->ssid, ssid_len) != 0)
575 continue;
576 if (s->mode == WPAS_MODE_P2P_GO &&
577 s != wpa_s->current_ssid)
578 continue;
579 if (go)
580 *go = s->mode == WPAS_MODE_P2P_GO;
581 return wpa_s;
582 }
583 }
584
585 return NULL;
586}
587
588
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800589static void run_wpas_p2p_disconnect(void *eloop_ctx, void *timeout_ctx)
590{
591 struct wpa_supplicant *wpa_s = eloop_ctx;
592 wpa_printf(MSG_DEBUG,
593 "P2P: Complete previously requested removal of %s",
594 wpa_s->ifname);
595 wpas_p2p_disconnect(wpa_s);
596}
597
598
599static int wpas_p2p_disconnect_safely(struct wpa_supplicant *wpa_s,
600 struct wpa_supplicant *calling_wpa_s)
601{
602 if (calling_wpa_s == wpa_s && wpa_s &&
603 wpa_s->p2p_group_interface != NOT_P2P_GROUP_INTERFACE) {
604 /*
605 * The calling wpa_s instance is going to be removed. Do that
606 * from an eloop callback to keep the instance available until
Hai Shalom899fcc72020-10-19 14:38:18 -0700607 * the caller has returned. This may be needed, e.g., to provide
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800608 * control interface responses on the per-interface socket.
609 */
610 if (eloop_register_timeout(0, 0, run_wpas_p2p_disconnect,
611 wpa_s, NULL) < 0)
612 return -1;
613 return 0;
614 }
615
616 return wpas_p2p_disconnect(wpa_s);
617}
618
619
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800620/* Determine total number of clients in active groups where we are the GO */
621static unsigned int p2p_group_go_member_count(struct wpa_supplicant *wpa_s)
622{
623 unsigned int count = 0;
624 struct wpa_ssid *s;
625
626 for (wpa_s = wpa_s->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
627 for (s = wpa_s->conf->ssid; s; s = s->next) {
628 wpa_printf(MSG_DEBUG,
629 "P2P: sup:%p ssid:%p disabled:%d p2p:%d mode:%d",
630 wpa_s, s, s->disabled, s->p2p_group,
631 s->mode);
632 if (!s->disabled && s->p2p_group &&
633 s->mode == WPAS_MODE_P2P_GO) {
634 count += p2p_get_group_num_members(
635 wpa_s->p2p_group);
636 }
637 }
638 }
639
640 return count;
641}
642
643
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800644static unsigned int p2p_is_active_persistent_group(struct wpa_supplicant *wpa_s)
645{
646 return !wpa_s->p2p_mgmt && wpa_s->current_ssid &&
647 !wpa_s->current_ssid->disabled &&
648 wpa_s->current_ssid->p2p_group &&
649 wpa_s->current_ssid->p2p_persistent_group;
650}
651
652
653static unsigned int p2p_is_active_persistent_go(struct wpa_supplicant *wpa_s)
654{
655 return p2p_is_active_persistent_group(wpa_s) &&
656 wpa_s->current_ssid->mode == WPAS_MODE_P2P_GO;
657}
658
659
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800660/* Find an interface for a P2P group where we are the GO */
661static struct wpa_supplicant *
662wpas_p2p_get_go_group(struct wpa_supplicant *wpa_s)
663{
664 struct wpa_supplicant *save = NULL;
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800665
666 if (!wpa_s)
667 return NULL;
668
669 for (wpa_s = wpa_s->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800670 if (!p2p_is_active_persistent_go(wpa_s))
671 continue;
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800672
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800673 /* Prefer a group with connected clients */
674 if (p2p_get_group_num_members(wpa_s->p2p_group))
675 return wpa_s;
676 save = wpa_s;
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800677 }
678
679 /* No group with connected clients, so pick the one without (if any) */
680 return save;
681}
682
683
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800684static unsigned int p2p_is_active_persistent_cli(struct wpa_supplicant *wpa_s)
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800685{
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800686 return p2p_is_active_persistent_group(wpa_s) &&
687 wpa_s->current_ssid->mode == WPAS_MODE_INFRA;
688}
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800689
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800690
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800691/* Find an interface for a P2P group where we are the P2P Client */
692static struct wpa_supplicant *
693wpas_p2p_get_cli_group(struct wpa_supplicant *wpa_s)
694{
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800695 for (wpa_s = wpa_s->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800696 if (p2p_is_active_persistent_cli(wpa_s))
697 return wpa_s;
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800698 }
699
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800700 return NULL;
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800701}
702
703
704/* Find a persistent group where we are the GO */
705static struct wpa_ssid *
706wpas_p2p_get_persistent_go(struct wpa_supplicant *wpa_s)
707{
708 struct wpa_ssid *s;
709
710 for (s = wpa_s->conf->ssid; s; s = s->next) {
711 if (s->disabled == 2 && s->mode == WPAS_MODE_P2P_GO)
712 return s;
713 }
714
715 return NULL;
716}
717
718
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800719static u8 p2ps_group_capability(void *ctx, u8 incoming, u8 role,
720 unsigned int *force_freq,
721 unsigned int *pref_freq)
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800722{
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800723 struct wpa_supplicant *wpa_s = ctx;
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800724 struct wpa_ssid *s;
725 u8 conncap = P2PS_SETUP_NONE;
726 unsigned int owned_members = 0;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800727 struct wpa_supplicant *go_wpa_s, *cli_wpa_s;
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800728 struct wpa_ssid *persistent_go;
729 int p2p_no_group_iface;
Sunil8cd6f4d2022-06-28 18:40:46 +0000730 struct weighted_pcl pref_freq_list[P2P_MAX_PREF_CHANNELS];
731 unsigned int size;
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800732
733 wpa_printf(MSG_DEBUG, "P2P: Conncap - in:%d role:%d", incoming, role);
734
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800735 if (force_freq)
736 *force_freq = 0;
737 if (pref_freq)
738 *pref_freq = 0;
739
740 size = P2P_MAX_PREF_CHANNELS;
741 if (force_freq && pref_freq &&
742 !wpas_p2p_setup_freqs(wpa_s, 0, (int *) force_freq,
743 (int *) pref_freq, 0, pref_freq_list, &size))
744 wpas_p2p_set_own_freq_preference(wpa_s,
745 *force_freq ? *force_freq :
746 *pref_freq);
747
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800748 /*
749 * For non-concurrent capable devices:
750 * If persistent_go, then no new.
751 * If GO, then no client.
752 * If client, then no GO.
753 */
754 go_wpa_s = wpas_p2p_get_go_group(wpa_s);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800755 if (go_wpa_s)
756 owned_members = p2p_get_group_num_members(go_wpa_s->p2p_group);
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800757 persistent_go = wpas_p2p_get_persistent_go(wpa_s);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800758 p2p_no_group_iface = !wpas_p2p_create_iface(wpa_s);
759 cli_wpa_s = wpas_p2p_get_cli_group(wpa_s);
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800760
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800761 wpa_printf(MSG_DEBUG,
762 "P2P: GO(iface)=%p members=%u CLI(iface)=%p persistent(ssid)=%p",
763 go_wpa_s, owned_members, cli_wpa_s, persistent_go);
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800764
765 /* If not concurrent, restrict our choices */
766 if (p2p_no_group_iface) {
767 wpa_printf(MSG_DEBUG, "P2P: p2p_no_group_iface");
768
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800769 if (cli_wpa_s)
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800770 return P2PS_SETUP_NONE;
771
772 if (go_wpa_s) {
773 if (role == P2PS_SETUP_CLIENT ||
774 incoming == P2PS_SETUP_GROUP_OWNER ||
775 p2p_client_limit_reached(go_wpa_s->p2p_group))
776 return P2PS_SETUP_NONE;
777
778 return P2PS_SETUP_GROUP_OWNER;
779 }
780
781 if (persistent_go) {
782 if (role == P2PS_SETUP_NONE || role == P2PS_SETUP_NEW) {
783 if (!incoming)
784 return P2PS_SETUP_GROUP_OWNER |
785 P2PS_SETUP_CLIENT;
786 if (incoming == P2PS_SETUP_NEW) {
787 u8 r;
788
789 if (os_get_random(&r, sizeof(r)) < 0 ||
790 (r & 1))
791 return P2PS_SETUP_CLIENT;
792 return P2PS_SETUP_GROUP_OWNER;
793 }
794 }
795 }
796 }
797
798 /* If a required role has been specified, handle it here */
799 if (role && role != P2PS_SETUP_NEW) {
800 switch (incoming) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800801 case P2PS_SETUP_GROUP_OWNER | P2PS_SETUP_NEW:
802 case P2PS_SETUP_GROUP_OWNER | P2PS_SETUP_CLIENT:
803 /*
804 * Peer has an active GO, so if the role allows it and
805 * we do not have any active roles, become client.
806 */
807 if ((role & P2PS_SETUP_CLIENT) && !go_wpa_s &&
808 !cli_wpa_s)
809 return P2PS_SETUP_CLIENT;
810
811 /* fall through */
812
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800813 case P2PS_SETUP_NONE:
814 case P2PS_SETUP_NEW:
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800815 conncap = role;
816 goto grp_owner;
817
818 case P2PS_SETUP_GROUP_OWNER:
819 /*
820 * Must be a complimentary role - cannot be a client to
821 * more than one peer.
822 */
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800823 if (incoming == role || cli_wpa_s)
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800824 return P2PS_SETUP_NONE;
825
826 return P2PS_SETUP_CLIENT;
827
828 case P2PS_SETUP_CLIENT:
829 /* Must be a complimentary role */
830 if (incoming != role) {
831 conncap = P2PS_SETUP_GROUP_OWNER;
832 goto grp_owner;
833 }
Roshan Pius3a1667e2018-07-03 15:17:14 -0700834 /* fall through */
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800835
836 default:
837 return P2PS_SETUP_NONE;
838 }
839 }
840
841 /*
842 * For now, we only will support ownership of one group, and being a
843 * client of one group. Therefore, if we have either an existing GO
844 * group, or an existing client group, we will not do a new GO
845 * negotiation, but rather try to re-use the existing groups.
846 */
847 switch (incoming) {
848 case P2PS_SETUP_NONE:
849 case P2PS_SETUP_NEW:
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800850 if (cli_wpa_s)
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800851 conncap = P2PS_SETUP_GROUP_OWNER;
852 else if (!owned_members)
853 conncap = P2PS_SETUP_NEW;
854 else if (incoming == P2PS_SETUP_NONE)
855 conncap = P2PS_SETUP_GROUP_OWNER | P2PS_SETUP_CLIENT;
856 else
857 conncap = P2PS_SETUP_CLIENT;
858 break;
859
860 case P2PS_SETUP_CLIENT:
861 conncap = P2PS_SETUP_GROUP_OWNER;
862 break;
863
864 case P2PS_SETUP_GROUP_OWNER:
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800865 if (!cli_wpa_s)
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800866 conncap = P2PS_SETUP_CLIENT;
867 break;
868
869 case P2PS_SETUP_GROUP_OWNER | P2PS_SETUP_NEW:
870 case P2PS_SETUP_GROUP_OWNER | P2PS_SETUP_CLIENT:
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800871 if (cli_wpa_s)
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800872 conncap = P2PS_SETUP_GROUP_OWNER;
873 else {
874 u8 r;
875
876 if (os_get_random(&r, sizeof(r)) < 0 ||
877 (r & 1))
878 conncap = P2PS_SETUP_CLIENT;
879 else
880 conncap = P2PS_SETUP_GROUP_OWNER;
881 }
882 break;
883
884 default:
885 return P2PS_SETUP_NONE;
886 }
887
888grp_owner:
889 if ((conncap & P2PS_SETUP_GROUP_OWNER) ||
890 (!incoming && (conncap & P2PS_SETUP_NEW))) {
891 if (go_wpa_s && p2p_client_limit_reached(go_wpa_s->p2p_group))
892 conncap &= ~P2PS_SETUP_GROUP_OWNER;
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800893
894 s = wpas_p2p_get_persistent_go(wpa_s);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800895 if (!s && !go_wpa_s && p2p_no_group_iface) {
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800896 p2p_set_intended_addr(wpa_s->global->p2p,
Dmitry Shmidt9c175262016-03-03 10:20:07 -0800897 wpa_s->p2p_mgmt ?
898 wpa_s->parent->own_addr :
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800899 wpa_s->own_addr);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800900 } else if (!s && !go_wpa_s) {
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800901 if (wpas_p2p_add_group_interface(wpa_s,
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -0800902 WPA_IF_P2P_GROUP) < 0) {
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800903 wpa_printf(MSG_ERROR,
904 "P2P: Failed to allocate a new interface for the group");
905 return P2PS_SETUP_NONE;
906 }
907 wpa_s->global->pending_group_iface_for_p2ps = 1;
908 p2p_set_intended_addr(wpa_s->global->p2p,
909 wpa_s->pending_interface_addr);
910 }
911 }
912
913 return conncap;
914}
915
916
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700917static int wpas_p2p_group_delete(struct wpa_supplicant *wpa_s,
918 enum p2p_group_removal_reason removal_reason)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700919{
920 struct wpa_ssid *ssid;
921 char *gtype;
922 const char *reason;
923
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700924 ssid = wpa_s->current_ssid;
925 if (ssid == NULL) {
926 /*
927 * The current SSID was not known, but there may still be a
Dmitry Shmidtaa532512012-09-24 10:35:31 -0700928 * pending P2P group interface waiting for provisioning or a
929 * P2P group that is trying to reconnect.
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700930 */
931 ssid = wpa_s->conf->ssid;
932 while (ssid) {
Jouni Malinen9d712832012-10-05 11:01:57 -0700933 if (ssid->p2p_group && ssid->disabled != 2)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700934 break;
935 ssid = ssid->next;
936 }
Jouni Malinen5c44edb2012-08-31 21:35:32 +0300937 if (ssid == NULL &&
938 wpa_s->p2p_group_interface == NOT_P2P_GROUP_INTERFACE)
939 {
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700940 wpa_printf(MSG_ERROR, "P2P: P2P group interface "
941 "not found");
942 return -1;
943 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700944 }
945 if (wpa_s->p2p_group_interface == P2P_GROUP_INTERFACE_GO)
946 gtype = "GO";
947 else if (wpa_s->p2p_group_interface == P2P_GROUP_INTERFACE_CLIENT ||
948 (ssid && ssid->mode == WPAS_MODE_INFRA)) {
949 wpa_s->reassociate = 0;
950 wpa_s->disconnected = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700951 gtype = "client";
952 } else
953 gtype = "GO";
Dmitry Shmidt661b4f72014-09-29 14:58:27 -0700954
955 if (removal_reason != P2P_GROUP_REMOVAL_SILENT && ssid)
956 wpas_notify_p2p_group_removed(wpa_s, ssid, gtype);
957
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800958 if (os_strcmp(gtype, "client") == 0) {
Dmitry Shmidt661b4f72014-09-29 14:58:27 -0700959 wpa_supplicant_deauthenticate(wpa_s, WLAN_REASON_DEAUTH_LEAVING);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800960 if (eloop_is_timeout_registered(wpas_p2p_psk_failure_removal,
961 wpa_s, NULL)) {
962 wpa_printf(MSG_DEBUG,
963 "P2P: PSK failure removal was scheduled, so use PSK failure as reason for group removal");
964 removal_reason = P2P_GROUP_REMOVAL_PSK_FAILURE;
965 eloop_cancel_timeout(wpas_p2p_psk_failure_removal,
966 wpa_s, NULL);
967 }
968 }
Dmitry Shmidt661b4f72014-09-29 14:58:27 -0700969
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700970 if (wpa_s->cross_connect_in_use) {
971 wpa_s->cross_connect_in_use = 0;
Dmitry Shmidt9c175262016-03-03 10:20:07 -0800972 wpa_msg_global(wpa_s->p2pdev, MSG_INFO,
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -0700973 P2P_EVENT_CROSS_CONNECT_DISABLE "%s %s",
974 wpa_s->ifname, wpa_s->cross_connect_uplink);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700975 }
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700976 switch (removal_reason) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700977 case P2P_GROUP_REMOVAL_REQUESTED:
978 reason = " reason=REQUESTED";
979 break;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700980 case P2P_GROUP_REMOVAL_FORMATION_FAILED:
981 reason = " reason=FORMATION_FAILED";
982 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700983 case P2P_GROUP_REMOVAL_IDLE_TIMEOUT:
984 reason = " reason=IDLE";
985 break;
986 case P2P_GROUP_REMOVAL_UNAVAILABLE:
987 reason = " reason=UNAVAILABLE";
988 break;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700989 case P2P_GROUP_REMOVAL_GO_ENDING_SESSION:
990 reason = " reason=GO_ENDING_SESSION";
991 break;
Dmitry Shmidt391c59f2013-09-03 12:16:28 -0700992 case P2P_GROUP_REMOVAL_PSK_FAILURE:
993 reason = " reason=PSK_FAILURE";
994 break;
Dmitry Shmidt04f534e2013-12-09 15:50:16 -0800995 case P2P_GROUP_REMOVAL_FREQ_CONFLICT:
996 reason = " reason=FREQ_CONFLICT";
997 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700998 default:
999 reason = "";
1000 break;
1001 }
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001002 if (removal_reason != P2P_GROUP_REMOVAL_SILENT) {
Dmitry Shmidt9c175262016-03-03 10:20:07 -08001003 wpa_msg_global(wpa_s->p2pdev, MSG_INFO,
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07001004 P2P_EVENT_GROUP_REMOVED "%s %s%s",
1005 wpa_s->ifname, gtype, reason);
Dmitry Shmidt04949592012-07-19 12:16:46 -07001006 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001007
Dmitry Shmidt04f534e2013-12-09 15:50:16 -08001008 if (eloop_cancel_timeout(wpas_p2p_group_freq_conflict, wpa_s, NULL) > 0)
1009 wpa_printf(MSG_DEBUG, "P2P: Cancelled P2P group freq_conflict timeout");
Dmitry Shmidt04949592012-07-19 12:16:46 -07001010 if (eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL) > 0)
1011 wpa_printf(MSG_DEBUG, "P2P: Cancelled P2P group idle timeout");
Dmitry Shmidtf8623282013-02-20 14:34:59 -08001012 if (eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
Dmitry Shmidt9c175262016-03-03 10:20:07 -08001013 wpa_s->p2pdev, NULL) > 0) {
Dmitry Shmidtf8623282013-02-20 14:34:59 -08001014 wpa_printf(MSG_DEBUG, "P2P: Cancelled P2P group formation "
1015 "timeout");
Dmitry Shmidt2f023192013-03-12 12:44:17 -07001016 wpa_s->p2p_in_provisioning = 0;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001017 wpas_p2p_group_formation_failed(wpa_s, 1);
Dmitry Shmidt2f023192013-03-12 12:44:17 -07001018 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07001019
Dmitry Shmidt15907092014-03-25 10:42:57 -07001020 wpa_s->p2p_in_invitation = 0;
Matthew Wang06b42472022-11-10 06:56:31 +00001021 wpa_s->p2p_retry_limit = 0;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001022 eloop_cancel_timeout(wpas_p2p_move_go, wpa_s, NULL);
1023 eloop_cancel_timeout(wpas_p2p_reconsider_moving_go, wpa_s, NULL);
Dmitry Shmidt15907092014-03-25 10:42:57 -07001024
Dmitry Shmidt96571392013-10-14 12:54:46 -07001025 /*
1026 * Make sure wait for the first client does not remain active after the
1027 * group has been removed.
1028 */
1029 wpa_s->global->p2p_go_wait_client.sec = 0;
1030
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001031 if (wpa_s->p2p_group_interface != NOT_P2P_GROUP_INTERFACE) {
1032 struct wpa_global *global;
1033 char *ifname;
1034 enum wpa_driver_if_type type;
1035 wpa_printf(MSG_DEBUG, "P2P: Remove group interface %s",
1036 wpa_s->ifname);
1037 global = wpa_s->global;
1038 ifname = os_strdup(wpa_s->ifname);
1039 type = wpas_p2p_if_type(wpa_s->p2p_group_interface);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001040 eloop_cancel_timeout(run_wpas_p2p_disconnect, wpa_s, NULL);
Dmitry Shmidte15c7b52011-08-03 15:04:35 -07001041 wpa_supplicant_remove_iface(wpa_s->global, wpa_s, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001042 wpa_s = global->ifaces;
1043 if (wpa_s && ifname)
1044 wpa_drv_if_remove(wpa_s, type, ifname);
1045 os_free(ifname);
Jouni Malinen2b89da82012-08-31 22:04:41 +03001046 return 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001047 }
1048
Dmitry Shmidt9c175262016-03-03 10:20:07 -08001049 /*
1050 * The primary interface was used for P2P group operations, so
1051 * need to reset its p2pdev.
1052 */
1053 wpa_s->p2pdev = wpa_s->parent;
1054
Dmitry Shmidt96571392013-10-14 12:54:46 -07001055 if (!wpa_s->p2p_go_group_formation_completed) {
1056 wpa_s->global->p2p_group_formation = NULL;
1057 wpa_s->p2p_in_provisioning = 0;
1058 }
1059
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001060 wpa_s->show_group_started = 0;
1061 os_free(wpa_s->go_params);
1062 wpa_s->go_params = NULL;
1063
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001064 os_free(wpa_s->p2p_group_common_freqs);
1065 wpa_s->p2p_group_common_freqs = NULL;
1066 wpa_s->p2p_group_common_freqs_num = 0;
Hai Shalom021b0b52019-04-10 11:17:58 -07001067 wpa_s->p2p_go_do_acs = 0;
Hai Shalomc1a21442022-02-04 13:43:00 -08001068 wpa_s->p2p_go_allow_dfs = 0;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001069
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001070 wpa_s->waiting_presence_resp = 0;
1071
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001072 wpa_printf(MSG_DEBUG, "P2P: Remove temporary group network");
1073 if (ssid && (ssid->p2p_group ||
1074 ssid->mode == WPAS_MODE_P2P_GROUP_FORMATION ||
1075 (ssid->key_mgmt & WPA_KEY_MGMT_WPS))) {
1076 int id = ssid->id;
Jouni Malinen75ecf522011-06-27 15:19:46 -07001077 if (ssid == wpa_s->current_ssid) {
1078 wpa_sm_set_config(wpa_s->wpa, NULL);
1079 eapol_sm_notify_config(wpa_s->eapol, NULL, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001080 wpa_s->current_ssid = NULL;
Jouni Malinen75ecf522011-06-27 15:19:46 -07001081 }
1082 /*
1083 * Networks objects created during any P2P activities are not
1084 * exposed out as they might/will confuse certain non-P2P aware
1085 * applications since these network objects won't behave like
1086 * regular ones.
1087 *
1088 * Likewise, we don't send out network removed signals for such
1089 * network objects.
1090 */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001091 wpa_config_remove_network(wpa_s->conf, id);
1092 wpa_supplicant_clear_status(wpa_s);
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08001093 wpa_supplicant_cancel_sched_scan(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001094 } else {
1095 wpa_printf(MSG_DEBUG, "P2P: Temporary group network not "
1096 "found");
1097 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07001098 if (wpa_s->ap_iface)
1099 wpa_supplicant_ap_deinit(wpa_s);
1100 else
1101 wpa_drv_deinit_p2p_cli(wpa_s);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001102
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001103 os_memset(wpa_s->go_dev_addr, 0, ETH_ALEN);
1104
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001105 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001106}
1107
1108
1109static int wpas_p2p_persistent_group(struct wpa_supplicant *wpa_s,
1110 u8 *go_dev_addr,
1111 const u8 *ssid, size_t ssid_len)
1112{
1113 struct wpa_bss *bss;
1114 const u8 *bssid;
1115 struct wpabuf *p2p;
1116 u8 group_capab;
1117 const u8 *addr;
1118
1119 if (wpa_s->go_params)
1120 bssid = wpa_s->go_params->peer_interface_addr;
1121 else
1122 bssid = wpa_s->bssid;
1123
1124 bss = wpa_bss_get(wpa_s, bssid, ssid, ssid_len);
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001125 if (bss == NULL && wpa_s->go_params &&
1126 !is_zero_ether_addr(wpa_s->go_params->peer_device_addr))
1127 bss = wpa_bss_get_p2p_dev_addr(
1128 wpa_s, wpa_s->go_params->peer_device_addr);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001129 if (bss == NULL) {
1130 u8 iface_addr[ETH_ALEN];
1131 if (p2p_get_interface_addr(wpa_s->global->p2p, bssid,
1132 iface_addr) == 0)
1133 bss = wpa_bss_get(wpa_s, iface_addr, ssid, ssid_len);
1134 }
1135 if (bss == NULL) {
1136 wpa_printf(MSG_DEBUG, "P2P: Could not figure out whether "
1137 "group is persistent - BSS " MACSTR " not found",
1138 MAC2STR(bssid));
1139 return 0;
1140 }
1141
1142 p2p = wpa_bss_get_vendor_ie_multi(bss, P2P_IE_VENDOR_TYPE);
Dmitry Shmidt96571392013-10-14 12:54:46 -07001143 if (p2p == NULL)
1144 p2p = wpa_bss_get_vendor_ie_multi_beacon(bss,
1145 P2P_IE_VENDOR_TYPE);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001146 if (p2p == NULL) {
1147 wpa_printf(MSG_DEBUG, "P2P: Could not figure out whether "
1148 "group is persistent - BSS " MACSTR
1149 " did not include P2P IE", MAC2STR(bssid));
1150 wpa_hexdump(MSG_DEBUG, "P2P: Probe Response IEs",
Hai Shalom60840252021-02-19 19:02:11 -08001151 wpa_bss_ie_ptr(bss), bss->ie_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001152 wpa_hexdump(MSG_DEBUG, "P2P: Beacon IEs",
Hai Shalom60840252021-02-19 19:02:11 -08001153 wpa_bss_ie_ptr(bss) + bss->ie_len,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001154 bss->beacon_ie_len);
1155 return 0;
1156 }
1157
1158 group_capab = p2p_get_group_capab(p2p);
1159 addr = p2p_get_go_dev_addr(p2p);
1160 wpa_printf(MSG_DEBUG, "P2P: Checking whether group is persistent: "
1161 "group_capab=0x%x", group_capab);
1162 if (addr) {
1163 os_memcpy(go_dev_addr, addr, ETH_ALEN);
1164 wpa_printf(MSG_DEBUG, "P2P: GO Device Address " MACSTR,
1165 MAC2STR(addr));
1166 } else
1167 os_memset(go_dev_addr, 0, ETH_ALEN);
1168 wpabuf_free(p2p);
1169
1170 wpa_printf(MSG_DEBUG, "P2P: BSS " MACSTR " group_capab=0x%x "
1171 "go_dev_addr=" MACSTR,
1172 MAC2STR(bssid), group_capab, MAC2STR(go_dev_addr));
1173
Dmitry Shmidt7f2c7532016-08-15 09:48:12 -07001174 return !!(group_capab & P2P_GROUP_CAPAB_PERSISTENT_GROUP);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001175}
1176
1177
Jouni Malinen75ecf522011-06-27 15:19:46 -07001178static int wpas_p2p_store_persistent_group(struct wpa_supplicant *wpa_s,
1179 struct wpa_ssid *ssid,
1180 const u8 *go_dev_addr)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001181{
1182 struct wpa_ssid *s;
1183 int changed = 0;
1184
1185 wpa_printf(MSG_DEBUG, "P2P: Storing credentials for a persistent "
1186 "group (GO Dev Addr " MACSTR ")", MAC2STR(go_dev_addr));
1187 for (s = wpa_s->conf->ssid; s; s = s->next) {
1188 if (s->disabled == 2 &&
1189 os_memcmp(go_dev_addr, s->bssid, ETH_ALEN) == 0 &&
1190 s->ssid_len == ssid->ssid_len &&
1191 os_memcmp(ssid->ssid, s->ssid, ssid->ssid_len) == 0)
1192 break;
1193 }
1194
1195 if (s) {
1196 wpa_printf(MSG_DEBUG, "P2P: Update existing persistent group "
1197 "entry");
1198 if (ssid->passphrase && !s->passphrase)
1199 changed = 1;
1200 else if (ssid->passphrase && s->passphrase &&
1201 os_strcmp(ssid->passphrase, s->passphrase) != 0)
1202 changed = 1;
1203 } else {
1204 wpa_printf(MSG_DEBUG, "P2P: Create a new persistent group "
1205 "entry");
1206 changed = 1;
1207 s = wpa_config_add_network(wpa_s->conf);
1208 if (s == NULL)
Jouni Malinen75ecf522011-06-27 15:19:46 -07001209 return -1;
1210
1211 /*
1212 * Instead of network_added we emit persistent_group_added
1213 * notification. Also to keep the defense checks in
1214 * persistent_group obj registration method, we set the
1215 * relevant flags in s to designate it as a persistent group.
1216 */
1217 s->p2p_group = 1;
1218 s->p2p_persistent_group = 1;
1219 wpas_notify_persistent_group_added(wpa_s, s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001220 wpa_config_set_network_defaults(s);
1221 }
1222
1223 s->p2p_group = 1;
1224 s->p2p_persistent_group = 1;
1225 s->disabled = 2;
1226 s->bssid_set = 1;
1227 os_memcpy(s->bssid, go_dev_addr, ETH_ALEN);
1228 s->mode = ssid->mode;
1229 s->auth_alg = WPA_AUTH_ALG_OPEN;
1230 s->key_mgmt = WPA_KEY_MGMT_PSK;
1231 s->proto = WPA_PROTO_RSN;
Dmitry Shmidte4663042016-04-04 10:07:49 -07001232 s->pbss = ssid->pbss;
1233 s->pairwise_cipher = ssid->pbss ? WPA_CIPHER_GCMP : WPA_CIPHER_CCMP;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001234 s->export_keys = 1;
1235 if (ssid->passphrase) {
1236 os_free(s->passphrase);
1237 s->passphrase = os_strdup(ssid->passphrase);
1238 }
1239 if (ssid->psk_set) {
1240 s->psk_set = 1;
1241 os_memcpy(s->psk, ssid->psk, 32);
1242 }
1243 if (s->passphrase && !s->psk_set)
1244 wpa_config_update_psk(s);
1245 if (s->ssid == NULL || s->ssid_len < ssid->ssid_len) {
1246 os_free(s->ssid);
1247 s->ssid = os_malloc(ssid->ssid_len);
1248 }
1249 if (s->ssid) {
1250 s->ssid_len = ssid->ssid_len;
1251 os_memcpy(s->ssid, ssid->ssid, s->ssid_len);
1252 }
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07001253 if (ssid->mode == WPAS_MODE_P2P_GO && wpa_s->global->add_psk) {
1254 dl_list_add(&s->psk_list, &wpa_s->global->add_psk->list);
1255 wpa_s->global->add_psk = NULL;
1256 changed = 1;
1257 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001258
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001259 if (changed && wpa_s->conf->update_config &&
1260 wpa_config_write(wpa_s->confname, wpa_s->conf)) {
1261 wpa_printf(MSG_DEBUG, "P2P: Failed to update configuration");
1262 }
Jouni Malinen75ecf522011-06-27 15:19:46 -07001263
1264 return s->id;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001265}
1266
1267
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001268static void wpas_p2p_add_persistent_group_client(struct wpa_supplicant *wpa_s,
1269 const u8 *addr)
1270{
1271 struct wpa_ssid *ssid, *s;
1272 u8 *n;
1273 size_t i;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001274 int found = 0;
Dmitry Shmidt8bd70b72015-05-26 16:02:19 -07001275 struct wpa_supplicant *p2p_wpa_s = wpa_s->global->p2p_init_wpa_s;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001276
1277 ssid = wpa_s->current_ssid;
1278 if (ssid == NULL || ssid->mode != WPAS_MODE_P2P_GO ||
1279 !ssid->p2p_persistent_group)
1280 return;
1281
Dmitry Shmidt8bd70b72015-05-26 16:02:19 -07001282 for (s = p2p_wpa_s->conf->ssid; s; s = s->next) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001283 if (s->disabled != 2 || s->mode != WPAS_MODE_P2P_GO)
1284 continue;
1285
1286 if (s->ssid_len == ssid->ssid_len &&
1287 os_memcmp(s->ssid, ssid->ssid, s->ssid_len) == 0)
1288 break;
1289 }
1290
1291 if (s == NULL)
1292 return;
1293
1294 for (i = 0; s->p2p_client_list && i < s->num_p2p_clients; i++) {
Dmitry Shmidtff787d52015-01-12 13:01:47 -08001295 if (os_memcmp(s->p2p_client_list + i * 2 * ETH_ALEN, addr,
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001296 ETH_ALEN) != 0)
1297 continue;
1298
1299 if (i == s->num_p2p_clients - 1)
1300 return; /* already the most recent entry */
1301
1302 /* move the entry to mark it most recent */
Dmitry Shmidtff787d52015-01-12 13:01:47 -08001303 os_memmove(s->p2p_client_list + i * 2 * ETH_ALEN,
1304 s->p2p_client_list + (i + 1) * 2 * ETH_ALEN,
1305 (s->num_p2p_clients - i - 1) * 2 * ETH_ALEN);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001306 os_memcpy(s->p2p_client_list +
Dmitry Shmidtff787d52015-01-12 13:01:47 -08001307 (s->num_p2p_clients - 1) * 2 * ETH_ALEN, addr,
1308 ETH_ALEN);
1309 os_memset(s->p2p_client_list +
1310 (s->num_p2p_clients - 1) * 2 * ETH_ALEN + ETH_ALEN,
1311 0xff, ETH_ALEN);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001312 found = 1;
1313 break;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001314 }
1315
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001316 if (!found && s->num_p2p_clients < P2P_MAX_STORED_CLIENTS) {
1317 n = os_realloc_array(s->p2p_client_list,
Dmitry Shmidtff787d52015-01-12 13:01:47 -08001318 s->num_p2p_clients + 1, 2 * ETH_ALEN);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001319 if (n == NULL)
1320 return;
Dmitry Shmidtff787d52015-01-12 13:01:47 -08001321 os_memcpy(n + s->num_p2p_clients * 2 * ETH_ALEN, addr,
1322 ETH_ALEN);
1323 os_memset(n + s->num_p2p_clients * 2 * ETH_ALEN + ETH_ALEN,
1324 0xff, ETH_ALEN);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001325 s->p2p_client_list = n;
1326 s->num_p2p_clients++;
Dmitry Shmidt6aa8ae42014-07-07 09:31:33 -07001327 } else if (!found && s->p2p_client_list) {
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001328 /* Not enough room for an additional entry - drop the oldest
1329 * entry */
1330 os_memmove(s->p2p_client_list,
Dmitry Shmidtff787d52015-01-12 13:01:47 -08001331 s->p2p_client_list + 2 * ETH_ALEN,
1332 (s->num_p2p_clients - 1) * 2 * ETH_ALEN);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001333 os_memcpy(s->p2p_client_list +
Dmitry Shmidtff787d52015-01-12 13:01:47 -08001334 (s->num_p2p_clients - 1) * 2 * ETH_ALEN,
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001335 addr, ETH_ALEN);
Dmitry Shmidtff787d52015-01-12 13:01:47 -08001336 os_memset(s->p2p_client_list +
1337 (s->num_p2p_clients - 1) * 2 * ETH_ALEN + ETH_ALEN,
1338 0xff, ETH_ALEN);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001339 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001340
Dmitry Shmidt8bd70b72015-05-26 16:02:19 -07001341 if (p2p_wpa_s->conf->update_config &&
1342 wpa_config_write(p2p_wpa_s->confname, p2p_wpa_s->conf))
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001343 wpa_printf(MSG_DEBUG, "P2P: Failed to update configuration");
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001344}
1345
1346
Dmitry Shmidt7f0b69e2014-07-28 10:35:20 -07001347static void wpas_p2p_group_started(struct wpa_supplicant *wpa_s,
1348 int go, struct wpa_ssid *ssid, int freq,
1349 const u8 *psk, const char *passphrase,
1350 const u8 *go_dev_addr, int persistent,
1351 const char *extra)
1352{
1353 const char *ssid_txt;
1354 char psk_txt[65];
1355
1356 if (psk)
1357 wpa_snprintf_hex(psk_txt, sizeof(psk_txt), psk, 32);
1358 else
1359 psk_txt[0] = '\0';
1360
1361 if (ssid)
1362 ssid_txt = wpa_ssid_txt(ssid->ssid, ssid->ssid_len);
1363 else
1364 ssid_txt = "";
1365
1366 if (passphrase && passphrase[0] == '\0')
1367 passphrase = NULL;
1368
1369 /*
1370 * Include PSK/passphrase only in the control interface message and
1371 * leave it out from the debug log entry.
1372 */
Dmitry Shmidt9c175262016-03-03 10:20:07 -08001373 wpa_msg_global_ctrl(wpa_s->p2pdev, MSG_INFO,
Dmitry Shmidt7f0b69e2014-07-28 10:35:20 -07001374 P2P_EVENT_GROUP_STARTED
1375 "%s %s ssid=\"%s\" freq=%d%s%s%s%s%s go_dev_addr="
1376 MACSTR "%s%s",
1377 wpa_s->ifname, go ? "GO" : "client", ssid_txt, freq,
1378 psk ? " psk=" : "", psk_txt,
1379 passphrase ? " passphrase=\"" : "",
1380 passphrase ? passphrase : "",
1381 passphrase ? "\"" : "",
1382 MAC2STR(go_dev_addr),
1383 persistent ? " [PERSISTENT]" : "", extra);
1384 wpa_printf(MSG_INFO, P2P_EVENT_GROUP_STARTED
1385 "%s %s ssid=\"%s\" freq=%d go_dev_addr=" MACSTR "%s%s",
1386 wpa_s->ifname, go ? "GO" : "client", ssid_txt, freq,
1387 MAC2STR(go_dev_addr), persistent ? " [PERSISTENT]" : "",
1388 extra);
1389}
1390
1391
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001392static void wpas_group_formation_completed(struct wpa_supplicant *wpa_s,
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07001393 int success, int already_deleted)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001394{
1395 struct wpa_ssid *ssid;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001396 int client;
1397 int persistent;
1398 u8 go_dev_addr[ETH_ALEN];
1399
1400 /*
1401 * This callback is likely called for the main interface. Update wpa_s
1402 * to use the group interface if a new interface was created for the
1403 * group.
1404 */
1405 if (wpa_s->global->p2p_group_formation)
1406 wpa_s = wpa_s->global->p2p_group_formation;
Dmitry Shmidt56052862013-10-04 10:23:25 -07001407 if (wpa_s->p2p_go_group_formation_completed) {
1408 wpa_s->global->p2p_group_formation = NULL;
1409 wpa_s->p2p_in_provisioning = 0;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001410 } else if (wpa_s->p2p_in_provisioning && !success) {
1411 wpa_msg(wpa_s, MSG_DEBUG,
1412 "P2P: Stop provisioning state due to failure");
1413 wpa_s->p2p_in_provisioning = 0;
Dmitry Shmidt56052862013-10-04 10:23:25 -07001414 }
Dmitry Shmidt21de2142014-04-08 10:50:52 -07001415 wpa_s->p2p_in_invitation = 0;
Matthew Wang06b42472022-11-10 06:56:31 +00001416 wpa_s->p2p_retry_limit = 0;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001417 wpa_s->group_formation_reported = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001418
1419 if (!success) {
Dmitry Shmidt9c175262016-03-03 10:20:07 -08001420 wpa_msg_global(wpa_s->p2pdev, MSG_INFO,
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07001421 P2P_EVENT_GROUP_FORMATION_FAILURE);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001422 wpas_notify_p2p_group_formation_failure(wpa_s, "");
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07001423 if (already_deleted)
1424 return;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001425 wpas_p2p_group_delete(wpa_s,
1426 P2P_GROUP_REMOVAL_FORMATION_FAILED);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001427 return;
1428 }
1429
Dmitry Shmidt9c175262016-03-03 10:20:07 -08001430 wpa_msg_global(wpa_s->p2pdev, MSG_INFO,
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07001431 P2P_EVENT_GROUP_FORMATION_SUCCESS);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001432
1433 ssid = wpa_s->current_ssid;
1434 if (ssid && ssid->mode == WPAS_MODE_P2P_GROUP_FORMATION) {
1435 ssid->mode = WPAS_MODE_P2P_GO;
1436 p2p_group_notif_formation_done(wpa_s->p2p_group);
1437 wpa_supplicant_ap_mac_addr_filter(wpa_s, NULL);
1438 }
1439
1440 persistent = 0;
1441 if (ssid) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001442 client = ssid->mode == WPAS_MODE_INFRA;
1443 if (ssid->mode == WPAS_MODE_P2P_GO) {
1444 persistent = ssid->p2p_persistent_group;
Dmitry Shmidt497c1d52011-07-21 15:19:46 -07001445 os_memcpy(go_dev_addr, wpa_s->global->p2p_dev_addr,
1446 ETH_ALEN);
George Burgess IV0d2d1bf2021-11-01 03:08:33 -07001447 } else {
1448 os_memset(go_dev_addr, 0, ETH_ALEN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001449 persistent = wpas_p2p_persistent_group(wpa_s,
1450 go_dev_addr,
1451 ssid->ssid,
1452 ssid->ssid_len);
George Burgess IV0d2d1bf2021-11-01 03:08:33 -07001453 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001454 } else {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001455 client = wpa_s->p2p_group_interface ==
1456 P2P_GROUP_INTERFACE_CLIENT;
1457 os_memset(go_dev_addr, 0, ETH_ALEN);
1458 }
1459
1460 wpa_s->show_group_started = 0;
1461 if (client) {
1462 /*
1463 * Indicate event only after successfully completed 4-way
1464 * handshake, i.e., when the interface is ready for data
1465 * packets.
1466 */
1467 wpa_s->show_group_started = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001468 } else {
Dmitry Shmidt7f0b69e2014-07-28 10:35:20 -07001469 wpas_p2p_group_started(wpa_s, 1, ssid,
1470 ssid ? ssid->frequency : 0,
1471 ssid && ssid->passphrase == NULL &&
1472 ssid->psk_set ? ssid->psk : NULL,
1473 ssid ? ssid->passphrase : NULL,
1474 go_dev_addr, persistent, "");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001475 wpas_p2p_cross_connect_setup(wpa_s);
1476 wpas_p2p_set_group_idle_timeout(wpa_s);
1477 }
1478
1479 if (persistent)
Dmitry Shmidtd5ab1b52016-06-21 12:38:41 -07001480 wpas_p2p_store_persistent_group(wpa_s->p2pdev,
1481 ssid, go_dev_addr);
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07001482 else {
1483 os_free(wpa_s->global->add_psk);
1484 wpa_s->global->add_psk = NULL;
1485 }
Dmitry Shmidtd5ab1b52016-06-21 12:38:41 -07001486
Dmitry Shmidt92c368d2013-08-29 12:37:21 -07001487 if (!client) {
Dmitry Shmidtabb90a32016-12-05 15:34:39 -08001488 wpas_notify_p2p_group_started(wpa_s, ssid, persistent, 0, NULL);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001489 os_get_reltime(&wpa_s->global->p2p_go_wait_client);
Dmitry Shmidt92c368d2013-08-29 12:37:21 -07001490 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001491}
1492
1493
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001494struct send_action_work {
1495 unsigned int freq;
1496 u8 dst[ETH_ALEN];
1497 u8 src[ETH_ALEN];
1498 u8 bssid[ETH_ALEN];
1499 size_t len;
1500 unsigned int wait_time;
1501 u8 buf[0];
1502};
1503
1504
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001505static void wpas_p2p_free_send_action_work(struct wpa_supplicant *wpa_s)
1506{
1507 struct send_action_work *awork = wpa_s->p2p_send_action_work->ctx;
1508
1509 wpa_printf(MSG_DEBUG,
1510 "P2P: Free Action frame radio work @%p (freq=%u dst="
1511 MACSTR " src=" MACSTR " bssid=" MACSTR " wait_time=%u)",
1512 wpa_s->p2p_send_action_work, awork->freq,
1513 MAC2STR(awork->dst), MAC2STR(awork->src),
1514 MAC2STR(awork->bssid), awork->wait_time);
1515 wpa_hexdump(MSG_DEBUG, "P2P: Freeing pending Action frame",
1516 awork->buf, awork->len);
1517 os_free(awork);
1518 wpa_s->p2p_send_action_work->ctx = NULL;
1519 radio_work_done(wpa_s->p2p_send_action_work);
1520 wpa_s->p2p_send_action_work = NULL;
1521}
1522
1523
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001524static void wpas_p2p_send_action_work_timeout(void *eloop_ctx,
1525 void *timeout_ctx)
1526{
1527 struct wpa_supplicant *wpa_s = eloop_ctx;
1528
1529 if (!wpa_s->p2p_send_action_work)
1530 return;
1531
1532 wpa_printf(MSG_DEBUG, "P2P: Send Action frame radio work timed out");
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001533 wpas_p2p_free_send_action_work(wpa_s);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001534}
1535
1536
Dmitry Shmidt03658832014-08-13 11:03:49 -07001537static void wpas_p2p_action_tx_clear(struct wpa_supplicant *wpa_s)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001538{
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001539 if (wpa_s->p2p_send_action_work) {
1540 struct send_action_work *awork;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001541
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001542 awork = wpa_s->p2p_send_action_work->ctx;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001543 wpa_printf(MSG_DEBUG,
1544 "P2P: Clear Action TX work @%p (wait_time=%u)",
1545 wpa_s->p2p_send_action_work, awork->wait_time);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001546 if (awork->wait_time == 0) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001547 wpas_p2p_free_send_action_work(wpa_s);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001548 } else {
1549 /*
1550 * In theory, this should not be needed, but number of
1551 * places in the P2P code is still using non-zero wait
1552 * time for the last Action frame in the sequence and
1553 * some of these do not call send_action_done().
1554 */
1555 eloop_cancel_timeout(wpas_p2p_send_action_work_timeout,
1556 wpa_s, NULL);
1557 eloop_register_timeout(
1558 0, awork->wait_time * 1000,
1559 wpas_p2p_send_action_work_timeout,
1560 wpa_s, NULL);
1561 }
1562 }
Dmitry Shmidt03658832014-08-13 11:03:49 -07001563}
1564
1565
1566static void wpas_p2p_send_action_tx_status(struct wpa_supplicant *wpa_s,
1567 unsigned int freq,
1568 const u8 *dst, const u8 *src,
1569 const u8 *bssid,
1570 const u8 *data, size_t data_len,
1571 enum offchannel_send_action_result
1572 result)
1573{
1574 enum p2p_send_action_result res = P2P_SEND_ACTION_SUCCESS;
1575
1576 wpas_p2p_action_tx_clear(wpa_s);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001577
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001578 if (wpa_s->global->p2p == NULL || wpa_s->global->p2p_disabled)
1579 return;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001580
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001581 switch (result) {
1582 case OFFCHANNEL_SEND_ACTION_SUCCESS:
1583 res = P2P_SEND_ACTION_SUCCESS;
1584 break;
1585 case OFFCHANNEL_SEND_ACTION_NO_ACK:
1586 res = P2P_SEND_ACTION_NO_ACK;
1587 break;
1588 case OFFCHANNEL_SEND_ACTION_FAILED:
1589 res = P2P_SEND_ACTION_FAILED;
1590 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001591 }
1592
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001593 p2p_send_action_cb(wpa_s->global->p2p, freq, dst, src, bssid, res);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001594
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08001595 if (result != OFFCHANNEL_SEND_ACTION_SUCCESS &&
1596 wpa_s->pending_pd_before_join &&
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001597 (os_memcmp(dst, wpa_s->pending_join_dev_addr, ETH_ALEN) == 0 ||
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001598 os_memcmp(dst, wpa_s->pending_join_iface_addr, ETH_ALEN) == 0) &&
1599 wpa_s->p2p_fallback_to_go_neg) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001600 wpa_s->pending_pd_before_join = 0;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001601 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: No ACK for PD Req "
1602 "during p2p_connect-auto");
Dmitry Shmidt9c175262016-03-03 10:20:07 -08001603 wpa_msg_global(wpa_s->p2pdev, MSG_INFO,
Dmitry Shmidt7f656022015-02-25 14:36:37 -08001604 P2P_EVENT_FALLBACK_TO_GO_NEG
1605 "reason=no-ACK-to-PD-Req");
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001606 wpas_p2p_fallback_to_go_neg(wpa_s, 0);
1607 return;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001608 }
1609}
1610
1611
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001612static void wpas_send_action_cb(struct wpa_radio_work *work, int deinit)
1613{
1614 struct wpa_supplicant *wpa_s = work->wpa_s;
1615 struct send_action_work *awork = work->ctx;
1616
1617 if (deinit) {
Dmitry Shmidtbd14a572014-02-18 10:33:49 -08001618 if (work->started) {
1619 eloop_cancel_timeout(wpas_p2p_send_action_work_timeout,
1620 wpa_s, NULL);
1621 wpa_s->p2p_send_action_work = NULL;
1622 offchannel_send_action_done(wpa_s);
1623 }
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001624 os_free(awork);
1625 return;
1626 }
1627
1628 if (offchannel_send_action(wpa_s, awork->freq, awork->dst, awork->src,
1629 awork->bssid, awork->buf, awork->len,
1630 awork->wait_time,
1631 wpas_p2p_send_action_tx_status, 1) < 0) {
1632 os_free(awork);
1633 radio_work_done(work);
1634 return;
1635 }
1636 wpa_s->p2p_send_action_work = work;
1637}
1638
1639
1640static int wpas_send_action_work(struct wpa_supplicant *wpa_s,
1641 unsigned int freq, const u8 *dst,
1642 const u8 *src, const u8 *bssid, const u8 *buf,
1643 size_t len, unsigned int wait_time)
1644{
1645 struct send_action_work *awork;
1646
Hai Shalom81f62d82019-07-22 12:10:00 -07001647 if (radio_work_pending(wpa_s, "p2p-send-action")) {
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001648 wpa_printf(MSG_DEBUG, "P2P: Cannot schedule new p2p-send-action work since one is already pending");
1649 return -1;
1650 }
1651
1652 awork = os_zalloc(sizeof(*awork) + len);
1653 if (awork == NULL)
1654 return -1;
1655
1656 awork->freq = freq;
1657 os_memcpy(awork->dst, dst, ETH_ALEN);
1658 os_memcpy(awork->src, src, ETH_ALEN);
1659 os_memcpy(awork->bssid, bssid, ETH_ALEN);
1660 awork->len = len;
1661 awork->wait_time = wait_time;
1662 os_memcpy(awork->buf, buf, len);
1663
Hai Shalom81f62d82019-07-22 12:10:00 -07001664 if (radio_add_work(wpa_s, freq, "p2p-send-action", 1,
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001665 wpas_send_action_cb, awork) < 0) {
1666 os_free(awork);
1667 return -1;
1668 }
1669
1670 return 0;
1671}
1672
1673
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001674static int wpas_send_action(void *ctx, unsigned int freq, const u8 *dst,
1675 const u8 *src, const u8 *bssid, const u8 *buf,
Hai Shalom021b0b52019-04-10 11:17:58 -07001676 size_t len, unsigned int wait_time, int *scheduled)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001677{
1678 struct wpa_supplicant *wpa_s = ctx;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001679 int listen_freq = -1, send_freq = -1;
1680
Hai Shalom021b0b52019-04-10 11:17:58 -07001681 if (scheduled)
1682 *scheduled = 0;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001683 if (wpa_s->p2p_listen_work)
1684 listen_freq = wpa_s->p2p_listen_work->freq;
1685 if (wpa_s->p2p_send_action_work)
1686 send_freq = wpa_s->p2p_send_action_work->freq;
1687 if (listen_freq != (int) freq && send_freq != (int) freq) {
Hai Shalom021b0b52019-04-10 11:17:58 -07001688 int res;
1689
1690 wpa_printf(MSG_DEBUG, "P2P: Schedule new radio work for Action frame TX (listen_freq=%d send_freq=%d freq=%u)",
1691 listen_freq, send_freq, freq);
1692 res = wpas_send_action_work(wpa_s, freq, dst, src, bssid, buf,
1693 len, wait_time);
1694 if (res == 0 && scheduled)
1695 *scheduled = 1;
1696 return res;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001697 }
1698
1699 wpa_printf(MSG_DEBUG, "P2P: Use ongoing radio work for Action frame TX");
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001700 return offchannel_send_action(wpa_s, freq, dst, src, bssid, buf, len,
1701 wait_time,
1702 wpas_p2p_send_action_tx_status, 1);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001703}
1704
1705
1706static void wpas_send_action_done(void *ctx)
1707{
1708 struct wpa_supplicant *wpa_s = ctx;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001709
1710 if (wpa_s->p2p_send_action_work) {
1711 eloop_cancel_timeout(wpas_p2p_send_action_work_timeout,
1712 wpa_s, NULL);
1713 os_free(wpa_s->p2p_send_action_work->ctx);
1714 radio_work_done(wpa_s->p2p_send_action_work);
1715 wpa_s->p2p_send_action_work = NULL;
1716 }
1717
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001718 offchannel_send_action_done(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001719}
1720
1721
1722static int wpas_copy_go_neg_results(struct wpa_supplicant *wpa_s,
1723 struct p2p_go_neg_results *params)
1724{
1725 if (wpa_s->go_params == NULL) {
1726 wpa_s->go_params = os_malloc(sizeof(*params));
1727 if (wpa_s->go_params == NULL)
1728 return -1;
1729 }
1730 os_memcpy(wpa_s->go_params, params, sizeof(*params));
1731 return 0;
1732}
1733
1734
1735static void wpas_start_wps_enrollee(struct wpa_supplicant *wpa_s,
1736 struct p2p_go_neg_results *res)
1737{
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001738 wpa_s->group_formation_reported = 0;
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001739 wpa_printf(MSG_DEBUG, "P2P: Start WPS Enrollee for peer " MACSTR
1740 " dev_addr " MACSTR " wps_method %d",
1741 MAC2STR(res->peer_interface_addr),
1742 MAC2STR(res->peer_device_addr), res->wps_method);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001743 wpa_hexdump_ascii(MSG_DEBUG, "P2P: Start WPS Enrollee for SSID",
1744 res->ssid, res->ssid_len);
1745 wpa_supplicant_ap_deinit(wpa_s);
1746 wpas_copy_go_neg_results(wpa_s, res);
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001747 if (res->wps_method == WPS_PBC) {
Hai Shalom021b0b52019-04-10 11:17:58 -07001748 wpas_wps_start_pbc(wpa_s, res->peer_interface_addr, 1, 0);
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001749#ifdef CONFIG_WPS_NFC
1750 } else if (res->wps_method == WPS_NFC) {
1751 wpas_wps_start_nfc(wpa_s, res->peer_device_addr,
1752 res->peer_interface_addr,
Dmitry Shmidt9c175262016-03-03 10:20:07 -08001753 wpa_s->p2pdev->p2p_oob_dev_pw,
1754 wpa_s->p2pdev->p2p_oob_dev_pw_id, 1,
1755 wpa_s->p2pdev->p2p_oob_dev_pw_id ==
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001756 DEV_PW_NFC_CONNECTION_HANDOVER ?
Dmitry Shmidt9c175262016-03-03 10:20:07 -08001757 wpa_s->p2pdev->p2p_peer_oob_pubkey_hash :
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001758 NULL,
1759 NULL, 0, 0);
1760#endif /* CONFIG_WPS_NFC */
1761 } else {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001762 u16 dev_pw_id = DEV_PW_DEFAULT;
Dmitry Shmidt216983b2015-02-06 10:50:36 -08001763 if (wpa_s->p2p_wps_method == WPS_P2PS)
1764 dev_pw_id = DEV_PW_P2PS_DEFAULT;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001765 if (wpa_s->p2p_wps_method == WPS_PIN_KEYPAD)
1766 dev_pw_id = DEV_PW_REGISTRAR_SPECIFIED;
1767 wpas_wps_start_pin(wpa_s, res->peer_interface_addr,
1768 wpa_s->p2p_pin, 1, dev_pw_id);
1769 }
1770}
1771
1772
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07001773static void wpas_p2p_add_psk_list(struct wpa_supplicant *wpa_s,
1774 struct wpa_ssid *ssid)
1775{
1776 struct wpa_ssid *persistent;
1777 struct psk_list_entry *psk;
1778 struct hostapd_data *hapd;
1779
1780 if (!wpa_s->ap_iface)
1781 return;
1782
Dmitry Shmidt9c175262016-03-03 10:20:07 -08001783 persistent = wpas_p2p_get_persistent(wpa_s->p2pdev, NULL, ssid->ssid,
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07001784 ssid->ssid_len);
1785 if (persistent == NULL)
1786 return;
1787
1788 hapd = wpa_s->ap_iface->bss[0];
1789
1790 dl_list_for_each(psk, &persistent->psk_list, struct psk_list_entry,
1791 list) {
1792 struct hostapd_wpa_psk *hpsk;
1793
1794 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Add persistent group PSK entry for "
1795 MACSTR " psk=%d",
1796 MAC2STR(psk->addr), psk->p2p);
1797 hpsk = os_zalloc(sizeof(*hpsk));
1798 if (hpsk == NULL)
1799 break;
1800 os_memcpy(hpsk->psk, psk->psk, PMK_LEN);
1801 if (psk->p2p)
1802 os_memcpy(hpsk->p2p_dev_addr, psk->addr, ETH_ALEN);
1803 else
1804 os_memcpy(hpsk->addr, psk->addr, ETH_ALEN);
1805 hpsk->next = hapd->conf->ssid.wpa_psk;
1806 hapd->conf->ssid.wpa_psk = hpsk;
1807 }
1808}
1809
1810
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001811static void p2p_go_dump_common_freqs(struct wpa_supplicant *wpa_s)
1812{
Roshan Pius3a1667e2018-07-03 15:17:14 -07001813 char buf[20 + P2P_MAX_CHANNELS * 6];
1814 char *pos, *end;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001815 unsigned int i;
Roshan Pius3a1667e2018-07-03 15:17:14 -07001816 int res;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001817
Roshan Pius3a1667e2018-07-03 15:17:14 -07001818 pos = buf;
1819 end = pos + sizeof(buf);
1820 for (i = 0; i < wpa_s->p2p_group_common_freqs_num; i++) {
1821 res = os_snprintf(pos, end - pos, " %d",
1822 wpa_s->p2p_group_common_freqs[i]);
1823 if (os_snprintf_error(end - pos, res))
1824 break;
1825 pos += res;
1826 }
1827 *pos = '\0';
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001828
Roshan Pius3a1667e2018-07-03 15:17:14 -07001829 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Common group frequencies:%s", buf);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001830}
1831
1832
1833static void p2p_go_save_group_common_freqs(struct wpa_supplicant *wpa_s,
1834 struct p2p_go_neg_results *params)
1835{
1836 unsigned int i, len = int_array_len(wpa_s->go_params->freq_list);
1837
1838 wpa_s->p2p_group_common_freqs_num = 0;
1839 os_free(wpa_s->p2p_group_common_freqs);
1840 wpa_s->p2p_group_common_freqs = os_calloc(len, sizeof(int));
1841 if (!wpa_s->p2p_group_common_freqs)
1842 return;
1843
1844 for (i = 0; i < len; i++) {
1845 if (!wpa_s->go_params->freq_list[i])
1846 break;
1847 wpa_s->p2p_group_common_freqs[i] =
1848 wpa_s->go_params->freq_list[i];
1849 }
1850 wpa_s->p2p_group_common_freqs_num = i;
1851}
1852
1853
Dmitry Shmidt216983b2015-02-06 10:50:36 -08001854static void p2p_config_write(struct wpa_supplicant *wpa_s)
1855{
1856#ifndef CONFIG_NO_CONFIG_WRITE
Dmitry Shmidt9c175262016-03-03 10:20:07 -08001857 if (wpa_s->p2pdev->conf->update_config &&
1858 wpa_config_write(wpa_s->p2pdev->confname, wpa_s->p2pdev->conf))
Dmitry Shmidt216983b2015-02-06 10:50:36 -08001859 wpa_printf(MSG_DEBUG, "P2P: Failed to update configuration");
1860#endif /* CONFIG_NO_CONFIG_WRITE */
1861}
1862
1863
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001864static void p2p_go_configured(void *ctx, void *data)
1865{
1866 struct wpa_supplicant *wpa_s = ctx;
1867 struct p2p_go_neg_results *params = data;
1868 struct wpa_ssid *ssid;
1869
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08001870 wpa_s->ap_configured_cb = NULL;
1871 wpa_s->ap_configured_cb_ctx = NULL;
1872 wpa_s->ap_configured_cb_data = NULL;
1873 if (!wpa_s->go_params) {
1874 wpa_printf(MSG_ERROR,
1875 "P2P: p2p_go_configured() called with wpa_s->go_params == NULL");
1876 return;
1877 }
1878
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001879 p2p_go_save_group_common_freqs(wpa_s, params);
1880 p2p_go_dump_common_freqs(wpa_s);
1881
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001882 ssid = wpa_s->current_ssid;
1883 if (ssid && ssid->mode == WPAS_MODE_P2P_GO) {
1884 wpa_printf(MSG_DEBUG, "P2P: Group setup without provisioning");
1885 if (wpa_s->global->p2p_group_formation == wpa_s)
1886 wpa_s->global->p2p_group_formation = NULL;
Dmitry Shmidt7f0b69e2014-07-28 10:35:20 -07001887 wpas_p2p_group_started(wpa_s, 1, ssid, ssid->frequency,
1888 params->passphrase[0] == '\0' ?
1889 params->psk : NULL,
1890 params->passphrase,
1891 wpa_s->global->p2p_dev_addr,
1892 params->persistent_group, "");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001893 wpa_s->group_formation_reported = 1;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001894
Dmitry Shmidt9c175262016-03-03 10:20:07 -08001895 if (wpa_s->p2pdev->p2ps_method_config_any) {
1896 if (is_zero_ether_addr(wpa_s->p2pdev->p2ps_join_addr)) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001897 wpa_dbg(wpa_s, MSG_DEBUG,
1898 "P2PS: Setting default PIN for ANY");
1899 wpa_supplicant_ap_wps_pin(wpa_s, NULL,
1900 "12345670", NULL, 0,
1901 0);
1902 } else {
1903 wpa_dbg(wpa_s, MSG_DEBUG,
1904 "P2PS: Setting default PIN for " MACSTR,
Dmitry Shmidt9c175262016-03-03 10:20:07 -08001905 MAC2STR(wpa_s->p2pdev->p2ps_join_addr));
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001906 wpa_supplicant_ap_wps_pin(
Dmitry Shmidt9c175262016-03-03 10:20:07 -08001907 wpa_s, wpa_s->p2pdev->p2ps_join_addr,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001908 "12345670", NULL, 0, 0);
1909 }
Dmitry Shmidt9c175262016-03-03 10:20:07 -08001910 wpa_s->p2pdev->p2ps_method_config_any = 0;
Dmitry Shmidt216983b2015-02-06 10:50:36 -08001911 }
1912
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001913 os_get_reltime(&wpa_s->global->p2p_go_wait_client);
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07001914 if (params->persistent_group) {
Dmitry Shmidtd5ab1b52016-06-21 12:38:41 -07001915 wpas_p2p_store_persistent_group(
Dmitry Shmidt9c175262016-03-03 10:20:07 -08001916 wpa_s->p2pdev, ssid,
Dmitry Shmidt497c1d52011-07-21 15:19:46 -07001917 wpa_s->global->p2p_dev_addr);
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07001918 wpas_p2p_add_psk_list(wpa_s, ssid);
1919 }
Dmitry Shmidtd5ab1b52016-06-21 12:38:41 -07001920
1921 wpas_notify_p2p_group_started(wpa_s, ssid,
Dmitry Shmidtabb90a32016-12-05 15:34:39 -08001922 params->persistent_group, 0,
1923 NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001924 wpas_p2p_cross_connect_setup(wpa_s);
1925 wpas_p2p_set_group_idle_timeout(wpa_s);
Dmitry Shmidt56052862013-10-04 10:23:25 -07001926
1927 if (wpa_s->p2p_first_connection_timeout) {
1928 wpa_dbg(wpa_s, MSG_DEBUG,
1929 "P2P: Start group formation timeout of %d seconds until first data connection on GO",
1930 wpa_s->p2p_first_connection_timeout);
1931 wpa_s->p2p_go_group_formation_completed = 0;
1932 wpa_s->global->p2p_group_formation = wpa_s;
1933 eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
Dmitry Shmidt9c175262016-03-03 10:20:07 -08001934 wpa_s->p2pdev, NULL);
Dmitry Shmidt56052862013-10-04 10:23:25 -07001935 eloop_register_timeout(
1936 wpa_s->p2p_first_connection_timeout, 0,
1937 wpas_p2p_group_formation_timeout,
Dmitry Shmidt9c175262016-03-03 10:20:07 -08001938 wpa_s->p2pdev, NULL);
Dmitry Shmidt56052862013-10-04 10:23:25 -07001939 }
1940
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001941 return;
1942 }
1943
1944 wpa_printf(MSG_DEBUG, "P2P: Setting up WPS for GO provisioning");
1945 if (wpa_supplicant_ap_mac_addr_filter(wpa_s,
1946 params->peer_interface_addr)) {
1947 wpa_printf(MSG_DEBUG, "P2P: Failed to setup MAC address "
1948 "filtering");
1949 return;
1950 }
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001951 if (params->wps_method == WPS_PBC) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001952 wpa_supplicant_ap_wps_pbc(wpa_s, params->peer_interface_addr,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001953 params->peer_device_addr);
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001954#ifdef CONFIG_WPS_NFC
1955 } else if (params->wps_method == WPS_NFC) {
Dmitry Shmidt9c175262016-03-03 10:20:07 -08001956 if (wpa_s->p2pdev->p2p_oob_dev_pw_id !=
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001957 DEV_PW_NFC_CONNECTION_HANDOVER &&
Dmitry Shmidt9c175262016-03-03 10:20:07 -08001958 !wpa_s->p2pdev->p2p_oob_dev_pw) {
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001959 wpa_printf(MSG_DEBUG, "P2P: No NFC Dev Pw known");
1960 return;
1961 }
1962 wpas_ap_wps_add_nfc_pw(
Dmitry Shmidt9c175262016-03-03 10:20:07 -08001963 wpa_s, wpa_s->p2pdev->p2p_oob_dev_pw_id,
1964 wpa_s->p2pdev->p2p_oob_dev_pw,
1965 wpa_s->p2pdev->p2p_peer_oob_pk_hash_known ?
1966 wpa_s->p2pdev->p2p_peer_oob_pubkey_hash : NULL);
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001967#endif /* CONFIG_WPS_NFC */
1968 } else if (wpa_s->p2p_pin[0])
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001969 wpa_supplicant_ap_wps_pin(wpa_s, params->peer_interface_addr,
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001970 wpa_s->p2p_pin, NULL, 0, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001971 os_free(wpa_s->go_params);
1972 wpa_s->go_params = NULL;
1973}
1974
1975
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08001976/**
1977 * wpas_p2p_freq_to_edmg_channel - Convert frequency into EDMG channel
1978 * @freq: Frequency (MHz) to convert
1979 * @op_class: Buffer for returning operating class
1980 * @op_edmg_channel: Buffer for returning channel number
1981 * Returns: 0 on success, -1 on failure
1982 *
1983 * This can be used to find the highest channel bonding which includes the
1984 * specified frequency.
1985 */
1986static int wpas_p2p_freq_to_edmg_channel(struct wpa_supplicant *wpa_s,
1987 unsigned int freq,
1988 u8 *op_class, u8 *op_edmg_channel)
1989{
1990 struct hostapd_hw_modes *hwmode;
1991 struct ieee80211_edmg_config edmg;
1992 unsigned int i;
1993 enum chan_width chanwidth[] = {
1994 CHAN_WIDTH_8640,
1995 CHAN_WIDTH_6480,
1996 CHAN_WIDTH_4320,
1997 };
1998
1999 if (!wpa_s->hw.modes)
2000 return -1;
2001
2002 hwmode = get_mode(wpa_s->hw.modes, wpa_s->hw.num_modes,
Hai Shalom60840252021-02-19 19:02:11 -08002003 HOSTAPD_MODE_IEEE80211AD, false);
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08002004 if (!hwmode) {
2005 wpa_printf(MSG_ERROR,
2006 "Unsupported AP mode: HOSTAPD_MODE_IEEE80211AD");
2007 return -1;
2008 }
2009
2010 /* Find the highest EDMG channel bandwidth to start the P2P GO */
2011 for (i = 0; i < ARRAY_SIZE(chanwidth); i++) {
2012 if (ieee80211_chaninfo_to_channel(freq, chanwidth[i], 0,
2013 op_class,
2014 op_edmg_channel) < 0)
2015 continue;
2016
2017 hostapd_encode_edmg_chan(1, *op_edmg_channel, 0, &edmg);
2018 if (edmg.channels &&
2019 ieee802_edmg_is_allowed(hwmode->edmg, edmg)) {
2020 wpa_printf(MSG_DEBUG,
2021 "Freq %u to EDMG channel %u at opclass %u",
2022 freq, *op_edmg_channel, *op_class);
2023 return 0;
2024 }
2025 }
2026
2027 return -1;
2028}
2029
2030
2031int wpas_p2p_try_edmg_channel(struct wpa_supplicant *wpa_s,
2032 struct p2p_go_neg_results *params)
2033{
2034 u8 op_channel, op_class;
2035 int freq;
2036
2037 /* Try social channel as primary channel frequency */
2038 freq = (!params->freq) ? 58320 + 1 * 2160 : params->freq;
2039
2040 if (wpas_p2p_freq_to_edmg_channel(wpa_s, freq, &op_class,
2041 &op_channel) == 0) {
2042 wpa_printf(MSG_DEBUG,
2043 "Freq %d will be used to set an EDMG connection (channel=%u opclass=%u)",
2044 freq, op_channel, op_class);
2045 params->freq = freq;
2046 return 0;
2047 }
2048
2049 return -1;
2050}
2051
2052
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002053static void wpas_start_wps_go(struct wpa_supplicant *wpa_s,
2054 struct p2p_go_neg_results *params,
2055 int group_formation)
2056{
2057 struct wpa_ssid *ssid;
2058
Dmitry Shmidtaa532512012-09-24 10:35:31 -07002059 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Starting GO");
2060 if (wpas_copy_go_neg_results(wpa_s, params) < 0) {
2061 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Could not copy GO Negotiation "
2062 "results");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002063 return;
Dmitry Shmidtaa532512012-09-24 10:35:31 -07002064 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002065
2066 ssid = wpa_config_add_network(wpa_s->conf);
Dmitry Shmidtaa532512012-09-24 10:35:31 -07002067 if (ssid == NULL) {
2068 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Could not add network for GO");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002069 return;
Dmitry Shmidtaa532512012-09-24 10:35:31 -07002070 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002071
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002072 wpa_s->show_group_started = 0;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002073 wpa_s->p2p_go_group_formation_completed = 0;
2074 wpa_s->group_formation_reported = 0;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002075 os_memset(wpa_s->go_dev_addr, 0, ETH_ALEN);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002076
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002077 wpa_config_set_network_defaults(ssid);
2078 ssid->temporary = 1;
2079 ssid->p2p_group = 1;
Dmitry Shmidtaca489e2016-09-28 15:44:14 -07002080 ssid->p2p_persistent_group = !!params->persistent_group;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002081 ssid->mode = group_formation ? WPAS_MODE_P2P_GROUP_FORMATION :
2082 WPAS_MODE_P2P_GO;
2083 ssid->frequency = params->freq;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002084 ssid->ht40 = params->ht40;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07002085 ssid->vht = params->vht;
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08002086 ssid->max_oper_chwidth = params->max_oper_chwidth;
2087 ssid->vht_center_freq2 = params->vht_center_freq2;
Hai Shalom74f70d42019-02-11 14:42:39 -08002088 ssid->he = params->he;
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08002089 if (params->edmg) {
2090 u8 op_channel, op_class;
2091
2092 if (!wpas_p2p_freq_to_edmg_channel(wpa_s, params->freq,
2093 &op_class, &op_channel)) {
2094 ssid->edmg_channel = op_channel;
2095 ssid->enable_edmg = params->edmg;
2096 } else {
2097 wpa_dbg(wpa_s, MSG_DEBUG,
2098 "P2P: Could not match EDMG channel, freq %d, for GO",
2099 params->freq);
2100 }
2101 }
2102
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002103 ssid->ssid = os_zalloc(params->ssid_len + 1);
2104 if (ssid->ssid) {
2105 os_memcpy(ssid->ssid, params->ssid, params->ssid_len);
2106 ssid->ssid_len = params->ssid_len;
2107 }
2108 ssid->auth_alg = WPA_AUTH_ALG_OPEN;
2109 ssid->key_mgmt = WPA_KEY_MGMT_PSK;
Hai Shalomc1a21442022-02-04 13:43:00 -08002110 if (is_6ghz_freq(ssid->frequency) &&
2111 is_p2p_6ghz_capable(wpa_s->global->p2p)) {
2112 ssid->auth_alg |= WPA_AUTH_ALG_SAE;
2113 ssid->key_mgmt = WPA_KEY_MGMT_SAE;
2114 ssid->ieee80211w = MGMT_FRAME_PROTECTION_REQUIRED;
Sunil Ravi77d572f2023-01-17 23:58:31 +00002115 ssid->sae_pwe = SAE_PWE_HASH_TO_ELEMENT;
Hai Shalomc1a21442022-02-04 13:43:00 -08002116 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Use SAE auth_alg and key_mgmt");
2117 } else {
2118 p2p_set_6ghz_dev_capab(wpa_s->global->p2p, false);
2119 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002120 ssid->proto = WPA_PROTO_RSN;
2121 ssid->pairwise_cipher = WPA_CIPHER_CCMP;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002122 ssid->group_cipher = WPA_CIPHER_CCMP;
2123 if (params->freq > 56160) {
2124 /*
2125 * Enable GCMP instead of CCMP as pairwise_cipher and
2126 * group_cipher in 60 GHz.
2127 */
2128 ssid->pairwise_cipher = WPA_CIPHER_GCMP;
2129 ssid->group_cipher = WPA_CIPHER_GCMP;
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08002130 /* P2P GO in 60 GHz is always a PCP (PBSS) */
2131 ssid->pbss = 1;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002132 }
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08002133 if (os_strlen(params->passphrase) > 0) {
2134 ssid->passphrase = os_strdup(params->passphrase);
2135 if (ssid->passphrase == NULL) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07002136 wpa_msg_global(wpa_s, MSG_ERROR,
2137 "P2P: Failed to copy passphrase for GO");
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08002138 wpa_config_remove_network(wpa_s->conf, ssid->id);
2139 return;
2140 }
2141 } else
2142 ssid->passphrase = NULL;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002143 ssid->psk_set = params->psk_set;
2144 if (ssid->psk_set)
2145 os_memcpy(ssid->psk, params->psk, sizeof(ssid->psk));
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08002146 else if (ssid->passphrase)
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002147 wpa_config_update_psk(ssid);
Dmitry Shmidt9c175262016-03-03 10:20:07 -08002148 ssid->ap_max_inactivity = wpa_s->p2pdev->conf->p2p_go_max_inactivity;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002149
2150 wpa_s->ap_configured_cb = p2p_go_configured;
2151 wpa_s->ap_configured_cb_ctx = wpa_s;
2152 wpa_s->ap_configured_cb_data = wpa_s->go_params;
Dmitry Shmidt6dc03bd2014-05-16 10:40:13 -07002153 wpa_s->scan_req = NORMAL_SCAN_REQ;
Jouni Malinen75ecf522011-06-27 15:19:46 -07002154 wpa_s->connect_without_scan = ssid;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002155 wpa_s->reassociate = 1;
2156 wpa_s->disconnected = 0;
Dmitry Shmidtaa532512012-09-24 10:35:31 -07002157 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Request scan (that will be skipped) to "
2158 "start GO)");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002159 wpa_supplicant_req_scan(wpa_s, 0, 0);
2160}
2161
2162
2163static void wpas_p2p_clone_config(struct wpa_supplicant *dst,
2164 const struct wpa_supplicant *src)
2165{
2166 struct wpa_config *d;
2167 const struct wpa_config *s;
2168
2169 d = dst->conf;
2170 s = src->conf;
2171
Dmitry Shmidt849734c2016-05-27 09:59:01 -07002172#define C(n) \
2173do { \
2174 if (s->n && !d->n) \
2175 d->n = os_strdup(s->n); \
2176} while (0)
2177
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002178 C(device_name);
2179 C(manufacturer);
2180 C(model_name);
2181 C(model_number);
2182 C(serial_number);
2183 C(config_methods);
2184#undef C
2185
2186 os_memcpy(d->device_type, s->device_type, WPS_DEV_TYPE_LEN);
2187 os_memcpy(d->sec_device_type, s->sec_device_type,
2188 sizeof(d->sec_device_type));
2189 d->num_sec_device_types = s->num_sec_device_types;
2190
2191 d->p2p_group_idle = s->p2p_group_idle;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002192 d->p2p_go_freq_change_policy = s->p2p_go_freq_change_policy;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002193 d->p2p_intra_bss = s->p2p_intra_bss;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002194 d->persistent_reconnect = s->persistent_reconnect;
Dmitry Shmidt04949592012-07-19 12:16:46 -07002195 d->max_num_sta = s->max_num_sta;
2196 d->pbc_in_m1 = s->pbc_in_m1;
Dmitry Shmidt444d5672013-04-01 13:08:44 -07002197 d->ignore_old_scan_res = s->ignore_old_scan_res;
Dmitry Shmidt51b6ea82013-05-08 10:42:09 -07002198 d->beacon_int = s->beacon_int;
Dmitry Shmidt18463232014-01-24 12:29:41 -08002199 d->dtim_period = s->dtim_period;
Dmitry Shmidt7f656022015-02-25 14:36:37 -08002200 d->p2p_go_ctwindow = s->p2p_go_ctwindow;
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07002201 d->disassoc_low_ack = s->disassoc_low_ack;
Dmitry Shmidt96571392013-10-14 12:54:46 -07002202 d->disable_scan_offload = s->disable_scan_offload;
Dmitry Shmidt807291d2015-01-27 13:40:23 -08002203 d->passive_scan = s->passive_scan;
Hai Shalomc1a21442022-02-04 13:43:00 -08002204 d->pmf = s->pmf;
2205 d->p2p_6ghz_disable = s->p2p_6ghz_disable;
Sunil Ravi77d572f2023-01-17 23:58:31 +00002206 d->sae_pwe = s->sae_pwe;
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08002207
Dmitry Shmidt849734c2016-05-27 09:59:01 -07002208 if (s->wps_nfc_dh_privkey && s->wps_nfc_dh_pubkey &&
2209 !d->wps_nfc_pw_from_config) {
2210 wpabuf_free(d->wps_nfc_dh_privkey);
2211 wpabuf_free(d->wps_nfc_dh_pubkey);
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08002212 d->wps_nfc_dh_privkey = wpabuf_dup(s->wps_nfc_dh_privkey);
2213 d->wps_nfc_dh_pubkey = wpabuf_dup(s->wps_nfc_dh_pubkey);
2214 }
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07002215 d->p2p_cli_probe = s->p2p_cli_probe;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002216 d->go_interworking = s->go_interworking;
2217 d->go_access_network_type = s->go_access_network_type;
2218 d->go_internet = s->go_internet;
2219 d->go_venue_group = s->go_venue_group;
2220 d->go_venue_type = s->go_venue_type;
Hai Shalom60840252021-02-19 19:02:11 -08002221 d->p2p_add_cli_chan = s->p2p_add_cli_chan;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002222}
2223
2224
Dmitry Shmidt34af3062013-07-11 10:46:32 -07002225static void wpas_p2p_get_group_ifname(struct wpa_supplicant *wpa_s,
2226 char *ifname, size_t len)
2227{
2228 char *ifname_ptr = wpa_s->ifname;
2229
2230 if (os_strncmp(wpa_s->ifname, P2P_MGMT_DEVICE_PREFIX,
2231 os_strlen(P2P_MGMT_DEVICE_PREFIX)) == 0) {
2232 ifname_ptr = os_strrchr(wpa_s->ifname, '-') + 1;
2233 }
2234
2235 os_snprintf(ifname, len, "p2p-%s-%d", ifname_ptr, wpa_s->p2p_group_idx);
2236 if (os_strlen(ifname) >= IFNAMSIZ &&
2237 os_strlen(wpa_s->ifname) < IFNAMSIZ) {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002238 int res;
2239
Dmitry Shmidt34af3062013-07-11 10:46:32 -07002240 /* Try to avoid going over the IFNAMSIZ length limit */
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002241 res = os_snprintf(ifname, len, "p2p-%d", wpa_s->p2p_group_idx);
2242 if (os_snprintf_error(len, res) && len)
2243 ifname[len - 1] = '\0';
Dmitry Shmidt34af3062013-07-11 10:46:32 -07002244 }
2245}
2246
2247
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002248static int wpas_p2p_add_group_interface(struct wpa_supplicant *wpa_s,
2249 enum wpa_driver_if_type type)
2250{
2251 char ifname[120], force_ifname[120];
2252
2253 if (wpa_s->pending_interface_name[0]) {
2254 wpa_printf(MSG_DEBUG, "P2P: Pending virtual interface exists "
2255 "- skip creation of a new one");
2256 if (is_zero_ether_addr(wpa_s->pending_interface_addr)) {
2257 wpa_printf(MSG_DEBUG, "P2P: Pending virtual address "
2258 "unknown?! ifname='%s'",
2259 wpa_s->pending_interface_name);
2260 return -1;
2261 }
2262 return 0;
2263 }
2264
Dmitry Shmidt34af3062013-07-11 10:46:32 -07002265 wpas_p2p_get_group_ifname(wpa_s, ifname, sizeof(ifname));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002266 force_ifname[0] = '\0';
2267
2268 wpa_printf(MSG_DEBUG, "P2P: Create a new interface %s for the group",
2269 ifname);
2270 wpa_s->p2p_group_idx++;
2271
2272 wpa_s->pending_interface_type = type;
2273 if (wpa_drv_if_add(wpa_s, type, ifname, NULL, NULL, force_ifname,
2274 wpa_s->pending_interface_addr, NULL) < 0) {
2275 wpa_printf(MSG_ERROR, "P2P: Failed to create new group "
2276 "interface");
2277 return -1;
2278 }
2279
Jimmy Chen36c21992018-11-29 16:46:43 +08002280 if (wpa_s->conf->p2p_interface_random_mac_addr) {
Hai Shalom74f70d42019-02-11 14:42:39 -08002281 random_mac_addr(wpa_s->pending_interface_addr);
2282 wpa_printf(MSG_DEBUG, "P2P: Generate random MAC address " MACSTR
2283 " for the group",
2284 MAC2STR(wpa_s->pending_interface_addr));
Jimmy Chen36c21992018-11-29 16:46:43 +08002285 }
2286
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002287 if (force_ifname[0]) {
2288 wpa_printf(MSG_DEBUG, "P2P: Driver forced interface name %s",
2289 force_ifname);
2290 os_strlcpy(wpa_s->pending_interface_name, force_ifname,
2291 sizeof(wpa_s->pending_interface_name));
2292 } else
2293 os_strlcpy(wpa_s->pending_interface_name, ifname,
2294 sizeof(wpa_s->pending_interface_name));
2295 wpa_printf(MSG_DEBUG, "P2P: Created pending virtual interface %s addr "
2296 MACSTR, wpa_s->pending_interface_name,
2297 MAC2STR(wpa_s->pending_interface_addr));
2298
2299 return 0;
2300}
2301
2302
2303static void wpas_p2p_remove_pending_group_interface(
2304 struct wpa_supplicant *wpa_s)
2305{
2306 if (!wpa_s->pending_interface_name[0] ||
2307 is_zero_ether_addr(wpa_s->pending_interface_addr))
2308 return; /* No pending virtual interface */
2309
2310 wpa_printf(MSG_DEBUG, "P2P: Removing pending group interface %s",
2311 wpa_s->pending_interface_name);
2312 wpa_drv_if_remove(wpa_s, wpa_s->pending_interface_type,
2313 wpa_s->pending_interface_name);
2314 os_memset(wpa_s->pending_interface_addr, 0, ETH_ALEN);
2315 wpa_s->pending_interface_name[0] = '\0';
Dmitry Shmidt216983b2015-02-06 10:50:36 -08002316 wpa_s->global->pending_group_iface_for_p2ps = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002317}
2318
2319
2320static struct wpa_supplicant *
2321wpas_p2p_init_group_interface(struct wpa_supplicant *wpa_s, int go)
2322{
2323 struct wpa_interface iface;
2324 struct wpa_supplicant *group_wpa_s;
2325
2326 if (!wpa_s->pending_interface_name[0]) {
2327 wpa_printf(MSG_ERROR, "P2P: No pending group interface");
2328 if (!wpas_p2p_create_iface(wpa_s))
2329 return NULL;
2330 /*
2331 * Something has forced us to remove the pending interface; try
2332 * to create a new one and hope for the best that we will get
2333 * the same local address.
2334 */
2335 if (wpas_p2p_add_group_interface(wpa_s, go ? WPA_IF_P2P_GO :
2336 WPA_IF_P2P_CLIENT) < 0)
2337 return NULL;
2338 }
2339
2340 os_memset(&iface, 0, sizeof(iface));
2341 iface.ifname = wpa_s->pending_interface_name;
2342 iface.driver = wpa_s->driver->name;
Dmitry Shmidt34af3062013-07-11 10:46:32 -07002343 if (wpa_s->conf->ctrl_interface == NULL &&
2344 wpa_s->parent != wpa_s &&
2345 wpa_s->p2p_mgmt &&
2346 (wpa_s->drv_flags & WPA_DRIVER_FLAGS_DEDICATED_P2P_DEVICE))
2347 iface.ctrl_interface = wpa_s->parent->conf->ctrl_interface;
2348 else
2349 iface.ctrl_interface = wpa_s->conf->ctrl_interface;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002350 iface.driver_param = wpa_s->conf->driver_param;
Dmitry Shmidt203eadb2015-03-05 14:16:04 -08002351 group_wpa_s = wpa_supplicant_add_iface(wpa_s->global, &iface, wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002352 if (group_wpa_s == NULL) {
2353 wpa_printf(MSG_ERROR, "P2P: Failed to create new "
2354 "wpa_supplicant interface");
2355 return NULL;
2356 }
2357 wpa_s->pending_interface_name[0] = '\0';
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002358 group_wpa_s->p2p_group_interface = go ? P2P_GROUP_INTERFACE_GO :
2359 P2P_GROUP_INTERFACE_CLIENT;
2360 wpa_s->global->p2p_group_formation = group_wpa_s;
Dmitry Shmidt216983b2015-02-06 10:50:36 -08002361 wpa_s->global->pending_group_iface_for_p2ps = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002362
2363 wpas_p2p_clone_config(group_wpa_s, wpa_s);
2364
Jimmy Chen36c21992018-11-29 16:46:43 +08002365 if (wpa_s->conf->p2p_interface_random_mac_addr) {
Hai Shalom74f70d42019-02-11 14:42:39 -08002366 if (wpa_drv_set_mac_addr(group_wpa_s,
2367 wpa_s->pending_interface_addr) < 0) {
Jimmy Chen36c21992018-11-29 16:46:43 +08002368 wpa_msg(group_wpa_s, MSG_INFO,
2369 "Failed to set random MAC address");
Hai Shalom74f70d42019-02-11 14:42:39 -08002370 wpa_supplicant_remove_iface(wpa_s->global, group_wpa_s,
2371 0);
Jimmy Chen36c21992018-11-29 16:46:43 +08002372 return NULL;
2373 }
2374
2375 if (wpa_supplicant_update_mac_addr(group_wpa_s) < 0) {
2376 wpa_msg(group_wpa_s, MSG_INFO,
2377 "Could not update MAC address information");
Hai Shalom74f70d42019-02-11 14:42:39 -08002378 wpa_supplicant_remove_iface(wpa_s->global, group_wpa_s,
2379 0);
Jimmy Chen36c21992018-11-29 16:46:43 +08002380 return NULL;
2381 }
2382
Hai Shalom74f70d42019-02-11 14:42:39 -08002383 wpa_printf(MSG_DEBUG, "P2P: Using random MAC address " MACSTR
2384 " for the group",
2385 MAC2STR(wpa_s->pending_interface_addr));
Jimmy Chen36c21992018-11-29 16:46:43 +08002386 }
2387
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002388 return group_wpa_s;
2389}
2390
2391
2392static void wpas_p2p_group_formation_timeout(void *eloop_ctx,
2393 void *timeout_ctx)
2394{
2395 struct wpa_supplicant *wpa_s = eloop_ctx;
2396 wpa_printf(MSG_DEBUG, "P2P: Group Formation timed out");
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07002397 wpas_p2p_group_formation_failed(wpa_s, 0);
Dmitry Shmidt34af3062013-07-11 10:46:32 -07002398}
2399
2400
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07002401static void wpas_p2p_group_formation_failed(struct wpa_supplicant *wpa_s,
2402 int already_deleted)
Dmitry Shmidt34af3062013-07-11 10:46:32 -07002403{
2404 eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
Dmitry Shmidt9c175262016-03-03 10:20:07 -08002405 wpa_s->p2pdev, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002406 if (wpa_s->global->p2p)
2407 p2p_group_formation_failed(wpa_s->global->p2p);
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07002408 wpas_group_formation_completed(wpa_s, 0, already_deleted);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002409}
2410
2411
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002412static void wpas_p2p_grpform_fail_after_wps(struct wpa_supplicant *wpa_s)
2413{
2414 wpa_printf(MSG_DEBUG, "P2P: Reject group formation due to WPS provisioning failure");
2415 eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
Dmitry Shmidt9c175262016-03-03 10:20:07 -08002416 wpa_s->p2pdev, NULL);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002417 eloop_register_timeout(0, 0, wpas_p2p_group_formation_timeout,
Dmitry Shmidt9c175262016-03-03 10:20:07 -08002418 wpa_s->p2pdev, NULL);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002419 wpa_s->global->p2p_fail_on_wps_complete = 0;
2420}
2421
2422
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07002423void wpas_p2p_ap_setup_failed(struct wpa_supplicant *wpa_s)
2424{
2425 if (wpa_s->global->p2p_group_formation != wpa_s)
2426 return;
2427 /* Speed up group formation timeout since this cannot succeed */
2428 eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
Dmitry Shmidt9c175262016-03-03 10:20:07 -08002429 wpa_s->p2pdev, NULL);
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07002430 eloop_register_timeout(0, 0, wpas_p2p_group_formation_timeout,
Dmitry Shmidt9c175262016-03-03 10:20:07 -08002431 wpa_s->p2pdev, NULL);
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07002432}
2433
2434
Matthew Wang06b42472022-11-10 06:56:31 +00002435bool wpas_p2p_retry_limit_exceeded(struct wpa_supplicant *wpa_s)
2436{
2437 if (!wpa_s->p2p_in_invitation || !wpa_s->p2p_retry_limit ||
Sunil Ravi77d572f2023-01-17 23:58:31 +00002438 wpa_s->p2p_in_invitation <= wpa_s->p2p_retry_limit)
Matthew Wang06b42472022-11-10 06:56:31 +00002439 return false;
Sunil Ravi77d572f2023-01-17 23:58:31 +00002440
Matthew Wang06b42472022-11-10 06:56:31 +00002441 wpa_printf(MSG_DEBUG, "P2P: Group join retry limit exceeded");
2442 eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
2443 wpa_s->p2pdev, NULL);
2444 eloop_register_timeout(0, 0, wpas_p2p_group_formation_timeout,
2445 wpa_s->p2pdev, NULL);
2446 return true;
2447}
2448
2449
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002450static void wpas_go_neg_completed(void *ctx, struct p2p_go_neg_results *res)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002451{
2452 struct wpa_supplicant *wpa_s = ctx;
Dmitry Shmidt9c175262016-03-03 10:20:07 -08002453 struct wpa_supplicant *group_wpa_s;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002454
2455 if (wpa_s->off_channel_freq || wpa_s->roc_waiting_drv_freq) {
2456 wpa_drv_cancel_remain_on_channel(wpa_s);
2457 wpa_s->off_channel_freq = 0;
2458 wpa_s->roc_waiting_drv_freq = 0;
2459 }
2460
2461 if (res->status) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07002462 wpa_msg_global(wpa_s, MSG_INFO,
2463 P2P_EVENT_GO_NEG_FAILURE "status=%d",
2464 res->status);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002465 wpas_notify_p2p_go_neg_completed(wpa_s, res);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002466 wpas_p2p_remove_pending_group_interface(wpa_s);
2467 return;
2468 }
2469
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002470 if (!res->role_go) {
2471 /* Inform driver of the operating channel of GO. */
2472 wpa_drv_set_prob_oper_freq(wpa_s, res->freq);
2473 }
2474
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002475 if (wpa_s->p2p_go_ht40)
2476 res->ht40 = 1;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07002477 if (wpa_s->p2p_go_vht)
2478 res->vht = 1;
Hai Shalomc3565922019-10-28 11:58:20 -07002479 if (wpa_s->p2p_go_he)
2480 res->he = 1;
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08002481 if (wpa_s->p2p_go_edmg)
2482 res->edmg = 1;
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08002483 res->max_oper_chwidth = wpa_s->p2p_go_max_oper_chwidth;
2484 res->vht_center_freq2 = wpa_s->p2p_go_vht_center_freq2;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002485
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07002486 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_GO_NEG_SUCCESS "role=%s "
2487 "freq=%d ht40=%d peer_dev=" MACSTR " peer_iface=" MACSTR
2488 " wps_method=%s",
2489 res->role_go ? "GO" : "client", res->freq, res->ht40,
2490 MAC2STR(res->peer_device_addr),
2491 MAC2STR(res->peer_interface_addr),
2492 p2p_wps_method_text(res->wps_method));
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002493 wpas_notify_p2p_go_neg_completed(wpa_s, res);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002494
Dmitry Shmidt04949592012-07-19 12:16:46 -07002495 if (res->role_go && wpa_s->p2p_persistent_id >= 0) {
2496 struct wpa_ssid *ssid;
2497 ssid = wpa_config_get_network(wpa_s->conf,
2498 wpa_s->p2p_persistent_id);
2499 if (ssid && ssid->disabled == 2 &&
2500 ssid->mode == WPAS_MODE_P2P_GO && ssid->passphrase) {
2501 size_t len = os_strlen(ssid->passphrase);
2502 wpa_printf(MSG_DEBUG, "P2P: Override passphrase based "
2503 "on requested persistent group");
2504 os_memcpy(res->passphrase, ssid->passphrase, len);
2505 res->passphrase[len] = '\0';
2506 }
2507 }
2508
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002509 if (wpa_s->create_p2p_iface) {
Dmitry Shmidt9c175262016-03-03 10:20:07 -08002510 group_wpa_s =
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002511 wpas_p2p_init_group_interface(wpa_s, res->role_go);
2512 if (group_wpa_s == NULL) {
2513 wpas_p2p_remove_pending_group_interface(wpa_s);
Dmitry Shmidtd11f0192014-03-24 12:09:47 -07002514 eloop_cancel_timeout(wpas_p2p_long_listen_timeout,
2515 wpa_s, NULL);
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07002516 wpas_p2p_group_formation_failed(wpa_s, 1);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002517 return;
2518 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002519 os_memset(wpa_s->pending_interface_addr, 0, ETH_ALEN);
2520 wpa_s->pending_interface_name[0] = '\0';
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002521 } else {
Dmitry Shmidt9c175262016-03-03 10:20:07 -08002522 group_wpa_s = wpa_s->parent;
2523 wpa_s->global->p2p_group_formation = group_wpa_s;
2524 if (group_wpa_s != wpa_s)
Dmitry Shmidt849734c2016-05-27 09:59:01 -07002525 wpas_p2p_clone_config(group_wpa_s, wpa_s);
Dmitry Shmidt9c175262016-03-03 10:20:07 -08002526 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002527
Dmitry Shmidt9c175262016-03-03 10:20:07 -08002528 group_wpa_s->p2p_in_provisioning = 1;
2529 group_wpa_s->p2pdev = wpa_s;
2530 if (group_wpa_s != wpa_s) {
2531 os_memcpy(group_wpa_s->p2p_pin, wpa_s->p2p_pin,
2532 sizeof(group_wpa_s->p2p_pin));
2533 group_wpa_s->p2p_wps_method = wpa_s->p2p_wps_method;
2534 }
2535 if (res->role_go) {
2536 wpas_start_wps_go(group_wpa_s, res, 1);
2537 } else {
2538 os_get_reltime(&group_wpa_s->scan_min_time);
2539 wpas_start_wps_enrollee(group_wpa_s, res);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002540 }
2541
Hai Shalomfdcde762020-04-02 11:19:20 -07002542 wpa_s->global->p2p_long_listen = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002543 eloop_cancel_timeout(wpas_p2p_long_listen_timeout, wpa_s, NULL);
2544
2545 eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s, NULL);
2546 eloop_register_timeout(15 + res->peer_config_timeout / 100,
2547 (res->peer_config_timeout % 100) * 10000,
2548 wpas_p2p_group_formation_timeout, wpa_s, NULL);
2549}
2550
2551
Dmitry Shmidt7a53dbb2015-06-11 13:13:53 -07002552static void wpas_go_neg_req_rx(void *ctx, const u8 *src, u16 dev_passwd_id,
2553 u8 go_intent)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002554{
2555 struct wpa_supplicant *wpa_s = ctx;
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07002556 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_GO_NEG_REQUEST MACSTR
Dmitry Shmidt7a53dbb2015-06-11 13:13:53 -07002557 " dev_passwd_id=%u go_intent=%u", MAC2STR(src),
2558 dev_passwd_id, go_intent);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002559
Dmitry Shmidt7a53dbb2015-06-11 13:13:53 -07002560 wpas_notify_p2p_go_neg_req(wpa_s, src, dev_passwd_id, go_intent);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002561}
2562
2563
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002564static void wpas_dev_found(void *ctx, const u8 *addr,
2565 const struct p2p_peer_info *info,
2566 int new_device)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002567{
Roshan Piusfd2fd662017-01-23 13:41:57 -08002568 u8 *wfd_dev_info = NULL;
2569 u8 wfd_dev_info_len = 0;
Jimmy Chen0133fc12021-03-04 13:56:11 +08002570 u8 *wfd_r2_dev_info = NULL;
Jimmy Chen57e19f52021-03-04 14:19:52 +08002571 u8 wfd_r2_dev_info_len = 0;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002572#ifndef CONFIG_NO_STDOUT_DEBUG
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002573 struct wpa_supplicant *wpa_s = ctx;
2574 char devtype[WPS_DEV_TYPE_BUFSIZE];
Dmitry Shmidt04f534e2013-12-09 15:50:16 -08002575 char *wfd_dev_info_hex = NULL;
Jimmy Chen0133fc12021-03-04 13:56:11 +08002576 char *wfd_r2_dev_info_hex = NULL;
Dmitry Shmidt04f534e2013-12-09 15:50:16 -08002577
Irfan Sheriff8367dc92012-09-09 17:08:19 -07002578#ifdef CONFIG_WIFI_DISPLAY
Dmitry Shmidt04f534e2013-12-09 15:50:16 -08002579 wfd_dev_info_hex = wifi_display_subelem_hex(info->wfd_subelems,
2580 WFD_SUBELEM_DEVICE_INFO);
Roshan Piusfd2fd662017-01-23 13:41:57 -08002581 if (wfd_dev_info_hex) {
2582 wfd_dev_info_len = strlen(wfd_dev_info_hex) / 2;
2583 wfd_dev_info = os_zalloc(wfd_dev_info_len);
2584 // Only used for notification, so not handling error.
2585 hexstr2bin(wfd_dev_info_hex, wfd_dev_info, wfd_dev_info_len);
2586 }
Jimmy Chen0133fc12021-03-04 13:56:11 +08002587
2588 wfd_r2_dev_info_hex = wifi_display_subelem_hex(info->wfd_subelems,
2589 WFD_SUBELEM_R2_DEVICE_INFO);
2590 if (wfd_r2_dev_info_hex) {
2591 wfd_r2_dev_info_len = strlen(wfd_r2_dev_info_hex) / 2;
2592 wfd_r2_dev_info = os_zalloc(wfd_r2_dev_info_len);
2593 // Only used for notification, so not handling error.
2594 hexstr2bin(wfd_r2_dev_info_hex, wfd_r2_dev_info, wfd_r2_dev_info_len);
2595 }
Irfan Sheriff8367dc92012-09-09 17:08:19 -07002596#endif /* CONFIG_WIFI_DISPLAY */
Dmitry Shmidt04f534e2013-12-09 15:50:16 -08002597
Dmitry Shmidt216983b2015-02-06 10:50:36 -08002598 if (info->p2ps_instance) {
2599 char str[256];
2600 const u8 *buf = wpabuf_head(info->p2ps_instance);
2601 size_t len = wpabuf_len(info->p2ps_instance);
2602
2603 while (len) {
2604 u32 id;
2605 u16 methods;
2606 u8 str_len;
2607
2608 if (len < 4 + 2 + 1)
2609 break;
2610 id = WPA_GET_LE32(buf);
2611 buf += sizeof(u32);
2612 methods = WPA_GET_BE16(buf);
2613 buf += sizeof(u16);
2614 str_len = *buf++;
2615 if (str_len > len - 4 - 2 - 1)
2616 break;
2617 os_memcpy(str, buf, str_len);
2618 str[str_len] = '\0';
2619 buf += str_len;
2620 len -= str_len + sizeof(u32) + sizeof(u16) + sizeof(u8);
2621
2622 wpa_msg_global(wpa_s, MSG_INFO,
2623 P2P_EVENT_DEVICE_FOUND MACSTR
2624 " p2p_dev_addr=" MACSTR
2625 " pri_dev_type=%s name='%s'"
2626 " config_methods=0x%x"
2627 " dev_capab=0x%x"
2628 " group_capab=0x%x"
2629 " adv_id=%x asp_svc=%s%s",
2630 MAC2STR(addr),
2631 MAC2STR(info->p2p_device_addr),
2632 wps_dev_type_bin2str(
2633 info->pri_dev_type,
2634 devtype, sizeof(devtype)),
2635 info->device_name, methods,
2636 info->dev_capab, info->group_capab,
2637 id, str,
2638 info->vendor_elems ?
2639 " vendor_elems=1" : "");
2640 }
2641 goto done;
2642 }
2643
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07002644 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_DEVICE_FOUND MACSTR
2645 " p2p_dev_addr=" MACSTR
2646 " pri_dev_type=%s name='%s' config_methods=0x%x "
Jimmy Chen0133fc12021-03-04 13:56:11 +08002647 "dev_capab=0x%x group_capab=0x%x%s%s%s%s%s new=%d",
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07002648 MAC2STR(addr), MAC2STR(info->p2p_device_addr),
2649 wps_dev_type_bin2str(info->pri_dev_type, devtype,
2650 sizeof(devtype)),
2651 info->device_name, info->config_methods,
2652 info->dev_capab, info->group_capab,
Dmitry Shmidt04f534e2013-12-09 15:50:16 -08002653 wfd_dev_info_hex ? " wfd_dev_info=0x" : "",
Dmitry Shmidt2e67f062014-07-16 09:55:28 -07002654 wfd_dev_info_hex ? wfd_dev_info_hex : "",
Jimmy Chen0133fc12021-03-04 13:56:11 +08002655 wfd_r2_dev_info_hex ? " wfd_r2_dev_info=0x" : "",
2656 wfd_r2_dev_info_hex ? wfd_r2_dev_info_hex : "",
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002657 info->vendor_elems ? " vendor_elems=1" : "",
2658 new_device);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002659
Dmitry Shmidt216983b2015-02-06 10:50:36 -08002660done:
Dmitry Shmidt04f534e2013-12-09 15:50:16 -08002661 os_free(wfd_dev_info_hex);
Jimmy Chen0133fc12021-03-04 13:56:11 +08002662 os_free(wfd_r2_dev_info_hex);
Dmitry Shmidt97672262014-02-03 13:02:54 -08002663#endif /* CONFIG_NO_STDOUT_DEBUG */
Dmitry Shmidt04f534e2013-12-09 15:50:16 -08002664
Roshan Piusfd2fd662017-01-23 13:41:57 -08002665 wpas_notify_p2p_device_found(ctx, addr, info, wfd_dev_info,
Jimmy Chen0133fc12021-03-04 13:56:11 +08002666 wfd_dev_info_len, wfd_r2_dev_info,
2667 wfd_r2_dev_info_len, new_device);
Roshan Piusfd2fd662017-01-23 13:41:57 -08002668 os_free(wfd_dev_info);
jiangpingpinga96a5e82023-11-16 15:02:17 +08002669 os_free(wfd_r2_dev_info);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002670}
2671
2672
2673static void wpas_dev_lost(void *ctx, const u8 *dev_addr)
2674{
2675 struct wpa_supplicant *wpa_s = ctx;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002676
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07002677 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_DEVICE_LOST
2678 "p2p_dev_addr=" MACSTR, MAC2STR(dev_addr));
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002679
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002680 wpas_notify_p2p_device_lost(wpa_s, dev_addr);
2681}
2682
2683
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07002684static void wpas_find_stopped(void *ctx)
2685{
2686 struct wpa_supplicant *wpa_s = ctx;
Dmitry Shmidt9c175262016-03-03 10:20:07 -08002687
2688 if (wpa_s->p2p_scan_work && wpas_abort_ongoing_scan(wpa_s) < 0)
2689 wpa_printf(MSG_DEBUG, "P2P: Abort ongoing scan failed");
2690
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07002691 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_FIND_STOPPED);
Dmitry Shmidt8bd70b72015-05-26 16:02:19 -07002692 wpas_notify_p2p_find_stopped(wpa_s);
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07002693}
2694
2695
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002696struct wpas_p2p_listen_work {
2697 unsigned int freq;
2698 unsigned int duration;
2699 struct wpabuf *probe_resp_ie;
2700};
2701
2702
2703static void wpas_p2p_listen_work_free(struct wpas_p2p_listen_work *lwork)
2704{
2705 if (lwork == NULL)
2706 return;
2707 wpabuf_free(lwork->probe_resp_ie);
2708 os_free(lwork);
2709}
2710
2711
2712static void wpas_p2p_listen_work_done(struct wpa_supplicant *wpa_s)
2713{
2714 struct wpas_p2p_listen_work *lwork;
2715
2716 if (!wpa_s->p2p_listen_work)
2717 return;
2718
2719 lwork = wpa_s->p2p_listen_work->ctx;
2720 wpas_p2p_listen_work_free(lwork);
2721 radio_work_done(wpa_s->p2p_listen_work);
2722 wpa_s->p2p_listen_work = NULL;
2723}
2724
2725
2726static void wpas_start_listen_cb(struct wpa_radio_work *work, int deinit)
2727{
2728 struct wpa_supplicant *wpa_s = work->wpa_s;
2729 struct wpas_p2p_listen_work *lwork = work->ctx;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002730 unsigned int duration;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002731
2732 if (deinit) {
Dmitry Shmidtbd14a572014-02-18 10:33:49 -08002733 if (work->started) {
2734 wpa_s->p2p_listen_work = NULL;
2735 wpas_stop_listen(wpa_s);
2736 }
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002737 wpas_p2p_listen_work_free(lwork);
2738 return;
2739 }
2740
2741 wpa_s->p2p_listen_work = work;
2742
2743 wpa_drv_set_ap_wps_ie(wpa_s, NULL, lwork->probe_resp_ie, NULL);
2744
2745 if (wpa_drv_probe_req_report(wpa_s, 1) < 0) {
2746 wpa_printf(MSG_DEBUG, "P2P: Failed to request the driver to "
2747 "report received Probe Request frames");
2748 wpas_p2p_listen_work_done(wpa_s);
2749 return;
2750 }
2751
2752 wpa_s->pending_listen_freq = lwork->freq;
2753 wpa_s->pending_listen_duration = lwork->duration;
2754
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002755 duration = lwork->duration;
2756#ifdef CONFIG_TESTING_OPTIONS
2757 if (wpa_s->extra_roc_dur) {
2758 wpa_printf(MSG_DEBUG, "TESTING: Increase ROC duration %u -> %u",
2759 duration, duration + wpa_s->extra_roc_dur);
2760 duration += wpa_s->extra_roc_dur;
2761 }
2762#endif /* CONFIG_TESTING_OPTIONS */
2763
2764 if (wpa_drv_remain_on_channel(wpa_s, lwork->freq, duration) < 0) {
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002765 wpa_printf(MSG_DEBUG, "P2P: Failed to request the driver "
2766 "to remain on channel (%u MHz) for Listen "
2767 "state", lwork->freq);
2768 wpas_p2p_listen_work_done(wpa_s);
2769 wpa_s->pending_listen_freq = 0;
2770 return;
2771 }
2772 wpa_s->off_channel_freq = 0;
2773 wpa_s->roc_waiting_drv_freq = lwork->freq;
2774}
2775
2776
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002777static int wpas_start_listen(void *ctx, unsigned int freq,
2778 unsigned int duration,
2779 const struct wpabuf *probe_resp_ie)
2780{
2781 struct wpa_supplicant *wpa_s = ctx;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002782 struct wpas_p2p_listen_work *lwork;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002783
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002784 if (wpa_s->p2p_listen_work) {
2785 wpa_printf(MSG_DEBUG, "P2P: Reject start_listen since p2p_listen_work already exists");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002786 return -1;
2787 }
2788
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002789 lwork = os_zalloc(sizeof(*lwork));
2790 if (lwork == NULL)
2791 return -1;
2792 lwork->freq = freq;
2793 lwork->duration = duration;
2794 if (probe_resp_ie) {
2795 lwork->probe_resp_ie = wpabuf_dup(probe_resp_ie);
2796 if (lwork->probe_resp_ie == NULL) {
2797 wpas_p2p_listen_work_free(lwork);
2798 return -1;
2799 }
2800 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002801
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002802 if (radio_add_work(wpa_s, freq, "p2p-listen", 0, wpas_start_listen_cb,
2803 lwork) < 0) {
2804 wpas_p2p_listen_work_free(lwork);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002805 return -1;
2806 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002807
2808 return 0;
2809}
2810
2811
2812static void wpas_stop_listen(void *ctx)
2813{
2814 struct wpa_supplicant *wpa_s = ctx;
2815 if (wpa_s->off_channel_freq || wpa_s->roc_waiting_drv_freq) {
2816 wpa_drv_cancel_remain_on_channel(wpa_s);
2817 wpa_s->off_channel_freq = 0;
2818 wpa_s->roc_waiting_drv_freq = 0;
2819 }
2820 wpa_drv_set_ap_wps_ie(wpa_s, NULL, NULL, NULL);
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07002821
2822 /*
2823 * Don't cancel Probe Request RX reporting for a connected P2P Client
2824 * handling Probe Request frames.
2825 */
2826 if (!wpa_s->p2p_cli_probe)
2827 wpa_drv_probe_req_report(wpa_s, 0);
2828
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002829 wpas_p2p_listen_work_done(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002830}
2831
2832
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07002833static int wpas_send_probe_resp(void *ctx, const struct wpabuf *buf,
2834 unsigned int freq)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002835{
2836 struct wpa_supplicant *wpa_s = ctx;
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07002837 return wpa_drv_send_mlme(wpa_s, wpabuf_head(buf), wpabuf_len(buf), 1,
Hai Shalomfdcde762020-04-02 11:19:20 -07002838 freq, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002839}
2840
2841
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002842static void wpas_prov_disc_local_display(struct wpa_supplicant *wpa_s,
2843 const u8 *peer, const char *params,
2844 unsigned int generated_pin)
2845{
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07002846 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_SHOW_PIN MACSTR
2847 " %08d%s", MAC2STR(peer), generated_pin, params);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002848}
2849
2850
2851static void wpas_prov_disc_local_keypad(struct wpa_supplicant *wpa_s,
2852 const u8 *peer, const char *params)
2853{
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07002854 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_ENTER_PIN MACSTR
2855 "%s", MAC2STR(peer), params);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002856}
2857
2858
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002859static void wpas_prov_disc_req(void *ctx, const u8 *peer, u16 config_methods,
2860 const u8 *dev_addr, const u8 *pri_dev_type,
2861 const char *dev_name, u16 supp_config_methods,
2862 u8 dev_capab, u8 group_capab, const u8 *group_id,
2863 size_t group_id_len)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002864{
2865 struct wpa_supplicant *wpa_s = ctx;
2866 char devtype[WPS_DEV_TYPE_BUFSIZE];
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002867 char params[300];
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002868 u8 empty_dev_type[8];
2869 unsigned int generated_pin = 0;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002870 struct wpa_supplicant *group = NULL;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002871 int res;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002872
2873 if (group_id) {
2874 for (group = wpa_s->global->ifaces; group; group = group->next)
2875 {
2876 struct wpa_ssid *s = group->current_ssid;
2877 if (s != NULL &&
2878 s->mode == WPAS_MODE_P2P_GO &&
2879 group_id_len - ETH_ALEN == s->ssid_len &&
2880 os_memcmp(group_id + ETH_ALEN, s->ssid,
2881 s->ssid_len) == 0)
2882 break;
2883 }
2884 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002885
2886 if (pri_dev_type == NULL) {
2887 os_memset(empty_dev_type, 0, sizeof(empty_dev_type));
2888 pri_dev_type = empty_dev_type;
2889 }
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002890 res = os_snprintf(params, sizeof(params), " p2p_dev_addr=" MACSTR
2891 " pri_dev_type=%s name='%s' config_methods=0x%x "
2892 "dev_capab=0x%x group_capab=0x%x%s%s",
2893 MAC2STR(dev_addr),
2894 wps_dev_type_bin2str(pri_dev_type, devtype,
2895 sizeof(devtype)),
2896 dev_name, supp_config_methods, dev_capab, group_capab,
2897 group ? " group=" : "",
2898 group ? group->ifname : "");
2899 if (os_snprintf_error(sizeof(params), res))
2900 wpa_printf(MSG_DEBUG, "P2P: PD Request event truncated");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002901 params[sizeof(params) - 1] = '\0';
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08002902
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002903 if (config_methods & WPS_CONFIG_DISPLAY) {
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08002904 if (wps_generate_pin(&generated_pin) < 0) {
2905 wpa_printf(MSG_DEBUG, "P2P: Could not generate PIN");
2906 wpas_notify_p2p_provision_discovery(
2907 wpa_s, peer, 0 /* response */,
Sunil Ravi8bae4252023-11-16 03:05:19 +00002908 P2P_PROV_DISC_INFO_UNAVAILABLE, 0, 0, NULL);
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08002909 return;
2910 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002911 wpas_prov_disc_local_display(wpa_s, peer, params,
2912 generated_pin);
2913 } else if (config_methods & WPS_CONFIG_KEYPAD)
2914 wpas_prov_disc_local_keypad(wpa_s, peer, params);
2915 else if (config_methods & WPS_CONFIG_PUSHBUTTON)
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07002916 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_PBC_REQ
2917 MACSTR "%s", MAC2STR(peer), params);
Jouni Malinen75ecf522011-06-27 15:19:46 -07002918
2919 wpas_notify_p2p_provision_discovery(wpa_s, peer, 1 /* request */,
2920 P2P_PROV_DISC_SUCCESS,
Sunil Ravi8bae4252023-11-16 03:05:19 +00002921 config_methods, generated_pin,
2922 group ? group->ifname : NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002923}
2924
2925
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002926static void wpas_prov_disc_resp(void *ctx, const u8 *peer, u16 config_methods)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002927{
2928 struct wpa_supplicant *wpa_s = ctx;
2929 unsigned int generated_pin = 0;
Dmitry Shmidt04949592012-07-19 12:16:46 -07002930 char params[20];
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002931
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08002932 if (wpa_s->pending_pd_before_join &&
2933 (os_memcmp(peer, wpa_s->pending_join_dev_addr, ETH_ALEN) == 0 ||
2934 os_memcmp(peer, wpa_s->pending_join_iface_addr, ETH_ALEN) == 0)) {
2935 wpa_s->pending_pd_before_join = 0;
2936 wpa_printf(MSG_DEBUG, "P2P: Starting pending "
2937 "join-existing-group operation");
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08002938 wpas_p2p_join_start(wpa_s, 0, NULL, 0);
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08002939 return;
2940 }
2941
Dmitry Shmidt04949592012-07-19 12:16:46 -07002942 if (wpa_s->pending_pd_use == AUTO_PD_JOIN ||
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002943 wpa_s->pending_pd_use == AUTO_PD_GO_NEG) {
2944 int res;
2945
2946 res = os_snprintf(params, sizeof(params), " peer_go=%d",
2947 wpa_s->pending_pd_use == AUTO_PD_JOIN);
2948 if (os_snprintf_error(sizeof(params), res))
2949 params[sizeof(params) - 1] = '\0';
2950 } else
Dmitry Shmidt04949592012-07-19 12:16:46 -07002951 params[0] = '\0';
2952
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002953 if (config_methods & WPS_CONFIG_DISPLAY)
Dmitry Shmidt04949592012-07-19 12:16:46 -07002954 wpas_prov_disc_local_keypad(wpa_s, peer, params);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002955 else if (config_methods & WPS_CONFIG_KEYPAD) {
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08002956 if (wps_generate_pin(&generated_pin) < 0) {
2957 wpa_printf(MSG_DEBUG, "P2P: Could not generate PIN");
2958 wpas_notify_p2p_provision_discovery(
2959 wpa_s, peer, 0 /* response */,
Sunil Ravi8bae4252023-11-16 03:05:19 +00002960 P2P_PROV_DISC_INFO_UNAVAILABLE, 0, 0, NULL);
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08002961 return;
2962 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07002963 wpas_prov_disc_local_display(wpa_s, peer, params,
2964 generated_pin);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002965 } else if (config_methods & WPS_CONFIG_PUSHBUTTON)
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07002966 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_PBC_RESP
2967 MACSTR "%s", MAC2STR(peer), params);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002968
Jouni Malinen75ecf522011-06-27 15:19:46 -07002969 wpas_notify_p2p_provision_discovery(wpa_s, peer, 0 /* response */,
2970 P2P_PROV_DISC_SUCCESS,
Sunil Ravi8bae4252023-11-16 03:05:19 +00002971 config_methods, generated_pin,
2972 NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002973}
2974
2975
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002976static void wpas_prov_disc_fail(void *ctx, const u8 *peer,
Dmitry Shmidt216983b2015-02-06 10:50:36 -08002977 enum p2p_prov_disc_status status,
2978 u32 adv_id, const u8 *adv_mac,
2979 const char *deferred_session_resp)
Jouni Malinen75ecf522011-06-27 15:19:46 -07002980{
2981 struct wpa_supplicant *wpa_s = ctx;
2982
Dmitry Shmidt04949592012-07-19 12:16:46 -07002983 if (wpa_s->p2p_fallback_to_go_neg) {
2984 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: PD for p2p_connect-auto "
2985 "failed - fall back to GO Negotiation");
Dmitry Shmidt9c175262016-03-03 10:20:07 -08002986 wpa_msg_global(wpa_s->p2pdev, MSG_INFO,
Dmitry Shmidt7f656022015-02-25 14:36:37 -08002987 P2P_EVENT_FALLBACK_TO_GO_NEG
2988 "reason=PD-failed");
Dmitry Shmidt04949592012-07-19 12:16:46 -07002989 wpas_p2p_fallback_to_go_neg(wpa_s, 0);
2990 return;
2991 }
2992
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002993 if (status == P2P_PROV_DISC_TIMEOUT_JOIN) {
Dmitry Shmidt2b380482012-09-13 10:52:13 -07002994 wpa_s->pending_pd_before_join = 0;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002995 wpa_printf(MSG_DEBUG, "P2P: Starting pending "
2996 "join-existing-group operation (no ACK for PD "
2997 "Req attempts)");
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08002998 wpas_p2p_join_start(wpa_s, 0, NULL, 0);
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002999 return;
Dmitry Shmidt2b380482012-09-13 10:52:13 -07003000 }
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08003001
Dmitry Shmidt216983b2015-02-06 10:50:36 -08003002 if (adv_id && adv_mac && deferred_session_resp) {
3003 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_FAILURE
3004 " p2p_dev_addr=" MACSTR " status=%d adv_id=%x"
3005 " deferred_session_resp='%s'",
3006 MAC2STR(peer), status, adv_id,
3007 deferred_session_resp);
3008 } else if (adv_id && adv_mac) {
3009 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_FAILURE
3010 " p2p_dev_addr=" MACSTR " status=%d adv_id=%x",
3011 MAC2STR(peer), status, adv_id);
3012 } else {
3013 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_FAILURE
3014 " p2p_dev_addr=" MACSTR " status=%d",
3015 MAC2STR(peer), status);
3016 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07003017
Jouni Malinen75ecf522011-06-27 15:19:46 -07003018 wpas_notify_p2p_provision_discovery(wpa_s, peer, 0 /* response */,
Sunil Ravi8bae4252023-11-16 03:05:19 +00003019 status, 0, 0, NULL);
Jouni Malinen75ecf522011-06-27 15:19:46 -07003020}
3021
3022
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003023static int freq_included(struct wpa_supplicant *wpa_s,
3024 const struct p2p_channels *channels,
3025 unsigned int freq)
Dmitry Shmidtf7e0a992013-05-23 11:03:10 -07003026{
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003027 if ((channels == NULL || p2p_channels_includes_freq(channels, freq)) &&
3028 wpas_p2p_go_is_peer_freq(wpa_s, freq))
3029 return 1;
3030 return 0;
3031}
Dmitry Shmidtf7e0a992013-05-23 11:03:10 -07003032
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003033
3034static void wpas_p2p_go_update_common_freqs(struct wpa_supplicant *wpa_s)
3035{
3036 unsigned int num = P2P_MAX_CHANNELS;
3037 int *common_freqs;
3038 int ret;
3039
3040 p2p_go_dump_common_freqs(wpa_s);
3041 common_freqs = os_calloc(num, sizeof(int));
3042 if (!common_freqs)
3043 return;
3044
3045 ret = p2p_group_get_common_freqs(wpa_s->p2p_group, common_freqs, &num);
3046 if (ret < 0) {
3047 wpa_dbg(wpa_s, MSG_DEBUG,
3048 "P2P: Failed to get group common freqs");
3049 os_free(common_freqs);
3050 return;
3051 }
3052
3053 os_free(wpa_s->p2p_group_common_freqs);
3054 wpa_s->p2p_group_common_freqs = common_freqs;
3055 wpa_s->p2p_group_common_freqs_num = num;
3056 p2p_go_dump_common_freqs(wpa_s);
3057}
3058
3059
3060/*
3061 * Check if the given frequency is one of the possible operating frequencies
3062 * set after the completion of the GO Negotiation.
3063 */
3064static int wpas_p2p_go_is_peer_freq(struct wpa_supplicant *wpa_s, int freq)
3065{
3066 unsigned int i;
3067
3068 p2p_go_dump_common_freqs(wpa_s);
3069
3070 /* assume no restrictions */
3071 if (!wpa_s->p2p_group_common_freqs_num)
3072 return 1;
3073
3074 for (i = 0; i < wpa_s->p2p_group_common_freqs_num; i++) {
3075 if (wpa_s->p2p_group_common_freqs[i] == freq)
3076 return 1;
3077 }
3078 return 0;
3079}
3080
3081
3082static int wpas_sta_check_ecsa(struct hostapd_data *hapd,
3083 struct sta_info *sta, void *ctx)
3084{
3085 int *ecsa_support = ctx;
3086
3087 *ecsa_support &= sta->ecsa_supported;
3088
3089 return 0;
3090}
3091
3092
3093/* Check if all the peers support eCSA */
3094static int wpas_p2p_go_clients_support_ecsa(struct wpa_supplicant *wpa_s)
3095{
3096 int ecsa_support = 1;
3097
3098 ap_for_each_sta(wpa_s->ap_iface->bss[0], wpas_sta_check_ecsa,
3099 &ecsa_support);
3100
3101 return ecsa_support;
Dmitry Shmidtf7e0a992013-05-23 11:03:10 -07003102}
3103
3104
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07003105/**
3106 * Pick the best frequency to use from all the currently used frequencies.
3107 */
3108static int wpas_p2p_pick_best_used_freq(struct wpa_supplicant *wpa_s,
3109 struct wpa_used_freq_data *freqs,
3110 unsigned int num)
3111{
3112 unsigned int i, c;
3113
3114 /* find a candidate freq that is supported by P2P */
3115 for (c = 0; c < num; c++)
3116 if (p2p_supported_freq(wpa_s->global->p2p, freqs[c].freq))
3117 break;
3118
3119 if (c == num)
3120 return 0;
3121
3122 /* once we have a candidate, try to find a 'better' one */
3123 for (i = c + 1; i < num; i++) {
3124 if (!p2p_supported_freq(wpa_s->global->p2p, freqs[i].freq))
3125 continue;
3126
3127 /*
3128 * 1. Infrastructure station interfaces have higher preference.
3129 * 2. P2P Clients have higher preference.
3130 * 3. All others.
3131 */
3132 if (freqs[i].flags & WPA_FREQ_USED_BY_INFRA_STATION) {
3133 c = i;
3134 break;
3135 }
3136
3137 if ((freqs[i].flags & WPA_FREQ_USED_BY_P2P_CLIENT))
3138 c = i;
3139 }
3140 return freqs[c].freq;
3141}
3142
3143
Sunil Ravi036cec52023-03-29 11:35:17 -07003144/**
3145 * Pick the best frequency the driver suggests.
3146 *
3147 * num_pref_freq is used as both input and output
3148 * - input: the max size of pref_freq_list,
3149 * - output: the valid size of pref_freq_list filled with data.
3150 */
3151static int wpas_p2p_pick_best_pref_freq(struct wpa_supplicant *wpa_s, bool go,
3152 struct weighted_pcl *pref_freq_list,
3153 unsigned int *num_pref_freq)
3154{
3155 int best_freq = 0;
3156 unsigned int max_pref_freq, i;
3157 int res;
3158 enum wpa_driver_if_type iface_type;
3159
3160 max_pref_freq = *num_pref_freq;
3161 *num_pref_freq = 0;
3162
3163 if (go)
3164 iface_type = WPA_IF_P2P_GO;
3165 else
3166 iface_type = WPA_IF_P2P_CLIENT;
3167
3168 res = wpa_drv_get_pref_freq_list(wpa_s, iface_type, &max_pref_freq,
3169 pref_freq_list);
3170 if (!res && !is_p2p_allow_6ghz(wpa_s->global->p2p))
3171 max_pref_freq = p2p_remove_6ghz_channels(pref_freq_list,
3172 max_pref_freq);
3173 if (res || !max_pref_freq) {
3174 wpa_printf(MSG_DEBUG,
3175 "P2P: No preferred frequency list available");
3176 return 0;
3177 }
3178
3179 *num_pref_freq = max_pref_freq;
3180 i = 0;
3181 while (i < *num_pref_freq &&
3182 (!p2p_supported_freq(wpa_s->global->p2p,
3183 pref_freq_list[i].freq) ||
3184 wpas_p2p_disallowed_freq(wpa_s->global,
3185 pref_freq_list[i].freq) ||
3186 !p2p_pref_freq_allowed(&pref_freq_list[i], go))) {
3187 wpa_printf(MSG_DEBUG,
3188 "P2P: preferred_freq_list[%d]=%d is disallowed",
3189 i, pref_freq_list[i].freq);
3190 i++;
3191 }
3192 if (i != *num_pref_freq) {
3193 best_freq = pref_freq_list[i].freq;
3194 wpa_printf(MSG_DEBUG, "P2P: Using preferred_freq_list[%d]=%d",
3195 i, best_freq);
3196 } else {
3197 wpa_printf(MSG_DEBUG,
3198 "P2P: All driver preferred frequencies are disallowed for P2P use");
3199 *num_pref_freq = 0;
3200 }
3201
3202 return best_freq;
3203}
3204
3205
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003206static u8 wpas_invitation_process(void *ctx, const u8 *sa, const u8 *bssid,
3207 const u8 *go_dev_addr, const u8 *ssid,
3208 size_t ssid_len, int *go, u8 *group_bssid,
Dmitry Shmidtf7e0a992013-05-23 11:03:10 -07003209 int *force_freq, int persistent_group,
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08003210 const struct p2p_channels *channels,
3211 int dev_pw_id)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003212{
3213 struct wpa_supplicant *wpa_s = ctx;
3214 struct wpa_ssid *s;
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07003215 struct wpa_used_freq_data *freqs;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003216 struct wpa_supplicant *grp;
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07003217 int best_freq;
Sunil Ravi036cec52023-03-29 11:35:17 -07003218 struct weighted_pcl pref_freq_list[P2P_MAX_PREF_CHANNELS];
3219 unsigned int num_pref_freq;
3220 int res;
3221
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003222
3223 if (!persistent_group) {
3224 wpa_printf(MSG_DEBUG, "P2P: Invitation from " MACSTR
Dmitry Shmidt344abd32014-01-14 13:17:00 -08003225 " to join an active group (SSID: %s)",
3226 MAC2STR(sa), wpa_ssid_txt(ssid, ssid_len));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003227 if (!is_zero_ether_addr(wpa_s->p2p_auth_invite) &&
3228 (os_memcmp(go_dev_addr, wpa_s->p2p_auth_invite, ETH_ALEN)
3229 == 0 ||
3230 os_memcmp(sa, wpa_s->p2p_auth_invite, ETH_ALEN) == 0)) {
3231 wpa_printf(MSG_DEBUG, "P2P: Accept previously "
3232 "authorized invitation");
3233 goto accept_inv;
3234 }
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08003235
3236#ifdef CONFIG_WPS_NFC
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08003237 if (dev_pw_id >= 0 && wpa_s->p2p_nfc_tag_enabled &&
3238 dev_pw_id == wpa_s->p2p_oob_dev_pw_id) {
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08003239 wpa_printf(MSG_DEBUG, "P2P: Accept invitation based on local enabled NFC Tag");
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08003240 wpa_s->p2p_wps_method = WPS_NFC;
3241 wpa_s->pending_join_wps_method = WPS_NFC;
3242 os_memcpy(wpa_s->pending_join_dev_addr,
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08003243 go_dev_addr, ETH_ALEN);
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08003244 os_memcpy(wpa_s->pending_join_iface_addr,
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08003245 bssid, ETH_ALEN);
3246 goto accept_inv;
3247 }
3248#endif /* CONFIG_WPS_NFC */
3249
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003250 /*
3251 * Do not accept the invitation automatically; notify user and
3252 * request approval.
3253 */
3254 return P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE;
3255 }
3256
3257 grp = wpas_get_p2p_group(wpa_s, ssid, ssid_len, go);
3258 if (grp) {
3259 wpa_printf(MSG_DEBUG, "P2P: Accept invitation to already "
3260 "running persistent group");
3261 if (*go)
3262 os_memcpy(group_bssid, grp->own_addr, ETH_ALEN);
3263 goto accept_inv;
3264 }
3265
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08003266 if (!is_zero_ether_addr(wpa_s->p2p_auth_invite) &&
3267 os_memcmp(sa, wpa_s->p2p_auth_invite, ETH_ALEN) == 0) {
3268 wpa_printf(MSG_DEBUG, "P2P: Accept previously initiated "
3269 "invitation to re-invoke a persistent group");
Dmitry Shmidt76cd2cc2014-05-27 12:56:04 -07003270 os_memset(wpa_s->p2p_auth_invite, 0, ETH_ALEN);
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08003271 } else if (!wpa_s->conf->persistent_reconnect)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003272 return P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE;
3273
3274 for (s = wpa_s->conf->ssid; s; s = s->next) {
3275 if (s->disabled == 2 &&
3276 os_memcmp(s->bssid, go_dev_addr, ETH_ALEN) == 0 &&
3277 s->ssid_len == ssid_len &&
3278 os_memcmp(ssid, s->ssid, ssid_len) == 0)
3279 break;
3280 }
3281
3282 if (!s) {
3283 wpa_printf(MSG_DEBUG, "P2P: Invitation from " MACSTR
3284 " requested reinvocation of an unknown group",
3285 MAC2STR(sa));
3286 return P2P_SC_FAIL_UNKNOWN_GROUP;
3287 }
3288
3289 if (s->mode == WPAS_MODE_P2P_GO && !wpas_p2p_create_iface(wpa_s)) {
3290 *go = 1;
3291 if (wpa_s->wpa_state >= WPA_AUTHENTICATING) {
3292 wpa_printf(MSG_DEBUG, "P2P: The only available "
3293 "interface is already in use - reject "
3294 "invitation");
3295 return P2P_SC_FAIL_UNABLE_TO_ACCOMMODATE;
3296 }
Dmitry Shmidt9c175262016-03-03 10:20:07 -08003297 if (wpa_s->p2p_mgmt)
3298 os_memcpy(group_bssid, wpa_s->parent->own_addr,
3299 ETH_ALEN);
3300 else
3301 os_memcpy(group_bssid, wpa_s->own_addr, ETH_ALEN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003302 } else if (s->mode == WPAS_MODE_P2P_GO) {
3303 *go = 1;
3304 if (wpas_p2p_add_group_interface(wpa_s, WPA_IF_P2P_GO) < 0)
3305 {
3306 wpa_printf(MSG_ERROR, "P2P: Failed to allocate a new "
3307 "interface address for the group");
3308 return P2P_SC_FAIL_UNABLE_TO_ACCOMMODATE;
3309 }
3310 os_memcpy(group_bssid, wpa_s->pending_interface_addr,
3311 ETH_ALEN);
3312 }
3313
3314accept_inv:
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003315 wpas_p2p_set_own_freq_preference(wpa_s, 0);
3316
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07003317 best_freq = 0;
3318 freqs = os_calloc(wpa_s->num_multichan_concurrent,
3319 sizeof(struct wpa_used_freq_data));
3320 if (freqs) {
3321 int num_channels = wpa_s->num_multichan_concurrent;
3322 int num = wpas_p2p_valid_oper_freqs(wpa_s, freqs, num_channels);
3323 best_freq = wpas_p2p_pick_best_used_freq(wpa_s, freqs, num);
3324 os_free(freqs);
3325 }
3326
Sunil Ravi036cec52023-03-29 11:35:17 -07003327 num_pref_freq = P2P_MAX_PREF_CHANNELS;
3328 res = wpas_p2p_pick_best_pref_freq(wpa_s, *go, pref_freq_list,
3329 &num_pref_freq);
3330 if (res > 0)
3331 best_freq = res;
3332
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07003333 /* Get one of the frequencies currently in use */
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07003334 if (best_freq > 0) {
Dmitry Shmidta0d265f2013-11-19 13:13:41 -08003335 wpa_printf(MSG_DEBUG, "P2P: Trying to prefer a channel already used by one of the interfaces");
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07003336 wpas_p2p_set_own_freq_preference(wpa_s, best_freq);
Dmitry Shmidta0d265f2013-11-19 13:13:41 -08003337
3338 if (wpa_s->num_multichan_concurrent < 2 ||
3339 wpas_p2p_num_unused_channels(wpa_s) < 1) {
3340 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 -07003341 *force_freq = best_freq;
Dmitry Shmidta0d265f2013-11-19 13:13:41 -08003342 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003343 }
3344
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07003345 if (*force_freq > 0 && wpa_s->num_multichan_concurrent > 1 &&
3346 wpas_p2p_num_unused_channels(wpa_s) > 0) {
Dmitry Shmidtf7e0a992013-05-23 11:03:10 -07003347 if (*go == 0) {
3348 /* We are the client */
3349 wpa_printf(MSG_DEBUG, "P2P: Peer was found to be "
3350 "running a GO but we are capable of MCC, "
3351 "figure out the best channel to use");
3352 *force_freq = 0;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003353 } else if (!freq_included(wpa_s, channels, *force_freq)) {
Dmitry Shmidtf7e0a992013-05-23 11:03:10 -07003354 /* We are the GO, and *force_freq is not in the
3355 * intersection */
3356 wpa_printf(MSG_DEBUG, "P2P: Forced GO freq %d MHz not "
3357 "in intersection but we are capable of MCC, "
3358 "figure out the best channel to use",
3359 *force_freq);
3360 *force_freq = 0;
3361 }
3362 }
3363
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003364 return P2P_SC_SUCCESS;
3365}
3366
3367
3368static void wpas_invitation_received(void *ctx, const u8 *sa, const u8 *bssid,
3369 const u8 *ssid, size_t ssid_len,
3370 const u8 *go_dev_addr, u8 status,
3371 int op_freq)
3372{
3373 struct wpa_supplicant *wpa_s = ctx;
3374 struct wpa_ssid *s;
3375
3376 for (s = wpa_s->conf->ssid; s; s = s->next) {
3377 if (s->disabled == 2 &&
3378 s->ssid_len == ssid_len &&
3379 os_memcmp(ssid, s->ssid, ssid_len) == 0)
3380 break;
3381 }
3382
3383 if (status == P2P_SC_SUCCESS) {
3384 wpa_printf(MSG_DEBUG, "P2P: Invitation from peer " MACSTR
Dmitry Shmidt344abd32014-01-14 13:17:00 -08003385 " was accepted; op_freq=%d MHz, SSID=%s",
3386 MAC2STR(sa), op_freq, wpa_ssid_txt(ssid, ssid_len));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003387 if (s) {
Dmitry Shmidt91c40cd2012-09-25 14:23:53 -07003388 int go = s->mode == WPAS_MODE_P2P_GO;
Dmitry Shmidt849734c2016-05-27 09:59:01 -07003389 if (go) {
3390 wpa_msg_global(wpa_s, MSG_INFO,
3391 P2P_EVENT_INVITATION_ACCEPTED
3392 "sa=" MACSTR
3393 " persistent=%d freq=%d",
3394 MAC2STR(sa), s->id, op_freq);
3395 } else {
3396 wpa_msg_global(wpa_s, MSG_INFO,
3397 P2P_EVENT_INVITATION_ACCEPTED
3398 "sa=" MACSTR
3399 " persistent=%d",
3400 MAC2STR(sa), s->id);
3401 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003402 wpas_p2p_group_add_persistent(
Hai Shalomb755a2a2020-04-23 21:49:02 -07003403 wpa_s, s, go, 0, op_freq, 0,
3404 wpa_s->conf->p2p_go_ht40,
3405 wpa_s->conf->p2p_go_vht,
3406 0,
3407 wpa_s->conf->p2p_go_he,
3408 wpa_s->conf->p2p_go_edmg, NULL,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003409 go ? P2P_MAX_INITIAL_CONN_WAIT_GO_REINVOKE : 0,
Matthew Wang124e5f42022-12-29 07:23:06 +00003410 1, is_p2p_allow_6ghz(wpa_s->global->p2p), 0,
Sunil Ravi036cec52023-03-29 11:35:17 -07003411 NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003412 } else if (bssid) {
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08003413 wpa_s->user_initiated_pd = 0;
Dmitry Shmidt849734c2016-05-27 09:59:01 -07003414 wpa_msg_global(wpa_s, MSG_INFO,
3415 P2P_EVENT_INVITATION_ACCEPTED
3416 "sa=" MACSTR " go_dev_addr=" MACSTR
3417 " bssid=" MACSTR " unknown-network",
3418 MAC2STR(sa), MAC2STR(go_dev_addr),
3419 MAC2STR(bssid));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003420 wpas_p2p_join(wpa_s, bssid, go_dev_addr,
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08003421 wpa_s->p2p_wps_method, 0, op_freq,
Dmitry Shmidt344abd32014-01-14 13:17:00 -08003422 ssid, ssid_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003423 }
3424 return;
3425 }
3426
3427 if (status != P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE) {
3428 wpa_printf(MSG_DEBUG, "P2P: Invitation from peer " MACSTR
3429 " was rejected (status %u)", MAC2STR(sa), status);
3430 return;
3431 }
3432
3433 if (!s) {
3434 if (bssid) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07003435 wpa_msg_global(wpa_s, MSG_INFO,
3436 P2P_EVENT_INVITATION_RECEIVED
3437 "sa=" MACSTR " go_dev_addr=" MACSTR
3438 " bssid=" MACSTR " unknown-network",
3439 MAC2STR(sa), MAC2STR(go_dev_addr),
3440 MAC2STR(bssid));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003441 } else {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07003442 wpa_msg_global(wpa_s, MSG_INFO,
3443 P2P_EVENT_INVITATION_RECEIVED
3444 "sa=" MACSTR " go_dev_addr=" MACSTR
3445 " unknown-network",
3446 MAC2STR(sa), MAC2STR(go_dev_addr));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003447 }
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003448 wpas_notify_p2p_invitation_received(wpa_s, sa, go_dev_addr,
3449 bssid, 0, op_freq);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003450 return;
3451 }
3452
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08003453 if (s->mode == WPAS_MODE_P2P_GO && op_freq) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07003454 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_INVITATION_RECEIVED
3455 "sa=" MACSTR " persistent=%d freq=%d",
3456 MAC2STR(sa), s->id, op_freq);
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08003457 } else {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07003458 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_INVITATION_RECEIVED
3459 "sa=" MACSTR " persistent=%d",
3460 MAC2STR(sa), s->id);
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08003461 }
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003462 wpas_notify_p2p_invitation_received(wpa_s, sa, go_dev_addr, bssid,
3463 s->id, op_freq);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003464}
3465
3466
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003467static void wpas_remove_persistent_peer(struct wpa_supplicant *wpa_s,
3468 struct wpa_ssid *ssid,
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07003469 const u8 *peer, int inv)
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003470{
3471 size_t i;
Dmitry Shmidt8bd70b72015-05-26 16:02:19 -07003472 struct wpa_supplicant *p2p_wpa_s = wpa_s->global->p2p_init_wpa_s;
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003473
3474 if (ssid == NULL)
3475 return;
3476
3477 for (i = 0; ssid->p2p_client_list && i < ssid->num_p2p_clients; i++) {
Dmitry Shmidtff787d52015-01-12 13:01:47 -08003478 if (os_memcmp(ssid->p2p_client_list + i * 2 * ETH_ALEN, peer,
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003479 ETH_ALEN) == 0)
3480 break;
3481 }
Dmitry Shmidt6aa8ae42014-07-07 09:31:33 -07003482 if (i >= ssid->num_p2p_clients || !ssid->p2p_client_list) {
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003483 if (ssid->mode != WPAS_MODE_P2P_GO &&
3484 os_memcmp(ssid->bssid, peer, ETH_ALEN) == 0) {
3485 wpa_printf(MSG_DEBUG, "P2P: Remove persistent group %d "
3486 "due to invitation result", ssid->id);
3487 wpas_notify_network_removed(wpa_s, ssid);
3488 wpa_config_remove_network(wpa_s->conf, ssid->id);
3489 return;
3490 }
3491 return; /* Peer not found in client list */
3492 }
3493
3494 wpa_printf(MSG_DEBUG, "P2P: Remove peer " MACSTR " from persistent "
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07003495 "group %d client list%s",
3496 MAC2STR(peer), ssid->id,
3497 inv ? " due to invitation result" : "");
Dmitry Shmidtff787d52015-01-12 13:01:47 -08003498 os_memmove(ssid->p2p_client_list + i * 2 * ETH_ALEN,
3499 ssid->p2p_client_list + (i + 1) * 2 * ETH_ALEN,
3500 (ssid->num_p2p_clients - i - 1) * 2 * ETH_ALEN);
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003501 ssid->num_p2p_clients--;
Dmitry Shmidt8bd70b72015-05-26 16:02:19 -07003502 if (p2p_wpa_s->conf->update_config &&
3503 wpa_config_write(p2p_wpa_s->confname, p2p_wpa_s->conf))
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003504 wpa_printf(MSG_DEBUG, "P2P: Failed to update configuration");
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003505}
3506
3507
3508static void wpas_remove_persistent_client(struct wpa_supplicant *wpa_s,
3509 const u8 *peer)
3510{
3511 struct wpa_ssid *ssid;
3512
3513 wpa_s = wpa_s->global->p2p_invite_group;
3514 if (wpa_s == NULL)
3515 return; /* No known invitation group */
3516 ssid = wpa_s->current_ssid;
3517 if (ssid == NULL || ssid->mode != WPAS_MODE_P2P_GO ||
3518 !ssid->p2p_persistent_group)
3519 return; /* Not operating as a GO in persistent group */
Dmitry Shmidt9c175262016-03-03 10:20:07 -08003520 ssid = wpas_p2p_get_persistent(wpa_s->p2pdev, peer,
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003521 ssid->ssid, ssid->ssid_len);
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07003522 wpas_remove_persistent_peer(wpa_s, ssid, peer, 1);
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003523}
3524
3525
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08003526static void wpas_invitation_result(void *ctx, int status, const u8 *bssid,
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003527 const struct p2p_channels *channels,
Dmitry Shmidt15907092014-03-25 10:42:57 -07003528 const u8 *peer, int neg_freq,
3529 int peer_oper_freq)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003530{
3531 struct wpa_supplicant *wpa_s = ctx;
3532 struct wpa_ssid *ssid;
Dmitry Shmidt15907092014-03-25 10:42:57 -07003533 int freq;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003534
3535 if (bssid) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07003536 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_INVITATION_RESULT
3537 "status=%d " MACSTR,
3538 status, MAC2STR(bssid));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003539 } else {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07003540 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_INVITATION_RESULT
3541 "status=%d ", status);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003542 }
3543 wpas_notify_p2p_invitation_result(wpa_s, status, bssid);
3544
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003545 wpa_printf(MSG_DEBUG, "P2P: Invitation result - status=%d peer=" MACSTR,
3546 status, MAC2STR(peer));
3547 if (wpa_s->pending_invite_ssid_id == -1) {
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08003548 struct wpa_supplicant *group_if =
3549 wpa_s->global->p2p_invite_group;
3550
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003551 if (status == P2P_SC_FAIL_UNKNOWN_GROUP)
3552 wpas_remove_persistent_client(wpa_s, peer);
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08003553
3554 /*
3555 * Invitation to an active group. If this is successful and we
3556 * are the GO, set the client wait to postpone some concurrent
3557 * operations and to allow provisioning and connection to happen
3558 * more quickly.
3559 */
3560 if (status == P2P_SC_SUCCESS &&
3561 group_if && group_if->current_ssid &&
3562 group_if->current_ssid->mode == WPAS_MODE_P2P_GO) {
3563 os_get_reltime(&wpa_s->global->p2p_go_wait_client);
3564#ifdef CONFIG_TESTING_OPTIONS
3565 if (group_if->p2p_go_csa_on_inv) {
3566 wpa_printf(MSG_DEBUG,
3567 "Testing: force P2P GO CSA after invitation");
3568 eloop_cancel_timeout(
3569 wpas_p2p_reconsider_moving_go,
3570 wpa_s, NULL);
3571 eloop_register_timeout(
3572 0, 50000,
3573 wpas_p2p_reconsider_moving_go,
3574 wpa_s, NULL);
3575 }
3576#endif /* CONFIG_TESTING_OPTIONS */
3577 }
3578 return;
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003579 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003580
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08003581 if (status == P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE) {
3582 wpa_printf(MSG_DEBUG, "P2P: Waiting for peer to start another "
3583 "invitation exchange to indicate readiness for "
3584 "re-invocation");
3585 }
3586
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003587 if (status != P2P_SC_SUCCESS) {
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003588 if (status == P2P_SC_FAIL_UNKNOWN_GROUP) {
3589 ssid = wpa_config_get_network(
3590 wpa_s->conf, wpa_s->pending_invite_ssid_id);
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07003591 wpas_remove_persistent_peer(wpa_s, ssid, peer, 1);
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003592 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003593 wpas_p2p_remove_pending_group_interface(wpa_s);
3594 return;
3595 }
3596
3597 ssid = wpa_config_get_network(wpa_s->conf,
3598 wpa_s->pending_invite_ssid_id);
3599 if (ssid == NULL) {
3600 wpa_printf(MSG_ERROR, "P2P: Could not find persistent group "
3601 "data matching with invitation");
3602 return;
3603 }
3604
Jouni Malinen2c5b17d2012-10-13 21:52:46 -07003605 /*
3606 * The peer could have missed our ctrl::ack frame for Invitation
3607 * Response and continue retransmitting the frame. To reduce the
3608 * likelihood of the peer not getting successful TX status for the
3609 * Invitation Response frame, wait a short time here before starting
3610 * the persistent group so that we will remain on the current channel to
3611 * acknowledge any possible retransmission from the peer.
3612 */
3613 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: 50 ms wait on current channel before "
3614 "starting persistent group");
3615 os_sleep(0, 50000);
3616
Dmitry Shmidt15907092014-03-25 10:42:57 -07003617 if (neg_freq > 0 && ssid->mode == WPAS_MODE_P2P_GO &&
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003618 freq_included(wpa_s, channels, neg_freq))
Dmitry Shmidt15907092014-03-25 10:42:57 -07003619 freq = neg_freq;
3620 else if (peer_oper_freq > 0 && ssid->mode != WPAS_MODE_P2P_GO &&
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003621 freq_included(wpa_s, channels, peer_oper_freq))
Dmitry Shmidt15907092014-03-25 10:42:57 -07003622 freq = peer_oper_freq;
3623 else
3624 freq = 0;
3625
3626 wpa_printf(MSG_DEBUG, "P2P: Persistent group invitation success - op_freq=%d MHz SSID=%s",
3627 freq, wpa_ssid_txt(ssid->ssid, ssid->ssid_len));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003628 wpas_p2p_group_add_persistent(wpa_s, ssid,
Jouni Malinen31be0a42012-08-31 21:20:51 +03003629 ssid->mode == WPAS_MODE_P2P_GO,
Dmitry Shmidt96be6222014-02-13 10:16:51 -08003630 wpa_s->p2p_persistent_go_freq,
Dmitry Shmidt15907092014-03-25 10:42:57 -07003631 freq,
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08003632 wpa_s->p2p_go_vht_center_freq2,
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003633 wpa_s->p2p_go_ht40, wpa_s->p2p_go_vht,
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08003634 wpa_s->p2p_go_max_oper_chwidth,
Hai Shalom74f70d42019-02-11 14:42:39 -08003635 wpa_s->p2p_go_he,
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08003636 wpa_s->p2p_go_edmg,
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003637 channels,
Dmitry Shmidt56052862013-10-04 10:23:25 -07003638 ssid->mode == WPAS_MODE_P2P_GO ?
3639 P2P_MAX_INITIAL_CONN_WAIT_GO_REINVOKE :
Hai Shalomc1a21442022-02-04 13:43:00 -08003640 0, 1,
Matthew Wang124e5f42022-12-29 07:23:06 +00003641 is_p2p_allow_6ghz(wpa_s->global->p2p), 0,
Sunil Ravi036cec52023-03-29 11:35:17 -07003642 NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003643}
3644
3645
Dmitry Shmidt04949592012-07-19 12:16:46 -07003646static int wpas_p2p_disallowed_freq(struct wpa_global *global,
3647 unsigned int freq)
3648{
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08003649 if (freq_range_list_includes(&global->p2p_go_avoid_freq, freq))
3650 return 1;
Dmitry Shmidt4ce9c872013-10-24 11:08:13 -07003651 return freq_range_list_includes(&global->p2p_disallow_freq, freq);
Dmitry Shmidt04949592012-07-19 12:16:46 -07003652}
3653
3654
3655static void wpas_p2p_add_chan(struct p2p_reg_class *reg, u8 chan)
3656{
3657 reg->channel[reg->channels] = chan;
3658 reg->channels++;
3659}
3660
3661
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003662static int wpas_p2p_default_channels(struct wpa_supplicant *wpa_s,
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003663 struct p2p_channels *chan,
3664 struct p2p_channels *cli_chan)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003665{
3666 int i, cla = 0;
3667
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003668 wpa_s->global->p2p_24ghz_social_channels = 1;
3669
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003670 os_memset(cli_chan, 0, sizeof(*cli_chan));
3671
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003672 wpa_printf(MSG_DEBUG, "P2P: Enable operating classes for 2.4 GHz "
3673 "band");
3674
3675 /* Operating class 81 - 2.4 GHz band channels 1..13 */
3676 chan->reg_class[cla].reg_class = 81;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003677 chan->reg_class[cla].channels = 0;
3678 for (i = 0; i < 11; i++) {
3679 if (!wpas_p2p_disallowed_freq(wpa_s->global, 2412 + i * 5))
3680 wpas_p2p_add_chan(&chan->reg_class[cla], i + 1);
3681 }
3682 if (chan->reg_class[cla].channels)
3683 cla++;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003684
3685 wpa_printf(MSG_DEBUG, "P2P: Enable operating classes for lower 5 GHz "
3686 "band");
3687
3688 /* Operating class 115 - 5 GHz, channels 36-48 */
3689 chan->reg_class[cla].reg_class = 115;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003690 chan->reg_class[cla].channels = 0;
3691 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 36 * 5))
3692 wpas_p2p_add_chan(&chan->reg_class[cla], 36);
3693 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 40 * 5))
3694 wpas_p2p_add_chan(&chan->reg_class[cla], 40);
3695 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 44 * 5))
3696 wpas_p2p_add_chan(&chan->reg_class[cla], 44);
3697 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 48 * 5))
3698 wpas_p2p_add_chan(&chan->reg_class[cla], 48);
3699 if (chan->reg_class[cla].channels)
3700 cla++;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003701
3702 wpa_printf(MSG_DEBUG, "P2P: Enable operating classes for higher 5 GHz "
3703 "band");
3704
3705 /* Operating class 124 - 5 GHz, channels 149,153,157,161 */
3706 chan->reg_class[cla].reg_class = 124;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003707 chan->reg_class[cla].channels = 0;
3708 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 149 * 5))
3709 wpas_p2p_add_chan(&chan->reg_class[cla], 149);
3710 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 153 * 5))
3711 wpas_p2p_add_chan(&chan->reg_class[cla], 153);
3712 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 156 * 5))
3713 wpas_p2p_add_chan(&chan->reg_class[cla], 157);
3714 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 161 * 5))
3715 wpas_p2p_add_chan(&chan->reg_class[cla], 161);
3716 if (chan->reg_class[cla].channels)
3717 cla++;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003718
3719 chan->reg_classes = cla;
3720 return 0;
3721}
3722
3723
Hai Shalomc1a21442022-02-04 13:43:00 -08003724static enum chan_allowed has_channel(struct wpa_global *global,
3725 struct hostapd_hw_modes *mode, u8 op_class,
3726 u8 chan, int *flags)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003727{
3728 int i;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003729 unsigned int freq;
3730
Hai Shalomc1a21442022-02-04 13:43:00 -08003731 freq = ieee80211_chan_to_freq(NULL, op_class, chan);
Dmitry Shmidt04949592012-07-19 12:16:46 -07003732 if (wpas_p2p_disallowed_freq(global, freq))
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003733 return NOT_ALLOWED;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003734
3735 for (i = 0; i < mode->num_channels; i++) {
Hai Shalomc1a21442022-02-04 13:43:00 -08003736 if ((unsigned int) mode->channels[i].freq == freq) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003737 if (flags)
3738 *flags = mode->channels[i].flag;
Hai Shalomc1a21442022-02-04 13:43:00 -08003739 if (mode->channels[i].flag & HOSTAPD_CHAN_DISABLED)
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003740 return NOT_ALLOWED;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003741 if (mode->channels[i].flag & HOSTAPD_CHAN_NO_IR)
3742 return NO_IR;
Hai Shalomc1a21442022-02-04 13:43:00 -08003743 if (mode->channels[i].flag & HOSTAPD_CHAN_RADAR)
3744 return RADAR;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003745 return ALLOWED;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003746 }
3747 }
3748
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003749 return NOT_ALLOWED;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003750}
3751
3752
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003753static int wpas_p2p_get_center_80mhz(struct wpa_supplicant *wpa_s,
3754 struct hostapd_hw_modes *mode,
Hai Shalomc1a21442022-02-04 13:43:00 -08003755 u8 channel, const u8 *center_channels,
3756 size_t num_chan)
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003757{
Dmitry Shmidtaca489e2016-09-28 15:44:14 -07003758 size_t i;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003759
3760 if (mode->mode != HOSTAPD_MODE_IEEE80211A)
3761 return 0;
3762
Hai Shalomc1a21442022-02-04 13:43:00 -08003763 for (i = 0; i < num_chan; i++)
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003764 /*
3765 * In 80 MHz, the bandwidth "spans" 12 channels (e.g., 36-48),
3766 * so the center channel is 6 channels away from the start/end.
3767 */
3768 if (channel >= center_channels[i] - 6 &&
3769 channel <= center_channels[i] + 6)
3770 return center_channels[i];
3771
3772 return 0;
3773}
3774
3775
Hai Shalomc1a21442022-02-04 13:43:00 -08003776static const u8 center_channels_5ghz_80mhz[] = { 42, 58, 106, 122, 138,
3777 155, 171 };
3778static const u8 center_channels_6ghz_80mhz[] = { 7, 23, 39, 55, 71, 87, 103,
3779 119, 135, 151, 167, 183, 199,
3780 215 };
3781
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003782static enum chan_allowed wpas_p2p_verify_80mhz(struct wpa_supplicant *wpa_s,
3783 struct hostapd_hw_modes *mode,
Hai Shalomc1a21442022-02-04 13:43:00 -08003784 u8 op_class, u8 channel, u8 bw)
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003785{
3786 u8 center_chan;
3787 int i, flags;
3788 enum chan_allowed res, ret = ALLOWED;
Hai Shalomc1a21442022-02-04 13:43:00 -08003789 const u8 *chans;
3790 size_t num_chans;
3791 bool is_6ghz = is_6ghz_op_class(op_class);
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003792
Hai Shalomc1a21442022-02-04 13:43:00 -08003793 if (is_6ghz) {
3794 chans = center_channels_6ghz_80mhz;
3795 num_chans = ARRAY_SIZE(center_channels_6ghz_80mhz);
3796 } else {
3797 chans = center_channels_5ghz_80mhz;
3798 num_chans = ARRAY_SIZE(center_channels_5ghz_80mhz);
3799 }
3800 center_chan = wpas_p2p_get_center_80mhz(wpa_s, mode, channel,
3801 chans, num_chans);
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003802 if (!center_chan)
3803 return NOT_ALLOWED;
Hai Shalomc1a21442022-02-04 13:43:00 -08003804 if (!wpa_s->p2p_go_allow_dfs &&
3805 !is_6ghz && center_chan >= 58 && center_chan <= 138)
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003806 return NOT_ALLOWED; /* Do not allow DFS channels for P2P */
3807
3808 /* check all the channels are available */
3809 for (i = 0; i < 4; i++) {
3810 int adj_chan = center_chan - 6 + i * 4;
3811
Hai Shalomc1a21442022-02-04 13:43:00 -08003812 res = has_channel(wpa_s->global, mode, op_class, adj_chan,
3813 &flags);
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003814 if (res == NOT_ALLOWED)
3815 return NOT_ALLOWED;
Hai Shalomc1a21442022-02-04 13:43:00 -08003816 if (res == RADAR)
3817 ret = RADAR;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003818 if (res == NO_IR)
3819 ret = NO_IR;
Hai Shalomc1a21442022-02-04 13:43:00 -08003820 if (!is_6ghz) {
Sunil Ravi036cec52023-03-29 11:35:17 -07003821 if (!(flags & HOSTAPD_CHAN_VHT_80MHZ_SUBCHANNEL))
Hai Shalomc1a21442022-02-04 13:43:00 -08003822 return NOT_ALLOWED;
3823 } else if (is_6ghz &&
3824 (!(wpas_get_6ghz_he_chwidth_capab(mode) &
3825 HE_PHYCAP_CHANNEL_WIDTH_SET_40MHZ_80MHZ_IN_5G))) {
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003826 return NOT_ALLOWED;
Hai Shalomc1a21442022-02-04 13:43:00 -08003827 }
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003828 }
3829
3830 return ret;
3831}
3832
3833
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08003834static int wpas_p2p_get_center_160mhz(struct wpa_supplicant *wpa_s,
3835 struct hostapd_hw_modes *mode,
Hai Shalomc1a21442022-02-04 13:43:00 -08003836 u8 channel, const u8 *center_channels,
3837 size_t num_chan)
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08003838{
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08003839 unsigned int i;
3840
3841 if (mode->mode != HOSTAPD_MODE_IEEE80211A)
3842 return 0;
3843
Hai Shalomc1a21442022-02-04 13:43:00 -08003844 for (i = 0; i < num_chan; i++)
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08003845 /*
3846 * In 160 MHz, the bandwidth "spans" 28 channels (e.g., 36-64),
3847 * so the center channel is 14 channels away from the start/end.
3848 */
3849 if (channel >= center_channels[i] - 14 &&
3850 channel <= center_channels[i] + 14)
3851 return center_channels[i];
3852
3853 return 0;
3854}
3855
3856
Hai Shalomc1a21442022-02-04 13:43:00 -08003857static const u8 center_channels_5ghz_160mhz[] = { 50, 114, 163 };
3858static const u8 center_channels_6ghz_160mhz[] = { 15, 47, 79, 111, 143, 175,
3859 207 };
3860
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08003861static enum chan_allowed wpas_p2p_verify_160mhz(struct wpa_supplicant *wpa_s,
3862 struct hostapd_hw_modes *mode,
Hai Shalomc1a21442022-02-04 13:43:00 -08003863 u8 op_class, u8 channel, u8 bw)
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08003864{
3865 u8 center_chan;
3866 int i, flags;
3867 enum chan_allowed res, ret = ALLOWED;
Hai Shalomc1a21442022-02-04 13:43:00 -08003868 const u8 *chans;
3869 size_t num_chans;
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08003870
Hai Shalomc1a21442022-02-04 13:43:00 -08003871 if (is_6ghz_op_class(op_class)) {
3872 chans = center_channels_6ghz_160mhz;
3873 num_chans = ARRAY_SIZE(center_channels_6ghz_160mhz);
3874 } else {
3875 chans = center_channels_5ghz_160mhz;
3876 num_chans = ARRAY_SIZE(center_channels_5ghz_160mhz);
3877 }
3878 center_chan = wpas_p2p_get_center_160mhz(wpa_s, mode, channel,
3879 chans, num_chans);
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08003880 if (!center_chan)
3881 return NOT_ALLOWED;
3882 /* VHT 160 MHz uses DFS channels in most countries. */
3883
3884 /* Check all the channels are available */
3885 for (i = 0; i < 8; i++) {
3886 int adj_chan = center_chan - 14 + i * 4;
3887
Hai Shalomc1a21442022-02-04 13:43:00 -08003888 res = has_channel(wpa_s->global, mode, op_class, adj_chan,
3889 &flags);
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08003890 if (res == NOT_ALLOWED)
3891 return NOT_ALLOWED;
3892
Hai Shalomc1a21442022-02-04 13:43:00 -08003893 if (res == RADAR)
3894 ret = RADAR;
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08003895 if (res == NO_IR)
3896 ret = NO_IR;
3897
Hai Shalomc1a21442022-02-04 13:43:00 -08003898 if (!is_6ghz_op_class(op_class)) {
Sunil Ravi036cec52023-03-29 11:35:17 -07003899 if (!(flags & HOSTAPD_CHAN_VHT_80MHZ_SUBCHANNEL) ||
3900 !(flags & HOSTAPD_CHAN_VHT_160MHZ_SUBCHANNEL))
Hai Shalomc1a21442022-02-04 13:43:00 -08003901 return NOT_ALLOWED;
3902 } else if (is_6ghz_op_class(op_class) &&
3903 (!(wpas_get_6ghz_he_chwidth_capab(mode) &
3904 HE_PHYCAP_CHANNEL_WIDTH_SET_160MHZ_IN_5G))) {
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08003905 return NOT_ALLOWED;
Hai Shalomc1a21442022-02-04 13:43:00 -08003906 }
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08003907 }
3908
3909 return ret;
3910}
3911
3912
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08003913static enum chan_allowed wpas_p2p_verify_edmg(struct wpa_supplicant *wpa_s,
3914 struct hostapd_hw_modes *mode,
3915 u8 channel)
3916{
3917 struct ieee80211_edmg_config edmg;
3918
3919 hostapd_encode_edmg_chan(1, channel, 0, &edmg);
3920 if (edmg.channels && ieee802_edmg_is_allowed(mode->edmg, edmg))
3921 return ALLOWED;
3922
3923 return NOT_ALLOWED;
3924}
3925
3926
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003927static enum chan_allowed wpas_p2p_verify_channel(struct wpa_supplicant *wpa_s,
3928 struct hostapd_hw_modes *mode,
Hai Shalomc1a21442022-02-04 13:43:00 -08003929 u8 op_class, u8 channel, u8 bw)
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003930{
Dmitry Shmidt96be6222014-02-13 10:16:51 -08003931 int flag = 0;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003932 enum chan_allowed res, res2;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003933
Sunil Ravi89eba102022-09-13 21:04:37 -07003934 if (is_6ghz_op_class(op_class) && !is_6ghz_psc_frequency(
3935 p2p_channel_to_freq(op_class, channel)))
3936 return NOT_ALLOWED;
3937
Hai Shalomc1a21442022-02-04 13:43:00 -08003938 res2 = res = has_channel(wpa_s->global, mode, op_class, channel, &flag);
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003939 if (bw == BW40MINUS) {
3940 if (!(flag & HOSTAPD_CHAN_HT40MINUS))
3941 return NOT_ALLOWED;
Hai Shalomc1a21442022-02-04 13:43:00 -08003942 res2 = has_channel(wpa_s->global, mode, op_class, channel - 4,
3943 NULL);
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003944 } else if (bw == BW40PLUS) {
3945 if (!(flag & HOSTAPD_CHAN_HT40PLUS))
3946 return NOT_ALLOWED;
Hai Shalomc1a21442022-02-04 13:43:00 -08003947 res2 = has_channel(wpa_s->global, mode, op_class, channel + 4,
3948 NULL);
3949 } else if (is_6ghz_op_class(op_class) && bw == BW40) {
3950 if (mode->mode != HOSTAPD_MODE_IEEE80211A)
3951 return NOT_ALLOWED;
3952 if (get_6ghz_sec_channel(channel) < 0)
3953 res2 = has_channel(wpa_s->global, mode, op_class,
3954 channel - 4, NULL);
3955 else
3956 res2 = has_channel(wpa_s->global, mode, op_class,
3957 channel + 4, NULL);
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003958 } else if (bw == BW80) {
Hai Shalomc1a21442022-02-04 13:43:00 -08003959 res2 = wpas_p2p_verify_80mhz(wpa_s, mode, op_class, channel,
3960 bw);
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08003961 } else if (bw == BW160) {
Hai Shalomc1a21442022-02-04 13:43:00 -08003962 res2 = wpas_p2p_verify_160mhz(wpa_s, mode, op_class, channel,
3963 bw);
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08003964 } else if (bw == BW4320 || bw == BW6480 || bw == BW8640) {
3965 return wpas_p2p_verify_edmg(wpa_s, mode, channel);
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003966 }
3967
3968 if (res == NOT_ALLOWED || res2 == NOT_ALLOWED)
3969 return NOT_ALLOWED;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003970 if (res == NO_IR || res2 == NO_IR)
3971 return NO_IR;
Hai Shalomc1a21442022-02-04 13:43:00 -08003972 if (res == RADAR || res2 == RADAR)
3973 return RADAR;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003974 return res;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003975}
3976
3977
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003978static int wpas_p2p_setup_channels(struct wpa_supplicant *wpa_s,
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003979 struct p2p_channels *chan,
Hai Shalom60840252021-02-19 19:02:11 -08003980 struct p2p_channels *cli_chan,
3981 bool p2p_disable_6ghz)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003982{
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003983 struct hostapd_hw_modes *mode;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003984 int cla, op, cli_cla;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003985
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003986 if (wpa_s->hw.modes == NULL) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003987 wpa_printf(MSG_DEBUG, "P2P: Driver did not support fetching "
3988 "of all supported channels; assume dualband "
3989 "support");
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003990 return wpas_p2p_default_channels(wpa_s, chan, cli_chan);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003991 }
3992
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003993 cla = cli_cla = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003994
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08003995 for (op = 0; global_op_class[op].op_class; op++) {
3996 const struct oper_class_map *o = &global_op_class[op];
Hai Shalomc1a21442022-02-04 13:43:00 -08003997 unsigned int ch;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003998 struct p2p_reg_class *reg = NULL, *cli_reg = NULL;
Shuibing Daie2fad412023-05-05 14:08:11 -07003999 bool check_dfs_supported = (is_p2p_dfs_chan_enabled(wpa_s->global->p2p)
Shuibing Dai64a8a892023-03-08 10:26:22 -08004000 && is_dfs_global_op_class(o->op_class));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004001
Shuibing Dai64a8a892023-03-08 10:26:22 -08004002 if ((!check_dfs_supported && o->p2p == NO_P2P_SUPP) ||
Hai Shalom60840252021-02-19 19:02:11 -08004003 (is_6ghz_op_class(o->op_class) && p2p_disable_6ghz))
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08004004 continue;
4005
Hai Shalomfdcde762020-04-02 11:19:20 -07004006 mode = get_mode(wpa_s->hw.modes, wpa_s->hw.num_modes, o->mode,
4007 is_6ghz_op_class(o->op_class));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004008 if (mode == NULL)
4009 continue;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004010 if (mode->mode == HOSTAPD_MODE_IEEE80211G)
4011 wpa_s->global->p2p_24ghz_social_channels = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004012 for (ch = o->min_chan; ch <= o->max_chan; ch += o->inc) {
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004013 enum chan_allowed res;
Hai Shalom899fcc72020-10-19 14:38:18 -07004014
4015 /* Check for non-continuous jump in channel index
4016 * incrementation */
Hai Shalom60840252021-02-19 19:02:11 -08004017 if ((o->op_class >= 128 && o->op_class <= 130) &&
Hai Shalom899fcc72020-10-19 14:38:18 -07004018 ch < 149 && ch + o->inc > 149)
4019 ch = 149;
4020
Hai Shalomc1a21442022-02-04 13:43:00 -08004021 res = wpas_p2p_verify_channel(wpa_s, mode, o->op_class,
4022 ch, o->bw);
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004023 if (res == ALLOWED) {
4024 if (reg == NULL) {
Hai Shalomfdcde762020-04-02 11:19:20 -07004025 if (cla == P2P_MAX_REG_CLASSES)
4026 continue;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004027 wpa_printf(MSG_DEBUG, "P2P: Add operating class %u",
4028 o->op_class);
4029 reg = &chan->reg_class[cla];
4030 cla++;
4031 reg->reg_class = o->op_class;
4032 }
Hai Shalomfdcde762020-04-02 11:19:20 -07004033 if (reg->channels == P2P_MAX_REG_CLASS_CHANNELS)
4034 continue;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004035 reg->channel[reg->channels] = ch;
4036 reg->channels++;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004037 } else if (res == NO_IR &&
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004038 wpa_s->conf->p2p_add_cli_chan) {
4039 if (cli_reg == NULL) {
Hai Shalomfdcde762020-04-02 11:19:20 -07004040 if (cli_cla == P2P_MAX_REG_CLASSES)
4041 continue;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004042 wpa_printf(MSG_DEBUG, "P2P: Add operating class %u (client only)",
4043 o->op_class);
4044 cli_reg = &cli_chan->reg_class[cli_cla];
4045 cli_cla++;
4046 cli_reg->reg_class = o->op_class;
4047 }
Hai Shalomfdcde762020-04-02 11:19:20 -07004048 if (cli_reg->channels ==
4049 P2P_MAX_REG_CLASS_CHANNELS)
4050 continue;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004051 cli_reg->channel[cli_reg->channels] = ch;
4052 cli_reg->channels++;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004053 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004054 }
4055 if (reg) {
4056 wpa_hexdump(MSG_DEBUG, "P2P: Channels",
4057 reg->channel, reg->channels);
4058 }
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004059 if (cli_reg) {
4060 wpa_hexdump(MSG_DEBUG, "P2P: Channels (client only)",
4061 cli_reg->channel, cli_reg->channels);
4062 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004063 }
4064
4065 chan->reg_classes = cla;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004066 cli_chan->reg_classes = cli_cla;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004067
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004068 return 0;
4069}
4070
4071
Hai Shalomc1a21442022-02-04 13:43:00 -08004072int wpas_p2p_get_sec_channel_offset_40mhz(struct wpa_supplicant *wpa_s,
4073 struct hostapd_hw_modes *mode,
4074 u8 channel)
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004075{
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004076 int op;
4077 enum chan_allowed ret;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004078
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08004079 for (op = 0; global_op_class[op].op_class; op++) {
4080 const struct oper_class_map *o = &global_op_class[op];
Sunil Ravi77d572f2023-01-17 23:58:31 +00004081 u16 ch = 0;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004082
Hai Shalomc1a21442022-02-04 13:43:00 -08004083 /* Allow DFS channels marked as NO_P2P_SUPP to be used with
4084 * driver offloaded DFS. */
4085 if ((o->p2p == NO_P2P_SUPP &&
4086 (!is_dfs_global_op_class(o->op_class) ||
4087 !wpa_s->p2p_go_allow_dfs)) ||
Hai Shalom899fcc72020-10-19 14:38:18 -07004088 (is_6ghz_op_class(o->op_class) &&
4089 wpa_s->conf->p2p_6ghz_disable))
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08004090 continue;
4091
Sunil Ravi77d572f2023-01-17 23:58:31 +00004092 /* IEEE Std 802.11ax-2021 26.17.2.3.2: "A 6 GHz-only AP should
4093 * set up the BSS with a primary 20 MHz channel that coincides
4094 * with a preferred scanning channel (PSC)."
4095 * 6 GHz BW40 operation class 132 in wpa_supplicant uses the
4096 * lowest 20 MHz channel for simplicity, so increase ch by 4 to
4097 * match the PSC.
4098 */
Hai Shalomc1a21442022-02-04 13:43:00 -08004099 if (is_6ghz_op_class(o->op_class) && o->bw == BW40 &&
4100 get_6ghz_sec_channel(channel) < 0)
Sunil Ravi77d572f2023-01-17 23:58:31 +00004101 ch = 4;
Hai Shalomc1a21442022-02-04 13:43:00 -08004102
Sunil Ravi77d572f2023-01-17 23:58:31 +00004103 for (ch += o->min_chan; ch <= o->max_chan; ch += o->inc) {
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004104 if (o->mode != HOSTAPD_MODE_IEEE80211A ||
Hai Shalomc1a21442022-02-04 13:43:00 -08004105 (o->bw != BW40PLUS && o->bw != BW40MINUS &&
4106 o->bw != BW40) ||
Sunil Ravi77d572f2023-01-17 23:58:31 +00004107 ch != channel)
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004108 continue;
Hai Shalomc1a21442022-02-04 13:43:00 -08004109 ret = wpas_p2p_verify_channel(wpa_s, mode, o->op_class,
4110 ch, o->bw);
4111 if (ret == ALLOWED) {
4112 if (is_6ghz_op_class(o->op_class) &&
4113 o->bw == BW40)
4114 return get_6ghz_sec_channel(channel);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004115 return (o->bw == BW40MINUS) ? -1 : 1;
Hai Shalomc1a21442022-02-04 13:43:00 -08004116 }
4117 if (ret == RADAR && wpa_s->p2p_go_allow_dfs) {
4118 /* Allow RADAR channels used for driver
4119 * offloaded DFS */
4120 return (o->bw == BW40MINUS) ? -1 : 1;
4121 }
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004122 }
4123 }
4124 return 0;
4125}
4126
4127
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004128int wpas_p2p_get_vht80_center(struct wpa_supplicant *wpa_s,
Hai Shalomc1a21442022-02-04 13:43:00 -08004129 struct hostapd_hw_modes *mode, u8 channel,
4130 u8 op_class)
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004131{
Hai Shalomc1a21442022-02-04 13:43:00 -08004132 const u8 *chans;
4133 size_t num_chans;
4134 enum chan_allowed ret;
4135
4136 ret = wpas_p2p_verify_channel(wpa_s, mode, op_class, channel, BW80);
4137 if (!(ret == ALLOWED || (ret == RADAR && wpa_s->p2p_go_allow_dfs)))
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004138 return 0;
4139
Hai Shalomc1a21442022-02-04 13:43:00 -08004140 if (is_6ghz_op_class(op_class)) {
4141 chans = center_channels_6ghz_80mhz;
4142 num_chans = ARRAY_SIZE(center_channels_6ghz_80mhz);
4143 } else {
4144 chans = center_channels_5ghz_80mhz;
4145 num_chans = ARRAY_SIZE(center_channels_5ghz_80mhz);
4146 }
4147 return wpas_p2p_get_center_80mhz(wpa_s, mode, channel,
4148 chans, num_chans);
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004149}
4150
4151
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08004152int wpas_p2p_get_vht160_center(struct wpa_supplicant *wpa_s,
Hai Shalomc1a21442022-02-04 13:43:00 -08004153 struct hostapd_hw_modes *mode, u8 channel,
4154 u8 op_class)
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08004155{
Hai Shalomc1a21442022-02-04 13:43:00 -08004156 const u8 *chans;
4157 size_t num_chans;
4158 enum chan_allowed ret;
4159
4160 ret = wpas_p2p_verify_channel(wpa_s, mode, op_class, channel, BW160);
4161 if (!(ret == ALLOWED || (ret == RADAR && wpa_s->p2p_go_allow_dfs)))
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08004162 return 0;
Hai Shalomc1a21442022-02-04 13:43:00 -08004163 if (is_6ghz_op_class(op_class)) {
4164 chans = center_channels_6ghz_160mhz;
4165 num_chans = ARRAY_SIZE(center_channels_6ghz_160mhz);
4166 } else {
4167 chans = center_channels_5ghz_160mhz;
4168 num_chans = ARRAY_SIZE(center_channels_5ghz_160mhz);
4169 }
4170 return wpas_p2p_get_center_160mhz(wpa_s, mode, channel,
4171 chans, num_chans);
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08004172}
4173
4174
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004175static int wpas_get_noa(void *ctx, const u8 *interface_addr, u8 *buf,
4176 size_t buf_len)
4177{
4178 struct wpa_supplicant *wpa_s = ctx;
4179
4180 for (wpa_s = wpa_s->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
4181 if (os_memcmp(wpa_s->own_addr, interface_addr, ETH_ALEN) == 0)
4182 break;
4183 }
4184 if (wpa_s == NULL)
4185 return -1;
4186
4187 return wpa_drv_get_noa(wpa_s, buf, buf_len);
4188}
4189
4190
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07004191struct wpa_supplicant * wpas_get_p2p_go_iface(struct wpa_supplicant *wpa_s,
4192 const u8 *ssid, size_t ssid_len)
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08004193{
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07004194 for (wpa_s = wpa_s->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
4195 struct wpa_ssid *s = wpa_s->current_ssid;
4196 if (s == NULL)
4197 continue;
4198 if (s->mode != WPAS_MODE_P2P_GO &&
4199 s->mode != WPAS_MODE_AP &&
4200 s->mode != WPAS_MODE_P2P_GROUP_FORMATION)
4201 continue;
4202 if (s->ssid_len != ssid_len ||
Dmitry Shmidt03658832014-08-13 11:03:49 -07004203 os_memcmp(ssid, s->ssid, ssid_len) != 0)
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07004204 continue;
4205 return wpa_s;
4206 }
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08004207
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07004208 return NULL;
4209
4210}
4211
4212
4213struct wpa_supplicant * wpas_get_p2p_client_iface(struct wpa_supplicant *wpa_s,
4214 const u8 *peer_dev_addr)
4215{
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08004216 for (wpa_s = wpa_s->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
4217 struct wpa_ssid *ssid = wpa_s->current_ssid;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004218 if (ssid && (ssid->mode != WPAS_MODE_INFRA || !ssid->p2p_group))
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08004219 continue;
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07004220 if (os_memcmp(wpa_s->go_dev_addr, peer_dev_addr, ETH_ALEN) == 0)
4221 return wpa_s;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08004222 }
4223
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07004224 return NULL;
4225}
4226
4227
4228static int wpas_go_connected(void *ctx, const u8 *dev_addr)
4229{
4230 struct wpa_supplicant *wpa_s = ctx;
4231
4232 return wpas_get_p2p_client_iface(wpa_s, dev_addr) != NULL;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08004233}
4234
4235
Dmitry Shmidt18463232014-01-24 12:29:41 -08004236static int wpas_is_concurrent_session_active(void *ctx)
4237{
4238 struct wpa_supplicant *wpa_s = ctx;
4239 struct wpa_supplicant *ifs;
4240
4241 for (ifs = wpa_s->global->ifaces; ifs; ifs = ifs->next) {
4242 if (ifs == wpa_s)
4243 continue;
4244 if (ifs->wpa_state > WPA_ASSOCIATED)
4245 return 1;
4246 }
4247 return 0;
4248}
4249
4250
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07004251static void wpas_p2p_debug_print(void *ctx, int level, const char *msg)
4252{
4253 struct wpa_supplicant *wpa_s = ctx;
4254 wpa_msg_global(wpa_s, level, "P2P: %s", msg);
4255}
4256
4257
Dmitry Shmidt9ead16e2014-10-07 13:15:23 -07004258int wpas_p2p_add_p2pdev_interface(struct wpa_supplicant *wpa_s,
4259 const char *conf_p2p_dev)
Dmitry Shmidt34af3062013-07-11 10:46:32 -07004260{
4261 struct wpa_interface iface;
4262 struct wpa_supplicant *p2pdev_wpa_s;
4263 char ifname[100];
4264 char force_name[100];
4265 int ret;
Hai Shalom60840252021-02-19 19:02:11 -08004266 const u8 *if_addr = NULL;
Dmitry Shmidt34af3062013-07-11 10:46:32 -07004267
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004268 ret = os_snprintf(ifname, sizeof(ifname), P2P_MGMT_DEVICE_PREFIX "%s",
4269 wpa_s->ifname);
4270 if (os_snprintf_error(sizeof(ifname), ret))
4271 return -1;
Hai Shalom899fcc72020-10-19 14:38:18 -07004272 /* Cut length at the maximum size. Note that we don't need to ensure
4273 * collision free names here as the created interface is not a netdev.
4274 */
4275 ifname[IFNAMSIZ - 1] = '\0';
Dmitry Shmidt34af3062013-07-11 10:46:32 -07004276 force_name[0] = '\0';
4277 wpa_s->pending_interface_type = WPA_IF_P2P_DEVICE;
Hai Shalom60840252021-02-19 19:02:11 -08004278
4279 if (wpa_s->conf->p2p_device_random_mac_addr == 2 &&
4280 !is_zero_ether_addr(wpa_s->conf->p2p_device_persistent_mac_addr))
4281 if_addr = wpa_s->conf->p2p_device_persistent_mac_addr;
4282
4283 ret = wpa_drv_if_add(wpa_s, WPA_IF_P2P_DEVICE, ifname, if_addr, NULL,
Dmitry Shmidt34af3062013-07-11 10:46:32 -07004284 force_name, wpa_s->pending_interface_addr, NULL);
4285 if (ret < 0) {
4286 wpa_printf(MSG_DEBUG, "P2P: Failed to create P2P Device interface");
4287 return ret;
4288 }
4289 os_strlcpy(wpa_s->pending_interface_name, ifname,
4290 sizeof(wpa_s->pending_interface_name));
4291
4292 os_memset(&iface, 0, sizeof(iface));
4293 iface.p2p_mgmt = 1;
4294 iface.ifname = wpa_s->pending_interface_name;
4295 iface.driver = wpa_s->driver->name;
4296 iface.driver_param = wpa_s->conf->driver_param;
Dmitry Shmidt2ac5f602014-03-07 10:08:21 -08004297
4298 /*
4299 * If a P2P Device configuration file was given, use it as the interface
4300 * configuration file (instead of using parent's configuration file.
4301 */
Dmitry Shmidt9ead16e2014-10-07 13:15:23 -07004302 if (conf_p2p_dev) {
4303 iface.confname = conf_p2p_dev;
Dmitry Shmidt2ac5f602014-03-07 10:08:21 -08004304 iface.ctrl_interface = NULL;
4305 } else {
4306 iface.confname = wpa_s->confname;
4307 iface.ctrl_interface = wpa_s->conf->ctrl_interface;
4308 }
Dmitry Shmidt2ac5f602014-03-07 10:08:21 -08004309
Dmitry Shmidt203eadb2015-03-05 14:16:04 -08004310 p2pdev_wpa_s = wpa_supplicant_add_iface(wpa_s->global, &iface, wpa_s);
Dmitry Shmidt34af3062013-07-11 10:46:32 -07004311 if (!p2pdev_wpa_s) {
4312 wpa_printf(MSG_DEBUG, "P2P: Failed to add P2P Device interface");
4313 return -1;
4314 }
Dmitry Shmidt34af3062013-07-11 10:46:32 -07004315
Dmitry Shmidt9c175262016-03-03 10:20:07 -08004316 p2pdev_wpa_s->p2pdev = p2pdev_wpa_s;
Dmitry Shmidt34af3062013-07-11 10:46:32 -07004317 wpa_s->pending_interface_name[0] = '\0';
4318 return 0;
4319}
4320
4321
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08004322static void wpas_presence_resp(void *ctx, const u8 *src, u8 status,
4323 const u8 *noa, size_t noa_len)
4324{
4325 struct wpa_supplicant *wpa_s, *intf = ctx;
4326 char hex[100];
4327
4328 for (wpa_s = intf->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
4329 if (wpa_s->waiting_presence_resp)
4330 break;
4331 }
4332 if (!wpa_s) {
4333 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: No group interface was waiting for presence response");
4334 return;
4335 }
4336 wpa_s->waiting_presence_resp = 0;
4337
4338 wpa_snprintf_hex(hex, sizeof(hex), noa, noa_len);
4339 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_PRESENCE_RESPONSE "src=" MACSTR
4340 " status=%u noa=%s", MAC2STR(src), status, hex);
4341}
4342
4343
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004344static int wpas_get_persistent_group(void *ctx, const u8 *addr, const u8 *ssid,
4345 size_t ssid_len, u8 *go_dev_addr,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004346 u8 *ret_ssid, size_t *ret_ssid_len,
4347 u8 *intended_iface_addr)
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004348{
4349 struct wpa_supplicant *wpa_s = ctx;
4350 struct wpa_ssid *s;
4351
4352 s = wpas_p2p_get_persistent(wpa_s, addr, ssid, ssid_len);
4353 if (s) {
4354 os_memcpy(ret_ssid, s->ssid, s->ssid_len);
4355 *ret_ssid_len = s->ssid_len;
4356 os_memcpy(go_dev_addr, s->bssid, ETH_ALEN);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004357
4358 if (s->mode != WPAS_MODE_P2P_GO) {
4359 os_memset(intended_iface_addr, 0, ETH_ALEN);
4360 } else if (wpas_p2p_create_iface(wpa_s)) {
4361 if (wpas_p2p_add_group_interface(wpa_s, WPA_IF_P2P_GO))
4362 return 0;
4363
4364 os_memcpy(intended_iface_addr,
4365 wpa_s->pending_interface_addr, ETH_ALEN);
4366 } else {
4367 os_memcpy(intended_iface_addr, wpa_s->own_addr,
4368 ETH_ALEN);
4369 }
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004370 return 1;
4371 }
4372
4373 return 0;
4374}
4375
4376
4377static int wpas_get_go_info(void *ctx, u8 *intended_addr,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004378 u8 *ssid, size_t *ssid_len, int *group_iface,
4379 unsigned int *freq)
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004380{
4381 struct wpa_supplicant *wpa_s = ctx;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004382 struct wpa_supplicant *go;
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004383 struct wpa_ssid *s;
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004384
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004385 /*
4386 * group_iface will be set to 1 only if a dedicated interface for P2P
4387 * role is required. First, we try to reuse an active GO. However,
4388 * if it is not present, we will try to reactivate an existing
4389 * persistent group and set group_iface to 1, so the caller will know
4390 * that the pending interface should be used.
4391 */
4392 *group_iface = 0;
4393
4394 if (freq)
4395 *freq = 0;
4396
4397 go = wpas_p2p_get_go_group(wpa_s);
4398 if (!go) {
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004399 s = wpas_p2p_get_persistent_go(wpa_s);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004400 *group_iface = wpas_p2p_create_iface(wpa_s);
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004401 if (s)
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004402 os_memcpy(intended_addr, s->bssid, ETH_ALEN);
4403 else
4404 return 0;
4405 } else {
4406 s = go->current_ssid;
4407 os_memcpy(intended_addr, go->own_addr, ETH_ALEN);
4408 if (freq)
4409 *freq = go->assoc_freq;
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004410 }
4411
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004412 os_memcpy(ssid, s->ssid, s->ssid_len);
4413 *ssid_len = s->ssid_len;
4414
4415 return 1;
4416}
4417
4418
4419static int wpas_remove_stale_groups(void *ctx, const u8 *peer, const u8 *go,
4420 const u8 *ssid, size_t ssid_len)
4421{
4422 struct wpa_supplicant *wpa_s = ctx;
4423 struct wpa_ssid *s;
4424 int save_config = 0;
4425 size_t i;
4426
4427 /* Start with our first choice of Persistent Groups */
4428 while ((s = wpas_p2p_get_persistent(wpa_s, peer, NULL, 0))) {
4429 if (go && ssid && ssid_len &&
4430 s->ssid_len == ssid_len &&
4431 os_memcmp(go, s->bssid, ETH_ALEN) == 0 &&
4432 os_memcmp(ssid, s->ssid, ssid_len) == 0)
4433 break;
4434
4435 /* Remove stale persistent group */
4436 if (s->mode != WPAS_MODE_P2P_GO || s->num_p2p_clients <= 1) {
Hai Shalom74f70d42019-02-11 14:42:39 -08004437 wpa_dbg(wpa_s, MSG_DEBUG,
4438 "P2P: Remove stale persistent group id=%d",
4439 s->id);
4440 wpas_notify_persistent_group_removed(wpa_s, s);
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004441 wpa_config_remove_network(wpa_s->conf, s->id);
4442 save_config = 1;
4443 continue;
4444 }
4445
4446 for (i = 0; i < s->num_p2p_clients; i++) {
4447 if (os_memcmp(s->p2p_client_list + i * 2 * ETH_ALEN,
4448 peer, ETH_ALEN) != 0)
4449 continue;
4450
4451 os_memmove(s->p2p_client_list + i * 2 * ETH_ALEN,
4452 s->p2p_client_list + (i + 1) * 2 * ETH_ALEN,
4453 (s->num_p2p_clients - i - 1) * 2 * ETH_ALEN);
4454 break;
4455 }
4456 s->num_p2p_clients--;
4457 save_config = 1;
4458 }
4459
4460 if (save_config)
4461 p2p_config_write(wpa_s);
4462
4463 /* Return TRUE if valid SSID remains */
4464 return s != NULL;
4465}
4466
4467
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004468static void wpas_p2ps_get_feat_cap_str(char *buf, size_t buf_len,
4469 const u8 *feat_cap, size_t feat_cap_len)
4470{
4471 static const char pref[] = " feature_cap=";
4472 int ret;
4473
4474 buf[0] = '\0';
4475
4476 /*
4477 * We expect a feature capability to contain at least one byte to be
4478 * reported. The string buffer provided by the caller function is
4479 * expected to be big enough to contain all bytes of the attribute for
4480 * known specifications. This function truncates the reported bytes if
4481 * the feature capability data exceeds the string buffer size.
4482 */
4483 if (!feat_cap || !feat_cap_len || buf_len < sizeof(pref) + 2)
4484 return;
4485
4486 os_memcpy(buf, pref, sizeof(pref));
4487 ret = wpa_snprintf_hex(&buf[sizeof(pref) - 1],
4488 buf_len - sizeof(pref) + 1,
4489 feat_cap, feat_cap_len);
4490
4491 if (ret != (2 * (int) feat_cap_len))
4492 wpa_printf(MSG_WARNING, "P2PS feature_cap bytes truncated");
4493}
4494
4495
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004496static void wpas_p2ps_prov_complete(void *ctx, u8 status, const u8 *dev,
4497 const u8 *adv_mac, const u8 *ses_mac,
4498 const u8 *grp_mac, u32 adv_id, u32 ses_id,
4499 u8 conncap, int passwd_id,
4500 const u8 *persist_ssid,
4501 size_t persist_ssid_size, int response_done,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004502 int prov_start, const char *session_info,
4503 const u8 *feat_cap, size_t feat_cap_len,
Dmitry Shmidtde47be72016-01-07 12:52:55 -08004504 unsigned int freq,
4505 const u8 *group_ssid, size_t group_ssid_len)
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004506{
4507 struct wpa_supplicant *wpa_s = ctx;
4508 u8 mac[ETH_ALEN];
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004509 struct wpa_ssid *persistent_go, *stale, *s = NULL;
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004510 int save_config = 0;
4511 struct wpa_supplicant *go_wpa_s;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004512 char feat_cap_str[256];
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004513
4514 if (!dev)
4515 return;
4516
4517 os_memset(mac, 0, ETH_ALEN);
4518 if (!adv_mac)
4519 adv_mac = mac;
4520 if (!ses_mac)
4521 ses_mac = mac;
4522 if (!grp_mac)
4523 grp_mac = mac;
4524
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004525 wpas_p2ps_get_feat_cap_str(feat_cap_str, sizeof(feat_cap_str),
4526 feat_cap, feat_cap_len);
4527
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004528 if (prov_start) {
4529 if (session_info == NULL) {
4530 wpa_msg_global(wpa_s, MSG_INFO,
4531 P2P_EVENT_P2PS_PROVISION_START MACSTR
4532 " adv_id=%x conncap=%x"
4533 " adv_mac=" MACSTR
4534 " session=%x mac=" MACSTR
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004535 " dev_passwd_id=%d%s",
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004536 MAC2STR(dev), adv_id, conncap,
4537 MAC2STR(adv_mac),
4538 ses_id, MAC2STR(ses_mac),
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004539 passwd_id, feat_cap_str);
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004540 } else {
4541 wpa_msg_global(wpa_s, MSG_INFO,
4542 P2P_EVENT_P2PS_PROVISION_START MACSTR
4543 " adv_id=%x conncap=%x"
4544 " adv_mac=" MACSTR
4545 " session=%x mac=" MACSTR
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004546 " dev_passwd_id=%d info='%s'%s",
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004547 MAC2STR(dev), adv_id, conncap,
4548 MAC2STR(adv_mac),
4549 ses_id, MAC2STR(ses_mac),
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004550 passwd_id, session_info, feat_cap_str);
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004551 }
4552 return;
4553 }
4554
4555 go_wpa_s = wpas_p2p_get_go_group(wpa_s);
4556 persistent_go = wpas_p2p_get_persistent_go(wpa_s);
4557
4558 if (status && status != P2P_SC_SUCCESS_DEFERRED) {
4559 if (go_wpa_s && !p2p_group_go_member_count(wpa_s))
4560 wpas_p2p_group_remove(wpa_s, go_wpa_s->ifname);
4561
4562 if (persistent_go && !persistent_go->num_p2p_clients) {
4563 /* remove empty persistent GO */
Hai Shalom74f70d42019-02-11 14:42:39 -08004564 wpa_dbg(wpa_s, MSG_DEBUG,
4565 "P2P: Remove empty persistent group id=%d",
4566 persistent_go->id);
4567 wpas_notify_persistent_group_removed(wpa_s,
4568 persistent_go);
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004569 wpa_config_remove_network(wpa_s->conf,
4570 persistent_go->id);
4571 }
4572
4573 wpa_msg_global(wpa_s, MSG_INFO,
4574 P2P_EVENT_P2PS_PROVISION_DONE MACSTR
4575 " status=%d"
4576 " adv_id=%x adv_mac=" MACSTR
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004577 " session=%x mac=" MACSTR "%s",
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004578 MAC2STR(dev), status,
4579 adv_id, MAC2STR(adv_mac),
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004580 ses_id, MAC2STR(ses_mac), feat_cap_str);
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004581 return;
4582 }
4583
4584 /* Clean up stale persistent groups with this device */
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004585 if (persist_ssid && persist_ssid_size)
4586 s = wpas_p2p_get_persistent(wpa_s, dev, persist_ssid,
4587 persist_ssid_size);
4588
4589 if (persist_ssid && s && s->mode != WPAS_MODE_P2P_GO &&
4590 is_zero_ether_addr(grp_mac)) {
4591 wpa_dbg(wpa_s, MSG_ERROR,
4592 "P2P: Peer device is a GO in a persistent group, but it did not provide the intended MAC address");
4593 return;
4594 }
4595
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004596 for (;;) {
4597 stale = wpas_p2p_get_persistent(wpa_s, dev, NULL, 0);
4598 if (!stale)
4599 break;
4600
4601 if (s && s->ssid_len == stale->ssid_len &&
4602 os_memcmp(stale->bssid, s->bssid, ETH_ALEN) == 0 &&
4603 os_memcmp(stale->ssid, s->ssid, s->ssid_len) == 0)
4604 break;
4605
4606 /* Remove stale persistent group */
4607 if (stale->mode != WPAS_MODE_P2P_GO ||
4608 stale->num_p2p_clients <= 1) {
Hai Shalom74f70d42019-02-11 14:42:39 -08004609 wpa_dbg(wpa_s, MSG_DEBUG,
4610 "P2P: Remove stale persistent group id=%d",
4611 stale->id);
4612 wpas_notify_persistent_group_removed(wpa_s, stale);
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004613 wpa_config_remove_network(wpa_s->conf, stale->id);
4614 } else {
4615 size_t i;
4616
4617 for (i = 0; i < stale->num_p2p_clients; i++) {
4618 if (os_memcmp(stale->p2p_client_list +
4619 i * ETH_ALEN,
4620 dev, ETH_ALEN) == 0) {
4621 os_memmove(stale->p2p_client_list +
4622 i * ETH_ALEN,
4623 stale->p2p_client_list +
4624 (i + 1) * ETH_ALEN,
4625 (stale->num_p2p_clients -
4626 i - 1) * ETH_ALEN);
4627 break;
4628 }
4629 }
4630 stale->num_p2p_clients--;
4631 }
4632 save_config = 1;
4633 }
4634
4635 if (save_config)
4636 p2p_config_write(wpa_s);
4637
4638 if (s) {
4639 if (go_wpa_s && !p2p_group_go_member_count(wpa_s))
4640 wpas_p2p_group_remove(wpa_s, go_wpa_s->ifname);
4641
4642 if (persistent_go && s != persistent_go &&
4643 !persistent_go->num_p2p_clients) {
4644 /* remove empty persistent GO */
Hai Shalom74f70d42019-02-11 14:42:39 -08004645 wpa_dbg(wpa_s, MSG_DEBUG,
4646 "P2P: Remove empty persistent group id=%d",
4647 persistent_go->id);
4648 wpas_notify_persistent_group_removed(wpa_s,
4649 persistent_go);
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004650 wpa_config_remove_network(wpa_s->conf,
4651 persistent_go->id);
4652 /* Save config */
4653 }
4654
4655 wpa_msg_global(wpa_s, MSG_INFO,
4656 P2P_EVENT_P2PS_PROVISION_DONE MACSTR
4657 " status=%d"
4658 " adv_id=%x adv_mac=" MACSTR
4659 " session=%x mac=" MACSTR
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004660 " persist=%d%s",
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004661 MAC2STR(dev), status,
4662 adv_id, MAC2STR(adv_mac),
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004663 ses_id, MAC2STR(ses_mac), s->id, feat_cap_str);
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004664 return;
4665 }
4666
Hai Shalom5f92bc92019-04-18 11:54:11 -07004667 wpa_s->global->pending_p2ps_group = 0;
4668 wpa_s->global->pending_p2ps_group_freq = 0;
4669
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004670 if (conncap == P2PS_SETUP_GROUP_OWNER) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004671 /*
4672 * We need to copy the interface name. Simply saving a
4673 * pointer isn't enough, since if we use pending_interface_name
4674 * it will be overwritten when the group is added.
4675 */
4676 char go_ifname[100];
4677
4678 go_ifname[0] = '\0';
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004679 if (!go_wpa_s) {
Hai Shalom5f92bc92019-04-18 11:54:11 -07004680 if (!response_done) {
4681 wpa_s->global->pending_p2ps_group = 1;
4682 wpa_s->global->pending_p2ps_group_freq = freq;
4683 }
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004684
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004685 if (!wpas_p2p_create_iface(wpa_s))
4686 os_memcpy(go_ifname, wpa_s->ifname,
4687 sizeof(go_ifname));
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004688 else if (wpa_s->pending_interface_name[0])
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004689 os_memcpy(go_ifname,
4690 wpa_s->pending_interface_name,
4691 sizeof(go_ifname));
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004692
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004693 if (!go_ifname[0]) {
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004694 wpas_p2ps_prov_complete(
4695 wpa_s, P2P_SC_FAIL_UNKNOWN_GROUP,
4696 dev, adv_mac, ses_mac,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004697 grp_mac, adv_id, ses_id, 0, 0,
Dmitry Shmidtde47be72016-01-07 12:52:55 -08004698 NULL, 0, 0, 0, NULL, NULL, 0, 0,
4699 NULL, 0);
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004700 return;
4701 }
4702
4703 /* If PD Resp complete, start up the GO */
4704 if (response_done && persistent_go) {
4705 wpas_p2p_group_add_persistent(
4706 wpa_s, persistent_go,
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08004707 0, 0, freq, 0, 0, 0, 0, 0, 0, NULL,
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004708 persistent_go->mode ==
4709 WPAS_MODE_P2P_GO ?
4710 P2P_MAX_INITIAL_CONN_WAIT_GO_REINVOKE :
Sunil Ravi036cec52023-03-29 11:35:17 -07004711 0, 0, false, 0, NULL);
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004712 } else if (response_done) {
Hai Shalom74f70d42019-02-11 14:42:39 -08004713 wpas_p2p_group_add(wpa_s, 1, freq,
Hai Shalomc1a21442022-02-04 13:43:00 -08004714 0, 0, 0, 0, 0, 0, false);
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004715 }
4716
4717 if (passwd_id == DEV_PW_P2PS_DEFAULT) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004718 os_memcpy(wpa_s->p2ps_join_addr, grp_mac,
4719 ETH_ALEN);
4720 wpa_s->p2ps_method_config_any = 1;
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004721 }
4722 } else if (passwd_id == DEV_PW_P2PS_DEFAULT) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004723 os_memcpy(go_ifname, go_wpa_s->ifname,
4724 sizeof(go_ifname));
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004725
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004726 if (is_zero_ether_addr(grp_mac)) {
4727 wpa_dbg(go_wpa_s, MSG_DEBUG,
4728 "P2P: Setting PIN-1 for ANY");
4729 wpa_supplicant_ap_wps_pin(go_wpa_s, NULL,
4730 "12345670", NULL, 0,
4731 0);
4732 } else {
4733 wpa_dbg(go_wpa_s, MSG_DEBUG,
4734 "P2P: Setting PIN-1 for " MACSTR,
4735 MAC2STR(grp_mac));
4736 wpa_supplicant_ap_wps_pin(go_wpa_s, grp_mac,
4737 "12345670", NULL, 0,
4738 0);
4739 }
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004740
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004741 os_memcpy(wpa_s->p2ps_join_addr, grp_mac, ETH_ALEN);
4742 wpa_s->p2ps_method_config_any = 1;
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004743 }
4744
4745 wpa_msg_global(wpa_s, MSG_INFO,
4746 P2P_EVENT_P2PS_PROVISION_DONE MACSTR
4747 " status=%d conncap=%x"
4748 " adv_id=%x adv_mac=" MACSTR
4749 " session=%x mac=" MACSTR
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004750 " dev_passwd_id=%d go=%s%s",
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004751 MAC2STR(dev), status, conncap,
4752 adv_id, MAC2STR(adv_mac),
4753 ses_id, MAC2STR(ses_mac),
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004754 passwd_id, go_ifname, feat_cap_str);
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004755 return;
4756 }
4757
4758 if (go_wpa_s && !p2p_group_go_member_count(wpa_s))
4759 wpas_p2p_group_remove(wpa_s, go_wpa_s->ifname);
4760
4761 if (persistent_go && !persistent_go->num_p2p_clients) {
4762 /* remove empty persistent GO */
Hai Shalom74f70d42019-02-11 14:42:39 -08004763 wpa_dbg(wpa_s, MSG_DEBUG,
4764 "P2P: Remove empty persistent group id=%d",
4765 persistent_go->id);
4766 wpas_notify_persistent_group_removed(wpa_s, persistent_go);
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004767 wpa_config_remove_network(wpa_s->conf, persistent_go->id);
4768 }
4769
4770 if (conncap == P2PS_SETUP_CLIENT) {
Dmitry Shmidtde47be72016-01-07 12:52:55 -08004771 char ssid_hex[32 * 2 + 1];
4772
4773 if (group_ssid)
4774 wpa_snprintf_hex(ssid_hex, sizeof(ssid_hex),
4775 group_ssid, group_ssid_len);
4776 else
4777 ssid_hex[0] = '\0';
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004778 wpa_msg_global(wpa_s, MSG_INFO,
4779 P2P_EVENT_P2PS_PROVISION_DONE MACSTR
4780 " status=%d conncap=%x"
4781 " adv_id=%x adv_mac=" MACSTR
4782 " session=%x mac=" MACSTR
Dmitry Shmidtde47be72016-01-07 12:52:55 -08004783 " dev_passwd_id=%d join=" MACSTR "%s%s%s",
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004784 MAC2STR(dev), status, conncap,
4785 adv_id, MAC2STR(adv_mac),
4786 ses_id, MAC2STR(ses_mac),
Dmitry Shmidtde47be72016-01-07 12:52:55 -08004787 passwd_id, MAC2STR(grp_mac), feat_cap_str,
4788 group_ssid ? " group_ssid=" : "", ssid_hex);
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004789 } else {
4790 wpa_msg_global(wpa_s, MSG_INFO,
4791 P2P_EVENT_P2PS_PROVISION_DONE MACSTR
4792 " status=%d conncap=%x"
4793 " adv_id=%x adv_mac=" MACSTR
4794 " session=%x mac=" MACSTR
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004795 " dev_passwd_id=%d%s",
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004796 MAC2STR(dev), status, conncap,
4797 adv_id, MAC2STR(adv_mac),
4798 ses_id, MAC2STR(ses_mac),
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004799 passwd_id, feat_cap_str);
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004800 }
4801}
4802
4803
Dmitry Shmidt5a1480c2014-05-12 09:46:02 -07004804static int _wpas_p2p_in_progress(void *ctx)
4805{
4806 struct wpa_supplicant *wpa_s = ctx;
4807 return wpas_p2p_in_progress(wpa_s);
4808}
4809
4810
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004811static int wpas_prov_disc_resp_cb(void *ctx)
4812{
4813 struct wpa_supplicant *wpa_s = ctx;
4814 struct wpa_ssid *persistent_go;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004815 unsigned int freq;
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004816
4817 if (!wpa_s->global->pending_p2ps_group)
4818 return 0;
4819
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004820 freq = wpa_s->global->pending_p2ps_group_freq;
4821 wpa_s->global->pending_p2ps_group_freq = 0;
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004822 wpa_s->global->pending_p2ps_group = 0;
4823
4824 if (wpas_p2p_get_go_group(wpa_s))
4825 return 0;
4826 persistent_go = wpas_p2p_get_persistent_go(wpa_s);
4827
4828 if (persistent_go) {
4829 wpas_p2p_group_add_persistent(
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08004830 wpa_s, persistent_go, 0, 0, 0, 0, 0, 0, 0, 0, 0,
4831 NULL,
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004832 persistent_go->mode == WPAS_MODE_P2P_GO ?
Hai Shalomc1a21442022-02-04 13:43:00 -08004833 P2P_MAX_INITIAL_CONN_WAIT_GO_REINVOKE : 0, 0,
Sunil Ravi036cec52023-03-29 11:35:17 -07004834 is_p2p_allow_6ghz(wpa_s->global->p2p), 0, NULL);
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004835 } else {
Hai Shalomc1a21442022-02-04 13:43:00 -08004836 wpas_p2p_group_add(wpa_s, 1, freq, 0, 0, 0, 0, 0, 0,
4837 is_p2p_allow_6ghz(wpa_s->global->p2p));
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004838 }
4839
4840 return 1;
4841}
4842
4843
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004844static int wpas_p2p_get_pref_freq_list(void *ctx, int go,
4845 unsigned int *len,
Sunil8cd6f4d2022-06-28 18:40:46 +00004846 struct weighted_pcl *freq_list)
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004847{
4848 struct wpa_supplicant *wpa_s = ctx;
4849
4850 return wpa_drv_get_pref_freq_list(wpa_s, go ? WPA_IF_P2P_GO :
4851 WPA_IF_P2P_CLIENT, len, freq_list);
4852}
4853
Jimmy Chenf887c7b2018-11-13 15:19:57 +08004854int wpas_p2p_mac_setup(struct wpa_supplicant *wpa_s)
4855{
JaeMan Park9de97322022-07-11 15:36:43 +09004856 int ret = 0;
Jimmy Chenf887c7b2018-11-13 15:19:57 +08004857 u8 addr[ETH_ALEN] = {0};
4858
4859 if (wpa_s->conf->p2p_device_random_mac_addr == 0)
4860 return 0;
4861
Hai Shalom60840252021-02-19 19:02:11 -08004862 if (wpa_s->conf->p2p_device_random_mac_addr == 2) {
4863 if (is_zero_ether_addr(
4864 wpa_s->conf->p2p_device_persistent_mac_addr) &&
4865 !is_zero_ether_addr(wpa_s->own_addr)) {
4866 os_memcpy(wpa_s->conf->p2p_device_persistent_mac_addr,
4867 wpa_s->own_addr, ETH_ALEN);
4868 }
4869 return 0;
4870 }
4871
4872 if (!wpa_s->conf->ssid) {
Jimmy Chenf887c7b2018-11-13 15:19:57 +08004873 if (random_mac_addr(addr) < 0) {
4874 wpa_msg(wpa_s, MSG_INFO,
4875 "Failed to generate random MAC address");
4876 return -EINVAL;
4877 }
4878
Hai Shalom39ba6fc2019-01-22 12:40:38 -08004879 /* Store generated MAC address. */
4880 os_memcpy(wpa_s->conf->p2p_device_persistent_mac_addr, addr,
4881 ETH_ALEN);
Jimmy Chenf887c7b2018-11-13 15:19:57 +08004882 } else {
Hai Shalom39ba6fc2019-01-22 12:40:38 -08004883 /* If there are existing saved groups, restore last MAC address.
4884 * if there is no last used MAC address, the last one is
4885 * factory MAC. */
4886 if (is_zero_ether_addr(
4887 wpa_s->conf->p2p_device_persistent_mac_addr))
Jimmy Chenf887c7b2018-11-13 15:19:57 +08004888 return 0;
Hai Shalom39ba6fc2019-01-22 12:40:38 -08004889 os_memcpy(addr, wpa_s->conf->p2p_device_persistent_mac_addr,
4890 ETH_ALEN);
Jimmy Chenf887c7b2018-11-13 15:19:57 +08004891 wpa_msg(wpa_s, MSG_DEBUG, "Restore last used MAC address.");
4892 }
4893
JaeMan Park9de97322022-07-11 15:36:43 +09004894 ret = wpa_drv_set_mac_addr(wpa_s, addr);
4895
4896 if (ret < 0) {
Jimmy Chenf887c7b2018-11-13 15:19:57 +08004897 wpa_msg(wpa_s, MSG_INFO,
4898 "Failed to set random MAC address");
JaeMan Park9de97322022-07-11 15:36:43 +09004899 return ret;
Jimmy Chenf887c7b2018-11-13 15:19:57 +08004900 }
4901
JaeMan Park9de97322022-07-11 15:36:43 +09004902 ret = wpa_supplicant_update_mac_addr(wpa_s);
4903
4904 if (ret < 0) {
Jimmy Chenf887c7b2018-11-13 15:19:57 +08004905 wpa_msg(wpa_s, MSG_INFO,
4906 "Could not update MAC address information");
JaeMan Park9de97322022-07-11 15:36:43 +09004907 return ret;
Jimmy Chenf887c7b2018-11-13 15:19:57 +08004908 }
4909
4910 wpa_msg(wpa_s, MSG_DEBUG, "Using random MAC address " MACSTR,
4911 MAC2STR(addr));
4912
4913 return 0;
4914}
4915
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004916/**
4917 * wpas_p2p_init - Initialize P2P module for %wpa_supplicant
4918 * @global: Pointer to global data from wpa_supplicant_init()
4919 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
4920 * Returns: 0 on success, -1 on failure
4921 */
4922int wpas_p2p_init(struct wpa_global *global, struct wpa_supplicant *wpa_s)
4923{
4924 struct p2p_config p2p;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004925 int i;
4926
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07004927 if (wpa_s->conf->p2p_disabled)
4928 return 0;
4929
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004930 if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_CAPABLE))
4931 return 0;
4932
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004933 if (global->p2p)
4934 return 0;
4935
Jimmy Chenf887c7b2018-11-13 15:19:57 +08004936 if (wpas_p2p_mac_setup(wpa_s) < 0) {
4937 wpa_msg(wpa_s, MSG_ERROR,
4938 "Failed to initialize P2P random MAC address.");
4939 return -1;
4940 }
4941
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004942 os_memset(&p2p, 0, sizeof(p2p));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004943 p2p.cb_ctx = wpa_s;
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07004944 p2p.debug_print = wpas_p2p_debug_print;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004945 p2p.p2p_scan = wpas_p2p_scan;
4946 p2p.send_action = wpas_send_action;
4947 p2p.send_action_done = wpas_send_action_done;
4948 p2p.go_neg_completed = wpas_go_neg_completed;
4949 p2p.go_neg_req_rx = wpas_go_neg_req_rx;
4950 p2p.dev_found = wpas_dev_found;
4951 p2p.dev_lost = wpas_dev_lost;
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07004952 p2p.find_stopped = wpas_find_stopped;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004953 p2p.start_listen = wpas_start_listen;
4954 p2p.stop_listen = wpas_stop_listen;
4955 p2p.send_probe_resp = wpas_send_probe_resp;
4956 p2p.sd_request = wpas_sd_request;
4957 p2p.sd_response = wpas_sd_response;
4958 p2p.prov_disc_req = wpas_prov_disc_req;
4959 p2p.prov_disc_resp = wpas_prov_disc_resp;
Jouni Malinen75ecf522011-06-27 15:19:46 -07004960 p2p.prov_disc_fail = wpas_prov_disc_fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004961 p2p.invitation_process = wpas_invitation_process;
4962 p2p.invitation_received = wpas_invitation_received;
4963 p2p.invitation_result = wpas_invitation_result;
4964 p2p.get_noa = wpas_get_noa;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08004965 p2p.go_connected = wpas_go_connected;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08004966 p2p.presence_resp = wpas_presence_resp;
Dmitry Shmidt18463232014-01-24 12:29:41 -08004967 p2p.is_concurrent_session_active = wpas_is_concurrent_session_active;
Dmitry Shmidt5a1480c2014-05-12 09:46:02 -07004968 p2p.is_p2p_in_progress = _wpas_p2p_in_progress;
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004969 p2p.get_persistent_group = wpas_get_persistent_group;
4970 p2p.get_go_info = wpas_get_go_info;
4971 p2p.remove_stale_groups = wpas_remove_stale_groups;
4972 p2p.p2ps_prov_complete = wpas_p2ps_prov_complete;
4973 p2p.prov_disc_resp_cb = wpas_prov_disc_resp_cb;
4974 p2p.p2ps_group_capability = p2ps_group_capability;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004975 p2p.get_pref_freq_list = wpas_p2p_get_pref_freq_list;
Hai Shalom60840252021-02-19 19:02:11 -08004976 p2p.p2p_6ghz_disable = wpa_s->conf->p2p_6ghz_disable;
Shuibing Daie2fad412023-05-05 14:08:11 -07004977 p2p.p2p_dfs_chan_enable = wpa_s->conf->p2p_dfs_chan_enable;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004978
4979 os_memcpy(wpa_s->global->p2p_dev_addr, wpa_s->own_addr, ETH_ALEN);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004980 os_memcpy(p2p.dev_addr, wpa_s->global->p2p_dev_addr, ETH_ALEN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004981 p2p.dev_name = wpa_s->conf->device_name;
4982 p2p.manufacturer = wpa_s->conf->manufacturer;
4983 p2p.model_name = wpa_s->conf->model_name;
4984 p2p.model_number = wpa_s->conf->model_number;
4985 p2p.serial_number = wpa_s->conf->serial_number;
4986 if (wpa_s->wps) {
4987 os_memcpy(p2p.uuid, wpa_s->wps->uuid, 16);
4988 p2p.config_methods = wpa_s->wps->config_methods;
4989 }
4990
Hai Shalom60840252021-02-19 19:02:11 -08004991 if (wpas_p2p_setup_channels(wpa_s, &p2p.channels, &p2p.cli_channels,
4992 p2p.p2p_6ghz_disable)) {
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07004993 wpa_printf(MSG_ERROR,
4994 "P2P: Failed to configure supported channel list");
4995 return -1;
4996 }
4997
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004998 if (wpa_s->conf->p2p_listen_reg_class &&
4999 wpa_s->conf->p2p_listen_channel) {
5000 p2p.reg_class = wpa_s->conf->p2p_listen_reg_class;
5001 p2p.channel = wpa_s->conf->p2p_listen_channel;
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07005002 p2p.channel_forced = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005003 } else {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005004 /*
5005 * Pick one of the social channels randomly as the listen
5006 * channel.
5007 */
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07005008 if (p2p_config_get_random_social(&p2p, &p2p.reg_class,
Hai Shalom74f70d42019-02-11 14:42:39 -08005009 &p2p.channel,
5010 &global->p2p_go_avoid_freq,
5011 &global->p2p_disallow_freq) !=
5012 0) {
Dmitry Shmidt1d755d02015-04-28 10:34:29 -07005013 wpa_printf(MSG_INFO,
5014 "P2P: No social channels supported by the driver - do not enable P2P");
5015 return 0;
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07005016 }
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07005017 p2p.channel_forced = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005018 }
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07005019 wpa_printf(MSG_DEBUG, "P2P: Own listen channel: %d:%d",
5020 p2p.reg_class, p2p.channel);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005021
5022 if (wpa_s->conf->p2p_oper_reg_class &&
5023 wpa_s->conf->p2p_oper_channel) {
5024 p2p.op_reg_class = wpa_s->conf->p2p_oper_reg_class;
5025 p2p.op_channel = wpa_s->conf->p2p_oper_channel;
5026 p2p.cfg_op_channel = 1;
5027 wpa_printf(MSG_DEBUG, "P2P: Configured operating channel: "
5028 "%d:%d", p2p.op_reg_class, p2p.op_channel);
5029
5030 } else {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005031 /*
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07005032 * Use random operation channel from 2.4 GHz band social
5033 * channels (1, 6, 11) or band 60 GHz social channel (2) if no
5034 * other preference is indicated.
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005035 */
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07005036 if (p2p_config_get_random_social(&p2p, &p2p.op_reg_class,
Hai Shalom74f70d42019-02-11 14:42:39 -08005037 &p2p.op_channel, NULL,
5038 NULL) != 0) {
5039 wpa_printf(MSG_INFO,
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07005040 "P2P: Failed to select random social channel as operation channel");
Hai Shalom74f70d42019-02-11 14:42:39 -08005041 p2p.op_reg_class = 0;
5042 p2p.op_channel = 0;
5043 /* This will be overridden during group setup in
5044 * p2p_prepare_channel(), so allow setup to continue. */
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07005045 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005046 p2p.cfg_op_channel = 0;
5047 wpa_printf(MSG_DEBUG, "P2P: Random operating channel: "
5048 "%d:%d", p2p.op_reg_class, p2p.op_channel);
5049 }
Dmitry Shmidt44c95782013-05-17 09:51:35 -07005050
5051 if (wpa_s->conf->p2p_pref_chan && wpa_s->conf->num_p2p_pref_chan) {
5052 p2p.pref_chan = wpa_s->conf->p2p_pref_chan;
5053 p2p.num_pref_chan = wpa_s->conf->num_p2p_pref_chan;
5054 }
5055
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005056 if (wpa_s->conf->country[0] && wpa_s->conf->country[1]) {
5057 os_memcpy(p2p.country, wpa_s->conf->country, 2);
5058 p2p.country[2] = 0x04;
5059 } else
5060 os_memcpy(p2p.country, "XX\x04", 3);
5061
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005062 os_memcpy(p2p.pri_dev_type, wpa_s->conf->device_type,
5063 WPS_DEV_TYPE_LEN);
5064
5065 p2p.num_sec_dev_types = wpa_s->conf->num_sec_device_types;
5066 os_memcpy(p2p.sec_dev_type, wpa_s->conf->sec_device_type,
5067 p2p.num_sec_dev_types * WPS_DEV_TYPE_LEN);
5068
5069 p2p.concurrent_operations = !!(wpa_s->drv_flags &
5070 WPA_DRIVER_FLAGS_P2P_CONCURRENT);
5071
5072 p2p.max_peers = 100;
5073
5074 if (wpa_s->conf->p2p_ssid_postfix) {
5075 p2p.ssid_postfix_len =
5076 os_strlen(wpa_s->conf->p2p_ssid_postfix);
5077 if (p2p.ssid_postfix_len > sizeof(p2p.ssid_postfix))
5078 p2p.ssid_postfix_len = sizeof(p2p.ssid_postfix);
5079 os_memcpy(p2p.ssid_postfix, wpa_s->conf->p2p_ssid_postfix,
5080 p2p.ssid_postfix_len);
5081 }
5082
5083 p2p.p2p_intra_bss = wpa_s->conf->p2p_intra_bss;
5084
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08005085 p2p.max_listen = wpa_s->max_remain_on_chan;
5086
Dmitry Shmidt2271d3f2014-06-23 12:16:31 -07005087 if (wpa_s->conf->p2p_passphrase_len >= 8 &&
5088 wpa_s->conf->p2p_passphrase_len <= 63)
5089 p2p.passphrase_len = wpa_s->conf->p2p_passphrase_len;
5090 else
5091 p2p.passphrase_len = 8;
5092
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005093 global->p2p = p2p_init(&p2p);
5094 if (global->p2p == NULL)
5095 return -1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07005096 global->p2p_init_wpa_s = wpa_s;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005097
5098 for (i = 0; i < MAX_WPS_VENDOR_EXT; i++) {
5099 if (wpa_s->conf->wps_vendor_ext[i] == NULL)
5100 continue;
5101 p2p_add_wps_vendor_extension(
5102 global->p2p, wpa_s->conf->wps_vendor_ext[i]);
5103 }
5104
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07005105 p2p_set_no_go_freq(global->p2p, &wpa_s->conf->p2p_no_go_freq);
5106
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005107 return 0;
5108}
5109
5110
5111/**
5112 * wpas_p2p_deinit - Deinitialize per-interface P2P data
5113 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
5114 *
5115 * This function deinitialize per-interface P2P data.
5116 */
5117void wpas_p2p_deinit(struct wpa_supplicant *wpa_s)
5118{
5119 if (wpa_s->driver && wpa_s->drv_priv)
5120 wpa_drv_probe_req_report(wpa_s, 0);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005121
5122 if (wpa_s->go_params) {
5123 /* Clear any stored provisioning info */
5124 p2p_clear_provisioning_info(
5125 wpa_s->global->p2p,
Dmitry Shmidt04949592012-07-19 12:16:46 -07005126 wpa_s->go_params->peer_device_addr);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005127 }
5128
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005129 os_free(wpa_s->go_params);
5130 wpa_s->go_params = NULL;
Dmitry Shmidt684785c2014-05-12 13:34:29 -07005131 eloop_cancel_timeout(wpas_p2p_psk_failure_removal, wpa_s, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005132 eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s, NULL);
5133 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
Hai Shalomfdcde762020-04-02 11:19:20 -07005134 wpa_s->global->p2p_long_listen = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005135 eloop_cancel_timeout(wpas_p2p_long_listen_timeout, wpa_s, NULL);
5136 eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL);
5137 wpas_p2p_remove_pending_group_interface(wpa_s);
Dmitry Shmidt04f534e2013-12-09 15:50:16 -08005138 eloop_cancel_timeout(wpas_p2p_group_freq_conflict, wpa_s, NULL);
Hai Shalom899fcc72020-10-19 14:38:18 -07005139 eloop_cancel_timeout(wpas_p2p_reconsider_moving_go, wpa_s, NULL);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005140 wpas_p2p_listen_work_done(wpa_s);
5141 if (wpa_s->p2p_send_action_work) {
5142 os_free(wpa_s->p2p_send_action_work->ctx);
5143 radio_work_done(wpa_s->p2p_send_action_work);
5144 wpa_s->p2p_send_action_work = NULL;
5145 }
5146 eloop_cancel_timeout(wpas_p2p_send_action_work_timeout, wpa_s, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005147
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08005148 wpabuf_free(wpa_s->p2p_oob_dev_pw);
5149 wpa_s->p2p_oob_dev_pw = NULL;
5150
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005151 os_free(wpa_s->p2p_group_common_freqs);
5152 wpa_s->p2p_group_common_freqs = NULL;
5153 wpa_s->p2p_group_common_freqs_num = 0;
5154
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005155 /* TODO: remove group interface from the driver if this wpa_s instance
5156 * is on top of a P2P group interface */
5157}
5158
5159
5160/**
5161 * wpas_p2p_deinit_global - Deinitialize global P2P module
5162 * @global: Pointer to global data from wpa_supplicant_init()
5163 *
5164 * This function deinitializes the global (per device) P2P module.
5165 */
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07005166static void wpas_p2p_deinit_global(struct wpa_global *global)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005167{
5168 struct wpa_supplicant *wpa_s, *tmp;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005169
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005170 wpa_s = global->ifaces;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005171
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07005172 wpas_p2p_service_flush(global->p2p_init_wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005173
5174 /* Remove remaining P2P group interfaces */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005175 while (wpa_s && wpa_s->p2p_group_interface != NOT_P2P_GROUP_INTERFACE)
5176 wpa_s = wpa_s->next;
5177 while (wpa_s) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005178 tmp = global->ifaces;
5179 while (tmp &&
5180 (tmp == wpa_s ||
5181 tmp->p2p_group_interface == NOT_P2P_GROUP_INTERFACE)) {
5182 tmp = tmp->next;
5183 }
5184 if (tmp == NULL)
5185 break;
Dmitry Shmidt04949592012-07-19 12:16:46 -07005186 /* Disconnect from the P2P group and deinit the interface */
5187 wpas_p2p_disconnect(tmp);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005188 }
5189
5190 /*
5191 * Deinit GO data on any possibly remaining interface (if main
5192 * interface is used as GO).
5193 */
5194 for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
5195 if (wpa_s->ap_iface)
5196 wpas_p2p_group_deinit(wpa_s);
5197 }
5198
5199 p2p_deinit(global->p2p);
5200 global->p2p = NULL;
Dmitry Shmidt04949592012-07-19 12:16:46 -07005201 global->p2p_init_wpa_s = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005202}
5203
5204
5205static int wpas_p2p_create_iface(struct wpa_supplicant *wpa_s)
5206{
Dmitry Shmidt9c175262016-03-03 10:20:07 -08005207 if (wpa_s->conf->p2p_no_group_iface)
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08005208 return 0; /* separate interface disabled per configuration */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005209 if (wpa_s->drv_flags &
5210 (WPA_DRIVER_FLAGS_P2P_DEDICATED_INTERFACE |
5211 WPA_DRIVER_FLAGS_P2P_MGMT_AND_NON_P2P))
5212 return 1; /* P2P group requires a new interface in every case
5213 */
5214 if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_CONCURRENT))
5215 return 0; /* driver does not support concurrent operations */
5216 if (wpa_s->global->ifaces->next)
5217 return 1; /* more that one interface already in use */
5218 if (wpa_s->wpa_state >= WPA_AUTHENTICATING)
5219 return 1; /* this interface is already in use */
5220 return 0;
5221}
5222
5223
5224static int wpas_p2p_start_go_neg(struct wpa_supplicant *wpa_s,
5225 const u8 *peer_addr,
5226 enum p2p_wps_method wps_method,
5227 int go_intent, const u8 *own_interface_addr,
Dmitry Shmidt04949592012-07-19 12:16:46 -07005228 unsigned int force_freq, int persistent_group,
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08005229 struct wpa_ssid *ssid, unsigned int pref_freq)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005230{
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005231 if (persistent_group && wpa_s->conf->persistent_reconnect)
5232 persistent_group = 2;
5233
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005234 /*
5235 * Increase GO config timeout if HT40 is used since it takes some time
5236 * to scan channels for coex purposes before the BSS can be started.
5237 */
5238 p2p_set_config_timeout(wpa_s->global->p2p,
5239 wpa_s->p2p_go_ht40 ? 255 : 100, 20);
5240
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005241 return p2p_connect(wpa_s->global->p2p, peer_addr, wps_method,
5242 go_intent, own_interface_addr, force_freq,
Dmitry Shmidt04949592012-07-19 12:16:46 -07005243 persistent_group, ssid ? ssid->ssid : NULL,
5244 ssid ? ssid->ssid_len : 0,
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08005245 wpa_s->p2p_pd_before_go_neg, pref_freq,
5246 wps_method == WPS_NFC ? wpa_s->p2p_oob_dev_pw_id :
5247 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005248}
5249
5250
5251static int wpas_p2p_auth_go_neg(struct wpa_supplicant *wpa_s,
5252 const u8 *peer_addr,
5253 enum p2p_wps_method wps_method,
5254 int go_intent, const u8 *own_interface_addr,
Dmitry Shmidt04949592012-07-19 12:16:46 -07005255 unsigned int force_freq, int persistent_group,
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08005256 struct wpa_ssid *ssid, unsigned int pref_freq)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005257{
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005258 if (persistent_group && wpa_s->conf->persistent_reconnect)
5259 persistent_group = 2;
5260
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005261 return p2p_authorize(wpa_s->global->p2p, peer_addr, wps_method,
5262 go_intent, own_interface_addr, force_freq,
Dmitry Shmidt04949592012-07-19 12:16:46 -07005263 persistent_group, ssid ? ssid->ssid : NULL,
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08005264 ssid ? ssid->ssid_len : 0, pref_freq,
5265 wps_method == WPS_NFC ? wpa_s->p2p_oob_dev_pw_id :
5266 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005267}
5268
5269
5270static void wpas_p2p_check_join_scan_limit(struct wpa_supplicant *wpa_s)
5271{
5272 wpa_s->p2p_join_scan_count++;
5273 wpa_printf(MSG_DEBUG, "P2P: Join scan attempt %d",
5274 wpa_s->p2p_join_scan_count);
5275 if (wpa_s->p2p_join_scan_count > P2P_MAX_JOIN_SCAN_ATTEMPTS) {
5276 wpa_printf(MSG_DEBUG, "P2P: Failed to find GO " MACSTR
5277 " for join operationg - stop join attempt",
5278 MAC2STR(wpa_s->pending_join_iface_addr));
5279 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
Dmitry Shmidt04949592012-07-19 12:16:46 -07005280 if (wpa_s->p2p_auto_pd) {
5281 wpa_s->p2p_auto_pd = 0;
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07005282 wpa_msg_global(wpa_s, MSG_INFO,
5283 P2P_EVENT_PROV_DISC_FAILURE
5284 " p2p_dev_addr=" MACSTR " status=N/A",
5285 MAC2STR(wpa_s->pending_join_dev_addr));
Dmitry Shmidt04949592012-07-19 12:16:46 -07005286 return;
5287 }
Jimmy Chenb7b3f4d2020-09-02 16:50:11 +08005288 if (wpa_s->p2p_fallback_to_go_neg) {
5289 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Join operating "
5290 "failed - fall back to GO Negotiation");
5291 wpa_msg_global(wpa_s->p2pdev, MSG_INFO,
5292 P2P_EVENT_FALLBACK_TO_GO_NEG
5293 "reason=join-failed");
5294 wpas_p2p_fallback_to_go_neg(wpa_s, 0);
5295 return;
5296 }
Dmitry Shmidt9c175262016-03-03 10:20:07 -08005297 wpa_msg_global(wpa_s->p2pdev, MSG_INFO,
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07005298 P2P_EVENT_GROUP_FORMATION_FAILURE);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005299 wpas_notify_p2p_group_formation_failure(wpa_s, "");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005300 }
5301}
5302
5303
Dmitry Shmidt04949592012-07-19 12:16:46 -07005304static int wpas_check_freq_conflict(struct wpa_supplicant *wpa_s, int freq)
5305{
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07005306 int res;
5307 unsigned int num, i;
5308 struct wpa_used_freq_data *freqs;
Dmitry Shmidt04949592012-07-19 12:16:46 -07005309
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07005310 if (wpas_p2p_num_unused_channels(wpa_s) > 0) {
5311 /* Multiple channels are supported and not all are in use */
Dmitry Shmidt04949592012-07-19 12:16:46 -07005312 return 0;
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07005313 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07005314
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07005315 freqs = os_calloc(wpa_s->num_multichan_concurrent,
5316 sizeof(struct wpa_used_freq_data));
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07005317 if (!freqs)
5318 return 1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07005319
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07005320 num = wpas_p2p_valid_oper_freqs(wpa_s, freqs,
5321 wpa_s->num_multichan_concurrent);
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07005322
5323 for (i = 0; i < num; i++) {
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07005324 if (freqs[i].freq == freq) {
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07005325 wpa_printf(MSG_DEBUG, "P2P: Frequency %d MHz in use by another virtual interface and can be used",
5326 freq);
5327 res = 0;
5328 goto exit_free;
Dmitry Shmidt04949592012-07-19 12:16:46 -07005329 }
5330 }
5331
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07005332 wpa_printf(MSG_DEBUG, "P2P: No valid operating frequencies");
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07005333 res = 1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07005334
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07005335exit_free:
5336 os_free(freqs);
5337 return res;
Dmitry Shmidt04949592012-07-19 12:16:46 -07005338}
5339
5340
5341static int wpas_p2p_peer_go(struct wpa_supplicant *wpa_s,
5342 const u8 *peer_dev_addr)
5343{
5344 struct wpa_bss *bss;
5345 int updated;
5346
5347 bss = wpa_bss_get_p2p_dev_addr(wpa_s, peer_dev_addr);
5348 if (bss == NULL)
5349 return -1;
5350 if (bss->last_update_idx < wpa_s->bss_update_idx) {
5351 wpa_printf(MSG_DEBUG, "P2P: Peer BSS entry not updated in the "
5352 "last scan");
5353 return 0;
5354 }
5355
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005356 updated = os_reltime_before(&wpa_s->p2p_auto_started,
5357 &bss->last_update);
Dmitry Shmidt04949592012-07-19 12:16:46 -07005358 wpa_printf(MSG_DEBUG, "P2P: Current BSS entry for peer updated at "
5359 "%ld.%06ld (%supdated in last scan)",
5360 bss->last_update.sec, bss->last_update.usec,
5361 updated ? "": "not ");
5362
5363 return updated;
5364}
5365
5366
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005367static void wpas_p2p_scan_res_join(struct wpa_supplicant *wpa_s,
5368 struct wpa_scan_results *scan_res)
5369{
Dmitry Shmidt344abd32014-01-14 13:17:00 -08005370 struct wpa_bss *bss = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005371 int freq;
5372 u8 iface_addr[ETH_ALEN];
Dmitry Shmidt04949592012-07-19 12:16:46 -07005373
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005374 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
5375
5376 if (wpa_s->global->p2p_disabled)
5377 return;
5378
Dmitry Shmidt04949592012-07-19 12:16:46 -07005379 wpa_printf(MSG_DEBUG, "P2P: Scan results received (%d BSS) for %sjoin",
5380 scan_res ? (int) scan_res->num : -1,
5381 wpa_s->p2p_auto_join ? "auto_" : "");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005382
5383 if (scan_res)
5384 wpas_p2p_scan_res_handler(wpa_s, scan_res);
5385
Dmitry Shmidt04949592012-07-19 12:16:46 -07005386 if (wpa_s->p2p_auto_pd) {
5387 int join = wpas_p2p_peer_go(wpa_s,
5388 wpa_s->pending_join_dev_addr);
5389 if (join == 0 &&
5390 wpa_s->auto_pd_scan_retry < P2P_AUTO_PD_SCAN_ATTEMPTS) {
5391 wpa_s->auto_pd_scan_retry++;
Dmitry Shmidt444d5672013-04-01 13:08:44 -07005392 bss = wpa_bss_get_bssid_latest(
5393 wpa_s, wpa_s->pending_join_dev_addr);
Dmitry Shmidt04949592012-07-19 12:16:46 -07005394 if (bss) {
5395 freq = bss->freq;
5396 wpa_printf(MSG_DEBUG, "P2P: Scan retry %d for "
5397 "the peer " MACSTR " at %d MHz",
5398 wpa_s->auto_pd_scan_retry,
5399 MAC2STR(wpa_s->
5400 pending_join_dev_addr),
5401 freq);
Dmitry Shmidt344abd32014-01-14 13:17:00 -08005402 wpas_p2p_join_scan_req(wpa_s, freq, NULL, 0);
Dmitry Shmidt04949592012-07-19 12:16:46 -07005403 return;
5404 }
5405 }
5406
5407 if (join < 0)
5408 join = 0;
5409
5410 wpa_s->p2p_auto_pd = 0;
5411 wpa_s->pending_pd_use = join ? AUTO_PD_JOIN : AUTO_PD_GO_NEG;
5412 wpa_printf(MSG_DEBUG, "P2P: Auto PD with " MACSTR " join=%d",
5413 MAC2STR(wpa_s->pending_join_dev_addr), join);
5414 if (p2p_prov_disc_req(wpa_s->global->p2p,
Dmitry Shmidt216983b2015-02-06 10:50:36 -08005415 wpa_s->pending_join_dev_addr, NULL,
Dmitry Shmidt04949592012-07-19 12:16:46 -07005416 wpa_s->pending_pd_config_methods, join,
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08005417 0, wpa_s->user_initiated_pd) < 0) {
Dmitry Shmidt04949592012-07-19 12:16:46 -07005418 wpa_s->p2p_auto_pd = 0;
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07005419 wpa_msg_global(wpa_s, MSG_INFO,
5420 P2P_EVENT_PROV_DISC_FAILURE
5421 " p2p_dev_addr=" MACSTR " status=N/A",
5422 MAC2STR(wpa_s->pending_join_dev_addr));
Dmitry Shmidt04949592012-07-19 12:16:46 -07005423 }
5424 return;
5425 }
5426
5427 if (wpa_s->p2p_auto_join) {
5428 int join = wpas_p2p_peer_go(wpa_s,
5429 wpa_s->pending_join_dev_addr);
5430 if (join < 0) {
5431 wpa_printf(MSG_DEBUG, "P2P: Peer was not found to be "
5432 "running a GO -> use GO Negotiation");
Dmitry Shmidt9c175262016-03-03 10:20:07 -08005433 wpa_msg_global(wpa_s->p2pdev, MSG_INFO,
Dmitry Shmidt7f656022015-02-25 14:36:37 -08005434 P2P_EVENT_FALLBACK_TO_GO_NEG
5435 "reason=peer-not-running-GO");
Dmitry Shmidt04949592012-07-19 12:16:46 -07005436 wpas_p2p_connect(wpa_s, wpa_s->pending_join_dev_addr,
5437 wpa_s->p2p_pin, wpa_s->p2p_wps_method,
5438 wpa_s->p2p_persistent_group, 0, 0, 0,
5439 wpa_s->p2p_go_intent,
5440 wpa_s->p2p_connect_freq,
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08005441 wpa_s->p2p_go_vht_center_freq2,
Dmitry Shmidt04949592012-07-19 12:16:46 -07005442 wpa_s->p2p_persistent_id,
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005443 wpa_s->p2p_pd_before_go_neg,
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07005444 wpa_s->p2p_go_ht40,
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08005445 wpa_s->p2p_go_vht,
Dmitry Shmidtde47be72016-01-07 12:52:55 -08005446 wpa_s->p2p_go_max_oper_chwidth,
Hai Shalom74f70d42019-02-11 14:42:39 -08005447 wpa_s->p2p_go_he,
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08005448 wpa_s->p2p_go_edmg,
Hai Shalomc1a21442022-02-04 13:43:00 -08005449 NULL, 0,
5450 is_p2p_allow_6ghz(wpa_s->global->p2p));
Dmitry Shmidt04949592012-07-19 12:16:46 -07005451 return;
5452 }
5453
5454 wpa_printf(MSG_DEBUG, "P2P: Peer was found running GO%s -> "
5455 "try to join the group", join ? "" :
5456 " in older scan");
Dmitry Shmidt7f656022015-02-25 14:36:37 -08005457 if (!join) {
Dmitry Shmidt9c175262016-03-03 10:20:07 -08005458 wpa_msg_global(wpa_s->p2pdev, MSG_INFO,
Dmitry Shmidt7f656022015-02-25 14:36:37 -08005459 P2P_EVENT_FALLBACK_TO_GO_NEG_ENABLED);
Dmitry Shmidt04949592012-07-19 12:16:46 -07005460 wpa_s->p2p_fallback_to_go_neg = 1;
Dmitry Shmidt7f656022015-02-25 14:36:37 -08005461 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07005462 }
5463
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005464 freq = p2p_get_oper_freq(wpa_s->global->p2p,
5465 wpa_s->pending_join_iface_addr);
5466 if (freq < 0 &&
5467 p2p_get_interface_addr(wpa_s->global->p2p,
5468 wpa_s->pending_join_dev_addr,
5469 iface_addr) == 0 &&
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07005470 os_memcmp(iface_addr, wpa_s->pending_join_dev_addr, ETH_ALEN) != 0
5471 && !wpa_bss_get_bssid(wpa_s, wpa_s->pending_join_iface_addr)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005472 wpa_printf(MSG_DEBUG, "P2P: Overwrite pending interface "
5473 "address for join from " MACSTR " to " MACSTR
5474 " based on newly discovered P2P peer entry",
5475 MAC2STR(wpa_s->pending_join_iface_addr),
5476 MAC2STR(iface_addr));
5477 os_memcpy(wpa_s->pending_join_iface_addr, iface_addr,
5478 ETH_ALEN);
5479
5480 freq = p2p_get_oper_freq(wpa_s->global->p2p,
5481 wpa_s->pending_join_iface_addr);
5482 }
5483 if (freq >= 0) {
5484 wpa_printf(MSG_DEBUG, "P2P: Target GO operating frequency "
5485 "from P2P peer table: %d MHz", freq);
5486 }
Dmitry Shmidt344abd32014-01-14 13:17:00 -08005487 if (wpa_s->p2p_join_ssid_len) {
5488 wpa_printf(MSG_DEBUG, "P2P: Trying to find target GO BSS entry based on BSSID "
5489 MACSTR " and SSID %s",
5490 MAC2STR(wpa_s->pending_join_iface_addr),
5491 wpa_ssid_txt(wpa_s->p2p_join_ssid,
5492 wpa_s->p2p_join_ssid_len));
5493 bss = wpa_bss_get(wpa_s, wpa_s->pending_join_iface_addr,
5494 wpa_s->p2p_join_ssid,
5495 wpa_s->p2p_join_ssid_len);
Dmitry Shmidtde47be72016-01-07 12:52:55 -08005496 } else if (!bss) {
Dmitry Shmidt344abd32014-01-14 13:17:00 -08005497 wpa_printf(MSG_DEBUG, "P2P: Trying to find target GO BSS entry based on BSSID "
5498 MACSTR, MAC2STR(wpa_s->pending_join_iface_addr));
5499 bss = wpa_bss_get_bssid_latest(wpa_s,
5500 wpa_s->pending_join_iface_addr);
5501 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005502 if (bss) {
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07005503 u8 dev_addr[ETH_ALEN];
5504
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005505 freq = bss->freq;
5506 wpa_printf(MSG_DEBUG, "P2P: Target GO operating frequency "
Dmitry Shmidt051af732013-10-22 13:52:46 -07005507 "from BSS table: %d MHz (SSID %s)", freq,
5508 wpa_ssid_txt(bss->ssid, bss->ssid_len));
Hai Shalom60840252021-02-19 19:02:11 -08005509 if (p2p_parse_dev_addr(wpa_bss_ie_ptr(bss), bss->ie_len,
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07005510 dev_addr) == 0 &&
5511 os_memcmp(wpa_s->pending_join_dev_addr,
5512 wpa_s->pending_join_iface_addr, ETH_ALEN) == 0 &&
5513 os_memcmp(dev_addr, wpa_s->pending_join_dev_addr,
5514 ETH_ALEN) != 0) {
5515 wpa_printf(MSG_DEBUG,
5516 "P2P: Update target GO device address based on BSS entry: " MACSTR " (was " MACSTR ")",
5517 MAC2STR(dev_addr),
5518 MAC2STR(wpa_s->pending_join_dev_addr));
5519 os_memcpy(wpa_s->pending_join_dev_addr, dev_addr,
5520 ETH_ALEN);
5521 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005522 }
5523 if (freq > 0) {
5524 u16 method;
5525
Dmitry Shmidt04949592012-07-19 12:16:46 -07005526 if (wpas_check_freq_conflict(wpa_s, freq) > 0) {
Dmitry Shmidt9c175262016-03-03 10:20:07 -08005527 wpa_msg_global(wpa_s->p2pdev, MSG_INFO,
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07005528 P2P_EVENT_GROUP_FORMATION_FAILURE
5529 "reason=FREQ_CONFLICT");
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005530 wpas_notify_p2p_group_formation_failure(
5531 wpa_s, "FREQ_CONFLICT");
Dmitry Shmidt04949592012-07-19 12:16:46 -07005532 return;
5533 }
5534
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005535 wpa_printf(MSG_DEBUG, "P2P: Send Provision Discovery Request "
5536 "prior to joining an existing group (GO " MACSTR
5537 " freq=%u MHz)",
5538 MAC2STR(wpa_s->pending_join_dev_addr), freq);
5539 wpa_s->pending_pd_before_join = 1;
5540
5541 switch (wpa_s->pending_join_wps_method) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005542 case WPS_PIN_DISPLAY:
5543 method = WPS_CONFIG_KEYPAD;
5544 break;
5545 case WPS_PIN_KEYPAD:
5546 method = WPS_CONFIG_DISPLAY;
5547 break;
5548 case WPS_PBC:
5549 method = WPS_CONFIG_PUSHBUTTON;
5550 break;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005551 case WPS_P2PS:
5552 method = WPS_CONFIG_P2PS;
5553 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005554 default:
5555 method = 0;
5556 break;
5557 }
5558
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005559 if ((p2p_get_provisioning_info(wpa_s->global->p2p,
5560 wpa_s->pending_join_dev_addr) ==
5561 method)) {
5562 /*
5563 * We have already performed provision discovery for
5564 * joining the group. Proceed directly to join
5565 * operation without duplicated provision discovery. */
5566 wpa_printf(MSG_DEBUG, "P2P: Provision discovery "
5567 "with " MACSTR " already done - proceed to "
5568 "join",
5569 MAC2STR(wpa_s->pending_join_dev_addr));
5570 wpa_s->pending_pd_before_join = 0;
5571 goto start;
5572 }
5573
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005574 if (p2p_prov_disc_req(wpa_s->global->p2p,
Dmitry Shmidt216983b2015-02-06 10:50:36 -08005575 wpa_s->pending_join_dev_addr,
5576 NULL, method, 1,
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08005577 freq, wpa_s->user_initiated_pd) < 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005578 wpa_printf(MSG_DEBUG, "P2P: Failed to send Provision "
5579 "Discovery Request before joining an "
5580 "existing group");
5581 wpa_s->pending_pd_before_join = 0;
5582 goto start;
5583 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005584 return;
5585 }
5586
5587 wpa_printf(MSG_DEBUG, "P2P: Failed to find BSS/GO - try again later");
5588 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
5589 eloop_register_timeout(1, 0, wpas_p2p_join_scan, wpa_s, NULL);
5590 wpas_p2p_check_join_scan_limit(wpa_s);
5591 return;
5592
5593start:
5594 /* Start join operation immediately */
Dmitry Shmidtde47be72016-01-07 12:52:55 -08005595 wpas_p2p_join_start(wpa_s, 0, wpa_s->p2p_join_ssid,
5596 wpa_s->p2p_join_ssid_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005597}
5598
5599
Dmitry Shmidt344abd32014-01-14 13:17:00 -08005600static void wpas_p2p_join_scan_req(struct wpa_supplicant *wpa_s, int freq,
5601 const u8 *ssid, size_t ssid_len)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005602{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005603 int ret;
5604 struct wpa_driver_scan_params params;
5605 struct wpabuf *wps_ie, *ies;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005606 size_t ielen;
Dmitry Shmidt04949592012-07-19 12:16:46 -07005607 int freqs[2] = { 0, 0 };
Dmitry Shmidt9c175262016-03-03 10:20:07 -08005608 unsigned int bands;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005609
5610 os_memset(&params, 0, sizeof(params));
5611
5612 /* P2P Wildcard SSID */
5613 params.num_ssids = 1;
Dmitry Shmidt344abd32014-01-14 13:17:00 -08005614 if (ssid && ssid_len) {
5615 params.ssids[0].ssid = ssid;
5616 params.ssids[0].ssid_len = ssid_len;
5617 os_memcpy(wpa_s->p2p_join_ssid, ssid, ssid_len);
5618 wpa_s->p2p_join_ssid_len = ssid_len;
5619 } else {
5620 params.ssids[0].ssid = (u8 *) P2P_WILDCARD_SSID;
5621 params.ssids[0].ssid_len = P2P_WILDCARD_SSID_LEN;
5622 wpa_s->p2p_join_ssid_len = 0;
5623 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005624
5625 wpa_s->wps->dev.p2p = 1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07005626 wps_ie = wps_build_probe_req_ie(DEV_PW_DEFAULT, &wpa_s->wps->dev,
5627 wpa_s->wps->uuid, WPS_REQ_ENROLLEE, 0,
5628 NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005629 if (wps_ie == NULL) {
5630 wpas_p2p_scan_res_join(wpa_s, NULL);
5631 return;
5632 }
5633
Dmitry Shmidt9e3f8ee2014-01-17 10:52:01 -08005634 if (!freq) {
5635 int oper_freq;
5636 /*
5637 * If freq is not provided, check the operating freq of the GO
5638 * and use a single channel scan on if possible.
5639 */
5640 oper_freq = p2p_get_oper_freq(wpa_s->global->p2p,
5641 wpa_s->pending_join_iface_addr);
5642 if (oper_freq > 0)
5643 freq = oper_freq;
5644 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07005645 if (freq > 0) {
5646 freqs[0] = freq;
5647 params.freqs = freqs;
Sunil Ravi77d572f2023-01-17 23:58:31 +00005648 } else {
5649 wpas_p2p_scan_freqs(wpa_s, &params, true);
Dmitry Shmidt04949592012-07-19 12:16:46 -07005650 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005651
Dmitry Shmidt9c175262016-03-03 10:20:07 -08005652 ielen = p2p_scan_ie_buf_len(wpa_s->global->p2p);
5653 ies = wpabuf_alloc(wpabuf_len(wps_ie) + ielen);
5654 if (ies == NULL) {
5655 wpabuf_free(wps_ie);
5656 wpas_p2p_scan_res_join(wpa_s, NULL);
5657 return;
5658 }
5659 wpabuf_put_buf(ies, wps_ie);
5660 wpabuf_free(wps_ie);
5661
5662 bands = wpas_get_bands(wpa_s, freqs);
5663 p2p_scan_ie(wpa_s->global->p2p, ies, NULL, bands);
5664
5665 params.p2p_probe = 1;
5666 params.extra_ies = wpabuf_head(ies);
5667 params.extra_ies_len = wpabuf_len(ies);
5668
Dmitry Shmidtebd93af2017-02-21 13:40:44 -08005669 if (wpa_s->clear_driver_scan_cache) {
5670 wpa_printf(MSG_DEBUG,
5671 "Request driver to clear scan cache due to local BSS flush");
5672 params.only_new_results = 1;
5673 }
5674
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005675 /*
5676 * Run a scan to update BSS table and start Provision Discovery once
5677 * the new scan results become available.
5678 */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005679 ret = wpa_drv_scan(wpa_s, &params);
Hai Shalomc1a21442022-02-04 13:43:00 -08005680 if (params.freqs != freqs)
Hai Shalom899fcc72020-10-19 14:38:18 -07005681 os_free(params.freqs);
Dmitry Shmidt444d5672013-04-01 13:08:44 -07005682 if (!ret) {
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005683 os_get_reltime(&wpa_s->scan_trigger_time);
Dmitry Shmidt04949592012-07-19 12:16:46 -07005684 wpa_s->scan_res_handler = wpas_p2p_scan_res_join;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005685 wpa_s->own_scan_requested = 1;
Dmitry Shmidtebd93af2017-02-21 13:40:44 -08005686 wpa_s->clear_driver_scan_cache = 0;
Dmitry Shmidt444d5672013-04-01 13:08:44 -07005687 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005688
5689 wpabuf_free(ies);
5690
5691 if (ret) {
5692 wpa_printf(MSG_DEBUG, "P2P: Failed to start scan for join - "
5693 "try again later");
5694 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
5695 eloop_register_timeout(1, 0, wpas_p2p_join_scan, wpa_s, NULL);
5696 wpas_p2p_check_join_scan_limit(wpa_s);
5697 }
5698}
5699
5700
Dmitry Shmidt04949592012-07-19 12:16:46 -07005701static void wpas_p2p_join_scan(void *eloop_ctx, void *timeout_ctx)
5702{
5703 struct wpa_supplicant *wpa_s = eloop_ctx;
Dmitry Shmidt344abd32014-01-14 13:17:00 -08005704 wpas_p2p_join_scan_req(wpa_s, 0, NULL, 0);
Dmitry Shmidt04949592012-07-19 12:16:46 -07005705}
5706
5707
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005708static int wpas_p2p_join(struct wpa_supplicant *wpa_s, const u8 *iface_addr,
Dmitry Shmidt04949592012-07-19 12:16:46 -07005709 const u8 *dev_addr, enum p2p_wps_method wps_method,
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08005710 int auto_join, int op_freq,
5711 const u8 *ssid, size_t ssid_len)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005712{
5713 wpa_printf(MSG_DEBUG, "P2P: Request to join existing group (iface "
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08005714 MACSTR " dev " MACSTR " op_freq=%d)%s",
5715 MAC2STR(iface_addr), MAC2STR(dev_addr), op_freq,
Dmitry Shmidt04949592012-07-19 12:16:46 -07005716 auto_join ? " (auto_join)" : "");
Dmitry Shmidt344abd32014-01-14 13:17:00 -08005717 if (ssid && ssid_len) {
5718 wpa_printf(MSG_DEBUG, "P2P: Group SSID specified: %s",
5719 wpa_ssid_txt(ssid, ssid_len));
5720 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005721
Dmitry Shmidt04949592012-07-19 12:16:46 -07005722 wpa_s->p2p_auto_pd = 0;
5723 wpa_s->p2p_auto_join = !!auto_join;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005724 os_memcpy(wpa_s->pending_join_iface_addr, iface_addr, ETH_ALEN);
5725 os_memcpy(wpa_s->pending_join_dev_addr, dev_addr, ETH_ALEN);
5726 wpa_s->pending_join_wps_method = wps_method;
5727
5728 /* Make sure we are not running find during connection establishment */
5729 wpas_p2p_stop_find(wpa_s);
5730
5731 wpa_s->p2p_join_scan_count = 0;
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08005732 wpas_p2p_join_scan_req(wpa_s, op_freq, ssid, ssid_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005733 return 0;
5734}
5735
5736
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08005737static int wpas_p2p_join_start(struct wpa_supplicant *wpa_s, int freq,
5738 const u8 *ssid, size_t ssid_len)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005739{
5740 struct wpa_supplicant *group;
5741 struct p2p_go_neg_results res;
Dmitry Shmidt04949592012-07-19 12:16:46 -07005742 struct wpa_bss *bss;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005743
5744 group = wpas_p2p_get_group_iface(wpa_s, 0, 0);
5745 if (group == NULL)
5746 return -1;
5747 if (group != wpa_s) {
5748 os_memcpy(group->p2p_pin, wpa_s->p2p_pin,
5749 sizeof(group->p2p_pin));
5750 group->p2p_wps_method = wpa_s->p2p_wps_method;
5751 }
5752
Hai Shalom74f70d42019-02-11 14:42:39 -08005753 /*
5754 * Need to mark the current interface for p2p_group_formation
5755 * when a separate group interface is not used. This is needed
5756 * to allow p2p_cancel stop a pending p2p_connect-join.
5757 * wpas_p2p_init_group_interface() addresses this for the case
5758 * where a separate group interface is used.
5759 */
5760 if (group == wpa_s->parent)
5761 wpa_s->global->p2p_group_formation = group;
5762
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005763 group->p2p_in_provisioning = 1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07005764 group->p2p_fallback_to_go_neg = wpa_s->p2p_fallback_to_go_neg;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005765
5766 os_memset(&res, 0, sizeof(res));
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08005767 os_memcpy(res.peer_device_addr, wpa_s->pending_join_dev_addr, ETH_ALEN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005768 os_memcpy(res.peer_interface_addr, wpa_s->pending_join_iface_addr,
5769 ETH_ALEN);
5770 res.wps_method = wpa_s->pending_join_wps_method;
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08005771 if (freq && ssid && ssid_len) {
5772 res.freq = freq;
5773 res.ssid_len = ssid_len;
5774 os_memcpy(res.ssid, ssid, ssid_len);
5775 } else {
Dmitry Shmidtde47be72016-01-07 12:52:55 -08005776 if (ssid && ssid_len) {
5777 bss = wpa_bss_get(wpa_s, wpa_s->pending_join_iface_addr,
5778 ssid, ssid_len);
5779 } else {
5780 bss = wpa_bss_get_bssid_latest(
5781 wpa_s, wpa_s->pending_join_iface_addr);
5782 }
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08005783 if (bss) {
5784 res.freq = bss->freq;
5785 res.ssid_len = bss->ssid_len;
5786 os_memcpy(res.ssid, bss->ssid, bss->ssid_len);
5787 wpa_printf(MSG_DEBUG, "P2P: Join target GO operating frequency from BSS table: %d MHz (SSID %s)",
5788 bss->freq,
5789 wpa_ssid_txt(bss->ssid, bss->ssid_len));
Dmitry Shmidtde47be72016-01-07 12:52:55 -08005790 } else if (ssid && ssid_len) {
5791 res.ssid_len = ssid_len;
5792 os_memcpy(res.ssid, ssid, ssid_len);
5793 wpa_printf(MSG_DEBUG, "P2P: Join target GO (SSID %s)",
5794 wpa_ssid_txt(ssid, ssid_len));
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08005795 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07005796 }
5797
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08005798 if (wpa_s->off_channel_freq || wpa_s->roc_waiting_drv_freq) {
5799 wpa_printf(MSG_DEBUG, "P2P: Cancel remain-on-channel prior to "
5800 "starting client");
5801 wpa_drv_cancel_remain_on_channel(wpa_s);
5802 wpa_s->off_channel_freq = 0;
5803 wpa_s->roc_waiting_drv_freq = 0;
5804 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005805 wpas_start_wps_enrollee(group, &res);
5806
5807 /*
5808 * Allow a longer timeout for join-a-running-group than normal 15
5809 * second group formation timeout since the GO may not have authorized
5810 * our connection yet.
5811 */
5812 eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s, NULL);
5813 eloop_register_timeout(60, 0, wpas_p2p_group_formation_timeout,
5814 wpa_s, NULL);
5815
5816 return 0;
5817}
5818
5819
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08005820static int wpas_p2p_setup_freqs(struct wpa_supplicant *wpa_s, int freq,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005821 int *force_freq, int *pref_freq, int go,
Sunil8cd6f4d2022-06-28 18:40:46 +00005822 struct weighted_pcl *pref_freq_list,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005823 unsigned int *num_pref_freq)
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08005824{
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07005825 struct wpa_used_freq_data *freqs;
5826 int res, best_freq, num_unused;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005827 unsigned int freq_in_use = 0, num, i, max_pref_freq;
Vinayak Yadawad8db34572021-08-30 21:28:05 +05305828 int p2p_pref_freq;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005829
5830 max_pref_freq = *num_pref_freq;
5831 *num_pref_freq = 0;
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07005832
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07005833 freqs = os_calloc(wpa_s->num_multichan_concurrent,
5834 sizeof(struct wpa_used_freq_data));
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07005835 if (!freqs)
5836 return -1;
5837
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07005838 num = wpas_p2p_valid_oper_freqs(wpa_s, freqs,
5839 wpa_s->num_multichan_concurrent);
5840
5841 /*
5842 * It is possible that the total number of used frequencies is bigger
5843 * than the number of frequencies used for P2P, so get the system wide
5844 * number of unused frequencies.
5845 */
5846 num_unused = wpas_p2p_num_unused_channels(wpa_s);
5847
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07005848 wpa_printf(MSG_DEBUG,
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07005849 "P2P: Setup freqs: freq=%d num_MCC=%d shared_freqs=%u num_unused=%d",
5850 freq, wpa_s->num_multichan_concurrent, num, num_unused);
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07005851
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08005852 if (freq > 0) {
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07005853 int ret;
5854 if (go)
5855 ret = p2p_supported_freq(wpa_s->global->p2p, freq);
5856 else
5857 ret = p2p_supported_freq_cli(wpa_s->global->p2p, freq);
5858 if (!ret) {
Dmitry Shmidt203eadb2015-03-05 14:16:04 -08005859 if ((wpa_s->drv_flags & WPA_DRIVER_FLAGS_DFS_OFFLOAD) &&
Roshan Pius3a1667e2018-07-03 15:17:14 -07005860 ieee80211_is_dfs(freq, wpa_s->hw.modes,
5861 wpa_s->hw.num_modes)) {
Dmitry Shmidt203eadb2015-03-05 14:16:04 -08005862 /*
5863 * If freq is a DFS channel and DFS is offloaded
5864 * to the driver, allow P2P GO to use it.
5865 */
5866 wpa_printf(MSG_DEBUG,
5867 "P2P: The forced channel for GO (%u MHz) is DFS, and DFS is offloaded to the driver",
5868 freq);
5869 } else {
5870 wpa_printf(MSG_DEBUG,
5871 "P2P: The forced channel (%u MHz) is not supported for P2P uses",
5872 freq);
5873 res = -3;
5874 goto exit_free;
5875 }
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08005876 }
5877
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07005878 for (i = 0; i < num; i++) {
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07005879 if (freqs[i].freq == freq)
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07005880 freq_in_use = 1;
5881 }
5882
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07005883 if (num_unused <= 0 && !freq_in_use) {
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07005884 wpa_printf(MSG_DEBUG, "P2P: Cannot start P2P group on %u MHz as there are no available channels",
5885 freq);
5886 res = -2;
5887 goto exit_free;
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08005888 }
5889 wpa_printf(MSG_DEBUG, "P2P: Trying to force us to use the "
5890 "requested channel (%u MHz)", freq);
5891 *force_freq = freq;
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07005892 goto exit_ok;
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08005893 }
5894
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07005895 best_freq = wpas_p2p_pick_best_used_freq(wpa_s, freqs, num);
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07005896
Vinayak Yadawad8db34572021-08-30 21:28:05 +05305897 if (*pref_freq == 0) {
5898 if (wpa_s->conf->num_p2p_pref_chan && IS_2GHZ(best_freq)) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005899 i = 0;
Vinayak Yadawad8db34572021-08-30 21:28:05 +05305900 while (i < wpa_s->conf->num_p2p_pref_chan) {
5901 p2p_pref_freq = ieee80211_chan_to_freq(NULL,
5902 wpa_s->conf->p2p_pref_chan[i].op_class,
5903 wpa_s->conf->p2p_pref_chan[i].chan);
5904
5905 if (p2p_supported_freq(wpa_s->global->p2p, p2p_pref_freq) &&
5906 !wpas_p2p_disallowed_freq(wpa_s->global, p2p_pref_freq)) {
5907 best_freq = p2p_pref_freq;
5908 wpa_printf(MSG_DEBUG, "P2P: Using frequency (%u MHz) "
5909 "from P2P preferred channel list", best_freq);
5910 break;
5911 } else {
5912 wpa_printf(MSG_MSGDUMP, "P2P: Skipping preferred "
5913 "frequency (%u MHz) ", p2p_pref_freq);
5914 }
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005915 i++;
5916 }
Sreeramya Soratkalf928e8f2022-03-22 17:33:31 +05305917 } else if (!wpa_s->conf->num_p2p_pref_chan) {
Vinayak Yadawad8db34572021-08-30 21:28:05 +05305918 wpa_printf(MSG_DEBUG, "P2P: best_freq=%d, go=%d",
Sunil Ravi036cec52023-03-29 11:35:17 -07005919 best_freq, go);
Vinayak Yadawad8db34572021-08-30 21:28:05 +05305920
Sunil Ravi036cec52023-03-29 11:35:17 -07005921 *num_pref_freq = max_pref_freq;
5922 res = wpas_p2p_pick_best_pref_freq(wpa_s, go, pref_freq_list,
5923 num_pref_freq);
5924 if (res > 0)
5925 best_freq = res;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005926 }
5927 }
5928
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07005929 /* We have a candidate frequency to use */
5930 if (best_freq > 0) {
5931 if (*pref_freq == 0 && num_unused > 0) {
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08005932 wpa_printf(MSG_DEBUG, "P2P: Try to prefer a frequency (%u MHz) we are already using",
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07005933 best_freq);
5934 *pref_freq = best_freq;
Dmitry Shmidt51a47d52013-09-10 10:52:57 -07005935 } else {
5936 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 -07005937 best_freq);
5938 *force_freq = best_freq;
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07005939 }
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07005940 } else if (num_unused > 0) {
5941 wpa_printf(MSG_DEBUG,
5942 "P2P: Current operating channels are not available for P2P. Try to use another channel");
5943 *force_freq = 0;
5944 } else {
5945 wpa_printf(MSG_DEBUG,
5946 "P2P: All channels are in use and none of them are P2P enabled. Cannot start P2P group");
5947 res = -2;
5948 goto exit_free;
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07005949 }
5950
5951exit_ok:
5952 res = 0;
5953exit_free:
5954 os_free(freqs);
5955 return res;
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08005956}
5957
5958
Hai Shalomc1a21442022-02-04 13:43:00 -08005959static bool is_p2p_6ghz_supported(struct wpa_supplicant *wpa_s,
5960 const u8 *peer_addr)
5961{
5962 if (wpa_s->conf->p2p_6ghz_disable ||
5963 !get_mode(wpa_s->hw.modes, wpa_s->hw.num_modes,
5964 HOSTAPD_MODE_IEEE80211A, true))
5965 return false;
5966
5967 if (!p2p_wfd_enabled(wpa_s->global->p2p))
5968 return false;
5969 if (peer_addr && !p2p_peer_wfd_enabled(wpa_s->global->p2p, peer_addr))
5970 return false;
5971
5972 return true;
5973}
5974
5975
5976static int wpas_p2p_check_6ghz(struct wpa_supplicant *wpa_s,
5977 const u8 *peer_addr, bool allow_6ghz, int freq)
5978{
5979 if (allow_6ghz && is_p2p_6ghz_supported(wpa_s, peer_addr)) {
5980 wpa_printf(MSG_DEBUG,
5981 "P2P: Allow connection on 6 GHz channels");
5982 p2p_set_6ghz_dev_capab(wpa_s->global->p2p, true);
5983 } else {
5984 if (is_6ghz_freq(freq))
5985 return -2;
5986 p2p_set_6ghz_dev_capab(wpa_s->global->p2p, false);
5987 }
5988
5989 return 0;
5990}
5991
5992
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005993/**
5994 * wpas_p2p_connect - Request P2P Group Formation to be started
5995 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
5996 * @peer_addr: Address of the peer P2P Device
5997 * @pin: PIN to use during provisioning or %NULL to indicate PBC mode
5998 * @persistent_group: Whether to create a persistent group
Dmitry Shmidt04949592012-07-19 12:16:46 -07005999 * @auto_join: Whether to select join vs. GO Negotiation automatically
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006000 * @join: Whether to join an existing group (as a client) instead of starting
6001 * Group Owner negotiation; @peer_addr is BSSID in that case
6002 * @auth: Whether to only authorize the connection instead of doing that and
6003 * initiating Group Owner negotiation
6004 * @go_intent: GO Intent or -1 to use default
6005 * @freq: Frequency for the group or 0 for auto-selection
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08006006 * @freq2: Center frequency of segment 1 for the GO operating in VHT 80P80 mode
Dmitry Shmidt04949592012-07-19 12:16:46 -07006007 * @persistent_id: Persistent group credentials to use for forcing GO
6008 * parameters or -1 to generate new values (SSID/passphrase)
6009 * @pd: Whether to send Provision Discovery prior to GO Negotiation as an
6010 * interoperability workaround when initiating group formation
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07006011 * @ht40: Start GO with 40 MHz channel width
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07006012 * @vht: Start GO with VHT support
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08006013 * @vht_chwidth: Channel width supported by GO operating with VHT support
Hai Shalom81f62d82019-07-22 12:10:00 -07006014 * (CHANWIDTH_*).
Dmitry Shmidtde47be72016-01-07 12:52:55 -08006015 * @group_ssid: Specific Group SSID for join or %NULL if not set
6016 * @group_ssid_len: Length of @group_ssid in octets
Hai Shalomc1a21442022-02-04 13:43:00 -08006017 * @allow_6ghz: Allow P2P connection on 6 GHz channels
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006018 * Returns: 0 or new PIN (if pin was %NULL) on success, -1 on unspecified
6019 * failure, -2 on failure due to channel not currently available,
6020 * -3 if forced channel is not supported
6021 */
6022int wpas_p2p_connect(struct wpa_supplicant *wpa_s, const u8 *peer_addr,
6023 const char *pin, enum p2p_wps_method wps_method,
Dmitry Shmidt04949592012-07-19 12:16:46 -07006024 int persistent_group, int auto_join, int join, int auth,
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08006025 int go_intent, int freq, unsigned int vht_center_freq2,
6026 int persistent_id, int pd, int ht40, int vht,
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08006027 unsigned int vht_chwidth, int he, int edmg,
Hai Shalomc1a21442022-02-04 13:43:00 -08006028 const u8 *group_ssid, size_t group_ssid_len,
6029 bool allow_6ghz)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006030{
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07006031 int force_freq = 0, pref_freq = 0;
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08006032 int ret = 0, res;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006033 enum wpa_driver_if_type iftype;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006034 const u8 *if_addr;
Dmitry Shmidt04949592012-07-19 12:16:46 -07006035 struct wpa_ssid *ssid = NULL;
Sunil8cd6f4d2022-06-28 18:40:46 +00006036 struct weighted_pcl pref_freq_list[P2P_MAX_PREF_CHANNELS];
6037 unsigned int size;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006038
6039 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
6040 return -1;
6041
Dmitry Shmidt04949592012-07-19 12:16:46 -07006042 if (persistent_id >= 0) {
6043 ssid = wpa_config_get_network(wpa_s->conf, persistent_id);
6044 if (ssid == NULL || ssid->disabled != 2 ||
6045 ssid->mode != WPAS_MODE_P2P_GO)
6046 return -1;
6047 }
6048
Hai Shalomc1a21442022-02-04 13:43:00 -08006049 if (wpas_p2p_check_6ghz(wpa_s, peer_addr, allow_6ghz, freq))
Hai Shalom899fcc72020-10-19 14:38:18 -07006050 return -2;
6051
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07006052 os_free(wpa_s->global->add_psk);
6053 wpa_s->global->add_psk = NULL;
6054
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08006055 wpa_s->global->p2p_fail_on_wps_complete = 0;
Dmitry Shmidt216983b2015-02-06 10:50:36 -08006056 wpa_s->global->pending_p2ps_group = 0;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006057 wpa_s->global->pending_p2ps_group_freq = 0;
6058 wpa_s->p2ps_method_config_any = 0;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08006059
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006060 if (go_intent < 0)
6061 go_intent = wpa_s->conf->p2p_go_intent;
6062
6063 if (!auth)
Hai Shalomfdcde762020-04-02 11:19:20 -07006064 wpa_s->global->p2p_long_listen = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006065
6066 wpa_s->p2p_wps_method = wps_method;
Dmitry Shmidt04949592012-07-19 12:16:46 -07006067 wpa_s->p2p_persistent_group = !!persistent_group;
6068 wpa_s->p2p_persistent_id = persistent_id;
6069 wpa_s->p2p_go_intent = go_intent;
6070 wpa_s->p2p_connect_freq = freq;
6071 wpa_s->p2p_fallback_to_go_neg = 0;
6072 wpa_s->p2p_pd_before_go_neg = !!pd;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07006073 wpa_s->p2p_go_ht40 = !!ht40;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07006074 wpa_s->p2p_go_vht = !!vht;
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08006075 wpa_s->p2p_go_vht_center_freq2 = vht_center_freq2;
6076 wpa_s->p2p_go_max_oper_chwidth = vht_chwidth;
Hai Shalom74f70d42019-02-11 14:42:39 -08006077 wpa_s->p2p_go_he = !!he;
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08006078 wpa_s->p2p_go_edmg = !!edmg;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006079
6080 if (pin)
6081 os_strlcpy(wpa_s->p2p_pin, pin, sizeof(wpa_s->p2p_pin));
6082 else if (wps_method == WPS_PIN_DISPLAY) {
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08006083 if (wps_generate_pin((unsigned int *) &ret) < 0)
6084 return -1;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006085 res = os_snprintf(wpa_s->p2p_pin, sizeof(wpa_s->p2p_pin),
6086 "%08d", ret);
6087 if (os_snprintf_error(sizeof(wpa_s->p2p_pin), res))
6088 wpa_s->p2p_pin[sizeof(wpa_s->p2p_pin) - 1] = '\0';
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006089 wpa_printf(MSG_DEBUG, "P2P: Randomly generated PIN: %s",
6090 wpa_s->p2p_pin);
Dmitry Shmidt849734c2016-05-27 09:59:01 -07006091 } else if (wps_method == WPS_P2PS) {
6092 /* Force the P2Ps default PIN to be used */
6093 os_strlcpy(wpa_s->p2p_pin, "12345670", sizeof(wpa_s->p2p_pin));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006094 } else
6095 wpa_s->p2p_pin[0] = '\0';
6096
Dmitry Shmidt04949592012-07-19 12:16:46 -07006097 if (join || auto_join) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006098 u8 iface_addr[ETH_ALEN], dev_addr[ETH_ALEN];
6099 if (auth) {
6100 wpa_printf(MSG_DEBUG, "P2P: Authorize invitation to "
6101 "connect a running group from " MACSTR,
6102 MAC2STR(peer_addr));
6103 os_memcpy(wpa_s->p2p_auth_invite, peer_addr, ETH_ALEN);
6104 return ret;
6105 }
6106 os_memcpy(dev_addr, peer_addr, ETH_ALEN);
6107 if (p2p_get_interface_addr(wpa_s->global->p2p, peer_addr,
6108 iface_addr) < 0) {
6109 os_memcpy(iface_addr, peer_addr, ETH_ALEN);
6110 p2p_get_dev_addr(wpa_s->global->p2p, peer_addr,
6111 dev_addr);
6112 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07006113 if (auto_join) {
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08006114 os_get_reltime(&wpa_s->p2p_auto_started);
Dmitry Shmidt04949592012-07-19 12:16:46 -07006115 wpa_printf(MSG_DEBUG, "P2P: Auto join started at "
6116 "%ld.%06ld",
6117 wpa_s->p2p_auto_started.sec,
6118 wpa_s->p2p_auto_started.usec);
6119 }
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08006120 wpa_s->user_initiated_pd = 1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07006121 if (wpas_p2p_join(wpa_s, iface_addr, dev_addr, wps_method,
Dmitry Shmidtde47be72016-01-07 12:52:55 -08006122 auto_join, freq,
6123 group_ssid, group_ssid_len) < 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006124 return -1;
6125 return ret;
6126 }
6127
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006128 size = P2P_MAX_PREF_CHANNELS;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07006129 res = wpas_p2p_setup_freqs(wpa_s, freq, &force_freq, &pref_freq,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006130 go_intent == 15, pref_freq_list, &size);
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08006131 if (res)
6132 return res;
Dmitry Shmidta0d265f2013-11-19 13:13:41 -08006133 wpas_p2p_set_own_freq_preference(wpa_s,
6134 force_freq ? force_freq : pref_freq);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006135
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006136 p2p_set_own_pref_freq_list(wpa_s->global->p2p, pref_freq_list, size);
6137
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006138 wpa_s->create_p2p_iface = wpas_p2p_create_iface(wpa_s);
6139
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006140 if (wpa_s->create_p2p_iface) {
6141 /* Prepare to add a new interface for the group */
6142 iftype = WPA_IF_P2P_GROUP;
6143 if (go_intent == 15)
6144 iftype = WPA_IF_P2P_GO;
6145 if (wpas_p2p_add_group_interface(wpa_s, iftype) < 0) {
6146 wpa_printf(MSG_ERROR, "P2P: Failed to allocate a new "
6147 "interface for the group");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006148 return -1;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006149 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006150
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006151 if_addr = wpa_s->pending_interface_addr;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006152 } else {
Dmitry Shmidt9c175262016-03-03 10:20:07 -08006153 if (wpa_s->p2p_mgmt)
6154 if_addr = wpa_s->parent->own_addr;
6155 else
6156 if_addr = wpa_s->own_addr;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006157 os_memset(wpa_s->go_dev_addr, 0, ETH_ALEN);
6158 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006159
6160 if (auth) {
6161 if (wpas_p2p_auth_go_neg(wpa_s, peer_addr, wps_method,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006162 go_intent, if_addr,
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08006163 force_freq, persistent_group, ssid,
6164 pref_freq) < 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006165 return -1;
6166 return ret;
6167 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006168
6169 if (wpas_p2p_start_go_neg(wpa_s, peer_addr, wps_method,
6170 go_intent, if_addr, force_freq,
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08006171 persistent_group, ssid, pref_freq) < 0) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006172 if (wpa_s->create_p2p_iface)
6173 wpas_p2p_remove_pending_group_interface(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006174 return -1;
6175 }
6176 return ret;
6177}
6178
6179
6180/**
6181 * wpas_p2p_remain_on_channel_cb - Indication of remain-on-channel start
6182 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
6183 * @freq: Frequency of the channel in MHz
6184 * @duration: Duration of the stay on the channel in milliseconds
6185 *
6186 * This callback is called when the driver indicates that it has started the
6187 * requested remain-on-channel duration.
6188 */
6189void wpas_p2p_remain_on_channel_cb(struct wpa_supplicant *wpa_s,
6190 unsigned int freq, unsigned int duration)
6191{
6192 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
6193 return;
Dmitry Shmidt216983b2015-02-06 10:50:36 -08006194 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)",
6195 wpa_s->off_channel_freq, wpa_s->pending_listen_freq,
6196 wpa_s->roc_waiting_drv_freq, freq, duration);
6197 if (wpa_s->off_channel_freq &&
6198 wpa_s->off_channel_freq == wpa_s->pending_listen_freq) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006199 p2p_listen_cb(wpa_s->global->p2p, wpa_s->pending_listen_freq,
6200 wpa_s->pending_listen_duration);
6201 wpa_s->pending_listen_freq = 0;
Dmitry Shmidt7f93d6f2014-02-21 11:22:49 -08006202 } else {
6203 wpa_printf(MSG_DEBUG, "P2P: Ignore remain-on-channel callback (off_channel_freq=%u pending_listen_freq=%d freq=%u duration=%u)",
6204 wpa_s->off_channel_freq, wpa_s->pending_listen_freq,
6205 freq, duration);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006206 }
6207}
6208
6209
Jithu Jance57cea1a2014-08-20 10:22:04 -07006210int wpas_p2p_listen_start(struct wpa_supplicant *wpa_s, unsigned int timeout)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006211{
6212 /* Limit maximum Listen state time based on driver limitation. */
6213 if (timeout > wpa_s->max_remain_on_chan)
6214 timeout = wpa_s->max_remain_on_chan;
6215
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006216 return p2p_listen(wpa_s->global->p2p, timeout);
6217}
6218
6219
6220/**
6221 * wpas_p2p_cancel_remain_on_channel_cb - Remain-on-channel timeout
6222 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
6223 * @freq: Frequency of the channel in MHz
6224 *
6225 * This callback is called when the driver indicates that a remain-on-channel
6226 * operation has been completed, i.e., the duration on the requested channel
6227 * has timed out.
6228 */
6229void wpas_p2p_cancel_remain_on_channel_cb(struct wpa_supplicant *wpa_s,
6230 unsigned int freq)
6231{
6232 wpa_printf(MSG_DEBUG, "P2P: Cancel remain-on-channel callback "
6233 "(p2p_long_listen=%d ms pending_action_tx=%p)",
Hai Shalomfdcde762020-04-02 11:19:20 -07006234 wpa_s->global->p2p_long_listen,
6235 offchannel_pending_action_tx(wpa_s));
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08006236 wpas_p2p_listen_work_done(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006237 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
6238 return;
Hai Shalomfdcde762020-04-02 11:19:20 -07006239 if (wpa_s->global->p2p_long_listen > 0)
6240 wpa_s->global->p2p_long_listen -= wpa_s->max_remain_on_chan;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006241 if (p2p_listen_end(wpa_s->global->p2p, freq) > 0)
6242 return; /* P2P module started a new operation */
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08006243 if (offchannel_pending_action_tx(wpa_s))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006244 return;
Hai Shalomfdcde762020-04-02 11:19:20 -07006245 if (wpa_s->global->p2p_long_listen > 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006246 wpa_printf(MSG_DEBUG, "P2P: Continuing long Listen state");
Hai Shalomfdcde762020-04-02 11:19:20 -07006247 wpas_p2p_listen_start(wpa_s, wpa_s->global->p2p_long_listen);
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07006248 } else {
6249 /*
6250 * When listen duration is over, stop listen & update p2p_state
6251 * to IDLE.
6252 */
6253 p2p_stop_listen(wpa_s->global->p2p);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006254 }
6255}
6256
6257
6258/**
6259 * wpas_p2p_group_remove - Remove a P2P group
6260 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
6261 * @ifname: Network interface name of the group interface or "*" to remove all
6262 * groups
6263 * Returns: 0 on success, -1 on failure
6264 *
6265 * This function is used to remove a P2P group. This can be used to disconnect
6266 * from a group in which the local end is a P2P Client or to end a P2P Group in
6267 * case the local end is the Group Owner. If a virtual network interface was
6268 * created for this group, that interface will be removed. Otherwise, only the
6269 * configured P2P group network will be removed from the interface.
6270 */
6271int wpas_p2p_group_remove(struct wpa_supplicant *wpa_s, const char *ifname)
6272{
6273 struct wpa_global *global = wpa_s->global;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006274 struct wpa_supplicant *calling_wpa_s = wpa_s;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006275
6276 if (os_strcmp(ifname, "*") == 0) {
6277 struct wpa_supplicant *prev;
Hai Shalom899fcc72020-10-19 14:38:18 -07006278 bool calling_wpa_s_group_removed = false;
6279
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006280 wpa_s = global->ifaces;
6281 while (wpa_s) {
6282 prev = wpa_s;
6283 wpa_s = wpa_s->next;
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07006284 if (prev->p2p_group_interface !=
6285 NOT_P2P_GROUP_INTERFACE ||
6286 (prev->current_ssid &&
Hai Shalom899fcc72020-10-19 14:38:18 -07006287 prev->current_ssid->p2p_group)) {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006288 wpas_p2p_disconnect_safely(prev, calling_wpa_s);
Hai Shalom899fcc72020-10-19 14:38:18 -07006289 if (prev == calling_wpa_s)
6290 calling_wpa_s_group_removed = true;
6291 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006292 }
Hai Shalom899fcc72020-10-19 14:38:18 -07006293
6294 if (!calling_wpa_s_group_removed &&
6295 (calling_wpa_s->p2p_group_interface !=
6296 NOT_P2P_GROUP_INTERFACE ||
6297 (calling_wpa_s->current_ssid &&
6298 calling_wpa_s->current_ssid->p2p_group))) {
6299 wpa_printf(MSG_DEBUG, "Remove calling_wpa_s P2P group");
6300 wpas_p2p_disconnect_safely(calling_wpa_s,
6301 calling_wpa_s);
6302 }
6303
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006304 return 0;
6305 }
6306
6307 for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
6308 if (os_strcmp(wpa_s->ifname, ifname) == 0)
6309 break;
6310 }
6311
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006312 return wpas_p2p_disconnect_safely(wpa_s, calling_wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006313}
6314
6315
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07006316static int wpas_p2p_select_go_freq(struct wpa_supplicant *wpa_s, int freq)
6317{
6318 unsigned int r;
6319
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006320 if (!wpa_s->conf->num_p2p_pref_chan && !freq) {
6321 unsigned int i, size = P2P_MAX_PREF_CHANNELS;
Sunil8cd6f4d2022-06-28 18:40:46 +00006322 struct weighted_pcl pref_freq_list[P2P_MAX_PREF_CHANNELS];
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006323 int res;
6324
6325 res = wpa_drv_get_pref_freq_list(wpa_s, WPA_IF_P2P_GO,
6326 &size, pref_freq_list);
Hai Shalomc1a21442022-02-04 13:43:00 -08006327 if (!is_p2p_allow_6ghz(wpa_s->global->p2p))
6328 size = p2p_remove_6ghz_channels(pref_freq_list, size);
6329
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006330 if (!res && size > 0) {
6331 i = 0;
Dmitry Shmidt29333592017-01-09 12:27:11 -08006332 while (i < size &&
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07006333 (!p2p_supported_freq(wpa_s->global->p2p,
Sunil8cd6f4d2022-06-28 18:40:46 +00006334 pref_freq_list[i].freq) ||
6335 wpas_p2p_disallowed_freq(
6336 wpa_s->global,
6337 pref_freq_list[i].freq) ||
6338 !p2p_pref_freq_allowed(&pref_freq_list[i],
6339 true))) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006340 wpa_printf(MSG_DEBUG,
6341 "P2P: preferred_freq_list[%d]=%d is disallowed",
Sunil8cd6f4d2022-06-28 18:40:46 +00006342 i, pref_freq_list[i].freq);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006343 i++;
6344 }
6345 if (i != size) {
Sunil8cd6f4d2022-06-28 18:40:46 +00006346 freq = pref_freq_list[i].freq;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006347 wpa_printf(MSG_DEBUG,
6348 "P2P: Using preferred_freq_list[%d]=%d",
6349 i, freq);
6350 } else {
6351 wpa_printf(MSG_DEBUG,
6352 "P2P: All driver preferred frequencies are disallowed for P2P use");
6353 }
6354 } else {
6355 wpa_printf(MSG_DEBUG,
6356 "P2P: No preferred frequency list available");
6357 }
6358 }
6359
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07006360 if (freq == 2) {
6361 wpa_printf(MSG_DEBUG, "P2P: Request to start GO on 2.4 GHz "
6362 "band");
6363 if (wpa_s->best_24_freq > 0 &&
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07006364 p2p_supported_freq_go(wpa_s->global->p2p,
6365 wpa_s->best_24_freq)) {
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07006366 freq = wpa_s->best_24_freq;
6367 wpa_printf(MSG_DEBUG, "P2P: Use best 2.4 GHz band "
6368 "channel: %d MHz", freq);
6369 } else {
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07006370 if (os_get_random((u8 *) &r, sizeof(r)) < 0)
6371 return -1;
Jimmy Chen801bf462021-11-09 23:08:48 +08006372 int possible_2g_freqs[] = {
6373 /* operating class 81 */
6374 2412, 2437, 2462,
6375 };
6376 int possible_2g_freqs_num =
6377 sizeof(possible_2g_freqs)/sizeof(possible_2g_freqs[0]);
6378 int i;
6379 for (i = 0; i < possible_2g_freqs_num; i++, r++) {
6380 freq = possible_2g_freqs[r % possible_2g_freqs_num];
6381 if (p2p_supported_freq_go(wpa_s->global->p2p, freq)) {
6382 break;
6383 }
6384 }
6385
6386 if (i >= possible_2g_freqs_num) {
6387 wpa_printf(MSG_DEBUG, "P2P: Could not select "
6388 "2.4 GHz channel for P2P group");
6389 return -1;
6390 }
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07006391 wpa_printf(MSG_DEBUG, "P2P: Use random 2.4 GHz band "
6392 "channel: %d MHz", freq);
6393 }
6394 }
6395
6396 if (freq == 5) {
6397 wpa_printf(MSG_DEBUG, "P2P: Request to start GO on 5 GHz "
6398 "band");
6399 if (wpa_s->best_5_freq > 0 &&
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07006400 p2p_supported_freq_go(wpa_s->global->p2p,
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07006401 wpa_s->best_5_freq)) {
6402 freq = wpa_s->best_5_freq;
6403 wpa_printf(MSG_DEBUG, "P2P: Use best 5 GHz band "
6404 "channel: %d MHz", freq);
6405 } else {
Hai Shalomc1a21442022-02-04 13:43:00 -08006406 const int freqs[] = {
6407 /* operating class 115 */
6408 5180, 5200, 5220, 5240,
6409 /* operating class 124 */
6410 5745, 5765, 5785, 5805,
6411 };
6412 unsigned int i, num_freqs = ARRAY_SIZE(freqs);
6413
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07006414 if (os_get_random((u8 *) &r, sizeof(r)) < 0)
6415 return -1;
Jimmy Chen64b90632020-09-29 17:27:34 +08006416
6417 /*
6418 * most of 5G channels are DFS, only operating class 115 and 124
6419 * are available possibly, randomly pick a start to check them.
6420 */
6421 int possible_5g_freqs[] = {
6422 /* operating class 115 */
6423 5180, 5200, 5220, 5240,
6424 /* operating class 124 */
6425 5745, 5765, 5785, 5805,
6426 };
6427 int possible_5g_freqs_num =
6428 sizeof(possible_5g_freqs)/sizeof(possible_5g_freqs[0]);
6429
Jimmy Chen64b90632020-09-29 17:27:34 +08006430 for (i = 0; i < possible_5g_freqs_num; i++, r++) {
6431 if (p2p_supported_freq_go(
6432 wpa_s->global->p2p,
6433 possible_5g_freqs[r % possible_5g_freqs_num])) {
6434 freq = possible_5g_freqs[r % possible_5g_freqs_num];
6435 break;
6436 }
6437 }
6438
6439 if (i >= possible_5g_freqs_num) {
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07006440 wpa_printf(MSG_DEBUG, "P2P: Could not select "
6441 "5 GHz channel for P2P group");
6442 return -1;
6443 }
6444 wpa_printf(MSG_DEBUG, "P2P: Use random 5 GHz band "
6445 "channel: %d MHz", freq);
6446 }
6447 }
6448
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07006449 if (freq > 0 && !p2p_supported_freq_go(wpa_s->global->p2p, freq)) {
Dmitry Shmidt203eadb2015-03-05 14:16:04 -08006450 if ((wpa_s->drv_flags & WPA_DRIVER_FLAGS_DFS_OFFLOAD) &&
Roshan Pius3a1667e2018-07-03 15:17:14 -07006451 ieee80211_is_dfs(freq, wpa_s->hw.modes,
6452 wpa_s->hw.num_modes)) {
Dmitry Shmidt203eadb2015-03-05 14:16:04 -08006453 /*
6454 * If freq is a DFS channel and DFS is offloaded to the
6455 * driver, allow P2P GO to use it.
6456 */
6457 wpa_printf(MSG_DEBUG, "P2P: "
6458 "%s: The forced channel for GO (%u MHz) is DFS, and DFS is offloaded",
6459 __func__, freq);
6460 return freq;
6461 }
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07006462 wpa_printf(MSG_DEBUG, "P2P: The forced channel for GO "
6463 "(%u MHz) is not supported for P2P uses",
6464 freq);
6465 return -1;
6466 }
6467
6468 return freq;
6469}
6470
6471
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006472static int wpas_p2p_supported_freq_go(struct wpa_supplicant *wpa_s,
6473 const struct p2p_channels *channels,
6474 int freq)
6475{
6476 if (!wpas_p2p_disallowed_freq(wpa_s->global, freq) &&
6477 p2p_supported_freq_go(wpa_s->global->p2p, freq) &&
6478 freq_included(wpa_s, channels, freq))
6479 return 1;
6480 return 0;
6481}
6482
6483
6484static void wpas_p2p_select_go_freq_no_pref(struct wpa_supplicant *wpa_s,
6485 struct p2p_go_neg_results *params,
6486 const struct p2p_channels *channels)
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07006487{
6488 unsigned int i, r;
6489
Dmitry Shmidt203eadb2015-03-05 14:16:04 -08006490 /* try all channels in operating class 115 */
6491 for (i = 0; i < 4; i++) {
6492 params->freq = 5180 + i * 20;
Hai Shalomc1a21442022-02-04 13:43:00 -08006493 if (wpas_p2p_supported_freq_go(wpa_s, channels, params->freq))
Dmitry Shmidt203eadb2015-03-05 14:16:04 -08006494 goto out;
6495 }
6496
6497 /* try all channels in operating class 124 */
6498 for (i = 0; i < 4; i++) {
6499 params->freq = 5745 + i * 20;
Hai Shalomc1a21442022-02-04 13:43:00 -08006500 if (wpas_p2p_supported_freq_go(wpa_s, channels, params->freq))
Dmitry Shmidt203eadb2015-03-05 14:16:04 -08006501 goto out;
6502 }
6503
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07006504 /* try social channel class 180 channel 2 */
6505 params->freq = 58320 + 1 * 2160;
Hai Shalomc1a21442022-02-04 13:43:00 -08006506 if (wpas_p2p_supported_freq_go(wpa_s, channels, params->freq))
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07006507 goto out;
6508
6509 /* try all channels in reg. class 180 */
6510 for (i = 0; i < 4; i++) {
6511 params->freq = 58320 + i * 2160;
Hai Shalomc1a21442022-02-04 13:43:00 -08006512 if (wpas_p2p_supported_freq_go(wpa_s, channels, params->freq))
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07006513 goto out;
6514 }
6515
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07006516 /* try some random selection of the social channels */
6517 if (os_get_random((u8 *) &r, sizeof(r)) < 0)
6518 return;
6519
6520 for (i = 0; i < 3; i++) {
6521 params->freq = 2412 + ((r + i) % 3) * 25;
6522 if (wpas_p2p_supported_freq_go(wpa_s, channels, params->freq))
6523 goto out;
6524 }
6525
6526 /* try all other channels in operating class 81 */
6527 for (i = 0; i < 11; i++) {
6528 params->freq = 2412 + i * 5;
6529
6530 /* skip social channels; covered in the previous loop */
6531 if (params->freq == 2412 ||
6532 params->freq == 2437 ||
6533 params->freq == 2462)
6534 continue;
6535
6536 if (wpas_p2p_supported_freq_go(wpa_s, channels, params->freq))
6537 goto out;
6538 }
6539
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006540 params->freq = 0;
Dmitry Shmidt203eadb2015-03-05 14:16:04 -08006541 wpa_printf(MSG_DEBUG, "P2P: No 2.4, 5, or 60 GHz channel allowed");
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006542 return;
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07006543out:
6544 wpa_printf(MSG_DEBUG, "P2P: Set GO freq %d MHz (no preference known)",
6545 params->freq);
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07006546}
6547
6548
Roshan Pius3a1667e2018-07-03 15:17:14 -07006549static int wpas_same_band(int freq1, int freq2)
6550{
6551 enum hostapd_hw_mode mode1, mode2;
6552 u8 chan1, chan2;
6553
6554 mode1 = ieee80211_freq_to_chan(freq1, &chan1);
6555 mode2 = ieee80211_freq_to_chan(freq2, &chan2);
6556 if (mode1 == NUM_HOSTAPD_MODES)
6557 return 0;
6558 return mode1 == mode2;
6559}
6560
6561
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006562static int wpas_p2p_init_go_params(struct wpa_supplicant *wpa_s,
6563 struct p2p_go_neg_results *params,
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08006564 int freq, int vht_center_freq2, int ht40,
Hai Shalom74f70d42019-02-11 14:42:39 -08006565 int vht, int max_oper_chwidth, int he,
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08006566 int edmg,
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08006567 const struct p2p_channels *channels)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006568{
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07006569 struct wpa_used_freq_data *freqs;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006570 unsigned int cand;
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07006571 unsigned int num, i;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006572 int ignore_no_freqs = 0;
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08006573 int unused_channels = wpas_p2p_num_unused_channels(wpa_s) > 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006574
6575 os_memset(params, 0, sizeof(*params));
6576 params->role_go = 1;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07006577 params->ht40 = ht40;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07006578 params->vht = vht;
Hai Shalom74f70d42019-02-11 14:42:39 -08006579 params->he = he;
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08006580 params->max_oper_chwidth = max_oper_chwidth;
6581 params->vht_center_freq2 = vht_center_freq2;
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08006582 params->edmg = edmg;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006583
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07006584 freqs = os_calloc(wpa_s->num_multichan_concurrent,
6585 sizeof(struct wpa_used_freq_data));
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07006586 if (!freqs)
6587 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006588
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006589 num = get_shared_radio_freqs_data(wpa_s, freqs,
Sunil Ravi77d572f2023-01-17 23:58:31 +00006590 wpa_s->num_multichan_concurrent,
6591 false);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006592
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006593 if (wpa_s->current_ssid &&
6594 wpa_s->current_ssid->mode == WPAS_MODE_P2P_GO &&
6595 wpa_s->wpa_state == WPA_COMPLETED) {
6596 wpa_printf(MSG_DEBUG, "P2P: %s called for an active GO",
6597 __func__);
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07006598
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006599 /*
6600 * If the frequency selection is done for an active P2P GO that
6601 * is not sharing a frequency, allow to select a new frequency
6602 * even if there are no unused frequencies as we are about to
6603 * move the P2P GO so its frequency can be re-used.
6604 */
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07006605 for (i = 0; i < num; i++) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006606 if (freqs[i].freq == wpa_s->current_ssid->frequency &&
6607 freqs[i].flags == 0) {
6608 ignore_no_freqs = 1;
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07006609 break;
6610 }
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07006611 }
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08006612 }
6613
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08006614 /* Try to use EDMG channel */
6615 if (params->edmg) {
6616 if (wpas_p2p_try_edmg_channel(wpa_s, params) == 0)
6617 goto success;
6618 params->edmg = 0;
6619 }
6620
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006621 /* try using the forced freq */
6622 if (freq) {
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07006623 if (wpas_p2p_disallowed_freq(wpa_s->global, freq) ||
6624 !freq_included(wpa_s, channels, freq)) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006625 wpa_printf(MSG_DEBUG,
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07006626 "P2P: Forced GO freq %d MHz disallowed",
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006627 freq);
6628 goto fail;
6629 }
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07006630 if (!p2p_supported_freq_go(wpa_s->global->p2p, freq)) {
6631 if ((wpa_s->drv_flags & WPA_DRIVER_FLAGS_DFS_OFFLOAD) &&
Roshan Pius3a1667e2018-07-03 15:17:14 -07006632 ieee80211_is_dfs(freq, wpa_s->hw.modes,
6633 wpa_s->hw.num_modes)) {
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07006634 /*
6635 * If freq is a DFS channel and DFS is offloaded
6636 * to the driver, allow P2P GO to use it.
6637 */
6638 wpa_printf(MSG_DEBUG,
6639 "P2P: %s: The forced channel for GO (%u MHz) requires DFS and DFS is offloaded",
6640 __func__, freq);
6641 } else {
6642 wpa_printf(MSG_DEBUG,
6643 "P2P: The forced channel for GO (%u MHz) is not supported for P2P uses",
6644 freq);
6645 goto fail;
6646 }
6647 }
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006648
6649 for (i = 0; i < num; i++) {
6650 if (freqs[i].freq == freq) {
6651 wpa_printf(MSG_DEBUG,
6652 "P2P: forced freq (%d MHz) is also shared",
6653 freq);
6654 params->freq = freq;
6655 goto success;
6656 }
6657 }
6658
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08006659 if (!ignore_no_freqs && !unused_channels) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006660 wpa_printf(MSG_DEBUG,
6661 "P2P: Cannot force GO on freq (%d MHz) as all the channels are in use",
6662 freq);
6663 goto fail;
6664 }
6665
6666 wpa_printf(MSG_DEBUG,
6667 "P2P: force GO freq (%d MHz) on a free channel",
6668 freq);
6669 params->freq = freq;
6670 goto success;
6671 }
6672
6673 /* consider using one of the shared frequencies */
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08006674 if (num &&
6675 (!wpa_s->conf->p2p_ignore_shared_freq || !unused_channels)) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006676 cand = wpas_p2p_pick_best_used_freq(wpa_s, freqs, num);
6677 if (wpas_p2p_supported_freq_go(wpa_s, channels, cand)) {
6678 wpa_printf(MSG_DEBUG,
6679 "P2P: Use shared freq (%d MHz) for GO",
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08006680 cand);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006681 params->freq = cand;
6682 goto success;
6683 }
6684
6685 /* try using one of the shared freqs */
6686 for (i = 0; i < num; i++) {
6687 if (wpas_p2p_supported_freq_go(wpa_s, channels,
6688 freqs[i].freq)) {
6689 wpa_printf(MSG_DEBUG,
6690 "P2P: Use shared freq (%d MHz) for GO",
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08006691 freqs[i].freq);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006692 params->freq = freqs[i].freq;
6693 goto success;
6694 }
6695 }
6696 }
6697
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08006698 if (!ignore_no_freqs && !unused_channels) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006699 wpa_printf(MSG_DEBUG,
6700 "P2P: Cannot force GO on any of the channels we are already using");
6701 goto fail;
6702 }
6703
6704 /* try using the setting from the configuration file */
6705 if (wpa_s->conf->p2p_oper_reg_class == 81 &&
6706 wpa_s->conf->p2p_oper_channel >= 1 &&
6707 wpa_s->conf->p2p_oper_channel <= 11 &&
6708 wpas_p2p_supported_freq_go(
6709 wpa_s, channels,
6710 2407 + 5 * wpa_s->conf->p2p_oper_channel)) {
6711 params->freq = 2407 + 5 * wpa_s->conf->p2p_oper_channel;
6712 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on configured "
6713 "frequency %d MHz", params->freq);
6714 goto success;
6715 }
6716
6717 if ((wpa_s->conf->p2p_oper_reg_class == 115 ||
6718 wpa_s->conf->p2p_oper_reg_class == 116 ||
6719 wpa_s->conf->p2p_oper_reg_class == 117 ||
6720 wpa_s->conf->p2p_oper_reg_class == 124 ||
6721 wpa_s->conf->p2p_oper_reg_class == 125 ||
6722 wpa_s->conf->p2p_oper_reg_class == 126 ||
6723 wpa_s->conf->p2p_oper_reg_class == 127) &&
6724 wpas_p2p_supported_freq_go(wpa_s, channels,
6725 5000 +
6726 5 * wpa_s->conf->p2p_oper_channel)) {
6727 params->freq = 5000 + 5 * wpa_s->conf->p2p_oper_channel;
6728 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on configured "
6729 "frequency %d MHz", params->freq);
6730 goto success;
6731 }
6732
6733 /* Try using best channels */
6734 if (wpa_s->conf->p2p_oper_channel == 0 &&
6735 wpa_s->best_overall_freq > 0 &&
6736 wpas_p2p_supported_freq_go(wpa_s, channels,
6737 wpa_s->best_overall_freq)) {
6738 params->freq = wpa_s->best_overall_freq;
6739 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on best overall "
6740 "channel %d MHz", params->freq);
6741 goto success;
6742 }
6743
6744 if (wpa_s->conf->p2p_oper_channel == 0 &&
6745 wpa_s->best_24_freq > 0 &&
6746 wpas_p2p_supported_freq_go(wpa_s, channels,
6747 wpa_s->best_24_freq)) {
6748 params->freq = wpa_s->best_24_freq;
6749 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on best 2.4 GHz "
6750 "channel %d MHz", params->freq);
6751 goto success;
6752 }
6753
6754 if (wpa_s->conf->p2p_oper_channel == 0 &&
6755 wpa_s->best_5_freq > 0 &&
6756 wpas_p2p_supported_freq_go(wpa_s, channels,
6757 wpa_s->best_5_freq)) {
6758 params->freq = wpa_s->best_5_freq;
6759 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on best 5 GHz "
6760 "channel %d MHz", params->freq);
6761 goto success;
6762 }
6763
6764 /* try using preferred channels */
6765 cand = p2p_get_pref_freq(wpa_s->global->p2p, channels);
6766 if (cand && wpas_p2p_supported_freq_go(wpa_s, channels, cand)) {
6767 params->freq = cand;
6768 wpa_printf(MSG_DEBUG, "P2P: Set GO freq %d MHz from preferred "
6769 "channels", params->freq);
6770 goto success;
6771 }
6772
Roshan Pius3a1667e2018-07-03 15:17:14 -07006773 /* Try using a channel that allows VHT to be used with 80 MHz */
6774 if (wpa_s->hw.modes && wpa_s->p2p_group_common_freqs) {
6775 for (i = 0; i < wpa_s->p2p_group_common_freqs_num; i++) {
6776 enum hostapd_hw_mode mode;
6777 struct hostapd_hw_modes *hwmode;
6778 u8 chan;
Hai Shalomc1a21442022-02-04 13:43:00 -08006779 u8 op_class;
Roshan Pius3a1667e2018-07-03 15:17:14 -07006780
6781 cand = wpa_s->p2p_group_common_freqs[i];
Hai Shalomc1a21442022-02-04 13:43:00 -08006782 op_class = is_6ghz_freq(cand) ? 133 : 128;
Roshan Pius3a1667e2018-07-03 15:17:14 -07006783 mode = ieee80211_freq_to_chan(cand, &chan);
6784 hwmode = get_mode(wpa_s->hw.modes, wpa_s->hw.num_modes,
Hai Shalomfdcde762020-04-02 11:19:20 -07006785 mode, is_6ghz_freq(cand));
Roshan Pius3a1667e2018-07-03 15:17:14 -07006786 if (!hwmode ||
Hai Shalomc1a21442022-02-04 13:43:00 -08006787 wpas_p2p_verify_channel(wpa_s, hwmode, op_class,
6788 chan, BW80) != ALLOWED)
Roshan Pius3a1667e2018-07-03 15:17:14 -07006789 continue;
6790 if (wpas_p2p_supported_freq_go(wpa_s, channels, cand)) {
6791 params->freq = cand;
6792 wpa_printf(MSG_DEBUG,
6793 "P2P: Use freq %d MHz common with the peer and allowing VHT80",
6794 params->freq);
6795 goto success;
6796 }
6797 }
6798 }
6799
6800 /* Try using a channel that allows HT to be used with 40 MHz on the same
6801 * band so that CSA can be used */
6802 if (wpa_s->current_ssid && wpa_s->hw.modes &&
6803 wpa_s->p2p_group_common_freqs) {
6804 for (i = 0; i < wpa_s->p2p_group_common_freqs_num; i++) {
6805 enum hostapd_hw_mode mode;
6806 struct hostapd_hw_modes *hwmode;
Hai Shalomc1a21442022-02-04 13:43:00 -08006807 u8 chan, op_class;
6808 bool is_6ghz, supported = false;
Roshan Pius3a1667e2018-07-03 15:17:14 -07006809
Hai Shalomc1a21442022-02-04 13:43:00 -08006810 is_6ghz = is_6ghz_freq(cand);
Roshan Pius3a1667e2018-07-03 15:17:14 -07006811 cand = wpa_s->p2p_group_common_freqs[i];
6812 mode = ieee80211_freq_to_chan(cand, &chan);
6813 hwmode = get_mode(wpa_s->hw.modes, wpa_s->hw.num_modes,
Hai Shalomc1a21442022-02-04 13:43:00 -08006814 mode, is_6ghz);
Roshan Pius3a1667e2018-07-03 15:17:14 -07006815 if (!wpas_same_band(wpa_s->current_ssid->frequency,
6816 cand) ||
Hai Shalomc1a21442022-02-04 13:43:00 -08006817 !hwmode)
Roshan Pius3a1667e2018-07-03 15:17:14 -07006818 continue;
Hai Shalomc1a21442022-02-04 13:43:00 -08006819 if (is_6ghz &&
6820 wpas_p2p_verify_channel(wpa_s, hwmode, 132, chan,
6821 BW40) == ALLOWED)
6822 supported = true;
6823
6824 if (!is_6ghz &&
6825 ieee80211_freq_to_channel_ext(
Sunil8cd6f4d2022-06-28 18:40:46 +00006826 cand, -1, CONF_OPER_CHWIDTH_USE_HT,
6827 &op_class, &chan) != NUM_HOSTAPD_MODES &&
Hai Shalomc1a21442022-02-04 13:43:00 -08006828 wpas_p2p_verify_channel(
6829 wpa_s, hwmode, op_class, chan,
6830 BW40MINUS) == ALLOWED)
6831 supported = true;
6832
6833 if (!supported && !is_6ghz &&
6834 ieee80211_freq_to_channel_ext(
Sunil8cd6f4d2022-06-28 18:40:46 +00006835 cand, 1, CONF_OPER_CHWIDTH_USE_HT,
6836 &op_class, &chan) != NUM_HOSTAPD_MODES &&
Hai Shalomc1a21442022-02-04 13:43:00 -08006837 wpas_p2p_verify_channel(
6838 wpa_s, hwmode, op_class, chan,
6839 BW40PLUS) == ALLOWED)
6840 supported = true;
6841
6842 if (!supported)
6843 continue;
6844
Roshan Pius3a1667e2018-07-03 15:17:14 -07006845 if (wpas_p2p_supported_freq_go(wpa_s, channels, cand)) {
6846 params->freq = cand;
6847 wpa_printf(MSG_DEBUG,
6848 "P2P: Use freq %d MHz common with the peer, allowing HT40, and maintaining same band",
6849 params->freq);
6850 goto success;
6851 }
6852 }
6853 }
6854
6855 /* Try using one of the group common freqs on the same band so that CSA
6856 * can be used */
6857 if (wpa_s->current_ssid && wpa_s->p2p_group_common_freqs) {
6858 for (i = 0; i < wpa_s->p2p_group_common_freqs_num; i++) {
6859 cand = wpa_s->p2p_group_common_freqs[i];
6860 if (!wpas_same_band(wpa_s->current_ssid->frequency,
6861 cand))
6862 continue;
6863 if (wpas_p2p_supported_freq_go(wpa_s, channels, cand)) {
6864 params->freq = cand;
6865 wpa_printf(MSG_DEBUG,
6866 "P2P: Use freq %d MHz common with the peer and maintaining same band",
6867 params->freq);
6868 goto success;
6869 }
6870 }
6871 }
6872
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006873 /* Try using one of the group common freqs */
6874 if (wpa_s->p2p_group_common_freqs) {
6875 for (i = 0; i < wpa_s->p2p_group_common_freqs_num; i++) {
6876 cand = wpa_s->p2p_group_common_freqs[i];
6877 if (wpas_p2p_supported_freq_go(wpa_s, channels, cand)) {
6878 params->freq = cand;
6879 wpa_printf(MSG_DEBUG,
6880 "P2P: Use freq %d MHz common with the peer",
6881 params->freq);
6882 goto success;
6883 }
6884 }
6885 }
6886
6887 /* no preference, select some channel */
6888 wpas_p2p_select_go_freq_no_pref(wpa_s, params, channels);
6889
6890 if (params->freq == 0) {
6891 wpa_printf(MSG_DEBUG, "P2P: did not find a freq for GO use");
6892 goto fail;
6893 }
6894
6895success:
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07006896 os_free(freqs);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006897 return 0;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006898fail:
6899 os_free(freqs);
6900 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006901}
6902
6903
6904static struct wpa_supplicant *
6905wpas_p2p_get_group_iface(struct wpa_supplicant *wpa_s, int addr_allocated,
6906 int go)
6907{
6908 struct wpa_supplicant *group_wpa_s;
6909
Dmitry Shmidtaa532512012-09-24 10:35:31 -07006910 if (!wpas_p2p_create_iface(wpa_s)) {
Dmitry Shmidt9c175262016-03-03 10:20:07 -08006911 if (wpa_s->p2p_mgmt) {
6912 /*
6913 * We may be called on the p2p_dev interface which
6914 * cannot be used for group operations, so always use
6915 * the primary interface.
6916 */
6917 wpa_s->parent->p2pdev = wpa_s;
6918 wpa_s = wpa_s->parent;
6919 }
6920 wpa_dbg(wpa_s, MSG_DEBUG,
6921 "P2P: Use primary interface for group operations");
Dmitry Shmidt56052862013-10-04 10:23:25 -07006922 wpa_s->p2p_first_connection_timeout = 0;
Dmitry Shmidt9c175262016-03-03 10:20:07 -08006923 if (wpa_s != wpa_s->p2pdev)
Dmitry Shmidt849734c2016-05-27 09:59:01 -07006924 wpas_p2p_clone_config(wpa_s, wpa_s->p2pdev);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006925 return wpa_s;
Dmitry Shmidtaa532512012-09-24 10:35:31 -07006926 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006927
6928 if (wpas_p2p_add_group_interface(wpa_s, go ? WPA_IF_P2P_GO :
Dmitry Shmidtaa532512012-09-24 10:35:31 -07006929 WPA_IF_P2P_CLIENT) < 0) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07006930 wpa_msg_global(wpa_s, MSG_ERROR,
6931 "P2P: Failed to add group interface");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006932 return NULL;
Dmitry Shmidtaa532512012-09-24 10:35:31 -07006933 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006934 group_wpa_s = wpas_p2p_init_group_interface(wpa_s, go);
6935 if (group_wpa_s == NULL) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07006936 wpa_msg_global(wpa_s, MSG_ERROR,
6937 "P2P: Failed to initialize group interface");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006938 wpas_p2p_remove_pending_group_interface(wpa_s);
6939 return NULL;
6940 }
6941
Roshan Pius3a1667e2018-07-03 15:17:14 -07006942 if (go && wpa_s->p2p_go_do_acs) {
6943 group_wpa_s->p2p_go_do_acs = wpa_s->p2p_go_do_acs;
6944 group_wpa_s->p2p_go_acs_band = wpa_s->p2p_go_acs_band;
6945 wpa_s->p2p_go_do_acs = 0;
6946 }
6947
Hai Shalomc1a21442022-02-04 13:43:00 -08006948 if (go && wpa_s->p2p_go_allow_dfs) {
6949 group_wpa_s->p2p_go_allow_dfs = wpa_s->p2p_go_allow_dfs;
6950 wpa_s->p2p_go_allow_dfs = 0;
6951 }
6952
Dmitry Shmidtaa532512012-09-24 10:35:31 -07006953 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Use separate group interface %s",
6954 group_wpa_s->ifname);
Dmitry Shmidt56052862013-10-04 10:23:25 -07006955 group_wpa_s->p2p_first_connection_timeout = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006956 return group_wpa_s;
6957}
6958
6959
6960/**
6961 * wpas_p2p_group_add - Add a new P2P group with local end as Group Owner
6962 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
6963 * @persistent_group: Whether to create a persistent group
6964 * @freq: Frequency for the group or 0 to indicate no hardcoding
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08006965 * @vht_center_freq2: segment_1 center frequency for GO operating in VHT 80P80
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07006966 * @ht40: Start GO with 40 MHz channel width
6967 * @vht: Start GO with VHT support
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08006968 * @vht_chwidth: channel bandwidth for GO operating with VHT support
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08006969 * @edmg: Start GO with EDMG support
Hai Shalomc1a21442022-02-04 13:43:00 -08006970 * @allow_6ghz: Allow P2P group creation on a 6 GHz channel
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006971 * Returns: 0 on success, -1 on failure
6972 *
6973 * This function creates a new P2P group with the local end as the Group Owner,
6974 * i.e., without using Group Owner Negotiation.
6975 */
6976int wpas_p2p_group_add(struct wpa_supplicant *wpa_s, int persistent_group,
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08006977 int freq, int vht_center_freq2, int ht40, int vht,
Hai Shalomc1a21442022-02-04 13:43:00 -08006978 int max_oper_chwidth, int he, int edmg,
6979 bool allow_6ghz)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006980{
6981 struct p2p_go_neg_results params;
Sunil Ravi77a0f092022-10-03 00:53:41 +00006982 int selected_freq = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006983
6984 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
6985 return -1;
Hai Shalomc1a21442022-02-04 13:43:00 -08006986 if (wpas_p2p_check_6ghz(wpa_s, NULL, allow_6ghz, freq))
6987 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006988
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07006989 os_free(wpa_s->global->add_psk);
6990 wpa_s->global->add_psk = NULL;
6991
Dmitry Shmidtdca39792011-09-06 11:17:33 -07006992 /* Make sure we are not running find during connection establishment */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006993 wpa_printf(MSG_DEBUG, "P2P: Stop any on-going P2P FIND");
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08006994 wpas_p2p_stop_find_oper(wpa_s);
Dmitry Shmidtdca39792011-09-06 11:17:33 -07006995
Roshan Pius3a1667e2018-07-03 15:17:14 -07006996 if (!wpa_s->p2p_go_do_acs) {
Sunil Ravi77a0f092022-10-03 00:53:41 +00006997 selected_freq = wpas_p2p_select_go_freq(wpa_s, freq);
6998 if (selected_freq < 0)
Roshan Pius3a1667e2018-07-03 15:17:14 -07006999 return -1;
7000 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007001
Sunil Ravi77a0f092022-10-03 00:53:41 +00007002 if (wpas_p2p_init_go_params(wpa_s, &params, selected_freq, vht_center_freq2,
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08007003 ht40, vht, max_oper_chwidth, he, edmg,
7004 NULL))
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007005 return -1;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07007006
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007007 p2p_go_params(wpa_s->global->p2p, &params);
7008 params.persistent_group = persistent_group;
7009
7010 wpa_s = wpas_p2p_get_group_iface(wpa_s, 0, 1);
7011 if (wpa_s == NULL)
7012 return -1;
Sunil Ravi77a0f092022-10-03 00:53:41 +00007013 if (freq > 0)
7014 wpa_s->p2p_go_no_pri_sec_switch = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007015 wpas_start_wps_go(wpa_s, &params, 0);
7016
7017 return 0;
7018}
7019
7020
7021static int wpas_start_p2p_client(struct wpa_supplicant *wpa_s,
Dmitry Shmidt15907092014-03-25 10:42:57 -07007022 struct wpa_ssid *params, int addr_allocated,
Matthew Wang124e5f42022-12-29 07:23:06 +00007023 int freq, int force_scan, int retry_limit,
Sunil Ravi036cec52023-03-29 11:35:17 -07007024 const u8 *go_bssid)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007025{
7026 struct wpa_ssid *ssid;
Matthew Wang9ae940b2022-09-14 21:25:34 -07007027 int other_iface_found = 0;
7028 struct wpa_supplicant *ifs;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007029
7030 wpa_s = wpas_p2p_get_group_iface(wpa_s, addr_allocated, 0);
7031 if (wpa_s == NULL)
7032 return -1;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08007033 if (force_scan)
7034 os_get_reltime(&wpa_s->scan_min_time);
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07007035 wpa_s->p2p_last_4way_hs_fail = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007036
7037 wpa_supplicant_ap_deinit(wpa_s);
7038
7039 ssid = wpa_config_add_network(wpa_s->conf);
7040 if (ssid == NULL)
7041 return -1;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08007042 os_memset(wpa_s->go_dev_addr, 0, ETH_ALEN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007043 wpa_config_set_network_defaults(ssid);
7044 ssid->temporary = 1;
7045 ssid->proto = WPA_PROTO_RSN;
Dmitry Shmidte4663042016-04-04 10:07:49 -07007046 ssid->pbss = params->pbss;
7047 ssid->pairwise_cipher = params->pbss ? WPA_CIPHER_GCMP :
7048 WPA_CIPHER_CCMP;
7049 ssid->group_cipher = params->pbss ? WPA_CIPHER_GCMP : WPA_CIPHER_CCMP;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007050 ssid->key_mgmt = WPA_KEY_MGMT_PSK;
Sunil Ravi036cec52023-03-29 11:35:17 -07007051 if (is_6ghz_freq(freq) &&
7052 is_p2p_6ghz_capable(wpa_s->global->p2p)) {
7053 ssid->auth_alg |= WPA_AUTH_ALG_SAE;
7054 ssid->key_mgmt |= WPA_KEY_MGMT_SAE;
7055 ssid->ieee80211w = MGMT_FRAME_PROTECTION_OPTIONAL;
7056 ssid->sae_pwe = SAE_PWE_HASH_TO_ELEMENT;
7057 wpa_dbg(wpa_s, MSG_DEBUG,
7058 "P2P: Enable SAE auth_alg and key_mgmt");
7059 }
7060
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007061 ssid->ssid = os_malloc(params->ssid_len);
7062 if (ssid->ssid == NULL) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007063 wpa_config_remove_network(wpa_s->conf, ssid->id);
7064 return -1;
7065 }
7066 os_memcpy(ssid->ssid, params->ssid, params->ssid_len);
7067 ssid->ssid_len = params->ssid_len;
7068 ssid->p2p_group = 1;
7069 ssid->export_keys = 1;
7070 if (params->psk_set) {
7071 os_memcpy(ssid->psk, params->psk, 32);
7072 ssid->psk_set = 1;
7073 }
7074 if (params->passphrase)
7075 ssid->passphrase = os_strdup(params->passphrase);
7076
Sunil Ravi036cec52023-03-29 11:35:17 -07007077 if (go_bssid) {
Matthew Wangdcf19452022-11-07 20:42:52 -08007078 ssid->bssid_set = 1;
Sunil Ravi036cec52023-03-29 11:35:17 -07007079 os_memcpy(ssid->bssid, go_bssid, ETH_ALEN);
Matthew Wangdcf19452022-11-07 20:42:52 -08007080 }
7081
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007082 wpa_s->show_group_started = 1;
Dmitry Shmidt15907092014-03-25 10:42:57 -07007083 wpa_s->p2p_in_invitation = 1;
Matthew Wang06b42472022-11-10 06:56:31 +00007084 wpa_s->p2p_retry_limit = retry_limit;
Dmitry Shmidt15907092014-03-25 10:42:57 -07007085 wpa_s->p2p_invite_go_freq = freq;
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08007086 wpa_s->p2p_go_group_formation_completed = 0;
7087 wpa_s->global->p2p_group_formation = wpa_s;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007088
Matthew Wang9ae940b2022-09-14 21:25:34 -07007089 /*
7090 * Get latest scan results from driver in case cached scan results from
7091 * interfaces on the same wiphy allow us to skip the next scan by fast
7092 * associating. Also update the scan time to the most recent scan result
7093 * fetch time on the same radio so it reflects the actual time the last
7094 * scan result event occurred.
7095 */
7096 wpa_supplicant_update_scan_results(wpa_s);
7097 dl_list_for_each(ifs, &wpa_s->radio->ifaces, struct wpa_supplicant,
7098 radio_list) {
7099 if (ifs == wpa_s)
7100 continue;
7101 if (!other_iface_found || os_reltime_before(&wpa_s->last_scan,
7102 &ifs->last_scan)) {
7103 other_iface_found = 1;
7104 wpa_s->last_scan.sec = ifs->last_scan.sec;
7105 wpa_s->last_scan.usec = ifs->last_scan.usec;
7106 }
7107 }
7108
Dmitry Shmidt9c175262016-03-03 10:20:07 -08007109 eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s->p2pdev,
Dmitry Shmidt15907092014-03-25 10:42:57 -07007110 NULL);
7111 eloop_register_timeout(P2P_MAX_INITIAL_CONN_WAIT, 0,
7112 wpas_p2p_group_formation_timeout,
Dmitry Shmidt9c175262016-03-03 10:20:07 -08007113 wpa_s->p2pdev, NULL);
Dmitry Shmidtfa3fc4a2013-11-21 13:34:38 -08007114 wpa_supplicant_select_network(wpa_s, ssid);
7115
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007116 return 0;
7117}
7118
7119
7120int wpas_p2p_group_add_persistent(struct wpa_supplicant *wpa_s,
7121 struct wpa_ssid *ssid, int addr_allocated,
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08007122 int force_freq, int neg_freq,
7123 int vht_center_freq2, int ht40,
Hai Shalom74f70d42019-02-11 14:42:39 -08007124 int vht, int max_oper_chwidth, int he,
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08007125 int edmg,
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08007126 const struct p2p_channels *channels,
Hai Shalomc1a21442022-02-04 13:43:00 -08007127 int connection_timeout, int force_scan,
Matthew Wang124e5f42022-12-29 07:23:06 +00007128 bool allow_6ghz, int retry_limit,
Sunil Ravi036cec52023-03-29 11:35:17 -07007129 const u8 *go_bssid)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007130{
7131 struct p2p_go_neg_results params;
Dmitry Shmidt96be6222014-02-13 10:16:51 -08007132 int go = 0, freq;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007133
7134 if (ssid->disabled != 2 || ssid->ssid == NULL)
7135 return -1;
7136
7137 if (wpas_get_p2p_group(wpa_s, ssid->ssid, ssid->ssid_len, &go) &&
7138 go == (ssid->mode == WPAS_MODE_P2P_GO)) {
7139 wpa_printf(MSG_DEBUG, "P2P: Requested persistent group is "
7140 "already running");
Dmitry Shmidtb1e52102015-05-29 12:36:29 -07007141 if (go == 0 &&
7142 eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
Dmitry Shmidt9c175262016-03-03 10:20:07 -08007143 wpa_s->p2pdev, NULL)) {
Dmitry Shmidtb1e52102015-05-29 12:36:29 -07007144 /*
7145 * This can happen if Invitation Response frame was lost
7146 * and the peer (GO of a persistent group) tries to
7147 * invite us again. Reschedule the timeout to avoid
7148 * terminating the wait for the connection too early
7149 * since we now know that the peer is still trying to
7150 * invite us instead of having already started the GO.
7151 */
7152 wpa_printf(MSG_DEBUG,
7153 "P2P: Reschedule group formation timeout since peer is still trying to invite us");
7154 eloop_register_timeout(P2P_MAX_INITIAL_CONN_WAIT, 0,
7155 wpas_p2p_group_formation_timeout,
Dmitry Shmidt9c175262016-03-03 10:20:07 -08007156 wpa_s->p2pdev, NULL);
Dmitry Shmidtb1e52102015-05-29 12:36:29 -07007157 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007158 return 0;
7159 }
7160
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07007161 os_free(wpa_s->global->add_psk);
7162 wpa_s->global->add_psk = NULL;
7163
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007164 /* Make sure we are not running find during connection establishment */
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08007165 wpas_p2p_stop_find_oper(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007166
Dmitry Shmidt04949592012-07-19 12:16:46 -07007167 wpa_s->p2p_fallback_to_go_neg = 0;
7168
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007169 if (ssid->mode == WPAS_MODE_P2P_GO) {
7170 if (force_freq > 0) {
7171 freq = wpas_p2p_select_go_freq(wpa_s, force_freq);
7172 if (freq < 0)
7173 return -1;
Sunil Ravi77a0f092022-10-03 00:53:41 +00007174 wpa_s->p2p_go_no_pri_sec_switch = 1;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007175 } else {
7176 freq = wpas_p2p_select_go_freq(wpa_s, neg_freq);
7177 if (freq < 0 ||
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08007178 (freq > 0 && !freq_included(wpa_s, channels, freq)))
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007179 freq = 0;
7180 }
Dmitry Shmidt912c6ec2015-03-30 13:16:51 -07007181 } else if (ssid->mode == WPAS_MODE_INFRA) {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007182 freq = neg_freq;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08007183 if (freq <= 0 || !freq_included(wpa_s, channels, freq)) {
Dmitry Shmidt912c6ec2015-03-30 13:16:51 -07007184 struct os_reltime now;
7185 struct wpa_bss *bss =
7186 wpa_bss_get_p2p_dev_addr(wpa_s, ssid->bssid);
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07007187
Dmitry Shmidt912c6ec2015-03-30 13:16:51 -07007188 os_get_reltime(&now);
7189 if (bss &&
7190 !os_reltime_expired(&now, &bss->last_update, 5) &&
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08007191 freq_included(wpa_s, channels, bss->freq))
Dmitry Shmidt912c6ec2015-03-30 13:16:51 -07007192 freq = bss->freq;
7193 else
7194 freq = 0;
7195 }
7196
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08007197 return wpas_start_p2p_client(wpa_s, ssid, addr_allocated, freq,
Sunil Ravi036cec52023-03-29 11:35:17 -07007198 force_scan, retry_limit, go_bssid);
Dmitry Shmidt912c6ec2015-03-30 13:16:51 -07007199 } else {
Dmitry Shmidt15907092014-03-25 10:42:57 -07007200 return -1;
Dmitry Shmidt912c6ec2015-03-30 13:16:51 -07007201 }
Dmitry Shmidt15907092014-03-25 10:42:57 -07007202
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08007203 if (wpas_p2p_init_go_params(wpa_s, &params, freq, vht_center_freq2,
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08007204 ht40, vht, max_oper_chwidth, he, edmg,
7205 channels))
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007206 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007207
7208 params.role_go = 1;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08007209 params.psk_set = ssid->psk_set;
7210 if (params.psk_set)
7211 os_memcpy(params.psk, ssid->psk, sizeof(params.psk));
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08007212 if (ssid->passphrase) {
7213 if (os_strlen(ssid->passphrase) >= sizeof(params.passphrase)) {
7214 wpa_printf(MSG_ERROR, "P2P: Invalid passphrase in "
7215 "persistent group");
7216 return -1;
7217 }
7218 os_strlcpy(params.passphrase, ssid->passphrase,
7219 sizeof(params.passphrase));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007220 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007221 os_memcpy(params.ssid, ssid->ssid, ssid->ssid_len);
7222 params.ssid_len = ssid->ssid_len;
7223 params.persistent_group = 1;
7224
7225 wpa_s = wpas_p2p_get_group_iface(wpa_s, addr_allocated, 1);
7226 if (wpa_s == NULL)
7227 return -1;
7228
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007229 p2p_channels_to_freqs(channels, params.freq_list, P2P_MAX_CHANNELS);
7230
Dmitry Shmidt56052862013-10-04 10:23:25 -07007231 wpa_s->p2p_first_connection_timeout = connection_timeout;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007232 wpas_start_wps_go(wpa_s, &params, 0);
7233
7234 return 0;
7235}
7236
7237
7238static void wpas_p2p_ie_update(void *ctx, struct wpabuf *beacon_ies,
7239 struct wpabuf *proberesp_ies)
7240{
7241 struct wpa_supplicant *wpa_s = ctx;
7242 if (wpa_s->ap_iface) {
7243 struct hostapd_data *hapd = wpa_s->ap_iface->bss[0];
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007244 if (!(hapd->conf->p2p & P2P_GROUP_OWNER)) {
7245 wpabuf_free(beacon_ies);
7246 wpabuf_free(proberesp_ies);
7247 return;
7248 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007249 if (beacon_ies) {
7250 wpabuf_free(hapd->p2p_beacon_ie);
7251 hapd->p2p_beacon_ie = beacon_ies;
7252 }
7253 wpabuf_free(hapd->p2p_probe_resp_ie);
7254 hapd->p2p_probe_resp_ie = proberesp_ies;
7255 } else {
7256 wpabuf_free(beacon_ies);
7257 wpabuf_free(proberesp_ies);
7258 }
7259 wpa_supplicant_ap_update_beacon(wpa_s);
7260}
7261
7262
7263static void wpas_p2p_idle_update(void *ctx, int idle)
7264{
7265 struct wpa_supplicant *wpa_s = ctx;
7266 if (!wpa_s->ap_iface)
7267 return;
7268 wpa_printf(MSG_DEBUG, "P2P: GO - group %sidle", idle ? "" : "not ");
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08007269 if (idle) {
7270 if (wpa_s->global->p2p_fail_on_wps_complete &&
7271 wpa_s->p2p_in_provisioning) {
7272 wpas_p2p_grpform_fail_after_wps(wpa_s);
7273 return;
7274 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007275 wpas_p2p_set_group_idle_timeout(wpa_s);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08007276 } else
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007277 eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL);
7278}
7279
7280
7281struct p2p_group * wpas_p2p_group_init(struct wpa_supplicant *wpa_s,
Dmitry Shmidt04949592012-07-19 12:16:46 -07007282 struct wpa_ssid *ssid)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007283{
7284 struct p2p_group *group;
7285 struct p2p_group_config *cfg;
7286
Dmitry Shmidt849734c2016-05-27 09:59:01 -07007287 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL ||
7288 !ssid->p2p_group)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007289 return NULL;
7290
7291 cfg = os_zalloc(sizeof(*cfg));
7292 if (cfg == NULL)
7293 return NULL;
7294
Dmitry Shmidt04949592012-07-19 12:16:46 -07007295 if (ssid->p2p_persistent_group && wpa_s->conf->persistent_reconnect)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007296 cfg->persistent_group = 2;
Dmitry Shmidt04949592012-07-19 12:16:46 -07007297 else if (ssid->p2p_persistent_group)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007298 cfg->persistent_group = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007299 os_memcpy(cfg->interface_addr, wpa_s->own_addr, ETH_ALEN);
7300 if (wpa_s->max_stations &&
7301 wpa_s->max_stations < wpa_s->conf->max_num_sta)
7302 cfg->max_clients = wpa_s->max_stations;
7303 else
7304 cfg->max_clients = wpa_s->conf->max_num_sta;
Dmitry Shmidt04949592012-07-19 12:16:46 -07007305 os_memcpy(cfg->ssid, ssid->ssid, ssid->ssid_len);
7306 cfg->ssid_len = ssid->ssid_len;
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08007307 cfg->freq = ssid->frequency;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007308 cfg->cb_ctx = wpa_s;
7309 cfg->ie_update = wpas_p2p_ie_update;
7310 cfg->idle_update = wpas_p2p_idle_update;
Dmitry Shmidte4663042016-04-04 10:07:49 -07007311 cfg->ip_addr_alloc = WPA_GET_BE32(wpa_s->p2pdev->conf->ip_addr_start)
7312 != 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007313
7314 group = p2p_group_init(wpa_s->global->p2p, cfg);
7315 if (group == NULL)
7316 os_free(cfg);
Dmitry Shmidt04949592012-07-19 12:16:46 -07007317 if (ssid->mode != WPAS_MODE_P2P_GROUP_FORMATION)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007318 p2p_group_notif_formation_done(group);
7319 wpa_s->p2p_group = group;
7320 return group;
7321}
7322
7323
7324void wpas_p2p_wps_success(struct wpa_supplicant *wpa_s, const u8 *peer_addr,
7325 int registrar)
7326{
Dmitry Shmidt04949592012-07-19 12:16:46 -07007327 struct wpa_ssid *ssid = wpa_s->current_ssid;
7328
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007329 if (!wpa_s->p2p_in_provisioning) {
7330 wpa_printf(MSG_DEBUG, "P2P: Ignore WPS success event - P2P "
7331 "provisioning not in progress");
7332 return;
7333 }
7334
Dmitry Shmidt04949592012-07-19 12:16:46 -07007335 if (ssid && ssid->mode == WPAS_MODE_INFRA) {
7336 u8 go_dev_addr[ETH_ALEN];
7337 os_memcpy(go_dev_addr, wpa_s->bssid, ETH_ALEN);
7338 wpas_p2p_persistent_group(wpa_s, go_dev_addr, ssid->ssid,
7339 ssid->ssid_len);
7340 /* Clear any stored provisioning info */
7341 p2p_clear_provisioning_info(wpa_s->global->p2p, go_dev_addr);
7342 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007343
Dmitry Shmidt9c175262016-03-03 10:20:07 -08007344 eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s->p2pdev,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007345 NULL);
Dmitry Shmidt56052862013-10-04 10:23:25 -07007346 wpa_s->p2p_go_group_formation_completed = 1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07007347 if (ssid && ssid->mode == WPAS_MODE_INFRA) {
7348 /*
7349 * Use a separate timeout for initial data connection to
7350 * complete to allow the group to be removed automatically if
7351 * something goes wrong in this step before the P2P group idle
7352 * timeout mechanism is taken into use.
7353 */
Dmitry Shmidt56052862013-10-04 10:23:25 -07007354 wpa_dbg(wpa_s, MSG_DEBUG,
7355 "P2P: Re-start group formation timeout (%d seconds) as client for initial connection",
7356 P2P_MAX_INITIAL_CONN_WAIT);
Dmitry Shmidt04949592012-07-19 12:16:46 -07007357 eloop_register_timeout(P2P_MAX_INITIAL_CONN_WAIT, 0,
7358 wpas_p2p_group_formation_timeout,
Dmitry Shmidt9c175262016-03-03 10:20:07 -08007359 wpa_s->p2pdev, NULL);
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08007360 /* Complete group formation on successful data connection. */
7361 wpa_s->p2p_go_group_formation_completed = 0;
Dmitry Shmidt56052862013-10-04 10:23:25 -07007362 } else if (ssid) {
7363 /*
7364 * Use a separate timeout for initial data connection to
7365 * complete to allow the group to be removed automatically if
7366 * the client does not complete data connection successfully.
7367 */
7368 wpa_dbg(wpa_s, MSG_DEBUG,
7369 "P2P: Re-start group formation timeout (%d seconds) as GO for initial connection",
7370 P2P_MAX_INITIAL_CONN_WAIT_GO);
7371 eloop_register_timeout(P2P_MAX_INITIAL_CONN_WAIT_GO, 0,
7372 wpas_p2p_group_formation_timeout,
Dmitry Shmidt9c175262016-03-03 10:20:07 -08007373 wpa_s->p2pdev, NULL);
Dmitry Shmidt56052862013-10-04 10:23:25 -07007374 /*
7375 * Complete group formation on first successful data connection
7376 */
7377 wpa_s->p2p_go_group_formation_completed = 0;
Dmitry Shmidt04949592012-07-19 12:16:46 -07007378 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007379 if (wpa_s->global->p2p)
7380 p2p_wps_success_cb(wpa_s->global->p2p, peer_addr);
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07007381 wpas_group_formation_completed(wpa_s, 1, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007382}
7383
7384
Jouni Malinen75ecf522011-06-27 15:19:46 -07007385void wpas_p2p_wps_failed(struct wpa_supplicant *wpa_s,
7386 struct wps_event_fail *fail)
7387{
7388 if (!wpa_s->p2p_in_provisioning) {
7389 wpa_printf(MSG_DEBUG, "P2P: Ignore WPS fail event - P2P "
7390 "provisioning not in progress");
7391 return;
7392 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007393
7394 if (wpa_s->go_params) {
7395 p2p_clear_provisioning_info(
7396 wpa_s->global->p2p,
Dmitry Shmidt04949592012-07-19 12:16:46 -07007397 wpa_s->go_params->peer_device_addr);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007398 }
7399
Jouni Malinen75ecf522011-06-27 15:19:46 -07007400 wpas_notify_p2p_wps_failed(wpa_s, fail);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08007401
7402 if (wpa_s == wpa_s->global->p2p_group_formation) {
7403 /*
7404 * Allow some time for the failed WPS negotiation exchange to
7405 * complete, but remove the group since group formation cannot
7406 * succeed after provisioning failure.
7407 */
7408 wpa_printf(MSG_DEBUG, "P2P: WPS step failed during group formation - reject connection from timeout");
7409 wpa_s->global->p2p_fail_on_wps_complete = 1;
7410 eloop_deplete_timeout(0, 50000,
7411 wpas_p2p_group_formation_timeout,
Dmitry Shmidt9c175262016-03-03 10:20:07 -08007412 wpa_s->p2pdev, NULL);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08007413 }
7414}
7415
7416
7417int wpas_p2p_wps_eapol_cb(struct wpa_supplicant *wpa_s)
7418{
7419 if (!wpa_s->global->p2p_fail_on_wps_complete ||
7420 !wpa_s->p2p_in_provisioning)
7421 return 0;
7422
7423 wpas_p2p_grpform_fail_after_wps(wpa_s);
7424
7425 return 1;
Jouni Malinen75ecf522011-06-27 15:19:46 -07007426}
7427
7428
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007429int wpas_p2p_prov_disc(struct wpa_supplicant *wpa_s, const u8 *peer_addr,
Dmitry Shmidt04949592012-07-19 12:16:46 -07007430 const char *config_method,
Dmitry Shmidt216983b2015-02-06 10:50:36 -08007431 enum wpas_p2p_prov_disc_use use,
7432 struct p2ps_provision *p2ps_prov)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007433{
7434 u16 config_methods;
7435
Dmitry Shmidt216983b2015-02-06 10:50:36 -08007436 wpa_s->global->pending_p2ps_group = 0;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08007437 wpa_s->global->pending_p2ps_group_freq = 0;
Dmitry Shmidt04949592012-07-19 12:16:46 -07007438 wpa_s->p2p_fallback_to_go_neg = 0;
7439 wpa_s->pending_pd_use = NORMAL_PD;
Dmitry Shmidt216983b2015-02-06 10:50:36 -08007440 if (p2ps_prov && use == WPAS_P2P_PD_FOR_ASP) {
7441 p2ps_prov->conncap = p2ps_group_capability(
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08007442 wpa_s, P2PS_SETUP_NONE, p2ps_prov->role,
7443 &p2ps_prov->force_freq, &p2ps_prov->pref_freq);
7444
Dmitry Shmidt216983b2015-02-06 10:50:36 -08007445 wpa_printf(MSG_DEBUG,
7446 "P2P: %s conncap: %d - ASP parsed: %x %x %d %s",
7447 __func__, p2ps_prov->conncap,
7448 p2ps_prov->adv_id, p2ps_prov->conncap,
7449 p2ps_prov->status, p2ps_prov->info);
7450
7451 config_methods = 0;
7452 } else if (os_strncmp(config_method, "display", 7) == 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007453 config_methods = WPS_CONFIG_DISPLAY;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007454 else if (os_strncmp(config_method, "keypad", 6) == 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007455 config_methods = WPS_CONFIG_KEYPAD;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007456 else if (os_strncmp(config_method, "pbc", 3) == 0 ||
7457 os_strncmp(config_method, "pushbutton", 10) == 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007458 config_methods = WPS_CONFIG_PUSHBUTTON;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007459 else {
7460 wpa_printf(MSG_DEBUG, "P2P: Unknown config method");
Dmitry Shmidt216983b2015-02-06 10:50:36 -08007461 os_free(p2ps_prov);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007462 return -1;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007463 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007464
Dmitry Shmidt04949592012-07-19 12:16:46 -07007465 if (use == WPAS_P2P_PD_AUTO) {
7466 os_memcpy(wpa_s->pending_join_dev_addr, peer_addr, ETH_ALEN);
7467 wpa_s->pending_pd_config_methods = config_methods;
7468 wpa_s->p2p_auto_pd = 1;
7469 wpa_s->p2p_auto_join = 0;
7470 wpa_s->pending_pd_before_join = 0;
7471 wpa_s->auto_pd_scan_retry = 0;
7472 wpas_p2p_stop_find(wpa_s);
7473 wpa_s->p2p_join_scan_count = 0;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08007474 os_get_reltime(&wpa_s->p2p_auto_started);
Dmitry Shmidt04949592012-07-19 12:16:46 -07007475 wpa_printf(MSG_DEBUG, "P2P: Auto PD started at %ld.%06ld",
7476 wpa_s->p2p_auto_started.sec,
7477 wpa_s->p2p_auto_started.usec);
7478 wpas_p2p_join_scan(wpa_s, NULL);
7479 return 0;
7480 }
7481
Dmitry Shmidt216983b2015-02-06 10:50:36 -08007482 if (wpa_s->global->p2p == NULL || wpa_s->global->p2p_disabled) {
7483 os_free(p2ps_prov);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007484 return -1;
Dmitry Shmidt216983b2015-02-06 10:50:36 -08007485 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007486
Dmitry Shmidt216983b2015-02-06 10:50:36 -08007487 return p2p_prov_disc_req(wpa_s->global->p2p, peer_addr, p2ps_prov,
Dmitry Shmidt04949592012-07-19 12:16:46 -07007488 config_methods, use == WPAS_P2P_PD_FOR_JOIN,
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08007489 0, 1);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007490}
7491
7492
7493int wpas_p2p_scan_result_text(const u8 *ies, size_t ies_len, char *buf,
7494 char *end)
7495{
7496 return p2p_scan_result_text(ies, ies_len, buf, end);
7497}
7498
7499
7500static void wpas_p2p_clear_pending_action_tx(struct wpa_supplicant *wpa_s)
7501{
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08007502 if (!offchannel_pending_action_tx(wpa_s))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007503 return;
7504
Dmitry Shmidtde47be72016-01-07 12:52:55 -08007505 if (wpa_s->p2p_send_action_work) {
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08007506 wpas_p2p_free_send_action_work(wpa_s);
Dmitry Shmidtde47be72016-01-07 12:52:55 -08007507 eloop_cancel_timeout(wpas_p2p_send_action_work_timeout,
7508 wpa_s, NULL);
7509 offchannel_send_action_done(wpa_s);
7510 }
Dmitry Shmidt03658832014-08-13 11:03:49 -07007511
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007512 wpa_printf(MSG_DEBUG, "P2P: Drop pending Action TX due to new "
7513 "operation request");
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08007514 offchannel_clear_pending_action_tx(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007515}
7516
7517
7518int wpas_p2p_find(struct wpa_supplicant *wpa_s, unsigned int timeout,
7519 enum p2p_discovery_type type,
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08007520 unsigned int num_req_dev_types, const u8 *req_dev_types,
Dmitry Shmidt216983b2015-02-06 10:50:36 -08007521 const u8 *dev_id, unsigned int search_delay,
Hai Shalomc1a21442022-02-04 13:43:00 -08007522 u8 seek_cnt, const char **seek_string, int freq,
7523 bool include_6ghz)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007524{
7525 wpas_p2p_clear_pending_action_tx(wpa_s);
Hai Shalomfdcde762020-04-02 11:19:20 -07007526 wpa_s->global->p2p_long_listen = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007527
Dmitry Shmidt04949592012-07-19 12:16:46 -07007528 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL ||
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07007529 wpa_s->p2p_in_provisioning) {
7530 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Reject p2p_find operation%s%s",
7531 (wpa_s->global->p2p_disabled || !wpa_s->global->p2p) ?
7532 " (P2P disabled)" : "",
7533 wpa_s->p2p_in_provisioning ?
7534 " (p2p_in_provisioning)" : "");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007535 return -1;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07007536 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007537
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08007538 wpa_supplicant_cancel_sched_scan(wpa_s);
7539
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007540 return p2p_find(wpa_s->global->p2p, timeout, type,
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07007541 num_req_dev_types, req_dev_types, dev_id,
Hai Shalomc1a21442022-02-04 13:43:00 -08007542 search_delay, seek_cnt, seek_string, freq,
7543 include_6ghz);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007544}
7545
7546
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007547static void wpas_p2p_scan_res_ignore_search(struct wpa_supplicant *wpa_s,
7548 struct wpa_scan_results *scan_res)
7549{
7550 wpa_printf(MSG_DEBUG, "P2P: Ignore scan results");
7551
7552 if (wpa_s->p2p_scan_work) {
7553 struct wpa_radio_work *work = wpa_s->p2p_scan_work;
7554 wpa_s->p2p_scan_work = NULL;
7555 radio_work_done(work);
7556 }
7557
7558 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
7559 return;
7560
7561 /*
7562 * Indicate that results have been processed so that the P2P module can
7563 * continue pending tasks.
7564 */
Hai Shalom60840252021-02-19 19:02:11 -08007565 wpas_p2p_scan_res_handled(wpa_s);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007566}
7567
7568
7569static void wpas_p2p_stop_find_oper(struct wpa_supplicant *wpa_s)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007570{
7571 wpas_p2p_clear_pending_action_tx(wpa_s);
Hai Shalomfdcde762020-04-02 11:19:20 -07007572 wpa_s->global->p2p_long_listen = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007573 eloop_cancel_timeout(wpas_p2p_long_listen_timeout, wpa_s, NULL);
7574 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007575
7576 if (wpa_s->global->p2p)
7577 p2p_stop_find(wpa_s->global->p2p);
7578
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007579 if (wpa_s->scan_res_handler == wpas_p2p_scan_res_handler) {
7580 wpa_printf(MSG_DEBUG,
7581 "P2P: Do not consider the scan results after stop_find");
7582 wpa_s->scan_res_handler = wpas_p2p_scan_res_ignore_search;
7583 }
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08007584}
7585
7586
7587void wpas_p2p_stop_find(struct wpa_supplicant *wpa_s)
7588{
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007589 wpas_p2p_stop_find_oper(wpa_s);
Dmitry Shmidt216983b2015-02-06 10:50:36 -08007590 if (!wpa_s->global->pending_group_iface_for_p2ps)
7591 wpas_p2p_remove_pending_group_interface(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007592}
7593
7594
7595static void wpas_p2p_long_listen_timeout(void *eloop_ctx, void *timeout_ctx)
7596{
7597 struct wpa_supplicant *wpa_s = eloop_ctx;
Hai Shalomfdcde762020-04-02 11:19:20 -07007598 wpa_s->global->p2p_long_listen = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007599}
7600
7601
7602int wpas_p2p_listen(struct wpa_supplicant *wpa_s, unsigned int timeout)
7603{
7604 int res;
7605
7606 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
7607 return -1;
7608
Dmitry Shmidt58d12ad2016-07-28 10:07:03 -07007609 if (wpa_s->p2p_lo_started) {
7610 wpa_printf(MSG_DEBUG,
7611 "P2P: Cannot start P2P listen, it is offloaded");
7612 return -1;
7613 }
7614
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08007615 wpa_supplicant_cancel_sched_scan(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007616 wpas_p2p_clear_pending_action_tx(wpa_s);
7617
7618 if (timeout == 0) {
7619 /*
7620 * This is a request for unlimited Listen state. However, at
7621 * least for now, this is mapped to a Listen state for one
7622 * hour.
7623 */
7624 timeout = 3600;
7625 }
7626 eloop_cancel_timeout(wpas_p2p_long_listen_timeout, wpa_s, NULL);
Hai Shalomfdcde762020-04-02 11:19:20 -07007627 wpa_s->global->p2p_long_listen = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007628
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007629 /*
7630 * Stop previous find/listen operation to avoid trying to request a new
7631 * remain-on-channel operation while the driver is still running the
7632 * previous one.
7633 */
7634 if (wpa_s->global->p2p)
7635 p2p_stop_find(wpa_s->global->p2p);
7636
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007637 res = wpas_p2p_listen_start(wpa_s, timeout * 1000);
7638 if (res == 0 && timeout * 1000 > wpa_s->max_remain_on_chan) {
Hai Shalomfdcde762020-04-02 11:19:20 -07007639 wpa_s->global->p2p_long_listen = timeout * 1000;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007640 eloop_register_timeout(timeout, 0,
7641 wpas_p2p_long_listen_timeout,
7642 wpa_s, NULL);
7643 }
7644
7645 return res;
7646}
7647
7648
7649int wpas_p2p_assoc_req_ie(struct wpa_supplicant *wpa_s, struct wpa_bss *bss,
7650 u8 *buf, size_t len, int p2p_group)
7651{
7652 struct wpabuf *p2p_ie;
7653 int ret;
7654
7655 if (wpa_s->global->p2p_disabled)
7656 return -1;
Dmitry Shmidt8bd70b72015-05-26 16:02:19 -07007657 /*
7658 * Advertize mandatory cross connection capability even on
7659 * p2p_disabled=1 interface when associating with a P2P Manager WLAN AP.
7660 */
7661 if (wpa_s->conf->p2p_disabled && p2p_group)
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07007662 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007663 if (wpa_s->global->p2p == NULL)
7664 return -1;
7665 if (bss == NULL)
7666 return -1;
7667
7668 p2p_ie = wpa_bss_get_vendor_ie_multi(bss, P2P_IE_VENDOR_TYPE);
7669 ret = p2p_assoc_req_ie(wpa_s->global->p2p, bss->bssid, buf, len,
7670 p2p_group, p2p_ie);
7671 wpabuf_free(p2p_ie);
7672
7673 return ret;
7674}
7675
7676
7677int wpas_p2p_probe_req_rx(struct wpa_supplicant *wpa_s, const u8 *addr,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007678 const u8 *dst, const u8 *bssid,
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07007679 const u8 *ie, size_t ie_len,
7680 unsigned int rx_freq, int ssi_signal)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007681{
7682 if (wpa_s->global->p2p_disabled)
7683 return 0;
7684 if (wpa_s->global->p2p == NULL)
7685 return 0;
7686
Dmitry Shmidt04949592012-07-19 12:16:46 -07007687 switch (p2p_probe_req_rx(wpa_s->global->p2p, addr, dst, bssid,
Dmitry Shmidt58d12ad2016-07-28 10:07:03 -07007688 ie, ie_len, rx_freq, wpa_s->p2p_lo_started)) {
Dmitry Shmidt04949592012-07-19 12:16:46 -07007689 case P2P_PREQ_NOT_P2P:
7690 wpas_notify_preq(wpa_s, addr, dst, bssid, ie, ie_len,
7691 ssi_signal);
7692 /* fall through */
7693 case P2P_PREQ_MALFORMED:
7694 case P2P_PREQ_NOT_LISTEN:
7695 case P2P_PREQ_NOT_PROCESSED:
7696 default: /* make gcc happy */
7697 return 0;
7698 case P2P_PREQ_PROCESSED:
7699 return 1;
7700 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007701}
7702
7703
7704void wpas_p2p_rx_action(struct wpa_supplicant *wpa_s, const u8 *da,
7705 const u8 *sa, const u8 *bssid,
7706 u8 category, const u8 *data, size_t len, int freq)
7707{
7708 if (wpa_s->global->p2p_disabled)
7709 return;
7710 if (wpa_s->global->p2p == NULL)
7711 return;
7712
7713 p2p_rx_action(wpa_s->global->p2p, da, sa, bssid, category, data, len,
7714 freq);
7715}
7716
7717
7718void wpas_p2p_scan_ie(struct wpa_supplicant *wpa_s, struct wpabuf *ies)
7719{
Dmitry Shmidt9c175262016-03-03 10:20:07 -08007720 unsigned int bands;
7721
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007722 if (wpa_s->global->p2p_disabled)
7723 return;
7724 if (wpa_s->global->p2p == NULL)
7725 return;
7726
Dmitry Shmidt9c175262016-03-03 10:20:07 -08007727 bands = wpas_get_bands(wpa_s, NULL);
7728 p2p_scan_ie(wpa_s->global->p2p, ies, NULL, bands);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007729}
7730
7731
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07007732static void wpas_p2p_group_deinit(struct wpa_supplicant *wpa_s)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007733{
7734 p2p_group_deinit(wpa_s->p2p_group);
7735 wpa_s->p2p_group = NULL;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007736
7737 wpa_s->ap_configured_cb = NULL;
7738 wpa_s->ap_configured_cb_ctx = NULL;
7739 wpa_s->ap_configured_cb_data = NULL;
7740 wpa_s->connect_without_scan = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007741}
7742
7743
7744int wpas_p2p_reject(struct wpa_supplicant *wpa_s, const u8 *addr)
7745{
Hai Shalomfdcde762020-04-02 11:19:20 -07007746 wpa_s->global->p2p_long_listen = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007747
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007748 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
7749 return -1;
7750
7751 return p2p_reject(wpa_s->global->p2p, addr);
7752}
7753
7754
7755/* Invite to reinvoke a persistent group */
7756int wpas_p2p_invite(struct wpa_supplicant *wpa_s, const u8 *peer_addr,
Jouni Malinen31be0a42012-08-31 21:20:51 +03007757 struct wpa_ssid *ssid, const u8 *go_dev_addr, int freq,
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08007758 int vht_center_freq2, int ht40, int vht, int max_chwidth,
Hai Shalomc1a21442022-02-04 13:43:00 -08007759 int pref_freq, int he, int edmg, bool allow_6ghz)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007760{
7761 enum p2p_invite_role role;
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07007762 u8 *bssid = NULL;
7763 int force_freq = 0;
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08007764 int res;
Dmitry Shmidta0d265f2013-11-19 13:13:41 -08007765 int no_pref_freq_given = pref_freq == 0;
Sunil8cd6f4d2022-06-28 18:40:46 +00007766 struct weighted_pcl pref_freq_list[P2P_MAX_PREF_CHANNELS];
7767 unsigned int size;
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08007768
Hai Shalomc1a21442022-02-04 13:43:00 -08007769 if (wpas_p2p_check_6ghz(wpa_s, NULL, allow_6ghz, freq))
7770 return -1;
7771
Dmitry Shmidt700a1372013-03-15 14:14:44 -07007772 wpa_s->global->p2p_invite_group = NULL;
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08007773 if (peer_addr)
7774 os_memcpy(wpa_s->p2p_auth_invite, peer_addr, ETH_ALEN);
7775 else
7776 os_memset(wpa_s->p2p_auth_invite, 0, ETH_ALEN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007777
Jouni Malinen31be0a42012-08-31 21:20:51 +03007778 wpa_s->p2p_persistent_go_freq = freq;
7779 wpa_s->p2p_go_ht40 = !!ht40;
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08007780 wpa_s->p2p_go_vht = !!vht;
Hai Shalom74f70d42019-02-11 14:42:39 -08007781 wpa_s->p2p_go_he = !!he;
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08007782 wpa_s->p2p_go_max_oper_chwidth = max_chwidth;
7783 wpa_s->p2p_go_vht_center_freq2 = vht_center_freq2;
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08007784 wpa_s->p2p_go_edmg = !!edmg;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007785 if (ssid->mode == WPAS_MODE_P2P_GO) {
7786 role = P2P_INVITE_ROLE_GO;
7787 if (peer_addr == NULL) {
7788 wpa_printf(MSG_DEBUG, "P2P: Missing peer "
7789 "address in invitation command");
7790 return -1;
7791 }
7792 if (wpas_p2p_create_iface(wpa_s)) {
7793 if (wpas_p2p_add_group_interface(wpa_s,
7794 WPA_IF_P2P_GO) < 0) {
7795 wpa_printf(MSG_ERROR, "P2P: Failed to "
7796 "allocate a new interface for the "
7797 "group");
7798 return -1;
7799 }
7800 bssid = wpa_s->pending_interface_addr;
Dmitry Shmidt9c175262016-03-03 10:20:07 -08007801 } else if (wpa_s->p2p_mgmt)
7802 bssid = wpa_s->parent->own_addr;
7803 else
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007804 bssid = wpa_s->own_addr;
7805 } else {
7806 role = P2P_INVITE_ROLE_CLIENT;
7807 peer_addr = ssid->bssid;
7808 }
7809 wpa_s->pending_invite_ssid_id = ssid->id;
7810
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08007811 size = P2P_MAX_PREF_CHANNELS;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07007812 res = wpas_p2p_setup_freqs(wpa_s, freq, &force_freq, &pref_freq,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08007813 role == P2P_INVITE_ROLE_GO,
7814 pref_freq_list, &size);
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08007815 if (res)
7816 return res;
Irfan Sheriffaf84a572012-09-22 16:59:30 -07007817
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007818 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
7819 return -1;
7820
Dmitry Shmidt7d56b752015-12-22 10:59:44 -08007821 p2p_set_own_pref_freq_list(wpa_s->global->p2p, pref_freq_list, size);
7822
Dmitry Shmidta0d265f2013-11-19 13:13:41 -08007823 if (wpa_s->parent->conf->p2p_ignore_shared_freq &&
7824 no_pref_freq_given && pref_freq > 0 &&
7825 wpa_s->num_multichan_concurrent > 1 &&
7826 wpas_p2p_num_unused_channels(wpa_s) > 0) {
7827 wpa_printf(MSG_DEBUG, "P2P: Ignore own channel preference %d MHz for invitation due to p2p_ignore_shared_freq=1 configuration",
7828 pref_freq);
7829 pref_freq = 0;
7830 }
7831
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08007832 /*
7833 * Stop any find/listen operations before invitation and possibly
7834 * connection establishment.
7835 */
7836 wpas_p2p_stop_find_oper(wpa_s);
7837
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007838 return p2p_invite(wpa_s->global->p2p, peer_addr, role, bssid,
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08007839 ssid->ssid, ssid->ssid_len, force_freq, go_dev_addr,
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08007840 1, pref_freq, -1);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007841}
7842
7843
7844/* Invite to join an active group */
7845int wpas_p2p_invite_group(struct wpa_supplicant *wpa_s, const char *ifname,
Hai Shalomc1a21442022-02-04 13:43:00 -08007846 const u8 *peer_addr, const u8 *go_dev_addr,
7847 bool allow_6ghz)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007848{
7849 struct wpa_global *global = wpa_s->global;
7850 enum p2p_invite_role role;
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07007851 u8 *bssid = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007852 struct wpa_ssid *ssid;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08007853 int persistent;
Dmitry Shmidt051af732013-10-22 13:52:46 -07007854 int freq = 0, force_freq = 0, pref_freq = 0;
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08007855 int res;
Sunil8cd6f4d2022-06-28 18:40:46 +00007856 struct weighted_pcl pref_freq_list[P2P_MAX_PREF_CHANNELS];
7857 unsigned int size;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007858
Jouni Malinen31be0a42012-08-31 21:20:51 +03007859 wpa_s->p2p_persistent_go_freq = 0;
7860 wpa_s->p2p_go_ht40 = 0;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07007861 wpa_s->p2p_go_vht = 0;
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08007862 wpa_s->p2p_go_vht_center_freq2 = 0;
7863 wpa_s->p2p_go_max_oper_chwidth = 0;
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08007864 wpa_s->p2p_go_edmg = 0;
Jouni Malinen31be0a42012-08-31 21:20:51 +03007865
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007866 for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
7867 if (os_strcmp(wpa_s->ifname, ifname) == 0)
7868 break;
7869 }
7870 if (wpa_s == NULL) {
7871 wpa_printf(MSG_DEBUG, "P2P: Interface '%s' not found", ifname);
7872 return -1;
7873 }
7874
7875 ssid = wpa_s->current_ssid;
7876 if (ssid == NULL) {
7877 wpa_printf(MSG_DEBUG, "P2P: No current SSID to use for "
7878 "invitation");
7879 return -1;
7880 }
7881
Dmitry Shmidt700a1372013-03-15 14:14:44 -07007882 wpa_s->global->p2p_invite_group = wpa_s;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08007883 persistent = ssid->p2p_persistent_group &&
Dmitry Shmidt9c175262016-03-03 10:20:07 -08007884 wpas_p2p_get_persistent(wpa_s->p2pdev, peer_addr,
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08007885 ssid->ssid, ssid->ssid_len);
7886
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007887 if (ssid->mode == WPAS_MODE_P2P_GO) {
7888 role = P2P_INVITE_ROLE_ACTIVE_GO;
7889 bssid = wpa_s->own_addr;
7890 if (go_dev_addr == NULL)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007891 go_dev_addr = wpa_s->global->p2p_dev_addr;
Dmitry Shmidt051af732013-10-22 13:52:46 -07007892 freq = ssid->frequency;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007893 } else {
7894 role = P2P_INVITE_ROLE_CLIENT;
7895 if (wpa_s->wpa_state < WPA_ASSOCIATED) {
7896 wpa_printf(MSG_DEBUG, "P2P: Not associated - cannot "
7897 "invite to current group");
7898 return -1;
7899 }
7900 bssid = wpa_s->bssid;
7901 if (go_dev_addr == NULL &&
7902 !is_zero_ether_addr(wpa_s->go_dev_addr))
7903 go_dev_addr = wpa_s->go_dev_addr;
Dmitry Shmidt051af732013-10-22 13:52:46 -07007904 freq = wpa_s->current_bss ? wpa_s->current_bss->freq :
7905 (int) wpa_s->assoc_freq;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007906 }
Dmitry Shmidt9c175262016-03-03 10:20:07 -08007907 wpa_s->p2pdev->pending_invite_ssid_id = -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007908
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007909 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
7910 return -1;
Hai Shalomc1a21442022-02-04 13:43:00 -08007911 if (wpas_p2p_check_6ghz(wpa_s, peer_addr, allow_6ghz, freq))
7912 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007913
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08007914 size = P2P_MAX_PREF_CHANNELS;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07007915 res = wpas_p2p_setup_freqs(wpa_s, freq, &force_freq, &pref_freq,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08007916 role == P2P_INVITE_ROLE_ACTIVE_GO,
7917 pref_freq_list, &size);
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08007918 if (res)
7919 return res;
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07007920 wpas_p2p_set_own_freq_preference(wpa_s, force_freq);
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08007921
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007922 return p2p_invite(wpa_s->global->p2p, peer_addr, role, bssid,
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08007923 ssid->ssid, ssid->ssid_len, force_freq,
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08007924 go_dev_addr, persistent, pref_freq, -1);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007925}
7926
7927
7928void wpas_p2p_completed(struct wpa_supplicant *wpa_s)
7929{
7930 struct wpa_ssid *ssid = wpa_s->current_ssid;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007931 u8 go_dev_addr[ETH_ALEN];
7932 int persistent;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007933 int freq;
Hai Shalom5f92bc92019-04-18 11:54:11 -07007934 u8 ip[3 * 4], *ip_ptr = NULL;
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08007935 char ip_addr[100];
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007936
Dmitry Shmidt04949592012-07-19 12:16:46 -07007937 if (ssid == NULL || ssid->mode != WPAS_MODE_P2P_GROUP_FORMATION) {
7938 eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
Dmitry Shmidt9c175262016-03-03 10:20:07 -08007939 wpa_s->p2pdev, NULL);
Dmitry Shmidt04949592012-07-19 12:16:46 -07007940 }
7941
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007942 if (!wpa_s->show_group_started || !ssid)
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08007943 return;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007944
7945 wpa_s->show_group_started = 0;
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08007946 if (!wpa_s->p2p_go_group_formation_completed &&
7947 wpa_s->global->p2p_group_formation == wpa_s) {
7948 wpa_dbg(wpa_s, MSG_DEBUG,
7949 "P2P: Marking group formation completed on client on data connection");
7950 wpa_s->p2p_go_group_formation_completed = 1;
7951 wpa_s->global->p2p_group_formation = NULL;
7952 wpa_s->p2p_in_provisioning = 0;
7953 wpa_s->p2p_in_invitation = 0;
Matthew Wang06b42472022-11-10 06:56:31 +00007954 wpa_s->p2p_retry_limit = 0;
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08007955 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007956
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007957 os_memset(go_dev_addr, 0, ETH_ALEN);
7958 if (ssid->bssid_set)
7959 os_memcpy(go_dev_addr, ssid->bssid, ETH_ALEN);
7960 persistent = wpas_p2p_persistent_group(wpa_s, go_dev_addr, ssid->ssid,
7961 ssid->ssid_len);
7962 os_memcpy(wpa_s->go_dev_addr, go_dev_addr, ETH_ALEN);
7963
7964 if (wpa_s->global->p2p_group_formation == wpa_s)
7965 wpa_s->global->p2p_group_formation = NULL;
7966
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007967 freq = wpa_s->current_bss ? wpa_s->current_bss->freq :
7968 (int) wpa_s->assoc_freq;
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08007969
7970 ip_addr[0] = '\0';
7971 if (wpa_sm_get_p2p_ip_addr(wpa_s->wpa, ip) == 0) {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007972 int res;
7973
7974 res = os_snprintf(ip_addr, sizeof(ip_addr),
7975 " ip_addr=%u.%u.%u.%u "
7976 "ip_mask=%u.%u.%u.%u go_ip_addr=%u.%u.%u.%u",
7977 ip[0], ip[1], ip[2], ip[3],
7978 ip[4], ip[5], ip[6], ip[7],
7979 ip[8], ip[9], ip[10], ip[11]);
7980 if (os_snprintf_error(sizeof(ip_addr), res))
7981 ip_addr[0] = '\0';
Hai Shalom5f92bc92019-04-18 11:54:11 -07007982 ip_ptr = ip;
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08007983 }
7984
Dmitry Shmidt7f0b69e2014-07-28 10:35:20 -07007985 wpas_p2p_group_started(wpa_s, 0, ssid, freq,
7986 ssid->passphrase == NULL && ssid->psk_set ?
7987 ssid->psk : NULL,
7988 ssid->passphrase, go_dev_addr, persistent,
7989 ip_addr);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007990
7991 if (persistent)
Dmitry Shmidtd5ab1b52016-06-21 12:38:41 -07007992 wpas_p2p_store_persistent_group(wpa_s->p2pdev,
7993 ssid, go_dev_addr);
7994
Hai Shalom5f92bc92019-04-18 11:54:11 -07007995 wpas_notify_p2p_group_started(wpa_s, ssid, persistent, 1, ip_ptr);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007996}
7997
7998
7999int wpas_p2p_presence_req(struct wpa_supplicant *wpa_s, u32 duration1,
8000 u32 interval1, u32 duration2, u32 interval2)
8001{
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08008002 int ret;
8003
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008004 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
8005 return -1;
8006
8007 if (wpa_s->wpa_state < WPA_ASSOCIATED ||
8008 wpa_s->current_ssid == NULL ||
8009 wpa_s->current_ssid->mode != WPAS_MODE_INFRA)
8010 return -1;
8011
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08008012 ret = p2p_presence_req(wpa_s->global->p2p, wpa_s->bssid,
8013 wpa_s->own_addr, wpa_s->assoc_freq,
8014 duration1, interval1, duration2, interval2);
8015 if (ret == 0)
8016 wpa_s->waiting_presence_resp = 1;
8017
8018 return ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008019}
8020
8021
8022int wpas_p2p_ext_listen(struct wpa_supplicant *wpa_s, unsigned int period,
8023 unsigned int interval)
8024{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008025 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
8026 return -1;
8027
8028 return p2p_ext_listen(wpa_s->global->p2p, period, interval);
8029}
8030
8031
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008032static int wpas_p2p_is_client(struct wpa_supplicant *wpa_s)
8033{
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08008034 if (wpa_s->current_ssid == NULL) {
8035 /*
8036 * current_ssid can be cleared when P2P client interface gets
8037 * disconnected, so assume this interface was used as P2P
8038 * client.
8039 */
8040 return 1;
8041 }
8042 return wpa_s->current_ssid->p2p_group &&
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008043 wpa_s->current_ssid->mode == WPAS_MODE_INFRA;
8044}
8045
8046
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008047static void wpas_p2p_group_idle_timeout(void *eloop_ctx, void *timeout_ctx)
8048{
8049 struct wpa_supplicant *wpa_s = eloop_ctx;
8050
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008051 if (wpa_s->conf->p2p_group_idle == 0 && !wpas_p2p_is_client(wpa_s)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008052 wpa_printf(MSG_DEBUG, "P2P: Ignore group idle timeout - "
8053 "disabled");
8054 return;
8055 }
8056
Dmitry Shmidt04949592012-07-19 12:16:46 -07008057 wpa_printf(MSG_DEBUG, "P2P: Group idle timeout reached - terminate "
8058 "group");
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07008059 wpas_p2p_group_delete(wpa_s, P2P_GROUP_REMOVAL_IDLE_TIMEOUT);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008060}
8061
8062
8063static void wpas_p2p_set_group_idle_timeout(struct wpa_supplicant *wpa_s)
8064{
Dmitry Shmidt04949592012-07-19 12:16:46 -07008065 int timeout;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008066
Dmitry Shmidt04949592012-07-19 12:16:46 -07008067 if (eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL) > 0)
8068 wpa_printf(MSG_DEBUG, "P2P: Cancelled P2P group idle timeout");
8069
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008070 if (wpa_s->current_ssid == NULL || !wpa_s->current_ssid->p2p_group)
8071 return;
8072
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008073 timeout = wpa_s->conf->p2p_group_idle;
8074 if (wpa_s->current_ssid->mode == WPAS_MODE_INFRA &&
8075 (timeout == 0 || timeout > P2P_MAX_CLIENT_IDLE))
8076 timeout = P2P_MAX_CLIENT_IDLE;
8077
8078 if (timeout == 0)
8079 return;
8080
Dmitry Shmidt04949592012-07-19 12:16:46 -07008081 if (timeout < 0) {
8082 if (wpa_s->current_ssid->mode == WPAS_MODE_INFRA)
8083 timeout = 0; /* special client mode no-timeout */
8084 else
8085 return;
8086 }
8087
8088 if (wpa_s->p2p_in_provisioning) {
8089 /*
8090 * Use the normal group formation timeout during the
8091 * provisioning phase to avoid terminating this process too
8092 * early due to group idle timeout.
8093 */
8094 wpa_printf(MSG_DEBUG, "P2P: Do not use P2P group idle timeout "
8095 "during provisioning");
8096 return;
8097 }
Deepthi Gowri9e4e8ac2013-04-16 11:57:05 +05308098
Dmitry Shmidt04949592012-07-19 12:16:46 -07008099 if (wpa_s->show_group_started) {
8100 /*
8101 * Use the normal group formation timeout between the end of
8102 * the provisioning phase and completion of 4-way handshake to
8103 * avoid terminating this process too early due to group idle
8104 * timeout.
8105 */
8106 wpa_printf(MSG_DEBUG, "P2P: Do not use P2P group idle timeout "
8107 "while waiting for initial 4-way handshake to "
8108 "complete");
8109 return;
8110 }
8111
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008112 wpa_printf(MSG_DEBUG, "P2P: Set P2P group idle timeout to %u seconds",
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008113 timeout);
8114 eloop_register_timeout(timeout, 0, wpas_p2p_group_idle_timeout,
8115 wpa_s, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008116}
8117
8118
Jouni Malinen2b89da82012-08-31 22:04:41 +03008119/* Returns 1 if the interface was removed */
8120int wpas_p2p_deauth_notif(struct wpa_supplicant *wpa_s, const u8 *bssid,
8121 u16 reason_code, const u8 *ie, size_t ie_len,
8122 int locally_generated)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008123{
8124 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
Jouni Malinen2b89da82012-08-31 22:04:41 +03008125 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008126
Dmitry Shmidt04949592012-07-19 12:16:46 -07008127 if (!locally_generated)
8128 p2p_deauth_notif(wpa_s->global->p2p, bssid, reason_code, ie,
8129 ie_len);
8130
8131 if (reason_code == WLAN_REASON_DEAUTH_LEAVING && !locally_generated &&
8132 wpa_s->current_ssid &&
8133 wpa_s->current_ssid->p2p_group &&
8134 wpa_s->current_ssid->mode == WPAS_MODE_INFRA) {
8135 wpa_printf(MSG_DEBUG, "P2P: GO indicated that the P2P Group "
8136 "session is ending");
Jouni Malinen2b89da82012-08-31 22:04:41 +03008137 if (wpas_p2p_group_delete(wpa_s,
8138 P2P_GROUP_REMOVAL_GO_ENDING_SESSION)
8139 > 0)
8140 return 1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07008141 }
Jouni Malinen2b89da82012-08-31 22:04:41 +03008142
8143 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008144}
8145
8146
8147void wpas_p2p_disassoc_notif(struct wpa_supplicant *wpa_s, const u8 *bssid,
Dmitry Shmidt04949592012-07-19 12:16:46 -07008148 u16 reason_code, const u8 *ie, size_t ie_len,
8149 int locally_generated)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008150{
8151 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
8152 return;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008153
Dmitry Shmidt04949592012-07-19 12:16:46 -07008154 if (!locally_generated)
8155 p2p_disassoc_notif(wpa_s->global->p2p, bssid, reason_code, ie,
8156 ie_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008157}
8158
8159
8160void wpas_p2p_update_config(struct wpa_supplicant *wpa_s)
8161{
8162 struct p2p_data *p2p = wpa_s->global->p2p;
8163
8164 if (p2p == NULL)
8165 return;
8166
8167 if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_CAPABLE))
8168 return;
8169
8170 if (wpa_s->conf->changed_parameters & CFG_CHANGED_DEVICE_NAME)
8171 p2p_set_dev_name(p2p, wpa_s->conf->device_name);
8172
8173 if (wpa_s->conf->changed_parameters & CFG_CHANGED_DEVICE_TYPE)
8174 p2p_set_pri_dev_type(p2p, wpa_s->conf->device_type);
8175
8176 if (wpa_s->wps &&
8177 (wpa_s->conf->changed_parameters & CFG_CHANGED_CONFIG_METHODS))
8178 p2p_set_config_methods(p2p, wpa_s->wps->config_methods);
8179
8180 if (wpa_s->wps && (wpa_s->conf->changed_parameters & CFG_CHANGED_UUID))
8181 p2p_set_uuid(p2p, wpa_s->wps->uuid);
8182
8183 if (wpa_s->conf->changed_parameters & CFG_CHANGED_WPS_STRING) {
8184 p2p_set_manufacturer(p2p, wpa_s->conf->manufacturer);
8185 p2p_set_model_name(p2p, wpa_s->conf->model_name);
8186 p2p_set_model_number(p2p, wpa_s->conf->model_number);
8187 p2p_set_serial_number(p2p, wpa_s->conf->serial_number);
8188 }
8189
8190 if (wpa_s->conf->changed_parameters & CFG_CHANGED_SEC_DEVICE_TYPE)
8191 p2p_set_sec_dev_types(p2p,
8192 (void *) wpa_s->conf->sec_device_type,
8193 wpa_s->conf->num_sec_device_types);
8194
8195 if (wpa_s->conf->changed_parameters & CFG_CHANGED_VENDOR_EXTENSION) {
8196 int i;
8197 p2p_remove_wps_vendor_extensions(p2p);
8198 for (i = 0; i < MAX_WPS_VENDOR_EXT; i++) {
8199 if (wpa_s->conf->wps_vendor_ext[i] == NULL)
8200 continue;
8201 p2p_add_wps_vendor_extension(
8202 p2p, wpa_s->conf->wps_vendor_ext[i]);
8203 }
8204 }
8205
8206 if ((wpa_s->conf->changed_parameters & CFG_CHANGED_COUNTRY) &&
8207 wpa_s->conf->country[0] && wpa_s->conf->country[1]) {
8208 char country[3];
8209 country[0] = wpa_s->conf->country[0];
8210 country[1] = wpa_s->conf->country[1];
8211 country[2] = 0x04;
8212 p2p_set_country(p2p, country);
8213 }
8214
8215 if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_SSID_POSTFIX) {
8216 p2p_set_ssid_postfix(p2p, (u8 *) wpa_s->conf->p2p_ssid_postfix,
8217 wpa_s->conf->p2p_ssid_postfix ?
8218 os_strlen(wpa_s->conf->p2p_ssid_postfix) :
8219 0);
8220 }
8221
8222 if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_INTRA_BSS)
8223 p2p_set_intra_bss_dist(p2p, wpa_s->conf->p2p_intra_bss);
Jouni Malinen75ecf522011-06-27 15:19:46 -07008224
8225 if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_LISTEN_CHANNEL) {
8226 u8 reg_class, channel;
8227 int ret;
8228 unsigned int r;
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07008229 u8 channel_forced;
8230
Jouni Malinen75ecf522011-06-27 15:19:46 -07008231 if (wpa_s->conf->p2p_listen_reg_class &&
8232 wpa_s->conf->p2p_listen_channel) {
8233 reg_class = wpa_s->conf->p2p_listen_reg_class;
8234 channel = wpa_s->conf->p2p_listen_channel;
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07008235 channel_forced = 1;
Jouni Malinen75ecf522011-06-27 15:19:46 -07008236 } else {
8237 reg_class = 81;
8238 /*
8239 * Pick one of the social channels randomly as the
8240 * listen channel.
8241 */
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07008242 if (os_get_random((u8 *) &r, sizeof(r)) < 0)
8243 channel = 1;
8244 else
8245 channel = 1 + (r % 3) * 5;
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07008246 channel_forced = 0;
Jouni Malinen75ecf522011-06-27 15:19:46 -07008247 }
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07008248 ret = p2p_set_listen_channel(p2p, reg_class, channel,
8249 channel_forced);
Jouni Malinen75ecf522011-06-27 15:19:46 -07008250 if (ret)
8251 wpa_printf(MSG_ERROR, "P2P: Own listen channel update "
8252 "failed: %d", ret);
8253 }
8254 if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_OPER_CHANNEL) {
8255 u8 op_reg_class, op_channel, cfg_op_channel;
8256 int ret = 0;
8257 unsigned int r;
8258 if (wpa_s->conf->p2p_oper_reg_class &&
8259 wpa_s->conf->p2p_oper_channel) {
8260 op_reg_class = wpa_s->conf->p2p_oper_reg_class;
8261 op_channel = wpa_s->conf->p2p_oper_channel;
8262 cfg_op_channel = 1;
8263 } else {
8264 op_reg_class = 81;
8265 /*
8266 * Use random operation channel from (1, 6, 11)
8267 *if no other preference is indicated.
8268 */
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07008269 if (os_get_random((u8 *) &r, sizeof(r)) < 0)
8270 op_channel = 1;
8271 else
8272 op_channel = 1 + (r % 3) * 5;
Jouni Malinen75ecf522011-06-27 15:19:46 -07008273 cfg_op_channel = 0;
8274 }
8275 ret = p2p_set_oper_channel(p2p, op_reg_class, op_channel,
8276 cfg_op_channel);
8277 if (ret)
8278 wpa_printf(MSG_ERROR, "P2P: Own oper channel update "
8279 "failed: %d", ret);
8280 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07008281
8282 if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_PREF_CHAN) {
8283 if (p2p_set_pref_chan(p2p, wpa_s->conf->num_p2p_pref_chan,
8284 wpa_s->conf->p2p_pref_chan) < 0) {
8285 wpa_printf(MSG_ERROR, "P2P: Preferred channel list "
8286 "update failed");
8287 }
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07008288
8289 if (p2p_set_no_go_freq(p2p, &wpa_s->conf->p2p_no_go_freq) < 0) {
8290 wpa_printf(MSG_ERROR, "P2P: No GO channel list "
8291 "update failed");
8292 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07008293 }
Dmitry Shmidt2271d3f2014-06-23 12:16:31 -07008294
8295 if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_PASSPHRASE_LEN)
8296 p2p_set_passphrase_len(p2p, wpa_s->conf->p2p_passphrase_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008297}
8298
8299
8300int wpas_p2p_set_noa(struct wpa_supplicant *wpa_s, u8 count, int start,
8301 int duration)
8302{
8303 if (!wpa_s->ap_iface)
8304 return -1;
8305 return hostapd_p2p_set_noa(wpa_s->ap_iface->bss[0], count, start,
8306 duration);
8307}
8308
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008309
8310int wpas_p2p_set_cross_connect(struct wpa_supplicant *wpa_s, int enabled)
8311{
8312 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
8313 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008314
8315 wpa_s->global->cross_connection = enabled;
8316 p2p_set_cross_connect(wpa_s->global->p2p, enabled);
8317
8318 if (!enabled) {
8319 struct wpa_supplicant *iface;
8320
8321 for (iface = wpa_s->global->ifaces; iface; iface = iface->next)
8322 {
8323 if (iface->cross_connect_enabled == 0)
8324 continue;
8325
8326 iface->cross_connect_enabled = 0;
8327 iface->cross_connect_in_use = 0;
Dmitry Shmidt9c175262016-03-03 10:20:07 -08008328 wpa_msg_global(iface->p2pdev, MSG_INFO,
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07008329 P2P_EVENT_CROSS_CONNECT_DISABLE "%s %s",
8330 iface->ifname,
8331 iface->cross_connect_uplink);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008332 }
8333 }
8334
8335 return 0;
8336}
8337
8338
8339static void wpas_p2p_enable_cross_connect(struct wpa_supplicant *uplink)
8340{
8341 struct wpa_supplicant *iface;
8342
8343 if (!uplink->global->cross_connection)
8344 return;
8345
8346 for (iface = uplink->global->ifaces; iface; iface = iface->next) {
8347 if (!iface->cross_connect_enabled)
8348 continue;
8349 if (os_strcmp(uplink->ifname, iface->cross_connect_uplink) !=
8350 0)
8351 continue;
8352 if (iface->ap_iface == NULL)
8353 continue;
8354 if (iface->cross_connect_in_use)
8355 continue;
8356
8357 iface->cross_connect_in_use = 1;
Dmitry Shmidt9c175262016-03-03 10:20:07 -08008358 wpa_msg_global(iface->p2pdev, MSG_INFO,
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07008359 P2P_EVENT_CROSS_CONNECT_ENABLE "%s %s",
8360 iface->ifname, iface->cross_connect_uplink);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008361 }
8362}
8363
8364
8365static void wpas_p2p_disable_cross_connect(struct wpa_supplicant *uplink)
8366{
8367 struct wpa_supplicant *iface;
8368
8369 for (iface = uplink->global->ifaces; iface; iface = iface->next) {
8370 if (!iface->cross_connect_enabled)
8371 continue;
8372 if (os_strcmp(uplink->ifname, iface->cross_connect_uplink) !=
8373 0)
8374 continue;
8375 if (!iface->cross_connect_in_use)
8376 continue;
8377
Dmitry Shmidt9c175262016-03-03 10:20:07 -08008378 wpa_msg_global(iface->p2pdev, MSG_INFO,
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07008379 P2P_EVENT_CROSS_CONNECT_DISABLE "%s %s",
8380 iface->ifname, iface->cross_connect_uplink);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008381 iface->cross_connect_in_use = 0;
8382 }
8383}
8384
8385
8386void wpas_p2p_notif_connected(struct wpa_supplicant *wpa_s)
8387{
8388 if (wpa_s->ap_iface || wpa_s->current_ssid == NULL ||
8389 wpa_s->current_ssid->mode != WPAS_MODE_INFRA ||
8390 wpa_s->cross_connect_disallowed)
8391 wpas_p2p_disable_cross_connect(wpa_s);
8392 else
8393 wpas_p2p_enable_cross_connect(wpa_s);
Dmitry Shmidt04949592012-07-19 12:16:46 -07008394 if (!wpa_s->ap_iface &&
8395 eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL) > 0)
8396 wpa_printf(MSG_DEBUG, "P2P: Cancelled P2P group idle timeout");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008397}
8398
8399
8400void wpas_p2p_notif_disconnected(struct wpa_supplicant *wpa_s)
8401{
8402 wpas_p2p_disable_cross_connect(wpa_s);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008403 if (!wpa_s->ap_iface &&
8404 !eloop_is_timeout_registered(wpas_p2p_group_idle_timeout,
8405 wpa_s, NULL))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008406 wpas_p2p_set_group_idle_timeout(wpa_s);
8407}
8408
8409
8410static void wpas_p2p_cross_connect_setup(struct wpa_supplicant *wpa_s)
8411{
8412 struct wpa_supplicant *iface;
8413
8414 if (!wpa_s->global->cross_connection)
8415 return;
8416
8417 for (iface = wpa_s->global->ifaces; iface; iface = iface->next) {
8418 if (iface == wpa_s)
8419 continue;
8420 if (iface->drv_flags &
8421 WPA_DRIVER_FLAGS_P2P_DEDICATED_INTERFACE)
8422 continue;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08008423 if ((iface->drv_flags & WPA_DRIVER_FLAGS_P2P_CAPABLE) &&
8424 iface != wpa_s->parent)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008425 continue;
8426
8427 wpa_s->cross_connect_enabled = 1;
8428 os_strlcpy(wpa_s->cross_connect_uplink, iface->ifname,
8429 sizeof(wpa_s->cross_connect_uplink));
8430 wpa_printf(MSG_DEBUG, "P2P: Enable cross connection from "
8431 "%s to %s whenever uplink is available",
8432 wpa_s->ifname, wpa_s->cross_connect_uplink);
8433
8434 if (iface->ap_iface || iface->current_ssid == NULL ||
8435 iface->current_ssid->mode != WPAS_MODE_INFRA ||
8436 iface->cross_connect_disallowed ||
8437 iface->wpa_state != WPA_COMPLETED)
8438 break;
8439
8440 wpa_s->cross_connect_in_use = 1;
Dmitry Shmidt9c175262016-03-03 10:20:07 -08008441 wpa_msg_global(wpa_s->p2pdev, MSG_INFO,
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07008442 P2P_EVENT_CROSS_CONNECT_ENABLE "%s %s",
8443 wpa_s->ifname, wpa_s->cross_connect_uplink);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008444 break;
8445 }
8446}
8447
8448
Sunil Ravi036cec52023-03-29 11:35:17 -07008449static int wpas_p2p_notif_pbc_overlap(struct wpa_supplicant *wpa_s)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008450{
8451 if (wpa_s->p2p_group_interface != P2P_GROUP_INTERFACE_CLIENT &&
8452 !wpa_s->p2p_in_provisioning)
8453 return 0; /* not P2P client operation */
8454
8455 wpa_printf(MSG_DEBUG, "P2P: Terminate connection due to WPS PBC "
8456 "session overlap");
Dmitry Shmidt9c175262016-03-03 10:20:07 -08008457 if (wpa_s != wpa_s->p2pdev)
8458 wpa_msg_ctrl(wpa_s->p2pdev, MSG_INFO, WPS_EVENT_OVERLAP);
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07008459 wpas_p2p_group_formation_failed(wpa_s, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008460 return 1;
8461}
8462
8463
Dmitry Shmidtdf5a7e42014-04-02 12:59:59 -07008464void wpas_p2p_pbc_overlap_cb(void *eloop_ctx, void *timeout_ctx)
8465{
8466 struct wpa_supplicant *wpa_s = eloop_ctx;
8467 wpas_p2p_notif_pbc_overlap(wpa_s);
8468}
8469
8470
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08008471void wpas_p2p_update_channel_list(struct wpa_supplicant *wpa_s,
8472 enum wpas_p2p_channel_update_trig trig)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008473{
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07008474 struct p2p_channels chan, cli_chan;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08008475 struct wpa_used_freq_data *freqs = NULL;
8476 unsigned int num = wpa_s->num_multichan_concurrent;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008477
8478 if (wpa_s->global == NULL || wpa_s->global->p2p == NULL)
8479 return;
8480
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08008481 freqs = os_calloc(num, sizeof(struct wpa_used_freq_data));
8482 if (!freqs)
8483 return;
8484
Sunil Ravi77d572f2023-01-17 23:58:31 +00008485 num = get_shared_radio_freqs_data(wpa_s, freqs, num, false);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08008486
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008487 os_memset(&chan, 0, sizeof(chan));
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07008488 os_memset(&cli_chan, 0, sizeof(cli_chan));
Hai Shalom60840252021-02-19 19:02:11 -08008489 if (wpas_p2p_setup_channels(wpa_s, &chan, &cli_chan,
8490 is_p2p_6ghz_disabled(wpa_s->global->p2p))) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008491 wpa_printf(MSG_ERROR, "P2P: Failed to update supported "
8492 "channel list");
8493 return;
8494 }
8495
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07008496 p2p_update_channel_list(wpa_s->global->p2p, &chan, &cli_chan);
Dmitry Shmidt684785c2014-05-12 13:34:29 -07008497
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08008498 wpas_p2p_optimize_listen_channel(wpa_s, freqs, num);
Dmitry Shmidt684785c2014-05-12 13:34:29 -07008499
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08008500 /*
8501 * The used frequencies map changed, so it is possible that a GO is
8502 * using a channel that is no longer valid for P2P use. It is also
8503 * possible that due to policy consideration, it would be preferable to
8504 * move it to a frequency already used by other station interfaces.
8505 */
8506 wpas_p2p_consider_moving_gos(wpa_s, freqs, num, trig);
8507
8508 os_free(freqs);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008509}
8510
8511
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08008512static void wpas_p2p_scan_res_ignore(struct wpa_supplicant *wpa_s,
8513 struct wpa_scan_results *scan_res)
8514{
8515 wpa_printf(MSG_DEBUG, "P2P: Ignore scan results");
8516}
8517
8518
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008519int wpas_p2p_cancel(struct wpa_supplicant *wpa_s)
8520{
8521 struct wpa_global *global = wpa_s->global;
8522 int found = 0;
8523 const u8 *peer;
8524
8525 if (global->p2p == NULL)
8526 return -1;
8527
8528 wpa_printf(MSG_DEBUG, "P2P: Request to cancel group formation");
8529
8530 if (wpa_s->pending_interface_name[0] &&
8531 !is_zero_ether_addr(wpa_s->pending_interface_addr))
8532 found = 1;
8533
8534 peer = p2p_get_go_neg_peer(global->p2p);
8535 if (peer) {
8536 wpa_printf(MSG_DEBUG, "P2P: Unauthorize pending GO Neg peer "
8537 MACSTR, MAC2STR(peer));
8538 p2p_unauthorize(global->p2p, peer);
Dmitry Shmidt04949592012-07-19 12:16:46 -07008539 found = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008540 }
8541
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08008542 if (wpa_s->scan_res_handler == wpas_p2p_scan_res_join) {
8543 wpa_printf(MSG_DEBUG, "P2P: Stop pending scan for join");
8544 wpa_s->scan_res_handler = wpas_p2p_scan_res_ignore;
8545 found = 1;
8546 }
8547
8548 if (wpa_s->pending_pd_before_join) {
8549 wpa_printf(MSG_DEBUG, "P2P: Stop pending PD before join");
8550 wpa_s->pending_pd_before_join = 0;
8551 found = 1;
8552 }
8553
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008554 wpas_p2p_stop_find(wpa_s);
8555
8556 for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
8557 if (wpa_s == global->p2p_group_formation &&
8558 (wpa_s->p2p_in_provisioning ||
8559 wpa_s->parent->pending_interface_type ==
8560 WPA_IF_P2P_CLIENT)) {
8561 wpa_printf(MSG_DEBUG, "P2P: Interface %s in group "
8562 "formation found - cancelling",
8563 wpa_s->ifname);
8564 found = 1;
8565 eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
Dmitry Shmidt9c175262016-03-03 10:20:07 -08008566 wpa_s->p2pdev, NULL);
Jouni Malinenadddfc42012-10-03 14:31:41 -07008567 if (wpa_s->p2p_in_provisioning) {
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07008568 wpas_group_formation_completed(wpa_s, 0, 0);
Jouni Malinenadddfc42012-10-03 14:31:41 -07008569 break;
8570 }
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07008571 wpas_p2p_group_delete(wpa_s,
8572 P2P_GROUP_REMOVAL_REQUESTED);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008573 break;
Dmitry Shmidt21de2142014-04-08 10:50:52 -07008574 } else if (wpa_s->p2p_in_invitation) {
8575 wpa_printf(MSG_DEBUG, "P2P: Interface %s in invitation found - cancelling",
8576 wpa_s->ifname);
8577 found = 1;
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07008578 wpas_p2p_group_formation_failed(wpa_s, 0);
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08008579 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008580 }
8581 }
8582
8583 if (!found) {
8584 wpa_printf(MSG_DEBUG, "P2P: No ongoing group formation found");
8585 return -1;
8586 }
8587
8588 return 0;
8589}
8590
8591
8592void wpas_p2p_interface_unavailable(struct wpa_supplicant *wpa_s)
8593{
8594 if (wpa_s->current_ssid == NULL || !wpa_s->current_ssid->p2p_group)
8595 return;
8596
8597 wpa_printf(MSG_DEBUG, "P2P: Remove group due to driver resource not "
8598 "being available anymore");
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07008599 wpas_p2p_group_delete(wpa_s, P2P_GROUP_REMOVAL_UNAVAILABLE);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008600}
8601
8602
8603void wpas_p2p_update_best_channels(struct wpa_supplicant *wpa_s,
8604 int freq_24, int freq_5, int freq_overall)
8605{
8606 struct p2p_data *p2p = wpa_s->global->p2p;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08008607 if (p2p == NULL)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008608 return;
8609 p2p_set_best_channels(p2p, freq_24, freq_5, freq_overall);
8610}
8611
8612
8613int wpas_p2p_unauthorize(struct wpa_supplicant *wpa_s, const char *addr)
8614{
8615 u8 peer[ETH_ALEN];
8616 struct p2p_data *p2p = wpa_s->global->p2p;
8617
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08008618 if (p2p == NULL)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008619 return -1;
8620
8621 if (hwaddr_aton(addr, peer))
8622 return -1;
8623
8624 return p2p_unauthorize(p2p, peer);
8625}
8626
8627
8628/**
8629 * wpas_p2p_disconnect - Disconnect from a P2P Group
8630 * @wpa_s: Pointer to wpa_supplicant data
8631 * Returns: 0 on success, -1 on failure
8632 *
8633 * This can be used to disconnect from a group in which the local end is a P2P
8634 * Client or to end a P2P Group in case the local end is the Group Owner. If a
8635 * virtual network interface was created for this group, that interface will be
8636 * removed. Otherwise, only the configured P2P group network will be removed
8637 * from the interface.
8638 */
8639int wpas_p2p_disconnect(struct wpa_supplicant *wpa_s)
8640{
8641
8642 if (wpa_s == NULL)
8643 return -1;
8644
Jouni Malinen2b89da82012-08-31 22:04:41 +03008645 return wpas_p2p_group_delete(wpa_s, P2P_GROUP_REMOVAL_REQUESTED) < 0 ?
8646 -1 : 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008647}
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008648
8649
8650int wpas_p2p_in_progress(struct wpa_supplicant *wpa_s)
8651{
Dmitry Shmidtf8623282013-02-20 14:34:59 -08008652 int ret;
8653
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008654 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
8655 return 0;
8656
Dmitry Shmidtf8623282013-02-20 14:34:59 -08008657 ret = p2p_in_progress(wpa_s->global->p2p);
8658 if (ret == 0) {
8659 /*
8660 * Check whether there is an ongoing WPS provisioning step (or
8661 * other parts of group formation) on another interface since
8662 * p2p_in_progress() does not report this to avoid issues for
8663 * scans during such provisioning step.
8664 */
8665 if (wpa_s->global->p2p_group_formation &&
8666 wpa_s->global->p2p_group_formation != wpa_s) {
8667 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Another interface (%s) "
8668 "in group formation",
8669 wpa_s->global->p2p_group_formation->ifname);
8670 ret = 1;
Sunil Ravi77d572f2023-01-17 23:58:31 +00008671 } else if (wpa_s->global->p2p_group_formation == wpa_s) {
8672 wpa_dbg(wpa_s, MSG_DEBUG,
8673 "P2P: Skip Extended Listen timeout and allow scans on current interface for group formation");
8674 ret = 2;
Dmitry Shmidtf8623282013-02-20 14:34:59 -08008675 }
8676 }
8677
Dmitry Shmidt92c368d2013-08-29 12:37:21 -07008678 if (!ret && wpa_s->global->p2p_go_wait_client.sec) {
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08008679 struct os_reltime now;
8680 os_get_reltime(&now);
8681 if (os_reltime_expired(&now, &wpa_s->global->p2p_go_wait_client,
8682 P2P_MAX_INITIAL_CONN_WAIT_GO)) {
Dmitry Shmidt92c368d2013-08-29 12:37:21 -07008683 /* Wait for the first client has expired */
8684 wpa_s->global->p2p_go_wait_client.sec = 0;
8685 } else {
8686 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Waiting for initial client connection during group formation");
8687 ret = 1;
8688 }
8689 }
8690
Dmitry Shmidtf8623282013-02-20 14:34:59 -08008691 return ret;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008692}
8693
8694
8695void wpas_p2p_network_removed(struct wpa_supplicant *wpa_s,
8696 struct wpa_ssid *ssid)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008697{
8698 if (wpa_s->p2p_in_provisioning && ssid->p2p_group &&
8699 eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
Dmitry Shmidt9c175262016-03-03 10:20:07 -08008700 wpa_s->p2pdev, NULL) > 0) {
Dmitry Shmidt04949592012-07-19 12:16:46 -07008701 /**
8702 * Remove the network by scheduling the group formation
8703 * timeout to happen immediately. The teardown code
8704 * needs to be scheduled to run asynch later so that we
8705 * don't delete data from under ourselves unexpectedly.
8706 * Calling wpas_p2p_group_formation_timeout directly
8707 * causes a series of crashes in WPS failure scenarios.
8708 */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008709 wpa_printf(MSG_DEBUG, "P2P: Canceled group formation due to "
8710 "P2P group network getting removed");
Dmitry Shmidt04949592012-07-19 12:16:46 -07008711 eloop_register_timeout(0, 0, wpas_p2p_group_formation_timeout,
Dmitry Shmidt9c175262016-03-03 10:20:07 -08008712 wpa_s->p2pdev, NULL);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008713 }
8714}
8715
8716
8717struct wpa_ssid * wpas_p2p_get_persistent(struct wpa_supplicant *wpa_s,
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08008718 const u8 *addr, const u8 *ssid,
8719 size_t ssid_len)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008720{
8721 struct wpa_ssid *s;
8722 size_t i;
8723
8724 for (s = wpa_s->conf->ssid; s; s = s->next) {
8725 if (s->disabled != 2)
8726 continue;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08008727 if (ssid &&
8728 (ssid_len != s->ssid_len ||
8729 os_memcmp(ssid, s->ssid, ssid_len) != 0))
8730 continue;
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07008731 if (addr == NULL) {
8732 if (s->mode == WPAS_MODE_P2P_GO)
8733 return s;
8734 continue;
8735 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008736 if (os_memcmp(s->bssid, addr, ETH_ALEN) == 0)
8737 return s; /* peer is GO in the persistent group */
8738 if (s->mode != WPAS_MODE_P2P_GO || s->p2p_client_list == NULL)
8739 continue;
8740 for (i = 0; i < s->num_p2p_clients; i++) {
Dmitry Shmidtff787d52015-01-12 13:01:47 -08008741 if (os_memcmp(s->p2p_client_list + i * 2 * ETH_ALEN,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008742 addr, ETH_ALEN) == 0)
8743 return s; /* peer is P2P client in persistent
8744 * group */
8745 }
8746 }
8747
8748 return NULL;
8749}
8750
8751
8752void wpas_p2p_notify_ap_sta_authorized(struct wpa_supplicant *wpa_s,
8753 const u8 *addr)
8754{
Dmitry Shmidt56052862013-10-04 10:23:25 -07008755 if (eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
Dmitry Shmidt9c175262016-03-03 10:20:07 -08008756 wpa_s->p2pdev, NULL) > 0) {
Dmitry Shmidt56052862013-10-04 10:23:25 -07008757 /*
8758 * This can happen if WPS provisioning step is not terminated
8759 * cleanly (e.g., P2P Client does not send WSC_Done). Since the
8760 * peer was able to connect, there is no need to time out group
8761 * formation after this, though. In addition, this is used with
8762 * the initial connection wait on the GO as a separate formation
8763 * timeout and as such, expected to be hit after the initial WPS
8764 * provisioning step.
8765 */
8766 wpa_printf(MSG_DEBUG, "P2P: Canceled P2P group formation timeout on data connection");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08008767
8768 if (!wpa_s->p2p_go_group_formation_completed &&
8769 !wpa_s->group_formation_reported) {
8770 /*
8771 * GO has not yet notified group formation success since
8772 * the WPS step was not completed cleanly. Do that
8773 * notification now since the P2P Client was able to
8774 * connect and as such, must have received the
8775 * credential from the WPS step.
8776 */
8777 if (wpa_s->global->p2p)
8778 p2p_wps_success_cb(wpa_s->global->p2p, addr);
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07008779 wpas_group_formation_completed(wpa_s, 1, 0);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08008780 }
Dmitry Shmidt56052862013-10-04 10:23:25 -07008781 }
8782 if (!wpa_s->p2p_go_group_formation_completed) {
8783 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Marking group formation completed on GO on first data connection");
8784 wpa_s->p2p_go_group_formation_completed = 1;
8785 wpa_s->global->p2p_group_formation = NULL;
8786 wpa_s->p2p_in_provisioning = 0;
Dmitry Shmidt21de2142014-04-08 10:50:52 -07008787 wpa_s->p2p_in_invitation = 0;
Matthew Wang06b42472022-11-10 06:56:31 +00008788 wpa_s->p2p_retry_limit = 0;
Dmitry Shmidt56052862013-10-04 10:23:25 -07008789 }
Dmitry Shmidt92c368d2013-08-29 12:37:21 -07008790 wpa_s->global->p2p_go_wait_client.sec = 0;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008791 if (addr == NULL)
8792 return;
8793 wpas_p2p_add_persistent_group_client(wpa_s, addr);
8794}
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08008795
Dmitry Shmidt04949592012-07-19 12:16:46 -07008796
Dmitry Shmidt7f656022015-02-25 14:36:37 -08008797static int wpas_p2p_fallback_to_go_neg(struct wpa_supplicant *wpa_s,
8798 int group_added)
Dmitry Shmidt04949592012-07-19 12:16:46 -07008799{
8800 struct wpa_supplicant *group = wpa_s;
Dmitry Shmidt7f656022015-02-25 14:36:37 -08008801 int ret = 0;
8802
Dmitry Shmidt04949592012-07-19 12:16:46 -07008803 if (wpa_s->global->p2p_group_formation)
8804 group = wpa_s->global->p2p_group_formation;
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07008805 wpa_s = wpa_s->global->p2p_init_wpa_s;
Dmitry Shmidt04949592012-07-19 12:16:46 -07008806 offchannel_send_action_done(wpa_s);
8807 if (group_added)
Dmitry Shmidt7f656022015-02-25 14:36:37 -08008808 ret = wpas_p2p_group_delete(group, P2P_GROUP_REMOVAL_SILENT);
Dmitry Shmidt04949592012-07-19 12:16:46 -07008809 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Fall back to GO Negotiation");
8810 wpas_p2p_connect(wpa_s, wpa_s->pending_join_dev_addr, wpa_s->p2p_pin,
8811 wpa_s->p2p_wps_method, wpa_s->p2p_persistent_group, 0,
8812 0, 0, wpa_s->p2p_go_intent, wpa_s->p2p_connect_freq,
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08008813 wpa_s->p2p_go_vht_center_freq2,
Dmitry Shmidt04949592012-07-19 12:16:46 -07008814 wpa_s->p2p_persistent_id,
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07008815 wpa_s->p2p_pd_before_go_neg,
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07008816 wpa_s->p2p_go_ht40,
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08008817 wpa_s->p2p_go_vht,
Hai Shalom74f70d42019-02-11 14:42:39 -08008818 wpa_s->p2p_go_max_oper_chwidth,
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08008819 wpa_s->p2p_go_he,
8820 wpa_s->p2p_go_edmg,
Hai Shalomc1a21442022-02-04 13:43:00 -08008821 NULL, 0, is_p2p_allow_6ghz(wpa_s->global->p2p));
Dmitry Shmidt7f656022015-02-25 14:36:37 -08008822 return ret;
Dmitry Shmidt04949592012-07-19 12:16:46 -07008823}
8824
8825
8826int wpas_p2p_scan_no_go_seen(struct wpa_supplicant *wpa_s)
8827{
Dmitry Shmidt7f656022015-02-25 14:36:37 -08008828 int res;
8829
Dmitry Shmidt04949592012-07-19 12:16:46 -07008830 if (!wpa_s->p2p_fallback_to_go_neg ||
8831 wpa_s->p2p_in_provisioning <= 5)
8832 return 0;
8833
8834 if (wpas_p2p_peer_go(wpa_s, wpa_s->pending_join_dev_addr) > 0)
8835 return 0; /* peer operating as a GO */
8836
8837 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: GO not found for p2p_connect-auto - "
8838 "fallback to GO Negotiation");
Dmitry Shmidt9c175262016-03-03 10:20:07 -08008839 wpa_msg_global(wpa_s->p2pdev, MSG_INFO, P2P_EVENT_FALLBACK_TO_GO_NEG
Dmitry Shmidt7f656022015-02-25 14:36:37 -08008840 "reason=GO-not-found");
8841 res = wpas_p2p_fallback_to_go_neg(wpa_s, 1);
Dmitry Shmidt04949592012-07-19 12:16:46 -07008842
Dmitry Shmidt7f656022015-02-25 14:36:37 -08008843 return res == 1 ? 2 : 1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07008844}
8845
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07008846
8847unsigned int wpas_p2p_search_delay(struct wpa_supplicant *wpa_s)
8848{
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07008849 struct wpa_supplicant *ifs;
8850
8851 if (wpa_s->wpa_state > WPA_SCANNING) {
8852 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Use %u ms search delay due to "
8853 "concurrent operation",
Dmitry Shmidt09f57ba2014-06-10 16:07:13 -07008854 wpa_s->conf->p2p_search_delay);
8855 return wpa_s->conf->p2p_search_delay;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07008856 }
8857
Dmitry Shmidt01904cf2013-12-05 11:08:35 -08008858 dl_list_for_each(ifs, &wpa_s->radio->ifaces, struct wpa_supplicant,
8859 radio_list) {
8860 if (ifs != wpa_s && ifs->wpa_state > WPA_SCANNING) {
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07008861 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Use %u ms search "
8862 "delay due to concurrent operation on "
8863 "interface %s",
Dmitry Shmidt09f57ba2014-06-10 16:07:13 -07008864 wpa_s->conf->p2p_search_delay,
8865 ifs->ifname);
8866 return wpa_s->conf->p2p_search_delay;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07008867 }
8868 }
8869
8870 return 0;
8871}
8872
Dmitry Shmidt37d4d6a2013-03-18 13:09:42 -07008873
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07008874static int wpas_p2p_remove_psk_entry(struct wpa_supplicant *wpa_s,
8875 struct wpa_ssid *s, const u8 *addr,
8876 int iface_addr)
8877{
8878 struct psk_list_entry *psk, *tmp;
8879 int changed = 0;
8880
8881 dl_list_for_each_safe(psk, tmp, &s->psk_list, struct psk_list_entry,
8882 list) {
8883 if ((iface_addr && !psk->p2p &&
8884 os_memcmp(addr, psk->addr, ETH_ALEN) == 0) ||
8885 (!iface_addr && psk->p2p &&
8886 os_memcmp(addr, psk->addr, ETH_ALEN) == 0)) {
8887 wpa_dbg(wpa_s, MSG_DEBUG,
8888 "P2P: Remove persistent group PSK list entry for "
8889 MACSTR " p2p=%u",
8890 MAC2STR(psk->addr), psk->p2p);
8891 dl_list_del(&psk->list);
8892 os_free(psk);
8893 changed++;
8894 }
8895 }
8896
8897 return changed;
8898}
8899
8900
8901void wpas_p2p_new_psk_cb(struct wpa_supplicant *wpa_s, const u8 *mac_addr,
8902 const u8 *p2p_dev_addr,
8903 const u8 *psk, size_t psk_len)
8904{
8905 struct wpa_ssid *ssid = wpa_s->current_ssid;
8906 struct wpa_ssid *persistent;
Dmitry Shmidt7832adb2014-04-29 10:53:02 -07008907 struct psk_list_entry *p, *last;
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07008908
8909 if (psk_len != sizeof(p->psk))
8910 return;
8911
8912 if (p2p_dev_addr) {
8913 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: New PSK for addr=" MACSTR
8914 " p2p_dev_addr=" MACSTR,
8915 MAC2STR(mac_addr), MAC2STR(p2p_dev_addr));
8916 if (is_zero_ether_addr(p2p_dev_addr))
8917 p2p_dev_addr = NULL;
8918 } else {
8919 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: New PSK for addr=" MACSTR,
8920 MAC2STR(mac_addr));
8921 }
8922
8923 if (ssid->mode == WPAS_MODE_P2P_GROUP_FORMATION) {
8924 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: new_psk_cb during group formation");
8925 /* To be added to persistent group once created */
8926 if (wpa_s->global->add_psk == NULL) {
8927 wpa_s->global->add_psk = os_zalloc(sizeof(*p));
8928 if (wpa_s->global->add_psk == NULL)
8929 return;
8930 }
8931 p = wpa_s->global->add_psk;
8932 if (p2p_dev_addr) {
8933 p->p2p = 1;
8934 os_memcpy(p->addr, p2p_dev_addr, ETH_ALEN);
8935 } else {
8936 p->p2p = 0;
8937 os_memcpy(p->addr, mac_addr, ETH_ALEN);
8938 }
8939 os_memcpy(p->psk, psk, psk_len);
8940 return;
8941 }
8942
8943 if (ssid->mode != WPAS_MODE_P2P_GO || !ssid->p2p_persistent_group) {
8944 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Ignore new_psk_cb on not-persistent GO");
8945 return;
8946 }
8947
Dmitry Shmidt9c175262016-03-03 10:20:07 -08008948 persistent = wpas_p2p_get_persistent(wpa_s->p2pdev, NULL, ssid->ssid,
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07008949 ssid->ssid_len);
8950 if (!persistent) {
8951 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Could not find persistent group information to store the new PSK");
8952 return;
8953 }
8954
8955 p = os_zalloc(sizeof(*p));
8956 if (p == NULL)
8957 return;
8958 if (p2p_dev_addr) {
8959 p->p2p = 1;
8960 os_memcpy(p->addr, p2p_dev_addr, ETH_ALEN);
8961 } else {
8962 p->p2p = 0;
8963 os_memcpy(p->addr, mac_addr, ETH_ALEN);
8964 }
8965 os_memcpy(p->psk, psk, psk_len);
8966
Dmitry Shmidt7832adb2014-04-29 10:53:02 -07008967 if (dl_list_len(&persistent->psk_list) > P2P_MAX_STORED_CLIENTS &&
8968 (last = dl_list_last(&persistent->psk_list,
8969 struct psk_list_entry, list))) {
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07008970 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Remove oldest PSK entry for "
8971 MACSTR " (p2p=%u) to make room for a new one",
8972 MAC2STR(last->addr), last->p2p);
8973 dl_list_del(&last->list);
8974 os_free(last);
8975 }
8976
Dmitry Shmidt9c175262016-03-03 10:20:07 -08008977 wpas_p2p_remove_psk_entry(wpa_s->p2pdev, persistent,
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07008978 p2p_dev_addr ? p2p_dev_addr : mac_addr,
8979 p2p_dev_addr == NULL);
8980 if (p2p_dev_addr) {
8981 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Add new PSK for p2p_dev_addr="
8982 MACSTR, MAC2STR(p2p_dev_addr));
8983 } else {
8984 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Add new PSK for addr=" MACSTR,
8985 MAC2STR(mac_addr));
8986 }
8987 dl_list_add(&persistent->psk_list, &p->list);
8988
Dmitry Shmidt9c175262016-03-03 10:20:07 -08008989 if (wpa_s->p2pdev->conf->update_config &&
8990 wpa_config_write(wpa_s->p2pdev->confname, wpa_s->p2pdev->conf))
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07008991 wpa_printf(MSG_DEBUG, "P2P: Failed to update configuration");
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07008992}
8993
8994
8995static void wpas_p2p_remove_psk(struct wpa_supplicant *wpa_s,
8996 struct wpa_ssid *s, const u8 *addr,
8997 int iface_addr)
8998{
8999 int res;
9000
9001 res = wpas_p2p_remove_psk_entry(wpa_s, s, addr, iface_addr);
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08009002 if (res > 0 && wpa_s->conf->update_config &&
9003 wpa_config_write(wpa_s->confname, wpa_s->conf))
9004 wpa_dbg(wpa_s, MSG_DEBUG,
9005 "P2P: Failed to update configuration");
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07009006}
9007
9008
9009static void wpas_p2p_remove_client_go(struct wpa_supplicant *wpa_s,
9010 const u8 *peer, int iface_addr)
9011{
9012 struct hostapd_data *hapd;
9013 struct hostapd_wpa_psk *psk, *prev, *rem;
9014 struct sta_info *sta;
9015
9016 if (wpa_s->ap_iface == NULL || wpa_s->current_ssid == NULL ||
9017 wpa_s->current_ssid->mode != WPAS_MODE_P2P_GO)
9018 return;
9019
9020 /* Remove per-station PSK entry */
9021 hapd = wpa_s->ap_iface->bss[0];
9022 prev = NULL;
9023 psk = hapd->conf->ssid.wpa_psk;
9024 while (psk) {
9025 if ((iface_addr && os_memcmp(peer, psk->addr, ETH_ALEN) == 0) ||
9026 (!iface_addr &&
9027 os_memcmp(peer, psk->p2p_dev_addr, ETH_ALEN) == 0)) {
9028 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Remove operating group PSK entry for "
9029 MACSTR " iface_addr=%d",
9030 MAC2STR(peer), iface_addr);
9031 if (prev)
9032 prev->next = psk->next;
9033 else
9034 hapd->conf->ssid.wpa_psk = psk->next;
9035 rem = psk;
9036 psk = psk->next;
Sunil Ravia04bd252022-05-02 22:54:18 -07009037 bin_clear_free(rem, sizeof(*rem));
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07009038 } else {
9039 prev = psk;
9040 psk = psk->next;
9041 }
9042 }
9043
9044 /* Disconnect from group */
9045 if (iface_addr)
9046 sta = ap_get_sta(hapd, peer);
9047 else
9048 sta = ap_get_sta_p2p(hapd, peer);
9049 if (sta) {
9050 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Disconnect peer " MACSTR
9051 " (iface_addr=%d) from group",
9052 MAC2STR(peer), iface_addr);
9053 hostapd_drv_sta_deauth(hapd, sta->addr,
9054 WLAN_REASON_DEAUTH_LEAVING);
9055 ap_sta_deauthenticate(hapd, sta, WLAN_REASON_DEAUTH_LEAVING);
9056 }
9057}
9058
9059
9060void wpas_p2p_remove_client(struct wpa_supplicant *wpa_s, const u8 *peer,
9061 int iface_addr)
9062{
9063 struct wpa_ssid *s;
9064 struct wpa_supplicant *w;
Dmitry Shmidt8bd70b72015-05-26 16:02:19 -07009065 struct wpa_supplicant *p2p_wpa_s = wpa_s->global->p2p_init_wpa_s;
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07009066
9067 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Remove client " MACSTR, MAC2STR(peer));
9068
9069 /* Remove from any persistent group */
Dmitry Shmidt8bd70b72015-05-26 16:02:19 -07009070 for (s = p2p_wpa_s->conf->ssid; s; s = s->next) {
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07009071 if (s->disabled != 2 || s->mode != WPAS_MODE_P2P_GO)
9072 continue;
9073 if (!iface_addr)
Dmitry Shmidt8bd70b72015-05-26 16:02:19 -07009074 wpas_remove_persistent_peer(p2p_wpa_s, s, peer, 0);
9075 wpas_p2p_remove_psk(p2p_wpa_s, s, peer, iface_addr);
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07009076 }
9077
9078 /* Remove from any operating group */
9079 for (w = wpa_s->global->ifaces; w; w = w->next)
9080 wpas_p2p_remove_client_go(w, peer, iface_addr);
9081}
9082
9083
9084static void wpas_p2p_psk_failure_removal(void *eloop_ctx, void *timeout_ctx)
9085{
9086 struct wpa_supplicant *wpa_s = eloop_ctx;
9087 wpas_p2p_group_delete(wpa_s, P2P_GROUP_REMOVAL_PSK_FAILURE);
9088}
9089
9090
Dmitry Shmidt04f534e2013-12-09 15:50:16 -08009091static void wpas_p2p_group_freq_conflict(void *eloop_ctx, void *timeout_ctx)
9092{
9093 struct wpa_supplicant *wpa_s = eloop_ctx;
9094
9095 wpa_printf(MSG_DEBUG, "P2P: Frequency conflict - terminate group");
9096 wpas_p2p_group_delete(wpa_s, P2P_GROUP_REMOVAL_FREQ_CONFLICT);
9097}
9098
9099
9100int wpas_p2p_handle_frequency_conflicts(struct wpa_supplicant *wpa_s, int freq,
9101 struct wpa_ssid *ssid)
9102{
9103 struct wpa_supplicant *iface;
9104
9105 for (iface = wpa_s->global->ifaces; iface; iface = iface->next) {
9106 if (!iface->current_ssid ||
9107 iface->current_ssid->frequency == freq ||
9108 (iface->p2p_group_interface == NOT_P2P_GROUP_INTERFACE &&
9109 !iface->current_ssid->p2p_group))
9110 continue;
9111
9112 /* Remove the connection with least priority */
9113 if (!wpas_is_p2p_prioritized(iface)) {
9114 /* STA connection has priority over existing
9115 * P2P connection, so remove the interface. */
9116 wpa_printf(MSG_DEBUG, "P2P: Removing P2P connection due to single channel concurrent mode frequency conflict");
9117 eloop_register_timeout(0, 0,
9118 wpas_p2p_group_freq_conflict,
9119 iface, NULL);
9120 /* If connection in progress is P2P connection, do not
9121 * proceed for the connection. */
9122 if (wpa_s == iface)
9123 return -1;
9124 else
9125 return 0;
9126 } else {
9127 /* P2P connection has priority, disable the STA network
9128 */
9129 wpa_supplicant_disable_network(wpa_s->global->ifaces,
9130 ssid);
9131 wpa_msg(wpa_s->global->ifaces, MSG_INFO,
9132 WPA_EVENT_FREQ_CONFLICT " id=%d", ssid->id);
9133 os_memset(wpa_s->global->ifaces->pending_bssid, 0,
9134 ETH_ALEN);
9135 /* If P2P connection is in progress, continue
9136 * connecting...*/
9137 if (wpa_s == iface)
9138 return 0;
9139 else
9140 return -1;
9141 }
9142 }
9143
9144 return 0;
9145}
9146
9147
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07009148int wpas_p2p_4way_hs_failed(struct wpa_supplicant *wpa_s)
9149{
9150 struct wpa_ssid *ssid = wpa_s->current_ssid;
9151
9152 if (ssid == NULL || !ssid->p2p_group)
9153 return 0;
9154
9155 if (wpa_s->p2p_last_4way_hs_fail &&
9156 wpa_s->p2p_last_4way_hs_fail == ssid) {
9157 u8 go_dev_addr[ETH_ALEN];
9158 struct wpa_ssid *persistent;
9159
9160 if (wpas_p2p_persistent_group(wpa_s, go_dev_addr,
9161 ssid->ssid,
9162 ssid->ssid_len) <= 0) {
9163 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Could not determine whether 4-way handshake failures were for a persistent group");
9164 goto disconnect;
9165 }
9166
9167 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Two 4-way handshake failures for a P2P group - go_dev_addr="
9168 MACSTR, MAC2STR(go_dev_addr));
Dmitry Shmidt9c175262016-03-03 10:20:07 -08009169 persistent = wpas_p2p_get_persistent(wpa_s->p2pdev, go_dev_addr,
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07009170 ssid->ssid,
9171 ssid->ssid_len);
9172 if (persistent == NULL || persistent->mode != WPAS_MODE_INFRA) {
9173 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: No matching persistent group stored");
9174 goto disconnect;
9175 }
Dmitry Shmidt9c175262016-03-03 10:20:07 -08009176 wpa_msg_global(wpa_s->p2pdev, MSG_INFO,
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07009177 P2P_EVENT_PERSISTENT_PSK_FAIL "%d",
9178 persistent->id);
9179 disconnect:
9180 wpa_s->p2p_last_4way_hs_fail = NULL;
9181 /*
9182 * Remove the group from a timeout to avoid issues with caller
9183 * continuing to use the interface if this is on a P2P group
9184 * interface.
9185 */
9186 eloop_register_timeout(0, 0, wpas_p2p_psk_failure_removal,
9187 wpa_s, NULL);
9188 return 1;
9189 }
9190
9191 wpa_s->p2p_last_4way_hs_fail = ssid;
9192 return 0;
9193}
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08009194
9195
9196#ifdef CONFIG_WPS_NFC
9197
9198static struct wpabuf * wpas_p2p_nfc_handover(int ndef, struct wpabuf *wsc,
9199 struct wpabuf *p2p)
9200{
9201 struct wpabuf *ret;
9202 size_t wsc_len;
9203
9204 if (p2p == NULL) {
9205 wpabuf_free(wsc);
9206 wpa_printf(MSG_DEBUG, "P2P: No p2p buffer for handover");
9207 return NULL;
9208 }
9209
9210 wsc_len = wsc ? wpabuf_len(wsc) : 0;
9211 ret = wpabuf_alloc(2 + wsc_len + 2 + wpabuf_len(p2p));
9212 if (ret == NULL) {
9213 wpabuf_free(wsc);
9214 wpabuf_free(p2p);
9215 return NULL;
9216 }
9217
9218 wpabuf_put_be16(ret, wsc_len);
9219 if (wsc)
9220 wpabuf_put_buf(ret, wsc);
9221 wpabuf_put_be16(ret, wpabuf_len(p2p));
9222 wpabuf_put_buf(ret, p2p);
9223
9224 wpabuf_free(wsc);
9225 wpabuf_free(p2p);
9226 wpa_hexdump_buf(MSG_DEBUG,
9227 "P2P: Generated NFC connection handover message", ret);
9228
9229 if (ndef && ret) {
9230 struct wpabuf *tmp;
9231 tmp = ndef_build_p2p(ret);
9232 wpabuf_free(ret);
9233 if (tmp == NULL) {
9234 wpa_printf(MSG_DEBUG, "P2P: Failed to NDEF encapsulate handover request");
9235 return NULL;
9236 }
9237 ret = tmp;
9238 }
9239
9240 return ret;
9241}
9242
9243
9244static int wpas_p2p_cli_freq(struct wpa_supplicant *wpa_s,
9245 struct wpa_ssid **ssid, u8 *go_dev_addr)
9246{
9247 struct wpa_supplicant *iface;
9248
9249 if (go_dev_addr)
9250 os_memset(go_dev_addr, 0, ETH_ALEN);
9251 if (ssid)
9252 *ssid = NULL;
9253 for (iface = wpa_s->global->ifaces; iface; iface = iface->next) {
9254 if (iface->wpa_state < WPA_ASSOCIATING ||
9255 iface->current_ssid == NULL || iface->assoc_freq == 0 ||
9256 !iface->current_ssid->p2p_group ||
9257 iface->current_ssid->mode != WPAS_MODE_INFRA)
9258 continue;
9259 if (ssid)
9260 *ssid = iface->current_ssid;
9261 if (go_dev_addr)
9262 os_memcpy(go_dev_addr, iface->go_dev_addr, ETH_ALEN);
9263 return iface->assoc_freq;
9264 }
9265 return 0;
9266}
9267
9268
9269struct wpabuf * wpas_p2p_nfc_handover_req(struct wpa_supplicant *wpa_s,
9270 int ndef)
9271{
9272 struct wpabuf *wsc, *p2p;
9273 struct wpa_ssid *ssid;
9274 u8 go_dev_addr[ETH_ALEN];
9275 int cli_freq = wpas_p2p_cli_freq(wpa_s, &ssid, go_dev_addr);
9276
9277 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL) {
9278 wpa_printf(MSG_DEBUG, "P2P: P2P disabled - cannot build handover request");
9279 return NULL;
9280 }
9281
9282 if (wpa_s->conf->wps_nfc_dh_pubkey == NULL &&
9283 wps_nfc_gen_dh(&wpa_s->conf->wps_nfc_dh_pubkey,
9284 &wpa_s->conf->wps_nfc_dh_privkey) < 0) {
9285 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: No DH key available for handover request");
9286 return NULL;
9287 }
9288
9289 if (cli_freq == 0) {
9290 wsc = wps_build_nfc_handover_req_p2p(
9291 wpa_s->parent->wps, wpa_s->conf->wps_nfc_dh_pubkey);
9292 } else
9293 wsc = NULL;
9294 p2p = p2p_build_nfc_handover_req(wpa_s->global->p2p, cli_freq,
9295 go_dev_addr, ssid ? ssid->ssid : NULL,
9296 ssid ? ssid->ssid_len : 0);
9297
9298 return wpas_p2p_nfc_handover(ndef, wsc, p2p);
9299}
9300
9301
9302struct wpabuf * wpas_p2p_nfc_handover_sel(struct wpa_supplicant *wpa_s,
9303 int ndef, int tag)
9304{
9305 struct wpabuf *wsc, *p2p;
9306 struct wpa_ssid *ssid;
9307 u8 go_dev_addr[ETH_ALEN];
9308 int cli_freq = wpas_p2p_cli_freq(wpa_s, &ssid, go_dev_addr);
9309
9310 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
9311 return NULL;
9312
9313 if (!tag && wpa_s->conf->wps_nfc_dh_pubkey == NULL &&
9314 wps_nfc_gen_dh(&wpa_s->conf->wps_nfc_dh_pubkey,
9315 &wpa_s->conf->wps_nfc_dh_privkey) < 0)
9316 return NULL;
9317
9318 if (cli_freq == 0) {
9319 wsc = wps_build_nfc_handover_sel_p2p(
9320 wpa_s->parent->wps,
9321 tag ? wpa_s->conf->wps_nfc_dev_pw_id :
9322 DEV_PW_NFC_CONNECTION_HANDOVER,
9323 wpa_s->conf->wps_nfc_dh_pubkey,
9324 tag ? wpa_s->conf->wps_nfc_dev_pw : NULL);
9325 } else
9326 wsc = NULL;
9327 p2p = p2p_build_nfc_handover_sel(wpa_s->global->p2p, cli_freq,
9328 go_dev_addr, ssid ? ssid->ssid : NULL,
9329 ssid ? ssid->ssid_len : 0);
9330
9331 return wpas_p2p_nfc_handover(ndef, wsc, p2p);
9332}
9333
9334
9335static int wpas_p2p_nfc_join_group(struct wpa_supplicant *wpa_s,
9336 struct p2p_nfc_params *params)
9337{
9338 wpa_printf(MSG_DEBUG, "P2P: Initiate join-group based on NFC "
9339 "connection handover (freq=%d)",
9340 params->go_freq);
9341
9342 if (params->go_freq && params->go_ssid_len) {
9343 wpa_s->p2p_wps_method = WPS_NFC;
9344 wpa_s->pending_join_wps_method = WPS_NFC;
9345 os_memset(wpa_s->pending_join_iface_addr, 0, ETH_ALEN);
9346 os_memcpy(wpa_s->pending_join_dev_addr, params->go_dev_addr,
9347 ETH_ALEN);
9348 return wpas_p2p_join_start(wpa_s, params->go_freq,
9349 params->go_ssid,
9350 params->go_ssid_len);
9351 }
9352
9353 return wpas_p2p_connect(wpa_s, params->peer->p2p_device_addr, NULL,
9354 WPS_NFC, 0, 0, 1, 0, wpa_s->conf->p2p_go_intent,
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08009355 params->go_freq, wpa_s->p2p_go_vht_center_freq2,
Dmitry Shmidtde47be72016-01-07 12:52:55 -08009356 -1, 0, 1, 1, wpa_s->p2p_go_max_oper_chwidth,
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08009357 wpa_s->p2p_go_he, wpa_s->p2p_go_edmg,
Dmitry Shmidtde47be72016-01-07 12:52:55 -08009358 params->go_ssid_len ? params->go_ssid : NULL,
Hai Shalomc1a21442022-02-04 13:43:00 -08009359 params->go_ssid_len, false);
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08009360}
9361
9362
9363static int wpas_p2p_nfc_auth_join(struct wpa_supplicant *wpa_s,
9364 struct p2p_nfc_params *params, int tag)
9365{
9366 int res, persistent;
9367 struct wpa_ssid *ssid;
9368
9369 wpa_printf(MSG_DEBUG, "P2P: Authorize join-group based on NFC "
9370 "connection handover");
9371 for (wpa_s = wpa_s->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
9372 ssid = wpa_s->current_ssid;
9373 if (ssid == NULL)
9374 continue;
9375 if (ssid->mode != WPAS_MODE_P2P_GO)
9376 continue;
9377 if (wpa_s->ap_iface == NULL)
9378 continue;
9379 break;
9380 }
9381 if (wpa_s == NULL) {
9382 wpa_printf(MSG_DEBUG, "P2P: Could not find GO interface");
9383 return -1;
9384 }
9385
Dmitry Shmidt849734c2016-05-27 09:59:01 -07009386 if (wpa_s->p2pdev->p2p_oob_dev_pw_id !=
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08009387 DEV_PW_NFC_CONNECTION_HANDOVER &&
Dmitry Shmidt849734c2016-05-27 09:59:01 -07009388 !wpa_s->p2pdev->p2p_oob_dev_pw) {
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08009389 wpa_printf(MSG_DEBUG, "P2P: No NFC Dev Pw known");
9390 return -1;
9391 }
9392 res = wpas_ap_wps_add_nfc_pw(
Dmitry Shmidt849734c2016-05-27 09:59:01 -07009393 wpa_s, wpa_s->p2pdev->p2p_oob_dev_pw_id,
9394 wpa_s->p2pdev->p2p_oob_dev_pw,
9395 wpa_s->p2pdev->p2p_peer_oob_pk_hash_known ?
9396 wpa_s->p2pdev->p2p_peer_oob_pubkey_hash : NULL);
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08009397 if (res)
9398 return res;
9399
9400 if (!tag) {
9401 wpa_printf(MSG_DEBUG, "P2P: Negotiated handover - wait for peer to join without invitation");
9402 return 0;
9403 }
9404
9405 if (!params->peer ||
9406 !(params->peer->dev_capab & P2P_DEV_CAPAB_INVITATION_PROCEDURE))
9407 return 0;
9408
9409 wpa_printf(MSG_DEBUG, "P2P: Static handover - invite peer " MACSTR
9410 " to join", MAC2STR(params->peer->p2p_device_addr));
9411
9412 wpa_s->global->p2p_invite_group = wpa_s;
9413 persistent = ssid->p2p_persistent_group &&
Dmitry Shmidt849734c2016-05-27 09:59:01 -07009414 wpas_p2p_get_persistent(wpa_s->p2pdev,
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08009415 params->peer->p2p_device_addr,
9416 ssid->ssid, ssid->ssid_len);
Dmitry Shmidt849734c2016-05-27 09:59:01 -07009417 wpa_s->p2pdev->pending_invite_ssid_id = -1;
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08009418
9419 return p2p_invite(wpa_s->global->p2p, params->peer->p2p_device_addr,
9420 P2P_INVITE_ROLE_ACTIVE_GO, wpa_s->own_addr,
9421 ssid->ssid, ssid->ssid_len, ssid->frequency,
9422 wpa_s->global->p2p_dev_addr, persistent, 0,
Dmitry Shmidt849734c2016-05-27 09:59:01 -07009423 wpa_s->p2pdev->p2p_oob_dev_pw_id);
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08009424}
9425
9426
9427static int wpas_p2p_nfc_init_go_neg(struct wpa_supplicant *wpa_s,
9428 struct p2p_nfc_params *params,
9429 int forced_freq)
9430{
9431 wpa_printf(MSG_DEBUG, "P2P: Initiate GO Negotiation based on NFC "
9432 "connection handover");
9433 return wpas_p2p_connect(wpa_s, params->peer->p2p_device_addr, NULL,
9434 WPS_NFC, 0, 0, 0, 0, wpa_s->conf->p2p_go_intent,
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08009435 forced_freq, wpa_s->p2p_go_vht_center_freq2,
Dmitry Shmidtde47be72016-01-07 12:52:55 -08009436 -1, 0, 1, 1, wpa_s->p2p_go_max_oper_chwidth,
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08009437 wpa_s->p2p_go_he, wpa_s->p2p_go_edmg,
Hai Shalomc1a21442022-02-04 13:43:00 -08009438 NULL, 0, false);
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08009439}
9440
9441
9442static int wpas_p2p_nfc_resp_go_neg(struct wpa_supplicant *wpa_s,
9443 struct p2p_nfc_params *params,
9444 int forced_freq)
9445{
9446 int res;
9447
9448 wpa_printf(MSG_DEBUG, "P2P: Authorize GO Negotiation based on NFC "
9449 "connection handover");
9450 res = wpas_p2p_connect(wpa_s, params->peer->p2p_device_addr, NULL,
9451 WPS_NFC, 0, 0, 0, 1, wpa_s->conf->p2p_go_intent,
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08009452 forced_freq, wpa_s->p2p_go_vht_center_freq2,
Dmitry Shmidtde47be72016-01-07 12:52:55 -08009453 -1, 0, 1, 1, wpa_s->p2p_go_max_oper_chwidth,
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08009454 wpa_s->p2p_go_he, wpa_s->p2p_go_edmg,
Hai Shalomc1a21442022-02-04 13:43:00 -08009455 NULL, 0, false);
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08009456 if (res)
9457 return res;
9458
9459 res = wpas_p2p_listen(wpa_s, 60);
9460 if (res) {
9461 p2p_unauthorize(wpa_s->global->p2p,
9462 params->peer->p2p_device_addr);
9463 }
9464
9465 return res;
9466}
9467
9468
9469static int wpas_p2p_nfc_connection_handover(struct wpa_supplicant *wpa_s,
9470 const struct wpabuf *data,
9471 int sel, int tag, int forced_freq)
9472{
9473 const u8 *pos, *end;
9474 u16 len, id;
9475 struct p2p_nfc_params params;
9476 int res;
9477
9478 os_memset(&params, 0, sizeof(params));
9479 params.sel = sel;
9480
9481 wpa_hexdump_buf(MSG_DEBUG, "P2P: Received NFC tag payload", data);
9482
9483 pos = wpabuf_head(data);
9484 end = pos + wpabuf_len(data);
9485
9486 if (end - pos < 2) {
9487 wpa_printf(MSG_DEBUG, "P2P: Not enough data for Length of WSC "
9488 "attributes");
9489 return -1;
9490 }
9491 len = WPA_GET_BE16(pos);
9492 pos += 2;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08009493 if (len > end - pos) {
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08009494 wpa_printf(MSG_DEBUG, "P2P: Not enough data for WSC "
9495 "attributes");
9496 return -1;
9497 }
9498 params.wsc_attr = pos;
9499 params.wsc_len = len;
9500 pos += len;
9501
9502 if (end - pos < 2) {
9503 wpa_printf(MSG_DEBUG, "P2P: Not enough data for Length of P2P "
9504 "attributes");
9505 return -1;
9506 }
9507 len = WPA_GET_BE16(pos);
9508 pos += 2;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08009509 if (len > end - pos) {
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08009510 wpa_printf(MSG_DEBUG, "P2P: Not enough data for P2P "
9511 "attributes");
9512 return -1;
9513 }
9514 params.p2p_attr = pos;
9515 params.p2p_len = len;
9516 pos += len;
9517
9518 wpa_hexdump(MSG_DEBUG, "P2P: WSC attributes",
9519 params.wsc_attr, params.wsc_len);
9520 wpa_hexdump(MSG_DEBUG, "P2P: P2P attributes",
9521 params.p2p_attr, params.p2p_len);
9522 if (pos < end) {
9523 wpa_hexdump(MSG_DEBUG,
9524 "P2P: Ignored extra data after P2P attributes",
9525 pos, end - pos);
9526 }
9527
9528 res = p2p_process_nfc_connection_handover(wpa_s->global->p2p, &params);
9529 if (res)
9530 return res;
9531
9532 if (params.next_step == NO_ACTION)
9533 return 0;
9534
9535 if (params.next_step == BOTH_GO) {
9536 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_NFC_BOTH_GO "peer=" MACSTR,
9537 MAC2STR(params.peer->p2p_device_addr));
9538 return 0;
9539 }
9540
9541 if (params.next_step == PEER_CLIENT) {
9542 if (!is_zero_ether_addr(params.go_dev_addr)) {
9543 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_NFC_PEER_CLIENT
9544 "peer=" MACSTR " freq=%d go_dev_addr=" MACSTR
9545 " ssid=\"%s\"",
9546 MAC2STR(params.peer->p2p_device_addr),
9547 params.go_freq,
9548 MAC2STR(params.go_dev_addr),
9549 wpa_ssid_txt(params.go_ssid,
9550 params.go_ssid_len));
9551 } else {
9552 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_NFC_PEER_CLIENT
9553 "peer=" MACSTR " freq=%d",
9554 MAC2STR(params.peer->p2p_device_addr),
9555 params.go_freq);
9556 }
9557 return 0;
9558 }
9559
9560 if (wpas_p2p_cli_freq(wpa_s, NULL, NULL)) {
9561 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_NFC_WHILE_CLIENT "peer="
9562 MACSTR, MAC2STR(params.peer->p2p_device_addr));
9563 return 0;
9564 }
9565
9566 wpabuf_free(wpa_s->p2p_oob_dev_pw);
9567 wpa_s->p2p_oob_dev_pw = NULL;
9568
9569 if (params.oob_dev_pw_len < WPS_OOB_PUBKEY_HASH_LEN + 2) {
9570 wpa_printf(MSG_DEBUG, "P2P: No peer OOB Dev Pw "
9571 "received");
9572 return -1;
9573 }
9574
9575 id = WPA_GET_BE16(params.oob_dev_pw + WPS_OOB_PUBKEY_HASH_LEN);
9576 wpa_printf(MSG_DEBUG, "P2P: Peer OOB Dev Pw %u", id);
9577 wpa_hexdump(MSG_DEBUG, "P2P: Peer OOB Public Key hash",
9578 params.oob_dev_pw, WPS_OOB_PUBKEY_HASH_LEN);
9579 os_memcpy(wpa_s->p2p_peer_oob_pubkey_hash,
9580 params.oob_dev_pw, WPS_OOB_PUBKEY_HASH_LEN);
9581 wpa_s->p2p_peer_oob_pk_hash_known = 1;
9582
9583 if (tag) {
9584 if (id < 0x10) {
9585 wpa_printf(MSG_DEBUG, "P2P: Static handover - invalid "
9586 "peer OOB Device Password Id %u", id);
9587 return -1;
9588 }
9589 wpa_printf(MSG_DEBUG, "P2P: Static handover - use peer OOB "
9590 "Device Password Id %u", id);
9591 wpa_hexdump_key(MSG_DEBUG, "P2P: Peer OOB Device Password",
9592 params.oob_dev_pw + WPS_OOB_PUBKEY_HASH_LEN + 2,
9593 params.oob_dev_pw_len -
9594 WPS_OOB_PUBKEY_HASH_LEN - 2);
9595 wpa_s->p2p_oob_dev_pw_id = id;
9596 wpa_s->p2p_oob_dev_pw = wpabuf_alloc_copy(
9597 params.oob_dev_pw + WPS_OOB_PUBKEY_HASH_LEN + 2,
9598 params.oob_dev_pw_len -
9599 WPS_OOB_PUBKEY_HASH_LEN - 2);
9600 if (wpa_s->p2p_oob_dev_pw == NULL)
9601 return -1;
9602
9603 if (wpa_s->conf->wps_nfc_dh_pubkey == NULL &&
9604 wps_nfc_gen_dh(&wpa_s->conf->wps_nfc_dh_pubkey,
9605 &wpa_s->conf->wps_nfc_dh_privkey) < 0)
9606 return -1;
9607 } else {
9608 wpa_printf(MSG_DEBUG, "P2P: Using abbreviated WPS handshake "
9609 "without Device Password");
9610 wpa_s->p2p_oob_dev_pw_id = DEV_PW_NFC_CONNECTION_HANDOVER;
9611 }
9612
9613 switch (params.next_step) {
9614 case NO_ACTION:
9615 case BOTH_GO:
9616 case PEER_CLIENT:
9617 /* already covered above */
9618 return 0;
9619 case JOIN_GROUP:
9620 return wpas_p2p_nfc_join_group(wpa_s, &params);
9621 case AUTH_JOIN:
9622 return wpas_p2p_nfc_auth_join(wpa_s, &params, tag);
9623 case INIT_GO_NEG:
9624 return wpas_p2p_nfc_init_go_neg(wpa_s, &params, forced_freq);
9625 case RESP_GO_NEG:
9626 /* TODO: use own OOB Dev Pw */
9627 return wpas_p2p_nfc_resp_go_neg(wpa_s, &params, forced_freq);
9628 }
9629
9630 return -1;
9631}
9632
9633
9634int wpas_p2p_nfc_tag_process(struct wpa_supplicant *wpa_s,
9635 const struct wpabuf *data, int forced_freq)
9636{
9637 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
9638 return -1;
9639
9640 return wpas_p2p_nfc_connection_handover(wpa_s, data, 1, 1, forced_freq);
9641}
9642
9643
9644int wpas_p2p_nfc_report_handover(struct wpa_supplicant *wpa_s, int init,
9645 const struct wpabuf *req,
9646 const struct wpabuf *sel, int forced_freq)
9647{
9648 struct wpabuf *tmp;
9649 int ret;
9650
9651 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
9652 return -1;
9653
9654 wpa_printf(MSG_DEBUG, "NFC: P2P connection handover reported");
9655
9656 wpa_hexdump_ascii(MSG_DEBUG, "NFC: Req",
9657 wpabuf_head(req), wpabuf_len(req));
9658 wpa_hexdump_ascii(MSG_DEBUG, "NFC: Sel",
9659 wpabuf_head(sel), wpabuf_len(sel));
9660 if (forced_freq)
9661 wpa_printf(MSG_DEBUG, "NFC: Forced freq %d", forced_freq);
9662 tmp = ndef_parse_p2p(init ? sel : req);
9663 if (tmp == NULL) {
9664 wpa_printf(MSG_DEBUG, "P2P: Could not parse NDEF");
9665 return -1;
9666 }
9667
9668 ret = wpas_p2p_nfc_connection_handover(wpa_s, tmp, init, 0,
9669 forced_freq);
9670 wpabuf_free(tmp);
9671
9672 return ret;
9673}
9674
9675
9676int wpas_p2p_nfc_tag_enabled(struct wpa_supplicant *wpa_s, int enabled)
9677{
9678 const u8 *if_addr;
9679 int go_intent = wpa_s->conf->p2p_go_intent;
9680 struct wpa_supplicant *iface;
9681
9682 if (wpa_s->global->p2p == NULL)
9683 return -1;
9684
9685 if (!enabled) {
9686 wpa_printf(MSG_DEBUG, "P2P: Disable use of own NFC Tag");
9687 for (iface = wpa_s->global->ifaces; iface; iface = iface->next)
9688 {
9689 if (!iface->ap_iface)
9690 continue;
9691 hostapd_wps_nfc_token_disable(iface->ap_iface->bss[0]);
9692 }
9693 p2p_set_authorized_oob_dev_pw_id(wpa_s->global->p2p, 0,
9694 0, NULL);
9695 if (wpa_s->p2p_nfc_tag_enabled)
9696 wpas_p2p_remove_pending_group_interface(wpa_s);
9697 wpa_s->p2p_nfc_tag_enabled = 0;
9698 return 0;
9699 }
9700
9701 if (wpa_s->global->p2p_disabled)
9702 return -1;
9703
9704 if (wpa_s->conf->wps_nfc_dh_pubkey == NULL ||
9705 wpa_s->conf->wps_nfc_dh_privkey == NULL ||
9706 wpa_s->conf->wps_nfc_dev_pw == NULL ||
9707 wpa_s->conf->wps_nfc_dev_pw_id < 0x10) {
9708 wpa_printf(MSG_DEBUG, "P2P: NFC password token not configured "
9709 "to allow static handover cases");
9710 return -1;
9711 }
9712
9713 wpa_printf(MSG_DEBUG, "P2P: Enable use of own NFC Tag");
9714
9715 wpa_s->p2p_oob_dev_pw_id = wpa_s->conf->wps_nfc_dev_pw_id;
9716 wpabuf_free(wpa_s->p2p_oob_dev_pw);
9717 wpa_s->p2p_oob_dev_pw = wpabuf_dup(wpa_s->conf->wps_nfc_dev_pw);
9718 if (wpa_s->p2p_oob_dev_pw == NULL)
9719 return -1;
9720 wpa_s->p2p_peer_oob_pk_hash_known = 0;
9721
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -07009722 if (wpa_s->p2p_group_interface == P2P_GROUP_INTERFACE_GO ||
9723 wpa_s->p2p_group_interface == P2P_GROUP_INTERFACE_CLIENT) {
9724 /*
9725 * P2P Group Interface present and the command came on group
9726 * interface, so enable the token for the current interface.
9727 */
9728 wpa_s->create_p2p_iface = 0;
9729 } else {
9730 wpa_s->create_p2p_iface = wpas_p2p_create_iface(wpa_s);
9731 }
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08009732
9733 if (wpa_s->create_p2p_iface) {
9734 enum wpa_driver_if_type iftype;
9735 /* Prepare to add a new interface for the group */
9736 iftype = WPA_IF_P2P_GROUP;
9737 if (go_intent == 15)
9738 iftype = WPA_IF_P2P_GO;
9739 if (wpas_p2p_add_group_interface(wpa_s, iftype) < 0) {
9740 wpa_printf(MSG_ERROR, "P2P: Failed to allocate a new "
9741 "interface for the group");
9742 return -1;
9743 }
9744
9745 if_addr = wpa_s->pending_interface_addr;
Dmitry Shmidt9c175262016-03-03 10:20:07 -08009746 } else if (wpa_s->p2p_mgmt)
9747 if_addr = wpa_s->parent->own_addr;
9748 else
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08009749 if_addr = wpa_s->own_addr;
9750
9751 wpa_s->p2p_nfc_tag_enabled = enabled;
9752
9753 for (iface = wpa_s->global->ifaces; iface; iface = iface->next) {
9754 struct hostapd_data *hapd;
9755 if (iface->ap_iface == NULL)
9756 continue;
9757 hapd = iface->ap_iface->bss[0];
9758 wpabuf_free(hapd->conf->wps_nfc_dh_pubkey);
9759 hapd->conf->wps_nfc_dh_pubkey =
9760 wpabuf_dup(wpa_s->conf->wps_nfc_dh_pubkey);
9761 wpabuf_free(hapd->conf->wps_nfc_dh_privkey);
9762 hapd->conf->wps_nfc_dh_privkey =
9763 wpabuf_dup(wpa_s->conf->wps_nfc_dh_privkey);
9764 wpabuf_free(hapd->conf->wps_nfc_dev_pw);
9765 hapd->conf->wps_nfc_dev_pw =
9766 wpabuf_dup(wpa_s->conf->wps_nfc_dev_pw);
9767 hapd->conf->wps_nfc_dev_pw_id = wpa_s->conf->wps_nfc_dev_pw_id;
9768
9769 if (hostapd_wps_nfc_token_enable(iface->ap_iface->bss[0]) < 0) {
9770 wpa_dbg(iface, MSG_DEBUG,
9771 "P2P: Failed to enable NFC Tag for GO");
9772 }
9773 }
9774 p2p_set_authorized_oob_dev_pw_id(
9775 wpa_s->global->p2p, wpa_s->conf->wps_nfc_dev_pw_id, go_intent,
9776 if_addr);
9777
9778 return 0;
9779}
9780
9781#endif /* CONFIG_WPS_NFC */
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07009782
9783
9784static void wpas_p2p_optimize_listen_channel(struct wpa_supplicant *wpa_s,
9785 struct wpa_used_freq_data *freqs,
9786 unsigned int num)
9787{
9788 u8 curr_chan, cand, chan;
9789 unsigned int i;
9790
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08009791 /*
9792 * If possible, optimize the Listen channel to be a channel that is
9793 * already used by one of the other interfaces.
9794 */
9795 if (!wpa_s->conf->p2p_optimize_listen_chan)
9796 return;
9797
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07009798 curr_chan = p2p_get_listen_channel(wpa_s->global->p2p);
9799 for (i = 0, cand = 0; i < num; i++) {
9800 ieee80211_freq_to_chan(freqs[i].freq, &chan);
9801 if (curr_chan == chan) {
9802 cand = 0;
9803 break;
9804 }
9805
9806 if (chan == 1 || chan == 6 || chan == 11)
9807 cand = chan;
9808 }
9809
9810 if (cand) {
9811 wpa_dbg(wpa_s, MSG_DEBUG,
Dmitry Shmidt216983b2015-02-06 10:50:36 -08009812 "P2P: Update Listen channel to %u based on operating channel",
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07009813 cand);
9814 p2p_set_listen_channel(wpa_s->global->p2p, 81, cand, 0);
9815 }
9816}
9817
9818
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08009819static int wpas_p2p_move_go_csa(struct wpa_supplicant *wpa_s)
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07009820{
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08009821 struct hostapd_config *conf;
9822 struct p2p_go_neg_results params;
9823 struct csa_settings csa_settings;
9824 struct wpa_ssid *current_ssid = wpa_s->current_ssid;
9825 int old_freq = current_ssid->frequency;
9826 int ret;
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07009827
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08009828 if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_AP_CSA)) {
9829 wpa_dbg(wpa_s, MSG_DEBUG, "CSA is not enabled");
9830 return -1;
9831 }
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07009832
9833 /*
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08009834 * TODO: This function may not always work correctly. For example,
9835 * when we have a running GO and a BSS on a DFS channel.
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07009836 */
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08009837 if (wpas_p2p_init_go_params(wpa_s, &params, 0, 0, 0, 0, 0, 0, 0,
9838 NULL)) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08009839 wpa_dbg(wpa_s, MSG_DEBUG,
9840 "P2P CSA: Failed to select new frequency for GO");
9841 return -1;
9842 }
9843
9844 if (current_ssid->frequency == params.freq) {
9845 wpa_dbg(wpa_s, MSG_DEBUG,
9846 "P2P CSA: Selected same frequency - not moving GO");
9847 return 0;
9848 }
9849
9850 conf = hostapd_config_defaults();
9851 if (!conf) {
9852 wpa_dbg(wpa_s, MSG_DEBUG,
9853 "P2P CSA: Failed to allocate default config");
9854 return -1;
9855 }
9856
9857 current_ssid->frequency = params.freq;
9858 if (wpa_supplicant_conf_ap_ht(wpa_s, current_ssid, conf)) {
9859 wpa_dbg(wpa_s, MSG_DEBUG,
9860 "P2P CSA: Failed to create new GO config");
9861 ret = -1;
9862 goto out;
9863 }
9864
Sunil4a3f9f52022-07-19 22:04:39 +00009865 if (conf->hw_mode != wpa_s->ap_iface->current_mode->mode &&
9866 (wpa_s->ap_iface->current_mode->mode != HOSTAPD_MODE_IEEE80211A ||
Sunil Ravi89eba102022-09-13 21:04:37 -07009867 is_6ghz_freq(wpa_s->ap_iface->freq) ||
Sunil4a3f9f52022-07-19 22:04:39 +00009868 conf->hw_mode != HOSTAPD_MODE_IEEE80211G)) {
9869 wpa_dbg(wpa_s, MSG_INFO,
Sunil Ravi89eba102022-09-13 21:04:37 -07009870 "P2P CSA: CSA from hardware mode %d%s to %d is not supported",
9871 wpa_s->ap_iface->current_mode->mode,
9872 is_6ghz_freq(wpa_s->ap_iface->freq) ? " (6 GHz)" : "",
9873 conf->hw_mode);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08009874 ret = -1;
9875 goto out;
9876 }
9877
9878 os_memset(&csa_settings, 0, sizeof(csa_settings));
9879 csa_settings.cs_count = P2P_GO_CSA_COUNT;
9880 csa_settings.block_tx = P2P_GO_CSA_BLOCK_TX;
9881 csa_settings.freq_params.freq = params.freq;
9882 csa_settings.freq_params.sec_channel_offset = conf->secondary_channel;
9883 csa_settings.freq_params.ht_enabled = conf->ieee80211n;
9884 csa_settings.freq_params.bandwidth = conf->secondary_channel ? 40 : 20;
9885
9886 if (conf->ieee80211ac) {
9887 int freq1 = 0, freq2 = 0;
9888 u8 chan, opclass;
9889
9890 if (ieee80211_freq_to_channel_ext(params.freq,
9891 conf->secondary_channel,
9892 conf->vht_oper_chwidth,
9893 &opclass, &chan) ==
9894 NUM_HOSTAPD_MODES) {
9895 wpa_printf(MSG_ERROR, "P2P CSA: Bad freq");
9896 ret = -1;
9897 goto out;
9898 }
9899
9900 if (conf->vht_oper_centr_freq_seg0_idx)
9901 freq1 = ieee80211_chan_to_freq(
9902 NULL, opclass,
9903 conf->vht_oper_centr_freq_seg0_idx);
9904
9905 if (conf->vht_oper_centr_freq_seg1_idx)
9906 freq2 = ieee80211_chan_to_freq(
9907 NULL, opclass,
9908 conf->vht_oper_centr_freq_seg1_idx);
9909
9910 if (freq1 < 0 || freq2 < 0) {
9911 wpa_dbg(wpa_s, MSG_DEBUG,
9912 "P2P CSA: Selected invalid VHT center freqs");
9913 ret = -1;
9914 goto out;
9915 }
9916
9917 csa_settings.freq_params.vht_enabled = conf->ieee80211ac;
9918 csa_settings.freq_params.center_freq1 = freq1;
9919 csa_settings.freq_params.center_freq2 = freq2;
9920
9921 switch (conf->vht_oper_chwidth) {
Sunil8cd6f4d2022-06-28 18:40:46 +00009922 case CONF_OPER_CHWIDTH_80MHZ:
9923 case CONF_OPER_CHWIDTH_80P80MHZ:
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08009924 csa_settings.freq_params.bandwidth = 80;
9925 break;
Sunil8cd6f4d2022-06-28 18:40:46 +00009926 case CONF_OPER_CHWIDTH_160MHZ:
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08009927 csa_settings.freq_params.bandwidth = 160;
9928 break;
Sunil8cd6f4d2022-06-28 18:40:46 +00009929 default:
9930 break;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08009931 }
9932 }
9933
9934 ret = ap_switch_channel(wpa_s, &csa_settings);
9935out:
9936 current_ssid->frequency = old_freq;
9937 hostapd_config_free(conf);
9938 return ret;
9939}
9940
9941
9942static void wpas_p2p_move_go_no_csa(struct wpa_supplicant *wpa_s)
9943{
9944 struct p2p_go_neg_results params;
9945 struct wpa_ssid *current_ssid = wpa_s->current_ssid;
Hai Shalom899fcc72020-10-19 14:38:18 -07009946 void (*ap_configured_cb)(void *ctx, void *data);
9947 void *ap_configured_cb_ctx, *ap_configured_cb_data;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08009948
9949 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_REMOVE_AND_REFORM_GROUP);
9950
9951 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Move GO from freq=%d MHz",
9952 current_ssid->frequency);
9953
9954 /* Stop the AP functionality */
9955 /* TODO: Should do this in a way that does not indicated to possible
9956 * P2P Clients in the group that the group is terminated. */
Hai Shalom899fcc72020-10-19 14:38:18 -07009957 /* If this action occurs before a group is started, the callback should
9958 * be preserved, or GROUP-STARTED event would be lost. If this action
9959 * occurs after a group is started, these pointers are all NULL and
9960 * harmless. */
9961 ap_configured_cb = wpa_s->ap_configured_cb;
9962 ap_configured_cb_ctx = wpa_s->ap_configured_cb_ctx;
9963 ap_configured_cb_data = wpa_s->ap_configured_cb_data;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08009964 wpa_supplicant_ap_deinit(wpa_s);
9965
9966 /* Reselect the GO frequency */
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08009967 if (wpas_p2p_init_go_params(wpa_s, &params, 0, 0, 0, 0, 0, 0, 0,
9968 NULL)) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08009969 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Failed to reselect freq");
9970 wpas_p2p_group_delete(wpa_s,
9971 P2P_GROUP_REMOVAL_GO_LEAVE_CHANNEL);
9972 return;
9973 }
9974 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: New freq selected for the GO (%u MHz)",
9975 params.freq);
9976
9977 if (params.freq &&
9978 !p2p_supported_freq_go(wpa_s->global->p2p, params.freq)) {
9979 wpa_printf(MSG_DEBUG,
9980 "P2P: Selected freq (%u MHz) is not valid for P2P",
9981 params.freq);
9982 wpas_p2p_group_delete(wpa_s,
9983 P2P_GROUP_REMOVAL_GO_LEAVE_CHANNEL);
9984 return;
9985 }
9986
Hai Shalom899fcc72020-10-19 14:38:18 -07009987 /* Restore preserved callback parameters */
Jimmy Chen4b2a5022020-08-26 18:55:45 +08009988 wpa_s->ap_configured_cb = ap_configured_cb;
9989 wpa_s->ap_configured_cb_ctx = ap_configured_cb_ctx;
9990 wpa_s->ap_configured_cb_data = ap_configured_cb_data;
Hai Shalom899fcc72020-10-19 14:38:18 -07009991
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08009992 /* Update the frequency */
9993 current_ssid->frequency = params.freq;
9994 wpa_s->connect_without_scan = current_ssid;
9995 wpa_s->reassociate = 1;
9996 wpa_s->disconnected = 0;
9997 wpa_supplicant_req_scan(wpa_s, 0, 0);
9998}
9999
10000
10001static void wpas_p2p_move_go(void *eloop_ctx, void *timeout_ctx)
10002{
10003 struct wpa_supplicant *wpa_s = eloop_ctx;
10004
10005 if (!wpa_s->ap_iface || !wpa_s->current_ssid)
Dmitry Shmidt43cb5782014-06-16 16:23:22 -070010006 return;
10007
Dmitry Shmidtd80a4012015-11-05 16:35:40 -080010008 wpas_p2p_go_update_common_freqs(wpa_s);
10009
10010 /* Do not move GO in the middle of a CSA */
10011 if (hostapd_csa_in_progress(wpa_s->ap_iface)) {
10012 wpa_printf(MSG_DEBUG,
10013 "P2P: CSA is in progress - not moving GO");
Dmitry Shmidt43cb5782014-06-16 16:23:22 -070010014 return;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -080010015 }
10016
10017 /*
10018 * First, try a channel switch flow. If it is not supported or fails,
10019 * take down the GO and bring it up again.
10020 */
10021 if (wpas_p2p_move_go_csa(wpa_s) < 0)
10022 wpas_p2p_move_go_no_csa(wpa_s);
10023}
10024
10025
10026static void wpas_p2p_reconsider_moving_go(void *eloop_ctx, void *timeout_ctx)
10027{
10028 struct wpa_supplicant *wpa_s = eloop_ctx;
10029 struct wpa_used_freq_data *freqs = NULL;
10030 unsigned int num = wpa_s->num_multichan_concurrent;
Dmitry Shmidt43cb5782014-06-16 16:23:22 -070010031
10032 freqs = os_calloc(num, sizeof(struct wpa_used_freq_data));
10033 if (!freqs)
10034 return;
10035
Sunil Ravi77d572f2023-01-17 23:58:31 +000010036 num = get_shared_radio_freqs_data(wpa_s, freqs, num, false);
Dmitry Shmidt43cb5782014-06-16 16:23:22 -070010037
Dmitry Shmidtd80a4012015-11-05 16:35:40 -080010038 /* Previous attempt to move a GO was not possible -- try again. */
10039 wpas_p2p_consider_moving_gos(wpa_s, freqs, num,
10040 WPAS_P2P_CHANNEL_UPDATE_ANY);
10041
Dmitry Shmidt43cb5782014-06-16 16:23:22 -070010042 os_free(freqs);
10043}
10044
10045
Dmitry Shmidtd80a4012015-11-05 16:35:40 -080010046/*
10047 * Consider moving a GO from its currently used frequency:
10048 * 1. It is possible that due to regulatory consideration the frequency
10049 * can no longer be used and there is a need to evacuate the GO.
10050 * 2. It is possible that due to MCC considerations, it would be preferable
10051 * to move the GO to a channel that is currently used by some other
10052 * station interface.
10053 *
10054 * In case a frequency that became invalid is once again valid, cancel a
10055 * previously initiated GO frequency change.
10056 */
10057static void wpas_p2p_consider_moving_one_go(struct wpa_supplicant *wpa_s,
10058 struct wpa_used_freq_data *freqs,
10059 unsigned int num)
10060{
10061 unsigned int i, invalid_freq = 0, policy_move = 0, flags = 0;
10062 unsigned int timeout;
10063 int freq;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -070010064 int dfs_offload;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -080010065
10066 wpas_p2p_go_update_common_freqs(wpa_s);
10067
10068 freq = wpa_s->current_ssid->frequency;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -070010069 dfs_offload = (wpa_s->drv_flags & WPA_DRIVER_FLAGS_DFS_OFFLOAD) &&
Roshan Pius3a1667e2018-07-03 15:17:14 -070010070 ieee80211_is_dfs(freq, wpa_s->hw.modes, wpa_s->hw.num_modes);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -080010071 for (i = 0, invalid_freq = 0; i < num; i++) {
10072 if (freqs[i].freq == freq) {
10073 flags = freqs[i].flags;
10074
10075 /* The channel is invalid, must change it */
Dmitry Shmidtd2986c22017-10-23 14:22:09 -070010076 if (!p2p_supported_freq_go(wpa_s->global->p2p, freq) &&
10077 !dfs_offload) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -080010078 wpa_dbg(wpa_s, MSG_DEBUG,
10079 "P2P: Freq=%d MHz no longer valid for GO",
10080 freq);
10081 invalid_freq = 1;
10082 }
10083 } else if (freqs[i].flags == 0) {
10084 /* Freq is not used by any other station interface */
10085 continue;
10086 } else if (!p2p_supported_freq(wpa_s->global->p2p,
Dmitry Shmidtd2986c22017-10-23 14:22:09 -070010087 freqs[i].freq) && !dfs_offload) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -080010088 /* Freq is not valid for P2P use cases */
10089 continue;
10090 } else if (wpa_s->conf->p2p_go_freq_change_policy ==
10091 P2P_GO_FREQ_MOVE_SCM) {
10092 policy_move = 1;
10093 } else if (wpa_s->conf->p2p_go_freq_change_policy ==
10094 P2P_GO_FREQ_MOVE_SCM_PEER_SUPPORTS &&
10095 wpas_p2p_go_is_peer_freq(wpa_s, freqs[i].freq)) {
10096 policy_move = 1;
10097 } else if ((wpa_s->conf->p2p_go_freq_change_policy ==
10098 P2P_GO_FREQ_MOVE_SCM_ECSA) &&
10099 wpas_p2p_go_is_peer_freq(wpa_s, freqs[i].freq)) {
10100 if (!p2p_get_group_num_members(wpa_s->p2p_group)) {
10101 policy_move = 1;
10102 } else if ((wpa_s->drv_flags &
10103 WPA_DRIVER_FLAGS_AP_CSA) &&
10104 wpas_p2p_go_clients_support_ecsa(wpa_s)) {
10105 u8 chan;
10106
10107 /*
10108 * We do not support CSA between bands, so move
10109 * GO only within the same band.
10110 */
10111 if (wpa_s->ap_iface->current_mode->mode ==
10112 ieee80211_freq_to_chan(freqs[i].freq,
10113 &chan))
10114 policy_move = 1;
10115 }
10116 }
10117 }
10118
10119 wpa_dbg(wpa_s, MSG_DEBUG,
10120 "P2P: GO move: invalid_freq=%u, policy_move=%u, flags=0x%X",
10121 invalid_freq, policy_move, flags);
10122
10123 /*
10124 * The channel is valid, or we are going to have a policy move, so
10125 * cancel timeout.
10126 */
10127 if (!invalid_freq || policy_move) {
10128 wpa_dbg(wpa_s, MSG_DEBUG,
10129 "P2P: Cancel a GO move from freq=%d MHz", freq);
10130 eloop_cancel_timeout(wpas_p2p_move_go, wpa_s, NULL);
10131
10132 if (wpas_p2p_in_progress(wpa_s)) {
10133 wpa_dbg(wpa_s, MSG_DEBUG,
10134 "P2P: GO move: policy CS is not allowed - setting timeout to re-consider GO move");
10135 eloop_cancel_timeout(wpas_p2p_reconsider_moving_go,
10136 wpa_s, NULL);
10137 eloop_register_timeout(P2P_RECONSIDER_GO_MOVE_DELAY, 0,
10138 wpas_p2p_reconsider_moving_go,
10139 wpa_s, NULL);
10140 return;
10141 }
10142 }
10143
10144 if (!invalid_freq && (!policy_move || flags != 0)) {
10145 wpa_dbg(wpa_s, MSG_DEBUG,
10146 "P2P: Not initiating a GO frequency change");
10147 return;
10148 }
10149
10150 /*
10151 * Do not consider moving GO if it is in the middle of a CSA. When the
10152 * CSA is finished this flow should be retriggered.
10153 */
10154 if (hostapd_csa_in_progress(wpa_s->ap_iface)) {
10155 wpa_dbg(wpa_s, MSG_DEBUG,
10156 "P2P: Not initiating a GO frequency change - CSA is in progress");
10157 return;
10158 }
10159
10160 if (invalid_freq && !wpas_p2p_disallowed_freq(wpa_s->global, freq))
10161 timeout = P2P_GO_FREQ_CHANGE_TIME;
10162 else
10163 timeout = 0;
10164
10165 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Move GO from freq=%d MHz in %d secs",
10166 freq, timeout);
10167 eloop_cancel_timeout(wpas_p2p_move_go, wpa_s, NULL);
10168 eloop_register_timeout(timeout, 0, wpas_p2p_move_go, wpa_s, NULL);
10169}
10170
10171
10172static void wpas_p2p_consider_moving_gos(struct wpa_supplicant *wpa_s,
10173 struct wpa_used_freq_data *freqs,
10174 unsigned int num,
10175 enum wpas_p2p_channel_update_trig trig)
10176{
10177 struct wpa_supplicant *ifs;
10178
10179 eloop_cancel_timeout(wpas_p2p_reconsider_moving_go, ELOOP_ALL_CTX,
10180 NULL);
10181
10182 /*
10183 * Travers all the radio interfaces, and for each GO interface, check
10184 * if there is a need to move the GO from the frequency it is using,
10185 * or in case the frequency is valid again, cancel the evacuation flow.
10186 */
10187 dl_list_for_each(ifs, &wpa_s->radio->ifaces, struct wpa_supplicant,
10188 radio_list) {
10189 if (ifs->current_ssid == NULL ||
10190 ifs->current_ssid->mode != WPAS_MODE_P2P_GO)
10191 continue;
10192
10193 /*
10194 * The GO was just started or completed channel switch, no need
10195 * to move it.
10196 */
10197 if (wpa_s == ifs &&
10198 (trig == WPAS_P2P_CHANNEL_UPDATE_STATE_CHANGE ||
10199 trig == WPAS_P2P_CHANNEL_UPDATE_CS)) {
10200 wpa_dbg(wpa_s, MSG_DEBUG,
10201 "P2P: GO move - schedule re-consideration");
10202 eloop_register_timeout(P2P_RECONSIDER_GO_MOVE_DELAY, 0,
10203 wpas_p2p_reconsider_moving_go,
10204 wpa_s, NULL);
10205 continue;
10206 }
10207
10208 wpas_p2p_consider_moving_one_go(ifs, freqs, num);
10209 }
10210}
10211
10212
10213void wpas_p2p_indicate_state_change(struct wpa_supplicant *wpa_s)
10214{
10215 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
10216 return;
10217
10218 wpas_p2p_update_channel_list(wpa_s,
10219 WPAS_P2P_CHANNEL_UPDATE_STATE_CHANGE);
10220}
10221
10222
Dmitry Shmidt43cb5782014-06-16 16:23:22 -070010223void wpas_p2p_deinit_iface(struct wpa_supplicant *wpa_s)
10224{
Dmitry Shmidt43cb5782014-06-16 16:23:22 -070010225 if (wpa_s == wpa_s->global->p2p_init_wpa_s && wpa_s->global->p2p) {
10226 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Disable P2P since removing "
10227 "the management interface is being removed");
10228 wpas_p2p_deinit_global(wpa_s->global);
10229 }
10230}
10231
10232
10233void wpas_p2p_ap_deinit(struct wpa_supplicant *wpa_s)
10234{
10235 if (wpa_s->ap_iface->bss)
10236 wpa_s->ap_iface->bss[0]->p2p_group = NULL;
10237 wpas_p2p_group_deinit(wpa_s);
10238}
Dmitry Shmidt58d12ad2016-07-28 10:07:03 -070010239
10240
10241int wpas_p2p_lo_start(struct wpa_supplicant *wpa_s, unsigned int freq,
10242 unsigned int period, unsigned int interval,
10243 unsigned int count)
10244{
10245 struct p2p_data *p2p = wpa_s->global->p2p;
10246 u8 *device_types;
10247 size_t dev_types_len;
10248 struct wpabuf *buf;
10249 int ret;
10250
10251 if (wpa_s->p2p_lo_started) {
10252 wpa_dbg(wpa_s, MSG_DEBUG,
10253 "P2P Listen offload is already started");
10254 return 0;
10255 }
10256
10257 if (wpa_s->global->p2p == NULL ||
10258 !(wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_LISTEN_OFFLOAD)) {
10259 wpa_printf(MSG_DEBUG, "P2P: Listen offload not supported");
10260 return -1;
10261 }
10262
10263 if (!p2p_supported_freq(wpa_s->global->p2p, freq)) {
10264 wpa_printf(MSG_ERROR, "P2P: Input channel not supported: %u",
10265 freq);
10266 return -1;
10267 }
10268
10269 /* Get device type */
10270 dev_types_len = (wpa_s->conf->num_sec_device_types + 1) *
10271 WPS_DEV_TYPE_LEN;
10272 device_types = os_malloc(dev_types_len);
10273 if (!device_types)
10274 return -1;
10275 os_memcpy(device_types, wpa_s->conf->device_type, WPS_DEV_TYPE_LEN);
10276 os_memcpy(&device_types[WPS_DEV_TYPE_LEN], wpa_s->conf->sec_device_type,
10277 wpa_s->conf->num_sec_device_types * WPS_DEV_TYPE_LEN);
10278
10279 /* Get Probe Response IE(s) */
10280 buf = p2p_build_probe_resp_template(p2p, freq);
10281 if (!buf) {
10282 os_free(device_types);
10283 return -1;
10284 }
10285
10286 ret = wpa_drv_p2p_lo_start(wpa_s, freq, period, interval, count,
10287 device_types, dev_types_len,
10288 wpabuf_mhead_u8(buf), wpabuf_len(buf));
10289 if (ret < 0)
10290 wpa_dbg(wpa_s, MSG_DEBUG,
10291 "P2P: Failed to start P2P listen offload");
10292
10293 os_free(device_types);
10294 wpabuf_free(buf);
10295
10296 if (ret == 0) {
10297 wpa_s->p2p_lo_started = 1;
10298
10299 /* Stop current P2P listen if any */
10300 wpas_stop_listen(wpa_s);
10301 }
10302
10303 return ret;
10304}
10305
10306
10307int wpas_p2p_lo_stop(struct wpa_supplicant *wpa_s)
10308{
10309 int ret;
10310
10311 if (!wpa_s->p2p_lo_started)
10312 return 0;
10313
10314 ret = wpa_drv_p2p_lo_stop(wpa_s);
10315 if (ret < 0)
10316 wpa_dbg(wpa_s, MSG_DEBUG,
10317 "P2P: Failed to stop P2P listen offload");
10318
10319 wpa_s->p2p_lo_started = 0;
10320 return ret;
10321}