blob: 768b9178fe5e2b50d5a017fd384c2c29715d3d73 [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 */
Sunil Ravi2a14cf12023-11-21 00:54:38 +00001091 wpas_notify_network_removed(wpa_s, ssid);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001092 wpa_config_remove_network(wpa_s->conf, id);
1093 wpa_supplicant_clear_status(wpa_s);
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08001094 wpa_supplicant_cancel_sched_scan(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001095 } else {
1096 wpa_printf(MSG_DEBUG, "P2P: Temporary group network not "
1097 "found");
1098 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07001099 if (wpa_s->ap_iface)
1100 wpa_supplicant_ap_deinit(wpa_s);
1101 else
1102 wpa_drv_deinit_p2p_cli(wpa_s);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001103
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001104 os_memset(wpa_s->go_dev_addr, 0, ETH_ALEN);
1105
Sunil Ravi99c035e2024-07-12 01:42:03 +00001106 wpa_s->p2p_go_no_pri_sec_switch = 0;
1107
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001108 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001109}
1110
1111
1112static int wpas_p2p_persistent_group(struct wpa_supplicant *wpa_s,
1113 u8 *go_dev_addr,
1114 const u8 *ssid, size_t ssid_len)
1115{
1116 struct wpa_bss *bss;
1117 const u8 *bssid;
1118 struct wpabuf *p2p;
1119 u8 group_capab;
1120 const u8 *addr;
1121
1122 if (wpa_s->go_params)
1123 bssid = wpa_s->go_params->peer_interface_addr;
1124 else
1125 bssid = wpa_s->bssid;
1126
1127 bss = wpa_bss_get(wpa_s, bssid, ssid, ssid_len);
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001128 if (bss == NULL && wpa_s->go_params &&
1129 !is_zero_ether_addr(wpa_s->go_params->peer_device_addr))
1130 bss = wpa_bss_get_p2p_dev_addr(
1131 wpa_s, wpa_s->go_params->peer_device_addr);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001132 if (bss == NULL) {
1133 u8 iface_addr[ETH_ALEN];
1134 if (p2p_get_interface_addr(wpa_s->global->p2p, bssid,
1135 iface_addr) == 0)
1136 bss = wpa_bss_get(wpa_s, iface_addr, ssid, ssid_len);
1137 }
1138 if (bss == NULL) {
1139 wpa_printf(MSG_DEBUG, "P2P: Could not figure out whether "
1140 "group is persistent - BSS " MACSTR " not found",
1141 MAC2STR(bssid));
1142 return 0;
1143 }
1144
1145 p2p = wpa_bss_get_vendor_ie_multi(bss, P2P_IE_VENDOR_TYPE);
Dmitry Shmidt96571392013-10-14 12:54:46 -07001146 if (p2p == NULL)
1147 p2p = wpa_bss_get_vendor_ie_multi_beacon(bss,
1148 P2P_IE_VENDOR_TYPE);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001149 if (p2p == NULL) {
1150 wpa_printf(MSG_DEBUG, "P2P: Could not figure out whether "
1151 "group is persistent - BSS " MACSTR
1152 " did not include P2P IE", MAC2STR(bssid));
1153 wpa_hexdump(MSG_DEBUG, "P2P: Probe Response IEs",
Hai Shalom60840252021-02-19 19:02:11 -08001154 wpa_bss_ie_ptr(bss), bss->ie_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001155 wpa_hexdump(MSG_DEBUG, "P2P: Beacon IEs",
Hai Shalom60840252021-02-19 19:02:11 -08001156 wpa_bss_ie_ptr(bss) + bss->ie_len,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001157 bss->beacon_ie_len);
1158 return 0;
1159 }
1160
1161 group_capab = p2p_get_group_capab(p2p);
1162 addr = p2p_get_go_dev_addr(p2p);
1163 wpa_printf(MSG_DEBUG, "P2P: Checking whether group is persistent: "
1164 "group_capab=0x%x", group_capab);
1165 if (addr) {
1166 os_memcpy(go_dev_addr, addr, ETH_ALEN);
1167 wpa_printf(MSG_DEBUG, "P2P: GO Device Address " MACSTR,
1168 MAC2STR(addr));
1169 } else
1170 os_memset(go_dev_addr, 0, ETH_ALEN);
1171 wpabuf_free(p2p);
1172
1173 wpa_printf(MSG_DEBUG, "P2P: BSS " MACSTR " group_capab=0x%x "
1174 "go_dev_addr=" MACSTR,
1175 MAC2STR(bssid), group_capab, MAC2STR(go_dev_addr));
1176
Dmitry Shmidt7f2c7532016-08-15 09:48:12 -07001177 return !!(group_capab & P2P_GROUP_CAPAB_PERSISTENT_GROUP);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001178}
1179
1180
Jouni Malinen75ecf522011-06-27 15:19:46 -07001181static int wpas_p2p_store_persistent_group(struct wpa_supplicant *wpa_s,
1182 struct wpa_ssid *ssid,
1183 const u8 *go_dev_addr)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001184{
1185 struct wpa_ssid *s;
1186 int changed = 0;
1187
1188 wpa_printf(MSG_DEBUG, "P2P: Storing credentials for a persistent "
1189 "group (GO Dev Addr " MACSTR ")", MAC2STR(go_dev_addr));
1190 for (s = wpa_s->conf->ssid; s; s = s->next) {
1191 if (s->disabled == 2 &&
Sunil Ravib0ac25f2024-07-12 01:42:03 +00001192 ether_addr_equal(go_dev_addr, s->bssid) &&
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001193 s->ssid_len == ssid->ssid_len &&
1194 os_memcmp(ssid->ssid, s->ssid, ssid->ssid_len) == 0)
1195 break;
1196 }
1197
1198 if (s) {
1199 wpa_printf(MSG_DEBUG, "P2P: Update existing persistent group "
1200 "entry");
1201 if (ssid->passphrase && !s->passphrase)
1202 changed = 1;
1203 else if (ssid->passphrase && s->passphrase &&
1204 os_strcmp(ssid->passphrase, s->passphrase) != 0)
1205 changed = 1;
1206 } else {
1207 wpa_printf(MSG_DEBUG, "P2P: Create a new persistent group "
1208 "entry");
1209 changed = 1;
1210 s = wpa_config_add_network(wpa_s->conf);
1211 if (s == NULL)
Jouni Malinen75ecf522011-06-27 15:19:46 -07001212 return -1;
1213
1214 /*
1215 * Instead of network_added we emit persistent_group_added
1216 * notification. Also to keep the defense checks in
1217 * persistent_group obj registration method, we set the
1218 * relevant flags in s to designate it as a persistent group.
1219 */
1220 s->p2p_group = 1;
1221 s->p2p_persistent_group = 1;
1222 wpas_notify_persistent_group_added(wpa_s, s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001223 wpa_config_set_network_defaults(s);
1224 }
1225
1226 s->p2p_group = 1;
1227 s->p2p_persistent_group = 1;
1228 s->disabled = 2;
1229 s->bssid_set = 1;
1230 os_memcpy(s->bssid, go_dev_addr, ETH_ALEN);
1231 s->mode = ssid->mode;
1232 s->auth_alg = WPA_AUTH_ALG_OPEN;
1233 s->key_mgmt = WPA_KEY_MGMT_PSK;
1234 s->proto = WPA_PROTO_RSN;
Dmitry Shmidte4663042016-04-04 10:07:49 -07001235 s->pbss = ssid->pbss;
1236 s->pairwise_cipher = ssid->pbss ? WPA_CIPHER_GCMP : WPA_CIPHER_CCMP;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001237 s->export_keys = 1;
1238 if (ssid->passphrase) {
1239 os_free(s->passphrase);
1240 s->passphrase = os_strdup(ssid->passphrase);
1241 }
1242 if (ssid->psk_set) {
1243 s->psk_set = 1;
1244 os_memcpy(s->psk, ssid->psk, 32);
1245 }
1246 if (s->passphrase && !s->psk_set)
1247 wpa_config_update_psk(s);
1248 if (s->ssid == NULL || s->ssid_len < ssid->ssid_len) {
1249 os_free(s->ssid);
1250 s->ssid = os_malloc(ssid->ssid_len);
1251 }
1252 if (s->ssid) {
1253 s->ssid_len = ssid->ssid_len;
1254 os_memcpy(s->ssid, ssid->ssid, s->ssid_len);
1255 }
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07001256 if (ssid->mode == WPAS_MODE_P2P_GO && wpa_s->global->add_psk) {
1257 dl_list_add(&s->psk_list, &wpa_s->global->add_psk->list);
1258 wpa_s->global->add_psk = NULL;
1259 changed = 1;
1260 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001261
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001262 if (changed && wpa_s->conf->update_config &&
1263 wpa_config_write(wpa_s->confname, wpa_s->conf)) {
1264 wpa_printf(MSG_DEBUG, "P2P: Failed to update configuration");
1265 }
Jouni Malinen75ecf522011-06-27 15:19:46 -07001266
1267 return s->id;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001268}
1269
1270
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001271static void wpas_p2p_add_persistent_group_client(struct wpa_supplicant *wpa_s,
1272 const u8 *addr)
1273{
1274 struct wpa_ssid *ssid, *s;
1275 u8 *n;
1276 size_t i;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001277 int found = 0;
Dmitry Shmidt8bd70b72015-05-26 16:02:19 -07001278 struct wpa_supplicant *p2p_wpa_s = wpa_s->global->p2p_init_wpa_s;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001279
1280 ssid = wpa_s->current_ssid;
1281 if (ssid == NULL || ssid->mode != WPAS_MODE_P2P_GO ||
1282 !ssid->p2p_persistent_group)
1283 return;
1284
Dmitry Shmidt8bd70b72015-05-26 16:02:19 -07001285 for (s = p2p_wpa_s->conf->ssid; s; s = s->next) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001286 if (s->disabled != 2 || s->mode != WPAS_MODE_P2P_GO)
1287 continue;
1288
1289 if (s->ssid_len == ssid->ssid_len &&
1290 os_memcmp(s->ssid, ssid->ssid, s->ssid_len) == 0)
1291 break;
1292 }
1293
1294 if (s == NULL)
1295 return;
1296
1297 for (i = 0; s->p2p_client_list && i < s->num_p2p_clients; i++) {
Sunil Ravib0ac25f2024-07-12 01:42:03 +00001298 if (!ether_addr_equal(s->p2p_client_list + i * 2 * ETH_ALEN,
1299 addr))
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001300 continue;
1301
1302 if (i == s->num_p2p_clients - 1)
1303 return; /* already the most recent entry */
1304
1305 /* move the entry to mark it most recent */
Dmitry Shmidtff787d52015-01-12 13:01:47 -08001306 os_memmove(s->p2p_client_list + i * 2 * ETH_ALEN,
1307 s->p2p_client_list + (i + 1) * 2 * ETH_ALEN,
1308 (s->num_p2p_clients - i - 1) * 2 * ETH_ALEN);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001309 os_memcpy(s->p2p_client_list +
Dmitry Shmidtff787d52015-01-12 13:01:47 -08001310 (s->num_p2p_clients - 1) * 2 * ETH_ALEN, addr,
1311 ETH_ALEN);
1312 os_memset(s->p2p_client_list +
1313 (s->num_p2p_clients - 1) * 2 * ETH_ALEN + ETH_ALEN,
1314 0xff, ETH_ALEN);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001315 found = 1;
1316 break;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001317 }
1318
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001319 if (!found && s->num_p2p_clients < P2P_MAX_STORED_CLIENTS) {
1320 n = os_realloc_array(s->p2p_client_list,
Dmitry Shmidtff787d52015-01-12 13:01:47 -08001321 s->num_p2p_clients + 1, 2 * ETH_ALEN);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001322 if (n == NULL)
1323 return;
Dmitry Shmidtff787d52015-01-12 13:01:47 -08001324 os_memcpy(n + s->num_p2p_clients * 2 * ETH_ALEN, addr,
1325 ETH_ALEN);
1326 os_memset(n + s->num_p2p_clients * 2 * ETH_ALEN + ETH_ALEN,
1327 0xff, ETH_ALEN);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001328 s->p2p_client_list = n;
1329 s->num_p2p_clients++;
Dmitry Shmidt6aa8ae42014-07-07 09:31:33 -07001330 } else if (!found && s->p2p_client_list) {
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001331 /* Not enough room for an additional entry - drop the oldest
1332 * entry */
1333 os_memmove(s->p2p_client_list,
Dmitry Shmidtff787d52015-01-12 13:01:47 -08001334 s->p2p_client_list + 2 * ETH_ALEN,
1335 (s->num_p2p_clients - 1) * 2 * ETH_ALEN);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001336 os_memcpy(s->p2p_client_list +
Dmitry Shmidtff787d52015-01-12 13:01:47 -08001337 (s->num_p2p_clients - 1) * 2 * ETH_ALEN,
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001338 addr, ETH_ALEN);
Dmitry Shmidtff787d52015-01-12 13:01:47 -08001339 os_memset(s->p2p_client_list +
1340 (s->num_p2p_clients - 1) * 2 * ETH_ALEN + ETH_ALEN,
1341 0xff, ETH_ALEN);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001342 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001343
Dmitry Shmidt8bd70b72015-05-26 16:02:19 -07001344 if (p2p_wpa_s->conf->update_config &&
1345 wpa_config_write(p2p_wpa_s->confname, p2p_wpa_s->conf))
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001346 wpa_printf(MSG_DEBUG, "P2P: Failed to update configuration");
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001347}
1348
1349
Dmitry Shmidt7f0b69e2014-07-28 10:35:20 -07001350static void wpas_p2p_group_started(struct wpa_supplicant *wpa_s,
1351 int go, struct wpa_ssid *ssid, int freq,
1352 const u8 *psk, const char *passphrase,
1353 const u8 *go_dev_addr, int persistent,
1354 const char *extra)
1355{
1356 const char *ssid_txt;
1357 char psk_txt[65];
1358
1359 if (psk)
1360 wpa_snprintf_hex(psk_txt, sizeof(psk_txt), psk, 32);
1361 else
1362 psk_txt[0] = '\0';
1363
1364 if (ssid)
1365 ssid_txt = wpa_ssid_txt(ssid->ssid, ssid->ssid_len);
1366 else
1367 ssid_txt = "";
1368
1369 if (passphrase && passphrase[0] == '\0')
1370 passphrase = NULL;
1371
1372 /*
1373 * Include PSK/passphrase only in the control interface message and
1374 * leave it out from the debug log entry.
1375 */
Dmitry Shmidt9c175262016-03-03 10:20:07 -08001376 wpa_msg_global_ctrl(wpa_s->p2pdev, MSG_INFO,
Dmitry Shmidt7f0b69e2014-07-28 10:35:20 -07001377 P2P_EVENT_GROUP_STARTED
1378 "%s %s ssid=\"%s\" freq=%d%s%s%s%s%s go_dev_addr="
1379 MACSTR "%s%s",
1380 wpa_s->ifname, go ? "GO" : "client", ssid_txt, freq,
1381 psk ? " psk=" : "", psk_txt,
1382 passphrase ? " passphrase=\"" : "",
1383 passphrase ? passphrase : "",
1384 passphrase ? "\"" : "",
1385 MAC2STR(go_dev_addr),
1386 persistent ? " [PERSISTENT]" : "", extra);
1387 wpa_printf(MSG_INFO, P2P_EVENT_GROUP_STARTED
1388 "%s %s ssid=\"%s\" freq=%d go_dev_addr=" MACSTR "%s%s",
1389 wpa_s->ifname, go ? "GO" : "client", ssid_txt, freq,
1390 MAC2STR(go_dev_addr), persistent ? " [PERSISTENT]" : "",
1391 extra);
1392}
1393
1394
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001395static void wpas_group_formation_completed(struct wpa_supplicant *wpa_s,
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07001396 int success, int already_deleted)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001397{
1398 struct wpa_ssid *ssid;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001399 int client;
1400 int persistent;
1401 u8 go_dev_addr[ETH_ALEN];
1402
1403 /*
1404 * This callback is likely called for the main interface. Update wpa_s
1405 * to use the group interface if a new interface was created for the
1406 * group.
1407 */
1408 if (wpa_s->global->p2p_group_formation)
1409 wpa_s = wpa_s->global->p2p_group_formation;
Dmitry Shmidt56052862013-10-04 10:23:25 -07001410 if (wpa_s->p2p_go_group_formation_completed) {
1411 wpa_s->global->p2p_group_formation = NULL;
1412 wpa_s->p2p_in_provisioning = 0;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001413 } else if (wpa_s->p2p_in_provisioning && !success) {
1414 wpa_msg(wpa_s, MSG_DEBUG,
1415 "P2P: Stop provisioning state due to failure");
1416 wpa_s->p2p_in_provisioning = 0;
Dmitry Shmidt56052862013-10-04 10:23:25 -07001417 }
Dmitry Shmidt21de2142014-04-08 10:50:52 -07001418 wpa_s->p2p_in_invitation = 0;
Matthew Wang06b42472022-11-10 06:56:31 +00001419 wpa_s->p2p_retry_limit = 0;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001420 wpa_s->group_formation_reported = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001421
1422 if (!success) {
Dmitry Shmidt9c175262016-03-03 10:20:07 -08001423 wpa_msg_global(wpa_s->p2pdev, MSG_INFO,
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07001424 P2P_EVENT_GROUP_FORMATION_FAILURE);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001425 wpas_notify_p2p_group_formation_failure(wpa_s, "");
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07001426 if (already_deleted)
1427 return;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001428 wpas_p2p_group_delete(wpa_s,
1429 P2P_GROUP_REMOVAL_FORMATION_FAILED);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001430 return;
1431 }
1432
Dmitry Shmidt9c175262016-03-03 10:20:07 -08001433 wpa_msg_global(wpa_s->p2pdev, MSG_INFO,
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07001434 P2P_EVENT_GROUP_FORMATION_SUCCESS);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001435
1436 ssid = wpa_s->current_ssid;
1437 if (ssid && ssid->mode == WPAS_MODE_P2P_GROUP_FORMATION) {
1438 ssid->mode = WPAS_MODE_P2P_GO;
1439 p2p_group_notif_formation_done(wpa_s->p2p_group);
1440 wpa_supplicant_ap_mac_addr_filter(wpa_s, NULL);
1441 }
1442
1443 persistent = 0;
1444 if (ssid) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001445 client = ssid->mode == WPAS_MODE_INFRA;
1446 if (ssid->mode == WPAS_MODE_P2P_GO) {
1447 persistent = ssid->p2p_persistent_group;
Dmitry Shmidt497c1d52011-07-21 15:19:46 -07001448 os_memcpy(go_dev_addr, wpa_s->global->p2p_dev_addr,
1449 ETH_ALEN);
George Burgess IV0d2d1bf2021-11-01 03:08:33 -07001450 } else {
1451 os_memset(go_dev_addr, 0, ETH_ALEN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001452 persistent = wpas_p2p_persistent_group(wpa_s,
1453 go_dev_addr,
1454 ssid->ssid,
1455 ssid->ssid_len);
George Burgess IV0d2d1bf2021-11-01 03:08:33 -07001456 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001457 } else {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001458 client = wpa_s->p2p_group_interface ==
1459 P2P_GROUP_INTERFACE_CLIENT;
1460 os_memset(go_dev_addr, 0, ETH_ALEN);
1461 }
1462
1463 wpa_s->show_group_started = 0;
1464 if (client) {
1465 /*
1466 * Indicate event only after successfully completed 4-way
1467 * handshake, i.e., when the interface is ready for data
1468 * packets.
1469 */
1470 wpa_s->show_group_started = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001471 } else {
Dmitry Shmidt7f0b69e2014-07-28 10:35:20 -07001472 wpas_p2p_group_started(wpa_s, 1, ssid,
1473 ssid ? ssid->frequency : 0,
1474 ssid && ssid->passphrase == NULL &&
1475 ssid->psk_set ? ssid->psk : NULL,
1476 ssid ? ssid->passphrase : NULL,
1477 go_dev_addr, persistent, "");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001478 wpas_p2p_cross_connect_setup(wpa_s);
1479 wpas_p2p_set_group_idle_timeout(wpa_s);
1480 }
1481
1482 if (persistent)
Dmitry Shmidtd5ab1b52016-06-21 12:38:41 -07001483 wpas_p2p_store_persistent_group(wpa_s->p2pdev,
1484 ssid, go_dev_addr);
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07001485 else {
1486 os_free(wpa_s->global->add_psk);
1487 wpa_s->global->add_psk = NULL;
1488 }
Dmitry Shmidtd5ab1b52016-06-21 12:38:41 -07001489
Dmitry Shmidt92c368d2013-08-29 12:37:21 -07001490 if (!client) {
Dmitry Shmidtabb90a32016-12-05 15:34:39 -08001491 wpas_notify_p2p_group_started(wpa_s, ssid, persistent, 0, NULL);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001492 os_get_reltime(&wpa_s->global->p2p_go_wait_client);
Dmitry Shmidt92c368d2013-08-29 12:37:21 -07001493 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001494}
1495
1496
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001497struct send_action_work {
1498 unsigned int freq;
1499 u8 dst[ETH_ALEN];
1500 u8 src[ETH_ALEN];
1501 u8 bssid[ETH_ALEN];
1502 size_t len;
1503 unsigned int wait_time;
1504 u8 buf[0];
1505};
1506
1507
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001508static void wpas_p2p_free_send_action_work(struct wpa_supplicant *wpa_s)
1509{
1510 struct send_action_work *awork = wpa_s->p2p_send_action_work->ctx;
1511
1512 wpa_printf(MSG_DEBUG,
1513 "P2P: Free Action frame radio work @%p (freq=%u dst="
1514 MACSTR " src=" MACSTR " bssid=" MACSTR " wait_time=%u)",
1515 wpa_s->p2p_send_action_work, awork->freq,
1516 MAC2STR(awork->dst), MAC2STR(awork->src),
1517 MAC2STR(awork->bssid), awork->wait_time);
1518 wpa_hexdump(MSG_DEBUG, "P2P: Freeing pending Action frame",
1519 awork->buf, awork->len);
1520 os_free(awork);
1521 wpa_s->p2p_send_action_work->ctx = NULL;
1522 radio_work_done(wpa_s->p2p_send_action_work);
1523 wpa_s->p2p_send_action_work = NULL;
1524}
1525
1526
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001527static void wpas_p2p_send_action_work_timeout(void *eloop_ctx,
1528 void *timeout_ctx)
1529{
1530 struct wpa_supplicant *wpa_s = eloop_ctx;
1531
1532 if (!wpa_s->p2p_send_action_work)
1533 return;
1534
1535 wpa_printf(MSG_DEBUG, "P2P: Send Action frame radio work timed out");
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001536 wpas_p2p_free_send_action_work(wpa_s);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001537}
1538
1539
Dmitry Shmidt03658832014-08-13 11:03:49 -07001540static void wpas_p2p_action_tx_clear(struct wpa_supplicant *wpa_s)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001541{
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001542 if (wpa_s->p2p_send_action_work) {
1543 struct send_action_work *awork;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001544
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001545 awork = wpa_s->p2p_send_action_work->ctx;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001546 wpa_printf(MSG_DEBUG,
1547 "P2P: Clear Action TX work @%p (wait_time=%u)",
1548 wpa_s->p2p_send_action_work, awork->wait_time);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001549 if (awork->wait_time == 0) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001550 wpas_p2p_free_send_action_work(wpa_s);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001551 } else {
1552 /*
1553 * In theory, this should not be needed, but number of
1554 * places in the P2P code is still using non-zero wait
1555 * time for the last Action frame in the sequence and
1556 * some of these do not call send_action_done().
1557 */
1558 eloop_cancel_timeout(wpas_p2p_send_action_work_timeout,
1559 wpa_s, NULL);
1560 eloop_register_timeout(
1561 0, awork->wait_time * 1000,
1562 wpas_p2p_send_action_work_timeout,
1563 wpa_s, NULL);
1564 }
1565 }
Dmitry Shmidt03658832014-08-13 11:03:49 -07001566}
1567
1568
1569static void wpas_p2p_send_action_tx_status(struct wpa_supplicant *wpa_s,
1570 unsigned int freq,
1571 const u8 *dst, const u8 *src,
1572 const u8 *bssid,
1573 const u8 *data, size_t data_len,
1574 enum offchannel_send_action_result
1575 result)
1576{
1577 enum p2p_send_action_result res = P2P_SEND_ACTION_SUCCESS;
1578
1579 wpas_p2p_action_tx_clear(wpa_s);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001580
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001581 if (wpa_s->global->p2p == NULL || wpa_s->global->p2p_disabled)
1582 return;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001583
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001584 switch (result) {
1585 case OFFCHANNEL_SEND_ACTION_SUCCESS:
1586 res = P2P_SEND_ACTION_SUCCESS;
1587 break;
1588 case OFFCHANNEL_SEND_ACTION_NO_ACK:
1589 res = P2P_SEND_ACTION_NO_ACK;
1590 break;
1591 case OFFCHANNEL_SEND_ACTION_FAILED:
1592 res = P2P_SEND_ACTION_FAILED;
1593 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001594 }
1595
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001596 p2p_send_action_cb(wpa_s->global->p2p, freq, dst, src, bssid, res);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001597
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08001598 if (result != OFFCHANNEL_SEND_ACTION_SUCCESS &&
1599 wpa_s->pending_pd_before_join &&
Sunil Ravib0ac25f2024-07-12 01:42:03 +00001600 (ether_addr_equal(dst, wpa_s->pending_join_dev_addr) ||
1601 ether_addr_equal(dst, wpa_s->pending_join_iface_addr)) &&
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001602 wpa_s->p2p_fallback_to_go_neg) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001603 wpa_s->pending_pd_before_join = 0;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001604 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: No ACK for PD Req "
1605 "during p2p_connect-auto");
Dmitry Shmidt9c175262016-03-03 10:20:07 -08001606 wpa_msg_global(wpa_s->p2pdev, MSG_INFO,
Dmitry Shmidt7f656022015-02-25 14:36:37 -08001607 P2P_EVENT_FALLBACK_TO_GO_NEG
1608 "reason=no-ACK-to-PD-Req");
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001609 wpas_p2p_fallback_to_go_neg(wpa_s, 0);
1610 return;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001611 }
1612}
1613
1614
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001615static void wpas_send_action_cb(struct wpa_radio_work *work, int deinit)
1616{
1617 struct wpa_supplicant *wpa_s = work->wpa_s;
1618 struct send_action_work *awork = work->ctx;
1619
1620 if (deinit) {
Dmitry Shmidtbd14a572014-02-18 10:33:49 -08001621 if (work->started) {
1622 eloop_cancel_timeout(wpas_p2p_send_action_work_timeout,
1623 wpa_s, NULL);
1624 wpa_s->p2p_send_action_work = NULL;
1625 offchannel_send_action_done(wpa_s);
1626 }
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001627 os_free(awork);
1628 return;
1629 }
1630
1631 if (offchannel_send_action(wpa_s, awork->freq, awork->dst, awork->src,
1632 awork->bssid, awork->buf, awork->len,
1633 awork->wait_time,
1634 wpas_p2p_send_action_tx_status, 1) < 0) {
1635 os_free(awork);
1636 radio_work_done(work);
1637 return;
1638 }
1639 wpa_s->p2p_send_action_work = work;
1640}
1641
1642
1643static int wpas_send_action_work(struct wpa_supplicant *wpa_s,
1644 unsigned int freq, const u8 *dst,
1645 const u8 *src, const u8 *bssid, const u8 *buf,
1646 size_t len, unsigned int wait_time)
1647{
1648 struct send_action_work *awork;
1649
Hai Shalom81f62d82019-07-22 12:10:00 -07001650 if (radio_work_pending(wpa_s, "p2p-send-action")) {
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001651 wpa_printf(MSG_DEBUG, "P2P: Cannot schedule new p2p-send-action work since one is already pending");
1652 return -1;
1653 }
1654
1655 awork = os_zalloc(sizeof(*awork) + len);
1656 if (awork == NULL)
1657 return -1;
1658
1659 awork->freq = freq;
1660 os_memcpy(awork->dst, dst, ETH_ALEN);
1661 os_memcpy(awork->src, src, ETH_ALEN);
1662 os_memcpy(awork->bssid, bssid, ETH_ALEN);
1663 awork->len = len;
1664 awork->wait_time = wait_time;
1665 os_memcpy(awork->buf, buf, len);
1666
Hai Shalom81f62d82019-07-22 12:10:00 -07001667 if (radio_add_work(wpa_s, freq, "p2p-send-action", 1,
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001668 wpas_send_action_cb, awork) < 0) {
1669 os_free(awork);
1670 return -1;
1671 }
1672
1673 return 0;
1674}
1675
1676
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001677static int wpas_send_action(void *ctx, unsigned int freq, const u8 *dst,
1678 const u8 *src, const u8 *bssid, const u8 *buf,
Hai Shalom021b0b52019-04-10 11:17:58 -07001679 size_t len, unsigned int wait_time, int *scheduled)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001680{
1681 struct wpa_supplicant *wpa_s = ctx;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001682 int listen_freq = -1, send_freq = -1;
1683
Hai Shalom021b0b52019-04-10 11:17:58 -07001684 if (scheduled)
1685 *scheduled = 0;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001686 if (wpa_s->p2p_listen_work)
1687 listen_freq = wpa_s->p2p_listen_work->freq;
1688 if (wpa_s->p2p_send_action_work)
1689 send_freq = wpa_s->p2p_send_action_work->freq;
1690 if (listen_freq != (int) freq && send_freq != (int) freq) {
Hai Shalom021b0b52019-04-10 11:17:58 -07001691 int res;
1692
1693 wpa_printf(MSG_DEBUG, "P2P: Schedule new radio work for Action frame TX (listen_freq=%d send_freq=%d freq=%u)",
1694 listen_freq, send_freq, freq);
1695 res = wpas_send_action_work(wpa_s, freq, dst, src, bssid, buf,
1696 len, wait_time);
1697 if (res == 0 && scheduled)
1698 *scheduled = 1;
1699 return res;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001700 }
1701
1702 wpa_printf(MSG_DEBUG, "P2P: Use ongoing radio work for Action frame TX");
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001703 return offchannel_send_action(wpa_s, freq, dst, src, bssid, buf, len,
1704 wait_time,
1705 wpas_p2p_send_action_tx_status, 1);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001706}
1707
1708
1709static void wpas_send_action_done(void *ctx)
1710{
1711 struct wpa_supplicant *wpa_s = ctx;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001712
1713 if (wpa_s->p2p_send_action_work) {
1714 eloop_cancel_timeout(wpas_p2p_send_action_work_timeout,
1715 wpa_s, NULL);
1716 os_free(wpa_s->p2p_send_action_work->ctx);
1717 radio_work_done(wpa_s->p2p_send_action_work);
1718 wpa_s->p2p_send_action_work = NULL;
1719 }
1720
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001721 offchannel_send_action_done(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001722}
1723
1724
1725static int wpas_copy_go_neg_results(struct wpa_supplicant *wpa_s,
1726 struct p2p_go_neg_results *params)
1727{
1728 if (wpa_s->go_params == NULL) {
1729 wpa_s->go_params = os_malloc(sizeof(*params));
1730 if (wpa_s->go_params == NULL)
1731 return -1;
1732 }
1733 os_memcpy(wpa_s->go_params, params, sizeof(*params));
1734 return 0;
1735}
1736
1737
1738static void wpas_start_wps_enrollee(struct wpa_supplicant *wpa_s,
1739 struct p2p_go_neg_results *res)
1740{
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001741 wpa_s->group_formation_reported = 0;
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001742 wpa_printf(MSG_DEBUG, "P2P: Start WPS Enrollee for peer " MACSTR
1743 " dev_addr " MACSTR " wps_method %d",
1744 MAC2STR(res->peer_interface_addr),
1745 MAC2STR(res->peer_device_addr), res->wps_method);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001746 wpa_hexdump_ascii(MSG_DEBUG, "P2P: Start WPS Enrollee for SSID",
1747 res->ssid, res->ssid_len);
1748 wpa_supplicant_ap_deinit(wpa_s);
1749 wpas_copy_go_neg_results(wpa_s, res);
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001750 if (res->wps_method == WPS_PBC) {
Hai Shalom021b0b52019-04-10 11:17:58 -07001751 wpas_wps_start_pbc(wpa_s, res->peer_interface_addr, 1, 0);
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001752#ifdef CONFIG_WPS_NFC
1753 } else if (res->wps_method == WPS_NFC) {
1754 wpas_wps_start_nfc(wpa_s, res->peer_device_addr,
1755 res->peer_interface_addr,
Dmitry Shmidt9c175262016-03-03 10:20:07 -08001756 wpa_s->p2pdev->p2p_oob_dev_pw,
1757 wpa_s->p2pdev->p2p_oob_dev_pw_id, 1,
1758 wpa_s->p2pdev->p2p_oob_dev_pw_id ==
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001759 DEV_PW_NFC_CONNECTION_HANDOVER ?
Dmitry Shmidt9c175262016-03-03 10:20:07 -08001760 wpa_s->p2pdev->p2p_peer_oob_pubkey_hash :
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001761 NULL,
1762 NULL, 0, 0);
1763#endif /* CONFIG_WPS_NFC */
1764 } else {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001765 u16 dev_pw_id = DEV_PW_DEFAULT;
Dmitry Shmidt216983b2015-02-06 10:50:36 -08001766 if (wpa_s->p2p_wps_method == WPS_P2PS)
1767 dev_pw_id = DEV_PW_P2PS_DEFAULT;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001768 if (wpa_s->p2p_wps_method == WPS_PIN_KEYPAD)
1769 dev_pw_id = DEV_PW_REGISTRAR_SPECIFIED;
1770 wpas_wps_start_pin(wpa_s, res->peer_interface_addr,
1771 wpa_s->p2p_pin, 1, dev_pw_id);
1772 }
1773}
1774
1775
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07001776static void wpas_p2p_add_psk_list(struct wpa_supplicant *wpa_s,
1777 struct wpa_ssid *ssid)
1778{
1779 struct wpa_ssid *persistent;
1780 struct psk_list_entry *psk;
1781 struct hostapd_data *hapd;
1782
1783 if (!wpa_s->ap_iface)
1784 return;
1785
Dmitry Shmidt9c175262016-03-03 10:20:07 -08001786 persistent = wpas_p2p_get_persistent(wpa_s->p2pdev, NULL, ssid->ssid,
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07001787 ssid->ssid_len);
1788 if (persistent == NULL)
1789 return;
1790
1791 hapd = wpa_s->ap_iface->bss[0];
1792
1793 dl_list_for_each(psk, &persistent->psk_list, struct psk_list_entry,
1794 list) {
1795 struct hostapd_wpa_psk *hpsk;
1796
1797 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Add persistent group PSK entry for "
1798 MACSTR " psk=%d",
1799 MAC2STR(psk->addr), psk->p2p);
1800 hpsk = os_zalloc(sizeof(*hpsk));
1801 if (hpsk == NULL)
1802 break;
1803 os_memcpy(hpsk->psk, psk->psk, PMK_LEN);
1804 if (psk->p2p)
1805 os_memcpy(hpsk->p2p_dev_addr, psk->addr, ETH_ALEN);
1806 else
1807 os_memcpy(hpsk->addr, psk->addr, ETH_ALEN);
1808 hpsk->next = hapd->conf->ssid.wpa_psk;
1809 hapd->conf->ssid.wpa_psk = hpsk;
1810 }
1811}
1812
1813
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001814static void p2p_go_dump_common_freqs(struct wpa_supplicant *wpa_s)
1815{
Roshan Pius3a1667e2018-07-03 15:17:14 -07001816 char buf[20 + P2P_MAX_CHANNELS * 6];
1817 char *pos, *end;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001818 unsigned int i;
Roshan Pius3a1667e2018-07-03 15:17:14 -07001819 int res;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001820
Roshan Pius3a1667e2018-07-03 15:17:14 -07001821 pos = buf;
1822 end = pos + sizeof(buf);
1823 for (i = 0; i < wpa_s->p2p_group_common_freqs_num; i++) {
1824 res = os_snprintf(pos, end - pos, " %d",
1825 wpa_s->p2p_group_common_freqs[i]);
1826 if (os_snprintf_error(end - pos, res))
1827 break;
1828 pos += res;
1829 }
1830 *pos = '\0';
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001831
Roshan Pius3a1667e2018-07-03 15:17:14 -07001832 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Common group frequencies:%s", buf);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001833}
1834
1835
1836static void p2p_go_save_group_common_freqs(struct wpa_supplicant *wpa_s,
1837 struct p2p_go_neg_results *params)
1838{
1839 unsigned int i, len = int_array_len(wpa_s->go_params->freq_list);
1840
1841 wpa_s->p2p_group_common_freqs_num = 0;
1842 os_free(wpa_s->p2p_group_common_freqs);
1843 wpa_s->p2p_group_common_freqs = os_calloc(len, sizeof(int));
1844 if (!wpa_s->p2p_group_common_freqs)
1845 return;
1846
1847 for (i = 0; i < len; i++) {
1848 if (!wpa_s->go_params->freq_list[i])
1849 break;
1850 wpa_s->p2p_group_common_freqs[i] =
1851 wpa_s->go_params->freq_list[i];
1852 }
1853 wpa_s->p2p_group_common_freqs_num = i;
1854}
1855
1856
Dmitry Shmidt216983b2015-02-06 10:50:36 -08001857static void p2p_config_write(struct wpa_supplicant *wpa_s)
1858{
1859#ifndef CONFIG_NO_CONFIG_WRITE
Dmitry Shmidt9c175262016-03-03 10:20:07 -08001860 if (wpa_s->p2pdev->conf->update_config &&
1861 wpa_config_write(wpa_s->p2pdev->confname, wpa_s->p2pdev->conf))
Dmitry Shmidt216983b2015-02-06 10:50:36 -08001862 wpa_printf(MSG_DEBUG, "P2P: Failed to update configuration");
1863#endif /* CONFIG_NO_CONFIG_WRITE */
1864}
1865
1866
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001867static void p2p_go_configured(void *ctx, void *data)
1868{
1869 struct wpa_supplicant *wpa_s = ctx;
1870 struct p2p_go_neg_results *params = data;
1871 struct wpa_ssid *ssid;
1872
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08001873 wpa_s->ap_configured_cb = NULL;
1874 wpa_s->ap_configured_cb_ctx = NULL;
1875 wpa_s->ap_configured_cb_data = NULL;
1876 if (!wpa_s->go_params) {
1877 wpa_printf(MSG_ERROR,
1878 "P2P: p2p_go_configured() called with wpa_s->go_params == NULL");
1879 return;
1880 }
1881
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001882 p2p_go_save_group_common_freqs(wpa_s, params);
1883 p2p_go_dump_common_freqs(wpa_s);
1884
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001885 ssid = wpa_s->current_ssid;
1886 if (ssid && ssid->mode == WPAS_MODE_P2P_GO) {
1887 wpa_printf(MSG_DEBUG, "P2P: Group setup without provisioning");
1888 if (wpa_s->global->p2p_group_formation == wpa_s)
1889 wpa_s->global->p2p_group_formation = NULL;
Dmitry Shmidt7f0b69e2014-07-28 10:35:20 -07001890 wpas_p2p_group_started(wpa_s, 1, ssid, ssid->frequency,
1891 params->passphrase[0] == '\0' ?
1892 params->psk : NULL,
1893 params->passphrase,
1894 wpa_s->global->p2p_dev_addr,
1895 params->persistent_group, "");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001896 wpa_s->group_formation_reported = 1;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001897
Dmitry Shmidt9c175262016-03-03 10:20:07 -08001898 if (wpa_s->p2pdev->p2ps_method_config_any) {
1899 if (is_zero_ether_addr(wpa_s->p2pdev->p2ps_join_addr)) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001900 wpa_dbg(wpa_s, MSG_DEBUG,
1901 "P2PS: Setting default PIN for ANY");
1902 wpa_supplicant_ap_wps_pin(wpa_s, NULL,
1903 "12345670", NULL, 0,
1904 0);
1905 } else {
1906 wpa_dbg(wpa_s, MSG_DEBUG,
1907 "P2PS: Setting default PIN for " MACSTR,
Dmitry Shmidt9c175262016-03-03 10:20:07 -08001908 MAC2STR(wpa_s->p2pdev->p2ps_join_addr));
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001909 wpa_supplicant_ap_wps_pin(
Dmitry Shmidt9c175262016-03-03 10:20:07 -08001910 wpa_s, wpa_s->p2pdev->p2ps_join_addr,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001911 "12345670", NULL, 0, 0);
1912 }
Dmitry Shmidt9c175262016-03-03 10:20:07 -08001913 wpa_s->p2pdev->p2ps_method_config_any = 0;
Dmitry Shmidt216983b2015-02-06 10:50:36 -08001914 }
1915
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001916 os_get_reltime(&wpa_s->global->p2p_go_wait_client);
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07001917 if (params->persistent_group) {
Dmitry Shmidtd5ab1b52016-06-21 12:38:41 -07001918 wpas_p2p_store_persistent_group(
Dmitry Shmidt9c175262016-03-03 10:20:07 -08001919 wpa_s->p2pdev, ssid,
Dmitry Shmidt497c1d52011-07-21 15:19:46 -07001920 wpa_s->global->p2p_dev_addr);
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07001921 wpas_p2p_add_psk_list(wpa_s, ssid);
1922 }
Dmitry Shmidtd5ab1b52016-06-21 12:38:41 -07001923
1924 wpas_notify_p2p_group_started(wpa_s, ssid,
Dmitry Shmidtabb90a32016-12-05 15:34:39 -08001925 params->persistent_group, 0,
1926 NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001927 wpas_p2p_cross_connect_setup(wpa_s);
1928 wpas_p2p_set_group_idle_timeout(wpa_s);
Dmitry Shmidt56052862013-10-04 10:23:25 -07001929
1930 if (wpa_s->p2p_first_connection_timeout) {
1931 wpa_dbg(wpa_s, MSG_DEBUG,
1932 "P2P: Start group formation timeout of %d seconds until first data connection on GO",
1933 wpa_s->p2p_first_connection_timeout);
1934 wpa_s->p2p_go_group_formation_completed = 0;
1935 wpa_s->global->p2p_group_formation = wpa_s;
1936 eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
Dmitry Shmidt9c175262016-03-03 10:20:07 -08001937 wpa_s->p2pdev, NULL);
Dmitry Shmidt56052862013-10-04 10:23:25 -07001938 eloop_register_timeout(
1939 wpa_s->p2p_first_connection_timeout, 0,
1940 wpas_p2p_group_formation_timeout,
Dmitry Shmidt9c175262016-03-03 10:20:07 -08001941 wpa_s->p2pdev, NULL);
Dmitry Shmidt56052862013-10-04 10:23:25 -07001942 }
1943
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001944 return;
1945 }
1946
1947 wpa_printf(MSG_DEBUG, "P2P: Setting up WPS for GO provisioning");
1948 if (wpa_supplicant_ap_mac_addr_filter(wpa_s,
1949 params->peer_interface_addr)) {
1950 wpa_printf(MSG_DEBUG, "P2P: Failed to setup MAC address "
1951 "filtering");
1952 return;
1953 }
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001954 if (params->wps_method == WPS_PBC) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001955 wpa_supplicant_ap_wps_pbc(wpa_s, params->peer_interface_addr,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001956 params->peer_device_addr);
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001957#ifdef CONFIG_WPS_NFC
1958 } else if (params->wps_method == WPS_NFC) {
Dmitry Shmidt9c175262016-03-03 10:20:07 -08001959 if (wpa_s->p2pdev->p2p_oob_dev_pw_id !=
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001960 DEV_PW_NFC_CONNECTION_HANDOVER &&
Dmitry Shmidt9c175262016-03-03 10:20:07 -08001961 !wpa_s->p2pdev->p2p_oob_dev_pw) {
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001962 wpa_printf(MSG_DEBUG, "P2P: No NFC Dev Pw known");
1963 return;
1964 }
1965 wpas_ap_wps_add_nfc_pw(
Dmitry Shmidt9c175262016-03-03 10:20:07 -08001966 wpa_s, wpa_s->p2pdev->p2p_oob_dev_pw_id,
1967 wpa_s->p2pdev->p2p_oob_dev_pw,
1968 wpa_s->p2pdev->p2p_peer_oob_pk_hash_known ?
1969 wpa_s->p2pdev->p2p_peer_oob_pubkey_hash : NULL);
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001970#endif /* CONFIG_WPS_NFC */
1971 } else if (wpa_s->p2p_pin[0])
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001972 wpa_supplicant_ap_wps_pin(wpa_s, params->peer_interface_addr,
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001973 wpa_s->p2p_pin, NULL, 0, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001974 os_free(wpa_s->go_params);
1975 wpa_s->go_params = NULL;
1976}
1977
1978
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08001979/**
1980 * wpas_p2p_freq_to_edmg_channel - Convert frequency into EDMG channel
1981 * @freq: Frequency (MHz) to convert
1982 * @op_class: Buffer for returning operating class
1983 * @op_edmg_channel: Buffer for returning channel number
1984 * Returns: 0 on success, -1 on failure
1985 *
1986 * This can be used to find the highest channel bonding which includes the
1987 * specified frequency.
1988 */
1989static int wpas_p2p_freq_to_edmg_channel(struct wpa_supplicant *wpa_s,
1990 unsigned int freq,
1991 u8 *op_class, u8 *op_edmg_channel)
1992{
1993 struct hostapd_hw_modes *hwmode;
1994 struct ieee80211_edmg_config edmg;
1995 unsigned int i;
1996 enum chan_width chanwidth[] = {
1997 CHAN_WIDTH_8640,
1998 CHAN_WIDTH_6480,
1999 CHAN_WIDTH_4320,
2000 };
2001
2002 if (!wpa_s->hw.modes)
2003 return -1;
2004
2005 hwmode = get_mode(wpa_s->hw.modes, wpa_s->hw.num_modes,
Hai Shalom60840252021-02-19 19:02:11 -08002006 HOSTAPD_MODE_IEEE80211AD, false);
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08002007 if (!hwmode) {
2008 wpa_printf(MSG_ERROR,
2009 "Unsupported AP mode: HOSTAPD_MODE_IEEE80211AD");
2010 return -1;
2011 }
2012
2013 /* Find the highest EDMG channel bandwidth to start the P2P GO */
2014 for (i = 0; i < ARRAY_SIZE(chanwidth); i++) {
2015 if (ieee80211_chaninfo_to_channel(freq, chanwidth[i], 0,
2016 op_class,
2017 op_edmg_channel) < 0)
2018 continue;
2019
2020 hostapd_encode_edmg_chan(1, *op_edmg_channel, 0, &edmg);
2021 if (edmg.channels &&
2022 ieee802_edmg_is_allowed(hwmode->edmg, edmg)) {
2023 wpa_printf(MSG_DEBUG,
2024 "Freq %u to EDMG channel %u at opclass %u",
2025 freq, *op_edmg_channel, *op_class);
2026 return 0;
2027 }
2028 }
2029
2030 return -1;
2031}
2032
2033
2034int wpas_p2p_try_edmg_channel(struct wpa_supplicant *wpa_s,
2035 struct p2p_go_neg_results *params)
2036{
2037 u8 op_channel, op_class;
2038 int freq;
2039
2040 /* Try social channel as primary channel frequency */
2041 freq = (!params->freq) ? 58320 + 1 * 2160 : params->freq;
2042
2043 if (wpas_p2p_freq_to_edmg_channel(wpa_s, freq, &op_class,
2044 &op_channel) == 0) {
2045 wpa_printf(MSG_DEBUG,
2046 "Freq %d will be used to set an EDMG connection (channel=%u opclass=%u)",
2047 freq, op_channel, op_class);
2048 params->freq = freq;
2049 return 0;
2050 }
2051
2052 return -1;
2053}
2054
2055
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002056static void wpas_start_wps_go(struct wpa_supplicant *wpa_s,
2057 struct p2p_go_neg_results *params,
2058 int group_formation)
2059{
2060 struct wpa_ssid *ssid;
2061
Dmitry Shmidtaa532512012-09-24 10:35:31 -07002062 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Starting GO");
2063 if (wpas_copy_go_neg_results(wpa_s, params) < 0) {
2064 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Could not copy GO Negotiation "
2065 "results");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002066 return;
Dmitry Shmidtaa532512012-09-24 10:35:31 -07002067 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002068
2069 ssid = wpa_config_add_network(wpa_s->conf);
Dmitry Shmidtaa532512012-09-24 10:35:31 -07002070 if (ssid == NULL) {
2071 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Could not add network for GO");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002072 return;
Dmitry Shmidtaa532512012-09-24 10:35:31 -07002073 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002074
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002075 wpa_s->show_group_started = 0;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002076 wpa_s->p2p_go_group_formation_completed = 0;
2077 wpa_s->group_formation_reported = 0;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002078 os_memset(wpa_s->go_dev_addr, 0, ETH_ALEN);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002079
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002080 wpa_config_set_network_defaults(ssid);
2081 ssid->temporary = 1;
2082 ssid->p2p_group = 1;
Dmitry Shmidtaca489e2016-09-28 15:44:14 -07002083 ssid->p2p_persistent_group = !!params->persistent_group;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002084 ssid->mode = group_formation ? WPAS_MODE_P2P_GROUP_FORMATION :
2085 WPAS_MODE_P2P_GO;
2086 ssid->frequency = params->freq;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002087 ssid->ht40 = params->ht40;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07002088 ssid->vht = params->vht;
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08002089 ssid->max_oper_chwidth = params->max_oper_chwidth;
2090 ssid->vht_center_freq2 = params->vht_center_freq2;
Hai Shalom74f70d42019-02-11 14:42:39 -08002091 ssid->he = params->he;
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08002092 if (params->edmg) {
2093 u8 op_channel, op_class;
2094
2095 if (!wpas_p2p_freq_to_edmg_channel(wpa_s, params->freq,
2096 &op_class, &op_channel)) {
2097 ssid->edmg_channel = op_channel;
2098 ssid->enable_edmg = params->edmg;
2099 } else {
2100 wpa_dbg(wpa_s, MSG_DEBUG,
2101 "P2P: Could not match EDMG channel, freq %d, for GO",
2102 params->freq);
2103 }
2104 }
2105
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002106 ssid->ssid = os_zalloc(params->ssid_len + 1);
2107 if (ssid->ssid) {
2108 os_memcpy(ssid->ssid, params->ssid, params->ssid_len);
2109 ssid->ssid_len = params->ssid_len;
2110 }
2111 ssid->auth_alg = WPA_AUTH_ALG_OPEN;
2112 ssid->key_mgmt = WPA_KEY_MGMT_PSK;
Hai Shalomc1a21442022-02-04 13:43:00 -08002113 if (is_6ghz_freq(ssid->frequency) &&
2114 is_p2p_6ghz_capable(wpa_s->global->p2p)) {
2115 ssid->auth_alg |= WPA_AUTH_ALG_SAE;
2116 ssid->key_mgmt = WPA_KEY_MGMT_SAE;
2117 ssid->ieee80211w = MGMT_FRAME_PROTECTION_REQUIRED;
Sunil Ravi77d572f2023-01-17 23:58:31 +00002118 ssid->sae_pwe = SAE_PWE_HASH_TO_ELEMENT;
Hai Shalomc1a21442022-02-04 13:43:00 -08002119 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Use SAE auth_alg and key_mgmt");
2120 } else {
2121 p2p_set_6ghz_dev_capab(wpa_s->global->p2p, false);
2122 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002123 ssid->proto = WPA_PROTO_RSN;
2124 ssid->pairwise_cipher = WPA_CIPHER_CCMP;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002125 ssid->group_cipher = WPA_CIPHER_CCMP;
2126 if (params->freq > 56160) {
2127 /*
2128 * Enable GCMP instead of CCMP as pairwise_cipher and
2129 * group_cipher in 60 GHz.
2130 */
2131 ssid->pairwise_cipher = WPA_CIPHER_GCMP;
2132 ssid->group_cipher = WPA_CIPHER_GCMP;
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08002133 /* P2P GO in 60 GHz is always a PCP (PBSS) */
2134 ssid->pbss = 1;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002135 }
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08002136 if (os_strlen(params->passphrase) > 0) {
2137 ssid->passphrase = os_strdup(params->passphrase);
2138 if (ssid->passphrase == NULL) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07002139 wpa_msg_global(wpa_s, MSG_ERROR,
2140 "P2P: Failed to copy passphrase for GO");
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08002141 wpa_config_remove_network(wpa_s->conf, ssid->id);
2142 return;
2143 }
2144 } else
2145 ssid->passphrase = NULL;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002146 ssid->psk_set = params->psk_set;
2147 if (ssid->psk_set)
2148 os_memcpy(ssid->psk, params->psk, sizeof(ssid->psk));
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08002149 else if (ssid->passphrase)
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002150 wpa_config_update_psk(ssid);
Dmitry Shmidt9c175262016-03-03 10:20:07 -08002151 ssid->ap_max_inactivity = wpa_s->p2pdev->conf->p2p_go_max_inactivity;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002152
2153 wpa_s->ap_configured_cb = p2p_go_configured;
2154 wpa_s->ap_configured_cb_ctx = wpa_s;
2155 wpa_s->ap_configured_cb_data = wpa_s->go_params;
Dmitry Shmidt6dc03bd2014-05-16 10:40:13 -07002156 wpa_s->scan_req = NORMAL_SCAN_REQ;
Jouni Malinen75ecf522011-06-27 15:19:46 -07002157 wpa_s->connect_without_scan = ssid;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002158 wpa_s->reassociate = 1;
2159 wpa_s->disconnected = 0;
Dmitry Shmidtaa532512012-09-24 10:35:31 -07002160 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Request scan (that will be skipped) to "
2161 "start GO)");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002162 wpa_supplicant_req_scan(wpa_s, 0, 0);
2163}
2164
2165
2166static void wpas_p2p_clone_config(struct wpa_supplicant *dst,
2167 const struct wpa_supplicant *src)
2168{
2169 struct wpa_config *d;
2170 const struct wpa_config *s;
2171
2172 d = dst->conf;
2173 s = src->conf;
2174
Dmitry Shmidt849734c2016-05-27 09:59:01 -07002175#define C(n) \
2176do { \
2177 if (s->n && !d->n) \
2178 d->n = os_strdup(s->n); \
2179} while (0)
2180
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002181 C(device_name);
2182 C(manufacturer);
2183 C(model_name);
2184 C(model_number);
2185 C(serial_number);
2186 C(config_methods);
2187#undef C
2188
2189 os_memcpy(d->device_type, s->device_type, WPS_DEV_TYPE_LEN);
2190 os_memcpy(d->sec_device_type, s->sec_device_type,
2191 sizeof(d->sec_device_type));
2192 d->num_sec_device_types = s->num_sec_device_types;
2193
2194 d->p2p_group_idle = s->p2p_group_idle;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002195 d->p2p_go_freq_change_policy = s->p2p_go_freq_change_policy;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002196 d->p2p_intra_bss = s->p2p_intra_bss;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002197 d->persistent_reconnect = s->persistent_reconnect;
Dmitry Shmidt04949592012-07-19 12:16:46 -07002198 d->max_num_sta = s->max_num_sta;
2199 d->pbc_in_m1 = s->pbc_in_m1;
Dmitry Shmidt444d5672013-04-01 13:08:44 -07002200 d->ignore_old_scan_res = s->ignore_old_scan_res;
Dmitry Shmidt51b6ea82013-05-08 10:42:09 -07002201 d->beacon_int = s->beacon_int;
Dmitry Shmidt18463232014-01-24 12:29:41 -08002202 d->dtim_period = s->dtim_period;
Dmitry Shmidt7f656022015-02-25 14:36:37 -08002203 d->p2p_go_ctwindow = s->p2p_go_ctwindow;
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07002204 d->disassoc_low_ack = s->disassoc_low_ack;
Dmitry Shmidt96571392013-10-14 12:54:46 -07002205 d->disable_scan_offload = s->disable_scan_offload;
Dmitry Shmidt807291d2015-01-27 13:40:23 -08002206 d->passive_scan = s->passive_scan;
Hai Shalomc1a21442022-02-04 13:43:00 -08002207 d->pmf = s->pmf;
2208 d->p2p_6ghz_disable = s->p2p_6ghz_disable;
Sunil Ravi77d572f2023-01-17 23:58:31 +00002209 d->sae_pwe = s->sae_pwe;
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08002210
Dmitry Shmidt849734c2016-05-27 09:59:01 -07002211 if (s->wps_nfc_dh_privkey && s->wps_nfc_dh_pubkey &&
2212 !d->wps_nfc_pw_from_config) {
2213 wpabuf_free(d->wps_nfc_dh_privkey);
2214 wpabuf_free(d->wps_nfc_dh_pubkey);
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08002215 d->wps_nfc_dh_privkey = wpabuf_dup(s->wps_nfc_dh_privkey);
2216 d->wps_nfc_dh_pubkey = wpabuf_dup(s->wps_nfc_dh_pubkey);
2217 }
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07002218 d->p2p_cli_probe = s->p2p_cli_probe;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002219 d->go_interworking = s->go_interworking;
2220 d->go_access_network_type = s->go_access_network_type;
2221 d->go_internet = s->go_internet;
2222 d->go_venue_group = s->go_venue_group;
2223 d->go_venue_type = s->go_venue_type;
Hai Shalom60840252021-02-19 19:02:11 -08002224 d->p2p_add_cli_chan = s->p2p_add_cli_chan;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002225}
2226
2227
Dmitry Shmidt34af3062013-07-11 10:46:32 -07002228static void wpas_p2p_get_group_ifname(struct wpa_supplicant *wpa_s,
2229 char *ifname, size_t len)
2230{
2231 char *ifname_ptr = wpa_s->ifname;
2232
2233 if (os_strncmp(wpa_s->ifname, P2P_MGMT_DEVICE_PREFIX,
2234 os_strlen(P2P_MGMT_DEVICE_PREFIX)) == 0) {
2235 ifname_ptr = os_strrchr(wpa_s->ifname, '-') + 1;
2236 }
2237
2238 os_snprintf(ifname, len, "p2p-%s-%d", ifname_ptr, wpa_s->p2p_group_idx);
2239 if (os_strlen(ifname) >= IFNAMSIZ &&
2240 os_strlen(wpa_s->ifname) < IFNAMSIZ) {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002241 int res;
2242
Dmitry Shmidt34af3062013-07-11 10:46:32 -07002243 /* Try to avoid going over the IFNAMSIZ length limit */
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002244 res = os_snprintf(ifname, len, "p2p-%d", wpa_s->p2p_group_idx);
2245 if (os_snprintf_error(len, res) && len)
2246 ifname[len - 1] = '\0';
Dmitry Shmidt34af3062013-07-11 10:46:32 -07002247 }
2248}
2249
2250
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002251static int wpas_p2p_add_group_interface(struct wpa_supplicant *wpa_s,
2252 enum wpa_driver_if_type type)
2253{
2254 char ifname[120], force_ifname[120];
2255
2256 if (wpa_s->pending_interface_name[0]) {
2257 wpa_printf(MSG_DEBUG, "P2P: Pending virtual interface exists "
2258 "- skip creation of a new one");
2259 if (is_zero_ether_addr(wpa_s->pending_interface_addr)) {
2260 wpa_printf(MSG_DEBUG, "P2P: Pending virtual address "
2261 "unknown?! ifname='%s'",
2262 wpa_s->pending_interface_name);
2263 return -1;
2264 }
2265 return 0;
2266 }
2267
Dmitry Shmidt34af3062013-07-11 10:46:32 -07002268 wpas_p2p_get_group_ifname(wpa_s, ifname, sizeof(ifname));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002269 force_ifname[0] = '\0';
2270
2271 wpa_printf(MSG_DEBUG, "P2P: Create a new interface %s for the group",
2272 ifname);
2273 wpa_s->p2p_group_idx++;
2274
2275 wpa_s->pending_interface_type = type;
2276 if (wpa_drv_if_add(wpa_s, type, ifname, NULL, NULL, force_ifname,
2277 wpa_s->pending_interface_addr, NULL) < 0) {
2278 wpa_printf(MSG_ERROR, "P2P: Failed to create new group "
2279 "interface");
2280 return -1;
2281 }
2282
Jimmy Chen36c21992018-11-29 16:46:43 +08002283 if (wpa_s->conf->p2p_interface_random_mac_addr) {
Hai Shalom74f70d42019-02-11 14:42:39 -08002284 random_mac_addr(wpa_s->pending_interface_addr);
2285 wpa_printf(MSG_DEBUG, "P2P: Generate random MAC address " MACSTR
2286 " for the group",
2287 MAC2STR(wpa_s->pending_interface_addr));
Jimmy Chen36c21992018-11-29 16:46:43 +08002288 }
2289
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002290 if (force_ifname[0]) {
2291 wpa_printf(MSG_DEBUG, "P2P: Driver forced interface name %s",
2292 force_ifname);
2293 os_strlcpy(wpa_s->pending_interface_name, force_ifname,
2294 sizeof(wpa_s->pending_interface_name));
2295 } else
2296 os_strlcpy(wpa_s->pending_interface_name, ifname,
2297 sizeof(wpa_s->pending_interface_name));
2298 wpa_printf(MSG_DEBUG, "P2P: Created pending virtual interface %s addr "
2299 MACSTR, wpa_s->pending_interface_name,
2300 MAC2STR(wpa_s->pending_interface_addr));
2301
2302 return 0;
2303}
2304
2305
2306static void wpas_p2p_remove_pending_group_interface(
2307 struct wpa_supplicant *wpa_s)
2308{
2309 if (!wpa_s->pending_interface_name[0] ||
2310 is_zero_ether_addr(wpa_s->pending_interface_addr))
2311 return; /* No pending virtual interface */
2312
2313 wpa_printf(MSG_DEBUG, "P2P: Removing pending group interface %s",
2314 wpa_s->pending_interface_name);
2315 wpa_drv_if_remove(wpa_s, wpa_s->pending_interface_type,
2316 wpa_s->pending_interface_name);
2317 os_memset(wpa_s->pending_interface_addr, 0, ETH_ALEN);
2318 wpa_s->pending_interface_name[0] = '\0';
Dmitry Shmidt216983b2015-02-06 10:50:36 -08002319 wpa_s->global->pending_group_iface_for_p2ps = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002320}
2321
2322
2323static struct wpa_supplicant *
2324wpas_p2p_init_group_interface(struct wpa_supplicant *wpa_s, int go)
2325{
2326 struct wpa_interface iface;
2327 struct wpa_supplicant *group_wpa_s;
2328
2329 if (!wpa_s->pending_interface_name[0]) {
2330 wpa_printf(MSG_ERROR, "P2P: No pending group interface");
2331 if (!wpas_p2p_create_iface(wpa_s))
2332 return NULL;
2333 /*
2334 * Something has forced us to remove the pending interface; try
2335 * to create a new one and hope for the best that we will get
2336 * the same local address.
2337 */
2338 if (wpas_p2p_add_group_interface(wpa_s, go ? WPA_IF_P2P_GO :
2339 WPA_IF_P2P_CLIENT) < 0)
2340 return NULL;
2341 }
2342
2343 os_memset(&iface, 0, sizeof(iface));
2344 iface.ifname = wpa_s->pending_interface_name;
2345 iface.driver = wpa_s->driver->name;
Dmitry Shmidt34af3062013-07-11 10:46:32 -07002346 if (wpa_s->conf->ctrl_interface == NULL &&
2347 wpa_s->parent != wpa_s &&
2348 wpa_s->p2p_mgmt &&
2349 (wpa_s->drv_flags & WPA_DRIVER_FLAGS_DEDICATED_P2P_DEVICE))
2350 iface.ctrl_interface = wpa_s->parent->conf->ctrl_interface;
2351 else
2352 iface.ctrl_interface = wpa_s->conf->ctrl_interface;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002353 iface.driver_param = wpa_s->conf->driver_param;
Dmitry Shmidt203eadb2015-03-05 14:16:04 -08002354 group_wpa_s = wpa_supplicant_add_iface(wpa_s->global, &iface, wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002355 if (group_wpa_s == NULL) {
2356 wpa_printf(MSG_ERROR, "P2P: Failed to create new "
2357 "wpa_supplicant interface");
2358 return NULL;
2359 }
2360 wpa_s->pending_interface_name[0] = '\0';
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002361 group_wpa_s->p2p_group_interface = go ? P2P_GROUP_INTERFACE_GO :
2362 P2P_GROUP_INTERFACE_CLIENT;
2363 wpa_s->global->p2p_group_formation = group_wpa_s;
Dmitry Shmidt216983b2015-02-06 10:50:36 -08002364 wpa_s->global->pending_group_iface_for_p2ps = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002365
2366 wpas_p2p_clone_config(group_wpa_s, wpa_s);
2367
Jimmy Chen36c21992018-11-29 16:46:43 +08002368 if (wpa_s->conf->p2p_interface_random_mac_addr) {
Hai Shalom74f70d42019-02-11 14:42:39 -08002369 if (wpa_drv_set_mac_addr(group_wpa_s,
2370 wpa_s->pending_interface_addr) < 0) {
Jimmy Chen36c21992018-11-29 16:46:43 +08002371 wpa_msg(group_wpa_s, MSG_INFO,
2372 "Failed to set random MAC address");
Hai Shalom74f70d42019-02-11 14:42:39 -08002373 wpa_supplicant_remove_iface(wpa_s->global, group_wpa_s,
2374 0);
Jimmy Chen36c21992018-11-29 16:46:43 +08002375 return NULL;
2376 }
2377
2378 if (wpa_supplicant_update_mac_addr(group_wpa_s) < 0) {
2379 wpa_msg(group_wpa_s, MSG_INFO,
2380 "Could not update MAC address information");
Hai Shalom74f70d42019-02-11 14:42:39 -08002381 wpa_supplicant_remove_iface(wpa_s->global, group_wpa_s,
2382 0);
Jimmy Chen36c21992018-11-29 16:46:43 +08002383 return NULL;
2384 }
2385
Hai Shalom74f70d42019-02-11 14:42:39 -08002386 wpa_printf(MSG_DEBUG, "P2P: Using random MAC address " MACSTR
2387 " for the group",
2388 MAC2STR(wpa_s->pending_interface_addr));
Jimmy Chen36c21992018-11-29 16:46:43 +08002389 }
2390
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002391 return group_wpa_s;
2392}
2393
2394
2395static void wpas_p2p_group_formation_timeout(void *eloop_ctx,
2396 void *timeout_ctx)
2397{
2398 struct wpa_supplicant *wpa_s = eloop_ctx;
2399 wpa_printf(MSG_DEBUG, "P2P: Group Formation timed out");
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07002400 wpas_p2p_group_formation_failed(wpa_s, 0);
Dmitry Shmidt34af3062013-07-11 10:46:32 -07002401}
2402
2403
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07002404static void wpas_p2p_group_formation_failed(struct wpa_supplicant *wpa_s,
2405 int already_deleted)
Dmitry Shmidt34af3062013-07-11 10:46:32 -07002406{
2407 eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
Dmitry Shmidt9c175262016-03-03 10:20:07 -08002408 wpa_s->p2pdev, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002409 if (wpa_s->global->p2p)
2410 p2p_group_formation_failed(wpa_s->global->p2p);
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07002411 wpas_group_formation_completed(wpa_s, 0, already_deleted);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002412}
2413
2414
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002415static void wpas_p2p_grpform_fail_after_wps(struct wpa_supplicant *wpa_s)
2416{
2417 wpa_printf(MSG_DEBUG, "P2P: Reject group formation due to WPS provisioning failure");
2418 eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
Dmitry Shmidt9c175262016-03-03 10:20:07 -08002419 wpa_s->p2pdev, NULL);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002420 eloop_register_timeout(0, 0, wpas_p2p_group_formation_timeout,
Dmitry Shmidt9c175262016-03-03 10:20:07 -08002421 wpa_s->p2pdev, NULL);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002422 wpa_s->global->p2p_fail_on_wps_complete = 0;
2423}
2424
2425
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07002426void wpas_p2p_ap_setup_failed(struct wpa_supplicant *wpa_s)
2427{
2428 if (wpa_s->global->p2p_group_formation != wpa_s)
2429 return;
2430 /* Speed up group formation timeout since this cannot succeed */
2431 eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
Dmitry Shmidt9c175262016-03-03 10:20:07 -08002432 wpa_s->p2pdev, NULL);
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07002433 eloop_register_timeout(0, 0, wpas_p2p_group_formation_timeout,
Dmitry Shmidt9c175262016-03-03 10:20:07 -08002434 wpa_s->p2pdev, NULL);
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07002435}
2436
2437
Matthew Wang06b42472022-11-10 06:56:31 +00002438bool wpas_p2p_retry_limit_exceeded(struct wpa_supplicant *wpa_s)
2439{
2440 if (!wpa_s->p2p_in_invitation || !wpa_s->p2p_retry_limit ||
Sunil Ravi77d572f2023-01-17 23:58:31 +00002441 wpa_s->p2p_in_invitation <= wpa_s->p2p_retry_limit)
Matthew Wang06b42472022-11-10 06:56:31 +00002442 return false;
Sunil Ravi77d572f2023-01-17 23:58:31 +00002443
Matthew Wang06b42472022-11-10 06:56:31 +00002444 wpa_printf(MSG_DEBUG, "P2P: Group join retry limit exceeded");
2445 eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
2446 wpa_s->p2pdev, NULL);
2447 eloop_register_timeout(0, 0, wpas_p2p_group_formation_timeout,
2448 wpa_s->p2pdev, NULL);
2449 return true;
2450}
2451
2452
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002453static void wpas_go_neg_completed(void *ctx, struct p2p_go_neg_results *res)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002454{
2455 struct wpa_supplicant *wpa_s = ctx;
Dmitry Shmidt9c175262016-03-03 10:20:07 -08002456 struct wpa_supplicant *group_wpa_s;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002457
2458 if (wpa_s->off_channel_freq || wpa_s->roc_waiting_drv_freq) {
2459 wpa_drv_cancel_remain_on_channel(wpa_s);
2460 wpa_s->off_channel_freq = 0;
2461 wpa_s->roc_waiting_drv_freq = 0;
2462 }
2463
2464 if (res->status) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07002465 wpa_msg_global(wpa_s, MSG_INFO,
2466 P2P_EVENT_GO_NEG_FAILURE "status=%d",
2467 res->status);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002468 wpas_notify_p2p_go_neg_completed(wpa_s, res);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002469 wpas_p2p_remove_pending_group_interface(wpa_s);
2470 return;
2471 }
2472
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002473 if (!res->role_go) {
2474 /* Inform driver of the operating channel of GO. */
2475 wpa_drv_set_prob_oper_freq(wpa_s, res->freq);
2476 }
2477
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002478 if (wpa_s->p2p_go_ht40)
2479 res->ht40 = 1;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07002480 if (wpa_s->p2p_go_vht)
2481 res->vht = 1;
Hai Shalomc3565922019-10-28 11:58:20 -07002482 if (wpa_s->p2p_go_he)
2483 res->he = 1;
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08002484 if (wpa_s->p2p_go_edmg)
2485 res->edmg = 1;
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08002486 res->max_oper_chwidth = wpa_s->p2p_go_max_oper_chwidth;
2487 res->vht_center_freq2 = wpa_s->p2p_go_vht_center_freq2;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002488
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07002489 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_GO_NEG_SUCCESS "role=%s "
2490 "freq=%d ht40=%d peer_dev=" MACSTR " peer_iface=" MACSTR
2491 " wps_method=%s",
2492 res->role_go ? "GO" : "client", res->freq, res->ht40,
2493 MAC2STR(res->peer_device_addr),
2494 MAC2STR(res->peer_interface_addr),
2495 p2p_wps_method_text(res->wps_method));
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002496 wpas_notify_p2p_go_neg_completed(wpa_s, res);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002497
Dmitry Shmidt04949592012-07-19 12:16:46 -07002498 if (res->role_go && wpa_s->p2p_persistent_id >= 0) {
2499 struct wpa_ssid *ssid;
2500 ssid = wpa_config_get_network(wpa_s->conf,
2501 wpa_s->p2p_persistent_id);
2502 if (ssid && ssid->disabled == 2 &&
2503 ssid->mode == WPAS_MODE_P2P_GO && ssid->passphrase) {
2504 size_t len = os_strlen(ssid->passphrase);
2505 wpa_printf(MSG_DEBUG, "P2P: Override passphrase based "
2506 "on requested persistent group");
2507 os_memcpy(res->passphrase, ssid->passphrase, len);
2508 res->passphrase[len] = '\0';
2509 }
2510 }
2511
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002512 if (wpa_s->create_p2p_iface) {
Dmitry Shmidt9c175262016-03-03 10:20:07 -08002513 group_wpa_s =
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002514 wpas_p2p_init_group_interface(wpa_s, res->role_go);
2515 if (group_wpa_s == NULL) {
2516 wpas_p2p_remove_pending_group_interface(wpa_s);
Dmitry Shmidtd11f0192014-03-24 12:09:47 -07002517 eloop_cancel_timeout(wpas_p2p_long_listen_timeout,
2518 wpa_s, NULL);
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07002519 wpas_p2p_group_formation_failed(wpa_s, 1);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002520 return;
2521 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002522 os_memset(wpa_s->pending_interface_addr, 0, ETH_ALEN);
2523 wpa_s->pending_interface_name[0] = '\0';
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002524 } else {
Dmitry Shmidt9c175262016-03-03 10:20:07 -08002525 group_wpa_s = wpa_s->parent;
2526 wpa_s->global->p2p_group_formation = group_wpa_s;
2527 if (group_wpa_s != wpa_s)
Dmitry Shmidt849734c2016-05-27 09:59:01 -07002528 wpas_p2p_clone_config(group_wpa_s, wpa_s);
Dmitry Shmidt9c175262016-03-03 10:20:07 -08002529 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002530
Dmitry Shmidt9c175262016-03-03 10:20:07 -08002531 group_wpa_s->p2p_in_provisioning = 1;
2532 group_wpa_s->p2pdev = wpa_s;
2533 if (group_wpa_s != wpa_s) {
2534 os_memcpy(group_wpa_s->p2p_pin, wpa_s->p2p_pin,
2535 sizeof(group_wpa_s->p2p_pin));
2536 group_wpa_s->p2p_wps_method = wpa_s->p2p_wps_method;
2537 }
2538 if (res->role_go) {
2539 wpas_start_wps_go(group_wpa_s, res, 1);
2540 } else {
2541 os_get_reltime(&group_wpa_s->scan_min_time);
2542 wpas_start_wps_enrollee(group_wpa_s, res);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002543 }
2544
Hai Shalomfdcde762020-04-02 11:19:20 -07002545 wpa_s->global->p2p_long_listen = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002546 eloop_cancel_timeout(wpas_p2p_long_listen_timeout, wpa_s, NULL);
2547
2548 eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s, NULL);
2549 eloop_register_timeout(15 + res->peer_config_timeout / 100,
2550 (res->peer_config_timeout % 100) * 10000,
2551 wpas_p2p_group_formation_timeout, wpa_s, NULL);
2552}
2553
2554
Dmitry Shmidt7a53dbb2015-06-11 13:13:53 -07002555static void wpas_go_neg_req_rx(void *ctx, const u8 *src, u16 dev_passwd_id,
2556 u8 go_intent)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002557{
2558 struct wpa_supplicant *wpa_s = ctx;
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07002559 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_GO_NEG_REQUEST MACSTR
Dmitry Shmidt7a53dbb2015-06-11 13:13:53 -07002560 " dev_passwd_id=%u go_intent=%u", MAC2STR(src),
2561 dev_passwd_id, go_intent);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002562
Dmitry Shmidt7a53dbb2015-06-11 13:13:53 -07002563 wpas_notify_p2p_go_neg_req(wpa_s, src, dev_passwd_id, go_intent);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002564}
2565
2566
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002567static void wpas_dev_found(void *ctx, const u8 *addr,
2568 const struct p2p_peer_info *info,
2569 int new_device)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002570{
Roshan Piusfd2fd662017-01-23 13:41:57 -08002571 u8 *wfd_dev_info = NULL;
2572 u8 wfd_dev_info_len = 0;
Jimmy Chen0133fc12021-03-04 13:56:11 +08002573 u8 *wfd_r2_dev_info = NULL;
Jimmy Chen57e19f52021-03-04 14:19:52 +08002574 u8 wfd_r2_dev_info_len = 0;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002575#ifndef CONFIG_NO_STDOUT_DEBUG
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002576 struct wpa_supplicant *wpa_s = ctx;
2577 char devtype[WPS_DEV_TYPE_BUFSIZE];
Dmitry Shmidt04f534e2013-12-09 15:50:16 -08002578 char *wfd_dev_info_hex = NULL;
Jimmy Chen0133fc12021-03-04 13:56:11 +08002579 char *wfd_r2_dev_info_hex = NULL;
Dmitry Shmidt04f534e2013-12-09 15:50:16 -08002580
Irfan Sheriff8367dc92012-09-09 17:08:19 -07002581#ifdef CONFIG_WIFI_DISPLAY
Dmitry Shmidt04f534e2013-12-09 15:50:16 -08002582 wfd_dev_info_hex = wifi_display_subelem_hex(info->wfd_subelems,
2583 WFD_SUBELEM_DEVICE_INFO);
Roshan Piusfd2fd662017-01-23 13:41:57 -08002584 if (wfd_dev_info_hex) {
2585 wfd_dev_info_len = strlen(wfd_dev_info_hex) / 2;
2586 wfd_dev_info = os_zalloc(wfd_dev_info_len);
2587 // Only used for notification, so not handling error.
2588 hexstr2bin(wfd_dev_info_hex, wfd_dev_info, wfd_dev_info_len);
2589 }
Jimmy Chen0133fc12021-03-04 13:56:11 +08002590
2591 wfd_r2_dev_info_hex = wifi_display_subelem_hex(info->wfd_subelems,
2592 WFD_SUBELEM_R2_DEVICE_INFO);
2593 if (wfd_r2_dev_info_hex) {
2594 wfd_r2_dev_info_len = strlen(wfd_r2_dev_info_hex) / 2;
2595 wfd_r2_dev_info = os_zalloc(wfd_r2_dev_info_len);
2596 // Only used for notification, so not handling error.
2597 hexstr2bin(wfd_r2_dev_info_hex, wfd_r2_dev_info, wfd_r2_dev_info_len);
2598 }
Irfan Sheriff8367dc92012-09-09 17:08:19 -07002599#endif /* CONFIG_WIFI_DISPLAY */
Dmitry Shmidt04f534e2013-12-09 15:50:16 -08002600
Dmitry Shmidt216983b2015-02-06 10:50:36 -08002601 if (info->p2ps_instance) {
2602 char str[256];
2603 const u8 *buf = wpabuf_head(info->p2ps_instance);
2604 size_t len = wpabuf_len(info->p2ps_instance);
2605
2606 while (len) {
2607 u32 id;
2608 u16 methods;
2609 u8 str_len;
2610
2611 if (len < 4 + 2 + 1)
2612 break;
2613 id = WPA_GET_LE32(buf);
2614 buf += sizeof(u32);
2615 methods = WPA_GET_BE16(buf);
2616 buf += sizeof(u16);
2617 str_len = *buf++;
2618 if (str_len > len - 4 - 2 - 1)
2619 break;
2620 os_memcpy(str, buf, str_len);
2621 str[str_len] = '\0';
2622 buf += str_len;
2623 len -= str_len + sizeof(u32) + sizeof(u16) + sizeof(u8);
2624
2625 wpa_msg_global(wpa_s, MSG_INFO,
2626 P2P_EVENT_DEVICE_FOUND MACSTR
2627 " p2p_dev_addr=" MACSTR
2628 " pri_dev_type=%s name='%s'"
2629 " config_methods=0x%x"
2630 " dev_capab=0x%x"
2631 " group_capab=0x%x"
2632 " adv_id=%x asp_svc=%s%s",
2633 MAC2STR(addr),
2634 MAC2STR(info->p2p_device_addr),
2635 wps_dev_type_bin2str(
2636 info->pri_dev_type,
2637 devtype, sizeof(devtype)),
2638 info->device_name, methods,
2639 info->dev_capab, info->group_capab,
2640 id, str,
2641 info->vendor_elems ?
2642 " vendor_elems=1" : "");
2643 }
2644 goto done;
2645 }
2646
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07002647 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_DEVICE_FOUND MACSTR
2648 " p2p_dev_addr=" MACSTR
2649 " pri_dev_type=%s name='%s' config_methods=0x%x "
Jimmy Chen0133fc12021-03-04 13:56:11 +08002650 "dev_capab=0x%x group_capab=0x%x%s%s%s%s%s new=%d",
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07002651 MAC2STR(addr), MAC2STR(info->p2p_device_addr),
2652 wps_dev_type_bin2str(info->pri_dev_type, devtype,
2653 sizeof(devtype)),
2654 info->device_name, info->config_methods,
2655 info->dev_capab, info->group_capab,
Dmitry Shmidt04f534e2013-12-09 15:50:16 -08002656 wfd_dev_info_hex ? " wfd_dev_info=0x" : "",
Dmitry Shmidt2e67f062014-07-16 09:55:28 -07002657 wfd_dev_info_hex ? wfd_dev_info_hex : "",
Jimmy Chen0133fc12021-03-04 13:56:11 +08002658 wfd_r2_dev_info_hex ? " wfd_r2_dev_info=0x" : "",
2659 wfd_r2_dev_info_hex ? wfd_r2_dev_info_hex : "",
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002660 info->vendor_elems ? " vendor_elems=1" : "",
2661 new_device);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002662
Dmitry Shmidt216983b2015-02-06 10:50:36 -08002663done:
Dmitry Shmidt04f534e2013-12-09 15:50:16 -08002664 os_free(wfd_dev_info_hex);
Jimmy Chen0133fc12021-03-04 13:56:11 +08002665 os_free(wfd_r2_dev_info_hex);
Dmitry Shmidt97672262014-02-03 13:02:54 -08002666#endif /* CONFIG_NO_STDOUT_DEBUG */
Dmitry Shmidt04f534e2013-12-09 15:50:16 -08002667
Roshan Piusfd2fd662017-01-23 13:41:57 -08002668 wpas_notify_p2p_device_found(ctx, addr, info, wfd_dev_info,
Jimmy Chen0133fc12021-03-04 13:56:11 +08002669 wfd_dev_info_len, wfd_r2_dev_info,
2670 wfd_r2_dev_info_len, new_device);
Roshan Piusfd2fd662017-01-23 13:41:57 -08002671 os_free(wfd_dev_info);
jiangpingpinga96a5e82023-11-16 15:02:17 +08002672 os_free(wfd_r2_dev_info);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002673}
2674
2675
2676static void wpas_dev_lost(void *ctx, const u8 *dev_addr)
2677{
2678 struct wpa_supplicant *wpa_s = ctx;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002679
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07002680 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_DEVICE_LOST
2681 "p2p_dev_addr=" MACSTR, MAC2STR(dev_addr));
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002682
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002683 wpas_notify_p2p_device_lost(wpa_s, dev_addr);
2684}
2685
2686
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07002687static void wpas_find_stopped(void *ctx)
2688{
2689 struct wpa_supplicant *wpa_s = ctx;
Dmitry Shmidt9c175262016-03-03 10:20:07 -08002690
2691 if (wpa_s->p2p_scan_work && wpas_abort_ongoing_scan(wpa_s) < 0)
2692 wpa_printf(MSG_DEBUG, "P2P: Abort ongoing scan failed");
2693
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07002694 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_FIND_STOPPED);
Dmitry Shmidt8bd70b72015-05-26 16:02:19 -07002695 wpas_notify_p2p_find_stopped(wpa_s);
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07002696}
2697
2698
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002699struct wpas_p2p_listen_work {
2700 unsigned int freq;
2701 unsigned int duration;
2702 struct wpabuf *probe_resp_ie;
2703};
2704
2705
2706static void wpas_p2p_listen_work_free(struct wpas_p2p_listen_work *lwork)
2707{
2708 if (lwork == NULL)
2709 return;
2710 wpabuf_free(lwork->probe_resp_ie);
2711 os_free(lwork);
2712}
2713
2714
2715static void wpas_p2p_listen_work_done(struct wpa_supplicant *wpa_s)
2716{
2717 struct wpas_p2p_listen_work *lwork;
2718
2719 if (!wpa_s->p2p_listen_work)
2720 return;
2721
2722 lwork = wpa_s->p2p_listen_work->ctx;
2723 wpas_p2p_listen_work_free(lwork);
2724 radio_work_done(wpa_s->p2p_listen_work);
2725 wpa_s->p2p_listen_work = NULL;
2726}
2727
2728
2729static void wpas_start_listen_cb(struct wpa_radio_work *work, int deinit)
2730{
2731 struct wpa_supplicant *wpa_s = work->wpa_s;
2732 struct wpas_p2p_listen_work *lwork = work->ctx;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002733 unsigned int duration;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002734
2735 if (deinit) {
Dmitry Shmidtbd14a572014-02-18 10:33:49 -08002736 if (work->started) {
2737 wpa_s->p2p_listen_work = NULL;
2738 wpas_stop_listen(wpa_s);
2739 }
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002740 wpas_p2p_listen_work_free(lwork);
2741 return;
2742 }
2743
2744 wpa_s->p2p_listen_work = work;
2745
2746 wpa_drv_set_ap_wps_ie(wpa_s, NULL, lwork->probe_resp_ie, NULL);
2747
2748 if (wpa_drv_probe_req_report(wpa_s, 1) < 0) {
2749 wpa_printf(MSG_DEBUG, "P2P: Failed to request the driver to "
2750 "report received Probe Request frames");
2751 wpas_p2p_listen_work_done(wpa_s);
2752 return;
2753 }
2754
2755 wpa_s->pending_listen_freq = lwork->freq;
2756 wpa_s->pending_listen_duration = lwork->duration;
2757
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002758 duration = lwork->duration;
2759#ifdef CONFIG_TESTING_OPTIONS
2760 if (wpa_s->extra_roc_dur) {
2761 wpa_printf(MSG_DEBUG, "TESTING: Increase ROC duration %u -> %u",
2762 duration, duration + wpa_s->extra_roc_dur);
2763 duration += wpa_s->extra_roc_dur;
2764 }
2765#endif /* CONFIG_TESTING_OPTIONS */
2766
2767 if (wpa_drv_remain_on_channel(wpa_s, lwork->freq, duration) < 0) {
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002768 wpa_printf(MSG_DEBUG, "P2P: Failed to request the driver "
2769 "to remain on channel (%u MHz) for Listen "
2770 "state", lwork->freq);
2771 wpas_p2p_listen_work_done(wpa_s);
2772 wpa_s->pending_listen_freq = 0;
2773 return;
2774 }
2775 wpa_s->off_channel_freq = 0;
2776 wpa_s->roc_waiting_drv_freq = lwork->freq;
2777}
2778
2779
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002780static int wpas_start_listen(void *ctx, unsigned int freq,
2781 unsigned int duration,
2782 const struct wpabuf *probe_resp_ie)
2783{
2784 struct wpa_supplicant *wpa_s = ctx;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002785 struct wpas_p2p_listen_work *lwork;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002786
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002787 if (wpa_s->p2p_listen_work) {
2788 wpa_printf(MSG_DEBUG, "P2P: Reject start_listen since p2p_listen_work already exists");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002789 return -1;
2790 }
2791
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002792 lwork = os_zalloc(sizeof(*lwork));
2793 if (lwork == NULL)
2794 return -1;
2795 lwork->freq = freq;
2796 lwork->duration = duration;
2797 if (probe_resp_ie) {
2798 lwork->probe_resp_ie = wpabuf_dup(probe_resp_ie);
2799 if (lwork->probe_resp_ie == NULL) {
2800 wpas_p2p_listen_work_free(lwork);
2801 return -1;
2802 }
2803 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002804
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002805 if (radio_add_work(wpa_s, freq, "p2p-listen", 0, wpas_start_listen_cb,
2806 lwork) < 0) {
2807 wpas_p2p_listen_work_free(lwork);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002808 return -1;
2809 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002810
2811 return 0;
2812}
2813
2814
2815static void wpas_stop_listen(void *ctx)
2816{
2817 struct wpa_supplicant *wpa_s = ctx;
2818 if (wpa_s->off_channel_freq || wpa_s->roc_waiting_drv_freq) {
2819 wpa_drv_cancel_remain_on_channel(wpa_s);
2820 wpa_s->off_channel_freq = 0;
2821 wpa_s->roc_waiting_drv_freq = 0;
2822 }
2823 wpa_drv_set_ap_wps_ie(wpa_s, NULL, NULL, NULL);
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07002824
2825 /*
2826 * Don't cancel Probe Request RX reporting for a connected P2P Client
2827 * handling Probe Request frames.
2828 */
2829 if (!wpa_s->p2p_cli_probe)
2830 wpa_drv_probe_req_report(wpa_s, 0);
2831
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002832 wpas_p2p_listen_work_done(wpa_s);
Sunil Ravi2a14cf12023-11-21 00:54:38 +00002833
2834 if (radio_work_pending(wpa_s, "p2p-listen")) {
2835 wpa_printf(MSG_DEBUG,
2836 "P2P: p2p-listen is still pending - remove it");
2837 radio_remove_works(wpa_s, "p2p-listen", 0);
2838 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002839}
2840
2841
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07002842static int wpas_send_probe_resp(void *ctx, const struct wpabuf *buf,
2843 unsigned int freq)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002844{
2845 struct wpa_supplicant *wpa_s = ctx;
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07002846 return wpa_drv_send_mlme(wpa_s, wpabuf_head(buf), wpabuf_len(buf), 1,
Hai Shalomfdcde762020-04-02 11:19:20 -07002847 freq, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002848}
2849
2850
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002851static void wpas_prov_disc_local_display(struct wpa_supplicant *wpa_s,
2852 const u8 *peer, const char *params,
2853 unsigned int generated_pin)
2854{
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07002855 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_SHOW_PIN MACSTR
2856 " %08d%s", MAC2STR(peer), generated_pin, params);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002857}
2858
2859
2860static void wpas_prov_disc_local_keypad(struct wpa_supplicant *wpa_s,
2861 const u8 *peer, const char *params)
2862{
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07002863 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_ENTER_PIN MACSTR
2864 "%s", MAC2STR(peer), params);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002865}
2866
2867
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002868static void wpas_prov_disc_req(void *ctx, const u8 *peer, u16 config_methods,
2869 const u8 *dev_addr, const u8 *pri_dev_type,
2870 const char *dev_name, u16 supp_config_methods,
2871 u8 dev_capab, u8 group_capab, const u8 *group_id,
2872 size_t group_id_len)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002873{
2874 struct wpa_supplicant *wpa_s = ctx;
2875 char devtype[WPS_DEV_TYPE_BUFSIZE];
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002876 char params[300];
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002877 u8 empty_dev_type[8];
2878 unsigned int generated_pin = 0;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002879 struct wpa_supplicant *group = NULL;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002880 int res;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002881
2882 if (group_id) {
2883 for (group = wpa_s->global->ifaces; group; group = group->next)
2884 {
2885 struct wpa_ssid *s = group->current_ssid;
2886 if (s != NULL &&
2887 s->mode == WPAS_MODE_P2P_GO &&
2888 group_id_len - ETH_ALEN == s->ssid_len &&
2889 os_memcmp(group_id + ETH_ALEN, s->ssid,
2890 s->ssid_len) == 0)
2891 break;
2892 }
2893 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002894
2895 if (pri_dev_type == NULL) {
2896 os_memset(empty_dev_type, 0, sizeof(empty_dev_type));
2897 pri_dev_type = empty_dev_type;
2898 }
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002899 res = os_snprintf(params, sizeof(params), " p2p_dev_addr=" MACSTR
2900 " pri_dev_type=%s name='%s' config_methods=0x%x "
2901 "dev_capab=0x%x group_capab=0x%x%s%s",
2902 MAC2STR(dev_addr),
2903 wps_dev_type_bin2str(pri_dev_type, devtype,
2904 sizeof(devtype)),
2905 dev_name, supp_config_methods, dev_capab, group_capab,
2906 group ? " group=" : "",
2907 group ? group->ifname : "");
2908 if (os_snprintf_error(sizeof(params), res))
2909 wpa_printf(MSG_DEBUG, "P2P: PD Request event truncated");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002910 params[sizeof(params) - 1] = '\0';
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08002911
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002912 if (config_methods & WPS_CONFIG_DISPLAY) {
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08002913 if (wps_generate_pin(&generated_pin) < 0) {
2914 wpa_printf(MSG_DEBUG, "P2P: Could not generate PIN");
2915 wpas_notify_p2p_provision_discovery(
2916 wpa_s, peer, 0 /* response */,
Sunil Ravi8bae4252023-11-16 03:05:19 +00002917 P2P_PROV_DISC_INFO_UNAVAILABLE, 0, 0, NULL);
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08002918 return;
2919 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002920 wpas_prov_disc_local_display(wpa_s, peer, params,
2921 generated_pin);
2922 } else if (config_methods & WPS_CONFIG_KEYPAD)
2923 wpas_prov_disc_local_keypad(wpa_s, peer, params);
2924 else if (config_methods & WPS_CONFIG_PUSHBUTTON)
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07002925 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_PBC_REQ
2926 MACSTR "%s", MAC2STR(peer), params);
Jouni Malinen75ecf522011-06-27 15:19:46 -07002927
2928 wpas_notify_p2p_provision_discovery(wpa_s, peer, 1 /* request */,
2929 P2P_PROV_DISC_SUCCESS,
Sunil Ravi8bae4252023-11-16 03:05:19 +00002930 config_methods, generated_pin,
2931 group ? group->ifname : NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002932}
2933
2934
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002935static void wpas_prov_disc_resp(void *ctx, const u8 *peer, u16 config_methods)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002936{
2937 struct wpa_supplicant *wpa_s = ctx;
2938 unsigned int generated_pin = 0;
Dmitry Shmidt04949592012-07-19 12:16:46 -07002939 char params[20];
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002940
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08002941 if (wpa_s->pending_pd_before_join &&
Sunil Ravib0ac25f2024-07-12 01:42:03 +00002942 (ether_addr_equal(peer, wpa_s->pending_join_dev_addr) ||
2943 ether_addr_equal(peer, wpa_s->pending_join_iface_addr))) {
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08002944 wpa_s->pending_pd_before_join = 0;
2945 wpa_printf(MSG_DEBUG, "P2P: Starting pending "
2946 "join-existing-group operation");
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08002947 wpas_p2p_join_start(wpa_s, 0, NULL, 0);
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08002948 return;
2949 }
2950
Dmitry Shmidt04949592012-07-19 12:16:46 -07002951 if (wpa_s->pending_pd_use == AUTO_PD_JOIN ||
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002952 wpa_s->pending_pd_use == AUTO_PD_GO_NEG) {
2953 int res;
2954
2955 res = os_snprintf(params, sizeof(params), " peer_go=%d",
2956 wpa_s->pending_pd_use == AUTO_PD_JOIN);
2957 if (os_snprintf_error(sizeof(params), res))
2958 params[sizeof(params) - 1] = '\0';
2959 } else
Dmitry Shmidt04949592012-07-19 12:16:46 -07002960 params[0] = '\0';
2961
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002962 if (config_methods & WPS_CONFIG_DISPLAY)
Dmitry Shmidt04949592012-07-19 12:16:46 -07002963 wpas_prov_disc_local_keypad(wpa_s, peer, params);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002964 else if (config_methods & WPS_CONFIG_KEYPAD) {
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08002965 if (wps_generate_pin(&generated_pin) < 0) {
2966 wpa_printf(MSG_DEBUG, "P2P: Could not generate PIN");
2967 wpas_notify_p2p_provision_discovery(
2968 wpa_s, peer, 0 /* response */,
Sunil Ravi8bae4252023-11-16 03:05:19 +00002969 P2P_PROV_DISC_INFO_UNAVAILABLE, 0, 0, NULL);
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08002970 return;
2971 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07002972 wpas_prov_disc_local_display(wpa_s, peer, params,
2973 generated_pin);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002974 } else if (config_methods & WPS_CONFIG_PUSHBUTTON)
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07002975 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_PBC_RESP
2976 MACSTR "%s", MAC2STR(peer), params);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002977
Jouni Malinen75ecf522011-06-27 15:19:46 -07002978 wpas_notify_p2p_provision_discovery(wpa_s, peer, 0 /* response */,
2979 P2P_PROV_DISC_SUCCESS,
Sunil Ravi8bae4252023-11-16 03:05:19 +00002980 config_methods, generated_pin,
2981 NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002982}
2983
2984
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002985static void wpas_prov_disc_fail(void *ctx, const u8 *peer,
Dmitry Shmidt216983b2015-02-06 10:50:36 -08002986 enum p2p_prov_disc_status status,
2987 u32 adv_id, const u8 *adv_mac,
2988 const char *deferred_session_resp)
Jouni Malinen75ecf522011-06-27 15:19:46 -07002989{
2990 struct wpa_supplicant *wpa_s = ctx;
2991
Dmitry Shmidt04949592012-07-19 12:16:46 -07002992 if (wpa_s->p2p_fallback_to_go_neg) {
2993 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: PD for p2p_connect-auto "
2994 "failed - fall back to GO Negotiation");
Dmitry Shmidt9c175262016-03-03 10:20:07 -08002995 wpa_msg_global(wpa_s->p2pdev, MSG_INFO,
Dmitry Shmidt7f656022015-02-25 14:36:37 -08002996 P2P_EVENT_FALLBACK_TO_GO_NEG
2997 "reason=PD-failed");
Dmitry Shmidt04949592012-07-19 12:16:46 -07002998 wpas_p2p_fallback_to_go_neg(wpa_s, 0);
2999 return;
3000 }
3001
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08003002 if (status == P2P_PROV_DISC_TIMEOUT_JOIN) {
Dmitry Shmidt2b380482012-09-13 10:52:13 -07003003 wpa_s->pending_pd_before_join = 0;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08003004 wpa_printf(MSG_DEBUG, "P2P: Starting pending "
3005 "join-existing-group operation (no ACK for PD "
3006 "Req attempts)");
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08003007 wpas_p2p_join_start(wpa_s, 0, NULL, 0);
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08003008 return;
Dmitry Shmidt2b380482012-09-13 10:52:13 -07003009 }
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08003010
Dmitry Shmidt216983b2015-02-06 10:50:36 -08003011 if (adv_id && adv_mac && deferred_session_resp) {
3012 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_FAILURE
3013 " p2p_dev_addr=" MACSTR " status=%d adv_id=%x"
3014 " deferred_session_resp='%s'",
3015 MAC2STR(peer), status, adv_id,
3016 deferred_session_resp);
3017 } else if (adv_id && adv_mac) {
3018 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_FAILURE
3019 " p2p_dev_addr=" MACSTR " status=%d adv_id=%x",
3020 MAC2STR(peer), status, adv_id);
3021 } else {
3022 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_FAILURE
3023 " p2p_dev_addr=" MACSTR " status=%d",
3024 MAC2STR(peer), status);
3025 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07003026
Jouni Malinen75ecf522011-06-27 15:19:46 -07003027 wpas_notify_p2p_provision_discovery(wpa_s, peer, 0 /* response */,
Sunil Ravi8bae4252023-11-16 03:05:19 +00003028 status, 0, 0, NULL);
Jouni Malinen75ecf522011-06-27 15:19:46 -07003029}
3030
3031
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003032static int freq_included(struct wpa_supplicant *wpa_s,
3033 const struct p2p_channels *channels,
3034 unsigned int freq)
Dmitry Shmidtf7e0a992013-05-23 11:03:10 -07003035{
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003036 if ((channels == NULL || p2p_channels_includes_freq(channels, freq)) &&
3037 wpas_p2p_go_is_peer_freq(wpa_s, freq))
3038 return 1;
3039 return 0;
3040}
Dmitry Shmidtf7e0a992013-05-23 11:03:10 -07003041
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003042
3043static void wpas_p2p_go_update_common_freqs(struct wpa_supplicant *wpa_s)
3044{
3045 unsigned int num = P2P_MAX_CHANNELS;
3046 int *common_freqs;
3047 int ret;
3048
3049 p2p_go_dump_common_freqs(wpa_s);
3050 common_freqs = os_calloc(num, sizeof(int));
3051 if (!common_freqs)
3052 return;
3053
3054 ret = p2p_group_get_common_freqs(wpa_s->p2p_group, common_freqs, &num);
3055 if (ret < 0) {
3056 wpa_dbg(wpa_s, MSG_DEBUG,
3057 "P2P: Failed to get group common freqs");
3058 os_free(common_freqs);
3059 return;
3060 }
3061
3062 os_free(wpa_s->p2p_group_common_freqs);
3063 wpa_s->p2p_group_common_freqs = common_freqs;
3064 wpa_s->p2p_group_common_freqs_num = num;
3065 p2p_go_dump_common_freqs(wpa_s);
3066}
3067
3068
3069/*
3070 * Check if the given frequency is one of the possible operating frequencies
3071 * set after the completion of the GO Negotiation.
3072 */
3073static int wpas_p2p_go_is_peer_freq(struct wpa_supplicant *wpa_s, int freq)
3074{
3075 unsigned int i;
3076
3077 p2p_go_dump_common_freqs(wpa_s);
3078
3079 /* assume no restrictions */
3080 if (!wpa_s->p2p_group_common_freqs_num)
3081 return 1;
3082
3083 for (i = 0; i < wpa_s->p2p_group_common_freqs_num; i++) {
3084 if (wpa_s->p2p_group_common_freqs[i] == freq)
3085 return 1;
3086 }
3087 return 0;
3088}
3089
3090
3091static int wpas_sta_check_ecsa(struct hostapd_data *hapd,
3092 struct sta_info *sta, void *ctx)
3093{
3094 int *ecsa_support = ctx;
3095
3096 *ecsa_support &= sta->ecsa_supported;
3097
3098 return 0;
3099}
3100
3101
3102/* Check if all the peers support eCSA */
3103static int wpas_p2p_go_clients_support_ecsa(struct wpa_supplicant *wpa_s)
3104{
3105 int ecsa_support = 1;
3106
3107 ap_for_each_sta(wpa_s->ap_iface->bss[0], wpas_sta_check_ecsa,
3108 &ecsa_support);
3109
3110 return ecsa_support;
Dmitry Shmidtf7e0a992013-05-23 11:03:10 -07003111}
3112
3113
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07003114/**
3115 * Pick the best frequency to use from all the currently used frequencies.
3116 */
3117static int wpas_p2p_pick_best_used_freq(struct wpa_supplicant *wpa_s,
3118 struct wpa_used_freq_data *freqs,
3119 unsigned int num)
3120{
3121 unsigned int i, c;
3122
3123 /* find a candidate freq that is supported by P2P */
3124 for (c = 0; c < num; c++)
3125 if (p2p_supported_freq(wpa_s->global->p2p, freqs[c].freq))
3126 break;
3127
3128 if (c == num)
3129 return 0;
3130
3131 /* once we have a candidate, try to find a 'better' one */
3132 for (i = c + 1; i < num; i++) {
3133 if (!p2p_supported_freq(wpa_s->global->p2p, freqs[i].freq))
3134 continue;
3135
3136 /*
3137 * 1. Infrastructure station interfaces have higher preference.
3138 * 2. P2P Clients have higher preference.
3139 * 3. All others.
3140 */
3141 if (freqs[i].flags & WPA_FREQ_USED_BY_INFRA_STATION) {
3142 c = i;
3143 break;
3144 }
3145
3146 if ((freqs[i].flags & WPA_FREQ_USED_BY_P2P_CLIENT))
3147 c = i;
3148 }
3149 return freqs[c].freq;
3150}
3151
3152
Sunil Ravi036cec52023-03-29 11:35:17 -07003153/**
3154 * Pick the best frequency the driver suggests.
3155 *
3156 * num_pref_freq is used as both input and output
3157 * - input: the max size of pref_freq_list,
3158 * - output: the valid size of pref_freq_list filled with data.
3159 */
3160static int wpas_p2p_pick_best_pref_freq(struct wpa_supplicant *wpa_s, bool go,
3161 struct weighted_pcl *pref_freq_list,
3162 unsigned int *num_pref_freq)
3163{
3164 int best_freq = 0;
3165 unsigned int max_pref_freq, i;
3166 int res;
3167 enum wpa_driver_if_type iface_type;
3168
3169 max_pref_freq = *num_pref_freq;
3170 *num_pref_freq = 0;
3171
3172 if (go)
3173 iface_type = WPA_IF_P2P_GO;
3174 else
3175 iface_type = WPA_IF_P2P_CLIENT;
3176
3177 res = wpa_drv_get_pref_freq_list(wpa_s, iface_type, &max_pref_freq,
3178 pref_freq_list);
3179 if (!res && !is_p2p_allow_6ghz(wpa_s->global->p2p))
3180 max_pref_freq = p2p_remove_6ghz_channels(pref_freq_list,
3181 max_pref_freq);
3182 if (res || !max_pref_freq) {
3183 wpa_printf(MSG_DEBUG,
3184 "P2P: No preferred frequency list available");
3185 return 0;
3186 }
3187
3188 *num_pref_freq = max_pref_freq;
3189 i = 0;
3190 while (i < *num_pref_freq &&
3191 (!p2p_supported_freq(wpa_s->global->p2p,
3192 pref_freq_list[i].freq) ||
3193 wpas_p2p_disallowed_freq(wpa_s->global,
3194 pref_freq_list[i].freq) ||
3195 !p2p_pref_freq_allowed(&pref_freq_list[i], go))) {
3196 wpa_printf(MSG_DEBUG,
3197 "P2P: preferred_freq_list[%d]=%d is disallowed",
3198 i, pref_freq_list[i].freq);
3199 i++;
3200 }
3201 if (i != *num_pref_freq) {
3202 best_freq = pref_freq_list[i].freq;
3203 wpa_printf(MSG_DEBUG, "P2P: Using preferred_freq_list[%d]=%d",
3204 i, best_freq);
3205 } else {
3206 wpa_printf(MSG_DEBUG,
3207 "P2P: All driver preferred frequencies are disallowed for P2P use");
3208 *num_pref_freq = 0;
3209 }
3210
3211 return best_freq;
3212}
3213
3214
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003215static u8 wpas_invitation_process(void *ctx, const u8 *sa, const u8 *bssid,
3216 const u8 *go_dev_addr, const u8 *ssid,
3217 size_t ssid_len, int *go, u8 *group_bssid,
Dmitry Shmidtf7e0a992013-05-23 11:03:10 -07003218 int *force_freq, int persistent_group,
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08003219 const struct p2p_channels *channels,
3220 int dev_pw_id)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003221{
3222 struct wpa_supplicant *wpa_s = ctx;
3223 struct wpa_ssid *s;
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07003224 struct wpa_used_freq_data *freqs;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003225 struct wpa_supplicant *grp;
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07003226 int best_freq;
Sunil Ravi036cec52023-03-29 11:35:17 -07003227 struct weighted_pcl pref_freq_list[P2P_MAX_PREF_CHANNELS];
3228 unsigned int num_pref_freq;
3229 int res;
3230
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003231
3232 if (!persistent_group) {
3233 wpa_printf(MSG_DEBUG, "P2P: Invitation from " MACSTR
Dmitry Shmidt344abd32014-01-14 13:17:00 -08003234 " to join an active group (SSID: %s)",
3235 MAC2STR(sa), wpa_ssid_txt(ssid, ssid_len));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003236 if (!is_zero_ether_addr(wpa_s->p2p_auth_invite) &&
Sunil Ravib0ac25f2024-07-12 01:42:03 +00003237 (ether_addr_equal(go_dev_addr, wpa_s->p2p_auth_invite) ||
3238 ether_addr_equal(sa, wpa_s->p2p_auth_invite))) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003239 wpa_printf(MSG_DEBUG, "P2P: Accept previously "
3240 "authorized invitation");
3241 goto accept_inv;
3242 }
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08003243
3244#ifdef CONFIG_WPS_NFC
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08003245 if (dev_pw_id >= 0 && wpa_s->p2p_nfc_tag_enabled &&
3246 dev_pw_id == wpa_s->p2p_oob_dev_pw_id) {
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08003247 wpa_printf(MSG_DEBUG, "P2P: Accept invitation based on local enabled NFC Tag");
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08003248 wpa_s->p2p_wps_method = WPS_NFC;
3249 wpa_s->pending_join_wps_method = WPS_NFC;
3250 os_memcpy(wpa_s->pending_join_dev_addr,
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08003251 go_dev_addr, ETH_ALEN);
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08003252 os_memcpy(wpa_s->pending_join_iface_addr,
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08003253 bssid, ETH_ALEN);
3254 goto accept_inv;
3255 }
3256#endif /* CONFIG_WPS_NFC */
3257
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003258 /*
3259 * Do not accept the invitation automatically; notify user and
3260 * request approval.
3261 */
3262 return P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE;
3263 }
3264
3265 grp = wpas_get_p2p_group(wpa_s, ssid, ssid_len, go);
3266 if (grp) {
3267 wpa_printf(MSG_DEBUG, "P2P: Accept invitation to already "
3268 "running persistent group");
3269 if (*go)
3270 os_memcpy(group_bssid, grp->own_addr, ETH_ALEN);
3271 goto accept_inv;
3272 }
3273
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08003274 if (!is_zero_ether_addr(wpa_s->p2p_auth_invite) &&
Sunil Ravib0ac25f2024-07-12 01:42:03 +00003275 ether_addr_equal(sa, wpa_s->p2p_auth_invite)) {
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08003276 wpa_printf(MSG_DEBUG, "P2P: Accept previously initiated "
3277 "invitation to re-invoke a persistent group");
Dmitry Shmidt76cd2cc2014-05-27 12:56:04 -07003278 os_memset(wpa_s->p2p_auth_invite, 0, ETH_ALEN);
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08003279 } else if (!wpa_s->conf->persistent_reconnect)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003280 return P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE;
3281
3282 for (s = wpa_s->conf->ssid; s; s = s->next) {
3283 if (s->disabled == 2 &&
Sunil Ravib0ac25f2024-07-12 01:42:03 +00003284 ether_addr_equal(s->bssid, go_dev_addr) &&
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003285 s->ssid_len == ssid_len &&
3286 os_memcmp(ssid, s->ssid, ssid_len) == 0)
3287 break;
3288 }
3289
3290 if (!s) {
3291 wpa_printf(MSG_DEBUG, "P2P: Invitation from " MACSTR
3292 " requested reinvocation of an unknown group",
3293 MAC2STR(sa));
3294 return P2P_SC_FAIL_UNKNOWN_GROUP;
3295 }
3296
3297 if (s->mode == WPAS_MODE_P2P_GO && !wpas_p2p_create_iface(wpa_s)) {
3298 *go = 1;
3299 if (wpa_s->wpa_state >= WPA_AUTHENTICATING) {
3300 wpa_printf(MSG_DEBUG, "P2P: The only available "
3301 "interface is already in use - reject "
3302 "invitation");
3303 return P2P_SC_FAIL_UNABLE_TO_ACCOMMODATE;
3304 }
Dmitry Shmidt9c175262016-03-03 10:20:07 -08003305 if (wpa_s->p2p_mgmt)
3306 os_memcpy(group_bssid, wpa_s->parent->own_addr,
3307 ETH_ALEN);
3308 else
3309 os_memcpy(group_bssid, wpa_s->own_addr, ETH_ALEN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003310 } else if (s->mode == WPAS_MODE_P2P_GO) {
3311 *go = 1;
3312 if (wpas_p2p_add_group_interface(wpa_s, WPA_IF_P2P_GO) < 0)
3313 {
3314 wpa_printf(MSG_ERROR, "P2P: Failed to allocate a new "
3315 "interface address for the group");
3316 return P2P_SC_FAIL_UNABLE_TO_ACCOMMODATE;
3317 }
3318 os_memcpy(group_bssid, wpa_s->pending_interface_addr,
3319 ETH_ALEN);
3320 }
3321
3322accept_inv:
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003323 wpas_p2p_set_own_freq_preference(wpa_s, 0);
3324
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07003325 best_freq = 0;
3326 freqs = os_calloc(wpa_s->num_multichan_concurrent,
3327 sizeof(struct wpa_used_freq_data));
3328 if (freqs) {
3329 int num_channels = wpa_s->num_multichan_concurrent;
3330 int num = wpas_p2p_valid_oper_freqs(wpa_s, freqs, num_channels);
3331 best_freq = wpas_p2p_pick_best_used_freq(wpa_s, freqs, num);
3332 os_free(freqs);
3333 }
3334
Sunil Ravi036cec52023-03-29 11:35:17 -07003335 num_pref_freq = P2P_MAX_PREF_CHANNELS;
3336 res = wpas_p2p_pick_best_pref_freq(wpa_s, *go, pref_freq_list,
3337 &num_pref_freq);
3338 if (res > 0)
3339 best_freq = res;
3340
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07003341 /* Get one of the frequencies currently in use */
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07003342 if (best_freq > 0) {
Dmitry Shmidta0d265f2013-11-19 13:13:41 -08003343 wpa_printf(MSG_DEBUG, "P2P: Trying to prefer a channel already used by one of the interfaces");
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07003344 wpas_p2p_set_own_freq_preference(wpa_s, best_freq);
Dmitry Shmidta0d265f2013-11-19 13:13:41 -08003345
3346 if (wpa_s->num_multichan_concurrent < 2 ||
3347 wpas_p2p_num_unused_channels(wpa_s) < 1) {
3348 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 -07003349 *force_freq = best_freq;
Dmitry Shmidta0d265f2013-11-19 13:13:41 -08003350 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003351 }
3352
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07003353 if (*force_freq > 0 && wpa_s->num_multichan_concurrent > 1 &&
3354 wpas_p2p_num_unused_channels(wpa_s) > 0) {
Dmitry Shmidtf7e0a992013-05-23 11:03:10 -07003355 if (*go == 0) {
3356 /* We are the client */
3357 wpa_printf(MSG_DEBUG, "P2P: Peer was found to be "
3358 "running a GO but we are capable of MCC, "
3359 "figure out the best channel to use");
3360 *force_freq = 0;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003361 } else if (!freq_included(wpa_s, channels, *force_freq)) {
Dmitry Shmidtf7e0a992013-05-23 11:03:10 -07003362 /* We are the GO, and *force_freq is not in the
3363 * intersection */
3364 wpa_printf(MSG_DEBUG, "P2P: Forced GO freq %d MHz not "
3365 "in intersection but we are capable of MCC, "
3366 "figure out the best channel to use",
3367 *force_freq);
3368 *force_freq = 0;
3369 }
3370 }
3371
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003372 return P2P_SC_SUCCESS;
3373}
3374
3375
3376static void wpas_invitation_received(void *ctx, const u8 *sa, const u8 *bssid,
3377 const u8 *ssid, size_t ssid_len,
3378 const u8 *go_dev_addr, u8 status,
3379 int op_freq)
3380{
3381 struct wpa_supplicant *wpa_s = ctx;
3382 struct wpa_ssid *s;
3383
3384 for (s = wpa_s->conf->ssid; s; s = s->next) {
3385 if (s->disabled == 2 &&
3386 s->ssid_len == ssid_len &&
3387 os_memcmp(ssid, s->ssid, ssid_len) == 0)
3388 break;
3389 }
3390
3391 if (status == P2P_SC_SUCCESS) {
3392 wpa_printf(MSG_DEBUG, "P2P: Invitation from peer " MACSTR
Dmitry Shmidt344abd32014-01-14 13:17:00 -08003393 " was accepted; op_freq=%d MHz, SSID=%s",
3394 MAC2STR(sa), op_freq, wpa_ssid_txt(ssid, ssid_len));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003395 if (s) {
Dmitry Shmidt91c40cd2012-09-25 14:23:53 -07003396 int go = s->mode == WPAS_MODE_P2P_GO;
Dmitry Shmidt849734c2016-05-27 09:59:01 -07003397 if (go) {
3398 wpa_msg_global(wpa_s, MSG_INFO,
3399 P2P_EVENT_INVITATION_ACCEPTED
3400 "sa=" MACSTR
3401 " persistent=%d freq=%d",
3402 MAC2STR(sa), s->id, op_freq);
3403 } else {
3404 wpa_msg_global(wpa_s, MSG_INFO,
3405 P2P_EVENT_INVITATION_ACCEPTED
3406 "sa=" MACSTR
3407 " persistent=%d",
3408 MAC2STR(sa), s->id);
3409 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003410 wpas_p2p_group_add_persistent(
Hai Shalomb755a2a2020-04-23 21:49:02 -07003411 wpa_s, s, go, 0, op_freq, 0,
3412 wpa_s->conf->p2p_go_ht40,
3413 wpa_s->conf->p2p_go_vht,
3414 0,
3415 wpa_s->conf->p2p_go_he,
3416 wpa_s->conf->p2p_go_edmg, NULL,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003417 go ? P2P_MAX_INITIAL_CONN_WAIT_GO_REINVOKE : 0,
Matthew Wang124e5f42022-12-29 07:23:06 +00003418 1, is_p2p_allow_6ghz(wpa_s->global->p2p), 0,
Sunil Ravi036cec52023-03-29 11:35:17 -07003419 NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003420 } else if (bssid) {
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08003421 wpa_s->user_initiated_pd = 0;
Dmitry Shmidt849734c2016-05-27 09:59:01 -07003422 wpa_msg_global(wpa_s, MSG_INFO,
3423 P2P_EVENT_INVITATION_ACCEPTED
3424 "sa=" MACSTR " go_dev_addr=" MACSTR
3425 " bssid=" MACSTR " unknown-network",
3426 MAC2STR(sa), MAC2STR(go_dev_addr),
3427 MAC2STR(bssid));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003428 wpas_p2p_join(wpa_s, bssid, go_dev_addr,
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08003429 wpa_s->p2p_wps_method, 0, op_freq,
Dmitry Shmidt344abd32014-01-14 13:17:00 -08003430 ssid, ssid_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003431 }
3432 return;
3433 }
3434
3435 if (status != P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE) {
3436 wpa_printf(MSG_DEBUG, "P2P: Invitation from peer " MACSTR
3437 " was rejected (status %u)", MAC2STR(sa), status);
3438 return;
3439 }
3440
3441 if (!s) {
3442 if (bssid) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07003443 wpa_msg_global(wpa_s, MSG_INFO,
3444 P2P_EVENT_INVITATION_RECEIVED
3445 "sa=" MACSTR " go_dev_addr=" MACSTR
3446 " bssid=" MACSTR " unknown-network",
3447 MAC2STR(sa), MAC2STR(go_dev_addr),
3448 MAC2STR(bssid));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003449 } else {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07003450 wpa_msg_global(wpa_s, MSG_INFO,
3451 P2P_EVENT_INVITATION_RECEIVED
3452 "sa=" MACSTR " go_dev_addr=" MACSTR
3453 " unknown-network",
3454 MAC2STR(sa), MAC2STR(go_dev_addr));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003455 }
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003456 wpas_notify_p2p_invitation_received(wpa_s, sa, go_dev_addr,
3457 bssid, 0, op_freq);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003458 return;
3459 }
3460
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08003461 if (s->mode == WPAS_MODE_P2P_GO && op_freq) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07003462 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_INVITATION_RECEIVED
3463 "sa=" MACSTR " persistent=%d freq=%d",
3464 MAC2STR(sa), s->id, op_freq);
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08003465 } else {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07003466 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_INVITATION_RECEIVED
3467 "sa=" MACSTR " persistent=%d",
3468 MAC2STR(sa), s->id);
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08003469 }
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003470 wpas_notify_p2p_invitation_received(wpa_s, sa, go_dev_addr, bssid,
3471 s->id, op_freq);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003472}
3473
3474
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003475static void wpas_remove_persistent_peer(struct wpa_supplicant *wpa_s,
3476 struct wpa_ssid *ssid,
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07003477 const u8 *peer, int inv)
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003478{
3479 size_t i;
Dmitry Shmidt8bd70b72015-05-26 16:02:19 -07003480 struct wpa_supplicant *p2p_wpa_s = wpa_s->global->p2p_init_wpa_s;
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003481
3482 if (ssid == NULL)
3483 return;
3484
3485 for (i = 0; ssid->p2p_client_list && i < ssid->num_p2p_clients; i++) {
Sunil Ravib0ac25f2024-07-12 01:42:03 +00003486 if (ether_addr_equal(ssid->p2p_client_list + i * 2 * ETH_ALEN,
3487 peer))
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003488 break;
3489 }
Dmitry Shmidt6aa8ae42014-07-07 09:31:33 -07003490 if (i >= ssid->num_p2p_clients || !ssid->p2p_client_list) {
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003491 if (ssid->mode != WPAS_MODE_P2P_GO &&
Sunil Ravib0ac25f2024-07-12 01:42:03 +00003492 ether_addr_equal(ssid->bssid, peer)) {
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003493 wpa_printf(MSG_DEBUG, "P2P: Remove persistent group %d "
3494 "due to invitation result", ssid->id);
3495 wpas_notify_network_removed(wpa_s, ssid);
3496 wpa_config_remove_network(wpa_s->conf, ssid->id);
3497 return;
3498 }
3499 return; /* Peer not found in client list */
3500 }
3501
3502 wpa_printf(MSG_DEBUG, "P2P: Remove peer " MACSTR " from persistent "
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07003503 "group %d client list%s",
3504 MAC2STR(peer), ssid->id,
3505 inv ? " due to invitation result" : "");
Dmitry Shmidtff787d52015-01-12 13:01:47 -08003506 os_memmove(ssid->p2p_client_list + i * 2 * ETH_ALEN,
3507 ssid->p2p_client_list + (i + 1) * 2 * ETH_ALEN,
3508 (ssid->num_p2p_clients - i - 1) * 2 * ETH_ALEN);
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003509 ssid->num_p2p_clients--;
Dmitry Shmidt8bd70b72015-05-26 16:02:19 -07003510 if (p2p_wpa_s->conf->update_config &&
3511 wpa_config_write(p2p_wpa_s->confname, p2p_wpa_s->conf))
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003512 wpa_printf(MSG_DEBUG, "P2P: Failed to update configuration");
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003513}
3514
3515
3516static void wpas_remove_persistent_client(struct wpa_supplicant *wpa_s,
3517 const u8 *peer)
3518{
3519 struct wpa_ssid *ssid;
3520
3521 wpa_s = wpa_s->global->p2p_invite_group;
3522 if (wpa_s == NULL)
3523 return; /* No known invitation group */
3524 ssid = wpa_s->current_ssid;
3525 if (ssid == NULL || ssid->mode != WPAS_MODE_P2P_GO ||
3526 !ssid->p2p_persistent_group)
3527 return; /* Not operating as a GO in persistent group */
Dmitry Shmidt9c175262016-03-03 10:20:07 -08003528 ssid = wpas_p2p_get_persistent(wpa_s->p2pdev, peer,
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003529 ssid->ssid, ssid->ssid_len);
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07003530 wpas_remove_persistent_peer(wpa_s, ssid, peer, 1);
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003531}
3532
3533
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08003534static void wpas_invitation_result(void *ctx, int status, const u8 *bssid,
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003535 const struct p2p_channels *channels,
Dmitry Shmidt15907092014-03-25 10:42:57 -07003536 const u8 *peer, int neg_freq,
3537 int peer_oper_freq)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003538{
3539 struct wpa_supplicant *wpa_s = ctx;
3540 struct wpa_ssid *ssid;
Dmitry Shmidt15907092014-03-25 10:42:57 -07003541 int freq;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003542
3543 if (bssid) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07003544 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_INVITATION_RESULT
3545 "status=%d " MACSTR,
3546 status, MAC2STR(bssid));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003547 } else {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07003548 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_INVITATION_RESULT
3549 "status=%d ", status);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003550 }
3551 wpas_notify_p2p_invitation_result(wpa_s, status, bssid);
3552
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003553 wpa_printf(MSG_DEBUG, "P2P: Invitation result - status=%d peer=" MACSTR,
3554 status, MAC2STR(peer));
3555 if (wpa_s->pending_invite_ssid_id == -1) {
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08003556 struct wpa_supplicant *group_if =
3557 wpa_s->global->p2p_invite_group;
3558
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003559 if (status == P2P_SC_FAIL_UNKNOWN_GROUP)
3560 wpas_remove_persistent_client(wpa_s, peer);
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08003561
3562 /*
3563 * Invitation to an active group. If this is successful and we
3564 * are the GO, set the client wait to postpone some concurrent
3565 * operations and to allow provisioning and connection to happen
3566 * more quickly.
3567 */
3568 if (status == P2P_SC_SUCCESS &&
3569 group_if && group_if->current_ssid &&
3570 group_if->current_ssid->mode == WPAS_MODE_P2P_GO) {
3571 os_get_reltime(&wpa_s->global->p2p_go_wait_client);
3572#ifdef CONFIG_TESTING_OPTIONS
3573 if (group_if->p2p_go_csa_on_inv) {
3574 wpa_printf(MSG_DEBUG,
3575 "Testing: force P2P GO CSA after invitation");
3576 eloop_cancel_timeout(
3577 wpas_p2p_reconsider_moving_go,
3578 wpa_s, NULL);
3579 eloop_register_timeout(
3580 0, 50000,
3581 wpas_p2p_reconsider_moving_go,
3582 wpa_s, NULL);
3583 }
3584#endif /* CONFIG_TESTING_OPTIONS */
3585 }
3586 return;
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003587 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003588
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08003589 if (status == P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE) {
3590 wpa_printf(MSG_DEBUG, "P2P: Waiting for peer to start another "
3591 "invitation exchange to indicate readiness for "
3592 "re-invocation");
3593 }
3594
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003595 if (status != P2P_SC_SUCCESS) {
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003596 if (status == P2P_SC_FAIL_UNKNOWN_GROUP) {
3597 ssid = wpa_config_get_network(
3598 wpa_s->conf, wpa_s->pending_invite_ssid_id);
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07003599 wpas_remove_persistent_peer(wpa_s, ssid, peer, 1);
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003600 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003601 wpas_p2p_remove_pending_group_interface(wpa_s);
3602 return;
3603 }
3604
3605 ssid = wpa_config_get_network(wpa_s->conf,
3606 wpa_s->pending_invite_ssid_id);
3607 if (ssid == NULL) {
3608 wpa_printf(MSG_ERROR, "P2P: Could not find persistent group "
3609 "data matching with invitation");
3610 return;
3611 }
3612
Jouni Malinen2c5b17d2012-10-13 21:52:46 -07003613 /*
3614 * The peer could have missed our ctrl::ack frame for Invitation
3615 * Response and continue retransmitting the frame. To reduce the
3616 * likelihood of the peer not getting successful TX status for the
3617 * Invitation Response frame, wait a short time here before starting
3618 * the persistent group so that we will remain on the current channel to
3619 * acknowledge any possible retransmission from the peer.
3620 */
3621 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: 50 ms wait on current channel before "
3622 "starting persistent group");
3623 os_sleep(0, 50000);
3624
Dmitry Shmidt15907092014-03-25 10:42:57 -07003625 if (neg_freq > 0 && ssid->mode == WPAS_MODE_P2P_GO &&
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003626 freq_included(wpa_s, channels, neg_freq))
Dmitry Shmidt15907092014-03-25 10:42:57 -07003627 freq = neg_freq;
3628 else if (peer_oper_freq > 0 && ssid->mode != WPAS_MODE_P2P_GO &&
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003629 freq_included(wpa_s, channels, peer_oper_freq))
Dmitry Shmidt15907092014-03-25 10:42:57 -07003630 freq = peer_oper_freq;
3631 else
3632 freq = 0;
3633
3634 wpa_printf(MSG_DEBUG, "P2P: Persistent group invitation success - op_freq=%d MHz SSID=%s",
3635 freq, wpa_ssid_txt(ssid->ssid, ssid->ssid_len));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003636 wpas_p2p_group_add_persistent(wpa_s, ssid,
Jouni Malinen31be0a42012-08-31 21:20:51 +03003637 ssid->mode == WPAS_MODE_P2P_GO,
Dmitry Shmidt96be6222014-02-13 10:16:51 -08003638 wpa_s->p2p_persistent_go_freq,
Dmitry Shmidt15907092014-03-25 10:42:57 -07003639 freq,
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08003640 wpa_s->p2p_go_vht_center_freq2,
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003641 wpa_s->p2p_go_ht40, wpa_s->p2p_go_vht,
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08003642 wpa_s->p2p_go_max_oper_chwidth,
Hai Shalom74f70d42019-02-11 14:42:39 -08003643 wpa_s->p2p_go_he,
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08003644 wpa_s->p2p_go_edmg,
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003645 channels,
Dmitry Shmidt56052862013-10-04 10:23:25 -07003646 ssid->mode == WPAS_MODE_P2P_GO ?
3647 P2P_MAX_INITIAL_CONN_WAIT_GO_REINVOKE :
Hai Shalomc1a21442022-02-04 13:43:00 -08003648 0, 1,
Matthew Wang124e5f42022-12-29 07:23:06 +00003649 is_p2p_allow_6ghz(wpa_s->global->p2p), 0,
Sunil Ravi036cec52023-03-29 11:35:17 -07003650 NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003651}
3652
3653
Dmitry Shmidt04949592012-07-19 12:16:46 -07003654static int wpas_p2p_disallowed_freq(struct wpa_global *global,
3655 unsigned int freq)
3656{
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08003657 if (freq_range_list_includes(&global->p2p_go_avoid_freq, freq))
3658 return 1;
Dmitry Shmidt4ce9c872013-10-24 11:08:13 -07003659 return freq_range_list_includes(&global->p2p_disallow_freq, freq);
Dmitry Shmidt04949592012-07-19 12:16:46 -07003660}
3661
3662
3663static void wpas_p2p_add_chan(struct p2p_reg_class *reg, u8 chan)
3664{
3665 reg->channel[reg->channels] = chan;
3666 reg->channels++;
3667}
3668
3669
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003670static int wpas_p2p_default_channels(struct wpa_supplicant *wpa_s,
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003671 struct p2p_channels *chan,
3672 struct p2p_channels *cli_chan)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003673{
3674 int i, cla = 0;
3675
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003676 wpa_s->global->p2p_24ghz_social_channels = 1;
3677
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003678 os_memset(cli_chan, 0, sizeof(*cli_chan));
3679
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003680 wpa_printf(MSG_DEBUG, "P2P: Enable operating classes for 2.4 GHz "
3681 "band");
3682
3683 /* Operating class 81 - 2.4 GHz band channels 1..13 */
3684 chan->reg_class[cla].reg_class = 81;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003685 chan->reg_class[cla].channels = 0;
3686 for (i = 0; i < 11; i++) {
3687 if (!wpas_p2p_disallowed_freq(wpa_s->global, 2412 + i * 5))
3688 wpas_p2p_add_chan(&chan->reg_class[cla], i + 1);
3689 }
3690 if (chan->reg_class[cla].channels)
3691 cla++;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003692
3693 wpa_printf(MSG_DEBUG, "P2P: Enable operating classes for lower 5 GHz "
3694 "band");
3695
3696 /* Operating class 115 - 5 GHz, channels 36-48 */
3697 chan->reg_class[cla].reg_class = 115;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003698 chan->reg_class[cla].channels = 0;
3699 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 36 * 5))
3700 wpas_p2p_add_chan(&chan->reg_class[cla], 36);
3701 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 40 * 5))
3702 wpas_p2p_add_chan(&chan->reg_class[cla], 40);
3703 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 44 * 5))
3704 wpas_p2p_add_chan(&chan->reg_class[cla], 44);
3705 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 48 * 5))
3706 wpas_p2p_add_chan(&chan->reg_class[cla], 48);
3707 if (chan->reg_class[cla].channels)
3708 cla++;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003709
3710 wpa_printf(MSG_DEBUG, "P2P: Enable operating classes for higher 5 GHz "
3711 "band");
3712
3713 /* Operating class 124 - 5 GHz, channels 149,153,157,161 */
3714 chan->reg_class[cla].reg_class = 124;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003715 chan->reg_class[cla].channels = 0;
3716 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 149 * 5))
3717 wpas_p2p_add_chan(&chan->reg_class[cla], 149);
3718 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 153 * 5))
3719 wpas_p2p_add_chan(&chan->reg_class[cla], 153);
3720 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 156 * 5))
3721 wpas_p2p_add_chan(&chan->reg_class[cla], 157);
3722 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 161 * 5))
3723 wpas_p2p_add_chan(&chan->reg_class[cla], 161);
3724 if (chan->reg_class[cla].channels)
3725 cla++;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003726
3727 chan->reg_classes = cla;
3728 return 0;
3729}
3730
3731
Hai Shalomc1a21442022-02-04 13:43:00 -08003732static enum chan_allowed has_channel(struct wpa_global *global,
3733 struct hostapd_hw_modes *mode, u8 op_class,
3734 u8 chan, int *flags)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003735{
3736 int i;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003737 unsigned int freq;
3738
Hai Shalomc1a21442022-02-04 13:43:00 -08003739 freq = ieee80211_chan_to_freq(NULL, op_class, chan);
Dmitry Shmidt04949592012-07-19 12:16:46 -07003740 if (wpas_p2p_disallowed_freq(global, freq))
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003741 return NOT_ALLOWED;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003742
3743 for (i = 0; i < mode->num_channels; i++) {
Hai Shalomc1a21442022-02-04 13:43:00 -08003744 if ((unsigned int) mode->channels[i].freq == freq) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003745 if (flags)
3746 *flags = mode->channels[i].flag;
Hai Shalomc1a21442022-02-04 13:43:00 -08003747 if (mode->channels[i].flag & HOSTAPD_CHAN_DISABLED)
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003748 return NOT_ALLOWED;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003749 if (mode->channels[i].flag & HOSTAPD_CHAN_NO_IR)
3750 return NO_IR;
Hai Shalomc1a21442022-02-04 13:43:00 -08003751 if (mode->channels[i].flag & HOSTAPD_CHAN_RADAR)
3752 return RADAR;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003753 return ALLOWED;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003754 }
3755 }
3756
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003757 return NOT_ALLOWED;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003758}
3759
3760
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003761static int wpas_p2p_get_center_80mhz(struct wpa_supplicant *wpa_s,
3762 struct hostapd_hw_modes *mode,
Hai Shalomc1a21442022-02-04 13:43:00 -08003763 u8 channel, const u8 *center_channels,
3764 size_t num_chan)
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003765{
Dmitry Shmidtaca489e2016-09-28 15:44:14 -07003766 size_t i;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003767
3768 if (mode->mode != HOSTAPD_MODE_IEEE80211A)
3769 return 0;
3770
Hai Shalomc1a21442022-02-04 13:43:00 -08003771 for (i = 0; i < num_chan; i++)
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003772 /*
3773 * In 80 MHz, the bandwidth "spans" 12 channels (e.g., 36-48),
3774 * so the center channel is 6 channels away from the start/end.
3775 */
3776 if (channel >= center_channels[i] - 6 &&
3777 channel <= center_channels[i] + 6)
3778 return center_channels[i];
3779
3780 return 0;
3781}
3782
3783
Hai Shalomc1a21442022-02-04 13:43:00 -08003784static const u8 center_channels_5ghz_80mhz[] = { 42, 58, 106, 122, 138,
3785 155, 171 };
3786static const u8 center_channels_6ghz_80mhz[] = { 7, 23, 39, 55, 71, 87, 103,
3787 119, 135, 151, 167, 183, 199,
3788 215 };
3789
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003790static enum chan_allowed wpas_p2p_verify_80mhz(struct wpa_supplicant *wpa_s,
3791 struct hostapd_hw_modes *mode,
Hai Shalomc1a21442022-02-04 13:43:00 -08003792 u8 op_class, u8 channel, u8 bw)
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003793{
3794 u8 center_chan;
3795 int i, flags;
3796 enum chan_allowed res, ret = ALLOWED;
Hai Shalomc1a21442022-02-04 13:43:00 -08003797 const u8 *chans;
3798 size_t num_chans;
3799 bool is_6ghz = is_6ghz_op_class(op_class);
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003800
Hai Shalomc1a21442022-02-04 13:43:00 -08003801 if (is_6ghz) {
3802 chans = center_channels_6ghz_80mhz;
3803 num_chans = ARRAY_SIZE(center_channels_6ghz_80mhz);
3804 } else {
3805 chans = center_channels_5ghz_80mhz;
3806 num_chans = ARRAY_SIZE(center_channels_5ghz_80mhz);
3807 }
3808 center_chan = wpas_p2p_get_center_80mhz(wpa_s, mode, channel,
3809 chans, num_chans);
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003810 if (!center_chan)
3811 return NOT_ALLOWED;
Hai Shalomc1a21442022-02-04 13:43:00 -08003812 if (!wpa_s->p2p_go_allow_dfs &&
3813 !is_6ghz && center_chan >= 58 && center_chan <= 138)
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003814 return NOT_ALLOWED; /* Do not allow DFS channels for P2P */
3815
3816 /* check all the channels are available */
3817 for (i = 0; i < 4; i++) {
3818 int adj_chan = center_chan - 6 + i * 4;
3819
Hai Shalomc1a21442022-02-04 13:43:00 -08003820 res = has_channel(wpa_s->global, mode, op_class, adj_chan,
3821 &flags);
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003822 if (res == NOT_ALLOWED)
3823 return NOT_ALLOWED;
Hai Shalomc1a21442022-02-04 13:43:00 -08003824 if (res == RADAR)
3825 ret = RADAR;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003826 if (res == NO_IR)
3827 ret = NO_IR;
Hai Shalomc1a21442022-02-04 13:43:00 -08003828 if (!is_6ghz) {
Sunil Ravi036cec52023-03-29 11:35:17 -07003829 if (!(flags & HOSTAPD_CHAN_VHT_80MHZ_SUBCHANNEL))
Hai Shalomc1a21442022-02-04 13:43:00 -08003830 return NOT_ALLOWED;
3831 } else if (is_6ghz &&
3832 (!(wpas_get_6ghz_he_chwidth_capab(mode) &
3833 HE_PHYCAP_CHANNEL_WIDTH_SET_40MHZ_80MHZ_IN_5G))) {
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003834 return NOT_ALLOWED;
Hai Shalomc1a21442022-02-04 13:43:00 -08003835 }
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003836 }
3837
3838 return ret;
3839}
3840
3841
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08003842static int wpas_p2p_get_center_160mhz(struct wpa_supplicant *wpa_s,
3843 struct hostapd_hw_modes *mode,
Hai Shalomc1a21442022-02-04 13:43:00 -08003844 u8 channel, const u8 *center_channels,
3845 size_t num_chan)
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08003846{
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08003847 unsigned int i;
3848
3849 if (mode->mode != HOSTAPD_MODE_IEEE80211A)
3850 return 0;
3851
Hai Shalomc1a21442022-02-04 13:43:00 -08003852 for (i = 0; i < num_chan; i++)
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08003853 /*
3854 * In 160 MHz, the bandwidth "spans" 28 channels (e.g., 36-64),
3855 * so the center channel is 14 channels away from the start/end.
3856 */
3857 if (channel >= center_channels[i] - 14 &&
3858 channel <= center_channels[i] + 14)
3859 return center_channels[i];
3860
3861 return 0;
3862}
3863
3864
Hai Shalomc1a21442022-02-04 13:43:00 -08003865static const u8 center_channels_5ghz_160mhz[] = { 50, 114, 163 };
3866static const u8 center_channels_6ghz_160mhz[] = { 15, 47, 79, 111, 143, 175,
3867 207 };
3868
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08003869static enum chan_allowed wpas_p2p_verify_160mhz(struct wpa_supplicant *wpa_s,
3870 struct hostapd_hw_modes *mode,
Hai Shalomc1a21442022-02-04 13:43:00 -08003871 u8 op_class, u8 channel, u8 bw)
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08003872{
3873 u8 center_chan;
3874 int i, flags;
3875 enum chan_allowed res, ret = ALLOWED;
Hai Shalomc1a21442022-02-04 13:43:00 -08003876 const u8 *chans;
3877 size_t num_chans;
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08003878
Hai Shalomc1a21442022-02-04 13:43:00 -08003879 if (is_6ghz_op_class(op_class)) {
3880 chans = center_channels_6ghz_160mhz;
3881 num_chans = ARRAY_SIZE(center_channels_6ghz_160mhz);
3882 } else {
3883 chans = center_channels_5ghz_160mhz;
3884 num_chans = ARRAY_SIZE(center_channels_5ghz_160mhz);
3885 }
3886 center_chan = wpas_p2p_get_center_160mhz(wpa_s, mode, channel,
3887 chans, num_chans);
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08003888 if (!center_chan)
3889 return NOT_ALLOWED;
3890 /* VHT 160 MHz uses DFS channels in most countries. */
3891
3892 /* Check all the channels are available */
3893 for (i = 0; i < 8; i++) {
3894 int adj_chan = center_chan - 14 + i * 4;
3895
Hai Shalomc1a21442022-02-04 13:43:00 -08003896 res = has_channel(wpa_s->global, mode, op_class, adj_chan,
3897 &flags);
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08003898 if (res == NOT_ALLOWED)
3899 return NOT_ALLOWED;
3900
Hai Shalomc1a21442022-02-04 13:43:00 -08003901 if (res == RADAR)
3902 ret = RADAR;
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08003903 if (res == NO_IR)
3904 ret = NO_IR;
3905
Hai Shalomc1a21442022-02-04 13:43:00 -08003906 if (!is_6ghz_op_class(op_class)) {
Sunil Ravi036cec52023-03-29 11:35:17 -07003907 if (!(flags & HOSTAPD_CHAN_VHT_80MHZ_SUBCHANNEL) ||
3908 !(flags & HOSTAPD_CHAN_VHT_160MHZ_SUBCHANNEL))
Hai Shalomc1a21442022-02-04 13:43:00 -08003909 return NOT_ALLOWED;
3910 } else if (is_6ghz_op_class(op_class) &&
3911 (!(wpas_get_6ghz_he_chwidth_capab(mode) &
3912 HE_PHYCAP_CHANNEL_WIDTH_SET_160MHZ_IN_5G))) {
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08003913 return NOT_ALLOWED;
Hai Shalomc1a21442022-02-04 13:43:00 -08003914 }
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08003915 }
3916
3917 return ret;
3918}
3919
3920
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08003921static enum chan_allowed wpas_p2p_verify_edmg(struct wpa_supplicant *wpa_s,
3922 struct hostapd_hw_modes *mode,
3923 u8 channel)
3924{
3925 struct ieee80211_edmg_config edmg;
3926
3927 hostapd_encode_edmg_chan(1, channel, 0, &edmg);
3928 if (edmg.channels && ieee802_edmg_is_allowed(mode->edmg, edmg))
3929 return ALLOWED;
3930
3931 return NOT_ALLOWED;
3932}
3933
3934
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003935static enum chan_allowed wpas_p2p_verify_channel(struct wpa_supplicant *wpa_s,
3936 struct hostapd_hw_modes *mode,
Hai Shalomc1a21442022-02-04 13:43:00 -08003937 u8 op_class, u8 channel, u8 bw)
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003938{
Dmitry Shmidt96be6222014-02-13 10:16:51 -08003939 int flag = 0;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003940 enum chan_allowed res, res2;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003941
Sunil Ravi89eba102022-09-13 21:04:37 -07003942 if (is_6ghz_op_class(op_class) && !is_6ghz_psc_frequency(
3943 p2p_channel_to_freq(op_class, channel)))
3944 return NOT_ALLOWED;
3945
Hai Shalomc1a21442022-02-04 13:43:00 -08003946 res2 = res = has_channel(wpa_s->global, mode, op_class, channel, &flag);
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003947 if (bw == BW40MINUS) {
3948 if (!(flag & HOSTAPD_CHAN_HT40MINUS))
3949 return NOT_ALLOWED;
Hai Shalomc1a21442022-02-04 13:43:00 -08003950 res2 = has_channel(wpa_s->global, mode, op_class, channel - 4,
3951 NULL);
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003952 } else if (bw == BW40PLUS) {
3953 if (!(flag & HOSTAPD_CHAN_HT40PLUS))
3954 return NOT_ALLOWED;
Hai Shalomc1a21442022-02-04 13:43:00 -08003955 res2 = has_channel(wpa_s->global, mode, op_class, channel + 4,
3956 NULL);
3957 } else if (is_6ghz_op_class(op_class) && bw == BW40) {
3958 if (mode->mode != HOSTAPD_MODE_IEEE80211A)
3959 return NOT_ALLOWED;
3960 if (get_6ghz_sec_channel(channel) < 0)
3961 res2 = has_channel(wpa_s->global, mode, op_class,
3962 channel - 4, NULL);
3963 else
3964 res2 = has_channel(wpa_s->global, mode, op_class,
3965 channel + 4, NULL);
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003966 } else if (bw == BW80) {
Hai Shalomc1a21442022-02-04 13:43:00 -08003967 res2 = wpas_p2p_verify_80mhz(wpa_s, mode, op_class, channel,
3968 bw);
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08003969 } else if (bw == BW160) {
Hai Shalomc1a21442022-02-04 13:43:00 -08003970 res2 = wpas_p2p_verify_160mhz(wpa_s, mode, op_class, channel,
3971 bw);
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08003972 } else if (bw == BW4320 || bw == BW6480 || bw == BW8640) {
3973 return wpas_p2p_verify_edmg(wpa_s, mode, channel);
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003974 }
3975
3976 if (res == NOT_ALLOWED || res2 == NOT_ALLOWED)
3977 return NOT_ALLOWED;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003978 if (res == NO_IR || res2 == NO_IR)
3979 return NO_IR;
Hai Shalomc1a21442022-02-04 13:43:00 -08003980 if (res == RADAR || res2 == RADAR)
3981 return RADAR;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003982 return res;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003983}
3984
3985
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003986static int wpas_p2p_setup_channels(struct wpa_supplicant *wpa_s,
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003987 struct p2p_channels *chan,
Hai Shalom60840252021-02-19 19:02:11 -08003988 struct p2p_channels *cli_chan,
3989 bool p2p_disable_6ghz)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003990{
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003991 struct hostapd_hw_modes *mode;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003992 int cla, op, cli_cla;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003993
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003994 if (wpa_s->hw.modes == NULL) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003995 wpa_printf(MSG_DEBUG, "P2P: Driver did not support fetching "
3996 "of all supported channels; assume dualband "
3997 "support");
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003998 return wpas_p2p_default_channels(wpa_s, chan, cli_chan);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003999 }
4000
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004001 cla = cli_cla = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004002
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08004003 for (op = 0; global_op_class[op].op_class; op++) {
4004 const struct oper_class_map *o = &global_op_class[op];
Hai Shalomc1a21442022-02-04 13:43:00 -08004005 unsigned int ch;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004006 struct p2p_reg_class *reg = NULL, *cli_reg = NULL;
Shuibing Daie2fad412023-05-05 14:08:11 -07004007 bool check_dfs_supported = (is_p2p_dfs_chan_enabled(wpa_s->global->p2p)
Shuibing Dai64a8a892023-03-08 10:26:22 -08004008 && is_dfs_global_op_class(o->op_class));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004009
Shuibing Dai64a8a892023-03-08 10:26:22 -08004010 if ((!check_dfs_supported && o->p2p == NO_P2P_SUPP) ||
Hai Shalom60840252021-02-19 19:02:11 -08004011 (is_6ghz_op_class(o->op_class) && p2p_disable_6ghz))
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08004012 continue;
4013
Hai Shalomfdcde762020-04-02 11:19:20 -07004014 mode = get_mode(wpa_s->hw.modes, wpa_s->hw.num_modes, o->mode,
4015 is_6ghz_op_class(o->op_class));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004016 if (mode == NULL)
4017 continue;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004018 if (mode->mode == HOSTAPD_MODE_IEEE80211G)
4019 wpa_s->global->p2p_24ghz_social_channels = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004020 for (ch = o->min_chan; ch <= o->max_chan; ch += o->inc) {
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004021 enum chan_allowed res;
Hai Shalom899fcc72020-10-19 14:38:18 -07004022
4023 /* Check for non-continuous jump in channel index
4024 * incrementation */
Hai Shalom60840252021-02-19 19:02:11 -08004025 if ((o->op_class >= 128 && o->op_class <= 130) &&
Hai Shalom899fcc72020-10-19 14:38:18 -07004026 ch < 149 && ch + o->inc > 149)
4027 ch = 149;
4028
Hai Shalomc1a21442022-02-04 13:43:00 -08004029 res = wpas_p2p_verify_channel(wpa_s, mode, o->op_class,
4030 ch, o->bw);
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004031 if (res == ALLOWED) {
4032 if (reg == NULL) {
Hai Shalomfdcde762020-04-02 11:19:20 -07004033 if (cla == P2P_MAX_REG_CLASSES)
4034 continue;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004035 wpa_printf(MSG_DEBUG, "P2P: Add operating class %u",
4036 o->op_class);
4037 reg = &chan->reg_class[cla];
4038 cla++;
4039 reg->reg_class = o->op_class;
4040 }
Hai Shalomfdcde762020-04-02 11:19:20 -07004041 if (reg->channels == P2P_MAX_REG_CLASS_CHANNELS)
4042 continue;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004043 reg->channel[reg->channels] = ch;
4044 reg->channels++;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004045 } else if (res == NO_IR &&
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004046 wpa_s->conf->p2p_add_cli_chan) {
4047 if (cli_reg == NULL) {
Hai Shalomfdcde762020-04-02 11:19:20 -07004048 if (cli_cla == P2P_MAX_REG_CLASSES)
4049 continue;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004050 wpa_printf(MSG_DEBUG, "P2P: Add operating class %u (client only)",
4051 o->op_class);
4052 cli_reg = &cli_chan->reg_class[cli_cla];
4053 cli_cla++;
4054 cli_reg->reg_class = o->op_class;
4055 }
Hai Shalomfdcde762020-04-02 11:19:20 -07004056 if (cli_reg->channels ==
4057 P2P_MAX_REG_CLASS_CHANNELS)
4058 continue;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004059 cli_reg->channel[cli_reg->channels] = ch;
4060 cli_reg->channels++;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004061 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004062 }
4063 if (reg) {
4064 wpa_hexdump(MSG_DEBUG, "P2P: Channels",
4065 reg->channel, reg->channels);
4066 }
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004067 if (cli_reg) {
4068 wpa_hexdump(MSG_DEBUG, "P2P: Channels (client only)",
4069 cli_reg->channel, cli_reg->channels);
4070 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004071 }
4072
4073 chan->reg_classes = cla;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004074 cli_chan->reg_classes = cli_cla;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004075
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004076 return 0;
4077}
4078
4079
Hai Shalomc1a21442022-02-04 13:43:00 -08004080int wpas_p2p_get_sec_channel_offset_40mhz(struct wpa_supplicant *wpa_s,
4081 struct hostapd_hw_modes *mode,
4082 u8 channel)
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004083{
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004084 int op;
4085 enum chan_allowed ret;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004086
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08004087 for (op = 0; global_op_class[op].op_class; op++) {
4088 const struct oper_class_map *o = &global_op_class[op];
Sunil Ravi77d572f2023-01-17 23:58:31 +00004089 u16 ch = 0;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004090
Hai Shalomc1a21442022-02-04 13:43:00 -08004091 /* Allow DFS channels marked as NO_P2P_SUPP to be used with
4092 * driver offloaded DFS. */
4093 if ((o->p2p == NO_P2P_SUPP &&
4094 (!is_dfs_global_op_class(o->op_class) ||
4095 !wpa_s->p2p_go_allow_dfs)) ||
Hai Shalom899fcc72020-10-19 14:38:18 -07004096 (is_6ghz_op_class(o->op_class) &&
4097 wpa_s->conf->p2p_6ghz_disable))
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08004098 continue;
4099
Sunil Ravi77d572f2023-01-17 23:58:31 +00004100 /* IEEE Std 802.11ax-2021 26.17.2.3.2: "A 6 GHz-only AP should
4101 * set up the BSS with a primary 20 MHz channel that coincides
4102 * with a preferred scanning channel (PSC)."
4103 * 6 GHz BW40 operation class 132 in wpa_supplicant uses the
4104 * lowest 20 MHz channel for simplicity, so increase ch by 4 to
4105 * match the PSC.
4106 */
Hai Shalomc1a21442022-02-04 13:43:00 -08004107 if (is_6ghz_op_class(o->op_class) && o->bw == BW40 &&
4108 get_6ghz_sec_channel(channel) < 0)
Sunil Ravi77d572f2023-01-17 23:58:31 +00004109 ch = 4;
Hai Shalomc1a21442022-02-04 13:43:00 -08004110
Sunil Ravi77d572f2023-01-17 23:58:31 +00004111 for (ch += o->min_chan; ch <= o->max_chan; ch += o->inc) {
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004112 if (o->mode != HOSTAPD_MODE_IEEE80211A ||
Hai Shalomc1a21442022-02-04 13:43:00 -08004113 (o->bw != BW40PLUS && o->bw != BW40MINUS &&
4114 o->bw != BW40) ||
Sunil Ravi77d572f2023-01-17 23:58:31 +00004115 ch != channel)
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004116 continue;
Hai Shalomc1a21442022-02-04 13:43:00 -08004117 ret = wpas_p2p_verify_channel(wpa_s, mode, o->op_class,
4118 ch, o->bw);
4119 if (ret == ALLOWED) {
4120 if (is_6ghz_op_class(o->op_class) &&
4121 o->bw == BW40)
4122 return get_6ghz_sec_channel(channel);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004123 return (o->bw == BW40MINUS) ? -1 : 1;
Hai Shalomc1a21442022-02-04 13:43:00 -08004124 }
4125 if (ret == RADAR && wpa_s->p2p_go_allow_dfs) {
4126 /* Allow RADAR channels used for driver
4127 * offloaded DFS */
4128 return (o->bw == BW40MINUS) ? -1 : 1;
4129 }
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004130 }
4131 }
4132 return 0;
4133}
4134
4135
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004136int wpas_p2p_get_vht80_center(struct wpa_supplicant *wpa_s,
Hai Shalomc1a21442022-02-04 13:43:00 -08004137 struct hostapd_hw_modes *mode, u8 channel,
4138 u8 op_class)
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004139{
Hai Shalomc1a21442022-02-04 13:43:00 -08004140 const u8 *chans;
4141 size_t num_chans;
4142 enum chan_allowed ret;
4143
4144 ret = wpas_p2p_verify_channel(wpa_s, mode, op_class, channel, BW80);
4145 if (!(ret == ALLOWED || (ret == RADAR && wpa_s->p2p_go_allow_dfs)))
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004146 return 0;
4147
Hai Shalomc1a21442022-02-04 13:43:00 -08004148 if (is_6ghz_op_class(op_class)) {
4149 chans = center_channels_6ghz_80mhz;
4150 num_chans = ARRAY_SIZE(center_channels_6ghz_80mhz);
4151 } else {
4152 chans = center_channels_5ghz_80mhz;
4153 num_chans = ARRAY_SIZE(center_channels_5ghz_80mhz);
4154 }
4155 return wpas_p2p_get_center_80mhz(wpa_s, mode, channel,
4156 chans, num_chans);
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004157}
4158
4159
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08004160int wpas_p2p_get_vht160_center(struct wpa_supplicant *wpa_s,
Hai Shalomc1a21442022-02-04 13:43:00 -08004161 struct hostapd_hw_modes *mode, u8 channel,
4162 u8 op_class)
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08004163{
Hai Shalomc1a21442022-02-04 13:43:00 -08004164 const u8 *chans;
4165 size_t num_chans;
4166 enum chan_allowed ret;
4167
4168 ret = wpas_p2p_verify_channel(wpa_s, mode, op_class, channel, BW160);
4169 if (!(ret == ALLOWED || (ret == RADAR && wpa_s->p2p_go_allow_dfs)))
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08004170 return 0;
Hai Shalomc1a21442022-02-04 13:43:00 -08004171 if (is_6ghz_op_class(op_class)) {
4172 chans = center_channels_6ghz_160mhz;
4173 num_chans = ARRAY_SIZE(center_channels_6ghz_160mhz);
4174 } else {
4175 chans = center_channels_5ghz_160mhz;
4176 num_chans = ARRAY_SIZE(center_channels_5ghz_160mhz);
4177 }
4178 return wpas_p2p_get_center_160mhz(wpa_s, mode, channel,
4179 chans, num_chans);
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08004180}
4181
4182
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004183static int wpas_get_noa(void *ctx, const u8 *interface_addr, u8 *buf,
4184 size_t buf_len)
4185{
4186 struct wpa_supplicant *wpa_s = ctx;
4187
4188 for (wpa_s = wpa_s->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
Sunil Ravib0ac25f2024-07-12 01:42:03 +00004189 if (ether_addr_equal(wpa_s->own_addr, interface_addr))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004190 break;
4191 }
4192 if (wpa_s == NULL)
4193 return -1;
4194
4195 return wpa_drv_get_noa(wpa_s, buf, buf_len);
4196}
4197
4198
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07004199struct wpa_supplicant * wpas_get_p2p_go_iface(struct wpa_supplicant *wpa_s,
4200 const u8 *ssid, size_t ssid_len)
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08004201{
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07004202 for (wpa_s = wpa_s->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
4203 struct wpa_ssid *s = wpa_s->current_ssid;
4204 if (s == NULL)
4205 continue;
4206 if (s->mode != WPAS_MODE_P2P_GO &&
4207 s->mode != WPAS_MODE_AP &&
4208 s->mode != WPAS_MODE_P2P_GROUP_FORMATION)
4209 continue;
4210 if (s->ssid_len != ssid_len ||
Dmitry Shmidt03658832014-08-13 11:03:49 -07004211 os_memcmp(ssid, s->ssid, ssid_len) != 0)
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07004212 continue;
4213 return wpa_s;
4214 }
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08004215
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07004216 return NULL;
4217
4218}
4219
4220
4221struct wpa_supplicant * wpas_get_p2p_client_iface(struct wpa_supplicant *wpa_s,
4222 const u8 *peer_dev_addr)
4223{
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08004224 for (wpa_s = wpa_s->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
4225 struct wpa_ssid *ssid = wpa_s->current_ssid;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004226 if (ssid && (ssid->mode != WPAS_MODE_INFRA || !ssid->p2p_group))
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08004227 continue;
Sunil Ravib0ac25f2024-07-12 01:42:03 +00004228 if (ether_addr_equal(wpa_s->go_dev_addr, peer_dev_addr))
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07004229 return wpa_s;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08004230 }
4231
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07004232 return NULL;
4233}
4234
4235
4236static int wpas_go_connected(void *ctx, const u8 *dev_addr)
4237{
4238 struct wpa_supplicant *wpa_s = ctx;
4239
4240 return wpas_get_p2p_client_iface(wpa_s, dev_addr) != NULL;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08004241}
4242
4243
Dmitry Shmidt18463232014-01-24 12:29:41 -08004244static int wpas_is_concurrent_session_active(void *ctx)
4245{
4246 struct wpa_supplicant *wpa_s = ctx;
4247 struct wpa_supplicant *ifs;
4248
4249 for (ifs = wpa_s->global->ifaces; ifs; ifs = ifs->next) {
4250 if (ifs == wpa_s)
4251 continue;
4252 if (ifs->wpa_state > WPA_ASSOCIATED)
4253 return 1;
4254 }
4255 return 0;
4256}
4257
4258
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07004259static void wpas_p2p_debug_print(void *ctx, int level, const char *msg)
4260{
4261 struct wpa_supplicant *wpa_s = ctx;
4262 wpa_msg_global(wpa_s, level, "P2P: %s", msg);
4263}
4264
4265
Dmitry Shmidt9ead16e2014-10-07 13:15:23 -07004266int wpas_p2p_add_p2pdev_interface(struct wpa_supplicant *wpa_s,
4267 const char *conf_p2p_dev)
Dmitry Shmidt34af3062013-07-11 10:46:32 -07004268{
4269 struct wpa_interface iface;
4270 struct wpa_supplicant *p2pdev_wpa_s;
4271 char ifname[100];
4272 char force_name[100];
4273 int ret;
Hai Shalom60840252021-02-19 19:02:11 -08004274 const u8 *if_addr = NULL;
Dmitry Shmidt34af3062013-07-11 10:46:32 -07004275
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004276 ret = os_snprintf(ifname, sizeof(ifname), P2P_MGMT_DEVICE_PREFIX "%s",
4277 wpa_s->ifname);
4278 if (os_snprintf_error(sizeof(ifname), ret))
4279 return -1;
Hai Shalom899fcc72020-10-19 14:38:18 -07004280 /* Cut length at the maximum size. Note that we don't need to ensure
4281 * collision free names here as the created interface is not a netdev.
4282 */
4283 ifname[IFNAMSIZ - 1] = '\0';
Dmitry Shmidt34af3062013-07-11 10:46:32 -07004284 force_name[0] = '\0';
4285 wpa_s->pending_interface_type = WPA_IF_P2P_DEVICE;
Hai Shalom60840252021-02-19 19:02:11 -08004286
4287 if (wpa_s->conf->p2p_device_random_mac_addr == 2 &&
4288 !is_zero_ether_addr(wpa_s->conf->p2p_device_persistent_mac_addr))
4289 if_addr = wpa_s->conf->p2p_device_persistent_mac_addr;
4290
4291 ret = wpa_drv_if_add(wpa_s, WPA_IF_P2P_DEVICE, ifname, if_addr, NULL,
Dmitry Shmidt34af3062013-07-11 10:46:32 -07004292 force_name, wpa_s->pending_interface_addr, NULL);
4293 if (ret < 0) {
4294 wpa_printf(MSG_DEBUG, "P2P: Failed to create P2P Device interface");
4295 return ret;
4296 }
4297 os_strlcpy(wpa_s->pending_interface_name, ifname,
4298 sizeof(wpa_s->pending_interface_name));
4299
4300 os_memset(&iface, 0, sizeof(iface));
4301 iface.p2p_mgmt = 1;
4302 iface.ifname = wpa_s->pending_interface_name;
4303 iface.driver = wpa_s->driver->name;
4304 iface.driver_param = wpa_s->conf->driver_param;
Dmitry Shmidt2ac5f602014-03-07 10:08:21 -08004305
4306 /*
4307 * If a P2P Device configuration file was given, use it as the interface
4308 * configuration file (instead of using parent's configuration file.
4309 */
Dmitry Shmidt9ead16e2014-10-07 13:15:23 -07004310 if (conf_p2p_dev) {
4311 iface.confname = conf_p2p_dev;
Dmitry Shmidt2ac5f602014-03-07 10:08:21 -08004312 iface.ctrl_interface = NULL;
4313 } else {
4314 iface.confname = wpa_s->confname;
4315 iface.ctrl_interface = wpa_s->conf->ctrl_interface;
4316 }
Dmitry Shmidt2ac5f602014-03-07 10:08:21 -08004317
Dmitry Shmidt203eadb2015-03-05 14:16:04 -08004318 p2pdev_wpa_s = wpa_supplicant_add_iface(wpa_s->global, &iface, wpa_s);
Dmitry Shmidt34af3062013-07-11 10:46:32 -07004319 if (!p2pdev_wpa_s) {
4320 wpa_printf(MSG_DEBUG, "P2P: Failed to add P2P Device interface");
4321 return -1;
4322 }
Dmitry Shmidt34af3062013-07-11 10:46:32 -07004323
Dmitry Shmidt9c175262016-03-03 10:20:07 -08004324 p2pdev_wpa_s->p2pdev = p2pdev_wpa_s;
Dmitry Shmidt34af3062013-07-11 10:46:32 -07004325 wpa_s->pending_interface_name[0] = '\0';
4326 return 0;
4327}
4328
4329
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08004330static void wpas_presence_resp(void *ctx, const u8 *src, u8 status,
4331 const u8 *noa, size_t noa_len)
4332{
4333 struct wpa_supplicant *wpa_s, *intf = ctx;
4334 char hex[100];
4335
4336 for (wpa_s = intf->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
4337 if (wpa_s->waiting_presence_resp)
4338 break;
4339 }
4340 if (!wpa_s) {
4341 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: No group interface was waiting for presence response");
4342 return;
4343 }
4344 wpa_s->waiting_presence_resp = 0;
4345
4346 wpa_snprintf_hex(hex, sizeof(hex), noa, noa_len);
4347 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_PRESENCE_RESPONSE "src=" MACSTR
4348 " status=%u noa=%s", MAC2STR(src), status, hex);
4349}
4350
4351
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004352static int wpas_get_persistent_group(void *ctx, const u8 *addr, const u8 *ssid,
4353 size_t ssid_len, u8 *go_dev_addr,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004354 u8 *ret_ssid, size_t *ret_ssid_len,
4355 u8 *intended_iface_addr)
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004356{
4357 struct wpa_supplicant *wpa_s = ctx;
4358 struct wpa_ssid *s;
4359
4360 s = wpas_p2p_get_persistent(wpa_s, addr, ssid, ssid_len);
4361 if (s) {
4362 os_memcpy(ret_ssid, s->ssid, s->ssid_len);
4363 *ret_ssid_len = s->ssid_len;
4364 os_memcpy(go_dev_addr, s->bssid, ETH_ALEN);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004365
4366 if (s->mode != WPAS_MODE_P2P_GO) {
4367 os_memset(intended_iface_addr, 0, ETH_ALEN);
4368 } else if (wpas_p2p_create_iface(wpa_s)) {
4369 if (wpas_p2p_add_group_interface(wpa_s, WPA_IF_P2P_GO))
4370 return 0;
4371
4372 os_memcpy(intended_iface_addr,
4373 wpa_s->pending_interface_addr, ETH_ALEN);
4374 } else {
4375 os_memcpy(intended_iface_addr, wpa_s->own_addr,
4376 ETH_ALEN);
4377 }
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004378 return 1;
4379 }
4380
4381 return 0;
4382}
4383
4384
4385static int wpas_get_go_info(void *ctx, u8 *intended_addr,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004386 u8 *ssid, size_t *ssid_len, int *group_iface,
4387 unsigned int *freq)
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004388{
4389 struct wpa_supplicant *wpa_s = ctx;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004390 struct wpa_supplicant *go;
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004391 struct wpa_ssid *s;
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004392
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004393 /*
4394 * group_iface will be set to 1 only if a dedicated interface for P2P
4395 * role is required. First, we try to reuse an active GO. However,
4396 * if it is not present, we will try to reactivate an existing
4397 * persistent group and set group_iface to 1, so the caller will know
4398 * that the pending interface should be used.
4399 */
4400 *group_iface = 0;
4401
4402 if (freq)
4403 *freq = 0;
4404
4405 go = wpas_p2p_get_go_group(wpa_s);
4406 if (!go) {
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004407 s = wpas_p2p_get_persistent_go(wpa_s);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004408 *group_iface = wpas_p2p_create_iface(wpa_s);
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004409 if (s)
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004410 os_memcpy(intended_addr, s->bssid, ETH_ALEN);
4411 else
4412 return 0;
4413 } else {
4414 s = go->current_ssid;
4415 os_memcpy(intended_addr, go->own_addr, ETH_ALEN);
4416 if (freq)
4417 *freq = go->assoc_freq;
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004418 }
4419
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004420 os_memcpy(ssid, s->ssid, s->ssid_len);
4421 *ssid_len = s->ssid_len;
4422
4423 return 1;
4424}
4425
4426
4427static int wpas_remove_stale_groups(void *ctx, const u8 *peer, const u8 *go,
4428 const u8 *ssid, size_t ssid_len)
4429{
4430 struct wpa_supplicant *wpa_s = ctx;
4431 struct wpa_ssid *s;
4432 int save_config = 0;
4433 size_t i;
4434
4435 /* Start with our first choice of Persistent Groups */
4436 while ((s = wpas_p2p_get_persistent(wpa_s, peer, NULL, 0))) {
4437 if (go && ssid && ssid_len &&
4438 s->ssid_len == ssid_len &&
Sunil Ravib0ac25f2024-07-12 01:42:03 +00004439 ether_addr_equal(go, s->bssid) &&
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004440 os_memcmp(ssid, s->ssid, ssid_len) == 0)
4441 break;
4442
4443 /* Remove stale persistent group */
4444 if (s->mode != WPAS_MODE_P2P_GO || s->num_p2p_clients <= 1) {
Hai Shalom74f70d42019-02-11 14:42:39 -08004445 wpa_dbg(wpa_s, MSG_DEBUG,
4446 "P2P: Remove stale persistent group id=%d",
4447 s->id);
4448 wpas_notify_persistent_group_removed(wpa_s, s);
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004449 wpa_config_remove_network(wpa_s->conf, s->id);
4450 save_config = 1;
4451 continue;
4452 }
4453
4454 for (i = 0; i < s->num_p2p_clients; i++) {
Sunil Ravib0ac25f2024-07-12 01:42:03 +00004455 if (!ether_addr_equal(s->p2p_client_list +
4456 i * 2 * ETH_ALEN, peer))
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004457 continue;
4458
4459 os_memmove(s->p2p_client_list + i * 2 * ETH_ALEN,
4460 s->p2p_client_list + (i + 1) * 2 * ETH_ALEN,
4461 (s->num_p2p_clients - i - 1) * 2 * ETH_ALEN);
4462 break;
4463 }
4464 s->num_p2p_clients--;
4465 save_config = 1;
4466 }
4467
4468 if (save_config)
4469 p2p_config_write(wpa_s);
4470
4471 /* Return TRUE if valid SSID remains */
4472 return s != NULL;
4473}
4474
4475
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004476static void wpas_p2ps_get_feat_cap_str(char *buf, size_t buf_len,
4477 const u8 *feat_cap, size_t feat_cap_len)
4478{
4479 static const char pref[] = " feature_cap=";
4480 int ret;
4481
4482 buf[0] = '\0';
4483
4484 /*
4485 * We expect a feature capability to contain at least one byte to be
4486 * reported. The string buffer provided by the caller function is
4487 * expected to be big enough to contain all bytes of the attribute for
4488 * known specifications. This function truncates the reported bytes if
4489 * the feature capability data exceeds the string buffer size.
4490 */
4491 if (!feat_cap || !feat_cap_len || buf_len < sizeof(pref) + 2)
4492 return;
4493
4494 os_memcpy(buf, pref, sizeof(pref));
4495 ret = wpa_snprintf_hex(&buf[sizeof(pref) - 1],
4496 buf_len - sizeof(pref) + 1,
4497 feat_cap, feat_cap_len);
4498
4499 if (ret != (2 * (int) feat_cap_len))
4500 wpa_printf(MSG_WARNING, "P2PS feature_cap bytes truncated");
4501}
4502
4503
Sunil Ravic0f5d412024-09-11 22:12:49 +00004504static void wpas_p2ps_prov_complete(void *ctx, enum p2p_status_code status,
4505 const u8 *dev,
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004506 const u8 *adv_mac, const u8 *ses_mac,
4507 const u8 *grp_mac, u32 adv_id, u32 ses_id,
4508 u8 conncap, int passwd_id,
4509 const u8 *persist_ssid,
4510 size_t persist_ssid_size, int response_done,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004511 int prov_start, const char *session_info,
4512 const u8 *feat_cap, size_t feat_cap_len,
Dmitry Shmidtde47be72016-01-07 12:52:55 -08004513 unsigned int freq,
4514 const u8 *group_ssid, size_t group_ssid_len)
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004515{
4516 struct wpa_supplicant *wpa_s = ctx;
4517 u8 mac[ETH_ALEN];
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004518 struct wpa_ssid *persistent_go, *stale, *s = NULL;
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004519 int save_config = 0;
4520 struct wpa_supplicant *go_wpa_s;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004521 char feat_cap_str[256];
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004522
4523 if (!dev)
4524 return;
4525
4526 os_memset(mac, 0, ETH_ALEN);
4527 if (!adv_mac)
4528 adv_mac = mac;
4529 if (!ses_mac)
4530 ses_mac = mac;
4531 if (!grp_mac)
4532 grp_mac = mac;
4533
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004534 wpas_p2ps_get_feat_cap_str(feat_cap_str, sizeof(feat_cap_str),
4535 feat_cap, feat_cap_len);
4536
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004537 if (prov_start) {
4538 if (session_info == NULL) {
4539 wpa_msg_global(wpa_s, MSG_INFO,
4540 P2P_EVENT_P2PS_PROVISION_START MACSTR
4541 " adv_id=%x conncap=%x"
4542 " adv_mac=" MACSTR
4543 " session=%x mac=" MACSTR
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004544 " dev_passwd_id=%d%s",
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004545 MAC2STR(dev), adv_id, conncap,
4546 MAC2STR(adv_mac),
4547 ses_id, MAC2STR(ses_mac),
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004548 passwd_id, feat_cap_str);
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004549 } else {
4550 wpa_msg_global(wpa_s, MSG_INFO,
4551 P2P_EVENT_P2PS_PROVISION_START MACSTR
4552 " adv_id=%x conncap=%x"
4553 " adv_mac=" MACSTR
4554 " session=%x mac=" MACSTR
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004555 " dev_passwd_id=%d info='%s'%s",
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004556 MAC2STR(dev), adv_id, conncap,
4557 MAC2STR(adv_mac),
4558 ses_id, MAC2STR(ses_mac),
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004559 passwd_id, session_info, feat_cap_str);
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004560 }
4561 return;
4562 }
4563
4564 go_wpa_s = wpas_p2p_get_go_group(wpa_s);
4565 persistent_go = wpas_p2p_get_persistent_go(wpa_s);
4566
4567 if (status && status != P2P_SC_SUCCESS_DEFERRED) {
4568 if (go_wpa_s && !p2p_group_go_member_count(wpa_s))
4569 wpas_p2p_group_remove(wpa_s, go_wpa_s->ifname);
4570
4571 if (persistent_go && !persistent_go->num_p2p_clients) {
4572 /* remove empty persistent GO */
Hai Shalom74f70d42019-02-11 14:42:39 -08004573 wpa_dbg(wpa_s, MSG_DEBUG,
4574 "P2P: Remove empty persistent group id=%d",
4575 persistent_go->id);
4576 wpas_notify_persistent_group_removed(wpa_s,
4577 persistent_go);
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004578 wpa_config_remove_network(wpa_s->conf,
4579 persistent_go->id);
4580 }
4581
4582 wpa_msg_global(wpa_s, MSG_INFO,
4583 P2P_EVENT_P2PS_PROVISION_DONE MACSTR
4584 " status=%d"
4585 " adv_id=%x adv_mac=" MACSTR
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004586 " session=%x mac=" MACSTR "%s",
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004587 MAC2STR(dev), status,
4588 adv_id, MAC2STR(adv_mac),
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004589 ses_id, MAC2STR(ses_mac), feat_cap_str);
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004590 return;
4591 }
4592
4593 /* Clean up stale persistent groups with this device */
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004594 if (persist_ssid && persist_ssid_size)
4595 s = wpas_p2p_get_persistent(wpa_s, dev, persist_ssid,
4596 persist_ssid_size);
4597
4598 if (persist_ssid && s && s->mode != WPAS_MODE_P2P_GO &&
4599 is_zero_ether_addr(grp_mac)) {
4600 wpa_dbg(wpa_s, MSG_ERROR,
4601 "P2P: Peer device is a GO in a persistent group, but it did not provide the intended MAC address");
4602 return;
4603 }
4604
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004605 for (;;) {
4606 stale = wpas_p2p_get_persistent(wpa_s, dev, NULL, 0);
4607 if (!stale)
4608 break;
4609
4610 if (s && s->ssid_len == stale->ssid_len &&
Sunil Ravib0ac25f2024-07-12 01:42:03 +00004611 ether_addr_equal(stale->bssid, s->bssid) &&
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004612 os_memcmp(stale->ssid, s->ssid, s->ssid_len) == 0)
4613 break;
4614
4615 /* Remove stale persistent group */
4616 if (stale->mode != WPAS_MODE_P2P_GO ||
4617 stale->num_p2p_clients <= 1) {
Hai Shalom74f70d42019-02-11 14:42:39 -08004618 wpa_dbg(wpa_s, MSG_DEBUG,
4619 "P2P: Remove stale persistent group id=%d",
4620 stale->id);
4621 wpas_notify_persistent_group_removed(wpa_s, stale);
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004622 wpa_config_remove_network(wpa_s->conf, stale->id);
4623 } else {
4624 size_t i;
4625
4626 for (i = 0; i < stale->num_p2p_clients; i++) {
Sunil Ravib0ac25f2024-07-12 01:42:03 +00004627 if (ether_addr_equal(stale->p2p_client_list +
4628 i * ETH_ALEN, dev)) {
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004629 os_memmove(stale->p2p_client_list +
4630 i * ETH_ALEN,
4631 stale->p2p_client_list +
4632 (i + 1) * ETH_ALEN,
4633 (stale->num_p2p_clients -
4634 i - 1) * ETH_ALEN);
4635 break;
4636 }
4637 }
4638 stale->num_p2p_clients--;
4639 }
4640 save_config = 1;
4641 }
4642
4643 if (save_config)
4644 p2p_config_write(wpa_s);
4645
4646 if (s) {
4647 if (go_wpa_s && !p2p_group_go_member_count(wpa_s))
4648 wpas_p2p_group_remove(wpa_s, go_wpa_s->ifname);
4649
4650 if (persistent_go && s != persistent_go &&
4651 !persistent_go->num_p2p_clients) {
4652 /* remove empty persistent GO */
Hai Shalom74f70d42019-02-11 14:42:39 -08004653 wpa_dbg(wpa_s, MSG_DEBUG,
4654 "P2P: Remove empty persistent group id=%d",
4655 persistent_go->id);
4656 wpas_notify_persistent_group_removed(wpa_s,
4657 persistent_go);
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004658 wpa_config_remove_network(wpa_s->conf,
4659 persistent_go->id);
4660 /* Save config */
4661 }
4662
4663 wpa_msg_global(wpa_s, MSG_INFO,
4664 P2P_EVENT_P2PS_PROVISION_DONE MACSTR
4665 " status=%d"
4666 " adv_id=%x adv_mac=" MACSTR
4667 " session=%x mac=" MACSTR
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004668 " persist=%d%s",
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004669 MAC2STR(dev), status,
4670 adv_id, MAC2STR(adv_mac),
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004671 ses_id, MAC2STR(ses_mac), s->id, feat_cap_str);
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004672 return;
4673 }
4674
Hai Shalom5f92bc92019-04-18 11:54:11 -07004675 wpa_s->global->pending_p2ps_group = 0;
4676 wpa_s->global->pending_p2ps_group_freq = 0;
4677
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004678 if (conncap == P2PS_SETUP_GROUP_OWNER) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004679 /*
4680 * We need to copy the interface name. Simply saving a
4681 * pointer isn't enough, since if we use pending_interface_name
4682 * it will be overwritten when the group is added.
4683 */
4684 char go_ifname[100];
4685
4686 go_ifname[0] = '\0';
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004687 if (!go_wpa_s) {
Hai Shalom5f92bc92019-04-18 11:54:11 -07004688 if (!response_done) {
4689 wpa_s->global->pending_p2ps_group = 1;
4690 wpa_s->global->pending_p2ps_group_freq = freq;
4691 }
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004692
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004693 if (!wpas_p2p_create_iface(wpa_s))
4694 os_memcpy(go_ifname, wpa_s->ifname,
4695 sizeof(go_ifname));
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004696 else if (wpa_s->pending_interface_name[0])
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004697 os_memcpy(go_ifname,
4698 wpa_s->pending_interface_name,
4699 sizeof(go_ifname));
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004700
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004701 if (!go_ifname[0]) {
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004702 wpas_p2ps_prov_complete(
4703 wpa_s, P2P_SC_FAIL_UNKNOWN_GROUP,
4704 dev, adv_mac, ses_mac,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004705 grp_mac, adv_id, ses_id, 0, 0,
Dmitry Shmidtde47be72016-01-07 12:52:55 -08004706 NULL, 0, 0, 0, NULL, NULL, 0, 0,
4707 NULL, 0);
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004708 return;
4709 }
4710
4711 /* If PD Resp complete, start up the GO */
4712 if (response_done && persistent_go) {
4713 wpas_p2p_group_add_persistent(
4714 wpa_s, persistent_go,
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08004715 0, 0, freq, 0, 0, 0, 0, 0, 0, NULL,
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004716 persistent_go->mode ==
4717 WPAS_MODE_P2P_GO ?
4718 P2P_MAX_INITIAL_CONN_WAIT_GO_REINVOKE :
Sunil Ravi036cec52023-03-29 11:35:17 -07004719 0, 0, false, 0, NULL);
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004720 } else if (response_done) {
Hai Shalom74f70d42019-02-11 14:42:39 -08004721 wpas_p2p_group_add(wpa_s, 1, freq,
Hai Shalomc1a21442022-02-04 13:43:00 -08004722 0, 0, 0, 0, 0, 0, false);
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004723 }
4724
4725 if (passwd_id == DEV_PW_P2PS_DEFAULT) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004726 os_memcpy(wpa_s->p2ps_join_addr, grp_mac,
4727 ETH_ALEN);
4728 wpa_s->p2ps_method_config_any = 1;
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004729 }
4730 } else if (passwd_id == DEV_PW_P2PS_DEFAULT) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004731 os_memcpy(go_ifname, go_wpa_s->ifname,
4732 sizeof(go_ifname));
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004733
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004734 if (is_zero_ether_addr(grp_mac)) {
4735 wpa_dbg(go_wpa_s, MSG_DEBUG,
4736 "P2P: Setting PIN-1 for ANY");
4737 wpa_supplicant_ap_wps_pin(go_wpa_s, NULL,
4738 "12345670", NULL, 0,
4739 0);
4740 } else {
4741 wpa_dbg(go_wpa_s, MSG_DEBUG,
4742 "P2P: Setting PIN-1 for " MACSTR,
4743 MAC2STR(grp_mac));
4744 wpa_supplicant_ap_wps_pin(go_wpa_s, grp_mac,
4745 "12345670", NULL, 0,
4746 0);
4747 }
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004748
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004749 os_memcpy(wpa_s->p2ps_join_addr, grp_mac, ETH_ALEN);
4750 wpa_s->p2ps_method_config_any = 1;
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004751 }
4752
4753 wpa_msg_global(wpa_s, MSG_INFO,
4754 P2P_EVENT_P2PS_PROVISION_DONE MACSTR
4755 " status=%d conncap=%x"
4756 " adv_id=%x adv_mac=" MACSTR
4757 " session=%x mac=" MACSTR
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004758 " dev_passwd_id=%d go=%s%s",
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004759 MAC2STR(dev), status, conncap,
4760 adv_id, MAC2STR(adv_mac),
4761 ses_id, MAC2STR(ses_mac),
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004762 passwd_id, go_ifname, feat_cap_str);
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004763 return;
4764 }
4765
4766 if (go_wpa_s && !p2p_group_go_member_count(wpa_s))
4767 wpas_p2p_group_remove(wpa_s, go_wpa_s->ifname);
4768
4769 if (persistent_go && !persistent_go->num_p2p_clients) {
4770 /* remove empty persistent GO */
Hai Shalom74f70d42019-02-11 14:42:39 -08004771 wpa_dbg(wpa_s, MSG_DEBUG,
4772 "P2P: Remove empty persistent group id=%d",
4773 persistent_go->id);
4774 wpas_notify_persistent_group_removed(wpa_s, persistent_go);
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004775 wpa_config_remove_network(wpa_s->conf, persistent_go->id);
4776 }
4777
4778 if (conncap == P2PS_SETUP_CLIENT) {
Dmitry Shmidtde47be72016-01-07 12:52:55 -08004779 char ssid_hex[32 * 2 + 1];
4780
4781 if (group_ssid)
4782 wpa_snprintf_hex(ssid_hex, sizeof(ssid_hex),
4783 group_ssid, group_ssid_len);
4784 else
4785 ssid_hex[0] = '\0';
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004786 wpa_msg_global(wpa_s, MSG_INFO,
4787 P2P_EVENT_P2PS_PROVISION_DONE MACSTR
4788 " status=%d conncap=%x"
4789 " adv_id=%x adv_mac=" MACSTR
4790 " session=%x mac=" MACSTR
Dmitry Shmidtde47be72016-01-07 12:52:55 -08004791 " dev_passwd_id=%d join=" MACSTR "%s%s%s",
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004792 MAC2STR(dev), status, conncap,
4793 adv_id, MAC2STR(adv_mac),
4794 ses_id, MAC2STR(ses_mac),
Dmitry Shmidtde47be72016-01-07 12:52:55 -08004795 passwd_id, MAC2STR(grp_mac), feat_cap_str,
4796 group_ssid ? " group_ssid=" : "", ssid_hex);
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004797 } else {
4798 wpa_msg_global(wpa_s, MSG_INFO,
4799 P2P_EVENT_P2PS_PROVISION_DONE MACSTR
4800 " status=%d conncap=%x"
4801 " adv_id=%x adv_mac=" MACSTR
4802 " session=%x mac=" MACSTR
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004803 " dev_passwd_id=%d%s",
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004804 MAC2STR(dev), status, conncap,
4805 adv_id, MAC2STR(adv_mac),
4806 ses_id, MAC2STR(ses_mac),
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004807 passwd_id, feat_cap_str);
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004808 }
4809}
4810
4811
Dmitry Shmidt5a1480c2014-05-12 09:46:02 -07004812static int _wpas_p2p_in_progress(void *ctx)
4813{
4814 struct wpa_supplicant *wpa_s = ctx;
4815 return wpas_p2p_in_progress(wpa_s);
4816}
4817
4818
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004819static int wpas_prov_disc_resp_cb(void *ctx)
4820{
4821 struct wpa_supplicant *wpa_s = ctx;
4822 struct wpa_ssid *persistent_go;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004823 unsigned int freq;
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004824
4825 if (!wpa_s->global->pending_p2ps_group)
4826 return 0;
4827
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004828 freq = wpa_s->global->pending_p2ps_group_freq;
4829 wpa_s->global->pending_p2ps_group_freq = 0;
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004830 wpa_s->global->pending_p2ps_group = 0;
4831
4832 if (wpas_p2p_get_go_group(wpa_s))
4833 return 0;
4834 persistent_go = wpas_p2p_get_persistent_go(wpa_s);
4835
4836 if (persistent_go) {
4837 wpas_p2p_group_add_persistent(
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08004838 wpa_s, persistent_go, 0, 0, 0, 0, 0, 0, 0, 0, 0,
4839 NULL,
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004840 persistent_go->mode == WPAS_MODE_P2P_GO ?
Hai Shalomc1a21442022-02-04 13:43:00 -08004841 P2P_MAX_INITIAL_CONN_WAIT_GO_REINVOKE : 0, 0,
Sunil Ravi036cec52023-03-29 11:35:17 -07004842 is_p2p_allow_6ghz(wpa_s->global->p2p), 0, NULL);
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004843 } else {
Hai Shalomc1a21442022-02-04 13:43:00 -08004844 wpas_p2p_group_add(wpa_s, 1, freq, 0, 0, 0, 0, 0, 0,
4845 is_p2p_allow_6ghz(wpa_s->global->p2p));
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004846 }
4847
4848 return 1;
4849}
4850
4851
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004852static int wpas_p2p_get_pref_freq_list(void *ctx, int go,
4853 unsigned int *len,
Sunil8cd6f4d2022-06-28 18:40:46 +00004854 struct weighted_pcl *freq_list)
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004855{
4856 struct wpa_supplicant *wpa_s = ctx;
4857
4858 return wpa_drv_get_pref_freq_list(wpa_s, go ? WPA_IF_P2P_GO :
4859 WPA_IF_P2P_CLIENT, len, freq_list);
4860}
4861
Sunil Ravic0f5d412024-09-11 22:12:49 +00004862
4863static void wpas_p2p_send_bootstrap_comeback(void *eloop_ctx, void *timeout_ctx)
4864{
4865 struct wpa_supplicant *wpa_s = eloop_ctx;
4866
4867 wpa_printf(MSG_DEBUG, "P2P2: Send bootstrapping comeback PD Request");
4868 wpas_p2p_connect(wpa_s, wpa_s->p2p_bootstrap_dev_addr, wpa_s->p2p_pin,
4869 wpa_s->p2p_wps_method, wpa_s->p2p_persistent_group, 0,
4870 0, 0, wpa_s->p2p_go_intent, wpa_s->p2p_connect_freq,
4871 wpa_s->p2p_go_vht_center_freq2,
4872 wpa_s->p2p_persistent_id,
4873 wpa_s->p2p_pd_before_go_neg,
4874 wpa_s->p2p_go_ht40,
4875 wpa_s->p2p_go_vht,
4876 wpa_s->p2p_go_max_oper_chwidth,
4877 wpa_s->p2p_go_he,
4878 wpa_s->p2p_go_edmg,
4879 NULL, 0, is_p2p_allow_6ghz(wpa_s->global->p2p),
4880 wpa_s->p2p2, wpa_s->p2p_bootstrap, NULL);
4881}
4882
4883
4884static void wpas_p2p_register_bootstrap_comeback(void *ctx, const u8 *addr,
4885 u16 comeback_after)
4886{
4887 unsigned int timeout_us;
4888 struct wpa_supplicant *wpa_s = ctx;
4889
4890 timeout_us = comeback_after * 1024;
4891 os_memcpy(wpa_s->p2p_bootstrap_dev_addr, addr, ETH_ALEN);
4892
4893 eloop_cancel_timeout(wpas_p2p_send_bootstrap_comeback, wpa_s, NULL);
4894 eloop_register_timeout(0, timeout_us, wpas_p2p_send_bootstrap_comeback,
4895 wpa_s, NULL);
4896}
4897
4898
4899static void wpas_bootstrap_req_rx(void *ctx, const u8 *addr,
4900 u16 bootstrap_method)
4901{
4902 struct wpa_supplicant *wpa_s = ctx;
4903
4904 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_BOOTSTRAP_REQUEST MACSTR
4905 " bootstrap_method=%u", MAC2STR(addr), bootstrap_method);
4906}
4907
4908
4909static void wpas_bootstrap_completed(void *ctx, const u8 *addr,
4910 enum p2p_status_code status, int freq)
4911{
4912 struct wpa_supplicant *wpa_s = ctx;
4913
4914 if (status) {
4915 wpa_msg_global(wpa_s, MSG_INFO,
4916 P2P_EVENT_BOOTSTRAP_FAILURE MACSTR " status=%d",
4917 MAC2STR(addr), status);
4918 } else {
4919 wpa_msg_global(wpa_s, MSG_INFO,
4920 P2P_EVENT_BOOTSTRAP_SUCCESS MACSTR " status=%d",
4921 MAC2STR(addr), status);
4922 }
4923}
4924
4925
Jimmy Chenf887c7b2018-11-13 15:19:57 +08004926int wpas_p2p_mac_setup(struct wpa_supplicant *wpa_s)
4927{
JaeMan Park9de97322022-07-11 15:36:43 +09004928 int ret = 0;
Jimmy Chenf887c7b2018-11-13 15:19:57 +08004929 u8 addr[ETH_ALEN] = {0};
4930
4931 if (wpa_s->conf->p2p_device_random_mac_addr == 0)
4932 return 0;
4933
Hai Shalom60840252021-02-19 19:02:11 -08004934 if (wpa_s->conf->p2p_device_random_mac_addr == 2) {
4935 if (is_zero_ether_addr(
4936 wpa_s->conf->p2p_device_persistent_mac_addr) &&
4937 !is_zero_ether_addr(wpa_s->own_addr)) {
4938 os_memcpy(wpa_s->conf->p2p_device_persistent_mac_addr,
4939 wpa_s->own_addr, ETH_ALEN);
4940 }
4941 return 0;
4942 }
4943
4944 if (!wpa_s->conf->ssid) {
Jimmy Chenf887c7b2018-11-13 15:19:57 +08004945 if (random_mac_addr(addr) < 0) {
4946 wpa_msg(wpa_s, MSG_INFO,
4947 "Failed to generate random MAC address");
4948 return -EINVAL;
4949 }
4950
Hai Shalom39ba6fc2019-01-22 12:40:38 -08004951 /* Store generated MAC address. */
4952 os_memcpy(wpa_s->conf->p2p_device_persistent_mac_addr, addr,
4953 ETH_ALEN);
Jimmy Chenf887c7b2018-11-13 15:19:57 +08004954 } else {
Hai Shalom39ba6fc2019-01-22 12:40:38 -08004955 /* If there are existing saved groups, restore last MAC address.
4956 * if there is no last used MAC address, the last one is
4957 * factory MAC. */
4958 if (is_zero_ether_addr(
4959 wpa_s->conf->p2p_device_persistent_mac_addr))
Jimmy Chenf887c7b2018-11-13 15:19:57 +08004960 return 0;
Hai Shalom39ba6fc2019-01-22 12:40:38 -08004961 os_memcpy(addr, wpa_s->conf->p2p_device_persistent_mac_addr,
4962 ETH_ALEN);
Jimmy Chenf887c7b2018-11-13 15:19:57 +08004963 wpa_msg(wpa_s, MSG_DEBUG, "Restore last used MAC address.");
4964 }
4965
JaeMan Park9de97322022-07-11 15:36:43 +09004966 ret = wpa_drv_set_mac_addr(wpa_s, addr);
4967
4968 if (ret < 0) {
Jimmy Chenf887c7b2018-11-13 15:19:57 +08004969 wpa_msg(wpa_s, MSG_INFO,
4970 "Failed to set random MAC address");
JaeMan Park9de97322022-07-11 15:36:43 +09004971 return ret;
Jimmy Chenf887c7b2018-11-13 15:19:57 +08004972 }
4973
JaeMan Park9de97322022-07-11 15:36:43 +09004974 ret = wpa_supplicant_update_mac_addr(wpa_s);
4975
4976 if (ret < 0) {
Jimmy Chenf887c7b2018-11-13 15:19:57 +08004977 wpa_msg(wpa_s, MSG_INFO,
4978 "Could not update MAC address information");
JaeMan Park9de97322022-07-11 15:36:43 +09004979 return ret;
Jimmy Chenf887c7b2018-11-13 15:19:57 +08004980 }
4981
4982 wpa_msg(wpa_s, MSG_DEBUG, "Using random MAC address " MACSTR,
4983 MAC2STR(addr));
4984
4985 return 0;
4986}
4987
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004988/**
4989 * wpas_p2p_init - Initialize P2P module for %wpa_supplicant
4990 * @global: Pointer to global data from wpa_supplicant_init()
4991 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
4992 * Returns: 0 on success, -1 on failure
4993 */
4994int wpas_p2p_init(struct wpa_global *global, struct wpa_supplicant *wpa_s)
4995{
4996 struct p2p_config p2p;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004997 int i;
4998
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07004999 if (wpa_s->conf->p2p_disabled)
5000 return 0;
5001
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005002 if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_CAPABLE))
5003 return 0;
5004
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005005 if (global->p2p)
5006 return 0;
5007
Jimmy Chenf887c7b2018-11-13 15:19:57 +08005008 if (wpas_p2p_mac_setup(wpa_s) < 0) {
5009 wpa_msg(wpa_s, MSG_ERROR,
5010 "Failed to initialize P2P random MAC address.");
5011 return -1;
5012 }
5013
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005014 os_memset(&p2p, 0, sizeof(p2p));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005015 p2p.cb_ctx = wpa_s;
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07005016 p2p.debug_print = wpas_p2p_debug_print;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005017 p2p.p2p_scan = wpas_p2p_scan;
5018 p2p.send_action = wpas_send_action;
5019 p2p.send_action_done = wpas_send_action_done;
5020 p2p.go_neg_completed = wpas_go_neg_completed;
5021 p2p.go_neg_req_rx = wpas_go_neg_req_rx;
5022 p2p.dev_found = wpas_dev_found;
5023 p2p.dev_lost = wpas_dev_lost;
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07005024 p2p.find_stopped = wpas_find_stopped;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005025 p2p.start_listen = wpas_start_listen;
5026 p2p.stop_listen = wpas_stop_listen;
5027 p2p.send_probe_resp = wpas_send_probe_resp;
5028 p2p.sd_request = wpas_sd_request;
5029 p2p.sd_response = wpas_sd_response;
5030 p2p.prov_disc_req = wpas_prov_disc_req;
5031 p2p.prov_disc_resp = wpas_prov_disc_resp;
Jouni Malinen75ecf522011-06-27 15:19:46 -07005032 p2p.prov_disc_fail = wpas_prov_disc_fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005033 p2p.invitation_process = wpas_invitation_process;
5034 p2p.invitation_received = wpas_invitation_received;
5035 p2p.invitation_result = wpas_invitation_result;
5036 p2p.get_noa = wpas_get_noa;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08005037 p2p.go_connected = wpas_go_connected;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005038 p2p.presence_resp = wpas_presence_resp;
Dmitry Shmidt18463232014-01-24 12:29:41 -08005039 p2p.is_concurrent_session_active = wpas_is_concurrent_session_active;
Dmitry Shmidt5a1480c2014-05-12 09:46:02 -07005040 p2p.is_p2p_in_progress = _wpas_p2p_in_progress;
Dmitry Shmidt216983b2015-02-06 10:50:36 -08005041 p2p.get_persistent_group = wpas_get_persistent_group;
5042 p2p.get_go_info = wpas_get_go_info;
5043 p2p.remove_stale_groups = wpas_remove_stale_groups;
5044 p2p.p2ps_prov_complete = wpas_p2ps_prov_complete;
5045 p2p.prov_disc_resp_cb = wpas_prov_disc_resp_cb;
5046 p2p.p2ps_group_capability = p2ps_group_capability;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005047 p2p.get_pref_freq_list = wpas_p2p_get_pref_freq_list;
Hai Shalom60840252021-02-19 19:02:11 -08005048 p2p.p2p_6ghz_disable = wpa_s->conf->p2p_6ghz_disable;
Shuibing Daie2fad412023-05-05 14:08:11 -07005049 p2p.p2p_dfs_chan_enable = wpa_s->conf->p2p_dfs_chan_enable;
Sunil Ravic0f5d412024-09-11 22:12:49 +00005050 p2p.register_bootstrap_comeback = wpas_p2p_register_bootstrap_comeback;
5051 p2p.bootstrap_req_rx = wpas_bootstrap_req_rx;
5052 p2p.bootstrap_completed = wpas_bootstrap_completed;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005053
5054 os_memcpy(wpa_s->global->p2p_dev_addr, wpa_s->own_addr, ETH_ALEN);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005055 os_memcpy(p2p.dev_addr, wpa_s->global->p2p_dev_addr, ETH_ALEN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005056 p2p.dev_name = wpa_s->conf->device_name;
5057 p2p.manufacturer = wpa_s->conf->manufacturer;
5058 p2p.model_name = wpa_s->conf->model_name;
5059 p2p.model_number = wpa_s->conf->model_number;
5060 p2p.serial_number = wpa_s->conf->serial_number;
5061 if (wpa_s->wps) {
5062 os_memcpy(p2p.uuid, wpa_s->wps->uuid, 16);
5063 p2p.config_methods = wpa_s->wps->config_methods;
5064 }
5065
Hai Shalom60840252021-02-19 19:02:11 -08005066 if (wpas_p2p_setup_channels(wpa_s, &p2p.channels, &p2p.cli_channels,
5067 p2p.p2p_6ghz_disable)) {
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07005068 wpa_printf(MSG_ERROR,
5069 "P2P: Failed to configure supported channel list");
5070 return -1;
5071 }
5072
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005073 if (wpa_s->conf->p2p_listen_reg_class &&
5074 wpa_s->conf->p2p_listen_channel) {
5075 p2p.reg_class = wpa_s->conf->p2p_listen_reg_class;
5076 p2p.channel = wpa_s->conf->p2p_listen_channel;
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07005077 p2p.channel_forced = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005078 } else {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005079 /*
5080 * Pick one of the social channels randomly as the listen
5081 * channel.
5082 */
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07005083 if (p2p_config_get_random_social(&p2p, &p2p.reg_class,
Hai Shalom74f70d42019-02-11 14:42:39 -08005084 &p2p.channel,
5085 &global->p2p_go_avoid_freq,
5086 &global->p2p_disallow_freq) !=
5087 0) {
Dmitry Shmidt1d755d02015-04-28 10:34:29 -07005088 wpa_printf(MSG_INFO,
5089 "P2P: No social channels supported by the driver - do not enable P2P");
5090 return 0;
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07005091 }
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07005092 p2p.channel_forced = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005093 }
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07005094 wpa_printf(MSG_DEBUG, "P2P: Own listen channel: %d:%d",
5095 p2p.reg_class, p2p.channel);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005096
5097 if (wpa_s->conf->p2p_oper_reg_class &&
5098 wpa_s->conf->p2p_oper_channel) {
5099 p2p.op_reg_class = wpa_s->conf->p2p_oper_reg_class;
5100 p2p.op_channel = wpa_s->conf->p2p_oper_channel;
5101 p2p.cfg_op_channel = 1;
5102 wpa_printf(MSG_DEBUG, "P2P: Configured operating channel: "
5103 "%d:%d", p2p.op_reg_class, p2p.op_channel);
5104
5105 } else {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005106 /*
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07005107 * Use random operation channel from 2.4 GHz band social
5108 * channels (1, 6, 11) or band 60 GHz social channel (2) if no
5109 * other preference is indicated.
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005110 */
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07005111 if (p2p_config_get_random_social(&p2p, &p2p.op_reg_class,
Hai Shalom74f70d42019-02-11 14:42:39 -08005112 &p2p.op_channel, NULL,
5113 NULL) != 0) {
5114 wpa_printf(MSG_INFO,
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07005115 "P2P: Failed to select random social channel as operation channel");
Hai Shalom74f70d42019-02-11 14:42:39 -08005116 p2p.op_reg_class = 0;
5117 p2p.op_channel = 0;
5118 /* This will be overridden during group setup in
5119 * p2p_prepare_channel(), so allow setup to continue. */
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07005120 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005121 p2p.cfg_op_channel = 0;
5122 wpa_printf(MSG_DEBUG, "P2P: Random operating channel: "
5123 "%d:%d", p2p.op_reg_class, p2p.op_channel);
5124 }
Dmitry Shmidt44c95782013-05-17 09:51:35 -07005125
5126 if (wpa_s->conf->p2p_pref_chan && wpa_s->conf->num_p2p_pref_chan) {
5127 p2p.pref_chan = wpa_s->conf->p2p_pref_chan;
5128 p2p.num_pref_chan = wpa_s->conf->num_p2p_pref_chan;
5129 }
5130
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005131 if (wpa_s->conf->country[0] && wpa_s->conf->country[1]) {
5132 os_memcpy(p2p.country, wpa_s->conf->country, 2);
5133 p2p.country[2] = 0x04;
5134 } else
5135 os_memcpy(p2p.country, "XX\x04", 3);
5136
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005137 os_memcpy(p2p.pri_dev_type, wpa_s->conf->device_type,
5138 WPS_DEV_TYPE_LEN);
5139
5140 p2p.num_sec_dev_types = wpa_s->conf->num_sec_device_types;
5141 os_memcpy(p2p.sec_dev_type, wpa_s->conf->sec_device_type,
5142 p2p.num_sec_dev_types * WPS_DEV_TYPE_LEN);
5143
5144 p2p.concurrent_operations = !!(wpa_s->drv_flags &
5145 WPA_DRIVER_FLAGS_P2P_CONCURRENT);
5146
5147 p2p.max_peers = 100;
5148
5149 if (wpa_s->conf->p2p_ssid_postfix) {
5150 p2p.ssid_postfix_len =
5151 os_strlen(wpa_s->conf->p2p_ssid_postfix);
5152 if (p2p.ssid_postfix_len > sizeof(p2p.ssid_postfix))
5153 p2p.ssid_postfix_len = sizeof(p2p.ssid_postfix);
5154 os_memcpy(p2p.ssid_postfix, wpa_s->conf->p2p_ssid_postfix,
5155 p2p.ssid_postfix_len);
5156 }
5157
5158 p2p.p2p_intra_bss = wpa_s->conf->p2p_intra_bss;
5159
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08005160 p2p.max_listen = wpa_s->max_remain_on_chan;
5161
Dmitry Shmidt2271d3f2014-06-23 12:16:31 -07005162 if (wpa_s->conf->p2p_passphrase_len >= 8 &&
5163 wpa_s->conf->p2p_passphrase_len <= 63)
5164 p2p.passphrase_len = wpa_s->conf->p2p_passphrase_len;
5165 else
5166 p2p.passphrase_len = 8;
5167
Sunil Ravic0f5d412024-09-11 22:12:49 +00005168 if (wpa_s->conf->dik &&
5169 wpabuf_len(wpa_s->conf->dik) <= DEVICE_IDENTITY_KEY_MAX_LEN) {
5170 p2p.pairing_config.dik_cipher = wpa_s->conf->dik_cipher;
5171 p2p.pairing_config.dik_len = wpabuf_len(wpa_s->conf->dik);
5172 os_memcpy(p2p.pairing_config.dik_data,
5173 wpabuf_head(wpa_s->conf->dik),
5174 p2p.pairing_config.dik_len);
5175 } else {
5176 p2p.pairing_config.dik_cipher = DIRA_CIPHER_VERSION_128;
5177 p2p.pairing_config.dik_len = DEVICE_IDENTITY_KEY_LEN;
5178 if (os_get_random(p2p.pairing_config.dik_data,
5179 p2p.pairing_config.dik_len) < 0)
5180 return -1;
5181
5182 wpa_s->conf->dik =
5183 wpabuf_alloc_copy(p2p.pairing_config.dik_data,
5184 p2p.pairing_config.dik_len);
5185 if (!wpa_s->conf->dik)
5186 return -1;
5187
5188 wpa_s->conf->dik_cipher = p2p.pairing_config.dik_cipher;
5189
5190 if (wpa_s->conf->update_config &&
5191 wpa_config_write(wpa_s->confname, wpa_s->conf))
5192 wpa_printf(MSG_DEBUG,
5193 "P2P: Failed to update configuration");
5194 }
5195
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005196 global->p2p = p2p_init(&p2p);
5197 if (global->p2p == NULL)
5198 return -1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07005199 global->p2p_init_wpa_s = wpa_s;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005200
5201 for (i = 0; i < MAX_WPS_VENDOR_EXT; i++) {
5202 if (wpa_s->conf->wps_vendor_ext[i] == NULL)
5203 continue;
5204 p2p_add_wps_vendor_extension(
5205 global->p2p, wpa_s->conf->wps_vendor_ext[i]);
5206 }
5207
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07005208 p2p_set_no_go_freq(global->p2p, &wpa_s->conf->p2p_no_go_freq);
5209
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005210 return 0;
5211}
5212
5213
5214/**
5215 * wpas_p2p_deinit - Deinitialize per-interface P2P data
5216 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
5217 *
5218 * This function deinitialize per-interface P2P data.
5219 */
5220void wpas_p2p_deinit(struct wpa_supplicant *wpa_s)
5221{
5222 if (wpa_s->driver && wpa_s->drv_priv)
5223 wpa_drv_probe_req_report(wpa_s, 0);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005224
5225 if (wpa_s->go_params) {
5226 /* Clear any stored provisioning info */
5227 p2p_clear_provisioning_info(
5228 wpa_s->global->p2p,
Dmitry Shmidt04949592012-07-19 12:16:46 -07005229 wpa_s->go_params->peer_device_addr);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005230 }
5231
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005232 os_free(wpa_s->go_params);
5233 wpa_s->go_params = NULL;
Dmitry Shmidt684785c2014-05-12 13:34:29 -07005234 eloop_cancel_timeout(wpas_p2p_psk_failure_removal, wpa_s, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005235 eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s, NULL);
5236 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
Hai Shalomfdcde762020-04-02 11:19:20 -07005237 wpa_s->global->p2p_long_listen = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005238 eloop_cancel_timeout(wpas_p2p_long_listen_timeout, wpa_s, NULL);
5239 eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL);
5240 wpas_p2p_remove_pending_group_interface(wpa_s);
Dmitry Shmidt04f534e2013-12-09 15:50:16 -08005241 eloop_cancel_timeout(wpas_p2p_group_freq_conflict, wpa_s, NULL);
Hai Shalom899fcc72020-10-19 14:38:18 -07005242 eloop_cancel_timeout(wpas_p2p_reconsider_moving_go, wpa_s, NULL);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005243 wpas_p2p_listen_work_done(wpa_s);
5244 if (wpa_s->p2p_send_action_work) {
5245 os_free(wpa_s->p2p_send_action_work->ctx);
5246 radio_work_done(wpa_s->p2p_send_action_work);
5247 wpa_s->p2p_send_action_work = NULL;
5248 }
5249 eloop_cancel_timeout(wpas_p2p_send_action_work_timeout, wpa_s, NULL);
Sunil Ravic0f5d412024-09-11 22:12:49 +00005250 eloop_cancel_timeout(wpas_p2p_send_bootstrap_comeback, wpa_s, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005251
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08005252 wpabuf_free(wpa_s->p2p_oob_dev_pw);
5253 wpa_s->p2p_oob_dev_pw = NULL;
5254
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005255 os_free(wpa_s->p2p_group_common_freqs);
5256 wpa_s->p2p_group_common_freqs = NULL;
5257 wpa_s->p2p_group_common_freqs_num = 0;
5258
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005259 /* TODO: remove group interface from the driver if this wpa_s instance
5260 * is on top of a P2P group interface */
5261}
5262
5263
5264/**
5265 * wpas_p2p_deinit_global - Deinitialize global P2P module
5266 * @global: Pointer to global data from wpa_supplicant_init()
5267 *
5268 * This function deinitializes the global (per device) P2P module.
5269 */
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07005270static void wpas_p2p_deinit_global(struct wpa_global *global)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005271{
5272 struct wpa_supplicant *wpa_s, *tmp;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005273
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005274 wpa_s = global->ifaces;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005275
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07005276 wpas_p2p_service_flush(global->p2p_init_wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005277
5278 /* Remove remaining P2P group interfaces */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005279 while (wpa_s && wpa_s->p2p_group_interface != NOT_P2P_GROUP_INTERFACE)
5280 wpa_s = wpa_s->next;
5281 while (wpa_s) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005282 tmp = global->ifaces;
5283 while (tmp &&
5284 (tmp == wpa_s ||
5285 tmp->p2p_group_interface == NOT_P2P_GROUP_INTERFACE)) {
5286 tmp = tmp->next;
5287 }
5288 if (tmp == NULL)
5289 break;
Dmitry Shmidt04949592012-07-19 12:16:46 -07005290 /* Disconnect from the P2P group and deinit the interface */
5291 wpas_p2p_disconnect(tmp);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005292 }
5293
5294 /*
5295 * Deinit GO data on any possibly remaining interface (if main
5296 * interface is used as GO).
5297 */
5298 for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
5299 if (wpa_s->ap_iface)
5300 wpas_p2p_group_deinit(wpa_s);
5301 }
5302
5303 p2p_deinit(global->p2p);
5304 global->p2p = NULL;
Dmitry Shmidt04949592012-07-19 12:16:46 -07005305 global->p2p_init_wpa_s = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005306}
5307
5308
5309static int wpas_p2p_create_iface(struct wpa_supplicant *wpa_s)
5310{
Dmitry Shmidt9c175262016-03-03 10:20:07 -08005311 if (wpa_s->conf->p2p_no_group_iface)
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08005312 return 0; /* separate interface disabled per configuration */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005313 if (wpa_s->drv_flags &
5314 (WPA_DRIVER_FLAGS_P2P_DEDICATED_INTERFACE |
5315 WPA_DRIVER_FLAGS_P2P_MGMT_AND_NON_P2P))
5316 return 1; /* P2P group requires a new interface in every case
5317 */
5318 if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_CONCURRENT))
5319 return 0; /* driver does not support concurrent operations */
5320 if (wpa_s->global->ifaces->next)
5321 return 1; /* more that one interface already in use */
5322 if (wpa_s->wpa_state >= WPA_AUTHENTICATING)
5323 return 1; /* this interface is already in use */
5324 return 0;
5325}
5326
5327
5328static int wpas_p2p_start_go_neg(struct wpa_supplicant *wpa_s,
5329 const u8 *peer_addr,
5330 enum p2p_wps_method wps_method,
5331 int go_intent, const u8 *own_interface_addr,
Dmitry Shmidt04949592012-07-19 12:16:46 -07005332 unsigned int force_freq, int persistent_group,
Sunil Ravic0f5d412024-09-11 22:12:49 +00005333 struct wpa_ssid *ssid, unsigned int pref_freq,
5334 bool p2p2, u16 bootstrap, const char *password)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005335{
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005336 if (persistent_group && wpa_s->conf->persistent_reconnect)
5337 persistent_group = 2;
5338
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005339 /*
5340 * Increase GO config timeout if HT40 is used since it takes some time
5341 * to scan channels for coex purposes before the BSS can be started.
5342 */
5343 p2p_set_config_timeout(wpa_s->global->p2p,
5344 wpa_s->p2p_go_ht40 ? 255 : 100, 20);
5345
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005346 return p2p_connect(wpa_s->global->p2p, peer_addr, wps_method,
5347 go_intent, own_interface_addr, force_freq,
Dmitry Shmidt04949592012-07-19 12:16:46 -07005348 persistent_group, ssid ? ssid->ssid : NULL,
5349 ssid ? ssid->ssid_len : 0,
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08005350 wpa_s->p2p_pd_before_go_neg, pref_freq,
5351 wps_method == WPS_NFC ? wpa_s->p2p_oob_dev_pw_id :
Sunil Ravic0f5d412024-09-11 22:12:49 +00005352 0, p2p2, bootstrap, password);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005353}
5354
5355
5356static int wpas_p2p_auth_go_neg(struct wpa_supplicant *wpa_s,
5357 const u8 *peer_addr,
5358 enum p2p_wps_method wps_method,
5359 int go_intent, const u8 *own_interface_addr,
Dmitry Shmidt04949592012-07-19 12:16:46 -07005360 unsigned int force_freq, int persistent_group,
Sunil Ravic0f5d412024-09-11 22:12:49 +00005361 struct wpa_ssid *ssid, unsigned int pref_freq,
5362 u16 bootstrap, const char *password)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005363{
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005364 if (persistent_group && wpa_s->conf->persistent_reconnect)
5365 persistent_group = 2;
5366
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005367 return p2p_authorize(wpa_s->global->p2p, peer_addr, wps_method,
5368 go_intent, own_interface_addr, force_freq,
Dmitry Shmidt04949592012-07-19 12:16:46 -07005369 persistent_group, ssid ? ssid->ssid : NULL,
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08005370 ssid ? ssid->ssid_len : 0, pref_freq,
5371 wps_method == WPS_NFC ? wpa_s->p2p_oob_dev_pw_id :
Sunil Ravic0f5d412024-09-11 22:12:49 +00005372 0, bootstrap, password);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005373}
5374
5375
5376static void wpas_p2p_check_join_scan_limit(struct wpa_supplicant *wpa_s)
5377{
5378 wpa_s->p2p_join_scan_count++;
5379 wpa_printf(MSG_DEBUG, "P2P: Join scan attempt %d",
5380 wpa_s->p2p_join_scan_count);
5381 if (wpa_s->p2p_join_scan_count > P2P_MAX_JOIN_SCAN_ATTEMPTS) {
5382 wpa_printf(MSG_DEBUG, "P2P: Failed to find GO " MACSTR
5383 " for join operationg - stop join attempt",
5384 MAC2STR(wpa_s->pending_join_iface_addr));
5385 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
Dmitry Shmidt04949592012-07-19 12:16:46 -07005386 if (wpa_s->p2p_auto_pd) {
5387 wpa_s->p2p_auto_pd = 0;
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07005388 wpa_msg_global(wpa_s, MSG_INFO,
5389 P2P_EVENT_PROV_DISC_FAILURE
5390 " p2p_dev_addr=" MACSTR " status=N/A",
5391 MAC2STR(wpa_s->pending_join_dev_addr));
Dmitry Shmidt04949592012-07-19 12:16:46 -07005392 return;
5393 }
Jimmy Chenb7b3f4d2020-09-02 16:50:11 +08005394 if (wpa_s->p2p_fallback_to_go_neg) {
5395 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Join operating "
5396 "failed - fall back to GO Negotiation");
5397 wpa_msg_global(wpa_s->p2pdev, MSG_INFO,
5398 P2P_EVENT_FALLBACK_TO_GO_NEG
5399 "reason=join-failed");
5400 wpas_p2p_fallback_to_go_neg(wpa_s, 0);
5401 return;
5402 }
Dmitry Shmidt9c175262016-03-03 10:20:07 -08005403 wpa_msg_global(wpa_s->p2pdev, MSG_INFO,
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07005404 P2P_EVENT_GROUP_FORMATION_FAILURE);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005405 wpas_notify_p2p_group_formation_failure(wpa_s, "");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005406 }
5407}
5408
5409
Dmitry Shmidt04949592012-07-19 12:16:46 -07005410static int wpas_check_freq_conflict(struct wpa_supplicant *wpa_s, int freq)
5411{
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07005412 int res;
5413 unsigned int num, i;
5414 struct wpa_used_freq_data *freqs;
Dmitry Shmidt04949592012-07-19 12:16:46 -07005415
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07005416 if (wpas_p2p_num_unused_channels(wpa_s) > 0) {
5417 /* Multiple channels are supported and not all are in use */
Dmitry Shmidt04949592012-07-19 12:16:46 -07005418 return 0;
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07005419 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07005420
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07005421 freqs = os_calloc(wpa_s->num_multichan_concurrent,
5422 sizeof(struct wpa_used_freq_data));
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07005423 if (!freqs)
5424 return 1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07005425
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07005426 num = wpas_p2p_valid_oper_freqs(wpa_s, freqs,
5427 wpa_s->num_multichan_concurrent);
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07005428
5429 for (i = 0; i < num; i++) {
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07005430 if (freqs[i].freq == freq) {
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07005431 wpa_printf(MSG_DEBUG, "P2P: Frequency %d MHz in use by another virtual interface and can be used",
5432 freq);
5433 res = 0;
5434 goto exit_free;
Dmitry Shmidt04949592012-07-19 12:16:46 -07005435 }
5436 }
5437
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07005438 wpa_printf(MSG_DEBUG, "P2P: No valid operating frequencies");
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07005439 res = 1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07005440
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07005441exit_free:
5442 os_free(freqs);
5443 return res;
Dmitry Shmidt04949592012-07-19 12:16:46 -07005444}
5445
5446
5447static int wpas_p2p_peer_go(struct wpa_supplicant *wpa_s,
5448 const u8 *peer_dev_addr)
5449{
5450 struct wpa_bss *bss;
5451 int updated;
5452
5453 bss = wpa_bss_get_p2p_dev_addr(wpa_s, peer_dev_addr);
5454 if (bss == NULL)
5455 return -1;
5456 if (bss->last_update_idx < wpa_s->bss_update_idx) {
5457 wpa_printf(MSG_DEBUG, "P2P: Peer BSS entry not updated in the "
5458 "last scan");
5459 return 0;
5460 }
5461
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005462 updated = os_reltime_before(&wpa_s->p2p_auto_started,
5463 &bss->last_update);
Dmitry Shmidt04949592012-07-19 12:16:46 -07005464 wpa_printf(MSG_DEBUG, "P2P: Current BSS entry for peer updated at "
5465 "%ld.%06ld (%supdated in last scan)",
5466 bss->last_update.sec, bss->last_update.usec,
5467 updated ? "": "not ");
5468
5469 return updated;
5470}
5471
5472
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005473static void wpas_p2p_scan_res_join(struct wpa_supplicant *wpa_s,
5474 struct wpa_scan_results *scan_res)
5475{
Dmitry Shmidt344abd32014-01-14 13:17:00 -08005476 struct wpa_bss *bss = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005477 int freq;
5478 u8 iface_addr[ETH_ALEN];
Dmitry Shmidt04949592012-07-19 12:16:46 -07005479
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005480 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
5481
5482 if (wpa_s->global->p2p_disabled)
5483 return;
5484
Dmitry Shmidt04949592012-07-19 12:16:46 -07005485 wpa_printf(MSG_DEBUG, "P2P: Scan results received (%d BSS) for %sjoin",
5486 scan_res ? (int) scan_res->num : -1,
5487 wpa_s->p2p_auto_join ? "auto_" : "");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005488
5489 if (scan_res)
5490 wpas_p2p_scan_res_handler(wpa_s, scan_res);
5491
Dmitry Shmidt04949592012-07-19 12:16:46 -07005492 if (wpa_s->p2p_auto_pd) {
5493 int join = wpas_p2p_peer_go(wpa_s,
5494 wpa_s->pending_join_dev_addr);
5495 if (join == 0 &&
5496 wpa_s->auto_pd_scan_retry < P2P_AUTO_PD_SCAN_ATTEMPTS) {
5497 wpa_s->auto_pd_scan_retry++;
Dmitry Shmidt444d5672013-04-01 13:08:44 -07005498 bss = wpa_bss_get_bssid_latest(
5499 wpa_s, wpa_s->pending_join_dev_addr);
Dmitry Shmidt04949592012-07-19 12:16:46 -07005500 if (bss) {
5501 freq = bss->freq;
5502 wpa_printf(MSG_DEBUG, "P2P: Scan retry %d for "
5503 "the peer " MACSTR " at %d MHz",
5504 wpa_s->auto_pd_scan_retry,
5505 MAC2STR(wpa_s->
5506 pending_join_dev_addr),
5507 freq);
Dmitry Shmidt344abd32014-01-14 13:17:00 -08005508 wpas_p2p_join_scan_req(wpa_s, freq, NULL, 0);
Dmitry Shmidt04949592012-07-19 12:16:46 -07005509 return;
5510 }
5511 }
5512
5513 if (join < 0)
5514 join = 0;
5515
5516 wpa_s->p2p_auto_pd = 0;
5517 wpa_s->pending_pd_use = join ? AUTO_PD_JOIN : AUTO_PD_GO_NEG;
5518 wpa_printf(MSG_DEBUG, "P2P: Auto PD with " MACSTR " join=%d",
5519 MAC2STR(wpa_s->pending_join_dev_addr), join);
5520 if (p2p_prov_disc_req(wpa_s->global->p2p,
Dmitry Shmidt216983b2015-02-06 10:50:36 -08005521 wpa_s->pending_join_dev_addr, NULL,
Dmitry Shmidt04949592012-07-19 12:16:46 -07005522 wpa_s->pending_pd_config_methods, join,
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08005523 0, wpa_s->user_initiated_pd) < 0) {
Dmitry Shmidt04949592012-07-19 12:16:46 -07005524 wpa_s->p2p_auto_pd = 0;
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07005525 wpa_msg_global(wpa_s, MSG_INFO,
5526 P2P_EVENT_PROV_DISC_FAILURE
5527 " p2p_dev_addr=" MACSTR " status=N/A",
5528 MAC2STR(wpa_s->pending_join_dev_addr));
Dmitry Shmidt04949592012-07-19 12:16:46 -07005529 }
5530 return;
5531 }
5532
5533 if (wpa_s->p2p_auto_join) {
5534 int join = wpas_p2p_peer_go(wpa_s,
5535 wpa_s->pending_join_dev_addr);
5536 if (join < 0) {
5537 wpa_printf(MSG_DEBUG, "P2P: Peer was not found to be "
5538 "running a GO -> use GO Negotiation");
Dmitry Shmidt9c175262016-03-03 10:20:07 -08005539 wpa_msg_global(wpa_s->p2pdev, MSG_INFO,
Dmitry Shmidt7f656022015-02-25 14:36:37 -08005540 P2P_EVENT_FALLBACK_TO_GO_NEG
5541 "reason=peer-not-running-GO");
Dmitry Shmidt04949592012-07-19 12:16:46 -07005542 wpas_p2p_connect(wpa_s, wpa_s->pending_join_dev_addr,
5543 wpa_s->p2p_pin, wpa_s->p2p_wps_method,
5544 wpa_s->p2p_persistent_group, 0, 0, 0,
5545 wpa_s->p2p_go_intent,
5546 wpa_s->p2p_connect_freq,
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08005547 wpa_s->p2p_go_vht_center_freq2,
Dmitry Shmidt04949592012-07-19 12:16:46 -07005548 wpa_s->p2p_persistent_id,
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005549 wpa_s->p2p_pd_before_go_neg,
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07005550 wpa_s->p2p_go_ht40,
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08005551 wpa_s->p2p_go_vht,
Dmitry Shmidtde47be72016-01-07 12:52:55 -08005552 wpa_s->p2p_go_max_oper_chwidth,
Hai Shalom74f70d42019-02-11 14:42:39 -08005553 wpa_s->p2p_go_he,
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08005554 wpa_s->p2p_go_edmg,
Hai Shalomc1a21442022-02-04 13:43:00 -08005555 NULL, 0,
Sunil Ravic0f5d412024-09-11 22:12:49 +00005556 is_p2p_allow_6ghz(wpa_s->global->p2p),
5557 wpa_s->p2p2, wpa_s->p2p_bootstrap,
5558 NULL);
Dmitry Shmidt04949592012-07-19 12:16:46 -07005559 return;
5560 }
5561
5562 wpa_printf(MSG_DEBUG, "P2P: Peer was found running GO%s -> "
5563 "try to join the group", join ? "" :
5564 " in older scan");
Dmitry Shmidt7f656022015-02-25 14:36:37 -08005565 if (!join) {
Dmitry Shmidt9c175262016-03-03 10:20:07 -08005566 wpa_msg_global(wpa_s->p2pdev, MSG_INFO,
Dmitry Shmidt7f656022015-02-25 14:36:37 -08005567 P2P_EVENT_FALLBACK_TO_GO_NEG_ENABLED);
Dmitry Shmidt04949592012-07-19 12:16:46 -07005568 wpa_s->p2p_fallback_to_go_neg = 1;
Dmitry Shmidt7f656022015-02-25 14:36:37 -08005569 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07005570 }
5571
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005572 freq = p2p_get_oper_freq(wpa_s->global->p2p,
5573 wpa_s->pending_join_iface_addr);
5574 if (freq < 0 &&
5575 p2p_get_interface_addr(wpa_s->global->p2p,
5576 wpa_s->pending_join_dev_addr,
5577 iface_addr) == 0 &&
Sunil Ravib0ac25f2024-07-12 01:42:03 +00005578 !ether_addr_equal(iface_addr, wpa_s->pending_join_dev_addr) &&
5579 !wpa_bss_get_bssid(wpa_s, wpa_s->pending_join_iface_addr)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005580 wpa_printf(MSG_DEBUG, "P2P: Overwrite pending interface "
5581 "address for join from " MACSTR " to " MACSTR
5582 " based on newly discovered P2P peer entry",
5583 MAC2STR(wpa_s->pending_join_iface_addr),
5584 MAC2STR(iface_addr));
5585 os_memcpy(wpa_s->pending_join_iface_addr, iface_addr,
5586 ETH_ALEN);
5587
5588 freq = p2p_get_oper_freq(wpa_s->global->p2p,
5589 wpa_s->pending_join_iface_addr);
5590 }
5591 if (freq >= 0) {
5592 wpa_printf(MSG_DEBUG, "P2P: Target GO operating frequency "
5593 "from P2P peer table: %d MHz", freq);
5594 }
Dmitry Shmidt344abd32014-01-14 13:17:00 -08005595 if (wpa_s->p2p_join_ssid_len) {
5596 wpa_printf(MSG_DEBUG, "P2P: Trying to find target GO BSS entry based on BSSID "
5597 MACSTR " and SSID %s",
5598 MAC2STR(wpa_s->pending_join_iface_addr),
5599 wpa_ssid_txt(wpa_s->p2p_join_ssid,
5600 wpa_s->p2p_join_ssid_len));
5601 bss = wpa_bss_get(wpa_s, wpa_s->pending_join_iface_addr,
5602 wpa_s->p2p_join_ssid,
5603 wpa_s->p2p_join_ssid_len);
Dmitry Shmidtde47be72016-01-07 12:52:55 -08005604 } else if (!bss) {
Dmitry Shmidt344abd32014-01-14 13:17:00 -08005605 wpa_printf(MSG_DEBUG, "P2P: Trying to find target GO BSS entry based on BSSID "
5606 MACSTR, MAC2STR(wpa_s->pending_join_iface_addr));
5607 bss = wpa_bss_get_bssid_latest(wpa_s,
5608 wpa_s->pending_join_iface_addr);
5609 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005610 if (bss) {
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07005611 u8 dev_addr[ETH_ALEN];
5612
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005613 freq = bss->freq;
5614 wpa_printf(MSG_DEBUG, "P2P: Target GO operating frequency "
Dmitry Shmidt051af732013-10-22 13:52:46 -07005615 "from BSS table: %d MHz (SSID %s)", freq,
5616 wpa_ssid_txt(bss->ssid, bss->ssid_len));
Hai Shalom60840252021-02-19 19:02:11 -08005617 if (p2p_parse_dev_addr(wpa_bss_ie_ptr(bss), bss->ie_len,
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07005618 dev_addr) == 0 &&
Sunil Ravib0ac25f2024-07-12 01:42:03 +00005619 ether_addr_equal(wpa_s->pending_join_dev_addr,
5620 wpa_s->pending_join_iface_addr) &&
5621 !ether_addr_equal(dev_addr, wpa_s->pending_join_dev_addr)) {
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07005622 wpa_printf(MSG_DEBUG,
5623 "P2P: Update target GO device address based on BSS entry: " MACSTR " (was " MACSTR ")",
5624 MAC2STR(dev_addr),
5625 MAC2STR(wpa_s->pending_join_dev_addr));
5626 os_memcpy(wpa_s->pending_join_dev_addr, dev_addr,
5627 ETH_ALEN);
5628 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005629 }
5630 if (freq > 0) {
5631 u16 method;
5632
Dmitry Shmidt04949592012-07-19 12:16:46 -07005633 if (wpas_check_freq_conflict(wpa_s, freq) > 0) {
Dmitry Shmidt9c175262016-03-03 10:20:07 -08005634 wpa_msg_global(wpa_s->p2pdev, MSG_INFO,
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07005635 P2P_EVENT_GROUP_FORMATION_FAILURE
5636 "reason=FREQ_CONFLICT");
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005637 wpas_notify_p2p_group_formation_failure(
5638 wpa_s, "FREQ_CONFLICT");
Dmitry Shmidt04949592012-07-19 12:16:46 -07005639 return;
5640 }
5641
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005642 wpa_printf(MSG_DEBUG, "P2P: Send Provision Discovery Request "
5643 "prior to joining an existing group (GO " MACSTR
5644 " freq=%u MHz)",
5645 MAC2STR(wpa_s->pending_join_dev_addr), freq);
5646 wpa_s->pending_pd_before_join = 1;
5647
5648 switch (wpa_s->pending_join_wps_method) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005649 case WPS_PIN_DISPLAY:
5650 method = WPS_CONFIG_KEYPAD;
5651 break;
5652 case WPS_PIN_KEYPAD:
5653 method = WPS_CONFIG_DISPLAY;
5654 break;
5655 case WPS_PBC:
5656 method = WPS_CONFIG_PUSHBUTTON;
5657 break;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005658 case WPS_P2PS:
5659 method = WPS_CONFIG_P2PS;
5660 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005661 default:
5662 method = 0;
5663 break;
5664 }
5665
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005666 if ((p2p_get_provisioning_info(wpa_s->global->p2p,
5667 wpa_s->pending_join_dev_addr) ==
5668 method)) {
5669 /*
5670 * We have already performed provision discovery for
5671 * joining the group. Proceed directly to join
5672 * operation without duplicated provision discovery. */
5673 wpa_printf(MSG_DEBUG, "P2P: Provision discovery "
5674 "with " MACSTR " already done - proceed to "
5675 "join",
5676 MAC2STR(wpa_s->pending_join_dev_addr));
5677 wpa_s->pending_pd_before_join = 0;
5678 goto start;
5679 }
5680
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005681 if (p2p_prov_disc_req(wpa_s->global->p2p,
Dmitry Shmidt216983b2015-02-06 10:50:36 -08005682 wpa_s->pending_join_dev_addr,
5683 NULL, method, 1,
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08005684 freq, wpa_s->user_initiated_pd) < 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005685 wpa_printf(MSG_DEBUG, "P2P: Failed to send Provision "
5686 "Discovery Request before joining an "
5687 "existing group");
5688 wpa_s->pending_pd_before_join = 0;
5689 goto start;
5690 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005691 return;
5692 }
5693
5694 wpa_printf(MSG_DEBUG, "P2P: Failed to find BSS/GO - try again later");
5695 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
5696 eloop_register_timeout(1, 0, wpas_p2p_join_scan, wpa_s, NULL);
5697 wpas_p2p_check_join_scan_limit(wpa_s);
5698 return;
5699
5700start:
5701 /* Start join operation immediately */
Dmitry Shmidtde47be72016-01-07 12:52:55 -08005702 wpas_p2p_join_start(wpa_s, 0, wpa_s->p2p_join_ssid,
5703 wpa_s->p2p_join_ssid_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005704}
5705
5706
Dmitry Shmidt344abd32014-01-14 13:17:00 -08005707static void wpas_p2p_join_scan_req(struct wpa_supplicant *wpa_s, int freq,
5708 const u8 *ssid, size_t ssid_len)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005709{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005710 int ret;
5711 struct wpa_driver_scan_params params;
5712 struct wpabuf *wps_ie, *ies;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005713 size_t ielen;
Dmitry Shmidt04949592012-07-19 12:16:46 -07005714 int freqs[2] = { 0, 0 };
Dmitry Shmidt9c175262016-03-03 10:20:07 -08005715 unsigned int bands;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005716
5717 os_memset(&params, 0, sizeof(params));
5718
5719 /* P2P Wildcard SSID */
5720 params.num_ssids = 1;
Dmitry Shmidt344abd32014-01-14 13:17:00 -08005721 if (ssid && ssid_len) {
5722 params.ssids[0].ssid = ssid;
5723 params.ssids[0].ssid_len = ssid_len;
5724 os_memcpy(wpa_s->p2p_join_ssid, ssid, ssid_len);
5725 wpa_s->p2p_join_ssid_len = ssid_len;
5726 } else {
5727 params.ssids[0].ssid = (u8 *) P2P_WILDCARD_SSID;
5728 params.ssids[0].ssid_len = P2P_WILDCARD_SSID_LEN;
5729 wpa_s->p2p_join_ssid_len = 0;
5730 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005731
5732 wpa_s->wps->dev.p2p = 1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07005733 wps_ie = wps_build_probe_req_ie(DEV_PW_DEFAULT, &wpa_s->wps->dev,
5734 wpa_s->wps->uuid, WPS_REQ_ENROLLEE, 0,
5735 NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005736 if (wps_ie == NULL) {
5737 wpas_p2p_scan_res_join(wpa_s, NULL);
5738 return;
5739 }
5740
Dmitry Shmidt9e3f8ee2014-01-17 10:52:01 -08005741 if (!freq) {
5742 int oper_freq;
5743 /*
5744 * If freq is not provided, check the operating freq of the GO
5745 * and use a single channel scan on if possible.
5746 */
5747 oper_freq = p2p_get_oper_freq(wpa_s->global->p2p,
5748 wpa_s->pending_join_iface_addr);
5749 if (oper_freq > 0)
5750 freq = oper_freq;
5751 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07005752 if (freq > 0) {
5753 freqs[0] = freq;
5754 params.freqs = freqs;
Sunil Ravi77d572f2023-01-17 23:58:31 +00005755 } else {
5756 wpas_p2p_scan_freqs(wpa_s, &params, true);
Dmitry Shmidt04949592012-07-19 12:16:46 -07005757 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005758
Dmitry Shmidt9c175262016-03-03 10:20:07 -08005759 ielen = p2p_scan_ie_buf_len(wpa_s->global->p2p);
5760 ies = wpabuf_alloc(wpabuf_len(wps_ie) + ielen);
5761 if (ies == NULL) {
5762 wpabuf_free(wps_ie);
5763 wpas_p2p_scan_res_join(wpa_s, NULL);
5764 return;
5765 }
5766 wpabuf_put_buf(ies, wps_ie);
5767 wpabuf_free(wps_ie);
5768
5769 bands = wpas_get_bands(wpa_s, freqs);
5770 p2p_scan_ie(wpa_s->global->p2p, ies, NULL, bands);
5771
5772 params.p2p_probe = 1;
5773 params.extra_ies = wpabuf_head(ies);
5774 params.extra_ies_len = wpabuf_len(ies);
5775
Dmitry Shmidtebd93af2017-02-21 13:40:44 -08005776 if (wpa_s->clear_driver_scan_cache) {
5777 wpa_printf(MSG_DEBUG,
5778 "Request driver to clear scan cache due to local BSS flush");
5779 params.only_new_results = 1;
5780 }
5781
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005782 /*
5783 * Run a scan to update BSS table and start Provision Discovery once
5784 * the new scan results become available.
5785 */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005786 ret = wpa_drv_scan(wpa_s, &params);
Hai Shalomc1a21442022-02-04 13:43:00 -08005787 if (params.freqs != freqs)
Hai Shalom899fcc72020-10-19 14:38:18 -07005788 os_free(params.freqs);
Dmitry Shmidt444d5672013-04-01 13:08:44 -07005789 if (!ret) {
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005790 os_get_reltime(&wpa_s->scan_trigger_time);
Dmitry Shmidt04949592012-07-19 12:16:46 -07005791 wpa_s->scan_res_handler = wpas_p2p_scan_res_join;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005792 wpa_s->own_scan_requested = 1;
Dmitry Shmidtebd93af2017-02-21 13:40:44 -08005793 wpa_s->clear_driver_scan_cache = 0;
Dmitry Shmidt444d5672013-04-01 13:08:44 -07005794 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005795
5796 wpabuf_free(ies);
5797
5798 if (ret) {
5799 wpa_printf(MSG_DEBUG, "P2P: Failed to start scan for join - "
5800 "try again later");
5801 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
5802 eloop_register_timeout(1, 0, wpas_p2p_join_scan, wpa_s, NULL);
5803 wpas_p2p_check_join_scan_limit(wpa_s);
5804 }
5805}
5806
5807
Dmitry Shmidt04949592012-07-19 12:16:46 -07005808static void wpas_p2p_join_scan(void *eloop_ctx, void *timeout_ctx)
5809{
5810 struct wpa_supplicant *wpa_s = eloop_ctx;
Dmitry Shmidt344abd32014-01-14 13:17:00 -08005811 wpas_p2p_join_scan_req(wpa_s, 0, NULL, 0);
Dmitry Shmidt04949592012-07-19 12:16:46 -07005812}
5813
5814
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005815static int wpas_p2p_join(struct wpa_supplicant *wpa_s, const u8 *iface_addr,
Dmitry Shmidt04949592012-07-19 12:16:46 -07005816 const u8 *dev_addr, enum p2p_wps_method wps_method,
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08005817 int auto_join, int op_freq,
5818 const u8 *ssid, size_t ssid_len)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005819{
5820 wpa_printf(MSG_DEBUG, "P2P: Request to join existing group (iface "
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08005821 MACSTR " dev " MACSTR " op_freq=%d)%s",
5822 MAC2STR(iface_addr), MAC2STR(dev_addr), op_freq,
Dmitry Shmidt04949592012-07-19 12:16:46 -07005823 auto_join ? " (auto_join)" : "");
Dmitry Shmidt344abd32014-01-14 13:17:00 -08005824 if (ssid && ssid_len) {
5825 wpa_printf(MSG_DEBUG, "P2P: Group SSID specified: %s",
5826 wpa_ssid_txt(ssid, ssid_len));
5827 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005828
Dmitry Shmidt04949592012-07-19 12:16:46 -07005829 wpa_s->p2p_auto_pd = 0;
5830 wpa_s->p2p_auto_join = !!auto_join;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005831 os_memcpy(wpa_s->pending_join_iface_addr, iface_addr, ETH_ALEN);
5832 os_memcpy(wpa_s->pending_join_dev_addr, dev_addr, ETH_ALEN);
5833 wpa_s->pending_join_wps_method = wps_method;
5834
5835 /* Make sure we are not running find during connection establishment */
5836 wpas_p2p_stop_find(wpa_s);
5837
5838 wpa_s->p2p_join_scan_count = 0;
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08005839 wpas_p2p_join_scan_req(wpa_s, op_freq, ssid, ssid_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005840 return 0;
5841}
5842
5843
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08005844static int wpas_p2p_join_start(struct wpa_supplicant *wpa_s, int freq,
5845 const u8 *ssid, size_t ssid_len)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005846{
5847 struct wpa_supplicant *group;
5848 struct p2p_go_neg_results res;
Dmitry Shmidt04949592012-07-19 12:16:46 -07005849 struct wpa_bss *bss;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005850
5851 group = wpas_p2p_get_group_iface(wpa_s, 0, 0);
5852 if (group == NULL)
5853 return -1;
5854 if (group != wpa_s) {
5855 os_memcpy(group->p2p_pin, wpa_s->p2p_pin,
5856 sizeof(group->p2p_pin));
5857 group->p2p_wps_method = wpa_s->p2p_wps_method;
5858 }
5859
Hai Shalom74f70d42019-02-11 14:42:39 -08005860 /*
5861 * Need to mark the current interface for p2p_group_formation
5862 * when a separate group interface is not used. This is needed
5863 * to allow p2p_cancel stop a pending p2p_connect-join.
5864 * wpas_p2p_init_group_interface() addresses this for the case
5865 * where a separate group interface is used.
5866 */
5867 if (group == wpa_s->parent)
5868 wpa_s->global->p2p_group_formation = group;
5869
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005870 group->p2p_in_provisioning = 1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07005871 group->p2p_fallback_to_go_neg = wpa_s->p2p_fallback_to_go_neg;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005872
5873 os_memset(&res, 0, sizeof(res));
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08005874 os_memcpy(res.peer_device_addr, wpa_s->pending_join_dev_addr, ETH_ALEN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005875 os_memcpy(res.peer_interface_addr, wpa_s->pending_join_iface_addr,
5876 ETH_ALEN);
5877 res.wps_method = wpa_s->pending_join_wps_method;
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08005878 if (freq && ssid && ssid_len) {
5879 res.freq = freq;
5880 res.ssid_len = ssid_len;
5881 os_memcpy(res.ssid, ssid, ssid_len);
5882 } else {
Dmitry Shmidtde47be72016-01-07 12:52:55 -08005883 if (ssid && ssid_len) {
5884 bss = wpa_bss_get(wpa_s, wpa_s->pending_join_iface_addr,
5885 ssid, ssid_len);
5886 } else {
5887 bss = wpa_bss_get_bssid_latest(
5888 wpa_s, wpa_s->pending_join_iface_addr);
5889 }
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08005890 if (bss) {
5891 res.freq = bss->freq;
5892 res.ssid_len = bss->ssid_len;
5893 os_memcpy(res.ssid, bss->ssid, bss->ssid_len);
5894 wpa_printf(MSG_DEBUG, "P2P: Join target GO operating frequency from BSS table: %d MHz (SSID %s)",
5895 bss->freq,
5896 wpa_ssid_txt(bss->ssid, bss->ssid_len));
Dmitry Shmidtde47be72016-01-07 12:52:55 -08005897 } else if (ssid && ssid_len) {
5898 res.ssid_len = ssid_len;
5899 os_memcpy(res.ssid, ssid, ssid_len);
5900 wpa_printf(MSG_DEBUG, "P2P: Join target GO (SSID %s)",
5901 wpa_ssid_txt(ssid, ssid_len));
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08005902 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07005903 }
5904
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08005905 if (wpa_s->off_channel_freq || wpa_s->roc_waiting_drv_freq) {
5906 wpa_printf(MSG_DEBUG, "P2P: Cancel remain-on-channel prior to "
5907 "starting client");
5908 wpa_drv_cancel_remain_on_channel(wpa_s);
5909 wpa_s->off_channel_freq = 0;
5910 wpa_s->roc_waiting_drv_freq = 0;
5911 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005912 wpas_start_wps_enrollee(group, &res);
5913
5914 /*
5915 * Allow a longer timeout for join-a-running-group than normal 15
5916 * second group formation timeout since the GO may not have authorized
5917 * our connection yet.
5918 */
5919 eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s, NULL);
5920 eloop_register_timeout(60, 0, wpas_p2p_group_formation_timeout,
5921 wpa_s, NULL);
5922
5923 return 0;
5924}
5925
5926
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08005927static int wpas_p2p_setup_freqs(struct wpa_supplicant *wpa_s, int freq,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005928 int *force_freq, int *pref_freq, int go,
Sunil8cd6f4d2022-06-28 18:40:46 +00005929 struct weighted_pcl *pref_freq_list,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005930 unsigned int *num_pref_freq)
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08005931{
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07005932 struct wpa_used_freq_data *freqs;
5933 int res, best_freq, num_unused;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005934 unsigned int freq_in_use = 0, num, i, max_pref_freq;
Vinayak Yadawad8db34572021-08-30 21:28:05 +05305935 int p2p_pref_freq;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005936
5937 max_pref_freq = *num_pref_freq;
5938 *num_pref_freq = 0;
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07005939
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07005940 freqs = os_calloc(wpa_s->num_multichan_concurrent,
5941 sizeof(struct wpa_used_freq_data));
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07005942 if (!freqs)
5943 return -1;
5944
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07005945 num = wpas_p2p_valid_oper_freqs(wpa_s, freqs,
5946 wpa_s->num_multichan_concurrent);
5947
5948 /*
5949 * It is possible that the total number of used frequencies is bigger
5950 * than the number of frequencies used for P2P, so get the system wide
5951 * number of unused frequencies.
5952 */
5953 num_unused = wpas_p2p_num_unused_channels(wpa_s);
5954
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07005955 wpa_printf(MSG_DEBUG,
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07005956 "P2P: Setup freqs: freq=%d num_MCC=%d shared_freqs=%u num_unused=%d",
5957 freq, wpa_s->num_multichan_concurrent, num, num_unused);
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07005958
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08005959 if (freq > 0) {
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07005960 int ret;
5961 if (go)
5962 ret = p2p_supported_freq(wpa_s->global->p2p, freq);
5963 else
5964 ret = p2p_supported_freq_cli(wpa_s->global->p2p, freq);
5965 if (!ret) {
Dmitry Shmidt203eadb2015-03-05 14:16:04 -08005966 if ((wpa_s->drv_flags & WPA_DRIVER_FLAGS_DFS_OFFLOAD) &&
Roshan Pius3a1667e2018-07-03 15:17:14 -07005967 ieee80211_is_dfs(freq, wpa_s->hw.modes,
5968 wpa_s->hw.num_modes)) {
Dmitry Shmidt203eadb2015-03-05 14:16:04 -08005969 /*
5970 * If freq is a DFS channel and DFS is offloaded
5971 * to the driver, allow P2P GO to use it.
5972 */
5973 wpa_printf(MSG_DEBUG,
5974 "P2P: The forced channel for GO (%u MHz) is DFS, and DFS is offloaded to the driver",
5975 freq);
5976 } else {
5977 wpa_printf(MSG_DEBUG,
5978 "P2P: The forced channel (%u MHz) is not supported for P2P uses",
5979 freq);
5980 res = -3;
5981 goto exit_free;
5982 }
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08005983 }
5984
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07005985 for (i = 0; i < num; i++) {
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07005986 if (freqs[i].freq == freq)
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07005987 freq_in_use = 1;
5988 }
5989
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07005990 if (num_unused <= 0 && !freq_in_use) {
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07005991 wpa_printf(MSG_DEBUG, "P2P: Cannot start P2P group on %u MHz as there are no available channels",
5992 freq);
5993 res = -2;
5994 goto exit_free;
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08005995 }
5996 wpa_printf(MSG_DEBUG, "P2P: Trying to force us to use the "
5997 "requested channel (%u MHz)", freq);
5998 *force_freq = freq;
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07005999 goto exit_ok;
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08006000 }
6001
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07006002 best_freq = wpas_p2p_pick_best_used_freq(wpa_s, freqs, num);
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07006003
Vinayak Yadawad8db34572021-08-30 21:28:05 +05306004 if (*pref_freq == 0) {
6005 if (wpa_s->conf->num_p2p_pref_chan && IS_2GHZ(best_freq)) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006006 i = 0;
Vinayak Yadawad8db34572021-08-30 21:28:05 +05306007 while (i < wpa_s->conf->num_p2p_pref_chan) {
6008 p2p_pref_freq = ieee80211_chan_to_freq(NULL,
6009 wpa_s->conf->p2p_pref_chan[i].op_class,
6010 wpa_s->conf->p2p_pref_chan[i].chan);
6011
6012 if (p2p_supported_freq(wpa_s->global->p2p, p2p_pref_freq) &&
6013 !wpas_p2p_disallowed_freq(wpa_s->global, p2p_pref_freq)) {
6014 best_freq = p2p_pref_freq;
6015 wpa_printf(MSG_DEBUG, "P2P: Using frequency (%u MHz) "
6016 "from P2P preferred channel list", best_freq);
6017 break;
6018 } else {
6019 wpa_printf(MSG_MSGDUMP, "P2P: Skipping preferred "
6020 "frequency (%u MHz) ", p2p_pref_freq);
6021 }
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006022 i++;
6023 }
Sreeramya Soratkalf928e8f2022-03-22 17:33:31 +05306024 } else if (!wpa_s->conf->num_p2p_pref_chan) {
Vinayak Yadawad8db34572021-08-30 21:28:05 +05306025 wpa_printf(MSG_DEBUG, "P2P: best_freq=%d, go=%d",
Sunil Ravi036cec52023-03-29 11:35:17 -07006026 best_freq, go);
Vinayak Yadawad8db34572021-08-30 21:28:05 +05306027
Sunil Ravi036cec52023-03-29 11:35:17 -07006028 *num_pref_freq = max_pref_freq;
6029 res = wpas_p2p_pick_best_pref_freq(wpa_s, go, pref_freq_list,
6030 num_pref_freq);
6031 if (res > 0)
6032 best_freq = res;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006033 }
6034 }
6035
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07006036 /* We have a candidate frequency to use */
6037 if (best_freq > 0) {
6038 if (*pref_freq == 0 && num_unused > 0) {
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08006039 wpa_printf(MSG_DEBUG, "P2P: Try to prefer a frequency (%u MHz) we are already using",
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07006040 best_freq);
6041 *pref_freq = best_freq;
Dmitry Shmidt51a47d52013-09-10 10:52:57 -07006042 } else {
6043 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 -07006044 best_freq);
6045 *force_freq = best_freq;
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07006046 }
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07006047 } else if (num_unused > 0) {
6048 wpa_printf(MSG_DEBUG,
6049 "P2P: Current operating channels are not available for P2P. Try to use another channel");
6050 *force_freq = 0;
6051 } else {
6052 wpa_printf(MSG_DEBUG,
6053 "P2P: All channels are in use and none of them are P2P enabled. Cannot start P2P group");
6054 res = -2;
6055 goto exit_free;
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07006056 }
6057
6058exit_ok:
6059 res = 0;
6060exit_free:
6061 os_free(freqs);
6062 return res;
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08006063}
6064
6065
Hai Shalomc1a21442022-02-04 13:43:00 -08006066static bool is_p2p_6ghz_supported(struct wpa_supplicant *wpa_s,
6067 const u8 *peer_addr)
6068{
6069 if (wpa_s->conf->p2p_6ghz_disable ||
6070 !get_mode(wpa_s->hw.modes, wpa_s->hw.num_modes,
6071 HOSTAPD_MODE_IEEE80211A, true))
6072 return false;
6073
Sunil Ravic0f5d412024-09-11 22:12:49 +00006074 if (wpa_s->p2p2)
6075 return true;
6076
Hai Shalomc1a21442022-02-04 13:43:00 -08006077 if (!p2p_wfd_enabled(wpa_s->global->p2p))
6078 return false;
6079 if (peer_addr && !p2p_peer_wfd_enabled(wpa_s->global->p2p, peer_addr))
6080 return false;
6081
6082 return true;
6083}
6084
6085
6086static int wpas_p2p_check_6ghz(struct wpa_supplicant *wpa_s,
6087 const u8 *peer_addr, bool allow_6ghz, int freq)
6088{
6089 if (allow_6ghz && is_p2p_6ghz_supported(wpa_s, peer_addr)) {
6090 wpa_printf(MSG_DEBUG,
6091 "P2P: Allow connection on 6 GHz channels");
6092 p2p_set_6ghz_dev_capab(wpa_s->global->p2p, true);
6093 } else {
6094 if (is_6ghz_freq(freq))
6095 return -2;
6096 p2p_set_6ghz_dev_capab(wpa_s->global->p2p, false);
6097 }
6098
6099 return 0;
6100}
6101
6102
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006103/**
6104 * wpas_p2p_connect - Request P2P Group Formation to be started
6105 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
6106 * @peer_addr: Address of the peer P2P Device
6107 * @pin: PIN to use during provisioning or %NULL to indicate PBC mode
6108 * @persistent_group: Whether to create a persistent group
Dmitry Shmidt04949592012-07-19 12:16:46 -07006109 * @auto_join: Whether to select join vs. GO Negotiation automatically
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006110 * @join: Whether to join an existing group (as a client) instead of starting
6111 * Group Owner negotiation; @peer_addr is BSSID in that case
6112 * @auth: Whether to only authorize the connection instead of doing that and
6113 * initiating Group Owner negotiation
6114 * @go_intent: GO Intent or -1 to use default
6115 * @freq: Frequency for the group or 0 for auto-selection
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08006116 * @freq2: Center frequency of segment 1 for the GO operating in VHT 80P80 mode
Dmitry Shmidt04949592012-07-19 12:16:46 -07006117 * @persistent_id: Persistent group credentials to use for forcing GO
6118 * parameters or -1 to generate new values (SSID/passphrase)
6119 * @pd: Whether to send Provision Discovery prior to GO Negotiation as an
6120 * interoperability workaround when initiating group formation
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07006121 * @ht40: Start GO with 40 MHz channel width
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07006122 * @vht: Start GO with VHT support
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08006123 * @vht_chwidth: Channel width supported by GO operating with VHT support
Hai Shalom81f62d82019-07-22 12:10:00 -07006124 * (CHANWIDTH_*).
Dmitry Shmidtde47be72016-01-07 12:52:55 -08006125 * @group_ssid: Specific Group SSID for join or %NULL if not set
6126 * @group_ssid_len: Length of @group_ssid in octets
Hai Shalomc1a21442022-02-04 13:43:00 -08006127 * @allow_6ghz: Allow P2P connection on 6 GHz channels
Sunil Ravic0f5d412024-09-11 22:12:49 +00006128 * @p2p2: Whether device is in P2P R2 mode
6129 * @bootstrap: Requested bootstrap method for pairing in P2P2
6130 * @password: Password for pairing setup or NULL for oppurtunistic method
6131 * in P2P2
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006132 * Returns: 0 or new PIN (if pin was %NULL) on success, -1 on unspecified
6133 * failure, -2 on failure due to channel not currently available,
6134 * -3 if forced channel is not supported
6135 */
6136int wpas_p2p_connect(struct wpa_supplicant *wpa_s, const u8 *peer_addr,
6137 const char *pin, enum p2p_wps_method wps_method,
Dmitry Shmidt04949592012-07-19 12:16:46 -07006138 int persistent_group, int auto_join, int join, int auth,
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08006139 int go_intent, int freq, unsigned int vht_center_freq2,
6140 int persistent_id, int pd, int ht40, int vht,
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08006141 unsigned int vht_chwidth, int he, int edmg,
Hai Shalomc1a21442022-02-04 13:43:00 -08006142 const u8 *group_ssid, size_t group_ssid_len,
Sunil Ravic0f5d412024-09-11 22:12:49 +00006143 bool allow_6ghz, bool p2p2, u16 bootstrap,
6144 const char *password)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006145{
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07006146 int force_freq = 0, pref_freq = 0;
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08006147 int ret = 0, res;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006148 enum wpa_driver_if_type iftype;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006149 const u8 *if_addr;
Dmitry Shmidt04949592012-07-19 12:16:46 -07006150 struct wpa_ssid *ssid = NULL;
Sunil8cd6f4d2022-06-28 18:40:46 +00006151 struct weighted_pcl pref_freq_list[P2P_MAX_PREF_CHANNELS];
6152 unsigned int size;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006153
6154 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
6155 return -1;
6156
Dmitry Shmidt04949592012-07-19 12:16:46 -07006157 if (persistent_id >= 0) {
6158 ssid = wpa_config_get_network(wpa_s->conf, persistent_id);
6159 if (ssid == NULL || ssid->disabled != 2 ||
6160 ssid->mode != WPAS_MODE_P2P_GO)
6161 return -1;
6162 }
6163
Sunil Ravic0f5d412024-09-11 22:12:49 +00006164 wpa_s->p2p2 = p2p2;
6165
Hai Shalomc1a21442022-02-04 13:43:00 -08006166 if (wpas_p2p_check_6ghz(wpa_s, peer_addr, allow_6ghz, freq))
Hai Shalom899fcc72020-10-19 14:38:18 -07006167 return -2;
6168
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07006169 os_free(wpa_s->global->add_psk);
6170 wpa_s->global->add_psk = NULL;
6171
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08006172 wpa_s->global->p2p_fail_on_wps_complete = 0;
Dmitry Shmidt216983b2015-02-06 10:50:36 -08006173 wpa_s->global->pending_p2ps_group = 0;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006174 wpa_s->global->pending_p2ps_group_freq = 0;
6175 wpa_s->p2ps_method_config_any = 0;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08006176
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006177 if (go_intent < 0)
6178 go_intent = wpa_s->conf->p2p_go_intent;
6179
6180 if (!auth)
Hai Shalomfdcde762020-04-02 11:19:20 -07006181 wpa_s->global->p2p_long_listen = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006182
6183 wpa_s->p2p_wps_method = wps_method;
Dmitry Shmidt04949592012-07-19 12:16:46 -07006184 wpa_s->p2p_persistent_group = !!persistent_group;
6185 wpa_s->p2p_persistent_id = persistent_id;
6186 wpa_s->p2p_go_intent = go_intent;
6187 wpa_s->p2p_connect_freq = freq;
6188 wpa_s->p2p_fallback_to_go_neg = 0;
6189 wpa_s->p2p_pd_before_go_neg = !!pd;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07006190 wpa_s->p2p_go_ht40 = !!ht40;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07006191 wpa_s->p2p_go_vht = !!vht;
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08006192 wpa_s->p2p_go_vht_center_freq2 = vht_center_freq2;
6193 wpa_s->p2p_go_max_oper_chwidth = vht_chwidth;
Hai Shalom74f70d42019-02-11 14:42:39 -08006194 wpa_s->p2p_go_he = !!he;
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08006195 wpa_s->p2p_go_edmg = !!edmg;
Sunil Ravic0f5d412024-09-11 22:12:49 +00006196 wpa_s->p2p_bootstrap = bootstrap;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006197
6198 if (pin)
6199 os_strlcpy(wpa_s->p2p_pin, pin, sizeof(wpa_s->p2p_pin));
6200 else if (wps_method == WPS_PIN_DISPLAY) {
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08006201 if (wps_generate_pin((unsigned int *) &ret) < 0)
6202 return -1;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006203 res = os_snprintf(wpa_s->p2p_pin, sizeof(wpa_s->p2p_pin),
6204 "%08d", ret);
6205 if (os_snprintf_error(sizeof(wpa_s->p2p_pin), res))
6206 wpa_s->p2p_pin[sizeof(wpa_s->p2p_pin) - 1] = '\0';
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006207 wpa_printf(MSG_DEBUG, "P2P: Randomly generated PIN: %s",
6208 wpa_s->p2p_pin);
Dmitry Shmidt849734c2016-05-27 09:59:01 -07006209 } else if (wps_method == WPS_P2PS) {
6210 /* Force the P2Ps default PIN to be used */
6211 os_strlcpy(wpa_s->p2p_pin, "12345670", sizeof(wpa_s->p2p_pin));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006212 } else
6213 wpa_s->p2p_pin[0] = '\0';
6214
Dmitry Shmidt04949592012-07-19 12:16:46 -07006215 if (join || auto_join) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006216 u8 iface_addr[ETH_ALEN], dev_addr[ETH_ALEN];
6217 if (auth) {
6218 wpa_printf(MSG_DEBUG, "P2P: Authorize invitation to "
6219 "connect a running group from " MACSTR,
6220 MAC2STR(peer_addr));
6221 os_memcpy(wpa_s->p2p_auth_invite, peer_addr, ETH_ALEN);
6222 return ret;
6223 }
6224 os_memcpy(dev_addr, peer_addr, ETH_ALEN);
6225 if (p2p_get_interface_addr(wpa_s->global->p2p, peer_addr,
6226 iface_addr) < 0) {
6227 os_memcpy(iface_addr, peer_addr, ETH_ALEN);
6228 p2p_get_dev_addr(wpa_s->global->p2p, peer_addr,
6229 dev_addr);
6230 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07006231 if (auto_join) {
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08006232 os_get_reltime(&wpa_s->p2p_auto_started);
Dmitry Shmidt04949592012-07-19 12:16:46 -07006233 wpa_printf(MSG_DEBUG, "P2P: Auto join started at "
6234 "%ld.%06ld",
6235 wpa_s->p2p_auto_started.sec,
6236 wpa_s->p2p_auto_started.usec);
6237 }
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08006238 wpa_s->user_initiated_pd = 1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07006239 if (wpas_p2p_join(wpa_s, iface_addr, dev_addr, wps_method,
Dmitry Shmidtde47be72016-01-07 12:52:55 -08006240 auto_join, freq,
6241 group_ssid, group_ssid_len) < 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006242 return -1;
6243 return ret;
6244 }
6245
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006246 size = P2P_MAX_PREF_CHANNELS;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07006247 res = wpas_p2p_setup_freqs(wpa_s, freq, &force_freq, &pref_freq,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006248 go_intent == 15, pref_freq_list, &size);
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08006249 if (res)
6250 return res;
Dmitry Shmidta0d265f2013-11-19 13:13:41 -08006251 wpas_p2p_set_own_freq_preference(wpa_s,
6252 force_freq ? force_freq : pref_freq);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006253
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006254 p2p_set_own_pref_freq_list(wpa_s->global->p2p, pref_freq_list, size);
6255
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006256 wpa_s->create_p2p_iface = wpas_p2p_create_iface(wpa_s);
6257
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006258 if (wpa_s->create_p2p_iface) {
6259 /* Prepare to add a new interface for the group */
6260 iftype = WPA_IF_P2P_GROUP;
6261 if (go_intent == 15)
6262 iftype = WPA_IF_P2P_GO;
6263 if (wpas_p2p_add_group_interface(wpa_s, iftype) < 0) {
6264 wpa_printf(MSG_ERROR, "P2P: Failed to allocate a new "
6265 "interface for the group");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006266 return -1;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006267 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006268
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006269 if_addr = wpa_s->pending_interface_addr;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006270 } else {
Dmitry Shmidt9c175262016-03-03 10:20:07 -08006271 if (wpa_s->p2p_mgmt)
6272 if_addr = wpa_s->parent->own_addr;
6273 else
6274 if_addr = wpa_s->own_addr;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006275 os_memset(wpa_s->go_dev_addr, 0, ETH_ALEN);
6276 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006277
6278 if (auth) {
6279 if (wpas_p2p_auth_go_neg(wpa_s, peer_addr, wps_method,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006280 go_intent, if_addr,
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08006281 force_freq, persistent_group, ssid,
Sunil Ravic0f5d412024-09-11 22:12:49 +00006282 pref_freq, bootstrap, password) < 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006283 return -1;
6284 return ret;
6285 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006286
6287 if (wpas_p2p_start_go_neg(wpa_s, peer_addr, wps_method,
6288 go_intent, if_addr, force_freq,
Sunil Ravic0f5d412024-09-11 22:12:49 +00006289 persistent_group, ssid, pref_freq, p2p2,
6290 bootstrap, password) < 0) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006291 if (wpa_s->create_p2p_iface)
6292 wpas_p2p_remove_pending_group_interface(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006293 return -1;
6294 }
6295 return ret;
6296}
6297
6298
6299/**
6300 * wpas_p2p_remain_on_channel_cb - Indication of remain-on-channel start
6301 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
6302 * @freq: Frequency of the channel in MHz
6303 * @duration: Duration of the stay on the channel in milliseconds
6304 *
6305 * This callback is called when the driver indicates that it has started the
6306 * requested remain-on-channel duration.
6307 */
6308void wpas_p2p_remain_on_channel_cb(struct wpa_supplicant *wpa_s,
6309 unsigned int freq, unsigned int duration)
6310{
6311 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
6312 return;
Dmitry Shmidt216983b2015-02-06 10:50:36 -08006313 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)",
6314 wpa_s->off_channel_freq, wpa_s->pending_listen_freq,
6315 wpa_s->roc_waiting_drv_freq, freq, duration);
6316 if (wpa_s->off_channel_freq &&
6317 wpa_s->off_channel_freq == wpa_s->pending_listen_freq) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006318 p2p_listen_cb(wpa_s->global->p2p, wpa_s->pending_listen_freq,
6319 wpa_s->pending_listen_duration);
6320 wpa_s->pending_listen_freq = 0;
Dmitry Shmidt7f93d6f2014-02-21 11:22:49 -08006321 } else {
6322 wpa_printf(MSG_DEBUG, "P2P: Ignore remain-on-channel callback (off_channel_freq=%u pending_listen_freq=%d freq=%u duration=%u)",
6323 wpa_s->off_channel_freq, wpa_s->pending_listen_freq,
6324 freq, duration);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006325 }
6326}
6327
6328
Jithu Jance57cea1a2014-08-20 10:22:04 -07006329int wpas_p2p_listen_start(struct wpa_supplicant *wpa_s, unsigned int timeout)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006330{
6331 /* Limit maximum Listen state time based on driver limitation. */
6332 if (timeout > wpa_s->max_remain_on_chan)
6333 timeout = wpa_s->max_remain_on_chan;
6334
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006335 return p2p_listen(wpa_s->global->p2p, timeout);
6336}
6337
6338
6339/**
6340 * wpas_p2p_cancel_remain_on_channel_cb - Remain-on-channel timeout
6341 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
6342 * @freq: Frequency of the channel in MHz
6343 *
6344 * This callback is called when the driver indicates that a remain-on-channel
6345 * operation has been completed, i.e., the duration on the requested channel
6346 * has timed out.
6347 */
6348void wpas_p2p_cancel_remain_on_channel_cb(struct wpa_supplicant *wpa_s,
6349 unsigned int freq)
6350{
6351 wpa_printf(MSG_DEBUG, "P2P: Cancel remain-on-channel callback "
6352 "(p2p_long_listen=%d ms pending_action_tx=%p)",
Hai Shalomfdcde762020-04-02 11:19:20 -07006353 wpa_s->global->p2p_long_listen,
6354 offchannel_pending_action_tx(wpa_s));
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08006355 wpas_p2p_listen_work_done(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006356 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
6357 return;
Hai Shalomfdcde762020-04-02 11:19:20 -07006358 if (wpa_s->global->p2p_long_listen > 0)
6359 wpa_s->global->p2p_long_listen -= wpa_s->max_remain_on_chan;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006360 if (p2p_listen_end(wpa_s->global->p2p, freq) > 0)
6361 return; /* P2P module started a new operation */
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08006362 if (offchannel_pending_action_tx(wpa_s))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006363 return;
Hai Shalomfdcde762020-04-02 11:19:20 -07006364 if (wpa_s->global->p2p_long_listen > 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006365 wpa_printf(MSG_DEBUG, "P2P: Continuing long Listen state");
Hai Shalomfdcde762020-04-02 11:19:20 -07006366 wpas_p2p_listen_start(wpa_s, wpa_s->global->p2p_long_listen);
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07006367 } else {
6368 /*
6369 * When listen duration is over, stop listen & update p2p_state
6370 * to IDLE.
6371 */
6372 p2p_stop_listen(wpa_s->global->p2p);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006373 }
6374}
6375
6376
6377/**
6378 * wpas_p2p_group_remove - Remove a P2P group
6379 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
6380 * @ifname: Network interface name of the group interface or "*" to remove all
6381 * groups
6382 * Returns: 0 on success, -1 on failure
6383 *
6384 * This function is used to remove a P2P group. This can be used to disconnect
6385 * from a group in which the local end is a P2P Client or to end a P2P Group in
6386 * case the local end is the Group Owner. If a virtual network interface was
6387 * created for this group, that interface will be removed. Otherwise, only the
6388 * configured P2P group network will be removed from the interface.
6389 */
6390int wpas_p2p_group_remove(struct wpa_supplicant *wpa_s, const char *ifname)
6391{
6392 struct wpa_global *global = wpa_s->global;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006393 struct wpa_supplicant *calling_wpa_s = wpa_s;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006394
6395 if (os_strcmp(ifname, "*") == 0) {
6396 struct wpa_supplicant *prev;
Hai Shalom899fcc72020-10-19 14:38:18 -07006397 bool calling_wpa_s_group_removed = false;
6398
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006399 wpa_s = global->ifaces;
6400 while (wpa_s) {
6401 prev = wpa_s;
6402 wpa_s = wpa_s->next;
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07006403 if (prev->p2p_group_interface !=
6404 NOT_P2P_GROUP_INTERFACE ||
6405 (prev->current_ssid &&
Hai Shalom899fcc72020-10-19 14:38:18 -07006406 prev->current_ssid->p2p_group)) {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006407 wpas_p2p_disconnect_safely(prev, calling_wpa_s);
Hai Shalom899fcc72020-10-19 14:38:18 -07006408 if (prev == calling_wpa_s)
6409 calling_wpa_s_group_removed = true;
6410 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006411 }
Hai Shalom899fcc72020-10-19 14:38:18 -07006412
6413 if (!calling_wpa_s_group_removed &&
6414 (calling_wpa_s->p2p_group_interface !=
6415 NOT_P2P_GROUP_INTERFACE ||
6416 (calling_wpa_s->current_ssid &&
6417 calling_wpa_s->current_ssid->p2p_group))) {
6418 wpa_printf(MSG_DEBUG, "Remove calling_wpa_s P2P group");
6419 wpas_p2p_disconnect_safely(calling_wpa_s,
6420 calling_wpa_s);
6421 }
6422
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006423 return 0;
6424 }
6425
6426 for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
6427 if (os_strcmp(wpa_s->ifname, ifname) == 0)
6428 break;
6429 }
6430
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006431 return wpas_p2p_disconnect_safely(wpa_s, calling_wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006432}
6433
6434
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07006435static int wpas_p2p_select_go_freq(struct wpa_supplicant *wpa_s, int freq)
6436{
6437 unsigned int r;
6438
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006439 if (!wpa_s->conf->num_p2p_pref_chan && !freq) {
6440 unsigned int i, size = P2P_MAX_PREF_CHANNELS;
Sunil8cd6f4d2022-06-28 18:40:46 +00006441 struct weighted_pcl pref_freq_list[P2P_MAX_PREF_CHANNELS];
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006442 int res;
6443
6444 res = wpa_drv_get_pref_freq_list(wpa_s, WPA_IF_P2P_GO,
6445 &size, pref_freq_list);
Sunil Ravi2a14cf12023-11-21 00:54:38 +00006446 if (!res && size > 0 && !is_p2p_allow_6ghz(wpa_s->global->p2p))
Hai Shalomc1a21442022-02-04 13:43:00 -08006447 size = p2p_remove_6ghz_channels(pref_freq_list, size);
6448
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006449 if (!res && size > 0) {
6450 i = 0;
Dmitry Shmidt29333592017-01-09 12:27:11 -08006451 while (i < size &&
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07006452 (!p2p_supported_freq(wpa_s->global->p2p,
Sunil8cd6f4d2022-06-28 18:40:46 +00006453 pref_freq_list[i].freq) ||
6454 wpas_p2p_disallowed_freq(
6455 wpa_s->global,
6456 pref_freq_list[i].freq) ||
6457 !p2p_pref_freq_allowed(&pref_freq_list[i],
6458 true))) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006459 wpa_printf(MSG_DEBUG,
6460 "P2P: preferred_freq_list[%d]=%d is disallowed",
Sunil8cd6f4d2022-06-28 18:40:46 +00006461 i, pref_freq_list[i].freq);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006462 i++;
6463 }
6464 if (i != size) {
Sunil8cd6f4d2022-06-28 18:40:46 +00006465 freq = pref_freq_list[i].freq;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006466 wpa_printf(MSG_DEBUG,
6467 "P2P: Using preferred_freq_list[%d]=%d",
6468 i, freq);
6469 } else {
6470 wpa_printf(MSG_DEBUG,
6471 "P2P: All driver preferred frequencies are disallowed for P2P use");
6472 }
6473 } else {
6474 wpa_printf(MSG_DEBUG,
6475 "P2P: No preferred frequency list available");
6476 }
6477 }
6478
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07006479 if (freq == 2) {
6480 wpa_printf(MSG_DEBUG, "P2P: Request to start GO on 2.4 GHz "
6481 "band");
6482 if (wpa_s->best_24_freq > 0 &&
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07006483 p2p_supported_freq_go(wpa_s->global->p2p,
6484 wpa_s->best_24_freq)) {
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07006485 freq = wpa_s->best_24_freq;
6486 wpa_printf(MSG_DEBUG, "P2P: Use best 2.4 GHz band "
6487 "channel: %d MHz", freq);
6488 } else {
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07006489 if (os_get_random((u8 *) &r, sizeof(r)) < 0)
6490 return -1;
Jimmy Chen801bf462021-11-09 23:08:48 +08006491 int possible_2g_freqs[] = {
6492 /* operating class 81 */
6493 2412, 2437, 2462,
6494 };
6495 int possible_2g_freqs_num =
6496 sizeof(possible_2g_freqs)/sizeof(possible_2g_freqs[0]);
6497 int i;
6498 for (i = 0; i < possible_2g_freqs_num; i++, r++) {
6499 freq = possible_2g_freqs[r % possible_2g_freqs_num];
6500 if (p2p_supported_freq_go(wpa_s->global->p2p, freq)) {
6501 break;
6502 }
6503 }
6504
6505 if (i >= possible_2g_freqs_num) {
6506 wpa_printf(MSG_DEBUG, "P2P: Could not select "
6507 "2.4 GHz channel for P2P group");
6508 return -1;
6509 }
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07006510 wpa_printf(MSG_DEBUG, "P2P: Use random 2.4 GHz band "
6511 "channel: %d MHz", freq);
6512 }
6513 }
6514
6515 if (freq == 5) {
6516 wpa_printf(MSG_DEBUG, "P2P: Request to start GO on 5 GHz "
6517 "band");
6518 if (wpa_s->best_5_freq > 0 &&
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07006519 p2p_supported_freq_go(wpa_s->global->p2p,
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07006520 wpa_s->best_5_freq)) {
6521 freq = wpa_s->best_5_freq;
6522 wpa_printf(MSG_DEBUG, "P2P: Use best 5 GHz band "
6523 "channel: %d MHz", freq);
6524 } else {
Hai Shalomc1a21442022-02-04 13:43:00 -08006525 const int freqs[] = {
6526 /* operating class 115 */
6527 5180, 5200, 5220, 5240,
6528 /* operating class 124 */
6529 5745, 5765, 5785, 5805,
6530 };
6531 unsigned int i, num_freqs = ARRAY_SIZE(freqs);
6532
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07006533 if (os_get_random((u8 *) &r, sizeof(r)) < 0)
6534 return -1;
Jimmy Chen64b90632020-09-29 17:27:34 +08006535
6536 /*
6537 * most of 5G channels are DFS, only operating class 115 and 124
6538 * are available possibly, randomly pick a start to check them.
6539 */
6540 int possible_5g_freqs[] = {
6541 /* operating class 115 */
6542 5180, 5200, 5220, 5240,
6543 /* operating class 124 */
6544 5745, 5765, 5785, 5805,
6545 };
6546 int possible_5g_freqs_num =
6547 sizeof(possible_5g_freqs)/sizeof(possible_5g_freqs[0]);
6548
Jimmy Chen64b90632020-09-29 17:27:34 +08006549 for (i = 0; i < possible_5g_freqs_num; i++, r++) {
6550 if (p2p_supported_freq_go(
6551 wpa_s->global->p2p,
6552 possible_5g_freqs[r % possible_5g_freqs_num])) {
6553 freq = possible_5g_freqs[r % possible_5g_freqs_num];
6554 break;
6555 }
6556 }
6557
6558 if (i >= possible_5g_freqs_num) {
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07006559 wpa_printf(MSG_DEBUG, "P2P: Could not select "
6560 "5 GHz channel for P2P group");
6561 return -1;
6562 }
6563 wpa_printf(MSG_DEBUG, "P2P: Use random 5 GHz band "
6564 "channel: %d MHz", freq);
6565 }
6566 }
6567
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07006568 if (freq > 0 && !p2p_supported_freq_go(wpa_s->global->p2p, freq)) {
Dmitry Shmidt203eadb2015-03-05 14:16:04 -08006569 if ((wpa_s->drv_flags & WPA_DRIVER_FLAGS_DFS_OFFLOAD) &&
Roshan Pius3a1667e2018-07-03 15:17:14 -07006570 ieee80211_is_dfs(freq, wpa_s->hw.modes,
6571 wpa_s->hw.num_modes)) {
Dmitry Shmidt203eadb2015-03-05 14:16:04 -08006572 /*
6573 * If freq is a DFS channel and DFS is offloaded to the
6574 * driver, allow P2P GO to use it.
6575 */
6576 wpa_printf(MSG_DEBUG, "P2P: "
6577 "%s: The forced channel for GO (%u MHz) is DFS, and DFS is offloaded",
6578 __func__, freq);
6579 return freq;
6580 }
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07006581 wpa_printf(MSG_DEBUG, "P2P: The forced channel for GO "
6582 "(%u MHz) is not supported for P2P uses",
6583 freq);
6584 return -1;
6585 }
6586
6587 return freq;
6588}
6589
6590
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006591static int wpas_p2p_supported_freq_go(struct wpa_supplicant *wpa_s,
6592 const struct p2p_channels *channels,
6593 int freq)
6594{
6595 if (!wpas_p2p_disallowed_freq(wpa_s->global, freq) &&
6596 p2p_supported_freq_go(wpa_s->global->p2p, freq) &&
6597 freq_included(wpa_s, channels, freq))
6598 return 1;
6599 return 0;
6600}
6601
6602
6603static void wpas_p2p_select_go_freq_no_pref(struct wpa_supplicant *wpa_s,
6604 struct p2p_go_neg_results *params,
6605 const struct p2p_channels *channels)
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07006606{
6607 unsigned int i, r;
6608
Dmitry Shmidt203eadb2015-03-05 14:16:04 -08006609 /* try all channels in operating class 115 */
6610 for (i = 0; i < 4; i++) {
6611 params->freq = 5180 + i * 20;
Hai Shalomc1a21442022-02-04 13:43:00 -08006612 if (wpas_p2p_supported_freq_go(wpa_s, channels, params->freq))
Dmitry Shmidt203eadb2015-03-05 14:16:04 -08006613 goto out;
6614 }
6615
6616 /* try all channels in operating class 124 */
6617 for (i = 0; i < 4; i++) {
6618 params->freq = 5745 + i * 20;
Hai Shalomc1a21442022-02-04 13:43:00 -08006619 if (wpas_p2p_supported_freq_go(wpa_s, channels, params->freq))
Dmitry Shmidt203eadb2015-03-05 14:16:04 -08006620 goto out;
6621 }
6622
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07006623 /* try social channel class 180 channel 2 */
6624 params->freq = 58320 + 1 * 2160;
Hai Shalomc1a21442022-02-04 13:43:00 -08006625 if (wpas_p2p_supported_freq_go(wpa_s, channels, params->freq))
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07006626 goto out;
6627
6628 /* try all channels in reg. class 180 */
6629 for (i = 0; i < 4; i++) {
6630 params->freq = 58320 + i * 2160;
Hai Shalomc1a21442022-02-04 13:43:00 -08006631 if (wpas_p2p_supported_freq_go(wpa_s, channels, params->freq))
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07006632 goto out;
6633 }
6634
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07006635 /* try some random selection of the social channels */
6636 if (os_get_random((u8 *) &r, sizeof(r)) < 0)
6637 return;
6638
6639 for (i = 0; i < 3; i++) {
6640 params->freq = 2412 + ((r + i) % 3) * 25;
6641 if (wpas_p2p_supported_freq_go(wpa_s, channels, params->freq))
6642 goto out;
6643 }
6644
6645 /* try all other channels in operating class 81 */
6646 for (i = 0; i < 11; i++) {
6647 params->freq = 2412 + i * 5;
6648
6649 /* skip social channels; covered in the previous loop */
6650 if (params->freq == 2412 ||
6651 params->freq == 2437 ||
6652 params->freq == 2462)
6653 continue;
6654
6655 if (wpas_p2p_supported_freq_go(wpa_s, channels, params->freq))
6656 goto out;
6657 }
6658
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006659 params->freq = 0;
Dmitry Shmidt203eadb2015-03-05 14:16:04 -08006660 wpa_printf(MSG_DEBUG, "P2P: No 2.4, 5, or 60 GHz channel allowed");
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006661 return;
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07006662out:
6663 wpa_printf(MSG_DEBUG, "P2P: Set GO freq %d MHz (no preference known)",
6664 params->freq);
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07006665}
6666
6667
Roshan Pius3a1667e2018-07-03 15:17:14 -07006668static int wpas_same_band(int freq1, int freq2)
6669{
6670 enum hostapd_hw_mode mode1, mode2;
6671 u8 chan1, chan2;
6672
6673 mode1 = ieee80211_freq_to_chan(freq1, &chan1);
6674 mode2 = ieee80211_freq_to_chan(freq2, &chan2);
6675 if (mode1 == NUM_HOSTAPD_MODES)
6676 return 0;
6677 return mode1 == mode2;
6678}
6679
6680
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006681static int wpas_p2p_init_go_params(struct wpa_supplicant *wpa_s,
6682 struct p2p_go_neg_results *params,
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08006683 int freq, int vht_center_freq2, int ht40,
Hai Shalom74f70d42019-02-11 14:42:39 -08006684 int vht, int max_oper_chwidth, int he,
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08006685 int edmg,
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08006686 const struct p2p_channels *channels)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006687{
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07006688 struct wpa_used_freq_data *freqs;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006689 unsigned int cand;
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07006690 unsigned int num, i;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006691 int ignore_no_freqs = 0;
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08006692 int unused_channels = wpas_p2p_num_unused_channels(wpa_s) > 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006693
6694 os_memset(params, 0, sizeof(*params));
6695 params->role_go = 1;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07006696 params->ht40 = ht40;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07006697 params->vht = vht;
Hai Shalom74f70d42019-02-11 14:42:39 -08006698 params->he = he;
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08006699 params->max_oper_chwidth = max_oper_chwidth;
6700 params->vht_center_freq2 = vht_center_freq2;
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08006701 params->edmg = edmg;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006702
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07006703 freqs = os_calloc(wpa_s->num_multichan_concurrent,
6704 sizeof(struct wpa_used_freq_data));
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07006705 if (!freqs)
6706 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006707
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006708 num = get_shared_radio_freqs_data(wpa_s, freqs,
Sunil Ravi77d572f2023-01-17 23:58:31 +00006709 wpa_s->num_multichan_concurrent,
6710 false);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006711
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006712 if (wpa_s->current_ssid &&
6713 wpa_s->current_ssid->mode == WPAS_MODE_P2P_GO &&
6714 wpa_s->wpa_state == WPA_COMPLETED) {
6715 wpa_printf(MSG_DEBUG, "P2P: %s called for an active GO",
6716 __func__);
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07006717
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006718 /*
6719 * If the frequency selection is done for an active P2P GO that
6720 * is not sharing a frequency, allow to select a new frequency
6721 * even if there are no unused frequencies as we are about to
6722 * move the P2P GO so its frequency can be re-used.
6723 */
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07006724 for (i = 0; i < num; i++) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006725 if (freqs[i].freq == wpa_s->current_ssid->frequency &&
6726 freqs[i].flags == 0) {
6727 ignore_no_freqs = 1;
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07006728 break;
6729 }
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07006730 }
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08006731 }
6732
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08006733 /* Try to use EDMG channel */
6734 if (params->edmg) {
6735 if (wpas_p2p_try_edmg_channel(wpa_s, params) == 0)
6736 goto success;
6737 params->edmg = 0;
6738 }
6739
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006740 /* try using the forced freq */
6741 if (freq) {
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07006742 if (wpas_p2p_disallowed_freq(wpa_s->global, freq) ||
6743 !freq_included(wpa_s, channels, freq)) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006744 wpa_printf(MSG_DEBUG,
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07006745 "P2P: Forced GO freq %d MHz disallowed",
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006746 freq);
6747 goto fail;
6748 }
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07006749 if (!p2p_supported_freq_go(wpa_s->global->p2p, freq)) {
6750 if ((wpa_s->drv_flags & WPA_DRIVER_FLAGS_DFS_OFFLOAD) &&
Roshan Pius3a1667e2018-07-03 15:17:14 -07006751 ieee80211_is_dfs(freq, wpa_s->hw.modes,
6752 wpa_s->hw.num_modes)) {
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07006753 /*
6754 * If freq is a DFS channel and DFS is offloaded
6755 * to the driver, allow P2P GO to use it.
6756 */
6757 wpa_printf(MSG_DEBUG,
6758 "P2P: %s: The forced channel for GO (%u MHz) requires DFS and DFS is offloaded",
6759 __func__, freq);
6760 } else {
6761 wpa_printf(MSG_DEBUG,
6762 "P2P: The forced channel for GO (%u MHz) is not supported for P2P uses",
6763 freq);
6764 goto fail;
6765 }
6766 }
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006767
6768 for (i = 0; i < num; i++) {
6769 if (freqs[i].freq == freq) {
6770 wpa_printf(MSG_DEBUG,
6771 "P2P: forced freq (%d MHz) is also shared",
6772 freq);
6773 params->freq = freq;
6774 goto success;
6775 }
6776 }
6777
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08006778 if (!ignore_no_freqs && !unused_channels) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006779 wpa_printf(MSG_DEBUG,
6780 "P2P: Cannot force GO on freq (%d MHz) as all the channels are in use",
6781 freq);
6782 goto fail;
6783 }
6784
6785 wpa_printf(MSG_DEBUG,
6786 "P2P: force GO freq (%d MHz) on a free channel",
6787 freq);
6788 params->freq = freq;
6789 goto success;
6790 }
6791
6792 /* consider using one of the shared frequencies */
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08006793 if (num &&
6794 (!wpa_s->conf->p2p_ignore_shared_freq || !unused_channels)) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006795 cand = wpas_p2p_pick_best_used_freq(wpa_s, freqs, num);
6796 if (wpas_p2p_supported_freq_go(wpa_s, channels, cand)) {
6797 wpa_printf(MSG_DEBUG,
6798 "P2P: Use shared freq (%d MHz) for GO",
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08006799 cand);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006800 params->freq = cand;
6801 goto success;
6802 }
6803
6804 /* try using one of the shared freqs */
6805 for (i = 0; i < num; i++) {
6806 if (wpas_p2p_supported_freq_go(wpa_s, channels,
6807 freqs[i].freq)) {
6808 wpa_printf(MSG_DEBUG,
6809 "P2P: Use shared freq (%d MHz) for GO",
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08006810 freqs[i].freq);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006811 params->freq = freqs[i].freq;
6812 goto success;
6813 }
6814 }
6815 }
6816
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08006817 if (!ignore_no_freqs && !unused_channels) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006818 wpa_printf(MSG_DEBUG,
6819 "P2P: Cannot force GO on any of the channels we are already using");
6820 goto fail;
6821 }
6822
6823 /* try using the setting from the configuration file */
6824 if (wpa_s->conf->p2p_oper_reg_class == 81 &&
6825 wpa_s->conf->p2p_oper_channel >= 1 &&
6826 wpa_s->conf->p2p_oper_channel <= 11 &&
6827 wpas_p2p_supported_freq_go(
6828 wpa_s, channels,
6829 2407 + 5 * wpa_s->conf->p2p_oper_channel)) {
6830 params->freq = 2407 + 5 * wpa_s->conf->p2p_oper_channel;
6831 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on configured "
6832 "frequency %d MHz", params->freq);
6833 goto success;
6834 }
6835
6836 if ((wpa_s->conf->p2p_oper_reg_class == 115 ||
6837 wpa_s->conf->p2p_oper_reg_class == 116 ||
6838 wpa_s->conf->p2p_oper_reg_class == 117 ||
6839 wpa_s->conf->p2p_oper_reg_class == 124 ||
6840 wpa_s->conf->p2p_oper_reg_class == 125 ||
6841 wpa_s->conf->p2p_oper_reg_class == 126 ||
6842 wpa_s->conf->p2p_oper_reg_class == 127) &&
6843 wpas_p2p_supported_freq_go(wpa_s, channels,
6844 5000 +
6845 5 * wpa_s->conf->p2p_oper_channel)) {
6846 params->freq = 5000 + 5 * wpa_s->conf->p2p_oper_channel;
6847 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on configured "
6848 "frequency %d MHz", params->freq);
6849 goto success;
6850 }
6851
6852 /* Try using best channels */
6853 if (wpa_s->conf->p2p_oper_channel == 0 &&
6854 wpa_s->best_overall_freq > 0 &&
6855 wpas_p2p_supported_freq_go(wpa_s, channels,
6856 wpa_s->best_overall_freq)) {
6857 params->freq = wpa_s->best_overall_freq;
6858 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on best overall "
6859 "channel %d MHz", params->freq);
6860 goto success;
6861 }
6862
6863 if (wpa_s->conf->p2p_oper_channel == 0 &&
6864 wpa_s->best_24_freq > 0 &&
6865 wpas_p2p_supported_freq_go(wpa_s, channels,
6866 wpa_s->best_24_freq)) {
6867 params->freq = wpa_s->best_24_freq;
6868 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on best 2.4 GHz "
6869 "channel %d MHz", params->freq);
6870 goto success;
6871 }
6872
6873 if (wpa_s->conf->p2p_oper_channel == 0 &&
6874 wpa_s->best_5_freq > 0 &&
6875 wpas_p2p_supported_freq_go(wpa_s, channels,
6876 wpa_s->best_5_freq)) {
6877 params->freq = wpa_s->best_5_freq;
6878 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on best 5 GHz "
6879 "channel %d MHz", params->freq);
6880 goto success;
6881 }
6882
6883 /* try using preferred channels */
6884 cand = p2p_get_pref_freq(wpa_s->global->p2p, channels);
6885 if (cand && wpas_p2p_supported_freq_go(wpa_s, channels, cand)) {
6886 params->freq = cand;
6887 wpa_printf(MSG_DEBUG, "P2P: Set GO freq %d MHz from preferred "
6888 "channels", params->freq);
6889 goto success;
6890 }
6891
Roshan Pius3a1667e2018-07-03 15:17:14 -07006892 /* Try using a channel that allows VHT to be used with 80 MHz */
6893 if (wpa_s->hw.modes && wpa_s->p2p_group_common_freqs) {
6894 for (i = 0; i < wpa_s->p2p_group_common_freqs_num; i++) {
6895 enum hostapd_hw_mode mode;
6896 struct hostapd_hw_modes *hwmode;
6897 u8 chan;
Hai Shalomc1a21442022-02-04 13:43:00 -08006898 u8 op_class;
Roshan Pius3a1667e2018-07-03 15:17:14 -07006899
6900 cand = wpa_s->p2p_group_common_freqs[i];
Hai Shalomc1a21442022-02-04 13:43:00 -08006901 op_class = is_6ghz_freq(cand) ? 133 : 128;
Roshan Pius3a1667e2018-07-03 15:17:14 -07006902 mode = ieee80211_freq_to_chan(cand, &chan);
6903 hwmode = get_mode(wpa_s->hw.modes, wpa_s->hw.num_modes,
Hai Shalomfdcde762020-04-02 11:19:20 -07006904 mode, is_6ghz_freq(cand));
Roshan Pius3a1667e2018-07-03 15:17:14 -07006905 if (!hwmode ||
Hai Shalomc1a21442022-02-04 13:43:00 -08006906 wpas_p2p_verify_channel(wpa_s, hwmode, op_class,
6907 chan, BW80) != ALLOWED)
Roshan Pius3a1667e2018-07-03 15:17:14 -07006908 continue;
6909 if (wpas_p2p_supported_freq_go(wpa_s, channels, cand)) {
6910 params->freq = cand;
6911 wpa_printf(MSG_DEBUG,
6912 "P2P: Use freq %d MHz common with the peer and allowing VHT80",
6913 params->freq);
6914 goto success;
6915 }
6916 }
6917 }
6918
6919 /* Try using a channel that allows HT to be used with 40 MHz on the same
6920 * band so that CSA can be used */
6921 if (wpa_s->current_ssid && wpa_s->hw.modes &&
6922 wpa_s->p2p_group_common_freqs) {
6923 for (i = 0; i < wpa_s->p2p_group_common_freqs_num; i++) {
6924 enum hostapd_hw_mode mode;
6925 struct hostapd_hw_modes *hwmode;
Hai Shalomc1a21442022-02-04 13:43:00 -08006926 u8 chan, op_class;
6927 bool is_6ghz, supported = false;
Roshan Pius3a1667e2018-07-03 15:17:14 -07006928
Hai Shalomc1a21442022-02-04 13:43:00 -08006929 is_6ghz = is_6ghz_freq(cand);
Roshan Pius3a1667e2018-07-03 15:17:14 -07006930 cand = wpa_s->p2p_group_common_freqs[i];
6931 mode = ieee80211_freq_to_chan(cand, &chan);
6932 hwmode = get_mode(wpa_s->hw.modes, wpa_s->hw.num_modes,
Hai Shalomc1a21442022-02-04 13:43:00 -08006933 mode, is_6ghz);
Roshan Pius3a1667e2018-07-03 15:17:14 -07006934 if (!wpas_same_band(wpa_s->current_ssid->frequency,
6935 cand) ||
Hai Shalomc1a21442022-02-04 13:43:00 -08006936 !hwmode)
Roshan Pius3a1667e2018-07-03 15:17:14 -07006937 continue;
Hai Shalomc1a21442022-02-04 13:43:00 -08006938 if (is_6ghz &&
6939 wpas_p2p_verify_channel(wpa_s, hwmode, 132, chan,
6940 BW40) == ALLOWED)
6941 supported = true;
6942
6943 if (!is_6ghz &&
6944 ieee80211_freq_to_channel_ext(
Sunil8cd6f4d2022-06-28 18:40:46 +00006945 cand, -1, CONF_OPER_CHWIDTH_USE_HT,
6946 &op_class, &chan) != NUM_HOSTAPD_MODES &&
Hai Shalomc1a21442022-02-04 13:43:00 -08006947 wpas_p2p_verify_channel(
6948 wpa_s, hwmode, op_class, chan,
6949 BW40MINUS) == ALLOWED)
6950 supported = true;
6951
6952 if (!supported && !is_6ghz &&
6953 ieee80211_freq_to_channel_ext(
Sunil8cd6f4d2022-06-28 18:40:46 +00006954 cand, 1, CONF_OPER_CHWIDTH_USE_HT,
6955 &op_class, &chan) != NUM_HOSTAPD_MODES &&
Hai Shalomc1a21442022-02-04 13:43:00 -08006956 wpas_p2p_verify_channel(
6957 wpa_s, hwmode, op_class, chan,
6958 BW40PLUS) == ALLOWED)
6959 supported = true;
6960
6961 if (!supported)
6962 continue;
6963
Roshan Pius3a1667e2018-07-03 15:17:14 -07006964 if (wpas_p2p_supported_freq_go(wpa_s, channels, cand)) {
6965 params->freq = cand;
6966 wpa_printf(MSG_DEBUG,
6967 "P2P: Use freq %d MHz common with the peer, allowing HT40, and maintaining same band",
6968 params->freq);
6969 goto success;
6970 }
6971 }
6972 }
6973
6974 /* Try using one of the group common freqs on the same band so that CSA
6975 * can be used */
6976 if (wpa_s->current_ssid && wpa_s->p2p_group_common_freqs) {
6977 for (i = 0; i < wpa_s->p2p_group_common_freqs_num; i++) {
6978 cand = wpa_s->p2p_group_common_freqs[i];
6979 if (!wpas_same_band(wpa_s->current_ssid->frequency,
6980 cand))
6981 continue;
6982 if (wpas_p2p_supported_freq_go(wpa_s, channels, cand)) {
6983 params->freq = cand;
6984 wpa_printf(MSG_DEBUG,
6985 "P2P: Use freq %d MHz common with the peer and maintaining same band",
6986 params->freq);
6987 goto success;
6988 }
6989 }
6990 }
6991
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006992 /* Try using one of the group common freqs */
6993 if (wpa_s->p2p_group_common_freqs) {
6994 for (i = 0; i < wpa_s->p2p_group_common_freqs_num; i++) {
6995 cand = wpa_s->p2p_group_common_freqs[i];
6996 if (wpas_p2p_supported_freq_go(wpa_s, channels, cand)) {
6997 params->freq = cand;
6998 wpa_printf(MSG_DEBUG,
6999 "P2P: Use freq %d MHz common with the peer",
7000 params->freq);
7001 goto success;
7002 }
7003 }
7004 }
7005
7006 /* no preference, select some channel */
7007 wpas_p2p_select_go_freq_no_pref(wpa_s, params, channels);
7008
7009 if (params->freq == 0) {
7010 wpa_printf(MSG_DEBUG, "P2P: did not find a freq for GO use");
7011 goto fail;
7012 }
7013
7014success:
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07007015 os_free(freqs);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007016 return 0;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08007017fail:
7018 os_free(freqs);
7019 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007020}
7021
7022
7023static struct wpa_supplicant *
7024wpas_p2p_get_group_iface(struct wpa_supplicant *wpa_s, int addr_allocated,
7025 int go)
7026{
7027 struct wpa_supplicant *group_wpa_s;
7028
Dmitry Shmidtaa532512012-09-24 10:35:31 -07007029 if (!wpas_p2p_create_iface(wpa_s)) {
Dmitry Shmidt9c175262016-03-03 10:20:07 -08007030 if (wpa_s->p2p_mgmt) {
7031 /*
7032 * We may be called on the p2p_dev interface which
7033 * cannot be used for group operations, so always use
7034 * the primary interface.
7035 */
7036 wpa_s->parent->p2pdev = wpa_s;
7037 wpa_s = wpa_s->parent;
7038 }
7039 wpa_dbg(wpa_s, MSG_DEBUG,
7040 "P2P: Use primary interface for group operations");
Dmitry Shmidt56052862013-10-04 10:23:25 -07007041 wpa_s->p2p_first_connection_timeout = 0;
Dmitry Shmidt9c175262016-03-03 10:20:07 -08007042 if (wpa_s != wpa_s->p2pdev)
Dmitry Shmidt849734c2016-05-27 09:59:01 -07007043 wpas_p2p_clone_config(wpa_s, wpa_s->p2pdev);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007044 return wpa_s;
Dmitry Shmidtaa532512012-09-24 10:35:31 -07007045 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007046
7047 if (wpas_p2p_add_group_interface(wpa_s, go ? WPA_IF_P2P_GO :
Dmitry Shmidtaa532512012-09-24 10:35:31 -07007048 WPA_IF_P2P_CLIENT) < 0) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07007049 wpa_msg_global(wpa_s, MSG_ERROR,
7050 "P2P: Failed to add group interface");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007051 return NULL;
Dmitry Shmidtaa532512012-09-24 10:35:31 -07007052 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007053 group_wpa_s = wpas_p2p_init_group_interface(wpa_s, go);
7054 if (group_wpa_s == NULL) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07007055 wpa_msg_global(wpa_s, MSG_ERROR,
7056 "P2P: Failed to initialize group interface");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007057 wpas_p2p_remove_pending_group_interface(wpa_s);
7058 return NULL;
7059 }
7060
Roshan Pius3a1667e2018-07-03 15:17:14 -07007061 if (go && wpa_s->p2p_go_do_acs) {
7062 group_wpa_s->p2p_go_do_acs = wpa_s->p2p_go_do_acs;
7063 group_wpa_s->p2p_go_acs_band = wpa_s->p2p_go_acs_band;
7064 wpa_s->p2p_go_do_acs = 0;
7065 }
7066
Hai Shalomc1a21442022-02-04 13:43:00 -08007067 if (go && wpa_s->p2p_go_allow_dfs) {
7068 group_wpa_s->p2p_go_allow_dfs = wpa_s->p2p_go_allow_dfs;
7069 wpa_s->p2p_go_allow_dfs = 0;
7070 }
7071
Dmitry Shmidtaa532512012-09-24 10:35:31 -07007072 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Use separate group interface %s",
7073 group_wpa_s->ifname);
Dmitry Shmidt56052862013-10-04 10:23:25 -07007074 group_wpa_s->p2p_first_connection_timeout = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007075 return group_wpa_s;
7076}
7077
7078
7079/**
7080 * wpas_p2p_group_add - Add a new P2P group with local end as Group Owner
7081 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
7082 * @persistent_group: Whether to create a persistent group
7083 * @freq: Frequency for the group or 0 to indicate no hardcoding
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08007084 * @vht_center_freq2: segment_1 center frequency for GO operating in VHT 80P80
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07007085 * @ht40: Start GO with 40 MHz channel width
7086 * @vht: Start GO with VHT support
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08007087 * @vht_chwidth: channel bandwidth for GO operating with VHT support
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08007088 * @edmg: Start GO with EDMG support
Hai Shalomc1a21442022-02-04 13:43:00 -08007089 * @allow_6ghz: Allow P2P group creation on a 6 GHz channel
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007090 * Returns: 0 on success, -1 on failure
7091 *
7092 * This function creates a new P2P group with the local end as the Group Owner,
7093 * i.e., without using Group Owner Negotiation.
7094 */
7095int wpas_p2p_group_add(struct wpa_supplicant *wpa_s, int persistent_group,
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08007096 int freq, int vht_center_freq2, int ht40, int vht,
Hai Shalomc1a21442022-02-04 13:43:00 -08007097 int max_oper_chwidth, int he, int edmg,
7098 bool allow_6ghz)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007099{
7100 struct p2p_go_neg_results params;
Sunil Ravi77a0f092022-10-03 00:53:41 +00007101 int selected_freq = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007102
7103 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
7104 return -1;
Hai Shalomc1a21442022-02-04 13:43:00 -08007105 if (wpas_p2p_check_6ghz(wpa_s, NULL, allow_6ghz, freq))
7106 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007107
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07007108 os_free(wpa_s->global->add_psk);
7109 wpa_s->global->add_psk = NULL;
7110
Dmitry Shmidtdca39792011-09-06 11:17:33 -07007111 /* Make sure we are not running find during connection establishment */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007112 wpa_printf(MSG_DEBUG, "P2P: Stop any on-going P2P FIND");
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08007113 wpas_p2p_stop_find_oper(wpa_s);
Dmitry Shmidtdca39792011-09-06 11:17:33 -07007114
Roshan Pius3a1667e2018-07-03 15:17:14 -07007115 if (!wpa_s->p2p_go_do_acs) {
Sunil Ravi77a0f092022-10-03 00:53:41 +00007116 selected_freq = wpas_p2p_select_go_freq(wpa_s, freq);
7117 if (selected_freq < 0)
Roshan Pius3a1667e2018-07-03 15:17:14 -07007118 return -1;
7119 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007120
Sunil Ravi99c035e2024-07-12 01:42:03 +00007121 if (wpas_p2p_init_go_params(wpa_s, &params, selected_freq,
7122 vht_center_freq2,
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08007123 ht40, vht, max_oper_chwidth, he, edmg,
7124 NULL))
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007125 return -1;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07007126
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007127 p2p_go_params(wpa_s->global->p2p, &params);
7128 params.persistent_group = persistent_group;
7129
7130 wpa_s = wpas_p2p_get_group_iface(wpa_s, 0, 1);
7131 if (wpa_s == NULL)
7132 return -1;
Sunil Ravi77a0f092022-10-03 00:53:41 +00007133 if (freq > 0)
7134 wpa_s->p2p_go_no_pri_sec_switch = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007135 wpas_start_wps_go(wpa_s, &params, 0);
7136
7137 return 0;
7138}
7139
7140
7141static int wpas_start_p2p_client(struct wpa_supplicant *wpa_s,
Dmitry Shmidt15907092014-03-25 10:42:57 -07007142 struct wpa_ssid *params, int addr_allocated,
Matthew Wang124e5f42022-12-29 07:23:06 +00007143 int freq, int force_scan, int retry_limit,
Sunil Ravi036cec52023-03-29 11:35:17 -07007144 const u8 *go_bssid)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007145{
7146 struct wpa_ssid *ssid;
Matthew Wang9ae940b2022-09-14 21:25:34 -07007147 int other_iface_found = 0;
7148 struct wpa_supplicant *ifs;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007149
7150 wpa_s = wpas_p2p_get_group_iface(wpa_s, addr_allocated, 0);
7151 if (wpa_s == NULL)
7152 return -1;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08007153 if (force_scan)
7154 os_get_reltime(&wpa_s->scan_min_time);
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07007155 wpa_s->p2p_last_4way_hs_fail = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007156
7157 wpa_supplicant_ap_deinit(wpa_s);
7158
7159 ssid = wpa_config_add_network(wpa_s->conf);
7160 if (ssid == NULL)
7161 return -1;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08007162 os_memset(wpa_s->go_dev_addr, 0, ETH_ALEN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007163 wpa_config_set_network_defaults(ssid);
7164 ssid->temporary = 1;
7165 ssid->proto = WPA_PROTO_RSN;
Dmitry Shmidte4663042016-04-04 10:07:49 -07007166 ssid->pbss = params->pbss;
7167 ssid->pairwise_cipher = params->pbss ? WPA_CIPHER_GCMP :
7168 WPA_CIPHER_CCMP;
7169 ssid->group_cipher = params->pbss ? WPA_CIPHER_GCMP : WPA_CIPHER_CCMP;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007170 ssid->key_mgmt = WPA_KEY_MGMT_PSK;
Sunil Ravi036cec52023-03-29 11:35:17 -07007171 if (is_6ghz_freq(freq) &&
7172 is_p2p_6ghz_capable(wpa_s->global->p2p)) {
7173 ssid->auth_alg |= WPA_AUTH_ALG_SAE;
7174 ssid->key_mgmt |= WPA_KEY_MGMT_SAE;
7175 ssid->ieee80211w = MGMT_FRAME_PROTECTION_OPTIONAL;
7176 ssid->sae_pwe = SAE_PWE_HASH_TO_ELEMENT;
7177 wpa_dbg(wpa_s, MSG_DEBUG,
7178 "P2P: Enable SAE auth_alg and key_mgmt");
7179 }
7180
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007181 ssid->ssid = os_malloc(params->ssid_len);
7182 if (ssid->ssid == NULL) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007183 wpa_config_remove_network(wpa_s->conf, ssid->id);
7184 return -1;
7185 }
7186 os_memcpy(ssid->ssid, params->ssid, params->ssid_len);
7187 ssid->ssid_len = params->ssid_len;
7188 ssid->p2p_group = 1;
7189 ssid->export_keys = 1;
7190 if (params->psk_set) {
7191 os_memcpy(ssid->psk, params->psk, 32);
7192 ssid->psk_set = 1;
7193 }
7194 if (params->passphrase)
7195 ssid->passphrase = os_strdup(params->passphrase);
7196
Sunil Ravi036cec52023-03-29 11:35:17 -07007197 if (go_bssid) {
Matthew Wangdcf19452022-11-07 20:42:52 -08007198 ssid->bssid_set = 1;
Sunil Ravi036cec52023-03-29 11:35:17 -07007199 os_memcpy(ssid->bssid, go_bssid, ETH_ALEN);
Matthew Wangdcf19452022-11-07 20:42:52 -08007200 }
7201
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007202 wpa_s->show_group_started = 1;
Dmitry Shmidt15907092014-03-25 10:42:57 -07007203 wpa_s->p2p_in_invitation = 1;
Matthew Wang06b42472022-11-10 06:56:31 +00007204 wpa_s->p2p_retry_limit = retry_limit;
Dmitry Shmidt15907092014-03-25 10:42:57 -07007205 wpa_s->p2p_invite_go_freq = freq;
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08007206 wpa_s->p2p_go_group_formation_completed = 0;
7207 wpa_s->global->p2p_group_formation = wpa_s;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007208
Matthew Wang9ae940b2022-09-14 21:25:34 -07007209 /*
7210 * Get latest scan results from driver in case cached scan results from
7211 * interfaces on the same wiphy allow us to skip the next scan by fast
7212 * associating. Also update the scan time to the most recent scan result
7213 * fetch time on the same radio so it reflects the actual time the last
7214 * scan result event occurred.
7215 */
Sunil Ravi99c035e2024-07-12 01:42:03 +00007216 wpa_supplicant_update_scan_results(wpa_s, go_bssid);
Matthew Wang9ae940b2022-09-14 21:25:34 -07007217 dl_list_for_each(ifs, &wpa_s->radio->ifaces, struct wpa_supplicant,
7218 radio_list) {
7219 if (ifs == wpa_s)
7220 continue;
7221 if (!other_iface_found || os_reltime_before(&wpa_s->last_scan,
7222 &ifs->last_scan)) {
7223 other_iface_found = 1;
7224 wpa_s->last_scan.sec = ifs->last_scan.sec;
7225 wpa_s->last_scan.usec = ifs->last_scan.usec;
7226 }
7227 }
7228
Dmitry Shmidt9c175262016-03-03 10:20:07 -08007229 eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s->p2pdev,
Dmitry Shmidt15907092014-03-25 10:42:57 -07007230 NULL);
7231 eloop_register_timeout(P2P_MAX_INITIAL_CONN_WAIT, 0,
7232 wpas_p2p_group_formation_timeout,
Dmitry Shmidt9c175262016-03-03 10:20:07 -08007233 wpa_s->p2pdev, NULL);
Dmitry Shmidtfa3fc4a2013-11-21 13:34:38 -08007234 wpa_supplicant_select_network(wpa_s, ssid);
7235
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007236 return 0;
7237}
7238
7239
7240int wpas_p2p_group_add_persistent(struct wpa_supplicant *wpa_s,
7241 struct wpa_ssid *ssid, int addr_allocated,
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08007242 int force_freq, int neg_freq,
7243 int vht_center_freq2, int ht40,
Hai Shalom74f70d42019-02-11 14:42:39 -08007244 int vht, int max_oper_chwidth, int he,
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08007245 int edmg,
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08007246 const struct p2p_channels *channels,
Hai Shalomc1a21442022-02-04 13:43:00 -08007247 int connection_timeout, int force_scan,
Matthew Wang124e5f42022-12-29 07:23:06 +00007248 bool allow_6ghz, int retry_limit,
Sunil Ravi036cec52023-03-29 11:35:17 -07007249 const u8 *go_bssid)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007250{
7251 struct p2p_go_neg_results params;
Dmitry Shmidt96be6222014-02-13 10:16:51 -08007252 int go = 0, freq;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007253
7254 if (ssid->disabled != 2 || ssid->ssid == NULL)
7255 return -1;
7256
7257 if (wpas_get_p2p_group(wpa_s, ssid->ssid, ssid->ssid_len, &go) &&
7258 go == (ssid->mode == WPAS_MODE_P2P_GO)) {
7259 wpa_printf(MSG_DEBUG, "P2P: Requested persistent group is "
7260 "already running");
Dmitry Shmidtb1e52102015-05-29 12:36:29 -07007261 if (go == 0 &&
7262 eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
Dmitry Shmidt9c175262016-03-03 10:20:07 -08007263 wpa_s->p2pdev, NULL)) {
Dmitry Shmidtb1e52102015-05-29 12:36:29 -07007264 /*
7265 * This can happen if Invitation Response frame was lost
7266 * and the peer (GO of a persistent group) tries to
7267 * invite us again. Reschedule the timeout to avoid
7268 * terminating the wait for the connection too early
7269 * since we now know that the peer is still trying to
7270 * invite us instead of having already started the GO.
7271 */
7272 wpa_printf(MSG_DEBUG,
7273 "P2P: Reschedule group formation timeout since peer is still trying to invite us");
7274 eloop_register_timeout(P2P_MAX_INITIAL_CONN_WAIT, 0,
7275 wpas_p2p_group_formation_timeout,
Dmitry Shmidt9c175262016-03-03 10:20:07 -08007276 wpa_s->p2pdev, NULL);
Dmitry Shmidtb1e52102015-05-29 12:36:29 -07007277 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007278 return 0;
7279 }
7280
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07007281 os_free(wpa_s->global->add_psk);
7282 wpa_s->global->add_psk = NULL;
7283
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007284 /* Make sure we are not running find during connection establishment */
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08007285 wpas_p2p_stop_find_oper(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007286
Dmitry Shmidt04949592012-07-19 12:16:46 -07007287 wpa_s->p2p_fallback_to_go_neg = 0;
7288
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007289 if (ssid->mode == WPAS_MODE_P2P_GO) {
7290 if (force_freq > 0) {
7291 freq = wpas_p2p_select_go_freq(wpa_s, force_freq);
7292 if (freq < 0)
7293 return -1;
Sunil Ravi77a0f092022-10-03 00:53:41 +00007294 wpa_s->p2p_go_no_pri_sec_switch = 1;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007295 } else {
7296 freq = wpas_p2p_select_go_freq(wpa_s, neg_freq);
7297 if (freq < 0 ||
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08007298 (freq > 0 && !freq_included(wpa_s, channels, freq)))
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007299 freq = 0;
7300 }
Dmitry Shmidt912c6ec2015-03-30 13:16:51 -07007301 } else if (ssid->mode == WPAS_MODE_INFRA) {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007302 freq = neg_freq;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08007303 if (freq <= 0 || !freq_included(wpa_s, channels, freq)) {
Dmitry Shmidt912c6ec2015-03-30 13:16:51 -07007304 struct os_reltime now;
7305 struct wpa_bss *bss =
7306 wpa_bss_get_p2p_dev_addr(wpa_s, ssid->bssid);
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07007307
Dmitry Shmidt912c6ec2015-03-30 13:16:51 -07007308 os_get_reltime(&now);
7309 if (bss &&
7310 !os_reltime_expired(&now, &bss->last_update, 5) &&
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08007311 freq_included(wpa_s, channels, bss->freq))
Dmitry Shmidt912c6ec2015-03-30 13:16:51 -07007312 freq = bss->freq;
7313 else
7314 freq = 0;
7315 }
7316
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08007317 return wpas_start_p2p_client(wpa_s, ssid, addr_allocated, freq,
Sunil Ravi036cec52023-03-29 11:35:17 -07007318 force_scan, retry_limit, go_bssid);
Dmitry Shmidt912c6ec2015-03-30 13:16:51 -07007319 } else {
Dmitry Shmidt15907092014-03-25 10:42:57 -07007320 return -1;
Dmitry Shmidt912c6ec2015-03-30 13:16:51 -07007321 }
Dmitry Shmidt15907092014-03-25 10:42:57 -07007322
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08007323 if (wpas_p2p_init_go_params(wpa_s, &params, freq, vht_center_freq2,
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08007324 ht40, vht, max_oper_chwidth, he, edmg,
7325 channels))
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007326 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007327
7328 params.role_go = 1;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08007329 params.psk_set = ssid->psk_set;
7330 if (params.psk_set)
7331 os_memcpy(params.psk, ssid->psk, sizeof(params.psk));
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08007332 if (ssid->passphrase) {
7333 if (os_strlen(ssid->passphrase) >= sizeof(params.passphrase)) {
7334 wpa_printf(MSG_ERROR, "P2P: Invalid passphrase in "
7335 "persistent group");
7336 return -1;
7337 }
7338 os_strlcpy(params.passphrase, ssid->passphrase,
7339 sizeof(params.passphrase));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007340 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007341 os_memcpy(params.ssid, ssid->ssid, ssid->ssid_len);
7342 params.ssid_len = ssid->ssid_len;
7343 params.persistent_group = 1;
7344
7345 wpa_s = wpas_p2p_get_group_iface(wpa_s, addr_allocated, 1);
7346 if (wpa_s == NULL)
7347 return -1;
7348
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007349 p2p_channels_to_freqs(channels, params.freq_list, P2P_MAX_CHANNELS);
7350
Dmitry Shmidt56052862013-10-04 10:23:25 -07007351 wpa_s->p2p_first_connection_timeout = connection_timeout;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007352 wpas_start_wps_go(wpa_s, &params, 0);
7353
7354 return 0;
7355}
7356
7357
7358static void wpas_p2p_ie_update(void *ctx, struct wpabuf *beacon_ies,
7359 struct wpabuf *proberesp_ies)
7360{
7361 struct wpa_supplicant *wpa_s = ctx;
7362 if (wpa_s->ap_iface) {
7363 struct hostapd_data *hapd = wpa_s->ap_iface->bss[0];
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007364 if (!(hapd->conf->p2p & P2P_GROUP_OWNER)) {
7365 wpabuf_free(beacon_ies);
7366 wpabuf_free(proberesp_ies);
7367 return;
7368 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007369 if (beacon_ies) {
7370 wpabuf_free(hapd->p2p_beacon_ie);
7371 hapd->p2p_beacon_ie = beacon_ies;
7372 }
7373 wpabuf_free(hapd->p2p_probe_resp_ie);
7374 hapd->p2p_probe_resp_ie = proberesp_ies;
7375 } else {
7376 wpabuf_free(beacon_ies);
7377 wpabuf_free(proberesp_ies);
7378 }
7379 wpa_supplicant_ap_update_beacon(wpa_s);
7380}
7381
7382
7383static void wpas_p2p_idle_update(void *ctx, int idle)
7384{
7385 struct wpa_supplicant *wpa_s = ctx;
7386 if (!wpa_s->ap_iface)
7387 return;
7388 wpa_printf(MSG_DEBUG, "P2P: GO - group %sidle", idle ? "" : "not ");
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08007389 if (idle) {
7390 if (wpa_s->global->p2p_fail_on_wps_complete &&
7391 wpa_s->p2p_in_provisioning) {
7392 wpas_p2p_grpform_fail_after_wps(wpa_s);
7393 return;
7394 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007395 wpas_p2p_set_group_idle_timeout(wpa_s);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08007396 } else
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007397 eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL);
7398}
7399
7400
7401struct p2p_group * wpas_p2p_group_init(struct wpa_supplicant *wpa_s,
Dmitry Shmidt04949592012-07-19 12:16:46 -07007402 struct wpa_ssid *ssid)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007403{
7404 struct p2p_group *group;
7405 struct p2p_group_config *cfg;
7406
Dmitry Shmidt849734c2016-05-27 09:59:01 -07007407 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL ||
7408 !ssid->p2p_group)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007409 return NULL;
7410
7411 cfg = os_zalloc(sizeof(*cfg));
7412 if (cfg == NULL)
7413 return NULL;
7414
Dmitry Shmidt04949592012-07-19 12:16:46 -07007415 if (ssid->p2p_persistent_group && wpa_s->conf->persistent_reconnect)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007416 cfg->persistent_group = 2;
Dmitry Shmidt04949592012-07-19 12:16:46 -07007417 else if (ssid->p2p_persistent_group)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007418 cfg->persistent_group = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007419 os_memcpy(cfg->interface_addr, wpa_s->own_addr, ETH_ALEN);
7420 if (wpa_s->max_stations &&
7421 wpa_s->max_stations < wpa_s->conf->max_num_sta)
7422 cfg->max_clients = wpa_s->max_stations;
7423 else
7424 cfg->max_clients = wpa_s->conf->max_num_sta;
Dmitry Shmidt04949592012-07-19 12:16:46 -07007425 os_memcpy(cfg->ssid, ssid->ssid, ssid->ssid_len);
7426 cfg->ssid_len = ssid->ssid_len;
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08007427 cfg->freq = ssid->frequency;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007428 cfg->cb_ctx = wpa_s;
7429 cfg->ie_update = wpas_p2p_ie_update;
7430 cfg->idle_update = wpas_p2p_idle_update;
Dmitry Shmidte4663042016-04-04 10:07:49 -07007431 cfg->ip_addr_alloc = WPA_GET_BE32(wpa_s->p2pdev->conf->ip_addr_start)
7432 != 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007433
7434 group = p2p_group_init(wpa_s->global->p2p, cfg);
7435 if (group == NULL)
7436 os_free(cfg);
Dmitry Shmidt04949592012-07-19 12:16:46 -07007437 if (ssid->mode != WPAS_MODE_P2P_GROUP_FORMATION)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007438 p2p_group_notif_formation_done(group);
7439 wpa_s->p2p_group = group;
7440 return group;
7441}
7442
7443
7444void wpas_p2p_wps_success(struct wpa_supplicant *wpa_s, const u8 *peer_addr,
7445 int registrar)
7446{
Dmitry Shmidt04949592012-07-19 12:16:46 -07007447 struct wpa_ssid *ssid = wpa_s->current_ssid;
7448
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007449 if (!wpa_s->p2p_in_provisioning) {
7450 wpa_printf(MSG_DEBUG, "P2P: Ignore WPS success event - P2P "
7451 "provisioning not in progress");
7452 return;
7453 }
7454
Dmitry Shmidt04949592012-07-19 12:16:46 -07007455 if (ssid && ssid->mode == WPAS_MODE_INFRA) {
7456 u8 go_dev_addr[ETH_ALEN];
7457 os_memcpy(go_dev_addr, wpa_s->bssid, ETH_ALEN);
7458 wpas_p2p_persistent_group(wpa_s, go_dev_addr, ssid->ssid,
7459 ssid->ssid_len);
7460 /* Clear any stored provisioning info */
7461 p2p_clear_provisioning_info(wpa_s->global->p2p, go_dev_addr);
7462 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007463
Dmitry Shmidt9c175262016-03-03 10:20:07 -08007464 eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s->p2pdev,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007465 NULL);
Dmitry Shmidt56052862013-10-04 10:23:25 -07007466 wpa_s->p2p_go_group_formation_completed = 1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07007467 if (ssid && ssid->mode == WPAS_MODE_INFRA) {
7468 /*
7469 * Use a separate timeout for initial data connection to
7470 * complete to allow the group to be removed automatically if
7471 * something goes wrong in this step before the P2P group idle
7472 * timeout mechanism is taken into use.
7473 */
Dmitry Shmidt56052862013-10-04 10:23:25 -07007474 wpa_dbg(wpa_s, MSG_DEBUG,
7475 "P2P: Re-start group formation timeout (%d seconds) as client for initial connection",
7476 P2P_MAX_INITIAL_CONN_WAIT);
Dmitry Shmidt04949592012-07-19 12:16:46 -07007477 eloop_register_timeout(P2P_MAX_INITIAL_CONN_WAIT, 0,
7478 wpas_p2p_group_formation_timeout,
Dmitry Shmidt9c175262016-03-03 10:20:07 -08007479 wpa_s->p2pdev, NULL);
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08007480 /* Complete group formation on successful data connection. */
7481 wpa_s->p2p_go_group_formation_completed = 0;
Dmitry Shmidt56052862013-10-04 10:23:25 -07007482 } else if (ssid) {
7483 /*
7484 * Use a separate timeout for initial data connection to
7485 * complete to allow the group to be removed automatically if
7486 * the client does not complete data connection successfully.
7487 */
7488 wpa_dbg(wpa_s, MSG_DEBUG,
7489 "P2P: Re-start group formation timeout (%d seconds) as GO for initial connection",
7490 P2P_MAX_INITIAL_CONN_WAIT_GO);
7491 eloop_register_timeout(P2P_MAX_INITIAL_CONN_WAIT_GO, 0,
7492 wpas_p2p_group_formation_timeout,
Dmitry Shmidt9c175262016-03-03 10:20:07 -08007493 wpa_s->p2pdev, NULL);
Dmitry Shmidt56052862013-10-04 10:23:25 -07007494 /*
7495 * Complete group formation on first successful data connection
7496 */
7497 wpa_s->p2p_go_group_formation_completed = 0;
Dmitry Shmidt04949592012-07-19 12:16:46 -07007498 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007499 if (wpa_s->global->p2p)
7500 p2p_wps_success_cb(wpa_s->global->p2p, peer_addr);
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07007501 wpas_group_formation_completed(wpa_s, 1, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007502}
7503
7504
Jouni Malinen75ecf522011-06-27 15:19:46 -07007505void wpas_p2p_wps_failed(struct wpa_supplicant *wpa_s,
7506 struct wps_event_fail *fail)
7507{
7508 if (!wpa_s->p2p_in_provisioning) {
7509 wpa_printf(MSG_DEBUG, "P2P: Ignore WPS fail event - P2P "
7510 "provisioning not in progress");
7511 return;
7512 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007513
7514 if (wpa_s->go_params) {
7515 p2p_clear_provisioning_info(
7516 wpa_s->global->p2p,
Dmitry Shmidt04949592012-07-19 12:16:46 -07007517 wpa_s->go_params->peer_device_addr);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007518 }
7519
Jouni Malinen75ecf522011-06-27 15:19:46 -07007520 wpas_notify_p2p_wps_failed(wpa_s, fail);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08007521
7522 if (wpa_s == wpa_s->global->p2p_group_formation) {
7523 /*
7524 * Allow some time for the failed WPS negotiation exchange to
7525 * complete, but remove the group since group formation cannot
7526 * succeed after provisioning failure.
7527 */
7528 wpa_printf(MSG_DEBUG, "P2P: WPS step failed during group formation - reject connection from timeout");
7529 wpa_s->global->p2p_fail_on_wps_complete = 1;
7530 eloop_deplete_timeout(0, 50000,
7531 wpas_p2p_group_formation_timeout,
Dmitry Shmidt9c175262016-03-03 10:20:07 -08007532 wpa_s->p2pdev, NULL);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08007533 }
7534}
7535
7536
7537int wpas_p2p_wps_eapol_cb(struct wpa_supplicant *wpa_s)
7538{
7539 if (!wpa_s->global->p2p_fail_on_wps_complete ||
7540 !wpa_s->p2p_in_provisioning)
7541 return 0;
7542
7543 wpas_p2p_grpform_fail_after_wps(wpa_s);
7544
7545 return 1;
Jouni Malinen75ecf522011-06-27 15:19:46 -07007546}
7547
7548
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007549int wpas_p2p_prov_disc(struct wpa_supplicant *wpa_s, const u8 *peer_addr,
Dmitry Shmidt04949592012-07-19 12:16:46 -07007550 const char *config_method,
Dmitry Shmidt216983b2015-02-06 10:50:36 -08007551 enum wpas_p2p_prov_disc_use use,
7552 struct p2ps_provision *p2ps_prov)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007553{
7554 u16 config_methods;
7555
Dmitry Shmidt216983b2015-02-06 10:50:36 -08007556 wpa_s->global->pending_p2ps_group = 0;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08007557 wpa_s->global->pending_p2ps_group_freq = 0;
Dmitry Shmidt04949592012-07-19 12:16:46 -07007558 wpa_s->p2p_fallback_to_go_neg = 0;
7559 wpa_s->pending_pd_use = NORMAL_PD;
Dmitry Shmidt216983b2015-02-06 10:50:36 -08007560 if (p2ps_prov && use == WPAS_P2P_PD_FOR_ASP) {
7561 p2ps_prov->conncap = p2ps_group_capability(
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08007562 wpa_s, P2PS_SETUP_NONE, p2ps_prov->role,
7563 &p2ps_prov->force_freq, &p2ps_prov->pref_freq);
7564
Dmitry Shmidt216983b2015-02-06 10:50:36 -08007565 wpa_printf(MSG_DEBUG,
7566 "P2P: %s conncap: %d - ASP parsed: %x %x %d %s",
7567 __func__, p2ps_prov->conncap,
7568 p2ps_prov->adv_id, p2ps_prov->conncap,
7569 p2ps_prov->status, p2ps_prov->info);
7570
7571 config_methods = 0;
7572 } else if (os_strncmp(config_method, "display", 7) == 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007573 config_methods = WPS_CONFIG_DISPLAY;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007574 else if (os_strncmp(config_method, "keypad", 6) == 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007575 config_methods = WPS_CONFIG_KEYPAD;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007576 else if (os_strncmp(config_method, "pbc", 3) == 0 ||
7577 os_strncmp(config_method, "pushbutton", 10) == 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007578 config_methods = WPS_CONFIG_PUSHBUTTON;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007579 else {
7580 wpa_printf(MSG_DEBUG, "P2P: Unknown config method");
Dmitry Shmidt216983b2015-02-06 10:50:36 -08007581 os_free(p2ps_prov);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007582 return -1;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007583 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007584
Dmitry Shmidt04949592012-07-19 12:16:46 -07007585 if (use == WPAS_P2P_PD_AUTO) {
7586 os_memcpy(wpa_s->pending_join_dev_addr, peer_addr, ETH_ALEN);
7587 wpa_s->pending_pd_config_methods = config_methods;
7588 wpa_s->p2p_auto_pd = 1;
7589 wpa_s->p2p_auto_join = 0;
7590 wpa_s->pending_pd_before_join = 0;
7591 wpa_s->auto_pd_scan_retry = 0;
7592 wpas_p2p_stop_find(wpa_s);
7593 wpa_s->p2p_join_scan_count = 0;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08007594 os_get_reltime(&wpa_s->p2p_auto_started);
Dmitry Shmidt04949592012-07-19 12:16:46 -07007595 wpa_printf(MSG_DEBUG, "P2P: Auto PD started at %ld.%06ld",
7596 wpa_s->p2p_auto_started.sec,
7597 wpa_s->p2p_auto_started.usec);
7598 wpas_p2p_join_scan(wpa_s, NULL);
7599 return 0;
7600 }
7601
Dmitry Shmidt216983b2015-02-06 10:50:36 -08007602 if (wpa_s->global->p2p == NULL || wpa_s->global->p2p_disabled) {
7603 os_free(p2ps_prov);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007604 return -1;
Dmitry Shmidt216983b2015-02-06 10:50:36 -08007605 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007606
Dmitry Shmidt216983b2015-02-06 10:50:36 -08007607 return p2p_prov_disc_req(wpa_s->global->p2p, peer_addr, p2ps_prov,
Dmitry Shmidt04949592012-07-19 12:16:46 -07007608 config_methods, use == WPAS_P2P_PD_FOR_JOIN,
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08007609 0, 1);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007610}
7611
7612
7613int wpas_p2p_scan_result_text(const u8 *ies, size_t ies_len, char *buf,
7614 char *end)
7615{
7616 return p2p_scan_result_text(ies, ies_len, buf, end);
7617}
7618
7619
Sunil Ravib0ac25f2024-07-12 01:42:03 +00007620static void wpas_p2p_clear_pending_action_tx(struct wpa_supplicant *wpa_s,
7621 bool force)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007622{
Sunil Ravib0ac25f2024-07-12 01:42:03 +00007623 if (!offchannel_pending_action_tx(wpa_s) && !force)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007624 return;
7625
Dmitry Shmidtde47be72016-01-07 12:52:55 -08007626 if (wpa_s->p2p_send_action_work) {
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08007627 wpas_p2p_free_send_action_work(wpa_s);
Dmitry Shmidtde47be72016-01-07 12:52:55 -08007628 eloop_cancel_timeout(wpas_p2p_send_action_work_timeout,
7629 wpa_s, NULL);
7630 offchannel_send_action_done(wpa_s);
7631 }
Dmitry Shmidt03658832014-08-13 11:03:49 -07007632
Sunil Ravib0ac25f2024-07-12 01:42:03 +00007633 if (!offchannel_pending_action_tx(wpa_s))
7634 return;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007635 wpa_printf(MSG_DEBUG, "P2P: Drop pending Action TX due to new "
7636 "operation request");
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08007637 offchannel_clear_pending_action_tx(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007638}
7639
7640
7641int wpas_p2p_find(struct wpa_supplicant *wpa_s, unsigned int timeout,
7642 enum p2p_discovery_type type,
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08007643 unsigned int num_req_dev_types, const u8 *req_dev_types,
Dmitry Shmidt216983b2015-02-06 10:50:36 -08007644 const u8 *dev_id, unsigned int search_delay,
Hai Shalomc1a21442022-02-04 13:43:00 -08007645 u8 seek_cnt, const char **seek_string, int freq,
7646 bool include_6ghz)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007647{
Sunil Ravib0ac25f2024-07-12 01:42:03 +00007648 wpas_p2p_clear_pending_action_tx(wpa_s, false);
Hai Shalomfdcde762020-04-02 11:19:20 -07007649 wpa_s->global->p2p_long_listen = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007650
Dmitry Shmidt04949592012-07-19 12:16:46 -07007651 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL ||
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07007652 wpa_s->p2p_in_provisioning) {
7653 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Reject p2p_find operation%s%s",
7654 (wpa_s->global->p2p_disabled || !wpa_s->global->p2p) ?
7655 " (P2P disabled)" : "",
7656 wpa_s->p2p_in_provisioning ?
7657 " (p2p_in_provisioning)" : "");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007658 return -1;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07007659 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007660
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08007661 wpa_supplicant_cancel_sched_scan(wpa_s);
7662
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007663 return p2p_find(wpa_s->global->p2p, timeout, type,
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07007664 num_req_dev_types, req_dev_types, dev_id,
Hai Shalomc1a21442022-02-04 13:43:00 -08007665 search_delay, seek_cnt, seek_string, freq,
7666 include_6ghz);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007667}
7668
7669
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007670static void wpas_p2p_scan_res_ignore_search(struct wpa_supplicant *wpa_s,
7671 struct wpa_scan_results *scan_res)
7672{
7673 wpa_printf(MSG_DEBUG, "P2P: Ignore scan results");
7674
7675 if (wpa_s->p2p_scan_work) {
7676 struct wpa_radio_work *work = wpa_s->p2p_scan_work;
7677 wpa_s->p2p_scan_work = NULL;
7678 radio_work_done(work);
7679 }
7680
7681 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
7682 return;
7683
7684 /*
7685 * Indicate that results have been processed so that the P2P module can
7686 * continue pending tasks.
7687 */
Hai Shalom60840252021-02-19 19:02:11 -08007688 wpas_p2p_scan_res_handled(wpa_s);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007689}
7690
7691
7692static void wpas_p2p_stop_find_oper(struct wpa_supplicant *wpa_s)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007693{
Sunil Ravib0ac25f2024-07-12 01:42:03 +00007694 wpas_p2p_clear_pending_action_tx(wpa_s, true);
Hai Shalomfdcde762020-04-02 11:19:20 -07007695 wpa_s->global->p2p_long_listen = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007696 eloop_cancel_timeout(wpas_p2p_long_listen_timeout, wpa_s, NULL);
7697 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007698
7699 if (wpa_s->global->p2p)
7700 p2p_stop_find(wpa_s->global->p2p);
7701
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007702 if (wpa_s->scan_res_handler == wpas_p2p_scan_res_handler) {
7703 wpa_printf(MSG_DEBUG,
7704 "P2P: Do not consider the scan results after stop_find");
7705 wpa_s->scan_res_handler = wpas_p2p_scan_res_ignore_search;
7706 }
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08007707}
7708
7709
7710void wpas_p2p_stop_find(struct wpa_supplicant *wpa_s)
7711{
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007712 wpas_p2p_stop_find_oper(wpa_s);
Dmitry Shmidt216983b2015-02-06 10:50:36 -08007713 if (!wpa_s->global->pending_group_iface_for_p2ps)
7714 wpas_p2p_remove_pending_group_interface(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007715}
7716
7717
7718static void wpas_p2p_long_listen_timeout(void *eloop_ctx, void *timeout_ctx)
7719{
7720 struct wpa_supplicant *wpa_s = eloop_ctx;
Hai Shalomfdcde762020-04-02 11:19:20 -07007721 wpa_s->global->p2p_long_listen = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007722}
7723
7724
7725int wpas_p2p_listen(struct wpa_supplicant *wpa_s, unsigned int timeout)
7726{
7727 int res;
7728
7729 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
7730 return -1;
7731
Dmitry Shmidt58d12ad2016-07-28 10:07:03 -07007732 if (wpa_s->p2p_lo_started) {
7733 wpa_printf(MSG_DEBUG,
7734 "P2P: Cannot start P2P listen, it is offloaded");
7735 return -1;
7736 }
7737
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08007738 wpa_supplicant_cancel_sched_scan(wpa_s);
Sunil Ravib0ac25f2024-07-12 01:42:03 +00007739 wpas_p2p_clear_pending_action_tx(wpa_s, false);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007740
7741 if (timeout == 0) {
7742 /*
7743 * This is a request for unlimited Listen state. However, at
7744 * least for now, this is mapped to a Listen state for one
7745 * hour.
7746 */
7747 timeout = 3600;
7748 }
7749 eloop_cancel_timeout(wpas_p2p_long_listen_timeout, wpa_s, NULL);
Hai Shalomfdcde762020-04-02 11:19:20 -07007750 wpa_s->global->p2p_long_listen = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007751
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007752 /*
7753 * Stop previous find/listen operation to avoid trying to request a new
7754 * remain-on-channel operation while the driver is still running the
7755 * previous one.
7756 */
7757 if (wpa_s->global->p2p)
7758 p2p_stop_find(wpa_s->global->p2p);
7759
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007760 res = wpas_p2p_listen_start(wpa_s, timeout * 1000);
7761 if (res == 0 && timeout * 1000 > wpa_s->max_remain_on_chan) {
Hai Shalomfdcde762020-04-02 11:19:20 -07007762 wpa_s->global->p2p_long_listen = timeout * 1000;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007763 eloop_register_timeout(timeout, 0,
7764 wpas_p2p_long_listen_timeout,
7765 wpa_s, NULL);
7766 }
7767
7768 return res;
7769}
7770
7771
7772int wpas_p2p_assoc_req_ie(struct wpa_supplicant *wpa_s, struct wpa_bss *bss,
7773 u8 *buf, size_t len, int p2p_group)
7774{
7775 struct wpabuf *p2p_ie;
7776 int ret;
7777
7778 if (wpa_s->global->p2p_disabled)
7779 return -1;
Dmitry Shmidt8bd70b72015-05-26 16:02:19 -07007780 /*
7781 * Advertize mandatory cross connection capability even on
7782 * p2p_disabled=1 interface when associating with a P2P Manager WLAN AP.
7783 */
7784 if (wpa_s->conf->p2p_disabled && p2p_group)
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07007785 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007786 if (wpa_s->global->p2p == NULL)
7787 return -1;
7788 if (bss == NULL)
7789 return -1;
7790
7791 p2p_ie = wpa_bss_get_vendor_ie_multi(bss, P2P_IE_VENDOR_TYPE);
7792 ret = p2p_assoc_req_ie(wpa_s->global->p2p, bss->bssid, buf, len,
7793 p2p_group, p2p_ie);
7794 wpabuf_free(p2p_ie);
7795
7796 return ret;
7797}
7798
7799
7800int wpas_p2p_probe_req_rx(struct wpa_supplicant *wpa_s, const u8 *addr,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007801 const u8 *dst, const u8 *bssid,
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07007802 const u8 *ie, size_t ie_len,
7803 unsigned int rx_freq, int ssi_signal)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007804{
7805 if (wpa_s->global->p2p_disabled)
7806 return 0;
7807 if (wpa_s->global->p2p == NULL)
7808 return 0;
7809
Dmitry Shmidt04949592012-07-19 12:16:46 -07007810 switch (p2p_probe_req_rx(wpa_s->global->p2p, addr, dst, bssid,
Dmitry Shmidt58d12ad2016-07-28 10:07:03 -07007811 ie, ie_len, rx_freq, wpa_s->p2p_lo_started)) {
Dmitry Shmidt04949592012-07-19 12:16:46 -07007812 case P2P_PREQ_NOT_P2P:
7813 wpas_notify_preq(wpa_s, addr, dst, bssid, ie, ie_len,
7814 ssi_signal);
7815 /* fall through */
7816 case P2P_PREQ_MALFORMED:
7817 case P2P_PREQ_NOT_LISTEN:
7818 case P2P_PREQ_NOT_PROCESSED:
7819 default: /* make gcc happy */
7820 return 0;
7821 case P2P_PREQ_PROCESSED:
7822 return 1;
7823 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007824}
7825
7826
7827void wpas_p2p_rx_action(struct wpa_supplicant *wpa_s, const u8 *da,
7828 const u8 *sa, const u8 *bssid,
7829 u8 category, const u8 *data, size_t len, int freq)
7830{
7831 if (wpa_s->global->p2p_disabled)
7832 return;
7833 if (wpa_s->global->p2p == NULL)
7834 return;
7835
7836 p2p_rx_action(wpa_s->global->p2p, da, sa, bssid, category, data, len,
7837 freq);
7838}
7839
7840
7841void wpas_p2p_scan_ie(struct wpa_supplicant *wpa_s, struct wpabuf *ies)
7842{
Dmitry Shmidt9c175262016-03-03 10:20:07 -08007843 unsigned int bands;
7844
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007845 if (wpa_s->global->p2p_disabled)
7846 return;
7847 if (wpa_s->global->p2p == NULL)
7848 return;
7849
Dmitry Shmidt9c175262016-03-03 10:20:07 -08007850 bands = wpas_get_bands(wpa_s, NULL);
7851 p2p_scan_ie(wpa_s->global->p2p, ies, NULL, bands);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007852}
7853
7854
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07007855static void wpas_p2p_group_deinit(struct wpa_supplicant *wpa_s)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007856{
7857 p2p_group_deinit(wpa_s->p2p_group);
7858 wpa_s->p2p_group = NULL;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007859
7860 wpa_s->ap_configured_cb = NULL;
7861 wpa_s->ap_configured_cb_ctx = NULL;
7862 wpa_s->ap_configured_cb_data = NULL;
7863 wpa_s->connect_without_scan = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007864}
7865
7866
7867int wpas_p2p_reject(struct wpa_supplicant *wpa_s, const u8 *addr)
7868{
Hai Shalomfdcde762020-04-02 11:19:20 -07007869 wpa_s->global->p2p_long_listen = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007870
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007871 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
7872 return -1;
7873
7874 return p2p_reject(wpa_s->global->p2p, addr);
7875}
7876
7877
7878/* Invite to reinvoke a persistent group */
7879int wpas_p2p_invite(struct wpa_supplicant *wpa_s, const u8 *peer_addr,
Jouni Malinen31be0a42012-08-31 21:20:51 +03007880 struct wpa_ssid *ssid, const u8 *go_dev_addr, int freq,
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08007881 int vht_center_freq2, int ht40, int vht, int max_chwidth,
Hai Shalomc1a21442022-02-04 13:43:00 -08007882 int pref_freq, int he, int edmg, bool allow_6ghz)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007883{
7884 enum p2p_invite_role role;
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07007885 u8 *bssid = NULL;
7886 int force_freq = 0;
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08007887 int res;
Dmitry Shmidta0d265f2013-11-19 13:13:41 -08007888 int no_pref_freq_given = pref_freq == 0;
Sunil8cd6f4d2022-06-28 18:40:46 +00007889 struct weighted_pcl pref_freq_list[P2P_MAX_PREF_CHANNELS];
7890 unsigned int size;
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08007891
Hai Shalomc1a21442022-02-04 13:43:00 -08007892 if (wpas_p2p_check_6ghz(wpa_s, NULL, allow_6ghz, freq))
7893 return -1;
7894
Dmitry Shmidt700a1372013-03-15 14:14:44 -07007895 wpa_s->global->p2p_invite_group = NULL;
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08007896 if (peer_addr)
7897 os_memcpy(wpa_s->p2p_auth_invite, peer_addr, ETH_ALEN);
7898 else
7899 os_memset(wpa_s->p2p_auth_invite, 0, ETH_ALEN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007900
Jouni Malinen31be0a42012-08-31 21:20:51 +03007901 wpa_s->p2p_persistent_go_freq = freq;
7902 wpa_s->p2p_go_ht40 = !!ht40;
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08007903 wpa_s->p2p_go_vht = !!vht;
Hai Shalom74f70d42019-02-11 14:42:39 -08007904 wpa_s->p2p_go_he = !!he;
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08007905 wpa_s->p2p_go_max_oper_chwidth = max_chwidth;
7906 wpa_s->p2p_go_vht_center_freq2 = vht_center_freq2;
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08007907 wpa_s->p2p_go_edmg = !!edmg;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007908 if (ssid->mode == WPAS_MODE_P2P_GO) {
7909 role = P2P_INVITE_ROLE_GO;
7910 if (peer_addr == NULL) {
7911 wpa_printf(MSG_DEBUG, "P2P: Missing peer "
7912 "address in invitation command");
7913 return -1;
7914 }
7915 if (wpas_p2p_create_iface(wpa_s)) {
7916 if (wpas_p2p_add_group_interface(wpa_s,
7917 WPA_IF_P2P_GO) < 0) {
7918 wpa_printf(MSG_ERROR, "P2P: Failed to "
7919 "allocate a new interface for the "
7920 "group");
7921 return -1;
7922 }
7923 bssid = wpa_s->pending_interface_addr;
Dmitry Shmidt9c175262016-03-03 10:20:07 -08007924 } else if (wpa_s->p2p_mgmt)
7925 bssid = wpa_s->parent->own_addr;
7926 else
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007927 bssid = wpa_s->own_addr;
7928 } else {
7929 role = P2P_INVITE_ROLE_CLIENT;
7930 peer_addr = ssid->bssid;
7931 }
7932 wpa_s->pending_invite_ssid_id = ssid->id;
7933
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08007934 size = P2P_MAX_PREF_CHANNELS;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07007935 res = wpas_p2p_setup_freqs(wpa_s, freq, &force_freq, &pref_freq,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08007936 role == P2P_INVITE_ROLE_GO,
7937 pref_freq_list, &size);
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08007938 if (res)
7939 return res;
Irfan Sheriffaf84a572012-09-22 16:59:30 -07007940
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007941 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
7942 return -1;
7943
Dmitry Shmidt7d56b752015-12-22 10:59:44 -08007944 p2p_set_own_pref_freq_list(wpa_s->global->p2p, pref_freq_list, size);
7945
Dmitry Shmidta0d265f2013-11-19 13:13:41 -08007946 if (wpa_s->parent->conf->p2p_ignore_shared_freq &&
7947 no_pref_freq_given && pref_freq > 0 &&
7948 wpa_s->num_multichan_concurrent > 1 &&
7949 wpas_p2p_num_unused_channels(wpa_s) > 0) {
7950 wpa_printf(MSG_DEBUG, "P2P: Ignore own channel preference %d MHz for invitation due to p2p_ignore_shared_freq=1 configuration",
7951 pref_freq);
7952 pref_freq = 0;
7953 }
7954
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08007955 /*
7956 * Stop any find/listen operations before invitation and possibly
7957 * connection establishment.
7958 */
7959 wpas_p2p_stop_find_oper(wpa_s);
7960
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007961 return p2p_invite(wpa_s->global->p2p, peer_addr, role, bssid,
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08007962 ssid->ssid, ssid->ssid_len, force_freq, go_dev_addr,
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08007963 1, pref_freq, -1);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007964}
7965
7966
7967/* Invite to join an active group */
7968int wpas_p2p_invite_group(struct wpa_supplicant *wpa_s, const char *ifname,
Hai Shalomc1a21442022-02-04 13:43:00 -08007969 const u8 *peer_addr, const u8 *go_dev_addr,
7970 bool allow_6ghz)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007971{
7972 struct wpa_global *global = wpa_s->global;
7973 enum p2p_invite_role role;
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07007974 u8 *bssid = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007975 struct wpa_ssid *ssid;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08007976 int persistent;
Dmitry Shmidt051af732013-10-22 13:52:46 -07007977 int freq = 0, force_freq = 0, pref_freq = 0;
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08007978 int res;
Sunil8cd6f4d2022-06-28 18:40:46 +00007979 struct weighted_pcl pref_freq_list[P2P_MAX_PREF_CHANNELS];
7980 unsigned int size;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007981
Jouni Malinen31be0a42012-08-31 21:20:51 +03007982 wpa_s->p2p_persistent_go_freq = 0;
7983 wpa_s->p2p_go_ht40 = 0;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07007984 wpa_s->p2p_go_vht = 0;
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08007985 wpa_s->p2p_go_vht_center_freq2 = 0;
7986 wpa_s->p2p_go_max_oper_chwidth = 0;
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08007987 wpa_s->p2p_go_edmg = 0;
Jouni Malinen31be0a42012-08-31 21:20:51 +03007988
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007989 for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
7990 if (os_strcmp(wpa_s->ifname, ifname) == 0)
7991 break;
7992 }
7993 if (wpa_s == NULL) {
7994 wpa_printf(MSG_DEBUG, "P2P: Interface '%s' not found", ifname);
7995 return -1;
7996 }
7997
7998 ssid = wpa_s->current_ssid;
7999 if (ssid == NULL) {
8000 wpa_printf(MSG_DEBUG, "P2P: No current SSID to use for "
8001 "invitation");
8002 return -1;
8003 }
8004
Dmitry Shmidt700a1372013-03-15 14:14:44 -07008005 wpa_s->global->p2p_invite_group = wpa_s;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08008006 persistent = ssid->p2p_persistent_group &&
Dmitry Shmidt9c175262016-03-03 10:20:07 -08008007 wpas_p2p_get_persistent(wpa_s->p2pdev, peer_addr,
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08008008 ssid->ssid, ssid->ssid_len);
8009
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008010 if (ssid->mode == WPAS_MODE_P2P_GO) {
8011 role = P2P_INVITE_ROLE_ACTIVE_GO;
8012 bssid = wpa_s->own_addr;
8013 if (go_dev_addr == NULL)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008014 go_dev_addr = wpa_s->global->p2p_dev_addr;
Dmitry Shmidt051af732013-10-22 13:52:46 -07008015 freq = ssid->frequency;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008016 } else {
8017 role = P2P_INVITE_ROLE_CLIENT;
8018 if (wpa_s->wpa_state < WPA_ASSOCIATED) {
8019 wpa_printf(MSG_DEBUG, "P2P: Not associated - cannot "
8020 "invite to current group");
8021 return -1;
8022 }
8023 bssid = wpa_s->bssid;
8024 if (go_dev_addr == NULL &&
8025 !is_zero_ether_addr(wpa_s->go_dev_addr))
8026 go_dev_addr = wpa_s->go_dev_addr;
Dmitry Shmidt051af732013-10-22 13:52:46 -07008027 freq = wpa_s->current_bss ? wpa_s->current_bss->freq :
8028 (int) wpa_s->assoc_freq;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008029 }
Dmitry Shmidt9c175262016-03-03 10:20:07 -08008030 wpa_s->p2pdev->pending_invite_ssid_id = -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008031
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008032 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
8033 return -1;
Hai Shalomc1a21442022-02-04 13:43:00 -08008034 if (wpas_p2p_check_6ghz(wpa_s, peer_addr, allow_6ghz, freq))
8035 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008036
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08008037 size = P2P_MAX_PREF_CHANNELS;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07008038 res = wpas_p2p_setup_freqs(wpa_s, freq, &force_freq, &pref_freq,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08008039 role == P2P_INVITE_ROLE_ACTIVE_GO,
8040 pref_freq_list, &size);
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08008041 if (res)
8042 return res;
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07008043 wpas_p2p_set_own_freq_preference(wpa_s, force_freq);
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08008044
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008045 return p2p_invite(wpa_s->global->p2p, peer_addr, role, bssid,
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08008046 ssid->ssid, ssid->ssid_len, force_freq,
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08008047 go_dev_addr, persistent, pref_freq, -1);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008048}
8049
8050
8051void wpas_p2p_completed(struct wpa_supplicant *wpa_s)
8052{
8053 struct wpa_ssid *ssid = wpa_s->current_ssid;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008054 u8 go_dev_addr[ETH_ALEN];
8055 int persistent;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008056 int freq;
Hai Shalom5f92bc92019-04-18 11:54:11 -07008057 u8 ip[3 * 4], *ip_ptr = NULL;
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08008058 char ip_addr[100];
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008059
Dmitry Shmidt04949592012-07-19 12:16:46 -07008060 if (ssid == NULL || ssid->mode != WPAS_MODE_P2P_GROUP_FORMATION) {
8061 eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
Dmitry Shmidt9c175262016-03-03 10:20:07 -08008062 wpa_s->p2pdev, NULL);
Dmitry Shmidt04949592012-07-19 12:16:46 -07008063 }
8064
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008065 if (!wpa_s->show_group_started || !ssid)
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08008066 return;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008067
8068 wpa_s->show_group_started = 0;
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08008069 if (!wpa_s->p2p_go_group_formation_completed &&
8070 wpa_s->global->p2p_group_formation == wpa_s) {
8071 wpa_dbg(wpa_s, MSG_DEBUG,
8072 "P2P: Marking group formation completed on client on data connection");
8073 wpa_s->p2p_go_group_formation_completed = 1;
8074 wpa_s->global->p2p_group_formation = NULL;
8075 wpa_s->p2p_in_provisioning = 0;
8076 wpa_s->p2p_in_invitation = 0;
Matthew Wang06b42472022-11-10 06:56:31 +00008077 wpa_s->p2p_retry_limit = 0;
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08008078 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008079
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008080 os_memset(go_dev_addr, 0, ETH_ALEN);
8081 if (ssid->bssid_set)
8082 os_memcpy(go_dev_addr, ssid->bssid, ETH_ALEN);
8083 persistent = wpas_p2p_persistent_group(wpa_s, go_dev_addr, ssid->ssid,
8084 ssid->ssid_len);
8085 os_memcpy(wpa_s->go_dev_addr, go_dev_addr, ETH_ALEN);
8086
8087 if (wpa_s->global->p2p_group_formation == wpa_s)
8088 wpa_s->global->p2p_group_formation = NULL;
8089
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008090 freq = wpa_s->current_bss ? wpa_s->current_bss->freq :
8091 (int) wpa_s->assoc_freq;
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08008092
8093 ip_addr[0] = '\0';
8094 if (wpa_sm_get_p2p_ip_addr(wpa_s->wpa, ip) == 0) {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08008095 int res;
8096
8097 res = os_snprintf(ip_addr, sizeof(ip_addr),
8098 " ip_addr=%u.%u.%u.%u "
8099 "ip_mask=%u.%u.%u.%u go_ip_addr=%u.%u.%u.%u",
8100 ip[0], ip[1], ip[2], ip[3],
8101 ip[4], ip[5], ip[6], ip[7],
8102 ip[8], ip[9], ip[10], ip[11]);
8103 if (os_snprintf_error(sizeof(ip_addr), res))
8104 ip_addr[0] = '\0';
Hai Shalom5f92bc92019-04-18 11:54:11 -07008105 ip_ptr = ip;
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08008106 }
8107
Dmitry Shmidt7f0b69e2014-07-28 10:35:20 -07008108 wpas_p2p_group_started(wpa_s, 0, ssid, freq,
8109 ssid->passphrase == NULL && ssid->psk_set ?
8110 ssid->psk : NULL,
8111 ssid->passphrase, go_dev_addr, persistent,
8112 ip_addr);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008113
8114 if (persistent)
Dmitry Shmidtd5ab1b52016-06-21 12:38:41 -07008115 wpas_p2p_store_persistent_group(wpa_s->p2pdev,
8116 ssid, go_dev_addr);
8117
Hai Shalom5f92bc92019-04-18 11:54:11 -07008118 wpas_notify_p2p_group_started(wpa_s, ssid, persistent, 1, ip_ptr);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008119}
8120
8121
8122int wpas_p2p_presence_req(struct wpa_supplicant *wpa_s, u32 duration1,
8123 u32 interval1, u32 duration2, u32 interval2)
8124{
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08008125 int ret;
8126
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008127 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
8128 return -1;
8129
8130 if (wpa_s->wpa_state < WPA_ASSOCIATED ||
8131 wpa_s->current_ssid == NULL ||
8132 wpa_s->current_ssid->mode != WPAS_MODE_INFRA)
8133 return -1;
8134
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08008135 ret = p2p_presence_req(wpa_s->global->p2p, wpa_s->bssid,
8136 wpa_s->own_addr, wpa_s->assoc_freq,
8137 duration1, interval1, duration2, interval2);
8138 if (ret == 0)
8139 wpa_s->waiting_presence_resp = 1;
8140
8141 return ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008142}
8143
8144
8145int wpas_p2p_ext_listen(struct wpa_supplicant *wpa_s, unsigned int period,
8146 unsigned int interval)
8147{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008148 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
8149 return -1;
8150
8151 return p2p_ext_listen(wpa_s->global->p2p, period, interval);
8152}
8153
8154
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008155static int wpas_p2p_is_client(struct wpa_supplicant *wpa_s)
8156{
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08008157 if (wpa_s->current_ssid == NULL) {
8158 /*
8159 * current_ssid can be cleared when P2P client interface gets
8160 * disconnected, so assume this interface was used as P2P
8161 * client.
8162 */
8163 return 1;
8164 }
8165 return wpa_s->current_ssid->p2p_group &&
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008166 wpa_s->current_ssid->mode == WPAS_MODE_INFRA;
8167}
8168
8169
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008170static void wpas_p2p_group_idle_timeout(void *eloop_ctx, void *timeout_ctx)
8171{
8172 struct wpa_supplicant *wpa_s = eloop_ctx;
8173
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008174 if (wpa_s->conf->p2p_group_idle == 0 && !wpas_p2p_is_client(wpa_s)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008175 wpa_printf(MSG_DEBUG, "P2P: Ignore group idle timeout - "
8176 "disabled");
8177 return;
8178 }
8179
Dmitry Shmidt04949592012-07-19 12:16:46 -07008180 wpa_printf(MSG_DEBUG, "P2P: Group idle timeout reached - terminate "
8181 "group");
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07008182 wpas_p2p_group_delete(wpa_s, P2P_GROUP_REMOVAL_IDLE_TIMEOUT);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008183}
8184
8185
8186static void wpas_p2p_set_group_idle_timeout(struct wpa_supplicant *wpa_s)
8187{
Dmitry Shmidt04949592012-07-19 12:16:46 -07008188 int timeout;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008189
Dmitry Shmidt04949592012-07-19 12:16:46 -07008190 if (eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL) > 0)
8191 wpa_printf(MSG_DEBUG, "P2P: Cancelled P2P group idle timeout");
8192
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008193 if (wpa_s->current_ssid == NULL || !wpa_s->current_ssid->p2p_group)
8194 return;
8195
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008196 timeout = wpa_s->conf->p2p_group_idle;
8197 if (wpa_s->current_ssid->mode == WPAS_MODE_INFRA &&
8198 (timeout == 0 || timeout > P2P_MAX_CLIENT_IDLE))
8199 timeout = P2P_MAX_CLIENT_IDLE;
8200
8201 if (timeout == 0)
8202 return;
8203
Dmitry Shmidt04949592012-07-19 12:16:46 -07008204 if (timeout < 0) {
8205 if (wpa_s->current_ssid->mode == WPAS_MODE_INFRA)
8206 timeout = 0; /* special client mode no-timeout */
8207 else
8208 return;
8209 }
8210
8211 if (wpa_s->p2p_in_provisioning) {
8212 /*
8213 * Use the normal group formation timeout during the
8214 * provisioning phase to avoid terminating this process too
8215 * early due to group idle timeout.
8216 */
8217 wpa_printf(MSG_DEBUG, "P2P: Do not use P2P group idle timeout "
8218 "during provisioning");
8219 return;
8220 }
Deepthi Gowri9e4e8ac2013-04-16 11:57:05 +05308221
Dmitry Shmidt04949592012-07-19 12:16:46 -07008222 if (wpa_s->show_group_started) {
8223 /*
8224 * Use the normal group formation timeout between the end of
8225 * the provisioning phase and completion of 4-way handshake to
8226 * avoid terminating this process too early due to group idle
8227 * timeout.
8228 */
8229 wpa_printf(MSG_DEBUG, "P2P: Do not use P2P group idle timeout "
8230 "while waiting for initial 4-way handshake to "
8231 "complete");
8232 return;
8233 }
8234
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008235 wpa_printf(MSG_DEBUG, "P2P: Set P2P group idle timeout to %u seconds",
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008236 timeout);
8237 eloop_register_timeout(timeout, 0, wpas_p2p_group_idle_timeout,
8238 wpa_s, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008239}
8240
8241
Jouni Malinen2b89da82012-08-31 22:04:41 +03008242/* Returns 1 if the interface was removed */
8243int wpas_p2p_deauth_notif(struct wpa_supplicant *wpa_s, const u8 *bssid,
8244 u16 reason_code, const u8 *ie, size_t ie_len,
8245 int locally_generated)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008246{
8247 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
Jouni Malinen2b89da82012-08-31 22:04:41 +03008248 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008249
Dmitry Shmidt04949592012-07-19 12:16:46 -07008250 if (!locally_generated)
8251 p2p_deauth_notif(wpa_s->global->p2p, bssid, reason_code, ie,
8252 ie_len);
8253
8254 if (reason_code == WLAN_REASON_DEAUTH_LEAVING && !locally_generated &&
8255 wpa_s->current_ssid &&
8256 wpa_s->current_ssid->p2p_group &&
8257 wpa_s->current_ssid->mode == WPAS_MODE_INFRA) {
8258 wpa_printf(MSG_DEBUG, "P2P: GO indicated that the P2P Group "
8259 "session is ending");
Jouni Malinen2b89da82012-08-31 22:04:41 +03008260 if (wpas_p2p_group_delete(wpa_s,
8261 P2P_GROUP_REMOVAL_GO_ENDING_SESSION)
8262 > 0)
8263 return 1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07008264 }
Jouni Malinen2b89da82012-08-31 22:04:41 +03008265
8266 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008267}
8268
8269
8270void wpas_p2p_disassoc_notif(struct wpa_supplicant *wpa_s, const u8 *bssid,
Dmitry Shmidt04949592012-07-19 12:16:46 -07008271 u16 reason_code, const u8 *ie, size_t ie_len,
8272 int locally_generated)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008273{
8274 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
8275 return;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008276
Dmitry Shmidt04949592012-07-19 12:16:46 -07008277 if (!locally_generated)
8278 p2p_disassoc_notif(wpa_s->global->p2p, bssid, reason_code, ie,
8279 ie_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008280}
8281
8282
8283void wpas_p2p_update_config(struct wpa_supplicant *wpa_s)
8284{
8285 struct p2p_data *p2p = wpa_s->global->p2p;
8286
8287 if (p2p == NULL)
8288 return;
8289
8290 if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_CAPABLE))
8291 return;
8292
8293 if (wpa_s->conf->changed_parameters & CFG_CHANGED_DEVICE_NAME)
8294 p2p_set_dev_name(p2p, wpa_s->conf->device_name);
8295
8296 if (wpa_s->conf->changed_parameters & CFG_CHANGED_DEVICE_TYPE)
8297 p2p_set_pri_dev_type(p2p, wpa_s->conf->device_type);
8298
8299 if (wpa_s->wps &&
8300 (wpa_s->conf->changed_parameters & CFG_CHANGED_CONFIG_METHODS))
8301 p2p_set_config_methods(p2p, wpa_s->wps->config_methods);
8302
8303 if (wpa_s->wps && (wpa_s->conf->changed_parameters & CFG_CHANGED_UUID))
8304 p2p_set_uuid(p2p, wpa_s->wps->uuid);
8305
8306 if (wpa_s->conf->changed_parameters & CFG_CHANGED_WPS_STRING) {
8307 p2p_set_manufacturer(p2p, wpa_s->conf->manufacturer);
8308 p2p_set_model_name(p2p, wpa_s->conf->model_name);
8309 p2p_set_model_number(p2p, wpa_s->conf->model_number);
8310 p2p_set_serial_number(p2p, wpa_s->conf->serial_number);
8311 }
8312
8313 if (wpa_s->conf->changed_parameters & CFG_CHANGED_SEC_DEVICE_TYPE)
8314 p2p_set_sec_dev_types(p2p,
8315 (void *) wpa_s->conf->sec_device_type,
8316 wpa_s->conf->num_sec_device_types);
8317
8318 if (wpa_s->conf->changed_parameters & CFG_CHANGED_VENDOR_EXTENSION) {
8319 int i;
8320 p2p_remove_wps_vendor_extensions(p2p);
8321 for (i = 0; i < MAX_WPS_VENDOR_EXT; i++) {
8322 if (wpa_s->conf->wps_vendor_ext[i] == NULL)
8323 continue;
8324 p2p_add_wps_vendor_extension(
8325 p2p, wpa_s->conf->wps_vendor_ext[i]);
8326 }
8327 }
8328
8329 if ((wpa_s->conf->changed_parameters & CFG_CHANGED_COUNTRY) &&
8330 wpa_s->conf->country[0] && wpa_s->conf->country[1]) {
8331 char country[3];
8332 country[0] = wpa_s->conf->country[0];
8333 country[1] = wpa_s->conf->country[1];
8334 country[2] = 0x04;
8335 p2p_set_country(p2p, country);
8336 }
8337
8338 if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_SSID_POSTFIX) {
8339 p2p_set_ssid_postfix(p2p, (u8 *) wpa_s->conf->p2p_ssid_postfix,
8340 wpa_s->conf->p2p_ssid_postfix ?
8341 os_strlen(wpa_s->conf->p2p_ssid_postfix) :
8342 0);
8343 }
8344
8345 if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_INTRA_BSS)
8346 p2p_set_intra_bss_dist(p2p, wpa_s->conf->p2p_intra_bss);
Jouni Malinen75ecf522011-06-27 15:19:46 -07008347
8348 if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_LISTEN_CHANNEL) {
8349 u8 reg_class, channel;
8350 int ret;
8351 unsigned int r;
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07008352 u8 channel_forced;
8353
Jouni Malinen75ecf522011-06-27 15:19:46 -07008354 if (wpa_s->conf->p2p_listen_reg_class &&
8355 wpa_s->conf->p2p_listen_channel) {
8356 reg_class = wpa_s->conf->p2p_listen_reg_class;
8357 channel = wpa_s->conf->p2p_listen_channel;
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07008358 channel_forced = 1;
Jouni Malinen75ecf522011-06-27 15:19:46 -07008359 } else {
8360 reg_class = 81;
8361 /*
8362 * Pick one of the social channels randomly as the
8363 * listen channel.
8364 */
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07008365 if (os_get_random((u8 *) &r, sizeof(r)) < 0)
8366 channel = 1;
8367 else
8368 channel = 1 + (r % 3) * 5;
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07008369 channel_forced = 0;
Jouni Malinen75ecf522011-06-27 15:19:46 -07008370 }
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07008371 ret = p2p_set_listen_channel(p2p, reg_class, channel,
8372 channel_forced);
Jouni Malinen75ecf522011-06-27 15:19:46 -07008373 if (ret)
8374 wpa_printf(MSG_ERROR, "P2P: Own listen channel update "
8375 "failed: %d", ret);
8376 }
8377 if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_OPER_CHANNEL) {
8378 u8 op_reg_class, op_channel, cfg_op_channel;
8379 int ret = 0;
8380 unsigned int r;
8381 if (wpa_s->conf->p2p_oper_reg_class &&
8382 wpa_s->conf->p2p_oper_channel) {
8383 op_reg_class = wpa_s->conf->p2p_oper_reg_class;
8384 op_channel = wpa_s->conf->p2p_oper_channel;
8385 cfg_op_channel = 1;
8386 } else {
8387 op_reg_class = 81;
8388 /*
8389 * Use random operation channel from (1, 6, 11)
8390 *if no other preference is indicated.
8391 */
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07008392 if (os_get_random((u8 *) &r, sizeof(r)) < 0)
8393 op_channel = 1;
8394 else
8395 op_channel = 1 + (r % 3) * 5;
Jouni Malinen75ecf522011-06-27 15:19:46 -07008396 cfg_op_channel = 0;
8397 }
8398 ret = p2p_set_oper_channel(p2p, op_reg_class, op_channel,
8399 cfg_op_channel);
8400 if (ret)
8401 wpa_printf(MSG_ERROR, "P2P: Own oper channel update "
8402 "failed: %d", ret);
8403 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07008404
8405 if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_PREF_CHAN) {
8406 if (p2p_set_pref_chan(p2p, wpa_s->conf->num_p2p_pref_chan,
8407 wpa_s->conf->p2p_pref_chan) < 0) {
8408 wpa_printf(MSG_ERROR, "P2P: Preferred channel list "
8409 "update failed");
8410 }
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07008411
8412 if (p2p_set_no_go_freq(p2p, &wpa_s->conf->p2p_no_go_freq) < 0) {
8413 wpa_printf(MSG_ERROR, "P2P: No GO channel list "
8414 "update failed");
8415 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07008416 }
Dmitry Shmidt2271d3f2014-06-23 12:16:31 -07008417
8418 if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_PASSPHRASE_LEN)
8419 p2p_set_passphrase_len(p2p, wpa_s->conf->p2p_passphrase_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008420}
8421
8422
8423int wpas_p2p_set_noa(struct wpa_supplicant *wpa_s, u8 count, int start,
8424 int duration)
8425{
8426 if (!wpa_s->ap_iface)
8427 return -1;
8428 return hostapd_p2p_set_noa(wpa_s->ap_iface->bss[0], count, start,
8429 duration);
8430}
8431
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008432
8433int wpas_p2p_set_cross_connect(struct wpa_supplicant *wpa_s, int enabled)
8434{
8435 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
8436 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008437
8438 wpa_s->global->cross_connection = enabled;
8439 p2p_set_cross_connect(wpa_s->global->p2p, enabled);
8440
8441 if (!enabled) {
8442 struct wpa_supplicant *iface;
8443
8444 for (iface = wpa_s->global->ifaces; iface; iface = iface->next)
8445 {
8446 if (iface->cross_connect_enabled == 0)
8447 continue;
8448
8449 iface->cross_connect_enabled = 0;
8450 iface->cross_connect_in_use = 0;
Dmitry Shmidt9c175262016-03-03 10:20:07 -08008451 wpa_msg_global(iface->p2pdev, MSG_INFO,
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07008452 P2P_EVENT_CROSS_CONNECT_DISABLE "%s %s",
8453 iface->ifname,
8454 iface->cross_connect_uplink);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008455 }
8456 }
8457
8458 return 0;
8459}
8460
8461
8462static void wpas_p2p_enable_cross_connect(struct wpa_supplicant *uplink)
8463{
8464 struct wpa_supplicant *iface;
8465
8466 if (!uplink->global->cross_connection)
8467 return;
8468
8469 for (iface = uplink->global->ifaces; iface; iface = iface->next) {
8470 if (!iface->cross_connect_enabled)
8471 continue;
8472 if (os_strcmp(uplink->ifname, iface->cross_connect_uplink) !=
8473 0)
8474 continue;
8475 if (iface->ap_iface == NULL)
8476 continue;
8477 if (iface->cross_connect_in_use)
8478 continue;
8479
8480 iface->cross_connect_in_use = 1;
Dmitry Shmidt9c175262016-03-03 10:20:07 -08008481 wpa_msg_global(iface->p2pdev, MSG_INFO,
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07008482 P2P_EVENT_CROSS_CONNECT_ENABLE "%s %s",
8483 iface->ifname, iface->cross_connect_uplink);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008484 }
8485}
8486
8487
8488static void wpas_p2p_disable_cross_connect(struct wpa_supplicant *uplink)
8489{
8490 struct wpa_supplicant *iface;
8491
8492 for (iface = uplink->global->ifaces; iface; iface = iface->next) {
8493 if (!iface->cross_connect_enabled)
8494 continue;
8495 if (os_strcmp(uplink->ifname, iface->cross_connect_uplink) !=
8496 0)
8497 continue;
8498 if (!iface->cross_connect_in_use)
8499 continue;
8500
Dmitry Shmidt9c175262016-03-03 10:20:07 -08008501 wpa_msg_global(iface->p2pdev, MSG_INFO,
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07008502 P2P_EVENT_CROSS_CONNECT_DISABLE "%s %s",
8503 iface->ifname, iface->cross_connect_uplink);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008504 iface->cross_connect_in_use = 0;
8505 }
8506}
8507
8508
8509void wpas_p2p_notif_connected(struct wpa_supplicant *wpa_s)
8510{
8511 if (wpa_s->ap_iface || wpa_s->current_ssid == NULL ||
8512 wpa_s->current_ssid->mode != WPAS_MODE_INFRA ||
8513 wpa_s->cross_connect_disallowed)
8514 wpas_p2p_disable_cross_connect(wpa_s);
8515 else
8516 wpas_p2p_enable_cross_connect(wpa_s);
Dmitry Shmidt04949592012-07-19 12:16:46 -07008517 if (!wpa_s->ap_iface &&
8518 eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL) > 0)
8519 wpa_printf(MSG_DEBUG, "P2P: Cancelled P2P group idle timeout");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008520}
8521
8522
8523void wpas_p2p_notif_disconnected(struct wpa_supplicant *wpa_s)
8524{
8525 wpas_p2p_disable_cross_connect(wpa_s);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008526 if (!wpa_s->ap_iface &&
8527 !eloop_is_timeout_registered(wpas_p2p_group_idle_timeout,
8528 wpa_s, NULL))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008529 wpas_p2p_set_group_idle_timeout(wpa_s);
8530}
8531
8532
8533static void wpas_p2p_cross_connect_setup(struct wpa_supplicant *wpa_s)
8534{
8535 struct wpa_supplicant *iface;
8536
8537 if (!wpa_s->global->cross_connection)
8538 return;
8539
8540 for (iface = wpa_s->global->ifaces; iface; iface = iface->next) {
8541 if (iface == wpa_s)
8542 continue;
8543 if (iface->drv_flags &
8544 WPA_DRIVER_FLAGS_P2P_DEDICATED_INTERFACE)
8545 continue;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08008546 if ((iface->drv_flags & WPA_DRIVER_FLAGS_P2P_CAPABLE) &&
8547 iface != wpa_s->parent)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008548 continue;
8549
8550 wpa_s->cross_connect_enabled = 1;
8551 os_strlcpy(wpa_s->cross_connect_uplink, iface->ifname,
8552 sizeof(wpa_s->cross_connect_uplink));
8553 wpa_printf(MSG_DEBUG, "P2P: Enable cross connection from "
8554 "%s to %s whenever uplink is available",
8555 wpa_s->ifname, wpa_s->cross_connect_uplink);
8556
8557 if (iface->ap_iface || iface->current_ssid == NULL ||
8558 iface->current_ssid->mode != WPAS_MODE_INFRA ||
8559 iface->cross_connect_disallowed ||
8560 iface->wpa_state != WPA_COMPLETED)
8561 break;
8562
8563 wpa_s->cross_connect_in_use = 1;
Dmitry Shmidt9c175262016-03-03 10:20:07 -08008564 wpa_msg_global(wpa_s->p2pdev, MSG_INFO,
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07008565 P2P_EVENT_CROSS_CONNECT_ENABLE "%s %s",
8566 wpa_s->ifname, wpa_s->cross_connect_uplink);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008567 break;
8568 }
8569}
8570
8571
Sunil Ravi036cec52023-03-29 11:35:17 -07008572static int wpas_p2p_notif_pbc_overlap(struct wpa_supplicant *wpa_s)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008573{
8574 if (wpa_s->p2p_group_interface != P2P_GROUP_INTERFACE_CLIENT &&
8575 !wpa_s->p2p_in_provisioning)
8576 return 0; /* not P2P client operation */
8577
8578 wpa_printf(MSG_DEBUG, "P2P: Terminate connection due to WPS PBC "
8579 "session overlap");
Dmitry Shmidt9c175262016-03-03 10:20:07 -08008580 if (wpa_s != wpa_s->p2pdev)
8581 wpa_msg_ctrl(wpa_s->p2pdev, MSG_INFO, WPS_EVENT_OVERLAP);
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07008582 wpas_p2p_group_formation_failed(wpa_s, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008583 return 1;
8584}
8585
8586
Dmitry Shmidtdf5a7e42014-04-02 12:59:59 -07008587void wpas_p2p_pbc_overlap_cb(void *eloop_ctx, void *timeout_ctx)
8588{
8589 struct wpa_supplicant *wpa_s = eloop_ctx;
8590 wpas_p2p_notif_pbc_overlap(wpa_s);
8591}
8592
8593
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08008594void wpas_p2p_update_channel_list(struct wpa_supplicant *wpa_s,
8595 enum wpas_p2p_channel_update_trig trig)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008596{
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07008597 struct p2p_channels chan, cli_chan;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08008598 struct wpa_used_freq_data *freqs = NULL;
8599 unsigned int num = wpa_s->num_multichan_concurrent;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008600
8601 if (wpa_s->global == NULL || wpa_s->global->p2p == NULL)
8602 return;
8603
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08008604 freqs = os_calloc(num, sizeof(struct wpa_used_freq_data));
8605 if (!freqs)
8606 return;
8607
Sunil Ravi77d572f2023-01-17 23:58:31 +00008608 num = get_shared_radio_freqs_data(wpa_s, freqs, num, false);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08008609
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008610 os_memset(&chan, 0, sizeof(chan));
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07008611 os_memset(&cli_chan, 0, sizeof(cli_chan));
Hai Shalom60840252021-02-19 19:02:11 -08008612 if (wpas_p2p_setup_channels(wpa_s, &chan, &cli_chan,
8613 is_p2p_6ghz_disabled(wpa_s->global->p2p))) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008614 wpa_printf(MSG_ERROR, "P2P: Failed to update supported "
8615 "channel list");
8616 return;
8617 }
8618
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07008619 p2p_update_channel_list(wpa_s->global->p2p, &chan, &cli_chan);
Dmitry Shmidt684785c2014-05-12 13:34:29 -07008620
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08008621 wpas_p2p_optimize_listen_channel(wpa_s, freqs, num);
Dmitry Shmidt684785c2014-05-12 13:34:29 -07008622
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08008623 /*
8624 * The used frequencies map changed, so it is possible that a GO is
8625 * using a channel that is no longer valid for P2P use. It is also
8626 * possible that due to policy consideration, it would be preferable to
8627 * move it to a frequency already used by other station interfaces.
8628 */
8629 wpas_p2p_consider_moving_gos(wpa_s, freqs, num, trig);
8630
8631 os_free(freqs);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008632}
8633
8634
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08008635static void wpas_p2p_scan_res_ignore(struct wpa_supplicant *wpa_s,
8636 struct wpa_scan_results *scan_res)
8637{
8638 wpa_printf(MSG_DEBUG, "P2P: Ignore scan results");
8639}
8640
8641
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008642int wpas_p2p_cancel(struct wpa_supplicant *wpa_s)
8643{
8644 struct wpa_global *global = wpa_s->global;
8645 int found = 0;
8646 const u8 *peer;
8647
8648 if (global->p2p == NULL)
8649 return -1;
8650
8651 wpa_printf(MSG_DEBUG, "P2P: Request to cancel group formation");
8652
8653 if (wpa_s->pending_interface_name[0] &&
8654 !is_zero_ether_addr(wpa_s->pending_interface_addr))
8655 found = 1;
8656
8657 peer = p2p_get_go_neg_peer(global->p2p);
8658 if (peer) {
8659 wpa_printf(MSG_DEBUG, "P2P: Unauthorize pending GO Neg peer "
8660 MACSTR, MAC2STR(peer));
8661 p2p_unauthorize(global->p2p, peer);
Dmitry Shmidt04949592012-07-19 12:16:46 -07008662 found = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008663 }
8664
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08008665 if (wpa_s->scan_res_handler == wpas_p2p_scan_res_join) {
8666 wpa_printf(MSG_DEBUG, "P2P: Stop pending scan for join");
8667 wpa_s->scan_res_handler = wpas_p2p_scan_res_ignore;
8668 found = 1;
8669 }
8670
8671 if (wpa_s->pending_pd_before_join) {
8672 wpa_printf(MSG_DEBUG, "P2P: Stop pending PD before join");
8673 wpa_s->pending_pd_before_join = 0;
8674 found = 1;
8675 }
8676
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008677 wpas_p2p_stop_find(wpa_s);
8678
8679 for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
8680 if (wpa_s == global->p2p_group_formation &&
8681 (wpa_s->p2p_in_provisioning ||
8682 wpa_s->parent->pending_interface_type ==
8683 WPA_IF_P2P_CLIENT)) {
8684 wpa_printf(MSG_DEBUG, "P2P: Interface %s in group "
8685 "formation found - cancelling",
8686 wpa_s->ifname);
8687 found = 1;
8688 eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
Dmitry Shmidt9c175262016-03-03 10:20:07 -08008689 wpa_s->p2pdev, NULL);
Jouni Malinenadddfc42012-10-03 14:31:41 -07008690 if (wpa_s->p2p_in_provisioning) {
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07008691 wpas_group_formation_completed(wpa_s, 0, 0);
Jouni Malinenadddfc42012-10-03 14:31:41 -07008692 break;
8693 }
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07008694 wpas_p2p_group_delete(wpa_s,
8695 P2P_GROUP_REMOVAL_REQUESTED);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008696 break;
Dmitry Shmidt21de2142014-04-08 10:50:52 -07008697 } else if (wpa_s->p2p_in_invitation) {
8698 wpa_printf(MSG_DEBUG, "P2P: Interface %s in invitation found - cancelling",
8699 wpa_s->ifname);
8700 found = 1;
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07008701 wpas_p2p_group_formation_failed(wpa_s, 0);
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08008702 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008703 }
8704 }
8705
8706 if (!found) {
8707 wpa_printf(MSG_DEBUG, "P2P: No ongoing group formation found");
8708 return -1;
8709 }
8710
8711 return 0;
8712}
8713
8714
8715void wpas_p2p_interface_unavailable(struct wpa_supplicant *wpa_s)
8716{
8717 if (wpa_s->current_ssid == NULL || !wpa_s->current_ssid->p2p_group)
8718 return;
8719
8720 wpa_printf(MSG_DEBUG, "P2P: Remove group due to driver resource not "
8721 "being available anymore");
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07008722 wpas_p2p_group_delete(wpa_s, P2P_GROUP_REMOVAL_UNAVAILABLE);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008723}
8724
8725
8726void wpas_p2p_update_best_channels(struct wpa_supplicant *wpa_s,
8727 int freq_24, int freq_5, int freq_overall)
8728{
8729 struct p2p_data *p2p = wpa_s->global->p2p;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08008730 if (p2p == NULL)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008731 return;
8732 p2p_set_best_channels(p2p, freq_24, freq_5, freq_overall);
8733}
8734
8735
8736int wpas_p2p_unauthorize(struct wpa_supplicant *wpa_s, const char *addr)
8737{
8738 u8 peer[ETH_ALEN];
8739 struct p2p_data *p2p = wpa_s->global->p2p;
8740
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08008741 if (p2p == NULL)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008742 return -1;
8743
8744 if (hwaddr_aton(addr, peer))
8745 return -1;
8746
8747 return p2p_unauthorize(p2p, peer);
8748}
8749
8750
8751/**
8752 * wpas_p2p_disconnect - Disconnect from a P2P Group
8753 * @wpa_s: Pointer to wpa_supplicant data
8754 * Returns: 0 on success, -1 on failure
8755 *
8756 * This can be used to disconnect from a group in which the local end is a P2P
8757 * Client or to end a P2P Group in case the local end is the Group Owner. If a
8758 * virtual network interface was created for this group, that interface will be
8759 * removed. Otherwise, only the configured P2P group network will be removed
8760 * from the interface.
8761 */
8762int wpas_p2p_disconnect(struct wpa_supplicant *wpa_s)
8763{
8764
8765 if (wpa_s == NULL)
8766 return -1;
8767
Jouni Malinen2b89da82012-08-31 22:04:41 +03008768 return wpas_p2p_group_delete(wpa_s, P2P_GROUP_REMOVAL_REQUESTED) < 0 ?
8769 -1 : 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008770}
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008771
8772
8773int wpas_p2p_in_progress(struct wpa_supplicant *wpa_s)
8774{
Dmitry Shmidtf8623282013-02-20 14:34:59 -08008775 int ret;
8776
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008777 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
8778 return 0;
8779
Dmitry Shmidtf8623282013-02-20 14:34:59 -08008780 ret = p2p_in_progress(wpa_s->global->p2p);
8781 if (ret == 0) {
8782 /*
8783 * Check whether there is an ongoing WPS provisioning step (or
8784 * other parts of group formation) on another interface since
8785 * p2p_in_progress() does not report this to avoid issues for
8786 * scans during such provisioning step.
8787 */
8788 if (wpa_s->global->p2p_group_formation &&
8789 wpa_s->global->p2p_group_formation != wpa_s) {
8790 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Another interface (%s) "
8791 "in group formation",
8792 wpa_s->global->p2p_group_formation->ifname);
8793 ret = 1;
Sunil Ravi77d572f2023-01-17 23:58:31 +00008794 } else if (wpa_s->global->p2p_group_formation == wpa_s) {
8795 wpa_dbg(wpa_s, MSG_DEBUG,
8796 "P2P: Skip Extended Listen timeout and allow scans on current interface for group formation");
8797 ret = 2;
Dmitry Shmidtf8623282013-02-20 14:34:59 -08008798 }
8799 }
8800
Dmitry Shmidt92c368d2013-08-29 12:37:21 -07008801 if (!ret && wpa_s->global->p2p_go_wait_client.sec) {
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08008802 struct os_reltime now;
8803 os_get_reltime(&now);
8804 if (os_reltime_expired(&now, &wpa_s->global->p2p_go_wait_client,
8805 P2P_MAX_INITIAL_CONN_WAIT_GO)) {
Dmitry Shmidt92c368d2013-08-29 12:37:21 -07008806 /* Wait for the first client has expired */
8807 wpa_s->global->p2p_go_wait_client.sec = 0;
8808 } else {
8809 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Waiting for initial client connection during group formation");
8810 ret = 1;
8811 }
8812 }
8813
Dmitry Shmidtf8623282013-02-20 14:34:59 -08008814 return ret;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008815}
8816
8817
8818void wpas_p2p_network_removed(struct wpa_supplicant *wpa_s,
8819 struct wpa_ssid *ssid)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008820{
8821 if (wpa_s->p2p_in_provisioning && ssid->p2p_group &&
8822 eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
Dmitry Shmidt9c175262016-03-03 10:20:07 -08008823 wpa_s->p2pdev, NULL) > 0) {
Dmitry Shmidt04949592012-07-19 12:16:46 -07008824 /**
8825 * Remove the network by scheduling the group formation
8826 * timeout to happen immediately. The teardown code
8827 * needs to be scheduled to run asynch later so that we
8828 * don't delete data from under ourselves unexpectedly.
8829 * Calling wpas_p2p_group_formation_timeout directly
8830 * causes a series of crashes in WPS failure scenarios.
8831 */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008832 wpa_printf(MSG_DEBUG, "P2P: Canceled group formation due to "
8833 "P2P group network getting removed");
Dmitry Shmidt04949592012-07-19 12:16:46 -07008834 eloop_register_timeout(0, 0, wpas_p2p_group_formation_timeout,
Dmitry Shmidt9c175262016-03-03 10:20:07 -08008835 wpa_s->p2pdev, NULL);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008836 }
8837}
8838
8839
8840struct wpa_ssid * wpas_p2p_get_persistent(struct wpa_supplicant *wpa_s,
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08008841 const u8 *addr, const u8 *ssid,
8842 size_t ssid_len)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008843{
8844 struct wpa_ssid *s;
8845 size_t i;
8846
8847 for (s = wpa_s->conf->ssid; s; s = s->next) {
8848 if (s->disabled != 2)
8849 continue;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08008850 if (ssid &&
8851 (ssid_len != s->ssid_len ||
8852 os_memcmp(ssid, s->ssid, ssid_len) != 0))
8853 continue;
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07008854 if (addr == NULL) {
8855 if (s->mode == WPAS_MODE_P2P_GO)
8856 return s;
8857 continue;
8858 }
Sunil Ravib0ac25f2024-07-12 01:42:03 +00008859 if (ether_addr_equal(s->bssid, addr))
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008860 return s; /* peer is GO in the persistent group */
8861 if (s->mode != WPAS_MODE_P2P_GO || s->p2p_client_list == NULL)
8862 continue;
8863 for (i = 0; i < s->num_p2p_clients; i++) {
Sunil Ravib0ac25f2024-07-12 01:42:03 +00008864 if (ether_addr_equal(s->p2p_client_list +
8865 i * 2 * ETH_ALEN, addr))
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008866 return s; /* peer is P2P client in persistent
8867 * group */
8868 }
8869 }
8870
8871 return NULL;
8872}
8873
8874
8875void wpas_p2p_notify_ap_sta_authorized(struct wpa_supplicant *wpa_s,
8876 const u8 *addr)
8877{
Dmitry Shmidt56052862013-10-04 10:23:25 -07008878 if (eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
Dmitry Shmidt9c175262016-03-03 10:20:07 -08008879 wpa_s->p2pdev, NULL) > 0) {
Dmitry Shmidt56052862013-10-04 10:23:25 -07008880 /*
8881 * This can happen if WPS provisioning step is not terminated
8882 * cleanly (e.g., P2P Client does not send WSC_Done). Since the
8883 * peer was able to connect, there is no need to time out group
8884 * formation after this, though. In addition, this is used with
8885 * the initial connection wait on the GO as a separate formation
8886 * timeout and as such, expected to be hit after the initial WPS
8887 * provisioning step.
8888 */
8889 wpa_printf(MSG_DEBUG, "P2P: Canceled P2P group formation timeout on data connection");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08008890
8891 if (!wpa_s->p2p_go_group_formation_completed &&
8892 !wpa_s->group_formation_reported) {
8893 /*
8894 * GO has not yet notified group formation success since
8895 * the WPS step was not completed cleanly. Do that
8896 * notification now since the P2P Client was able to
8897 * connect and as such, must have received the
8898 * credential from the WPS step.
8899 */
8900 if (wpa_s->global->p2p)
8901 p2p_wps_success_cb(wpa_s->global->p2p, addr);
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07008902 wpas_group_formation_completed(wpa_s, 1, 0);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08008903 }
Dmitry Shmidt56052862013-10-04 10:23:25 -07008904 }
8905 if (!wpa_s->p2p_go_group_formation_completed) {
8906 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Marking group formation completed on GO on first data connection");
8907 wpa_s->p2p_go_group_formation_completed = 1;
8908 wpa_s->global->p2p_group_formation = NULL;
8909 wpa_s->p2p_in_provisioning = 0;
Dmitry Shmidt21de2142014-04-08 10:50:52 -07008910 wpa_s->p2p_in_invitation = 0;
Matthew Wang06b42472022-11-10 06:56:31 +00008911 wpa_s->p2p_retry_limit = 0;
Dmitry Shmidt56052862013-10-04 10:23:25 -07008912 }
Dmitry Shmidt92c368d2013-08-29 12:37:21 -07008913 wpa_s->global->p2p_go_wait_client.sec = 0;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008914 if (addr == NULL)
8915 return;
8916 wpas_p2p_add_persistent_group_client(wpa_s, addr);
8917}
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08008918
Dmitry Shmidt04949592012-07-19 12:16:46 -07008919
Dmitry Shmidt7f656022015-02-25 14:36:37 -08008920static int wpas_p2p_fallback_to_go_neg(struct wpa_supplicant *wpa_s,
8921 int group_added)
Dmitry Shmidt04949592012-07-19 12:16:46 -07008922{
8923 struct wpa_supplicant *group = wpa_s;
Dmitry Shmidt7f656022015-02-25 14:36:37 -08008924 int ret = 0;
8925
Dmitry Shmidt04949592012-07-19 12:16:46 -07008926 if (wpa_s->global->p2p_group_formation)
8927 group = wpa_s->global->p2p_group_formation;
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07008928 wpa_s = wpa_s->global->p2p_init_wpa_s;
Dmitry Shmidt04949592012-07-19 12:16:46 -07008929 offchannel_send_action_done(wpa_s);
8930 if (group_added)
Dmitry Shmidt7f656022015-02-25 14:36:37 -08008931 ret = wpas_p2p_group_delete(group, P2P_GROUP_REMOVAL_SILENT);
Dmitry Shmidt04949592012-07-19 12:16:46 -07008932 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Fall back to GO Negotiation");
8933 wpas_p2p_connect(wpa_s, wpa_s->pending_join_dev_addr, wpa_s->p2p_pin,
8934 wpa_s->p2p_wps_method, wpa_s->p2p_persistent_group, 0,
8935 0, 0, wpa_s->p2p_go_intent, wpa_s->p2p_connect_freq,
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08008936 wpa_s->p2p_go_vht_center_freq2,
Dmitry Shmidt04949592012-07-19 12:16:46 -07008937 wpa_s->p2p_persistent_id,
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07008938 wpa_s->p2p_pd_before_go_neg,
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07008939 wpa_s->p2p_go_ht40,
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08008940 wpa_s->p2p_go_vht,
Hai Shalom74f70d42019-02-11 14:42:39 -08008941 wpa_s->p2p_go_max_oper_chwidth,
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08008942 wpa_s->p2p_go_he,
8943 wpa_s->p2p_go_edmg,
Sunil Ravic0f5d412024-09-11 22:12:49 +00008944 NULL, 0, is_p2p_allow_6ghz(wpa_s->global->p2p),
8945 wpa_s->p2p2, wpa_s->p2p_bootstrap, NULL);
Dmitry Shmidt7f656022015-02-25 14:36:37 -08008946 return ret;
Dmitry Shmidt04949592012-07-19 12:16:46 -07008947}
8948
8949
8950int wpas_p2p_scan_no_go_seen(struct wpa_supplicant *wpa_s)
8951{
Dmitry Shmidt7f656022015-02-25 14:36:37 -08008952 int res;
8953
Dmitry Shmidt04949592012-07-19 12:16:46 -07008954 if (!wpa_s->p2p_fallback_to_go_neg ||
8955 wpa_s->p2p_in_provisioning <= 5)
8956 return 0;
8957
8958 if (wpas_p2p_peer_go(wpa_s, wpa_s->pending_join_dev_addr) > 0)
8959 return 0; /* peer operating as a GO */
8960
8961 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: GO not found for p2p_connect-auto - "
8962 "fallback to GO Negotiation");
Dmitry Shmidt9c175262016-03-03 10:20:07 -08008963 wpa_msg_global(wpa_s->p2pdev, MSG_INFO, P2P_EVENT_FALLBACK_TO_GO_NEG
Dmitry Shmidt7f656022015-02-25 14:36:37 -08008964 "reason=GO-not-found");
8965 res = wpas_p2p_fallback_to_go_neg(wpa_s, 1);
Dmitry Shmidt04949592012-07-19 12:16:46 -07008966
Dmitry Shmidt7f656022015-02-25 14:36:37 -08008967 return res == 1 ? 2 : 1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07008968}
8969
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07008970
8971unsigned int wpas_p2p_search_delay(struct wpa_supplicant *wpa_s)
8972{
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07008973 struct wpa_supplicant *ifs;
8974
8975 if (wpa_s->wpa_state > WPA_SCANNING) {
8976 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Use %u ms search delay due to "
8977 "concurrent operation",
Dmitry Shmidt09f57ba2014-06-10 16:07:13 -07008978 wpa_s->conf->p2p_search_delay);
8979 return wpa_s->conf->p2p_search_delay;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07008980 }
8981
Dmitry Shmidt01904cf2013-12-05 11:08:35 -08008982 dl_list_for_each(ifs, &wpa_s->radio->ifaces, struct wpa_supplicant,
8983 radio_list) {
8984 if (ifs != wpa_s && ifs->wpa_state > WPA_SCANNING) {
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07008985 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Use %u ms search "
8986 "delay due to concurrent operation on "
8987 "interface %s",
Dmitry Shmidt09f57ba2014-06-10 16:07:13 -07008988 wpa_s->conf->p2p_search_delay,
8989 ifs->ifname);
8990 return wpa_s->conf->p2p_search_delay;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07008991 }
8992 }
8993
8994 return 0;
8995}
8996
Dmitry Shmidt37d4d6a2013-03-18 13:09:42 -07008997
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07008998static int wpas_p2p_remove_psk_entry(struct wpa_supplicant *wpa_s,
8999 struct wpa_ssid *s, const u8 *addr,
9000 int iface_addr)
9001{
9002 struct psk_list_entry *psk, *tmp;
9003 int changed = 0;
9004
9005 dl_list_for_each_safe(psk, tmp, &s->psk_list, struct psk_list_entry,
9006 list) {
9007 if ((iface_addr && !psk->p2p &&
Sunil Ravib0ac25f2024-07-12 01:42:03 +00009008 ether_addr_equal(addr, psk->addr)) ||
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07009009 (!iface_addr && psk->p2p &&
Sunil Ravib0ac25f2024-07-12 01:42:03 +00009010 ether_addr_equal(addr, psk->addr))) {
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07009011 wpa_dbg(wpa_s, MSG_DEBUG,
9012 "P2P: Remove persistent group PSK list entry for "
9013 MACSTR " p2p=%u",
9014 MAC2STR(psk->addr), psk->p2p);
9015 dl_list_del(&psk->list);
9016 os_free(psk);
9017 changed++;
9018 }
9019 }
9020
9021 return changed;
9022}
9023
9024
9025void wpas_p2p_new_psk_cb(struct wpa_supplicant *wpa_s, const u8 *mac_addr,
9026 const u8 *p2p_dev_addr,
9027 const u8 *psk, size_t psk_len)
9028{
9029 struct wpa_ssid *ssid = wpa_s->current_ssid;
9030 struct wpa_ssid *persistent;
Dmitry Shmidt7832adb2014-04-29 10:53:02 -07009031 struct psk_list_entry *p, *last;
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07009032
9033 if (psk_len != sizeof(p->psk))
9034 return;
9035
9036 if (p2p_dev_addr) {
9037 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: New PSK for addr=" MACSTR
9038 " p2p_dev_addr=" MACSTR,
9039 MAC2STR(mac_addr), MAC2STR(p2p_dev_addr));
9040 if (is_zero_ether_addr(p2p_dev_addr))
9041 p2p_dev_addr = NULL;
9042 } else {
9043 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: New PSK for addr=" MACSTR,
9044 MAC2STR(mac_addr));
9045 }
9046
9047 if (ssid->mode == WPAS_MODE_P2P_GROUP_FORMATION) {
9048 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: new_psk_cb during group formation");
9049 /* To be added to persistent group once created */
9050 if (wpa_s->global->add_psk == NULL) {
9051 wpa_s->global->add_psk = os_zalloc(sizeof(*p));
9052 if (wpa_s->global->add_psk == NULL)
9053 return;
9054 }
9055 p = wpa_s->global->add_psk;
9056 if (p2p_dev_addr) {
9057 p->p2p = 1;
9058 os_memcpy(p->addr, p2p_dev_addr, ETH_ALEN);
9059 } else {
9060 p->p2p = 0;
9061 os_memcpy(p->addr, mac_addr, ETH_ALEN);
9062 }
9063 os_memcpy(p->psk, psk, psk_len);
9064 return;
9065 }
9066
9067 if (ssid->mode != WPAS_MODE_P2P_GO || !ssid->p2p_persistent_group) {
9068 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Ignore new_psk_cb on not-persistent GO");
9069 return;
9070 }
9071
Dmitry Shmidt9c175262016-03-03 10:20:07 -08009072 persistent = wpas_p2p_get_persistent(wpa_s->p2pdev, NULL, ssid->ssid,
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07009073 ssid->ssid_len);
9074 if (!persistent) {
9075 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Could not find persistent group information to store the new PSK");
9076 return;
9077 }
9078
9079 p = os_zalloc(sizeof(*p));
9080 if (p == NULL)
9081 return;
9082 if (p2p_dev_addr) {
9083 p->p2p = 1;
9084 os_memcpy(p->addr, p2p_dev_addr, ETH_ALEN);
9085 } else {
9086 p->p2p = 0;
9087 os_memcpy(p->addr, mac_addr, ETH_ALEN);
9088 }
9089 os_memcpy(p->psk, psk, psk_len);
9090
Dmitry Shmidt7832adb2014-04-29 10:53:02 -07009091 if (dl_list_len(&persistent->psk_list) > P2P_MAX_STORED_CLIENTS &&
9092 (last = dl_list_last(&persistent->psk_list,
9093 struct psk_list_entry, list))) {
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07009094 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Remove oldest PSK entry for "
9095 MACSTR " (p2p=%u) to make room for a new one",
9096 MAC2STR(last->addr), last->p2p);
9097 dl_list_del(&last->list);
9098 os_free(last);
9099 }
9100
Dmitry Shmidt9c175262016-03-03 10:20:07 -08009101 wpas_p2p_remove_psk_entry(wpa_s->p2pdev, persistent,
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07009102 p2p_dev_addr ? p2p_dev_addr : mac_addr,
9103 p2p_dev_addr == NULL);
9104 if (p2p_dev_addr) {
9105 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Add new PSK for p2p_dev_addr="
9106 MACSTR, MAC2STR(p2p_dev_addr));
9107 } else {
9108 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Add new PSK for addr=" MACSTR,
9109 MAC2STR(mac_addr));
9110 }
9111 dl_list_add(&persistent->psk_list, &p->list);
9112
Dmitry Shmidt9c175262016-03-03 10:20:07 -08009113 if (wpa_s->p2pdev->conf->update_config &&
9114 wpa_config_write(wpa_s->p2pdev->confname, wpa_s->p2pdev->conf))
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07009115 wpa_printf(MSG_DEBUG, "P2P: Failed to update configuration");
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07009116}
9117
9118
9119static void wpas_p2p_remove_psk(struct wpa_supplicant *wpa_s,
9120 struct wpa_ssid *s, const u8 *addr,
9121 int iface_addr)
9122{
9123 int res;
9124
9125 res = wpas_p2p_remove_psk_entry(wpa_s, s, addr, iface_addr);
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08009126 if (res > 0 && wpa_s->conf->update_config &&
9127 wpa_config_write(wpa_s->confname, wpa_s->conf))
9128 wpa_dbg(wpa_s, MSG_DEBUG,
9129 "P2P: Failed to update configuration");
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07009130}
9131
9132
9133static void wpas_p2p_remove_client_go(struct wpa_supplicant *wpa_s,
9134 const u8 *peer, int iface_addr)
9135{
9136 struct hostapd_data *hapd;
9137 struct hostapd_wpa_psk *psk, *prev, *rem;
9138 struct sta_info *sta;
9139
9140 if (wpa_s->ap_iface == NULL || wpa_s->current_ssid == NULL ||
9141 wpa_s->current_ssid->mode != WPAS_MODE_P2P_GO)
9142 return;
9143
9144 /* Remove per-station PSK entry */
9145 hapd = wpa_s->ap_iface->bss[0];
9146 prev = NULL;
9147 psk = hapd->conf->ssid.wpa_psk;
9148 while (psk) {
Sunil Ravib0ac25f2024-07-12 01:42:03 +00009149 if ((iface_addr && ether_addr_equal(peer, psk->addr)) ||
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07009150 (!iface_addr &&
Sunil Ravib0ac25f2024-07-12 01:42:03 +00009151 ether_addr_equal(peer, psk->p2p_dev_addr))) {
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07009152 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Remove operating group PSK entry for "
9153 MACSTR " iface_addr=%d",
9154 MAC2STR(peer), iface_addr);
9155 if (prev)
9156 prev->next = psk->next;
9157 else
9158 hapd->conf->ssid.wpa_psk = psk->next;
9159 rem = psk;
9160 psk = psk->next;
Sunil Ravia04bd252022-05-02 22:54:18 -07009161 bin_clear_free(rem, sizeof(*rem));
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07009162 } else {
9163 prev = psk;
9164 psk = psk->next;
9165 }
9166 }
9167
9168 /* Disconnect from group */
9169 if (iface_addr)
9170 sta = ap_get_sta(hapd, peer);
9171 else
9172 sta = ap_get_sta_p2p(hapd, peer);
9173 if (sta) {
9174 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Disconnect peer " MACSTR
9175 " (iface_addr=%d) from group",
9176 MAC2STR(peer), iface_addr);
9177 hostapd_drv_sta_deauth(hapd, sta->addr,
9178 WLAN_REASON_DEAUTH_LEAVING);
9179 ap_sta_deauthenticate(hapd, sta, WLAN_REASON_DEAUTH_LEAVING);
9180 }
9181}
9182
9183
9184void wpas_p2p_remove_client(struct wpa_supplicant *wpa_s, const u8 *peer,
9185 int iface_addr)
9186{
9187 struct wpa_ssid *s;
9188 struct wpa_supplicant *w;
Dmitry Shmidt8bd70b72015-05-26 16:02:19 -07009189 struct wpa_supplicant *p2p_wpa_s = wpa_s->global->p2p_init_wpa_s;
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07009190
9191 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Remove client " MACSTR, MAC2STR(peer));
9192
9193 /* Remove from any persistent group */
Dmitry Shmidt8bd70b72015-05-26 16:02:19 -07009194 for (s = p2p_wpa_s->conf->ssid; s; s = s->next) {
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07009195 if (s->disabled != 2 || s->mode != WPAS_MODE_P2P_GO)
9196 continue;
9197 if (!iface_addr)
Dmitry Shmidt8bd70b72015-05-26 16:02:19 -07009198 wpas_remove_persistent_peer(p2p_wpa_s, s, peer, 0);
9199 wpas_p2p_remove_psk(p2p_wpa_s, s, peer, iface_addr);
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07009200 }
9201
9202 /* Remove from any operating group */
9203 for (w = wpa_s->global->ifaces; w; w = w->next)
9204 wpas_p2p_remove_client_go(w, peer, iface_addr);
9205}
9206
9207
9208static void wpas_p2p_psk_failure_removal(void *eloop_ctx, void *timeout_ctx)
9209{
9210 struct wpa_supplicant *wpa_s = eloop_ctx;
9211 wpas_p2p_group_delete(wpa_s, P2P_GROUP_REMOVAL_PSK_FAILURE);
9212}
9213
9214
Dmitry Shmidt04f534e2013-12-09 15:50:16 -08009215static void wpas_p2p_group_freq_conflict(void *eloop_ctx, void *timeout_ctx)
9216{
9217 struct wpa_supplicant *wpa_s = eloop_ctx;
9218
9219 wpa_printf(MSG_DEBUG, "P2P: Frequency conflict - terminate group");
9220 wpas_p2p_group_delete(wpa_s, P2P_GROUP_REMOVAL_FREQ_CONFLICT);
9221}
9222
9223
9224int wpas_p2p_handle_frequency_conflicts(struct wpa_supplicant *wpa_s, int freq,
9225 struct wpa_ssid *ssid)
9226{
9227 struct wpa_supplicant *iface;
9228
9229 for (iface = wpa_s->global->ifaces; iface; iface = iface->next) {
9230 if (!iface->current_ssid ||
9231 iface->current_ssid->frequency == freq ||
9232 (iface->p2p_group_interface == NOT_P2P_GROUP_INTERFACE &&
9233 !iface->current_ssid->p2p_group))
9234 continue;
9235
9236 /* Remove the connection with least priority */
9237 if (!wpas_is_p2p_prioritized(iface)) {
9238 /* STA connection has priority over existing
9239 * P2P connection, so remove the interface. */
9240 wpa_printf(MSG_DEBUG, "P2P: Removing P2P connection due to single channel concurrent mode frequency conflict");
9241 eloop_register_timeout(0, 0,
9242 wpas_p2p_group_freq_conflict,
9243 iface, NULL);
9244 /* If connection in progress is P2P connection, do not
9245 * proceed for the connection. */
9246 if (wpa_s == iface)
9247 return -1;
9248 else
9249 return 0;
9250 } else {
9251 /* P2P connection has priority, disable the STA network
9252 */
9253 wpa_supplicant_disable_network(wpa_s->global->ifaces,
9254 ssid);
9255 wpa_msg(wpa_s->global->ifaces, MSG_INFO,
9256 WPA_EVENT_FREQ_CONFLICT " id=%d", ssid->id);
9257 os_memset(wpa_s->global->ifaces->pending_bssid, 0,
9258 ETH_ALEN);
9259 /* If P2P connection is in progress, continue
9260 * connecting...*/
9261 if (wpa_s == iface)
9262 return 0;
9263 else
9264 return -1;
9265 }
9266 }
9267
9268 return 0;
9269}
9270
9271
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07009272int wpas_p2p_4way_hs_failed(struct wpa_supplicant *wpa_s)
9273{
9274 struct wpa_ssid *ssid = wpa_s->current_ssid;
9275
9276 if (ssid == NULL || !ssid->p2p_group)
9277 return 0;
9278
9279 if (wpa_s->p2p_last_4way_hs_fail &&
9280 wpa_s->p2p_last_4way_hs_fail == ssid) {
9281 u8 go_dev_addr[ETH_ALEN];
9282 struct wpa_ssid *persistent;
9283
9284 if (wpas_p2p_persistent_group(wpa_s, go_dev_addr,
9285 ssid->ssid,
9286 ssid->ssid_len) <= 0) {
9287 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Could not determine whether 4-way handshake failures were for a persistent group");
9288 goto disconnect;
9289 }
9290
9291 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Two 4-way handshake failures for a P2P group - go_dev_addr="
9292 MACSTR, MAC2STR(go_dev_addr));
Dmitry Shmidt9c175262016-03-03 10:20:07 -08009293 persistent = wpas_p2p_get_persistent(wpa_s->p2pdev, go_dev_addr,
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07009294 ssid->ssid,
9295 ssid->ssid_len);
9296 if (persistent == NULL || persistent->mode != WPAS_MODE_INFRA) {
9297 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: No matching persistent group stored");
9298 goto disconnect;
9299 }
Dmitry Shmidt9c175262016-03-03 10:20:07 -08009300 wpa_msg_global(wpa_s->p2pdev, MSG_INFO,
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07009301 P2P_EVENT_PERSISTENT_PSK_FAIL "%d",
9302 persistent->id);
9303 disconnect:
9304 wpa_s->p2p_last_4way_hs_fail = NULL;
9305 /*
9306 * Remove the group from a timeout to avoid issues with caller
9307 * continuing to use the interface if this is on a P2P group
9308 * interface.
9309 */
9310 eloop_register_timeout(0, 0, wpas_p2p_psk_failure_removal,
9311 wpa_s, NULL);
9312 return 1;
9313 }
9314
9315 wpa_s->p2p_last_4way_hs_fail = ssid;
9316 return 0;
9317}
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08009318
9319
9320#ifdef CONFIG_WPS_NFC
9321
9322static struct wpabuf * wpas_p2p_nfc_handover(int ndef, struct wpabuf *wsc,
9323 struct wpabuf *p2p)
9324{
9325 struct wpabuf *ret;
9326 size_t wsc_len;
9327
9328 if (p2p == NULL) {
9329 wpabuf_free(wsc);
9330 wpa_printf(MSG_DEBUG, "P2P: No p2p buffer for handover");
9331 return NULL;
9332 }
9333
9334 wsc_len = wsc ? wpabuf_len(wsc) : 0;
9335 ret = wpabuf_alloc(2 + wsc_len + 2 + wpabuf_len(p2p));
9336 if (ret == NULL) {
9337 wpabuf_free(wsc);
9338 wpabuf_free(p2p);
9339 return NULL;
9340 }
9341
9342 wpabuf_put_be16(ret, wsc_len);
9343 if (wsc)
9344 wpabuf_put_buf(ret, wsc);
9345 wpabuf_put_be16(ret, wpabuf_len(p2p));
9346 wpabuf_put_buf(ret, p2p);
9347
9348 wpabuf_free(wsc);
9349 wpabuf_free(p2p);
9350 wpa_hexdump_buf(MSG_DEBUG,
9351 "P2P: Generated NFC connection handover message", ret);
9352
9353 if (ndef && ret) {
9354 struct wpabuf *tmp;
9355 tmp = ndef_build_p2p(ret);
9356 wpabuf_free(ret);
9357 if (tmp == NULL) {
9358 wpa_printf(MSG_DEBUG, "P2P: Failed to NDEF encapsulate handover request");
9359 return NULL;
9360 }
9361 ret = tmp;
9362 }
9363
9364 return ret;
9365}
9366
9367
9368static int wpas_p2p_cli_freq(struct wpa_supplicant *wpa_s,
9369 struct wpa_ssid **ssid, u8 *go_dev_addr)
9370{
9371 struct wpa_supplicant *iface;
9372
9373 if (go_dev_addr)
9374 os_memset(go_dev_addr, 0, ETH_ALEN);
9375 if (ssid)
9376 *ssid = NULL;
9377 for (iface = wpa_s->global->ifaces; iface; iface = iface->next) {
9378 if (iface->wpa_state < WPA_ASSOCIATING ||
9379 iface->current_ssid == NULL || iface->assoc_freq == 0 ||
9380 !iface->current_ssid->p2p_group ||
9381 iface->current_ssid->mode != WPAS_MODE_INFRA)
9382 continue;
9383 if (ssid)
9384 *ssid = iface->current_ssid;
9385 if (go_dev_addr)
9386 os_memcpy(go_dev_addr, iface->go_dev_addr, ETH_ALEN);
9387 return iface->assoc_freq;
9388 }
9389 return 0;
9390}
9391
9392
9393struct wpabuf * wpas_p2p_nfc_handover_req(struct wpa_supplicant *wpa_s,
9394 int ndef)
9395{
9396 struct wpabuf *wsc, *p2p;
9397 struct wpa_ssid *ssid;
9398 u8 go_dev_addr[ETH_ALEN];
9399 int cli_freq = wpas_p2p_cli_freq(wpa_s, &ssid, go_dev_addr);
9400
9401 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL) {
9402 wpa_printf(MSG_DEBUG, "P2P: P2P disabled - cannot build handover request");
9403 return NULL;
9404 }
9405
9406 if (wpa_s->conf->wps_nfc_dh_pubkey == NULL &&
9407 wps_nfc_gen_dh(&wpa_s->conf->wps_nfc_dh_pubkey,
9408 &wpa_s->conf->wps_nfc_dh_privkey) < 0) {
9409 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: No DH key available for handover request");
9410 return NULL;
9411 }
9412
9413 if (cli_freq == 0) {
9414 wsc = wps_build_nfc_handover_req_p2p(
9415 wpa_s->parent->wps, wpa_s->conf->wps_nfc_dh_pubkey);
9416 } else
9417 wsc = NULL;
9418 p2p = p2p_build_nfc_handover_req(wpa_s->global->p2p, cli_freq,
9419 go_dev_addr, ssid ? ssid->ssid : NULL,
9420 ssid ? ssid->ssid_len : 0);
9421
9422 return wpas_p2p_nfc_handover(ndef, wsc, p2p);
9423}
9424
9425
9426struct wpabuf * wpas_p2p_nfc_handover_sel(struct wpa_supplicant *wpa_s,
9427 int ndef, int tag)
9428{
9429 struct wpabuf *wsc, *p2p;
9430 struct wpa_ssid *ssid;
9431 u8 go_dev_addr[ETH_ALEN];
9432 int cli_freq = wpas_p2p_cli_freq(wpa_s, &ssid, go_dev_addr);
9433
9434 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
9435 return NULL;
9436
9437 if (!tag && wpa_s->conf->wps_nfc_dh_pubkey == NULL &&
9438 wps_nfc_gen_dh(&wpa_s->conf->wps_nfc_dh_pubkey,
9439 &wpa_s->conf->wps_nfc_dh_privkey) < 0)
9440 return NULL;
9441
9442 if (cli_freq == 0) {
9443 wsc = wps_build_nfc_handover_sel_p2p(
9444 wpa_s->parent->wps,
9445 tag ? wpa_s->conf->wps_nfc_dev_pw_id :
9446 DEV_PW_NFC_CONNECTION_HANDOVER,
9447 wpa_s->conf->wps_nfc_dh_pubkey,
9448 tag ? wpa_s->conf->wps_nfc_dev_pw : NULL);
9449 } else
9450 wsc = NULL;
9451 p2p = p2p_build_nfc_handover_sel(wpa_s->global->p2p, cli_freq,
9452 go_dev_addr, ssid ? ssid->ssid : NULL,
9453 ssid ? ssid->ssid_len : 0);
9454
9455 return wpas_p2p_nfc_handover(ndef, wsc, p2p);
9456}
9457
9458
9459static int wpas_p2p_nfc_join_group(struct wpa_supplicant *wpa_s,
9460 struct p2p_nfc_params *params)
9461{
9462 wpa_printf(MSG_DEBUG, "P2P: Initiate join-group based on NFC "
9463 "connection handover (freq=%d)",
9464 params->go_freq);
9465
9466 if (params->go_freq && params->go_ssid_len) {
9467 wpa_s->p2p_wps_method = WPS_NFC;
9468 wpa_s->pending_join_wps_method = WPS_NFC;
9469 os_memset(wpa_s->pending_join_iface_addr, 0, ETH_ALEN);
9470 os_memcpy(wpa_s->pending_join_dev_addr, params->go_dev_addr,
9471 ETH_ALEN);
9472 return wpas_p2p_join_start(wpa_s, params->go_freq,
9473 params->go_ssid,
9474 params->go_ssid_len);
9475 }
9476
9477 return wpas_p2p_connect(wpa_s, params->peer->p2p_device_addr, NULL,
9478 WPS_NFC, 0, 0, 1, 0, wpa_s->conf->p2p_go_intent,
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08009479 params->go_freq, wpa_s->p2p_go_vht_center_freq2,
Dmitry Shmidtde47be72016-01-07 12:52:55 -08009480 -1, 0, 1, 1, wpa_s->p2p_go_max_oper_chwidth,
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08009481 wpa_s->p2p_go_he, wpa_s->p2p_go_edmg,
Dmitry Shmidtde47be72016-01-07 12:52:55 -08009482 params->go_ssid_len ? params->go_ssid : NULL,
Sunil Ravic0f5d412024-09-11 22:12:49 +00009483 params->go_ssid_len, false, wpa_s->p2p2,
9484 wpa_s->p2p_bootstrap, NULL);
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08009485}
9486
9487
9488static int wpas_p2p_nfc_auth_join(struct wpa_supplicant *wpa_s,
9489 struct p2p_nfc_params *params, int tag)
9490{
9491 int res, persistent;
9492 struct wpa_ssid *ssid;
9493
9494 wpa_printf(MSG_DEBUG, "P2P: Authorize join-group based on NFC "
9495 "connection handover");
9496 for (wpa_s = wpa_s->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
9497 ssid = wpa_s->current_ssid;
9498 if (ssid == NULL)
9499 continue;
9500 if (ssid->mode != WPAS_MODE_P2P_GO)
9501 continue;
9502 if (wpa_s->ap_iface == NULL)
9503 continue;
9504 break;
9505 }
9506 if (wpa_s == NULL) {
9507 wpa_printf(MSG_DEBUG, "P2P: Could not find GO interface");
9508 return -1;
9509 }
9510
Dmitry Shmidt849734c2016-05-27 09:59:01 -07009511 if (wpa_s->p2pdev->p2p_oob_dev_pw_id !=
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08009512 DEV_PW_NFC_CONNECTION_HANDOVER &&
Dmitry Shmidt849734c2016-05-27 09:59:01 -07009513 !wpa_s->p2pdev->p2p_oob_dev_pw) {
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08009514 wpa_printf(MSG_DEBUG, "P2P: No NFC Dev Pw known");
9515 return -1;
9516 }
9517 res = wpas_ap_wps_add_nfc_pw(
Dmitry Shmidt849734c2016-05-27 09:59:01 -07009518 wpa_s, wpa_s->p2pdev->p2p_oob_dev_pw_id,
9519 wpa_s->p2pdev->p2p_oob_dev_pw,
9520 wpa_s->p2pdev->p2p_peer_oob_pk_hash_known ?
9521 wpa_s->p2pdev->p2p_peer_oob_pubkey_hash : NULL);
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08009522 if (res)
9523 return res;
9524
9525 if (!tag) {
9526 wpa_printf(MSG_DEBUG, "P2P: Negotiated handover - wait for peer to join without invitation");
9527 return 0;
9528 }
9529
9530 if (!params->peer ||
9531 !(params->peer->dev_capab & P2P_DEV_CAPAB_INVITATION_PROCEDURE))
9532 return 0;
9533
9534 wpa_printf(MSG_DEBUG, "P2P: Static handover - invite peer " MACSTR
9535 " to join", MAC2STR(params->peer->p2p_device_addr));
9536
9537 wpa_s->global->p2p_invite_group = wpa_s;
9538 persistent = ssid->p2p_persistent_group &&
Dmitry Shmidt849734c2016-05-27 09:59:01 -07009539 wpas_p2p_get_persistent(wpa_s->p2pdev,
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08009540 params->peer->p2p_device_addr,
9541 ssid->ssid, ssid->ssid_len);
Dmitry Shmidt849734c2016-05-27 09:59:01 -07009542 wpa_s->p2pdev->pending_invite_ssid_id = -1;
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08009543
9544 return p2p_invite(wpa_s->global->p2p, params->peer->p2p_device_addr,
9545 P2P_INVITE_ROLE_ACTIVE_GO, wpa_s->own_addr,
9546 ssid->ssid, ssid->ssid_len, ssid->frequency,
9547 wpa_s->global->p2p_dev_addr, persistent, 0,
Dmitry Shmidt849734c2016-05-27 09:59:01 -07009548 wpa_s->p2pdev->p2p_oob_dev_pw_id);
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08009549}
9550
9551
9552static int wpas_p2p_nfc_init_go_neg(struct wpa_supplicant *wpa_s,
9553 struct p2p_nfc_params *params,
9554 int forced_freq)
9555{
9556 wpa_printf(MSG_DEBUG, "P2P: Initiate GO Negotiation based on NFC "
9557 "connection handover");
9558 return wpas_p2p_connect(wpa_s, params->peer->p2p_device_addr, NULL,
9559 WPS_NFC, 0, 0, 0, 0, wpa_s->conf->p2p_go_intent,
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08009560 forced_freq, wpa_s->p2p_go_vht_center_freq2,
Dmitry Shmidtde47be72016-01-07 12:52:55 -08009561 -1, 0, 1, 1, wpa_s->p2p_go_max_oper_chwidth,
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08009562 wpa_s->p2p_go_he, wpa_s->p2p_go_edmg,
Sunil Ravic0f5d412024-09-11 22:12:49 +00009563 NULL, 0, false, wpa_s->p2p2,
9564 wpa_s->p2p_bootstrap, NULL);
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08009565}
9566
9567
9568static int wpas_p2p_nfc_resp_go_neg(struct wpa_supplicant *wpa_s,
9569 struct p2p_nfc_params *params,
9570 int forced_freq)
9571{
9572 int res;
9573
9574 wpa_printf(MSG_DEBUG, "P2P: Authorize GO Negotiation based on NFC "
9575 "connection handover");
9576 res = wpas_p2p_connect(wpa_s, params->peer->p2p_device_addr, NULL,
9577 WPS_NFC, 0, 0, 0, 1, wpa_s->conf->p2p_go_intent,
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08009578 forced_freq, wpa_s->p2p_go_vht_center_freq2,
Dmitry Shmidtde47be72016-01-07 12:52:55 -08009579 -1, 0, 1, 1, wpa_s->p2p_go_max_oper_chwidth,
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08009580 wpa_s->p2p_go_he, wpa_s->p2p_go_edmg,
Sunil Ravic0f5d412024-09-11 22:12:49 +00009581 NULL, 0, false, wpa_s->p2p2,
9582 wpa_s->p2p_bootstrap, NULL);
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08009583 if (res)
9584 return res;
9585
9586 res = wpas_p2p_listen(wpa_s, 60);
9587 if (res) {
9588 p2p_unauthorize(wpa_s->global->p2p,
9589 params->peer->p2p_device_addr);
9590 }
9591
9592 return res;
9593}
9594
9595
9596static int wpas_p2p_nfc_connection_handover(struct wpa_supplicant *wpa_s,
9597 const struct wpabuf *data,
9598 int sel, int tag, int forced_freq)
9599{
9600 const u8 *pos, *end;
9601 u16 len, id;
9602 struct p2p_nfc_params params;
9603 int res;
9604
9605 os_memset(&params, 0, sizeof(params));
9606 params.sel = sel;
9607
9608 wpa_hexdump_buf(MSG_DEBUG, "P2P: Received NFC tag payload", data);
9609
9610 pos = wpabuf_head(data);
9611 end = pos + wpabuf_len(data);
9612
9613 if (end - pos < 2) {
9614 wpa_printf(MSG_DEBUG, "P2P: Not enough data for Length of WSC "
9615 "attributes");
9616 return -1;
9617 }
9618 len = WPA_GET_BE16(pos);
9619 pos += 2;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08009620 if (len > end - pos) {
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08009621 wpa_printf(MSG_DEBUG, "P2P: Not enough data for WSC "
9622 "attributes");
9623 return -1;
9624 }
9625 params.wsc_attr = pos;
9626 params.wsc_len = len;
9627 pos += len;
9628
9629 if (end - pos < 2) {
9630 wpa_printf(MSG_DEBUG, "P2P: Not enough data for Length of P2P "
9631 "attributes");
9632 return -1;
9633 }
9634 len = WPA_GET_BE16(pos);
9635 pos += 2;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08009636 if (len > end - pos) {
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08009637 wpa_printf(MSG_DEBUG, "P2P: Not enough data for P2P "
9638 "attributes");
9639 return -1;
9640 }
9641 params.p2p_attr = pos;
9642 params.p2p_len = len;
9643 pos += len;
9644
9645 wpa_hexdump(MSG_DEBUG, "P2P: WSC attributes",
9646 params.wsc_attr, params.wsc_len);
9647 wpa_hexdump(MSG_DEBUG, "P2P: P2P attributes",
9648 params.p2p_attr, params.p2p_len);
9649 if (pos < end) {
9650 wpa_hexdump(MSG_DEBUG,
9651 "P2P: Ignored extra data after P2P attributes",
9652 pos, end - pos);
9653 }
9654
9655 res = p2p_process_nfc_connection_handover(wpa_s->global->p2p, &params);
9656 if (res)
9657 return res;
9658
9659 if (params.next_step == NO_ACTION)
9660 return 0;
9661
9662 if (params.next_step == BOTH_GO) {
9663 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_NFC_BOTH_GO "peer=" MACSTR,
9664 MAC2STR(params.peer->p2p_device_addr));
9665 return 0;
9666 }
9667
9668 if (params.next_step == PEER_CLIENT) {
9669 if (!is_zero_ether_addr(params.go_dev_addr)) {
9670 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_NFC_PEER_CLIENT
9671 "peer=" MACSTR " freq=%d go_dev_addr=" MACSTR
9672 " ssid=\"%s\"",
9673 MAC2STR(params.peer->p2p_device_addr),
9674 params.go_freq,
9675 MAC2STR(params.go_dev_addr),
9676 wpa_ssid_txt(params.go_ssid,
9677 params.go_ssid_len));
9678 } else {
9679 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_NFC_PEER_CLIENT
9680 "peer=" MACSTR " freq=%d",
9681 MAC2STR(params.peer->p2p_device_addr),
9682 params.go_freq);
9683 }
9684 return 0;
9685 }
9686
9687 if (wpas_p2p_cli_freq(wpa_s, NULL, NULL)) {
9688 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_NFC_WHILE_CLIENT "peer="
9689 MACSTR, MAC2STR(params.peer->p2p_device_addr));
9690 return 0;
9691 }
9692
9693 wpabuf_free(wpa_s->p2p_oob_dev_pw);
9694 wpa_s->p2p_oob_dev_pw = NULL;
9695
9696 if (params.oob_dev_pw_len < WPS_OOB_PUBKEY_HASH_LEN + 2) {
9697 wpa_printf(MSG_DEBUG, "P2P: No peer OOB Dev Pw "
9698 "received");
9699 return -1;
9700 }
9701
9702 id = WPA_GET_BE16(params.oob_dev_pw + WPS_OOB_PUBKEY_HASH_LEN);
9703 wpa_printf(MSG_DEBUG, "P2P: Peer OOB Dev Pw %u", id);
9704 wpa_hexdump(MSG_DEBUG, "P2P: Peer OOB Public Key hash",
9705 params.oob_dev_pw, WPS_OOB_PUBKEY_HASH_LEN);
9706 os_memcpy(wpa_s->p2p_peer_oob_pubkey_hash,
9707 params.oob_dev_pw, WPS_OOB_PUBKEY_HASH_LEN);
9708 wpa_s->p2p_peer_oob_pk_hash_known = 1;
9709
9710 if (tag) {
9711 if (id < 0x10) {
9712 wpa_printf(MSG_DEBUG, "P2P: Static handover - invalid "
9713 "peer OOB Device Password Id %u", id);
9714 return -1;
9715 }
9716 wpa_printf(MSG_DEBUG, "P2P: Static handover - use peer OOB "
9717 "Device Password Id %u", id);
9718 wpa_hexdump_key(MSG_DEBUG, "P2P: Peer OOB Device Password",
9719 params.oob_dev_pw + WPS_OOB_PUBKEY_HASH_LEN + 2,
9720 params.oob_dev_pw_len -
9721 WPS_OOB_PUBKEY_HASH_LEN - 2);
9722 wpa_s->p2p_oob_dev_pw_id = id;
9723 wpa_s->p2p_oob_dev_pw = wpabuf_alloc_copy(
9724 params.oob_dev_pw + WPS_OOB_PUBKEY_HASH_LEN + 2,
9725 params.oob_dev_pw_len -
9726 WPS_OOB_PUBKEY_HASH_LEN - 2);
9727 if (wpa_s->p2p_oob_dev_pw == NULL)
9728 return -1;
9729
9730 if (wpa_s->conf->wps_nfc_dh_pubkey == NULL &&
9731 wps_nfc_gen_dh(&wpa_s->conf->wps_nfc_dh_pubkey,
9732 &wpa_s->conf->wps_nfc_dh_privkey) < 0)
9733 return -1;
9734 } else {
9735 wpa_printf(MSG_DEBUG, "P2P: Using abbreviated WPS handshake "
9736 "without Device Password");
9737 wpa_s->p2p_oob_dev_pw_id = DEV_PW_NFC_CONNECTION_HANDOVER;
9738 }
9739
9740 switch (params.next_step) {
9741 case NO_ACTION:
9742 case BOTH_GO:
9743 case PEER_CLIENT:
9744 /* already covered above */
9745 return 0;
9746 case JOIN_GROUP:
9747 return wpas_p2p_nfc_join_group(wpa_s, &params);
9748 case AUTH_JOIN:
9749 return wpas_p2p_nfc_auth_join(wpa_s, &params, tag);
9750 case INIT_GO_NEG:
9751 return wpas_p2p_nfc_init_go_neg(wpa_s, &params, forced_freq);
9752 case RESP_GO_NEG:
9753 /* TODO: use own OOB Dev Pw */
9754 return wpas_p2p_nfc_resp_go_neg(wpa_s, &params, forced_freq);
9755 }
9756
9757 return -1;
9758}
9759
9760
9761int wpas_p2p_nfc_tag_process(struct wpa_supplicant *wpa_s,
9762 const struct wpabuf *data, int forced_freq)
9763{
9764 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
9765 return -1;
9766
9767 return wpas_p2p_nfc_connection_handover(wpa_s, data, 1, 1, forced_freq);
9768}
9769
9770
9771int wpas_p2p_nfc_report_handover(struct wpa_supplicant *wpa_s, int init,
9772 const struct wpabuf *req,
9773 const struct wpabuf *sel, int forced_freq)
9774{
9775 struct wpabuf *tmp;
9776 int ret;
9777
9778 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
9779 return -1;
9780
9781 wpa_printf(MSG_DEBUG, "NFC: P2P connection handover reported");
9782
9783 wpa_hexdump_ascii(MSG_DEBUG, "NFC: Req",
9784 wpabuf_head(req), wpabuf_len(req));
9785 wpa_hexdump_ascii(MSG_DEBUG, "NFC: Sel",
9786 wpabuf_head(sel), wpabuf_len(sel));
9787 if (forced_freq)
9788 wpa_printf(MSG_DEBUG, "NFC: Forced freq %d", forced_freq);
9789 tmp = ndef_parse_p2p(init ? sel : req);
9790 if (tmp == NULL) {
9791 wpa_printf(MSG_DEBUG, "P2P: Could not parse NDEF");
9792 return -1;
9793 }
9794
9795 ret = wpas_p2p_nfc_connection_handover(wpa_s, tmp, init, 0,
9796 forced_freq);
9797 wpabuf_free(tmp);
9798
9799 return ret;
9800}
9801
9802
9803int wpas_p2p_nfc_tag_enabled(struct wpa_supplicant *wpa_s, int enabled)
9804{
9805 const u8 *if_addr;
9806 int go_intent = wpa_s->conf->p2p_go_intent;
9807 struct wpa_supplicant *iface;
9808
9809 if (wpa_s->global->p2p == NULL)
9810 return -1;
9811
9812 if (!enabled) {
9813 wpa_printf(MSG_DEBUG, "P2P: Disable use of own NFC Tag");
9814 for (iface = wpa_s->global->ifaces; iface; iface = iface->next)
9815 {
9816 if (!iface->ap_iface)
9817 continue;
9818 hostapd_wps_nfc_token_disable(iface->ap_iface->bss[0]);
9819 }
9820 p2p_set_authorized_oob_dev_pw_id(wpa_s->global->p2p, 0,
9821 0, NULL);
9822 if (wpa_s->p2p_nfc_tag_enabled)
9823 wpas_p2p_remove_pending_group_interface(wpa_s);
9824 wpa_s->p2p_nfc_tag_enabled = 0;
9825 return 0;
9826 }
9827
9828 if (wpa_s->global->p2p_disabled)
9829 return -1;
9830
9831 if (wpa_s->conf->wps_nfc_dh_pubkey == NULL ||
9832 wpa_s->conf->wps_nfc_dh_privkey == NULL ||
9833 wpa_s->conf->wps_nfc_dev_pw == NULL ||
9834 wpa_s->conf->wps_nfc_dev_pw_id < 0x10) {
9835 wpa_printf(MSG_DEBUG, "P2P: NFC password token not configured "
9836 "to allow static handover cases");
9837 return -1;
9838 }
9839
9840 wpa_printf(MSG_DEBUG, "P2P: Enable use of own NFC Tag");
9841
9842 wpa_s->p2p_oob_dev_pw_id = wpa_s->conf->wps_nfc_dev_pw_id;
9843 wpabuf_free(wpa_s->p2p_oob_dev_pw);
9844 wpa_s->p2p_oob_dev_pw = wpabuf_dup(wpa_s->conf->wps_nfc_dev_pw);
9845 if (wpa_s->p2p_oob_dev_pw == NULL)
9846 return -1;
9847 wpa_s->p2p_peer_oob_pk_hash_known = 0;
9848
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -07009849 if (wpa_s->p2p_group_interface == P2P_GROUP_INTERFACE_GO ||
9850 wpa_s->p2p_group_interface == P2P_GROUP_INTERFACE_CLIENT) {
9851 /*
9852 * P2P Group Interface present and the command came on group
9853 * interface, so enable the token for the current interface.
9854 */
9855 wpa_s->create_p2p_iface = 0;
9856 } else {
9857 wpa_s->create_p2p_iface = wpas_p2p_create_iface(wpa_s);
9858 }
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08009859
9860 if (wpa_s->create_p2p_iface) {
9861 enum wpa_driver_if_type iftype;
9862 /* Prepare to add a new interface for the group */
9863 iftype = WPA_IF_P2P_GROUP;
9864 if (go_intent == 15)
9865 iftype = WPA_IF_P2P_GO;
9866 if (wpas_p2p_add_group_interface(wpa_s, iftype) < 0) {
9867 wpa_printf(MSG_ERROR, "P2P: Failed to allocate a new "
9868 "interface for the group");
9869 return -1;
9870 }
9871
9872 if_addr = wpa_s->pending_interface_addr;
Dmitry Shmidt9c175262016-03-03 10:20:07 -08009873 } else if (wpa_s->p2p_mgmt)
9874 if_addr = wpa_s->parent->own_addr;
9875 else
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08009876 if_addr = wpa_s->own_addr;
9877
9878 wpa_s->p2p_nfc_tag_enabled = enabled;
9879
9880 for (iface = wpa_s->global->ifaces; iface; iface = iface->next) {
9881 struct hostapd_data *hapd;
9882 if (iface->ap_iface == NULL)
9883 continue;
9884 hapd = iface->ap_iface->bss[0];
9885 wpabuf_free(hapd->conf->wps_nfc_dh_pubkey);
9886 hapd->conf->wps_nfc_dh_pubkey =
9887 wpabuf_dup(wpa_s->conf->wps_nfc_dh_pubkey);
9888 wpabuf_free(hapd->conf->wps_nfc_dh_privkey);
9889 hapd->conf->wps_nfc_dh_privkey =
9890 wpabuf_dup(wpa_s->conf->wps_nfc_dh_privkey);
9891 wpabuf_free(hapd->conf->wps_nfc_dev_pw);
9892 hapd->conf->wps_nfc_dev_pw =
9893 wpabuf_dup(wpa_s->conf->wps_nfc_dev_pw);
9894 hapd->conf->wps_nfc_dev_pw_id = wpa_s->conf->wps_nfc_dev_pw_id;
9895
9896 if (hostapd_wps_nfc_token_enable(iface->ap_iface->bss[0]) < 0) {
9897 wpa_dbg(iface, MSG_DEBUG,
9898 "P2P: Failed to enable NFC Tag for GO");
9899 }
9900 }
9901 p2p_set_authorized_oob_dev_pw_id(
9902 wpa_s->global->p2p, wpa_s->conf->wps_nfc_dev_pw_id, go_intent,
9903 if_addr);
9904
9905 return 0;
9906}
9907
9908#endif /* CONFIG_WPS_NFC */
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07009909
9910
9911static void wpas_p2p_optimize_listen_channel(struct wpa_supplicant *wpa_s,
9912 struct wpa_used_freq_data *freqs,
9913 unsigned int num)
9914{
9915 u8 curr_chan, cand, chan;
9916 unsigned int i;
9917
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08009918 /*
9919 * If possible, optimize the Listen channel to be a channel that is
9920 * already used by one of the other interfaces.
9921 */
9922 if (!wpa_s->conf->p2p_optimize_listen_chan)
9923 return;
9924
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07009925 curr_chan = p2p_get_listen_channel(wpa_s->global->p2p);
9926 for (i = 0, cand = 0; i < num; i++) {
9927 ieee80211_freq_to_chan(freqs[i].freq, &chan);
9928 if (curr_chan == chan) {
9929 cand = 0;
9930 break;
9931 }
9932
9933 if (chan == 1 || chan == 6 || chan == 11)
9934 cand = chan;
9935 }
9936
9937 if (cand) {
9938 wpa_dbg(wpa_s, MSG_DEBUG,
Dmitry Shmidt216983b2015-02-06 10:50:36 -08009939 "P2P: Update Listen channel to %u based on operating channel",
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07009940 cand);
9941 p2p_set_listen_channel(wpa_s->global->p2p, 81, cand, 0);
9942 }
9943}
9944
9945
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08009946static int wpas_p2p_move_go_csa(struct wpa_supplicant *wpa_s)
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07009947{
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08009948 struct hostapd_config *conf;
9949 struct p2p_go_neg_results params;
9950 struct csa_settings csa_settings;
9951 struct wpa_ssid *current_ssid = wpa_s->current_ssid;
9952 int old_freq = current_ssid->frequency;
9953 int ret;
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07009954
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08009955 if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_AP_CSA)) {
9956 wpa_dbg(wpa_s, MSG_DEBUG, "CSA is not enabled");
9957 return -1;
9958 }
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07009959
9960 /*
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08009961 * TODO: This function may not always work correctly. For example,
9962 * when we have a running GO and a BSS on a DFS channel.
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07009963 */
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08009964 if (wpas_p2p_init_go_params(wpa_s, &params, 0, 0, 0, 0, 0, 0, 0,
9965 NULL)) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08009966 wpa_dbg(wpa_s, MSG_DEBUG,
9967 "P2P CSA: Failed to select new frequency for GO");
9968 return -1;
9969 }
9970
9971 if (current_ssid->frequency == params.freq) {
9972 wpa_dbg(wpa_s, MSG_DEBUG,
9973 "P2P CSA: Selected same frequency - not moving GO");
9974 return 0;
9975 }
9976
9977 conf = hostapd_config_defaults();
9978 if (!conf) {
9979 wpa_dbg(wpa_s, MSG_DEBUG,
9980 "P2P CSA: Failed to allocate default config");
9981 return -1;
9982 }
9983
9984 current_ssid->frequency = params.freq;
9985 if (wpa_supplicant_conf_ap_ht(wpa_s, current_ssid, conf)) {
9986 wpa_dbg(wpa_s, MSG_DEBUG,
9987 "P2P CSA: Failed to create new GO config");
9988 ret = -1;
9989 goto out;
9990 }
9991
Sunil4a3f9f52022-07-19 22:04:39 +00009992 if (conf->hw_mode != wpa_s->ap_iface->current_mode->mode &&
9993 (wpa_s->ap_iface->current_mode->mode != HOSTAPD_MODE_IEEE80211A ||
Sunil Ravi89eba102022-09-13 21:04:37 -07009994 is_6ghz_freq(wpa_s->ap_iface->freq) ||
Sunil4a3f9f52022-07-19 22:04:39 +00009995 conf->hw_mode != HOSTAPD_MODE_IEEE80211G)) {
9996 wpa_dbg(wpa_s, MSG_INFO,
Sunil Ravi89eba102022-09-13 21:04:37 -07009997 "P2P CSA: CSA from hardware mode %d%s to %d is not supported",
9998 wpa_s->ap_iface->current_mode->mode,
9999 is_6ghz_freq(wpa_s->ap_iface->freq) ? " (6 GHz)" : "",
10000 conf->hw_mode);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -080010001 ret = -1;
10002 goto out;
10003 }
10004
10005 os_memset(&csa_settings, 0, sizeof(csa_settings));
10006 csa_settings.cs_count = P2P_GO_CSA_COUNT;
10007 csa_settings.block_tx = P2P_GO_CSA_BLOCK_TX;
Sunil Ravib0ac25f2024-07-12 01:42:03 +000010008 csa_settings.link_id = -1;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -080010009 csa_settings.freq_params.freq = params.freq;
10010 csa_settings.freq_params.sec_channel_offset = conf->secondary_channel;
10011 csa_settings.freq_params.ht_enabled = conf->ieee80211n;
10012 csa_settings.freq_params.bandwidth = conf->secondary_channel ? 40 : 20;
10013
10014 if (conf->ieee80211ac) {
10015 int freq1 = 0, freq2 = 0;
10016 u8 chan, opclass;
10017
10018 if (ieee80211_freq_to_channel_ext(params.freq,
10019 conf->secondary_channel,
10020 conf->vht_oper_chwidth,
10021 &opclass, &chan) ==
10022 NUM_HOSTAPD_MODES) {
10023 wpa_printf(MSG_ERROR, "P2P CSA: Bad freq");
10024 ret = -1;
10025 goto out;
10026 }
10027
10028 if (conf->vht_oper_centr_freq_seg0_idx)
10029 freq1 = ieee80211_chan_to_freq(
10030 NULL, opclass,
10031 conf->vht_oper_centr_freq_seg0_idx);
10032
10033 if (conf->vht_oper_centr_freq_seg1_idx)
10034 freq2 = ieee80211_chan_to_freq(
10035 NULL, opclass,
10036 conf->vht_oper_centr_freq_seg1_idx);
10037
10038 if (freq1 < 0 || freq2 < 0) {
10039 wpa_dbg(wpa_s, MSG_DEBUG,
10040 "P2P CSA: Selected invalid VHT center freqs");
10041 ret = -1;
10042 goto out;
10043 }
10044
10045 csa_settings.freq_params.vht_enabled = conf->ieee80211ac;
10046 csa_settings.freq_params.center_freq1 = freq1;
10047 csa_settings.freq_params.center_freq2 = freq2;
10048
10049 switch (conf->vht_oper_chwidth) {
Sunil8cd6f4d2022-06-28 18:40:46 +000010050 case CONF_OPER_CHWIDTH_80MHZ:
10051 case CONF_OPER_CHWIDTH_80P80MHZ:
Dmitry Shmidtd80a4012015-11-05 16:35:40 -080010052 csa_settings.freq_params.bandwidth = 80;
10053 break;
Sunil8cd6f4d2022-06-28 18:40:46 +000010054 case CONF_OPER_CHWIDTH_160MHZ:
Dmitry Shmidtd80a4012015-11-05 16:35:40 -080010055 csa_settings.freq_params.bandwidth = 160;
10056 break;
Sunil8cd6f4d2022-06-28 18:40:46 +000010057 default:
10058 break;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -080010059 }
10060 }
10061
10062 ret = ap_switch_channel(wpa_s, &csa_settings);
10063out:
10064 current_ssid->frequency = old_freq;
10065 hostapd_config_free(conf);
10066 return ret;
10067}
10068
10069
10070static void wpas_p2p_move_go_no_csa(struct wpa_supplicant *wpa_s)
10071{
10072 struct p2p_go_neg_results params;
10073 struct wpa_ssid *current_ssid = wpa_s->current_ssid;
Hai Shalom899fcc72020-10-19 14:38:18 -070010074 void (*ap_configured_cb)(void *ctx, void *data);
10075 void *ap_configured_cb_ctx, *ap_configured_cb_data;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -080010076
10077 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_REMOVE_AND_REFORM_GROUP);
10078
10079 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Move GO from freq=%d MHz",
10080 current_ssid->frequency);
10081
10082 /* Stop the AP functionality */
10083 /* TODO: Should do this in a way that does not indicated to possible
10084 * P2P Clients in the group that the group is terminated. */
Hai Shalom899fcc72020-10-19 14:38:18 -070010085 /* If this action occurs before a group is started, the callback should
10086 * be preserved, or GROUP-STARTED event would be lost. If this action
10087 * occurs after a group is started, these pointers are all NULL and
10088 * harmless. */
10089 ap_configured_cb = wpa_s->ap_configured_cb;
10090 ap_configured_cb_ctx = wpa_s->ap_configured_cb_ctx;
10091 ap_configured_cb_data = wpa_s->ap_configured_cb_data;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -080010092 wpa_supplicant_ap_deinit(wpa_s);
10093
10094 /* Reselect the GO frequency */
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -080010095 if (wpas_p2p_init_go_params(wpa_s, &params, 0, 0, 0, 0, 0, 0, 0,
10096 NULL)) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -080010097 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Failed to reselect freq");
10098 wpas_p2p_group_delete(wpa_s,
10099 P2P_GROUP_REMOVAL_GO_LEAVE_CHANNEL);
10100 return;
10101 }
10102 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: New freq selected for the GO (%u MHz)",
10103 params.freq);
10104
10105 if (params.freq &&
10106 !p2p_supported_freq_go(wpa_s->global->p2p, params.freq)) {
10107 wpa_printf(MSG_DEBUG,
10108 "P2P: Selected freq (%u MHz) is not valid for P2P",
10109 params.freq);
10110 wpas_p2p_group_delete(wpa_s,
10111 P2P_GROUP_REMOVAL_GO_LEAVE_CHANNEL);
10112 return;
10113 }
10114
Hai Shalom899fcc72020-10-19 14:38:18 -070010115 /* Restore preserved callback parameters */
Jimmy Chen4b2a5022020-08-26 18:55:45 +080010116 wpa_s->ap_configured_cb = ap_configured_cb;
10117 wpa_s->ap_configured_cb_ctx = ap_configured_cb_ctx;
10118 wpa_s->ap_configured_cb_data = ap_configured_cb_data;
Hai Shalom899fcc72020-10-19 14:38:18 -070010119
Dmitry Shmidtd80a4012015-11-05 16:35:40 -080010120 /* Update the frequency */
10121 current_ssid->frequency = params.freq;
10122 wpa_s->connect_without_scan = current_ssid;
10123 wpa_s->reassociate = 1;
10124 wpa_s->disconnected = 0;
10125 wpa_supplicant_req_scan(wpa_s, 0, 0);
10126}
10127
10128
10129static void wpas_p2p_move_go(void *eloop_ctx, void *timeout_ctx)
10130{
10131 struct wpa_supplicant *wpa_s = eloop_ctx;
10132
10133 if (!wpa_s->ap_iface || !wpa_s->current_ssid)
Dmitry Shmidt43cb5782014-06-16 16:23:22 -070010134 return;
10135
Dmitry Shmidtd80a4012015-11-05 16:35:40 -080010136 wpas_p2p_go_update_common_freqs(wpa_s);
10137
10138 /* Do not move GO in the middle of a CSA */
10139 if (hostapd_csa_in_progress(wpa_s->ap_iface)) {
10140 wpa_printf(MSG_DEBUG,
10141 "P2P: CSA is in progress - not moving GO");
Dmitry Shmidt43cb5782014-06-16 16:23:22 -070010142 return;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -080010143 }
10144
10145 /*
10146 * First, try a channel switch flow. If it is not supported or fails,
10147 * take down the GO and bring it up again.
10148 */
10149 if (wpas_p2p_move_go_csa(wpa_s) < 0)
10150 wpas_p2p_move_go_no_csa(wpa_s);
10151}
10152
10153
10154static void wpas_p2p_reconsider_moving_go(void *eloop_ctx, void *timeout_ctx)
10155{
10156 struct wpa_supplicant *wpa_s = eloop_ctx;
10157 struct wpa_used_freq_data *freqs = NULL;
10158 unsigned int num = wpa_s->num_multichan_concurrent;
Dmitry Shmidt43cb5782014-06-16 16:23:22 -070010159
10160 freqs = os_calloc(num, sizeof(struct wpa_used_freq_data));
10161 if (!freqs)
10162 return;
10163
Sunil Ravi77d572f2023-01-17 23:58:31 +000010164 num = get_shared_radio_freqs_data(wpa_s, freqs, num, false);
Dmitry Shmidt43cb5782014-06-16 16:23:22 -070010165
Dmitry Shmidtd80a4012015-11-05 16:35:40 -080010166 /* Previous attempt to move a GO was not possible -- try again. */
10167 wpas_p2p_consider_moving_gos(wpa_s, freqs, num,
10168 WPAS_P2P_CHANNEL_UPDATE_ANY);
10169
Dmitry Shmidt43cb5782014-06-16 16:23:22 -070010170 os_free(freqs);
10171}
10172
10173
Dmitry Shmidtd80a4012015-11-05 16:35:40 -080010174/*
10175 * Consider moving a GO from its currently used frequency:
10176 * 1. It is possible that due to regulatory consideration the frequency
10177 * can no longer be used and there is a need to evacuate the GO.
10178 * 2. It is possible that due to MCC considerations, it would be preferable
10179 * to move the GO to a channel that is currently used by some other
10180 * station interface.
10181 *
10182 * In case a frequency that became invalid is once again valid, cancel a
10183 * previously initiated GO frequency change.
10184 */
10185static void wpas_p2p_consider_moving_one_go(struct wpa_supplicant *wpa_s,
10186 struct wpa_used_freq_data *freqs,
10187 unsigned int num)
10188{
10189 unsigned int i, invalid_freq = 0, policy_move = 0, flags = 0;
10190 unsigned int timeout;
10191 int freq;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -070010192 int dfs_offload;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -080010193
10194 wpas_p2p_go_update_common_freqs(wpa_s);
10195
10196 freq = wpa_s->current_ssid->frequency;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -070010197 dfs_offload = (wpa_s->drv_flags & WPA_DRIVER_FLAGS_DFS_OFFLOAD) &&
Roshan Pius3a1667e2018-07-03 15:17:14 -070010198 ieee80211_is_dfs(freq, wpa_s->hw.modes, wpa_s->hw.num_modes);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -080010199 for (i = 0, invalid_freq = 0; i < num; i++) {
10200 if (freqs[i].freq == freq) {
10201 flags = freqs[i].flags;
10202
10203 /* The channel is invalid, must change it */
Dmitry Shmidtd2986c22017-10-23 14:22:09 -070010204 if (!p2p_supported_freq_go(wpa_s->global->p2p, freq) &&
10205 !dfs_offload) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -080010206 wpa_dbg(wpa_s, MSG_DEBUG,
10207 "P2P: Freq=%d MHz no longer valid for GO",
10208 freq);
10209 invalid_freq = 1;
10210 }
10211 } else if (freqs[i].flags == 0) {
10212 /* Freq is not used by any other station interface */
10213 continue;
10214 } else if (!p2p_supported_freq(wpa_s->global->p2p,
Dmitry Shmidtd2986c22017-10-23 14:22:09 -070010215 freqs[i].freq) && !dfs_offload) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -080010216 /* Freq is not valid for P2P use cases */
10217 continue;
10218 } else if (wpa_s->conf->p2p_go_freq_change_policy ==
10219 P2P_GO_FREQ_MOVE_SCM) {
10220 policy_move = 1;
10221 } else if (wpa_s->conf->p2p_go_freq_change_policy ==
10222 P2P_GO_FREQ_MOVE_SCM_PEER_SUPPORTS &&
10223 wpas_p2p_go_is_peer_freq(wpa_s, freqs[i].freq)) {
10224 policy_move = 1;
10225 } else if ((wpa_s->conf->p2p_go_freq_change_policy ==
10226 P2P_GO_FREQ_MOVE_SCM_ECSA) &&
10227 wpas_p2p_go_is_peer_freq(wpa_s, freqs[i].freq)) {
10228 if (!p2p_get_group_num_members(wpa_s->p2p_group)) {
10229 policy_move = 1;
10230 } else if ((wpa_s->drv_flags &
10231 WPA_DRIVER_FLAGS_AP_CSA) &&
10232 wpas_p2p_go_clients_support_ecsa(wpa_s)) {
10233 u8 chan;
10234
10235 /*
10236 * We do not support CSA between bands, so move
10237 * GO only within the same band.
10238 */
10239 if (wpa_s->ap_iface->current_mode->mode ==
10240 ieee80211_freq_to_chan(freqs[i].freq,
10241 &chan))
10242 policy_move = 1;
10243 }
10244 }
10245 }
10246
10247 wpa_dbg(wpa_s, MSG_DEBUG,
10248 "P2P: GO move: invalid_freq=%u, policy_move=%u, flags=0x%X",
10249 invalid_freq, policy_move, flags);
10250
10251 /*
10252 * The channel is valid, or we are going to have a policy move, so
10253 * cancel timeout.
10254 */
10255 if (!invalid_freq || policy_move) {
10256 wpa_dbg(wpa_s, MSG_DEBUG,
10257 "P2P: Cancel a GO move from freq=%d MHz", freq);
10258 eloop_cancel_timeout(wpas_p2p_move_go, wpa_s, NULL);
10259
10260 if (wpas_p2p_in_progress(wpa_s)) {
10261 wpa_dbg(wpa_s, MSG_DEBUG,
10262 "P2P: GO move: policy CS is not allowed - setting timeout to re-consider GO move");
10263 eloop_cancel_timeout(wpas_p2p_reconsider_moving_go,
10264 wpa_s, NULL);
10265 eloop_register_timeout(P2P_RECONSIDER_GO_MOVE_DELAY, 0,
10266 wpas_p2p_reconsider_moving_go,
10267 wpa_s, NULL);
10268 return;
10269 }
10270 }
10271
10272 if (!invalid_freq && (!policy_move || flags != 0)) {
10273 wpa_dbg(wpa_s, MSG_DEBUG,
10274 "P2P: Not initiating a GO frequency change");
10275 return;
10276 }
10277
10278 /*
10279 * Do not consider moving GO if it is in the middle of a CSA. When the
10280 * CSA is finished this flow should be retriggered.
10281 */
10282 if (hostapd_csa_in_progress(wpa_s->ap_iface)) {
10283 wpa_dbg(wpa_s, MSG_DEBUG,
10284 "P2P: Not initiating a GO frequency change - CSA is in progress");
10285 return;
10286 }
10287
10288 if (invalid_freq && !wpas_p2p_disallowed_freq(wpa_s->global, freq))
10289 timeout = P2P_GO_FREQ_CHANGE_TIME;
10290 else
10291 timeout = 0;
10292
10293 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Move GO from freq=%d MHz in %d secs",
10294 freq, timeout);
10295 eloop_cancel_timeout(wpas_p2p_move_go, wpa_s, NULL);
10296 eloop_register_timeout(timeout, 0, wpas_p2p_move_go, wpa_s, NULL);
10297}
10298
10299
10300static void wpas_p2p_consider_moving_gos(struct wpa_supplicant *wpa_s,
10301 struct wpa_used_freq_data *freqs,
10302 unsigned int num,
10303 enum wpas_p2p_channel_update_trig trig)
10304{
10305 struct wpa_supplicant *ifs;
10306
10307 eloop_cancel_timeout(wpas_p2p_reconsider_moving_go, ELOOP_ALL_CTX,
10308 NULL);
10309
10310 /*
10311 * Travers all the radio interfaces, and for each GO interface, check
10312 * if there is a need to move the GO from the frequency it is using,
10313 * or in case the frequency is valid again, cancel the evacuation flow.
10314 */
10315 dl_list_for_each(ifs, &wpa_s->radio->ifaces, struct wpa_supplicant,
10316 radio_list) {
10317 if (ifs->current_ssid == NULL ||
10318 ifs->current_ssid->mode != WPAS_MODE_P2P_GO)
10319 continue;
10320
10321 /*
10322 * The GO was just started or completed channel switch, no need
10323 * to move it.
10324 */
10325 if (wpa_s == ifs &&
10326 (trig == WPAS_P2P_CHANNEL_UPDATE_STATE_CHANGE ||
10327 trig == WPAS_P2P_CHANNEL_UPDATE_CS)) {
10328 wpa_dbg(wpa_s, MSG_DEBUG,
10329 "P2P: GO move - schedule re-consideration");
10330 eloop_register_timeout(P2P_RECONSIDER_GO_MOVE_DELAY, 0,
10331 wpas_p2p_reconsider_moving_go,
10332 wpa_s, NULL);
10333 continue;
10334 }
10335
10336 wpas_p2p_consider_moving_one_go(ifs, freqs, num);
10337 }
10338}
10339
10340
10341void wpas_p2p_indicate_state_change(struct wpa_supplicant *wpa_s)
10342{
10343 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
10344 return;
10345
10346 wpas_p2p_update_channel_list(wpa_s,
10347 WPAS_P2P_CHANNEL_UPDATE_STATE_CHANGE);
10348}
10349
10350
Dmitry Shmidt43cb5782014-06-16 16:23:22 -070010351void wpas_p2p_deinit_iface(struct wpa_supplicant *wpa_s)
10352{
Dmitry Shmidt43cb5782014-06-16 16:23:22 -070010353 if (wpa_s == wpa_s->global->p2p_init_wpa_s && wpa_s->global->p2p) {
10354 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Disable P2P since removing "
10355 "the management interface is being removed");
10356 wpas_p2p_deinit_global(wpa_s->global);
10357 }
10358}
10359
10360
10361void wpas_p2p_ap_deinit(struct wpa_supplicant *wpa_s)
10362{
10363 if (wpa_s->ap_iface->bss)
10364 wpa_s->ap_iface->bss[0]->p2p_group = NULL;
10365 wpas_p2p_group_deinit(wpa_s);
10366}
Dmitry Shmidt58d12ad2016-07-28 10:07:03 -070010367
10368
10369int wpas_p2p_lo_start(struct wpa_supplicant *wpa_s, unsigned int freq,
10370 unsigned int period, unsigned int interval,
10371 unsigned int count)
10372{
10373 struct p2p_data *p2p = wpa_s->global->p2p;
10374 u8 *device_types;
10375 size_t dev_types_len;
10376 struct wpabuf *buf;
10377 int ret;
10378
10379 if (wpa_s->p2p_lo_started) {
10380 wpa_dbg(wpa_s, MSG_DEBUG,
10381 "P2P Listen offload is already started");
10382 return 0;
10383 }
10384
10385 if (wpa_s->global->p2p == NULL ||
10386 !(wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_LISTEN_OFFLOAD)) {
10387 wpa_printf(MSG_DEBUG, "P2P: Listen offload not supported");
10388 return -1;
10389 }
10390
10391 if (!p2p_supported_freq(wpa_s->global->p2p, freq)) {
10392 wpa_printf(MSG_ERROR, "P2P: Input channel not supported: %u",
10393 freq);
10394 return -1;
10395 }
10396
10397 /* Get device type */
10398 dev_types_len = (wpa_s->conf->num_sec_device_types + 1) *
10399 WPS_DEV_TYPE_LEN;
10400 device_types = os_malloc(dev_types_len);
10401 if (!device_types)
10402 return -1;
10403 os_memcpy(device_types, wpa_s->conf->device_type, WPS_DEV_TYPE_LEN);
10404 os_memcpy(&device_types[WPS_DEV_TYPE_LEN], wpa_s->conf->sec_device_type,
10405 wpa_s->conf->num_sec_device_types * WPS_DEV_TYPE_LEN);
10406
10407 /* Get Probe Response IE(s) */
10408 buf = p2p_build_probe_resp_template(p2p, freq);
10409 if (!buf) {
10410 os_free(device_types);
10411 return -1;
10412 }
10413
10414 ret = wpa_drv_p2p_lo_start(wpa_s, freq, period, interval, count,
10415 device_types, dev_types_len,
10416 wpabuf_mhead_u8(buf), wpabuf_len(buf));
10417 if (ret < 0)
10418 wpa_dbg(wpa_s, MSG_DEBUG,
10419 "P2P: Failed to start P2P listen offload");
10420
10421 os_free(device_types);
10422 wpabuf_free(buf);
10423
10424 if (ret == 0) {
10425 wpa_s->p2p_lo_started = 1;
10426
10427 /* Stop current P2P listen if any */
10428 wpas_stop_listen(wpa_s);
10429 }
10430
10431 return ret;
10432}
10433
10434
10435int wpas_p2p_lo_stop(struct wpa_supplicant *wpa_s)
10436{
10437 int ret;
10438
10439 if (!wpa_s->p2p_lo_started)
10440 return 0;
10441
10442 ret = wpa_drv_p2p_lo_stop(wpa_s);
10443 if (ret < 0)
10444 wpa_dbg(wpa_s, MSG_DEBUG,
10445 "P2P: Failed to stop P2P listen offload");
10446
10447 wpa_s->p2p_lo_started = 0;
10448 return ret;
10449}
Sunil Ravic0f5d412024-09-11 22:12:49 +000010450
10451
10452struct wpabuf * wpas_p2p_usd_elems(struct wpa_supplicant *wpa_s)
10453{
10454 struct p2p_data *p2p = wpa_s->global->p2p;
10455
10456 if (wpa_s->global->p2p_disabled || !p2p)
10457 return NULL;
10458 return p2p_usd_elems(p2p);
10459}
10460
10461
10462void wpas_p2p_process_usd_elems(struct wpa_supplicant *wpa_s, const u8 *buf,
10463 u16 buf_len, const u8 *peer_addr,
10464 unsigned int freq)
10465{
10466 struct p2p_data *p2p = wpa_s->global->p2p;
10467
10468 if (wpa_s->global->p2p_disabled || !p2p)
10469 return;
10470 p2p_process_usd_elems(p2p, buf, buf_len, peer_addr, freq);
10471}